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