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