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