From 47b4c94c618e24ae7063d84aa5cba8b3cf6d214f Mon Sep 17 00:00:00 2001 From: Jackie Li Date: Sun, 26 Oct 2025 11:08:43 +0000 Subject: [PATCH] implement --base-dir and --focus-tab to save_as_session --- kitty/session.py | 14 ++++++++++++++ kitty/tabs.py | 9 ++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/kitty/session.py b/kitty/session.py index e6e175dd4..e6eb3a898 100644 --- a/kitty/session.py +++ b/kitty/session.py @@ -627,6 +627,16 @@ If specified, only save all windows (and their parent tabs/OS Windows) that matc search expression. See :ref:`search_syntax` for details on the search language. In particular if you want to only save windows that are present in the currently active session, use :code:`--match=session:.`. + + +--base-dir +When specified, relative session filenames will be saved to this directory instead of the current +working directory. Absolute paths are not affected by this option. + + +--focus-tab +type=bool-set +When enabled, add a focus_tab command to the saved session file to preserve the currently active tab. ''' @@ -634,6 +644,10 @@ def save_as_session_part2(boss: BossType, opts: SaveAsSessionOptions, path: str) if not path: return from .config import atomic_save + # Handle --base-dir option + if opts.base_dir and not os.path.isabs(path): + base_dir = os.path.abspath(os.path.expanduser(opts.base_dir)) + path = os.path.join(base_dir, path) path = os.path.abspath(os.path.expanduser(path)) session = '\n'.join(boss.serialize_state_as_session(path, opts)) os.makedirs(os.path.dirname(path), exist_ok=True) diff --git a/kitty/tabs.py b/kitty/tabs.py index d2eafa2f3..1baf10a34 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -1317,7 +1317,10 @@ class TabManager: # {{{ hmap[at.id] = len(self.active_tab_history) + 1 def skey(tab: Tab) -> int: return hmap.get(tab.id, -1) - for tab in sorted(self, key=skey): + active_tab_index = -1 + for i, tab in enumerate(sorted(self, key=skey)): + if tab is self.active_tab: + active_tab_index = i ans.extend(tab.serialize_state_as_session(session_path, matched_windows, ser_opts)) if ans: prefix = [] if is_first else ['', '', 'new_os_window'] @@ -1326,6 +1329,10 @@ class TabManager: # {{{ if self.wm_name and self.wm_name != appname: prefix.append(f'os_window_name {self.wm_name}') ans = prefix + ans + # Add focus_tab command if --focus-tab option is enabled + if ser_opts.focus_tab and active_tab_index >= 0: + ans.append('') + ans.append(f'focus_tab {active_tab_index}') return ans @property