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