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