1 ;;; dired-x.el --- extra Dired functionality -*- lexical-binding:t -*-
3 ;; Copyright (C) 1993-1994, 1997, 2001-2016 Free Software Foundation,
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
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/>.
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 ;; At load time dired-x.el will install itself and bind some dired keys.
33 ;; Some dired.el and dired-aux.el functions have extra features if
36 ;; User customization: M-x customize-group RET dired-x RET.
38 ;; *Please* see the `dired-x' info pages for more details.
43 ;; This is a no-op if dired-x is being loaded via `dired-load-hook',
44 ;; but maybe not if a dired-x function is being autoloaded.
47 ;;; User-defined variables.
50 "Extended directory editing (dired-x)."
53 (defgroup dired-keys nil
54 "Dired keys customizations."
58 (defcustom dired-bind-vm nil
59 "Non-nil means \"V\" runs `dired-vm', otherwise \"V\" runs `dired-rmail'.
60 RMAIL files in the old Babyl format (used before before Emacs 23.1)
61 contain \"-*- rmail -*-\" at the top, so `dired-find-file'
62 will run `rmail' on these files. New RMAIL files use the standard
63 mbox format, and so cannot be distinguished in this way."
67 (defcustom dired-bind-jump t
68 "Non-nil means bind `dired-jump' to C-x C-j, otherwise do not.
69 Setting this variable directly after dired-x is loaded has no effect -
72 :set
(lambda (sym val
)
75 (define-key ctl-x-map
"\C-j" 'dired-jump
)
76 (define-key ctl-x-4-map
"\C-j" 'dired-jump-other-window
))
77 (if (eq 'dired-jump
(lookup-key ctl-x-map
"\C-j"))
78 (define-key ctl-x-map
"\C-j" nil
))
79 (if (eq 'dired-jump-other-window
(lookup-key ctl-x-4-map
"\C-j"))
80 (define-key ctl-x-4-map
"\C-j" nil
))))
83 (defcustom dired-bind-man t
84 "Non-nil means bind `dired-man' to \"N\" in Dired, otherwise do not.
85 Setting this variable directly after dired-x is loaded has no effect -
88 :set
(lambda (sym val
)
90 (define-key dired-mode-map
"N" 'dired-man
)
91 (if (eq 'dired-man
(lookup-key dired-mode-map
"N"))
92 (define-key dired-mode-map
"N" nil
))))
95 (defcustom dired-bind-info t
96 "Non-nil means bind `dired-info' to \"I\" in Dired, otherwise do not.
97 Setting this variable directly after dired-x is loaded has no effect -
100 :set
(lambda (sym val
)
102 (define-key dired-mode-map
"I" 'dired-info
)
103 (if (eq 'dired-info
(lookup-key dired-mode-map
"I"))
104 (define-key dired-mode-map
"I" nil
))))
107 (defcustom dired-vm-read-only-folders nil
108 "If non-nil, \\[dired-vm] will visit all folders read-only.
109 If neither nil nor t, e.g. the symbol `if-file-read-only', only
110 files not writable by you are visited read-only."
111 :type
'(choice (const :tag
"off" nil
)
113 (other :tag
"non-writable only" if-file-read-only
))
116 (defcustom dired-omit-size-limit
30000
117 "Maximum size for the \"omitting\" feature.
118 If nil, there is no maximum size."
119 :type
'(choice (const :tag
"no maximum" nil
) integer
)
122 (defcustom dired-omit-case-fold
'filesystem
123 "Determine whether \"omitting\" patterns are case-sensitive.
124 When nil, always be case-sensitive; when t, always be
125 case-insensitive; the default value, `filesystem', causes case
126 folding to be used on case-insensitive filesystems only."
127 :type
'(choice (const :tag
"Always case-sensitive" nil
)
128 (const :tag
"Always case-insensitive" t
)
129 (const :tag
"According to filesystem" filesystem
))
133 (defun dired-omit-case-fold-p (dir)
134 "Non-nil if `dired-omit-mode' should be case-insensitive in DIR."
135 (if (eq dired-omit-case-fold
'filesystem
)
136 (file-name-case-sensitive-p dir
)
137 dired-omit-case-fold
))
139 ;; For backward compatibility
140 (define-obsolete-variable-alias 'dired-omit-files-p
'dired-omit-mode
"22.1")
141 (define-minor-mode dired-omit-mode
142 "Toggle omission of uninteresting files in Dired (Dired-Omit mode).
143 With a prefix argument ARG, enable Dired-Omit mode if ARG is
144 positive, and disable it otherwise. If called from Lisp, enable
145 the mode if ARG is omitted or nil.
147 Dired-Omit mode is a buffer-local minor mode. When enabled in a
148 Dired buffer, Dired does not list files whose filenames match
149 regexp `dired-omit-files', nor files ending with extensions in
150 `dired-omit-extensions'.
152 To enable omitting in every Dired buffer, you can put this in
155 (add-hook \\='dired-mode-hook (lambda () (dired-omit-mode)))
157 See Info node `(dired-x) Omitting Variables' for more information."
160 ;; This will mention how many lines were omitted:
161 (let ((dired-omit-size-limit nil
)) (dired-omit-expunge))
164 (put 'dired-omit-mode
'safe-local-variable
'booleanp
)
166 (defcustom dired-omit-files
"^\\.?#\\|^\\.$\\|^\\.\\.$"
167 "Filenames matching this regexp will not be displayed.
168 This only has effect when `dired-omit-mode' is t. See interactive function
169 `dired-omit-mode' (\\[dired-omit-mode]) and variable
170 `dired-omit-extensions'. The default is to omit `.', `..', auto-save
171 files and lock files."
175 (defcustom dired-omit-verbose t
176 "When non-nil, show messages when omitting files.
177 When nil, don't show messages."
182 (defcustom dired-find-subdir nil
; t is pretty near to DWIM...
183 "If non-nil, Dired always finds a directory in a buffer of its own.
184 If nil, Dired finds the directory as a subdirectory in some other buffer
185 if it is present as one.
187 If there are several Dired buffers for a directory, the most recently
190 Dired avoids switching to the current buffer, so that if you have
191 a normal and a wildcard buffer for the same directory, \\[dired] will
192 toggle between those two."
196 (defcustom dired-enable-local-variables t
197 "Control use of local-variables lists in Dired.
198 This temporarily overrides the value of `enable-local-variables' when
199 listing a directory. See also `dired-local-variables-file'."
201 :type
'(choice (const :tag
"Query Unsafe" t
)
202 (const :tag
"Safe Only" :safe
)
203 (const :tag
"Do all" :all
)
204 (const :tag
"Ignore" nil
)
205 (other :tag
"Query" other
))
208 (make-obsolete-variable 'dired-enable-local-variables
209 "use a standard `dir-locals-file' instead." "24.1")
211 (defcustom dired-guess-shell-gnutar
213 (dolist (exe '("tar" "gtar"))
214 (if (with-temp-buffer
215 (ignore-errors (call-process exe nil t nil
"--version"))
216 (and (re-search-backward "GNU tar" nil t
) t
))
217 (throw 'found exe
))))
218 "If non-nil, name of GNU tar executable.
219 \(E.g., \"tar\" or \"gtar\"). The `z' switch will be used with it for
220 compressed or gzip'ed tar files. If you don't have GNU tar, set this
221 to nil: a pipe using `zcat' or `gunzip -c' will be used."
222 ;; Changed from system-type test to testing --version output.
223 ;; Maybe test --help for -z instead?
225 :type
'(choice (const :tag
"Not GNU tar" nil
)
226 (string :tag
"Command name"))
229 (defcustom dired-guess-shell-gzip-quiet t
230 "Non-nil says pass -q to gzip overriding verbose GZIP environment."
234 (defcustom dired-guess-shell-znew-switches nil
235 "If non-nil, then string of switches passed to `znew', example: \"-K\"."
236 :type
'(choice (const :tag
"None" nil
)
237 (string :tag
"Switches"))
240 (defcustom dired-clean-up-buffers-too t
241 "Non-nil means offer to kill buffers visiting files and dirs deleted in Dired."
247 (define-key dired-mode-map
"\C-x\M-o" 'dired-omit-mode
)
248 (define-key dired-mode-map
"*O" 'dired-mark-omitted
)
249 (define-key dired-mode-map
"\M-(" 'dired-mark-sexp
)
250 (define-key dired-mode-map
"*(" 'dired-mark-sexp
)
251 (define-key dired-mode-map
"*." 'dired-mark-extension
)
252 (define-key dired-mode-map
"\M-!" 'dired-smart-shell-command
)
253 (define-key dired-mode-map
"\M-G" 'dired-goto-subdir
)
254 (define-key dired-mode-map
"F" 'dired-do-find-marked-files
)
255 (define-key dired-mode-map
"Y" 'dired-do-relsymlink
)
256 (define-key dired-mode-map
"%Y" 'dired-do-relsymlink-regexp
)
257 (define-key dired-mode-map
"V" 'dired-do-run-mail
)
263 (let ((menu (lookup-key dired-mode-map
[menu-bar
])))
264 (easy-menu-add-item menu
'("Operate")
265 ["Find Files" dired-do-find-marked-files
266 :help
"Find current or marked files"]
268 (easy-menu-add-item menu
'("Operate")
269 ["Relative Symlink to..." dired-do-relsymlink
270 :visible
(fboundp 'make-symbolic-link
)
271 :help
"Make relative symbolic links for current or \
274 (easy-menu-add-item menu
'("Mark")
275 ["Flag Extension..." dired-flag-extension
276 :help
"Flag files with a certain extension for deletion"]
278 (easy-menu-add-item menu
'("Mark")
279 ["Mark Extension..." dired-mark-extension
280 :help
"Mark files with a certain extension"]
282 (easy-menu-add-item menu
'("Mark")
283 ["Mark Omitted" dired-mark-omitted
284 :help
"Mark files matching `dired-omit-files' \
285 and `dired-omit-extensions'"]
287 (easy-menu-add-item menu
'("Regexp")
288 ["Relative Symlink..." dired-do-relsymlink-regexp
289 :visible
(fboundp 'make-symbolic-link
)
290 :help
"Make relative symbolic links for files \
293 (easy-menu-add-item menu
'("Immediate")
294 ["Omit Mode" dired-omit-mode
295 :style toggle
:selected dired-omit-mode
296 :help
"Enable or disable omitting \"uninteresting\" \
301 ;; Install into appropriate hooks.
303 (add-hook 'dired-mode-hook
'dired-extra-startup
)
304 (add-hook 'dired-after-readin-hook
'dired-omit-expunge
)
306 (defun dired-extra-startup ()
307 "Automatically put on `dired-mode-hook' to get extra Dired features:
309 \\[dired-do-run-mail]\t-- run mail on folder (see `dired-bind-vm')
310 \\[dired-info]\t-- run info on file
311 \\[dired-man]\t-- run man on file
312 \\[dired-do-find-marked-files]\t-- visit all marked files simultaneously
313 \\[dired-omit-mode]\t-- toggle omitting of files
314 \\[dired-mark-sexp]\t-- mark by Lisp expression
316 To see the options you can set, use M-x customize-group RET dired-x RET.
317 See also the functions:
318 `dired-flag-extension'
325 `dired-do-find-marked-files'"
327 ;; These must be done in each new dired buffer.
328 (dired-hack-local-variables)
329 (dired-omit-startup))
332 ;;; EXTENSION MARKING FUNCTIONS.
334 ;; Mark files with some extension.
335 (defun dired-mark-extension (extension &optional marker-char
)
336 "Mark all files with a certain EXTENSION for use in later commands.
337 A `.' is *not* automatically prepended to the string entered.
338 EXTENSION may also be a list of extensions instead of a single one.
339 Optional MARKER-CHAR is marker to use.
340 Interactively, ask for EXTENSION.
341 Prefixed with one C-u, unmark files instead.
342 Prefixed with two C-u's, prompt for MARKER-CHAR and mark files with it."
345 (read-string (format "%s extension: "
346 (if (equal current-prefix-arg
'(4))
350 (pcase current-prefix-arg
353 (let* ((dflt (char-to-string dired-marker-char
))
356 "Marker character to use (default %s): " dflt
)
359 (_ dired-marker-char
))))
360 (list suffix marker
)))
361 (or (listp extension
)
362 (setq extension
(list extension
)))
363 (dired-mark-files-regexp
364 (concat ".";; don't match names with nothing but an extension
366 (mapconcat 'regexp-quote extension
"\\|")
370 (defun dired-flag-extension (extension)
371 "In Dired, flag all files with a certain EXTENSION for deletion.
372 A `.' is *not* automatically prepended to the string entered."
373 (interactive "sFlagging extension: ")
374 (dired-mark-extension extension dired-del-marker
))
376 ;; Define some unpopular file extensions. Used for cleaning and omitting.
378 (defvar dired-patch-unclean-extensions
380 "List of extensions of dispensable files created by the `patch' program.")
382 (defvar dired-tex-unclean-extensions
383 '(".toc" ".log" ".aux");; these are already in completion-ignored-extensions
384 "List of extensions of dispensable files created by TeX.")
386 (defvar dired-latex-unclean-extensions
387 '(".idx" ".lof" ".lot" ".glo")
388 "List of extensions of dispensable files created by LaTeX.")
390 (defvar dired-bibtex-unclean-extensions
392 "List of extensions of dispensable files created by BibTeX.")
394 (defvar dired-texinfo-unclean-extensions
395 '(".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
396 ".tp" ".tps" ".vr" ".vrs")
397 "List of extensions of dispensable files created by texinfo.")
399 (defun dired-clean-patch ()
400 "Flag dispensable files created by patch for deletion.
401 See variable `dired-patch-unclean-extensions'."
403 (dired-flag-extension dired-patch-unclean-extensions
))
405 (defun dired-clean-tex ()
406 "Flag dispensable files created by [La]TeX etc. for deletion.
407 See variables `dired-tex-unclean-extensions',
408 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
409 `dired-texinfo-unclean-extensions'."
411 (dired-flag-extension (append dired-texinfo-unclean-extensions
412 dired-latex-unclean-extensions
413 dired-bibtex-unclean-extensions
414 dired-tex-unclean-extensions
)))
416 (defun dired-very-clean-tex ()
417 "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
418 See variables `dired-texinfo-unclean-extensions',
419 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
420 `dired-texinfo-unclean-extensions'."
422 (dired-flag-extension (append dired-texinfo-unclean-extensions
423 dired-latex-unclean-extensions
424 dired-bibtex-unclean-extensions
425 dired-tex-unclean-extensions
428 (defvar tar-superior-buffer
)
432 (defun dired-jump (&optional other-window file-name
)
433 "Jump to Dired buffer corresponding to current buffer.
434 If in a file, Dired the current directory and move to file's line.
435 If in Dired already, pop up a level and goto old directory's line.
436 In case the proper Dired file line cannot be found, refresh the dired
437 buffer and try again.
438 When OTHER-WINDOW is non-nil, jump to Dired buffer in other window.
439 When FILE-NAME is non-nil, jump to its line in Dired.
440 Interactively with prefix argument, read FILE-NAME."
442 (list nil
(and current-prefix-arg
443 (read-file-name "Jump to Dired file: "))))
444 (if (bound-and-true-p tar-subfile-mode
)
445 (switch-to-buffer tar-superior-buffer
)
446 ;; Expand file-name before `dired-goto-file' call:
447 ;; `dired-goto-file' requires its argument to be an absolute
448 ;; file name; the result of `read-file-name' could be
449 ;; an abbreviated file name (Bug#24409).
450 (let* ((file (or (and file-name
(expand-file-name file-name
))
452 (dir (if file
(file-name-directory file
) default-directory
)))
453 (if (and (eq major-mode
'dired-mode
) (null file-name
))
455 (setq dir
(dired-current-directory))
456 (dired-up-directory other-window
)
457 (unless (dired-goto-file dir
)
458 ;; refresh and try again
459 (dired-insert-subdir (file-name-directory dir
))
460 (dired-goto-file dir
)))
462 (dired-other-window dir
)
465 (or (dired-goto-file file
)
466 ;; refresh and try again
468 (dired-insert-subdir (file-name-directory file
))
469 (dired-goto-file file
))
470 ;; Toggle omitting, if it is on, and try again.
471 (when dired-omit-mode
473 (dired-goto-file file
))))))))
476 (defun dired-jump-other-window (&optional file-name
)
477 "Like \\[dired-jump] (`dired-jump') but in other window."
479 (list (and current-prefix-arg
480 (read-file-name "Jump to Dired file: "))))
481 (dired-jump t file-name
))
485 ;; Enhanced omitting of lines from directory listings.
486 ;; Marked files are never omitted.
488 ;; should probably get rid of this and always use 'no-dir.
489 ;; sk 28-Aug-1991 09:37
490 (defvar dired-omit-localp
'no-dir
491 "The LOCALP argument `dired-omit-expunge' passes to `dired-get-filename'.
492 If it is `no-dir', omitting is much faster, but you can only match
493 against the non-directory part of the file name. Set it to nil if you
494 need to match the entire file name.")
496 ;; \017=^O for Omit - other packages can chose other control characters.
497 (defvar dired-omit-marker-char ?
\017
498 "Temporary marker used by Dired-Omit.
499 Should never be used as marker by the user or other packages.")
501 (defun dired-omit-startup ()
502 (or (assq 'dired-omit-mode minor-mode-alist
)
503 (setq minor-mode-alist
504 (append '((dired-omit-mode
505 (:eval
(if (eq major-mode
'dired-mode
)
509 (defun dired-mark-omitted ()
510 "Mark files matching `dired-omit-files' and `dired-omit-extensions'."
512 (let ((dired-omit-mode nil
)) (revert-buffer)) ;; Show omitted files
513 (dired-mark-unmarked-files (dired-omit-regexp) nil nil dired-omit-localp
514 (dired-omit-case-fold-p dired-directory
)))
516 (defcustom dired-omit-extensions
517 (append completion-ignored-extensions
518 dired-latex-unclean-extensions
519 dired-bibtex-unclean-extensions
520 dired-texinfo-unclean-extensions
)
521 "If non-nil, a list of extensions (strings) to omit from Dired listings.
522 Defaults to elements of `completion-ignored-extensions',
523 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
524 `dired-texinfo-unclean-extensions'.
526 See interactive function `dired-omit-mode' (\\[dired-omit-mode]) and
527 variables `dired-omit-mode' and `dired-omit-files'."
528 :type
'(repeat string
)
531 (defun dired-omit-expunge (&optional regexp
)
532 "Erases all unmarked files matching REGEXP.
533 Does nothing if global variable `dired-omit-mode' is nil, or if called
534 non-interactively and buffer is bigger than `dired-omit-size-limit'.
535 If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
536 filenames ending in `dired-omit-extensions'.
537 If REGEXP is the empty string, this function is a no-op.
539 This functions works by temporarily binding `dired-marker-char' to
540 `dired-omit-marker-char' and calling `dired-do-kill-lines'."
541 (interactive "sOmit files (regexp): ")
542 (if (and dired-omit-mode
543 (or (called-interactively-p 'interactive
)
544 (not dired-omit-size-limit
)
545 (< (buffer-size) dired-omit-size-limit
)
547 (when dired-omit-verbose
548 (message "Not omitting: directory larger than %d characters."
549 dired-omit-size-limit
))
550 (setq dired-omit-mode nil
)
552 (let ((omit-re (or regexp
(dired-omit-regexp)))
553 (old-modified-p (buffer-modified-p))
555 (or (string= omit-re
"")
556 (let ((dired-marker-char dired-omit-marker-char
))
557 (when dired-omit-verbose
(message "Omitting..."))
558 (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp
559 (dired-omit-case-fold-p dired-directory
))
561 (setq count
(dired-do-kill-lines
563 (if dired-omit-verbose
"Omitted %d line%s." "")))
564 (force-mode-line-update))
565 (when dired-omit-verbose
(message "(Nothing to omit)")))))
566 ;; Try to preserve modified state of buffer. So `%*' doesn't appear
567 ;; in mode-line of omitted buffers.
568 (set-buffer-modified-p (and old-modified-p
570 (goto-char (point-min))
571 (re-search-forward dired-re-mark nil t
))))
574 (defun dired-omit-regexp ()
575 (concat (if dired-omit-files
(concat "\\(" dired-omit-files
"\\)") "")
576 (if (and dired-omit-files dired-omit-extensions
) "\\|" "")
577 (if dired-omit-extensions
578 (concat ".";; a non-extension part should exist
580 (mapconcat 'regexp-quote dired-omit-extensions
"\\|")
584 ;; Returns t if any work was done, nil otherwise.
585 (defun dired-mark-unmarked-files (regexp msg
&optional unflag-p localp case-fold-p
)
586 "Mark unmarked files matching REGEXP, displaying MSG.
587 REGEXP is matched against the entire file name. When called
588 interactively, prompt for REGEXP.
589 With prefix argument, unflag all those files.
590 Optional fourth argument LOCALP is as in `dired-get-filename'.
591 Optional fifth argument CASE-FOLD-P specifies the value of
592 `case-fold-search' used for matching REGEXP."
595 "Mark unmarked files matching regexp (default all): "
596 nil
'dired-regexp-history
)
597 nil current-prefix-arg nil
))
598 (let ((dired-marker-char (if unflag-p ?\s dired-marker-char
)))
601 ;; not already marked
604 (let ((fn (dired-get-filename localp t
))
605 ;; Match patterns case-insensitively on case-insensitive
607 (case-fold-search case-fold-p
))
608 (and fn
(string-match-p regexp fn
))))
612 ;;; VIRTUAL DIRED MODE.
614 ;; For browsing `ls -lR' listings in a dired-like fashion.
616 (defalias 'virtual-dired
'dired-virtual
)
617 (defun dired-virtual (dirname &optional switches
)
618 "Put this buffer into Virtual Dired mode.
620 In Virtual Dired mode, all commands that do not actually consult the
621 filesystem will work.
623 This is useful if you want to peruse and move around in an ls -lR
624 output file, for example one you got from an ftp server. With
625 ange-ftp, you can even Dired a directory containing an ls-lR file,
626 visit that file and turn on Virtual Dired mode. But don't try to save
627 this file, as dired-virtual indents the listing and thus changes the
630 If you have save a Dired buffer in a file you can use \\[dired-virtual] to
631 resume it in a later session.
633 Type \\<dired-mode-map>\\[revert-buffer] \
634 in the Virtual Dired buffer and answer `y' to convert
635 the virtual to a real Dired buffer again. You don't have to do this, though:
636 you can relist single subdirs using \\[dired-do-redisplay]."
638 ;; DIRNAME is the top level directory of the buffer. It will become
639 ;; its `default-directory'. If nil, the old value of
640 ;; default-directory is used.
642 ;; Optional SWITCHES are the ls switches to use.
644 ;; Shell wildcards will be used if there already is a `wildcard'
645 ;; line in the buffer (thus it is a saved Dired buffer), but there
646 ;; is no other way to get wildcards. Insert a `wildcard' line by
647 ;; hand if you want them.
650 (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
651 (goto-char (point-min))
652 (or (looking-at-p " ")
653 ;; if not already indented, do it now:
654 (indent-region (point-min) (point-max) 2))
655 (or dirname
(setq dirname default-directory
))
656 (setq dirname
(expand-file-name (file-name-as-directory dirname
)))
657 (setq default-directory dirname
) ; contains no wildcards
658 (let ((wildcard (save-excursion
659 (goto-char (point-min))
661 (and (looking-at "^ wildcard ")
662 (buffer-substring (match-end 0)
663 (line-end-position))))))
665 (setq dirname
(expand-file-name wildcard default-directory
))))
666 ;; If raw ls listing (not a saved old dired buffer), give it a
667 ;; decent subdir headerline:
668 (goto-char (point-min))
669 (or (looking-at-p dired-subdir-regexp
)
671 (directory-file-name (file-name-directory default-directory
))
673 (dired-mode dirname
(or switches dired-listing-switches
))
674 (setq mode-name
"Virtual Dired"
675 revert-buffer-function
'dired-virtual-revert
)
676 (set (make-local-variable 'dired-subdir-alist
) nil
)
677 (dired-build-subdir-alist)
678 (goto-char (point-min))
679 (dired-initial-position dirname
))
681 (defun dired-virtual-guess-dir ()
682 "Guess and return appropriate working directory of this buffer.
683 The buffer is assumed to be in Dired or ls -lR format. The guess is
684 based upon buffer contents. If nothing could be guessed, returns
687 (let ((regexp "^\\( \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
689 (goto-char (point-min))
690 (cond ((looking-at regexp
)
691 ;; If a saved dired buffer, look to which dir and
692 ;; perhaps wildcard it belongs:
693 (let ((dir (buffer-substring (match-beginning subexpr
)
694 (match-end subexpr
))))
695 (file-name-as-directory dir
)))
696 ;; Else no match for headerline found. It's a raw ls listing.
697 ;; In raw ls listings the directory does not have a headerline
698 ;; try parent of first subdir, if any
699 ((re-search-forward regexp nil t
)
702 (file-name-as-directory
703 (buffer-substring (match-beginning subexpr
)
704 (match-end subexpr
))))))
705 (t ; if all else fails
709 (defun dired-virtual-revert (&optional _arg _noconfirm
)
711 (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
712 (error "Cannot revert a Virtual Dired buffer")
713 (setq mode-name
"Dired"
714 revert-buffer-function
'dired-revert
)
717 ;; A zero-arg version of dired-virtual.
718 (defun dired-virtual-mode ()
719 "Put current buffer into Virtual Dired mode (see `dired-virtual').
720 Useful on `magic-mode-alist' with the regexp
722 \"^ \\\\(/[^ /]+\\\\)+/?:$\"
724 to put saved Dired buffers automatically into Virtual Dired mode.
726 Also useful for `auto-mode-alist' like this:
728 (add-to-list \\='auto-mode-alist
729 \\='(\"[^/]\\\\.dired\\\\\\='\" . dired-virtual-mode))"
731 (dired-virtual (dired-virtual-guess-dir)))
736 ;; An Emacs buffer can have but one working directory, stored in the
737 ;; buffer-local variable `default-directory'. A Dired buffer may have
738 ;; several subdirectories inserted, but still has but one working directory:
739 ;; that of the top level Dired directory in that buffer. For some commands
740 ;; it is appropriate that they use the current Dired directory instead of
741 ;; `default-directory', e.g., `find-file' and `compile'. This is a general
742 ;; mechanism is provided for special handling of the working directory in
743 ;; special major modes.
745 (define-obsolete-variable-alias 'default-directory-alist
746 'dired-default-directory-alist
"24.1")
748 ;; It's easier to add to this alist than redefine function
749 ;; default-directory while keeping the old information.
750 (defconst dired-default-directory-alist
751 '((dired-mode .
(if (fboundp 'dired-current-directory
)
752 (dired-current-directory)
754 "Alist of major modes and their opinion on `default-directory'.
755 Each element has the form (MAJOR . EXPRESSION).
756 The function `dired-default-directory' evaluates EXPRESSION to
757 determine a default directory.")
759 (put 'dired-default-directory-alist
'risky-local-variable t
) ; gets eval'd
760 (make-obsolete-variable 'dired-default-directory-alist
761 "this feature is due to be removed." "24.1")
763 (defun dired-default-directory ()
764 "Return the `dired-default-directory-alist' entry for the current major-mode.
765 If none, return `default-directory'."
766 ;; It looks like this was intended to be something of a "general"
767 ;; feature, but it only ever seems to have been used in
768 ;; dired-smart-shell-command, and doesn't seem worth keeping around.
769 (declare (obsolete nil
"24.1"))
770 (or (eval (cdr (assq major-mode dired-default-directory-alist
)))
773 (defun dired-smart-shell-command (command &optional output-buffer error-buffer
)
774 "Like function `shell-command', but in the current Virtual Dired directory."
777 (read-shell-command "Shell command: " nil nil
779 (buffer-file-name (file-relative-name buffer-file-name
))
780 ((eq major-mode
'dired-mode
) (dired-get-filename t t
))))
782 shell-command-default-error-buffer
))
783 (let ((default-directory (or (and (eq major-mode
'dired-mode
)
784 (dired-current-directory))
786 (shell-command command output-buffer error-buffer
)))
789 ;;; LOCAL VARIABLES FOR DIRED BUFFERS.
791 ;; Brief Description (This feature is obsolete as of Emacs 24.1)
793 ;; * `dired-extra-startup' is part of the `dired-mode-hook'.
795 ;; * `dired-extra-startup' calls `dired-hack-local-variables'
797 ;; * `dired-hack-local-variables' checks the value of
798 ;; `dired-local-variables-file'
800 ;; * Check if `dired-local-variables-file' is a non-nil string and is a
801 ;; filename found in the directory of the Dired Buffer being created.
803 ;; * If `dired-local-variables-file' satisfies the above, then temporarily
804 ;; include it in the Dired Buffer at the bottom.
806 ;; * Set `enable-local-variables' temporarily to the user variable
807 ;; `dired-enable-local-variables' and run `hack-local-variables' on the
810 (defcustom dired-local-variables-file
(convert-standard-filename ".dired")
811 "Filename, as string, containing local Dired buffer variables to be hacked.
812 If this file found in current directory, then it will be inserted into dired
813 buffer and `hack-local-variables' will be run. See Info node
814 `(emacs)File Variables' for more information on local variables.
815 See also `dired-enable-local-variables'."
819 (make-obsolete-variable 'dired-local-variables-file
'dir-locals-file
"24.1")
821 (defun dired-hack-local-variables ()
822 "Evaluate local variables in `dired-local-variables-file' for Dired buffer."
823 (declare (obsolete hack-dir-local-variables-non-file-buffer
"24.1"))
824 (and (stringp dired-local-variables-file
)
825 (file-exists-p dired-local-variables-file
)
826 (let ((opoint (point-max))
827 (inhibit-read-only t
)
828 ;; In case user has `enable-local-variables' set to nil we
829 ;; override it locally with dired's variable.
830 (enable-local-variables dired-enable-local-variables
))
835 (insert-file-contents dired-local-variables-file
))
838 (let ((buffer-file-name dired-local-variables-file
))
839 (hack-local-variables))
840 ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
841 (delete-region opoint
(point-max)))
842 ;; Make sure that the mode line shows the proper information.
843 (dired-sort-set-mode-line))))
845 ;; Does not seem worth a dedicated command.
846 ;; See the more general features in files-x.el.
847 (defun dired-omit-here-always ()
848 "Create `dir-locals-file' setting `dired-omit-mode' to t in `dired-mode'.
849 If in a Dired buffer, reverts it."
850 (declare (obsolete add-dir-local-variable
"24.1"))
852 (if (file-exists-p dired-local-variables-file
)
853 (error "Old-style dired-local-variables-file `./%s' found;
854 replace it with a dir-locals-file `./%s'"
855 dired-local-variables-file
857 (if (file-exists-p dir-locals-file
)
858 (message "File `./%s' already exists." dir-locals-file
)
859 (add-dir-local-variable 'dired-mode
'subdirs nil
)
860 (add-dir-local-variable 'dired-mode
'dired-omit-mode t
)
861 ;; Run extra-hooks and revert directory.
862 (when (derived-mode-p 'dired-mode
)
863 (hack-dir-local-variables-non-file-buffer)
864 (dired-extra-startup)
868 ;;; GUESS SHELL COMMAND.
870 ;; Brief Description:
872 ;; * `dired-do-shell-command' is bound to `!' by dired.el.
874 ;; * `dired-guess-shell-command' provides smarter defaults for
875 ;;; dired-aux.el's `dired-read-shell-command'.
877 ;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
880 ;; * Parse `dired-guess-shell-alist-user' and
881 ;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
882 ;;; that matches the first file in the file list.
884 ;; * If the REGEXP matches all the entries of the file list then evaluate
885 ;;; COMMAND, which is either a string or a Lisp expression returning a
886 ;;; string. COMMAND may be a list of commands.
888 ;; * Return this command to `dired-guess-shell-command' which prompts user
889 ;;; with it. The list of commands is put into the list of default values.
890 ;;; If a command is used successfully then it is stored permanently in
891 ;;; `dired-shell-command-history'.
893 ;; Guess what shell command to apply to a file.
894 (defvar dired-shell-command-history nil
895 "History list for commands that read dired-shell commands.")
897 ;; Default list of shell commands.
899 ;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
900 ;; install GNU zip's version of zcat.
902 (autoload 'Man-support-local-filenames
"man")
904 (defvar dired-guess-shell-alist-default
907 '(if dired-guess-shell-gnutar
908 (concat dired-guess-shell-gnutar
" xvf")
910 ;; Extract files into a separate subdirectory
911 '(if dired-guess-shell-gnutar
912 (concat "mkdir " (file-name-sans-extension file
)
913 "; " dired-guess-shell-gnutar
" -C "
914 (file-name-sans-extension file
) " -xvf")
915 (concat "mkdir " (file-name-sans-extension file
)
916 "; tar -C " (file-name-sans-extension file
) " -xvf"))
917 ;; List archive contents.
918 '(if dired-guess-shell-gnutar
919 (concat dired-guess-shell-gnutar
" tvf")
922 ;; REGEXPS for compressed archives must come before the .Z rule to
924 (list "\\.tar\\.Z\\'"
926 '(if dired-guess-shell-gnutar
927 (concat dired-guess-shell-gnutar
" zxvf")
928 (concat "zcat * | tar xvf -"))
929 ;; Optional conversion to gzip format.
930 '(concat "znew" (if dired-guess-shell-gzip-quiet
" -q")
931 " " dired-guess-shell-znew-switches
))
934 (list "\\.t\\(ar\\.\\)?gz\\'"
935 '(if dired-guess-shell-gnutar
936 (concat dired-guess-shell-gnutar
" zxvf")
937 (concat "gunzip -qc * | tar xvf -"))
938 ;; Extract files into a separate subdirectory
939 '(if dired-guess-shell-gnutar
940 (concat "mkdir " (file-name-sans-extension file
)
941 "; " dired-guess-shell-gnutar
" -C "
942 (file-name-sans-extension file
) " -zxvf")
943 (concat "mkdir " (file-name-sans-extension file
)
944 "; gunzip -qc * | tar -C "
945 (file-name-sans-extension file
) " -xvf -"))
946 ;; Optional decompression.
947 '(concat "gunzip" (if dired-guess-shell-gzip-quiet
" -q" ""))
948 ;; List archive contents.
949 '(if dired-guess-shell-gnutar
950 (concat dired-guess-shell-gnutar
" ztvf")
951 (concat "gunzip -qc * | tar tvf -")))
954 (list "\\.t\\(ar\\.bz2\\|bz\\)\\'"
955 "bunzip2 -c * | tar xvf -"
956 ;; Extract files into a separate subdirectory
957 '(concat "mkdir " (file-name-sans-extension file
)
958 "; bunzip2 -c * | tar -C "
959 (file-name-sans-extension file
) " -xvf -")
960 ;; Optional decompression.
964 (list "\\.t\\(ar\\.\\)?xz\\'"
965 "unxz -c * | tar xvf -"
966 ;; Extract files into a separate subdirectory
967 '(concat "mkdir " (file-name-sans-extension file
)
968 "; unxz -c * | tar -C "
969 (file-name-sans-extension file
) " -xvf -")
970 ;; Optional decompression.
973 '("\\.shar\\.Z\\'" "zcat * | unshar")
974 '("\\.shar\\.g?z\\'" "gunzip -qc * | unshar")
976 '("\\.e?ps\\'" "ghostview" "xloadimage" "lpr")
977 (list "\\.e?ps\\.g?z\\'" "gunzip -qc * | ghostview -"
978 ;; Optional decompression.
979 '(concat "gunzip" (if dired-guess-shell-gzip-quiet
" -q")))
980 (list "\\.e?ps\\.Z\\'" "zcat * | ghostview -"
981 ;; Optional conversion to gzip format.
982 '(concat "znew" (if dired-guess-shell-gzip-quiet
" -q")
983 " " dired-guess-shell-znew-switches
))
985 '("\\.patch\\'" "cat * | patch")
986 (list "\\.patch\\.g?z\\'" "gunzip -qc * | patch"
987 ;; Optional decompression.
988 '(concat "gunzip" (if dired-guess-shell-gzip-quiet
" -q")))
989 (list "\\.patch\\.Z\\'" "zcat * | patch"
990 ;; Optional conversion to gzip format.
991 '(concat "znew" (if dired-guess-shell-gzip-quiet
" -q")
992 " " dired-guess-shell-znew-switches
))
994 ;; The following four extensions are useful with dired-man ("N" key)
995 ;; FIXME "man ./" does not work with dired-do-shell-command,
996 ;; because there seems to be no way for us to modify the filename,
997 ;; only the command. Hmph. `dired-man' works though.
998 (list "\\.\\(?:[0-9]\\|man\\)\\'"
999 '(let ((loc (Man-support-local-filenames)))
1000 (cond ((eq loc
'man-db
) "man -l")
1001 ((eq loc
'man
) "man ./")
1003 "cat * | tbl | nroff -man -h | col -b"))))
1004 (list "\\.\\(?:[0-9]\\|man\\)\\.g?z\\'"
1005 '(let ((loc (Man-support-local-filenames)))
1006 (cond ((eq loc
'man-db
)
1010 (t "gunzip -qc * | tbl | nroff -man -h | col -b")))
1011 ;; Optional decompression.
1012 '(concat "gunzip" (if dired-guess-shell-gzip-quiet
" -q")))
1013 (list "\\.[0-9]\\.Z\\'"
1014 '(let ((loc (Man-support-local-filenames)))
1015 (cond ((eq loc
'man-db
) "man -l")
1016 ((eq loc
'man
) "man ./")
1017 (t "zcat * | tbl | nroff -man -h | col -b")))
1018 ;; Optional conversion to gzip format.
1019 '(concat "znew" (if dired-guess-shell-gzip-quiet
" -q")
1020 " " dired-guess-shell-znew-switches
))
1021 '("\\.pod\\'" "perldoc" "pod2man * | nroff -man")
1023 '("\\.dvi\\'" "xdvi" "dvips") ; preview and printing
1024 '("\\.au\\'" "play") ; play Sun audiofiles
1025 '("\\.mpe?g\\'\\|\\.avi\\'" "xine -p")
1026 '("\\.ogg\\'" "ogg123")
1027 '("\\.mp3\\'" "mpg123")
1028 '("\\.wav\\'" "play")
1029 '("\\.uu\\'" "uudecode") ; for uudecoded files
1030 '("\\.hqx\\'" "mcvert")
1031 '("\\.sh\\'" "sh") ; execute shell scripts
1032 '("\\.xbm\\'" "bitmap") ; view X11 bitmaps
1033 '("\\.gp\\'" "gnuplot")
1034 '("\\.p[bgpn]m\\'" "xloadimage")
1035 '("\\.gif\\'" "xloadimage") ; view gif pictures
1036 '("\\.tif\\'" "xloadimage")
1037 '("\\.png\\'" "display") ; xloadimage 4.1 doesn't grok PNG
1038 '("\\.jpe?g\\'" "xloadimage")
1039 '("\\.fig\\'" "xfig") ; edit fig pictures
1040 '("\\.out\\'" "xgraph") ; for plotting purposes.
1041 '("\\.tex\\'" "latex" "tex")
1042 '("\\.texi\\(nfo\\)?\\'" "makeinfo" "texi2dvi")
1043 '("\\.pdf\\'" "xpdf")
1044 '("\\.doc\\'" "antiword" "strings")
1045 '("\\.rpm\\'" "rpm -qilp" "rpm -ivh")
1046 '("\\.dia\\'" "dia")
1047 '("\\.mgp\\'" "mgp")
1049 ;; Some other popular archivers.
1050 (list "\\.zip\\'" "unzip" "unzip -l"
1051 ;; Extract files into a separate subdirectory
1052 '(concat "unzip" (if dired-guess-shell-gzip-quiet
" -q")
1053 " -d " (file-name-sans-extension file
)))
1054 '("\\.zoo\\'" "zoo x//")
1055 '("\\.lzh\\'" "lharc x")
1056 '("\\.arc\\'" "arc x")
1057 '("\\.shar\\'" "unshar")
1058 '("\\.rar\\'" "unrar x")
1059 '("\\.7z\\'" "7z x")
1062 (list "\\.g?z\\'" '(concat "gunzip" (if dired-guess-shell-gzip-quiet
" -q")))
1063 (list "\\.dz\\'" "dictunzip")
1064 (list "\\.bz2\\'" "bunzip2")
1065 (list "\\.xz\\'" "unxz")
1066 (list "\\.Z\\'" "uncompress"
1067 ;; Optional conversion to gzip format.
1068 '(concat "znew" (if dired-guess-shell-gzip-quiet
" -q")
1069 " " dired-guess-shell-znew-switches
))
1071 '("\\.sign?\\'" "gpg --verify"))
1073 "Default alist used for shell command guessing.
1074 See `dired-guess-shell-alist-user'.")
1076 (defcustom dired-guess-shell-alist-user nil
1077 "User-defined alist of rules for suggested commands.
1078 These rules take precedence over the predefined rules in the variable
1079 `dired-guess-shell-alist-default' (to which they are prepended).
1081 Each element of this list looks like
1085 where each COMMAND can either be a string or a Lisp expression that evaluates
1086 to a string. This expression can access the file name as the variable `file'.
1087 If several COMMANDs are given, the first one will be the default
1088 and the rest will be added temporarily to the history and can be retrieved
1089 with \\[previous-history-element] (M-p) .
1091 The variable `dired-guess-shell-case-fold-search' controls whether
1092 REGEXP is matched case-sensitively."
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."
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
))
1114 ;; Find the first match in the alist for first file in FILES.
1116 (setq elt
(car alist
)
1119 (if (string-match-p regexp file
)
1120 (setq cmds
(cdr elt
)
1123 ;; If more than one file, see if all of FILES match regular expression.
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.
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
))
1144 (read-shell-command prompt nil
'dired-shell-command-history
)
1145 (setq prompt
(replace-regexp-in-string ": $" " " prompt
))
1147 ;; More than one guess
1148 (setq default-list default
1149 default
(car default
)
1152 (format "{%d guesses} " (length default-list
))))
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
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\"
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
)
1186 len2
(length file2
))
1187 ;; Find common initial file name components:
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
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
)))
1201 sub
(substring file1
0 index
)
1202 name1
(substring file1 index
)))
1203 (if (string-equal sub
"/")
1204 ;; No common initial file name found
1206 ;; Else they have a common parent directory
1207 (let ((tem (substring file2 index
))
1210 ;; Count number of slashes we must compensate for ...
1211 (while (setq start
(string-match "/" tem start
))
1212 (setq count
(1+ count
)
1214 ;; ... and prepend a "../" for each slash found:
1216 (setq name1
(concat "../" name1
)))))
1218 (directory-file-name name1
) ; must not link to foo/
1220 name2 ok-if-already-exists
)))
1222 (autoload 'dired-do-create-files
"dired-aux")
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
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]."
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'
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
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."
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.
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
)))
1312 (dolist (file (cdr file-list
))
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
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.
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."
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'."
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."
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'
1379 (let ((file (dired-get-filename t
)))
1381 (if (y-or-n-p (format-message
1382 "Visit `%s' as a mail folder with VM?" file
))
1384 ;; Read mail folder using rmail.
1385 (if (y-or-n-p (format-message
1386 "Visit `%s' as a mailbox with RMAIL?" file
))
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))))))
1401 ;; Needed if ls -lh is supported and also for GNU ls -ls.
1402 (defun dired-x--string-to-number (str)
1403 "Like `string-to-number' but recognize a trailing unit prefix.
1404 For example, 2K is expanded to 2048.0. The caller should make
1405 sure that a trailing letter in STR is one of BKkMGTPEZY."
1406 (let* ((val (string-to-number str
))
1407 (u (unless (zerop val
)
1408 (aref str
(1- (length str
))))))
1409 (when (and u
(> u ?
9))
1412 (let ((units '(?B ?K ?M ?G ?T ?P ?E ?Z ?Y
)))
1413 (while (and units
(/= (pop units
) u
))
1414 (setq val
(* 1024.0 val
)))))
1417 (defun dired-mark-sexp (predicate &optional unflag-p
)
1418 "Mark files for which PREDICATE returns non-nil.
1419 With a prefix arg, unmark or unflag those files instead.
1421 PREDICATE is a lisp expression that can refer to the following symbols:
1423 inode [integer] the inode of the file (only for ls -i output)
1424 s [integer] the size of the file for ls -s output
1425 (usually in blocks or, with -k, in KByte)
1426 mode [string] file permission bits, e.g. \"-rw-r--r--\"
1427 nlink [integer] number of links to file
1429 gid [string] group (If the gid is not displayed by ls,
1430 this will still be set (to the same as uid))
1431 size [integer] file size in bytes
1432 time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
1433 name [string] the name of the file
1434 sym [string] if file is a symbolic link, the linked-to name, else \"\"
1440 to mark all zero length files.
1442 There's an ambiguity when a single integer not followed by a unit
1443 prefix precedes the file mode: It is then parsed as inode number
1444 and not as block size (this always works for GNU coreutils ls).
1446 Another limitation is that the uid field is needed for the
1447 function to work correctly. In particular, the field is not
1448 present for some values of `ls-lisp-emulation'.
1450 This function operates only on the buffer content and does not
1451 refer at all to the underlying file system. Contrast this with
1452 `find-dired', which might be preferable for the task at hand."
1453 ;; Using sym="" instead of nil avoids the trap of
1454 ;; (string-match "foo" sym) into which a user would soon fall.
1455 ;; Give `equal' instead of `=' in the example, as this works on
1456 ;; integers and strings.
1458 (list (read--expression
1459 (format "%s if (lisp expr): "
1460 (if current-prefix-arg
1463 current-prefix-arg
))
1464 (message "%s" predicate
)
1465 (let ((dired-marker-char (if unflag-p ?
\040 dired-marker-char
))
1466 inode s mode nlink uid gid size time name sym
)
1471 ;; inode s mode nlink uid gid size time name sym
1473 ;; according to current file line. Returns t for success, nil if
1474 ;; there is no file line. Upon success, all variables are set, either
1475 ;; to nil or the appropriate value, so they need not be initialized.
1476 ;; Moves point within the current line.
1477 (dired-move-to-filename)
1478 (let ((mode-len 10) ; length of mode string
1479 ;; like in dired.el, but with subexpressions \1=inode, \2=s:
1480 ;; GNU ls -hs suffixes the block count with a unit and
1481 ;; prints it as a float, FreeBSD does neither.
1482 (dired-re-inode-size "\\=\\s *\\([0-9]+\\s +\\)?\
1483 \\(?:\\([0-9]+\\(?:\\.[0-9]*\\)?[BkKMGTPEZY]?\\)? ?\\)"))
1486 (search-forward-regexp dired-re-inode-size nil t
)
1487 ;; XXX Might be a size not followed by a unit prefix.
1488 ;; We could set s to inode if it were otherwise nil,
1489 ;; with a similar reasoning as below for setting gid to uid,
1490 ;; but it would be even more whimsical.
1491 (setq inode
(when (match-string 1)
1492 (string-to-number (match-string 1))))
1493 (setq s
(when (match-string 2)
1494 (dired-x--string-to-number (match-string 2))))
1495 (setq mode
(buffer-substring (point) (+ mode-len
(point))))
1496 (forward-char mode-len
)
1497 ;; Skip any extended attributes marker ("." or "+").
1498 (or (looking-at " ")
1500 (setq nlink
(read (current-buffer)))
1501 ;; Karsten Wenger <kw@cis.uni-muenchen.de> fixed uid.
1502 ;; Another issue is that GNU ls -n right-justifies numerical
1503 ;; UIDs and GIDs, while FreeBSD left-justifies them, so
1504 ;; don't rely on a specific whitespace layout. Both of them
1505 ;; right-justify all other numbers, though.
1506 ;; XXX Return a number if the uid or gid seems to be
1508 (setq uid
(buffer-substring (progn
1509 (skip-chars-forward " \t")
1512 (skip-chars-forward "^ \t")
1514 (dired-move-to-filename)
1517 ;; The regexp below tries to match from the last
1518 ;; digit of the size field through a space after the
1519 ;; date. Also, dates may have different formats
1520 ;; depending on file age, so the date column need
1521 ;; not be aligned to the right.
1522 (buffer-substring (save-excursion
1523 (skip-chars-backward " \t")
1527 directory-listing-before-filename-regexp
)
1528 (skip-chars-forward "^ \t")
1530 size
(dired-x--string-to-number
1531 ;; We know that there's some kind of number
1532 ;; before point because the regexp search
1533 ;; above succeeded. I don't think it's worth
1534 ;; doing an extra check for leading garbage.
1535 (buffer-substring (point)
1537 (skip-chars-backward "^ \t")
1539 ;; If no gid is displayed, gid will be set to uid
1540 ;; but the user will then not reference it anyway in
1542 gid
(buffer-substring (progn
1543 (skip-chars-backward " \t")
1546 (skip-chars-backward "^ \t")
1548 (setq name
(buffer-substring (point)
1550 (dired-move-to-end-of-filename t
)
1552 sym
(if (looking-at " -> ")
1553 (buffer-substring (progn (forward-char 4) (point))
1554 (line-end-position))
1568 (format "'%s file" predicate
))))
1571 ;;; FIND FILE AT POINT.
1573 (defcustom dired-x-hands-off-my-keys t
1574 "Non-nil means don't remap `find-file' to `dired-x-find-file'.
1575 Similarly for `find-file-other-window' and `dired-x-find-file-other-window'.
1576 If you change this variable without using \\[customize] after `dired-x.el'
1577 is loaded then call \\[dired-x-bind-find-file]."
1579 :initialize
'custom-initialize-default
1580 :set
(lambda (symbol value
)
1582 (dired-x-bind-find-file))
1585 (defun dired-x-bind-find-file ()
1586 "Bind `dired-x-find-file' in place of `find-file' (or vice-versa).
1587 Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
1588 Binding direction based on `dired-x-hands-off-my-keys'."
1590 (if (called-interactively-p 'interactive
)
1591 (setq dired-x-hands-off-my-keys
1592 (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
1593 (define-key (current-global-map) [remap find-file
]
1594 (if (not dired-x-hands-off-my-keys
) 'dired-x-find-file
))
1595 (define-key (current-global-map) [remap find-file-other-window
]
1596 (if (not dired-x-hands-off-my-keys
) 'dired-x-find-file-other-window
)))
1598 ;; Now call it so binding is correct. This could go in the :initialize
1599 ;; slot, but then dired-x-bind-find-file has to be defined before the
1600 ;; defcustom, and we get free variable warnings.
1601 (dired-x-bind-find-file)
1603 (defun dired-x-find-file (filename)
1604 "Edit file FILENAME.
1605 Like `find-file', except that when called interactively with a
1606 prefix argument, it offers the filename near point as a default."
1607 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1608 (find-file filename
))
1610 (defun dired-x-find-file-other-window (filename)
1611 "Edit file FILENAME, in another window.
1612 Like `find-file-other-window', except that when called interactively with
1613 a prefix argument, when it offers the filename near point as a default."
1614 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1615 (find-file-other-window filename
))
1617 ;;; Internal functions.
1619 ;; Fixme: This should probably use `thing-at-point'. -- fx
1620 (defun dired-filename-at-point ()
1621 "Return the filename closest to point, expanded.
1622 Point should be in or after a filename."
1624 ;; First see if just past a filename.
1626 (when (looking-at-p "[] \t\n[{}()]") ; whitespace or some parens
1627 (skip-chars-backward " \n\t\r({[]})")
1628 (or (bobp) (backward-char 1))))
1629 (let ((filename-chars "-.[:alnum:]_/:$+@")
1631 (if (looking-at-p (format "[%s]" filename-chars
))
1633 (skip-chars-backward filename-chars
)
1636 ;; This is something to do with ange-ftp filenames.
1637 ;; It convert foo@bar to /foo@bar.
1638 ;; But when does the former occur in dired buffers?
1639 (and (string-match-p
1641 (buffer-substring start
(line-end-position)))
1643 (if (string-match-p "[/~]" (char-to-string (preceding-char)))
1644 (setq start
(1- start
)))
1645 (skip-chars-forward filename-chars
))
1646 (error "No file found around point!"))
1648 (expand-file-name (concat prefix
(buffer-substring start
(point)))))))
1650 (defun dired-x-read-filename-at-point (prompt)
1651 "Return filename prompting with PROMPT with completion.
1652 If `current-prefix-arg' is non-nil, uses name at point as guess."
1653 (if current-prefix-arg
1654 (let ((guess (dired-filename-at-point)))
1655 (read-file-name prompt
1656 (file-name-directory guess
)
1658 nil
(file-name-nondirectory guess
)))
1659 (read-file-name prompt default-directory
)))
1661 (define-obsolete-function-alias 'read-filename-at-point
1662 'dired-x-read-filename-at-point
"24.1") ; is this even needed?
1666 (define-obsolete-function-alias 'dired-x-submit-report
'report-emacs-bug
"24.1")
1669 ;; As Barry Warsaw would say: "This might be useful..."
1673 ;; byte-compile-dynamic: t
1674 ;; generated-autoload-file: "dired-loaddefs.el"
1677 ;;; dired-x.el ends here