em-dirs.el 18.9 KB
Newer Older
1
;;; em-dirs.el --- directory navigation commands  -*- lexical-binding:t -*-
Gerd Moellmann's avatar
Gerd Moellmann committed
2

3
;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
Gerd Moellmann's avatar
Gerd Moellmann committed
4

Gerd Moellmann's avatar
Gerd Moellmann committed
5 6
;; Author: John Wiegley <johnw@gnu.org>

Gerd Moellmann's avatar
Gerd Moellmann committed
7 8
;; This file is part of GNU Emacs.

9
;; GNU Emacs is free software: you can redistribute it and/or modify
Gerd Moellmann's avatar
Gerd Moellmann committed
10
;; it under the terms of the GNU General Public License as published by
11 12
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Gerd Moellmann's avatar
Gerd Moellmann committed
13 14 15 16 17 18 19

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
20
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Gerd Moellmann's avatar
Gerd Moellmann committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

;;; Commentary:

;; The only special feature that Eshell offers in the last-dir-ring.
;; To view the ring, enter:
;;
;;   cd =
;;
;; Changing to an index within the ring is done using:
;;
;;   cd -      ; same as cd -0
;;   cd -4
;;
;; Or, it is possible to change the first member in the ring which
;; matches a regexp:
;;
;;   cd =bcc   ; change to the last directory visited containing "bcc"
;;
;; This ring is maintained automatically, and is persisted across
;; Eshell sessions.  It is a separate mechanism from `pushd' and
;; `popd', and the two may be used at the same time.

43 44 45
;;; Code:

(require 'eshell)
Gerd Moellmann's avatar
Gerd Moellmann committed
46 47 48
(require 'ring)
(require 'esh-opt)

49
;;;###autoload
50 51
(progn
(defgroup eshell-dirs nil
52 53 54 55 56 57
  "Directory navigation involves changing directories, examining the
current directory, maintaining a directory stack, and also keeping
track of a history of the last directory locations the user was in.
Emacs does provide standard Lisp definitions of `pwd' and `cd', but
they lack somewhat in feel from the typical shell equivalents."
  :tag "Directory navigation"
58
  :group 'eshell-module))
59

Gerd Moellmann's avatar
Gerd Moellmann committed
60 61
;;; User Variables:

62
(defcustom eshell-dirs-load-hook nil
Glenn Morris's avatar
Glenn Morris committed
63
  "A hook that gets run when `eshell-dirs' is loaded."
64
  :version "24.1"			; removed eshell-dirs-initialize
Gerd Moellmann's avatar
Gerd Moellmann committed
65 66 67 68 69 70
  :type 'hook
  :group 'eshell-dirs)

(defcustom eshell-pwd-convert-function (if (eshell-under-windows-p)
					   'expand-file-name
					 'identity)
Glenn Morris's avatar
Glenn Morris committed
71
  "The function used to normalize the value of Eshell's `pwd'.
Gerd Moellmann's avatar
Gerd Moellmann committed
72 73 74 75 76 77 78 79 80 81
The value returned by `pwd' is also used when recording the
last-visited directory in the last-dir-ring, so it will affect the
form of the list used by 'cd ='."
  :type '(radio (function-item file-truename)
		(function-item expand-file-name)
		(function-item identity)
		(function :tag "Other"))
  :group 'eshell-dirs)

(defcustom eshell-ask-to-save-last-dir 'always
Glenn Morris's avatar
Glenn Morris committed
82
  "Determine if the last-dir-ring should be automatically saved.
Gerd Moellmann's avatar
Gerd Moellmann committed
83 84 85 86 87 88 89 90 91 92 93 94
The last-dir-ring is always preserved when exiting an Eshell buffer.
However, when Emacs is being shut down, this variable determines
whether to prompt the user, or just save the ring.
If set to nil, it means never ask whether to save the last-dir-ring.
If set to t, always ask if any Eshell buffers are open at exit time.
If set to `always', the list-dir-ring will always be saved, silently."
  :type '(choice (const :tag "Never" nil)
		 (const :tag "Ask" t)
		 (const :tag "Always save" always))
  :group 'eshell-dirs)

(defcustom eshell-cd-shows-directory nil
Glenn Morris's avatar
Glenn Morris committed
95
  "If non-nil, using `cd' will report the directory it changes to."
Gerd Moellmann's avatar
Gerd Moellmann committed
96 97 98 99
  :type 'boolean
  :group 'eshell-dirs)

(defcustom eshell-cd-on-directory t
Glenn Morris's avatar
Glenn Morris committed
100
  "If non-nil, do a cd if a directory is in command position."
Gerd Moellmann's avatar
Gerd Moellmann committed
101 102 103 104
  :type 'boolean
  :group 'eshell-dirs)

(defcustom eshell-directory-change-hook nil
Glenn Morris's avatar
Glenn Morris committed
105
  "A hook to run when the current directory changes."
Gerd Moellmann's avatar
Gerd Moellmann committed
106 107 108 109
  :type 'hook
  :group 'eshell-dirs)

(defcustom eshell-list-files-after-cd nil
Glenn Morris's avatar
Glenn Morris committed
110
  "If non-nil, call \"ls\" with any remaining args after doing a cd.
Gerd Moellmann's avatar
Gerd Moellmann committed
111 112 113 114 115 116 117
This is provided for convenience, since the same effect is easily
achieved by adding a function to `eshell-directory-change-hook' that
calls \"ls\" and references `eshell-last-arguments'."
  :type 'boolean
  :group 'eshell-dirs)

(defcustom eshell-pushd-tohome nil
Glenn Morris's avatar
Glenn Morris committed
118
  "If non-nil, make pushd with no arg behave as 'pushd ~' (like `cd').
Gerd Moellmann's avatar
Gerd Moellmann committed
119 120 121 122 123
This mirrors the optional behavior of tcsh."
  :type 'boolean
  :group 'eshell-dirs)

(defcustom eshell-pushd-dextract nil
Glenn Morris's avatar
Glenn Morris committed
124
  "If non-nil, make \"pushd +n\" pop the nth dir to the stack top.
Gerd Moellmann's avatar
Gerd Moellmann committed
125 126 127 128 129
This mirrors the optional behavior of tcsh."
  :type 'boolean
  :group 'eshell-dirs)

(defcustom eshell-pushd-dunique nil
Glenn Morris's avatar
Glenn Morris committed
130
  "If non-nil, make pushd only add unique directories to the stack.
Gerd Moellmann's avatar
Gerd Moellmann committed
131 132 133 134 135
This mirrors the optional behavior of tcsh."
  :type 'boolean
  :group 'eshell-dirs)

(defcustom eshell-dirtrack-verbose t
Glenn Morris's avatar
Glenn Morris committed
136
  "If non-nil, show the directory stack following directory change.
Gerd Moellmann's avatar
Gerd Moellmann committed
137 138 139 140 141
This is effective only if directory tracking is enabled."
  :type 'boolean
  :group 'eshell-dirs)

(defcustom eshell-last-dir-ring-file-name
Chong Yidong's avatar
Chong Yidong committed
142
  (expand-file-name "lastdir" eshell-directory-name)
Glenn Morris's avatar
Glenn Morris committed
143
  "If non-nil, name of the file to read/write the last-dir-ring.
Gerd Moellmann's avatar
Gerd Moellmann committed
144 145 146 147 148 149
See also `eshell-read-last-dir-ring' and `eshell-write-last-dir-ring'.
If it is nil, the last-dir-ring will not be written to disk."
  :type 'file
  :group 'eshell-dirs)

(defcustom eshell-last-dir-ring-size 32
Glenn Morris's avatar
Glenn Morris committed
150
  "If non-nil, the size of the directory history ring.
Gerd Moellmann's avatar
Gerd Moellmann committed
151 152 153 154 155 156 157 158 159 160 161 162 163
This ring is added to every time `cd' or `pushd' is used.  It simply
stores the most recent directory locations Eshell has been in.  To
return to the most recent entry, use 'cd -' (equivalent to 'cd -0').
To return to an older entry, use 'cd -N', where N is an integer less
than `eshell-last-dir-ring-size'.  To return to the last directory
matching a particular regexp, use 'cd =REGEXP'.  To display the
directory history list, use 'cd ='.

This mechanism is very similar to that provided by `pushd', except
it's far more automatic.  `pushd' allows the user to decide which
directories gets pushed, and its size is unlimited.

`eshell-last-dir-ring' is meant for users who don't use `pushd'
Paul Eggert's avatar
Paul Eggert committed
164
explicitly very much, but every once in a while would like to return to
Gerd Moellmann's avatar
Gerd Moellmann committed
165 166 167 168 169 170
a previously visited directory without having to type in the whole
thing again."
  :type 'integer
  :group 'eshell-dirs)

(defcustom eshell-last-dir-unique t
Glenn Morris's avatar
Glenn Morris committed
171
  "If non-nil, `eshell-last-dir-ring' contains only unique entries."
Gerd Moellmann's avatar
Gerd Moellmann committed
172 173 174
  :type 'boolean
  :group 'eshell-dirs)

175
;;; Internal Variables:
Gerd Moellmann's avatar
Gerd Moellmann committed
176 177 178 179 180 181

(defvar eshell-dirstack nil
  "List of directories saved by pushd in the Eshell buffer.
Thus, this does not include the current directory.")

(defvar eshell-last-dir-ring nil
Juanma Barranquero's avatar
Juanma Barranquero committed
182
  "The last directory that Eshell was in.")
Gerd Moellmann's avatar
Gerd Moellmann committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

;;; Functions:

(defun eshell-dirs-initialize ()
  "Initialize the builtin functions for Eshell."
  (make-local-variable 'eshell-variable-aliases-list)
  (setq eshell-variable-aliases-list
	(append
	 eshell-variable-aliases-list
	 '(("-" (lambda (indices)
		  (if (not indices)
		      (unless (ring-empty-p eshell-last-dir-ring)
			(expand-file-name
			 (ring-ref eshell-last-dir-ring 0)))
		    (expand-file-name
		     (eshell-apply-indices eshell-last-dir-ring indices)))))
	   ("+" "PWD")
	   ("PWD" (lambda (indices)
		    (expand-file-name (eshell/pwd))) t)
	   ("OLDPWD" (lambda (indices)
		       (unless (ring-empty-p eshell-last-dir-ring)
			 (expand-file-name
			  (ring-ref eshell-last-dir-ring 0)))) t))))

  (when eshell-cd-on-directory
    (make-local-variable 'eshell-interpreter-alist)
    (setq eshell-interpreter-alist
210 211
	  (cons (cons #'(lambda (file args)
                          (eshell-lone-directory-p file))
Gerd Moellmann's avatar
Gerd Moellmann committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
		      'eshell-dirs-substitute-cd)
		eshell-interpreter-alist)))

  (add-hook 'eshell-parse-argument-hook
	    'eshell-parse-user-reference nil t)
  (if (eshell-under-windows-p)
      (add-hook 'eshell-parse-argument-hook
		'eshell-parse-drive-letter nil t))

  (when (eshell-using-module 'eshell-cmpl)
    (add-hook 'pcomplete-try-first-hook
	      'eshell-complete-user-reference nil t))

  (make-local-variable 'eshell-dirstack)
  (make-local-variable 'eshell-last-dir-ring)

  (if eshell-last-dir-ring-file-name
      (eshell-read-last-dir-ring))
  (unless eshell-last-dir-ring
    (setq eshell-last-dir-ring (make-ring eshell-last-dir-ring-size)))

  (add-hook 'eshell-exit-hook 'eshell-write-last-dir-ring nil t)

  (add-hook 'kill-emacs-hook 'eshell-save-some-last-dir))

(defun eshell-save-some-last-dir ()
  "Save the list-dir-ring for any open Eshell buffers."
239
  (dolist (buf (buffer-list))
Gerd Moellmann's avatar
Gerd Moellmann committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    (if (buffer-live-p buf)
	(with-current-buffer buf
	  (if (and eshell-mode
		   eshell-ask-to-save-last-dir
		   (or (eq eshell-ask-to-save-last-dir 'always)
		       (y-or-n-p
			(format "Save last dir ring for Eshell buffer `%s'? "
				(buffer-name buf)))))
	      (eshell-write-last-dir-ring))))))

(defun eshell-lone-directory-p (file)
  "Test whether FILE is just a directory name, and not a command name."
  (and (file-directory-p file)
       (or (file-name-directory file)
	   (not (eshell-search-path file)))))

(defun eshell-dirs-substitute-cd (&rest args)
  "Substitute the given command for a call to `cd' on that name."
  (if (> (length args) 1)
      (error "%s: command not found" (car args))
    (throw 'eshell-replace-command
John Wiegley's avatar
John Wiegley committed
261
	   (eshell-parse-command "cd" (eshell-flatten-list args)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
262 263 264 265 266 267 268 269 270 271

(defun eshell-parse-user-reference ()
  "An argument beginning with ~ is a filename to be expanded."
  (when (and (not eshell-current-argument)
	     (eq (char-after) ?~))
    (add-to-list 'eshell-current-modifiers 'expand-file-name)
    (forward-char)
    (char-to-string (char-before))))

(defun eshell-parse-drive-letter ()
Juanma Barranquero's avatar
Juanma Barranquero committed
272
  "An argument beginning with X:[^/] is a drive letter reference."
Gerd Moellmann's avatar
Gerd Moellmann committed
273 274 275 276 277 278
  (when (and (not eshell-current-argument)
	     (looking-at "\\([A-Za-z]:\\)\\([^/\\\\]\\|\\'\\)"))
    (goto-char (match-end 1))
    (let* ((letter (match-string 1))
	   (regexp (concat "\\`" letter))
	   (path (eshell-find-previous-directory regexp)))
279
      (concat (or path letter) "/"))))
Gerd Moellmann's avatar
Gerd Moellmann committed
280

Chong Yidong's avatar
Chong Yidong committed
281 282 283 284 285
(defvar pcomplete-stub)
(defvar pcomplete-last-completion-raw)
(declare-function pcomplete-actual-arg "pcomplete")
(declare-function pcomplete-uniqify-list "pcomplete")

Gerd Moellmann's avatar
Gerd Moellmann committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
(defun eshell-complete-user-reference ()
  "If there is a user reference, complete it."
  (let ((arg (pcomplete-actual-arg)))
    (when (string-match "\\`~[a-z]*\\'" arg)
      (setq pcomplete-stub (substring arg 1)
	    pcomplete-last-completion-raw t)
      (throw 'pcomplete-completions
	     (progn
	       (eshell-read-user-names)
	       (pcomplete-uniqify-list
		(mapcar
		 (function
		  (lambda (user)
		    (file-name-as-directory (cdr user))))
		 eshell-user-names)))))))

John Wiegley's avatar
John Wiegley committed
302
(defun eshell/pwd (&rest args)
Gerd Moellmann's avatar
Gerd Moellmann committed
303 304 305 306
  "Change output from `pwd` to be cleaner."
  (let* ((path default-directory)
	 (len (length path)))
    (if (and (> len 1)
307
	     (eq (aref path (1- len)) ?/)
Gerd Moellmann's avatar
Gerd Moellmann committed
308 309 310 311
	     (not (and (eshell-under-windows-p)
		       (string-match "\\`[A-Za-z]:[\\\\/]\\'" path))))
	(setq path (substring path 0 (1- (length path)))))
    (if eshell-pwd-convert-function
John Wiegley's avatar
John Wiegley committed
312 313
	(funcall eshell-pwd-convert-function path)
      path)))
Gerd Moellmann's avatar
Gerd Moellmann committed
314 315 316 317 318 319 320 321 322 323 324 325

(defun eshell-expand-multiple-dots (path)
  "Convert '...' to '../..', '....' to '../../..', etc..

With the following piece of advice, you can make this functionality
available in most of Emacs, with the exception of filename completion
in the minibuffer:

  (defadvice expand-file-name
    (before translate-multiple-dots
	    (filename &optional directory) activate)
    (setq filename (eshell-expand-multiple-dots filename)))"
326
  (while (string-match "\\(?:^\\|/\\)\\.\\.\\(\\.+\\)\\(?:$\\|/\\)" path)
Gerd Moellmann's avatar
Gerd Moellmann committed
327 328 329 330
    (let* ((extra-dots (match-string 1 path))
	   (len (length extra-dots))
	   replace-text)
      (while (> len 0)
331
	(setq replace-text (concat replace-text "/..")
Gerd Moellmann's avatar
Gerd Moellmann committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	      len (1- len)))
      (setq path
	    (replace-match replace-text t t path 1))))
  path)

(defun eshell-find-previous-directory (regexp)
  "Find the most recent last-dir matching REGEXP."
  (let ((index 0)
	(len (ring-length eshell-last-dir-ring))
	oldpath)
    (if (> (length regexp) 0)
	(while (< index len)
	  (setq oldpath (ring-ref eshell-last-dir-ring index))
	  (if (string-match regexp oldpath)
	      (setq index len)
	    (setq oldpath nil
		  index (1+ index)))))
    oldpath))

351
(defvar dired-directory)
Gerd Moellmann's avatar
Gerd Moellmann committed
352 353 354

(defun eshell/cd (&rest args)           ; all but first ignored
  "Alias to extend the behavior of `cd'."
John Wiegley's avatar
John Wiegley committed
355
  (setq args (eshell-flatten-list args))
Gerd Moellmann's avatar
Gerd Moellmann committed
356 357
  (let ((path (car args))
	(subpath (car (cdr args)))
358
	(case-fold-search (eshell-under-windows-p))
Gerd Moellmann's avatar
Gerd Moellmann committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
	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
375
			       (string-to-number index)
Gerd Moellmann's avatar
Gerd Moellmann committed
376 377 378 379 380 381 382 383 384 385
			     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"))
John Wiegley's avatar
John Wiegley committed
386
	    (eshell-init-print-buffer)
Gerd Moellmann's avatar
Gerd Moellmann committed
387
	    (while (< index len)
John Wiegley's avatar
John Wiegley committed
388
	      (eshell-buffered-print
Gerd Moellmann's avatar
Gerd Moellmann committed
389
	       (concat (number-to-string index) ": "
John Wiegley's avatar
John Wiegley committed
390
		       (ring-ref eshell-last-dir-ring index) "\n"))
Gerd Moellmann's avatar
Gerd Moellmann committed
391
	      (setq index (1+ index)))
John Wiegley's avatar
John Wiegley committed
392
	    (eshell-flush)
Gerd Moellmann's avatar
Gerd Moellmann committed
393 394 395 396 397 398 399 400 401 402 403 404 405
	    (setq handled t)))))
     (path
      (setq path (eshell-expand-multiple-dots path))))
    (unless handled
      (setq dired-directory (or path "~"))
      (let ((curdir (eshell/pwd)))
	(unless (equal curdir dired-directory)
	  (eshell-add-to-dir-ring curdir))
	(let ((result (cd dired-directory)))
	  (and eshell-cd-shows-directory
	       (eshell-printn result)))
	(run-hooks 'eshell-directory-change-hook)
	(if eshell-list-files-after-cd
406 407
	    ;; Let-bind eshell-last-command around this?
	    (eshell-plain-command "ls" (cdr args)))
Gerd Moellmann's avatar
Gerd Moellmann committed
408 409
	nil))))

410 411
(put 'eshell/cd 'eshell-no-numeric-conversions t)

Gerd Moellmann's avatar
Gerd Moellmann committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
(defun eshell-add-to-dir-ring (path)
  "Add PATH to the last-dir-ring, if applicable."
  (unless (and (not (ring-empty-p eshell-last-dir-ring))
	       (equal path (ring-ref eshell-last-dir-ring 0)))
    (if eshell-last-dir-unique
	(let ((index 0)
	      (len (ring-length eshell-last-dir-ring)))
	  (while (< index len)
	    (if (equal (ring-ref eshell-last-dir-ring index) path)
		(ring-remove eshell-last-dir-ring index)
	      (setq index (1+ index))))))
    (ring-insert eshell-last-dir-ring path)))

;;; pushd [+n | dir]
(defun eshell/pushd (&rest args)        ; all but first ignored
  "Implementation of pushd in Lisp."
  (let ((path (car args)))
    (cond
     ((null path)
      ;; no arg -- swap pwd and car of stack unless eshell-pushd-tohome
      (cond (eshell-pushd-tohome
	     (eshell/pushd "~"))
	    (eshell-dirstack
	     (let ((old (eshell/pwd)))
	       (eshell/cd (car eshell-dirstack))
	       (setq eshell-dirstack (cons old (cdr eshell-dirstack)))
	       (eshell/dirs t)))
	    (t
	     (error "pushd: No other directory"))))
     ((string-match "^\\+\\([0-9]\\)" path)
      ;; pushd +n
      (setq path (string-to-number (match-string 1 path)))
      (cond ((> path (length eshell-dirstack))
	     (error "Directory stack not that deep"))
	    ((= path 0)
	     (error "Couldn't cd"))
	    (eshell-pushd-dextract
	     (let ((dir (nth (1- path) eshell-dirstack)))
	       (eshell/popd path)
	       (eshell/pushd (eshell/pwd))
	       (eshell/cd dir)
	       (eshell/dirs t)))
	    (t
	     (let* ((ds (cons (eshell/pwd) eshell-dirstack))
		    (dslen (length ds))
		    (front (nthcdr path ds))
		    (back (nreverse (nthcdr (- dslen path) (reverse ds))))
		    (new-ds (append front back)))
	       (eshell/cd (car new-ds))
	       (setq eshell-dirstack (cdr new-ds))
	       (eshell/dirs t)))))
     (t
      ;; pushd <dir>
      (let ((old-wd (eshell/pwd)))
	(eshell/cd path)
	(if (or (null eshell-pushd-dunique)
		(not (member old-wd eshell-dirstack)))
	    (setq eshell-dirstack (cons old-wd eshell-dirstack)))
	(eshell/dirs t)))))
  nil)

473 474
(put 'eshell/pushd 'eshell-no-numeric-conversions t)

Gerd Moellmann's avatar
Gerd Moellmann committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
;;; popd [+n]
(defun eshell/popd (&rest args)
  "Implementation of popd in Lisp."
  (let ((ref (or (car args) "+0")))
    (unless (and (stringp ref)
		 (string-match "\\`\\([+-][0-9]+\\)\\'" ref))
      (error "popd: bad arg `%s'" ref))
    (setq ref (string-to-number (match-string 1 ref)))
    (cond ((= ref 0)
	   (unless eshell-dirstack
	     (error "popd: Directory stack empty"))
	   (eshell/cd (car eshell-dirstack))
	   (setq eshell-dirstack (cdr eshell-dirstack))
	   (eshell/dirs t))
	  ((<= (abs ref) (length eshell-dirstack))
	   (let* ((ds (cons nil eshell-dirstack))
		  (cell (nthcdr (if (> ref 0)
				    (1- ref)
				  (+ (length eshell-dirstack) ref)) ds))
		  (dir (cadr cell)))
	     (eshell/cd dir)
	     (setcdr cell (cdr (cdr cell)))
	     (setq eshell-dirstack (cdr ds))
	     (eshell/dirs t)))
	  (t
	   (error "Couldn't popd"))))
  nil)

503 504
(put 'eshell/popd 'eshell-no-numeric-conversions t)

Gerd Moellmann's avatar
Gerd Moellmann committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
(defun eshell/dirs (&optional if-verbose)
  "Implementation of dirs in Lisp."
  (when (or (not if-verbose) eshell-dirtrack-verbose)
    (let* ((msg "")
	   (ds (cons (eshell/pwd) eshell-dirstack))
	   (home (expand-file-name "~/"))
	   (homelen (length home)))
      (while ds
	(let ((dir (car ds)))
	  (and (>= (length dir) homelen)
	       (string= home (substring dir 0 homelen))
	       (setq dir (concat "~/" (substring dir homelen))))
	  (setq msg (concat msg (directory-file-name dir) " "))
	  (setq ds (cdr ds))))
      msg)))

(defun eshell-read-last-dir-ring ()
Juanma Barranquero's avatar
Juanma Barranquero committed
522
  "Set the buffer's `eshell-last-dir-ring' from a history file."
Gerd Moellmann's avatar
Gerd Moellmann committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
  (let ((file eshell-last-dir-ring-file-name))
    (cond
     ((or (null file)
	  (equal file "")
	  (not (file-readable-p file)))
      nil)
     (t
      (let* ((count 0)
	     (size eshell-last-dir-ring-size)
	     (ring (make-ring size)))
	(with-temp-buffer
	  (insert-file-contents file)
	  ;; Save restriction in case file is already visited...
	  ;; Watch for those date stamps in history files!
	  (goto-char (point-max))
	  (while (and (< count size)
		      (re-search-backward "^\\([^\n].*\\)$" nil t))
	    (ring-insert-at-beginning ring (match-string 1))
	    (setq count (1+ count)))
	  ;; never allow the top element to equal the current
	  ;; directory
	  (while (and (not (ring-empty-p ring))
		      (equal (ring-ref ring 0) (eshell/pwd)))
	    (ring-remove ring 0)))
	(setq eshell-last-dir-ring ring))))))

(defun eshell-write-last-dir-ring ()
Juanma Barranquero's avatar
Juanma Barranquero committed
550
  "Write the buffer's `eshell-last-dir-ring' to a history file."
Gerd Moellmann's avatar
Gerd Moellmann committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
  (let ((file eshell-last-dir-ring-file-name))
    (cond
     ((or (null file)
	  (equal file "")
	  (null eshell-last-dir-ring)
	  (ring-empty-p eshell-last-dir-ring))
      nil)
     ((not (file-writable-p file))
      (message "Cannot write last-dir-ring file %s" file))
     (t
      (let* ((ring eshell-last-dir-ring)
	     (index (ring-length ring)))
	(with-temp-buffer
	  (while (> index 0)
	    (setq index (1- index))
	    (insert (ring-ref ring index) ?\n))
	  (insert (eshell/pwd) ?\n)
	  (eshell-with-private-file-modes
	   (write-region (point-min) (point-max) file nil
			 'no-message))))))))

572
(provide 'em-dirs)
Gerd Moellmann's avatar
Gerd Moellmann committed
573

574 575 576 577
;; Local Variables:
;; generated-autoload-file: "esh-groups.el"
;; End:

Gerd Moellmann's avatar
Gerd Moellmann committed
578
;;; em-dirs.el ends here