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