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