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