]> code.delx.au - gnu-emacs/blob - lisp/dired.el
Customized.
[gnu-emacs] / lisp / dired.el
1 ;;; dired.el --- directory-browsing commands
2
3 ;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
6 ;; Maintainer: FSF
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This is a major mode for directory browsing and editing. It is
28 ;; documented in the Emacs manual.
29
30 ;; Rewritten in 1990/1991 to add tree features, file marking and
31 ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
32 ;; Finished up by rms in 1992.
33
34 ;;; Code:
35
36 ;;; Customizable variables
37
38 (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" 'quit-window)
895 (define-key map "s" 'dired-sort-toggle-or-edit)
896 (define-key map "t" 'dired-do-toggle)
897 (define-key map "u" 'dired-unmark)
898 (define-key map "v" 'dired-view-file)
899 (define-key map "x" 'dired-do-flagged-delete)
900 (define-key map "+" 'dired-create-directory)
901 ;; moving
902 (define-key map "<" 'dired-prev-dirline)
903 (define-key map ">" 'dired-next-dirline)
904 (define-key map "^" 'dired-up-directory)
905 (define-key map " " 'dired-next-line)
906 (define-key map "\C-n" 'dired-next-line)
907 (define-key map "\C-p" 'dired-previous-line)
908 (define-key map [down] 'dired-next-line)
909 (define-key map [up] 'dired-previous-line)
910 ;; hiding
911 (define-key map "$" 'dired-hide-subdir)
912 (define-key map "\M-$" 'dired-hide-all)
913 ;; misc
914 (define-key map "?" 'dired-summary)
915 (define-key map "\177" 'dired-unmark-backward)
916 (define-key map "\C-_" 'dired-undo)
917 (define-key map "\C-xu" 'dired-undo)
918
919 ;; Make menu bar items.
920
921 ;; Get rid of the Edit menu bar item to save space.
922 (define-key map [menu-bar edit] 'undefined)
923
924 (define-key map [menu-bar subdir]
925 (cons "Subdir" (make-sparse-keymap "Subdir")))
926
927 (define-key map [menu-bar subdir hide-all]
928 '("Hide All" . dired-hide-all))
929 (define-key map [menu-bar subdir hide-subdir]
930 '("Hide Subdir" . dired-hide-subdir))
931 (define-key map [menu-bar subdir tree-down]
932 '("Tree Down" . dired-tree-down))
933 (define-key map [menu-bar subdir tree-up]
934 '("Tree Up" . dired-tree-up))
935 (define-key map [menu-bar subdir up]
936 '("Up Directory" . dired-up-directory))
937 (define-key map [menu-bar subdir prev-subdir]
938 '("Prev Subdir" . dired-prev-subdir))
939 (define-key map [menu-bar subdir next-subdir]
940 '("Next Subdir" . dired-next-subdir))
941 (define-key map [menu-bar subdir prev-dirline]
942 '("Prev Dirline" . dired-prev-dirline))
943 (define-key map [menu-bar subdir next-dirline]
944 '("Next Dirline" . dired-next-dirline))
945 (define-key map [menu-bar subdir insert]
946 '("Insert This Subdir" . dired-maybe-insert-subdir))
947
948 (define-key map [menu-bar immediate]
949 (cons "Immediate" (make-sparse-keymap "Immediate")))
950
951 (define-key map [menu-bar immediate revert-buffer]
952 '("Update Buffer" . revert-buffer))
953
954 (define-key map [menu-bar immediate dashes]
955 '("--"))
956
957 (define-key map [menu-bar immediate backup-diff]
958 '("Compare with Backup" . dired-backup-diff))
959 (define-key map [menu-bar immediate diff]
960 '("Diff" . dired-diff))
961 (define-key map [menu-bar immediate view]
962 '("View This File" . dired-view-file))
963 (define-key map [menu-bar immediate display]
964 '("Display in Other Window" . dired-display-file))
965 (define-key map [menu-bar immediate find-file-other-window]
966 '("Find in Other Window" . dired-find-file-other-window))
967 (define-key map [menu-bar immediate find-file]
968 '("Find This File" . dired-find-file))
969 (define-key map [menu-bar immediate create-directory]
970 '("Create Directory..." . dired-create-directory))
971
972 (define-key map [menu-bar regexp]
973 (cons "Regexp" (make-sparse-keymap "Regexp")))
974
975 (define-key map [menu-bar regexp downcase]
976 '("Downcase" . dired-downcase))
977 (define-key map [menu-bar regexp upcase]
978 '("Upcase" . dired-upcase))
979 (define-key map [menu-bar regexp hardlink]
980 '("Hardlink..." . dired-do-hardlink-regexp))
981 (define-key map [menu-bar regexp symlink]
982 '("Symlink..." . dired-do-symlink-regexp))
983 (define-key map [menu-bar regexp rename]
984 '("Rename..." . dired-do-rename-regexp))
985 (define-key map [menu-bar regexp copy]
986 '("Copy..." . dired-do-copy-regexp))
987 (define-key map [menu-bar regexp flag]
988 '("Flag..." . dired-flag-files-regexp))
989 (define-key map [menu-bar regexp mark]
990 '("Mark..." . dired-mark-files-regexp))
991
992 (define-key map [menu-bar mark]
993 (cons "Mark" (make-sparse-keymap "Mark")))
994
995 (define-key map [menu-bar mark prev]
996 '("Previous Marked" . dired-prev-marked-file))
997 (define-key map [menu-bar mark next]
998 '("Next Marked" . dired-next-marked-file))
999 (define-key map [menu-bar mark marks]
1000 '("Change Marks..." . dired-change-marks))
1001 (define-key map [menu-bar mark unmark-all]
1002 '("Unmark All" . dired-unmark-all-marks))
1003 (define-key map [menu-bar mark symlinks]
1004 '("Mark Symlinks" . dired-mark-symlinks))
1005 (define-key map [menu-bar mark directories]
1006 '("Mark Directories" . dired-mark-directories))
1007 (define-key map [menu-bar mark directory]
1008 '("Mark Old Backups" . dired-clean-directory))
1009 (define-key map [menu-bar mark executables]
1010 '("Mark Executables" . dired-mark-executables))
1011 (define-key map [menu-bar mark garbage-files]
1012 '("Flag Garbage Files" . dired-flag-garbage-files))
1013 (define-key map [menu-bar mark backup-files]
1014 '("Flag Backup Files" . dired-flag-backup-files))
1015 (define-key map [menu-bar mark auto-save-files]
1016 '("Flag Auto-save Files" . dired-flag-auto-save-files))
1017 (define-key map [menu-bar mark deletion]
1018 '("Flag" . dired-flag-file-deletion))
1019 (define-key map [menu-bar mark unmark]
1020 '("Unmark" . dired-unmark))
1021 (define-key map [menu-bar mark mark]
1022 '("Mark" . dired-mark))
1023 (define-key map [menu-bar mark toggle-marks]
1024 '("Toggle Marks" . dired-do-toggle))
1025
1026 (define-key map [menu-bar operate]
1027 (cons "Operate" (make-sparse-keymap "Operate")))
1028
1029 (define-key map [menu-bar operate query-replace]
1030 '("Query Replace in Files..." . dired-do-query-replace))
1031 (define-key map [menu-bar operate search]
1032 '("Search Files..." . dired-do-search))
1033 (define-key map [menu-bar operate chown]
1034 '("Change Owner..." . dired-do-chown))
1035 (define-key map [menu-bar operate chgrp]
1036 '("Change Group..." . dired-do-chgrp))
1037 (define-key map [menu-bar operate chmod]
1038 '("Change Mode..." . dired-do-chmod))
1039 (define-key map [menu-bar operate load]
1040 '("Load" . dired-do-load))
1041 (define-key map [menu-bar operate compile]
1042 '("Byte-compile" . dired-do-byte-compile))
1043 (define-key map [menu-bar operate compress]
1044 '("Compress" . dired-do-compress))
1045 (define-key map [menu-bar operate print]
1046 '("Print" . dired-do-print))
1047 (define-key map [menu-bar operate hardlink]
1048 '("Hardlink to..." . dired-do-hardlink))
1049 (define-key map [menu-bar operate symlink]
1050 '("Symlink to..." . dired-do-symlink))
1051 (define-key map [menu-bar operate command]
1052 '("Shell Command..." . dired-do-shell-command))
1053 (define-key map [menu-bar operate delete]
1054 '("Delete" . dired-do-delete))
1055 (define-key map [menu-bar operate rename]
1056 '("Rename to..." . dired-do-rename))
1057 (define-key map [menu-bar operate copy]
1058 '("Copy to..." . dired-do-copy))
1059
1060 (setq dired-mode-map map)))
1061 \f
1062 ;; Dired mode is suitable only for specially formatted data.
1063 (put 'dired-mode 'mode-class 'special)
1064
1065 (defun dired-mode (&optional dirname switches)
1066 "\
1067 Mode for \"editing\" directory listings.
1068 In dired, you are \"editing\" a list of the files in a directory and
1069 \(optionally) its subdirectories, in the format of `ls -lR'.
1070 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
1071 \"Editing\" means that you can run shell commands on files, visit,
1072 compress, load or byte-compile them, change their file attributes
1073 and insert subdirectories into the same buffer. You can \"mark\"
1074 files for later commands or \"flag\" them for deletion, either file
1075 by file or all files matching certain criteria.
1076 You can move using the usual cursor motion commands.\\<dired-mode-map>
1077 Letters no longer insert themselves. Digits are prefix arguments.
1078 Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
1079 Type \\[dired-mark] to Mark a file or subdirectory for later commands.
1080 Most commands operate on the marked files and use the current file
1081 if no files are marked. Use a numeric prefix argument to operate on
1082 the next ARG (or previous -ARG if ARG<0) files, or just `1'
1083 to operate on the current file only. Prefix arguments override marks.
1084 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
1085 to see why something went wrong.
1086 Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
1087 Type \\[dired-unmark-backward] to back up one line and unflag.
1088 Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
1089 Type \\[dired-advertised-find-file] to Find the current line's file
1090 (or dired it in another buffer, if it is a directory).
1091 Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
1092 Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
1093 Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
1094 Type \\[dired-do-copy] to Copy files.
1095 Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
1096 Type \\[revert-buffer] to read all currently expanded directories again.
1097 This retains all marks and hides subdirs again that were hidden before.
1098 SPC and DEL can be used to move down and up by lines.
1099
1100 If dired ever gets confused, you can either type \\[revert-buffer] \
1101 to read the
1102 directories again, type \\[dired-do-redisplay] \
1103 to relist a single or the marked files or a
1104 subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
1105 again for the directory tree.
1106
1107 Customization variables (rename this buffer and type \\[describe-variable] on each line
1108 for more info):
1109
1110 dired-listing-switches
1111 dired-trivial-filenames
1112 dired-shrink-to-fit
1113 dired-marker-char
1114 dired-del-marker
1115 dired-keep-marker-rename
1116 dired-keep-marker-copy
1117 dired-keep-marker-hardlink
1118 dired-keep-marker-symlink
1119
1120 Hooks (use \\[describe-variable] to see their documentation):
1121
1122 dired-before-readin-hook
1123 dired-after-readin-hook
1124 dired-mode-hook
1125 dired-load-hook
1126
1127 Keybindings:
1128 \\{dired-mode-map}"
1129 ;; Not to be called interactively (e.g. dired-directory will be set
1130 ;; to default-directory, which is wrong with wildcards).
1131 (kill-all-local-variables)
1132 (use-local-map dired-mode-map)
1133 (dired-advertise) ; default-directory is already set
1134 (setq major-mode 'dired-mode
1135 mode-name "Dired"
1136 ;; case-fold-search nil
1137 buffer-read-only t
1138 selective-display t ; for subdirectory hiding
1139 mode-line-buffer-identification '("%17b"))
1140 (set (make-local-variable 'revert-buffer-function)
1141 (function dired-revert))
1142 (set (make-local-variable 'page-delimiter)
1143 "\n\n")
1144 (set (make-local-variable 'dired-directory)
1145 (or dirname default-directory))
1146 ;; list-buffers uses this to display the dir being edited in this buffer.
1147 (set (make-local-variable 'list-buffers-directory)
1148 (expand-file-name dired-directory))
1149 (set (make-local-variable 'dired-actual-switches)
1150 (or switches dired-listing-switches))
1151 (set (make-local-variable 'font-lock-defaults) '(dired-font-lock-keywords t))
1152 (dired-sort-other dired-actual-switches t)
1153 (run-hooks 'dired-mode-hook))
1154 \f
1155 ;; Idiosyncratic dired commands that don't deal with marks.
1156
1157 (defun dired-summary ()
1158 "Summarize basic Dired commands and show recent Dired errors."
1159 (interactive)
1160 (dired-why)
1161 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
1162 (message
1163 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
1164
1165 (defun dired-undo ()
1166 "Undo in a dired buffer.
1167 This doesn't recover lost files, it just undoes changes in the buffer itself.
1168 You can use it to recover marks, killed lines or subdirs.
1169 In the latter case, you have to do \\[dired-build-subdir-alist] to
1170 parse the buffer again."
1171 (interactive)
1172 (let (buffer-read-only)
1173 (undo)))
1174
1175 (defun dired-next-line (arg)
1176 "Move down lines then position at filename.
1177 Optional prefix ARG says how many lines to move; default is one line."
1178 (interactive "p")
1179 (next-line arg)
1180 (dired-move-to-filename))
1181
1182 (defun dired-previous-line (arg)
1183 "Move up lines then position at filename.
1184 Optional prefix ARG says how many lines to move; default is one line."
1185 (interactive "p")
1186 (previous-line arg)
1187 (dired-move-to-filename))
1188
1189 (defun dired-next-dirline (arg &optional opoint)
1190 "Goto ARG'th next directory file line."
1191 (interactive "p")
1192 (or opoint (setq opoint (point)))
1193 (if (if (> arg 0)
1194 (re-search-forward dired-re-dir nil t arg)
1195 (beginning-of-line)
1196 (re-search-backward dired-re-dir nil t (- arg)))
1197 (dired-move-to-filename) ; user may type `i' or `f'
1198 (goto-char opoint)
1199 (error "No more subdirectories")))
1200
1201 (defun dired-prev-dirline (arg)
1202 "Goto ARG'th previous directory file line."
1203 (interactive "p")
1204 (dired-next-dirline (- arg)))
1205
1206 (defun dired-up-directory (&optional other-window)
1207 "Run dired on parent directory of current directory.
1208 Find the parent directory either in this buffer or another buffer.
1209 Creates a buffer if necessary."
1210 (interactive "P")
1211 (let* ((dir (dired-current-directory))
1212 (up (file-name-directory (directory-file-name dir))))
1213 (or (dired-goto-file (directory-file-name dir))
1214 ;; Only try dired-goto-subdir if buffer has more than one dir.
1215 (and (cdr dired-subdir-alist)
1216 (dired-goto-subdir up))
1217 (progn
1218 (if other-window
1219 (dired-other-window up)
1220 (dired up))
1221 (dired-goto-file dir)))))
1222
1223 ;; Force `f' rather than `e' in the mode doc:
1224 (defalias 'dired-advertised-find-file 'dired-find-file)
1225 (defun dired-find-file ()
1226 "In dired, visit the file or directory named on this line."
1227 (interactive)
1228 (let ((file-name (file-name-sans-versions (dired-get-filename) t)))
1229 (if (file-exists-p file-name)
1230 (find-file file-name)
1231 (if (file-symlink-p file-name)
1232 (error "File is a symlink to a nonexistent target")
1233 (error "File no longer exists; type `g' to update Dired buffer")))))
1234
1235 (defun dired-mouse-find-file-other-window (event)
1236 "In dired, visit the file or directory name you click on."
1237 (interactive "e")
1238 (let (file)
1239 (save-excursion
1240 (set-buffer (window-buffer (posn-window (event-end event))))
1241 (save-excursion
1242 (goto-char (posn-point (event-end event)))
1243 (setq file (dired-get-filename))))
1244 (select-window (posn-window (event-end event)))
1245 (find-file-other-window (file-name-sans-versions file t))))
1246
1247 (defun dired-view-file ()
1248 "In dired, examine a file in view mode, returning to dired when done.
1249 When file is a directory, show it in this buffer if it is inserted;
1250 otherwise, display it in another buffer."
1251 (interactive)
1252 (if (file-directory-p (dired-get-filename))
1253 (or (and (cdr dired-subdir-alist)
1254 (dired-goto-subdir (dired-get-filename)))
1255 (dired (dired-get-filename)))
1256 (view-file (dired-get-filename))))
1257
1258 (defun dired-find-file-other-window ()
1259 "In dired, visit this file or directory in another window."
1260 (interactive)
1261 (find-file-other-window (file-name-sans-versions (dired-get-filename) t)))
1262
1263 (defun dired-display-file ()
1264 "In dired, display this file or directory in another window."
1265 (interactive)
1266 (let ((file (file-name-sans-versions (dired-get-filename) t)))
1267 (display-buffer (find-file-noselect file))))
1268 \f
1269 ;;; Functions for extracting and manipulating file names in dired buffers.
1270
1271 (defun dired-get-filename (&optional localp no-error-if-not-filep)
1272 "In dired, return name of file mentioned on this line.
1273 Value returned normally includes the directory name.
1274 Optional arg LOCALP with value `no-dir' means don't include directory
1275 name in result. A value of t means construct name relative to
1276 `default-directory', which still may contain slashes if in a subdirectory.
1277 Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
1278 this line, otherwise an error occurs."
1279 (let (case-fold-search file p1 p2)
1280 (save-excursion
1281 (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
1282 (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
1283 ;; nil if no file on this line, but no-error-if-not-filep is t:
1284 (if (setq file (and p1 p2 (buffer-substring p1 p2)))
1285 (progn
1286 ;; Get rid of the mouse-face property that file names have.
1287 (set-text-properties 0 (length file) nil file)
1288 ;; Unquote names quoted by ls or by dired-insert-directory.
1289 ;; Using read to unquote is much faster than substituting
1290 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
1291 (setq file
1292 (read
1293 (concat "\""
1294 ;; some ls -b don't escape quotes, argh!
1295 ;; This is not needed for GNU ls, though.
1296 (or (dired-string-replace-match
1297 "\\([^\\]\\|\\`\\)\"" file "\\1\\\\\"" nil t)
1298 file)
1299 "\"")))))
1300 (and file buffer-file-coding-system
1301 (not file-name-coding-system)
1302 (not default-file-name-coding-system)
1303 (setq file (encode-coding-string file buffer-file-coding-system)))
1304 (if (eq localp 'no-dir)
1305 file
1306 (and file (concat (dired-current-directory localp) file)))))
1307
1308 (defun dired-string-replace-match (regexp string newtext
1309 &optional literal global)
1310 "Replace first match of REGEXP in STRING with NEWTEXT.
1311 If it does not match, nil is returned instead of the new string.
1312 Optional arg LITERAL means to take NEWTEXT literally.
1313 Optional arg GLOBAL means to replace all matches."
1314 (if global
1315 (let ((start 0))
1316 (while (string-match regexp string start)
1317 (let ((from-end (- (length string) (match-end 0))))
1318 (setq string (replace-match newtext t literal string))
1319 (setq start (- (length string) from-end))))
1320 string)
1321 (if (not (string-match regexp string 0))
1322 nil
1323 (replace-match newtext t literal string))))
1324
1325 (defun dired-make-absolute (file &optional dir)
1326 ;;"Convert FILE (a pathname relative to DIR) to an absolute pathname."
1327 ;; We can't always use expand-file-name as this would get rid of `.'
1328 ;; or expand in / instead default-directory if DIR=="".
1329 ;; This should be good enough for ange-ftp, but might easily be
1330 ;; redefined (for VMS?).
1331 ;; It should be reasonably fast, though, as it is called in
1332 ;; dired-get-filename.
1333 (concat (or dir default-directory) file))
1334
1335 (defun dired-make-relative (file &optional dir ignore)
1336 "Convert FILE (an absolute file name) to a name relative to DIR.
1337 If this is impossible, return FILE unchanged.
1338 DIR must be a directory name, not a file name."
1339 (or dir (setq dir default-directory))
1340 ;; This case comes into play if default-directory is set to
1341 ;; use ~.
1342 (if (and (> (length dir) 0) (= (aref dir 0) ?~))
1343 (setq dir (expand-file-name dir)))
1344 (if (string-match (concat "^" (regexp-quote dir)) file)
1345 (substring file (match-end 0))
1346 ;;; (or no-error
1347 ;;; (error "%s: not in directory tree growing at %s" file dir))
1348 file))
1349 \f
1350 ;;; Functions for finding the file name in a dired buffer line.
1351
1352 (defvar dired-move-to-filename-regexp
1353 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
1354 ;; In some locales, month abbreviations are as short as 2 letters,
1355 ;; and they can be padded on the right with spaces.
1356 (month (concat l l "+ *"))
1357 ;; Recognize any non-ASCII character.
1358 ;; The purpose is to match a Kanji character.
1359 (k "[^\0-\177]")
1360 ;; (k "[^\x00-\x7f\x80-\xff]")
1361 (s " ")
1362 (yyyy "[0-9][0-9][0-9][0-9]")
1363 (mm "[ 0-1][0-9]")
1364 (dd "[ 0-3][0-9]")
1365 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
1366 (western (concat "\\(" month s dd "\\|" dd s month "\\)"
1367 s "\\(" HH:MM "\\|" s yyyy "\\)"))
1368 (japanese (concat mm k s dd k s "\\(" s HH:MM "\\|" yyyy k "\\)")))
1369 ;; Require the previous column to end in a digit.
1370 ;; This avoids recognizing `1 may 1997' as a date in the line:
1371 ;; -r--r--r-- 1 may 1997 1168 Oct 19 16:49 README
1372 (concat "[0-9]" s "\\(" western "\\|" japanese "\\)" s))
1373 "Regular expression to match up to the file name in a directory listing.
1374 The default value is designed to recognize dates and times
1375 regardless of the language.")
1376
1377 (defvar dired-permission-flags-regexp
1378 "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
1379 "Regular expression to match the permission flags in `ls -l'.")
1380
1381 ;; Move to first char of filename on this line.
1382 ;; Returns position (point) or nil if no filename on this line."
1383 (defun dired-move-to-filename (&optional raise-error eol)
1384 ;; This is the UNIX version.
1385 (or eol (setq eol (progn (end-of-line) (point))))
1386 (beginning-of-line)
1387 (if (re-search-forward dired-move-to-filename-regexp eol t)
1388 (goto-char (match-end 0))
1389 (if raise-error
1390 (error "No file on this line"))))
1391
1392 (defun dired-move-to-end-of-filename (&optional no-error)
1393 ;; Assumes point is at beginning of filename,
1394 ;; thus the rwx bit re-search-backward below will succeed in *this*
1395 ;; line if at all. So, it should be called only after
1396 ;; (dired-move-to-filename t).
1397 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
1398 ;; This is the UNIX version.
1399 (let (opoint file-type executable symlink hidden case-fold-search used-F eol)
1400 ;; case-fold-search is nil now, so we can test for capital F:
1401 (setq used-F (string-match "F" dired-actual-switches)
1402 opoint (point)
1403 eol (save-excursion (end-of-line) (point))
1404 hidden (and selective-display
1405 (save-excursion (search-forward "\r" eol t))))
1406 (if hidden
1407 nil
1408 (save-excursion;; Find out what kind of file this is:
1409 ;; Restrict perm bits to be non-blank,
1410 ;; otherwise this matches one char to early (looking backward):
1411 ;; "l---------" (some systems make symlinks that way)
1412 ;; "----------" (plain file with zero perms)
1413 (if (re-search-backward
1414 dired-permission-flags-regexp nil t)
1415 (setq file-type (char-after (match-beginning 1))
1416 symlink (eq file-type ?l)
1417 ;; Only with -F we need to know whether it's an executable
1418 executable (and
1419 used-F
1420 (string-match
1421 "[xst]";; execute bit set anywhere?
1422 (concat
1423 (buffer-substring (match-beginning 2)
1424 (match-end 2))
1425 (buffer-substring (match-beginning 3)
1426 (match-end 3))
1427 (buffer-substring (match-beginning 4)
1428 (match-end 4))))))
1429 (or no-error (error "No file on this line"))))
1430 ;; Move point to end of name:
1431 (if symlink
1432 (if (search-forward " ->" eol t)
1433 (progn
1434 (forward-char -3)
1435 (and used-F
1436 dired-ls-F-marks-symlinks
1437 (eq (preceding-char) ?@);; did ls really mark the link?
1438 (forward-char -1))))
1439 (goto-char eol);; else not a symbolic link
1440 ;; ls -lF marks dirs, sockets and executables with exactly one
1441 ;; trailing character. (Executable bits on symlinks ain't mean
1442 ;; a thing, even to ls, but we know it's not a symlink.)
1443 (and used-F
1444 (or (memq file-type '(?d ?s))
1445 executable)
1446 (forward-char -1))))
1447 (or no-error
1448 (not (eq opoint (point)))
1449 (error (if hidden
1450 (substitute-command-keys
1451 "File line is hidden, type \\[dired-hide-subdir] to unhide")
1452 "No file on this line")))
1453 (if (eq opoint (point))
1454 nil
1455 (point))))
1456
1457 \f
1458 ;; Keeping Dired buffers in sync with the filesystem and with each other
1459
1460 (defvar dired-buffers nil
1461 ;; Enlarged by dired-advertise
1462 ;; Queried by function dired-buffers-for-dir. When this detects a
1463 ;; killed buffer, it is removed from this list.
1464 "Alist of expanded directories and their associated dired buffers.")
1465
1466 (defun dired-buffers-for-dir (dir &optional file)
1467 ;; Return a list of buffers that dired DIR (top level or in-situ subdir).
1468 ;; If FILE is non-nil, include only those whose wildcard pattern (if any)
1469 ;; matches FILE.
1470 ;; The list is in reverse order of buffer creation, most recent last.
1471 ;; As a side effect, killed dired buffers for DIR are removed from
1472 ;; dired-buffers.
1473 (setq dir (file-name-as-directory dir))
1474 (let ((alist dired-buffers) result elt buf pattern)
1475 (while alist
1476 (setq elt (car alist)
1477 buf (cdr elt))
1478 (if (buffer-name buf)
1479 (if (dired-in-this-tree dir (car elt))
1480 (with-current-buffer buf
1481 (and (assoc dir dired-subdir-alist)
1482 (or (null file)
1483 (let ((wildcards
1484 (file-name-nondirectory dired-directory)))
1485 (or (= 0 (length wildcards))
1486 (string-match (dired-glob-regexp wildcards)
1487 file))))
1488 (setq result (cons buf result)))))
1489 ;; else buffer is killed - clean up:
1490 (setq dired-buffers (delq elt dired-buffers)))
1491 (setq alist (cdr alist)))
1492 result))
1493
1494 (defun dired-glob-regexp (pattern)
1495 "Convert glob-pattern PATTERN to a regular expression."
1496 (let ((matched-in-pattern 0) ;; How many chars of PATTERN we've handled.
1497 regexp)
1498 (while (string-match "[[?*]" pattern matched-in-pattern)
1499 (let ((op-end (match-end 0))
1500 (next-op (aref pattern (match-beginning 0))))
1501 (setq regexp (concat regexp
1502 (regexp-quote
1503 (substring pattern matched-in-pattern
1504 (match-beginning 0)))))
1505 (cond ((= next-op ??)
1506 (setq regexp (concat regexp "."))
1507 (setq matched-in-pattern op-end))
1508 ((= next-op ?\[)
1509 ;; Fails to handle ^ yet ????
1510 (let* ((set-start (match-beginning 0))
1511 (set-cont
1512 (if (= (aref pattern (1+ set-start)) ?^)
1513 (+ 3 set-start)
1514 (+ 2 set-start)))
1515 (set-end (string-match "]" pattern set-cont))
1516 (set (substring pattern set-start (1+ set-end))))
1517 (setq regexp (concat regexp set))
1518 (setq matched-in-pattern (1+ set-end))))
1519 ((= next-op ?*)
1520 (setq regexp (concat regexp ".*"))
1521 (setq matched-in-pattern op-end)))))
1522 (concat "\\`"
1523 regexp
1524 (regexp-quote
1525 (substring pattern matched-in-pattern))
1526 "\\'")))
1527
1528
1529
1530 (defun dired-advertise ()
1531 ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
1532 ;; With wildcards we actually advertise too much.
1533 (let ((expanded-default (expand-file-name default-directory)))
1534 (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
1535 t ; we have already advertised ourselves
1536 (setq dired-buffers
1537 (cons (cons expanded-default (current-buffer))
1538 dired-buffers)))))
1539
1540 (defun dired-unadvertise (dir)
1541 ;; Remove DIR from the buffer alist in variable dired-buffers.
1542 ;; This has the effect of removing any buffer whose main directory is DIR.
1543 ;; It does not affect buffers in which DIR is a subdir.
1544 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
1545 (setq dired-buffers
1546 (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
1547 \f
1548 ;; Tree Dired
1549
1550 ;;; utility functions
1551
1552 (defun dired-in-this-tree (file dir)
1553 ;;"Is FILE part of the directory tree starting at DIR?"
1554 (let (case-fold-search)
1555 (string-match (concat "^" (regexp-quote dir)) file)))
1556
1557 (defun dired-normalize-subdir (dir)
1558 ;; Prepend default-directory to DIR if relative path name.
1559 ;; dired-get-filename must be able to make a valid filename from a
1560 ;; file and its directory DIR.
1561 (file-name-as-directory
1562 (if (file-name-absolute-p dir)
1563 dir
1564 (expand-file-name dir default-directory))))
1565
1566 (defun dired-get-subdir ()
1567 ;;"Return the subdir name on this line, or nil if not on a headerline."
1568 ;; Look up in the alist whether this is a headerline.
1569 (save-excursion
1570 (let ((cur-dir (dired-current-directory)))
1571 (beginning-of-line) ; alist stores b-o-l positions
1572 (and (zerop (- (point)
1573 (dired-get-subdir-min (assoc cur-dir
1574 dired-subdir-alist))))
1575 cur-dir))))
1576
1577 ;(defun dired-get-subdir-min (elt)
1578 ; (cdr elt))
1579 ;; can't use macro, must be redefinable for other alist format in dired-nstd.
1580 (defalias 'dired-get-subdir-min 'cdr)
1581
1582 (defun dired-get-subdir-max (elt)
1583 (save-excursion
1584 (goto-char (dired-get-subdir-min elt))
1585 (dired-subdir-max)))
1586
1587 (defun dired-clear-alist ()
1588 (while dired-subdir-alist
1589 (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
1590 (setq dired-subdir-alist (cdr dired-subdir-alist))))
1591
1592 (defun dired-subdir-index (dir)
1593 ;; Return an index into alist for use with nth
1594 ;; for the sake of subdir moving commands.
1595 (let (found (index 0) (alist dired-subdir-alist))
1596 (while alist
1597 (if (string= dir (car (car alist)))
1598 (setq alist nil found t)
1599 (setq alist (cdr alist) index (1+ index))))
1600 (if found index nil)))
1601
1602 (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
1603 "Go to next subdirectory, regardless of level."
1604 ;; Use 0 arg to go to this directory's header line.
1605 ;; NO-SKIP prevents moving to end of header line, returning whatever
1606 ;; position was found in dired-subdir-alist.
1607 (interactive "p")
1608 (let ((this-dir (dired-current-directory))
1609 pos index)
1610 ;; nth with negative arg does not return nil but the first element
1611 (setq index (- (dired-subdir-index this-dir) arg))
1612 (setq pos (if (>= index 0)
1613 (dired-get-subdir-min (nth index dired-subdir-alist))))
1614 (if pos
1615 (progn
1616 (goto-char pos)
1617 (or no-skip (skip-chars-forward "^\n\r"))
1618 (point))
1619 (if no-error-if-not-found
1620 nil ; return nil if not found
1621 (error "%s directory" (if (> arg 0) "Last" "First"))))))
1622
1623 (defun dired-build-subdir-alist ()
1624 "Build `dired-subdir-alist' by parsing the buffer.
1625 Returns the new value of the alist."
1626 (interactive)
1627 (dired-clear-alist)
1628 (save-excursion
1629 (let ((count 0)
1630 (buffer-read-only nil)
1631 new-dir-name)
1632 (goto-char (point-min))
1633 (setq dired-subdir-alist nil)
1634 (while (and (re-search-forward dired-subdir-regexp nil t)
1635 ;; Avoid taking a file name ending in a colon
1636 ;; as a subdir name.
1637 (not (save-excursion
1638 (goto-char (match-beginning 0))
1639 (beginning-of-line)
1640 (forward-char 2)
1641 (save-match-data (looking-at dired-re-perms)))))
1642 (save-excursion
1643 (goto-char (match-beginning 1))
1644 (setq new-dir-name
1645 (expand-file-name (buffer-substring (point) (match-end 1))))
1646 (delete-region (point) (match-end 1))
1647 (insert new-dir-name))
1648 (setq count (1+ count))
1649 (dired-alist-add-1 new-dir-name
1650 ;; Place a sub directory boundary between lines.
1651 (save-excursion
1652 (goto-char (match-beginning 0))
1653 (beginning-of-line)
1654 (point-marker))))
1655 (if (> count 1)
1656 (message "Buffer includes %d directories" count))
1657 ;; We don't need to sort it because it is in buffer order per
1658 ;; constructionem. Return new alist:
1659 dired-subdir-alist)))
1660
1661 (defun dired-alist-add-1 (dir new-marker)
1662 ;; Add new DIR at NEW-MARKER. Don't sort.
1663 (setq dired-subdir-alist
1664 (cons (cons (dired-normalize-subdir dir) new-marker)
1665 dired-subdir-alist)))
1666
1667 (defun dired-goto-next-nontrivial-file ()
1668 ;; Position point on first nontrivial file after point.
1669 (dired-goto-next-file);; so there is a file to compare with
1670 (if (stringp dired-trivial-filenames)
1671 (while (and (not (eobp))
1672 (string-match dired-trivial-filenames
1673 (file-name-nondirectory
1674 (or (dired-get-filename nil t) ""))))
1675 (forward-line 1)
1676 (dired-move-to-filename))))
1677
1678 (defun dired-goto-next-file ()
1679 (let ((max (1- (dired-subdir-max))))
1680 (while (and (not (dired-move-to-filename)) (< (point) max))
1681 (forward-line 1))))
1682
1683 (defun dired-goto-file (file)
1684 "Go to file line of FILE in this dired buffer."
1685 ;; Return value of point on success, else nil.
1686 ;; FILE must be an absolute pathname.
1687 ;; Loses if FILE contains control chars like "\007" for which ls
1688 ;; either inserts "?" or "\\007" into the buffer, so we won't find
1689 ;; it in the buffer.
1690 (interactive
1691 (prog1 ; let push-mark display its message
1692 (list (expand-file-name
1693 (read-file-name "Goto file: "
1694 (dired-current-directory))))
1695 (push-mark)))
1696 (setq file (directory-file-name file)) ; does no harm if no directory
1697 (let (found case-fold-search dir)
1698 (setq dir (or (file-name-directory file)
1699 (error "Need absolute pathname for %s" file)))
1700 (save-excursion
1701 ;; The hair here is to get the result of dired-goto-subdir
1702 ;; without really calling it if we don't have any subdirs.
1703 (if (if (string= dir (expand-file-name default-directory))
1704 (goto-char (point-min))
1705 (and (cdr dired-subdir-alist)
1706 (dired-goto-subdir dir)))
1707 (let ((base (file-name-nondirectory file))
1708 (boundary (dired-subdir-max)))
1709 (while (and (not found)
1710 ;; filenames are preceded by SPC, this makes
1711 ;; the search faster (e.g. for the filename "-"!).
1712 (search-forward (concat " " base) boundary 'move))
1713 ;; Match could have BASE just as initial substring or
1714 ;; or in permission bits or date or
1715 ;; not be a proper filename at all:
1716 (if (equal base (dired-get-filename 'no-dir t))
1717 ;; Must move to filename since an (actually
1718 ;; correct) match could have been elsewhere on the
1719 ;; ;; line (e.g. "-" would match somewhere in the
1720 ;; permission bits).
1721 (setq found (dired-move-to-filename))
1722 ;; If this isn't the right line, move forward to avoid
1723 ;; trying this line again.
1724 (forward-line 1))))))
1725 (and found
1726 ;; return value of point (i.e., FOUND):
1727 (goto-char found))))
1728
1729 (defun dired-initial-position (dirname)
1730 ;; Where point should go in a new listing of DIRNAME.
1731 ;; Point assumed at beginning of new subdir line.
1732 ;; You may redefine this function as you wish, e.g. like in dired-x.el.
1733 (end-of-line)
1734 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
1735 \f
1736 ;; These are hooks which make tree dired work.
1737 ;; They are in this file because other parts of dired need to call them.
1738 ;; But they don't call the rest of tree dired unless there are subdirs loaded.
1739
1740 ;; This function is called for each retrieved filename.
1741 ;; It could stand to be faster, though it's mostly function call
1742 ;; overhead. Avoiding the function call seems to save about 10% in
1743 ;; dired-get-filename. Make it a defsubst?
1744 (defun dired-current-directory (&optional localp)
1745 "Return the name of the subdirectory to which this line belongs.
1746 This returns a string with trailing slash, like `default-directory'.
1747 Optional argument means return a file name relative to `default-directory'."
1748 (let ((here (point))
1749 (alist (or dired-subdir-alist
1750 ;; probably because called in a non-dired buffer
1751 (error "No subdir-alist in %s" (current-buffer))))
1752 elt dir)
1753 (while alist
1754 (setq elt (car alist)
1755 dir (car elt)
1756 ;; use `<=' (not `<') as subdir line is part of subdir
1757 alist (if (<= (dired-get-subdir-min elt) here)
1758 nil ; found
1759 (cdr alist))))
1760 (if localp
1761 (dired-make-relative dir default-directory)
1762 dir)))
1763
1764 ;; Subdirs start at the beginning of their header lines and end just
1765 ;; before the beginning of the next header line (or end of buffer).
1766
1767 (defun dired-subdir-max ()
1768 (save-excursion
1769 (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
1770 (point-max)
1771 (point))))
1772 \f
1773 ;; Deleting files
1774
1775 (defun dired-do-flagged-delete (&optional nomessage)
1776 "In dired, delete the files flagged for deletion.
1777 If NOMESSAGE is non-nil, we don't display any message
1778 if there are no flagged files."
1779 (interactive)
1780 (let* ((dired-marker-char dired-del-marker)
1781 (regexp (dired-marker-regexp))
1782 case-fold-search)
1783 (if (save-excursion (goto-char (point-min))
1784 (re-search-forward regexp nil t))
1785 (dired-internal-do-deletions
1786 ;; this can't move point since ARG is nil
1787 (dired-map-over-marks (cons (dired-get-filename) (point))
1788 nil)
1789 nil)
1790 (or nomessage
1791 (message "(No deletions requested)")))))
1792
1793 (defun dired-do-delete (&optional arg)
1794 "Delete all marked (or next ARG) files."
1795 ;; This is more consistent with the file marking feature than
1796 ;; dired-do-flagged-delete.
1797 (interactive "P")
1798 (dired-internal-do-deletions
1799 ;; this may move point if ARG is an integer
1800 (dired-map-over-marks (cons (dired-get-filename) (point))
1801 arg)
1802 arg))
1803
1804 (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
1805
1806 (defun dired-internal-do-deletions (l arg)
1807 ;; L is an alist of files to delete, with their buffer positions.
1808 ;; ARG is the prefix arg.
1809 ;; Filenames are absolute (VMS needs this for logical search paths).
1810 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
1811 ;; That way as changes are made in the buffer they do not shift the
1812 ;; lines still to be changed, so the (point) values in L stay valid.
1813 ;; Also, for subdirs in natural order, a subdir's files are deleted
1814 ;; before the subdir itself - the other way around would not work.
1815 (let ((files (mapcar (function car) l))
1816 (count (length l))
1817 (succ 0))
1818 ;; canonicalize file list for pop up
1819 (setq files (nreverse (mapcar (function dired-make-relative) files)))
1820 (if (dired-mark-pop-up
1821 " *Deletions*" 'delete files dired-deletion-confirmer
1822 (format "Delete %s " (dired-mark-prompt arg files)))
1823 (save-excursion
1824 (let (failures);; files better be in reverse order for this loop!
1825 (while l
1826 (goto-char (cdr (car l)))
1827 (let (buffer-read-only)
1828 (condition-case err
1829 (let ((fn (car (car l))))
1830 ;; This test is equivalent to
1831 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
1832 ;; but more efficient
1833 (if (eq t (car (file-attributes fn)))
1834 (delete-directory fn)
1835 (delete-file fn))
1836 ;; if we get here, removing worked
1837 (setq succ (1+ succ))
1838 (message "%s of %s deletions" succ count)
1839 (delete-region (progn (beginning-of-line) (point))
1840 (progn (forward-line 1) (point)))
1841 (dired-clean-up-after-deletion fn))
1842 (error;; catch errors from failed deletions
1843 (dired-log "%s\n" err)
1844 (setq failures (cons (car (car l)) failures)))))
1845 (setq l (cdr l)))
1846 (if (not failures)
1847 (message "%d deletion%s done" count (dired-plural-s count))
1848 (dired-log-summary
1849 (format "%d of %d deletion%s failed"
1850 (length failures) count
1851 (dired-plural-s count))
1852 failures))))
1853 (message "(No deletions performed)")))
1854 (dired-move-to-filename))
1855
1856 ;; This is a separate function for the sake of dired-x.el.
1857 (defun dired-clean-up-after-deletion (fn)
1858 ;; Clean up after a deleted file or directory FN.
1859 (save-excursion (and (cdr dired-subdir-alist)
1860 (dired-goto-subdir fn)
1861 (dired-kill-subdir))))
1862 \f
1863 ;; Confirmation
1864
1865 (defun dired-marker-regexp ()
1866 (concat "^" (regexp-quote (char-to-string dired-marker-char))))
1867
1868 (defun dired-plural-s (count)
1869 (if (= 1 count) "" "s"))
1870
1871 (defun dired-mark-prompt (arg files)
1872 ;; Return a string for use in a prompt, either the current file
1873 ;; name, or the marker and a count of marked files.
1874 (let ((count (length files)))
1875 (if (= count 1)
1876 (car files)
1877 ;; more than 1 file:
1878 (if (integerp arg)
1879 ;; abs(arg) = count
1880 ;; Perhaps this is nicer, but it also takes more screen space:
1881 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
1882 ;; count)
1883 (format "[next %d files]" arg)
1884 (format "%c [%d files]" dired-marker-char count)))))
1885
1886 (defun dired-pop-to-buffer (buf)
1887 ;; Pop up buffer BUF.
1888 ;; If dired-shrink-to-fit is t, make its window fit its contents.
1889 (if (not dired-shrink-to-fit)
1890 (pop-to-buffer (get-buffer-create buf))
1891 ;; let window shrink to fit:
1892 (let ((window (selected-window))
1893 target-lines w2)
1894 (cond ;; if split-window-threshold is enabled, use the largest window
1895 ((and (> (window-height (setq w2 (get-largest-window)))
1896 split-height-threshold)
1897 (= (frame-width) (window-width w2)))
1898 (setq window w2))
1899 ;; if the least-recently-used window is big enough, use it
1900 ((and (> (window-height (setq w2 (get-lru-window)))
1901 (* 2 window-min-height))
1902 (= (frame-width) (window-width w2)))
1903 (setq window w2)))
1904 (save-excursion
1905 (set-buffer buf)
1906 (goto-char (point-max))
1907 (skip-chars-backward "\n\r\t ")
1908 (setq target-lines (count-lines (point-min) (point)))
1909 ;; Don't forget to count the last line.
1910 (if (not (bolp))
1911 (setq target-lines (1+ target-lines))))
1912 (if (<= (window-height window) (* 2 window-min-height))
1913 ;; At this point, every window on the frame is too small to split.
1914 (setq w2 (display-buffer buf))
1915 (setq w2 (split-window window
1916 (max window-min-height
1917 (- (window-height window)
1918 (1+ (max window-min-height target-lines)))))))
1919 (set-window-buffer w2 buf)
1920 (if (< (1- (window-height w2)) target-lines)
1921 (progn
1922 (select-window w2)
1923 (enlarge-window (- target-lines (1- (window-height w2))))))
1924 (set-window-start w2 1)
1925 )))
1926
1927 (defvar dired-no-confirm nil
1928 "A list of symbols for commands dired should not confirm.
1929 Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress',
1930 `copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink' and
1931 `uncompress'.")
1932
1933 (defun dired-mark-pop-up (bufname op-symbol files function &rest args)
1934 ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
1935 ;;Return FUNCTION's result on ARGS after popping up a window (in a buffer
1936 ;;named BUFNAME, nil gives \" *Marked Files*\") showing the marked
1937 ;;files. Uses function `dired-pop-to-buffer' to do that.
1938 ;; FUNCTION should not manipulate files.
1939 ;; It should only read input (an argument or confirmation).
1940 ;;The window is not shown if there is just one file or
1941 ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
1942 ;;FILES is the list of marked files."
1943 (or bufname (setq bufname " *Marked Files*"))
1944 (if (or (eq dired-no-confirm t)
1945 (memq op-symbol dired-no-confirm)
1946 (= (length files) 1))
1947 (apply function args)
1948 (save-excursion
1949 (set-buffer (get-buffer-create bufname))
1950 (erase-buffer)
1951 (dired-format-columns-of-files files)
1952 (remove-text-properties (point-min) (point-max) '(mouse-face)))
1953 (save-window-excursion
1954 (dired-pop-to-buffer bufname)
1955 (apply function args))))
1956
1957 (defun dired-format-columns-of-files (files)
1958 ;; Files should be in forward order for this loop.
1959 ;; i.e., (car files) = first file in buffer.
1960 ;; Returns the number of lines used.
1961 (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files))))
1962 (width (- (window-width (selected-window)) 2))
1963 (columns (max 1 (/ width maxlen)))
1964 (nfiles (length files))
1965 (rows (+ (/ nfiles columns)
1966 (if (zerop (% nfiles columns)) 0 1)))
1967 (i 0)
1968 (j 0))
1969 (setq files (nconc (copy-sequence files) ; fill up with empty fns
1970 (make-list (- (* columns rows) nfiles) "")))
1971 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
1972 (while (< j rows)
1973 (while (< i columns)
1974 (indent-to (* i maxlen))
1975 (insert (car files))
1976 (setq files (nthcdr rows files)
1977 i (1+ i)))
1978 (insert "\n")
1979 (setq i 0
1980 j (1+ j)
1981 files (cdr files)))
1982 rows))
1983 \f
1984 ;; Commands to mark or flag file(s) at or near current line.
1985
1986 (defun dired-repeat-over-lines (arg function)
1987 ;; This version skips non-file lines.
1988 (let ((pos (make-marker)))
1989 (beginning-of-line)
1990 (while (and (> arg 0) (not (eobp)))
1991 (setq arg (1- arg))
1992 (beginning-of-line)
1993 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
1994 (save-excursion
1995 (forward-line 1)
1996 (move-marker pos (1+ (point))))
1997 (save-excursion (funcall function))
1998 ;; Advance to the next line--actually, to the line that *was* next.
1999 ;; (If FUNCTION inserted some new lines in between, skip them.)
2000 (goto-char pos))
2001 (while (and (< arg 0) (not (bobp)))
2002 (setq arg (1+ arg))
2003 (forward-line -1)
2004 (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
2005 (beginning-of-line)
2006 (save-excursion (funcall function)))
2007 (move-marker pos nil)
2008 (dired-move-to-filename)))
2009
2010 (defun dired-between-files ()
2011 ;; Point must be at beginning of line
2012 ;; Should be equivalent to (save-excursion (not (dired-move-to-filename)))
2013 ;; but is about 1.5..2.0 times as fast. (Actually that's not worth it)
2014 (or (looking-at "^$\\|^. *$\\|^. total\\|^. wildcard")
2015 (and (looking-at dired-subdir-regexp)
2016 (save-excursion (not (dired-move-to-filename))))))
2017
2018 (defun dired-next-marked-file (arg &optional wrap opoint)
2019 "Move to the next marked file, wrapping around the end of the buffer."
2020 (interactive "p\np")
2021 (or opoint (setq opoint (point)));; return to where interactively started
2022 (if (if (> arg 0)
2023 (re-search-forward dired-re-mark nil t arg)
2024 (beginning-of-line)
2025 (re-search-backward dired-re-mark nil t (- arg)))
2026 (dired-move-to-filename)
2027 (if (null wrap)
2028 (progn
2029 (goto-char opoint)
2030 (error "No next marked file"))
2031 (message "(Wraparound for next marked file)")
2032 (goto-char (if (> arg 0) (point-min) (point-max)))
2033 (dired-next-marked-file arg nil opoint))))
2034
2035 (defun dired-prev-marked-file (arg &optional wrap)
2036 "Move to the previous marked file, wrapping around the end of the buffer."
2037 (interactive "p\np")
2038 (dired-next-marked-file (- arg) wrap))
2039
2040 (defun dired-file-marker (file)
2041 ;; Return FILE's marker, or nil if unmarked.
2042 (save-excursion
2043 (and (dired-goto-file file)
2044 (progn
2045 (beginning-of-line)
2046 (if (not (equal ?\040 (following-char)))
2047 (following-char))))))
2048
2049 (defun dired-mark-files-in-region (start end)
2050 (let (buffer-read-only)
2051 (if (> start end)
2052 (error "start > end"))
2053 (goto-char start) ; assumed at beginning of line
2054 (while (< (point) end)
2055 ;; Skip subdir line and following garbage like the `total' line:
2056 (while (and (< (point) end) (dired-between-files))
2057 (forward-line 1))
2058 (if (and (not (looking-at dired-re-dot))
2059 (dired-get-filename nil t))
2060 (progn
2061 (delete-char 1)
2062 (insert dired-marker-char)))
2063 (forward-line 1))))
2064
2065 (defun dired-mark (arg)
2066 "Mark the current (or next ARG) files.
2067 If on a subdir headerline, mark all its files except `.' and `..'.
2068
2069 Use \\[dired-unmark-all-files] to remove all marks
2070 and \\[dired-unmark] on a subdir to remove the marks in
2071 this subdir."
2072 (interactive "P")
2073 (if (dired-get-subdir)
2074 (save-excursion (dired-mark-subdir-files))
2075 (let (buffer-read-only)
2076 (dired-repeat-over-lines
2077 (prefix-numeric-value arg)
2078 (function (lambda () (delete-char 1) (insert dired-marker-char)))))))
2079
2080 (defun dired-unmark (arg)
2081 "Unmark the current (or next ARG) files.
2082 If looking at a subdir, unmark all its files except `.' and `..'."
2083 (interactive "P")
2084 (let ((dired-marker-char ?\040))
2085 (dired-mark arg)))
2086
2087 (defun dired-flag-file-deletion (arg)
2088 "In dired, flag the current line's file for deletion.
2089 With prefix arg, repeat over several lines.
2090
2091 If on a subdir headerline, mark all its files except `.' and `..'."
2092 (interactive "P")
2093 (let ((dired-marker-char dired-del-marker))
2094 (dired-mark arg)))
2095
2096 (defun dired-unmark-backward (arg)
2097 "In dired, move up lines and remove deletion flag there.
2098 Optional prefix ARG says how many lines to unflag; default is one line."
2099 (interactive "p")
2100 (dired-unmark (- arg)))
2101
2102 (defun dired-do-toggle ()
2103 "Toggle marks.
2104 That is, currently marked files become unmarked and vice versa.
2105 Files marked with other flags (such as `D') are not affected.
2106 `.' and `..' are never toggled.
2107 As always, hidden subdirs are not affected."
2108 (interactive)
2109 (save-excursion
2110 (goto-char (point-min))
2111 (let (buffer-read-only)
2112 (while (not (eobp))
2113 (or (dired-between-files)
2114 (looking-at dired-re-dot)
2115 ;; use subst instead of insdel because it does not move
2116 ;; the gap and thus should be faster and because
2117 ;; other characters are left alone automatically
2118 (apply 'subst-char-in-region
2119 (point) (1+ (point))
2120 (if (eq ?\040 (following-char)) ; SPC
2121 (list ?\040 dired-marker-char)
2122 (list dired-marker-char ?\040))))
2123 (forward-line 1)))))
2124 \f
2125 ;;; Commands to mark or flag files based on their characteristics or names.
2126
2127 (defvar dired-regexp-history nil
2128 "History list of regular expressions used in Dired commands.")
2129
2130 (defun dired-read-regexp (prompt)
2131 (read-from-minibuffer prompt nil nil nil 'dired-regexp-history))
2132
2133 (defun dired-mark-files-regexp (regexp &optional marker-char)
2134 "Mark all files matching REGEXP for use in later commands.
2135 A prefix argument means to unmark them instead.
2136 `.' and `..' are never marked.
2137
2138 REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
2139 object files--just `.o' will mark more than you might think."
2140 (interactive
2141 (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
2142 " files (regexp): "))
2143 (if current-prefix-arg ?\040)))
2144 (let ((dired-marker-char (or marker-char dired-marker-char)))
2145 (dired-mark-if
2146 (and (not (looking-at dired-re-dot))
2147 (not (eolp)) ; empty line
2148 (let ((fn (dired-get-filename nil t)))
2149 (and fn (string-match regexp (file-name-nondirectory fn)))))
2150 "matching file")))
2151
2152 (defun dired-flag-files-regexp (regexp)
2153 "In dired, flag all files containing the specified REGEXP for deletion.
2154 The match is against the non-directory part of the filename. Use `^'
2155 and `$' to anchor matches. Exclude subdirs by hiding them.
2156 `.' and `..' are never flagged."
2157 (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
2158 (dired-mark-files-regexp regexp dired-del-marker))
2159
2160 (defun dired-mark-symlinks (unflag-p)
2161 "Mark all symbolic links.
2162 With prefix argument, unflag all those files."
2163 (interactive "P")
2164 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2165 (dired-mark-if (looking-at dired-re-sym) "symbolic link")))
2166
2167 (defun dired-mark-directories (unflag-p)
2168 "Mark all directory file lines except `.' and `..'.
2169 With prefix argument, unflag all those files."
2170 (interactive "P")
2171 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2172 (dired-mark-if (and (looking-at dired-re-dir)
2173 (not (looking-at dired-re-dot)))
2174 "directory file")))
2175
2176 (defun dired-mark-executables (unflag-p)
2177 "Mark all executable files.
2178 With prefix argument, unflag all those files."
2179 (interactive "P")
2180 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2181 (dired-mark-if (looking-at dired-re-exe) "executable file")))
2182
2183 ;; dired-x.el has a dired-mark-sexp interactive command: mark
2184 ;; files for which PREDICATE returns non-nil.
2185
2186 (defun dired-flag-auto-save-files (&optional unflag-p)
2187 "Flag for deletion files whose names suggest they are auto save files.
2188 A prefix argument says to unflag those files instead."
2189 (interactive "P")
2190 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
2191 (dired-mark-if
2192 ;; It is less than general to check for # here,
2193 ;; but it's the only way this runs fast enough.
2194 (and (save-excursion (end-of-line)
2195 (or
2196 (eq (preceding-char) ?#)
2197 ;; Handle executables in case of -F option.
2198 ;; We need not worry about the other kinds
2199 ;; of markings that -F makes, since they won't
2200 ;; appear on real auto-save files.
2201 (if (eq (preceding-char) ?*)
2202 (progn
2203 (forward-char -1)
2204 (eq (preceding-char) ?#)))))
2205 (not (looking-at dired-re-dir))
2206 (let ((fn (dired-get-filename t t)))
2207 (if fn (auto-save-file-name-p
2208 (file-name-nondirectory fn)))))
2209 "auto save file")))
2210
2211 (defvar dired-garbage-files-regexp
2212 "\\.log$\\|\\.toc$\\|.dvi$|\\.bak$\\|\\.orig$\\|\\.rej$"
2213 "*Regular expression to match \"garbage\" files for `dired-flag-garbage-files'.")
2214
2215 (defun dired-flag-garbage-files ()
2216 "Flag for deletion all files that match `dired-garbage-files-regexp'."
2217 (interactive)
2218 (dired-flag-files-regexp dired-garbage-files-regexp))
2219
2220 (defun dired-flag-backup-files (&optional unflag-p)
2221 "Flag all backup files (names ending with `~') for deletion.
2222 With prefix argument, unflag these files."
2223 (interactive "P")
2224 (let ((dired-marker-char (if unflag-p ?\ dired-del-marker)))
2225 (dired-mark-if
2226 ;; Don't call backup-file-name-p unless the last character looks like
2227 ;; it might be the end of a backup file name. This isn't very general,
2228 ;; but it's the only way this runs fast enough.
2229 (and (save-excursion (end-of-line)
2230 ;; Handle executables in case of -F option.
2231 ;; We need not worry about the other kinds
2232 ;; of markings that -F makes, since they won't
2233 ;; appear on real backup files.
2234 (if (eq (preceding-char) ?*)
2235 (forward-char -1))
2236 (eq (preceding-char) ?~))
2237 (not (looking-at dired-re-dir))
2238 (let ((fn (dired-get-filename t t)))
2239 (if fn (backup-file-name-p fn))))
2240 "backup file")))
2241
2242 (defun dired-change-marks (&optional old new)
2243 "Change all OLD marks to NEW marks.
2244 OLD and NEW are both characters used to mark files."
2245 (interactive
2246 (let* ((cursor-in-echo-area t)
2247 (old (progn (message "Change (old mark): ") (read-char)))
2248 (new (progn (message "Change %c marks to (new mark): " old)
2249 (read-char))))
2250 (list old new)))
2251 (if (or (eq old ?\r) (eq new ?\r))
2252 (ding)
2253 (let ((string (format "\n%c" old))
2254 (buffer-read-only))
2255 (save-excursion
2256 (goto-char (point-min))
2257 (while (search-forward string nil t)
2258 (if (if (= old ?\ )
2259 (save-match-data
2260 (dired-get-filename 'no-dir t))
2261 t)
2262 (subst-char-in-region (match-beginning 0)
2263 (match-end 0) old new)))))))
2264
2265 (defun dired-unmark-all-marks ()
2266 "Remove all marks from all files in the Dired buffer."
2267 (interactive)
2268 (dired-unmark-all-files ?\r))
2269
2270 (defun dired-unmark-all-files (mark &optional arg)
2271 "Remove a specific mark (or any mark) from every file.
2272 After this command, type the mark character to remove,
2273 or type RET to remove all marks.
2274 With prefix arg, query for each marked file.
2275 Type \\[help-command] at that time for help."
2276 (interactive "cRemove marks (RET means all): \nP")
2277 (save-excursion
2278 (let* ((count 0)
2279 buffer-read-only case-fold-search query
2280 (string (format "\n%c" mark))
2281 (help-form "\
2282 Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
2283 `!' to unmark all remaining files with no more questions."))
2284 (goto-char (point-min))
2285 (while (if (eq mark ?\r)
2286 (re-search-forward dired-re-mark nil t)
2287 (search-forward string nil t))
2288 (if (or (not arg)
2289 (dired-query 'query "Unmark file `%s'? "
2290 (dired-get-filename t)))
2291 (progn (subst-char-in-region (1- (point)) (point)
2292 (preceding-char) ?\ )
2293 (setq count (1+ count)))))
2294 (message (if (= count 1) "1 mark removed"
2295 "%d marks removed")
2296 count))))
2297 \f
2298 ;; Logging failures operating on files, and showing the results.
2299
2300 (defvar dired-log-buffer "*Dired log*")
2301
2302 (defun dired-why ()
2303 "Pop up a buffer with error log output from Dired.
2304 A group of errors from a single command ends with a formfeed.
2305 Thus, use \\[backward-page] to find the beginning of a group of errors."
2306 (interactive)
2307 (if (get-buffer dired-log-buffer)
2308 (let ((owindow (selected-window))
2309 (window (display-buffer (get-buffer dired-log-buffer))))
2310 (unwind-protect
2311 (progn
2312 (select-window window)
2313 (goto-char (point-max))
2314 (recenter -1))
2315 (select-window owindow)))))
2316
2317 (defun dired-log (log &rest args)
2318 ;; Log a message or the contents of a buffer.
2319 ;; If LOG is a string and there are more args, it is formatted with
2320 ;; those ARGS. Usually the LOG string ends with a \n.
2321 ;; End each bunch of errors with (dired-log t): this inserts
2322 ;; current time and buffer, and a \f (formfeed).
2323 (let ((obuf (current-buffer)))
2324 (unwind-protect ; want to move point
2325 (progn
2326 (set-buffer (get-buffer-create dired-log-buffer))
2327 (goto-char (point-max))
2328 (let (buffer-read-only)
2329 (cond ((stringp log)
2330 (insert (if args
2331 (apply (function format) log args)
2332 log)))
2333 ((bufferp log)
2334 (insert-buffer log))
2335 ((eq t log)
2336 (insert "\n\t" (current-time-string)
2337 "\tBuffer `" (buffer-name obuf) "'\n\f\n")))))
2338 (set-buffer obuf))))
2339
2340 (defun dired-log-summary (string failures)
2341 (message (if failures "%s--type ? for details (%s)"
2342 "%s--type ? for details")
2343 string failures)
2344 ;; Log a summary describing a bunch of errors.
2345 (dired-log (concat "\n" string))
2346 (dired-log t))
2347 \f
2348 ;;; Sorting
2349
2350 ;; Most ls can only sort by name or by date (with -t), nothing else.
2351 ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
2352 ;; So anything that does not contain these is sort "by name".
2353
2354 (defvar dired-ls-sorting-switches "SXU"
2355 "String of `ls' switches (single letters) except `t' that influence sorting.")
2356
2357 (defvar dired-sort-by-date-regexp
2358 (concat "^-[^" dired-ls-sorting-switches
2359 "]*t[^" dired-ls-sorting-switches "]*$")
2360 "Regexp recognized by dired to set `by date' mode.")
2361
2362 (defvar dired-sort-by-name-regexp
2363 (concat "^-[^t" dired-ls-sorting-switches "]+$")
2364 "Regexp recognized by dired to set `by name' mode.")
2365
2366 (defun dired-sort-set-modeline ()
2367 ;; Set modeline display according to dired-actual-switches.
2368 ;; Modeline display of "by name" or "by date" guarantees the user a
2369 ;; match with the corresponding regexps. Non-matching switches are
2370 ;; shown literally.
2371 (setq mode-name
2372 (let (case-fold-search)
2373 (cond ((string-match dired-sort-by-name-regexp dired-actual-switches)
2374 "Dired by name")
2375 ((string-match dired-sort-by-date-regexp dired-actual-switches)
2376 "Dired by date")
2377 (t
2378 (concat "Dired " dired-actual-switches)))))
2379 (force-mode-line-update))
2380
2381 (defun dired-sort-toggle-or-edit (&optional arg)
2382 "Toggle between sort by date/name and refresh the dired buffer.
2383 With a prefix argument you can edit the current listing switches instead."
2384 (interactive "P")
2385 (if arg
2386 (dired-sort-other
2387 (read-string "ls switches (must contain -l): " dired-actual-switches))
2388 (dired-sort-toggle)))
2389
2390 (defun dired-sort-toggle ()
2391 ;; Toggle between sort by date/name. Reverts the buffer.
2392 (setq dired-actual-switches
2393 (let (case-fold-search)
2394 (concat
2395 "-l"
2396 (dired-replace-in-string (concat "[-lt"
2397 dired-ls-sorting-switches "]")
2398 ""
2399 dired-actual-switches)
2400 (if (string-match (concat "[t" dired-ls-sorting-switches "]")
2401 dired-actual-switches)
2402 ""
2403 "t"))))
2404 (dired-sort-set-modeline)
2405 (revert-buffer))
2406
2407 (defun dired-replace-in-string (regexp newtext string)
2408 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
2409 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
2410 (let ((result "") (start 0) mb me)
2411 (while (string-match regexp string start)
2412 (setq mb (match-beginning 0)
2413 me (match-end 0)
2414 result (concat result (substring string start mb) newtext)
2415 start me))
2416 (concat result (substring string start))))
2417
2418 (defun dired-sort-other (switches &optional no-revert)
2419 ;; Specify new ls SWITCHES for current dired buffer. Values matching
2420 ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
2421 ;; minor mode accordingly, others appear literally in the mode line.
2422 ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
2423 (setq dired-actual-switches switches)
2424 (if (eq major-mode 'dired-mode) (dired-sort-set-modeline))
2425 (or no-revert (revert-buffer)))
2426 \f
2427 ;; To make this file smaller, the less common commands
2428 ;; go in a separate file. But autoload them here
2429 ;; to make the separation invisible.
2430
2431 (autoload 'dired-diff "dired-aux"
2432 "Compare file at point with file FILE using `diff'.
2433 FILE defaults to the file at the mark.
2434 The prompted-for file is the first file given to `diff'."
2435 t)
2436
2437 (autoload 'dired-backup-diff "dired-aux"
2438 "Diff this file with its backup file or vice versa.
2439 Uses the latest backup, if there are several numerical backups.
2440 If this file is a backup, diff it with its original.
2441 The backup file is the first file given to `diff'."
2442 t)
2443
2444 (autoload 'dired-clean-directory "dired-aux"
2445 "Flag numerical backups for deletion.
2446 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
2447 Positive prefix arg KEEP overrides `dired-kept-versions';
2448 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
2449
2450 To clear the flags on these files, you can use \\[dired-flag-backup-files]
2451 with a prefix argument."
2452 t)
2453
2454 (autoload 'dired-do-chmod "dired-aux"
2455 "Change the mode of the marked (or next ARG) files.
2456 This calls chmod, thus symbolic modes like `g+w' are allowed."
2457 t)
2458
2459 (autoload 'dired-do-chgrp "dired-aux"
2460 "Change the group of the marked (or next ARG) files."
2461 t)
2462
2463 (autoload 'dired-do-chown "dired-aux"
2464 "Change the owner of the marked (or next ARG) files."
2465 t)
2466
2467 (autoload 'dired-do-print "dired-aux"
2468 "Print the marked (or next ARG) files.
2469 Uses the shell command coming from variables `lpr-command' and
2470 `lpr-switches' as default."
2471 t)
2472
2473 (autoload 'dired-do-shell-command "dired-aux"
2474 "Run a shell command COMMAND on the marked files.
2475 If no files are marked or a specific numeric prefix arg is given,
2476 the next ARG files are used. Just \\[universal-argument] means the current file.
2477 The prompt mentions the file(s) or the marker, as appropriate.
2478
2479 If there is output, it goes to a separate buffer.
2480
2481 Normally the command is run on each file individually.
2482 However, if there is a `*' in the command then it is run
2483 just once with the entire file list substituted there.
2484
2485 No automatic redisplay of dired buffers is attempted, as there's no
2486 telling what files the command may have changed. Type
2487 \\[dired-do-redisplay] to redisplay the marked files.
2488
2489 The shell command has the top level directory as working directory, so
2490 output files usually are created there instead of in a subdir."
2491 t)
2492
2493 (autoload 'dired-do-kill-lines "dired-aux"
2494 "Kill all marked lines (not the files).
2495 With a prefix arg, kill all lines not marked or flagged."
2496 t)
2497
2498 (autoload 'dired-do-compress "dired-aux"
2499 "Compress or uncompress marked (or next ARG) files."
2500 t)
2501
2502 (autoload 'dired-do-byte-compile "dired-aux"
2503 "Byte compile marked (or next ARG) Emacs Lisp files."
2504 t)
2505
2506 (autoload 'dired-do-load "dired-aux"
2507 "Load the marked (or next ARG) Emacs Lisp files."
2508 t)
2509
2510 (autoload 'dired-do-redisplay "dired-aux"
2511 "Redisplay all marked (or next ARG) files.
2512 If on a subdir line, redisplay that subdirectory. In that case,
2513 a prefix arg lets you edit the `ls' switches used for the new listing."
2514 t)
2515
2516 (autoload 'dired-create-directory "dired-aux"
2517 "Create a directory called DIRECTORY."
2518 t)
2519
2520 (autoload 'dired-do-copy "dired-aux"
2521 "Copy all marked (or next ARG) files, or copy the current file.
2522 Thus, a zero prefix argument copies nothing. But it toggles the
2523 variable `dired-copy-preserve-time' (which see)."
2524 t)
2525
2526 (autoload 'dired-do-symlink "dired-aux"
2527 "Make symbolic links to current file or all marked (or next ARG) files.
2528 When operating on just the current file, you specify the new name.
2529 When operating on multiple or marked files, you specify a directory
2530 and new symbolic links are made in that directory
2531 with the same names that the files currently have."
2532 t)
2533
2534 (autoload 'dired-do-hardlink "dired-aux"
2535 "Add names (hard links) current file or all marked (or next ARG) files.
2536 When operating on just the current file, you specify the new name.
2537 When operating on multiple or marked files, you specify a directory
2538 and new hard links are made in that directory
2539 with the same names that the files currently have."
2540 t)
2541
2542 (autoload 'dired-do-rename "dired-aux"
2543 "Rename current file or all marked (or next ARG) files.
2544 When renaming just the current file, you specify the new name.
2545 When renaming multiple or marked files, you specify a directory."
2546 t)
2547
2548 (autoload 'dired-do-rename-regexp "dired-aux"
2549 "Rename marked files containing REGEXP to NEWNAME.
2550 As each match is found, the user must type a character saying
2551 what to do with it. For directions, type \\[help-command] at that time.
2552 NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
2553 REGEXP defaults to the last regexp used.
2554 With a zero prefix arg, renaming by regexp affects the complete
2555 pathname - usually only the non-directory part of file names is used
2556 and changed."
2557 t)
2558
2559 (autoload 'dired-do-copy-regexp "dired-aux"
2560 "Copy all marked files containing REGEXP to NEWNAME.
2561 See function `dired-rename-regexp' for more info."
2562 t)
2563
2564 (autoload 'dired-do-hardlink-regexp "dired-aux"
2565 "Hardlink all marked files containing REGEXP to NEWNAME.
2566 See function `dired-rename-regexp' for more info."
2567 t)
2568
2569 (autoload 'dired-do-symlink-regexp "dired-aux"
2570 "Symlink all marked files containing REGEXP to NEWNAME.
2571 See function `dired-rename-regexp' for more info."
2572 t)
2573
2574 (autoload 'dired-upcase "dired-aux"
2575 "Rename all marked (or next ARG) files to upper case."
2576 t)
2577
2578 (autoload 'dired-downcase "dired-aux"
2579 "Rename all marked (or next ARG) files to lower case."
2580 t)
2581
2582 (autoload 'dired-maybe-insert-subdir "dired-aux"
2583 "Insert this subdirectory into the same dired buffer.
2584 If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
2585 else inserts it at its natural place (as `ls -lR' would have done).
2586 With a prefix arg, you may edit the ls switches used for this listing.
2587 You can add `R' to the switches to expand the whole tree starting at
2588 this subdirectory.
2589 This function takes some pains to conform to `ls -lR' output."
2590 t)
2591
2592 (autoload 'dired-next-subdir "dired-aux"
2593 "Go to next subdirectory, regardless of level."
2594 t)
2595
2596 (autoload 'dired-prev-subdir "dired-aux"
2597 "Go to previous subdirectory, regardless of level.
2598 When called interactively and not on a subdir line, go to this subdir's line."
2599 t)
2600
2601 (autoload 'dired-goto-subdir "dired-aux"
2602 "Go to end of header line of DIR in this dired buffer.
2603 Return value of point on success, otherwise return nil.
2604 The next char is either \\n, or \\r if DIR is hidden."
2605 t)
2606
2607 (autoload 'dired-mark-subdir-files "dired-aux"
2608 "Mark all files except `.' and `..'."
2609 t)
2610
2611 (autoload 'dired-kill-subdir "dired-aux"
2612 "Remove all lines of current subdirectory.
2613 Lower levels are unaffected."
2614 t)
2615
2616 (autoload 'dired-tree-up "dired-aux"
2617 "Go up ARG levels in the dired tree."
2618 t)
2619
2620 (autoload 'dired-tree-down "dired-aux"
2621 "Go down in the dired tree."
2622 t)
2623
2624 (autoload 'dired-hide-subdir "dired-aux"
2625 "Hide or unhide the current subdirectory and move to next directory.
2626 Optional prefix arg is a repeat factor.
2627 Use \\[dired-hide-all] to (un)hide all directories."
2628 t)
2629
2630 (autoload 'dired-hide-all "dired-aux"
2631 "Hide all subdirectories, leaving only their header lines.
2632 If there is already something hidden, make everything visible again.
2633 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2634 t)
2635 \f
2636 (if (eq system-type 'vax-vms)
2637 (load "dired-vms"))
2638
2639 (provide 'dired)
2640
2641 (run-hooks 'dired-load-hook) ; for your customizations
2642
2643 ;;; dired.el ends here