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