From f03a665e094ee42e0889a488bc527c220382eba5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 10 Dec 2022 11:17:55 +0530 Subject: [PATCH] Remote control: When matching windows allow using negative id numbers to match recently created windows Fixes #5753 --- docs/changelog.rst | 2 ++ kitty/boss.py | 18 ++++++++++++++++++ kitty/rc/base.py | 6 ++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index da8eb572f..5de5ecc52 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -67,6 +67,8 @@ Detailed list of changes - Wayland: Fix signal handling not working with some GPU drivers (:iss:`4636`) +- Remote control: When matching windows allow using negative id numbers to match recently created windows (:iss:`5753`) + 0.26.5 [2022-11-07] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/boss.py b/kitty/boss.py index 328e36283..87d29ab98 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -379,8 +379,16 @@ class Boss: tm = self.os_window_map.get(last_focused_os_window_id()) if tm is not None: tab = tm.active_tab + window_id_limit = max(self.window_id_map, default=-1) + 1 def get_matches(location: str, query: str, candidates: Set[int]) -> Set[int]: + if location == 'id' and query.startswith('-'): + try: + q = int(query) + except Exception: + return set() + if q < 0: + query = str(window_id_limit + q) return {wid for wid in candidates if self.window_id_map[wid].matches_query(location, query, tab)} for wid in search(match, ( @@ -403,8 +411,18 @@ class Boss: if current_focused_os_window_id() <= 0: tm = self.os_window_map.get(last_focused_os_window_id()) or tm tim = {t.id: t for t in self.all_tabs} + tab_id_limit = max(tim, default=-1) + 1 + window_id_limit = max(self.window_id_map, default=-1) + 1 def get_matches(location: str, query: str, candidates: Set[int]) -> Set[int]: + if location in ('id', 'window_id') and query.startswith('-'): + try: + q = int(query) + except Exception: + return set() + if q < 0: + limit = tab_id_limit if location == 'id' else window_id_limit + query = str(limit + q) return {wid for wid in candidates if tim[wid].matches_query(location, query, tm)} found = False diff --git a/kitty/rc/base.py b/kitty/rc/base.py index 91a26361f..c614a7de8 100644 --- a/kitty/rc/base.py +++ b/kitty/rc/base.py @@ -103,7 +103,8 @@ Where :italic:`field` can be one of: :code:`id`, :code:`title`, :code:`pid`, :co The special value :code:`all` matches all windows. For numeric fields: :code:`id`, :code:`pid`, :code:`num` and :code:`recent`, the expression is interpreted as -a number, not a regular expression. +a number, not a regular expression. Negative values for :code:`id` match from the highest id number down, in particular, +-1 is the most recently created window. The field :code:`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 :ref:`kitty @ ls ` command). @@ -134,7 +135,8 @@ Where :italic:`field` can be one of: :code:`id`, :code:`index`, :code:`title`, : The special value :code:`all` matches all tabs. For numeric fields: :code:`id`, :code:`index`, :code:`window_id`, :code:`pid` and :code:`recent`, the -expression is interpreted as a number, not a regular expression. +expression is interpreted as a number, not a regular expression. Negative values for :code:`id`/:code:`window_id` match +from the highest id number down, in particular, -1 is the most recently created tab/window. When using :code:`title` or :code:`id`, first a matching tab is looked for, and if not found a matching window is looked for, and the tab for that window is used.