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