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