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