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