(find-file): Delete the facility of handling a prefix
[bpt/emacs.git] / lisp / dired.el
CommitLineData
c8472948 1;;; dired.el --- directory-browsing commands
52041219 2
ef746164 3;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
ab67260b 4
b36b40ae
RS
5;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
6;; Maintainer: FSF
84fc2cfa
ER
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
52041219 12;; the Free Software Foundation; either version 2, or (at your option)
84fc2cfa
ER
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
84fc2cfa 24
52041219
RS
25;;; Commentary:
26
e41b2db1
ER
27;; This is a major mode for directory browsing and editing. It is
28;; documented in the Emacs manual.
29
492d2437
RS
30;; Rewritten in 1990/1991 to add tree features, file marking and
31;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
32;; Finished up by rms in 1992.
33
52041219 34;;; Code:
492d2437 35
492d2437
RS
36;;; Customizable variables
37
286c247d
RS
38(defgroup dired nil
39 "Directory editing."
40 :group 'environment)
41
42(defgroup dired-mark nil
43 "Handling marks in dired."
44 :prefix "dired-"
45 :group 'dired)
46
47
84fc2cfa 48;;;###autoload
286c247d 49(defcustom dired-listing-switches "-al"
492d2437
RS
50 "*Switches passed to `ls' for dired. MUST contain the `l' option.
51May contain all other options that don't contradict `-l';
b36b40ae 52may contain even `F', `b', `i' and `s'. See also the variable
286c247d
RS
53`dired-ls-F-marks-symlinks' concerning the `F' switch."
54 :type 'string
55 :group 'dired)
84fc2cfa 56
492d2437
RS
57; Don't use absolute paths as /bin should be in any PATH and people
58; may prefer /usr/local/gnu/bin or whatever. However, chown is
59; usually not in PATH.
60
61;;;###autoload
84fc2cfa 62(defvar dired-chown-program
64202797 63 (if (memq system-type '(hpux dgux usg-unix-v irix linux gnu/linux))
6702f50d
RS
64 "chown"
65 (if (file-exists-p "/usr/sbin/chown")
66 "/usr/sbin/chown"
67 "/etc/chown"))
eb8c3be9 68 "Name of chown command (usually `chown' or `/etc/chown').")
492d2437 69
723ddb1f
KH
70(defvar dired-chmod-program
71 (if (eq system-type 'windows-nt)
72 "chmode" "chmod")
73 "Name of chmod command (usually `chmod' or `chmode').")
74
492d2437 75;;;###autoload
286c247d 76(defcustom dired-ls-F-marks-symlinks nil
492d2437 77 "*Informs dired about how `ls -lF' marks symbolic links.
fc83abf7
RS
78Set this to t if `ls' (or whatever program is specified by
79`insert-directory-program') with `-lF' marks the symbolic link
492d2437 80itself with a trailing @ (usually the case under Ultrix).
84fc2cfa 81
492d2437
RS
82Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
83nil (the default), if it gives `bar@ -> foo', set it to t.
84
85Dired checks if there is really a @ appended. Thus, if you have a
86marking `ls' program on one host and a non-marking on another host, and
87don't care about symbolic links which really end in a @, you can
286c247d
RS
88always set this variable to t."
89 :type 'boolean
90 :group 'dired-mark)
492d2437
RS
91
92;;;###autoload
286c247d 93(defcustom dired-trivial-filenames "^\\.\\.?$\\|^#"
492d2437
RS
94 "*Regexp of files to skip when finding first file of a directory.
95A value of nil means move to the subdir line.
286c247d
RS
96A value of t means move to first file."
97 :type '(choice (const :tag "Move to subdir" nil)
98 (const :tag "Move to first" t)
99 regexp)
100 :group 'dired)
492d2437
RS
101
102;;;###autoload
286c247d 103(defcustom dired-keep-marker-rename t
492d2437
RS
104 ;; Use t as default so that moved files "take their markers with them".
105 "*Controls marking of renamed files.
106If t, files keep their previous marks when they are renamed.
107If a character, renamed files (whether previously marked or not)
286c247d
RS
108are afterward marked with that character."
109 :type '(choice (const :tag "Keep" t)
110 (character :tag "Mark"))
111 :group 'dired-mark)
492d2437
RS
112
113;;;###autoload
286c247d 114(defcustom dired-keep-marker-copy ?C
492d2437
RS
115 "*Controls marking of copied files.
116If t, copied files are marked if and as the corresponding original files were.
286c247d
RS
117If a character, copied files are unconditionally marked with that character."
118 :type '(choice (const :tag "Keep" t)
119 (character :tag "Mark"))
120 :group 'dired-mark)
492d2437
RS
121
122;;;###autoload
286c247d 123(defcustom dired-keep-marker-hardlink ?H
492d2437
RS
124 "*Controls marking of newly made hard links.
125If t, they are marked if and as the files linked to were marked.
286c247d
RS
126If a character, new links are unconditionally marked with that character."
127 :type '(choice (const :tag "Keep" t)
128 (character :tag "Mark"))
129 :group 'dired-mark)
492d2437
RS
130
131;;;###autoload
286c247d 132(defcustom dired-keep-marker-symlink ?Y
492d2437
RS
133 "*Controls marking of newly made symbolic links.
134If t, they are marked if and as the files linked to were marked.
286c247d
RS
135If a character, new links are unconditionally marked with that character."
136 :type '(choice (const :tag "Keep" t)
137 (character :tag "Mark"))
138 :group 'dired-mark)
492d2437
RS
139
140;;;###autoload
286c247d 141(defcustom dired-dwim-target nil
492d2437
RS
142 "*If non-nil, dired tries to guess a default target directory.
143This means: if there is a dired buffer displayed in the next window,
144use its current subdir, instead of the current subdir of this dired buffer.
145
286c247d
RS
146The target is used in the prompt for file copy, rename etc."
147 :type 'boolean
148 :group 'dired)
492d2437
RS
149
150;;;###autoload
286c247d 151(defcustom dired-copy-preserve-time t
492d2437 152 "*If non-nil, Dired preserves the last-modified time in a file copy.
286c247d
RS
153\(This works on only some systems.)"
154 :type 'boolean
155 :group 'dired)
492d2437
RS
156
157;;; Hook variables
158
159(defvar dired-load-hook nil
160 "Run after loading dired.
161You can customize key bindings or load extensions with this.")
162
163(defvar dired-mode-hook nil
164 "Run at the very end of dired-mode.")
165
166(defvar dired-before-readin-hook nil
167 "This hook is run before a dired buffer is read in (created or reverted).")
168
169(defvar dired-after-readin-hook nil
170 "Hook run after each time a file or directory is read by Dired.
171After each listing of a file or directory, this hook is run
172with the buffer narrowed to the listing.")
173;; Note this can't simply be run inside function `dired-ls' as the hook
174;; functions probably depend on the dired-subdir-alist to be OK.
175
176;;; Internal variables
177
178(defvar dired-marker-char ?* ; the answer is 42
179 ;; so that you can write things like
180 ;; (let ((dired-marker-char ?X))
181 ;; ;; great code using X markers ...
182 ;; )
183 ;; For example, commands operating on two sets of files, A and B.
184 ;; Or marking files with digits 0-9. This could implicate
185 ;; concentric sets or an order for the marked files.
186 ;; The code depends on dynamic scoping on the marker char.
187 "In Dired, the current mark character.
188This is what the `do' commands look for and what the `mark' commands store.")
189
190(defvar dired-del-marker ?D
191 "Character used to flag files for deletion.")
192
193(defvar dired-shrink-to-fit
ab67260b
RS
194 t
195;; I see no reason ever to make this nil -- rms.
196;; (> baud-rate search-slow-speed)
492d2437
RS
197 "Non-nil means Dired shrinks the display buffer to fit the marked files.")
198
199(defvar dired-flagging-regexp nil);; Last regexp used to flag files.
200
ab67260b
RS
201(defvar dired-file-version-alist)
202
492d2437
RS
203(defvar dired-directory nil
204 "The directory name or shell wildcard that was used as argument to `ls'.
8d23c16b
ER
205Local to each dired buffer. May be a list, in which case the car is the
206directory name and the cdr is the actual files to list.")
492d2437
RS
207
208(defvar dired-actual-switches nil
209 "The value of `dired-listing-switches' used to make this buffer's text.")
210
211(defvar dired-re-inode-size "[0-9 \t]*"
212 "Regexp for optional initial inode and file size as made by `ls -i -s'.")
213
214;; These regexps must be tested at beginning-of-line, but are also
215;; used to search for next matches, so neither omitting "^" nor
216;; replacing "^" by "\n" (to make it slightly faster) will work.
217
218(defvar dired-re-mark "^[^ \n]")
219;; "Regexp matching a marked line.
220;; Important: the match ends just after the marker."
221(defvar dired-re-maybe-mark "^. ")
222(defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d"))
223(defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l"))
224(defvar dired-re-exe;; match ls permission string of an executable file
225 (mapconcat (function
226 (lambda (x)
227 (concat dired-re-maybe-mark dired-re-inode-size x)))
228 '("-[-r][-w][xs][-r][-w].[-r][-w]."
229 "-[-r][-w].[-r][-w][xs][-r][-w]."
230 "-[-r][-w].[-r][-w].[-r][-w][xst]")
231 "\\|"))
e6cecd2b 232(defvar dired-re-perms "[-bcdlps][-r][-w].[-r][-w].[-r][-w].")
492d2437
RS
233(defvar dired-re-dot "^.* \\.\\.?$")
234
38819778 235;; The subdirectory names in this list are expanded.
492d2437
RS
236(defvar dired-subdir-alist nil
237 "Association list of subdirectories and their buffer positions.
238Each subdirectory has an element: (DIRNAME . STARTMARKER).
7b2469ae
RS
239The order of elements is the reverse of the order in the buffer.
240In simple cases, this list contains one element.")
492d2437 241
cdf156a9 242(defvar dired-subdir-regexp "^. \\([^\n\r]+\\)\\(:\\)[\n\r]"
492d2437
RS
243 "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
244Subexpression 1 is the subdirectory proper, no trailing colon.
245The match starts at the beginning of the line and ends after the end
246of the line (\\n or \\r).
247Subexpression 2 must end right before the \\n or \\r.")
248
2439570d
SM
249(defvar dired-font-lock-keywords
250 (list
251 ;;
252 ;; Directory headers.
253 (list dired-subdir-regexp '(1 font-lock-type-face))
254 ;;
255 ;; We make heavy use of MATCH-ANCHORED, since the regexps don't identify the
256 ;; file name itself. We search for Dired defined regexps, and then use the
257 ;; Dired defined function `dired-move-to-filename' before searching for the
258 ;; simple regexp ".+". It is that regexp which matches the file name.
259 ;;
260 ;; Dired marks.
261 (list dired-re-mark
262 '(0 font-lock-reference-face)
263 '(".+" (dired-move-to-filename) nil (0 font-lock-warning-face)))
264 ;;
265 ;; Files that are group or world writable.
266 (list (concat dired-re-maybe-mark dired-re-inode-size
267 "\\([-d]\\(....w....\\|.......w.\\)\\)")
268 '(1 font-lock-comment-face)
269 '(".+" (dired-move-to-filename) nil (0 font-lock-comment-face)))
270 ;;
271 ;; Subdirectories.
272 (list dired-re-dir
273 '(".+" (dired-move-to-filename) nil (0 font-lock-function-name-face)))
274 ;;
275 ;; Symbolic links.
276 (list dired-re-sym
277 '(".+" (dired-move-to-filename) nil (0 font-lock-keyword-face)))
278 ;;
279 ;; Files suffixed with `completion-ignored-extensions'.
280 '(eval .
281 (let ((extensions (mapcar 'regexp-quote completion-ignored-extensions)))
282 ;; It is quicker to first find just an extension, then go back to the
283 ;; start of that file name. So we do this complex MATCH-ANCHORED form.
284 (list (concat "\\(" (mapconcat 'identity extensions "\\|") "\\|#\\)$")
285 '(".+" (dired-move-to-filename) nil (0 font-lock-string-face))))))
286 "Additional expressions to highlight in Dired mode.")
492d2437
RS
287\f
288;;; Macros must be defined before they are used, for the byte compiler.
289
290;; Mark all files for which CONDITION evals to non-nil.
291;; CONDITION is evaluated on each line, with point at beginning of line.
292;; MSG is a noun phrase for the type of files being marked.
293;; It should end with a noun that can be pluralized by adding `s'.
294;; Return value is the number of files marked, or nil if none were marked.
295(defmacro dired-mark-if (predicate msg)
296 (` (let (buffer-read-only count)
297 (save-excursion
298 (setq count 0)
299 (if (, msg) (message "Marking %ss..." (, msg)))
300 (goto-char (point-min))
301 (while (not (eobp))
302 (if (, predicate)
303 (progn
304 (delete-char 1)
305 (insert dired-marker-char)
306 (setq count (1+ count))))
307 (forward-line 1))
308 (if (, msg) (message "%s %s%s %s%s."
309 count
310 (, msg)
311 (dired-plural-s count)
312 (if (eq dired-marker-char ?\040) "un" "")
313 (if (eq dired-marker-char dired-del-marker)
314 "flagged" "marked"))))
315 (and (> count 0) count))))
316
317(defmacro dired-map-over-marks (body arg &optional show-progress)
318;; "Macro: Perform BODY with point somewhere on each marked line
319;;and return a list of BODY's results.
320;;If no marked file could be found, execute BODY on the current line.
321;; If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
322;; files instead of the marked files.
323;; In that case point is dragged along. This is so that commands on
324;; the next ARG (instead of the marked) files can be chained easily.
325;; If ARG is otherwise non-nil, use current file instead.
326;;If optional third arg SHOW-PROGRESS evaluates to non-nil,
327;; redisplay the dired buffer after each file is processed.
328;;No guarantee is made about the position on the marked line.
329;; BODY must ensure this itself if it depends on this.
330;;Search starts at the beginning of the buffer, thus the car of the list
331;; corresponds to the line nearest to the buffer's bottom. This
332;; is also true for (positive and negative) integer values of ARG.
333;;BODY should not be too long as it is expanded four times."
334;;
335;;Warning: BODY must not add new lines before point - this may cause an
336;;endless loop.
337;;This warning should not apply any longer, sk 2-Sep-1991 14:10.
338 (` (prog1
339 (let (buffer-read-only case-fold-search found results)
340 (if (, arg)
341 (if (integerp (, arg))
342 (progn;; no save-excursion, want to move point.
343 (dired-repeat-over-lines
344 (, arg)
345 (function (lambda ()
346 (if (, show-progress) (sit-for 0))
347 (setq results (cons (, body) results)))))
348 (if (< (, arg) 0)
349 (nreverse results)
350 results))
351 ;; non-nil, non-integer ARG means use current file:
352 (list (, body)))
353 (let ((regexp (dired-marker-regexp)) next-position)
354 (save-excursion
355 (goto-char (point-min))
356 ;; remember position of next marked file before BODY
357 ;; can insert lines before the just found file,
358 ;; confusing us by finding the same marked file again
359 ;; and again and...
360 (setq next-position (and (re-search-forward regexp nil t)
361 (point-marker))
362 found (not (null next-position)))
363 (while next-position
364 (goto-char next-position)
365 (if (, show-progress) (sit-for 0))
366 (setq results (cons (, body) results))
367 ;; move after last match
368 (goto-char next-position)
369 (forward-line 1)
370 (set-marker next-position nil)
371 (setq next-position (and (re-search-forward regexp nil t)
372 (point-marker)))))
373 (if found
374 results
375 (list (, body))))))
376 ;; save-excursion loses, again
377 (dired-move-to-filename))))
378
379(defun dired-get-marked-files (&optional localp arg)
380 "Return the marked files' names as list of strings.
381The list is in the same order as the buffer, that is, the car is the
382 first marked file.
383Values returned are normally absolute pathnames.
384Optional arg LOCALP as in `dired-get-filename'.
385Optional second argument ARG forces to use other files. If ARG is an
386 integer, use the next ARG files. If ARG is otherwise non-nil, use
387 current file. Usually ARG comes from the current prefix arg."
84fc2cfa 388 (save-excursion
492d2437 389 (nreverse (dired-map-over-marks (dired-get-filename localp) arg))))
84fc2cfa 390
492d2437
RS
391\f
392;; Function dired-ls is redefinable for VMS, ange-ftp, Prospero or
393;; other special applications.
492d2437
RS
394\f
395;; The dired command
396
397(defun dired-read-dir-and-switches (str)
398 ;; For use in interactive.
399 (reverse (list
400 (if current-prefix-arg
401 (read-string "Dired listing switches: "
402 dired-listing-switches))
403 (read-file-name (format "Dired %s(directory): " str)
404 nil default-directory nil))))
84fc2cfa 405
492d2437 406;;;###autoload (define-key ctl-x-map "d" 'dired)
84fc2cfa 407;;;###autoload
492d2437 408(defun dired (dirname &optional switches)
84fc2cfa 409 "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
492d2437
RS
410Optional second argument SWITCHES specifies the `ls' options used.
411\(Interactively, use a prefix argument to be able to specify SWITCHES.)
412Dired displays a list of files in DIRNAME (which may also have
8d23c16b 413shell wildcards appended to select certain files). If DIRNAME is a cons,
1015daba 414its first element is taken as the directory name and the rest as an explicit
8d23c16b 415list of files to make directory entries for.
52041219 416\\<dired-mode-map>\
492d2437 417You can move around in it with the usual commands.
8d23c16b
ER
418You can flag files for deletion with \\[dired-flag-file-deletion] and then
419delete them by typing \\[dired-do-flagged-delete].
492d2437 420Type \\[describe-mode] after entering dired for more info.
84fc2cfa 421
492d2437
RS
422If DIRNAME is already in a dired buffer, that buffer is used without refresh."
423 ;; Cannot use (interactive "D") because of wildcards.
424 (interactive (dired-read-dir-and-switches ""))
425 (switch-to-buffer (dired-noselect dirname switches)))
426
427;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
84fc2cfa 428;;;###autoload
492d2437 429(defun dired-other-window (dirname &optional switches)
84fc2cfa 430 "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
492d2437
RS
431 (interactive (dired-read-dir-and-switches "in other window "))
432 (switch-to-buffer-other-window (dired-noselect dirname switches)))
84fc2cfa 433
ec03e49c
RS
434;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
435;;;###autoload
436(defun dired-other-frame (dirname &optional switches)
437 "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame."
438 (interactive (dired-read-dir-and-switches "in other frame "))
439 (switch-to-buffer-other-frame (dired-noselect dirname switches)))
440
84fc2cfa 441;;;###autoload
8d23c16b 442(defun dired-noselect (dir-or-list &optional switches)
84fc2cfa 443 "Like `dired' but returns the dired buffer as value, does not select it."
8d23c16b 444 (or dir-or-list (setq dir-or-list default-directory))
492d2437
RS
445 ;; This loses the distinction between "/foo/*/" and "/foo/*" that
446 ;; some shells make:
54df7d9a 447 (let (dirname initially-was-dirname)
8d23c16b
ER
448 (if (consp dir-or-list)
449 (setq dirname (car dir-or-list))
450 (setq dirname dir-or-list))
54df7d9a
RS
451 (setq initially-was-dirname
452 (string= (file-name-as-directory dirname) dirname))
370b6149
RS
453 (setq dirname (abbreviate-file-name
454 (expand-file-name (directory-file-name dirname))))
cb2e51f8
RS
455 (if find-file-visit-truename
456 (setq dirname (file-truename dirname)))
54df7d9a
RS
457 ;; If the argument was syntactically a directory name not a file name,
458 ;; or if it happens to name a file that is a directory,
459 ;; convert it syntactically to a directory name.
460 ;; The reason for checking initially-was-dirname
461 ;; and not just file-directory-p
462 ;; is that file-directory-p is slow over ftp.
463 (if (or initially-was-dirname (file-directory-p dirname))
464 (setq dirname (file-name-as-directory dirname)))
8d23c16b
ER
465 (if (consp dir-or-list)
466 (setq dir-or-list (cons dirname (cdr dir-or-list)))
467 (setq dir-or-list dirname))
468 (dired-internal-noselect dir-or-list switches)))
492d2437
RS
469
470;; Separate function from dired-noselect for the sake of dired-vms.el.
6a4cd605 471(defun dired-internal-noselect (dir-or-list &optional switches mode)
492d2437
RS
472 ;; If there is an existing dired buffer for DIRNAME, just leave
473 ;; buffer as it is (don't even call dired-revert).
474 ;; This saves time especially for deep trees or with ange-ftp.
475 ;; The user can type `g'easily, and it is more consistent with find-file.
476 ;; But if SWITCHES are given they are probably different from the
477 ;; buffer's old value, so call dired-sort-other, which does
478 ;; revert the buffer.
479 ;; A pity we can't possibly do "Directory has changed - refresh? "
480 ;; like find-file does.
6a4cd605
RS
481 ;; Optional argument MODE is passed to dired-find-buffer-nocreate,
482 ;; see there.
8d23c16b 483 (let* ((dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
6a4cd605
RS
484 ;; The following line used to use dir-or-list.
485 ;; That never found an existing buffer, in the case
486 ;; where it is a list.
487 (buffer (dired-find-buffer-nocreate dirname mode))
492d2437
RS
488 ;; note that buffer already is in dired-mode, if found
489 (new-buffer-p (not buffer))
490 (old-buf (current-buffer)))
491 (or buffer
492 (let ((default-major-mode 'fundamental-mode))
493 ;; We don't want default-major-mode to run hooks and set auto-fill
494 ;; or whatever, now that dired-mode does not
495 ;; kill-all-local-variables any longer.
496 (setq buffer (create-file-buffer (directory-file-name dirname)))))
497 (set-buffer buffer)
6cfd24f9
AS
498 (if (not new-buffer-p) ; existing buffer ...
499 (cond (switches ; ... but new switches
500 ;; file list may have changed
501 (if (consp dir-or-list)
502 (setq dired-directory dir-or-list))
503 ;; this calls dired-revert
504 (dired-sort-other switches))
505 ;; If directory has changed on disk, offer to revert.
506 ((if (let ((attributes (file-attributes dirname))
507 (modtime (visited-file-modtime)))
508 (or (eq modtime 0)
509 (not (eq (car attributes) t))
510 (and (= (car (nth 5 attributes)) (car modtime))
511 (= (nth 1 (nth 5 attributes)) (cdr modtime)))))
512 nil
952e8cb5 513 (message "%s"
212b2266
KH
514 (substitute-command-keys
515 "Directory has changed on disk; type \\[revert-buffer] to update Dired")))))
492d2437 516 ;; Else a new buffer
847aabce 517 (setq default-directory
e3cd4b53
RS
518 ;; We can do this unconditionally
519 ;; because dired-noselect ensures that the name
520 ;; is passed in directory name syntax
521 ;; if it was the name of a directory at all.
522 (file-name-directory dirname))
492d2437
RS
523 (or switches (setq switches dired-listing-switches))
524 (dired-mode dirname switches)
6a4cd605 525 (if mode (funcall mode))
492d2437
RS
526 ;; default-directory and dired-actual-switches are set now
527 ;; (buffer-local), so we can call dired-readin:
528 (let ((failed t))
529 (unwind-protect
8d23c16b 530 (progn (dired-readin dir-or-list buffer)
492d2437
RS
531 (setq failed nil))
532 ;; dired-readin can fail if parent directories are inaccessible.
533 ;; Don't leave an empty buffer around in that case.
534 (if failed (kill-buffer buffer))))
535 ;; No need to narrow since the whole buffer contains just
536 ;; dired-readin's output, nothing else. The hook can
537 ;; successfully use dired functions (e.g. dired-get-filename)
538 ;; as the subdir-alist has been built in dired-readin.
539 (run-hooks 'dired-after-readin-hook)
540 (goto-char (point-min))
541 (dired-initial-position dirname))
542 (set-buffer old-buf)
84fc2cfa
ER
543 buffer))
544
6a4cd605
RS
545(defun dired-find-buffer-nocreate (dirname &optional mode)
546 ;; This differs from dired-buffers-for-dir in that it does not consider
547 ;; subdirs of default-directory and searches for the first match only.
548 ;; Also, the major mode must be MODE.
f5b06a95 549 (let (found (blist dired-buffers)) ; was (buffer-list)
6a4cd605 550 (or mode (setq mode 'dired-mode))
492d2437 551 (while blist
6103c44e
KH
552 (if (null (buffer-name (cdr (car blist))))
553 (setq blist (cdr blist))
554 (save-excursion
555 (set-buffer (cdr (car blist)))
6a4cd605 556 (if (and (eq major-mode mode)
6cfd24f9
AS
557 (if (consp dired-directory)
558 (equal (car dired-directory) dirname)
559 (equal dired-directory dirname)))
6103c44e
KH
560 (setq found (cdr (car blist))
561 blist nil)
562 (setq blist (cdr blist))))))
492d2437
RS
563 found))
564
565\f
566;; Read in a new dired buffer
567
568;; dired-readin differs from dired-insert-subdir in that it accepts
569;; wildcards, erases the buffer, and builds the subdir-alist anew
570;; (including making it buffer-local and clearing it first).
8d23c16b 571(defun dired-readin (dir-or-list buffer)
492d2437
RS
572 ;; default-directory and dired-actual-switches must be buffer-local
573 ;; and initialized by now.
574 ;; Thus we can test (equal default-directory dirname) instead of
575 ;; (file-directory-p dirname) and save a filesystem transaction.
576 ;; Also, we can run this hook which may want to modify the switches
577 ;; based on default-directory, e.g. with ange-ftp to a SysV host
578 ;; where ls won't understand -Al switches.
8d23c16b
ER
579 (let (dirname)
580 (if (consp dir-or-list)
581 (setq dirname (car dir-or-list))
582 (setq dirname dir-or-list))
583 (setq dirname (expand-file-name dirname))
584 (if (consp dir-or-list)
585 (setq dir-or-list (cons dirname (cdr dir-or-list))))
586 (run-hooks 'dired-before-readin-hook)
587 (save-excursion
588 (message "Reading directory %s..." dirname)
589 (set-buffer buffer)
590 (let (buffer-read-only (failed t))
591 (widen)
592 (erase-buffer)
593 (dired-readin-insert dir-or-list)
594 (indent-rigidly (point-min) (point-max) 2)
595 ;; We need this to make the root dir have a header line as all
596 ;; other subdirs have:
597 (goto-char (point-min))
598 (dired-insert-headerline default-directory)
599 ;; can't run dired-after-readin-hook here, it may depend on the subdir
600 ;; alist to be OK.
601 )
602 (message "Reading directory %s...done" dirname)
8d23c16b
ER
603 ;; Must first make alist buffer local and set it to nil because
604 ;; dired-build-subdir-alist will call dired-clear-alist first
605 (set (make-local-variable 'dired-subdir-alist) nil)
8197b8bf 606 (dired-build-subdir-alist)
fa562dd5
RS
607 (let ((attributes (file-attributes dirname)))
608 (if (eq (car attributes) t)
609 (set-visited-file-modtime (nth 5 attributes))))
8197b8bf 610 (set-buffer-modified-p nil))))
492d2437
RS
611
612;; Subroutines of dired-readin
613
8d23c16b
ER
614(defun dired-readin-insert (dir-or-list)
615 ;; Just insert listing for the passed-in directory or
616 ;; directory-and-file list, assuming a clean buffer.
617 (let (dirname)
618 (if (consp dir-or-list)
619 (setq dirname (car dir-or-list))
620 (setq dirname dir-or-list))
f45da5d1
RS
621 ;; Expand before comparing in case one or both have been abbreviated.
622 (if (and (equal (expand-file-name default-directory)
623 (expand-file-name dirname))
cf39fa9b
RS
624 (not (consp dir-or-list)))
625 ;; If we are reading a whole single directory...
8d23c16b
ER
626 (dired-insert-directory dir-or-list dired-actual-switches nil t)
627 (if (not (file-readable-p
628 (directory-file-name (file-name-directory dirname))))
629 (error "Directory %s inaccessible or nonexistent" dirname)
cf39fa9b
RS
630 ;; Else assume it contains wildcards,
631 ;; unless it is an explicit list of files.
632 (dired-insert-directory dir-or-list dired-actual-switches
633 (not (listp dir-or-list)))
8d23c16b
ER
634 (save-excursion ;; insert wildcard instead of total line:
635 (goto-char (point-min))
636 (insert "wildcard " (file-name-nondirectory dirname) "\n"))))))
637
638(defun dired-insert-directory (dir-or-list switches &optional wildcard full-p)
639 ;; Do the right thing whether dir-or-list is atomic or not. If it is,
640 ;; inset all files listed in the cdr (the car is the passed-in directory
f726257e 641 ;; list).
9888aa65 642 (let ((opoint (point))
02b5d79c 643 (process-environment (copy-sequence process-environment))
9888aa65 644 end)
6492483f
RS
645 ;; We used to specify the C locale here, to force English month names;
646 ;; but this should not be necessary any more,
647 ;; with the new value of dired-move-to-filename-regexp.
cb88a3db 648 (if (consp dir-or-list)
bc9c99bc
RS
649 ;; In this case, use the file names in the cdr
650 ;; exactly as originally given to dired-noselect.
651 (mapcar
652 (function (lambda (x) (insert-directory x switches wildcard full-p)))
653 (cdr dir-or-list))
654 ;; Expand the file name here because it may have been abbreviated
655 ;; in dired-noselect.
f150bd2b 656 (insert-directory (expand-file-name dir-or-list) switches wildcard full-p))
9888aa65 657 ;; Quote certain characters, unless ls quoted them for us.
ad632492
SM
658 (if (not (string-match "b" dired-actual-switches))
659 (save-excursion
660 (setq end (point-marker))
661 (goto-char opoint)
662 (while (search-forward "\\" end t)
663 (replace-match "\\\\" nil t))
664 (goto-char opoint)
665 (while (search-forward "\^m" end t)
666 (replace-match "\\015" nil t))
667 (set-marker end nil)))
cb88a3db 668 (dired-insert-set-properties opoint (point)))
8d23c16b 669 (setq dired-directory dir-or-list))
492d2437 670
9888aa65 671;; Make the file names highlight when the mouse is on them.
cb88a3db
RS
672(defun dired-insert-set-properties (beg end)
673 (save-excursion
674 (goto-char beg)
675 (while (< (point) end)
7da0e9b4
RS
676 (condition-case nil
677 (if (dired-move-to-filename)
678 (put-text-property (point)
679 (save-excursion
680 (dired-move-to-end-of-filename)
681 (point))
682 'mouse-face 'highlight))
683 (error nil))
cb88a3db
RS
684 (forward-line 1))))
685
492d2437
RS
686(defun dired-insert-headerline (dir);; also used by dired-insert-subdir
687 ;; Insert DIR's headerline with no trailing slash, exactly like ls
688 ;; would, and put cursor where dired-build-subdir-alist puts subdir
689 ;; boundaries.
690 (save-excursion (insert " " (directory-file-name dir) ":\n")))
691
692\f
693;; Reverting a dired buffer
694
84fc2cfa 695(defun dired-revert (&optional arg noconfirm)
492d2437
RS
696 ;; Reread the dired buffer. Must also be called after
697 ;; dired-actual-switches have changed.
698 ;; Should not fail even on completely garbaged buffers.
699 ;; Preserves old cursor, marks/flags, hidden-p.
700 (widen) ; just in case user narrowed
84fc2cfa 701 (let ((opoint (point))
492d2437
RS
702 (ofile (dired-get-filename nil t))
703 (mark-alist nil) ; save marked files
704 (hidden-subdirs (dired-remember-hidden))
705 (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
706 (case-fold-search nil) ; we check for upper case ls flags
707 buffer-read-only)
708 (goto-char (point-min))
709 (setq mark-alist;; only after dired-remember-hidden since this unhides:
710 (dired-remember-marks (point-min) (point-max)))
711 ;; treat top level dir extra (it may contain wildcards)
8d23c16b
ER
712 (dired-uncache
713 (if (consp dired-directory) (car dired-directory) dired-directory))
84fc2cfa 714 (dired-readin dired-directory (current-buffer))
492d2437
RS
715 (let ((dired-after-readin-hook nil))
716 ;; don't run that hook for each subdir...
717 (dired-insert-old-subdirs old-subdir-alist))
718 (dired-mark-remembered mark-alist) ; mark files that were marked
719 ;; ... run the hook for the whole buffer, and only after markers
720 ;; have been reinserted (else omitting in dired-x would omit marked files)
721 (run-hooks 'dired-after-readin-hook) ; no need to narrow
722 (or (and ofile (dired-goto-file ofile)) ; move cursor to where it
723 (goto-char opoint)) ; was before
84fc2cfa 724 (dired-move-to-filename)
492d2437
RS
725 (save-excursion ; hide subdirs that were hidden
726 (mapcar (function (lambda (dir)
727 (if (dired-goto-subdir dir)
728 (dired-hide-subdir 1))))
729 hidden-subdirs)))
730 ;; outside of the let scope
731;;; Might as well not override the user if the user changed this.
732;;; (setq buffer-read-only t)
733 )
734
735;; Subroutines of dired-revert
736;; Some of these are also used when inserting subdirs.
737
738(defun dired-remember-marks (beg end)
739 ;; Return alist of files and their marks, from BEG to END.
740 (if selective-display ; must unhide to make this work.
741 (let (buffer-read-only)
742 (subst-char-in-region beg end ?\r ?\n)))
743 (let (fil chr alist)
744 (save-excursion
745 (goto-char beg)
746 (while (re-search-forward dired-re-mark end t)
747 (if (setq fil (dired-get-filename nil t))
748 (setq chr (preceding-char)
749 alist (cons (cons fil chr) alist)))))
750 alist))
751
752;; Mark all files remembered in ALIST.
753;; Each element of ALIST looks like (FILE . MARKERCHAR).
754(defun dired-mark-remembered (alist)
755 (let (elt fil chr)
756 (while alist
757 (setq elt (car alist)
758 alist (cdr alist)
759 fil (car elt)
760 chr (cdr elt))
761 (if (dired-goto-file fil)
762 (save-excursion
763 (beginning-of-line)
764 (delete-char 1)
765 (insert chr))))))
766
767;; Return a list of names of subdirs currently hidden.
768(defun dired-remember-hidden ()
769 (let ((l dired-subdir-alist) dir pos result)
770 (while l
771 (setq dir (car (car l))
772 pos (cdr (car l))
773 l (cdr l))
774 (goto-char pos)
775 (skip-chars-forward "^\r\n")
52041219 776 (if (eq (following-char) ?\r)
492d2437
RS
777 (setq result (cons dir result))))
778 result))
779
780;; Try to insert all subdirs that were displayed before,
781;; according to the former subdir alist OLD-SUBDIR-ALIST.
782(defun dired-insert-old-subdirs (old-subdir-alist)
783 (or (string-match "R" dired-actual-switches)
784 (let (elt dir)
785 (while old-subdir-alist
786 (setq elt (car old-subdir-alist)
787 old-subdir-alist (cdr old-subdir-alist)
788 dir (car elt))
789 (condition-case ()
715984d3
RS
790 (progn
791 (dired-uncache dir)
792 (dired-insert-subdir dir))
492d2437 793 (error nil))))))
715984d3
RS
794
795;; Remove directory DIR from any directory cache.
796(defun dired-uncache (dir)
6eaebaa2 797 (let ((handler (find-file-name-handler dir 'dired-uncache)))
715984d3
RS
798 (if handler
799 (funcall handler 'dired-uncache dir))))
492d2437
RS
800\f
801;; dired mode key bindings and initialization
84fc2cfa
ER
802
803(defvar dired-mode-map nil "Local keymap for dired-mode buffers.")
804(if dired-mode-map
805 nil
492d2437
RS
806 ;; This looks ugly when substitute-command-keys uses C-d instead d:
807 ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
808
3e1fb00f
RS
809 (let ((map (make-keymap)))
810 (suppress-keymap map)
811 (define-key map [mouse-2] 'dired-mouse-find-file-other-window)
812 ;; Commands to mark or flag certain categories of files
813 (define-key map "#" 'dired-flag-auto-save-files)
3e1fb00f 814 (define-key map "." 'dired-clean-directory)
3e1fb00f 815 (define-key map "~" 'dired-flag-backup-files)
84d3f6e8 816 (define-key map "&" 'dired-flag-garbage-files)
3e1fb00f
RS
817 ;; Upper case keys (except !) for operating on the marked files
818 (define-key map "A" 'dired-do-search)
819 (define-key map "C" 'dired-do-copy)
820 (define-key map "B" 'dired-do-byte-compile)
821 (define-key map "D" 'dired-do-delete)
822 (define-key map "G" 'dired-do-chgrp)
823 (define-key map "H" 'dired-do-hardlink)
824 (define-key map "L" 'dired-do-load)
825 (define-key map "M" 'dired-do-chmod)
826 (define-key map "O" 'dired-do-chown)
827 (define-key map "P" 'dired-do-print)
828 (define-key map "Q" 'dired-do-query-replace)
829 (define-key map "R" 'dired-do-rename)
830 (define-key map "S" 'dired-do-symlink)
831 (define-key map "X" 'dired-do-shell-command)
832 (define-key map "Z" 'dired-do-compress)
833 (define-key map "!" 'dired-do-shell-command)
834 ;; Comparison commands
835 (define-key map "=" 'dired-diff)
836 (define-key map "\M-=" 'dired-backup-diff)
837 ;; Tree Dired commands
838 (define-key map "\M-\C-?" 'dired-unmark-all-files)
839 (define-key map "\M-\C-d" 'dired-tree-down)
840 (define-key map "\M-\C-u" 'dired-tree-up)
841 (define-key map "\M-\C-n" 'dired-next-subdir)
842 (define-key map "\M-\C-p" 'dired-prev-subdir)
843 ;; move to marked files
844 (define-key map "\M-{" 'dired-prev-marked-file)
845 (define-key map "\M-}" 'dired-next-marked-file)
846 ;; Make all regexp commands share a `%' prefix:
847 ;; We used to get to the submap via a symbol dired-regexp-prefix,
848 ;; but that seems to serve little purpose, and copy-keymap
849 ;; does a better job without it.
850 (define-key map "%" nil)
851 (define-key map "%u" 'dired-upcase)
852 (define-key map "%l" 'dired-downcase)
853 (define-key map "%d" 'dired-flag-files-regexp)
854 (define-key map "%m" 'dired-mark-files-regexp)
855 (define-key map "%r" 'dired-do-rename-regexp)
856 (define-key map "%C" 'dired-do-copy-regexp)
857 (define-key map "%H" 'dired-do-hardlink-regexp)
858 (define-key map "%R" 'dired-do-rename-regexp)
859 (define-key map "%S" 'dired-do-symlink-regexp)
4a3ca6ed
RS
860 ;; Commands for marking and unmarking.
861 (define-key map "*" nil)
862 (define-key map "**" 'dired-mark-executables)
863 (define-key map "*/" 'dired-mark-directories)
864 (define-key map "*@" 'dired-mark-symlinks)
865 (define-key map "*%" 'dired-mark-files-regexp)
866 (define-key map "*c" 'dired-change-marks)
0609c998 867 (define-key map "*s" 'dired-mark-subdir-files)
4a3ca6ed
RS
868 (define-key map "*m" 'dired-mark)
869 (define-key map "*u" 'dired-unmark)
870 (define-key map "*?" 'dired-unmark-all-files)
03059963 871 (define-key map "*!" 'dired-unmark-all-files-no-query)
4a3ca6ed
RS
872 (define-key map "*\177" 'dired-unmark-backward)
873 (define-key map "*\C-n" 'dired-next-marked-file)
874 (define-key map "*\C-p" 'dired-prev-marked-file)
3e1fb00f 875 ;; Lower keys for commands not operating on all the marked files
3e1fb00f
RS
876 (define-key map "d" 'dired-flag-file-deletion)
877 (define-key map "e" 'dired-find-file)
878 (define-key map "f" 'dired-find-file)
879 (define-key map "\C-m" 'dired-advertised-find-file)
880 (define-key map "g" 'revert-buffer)
881 (define-key map "h" 'describe-mode)
882 (define-key map "i" 'dired-maybe-insert-subdir)
883 (define-key map "k" 'dired-do-kill-lines)
884 (define-key map "l" 'dired-do-redisplay)
885 (define-key map "m" 'dired-mark)
886 (define-key map "n" 'dired-next-line)
887 (define-key map "o" 'dired-find-file-other-window)
888 (define-key map "\C-o" 'dired-display-file)
889 (define-key map "p" 'dired-previous-line)
890 (define-key map "q" 'dired-quit)
891 (define-key map "s" 'dired-sort-toggle-or-edit)
892 (define-key map "u" 'dired-unmark)
893 (define-key map "v" 'dired-view-file)
894 (define-key map "x" 'dired-do-flagged-delete)
895 (define-key map "+" 'dired-create-directory)
896 ;; moving
897 (define-key map "<" 'dired-prev-dirline)
898 (define-key map ">" 'dired-next-dirline)
899 (define-key map "^" 'dired-up-directory)
900 (define-key map " " 'dired-next-line)
901 (define-key map "\C-n" 'dired-next-line)
902 (define-key map "\C-p" 'dired-previous-line)
903 (define-key map [down] 'dired-next-line)
904 (define-key map [up] 'dired-previous-line)
905 ;; hiding
906 (define-key map "$" 'dired-hide-subdir)
907 (define-key map "\M-$" 'dired-hide-all)
908 ;; misc
909 (define-key map "?" 'dired-summary)
910 (define-key map "\177" 'dired-unmark-backward)
911 (define-key map "\C-_" 'dired-undo)
912 (define-key map "\C-xu" 'dired-undo)
913
914 ;; Make menu bar items.
915
916 ;; Get rid of the Edit menu bar item to save space.
917 (define-key map [menu-bar edit] 'undefined)
918
919 (define-key map [menu-bar subdir]
920 (cons "Subdir" (make-sparse-keymap "Subdir")))
921
922 (define-key map [menu-bar subdir hide-all]
923 '("Hide All" . dired-hide-all))
924 (define-key map [menu-bar subdir hide-subdir]
925 '("Hide Subdir" . dired-hide-subdir))
926 (define-key map [menu-bar subdir tree-down]
927 '("Tree Down" . dired-tree-down))
928 (define-key map [menu-bar subdir tree-up]
929 '("Tree Up" . dired-tree-up))
930 (define-key map [menu-bar subdir up]
931 '("Up Directory" . dired-up-directory))
932 (define-key map [menu-bar subdir prev-subdir]
933 '("Prev Subdir" . dired-prev-subdir))
934 (define-key map [menu-bar subdir next-subdir]
935 '("Next Subdir" . dired-next-subdir))
936 (define-key map [menu-bar subdir prev-dirline]
937 '("Prev Dirline" . dired-prev-dirline))
938 (define-key map [menu-bar subdir next-dirline]
939 '("Next Dirline" . dired-next-dirline))
940 (define-key map [menu-bar subdir insert]
941 '("Insert This Subdir" . dired-maybe-insert-subdir))
942
943 (define-key map [menu-bar immediate]
944 (cons "Immediate" (make-sparse-keymap "Immediate")))
945
a3285795
RS
946 (define-key map [menu-bar immediate revert-buffer]
947 '("Update Buffer" . revert-buffer))
948
949 (define-key map [menu-bar immediate dashes]
950 '("--"))
951
3e1fb00f
RS
952 (define-key map [menu-bar immediate backup-diff]
953 '("Compare with Backup" . dired-backup-diff))
954 (define-key map [menu-bar immediate diff]
955 '("Diff" . dired-diff))
956 (define-key map [menu-bar immediate view]
957 '("View This File" . dired-view-file))
958 (define-key map [menu-bar immediate display]
959 '("Display in Other Window" . dired-display-file))
960 (define-key map [menu-bar immediate find-file-other-window]
961 '("Find in Other Window" . dired-find-file-other-window))
962 (define-key map [menu-bar immediate find-file]
963 '("Find This File" . dired-find-file))
964 (define-key map [menu-bar immediate create-directory]
965 '("Create Directory..." . dired-create-directory))
966
967 (define-key map [menu-bar regexp]
968 (cons "Regexp" (make-sparse-keymap "Regexp")))
969
970 (define-key map [menu-bar regexp downcase]
971 '("Downcase" . dired-downcase))
972 (define-key map [menu-bar regexp upcase]
973 '("Upcase" . dired-upcase))
974 (define-key map [menu-bar regexp hardlink]
975 '("Hardlink..." . dired-do-hardlink-regexp))
976 (define-key map [menu-bar regexp symlink]
977 '("Symlink..." . dired-do-symlink-regexp))
978 (define-key map [menu-bar regexp rename]
979 '("Rename..." . dired-do-rename-regexp))
980 (define-key map [menu-bar regexp copy]
981 '("Copy..." . dired-do-copy-regexp))
982 (define-key map [menu-bar regexp flag]
983 '("Flag..." . dired-flag-files-regexp))
984 (define-key map [menu-bar regexp mark]
985 '("Mark..." . dired-mark-files-regexp))
986
987 (define-key map [menu-bar mark]
988 (cons "Mark" (make-sparse-keymap "Mark")))
989
990 (define-key map [menu-bar mark prev]
991 '("Previous Marked" . dired-prev-marked-file))
992 (define-key map [menu-bar mark next]
993 '("Next Marked" . dired-next-marked-file))
994 (define-key map [menu-bar mark marks]
995 '("Change Marks..." . dired-change-marks))
996 (define-key map [menu-bar mark unmark-all]
997 '("Unmark All" . dired-unmark-all-files-no-query))
998 (define-key map [menu-bar mark symlinks]
999 '("Mark Symlinks" . dired-mark-symlinks))
1000 (define-key map [menu-bar mark directories]
1001 '("Mark Directories" . dired-mark-directories))
1002 (define-key map [menu-bar mark directory]
1003 '("Mark Old Backups" . dired-clean-directory))
1004 (define-key map [menu-bar mark executables]
1005 '("Mark Executables" . dired-mark-executables))
84d3f6e8
RS
1006 (define-key map [menu-bar mark garbage-files]
1007 '("Flag Garbage Files" . dired-flag-garbage-files))
3e1fb00f
RS
1008 (define-key map [menu-bar mark backup-files]
1009 '("Flag Backup Files" . dired-flag-backup-files))
1010 (define-key map [menu-bar mark auto-save-files]
1011 '("Flag Auto-save Files" . dired-flag-auto-save-files))
1012 (define-key map [menu-bar mark deletion]
1013 '("Flag" . dired-flag-file-deletion))
1014 (define-key map [menu-bar mark unmark]
1015 '("Unmark" . dired-unmark))
1016 (define-key map [menu-bar mark mark]
1017 '("Mark" . dired-mark))
1018
1019 (define-key map [menu-bar operate]
1020 (cons "Operate" (make-sparse-keymap "Operate")))
1021
1022 (define-key map [menu-bar operate query-replace]
1023 '("Query Replace in Files..." . dired-do-query-replace))
1024 (define-key map [menu-bar operate search]
1025 '("Search Files..." . dired-do-search))
1026 (define-key map [menu-bar operate chown]
1027 '("Change Owner..." . dired-do-chown))
1028 (define-key map [menu-bar operate chgrp]
1029 '("Change Group..." . dired-do-chgrp))
1030 (define-key map [menu-bar operate chmod]
1031 '("Change Mode..." . dired-do-chmod))
1032 (define-key map [menu-bar operate load]
1033 '("Load" . dired-do-load))
1034 (define-key map [menu-bar operate compile]
1035 '("Byte-compile" . dired-do-byte-compile))
1036 (define-key map [menu-bar operate compress]
1037 '("Compress" . dired-do-compress))
1038 (define-key map [menu-bar operate print]
1039 '("Print" . dired-do-print))
1040 (define-key map [menu-bar operate hardlink]
1041 '("Hardlink to..." . dired-do-hardlink))
1042 (define-key map [menu-bar operate symlink]
1043 '("Symlink to..." . dired-do-symlink))
1044 (define-key map [menu-bar operate command]
1045 '("Shell Command..." . dired-do-shell-command))
1046 (define-key map [menu-bar operate delete]
1047 '("Delete" . dired-do-delete))
1048 (define-key map [menu-bar operate rename]
1049 '("Rename to..." . dired-do-rename))
1050 (define-key map [menu-bar operate copy]
1051 '("Copy to..." . dired-do-copy))
1052
1053 (setq dired-mode-map map)))
fc53efda 1054\f
84fc2cfa
ER
1055;; Dired mode is suitable only for specially formatted data.
1056(put 'dired-mode 'mode-class 'special)
1057
492d2437
RS
1058(defun dired-mode (&optional dirname switches)
1059 "\
1060Mode for \"editing\" directory listings.
1061In dired, you are \"editing\" a list of the files in a directory and
1062 \(optionally) its subdirectories, in the format of `ls -lR'.
1063 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
1064\"Editing\" means that you can run shell commands on files, visit,
1065 compress, load or byte-compile them, change their file attributes
1066 and insert subdirectories into the same buffer. You can \"mark\"
1067 files for later commands or \"flag\" them for deletion, either file
1068 by file or all files matching certain criteria.
1069You can move using the usual cursor motion commands.\\<dired-mode-map>
1070Letters no longer insert themselves. Digits are prefix arguments.
1071Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
1072Type \\[dired-mark] to Mark a file or subdirectory for later commands.
1073 Most commands operate on the marked files and use the current file
1074 if no files are marked. Use a numeric prefix argument to operate on
1075 the next ARG (or previous -ARG if ARG<0) files, or just `1'
1076 to operate on the current file only. Prefix arguments override marks.
1077 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
1078 to see why something went wrong.
1079Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
1080Type \\[dired-unmark-backward] to back up one line and unflag.
1081Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
1082Type \\[dired-advertised-find-file] to Find the current line's file
1083 (or dired it in another buffer, if it is a directory).
1084Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
1085Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
1086Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
1087Type \\[dired-do-copy] to Copy files.
1088Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
1089Type \\[revert-buffer] to read all currently expanded directories again.
1090 This retains all marks and hides subdirs again that were hidden before.
1091SPC and DEL can be used to move down and up by lines.
1092
1093If dired ever gets confused, you can either type \\[revert-buffer] \
1094to read the
1095directories again, type \\[dired-do-redisplay] \
1096to relist a single or the marked files or a
1097subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
1098again for the directory tree.
1099
1100Customization variables (rename this buffer and type \\[describe-variable] on each line
1101for more info):
1102
1103 dired-listing-switches
1104 dired-trivial-filenames
1105 dired-shrink-to-fit
1106 dired-marker-char
1107 dired-del-marker
1108 dired-keep-marker-rename
1109 dired-keep-marker-copy
1110 dired-keep-marker-hardlink
1111 dired-keep-marker-symlink
1112
1113Hooks (use \\[describe-variable] to see their documentation):
1114
1115 dired-before-readin-hook
1116 dired-after-readin-hook
1117 dired-mode-hook
1118 dired-load-hook
1119
1120Keybindings:
84fc2cfa 1121\\{dired-mode-map}"
492d2437
RS
1122 ;; Not to be called interactively (e.g. dired-directory will be set
1123 ;; to default-directory, which is wrong with wildcards).
84fc2cfa 1124 (kill-all-local-variables)
84fc2cfa 1125 (use-local-map dired-mode-map)
492d2437
RS
1126 (dired-advertise) ; default-directory is already set
1127 (setq major-mode 'dired-mode
1128 mode-name "Dired"
3222085e 1129;; case-fold-search nil
492d2437
RS
1130 buffer-read-only t
1131 selective-display t ; for subdirectory hiding
982fa4e5 1132 mode-line-buffer-identification '("%17b"))
492d2437
RS
1133 (set (make-local-variable 'revert-buffer-function)
1134 (function dired-revert))
1135 (set (make-local-variable 'page-delimiter)
1136 "\n\n")
1137 (set (make-local-variable 'dired-directory)
1138 (or dirname default-directory))
1139 ;; list-buffers uses this to display the dir being edited in this buffer.
1140 (set (make-local-variable 'list-buffers-directory)
474ac5bb 1141 (expand-file-name dired-directory))
492d2437
RS
1142 (set (make-local-variable 'dired-actual-switches)
1143 (or switches dired-listing-switches))
4481ae96 1144 (set (make-local-variable 'font-lock-defaults) '(dired-font-lock-keywords t))
492d2437 1145 (dired-sort-other dired-actual-switches t)
84fc2cfa
ER
1146 (run-hooks 'dired-mode-hook))
1147\f
eb8c3be9 1148;; Idiosyncratic dired commands that don't deal with marks.
84fc2cfa 1149
492d2437
RS
1150(defun dired-quit ()
1151 "Bury the current dired buffer."
1152 (interactive)
1153 (bury-buffer))
84fc2cfa
ER
1154
1155(defun dired-summary ()
492d2437 1156 "Summarize basic Dired commands and show recent Dired errors."
84fc2cfa 1157 (interactive)
492d2437 1158 (dired-why)
84fc2cfa
ER
1159 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
1160 (message
50f74744 1161 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
84fc2cfa 1162
492d2437
RS
1163(defun dired-undo ()
1164 "Undo in a dired buffer.
1165This doesn't recover lost files, it just undoes changes in the buffer itself.
1166You can use it to recover marks, killed lines or subdirs.
1167In the latter case, you have to do \\[dired-build-subdir-alist] to
1168parse the buffer again."
1169 (interactive)
1170 (let (buffer-read-only)
1171 (undo)))
84fc2cfa
ER
1172
1173(defun dired-next-line (arg)
1174 "Move down lines then position at filename.
1175Optional prefix ARG says how many lines to move; default is one line."
1176 (interactive "p")
1177 (next-line arg)
1178 (dired-move-to-filename))
1179
1180(defun dired-previous-line (arg)
1181 "Move up lines then position at filename.
1182Optional prefix ARG says how many lines to move; default is one line."
1183 (interactive "p")
1184 (previous-line arg)
1185 (dired-move-to-filename))
1186
492d2437
RS
1187(defun dired-next-dirline (arg &optional opoint)
1188 "Goto ARG'th next directory file line."
1189 (interactive "p")
1190 (or opoint (setq opoint (point)))
1191 (if (if (> arg 0)
1192 (re-search-forward dired-re-dir nil t arg)
1193 (beginning-of-line)
1194 (re-search-backward dired-re-dir nil t (- arg)))
1195 (dired-move-to-filename) ; user may type `i' or `f'
1196 (goto-char opoint)
1197 (error "No more subdirectories")))
1198
1199(defun dired-prev-dirline (arg)
1200 "Goto ARG'th previous directory file line."
1201 (interactive "p")
1202 (dired-next-dirline (- arg)))
1203
ac1ce341 1204(defun dired-up-directory (&optional other-window)
492d2437
RS
1205 "Run dired on parent directory of current directory.
1206Find the parent directory either in this buffer or another buffer.
1207Creates a buffer if necessary."
ac1ce341 1208 (interactive "P")
492d2437
RS
1209 (let* ((dir (dired-current-directory))
1210 (up (file-name-directory (directory-file-name dir))))
1211 (or (dired-goto-file (directory-file-name dir))
7b2469ae
RS
1212 ;; Only try dired-goto-subdir if buffer has more than one dir.
1213 (and (cdr dired-subdir-alist)
492d2437
RS
1214 (dired-goto-subdir up))
1215 (progn
ac1ce341
EN
1216 (if other-window
1217 (dired-other-window up)
1218 (dired up))
492d2437 1219 (dired-goto-file dir)))))
84fc2cfa 1220
dbcb9389
RS
1221;; Force `f' rather than `e' in the mode doc:
1222(defalias 'dired-advertised-find-file 'dired-find-file)
94fa4eb8 1223(defun dired-find-file (&optional coding-system)
84fc2cfa 1224 "In dired, visit the file or directory named on this line."
94fa4eb8 1225 (interactive "ZCoding-system: ")
c439687b
RS
1226 (let ((file-name (file-name-sans-versions (dired-get-filename) t)))
1227 (if (file-exists-p file-name)
94fa4eb8 1228 (find-file file-name coding-system)
c439687b 1229 (error "File no longer exists; type `g' to update Dired buffer"))))
84fc2cfa 1230
dbcb9389
RS
1231(defun dired-mouse-find-file-other-window (event)
1232 "In dired, visit the file or directory name you click on."
1233 (interactive "e")
1234 (let (file)
1235 (save-excursion
1236 (set-buffer (window-buffer (posn-window (event-end event))))
1237 (save-excursion
1238 (goto-char (posn-point (event-end event)))
1239 (setq file (dired-get-filename))))
1240 (select-window (posn-window (event-end event)))
1241 (find-file-other-window (file-name-sans-versions file t))))
1242
84fc2cfa 1243(defun dired-view-file ()
492d2437
RS
1244 "In dired, examine a file in view mode, returning to dired when done.
1245When file is a directory, show it in this buffer if it is inserted;
1246otherwise, display it in another buffer."
84fc2cfa
ER
1247 (interactive)
1248 (if (file-directory-p (dired-get-filename))
7b2469ae
RS
1249 (or (and (cdr dired-subdir-alist)
1250 (dired-goto-subdir (dired-get-filename)))
492d2437 1251 (dired (dired-get-filename)))
715984d3 1252 (view-file (dired-get-filename))))
84fc2cfa
ER
1253
1254(defun dired-find-file-other-window ()
1255 "In dired, visit this file or directory in another window."
1256 (interactive)
c3554e95 1257 (find-file-other-window (file-name-sans-versions (dired-get-filename) t)))
ab67260b
RS
1258
1259(defun dired-display-file ()
1260 "In dired, display this file or directory in another window."
1261 (interactive)
c3554e95
RS
1262 (let ((file (file-name-sans-versions (dired-get-filename) t)))
1263 (display-buffer (find-file-noselect file))))
492d2437
RS
1264\f
1265;;; Functions for extracting and manipulating file names in dired buffers.
84fc2cfa
ER
1266
1267(defun dired-get-filename (&optional localp no-error-if-not-filep)
1268 "In dired, return name of file mentioned on this line.
1269Value returned normally includes the directory name.
492d2437
RS
1270Optional arg LOCALP with value `no-dir' means don't include directory
1271 name in result. A value of t means construct name relative to
1272 `default-directory', which still may contain slashes if in a subdirectory.
1273Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
1274 this line, otherwise an error occurs."
1275 (let (case-fold-search file p1 p2)
84fc2cfa 1276 (save-excursion
492d2437
RS
1277 (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
1278 (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
1279 ;; nil if no file on this line, but no-error-if-not-filep is t:
9888aa65
RS
1280 (if (setq file (and p1 p2 (buffer-substring p1 p2)))
1281 (progn
1282 ;; Get rid of the mouse-face property that file names have.
1283 (set-text-properties 0 (length file) nil file)
1284 ;; Unquote names quoted by ls or by dired-insert-directory.
1285 ;; Using read to unquote is much faster than substituting
1286 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
1287 (setq file
1288 (read
1289 (concat "\""
1290 ;; some ls -b don't escape quotes, argh!
1291 ;; This is not needed for GNU ls, though.
1292 (or (dired-string-replace-match
bc07e5cb 1293 "\\([^\\]\\|\\`\\)\"" file "\\1\\\\\"" nil t)
9888aa65
RS
1294 file)
1295 "\"")))))
492d2437
RS
1296 (if (eq localp 'no-dir)
1297 file
1298 (and file (concat (dired-current-directory localp) file)))))
1299
437fe5ae
RS
1300(defun dired-string-replace-match (regexp string newtext
1301 &optional literal global)
1302 "Replace first match of REGEXP in STRING with NEWTEXT.
1303If it does not match, nil is returned instead of the new string.
1304Optional arg LITERAL means to take NEWTEXT literally.
1305Optional arg GLOBAL means to replace all matches."
1306 (if global
856321e2
RS
1307 (let ((start 0))
1308 (while (string-match regexp string start)
1309 (let ((from-end (- (length string) (match-end 0))))
1310 (setq string (replace-match newtext t literal string))
1311 (setq start (- (length string) from-end))))
1312 string)
437fe5ae
RS
1313 (if (not (string-match regexp string 0))
1314 nil
856321e2 1315 (replace-match newtext t literal string))))
437fe5ae 1316
492d2437
RS
1317(defun dired-make-absolute (file &optional dir)
1318 ;;"Convert FILE (a pathname relative to DIR) to an absolute pathname."
1319 ;; We can't always use expand-file-name as this would get rid of `.'
1320 ;; or expand in / instead default-directory if DIR=="".
1321 ;; This should be good enough for ange-ftp, but might easily be
1322 ;; redefined (for VMS?).
1323 ;; It should be reasonably fast, though, as it is called in
1324 ;; dired-get-filename.
1325 (concat (or dir default-directory) file))
1326
827dc60d
RS
1327(defun dired-make-relative (file &optional dir ignore)
1328 "Convert FILE (an absolute file name) to a name relative to DIR.
1329If this is impossible, return FILE unchanged.
1330DIR must be a directory name, not a file name."
492d2437 1331 (or dir (setq dir default-directory))
7425bbff
RS
1332 ;; This case comes into play if default-directory is set to
1333 ;; use ~.
1334 (if (and (> (length dir) 0) (= (aref dir 0) ?~))
1335 (setq dir (expand-file-name dir)))
492d2437
RS
1336 (if (string-match (concat "^" (regexp-quote dir)) file)
1337 (substring file (match-end 0))
827dc60d
RS
1338;;; (or no-error
1339;;; (error "%s: not in directory tree growing at %s" file dir))
1340 file))
492d2437
RS
1341\f
1342;;; Functions for finding the file name in a dired buffer line.
1343
c4a7b2a4 1344(defvar dired-move-to-filename-regexp
f3636ea7
RS
1345 " [A-Za-z\xa0-\xff][A-Za-z\xa0-\xff][A-Za-z\xa0-\xff] [0-3 ][0-9]\
1346 [ 0-9][0-9][:0-9][0-9][ 0-9] "
1347 "Regular expression to match a month abbreviation followed date/time.")
9eabb7d5 1348
23e217f6
RS
1349(defvar dired-permission-flags-regexp
1350 "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
1351 "Regular expression to match the permission flags in `ls -l'.")
1352
492d2437
RS
1353;; Move to first char of filename on this line.
1354;; Returns position (point) or nil if no filename on this line."
1355(defun dired-move-to-filename (&optional raise-error eol)
1356 ;; This is the UNIX version.
1357 (or eol (setq eol (progn (end-of-line) (point))))
1358 (beginning-of-line)
d90490d8
RS
1359 (if (re-search-forward dired-move-to-filename-regexp eol t)
1360 (goto-char (match-end 0))
1361 (if raise-error
1362 (error "No file on this line"))))
492d2437
RS
1363
1364(defun dired-move-to-end-of-filename (&optional no-error)
1365 ;; Assumes point is at beginning of filename,
1366 ;; thus the rwx bit re-search-backward below will succeed in *this*
1367 ;; line if at all. So, it should be called only after
1368 ;; (dired-move-to-filename t).
1369 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
1370 ;; This is the UNIX version.
1371 (let (opoint file-type executable symlink hidden case-fold-search used-F eol)
1372 ;; case-fold-search is nil now, so we can test for capital F:
1373 (setq used-F (string-match "F" dired-actual-switches)
1374 opoint (point)
1375 eol (save-excursion (end-of-line) (point))
1376 hidden (and selective-display
1377 (save-excursion (search-forward "\r" eol t))))
1378 (if hidden
1379 nil
1380 (save-excursion;; Find out what kind of file this is:
1381 ;; Restrict perm bits to be non-blank,
1382 ;; otherwise this matches one char to early (looking backward):
1383 ;; "l---------" (some systems make symlinks that way)
1384 ;; "----------" (plain file with zero perms)
1385 (if (re-search-backward
23e217f6 1386 dired-permission-flags-regexp nil t)
492d2437
RS
1387 (setq file-type (char-after (match-beginning 1))
1388 symlink (eq file-type ?l)
1389 ;; Only with -F we need to know whether it's an executable
1390 executable (and
1391 used-F
1392 (string-match
1393 "[xst]";; execute bit set anywhere?
1394 (concat
1395 (buffer-substring (match-beginning 2)
1396 (match-end 2))
1397 (buffer-substring (match-beginning 3)
1398 (match-end 3))
1399 (buffer-substring (match-beginning 4)
1400 (match-end 4))))))
1401 (or no-error (error "No file on this line"))))
1402 ;; Move point to end of name:
1403 (if symlink
1404 (if (search-forward " ->" eol t)
1405 (progn
1406 (forward-char -3)
1407 (and used-F
1408 dired-ls-F-marks-symlinks
1409 (eq (preceding-char) ?@);; did ls really mark the link?
1410 (forward-char -1))))
1411 (goto-char eol);; else not a symbolic link
1412 ;; ls -lF marks dirs, sockets and executables with exactly one
1413 ;; trailing character. (Executable bits on symlinks ain't mean
1414 ;; a thing, even to ls, but we know it's not a symlink.)
1415 (and used-F
1416 (or (memq file-type '(?d ?s))
1417 executable)
1418 (forward-char -1))))
1419 (or no-error
1420 (not (eq opoint (point)))
1421 (error (if hidden
1422 (substitute-command-keys
1423 "File line is hidden, type \\[dired-hide-subdir] to unhide")
1424 "No file on this line")))
1425 (if (eq opoint (point))
1426 nil
1427 (point))))
1428
1429\f
1430;; Keeping Dired buffers in sync with the filesystem and with each other
1431
1432(defvar dired-buffers nil
1433 ;; Enlarged by dired-advertise
1434 ;; Queried by function dired-buffers-for-dir. When this detects a
1435 ;; killed buffer, it is removed from this list.
38819778 1436 "Alist of expanded directories and their associated dired buffers.")
492d2437 1437
ef746164 1438(defun dired-buffers-for-dir (dir &optional file)
492d2437 1439;; Return a list of buffers that dired DIR (top level or in-situ subdir).
ef746164
RS
1440;; If FILE is non-nil, include only those whose wildcard pattern (if any)
1441;; matches FILE.
492d2437
RS
1442;; The list is in reverse order of buffer creation, most recent last.
1443;; As a side effect, killed dired buffers for DIR are removed from
1444;; dired-buffers.
1445 (setq dir (file-name-as-directory dir))
ef746164 1446 (let ((alist dired-buffers) result elt buf pattern)
492d2437 1447 (while alist
38819778
RS
1448 (setq elt (car alist)
1449 buf (cdr elt))
1450 (if (buffer-name buf)
1451 (if (dired-in-this-tree dir (car elt))
ef746164
RS
1452 (with-current-buffer buf
1453 (and (assoc dir dired-subdir-alist)
1454 (or (null file)
1455 (let ((wildcards
1456 (file-name-nondirectory dired-directory)))
1457 (or (= 0 (length wildcards))
1458 (string-match (dired-glob-regexp wildcards)
1459 file))))
1460 (setq result (cons buf result)))))
38819778
RS
1461 ;; else buffer is killed - clean up:
1462 (setq dired-buffers (delq elt dired-buffers)))
492d2437
RS
1463 (setq alist (cdr alist)))
1464 result))
1465
ef746164
RS
1466(defun dired-glob-regexp (pattern)
1467 "Convert glob-pattern PATTERN to a regular expression."
1468 (let ((matched-in-pattern 0) ;; How many chars of PATTERN we've handled.
1469 regexp)
1470 (while (string-match "[[?*]" pattern matched-in-pattern)
1471 (let ((op-end (match-end 0))
1472 (next-op (aref pattern (match-beginning 0))))
1473 (setq regexp (concat regexp
1474 (regexp-quote
1475 (substring pattern matched-in-pattern
1476 (match-beginning 0)))))
1477 (cond ((= next-op ??)
1478 (setq regexp (concat regexp "."))
1479 (setq matched-in-pattern op-end))
1480 ((= next-op ?\[)
1481 ;; Fails to handle ^ yet ????
1482 (let* ((set-start (match-beginning 0))
1483 (set-cont
1484 (if (= (aref pattern (1+ set-start)) ?^)
1485 (+ 3 set-start)
1486 (+ 2 set-start)))
1487 (set-end (string-match "]" pattern set-cont))
1488 (set (substring pattern set-start (1+ set-end))))
1489 (setq regexp (concat regexp set))
1490 (setq matched-in-pattern (1+ set-end))))
1491 ((= next-op ?*)
1492 (setq regexp (concat regexp ".*"))
1493 (setq matched-in-pattern op-end)))))
1494 (concat "\\`"
1495 regexp
1496 (regexp-quote
1497 (substring pattern matched-in-pattern))
1498 "\\'")))
1499
1500
1501
492d2437
RS
1502(defun dired-advertise ()
1503 ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
1504 ;; With wildcards we actually advertise too much.
38819778
RS
1505 (let ((expanded-default (expand-file-name default-directory)))
1506 (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
1507 t ; we have already advertised ourselves
1508 (setq dired-buffers
1509 (cons (cons expanded-default (current-buffer))
1510 dired-buffers)))))
492d2437
RS
1511
1512(defun dired-unadvertise (dir)
1513 ;; Remove DIR from the buffer alist in variable dired-buffers.
1514 ;; This has the effect of removing any buffer whose main directory is DIR.
1515 ;; It does not affect buffers in which DIR is a subdir.
1516 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
1517 (setq dired-buffers
38819778 1518 (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
492d2437
RS
1519\f
1520;; Tree Dired
1521
1522;;; utility functions
1523
1524(defun dired-in-this-tree (file dir)
1525 ;;"Is FILE part of the directory tree starting at DIR?"
1526 (let (case-fold-search)
38819778 1527 (string-match (concat "^" (regexp-quote dir)) file)))
492d2437
RS
1528
1529(defun dired-normalize-subdir (dir)
1530 ;; Prepend default-directory to DIR if relative path name.
1531 ;; dired-get-filename must be able to make a valid filename from a
1532 ;; file and its directory DIR.
1533 (file-name-as-directory
1534 (if (file-name-absolute-p dir)
1535 dir
1536 (expand-file-name dir default-directory))))
1537
1538(defun dired-get-subdir ()
1539 ;;"Return the subdir name on this line, or nil if not on a headerline."
1540 ;; Look up in the alist whether this is a headerline.
1541 (save-excursion
1542 (let ((cur-dir (dired-current-directory)))
1543 (beginning-of-line) ; alist stores b-o-l positions
1544 (and (zerop (- (point)
1545 (dired-get-subdir-min (assoc cur-dir
1546 dired-subdir-alist))))
1547 cur-dir))))
1548
1549;(defun dired-get-subdir-min (elt)
1550; (cdr elt))
1551;; can't use macro, must be redefinable for other alist format in dired-nstd.
62f61df0 1552(defalias 'dired-get-subdir-min 'cdr)
492d2437
RS
1553
1554(defun dired-get-subdir-max (elt)
84fc2cfa 1555 (save-excursion
492d2437
RS
1556 (goto-char (dired-get-subdir-min elt))
1557 (dired-subdir-max)))
1558
1559(defun dired-clear-alist ()
1560 (while dired-subdir-alist
1561 (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
1562 (setq dired-subdir-alist (cdr dired-subdir-alist))))
1563
c9d59fc2
RS
1564(defun dired-subdir-index (dir)
1565 ;; Return an index into alist for use with nth
1566 ;; for the sake of subdir moving commands.
1567 (let (found (index 0) (alist dired-subdir-alist))
1568 (while alist
1569 (if (string= dir (car (car alist)))
1570 (setq alist nil found t)
1571 (setq alist (cdr alist) index (1+ index))))
1572 (if found index nil)))
1573
1574(defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
1575 "Go to next subdirectory, regardless of level."
1576 ;; Use 0 arg to go to this directory's header line.
1577 ;; NO-SKIP prevents moving to end of header line, returning whatever
1578 ;; position was found in dired-subdir-alist.
1579 (interactive "p")
1580 (let ((this-dir (dired-current-directory))
1581 pos index)
1582 ;; nth with negative arg does not return nil but the first element
1583 (setq index (- (dired-subdir-index this-dir) arg))
1584 (setq pos (if (>= index 0)
1585 (dired-get-subdir-min (nth index dired-subdir-alist))))
1586 (if pos
1587 (progn
1588 (goto-char pos)
1589 (or no-skip (skip-chars-forward "^\n\r"))
1590 (point))
1591 (if no-error-if-not-found
1592 nil ; return nil if not found
1593 (error "%s directory" (if (> arg 0) "Last" "First"))))))
1594
492d2437
RS
1595(defun dired-build-subdir-alist ()
1596 "Build `dired-subdir-alist' by parsing the buffer.
1597Returns the new value of the alist."
1598 (interactive)
1599 (dired-clear-alist)
1600 (save-excursion
3222085e
BF
1601 (let ((count 0)
1602 (buffer-read-only nil)
1603 new-dir-name)
84fc2cfa 1604 (goto-char (point-min))
492d2437 1605 (setq dired-subdir-alist nil)
acf310c9
RS
1606 (while (and (re-search-forward dired-subdir-regexp nil t)
1607 ;; Avoid taking a file name ending in a colon
1608 ;; as a subdir name.
1609 (not (save-excursion
1610 (goto-char (match-beginning 0))
1611 (beginning-of-line)
1612 (forward-char 2)
46680faf 1613 (save-match-data (looking-at dired-re-perms)))))
3222085e
BF
1614 (save-excursion
1615 (goto-char (match-beginning 1))
1616 (setq new-dir-name
1617 (expand-file-name (buffer-substring (point) (match-end 1))))
1618 (delete-region (point) (match-end 1))
1619 (insert new-dir-name))
492d2437 1620 (setq count (1+ count))
3222085e
BF
1621 (dired-alist-add-1 new-dir-name
1622 ;; Place a sub directory boundary between lines.
1623 (save-excursion
1624 (goto-char (match-beginning 0))
1625 (beginning-of-line)
1626 (point-marker))))
c9d59fc2
RS
1627 (if (> count 1)
1628 (message "Buffer includes %d directories" count))
492d2437
RS
1629 ;; We don't need to sort it because it is in buffer order per
1630 ;; constructionem. Return new alist:
1631 dired-subdir-alist)))
1632
1633(defun dired-alist-add-1 (dir new-marker)
1634 ;; Add new DIR at NEW-MARKER. Don't sort.
1635 (setq dired-subdir-alist
1636 (cons (cons (dired-normalize-subdir dir) new-marker)
1637 dired-subdir-alist)))
1638
1639(defun dired-goto-next-nontrivial-file ()
1640 ;; Position point on first nontrivial file after point.
1641 (dired-goto-next-file);; so there is a file to compare with
1642 (if (stringp dired-trivial-filenames)
1643 (while (and (not (eobp))
1644 (string-match dired-trivial-filenames
1645 (file-name-nondirectory
1646 (or (dired-get-filename nil t) ""))))
1647 (forward-line 1)
1648 (dired-move-to-filename))))
1649
1650(defun dired-goto-next-file ()
1651 (let ((max (1- (dired-subdir-max))))
1652 (while (and (not (dired-move-to-filename)) (< (point) max))
1653 (forward-line 1))))
1654
1655(defun dired-goto-file (file)
1656 "Go to file line of FILE in this dired buffer."
1657 ;; Return value of point on success, else nil.
1658 ;; FILE must be an absolute pathname.
1659 ;; Loses if FILE contains control chars like "\007" for which ls
1660 ;; either inserts "?" or "\\007" into the buffer, so we won't find
1661 ;; it in the buffer.
1662 (interactive
1663 (prog1 ; let push-mark display its message
1664 (list (expand-file-name
1665 (read-file-name "Goto file: "
1666 (dired-current-directory))))
1667 (push-mark)))
1668 (setq file (directory-file-name file)) ; does no harm if no directory
1669 (let (found case-fold-search dir)
1670 (setq dir (or (file-name-directory file)
1671 (error "Need absolute pathname for %s" file)))
1672 (save-excursion
1673 ;; The hair here is to get the result of dired-goto-subdir
1674 ;; without really calling it if we don't have any subdirs.
233993a3 1675 (if (if (string= dir (expand-file-name default-directory))
492d2437 1676 (goto-char (point-min))
7b2469ae 1677 (and (cdr dired-subdir-alist)
492d2437
RS
1678 (dired-goto-subdir dir)))
1679 (let ((base (file-name-nondirectory file))
1680 (boundary (dired-subdir-max)))
1681 (while (and (not found)
1682 ;; filenames are preceded by SPC, this makes
1683 ;; the search faster (e.g. for the filename "-"!).
1684 (search-forward (concat " " base) boundary 'move))
1685 ;; Match could have BASE just as initial substring or
1686 ;; or in permission bits or date or
1687 ;; not be a proper filename at all:
1688 (if (equal base (dired-get-filename 'no-dir t))
1689 ;; Must move to filename since an (actually
1690 ;; correct) match could have been elsewhere on the
1691 ;; ;; line (e.g. "-" would match somewhere in the
1692 ;; permission bits).
55e86af6
RS
1693 (setq found (dired-move-to-filename))
1694 ;; If this isn't the right line, move forward to avoid
1695 ;; trying this line again.
1696 (forward-line 1))))))
492d2437
RS
1697 (and found
1698 ;; return value of point (i.e., FOUND):
1699 (goto-char found))))
1700
1701(defun dired-initial-position (dirname)
1702 ;; Where point should go in a new listing of DIRNAME.
1703 ;; Point assumed at beginning of new subdir line.
1704 ;; You may redefine this function as you wish, e.g. like in dired-x.el.
1705 (end-of-line)
1706 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
1707\f
1708;; These are hooks which make tree dired work.
1709;; They are in this file because other parts of dired need to call them.
1710;; But they don't call the rest of tree dired unless there are subdirs loaded.
1711
1712;; This function is called for each retrieved filename.
1713;; It could stand to be faster, though it's mostly function call
1714;; overhead. Avoiding the function call seems to save about 10% in
1715;; dired-get-filename. Make it a defsubst?
1716(defun dired-current-directory (&optional localp)
1717 "Return the name of the subdirectory to which this line belongs.
1718This returns a string with trailing slash, like `default-directory'.
1719Optional argument means return a file name relative to `default-directory'."
1720 (let ((here (point))
1721 (alist (or dired-subdir-alist
1722 ;; probably because called in a non-dired buffer
1723 (error "No subdir-alist in %s" (current-buffer))))
1724 elt dir)
1725 (while alist
1726 (setq elt (car alist)
1727 dir (car elt)
1728 ;; use `<=' (not `<') as subdir line is part of subdir
1729 alist (if (<= (dired-get-subdir-min elt) here)
1730 nil ; found
1731 (cdr alist))))
1732 (if localp
1733 (dired-make-relative dir default-directory)
1734 dir)))
1735
1736;; Subdirs start at the beginning of their header lines and end just
1737;; before the beginning of the next header line (or end of buffer).
1738
1739(defun dired-subdir-max ()
1740 (save-excursion
7b2469ae 1741 (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
492d2437
RS
1742 (point-max)
1743 (point))))
1744\f
1745;; Deleting files
1746
7da0e9b4
RS
1747(defun dired-do-flagged-delete (&optional nomessage)
1748 "In dired, delete the files flagged for deletion.
1749If NOMESSAGE is non-nil, we don't display any message
1750if there are no flagged files."
492d2437
RS
1751 (interactive)
1752 (let* ((dired-marker-char dired-del-marker)
1753 (regexp (dired-marker-regexp))
1754 case-fold-search)
1755 (if (save-excursion (goto-char (point-min))
1756 (re-search-forward regexp nil t))
1757 (dired-internal-do-deletions
1758 ;; this can't move point since ARG is nil
1759 (dired-map-over-marks (cons (dired-get-filename) (point))
1760 nil)
1761 nil)
7da0e9b4
RS
1762 (or nomessage
1763 (message "(No deletions requested)")))))
492d2437
RS
1764
1765(defun dired-do-delete (&optional arg)
1766 "Delete all marked (or next ARG) files."
1767 ;; This is more consistent with the file marking feature than
1768 ;; dired-do-flagged-delete.
1769 (interactive "P")
1770 (dired-internal-do-deletions
1771 ;; this may move point if ARG is an integer
1772 (dired-map-over-marks (cons (dired-get-filename) (point))
1773 arg)
1774 arg))
1775
1776(defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
1777
1778(defun dired-internal-do-deletions (l arg)
1779 ;; L is an alist of files to delete, with their buffer positions.
1780 ;; ARG is the prefix arg.
1781 ;; Filenames are absolute (VMS needs this for logical search paths).
1782 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
1783 ;; That way as changes are made in the buffer they do not shift the
1784 ;; lines still to be changed, so the (point) values in L stay valid.
1785 ;; Also, for subdirs in natural order, a subdir's files are deleted
1786 ;; before the subdir itself - the other way around would not work.
1787 (let ((files (mapcar (function car) l))
1788 (count (length l))
1789 (succ 0))
1790 ;; canonicalize file list for pop up
1791 (setq files (nreverse (mapcar (function dired-make-relative) files)))
1792 (if (dired-mark-pop-up
1793 " *Deletions*" 'delete files dired-deletion-confirmer
1794 (format "Delete %s " (dired-mark-prompt arg files)))
84fc2cfa 1795 (save-excursion
492d2437
RS
1796 (let (failures);; files better be in reverse order for this loop!
1797 (while l
1798 (goto-char (cdr (car l)))
1799 (let (buffer-read-only)
1800 (condition-case err
1801 (let ((fn (car (car l))))
1802 ;; This test is equivalent to
1803 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
1804 ;; but more efficient
1805 (if (eq t (car (file-attributes fn)))
ab67260b 1806 (delete-directory fn)
492d2437
RS
1807 (delete-file fn))
1808 ;; if we get here, removing worked
1809 (setq succ (1+ succ))
1810 (message "%s of %s deletions" succ count)
1811 (delete-region (progn (beginning-of-line) (point))
1812 (progn (forward-line 1) (point)))
1813 (dired-clean-up-after-deletion fn))
1814 (error;; catch errors from failed deletions
1815 (dired-log "%s\n" err)
1816 (setq failures (cons (car (car l)) failures)))))
1817 (setq l (cdr l)))
1818 (if (not failures)
1819 (message "%d deletion%s done" count (dired-plural-s count))
1820 (dired-log-summary
1821 (format "%d of %d deletion%s failed"
1822 (length failures) count
1823 (dired-plural-s count))
1824 failures))))
1825 (message "(No deletions performed)")))
1826 (dired-move-to-filename))
1827
1828;; This is a separate function for the sake of dired-x.el.
1829(defun dired-clean-up-after-deletion (fn)
1830 ;; Clean up after a deleted file or directory FN.
7b2469ae
RS
1831 (save-excursion (and (cdr dired-subdir-alist)
1832 (dired-goto-subdir fn)
492d2437
RS
1833 (dired-kill-subdir))))
1834\f
1835;; Confirmation
1836
1837(defun dired-marker-regexp ()
1838 (concat "^" (regexp-quote (char-to-string dired-marker-char))))
1839
1840(defun dired-plural-s (count)
1841 (if (= 1 count) "" "s"))
1842
1843(defun dired-mark-prompt (arg files)
1844 ;; Return a string for use in a prompt, either the current file
1845 ;; name, or the marker and a count of marked files.
1846 (let ((count (length files)))
1847 (if (= count 1)
1848 (car files)
1849 ;; more than 1 file:
1850 (if (integerp arg)
1851 ;; abs(arg) = count
1852 ;; Perhaps this is nicer, but it also takes more screen space:
1853 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
1854 ;; count)
1855 (format "[next %d files]" arg)
1856 (format "%c [%d files]" dired-marker-char count)))))
1857
1858(defun dired-pop-to-buffer (buf)
1859 ;; Pop up buffer BUF.
1860 ;; If dired-shrink-to-fit is t, make its window fit its contents.
1861 (if (not dired-shrink-to-fit)
1862 (pop-to-buffer (get-buffer-create buf))
1863 ;; let window shrink to fit:
1864 (let ((window (selected-window))
1865 target-lines w2)
1866 (cond ;; if split-window-threshold is enabled, use the largest window
1867 ((and (> (window-height (setq w2 (get-largest-window)))
1868 split-height-threshold)
f98955ea 1869 (= (frame-width) (window-width w2)))
492d2437
RS
1870 (setq window w2))
1871 ;; if the least-recently-used window is big enough, use it
1872 ((and (> (window-height (setq w2 (get-lru-window)))
1873 (* 2 window-min-height))
f98955ea 1874 (= (frame-width) (window-width w2)))
492d2437
RS
1875 (setq window w2)))
1876 (save-excursion
1877 (set-buffer buf)
1878 (goto-char (point-max))
1879 (skip-chars-backward "\n\r\t ")
63ea14a5
RS
1880 (setq target-lines (count-lines (point-min) (point)))
1881 ;; Don't forget to count the last line.
1882 (if (not (bolp))
1883 (setq target-lines (1+ target-lines))))
492d2437 1884 (if (<= (window-height window) (* 2 window-min-height))
f98955ea 1885 ;; At this point, every window on the frame is too small to split.
492d2437
RS
1886 (setq w2 (display-buffer buf))
1887 (setq w2 (split-window window
1888 (max window-min-height
1889 (- (window-height window)
1890 (1+ (max window-min-height target-lines)))))))
1891 (set-window-buffer w2 buf)
1892 (if (< (1- (window-height w2)) target-lines)
1893 (progn
1894 (select-window w2)
1895 (enlarge-window (- target-lines (1- (window-height w2))))))
1896 (set-window-start w2 1)
1897 )))
1898
1899(defvar dired-no-confirm nil
0347d069
SM
1900 "A list of symbols for commands dired should not confirm.
1901Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress',
1902`copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink' and
1903`uncompress'.")
492d2437
RS
1904
1905(defun dired-mark-pop-up (bufname op-symbol files function &rest args)
1906 ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
1907 ;;Return FUNCTION's result on ARGS after popping up a window (in a buffer
1908 ;;named BUFNAME, nil gives \" *Marked Files*\") showing the marked
1909 ;;files. Uses function `dired-pop-to-buffer' to do that.
1910 ;; FUNCTION should not manipulate files.
1911 ;; It should only read input (an argument or confirmation).
1912 ;;The window is not shown if there is just one file or
1913 ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
1914 ;;FILES is the list of marked files."
1915 (or bufname (setq bufname " *Marked Files*"))
0347d069
SM
1916 (if (or (eq dired-no-confirm t)
1917 (memq op-symbol dired-no-confirm)
492d2437
RS
1918 (= (length files) 1))
1919 (apply function args)
1920 (save-excursion
1921 (set-buffer (get-buffer-create bufname))
1922 (erase-buffer)
a07e7c4a
RS
1923 (dired-format-columns-of-files files)
1924 (remove-text-properties (point-min) (point-max) '(mouse-face)))
492d2437
RS
1925 (save-window-excursion
1926 (dired-pop-to-buffer bufname)
1927 (apply function args))))
1928
1929(defun dired-format-columns-of-files (files)
1930 ;; Files should be in forward order for this loop.
1931 ;; i.e., (car files) = first file in buffer.
1932 ;; Returns the number of lines used.
1933 (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files))))
1934 (width (- (window-width (selected-window)) 2))
1935 (columns (max 1 (/ width maxlen)))
1936 (nfiles (length files))
1937 (rows (+ (/ nfiles columns)
1938 (if (zerop (% nfiles columns)) 0 1)))
1939 (i 0)
1940 (j 0))
1941 (setq files (nconc (copy-sequence files) ; fill up with empty fns
1942 (make-list (- (* columns rows) nfiles) "")))
1943 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
1944 (while (< j rows)
1945 (while (< i columns)
1946 (indent-to (* i maxlen))
1947 (insert (car files))
1948 (setq files (nthcdr rows files)
1949 i (1+ i)))
1950 (insert "\n")
1951 (setq i 0
1952 j (1+ j)
1953 files (cdr files)))
1954 rows))
1955\f
1956;; Commands to mark or flag file(s) at or near current line.
1957
1958(defun dired-repeat-over-lines (arg function)
1959 ;; This version skips non-file lines.
cfe89c4f 1960 (let ((pos (make-marker)))
492d2437 1961 (beginning-of-line)
cfe89c4f
RS
1962 (while (and (> arg 0) (not (eobp)))
1963 (setq arg (1- arg))
1964 (beginning-of-line)
1965 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
1966 (save-excursion
1967 (forward-line 1)
1968 (move-marker pos (1+ (point))))
1969 (save-excursion (funcall function))
1970 ;; Advance to the next line--actually, to the line that *was* next.
1971 ;; (If FUNCTION inserted some new lines in between, skip them.)
1972 (goto-char pos))
1973 (while (and (< arg 0) (not (bobp)))
1974 (setq arg (1+ arg))
1975 (forward-line -1)
1976 (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
1977 (beginning-of-line)
1978 (save-excursion (funcall function)))
1979 (move-marker pos nil)
1980 (dired-move-to-filename)))
492d2437
RS
1981
1982(defun dired-between-files ()
1983 ;; Point must be at beginning of line
1984 ;; Should be equivalent to (save-excursion (not (dired-move-to-filename)))
1985 ;; but is about 1.5..2.0 times as fast. (Actually that's not worth it)
1986 (or (looking-at "^$\\|^. *$\\|^. total\\|^. wildcard")
a8004e4b
RS
1987 (and (looking-at dired-subdir-regexp)
1988 (save-excursion (not (dired-move-to-filename))))))
492d2437
RS
1989
1990(defun dired-next-marked-file (arg &optional wrap opoint)
1991 "Move to the next marked file, wrapping around the end of the buffer."
1992 (interactive "p\np")
1993 (or opoint (setq opoint (point)));; return to where interactively started
1994 (if (if (> arg 0)
1995 (re-search-forward dired-re-mark nil t arg)
1996 (beginning-of-line)
1997 (re-search-backward dired-re-mark nil t (- arg)))
1998 (dired-move-to-filename)
1999 (if (null wrap)
2000 (progn
2001 (goto-char opoint)
2002 (error "No next marked file"))
2003 (message "(Wraparound for next marked file)")
2004 (goto-char (if (> arg 0) (point-min) (point-max)))
2005 (dired-next-marked-file arg nil opoint))))
2006
2007(defun dired-prev-marked-file (arg &optional wrap)
2008 "Move to the previous marked file, wrapping around the end of the buffer."
2009 (interactive "p\np")
2010 (dired-next-marked-file (- arg) wrap))
2011
2012(defun dired-file-marker (file)
2013 ;; Return FILE's marker, or nil if unmarked.
2014 (save-excursion
2015 (and (dired-goto-file file)
2016 (progn
2017 (beginning-of-line)
2018 (if (not (equal ?\040 (following-char)))
2019 (following-char))))))
2020
2021(defun dired-mark-files-in-region (start end)
2022 (let (buffer-read-only)
2023 (if (> start end)
2024 (error "start > end"))
2025 (goto-char start) ; assumed at beginning of line
2026 (while (< (point) end)
2027 ;; Skip subdir line and following garbage like the `total' line:
2028 (while (and (< (point) end) (dired-between-files))
2029 (forward-line 1))
2030 (if (and (not (looking-at dired-re-dot))
2031 (dired-get-filename nil t))
2032 (progn
2033 (delete-char 1)
2034 (insert dired-marker-char)))
2035 (forward-line 1))))
2036
2037(defun dired-mark (arg)
2038 "Mark the current (or next ARG) files.
2039If on a subdir headerline, mark all its files except `.' and `..'.
2040
2041Use \\[dired-unmark-all-files] to remove all marks
2042and \\[dired-unmark] on a subdir to remove the marks in
2043this subdir."
2044 (interactive "P")
277568f2 2045 (if (dired-get-subdir)
492d2437
RS
2046 (save-excursion (dired-mark-subdir-files))
2047 (let (buffer-read-only)
2048 (dired-repeat-over-lines
52041219 2049 (prefix-numeric-value arg)
492d2437
RS
2050 (function (lambda () (delete-char 1) (insert dired-marker-char)))))))
2051
2052(defun dired-unmark (arg)
2053 "Unmark the current (or next ARG) files.
2054If looking at a subdir, unmark all its files except `.' and `..'."
2055 (interactive "P")
2056 (let ((dired-marker-char ?\040))
2057 (dired-mark arg)))
2058
2059(defun dired-flag-file-deletion (arg)
2060 "In dired, flag the current line's file for deletion.
2061With prefix arg, repeat over several lines.
2062
2063If on a subdir headerline, mark all its files except `.' and `..'."
2064 (interactive "P")
2065 (let ((dired-marker-char dired-del-marker))
2066 (dired-mark arg)))
2067
2068(defun dired-unmark-backward (arg)
2069 "In dired, move up lines and remove deletion flag there.
2070Optional prefix ARG says how many lines to unflag; default is one line."
2071 (interactive "p")
2072 (dired-unmark (- arg)))
84fc2cfa 2073\f
492d2437
RS
2074;;; Commands to mark or flag files based on their characteristics or names.
2075
d2cadc4b
RS
2076(defvar dired-regexp-history nil
2077 "History list of regular expressions used in Dired commands.")
2078
2079(defun dired-read-regexp (prompt)
2080 (read-from-minibuffer prompt nil nil nil 'dired-regexp-history))
492d2437
RS
2081
2082(defun dired-mark-files-regexp (regexp &optional marker-char)
2083 "Mark all files matching REGEXP for use in later commands.
2084A prefix argument means to unmark them instead.
2085`.' and `..' are never marked.
2086
2087REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
2088object files--just `.o' will mark more than you might think."
2089 (interactive
2090 (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
2091 " files (regexp): "))
2092 (if current-prefix-arg ?\040)))
2093 (let ((dired-marker-char (or marker-char dired-marker-char)))
2094 (dired-mark-if
2095 (and (not (looking-at dired-re-dot))
2096 (not (eolp)) ; empty line
2097 (let ((fn (dired-get-filename nil t)))
2098 (and fn (string-match regexp (file-name-nondirectory fn)))))
2099 "matching file")))
2100
2101(defun dired-flag-files-regexp (regexp)
2102 "In dired, flag all files containing the specified REGEXP for deletion.
2103The match is against the non-directory part of the filename. Use `^'
2104 and `$' to anchor matches. Exclude subdirs by hiding them.
2105`.' and `..' are never flagged."
2106 (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
2107 (dired-mark-files-regexp regexp dired-del-marker))
2108
2109(defun dired-mark-symlinks (unflag-p)
2110 "Mark all symbolic links.
2111With prefix argument, unflag all those files."
2112 (interactive "P")
2113 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2114 (dired-mark-if (looking-at dired-re-sym) "symbolic link")))
2115
2116(defun dired-mark-directories (unflag-p)
2117 "Mark all directory file lines except `.' and `..'.
2118With prefix argument, unflag all those files."
2119 (interactive "P")
2120 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2121 (dired-mark-if (and (looking-at dired-re-dir)
2122 (not (looking-at dired-re-dot)))
2123 "directory file")))
2124
2125(defun dired-mark-executables (unflag-p)
2126 "Mark all executable files.
2127With prefix argument, unflag all those files."
2128 (interactive "P")
2129 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2130 (dired-mark-if (looking-at dired-re-exe) "executable file")))
2131
2132;; dired-x.el has a dired-mark-sexp interactive command: mark
2133;; files for which PREDICATE returns non-nil.
2134
2135(defun dired-flag-auto-save-files (&optional unflag-p)
84fc2cfa
ER
2136 "Flag for deletion files whose names suggest they are auto save files.
2137A prefix argument says to unflag those files instead."
2138 (interactive "P")
492d2437
RS
2139 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
2140 (dired-mark-if
2a838614 2141 ;; It is less than general to check for # here,
a91526b9
RS
2142 ;; but it's the only way this runs fast enough.
2143 (and (save-excursion (end-of-line)
536ae2dd
RS
2144 (or
2145 (eq (preceding-char) ?#)
2146 ;; Handle executables in case of -F option.
2147 ;; We need not worry about the other kinds
2148 ;; of markings that -F makes, since they won't
2149 ;; appear on real auto-save files.
2150 (if (eq (preceding-char) ?*)
2151 (progn
2152 (forward-char -1)
2153 (eq (preceding-char) ?#)))))
a91526b9
RS
2154 (not (looking-at dired-re-dir))
2155 (let ((fn (dired-get-filename t t)))
2156 (if fn (auto-save-file-name-p
2157 (file-name-nondirectory fn)))))
2158 "auto save file")))
492d2437 2159
84d3f6e8
RS
2160(defvar dired-garbage-files-regexp
2161 "\\.log$\\|\\.toc$\\|.dvi$|\\.bak$\\|\\.orig$\\|\\.rej$"
2162 "*Regular expression to match \"garbage\" files for `dired-flag-garbage-files'.")
2163
2164(defun dired-flag-garbage-files ()
2165 (interactive)
2166 "Flag for deletion all files that match `dired-garbage-files-regexp'."
2167 (dired-flag-files-regexp dired-garbage-files-regexp))
2168
492d2437
RS
2169(defun dired-flag-backup-files (&optional unflag-p)
2170 "Flag all backup files (names ending with `~') for deletion.
2171With prefix argument, unflag these files."
2172 (interactive "P")
fdee13ec 2173 (let ((dired-marker-char (if unflag-p ?\ dired-del-marker)))
492d2437 2174 (dired-mark-if
f13101e9
KH
2175 ;; Don't call backup-file-name-p unless the last character looks like
2176 ;; it might be the end of a backup file name. This isn't very general,
a91526b9
RS
2177 ;; but it's the only way this runs fast enough.
2178 (and (save-excursion (end-of-line)
f13101e9
KH
2179 ;; Handle executables in case of -F option.
2180 ;; We need not worry about the other kinds
2181 ;; of markings that -F makes, since they won't
2182 ;; appear on real backup files.
2183 (if (eq (preceding-char) ?*)
2184 (forward-char -1))
fdee13ec 2185 (eq (preceding-char) ?~))
a91526b9 2186 (not (looking-at dired-re-dir))
492d2437
RS
2187 (let ((fn (dired-get-filename t t)))
2188 (if fn (backup-file-name-p fn))))
2189 "backup file")))
2190
6482fcac
RS
2191(defun dired-change-marks (&optional old new)
2192 "Change all OLD marks to NEW marks.
2193OLD and NEW are both characters used to mark files."
2194 (interactive
2195 (let* ((cursor-in-echo-area t)
2196 (old (progn (message "Change (old mark): ") (read-char)))
2197 (new (progn (message "Change %c marks to (new mark): " old)
2198 (read-char))))
2199 (list old new)))
e4e02841
RS
2200 (if (or (eq old ?\r) (eq new ?\r))
2201 (ding)
2202 (let ((string (format "\n%c" old))
2203 (buffer-read-only))
2204 (save-excursion
2205 (goto-char (point-min))
2206 (while (search-forward string nil t)
a15a76f7
RS
2207 (if (if (= old ?\ )
2208 (save-match-data
2209 (dired-get-filename 'no-dir t))
2210 t)
2211 (subst-char-in-region (match-beginning 0)
2212 (match-end 0) old new)))))))
6482fcac 2213
b1ecd9c6
RS
2214(defun dired-unmark-all-files-no-query ()
2215 "Remove all marks from all files in the Dired buffer."
2216 (interactive)
2217 (dired-unmark-all-files ?\r))
2218
8b87a301 2219(defun dired-unmark-all-files (mark &optional arg)
b602ba2f
RS
2220 "Remove a specific mark (or any mark) from every file.
2221After this command, type the mark character to remove,
2222or type RET to remove all marks.
3585916f 2223With prefix arg, query for each marked file.
492d2437 2224Type \\[help-command] at that time for help."
b602ba2f
RS
2225 (interactive "cRemove marks (RET means all): \nP")
2226 (save-excursion
2227 (let* ((count 0)
2228 buffer-read-only case-fold-search query
2229 (string (format "\n%c" mark))
2230 (help-form "\
8b87a301
RS
2231Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
2232`!' to unmark all remaining files with no more questions."))
b602ba2f
RS
2233 (goto-char (point-min))
2234 (while (if (eq mark ?\r)
2235 (re-search-forward dired-re-mark nil t)
2236 (search-forward string nil t))
2237 (if (or (not arg)
2238 (dired-query 'query "Unmark file `%s'? "
2239 (dired-get-filename t)))
2240 (progn (subst-char-in-region (1- (point)) (point)
2241 (preceding-char) ?\ )
2242 (setq count (1+ count)))))
2243 (message (if (= count 1) "1 mark removed"
2244 "%d marks removed")
2245 count))))
492d2437 2246\f
492d2437 2247;; Logging failures operating on files, and showing the results.
84fc2cfa 2248
492d2437 2249(defvar dired-log-buffer "*Dired log*")
84fc2cfa 2250
492d2437
RS
2251(defun dired-why ()
2252 "Pop up a buffer with error log output from Dired.
2253A group of errors from a single command ends with a formfeed.
2254Thus, use \\[backward-page] to find the beginning of a group of errors."
2255 (interactive)
2256 (if (get-buffer dired-log-buffer)
2257 (let ((owindow (selected-window))
2258 (window (display-buffer (get-buffer dired-log-buffer))))
2259 (unwind-protect
6482fcac 2260 (progn
492d2437
RS
2261 (select-window window)
2262 (goto-char (point-max))
2263 (recenter -1))
2264 (select-window owindow)))))
2265
2266(defun dired-log (log &rest args)
2267 ;; Log a message or the contents of a buffer.
2268 ;; If LOG is a string and there are more args, it is formatted with
2269 ;; those ARGS. Usually the LOG string ends with a \n.
2270 ;; End each bunch of errors with (dired-log t): this inserts
2271 ;; current time and buffer, and a \f (formfeed).
2272 (let ((obuf (current-buffer)))
2273 (unwind-protect ; want to move point
2274 (progn
2275 (set-buffer (get-buffer-create dired-log-buffer))
2276 (goto-char (point-max))
2277 (let (buffer-read-only)
2278 (cond ((stringp log)
2279 (insert (if args
2280 (apply (function format) log args)
2281 log)))
2282 ((bufferp log)
2283 (insert-buffer log))
2284 ((eq t log)
2285 (insert "\n\t" (current-time-string)
2286 "\tBuffer `" (buffer-name obuf) "'\n\f\n")))))
2287 (set-buffer obuf))))
2288
2289(defun dired-log-summary (string failures)
2290 (message (if failures "%s--type ? for details (%s)"
2291 "%s--type ? for details")
2292 string failures)
2293 ;; Log a summary describing a bunch of errors.
2294 (dired-log (concat "\n" string))
2295 (dired-log t))
2296\f
2297;;; Sorting
2298
2299;; Most ls can only sort by name or by date (with -t), nothing else.
2300;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
2301;; So anything that does not contain these is sort "by name".
2302
2303(defvar dired-ls-sorting-switches "SXU"
2304 "String of `ls' switches (single letters) except `t' that influence sorting.")
2305
2306(defvar dired-sort-by-date-regexp
2307 (concat "^-[^" dired-ls-sorting-switches
2308 "]*t[^" dired-ls-sorting-switches "]*$")
2309 "Regexp recognized by dired to set `by date' mode.")
2310
2311(defvar dired-sort-by-name-regexp
2312 (concat "^-[^t" dired-ls-sorting-switches "]+$")
2313 "Regexp recognized by dired to set `by name' mode.")
2314
492d2437
RS
2315(defun dired-sort-set-modeline ()
2316 ;; Set modeline display according to dired-actual-switches.
2317 ;; Modeline display of "by name" or "by date" guarantees the user a
2318 ;; match with the corresponding regexps. Non-matching switches are
2319 ;; shown literally.
1d673d85 2320 (setq mode-name
492d2437
RS
2321 (let (case-fold-search)
2322 (cond ((string-match dired-sort-by-name-regexp dired-actual-switches)
1d673d85 2323 "Dired by name")
492d2437 2324 ((string-match dired-sort-by-date-regexp dired-actual-switches)
1d673d85 2325 "Dired by date")
492d2437 2326 (t
1d673d85 2327 (concat "Dired " dired-actual-switches)))))
45d8d39f 2328 (force-mode-line-update))
492d2437
RS
2329
2330(defun dired-sort-toggle-or-edit (&optional arg)
2331 "Toggle between sort by date/name and refresh the dired buffer.
2332With a prefix argument you can edit the current listing switches instead."
84fc2cfa 2333 (interactive "P")
492d2437
RS
2334 (if arg
2335 (dired-sort-other
2336 (read-string "ls switches (must contain -l): " dired-actual-switches))
2337 (dired-sort-toggle)))
2338
2339(defun dired-sort-toggle ()
2340 ;; Toggle between sort by date/name. Reverts the buffer.
2341 (setq dired-actual-switches
2342 (let (case-fold-search)
2343 (concat
2344 "-l"
26add1bf 2345 (dired-replace-in-string (concat "[-lt"
492d2437
RS
2346 dired-ls-sorting-switches "]")
2347 ""
2348 dired-actual-switches)
2349 (if (string-match (concat "[t" dired-ls-sorting-switches "]")
2350 dired-actual-switches)
2351 ""
2352 "t"))))
2353 (dired-sort-set-modeline)
2354 (revert-buffer))
2355
2356(defun dired-replace-in-string (regexp newtext string)
2357 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
2358 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
2359 (let ((result "") (start 0) mb me)
2360 (while (string-match regexp string start)
2361 (setq mb (match-beginning 0)
2362 me (match-end 0)
2363 result (concat result (substring string start mb) newtext)
2364 start me))
2365 (concat result (substring string start))))
2366
2367(defun dired-sort-other (switches &optional no-revert)
2368 ;; Specify new ls SWITCHES for current dired buffer. Values matching
2369 ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
2370 ;; minor mode accordingly, others appear literally in the mode line.
2371 ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
2372 (setq dired-actual-switches switches)
6cfd24f9 2373 (if (eq major-mode 'dired-mode) (dired-sort-set-modeline))
492d2437 2374 (or no-revert (revert-buffer)))
84fc2cfa 2375\f
492d2437
RS
2376;; To make this file smaller, the less common commands
2377;; go in a separate file. But autoload them here
2378;; to make the separation invisible.
2379
2380(autoload 'dired-diff "dired-aux"
2381 "Compare file at point with file FILE using `diff'.
2382FILE defaults to the file at the mark.
ab67260b 2383The prompted-for file is the first file given to `diff'."
492d2437
RS
2384 t)
2385
2386(autoload 'dired-backup-diff "dired-aux"
2387 "Diff this file with its backup file or vice versa.
2388Uses the latest backup, if there are several numerical backups.
2389If this file is a backup, diff it with its original.
ab67260b 2390The backup file is the first file given to `diff'."
492d2437
RS
2391 t)
2392
2d051399
RS
2393(autoload 'dired-clean-directory "dired-aux"
2394 "Flag numerical backups for deletion.
2395Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
2396Positive prefix arg KEEP overrides `dired-kept-versions';
2397Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
2398
2399To clear the flags on these files, you can use \\[dired-flag-backup-files]
2400with a prefix argument."
2401 t)
2402
492d2437
RS
2403(autoload 'dired-do-chmod "dired-aux"
2404 "Change the mode of the marked (or next ARG) files.
2405This calls chmod, thus symbolic modes like `g+w' are allowed."
2406 t)
2407
2408(autoload 'dired-do-chgrp "dired-aux"
2409 "Change the group of the marked (or next ARG) files."
2410 t)
2411
2412(autoload 'dired-do-chown "dired-aux"
2413 "Change the owner of the marked (or next ARG) files."
2414 t)
2415
2416(autoload 'dired-do-print "dired-aux"
2417 "Print the marked (or next ARG) files.
2418Uses the shell command coming from variables `lpr-command' and
2419`lpr-switches' as default."
2420 t)
2421
2422(autoload 'dired-do-shell-command "dired-aux"
6482fcac
RS
2423 "Run a shell command COMMAND on the marked files.
2424If no files are marked or a specific numeric prefix arg is given,
2425the next ARG files are used. Just \\[universal-argument] means the current file.
2426The prompt mentions the file(s) or the marker, as appropriate.
2427
492d2437 2428If there is output, it goes to a separate buffer.
6482fcac 2429
492d2437
RS
2430Normally the command is run on each file individually.
2431However, if there is a `*' in the command then it is run
2432just once with the entire file list substituted there.
2433
6482fcac
RS
2434No automatic redisplay of dired buffers is attempted, as there's no
2435telling what files the command may have changed. Type
2436\\[dired-do-redisplay] to redisplay the marked files.
492d2437
RS
2437
2438The shell command has the top level directory as working directory, so
2439output files usually are created there instead of in a subdir."
2440 t)
2441
492d2437
RS
2442(autoload 'dired-do-kill-lines "dired-aux"
2443 "Kill all marked lines (not the files).
2444With a prefix arg, kill all lines not marked or flagged."
2445 t)
2446
2447(autoload 'dired-do-compress "dired-aux"
2448 "Compress or uncompress marked (or next ARG) files."
2449 t)
2450
2451(autoload 'dired-do-byte-compile "dired-aux"
2452 "Byte compile marked (or next ARG) Emacs Lisp files."
2453 t)
2454
2455(autoload 'dired-do-load "dired-aux"
2456 "Load the marked (or next ARG) Emacs Lisp files."
2457 t)
2458
2459(autoload 'dired-do-redisplay "dired-aux"
2460 "Redisplay all marked (or next ARG) files.
2461If on a subdir line, redisplay that subdirectory. In that case,
2462a prefix arg lets you edit the `ls' switches used for the new listing."
2463 t)
2464
492d2437 2465(autoload 'dired-create-directory "dired-aux"
84fc2cfa 2466 "Create a directory called DIRECTORY."
492d2437 2467 t)
84fc2cfa 2468
492d2437
RS
2469(autoload 'dired-do-copy "dired-aux"
2470 "Copy all marked (or next ARG) files, or copy the current file.
2471Thus, a zero prefix argument copies nothing. But it toggles the
2472variable `dired-copy-preserve-time' (which see)."
2473 t)
2474
2475(autoload 'dired-do-symlink "dired-aux"
2476 "Make symbolic links to current file or all marked (or next ARG) files.
2477When operating on just the current file, you specify the new name.
2478When operating on multiple or marked files, you specify a directory
2479and new symbolic links are made in that directory
2480with the same names that the files currently have."
2481 t)
2482
2483(autoload 'dired-do-hardlink "dired-aux"
2484 "Add names (hard links) current file or all marked (or next ARG) files.
2485When operating on just the current file, you specify the new name.
2486When operating on multiple or marked files, you specify a directory
2487and new hard links are made in that directory
2488with the same names that the files currently have."
2489 t)
2490
2491(autoload 'dired-do-rename "dired-aux"
2492 "Rename current file or all marked (or next ARG) files.
2493When renaming just the current file, you specify the new name.
2494When renaming multiple or marked files, you specify a directory."
2495 t)
2496
2497(autoload 'dired-do-rename-regexp "dired-aux"
2498 "Rename marked files containing REGEXP to NEWNAME.
2499As each match is found, the user must type a character saying
2500 what to do with it. For directions, type \\[help-command] at that time.
2501NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
2502REGEXP defaults to the last regexp used.
2503With a zero prefix arg, renaming by regexp affects the complete
2504 pathname - usually only the non-directory part of file names is used
2505 and changed."
2506 t)
2507
2508(autoload 'dired-do-copy-regexp "dired-aux"
2509 "Copy all marked files containing REGEXP to NEWNAME.
2510See function `dired-rename-regexp' for more info."
2511 t)
2512
2513(autoload 'dired-do-hardlink-regexp "dired-aux"
2514 "Hardlink all marked files containing REGEXP to NEWNAME.
2515See function `dired-rename-regexp' for more info."
2516 t)
2517
2518(autoload 'dired-do-symlink-regexp "dired-aux"
2519 "Symlink all marked files containing REGEXP to NEWNAME.
2520See function `dired-rename-regexp' for more info."
2521 t)
2522
2523(autoload 'dired-upcase "dired-aux"
2524 "Rename all marked (or next ARG) files to upper case."
2525 t)
2526
2527(autoload 'dired-downcase "dired-aux"
2528 "Rename all marked (or next ARG) files to lower case."
2529 t)
2530
2531(autoload 'dired-maybe-insert-subdir "dired-aux"
2532 "Insert this subdirectory into the same dired buffer.
2533If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
2534 else inserts it at its natural place (as `ls -lR' would have done).
2535With a prefix arg, you may edit the ls switches used for this listing.
2536 You can add `R' to the switches to expand the whole tree starting at
2537 this subdirectory.
2538This function takes some pains to conform to `ls -lR' output."
2539 t)
2540
2541(autoload 'dired-next-subdir "dired-aux"
2542 "Go to next subdirectory, regardless of level."
2543 t)
2544
2545(autoload 'dired-prev-subdir "dired-aux"
2546 "Go to previous subdirectory, regardless of level.
2547When called interactively and not on a subdir line, go to this subdir's line."
2548 t)
2549
2550(autoload 'dired-goto-subdir "dired-aux"
2551 "Go to end of header line of DIR in this dired buffer.
2552Return value of point on success, otherwise return nil.
2553The next char is either \\n, or \\r if DIR is hidden."
2554 t)
2555
2556(autoload 'dired-mark-subdir-files "dired-aux"
2557 "Mark all files except `.' and `..'."
2558 t)
2559
2560(autoload 'dired-kill-subdir "dired-aux"
2561 "Remove all lines of current subdirectory.
2562Lower levels are unaffected."
2563 t)
2564
2565(autoload 'dired-tree-up "dired-aux"
2566 "Go up ARG levels in the dired tree."
2567 t)
2568
2569(autoload 'dired-tree-down "dired-aux"
2570 "Go down in the dired tree."
2571 t)
2572
2573(autoload 'dired-hide-subdir "dired-aux"
2574 "Hide or unhide the current subdirectory and move to next directory.
2575Optional prefix arg is a repeat factor.
2576Use \\[dired-hide-all] to (un)hide all directories."
2577 t)
2578
2579(autoload 'dired-hide-all "dired-aux"
2580 "Hide all subdirectories, leaving only their header lines.
2581If there is already something hidden, make everything visible again.
2582Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2583 t)
84fc2cfa 2584\f
492d2437
RS
2585(if (eq system-type 'vax-vms)
2586 (load "dired-vms"))
84fc2cfa 2587
52041219
RS
2588(provide 'dired)
2589
57e15005
BF
2590(run-hooks 'dired-load-hook) ; for your customizations
2591
52041219 2592;;; dired.el ends here