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