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