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