Merge branch 'master' into comment-cache
[emacs.git] / lisp / dired.el
blob2733372eb7b0575273c0b8a236d77dcc8f7f7054
1 ;;; dired.el --- directory-browsing commands -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1992-1997, 2000-2017 Free Software
4 ;; Foundation, Inc.
6 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: files
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This is a major mode for directory browsing and editing.
29 ;; It is documented in the Emacs manual.
31 ;; Rewritten in 1990/1991 to add tree features, file marking and
32 ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
33 ;; Finished up by rms in 1992.
35 ;;; Code:
37 ;; When bootstrapping dired-loaddefs has not been generated.
38 (require 'dired-loaddefs nil t)
40 (declare-function dired-buffer-more-recently-used-p
41 "dired-x" (buffer1 buffer2))
43 ;;; Customizable variables
45 (defgroup dired nil
46 "Directory editing."
47 :link '(custom-manual "(emacs)Dired")
48 :group 'files)
50 (defgroup dired-mark nil
51 "Handling marks in Dired."
52 :prefix "dired-"
53 :group 'dired)
56 ;;;###autoload
57 (defcustom dired-listing-switches (purecopy "-al")
58 "Switches passed to `ls' for Dired. MUST contain the `l' option.
59 May contain all other options that don't contradict `-l';
60 may contain even `F', `b', `i' and `s'. See also the variable
61 `dired-ls-F-marks-symlinks' concerning the `F' switch.
62 Options that include embedded whitespace must be quoted
63 like this: \\\"--option=value with spaces\\\"; you can use
64 `combine-and-quote-strings' to produce the correct quoting of
65 each option.
66 On systems such as MS-DOS and MS-Windows, which use `ls' emulation in Lisp,
67 some of the `ls' switches are not supported; see the doc string of
68 `insert-directory' in `ls-lisp.el' for more details."
69 :type 'string
70 :group 'dired)
72 (defcustom dired-subdir-switches nil
73 "If non-nil, switches passed to `ls' for inserting subdirectories.
74 If nil, `dired-listing-switches' is used."
75 :group 'dired
76 :type '(choice (const :tag "Use dired-listing-switches" nil)
77 (string :tag "Switches")))
79 (defcustom dired-chown-program
80 (purecopy (cond ((executable-find "chown") "chown")
81 ((file-executable-p "/usr/sbin/chown") "/usr/sbin/chown")
82 ((file-executable-p "/etc/chown") "/etc/chown")
83 (t "chown")))
84 "Name of chown command (usually `chown')."
85 :group 'dired
86 :type 'file)
88 (defcustom dired-use-ls-dired 'unspecified
89 "Non-nil means Dired should pass the \"--dired\" option to \"ls\".
90 The special value of `unspecified' means to check explicitly, and
91 save the result in this variable. This is performed the first
92 time `dired-insert-directory' is called.
94 Note that if you set this option to nil, either through choice or
95 because your \"ls\" program does not support \"--dired\", Dired
96 will fail to parse some \"unusual\" file names, e.g. those with leading
97 spaces. You might want to install ls from GNU Coreutils, which does
98 support this option. Alternatively, you might want to use Emacs's
99 own emulation of \"ls\", by using:
100 (setq ls-lisp-use-insert-directory-program nil)
101 (require \\='ls-lisp)
102 This is used by default on MS Windows, which does not have an \"ls\" program.
103 Note that `ls-lisp' does not support as many options as GNU ls, though.
104 For more details, see Info node `(emacs)ls in Lisp'."
105 :group 'dired
106 :type '(choice (const :tag "Check for --dired support" unspecified)
107 (const :tag "Do not use --dired" nil)
108 (other :tag "Use --dired" t)))
110 (defcustom dired-chmod-program "chmod"
111 "Name of chmod command (usually `chmod')."
112 :group 'dired
113 :type 'file)
115 (defcustom dired-touch-program "touch"
116 "Name of touch command (usually `touch')."
117 :group 'dired
118 :type 'file)
120 (defcustom dired-ls-F-marks-symlinks nil
121 "Informs Dired about how `ls -lF' marks symbolic links.
122 Set this to t if `ls' (or whatever program is specified by
123 `insert-directory-program') with `-lF' marks the symbolic link
124 itself with a trailing @ (usually the case under Ultrix).
126 Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
127 nil (the default), if it gives `bar@ -> foo', set it to t.
129 Dired checks if there is really a @ appended. Thus, if you have a
130 marking `ls' program on one host and a non-marking on another host, and
131 don't care about symbolic links which really end in a @, you can
132 always set this variable to t."
133 :type 'boolean
134 :group 'dired-mark)
136 (defcustom dired-trivial-filenames (purecopy "^\\.\\.?$\\|^#")
137 "Regexp of files to skip when finding first file of a directory.
138 A value of nil means move to the subdir line.
139 A value of t means move to first file."
140 :type '(choice (const :tag "Move to subdir" nil)
141 (const :tag "Move to first" t)
142 regexp)
143 :group 'dired)
145 (defcustom dired-keep-marker-rename t
146 ;; Use t as default so that moved files "take their markers with them".
147 "Controls marking of renamed files.
148 If t, files keep their previous marks when they are renamed.
149 If a character, renamed files (whether previously marked or not)
150 are afterward marked with that character.
151 This option affects only files renamed by `dired-do-rename' and
152 `dired-do-rename-regexp'. See `wdired-keep-marker-rename'
153 if you want to do the same for files renamed in WDired mode."
154 :type '(choice (const :tag "Keep" t)
155 (character :tag "Mark" :value ?R))
156 :group 'dired-mark)
158 (defcustom dired-keep-marker-copy ?C
159 "Controls marking of copied files.
160 If t, copied files are marked if and as the corresponding original files were.
161 If a character, copied files are unconditionally marked with that character."
162 :type '(choice (const :tag "Keep" t)
163 (character :tag "Mark"))
164 :group 'dired-mark)
166 (defcustom dired-keep-marker-hardlink ?H
167 "Controls marking of newly made hard links.
168 If t, they are marked if and as the files linked to were marked.
169 If a character, new links are unconditionally marked with that character."
170 :type '(choice (const :tag "Keep" t)
171 (character :tag "Mark"))
172 :group 'dired-mark)
174 (defcustom dired-keep-marker-symlink ?Y
175 "Controls marking of newly made symbolic links.
176 If t, they are marked if and as the files linked to were marked.
177 If a character, new links are unconditionally marked with that character."
178 :type '(choice (const :tag "Keep" t)
179 (character :tag "Mark"))
180 :group 'dired-mark)
182 (defcustom dired-dwim-target nil
183 "If non-nil, Dired tries to guess a default target directory.
184 This means: if there is a Dired buffer displayed in the next
185 window, use its current directory, instead of this Dired buffer's
186 current directory.
188 The target is used in the prompt for file copy, rename etc."
189 :type 'boolean
190 :group 'dired)
192 (defcustom dired-copy-preserve-time t
193 "If non-nil, Dired preserves the last-modified time in a file copy.
194 \(This works on only some systems.)"
195 :type 'boolean
196 :group 'dired)
198 ; These variables were deleted and the replacements are on files.el.
199 ; We leave aliases behind for back-compatibility.
200 (defvaralias 'dired-free-space-program 'directory-free-space-program)
201 (defvaralias 'dired-free-space-args 'directory-free-space-args)
203 ;;; Hook variables
205 (defcustom dired-load-hook nil
206 "Run after loading Dired.
207 You can customize key bindings or load extensions with this."
208 :group 'dired
209 :type 'hook)
211 (defcustom dired-mode-hook nil
212 "Run at the very end of `dired-mode'."
213 :group 'dired
214 :type 'hook)
216 (defcustom dired-before-readin-hook nil
217 "This hook is run before a Dired buffer is read in (created or reverted)."
218 :group 'dired
219 :type 'hook)
221 (defcustom dired-after-readin-hook nil
222 "Hook run after each time a file or directory is read by Dired.
223 After each listing of a file or directory, this hook is run
224 with the buffer narrowed to the listing."
225 :group 'dired
226 :type 'hook)
227 ;; Note this can't simply be run inside function `dired-ls' as the hook
228 ;; functions probably depend on the dired-subdir-alist to be OK.
230 (defcustom dired-initial-position-hook nil
231 "This hook is used to position the point.
232 It is run by the function `dired-initial-position'."
233 :group 'dired
234 :type 'hook
235 :version "24.4")
237 (defcustom dired-dnd-protocol-alist
238 '(("^file:///" . dired-dnd-handle-local-file)
239 ("^file://" . dired-dnd-handle-file)
240 ("^file:" . dired-dnd-handle-local-file))
241 "The functions to call when a drop in `dired-mode' is made.
242 See `dnd-protocol-alist' for more information. When nil, behave
243 as in other buffers. Changing this option is effective only for
244 new Dired buffers."
245 :type '(choice (repeat (cons (regexp) (function)))
246 (const :tag "Behave as in other buffers" nil))
247 :version "22.1"
248 :group 'dired)
250 (defcustom dired-hide-details-hide-symlink-targets t
251 "Non-nil means `dired-hide-details-mode' hides symbolic link targets."
252 :type 'boolean
253 :version "24.4"
254 :group 'dired)
256 (defcustom dired-hide-details-hide-information-lines t
257 "Non-nil means `dired-hide-details-mode' hides all but header and file lines."
258 :type 'boolean
259 :version "24.4"
260 :group 'dired)
262 (defcustom dired-always-read-filesystem nil
263 "Non-nil means revert buffers visiting files before searching them.
264 By default, commands like `dired-mark-files-containing-regexp' will
265 search any buffers visiting the marked files without reverting them,
266 even if they were changed on disk. When this option is non-nil, such
267 buffers are always reverted in a temporary buffer before searching
268 them: the search is performed on the temporary buffer, the original
269 buffer visiting the file is not modified."
270 :type 'boolean
271 :version "26.1"
272 :group 'dired)
274 ;; Internal variables
276 (defvar dired-marker-char ?* ; the answer is 42
277 ;; so that you can write things like
278 ;; (let ((dired-marker-char ?X))
279 ;; ;; great code using X markers ...
280 ;; )
281 ;; For example, commands operating on two sets of files, A and B.
282 ;; Or marking files with digits 0-9. This could implicate
283 ;; concentric sets or an order for the marked files.
284 ;; The code depends on dynamic scoping on the marker char.
285 "In Dired, the current mark character.
286 This is what the do-commands look for, and what the mark-commands store.")
288 (defvar dired-del-marker ?D
289 "Character used to flag files for deletion.")
291 (defvar dired-shrink-to-fit t
292 ;; I see no reason ever to make this nil -- rms.
293 ;; (> baud-rate search-slow-speed)
294 "Non-nil means Dired shrinks the display buffer to fit the marked files.")
295 (make-obsolete-variable 'dired-shrink-to-fit
296 "use the Customization interface to add a new rule
297 to `display-buffer-alist' where condition regexp is \"^ \\*Marked Files\\*$\",
298 action argument symbol is `window-height' and its value is nil." "24.3")
300 (defvar dired-file-version-alist)
302 ;;;###autoload
303 (defvar dired-directory nil
304 "The directory name or wildcard spec that this Dired directory lists.
305 Local to each Dired buffer. May be a list, in which case the car is the
306 directory name and the cdr is the list of files to mention.
307 The directory name must be absolute, but need not be fully expanded.")
309 ;; Beware of "-l;reboot" etc. See bug#3230.
310 (defun dired-safe-switches-p (switches)
311 "Return non-nil if string SWITCHES does not look risky for Dired."
312 (or (not switches)
313 (and (stringp switches)
314 (< (length switches) 100) ; arbitrary
315 (string-match-p "\\` *-[- [:alnum:]]+\\'" switches))))
317 (defvar dired-actual-switches nil
318 "The value of `dired-listing-switches' used to make this buffer's text.")
320 (put 'dired-actual-switches 'safe-local-variable 'dired-safe-switches-p)
322 (defvar dired-re-inode-size "[0-9 \t]*[.,0-9]*[BkKMGTPEZY]?[ \t]*"
323 "Regexp for optional initial inode and file size as made by `ls -i -s'.")
325 ;; These regexps must be tested at beginning-of-line, but are also
326 ;; used to search for next matches, so neither omitting "^" nor
327 ;; replacing "^" by "\n" (to make it slightly faster) will work.
329 (defvar dired-re-mark "^[^ \n]")
330 ;; "Regexp matching a marked line.
331 ;; Important: the match ends just after the marker."
332 (defvar dired-re-maybe-mark "^. ")
333 ;; The [^:] part after "d" and "l" is to avoid confusion with the
334 ;; DOS/Windows-style drive letters in directory names, like in "d:/foo".
335 (defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d[^:]"))
336 (defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l[^:]"))
337 (defvar dired-re-exe;; match ls permission string of an executable file
338 (mapconcat (function
339 (lambda (x)
340 (concat dired-re-maybe-mark dired-re-inode-size x)))
341 '("-[-r][-w][xs][-r][-w].[-r][-w]."
342 "-[-r][-w].[-r][-w][xs][-r][-w]."
343 "-[-r][-w].[-r][-w].[-r][-w][xst]")
344 "\\|"))
345 (defvar dired-re-perms "[-bcdlps][-r][-w].[-r][-w].[-r][-w].")
346 (defvar dired-re-dot "^.* \\.\\.?/?$")
348 ;; The subdirectory names in the next two lists are expanded.
349 (defvar dired-subdir-alist nil
350 "Association list of subdirectories and their buffer positions.
351 Each subdirectory has an element: (DIRNAME . STARTMARKER).
352 The order of elements is the reverse of the order in the buffer.
353 In simple cases, this list contains one element.")
355 (defvar-local dired-switches-alist nil
356 "Keeps track of which switches to use for inserted subdirectories.
357 This is an alist of the form (SUBDIR . SWITCHES).")
359 (defvaralias 'dired-move-to-filename-regexp
360 'directory-listing-before-filename-regexp)
362 (defvar dired-subdir-regexp "^. \\([^\n\r]+\\)\\(:\\)[\n\r]"
363 "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
364 Subexpression 1 is the subdirectory proper, no trailing colon.
365 The match starts at the beginning of the line and ends after the end
366 of the line (\\n or \\r).
367 Subexpression 2 must end right before the \\n or \\r.")
369 (defgroup dired-faces nil
370 "Faces used by Dired."
371 :group 'dired
372 :group 'faces)
374 (defface dired-header
375 '((t (:inherit font-lock-type-face)))
376 "Face used for directory headers."
377 :group 'dired-faces
378 :version "22.1")
379 (defvar dired-header-face 'dired-header
380 "Face name used for directory headers.")
382 (defface dired-mark
383 '((t (:inherit font-lock-constant-face)))
384 "Face used for Dired marks."
385 :group 'dired-faces
386 :version "22.1")
387 (defvar dired-mark-face 'dired-mark
388 "Face name used for Dired marks.")
390 (defface dired-marked
391 '((t (:inherit warning)))
392 "Face used for marked files."
393 :group 'dired-faces
394 :version "22.1")
395 (defvar dired-marked-face 'dired-marked
396 "Face name used for marked files.")
398 (defface dired-flagged
399 '((t (:inherit error)))
400 "Face used for files flagged for deletion."
401 :group 'dired-faces
402 :version "22.1")
403 (defvar dired-flagged-face 'dired-flagged
404 "Face name used for files flagged for deletion.")
406 (defface dired-warning
407 ;; Inherit from font-lock-warning-face since with min-colors 8
408 ;; font-lock-comment-face is not colored any more.
409 '((t (:inherit font-lock-warning-face)))
410 "Face used to highlight a part of a buffer that needs user attention."
411 :group 'dired-faces
412 :version "22.1")
413 (defvar dired-warning-face 'dired-warning
414 "Face name used for a part of a buffer that needs user attention.")
416 (defface dired-perm-write
417 '((((type w32 pc)) :inherit default) ;; These default to rw-rw-rw.
418 ;; Inherit from font-lock-comment-delimiter-face since with min-colors 8
419 ;; font-lock-comment-face is not colored any more.
420 (t (:inherit font-lock-comment-delimiter-face)))
421 "Face used to highlight permissions of group- and world-writable files."
422 :group 'dired-faces
423 :version "22.2")
424 (defvar dired-perm-write-face 'dired-perm-write
425 "Face name used for permissions of group- and world-writable files.")
427 (defface dired-directory
428 '((t (:inherit font-lock-function-name-face)))
429 "Face used for subdirectories."
430 :group 'dired-faces
431 :version "22.1")
432 (defvar dired-directory-face 'dired-directory
433 "Face name used for subdirectories.")
435 (defface dired-symlink
436 '((t (:inherit font-lock-keyword-face)))
437 "Face used for symbolic links."
438 :group 'dired-faces
439 :version "22.1")
440 (defvar dired-symlink-face 'dired-symlink
441 "Face name used for symbolic links.")
443 (defface dired-ignored
444 '((t (:inherit shadow)))
445 "Face used for files suffixed with `completion-ignored-extensions'."
446 :group 'dired-faces
447 :version "22.1")
448 (defvar dired-ignored-face 'dired-ignored
449 "Face name used for files suffixed with `completion-ignored-extensions'.")
451 (defvar dired-font-lock-keywords
452 (list
454 ;; Dired marks.
455 (list dired-re-mark '(0 dired-mark-face))
457 ;; We make heavy use of MATCH-ANCHORED, since the regexps don't identify the
458 ;; file name itself. We search for Dired defined regexps, and then use the
459 ;; Dired defined function `dired-move-to-filename' before searching for the
460 ;; simple regexp ".+". It is that regexp which matches the file name.
462 ;; Marked files.
463 (list (concat "^[" (char-to-string dired-marker-char) "]")
464 '(".+" (dired-move-to-filename) nil (0 dired-marked-face)))
466 ;; Flagged files.
467 (list (concat "^[" (char-to-string dired-del-marker) "]")
468 '(".+" (dired-move-to-filename) nil (0 dired-flagged-face)))
469 ;; People who are paranoid about security would consider this more
470 ;; important than other things such as whether it is a directory.
471 ;; But we don't want to encourage paranoia, so our default
472 ;; should be what's most useful for non-paranoids. -- rms.
473 ;;; ;;
474 ;;; ;; Files that are group or world writable.
475 ;;; (list (concat dired-re-maybe-mark dired-re-inode-size
476 ;;; "\\([-d]\\(....w....\\|.......w.\\)\\)")
477 ;;; '(1 dired-warning-face)
478 ;;; '(".+" (dired-move-to-filename) nil (0 dired-warning-face)))
479 ;; However, we don't need to highlight the file name, only the
480 ;; permissions, to win generally. -- fx.
481 ;; Fixme: we could also put text properties on the permission
482 ;; fields with keymaps to frob the permissions, somewhat a la XEmacs.
483 (list (concat dired-re-maybe-mark dired-re-inode-size
484 "[-d]....\\(w\\)....") ; group writable
485 '(1 dired-perm-write-face))
486 (list (concat dired-re-maybe-mark dired-re-inode-size
487 "[-d].......\\(w\\).") ; world writable
488 '(1 dired-perm-write-face))
490 ;; Subdirectories.
491 (list dired-re-dir
492 '(".+" (dired-move-to-filename) nil (0 dired-directory-face)))
494 ;; Symbolic links.
495 (list dired-re-sym
496 '(".+" (dired-move-to-filename) nil (0 dired-symlink-face)))
498 ;; Files suffixed with `completion-ignored-extensions'.
499 '(eval .
500 ;; It is quicker to first find just an extension, then go back to the
501 ;; start of that file name. So we do this complex MATCH-ANCHORED form.
502 (list (concat "\\(" (regexp-opt completion-ignored-extensions) "\\|#\\)$")
503 '(".+" (dired-move-to-filename) nil (0 dired-ignored-face))))
505 ;; Files suffixed with `completion-ignored-extensions'
506 ;; plus a character put in by -F.
507 '(eval .
508 (list (concat "\\(" (regexp-opt completion-ignored-extensions)
509 "\\|#\\)[*=|]$")
510 '(".+" (progn
511 (end-of-line)
512 ;; If the last character is not part of the filename,
513 ;; move back to the start of the filename
514 ;; so it can be fontified.
515 ;; Otherwise, leave point at the end of the line;
516 ;; that way, nothing is fontified.
517 (unless (get-text-property (1- (point)) 'mouse-face)
518 (dired-move-to-filename)))
519 nil (0 dired-ignored-face))))
521 ;; Explicitly put the default face on file names ending in a colon to
522 ;; avoid fontifying them as directory header.
523 (list (concat dired-re-maybe-mark dired-re-inode-size dired-re-perms ".*:$")
524 '(".+" (dired-move-to-filename) nil (0 'default)))
526 ;; Directory headers.
527 (list dired-subdir-regexp '(1 dired-header-face))
529 "Additional expressions to highlight in Dired mode.")
531 (defvar dnd-protocol-alist)
533 ;;; Macros must be defined before they are used, for the byte compiler.
535 (defmacro dired-mark-if (predicate msg)
536 "Mark all files for which PREDICATE evals to non-nil.
537 PREDICATE is evaluated on each line, with point at beginning of line.
538 MSG is a noun phrase for the type of files being marked.
539 It should end with a noun that can be pluralized by adding `s'.
540 Return value is the number of files marked, or nil if none were marked."
541 `(let ((inhibit-read-only t) count)
542 (save-excursion
543 (setq count 0)
544 (when ,msg
545 (message "%s %ss%s..."
546 (cond ((eq dired-marker-char ?\040) "Unmarking")
547 ((eq dired-del-marker dired-marker-char)
548 "Flagging")
549 (t "Marking"))
550 ,msg
551 (if (eq dired-del-marker dired-marker-char)
552 " for deletion"
553 "")))
554 (goto-char (point-min))
555 (while (not (eobp))
556 (if ,predicate
557 (progn
558 (delete-char 1)
559 (insert dired-marker-char)
560 (setq count (1+ count))))
561 (forward-line 1))
562 (if ,msg (message "%s %s%s %s%s."
563 count
564 ,msg
565 (dired-plural-s count)
566 (if (eq dired-marker-char ?\040) "un" "")
567 (if (eq dired-marker-char dired-del-marker)
568 "flagged" "marked"))))
569 (and (> count 0) count)))
571 (defmacro dired-map-over-marks (body arg &optional show-progress
572 distinguish-one-marked)
573 "Eval BODY with point on each marked line. Return a list of BODY's results.
574 If no marked file could be found, execute BODY on the current
575 line. ARG, if non-nil, specifies the files to use instead of the
576 marked files.
578 If ARG is an integer, use the next ARG (or previous -ARG, if
579 ARG<0) files. In that case, point is dragged along. This is so
580 that commands on the next ARG (instead of the marked) files can
581 be chained easily.
582 For any other non-nil value of ARG, use the current file.
584 If optional third arg SHOW-PROGRESS evaluates to non-nil,
585 redisplay the dired buffer after each file is processed.
587 No guarantee is made about the position on the marked line.
588 BODY must ensure this itself if it depends on this.
590 Search starts at the beginning of the buffer, thus the car of the
591 list corresponds to the line nearest to the buffer's bottom.
592 This is also true for (positive and negative) integer values of
593 ARG.
595 BODY should not be too long as it is expanded four times.
597 If DISTINGUISH-ONE-MARKED is non-nil, then if we find just one
598 marked file, return (t FILENAME) instead of (FILENAME)."
600 ;;Warning: BODY must not add new lines before point - this may cause an
601 ;;endless loop.
602 ;;This warning should not apply any longer, sk 2-Sep-1991 14:10.
603 `(prog1
604 (let ((inhibit-read-only t) case-fold-search found results)
605 (if ,arg
606 (if (integerp ,arg)
607 (progn ;; no save-excursion, want to move point.
608 (dired-repeat-over-lines
609 ,arg
610 (function (lambda ()
611 (if ,show-progress (sit-for 0))
612 (setq results (cons ,body results)))))
613 (if (< ,arg 0)
614 (nreverse results)
615 results))
616 ;; non-nil, non-integer ARG means use current file:
617 (list ,body))
618 (let ((regexp (dired-marker-regexp)) next-position)
619 (save-excursion
620 (goto-char (point-min))
621 ;; remember position of next marked file before BODY
622 ;; can insert lines before the just found file,
623 ;; confusing us by finding the same marked file again
624 ;; and again and...
625 (setq next-position (and (re-search-forward regexp nil t)
626 (point-marker))
627 found (not (null next-position)))
628 (while next-position
629 (goto-char next-position)
630 (if ,show-progress (sit-for 0))
631 (setq results (cons ,body results))
632 ;; move after last match
633 (goto-char next-position)
634 (forward-line 1)
635 (set-marker next-position nil)
636 (setq next-position (and (re-search-forward regexp nil t)
637 (point-marker)))))
638 (if (and ,distinguish-one-marked (= (length results) 1))
639 (setq results (cons t results)))
640 (if found
641 results
642 (list ,body)))))
643 ;; save-excursion loses, again
644 (dired-move-to-filename)))
646 (defun dired-get-marked-files (&optional localp arg filter distinguish-one-marked)
647 "Return the marked files' names as list of strings.
648 The list is in the same order as the buffer, that is, the car is the
649 first marked file.
650 Values returned are normally absolute file names.
651 Optional arg LOCALP as in `dired-get-filename'.
652 Optional second argument ARG, if non-nil, specifies files near
653 point instead of marked files. It usually comes from the prefix
654 argument.
655 If ARG is an integer, use the next ARG files.
656 If ARG is any other non-nil value, return the current file name.
657 If no files are marked, and ARG is nil, also return the current file name.
658 Optional third argument FILTER, if non-nil, is a function to select
659 some of the files--those for which (funcall FILTER FILENAME) is non-nil.
661 If DISTINGUISH-ONE-MARKED is non-nil, then if we find just one marked file,
662 return (t FILENAME) instead of (FILENAME).
663 Don't use that together with FILTER."
664 (let ((all-of-them
665 (save-excursion
666 (delq nil (dired-map-over-marks
667 (dired-get-filename localp 'no-error-if-not-filep)
668 arg nil distinguish-one-marked))))
669 result)
670 (when (equal all-of-them '(t))
671 (setq all-of-them nil))
672 (if (not filter)
673 (if (and distinguish-one-marked (eq (car all-of-them) t))
674 all-of-them
675 (nreverse all-of-them))
676 (dolist (file all-of-them)
677 (if (funcall filter file)
678 (push file result)))
679 result)))
681 ;; The dired command
683 (defun dired-read-dir-and-switches (str)
684 ;; For use in interactive.
685 (reverse (list
686 (if current-prefix-arg
687 (read-string "Dired listing switches: "
688 dired-listing-switches))
689 ;; If a dialog is used, call `read-directory-name' so the
690 ;; dialog code knows we want directories. Some dialogs
691 ;; can only select directories or files when popped up,
692 ;; not both. If no dialog is used, call `read-file-name'
693 ;; because the user may want completion of file names for
694 ;; use in a wildcard pattern.
695 (if (next-read-file-uses-dialog-p)
696 (read-directory-name (format "Dired %s(directory): " str)
697 nil default-directory nil)
698 (read-file-name (format "Dired %s(directory): " str)
699 nil default-directory nil)))))
701 ;; We want to switch to a more sophisticated version of
702 ;; dired-read-dir-and-switches like the following, if there is a way
703 ;; to make it more intuitive. See bug#1285.
705 ;; (defun dired-read-dir-and-switches (str)
706 ;; ;; For use in interactive.
707 ;; (reverse
708 ;; (list
709 ;; (if current-prefix-arg
710 ;; (read-string "Dired listing switches: "
711 ;; dired-listing-switches))
712 ;; ;; If a dialog is about to be used, call read-directory-name so
713 ;; ;; the dialog code knows we want directories. Some dialogs can
714 ;; ;; only select directories or files when popped up, not both.
715 ;; (if (next-read-file-uses-dialog-p)
716 ;; (read-directory-name (format "Dired %s(directory): " str)
717 ;; nil default-directory nil)
718 ;; (let ((cie ()))
719 ;; (dolist (ext completion-ignored-extensions)
720 ;; (if (eq ?/ (aref ext (1- (length ext)))) (push ext cie)))
721 ;; (setq cie (concat (regexp-opt cie "\\(?:") "\\'"))
722 ;; (let* ((default (and buffer-file-name
723 ;; (abbreviate-file-name buffer-file-name)))
724 ;; (cie cie)
725 ;; (completion-table
726 ;; ;; We need a mix of read-file-name and
727 ;; ;; read-directory-name so that completion to directories
728 ;; ;; is preferred, but if the user wants to enter a global
729 ;; ;; pattern, he can still use completion on filenames to
730 ;; ;; help him write the pattern.
731 ;; ;; Essentially, we want to use
732 ;; ;; (completion-table-with-predicate
733 ;; ;; 'read-file-name-internal 'file-directory-p nil)
734 ;; ;; but that doesn't work because read-file-name-internal
735 ;; ;; does not obey its `predicate' argument.
736 ;; (completion-table-in-turn
737 ;; (lambda (str pred action)
738 ;; (let ((read-file-name-predicate
739 ;; (lambda (f)
740 ;; (and (not (member f '("./" "../")))
741 ;; ;; Hack! Faster than file-directory-p!
742 ;; (eq (aref f (1- (length f))) ?/)
743 ;; (not (string-match cie f))))))
744 ;; (complete-with-action
745 ;; action 'read-file-name-internal str nil)))
746 ;; 'read-file-name-internal)))
747 ;; (minibuffer-with-setup-hook
748 ;; (lambda ()
749 ;; (setq minibuffer-default default)
750 ;; (setq minibuffer-completion-table completion-table))
751 ;; (read-file-name (format "Dired %s(directory): " str)
752 ;; nil default-directory nil))))))))
754 (defun dired-file-name-at-point ()
755 "Try to get a file name at point in the current dired buffer.
756 This hook is intended to be put in `file-name-at-point-functions'.
757 Note that it returns an abbreviated name that can't be used
758 as an argument to `dired-goto-file'."
759 (let ((filename (dired-get-filename nil t)))
760 (when filename
761 (if (file-directory-p filename)
762 (file-name-as-directory (abbreviate-file-name filename))
763 (abbreviate-file-name filename)))))
765 ;;;###autoload (define-key ctl-x-map "d" 'dired)
766 ;;;###autoload
767 (defun dired (dirname &optional switches)
768 "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
769 Optional second argument SWITCHES specifies the `ls' options used.
770 \(Interactively, use a prefix argument to be able to specify SWITCHES.)
772 If DIRNAME is a string, Dired displays a list of files in DIRNAME (which
773 may also have shell wildcards appended to select certain files).
775 If DIRNAME is a cons, its first element is taken as the directory name
776 and the rest as an explicit list of files to make directory entries for.
777 In this case, SWITCHES are applied to each of the files separately, and
778 therefore switches that control the order of the files in the produced
779 listing have no effect.
781 \\<dired-mode-map>\
782 You can flag files for deletion with \\[dired-flag-file-deletion] and then
783 delete them by typing \\[dired-do-flagged-delete].
784 Type \\[describe-mode] after entering Dired for more info.
786 If DIRNAME is already in a Dired buffer, that buffer is used without refresh."
787 ;; Cannot use (interactive "D") because of wildcards.
788 (interactive (dired-read-dir-and-switches ""))
789 (switch-to-buffer (dired-noselect dirname switches)))
791 ;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
792 ;;;###autoload
793 (defun dired-other-window (dirname &optional switches)
794 "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
795 (interactive (dired-read-dir-and-switches "in other window "))
796 (switch-to-buffer-other-window (dired-noselect dirname switches)))
798 ;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
799 ;;;###autoload
800 (defun dired-other-frame (dirname &optional switches)
801 "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame."
802 (interactive (dired-read-dir-and-switches "in other frame "))
803 (switch-to-buffer-other-frame (dired-noselect dirname switches)))
805 ;;;###autoload
806 (defun dired-noselect (dir-or-list &optional switches)
807 "Like `dired' but returns the Dired buffer as value, does not select it."
808 (or dir-or-list (setq dir-or-list default-directory))
809 ;; This loses the distinction between "/foo/*/" and "/foo/*" that
810 ;; some shells make:
811 (let (dirname initially-was-dirname)
812 (if (consp dir-or-list)
813 (setq dirname (car dir-or-list))
814 (setq dirname dir-or-list))
815 (setq initially-was-dirname
816 (string= (file-name-as-directory dirname) dirname))
817 (setq dirname (abbreviate-file-name
818 (expand-file-name (directory-file-name dirname))))
819 (if find-file-visit-truename
820 (setq dirname (file-truename dirname)))
821 ;; If the argument was syntactically a directory name not a file name,
822 ;; or if it happens to name a file that is a directory,
823 ;; convert it syntactically to a directory name.
824 ;; The reason for checking initially-was-dirname
825 ;; and not just file-directory-p
826 ;; is that file-directory-p is slow over ftp.
827 (if (or initially-was-dirname (file-directory-p dirname))
828 (setq dirname (file-name-as-directory dirname)))
829 (if (consp dir-or-list)
830 (setq dir-or-list (cons dirname (cdr dir-or-list)))
831 (setq dir-or-list dirname))
832 (dired-internal-noselect dir-or-list switches)))
834 ;; The following is an internal dired function. It returns non-nil if
835 ;; the directory visited by the current dired buffer has changed on
836 ;; disk. DIRNAME should be the directory name of that directory.
837 (defun dired-directory-changed-p (dirname)
838 (not (let ((attributes (file-attributes dirname))
839 (modtime (visited-file-modtime)))
840 (or (eq modtime 0)
841 (not (eq (car attributes) t))
842 (equal (nth 5 attributes) modtime)))))
844 (defun dired-buffer-stale-p (&optional noconfirm)
845 "Return non-nil if current Dired buffer needs updating.
846 If NOCONFIRM is non-nil, then this function always returns nil
847 for a remote directory. This feature is used by Auto Revert mode."
848 (let ((dirname
849 (if (consp dired-directory) (car dired-directory) dired-directory)))
850 (and (stringp dirname)
851 (not (when noconfirm (file-remote-p dirname)))
852 (file-readable-p dirname)
853 ;; Do not auto-revert when the dired buffer can be currently
854 ;; written by the user as in `wdired-mode'.
855 buffer-read-only
856 (dired-directory-changed-p dirname))))
858 (defcustom dired-auto-revert-buffer nil
859 "Automatically revert Dired buffer on revisiting.
860 If t, revisiting an existing Dired buffer automatically reverts it.
861 If its value is a function, call this function with the directory
862 name as single argument and revert the buffer if it returns non-nil.
863 Otherwise, a message offering to revert the changed dired buffer
864 is displayed.
865 Note that this is not the same as `auto-revert-mode' that
866 periodically reverts at specified time intervals."
867 :type '(choice
868 (const :tag "Don't revert" nil)
869 (const :tag "Always revert visited Dired buffer" t)
870 (const :tag "Revert changed Dired buffer" dired-directory-changed-p)
871 (function :tag "Predicate function"))
872 :group 'dired
873 :version "23.2")
875 (defun dired-internal-noselect (dir-or-list &optional switches mode)
876 ;; If there is an existing dired buffer for DIRNAME, just leave
877 ;; buffer as it is (don't even call dired-revert).
878 ;; This saves time especially for deep trees or with ange-ftp.
879 ;; The user can type `g' easily, and it is more consistent with find-file.
880 ;; But if SWITCHES are given they are probably different from the
881 ;; buffer's old value, so call dired-sort-other, which does
882 ;; revert the buffer.
883 ;; A pity we can't possibly do "Directory has changed - refresh? "
884 ;; like find-file does.
885 ;; Optional argument MODE is passed to dired-find-buffer-nocreate,
886 ;; see there.
887 (let* ((old-buf (current-buffer))
888 (dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
889 ;; Look for an existing buffer.
890 (buffer (dired-find-buffer-nocreate dirname mode))
891 ;; Note that buffer already is in dired-mode, if found.
892 (new-buffer-p (null buffer)))
893 (or buffer
894 (setq buffer (create-file-buffer (directory-file-name dirname))))
895 (set-buffer buffer)
896 (if (not new-buffer-p) ; existing buffer ...
897 (cond (switches ; ... but new switches
898 ;; file list may have changed
899 (setq dired-directory dir-or-list)
900 ;; this calls dired-revert
901 (dired-sort-other switches))
902 ;; Always revert regardless of whether it has changed or not.
903 ((eq dired-auto-revert-buffer t)
904 (revert-buffer))
905 ;; Revert when predicate function returns non-nil.
906 ((functionp dired-auto-revert-buffer)
907 (when (funcall dired-auto-revert-buffer dirname)
908 (revert-buffer)
909 (message "Changed directory automatically updated")))
910 ;; If directory has changed on disk, offer to revert.
911 ((when (dired-directory-changed-p dirname)
912 (message "%s"
913 (substitute-command-keys
914 "Directory has changed on disk; type \\[revert-buffer] to update Dired")))))
915 ;; Else a new buffer
916 (setq default-directory
917 ;; We can do this unconditionally
918 ;; because dired-noselect ensures that the name
919 ;; is passed in directory name syntax
920 ;; if it was the name of a directory at all.
921 (file-name-directory dirname))
922 (or switches (setq switches dired-listing-switches))
923 (if mode (funcall mode)
924 (dired-mode dir-or-list switches))
925 ;; default-directory and dired-actual-switches are set now
926 ;; (buffer-local), so we can call dired-readin:
927 (let ((failed t))
928 (unwind-protect
929 (progn (dired-readin)
930 (setq failed nil))
931 ;; dired-readin can fail if parent directories are inaccessible.
932 ;; Don't leave an empty buffer around in that case.
933 (if failed (kill-buffer buffer))))
934 (goto-char (point-min))
935 (dired-initial-position dirname))
936 (set-buffer old-buf)
937 buffer))
939 (defvar dired-buffers nil
940 ;; Enlarged by dired-advertise
941 ;; Queried by function dired-buffers-for-dir. When this detects a
942 ;; killed buffer, it is removed from this list.
943 "Alist of expanded directories and their associated Dired buffers.")
945 (defvar dired-find-subdir)
947 ;; FIXME add a doc-string, and document dired-x extensions.
948 (defun dired-find-buffer-nocreate (dirname &optional mode)
949 ;; This differs from dired-buffers-for-dir in that it does not consider
950 ;; subdirs of default-directory and searches for the first match only.
951 ;; Also, the major mode must be MODE.
952 (if (and (featurep 'dired-x)
953 dired-find-subdir
954 ;; Don't try to find a wildcard as a subdirectory.
955 (string-equal dirname (file-name-directory dirname)))
956 (let* ((cur-buf (current-buffer))
957 (buffers (nreverse
958 (dired-buffers-for-dir (expand-file-name dirname))))
959 (cur-buf-matches (and (memq cur-buf buffers)
960 ;; Wildcards must match, too:
961 (equal dired-directory dirname))))
962 ;; We don't want to switch to the same buffer---
963 (setq buffers (delq cur-buf buffers))
964 (or (car (sort buffers #'dired-buffer-more-recently-used-p))
965 ;; ---unless it's the only possibility:
966 (and cur-buf-matches cur-buf)))
967 ;; No dired-x, or dired-find-subdir nil.
968 (setq dirname (expand-file-name dirname))
969 (let (found (blist dired-buffers)) ; was (buffer-list)
970 (or mode (setq mode 'dired-mode))
971 (while blist
972 (if (null (buffer-name (cdr (car blist))))
973 (setq blist (cdr blist))
974 (with-current-buffer (cdr (car blist))
975 (if (and (eq major-mode mode)
976 dired-directory ;; nil during find-alternate-file
977 (equal dirname
978 (expand-file-name
979 (if (consp dired-directory)
980 (car dired-directory)
981 dired-directory))))
982 (setq found (cdr (car blist))
983 blist nil)
984 (setq blist (cdr blist))))))
985 found)))
988 ;; Read in a new dired buffer
990 (defun dired-readin ()
991 "Read in a new Dired buffer.
992 Differs from `dired-insert-subdir' in that it accepts
993 wildcards, erases the buffer, and builds the subdir-alist anew
994 \(including making it buffer-local and clearing it first)."
996 ;; default-directory and dired-actual-switches must be buffer-local
997 ;; and initialized by now.
998 (let (dirname
999 ;; This makes readin much much faster.
1000 ;; In particular, it prevents the font lock hook from running
1001 ;; until the directory is all read in.
1002 (inhibit-modification-hooks t))
1003 (if (consp dired-directory)
1004 (setq dirname (car dired-directory))
1005 (setq dirname dired-directory))
1006 (setq dirname (expand-file-name dirname))
1007 (save-excursion
1008 ;; This hook which may want to modify dired-actual-switches
1009 ;; based on dired-directory, e.g. with ange-ftp to a SysV host
1010 ;; where ls won't understand -Al switches.
1011 (run-hooks 'dired-before-readin-hook)
1012 (if (consp buffer-undo-list)
1013 (setq buffer-undo-list nil))
1014 (setq-local file-name-coding-system
1015 (or coding-system-for-read file-name-coding-system))
1016 (let ((inhibit-read-only t)
1017 ;; Don't make undo entries for readin.
1018 (buffer-undo-list t))
1019 (widen)
1020 (erase-buffer)
1021 (dired-readin-insert))
1022 (goto-char (point-min))
1023 ;; Must first make alist buffer local and set it to nil because
1024 ;; dired-build-subdir-alist will call dired-clear-alist first
1025 (setq-local dired-subdir-alist nil)
1026 (dired-build-subdir-alist)
1027 (let ((attributes (file-attributes dirname)))
1028 (if (eq (car attributes) t)
1029 (set-visited-file-modtime (nth 5 attributes))))
1030 (set-buffer-modified-p nil)
1031 ;; No need to narrow since the whole buffer contains just
1032 ;; dired-readin's output, nothing else. The hook can
1033 ;; successfully use dired functions (e.g. dired-get-filename)
1034 ;; as the subdir-alist has been built in dired-readin.
1035 (run-hooks 'dired-after-readin-hook))))
1037 ;; Subroutines of dired-readin
1039 (defun dired-readin-insert ()
1040 ;; Insert listing for the specified dir (and maybe file list)
1041 ;; already in dired-directory, assuming a clean buffer.
1042 (let (dir file-list)
1043 (if (consp dired-directory)
1044 (setq dir (car dired-directory)
1045 file-list (cdr dired-directory))
1046 (setq dir dired-directory
1047 file-list nil))
1048 (setq dir (expand-file-name dir))
1049 (if (and (equal "" (file-name-nondirectory dir))
1050 (not file-list))
1051 ;; If we are reading a whole single directory...
1052 (dired-insert-directory dir dired-actual-switches nil nil t)
1053 (if (not (file-readable-p
1054 (directory-file-name (file-name-directory dir))))
1055 (error "Directory %s inaccessible or nonexistent" dir)
1056 ;; Else treat it as a wildcard spec
1057 ;; unless we have an explicit list of files.
1058 (dired-insert-directory dir dired-actual-switches
1059 file-list (not file-list) t)))))
1061 (defun dired-align-file (beg end)
1062 "Align the fields of a file to the ones of surrounding lines.
1063 BEG..END is the line where the file info is located."
1064 ;; Some versions of ls try to adjust the size of each field so as to just
1065 ;; hold the largest element ("largest" in the current invocation, of
1066 ;; course). So when a single line is output, the size of each field is
1067 ;; just big enough for that one output. Thus when dired refreshes one
1068 ;; line, the alignment if this line w.r.t the rest is messed up because
1069 ;; the fields of that one line will generally be smaller.
1071 ;; To work around this problem, we here add spaces to try and
1072 ;; re-align the fields as needed. Since this is purely aesthetic,
1073 ;; it is of utmost importance that it doesn't mess up anything like
1074 ;; `dired-move-to-filename'. To this end, we limit ourselves to
1075 ;; adding spaces only, and to only add them at places where there
1076 ;; was already at least one space. This way, as long as
1077 ;; `directory-listing-before-filename-regexp' always matches spaces
1078 ;; with "*" or "+", we know we haven't made anything worse. There
1079 ;; is one spot where the exact number of spaces is important, which
1080 ;; is just before the actual filename, so we refrain from adding
1081 ;; spaces there (and within the filename as well, of course).
1082 (save-excursion
1083 (let (file file-col other other-col)
1084 ;; Check that there is indeed a file, and that there is another adjacent
1085 ;; file with which to align, and that additional spaces are needed to
1086 ;; align the filenames.
1087 (when (and (setq file (progn (goto-char beg)
1088 (dired-move-to-filename nil end)))
1089 (setq file-col (current-column))
1090 (setq other
1091 (or (and (goto-char beg)
1092 (zerop (forward-line -1))
1093 (dired-move-to-filename))
1094 (and (goto-char beg)
1095 (zerop (forward-line 1))
1096 (dired-move-to-filename))))
1097 (setq other-col (current-column))
1098 (/= file other)
1099 ;; Make sure there is some work left to do.
1100 (> other-col file-col))
1101 ;; If we've only looked at the line above, check to see if the line
1102 ;; below exists as well and if so, align with the shorter one.
1103 (when (and (< other file)
1104 (goto-char beg)
1105 (zerop (forward-line 1))
1106 (dired-move-to-filename))
1107 (let ((alt-col (current-column)))
1108 (when (< alt-col other-col)
1109 (setq other-col alt-col)
1110 (setq other (point)))))
1111 ;; Keep positions uptodate when we insert stuff.
1112 (if (> other file) (setq other (copy-marker other)))
1113 (setq file (copy-marker file))
1114 ;; Main loop.
1115 (goto-char beg)
1116 (skip-chars-forward " ") ;Skip to the first field.
1117 (while (and (> other-col file-col)
1118 ;; Don't touch anything just before (and after) the
1119 ;; beginning of the filename.
1120 (> file (point)))
1121 ;; We're now just in front of a field, with a space behind us.
1122 (let* ((curcol (current-column))
1123 ;; Nums are right-aligned.
1124 (num-align (looking-at-p "[0-9]"))
1125 ;; Let's look at the other line, in the same column: we
1126 ;; should be either near the end of the previous field, or
1127 ;; in the space between that field and the next.
1128 ;; [ Of course, it's also possible that we're already within
1129 ;; the next field or even past it, but that's unlikely since
1130 ;; other-col > file-col. ]
1131 ;; Let's find the distance to the alignment-point (either
1132 ;; the beginning or the end of the next field, depending on
1133 ;; whether this field is left or right aligned).
1134 (align-pt-offset
1135 (save-excursion
1136 (goto-char other)
1137 (move-to-column curcol)
1138 (when (looking-at
1139 (concat
1140 (if (eq (char-before) ?\s) " *" "[^ ]* *")
1141 (if num-align "[0-9][^ ]*")))
1142 (- (match-end 0) (match-beginning 0)))))
1143 ;; Now, the number of spaces to insert is align-pt-offset
1144 ;; minus the distance to the equivalent point on the
1145 ;; current line.
1146 (spaces
1147 (if (not num-align)
1148 align-pt-offset
1149 (and align-pt-offset
1150 (save-excursion
1151 (skip-chars-forward "^ ")
1152 (- align-pt-offset (- (current-column) curcol)))))))
1153 (when (and spaces (> spaces 0))
1154 (setq file-col (+ spaces file-col))
1155 (if (> file-col other-col)
1156 (setq spaces (- spaces (- file-col other-col))))
1157 (insert-char ?\s spaces)
1158 ;; Let's just make really sure we did not mess up.
1159 (unless (save-excursion
1160 (eq (dired-move-to-filename) (marker-position file)))
1161 ;; Damn! We messed up: let's revert the change.
1162 (delete-char (- spaces)))))
1163 ;; Now skip to next field.
1164 (skip-chars-forward "^ ") (skip-chars-forward " "))
1165 (set-marker file nil)))))
1168 (defvar ls-lisp-use-insert-directory-program)
1170 (defun dired-check-switches (switches short &optional long)
1171 "Return non-nil if the string SWITCHES matches LONG or SHORT format."
1172 (let (case-fold-search)
1173 (and (stringp switches)
1174 (string-match-p (concat "\\(\\`\\| \\)-[[:alnum:]]*" short
1175 (if long (concat "\\|--" long "\\>") ""))
1176 switches))))
1178 (defun dired-switches-escape-p (switches)
1179 "Return non-nil if the string SWITCHES contains -b or --escape."
1180 ;; Do not match things like "--block-size" that happen to contain "b".
1181 (dired-check-switches switches "b" "escape"))
1183 (defun dired-switches-recursive-p (switches)
1184 "Return non-nil if the string SWITCHES contains -R or --recursive."
1185 (dired-check-switches switches "R" "recursive"))
1187 (defun dired-insert-directory (dir switches &optional file-list wildcard hdr)
1188 "Insert a directory listing of DIR, Dired style.
1189 Use SWITCHES to make the listings.
1190 If FILE-LIST is non-nil, list only those files.
1191 Otherwise, if WILDCARD is non-nil, expand wildcards;
1192 in that case, DIR should be a file name that uses wildcards.
1193 In other cases, DIR should be a directory name or a directory filename.
1194 If HDR is non-nil, insert a header line with the directory name."
1195 (let ((opoint (point))
1196 (process-environment (copy-sequence process-environment))
1197 end)
1198 (if (and
1199 ;; Don't try to invoke `ls' if we are on DOS/Windows where
1200 ;; ls-lisp emulation is used, except if they want to use `ls'
1201 ;; as indicated by `ls-lisp-use-insert-directory-program'.
1202 (not (and (featurep 'ls-lisp)
1203 (null ls-lisp-use-insert-directory-program)))
1204 (or (if (eq dired-use-ls-dired 'unspecified)
1205 ;; Check whether "ls --dired" gives exit code 0, and
1206 ;; save the answer in `dired-use-ls-dired'.
1207 (or (setq dired-use-ls-dired
1208 (eq 0 (call-process insert-directory-program
1209 nil nil nil "--dired")))
1210 (progn
1211 (message "ls does not support --dired; \
1212 see `dired-use-ls-dired' for more details.")
1213 nil))
1214 dired-use-ls-dired)
1215 (file-remote-p dir)))
1216 (setq switches (concat "--dired " switches)))
1217 ;; We used to specify the C locale here, to force English month names;
1218 ;; but this should not be necessary any more,
1219 ;; with the new value of `directory-listing-before-filename-regexp'.
1220 (if file-list
1221 (dolist (f file-list)
1222 (let ((beg (point)))
1223 (insert-directory f switches nil nil)
1224 ;; Re-align fields, if necessary.
1225 (dired-align-file beg (point))))
1226 (insert-directory dir switches wildcard (not wildcard)))
1227 ;; Quote certain characters, unless ls quoted them for us.
1228 (if (not (dired-switches-escape-p dired-actual-switches))
1229 (save-excursion
1230 (setq end (point-marker))
1231 (goto-char opoint)
1232 (while (search-forward "\\" end t)
1233 (replace-match (apply #'propertize
1234 "\\\\"
1235 (text-properties-at (match-beginning 0)))
1236 nil t))
1237 (goto-char opoint)
1238 (while (search-forward "\^m" end t)
1239 (replace-match (apply #'propertize
1240 "\\015"
1241 (text-properties-at (match-beginning 0)))
1242 nil t))
1243 (set-marker end nil))
1244 ;; Replace any newlines in DIR with literal "\n"s, for the sake
1245 ;; of the header line. To disambiguate a literal "\n" in the
1246 ;; actual dirname, we also replace "\" with "\\".
1247 ;; Personally, I think this should always be done, irrespective
1248 ;; of the value of dired-actual-switches, because:
1249 ;; i) Dired simply does not work with an unescaped newline in
1250 ;; the directory name used in the header (bug=10469#28), and
1251 ;; ii) "\" is always replaced with "\\" in the listing, so doing
1252 ;; it in the header as well makes things consistent.
1253 ;; But at present it is only done if "-b" is in ls-switches,
1254 ;; because newlines in dirnames are uncommon, and people may
1255 ;; have gotten used to seeing unescaped "\" in the headers.
1256 ;; Note: adjust dired-build-subdir-alist if you change this.
1257 (setq dir (replace-regexp-in-string "\\\\" "\\\\" dir nil t)
1258 dir (replace-regexp-in-string "\n" "\\n" dir nil t)))
1259 ;; If we used --dired and it worked, the lines are already indented.
1260 ;; Otherwise, indent them.
1261 (unless (save-excursion
1262 (goto-char opoint)
1263 (looking-at-p " "))
1264 (let ((indent-tabs-mode nil))
1265 (indent-rigidly opoint (point) 2)))
1266 ;; Insert text at the beginning to standardize things.
1267 (let ((content-point opoint))
1268 (save-excursion
1269 (goto-char opoint)
1270 (when (and (or hdr wildcard)
1271 (not (and (looking-at "^ \\(.*\\):$")
1272 (file-name-absolute-p (match-string 1)))))
1273 ;; Note that dired-build-subdir-alist will replace the name
1274 ;; by its expansion, so it does not matter whether what we insert
1275 ;; here is fully expanded, but it should be absolute.
1276 (insert " " (directory-file-name (file-name-directory dir)) ":\n")
1277 (setq content-point (point)))
1278 (when wildcard
1279 ;; Insert "wildcard" line where "total" line would be for a full dir.
1280 (insert " wildcard " (file-name-nondirectory dir) "\n")))
1281 (dired-insert-set-properties content-point (point)))))
1283 (defun dired-insert-set-properties (beg end)
1284 "Add various text properties to the lines in the region."
1285 (save-excursion
1286 (goto-char beg)
1287 (while (< (point) end)
1288 (ignore-errors
1289 (if (not (dired-move-to-filename))
1290 (unless (or (looking-at-p "^$")
1291 (looking-at-p dired-subdir-regexp))
1292 (put-text-property (line-beginning-position)
1293 (1+ (line-end-position))
1294 'invisible 'dired-hide-details-information))
1295 (put-text-property (+ (line-beginning-position) 1) (1- (point))
1296 'invisible 'dired-hide-details-detail)
1297 (add-text-properties
1298 (point)
1299 (progn
1300 (dired-move-to-end-of-filename)
1301 (point))
1302 '(mouse-face
1303 highlight
1304 dired-filename t
1305 help-echo "mouse-2: visit this file in other window"))
1306 (when (< (+ (point) 4) (line-end-position))
1307 (put-text-property (+ (point) 4) (line-end-position)
1308 'invisible 'dired-hide-details-link))))
1309 (forward-line 1))))
1311 ;; Reverting a dired buffer
1313 (defun dired-revert (&optional _arg _noconfirm)
1314 "Reread the Dired buffer.
1315 Must also be called after `dired-actual-switches' have changed.
1316 Should not fail even on completely garbaged buffers.
1317 Preserves old cursor, marks/flags, hidden-p.
1319 Dired sets `revert-buffer-function' to this function. The args
1320 ARG and NOCONFIRM, passed from `revert-buffer', are ignored."
1321 (widen) ; just in case user narrowed
1322 (let ((modflag (buffer-modified-p))
1323 (positions (dired-save-positions))
1324 (mark-alist nil) ; save marked files
1325 (hidden-subdirs (dired-remember-hidden))
1326 (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
1327 (case-fold-search nil) ; we check for upper case ls flags
1328 (inhibit-read-only t))
1329 (goto-char (point-min))
1330 (setq mark-alist;; only after dired-remember-hidden since this unhides:
1331 (dired-remember-marks (point-min) (point-max)))
1332 ;; treat top level dir extra (it may contain wildcards)
1333 (if (not (consp dired-directory))
1334 (dired-uncache dired-directory)
1335 (dired-uncache (car dired-directory))
1336 (dolist (dir (cdr dired-directory))
1337 (if (file-name-absolute-p dir)
1338 (dired-uncache dir))))
1339 ;; Run dired-after-readin-hook just once, below.
1340 (let ((dired-after-readin-hook nil))
1341 (dired-readin)
1342 (dired-insert-old-subdirs old-subdir-alist))
1343 (dired-mark-remembered mark-alist) ; mark files that were marked
1344 ;; ... run the hook for the whole buffer, and only after markers
1345 ;; have been reinserted (else omitting in dired-x would omit marked files)
1346 (run-hooks 'dired-after-readin-hook) ; no need to narrow
1347 (dired-restore-positions positions)
1348 (save-excursion ; hide subdirs that were hidden
1349 (dolist (dir hidden-subdirs)
1350 (if (dired-goto-subdir dir)
1351 (dired-hide-subdir 1))))
1352 (unless modflag (restore-buffer-modified-p nil)))
1353 ;; outside of the let scope
1354 ;;; Might as well not override the user if the user changed this.
1355 ;;; (setq buffer-read-only t)
1358 ;; Subroutines of dired-revert
1359 ;; Some of these are also used when inserting subdirs.
1361 (defun dired-save-positions ()
1362 "Return current positions in the buffer and all windows with this directory.
1363 The positions have the form (BUFFER-POSITION WINDOW-POSITIONS).
1365 BUFFER-POSITION is the point position in the current Dired buffer.
1366 It has the form (BUFFER DIRED-FILENAME BUFFER-POINT).
1368 WINDOW-POSITIONS are current positions in all windows displaying
1369 this dired buffer. The window positions have the form (WINDOW
1370 DIRED-FILENAME WINDOW-POINT)."
1371 (list
1372 (list (current-buffer) (dired-get-filename nil t) (point))
1373 (mapcar (lambda (w)
1374 (list w
1375 (with-selected-window w
1376 (dired-get-filename nil t))
1377 (window-point w)))
1378 (get-buffer-window-list nil 0 t))))
1380 (defun dired-restore-positions (positions)
1381 "Restore POSITIONS saved with `dired-save-positions'."
1382 (let* ((buf-file-pos (nth 0 positions))
1383 (buffer (nth 0 buf-file-pos)))
1384 (unless (and (nth 1 buf-file-pos)
1385 (dired-goto-file (nth 1 buf-file-pos)))
1386 (goto-char (nth 2 buf-file-pos))
1387 (dired-move-to-filename))
1388 (dolist (win-file-pos (nth 1 positions))
1389 ;; Ensure that window still displays the original buffer.
1390 (when (eq (window-buffer (nth 0 win-file-pos)) buffer)
1391 (with-selected-window (nth 0 win-file-pos)
1392 (unless (and (nth 1 win-file-pos)
1393 (dired-goto-file (nth 1 win-file-pos)))
1394 (goto-char (nth 2 win-file-pos))
1395 (dired-move-to-filename)))))))
1397 (defun dired-remember-marks (beg end)
1398 "Return alist of files and their marks, from BEG to END."
1399 (if selective-display ; must unhide to make this work.
1400 (let ((inhibit-read-only t))
1401 (subst-char-in-region beg end ?\r ?\n)))
1402 (let (fil chr alist)
1403 (save-excursion
1404 (goto-char beg)
1405 (while (re-search-forward dired-re-mark end t)
1406 (if (setq fil (dired-get-filename nil t))
1407 (setq chr (preceding-char)
1408 alist (cons (cons fil chr) alist)))))
1409 alist))
1411 (defun dired-mark-remembered (alist)
1412 "Mark all files remembered in ALIST.
1413 Each element of ALIST looks like (FILE . MARKERCHAR)."
1414 (let (elt fil chr)
1415 (save-excursion
1416 (while alist
1417 (setq elt (car alist)
1418 alist (cdr alist)
1419 fil (car elt)
1420 chr (cdr elt))
1421 (when (dired-goto-file fil)
1422 (beginning-of-line)
1423 (delete-char 1)
1424 (insert chr))))))
1426 (defun dired-remember-hidden ()
1427 "Return a list of names of subdirs currently hidden."
1428 (let ((l dired-subdir-alist) dir pos result)
1429 (while l
1430 (setq dir (car (car l))
1431 pos (cdr (car l))
1432 l (cdr l))
1433 (goto-char pos)
1434 (skip-chars-forward "^\r\n")
1435 (if (eq (following-char) ?\r)
1436 (setq result (cons dir result))))
1437 result))
1439 (defun dired-insert-old-subdirs (old-subdir-alist)
1440 "Try to insert all subdirs that were displayed before.
1441 Do so according to the former subdir alist OLD-SUBDIR-ALIST."
1442 (or (dired-switches-recursive-p dired-actual-switches)
1443 (let (elt dir)
1444 (while old-subdir-alist
1445 (setq elt (car old-subdir-alist)
1446 old-subdir-alist (cdr old-subdir-alist)
1447 dir (car elt))
1448 (ignore-errors
1449 (dired-uncache dir)
1450 (dired-insert-subdir dir))))))
1452 (defun dired-uncache (dir)
1453 "Remove directory DIR from any directory cache."
1454 (let ((handler (find-file-name-handler dir 'dired-uncache)))
1455 (if handler
1456 (funcall handler 'dired-uncache dir))))
1458 ;; dired mode key bindings and initialization
1460 (defvar dired-mode-map
1461 ;; This looks ugly when substitute-command-keys uses C-d instead d:
1462 ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
1463 (let ((map (make-keymap)))
1464 (set-keymap-parent map special-mode-map)
1465 (define-key map [mouse-2] 'dired-mouse-find-file-other-window)
1466 (define-key map [follow-link] 'mouse-face)
1467 ;; Commands to mark or flag certain categories of files
1468 (define-key map "#" 'dired-flag-auto-save-files)
1469 (define-key map "." 'dired-clean-directory)
1470 (define-key map "~" 'dired-flag-backup-files)
1471 ;; Upper case keys (except !) for operating on the marked files
1472 (define-key map "A" 'dired-do-find-regexp)
1473 (define-key map "C" 'dired-do-copy)
1474 (define-key map "B" 'dired-do-byte-compile)
1475 (define-key map "D" 'dired-do-delete)
1476 (define-key map "G" 'dired-do-chgrp)
1477 (define-key map "H" 'dired-do-hardlink)
1478 (define-key map "L" 'dired-do-load)
1479 (define-key map "M" 'dired-do-chmod)
1480 (define-key map "O" 'dired-do-chown)
1481 (define-key map "P" 'dired-do-print)
1482 (define-key map "Q" 'dired-do-find-regexp-and-replace)
1483 (define-key map "R" 'dired-do-rename)
1484 (define-key map "S" 'dired-do-symlink)
1485 (define-key map "T" 'dired-do-touch)
1486 (define-key map "X" 'dired-do-shell-command)
1487 (define-key map "Z" 'dired-do-compress)
1488 (define-key map "c" 'dired-do-compress-to)
1489 (define-key map "!" 'dired-do-shell-command)
1490 (define-key map "&" 'dired-do-async-shell-command)
1491 ;; Comparison commands
1492 (define-key map "=" 'dired-diff)
1493 ;; Tree Dired commands
1494 (define-key map "\M-\C-?" 'dired-unmark-all-files)
1495 (define-key map "\M-\C-d" 'dired-tree-down)
1496 (define-key map "\M-\C-u" 'dired-tree-up)
1497 (define-key map "\M-\C-n" 'dired-next-subdir)
1498 (define-key map "\M-\C-p" 'dired-prev-subdir)
1499 ;; move to marked files
1500 (define-key map "\M-{" 'dired-prev-marked-file)
1501 (define-key map "\M-}" 'dired-next-marked-file)
1502 ;; Make all regexp commands share a `%' prefix:
1503 ;; We used to get to the submap via a symbol dired-regexp-prefix,
1504 ;; but that seems to serve little purpose, and copy-keymap
1505 ;; does a better job without it.
1506 (define-key map "%" nil)
1507 (define-key map "%u" 'dired-upcase)
1508 (define-key map "%l" 'dired-downcase)
1509 (define-key map "%d" 'dired-flag-files-regexp)
1510 (define-key map "%g" 'dired-mark-files-containing-regexp)
1511 (define-key map "%m" 'dired-mark-files-regexp)
1512 (define-key map "%r" 'dired-do-rename-regexp)
1513 (define-key map "%C" 'dired-do-copy-regexp)
1514 (define-key map "%H" 'dired-do-hardlink-regexp)
1515 (define-key map "%R" 'dired-do-rename-regexp)
1516 (define-key map "%S" 'dired-do-symlink-regexp)
1517 (define-key map "%&" 'dired-flag-garbage-files)
1518 ;; Commands for marking and unmarking.
1519 (define-key map "*" nil)
1520 (define-key map "**" 'dired-mark-executables)
1521 (define-key map "*/" 'dired-mark-directories)
1522 (define-key map "*@" 'dired-mark-symlinks)
1523 (define-key map "*%" 'dired-mark-files-regexp)
1524 (define-key map "*c" 'dired-change-marks)
1525 (define-key map "*s" 'dired-mark-subdir-files)
1526 (define-key map "*m" 'dired-mark)
1527 (define-key map "*u" 'dired-unmark)
1528 (define-key map "*?" 'dired-unmark-all-files)
1529 (define-key map "*!" 'dired-unmark-all-marks)
1530 (define-key map "U" 'dired-unmark-all-marks)
1531 (define-key map "*\177" 'dired-unmark-backward)
1532 (define-key map "*\C-n" 'dired-next-marked-file)
1533 (define-key map "*\C-p" 'dired-prev-marked-file)
1534 (define-key map "*t" 'dired-toggle-marks)
1535 ;; Lower keys for commands not operating on all the marked files
1536 (define-key map "a" 'dired-find-alternate-file)
1537 (define-key map "d" 'dired-flag-file-deletion)
1538 (define-key map "e" 'dired-find-file)
1539 (define-key map "f" 'dired-find-file)
1540 (define-key map "\C-m" 'dired-find-file)
1541 (put 'dired-find-file :advertised-binding "\C-m")
1542 (define-key map "g" 'revert-buffer)
1543 (define-key map "i" 'dired-maybe-insert-subdir)
1544 (define-key map "j" 'dired-goto-file)
1545 (define-key map "k" 'dired-do-kill-lines)
1546 (define-key map "l" 'dired-do-redisplay)
1547 (define-key map "m" 'dired-mark)
1548 (define-key map "n" 'dired-next-line)
1549 (define-key map "o" 'dired-find-file-other-window)
1550 (define-key map "\C-o" 'dired-display-file)
1551 (define-key map "p" 'dired-previous-line)
1552 (define-key map "s" 'dired-sort-toggle-or-edit)
1553 (define-key map "t" 'dired-toggle-marks)
1554 (define-key map "u" 'dired-unmark)
1555 (define-key map "v" 'dired-view-file)
1556 (define-key map "w" 'dired-copy-filename-as-kill)
1557 (define-key map "W" 'browse-url-of-dired-file)
1558 (define-key map "x" 'dired-do-flagged-delete)
1559 (define-key map "y" 'dired-show-file-type)
1560 (define-key map "+" 'dired-create-directory)
1561 ;; moving
1562 (define-key map "<" 'dired-prev-dirline)
1563 (define-key map ">" 'dired-next-dirline)
1564 (define-key map "^" 'dired-up-directory)
1565 (define-key map " " 'dired-next-line)
1566 (define-key map [?\S-\ ] 'dired-previous-line)
1567 (define-key map [remap next-line] 'dired-next-line)
1568 (define-key map [remap previous-line] 'dired-previous-line)
1569 ;; hiding
1570 (define-key map "$" 'dired-hide-subdir)
1571 (define-key map "\M-$" 'dired-hide-all)
1572 (define-key map "(" 'dired-hide-details-mode)
1573 ;; isearch
1574 (define-key map (kbd "M-s a C-s") 'dired-do-isearch)
1575 (define-key map (kbd "M-s a M-C-s") 'dired-do-isearch-regexp)
1576 (define-key map (kbd "M-s f C-s") 'dired-isearch-filenames)
1577 (define-key map (kbd "M-s f M-C-s") 'dired-isearch-filenames-regexp)
1578 ;; misc
1579 (define-key map [remap read-only-mode] 'dired-toggle-read-only)
1580 ;; `toggle-read-only' is an obsolete alias for `read-only-mode'
1581 (define-key map [remap toggle-read-only] 'dired-toggle-read-only)
1582 (define-key map "?" 'dired-summary)
1583 (define-key map "\177" 'dired-unmark-backward)
1584 (define-key map [remap undo] 'dired-undo)
1585 (define-key map [remap advertised-undo] 'dired-undo)
1586 ;; thumbnail manipulation (image-dired)
1587 (define-key map "\C-td" 'image-dired-display-thumbs)
1588 (define-key map "\C-tt" 'image-dired-tag-files)
1589 (define-key map "\C-tr" 'image-dired-delete-tag)
1590 (define-key map "\C-tj" 'image-dired-jump-thumbnail-buffer)
1591 (define-key map "\C-ti" 'image-dired-dired-display-image)
1592 (define-key map "\C-tx" 'image-dired-dired-display-external)
1593 (define-key map "\C-ta" 'image-dired-display-thumbs-append)
1594 (define-key map "\C-t." 'image-dired-display-thumb)
1595 (define-key map "\C-tc" 'image-dired-dired-comment-files)
1596 (define-key map "\C-tf" 'image-dired-mark-tagged-files)
1597 (define-key map "\C-t\C-t" 'image-dired-dired-toggle-marked-thumbs)
1598 (define-key map "\C-te" 'image-dired-dired-edit-comment-and-tags)
1599 ;; encryption and decryption (epa-dired)
1600 (define-key map ":d" 'epa-dired-do-decrypt)
1601 (define-key map ":v" 'epa-dired-do-verify)
1602 (define-key map ":s" 'epa-dired-do-sign)
1603 (define-key map ":e" 'epa-dired-do-encrypt)
1605 ;; Make menu bar items.
1607 ;; No need to fo this, now that top-level items are fewer.
1608 ;;;;
1609 ;; Get rid of the Edit menu bar item to save space.
1610 ;(define-key map [menu-bar edit] 'undefined)
1612 (define-key map [menu-bar subdir]
1613 (cons "Subdir" (make-sparse-keymap "Subdir")))
1615 (define-key map [menu-bar subdir hide-all]
1616 '(menu-item "Hide All" dired-hide-all
1617 :help "Hide all subdirectories, leave only header lines"))
1618 (define-key map [menu-bar subdir hide-subdir]
1619 '(menu-item "Hide/UnHide Subdir" dired-hide-subdir
1620 :help "Hide or unhide current directory listing"))
1621 (define-key map [menu-bar subdir tree-down]
1622 '(menu-item "Tree Down" dired-tree-down
1623 :help "Go to first subdirectory header down the tree"))
1624 (define-key map [menu-bar subdir tree-up]
1625 '(menu-item "Tree Up" dired-tree-up
1626 :help "Go to first subdirectory header up the tree"))
1627 (define-key map [menu-bar subdir up]
1628 '(menu-item "Up Directory" dired-up-directory
1629 :help "Edit the parent directory"))
1630 (define-key map [menu-bar subdir prev-subdir]
1631 '(menu-item "Prev Subdir" dired-prev-subdir
1632 :help "Go to previous subdirectory header line"))
1633 (define-key map [menu-bar subdir next-subdir]
1634 '(menu-item "Next Subdir" dired-next-subdir
1635 :help "Go to next subdirectory header line"))
1636 (define-key map [menu-bar subdir prev-dirline]
1637 '(menu-item "Prev Dirline" dired-prev-dirline
1638 :help "Move to next directory-file line"))
1639 (define-key map [menu-bar subdir next-dirline]
1640 '(menu-item "Next Dirline" dired-next-dirline
1641 :help "Move to previous directory-file line"))
1642 (define-key map [menu-bar subdir insert]
1643 '(menu-item "Insert This Subdir" dired-maybe-insert-subdir
1644 :help "Insert contents of subdirectory"
1645 :enable (let ((f (dired-get-filename nil t)))
1646 (and f (file-directory-p f)))))
1647 (define-key map [menu-bar immediate]
1648 (cons "Immediate" (make-sparse-keymap "Immediate")))
1650 (define-key map
1651 [menu-bar immediate image-dired-dired-display-external]
1652 '(menu-item "Display Image Externally" image-dired-dired-display-external
1653 :help "Display image in external viewer"))
1654 (define-key map
1655 [menu-bar immediate image-dired-dired-display-image]
1656 '(menu-item "Display Image" image-dired-dired-display-image
1657 :help "Display sized image in a separate window"))
1658 (define-key map
1659 [menu-bar immediate image-dired-dired-toggle-marked-thumbs]
1660 '(menu-item "Toggle Image Thumbnails in This Buffer" image-dired-dired-toggle-marked-thumbs
1661 :help "Add or remove image thumbnails in front of marked file names"))
1663 (define-key map [menu-bar immediate hide-details]
1664 '(menu-item "Hide Details" dired-hide-details-mode
1665 :help "Hide details in buffer"
1666 :button (:toggle . dired-hide-details-mode)))
1667 (define-key map [menu-bar immediate revert-buffer]
1668 '(menu-item "Refresh" revert-buffer
1669 :help "Update contents of shown directories"))
1671 (define-key map [menu-bar immediate dashes]
1672 '("--"))
1674 (define-key map [menu-bar immediate isearch-filenames-regexp]
1675 '(menu-item "Isearch Regexp in File Names..." dired-isearch-filenames-regexp
1676 :help "Incrementally search for regexp in file names only"))
1677 (define-key map [menu-bar immediate isearch-filenames]
1678 '(menu-item "Isearch in File Names..." dired-isearch-filenames
1679 :help "Incrementally search for string in file names only."))
1680 (define-key map [menu-bar immediate compare-directories]
1681 '(menu-item "Compare Directories..." dired-compare-directories
1682 :help "Mark files with different attributes in two Dired buffers"))
1683 (define-key map [menu-bar immediate backup-diff]
1684 '(menu-item "Compare with Backup" dired-backup-diff
1685 :help "Diff file at cursor with its latest backup"))
1686 (define-key map [menu-bar immediate diff]
1687 '(menu-item "Diff..." dired-diff
1688 :help "Compare file at cursor with another file"))
1689 (define-key map [menu-bar immediate view]
1690 '(menu-item "View This File" dired-view-file
1691 :help "Examine file at cursor in read-only mode"))
1692 (define-key map [menu-bar immediate display]
1693 '(menu-item "Display in Other Window" dired-display-file
1694 :help "Display file at cursor in other window"))
1695 (define-key map [menu-bar immediate find-file-other-window]
1696 '(menu-item "Find in Other Window" dired-find-file-other-window
1697 :help "Edit file at cursor in other window"))
1698 (define-key map [menu-bar immediate find-file]
1699 '(menu-item "Find This File" dired-find-file
1700 :help "Edit file at cursor"))
1701 (define-key map [menu-bar immediate create-directory]
1702 '(menu-item "Create Directory..." dired-create-directory
1703 :help "Create a directory"))
1704 (define-key map [menu-bar immediate wdired-mode]
1705 '(menu-item "Edit File Names" wdired-change-to-wdired-mode
1706 :help "Put a Dired buffer in a mode in which filenames are editable"
1707 :keys "C-x C-q"
1708 :filter (lambda (x) (if (eq major-mode 'dired-mode) x))))
1710 (define-key map [menu-bar regexp]
1711 (cons "Regexp" (make-sparse-keymap "Regexp")))
1713 (define-key map
1714 [menu-bar regexp image-dired-mark-tagged-files]
1715 '(menu-item "Mark From Image Tag..." image-dired-mark-tagged-files
1716 :help "Mark files whose image tags matches regexp"))
1718 (define-key map [menu-bar regexp dashes-1]
1719 '("--"))
1721 (define-key map [menu-bar regexp downcase]
1722 '(menu-item "Downcase" dired-downcase
1723 ;; When running on plain MS-DOS, there's only one
1724 ;; letter-case for file names.
1725 :enable (or (not (fboundp 'msdos-long-file-names))
1726 (msdos-long-file-names))
1727 :help "Rename marked files to lower-case name"))
1728 (define-key map [menu-bar regexp upcase]
1729 '(menu-item "Upcase" dired-upcase
1730 :enable (or (not (fboundp 'msdos-long-file-names))
1731 (msdos-long-file-names))
1732 :help "Rename marked files to upper-case name"))
1733 (define-key map [menu-bar regexp hardlink]
1734 '(menu-item "Hardlink..." dired-do-hardlink-regexp
1735 :help "Make hard links for files matching regexp"))
1736 (define-key map [menu-bar regexp symlink]
1737 '(menu-item "Symlink..." dired-do-symlink-regexp
1738 :visible (fboundp 'make-symbolic-link)
1739 :help "Make symbolic links for files matching regexp"))
1740 (define-key map [menu-bar regexp rename]
1741 '(menu-item "Rename..." dired-do-rename-regexp
1742 :help "Rename marked files matching regexp"))
1743 (define-key map [menu-bar regexp copy]
1744 '(menu-item "Copy..." dired-do-copy-regexp
1745 :help "Copy marked files matching regexp"))
1746 (define-key map [menu-bar regexp flag]
1747 '(menu-item "Flag..." dired-flag-files-regexp
1748 :help "Flag files matching regexp for deletion"))
1749 (define-key map [menu-bar regexp mark]
1750 '(menu-item "Mark..." dired-mark-files-regexp
1751 :help "Mark files matching regexp for future operations"))
1752 (define-key map [menu-bar regexp mark-cont]
1753 '(menu-item "Mark Containing..." dired-mark-files-containing-regexp
1754 :help "Mark files whose contents matches regexp"))
1756 (define-key map [menu-bar mark]
1757 (cons "Mark" (make-sparse-keymap "Mark")))
1759 (define-key map [menu-bar mark prev]
1760 '(menu-item "Previous Marked" dired-prev-marked-file
1761 :help "Move to previous marked file"))
1762 (define-key map [menu-bar mark next]
1763 '(menu-item "Next Marked" dired-next-marked-file
1764 :help "Move to next marked file"))
1765 (define-key map [menu-bar mark marks]
1766 '(menu-item "Change Marks..." dired-change-marks
1767 :help "Replace marker with another character"))
1768 (define-key map [menu-bar mark unmark-all]
1769 '(menu-item "Unmark All" dired-unmark-all-marks))
1770 (define-key map [menu-bar mark symlinks]
1771 '(menu-item "Mark Symlinks" dired-mark-symlinks
1772 :visible (fboundp 'make-symbolic-link)
1773 :help "Mark all symbolic links"))
1774 (define-key map [menu-bar mark directories]
1775 '(menu-item "Mark Directories" dired-mark-directories
1776 :help "Mark all directories except `.' and `..'"))
1777 (define-key map [menu-bar mark directory]
1778 '(menu-item "Mark Old Backups" dired-clean-directory
1779 :help "Flag old numbered backups for deletion"))
1780 (define-key map [menu-bar mark executables]
1781 '(menu-item "Mark Executables" dired-mark-executables
1782 :help "Mark all executable files"))
1783 (define-key map [menu-bar mark garbage-files]
1784 '(menu-item "Flag Garbage Files" dired-flag-garbage-files
1785 :help "Flag unneeded files for deletion"))
1786 (define-key map [menu-bar mark backup-files]
1787 '(menu-item "Flag Backup Files" dired-flag-backup-files
1788 :help "Flag all backup files for deletion"))
1789 (define-key map [menu-bar mark auto-save-files]
1790 '(menu-item "Flag Auto-save Files" dired-flag-auto-save-files
1791 :help "Flag auto-save files for deletion"))
1792 (define-key map [menu-bar mark deletion]
1793 '(menu-item "Flag" dired-flag-file-deletion
1794 :help "Flag current line's file for deletion"))
1795 (define-key map [menu-bar mark unmark]
1796 '(menu-item "Unmark" dired-unmark
1797 :help "Unmark or unflag current line's file"))
1798 (define-key map [menu-bar mark mark]
1799 '(menu-item "Mark" dired-mark
1800 :help "Mark current line's file for future operations"))
1801 (define-key map [menu-bar mark toggle-marks]
1802 '(menu-item "Toggle Marks" dired-toggle-marks
1803 :help "Mark unmarked files, unmark marked ones"))
1805 (define-key map [menu-bar operate]
1806 (cons "Operate" (make-sparse-keymap "Operate")))
1808 (define-key map
1809 [menu-bar operate image-dired-delete-tag]
1810 '(menu-item "Delete Image Tag..." image-dired-delete-tag
1811 :help "Delete image tag from current or marked files"))
1812 (define-key map
1813 [menu-bar operate image-dired-tag-files]
1814 '(menu-item "Add Image Tags..." image-dired-tag-files
1815 :help "Add image tags to current or marked files"))
1816 (define-key map
1817 [menu-bar operate image-dired-dired-comment-files]
1818 '(menu-item "Add Image Comment..." image-dired-dired-comment-files
1819 :help "Add image comment to current or marked files"))
1820 (define-key map
1821 [menu-bar operate image-dired-display-thumbs]
1822 '(menu-item "Display Image Thumbnails" image-dired-display-thumbs
1823 :help "Display image thumbnails for current or marked image files"))
1825 (define-key map [menu-bar operate dashes-4]
1826 '("--"))
1828 (define-key map
1829 [menu-bar operate epa-dired-do-decrypt]
1830 '(menu-item "Decrypt..." epa-dired-do-decrypt
1831 :help "Decrypt current or marked files"))
1833 (define-key map
1834 [menu-bar operate epa-dired-do-verify]
1835 '(menu-item "Verify" epa-dired-do-verify
1836 :help "Verify digital signature of current or marked files"))
1838 (define-key map
1839 [menu-bar operate epa-dired-do-sign]
1840 '(menu-item "Sign..." epa-dired-do-sign
1841 :help "Create digital signature of current or marked files"))
1843 (define-key map
1844 [menu-bar operate epa-dired-do-encrypt]
1845 '(menu-item "Encrypt..." epa-dired-do-encrypt
1846 :help "Encrypt current or marked files"))
1848 (define-key map [menu-bar operate dashes-3]
1849 '("--"))
1851 (define-key map [menu-bar operate query-replace]
1852 '(menu-item "Query Replace in Files..." dired-do-find-regexp-and-replace
1853 :help "Replace regexp matches in marked files"))
1854 (define-key map [menu-bar operate search]
1855 '(menu-item "Search Files..." dired-do-find-regexp
1856 :help "Search marked files for matches of regexp"))
1857 (define-key map [menu-bar operate isearch-regexp]
1858 '(menu-item "Isearch Regexp Files..." dired-do-isearch-regexp
1859 :help "Incrementally search marked files for regexp"))
1860 (define-key map [menu-bar operate isearch]
1861 '(menu-item "Isearch Files..." dired-do-isearch
1862 :help "Incrementally search marked files for string"))
1863 (define-key map [menu-bar operate chown]
1864 '(menu-item "Change Owner..." dired-do-chown
1865 :visible (not (memq system-type '(ms-dos windows-nt)))
1866 :help "Change the owner of marked files"))
1867 (define-key map [menu-bar operate chgrp]
1868 '(menu-item "Change Group..." dired-do-chgrp
1869 :visible (not (memq system-type '(ms-dos windows-nt)))
1870 :help "Change the group of marked files"))
1871 (define-key map [menu-bar operate chmod]
1872 '(menu-item "Change Mode..." dired-do-chmod
1873 :help "Change mode (attributes) of marked files"))
1874 (define-key map [menu-bar operate touch]
1875 '(menu-item "Change Timestamp..." dired-do-touch
1876 :help "Change timestamp of marked files"))
1877 (define-key map [menu-bar operate load]
1878 '(menu-item "Load" dired-do-load
1879 :help "Load marked Emacs Lisp files"))
1880 (define-key map [menu-bar operate compile]
1881 '(menu-item "Byte-compile" dired-do-byte-compile
1882 :help "Byte-compile marked Emacs Lisp files"))
1883 (define-key map [menu-bar operate compress]
1884 '(menu-item "Compress" dired-do-compress
1885 :help "Compress/uncompress marked files"))
1886 (define-key map [menu-bar operate print]
1887 '(menu-item "Print..." dired-do-print
1888 :help "Ask for print command and print marked files"))
1889 (define-key map [menu-bar operate hardlink]
1890 '(menu-item "Hardlink to..." dired-do-hardlink
1891 :help "Make hard links for current or marked files"))
1892 (define-key map [menu-bar operate symlink]
1893 '(menu-item "Symlink to..." dired-do-symlink
1894 :visible (fboundp 'make-symbolic-link)
1895 :help "Make symbolic links for current or marked files"))
1896 (define-key map [menu-bar operate async-command]
1897 '(menu-item "Asynchronous Shell Command..." dired-do-async-shell-command
1898 :help "Run a shell command asynchronously on current or marked files"))
1899 (define-key map [menu-bar operate command]
1900 '(menu-item "Shell Command..." dired-do-shell-command
1901 :help "Run a shell command on current or marked files"))
1902 (define-key map [menu-bar operate delete]
1903 '(menu-item "Delete" dired-do-delete
1904 :help "Delete current file or all marked files"))
1905 (define-key map [menu-bar operate rename]
1906 '(menu-item "Rename to..." dired-do-rename
1907 :help "Rename current file or move marked files"))
1908 (define-key map [menu-bar operate copy]
1909 '(menu-item "Copy to..." dired-do-copy
1910 :help "Copy current file or all marked files"))
1912 map)
1913 "Local keymap for Dired mode buffers.")
1915 ;; Dired mode is suitable only for specially formatted data.
1916 (put 'dired-mode 'mode-class 'special)
1918 ;; Autoload cookie needed by desktop.el
1919 ;;;###autoload
1920 (defun dired-mode (&optional dirname switches)
1922 Mode for \"editing\" directory listings.
1923 In Dired, you are \"editing\" a list of the files in a directory and
1924 (optionally) its subdirectories, in the format of `ls -lR'.
1925 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
1926 \"Editing\" means that you can run shell commands on files, visit,
1927 compress, load or byte-compile them, change their file attributes
1928 and insert subdirectories into the same buffer. You can \"mark\"
1929 files for later commands or \"flag\" them for deletion, either file
1930 by file or all files matching certain criteria.
1931 You can move using the usual cursor motion commands.\\<dired-mode-map>
1932 The buffer is read-only. Digits are prefix arguments.
1933 Type \\[dired-flag-file-deletion] to flag a file `D' for deletion.
1934 Type \\[dired-mark] to Mark a file or subdirectory for later commands.
1935 Most commands operate on the marked files and use the current file
1936 if no files are marked. Use a numeric prefix argument to operate on
1937 the next ARG (or previous -ARG if ARG<0) files, or just `1'
1938 to operate on the current file only. Prefix arguments override marks.
1939 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
1940 to see why something went wrong.
1941 Type \\[dired-unmark] to Unmark a file or all files of an inserted subdirectory.
1942 Type \\[dired-unmark-backward] to back up one line and unmark or unflag.
1943 Type \\[dired-do-flagged-delete] to delete (eXpunge) the files flagged `D'.
1944 Type \\[dired-find-file] to Find the current line's file
1945 (or dired it in another buffer, if it is a directory).
1946 Type \\[dired-find-file-other-window] to find file or Dired directory in Other window.
1947 Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
1948 Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
1949 Type \\[dired-do-copy] to Copy files.
1950 Type \\[dired-sort-toggle-or-edit] to toggle Sorting by name/date or change the `ls' switches.
1951 Type \\[revert-buffer] to read all currently expanded directories aGain.
1952 This retains all marks and hides subdirs again that were hidden before.
1953 Use `SPC' and `DEL' to move down and up by lines.
1955 If Dired ever gets confused, you can either type \\[revert-buffer] \
1956 to read the
1957 directories again, type \\[dired-do-redisplay] \
1958 to relist the file at point or the marked files or a
1959 subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
1960 again for the directory tree.
1962 Customization variables (rename this buffer and type \\[describe-variable] on each line
1963 for more info):
1965 `dired-listing-switches'
1966 `dired-trivial-filenames'
1967 `dired-marker-char'
1968 `dired-del-marker'
1969 `dired-keep-marker-rename'
1970 `dired-keep-marker-copy'
1971 `dired-keep-marker-hardlink'
1972 `dired-keep-marker-symlink'
1974 Hooks (use \\[describe-variable] to see their documentation):
1976 `dired-before-readin-hook'
1977 `dired-after-readin-hook'
1978 `dired-mode-hook'
1979 `dired-load-hook'
1981 Keybindings:
1982 \\{dired-mode-map}"
1983 ;; Not to be called interactively (e.g. dired-directory will be set
1984 ;; to default-directory, which is wrong with wildcards).
1985 (kill-all-local-variables)
1986 (use-local-map dired-mode-map)
1987 (dired-advertise) ; default-directory is already set
1988 (setq major-mode 'dired-mode
1989 mode-name "Dired"
1990 ;; case-fold-search nil
1991 buffer-read-only t
1992 selective-display t ; for subdirectory hiding
1993 mode-line-buffer-identification
1994 (propertized-buffer-identification "%17b"))
1995 ;; Ignore dired-hide-details-* value of invisible text property by default.
1996 (when (eq buffer-invisibility-spec t)
1997 (setq buffer-invisibility-spec (list t)))
1998 (setq-local revert-buffer-function (function dired-revert))
1999 (setq-local buffer-stale-function (function dired-buffer-stale-p))
2000 (setq-local page-delimiter "\n\n")
2001 (setq-local dired-directory (or dirname default-directory))
2002 ;; list-buffers uses this to display the dir being edited in this buffer.
2003 (setq list-buffers-directory
2004 (expand-file-name (if (listp dired-directory)
2005 (car dired-directory)
2006 dired-directory)))
2007 (setq-local dired-actual-switches (or switches dired-listing-switches))
2008 (setq-local font-lock-defaults
2009 '(dired-font-lock-keywords t nil nil beginning-of-line))
2010 (setq-local desktop-save-buffer 'dired-desktop-buffer-misc-data)
2011 (setq dired-switches-alist nil)
2012 (hack-dir-local-variables-non-file-buffer) ; before sorting
2013 (dired-sort-other dired-actual-switches t)
2014 (when (featurep 'dnd)
2015 (setq-local dnd-protocol-alist
2016 (append dired-dnd-protocol-alist dnd-protocol-alist)))
2017 (add-hook 'file-name-at-point-functions 'dired-file-name-at-point nil t)
2018 (add-hook 'isearch-mode-hook 'dired-isearch-filenames-setup nil t)
2019 (run-mode-hooks 'dired-mode-hook))
2021 ;; Idiosyncratic dired commands that don't deal with marks.
2023 (defun dired-summary ()
2024 "Summarize basic Dired commands and show recent Dired errors."
2025 (interactive)
2026 (dired-why)
2027 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
2028 (message
2029 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
2031 (defun dired-undo ()
2032 "Undo in a Dired buffer.
2033 This doesn't recover lost files, it just undoes changes in the buffer itself.
2034 You can use it to recover marks, killed lines or subdirs."
2035 (interactive)
2036 (let ((inhibit-read-only t))
2037 (undo))
2038 (dired-build-subdir-alist)
2039 (message "Change in Dired buffer undone.
2040 Actual changes in files cannot be undone by Emacs."))
2042 (defun dired-toggle-read-only ()
2043 "Edit Dired buffer with Wdired, or make it read-only.
2044 If the current buffer can be edited with Wdired, (i.e. the major
2045 mode is `dired-mode'), call `wdired-change-to-wdired-mode'.
2046 Otherwise, toggle `read-only-mode'."
2047 (interactive)
2048 (if (derived-mode-p 'dired-mode)
2049 (wdired-change-to-wdired-mode)
2050 (read-only-mode 'toggle)))
2052 (defun dired-next-line (arg)
2053 "Move down lines then position at filename.
2054 Optional prefix ARG says how many lines to move; default is one line."
2055 (interactive "^p")
2056 (let ((line-move-visual)
2057 (goal-column))
2058 (line-move arg t))
2059 ;; We never want to move point into an invisible line.
2060 (while (and (invisible-p (point))
2061 (not (if (and arg (< arg 0)) (bobp) (eobp))))
2062 (forward-char (if (and arg (< arg 0)) -1 1)))
2063 (dired-move-to-filename))
2065 (defun dired-previous-line (arg)
2066 "Move up lines then position at filename.
2067 Optional prefix ARG says how many lines to move; default is one line."
2068 (interactive "^p")
2069 (dired-next-line (- (or arg 1))))
2071 (defun dired-next-dirline (arg &optional opoint)
2072 "Goto ARGth next directory file line."
2073 (interactive "p")
2074 (or opoint (setq opoint (point)))
2075 (if (if (> arg 0)
2076 (re-search-forward dired-re-dir nil t arg)
2077 (beginning-of-line)
2078 (re-search-backward dired-re-dir nil t (- arg)))
2079 (dired-move-to-filename) ; user may type `i' or `f'
2080 (goto-char opoint)
2081 (error "No more subdirectories")))
2083 (defun dired-prev-dirline (arg)
2084 "Goto ARGth previous directory file line."
2085 (interactive "p")
2086 (dired-next-dirline (- arg)))
2088 (defun dired-up-directory (&optional other-window)
2089 "Run Dired on parent directory of current directory.
2090 Find the parent directory either in this buffer or another buffer.
2091 Creates a buffer if necessary.
2092 If OTHER-WINDOW (the optional prefix arg), display the parent
2093 directory in another window."
2094 (interactive "P")
2095 (let* ((dir (dired-current-directory))
2096 (up (file-name-directory (directory-file-name dir))))
2097 (or (dired-goto-file (directory-file-name dir))
2098 ;; Only try dired-goto-subdir if buffer has more than one dir.
2099 (and (cdr dired-subdir-alist)
2100 (dired-goto-subdir up))
2101 (progn
2102 (if other-window
2103 (dired-other-window up)
2104 (dired up))
2105 (dired-goto-file dir)))))
2107 (defun dired-get-file-for-visit ()
2108 "Get the current line's file name, with an error if file does not exist."
2109 (interactive)
2110 ;; We pass t for second arg so that we don't get error for `.' and `..'.
2111 (let ((raw (dired-get-filename nil t))
2112 file-name)
2113 (if (null raw)
2114 (error "No file on this line"))
2115 (setq file-name (file-name-sans-versions raw t))
2116 (if (file-exists-p file-name)
2117 file-name
2118 (if (file-symlink-p file-name)
2119 (error "File is a symlink to a nonexistent target")
2120 (error "File no longer exists; type `g' to update Dired buffer")))))
2122 ;; Force C-m keybinding rather than `f' or `e' in the mode doc:
2123 (define-obsolete-function-alias 'dired-advertised-find-file 'dired-find-file "23.2")
2124 (defun dired-find-file ()
2125 "In Dired, visit the file or directory named on this line."
2126 (interactive)
2127 ;; Bind `find-file-run-dired' so that the command works on directories
2128 ;; too, independent of the user's setting.
2129 (let ((find-file-run-dired t))
2130 (find-file (dired-get-file-for-visit))))
2132 (defun dired-find-alternate-file ()
2133 "In Dired, visit this file or directory instead of the Dired buffer."
2134 (interactive)
2135 (set-buffer-modified-p nil)
2136 (find-alternate-file (dired-get-file-for-visit)))
2137 ;; Don't override the setting from .emacs.
2138 ;;;###autoload (put 'dired-find-alternate-file 'disabled t)
2140 (defun dired-mouse-find-file-other-window (event)
2141 "In Dired, visit the file or directory name you click on."
2142 (interactive "e")
2143 (let (window pos file)
2144 (save-excursion
2145 (setq window (posn-window (event-end event))
2146 pos (posn-point (event-end event)))
2147 (if (not (windowp window))
2148 (error "No file chosen"))
2149 (set-buffer (window-buffer window))
2150 (goto-char pos)
2151 (setq file (dired-get-file-for-visit)))
2152 (if (file-directory-p file)
2153 (or (and (cdr dired-subdir-alist)
2154 (dired-goto-subdir file))
2155 (progn
2156 (select-window window)
2157 (dired-other-window file)))
2158 (select-window window)
2159 (find-file-other-window (file-name-sans-versions file t)))))
2161 (defun dired-view-file ()
2162 "In Dired, examine a file in view mode, returning to Dired when done.
2163 When file is a directory, show it in this buffer if it is inserted.
2164 Otherwise, display it in another buffer."
2165 (interactive)
2166 (let ((file (dired-get-file-for-visit)))
2167 (if (file-directory-p file)
2168 (or (and (cdr dired-subdir-alist)
2169 (dired-goto-subdir file))
2170 (dired file))
2171 (view-file file))))
2173 (defun dired-find-file-other-window ()
2174 "In Dired, visit this file or directory in another window."
2175 (interactive)
2176 (find-file-other-window (dired-get-file-for-visit)))
2178 (defun dired-display-file ()
2179 "In Dired, display this file or directory in another window."
2180 (interactive)
2181 (display-buffer (find-file-noselect (dired-get-file-for-visit))
2184 ;;; Functions for extracting and manipulating file names in Dired buffers.
2186 (defun dired-get-filename (&optional localp no-error-if-not-filep)
2187 "In Dired, return name of file mentioned on this line.
2188 Value returned normally includes the directory name.
2189 Optional arg LOCALP with value `no-dir' means don't include directory
2190 name in result. A value of `verbatim' means to return the name exactly as
2191 it occurs in the buffer, and a value of t means construct name relative to
2192 `default-directory', which still may contain slashes if in a subdirectory.
2193 Optional arg NO-ERROR-IF-NOT-FILEP means treat `.' and `..' as
2194 regular filenames and return nil if no filename on this line.
2195 Otherwise, an error occurs in these cases."
2196 (let (case-fold-search file p1 p2 already-absolute)
2197 (save-excursion
2198 (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
2199 (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
2200 ;; nil if no file on this line, but no-error-if-not-filep is t:
2201 (if (setq file (and p1 p2 (buffer-substring p1 p2)))
2202 (progn
2203 ;; Get rid of the mouse-face property that file names have.
2204 (set-text-properties 0 (length file) nil file)
2205 ;; Unquote names quoted by ls or by dired-insert-directory.
2206 ;; This code was written using `read' to unquote, because
2207 ;; it's faster than substituting \007 (4 chars) -> ^G (1
2208 ;; char) etc. in a lisp loop. Unfortunately, this decision
2209 ;; has necessitated hacks such as dealing with filenames
2210 ;; with quotation marks in their names.
2211 (while (string-match "\\(?:[^\\]\\|\\`\\)\\(\"\\)" file)
2212 (setq file (replace-match "\\\"" nil t file 1)))
2213 ;; Unescape any spaces escaped by ls -b (bug#10469).
2214 ;; Other -b quotes, eg \t, \n, work transparently.
2215 (if (dired-switches-escape-p dired-actual-switches)
2216 (let ((start 0)
2217 (rep "")
2218 (shift -1))
2219 (if (eq localp 'verbatim)
2220 (setq rep "\\\\"
2221 shift +1))
2222 (while (string-match "\\(\\\\\\) " file start)
2223 (setq file (replace-match rep nil t file 1)
2224 start (+ shift (match-end 0))))))
2225 (when (eq system-type 'windows-nt)
2226 (save-match-data
2227 (let ((start 0))
2228 (while (string-match "\\\\" file start)
2229 (aset file (match-beginning 0) ?/)
2230 (setq start (match-end 0))))))
2232 ;; Hence we don't need to worry about converting `\\' back to `\'.
2233 (setq file (read (concat "\"" file "\"")))
2234 ;; The above `read' will return a unibyte string if FILE
2235 ;; contains eight-bit-control/graphic characters.
2236 (if (and enable-multibyte-characters
2237 (not (multibyte-string-p file)))
2238 (setq file (string-to-multibyte file)))))
2239 (and file (file-name-absolute-p file)
2240 ;; A relative file name can start with ~.
2241 ;; Don't treat it as absolute in this context.
2242 (not (eq (aref file 0) ?~))
2243 (setq already-absolute t))
2244 (cond
2245 ((null file)
2246 nil)
2247 ((eq localp 'verbatim)
2248 file)
2249 ((and (not no-error-if-not-filep)
2250 (member file '("." "..")))
2251 (error "Cannot operate on `.' or `..'"))
2252 ((and (eq localp 'no-dir) already-absolute)
2253 (file-name-nondirectory file))
2254 (already-absolute
2255 (let ((handler (find-file-name-handler file nil)))
2256 ;; check for safe-magic property so that we won't
2257 ;; put /: for names that don't really need them.
2258 ;; For instance, .gz files when auto-compression-mode is on.
2259 (if (and handler (not (get handler 'safe-magic)))
2260 (concat "/:" file)
2261 file)))
2262 ((eq localp 'no-dir)
2263 file)
2264 ((equal (dired-current-directory) "/")
2265 (setq file (concat (dired-current-directory localp) file))
2266 (let ((handler (find-file-name-handler file nil)))
2267 ;; check for safe-magic property so that we won't
2268 ;; put /: for names that don't really need them.
2269 ;; For instance, .gz files when auto-compression-mode is on.
2270 (if (and handler (not (get handler 'safe-magic)))
2271 (concat "/:" file)
2272 file)))
2274 (concat (dired-current-directory localp) file)))))
2276 (defun dired-string-replace-match (regexp string newtext
2277 &optional literal global)
2278 "Replace first match of REGEXP in STRING with NEWTEXT.
2279 If it does not match, nil is returned instead of the new string.
2280 Optional arg LITERAL means to take NEWTEXT literally.
2281 Optional arg GLOBAL means to replace all matches."
2282 (if global
2283 (let ((start 0) ret)
2284 (while (string-match regexp string start)
2285 (let ((from-end (- (length string) (match-end 0))))
2286 (setq ret (setq string (replace-match newtext t literal string)))
2287 (setq start (- (length string) from-end))))
2288 ret)
2289 (if (not (string-match regexp string 0))
2291 (replace-match newtext t literal string))))
2293 (defun dired-make-absolute (file &optional dir)
2294 ;;"Convert FILE (a file name relative to DIR) to an absolute file name."
2295 ;; We can't always use expand-file-name as this would get rid of `.'
2296 ;; or expand in / instead default-directory if DIR=="".
2297 ;; This should be good enough for ange-ftp.
2298 ;; It should be reasonably fast, though, as it is called in
2299 ;; dired-get-filename.
2300 (concat (or dir default-directory) file))
2302 (defun dired-make-relative (file &optional dir)
2303 "Convert FILE (an absolute file name) to a name relative to DIR.
2304 If DIR is omitted or nil, it defaults to `default-directory'.
2305 If FILE is not in the directory tree of DIR, return FILE
2306 unchanged."
2307 (or dir (setq dir default-directory))
2308 ;; This case comes into play if default-directory is set to
2309 ;; use ~.
2310 (if (and (> (length dir) 0) (= (aref dir 0) ?~))
2311 (setq dir (expand-file-name dir)))
2312 (if (string-match (concat "^" (regexp-quote dir)) file)
2313 (substring file (match-end 0))
2314 file))
2316 (define-minor-mode dired-hide-details-mode
2317 "Toggle visibility of detailed information in current Dired buffer.
2318 When this minor mode is enabled, details such as file ownership and
2319 permissions are hidden from view.
2321 See options: `dired-hide-details-hide-symlink-targets' and
2322 `dired-hide-details-hide-information-lines'."
2323 :group 'dired
2324 (unless (derived-mode-p 'dired-mode)
2325 (error "Not a Dired buffer"))
2326 (dired-hide-details-update-invisibility-spec)
2327 (if dired-hide-details-mode
2328 (add-hook 'wdired-mode-hook
2329 'dired-hide-details-update-invisibility-spec
2332 (remove-hook 'wdired-mode-hook
2333 'dired-hide-details-update-invisibility-spec
2334 t)))
2336 (defun dired-hide-details-update-invisibility-spec ()
2337 (funcall (if dired-hide-details-mode
2338 'add-to-invisibility-spec
2339 'remove-from-invisibility-spec)
2340 'dired-hide-details-detail)
2341 (funcall (if (and dired-hide-details-mode
2342 dired-hide-details-hide-information-lines)
2343 'add-to-invisibility-spec
2344 'remove-from-invisibility-spec)
2345 'dired-hide-details-information)
2346 (funcall (if (and dired-hide-details-mode
2347 dired-hide-details-hide-symlink-targets
2348 (not (derived-mode-p 'wdired-mode)))
2349 'add-to-invisibility-spec
2350 'remove-from-invisibility-spec)
2351 'dired-hide-details-link))
2353 ;;; Functions for finding the file name in a dired buffer line.
2355 (defvar dired-permission-flags-regexp
2356 "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
2357 "Regular expression to match the permission flags in `ls -l'.")
2359 ;; Move to first char of filename on this line.
2360 ;; Returns position (point) or nil if no filename on this line."
2361 (defun dired-move-to-filename (&optional raise-error eol)
2362 "Move to the beginning of the filename on the current line.
2363 Return the position of the beginning of the filename, or nil if none found."
2364 ;; This is the UNIX version.
2365 (or eol (setq eol (line-end-position)))
2366 (beginning-of-line)
2367 ;; First try assuming `ls --dired' was used.
2368 (let ((change (next-single-property-change (point) 'dired-filename nil eol)))
2369 (cond
2370 ((and change (< change eol))
2371 (goto-char change))
2372 ((re-search-forward directory-listing-before-filename-regexp eol t)
2373 (goto-char (match-end 0)))
2374 ((re-search-forward dired-permission-flags-regexp eol t)
2375 ;; Ha! There *is* a file. Our regexp-from-hell just failed to find it.
2376 (if raise-error
2377 (error "Unrecognized line! Check directory-listing-before-filename-regexp"))
2378 (beginning-of-line)
2379 nil)
2380 (raise-error
2381 (error "No file on this line")))))
2383 (defun dired-move-to-end-of-filename (&optional no-error)
2384 ;; Assumes point is at beginning of filename,
2385 ;; thus the rwx bit re-search-backward below will succeed in *this*
2386 ;; line if at all. So, it should be called only after
2387 ;; (dired-move-to-filename t).
2388 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
2389 ;; This is the UNIX version.
2390 (if (get-text-property (point) 'dired-filename)
2391 (goto-char (next-single-property-change (point) 'dired-filename))
2392 (let (opoint file-type executable symlink hidden used-F eol)
2393 (setq used-F (dired-check-switches dired-actual-switches "F" "classify")
2394 opoint (point)
2395 eol (line-end-position)
2396 hidden (and selective-display
2397 (save-excursion (search-forward "\r" eol t))))
2398 (if hidden
2400 (save-excursion ;; Find out what kind of file this is:
2401 ;; Restrict perm bits to be non-blank,
2402 ;; otherwise this matches one char to early (looking backward):
2403 ;; "l---------" (some systems make symlinks that way)
2404 ;; "----------" (plain file with zero perms)
2405 (if (re-search-backward
2406 dired-permission-flags-regexp nil t)
2407 (setq file-type (char-after (match-beginning 1))
2408 symlink (eq file-type ?l)
2409 ;; Only with -F we need to know whether it's an executable
2410 executable (and
2411 used-F
2412 (string-match
2413 "[xst]" ;; execute bit set anywhere?
2414 (concat
2415 (match-string 2)
2416 (match-string 3)
2417 (match-string 4)))))
2418 (or no-error (error "No file on this line"))))
2419 ;; Move point to end of name:
2420 (if symlink
2421 (if (search-forward " -> " eol t)
2422 (progn
2423 (forward-char -4)
2424 (and used-F
2425 dired-ls-F-marks-symlinks
2426 (eq (preceding-char) ?@) ;; did ls really mark the link?
2427 (forward-char -1))))
2428 (goto-char eol) ;; else not a symbolic link
2429 ;; ls -lF marks dirs, sockets, fifos and executables with exactly
2430 ;; one trailing character. (Executable bits on symlinks ain't mean
2431 ;; a thing, even to ls, but we know it's not a symlink.)
2432 (and used-F
2433 (or (memq file-type '(?d ?s ?p))
2434 executable)
2435 (forward-char -1))))
2436 (or no-error
2437 (not (eq opoint (point)))
2438 (error "%s" (if hidden
2439 (substitute-command-keys
2440 "File line is hidden, type \\[dired-hide-subdir] to unhide")
2441 "No file on this line")))
2442 (if (eq opoint (point))
2444 (point)))))
2447 ;;; COPY NAMES OF MARKED FILES INTO KILL-RING.
2449 (defun dired-copy-filename-as-kill (&optional arg)
2450 "Copy names of marked (or next ARG) files into the kill ring.
2451 The names are separated by a space.
2452 With a zero prefix arg, use the absolute file name of each marked file.
2453 With \\[universal-argument], use the file name relative to the Dired buffer's
2454 `default-directory'. (This still may contain slashes if in a subdirectory.)
2456 If on a subdir headerline, use absolute subdirname instead;
2457 prefix arg and marked files are ignored in this case.
2459 You can then feed the file name(s) to other commands with \\[yank]."
2460 (interactive "P")
2461 (let ((string
2462 (or (dired-get-subdir)
2463 (mapconcat (function identity)
2464 (if arg
2465 (cond ((zerop (prefix-numeric-value arg))
2466 (dired-get-marked-files))
2467 ((consp arg)
2468 (dired-get-marked-files t))
2470 (dired-get-marked-files
2471 'no-dir (prefix-numeric-value arg))))
2472 (dired-get-marked-files 'no-dir))
2473 " "))))
2474 (unless (string= string "")
2475 (if (eq last-command 'kill-region)
2476 (kill-append string nil)
2477 (kill-new string))
2478 (message "%s" string))))
2481 ;; Keeping Dired buffers in sync with the filesystem and with each other
2483 (defun dired-buffers-for-dir (dir &optional file)
2484 ;; Return a list of buffers for DIR (top level or in-situ subdir).
2485 ;; If FILE is non-nil, include only those whose wildcard pattern (if any)
2486 ;; matches FILE.
2487 ;; The list is in reverse order of buffer creation, most recent last.
2488 ;; As a side effect, killed dired buffers for DIR are removed from
2489 ;; dired-buffers.
2490 (setq dir (file-name-as-directory dir))
2491 (let (result buf)
2492 (dolist (elt dired-buffers)
2493 (setq buf (cdr elt))
2494 (cond
2495 ((null (buffer-name buf))
2496 ;; Buffer is killed - clean up:
2497 (setq dired-buffers (delq elt dired-buffers)))
2498 ((dired-in-this-tree dir (car elt))
2499 (with-current-buffer buf
2500 (and (assoc dir dired-subdir-alist)
2501 (or (null file)
2502 (if (stringp dired-directory)
2503 (let ((wildcards (file-name-nondirectory
2504 dired-directory)))
2505 (or (zerop (length wildcards))
2506 (string-match-p (dired-glob-regexp wildcards)
2507 file)))
2508 (member (expand-file-name file dir)
2509 (cdr dired-directory))))
2510 (setq result (cons buf result)))))))
2511 result))
2513 (defun dired-glob-regexp (pattern)
2514 "Convert glob-pattern PATTERN to a regular expression."
2515 (let ((matched-in-pattern 0) ;; How many chars of PATTERN we've handled.
2516 regexp)
2517 (while (string-match "[[?*]" pattern matched-in-pattern)
2518 (let ((op-end (match-end 0))
2519 (next-op (aref pattern (match-beginning 0))))
2520 (setq regexp (concat regexp
2521 (regexp-quote
2522 (substring pattern matched-in-pattern
2523 (match-beginning 0)))))
2524 (cond ((= next-op ??)
2525 (setq regexp (concat regexp "."))
2526 (setq matched-in-pattern op-end))
2527 ((= next-op ?\[)
2528 ;; Fails to handle ^ yet ????
2529 (let* ((set-start (match-beginning 0))
2530 (set-cont
2531 (if (= (aref pattern (1+ set-start)) ?^)
2532 (+ 3 set-start)
2533 (+ 2 set-start)))
2534 (set-end (string-match-p "]" pattern set-cont))
2535 (set (substring pattern set-start (1+ set-end))))
2536 (setq regexp (concat regexp set))
2537 (setq matched-in-pattern (1+ set-end))))
2538 ((= next-op ?*)
2539 (setq regexp (concat regexp ".*"))
2540 (setq matched-in-pattern op-end)))))
2541 (concat "\\`"
2542 regexp
2543 (regexp-quote
2544 (substring pattern matched-in-pattern))
2545 "\\'")))
2549 (defun dired-advertise ()
2550 ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
2551 ;; With wildcards we actually advertise too much.
2552 (let ((expanded-default (expand-file-name default-directory)))
2553 (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
2554 t ; we have already advertised ourselves
2555 (setq dired-buffers
2556 (cons (cons expanded-default (current-buffer))
2557 dired-buffers)))))
2559 (defun dired-unadvertise (dir)
2560 ;; Remove DIR from the buffer alist in variable dired-buffers.
2561 ;; This has the effect of removing any buffer whose main directory is DIR.
2562 ;; It does not affect buffers in which DIR is a subdir.
2563 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
2564 (setq dired-buffers
2565 (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
2567 ;; Tree Dired
2569 ;;; utility functions
2571 (defun dired-in-this-tree (file dir)
2572 ;;"Is FILE part of the directory tree starting at DIR?"
2573 (let (case-fold-search)
2574 (string-match-p (concat "^" (regexp-quote dir)) file)))
2576 (defun dired-normalize-subdir (dir)
2577 ;; Prepend default-directory to DIR if relative file name.
2578 ;; dired-get-filename must be able to make a valid file name from a
2579 ;; file and its directory DIR.
2580 (file-name-as-directory
2581 (if (file-name-absolute-p dir)
2583 (expand-file-name dir default-directory))))
2585 (defun dired-get-subdir ()
2586 ;;"Return the subdir name on this line, or nil if not on a headerline."
2587 ;; Look up in the alist whether this is a headerline.
2588 (save-excursion
2589 (let ((cur-dir (dired-current-directory)))
2590 (beginning-of-line) ; alist stores b-o-l positions
2591 (and (zerop (- (point)
2592 (dired-get-subdir-min (assoc cur-dir
2593 dired-subdir-alist))))
2594 cur-dir))))
2596 ;; can't use macro, must be redefinable for other alist format in dired-nstd.
2597 (defalias 'dired-get-subdir-min 'cdr)
2599 (defun dired-get-subdir-max (elt)
2600 (save-excursion
2601 (goto-char (dired-get-subdir-min elt))
2602 (dired-subdir-max)))
2604 (defun dired-clear-alist ()
2605 (while dired-subdir-alist
2606 (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
2607 (setq dired-subdir-alist (cdr dired-subdir-alist))))
2609 (defun dired-subdir-index (dir)
2610 ;; Return an index into alist for use with nth
2611 ;; for the sake of subdir moving commands.
2612 (let (found (index 0) (alist dired-subdir-alist))
2613 (while alist
2614 (if (string= dir (car (car alist)))
2615 (setq alist nil found t)
2616 (setq alist (cdr alist) index (1+ index))))
2617 (if found index nil)))
2619 (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
2620 "Go to next subdirectory, regardless of level."
2621 ;; Use 0 arg to go to this directory's header line.
2622 ;; NO-SKIP prevents moving to end of header line, returning whatever
2623 ;; position was found in dired-subdir-alist.
2624 (interactive "p")
2625 (let ((this-dir (dired-current-directory))
2626 pos index)
2627 ;; nth with negative arg does not return nil but the first element
2628 (setq index (- (dired-subdir-index this-dir) arg))
2629 (setq pos (if (>= index 0)
2630 (dired-get-subdir-min (nth index dired-subdir-alist))))
2631 (if pos
2632 (progn
2633 (goto-char pos)
2634 (or no-skip (skip-chars-forward "^\n\r"))
2635 (point))
2636 (if no-error-if-not-found
2637 nil ; return nil if not found
2638 (error "%s directory" (if (> arg 0) "Last" "First"))))))
2640 (defun dired-build-subdir-alist (&optional switches)
2641 "Build `dired-subdir-alist' by parsing the buffer.
2642 Returns the new value of the alist.
2643 If optional arg SWITCHES is non-nil, use its value
2644 instead of `dired-actual-switches'."
2645 (interactive)
2646 (dired-clear-alist)
2647 (save-excursion
2648 (let* ((count 0)
2649 (inhibit-read-only t)
2650 (buffer-undo-list t)
2651 (switches (or switches dired-actual-switches))
2652 new-dir-name
2653 (R-ftp-base-dir-regex
2654 ;; Used to expand subdirectory names correctly in recursive
2655 ;; ange-ftp listings.
2656 (and (dired-switches-recursive-p switches)
2657 (string-match "\\`/.*:\\(/.*\\)" default-directory)
2658 (concat "\\`" (match-string 1 default-directory)))))
2659 (goto-char (point-min))
2660 (setq dired-subdir-alist nil)
2661 (while (re-search-forward dired-subdir-regexp nil t)
2662 ;; Avoid taking a file name ending in a colon
2663 ;; as a subdir name.
2664 (unless (save-excursion
2665 (goto-char (match-beginning 0))
2666 (beginning-of-line)
2667 (forward-char 2)
2668 (looking-at-p dired-re-perms))
2669 (save-excursion
2670 (goto-char (match-beginning 1))
2671 (setq new-dir-name
2672 (buffer-substring-no-properties (point) (match-end 1))
2673 new-dir-name
2674 (save-match-data
2675 (if (and R-ftp-base-dir-regex
2676 (not (string= new-dir-name default-directory))
2677 (string-match R-ftp-base-dir-regex new-dir-name))
2678 (concat default-directory
2679 (substring new-dir-name (match-end 0)))
2680 (expand-file-name new-dir-name))))
2681 (delete-region (point) (match-end 1))
2682 (insert new-dir-name))
2683 (setq count (1+ count))
2684 ;; Undo any escaping of newlines and \ by dired-insert-directory.
2685 ;; Convert "n" preceded by odd number of \ to newline, and \\ to \.
2686 (when (and (dired-switches-escape-p switches)
2687 (string-match-p "\\\\" new-dir-name))
2688 (let (temp res)
2689 (mapc (lambda (char)
2690 (cond ((equal char ?\\)
2691 (if temp
2692 (setq res (concat res "\\")
2693 temp nil)
2694 (setq temp "\\")))
2695 ((and temp (equal char ?n))
2696 (setq res (concat res "\n")
2697 temp nil))
2699 (setq res (concat res temp (char-to-string char))
2700 temp nil))))
2701 new-dir-name)
2702 (setq new-dir-name res)))
2703 (dired-alist-add-1 new-dir-name
2704 ;; Place a sub directory boundary between lines.
2705 (save-excursion
2706 (goto-char (match-beginning 0))
2707 (beginning-of-line)
2708 (point-marker)))))
2709 (if (and (> count 1) (called-interactively-p 'interactive))
2710 (message "Buffer includes %d directories" count)))
2711 ;; We don't need to sort it because it is in buffer order per
2712 ;; constructionem. Return new alist:
2713 dired-subdir-alist))
2715 (defun dired-alist-add-1 (dir new-marker)
2716 ;; Add new DIR at NEW-MARKER. Don't sort.
2717 (setq dired-subdir-alist
2718 (cons (cons (dired-normalize-subdir dir) new-marker)
2719 dired-subdir-alist)))
2721 (defun dired-goto-next-nontrivial-file ()
2722 ;; Position point on first nontrivial file after point.
2723 (dired-goto-next-file);; so there is a file to compare with
2724 (if (stringp dired-trivial-filenames)
2725 (while (and (not (eobp))
2726 (string-match-p dired-trivial-filenames
2727 (file-name-nondirectory
2728 (or (dired-get-filename nil t) ""))))
2729 (forward-line 1)
2730 (dired-move-to-filename))))
2732 (defun dired-goto-next-file ()
2733 (let ((max (1- (dired-subdir-max))))
2734 (while (and (not (dired-move-to-filename)) (< (point) max))
2735 (forward-line 1))))
2737 (defun dired-goto-file (file)
2738 "Go to line describing file FILE in this Dired buffer."
2739 ;; Return value of point on success, else nil.
2740 ;; FILE must be an absolute file name.
2741 ;; Loses if FILE contains control chars like "\007" for which ls
2742 ;; either inserts "?" or "\\007" into the buffer, so we won't find
2743 ;; it in the buffer.
2744 (interactive
2745 (prog1 ; let push-mark display its message
2746 (list (expand-file-name
2747 (read-file-name "Goto file: "
2748 (dired-current-directory))))
2749 (push-mark)))
2750 (unless (file-name-absolute-p file)
2751 (error "File name `%s' is not absolute" file))
2752 (setq file (directory-file-name file)) ; does no harm if not a directory
2753 (let* ((case-fold-search nil)
2754 (dir (file-name-directory file))
2755 (found (or
2756 ;; First, look for a listing under the absolute name.
2757 (save-excursion
2758 (goto-char (point-min))
2759 (dired-goto-file-1 file file (point-max)))
2760 ;; Next, look for it as a relative name with leading
2761 ;; subdirectories. (This happens in Dired buffers
2762 ;; created by find-dired, for example.)
2763 (save-excursion
2764 (goto-char (point-min))
2765 (dired-goto-file-1 (file-relative-name file
2766 default-directory)
2767 file (point-max)))
2768 ;; Otherwise, look for it as a relative name, a base
2769 ;; name only. The hair is to get the result of
2770 ;; `dired-goto-subdir' without calling it if we don't
2771 ;; have any subdirs.
2772 (save-excursion
2773 (when (if (string= dir (expand-file-name default-directory))
2774 (goto-char (point-min))
2775 (and (cdr dired-subdir-alist)
2776 (dired-goto-subdir dir)))
2777 (dired-goto-file-1 (file-name-nondirectory file)
2778 file
2779 (dired-subdir-max)))))))
2780 ;; Return buffer position, if found.
2781 (if found
2782 (goto-char found))))
2784 (defun dired-goto-file-1 (file full-name limit)
2785 "Advance to the Dired listing labeled by FILE; return its position.
2786 Return nil if the listing is not found. If FILE contains
2787 characters that would not appear in a Dired buffer, search using
2788 the quoted forms of those characters.
2790 FULL-NAME specifies the actual file name the listing must have,
2791 as returned by `dired-get-filename'. LIMIT is the search limit."
2792 (let (str)
2793 (setq str (replace-regexp-in-string "\^m" "\\^m" file nil t))
2794 (setq str (replace-regexp-in-string "\\\\" "\\\\" str nil t))
2795 (and (dired-switches-escape-p dired-actual-switches)
2796 (string-match-p "[ \t\n]" str)
2797 ;; FIXME: to fix this for embedded control characters etc, we
2798 ;; should escape everything that `ls -b' does.
2799 (setq str (replace-regexp-in-string " " "\\ " str nil t)
2800 str (replace-regexp-in-string "\t" "\\t" str nil t)
2801 str (replace-regexp-in-string "\n" "\\n" str nil t)))
2802 (let ((found nil)
2803 ;; filenames are preceded by SPC, this makes the search faster
2804 ;; (e.g. for the filename "-").
2805 (search-string (concat " " str)))
2806 (while (and (not found)
2807 (search-forward search-string limit 'move))
2808 ;; Check that we are in the right place. Match could have
2809 ;; BASE just as initial substring or in permission bits etc.
2810 (if (equal full-name (dired-get-filename nil t))
2811 (setq found (dired-move-to-filename))
2812 (forward-line 1)))
2813 found)))
2815 (defvar dired-find-subdir)
2817 ;; FIXME document whatever dired-x is doing.
2818 (defun dired-initial-position (dirname)
2819 "Where point should go in a new listing of DIRNAME.
2820 Point is assumed to be at the beginning of new subdir line.
2821 It runs the hook `dired-initial-position-hook'."
2822 (end-of-line)
2823 (and (featurep 'dired-x) dired-find-subdir
2824 (dired-goto-subdir dirname))
2825 (if dired-trivial-filenames (dired-goto-next-nontrivial-file))
2826 (run-hooks 'dired-initial-position-hook))
2828 ;; These are hooks which make tree dired work.
2829 ;; They are in this file because other parts of dired need to call them.
2830 ;; But they don't call the rest of tree dired unless there are subdirs loaded.
2832 ;; This function is called for each retrieved filename.
2833 ;; It could stand to be faster, though it's mostly function call
2834 ;; overhead. Avoiding the function call seems to save about 10% in
2835 ;; dired-get-filename. Make it a defsubst?
2836 (defun dired-current-directory (&optional localp)
2837 "Return the name of the subdirectory to which this line belongs.
2838 This returns a string with trailing slash, like `default-directory'.
2839 Optional argument means return a file name relative to `default-directory',
2840 in which case the value could be an empty string if `default-directory'
2841 is the directory where the file on this line resides."
2842 (let ((here (point))
2843 (alist (or dired-subdir-alist
2844 ;; probably because called in a non-dired buffer
2845 (error "No subdir-alist in %s" (current-buffer))))
2846 elt dir)
2847 (while alist
2848 (setq elt (car alist)
2849 dir (car elt)
2850 ;; use `<=' (not `<') as subdir line is part of subdir
2851 alist (if (<= (dired-get-subdir-min elt) here)
2852 nil ; found
2853 (cdr alist))))
2854 (if localp
2855 (dired-make-relative dir default-directory)
2856 dir)))
2858 ;; Subdirs start at the beginning of their header lines and end just
2859 ;; before the beginning of the next header line (or end of buffer).
2861 (defun dired-subdir-max ()
2862 (save-excursion
2863 (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
2864 (point-max)
2865 (point))))
2867 ;; Deleting files
2869 (defcustom dired-recursive-deletes 'top
2870 "Whether Dired deletes directories recursively.
2871 If nil, Dired will not delete non-empty directories.
2872 `always' means to delete non-empty directories recursively,
2873 without asking. This is dangerous!
2874 `top' means to ask for each top-level directory specified by the
2875 Dired deletion command, and delete its subdirectories without
2876 asking.
2877 Any other value means to ask for each directory."
2878 :type '(choice :tag "Delete non-empty directories"
2879 (const :tag "Yes" always)
2880 (const :tag "No--only delete empty directories" nil)
2881 (const :tag "Confirm for each directory" t)
2882 (const :tag "Confirm for each top directory only" top))
2883 :group 'dired)
2885 ;; Match anything but `.' and `..'.
2886 (defvar dired-re-no-dot "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")
2888 ;; Delete file, possibly delete a directory and all its files.
2889 ;; This function is useful outside of dired. One could change its name
2890 ;; to e.g. recursive-delete-file and put it somewhere else.
2891 (defun dired-delete-file (file &optional recursive trash) "\
2892 Delete FILE or directory (possibly recursively if optional RECURSIVE is true.)
2893 RECURSIVE determines what to do with a non-empty directory. The effect of
2894 its possible values is:
2896 nil -- do not delete.
2897 `always' -- delete recursively without asking.
2898 `top' -- ask for each directory at top level.
2899 Anything else -- ask for each sub-directory.
2901 TRASH non-nil means to trash the file instead of deleting, provided
2902 `delete-by-moving-to-trash' (which see) is non-nil."
2903 ;; This test is equivalent to
2904 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
2905 ;; but more efficient
2906 (if (not (eq t (car (file-attributes file))))
2907 (delete-file file trash)
2908 (if (and recursive
2909 (directory-files file t dired-re-no-dot) ; Not empty.
2910 (or (eq recursive 'always)
2911 (yes-or-no-p (format "Recursively %s %s? "
2912 (if (and trash
2913 delete-by-moving-to-trash)
2914 "trash"
2915 "delete")
2916 (dired-make-relative file)))))
2917 (if (eq recursive 'top) (setq recursive 'always)) ; Don't ask again.
2918 (setq recursive nil))
2919 (delete-directory file recursive trash)))
2921 (defun dired-do-flagged-delete (&optional nomessage)
2922 "In Dired, delete the files flagged for deletion.
2923 If NOMESSAGE is non-nil, we don't display any message
2924 if there are no flagged files.
2925 `dired-recursive-deletes' controls whether deletion of
2926 non-empty directories is allowed."
2927 (interactive)
2928 (let* ((dired-marker-char dired-del-marker)
2929 (regexp (dired-marker-regexp))
2930 case-fold-search)
2931 (if (save-excursion (goto-char (point-min))
2932 (re-search-forward regexp nil t))
2933 (dired-internal-do-deletions
2934 ;; this can't move point since ARG is nil
2935 (dired-map-over-marks (cons (dired-get-filename) (point))
2936 nil)
2937 nil t)
2938 (or nomessage
2939 (message "(No deletions requested)")))))
2941 (defun dired-do-delete (&optional arg)
2942 "Delete all marked (or next ARG) files.
2943 `dired-recursive-deletes' controls whether deletion of
2944 non-empty directories is allowed."
2945 ;; This is more consistent with the file marking feature than
2946 ;; dired-do-flagged-delete.
2947 (interactive "P")
2948 (dired-internal-do-deletions
2949 ;; this may move point if ARG is an integer
2950 (dired-map-over-marks (cons (dired-get-filename) (point))
2951 arg)
2952 arg t))
2954 (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
2956 (defun dired-internal-do-deletions (l arg &optional trash)
2957 ;; L is an alist of files to delete, with their buffer positions.
2958 ;; ARG is the prefix arg.
2959 ;; Filenames are absolute.
2960 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
2961 ;; That way as changes are made in the buffer they do not shift the
2962 ;; lines still to be changed, so the (point) values in L stay valid.
2963 ;; Also, for subdirs in natural order, a subdir's files are deleted
2964 ;; before the subdir itself - the other way around would not work.
2965 (let* ((files (mapcar (function car) l))
2966 (count (length l))
2967 (succ 0)
2968 (trashing (and trash delete-by-moving-to-trash)))
2969 ;; canonicalize file list for pop up
2970 (setq files (nreverse (mapcar (function dired-make-relative) files)))
2971 (if (dired-mark-pop-up
2972 " *Deletions*" 'delete files dired-deletion-confirmer
2973 (format "%s %s "
2974 (if trashing "Trash" "Delete")
2975 (dired-mark-prompt arg files)))
2976 (save-excursion
2977 (let ((progress-reporter
2978 (make-progress-reporter
2979 (if trashing "Trashing..." "Deleting...")
2980 succ count))
2981 failures) ;; files better be in reverse order for this loop!
2982 (while l
2983 (goto-char (cdr (car l)))
2984 (let ((inhibit-read-only t))
2985 (condition-case err
2986 (let ((fn (car (car l))))
2987 (dired-delete-file fn dired-recursive-deletes trash)
2988 ;; if we get here, removing worked
2989 (setq succ (1+ succ))
2990 (progress-reporter-update progress-reporter succ)
2991 (dired-fun-in-all-buffers
2992 (file-name-directory fn) (file-name-nondirectory fn)
2993 (function dired-delete-entry) fn))
2994 (error ;; catch errors from failed deletions
2995 (dired-log "%s\n" err)
2996 (setq failures (cons (car (car l)) failures)))))
2997 (setq l (cdr l)))
2998 (if (not failures)
2999 (progress-reporter-done progress-reporter)
3000 (dired-log-summary
3001 (format "%d of %d deletion%s failed"
3002 (length failures) count
3003 (dired-plural-s count))
3004 failures))))
3005 (message "(No deletions performed)")))
3006 (dired-move-to-filename))
3008 (defun dired-fun-in-all-buffers (directory file fun &rest args)
3009 ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS.
3010 ;; If the buffer has a wildcard pattern, check that it matches FILE.
3011 ;; (FILE does not include a directory component.)
3012 ;; FILE may be nil, in which case ignore it.
3013 ;; Return list of buffers where FUN succeeded (i.e., returned non-nil).
3014 (let (success-list)
3015 (dolist (buf (dired-buffers-for-dir (expand-file-name directory)
3016 file))
3017 (with-current-buffer buf
3018 (if (apply fun args)
3019 (setq success-list (cons (buffer-name buf) success-list)))))
3020 success-list))
3022 ;; Delete the entry for FILE from
3023 (defun dired-delete-entry (file)
3024 (save-excursion
3025 (and (dired-goto-file file)
3026 (let ((inhibit-read-only t))
3027 (delete-region (progn (beginning-of-line) (point))
3028 (save-excursion (forward-line 1) (point))))))
3029 (dired-clean-up-after-deletion file))
3031 (defvar dired-clean-up-buffers-too)
3033 (defun dired-clean-up-after-deletion (fn)
3034 "Clean up after a deleted file or directory FN.
3035 Removes any expanded subdirectory of deleted directory.
3036 If `dired-x' is loaded and `dired-clean-up-buffers-too' is non-nil,
3037 also offers to kill buffers visiting deleted files and directories."
3038 (save-excursion (and (cdr dired-subdir-alist)
3039 (dired-goto-subdir fn)
3040 (dired-kill-subdir)))
3041 ;; Offer to kill buffer of deleted file FN.
3042 (when (and (featurep 'dired-x) dired-clean-up-buffers-too)
3043 (let ((buf (get-file-buffer fn)))
3044 (and buf
3045 (funcall #'y-or-n-p
3046 (format "Kill buffer of %s, too? "
3047 (file-name-nondirectory fn)))
3048 (kill-buffer buf)))
3049 (let ((buf-list (dired-buffers-for-dir (expand-file-name fn))))
3050 (and buf-list
3051 (y-or-n-p (format "Kill Dired buffer%s of %s, too? "
3052 (dired-plural-s (length buf-list))
3053 (file-name-nondirectory fn)))
3054 (dolist (buf buf-list)
3055 (kill-buffer buf))))))
3058 ;; Confirmation
3060 (defun dired-marker-regexp ()
3061 (concat "^" (regexp-quote (char-to-string dired-marker-char))))
3063 (defun dired-plural-s (count)
3064 (if (= 1 count) "" "s"))
3066 (defun dired-mark-prompt (arg files)
3067 "Return a string suitable for use in a Dired prompt.
3068 ARG is normally the prefix argument for the calling command.
3069 FILES should be a list of file names.
3071 The return value has a form like \"foo.txt\", \"[next 3 files]\",
3072 or \"* [3 files]\"."
3073 ;; distinguish-one-marked can cause the first element to be just t.
3074 (if (eq (car files) t) (setq files (cdr files)))
3075 (let ((count (length files)))
3076 (if (= count 1)
3077 (car files)
3078 ;; more than 1 file:
3079 (if (integerp arg)
3080 ;; abs(arg) = count
3081 ;; Perhaps this is nicer, but it also takes more screen space:
3082 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
3083 ;; count)
3084 (format "[next %d files]" arg)
3085 (format "%c [%d files]" dired-marker-char count)))))
3087 (defun dired-pop-to-buffer (buf)
3088 "Pop up buffer BUF in a way suitable for Dired."
3089 (declare (obsolete dired-mark-pop-up "24.3"))
3090 (let ((split-window-preferred-function
3091 (lambda (window)
3092 (or (and (let ((split-height-threshold 0))
3093 (window-splittable-p (selected-window)))
3094 ;; Try to split the selected window vertically if
3095 ;; that's possible. (Bug#1806)
3096 (split-window-below))
3097 ;; Otherwise, try to split WINDOW sensibly.
3098 (split-window-sensibly window))))
3099 pop-up-frames)
3100 (pop-to-buffer (get-buffer-create buf)))
3101 ;; See Bug#12281.
3102 (set-window-start nil (point-min))
3103 ;; If dired-shrink-to-fit is t, make its window fit its contents.
3104 (when dired-shrink-to-fit
3105 ;; Try to not delete window when we want to display less than
3106 ;; `window-min-height' lines.
3107 (fit-window-to-buffer (get-buffer-window buf) nil 1 nil nil t)))
3109 (defcustom dired-no-confirm nil
3110 "A list of symbols for commands Dired should not confirm, or t.
3111 Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress',
3112 `copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink',
3113 `touch' and `uncompress'.
3114 If t, confirmation is never needed."
3115 :group 'dired
3116 :type '(choice (const :tag "Confirmation never needed" t)
3117 (set (const byte-compile) (const chgrp)
3118 (const chmod) (const chown) (const compress)
3119 (const copy) (const delete) (const hardlink)
3120 (const load) (const move) (const print)
3121 (const shell) (const symlink) (const touch)
3122 (const uncompress))))
3124 (defun dired-mark-pop-up (buffer-or-name op-symbol files function &rest args)
3125 "Return FUNCTION's result on ARGS after showing which files are marked.
3126 Displays the file names in a window showing a buffer named
3127 BUFFER-OR-NAME; the default name being \" *Marked Files*\". The
3128 window is not shown if there is just one file, `dired-no-confirm'
3129 is t, or OP-SYMBOL is a member of the list in `dired-no-confirm'.
3131 By default, Dired shrinks the display buffer to fit the marked files.
3132 To disable this, use the Customization interface to add a new rule
3133 to `display-buffer-alist' where condition regexp is \"^ \\*Marked Files\\*$\",
3134 action argument symbol is `window-height' and its value is nil.
3136 FILES is the list of marked files. It can also be (t FILENAME)
3137 in the case of one marked file, to distinguish that from using
3138 just the current file.
3140 FUNCTION should not manipulate files, just read input (an
3141 argument or confirmation)."
3142 (if (or (eq dired-no-confirm t)
3143 (memq op-symbol dired-no-confirm)
3144 ;; If FILES defaulted to the current line's file.
3145 (= (length files) 1))
3146 (apply function args)
3147 (let ((buffer (get-buffer-create (or buffer-or-name " *Marked Files*")))
3148 ;; Mark *Marked Files* window as softly-dedicated, to prevent
3149 ;; other buffers e.g. *Completions* from reusing it (bug#17554).
3150 (display-buffer-mark-dedicated 'soft))
3151 (with-displayed-buffer-window
3152 buffer
3153 (cons 'display-buffer-below-selected
3154 '((window-height . fit-window-to-buffer)
3155 (preserve-size . (nil . t))))
3156 #'(lambda (window _value)
3157 (with-selected-window window
3158 (unwind-protect
3159 (apply function args)
3160 (when (window-live-p window)
3161 (quit-restore-window window 'kill)))))
3162 ;; Handle (t FILE) just like (FILE), here. That value is
3163 ;; used (only in some cases), to mean just one file that was
3164 ;; marked, rather than the current line file.
3165 (with-current-buffer buffer
3166 (dired-format-columns-of-files
3167 (if (eq (car files) t) (cdr files) files))
3168 (remove-text-properties (point-min) (point-max)
3169 '(mouse-face nil help-echo nil)))))))
3171 (defun dired-format-columns-of-files (files)
3172 (let ((beg (point)))
3173 (completion--insert-strings files)
3174 (put-text-property beg (point) 'mouse-face nil)))
3176 ;; Commands to mark or flag file(s) at or near current line.
3178 (defun dired-repeat-over-lines (arg function)
3179 ;; This version skips non-file lines.
3180 (let ((pos (make-marker)))
3181 (beginning-of-line)
3182 (while (and (> arg 0) (not (eobp)))
3183 (setq arg (1- arg))
3184 (beginning-of-line)
3185 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
3186 (save-excursion
3187 (forward-line 1)
3188 (move-marker pos (1+ (point))))
3189 (save-excursion (funcall function))
3190 ;; Advance to the next line--actually, to the line that *was* next.
3191 ;; (If FUNCTION inserted some new lines in between, skip them.)
3192 (goto-char pos))
3193 (while (and (< arg 0) (not (bobp)))
3194 (setq arg (1+ arg))
3195 (forward-line -1)
3196 (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
3197 (beginning-of-line)
3198 (save-excursion (funcall function)))
3199 (move-marker pos nil)
3200 (dired-move-to-filename)))
3202 (defun dired-between-files ()
3203 ;; This used to be a regexp match of the `total ...' line output by
3204 ;; ls, which is slightly faster, but that is not very robust; notably,
3205 ;; it fails for non-english locales.
3206 (save-excursion (not (dired-move-to-filename))))
3208 (defun dired-next-marked-file (arg &optional wrap opoint)
3209 "Move to the next marked file.
3210 If WRAP is non-nil, wrap around to the beginning of the buffer if
3211 we reach the end."
3212 (interactive "p\np")
3213 (or opoint (setq opoint (point)));; return to where interactively started
3214 (if (if (> arg 0)
3215 (re-search-forward dired-re-mark nil t arg)
3216 (beginning-of-line)
3217 (re-search-backward dired-re-mark nil t (- arg)))
3218 (dired-move-to-filename)
3219 (if (null wrap)
3220 (progn
3221 (goto-char opoint)
3222 (error "No next marked file"))
3223 (message "(Wraparound for next marked file)")
3224 (goto-char (if (> arg 0) (point-min) (point-max)))
3225 (dired-next-marked-file arg nil opoint))))
3227 (defun dired-prev-marked-file (arg &optional wrap)
3228 "Move to the previous marked file.
3229 If WRAP is non-nil, wrap around to the end of the buffer if we
3230 reach the beginning of the buffer."
3231 (interactive "p\np")
3232 (dired-next-marked-file (- arg) wrap))
3234 (defun dired-file-marker (file)
3235 ;; Return FILE's marker, or nil if unmarked.
3236 (save-excursion
3237 (and (dired-goto-file file)
3238 (progn
3239 (beginning-of-line)
3240 (if (not (equal ?\040 (following-char)))
3241 (following-char))))))
3243 (defun dired-mark-files-in-region (start end)
3244 (let ((inhibit-read-only t))
3245 (if (> start end)
3246 (error "start > end"))
3247 (goto-char start) ; assumed at beginning of line
3248 (while (< (point) end)
3249 ;; Skip subdir line and following garbage like the `total' line:
3250 (while (and (< (point) end) (dired-between-files))
3251 (forward-line 1))
3252 (if (and (not (looking-at-p dired-re-dot))
3253 (dired-get-filename nil t))
3254 (progn
3255 (delete-char 1)
3256 (insert dired-marker-char)))
3257 (forward-line 1))))
3259 (defun dired-mark (arg &optional interactive)
3260 "Mark the file at point in the Dired buffer.
3261 If the region is active, mark all files in the region.
3262 Otherwise, with a prefix arg, mark files on the next ARG lines.
3264 If on a subdir headerline, mark all its files except `.' and `..'.
3266 Use \\[dired-unmark-all-files] to remove all marks
3267 and \\[dired-unmark] on a subdir to remove the marks in
3268 this subdir."
3269 (interactive (list current-prefix-arg t))
3270 (cond
3271 ;; Mark files in the active region.
3272 ((and interactive (use-region-p))
3273 (save-excursion
3274 (let ((beg (region-beginning))
3275 (end (region-end)))
3276 (dired-mark-files-in-region
3277 (progn (goto-char beg) (line-beginning-position))
3278 (progn (goto-char end) (line-beginning-position))))))
3279 ;; Mark subdir files from the subdir headerline.
3280 ((dired-get-subdir)
3281 (save-excursion (dired-mark-subdir-files)))
3282 ;; Mark the current (or next ARG) files.
3284 (let ((inhibit-read-only t))
3285 (dired-repeat-over-lines
3286 (prefix-numeric-value arg)
3287 (function (lambda () (delete-char 1) (insert dired-marker-char))))))))
3289 (defun dired-unmark (arg &optional interactive)
3290 "Unmark the file at point in the Dired buffer.
3291 If the region is active, unmark all files in the region.
3292 Otherwise, with a prefix arg, unmark files on the next ARG lines.
3294 If looking at a subdir, unmark all its files except `.' and `..'.
3295 If the region is active in Transient Mark mode, unmark all files
3296 in the active region."
3297 (interactive (list current-prefix-arg t))
3298 (let ((dired-marker-char ?\040))
3299 (dired-mark arg interactive)))
3301 (defun dired-flag-file-deletion (arg &optional interactive)
3302 "In Dired, flag the current line's file for deletion.
3303 If the region is active, flag all files in the region.
3304 Otherwise, with a prefix arg, flag files on the next ARG lines.
3306 If on a subdir headerline, flag all its files except `.' and `..'.
3307 If the region is active in Transient Mark mode, flag all files
3308 in the active region."
3309 (interactive (list current-prefix-arg t))
3310 (let ((dired-marker-char dired-del-marker))
3311 (dired-mark arg interactive)))
3313 (defun dired-unmark-backward (arg)
3314 "In Dired, move up lines and remove marks or deletion flags there.
3315 Optional prefix ARG says how many lines to unmark/unflag; default
3316 is one line.
3317 If the region is active in Transient Mark mode, unmark all files
3318 in the active region."
3319 (interactive "p")
3320 (dired-unmark (- arg) t))
3322 (defun dired-toggle-marks ()
3323 "Toggle marks: marked files become unmarked, and vice versa.
3324 Files marked with other flags (such as `D') are not affected.
3325 `.' and `..' are never toggled.
3326 As always, hidden subdirs are not affected."
3327 (interactive)
3328 (save-excursion
3329 (goto-char (point-min))
3330 (let ((inhibit-read-only t))
3331 (while (not (eobp))
3332 (or (dired-between-files)
3333 (looking-at-p dired-re-dot)
3334 ;; use subst instead of insdel because it does not move
3335 ;; the gap and thus should be faster and because
3336 ;; other characters are left alone automatically
3337 (apply 'subst-char-in-region
3338 (point) (1+ (point))
3339 (if (eq ?\040 (following-char)) ; SPC
3340 (list ?\040 dired-marker-char)
3341 (list dired-marker-char ?\040))))
3342 (forward-line 1)))))
3344 ;;; Commands to mark or flag files based on their characteristics or names.
3346 (defvar dired-regexp-history nil
3347 "History list of regular expressions used in Dired commands.")
3349 (defun dired-read-regexp (prompt &optional default history)
3350 "Read a regexp using `read-regexp'."
3351 (declare (obsolete read-regexp "24.5"))
3352 (read-regexp prompt default (or history 'dired-regexp-history)))
3354 (defun dired-mark-files-regexp (regexp &optional marker-char)
3355 "Mark all files matching REGEXP for use in later commands.
3356 A prefix argument means to unmark them instead.
3357 `.' and `..' are never marked.
3359 REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
3360 object files--just `.o' will mark more than you might think."
3361 (interactive
3362 (list (read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
3363 " files (regexp): ")
3364 nil 'dired-regexp-history)
3365 (if current-prefix-arg ?\040)))
3366 (let ((dired-marker-char (or marker-char dired-marker-char)))
3367 (dired-mark-if
3368 (and (not (looking-at-p dired-re-dot))
3369 (not (eolp)) ; empty line
3370 (let ((fn (dired-get-filename t t)))
3371 (and fn (string-match-p regexp fn))))
3372 "matching file")))
3374 (defun dired-mark-files-containing-regexp (regexp &optional marker-char)
3375 "Mark all files with contents containing REGEXP for use in later commands.
3376 A prefix argument means to unmark them instead.
3377 `.' and `..' are never marked.
3379 Note that if a file is visited in an Emacs buffer, and
3380 `dired-always-read-filesystem' is nil, this command will
3381 look in the buffer without revisiting the file, so the results might
3382 be inconsistent with the file on disk if its contents has changed
3383 since it was last visited."
3384 (interactive
3385 (list (read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
3386 " files containing (regexp): ")
3387 nil 'dired-regexp-history)
3388 (if current-prefix-arg ?\040)))
3389 (let ((dired-marker-char (or marker-char dired-marker-char)))
3390 (dired-mark-if
3391 (and (not (looking-at-p dired-re-dot))
3392 (not (eolp)) ; empty line
3393 (let ((fn (dired-get-filename nil t)))
3394 (when (and fn (file-readable-p fn)
3395 (not (file-directory-p fn)))
3396 (let ((prebuf (get-file-buffer fn)))
3397 (message "Checking %s" fn)
3398 ;; For now we do it inside emacs
3399 ;; Grep might be better if there are a lot of files
3400 (if (and prebuf (not dired-always-read-filesystem))
3401 (with-current-buffer prebuf
3402 (save-excursion
3403 (goto-char (point-min))
3404 (re-search-forward regexp nil t)))
3405 (with-temp-buffer
3406 (insert-file-contents fn)
3407 (goto-char (point-min))
3408 (re-search-forward regexp nil t))))
3410 "matching file")))
3412 (defun dired-flag-files-regexp (regexp)
3413 "In Dired, flag all files containing the specified REGEXP for deletion.
3414 The match is against the non-directory part of the filename. Use `^'
3415 and `$' to anchor matches. Exclude subdirs by hiding them.
3416 `.' and `..' are never flagged."
3417 (interactive (list (read-regexp "Flag for deletion (regexp): "
3418 nil 'dired-regexp-history)))
3419 (dired-mark-files-regexp regexp dired-del-marker))
3421 (defun dired-mark-symlinks (unflag-p)
3422 "Mark all symbolic links.
3423 With prefix argument, unmark or unflag all those files."
3424 (interactive "P")
3425 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
3426 (dired-mark-if (looking-at-p dired-re-sym) "symbolic link")))
3428 (defun dired-mark-directories (unflag-p)
3429 "Mark all directory file lines except `.' and `..'.
3430 With prefix argument, unmark or unflag all those files."
3431 (interactive "P")
3432 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
3433 (dired-mark-if (and (looking-at-p dired-re-dir)
3434 (not (looking-at-p dired-re-dot)))
3435 "directory file")))
3437 (defun dired-mark-executables (unflag-p)
3438 "Mark all executable files.
3439 With prefix argument, unmark or unflag all those files."
3440 (interactive "P")
3441 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
3442 (dired-mark-if (looking-at-p dired-re-exe) "executable file")))
3444 ;; dired-x.el has a dired-mark-sexp interactive command: mark
3445 ;; files for which PREDICATE returns non-nil.
3447 (defun dired-flag-auto-save-files (&optional unflag-p)
3448 "Flag for deletion files whose names suggest they are auto save files.
3449 A prefix argument says to unmark or unflag those files instead."
3450 (interactive "P")
3451 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
3452 (dired-mark-if
3453 ;; It is less than general to check for # here,
3454 ;; but it's the only way this runs fast enough.
3455 (and (save-excursion (end-of-line)
3457 (eq (preceding-char) ?#)
3458 ;; Handle executables in case of -F option.
3459 ;; We need not worry about the other kinds
3460 ;; of markings that -F makes, since they won't
3461 ;; appear on real auto-save files.
3462 (if (eq (preceding-char) ?*)
3463 (progn
3464 (forward-char -1)
3465 (eq (preceding-char) ?#)))))
3466 (not (looking-at-p dired-re-dir))
3467 (let ((fn (dired-get-filename t t)))
3468 (if fn (auto-save-file-name-p
3469 (file-name-nondirectory fn)))))
3470 "auto save file")))
3472 (defcustom dired-garbage-files-regexp
3473 ;; `log' here is dubious, since it's typically used for useful log
3474 ;; files, not just TeX stuff. -- fx
3475 (concat (regexp-opt
3476 '(".log" ".toc" ".dvi" ".bak" ".orig" ".rej" ".aux"))
3477 "\\'")
3478 "Regular expression to match \"garbage\" files for `dired-flag-garbage-files'."
3479 :type 'regexp
3480 :group 'dired)
3482 (defun dired-flag-garbage-files ()
3483 "Flag for deletion all files that match `dired-garbage-files-regexp'."
3484 (interactive)
3485 (dired-flag-files-regexp dired-garbage-files-regexp))
3487 (defun dired-flag-backup-files (&optional unflag-p)
3488 "Flag all backup files (names ending with `~') for deletion.
3489 With prefix argument, unmark or unflag these files."
3490 (interactive "P")
3491 (let ((dired-marker-char (if unflag-p ?\s dired-del-marker)))
3492 (dired-mark-if
3493 ;; Don't call backup-file-name-p unless the last character looks like
3494 ;; it might be the end of a backup file name. This isn't very general,
3495 ;; but it's the only way this runs fast enough.
3496 (and (save-excursion (end-of-line)
3497 ;; Handle executables in case of -F option.
3498 ;; We need not worry about the other kinds
3499 ;; of markings that -F makes, since they won't
3500 ;; appear on real backup files.
3501 (if (eq (preceding-char) ?*)
3502 (forward-char -1))
3503 (eq (preceding-char) ?~))
3504 (not (looking-at-p dired-re-dir))
3505 (let ((fn (dired-get-filename t t)))
3506 (if fn (backup-file-name-p fn))))
3507 "backup file")))
3509 (defun dired-change-marks (&optional old new)
3510 "Change all OLD marks to NEW marks.
3511 OLD and NEW are both characters used to mark files."
3512 (interactive
3513 (let* ((cursor-in-echo-area t)
3514 (old (progn (message "Change (old mark): ") (read-char)))
3515 (new (progn (message "Change %c marks to (new mark): " old)
3516 (read-char))))
3517 (list old new)))
3518 (if (or (eq old ?\r) (eq new ?\r))
3519 (ding)
3520 (let ((string (format "\n%c" old))
3521 (inhibit-read-only t))
3522 (save-excursion
3523 (goto-char (point-min))
3524 (while (search-forward string nil t)
3525 (if (if (= old ?\s)
3526 (save-match-data
3527 (dired-get-filename 'no-dir t))
3529 (subst-char-in-region (match-beginning 0)
3530 (match-end 0) old new)))))))
3532 (defun dired-unmark-all-marks ()
3533 "Remove all marks from all files in the Dired buffer."
3534 (interactive)
3535 (dired-unmark-all-files ?\r))
3537 ;; Bound in dired-unmark-all-files
3538 (defvar dired-unmark-all-files-query)
3540 (defun dired-unmark-all-files (mark &optional arg)
3541 "Remove a specific mark (or any mark) from every file.
3542 After this command, type the mark character to remove,
3543 or type RET to remove all marks.
3544 With prefix arg, query for each marked file.
3545 Type \\[help-command] at that time for help."
3546 (interactive "cRemove marks (RET means all): \nP")
3547 (save-excursion
3548 (let* ((count 0)
3549 (inhibit-read-only t) case-fold-search
3550 dired-unmark-all-files-query
3551 (string (format "\n%c" mark))
3552 (help-form "\
3553 Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
3554 `!' to unmark all remaining files with no more questions."))
3555 (goto-char (point-min))
3556 (while (if (eq mark ?\r)
3557 (re-search-forward dired-re-mark nil t)
3558 (search-forward string nil t))
3559 (if (or (not arg)
3560 (let ((file (dired-get-filename t t)))
3561 (and file
3562 (dired-query 'dired-unmark-all-files-query
3563 "Unmark file `%s'? "
3564 file))))
3565 (progn (subst-char-in-region (1- (point)) (point)
3566 (preceding-char) ?\s)
3567 (setq count (1+ count)))))
3568 (message (if (= count 1) "1 mark removed"
3569 "%d marks removed")
3570 count))))
3572 ;; Logging failures operating on files, and showing the results.
3574 (defvar dired-log-buffer "*Dired log*")
3576 (defun dired-why ()
3577 "Pop up a buffer with error log output from Dired.
3578 A group of errors from a single command ends with a formfeed.
3579 Thus, use \\[backward-page] to find the beginning of a group of errors."
3580 (interactive)
3581 (if (get-buffer dired-log-buffer)
3582 (let ((owindow (selected-window))
3583 (window (display-buffer (get-buffer dired-log-buffer))))
3584 (unwind-protect
3585 (progn
3586 (select-window window)
3587 (goto-char (point-max))
3588 (forward-line -1)
3589 (backward-page 1)
3590 (recenter 0))
3591 (select-window owindow)))))
3593 (defun dired-log (log &rest args)
3594 ;; Log a message or the contents of a buffer.
3595 ;; If LOG is a string and there are more args, it is formatted with
3596 ;; those ARGS. Usually the LOG string ends with a \n.
3597 ;; End each bunch of errors with (dired-log t):
3598 ;; this inserts the current time and buffer at the start of the page,
3599 ;; and \f (formfeed) at the end.
3600 (let ((obuf (current-buffer)))
3601 (with-current-buffer (get-buffer-create dired-log-buffer)
3602 (goto-char (point-max))
3603 (let ((inhibit-read-only t))
3604 (cond ((stringp log)
3605 (insert (if args
3606 (apply #'format-message log args)
3607 log)))
3608 ((bufferp log)
3609 (insert-buffer-substring log))
3610 ((eq t log)
3611 (backward-page 1)
3612 (unless (bolp)
3613 (insert "\n"))
3614 (insert (current-time-string)
3615 (format-message "\tBuffer `%s'\n" (buffer-name obuf)))
3616 (goto-char (point-max))
3617 (insert "\f\n")))))))
3619 (defun dired-log-summary (string failures)
3620 "State a summary of a command's failures, in echo area and log buffer.
3621 STRING is an overall summary of the failures.
3622 FAILURES is a list of file names that we failed to operate on,
3623 or nil if file names are not applicable."
3624 (if (= (length failures) 1)
3625 (message "%s"
3626 (with-current-buffer dired-log-buffer
3627 (goto-char (point-max))
3628 (backward-page 1)
3629 (if (eolp) (forward-line 1))
3630 (buffer-substring (point) (point-max))))
3631 (message (if failures "%s--type ? for details (%s)"
3632 "%s--type ? for details")
3633 string failures))
3634 ;; Log a summary describing a bunch of errors.
3635 (dired-log (concat "\n" string "\n"))
3636 (dired-log t))
3638 ;;; Sorting
3640 ;; Most ls can only sort by name or by date (with -t), nothing else.
3641 ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
3642 ;; So anything that does not contain these is sort "by name".
3644 (defvar dired-ls-sorting-switches "SXU"
3645 "String of `ls' switches (single letters) except \"t\" that influence sorting.
3647 This indicates to Dired which option switches to watch out for because they
3648 will change the sorting order behavior of `ls'.
3650 To change the default sorting order (e.g. add a `-v' option), see the
3651 variable `dired-listing-switches'. To temporarily override the listing
3652 format, use `\\[universal-argument] \\[dired]'.")
3654 (defvar dired-sort-by-date-regexp
3655 (concat "\\(\\`\\| \\)-[^- ]*t"
3656 ;; `dired-ls-sorting-switches' after -t overrides -t.
3657 "[^ " dired-ls-sorting-switches "]*"
3658 "\\(\\(\\`\\| +\\)\\(--[^ ]+\\|-[^- t"
3659 dired-ls-sorting-switches "]+\\)\\)* *$")
3660 "Regexp recognized by Dired to set `by date' mode.")
3662 (defvar dired-sort-by-name-regexp
3663 (concat "\\`\\(\\(\\`\\| +\\)\\(--[^ ]+\\|"
3664 "-[^- t" dired-ls-sorting-switches "]+\\)\\)* *$")
3665 "Regexp recognized by Dired to set `by name' mode.")
3667 (defvar dired-sort-inhibit nil
3668 "Non-nil means the Dired sort command is disabled.
3669 The idea is to set this buffer-locally in special Dired buffers.")
3671 (defun dired-sort-set-mode-line ()
3672 ;; Set mode line display according to dired-actual-switches.
3673 ;; Mode line display of "by name" or "by date" guarantees the user a
3674 ;; match with the corresponding regexps. Non-matching switches are
3675 ;; shown literally.
3676 (when (eq major-mode 'dired-mode)
3677 (setq mode-name
3678 (let (case-fold-search)
3679 (cond ((string-match-p
3680 dired-sort-by-name-regexp dired-actual-switches)
3681 "Dired by name")
3682 ((string-match-p
3683 dired-sort-by-date-regexp dired-actual-switches)
3684 "Dired by date")
3686 (concat "Dired " dired-actual-switches)))))
3687 (force-mode-line-update)))
3689 (define-obsolete-function-alias 'dired-sort-set-modeline
3690 'dired-sort-set-mode-line "24.3")
3692 (defun dired-sort-toggle-or-edit (&optional arg)
3693 "Toggle sorting by date, and refresh the Dired buffer.
3694 With a prefix argument, edit the current listing switches instead."
3695 (interactive "P")
3696 (when dired-sort-inhibit
3697 (error "Cannot sort this Dired buffer"))
3698 (if arg
3699 (dired-sort-other
3700 (read-string "ls switches (must contain -l): " dired-actual-switches))
3701 (dired-sort-toggle)))
3703 (defun dired-sort-toggle ()
3704 ;; Toggle between sort by date/name. Reverts the buffer.
3705 (let ((sorting-by-date (string-match-p dired-sort-by-date-regexp
3706 dired-actual-switches))
3707 ;; Regexp for finding (possibly embedded) -t switches.
3708 (switch-regexp "\\(\\`\\| \\)-\\([a-su-zA-Z]*\\)\\(t\\)\\([^ ]*\\)")
3709 case-fold-search)
3710 ;; Remove the -t switch.
3711 (while (string-match switch-regexp dired-actual-switches)
3712 (if (and (equal (match-string 2 dired-actual-switches) "")
3713 (equal (match-string 4 dired-actual-switches) ""))
3714 ;; Remove a stand-alone -t switch.
3715 (setq dired-actual-switches
3716 (replace-match "" t t dired-actual-switches))
3717 ;; Remove a switch of the form -XtY for some X and Y.
3718 (setq dired-actual-switches
3719 (replace-match "" t t dired-actual-switches 3))))
3721 ;; Now, if we weren't sorting by date before, add the -t switch.
3722 ;; Some simple-minded ls implementations (eg ftp servers) only
3723 ;; allow a single option string, so try not to add " -t" if possible.
3724 (unless sorting-by-date
3725 (setq dired-actual-switches
3726 (concat dired-actual-switches
3727 (if (string-match-p "\\`-[[:alnum:]]+\\'"
3728 dired-actual-switches)
3730 " -t")))))
3731 (dired-sort-set-mode-line)
3732 (revert-buffer))
3734 ;; Some user code loads dired especially for this.
3735 ;; Don't do that--use replace-regexp-in-string instead.
3736 (defun dired-replace-in-string (regexp newtext string)
3737 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
3738 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
3739 (let ((result "") (start 0) mb me)
3740 (while (string-match regexp string start)
3741 (setq mb (match-beginning 0)
3742 me (match-end 0)
3743 result (concat result (substring string start mb) newtext)
3744 start me))
3745 (concat result (substring string start))))
3747 (defun dired-sort-other (switches &optional no-revert)
3748 "Specify new `ls' SWITCHES for current Dired buffer.
3749 Values matching `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp'
3750 set the minor mode accordingly, others appear literally in the mode line.
3751 With optional second arg NO-REVERT, don't refresh the listing afterwards."
3752 (dired-sort-R-check switches)
3753 (setq dired-actual-switches switches)
3754 (dired-sort-set-mode-line)
3755 (or no-revert (revert-buffer)))
3757 (defvar-local dired-subdir-alist-pre-R nil
3758 "Value of `dired-subdir-alist' before -R switch added.")
3760 (defun dired-sort-R-check (switches)
3761 "Additional processing of -R in ls option string SWITCHES.
3762 Saves `dired-subdir-alist' when R is set and restores saved value
3763 minus any directories explicitly deleted when R is cleared.
3764 To be called first in body of `dired-sort-other', etc."
3765 (cond
3766 ((and (dired-switches-recursive-p switches)
3767 (not (dired-switches-recursive-p dired-actual-switches)))
3768 ;; Adding -R to ls switches -- save `dired-subdir-alist':
3769 (setq dired-subdir-alist-pre-R dired-subdir-alist))
3770 ((and (dired-switches-recursive-p dired-actual-switches)
3771 (not (dired-switches-recursive-p switches)))
3772 ;; Deleting -R from ls switches -- revert to pre-R subdirs
3773 ;; that are still present:
3774 (setq dired-subdir-alist
3775 (if dired-subdir-alist-pre-R
3776 (let (subdirs)
3777 (while dired-subdir-alist-pre-R
3778 (if (assoc (caar dired-subdir-alist-pre-R)
3779 dired-subdir-alist)
3780 ;; subdir still present...
3781 (setq subdirs
3782 (cons (car dired-subdir-alist-pre-R)
3783 subdirs)))
3784 (setq dired-subdir-alist-pre-R
3785 (cdr dired-subdir-alist-pre-R)))
3786 (reverse subdirs))
3787 ;; No pre-R subdir alist, so revert to main directory
3788 ;; listing:
3789 (list (car (reverse dired-subdir-alist))))))))
3792 ;;;; Drag and drop support
3794 (defcustom dired-recursive-copies 'top
3795 "Whether Dired copies directories recursively.
3796 If nil, never copy recursively.
3797 `always' means to copy recursively without asking.
3798 `top' means to ask for each directory at top level.
3799 Any other value means to ask for each directory."
3800 :type '(choice :tag "Copy directories"
3801 (const :tag "No recursive copies" nil)
3802 (const :tag "Ask for each directory" t)
3803 (const :tag "Ask for each top directory only" top)
3804 (const :tag "Copy directories without asking" always))
3805 :group 'dired)
3807 (defun dired-dnd-popup-notice ()
3808 (message-box
3809 "Dired recursive copies are currently disabled.\nSee the variable `dired-recursive-copies'."))
3811 (declare-function x-popup-menu "menu.c" (position menu))
3813 (defun dired-dnd-do-ask-action (uri)
3814 ;; No need to get actions and descriptions from the source,
3815 ;; we only have three actions anyway.
3816 (let ((action (x-popup-menu
3818 (list "What action?"
3819 (cons ""
3820 '(("Copy here" . copy)
3821 ("Move here" . move)
3822 ("Link here" . link)
3823 "--"
3824 ("Cancel" . nil)))))))
3825 (if action
3826 (dired-dnd-handle-local-file uri action)
3827 nil)))
3829 (declare-function dired-relist-entry "dired-aux" (file))
3830 (declare-function make-symbolic-link "fileio.c")
3832 ;; Only used when (featurep 'dnd).
3833 (declare-function dnd-get-local-file-name "dnd" (uri &optional must-exist))
3834 (declare-function dnd-get-local-file-uri "dnd" (uri))
3836 (defvar dired-overwrite-confirmed) ;Defined in dired-aux.
3838 (defun dired-dnd-handle-local-file (uri action)
3839 "Copy, move or link a file to the Dired directory.
3840 URI is the file to handle, ACTION is one of copy, move, link or ask.
3841 Ask means pop up a menu for the user to select one of copy, move or link."
3842 (require 'dired-aux)
3843 (let* ((from (dnd-get-local-file-name uri t))
3844 (to (when from
3845 (concat (dired-current-directory)
3846 (file-name-nondirectory from)))))
3847 (when from
3848 (cond ((eq action 'ask)
3849 (dired-dnd-do-ask-action uri))
3850 ;; If copying a directory and dired-recursive-copies is
3851 ;; nil, dired-copy-file fails. Pop up a notice.
3852 ((and (memq action '(copy private))
3853 (file-directory-p from)
3854 (not dired-recursive-copies))
3855 (dired-dnd-popup-notice))
3856 ((memq action '(copy private move link))
3857 (let ((overwrite (and (file-exists-p to)
3858 (y-or-n-p
3859 (format-message
3860 "Overwrite existing file `%s'? " to))))
3861 ;; Binding dired-overwrite-confirmed to nil makes
3862 ;; dired-handle-overwrite a no-op. We instead use
3863 ;; y-or-n-p, which pops a graphical menu.
3864 dired-overwrite-confirmed backup-file)
3865 (when (and overwrite
3866 ;; d-b-o is defined in dired-aux.
3867 (boundp 'dired-backup-overwrite)
3868 dired-backup-overwrite
3869 (setq backup-file
3870 (car (find-backup-file-name to)))
3871 (or (eq dired-backup-overwrite 'always)
3872 (y-or-n-p
3873 (format-message
3874 "Make backup for existing file `%s'? " to))))
3875 (rename-file to backup-file 0)
3876 (dired-relist-entry backup-file))
3877 (cond ((memq action '(copy private))
3878 (dired-copy-file from to overwrite))
3879 ((eq action 'move)
3880 (dired-rename-file from to overwrite))
3881 ((eq action 'link)
3882 (make-symbolic-link from to overwrite)))
3883 (dired-relist-entry to)
3884 action))))))
3886 (defun dired-dnd-handle-file (uri action)
3887 "Copy, move or link a file to the Dired directory if it is a local file.
3888 URI is the file to handle. If the hostname in the URI isn't local, do nothing.
3889 ACTION is one of copy, move, link or ask.
3890 Ask means pop up a menu for the user to select one of copy, move or link."
3891 (let ((local-file (dnd-get-local-file-uri uri)))
3892 (if local-file (dired-dnd-handle-local-file local-file action)
3893 nil)))
3896 ;;;; Desktop support
3898 (eval-when-compile (require 'desktop))
3899 (declare-function desktop-file-name "desktop" (filename dirname))
3901 (defun dired-desktop-buffer-misc-data (dirname)
3902 "Auxiliary information to be saved in desktop file."
3903 (cons
3904 ;; Value of `dired-directory'.
3905 (if (consp dired-directory)
3906 ;; Directory name followed by list of files.
3907 (cons (desktop-file-name (car dired-directory) dirname)
3908 (cdr dired-directory))
3909 ;; Directory name, optionally with shell wildcard.
3910 (desktop-file-name dired-directory dirname))
3911 ;; Subdirectories in `dired-subdir-alist'.
3912 (cdr
3913 (nreverse
3914 (mapcar
3915 (function (lambda (f) (desktop-file-name (car f) dirname)))
3916 dired-subdir-alist)))))
3918 (defun dired-restore-desktop-buffer (_file-name
3919 _buffer-name
3920 misc-data)
3921 "Restore a Dired buffer specified in a desktop file."
3922 ;; First element of `misc-data' is the value of `dired-directory'.
3923 ;; This value is a directory name, optionally with shell wildcard or
3924 ;; a directory name followed by list of files.
3925 (let* ((dired-dir (car misc-data))
3926 (dir (if (consp dired-dir) (car dired-dir) dired-dir)))
3927 (if (file-directory-p (file-name-directory dir))
3928 (with-demoted-errors "Desktop: Problem restoring directory: %S"
3929 (dired dired-dir)
3930 ;; The following elements of `misc-data' are the keys
3931 ;; from `dired-subdir-alist'.
3932 (mapc 'dired-maybe-insert-subdir (cdr misc-data))
3933 (current-buffer))
3934 (message "Desktop: Directory %s no longer exists." dir)
3935 (when desktop-missing-file-warning (sit-for 1))
3936 nil)))
3938 (add-to-list 'desktop-buffer-mode-handlers
3939 '(dired-mode . dired-restore-desktop-buffer))
3941 (provide 'dired)
3943 (run-hooks 'dired-load-hook) ; for your customizations
3945 ;;; dired.el ends here