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