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