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