(dired-sort-set-modeline): Use force-mode-line-update.
[bpt/emacs.git] / lisp / dired-x.el
CommitLineData
cb3fe1f0
RS
1;;; dired-x.el --- Sebastian Kremer's Extra DIRED hacked up for GNU Emacs19
2
3;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
4;; Lawrence R. Dodd <dodd@roebling.poly.edu>
5;; Maintainer: Lawrence R. Dodd <dodd@roebling.poly.edu>
9b5ef74b
RS
6;; Version: 2.37+
7;; Date: 1994/08/18 19:27:42
cb3fe1f0
RS
8;; Keywords: dired extensions
9
10;; Copyright (C) 1993, 1994 Free Software Foundation
11
12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is free software; you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
de3b6439 16;; the Free Software Foundation; either version 2, or (at your option)
cb3fe1f0
RS
17;; any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
25;; along with GNU Emacs; see the file COPYING. If not, write to
26;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27
9572a9dd 28;;; Commentary:
cb3fe1f0 29
9572a9dd
KH
30;;; This is Sebastian Kremer's excellent dired-x.el (Dired Extra), version
31;;; 1.191, hacked up for GNU Emacs 19. Redundant or conflicting material
32;;; has been removed or renamed in order to work properly with dired of
33;;; GNU Emacs 19. All suggestions or comments are most welcomed.
cb3fe1f0 34
ccb1d39a
RS
35;;;
36;;; Please, PLEASE, *PLEASE* see the info pages.
37;;;
38
cb3fe1f0
RS
39;;; BUGS: Type M-x dired-x-submit-report and a report will be generated.
40
9572a9dd 41;;; INSTALLATION: In your ~/.emacs,
cb3fe1f0 42;;;
9572a9dd 43;;; (add-hook 'dired-load-hook
cb3fe1f0
RS
44;;; (function (lambda ()
45;;; (load "dired-x")
e2439696 46;;; ;; Set global variables here. For example:
cb3fe1f0 47;;; ;; (setq dired-guess-shell-gnutar "gtar")
e2439696
KH
48;;; )))
49;;; (add-hook 'dired-mode-hook
50;;; (function (lambda ()
51;;; ;; Set buffer-local variables here. For example:
9572a9dd 52;;; ;; (setq dired-omit-files-p t)
cb3fe1f0
RS
53;;; )))
54;;;
9572a9dd
KH
55;;; At load time dired-x.el will install itself, redefine some functions, and
56;;; bind some dired keys. *Please* see the info pages for more details.
cb3fe1f0 57
ccb1d39a
RS
58;;; CAUTION: If you are using a version of GNU Emacs earlier than 19.20 than
59;;; you may have to edit dired.el. The copy of dired.el in GNU Emacs versions
60;;; earlier than 19.20 incorrectly had the call to run-hooks *before* the call
61;;; to provide. In such a case, it is possible that byte-compiling and/or
62;;; loading dired can cause an infinite loop. To prevent this, make sure the
63;;; line of code
64;;;
65;;; (run-hooks 'dired-load-hook)
66;;;
67;;; is the *last* executable line in the file dired.el. That is, make sure it
68;;; comes *after* the line
69;;;
70;;; (provide 'dired)
71;;;
72;;; *Please* see the info pages for more details.
73
cb3fe1f0 74;;; User defined variables:
9572a9dd 75;;;
cb3fe1f0
RS
76;;; dired-bind-vm
77;;; dired-vm-read-only-folders
78;;; dired-bind-jump
79;;; dired-bind-info
80;;; dired-bind-man
ccb1d39a 81;;; dired-x-hands-off-my-keys
cb3fe1f0
RS
82;;; dired-find-subdir
83;;; dired-enable-local-variables
84;;; dired-local-variables-file
85;;; dired-guess-shell-gnutar
86;;; dired-guess-shell-gzip-quiet
87;;; dired-guess-shell-znew-switches
88;;; dired-guess-shell-alist-user
89;;; dired-clean-up-buffers-too
90;;; dired-omit-files-p
91;;; dired-omit-files
92;;; dired-omit-extensions
9572a9dd
KH
93;;;
94;;; To find out more about these variables, load this file, put your cursor at
95;;; the end of any of the variable names, and hit C-h v [RET]. *Please* see
96;;; the info pages for more details.
cb3fe1f0
RS
97
98;;; When loaded this code redefines the following functions of GNU Emacs
9572a9dd 99;;;
cb3fe1f0
RS
100;;; Function Found in this file of GNU Emacs
101;;; -------- -------------------------------
9572a9dd
KH
102;;; dired-clean-up-after-deletion ../lisp/dired.el
103;;; dired-find-buffer-nocreate ../lisp/dired.el
104;;; dired-initial-position ../lisp/dired.el
105;;; dired-up-directory ../lisp/dired.el
106;;;
cb3fe1f0 107;;; dired-add-entry ../lisp/dired-aux.el
9572a9dd
KH
108;;; dired-read-shell-command ../lisp/dired-aux.el
109;;;
cb3fe1f0
RS
110;;; One drawback is that dired-x.el will load dired-aux.el as soon as dired is
111;;; loaded. Thus, the advantage of separating out non-essential dired stuff
112;;; into dired-aux.el and only loading when necessary will be lost. Please
113;;; note also that some of the comments in dired.el and dired-aux.el are
9572a9dd
KH
114;;; Kremer's that referred to the old dired-x.el. This now should be referring
115;;; to this program. (This is also a good reason to call this dired-x.el
cb3fe1f0
RS
116;;; instead of dired-x19.el.)
117
118\f
119;;;; Code:
120
121;;; LOAD.
122
9572a9dd
KH
123;;; This is a no-op if dired-x is being loaded via `dired-load-hook'. It is
124;;; here in case the user has autoloaded dired-x via the dired-jump key binding
125;;; (instead of autoloading to dired as is suggested in the info-pages).
126
d82cfc0c 127(require 'dired)
9572a9dd 128
cb3fe1f0 129;;; We will redefine some functions and also need some macros so we need to
d82cfc0c
RS
130;;; load dired stuff of GNU Emacs.
131
132(require 'dired-aux)
9572a9dd 133
cb3fe1f0
RS
134;;;; User-defined variables.
135
136(defvar dired-bind-vm nil
137 "*t says \"V\" in dired-mode will `dired-vm', otherwise \"V\" is `dired-rmail'.
138Also, RMAIL files contain -*- rmail -*- at the top so \"f\",
139`dired-advertised-find-file', will run rmail.")
140
141(defvar dired-bind-jump t
142 "*t says bind `dired-jump' to C-x C-j, otherwise do not.")
143
144(defvar dired-bind-man t
145 "*t says bind `dired-man' to \"N\" in dired-mode, otherwise do not.")
146
147(defvar dired-bind-info t
148 "*t says bind `dired-info' to \"I\" in dired-mode, otherwise do not.")
149
150(defvar dired-vm-read-only-folders nil
151 "*If t, \\[dired-vm] will visit all folders read-only.
152If neither nil nor t, e.g. the symbol `if-file-read-only', only
153files not writable by you are visited read-only.
154
155Read-only folders only work in VM 5, not in VM 4.")
156
157(defvar dired-omit-files-p nil
158 "*If non-nil, \"uninteresting\" files are not listed (buffer-local).
159Use \\[dired-omit-toggle] to toggle its value.
160Uninteresting files are those whose filenames match regexp `dired-omit-files',
161plus those ending with extensions in `dired-omit-extensions'.")
e2439696 162(make-variable-buffer-local 'dired-omit-files-p)
cb3fe1f0 163
685ff9f8 164(defvar dired-omit-files "^#\\|^\\.$\\|^\\.\\.$"
e2439696 165 "*Filenames matching this regexp will not be displayed.
685ff9f8
RS
166This only has effect when `dired-omit-files-p' is t. See interactive function
167`dired-omit-toggle' \(\\[dired-omit-toggle]\) and variable
168`dired-omit-extensions'. The default is to omit `.', `..', and auto-save
169files.")
cb3fe1f0
RS
170
171(defvar dired-find-subdir nil ; t is pretty near to DWIM...
0a44133e
RS
172 "*If non-nil, Dired always finds a directory in a buffer of its own.
173If nil, Dired finds the directory as a subdirectory in some other buffer
174if it is present as one.
cb3fe1f0
RS
175
176If there are several Dired buffers for a directory, the most recently
177used is chosen.
178
179Dired avoids switching to the current buffer, so that if you have
180a normal and a wildcard buffer for the same directory, C-x d RET will
181toggle between those two.")
182
183(defvar dired-enable-local-variables t
184 "*Control use of local-variables lists in dired.
185The value can be t, nil or something else.
186A value of t means local-variables lists are obeyed;
187nil means they are ignored; anything else means query.
188
189This temporarily overrides the value of `enable-local-variables' when listing
190a directory. See also `dired-local-variables-file'.")
191
192(defvar dired-guess-shell-gnutar nil
193 "*If non-nil, name of GNU tar executable (e.g., \"tar\" or \"gtar\") and `z'
194switch will be used for compressed or gzip'ed tar files. If no GNU tar, set
195to nil: a pipe using `zcat' or `gunzip -c' will be used.")
196
197(defvar dired-guess-shell-gzip-quiet t
198 "*non-nil says pass -q to gzip overriding verbose GZIP environment.")
199
200(defvar dired-guess-shell-znew-switches nil
201 "*If non-nil, then string of switches passed to `znew', example: \"-K\"")
202
203(defvar dired-clean-up-buffers-too t
204 "*t says offer to kill buffers visiting files and dirs deleted in dired.")
205
206;;;; KEY BINDINGS.
207
208(define-key dired-mode-map "\M-o" 'dired-omit-toggle)
209(define-key dired-mode-map "\M-(" 'dired-mark-sexp)
210(define-key dired-mode-map "\M-!" 'dired-smart-shell-command)
211(define-key dired-mode-map "T" 'dired-do-toggle)
212(define-key dired-mode-map "w" 'dired-copy-filename-as-kill)
213(define-key dired-mode-map "\M-g" 'dired-goto-file)
214(define-key dired-mode-map "\M-G" 'dired-goto-subdir)
215(define-key dired-mode-map "F" 'dired-do-find-marked-files)
216(define-key dired-mode-map "Y" 'dired-do-relsymlink)
217(define-key dired-mode-map "%Y" 'dired-do-relsymlink-regexp)
218(define-key dired-mode-map "V" 'dired-do-run-mail)
219
220(if dired-bind-man
221 (define-key dired-mode-map "N" 'dired-man))
222
223(if dired-bind-info
224 (define-key dired-mode-map "I" 'dired-info))
225
9572a9dd 226;;; GLOBAL BINDING.
cb3fe1f0
RS
227(if dired-bind-jump
228 (progn
229 (define-key global-map "\C-x\C-j" 'dired-jump)
230 (define-key global-map "\C-x4\C-j" 'dired-jump-other-window)))
231
232\f
233;;;; Install into appropriate hooks.
234
235(add-hook 'dired-mode-hook 'dired-extra-startup)
236(add-hook 'dired-after-readin-hook 'dired-omit-expunge)
237
238(defun dired-extra-startup ()
239 "Automatically put on dired-mode-hook to get extra dired features:
240\\<dired-mode-map>
241
242 \\[dired-do-run-mail]\t-- run mail on folder (see `dired-bind-vm')
243 \\[dired-info]\t-- run info on file
244 \\[dired-man]\t-- run man on file
245 \\[dired-do-find-marked-files]\t-- visit all marked files simultaneously
246 \\[dired-omit-toggle]\t-- toggle omitting of files
247 \\[dired-do-toggle]\t-- toggle marks
248 \\[dired-mark-sexp]\t-- mark by lisp expression
249 \\[dired-copy-filename-as-kill]\t-- copy the file or subdir names into the kill ring.
250 \t You can feed it to other commands using \\[yank].
251
252For more features, see variables
253
254 dired-bind-vm
255 dired-bind-jump
256 dired-bind-info
257 dired-bind-man
258 dired-vm-read-only-folders
259 dired-omit-files-p
260 dired-omit-files
261 dired-omit-extensions
262 dired-find-subdir
263 dired-enable-local-variables
264 dired-local-variables-file
265 dired-guess-shell-gnutar
266 dired-guess-shell-gzip-quiet
267 dired-guess-shell-znew-switches
268 dired-guess-shell-alist-user
9572a9dd 269 dired-clean-up-buffers-too
cb3fe1f0
RS
270
271See also functions
272
273 dired-flag-extension
274 dired-virtual
275 dired-jump
276 dired-man
277 dired-vm
278 dired-rmail
279 dired-info
280 dired-do-find-marked-files
281"
282 (interactive)
283
284 ;; These must be done in each new dired buffer.
285 (dired-hack-local-variables)
286 (dired-omit-startup))
287
288\f
289;;;; BUFFER CLEANING.
290
291;;; REDEFINE.
292(defun dired-clean-up-after-deletion (fn)
293
294 ;; Clean up after a deleted file or directory FN.
295 ;; Remove expanded subdir of deleted dir, if any.
296 (save-excursion (and (cdr dired-subdir-alist)
297 (dired-goto-subdir fn)
298 (dired-kill-subdir)))
299
300 ;; Offer to kill buffer of deleted file FN.
301 (if dired-clean-up-buffers-too
302 (progn
303 (let ((buf (get-file-buffer fn)))
304 (and buf
305 (funcall (function y-or-n-p)
306 (format "Kill buffer of %s, too? "
307 (file-name-nondirectory fn)))
308 (save-excursion ; you never know where kill-buffer leaves you
309 (kill-buffer buf))))
12c38283 310 (let ((buf-list (dired-buffers-for-dir (expand-file-name fn)))
cb3fe1f0
RS
311 (buf nil))
312 (and buf-list
313 (y-or-n-p (format "Kill dired buffer%s of %s, too? "
314 (dired-plural-s (length buf-list))
315 (file-name-nondirectory fn)))
316 (while buf-list
317 (save-excursion (kill-buffer (car buf-list)))
318 (setq buf-list (cdr buf-list)))))))
9572a9dd 319 ;; Anything else?
cb3fe1f0
RS
320 )
321
322\f
323;;;; EXTENSION MARKING FUNCTIONS.
324
9572a9dd 325;;; Mark files with some extension.
cb3fe1f0
RS
326(defun dired-mark-extension (extension &optional marker-char)
327 "Mark all files with a certain extension for use in later commands.
328A `.' is not automatically prepended to the string entered."
329 ;; EXTENSION may also be a list of extensions instead of a single one.
330 ;; Optional MARKER-CHAR is marker to use.
331 (interactive "sMarking extension: \nP")
332 (or (listp extension)
333 (setq extension (list extension)))
334 (dired-mark-files-regexp
335 (concat ".";; don't match names with nothing but an extension
336 "\\("
337 (mapconcat 'regexp-quote extension "\\|")
338 "\\)$")
339 marker-char))
340
341(defun dired-flag-extension (extension)
342 "In dired, flag all files with a certain extension for deletion.
343A `.' is *not* automatically prepended to the string entered."
344 (interactive "sFlagging extension: ")
345 (dired-mark-extension extension dired-del-marker))
346
347;;; Define some unpopular file extensions. Used for cleaning and omitting.
348
349(defvar dired-patch-unclean-extensions
350 '(".rej" ".orig")
351 "List of extensions of dispensable files created by the `patch' program.")
352
353(defvar dired-tex-unclean-extensions
354 '(".toc" ".log" ".aux");; these are already in completion-ignored-extensions
355 "List of extensions of dispensable files created by TeX.")
356
357(defvar dired-latex-unclean-extensions
358 '(".idx" ".lof" ".lot" ".glo")
359 "List of extensions of dispensable files created by LaTeX.")
360
361(defvar dired-bibtex-unclean-extensions
362 '(".blg" ".bbl")
363 "List of extensions of dispensable files created by BibTeX.")
364
365(defvar dired-texinfo-unclean-extensions
366 '(".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
367 ".tp" ".tps" ".vr" ".vrs")
368 "List of extensions of dispensable files created by texinfo.")
369
370(defun dired-clean-patch ()
371 "Flag dispensable files created by patch for deletion.
372See variable `dired-patch-unclean-extensions'."
373 (interactive)
374 (dired-flag-extension dired-patch-unclean-extensions))
375
376(defun dired-clean-tex ()
9572a9dd
KH
377 "Flag dispensable files created by [La]TeX etc. for deletion.
378See variables `dired-texinfo-unclean-extensions',
379`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
380`dired-texinfo-unclean-extensions'."
cb3fe1f0
RS
381 (interactive)
382 (dired-flag-extension (append dired-texinfo-unclean-extensions
383 dired-latex-unclean-extensions
384 dired-bibtex-unclean-extensions
385 dired-tex-unclean-extensions)))
386
9572a9dd
KH
387(defun dired-very-clean-tex ()
388 "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
389See variables `dired-texinfo-unclean-extensions',
390`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
391`dired-texinfo-unclean-extensions'."
cb3fe1f0 392 (interactive)
9572a9dd
KH
393 (dired-flag-extension (append dired-texinfo-unclean-extensions
394 dired-latex-unclean-extensions
395 dired-bibtex-unclean-extensions
396 dired-tex-unclean-extensions
397 (list ".dvi"))))
cb3fe1f0
RS
398\f
399;;;; JUMP.
400
40c8b203 401;;;###autoload
cb3fe1f0
RS
402(defun dired-jump (&optional other-window)
403 "Jump to dired buffer corresponding to current buffer.
404If in a file, dired the current directory and move to file's line.
405If in dired already, pop up a level and goto old directory's line.
406In case the proper dired file line cannot be found, refresh the dired
407buffer and try again."
408 (interactive "P")
409 (let* ((file buffer-file-name)
410 (dir (if file (file-name-directory file) default-directory)))
411 (if (eq major-mode 'dired-mode)
412 (progn
413 (setq dir (dired-current-directory))
414 (dired-up-directory other-window)
415 (or (dired-goto-file dir)
416 ;; refresh and try again
9572a9dd 417 (progn
cb3fe1f0
RS
418 (dired-insert-subdir (file-name-directory dir))
419 (dired-goto-file dir))))
420 (if other-window
421 (dired-other-window dir)
422 (dired dir))
423 (if file
424 (or (dired-goto-file file)
9572a9dd
KH
425 ;; Toggle omitting, if necessary, and try again.
426 (progn
427 (dired-omit-toggle t)
428 (dired-goto-file file))
cb3fe1f0 429 ;; refresh and try again
9572a9dd 430 (progn
cb3fe1f0
RS
431 (dired-insert-subdir (file-name-directory file))
432 (dired-goto-file file)))))))
433
434(defun dired-jump-other-window ()
435 "Like \\[dired-jump] (dired-jump) but in other window."
436 (interactive)
437 (dired-jump t))
438
9572a9dd
KH
439;;; REDEFINE.
440;;; This replaces the version in dired.el
cb3fe1f0
RS
441;;; It simply adds the OTHER-WINDOW option to the one in dired.el.
442(defun dired-up-directory (&optional other-window)
443 "Run dired on parent directory of current directory.
444Find the parent directory either in this buffer or another buffer.
445Finds in current window or in other window with optional OTHER-WINDOW.
446Creates a buffer if necessary."
447 (interactive "P")
448 (let* ((dir (dired-current-directory))
449 (up (file-name-directory (directory-file-name dir))))
450 (or (dired-goto-file (directory-file-name dir))
451 ;; Only try dired-goto-subdir if buffer has more than one dir.
452 (and (cdr dired-subdir-alist)
453 (dired-goto-subdir up))
454 (progn
455 (if other-window
456 (dired-other-window up)
457 (dired up))
458 (dired-goto-file dir)))))
459
460\f
461;;;; TOGGLE.
462;;; Toggle marked files with unmarked files.
463
464(defun dired-do-toggle ()
465 "Toggle marks.
466That is, currently marked files become unmarked and vice versa.
467Files marked with other flags (such as `D') are not affected.
468`.' and `..' are never toggled.
469As always, hidden subdirs are not affected."
470 (interactive)
471 (save-excursion
472 (goto-char (point-min))
473 (let (buffer-read-only)
474 (while (not (eobp))
475 (or (dired-between-files)
476 (looking-at dired-re-dot)
477 ;; use subst instead of insdel because it does not move
478 ;; the gap and thus should be faster and because
479 ;; other characters are left alone automatically
480 (apply 'subst-char-in-region
481 (point) (1+ (point))
482 (if (eq ?\040 (following-char)) ; SPC
483 (list ?\040 dired-marker-char)
484 (list dired-marker-char ?\040))))
485 (forward-line 1)))))
486
487\f
488;;;; COPY NAMES OF MARKED FILES INTO KILL-RING.
9572a9dd 489
cb3fe1f0
RS
490(defun dired-copy-filename-as-kill (&optional arg)
491 "Copy names of marked (or next ARG) files into the kill ring.
492The names are separated by a space.
493With a zero prefix arg, use the complete pathname of each marked file.
494With \\[universal-argument], use the relative pathname of each marked file.
495
496If on a subdir headerline, use subdirname instead; prefix arg is ignored
497in this case.
498
499You can then feed the file name(s) to other commands with \\[yank]."
500 (interactive "P")
501 (let ((string
502 (or (dired-get-subdir)
503 (mapconcat (function identity)
504 (if arg
505 (cond ((zerop (prefix-numeric-value arg))
506 (dired-get-marked-files))
507 ((integerp arg)
508 (dired-get-marked-files 'no-dir arg))
509 (t ; else a raw arg
510 (dired-get-marked-files t)))
511 (dired-get-marked-files 'no-dir))
512 " "))))
513 (kill-new string)
514 (message "%s" string)))
515
516\f
517;;;; OMITTING.
518
519;;; Enhanced omitting of lines from directory listings.
520;;; Marked files are never omitted.
521
522;; should probably get rid of this and always use 'no-dir.
523;; sk 28-Aug-1991 09:37
524(defvar dired-omit-localp 'no-dir
525 "The LOCALP argument dired-omit-expunge passes to dired-get-filename.
526If it is 'no-dir, omitting is much faster, but you can only match
527against the basename of the file. Set it to nil if you need to match the
528whole pathname.")
529
530;; \017=^O for Omit - other packages can chose other control characters.
531(defvar dired-omit-marker-char ?\017
532 "Temporary marker used by by dired-omit.
533Should never be used as marker by the user or other packages.")
534
535(defun dired-omit-startup ()
cb3fe1f0
RS
536 (or (assq 'dired-omit-files-p minor-mode-alist)
537 (setq minor-mode-alist
538 (append '((dired-omit-files-p " Omit")) minor-mode-alist))))
539
540(defun dired-omit-toggle (&optional flag)
685ff9f8 541 "Toggle omitting files matching `dired-omit-files' and `dired-omit-extensions'.
cb3fe1f0
RS
542With an arg, and if omitting was off, don't toggle and just mark the
543 files but don't actually omit them.
544With an arg, and if omitting was on, turn it off but don't refresh the buffer."
545 (interactive "P")
546 (if flag
547 (if dired-omit-files-p
548 (setq dired-omit-files-p (not dired-omit-files-p))
549 (dired-mark-unmarked-files (dired-omit-regexp) nil nil
550 dired-omit-localp))
551 ;; no FLAG
552 (setq dired-omit-files-p (not dired-omit-files-p))
553 (if (not dired-omit-files-p)
554 (revert-buffer)
555 ;; this will mention how many were omitted:
556 (dired-omit-expunge))))
557
558(defvar dired-omit-extensions
559 (append completion-ignored-extensions
560 dired-latex-unclean-extensions
561 dired-bibtex-unclean-extensions
562 dired-texinfo-unclean-extensions)
685ff9f8
RS
563 "If non-nil, a list of extensions \(strings\) to omit from Dired listings.
564Defaults to elements of `completion-ignored-extensions',
565`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
566`dired-texinfo-unclean-extensions'.
567
568See interactive function `dired-omit-toggle' \(\\[dired-omit-toggle]\) and
569variables `dired-omit-files-p' and `dired-omit-files'.")
cb3fe1f0
RS
570
571(defun dired-omit-expunge (&optional regexp)
572 "Erases all unmarked files matching REGEXP.
573Does nothing if global variable `dired-omit-files-p' is nil.
574If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
575 filenames ending in `dired-omit-extensions'.
576If REGEXP is the empty string, this function is a no-op.
577
578This functions works by temporarily binding `dired-marker-char' to
579`dired-omit-marker-char' and calling `dired-do-kill-lines'."
580 (interactive "sOmit files (regexp): ")
581 (if dired-omit-files-p
582 (let ((omit-re (or regexp (dired-omit-regexp)))
9b5ef74b 583 (old-modified-p (buffer-modified-p))
cb3fe1f0
RS
584 count)
585 (or (string= omit-re "")
586 (let ((dired-marker-char dired-omit-marker-char))
587 (message "Omitting...")
588 (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp)
589 (progn
590 (setq count (dired-do-kill-lines nil "Omitted %d line%s."))
591 ;; Force an update of modeline.
592 (set-buffer-modified-p (buffer-modified-p)))
593 (message "(Nothing to omit)"))))
9b5ef74b
RS
594 ;; Try to preserve modified state of buffer. So `%*' doesn't appear
595 ;; in mode-line of omitted buffers.
596 (set-buffer-modified-p (and old-modified-p
597 (save-excursion
598 (goto-char (point-min))
599 (re-search-forward dired-re-mark nil t))))
cb3fe1f0
RS
600 count)))
601
602(defun dired-omit-regexp ()
603 (concat (if dired-omit-files (concat "\\(" dired-omit-files "\\)") "")
604 (if (and dired-omit-files dired-omit-extensions) "\\|" "")
605 (if dired-omit-extensions
606 (concat ".";; a non-extension part should exist
607 "\\("
608 (mapconcat 'regexp-quote dired-omit-extensions "\\|")
609 "\\)$")
610 "")))
611
612;; Returns t if any work was done, nil otherwise.
613(defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp)
614 "Marks unmarked files matching REGEXP, displaying MSG.
615REGEXP is matched against the complete pathname.
616Does not re-mark files which already have a mark.
617With prefix argument, unflag all those files.
618Second optional argument LOCALP is as in `dired-get-filename'."
619 (interactive "P")
620 (let ((dired-marker-char (if unflag-p ?\ dired-marker-char)))
621 (dired-mark-if
622 (and
623 ;; not already marked
624 (looking-at " ")
625 ;; uninteresting
626 (let ((fn (dired-get-filename localp t)))
627 (and fn (string-match regexp fn))))
628 msg)))
629
9572a9dd 630;;; REDEFINE.
cb3fe1f0
RS
631(defun dired-omit-new-add-entry (filename &optional marker-char)
632 ;; This redefines dired-aux.el's dired-add-entry to avoid calling ls for
633 ;; files that are going to be omitted anyway.
634 (if dired-omit-files-p
635 ;; perhaps return t without calling ls
636 (let ((omit-re (dired-omit-regexp)))
637 (if (or (string= omit-re "")
638 (not
639 (string-match omit-re
640 (cond
641 ((eq 'no-dir dired-omit-localp)
642 filename)
643 ((eq t dired-omit-localp)
644 (dired-make-relative filename))
645 (t
646 (dired-make-absolute
647 filename
648 (file-name-directory filename)))))))
649 ;; if it didn't match, go ahead and add the entry
650 (dired-omit-old-add-entry filename marker-char)
651 ;; dired-add-entry returns t for success, perhaps we should
652 ;; return file-exists-p
653 t))
654 ;; omitting is not turned on at all
655 (dired-omit-old-add-entry filename marker-char)))
656
9572a9dd 657;;; REDEFINE.
cb3fe1f0
RS
658;;; Redefine dired-aux.el's version of `dired-add-entry'
659;;; Save old defun if not already done:
660(or (fboundp 'dired-omit-old-add-entry)
661 (fset 'dired-omit-old-add-entry (symbol-function 'dired-add-entry)))
662;; Redefine it.
663(fset 'dired-add-entry 'dired-omit-new-add-entry)
664
665\f
666;;;; VIRTUAL DIRED MODE.
667
668;;; For browsing `ls -lR' listings in a dired-like fashion.
669
670(fset 'virtual-dired 'dired-virtual)
671(defun dired-virtual (dirname &optional switches)
672 "Put this buffer into Virtual Dired mode.
673
674In Virtual Dired mode, all commands that do not actually consult the
675filesystem will work.
676
677This is useful if you want to peruse and move around in an ls -lR
678output file, for example one you got from an ftp server. With
679ange-ftp, you can even dired a directory containing an ls-lR file,
680visit that file and turn on virtual dired mode. But don't try to save
681this file, as dired-virtual indents the listing and thus changes the
682buffer.
683
684If you have save a Dired buffer in a file you can use \\[dired-virtual] to
685resume it in a later session.
686
687Type \\<dired-mode-map>\\[revert-buffer] in the
688Virtual Dired buffer and answer `y' to convert the virtual to a real
689dired buffer again. You don't have to do this, though: you can relist
690single subdirs using \\[dired-do-redisplay].
691"
692
693 ;; DIRNAME is the top level directory of the buffer. It will become
694 ;; its `default-directory'. If nil, the old value of
695 ;; default-directory is used.
696
697 ;; Optional SWITCHES are the ls switches to use.
698
699 ;; Shell wildcards will be used if there already is a `wildcard'
700 ;; line in the buffer (thus it is a saved Dired buffer), but there
701 ;; is no other way to get wildcards. Insert a `wildcard' line by
702 ;; hand if you want them.
703
704 (interactive
705 (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
706 (goto-char (point-min))
707 (or (looking-at " ")
708 ;; if not already indented, do it now:
709 (indent-region (point-min) (point-max) 2))
710 (or dirname (setq dirname default-directory))
711 (setq dirname (expand-file-name (file-name-as-directory dirname)))
712 (setq default-directory dirname) ; contains no wildcards
713 (let ((wildcard (save-excursion
714 (goto-char (point-min))
715 (forward-line 1)
716 (and (looking-at "^ wildcard ")
717 (buffer-substring (match-end 0)
718 (progn (end-of-line) (point)))))))
719 (if wildcard
720 (setq dirname (expand-file-name wildcard default-directory))))
721 ;; If raw ls listing (not a saved old dired buffer), give it a
722 ;; decent subdir headerline:
723 (goto-char (point-min))
724 (or (looking-at dired-subdir-regexp)
725 (dired-insert-headerline default-directory))
726 (dired-mode dirname (or switches dired-listing-switches))
727 (setq mode-name "Virtual Dired"
728 revert-buffer-function 'dired-virtual-revert)
729 (set (make-local-variable 'dired-subdir-alist) nil)
730 (dired-build-subdir-alist)
731 (goto-char (point-min))
732 (dired-initial-position dirname))
733
734(defun dired-virtual-guess-dir ()
735
736 ;; Guess and return appropriate working directory of this buffer,
737 ;; assumed to be in Dired or ls -lR format.
738 ;; The guess is based upon buffer contents.
739 ;; If nothing could be guessed, returns nil.
740
741 (let ((regexp "^\\( \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
742 (subexpr 2))
743 (goto-char (point-min))
744 (cond ((looking-at regexp)
745 ;; If a saved dired buffer, look to which dir and
746 ;; perhaps wildcard it belongs:
747 (let ((dir (buffer-substring (match-beginning subexpr)
748 (match-end subexpr))))
749 (file-name-as-directory dir)))
750 ;; Else no match for headerline found. It's a raw ls listing.
751 ;; In raw ls listings the directory does not have a headerline
752 ;; try parent of first subdir, if any
753 ((re-search-forward regexp nil t)
754 (file-name-directory
755 (directory-file-name
756 (file-name-as-directory
757 (buffer-substring (match-beginning subexpr)
758 (match-end subexpr))))))
759 (t ; if all else fails
760 nil))))
761
762
763(defun dired-virtual-revert (&optional arg noconfirm)
764 (if (not
765 (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
766 (error "Cannot revert a Virtual Dired buffer.")
767 (setq mode-name "Dired"
768 revert-buffer-function 'dired-revert)
769 (revert-buffer)))
770
771;; A zero-arg version of dired-virtual.
772;; You need my modified version of set-auto-mode for the
773;; `buffer-contents-mode-alist'.
774;; Or you use infer-mode.el and infer-mode-alist, same syntax.
775(defun dired-virtual-mode ()
776 "Put current buffer into virtual dired mode (see `dired-virtual').
777Useful on `buffer-contents-mode-alist' (which see) with the regexp
778
779 \"^ \\(/[^ /]+\\)/?+:$\"
780
781to put saved dired buffers automatically into virtual dired mode.
782
783Also useful for `auto-mode-alist' (which see) like this:
784
e7070c01 785 \(setq auto-mode-alist (cons '(\"[^/]\\.dired\\'\" . dired-virtual-mode)
cb3fe1f0
RS
786 auto-mode-alist)\)"
787 (interactive)
788 (dired-virtual (dired-virtual-guess-dir)))
789
790\f
791;;;; SMART SHELL.
792
793;;; An Emacs buffer can have but one working directory, stored in the
794;;; buffer-local variable `default-directory'. A Dired buffer may have
795;;; several subdirectories inserted, but still has but one working directory:
796;;; that of the top level Dired directory in that buffer. For some commands
797;;; it is appropriate that they use the current Dired directory instead of
798;;; `default-directory', e.g., `find-file' and `compile'. This is a general
799;;; mechanism is provided for special handling of the working directory in
800;;; special major modes.
801
802;; It's easier to add to this alist than redefine function
803;; default-directory while keeping the old information.
804(defconst default-directory-alist
805 '((dired-mode . (if (fboundp 'dired-current-directory)
806 (dired-current-directory)
807 default-directory)))
808 "Alist of major modes and their opinion on default-directory, as a
809lisp expression to evaluate. A resulting value of nil is ignored in
810favor of default-directory.")
811
812(defun default-directory ()
813 "Usage like variable `default-directory', but knows about the special
814cases in variable `default-directory-alist' (which see)."
815 (or (eval (cdr (assq major-mode default-directory-alist)))
816 default-directory))
817
818(defun dired-smart-shell-command (cmd &optional insert)
819 "Like function `shell-command', but in the current Tree Dired directory."
820 (interactive "sShell command: \nP")
821 (let ((default-directory (default-directory)))
822 (shell-command cmd insert)))
823
824\f
825;;;; LOCAL VARIABLES FOR DIRED BUFFERS.
826
9572a9dd
KH
827;;; Brief Description:
828;;;
829;;; * `dired-extra-startup' is part of the `dired-mode-hook'.
830;;;
cb3fe1f0 831;;; * `dired-extra-startup' calls `dired-hack-local-variables'
9572a9dd 832;;;
cb3fe1f0 833;;; * `dired-hack-local-variables' checks the value of
9572a9dd
KH
834;;; `dired-local-variables-file'
835;;;
cb3fe1f0
RS
836;;; * Check if `dired-local-variables-file' is a non-nil string and is a
837;;; filename found in the directory of the Dired Buffer being created.
9572a9dd 838;;;
cb3fe1f0
RS
839;;; * If `dired-local-variables-file' satisfies the above, then temporarily
840;;; include it in the Dired Buffer at the bottom.
9572a9dd 841;;;
cb3fe1f0
RS
842;;; * Set `enable-local-variables' temporarily to the user variable
843;;; `dired-enable-local-variables' and run `hack-local-variables' on the
844;;; Dired Buffer.
845
846(defvar dired-local-variables-file ".dired"
847 "Filename, as string, containing local dired buffer variables to be hacked.
848If this file found in current directory, then it will be inserted into dired
849buffer and `hack-local-variables' will be run. See Emacs Info pages for more
850information on local variables. See also `dired-enable-local-variables'.")
851
852(defun dired-hack-local-variables ()
853 "Evaluate local variables in `dired-local-variables-file' for dired buffer."
854 (if (and dired-local-variables-file
855 (stringp dired-local-variables-file)
856 (file-exists-p dired-local-variables-file))
857 (let ((opoint (point-max))
858 buffer-read-only
859 ;; In case user has `enable-local-variables' set to nil we
860 ;; override it locally with dired's variable.
861 (enable-local-variables dired-enable-local-variables))
862 ;; Insert 'em.
863 (save-excursion
864 (goto-char opoint)
865 (insert "\^L\n")
866 (insert-file-contents dired-local-variables-file))
867 ;; Hack 'em.
868 (let ((buffer-file-name dired-local-variables-file))
869 (hack-local-variables))
870 ;; Make sure that the modeline shows the proper information.
871 (dired-sort-set-modeline)
872 ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
9572a9dd 873 (delete-region opoint (point-max)))))
cb3fe1f0 874
9572a9dd 875(defun dired-omit-here-always ()
cb3fe1f0
RS
876 "Creates `dired-local-variables-file' for omitting and reverts directory.
877Sets dired-omit-file-p to t in a local variables file that is readable by
878dired."
9572a9dd 879 (interactive)
cb3fe1f0
RS
880 (if (file-exists-p dired-local-variables-file)
881 (message "File `./%s' already exists." dired-local-variables-file)
882
883 ;; Create `dired-local-variables-file'.
884 (save-excursion
885 (set-buffer (get-buffer-create " *dot-dired*"))
886 (erase-buffer)
887 (insert "Local Variables:\ndired-omit-files-p: t\nEnd:\n")
888 (write-file dired-local-variables-file)
889 (kill-buffer (current-buffer)))
890
891 ;; Run extra-hooks and revert directory.
892 (dired-extra-startup)
893 (dired-revert)))
894
895\f
896;;;; GUESS SHELL COMMAND.
897
9572a9dd
KH
898;;; Brief Description:
899;;;
900;;; `dired-do-shell-command' is bound to `!' by dired.el.
901;;;
cb3fe1f0
RS
902;;; * Redefine `dired-do-shell-command' so it calls
903;;; `dired-guess-shell-command'.
9572a9dd 904;;;
cb3fe1f0
RS
905;;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
906;;; marked files.
9572a9dd 907;;;
cb3fe1f0
RS
908;;; * Parse `dired-guess-shell-alist-user' and
909;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
910;;; that matches the first file in the file list.
9572a9dd 911;;;
cb3fe1f0 912;;; * If the REGEXP matches all the entries of the file list then evaluate
c0d79871 913;;; COMMAND, which is either a string or a Lisp expression returning a
cb3fe1f0 914;;; string. COMMAND may be a list of commands.
9572a9dd 915;;;
cb3fe1f0
RS
916;;; * Return this command to `dired-guess-shell-command' which prompts user
917;;; with it. The list of commands are temporaily put into the history list.
918;;; If a command is used successfully then it is stored permanently in
919;;; `dired-shell-command-history'.
920
921;;; Guess what shell command to apply to a file.
922(defvar dired-shell-command-history nil
923 "History list for commands that read dired-shell commands.")
924
9572a9dd 925;;; Default list of shell commands.
cb3fe1f0
RS
926
927;;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
928;;; install GNU zip's version of zcat.
929
930(defvar dired-guess-shell-alist-default
931 (list
932 (list "\\.tar$" '(if dired-guess-shell-gnutar
933 (concat dired-guess-shell-gnutar " xvf")
934 "tar xvf"))
935
936 ;; REGEXPS for compressed archives must come before the .Z rule to
937 ;; be recognized:
938 (list "\\.tar\\.Z$"
939 ;; Untar it.
940 '(if dired-guess-shell-gnutar
941 (concat dired-guess-shell-gnutar " zxvf")
942 (concat "zcat * | tar xvf -"))
943 ;; Optional conversion to gzip format.
944 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
945 " " dired-guess-shell-znew-switches))
946
947 ;; gzip'ed archives
948 (list "\\.tar\\.g?z$"
949 '(if dired-guess-shell-gnutar
950 (concat dired-guess-shell-gnutar " zxvf")
951 (concat "gunzip -qc * | tar xvf -"))
952 ;; Optional decompression.
953 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" "")))
954
955 '("\\.shar.Z$" "zcat * | unshar")
956 '("\\.shar.g?z$" "gunzip -qc * | unshar")
957
958 '("\\.ps$" "ghostview" "xv" "lpr")
959 (list "\\.ps.g?z$" "gunzip -qc * | ghostview -"
960 ;; Optional decompression.
961 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
9572a9dd 962 (list "\\.ps.Z$" "zcat * | ghostview -"
cb3fe1f0
RS
963 ;; Optional conversion to gzip format.
964 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
965 " " dired-guess-shell-znew-switches))
966 '("\\.patch$" "cat * | patch")
967 '("\\.patch.g?z$" "gunzip -qc * | patch")
968 (list "\\.patch.Z$" "zcat * | patch"
969 ;; Optional conversion to gzip format.
970 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
971 " " dired-guess-shell-znew-switches))
972
973 '("\\.dvi$" "xdvi" "dvips") ; preview and printing
9572a9dd 974 '("\\.au$" "play") ; play Sun audiofiles
cb3fe1f0
RS
975 '("\\.mpg$" "mpeg_play")
976 '("\\.uu$" "uudecode") ; for uudecoded files
977 '("\\.hqx$" "mcvert")
978 '("\\.sh$" "sh") ; execute shell scripts
979 '("\\.xbm$" "bitmap") ; view X11 bitmaps
980 '("\\.gp$" "gnuplot")
981 '("\\.p[bgpn]m$" "xv")
982 '("\\.gif$" "xv") ; view gif pictures
983 '("\\.tif$" "xv")
984 '("\\.jpg$" "xv")
985 '("\\.fig$" "xfig") ; edit fig pictures
986 '("\\.out$" "xgraph") ; for plotting purposes.
987 '("\\.tex$" "latex" "tex")
988 '("\\.texi\\(nfo\\)?$" "makeinfo" "texi2dvi")
989
990 ;; Some other popular archivers.
991 '("\\.zoo$" "zoo x//")
992 '("\\.zip$" "unzip")
993 '("\\.lzh$" "lharc x")
994 '("\\.arc$" "arc x")
995 '("\\.shar$" "unshar")
996
997 ;; Compression.
998 (list "\\.g?z$" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
999 (list "\\.Z$" "uncompress"
1000 ;; Optional conversion to gzip format.
1001 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1002 " " dired-guess-shell-znew-switches))
1003 )
9572a9dd 1004
cb3fe1f0
RS
1005 "Default alist used for shell command guessing.
1006See `dired-guess-shell-alist-user'")
1007
1008(defvar dired-guess-shell-alist-user nil
1009 "User-defined alist of rules for suggested commands. These rules take
1010precedence over the predefined rules in the variable
1011`dired-guess-shell-alist-default' (to which they are prepended).
1012
1013Each element of this list looks like
1014
1015 \(REGEXP COMMAND...\)
1016
1017where each COMMAND can either be a string or a lisp expression that evaluates
1018to a string. If several COMMANDs are given, the first one will be the default
1019and the rest will be added temporarily to the history and can be retrieved
1020with \\[previous-history-element] (M-p) .
1021
1022You can set this variable in your ~/.emacs. For example, to add rules for
1023`.foo' and `.bar' files, write
1024
1025 \(setq dired-guess-shell-alist-user
1026 (list (list \"\\\\.foo$\" \"FOO-COMMAND\");; fixed rule
1027 ;; possibly more rules ...
1028 (list \"\\\\.bar$\";; rule with condition test
1029 '(if condition
1030 \"BAR-COMMAND-1\"
1031 \"BAR-COMMAND-2\")))\)
1032")
1033
1034(defun dired-guess-default (files)
1035
9572a9dd
KH
1036 ;; Guess a shell commands for FILES. Return command or list of commands.
1037 ;; See `dired-guess-shell-alist-user'.
cb3fe1f0
RS
1038
1039 (let* ((case-fold-search nil) ; case-sensitive matching
1040 ;; Prepend the user's alist to the default alist.
1041 (alist (append dired-guess-shell-alist-user
1042 dired-guess-shell-alist-default))
1043 (file (car files))
1044 (flist (cdr files))
1045 elt regexp cmds)
1046
1047 ;; Find the first match in the alist for first file in FILES.
1048 (while alist
1049 (setq elt (car alist)
1050 regexp (car elt)
1051 alist (cdr alist))
1052 (if (string-match regexp file)
1053 (setq cmds (cdr elt)
1054 alist nil)))
1055
1056 ;; If more than one file, see if all of FILES match regular expression.
1057 (while (and flist
1058 (string-match regexp (car flist)))
1059 (setq flist (cdr flist)))
9572a9dd 1060
cb3fe1f0
RS
1061 ;; If flist is still non-nil, then do not guess since this means that not
1062 ;; all the files in FILES were matched by the regexp.
1063 (setq cmds (and (not flist) cmds))
1064
9572a9dd
KH
1065 ;; Return commands or nil if flist is still non-nil.
1066 ;; Evaluate the commands in order that any logical testing will be done.
cb3fe1f0
RS
1067 (cond ((not (cdr cmds))
1068 (eval (car cmds))) ; single command
1069 (t
1070 (mapcar (function eval) cmds)))))
1071
1072(defun dired-guess-shell-command (prompt files)
1073
1074 ;; Ask user with PROMPT for a shell command, guessing a default from FILES.
1075
1076 (let ((default (dired-guess-default files))
1077 default-list old-history val (failed t))
1078
1079 (if (null default)
1080 ;; Nothing to guess
1081 (read-from-minibuffer prompt nil nil nil 'dired-shell-command-history)
1082
1083 ;; Save current history list
1084 (setq old-history dired-shell-command-history)
1085
1086 (if (listp default)
1087
1088 ;; More than one guess
1089 (setq default-list default
1090 default (car default)
1091 prompt (concat
1092 prompt
1093 (format "{%d guesses} " (length default-list))))
1094
1095 ;; Just one guess
1096 (setq default-list (list default)))
1097
1098 ;; Push all guesses onto history so that they can be retrieved with M-p
1099 ;; and put the first guess in the prompt but not in the initial value.
1100 (setq dired-shell-command-history
1101 (append default-list dired-shell-command-history)
1102 prompt (concat prompt (format "[%s] " default)))
1103
1104 ;; The unwind-protect returns VAL, and we too.
1105 (unwind-protect
1106 ;; BODYFORM
1107 (progn
1108 (setq val (read-from-minibuffer prompt nil nil nil
1109 'dired-shell-command-history)
1110 failed nil)
1111 ;; If we got a return, then use default.
1112 (if (equal val "")
1113 (setq val default))
1114 val)
1115
1116 ;; UNWINDFORMS
1117 ;; Undo pushing onto the history list so that an aborted
1118 ;; command doesn't get the default in the next command.
1119 (setq dired-shell-command-history old-history)
1120 (if (not failed)
1121 (or (equal val (car-safe dired-shell-command-history))
1122 (setq dired-shell-command-history
1123 (cons val dired-shell-command-history))))))))
1124
1125
9572a9dd 1126;;; REDEFINE.
cb3fe1f0
RS
1127;;; Redefine dired-aux.el's version:
1128(defun dired-read-shell-command (prompt arg files)
1129;; "Read a dired shell command prompting with PROMPT (using read-string).
1130;;ARG is the prefix arg and may be used to indicate in the prompt which
1131;; files are affected.
1132;;This is an extra function so that you can redefine it, e.g., to use gmhist."
1133 (dired-mark-pop-up
1134 nil 'shell files
1135 'dired-guess-shell-command
1136 (format prompt (dired-mark-prompt arg files)) ; PROMPT
1137 files)) ; FILES
1138
1139\f
1140;;;; RELATIVE SYMBOLIC LINKS.
1141
1142(defvar dired-keep-marker-relsymlink ?S
1143 "See variable `dired-keep-marker-move'.")
1144
1145(defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
1146 "Three arguments: FILE1 FILE2 &optional OK-IF-ALREADY-EXISTS
1147Make a symbolic link (pointing to FILE1) in FILE2.
1148The link is relative (if possible), for example
1149
1150 \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
1151
1152results in
1153
1154 \"../../tex/bin/foo\" \"/vol/local/bin/foo\"
1155"
1156 (interactive "FRelSymLink: \nFRelSymLink %s: \np")
1157 (let (name1 name2 len1 len2 (index 0) sub)
1158 (setq file1 (expand-file-name file1)
1159 file2 (expand-file-name file2)
1160 len1 (length file1)
1161 len2 (length file2))
1162 ;; Find common initial pathname components:
1163 (let (next)
1164 (while (and (setq next (string-match "/" file1 index))
1165 (setq next (1+ next))
1166 (< next (min len1 len2))
1167 ;; For the comparison, both substrings must end in
1168 ;; `/', so NEXT is *one plus* the result of the
1169 ;; string-match.
1170 ;; E.g., consider the case of linking "/tmp/a/abc"
1171 ;; to "/tmp/abc" erronously giving "/tmp/a" instead
1172 ;; of "/tmp/" as common initial component
1173 (string-equal (substring file1 0 next)
1174 (substring file2 0 next)))
1175 (setq index next))
1176 (setq name2 file2
1177 sub (substring file1 0 index)
1178 name1 (substring file1 index)))
1179 (if (string-equal sub "/")
1180 ;; No common initial pathname found
1181 (setq name1 file1)
1182 ;; Else they have a common parent directory
1183 (let ((tem (substring file2 index))
1184 (start 0)
1185 (count 0))
1186 ;; Count number of slashes we must compensate for ...
1187 (while (setq start (string-match "/" tem start))
1188 (setq count (1+ count)
1189 start (1+ start)))
1190 ;; ... and prepend a "../" for each slash found:
1191 (while (> count 0)
1192 (setq count (1- count)
1193 name1 (concat "../" name1)))))
1194 (make-symbolic-link
1195 (directory-file-name name1) ; must not link to foo/
1196 ; (trailing slash!)
1197 name2 ok-if-already-exists)))
1198
1199(defun dired-do-relsymlink (&optional arg)
1200 "Relative symlink all marked (or next ARG) files into a directory,
1201or make a relative symbolic link to the current file.
1202This creates relative symbolic links like
1203
1204 foo -> ../bar/foo
1205
1206not absolute ones like
1207
1208 foo -> /ugly/path/that/may/change/any/day/bar/foo"
1209 (interactive "P")
1210 (dired-do-create-files 'relsymlink (function dired-make-relative-symlink)
1211 "RelSymLink" arg dired-keep-marker-relsymlink))
1212
1213(defun dired-do-relsymlink-regexp (regexp newname &optional whole-path)
1214 "RelSymlink all marked files containing REGEXP to NEWNAME.
1215See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
1216for more info."
1217 (interactive (dired-mark-read-regexp "RelSymLink"))
1218 (dired-do-create-files-regexp
1219 (function dired-make-relative-symlink)
1220 "RelSymLink" nil regexp newname whole-path dired-keep-marker-relsymlink))
1221
1222\f
1223;;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
1224
9572a9dd
KH
1225;;; Brief Description:
1226;;;
1227;;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
1228;;;
cb3fe1f0 1229;;; * Use `dired-get-marked-files' to collect the marked files in the current
9572a9dd
KH
1230;;; Dired Buffer into a list of filenames `FILE-LIST'.
1231;;;
cb3fe1f0
RS
1232;;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
1233;;; `dired-do-find-marked-files''s prefix argument NOSELECT.
9572a9dd 1234;;;
cb3fe1f0
RS
1235;;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
1236;;; list each time.
9572a9dd 1237;;;
cb3fe1f0
RS
1238;;; * If NOSELECT is non-nil then just run `find-file-noselect' on each
1239;;; element of FILE-LIST.
9572a9dd 1240;;;
cb3fe1f0
RS
1241;;; * If NOSELECT is nil then calculate the `size' of the window for each file
1242;;; by dividing the `window-height' by length of FILE-LIST. Thus, `size' is
1243;;; cognizant of the window-configuration.
9572a9dd 1244;;;
cb3fe1f0
RS
1245;;; * If `size' is too small abort, otherwise run `find-file' on each element
1246;;; of FILE-LIST giving each a window of height `size'.
1247
1248(defun dired-do-find-marked-files (&optional noselect)
1249 "Find all marked files displaying all of them simultaneously.
1250With optional NOSELECT just find files but do not select them.
1251
1252The current window is split across all files marked, as evenly as possible.
1253Remaining lines go to bottom-most window. The number of files that can be
1254displayed this way is restricted by the height of the current window and
1255`window-min-height'.
1256
9572a9dd 1257To keep dired buffer displayed, type \\[split-window-vertically] first.
cb3fe1f0
RS
1258To display just marked files, type \\[delete-other-windows] first."
1259
1260 (interactive "P")
1261 (dired-simultaneous-find-file (dired-get-marked-files) noselect))
1262
1263(defun dired-simultaneous-find-file (file-list noselect)
1264
1265 ;; Visit all files in FILE-LIST and display them simultaneously. The
1266 ;; current window is split across all files in FILE-LIST, as evenly as
1267 ;; possible. Remaining lines go to the bottom-most window. The number of
1268 ;; files that can be displayed this way is restricted by the height of the
1269 ;; current window and the variable `window-min-height'. With non-nil
1270 ;; NOSELECT the files are merely found but not selected.
9572a9dd 1271
cb3fe1f0
RS
1272 ;; We don't make this function interactive because it is usually too clumsy
1273 ;; to specify FILE-LIST interactively unless via dired.
1274
1275 (let (size)
1276
1277 (if noselect
1278 ;; Do not select the buffer.
1279 (find-file-noselect (car file-list))
1280
1281 ;; We will have to select the buffer. Calculate and check window size.
1282 (setq size (/ (window-height) (length file-list)))
1283 (or (<= window-min-height size)
1284 (error "Too many files to visit simultaneously. Try C-u prefix."))
1285 (find-file (car file-list)))
1286
1287 ;; Decrement.
1288 (setq file-list (cdr file-list))
1289
1290 (while file-list
1291
1292 (if noselect
1293 ;; Do not select the buffer.
1294 (find-file-noselect (car file-list))
1295
1296 ;; Vertically split off a window of desired size. Upper window will
1297 ;; have SIZE lines. Select lower (larger) window. We split it again.
1298 (select-window (split-window nil size))
1299 (find-file (car file-list)))
1300
1301 ;; Decrement.
1302 (setq file-list (cdr file-list)))))
1303
1304\f
1305;;;; MISCELLANEOUS COMMANDS.
1306
9572a9dd
KH
1307;;; Run man on files.
1308
cb3fe1f0 1309(defun dired-man ()
9572a9dd 1310 "Run man on this file. Display old buffer if buffer name matches filename.
685ff9f8 1311Uses ../lisp/man.el of \\[manual-entry] fame."
cb3fe1f0 1312 (interactive)
685ff9f8
RS
1313 (require 'man)
1314 (let ((file (dired-get-filename))
9b5ef74b 1315 (manual-program "nroff -man -h"))
685ff9f8 1316 (Man-getpage-in-background file)))
cb3fe1f0
RS
1317
1318;;; Run Info on files.
1319
1320(defun dired-info ()
1321 "Run info on this file."
1322 (interactive)
1323 (info (dired-get-filename)))
1324
1325;;; Run mail on mail folders.
1326
9572a9dd
KH
1327;;; (and (not (fboundp 'vm-visit-folder))
1328;;; (defun vm-visit-folder (file &optional arg)
1329;;; nil))
1330
cb3fe1f0
RS
1331(defun dired-vm (&optional read-only)
1332 "Run VM on this file.
1333With prefix arg, visit folder read-only (this requires at least VM 5).
1334See also variable `dired-vm-read-only-folders'."
1335 (interactive "P")
1336 (let ((dir (dired-current-directory))
1337 (fil (dired-get-filename)))
1338 ;; take care to supply 2nd arg only if requested - may still run VM 4!
1339 (cond (read-only (vm-visit-folder fil t))
1340 ((eq t dired-vm-read-only-folders) (vm-visit-folder fil t))
1341 ((null dired-vm-read-only-folders) (vm-visit-folder fil))
1342 (t (vm-visit-folder fil (not (file-writable-p fil)))))
1343 ;; so that pressing `v' inside VM does prompt within current directory:
1344 (set (make-local-variable 'vm-folder-directory) dir)))
1345
1346(defun dired-rmail ()
1347 "Run RMAIL on this file."
1348 (interactive)
1349 (rmail (dired-get-filename)))
1350
1351(defun dired-do-run-mail ()
1352 "If `dired-bind-vm' is t, then function `dired-vm', otherwise `dired-rmail'."
1353 (interactive)
1354 (if dired-bind-vm
1355 ;; Read mail folder using vm.
1356 (dired-vm)
1357 ;; Read mail folder using rmail.
1358 (dired-rmail)))
1359
1360\f
1361;;;; MISCELLANEOUS INTERNAL FUNCTIONS.
1362
1363(or (fboundp 'dired-old-find-buffer-nocreate)
1364 (fset 'dired-old-find-buffer-nocreate
1365 (symbol-function 'dired-find-buffer-nocreate)))
1366
9572a9dd 1367;;; REDEFINE.
cb3fe1f0
RS
1368;;; Redefines dired.el's version of `dired-find-buffer-nocreate'
1369(defun dired-find-buffer-nocreate (dirname)
1370 (if dired-find-subdir
1371 (let* ((cur-buf (current-buffer))
12c38283
RS
1372 (buffers (nreverse
1373 (dired-buffers-for-dir (expand-file-name dirname))))
cb3fe1f0
RS
1374 (cur-buf-matches (and (memq cur-buf buffers)
1375 ;; wildcards must match, too:
1376 (equal dired-directory dirname))))
1377 ;; We don't want to switch to the same buffer---
1378 (setq buffers (delq cur-buf buffers));;need setq with delq
1379 (or (car (sort buffers (function dired-buffer-more-recently-used-p)))
1380 ;; ---unless it's the only possibility:
1381 (and cur-buf-matches cur-buf)))
1382 (dired-old-find-buffer-nocreate dirname)))
1383
1384;; This should be a builtin
1385(defun dired-buffer-more-recently-used-p (buffer1 buffer2)
1386 "Return t if BUFFER1 is more recently used than BUFFER2."
1387 (if (equal buffer1 buffer2)
1388 nil
1389 (let ((more-recent nil)
1390 (list (buffer-list)))
1391 (while (and list
1392 (not (setq more-recent (equal buffer1 (car list))))
1393 (not (equal buffer2 (car list))))
1394 (setq list (cdr list)))
1395 more-recent)))
1396
1397;;; Same thing as `dired-buffers-for-dir' of dired.el? - lrd 11/23/93
1398;;; (defun dired-buffers-for-dir-exact (dir)
1399;;; ;; Return a list of buffers that dired DIR (a directory or wildcard)
1400;;; ;; at top level, or as subdirectory.
1401;;; ;; Top level matches must match the wildcard part too, if any.
1402;;; ;; The list is in reverse order of buffer creation, most recent last.
1403;;; ;; As a side effect, killed dired buffers for DIR are removed from
1404;;; ;; dired-buffers.
1405;;; (let ((alist dired-buffers) result elt)
1406;;; (while alist
1407;;; (setq elt (car alist)
1408;;; alist (cdr alist))
1409;;; (let ((buf (cdr elt)))
1410;;; (if (buffer-name buf)
1411;;; ;; Top level must match exactly against dired-directory in
1412;;; ;; case one of them is a wildcard.
1413;;; (if (or (equal dir (save-excursion (set-buffer buf)
1414;;; dired-directory))
1415;;; (assoc dir (save-excursion (set-buffer buf)
1416;;; dired-subdir-alist)))
1417;;; (setq result (cons buf result)))
1418;;; ;; else buffer is killed - clean up:
1419;;; (setq dired-buffers (delq elt dired-buffers)))))
1420;;; result))
1421
9572a9dd 1422;;; REDEFINE.
cb3fe1f0
RS
1423;;; Redefines dired.el's version of `dired-initial-position'
1424(defun dired-initial-position (dirname)
1425 (end-of-line)
1426 (if dired-find-subdir (dired-goto-subdir dirname)) ; new
1427 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
1428
1429\f
1430;; Does anyone use this? - lrd 6/29/93.
1431(defun dired-mark-sexp (predicate &optional unflag-p)
1432 "Mark files for which PREDICATE returns non-nil.
1433With a prefix arg, unflag those files instead.
1434
1435PREDICATE is a lisp expression that can refer to the following symbols:
1436
1437 inode [integer] the inode of the file (only for ls -i output)
1438 s [integer] the size of the file for ls -s output
1439 (ususally in blocks or, with -k, in KByte)
1440 mode [string] file permission bits, e.g. \"-rw-r--r--\"
1441 nlink [integer] number of links to file
1442 uid [string] owner
1443 gid [string] group (If the gid is not displayed by ls,
1444 this will still be set (to the same as uid))
1445 size [integer] file size in bytes
1446 time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
1447 name [string] the name of the file
1448 sym [string] if file is a symbolic link, the linked-to name, else \"\"
1449
1450For example, use
1451
1452 (equal 0 size)
1453
1454to mark all zero length files."
1455 ;; Using sym="" instead of nil avoids the trap of
1456 ;; (string-match "foo" sym) into which a user would soon fall.
1457 ;; Give `equal' instead of `=' in the example, as this works on
1458 ;; integers and strings.
1459 (interactive "xMark if (lisp expr): \nP")
1460 (message "%s" predicate)
1461 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))
1462 inode s mode nlink uid gid size time name sym)
1463 (dired-mark-if
1464 (save-excursion
1465 (and
1466 ;; Sets vars
1467 ;; inode s mode nlink uid gid size time name sym
1468
1469 ;; according to current file line. Returns t for success, nil if
1470 ;; there is no file line. Upon success, all variables are set, either
1471 ;; to nil or the appropriate value, so they need not be initialized.
1472 ;; Moves point within the current line.
1473 (if (dired-move-to-filename)
1474 (let (pos
1475 (mode-len 10) ; length of mode string
1476 ;; like in dired.el, but with subexpressions \1=inode, \2=s:
1477 (dired-re-inode-size "\\s *\\([0-9]*\\)\\s *\\([0-9]*\\) ?"))
1478 (beginning-of-line)
1479 (forward-char 2)
1480 (if (looking-at dired-re-inode-size)
1481 (progn
1482 (goto-char (match-end 0))
1483 (setq inode (string-to-int (buffer-substring (match-beginning 1)
1484 (match-end 1)))
1485 s (string-to-int (buffer-substring (match-beginning 2)
1486 (match-end 2)))))
1487 (setq inode nil
1488 s nil))
1489 (setq mode (buffer-substring (point) (+ mode-len (point))))
1490 (forward-char mode-len)
1491 (setq nlink (read (current-buffer)))
1492 (setq uid (buffer-substring (point) (progn (forward-word 1) (point))))
1493 (re-search-forward "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)")
1494 (goto-char (match-beginning 1))
1495 (forward-char -1)
1496 (setq size (string-to-int (buffer-substring (save-excursion
1497 (backward-word 1)
1498 (setq pos (point)))
1499 (point))))
1500 (goto-char pos)
1501 (backward-word 1)
1502 ;; if no gid is displayed, gid will be set to uid
1503 ;; but user will then not reference it anyway in PREDICATE.
1504 (setq gid (buffer-substring (save-excursion (forward-word 1) (point))
1505 (point))
1506 time (buffer-substring (match-beginning 1)
1507 (1- (dired-move-to-filename)))
1508 name (buffer-substring (point)
1509 (or (dired-move-to-end-of-filename t)
1510 (point)))
1511 sym (progn
1512 (if (looking-at " -> ")
1513 (buffer-substring (progn (forward-char 4) (point))
1514 (progn (end-of-line) (point)))
1515 "")))
1516 t)
1517 nil)
1518 (eval predicate)))
1519 (format "'%s file" predicate))))
1520
1521\f
1522;;;; FIND FILE AT POINT.
cb3fe1f0 1523
ccb1d39a
RS
1524(defvar dired-x-hands-off-my-keys t
1525 "*t means don't bind `dired-x-find-file' over `find-file' on keyboard.
1526Similarly for `dired-x-find-file-other-window' over `find-file-other-window'.
1527If you change this variable after dired-x.el is loaded then do
1528\\[dired-x-bind-find-file].")
1529
1530;;; Bind `dired-x-find-file{-other-window}' over wherever
1531;;; `find-file{-other-window}' is bound?
1532(defun dired-x-bind-find-file ()
1533 "Bind `dired-x-find-file' in place of `find-file' \(or reverse\).
1534Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
1535Binding direction based on `dired-x-hands-off-my-keys'.
1536This function part of `after-init-hook'."
1537 (interactive)
1538 (if (interactive-p)
1539 (setq dired-x-hands-off-my-keys
1540 (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
1541 (cond ((not dired-x-hands-off-my-keys)
1542 (substitute-key-definition 'find-file
1543 'dired-x-find-file
1544 (current-global-map))
1545 (substitute-key-definition 'find-file-other-window
1546 'dired-x-find-file-other-window
1547 (current-global-map)))
1548 (t
1549 (substitute-key-definition 'dired-x-find-file
1550 'find-file
1551 (current-global-map))
1552 (substitute-key-definition 'dired-x-find-file-other-window
1553 'find-file-other-window
1554 (current-global-map))))
1555 ;; Clear mini-buffer.
1556 (message nil))
1557
1558;;; Now call it so binding is correct and put on `after-init-hook' in case
1559;;; user changes binding.
1560(dired-x-bind-find-file)
1561(add-hook 'after-init-hook 'dired-x-bind-find-file)
1562
1563(defun dired-x-find-file (filename)
1564 "Edit file FILENAME.
1565May create a new window, or reuse an existing one.
1566See the function `display-buffer'.
1567
1568Identical to `find-file' except when called interactively, with a prefix arg
1569\(e.g., \\[universal-argument]\), in which case it guesses filename near
1570point. Useful for editing file mentioned in buffer you are viewing, or to
1571test if that file exists. Use minibuffer after snatching filename."
1572 (interactive (list (read-filename-at-point "Find file: ")))
1573 (find-file (expand-file-name filename)))
1574
1575(defun dired-x-find-file-other-window (filename)
1576 "Edit file FILENAME, in another window.
1577May create a new window, or reuse an existing one.
1578See the function `display-buffer'.
1579
1580Identical to `find-file-other-window' except when called interactively, with a
1581prefix arg \(e.g., \\[universal-argument]\), in which case it guesses filename
1582near point. Useful for editing file mentioned in buffer you are viewing, or
1583to test if that file exists. Use minibuffer after snatching filename."
1584 (interactive (list (read-filename-at-point "Find file: ")))
1585 (find-file-other-window (expand-file-name filename)))
1586
1587;;; Internal functions.
cb3fe1f0
RS
1588(defun dired-filename-at-point ()
1589
1590 ;; Get the filename closest to point, but do not change position. Has a
1591 ;; preference for looking backward when not directly on a symbol. Not
1592 ;; perfect - point must be in middle of or end of filename.
1593
9572a9dd 1594 (let ((filename-chars ".a-zA-Z0-9---_/:$+")
cb3fe1f0
RS
1595 (bol (save-excursion (beginning-of-line) (point)))
1596 (eol (save-excursion (end-of-line) (point)))
1597 start end filename)
1598
1599 (save-excursion
1600 ;; First see if just past a filename.
1601 (if (not (eobp))
1602 (if (looking-at "[] \t\n[{}()]") ; whitespace or some parens
1603 (progn
1604 (skip-chars-backward " \n\t\r({[]})")
1605 (if (not (bobp))
1606 (backward-char 1)))))
9572a9dd 1607
cb3fe1f0
RS
1608 (if (string-match (concat "[" filename-chars "]")
1609 (char-to-string (following-char)))
1610 (progn
1611 (skip-chars-backward filename-chars)
1612 (setq start (point))
1613 (if (string-match "[/~]" (char-to-string (preceding-char)))
1614 (setq start (1- start)))
1615 (skip-chars-forward filename-chars))
1616
1617 (error "No file found around point!"))
1618
9572a9dd 1619 ;; Return string.
cb3fe1f0
RS
1620 (expand-file-name (buffer-substring start (point))))))
1621
ccb1d39a
RS
1622(defun read-filename-at-point (prompt)
1623 ;;; Returns filename prompting with PROMPT with completion. If
1624 ;;; `current-prefix-arg' is non-nil, uses name at point as guess.
1625 (if current-prefix-arg
1626 (let ((guess (dired-filename-at-point)))
1627 (read-file-name prompt
1628 (file-name-directory guess)
1629 guess
1630 nil (file-name-nondirectory guess)))
1631 (read-file-name prompt default-directory)))
1632
cb3fe1f0
RS
1633\f
1634;;;; BUG REPORTS
1635
1636;;; This section is provided for reports. It uses Barry A. Warsaw's
1637;;; reporter.el which is bundled with GNU Emacs v19.
1638
ccb1d39a 1639(defconst dired-x-version "2.37"
cb3fe1f0 1640 "Revision number of dired-x.el -- dired extra for GNU Emacs v19.
9572a9dd 1641Type \\[dired-x-submit-report] to send a bug report. Available via anonymous
cb3fe1f0
RS
1642ftp in
1643
1644 /roebling.poly.edu:/pub/packages/dired-x.tar.gz")
1645
1646(defconst dired-x-help-address "dodd@roebling.poly.edu"
1647 "Address(es) accepting submission of reports on dired-x.el.")
1648
1649(defconst dired-x-maintainer "Larry"
1650 "First name(s) of people accepting submission of reports on dired-x.el.")
1651
1652(defconst dired-x-file "dired-x.el"
1653 "Name of file containing emacs lisp code.")
1654
1655(defconst dired-x-variable-list
9572a9dd 1656 (list
cb3fe1f0
RS
1657 'dired-bind-vm
1658 'dired-vm-read-only-folders
1659 'dired-bind-jump
1660 'dired-bind-info
1661 'dired-bind-man
1662 'dired-find-subdir
1663 'dired-enable-local-variables
1664 'dired-local-variables-file
1665 'dired-guess-shell-gnutar
1666 'dired-guess-shell-gzip-quiet
1667 'dired-guess-shell-znew-switches
1668 'dired-guess-shell-alist-user
1669 'dired-clean-up-buffers-too
1670 'dired-omit-files-p
1671 'dired-omit-files
1672 'dired-omit-extensions
1673 )
1674 "List of variables to be appended to reports sent by `dired-x-submit-report.'")
1675
1676(defun dired-x-submit-report ()
1677 "Submit via reporter.el a bug report on program. Send report on `dired-x-file'
1678version `dired-x-version,' to `dired-x-maintainer' at address `dired-x-help-address'
1679listing variables `dired-x-variable-list' in the message."
1680 (interactive)
1681
1682 ;; In case we can't find reporter...
1683 (condition-case err
1684 (progn
1685 ;; Get it if we can.
1686 (require 'reporter)
1687
1688 (reporter-submit-bug-report
1689 dired-x-help-address ; address
1690 (concat dired-x-file " (" dired-x-version ")") ; pkgname
1691 dired-x-variable-list ; varlist
1692 nil nil ; pre-/post-hooks
1693 (concat dired-x-maintainer ","))) ; salutation
1694
1695 ;; ...fail gracefully.
9572a9dd 1696 (error
cb3fe1f0
RS
1697 (beep)
1698 (message "Sorry, reporter.el not found."))))
1699
1700\f
1701;; As Barry Warsaw would say: "This might be useful..."
1702(provide 'dired-x)
1703
1704;;; dired-x.el ends here