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