From 7dca68a6289abb1800b2dccfd26efe0e85aad6a4 Mon Sep 17 00:00:00 2001 From: Jason Kim Date: Fri, 7 May 2021 09:41:11 -0700 Subject: [PATCH] Add a defcustom to shell-mode to handle auto-cd feature (e.g. zsh) shell-has-auto-cd is a new defcustom (default: nil) for users with shells like zsh that can auto-cd into a directory without typing in the "cd" command. If set to true and the command is a directory, then shell-mode processes the command (which happens to be a directory) as if it were prepended with "cd" --- lisp/shell.el | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lisp/shell.el b/lisp/shell.el index 3098d3a14d..8b3499ccb4 100644 --- a/lisp/shell.el +++ b/lisp/shell.el @@ -321,6 +321,13 @@ Thus, this does not include the shell's current directory.") (defvar shell-dirstack-query nil "Command used by `shell-resync-dirs' to query the shell.") +(defcustom shell-has-auto-cd nil + "If non-nil, `shell-mode' will automatically process implicit \"cd\" commands. +Implicit \"cd\" is changing the directory if the command happens to be a directory. +You can make this variable buffer-local to change it, per shell-mode instance +Useful for shells like zsh that has this feature.") + + (defvar shell-mode-map (let ((map (make-sparse-keymap))) (define-key map "\C-c\C-f" 'shell-forward-command) @@ -836,13 +843,15 @@ Environment variables are expanded, see function `substitute-in-file-name'." str) ; skip whitespace (match-end 0))) (case-fold-search) - end cmd arg1) + end cmd arg1 cmd-subst-fn) (while (string-match shell-command-regexp str start) (setq end (match-end 0) cmd (comint-arguments (substring str start end) 0 0) arg1 (comint-arguments (substring str start end) 1 1)) (if arg1 (setq arg1 (shell-unquote-argument arg1))) + (if shell-has-auto-cd + (setq cmd-subst-fn (comint-substitute-in-file-name cmd))) (cond ((string-match (concat "\\`\\(" shell-popd-regexp "\\)\\($\\|[ \t]\\)") cmd) @@ -859,7 +868,9 @@ Environment variables are expanded, see function `substitute-in-file-name'." (string-match (concat "\\`\\(" shell-chdrive-regexp "\\)\\($\\|[ \t]\\)") cmd)) - (shell-process-cd (comint-substitute-in-file-name cmd)))) + (shell-process-cd (comint-substitute-in-file-name cmd))) + ((and shell-has-auto-cd (file-directory-p cmd-subst-fn)) + (shell-process-cd cmd-subst-fn))) (setq start (progn (string-match shell-command-separator-regexp str end) ;; skip again -- 2.25.1