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