diff --git a/docs/sessions.rst b/docs/sessions.rst index 5c997f4..0ff91b1 100644 --- a/docs/sessions.rst +++ b/docs/sessions.rst @@ -48,6 +48,10 @@ To restore it, start kitty with: kitty --session=last +The same saved state can be restored inside a running kitty with the +:ac:`restore_last_session` action, which is mapped to :kbd:`kitty_mod+alt+r` +by default and adds the saved tabs to the current window. + Restoring is never automatic: without this option the saved state is ignored, so an unexpected state is never imposed on you. A state file that is corrupt, unreadable or too large is discarded and kitty starts normally. :code:`cd` diff --git a/kitty/boss.py b/kitty/boss.py index 9e0fa72..0030c6e 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -3959,6 +3959,13 @@ class Boss: def goto_session(self, *cmdline: str) -> None: goto_session(self, cmdline) + @ac('session', 'Restore the tabs and windows of the session saved when kitty last quit. See :ref:`sessions`.') + def restore_last_session(self) -> None: + from .session_restore import restore_last_session_into + + if not restore_last_session_into(self): + self.ring_bell_if_allowed() + @ac('session', 'Save the current kitty state as a session file. See :ref:`save_as_session`.') def save_as_session(self, *cmdline: str) -> None: save_as_session(self, cmdline) diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 76454f8..0078a39 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -4649,6 +4649,23 @@ shortcuts. For a full description see :doc:`hints kitten `. """) # }}} +# shortcuts.session {{{ +agr('shortcuts.session', 'Sessions') + +map( + 'Restore the last session', + 'restore_last_session --allow-fallback=shifted,ascii kitty_mod+alt+r restore_last_session', + long_text=""" +Restores the tabs, windows, layouts and working directories of the session that +was saved when kitty last quit, without restarting kitty. The state is written +by :code:`quit`; see :ref:`sessions`. If there is nothing to restore, a bell is +rung instead. +""", +) + +egr() # }}} + + # shortcuts.misc {{{ agr('shortcuts.misc', 'Miscellaneous') diff --git a/kitty/options/to-c-generated.h b/kitty/options/to-c-generated.h index 7154bf1..5d09627 100644 --- a/kitty/options/to-c-generated.h +++ b/kitty/options/to-c-generated.h @@ -4,6 +4,7 @@ #include "to-c.h" + static void convert_from_python_font_size(PyObject *val, Options *opts) { opts->font_size = PyFloat_AsDouble(val); diff --git a/kitty/options/types.py b/kitty/options/types.py index 25804f5..40a33ea 100644 --- a/kitty/options/types.py +++ b/kitty/options/types.py @@ -1024,12 +1024,14 @@ defaults.map = [ KeyDefinition(is_sequence=True, trigger=SingleKey(mods=256, key=112), rest=(SingleKey(key=110),), options=KeyMapOptions(when_focus_on='', new_mode='', mode='', on_unknown='beep', on_action='keep', timeout=None, allow_fallback=(KeyFallbackType.shifted, KeyFallbackType.alternate)), definition='kitten hints --type linenum'), # open_selected_hyperlink KeyDefinition(is_sequence=True, trigger=SingleKey(mods=256, key=112), rest=(SingleKey(key=121),), options=KeyMapOptions(when_focus_on='', new_mode='', mode='', on_unknown='beep', on_action='keep', timeout=None, allow_fallback=(KeyFallbackType.shifted, KeyFallbackType.alternate)), definition='kitten hints --type hyperlink'), + # restore_last_session + KeyDefinition(trigger=SingleKey(mods=258, key=114), options=KeyMapOptions(when_focus_on='', new_mode='', mode='', on_unknown='beep', on_action='keep', timeout=None, allow_fallback=(KeyFallbackType.shifted, KeyFallbackType.alternate)), definition='restore_last_session'), # show_kitty_doc KeyDefinition(trigger=SingleKey(mods=256, key=57364), definition='show_kitty_doc overview'), # command_palette KeyDefinition(trigger=SingleKey(mods=256, key=57366), definition='command_palette'), # file_manager - KeyDefinition(trigger=SingleKey(mods=5, key=101), definition='launch --type=window-dock --dock-edge=right --dock-size=60 kitten file-manager'), + KeyDefinition(trigger=SingleKey(mods=5, key=101), definition='launch --type=window-dock --dock-edge=left --dock-size=60 kitten file-manager'), # toggle_fullscreen KeyDefinition(trigger=SingleKey(mods=256, key=57374), definition='toggle_fullscreen'), # toggle_maximized diff --git a/kitty/session_restore.py b/kitty/session_restore.py index 539edfc..f50dd0b 100644 --- a/kitty/session_restore.py +++ b/kitty/session_restore.py @@ -10,6 +10,7 @@ import json import os import time +from contextlib import suppress from typing import TYPE_CHECKING, Any from .config import atomic_save @@ -135,3 +136,27 @@ def save_session_of(boss: 'BossType') -> None: save_last_session(session) except Exception as e: log_error(f'Failed to save the last session: {e}') + + +def restore_last_session_into(boss: 'BossType') -> bool: + """Add the tabs and windows of the last saved session to a running kitty. + + Returns False when there is nothing to restore. The saved state is a JSON document, so it is + handed to the session parser through a short lived file rather than duplicating the parsing. + """ + raw = load_last_session() + if not raw: + return False + import tempfile + + from .session import create_session + + fd, path = tempfile.mkstemp(prefix='last-session-', suffix='.kitty-session') + try: + with open(fd, 'w', encoding='utf-8') as f: + f.write(raw) + create_session(boss, path) + finally: + with suppress(OSError): + os.unlink(path) + return True diff --git a/kitty_tests/session_restore.py b/kitty_tests/session_restore.py index d3ad522..6428ef9 100644 --- a/kitty_tests/session_restore.py +++ b/kitty_tests/session_restore.py @@ -138,3 +138,8 @@ class TestSessionRestore(BaseTest): self.ae(len(sessions), 1) self.ae(sessions[0].session_name, '') self.assertTrue(any('no saved session' in msg for msg in self.error_messages)) + + def test_restore_action_without_state_returns_false(self) -> None: + from kitty.session_restore import restore_last_session_into + + self.assertFalse(restore_last_session_into(None)) # type: ignore