Allow matching against session name in the kitty match language

This commit is contained in:
Kovid Goyal
2025-08-19 16:35:06 +05:30
parent 6d21ebd383
commit 68fdf3f65b
3 changed files with 12 additions and 2 deletions

View File

@@ -91,7 +91,7 @@ MATCH_WINDOW_OPTION = '''\
--match -m
The window to match. Match specifications are of the form: :italic:`field:query`.
Where :italic:`field` can be one of: :code:`id`, :code:`title`, :code:`pid`, :code:`cwd`, :code:`cmdline`, :code:`num`,
:code:`env`, :code:`var`, :code:`state`, :code:`neighbor`, and :code:`recent`.
:code:`env`, :code:`var`, :code:`state`, :code:`neighbor`, :code:`session` and :code:`recent`.
:italic:`query` is the expression to match. Expressions can be either a number or a regular expression, and can be
:ref:`combined using Boolean operators <search_syntax>`.
@@ -112,6 +112,9 @@ active window, one being the previously active window and so on.
The field :code:`neighbor` refers to a neighbor of the active window in the specified direction, which can be:
:code:`left`, :code:`right`, :code:`top` or :code:`bottom`.
The field :code:`session` matches windows that were created in the specified session.
Use the expression :code:`^$` to match windows that were not created in a session.
When using the :code:`env` field to match on environment variables, you can specify only the environment variable name
or a name and value, for example, :code:`env:MY_ENV_VAR=2`.
@@ -135,7 +138,7 @@ MATCH_TAB_OPTION = '''\
--match -m
The tab to match. Match specifications are of the form: :italic:`field:query`.
Where :italic:`field` can be one of: :code:`id`, :code:`index`, :code:`title`, :code:`window_id`, :code:`window_title`,
:code:`pid`, :code:`cwd`, :code:`cmdline` :code:`env`, :code:`var`, :code:`state` and :code:`recent`.
:code:`pid`, :code:`cwd`, :code:`cmdline` :code:`env`, :code:`var`, :code:`state`, :code:`session` and :code:`recent`.
:italic:`query` is the expression to match. Expressions can be either a number or a regular expression, and can be
:ref:`combined using Boolean operators <search_syntax>`.
@@ -155,6 +158,9 @@ The :code:`index` number is used to match the nth tab in the currently active OS
The :code:`recent` number matches recently active tabs in the currently active OS window, with zero being the currently
active tab, one the previously active tab and so on.
The field :code:`session` matches tabs that were created in the specified session.
Use the expression :code:`^$` to match tabs that were not created in a session.
When using the :code:`env` field to match on environment variables, you can specify only the environment variable name
or a name and value, for example, :code:`env:MY_ENV_VAR=2`. Tabs containing any window with the specified environment
variables are matched. Similarly, :code:`var` matches tabs containing any window with the specified user variable.

View File

@@ -956,6 +956,8 @@ class Tab: # {{{
if query == 'parent_focused':
return active_tab_manager is not None and self.tab_manager_ref() is active_tab_manager and self.os_window_id == last_focused_os_window_id()
return False
if field == 'session':
return re.search(query, self.created_in_session_name) is not None
return False
def __iter__(self) -> Iterator[Window]:

View File

@@ -909,6 +909,8 @@ class Window:
if pat.search(x) is not None:
return True
return False
if field == 'session':
return pat.search(self.created_in_session_name) is not None
return False
def matches_query(self, field: str, query: str, active_tab: TabType | None = None, self_window: Optional['Window'] = None) -> bool: