Skip to content

Commit e176b72

Browse files
committed
When creating a new window/tab with the same cwd as the current window automatically put the newwindow into the same session as the current window
1 parent d3c46a8 commit e176b72

File tree

6 files changed

+59
-16
lines changed

6 files changed

+59
-16
lines changed

docs/sessions.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,13 @@ Making newly created windows join an existing session
187187

188188
Normally, after activating a session, if you create new windows/tabs
189189
they don't belong to the session. If you would prefer to have them belong
190-
to the currently active session, you can use the :option:`launch --add-to-session`
191-
option, like this:
190+
to the currently active session, you can use the :ac:`new_window_with_cwd`
191+
and :ac:`new_tab_with_cwd` actions instead, like this::
192192

193-
map kitty_mod+enter launch --add-to-session=.
193+
map kitty_mod+enter new_window_with_cwd
194+
map kitty_mod+t new_tab_with_cwd
194195

195-
This will cause newly created windows to belong to the currently active
196+
This will cause newly created windows and tabs to belong to the currently active
196197
session, if any. Note that adding a window to a session in this way is
197198
temporary, it does not edit the session file. If you wish to update the
198199
session file of the currently active session, you can use the following
@@ -201,6 +202,9 @@ mapping for it::
201202
map f5 save_as_session --relocatable --use-foreground-process --match=session:. .
202203

203204
The two can be combined, using the :ac:`combine` action.
205+
For even more control of what session a window is added to use
206+
the :doc:`launch <launch>` command with the :option:`launch --add-to-session`
207+
flag.
204208

205209

206210
Sessions with remote connections

kitty/boss.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,14 @@ def _new_os_window(self, args: SpecialWindowInstance | Iterable[str], cwd_from:
644644
else:
645645
sw = self.args_to_special_window(args, cwd_from) if args else None
646646
startup_session = next(create_sessions(get_options(), special_window=sw, cwd_from=cwd_from))
647-
return self.add_os_window(startup_session)
647+
startup_session.session_name = ''
648+
ans = self.add_os_window(startup_session)
649+
if cwd_from is not None and (sow := cwd_from.window) and (tm := self.os_window_map.get(ans)) and sow.created_in_session_name:
650+
for tab in tm:
651+
tab.created_in_session_name = sow.created_in_session_name
652+
for w in tab:
653+
w.created_in_session_name = sow.created_in_session_name
654+
return ans
648655

649656
@ac('win', 'New OS Window')
650657
def new_os_window(self, *args: str) -> None:
@@ -2758,11 +2765,14 @@ def _new_window(self, args: list[str], cwd_from: CwdRequest | None = None) -> Wi
27582765
args = args[1:]
27592766
allow_remote_control = True
27602767
if args:
2761-
return tab.new_special_window(
2768+
w = tab.new_special_window(
27622769
self.args_to_special_window(args, cwd_from=cwd_from),
27632770
location=location, allow_remote_control=allow_remote_control)
27642771
else:
2765-
return tab.new_window(cwd_from=cwd_from, location=location, allow_remote_control=allow_remote_control)
2772+
w = tab.new_window(cwd_from=cwd_from, location=location, allow_remote_control=allow_remote_control)
2773+
if cwd_from is not None and (sw := cwd_from.window):
2774+
w.created_in_session_name = sw.created_in_session_name
2775+
return w
27662776

27672777
@ac('win', 'Create a new window')
27682778
def new_window(self, *args: str) -> None:

kitty/launch.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class LaunchSpec(NamedTuple):
2626
args: list[str]
2727

2828

29+
# Options definition {{{
2930
env_docs = '''\
3031
type=list
3132
Environment variables to set in the child process. Can be specified multiple
@@ -148,10 +149,13 @@ def options_spec() -> str:
148149
149150
--add-to-session
150151
Add the newly created window/tab to the specified session. Use :code:`.` to add
151-
to the currently active session, if any. See :ref:`sessions`
152-
for what a session is and how to use one. Adding a newly created window to a session
153-
is purely temporary, it does not change the actual session file, for that you have
154-
to resave the session. Note that using this flag in a launch
152+
to the session of the :option:`source window <launch --source-window>`, if any. See :ref:`sessions`
153+
for what a session is and how to use one. By default, the window is added to the
154+
session of the :option:`source window <launch --source-window>` when :option:`launch --cwd`
155+
is set to use the the working directory from that window, otherwise the newly created window
156+
does not belong to any session. To force adding to no session, use the value :code:`!`.
157+
Adding a newly created window to a session is purely temporary, it does not change the actual
158+
session file, for that you have to resave the session. Note that using this flag in a launch
155159
command within a session file has no effect as the window is always added to the
156160
session belonging to that file.
157161
@@ -415,6 +419,7 @@ def options_spec() -> str:
415419
the new window will run a local shell after disconnecting from the remote host, when this option
416420
is specified.
417421
"""
422+
# }}}
418423

419424

420425
def parse_launch_args(args: Sequence[str] | None = None) -> LaunchSpec:
@@ -781,8 +786,14 @@ def _launch(
781786
child_death_callback(0, None)
782787
else:
783788
add_to_session = opts.add_to_session or ''
784-
if add_to_session == '.':
785-
add_to_session = boss.active_session
789+
match add_to_session:
790+
case '.':
791+
add_to_session = source_window.created_in_session_name if source_window else ''
792+
case '!':
793+
add_to_session = ''
794+
case '':
795+
if kw['cwd_from'] is not None and source_window:
796+
add_to_session = source_window.created_in_session_name
786797
kw['hold'] = opts.hold
787798
if force_target_tab and target_tab is not None:
788799
tab = target_tab

kitty/options/definition.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,8 @@
14731473
The current layout name.
14741474
:code:`session_name`
14751475
The name of the kitty session file from which this tab was created, if any.
1476+
:code:`active_session_name`
1477+
The name of the kitty session file from which the active window in this tab was created, if any.
14761478
:code:`num_windows`
14771479
The number of windows in the tab.
14781480
:code:`num_window_groups`

kitty/tab_bar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class TabBarData(NamedTuple):
5252
total_progress: int
5353
last_focused_window_with_progress_id: int
5454
session_name: str
55+
active_session_name: str
5556

5657

5758
class DrawData(NamedTuple):
@@ -278,6 +279,8 @@ def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int,
278279
eval_locals = {
279280
'index': index,
280281
'layout_name': tab.layout_name,
282+
'session_name': tab.session_name,
283+
'active_session_name': tab.active_session_name,
281284
'num_windows': tab.num_windows,
282285
'num_window_groups': tab.num_window_groups,
283286
'title': tab.title,

kitty/tabs.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,11 @@ def __len__(self) -> int:
977977
def num_window_groups(self) -> int:
978978
return self.windows.num_groups
979979

980+
@property
981+
def active_session_name(self) -> str:
982+
w = self.active_window
983+
return '' if w is None else w.created_in_session_name
984+
980985
def __contains__(self, window: Window) -> bool:
981986
return window in self.windows
982987

@@ -1297,11 +1302,19 @@ def new_tab(
12971302
cwd_from: CwdRequest | None = None,
12981303
as_neighbor: bool = False,
12991304
empty_tab: bool = False,
1300-
location: str = 'last'
1305+
location: str = 'last',
13011306
) -> Tab:
13021307
idx = len(self.tabs)
13031308
orig_active_tab_idx = self.active_tab_idx
1304-
self._add_tab(Tab(self, no_initial_window=True) if empty_tab else Tab(self, special_window=special_window, cwd_from=cwd_from))
1309+
session_name = ''
1310+
if cwd_from is not None and (sw := cwd_from.window):
1311+
session_name = sw.created_in_session_name
1312+
t = Tab(self, no_initial_window=True, session_name=session_name) if empty_tab else Tab(
1313+
self, special_window=special_window, cwd_from=cwd_from, session_name=session_name)
1314+
if not empty_tab and session_name:
1315+
for w in t:
1316+
w.created_in_session_name = session_name
1317+
self._add_tab(t)
13051318
if as_neighbor:
13061319
location = 'after'
13071320
if location == 'neighbor':
@@ -1392,7 +1405,7 @@ def tab_bar_data(self) -> list[TabBarData]:
13921405
has_activity_since_last_focus, t.active_fg, t.active_bg,
13931406
t.inactive_fg, t.inactive_bg, t.num_of_windows_with_progress,
13941407
t.total_progress, t.last_focused_window_with_progress_id,
1395-
t.created_in_session_name,
1408+
t.created_in_session_name, t.active_session_name,
13961409
))
13971410
return ans
13981411

0 commit comments

Comments
 (0)