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