Some fixes to follow coding conventions.
[bpt/emacs.git] / lisp / eshell / em-dirs.el
CommitLineData
60370d40 1;;; em-dirs.el --- directory navigation commands
affbf647 2
faadfb0a 3;; Copyright (C) 1999, 2000 Free Software Foundation
affbf647 4
7de5b421
GM
5;; Author: John Wiegley <johnw@gnu.org>
6
affbf647
GM
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software; you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation; either version 2, or (at your option)
12;; any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs; see the file COPYING. If not, write to the
21;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22;; Boston, MA 02111-1307, USA.
23
24(provide 'em-dirs)
25
26(eval-when-compile (require 'esh-maint))
27
28(defgroup eshell-dirs nil
29 "Directory navigation involves changing directories, examining the
30current directory, maintaining a directory stack, and also keeping
31track of a history of the last directory locations the user was in.
32Emacs does provide standard Lisp definitions of `pwd' and `cd', but
33they lack somewhat in feel from the typical shell equivalents."
34 :tag "Directory navigation"
35 :group 'eshell-module)
36
37;;; Commentary:
38
39;; The only special feature that Eshell offers in the last-dir-ring.
40;; To view the ring, enter:
41;;
42;; cd =
43;;
44;; Changing to an index within the ring is done using:
45;;
46;; cd - ; same as cd -0
47;; cd -4
48;;
49;; Or, it is possible to change the first member in the ring which
50;; matches a regexp:
51;;
52;; cd =bcc ; change to the last directory visited containing "bcc"
53;;
54;; This ring is maintained automatically, and is persisted across
55;; Eshell sessions. It is a separate mechanism from `pushd' and
56;; `popd', and the two may be used at the same time.
57
58(require 'ring)
59(require 'esh-opt)
60
61;;; User Variables:
62
63(defcustom eshell-dirs-load-hook '(eshell-dirs-initialize)
64 "*A hook that gets run when `eshell-dirs' is loaded."
65 :type 'hook
66 :group 'eshell-dirs)
67
68(defcustom eshell-pwd-convert-function (if (eshell-under-windows-p)
69 'expand-file-name
70 'identity)
71 "*The function used to normalize the value of Eshell's `pwd'.
72The value returned by `pwd' is also used when recording the
73last-visited directory in the last-dir-ring, so it will affect the
74form 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"))
79 :group 'eshell-dirs)
80
81(defcustom eshell-ask-to-save-last-dir 'always
82 "*Determine if the last-dir-ring should be automatically saved.
83The last-dir-ring is always preserved when exiting an Eshell buffer.
84However, when Emacs is being shut down, this variable determines
85whether to prompt the user, or just save the ring.
86If set to nil, it means never ask whether to save the last-dir-ring.
87If set to t, always ask if any Eshell buffers are open at exit time.
88If set to `always', the list-dir-ring will always be saved, silently."
89 :type '(choice (const :tag "Never" nil)
90 (const :tag "Ask" t)
91 (const :tag "Always save" always))
92 :group 'eshell-dirs)
93
94(defcustom eshell-cd-shows-directory nil
95 "*If non-nil, using `cd' will report the directory it changes to."
96 :type 'boolean
97 :group 'eshell-dirs)
98
99(defcustom eshell-cd-on-directory t
100 "*If non-nil, do a cd if a directory is in command position."
101 :type 'boolean
102 :group 'eshell-dirs)
103
104(defcustom eshell-directory-change-hook nil
105 "*A hook to run when the current directory changes."
106 :type 'hook
107 :group 'eshell-dirs)
108
109(defcustom eshell-list-files-after-cd nil
110 "*If non-nil, call \"ls\" with any remaining args after doing a cd.
111This is provided for convenience, since the same effect is easily
112achieved by adding a function to `eshell-directory-change-hook' that
113calls \"ls\" and references `eshell-last-arguments'."
114 :type 'boolean
115 :group 'eshell-dirs)
116
117(defcustom eshell-pushd-tohome nil
118 "*If non-nil, make pushd with no arg behave as 'pushd ~' (like `cd').
119This mirrors the optional behavior of tcsh."
120 :type 'boolean
121 :group 'eshell-dirs)
122
123(defcustom eshell-pushd-dextract nil
124 "*If non-nil, make \"pushd +n\" pop the nth dir to the stack top.
125This mirrors the optional behavior of tcsh."
126 :type 'boolean
127 :group 'eshell-dirs)
128
129(defcustom eshell-pushd-dunique nil
130 "*If non-nil, make pushd only add unique directories to the stack.
131This mirrors the optional behavior of tcsh."
132 :type 'boolean
133 :group 'eshell-dirs)
134
135(defcustom eshell-dirtrack-verbose t
136 "*If non-nil, show the directory stack following directory change.
137This is effective only if directory tracking is enabled."
138 :type 'boolean
139 :group 'eshell-dirs)
140
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.
144See also `eshell-read-last-dir-ring' and `eshell-write-last-dir-ring'.
145If it is nil, the last-dir-ring will not be written to disk."
146 :type 'file
147 :group 'eshell-dirs)
148
149(defcustom eshell-last-dir-ring-size 32
150 "*If non-nil, the size of the directory history ring.
151This ring is added to every time `cd' or `pushd' is used. It simply
152stores the most recent directory locations Eshell has been in. To
153return to the most recent entry, use 'cd -' (equivalent to 'cd -0').
154To return to an older entry, use 'cd -N', where N is an integer less
155than `eshell-last-dir-ring-size'. To return to the last directory
156matching a particular regexp, use 'cd =REGEXP'. To display the
157directory history list, use 'cd ='.
158
159This mechanism is very similar to that provided by `pushd', except
160it's far more automatic. `pushd' allows the user to decide which
161directories gets pushed, and its size is unlimited.
162
163`eshell-last-dir-ring' is meant for users who don't use `pushd'
164explicity very much, but every once in a while would like to return to
165a previously visited directory without having to type in the whole
166thing again."
167 :type 'integer
168 :group 'eshell-dirs)
169
170(defcustom eshell-last-dir-unique t
171 "*If non-nil, `eshell-last-dir-ring' contains only unique entries."
172 :type 'boolean
173 :group 'eshell-dirs)
174
175;;; Internal Variables:
176
177(defvar eshell-dirstack nil
178 "List of directories saved by pushd in the Eshell buffer.
179Thus, this does not include the current directory.")
180
181(defvar eshell-last-dir-ring nil
182 "The last directory that eshell was in.")
183
184;;; Functions:
185
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
190 (append
191 eshell-variable-aliases-list
192 '(("-" (lambda (indices)
193 (if (not indices)
194 (unless (ring-empty-p eshell-last-dir-ring)
195 (expand-file-name
196 (ring-ref eshell-last-dir-ring 0)))
197 (expand-file-name
198 (eshell-apply-indices eshell-last-dir-ring indices)))))
199 ("+" "PWD")
200 ("PWD" (lambda (indices)
201 (expand-file-name (eshell/pwd))) t)
202 ("OLDPWD" (lambda (indices)
203 (unless (ring-empty-p eshell-last-dir-ring)
204 (expand-file-name
205 (ring-ref eshell-last-dir-ring 0)))) t))))
206
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)))
213
214 (make-local-hook 'eshell-parse-argument-hook)
215 (add-hook 'eshell-parse-argument-hook
216 'eshell-parse-user-reference nil t)
217 (if (eshell-under-windows-p)
218 (add-hook 'eshell-parse-argument-hook
219 'eshell-parse-drive-letter nil t))
220
221 (when (eshell-using-module 'eshell-cmpl)
222 (make-local-hook 'pcomplete-try-first-hook)
223 (add-hook 'pcomplete-try-first-hook
224 'eshell-complete-user-reference nil t))
225
226 (make-local-variable 'eshell-dirstack)
227 (make-local-variable 'eshell-last-dir-ring)
228
229 (if eshell-last-dir-ring-file-name
230 (eshell-read-last-dir-ring))
231 (unless eshell-last-dir-ring
232 (setq eshell-last-dir-ring (make-ring eshell-last-dir-ring-size)))
233
234 (make-local-hook 'eshell-exit-hook)
235 (add-hook 'eshell-exit-hook 'eshell-write-last-dir-ring nil t)
236
237 (add-hook 'kill-emacs-hook 'eshell-save-some-last-dir))
238
239(defun eshell-save-some-last-dir ()
240 "Save the list-dir-ring for any open Eshell buffers."
241 (eshell-for buf (buffer-list)
242 (if (buffer-live-p buf)
243 (with-current-buffer buf
244 (if (and eshell-mode
245 eshell-ask-to-save-last-dir
246 (or (eq eshell-ask-to-save-last-dir 'always)
247 (y-or-n-p
248 (format "Save last dir ring for Eshell buffer `%s'? "
249 (buffer-name buf)))))
250 (eshell-write-last-dir-ring))))))
251
252(defun eshell-lone-directory-p (file)
253 "Test whether FILE is just a directory name, and not a command name."
254 (and (file-directory-p file)
255 (or (file-name-directory file)
256 (not (eshell-search-path file)))))
257
258(defun eshell-dirs-substitute-cd (&rest args)
259 "Substitute the given command for a call to `cd' on that name."
260 (if (> (length args) 1)
261 (error "%s: command not found" (car args))
262 (throw 'eshell-replace-command
b4bd214e 263 (eshell-parse-command "cd" (eshell-flatten-list args)))))
affbf647
GM
264
265(defun eshell-parse-user-reference ()
266 "An argument beginning with ~ is a filename to be expanded."
267 (when (and (not eshell-current-argument)
268 (eq (char-after) ?~))
269 (add-to-list 'eshell-current-modifiers 'expand-file-name)
270 (forward-char)
271 (char-to-string (char-before))))
272
273(defun eshell-parse-drive-letter ()
274 "An argument beginning X:[^/] is a drive letter reference."
275 (when (and (not eshell-current-argument)
276 (looking-at "\\([A-Za-z]:\\)\\([^/\\\\]\\|\\'\\)"))
277 (goto-char (match-end 1))
278 (let* ((letter (match-string 1))
279 (regexp (concat "\\`" letter))
280 (path (eshell-find-previous-directory regexp)))
281 (concat (or path letter)
282 (char-to-string directory-sep-char)))))
283
284(defun eshell-complete-user-reference ()
285 "If there is a user reference, complete it."
286 (let ((arg (pcomplete-actual-arg)))
287 (when (string-match "\\`~[a-z]*\\'" arg)
288 (setq pcomplete-stub (substring arg 1)
289 pcomplete-last-completion-raw t)
290 (throw 'pcomplete-completions
291 (progn
292 (eshell-read-user-names)
293 (pcomplete-uniqify-list
294 (mapcar
295 (function
296 (lambda (user)
297 (file-name-as-directory (cdr user))))
298 eshell-user-names)))))))
299
dace60cf 300(defun eshell/pwd (&rest args)
affbf647
GM
301 "Change output from `pwd` to be cleaner."
302 (let* ((path default-directory)
303 (len (length path)))
304 (if (and (> len 1)
305 (eq (aref path (1- len)) directory-sep-char)
306 (not (and (eshell-under-windows-p)
307 (string-match "\\`[A-Za-z]:[\\\\/]\\'" path))))
308 (setq path (substring path 0 (1- (length path)))))
309 (if eshell-pwd-convert-function
dace60cf
JW
310 (funcall eshell-pwd-convert-function path)
311 path)))
affbf647
GM
312
313(defun eshell-expand-multiple-dots (path)
314 "Convert '...' to '../..', '....' to '../../..', etc..
315
316With the following piece of advice, you can make this functionality
317available in most of Emacs, with the exception of filename completion
318in the minibuffer:
319
320 (defadvice expand-file-name
321 (before translate-multiple-dots
322 (filename &optional directory) activate)
323 (setq filename (eshell-expand-multiple-dots filename)))"
324 (while (string-match "\\.\\.\\(\\.+\\)" path)
325 (let* ((extra-dots (match-string 1 path))
326 (len (length extra-dots))
327 replace-text)
328 (while (> len 0)
329 (setq replace-text
330 (concat replace-text
331 (char-to-string directory-sep-char) "..")
332 len (1- len)))
333 (setq path
334 (replace-match replace-text t t path 1))))
335 path)
336
337(defun eshell-find-previous-directory (regexp)
338 "Find the most recent last-dir matching REGEXP."
339 (let ((index 0)
340 (len (ring-length eshell-last-dir-ring))
341 oldpath)
342 (if (> (length regexp) 0)
343 (while (< index len)
344 (setq oldpath (ring-ref eshell-last-dir-ring index))
345 (if (string-match regexp oldpath)
346 (setq index len)
347 (setq oldpath nil
348 index (1+ index)))))
349 oldpath))
350
351(eval-when-compile
352 (defvar dired-directory))
353
354(defun eshell/cd (&rest args) ; all but first ignored
355 "Alias to extend the behavior of `cd'."
b4bd214e 356 (setq args (eshell-flatten-list args))
affbf647
GM
357 (let ((path (car args))
358 (subpath (car (cdr args)))
b4bd214e 359 (case-fold-search (eshell-under-windows-p))
affbf647
GM
360 handled)
361 (if (numberp path)
362 (setq path (number-to-string path)))
363 (if (numberp subpath)
364 (setq subpath (number-to-string subpath)))
365 (cond
366 (subpath
367 (let ((curdir (eshell/pwd)))
368 (if (string-match path curdir)
369 (setq path (replace-match subpath nil nil curdir))
370 (error "Path substring '%s' not found" path))))
371 ((and path (string-match "^-\\([0-9]*\\)$" path))
372 (let ((index (match-string 1 path)))
373 (setq path
374 (ring-remove eshell-last-dir-ring
375 (if index
376 (string-to-int index)
377 0)))))
378 ((and path (string-match "^=\\(.*\\)$" path))
379 (let ((oldpath (eshell-find-previous-directory
380 (match-string 1 path))))
381 (if oldpath
382 (setq path oldpath)
383 (let ((len (ring-length eshell-last-dir-ring))
384 (index 0))
385 (if (= len 0)
386 (error "Directory ring empty"))
ca7aae91 387 (eshell-init-print-buffer)
affbf647 388 (while (< index len)
ca7aae91 389 (eshell-buffered-print
affbf647 390 (concat (number-to-string index) ": "
ca7aae91 391 (ring-ref eshell-last-dir-ring index) "\n"))
affbf647 392 (setq index (1+ index)))
ca7aae91 393 (eshell-flush)
affbf647
GM
394 (setq handled t)))))
395 (path
396 (setq path (eshell-expand-multiple-dots path))))
397 (unless handled
398 (setq dired-directory (or path "~"))
399 (let ((curdir (eshell/pwd)))
400 (unless (equal curdir dired-directory)
401 (eshell-add-to-dir-ring curdir))
402 (let ((result (cd dired-directory)))
403 (and eshell-cd-shows-directory
404 (eshell-printn result)))
405 (run-hooks 'eshell-directory-change-hook)
406 (if eshell-list-files-after-cd
407 (throw 'eshell-replace-command
408 (eshell-parse-command "ls" (cdr args))))
409 nil))))
410
127fd3c2
JW
411(put 'eshell/cd 'eshell-no-numeric-conversions t)
412
affbf647
GM
413(defun eshell-add-to-dir-ring (path)
414 "Add PATH to the last-dir-ring, if applicable."
415 (unless (and (not (ring-empty-p eshell-last-dir-ring))
416 (equal path (ring-ref eshell-last-dir-ring 0)))
417 (if eshell-last-dir-unique
418 (let ((index 0)
419 (len (ring-length eshell-last-dir-ring)))
420 (while (< index len)
421 (if (equal (ring-ref eshell-last-dir-ring index) path)
422 (ring-remove eshell-last-dir-ring index)
423 (setq index (1+ index))))))
424 (ring-insert eshell-last-dir-ring path)))
425
426;;; pushd [+n | dir]
427(defun eshell/pushd (&rest args) ; all but first ignored
428 "Implementation of pushd in Lisp."
429 (let ((path (car args)))
430 (cond
431 ((null path)
432 ;; no arg -- swap pwd and car of stack unless eshell-pushd-tohome
433 (cond (eshell-pushd-tohome
434 (eshell/pushd "~"))
435 (eshell-dirstack
436 (let ((old (eshell/pwd)))
437 (eshell/cd (car eshell-dirstack))
438 (setq eshell-dirstack (cons old (cdr eshell-dirstack)))
439 (eshell/dirs t)))
440 (t
441 (error "pushd: No other directory"))))
442 ((string-match "^\\+\\([0-9]\\)" path)
443 ;; pushd +n
444 (setq path (string-to-number (match-string 1 path)))
445 (cond ((> path (length eshell-dirstack))
446 (error "Directory stack not that deep"))
447 ((= path 0)
448 (error "Couldn't cd"))
449 (eshell-pushd-dextract
450 (let ((dir (nth (1- path) eshell-dirstack)))
451 (eshell/popd path)
452 (eshell/pushd (eshell/pwd))
453 (eshell/cd dir)
454 (eshell/dirs t)))
455 (t
456 (let* ((ds (cons (eshell/pwd) eshell-dirstack))
457 (dslen (length ds))
458 (front (nthcdr path ds))
459 (back (nreverse (nthcdr (- dslen path) (reverse ds))))
460 (new-ds (append front back)))
461 (eshell/cd (car new-ds))
462 (setq eshell-dirstack (cdr new-ds))
463 (eshell/dirs t)))))
464 (t
465 ;; pushd <dir>
466 (let ((old-wd (eshell/pwd)))
467 (eshell/cd path)
468 (if (or (null eshell-pushd-dunique)
469 (not (member old-wd eshell-dirstack)))
470 (setq eshell-dirstack (cons old-wd eshell-dirstack)))
471 (eshell/dirs t)))))
472 nil)
473
127fd3c2
JW
474(put 'eshell/pushd 'eshell-no-numeric-conversions t)
475
affbf647
GM
476;;; popd [+n]
477(defun eshell/popd (&rest args)
478 "Implementation of popd in Lisp."
479 (let ((ref (or (car args) "+0")))
480 (unless (and (stringp ref)
481 (string-match "\\`\\([+-][0-9]+\\)\\'" ref))
482 (error "popd: bad arg `%s'" ref))
483 (setq ref (string-to-number (match-string 1 ref)))
484 (cond ((= ref 0)
485 (unless eshell-dirstack
486 (error "popd: Directory stack empty"))
487 (eshell/cd (car eshell-dirstack))
488 (setq eshell-dirstack (cdr eshell-dirstack))
489 (eshell/dirs t))
490 ((<= (abs ref) (length eshell-dirstack))
491 (let* ((ds (cons nil eshell-dirstack))
492 (cell (nthcdr (if (> ref 0)
493 (1- ref)
494 (+ (length eshell-dirstack) ref)) ds))
495 (dir (cadr cell)))
496 (eshell/cd dir)
497 (setcdr cell (cdr (cdr cell)))
498 (setq eshell-dirstack (cdr ds))
499 (eshell/dirs t)))
500 (t
501 (error "Couldn't popd"))))
502 nil)
503
127fd3c2
JW
504(put 'eshell/popd 'eshell-no-numeric-conversions t)
505
affbf647
GM
506(defun eshell/dirs (&optional if-verbose)
507 "Implementation of dirs in Lisp."
508 (when (or (not if-verbose) eshell-dirtrack-verbose)
509 (let* ((msg "")
510 (ds (cons (eshell/pwd) eshell-dirstack))
511 (home (expand-file-name "~/"))
512 (homelen (length home)))
513 (while ds
514 (let ((dir (car ds)))
515 (and (>= (length dir) homelen)
516 (string= home (substring dir 0 homelen))
517 (setq dir (concat "~/" (substring dir homelen))))
518 (setq msg (concat msg (directory-file-name dir) " "))
519 (setq ds (cdr ds))))
520 msg)))
521
522(defun eshell-read-last-dir-ring ()
523 "Sets the buffer's `eshell-last-dir-ring' from a history file."
524 (let ((file eshell-last-dir-ring-file-name))
525 (cond
526 ((or (null file)
527 (equal file "")
528 (not (file-readable-p file)))
529 nil)
530 (t
531 (let* ((count 0)
532 (size eshell-last-dir-ring-size)
533 (ring (make-ring size)))
534 (with-temp-buffer
535 (insert-file-contents file)
536 ;; Save restriction in case file is already visited...
537 ;; Watch for those date stamps in history files!
538 (goto-char (point-max))
539 (while (and (< count size)
540 (re-search-backward "^\\([^\n].*\\)$" nil t))
541 (ring-insert-at-beginning ring (match-string 1))
542 (setq count (1+ count)))
543 ;; never allow the top element to equal the current
544 ;; directory
545 (while (and (not (ring-empty-p ring))
546 (equal (ring-ref ring 0) (eshell/pwd)))
547 (ring-remove ring 0)))
548 (setq eshell-last-dir-ring ring))))))
549
550(defun eshell-write-last-dir-ring ()
551 "Writes the buffer's `eshell-last-dir-ring' to a history file."
552 (let ((file eshell-last-dir-ring-file-name))
553 (cond
554 ((or (null file)
555 (equal file "")
556 (null eshell-last-dir-ring)
557 (ring-empty-p eshell-last-dir-ring))
558 nil)
559 ((not (file-writable-p file))
560 (message "Cannot write last-dir-ring file %s" file))
561 (t
562 (let* ((ring eshell-last-dir-ring)
563 (index (ring-length ring)))
564 (with-temp-buffer
565 (while (> index 0)
566 (setq index (1- index))
567 (insert (ring-ref ring index) ?\n))
568 (insert (eshell/pwd) ?\n)
569 (eshell-with-private-file-modes
570 (write-region (point-min) (point-max) file nil
571 'no-message))))))))
572
573;;; Code:
574
575;;; em-dirs.el ends here