1 ;;; em-dirs.el --- directory navigation commands
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008 Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; The only special feature that Eshell offers in the last-dir-ring.
26 ;; To view the ring, enter:
30 ;; Changing to an index within the ring is done using:
32 ;; cd - ; same as cd -0
35 ;; Or, it is possible to change the first member in the ring which
38 ;; cd =bcc ; change to the last directory visited containing "bcc"
40 ;; This ring is maintained automatically, and is persisted across
41 ;; Eshell sessions. It is a separate mechanism from `pushd' and
42 ;; `popd', and the two may be used at the same time.
51 (eshell-defgroup eshell-dirs nil
52 "Directory navigation involves changing directories, examining the
53 current directory, maintaining a directory stack, and also keeping
54 track of a history of the last directory locations the user was in.
55 Emacs does provide standard Lisp definitions of `pwd' and `cd', but
56 they lack somewhat in feel from the typical shell equivalents."
57 :tag
"Directory navigation"
58 :group
'eshell-module
)
62 (defcustom eshell-dirs-load-hook
'(eshell-dirs-initialize)
63 "*A hook that gets run when `eshell-dirs' is loaded."
67 (defcustom eshell-pwd-convert-function
(if (eshell-under-windows-p)
70 "*The function used to normalize the value of Eshell's `pwd'.
71 The value returned by `pwd' is also used when recording the
72 last-visited directory in the last-dir-ring, so it will affect the
73 form of the list used by 'cd ='."
74 :type
'(radio (function-item file-truename
)
75 (function-item expand-file-name
)
76 (function-item identity
)
77 (function :tag
"Other"))
80 (defcustom eshell-ask-to-save-last-dir
'always
81 "*Determine if the last-dir-ring should be automatically saved.
82 The last-dir-ring is always preserved when exiting an Eshell buffer.
83 However, when Emacs is being shut down, this variable determines
84 whether to prompt the user, or just save the ring.
85 If set to nil, it means never ask whether to save the last-dir-ring.
86 If set to t, always ask if any Eshell buffers are open at exit time.
87 If set to `always', the list-dir-ring will always be saved, silently."
88 :type
'(choice (const :tag
"Never" nil
)
90 (const :tag
"Always save" always
))
93 (defcustom eshell-cd-shows-directory nil
94 "*If non-nil, using `cd' will report the directory it changes to."
98 (defcustom eshell-cd-on-directory t
99 "*If non-nil, do a cd if a directory is in command position."
103 (defcustom eshell-directory-change-hook nil
104 "*A hook to run when the current directory changes."
108 (defcustom eshell-list-files-after-cd nil
109 "*If non-nil, call \"ls\" with any remaining args after doing a cd.
110 This is provided for convenience, since the same effect is easily
111 achieved by adding a function to `eshell-directory-change-hook' that
112 calls \"ls\" and references `eshell-last-arguments'."
116 (defcustom eshell-pushd-tohome nil
117 "*If non-nil, make pushd with no arg behave as 'pushd ~' (like `cd').
118 This mirrors the optional behavior of tcsh."
122 (defcustom eshell-pushd-dextract nil
123 "*If non-nil, make \"pushd +n\" pop the nth dir to the stack top.
124 This mirrors the optional behavior of tcsh."
128 (defcustom eshell-pushd-dunique nil
129 "*If non-nil, make pushd only add unique directories to the stack.
130 This mirrors the optional behavior of tcsh."
134 (defcustom eshell-dirtrack-verbose t
135 "*If non-nil, show the directory stack following directory change.
136 This is effective only if directory tracking is enabled."
140 (defcustom eshell-last-dir-ring-file-name
141 (concat eshell-directory-name
"lastdir")
142 "*If non-nil, name of the file to read/write the last-dir-ring.
143 See also `eshell-read-last-dir-ring' and `eshell-write-last-dir-ring'.
144 If it is nil, the last-dir-ring will not be written to disk."
148 (defcustom eshell-last-dir-ring-size
32
149 "*If non-nil, the size of the directory history ring.
150 This ring is added to every time `cd' or `pushd' is used. It simply
151 stores the most recent directory locations Eshell has been in. To
152 return to the most recent entry, use 'cd -' (equivalent to 'cd -0').
153 To return to an older entry, use 'cd -N', where N is an integer less
154 than `eshell-last-dir-ring-size'. To return to the last directory
155 matching a particular regexp, use 'cd =REGEXP'. To display the
156 directory history list, use 'cd ='.
158 This mechanism is very similar to that provided by `pushd', except
159 it's far more automatic. `pushd' allows the user to decide which
160 directories gets pushed, and its size is unlimited.
162 `eshell-last-dir-ring' is meant for users who don't use `pushd'
163 explicity very much, but every once in a while would like to return to
164 a previously visited directory without having to type in the whole
169 (defcustom eshell-last-dir-unique t
170 "*If non-nil, `eshell-last-dir-ring' contains only unique entries."
174 ;;; Internal Variables:
176 (defvar eshell-dirstack nil
177 "List of directories saved by pushd in the Eshell buffer.
178 Thus, this does not include the current directory.")
180 (defvar eshell-last-dir-ring nil
181 "The last directory that eshell was in.")
185 (defun eshell-dirs-initialize ()
186 "Initialize the builtin functions for Eshell."
187 (make-local-variable 'eshell-variable-aliases-list
)
188 (setq eshell-variable-aliases-list
190 eshell-variable-aliases-list
191 '(("-" (lambda (indices)
193 (unless (ring-empty-p eshell-last-dir-ring
)
195 (ring-ref eshell-last-dir-ring
0)))
197 (eshell-apply-indices eshell-last-dir-ring indices
)))))
199 ("PWD" (lambda (indices)
200 (expand-file-name (eshell/pwd
))) t
)
201 ("OLDPWD" (lambda (indices)
202 (unless (ring-empty-p eshell-last-dir-ring
)
204 (ring-ref eshell-last-dir-ring
0)))) t
))))
206 (when eshell-cd-on-directory
207 (make-local-variable 'eshell-interpreter-alist
)
208 (setq eshell-interpreter-alist
209 (cons (cons 'eshell-lone-directory-p
210 'eshell-dirs-substitute-cd
)
211 eshell-interpreter-alist
)))
213 (add-hook 'eshell-parse-argument-hook
214 'eshell-parse-user-reference nil t
)
215 (if (eshell-under-windows-p)
216 (add-hook 'eshell-parse-argument-hook
217 'eshell-parse-drive-letter nil t
))
219 (when (eshell-using-module 'eshell-cmpl
)
220 (add-hook 'pcomplete-try-first-hook
221 'eshell-complete-user-reference nil t
))
223 (make-local-variable 'eshell-dirstack
)
224 (make-local-variable 'eshell-last-dir-ring
)
226 (if eshell-last-dir-ring-file-name
227 (eshell-read-last-dir-ring))
228 (unless eshell-last-dir-ring
229 (setq eshell-last-dir-ring
(make-ring eshell-last-dir-ring-size
)))
231 (add-hook 'eshell-exit-hook
'eshell-write-last-dir-ring nil t
)
233 (add-hook 'kill-emacs-hook
'eshell-save-some-last-dir
))
235 (defun eshell-save-some-last-dir ()
236 "Save the list-dir-ring for any open Eshell buffers."
237 (eshell-for buf
(buffer-list)
238 (if (buffer-live-p buf
)
239 (with-current-buffer buf
241 eshell-ask-to-save-last-dir
242 (or (eq eshell-ask-to-save-last-dir
'always
)
244 (format "Save last dir ring for Eshell buffer `%s'? "
245 (buffer-name buf
)))))
246 (eshell-write-last-dir-ring))))))
248 (defun eshell-lone-directory-p (file)
249 "Test whether FILE is just a directory name, and not a command name."
250 (and (file-directory-p file
)
251 (or (file-name-directory file
)
252 (not (eshell-search-path file
)))))
254 (defun eshell-dirs-substitute-cd (&rest args
)
255 "Substitute the given command for a call to `cd' on that name."
256 (if (> (length args
) 1)
257 (error "%s: command not found" (car args
))
258 (throw 'eshell-replace-command
259 (eshell-parse-command "cd" (eshell-flatten-list args
)))))
261 (defun eshell-parse-user-reference ()
262 "An argument beginning with ~ is a filename to be expanded."
263 (when (and (not eshell-current-argument
)
264 (eq (char-after) ?~
))
265 (add-to-list 'eshell-current-modifiers
'expand-file-name
)
267 (char-to-string (char-before))))
269 (defun eshell-parse-drive-letter ()
270 "An argument beginning X:[^/] is a drive letter reference."
271 (when (and (not eshell-current-argument
)
272 (looking-at "\\([A-Za-z]:\\)\\([^/\\\\]\\|\\'\\)"))
273 (goto-char (match-end 1))
274 (let* ((letter (match-string 1))
275 (regexp (concat "\\`" letter
))
276 (path (eshell-find-previous-directory regexp
)))
277 (concat (or path letter
) "/"))))
279 (defun eshell-complete-user-reference ()
280 "If there is a user reference, complete it."
281 (let ((arg (pcomplete-actual-arg)))
282 (when (string-match "\\`~[a-z]*\\'" arg
)
283 (setq pcomplete-stub
(substring arg
1)
284 pcomplete-last-completion-raw t
)
285 (throw 'pcomplete-completions
287 (eshell-read-user-names)
288 (pcomplete-uniqify-list
292 (file-name-as-directory (cdr user
))))
293 eshell-user-names
)))))))
295 (defun eshell/pwd
(&rest args
)
296 "Change output from `pwd` to be cleaner."
297 (let* ((path default-directory
)
300 (eq (aref path
(1- len
)) ?
/)
301 (not (and (eshell-under-windows-p)
302 (string-match "\\`[A-Za-z]:[\\\\/]\\'" path
))))
303 (setq path
(substring path
0 (1- (length path
)))))
304 (if eshell-pwd-convert-function
305 (funcall eshell-pwd-convert-function path
)
308 (defun eshell-expand-multiple-dots (path)
309 "Convert '...' to '../..', '....' to '../../..', etc..
311 With the following piece of advice, you can make this functionality
312 available in most of Emacs, with the exception of filename completion
315 (defadvice expand-file-name
316 (before translate-multiple-dots
317 (filename &optional directory) activate)
318 (setq filename (eshell-expand-multiple-dots filename)))"
319 (while (string-match "\\(?:^\\|/\\)\\.\\.\\(\\.+\\)\\(?:$\\|/\\)" path
)
320 (let* ((extra-dots (match-string 1 path
))
321 (len (length extra-dots
))
324 (setq replace-text
(concat replace-text
"/..")
327 (replace-match replace-text t t path
1))))
330 (defun eshell-find-previous-directory (regexp)
331 "Find the most recent last-dir matching REGEXP."
333 (len (ring-length eshell-last-dir-ring
))
335 (if (> (length regexp
) 0)
337 (setq oldpath
(ring-ref eshell-last-dir-ring index
))
338 (if (string-match regexp oldpath
)
344 (defvar dired-directory
)
346 (defun eshell/cd
(&rest args
) ; all but first ignored
347 "Alias to extend the behavior of `cd'."
348 (setq args
(eshell-flatten-list args
))
349 (let ((path (car args
))
350 (subpath (car (cdr args
)))
351 (case-fold-search (eshell-under-windows-p))
354 (setq path
(number-to-string path
)))
355 (if (numberp subpath
)
356 (setq subpath
(number-to-string subpath
)))
359 (let ((curdir (eshell/pwd
)))
360 (if (string-match path curdir
)
361 (setq path
(replace-match subpath nil nil curdir
))
362 (error "Path substring '%s' not found" path
))))
363 ((and path
(string-match "^-\\([0-9]*\\)$" path
))
364 (let ((index (match-string 1 path
)))
366 (ring-remove eshell-last-dir-ring
368 (string-to-number index
)
370 ((and path
(string-match "^=\\(.*\\)$" path
))
371 (let ((oldpath (eshell-find-previous-directory
372 (match-string 1 path
))))
375 (let ((len (ring-length eshell-last-dir-ring
))
378 (error "Directory ring empty"))
379 (eshell-init-print-buffer)
381 (eshell-buffered-print
382 (concat (number-to-string index
) ": "
383 (ring-ref eshell-last-dir-ring index
) "\n"))
384 (setq index
(1+ index
)))
388 (setq path
(eshell-expand-multiple-dots path
))))
390 (setq dired-directory
(or path
"~"))
391 (let ((curdir (eshell/pwd
)))
392 (unless (equal curdir dired-directory
)
393 (eshell-add-to-dir-ring curdir
))
394 (let ((result (cd dired-directory
)))
395 (and eshell-cd-shows-directory
396 (eshell-printn result
)))
397 (run-hooks 'eshell-directory-change-hook
)
398 (if eshell-list-files-after-cd
399 (throw 'eshell-replace-command
400 (eshell-parse-command "ls" (cdr args
))))
403 (put 'eshell
/cd
'eshell-no-numeric-conversions t
)
405 (defun eshell-add-to-dir-ring (path)
406 "Add PATH to the last-dir-ring, if applicable."
407 (unless (and (not (ring-empty-p eshell-last-dir-ring
))
408 (equal path
(ring-ref eshell-last-dir-ring
0)))
409 (if eshell-last-dir-unique
411 (len (ring-length eshell-last-dir-ring
)))
413 (if (equal (ring-ref eshell-last-dir-ring index
) path
)
414 (ring-remove eshell-last-dir-ring index
)
415 (setq index
(1+ index
))))))
416 (ring-insert eshell-last-dir-ring path
)))
419 (defun eshell/pushd
(&rest args
) ; all but first ignored
420 "Implementation of pushd in Lisp."
421 (let ((path (car args
)))
424 ;; no arg -- swap pwd and car of stack unless eshell-pushd-tohome
425 (cond (eshell-pushd-tohome
428 (let ((old (eshell/pwd
)))
429 (eshell/cd
(car eshell-dirstack
))
430 (setq eshell-dirstack
(cons old
(cdr eshell-dirstack
)))
433 (error "pushd: No other directory"))))
434 ((string-match "^\\+\\([0-9]\\)" path
)
436 (setq path
(string-to-number (match-string 1 path
)))
437 (cond ((> path
(length eshell-dirstack
))
438 (error "Directory stack not that deep"))
440 (error "Couldn't cd"))
441 (eshell-pushd-dextract
442 (let ((dir (nth (1- path
) eshell-dirstack
)))
444 (eshell/pushd
(eshell/pwd
))
448 (let* ((ds (cons (eshell/pwd
) eshell-dirstack
))
450 (front (nthcdr path ds
))
451 (back (nreverse (nthcdr (- dslen path
) (reverse ds
))))
452 (new-ds (append front back
)))
453 (eshell/cd
(car new-ds
))
454 (setq eshell-dirstack
(cdr new-ds
))
458 (let ((old-wd (eshell/pwd
)))
460 (if (or (null eshell-pushd-dunique
)
461 (not (member old-wd eshell-dirstack
)))
462 (setq eshell-dirstack
(cons old-wd eshell-dirstack
)))
466 (put 'eshell
/pushd
'eshell-no-numeric-conversions t
)
469 (defun eshell/popd
(&rest args
)
470 "Implementation of popd in Lisp."
471 (let ((ref (or (car args
) "+0")))
472 (unless (and (stringp ref
)
473 (string-match "\\`\\([+-][0-9]+\\)\\'" ref
))
474 (error "popd: bad arg `%s'" ref
))
475 (setq ref
(string-to-number (match-string 1 ref
)))
477 (unless eshell-dirstack
478 (error "popd: Directory stack empty"))
479 (eshell/cd
(car eshell-dirstack
))
480 (setq eshell-dirstack
(cdr eshell-dirstack
))
482 ((<= (abs ref
) (length eshell-dirstack
))
483 (let* ((ds (cons nil eshell-dirstack
))
484 (cell (nthcdr (if (> ref
0)
486 (+ (length eshell-dirstack
) ref
)) ds
))
489 (setcdr cell
(cdr (cdr cell
)))
490 (setq eshell-dirstack
(cdr ds
))
493 (error "Couldn't popd"))))
496 (put 'eshell
/popd
'eshell-no-numeric-conversions t
)
498 (defun eshell/dirs
(&optional if-verbose
)
499 "Implementation of dirs in Lisp."
500 (when (or (not if-verbose
) eshell-dirtrack-verbose
)
502 (ds (cons (eshell/pwd
) eshell-dirstack
))
503 (home (expand-file-name "~/"))
504 (homelen (length home
)))
506 (let ((dir (car ds
)))
507 (and (>= (length dir
) homelen
)
508 (string= home
(substring dir
0 homelen
))
509 (setq dir
(concat "~/" (substring dir homelen
))))
510 (setq msg
(concat msg
(directory-file-name dir
) " "))
514 (defun eshell-read-last-dir-ring ()
515 "Sets the buffer's `eshell-last-dir-ring' from a history file."
516 (let ((file eshell-last-dir-ring-file-name
))
520 (not (file-readable-p file
)))
524 (size eshell-last-dir-ring-size
)
525 (ring (make-ring size
)))
527 (insert-file-contents file
)
528 ;; Save restriction in case file is already visited...
529 ;; Watch for those date stamps in history files!
530 (goto-char (point-max))
531 (while (and (< count size
)
532 (re-search-backward "^\\([^\n].*\\)$" nil t
))
533 (ring-insert-at-beginning ring
(match-string 1))
534 (setq count
(1+ count
)))
535 ;; never allow the top element to equal the current
537 (while (and (not (ring-empty-p ring
))
538 (equal (ring-ref ring
0) (eshell/pwd
)))
539 (ring-remove ring
0)))
540 (setq eshell-last-dir-ring ring
))))))
542 (defun eshell-write-last-dir-ring ()
543 "Writes the buffer's `eshell-last-dir-ring' to a history file."
544 (let ((file eshell-last-dir-ring-file-name
))
548 (null eshell-last-dir-ring
)
549 (ring-empty-p eshell-last-dir-ring
))
551 ((not (file-writable-p file
))
552 (message "Cannot write last-dir-ring file %s" file
))
554 (let* ((ring eshell-last-dir-ring
)
555 (index (ring-length ring
)))
558 (setq index
(1- index
))
559 (insert (ring-ref ring index
) ?
\n))
560 (insert (eshell/pwd
) ?
\n)
561 (eshell-with-private-file-modes
562 (write-region (point-min) (point-max) file nil
568 ;; generated-autoload-file: "esh-groups.el"
571 ;; arch-tag: 1e9c5a95-f1bd-45f8-ad36-55aac706e787
572 ;;; em-dirs.el ends here