Remove duplicate cl--random-state definition
[emacs.git] / lisp / dired-x.el
blob7ceb672bf2f0862c126e24de4a376e499be7149c
1 ;;; dired-x.el --- extra Dired functionality -*- lexical-binding:t -*-
3 ;; Copyright (C) 1993-1994, 1997, 2001-2017 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
7 ;; Lawrence R. Dodd <dodd@roebling.poly.edu>
8 ;; Maintainer: Romain Francoise <rfrancoise@gnu.org>
9 ;; Keywords: dired extensions files
10 ;; Package: emacs
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; This is based on Sebastian Kremer's excellent dired-x.el (Dired Extra),
30 ;; version 1.191, adapted for GNU Emacs. See the `dired-x' info pages.
32 ;; 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 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 ;; For backward compatibility
141 (define-obsolete-variable-alias 'dired-omit-files-p 'dired-omit-mode "22.1")
142 (define-minor-mode dired-omit-mode
143 "Toggle omission of uninteresting files in Dired (Dired-Omit mode).
144 With a prefix argument ARG, enable Dired-Omit mode if ARG is
145 positive, and disable it otherwise. If called from Lisp, enable
146 the mode if ARG is omitted or nil.
148 Dired-Omit mode is a buffer-local minor mode. When enabled in a
149 Dired buffer, Dired does not list files whose filenames match
150 regexp `dired-omit-files', nor files ending with extensions in
151 `dired-omit-extensions'.
153 To enable omitting in every Dired buffer, you can put this in
154 your init file:
156 (add-hook \\='dired-mode-hook (lambda () (dired-omit-mode)))
158 See Info node `(dired-x) Omitting Variables' for more information."
159 :group 'dired-x
160 (if dired-omit-mode
161 ;; This will mention how many lines were omitted:
162 (let ((dired-omit-size-limit nil)) (dired-omit-expunge))
163 (revert-buffer)))
165 (put 'dired-omit-mode 'safe-local-variable 'booleanp)
167 (defcustom dired-omit-files "^\\.?#\\|^\\.$\\|^\\.\\.$"
168 "Filenames matching this regexp will not be displayed.
169 This only has effect when `dired-omit-mode' is t. See interactive function
170 `dired-omit-mode' (\\[dired-omit-mode]) and variable
171 `dired-omit-extensions'. The default is to omit `.', `..', auto-save
172 files and lock files."
173 :type 'regexp
174 :group 'dired-x)
176 (defcustom dired-omit-verbose t
177 "When non-nil, show messages when omitting files.
178 When nil, don't show messages."
179 :version "24.1"
180 :type 'boolean
181 :group 'dired-x)
183 (defcustom dired-find-subdir nil ; t is pretty near to DWIM...
184 "If non-nil, Dired always finds a directory in a buffer of its own.
185 If nil, Dired finds the directory as a subdirectory in some other buffer
186 if it is present as one.
188 If there are several Dired buffers for a directory, the most recently
189 used is chosen.
191 Dired avoids switching to the current buffer, so that if you have
192 a normal and a wildcard buffer for the same directory, \\[dired] will
193 toggle between those two."
194 :type 'boolean
195 :group 'dired-x)
197 (defcustom dired-enable-local-variables t
198 "Control use of local-variables lists in Dired.
199 This temporarily overrides the value of `enable-local-variables' when
200 listing a directory. See also `dired-local-variables-file'."
201 :risky t
202 :type '(choice (const :tag "Query Unsafe" t)
203 (const :tag "Safe Only" :safe)
204 (const :tag "Do all" :all)
205 (const :tag "Ignore" nil)
206 (other :tag "Query" other))
207 :group 'dired-x)
209 (make-obsolete-variable 'dired-enable-local-variables
210 "use a standard `dir-locals-file' instead." "24.1")
212 (defcustom dired-guess-shell-gnutar
213 (catch 'found
214 (dolist (exe '("tar" "gtar"))
215 (if (with-temp-buffer
216 (ignore-errors (call-process exe nil t nil "--version"))
217 (and (re-search-backward "GNU tar" nil t) t))
218 (throw 'found exe))))
219 "If non-nil, name of GNU tar executable.
220 \(E.g., \"tar\" or \"gtar\"). The `z' switch will be used with it for
221 compressed or gzip'ed tar files. If you don't have GNU tar, set this
222 to nil: a pipe using `zcat' or `gunzip -c' will be used."
223 ;; Changed from system-type test to testing --version output.
224 ;; Maybe test --help for -z instead?
225 :version "24.1"
226 :type '(choice (const :tag "Not GNU tar" nil)
227 (string :tag "Command name"))
228 :group 'dired-x)
230 (defcustom dired-guess-shell-gzip-quiet t
231 "Non-nil says pass -q to gzip overriding verbose GZIP environment."
232 :type 'boolean
233 :group 'dired-x)
235 (defcustom dired-guess-shell-znew-switches nil
236 "If non-nil, then string of switches passed to `znew', example: \"-K\"."
237 :type '(choice (const :tag "None" nil)
238 (string :tag "Switches"))
239 :group 'dired-x)
241 (defcustom dired-clean-up-buffers-too t
242 "Non-nil means offer to kill buffers visiting files and dirs deleted in Dired."
243 :type 'boolean
244 :group 'dired-x)
246 ;;; KEY BINDINGS.
248 (define-key dired-mode-map "\C-x\M-o" 'dired-omit-mode)
249 (define-key dired-mode-map "*O" 'dired-mark-omitted)
250 (define-key dired-mode-map "\M-(" 'dired-mark-sexp)
251 (define-key dired-mode-map "*(" 'dired-mark-sexp)
252 (define-key dired-mode-map "*." 'dired-mark-extension)
253 (define-key dired-mode-map "\M-!" 'dired-smart-shell-command)
254 (define-key dired-mode-map "\M-G" 'dired-goto-subdir)
255 (define-key dired-mode-map "F" 'dired-do-find-marked-files)
256 (define-key dired-mode-map "Y" 'dired-do-relsymlink)
257 (define-key dired-mode-map "%Y" 'dired-do-relsymlink-regexp)
258 (define-key dired-mode-map "V" 'dired-do-run-mail)
260 ;;; MENU BINDINGS
262 (require 'easymenu)
264 (let ((menu (lookup-key dired-mode-map [menu-bar])))
265 (easy-menu-add-item menu '("Operate")
266 ["Find Files" dired-do-find-marked-files
267 :help "Find current or marked files"]
268 "Shell Command...")
269 (easy-menu-add-item menu '("Operate")
270 ["Relative Symlink to..." dired-do-relsymlink
271 :visible (fboundp 'make-symbolic-link)
272 :help "Make relative symbolic links for current or \
273 marked files"]
274 "Hardlink to...")
275 (easy-menu-add-item menu '("Mark")
276 ["Flag Extension..." dired-flag-extension
277 :help "Flag files with a certain extension for deletion"]
278 "Mark Executables")
279 (easy-menu-add-item menu '("Mark")
280 ["Mark Extension..." dired-mark-extension
281 :help "Mark files with a certain extension"]
282 "Unmark All")
283 (easy-menu-add-item menu '("Mark")
284 ["Mark Omitted" dired-mark-omitted
285 :help "Mark files matching `dired-omit-files' \
286 and `dired-omit-extensions'"]
287 "Unmark All")
288 (easy-menu-add-item menu '("Regexp")
289 ["Relative Symlink..." dired-do-relsymlink-regexp
290 :visible (fboundp 'make-symbolic-link)
291 :help "Make relative symbolic links for files \
292 matching regexp"]
293 "Hardlink...")
294 (easy-menu-add-item menu '("Immediate")
295 ["Omit Mode" dired-omit-mode
296 :style toggle :selected dired-omit-mode
297 :help "Enable or disable omitting \"uninteresting\" \
298 files"]
299 "Refresh"))
302 ;; Install into appropriate hooks.
304 (add-hook 'dired-mode-hook 'dired-extra-startup)
305 (add-hook 'dired-after-readin-hook 'dired-omit-expunge)
307 (defun dired-extra-startup ()
308 "Automatically put on `dired-mode-hook' to get extra Dired features:
309 \\<dired-mode-map>
310 \\[dired-do-run-mail]\t-- run mail on folder (see `dired-bind-vm')
311 \\[dired-info]\t-- run info on file
312 \\[dired-man]\t-- run man on file
313 \\[dired-do-find-marked-files]\t-- visit all marked files simultaneously
314 \\[dired-omit-mode]\t-- toggle omitting of files
315 \\[dired-mark-sexp]\t-- mark by Lisp expression
317 To see the options you can set, use M-x customize-group RET dired-x RET.
318 See also the functions:
319 `dired-flag-extension'
320 `dired-virtual'
321 `dired-jump'
322 `dired-man'
323 `dired-vm'
324 `dired-rmail'
325 `dired-info'
326 `dired-do-find-marked-files'"
327 (interactive)
328 ;; These must be done in each new dired buffer.
329 (dired-hack-local-variables)
330 (dired-omit-startup))
333 ;;; EXTENSION MARKING FUNCTIONS.
335 (defun dired--mark-suffix-interactive-spec ()
336 (let* ((default
337 (let ((file (dired-get-filename nil t)))
338 (when file
339 (file-name-extension file))))
340 (suffix
341 (read-string (format "%s extension%s: "
342 (if (equal current-prefix-arg '(4))
343 "UNmarking"
344 "Marking")
345 (if default
346 (format " (default %s)" default)
347 "")) nil nil default))
348 (marker
349 (pcase current-prefix-arg
350 ('(4) ?\s)
351 ('(16)
352 (let* ((dflt (char-to-string dired-marker-char))
353 (input (read-string
354 (format
355 "Marker character to use (default %s): " dflt)
356 nil nil dflt)))
357 (aref input 0)))
358 (_ dired-marker-char))))
359 (list suffix marker)))
361 ;; Mark files with some extension.
362 (defun dired-mark-extension (extension &optional marker-char)
363 "Mark all files with a certain EXTENSION for use in later commands.
364 A `.' is automatically prepended to EXTENSION when not present.
365 EXTENSION may also be a list of extensions instead of a single one.
366 Optional MARKER-CHAR is marker to use.
367 Interactively, ask for EXTENSION.
368 Prefixed with one C-u, unmark files instead.
369 Prefixed with two C-u's, prompt for MARKER-CHAR and mark files with it."
370 (interactive (dired--mark-suffix-interactive-spec))
371 (unless (listp extension)
372 (setq extension (list extension)))
373 (dired-mark-files-regexp
374 (concat ".";; don't match names with nothing but an extension
375 "\\("
376 (mapconcat
377 (lambda (x)
378 (regexp-quote
379 (if (string-prefix-p "." x) x (concat "." x))))
380 extension "\\|")
381 "\\)$")
382 marker-char))
384 ;; Mark files ending with some suffix.
385 (defun dired-mark-suffix (suffix &optional marker-char)
386 "Mark all files with a certain SUFFIX for use in later commands.
387 A `.' is *not* automatically prepended to the string entered; see
388 also `dired-mark-extension', which is similar but automatically
389 prepends `.' when not present.
390 SUFFIX may also be a list of suffixes instead of a single one.
391 Optional MARKER-CHAR is marker to use.
392 Interactively, ask for SUFFIX.
393 Prefixed with one C-u, unmark files instead.
394 Prefixed with two C-u's, prompt for MARKER-CHAR and mark files with it."
395 (interactive (dired--mark-suffix-interactive-spec))
396 (unless (listp suffix)
397 (setq suffix (list suffix)))
398 (dired-mark-files-regexp
399 (concat ".";; don't match names with nothing but an extension
400 "\\("
401 (mapconcat 'regexp-quote suffix "\\|")
402 "\\)$")
403 marker-char))
405 (defun dired-flag-extension (extension)
406 "In Dired, flag all files with a certain EXTENSION for deletion.
407 A `.' is *not* automatically prepended to the string entered."
408 (interactive "sFlagging extension: ")
409 (dired-mark-extension extension dired-del-marker))
411 ;; Define some unpopular file extensions. Used for cleaning and omitting.
413 (defvar dired-patch-unclean-extensions
414 '(".rej" ".orig")
415 "List of extensions of dispensable files created by the `patch' program.")
417 (defvar dired-tex-unclean-extensions
418 '(".toc" ".log" ".aux");; these are already in completion-ignored-extensions
419 "List of extensions of dispensable files created by TeX.")
421 (defvar dired-latex-unclean-extensions
422 '(".idx" ".lof" ".lot" ".glo")
423 "List of extensions of dispensable files created by LaTeX.")
425 (defvar dired-bibtex-unclean-extensions
426 '(".blg" ".bbl")
427 "List of extensions of dispensable files created by BibTeX.")
429 (defvar dired-texinfo-unclean-extensions
430 '(".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
431 ".tp" ".tps" ".vr" ".vrs")
432 "List of extensions of dispensable files created by texinfo.")
434 (defun dired-clean-patch ()
435 "Flag dispensable files created by patch for deletion.
436 See variable `dired-patch-unclean-extensions'."
437 (interactive)
438 (dired-flag-extension dired-patch-unclean-extensions))
440 (defun dired-clean-tex ()
441 "Flag dispensable files created by [La]TeX etc. for deletion.
442 See variables `dired-tex-unclean-extensions',
443 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
444 `dired-texinfo-unclean-extensions'."
445 (interactive)
446 (dired-flag-extension (append dired-texinfo-unclean-extensions
447 dired-latex-unclean-extensions
448 dired-bibtex-unclean-extensions
449 dired-tex-unclean-extensions)))
451 (defun dired-very-clean-tex ()
452 "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
453 See variables `dired-texinfo-unclean-extensions',
454 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
455 `dired-texinfo-unclean-extensions'."
456 (interactive)
457 (dired-flag-extension (append dired-texinfo-unclean-extensions
458 dired-latex-unclean-extensions
459 dired-bibtex-unclean-extensions
460 dired-tex-unclean-extensions
461 (list ".dvi"))))
463 (defvar tar-superior-buffer)
464 ;;; JUMP.
466 ;;;###autoload
467 (defun dired-jump (&optional other-window file-name)
468 "Jump to Dired buffer corresponding to current buffer.
469 If in a file, Dired the current directory and move to file's line.
470 If in Dired already, pop up a level and goto old directory's line.
471 In case the proper Dired file line cannot be found, refresh the dired
472 buffer and try again.
473 When OTHER-WINDOW is non-nil, jump to Dired buffer in other window.
474 When FILE-NAME is non-nil, jump to its line in Dired.
475 Interactively with prefix argument, read FILE-NAME."
476 (interactive
477 (list nil (and current-prefix-arg
478 (read-file-name "Jump to Dired file: "))))
479 (if (bound-and-true-p tar-subfile-mode)
480 (switch-to-buffer tar-superior-buffer)
481 ;; Expand file-name before `dired-goto-file' call:
482 ;; `dired-goto-file' requires its argument to be an absolute
483 ;; file name; the result of `read-file-name' could be
484 ;; an abbreviated file name (Bug#24409).
485 (let* ((file (or (and file-name (expand-file-name file-name))
486 buffer-file-name))
487 (dir (if file (file-name-directory file) default-directory)))
488 (if (and (eq major-mode 'dired-mode) (null file-name))
489 (progn
490 (setq dir (dired-current-directory))
491 (dired-up-directory other-window)
492 (unless (dired-goto-file dir)
493 ;; refresh and try again
494 (dired-insert-subdir (file-name-directory dir))
495 (dired-goto-file dir)))
496 (if other-window
497 (dired-other-window dir)
498 (dired dir))
499 (if file
500 (or (dired-goto-file file)
501 ;; refresh and try again
502 (progn
503 (dired-insert-subdir (file-name-directory file))
504 (dired-goto-file file))
505 ;; Toggle omitting, if it is on, and try again.
506 (when dired-omit-mode
507 (dired-omit-mode)
508 (dired-goto-file file))))))))
510 ;;;###autoload
511 (defun dired-jump-other-window (&optional file-name)
512 "Like \\[dired-jump] (`dired-jump') but in other window."
513 (interactive
514 (list (and current-prefix-arg
515 (read-file-name "Jump to Dired file: "))))
516 (dired-jump t file-name))
518 ;;; OMITTING.
520 ;; Enhanced omitting of lines from directory listings.
521 ;; Marked files are never omitted.
523 ;; should probably get rid of this and always use 'no-dir.
524 ;; sk 28-Aug-1991 09:37
525 (defvar dired-omit-localp 'no-dir
526 "The LOCALP argument `dired-omit-expunge' passes to `dired-get-filename'.
527 If it is `no-dir', omitting is much faster, but you can only match
528 against the non-directory part of the file name. Set it to nil if you
529 need to match the entire file name.")
531 ;; \017=^O for Omit - other packages can chose other control characters.
532 (defvar dired-omit-marker-char ?\017
533 "Temporary marker used by Dired-Omit.
534 Should never be used as marker by the user or other packages.")
536 (defun dired-omit-startup ()
537 (or (assq 'dired-omit-mode minor-mode-alist)
538 (setq minor-mode-alist
539 (append '((dired-omit-mode
540 (:eval (if (eq major-mode 'dired-mode)
541 " Omit" ""))))
542 minor-mode-alist))))
544 (defun dired-mark-omitted ()
545 "Mark files matching `dired-omit-files' and `dired-omit-extensions'."
546 (interactive)
547 (let ((dired-omit-mode nil)) (revert-buffer)) ;; Show omitted files
548 (dired-mark-unmarked-files (dired-omit-regexp) nil nil dired-omit-localp
549 (dired-omit-case-fold-p dired-directory)))
551 (defcustom dired-omit-extensions
552 (append completion-ignored-extensions
553 dired-latex-unclean-extensions
554 dired-bibtex-unclean-extensions
555 dired-texinfo-unclean-extensions)
556 "If non-nil, a list of extensions (strings) to omit from Dired listings.
557 Defaults to elements of `completion-ignored-extensions',
558 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
559 `dired-texinfo-unclean-extensions'.
561 See interactive function `dired-omit-mode' (\\[dired-omit-mode]) and
562 variables `dired-omit-mode' and `dired-omit-files'."
563 :type '(repeat string)
564 :group 'dired-x)
566 (defun dired-omit-expunge (&optional regexp)
567 "Erases all unmarked files matching REGEXP.
568 Does nothing if global variable `dired-omit-mode' is nil, or if called
569 non-interactively and buffer is bigger than `dired-omit-size-limit'.
570 If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
571 filenames ending in `dired-omit-extensions'.
572 If REGEXP is the empty string, this function is a no-op.
574 This functions works by temporarily binding `dired-marker-char' to
575 `dired-omit-marker-char' and calling `dired-do-kill-lines'."
576 (interactive "sOmit files (regexp): ")
577 (if (and dired-omit-mode
578 (or (called-interactively-p 'interactive)
579 (not dired-omit-size-limit)
580 (< (buffer-size) dired-omit-size-limit)
581 (progn
582 (when dired-omit-verbose
583 (message "Not omitting: directory larger than %d characters."
584 dired-omit-size-limit))
585 (setq dired-omit-mode nil)
586 nil)))
587 (let ((omit-re (or regexp (dired-omit-regexp)))
588 (old-modified-p (buffer-modified-p))
589 count)
590 (or (string= omit-re "")
591 (let ((dired-marker-char dired-omit-marker-char))
592 (when dired-omit-verbose (message "Omitting..."))
593 (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp
594 (dired-omit-case-fold-p dired-directory))
595 (progn
596 (setq count (dired-do-kill-lines
598 (if dired-omit-verbose "Omitted %d line%s." "")))
599 (force-mode-line-update))
600 (when dired-omit-verbose (message "(Nothing to omit)")))))
601 ;; Try to preserve modified state of buffer. So `%*' doesn't appear
602 ;; in mode-line of omitted buffers.
603 (set-buffer-modified-p (and old-modified-p
604 (save-excursion
605 (goto-char (point-min))
606 (re-search-forward dired-re-mark nil t))))
607 count)))
609 (defun dired-omit-regexp ()
610 (concat (if dired-omit-files (concat "\\(" dired-omit-files "\\)") "")
611 (if (and dired-omit-files dired-omit-extensions) "\\|" "")
612 (if dired-omit-extensions
613 (concat ".";; a non-extension part should exist
614 "\\("
615 (mapconcat 'regexp-quote dired-omit-extensions "\\|")
616 "\\)$")
617 "")))
619 ;; Returns t if any work was done, nil otherwise.
620 (defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp case-fold-p)
621 "Mark unmarked files matching REGEXP, displaying MSG.
622 REGEXP is matched against the entire file name. When called
623 interactively, prompt for REGEXP.
624 With prefix argument, unflag all those files.
625 Optional fourth argument LOCALP is as in `dired-get-filename'.
626 Optional fifth argument CASE-FOLD-P specifies the value of
627 `case-fold-search' used for matching REGEXP."
628 (interactive
629 (list (read-regexp
630 "Mark unmarked files matching regexp (default all): "
631 nil 'dired-regexp-history)
632 nil current-prefix-arg nil))
633 (let ((dired-marker-char (if unflag-p ?\s dired-marker-char)))
634 (dired-mark-if
635 (and
636 ;; not already marked
637 (= (following-char) ?\s)
638 ;; uninteresting
639 (let ((fn (dired-get-filename localp t))
640 ;; Match patterns case-insensitively on case-insensitive
641 ;; systems
642 (case-fold-search case-fold-p))
643 (and fn (string-match-p regexp fn))))
644 msg)))
647 ;;; VIRTUAL DIRED MODE.
649 ;; For browsing `ls -lR' listings in a dired-like fashion.
651 (defalias 'virtual-dired 'dired-virtual)
652 (defun dired-virtual (dirname &optional switches)
653 "Put this buffer into Virtual Dired mode.
655 In Virtual Dired mode, all commands that do not actually consult the
656 filesystem will work.
658 This is useful if you want to peruse and move around in an ls -lR
659 output file, for example one you got from an ftp server. With
660 ange-ftp, you can even Dired a directory containing an ls-lR file,
661 visit that file and turn on Virtual Dired mode. But don't try to save
662 this file, as dired-virtual indents the listing and thus changes the
663 buffer.
665 If you have save a Dired buffer in a file you can use \\[dired-virtual] to
666 resume it in a later session.
668 Type \\<dired-mode-map>\\[revert-buffer] \
669 in the Virtual Dired buffer and answer `y' to convert
670 the virtual to a real Dired buffer again. You don't have to do this, though:
671 you can relist single subdirs using \\[dired-do-redisplay]."
673 ;; DIRNAME is the top level directory of the buffer. It will become
674 ;; its `default-directory'. If nil, the old value of
675 ;; default-directory is used.
677 ;; Optional SWITCHES are the ls switches to use.
679 ;; Shell wildcards will be used if there already is a `wildcard'
680 ;; line in the buffer (thus it is a saved Dired buffer), but there
681 ;; is no other way to get wildcards. Insert a `wildcard' line by
682 ;; hand if you want them.
684 (interactive
685 (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
686 (goto-char (point-min))
687 (or (looking-at-p " ")
688 ;; if not already indented, do it now:
689 (indent-region (point-min) (point-max) 2))
690 (or dirname (setq dirname default-directory))
691 (setq dirname (expand-file-name (file-name-as-directory dirname)))
692 (setq default-directory dirname) ; contains no wildcards
693 (let ((wildcard (save-excursion
694 (goto-char (point-min))
695 (forward-line 1)
696 (and (looking-at "^ wildcard ")
697 (buffer-substring (match-end 0)
698 (line-end-position))))))
699 (if wildcard
700 (setq dirname (expand-file-name wildcard default-directory))))
701 ;; If raw ls listing (not a saved old dired buffer), give it a
702 ;; decent subdir headerline:
703 (goto-char (point-min))
704 (or (looking-at-p dired-subdir-regexp)
705 (insert " "
706 (directory-file-name (file-name-directory default-directory))
707 ":\n"))
708 (dired-mode dirname (or switches dired-listing-switches))
709 (setq mode-name "Virtual Dired"
710 revert-buffer-function 'dired-virtual-revert)
711 (set (make-local-variable 'dired-subdir-alist) nil)
712 (dired-build-subdir-alist)
713 (goto-char (point-min))
714 (dired-initial-position dirname))
716 (defun dired-virtual-guess-dir ()
717 "Guess and return appropriate working directory of this buffer.
718 The buffer is assumed to be in Dired or ls -lR format. The guess is
719 based upon buffer contents. If nothing could be guessed, returns
720 nil."
722 (let ((regexp "^\\( \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
723 (subexpr 2))
724 (goto-char (point-min))
725 (cond ((looking-at regexp)
726 ;; If a saved dired buffer, look to which dir and
727 ;; perhaps wildcard it belongs:
728 (let ((dir (buffer-substring (match-beginning subexpr)
729 (match-end subexpr))))
730 (file-name-as-directory dir)))
731 ;; Else no match for headerline found. It's a raw ls listing.
732 ;; In raw ls listings the directory does not have a headerline
733 ;; try parent of first subdir, if any
734 ((re-search-forward regexp nil t)
735 (file-name-directory
736 (directory-file-name
737 (file-name-as-directory
738 (buffer-substring (match-beginning subexpr)
739 (match-end subexpr))))))
740 (t ; if all else fails
741 nil))))
744 (defun dired-virtual-revert (&optional _arg _noconfirm)
745 (if (not
746 (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
747 (error "Cannot revert a Virtual Dired buffer")
748 (setq mode-name "Dired"
749 revert-buffer-function 'dired-revert)
750 (revert-buffer)))
752 ;; A zero-arg version of dired-virtual.
753 (defun dired-virtual-mode ()
754 "Put current buffer into Virtual Dired mode (see `dired-virtual').
755 Useful on `magic-mode-alist' with the regexp
757 \"^ \\\\(/[^ /]+\\\\)+/?:$\"
759 to put saved Dired buffers automatically into Virtual Dired mode.
761 Also useful for `auto-mode-alist' like this:
763 (add-to-list \\='auto-mode-alist
764 \\='(\"[^/]\\\\.dired\\\\\\='\" . dired-virtual-mode))"
765 (interactive)
766 (dired-virtual (dired-virtual-guess-dir)))
769 ;;; SMART SHELL.
771 ;; An Emacs buffer can have but one working directory, stored in the
772 ;; buffer-local variable `default-directory'. A Dired buffer may have
773 ;; several subdirectories inserted, but still has but one working directory:
774 ;; that of the top level Dired directory in that buffer. For some commands
775 ;; it is appropriate that they use the current Dired directory instead of
776 ;; `default-directory', e.g., `find-file' and `compile'. This is a general
777 ;; mechanism is provided for special handling of the working directory in
778 ;; special major modes.
780 (define-obsolete-variable-alias 'default-directory-alist
781 'dired-default-directory-alist "24.1")
783 ;; It's easier to add to this alist than redefine function
784 ;; default-directory while keeping the old information.
785 (defconst dired-default-directory-alist
786 '((dired-mode . (if (fboundp 'dired-current-directory)
787 (dired-current-directory)
788 default-directory)))
789 "Alist of major modes and their opinion on `default-directory'.
790 Each element has the form (MAJOR . EXPRESSION).
791 The function `dired-default-directory' evaluates EXPRESSION to
792 determine a default directory.")
794 (put 'dired-default-directory-alist 'risky-local-variable t) ; gets eval'd
795 (make-obsolete-variable 'dired-default-directory-alist
796 "this feature is due to be removed." "24.1")
798 (defun dired-default-directory ()
799 "Return the `dired-default-directory-alist' entry for the current major-mode.
800 If none, return `default-directory'."
801 ;; It looks like this was intended to be something of a "general"
802 ;; feature, but it only ever seems to have been used in
803 ;; dired-smart-shell-command, and doesn't seem worth keeping around.
804 (declare (obsolete nil "24.1"))
805 (or (eval (cdr (assq major-mode dired-default-directory-alist)))
806 default-directory))
808 (defun dired-smart-shell-command (command &optional output-buffer error-buffer)
809 "Like function `shell-command', but in the current Virtual Dired directory."
810 (interactive
811 (list
812 (read-shell-command "Shell command: " nil nil
813 (cond
814 (buffer-file-name (file-relative-name buffer-file-name))
815 ((eq major-mode 'dired-mode) (dired-get-filename t t))))
816 current-prefix-arg
817 shell-command-default-error-buffer))
818 (let ((default-directory (or (and (eq major-mode 'dired-mode)
819 (dired-current-directory))
820 default-directory)))
821 (shell-command command output-buffer error-buffer)))
824 ;;; LOCAL VARIABLES FOR DIRED BUFFERS.
826 ;; Brief Description (This feature is obsolete as of Emacs 24.1)
828 ;; * `dired-extra-startup' is part of the `dired-mode-hook'.
830 ;; * `dired-extra-startup' calls `dired-hack-local-variables'
832 ;; * `dired-hack-local-variables' checks the value of
833 ;; `dired-local-variables-file'
835 ;; * Check if `dired-local-variables-file' is a non-nil string and is a
836 ;; filename found in the directory of the Dired Buffer being created.
838 ;; * If `dired-local-variables-file' satisfies the above, then temporarily
839 ;; include it in the Dired Buffer at the bottom.
841 ;; * Set `enable-local-variables' temporarily to the user variable
842 ;; `dired-enable-local-variables' and run `hack-local-variables' on the
843 ;; Dired Buffer.
845 (defcustom dired-local-variables-file (convert-standard-filename ".dired")
846 "Filename, as string, containing local Dired buffer variables to be hacked.
847 If this file found in current directory, then it will be inserted into dired
848 buffer and `hack-local-variables' will be run. See Info node
849 `(emacs)File Variables' for more information on local variables.
850 See also `dired-enable-local-variables'."
851 :type 'file
852 :group 'dired)
854 (make-obsolete-variable 'dired-local-variables-file 'dir-locals-file "24.1")
856 (defun dired-hack-local-variables ()
857 "Evaluate local variables in `dired-local-variables-file' for Dired buffer."
858 (declare (obsolete hack-dir-local-variables-non-file-buffer "24.1"))
859 (and (stringp dired-local-variables-file)
860 (file-exists-p dired-local-variables-file)
861 (let ((opoint (point-max))
862 (inhibit-read-only t)
863 ;; In case user has `enable-local-variables' set to nil we
864 ;; override it locally with dired's variable.
865 (enable-local-variables dired-enable-local-variables))
866 ;; Insert 'em.
867 (save-excursion
868 (goto-char opoint)
869 (insert "\^L\n")
870 (insert-file-contents dired-local-variables-file))
871 ;; Hack 'em.
872 (unwind-protect
873 (let ((buffer-file-name dired-local-variables-file))
874 (hack-local-variables))
875 ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
876 (delete-region opoint (point-max)))
877 ;; Make sure that the mode line shows the proper information.
878 (dired-sort-set-mode-line))))
880 ;; Does not seem worth a dedicated command.
881 ;; See the more general features in files-x.el.
882 (defun dired-omit-here-always ()
883 "Create `dir-locals-file' setting `dired-omit-mode' to t in `dired-mode'.
884 If in a Dired buffer, reverts it."
885 (declare (obsolete add-dir-local-variable "24.1"))
886 (interactive)
887 (if (file-exists-p dired-local-variables-file)
888 (error "Old-style dired-local-variables-file `./%s' found;
889 replace it with a dir-locals-file `./%s'"
890 dired-local-variables-file
891 dir-locals-file))
892 (if (file-exists-p dir-locals-file)
893 (message "File `./%s' already exists." dir-locals-file)
894 (add-dir-local-variable 'dired-mode 'subdirs nil)
895 (add-dir-local-variable 'dired-mode 'dired-omit-mode t)
896 ;; Run extra-hooks and revert directory.
897 (when (derived-mode-p 'dired-mode)
898 (hack-dir-local-variables-non-file-buffer)
899 (dired-extra-startup)
900 (dired-revert))))
903 ;;; GUESS SHELL COMMAND.
905 ;; Brief Description:
907 ;; * `dired-do-shell-command' is bound to `!' by dired.el.
909 ;; * `dired-guess-shell-command' provides smarter defaults for
910 ;;; dired-aux.el's `dired-read-shell-command'.
912 ;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
913 ;;; marked files.
915 ;; * Parse `dired-guess-shell-alist-user' and
916 ;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
917 ;;; that matches the first file in the file list.
919 ;; * If the REGEXP matches all the entries of the file list then evaluate
920 ;;; COMMAND, which is either a string or a Lisp expression returning a
921 ;;; string. COMMAND may be a list of commands.
923 ;; * Return this command to `dired-guess-shell-command' which prompts user
924 ;;; with it. The list of commands is put into the list of default values.
925 ;;; If a command is used successfully then it is stored permanently in
926 ;;; `dired-shell-command-history'.
928 ;; Guess what shell command to apply to a file.
929 (defvar dired-shell-command-history nil
930 "History list for commands that read dired-shell commands.")
932 ;; Default list of shell commands.
934 ;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
935 ;; install GNU zip's version of zcat.
937 (autoload 'Man-support-local-filenames "man")
939 (defvar dired-guess-shell-alist-default
940 (list
941 (list "\\.tar\\'"
942 '(if dired-guess-shell-gnutar
943 (concat dired-guess-shell-gnutar " xvf")
944 "tar xvf")
945 ;; Extract files into a separate subdirectory
946 '(if dired-guess-shell-gnutar
947 (concat "mkdir " (file-name-sans-extension file)
948 "; " dired-guess-shell-gnutar " -C "
949 (file-name-sans-extension file) " -xvf")
950 (concat "mkdir " (file-name-sans-extension file)
951 "; tar -C " (file-name-sans-extension file) " -xvf"))
952 ;; List archive contents.
953 '(if dired-guess-shell-gnutar
954 (concat dired-guess-shell-gnutar " tvf")
955 "tar tvf"))
957 ;; REGEXPS for compressed archives must come before the .Z rule to
958 ;; be recognized:
959 (list "\\.tar\\.Z\\'"
960 ;; Untar it.
961 '(if dired-guess-shell-gnutar
962 (concat dired-guess-shell-gnutar " zxvf")
963 (concat "zcat * | tar xvf -"))
964 ;; Optional conversion to gzip format.
965 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
966 " " dired-guess-shell-znew-switches))
968 ;; gzip'ed archives
969 (list "\\.t\\(ar\\.\\)?gz\\'"
970 '(if dired-guess-shell-gnutar
971 (concat dired-guess-shell-gnutar " zxvf")
972 (concat "gunzip -qc * | tar xvf -"))
973 ;; Extract files into a separate subdirectory
974 '(if dired-guess-shell-gnutar
975 (concat "mkdir " (file-name-sans-extension file)
976 "; " dired-guess-shell-gnutar " -C "
977 (file-name-sans-extension file) " -zxvf")
978 (concat "mkdir " (file-name-sans-extension file)
979 "; gunzip -qc * | tar -C "
980 (file-name-sans-extension file) " -xvf -"))
981 ;; Optional decompression.
982 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" ""))
983 ;; List archive contents.
984 '(if dired-guess-shell-gnutar
985 (concat dired-guess-shell-gnutar " ztvf")
986 (concat "gunzip -qc * | tar tvf -")))
988 ;; bzip2'ed archives
989 (list "\\.t\\(ar\\.bz2\\|bz\\)\\'"
990 "bunzip2 -c * | tar xvf -"
991 ;; Extract files into a separate subdirectory
992 '(concat "mkdir " (file-name-sans-extension file)
993 "; bunzip2 -c * | tar -C "
994 (file-name-sans-extension file) " -xvf -")
995 ;; Optional decompression.
996 "bunzip2")
998 ;; xz'ed archives
999 (list "\\.t\\(ar\\.\\)?xz\\'"
1000 "unxz -c * | tar xvf -"
1001 ;; Extract files into a separate subdirectory
1002 '(concat "mkdir " (file-name-sans-extension file)
1003 "; unxz -c * | tar -C "
1004 (file-name-sans-extension file) " -xvf -")
1005 ;; Optional decompression.
1006 "unxz")
1008 '("\\.shar\\.Z\\'" "zcat * | unshar")
1009 '("\\.shar\\.g?z\\'" "gunzip -qc * | unshar")
1011 '("\\.e?ps\\'" "ghostview" "xloadimage" "lpr")
1012 (list "\\.e?ps\\.g?z\\'" "gunzip -qc * | ghostview -"
1013 ;; Optional decompression.
1014 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1015 (list "\\.e?ps\\.Z\\'" "zcat * | ghostview -"
1016 ;; Optional conversion to gzip format.
1017 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1018 " " dired-guess-shell-znew-switches))
1020 '("\\.patch\\'" "cat * | patch")
1021 (list "\\.patch\\.g?z\\'" "gunzip -qc * | patch"
1022 ;; Optional decompression.
1023 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1024 (list "\\.patch\\.Z\\'" "zcat * | patch"
1025 ;; Optional conversion to gzip format.
1026 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1027 " " dired-guess-shell-znew-switches))
1029 ;; The following four extensions are useful with dired-man ("N" key)
1030 ;; FIXME "man ./" does not work with dired-do-shell-command,
1031 ;; because there seems to be no way for us to modify the filename,
1032 ;; only the command. Hmph. `dired-man' works though.
1033 (list "\\.\\(?:[0-9]\\|man\\)\\'"
1034 '(let ((loc (Man-support-local-filenames)))
1035 (cond ((eq loc 'man-db) "man -l")
1036 ((eq loc 'man) "man ./")
1038 "cat * | tbl | nroff -man -h | col -b"))))
1039 (list "\\.\\(?:[0-9]\\|man\\)\\.g?z\\'"
1040 '(let ((loc (Man-support-local-filenames)))
1041 (cond ((eq loc 'man-db)
1042 "man -l")
1043 ((eq loc 'man)
1044 "man ./")
1045 (t "gunzip -qc * | tbl | nroff -man -h | col -b")))
1046 ;; Optional decompression.
1047 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1048 (list "\\.[0-9]\\.Z\\'"
1049 '(let ((loc (Man-support-local-filenames)))
1050 (cond ((eq loc 'man-db) "man -l")
1051 ((eq loc 'man) "man ./")
1052 (t "zcat * | tbl | nroff -man -h | col -b")))
1053 ;; Optional conversion to gzip format.
1054 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1055 " " dired-guess-shell-znew-switches))
1056 '("\\.pod\\'" "perldoc" "pod2man * | nroff -man")
1058 '("\\.dvi\\'" "xdvi" "dvips") ; preview and printing
1059 '("\\.au\\'" "play") ; play Sun audiofiles
1060 '("\\.mpe?g\\'\\|\\.avi\\'" "xine -p")
1061 '("\\.ogg\\'" "ogg123")
1062 '("\\.mp3\\'" "mpg123")
1063 '("\\.wav\\'" "play")
1064 '("\\.uu\\'" "uudecode") ; for uudecoded files
1065 '("\\.hqx\\'" "mcvert")
1066 '("\\.sh\\'" "sh") ; execute shell scripts
1067 '("\\.xbm\\'" "bitmap") ; view X11 bitmaps
1068 '("\\.gp\\'" "gnuplot")
1069 '("\\.p[bgpn]m\\'" "xloadimage")
1070 '("\\.gif\\'" "xloadimage") ; view gif pictures
1071 '("\\.tif\\'" "xloadimage")
1072 '("\\.png\\'" "display") ; xloadimage 4.1 doesn't grok PNG
1073 '("\\.jpe?g\\'" "xloadimage")
1074 '("\\.fig\\'" "xfig") ; edit fig pictures
1075 '("\\.out\\'" "xgraph") ; for plotting purposes.
1076 '("\\.tex\\'" "latex" "tex")
1077 '("\\.texi\\(nfo\\)?\\'" "makeinfo" "texi2dvi")
1078 '("\\.pdf\\'" "xpdf")
1079 '("\\.doc\\'" "antiword" "strings")
1080 '("\\.rpm\\'" "rpm -qilp" "rpm -ivh")
1081 '("\\.dia\\'" "dia")
1082 '("\\.mgp\\'" "mgp")
1084 ;; Some other popular archivers.
1085 (list "\\.zip\\'" "unzip" "unzip -l"
1086 ;; Extract files into a separate subdirectory
1087 '(concat "unzip" (if dired-guess-shell-gzip-quiet " -q")
1088 " -d " (file-name-sans-extension file)))
1089 '("\\.zoo\\'" "zoo x//")
1090 '("\\.lzh\\'" "lharc x")
1091 '("\\.arc\\'" "arc x")
1092 '("\\.shar\\'" "unshar")
1093 '("\\.rar\\'" "unrar x")
1094 '("\\.7z\\'" "7z x")
1096 ;; Compression.
1097 (list "\\.g?z\\'" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1098 (list "\\.dz\\'" "dictunzip")
1099 (list "\\.bz2\\'" "bunzip2")
1100 (list "\\.xz\\'" "unxz")
1101 (list "\\.Z\\'" "uncompress"
1102 ;; Optional conversion to gzip format.
1103 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1104 " " dired-guess-shell-znew-switches))
1106 '("\\.sign?\\'" "gpg --verify"))
1108 "Default alist used for shell command guessing.
1109 See `dired-guess-shell-alist-user'.")
1111 (defcustom dired-guess-shell-alist-user nil
1112 "User-defined alist of rules for suggested commands.
1113 These rules take precedence over the predefined rules in the variable
1114 `dired-guess-shell-alist-default' (to which they are prepended).
1116 Each element of this list looks like
1118 (REGEXP COMMAND...)
1120 where each COMMAND can either be a string or a Lisp expression that evaluates
1121 to a string. This expression can access the file name as the variable `file'.
1122 If several COMMANDs are given, the first one will be the default
1123 and the rest will be added temporarily to the history and can be retrieved
1124 with \\[previous-history-element] (M-p) .
1126 The variable `dired-guess-shell-case-fold-search' controls whether
1127 REGEXP is matched case-sensitively."
1128 :group 'dired-x
1129 :type '(alist :key-type regexp :value-type (repeat sexp)))
1131 (defcustom dired-guess-shell-case-fold-search t
1132 "If non-nil, `dired-guess-shell-alist-default' and
1133 `dired-guess-shell-alist-user' are matched case-insensitively."
1134 :group 'dired-x
1135 :type 'boolean)
1137 (defun dired-guess-default (files)
1138 "Return a shell command, or a list of commands, appropriate for FILES.
1139 See `dired-guess-shell-alist-user'."
1141 (let* ((case-fold-search dired-guess-shell-case-fold-search)
1142 ;; Prepend the user's alist to the default alist.
1143 (alist (append dired-guess-shell-alist-user
1144 dired-guess-shell-alist-default))
1145 (file (car files))
1146 (flist (cdr files))
1147 elt regexp cmds)
1149 ;; Find the first match in the alist for first file in FILES.
1150 (while alist
1151 (setq elt (car alist)
1152 regexp (car elt)
1153 alist (cdr alist))
1154 (if (string-match-p regexp file)
1155 (setq cmds (cdr elt)
1156 alist nil)))
1158 ;; If more than one file, see if all of FILES match regular expression.
1159 (while (and flist
1160 (string-match-p regexp (car flist)))
1161 (setq flist (cdr flist)))
1163 ;; If flist is still non-nil, then do not guess since this means that not
1164 ;; all the files in FILES were matched by the regexp.
1165 (setq cmds (and (not flist) cmds))
1167 ;; Return commands or nil if flist is still non-nil.
1168 ;; Evaluate the commands in order that any logical testing will be done.
1169 (if (cdr cmds)
1170 (delete-dups (mapcar (lambda (cmd) (eval cmd `((file . ,file)))) cmds))
1171 (eval (car cmds) `((file . ,file)))))) ; single command
1173 (defun dired-guess-shell-command (prompt files)
1174 "Ask user with PROMPT for a shell command, guessing a default from FILES."
1175 (let ((default (dired-guess-default files))
1176 default-list val)
1177 (if (null default)
1178 ;; Nothing to guess
1179 (read-shell-command prompt nil 'dired-shell-command-history)
1180 (setq prompt (replace-regexp-in-string ": $" " " prompt))
1181 (if (listp default)
1182 ;; More than one guess
1183 (setq default-list default
1184 default (car default)
1185 prompt (concat
1186 prompt
1187 (format "{%d guesses} " (length default-list))))
1188 ;; Just one guess
1189 (setq default-list (list default)))
1190 ;; Put the first guess in the prompt but not in the initial value.
1191 (setq prompt (concat prompt (format "[%s]: " default)))
1192 ;; All guesses can be retrieved with M-n
1193 (setq val (read-shell-command prompt nil
1194 'dired-shell-command-history
1195 default-list))
1196 ;; If we got a return, then return default.
1197 (if (equal val "") default val))))
1200 ;;; RELATIVE SYMBOLIC LINKS.
1202 (declare-function make-symbolic-link "fileio.c")
1204 (defvar dired-keep-marker-relsymlink ?S
1205 "See variable `dired-keep-marker-move'.")
1207 (defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
1208 "Make a symbolic link (pointing to FILE1) in FILE2.
1209 The link is relative (if possible), for example
1211 \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
1213 results in
1215 \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
1216 (interactive "FRelSymLink: \nFRelSymLink %s: \np")
1217 (let (name1 name2 len1 len2 (index 0) sub)
1218 (setq file1 (expand-file-name file1)
1219 file2 (expand-file-name file2)
1220 len1 (length file1)
1221 len2 (length file2))
1222 ;; Find common initial file name components:
1223 (let (next)
1224 (while (and (setq next (string-match "/" file1 index))
1225 (< (setq next (1+ next)) (min len1 len2))
1226 ;; For the comparison, both substrings must end in
1227 ;; `/', so NEXT is *one plus* the result of the
1228 ;; string-match.
1229 ;; E.g., consider the case of linking "/tmp/a/abc"
1230 ;; to "/tmp/abc" erroneously giving "/tmp/a" instead
1231 ;; of "/tmp/" as common initial component
1232 (string-equal (substring file1 0 next)
1233 (substring file2 0 next)))
1234 (setq index next))
1235 (setq name2 file2
1236 sub (substring file1 0 index)
1237 name1 (substring file1 index)))
1238 (if (string-equal sub "/")
1239 ;; No common initial file name found
1240 (setq name1 file1)
1241 ;; Else they have a common parent directory
1242 (let ((tem (substring file2 index))
1243 (start 0)
1244 (count 0))
1245 ;; Count number of slashes we must compensate for ...
1246 (while (setq start (string-match "/" tem start))
1247 (setq count (1+ count)
1248 start (1+ start)))
1249 ;; ... and prepend a "../" for each slash found:
1250 (dotimes (_ count)
1251 (setq name1 (concat "../" name1)))))
1252 (make-symbolic-link
1253 (directory-file-name name1) ; must not link to foo/
1254 ; (trailing slash!)
1255 name2 ok-if-already-exists)))
1257 (autoload 'dired-do-create-files "dired-aux")
1259 ;;;###autoload
1260 (defun dired-do-relsymlink (&optional arg)
1261 "Relative symlink all marked (or next ARG) files into a directory.
1262 Otherwise make a relative symbolic link to the current file.
1263 This creates relative symbolic links like
1265 foo -> ../bar/foo
1267 not absolute ones like
1269 foo -> /ugly/file/name/that/may/change/any/day/bar/foo
1271 For absolute symlinks, use \\[dired-do-symlink]."
1272 (interactive "P")
1273 (dired-do-create-files 'relsymlink #'dired-make-relative-symlink
1274 "RelSymLink" arg dired-keep-marker-relsymlink))
1276 (autoload 'dired-mark-read-regexp "dired-aux")
1277 (autoload 'dired-do-create-files-regexp "dired-aux")
1279 (defun dired-do-relsymlink-regexp (regexp newname &optional arg whole-name)
1280 "RelSymlink all marked files containing REGEXP to NEWNAME.
1281 See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
1282 for more info."
1283 (interactive (dired-mark-read-regexp "RelSymLink"))
1284 (dired-do-create-files-regexp
1285 #'dired-make-relative-symlink
1286 "RelSymLink" arg regexp newname whole-name dired-keep-marker-relsymlink))
1289 ;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
1291 ;; Brief Description:
1293 ;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
1295 ;; * Use `dired-get-marked-files' to collect the marked files in the current
1296 ;;; Dired Buffer into a list of filenames `FILE-LIST'.
1298 ;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
1299 ;;; `dired-do-find-marked-files''s prefix argument NOSELECT.
1301 ;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
1302 ;;; list each time.
1304 ;; * If NOSELECT is non-nil then just run `find-file-noselect' on each
1305 ;;; element of FILE-LIST.
1307 ;; * If NOSELECT is nil then calculate the `size' of the window for each file
1308 ;;; by dividing the `window-height' by length of FILE-LIST. Thus, `size' is
1309 ;;; cognizant of the window-configuration.
1311 ;; * If `size' is too small abort, otherwise run `find-file' on each element
1312 ;;; of FILE-LIST giving each a window of height `size'.
1314 (defun dired-do-find-marked-files (&optional noselect)
1315 "Find all marked files displaying all of them simultaneously.
1316 With optional NOSELECT just find files but do not select them.
1318 The current window is split across all files marked, as evenly as possible.
1319 Remaining lines go to bottom-most window. The number of files that can be
1320 displayed this way is restricted by the height of the current window and
1321 `window-min-height'.
1323 To keep Dired buffer displayed, type \\[split-window-below] first.
1324 To display just marked files, type \\[delete-other-windows] first."
1325 (interactive "P")
1326 (dired-simultaneous-find-file (dired-get-marked-files) noselect))
1328 (defun dired-simultaneous-find-file (file-list noselect)
1329 "Visit all files in FILE-LIST and display them simultaneously.
1330 The current window is split across all files in FILE-LIST, as evenly as
1331 possible. Remaining lines go to the bottom-most window. The number of
1332 files that can be displayed this way is restricted by the height of the
1333 current window and the variable `window-min-height'. With non-nil
1334 NOSELECT the files are merely found but not selected."
1335 ;; We don't make this function interactive because it is usually too clumsy
1336 ;; to specify FILE-LIST interactively unless via dired.
1337 (let (size)
1338 (if noselect
1339 ;; Do not select the buffer.
1340 (find-file-noselect (car file-list))
1341 ;; We will have to select the buffer. Calculate and check window size.
1342 (setq size (/ (window-height) (length file-list)))
1343 (or (<= window-min-height size)
1344 (error "Too many files to visit simultaneously. Try C-u prefix"))
1345 (find-file (car file-list)))
1346 ;; Decrement.
1347 (dolist (file (cdr file-list))
1348 (if noselect
1349 ;; Do not select the buffer.
1350 (find-file-noselect file)
1351 ;; Vertically split off a window of desired size. Upper window will
1352 ;; have SIZE lines. Select lower (larger) window. We split it again.
1353 (select-window (split-window nil size))
1354 (find-file file)))))
1357 ;;; MISCELLANEOUS COMMANDS.
1359 ;; Run man on files.
1361 (declare-function Man-getpage-in-background "man" (topic))
1363 (defvar manual-program) ; from man.el
1365 (defun dired-man ()
1366 "Run `man' on this file."
1367 ;; Used also to say: "Display old buffer if buffer name matches filename."
1368 ;; but I have no idea what that means.
1369 (interactive)
1370 (require 'man)
1371 (let* ((file (dired-get-filename))
1372 (manual-program (replace-regexp-in-string "\\*" "%s"
1373 (dired-guess-shell-command
1374 "Man command: " (list file)))))
1375 (Man-getpage-in-background file)))
1377 ;; Run Info on files.
1379 (defun dired-info ()
1380 "Run `info' on this file."
1381 (interactive)
1382 (info (dired-get-filename)))
1384 ;; Run mail on mail folders.
1386 (declare-function vm-visit-folder "ext:vm" (folder &optional read-only))
1387 (defvar vm-folder-directory)
1389 (defun dired-vm (&optional read-only)
1390 "Run VM on this file.
1391 With optional prefix argument, visits the folder read-only.
1392 Otherwise obeys the value of `dired-vm-read-only-folders'."
1393 (interactive "P")
1394 (let ((dir (dired-current-directory))
1395 (fil (dired-get-filename)))
1396 (vm-visit-folder fil (or read-only
1397 (eq t dired-vm-read-only-folders)
1398 (and dired-vm-read-only-folders
1399 (not (file-writable-p fil)))))
1400 ;; So that pressing `v' inside VM does prompt within current directory:
1401 (set (make-local-variable 'vm-folder-directory) dir)))
1403 (defun dired-rmail ()
1404 "Run RMAIL on this file."
1405 (interactive)
1406 (rmail (dired-get-filename)))
1408 (defun dired-do-run-mail ()
1409 "Visit the current file as a mailbox, using VM or RMAIL.
1410 Prompt for confirmation first; if the user says yes, call
1411 `dired-vm' if `dired-bind-vm' is non-nil, `dired-rmail'
1412 otherwise."
1413 (interactive)
1414 (let ((file (dired-get-filename t)))
1415 (if dired-bind-vm
1416 (if (y-or-n-p (format-message
1417 "Visit `%s' as a mail folder with VM?" file))
1418 (dired-vm))
1419 ;; Read mail folder using rmail.
1420 (if (y-or-n-p (format-message
1421 "Visit `%s' as a mailbox with RMAIL?" file))
1422 (dired-rmail)))))
1425 ;;; MISCELLANEOUS INTERNAL FUNCTIONS.
1427 ;; This should be a builtin
1428 (defun dired-buffer-more-recently-used-p (buffer1 buffer2)
1429 "Return t if BUFFER1 is more recently used than BUFFER2.
1430 Considers buffers closer to the car of `buffer-list' to be more recent."
1431 (and (not (equal buffer1 buffer2))
1432 (memq buffer1 (buffer-list))
1433 (not (memq buffer1 (memq buffer2 (buffer-list))))))
1436 ;; Needed if ls -lh is supported and also for GNU ls -ls.
1437 (defun dired-x--string-to-number (str)
1438 "Like `string-to-number' but recognize a trailing unit prefix.
1439 For example, 2K is expanded to 2048.0. The caller should make
1440 sure that a trailing letter in STR is one of BKkMGTPEZY."
1441 (let* ((val (string-to-number str))
1442 (u (unless (zerop val)
1443 (aref str (1- (length str))))))
1444 (when (and u (> u ?9))
1445 (when (= u ?k)
1446 (setq u ?K))
1447 (let ((units '(?B ?K ?M ?G ?T ?P ?E ?Z ?Y)))
1448 (while (and units (/= (pop units) u))
1449 (setq val (* 1024.0 val)))))
1450 val))
1452 (defun dired-mark-sexp (predicate &optional unflag-p)
1453 "Mark files for which PREDICATE returns non-nil.
1454 With a prefix arg, unmark or unflag those files instead.
1456 PREDICATE is a lisp expression that can refer to the following symbols:
1458 inode [integer] the inode of the file (only for ls -i output)
1459 s [integer] the size of the file for ls -s output
1460 (usually in blocks or, with -k, in KByte)
1461 mode [string] file permission bits, e.g. \"-rw-r--r--\"
1462 nlink [integer] number of links to file
1463 uid [string] owner
1464 gid [string] group (If the gid is not displayed by ls,
1465 this will still be set (to the same as uid))
1466 size [integer] file size in bytes
1467 time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
1468 name [string] the name of the file
1469 sym [string] if file is a symbolic link, the linked-to name, else \"\"
1471 For example, use
1473 (equal 0 size)
1475 to mark all zero length files.
1477 There's an ambiguity when a single integer not followed by a unit
1478 prefix precedes the file mode: It is then parsed as inode number
1479 and not as block size (this always works for GNU coreutils ls).
1481 Another limitation is that the uid field is needed for the
1482 function to work correctly. In particular, the field is not
1483 present for some values of `ls-lisp-emulation'.
1485 This function operates only on the buffer content and does not
1486 refer at all to the underlying file system. Contrast this with
1487 `find-dired', which might be preferable for the task at hand."
1488 ;; Using sym="" instead of nil avoids the trap of
1489 ;; (string-match "foo" sym) into which a user would soon fall.
1490 ;; Give `equal' instead of `=' in the example, as this works on
1491 ;; integers and strings.
1492 (interactive
1493 (list (read--expression
1494 (format "%s if (lisp expr): "
1495 (if current-prefix-arg
1496 "UNmark"
1497 "Mark")))
1498 current-prefix-arg))
1499 (message "%s" predicate)
1500 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))
1501 inode s mode nlink uid gid size time name sym)
1502 (dired-mark-if
1503 (save-excursion
1504 (and
1505 ;; Sets vars
1506 ;; inode s mode nlink uid gid size time name sym
1508 ;; according to current file line. Returns t for success, nil if
1509 ;; there is no file line. Upon success, all variables are set, either
1510 ;; to nil or the appropriate value, so they need not be initialized.
1511 ;; Moves point within the current line.
1512 (dired-move-to-filename)
1513 (let ((mode-len 10) ; length of mode string
1514 ;; like in dired.el, but with subexpressions \1=inode, \2=s:
1515 ;; GNU ls -hs suffixes the block count with a unit and
1516 ;; prints it as a float, FreeBSD does neither.
1517 (dired-re-inode-size "\\=\\s *\\([0-9]+\\s +\\)?\
1518 \\(?:\\([0-9]+\\(?:\\.[0-9]*\\)?[BkKMGTPEZY]?\\)? ?\\)"))
1519 (beginning-of-line)
1520 (forward-char 2)
1521 (search-forward-regexp dired-re-inode-size nil t)
1522 ;; XXX Might be a size not followed by a unit prefix.
1523 ;; We could set s to inode if it were otherwise nil,
1524 ;; with a similar reasoning as below for setting gid to uid,
1525 ;; but it would be even more whimsical.
1526 (setq inode (when (match-string 1)
1527 (string-to-number (match-string 1))))
1528 (setq s (when (match-string 2)
1529 (dired-x--string-to-number (match-string 2))))
1530 (setq mode (buffer-substring (point) (+ mode-len (point))))
1531 (forward-char mode-len)
1532 ;; Skip any extended attributes marker ("." or "+").
1533 (or (= (following-char) ?\s)
1534 (forward-char 1))
1535 (setq nlink (read (current-buffer)))
1536 ;; Karsten Wenger <kw@cis.uni-muenchen.de> fixed uid.
1537 ;; Another issue is that GNU ls -n right-justifies numerical
1538 ;; UIDs and GIDs, while FreeBSD left-justifies them, so
1539 ;; don't rely on a specific whitespace layout. Both of them
1540 ;; right-justify all other numbers, though.
1541 ;; XXX Return a number if the uid or gid seems to be
1542 ;; numerical?
1543 (setq uid (buffer-substring (progn
1544 (skip-chars-forward " \t")
1545 (point))
1546 (progn
1547 (skip-chars-forward "^ \t")
1548 (point))))
1549 (dired-move-to-filename)
1550 (save-excursion
1551 (setq time
1552 ;; The regexp below tries to match from the last
1553 ;; digit of the size field through a space after the
1554 ;; date. Also, dates may have different formats
1555 ;; depending on file age, so the date column need
1556 ;; not be aligned to the right.
1557 (buffer-substring (save-excursion
1558 (skip-chars-backward " \t")
1559 (point))
1560 (progn
1561 (re-search-backward
1562 directory-listing-before-filename-regexp)
1563 (skip-chars-forward "^ \t")
1564 (1+ (point))))
1565 size (dired-x--string-to-number
1566 ;; We know that there's some kind of number
1567 ;; before point because the regexp search
1568 ;; above succeeded. I don't think it's worth
1569 ;; doing an extra check for leading garbage.
1570 (buffer-substring (point)
1571 (progn
1572 (skip-chars-backward "^ \t")
1573 (point))))
1574 ;; If no gid is displayed, gid will be set to uid
1575 ;; but the user will then not reference it anyway in
1576 ;; PREDICATE.
1577 gid (buffer-substring (progn
1578 (skip-chars-backward " \t")
1579 (point))
1580 (progn
1581 (skip-chars-backward "^ \t")
1582 (point)))))
1583 (setq name (buffer-substring (point)
1585 (dired-move-to-end-of-filename t)
1586 (point)))
1587 sym (if (looking-at " -> ")
1588 (buffer-substring (progn (forward-char 4) (point))
1589 (line-end-position))
1590 ""))
1592 (eval predicate
1593 `((inode . ,inode)
1594 (s . ,s)
1595 (mode . ,mode)
1596 (nlink . ,nlink)
1597 (uid . ,uid)
1598 (gid . ,gid)
1599 (size . ,size)
1600 (time . ,time)
1601 (name . ,name)
1602 (sym . ,sym)))))
1603 (format "'%s file" predicate))))
1606 ;;; FIND FILE AT POINT.
1608 (defcustom dired-x-hands-off-my-keys t
1609 "Non-nil means don't remap `find-file' to `dired-x-find-file'.
1610 Similarly for `find-file-other-window' and `dired-x-find-file-other-window'.
1611 If you change this variable without using \\[customize] after `dired-x.el'
1612 is loaded then call \\[dired-x-bind-find-file]."
1613 :type 'boolean
1614 :initialize 'custom-initialize-default
1615 :set (lambda (symbol value)
1616 (set symbol value)
1617 (dired-x-bind-find-file))
1618 :group 'dired-x)
1620 (defun dired-x-bind-find-file ()
1621 "Bind `dired-x-find-file' in place of `find-file' (or vice-versa).
1622 Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
1623 Binding direction based on `dired-x-hands-off-my-keys'."
1624 (interactive)
1625 (if (called-interactively-p 'interactive)
1626 (setq dired-x-hands-off-my-keys
1627 (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
1628 (define-key (current-global-map) [remap find-file]
1629 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file))
1630 (define-key (current-global-map) [remap find-file-other-window]
1631 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file-other-window)))
1633 ;; Now call it so binding is correct. This could go in the :initialize
1634 ;; slot, but then dired-x-bind-find-file has to be defined before the
1635 ;; defcustom, and we get free variable warnings.
1636 (dired-x-bind-find-file)
1638 (defun dired-x-find-file (filename)
1639 "Edit file FILENAME.
1640 Like `find-file', except that when called interactively with a
1641 prefix argument, it offers the filename near point as a default."
1642 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1643 (find-file filename))
1645 (defun dired-x-find-file-other-window (filename)
1646 "Edit file FILENAME, in another window.
1647 Like `find-file-other-window', except that when called interactively with
1648 a prefix argument, when it offers the filename near point as a default."
1649 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1650 (find-file-other-window filename))
1652 ;;; Internal functions.
1654 ;; Fixme: This should probably use `thing-at-point'. -- fx
1655 (defun dired-filename-at-point ()
1656 "Return the filename closest to point, expanded.
1657 Point should be in or after a filename."
1658 (save-excursion
1659 ;; First see if just past a filename.
1660 (or (eobp) ; why?
1661 (when (looking-at-p "[] \t\n[{}()]") ; whitespace or some parens
1662 (skip-chars-backward " \n\t\r({[]})")
1663 (or (bobp) (backward-char 1))))
1664 (let ((filename-chars "-.[:alnum:]_/:$+@")
1665 start prefix)
1666 (if (looking-at-p (format "[%s]" filename-chars))
1667 (progn
1668 (skip-chars-backward filename-chars)
1669 (setq start (point)
1670 prefix
1671 ;; This is something to do with ange-ftp filenames.
1672 ;; It convert foo@bar to /foo@bar.
1673 ;; But when does the former occur in dired buffers?
1674 (and (string-match-p
1675 "^\\w+@"
1676 (buffer-substring start (line-end-position)))
1677 "/"))
1678 (if (string-match-p "[/~]" (char-to-string (preceding-char)))
1679 (setq start (1- start)))
1680 (skip-chars-forward filename-chars))
1681 (error "No file found around point!"))
1682 ;; Return string.
1683 (expand-file-name (concat prefix (buffer-substring start (point)))))))
1685 (defun dired-x-read-filename-at-point (prompt)
1686 "Return filename prompting with PROMPT with completion.
1687 If `current-prefix-arg' is non-nil, uses name at point as guess."
1688 (if current-prefix-arg
1689 (let ((guess (dired-filename-at-point)))
1690 (read-file-name prompt
1691 (file-name-directory guess)
1692 guess
1693 nil (file-name-nondirectory guess)))
1694 (read-file-name prompt default-directory)))
1696 (define-obsolete-function-alias 'read-filename-at-point
1697 'dired-x-read-filename-at-point "24.1") ; is this even needed?
1699 ;;; BUG REPORTS
1701 (define-obsolete-function-alias 'dired-x-submit-report 'report-emacs-bug "24.1")
1704 ;; As Barry Warsaw would say: "This might be useful..."
1705 (provide 'dired-x)
1707 ;; Local Variables:
1708 ;; byte-compile-dynamic: t
1709 ;; generated-autoload-file: "dired-loaddefs.el"
1710 ;; End:
1712 ;;; dired-x.el ends here