* lisp/dired.el (dired-insert-directory): Move `file-remote-p' check up.
[emacs.git] / lisp / dired.el
bloba056ad679fa2df853b90b51a05eaa2c9fde64be9
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 (pop-to-buffer-same-window (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 DIR-OR-LIST is a string and there is an existing dired buffer
876 ;; for it, just leave 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 ;; Revert the buffer if DIR-OR-LIST is a cons or `dired-directory'
883 ;; is a cons and DIR-OR-LIST is a string.
884 ;; A pity we can't possibly do "Directory has changed - refresh? "
885 ;; like find-file does.
886 ;; Optional argument MODE is passed to dired-find-buffer-nocreate,
887 ;; see there.
888 (let* ((old-buf (current-buffer))
889 (dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
890 ;; Look for an existing buffer.
891 (buffer (dired-find-buffer-nocreate dirname mode))
892 ;; Note that buffer already is in dired-mode, if found.
893 (new-buffer-p (null buffer)))
894 (or buffer
895 (setq buffer (create-file-buffer (directory-file-name dirname))))
896 (set-buffer buffer)
897 (if (not new-buffer-p) ; existing buffer ...
898 (cond (switches ; ... but new switches
899 ;; file list may have changed
900 (setq dired-directory dir-or-list)
901 ;; this calls dired-revert
902 (dired-sort-other switches))
903 ;; Always revert when `dir-or-list' is a cons. Also revert
904 ;; if `dired-directory' is a cons but `dir-or-list' is not.
905 ((or (consp dir-or-list) (consp dired-directory))
906 (setq dired-directory dir-or-list)
907 (revert-buffer))
908 ;; Always revert regardless of whether it has changed or not.
909 ((eq dired-auto-revert-buffer t)
910 (revert-buffer))
911 ;; Revert when predicate function returns non-nil.
912 ((functionp dired-auto-revert-buffer)
913 (when (funcall dired-auto-revert-buffer dirname)
914 (revert-buffer)
915 (message "Changed directory automatically updated")))
916 ;; If directory has changed on disk, offer to revert.
917 ((when (dired-directory-changed-p dirname)
918 (message "%s"
919 (substitute-command-keys
920 "Directory has changed on disk; type \\[revert-buffer] to update Dired")))))
921 ;; Else a new buffer
922 (setq default-directory
923 (or (car-safe (insert-directory-wildcard-in-dir-p dirname))
924 ;; We can do this unconditionally
925 ;; because dired-noselect ensures that the name
926 ;; is passed in directory name syntax
927 ;; if it was the name of a directory at all.
928 (file-name-directory dirname)))
929 (or switches (setq switches dired-listing-switches))
930 (if mode (funcall mode)
931 (dired-mode dir-or-list switches))
932 ;; default-directory and dired-actual-switches are set now
933 ;; (buffer-local), so we can call dired-readin:
934 (let ((failed t))
935 (unwind-protect
936 (progn (dired-readin)
937 (setq failed nil))
938 ;; dired-readin can fail if parent directories are inaccessible.
939 ;; Don't leave an empty buffer around in that case.
940 (if failed (kill-buffer buffer))))
941 (goto-char (point-min))
942 (dired-initial-position dirname))
943 (set-buffer old-buf)
944 buffer))
946 (defvar dired-buffers nil
947 ;; Enlarged by dired-advertise
948 ;; Queried by function dired-buffers-for-dir. When this detects a
949 ;; killed buffer, it is removed from this list.
950 "Alist of expanded directories and their associated Dired buffers.")
952 (defvar dired-find-subdir)
954 ;; FIXME add a doc-string, and document dired-x extensions.
955 (defun dired-find-buffer-nocreate (dirname &optional mode)
956 ;; This differs from dired-buffers-for-dir in that it does not consider
957 ;; subdirs of default-directory and searches for the first match only.
958 ;; Also, the major mode must be MODE.
959 (if (and (featurep 'dired-x)
960 dired-find-subdir
961 ;; Don't try to find a wildcard as a subdirectory.
962 (string-equal dirname (file-name-directory dirname)))
963 (let* ((cur-buf (current-buffer))
964 (buffers (nreverse
965 (dired-buffers-for-dir (expand-file-name dirname))))
966 (cur-buf-matches (and (memq cur-buf buffers)
967 ;; Wildcards must match, too:
968 (equal dired-directory dirname))))
969 ;; We don't want to switch to the same buffer---
970 (setq buffers (delq cur-buf buffers))
971 (or (car (sort buffers #'dired-buffer-more-recently-used-p))
972 ;; ---unless it's the only possibility:
973 (and cur-buf-matches cur-buf)))
974 ;; No dired-x, or dired-find-subdir nil.
975 (setq dirname (expand-file-name dirname))
976 (let (found (blist dired-buffers)) ; was (buffer-list)
977 (or mode (setq mode 'dired-mode))
978 (while blist
979 (if (null (buffer-name (cdr (car blist))))
980 (setq blist (cdr blist))
981 (with-current-buffer (cdr (car blist))
982 (if (and (eq major-mode mode)
983 dired-directory ;; nil during find-alternate-file
984 (equal dirname
985 (expand-file-name
986 (if (consp dired-directory)
987 (car dired-directory)
988 dired-directory))))
989 (setq found (cdr (car blist))
990 blist nil)
991 (setq blist (cdr blist))))))
992 found)))
995 ;; Read in a new dired buffer
997 (defun dired-readin ()
998 "Read in a new Dired buffer.
999 Differs from `dired-insert-subdir' in that it accepts
1000 wildcards, erases the buffer, and builds the subdir-alist anew
1001 \(including making it buffer-local and clearing it first)."
1003 ;; default-directory and dired-actual-switches must be buffer-local
1004 ;; and initialized by now.
1005 (let (dirname
1006 ;; This makes readin much much faster.
1007 ;; In particular, it prevents the font lock hook from running
1008 ;; until the directory is all read in.
1009 (inhibit-modification-hooks t))
1010 (if (consp dired-directory)
1011 (setq dirname (car dired-directory))
1012 (setq dirname dired-directory))
1013 (setq dirname (expand-file-name dirname))
1014 (save-excursion
1015 ;; This hook which may want to modify dired-actual-switches
1016 ;; based on dired-directory, e.g. with ange-ftp to a SysV host
1017 ;; where ls won't understand -Al switches.
1018 (run-hooks 'dired-before-readin-hook)
1019 (if (consp buffer-undo-list)
1020 (setq buffer-undo-list nil))
1021 (setq-local file-name-coding-system
1022 (or coding-system-for-read file-name-coding-system))
1023 (let ((inhibit-read-only t)
1024 ;; Don't make undo entries for readin.
1025 (buffer-undo-list t))
1026 (widen)
1027 (erase-buffer)
1028 (dired-readin-insert))
1029 (goto-char (point-min))
1030 ;; Must first make alist buffer local and set it to nil because
1031 ;; dired-build-subdir-alist will call dired-clear-alist first
1032 (setq-local dired-subdir-alist nil)
1033 (dired-build-subdir-alist)
1034 (let ((attributes (file-attributes dirname)))
1035 (if (eq (car attributes) t)
1036 (set-visited-file-modtime (nth 5 attributes))))
1037 (set-buffer-modified-p nil)
1038 ;; No need to narrow since the whole buffer contains just
1039 ;; dired-readin's output, nothing else. The hook can
1040 ;; successfully use dired functions (e.g. dired-get-filename)
1041 ;; as the subdir-alist has been built in dired-readin.
1042 (run-hooks 'dired-after-readin-hook))))
1044 ;; Subroutines of dired-readin
1046 (defun dired-readin-insert ()
1047 ;; Insert listing for the specified dir (and maybe file list)
1048 ;; already in dired-directory, assuming a clean buffer.
1049 (let (dir file-list)
1050 (if (consp dired-directory)
1051 (setq dir (car dired-directory)
1052 file-list (cdr dired-directory))
1053 (setq dir dired-directory
1054 file-list nil))
1055 (setq dir (expand-file-name dir))
1056 (if (and (equal "" (file-name-nondirectory dir))
1057 (not file-list))
1058 ;; If we are reading a whole single directory...
1059 (dired-insert-directory dir dired-actual-switches nil nil t)
1060 (if (and (not (insert-directory-wildcard-in-dir-p dir))
1061 (not (file-readable-p
1062 (directory-file-name (file-name-directory dir)))))
1063 (error "Directory %s inaccessible or nonexistent" dir))
1064 ;; Else treat it as a wildcard spec
1065 ;; unless we have an explicit list of files.
1066 (dired-insert-directory dir dired-actual-switches
1067 file-list (not file-list) t))))
1069 (defun dired-align-file (beg end)
1070 "Align the fields of a file to the ones of surrounding lines.
1071 BEG..END is the line where the file info is located."
1072 ;; Some versions of ls try to adjust the size of each field so as to just
1073 ;; hold the largest element ("largest" in the current invocation, of
1074 ;; course). So when a single line is output, the size of each field is
1075 ;; just big enough for that one output. Thus when dired refreshes one
1076 ;; line, the alignment if this line w.r.t the rest is messed up because
1077 ;; the fields of that one line will generally be smaller.
1079 ;; To work around this problem, we here add spaces to try and
1080 ;; re-align the fields as needed. Since this is purely aesthetic,
1081 ;; it is of utmost importance that it doesn't mess up anything like
1082 ;; `dired-move-to-filename'. To this end, we limit ourselves to
1083 ;; adding spaces only, and to only add them at places where there
1084 ;; was already at least one space. This way, as long as
1085 ;; `directory-listing-before-filename-regexp' always matches spaces
1086 ;; with "*" or "+", we know we haven't made anything worse. There
1087 ;; is one spot where the exact number of spaces is important, which
1088 ;; is just before the actual filename, so we refrain from adding
1089 ;; spaces there (and within the filename as well, of course).
1090 (save-excursion
1091 (let (file file-col other other-col)
1092 ;; Check that there is indeed a file, and that there is another adjacent
1093 ;; file with which to align, and that additional spaces are needed to
1094 ;; align the filenames.
1095 (when (and (setq file (progn (goto-char beg)
1096 (dired-move-to-filename nil end)))
1097 (setq file-col (current-column))
1098 (setq other
1099 (or (and (goto-char beg)
1100 (zerop (forward-line -1))
1101 (dired-move-to-filename))
1102 (and (goto-char beg)
1103 (zerop (forward-line 1))
1104 (dired-move-to-filename))))
1105 (setq other-col (current-column))
1106 (/= file other)
1107 ;; Make sure there is some work left to do.
1108 (> other-col file-col))
1109 ;; If we've only looked at the line above, check to see if the line
1110 ;; below exists as well and if so, align with the shorter one.
1111 (when (and (< other file)
1112 (goto-char beg)
1113 (zerop (forward-line 1))
1114 (dired-move-to-filename))
1115 (let ((alt-col (current-column)))
1116 (when (< alt-col other-col)
1117 (setq other-col alt-col)
1118 (setq other (point)))))
1119 ;; Keep positions uptodate when we insert stuff.
1120 (if (> other file) (setq other (copy-marker other)))
1121 (setq file (copy-marker file))
1122 ;; Main loop.
1123 (goto-char beg)
1124 (skip-chars-forward " ") ;Skip to the first field.
1125 (while (and (> other-col file-col)
1126 ;; Don't touch anything just before (and after) the
1127 ;; beginning of the filename.
1128 (> file (point)))
1129 ;; We're now just in front of a field, with a space behind us.
1130 (let* ((curcol (current-column))
1131 ;; Nums are right-aligned.
1132 (num-align (looking-at-p "[0-9]"))
1133 ;; Let's look at the other line, in the same column: we
1134 ;; should be either near the end of the previous field, or
1135 ;; in the space between that field and the next.
1136 ;; [ Of course, it's also possible that we're already within
1137 ;; the next field or even past it, but that's unlikely since
1138 ;; other-col > file-col. ]
1139 ;; Let's find the distance to the alignment-point (either
1140 ;; the beginning or the end of the next field, depending on
1141 ;; whether this field is left or right aligned).
1142 (align-pt-offset
1143 (save-excursion
1144 (goto-char other)
1145 (move-to-column curcol)
1146 (when (looking-at
1147 (concat
1148 (if (eq (char-before) ?\s) " *" "[^ ]* *")
1149 (if num-align "[0-9][^ ]*")))
1150 (- (match-end 0) (match-beginning 0)))))
1151 ;; Now, the number of spaces to insert is align-pt-offset
1152 ;; minus the distance to the equivalent point on the
1153 ;; current line.
1154 (spaces
1155 (if (not num-align)
1156 align-pt-offset
1157 (and align-pt-offset
1158 (save-excursion
1159 (skip-chars-forward "^ ")
1160 (- align-pt-offset (- (current-column) curcol)))))))
1161 (when (and spaces (> spaces 0))
1162 (setq file-col (+ spaces file-col))
1163 (if (> file-col other-col)
1164 (setq spaces (- spaces (- file-col other-col))))
1165 (insert-char ?\s spaces)
1166 ;; Let's just make really sure we did not mess up.
1167 (unless (save-excursion
1168 (eq (dired-move-to-filename) (marker-position file)))
1169 ;; Damn! We messed up: let's revert the change.
1170 (delete-char (- spaces)))))
1171 ;; Now skip to next field.
1172 (skip-chars-forward "^ ") (skip-chars-forward " "))
1173 (set-marker file nil)))))
1176 (defvar ls-lisp-use-insert-directory-program)
1178 (defun dired-check-switches (switches short &optional long)
1179 "Return non-nil if the string SWITCHES matches LONG or SHORT format."
1180 (let (case-fold-search)
1181 (and (stringp switches)
1182 (string-match-p (concat "\\(\\`\\| \\)-[[:alnum:]]*" short
1183 (if long (concat "\\|--" long "\\>") ""))
1184 switches))))
1186 (defun dired-switches-escape-p (switches)
1187 "Return non-nil if the string SWITCHES contains -b or --escape."
1188 ;; Do not match things like "--block-size" that happen to contain "b".
1189 (dired-check-switches switches "b" "escape"))
1191 (defun dired-switches-recursive-p (switches)
1192 "Return non-nil if the string SWITCHES contains -R or --recursive."
1193 (dired-check-switches switches "R" "recursive"))
1195 (defun dired-insert-directory (dir switches &optional file-list wildcard hdr)
1196 "Insert a directory listing of DIR, Dired style.
1197 Use SWITCHES to make the listings.
1198 If FILE-LIST is non-nil, list only those files.
1199 Otherwise, if WILDCARD is non-nil, expand wildcards;
1200 in that case, DIR should be a file name that uses wildcards.
1201 In other cases, DIR should be a directory name or a directory filename.
1202 If HDR is non-nil, insert a header line with the directory name."
1203 (let ((opoint (point))
1204 (process-environment (copy-sequence process-environment))
1205 end)
1206 (if (and
1207 ;; Don't try to invoke `ls' if we are on DOS/Windows where
1208 ;; ls-lisp emulation is used, except if they want to use `ls'
1209 ;; as indicated by `ls-lisp-use-insert-directory-program'.
1210 (not (and (featurep 'ls-lisp)
1211 (null ls-lisp-use-insert-directory-program)))
1212 (not (and (featurep 'eshell)
1213 (bound-and-true-p eshell-ls-use-in-dired)))
1214 (or (file-remote-p dir)
1215 (if (eq dired-use-ls-dired 'unspecified)
1216 ;; Check whether "ls --dired" gives exit code 0, and
1217 ;; save the answer in `dired-use-ls-dired'.
1218 (or (setq dired-use-ls-dired
1219 (eq 0 (call-process insert-directory-program
1220 nil nil nil "--dired")))
1221 (progn
1222 (message "ls does not support --dired; \
1223 see `dired-use-ls-dired' for more details.")
1224 nil))
1225 dired-use-ls-dired)))
1226 (setq switches (concat "--dired " switches)))
1227 ;; Expand directory wildcards and fill file-list.
1228 (let ((dir-wildcard (insert-directory-wildcard-in-dir-p dir)))
1229 (cond (dir-wildcard
1230 (setq switches (concat "-d " switches))
1231 (let ((default-directory (car dir-wildcard))
1232 (script (format "ls %s %s" switches (cdr dir-wildcard))))
1233 (unless
1234 (zerop
1235 (process-file
1236 "/bin/sh" nil (current-buffer) nil "-c" script))
1237 (user-error
1238 "%s: No files matching wildcard" (cdr dir-wildcard)))
1239 (insert-directory-clean (point) switches)))
1241 ;; We used to specify the C locale here, to force English
1242 ;; month names; but this should not be necessary any
1243 ;; more, with the new value of
1244 ;; `directory-listing-before-filename-regexp'.
1245 (if file-list
1246 (dolist (f file-list)
1247 (let ((beg (point)))
1248 (insert-directory f switches nil nil)
1249 ;; Re-align fields, if necessary.
1250 (dired-align-file beg (point))))
1251 (insert-directory dir switches wildcard (not wildcard))))))
1252 ;; Quote certain characters, unless ls quoted them for us.
1253 (if (not (dired-switches-escape-p dired-actual-switches))
1254 (save-excursion
1255 (setq end (point-marker))
1256 (goto-char opoint)
1257 (while (search-forward "\\" end t)
1258 (replace-match (apply #'propertize
1259 "\\\\"
1260 (text-properties-at (match-beginning 0)))
1261 nil t))
1262 (goto-char opoint)
1263 (while (search-forward "\^m" end t)
1264 (replace-match (apply #'propertize
1265 "\\015"
1266 (text-properties-at (match-beginning 0)))
1267 nil t))
1268 (set-marker end nil))
1269 ;; Replace any newlines in DIR with literal "\n"s, for the sake
1270 ;; of the header line. To disambiguate a literal "\n" in the
1271 ;; actual dirname, we also replace "\" with "\\".
1272 ;; Personally, I think this should always be done, irrespective
1273 ;; of the value of dired-actual-switches, because:
1274 ;; i) Dired simply does not work with an unescaped newline in
1275 ;; the directory name used in the header (bug=10469#28), and
1276 ;; ii) "\" is always replaced with "\\" in the listing, so doing
1277 ;; it in the header as well makes things consistent.
1278 ;; But at present it is only done if "-b" is in ls-switches,
1279 ;; because newlines in dirnames are uncommon, and people may
1280 ;; have gotten used to seeing unescaped "\" in the headers.
1281 ;; Note: adjust dired-build-subdir-alist if you change this.
1282 (setq dir (replace-regexp-in-string "\\\\" "\\\\" dir nil t)
1283 dir (replace-regexp-in-string "\n" "\\n" dir nil t)))
1284 ;; If we used --dired and it worked, the lines are already indented.
1285 ;; Otherwise, indent them.
1286 (unless (save-excursion
1287 (goto-char opoint)
1288 (looking-at-p " "))
1289 (let ((indent-tabs-mode nil))
1290 (indent-rigidly opoint (point) 2)))
1291 ;; Insert text at the beginning to standardize things.
1292 (let ((content-point opoint))
1293 (save-excursion
1294 (goto-char opoint)
1295 (when (and (or hdr wildcard)
1296 (not (and (looking-at "^ \\(.*\\):$")
1297 (file-name-absolute-p (match-string 1)))))
1298 ;; Note that dired-build-subdir-alist will replace the name
1299 ;; by its expansion, so it does not matter whether what we insert
1300 ;; here is fully expanded, but it should be absolute.
1301 (insert " " (or (car-safe (insert-directory-wildcard-in-dir-p dir))
1302 (directory-file-name (file-name-directory dir))) ":\n")
1303 (setq content-point (point)))
1304 (when wildcard
1305 ;; Insert "wildcard" line where "total" line would be for a full dir.
1306 (insert " wildcard " (or (cdr-safe (insert-directory-wildcard-in-dir-p dir))
1307 (file-name-nondirectory dir))
1308 "\n")))
1309 (dired-insert-set-properties content-point (point)))))
1311 (defun dired-insert-set-properties (beg end)
1312 "Add various text properties to the lines in the region."
1313 (save-excursion
1314 (goto-char beg)
1315 (while (< (point) end)
1316 (ignore-errors
1317 (if (not (dired-move-to-filename))
1318 (unless (or (looking-at-p "^$")
1319 (looking-at-p dired-subdir-regexp))
1320 (put-text-property (line-beginning-position)
1321 (1+ (line-end-position))
1322 'invisible 'dired-hide-details-information))
1323 (put-text-property (+ (line-beginning-position) 1) (1- (point))
1324 'invisible 'dired-hide-details-detail)
1325 (add-text-properties
1326 (point)
1327 (progn
1328 (dired-move-to-end-of-filename)
1329 (point))
1330 '(mouse-face
1331 highlight
1332 dired-filename t
1333 help-echo "mouse-2: visit this file in other window"))
1334 (when (< (+ (point) 4) (line-end-position))
1335 (put-text-property (+ (point) 4) (line-end-position)
1336 'invisible 'dired-hide-details-link))))
1337 (forward-line 1))))
1339 ;; Reverting a dired buffer
1341 (defun dired-revert (&optional _arg _noconfirm)
1342 "Reread the Dired buffer.
1343 Must also be called after `dired-actual-switches' have changed.
1344 Should not fail even on completely garbaged buffers.
1345 Preserves old cursor, marks/flags, hidden-p.
1347 Dired sets `revert-buffer-function' to this function. The args
1348 ARG and NOCONFIRM, passed from `revert-buffer', are ignored."
1349 (widen) ; just in case user narrowed
1350 (let ((modflag (buffer-modified-p))
1351 (positions (dired-save-positions))
1352 (mark-alist nil) ; save marked files
1353 (hidden-subdirs (dired-remember-hidden))
1354 (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
1355 (case-fold-search nil) ; we check for upper case ls flags
1356 (inhibit-read-only t))
1357 (goto-char (point-min))
1358 (setq mark-alist;; only after dired-remember-hidden since this unhides:
1359 (dired-remember-marks (point-min) (point-max)))
1360 ;; treat top level dir extra (it may contain wildcards)
1361 (if (not (consp dired-directory))
1362 (dired-uncache dired-directory)
1363 (dired-uncache (car dired-directory))
1364 (dolist (dir (cdr dired-directory))
1365 (if (file-name-absolute-p dir)
1366 (dired-uncache dir))))
1367 ;; Run dired-after-readin-hook just once, below.
1368 (let ((dired-after-readin-hook nil))
1369 (dired-readin)
1370 (dired-insert-old-subdirs old-subdir-alist))
1371 (dired-mark-remembered mark-alist) ; mark files that were marked
1372 ;; ... run the hook for the whole buffer, and only after markers
1373 ;; have been reinserted (else omitting in dired-x would omit marked files)
1374 (run-hooks 'dired-after-readin-hook) ; no need to narrow
1375 (dired-restore-positions positions)
1376 (save-excursion ; hide subdirs that were hidden
1377 (dolist (dir hidden-subdirs)
1378 (if (dired-goto-subdir dir)
1379 (dired-hide-subdir 1))))
1380 (unless modflag (restore-buffer-modified-p nil)))
1381 ;; outside of the let scope
1382 ;;; Might as well not override the user if the user changed this.
1383 ;;; (setq buffer-read-only t)
1386 ;; Subroutines of dired-revert
1387 ;; Some of these are also used when inserting subdirs.
1389 (defun dired-save-positions ()
1390 "Return current positions in the buffer and all windows with this directory.
1391 The positions have the form (BUFFER-POSITION WINDOW-POSITIONS).
1393 BUFFER-POSITION is the point position in the current Dired buffer.
1394 It has the form (BUFFER DIRED-FILENAME BUFFER-POINT).
1396 WINDOW-POSITIONS are current positions in all windows displaying
1397 this dired buffer. The window positions have the form (WINDOW
1398 DIRED-FILENAME WINDOW-POINT)."
1399 (list
1400 (list (current-buffer) (dired-get-filename nil t) (point))
1401 (mapcar (lambda (w)
1402 (list w
1403 (with-selected-window w
1404 (dired-get-filename nil t))
1405 (window-point w)))
1406 (get-buffer-window-list nil 0 t))))
1408 (defun dired-restore-positions (positions)
1409 "Restore POSITIONS saved with `dired-save-positions'."
1410 (let* ((buf-file-pos (nth 0 positions))
1411 (buffer (nth 0 buf-file-pos)))
1412 (unless (and (nth 1 buf-file-pos)
1413 (dired-goto-file (nth 1 buf-file-pos)))
1414 (goto-char (nth 2 buf-file-pos))
1415 (dired-move-to-filename))
1416 (dolist (win-file-pos (nth 1 positions))
1417 ;; Ensure that window still displays the original buffer.
1418 (when (eq (window-buffer (nth 0 win-file-pos)) buffer)
1419 (with-selected-window (nth 0 win-file-pos)
1420 (unless (and (nth 1 win-file-pos)
1421 (dired-goto-file (nth 1 win-file-pos)))
1422 (goto-char (nth 2 win-file-pos))
1423 (dired-move-to-filename)))))))
1425 (defun dired-remember-marks (beg end)
1426 "Return alist of files and their marks, from BEG to END."
1427 (if selective-display ; must unhide to make this work.
1428 (let ((inhibit-read-only t))
1429 (subst-char-in-region beg end ?\r ?\n)))
1430 (let (fil chr alist)
1431 (save-excursion
1432 (goto-char beg)
1433 (while (re-search-forward dired-re-mark end t)
1434 (if (setq fil (dired-get-filename nil t))
1435 (setq chr (preceding-char)
1436 alist (cons (cons fil chr) alist)))))
1437 alist))
1439 (defun dired-mark-remembered (alist)
1440 "Mark all files remembered in ALIST.
1441 Each element of ALIST looks like (FILE . MARKERCHAR)."
1442 (let (elt fil chr)
1443 (save-excursion
1444 (while alist
1445 (setq elt (car alist)
1446 alist (cdr alist)
1447 fil (car elt)
1448 chr (cdr elt))
1449 (when (dired-goto-file fil)
1450 (beginning-of-line)
1451 (delete-char 1)
1452 (insert chr))))))
1454 (defun dired-remember-hidden ()
1455 "Return a list of names of subdirs currently hidden."
1456 (let ((l dired-subdir-alist) dir pos result)
1457 (while l
1458 (setq dir (car (car l))
1459 pos (cdr (car l))
1460 l (cdr l))
1461 (goto-char pos)
1462 (skip-chars-forward "^\r\n")
1463 (if (eq (following-char) ?\r)
1464 (setq result (cons dir result))))
1465 result))
1467 (defun dired-insert-old-subdirs (old-subdir-alist)
1468 "Try to insert all subdirs that were displayed before.
1469 Do so according to the former subdir alist OLD-SUBDIR-ALIST."
1470 (or (dired-switches-recursive-p dired-actual-switches)
1471 (let (elt dir)
1472 (while old-subdir-alist
1473 (setq elt (car old-subdir-alist)
1474 old-subdir-alist (cdr old-subdir-alist)
1475 dir (car elt))
1476 (ignore-errors
1477 (dired-uncache dir)
1478 (dired-insert-subdir dir))))))
1480 (defun dired-uncache (dir)
1481 "Remove directory DIR from any directory cache."
1482 (let ((handler (find-file-name-handler dir 'dired-uncache)))
1483 (if handler
1484 (funcall handler 'dired-uncache dir))))
1486 ;; dired mode key bindings and initialization
1488 (defvar dired-mode-map
1489 ;; This looks ugly when substitute-command-keys uses C-d instead d:
1490 ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
1491 (let ((map (make-keymap)))
1492 (set-keymap-parent map special-mode-map)
1493 (define-key map [mouse-2] 'dired-mouse-find-file-other-window)
1494 (define-key map [follow-link] 'mouse-face)
1495 ;; Commands to mark or flag certain categories of files
1496 (define-key map "#" 'dired-flag-auto-save-files)
1497 (define-key map "." 'dired-clean-directory)
1498 (define-key map "~" 'dired-flag-backup-files)
1499 ;; Upper case keys (except !) for operating on the marked files
1500 (define-key map "A" 'dired-do-find-regexp)
1501 (define-key map "C" 'dired-do-copy)
1502 (define-key map "B" 'dired-do-byte-compile)
1503 (define-key map "D" 'dired-do-delete)
1504 (define-key map "G" 'dired-do-chgrp)
1505 (define-key map "H" 'dired-do-hardlink)
1506 (define-key map "L" 'dired-do-load)
1507 (define-key map "M" 'dired-do-chmod)
1508 (define-key map "O" 'dired-do-chown)
1509 (define-key map "P" 'dired-do-print)
1510 (define-key map "Q" 'dired-do-find-regexp-and-replace)
1511 (define-key map "R" 'dired-do-rename)
1512 (define-key map "S" 'dired-do-symlink)
1513 (define-key map "T" 'dired-do-touch)
1514 (define-key map "X" 'dired-do-shell-command)
1515 (define-key map "Z" 'dired-do-compress)
1516 (define-key map "c" 'dired-do-compress-to)
1517 (define-key map "!" 'dired-do-shell-command)
1518 (define-key map "&" 'dired-do-async-shell-command)
1519 ;; Comparison commands
1520 (define-key map "=" 'dired-diff)
1521 ;; Tree Dired commands
1522 (define-key map "\M-\C-?" 'dired-unmark-all-files)
1523 (define-key map "\M-\C-d" 'dired-tree-down)
1524 (define-key map "\M-\C-u" 'dired-tree-up)
1525 (define-key map "\M-\C-n" 'dired-next-subdir)
1526 (define-key map "\M-\C-p" 'dired-prev-subdir)
1527 ;; move to marked files
1528 (define-key map "\M-{" 'dired-prev-marked-file)
1529 (define-key map "\M-}" 'dired-next-marked-file)
1530 ;; Make all regexp commands share a `%' prefix:
1531 ;; We used to get to the submap via a symbol dired-regexp-prefix,
1532 ;; but that seems to serve little purpose, and copy-keymap
1533 ;; does a better job without it.
1534 (define-key map "%" nil)
1535 (define-key map "%u" 'dired-upcase)
1536 (define-key map "%l" 'dired-downcase)
1537 (define-key map "%d" 'dired-flag-files-regexp)
1538 (define-key map "%g" 'dired-mark-files-containing-regexp)
1539 (define-key map "%m" 'dired-mark-files-regexp)
1540 (define-key map "%r" 'dired-do-rename-regexp)
1541 (define-key map "%C" 'dired-do-copy-regexp)
1542 (define-key map "%H" 'dired-do-hardlink-regexp)
1543 (define-key map "%R" 'dired-do-rename-regexp)
1544 (define-key map "%S" 'dired-do-symlink-regexp)
1545 (define-key map "%&" 'dired-flag-garbage-files)
1546 ;; Commands for marking and unmarking.
1547 (define-key map "*" nil)
1548 (define-key map "**" 'dired-mark-executables)
1549 (define-key map "*/" 'dired-mark-directories)
1550 (define-key map "*@" 'dired-mark-symlinks)
1551 (define-key map "*%" 'dired-mark-files-regexp)
1552 (define-key map "*c" 'dired-change-marks)
1553 (define-key map "*s" 'dired-mark-subdir-files)
1554 (define-key map "*m" 'dired-mark)
1555 (define-key map "*u" 'dired-unmark)
1556 (define-key map "*?" 'dired-unmark-all-files)
1557 (define-key map "*!" 'dired-unmark-all-marks)
1558 (define-key map "U" 'dired-unmark-all-marks)
1559 (define-key map "*\177" 'dired-unmark-backward)
1560 (define-key map "*\C-n" 'dired-next-marked-file)
1561 (define-key map "*\C-p" 'dired-prev-marked-file)
1562 (define-key map "*t" 'dired-toggle-marks)
1563 ;; Lower keys for commands not operating on all the marked files
1564 (define-key map "a" 'dired-find-alternate-file)
1565 (define-key map "d" 'dired-flag-file-deletion)
1566 (define-key map "e" 'dired-find-file)
1567 (define-key map "f" 'dired-find-file)
1568 (define-key map "\C-m" 'dired-find-file)
1569 (put 'dired-find-file :advertised-binding "\C-m")
1570 (define-key map "g" 'revert-buffer)
1571 (define-key map "i" 'dired-maybe-insert-subdir)
1572 (define-key map "j" 'dired-goto-file)
1573 (define-key map "k" 'dired-do-kill-lines)
1574 (define-key map "l" 'dired-do-redisplay)
1575 (define-key map "m" 'dired-mark)
1576 (define-key map "n" 'dired-next-line)
1577 (define-key map "o" 'dired-find-file-other-window)
1578 (define-key map "\C-o" 'dired-display-file)
1579 (define-key map "p" 'dired-previous-line)
1580 (define-key map "s" 'dired-sort-toggle-or-edit)
1581 (define-key map "t" 'dired-toggle-marks)
1582 (define-key map "u" 'dired-unmark)
1583 (define-key map "v" 'dired-view-file)
1584 (define-key map "w" 'dired-copy-filename-as-kill)
1585 (define-key map "W" 'browse-url-of-dired-file)
1586 (define-key map "x" 'dired-do-flagged-delete)
1587 (define-key map "y" 'dired-show-file-type)
1588 (define-key map "+" 'dired-create-directory)
1589 ;; moving
1590 (define-key map "<" 'dired-prev-dirline)
1591 (define-key map ">" 'dired-next-dirline)
1592 (define-key map "^" 'dired-up-directory)
1593 (define-key map " " 'dired-next-line)
1594 (define-key map [?\S-\ ] 'dired-previous-line)
1595 (define-key map [remap next-line] 'dired-next-line)
1596 (define-key map [remap previous-line] 'dired-previous-line)
1597 ;; hiding
1598 (define-key map "$" 'dired-hide-subdir)
1599 (define-key map "\M-$" 'dired-hide-all)
1600 (define-key map "(" 'dired-hide-details-mode)
1601 ;; isearch
1602 (define-key map (kbd "M-s a C-s") 'dired-do-isearch)
1603 (define-key map (kbd "M-s a M-C-s") 'dired-do-isearch-regexp)
1604 (define-key map (kbd "M-s f C-s") 'dired-isearch-filenames)
1605 (define-key map (kbd "M-s f M-C-s") 'dired-isearch-filenames-regexp)
1606 ;; misc
1607 (define-key map [remap read-only-mode] 'dired-toggle-read-only)
1608 ;; `toggle-read-only' is an obsolete alias for `read-only-mode'
1609 (define-key map [remap toggle-read-only] 'dired-toggle-read-only)
1610 (define-key map "?" 'dired-summary)
1611 (define-key map "\177" 'dired-unmark-backward)
1612 (define-key map [remap undo] 'dired-undo)
1613 (define-key map [remap advertised-undo] 'dired-undo)
1614 ;; thumbnail manipulation (image-dired)
1615 (define-key map "\C-td" 'image-dired-display-thumbs)
1616 (define-key map "\C-tt" 'image-dired-tag-files)
1617 (define-key map "\C-tr" 'image-dired-delete-tag)
1618 (define-key map "\C-tj" 'image-dired-jump-thumbnail-buffer)
1619 (define-key map "\C-ti" 'image-dired-dired-display-image)
1620 (define-key map "\C-tx" 'image-dired-dired-display-external)
1621 (define-key map "\C-ta" 'image-dired-display-thumbs-append)
1622 (define-key map "\C-t." 'image-dired-display-thumb)
1623 (define-key map "\C-tc" 'image-dired-dired-comment-files)
1624 (define-key map "\C-tf" 'image-dired-mark-tagged-files)
1625 (define-key map "\C-t\C-t" 'image-dired-dired-toggle-marked-thumbs)
1626 (define-key map "\C-te" 'image-dired-dired-edit-comment-and-tags)
1627 ;; encryption and decryption (epa-dired)
1628 (define-key map ":d" 'epa-dired-do-decrypt)
1629 (define-key map ":v" 'epa-dired-do-verify)
1630 (define-key map ":s" 'epa-dired-do-sign)
1631 (define-key map ":e" 'epa-dired-do-encrypt)
1633 ;; Make menu bar items.
1635 ;; No need to fo this, now that top-level items are fewer.
1636 ;;;;
1637 ;; Get rid of the Edit menu bar item to save space.
1638 ;(define-key map [menu-bar edit] 'undefined)
1640 (define-key map [menu-bar subdir]
1641 (cons "Subdir" (make-sparse-keymap "Subdir")))
1643 (define-key map [menu-bar subdir hide-all]
1644 '(menu-item "Hide All" dired-hide-all
1645 :help "Hide all subdirectories, leave only header lines"))
1646 (define-key map [menu-bar subdir hide-subdir]
1647 '(menu-item "Hide/UnHide Subdir" dired-hide-subdir
1648 :help "Hide or unhide current directory listing"))
1649 (define-key map [menu-bar subdir tree-down]
1650 '(menu-item "Tree Down" dired-tree-down
1651 :help "Go to first subdirectory header down the tree"))
1652 (define-key map [menu-bar subdir tree-up]
1653 '(menu-item "Tree Up" dired-tree-up
1654 :help "Go to first subdirectory header up the tree"))
1655 (define-key map [menu-bar subdir up]
1656 '(menu-item "Up Directory" dired-up-directory
1657 :help "Edit the parent directory"))
1658 (define-key map [menu-bar subdir prev-subdir]
1659 '(menu-item "Prev Subdir" dired-prev-subdir
1660 :help "Go to previous subdirectory header line"))
1661 (define-key map [menu-bar subdir next-subdir]
1662 '(menu-item "Next Subdir" dired-next-subdir
1663 :help "Go to next subdirectory header line"))
1664 (define-key map [menu-bar subdir prev-dirline]
1665 '(menu-item "Prev Dirline" dired-prev-dirline
1666 :help "Move to next directory-file line"))
1667 (define-key map [menu-bar subdir next-dirline]
1668 '(menu-item "Next Dirline" dired-next-dirline
1669 :help "Move to previous directory-file line"))
1670 (define-key map [menu-bar subdir insert]
1671 '(menu-item "Insert This Subdir" dired-maybe-insert-subdir
1672 :help "Insert contents of subdirectory"
1673 :enable (let ((f (dired-get-filename nil t)))
1674 (and f (file-directory-p f)))))
1675 (define-key map [menu-bar immediate]
1676 (cons "Immediate" (make-sparse-keymap "Immediate")))
1678 (define-key map
1679 [menu-bar immediate image-dired-dired-display-external]
1680 '(menu-item "Display Image Externally" image-dired-dired-display-external
1681 :help "Display image in external viewer"))
1682 (define-key map
1683 [menu-bar immediate image-dired-dired-display-image]
1684 '(menu-item "Display Image" image-dired-dired-display-image
1685 :help "Display sized image in a separate window"))
1686 (define-key map
1687 [menu-bar immediate image-dired-dired-toggle-marked-thumbs]
1688 '(menu-item "Toggle Image Thumbnails in This Buffer" image-dired-dired-toggle-marked-thumbs
1689 :help "Add or remove image thumbnails in front of marked file names"))
1691 (define-key map [menu-bar immediate hide-details]
1692 '(menu-item "Hide Details" dired-hide-details-mode
1693 :help "Hide details in buffer"
1694 :button (:toggle . dired-hide-details-mode)))
1695 (define-key map [menu-bar immediate revert-buffer]
1696 '(menu-item "Refresh" revert-buffer
1697 :help "Update contents of shown directories"))
1699 (define-key map [menu-bar immediate dashes]
1700 '("--"))
1702 (define-key map [menu-bar immediate isearch-filenames-regexp]
1703 '(menu-item "Isearch Regexp in File Names..." dired-isearch-filenames-regexp
1704 :help "Incrementally search for regexp in file names only"))
1705 (define-key map [menu-bar immediate isearch-filenames]
1706 '(menu-item "Isearch in File Names..." dired-isearch-filenames
1707 :help "Incrementally search for string in file names only."))
1708 (define-key map [menu-bar immediate compare-directories]
1709 '(menu-item "Compare Directories..." dired-compare-directories
1710 :help "Mark files with different attributes in two Dired buffers"))
1711 (define-key map [menu-bar immediate backup-diff]
1712 '(menu-item "Compare with Backup" dired-backup-diff
1713 :help "Diff file at cursor with its latest backup"))
1714 (define-key map [menu-bar immediate diff]
1715 '(menu-item "Diff..." dired-diff
1716 :help "Compare file at cursor with another file"))
1717 (define-key map [menu-bar immediate view]
1718 '(menu-item "View This File" dired-view-file
1719 :help "Examine file at cursor in read-only mode"))
1720 (define-key map [menu-bar immediate display]
1721 '(menu-item "Display in Other Window" dired-display-file
1722 :help "Display file at cursor in other window"))
1723 (define-key map [menu-bar immediate find-file-other-window]
1724 '(menu-item "Find in Other Window" dired-find-file-other-window
1725 :help "Edit file at cursor in other window"))
1726 (define-key map [menu-bar immediate find-file]
1727 '(menu-item "Find This File" dired-find-file
1728 :help "Edit file at cursor"))
1729 (define-key map [menu-bar immediate create-directory]
1730 '(menu-item "Create Directory..." dired-create-directory
1731 :help "Create a directory"))
1732 (define-key map [menu-bar immediate wdired-mode]
1733 '(menu-item "Edit File Names" wdired-change-to-wdired-mode
1734 :help "Put a Dired buffer in a mode in which filenames are editable"
1735 :keys "C-x C-q"
1736 :filter (lambda (x) (if (eq major-mode 'dired-mode) x))))
1738 (define-key map [menu-bar regexp]
1739 (cons "Regexp" (make-sparse-keymap "Regexp")))
1741 (define-key map
1742 [menu-bar regexp image-dired-mark-tagged-files]
1743 '(menu-item "Mark From Image Tag..." image-dired-mark-tagged-files
1744 :help "Mark files whose image tags matches regexp"))
1746 (define-key map [menu-bar regexp dashes-1]
1747 '("--"))
1749 (define-key map [menu-bar regexp downcase]
1750 '(menu-item "Downcase" dired-downcase
1751 ;; When running on plain MS-DOS, there's only one
1752 ;; letter-case for file names.
1753 :enable (or (not (fboundp 'msdos-long-file-names))
1754 (msdos-long-file-names))
1755 :help "Rename marked files to lower-case name"))
1756 (define-key map [menu-bar regexp upcase]
1757 '(menu-item "Upcase" dired-upcase
1758 :enable (or (not (fboundp 'msdos-long-file-names))
1759 (msdos-long-file-names))
1760 :help "Rename marked files to upper-case name"))
1761 (define-key map [menu-bar regexp hardlink]
1762 '(menu-item "Hardlink..." dired-do-hardlink-regexp
1763 :help "Make hard links for files matching regexp"))
1764 (define-key map [menu-bar regexp symlink]
1765 '(menu-item "Symlink..." dired-do-symlink-regexp
1766 :visible (fboundp 'make-symbolic-link)
1767 :help "Make symbolic links for files matching regexp"))
1768 (define-key map [menu-bar regexp rename]
1769 '(menu-item "Rename..." dired-do-rename-regexp
1770 :help "Rename marked files matching regexp"))
1771 (define-key map [menu-bar regexp copy]
1772 '(menu-item "Copy..." dired-do-copy-regexp
1773 :help "Copy marked files matching regexp"))
1774 (define-key map [menu-bar regexp flag]
1775 '(menu-item "Flag..." dired-flag-files-regexp
1776 :help "Flag files matching regexp for deletion"))
1777 (define-key map [menu-bar regexp mark]
1778 '(menu-item "Mark..." dired-mark-files-regexp
1779 :help "Mark files matching regexp for future operations"))
1780 (define-key map [menu-bar regexp mark-cont]
1781 '(menu-item "Mark Containing..." dired-mark-files-containing-regexp
1782 :help "Mark files whose contents matches regexp"))
1784 (define-key map [menu-bar mark]
1785 (cons "Mark" (make-sparse-keymap "Mark")))
1787 (define-key map [menu-bar mark prev]
1788 '(menu-item "Previous Marked" dired-prev-marked-file
1789 :help "Move to previous marked file"))
1790 (define-key map [menu-bar mark next]
1791 '(menu-item "Next Marked" dired-next-marked-file
1792 :help "Move to next marked file"))
1793 (define-key map [menu-bar mark marks]
1794 '(menu-item "Change Marks..." dired-change-marks
1795 :help "Replace marker with another character"))
1796 (define-key map [menu-bar mark unmark-all]
1797 '(menu-item "Unmark All" dired-unmark-all-marks))
1798 (define-key map [menu-bar mark symlinks]
1799 '(menu-item "Mark Symlinks" dired-mark-symlinks
1800 :visible (fboundp 'make-symbolic-link)
1801 :help "Mark all symbolic links"))
1802 (define-key map [menu-bar mark directories]
1803 '(menu-item "Mark Directories" dired-mark-directories
1804 :help "Mark all directories except `.' and `..'"))
1805 (define-key map [menu-bar mark directory]
1806 '(menu-item "Mark Old Backups" dired-clean-directory
1807 :help "Flag old numbered backups for deletion"))
1808 (define-key map [menu-bar mark executables]
1809 '(menu-item "Mark Executables" dired-mark-executables
1810 :help "Mark all executable files"))
1811 (define-key map [menu-bar mark garbage-files]
1812 '(menu-item "Flag Garbage Files" dired-flag-garbage-files
1813 :help "Flag unneeded files for deletion"))
1814 (define-key map [menu-bar mark backup-files]
1815 '(menu-item "Flag Backup Files" dired-flag-backup-files
1816 :help "Flag all backup files for deletion"))
1817 (define-key map [menu-bar mark auto-save-files]
1818 '(menu-item "Flag Auto-save Files" dired-flag-auto-save-files
1819 :help "Flag auto-save files for deletion"))
1820 (define-key map [menu-bar mark deletion]
1821 '(menu-item "Flag" dired-flag-file-deletion
1822 :help "Flag current line's file for deletion"))
1823 (define-key map [menu-bar mark unmark]
1824 '(menu-item "Unmark" dired-unmark
1825 :help "Unmark or unflag current line's file"))
1826 (define-key map [menu-bar mark mark]
1827 '(menu-item "Mark" dired-mark
1828 :help "Mark current line's file for future operations"))
1829 (define-key map [menu-bar mark toggle-marks]
1830 '(menu-item "Toggle Marks" dired-toggle-marks
1831 :help "Mark unmarked files, unmark marked ones"))
1833 (define-key map [menu-bar operate]
1834 (cons "Operate" (make-sparse-keymap "Operate")))
1836 (define-key map
1837 [menu-bar operate image-dired-delete-tag]
1838 '(menu-item "Delete Image Tag..." image-dired-delete-tag
1839 :help "Delete image tag from current or marked files"))
1840 (define-key map
1841 [menu-bar operate image-dired-tag-files]
1842 '(menu-item "Add Image Tags..." image-dired-tag-files
1843 :help "Add image tags to current or marked files"))
1844 (define-key map
1845 [menu-bar operate image-dired-dired-comment-files]
1846 '(menu-item "Add Image Comment..." image-dired-dired-comment-files
1847 :help "Add image comment to current or marked files"))
1848 (define-key map
1849 [menu-bar operate image-dired-display-thumbs]
1850 '(menu-item "Display Image Thumbnails" image-dired-display-thumbs
1851 :help "Display image thumbnails for current or marked image files"))
1853 (define-key map [menu-bar operate dashes-4]
1854 '("--"))
1856 (define-key map
1857 [menu-bar operate epa-dired-do-decrypt]
1858 '(menu-item "Decrypt..." epa-dired-do-decrypt
1859 :help "Decrypt current or marked files"))
1861 (define-key map
1862 [menu-bar operate epa-dired-do-verify]
1863 '(menu-item "Verify" epa-dired-do-verify
1864 :help "Verify digital signature of current or marked files"))
1866 (define-key map
1867 [menu-bar operate epa-dired-do-sign]
1868 '(menu-item "Sign..." epa-dired-do-sign
1869 :help "Create digital signature of current or marked files"))
1871 (define-key map
1872 [menu-bar operate epa-dired-do-encrypt]
1873 '(menu-item "Encrypt..." epa-dired-do-encrypt
1874 :help "Encrypt current or marked files"))
1876 (define-key map [menu-bar operate dashes-3]
1877 '("--"))
1879 (define-key map [menu-bar operate query-replace]
1880 '(menu-item "Query Replace in Files..." dired-do-find-regexp-and-replace
1881 :help "Replace regexp matches in marked files"))
1882 (define-key map [menu-bar operate search]
1883 '(menu-item "Search Files..." dired-do-find-regexp
1884 :help "Search marked files for matches of regexp"))
1885 (define-key map [menu-bar operate isearch-regexp]
1886 '(menu-item "Isearch Regexp Files..." dired-do-isearch-regexp
1887 :help "Incrementally search marked files for regexp"))
1888 (define-key map [menu-bar operate isearch]
1889 '(menu-item "Isearch Files..." dired-do-isearch
1890 :help "Incrementally search marked files for string"))
1891 (define-key map [menu-bar operate chown]
1892 '(menu-item "Change Owner..." dired-do-chown
1893 :visible (not (memq system-type '(ms-dos windows-nt)))
1894 :help "Change the owner of marked files"))
1895 (define-key map [menu-bar operate chgrp]
1896 '(menu-item "Change Group..." dired-do-chgrp
1897 :visible (not (memq system-type '(ms-dos windows-nt)))
1898 :help "Change the group of marked files"))
1899 (define-key map [menu-bar operate chmod]
1900 '(menu-item "Change Mode..." dired-do-chmod
1901 :help "Change mode (attributes) of marked files"))
1902 (define-key map [menu-bar operate touch]
1903 '(menu-item "Change Timestamp..." dired-do-touch
1904 :help "Change timestamp of marked files"))
1905 (define-key map [menu-bar operate load]
1906 '(menu-item "Load" dired-do-load
1907 :help "Load marked Emacs Lisp files"))
1908 (define-key map [menu-bar operate compile]
1909 '(menu-item "Byte-compile" dired-do-byte-compile
1910 :help "Byte-compile marked Emacs Lisp files"))
1911 (define-key map [menu-bar operate compress]
1912 '(menu-item "Compress" dired-do-compress
1913 :help "Compress/uncompress marked files"))
1914 (define-key map [menu-bar operate print]
1915 '(menu-item "Print..." dired-do-print
1916 :help "Ask for print command and print marked files"))
1917 (define-key map [menu-bar operate hardlink]
1918 '(menu-item "Hardlink to..." dired-do-hardlink
1919 :help "Make hard links for current or marked files"))
1920 (define-key map [menu-bar operate symlink]
1921 '(menu-item "Symlink to..." dired-do-symlink
1922 :visible (fboundp 'make-symbolic-link)
1923 :help "Make symbolic links for current or marked files"))
1924 (define-key map [menu-bar operate async-command]
1925 '(menu-item "Asynchronous Shell Command..." dired-do-async-shell-command
1926 :help "Run a shell command asynchronously on current or marked files"))
1927 (define-key map [menu-bar operate command]
1928 '(menu-item "Shell Command..." dired-do-shell-command
1929 :help "Run a shell command on current or marked files"))
1930 (define-key map [menu-bar operate delete]
1931 '(menu-item "Delete" dired-do-delete
1932 :help "Delete current file or all marked files"))
1933 (define-key map [menu-bar operate rename]
1934 '(menu-item "Rename to..." dired-do-rename
1935 :help "Rename current file or move marked files"))
1936 (define-key map [menu-bar operate copy]
1937 '(menu-item "Copy to..." dired-do-copy
1938 :help "Copy current file or all marked files"))
1940 map)
1941 "Local keymap for Dired mode buffers.")
1943 ;; Dired mode is suitable only for specially formatted data.
1944 (put 'dired-mode 'mode-class 'special)
1946 ;; Autoload cookie needed by desktop.el
1947 ;;;###autoload
1948 (defun dired-mode (&optional dirname switches)
1950 Mode for \"editing\" directory listings.
1951 In Dired, you are \"editing\" a list of the files in a directory and
1952 (optionally) its subdirectories, in the format of `ls -lR'.
1953 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
1954 \"Editing\" means that you can run shell commands on files, visit,
1955 compress, load or byte-compile them, change their file attributes
1956 and insert subdirectories into the same buffer. You can \"mark\"
1957 files for later commands or \"flag\" them for deletion, either file
1958 by file or all files matching certain criteria.
1959 You can move using the usual cursor motion commands.\\<dired-mode-map>
1960 The buffer is read-only. Digits are prefix arguments.
1961 Type \\[dired-flag-file-deletion] to flag a file `D' for deletion.
1962 Type \\[dired-mark] to Mark a file or subdirectory for later commands.
1963 Most commands operate on the marked files and use the current file
1964 if no files are marked. Use a numeric prefix argument to operate on
1965 the next ARG (or previous -ARG if ARG<0) files, or just `1'
1966 to operate on the current file only. Prefix arguments override marks.
1967 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
1968 to see why something went wrong.
1969 Type \\[dired-unmark] to Unmark a file or all files of an inserted subdirectory.
1970 Type \\[dired-unmark-backward] to back up one line and unmark or unflag.
1971 Type \\[dired-do-flagged-delete] to delete (eXpunge) the files flagged `D'.
1972 Type \\[dired-find-file] to Find the current line's file
1973 (or dired it in another buffer, if it is a directory).
1974 Type \\[dired-find-file-other-window] to find file or Dired directory in Other window.
1975 Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
1976 Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
1977 Type \\[dired-do-copy] to Copy files.
1978 Type \\[dired-sort-toggle-or-edit] to toggle Sorting by name/date or change the `ls' switches.
1979 Type \\[revert-buffer] to read all currently expanded directories aGain.
1980 This retains all marks and hides subdirs again that were hidden before.
1981 Use `SPC' and `DEL' to move down and up by lines.
1983 If Dired ever gets confused, you can either type \\[revert-buffer] \
1984 to read the
1985 directories again, type \\[dired-do-redisplay] \
1986 to relist the file at point or the marked files or a
1987 subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
1988 again for the directory tree.
1990 Customization variables (rename this buffer and type \\[describe-variable] on each line
1991 for more info):
1993 `dired-listing-switches'
1994 `dired-trivial-filenames'
1995 `dired-marker-char'
1996 `dired-del-marker'
1997 `dired-keep-marker-rename'
1998 `dired-keep-marker-copy'
1999 `dired-keep-marker-hardlink'
2000 `dired-keep-marker-symlink'
2002 Hooks (use \\[describe-variable] to see their documentation):
2004 `dired-before-readin-hook'
2005 `dired-after-readin-hook'
2006 `dired-mode-hook'
2007 `dired-load-hook'
2009 Keybindings:
2010 \\{dired-mode-map}"
2011 ;; Not to be called interactively (e.g. dired-directory will be set
2012 ;; to default-directory, which is wrong with wildcards).
2013 (kill-all-local-variables)
2014 (use-local-map dired-mode-map)
2015 (dired-advertise) ; default-directory is already set
2016 (setq major-mode 'dired-mode
2017 mode-name "Dired"
2018 ;; case-fold-search nil
2019 buffer-read-only t
2020 selective-display t ; for subdirectory hiding
2021 mode-line-buffer-identification
2022 (propertized-buffer-identification "%17b"))
2023 ;; Ignore dired-hide-details-* value of invisible text property by default.
2024 (when (eq buffer-invisibility-spec t)
2025 (setq buffer-invisibility-spec (list t)))
2026 (setq-local revert-buffer-function #'dired-revert)
2027 (setq-local buffer-stale-function #'dired-buffer-stale-p)
2028 (setq-local page-delimiter "\n\n")
2029 (setq-local dired-directory (or dirname default-directory))
2030 ;; list-buffers uses this to display the dir being edited in this buffer.
2031 (setq list-buffers-directory
2032 (expand-file-name (if (listp dired-directory)
2033 (car dired-directory)
2034 dired-directory)))
2035 (setq-local dired-actual-switches (or switches dired-listing-switches))
2036 (setq-local font-lock-defaults
2037 '(dired-font-lock-keywords t nil nil beginning-of-line))
2038 (setq-local desktop-save-buffer 'dired-desktop-buffer-misc-data)
2039 (setq dired-switches-alist nil)
2040 (hack-dir-local-variables-non-file-buffer) ; before sorting
2041 (dired-sort-other dired-actual-switches t)
2042 (when (featurep 'dnd)
2043 (setq-local dnd-protocol-alist
2044 (append dired-dnd-protocol-alist dnd-protocol-alist)))
2045 (add-hook 'file-name-at-point-functions 'dired-file-name-at-point nil t)
2046 (add-hook 'isearch-mode-hook 'dired-isearch-filenames-setup nil t)
2047 (run-mode-hooks 'dired-mode-hook))
2049 ;; Idiosyncratic dired commands that don't deal with marks.
2051 (defun dired-summary ()
2052 "Summarize basic Dired commands and show recent Dired errors."
2053 (interactive)
2054 (dired-why)
2055 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
2056 (message
2057 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
2059 (defun dired-undo ()
2060 "Undo in a Dired buffer.
2061 This doesn't recover lost files, it just undoes changes in the buffer itself.
2062 You can use it to recover marks, killed lines or subdirs."
2063 (interactive)
2064 (let ((inhibit-read-only t))
2065 (undo))
2066 (dired-build-subdir-alist)
2067 (message "Change in Dired buffer undone.
2068 Actual changes in files cannot be undone by Emacs."))
2070 (defun dired-toggle-read-only ()
2071 "Edit Dired buffer with Wdired, or make it read-only.
2072 If the current buffer can be edited with Wdired, (i.e. the major
2073 mode is `dired-mode'), call `wdired-change-to-wdired-mode'.
2074 Otherwise, toggle `read-only-mode'."
2075 (interactive)
2076 (if (derived-mode-p 'dired-mode)
2077 (wdired-change-to-wdired-mode)
2078 (read-only-mode 'toggle)))
2080 (defun dired-next-line (arg)
2081 "Move down lines then position at filename.
2082 Optional prefix ARG says how many lines to move; default is one line."
2083 (interactive "^p")
2084 (let ((line-move-visual)
2085 (goal-column))
2086 (line-move arg t))
2087 ;; We never want to move point into an invisible line.
2088 (while (and (invisible-p (point))
2089 (not (if (and arg (< arg 0)) (bobp) (eobp))))
2090 (forward-char (if (and arg (< arg 0)) -1 1)))
2091 (dired-move-to-filename))
2093 (defun dired-previous-line (arg)
2094 "Move up lines then position at filename.
2095 Optional prefix ARG says how many lines to move; default is one line."
2096 (interactive "^p")
2097 (dired-next-line (- (or arg 1))))
2099 (defun dired-next-dirline (arg &optional opoint)
2100 "Goto ARGth next directory file line."
2101 (interactive "p")
2102 (or opoint (setq opoint (point)))
2103 (if (if (> arg 0)
2104 (re-search-forward dired-re-dir nil t arg)
2105 (beginning-of-line)
2106 (re-search-backward dired-re-dir nil t (- arg)))
2107 (dired-move-to-filename) ; user may type `i' or `f'
2108 (goto-char opoint)
2109 (error "No more subdirectories")))
2111 (defun dired-prev-dirline (arg)
2112 "Goto ARGth previous directory file line."
2113 (interactive "p")
2114 (dired-next-dirline (- arg)))
2116 (defun dired-up-directory (&optional other-window)
2117 "Run Dired on parent directory of current directory.
2118 Find the parent directory either in this buffer or another buffer.
2119 Creates a buffer if necessary.
2120 If OTHER-WINDOW (the optional prefix arg), display the parent
2121 directory in another window."
2122 (interactive "P")
2123 (let* ((dir (dired-current-directory))
2124 (up (file-name-directory (directory-file-name dir))))
2125 (or (dired-goto-file (directory-file-name dir))
2126 ;; Only try dired-goto-subdir if buffer has more than one dir.
2127 (and (cdr dired-subdir-alist)
2128 (dired-goto-subdir up))
2129 (progn
2130 (if other-window
2131 (dired-other-window up)
2132 (dired up))
2133 (dired-goto-file dir)))))
2135 (defun dired-get-file-for-visit ()
2136 "Get the current line's file name, with an error if file does not exist."
2137 (interactive)
2138 ;; We pass t for second arg so that we don't get error for `.' and `..'.
2139 (let ((raw (dired-get-filename nil t))
2140 file-name)
2141 (if (null raw)
2142 (error "No file on this line"))
2143 (setq file-name (file-name-sans-versions raw t))
2144 (if (file-exists-p file-name)
2145 file-name
2146 (if (file-symlink-p file-name)
2147 (error "File is a symlink to a nonexistent target")
2148 (error "File no longer exists; type `g' to update Dired buffer")))))
2150 ;; Force C-m keybinding rather than `f' or `e' in the mode doc:
2151 (define-obsolete-function-alias 'dired-advertised-find-file 'dired-find-file "23.2")
2152 (defun dired-find-file ()
2153 "In Dired, visit the file or directory named on this line."
2154 (interactive)
2155 ;; Bind `find-file-run-dired' so that the command works on directories
2156 ;; too, independent of the user's setting.
2157 (let ((find-file-run-dired t)
2158 ;; This binding prevents problems with preserving point in
2159 ;; windows displaying Dired buffers, because reverting a Dired
2160 ;; buffer empties it, which changes the places where the
2161 ;; markers used by switch-to-buffer-preserve-window-point
2162 ;; point.
2163 (switch-to-buffer-preserve-window-point
2164 (if dired-auto-revert-buffer
2166 switch-to-buffer-preserve-window-point)))
2167 (find-file (dired-get-file-for-visit))))
2169 (defun dired-find-alternate-file ()
2170 "In Dired, visit this file or directory instead of the Dired buffer."
2171 (interactive)
2172 (set-buffer-modified-p nil)
2173 (find-alternate-file (dired-get-file-for-visit)))
2174 ;; Don't override the setting from .emacs.
2175 ;;;###autoload (put 'dired-find-alternate-file 'disabled t)
2177 (defun dired-mouse-find-file-other-window (event)
2178 "In Dired, visit the file or directory name you click on."
2179 (interactive "e")
2180 (let (window pos file)
2181 (save-excursion
2182 (setq window (posn-window (event-end event))
2183 pos (posn-point (event-end event)))
2184 (if (not (windowp window))
2185 (error "No file chosen"))
2186 (set-buffer (window-buffer window))
2187 (goto-char pos)
2188 (setq file (dired-get-file-for-visit)))
2189 (if (file-directory-p file)
2190 (or (and (cdr dired-subdir-alist)
2191 (dired-goto-subdir file))
2192 (progn
2193 (select-window window)
2194 (dired-other-window file)))
2195 (select-window window)
2196 (find-file-other-window (file-name-sans-versions file t)))))
2198 (defun dired-view-file ()
2199 "In Dired, examine a file in view mode, returning to Dired when done.
2200 When file is a directory, show it in this buffer if it is inserted.
2201 Otherwise, display it in another buffer."
2202 (interactive)
2203 (let ((file (dired-get-file-for-visit)))
2204 (if (file-directory-p file)
2205 (or (and (cdr dired-subdir-alist)
2206 (dired-goto-subdir file))
2207 (dired file))
2208 (view-file file))))
2210 (defun dired-find-file-other-window ()
2211 "In Dired, visit this file or directory in another window."
2212 (interactive)
2213 (find-file-other-window (dired-get-file-for-visit)))
2215 (defun dired-display-file ()
2216 "In Dired, display this file or directory in another window."
2217 (interactive)
2218 (display-buffer (find-file-noselect (dired-get-file-for-visit))
2221 ;;; Functions for extracting and manipulating file names in Dired buffers.
2223 (defun dired-get-filename (&optional localp no-error-if-not-filep)
2224 "In Dired, return name of file mentioned on this line.
2225 Value returned normally includes the directory name.
2226 Optional arg LOCALP with value `no-dir' means don't include directory
2227 name in result. A value of `verbatim' means to return the name exactly as
2228 it occurs in the buffer, and a value of t means construct name relative to
2229 `default-directory', which still may contain slashes if in a subdirectory.
2230 Optional arg NO-ERROR-IF-NOT-FILEP means treat `.' and `..' as
2231 regular filenames and return nil if no filename on this line.
2232 Otherwise, an error occurs in these cases."
2233 (let (case-fold-search file p1 p2 already-absolute)
2234 (save-excursion
2235 (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
2236 (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
2237 ;; nil if no file on this line, but no-error-if-not-filep is t:
2238 (if (setq file (and p1 p2 (buffer-substring p1 p2)))
2239 (progn
2240 ;; Get rid of the mouse-face property that file names have.
2241 (set-text-properties 0 (length file) nil file)
2242 ;; Unquote names quoted by ls or by dired-insert-directory.
2243 ;; This code was written using `read' to unquote, because
2244 ;; it's faster than substituting \007 (4 chars) -> ^G (1
2245 ;; char) etc. in a lisp loop. Unfortunately, this decision
2246 ;; has necessitated hacks such as dealing with filenames
2247 ;; with quotation marks in their names.
2248 (while (string-match "\\(?:[^\\]\\|\\`\\)\\(\"\\)" file)
2249 (setq file (replace-match "\\\"" nil t file 1)))
2250 ;; Unescape any spaces escaped by ls -b (bug#10469).
2251 ;; Other -b quotes, eg \t, \n, work transparently.
2252 (if (dired-switches-escape-p dired-actual-switches)
2253 (let ((start 0)
2254 (rep "")
2255 (shift -1))
2256 (if (eq localp 'verbatim)
2257 (setq rep "\\\\"
2258 shift +1))
2259 (while (string-match "\\(\\\\\\) " file start)
2260 (setq file (replace-match rep nil t file 1)
2261 start (+ shift (match-end 0))))))
2262 (when (eq system-type 'windows-nt)
2263 (save-match-data
2264 (let ((start 0))
2265 (while (string-match "\\\\" file start)
2266 (aset file (match-beginning 0) ?/)
2267 (setq start (match-end 0))))))
2269 ;; Hence we don't need to worry about converting `\\' back to `\'.
2270 (setq file (read (concat "\"" file "\"")))
2271 ;; The above `read' will return a unibyte string if FILE
2272 ;; contains eight-bit-control/graphic characters.
2273 (if (and enable-multibyte-characters
2274 (not (multibyte-string-p file)))
2275 (setq file (string-to-multibyte file)))))
2276 (and file (file-name-absolute-p file)
2277 ;; A relative file name can start with ~.
2278 ;; Don't treat it as absolute in this context.
2279 (not (eq (aref file 0) ?~))
2280 (setq already-absolute t))
2281 (cond
2282 ((null file)
2283 nil)
2284 ((eq localp 'verbatim)
2285 file)
2286 ((and (not no-error-if-not-filep)
2287 (member file '("." "..")))
2288 (error "Cannot operate on `.' or `..'"))
2289 ((and (eq localp 'no-dir) already-absolute)
2290 (file-name-nondirectory file))
2291 (already-absolute
2292 (let ((handler (find-file-name-handler file nil)))
2293 ;; check for safe-magic property so that we won't
2294 ;; put /: for names that don't really need them.
2295 ;; For instance, .gz files when auto-compression-mode is on.
2296 (if (and handler (not (get handler 'safe-magic)))
2297 (concat "/:" file)
2298 file)))
2299 ((eq localp 'no-dir)
2300 file)
2301 ((equal (dired-current-directory) "/")
2302 (setq file (concat (dired-current-directory localp) file))
2303 (let ((handler (find-file-name-handler file nil)))
2304 ;; check for safe-magic property so that we won't
2305 ;; put /: for names that don't really need them.
2306 ;; For instance, .gz files when auto-compression-mode is on.
2307 (if (and handler (not (get handler 'safe-magic)))
2308 (concat "/:" file)
2309 file)))
2311 (concat (dired-current-directory localp) file)))))
2313 (defun dired-string-replace-match (regexp string newtext
2314 &optional literal global)
2315 "Replace first match of REGEXP in STRING with NEWTEXT.
2316 If it does not match, nil is returned instead of the new string.
2317 Optional arg LITERAL means to take NEWTEXT literally.
2318 Optional arg GLOBAL means to replace all matches."
2319 (if global
2320 (let ((start 0) ret)
2321 (while (string-match regexp string start)
2322 (let ((from-end (- (length string) (match-end 0))))
2323 (setq ret (setq string (replace-match newtext t literal string)))
2324 (setq start (- (length string) from-end))))
2325 ret)
2326 (if (not (string-match regexp string 0))
2328 (replace-match newtext t literal string))))
2330 (defun dired-make-absolute (file &optional dir)
2331 ;;"Convert FILE (a file name relative to DIR) to an absolute file name."
2332 ;; We can't always use expand-file-name as this would get rid of `.'
2333 ;; or expand in / instead default-directory if DIR=="".
2334 ;; This should be good enough for ange-ftp.
2335 ;; It should be reasonably fast, though, as it is called in
2336 ;; dired-get-filename.
2337 (concat (or dir default-directory) file))
2339 (defun dired-make-relative (file &optional dir)
2340 "Convert FILE (an absolute file name) to a name relative to DIR.
2341 If DIR is omitted or nil, it defaults to `default-directory'.
2342 If FILE is not in the directory tree of DIR, return FILE
2343 unchanged."
2344 (or dir (setq dir default-directory))
2345 ;; This case comes into play if default-directory is set to
2346 ;; use ~.
2347 (if (and (> (length dir) 0) (= (aref dir 0) ?~))
2348 (setq dir (expand-file-name dir)))
2349 (if (string-match (concat "^" (regexp-quote dir)) file)
2350 (substring file (match-end 0))
2351 file))
2353 (define-minor-mode dired-hide-details-mode
2354 "Toggle visibility of detailed information in current Dired buffer.
2355 When this minor mode is enabled, details such as file ownership and
2356 permissions are hidden from view.
2358 See options: `dired-hide-details-hide-symlink-targets' and
2359 `dired-hide-details-hide-information-lines'."
2360 :group 'dired
2361 (unless (derived-mode-p 'dired-mode)
2362 (error "Not a Dired buffer"))
2363 (dired-hide-details-update-invisibility-spec)
2364 (if dired-hide-details-mode
2365 (add-hook 'wdired-mode-hook
2366 'dired-hide-details-update-invisibility-spec
2369 (remove-hook 'wdired-mode-hook
2370 'dired-hide-details-update-invisibility-spec
2371 t)))
2373 (defun dired-hide-details-update-invisibility-spec ()
2374 (funcall (if dired-hide-details-mode
2375 'add-to-invisibility-spec
2376 'remove-from-invisibility-spec)
2377 'dired-hide-details-detail)
2378 (funcall (if (and dired-hide-details-mode
2379 dired-hide-details-hide-information-lines)
2380 'add-to-invisibility-spec
2381 'remove-from-invisibility-spec)
2382 'dired-hide-details-information)
2383 (funcall (if (and dired-hide-details-mode
2384 dired-hide-details-hide-symlink-targets
2385 (not (derived-mode-p 'wdired-mode)))
2386 'add-to-invisibility-spec
2387 'remove-from-invisibility-spec)
2388 'dired-hide-details-link))
2390 ;;; Functions for finding the file name in a dired buffer line.
2392 (defvar dired-permission-flags-regexp
2393 "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
2394 "Regular expression to match the permission flags in `ls -l'.")
2396 ;; Move to first char of filename on this line.
2397 ;; Returns position (point) or nil if no filename on this line."
2398 (defun dired-move-to-filename (&optional raise-error eol)
2399 "Move to the beginning of the filename on the current line.
2400 Return the position of the beginning of the filename, or nil if none found."
2401 ;; This is the UNIX version.
2402 (or eol (setq eol (line-end-position)))
2403 (beginning-of-line)
2404 ;; First try assuming `ls --dired' was used.
2405 (let ((change (next-single-property-change (point) 'dired-filename nil eol)))
2406 (cond
2407 ((and change (< change eol))
2408 (goto-char change))
2409 ((re-search-forward directory-listing-before-filename-regexp eol t)
2410 (goto-char (match-end 0)))
2411 ((re-search-forward dired-permission-flags-regexp eol t)
2412 ;; Ha! There *is* a file. Our regexp-from-hell just failed to find it.
2413 (if raise-error
2414 (error "Unrecognized line! Check directory-listing-before-filename-regexp"))
2415 (beginning-of-line)
2416 nil)
2417 (raise-error
2418 (error "No file on this line")))))
2420 (defun dired-move-to-end-of-filename (&optional no-error)
2421 ;; Assumes point is at beginning of filename,
2422 ;; thus the rwx bit re-search-backward below will succeed in *this*
2423 ;; line if at all. So, it should be called only after
2424 ;; (dired-move-to-filename t).
2425 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
2426 ;; This is the UNIX version.
2427 (if (get-text-property (point) 'dired-filename)
2428 (goto-char (next-single-property-change (point) 'dired-filename))
2429 (let (opoint file-type executable symlink hidden used-F eol)
2430 (setq used-F (dired-check-switches dired-actual-switches "F" "classify")
2431 opoint (point)
2432 eol (line-end-position)
2433 hidden (and selective-display
2434 (save-excursion (search-forward "\r" eol t))))
2435 (if hidden
2437 (save-excursion ;; Find out what kind of file this is:
2438 ;; Restrict perm bits to be non-blank,
2439 ;; otherwise this matches one char to early (looking backward):
2440 ;; "l---------" (some systems make symlinks that way)
2441 ;; "----------" (plain file with zero perms)
2442 (if (re-search-backward
2443 dired-permission-flags-regexp nil t)
2444 (setq file-type (char-after (match-beginning 1))
2445 symlink (eq file-type ?l)
2446 ;; Only with -F we need to know whether it's an executable
2447 executable (and
2448 used-F
2449 (string-match
2450 "[xst]" ;; execute bit set anywhere?
2451 (concat
2452 (match-string 2)
2453 (match-string 3)
2454 (match-string 4)))))
2455 (or no-error (error "No file on this line"))))
2456 ;; Move point to end of name:
2457 (if symlink
2458 (if (search-forward " -> " eol t)
2459 (progn
2460 (forward-char -4)
2461 (and used-F
2462 dired-ls-F-marks-symlinks
2463 (eq (preceding-char) ?@) ;; did ls really mark the link?
2464 (forward-char -1))))
2465 (goto-char eol) ;; else not a symbolic link
2466 ;; ls -lF marks dirs, sockets, fifos and executables with exactly
2467 ;; one trailing character. (Executable bits on symlinks ain't mean
2468 ;; a thing, even to ls, but we know it's not a symlink.)
2469 (and used-F
2470 (or (memq file-type '(?d ?s ?p))
2471 executable)
2472 (forward-char -1))))
2473 (or no-error
2474 (not (eq opoint (point)))
2475 (error "%s" (if hidden
2476 (substitute-command-keys
2477 "File line is hidden, type \\[dired-hide-subdir] to unhide")
2478 "No file on this line")))
2479 (if (eq opoint (point))
2481 (point)))))
2484 ;;; COPY NAMES OF MARKED FILES INTO KILL-RING.
2486 (defun dired-copy-filename-as-kill (&optional arg)
2487 "Copy names of marked (or next ARG) files into the kill ring.
2488 The names are separated by a space.
2489 With a zero prefix arg, use the absolute file name of each marked file.
2490 With \\[universal-argument], use the file name relative to the Dired buffer's
2491 `default-directory'. (This still may contain slashes if in a subdirectory.)
2493 If on a subdir headerline, use absolute subdirname instead;
2494 prefix arg and marked files are ignored in this case.
2496 You can then feed the file name(s) to other commands with \\[yank]."
2497 (interactive "P")
2498 (let ((string
2499 (or (dired-get-subdir)
2500 (mapconcat #'identity
2501 (if arg
2502 (cond ((zerop (prefix-numeric-value arg))
2503 (dired-get-marked-files))
2504 ((consp arg)
2505 (dired-get-marked-files t))
2507 (dired-get-marked-files
2508 'no-dir (prefix-numeric-value arg))))
2509 (dired-get-marked-files 'no-dir))
2510 " "))))
2511 (unless (string= string "")
2512 (if (eq last-command 'kill-region)
2513 (kill-append string nil)
2514 (kill-new string))
2515 (message "%s" string))))
2518 ;; Keeping Dired buffers in sync with the filesystem and with each other
2520 (defun dired-buffers-for-dir (dir &optional file)
2521 ;; Return a list of buffers for DIR (top level or in-situ subdir).
2522 ;; If FILE is non-nil, include only those whose wildcard pattern (if any)
2523 ;; matches FILE.
2524 ;; The list is in reverse order of buffer creation, most recent last.
2525 ;; As a side effect, killed dired buffers for DIR are removed from
2526 ;; dired-buffers.
2527 (setq dir (file-name-as-directory dir))
2528 (let (result buf)
2529 (dolist (elt dired-buffers)
2530 (setq buf (cdr elt))
2531 (cond
2532 ((null (buffer-name buf))
2533 ;; Buffer is killed - clean up:
2534 (setq dired-buffers (delq elt dired-buffers)))
2535 ((dired-in-this-tree dir (car elt))
2536 (with-current-buffer buf
2537 (and (assoc dir dired-subdir-alist)
2538 (or (null file)
2539 (if (stringp dired-directory)
2540 (let ((wildcards (file-name-nondirectory
2541 dired-directory)))
2542 (or (zerop (length wildcards))
2543 (string-match-p (dired-glob-regexp wildcards)
2544 file)))
2545 (member (expand-file-name file dir)
2546 (cdr dired-directory))))
2547 (setq result (cons buf result)))))))
2548 result))
2550 (defun dired-glob-regexp (pattern)
2551 "Convert glob-pattern PATTERN to a regular expression."
2552 (let ((matched-in-pattern 0) ;; How many chars of PATTERN we've handled.
2553 regexp)
2554 (while (string-match "[[?*]" pattern matched-in-pattern)
2555 (let ((op-end (match-end 0))
2556 (next-op (aref pattern (match-beginning 0))))
2557 (setq regexp (concat regexp
2558 (regexp-quote
2559 (substring pattern matched-in-pattern
2560 (match-beginning 0)))))
2561 (cond ((= next-op ??)
2562 (setq regexp (concat regexp "."))
2563 (setq matched-in-pattern op-end))
2564 ((= next-op ?\[)
2565 ;; Fails to handle ^ yet ????
2566 (let* ((set-start (match-beginning 0))
2567 (set-cont
2568 (if (= (aref pattern (1+ set-start)) ?^)
2569 (+ 3 set-start)
2570 (+ 2 set-start)))
2571 (set-end (string-match-p "]" pattern set-cont))
2572 (set (substring pattern set-start (1+ set-end))))
2573 (setq regexp (concat regexp set))
2574 (setq matched-in-pattern (1+ set-end))))
2575 ((= next-op ?*)
2576 (setq regexp (concat regexp ".*"))
2577 (setq matched-in-pattern op-end)))))
2578 (concat "\\`"
2579 regexp
2580 (regexp-quote
2581 (substring pattern matched-in-pattern))
2582 "\\'")))
2586 (defun dired-advertise ()
2587 ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
2588 ;; With wildcards we actually advertise too much.
2589 (let ((expanded-default (expand-file-name default-directory)))
2590 (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
2591 t ; we have already advertised ourselves
2592 (setq dired-buffers
2593 (cons (cons expanded-default (current-buffer))
2594 dired-buffers)))))
2596 (defun dired-unadvertise (dir)
2597 ;; Remove DIR from the buffer alist in variable dired-buffers.
2598 ;; This has the effect of removing any buffer whose main directory is DIR.
2599 ;; It does not affect buffers in which DIR is a subdir.
2600 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
2601 (setq dired-buffers
2602 (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
2604 ;; Tree Dired
2606 ;;; utility functions
2608 (defun dired-in-this-tree (file dir)
2609 ;;"Is FILE part of the directory tree starting at DIR?"
2610 (let (case-fold-search)
2611 (string-match-p (concat "^" (regexp-quote dir)) file)))
2613 (defun dired-normalize-subdir (dir)
2614 ;; Prepend default-directory to DIR if relative file name.
2615 ;; dired-get-filename must be able to make a valid file name from a
2616 ;; file and its directory DIR.
2617 (file-name-as-directory
2618 (if (file-name-absolute-p dir)
2620 (expand-file-name dir default-directory))))
2622 (defun dired-get-subdir ()
2623 ;;"Return the subdir name on this line, or nil if not on a headerline."
2624 ;; Look up in the alist whether this is a headerline.
2625 (save-excursion
2626 (let ((cur-dir (dired-current-directory)))
2627 (beginning-of-line) ; alist stores b-o-l positions
2628 (and (zerop (- (point)
2629 (dired-get-subdir-min (assoc cur-dir
2630 dired-subdir-alist))))
2631 cur-dir))))
2633 ;; can't use macro, must be redefinable for other alist format in dired-nstd.
2634 (defalias 'dired-get-subdir-min 'cdr)
2636 (defun dired-get-subdir-max (elt)
2637 (save-excursion
2638 (goto-char (dired-get-subdir-min elt))
2639 (dired-subdir-max)))
2641 (defun dired-clear-alist ()
2642 (while dired-subdir-alist
2643 (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
2644 (setq dired-subdir-alist (cdr dired-subdir-alist))))
2646 (defun dired-subdir-index (dir)
2647 ;; Return an index into alist for use with nth
2648 ;; for the sake of subdir moving commands.
2649 (let (found (index 0) (alist dired-subdir-alist))
2650 (while alist
2651 (if (string= dir (car (car alist)))
2652 (setq alist nil found t)
2653 (setq alist (cdr alist) index (1+ index))))
2654 (if found index nil)))
2656 (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
2657 "Go to next subdirectory, regardless of level."
2658 ;; Use 0 arg to go to this directory's header line.
2659 ;; NO-SKIP prevents moving to end of header line, returning whatever
2660 ;; position was found in dired-subdir-alist.
2661 (interactive "p")
2662 (let ((this-dir (dired-current-directory))
2663 pos index)
2664 ;; nth with negative arg does not return nil but the first element
2665 (setq index (- (dired-subdir-index this-dir) arg))
2666 (setq pos (if (>= index 0)
2667 (dired-get-subdir-min (nth index dired-subdir-alist))))
2668 (if pos
2669 (progn
2670 (goto-char pos)
2671 (or no-skip (skip-chars-forward "^\n\r"))
2672 (point))
2673 (if no-error-if-not-found
2674 nil ; return nil if not found
2675 (error "%s directory" (if (> arg 0) "Last" "First"))))))
2677 (defun dired-build-subdir-alist (&optional switches)
2678 "Build `dired-subdir-alist' by parsing the buffer.
2679 Returns the new value of the alist.
2680 If optional arg SWITCHES is non-nil, use its value
2681 instead of `dired-actual-switches'."
2682 (interactive)
2683 (dired-clear-alist)
2684 (save-excursion
2685 (let* ((count 0)
2686 (inhibit-read-only t)
2687 (buffer-undo-list t)
2688 (switches (or switches dired-actual-switches))
2689 new-dir-name
2690 (R-ftp-base-dir-regex
2691 ;; Used to expand subdirectory names correctly in recursive
2692 ;; ange-ftp listings.
2693 (and (dired-switches-recursive-p switches)
2694 (string-match "\\`/.*:\\(/.*\\)" default-directory)
2695 (concat "\\`" (match-string 1 default-directory)))))
2696 (goto-char (point-min))
2697 (setq dired-subdir-alist nil)
2698 (while (re-search-forward dired-subdir-regexp nil t)
2699 ;; Avoid taking a file name ending in a colon
2700 ;; as a subdir name.
2701 (unless (save-excursion
2702 (goto-char (match-beginning 0))
2703 (beginning-of-line)
2704 (forward-char 2)
2705 (looking-at-p dired-re-perms))
2706 (save-excursion
2707 (goto-char (match-beginning 1))
2708 (setq new-dir-name
2709 (buffer-substring-no-properties (point) (match-end 1))
2710 new-dir-name
2711 (save-match-data
2712 (if (and R-ftp-base-dir-regex
2713 (not (string= new-dir-name default-directory))
2714 (string-match R-ftp-base-dir-regex new-dir-name))
2715 (concat default-directory
2716 (substring new-dir-name (match-end 0)))
2717 (expand-file-name new-dir-name))))
2718 (delete-region (point) (match-end 1))
2719 (insert new-dir-name))
2720 (setq count (1+ count))
2721 ;; Undo any escaping of newlines and \ by dired-insert-directory.
2722 ;; Convert "n" preceded by odd number of \ to newline, and \\ to \.
2723 (when (and (dired-switches-escape-p switches)
2724 (string-match-p "\\\\" new-dir-name))
2725 (let (temp res)
2726 (mapc (lambda (char)
2727 (cond ((equal char ?\\)
2728 (if temp
2729 (setq res (concat res "\\")
2730 temp nil)
2731 (setq temp "\\")))
2732 ((and temp (equal char ?n))
2733 (setq res (concat res "\n")
2734 temp nil))
2736 (setq res (concat res temp (char-to-string char))
2737 temp nil))))
2738 new-dir-name)
2739 (setq new-dir-name res)))
2740 (dired-alist-add-1 new-dir-name
2741 ;; Place a sub directory boundary between lines.
2742 (save-excursion
2743 (goto-char (match-beginning 0))
2744 (beginning-of-line)
2745 (point-marker)))))
2746 (if (and (> count 1) (called-interactively-p 'interactive))
2747 (message "Buffer includes %d directories" count)))
2748 ;; We don't need to sort it because it is in buffer order per
2749 ;; constructionem. Return new alist:
2750 dired-subdir-alist))
2752 (defun dired-alist-add-1 (dir new-marker)
2753 ;; Add new DIR at NEW-MARKER. Don't sort.
2754 (setq dired-subdir-alist
2755 (cons (cons (dired-normalize-subdir dir) new-marker)
2756 dired-subdir-alist)))
2758 (defun dired-goto-next-nontrivial-file ()
2759 ;; Position point on first nontrivial file after point.
2760 (dired-goto-next-file);; so there is a file to compare with
2761 (if (stringp dired-trivial-filenames)
2762 (while (and (not (eobp))
2763 (string-match-p dired-trivial-filenames
2764 (file-name-nondirectory
2765 (or (dired-get-filename nil t) ""))))
2766 (forward-line 1)
2767 (dired-move-to-filename))))
2769 (defun dired-goto-next-file ()
2770 (let ((max (1- (dired-subdir-max))))
2771 (while (and (not (dired-move-to-filename)) (< (point) max))
2772 (forward-line 1))))
2774 (defun dired-goto-file (file)
2775 "Go to line describing file FILE in this Dired buffer."
2776 ;; Return value of point on success, else nil.
2777 ;; FILE must be an absolute file name.
2778 ;; Loses if FILE contains control chars like "\007" for which ls
2779 ;; either inserts "?" or "\\007" into the buffer, so we won't find
2780 ;; it in the buffer.
2781 (interactive
2782 (prog1 ; let push-mark display its message
2783 (list (expand-file-name
2784 (read-file-name "Goto file: "
2785 (dired-current-directory))))
2786 (push-mark)))
2787 (unless (file-name-absolute-p file)
2788 (error "File name `%s' is not absolute" file))
2789 (setq file (directory-file-name file)) ; does no harm if not a directory
2790 (let* ((case-fold-search nil)
2791 (dir (file-name-directory file))
2792 (found (or
2793 ;; First, look for a listing under the absolute name.
2794 (save-excursion
2795 (goto-char (point-min))
2796 (dired-goto-file-1 file file (point-max)))
2797 ;; Next, look for it as a relative name with leading
2798 ;; subdirectories. (This happens in Dired buffers
2799 ;; created by find-dired, for example.)
2800 (save-excursion
2801 (goto-char (point-min))
2802 (dired-goto-file-1 (file-relative-name file
2803 default-directory)
2804 file (point-max)))
2805 ;; Otherwise, look for it as a relative name, a base
2806 ;; name only. The hair is to get the result of
2807 ;; `dired-goto-subdir' without calling it if we don't
2808 ;; have any subdirs.
2809 (save-excursion
2810 (when (if (string= dir (expand-file-name default-directory))
2811 (goto-char (point-min))
2812 (and (cdr dired-subdir-alist)
2813 (dired-goto-subdir dir)))
2814 (dired-goto-file-1 (file-name-nondirectory file)
2815 file
2816 (dired-subdir-max)))))))
2817 ;; Return buffer position, if found.
2818 (if found
2819 (goto-char found))))
2821 (defun dired-goto-file-1 (file full-name limit)
2822 "Advance to the Dired listing labeled by FILE; return its position.
2823 Return nil if the listing is not found. If FILE contains
2824 characters that would not appear in a Dired buffer, search using
2825 the quoted forms of those characters.
2827 FULL-NAME specifies the actual file name the listing must have,
2828 as returned by `dired-get-filename'. LIMIT is the search limit."
2829 (let (str)
2830 (setq str (replace-regexp-in-string "\^m" "\\^m" file nil t))
2831 (setq str (replace-regexp-in-string "\\\\" "\\\\" str nil t))
2832 (and (dired-switches-escape-p dired-actual-switches)
2833 (string-match-p "[ \t\n]" str)
2834 ;; FIXME: to fix this for embedded control characters etc, we
2835 ;; should escape everything that `ls -b' does.
2836 (setq str (replace-regexp-in-string " " "\\ " str nil t)
2837 str (replace-regexp-in-string "\t" "\\t" str nil t)
2838 str (replace-regexp-in-string "\n" "\\n" str nil t)))
2839 (let ((found nil)
2840 ;; filenames are preceded by SPC, this makes the search faster
2841 ;; (e.g. for the filename "-").
2842 (search-string (concat " " str)))
2843 (while (and (not found)
2844 (search-forward search-string limit 'move))
2845 ;; Check that we are in the right place. Match could have
2846 ;; BASE just as initial substring or in permission bits etc.
2847 (if (equal full-name (dired-get-filename nil t))
2848 (setq found (dired-move-to-filename))
2849 (forward-line 1)))
2850 found)))
2852 (defvar dired-find-subdir)
2854 ;; FIXME document whatever dired-x is doing.
2855 (defun dired-initial-position (dirname)
2856 "Where point should go in a new listing of DIRNAME.
2857 Point is assumed to be at the beginning of new subdir line.
2858 It runs the hook `dired-initial-position-hook'."
2859 (end-of-line)
2860 (and (featurep 'dired-x) dired-find-subdir
2861 (dired-goto-subdir dirname))
2862 (if dired-trivial-filenames (dired-goto-next-nontrivial-file))
2863 (run-hooks 'dired-initial-position-hook))
2865 ;; These are hooks which make tree dired work.
2866 ;; They are in this file because other parts of dired need to call them.
2867 ;; But they don't call the rest of tree dired unless there are subdirs loaded.
2869 ;; This function is called for each retrieved filename.
2870 ;; It could stand to be faster, though it's mostly function call
2871 ;; overhead. Avoiding the function call seems to save about 10% in
2872 ;; dired-get-filename. Make it a defsubst?
2873 (defun dired-current-directory (&optional localp)
2874 "Return the name of the subdirectory to which this line belongs.
2875 This returns a string with trailing slash, like `default-directory'.
2876 Optional argument means return a file name relative to `default-directory',
2877 in which case the value could be an empty string if `default-directory'
2878 is the directory where the file on this line resides."
2879 (let ((here (point))
2880 (alist (or dired-subdir-alist
2881 ;; probably because called in a non-dired buffer
2882 (error "No subdir-alist in %s" (current-buffer))))
2883 elt dir)
2884 (while alist
2885 (setq elt (car alist)
2886 dir (car elt)
2887 ;; use `<=' (not `<') as subdir line is part of subdir
2888 alist (if (<= (dired-get-subdir-min elt) here)
2889 nil ; found
2890 (cdr alist))))
2891 (if localp
2892 (dired-make-relative dir default-directory)
2893 dir)))
2895 ;; Subdirs start at the beginning of their header lines and end just
2896 ;; before the beginning of the next header line (or end of buffer).
2898 (defun dired-subdir-max ()
2899 (save-excursion
2900 (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
2901 (point-max)
2902 (point))))
2904 ;; Deleting files
2906 (defcustom dired-recursive-deletes 'top
2907 "Whether Dired deletes directories recursively.
2908 If nil, Dired will not delete non-empty directories.
2909 `always' means to delete non-empty directories recursively,
2910 without asking. This is dangerous!
2911 `top' means to ask for each top-level directory specified by the
2912 Dired deletion command, and delete its subdirectories without
2913 asking.
2914 Any other value means to ask for each directory."
2915 :type '(choice :tag "Delete non-empty directories"
2916 (const :tag "Yes" always)
2917 (const :tag "No--only delete empty directories" nil)
2918 (const :tag "Confirm for each directory" t)
2919 (const :tag "Confirm for each top directory only" top))
2920 :group 'dired)
2922 ;; Match anything but `.' and `..'.
2923 (defvar dired-re-no-dot "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")
2925 ;; Delete file, possibly delete a directory and all its files.
2926 ;; This function is useful outside of dired. One could change its name
2927 ;; to e.g. recursive-delete-file and put it somewhere else.
2928 (defun dired-delete-file (file &optional recursive trash) "\
2929 Delete FILE or directory (possibly recursively if optional RECURSIVE is true.)
2930 RECURSIVE determines what to do with a non-empty directory. The effect of
2931 its possible values is:
2933 nil -- do not delete.
2934 `always' -- delete recursively without asking.
2935 `top' -- ask for each directory at top level.
2936 Anything else -- ask for each sub-directory.
2938 TRASH non-nil means to trash the file instead of deleting, provided
2939 `delete-by-moving-to-trash' (which see) is non-nil."
2940 ;; This test is equivalent to
2941 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
2942 ;; but more efficient
2943 (if (not (eq t (car (file-attributes file))))
2944 (delete-file file trash)
2945 (if (and recursive
2946 (directory-files file t dired-re-no-dot) ; Not empty.
2947 (or (eq recursive 'always)
2948 (yes-or-no-p (format "Recursively %s %s? "
2949 (if (and trash
2950 delete-by-moving-to-trash)
2951 "trash"
2952 "delete")
2953 (dired-make-relative file)))))
2954 (if (eq recursive 'top) (setq recursive 'always)) ; Don't ask again.
2955 (setq recursive nil))
2956 (delete-directory file recursive trash)))
2958 (defun dired-do-flagged-delete (&optional nomessage)
2959 "In Dired, delete the files flagged for deletion.
2960 If NOMESSAGE is non-nil, we don't display any message
2961 if there are no flagged files.
2962 `dired-recursive-deletes' controls whether deletion of
2963 non-empty directories is allowed."
2964 (interactive)
2965 (let* ((dired-marker-char dired-del-marker)
2966 (regexp (dired-marker-regexp))
2967 case-fold-search)
2968 (if (save-excursion (goto-char (point-min))
2969 (re-search-forward regexp nil t))
2970 (dired-internal-do-deletions
2971 ;; this can't move point since ARG is nil
2972 (dired-map-over-marks (cons (dired-get-filename) (point))
2973 nil)
2974 nil t)
2975 (or nomessage
2976 (message "(No deletions requested)")))))
2978 (defun dired-do-delete (&optional arg)
2979 "Delete all marked (or next ARG) files.
2980 `dired-recursive-deletes' controls whether deletion of
2981 non-empty directories is allowed."
2982 ;; This is more consistent with the file marking feature than
2983 ;; dired-do-flagged-delete.
2984 (interactive "P")
2985 (dired-internal-do-deletions
2986 ;; this may move point if ARG is an integer
2987 (dired-map-over-marks (cons (dired-get-filename) (point))
2988 arg)
2989 arg t))
2991 (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
2993 (defun dired-internal-do-deletions (l arg &optional trash)
2994 ;; L is an alist of files to delete, with their buffer positions.
2995 ;; ARG is the prefix arg.
2996 ;; Filenames are absolute.
2997 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
2998 ;; That way as changes are made in the buffer they do not shift the
2999 ;; lines still to be changed, so the (point) values in L stay valid.
3000 ;; Also, for subdirs in natural order, a subdir's files are deleted
3001 ;; before the subdir itself - the other way around would not work.
3002 (let* ((files (mapcar #'car l))
3003 (count (length l))
3004 (succ 0)
3005 (trashing (and trash delete-by-moving-to-trash)))
3006 ;; canonicalize file list for pop up
3007 (setq files (nreverse (mapcar #'dired-make-relative files)))
3008 (if (dired-mark-pop-up
3009 " *Deletions*" 'delete files dired-deletion-confirmer
3010 (format "%s %s "
3011 (if trashing "Trash" "Delete")
3012 (dired-mark-prompt arg files)))
3013 (save-excursion
3014 (let ((progress-reporter
3015 (make-progress-reporter
3016 (if trashing "Trashing..." "Deleting...")
3017 succ count))
3018 failures) ;; files better be in reverse order for this loop!
3019 (while l
3020 (goto-char (cdr (car l)))
3021 (let ((inhibit-read-only t))
3022 (condition-case err
3023 (let ((fn (car (car l))))
3024 (dired-delete-file fn dired-recursive-deletes trash)
3025 ;; if we get here, removing worked
3026 (setq succ (1+ succ))
3027 (progress-reporter-update progress-reporter succ)
3028 (dired-fun-in-all-buffers
3029 (file-name-directory fn) (file-name-nondirectory fn)
3030 #'dired-delete-entry fn))
3031 (error ;; catch errors from failed deletions
3032 (dired-log "%s\n" err)
3033 (setq failures (cons (car (car l)) failures)))))
3034 (setq l (cdr l)))
3035 (if (not failures)
3036 (progress-reporter-done progress-reporter)
3037 (dired-log-summary
3038 (format "%d of %d deletion%s failed"
3039 (length failures) count
3040 (dired-plural-s count))
3041 failures))))
3042 (message "(No deletions performed)")))
3043 (dired-move-to-filename))
3045 (defun dired-fun-in-all-buffers (directory file fun &rest args)
3046 ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS.
3047 ;; If the buffer has a wildcard pattern, check that it matches FILE.
3048 ;; (FILE does not include a directory component.)
3049 ;; FILE may be nil, in which case ignore it.
3050 ;; Return list of buffers where FUN succeeded (i.e., returned non-nil).
3051 (let (success-list)
3052 (dolist (buf (dired-buffers-for-dir (expand-file-name directory)
3053 file))
3054 (with-current-buffer buf
3055 (if (apply fun args)
3056 (setq success-list (cons (buffer-name buf) success-list)))))
3057 success-list))
3059 ;; Delete the entry for FILE from
3060 (defun dired-delete-entry (file)
3061 (save-excursion
3062 (and (dired-goto-file file)
3063 (let ((inhibit-read-only t))
3064 (delete-region (progn (beginning-of-line) (point))
3065 (save-excursion (forward-line 1) (point))))))
3066 (dired-clean-up-after-deletion file))
3068 (defvar dired-clean-up-buffers-too)
3070 (defun dired-clean-up-after-deletion (fn)
3071 "Clean up after a deleted file or directory FN.
3072 Removes any expanded subdirectory of deleted directory.
3073 If `dired-x' is loaded and `dired-clean-up-buffers-too' is non-nil,
3074 also offers to kill buffers visiting deleted files and directories."
3075 (save-excursion (and (cdr dired-subdir-alist)
3076 (dired-goto-subdir fn)
3077 (dired-kill-subdir)))
3078 ;; Offer to kill buffer of deleted file FN.
3079 (when (and (featurep 'dired-x) dired-clean-up-buffers-too)
3080 (let ((buf (get-file-buffer fn)))
3081 (and buf
3082 (funcall #'y-or-n-p
3083 (format "Kill buffer of %s, too? "
3084 (file-name-nondirectory fn)))
3085 (kill-buffer buf)))
3086 (let ((buf-list (dired-buffers-for-dir (expand-file-name fn))))
3087 (and buf-list
3088 (y-or-n-p (format "Kill Dired buffer%s of %s, too? "
3089 (dired-plural-s (length buf-list))
3090 (file-name-nondirectory fn)))
3091 (dolist (buf buf-list)
3092 (kill-buffer buf))))))
3095 ;; Confirmation
3097 (defun dired-marker-regexp ()
3098 (concat "^" (regexp-quote (char-to-string dired-marker-char))))
3100 (defun dired-plural-s (count)
3101 (if (= 1 count) "" "s"))
3103 (defun dired-mark-prompt (arg files)
3104 "Return a string suitable for use in a Dired prompt.
3105 ARG is normally the prefix argument for the calling command.
3106 FILES should be a list of file names.
3108 The return value has a form like \"foo.txt\", \"[next 3 files]\",
3109 or \"* [3 files]\"."
3110 ;; distinguish-one-marked can cause the first element to be just t.
3111 (if (eq (car files) t) (setq files (cdr files)))
3112 (let ((count (length files)))
3113 (if (= count 1)
3114 (car files)
3115 ;; more than 1 file:
3116 (if (integerp arg)
3117 ;; abs(arg) = count
3118 ;; Perhaps this is nicer, but it also takes more screen space:
3119 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
3120 ;; count)
3121 (format "[next %d files]" arg)
3122 (format "%c [%d files]" dired-marker-char count)))))
3124 (defun dired-pop-to-buffer (buf)
3125 "Pop up buffer BUF in a way suitable for Dired."
3126 (declare (obsolete dired-mark-pop-up "24.3"))
3127 (let ((split-window-preferred-function
3128 (lambda (window)
3129 (or (and (let ((split-height-threshold 0))
3130 (window-splittable-p (selected-window)))
3131 ;; Try to split the selected window vertically if
3132 ;; that's possible. (Bug#1806)
3133 (split-window-below))
3134 ;; Otherwise, try to split WINDOW sensibly.
3135 (split-window-sensibly window))))
3136 pop-up-frames)
3137 (pop-to-buffer (get-buffer-create buf)))
3138 ;; See Bug#12281.
3139 (set-window-start nil (point-min))
3140 ;; If dired-shrink-to-fit is t, make its window fit its contents.
3141 (when dired-shrink-to-fit
3142 ;; Try to not delete window when we want to display less than
3143 ;; `window-min-height' lines.
3144 (fit-window-to-buffer (get-buffer-window buf) nil 1 nil nil t)))
3146 (defcustom dired-no-confirm nil
3147 "A list of symbols for commands Dired should not confirm, or t.
3148 Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress',
3149 `copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink',
3150 `touch' and `uncompress'.
3151 If t, confirmation is never needed."
3152 :group 'dired
3153 :type '(choice (const :tag "Confirmation never needed" t)
3154 (set (const byte-compile) (const chgrp)
3155 (const chmod) (const chown) (const compress)
3156 (const copy) (const delete) (const hardlink)
3157 (const load) (const move) (const print)
3158 (const shell) (const symlink) (const touch)
3159 (const uncompress))))
3161 (defun dired-mark-pop-up (buffer-or-name op-symbol files function &rest args)
3162 "Return FUNCTION's result on ARGS after showing which files are marked.
3163 Displays the file names in a window showing a buffer named
3164 BUFFER-OR-NAME; the default name being \" *Marked Files*\". The
3165 window is not shown if there is just one file, `dired-no-confirm'
3166 is t, or OP-SYMBOL is a member of the list in `dired-no-confirm'.
3168 By default, Dired shrinks the display buffer to fit the marked files.
3169 To disable this, use the Customization interface to add a new rule
3170 to `display-buffer-alist' where condition regexp is \"^ \\*Marked Files\\*$\",
3171 action argument symbol is `window-height' and its value is nil.
3173 FILES is the list of marked files. It can also be (t FILENAME)
3174 in the case of one marked file, to distinguish that from using
3175 just the current file.
3177 FUNCTION should not manipulate files, just read input (an
3178 argument or confirmation)."
3179 (if (or (eq dired-no-confirm t)
3180 (memq op-symbol dired-no-confirm)
3181 ;; If FILES defaulted to the current line's file.
3182 (= (length files) 1))
3183 (apply function args)
3184 (let ((buffer (get-buffer-create (or buffer-or-name " *Marked Files*")))
3185 ;; Mark *Marked Files* window as softly-dedicated, to prevent
3186 ;; other buffers e.g. *Completions* from reusing it (bug#17554).
3187 (display-buffer-mark-dedicated 'soft))
3188 (with-displayed-buffer-window
3189 buffer
3190 (cons 'display-buffer-below-selected
3191 '((window-height . fit-window-to-buffer)
3192 (preserve-size . (nil . t))))
3193 #'(lambda (window _value)
3194 (with-selected-window window
3195 (unwind-protect
3196 (apply function args)
3197 (when (window-live-p window)
3198 (quit-restore-window window 'kill)))))
3199 ;; Handle (t FILE) just like (FILE), here. That value is
3200 ;; used (only in some cases), to mean just one file that was
3201 ;; marked, rather than the current line file.
3202 (with-current-buffer buffer
3203 (dired-format-columns-of-files
3204 (if (eq (car files) t) (cdr files) files))
3205 (remove-text-properties (point-min) (point-max)
3206 '(mouse-face nil help-echo nil)))))))
3208 (defun dired-format-columns-of-files (files)
3209 (let ((beg (point)))
3210 (completion--insert-strings files)
3211 (put-text-property beg (point) 'mouse-face nil)))
3213 ;; Commands to mark or flag file(s) at or near current line.
3215 (defun dired-repeat-over-lines (arg function)
3216 ;; This version skips non-file lines.
3217 (let ((pos (make-marker)))
3218 (beginning-of-line)
3219 (while (and (> arg 0) (not (eobp)))
3220 (setq arg (1- arg))
3221 (beginning-of-line)
3222 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
3223 (save-excursion
3224 (forward-line 1)
3225 (move-marker pos (1+ (point))))
3226 (save-excursion (funcall function))
3227 ;; Advance to the next line--actually, to the line that *was* next.
3228 ;; (If FUNCTION inserted some new lines in between, skip them.)
3229 (goto-char pos))
3230 (while (and (< arg 0) (not (bobp)))
3231 (setq arg (1+ arg))
3232 (forward-line -1)
3233 (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
3234 (beginning-of-line)
3235 (save-excursion (funcall function)))
3236 (move-marker pos nil)
3237 (dired-move-to-filename)))
3239 (defun dired-between-files ()
3240 ;; This used to be a regexp match of the `total ...' line output by
3241 ;; ls, which is slightly faster, but that is not very robust; notably,
3242 ;; it fails for non-english locales.
3243 (save-excursion (not (dired-move-to-filename))))
3245 (defun dired-next-marked-file (arg &optional wrap opoint)
3246 "Move to the next marked file.
3247 If WRAP is non-nil, wrap around to the beginning of the buffer if
3248 we reach the end."
3249 (interactive "p\np")
3250 (or opoint (setq opoint (point)));; return to where interactively started
3251 (if (if (> arg 0)
3252 (re-search-forward dired-re-mark nil t arg)
3253 (beginning-of-line)
3254 (re-search-backward dired-re-mark nil t (- arg)))
3255 (dired-move-to-filename)
3256 (if (null wrap)
3257 (progn
3258 (goto-char opoint)
3259 (error "No next marked file"))
3260 (message "(Wraparound for next marked file)")
3261 (goto-char (if (> arg 0) (point-min) (point-max)))
3262 (dired-next-marked-file arg nil opoint))))
3264 (defun dired-prev-marked-file (arg &optional wrap)
3265 "Move to the previous marked file.
3266 If WRAP is non-nil, wrap around to the end of the buffer if we
3267 reach the beginning of the buffer."
3268 (interactive "p\np")
3269 (dired-next-marked-file (- arg) wrap))
3271 (defun dired-file-marker (file)
3272 ;; Return FILE's marker, or nil if unmarked.
3273 (save-excursion
3274 (and (dired-goto-file file)
3275 (progn
3276 (beginning-of-line)
3277 (if (not (equal ?\040 (following-char)))
3278 (following-char))))))
3280 (defun dired-mark-files-in-region (start end)
3281 (let ((inhibit-read-only t))
3282 (if (> start end)
3283 (error "start > end"))
3284 (goto-char start) ; assumed at beginning of line
3285 (while (< (point) end)
3286 ;; Skip subdir line and following garbage like the `total' line:
3287 (while (and (< (point) end) (dired-between-files))
3288 (forward-line 1))
3289 (if (and (not (looking-at-p dired-re-dot))
3290 (dired-get-filename nil t))
3291 (progn
3292 (delete-char 1)
3293 (insert dired-marker-char)))
3294 (forward-line 1))))
3296 (defun dired-mark (arg &optional interactive)
3297 "Mark the file at point in the Dired buffer.
3298 If the region is active, mark all files in the region.
3299 Otherwise, with a prefix arg, mark files on the next ARG lines.
3301 If on a subdir headerline, mark all its files except `.' and `..'.
3303 Use \\[dired-unmark-all-files] to remove all marks
3304 and \\[dired-unmark] on a subdir to remove the marks in
3305 this subdir."
3306 (interactive (list current-prefix-arg t))
3307 (cond
3308 ;; Mark files in the active region.
3309 ((and interactive (use-region-p))
3310 (save-excursion
3311 (let ((beg (region-beginning))
3312 (end (region-end)))
3313 (dired-mark-files-in-region
3314 (progn (goto-char beg) (line-beginning-position))
3315 (progn (goto-char end) (line-beginning-position))))))
3316 ;; Mark subdir files from the subdir headerline.
3317 ((dired-get-subdir)
3318 (save-excursion (dired-mark-subdir-files)))
3319 ;; Mark the current (or next ARG) files.
3321 (let ((inhibit-read-only t))
3322 (dired-repeat-over-lines
3323 (prefix-numeric-value arg)
3324 (lambda () (delete-char 1) (insert dired-marker-char)))))))
3326 (defun dired-unmark (arg &optional interactive)
3327 "Unmark the file at point in the Dired buffer.
3328 If the region is active, unmark all files in the region.
3329 Otherwise, with a prefix arg, unmark files on the next ARG lines.
3331 If looking at a subdir, unmark all its files except `.' and `..'.
3332 If the region is active in Transient Mark mode, unmark all files
3333 in the active region."
3334 (interactive (list current-prefix-arg t))
3335 (let ((dired-marker-char ?\040))
3336 (dired-mark arg interactive)))
3338 (defun dired-flag-file-deletion (arg &optional interactive)
3339 "In Dired, flag the current line's file for deletion.
3340 If the region is active, flag all files in the region.
3341 Otherwise, with a prefix arg, flag files on the next ARG lines.
3343 If on a subdir headerline, flag all its files except `.' and `..'.
3344 If the region is active in Transient Mark mode, flag all files
3345 in the active region."
3346 (interactive (list current-prefix-arg t))
3347 (let ((dired-marker-char dired-del-marker))
3348 (dired-mark arg interactive)))
3350 (defun dired-unmark-backward (arg)
3351 "In Dired, move up lines and remove marks or deletion flags there.
3352 Optional prefix ARG says how many lines to unmark/unflag; default
3353 is one line.
3354 If the region is active in Transient Mark mode, unmark all files
3355 in the active region."
3356 (interactive "p")
3357 (dired-unmark (- arg) t))
3359 (defun dired-toggle-marks ()
3360 "Toggle marks: marked files become unmarked, and vice versa.
3361 Files marked with other flags (such as `D') are not affected.
3362 `.' and `..' are never toggled.
3363 As always, hidden subdirs are not affected."
3364 (interactive)
3365 (save-excursion
3366 (goto-char (point-min))
3367 (let ((inhibit-read-only t))
3368 (while (not (eobp))
3369 (or (dired-between-files)
3370 (looking-at-p dired-re-dot)
3371 ;; use subst instead of insdel because it does not move
3372 ;; the gap and thus should be faster and because
3373 ;; other characters are left alone automatically
3374 (apply 'subst-char-in-region
3375 (point) (1+ (point))
3376 (if (eq ?\040 (following-char)) ; SPC
3377 (list ?\040 dired-marker-char)
3378 (list dired-marker-char ?\040))))
3379 (forward-line 1)))))
3381 ;;; Commands to mark or flag files based on their characteristics or names.
3383 (defvar dired-regexp-history nil
3384 "History list of regular expressions used in Dired commands.")
3386 (defun dired-read-regexp (prompt &optional default history)
3387 "Read a regexp using `read-regexp'."
3388 (declare (obsolete read-regexp "24.5"))
3389 (read-regexp prompt default (or history 'dired-regexp-history)))
3391 (defun dired-mark-files-regexp (regexp &optional marker-char)
3392 "Mark all files matching REGEXP for use in later commands.
3393 A prefix argument means to unmark them instead.
3394 `.' and `..' are never marked.
3396 REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
3397 object files--just `.o' will mark more than you might think."
3398 (interactive
3399 (list (read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
3400 " files (regexp): ")
3401 ;; Add more suggestions into the default list
3402 (cons nil (list (dired-get-filename t t)
3403 (and (dired-get-filename nil t)
3404 (concat (regexp-quote
3405 (file-name-extension
3406 (dired-get-filename nil t) t))
3407 "\\'"))))
3408 'dired-regexp-history)
3409 (if current-prefix-arg ?\040)))
3410 (let ((dired-marker-char (or marker-char dired-marker-char)))
3411 (dired-mark-if
3412 (and (not (looking-at-p dired-re-dot))
3413 (not (eolp)) ; empty line
3414 (let ((fn (dired-get-filename t t)))
3415 (and fn (string-match-p regexp fn))))
3416 "matching file")))
3418 (defun dired-mark-files-containing-regexp (regexp &optional marker-char)
3419 "Mark all files with contents containing REGEXP for use in later commands.
3420 A prefix argument means to unmark them instead.
3421 `.' and `..' are never marked.
3423 Note that if a file is visited in an Emacs buffer, and
3424 `dired-always-read-filesystem' is nil, this command will
3425 look in the buffer without revisiting the file, so the results might
3426 be inconsistent with the file on disk if its contents has changed
3427 since it was last visited."
3428 (interactive
3429 (list (read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
3430 " files containing (regexp): ")
3431 nil 'dired-regexp-history)
3432 (if current-prefix-arg ?\040)))
3433 (let ((dired-marker-char (or marker-char dired-marker-char)))
3434 (dired-mark-if
3435 (and (not (looking-at-p dired-re-dot))
3436 (not (eolp)) ; empty line
3437 (let ((fn (dired-get-filename nil t)))
3438 (when (and fn (file-readable-p fn)
3439 (not (file-directory-p fn)))
3440 (let ((prebuf (get-file-buffer fn)))
3441 (message "Checking %s" fn)
3442 ;; For now we do it inside emacs
3443 ;; Grep might be better if there are a lot of files
3444 (if (and prebuf (not dired-always-read-filesystem))
3445 (with-current-buffer prebuf
3446 (save-excursion
3447 (goto-char (point-min))
3448 (re-search-forward regexp nil t)))
3449 (with-temp-buffer
3450 (insert-file-contents fn)
3451 (goto-char (point-min))
3452 (re-search-forward regexp nil t))))
3454 "matching file")))
3456 (defun dired-flag-files-regexp (regexp)
3457 "In Dired, flag all files containing the specified REGEXP for deletion.
3458 The match is against the non-directory part of the filename. Use `^'
3459 and `$' to anchor matches. Exclude subdirs by hiding them.
3460 `.' and `..' are never flagged."
3461 (interactive (list (read-regexp "Flag for deletion (regexp): "
3462 nil 'dired-regexp-history)))
3463 (dired-mark-files-regexp regexp dired-del-marker))
3465 (defun dired-mark-symlinks (unflag-p)
3466 "Mark all symbolic links.
3467 With prefix argument, unmark or unflag all those files."
3468 (interactive "P")
3469 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
3470 (dired-mark-if (looking-at-p dired-re-sym) "symbolic link")))
3472 (defun dired-mark-directories (unflag-p)
3473 "Mark all directory file lines except `.' and `..'.
3474 With prefix argument, unmark or unflag all those files."
3475 (interactive "P")
3476 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
3477 (dired-mark-if (and (looking-at-p dired-re-dir)
3478 (not (looking-at-p dired-re-dot)))
3479 "directory file")))
3481 (defun dired-mark-executables (unflag-p)
3482 "Mark all executable files.
3483 With prefix argument, unmark or unflag all those files."
3484 (interactive "P")
3485 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
3486 (dired-mark-if (looking-at-p dired-re-exe) "executable file")))
3488 ;; dired-x.el has a dired-mark-sexp interactive command: mark
3489 ;; files for which PREDICATE returns non-nil.
3491 (defun dired-flag-auto-save-files (&optional unflag-p)
3492 "Flag for deletion files whose names suggest they are auto save files.
3493 A prefix argument says to unmark or unflag those files instead."
3494 (interactive "P")
3495 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
3496 (dired-mark-if
3497 ;; It is less than general to check for # here,
3498 ;; but it's the only way this runs fast enough.
3499 (and (save-excursion (end-of-line)
3501 (eq (preceding-char) ?#)
3502 ;; Handle executables in case of -F option.
3503 ;; We need not worry about the other kinds
3504 ;; of markings that -F makes, since they won't
3505 ;; appear on real auto-save files.
3506 (if (eq (preceding-char) ?*)
3507 (progn
3508 (forward-char -1)
3509 (eq (preceding-char) ?#)))))
3510 (not (looking-at-p dired-re-dir))
3511 (let ((fn (dired-get-filename t t)))
3512 (if fn (auto-save-file-name-p
3513 (file-name-nondirectory fn)))))
3514 "auto save file")))
3516 (defcustom dired-garbage-files-regexp
3517 ;; `log' here is dubious, since it's typically used for useful log
3518 ;; files, not just TeX stuff. -- fx
3519 (concat (regexp-opt
3520 '(".log" ".toc" ".dvi" ".bak" ".orig" ".rej" ".aux"))
3521 "\\'")
3522 "Regular expression to match \"garbage\" files for `dired-flag-garbage-files'."
3523 :type 'regexp
3524 :group 'dired)
3526 (defun dired-flag-garbage-files ()
3527 "Flag for deletion all files that match `dired-garbage-files-regexp'."
3528 (interactive)
3529 (dired-flag-files-regexp dired-garbage-files-regexp))
3531 (defun dired-flag-backup-files (&optional unflag-p)
3532 "Flag all backup files (names ending with `~') for deletion.
3533 With prefix argument, unmark or unflag these files."
3534 (interactive "P")
3535 (let ((dired-marker-char (if unflag-p ?\s dired-del-marker)))
3536 (dired-mark-if
3537 ;; Don't call backup-file-name-p unless the last character looks like
3538 ;; it might be the end of a backup file name. This isn't very general,
3539 ;; but it's the only way this runs fast enough.
3540 (and (save-excursion (end-of-line)
3541 ;; Handle executables in case of -F option.
3542 ;; We need not worry about the other kinds
3543 ;; of markings that -F makes, since they won't
3544 ;; appear on real backup files.
3545 (if (eq (preceding-char) ?*)
3546 (forward-char -1))
3547 (eq (preceding-char) ?~))
3548 (not (looking-at-p dired-re-dir))
3549 (let ((fn (dired-get-filename t t)))
3550 (if fn (backup-file-name-p fn))))
3551 "backup file")))
3553 (defun dired-change-marks (&optional old new)
3554 "Change all OLD marks to NEW marks.
3555 OLD and NEW are both characters used to mark files."
3556 (interactive
3557 (let* ((cursor-in-echo-area t)
3558 (old (progn (message "Change (old mark): ") (read-char)))
3559 (new (progn (message "Change %c marks to (new mark): " old)
3560 (read-char))))
3561 (list old new)))
3562 (if (or (eq old ?\r) (eq new ?\r))
3563 (ding)
3564 (let ((string (format "\n%c" old))
3565 (inhibit-read-only t))
3566 (save-excursion
3567 (goto-char (point-min))
3568 (while (search-forward string nil t)
3569 (if (if (= old ?\s)
3570 (save-match-data
3571 (dired-get-filename 'no-dir t))
3573 (subst-char-in-region (match-beginning 0)
3574 (match-end 0) old new)))))))
3576 (defun dired-unmark-all-marks ()
3577 "Remove all marks from all files in the Dired buffer."
3578 (interactive)
3579 (dired-unmark-all-files ?\r))
3581 ;; Bound in dired-unmark-all-files
3582 (defvar dired-unmark-all-files-query)
3584 (defun dired-unmark-all-files (mark &optional arg)
3585 "Remove a specific mark (or any mark) from every file.
3586 After this command, type the mark character to remove,
3587 or type RET to remove all marks.
3588 With prefix arg, query for each marked file.
3589 Type \\[help-command] at that time for help."
3590 (interactive "cRemove marks (RET means all): \nP")
3591 (save-excursion
3592 (let* ((count 0)
3593 (inhibit-read-only t) case-fold-search
3594 dired-unmark-all-files-query
3595 (string (format "\n%c" mark))
3596 (help-form "\
3597 Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
3598 `!' to unmark all remaining files with no more questions."))
3599 (goto-char (point-min))
3600 (while (if (eq mark ?\r)
3601 (re-search-forward dired-re-mark nil t)
3602 (search-forward string nil t))
3603 (if (or (not arg)
3604 (let ((file (dired-get-filename t t)))
3605 (and file
3606 (dired-query 'dired-unmark-all-files-query
3607 "Unmark file `%s'? "
3608 file))))
3609 (progn (subst-char-in-region (1- (point)) (point)
3610 (preceding-char) ?\s)
3611 (setq count (1+ count)))))
3612 (message (if (= count 1) "1 mark removed"
3613 "%d marks removed")
3614 count))))
3616 ;; Logging failures operating on files, and showing the results.
3618 (defvar dired-log-buffer "*Dired log*")
3620 (defun dired-why ()
3621 "Pop up a buffer with error log output from Dired.
3622 A group of errors from a single command ends with a formfeed.
3623 Thus, use \\[backward-page] to find the beginning of a group of errors."
3624 (interactive)
3625 (if (get-buffer dired-log-buffer)
3626 (let ((owindow (selected-window))
3627 (window (display-buffer (get-buffer dired-log-buffer))))
3628 (unwind-protect
3629 (progn
3630 (select-window window)
3631 (goto-char (point-max))
3632 (forward-line -1)
3633 (backward-page 1)
3634 (recenter 0))
3635 (select-window owindow)))))
3637 (defun dired-log (log &rest args)
3638 ;; Log a message or the contents of a buffer.
3639 ;; If LOG is a string and there are more args, it is formatted with
3640 ;; those ARGS. Usually the LOG string ends with a \n.
3641 ;; End each bunch of errors with (dired-log t):
3642 ;; this inserts the current time and buffer at the start of the page,
3643 ;; and \f (formfeed) at the end.
3644 (let ((obuf (current-buffer)))
3645 (with-current-buffer (get-buffer-create dired-log-buffer)
3646 (goto-char (point-max))
3647 (let ((inhibit-read-only t))
3648 (cond ((stringp log)
3649 (insert (if args
3650 (apply #'format-message log args)
3651 log)))
3652 ((bufferp log)
3653 (insert-buffer-substring log))
3654 ((eq t log)
3655 (backward-page 1)
3656 (unless (bolp)
3657 (insert "\n"))
3658 (insert (current-time-string)
3659 (format-message "\tBuffer `%s'\n" (buffer-name obuf)))
3660 (goto-char (point-max))
3661 (insert "\f\n")))))))
3663 (defun dired-log-summary (string failures)
3664 "State a summary of a command's failures, in echo area and log buffer.
3665 STRING is an overall summary of the failures.
3666 FAILURES is a list of file names that we failed to operate on,
3667 or nil if file names are not applicable."
3668 (if (= (length failures) 1)
3669 (message "%s"
3670 (with-current-buffer dired-log-buffer
3671 (goto-char (point-max))
3672 (backward-page 1)
3673 (if (eolp) (forward-line 1))
3674 (buffer-substring (point) (point-max))))
3675 (message (if failures "%s--type ? for details (%s)"
3676 "%s--type ? for details")
3677 string failures))
3678 ;; Log a summary describing a bunch of errors.
3679 (dired-log (concat "\n" string "\n"))
3680 (dired-log t))
3682 ;;; Sorting
3684 ;; Most ls can only sort by name or by date (with -t), nothing else.
3685 ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
3686 ;; So anything that does not contain these is sort "by name".
3688 (defvar dired-ls-sorting-switches "SXU"
3689 "String of `ls' switches (single letters) except \"t\" that influence sorting.
3691 This indicates to Dired which option switches to watch out for because they
3692 will change the sorting order behavior of `ls'.
3694 To change the default sorting order (e.g. add a `-v' option), see the
3695 variable `dired-listing-switches'. To temporarily override the listing
3696 format, use `\\[universal-argument] \\[dired]'.")
3698 (defvar dired-sort-by-date-regexp
3699 (concat "\\(\\`\\| \\)-[^- ]*t"
3700 ;; `dired-ls-sorting-switches' after -t overrides -t.
3701 "[^ " dired-ls-sorting-switches "]*"
3702 "\\(\\(\\`\\| +\\)\\(--[^ ]+\\|-[^- t"
3703 dired-ls-sorting-switches "]+\\)\\)* *$")
3704 "Regexp recognized by Dired to set `by date' mode.")
3706 (defvar dired-sort-by-name-regexp
3707 (concat "\\`\\(\\(\\`\\| +\\)\\(--[^ ]+\\|"
3708 "-[^- t" dired-ls-sorting-switches "]+\\)\\)* *$")
3709 "Regexp recognized by Dired to set `by name' mode.")
3711 (defvar dired-sort-inhibit nil
3712 "Non-nil means the Dired sort command is disabled.
3713 The idea is to set this buffer-locally in special Dired buffers.")
3715 (defun dired-sort-set-mode-line ()
3716 ;; Set mode line display according to dired-actual-switches.
3717 ;; Mode line display of "by name" or "by date" guarantees the user a
3718 ;; match with the corresponding regexps. Non-matching switches are
3719 ;; shown literally.
3720 (when (eq major-mode 'dired-mode)
3721 (setq mode-name
3722 (let (case-fold-search)
3723 (cond ((string-match-p
3724 dired-sort-by-name-regexp dired-actual-switches)
3725 "Dired by name")
3726 ((string-match-p
3727 dired-sort-by-date-regexp dired-actual-switches)
3728 "Dired by date")
3730 (concat "Dired " dired-actual-switches)))))
3731 (force-mode-line-update)))
3733 (define-obsolete-function-alias 'dired-sort-set-modeline
3734 'dired-sort-set-mode-line "24.3")
3736 (defun dired-sort-toggle-or-edit (&optional arg)
3737 "Toggle sorting by date, and refresh the Dired buffer.
3738 With a prefix argument, edit the current listing switches instead."
3739 (interactive "P")
3740 (when dired-sort-inhibit
3741 (error "Cannot sort this Dired buffer"))
3742 (if arg
3743 (dired-sort-other
3744 (read-string "ls switches (must contain -l): " dired-actual-switches))
3745 (dired-sort-toggle)))
3747 (defun dired-sort-toggle ()
3748 ;; Toggle between sort by date/name. Reverts the buffer.
3749 (let ((sorting-by-date (string-match-p dired-sort-by-date-regexp
3750 dired-actual-switches))
3751 ;; Regexp for finding (possibly embedded) -t switches.
3752 (switch-regexp "\\(\\`\\| \\)-\\([a-su-zA-Z]*\\)\\(t\\)\\([^ ]*\\)")
3753 case-fold-search)
3754 ;; Remove the -t switch.
3755 (while (string-match switch-regexp dired-actual-switches)
3756 (if (and (equal (match-string 2 dired-actual-switches) "")
3757 (equal (match-string 4 dired-actual-switches) ""))
3758 ;; Remove a stand-alone -t switch.
3759 (setq dired-actual-switches
3760 (replace-match "" t t dired-actual-switches))
3761 ;; Remove a switch of the form -XtY for some X and Y.
3762 (setq dired-actual-switches
3763 (replace-match "" t t dired-actual-switches 3))))
3765 ;; Now, if we weren't sorting by date before, add the -t switch.
3766 ;; Some simple-minded ls implementations (eg ftp servers) only
3767 ;; allow a single option string, so try not to add " -t" if possible.
3768 (unless sorting-by-date
3769 (setq dired-actual-switches
3770 (concat dired-actual-switches
3771 (if (string-match-p "\\`-[[:alnum:]]+\\'"
3772 dired-actual-switches)
3774 " -t")))))
3775 (dired-sort-set-mode-line)
3776 (revert-buffer))
3778 ;; Some user code loads dired especially for this.
3779 ;; Don't do that--use replace-regexp-in-string instead.
3780 (defun dired-replace-in-string (regexp newtext string)
3781 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
3782 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
3783 (let ((result "") (start 0) mb me)
3784 (while (string-match regexp string start)
3785 (setq mb (match-beginning 0)
3786 me (match-end 0)
3787 result (concat result (substring string start mb) newtext)
3788 start me))
3789 (concat result (substring string start))))
3791 (defun dired-sort-other (switches &optional no-revert)
3792 "Specify new `ls' SWITCHES for current Dired buffer.
3793 Values matching `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp'
3794 set the minor mode accordingly, others appear literally in the mode line.
3795 With optional second arg NO-REVERT, don't refresh the listing afterwards."
3796 (dired-sort-R-check switches)
3797 (setq dired-actual-switches switches)
3798 (dired-sort-set-mode-line)
3799 (or no-revert (revert-buffer)))
3801 (defvar-local dired-subdir-alist-pre-R nil
3802 "Value of `dired-subdir-alist' before -R switch added.")
3804 (defun dired-sort-R-check (switches)
3805 "Additional processing of -R in ls option string SWITCHES.
3806 Saves `dired-subdir-alist' when R is set and restores saved value
3807 minus any directories explicitly deleted when R is cleared.
3808 To be called first in body of `dired-sort-other', etc."
3809 (cond
3810 ((and (dired-switches-recursive-p switches)
3811 (not (dired-switches-recursive-p dired-actual-switches)))
3812 ;; Adding -R to ls switches -- save `dired-subdir-alist':
3813 (setq dired-subdir-alist-pre-R dired-subdir-alist))
3814 ((and (dired-switches-recursive-p dired-actual-switches)
3815 (not (dired-switches-recursive-p switches)))
3816 ;; Deleting -R from ls switches -- revert to pre-R subdirs
3817 ;; that are still present:
3818 (setq dired-subdir-alist
3819 (if dired-subdir-alist-pre-R
3820 (let (subdirs)
3821 (while dired-subdir-alist-pre-R
3822 (if (assoc (caar dired-subdir-alist-pre-R)
3823 dired-subdir-alist)
3824 ;; subdir still present...
3825 (setq subdirs
3826 (cons (car dired-subdir-alist-pre-R)
3827 subdirs)))
3828 (setq dired-subdir-alist-pre-R
3829 (cdr dired-subdir-alist-pre-R)))
3830 (reverse subdirs))
3831 ;; No pre-R subdir alist, so revert to main directory
3832 ;; listing:
3833 (list (car (reverse dired-subdir-alist))))))))
3836 ;;;; Drag and drop support
3838 (defcustom dired-recursive-copies 'top
3839 "Whether Dired copies directories recursively.
3840 If nil, never copy recursively.
3841 `always' means to copy recursively without asking.
3842 `top' means to ask for each directory at top level.
3843 Any other value means to ask for each directory."
3844 :type '(choice :tag "Copy directories"
3845 (const :tag "No recursive copies" nil)
3846 (const :tag "Ask for each directory" t)
3847 (const :tag "Ask for each top directory only" top)
3848 (const :tag "Copy directories without asking" always))
3849 :group 'dired)
3851 (defun dired-dnd-popup-notice ()
3852 (message-box
3853 "Dired recursive copies are currently disabled.\nSee the variable `dired-recursive-copies'."))
3855 (declare-function x-popup-menu "menu.c" (position menu))
3857 (defun dired-dnd-do-ask-action (uri)
3858 ;; No need to get actions and descriptions from the source,
3859 ;; we only have three actions anyway.
3860 (let ((action (x-popup-menu
3862 (list "What action?"
3863 (cons ""
3864 '(("Copy here" . copy)
3865 ("Move here" . move)
3866 ("Link here" . link)
3867 "--"
3868 ("Cancel" . nil)))))))
3869 (if action
3870 (dired-dnd-handle-local-file uri action)
3871 nil)))
3873 (declare-function dired-relist-entry "dired-aux" (file))
3874 (declare-function make-symbolic-link "fileio.c")
3876 ;; Only used when (featurep 'dnd).
3877 (declare-function dnd-get-local-file-name "dnd" (uri &optional must-exist))
3878 (declare-function dnd-get-local-file-uri "dnd" (uri))
3880 (defvar dired-overwrite-confirmed) ;Defined in dired-aux.
3882 (defun dired-dnd-handle-local-file (uri action)
3883 "Copy, move or link a file to the Dired directory.
3884 URI is the file to handle, ACTION is one of copy, move, link or ask.
3885 Ask means pop up a menu for the user to select one of copy, move or link."
3886 (require 'dired-aux)
3887 (let* ((from (dnd-get-local-file-name uri t))
3888 (to (when from
3889 (concat (dired-current-directory)
3890 (file-name-nondirectory from)))))
3891 (when from
3892 (cond ((eq action 'ask)
3893 (dired-dnd-do-ask-action uri))
3894 ;; If copying a directory and dired-recursive-copies is
3895 ;; nil, dired-copy-file fails. Pop up a notice.
3896 ((and (memq action '(copy private))
3897 (file-directory-p from)
3898 (not dired-recursive-copies))
3899 (dired-dnd-popup-notice))
3900 ((memq action '(copy private move link))
3901 (let ((overwrite (and (file-exists-p to)
3902 (y-or-n-p
3903 (format-message
3904 "Overwrite existing file `%s'? " to))))
3905 ;; Binding dired-overwrite-confirmed to nil makes
3906 ;; dired-handle-overwrite a no-op. We instead use
3907 ;; y-or-n-p, which pops a graphical menu.
3908 dired-overwrite-confirmed backup-file)
3909 (when (and overwrite
3910 ;; d-b-o is defined in dired-aux.
3911 (boundp 'dired-backup-overwrite)
3912 dired-backup-overwrite
3913 (setq backup-file
3914 (car (find-backup-file-name to)))
3915 (or (eq dired-backup-overwrite 'always)
3916 (y-or-n-p
3917 (format-message
3918 "Make backup for existing file `%s'? " to))))
3919 (rename-file to backup-file 0)
3920 (dired-relist-entry backup-file))
3921 (cond ((memq action '(copy private))
3922 (dired-copy-file from to overwrite))
3923 ((eq action 'move)
3924 (dired-rename-file from to overwrite))
3925 ((eq action 'link)
3926 (make-symbolic-link from to overwrite)))
3927 (dired-relist-entry to)
3928 action))))))
3930 (defun dired-dnd-handle-file (uri action)
3931 "Copy, move or link a file to the Dired directory if it is a local file.
3932 URI is the file to handle. If the hostname in the URI isn't local, do nothing.
3933 ACTION is one of copy, move, link or ask.
3934 Ask means pop up a menu for the user to select one of copy, move or link."
3935 (let ((local-file (dnd-get-local-file-uri uri)))
3936 (if local-file (dired-dnd-handle-local-file local-file action)
3937 nil)))
3940 ;;;; Desktop support
3942 (eval-when-compile (require 'desktop))
3943 (declare-function desktop-file-name "desktop" (filename dirname))
3945 (defun dired-desktop-buffer-misc-data (dirname)
3946 "Auxiliary information to be saved in desktop file."
3947 (cons
3948 ;; Value of `dired-directory'.
3949 (if (consp dired-directory)
3950 ;; Directory name followed by list of files.
3951 (cons (desktop-file-name (car dired-directory) dirname)
3952 (cdr dired-directory))
3953 ;; Directory name, optionally with shell wildcard.
3954 (desktop-file-name dired-directory dirname))
3955 ;; Subdirectories in `dired-subdir-alist'.
3956 (cdr
3957 (nreverse
3958 (mapcar
3959 (lambda (f) (desktop-file-name (car f) dirname))
3960 dired-subdir-alist)))))
3962 (defun dired-restore-desktop-buffer (_file-name
3963 _buffer-name
3964 misc-data)
3965 "Restore a Dired buffer specified in a desktop file."
3966 ;; First element of `misc-data' is the value of `dired-directory'.
3967 ;; This value is a directory name, optionally with shell wildcard or
3968 ;; a directory name followed by list of files.
3969 (let* ((dired-dir (car misc-data))
3970 (dir (if (consp dired-dir) (car dired-dir) dired-dir)))
3971 (if (file-directory-p (file-name-directory dir))
3972 (with-demoted-errors "Desktop: Problem restoring directory: %S"
3973 (dired dired-dir)
3974 ;; The following elements of `misc-data' are the keys
3975 ;; from `dired-subdir-alist'.
3976 (mapc 'dired-maybe-insert-subdir (cdr misc-data))
3977 (current-buffer))
3978 (message "Desktop: Directory %s no longer exists." dir)
3979 (when desktop-missing-file-warning (sit-for 1))
3980 nil)))
3982 (add-to-list 'desktop-buffer-mode-handlers
3983 '(dired-mode . dired-restore-desktop-buffer))
3985 (provide 'dired)
3987 (run-hooks 'dired-load-hook) ; for your customizations
3989 ;;; dired.el ends here