From 35ab7eb472c9bb3c1d2e4b91f9987fc57f7929fc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 11 Nov 2019 15:47:48 +0530 Subject: [PATCH] Add a new action :ref:`detach_window` that can be used to move the current window into a different tab Fixes #1310 --- docs/changelog.rst | 3 +++ docs/index.rst | 9 +++++++++ kitty/boss.py | 30 ++++++++++++++++++++++++++---- kitty/config.py | 4 +++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index ddcc15814..cb8e09deb 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -7,6 +7,9 @@ To update |kitty|, :doc:`follow the instructions `. 0.15.0 [future] -------------------- +- Add a new action :ref:`detach_window` that can be used to move the current + window into a different tab (:iss:`1310`) + - Add a new style ``powerline`` for tab bar rendering, see :opt:`tab_bar_style` (:pull:`2021`) - Allow changing colors by mapping a keyboard shortcut to read a kitty config diff --git a/docs/index.rst b/docs/index.rst index 28a97370a..6522ad2af 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -173,6 +173,15 @@ You can also define a shortcut to switch to the previously active window:: ``nth_window`` will focus the nth window for positive numbers and the previously active windows for negative numbers. +.. _detach_window: + +Finally you can define shortcuts to detach the current window and +move it to another tab or another OS window:: + + map ctrl+f2 detach_window # moves the window into a new OS window + map ctrl+f3 detach_window new-tab # moves the window into a new Tab + map ctrl+f4 detach_window ask # asks which tab to move the window into + Other keyboard shortcuts ---------------------------------- diff --git a/kitty/boss.py b/kitty/boss.py index 0f796976f..1facbcda1 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -675,7 +675,7 @@ class Boss: output += str(s.linebuf.line(i)) return output - def _run_kitten(self, kitten, args=(), input_data=None, window=None): + def _run_kitten(self, kitten, args=(), input_data=None, window=None, custom_callback=None): orig_args, args = list(args), list(args) from kittens.runner import create_kitten_handler end_kitten = create_kitten_handler(kitten, orig_args) @@ -724,7 +724,7 @@ class Boss: ), copy_colors_from=w ) - overlay_window.action_on_close = partial(self.on_kitten_finish, w.id, end_kitten) + overlay_window.action_on_close = partial(self.on_kitten_finish, w.id, custom_callback or end_kitten) return overlay_window def kitten(self, kitten, *args): @@ -1142,6 +1142,28 @@ class Boss: target_tab.make_active() def detach_window(self, *args): - if not args: + if not args or args[0] == 'new': + return self._move_window_to(target_os_window_id='new') + if args[0] == 'new-tab': return self._move_window_to(target_tab_id='new') - # TODO: Implementthis + lines = [ + 'Choose a tab to move the window to', + '' + ] + tab_id_map = {} + for i, tab in enumerate(self.all_tabs): + tab_id_map[i + 1] = tab.id + lines.append('{} {}'.format(i + 1, tab.title)) + + def done(data, target_window_id, self): + target_window = None + for w in self.all_windows: + if w.id == target_window_id: + target_window = w + break + tab_id = tab_id_map[int(data['match'][0].partition(' ')[0])] + self._move_window_to(window=target_window, target_tab_id=tab_id) + + self._run_kitten( + 'hints', args=('--type=regex', r'--regex=(?m)^\d+ .+$',), + input_data='\r\n'.join(lines).encode('utf-8'), custom_callback=done) diff --git a/kitty/config.py b/kitty/config.py index c5576e6e4..70cdc1104 100644 --- a/kitty/config.py +++ b/kitty/config.py @@ -94,7 +94,9 @@ def goto_tab_parse(func, rest): @func_with_args('detach_window') def detach_window_parse(func, rest): - return func, to_cmdline(rest) + if rest not in ('new', 'new-tab', 'ask'): + rest = 'new' + return func, (rest,) @func_with_args('set_background_opacity', 'goto_layout', 'kitty_shell')