1 ;;; em-dirs --- directory navigation commands
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
24 (eval-when-compile (require 'esh-maint
))
26 (defgroup eshell-dirs nil
27 "Directory navigation involves changing directories, examining the
28 current directory, maintaining a directory stack, and also keeping
29 track of a history of the last directory locations the user was in.
30 Emacs does provide standard Lisp definitions of `pwd' and `cd', but
31 they lack somewhat in feel from the typical shell equivalents."
32 :tag
"Directory navigation"
33 :group
'eshell-module
)
37 ;; The only special feature that Eshell offers in the last-dir-ring.
38 ;; To view the ring, enter:
42 ;; Changing to an index within the ring is done using:
44 ;; cd - ; same as cd -0
47 ;; Or, it is possible to change the first member in the ring which
50 ;; cd =bcc ; change to the last directory visited containing "bcc"
52 ;; This ring is maintained automatically, and is persisted across
53 ;; Eshell sessions. It is a separate mechanism from `pushd' and
54 ;; `popd', and the two may be used at the same time.
61 (defcustom eshell-dirs-load-hook
'(eshell-dirs-initialize)
62 "*A hook that gets run when `eshell-dirs' is loaded."
66 (defcustom eshell-pwd-convert-function
(if (eshell-under-windows-p)
69 "*The function used to normalize the value of Eshell's `pwd'.
70 The value returned by `pwd' is also used when recording the
71 last-visited directory in the last-dir-ring, so it will affect the
72 form of the list used by 'cd ='."
73 :type
'(radio (function-item file-truename
)
74 (function-item expand-file-name
)
75 (function-item identity
)
76 (function :tag
"Other"))
79 (defcustom eshell-ask-to-save-last-dir
'always
80 "*Determine if the last-dir-ring should be automatically saved.
81 The last-dir-ring is always preserved when exiting an Eshell buffer.
82 However, when Emacs is being shut down, this variable determines
83 whether to prompt the user, or just save the ring.
84 If set to nil, it means never ask whether to save the last-dir-ring.
85 If set to t, always ask if any Eshell buffers are open at exit time.
86 If set to `always', the list-dir-ring will always be saved, silently."
87 :type
'(choice (const :tag
"Never" nil
)
89 (const :tag
"Always save" always
))
92 (defcustom eshell-cd-shows-directory nil
93 "*If non-nil, using `cd' will report the directory it changes to."
97 (defcustom eshell-cd-on-directory t
98 "*If non-nil, do a cd if a directory is in command position."
102 (defcustom eshell-directory-change-hook nil
103 "*A hook to run when the current directory changes."
107 (defcustom eshell-list-files-after-cd nil
108 "*If non-nil, call \"ls\" with any remaining args after doing a cd.
109 This is provided for convenience, since the same effect is easily
110 achieved by adding a function to `eshell-directory-change-hook' that
111 calls \"ls\" and references `eshell-last-arguments'."
115 (defcustom eshell-pushd-tohome nil
116 "*If non-nil, make pushd with no arg behave as 'pushd ~' (like `cd').
117 This mirrors the optional behavior of tcsh."
121 (defcustom eshell-pushd-dextract nil
122 "*If non-nil, make \"pushd +n\" pop the nth dir to the stack top.
123 This mirrors the optional behavior of tcsh."
127 (defcustom eshell-pushd-dunique nil
128 "*If non-nil, make pushd only add unique directories to the stack.
129 This mirrors the optional behavior of tcsh."
133 (defcustom eshell-dirtrack-verbose t
134 "*If non-nil, show the directory stack following directory change.
135 This is effective only if directory tracking is enabled."
139 (defcustom eshell-last-dir-ring-file-name
140 (concat eshell-directory-name
"lastdir")
141 "*If non-nil, name of the file to read/write the last-dir-ring.
142 See also `eshell-read-last-dir-ring' and `eshell-write-last-dir-ring'.
143 If it is nil, the last-dir-ring will not be written to disk."
147 (defcustom eshell-last-dir-ring-size
32
148 "*If non-nil, the size of the directory history ring.
149 This ring is added to every time `cd' or `pushd' is used. It simply
150 stores the most recent directory locations Eshell has been in. To
151 return to the most recent entry, use 'cd -' (equivalent to 'cd -0').
152 To return to an older entry, use 'cd -N', where N is an integer less
153 than `eshell-last-dir-ring-size'. To return to the last directory
154 matching a particular regexp, use 'cd =REGEXP'. To display the
155 directory history list, use 'cd ='.
157 This mechanism is very similar to that provided by `pushd', except
158 it's far more automatic. `pushd' allows the user to decide which
159 directories gets pushed, and its size is unlimited.
161 `eshell-last-dir-ring' is meant for users who don't use `pushd'
162 explicity very much, but every once in a while would like to return to
163 a previously visited directory without having to type in the whole
168 (defcustom eshell-last-dir-unique t
169 "*If non-nil, `eshell-last-dir-ring' contains only unique entries."
173 ;;; Internal Variables:
175 (defvar eshell-dirstack nil
176 "List of directories saved by pushd in the Eshell buffer.
177 Thus, this does not include the current directory.")
179 (defvar eshell-last-dir-ring nil
180 "The last directory that eshell was in.")
184 (defun eshell-dirs-initialize ()
185 "Initialize the builtin functions for Eshell."
186 (make-local-variable 'eshell-variable-aliases-list
)
187 (setq eshell-variable-aliases-list
189 eshell-variable-aliases-list
190 '(("-" (lambda (indices)
192 (unless (ring-empty-p eshell-last-dir-ring
)
194 (ring-ref eshell-last-dir-ring
0)))
196 (eshell-apply-indices eshell-last-dir-ring indices
)))))
198 ("PWD" (lambda (indices)
199 (expand-file-name (eshell/pwd
))) t
)
200 ("OLDPWD" (lambda (indices)
201 (unless (ring-empty-p eshell-last-dir-ring
)
203 (ring-ref eshell-last-dir-ring
0)))) t
))))
205 (when eshell-cd-on-directory
206 (make-local-variable 'eshell-interpreter-alist
)
207 (setq eshell-interpreter-alist
208 (cons (cons 'eshell-lone-directory-p
209 'eshell-dirs-substitute-cd
)
210 eshell-interpreter-alist
)))
212 (make-local-hook 'eshell-parse-argument-hook
)
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 (make-local-hook 'pcomplete-try-first-hook
)
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 (make-local-hook 'eshell-exit-hook
)
233 (add-hook 'eshell-exit-hook
'eshell-write-last-dir-ring nil t
)
235 (add-hook 'kill-emacs-hook
'eshell-save-some-last-dir
))
237 (defun eshell-save-some-last-dir ()
238 "Save the list-dir-ring for any open Eshell buffers."
239 (eshell-for buf
(buffer-list)
240 (if (buffer-live-p buf
)
241 (with-current-buffer buf
243 eshell-ask-to-save-last-dir
244 (or (eq eshell-ask-to-save-last-dir
'always
)
246 (format "Save last dir ring for Eshell buffer `%s'? "
247 (buffer-name buf
)))))
248 (eshell-write-last-dir-ring))))))
250 (defun eshell-lone-directory-p (file)
251 "Test whether FILE is just a directory name, and not a command name."
252 (and (file-directory-p file
)
253 (or (file-name-directory file
)
254 (not (eshell-search-path file
)))))
256 (defun eshell-dirs-substitute-cd (&rest args
)
257 "Substitute the given command for a call to `cd' on that name."
258 (if (> (length args
) 1)
259 (error "%s: command not found" (car args
))
260 (throw 'eshell-replace-command
261 (eshell-parse-command "cd" (eshell-flatten-list args
)))))
263 (defun eshell-parse-user-reference ()
264 "An argument beginning with ~ is a filename to be expanded."
265 (when (and (not eshell-current-argument
)
266 (eq (char-after) ?~
))
267 (add-to-list 'eshell-current-modifiers
'expand-file-name
)
269 (char-to-string (char-before))))
271 (defun eshell-parse-drive-letter ()
272 "An argument beginning X:[^/] is a drive letter reference."
273 (when (and (not eshell-current-argument
)
274 (looking-at "\\([A-Za-z]:\\)\\([^/\\\\]\\|\\'\\)"))
275 (goto-char (match-end 1))
276 (let* ((letter (match-string 1))
277 (regexp (concat "\\`" letter
))
278 (path (eshell-find-previous-directory regexp
)))
279 (concat (or path letter
)
280 (char-to-string directory-sep-char
)))))
282 (defun eshell-complete-user-reference ()
283 "If there is a user reference, complete it."
284 (let ((arg (pcomplete-actual-arg)))
285 (when (string-match "\\`~[a-z]*\\'" arg
)
286 (setq pcomplete-stub
(substring arg
1)
287 pcomplete-last-completion-raw t
)
288 (throw 'pcomplete-completions
290 (eshell-read-user-names)
291 (pcomplete-uniqify-list
295 (file-name-as-directory (cdr user
))))
296 eshell-user-names
)))))))
298 (defun eshell/pwd
(&rest args
) ; ignored
299 "Change output from `pwd` to be cleaner."
300 (let* ((path default-directory
)
303 (eq (aref path
(1- len
)) directory-sep-char
)
304 (not (and (eshell-under-windows-p)
305 (string-match "\\`[A-Za-z]:[\\\\/]\\'" path
))))
306 (setq path
(substring path
0 (1- (length path
)))))
307 (if eshell-pwd-convert-function
308 (setq path
(funcall eshell-pwd-convert-function path
)))
311 (defun eshell-expand-multiple-dots (path)
312 "Convert '...' to '../..', '....' to '../../..', etc..
314 With the following piece of advice, you can make this functionality
315 available in most of Emacs, with the exception of filename completion
318 (defadvice expand-file-name
319 (before translate-multiple-dots
320 (filename &optional directory) activate)
321 (setq filename (eshell-expand-multiple-dots filename)))"
322 (while (string-match "\\.\\.\\(\\.+\\)" path
)
323 (let* ((extra-dots (match-string 1 path
))
324 (len (length extra-dots
))
329 (char-to-string directory-sep-char
) "..")
332 (replace-match replace-text t t path
1))))
335 (defun eshell-find-previous-directory (regexp)
336 "Find the most recent last-dir matching REGEXP."
338 (len (ring-length eshell-last-dir-ring
))
340 (if (> (length regexp
) 0)
342 (setq oldpath
(ring-ref eshell-last-dir-ring index
))
343 (if (string-match regexp oldpath
)
350 (defvar dired-directory
))
352 (defun eshell/cd
(&rest args
) ; all but first ignored
353 "Alias to extend the behavior of `cd'."
354 (setq args
(eshell-flatten-list args
))
355 (let ((path (car args
))
356 (subpath (car (cdr args
)))
357 (case-fold-search (eshell-under-windows-p))
360 (setq path
(number-to-string path
)))
361 (if (numberp subpath
)
362 (setq subpath
(number-to-string subpath
)))
365 (let ((curdir (eshell/pwd
)))
366 (if (string-match path curdir
)
367 (setq path
(replace-match subpath nil nil curdir
))
368 (error "Path substring '%s' not found" path
))))
369 ((and path
(string-match "^-\\([0-9]*\\)$" path
))
370 (let ((index (match-string 1 path
)))
372 (ring-remove eshell-last-dir-ring
374 (string-to-int index
)
376 ((and path
(string-match "^=\\(.*\\)$" path
))
377 (let ((oldpath (eshell-find-previous-directory
378 (match-string 1 path
))))
381 (let ((len (ring-length eshell-last-dir-ring
))
384 (error "Directory ring empty"))
385 (eshell-init-print-buffer)
387 (eshell-buffered-print
388 (concat (number-to-string index
) ": "
389 (ring-ref eshell-last-dir-ring index
) "\n"))
390 (setq index
(1+ index
)))
394 (setq path
(eshell-expand-multiple-dots path
))))
396 (setq dired-directory
(or path
"~"))
397 (let ((curdir (eshell/pwd
)))
398 (unless (equal curdir dired-directory
)
399 (eshell-add-to-dir-ring curdir
))
400 (let ((result (cd dired-directory
)))
401 (and eshell-cd-shows-directory
402 (eshell-printn result
)))
403 (run-hooks 'eshell-directory-change-hook
)
404 (if eshell-list-files-after-cd
405 (throw 'eshell-replace-command
406 (eshell-parse-command "ls" (cdr args
))))
409 (defun eshell-add-to-dir-ring (path)
410 "Add PATH to the last-dir-ring, if applicable."
411 (unless (and (not (ring-empty-p eshell-last-dir-ring
))
412 (equal path
(ring-ref eshell-last-dir-ring
0)))
413 (if eshell-last-dir-unique
415 (len (ring-length eshell-last-dir-ring
)))
417 (if (equal (ring-ref eshell-last-dir-ring index
) path
)
418 (ring-remove eshell-last-dir-ring index
)
419 (setq index
(1+ index
))))))
420 (ring-insert eshell-last-dir-ring path
)))
423 (defun eshell/pushd
(&rest args
) ; all but first ignored
424 "Implementation of pushd in Lisp."
425 (let ((path (car args
)))
428 ;; no arg -- swap pwd and car of stack unless eshell-pushd-tohome
429 (cond (eshell-pushd-tohome
432 (let ((old (eshell/pwd
)))
433 (eshell/cd
(car eshell-dirstack
))
434 (setq eshell-dirstack
(cons old
(cdr eshell-dirstack
)))
437 (error "pushd: No other directory"))))
438 ((string-match "^\\+\\([0-9]\\)" path
)
440 (setq path
(string-to-number (match-string 1 path
)))
441 (cond ((> path
(length eshell-dirstack
))
442 (error "Directory stack not that deep"))
444 (error "Couldn't cd"))
445 (eshell-pushd-dextract
446 (let ((dir (nth (1- path
) eshell-dirstack
)))
448 (eshell/pushd
(eshell/pwd
))
452 (let* ((ds (cons (eshell/pwd
) eshell-dirstack
))
454 (front (nthcdr path ds
))
455 (back (nreverse (nthcdr (- dslen path
) (reverse ds
))))
456 (new-ds (append front back
)))
457 (eshell/cd
(car new-ds
))
458 (setq eshell-dirstack
(cdr new-ds
))
462 (let ((old-wd (eshell/pwd
)))
464 (if (or (null eshell-pushd-dunique
)
465 (not (member old-wd eshell-dirstack
)))
466 (setq eshell-dirstack
(cons old-wd eshell-dirstack
)))
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 (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
567 ;;; em-dirs.el ends here