diff --git a/docs/changelog.rst b/docs/changelog.rst index 44609788e..5a9295d21 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -45,6 +45,8 @@ Detailed list of changes - Speed up the ``kitty @`` executable by ~10x reducing the time for typical remote control commands from ~50ms to ~5ms +- Allow using the cwd of the original process for :option:`launch --cwd` (:iss:`5672`) + 0.26.5 [2022-11-07] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/launch.py b/kitty/launch.py index 28c79d0aa..49fecaab7 100644 --- a/kitty/launch.py +++ b/kitty/launch.py @@ -93,14 +93,15 @@ opened window. --cwd -completion=type:directory kwds:current,oldest,last_reported +completion=type:directory kwds:current,oldest,last_reported,root The working directory for the newly launched child. Use the special value :code:`current` to use the working directory of the currently active window. The special value :code:`last_reported` uses the last working directory reported by the shell (needs :ref:`shell_integration` to work). The special value :code:`oldest` works like :code:`current` but uses the working directory of the oldest foreground process associated with the currently active window rather -than the newest foreground process. +than the newest foreground process. Finally, the special value :code:`root` +refers to the process that was originally started when the window was created. --env @@ -484,6 +485,9 @@ def launch( elif opts.cwd == 'oldest': if active: kw['cwd_from'] = CwdRequest(active, CwdRequestType.oldest) + elif opts.cwd == 'root': + if active: + kw['cwd_from'] = CwdRequest(active, CwdRequestType.root) else: kw['cwd'] = opts.cwd if opts.location != 'default': diff --git a/kitty/window.py b/kitty/window.py index 1532a09dc..3ae988c6d 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -69,6 +69,7 @@ class CwdRequestType(Enum): current: int = auto() last_reported: int = auto() oldest: int = auto() + root: int = auto() class CwdRequest: @@ -92,6 +93,8 @@ class CwdRequest: reported_cwd = path_from_osc7_url(window.screen.last_reported_cwd) if window.screen.last_reported_cwd else '' if reported_cwd and not window.child_is_remote and (self.request_type is CwdRequestType.last_reported or window.at_prompt): return reported_cwd + if self.request_type is CwdRequestType.root: + return window.child.current_cwd or '' return window.get_cwd_of_child(oldest=self.request_type is CwdRequestType.oldest) or '' def modify_argv_for_launch_with_cwd(self, argv: List[str]) -> str: @@ -99,7 +102,7 @@ class CwdRequest: if not window: return '' reported_cwd = path_from_osc7_url(window.screen.last_reported_cwd) if window.screen.last_reported_cwd else '' - if reported_cwd: + if reported_cwd and (self.request_type is not CwdRequestType.root or window.root_in_foreground_processes): # First check if we are running ssh kitten, and trying to open the configured login shell if argv[0] == resolved_shell(get_options())[0]: ssh_kitten_cmdline = window.ssh_kitten_cmdline() @@ -1389,6 +1392,14 @@ class Window: def cwd_of_child(self) -> Optional[str]: return self.get_cwd_of_child() + @property + def root_in_foreground_processes(self) -> bool: + q = self.child.pid + for p in self.child.foreground_processes: + if p['pid'] == q: + return True + return False + @property def child_is_remote(self) -> bool: for p in self.child.foreground_processes: