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