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