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