]> code.delx.au - gnu-emacs/blob - lisp/dired-x.el
dired-x largely cosmetic changes.
[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 #'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 (and (stringp dired-local-variables-file)
832 (file-exists-p dired-local-variables-file)
833 (let ((opoint (point-max))
834 (inhibit-read-only t)
835 ;; In case user has `enable-local-variables' set to nil we
836 ;; override it locally with dired's variable.
837 (enable-local-variables dired-enable-local-variables))
838 ;; Insert 'em.
839 (save-excursion
840 (goto-char opoint)
841 (insert "\^L\n")
842 (insert-file-contents dired-local-variables-file))
843 ;; Hack 'em.
844 (let ((buffer-file-name dired-local-variables-file))
845 (hack-local-variables))
846 ;; Make sure that the modeline shows the proper information.
847 (dired-sort-set-modeline)
848 ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
849 (delete-region opoint (point-max)))))
850
851 (defun dired-omit-here-always ()
852 "Create `dired-local-variables-file' for omitting and reverts directory.
853 Sets `dired-omit-mode' to t in a local variables file that is readable by
854 dired."
855 (interactive)
856 (if (file-exists-p dired-local-variables-file)
857 (message "File `./%s' already exists." dired-local-variables-file)
858 ;; Create `dired-local-variables-file'.
859 (with-current-buffer (get-buffer-create " *dot-dired*")
860 (erase-buffer)
861 (insert "Local Variables:\ndired-omit-mode: t\nEnd:\n")
862 (write-file dired-local-variables-file)
863 (kill-buffer))
864 ;; Run extra-hooks and revert directory.
865 (dired-extra-startup)
866 (dired-revert)))
867
868 \f
869 ;;; GUESS SHELL COMMAND.
870
871 ;; Brief Description:
872 ;;;
873 ;; `dired-do-shell-command' is bound to `!' by dired.el.
874 ;;;
875 ;; * Redefine `dired-read-shell-command' so it calls
876 ;;; `dired-guess-shell-command'.
877 ;;;
878 ;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
879 ;;; marked files.
880 ;;;
881 ;; * Parse `dired-guess-shell-alist-user' and
882 ;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
883 ;;; that matches the first file in the file list.
884 ;;;
885 ;; * If the REGEXP matches all the entries of the file list then evaluate
886 ;;; COMMAND, which is either a string or a Lisp expression returning a
887 ;;; string. COMMAND may be a list of commands.
888 ;;;
889 ;; * Return this command to `dired-guess-shell-command' which prompts user
890 ;;; with it. The list of commands is put into the list of default values.
891 ;;; If a command is used successfully then it is stored permanently in
892 ;;; `dired-shell-command-history'.
893
894 ;; Guess what shell command to apply to a file.
895 (defvar dired-shell-command-history nil
896 "History list for commands that read dired-shell commands.")
897
898 ;; Default list of shell commands.
899
900 ;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
901 ;; install GNU zip's version of zcat.
902
903 (declare-function Man-support-local-filenames "man" ())
904
905 (defvar dired-guess-shell-alist-default
906 (list
907 (list "\\.tar$"
908 '(if dired-guess-shell-gnutar
909 (concat dired-guess-shell-gnutar " xvf")
910 "tar xvf")
911 ;; Extract files into a separate subdirectory
912 '(if dired-guess-shell-gnutar
913 (concat "mkdir " (file-name-sans-extension file)
914 "; " dired-guess-shell-gnutar " -C "
915 (file-name-sans-extension file) " -xvf")
916 (concat "mkdir " (file-name-sans-extension file)
917 "; tar -C " (file-name-sans-extension file) " -xvf"))
918 ;; List archive contents.
919 '(if dired-guess-shell-gnutar
920 (concat dired-guess-shell-gnutar " tvf")
921 "tar tvf"))
922
923 ;; REGEXPS for compressed archives must come before the .Z rule to
924 ;; be recognized:
925 (list "\\.tar\\.Z$"
926 ;; Untar it.
927 '(if dired-guess-shell-gnutar
928 (concat dired-guess-shell-gnutar " zxvf")
929 (concat "zcat * | tar xvf -"))
930 ;; Optional conversion to gzip format.
931 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
932 " " dired-guess-shell-znew-switches))
933
934 ;; gzip'ed archives
935 (list "\\.t\\(ar\\.\\)?gz$"
936 '(if dired-guess-shell-gnutar
937 (concat dired-guess-shell-gnutar " zxvf")
938 (concat "gunzip -qc * | tar xvf -"))
939 ;; Extract files into a separate subdirectory
940 '(if dired-guess-shell-gnutar
941 (concat "mkdir " (file-name-sans-extension file)
942 "; " dired-guess-shell-gnutar " -C "
943 (file-name-sans-extension file) " -zxvf")
944 (concat "mkdir " (file-name-sans-extension file)
945 "; gunzip -qc * | tar -C "
946 (file-name-sans-extension file) " -xvf -"))
947 ;; Optional decompression.
948 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" ""))
949 ;; List archive contents.
950 '(if dired-guess-shell-gnutar
951 (concat dired-guess-shell-gnutar " ztvf")
952 (concat "gunzip -qc * | tar tvf -")))
953
954 ;; bzip2'ed archives
955 (list "\\.t\\(ar\\.bz2\\|bz\\)$"
956 "bunzip2 -c * | tar xvf -"
957 ;; Extract files into a separate subdirectory
958 '(concat "mkdir " (file-name-sans-extension file)
959 "; bunzip2 -c * | tar -C "
960 (file-name-sans-extension file) " -xvf -")
961 ;; Optional decompression.
962 "bunzip2")
963
964 ;; xz'ed archives
965 (list "\\.t\\(ar\\.\\)?xz$"
966 "unxz -c * | tar xvf -"
967 ;; Extract files into a separate subdirectory
968 '(concat "mkdir " (file-name-sans-extension file)
969 "; unxz -c * | tar -C "
970 (file-name-sans-extension file) " -xvf -")
971 ;; Optional decompression.
972 "unxz")
973
974 '("\\.shar\\.Z$" "zcat * | unshar")
975 '("\\.shar\\.g?z$" "gunzip -qc * | unshar")
976
977 '("\\.e?ps$" "ghostview" "xloadimage" "lpr")
978 (list "\\.e?ps\\.g?z$" "gunzip -qc * | ghostview -"
979 ;; Optional decompression.
980 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
981 (list "\\.e?ps\\.Z$" "zcat * | ghostview -"
982 ;; Optional conversion to gzip format.
983 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
984 " " dired-guess-shell-znew-switches))
985
986 '("\\.patch$" "cat * | patch")
987 (list "\\.patch\\.g?z$" "gunzip -qc * | patch"
988 ;; Optional decompression.
989 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
990 (list "\\.patch\\.Z$" "zcat * | patch"
991 ;; Optional conversion to gzip format.
992 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
993 " " dired-guess-shell-znew-switches))
994
995 ;; The following four extensions are useful with dired-man ("N" key)
996 (list "\\.\\(?:[0-9]\\|man\\)$" '(progn (require 'man)
997 (if (Man-support-local-filenames)
998 "man -l"
999 "cat * | tbl | nroff -man -h")))
1000 (list "\\.\\(?:[0-9]\\|man\\)\\.g?z$" '(progn (require 'man)
1001 (if (Man-support-local-filenames)
1002 "man -l"
1003 "gunzip -qc * | tbl | nroff -man -h"))
1004 ;; Optional decompression.
1005 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1006 (list "\\.[0-9]\\.Z$" '(progn (require 'man)
1007 (if (Man-support-local-filenames)
1008 "man -l"
1009 "zcat * | tbl | nroff -man -h"))
1010 ;; Optional conversion to gzip format.
1011 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1012 " " dired-guess-shell-znew-switches))
1013 '("\\.pod$" "perldoc" "pod2man * | nroff -man")
1014
1015 '("\\.dvi$" "xdvi" "dvips") ; preview and printing
1016 '("\\.au$" "play") ; play Sun audiofiles
1017 '("\\.mpe?g$\\|\\.avi$" "xine -p")
1018 '("\\.ogg$" "ogg123")
1019 '("\\.mp3$" "mpg123")
1020 '("\\.wav$" "play")
1021 '("\\.uu$" "uudecode") ; for uudecoded files
1022 '("\\.hqx$" "mcvert")
1023 '("\\.sh$" "sh") ; execute shell scripts
1024 '("\\.xbm$" "bitmap") ; view X11 bitmaps
1025 '("\\.gp$" "gnuplot")
1026 '("\\.p[bgpn]m$" "xloadimage")
1027 '("\\.gif$" "xloadimage") ; view gif pictures
1028 '("\\.tif$" "xloadimage")
1029 '("\\.png$" "display") ; xloadimage 4.1 doesn't grok PNG
1030 '("\\.jpe?g$" "xloadimage")
1031 '("\\.fig$" "xfig") ; edit fig pictures
1032 '("\\.out$" "xgraph") ; for plotting purposes.
1033 '("\\.tex$" "latex" "tex")
1034 '("\\.texi\\(nfo\\)?$" "makeinfo" "texi2dvi")
1035 '("\\.pdf$" "xpdf")
1036 '("\\.doc$" "antiword" "strings")
1037 '("\\.rpm$" "rpm -qilp" "rpm -ivh")
1038 '("\\.dia$" "dia")
1039 '("\\.mgp$" "mgp")
1040
1041 ;; Some other popular archivers.
1042 (list "\\.zip$" "unzip" "unzip -l"
1043 ;; Extract files into a separate subdirectory
1044 '(concat "unzip" (if dired-guess-shell-gzip-quiet " -q")
1045 " -d " (file-name-sans-extension file)))
1046 '("\\.zoo$" "zoo x//")
1047 '("\\.lzh$" "lharc x")
1048 '("\\.arc$" "arc x")
1049 '("\\.shar$" "unshar")
1050
1051 ;; Compression.
1052 (list "\\.g?z$" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1053 (list "\\.dz$" "dictunzip")
1054 (list "\\.bz2$" "bunzip2")
1055 (list "\\.xz$" "unxz")
1056 (list "\\.Z$" "uncompress"
1057 ;; Optional conversion to gzip format.
1058 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1059 " " dired-guess-shell-znew-switches))
1060
1061 '("\\.sign?$" "gpg --verify"))
1062
1063 "Default alist used for shell command guessing.
1064 See `dired-guess-shell-alist-user'.")
1065
1066 (defcustom dired-guess-shell-alist-user nil
1067 "User-defined alist of rules for suggested commands.
1068 These rules take precedence over the predefined rules in the variable
1069 `dired-guess-shell-alist-default' (to which they are prepended).
1070
1071 Each element of this list looks like
1072
1073 \(REGEXP COMMAND...\)
1074
1075 where each COMMAND can either be a string or a Lisp expression that evaluates
1076 to a string. If several COMMANDs are given, the first one will be the default
1077 and the rest will be added temporarily to the history and can be retrieved
1078 with \\[previous-history-element] (M-p) .
1079
1080 The variable `dired-guess-shell-case-fold-search' controls whether
1081 REGEXP is matched case-sensitively.
1082
1083 You can set this variable in your ~/.emacs. For example, to add rules for
1084 `.foo' and `.bar' files, write
1085
1086 \(setq dired-guess-shell-alist-user
1087 (list (list \"\\\\.foo\\\\'\" \"FOO-COMMAND\");; fixed rule
1088 ;; possibly more rules ...
1089 (list \"\\\\.bar\\\\'\";; rule with condition test
1090 '(if condition
1091 \"BAR-COMMAND-1\"
1092 \"BAR-COMMAND-2\")))\)"
1093 :group 'dired-x
1094 :type '(alist :key-type regexp :value-type (repeat sexp)))
1095
1096 (defcustom dired-guess-shell-case-fold-search t
1097 "If non-nil, `dired-guess-shell-alist-default' and
1098 `dired-guess-shell-alist-user' are matched case-insensitively."
1099 :group 'dired-x
1100 :type 'boolean)
1101
1102 (defun dired-guess-default (files)
1103 "Guess a shell commands for FILES. Return command or list of commands.
1104 See `dired-guess-shell-alist-user'."
1105
1106 (let* ((case-fold-search dired-guess-shell-case-fold-search)
1107 ;; Prepend the user's alist to the default alist.
1108 (alist (append dired-guess-shell-alist-user
1109 dired-guess-shell-alist-default))
1110 (file (car files))
1111 (flist (cdr files))
1112 elt regexp cmds)
1113
1114 ;; Find the first match in the alist for first file in FILES.
1115 (while alist
1116 (setq elt (car alist)
1117 regexp (car elt)
1118 alist (cdr alist))
1119 (if (string-match regexp file)
1120 (setq cmds (cdr elt)
1121 alist nil)))
1122
1123 ;; If more than one file, see if all of FILES match regular expression.
1124 (while (and flist
1125 (string-match regexp (car flist)))
1126 (setq flist (cdr flist)))
1127
1128 ;; If flist is still non-nil, then do not guess since this means that not
1129 ;; all the files in FILES were matched by the regexp.
1130 (setq cmds (and (not flist) cmds))
1131
1132 ;; Return commands or nil if flist is still non-nil.
1133 ;; Evaluate the commands in order that any logical testing will be done.
1134 (if (cdr cmds)
1135 (mapcar #'eval cmds)
1136 (eval (car cmds))))) ; single command
1137
1138 (defun dired-guess-shell-command (prompt files)
1139 "Ask user with PROMPT for a shell command, guessing a default from FILES."
1140 (let ((default (dired-guess-default files))
1141 default-list val)
1142 (if (null default)
1143 ;; Nothing to guess
1144 (read-shell-command prompt nil 'dired-shell-command-history)
1145 (if (listp default)
1146 ;; More than one guess
1147 (setq default-list default
1148 default (car default)
1149 prompt (concat
1150 prompt
1151 (format "{%d guesses} " (length default-list))))
1152 ;; Just one guess
1153 (setq default-list (list default)))
1154 ;; Put the first guess in the prompt but not in the initial value.
1155 (setq prompt (concat prompt (format "[%s] " default)))
1156 ;; All guesses can be retrieved with M-n
1157 (setq val (read-shell-command prompt nil
1158 'dired-shell-command-history
1159 default-list))
1160 ;; If we got a return, then return default.
1161 (if (equal val "") default val))))
1162
1163 ;; REDEFINE.
1164 ;; Redefine dired-aux.el's version:
1165 (defun dired-read-shell-command (prompt arg files)
1166 "Read a dired shell command prompting with PROMPT (using `read-shell-command').
1167 ARG is the prefix arg and may be used to indicate in the prompt which
1168 FILES are affected.
1169 This is an extra function so that you can redefine it."
1170 (minibuffer-with-setup-hook
1171 (lambda ()
1172 (set (make-local-variable 'minibuffer-default-add-function)
1173 'minibuffer-default-add-dired-shell-commands))
1174 (dired-mark-pop-up
1175 nil 'shell files
1176 'dired-guess-shell-command
1177 (format prompt (dired-mark-prompt arg files)) ; PROMPT
1178 files))) ; FILES
1179
1180 \f
1181 ;;; RELATIVE SYMBOLIC LINKS.
1182
1183 (declare-function make-symbolic-link "fileio.c")
1184
1185 (defvar dired-keep-marker-relsymlink ?S
1186 "See variable `dired-keep-marker-move'.")
1187
1188 (defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
1189 "Make a symbolic link (pointing to FILE1) in FILE2.
1190 The link is relative (if possible), for example
1191
1192 \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
1193
1194 results in
1195
1196 \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
1197 (interactive "FRelSymLink: \nFRelSymLink %s: \np")
1198 (let (name1 name2 len1 len2 (index 0) sub)
1199 (setq file1 (expand-file-name file1)
1200 file2 (expand-file-name file2)
1201 len1 (length file1)
1202 len2 (length file2))
1203 ;; Find common initial file name components:
1204 (let (next)
1205 (while (and (setq next (string-match "/" file1 index))
1206 (< (setq next (1+ next)) (min len1 len2))
1207 ;; For the comparison, both substrings must end in
1208 ;; `/', so NEXT is *one plus* the result of the
1209 ;; string-match.
1210 ;; E.g., consider the case of linking "/tmp/a/abc"
1211 ;; to "/tmp/abc" erroneously giving "/tmp/a" instead
1212 ;; of "/tmp/" as common initial component
1213 (string-equal (substring file1 0 next)
1214 (substring file2 0 next)))
1215 (setq index next))
1216 (setq name2 file2
1217 sub (substring file1 0 index)
1218 name1 (substring file1 index)))
1219 (if (string-equal sub "/")
1220 ;; No common initial file name found
1221 (setq name1 file1)
1222 ;; Else they have a common parent directory
1223 (let ((tem (substring file2 index))
1224 (start 0)
1225 (count 0))
1226 ;; Count number of slashes we must compensate for ...
1227 (while (setq start (string-match "/" tem start))
1228 (setq count (1+ count)
1229 start (1+ start)))
1230 ;; ... and prepend a "../" for each slash found:
1231 (dotimes (n count)
1232 (setq name1 (concat "../" name1)))))
1233 (make-symbolic-link
1234 (directory-file-name name1) ; must not link to foo/
1235 ; (trailing slash!)
1236 name2 ok-if-already-exists)))
1237
1238 ;;;###autoload
1239 (defun dired-do-relsymlink (&optional arg)
1240 "Relative symlink all marked (or next ARG) files into a directory.
1241 Otherwise make a relative symbolic link to the current file.
1242 This creates relative symbolic links like
1243
1244 foo -> ../bar/foo
1245
1246 not absolute ones like
1247
1248 foo -> /ugly/file/name/that/may/change/any/day/bar/foo
1249
1250 For absolute symlinks, use \\[dired-do-symlink]."
1251 (interactive "P")
1252 (dired-do-create-files 'relsymlink #'dired-make-relative-symlink
1253 "RelSymLink" arg dired-keep-marker-relsymlink))
1254
1255 (defun dired-do-relsymlink-regexp (regexp newname &optional arg whole-name)
1256 "RelSymlink all marked files containing REGEXP to NEWNAME.
1257 See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
1258 for more info."
1259 (interactive (dired-mark-read-regexp "RelSymLink"))
1260 (dired-do-create-files-regexp
1261 #'dired-make-relative-symlink
1262 "RelSymLink" arg regexp newname whole-name dired-keep-marker-relsymlink))
1263
1264 \f
1265 ;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
1266
1267 ;; Brief Description:
1268 ;;;
1269 ;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
1270 ;;;
1271 ;; * Use `dired-get-marked-files' to collect the marked files in the current
1272 ;;; Dired Buffer into a list of filenames `FILE-LIST'.
1273 ;;;
1274 ;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
1275 ;;; `dired-do-find-marked-files''s prefix argument NOSELECT.
1276 ;;;
1277 ;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
1278 ;;; list each time.
1279 ;;;
1280 ;; * If NOSELECT is non-nil then just run `find-file-noselect' on each
1281 ;;; element of FILE-LIST.
1282 ;;;
1283 ;; * If NOSELECT is nil then calculate the `size' of the window for each file
1284 ;;; by dividing the `window-height' by length of FILE-LIST. Thus, `size' is
1285 ;;; cognizant of the window-configuration.
1286 ;;;
1287 ;; * If `size' is too small abort, otherwise run `find-file' on each element
1288 ;;; of FILE-LIST giving each a window of height `size'.
1289
1290 (defun dired-do-find-marked-files (&optional noselect)
1291 "Find all marked files displaying all of them simultaneously.
1292 With optional NOSELECT just find files but do not select them.
1293
1294 The current window is split across all files marked, as evenly as possible.
1295 Remaining lines go to bottom-most window. The number of files that can be
1296 displayed this way is restricted by the height of the current window and
1297 `window-min-height'.
1298
1299 To keep dired buffer displayed, type \\[split-window-vertically] first.
1300 To display just marked files, type \\[delete-other-windows] first."
1301 (interactive "P")
1302 (dired-simultaneous-find-file (dired-get-marked-files) noselect))
1303
1304 (defun dired-simultaneous-find-file (file-list noselect)
1305 "Visit all files in FILE-LIST and display them simultaneously.
1306 The current window is split across all files in FILE-LIST, as evenly as
1307 possible. Remaining lines go to the bottom-most window. The number of
1308 files that can be displayed this way is restricted by the height of the
1309 current window and the variable `window-min-height'. With non-nil
1310 NOSELECT the files are merely found but not selected."
1311 ;; We don't make this function interactive because it is usually too clumsy
1312 ;; to specify FILE-LIST interactively unless via dired.
1313 (let (size)
1314 (if noselect
1315 ;; Do not select the buffer.
1316 (find-file-noselect (car file-list))
1317 ;; We will have to select the buffer. Calculate and check window size.
1318 (setq size (/ (window-height) (length file-list)))
1319 (or (<= window-min-height size)
1320 (error "Too many files to visit simultaneously. Try C-u prefix"))
1321 (find-file (car file-list)))
1322 ;; Decrement.
1323 (dolist (file (cdr file-list))
1324 (if noselect
1325 ;; Do not select the buffer.
1326 (find-file-noselect file)
1327 ;; Vertically split off a window of desired size. Upper window will
1328 ;; have SIZE lines. Select lower (larger) window. We split it again.
1329 (select-window (split-window nil size))
1330 (find-file file)))))
1331
1332 \f
1333 ;;; MISCELLANEOUS COMMANDS.
1334
1335 ;; Run man on files.
1336
1337 (declare-function Man-getpage-in-background "man" (topic))
1338
1339 (defun dired-man ()
1340 "Run `man' on this file."
1341 ;; Used also to say: "Display old buffer if buffer name matches filename."
1342 ;; but I have no idea what that means.
1343 (interactive)
1344 (require 'man)
1345 (let* ((file (dired-get-filename))
1346 (manual-program (replace-regexp-in-string "\\*" "%s"
1347 (dired-guess-shell-command
1348 "Man command: " (list file)))))
1349 (Man-getpage-in-background file)))
1350
1351 ;; Run Info on files.
1352
1353 (defun dired-info ()
1354 "Run `info' on this file."
1355 (interactive)
1356 (info (dired-get-filename)))
1357
1358 ;; Run mail on mail folders.
1359
1360 (declare-function vm-visit-folder "ext:vm" (folder &optional read-only))
1361 (defvar vm-folder-directory)
1362
1363 (defun dired-vm (&optional read-only)
1364 "Run VM on this file.
1365 With optional prefix argument, visits the folder read-only.
1366 Otherwise obeys the value of `dired-vm-read-only-folders'."
1367 (interactive "P")
1368 (let ((dir (dired-current-directory))
1369 (fil (dired-get-filename)))
1370 (vm-visit-folder fil (or read-only
1371 (eq t dired-vm-read-only-folders)
1372 (and dired-vm-read-only-folders
1373 (not (file-writable-p fil)))))
1374 ;; So that pressing `v' inside VM does prompt within current directory:
1375 (set (make-local-variable 'vm-folder-directory) dir)))
1376
1377 (defun dired-rmail ()
1378 "Run RMAIL on this file."
1379 (interactive)
1380 (rmail (dired-get-filename)))
1381
1382 (defun dired-do-run-mail ()
1383 "If `dired-bind-vm' is non-nil, call `dired-vm', else call `dired-rmail'."
1384 (interactive)
1385 (if dired-bind-vm
1386 ;; Read mail folder using vm.
1387 (dired-vm)
1388 ;; Read mail folder using rmail.
1389 (dired-rmail)))
1390
1391 \f
1392 ;;; MISCELLANEOUS INTERNAL FUNCTIONS.
1393
1394 (declare-function dired-old-find-buffer-nocreate "dired-x")
1395
1396 (or (fboundp 'dired-old-find-buffer-nocreate)
1397 (fset 'dired-old-find-buffer-nocreate
1398 (symbol-function 'dired-find-buffer-nocreate)))
1399
1400 ;; REDEFINE.
1401 ;; Redefines dired.el's version of `dired-find-buffer-nocreate'
1402 (defun dired-find-buffer-nocreate (dirname &optional mode)
1403 (if (and dired-find-subdir
1404 ;; don't try to find a wildcard as a subdirectory
1405 (string-equal dirname (file-name-directory dirname)))
1406 (let* ((cur-buf (current-buffer))
1407 (buffers (nreverse
1408 (dired-buffers-for-dir (expand-file-name dirname))))
1409 (cur-buf-matches (and (memq cur-buf buffers)
1410 ;; wildcards must match, too:
1411 (equal dired-directory dirname))))
1412 ;; We don't want to switch to the same buffer---
1413 (setq buffers (delq cur-buf buffers));;need setq with delq
1414 (or (car (sort buffers #'dired-buffer-more-recently-used-p))
1415 ;; ---unless it's the only possibility:
1416 (and cur-buf-matches cur-buf)))
1417 (dired-old-find-buffer-nocreate dirname mode)))
1418
1419 ;; This should be a builtin
1420 (defun dired-buffer-more-recently-used-p (buffer1 buffer2)
1421 "Return t if BUFFER1 is more recently used than BUFFER2.
1422 Considers buffers closer to the car of `buffer-list' to be more recent."
1423 (and (not (equal buffer1 buffer2))
1424 (memq buffer1 (buffer-list))
1425 (not (memq buffer1 (memq buffer2 (buffer-list))))))
1426
1427 ;; Same thing as `dired-buffers-for-dir' of dired.el? - lrd 11/23/93
1428 ;; (defun dired-buffers-for-dir-exact (dir)
1429 ;; ;; Return a list of buffers that dired DIR (a directory or wildcard)
1430 ;; ;; at top level, or as subdirectory.
1431 ;; ;; Top level matches must match the wildcard part too, if any.
1432 ;; ;; The list is in reverse order of buffer creation, most recent last.
1433 ;; ;; As a side effect, killed dired buffers for DIR are removed from
1434 ;; ;; dired-buffers.
1435 ;; (let ((alist dired-buffers) result elt)
1436 ;; (while alist
1437 ;; (setq elt (car alist)
1438 ;; alist (cdr alist))
1439 ;; (let ((buf (cdr elt)))
1440 ;; (if (buffer-name buf)
1441 ;; ;; Top level must match exactly against dired-directory in
1442 ;; ;; case one of them is a wildcard.
1443 ;; (if (or (equal dir (with-current-buffer buf dired-directory))
1444 ;; (assoc dir (with-current-buffer buf dired-subdir-alist)))
1445 ;; (setq result (cons buf result)))
1446 ;; ;; else buffer is killed - clean up:
1447 ;; (setq dired-buffers (delq elt dired-buffers)))))
1448 ;; result))
1449
1450 ;; REDEFINE.
1451 ;; Redefines dired.el's version of `dired-initial-position'
1452 (defun dired-initial-position (dirname)
1453 "Where point should go in a new listing of DIRNAME.
1454 Point assumed at beginning of new subdir line.
1455 You may redefine this function as you wish, e.g. like in `dired-x.el'."
1456 (end-of-line)
1457 (if dired-find-subdir (dired-goto-subdir dirname)) ; new
1458 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
1459
1460 \f
1461 ;; Does anyone use this? - lrd 6/29/93.
1462 ;; Apparently people do use it. - lrd 12/22/97.
1463 (defun dired-mark-sexp (predicate &optional unflag-p)
1464 "Mark files for which PREDICATE returns non-nil.
1465 With a prefix arg, unflag those files instead.
1466
1467 PREDICATE is a lisp expression that can refer to the following symbols:
1468
1469 inode [integer] the inode of the file (only for ls -i output)
1470 s [integer] the size of the file for ls -s output
1471 (usually in blocks or, with -k, in KByte)
1472 mode [string] file permission bits, e.g. \"-rw-r--r--\"
1473 nlink [integer] number of links to file
1474 uid [string] owner
1475 gid [string] group (If the gid is not displayed by ls,
1476 this will still be set (to the same as uid))
1477 size [integer] file size in bytes
1478 time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
1479 name [string] the name of the file
1480 sym [string] if file is a symbolic link, the linked-to name, else \"\"
1481
1482 For example, use
1483
1484 (equal 0 size)
1485
1486 to mark all zero length files."
1487 ;; Using sym="" instead of nil avoids the trap of
1488 ;; (string-match "foo" sym) into which a user would soon fall.
1489 ;; Give `equal' instead of `=' in the example, as this works on
1490 ;; integers and strings.
1491 (interactive "xMark if (lisp expr): \nP")
1492 (message "%s" predicate)
1493 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))
1494 inode s mode nlink uid gid size time name sym)
1495 (dired-mark-if
1496 (save-excursion
1497 (and
1498 ;; Sets vars
1499 ;; inode s mode nlink uid gid size time name sym
1500
1501 ;; according to current file line. Returns t for success, nil if
1502 ;; there is no file line. Upon success, all variables are set, either
1503 ;; to nil or the appropriate value, so they need not be initialized.
1504 ;; Moves point within the current line.
1505 (dired-move-to-filename)
1506 (let (pos
1507 (mode-len 10) ; length of mode string
1508 ;; like in dired.el, but with subexpressions \1=inode, \2=s:
1509 (dired-re-inode-size "\\s *\\([0-9]*\\)\\s *\\([0-9]*\\) ?"))
1510 (beginning-of-line)
1511 (forward-char 2)
1512 (if (looking-at dired-re-inode-size)
1513 (progn
1514 (goto-char (match-end 0))
1515 (setq inode (string-to-number
1516 (buffer-substring (match-beginning 1)
1517 (match-end 1)))
1518 s (string-to-number
1519 (buffer-substring (match-beginning 2)
1520 (match-end 2)))))
1521 (setq inode nil
1522 s nil))
1523 (setq mode (buffer-substring (point) (+ mode-len (point))))
1524 (forward-char mode-len)
1525 (setq nlink (read (current-buffer)))
1526 ;; Karsten Wenger <kw@cis.uni-muenchen.de> fixed uid.
1527 (setq uid (buffer-substring (1+ (point))
1528 (progn (forward-word 1) (point))))
1529 (re-search-forward directory-listing-before-filename-regexp)
1530 (goto-char (match-beginning 1))
1531 (forward-char -1)
1532 (setq size (string-to-number
1533 (buffer-substring (save-excursion
1534 (backward-word 1)
1535 (setq pos (point)))
1536 (point))))
1537 (goto-char pos)
1538 (backward-word 1)
1539 ;; if no gid is displayed, gid will be set to uid
1540 ;; but user will then not reference it anyway in PREDICATE.
1541 (setq gid (buffer-substring (save-excursion
1542 (forward-word 1) (point))
1543 (point))
1544 time (buffer-substring (match-beginning 1)
1545 (1- (dired-move-to-filename)))
1546 name (buffer-substring (point)
1547 (or
1548 (dired-move-to-end-of-filename t)
1549 (point)))
1550 sym (if (looking-at " -> ")
1551 (buffer-substring (progn (forward-char 4) (point))
1552 (line-end-position))
1553 ""))
1554 t)
1555 (eval predicate)))
1556 (format "'%s file" predicate))))
1557
1558 \f
1559 ;;; FIND FILE AT POINT.
1560
1561 (defcustom dired-x-hands-off-my-keys t
1562 "Non-nil means don't remap `find-file' to `dired-x-find-file'.
1563 Similarly for `find-file-other-window' and `dired-x-find-file-other-window'.
1564 If you change this variable without using \\[customize] after `dired-x.el'
1565 is loaded then call \\[dired-x-bind-find-file]."
1566 :type 'boolean
1567 :initialize 'custom-initialize-default
1568 :set (lambda (sym val)
1569 (set sym val)
1570 (dired-x-bind-find-file))
1571 :group 'dired-x)
1572
1573 (defun dired-x-bind-find-file ()
1574 "Bind `dired-x-find-file' in place of `find-file' (or vice-versa).
1575 Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
1576 Binding direction based on `dired-x-hands-off-my-keys'."
1577 (interactive)
1578 (if (called-interactively-p 'interactive)
1579 (setq dired-x-hands-off-my-keys
1580 (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
1581 (define-key (current-global-map) [remap find-file]
1582 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file))
1583 (define-key (current-global-map) [remap find-file-other-window]
1584 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file-other-window)))
1585
1586 ;; Now call it so binding is correct. This could go in the :initialize
1587 ;; slot, but then dired-x-bind-find-file has to be defined before the
1588 ;; defcustom, and we get free variable warnings.
1589 (dired-x-bind-find-file)
1590
1591 (defun dired-x-find-file (filename)
1592 "Edit file FILENAME.
1593 Like `find-file', except that when called interactively with a
1594 prefix argument, it offers the filename near point as a default."
1595 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1596 (find-file filename))
1597
1598 (defun dired-x-find-file-other-window (filename)
1599 "Edit file FILENAME, in another window.
1600 Like `find-file-other-window', except that when called interactively with
1601 a prefix argument, when it offers the filename near point as a default."
1602 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1603 (find-file-other-window filename))
1604
1605 ;;; Internal functions.
1606
1607 ;; Fixme: This should probably use `thing-at-point'. -- fx
1608 (defun dired-filename-at-point ()
1609 "Return the filename closest to point, expanded.
1610 Point should be in or after a filename."
1611 (save-excursion
1612 ;; First see if just past a filename.
1613 (or (eobp) ; why?
1614 (when (looking-at "[] \t\n[{}()]") ; whitespace or some parens
1615 (skip-chars-backward " \n\t\r({[]})")
1616 (or (bobp) (backward-char 1))))
1617 (let ((filename-chars "-.[:alnum:]_/:$+@")
1618 start prefix)
1619 (if (looking-at (format "[%s]" filename-chars))
1620 (progn
1621 (skip-chars-backward filename-chars)
1622 (setq start (point)
1623 prefix
1624 ;; This is something to do with ange-ftp filenames.
1625 ;; It convert foo@bar to /foo@bar.
1626 ;; But when does the former occur in dired buffers?
1627 (and (string-match
1628 "^\\w+@"
1629 (buffer-substring start (line-end-position)))
1630 "/"))
1631 (if (string-match "[/~]" (char-to-string (preceding-char)))
1632 (setq start (1- start)))
1633 (skip-chars-forward filename-chars))
1634 (error "No file found around point!"))
1635 ;; Return string.
1636 (expand-file-name (concat prefix (buffer-substring start (point)))))))
1637
1638 (defun dired-x-read-filename-at-point (prompt)
1639 "Return filename prompting with PROMPT with completion.
1640 If `current-prefix-arg' is non-nil, uses name at point as guess."
1641 (if current-prefix-arg
1642 (let ((guess (dired-filename-at-point)))
1643 (read-file-name prompt
1644 (file-name-directory guess)
1645 guess
1646 nil (file-name-nondirectory guess)))
1647 (read-file-name prompt default-directory)))
1648
1649 (define-obsolete-function-alias 'read-filename-at-point
1650 'dired-x-read-filename-at-point "24.1") ; is this even needed?
1651 \f
1652 ;;; BUG REPORTS
1653
1654 (define-obsolete-function-alias 'dired-x-submit-report 'report-emacs-bug "24.1")
1655
1656 \f
1657 ;; As Barry Warsaw would say: "This might be useful..."
1658 (provide 'dired-x)
1659
1660 ;; Local Variables:
1661 ;; byte-compile-dynamic: t
1662 ;; generated-autoload-file: "dired.el"
1663 ;; End:
1664
1665 ;;; dired-x.el ends here