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