1 ;;; em-dirs.el --- directory navigation commands
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 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, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; The only special feature that Eshell offers in the last-dir-ring.
28 ;; To view the ring, enter:
32 ;; Changing to an index within the ring is done using:
34 ;; cd - ; same as cd -0
37 ;; Or, it is possible to change the first member in the ring which
40 ;; cd =bcc ; change to the last directory visited containing "bcc"
42 ;; This ring is maintained automatically, and is persisted across
43 ;; Eshell sessions. It is a separate mechanism from `pushd' and
44 ;; `popd', and the two may be used at the same time.
52 (defgroup eshell-dirs nil
53 "Directory navigation involves changing directories, examining the
54 current directory, maintaining a directory stack, and also keeping
55 track of a history of the last directory locations the user was in.
56 Emacs does provide standard Lisp definitions of `pwd' and `cd', but
57 they lack somewhat in feel from the typical shell equivalents."
58 :tag
"Directory navigation"
59 :group
'eshell-module
)
63 (defcustom eshell-dirs-load-hook
'(eshell-dirs-initialize)
64 "*A hook that gets run when `eshell-dirs' is loaded."
68 (defcustom eshell-pwd-convert-function
(if (eshell-under-windows-p)
71 "*The function used to normalize the value of Eshell's `pwd'.
72 The value returned by `pwd' is also used when recording the
73 last-visited directory in the last-dir-ring, so it will affect the
74 form of the list used by 'cd ='."
75 :type
'(radio (function-item file-truename
)
76 (function-item expand-file-name
)
77 (function-item identity
)
78 (function :tag
"Other"))
81 (defcustom eshell-ask-to-save-last-dir
'always
82 "*Determine if the last-dir-ring should be automatically saved.
83 The last-dir-ring is always preserved when exiting an Eshell buffer.
84 However, when Emacs is being shut down, this variable determines
85 whether to prompt the user, or just save the ring.
86 If set to nil, it means never ask whether to save the last-dir-ring.
87 If set to t, always ask if any Eshell buffers are open at exit time.
88 If set to `always', the list-dir-ring will always be saved, silently."
89 :type
'(choice (const :tag
"Never" nil
)
91 (const :tag
"Always save" always
))
94 (defcustom eshell-cd-shows-directory nil
95 "*If non-nil, using `cd' will report the directory it changes to."
99 (defcustom eshell-cd-on-directory t
100 "*If non-nil, do a cd if a directory is in command position."
104 (defcustom eshell-directory-change-hook nil
105 "*A hook to run when the current directory changes."
109 (defcustom eshell-list-files-after-cd nil
110 "*If non-nil, call \"ls\" with any remaining args after doing a cd.
111 This is provided for convenience, since the same effect is easily
112 achieved by adding a function to `eshell-directory-change-hook' that
113 calls \"ls\" and references `eshell-last-arguments'."
117 (defcustom eshell-pushd-tohome nil
118 "*If non-nil, make pushd with no arg behave as 'pushd ~' (like `cd').
119 This mirrors the optional behavior of tcsh."
123 (defcustom eshell-pushd-dextract nil
124 "*If non-nil, make \"pushd +n\" pop the nth dir to the stack top.
125 This mirrors the optional behavior of tcsh."
129 (defcustom eshell-pushd-dunique nil
130 "*If non-nil, make pushd only add unique directories to the stack.
131 This mirrors the optional behavior of tcsh."
135 (defcustom eshell-dirtrack-verbose t
136 "*If non-nil, show the directory stack following directory change.
137 This is effective only if directory tracking is enabled."
141 (defcustom eshell-last-dir-ring-file-name
142 (concat eshell-directory-name
"lastdir")
143 "*If non-nil, name of the file to read/write the last-dir-ring.
144 See also `eshell-read-last-dir-ring' and `eshell-write-last-dir-ring'.
145 If it is nil, the last-dir-ring will not be written to disk."
149 (defcustom eshell-last-dir-ring-size
32
150 "*If non-nil, the size of the directory history ring.
151 This ring is added to every time `cd' or `pushd' is used. It simply
152 stores the most recent directory locations Eshell has been in. To
153 return to the most recent entry, use 'cd -' (equivalent to 'cd -0').
154 To return to an older entry, use 'cd -N', where N is an integer less
155 than `eshell-last-dir-ring-size'. To return to the last directory
156 matching a particular regexp, use 'cd =REGEXP'. To display the
157 directory history list, use 'cd ='.
159 This mechanism is very similar to that provided by `pushd', except
160 it's far more automatic. `pushd' allows the user to decide which
161 directories gets pushed, and its size is unlimited.
163 `eshell-last-dir-ring' is meant for users who don't use `pushd'
164 explicity very much, but every once in a while would like to return to
165 a previously visited directory without having to type in the whole
170 (defcustom eshell-last-dir-unique t
171 "*If non-nil, `eshell-last-dir-ring' contains only unique entries."
175 ;;; Internal Variables:
177 (defvar eshell-dirstack nil
178 "List of directories saved by pushd in the Eshell buffer.
179 Thus, this does not include the current directory.")
181 (defvar eshell-last-dir-ring nil
182 "The last directory that eshell was in.")
186 (defun eshell-dirs-initialize ()
187 "Initialize the builtin functions for Eshell."
188 (make-local-variable 'eshell-variable-aliases-list
)
189 (setq eshell-variable-aliases-list
191 eshell-variable-aliases-list
192 '(("-" (lambda (indices)
194 (unless (ring-empty-p eshell-last-dir-ring
)
196 (ring-ref eshell-last-dir-ring
0)))
198 (eshell-apply-indices eshell-last-dir-ring indices
)))))
200 ("PWD" (lambda (indices)
201 (expand-file-name (eshell/pwd
))) t
)
202 ("OLDPWD" (lambda (indices)
203 (unless (ring-empty-p eshell-last-dir-ring
)
205 (ring-ref eshell-last-dir-ring
0)))) t
))))
207 (when eshell-cd-on-directory
208 (make-local-variable 'eshell-interpreter-alist
)
209 (setq eshell-interpreter-alist
210 (cons (cons 'eshell-lone-directory-p
211 'eshell-dirs-substitute-cd
)
212 eshell-interpreter-alist
)))
214 (add-hook 'eshell-parse-argument-hook
215 'eshell-parse-user-reference nil t
)
216 (if (eshell-under-windows-p)
217 (add-hook 'eshell-parse-argument-hook
218 'eshell-parse-drive-letter nil t
))
220 (when (eshell-using-module 'eshell-cmpl
)
221 (add-hook 'pcomplete-try-first-hook
222 'eshell-complete-user-reference nil t
))
224 (make-local-variable 'eshell-dirstack
)
225 (make-local-variable 'eshell-last-dir-ring
)
227 (if eshell-last-dir-ring-file-name
228 (eshell-read-last-dir-ring))
229 (unless eshell-last-dir-ring
230 (setq eshell-last-dir-ring
(make-ring eshell-last-dir-ring-size
)))
232 (add-hook 'eshell-exit-hook
'eshell-write-last-dir-ring nil t
)
234 (add-hook 'kill-emacs-hook
'eshell-save-some-last-dir
))
236 (defun eshell-save-some-last-dir ()
237 "Save the list-dir-ring for any open Eshell buffers."
238 (eshell-for buf
(buffer-list)
239 (if (buffer-live-p buf
)
240 (with-current-buffer buf
242 eshell-ask-to-save-last-dir
243 (or (eq eshell-ask-to-save-last-dir
'always
)
245 (format "Save last dir ring for Eshell buffer `%s'? "
246 (buffer-name buf
)))))
247 (eshell-write-last-dir-ring))))))
249 (defun eshell-lone-directory-p (file)
250 "Test whether FILE is just a directory name, and not a command name."
251 (and (file-directory-p file
)
252 (or (file-name-directory file
)
253 (not (eshell-search-path file
)))))
255 (defun eshell-dirs-substitute-cd (&rest args
)
256 "Substitute the given command for a call to `cd' on that name."
257 (if (> (length args
) 1)
258 (error "%s: command not found" (car args
))
259 (throw 'eshell-replace-command
260 (eshell-parse-command "cd" (eshell-flatten-list args
)))))
262 (defun eshell-parse-user-reference ()
263 "An argument beginning with ~ is a filename to be expanded."
264 (when (and (not eshell-current-argument
)
265 (eq (char-after) ?~
))
266 (add-to-list 'eshell-current-modifiers
'expand-file-name
)
268 (char-to-string (char-before))))
270 (defun eshell-parse-drive-letter ()
271 "An argument beginning X:[^/] is a drive letter reference."
272 (when (and (not eshell-current-argument
)
273 (looking-at "\\([A-Za-z]:\\)\\([^/\\\\]\\|\\'\\)"))
274 (goto-char (match-end 1))
275 (let* ((letter (match-string 1))
276 (regexp (concat "\\`" letter
))
277 (path (eshell-find-previous-directory regexp
)))
278 (concat (or path letter
) "/"))))
280 (defun eshell-complete-user-reference ()
281 "If there is a user reference, complete it."
282 (let ((arg (pcomplete-actual-arg)))
283 (when (string-match "\\`~[a-z]*\\'" arg
)
284 (setq pcomplete-stub
(substring arg
1)
285 pcomplete-last-completion-raw t
)
286 (throw 'pcomplete-completions
288 (eshell-read-user-names)
289 (pcomplete-uniqify-list
293 (file-name-as-directory (cdr user
))))
294 eshell-user-names
)))))))
296 (defun eshell/pwd
(&rest args
)
297 "Change output from `pwd` to be cleaner."
298 (let* ((path default-directory
)
301 (eq (aref path
(1- len
)) ?
/)
302 (not (and (eshell-under-windows-p)
303 (string-match "\\`[A-Za-z]:[\\\\/]\\'" path
))))
304 (setq path
(substring path
0 (1- (length path
)))))
305 (if eshell-pwd-convert-function
306 (funcall eshell-pwd-convert-function path
)
309 (defun eshell-expand-multiple-dots (path)
310 "Convert '...' to '../..', '....' to '../../..', etc..
312 With the following piece of advice, you can make this functionality
313 available in most of Emacs, with the exception of filename completion
316 (defadvice expand-file-name
317 (before translate-multiple-dots
318 (filename &optional directory) activate)
319 (setq filename (eshell-expand-multiple-dots filename)))"
320 (while (string-match "\\(?:^\\|/\\)\\.\\.\\(\\.+\\)\\(?:$\\|/\\)" path
)
321 (let* ((extra-dots (match-string 1 path
))
322 (len (length extra-dots
))
325 (setq replace-text
(concat replace-text
"/..")
328 (replace-match replace-text t t path
1))))
331 (defun eshell-find-previous-directory (regexp)
332 "Find the most recent last-dir matching REGEXP."
334 (len (ring-length eshell-last-dir-ring
))
336 (if (> (length regexp
) 0)
338 (setq oldpath
(ring-ref eshell-last-dir-ring index
))
339 (if (string-match regexp oldpath
)
346 (defvar dired-directory
))
348 (defun eshell/cd
(&rest args
) ; all but first ignored
349 "Alias to extend the behavior of `cd'."
350 (setq args
(eshell-flatten-list args
))
351 (let ((path (car args
))
352 (subpath (car (cdr args
)))
353 (case-fold-search (eshell-under-windows-p))
356 (setq path
(number-to-string path
)))
357 (if (numberp subpath
)
358 (setq subpath
(number-to-string subpath
)))
361 (let ((curdir (eshell/pwd
)))
362 (if (string-match path curdir
)
363 (setq path
(replace-match subpath nil nil curdir
))
364 (error "Path substring '%s' not found" path
))))
365 ((and path
(string-match "^-\\([0-9]*\\)$" path
))
366 (let ((index (match-string 1 path
)))
368 (ring-remove eshell-last-dir-ring
370 (string-to-number index
)
372 ((and path
(string-match "^=\\(.*\\)$" path
))
373 (let ((oldpath (eshell-find-previous-directory
374 (match-string 1 path
))))
377 (let ((len (ring-length eshell-last-dir-ring
))
380 (error "Directory ring empty"))
381 (eshell-init-print-buffer)
383 (eshell-buffered-print
384 (concat (number-to-string index
) ": "
385 (ring-ref eshell-last-dir-ring index
) "\n"))
386 (setq index
(1+ index
)))
390 (setq path
(eshell-expand-multiple-dots path
))))
392 (setq dired-directory
(or path
"~"))
393 (let ((curdir (eshell/pwd
)))
394 (unless (equal curdir dired-directory
)
395 (eshell-add-to-dir-ring curdir
))
396 (let ((result (cd dired-directory
)))
397 (and eshell-cd-shows-directory
398 (eshell-printn result
)))
399 (run-hooks 'eshell-directory-change-hook
)
400 (if eshell-list-files-after-cd
401 (throw 'eshell-replace-command
402 (eshell-parse-command "ls" (cdr args
))))
405 (put 'eshell
/cd
'eshell-no-numeric-conversions t
)
407 (defun eshell-add-to-dir-ring (path)
408 "Add PATH to the last-dir-ring, if applicable."
409 (unless (and (not (ring-empty-p eshell-last-dir-ring
))
410 (equal path
(ring-ref eshell-last-dir-ring
0)))
411 (if eshell-last-dir-unique
413 (len (ring-length eshell-last-dir-ring
)))
415 (if (equal (ring-ref eshell-last-dir-ring index
) path
)
416 (ring-remove eshell-last-dir-ring index
)
417 (setq index
(1+ index
))))))
418 (ring-insert eshell-last-dir-ring path
)))
421 (defun eshell/pushd
(&rest args
) ; all but first ignored
422 "Implementation of pushd in Lisp."
423 (let ((path (car args
)))
426 ;; no arg -- swap pwd and car of stack unless eshell-pushd-tohome
427 (cond (eshell-pushd-tohome
430 (let ((old (eshell/pwd
)))
431 (eshell/cd
(car eshell-dirstack
))
432 (setq eshell-dirstack
(cons old
(cdr eshell-dirstack
)))
435 (error "pushd: No other directory"))))
436 ((string-match "^\\+\\([0-9]\\)" path
)
438 (setq path
(string-to-number (match-string 1 path
)))
439 (cond ((> path
(length eshell-dirstack
))
440 (error "Directory stack not that deep"))
442 (error "Couldn't cd"))
443 (eshell-pushd-dextract
444 (let ((dir (nth (1- path
) eshell-dirstack
)))
446 (eshell/pushd
(eshell/pwd
))
450 (let* ((ds (cons (eshell/pwd
) eshell-dirstack
))
452 (front (nthcdr path ds
))
453 (back (nreverse (nthcdr (- dslen path
) (reverse ds
))))
454 (new-ds (append front back
)))
455 (eshell/cd
(car new-ds
))
456 (setq eshell-dirstack
(cdr new-ds
))
460 (let ((old-wd (eshell/pwd
)))
462 (if (or (null eshell-pushd-dunique
)
463 (not (member old-wd eshell-dirstack
)))
464 (setq eshell-dirstack
(cons old-wd eshell-dirstack
)))
468 (put 'eshell
/pushd
'eshell-no-numeric-conversions t
)
471 (defun eshell/popd
(&rest args
)
472 "Implementation of popd in Lisp."
473 (let ((ref (or (car args
) "+0")))
474 (unless (and (stringp ref
)
475 (string-match "\\`\\([+-][0-9]+\\)\\'" ref
))
476 (error "popd: bad arg `%s'" ref
))
477 (setq ref
(string-to-number (match-string 1 ref
)))
479 (unless eshell-dirstack
480 (error "popd: Directory stack empty"))
481 (eshell/cd
(car eshell-dirstack
))
482 (setq eshell-dirstack
(cdr eshell-dirstack
))
484 ((<= (abs ref
) (length eshell-dirstack
))
485 (let* ((ds (cons nil eshell-dirstack
))
486 (cell (nthcdr (if (> ref
0)
488 (+ (length eshell-dirstack
) ref
)) ds
))
491 (setcdr cell
(cdr (cdr cell
)))
492 (setq eshell-dirstack
(cdr ds
))
495 (error "Couldn't popd"))))
498 (put 'eshell
/popd
'eshell-no-numeric-conversions t
)
500 (defun eshell/dirs
(&optional if-verbose
)
501 "Implementation of dirs in Lisp."
502 (when (or (not if-verbose
) eshell-dirtrack-verbose
)
504 (ds (cons (eshell/pwd
) eshell-dirstack
))
505 (home (expand-file-name "~/"))
506 (homelen (length home
)))
508 (let ((dir (car ds
)))
509 (and (>= (length dir
) homelen
)
510 (string= home
(substring dir
0 homelen
))
511 (setq dir
(concat "~/" (substring dir homelen
))))
512 (setq msg
(concat msg
(directory-file-name dir
) " "))
516 (defun eshell-read-last-dir-ring ()
517 "Sets the buffer's `eshell-last-dir-ring' from a history file."
518 (let ((file eshell-last-dir-ring-file-name
))
522 (not (file-readable-p file
)))
526 (size eshell-last-dir-ring-size
)
527 (ring (make-ring size
)))
529 (insert-file-contents file
)
530 ;; Save restriction in case file is already visited...
531 ;; Watch for those date stamps in history files!
532 (goto-char (point-max))
533 (while (and (< count size
)
534 (re-search-backward "^\\([^\n].*\\)$" nil t
))
535 (ring-insert-at-beginning ring
(match-string 1))
536 (setq count
(1+ count
)))
537 ;; never allow the top element to equal the current
539 (while (and (not (ring-empty-p ring
))
540 (equal (ring-ref ring
0) (eshell/pwd
)))
541 (ring-remove ring
0)))
542 (setq eshell-last-dir-ring ring
))))))
544 (defun eshell-write-last-dir-ring ()
545 "Writes the buffer's `eshell-last-dir-ring' to a history file."
546 (let ((file eshell-last-dir-ring-file-name
))
550 (null eshell-last-dir-ring
)
551 (ring-empty-p eshell-last-dir-ring
))
553 ((not (file-writable-p file
))
554 (message "Cannot write last-dir-ring file %s" file
))
556 (let* ((ring eshell-last-dir-ring
)
557 (index (ring-length ring
)))
560 (setq index
(1- index
))
561 (insert (ring-ref ring index
) ?
\n))
562 (insert (eshell/pwd
) ?
\n)
563 (eshell-with-private-file-modes
564 (write-region (point-min) (point-max) file nil
569 ;; arch-tag: 1e9c5a95-f1bd-45f8-ad36-55aac706e787
570 ;;; em-dirs.el ends here