From 23f2b29069c13dd2053219adf363b16fbe3e0d1e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 10 Jan 2018 12:36:15 +0530 Subject: [PATCH] Add --keep-focus for @new-window --- kitty/boss.py | 10 ++++++++++ kitty/remote_control.py | 15 +++++++++++++-- kitty/tabs.py | 15 +++++++++++---- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 3e3306309..d891a8d3a 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -136,6 +136,16 @@ class Boss: if tab: yield tab + def set_active_window(self, window): + for tm in self.os_window_map.values(): + for tab in tm: + for w in tab: + if w.id == window.id: + if tab is not self.active_tab: + tm.set_active_tab(tab) + tab.set_active_window(w) + return + def _new_os_window(self, args, cwd_from=None): sw = self.args_to_special_window(args, cwd_from) if args else None startup_session = create_session(self.opts, special_window=sw, cwd_from=cwd_from) diff --git a/kitty/remote_control.py b/kitty/remote_control.py index 7a85fb86d..d53f0ffd5 100644 --- a/kitty/remote_control.py +++ b/kitty/remote_control.py @@ -281,6 +281,11 @@ program running in it. The initial working directory for the new window. +--keep-focus +type=bool-set +Keep the current window focused instead of switching to the newly opened window + + --new-tab type=bool-set Open a new tab @@ -293,17 +298,21 @@ When using --new-tab set the title of the tab. def cmd_new_window(global_opts, opts, args): return {'match': opts.match, 'title': opts.title, 'cwd': opts.cwd, 'new_tab': opts.new_tab, 'tab_title': opts.tab_title, - 'args': args or []} + 'keep_focus': opts.keep_focus, 'args': args or []} def new_window(boss, window, payload): w = SpecialWindow(cmd=payload['args'] or None, override_title=payload['title'], cwd=payload['cwd']) + old_window = boss.active_window if payload['new_tab']: boss._new_tab(w) tab = boss.active_tab if payload['tab_title']: tab.set_title(payload['tab_title']) - return str(boss.active_window.id) + wid = boss.active_window.id + if payload['keep_focus'] and old_window: + boss.set_active_window(old_window) + return str(wid) match = payload['match'] if match: @@ -314,6 +323,8 @@ def new_window(boss, window, payload): tabs = [boss.active_tab] tab = tabs[0] w = tab.new_special_window(w) + if payload['keep_focus'] and old_window: + boss.set_active_window(old_window) return str(w.id) diff --git a/kitty/tabs.py b/kitty/tabs.py index af13bf6c3..e7b0ecc3b 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -376,18 +376,25 @@ class TabManager: # {{{ for tab in self.tabs: tab.relayout() - def set_active_tab(self, idx): + def set_active_tab_idx(self, idx): self._set_active_tab(idx) self.active_tab.relayout_borders() self.update_tab_bar() + def set_active_tab(self, tab): + try: + idx = self.tabs.index(tab) + except Exception: + return + self.set_active_tab_idx(idx) + def next_tab(self, delta=1): if len(self.tabs) > 1: - self.set_active_tab((self.active_tab_idx + len(self.tabs) + delta) % len(self.tabs)) + self.set_active_tab_idx((self.active_tab_idx + len(self.tabs) + delta) % len(self.tabs)) def goto_tab(self, tab_num): if tab_num < len(self.tabs) and 0 <= tab_num: - self.set_active_tab(tab_num) + self.set_active_tab_idx(tab_num) def __iter__(self): return iter(self.tabs) @@ -460,7 +467,7 @@ class TabManager: # {{{ def activate_tab_at(self, x): i = self.tab_bar.tab_at(x) if i is not None: - self.set_active_tab(i) + self.set_active_tab_idx(i) @property def blank_rects(self):