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