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