diff --git a/kittens/url_hints/main.py b/kittens/url_hints/main.py index 7027e244a..8c29dc566 100644 --- a/kittens/url_hints/main.py +++ b/kittens/url_hints/main.py @@ -176,12 +176,16 @@ def run_loop(args, lines, index_map): handler = URLHints(lines, index_map) loop.loop(handler) if handler.chosen and loop.return_code == 0: - cmd = command_for_open(args.program) - ret = subprocess.Popen(cmd + [handler.chosen]).wait() - if ret != 0: - print('URL handler "{}" failed with return code: {}'.format(' '.join(cmd), ret), file=sys.stderr) - input('Press Enter to quit') - loop.return_code = ret + if args.in_kitty: + import json + print('OK:', json.dumps({'url': handler.chosen, 'program': args.program, 'action': 'open_with'})) + else: + cmd = command_for_open(args.program) + ret = subprocess.Popen(cmd + [handler.chosen]).wait() + if ret != 0: + print('URL handler "{}" failed with return code: {}'.format(' '.join(cmd), ret), file=sys.stderr) + input('Press Enter to quit') + loop.return_code = ret raise SystemExit(loop.return_code) @@ -233,6 +237,12 @@ expression instead. default={0} Comma separated list of recognized URL prefixes. Defaults to: {0} + + +--in-kitty +type=bool-set +Output the URL instead of opening it. Intended for use from within +kitty. '''.format, ','.join(sorted(URL_PREFIXES))) diff --git a/kitty/boss.py b/kitty/boss.py index ea30a17d7..12b7bea90 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -471,6 +471,8 @@ class Boss: def get_output(self, source_window, num_lines=1): output = '' s = source_window.screen + if num_lines is None: + num_lines = s.lines for i in range(min(num_lines, s.lines)): output += str(s.linebuf.line(i)) return output @@ -517,19 +519,27 @@ class Boss: if w is not None and tab is not None and w.overlay_for is None: cmdline = args[0] if args else '' args = shlex.split(cmdline) if cmdline else [] - if '--program' not in cmdline: - args.extend(('--program', self.opts.open_url_with)) + if kitten == 'url_hints': + args[0:0] = ['--in-kitty', '--program', self.opts.open_url_with] if type_of_input in ('text', 'history', 'ansi', 'ansi-history'): data = w.as_text(as_ansi='ansi' in type_of_input, add_history='history' in type_of_input).encode('utf-8') elif type_of_input == 'none': data = None else: raise ValueError('Unknown type_of_input: {}'.format(type_of_input)) - tab.new_special_window( + overlay_window = tab.new_special_window( SpecialWindow( ['kitty', '+runpy', 'from kittens.{}.main import main; main()'.format(kitten)] + args, stdin=data, overlay_for=w.id)) + if kitten == 'url_hints': + overlay_window.action_on_close = self.open_hinted_url + + def open_hinted_url(self, source_window): + output = self.get_output(source_window, num_lines=None) + if output.startswith('OK: '): + cmd = json.loads(output.partition(' ')[2].strip()) + open_url(cmd['url'], cmd['program']) def switch_focus_to(self, window_idx): tab = self.active_tab