Cosmetic change to last mm-url change
[emacs.git] / lisp / dired-x.el
blob83139057659997f315a604054d43677a36dde126
1 ;;; dired-x.el --- extra Dired functionality -*- lexical-binding:t -*-
3 ;; Copyright (C) 1993-1994, 1997, 2001-2016 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
7 ;; Lawrence R. Dodd <dodd@roebling.poly.edu>
8 ;; Maintainer: Romain Francoise <rfrancoise@gnu.org>
9 ;; Keywords: dired extensions files
10 ;; Package: emacs
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; This is based on Sebastian Kremer's excellent dired-x.el (Dired Extra),
30 ;; version 1.191, adapted for GNU Emacs. See the `dired-x' info pages.
32 ;; USAGE: In your ~/.emacs,
34 ;; (add-hook 'dired-load-hook
35 ;; (lambda ()
36 ;; (load "dired-x")
37 ;; ;; Set global variables here. For example:
38 ;; ;; (setq dired-guess-shell-gnutar "gtar")
39 ;; ))
40 ;; (add-hook 'dired-mode-hook
41 ;; (lambda ()
42 ;; ;; Set buffer-local variables here. For example:
43 ;; ;; (dired-omit-mode 1)
44 ;; ))
46 ;; At load time dired-x.el will install itself and bind some dired keys.
47 ;; Some dired.el and dired-aux.el functions have extra features if
48 ;; dired-x is loaded.
50 ;; User customization: M-x customize-group RET dired-x RET.
52 ;; *Please* see the `dired-x' info pages for more details.
55 ;;; Code:
57 ;; This is a no-op if dired-x is being loaded via `dired-load-hook',
58 ;; but maybe not if a dired-x function is being autoloaded.
59 (require 'dired)
61 ;;; User-defined variables.
63 (defgroup dired-x nil
64 "Extended directory editing (dired-x)."
65 :group 'dired)
67 (defgroup dired-keys nil
68 "Dired keys customizations."
69 :prefix "dired-"
70 :group 'dired-x)
72 (defcustom dired-bind-vm nil
73 "Non-nil means \"V\" runs `dired-vm', otherwise \"V\" runs `dired-rmail'.
74 RMAIL files in the old Babyl format (used before before Emacs 23.1)
75 contain \"-*- rmail -*-\" at the top, so `dired-find-file'
76 will run `rmail' on these files. New RMAIL files use the standard
77 mbox format, and so cannot be distinguished in this way."
78 :type 'boolean
79 :group 'dired-keys)
81 (defcustom dired-bind-jump t
82 "Non-nil means bind `dired-jump' to C-x C-j, otherwise do not.
83 Setting this variable directly after dired-x is loaded has no effect -
84 use \\[customize]."
85 :type 'boolean
86 :set (lambda (sym val)
87 (if (set sym val)
88 (progn
89 (define-key ctl-x-map "\C-j" 'dired-jump)
90 (define-key ctl-x-4-map "\C-j" 'dired-jump-other-window))
91 (if (eq 'dired-jump (lookup-key ctl-x-map "\C-j"))
92 (define-key ctl-x-map "\C-j" nil))
93 (if (eq 'dired-jump-other-window (lookup-key ctl-x-4-map "\C-j"))
94 (define-key ctl-x-4-map "\C-j" nil))))
95 :group 'dired-keys)
97 (defcustom dired-bind-man t
98 "Non-nil means bind `dired-man' to \"N\" in Dired, otherwise do not.
99 Setting this variable directly after dired-x is loaded has no effect -
100 use \\[customize]."
101 :type 'boolean
102 :set (lambda (sym val)
103 (if (set sym val)
104 (define-key dired-mode-map "N" 'dired-man)
105 (if (eq 'dired-man (lookup-key dired-mode-map "N"))
106 (define-key dired-mode-map "N" nil))))
107 :group 'dired-keys)
109 (defcustom dired-bind-info t
110 "Non-nil means bind `dired-info' to \"I\" in Dired, otherwise do not.
111 Setting this variable directly after dired-x is loaded has no effect -
112 use \\[customize]."
113 :type 'boolean
114 :set (lambda (sym val)
115 (if (set sym val)
116 (define-key dired-mode-map "I" 'dired-info)
117 (if (eq 'dired-info (lookup-key dired-mode-map "I"))
118 (define-key dired-mode-map "I" nil))))
119 :group 'dired-keys)
121 (defcustom dired-vm-read-only-folders nil
122 "If non-nil, \\[dired-vm] will visit all folders read-only.
123 If neither nil nor t, e.g. the symbol `if-file-read-only', only
124 files not writable by you are visited read-only."
125 :type '(choice (const :tag "off" nil)
126 (const :tag "on" t)
127 (other :tag "non-writable only" if-file-read-only))
128 :group 'dired-x)
130 (defcustom dired-omit-size-limit 30000
131 "Maximum size for the \"omitting\" feature.
132 If nil, there is no maximum size."
133 :type '(choice (const :tag "no maximum" nil) integer)
134 :group 'dired-x)
136 ;; For backward compatibility
137 (define-obsolete-variable-alias 'dired-omit-files-p 'dired-omit-mode "22.1")
138 (define-minor-mode dired-omit-mode
139 "Toggle omission of uninteresting files in Dired (Dired-Omit mode).
140 With a prefix argument ARG, enable Dired-Omit mode if ARG is
141 positive, and disable it otherwise. If called from Lisp, enable
142 the mode if ARG is omitted or nil.
144 Dired-Omit mode is a buffer-local minor mode. When enabled in a
145 Dired buffer, Dired does not list files whose filenames match
146 regexp `dired-omit-files', nor files ending with extensions in
147 `dired-omit-extensions'.
149 To enable omitting in every Dired buffer, you can put this in
150 your init file:
152 (add-hook \\='dired-mode-hook (lambda () (dired-omit-mode)))
154 See Info node `(dired-x) Omitting Variables' for more information."
155 :group 'dired-x
156 (if dired-omit-mode
157 ;; This will mention how many lines were omitted:
158 (let ((dired-omit-size-limit nil)) (dired-omit-expunge))
159 (revert-buffer)))
161 (put 'dired-omit-mode 'safe-local-variable 'booleanp)
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 "\C-x\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 Interactively, ask for EXTENSION.
338 Prefixed with one C-u, unmark files instead.
339 Prefixed with two C-u's, prompt for MARKER-CHAR and mark files with it."
340 (interactive
341 (let ((suffix
342 (read-string (format "%s extension: "
343 (if (equal current-prefix-arg '(4))
344 "UNmarking"
345 "Marking"))))
346 (marker
347 (pcase current-prefix-arg
348 ('(4) ?\s)
349 ('(16)
350 (let* ((dflt (char-to-string dired-marker-char))
351 (input (read-string
352 (format
353 "Marker character to use (default %s): " dflt)
354 nil nil dflt)))
355 (aref input 0)))
356 (_ dired-marker-char))))
357 (list suffix marker)))
358 (or (listp extension)
359 (setq extension (list extension)))
360 (dired-mark-files-regexp
361 (concat ".";; don't match names with nothing but an extension
362 "\\("
363 (mapconcat 'regexp-quote extension "\\|")
364 "\\)$")
365 marker-char))
367 (defun dired-flag-extension (extension)
368 "In Dired, flag all files with a certain EXTENSION for deletion.
369 A `.' is *not* automatically prepended to the string entered."
370 (interactive "sFlagging extension: ")
371 (dired-mark-extension extension dired-del-marker))
373 ;; Define some unpopular file extensions. Used for cleaning and omitting.
375 (defvar dired-patch-unclean-extensions
376 '(".rej" ".orig")
377 "List of extensions of dispensable files created by the `patch' program.")
379 (defvar dired-tex-unclean-extensions
380 '(".toc" ".log" ".aux");; these are already in completion-ignored-extensions
381 "List of extensions of dispensable files created by TeX.")
383 (defvar dired-latex-unclean-extensions
384 '(".idx" ".lof" ".lot" ".glo")
385 "List of extensions of dispensable files created by LaTeX.")
387 (defvar dired-bibtex-unclean-extensions
388 '(".blg" ".bbl")
389 "List of extensions of dispensable files created by BibTeX.")
391 (defvar dired-texinfo-unclean-extensions
392 '(".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
393 ".tp" ".tps" ".vr" ".vrs")
394 "List of extensions of dispensable files created by texinfo.")
396 (defun dired-clean-patch ()
397 "Flag dispensable files created by patch for deletion.
398 See variable `dired-patch-unclean-extensions'."
399 (interactive)
400 (dired-flag-extension dired-patch-unclean-extensions))
402 (defun dired-clean-tex ()
403 "Flag dispensable files created by [La]TeX etc. for deletion.
404 See variables `dired-tex-unclean-extensions',
405 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
406 `dired-texinfo-unclean-extensions'."
407 (interactive)
408 (dired-flag-extension (append dired-texinfo-unclean-extensions
409 dired-latex-unclean-extensions
410 dired-bibtex-unclean-extensions
411 dired-tex-unclean-extensions)))
413 (defun dired-very-clean-tex ()
414 "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
415 See variables `dired-texinfo-unclean-extensions',
416 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
417 `dired-texinfo-unclean-extensions'."
418 (interactive)
419 (dired-flag-extension (append dired-texinfo-unclean-extensions
420 dired-latex-unclean-extensions
421 dired-bibtex-unclean-extensions
422 dired-tex-unclean-extensions
423 (list ".dvi"))))
425 (defvar tar-superior-buffer)
426 ;;; JUMP.
428 ;;;###autoload
429 (defun dired-jump (&optional other-window file-name)
430 "Jump to Dired buffer corresponding to current buffer.
431 If in a file, Dired the current directory and move to file's line.
432 If in Dired already, pop up a level and goto old directory's line.
433 In case the proper Dired file line cannot be found, refresh the dired
434 buffer and try again.
435 When OTHER-WINDOW is non-nil, jump to Dired buffer in other window.
436 When FILE-NAME is non-nil, jump to its line in Dired.
437 Interactively with prefix argument, read FILE-NAME."
438 (interactive
439 (list nil (and current-prefix-arg
440 (read-file-name "Jump to Dired file: "))))
441 (if (bound-and-true-p tar-subfile-mode)
442 (switch-to-buffer tar-superior-buffer)
443 ;; Expand file-name before `dired-goto-file' call:
444 ;; `dired-goto-file' requires its argument to be an absolute
445 ;; file name; the result of `read-file-name' could be
446 ;; an abbreviated file name (Bug#24409).
447 (let* ((file (or (and file-name (expand-file-name file-name))
448 buffer-file-name))
449 (dir (if file (file-name-directory file) default-directory)))
450 (if (and (eq major-mode 'dired-mode) (null file-name))
451 (progn
452 (setq dir (dired-current-directory))
453 (dired-up-directory other-window)
454 (unless (dired-goto-file dir)
455 ;; refresh and try again
456 (dired-insert-subdir (file-name-directory dir))
457 (dired-goto-file dir)))
458 (if other-window
459 (dired-other-window dir)
460 (dired dir))
461 (if file
462 (or (dired-goto-file file)
463 ;; refresh and try again
464 (progn
465 (dired-insert-subdir (file-name-directory file))
466 (dired-goto-file file))
467 ;; Toggle omitting, if it is on, and try again.
468 (when dired-omit-mode
469 (dired-omit-mode)
470 (dired-goto-file file))))))))
472 ;;;###autoload
473 (defun dired-jump-other-window (&optional file-name)
474 "Like \\[dired-jump] (`dired-jump') but in other window."
475 (interactive
476 (list (and current-prefix-arg
477 (read-file-name "Jump to Dired file: "))))
478 (dired-jump t file-name))
480 ;;; OMITTING.
482 ;; Enhanced omitting of lines from directory listings.
483 ;; Marked files are never omitted.
485 ;; should probably get rid of this and always use 'no-dir.
486 ;; sk 28-Aug-1991 09:37
487 (defvar dired-omit-localp 'no-dir
488 "The LOCALP argument `dired-omit-expunge' passes to `dired-get-filename'.
489 If it is `no-dir', omitting is much faster, but you can only match
490 against the non-directory part of the file name. Set it to nil if you
491 need to match the entire file name.")
493 ;; \017=^O for Omit - other packages can chose other control characters.
494 (defvar dired-omit-marker-char ?\017
495 "Temporary marker used by Dired-Omit.
496 Should never be used as marker by the user or other packages.")
498 (defun dired-omit-startup ()
499 (or (assq 'dired-omit-mode minor-mode-alist)
500 (setq minor-mode-alist
501 (append '((dired-omit-mode
502 (:eval (if (eq major-mode 'dired-mode)
503 " Omit" ""))))
504 minor-mode-alist))))
506 (defun dired-mark-omitted ()
507 "Mark files matching `dired-omit-files' and `dired-omit-extensions'."
508 (interactive)
509 (let ((dired-omit-mode nil)) (revert-buffer)) ;; Show omitted files
510 (dired-mark-unmarked-files (dired-omit-regexp) nil nil dired-omit-localp))
512 (defcustom dired-omit-extensions
513 (append completion-ignored-extensions
514 dired-latex-unclean-extensions
515 dired-bibtex-unclean-extensions
516 dired-texinfo-unclean-extensions)
517 "If non-nil, a list of extensions (strings) to omit from Dired listings.
518 Defaults to elements of `completion-ignored-extensions',
519 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
520 `dired-texinfo-unclean-extensions'.
522 See interactive function `dired-omit-mode' (\\[dired-omit-mode]) and
523 variables `dired-omit-mode' and `dired-omit-files'."
524 :type '(repeat string)
525 :group 'dired-x)
527 (defun dired-omit-expunge (&optional regexp)
528 "Erases all unmarked files matching REGEXP.
529 Does nothing if global variable `dired-omit-mode' is nil, or if called
530 non-interactively and buffer is bigger than `dired-omit-size-limit'.
531 If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
532 filenames ending in `dired-omit-extensions'.
533 If REGEXP is the empty string, this function is a no-op.
535 This functions works by temporarily binding `dired-marker-char' to
536 `dired-omit-marker-char' and calling `dired-do-kill-lines'."
537 (interactive "sOmit files (regexp): ")
538 (if (and dired-omit-mode
539 (or (called-interactively-p 'interactive)
540 (not dired-omit-size-limit)
541 (< (buffer-size) dired-omit-size-limit)
542 (progn
543 (when dired-omit-verbose
544 (message "Not omitting: directory larger than %d characters."
545 dired-omit-size-limit))
546 (setq dired-omit-mode nil)
547 nil)))
548 (let ((omit-re (or regexp (dired-omit-regexp)))
549 (old-modified-p (buffer-modified-p))
550 count)
551 (or (string= omit-re "")
552 (let ((dired-marker-char dired-omit-marker-char))
553 (when dired-omit-verbose (message "Omitting..."))
554 (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp)
555 (progn
556 (setq count (dired-do-kill-lines
558 (if dired-omit-verbose "Omitted %d line%s." "")))
559 (force-mode-line-update))
560 (when dired-omit-verbose (message "(Nothing to omit)")))))
561 ;; Try to preserve modified state of buffer. So `%*' doesn't appear
562 ;; in mode-line of omitted buffers.
563 (set-buffer-modified-p (and old-modified-p
564 (save-excursion
565 (goto-char (point-min))
566 (re-search-forward dired-re-mark nil t))))
567 count)))
569 (defun dired-omit-regexp ()
570 (concat (if dired-omit-files (concat "\\(" dired-omit-files "\\)") "")
571 (if (and dired-omit-files dired-omit-extensions) "\\|" "")
572 (if dired-omit-extensions
573 (concat ".";; a non-extension part should exist
574 "\\("
575 (mapconcat 'regexp-quote dired-omit-extensions "\\|")
576 "\\)$")
577 "")))
579 ;; Returns t if any work was done, nil otherwise.
580 (defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp)
581 "Mark unmarked files matching REGEXP, displaying MSG.
582 REGEXP is matched against the entire file name. When called
583 interactively, prompt for REGEXP.
584 With prefix argument, unflag all those files.
585 Optional fourth argument LOCALP is as in `dired-get-filename'."
586 (interactive
587 (list (read-regexp
588 "Mark unmarked files matching regexp (default all): "
589 nil 'dired-regexp-history)
590 nil current-prefix-arg nil))
591 (let ((dired-marker-char (if unflag-p ?\s dired-marker-char)))
592 (dired-mark-if
593 (and
594 ;; not already marked
595 (looking-at-p " ")
596 ;; uninteresting
597 (let ((fn (dired-get-filename localp t)))
598 (and fn (string-match-p regexp fn))))
599 msg)))
602 ;;; VIRTUAL DIRED MODE.
604 ;; For browsing `ls -lR' listings in a dired-like fashion.
606 (defalias 'virtual-dired 'dired-virtual)
607 (defun dired-virtual (dirname &optional switches)
608 "Put this buffer into Virtual Dired mode.
610 In Virtual Dired mode, all commands that do not actually consult the
611 filesystem will work.
613 This is useful if you want to peruse and move around in an ls -lR
614 output file, for example one you got from an ftp server. With
615 ange-ftp, you can even Dired a directory containing an ls-lR file,
616 visit that file and turn on Virtual Dired mode. But don't try to save
617 this file, as dired-virtual indents the listing and thus changes the
618 buffer.
620 If you have save a Dired buffer in a file you can use \\[dired-virtual] to
621 resume it in a later session.
623 Type \\<dired-mode-map>\\[revert-buffer] \
624 in the Virtual Dired buffer and answer `y' to convert
625 the virtual to a real Dired buffer again. You don't have to do this, though:
626 you can relist single subdirs using \\[dired-do-redisplay]."
628 ;; DIRNAME is the top level directory of the buffer. It will become
629 ;; its `default-directory'. If nil, the old value of
630 ;; default-directory is used.
632 ;; Optional SWITCHES are the ls switches to use.
634 ;; Shell wildcards will be used if there already is a `wildcard'
635 ;; line in the buffer (thus it is a saved Dired buffer), but there
636 ;; is no other way to get wildcards. Insert a `wildcard' line by
637 ;; hand if you want them.
639 (interactive
640 (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
641 (goto-char (point-min))
642 (or (looking-at-p " ")
643 ;; if not already indented, do it now:
644 (indent-region (point-min) (point-max) 2))
645 (or dirname (setq dirname default-directory))
646 (setq dirname (expand-file-name (file-name-as-directory dirname)))
647 (setq default-directory dirname) ; contains no wildcards
648 (let ((wildcard (save-excursion
649 (goto-char (point-min))
650 (forward-line 1)
651 (and (looking-at "^ wildcard ")
652 (buffer-substring (match-end 0)
653 (line-end-position))))))
654 (if wildcard
655 (setq dirname (expand-file-name wildcard default-directory))))
656 ;; If raw ls listing (not a saved old dired buffer), give it a
657 ;; decent subdir headerline:
658 (goto-char (point-min))
659 (or (looking-at-p dired-subdir-regexp)
660 (insert " "
661 (directory-file-name (file-name-directory default-directory))
662 ":\n"))
663 (dired-mode dirname (or switches dired-listing-switches))
664 (setq mode-name "Virtual Dired"
665 revert-buffer-function 'dired-virtual-revert)
666 (set (make-local-variable 'dired-subdir-alist) nil)
667 (dired-build-subdir-alist)
668 (goto-char (point-min))
669 (dired-initial-position dirname))
671 (defun dired-virtual-guess-dir ()
672 "Guess and return appropriate working directory of this buffer.
673 The buffer is assumed to be in Dired or ls -lR format. The guess is
674 based upon buffer contents. If nothing could be guessed, returns
675 nil."
677 (let ((regexp "^\\( \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
678 (subexpr 2))
679 (goto-char (point-min))
680 (cond ((looking-at regexp)
681 ;; If a saved dired buffer, look to which dir and
682 ;; perhaps wildcard it belongs:
683 (let ((dir (buffer-substring (match-beginning subexpr)
684 (match-end subexpr))))
685 (file-name-as-directory dir)))
686 ;; Else no match for headerline found. It's a raw ls listing.
687 ;; In raw ls listings the directory does not have a headerline
688 ;; try parent of first subdir, if any
689 ((re-search-forward regexp nil t)
690 (file-name-directory
691 (directory-file-name
692 (file-name-as-directory
693 (buffer-substring (match-beginning subexpr)
694 (match-end subexpr))))))
695 (t ; if all else fails
696 nil))))
699 (defun dired-virtual-revert (&optional _arg _noconfirm)
700 (if (not
701 (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
702 (error "Cannot revert a Virtual Dired buffer")
703 (setq mode-name "Dired"
704 revert-buffer-function 'dired-revert)
705 (revert-buffer)))
707 ;; A zero-arg version of dired-virtual.
708 (defun dired-virtual-mode ()
709 "Put current buffer into Virtual Dired mode (see `dired-virtual').
710 Useful on `magic-mode-alist' with the regexp
712 \"^ \\\\(/[^ /]+\\\\)+/?:$\"
714 to put saved Dired buffers automatically into Virtual Dired mode.
716 Also useful for `auto-mode-alist' like this:
718 (add-to-list \\='auto-mode-alist
719 \\='(\"[^/]\\\\.dired\\\\\\='\" . dired-virtual-mode))"
720 (interactive)
721 (dired-virtual (dired-virtual-guess-dir)))
724 ;;; SMART SHELL.
726 ;; An Emacs buffer can have but one working directory, stored in the
727 ;; buffer-local variable `default-directory'. A Dired buffer may have
728 ;; several subdirectories inserted, but still has but one working directory:
729 ;; that of the top level Dired directory in that buffer. For some commands
730 ;; it is appropriate that they use the current Dired directory instead of
731 ;; `default-directory', e.g., `find-file' and `compile'. This is a general
732 ;; mechanism is provided for special handling of the working directory in
733 ;; special major modes.
735 (define-obsolete-variable-alias 'default-directory-alist
736 'dired-default-directory-alist "24.1")
738 ;; It's easier to add to this alist than redefine function
739 ;; default-directory while keeping the old information.
740 (defconst dired-default-directory-alist
741 '((dired-mode . (if (fboundp 'dired-current-directory)
742 (dired-current-directory)
743 default-directory)))
744 "Alist of major modes and their opinion on `default-directory'.
745 Each element has the form (MAJOR . EXPRESSION).
746 The function `dired-default-directory' evaluates EXPRESSION to
747 determine a default directory.")
749 (put 'dired-default-directory-alist 'risky-local-variable t) ; gets eval'd
750 (make-obsolete-variable 'dired-default-directory-alist
751 "this feature is due to be removed." "24.1")
753 (defun dired-default-directory ()
754 "Return the `dired-default-directory-alist' entry for the current major-mode.
755 If none, return `default-directory'."
756 ;; It looks like this was intended to be something of a "general"
757 ;; feature, but it only ever seems to have been used in
758 ;; dired-smart-shell-command, and doesn't seem worth keeping around.
759 (declare (obsolete nil "24.1"))
760 (or (eval (cdr (assq major-mode dired-default-directory-alist)))
761 default-directory))
763 (defun dired-smart-shell-command (command &optional output-buffer error-buffer)
764 "Like function `shell-command', but in the current Virtual Dired directory."
765 (interactive
766 (list
767 (read-shell-command "Shell command: " nil nil
768 (cond
769 (buffer-file-name (file-relative-name buffer-file-name))
770 ((eq major-mode 'dired-mode) (dired-get-filename t t))))
771 current-prefix-arg
772 shell-command-default-error-buffer))
773 (let ((default-directory (or (and (eq major-mode 'dired-mode)
774 (dired-current-directory))
775 default-directory)))
776 (shell-command command output-buffer error-buffer)))
779 ;;; LOCAL VARIABLES FOR DIRED BUFFERS.
781 ;; Brief Description (This feature is obsolete as of Emacs 24.1)
783 ;; * `dired-extra-startup' is part of the `dired-mode-hook'.
785 ;; * `dired-extra-startup' calls `dired-hack-local-variables'
787 ;; * `dired-hack-local-variables' checks the value of
788 ;; `dired-local-variables-file'
790 ;; * Check if `dired-local-variables-file' is a non-nil string and is a
791 ;; filename found in the directory of the Dired Buffer being created.
793 ;; * If `dired-local-variables-file' satisfies the above, then temporarily
794 ;; include it in the Dired Buffer at the bottom.
796 ;; * Set `enable-local-variables' temporarily to the user variable
797 ;; `dired-enable-local-variables' and run `hack-local-variables' on the
798 ;; Dired Buffer.
800 (defcustom dired-local-variables-file (convert-standard-filename ".dired")
801 "Filename, as string, containing local Dired buffer variables to be hacked.
802 If this file found in current directory, then it will be inserted into dired
803 buffer and `hack-local-variables' will be run. See Info node
804 `(emacs)File Variables' for more information on local variables.
805 See also `dired-enable-local-variables'."
806 :type 'file
807 :group 'dired)
809 (make-obsolete-variable 'dired-local-variables-file 'dir-locals-file "24.1")
811 (defun dired-hack-local-variables ()
812 "Evaluate local variables in `dired-local-variables-file' for Dired buffer."
813 (declare (obsolete hack-dir-local-variables-non-file-buffer "24.1"))
814 (and (stringp dired-local-variables-file)
815 (file-exists-p dired-local-variables-file)
816 (let ((opoint (point-max))
817 (inhibit-read-only t)
818 ;; In case user has `enable-local-variables' set to nil we
819 ;; override it locally with dired's variable.
820 (enable-local-variables dired-enable-local-variables))
821 ;; Insert 'em.
822 (save-excursion
823 (goto-char opoint)
824 (insert "\^L\n")
825 (insert-file-contents dired-local-variables-file))
826 ;; Hack 'em.
827 (unwind-protect
828 (let ((buffer-file-name dired-local-variables-file))
829 (hack-local-variables))
830 ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
831 (delete-region opoint (point-max)))
832 ;; Make sure that the mode line shows the proper information.
833 (dired-sort-set-mode-line))))
835 ;; Does not seem worth a dedicated command.
836 ;; See the more general features in files-x.el.
837 (defun dired-omit-here-always ()
838 "Create `dir-locals-file' setting `dired-omit-mode' to t in `dired-mode'.
839 If in a Dired buffer, reverts it."
840 (declare (obsolete add-dir-local-variable "24.1"))
841 (interactive)
842 (if (file-exists-p dired-local-variables-file)
843 (error "Old-style dired-local-variables-file `./%s' found;
844 replace it with a dir-locals-file `./%s'"
845 dired-local-variables-file
846 dir-locals-file))
847 (if (file-exists-p dir-locals-file)
848 (message "File `./%s' already exists." dir-locals-file)
849 (add-dir-local-variable 'dired-mode 'subdirs nil)
850 (add-dir-local-variable 'dired-mode 'dired-omit-mode t)
851 ;; Run extra-hooks and revert directory.
852 (when (derived-mode-p 'dired-mode)
853 (hack-dir-local-variables-non-file-buffer)
854 (dired-extra-startup)
855 (dired-revert))))
858 ;;; GUESS SHELL COMMAND.
860 ;; Brief Description:
862 ;; * `dired-do-shell-command' is bound to `!' by dired.el.
864 ;; * `dired-guess-shell-command' provides smarter defaults for
865 ;;; dired-aux.el's `dired-read-shell-command'.
867 ;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
868 ;;; marked files.
870 ;; * Parse `dired-guess-shell-alist-user' and
871 ;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
872 ;;; that matches the first file in the file list.
874 ;; * If the REGEXP matches all the entries of the file list then evaluate
875 ;;; COMMAND, which is either a string or a Lisp expression returning a
876 ;;; string. COMMAND may be a list of commands.
878 ;; * Return this command to `dired-guess-shell-command' which prompts user
879 ;;; with it. The list of commands is put into the list of default values.
880 ;;; If a command is used successfully then it is stored permanently in
881 ;;; `dired-shell-command-history'.
883 ;; Guess what shell command to apply to a file.
884 (defvar dired-shell-command-history nil
885 "History list for commands that read dired-shell commands.")
887 ;; Default list of shell commands.
889 ;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
890 ;; install GNU zip's version of zcat.
892 (autoload 'Man-support-local-filenames "man")
894 (defvar dired-guess-shell-alist-default
895 (list
896 (list "\\.tar\\'"
897 '(if dired-guess-shell-gnutar
898 (concat dired-guess-shell-gnutar " xvf")
899 "tar xvf")
900 ;; Extract files into a separate subdirectory
901 '(if dired-guess-shell-gnutar
902 (concat "mkdir " (file-name-sans-extension file)
903 "; " dired-guess-shell-gnutar " -C "
904 (file-name-sans-extension file) " -xvf")
905 (concat "mkdir " (file-name-sans-extension file)
906 "; tar -C " (file-name-sans-extension file) " -xvf"))
907 ;; List archive contents.
908 '(if dired-guess-shell-gnutar
909 (concat dired-guess-shell-gnutar " tvf")
910 "tar tvf"))
912 ;; REGEXPS for compressed archives must come before the .Z rule to
913 ;; be recognized:
914 (list "\\.tar\\.Z\\'"
915 ;; Untar it.
916 '(if dired-guess-shell-gnutar
917 (concat dired-guess-shell-gnutar " zxvf")
918 (concat "zcat * | tar xvf -"))
919 ;; Optional conversion to gzip format.
920 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
921 " " dired-guess-shell-znew-switches))
923 ;; gzip'ed archives
924 (list "\\.t\\(ar\\.\\)?gz\\'"
925 '(if dired-guess-shell-gnutar
926 (concat dired-guess-shell-gnutar " zxvf")
927 (concat "gunzip -qc * | tar xvf -"))
928 ;; Extract files into a separate subdirectory
929 '(if dired-guess-shell-gnutar
930 (concat "mkdir " (file-name-sans-extension file)
931 "; " dired-guess-shell-gnutar " -C "
932 (file-name-sans-extension file) " -zxvf")
933 (concat "mkdir " (file-name-sans-extension file)
934 "; gunzip -qc * | tar -C "
935 (file-name-sans-extension file) " -xvf -"))
936 ;; Optional decompression.
937 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" ""))
938 ;; List archive contents.
939 '(if dired-guess-shell-gnutar
940 (concat dired-guess-shell-gnutar " ztvf")
941 (concat "gunzip -qc * | tar tvf -")))
943 ;; bzip2'ed archives
944 (list "\\.t\\(ar\\.bz2\\|bz\\)\\'"
945 "bunzip2 -c * | tar xvf -"
946 ;; Extract files into a separate subdirectory
947 '(concat "mkdir " (file-name-sans-extension file)
948 "; bunzip2 -c * | tar -C "
949 (file-name-sans-extension file) " -xvf -")
950 ;; Optional decompression.
951 "bunzip2")
953 ;; xz'ed archives
954 (list "\\.t\\(ar\\.\\)?xz\\'"
955 "unxz -c * | tar xvf -"
956 ;; Extract files into a separate subdirectory
957 '(concat "mkdir " (file-name-sans-extension file)
958 "; unxz -c * | tar -C "
959 (file-name-sans-extension file) " -xvf -")
960 ;; Optional decompression.
961 "unxz")
963 '("\\.shar\\.Z\\'" "zcat * | unshar")
964 '("\\.shar\\.g?z\\'" "gunzip -qc * | unshar")
966 '("\\.e?ps\\'" "ghostview" "xloadimage" "lpr")
967 (list "\\.e?ps\\.g?z\\'" "gunzip -qc * | ghostview -"
968 ;; Optional decompression.
969 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
970 (list "\\.e?ps\\.Z\\'" "zcat * | ghostview -"
971 ;; Optional conversion to gzip format.
972 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
973 " " dired-guess-shell-znew-switches))
975 '("\\.patch\\'" "cat * | patch")
976 (list "\\.patch\\.g?z\\'" "gunzip -qc * | patch"
977 ;; Optional decompression.
978 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
979 (list "\\.patch\\.Z\\'" "zcat * | patch"
980 ;; Optional conversion to gzip format.
981 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
982 " " dired-guess-shell-znew-switches))
984 ;; The following four extensions are useful with dired-man ("N" key)
985 ;; FIXME "man ./" does not work with dired-do-shell-command,
986 ;; because there seems to be no way for us to modify the filename,
987 ;; only the command. Hmph. `dired-man' works though.
988 (list "\\.\\(?:[0-9]\\|man\\)\\'"
989 '(let ((loc (Man-support-local-filenames)))
990 (cond ((eq loc 'man-db) "man -l")
991 ((eq loc 'man) "man ./")
993 "cat * | tbl | nroff -man -h | col -b"))))
994 (list "\\.\\(?:[0-9]\\|man\\)\\.g?z\\'"
995 '(let ((loc (Man-support-local-filenames)))
996 (cond ((eq loc 'man-db)
997 "man -l")
998 ((eq loc 'man)
999 "man ./")
1000 (t "gunzip -qc * | tbl | nroff -man -h | col -b")))
1001 ;; Optional decompression.
1002 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1003 (list "\\.[0-9]\\.Z\\'"
1004 '(let ((loc (Man-support-local-filenames)))
1005 (cond ((eq loc 'man-db) "man -l")
1006 ((eq loc 'man) "man ./")
1007 (t "zcat * | tbl | nroff -man -h | col -b")))
1008 ;; Optional conversion to gzip format.
1009 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1010 " " dired-guess-shell-znew-switches))
1011 '("\\.pod\\'" "perldoc" "pod2man * | nroff -man")
1013 '("\\.dvi\\'" "xdvi" "dvips") ; preview and printing
1014 '("\\.au\\'" "play") ; play Sun audiofiles
1015 '("\\.mpe?g\\'\\|\\.avi\\'" "xine -p")
1016 '("\\.ogg\\'" "ogg123")
1017 '("\\.mp3\\'" "mpg123")
1018 '("\\.wav\\'" "play")
1019 '("\\.uu\\'" "uudecode") ; for uudecoded files
1020 '("\\.hqx\\'" "mcvert")
1021 '("\\.sh\\'" "sh") ; execute shell scripts
1022 '("\\.xbm\\'" "bitmap") ; view X11 bitmaps
1023 '("\\.gp\\'" "gnuplot")
1024 '("\\.p[bgpn]m\\'" "xloadimage")
1025 '("\\.gif\\'" "xloadimage") ; view gif pictures
1026 '("\\.tif\\'" "xloadimage")
1027 '("\\.png\\'" "display") ; xloadimage 4.1 doesn't grok PNG
1028 '("\\.jpe?g\\'" "xloadimage")
1029 '("\\.fig\\'" "xfig") ; edit fig pictures
1030 '("\\.out\\'" "xgraph") ; for plotting purposes.
1031 '("\\.tex\\'" "latex" "tex")
1032 '("\\.texi\\(nfo\\)?\\'" "makeinfo" "texi2dvi")
1033 '("\\.pdf\\'" "xpdf")
1034 '("\\.doc\\'" "antiword" "strings")
1035 '("\\.rpm\\'" "rpm -qilp" "rpm -ivh")
1036 '("\\.dia\\'" "dia")
1037 '("\\.mgp\\'" "mgp")
1039 ;; Some other popular archivers.
1040 (list "\\.zip\\'" "unzip" "unzip -l"
1041 ;; Extract files into a separate subdirectory
1042 '(concat "unzip" (if dired-guess-shell-gzip-quiet " -q")
1043 " -d " (file-name-sans-extension file)))
1044 '("\\.zoo\\'" "zoo x//")
1045 '("\\.lzh\\'" "lharc x")
1046 '("\\.arc\\'" "arc x")
1047 '("\\.shar\\'" "unshar")
1048 '("\\.rar\\'" "unrar x")
1049 '("\\.7z\\'" "7z x")
1051 ;; Compression.
1052 (list "\\.g?z\\'" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1053 (list "\\.dz\\'" "dictunzip")
1054 (list "\\.bz2\\'" "bunzip2")
1055 (list "\\.xz\\'" "unxz")
1056 (list "\\.Z\\'" "uncompress"
1057 ;; Optional conversion to gzip format.
1058 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1059 " " dired-guess-shell-znew-switches))
1061 '("\\.sign?\\'" "gpg --verify"))
1063 "Default alist used for shell command guessing.
1064 See `dired-guess-shell-alist-user'.")
1066 (defcustom dired-guess-shell-alist-user nil
1067 "User-defined alist of rules for suggested commands.
1068 These rules take precedence over the predefined rules in the variable
1069 `dired-guess-shell-alist-default' (to which they are prepended).
1071 Each element of this list looks like
1073 (REGEXP COMMAND...)
1075 where each COMMAND can either be a string or a Lisp expression that evaluates
1076 to a string. This expression can access the file name as the variable `file'.
1077 If several COMMANDs are given, the first one will be the default
1078 and the rest will be added temporarily to the history and can be retrieved
1079 with \\[previous-history-element] (M-p) .
1081 The variable `dired-guess-shell-case-fold-search' controls whether
1082 REGEXP is matched case-sensitively.
1084 You can set this variable in your ~/.emacs. For example, to add rules for
1085 `.foo' and `.bar' files, write
1087 (setq dired-guess-shell-alist-user
1088 '((\"\\\\.foo\\\\'\" \"FOO-COMMAND\")
1089 (\"\\\\.bar\\\\'\"
1090 (if condition
1091 \"BAR-COMMAND-1\"
1092 \"BAR-COMMAND-2\"))))"
1093 :group 'dired-x
1094 :type '(alist :key-type regexp :value-type (repeat sexp)))
1096 (defcustom dired-guess-shell-case-fold-search t
1097 "If non-nil, `dired-guess-shell-alist-default' and
1098 `dired-guess-shell-alist-user' are matched case-insensitively."
1099 :group 'dired-x
1100 :type 'boolean)
1102 (defun dired-guess-default (files)
1103 "Return a shell command, or a list of commands, appropriate for FILES.
1104 See `dired-guess-shell-alist-user'."
1106 (let* ((case-fold-search dired-guess-shell-case-fold-search)
1107 ;; Prepend the user's alist to the default alist.
1108 (alist (append dired-guess-shell-alist-user
1109 dired-guess-shell-alist-default))
1110 (file (car files))
1111 (flist (cdr files))
1112 elt regexp cmds)
1114 ;; Find the first match in the alist for first file in FILES.
1115 (while alist
1116 (setq elt (car alist)
1117 regexp (car elt)
1118 alist (cdr alist))
1119 (if (string-match-p regexp file)
1120 (setq cmds (cdr elt)
1121 alist nil)))
1123 ;; If more than one file, see if all of FILES match regular expression.
1124 (while (and flist
1125 (string-match-p regexp (car flist)))
1126 (setq flist (cdr flist)))
1128 ;; If flist is still non-nil, then do not guess since this means that not
1129 ;; all the files in FILES were matched by the regexp.
1130 (setq cmds (and (not flist) cmds))
1132 ;; Return commands or nil if flist is still non-nil.
1133 ;; Evaluate the commands in order that any logical testing will be done.
1134 (if (cdr cmds)
1135 (delete-dups (mapcar (lambda (cmd) (eval cmd `((file . ,file)))) cmds))
1136 (eval (car cmds) `((file . ,file)))))) ; single command
1138 (defun dired-guess-shell-command (prompt files)
1139 "Ask user with PROMPT for a shell command, guessing a default from FILES."
1140 (let ((default (dired-guess-default files))
1141 default-list val)
1142 (if (null default)
1143 ;; Nothing to guess
1144 (read-shell-command prompt nil 'dired-shell-command-history)
1145 (setq prompt (replace-regexp-in-string ": $" " " prompt))
1146 (if (listp default)
1147 ;; More than one guess
1148 (setq default-list default
1149 default (car default)
1150 prompt (concat
1151 prompt
1152 (format "{%d guesses} " (length default-list))))
1153 ;; Just one guess
1154 (setq default-list (list default)))
1155 ;; Put the first guess in the prompt but not in the initial value.
1156 (setq prompt (concat prompt (format "[%s]: " default)))
1157 ;; All guesses can be retrieved with M-n
1158 (setq val (read-shell-command prompt nil
1159 'dired-shell-command-history
1160 default-list))
1161 ;; If we got a return, then return default.
1162 (if (equal val "") default val))))
1165 ;;; RELATIVE SYMBOLIC LINKS.
1167 (declare-function make-symbolic-link "fileio.c")
1169 (defvar dired-keep-marker-relsymlink ?S
1170 "See variable `dired-keep-marker-move'.")
1172 (defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
1173 "Make a symbolic link (pointing to FILE1) in FILE2.
1174 The link is relative (if possible), for example
1176 \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
1178 results in
1180 \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
1181 (interactive "FRelSymLink: \nFRelSymLink %s: \np")
1182 (let (name1 name2 len1 len2 (index 0) sub)
1183 (setq file1 (expand-file-name file1)
1184 file2 (expand-file-name file2)
1185 len1 (length file1)
1186 len2 (length file2))
1187 ;; Find common initial file name components:
1188 (let (next)
1189 (while (and (setq next (string-match "/" file1 index))
1190 (< (setq next (1+ next)) (min len1 len2))
1191 ;; For the comparison, both substrings must end in
1192 ;; `/', so NEXT is *one plus* the result of the
1193 ;; string-match.
1194 ;; E.g., consider the case of linking "/tmp/a/abc"
1195 ;; to "/tmp/abc" erroneously giving "/tmp/a" instead
1196 ;; of "/tmp/" as common initial component
1197 (string-equal (substring file1 0 next)
1198 (substring file2 0 next)))
1199 (setq index next))
1200 (setq name2 file2
1201 sub (substring file1 0 index)
1202 name1 (substring file1 index)))
1203 (if (string-equal sub "/")
1204 ;; No common initial file name found
1205 (setq name1 file1)
1206 ;; Else they have a common parent directory
1207 (let ((tem (substring file2 index))
1208 (start 0)
1209 (count 0))
1210 ;; Count number of slashes we must compensate for ...
1211 (while (setq start (string-match "/" tem start))
1212 (setq count (1+ count)
1213 start (1+ start)))
1214 ;; ... and prepend a "../" for each slash found:
1215 (dotimes (_ count)
1216 (setq name1 (concat "../" name1)))))
1217 (make-symbolic-link
1218 (directory-file-name name1) ; must not link to foo/
1219 ; (trailing slash!)
1220 name2 ok-if-already-exists)))
1222 (autoload 'dired-do-create-files "dired-aux")
1224 ;;;###autoload
1225 (defun dired-do-relsymlink (&optional arg)
1226 "Relative symlink all marked (or next ARG) files into a directory.
1227 Otherwise make a relative symbolic link to the current file.
1228 This creates relative symbolic links like
1230 foo -> ../bar/foo
1232 not absolute ones like
1234 foo -> /ugly/file/name/that/may/change/any/day/bar/foo
1236 For absolute symlinks, use \\[dired-do-symlink]."
1237 (interactive "P")
1238 (dired-do-create-files 'relsymlink #'dired-make-relative-symlink
1239 "RelSymLink" arg dired-keep-marker-relsymlink))
1241 (autoload 'dired-mark-read-regexp "dired-aux")
1242 (autoload 'dired-do-create-files-regexp "dired-aux")
1244 (defun dired-do-relsymlink-regexp (regexp newname &optional arg whole-name)
1245 "RelSymlink all marked files containing REGEXP to NEWNAME.
1246 See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
1247 for more info."
1248 (interactive (dired-mark-read-regexp "RelSymLink"))
1249 (dired-do-create-files-regexp
1250 #'dired-make-relative-symlink
1251 "RelSymLink" arg regexp newname whole-name dired-keep-marker-relsymlink))
1254 ;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
1256 ;; Brief Description:
1258 ;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
1260 ;; * Use `dired-get-marked-files' to collect the marked files in the current
1261 ;;; Dired Buffer into a list of filenames `FILE-LIST'.
1263 ;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
1264 ;;; `dired-do-find-marked-files''s prefix argument NOSELECT.
1266 ;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
1267 ;;; list each time.
1269 ;; * If NOSELECT is non-nil then just run `find-file-noselect' on each
1270 ;;; element of FILE-LIST.
1272 ;; * If NOSELECT is nil then calculate the `size' of the window for each file
1273 ;;; by dividing the `window-height' by length of FILE-LIST. Thus, `size' is
1274 ;;; cognizant of the window-configuration.
1276 ;; * If `size' is too small abort, otherwise run `find-file' on each element
1277 ;;; of FILE-LIST giving each a window of height `size'.
1279 (defun dired-do-find-marked-files (&optional noselect)
1280 "Find all marked files displaying all of them simultaneously.
1281 With optional NOSELECT just find files but do not select them.
1283 The current window is split across all files marked, as evenly as possible.
1284 Remaining lines go to bottom-most window. The number of files that can be
1285 displayed this way is restricted by the height of the current window and
1286 `window-min-height'.
1288 To keep Dired buffer displayed, type \\[split-window-below] first.
1289 To display just marked files, type \\[delete-other-windows] first."
1290 (interactive "P")
1291 (dired-simultaneous-find-file (dired-get-marked-files) noselect))
1293 (defun dired-simultaneous-find-file (file-list noselect)
1294 "Visit all files in FILE-LIST and display them simultaneously.
1295 The current window is split across all files in FILE-LIST, as evenly as
1296 possible. Remaining lines go to the bottom-most window. The number of
1297 files that can be displayed this way is restricted by the height of the
1298 current window and the variable `window-min-height'. With non-nil
1299 NOSELECT the files are merely found but not selected."
1300 ;; We don't make this function interactive because it is usually too clumsy
1301 ;; to specify FILE-LIST interactively unless via dired.
1302 (let (size)
1303 (if noselect
1304 ;; Do not select the buffer.
1305 (find-file-noselect (car file-list))
1306 ;; We will have to select the buffer. Calculate and check window size.
1307 (setq size (/ (window-height) (length file-list)))
1308 (or (<= window-min-height size)
1309 (error "Too many files to visit simultaneously. Try C-u prefix"))
1310 (find-file (car file-list)))
1311 ;; Decrement.
1312 (dolist (file (cdr file-list))
1313 (if noselect
1314 ;; Do not select the buffer.
1315 (find-file-noselect file)
1316 ;; Vertically split off a window of desired size. Upper window will
1317 ;; have SIZE lines. Select lower (larger) window. We split it again.
1318 (select-window (split-window nil size))
1319 (find-file file)))))
1322 ;;; MISCELLANEOUS COMMANDS.
1324 ;; Run man on files.
1326 (declare-function Man-getpage-in-background "man" (topic))
1328 (defvar manual-program) ; from man.el
1330 (defun dired-man ()
1331 "Run `man' on this file."
1332 ;; Used also to say: "Display old buffer if buffer name matches filename."
1333 ;; but I have no idea what that means.
1334 (interactive)
1335 (require 'man)
1336 (let* ((file (dired-get-filename))
1337 (manual-program (replace-regexp-in-string "\\*" "%s"
1338 (dired-guess-shell-command
1339 "Man command: " (list file)))))
1340 (Man-getpage-in-background file)))
1342 ;; Run Info on files.
1344 (defun dired-info ()
1345 "Run `info' on this file."
1346 (interactive)
1347 (info (dired-get-filename)))
1349 ;; Run mail on mail folders.
1351 (declare-function vm-visit-folder "ext:vm" (folder &optional read-only))
1352 (defvar vm-folder-directory)
1354 (defun dired-vm (&optional read-only)
1355 "Run VM on this file.
1356 With optional prefix argument, visits the folder read-only.
1357 Otherwise obeys the value of `dired-vm-read-only-folders'."
1358 (interactive "P")
1359 (let ((dir (dired-current-directory))
1360 (fil (dired-get-filename)))
1361 (vm-visit-folder fil (or read-only
1362 (eq t dired-vm-read-only-folders)
1363 (and dired-vm-read-only-folders
1364 (not (file-writable-p fil)))))
1365 ;; So that pressing `v' inside VM does prompt within current directory:
1366 (set (make-local-variable 'vm-folder-directory) dir)))
1368 (defun dired-rmail ()
1369 "Run RMAIL on this file."
1370 (interactive)
1371 (rmail (dired-get-filename)))
1373 (defun dired-do-run-mail ()
1374 "Visit the current file as a mailbox, using VM or RMAIL.
1375 Prompt for confirmation first; if the user says yes, call
1376 `dired-vm' if `dired-bind-vm' is non-nil, `dired-rmail'
1377 otherwise."
1378 (interactive)
1379 (let ((file (dired-get-filename t)))
1380 (if dired-bind-vm
1381 (if (y-or-n-p (format-message
1382 "Visit `%s' as a mail folder with VM?" file))
1383 (dired-vm))
1384 ;; Read mail folder using rmail.
1385 (if (y-or-n-p (format-message
1386 "Visit `%s' as a mailbox with RMAIL?" file))
1387 (dired-rmail)))))
1390 ;;; MISCELLANEOUS INTERNAL FUNCTIONS.
1392 ;; This should be a builtin
1393 (defun dired-buffer-more-recently-used-p (buffer1 buffer2)
1394 "Return t if BUFFER1 is more recently used than BUFFER2.
1395 Considers buffers closer to the car of `buffer-list' to be more recent."
1396 (and (not (equal buffer1 buffer2))
1397 (memq buffer1 (buffer-list))
1398 (not (memq buffer1 (memq buffer2 (buffer-list))))))
1400 ;; Same thing as `dired-buffers-for-dir' of dired.el? - lrd 11/23/93
1401 ;; (defun dired-buffers-for-dir-exact (dir)
1402 ;; ;; Return a list of buffers that dired DIR (a directory or wildcard)
1403 ;; ;; at top level, or as subdirectory.
1404 ;; ;; Top level matches must match the wildcard part too, if any.
1405 ;; ;; The list is in reverse order of buffer creation, most recent last.
1406 ;; ;; As a side effect, killed dired buffers for DIR are removed from
1407 ;; ;; dired-buffers.
1408 ;; (let ((alist dired-buffers) result elt)
1409 ;; (while alist
1410 ;; (setq elt (car alist)
1411 ;; alist (cdr alist))
1412 ;; (let ((buf (cdr elt)))
1413 ;; (if (buffer-name buf)
1414 ;; ;; Top level must match exactly against dired-directory in
1415 ;; ;; case one of them is a wildcard.
1416 ;; (if (or (equal dir (with-current-buffer buf dired-directory))
1417 ;; (assoc dir (with-current-buffer buf dired-subdir-alist)))
1418 ;; (setq result (cons buf result)))
1419 ;; ;; else buffer is killed - clean up:
1420 ;; (setq dired-buffers (delq elt dired-buffers)))))
1421 ;; result))
1424 ;; Needed if ls -lh is supported and also for GNU ls -ls.
1425 (defun dired-x--string-to-number (str)
1426 "Like `string-to-number' but recognize a trailing unit prefix.
1427 For example, 2K is expanded to 2048.0. The caller should make
1428 sure that a trailing letter in STR is one of BKkMGTPEZY."
1429 (let* ((val (string-to-number str))
1430 (u (unless (zerop val)
1431 (aref str (1- (length str))))))
1432 (when (and u (> u ?9))
1433 (when (= u ?k)
1434 (setq u ?K))
1435 (let ((units '(?B ?K ?M ?G ?T ?P ?E ?Z ?Y)))
1436 (while (and units (/= (pop units) u))
1437 (setq val (* 1024.0 val)))))
1438 val))
1440 ;; Does anyone use this? - lrd 6/29/93.
1441 ;; Apparently people do use it. - lrd 12/22/97.
1443 (defun dired-mark-sexp (predicate &optional unflag-p)
1444 "Mark files for which PREDICATE returns non-nil.
1445 With a prefix arg, unmark or unflag those files instead.
1447 PREDICATE is a lisp expression that can refer to the following symbols:
1449 inode [integer] the inode of the file (only for ls -i output)
1450 s [integer] the size of the file for ls -s output
1451 (usually in blocks or, with -k, in KByte)
1452 mode [string] file permission bits, e.g. \"-rw-r--r--\"
1453 nlink [integer] number of links to file
1454 uid [string] owner
1455 gid [string] group (If the gid is not displayed by ls,
1456 this will still be set (to the same as uid))
1457 size [integer] file size in bytes
1458 time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
1459 name [string] the name of the file
1460 sym [string] if file is a symbolic link, the linked-to name, else \"\"
1462 For example, use
1464 (equal 0 size)
1466 to mark all zero length files.
1468 There's an ambiguity when a single integer not followed by a unit
1469 prefix precedes the file mode: It is then parsed as inode number
1470 and not as block size (this always works for GNU coreutils ls).
1472 Another limitation is that the uid field is needed for the
1473 function to work correctly. In particular, the field is not
1474 present for some values of `ls-lisp-emulation'.
1476 This function operates only on the buffer content and does not
1477 refer at all to the underlying file system. Contrast this with
1478 `find-dired', which might be preferable for the task at hand."
1479 ;; Using sym="" instead of nil avoids the trap of
1480 ;; (string-match "foo" sym) into which a user would soon fall.
1481 ;; Give `equal' instead of `=' in the example, as this works on
1482 ;; integers and strings.
1483 (interactive
1484 (list (read--expression
1485 (format "%s if (lisp expr): "
1486 (if current-prefix-arg
1487 "UNmark"
1488 "Mark")))
1489 current-prefix-arg))
1490 (message "%s" predicate)
1491 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))
1492 inode s mode nlink uid gid size time name sym)
1493 (dired-mark-if
1494 (save-excursion
1495 (and
1496 ;; Sets vars
1497 ;; inode s mode nlink uid gid size time name sym
1499 ;; according to current file line. Returns t for success, nil if
1500 ;; there is no file line. Upon success, all variables are set, either
1501 ;; to nil or the appropriate value, so they need not be initialized.
1502 ;; Moves point within the current line.
1503 (dired-move-to-filename)
1504 (let ((mode-len 10) ; length of mode string
1505 ;; like in dired.el, but with subexpressions \1=inode, \2=s:
1506 ;; GNU ls -hs suffixes the block count with a unit and
1507 ;; prints it as a float, FreeBSD does neither.
1508 (dired-re-inode-size "\\=\\s *\\([0-9]+\\s +\\)?\
1509 \\(?:\\([0-9]+\\(?:\\.[0-9]*\\)?[BkKMGTPEZY]?\\)? ?\\)"))
1510 (beginning-of-line)
1511 (forward-char 2)
1512 (search-forward-regexp dired-re-inode-size nil t)
1513 ;; XXX Might be a size not followed by a unit prefix.
1514 ;; We could set s to inode if it were otherwise nil,
1515 ;; with a similar reasoning as below for setting gid to uid,
1516 ;; but it would be even more whimsical.
1517 (setq inode (when (match-string 1)
1518 (string-to-number (match-string 1))))
1519 (setq s (when (match-string 2)
1520 (dired-x--string-to-number (match-string 2))))
1521 (setq mode (buffer-substring (point) (+ mode-len (point))))
1522 (forward-char mode-len)
1523 ;; Skip any extended attributes marker ("." or "+").
1524 (or (looking-at " ")
1525 (forward-char 1))
1526 (setq nlink (read (current-buffer)))
1527 ;; Karsten Wenger <kw@cis.uni-muenchen.de> fixed uid.
1528 ;; Another issue is that GNU ls -n right-justifies numerical
1529 ;; UIDs and GIDs, while FreeBSD left-justifies them, so
1530 ;; don't rely on a specific whitespace layout. Both of them
1531 ;; right-justify all other numbers, though.
1532 ;; XXX Return a number if the uid or gid seems to be
1533 ;; numerical?
1534 (setq uid (buffer-substring (progn
1535 (skip-chars-forward " \t")
1536 (point))
1537 (progn
1538 (skip-chars-forward "^ \t")
1539 (point))))
1540 (dired-move-to-filename)
1541 (save-excursion
1542 (setq time
1543 ;; The regexp below tries to match from the last
1544 ;; digit of the size field through a space after the
1545 ;; date. Also, dates may have different formats
1546 ;; depending on file age, so the date column need
1547 ;; not be aligned to the right.
1548 (buffer-substring (save-excursion
1549 (skip-chars-backward " \t")
1550 (point))
1551 (progn
1552 (re-search-backward
1553 directory-listing-before-filename-regexp)
1554 (skip-chars-forward "^ \t")
1555 (1+ (point))))
1556 size (dired-x--string-to-number
1557 ;; We know that there's some kind of number
1558 ;; before point because the regexp search
1559 ;; above succeeded. I don't think it's worth
1560 ;; doing an extra check for leading garbage.
1561 (buffer-substring (point)
1562 (progn
1563 (skip-chars-backward "^ \t")
1564 (point))))
1565 ;; If no gid is displayed, gid will be set to uid
1566 ;; but the user will then not reference it anyway in
1567 ;; PREDICATE.
1568 gid (buffer-substring (progn
1569 (skip-chars-backward " \t")
1570 (point))
1571 (progn
1572 (skip-chars-backward "^ \t")
1573 (point)))))
1574 (setq name (buffer-substring (point)
1576 (dired-move-to-end-of-filename t)
1577 (point)))
1578 sym (if (looking-at " -> ")
1579 (buffer-substring (progn (forward-char 4) (point))
1580 (line-end-position))
1581 ""))
1583 (eval predicate
1584 `((inode . ,inode)
1585 (s . ,s)
1586 (mode . ,mode)
1587 (nlink . ,nlink)
1588 (uid . ,uid)
1589 (gid . ,gid)
1590 (size . ,size)
1591 (time . ,time)
1592 (name . ,name)
1593 (sym . ,sym)))))
1594 (format "'%s file" predicate))))
1597 ;;; FIND FILE AT POINT.
1599 (defcustom dired-x-hands-off-my-keys t
1600 "Non-nil means don't remap `find-file' to `dired-x-find-file'.
1601 Similarly for `find-file-other-window' and `dired-x-find-file-other-window'.
1602 If you change this variable without using \\[customize] after `dired-x.el'
1603 is loaded then call \\[dired-x-bind-find-file]."
1604 :type 'boolean
1605 :initialize 'custom-initialize-default
1606 :set (lambda (symbol value)
1607 (set symbol value)
1608 (dired-x-bind-find-file))
1609 :group 'dired-x)
1611 (defun dired-x-bind-find-file ()
1612 "Bind `dired-x-find-file' in place of `find-file' (or vice-versa).
1613 Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
1614 Binding direction based on `dired-x-hands-off-my-keys'."
1615 (interactive)
1616 (if (called-interactively-p 'interactive)
1617 (setq dired-x-hands-off-my-keys
1618 (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
1619 (define-key (current-global-map) [remap find-file]
1620 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file))
1621 (define-key (current-global-map) [remap find-file-other-window]
1622 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file-other-window)))
1624 ;; Now call it so binding is correct. This could go in the :initialize
1625 ;; slot, but then dired-x-bind-find-file has to be defined before the
1626 ;; defcustom, and we get free variable warnings.
1627 (dired-x-bind-find-file)
1629 (defun dired-x-find-file (filename)
1630 "Edit file FILENAME.
1631 Like `find-file', except that when called interactively with a
1632 prefix argument, it offers the filename near point as a default."
1633 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1634 (find-file filename))
1636 (defun dired-x-find-file-other-window (filename)
1637 "Edit file FILENAME, in another window.
1638 Like `find-file-other-window', except that when called interactively with
1639 a prefix argument, when it offers the filename near point as a default."
1640 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1641 (find-file-other-window filename))
1643 ;;; Internal functions.
1645 ;; Fixme: This should probably use `thing-at-point'. -- fx
1646 (defun dired-filename-at-point ()
1647 "Return the filename closest to point, expanded.
1648 Point should be in or after a filename."
1649 (save-excursion
1650 ;; First see if just past a filename.
1651 (or (eobp) ; why?
1652 (when (looking-at-p "[] \t\n[{}()]") ; whitespace or some parens
1653 (skip-chars-backward " \n\t\r({[]})")
1654 (or (bobp) (backward-char 1))))
1655 (let ((filename-chars "-.[:alnum:]_/:$+@")
1656 start prefix)
1657 (if (looking-at-p (format "[%s]" filename-chars))
1658 (progn
1659 (skip-chars-backward filename-chars)
1660 (setq start (point)
1661 prefix
1662 ;; This is something to do with ange-ftp filenames.
1663 ;; It convert foo@bar to /foo@bar.
1664 ;; But when does the former occur in dired buffers?
1665 (and (string-match-p
1666 "^\\w+@"
1667 (buffer-substring start (line-end-position)))
1668 "/"))
1669 (if (string-match-p "[/~]" (char-to-string (preceding-char)))
1670 (setq start (1- start)))
1671 (skip-chars-forward filename-chars))
1672 (error "No file found around point!"))
1673 ;; Return string.
1674 (expand-file-name (concat prefix (buffer-substring start (point)))))))
1676 (defun dired-x-read-filename-at-point (prompt)
1677 "Return filename prompting with PROMPT with completion.
1678 If `current-prefix-arg' is non-nil, uses name at point as guess."
1679 (if current-prefix-arg
1680 (let ((guess (dired-filename-at-point)))
1681 (read-file-name prompt
1682 (file-name-directory guess)
1683 guess
1684 nil (file-name-nondirectory guess)))
1685 (read-file-name prompt default-directory)))
1687 (define-obsolete-function-alias 'read-filename-at-point
1688 'dired-x-read-filename-at-point "24.1") ; is this even needed?
1690 ;;; BUG REPORTS
1692 (define-obsolete-function-alias 'dired-x-submit-report 'report-emacs-bug "24.1")
1695 ;; As Barry Warsaw would say: "This might be useful..."
1696 (provide 'dired-x)
1698 ;; Local Variables:
1699 ;; byte-compile-dynamic: t
1700 ;; generated-autoload-file: "dired-loaddefs.el"
1701 ;; End:
1703 ;;; dired-x.el ends here