Skip to content

Commit

Permalink
Handle auto-cd in shell-mode
Browse files Browse the repository at this point in the history
* lisp/shell.el (shell-has-auto-cd): New defcustom.
(shell-directory-tracker): Handle implicit "cd".

Copyright-paperwork-exempt: yes
  • Loading branch information
Jason Kim authored and albinus committed Jun 9, 2021
1 parent 4a1e97b commit e67883b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lisp/shell.el
Expand Up @@ -321,6 +321,15 @@ 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' handles implicit \"cd\" commands.
Implicit \"cd\" is changing the directory if the command is 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."
:type 'boolean
:group 'shell-directories
:version "28.1")

(defvar shell-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-f" 'shell-forward-command)
Expand Down Expand Up @@ -836,13 +845,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)
Expand All @@ -859,7 +870,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
Expand Down

0 comments on commit e67883b

Please sign in to comment.