Function: eshell/cd

eshell/cd is a byte-compiled function defined in em-dirs.el.gz.

Signature

(eshell/cd &rest ARGS)

Documentation

Alias to extend the behavior of cd.

Source Code

;; Defined in /usr/src/emacs/lisp/eshell/em-dirs.el.gz
(defun eshell/cd (&rest args)           ; all but first ignored
  "Alias to extend the behavior of `cd'."
  (setq args (flatten-tree args))
  (let ((path (car args))
	(subpath (car (cdr args)))
	(case-fold-search (eshell-under-windows-p))
	handled)
    (if (numberp path)
	(setq path (number-to-string path)))
    (if (numberp subpath)
	(setq subpath (number-to-string subpath)))
    (cond
     (subpath
      (let ((curdir (eshell/pwd)))
	(if (string-match path curdir)
	    (setq path (replace-match subpath nil nil curdir))
	  (error "Path substring `%s' not found" path))))
     ((and path (string-match "^-\\([0-9]*\\)$" path))
      (let ((index (match-string 1 path)))
	(setq path
	      (ring-remove eshell-last-dir-ring
			   (if index
			       (string-to-number index)
			     0)))))
     ((and path (string-match "^=\\(.*\\)$" path))
      (let ((oldpath (eshell-find-previous-directory
		      (match-string 1 path))))
	(if oldpath
	    (setq path oldpath)
	  (let ((len (ring-length eshell-last-dir-ring))
		(index 0))
	    (if (= len 0)
		(error "Directory ring empty"))
	    (eshell-init-print-buffer)
	    (while (< index len)
	      (eshell-buffered-print
	       (concat (number-to-string index) ": "
		       (ring-ref eshell-last-dir-ring index) "\n"))
	      (setq index (1+ index)))
	    (eshell-flush)
	    (setq handled t)))))
     (path
      (setq path (eshell-expand-multiple-dots path))))
    (unless handled
      (let ((curdir (eshell/pwd))
	    (newdir (or path "~")))
	(unless (equal curdir newdir)
	  (eshell-add-to-dir-ring curdir))
	(let ((result (cd newdir)))
          ;; If we're in "/" and cd to ".." or the like, make things
          ;; less confusing by changing "/.." to "/".
          (when (equal (file-truename result) "/")
            (setq result (cd "/")))
	  (and eshell-cd-shows-directory
	       (eshell-printn result)))
	(run-hooks 'eshell-directory-change-hook)
	(if eshell-list-files-after-cd
	    ;; Let-bind eshell-last-command around this?
	    (eshell-plain-command "ls" (cdr args)))
	nil))))