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