From 76a4840a0f597cf102366f44ad01727cf41daf57 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 7 Mar 2024 11:32:01 +0530 Subject: [PATCH] toggle_tab to easily switch to and back from a tab Fixes #7203 --- docs/changelog.rst | 2 ++ kitty/options/utils.py | 2 +- kitty/tabs.py | 26 +++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index ac10c4a43..394321db8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -73,6 +73,8 @@ Detailed list of changes - icat kitten: Add a command line argument to override terminal window size detection (:iss:`7165`) +- A new action :ac:`toggle_tab` to easily switch to and back from a tab with a single shortcut (:iss:`7203`) + - When :ac:`clearing terminal ` add a new type ``to_cursor_scroll`` which can be used to clear to prompt while moving cleared lines into the scrollback diff --git a/kitty/options/utils.py b/kitty/options/utils.py index d00ccd6d8..fd9449bc0 100644 --- a/kitty/options/utils.py +++ b/kitty/options/utils.py @@ -169,7 +169,7 @@ def detach_tab_parse(func: str, rest: str) -> FuncArgsType: return func, (rest,) -@func_with_args('set_background_opacity', 'goto_layout', 'toggle_layout', 'kitty_shell', 'show_kitty_doc', 'set_tab_title', 'push_keyboard_mode') +@func_with_args('set_background_opacity', 'goto_layout', 'toggle_layout', 'toggle_tab', 'kitty_shell', 'show_kitty_doc', 'set_tab_title', 'push_keyboard_mode') def simple_parse(func: str, rest: str) -> FuncArgsType: return func, [rest] diff --git a/kitty/tabs.py b/kitty/tabs.py index e5eafc402..53970cc7f 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -7,6 +7,7 @@ import stat import weakref from collections import deque from contextlib import suppress +from gettext import gettext as _ from operator import attrgetter from time import monotonic from typing import ( @@ -870,7 +871,6 @@ class TabManager: # {{{ self.wm_class = wm_class self.recent_mouse_events: Deque[TabMouseEvent] = deque() self.wm_name = wm_name - self.last_active_tab_id = None self.args = args self.tab_bar_hidden = get_options().tab_bar_style == 'hidden' self.tabs: List[Tab] = [] @@ -990,6 +990,30 @@ class TabManager: # {{{ if len(self.tabs) > 1: self.set_active_tab_idx((self.active_tab_idx + len(self.tabs) + delta) % len(self.tabs)) + @ac('win', ''' + Toggle to the tab matching the specified expression + + Switches to the matching tab if another tab is current, otherwise + switches to the last used tab. Useful to easily switch to and back from a + tab using a single shortcut. Note that toggling works only between + tabs in the same OS window. See :ref:`search_syntax` for details + on the match expression. For example:: + + map f1 toggle_tab title:mytab + ''') + def toggle_tab(self, match_expression: str) -> None: + tabs = set(get_boss().match_tabs(match_expression)) & set(self) + if not tabs: + get_boss().show_error(_('No matching tab'), _('No tab found matching the expression: {}').format(match_expression)) + return + if self.active_tab and self.active_tab in tabs: + self.goto_tab(-1) + else: + for x in self: + if x in tabs: + self.set_active_tab(x) + break + def tab_at_location(self, loc: str) -> Optional[Tab]: if loc == 'prev': if self.active_tab_history: