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