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