Function: shell-resync-dirs

shell-resync-dirs is an interactive and byte-compiled function defined in shell.el.gz.

Signature

(shell-resync-dirs)

Documentation

Resync the buffer's idea of the current directory stack.

This command queries the shell with the command bound to shell-dirstack-query (default "dirs"), reads the next line output and parses it to form the new directory stack.

Key Bindings

Aliases

dirs

Source Code

;; Defined in /usr/src/emacs/lisp/shell.el.gz
(defun shell-resync-dirs ()
  "Resync the buffer's idea of the current directory stack.
This command queries the shell with the command bound to
`shell-dirstack-query' (default \"dirs\"), reads the next
line output and parses it to form the new directory stack."
  (interactive)
  (let* ((dls (car
               (last
                (string-lines
                 (string-chop-newline
                  (shell-eval-command (concat shell-dirstack-query "\n")))))))
         (dlsl nil)
         (pos 0)
         (ds nil))
    (setq dls (string-trim-right dls "[ ]+"))
    ;; Split the dirlist into whitespace and non-whitespace chunks.
    ;; dlsl will be a reversed list of tokens.
    (while (string-match "\\(\\S-+\\|\\s-+\\)" dls pos)
      (push (match-string 1 dls) dlsl)
      (setq pos (match-end 1)))

    ;; Prepend trailing entries until they form an existing directory,
    ;; whitespace and all.  Discard the next whitespace and repeat.
    (while dlsl
      (let ((newelt "")
            tem1 tem2)
        (while (and dlsl newelt)
          ;; We need tem1 because we don't want to prepend
          ;; `comint-file-name-prefix' repeatedly into newelt via tem2.
          (setq tem1 (pop dlsl)
                tem2 (concat comint-file-name-prefix tem1 newelt))
          (cond ((file-directory-p tem2)
                 (push tem2 ds)
                 (when (string= " " (car dlsl))
                   (pop dlsl))
                 (setq newelt nil))
                (t
                 (setq newelt (concat tem1 newelt)))))))

    (with-demoted-errors "Couldn't cd: %s"
      (shell-cd (car ds))
      (setq shell-dirstack (cdr ds)
            shell-last-dir (car shell-dirstack))
      (shell-dirstack-message))))