diff --git a/docs/changelog.rst b/docs/changelog.rst index 1fc5de2e9..72b132d9a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -13,6 +13,10 @@ Changelog - Report the current foreground processes as well as the original child process, when using `kitty @ ls` +- Use the current working directory of the foreground process for the + `*_with_cwd` actions that open a new window with the current working + directory. + - Fix setting :opt:`background_opacity` causing window margins/padding to be slightly different shade from background (:iss:`1221`) diff --git a/kitty/boss.py b/kitty/boss.py index 5b77ecab4..fb102f8f3 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -256,7 +256,7 @@ class Boss: def new_os_window_with_cwd(self, *args): w = self.active_window_for_cwd - cwd_from = w.child.pid if w is not None else None + cwd_from = w.child.pid_for_cwd if w is not None else None self._new_os_window(args, cwd_from) def add_child(self, window): @@ -855,7 +855,7 @@ class Boss: def pipe(self, source, dest, exe, *args): cmd = [exe] + list(args) window = self.active_window - cwd_from = window.child.pid if window else None + cwd_from = window.child.pid_for_cwd if window else None def create_window(): return self.special_window_for_cmd( @@ -923,7 +923,7 @@ class Boss: def new_tab_with_cwd(self, *args): w = self.active_window_for_cwd - cwd_from = w.child.pid if w is not None else None + cwd_from = w.child.pid_for_cwd if w is not None else None self._create_tab(args, cwd_from=cwd_from) def _new_window(self, args, cwd_from=None): @@ -941,7 +941,7 @@ class Boss: w = self.active_window_for_cwd if w is None: return self.new_window(*args) - cwd_from = w.child.pid if w is not None else None + cwd_from = w.child.pid_for_cwd if w is not None else None self._new_window(args, cwd_from=cwd_from) def move_tab_forward(self): diff --git a/kitty/child.py b/kitty/child.py index 519d67820..fc91bd1bc 100644 --- a/kitty/child.py +++ b/kitty/child.py @@ -205,6 +205,10 @@ class Child: ans['cmdline'] = cmdline_of_process(pid) except Exception: pass + try: + ans['cwd'] = cwd_of_process(pid) or None + except Exception: + pass return ans return list(map(process_desc, foreground_processes)) @@ -231,3 +235,21 @@ class Child: return cwd_of_process(self.pid) except Exception: pass + + @property + def pid_for_cwd(self): + try: + pgrp = os.tcgetpgrp(self.child_fd) + foreground_processes = processes_in_group(pgrp) if pgrp >= 0 else [] + if len(foreground_processes) == 1: + return foreground_processes[0] + except Exception: + pass + return self.pid + + @property + def foreground_cwd(self): + try: + return cwd_of_process(self.pid_for_cwd) or None + except Exception: + pass diff --git a/kitty/window.py b/kitty/window.py index b44442c52..03d439cb8 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -10,7 +10,6 @@ from collections import deque from enum import IntEnum from itertools import chain -from .child import cwd_of_process from .config import build_ansi_color_table from .constants import ( ScreenGeometry, WindowGeometry, appname, get_boss, wakeup @@ -481,11 +480,7 @@ class Window: @property def cwd_of_child(self): - # TODO: Maybe use the cwd of the leader of the foreground process - # group in the session of the child process? - pid = self.child.pid - if pid is not None: - return cwd_of_process(pid) or None + return self.child.foreground_cwd or self.child.current_cwd def pipe_data(self, text, has_wrap_markers=False): text = text or ''