diff --git a/docs/changelog.rst b/docs/changelog.rst index b704e38ba..cce4440c0 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -28,6 +28,9 @@ To update |kitty|, :doc:`follow the instructions `. - Allow setting OS window size in session files +- Add an option :opt:`tab_switch_strategy` to control which + tab becomes active when the current tab is closed (:pull:`1524`) + - Allow specifying a value of ``none`` for the :opt:`selection_foreground` which will cause kitty to not change text color in selections (:iss:`1358`) diff --git a/kitty/config_data.py b/kitty/config_data.py index 198dbf277..e9cfea8bf 100644 --- a/kitty/config_data.py +++ b/kitty/config_data.py @@ -631,6 +631,13 @@ o('tab_bar_min_tabs', 2, option_type=lambda x: max(1, positive_int(x)), long_tex The minimum number of tabs that must exist before the tab bar is shown ''')) +o('tab_switch_strategy', 'previous', option_type=choices('previous', 'left', 'last'), long_text=_(''' +The algorithm to use when switching to a tab when the current tab is closed. +The default of :code:`previous` will switch to the last used tab. A value of +:code:`left` will switch to the tab to the left of the closed tab. A value +of :code:`last` will switch to the right-most tab. +''')) + def tab_fade(x): return tuple(map(unit_float, x.split())) diff --git a/kitty/tabs.py b/kitty/tabs.py index 8b7d5ca39..71cc08874 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -552,16 +552,25 @@ class TabManager: # {{{ def remove(self, tab): self._remove_tab(tab) next_active_tab = -1 - while self.active_tab_history and next_active_tab < 0: - tab_id = self.active_tab_history.pop() - if tab_id == tab.id: - continue - for idx, qtab in enumerate(self.tabs): - if qtab.id == tab_id: - next_active_tab = idx - break + while True: + try: + self.active_tab_history.remove(tab.id) + except ValueError: + break + + if self.opts.tab_switch_strategy == 'previous': + while self.active_tab_history and next_active_tab < 0: + tab_id = self.active_tab_history.pop() + for idx, qtab in enumerate(self.tabs): + if qtab.id == tab_id: + next_active_tab = idx + break + elif self.opts.tab_switch_strategy == 'left': + next_active_tab = max(0, self.active_tab_idx - 1) + if next_active_tab < 0: next_active_tab = max(0, min(self.active_tab_idx, len(self.tabs) - 1)) + self._set_active_tab(next_active_tab) self.mark_tab_bar_dirty() tab.destroy()