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