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