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