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