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