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