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