Remote control: Allow matching windows by number (visible position).

Fixes #501
This commit is contained in:
Kovid Goyal 2018-04-28 11:00:42 +05:30
parent 759aaf294a
commit 040ae75c77
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 26 additions and 8 deletions

View File

@ -149,10 +149,20 @@ class Boss:
field, exp = match.split(':', 1) field, exp = match.split(':', 1)
except ValueError: except ValueError:
return return
pat = re.compile(exp) if field == 'num':
for window in self.all_windows: tab = self.active_tab
if window.matches(field, pat): if tab is not None:
yield window 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): def tab_for_window(self, window):
for tab in self.all_tabs: for tab in self.all_tabs:

View File

@ -39,10 +39,12 @@ def cmd(short_desc, desc=None, options_spec=None, no_response=False, argspec='..
MATCH_WINDOW_OPTION = '''\ MATCH_WINDOW_OPTION = '''\
--match -m --match -m
The window to match. Match specifications are of the form: 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 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, numeric fields such as id, pid and num the expression is interpreted as a number,
not a regular expression. 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_TAB_OPTION = '''\
--match -m --match -m

View File

@ -75,9 +75,11 @@ class Layout:
# this layout, i.e. spaces that are not covered by any window # this layout, i.e. spaces that are not covered by any window
self.blank_rects = () 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] windows = process_overlaid_windows(all_windows)[1]
w = windows[min(num, len(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) active_window_idx = idx_for_id(w.id, all_windows)
return self.set_active_window(all_windows, active_window_idx) return self.set_active_window(all_windows, active_window_idx)

View File

@ -183,6 +183,10 @@ class Tab: # {{{
return return
self.set_active_window_idx(idx) 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): def nth_window(self, num=0):
if self.windows: if self.windows:
self.active_window_idx = self.current_layout.nth_window(self.windows, num) self.active_window_idx = self.current_layout.nth_window(self.windows, num)