From 040ae75c772e4a68ff7df1279a11b0c6b7280ae5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 28 Apr 2018 11:00:42 +0530 Subject: [PATCH] Remote control: Allow matching windows by number (visible position). Fixes #501 --- kitty/boss.py | 18 ++++++++++++++---- kitty/cmds.py | 8 +++++--- kitty/layout.py | 4 +++- kitty/tabs.py | 4 ++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index c6e1c7615..af15a7f14 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -149,10 +149,20 @@ class Boss: field, exp = match.split(':', 1) except ValueError: return - pat = re.compile(exp) - for window in self.all_windows: - if window.matches(field, pat): - yield window + if field == 'num': + tab = self.active_tab + if tab is not None: + try: + w = tab.get_nth_window(int(exp)) + except Exception: + return + if w is not None: + yield w + else: + pat = re.compile(exp) + for window in self.all_windows: + if window.matches(field, pat): + yield window def tab_for_window(self, window): for tab in self.all_tabs: diff --git a/kitty/cmds.py b/kitty/cmds.py index 28e45cfe9..67a5a41de 100644 --- a/kitty/cmds.py +++ b/kitty/cmds.py @@ -39,10 +39,12 @@ def cmd(short_desc, desc=None, options_spec=None, no_response=False, argspec='.. MATCH_WINDOW_OPTION = '''\ --match -m The window to match. Match specifications are of the form: -|_ field:regexp|. Where field can be one of: id, title, pid, cwd, cmdline. +|_ field:regexp|. Where field can be one of: id, title, pid, cwd, cmdline, num. You can use the |_ ls| command to get a list of windows. Note that for -numeric fields such as id and pid the expression is interpreted as a number, -not a regular expression. +numeric fields such as id, pid and num the expression is interpreted as a number, +not a regular expression. The field num refers to the window position in the current tab, +starting from zero and counting clockwise (this is the same as the order in which the +windows are reported by the |_ ls| command). ''' MATCH_TAB_OPTION = '''\ --match -m diff --git a/kitty/layout.py b/kitty/layout.py index 6978d1cae..8d31e01b3 100644 --- a/kitty/layout.py +++ b/kitty/layout.py @@ -75,9 +75,11 @@ class Layout: # this layout, i.e. spaces that are not covered by any window self.blank_rects = () - def nth_window(self, all_windows, num): + def nth_window(self, all_windows, num, make_active=True): windows = process_overlaid_windows(all_windows)[1] w = windows[min(num, len(windows) - 1)] + if not make_active: + return w active_window_idx = idx_for_id(w.id, all_windows) return self.set_active_window(all_windows, active_window_idx) diff --git a/kitty/tabs.py b/kitty/tabs.py index 9e38c8992..7f1a6fb10 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -183,6 +183,10 @@ class Tab: # {{{ return self.set_active_window_idx(idx) + def get_nth_window(self, n): + if self.windows: + return self.current_layout.nth_window(self.windows, n, make_active=False) + def nth_window(self, num=0): if self.windows: self.active_window_idx = self.current_layout.nth_window(self.windows, num)