Fix doc strings and error message syntax.
[emacs.git] / lisp / dired-aux.el
blobe83bb0fd5e632d7917bf43633a1284c4d22bb498
1 ;;; dired-aux.el --- all of dired except what people usually use
3 ;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
5 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>.
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 ;;; Commentary:
25 ;; The parts of dired mode not normally used. This is a space-saving hack
26 ;; to avoid having to load a large mode when all that's wanted are a few
27 ;; functions.
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.
33 ;;; Code:
35 ;; We need macros in dired.el to compile properly.
36 (eval-when-compile (require 'dired))
38 ;;; 15K
39 ;;;###begin dired-cmd.el
40 ;; Diffing and compressing
42 ;;;###autoload
43 (defun dired-diff (file &optional switches)
44 "Compare file at point with file FILE using `diff'.
45 FILE defaults to the file at the mark.
46 The prompted-for file is the first file given to `diff'."
47 (interactive
48 (let ((default (if (mark t)
49 (save-excursion (goto-char (mark t))
50 (dired-get-filename t t)))))
51 (list (read-file-name (format "Diff %s with: %s"
52 (dired-get-filename t)
53 (if default
54 (concat "(default " default ") ")
55 ""))
56 (dired-current-directory) default t)
57 (if (fboundp 'diff-read-switches)
58 (diff-read-switches "Options for diff: ")))))
59 (if switches ; Emacs 19's diff has but two
60 (diff file (dired-get-filename t) switches) ; args (yet ;-)
61 (diff file (dired-get-filename t))))
63 ;;;###autoload
64 (defun dired-backup-diff (&optional switches)
65 "Diff this file with its backup file or vice versa.
66 Uses the latest backup, if there are several numerical backups.
67 If this file is a backup, diff it with its original.
68 The backup file is the first file given to `diff'."
69 (interactive (list (if (fboundp 'diff-read-switches)
70 (diff-read-switches "Diff with switches: "))))
71 (if switches
72 (diff-backup (dired-get-filename) switches)
73 (diff-backup (dired-get-filename))))
75 (defun dired-do-chxxx (attribute-name program op-symbol arg)
76 ;; Change file attributes (mode, group, owner) of marked files and
77 ;; refresh their file lines.
78 ;; ATTRIBUTE-NAME is a string describing the attribute to the user.
79 ;; PROGRAM is the program used to change the attribute.
80 ;; OP-SYMBOL is the type of operation (for use in dired-mark-pop-up).
81 ;; ARG describes which files to use, as in dired-get-marked-files.
82 (let* ((files (dired-get-marked-files t arg))
83 (new-attribute
84 (dired-mark-read-string
85 (concat "Change " attribute-name " of %s to: ")
86 nil op-symbol arg files))
87 (operation (concat program " " new-attribute))
88 failures)
89 (setq failures
90 (dired-bunch-files 10000
91 (function dired-check-process)
92 (list operation program new-attribute)
93 files))
94 (dired-do-redisplay arg);; moves point if ARG is an integer
95 (if failures
96 (dired-log-summary
97 (format "%s: error" operation)
98 nil))))
100 ;;;###autoload
101 (defun dired-do-chmod (&optional arg)
102 "Change the mode of the marked (or next ARG) files.
103 This calls chmod, thus symbolic modes like `g+w' are allowed."
104 (interactive "P")
105 (dired-do-chxxx "Mode" "chmod" 'chmod arg))
107 ;;;###autoload
108 (defun dired-do-chgrp (&optional arg)
109 "Change the group of the marked (or next ARG) files."
110 (interactive "P")
111 (dired-do-chxxx "Group" "chgrp" 'chgrp arg))
113 ;;;###autoload
114 (defun dired-do-chown (&optional arg)
115 "Change the owner of the marked (or next ARG) files."
116 (interactive "P")
117 (dired-do-chxxx "Owner" dired-chown-program 'chown arg))
119 ;; Process all the files in FILES in batches of a convenient size,
120 ;; by means of (FUNCALL FUNCTION ARGS... SOME-FILES...).
121 ;; Batches are chosen to need less than MAX chars for the file names,
122 ;; allowing 3 extra characters of separator per file name.
123 (defun dired-bunch-files (max function args files)
124 (let (pending
125 (pending-length 0)
126 failures)
127 ;; Accumulate files as long as they fit in MAX chars,
128 ;; then process the ones accumulated so far.
129 (while files
130 (let* ((thisfile (car files))
131 (thislength (+ (length thisfile) 3))
132 (rest (cdr files)))
133 ;; If we have at least 1 pending file
134 ;; and this file won't fit in the length limit, process now.
135 (if (and pending (> (+ thislength pending-length) max))
136 (setq failures
137 (nconc (apply function (append args pending))
138 failures)
139 pending nil
140 pending-length 0))
141 ;; Do (setq pending (cons thisfile pending))
142 ;; but reuse the cons that was in `files'.
143 (setcdr files pending)
144 (setq pending files)
145 (setq pending-length (+ thislength pending-length))
146 (setq files rest)))
147 (nconc (apply function (append args pending))
148 failures)))
150 ;;;###autoload
151 (defun dired-do-print (&optional arg)
152 "Print the marked (or next ARG) files.
153 Uses the shell command coming from variables `lpr-command' and
154 `lpr-switches' as default."
155 (interactive "P")
156 (let* ((file-list (dired-get-marked-files t arg))
157 (command (dired-mark-read-string
158 "Print %s with: "
159 (apply 'concat lpr-command " " lpr-switches)
160 'print arg file-list)))
161 (dired-run-shell-command (dired-shell-stuff-it command file-list nil))))
163 ;; Read arguments for a marked-files command that wants a string
164 ;; that is not a file name,
165 ;; perhaps popping up the list of marked files.
166 ;; ARG is the prefix arg and indicates whether the files came from
167 ;; marks (ARG=nil) or a repeat factor (integerp ARG).
168 ;; If the current file was used, the list has but one element and ARG
169 ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)).
171 (defun dired-mark-read-string (prompt initial op-symbol arg files)
172 ;; PROMPT for a string, with INITIAL input.
173 ;; Other args are used to give user feedback and pop-up:
174 ;; OP-SYMBOL of command, prefix ARG, marked FILES.
175 (dired-mark-pop-up
176 nil op-symbol files
177 (function read-string)
178 (format prompt (dired-mark-prompt arg files)) initial))
180 ;;; Cleaning a directory: flagging some backups for deletion.
182 (defvar dired-file-version-alist)
184 (defun dired-clean-directory (keep)
185 "Flag numerical backups for deletion.
186 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
187 Positive prefix arg KEEP overrides `dired-kept-versions';
188 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
190 To clear the flags on these files, you can use \\[dired-flag-backup-files]
191 with a prefix argument."
192 (interactive "P")
193 (setq keep (if keep (prefix-numeric-value keep) dired-kept-versions))
194 (let ((early-retention (if (< keep 0) (- keep) kept-old-versions))
195 (late-retention (if (<= keep 0) dired-kept-versions keep))
196 (dired-file-version-alist ()))
197 (message "Cleaning numerical backups (keeping %d late, %d old)..."
198 late-retention early-retention)
199 ;; Look at each file.
200 ;; If the file has numeric backup versions,
201 ;; put on dired-file-version-alist an element of the form
202 ;; (FILENAME . VERSION-NUMBER-LIST)
203 (dired-map-dired-file-lines (function dired-collect-file-versions))
204 ;; Sort each VERSION-NUMBER-LIST,
205 ;; and remove the versions not to be deleted.
206 (let ((fval dired-file-version-alist))
207 (while fval
208 (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
209 (v-count (length sorted-v-list)))
210 (if (> v-count (+ early-retention late-retention))
211 (rplacd (nthcdr early-retention sorted-v-list)
212 (nthcdr (- v-count late-retention)
213 sorted-v-list)))
214 (rplacd (car fval)
215 (cdr sorted-v-list)))
216 (setq fval (cdr fval))))
217 ;; Look at each file. If it is a numeric backup file,
218 ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
219 (dired-map-dired-file-lines (function dired-trample-file-versions))
220 (message "Cleaning numerical backups...done")))
222 ;;; Subroutines of dired-clean-directory.
224 (defun dired-map-dired-file-lines (fun)
225 ;; Perform FUN with point at the end of each non-directory line.
226 ;; FUN takes one argument, the filename (complete pathname).
227 (save-excursion
228 (let (file buffer-read-only)
229 (goto-char (point-min))
230 (while (not (eobp))
231 (save-excursion
232 (and (not (looking-at dired-re-dir))
233 (not (eolp))
234 (setq file (dired-get-filename nil t)) ; nil on non-file
235 (progn (end-of-line)
236 (funcall fun file))))
237 (forward-line 1)))))
239 (defun dired-collect-file-versions (fn)
240 ;; "If it looks like file FN has versions, return a list of the versions.
241 ;;That is a list of strings which are file names.
242 ;;The caller may want to flag some of these files for deletion."
243 (let* ((base-versions
244 (concat (file-name-nondirectory fn) ".~"))
245 (bv-length (length base-versions))
246 (possibilities (file-name-all-completions
247 base-versions
248 (file-name-directory fn)))
249 (versions (mapcar 'backup-extract-version possibilities)))
250 (if versions
251 (setq dired-file-version-alist (cons (cons fn versions)
252 dired-file-version-alist)))))
254 (defun dired-trample-file-versions (fn)
255 (let* ((start-vn (string-match "\\.~[0-9]+~$" fn))
256 base-version-list)
257 (and start-vn
258 (setq base-version-list ; there was a base version to which
259 (assoc (substring fn 0 start-vn) ; this looks like a
260 dired-file-version-alist)) ; subversion
261 (not (memq (string-to-int (substring fn (+ 2 start-vn)))
262 base-version-list)) ; this one doesn't make the cut
263 (progn (beginning-of-line)
264 (delete-char 1)
265 (insert dired-del-marker)))))
267 ;;; Shell commands
268 ;;>>> install (move this function into simple.el)
269 (defun dired-shell-quote (filename)
270 "Quote a file name for inferior shell (see variable `shell-file-name')."
271 ;; Quote everything except POSIX filename characters.
272 ;; This should be safe enough even for really weird shells.
273 (let ((result "") (start 0) end)
274 (while (string-match "[^---0-9a-zA-Z_./]" filename start)
275 (setq end (match-beginning 0)
276 result (concat result (substring filename start end)
277 "\\" (substring filename end (1+ end)))
278 start (1+ end)))
279 (concat result (substring filename start))))
281 (defun dired-read-shell-command (prompt arg files)
282 ;; "Read a dired shell command prompting with PROMPT (using read-string).
283 ;;ARG is the prefix arg and may be used to indicate in the prompt which
284 ;; files are affected.
285 ;;This is an extra function so that you can redefine it, e.g., to use gmhist."
286 (dired-mark-pop-up
287 nil 'shell files
288 (function read-string)
289 (format prompt (dired-mark-prompt arg files))))
291 ;; The in-background argument is only needed in Emacs 18 where
292 ;; shell-command doesn't understand an appended ampersand `&'.
293 ;;;###autoload
294 (defun dired-do-shell-command (command &optional arg)
295 "Run a shell command COMMAND on the marked files.
296 If no files are marked or a specific numeric prefix arg is given,
297 the next ARG files are used. Just \\[universal-argument] means the current file.
298 The prompt mentions the file(s) or the marker, as appropriate.
300 If there is output, it goes to a separate buffer.
302 Normally the command is run on each file individually.
303 However, if there is a `*' in the command then it is run
304 just once with the entire file list substituted there.
306 No automatic redisplay of dired buffers is attempted, as there's no
307 telling what files the command may have changed. Type
308 \\[dired-do-redisplay] to redisplay the marked files.
310 The shell command has the top level directory as working directory, so
311 output files usually are created there instead of in a subdir."
312 ;;Functions dired-run-shell-command and dired-shell-stuff-it do the
313 ;;actual work and can be redefined for customization.
314 (interactive (list
315 ;; Want to give feedback whether this file or marked files are used:
316 (dired-read-shell-command (concat "! on "
317 "%s: ")
318 current-prefix-arg
319 (dired-get-marked-files
320 t current-prefix-arg))
321 current-prefix-arg))
322 (let* ((on-each (not (string-match "\\*" command)))
323 (file-list (dired-get-marked-files t arg)))
324 (if on-each
325 (dired-bunch-files
326 (- 10000 (length command))
327 (function (lambda (&rest files)
328 (dired-run-shell-command
329 (dired-shell-stuff-it command files t arg))))
331 file-list)
332 ;; execute the shell command
333 (dired-run-shell-command
334 (dired-shell-stuff-it command file-list nil arg)))))
336 ;; Might use {,} for bash or csh:
337 (defvar dired-mark-prefix ""
338 "Prepended to marked files in dired shell commands.")
339 (defvar dired-mark-postfix ""
340 "Appended to marked files in dired shell commands.")
341 (defvar dired-mark-separator " "
342 "Separates marked files in dired shell commands.")
344 (defun dired-shell-stuff-it (command file-list on-each &optional raw-arg)
345 ;; "Make up a shell command line from COMMAND and FILE-LIST.
346 ;; If ON-EACH is t, COMMAND should be applied to each file, else
347 ;; simply concat all files and apply COMMAND to this.
348 ;; FILE-LIST's elements will be quoted for the shell."
349 ;; Might be redefined for smarter things and could then use RAW-ARG
350 ;; (coming from interactive P and currently ignored) to decide what to do.
351 ;; Smart would be a way to access basename or extension of file names.
352 ;; See dired-trns.el for an approach to this.
353 ;; Bug: There is no way to quote a *
354 ;; On the other hand, you can never accidentally get a * into your cmd.
355 (let ((stuff-it
356 (if (string-match "\\*" command)
357 (function (lambda (x)
358 (dired-replace-in-string "\\*" x command)))
359 (function (lambda (x) (concat command " " x))))))
360 (if on-each
361 (mapconcat stuff-it (mapcar 'dired-shell-quote file-list) ";")
362 (let ((fns (mapconcat 'dired-shell-quote
363 file-list dired-mark-separator)))
364 (if (> (length file-list) 1)
365 (setq fns (concat dired-mark-prefix fns dired-mark-postfix)))
366 (funcall stuff-it fns)))))
368 ;; This is an extra function so that it can be redefined by ange-ftp.
369 (defun dired-run-shell-command (command)
370 (shell-command command)
371 ;; Return nil for sake of nconc in dired-bunch-files.
372 nil)
374 ;; In Emacs 19 this will return program's exit status.
375 ;; This is a separate function so that ange-ftp can redefine it.
376 (defun dired-call-process (program discard &rest arguments)
377 ; "Run PROGRAM with output to current buffer unless DISCARD is t.
378 ;Remaining arguments are strings passed as command arguments to PROGRAM."
379 (apply 'call-process program nil (not discard) nil arguments))
381 (defun dired-check-process (msg program &rest arguments)
382 ; "Display MSG while running PROGRAM, and check for output.
383 ;Remaining arguments are strings passed as command arguments to PROGRAM.
384 ; On error, insert output
385 ; in a log buffer and return the offending ARGUMENTS or PROGRAM.
386 ; Caller can cons up a list of failed args.
387 ;Else returns nil for success."
388 (let (err-buffer err (dir default-directory))
389 (message "%s..." msg)
390 (save-excursion
391 ;; Get a clean buffer for error output:
392 (setq err-buffer (get-buffer-create " *dired-check-process output*"))
393 (set-buffer err-buffer)
394 (erase-buffer)
395 (setq default-directory dir ; caller's default-directory
396 err (/= 0
397 (apply (function dired-call-process) program nil arguments)))
398 (if err
399 (progn
400 (dired-log (concat program " " (prin1-to-string arguments) "\n"))
401 (dired-log err-buffer)
402 (or arguments program t))
403 (kill-buffer err-buffer)
404 (message "%s...done" msg)
405 nil))))
407 ;; Commands that delete or redisplay part of the dired buffer.
409 (defun dired-kill-line (&optional arg)
410 (interactive "P")
411 (setq arg (prefix-numeric-value arg))
412 (let (buffer-read-only file)
413 (while (/= 0 arg)
414 (setq file (dired-get-filename nil t))
415 (if (not file)
416 (error "Can only kill file lines.")
417 (save-excursion (and file
418 (dired-goto-subdir file)
419 (dired-kill-subdir)))
420 (delete-region (progn (beginning-of-line) (point))
421 (progn (forward-line 1) (point)))
422 (if (> arg 0)
423 (setq arg (1- arg))
424 (setq arg (1+ arg))
425 (forward-line -1))))
426 (dired-move-to-filename)))
428 ;;;###autoload
429 (defun dired-do-kill-lines (&optional arg fmt)
430 "Kill all marked lines (not the files).
431 With a prefix argument, kill that many lines starting with the current line.
432 \(A negative argument kills lines before the current line.)
433 To kill an entire subdirectory, go to its directory header line
434 and use this command with a prefix argument (the value does not matter)."
435 ;; Returns count of killed lines. FMT="" suppresses message.
436 (interactive "P")
437 (if arg
438 (if (dired-get-subdir)
439 (dired-kill-subdir)
440 (dired-kill-line arg))
441 (save-excursion
442 (goto-char (point-min))
443 (let (buffer-read-only (count 0))
444 (if (not arg) ; kill marked lines
445 (let ((regexp (dired-marker-regexp)))
446 (while (and (not (eobp))
447 (re-search-forward regexp nil t))
448 (setq count (1+ count))
449 (delete-region (progn (beginning-of-line) (point))
450 (progn (forward-line 1) (point)))))
451 ;; else kill unmarked lines
452 (while (not (eobp))
453 (if (or (dired-between-files)
454 (not (looking-at "^ ")))
455 (forward-line 1)
456 (setq count (1+ count))
457 (delete-region (point) (save-excursion
458 (forward-line 1)
459 (point))))))
460 (or (equal "" fmt)
461 (message (or fmt "Killed %d line%s.") count (dired-plural-s count)))
462 count))))
464 ;;;###end dired-cmd.el
466 ;;; 30K
467 ;;;###begin dired-cp.el
469 (defun dired-compress ()
470 ;; Compress or uncompress the current file.
471 ;; Return nil for success, offending filename else.
472 (let* (buffer-read-only
473 (from-file (dired-get-filename))
474 (new-file (dired-compress-file from-file)))
475 (if new-file
476 (progn (dired-update-file-line new-file) nil)
477 (dired-log (concat "Failed to compress" from-file))
478 from-file)))
480 (defun dired-compress-file (file)
481 ;; Compress or uncompress FILE.
482 ;; Return the name of the compressed or uncompressed file.
483 ;; Return nil if no change in files.
484 (let ((handler (find-file-name-handler file)))
485 (cond (handler
486 (funcall handler 'dired-compress-file file))
487 ((file-symlink-p file)
488 nil)
489 ((let (case-fold-search)
490 (string-match "\\.Z$" file))
491 (if (not (dired-check-process (concat "Uncompressing " file)
492 "uncompress" file))
493 (substring file 0 -2)))
494 ((let (case-fold-search)
495 (string-match "\\.gz$" file))
496 (if (not (dired-check-process (concat "Uncompressing " file)
497 "gunzip" file))
498 (substring file 0 -3)))
500 ;;; Try gzip; if we don't have that, use compress.
501 (condition-case nil
502 (if (not (dired-check-process (concat "Compressing " file)
503 "gzip" "-f" file))
504 (concat file ".gz"))
505 (file-error
506 (if (not (dired-check-process (concat "Compressing " file)
507 "compress" "-f" file))
508 (concat file ".Z"))))))))
510 (defun dired-mark-confirm (op-symbol arg)
511 ;; Request confirmation from the user that the operation described
512 ;; by OP-SYMBOL is to be performed on the marked files.
513 ;; Confirmation consists in a y-or-n question with a file list
514 ;; pop-up unless OP-SYMBOL is a member of `dired-no-confirm'.
515 ;; The files used are determined by ARG (as in dired-get-marked-files).
516 (or (memq op-symbol dired-no-confirm)
517 (let ((files (dired-get-marked-files t arg))
518 (string (if (eq op-symbol 'compress) "Compress or uncompress"
519 (capitalize (symbol-name op-symbol)))))
520 (dired-mark-pop-up nil op-symbol files (function y-or-n-p)
521 (concat string " "
522 (dired-mark-prompt arg files) "? ")))))
524 (defun dired-map-over-marks-check (fun arg op-symbol &optional show-progress)
525 ; "Map FUN over marked files (with second ARG like in dired-map-over-marks)
526 ; and display failures.
528 ; FUN takes zero args. It returns non-nil (the offending object, e.g.
529 ; the short form of the filename) for a failure and probably logs a
530 ; detailed error explanation using function `dired-log'.
532 ; OP-SYMBOL is a symbol describing the operation performed (e.g.
533 ; `compress'). It is used with `dired-mark-pop-up' to prompt the user
534 ; (e.g. with `Compress * [2 files]? ') and to display errors (e.g.
535 ; `Failed to compress 1 of 2 files - type W to see why ("foo")')
537 ; SHOW-PROGRESS if non-nil means redisplay dired after each file."
538 (if (dired-mark-confirm op-symbol arg)
539 (let* ((total-list;; all of FUN's return values
540 (dired-map-over-marks (funcall fun) arg show-progress))
541 (total (length total-list))
542 (failures (delq nil total-list))
543 (count (length failures))
544 (string (if (eq op-symbol 'compress) "Compress or uncompress"
545 (capitalize (symbol-name op-symbol)))))
546 (if (not failures)
547 (message "%s: %d file%s."
548 string total (dired-plural-s total))
549 ;; end this bunch of errors:
550 (dired-log-summary
551 (format "Failed to %s %d of %d file%s"
552 (downcase string) count total (dired-plural-s total))
553 failures)))))
555 (defvar dired-query-alist
556 '((?\y . y) (?\040 . y) ; `y' or SPC means accept once
557 (?n . n) (?\177 . n) ; `n' or DEL skips once
558 (?! . yes) ; `!' accepts rest
559 (?q. no) (?\e . no) ; `q' or ESC skips rest
560 ;; None of these keys quit - use C-g for that.
563 (defun dired-query (qs-var qs-prompt &rest qs-args)
564 ;; Query user and return nil or t.
565 ;; Store answer in symbol VAR (which must initially be bound to nil).
566 ;; Format PROMPT with ARGS.
567 ;; Binding variable help-form will help the user who types the help key.
568 (let* ((char (symbol-value qs-var))
569 (action (cdr (assoc char dired-query-alist))))
570 (cond ((eq 'yes action)
571 t) ; accept, and don't ask again
572 ((eq 'no action)
573 nil) ; skip, and don't ask again
574 (t;; no lasting effects from last time we asked - ask now
575 (let ((qprompt (concat qs-prompt
576 (if help-form
577 (format " [Type yn!q or %s] "
578 (key-description
579 (char-to-string help-char)))
580 " [Type y, n, q or !] ")))
581 result elt)
582 ;; Actually it looks nicer without cursor-in-echo-area - you can
583 ;; look at the dired buffer instead of at the prompt to decide.
584 (apply 'message qprompt qs-args)
585 (setq char (set qs-var (read-char)))
586 (while (not (setq elt (assoc char dired-query-alist)))
587 (message "Invalid char - type %c for help." help-char)
588 (ding)
589 (sit-for 1)
590 (apply 'message qprompt qs-args)
591 (setq char (set qs-var (read-char))))
592 (memq (cdr elt) '(t y yes)))))))
594 ;;;###autoload
595 (defun dired-do-compress (&optional arg)
596 "Compress or uncompress marked (or next ARG) files."
597 (interactive "P")
598 (dired-map-over-marks-check (function dired-compress) arg 'compress t))
600 ;; Commands for Emacs Lisp files - load and byte compile
602 (defun dired-byte-compile ()
603 ;; Return nil for success, offending file name else.
604 (let* ((filename (dired-get-filename))
605 (elc-file
606 (if (eq system-type 'vax-vms)
607 (concat (substring filename 0 (string-match ";" filename)) "c")
608 (concat filename "c")))
609 buffer-read-only failure)
610 (condition-case err
611 (save-excursion (byte-compile-file filename))
612 (error
613 (setq failure err)))
614 (if failure
615 (progn
616 (dired-log "Byte compile error for %s:\n%s\n" filename failure)
617 (dired-make-relative filename))
618 (dired-remove-file elc-file)
619 (forward-line) ; insert .elc after its .el file
620 (dired-add-file elc-file)
621 nil)))
623 ;;;###autoload
624 (defun dired-do-byte-compile (&optional arg)
625 "Byte compile marked (or next ARG) Emacs Lisp files."
626 (interactive "P")
627 (dired-map-over-marks-check (function dired-byte-compile) arg 'byte-compile t))
629 (defun dired-load ()
630 ;; Return nil for success, offending file name else.
631 (let ((file (dired-get-filename)) failure)
632 (condition-case err
633 (load file nil nil t)
634 (error (setq failure err)))
635 (if (not failure)
637 (dired-log "Load error for %s:\n%s\n" file failure)
638 (dired-make-relative file))))
640 ;;;###autoload
641 (defun dired-do-load (&optional arg)
642 "Load the marked (or next ARG) Emacs Lisp files."
643 (interactive "P")
644 (dired-map-over-marks-check (function dired-load) arg 'load t))
646 ;;;###autoload
647 (defun dired-do-redisplay (&optional arg test-for-subdir)
648 "Redisplay all marked (or next ARG) files.
649 If on a subdir line, redisplay that subdirectory. In that case,
650 a prefix arg lets you edit the `ls' switches used for the new listing."
651 ;; Moves point if the next ARG files are redisplayed.
652 (interactive "P\np")
653 (if (and test-for-subdir (dired-get-subdir))
654 (dired-insert-subdir
655 (dired-get-subdir)
656 (if arg (read-string "Switches for listing: " dired-actual-switches)))
657 (message "Redisplaying...")
658 ;; message much faster than making dired-map-over-marks show progress
659 (dired-map-over-marks (let ((fname (dired-get-filename)))
660 (message "Redisplaying... %s" fname)
661 (dired-update-file-line fname))
662 arg)
663 (dired-move-to-filename)
664 (message "Redisplaying...done")))
666 (defun dired-update-file-line (file)
667 ;; Delete the current line, and insert an entry for FILE.
668 ;; If FILE is nil, then just delete the current line.
669 ;; Keeps any marks that may be present in column one (doing this
670 ;; here is faster than with dired-add-entry's optional arg).
671 ;; Does not update other dired buffers. Use dired-relist-entry for that.
672 (beginning-of-line)
673 (let ((char (following-char)) (opoint (point))
674 (buffer-read-only))
675 (delete-region (point) (progn (forward-line 1) (point)))
676 (if file
677 (progn
678 (dired-add-entry file)
679 ;; Replace space by old marker without moving point.
680 ;; Faster than goto+insdel inside a save-excursion?
681 (subst-char-in-region opoint (1+ opoint) ?\040 char))))
682 (dired-move-to-filename))
684 (defun dired-fun-in-all-buffers (directory fun &rest args)
685 ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS.
686 ;; Return list of buffers where FUN succeeded (i.e., returned non-nil).
687 (let ((buf-list (dired-buffers-for-dir directory))
688 (obuf (current-buffer))
689 buf success-list)
690 (while buf-list
691 (setq buf (car buf-list)
692 buf-list (cdr buf-list))
693 (unwind-protect
694 (progn
695 (set-buffer buf)
696 (if (apply fun args)
697 (setq success-list (cons (buffer-name buf) success-list))))
698 (set-buffer obuf)))
699 success-list))
701 (defun dired-add-file (filename &optional marker-char)
702 (dired-fun-in-all-buffers
703 (file-name-directory filename)
704 (function dired-add-entry) filename marker-char))
706 (defun dired-add-entry (filename &optional marker-char)
707 ;; Add a new entry for FILENAME, optionally marking it
708 ;; with MARKER-CHAR (a character, else dired-marker-char is used).
709 ;; Note that this adds the entry `out of order' if files sorted by
710 ;; time, etc.
711 ;; At least this version inserts in the right subdirectory (if present).
712 ;; And it skips "." or ".." (see `dired-trivial-filenames').
713 ;; Hidden subdirs are exposed if a file is added there.
714 (setq filename (directory-file-name filename))
715 ;; Entry is always for files, even if they happen to also be directories
716 (let ((opoint (point))
717 (cur-dir (dired-current-directory))
718 (directory (file-name-directory filename))
719 reason)
720 (setq filename (file-name-nondirectory filename)
721 reason
722 (catch 'not-found
723 (if (string= directory cur-dir)
724 (progn
725 (skip-chars-forward "^\r\n")
726 (if (eq (following-char) ?\r)
727 (dired-unhide-subdir))
728 ;; We are already where we should be, except when
729 ;; point is before the subdir line or its total line.
730 (let ((p (dired-after-subdir-garbage cur-dir)))
731 (if (< (point) p)
732 (goto-char p))))
733 ;; else try to find correct place to insert
734 (if (dired-goto-subdir directory)
735 (progn;; unhide if necessary
736 (if (looking-at "\r");; point is at end of subdir line
737 (dired-unhide-subdir))
738 ;; found - skip subdir and `total' line
739 ;; and uninteresting files like . and ..
740 ;; This better not moves into the next subdir!
741 (dired-goto-next-nontrivial-file))
742 ;; not found
743 (throw 'not-found "Subdir not found")))
744 ;; found and point is at The Right Place:
745 (let (buffer-read-only)
746 (beginning-of-line)
747 (dired-add-entry-do-indentation marker-char)
748 ;; don't expand `.' !
749 (insert-directory (dired-make-absolute filename directory)
750 (concat dired-actual-switches "d"))
751 (forward-line -1)
752 ;; We want to have the non-directory part, only:
753 (let* ((beg (dired-move-to-filename t)) ; error for strange output
754 (end (dired-move-to-end-of-filename)))
755 (setq filename (buffer-substring beg end))
756 (delete-region beg end)
757 (insert (file-name-nondirectory filename)))
758 (if dired-after-readin-hook;; the subdir-alist is not affected...
759 (save-excursion;; ...so we can run it right now:
760 (save-restriction
761 (beginning-of-line)
762 (narrow-to-region (point) (save-excursion
763 (forward-line 1) (point)))
764 (run-hooks 'dired-after-readin-hook))))
765 (dired-move-to-filename))
766 ;; return nil if all went well
767 nil))
768 (if reason ; don't move away on failure
769 (goto-char opoint))
770 (not reason))) ; return t on success, nil else
772 ;; This is a separate function for the sake of nested dired format.
773 (defun dired-add-entry-do-indentation (marker-char)
774 ;; two spaces or a marker plus a space:
775 (insert (if marker-char
776 (if (integerp marker-char) marker-char dired-marker-char)
777 ?\040)
778 ?\040))
780 (defun dired-after-subdir-garbage (dir)
781 ;; Return pos of first file line of DIR, skipping header and total
782 ;; or wildcard lines.
783 ;; Important: never moves into the next subdir.
784 ;; DIR is assumed to be unhidden.
785 ;; Will probably be redefined for VMS etc.
786 (save-excursion
787 (or (dired-goto-subdir dir) (error "This cannot happen"))
788 (forward-line 1)
789 (while (and (not (eolp)) ; don't cross subdir boundary
790 (not (dired-move-to-filename)))
791 (forward-line 1))
792 (point)))
794 (defun dired-remove-file (file)
795 (dired-fun-in-all-buffers
796 (file-name-directory file) (function dired-remove-entry) file))
798 (defun dired-remove-entry (file)
799 (save-excursion
800 (and (dired-goto-file file)
801 (let (buffer-read-only)
802 (delete-region (progn (beginning-of-line) (point))
803 (save-excursion (forward-line 1) (point)))))))
805 (defun dired-relist-file (file)
806 (dired-fun-in-all-buffers (file-name-directory file)
807 (function dired-relist-entry) file))
809 (defun dired-relist-entry (file)
810 ;; Relist the line for FILE, or just add it if it did not exist.
811 ;; FILE must be an absolute pathname.
812 (let (buffer-read-only marker)
813 ;; If cursor is already on FILE's line delete-region will cause
814 ;; save-excursion to fail because of floating makers,
815 ;; moving point to beginning of line. Sigh.
816 (save-excursion
817 (and (dired-goto-file file)
818 (delete-region (progn (beginning-of-line)
819 (setq marker (following-char))
820 (point))
821 (save-excursion (forward-line 1) (point))))
822 (setq file (directory-file-name file))
823 (dired-add-entry file (if (eq ?\040 marker) nil marker)))))
825 ;;; Copy, move/rename, making hard and symbolic links
827 (defvar dired-backup-overwrite nil
828 "*Non-nil if Dired should ask about making backups before overwriting files.
829 Special value `always' suppresses confirmation.")
831 (defvar dired-overwrite-confirmed)
833 (defun dired-handle-overwrite (to)
834 ;; Save old version of a to be overwritten file TO.
835 ;; `dired-overwrite-confirmed' and `overwrite-backup-query' are fluid vars
836 ;; from dired-create-files.
837 (if (and dired-backup-overwrite
838 dired-overwrite-confirmed
839 (or (eq 'always dired-backup-overwrite)
840 (dired-query 'overwrite-backup-query
841 (format "Make backup for existing file `%s'? " to))))
842 (let ((backup (car (find-backup-file-name to))))
843 (rename-file to backup 0) ; confirm overwrite of old backup
844 (dired-relist-entry backup))))
846 (defun dired-copy-file (from to ok-flag)
847 (dired-handle-overwrite to)
848 (copy-file from to ok-flag dired-copy-preserve-time))
850 (defun dired-rename-file (from to ok-flag)
851 (dired-handle-overwrite to)
852 (rename-file from to ok-flag) ; error is caught in -create-files
853 ;; Silently rename the visited file of any buffer visiting this file.
854 (and (get-file-buffer from)
855 (save-excursion
856 (set-buffer (get-file-buffer from))
857 (let ((modflag (buffer-modified-p)))
858 (set-visited-file-name to)
859 (set-buffer-modified-p modflag))))
860 (dired-remove-file from)
861 ;; See if it's an inserted subdir, and rename that, too.
862 (dired-rename-subdir from to))
864 (defun dired-rename-subdir (from-dir to-dir)
865 (setq from-dir (file-name-as-directory from-dir)
866 to-dir (file-name-as-directory to-dir))
867 (dired-fun-in-all-buffers from-dir
868 (function dired-rename-subdir-1) from-dir to-dir)
869 ;; Update visited file name of all affected buffers
870 (let ((blist (buffer-list)))
871 (while blist
872 (save-excursion
873 (set-buffer (car blist))
874 (if (and buffer-file-name
875 (dired-in-this-tree buffer-file-name from-dir))
876 (let ((modflag (buffer-modified-p))
877 (to-file (dired-replace-in-string
878 (concat "^" (regexp-quote from-dir))
879 to-dir
880 buffer-file-name)))
881 (set-visited-file-name to-file)
882 (set-buffer-modified-p modflag))))
883 (setq blist (cdr blist)))))
885 (defun dired-rename-subdir-1 (dir to)
886 ;; Rename DIR to TO in headerlines and dired-subdir-alist, if DIR or
887 ;; one of its subdirectories is expanded in this buffer.
888 (let ((alist dired-subdir-alist)
889 (elt nil))
890 (while alist
891 (setq elt (car alist)
892 alist (cdr alist))
893 (if (dired-in-this-tree (car elt) dir)
894 ;; ELT's subdir is affected by the rename
895 (dired-rename-subdir-2 elt dir to)))
896 (if (equal dir default-directory)
897 ;; if top level directory was renamed, lots of things have to be
898 ;; updated:
899 (progn
900 (dired-unadvertise dir) ; we no longer dired DIR...
901 (setq default-directory to
902 dired-directory (expand-file-name;; this is correct
903 ;; with and without wildcards
904 (file-name-nondirectory dired-directory)
905 to))
906 (let ((new-name (file-name-nondirectory
907 (directory-file-name dired-directory))))
908 ;; try to rename buffer, but just leave old name if new
909 ;; name would already exist (don't try appending "<%d>")
910 (or (get-buffer new-name)
911 (rename-buffer new-name)))
912 ;; ... we dired TO now:
913 (dired-advertise)))))
915 (defun dired-rename-subdir-2 (elt dir to)
916 ;; Update the headerline and dired-subdir-alist element of directory
917 ;; described by alist-element ELT to reflect the moving of DIR to TO.
918 ;; Thus, ELT describes either DIR itself or a subdir of DIR.
919 (save-excursion
920 (let ((regexp (regexp-quote (directory-file-name dir)))
921 (newtext (directory-file-name to))
922 buffer-read-only)
923 (goto-char (dired-get-subdir-min elt))
924 ;; Update subdir headerline in buffer
925 (if (not (looking-at dired-subdir-regexp))
926 (error "%s not found where expected - dired-subdir-alist broken?"
927 dir)
928 (goto-char (match-beginning 1))
929 (if (re-search-forward regexp (match-end 1) t)
930 (replace-match newtext t t)
931 (error "Expected to find `%s' in headerline of %s" dir (car elt))))
932 ;; Update buffer-local dired-subdir-alist
933 (setcar elt
934 (dired-normalize-subdir
935 (dired-replace-in-string regexp newtext (car elt)))))))
937 ;; Cloning replace-match to work on strings instead of in buffer:
938 ;; The FIXEDCASE parameter of replace-match is not implemented.
939 ;;;###autoload
940 (defun dired-string-replace-match (regexp string newtext
941 &optional literal global)
942 "Replace first match of REGEXP in STRING with NEWTEXT.
943 If it does not match, nil is returned instead of the new string.
944 Optional arg LITERAL means to take NEWTEXT literally.
945 Optional arg GLOBAL means to replace all matches."
946 (if global
947 (let ((result "") (start 0) mb me)
948 (while (string-match regexp string start)
949 (setq mb (match-beginning 0)
950 me (match-end 0)
951 result (concat result
952 (substring string start mb)
953 (if literal
954 newtext
955 (dired-expand-newtext string newtext)))
956 start me))
957 (if mb ; matched at least once
958 (concat result (substring string start))
959 nil))
960 ;; not GLOBAL
961 (if (not (string-match regexp string 0))
963 (concat (substring string 0 (match-beginning 0))
964 (if literal newtext (dired-expand-newtext string newtext))
965 (substring string (match-end 0))))))
967 (defun dired-expand-newtext (string newtext)
968 ;; Expand \& and \1..\9 (referring to STRING) in NEWTEXT, using match data.
969 ;; Note that in Emacs 18 match data are clipped to current buffer
970 ;; size...so the buffer should better not be smaller than STRING.
971 (let ((pos 0)
972 (len (length newtext))
973 (expanded-newtext ""))
974 (while (< pos len)
975 (setq expanded-newtext
976 (concat expanded-newtext
977 (let ((c (aref newtext pos)))
978 (if (= ?\\ c)
979 (cond ((= ?\& (setq c
980 (aref newtext
981 (setq pos (1+ pos)))))
982 (substring string
983 (match-beginning 0)
984 (match-end 0)))
985 ((and (>= c ?1) (<= c ?9))
986 ;; return empty string if N'th
987 ;; sub-regexp did not match:
988 (let ((n (- c ?0)))
989 (if (match-beginning n)
990 (substring string
991 (match-beginning n)
992 (match-end n))
993 "")))
995 (char-to-string c)))
996 (char-to-string c)))))
997 (setq pos (1+ pos)))
998 expanded-newtext))
1000 ;; The basic function for half a dozen variations on cp/mv/ln/ln -s.
1001 (defun dired-create-files (file-creator operation fn-list name-constructor
1002 &optional marker-char)
1004 ;; Create a new file for each from a list of existing files. The user
1005 ;; is queried, dired buffers are updated, and at the end a success or
1006 ;; failure message is displayed
1008 ;; FILE-CREATOR must accept three args: oldfile newfile ok-if-already-exists
1010 ;; It is called for each file and must create newfile, the entry of
1011 ;; which will be added. The user will be queried if the file already
1012 ;; exists. If oldfile is removed by FILE-CREATOR (i.e, it is a
1013 ;; rename), it is FILE-CREATOR's responsibility to update dired
1014 ;; buffers. FILE-CREATOR must abort by signalling a file-error if it
1015 ;; could not create newfile. The error is caught and logged.
1017 ;; OPERATION (a capitalized string, e.g. `Copy') describes the
1018 ;; operation performed. It is used for error logging.
1020 ;; FN-LIST is the list of files to copy (full absolute pathnames).
1022 ;; NAME-CONSTRUCTOR returns a newfile for every oldfile, or nil to
1023 ;; skip. If it skips files for other reasons than a direct user
1024 ;; query, it is supposed to tell why (using dired-log).
1026 ;; Optional MARKER-CHAR is a character with which to mark every
1027 ;; newfile's entry, or t to use the current marker character if the
1028 ;; oldfile was marked.
1030 (let (failures skipped (success-count 0) (total (length fn-list)))
1031 (let (to overwrite-query
1032 overwrite-backup-query) ; for dired-handle-overwrite
1033 (mapcar
1034 (function
1035 (lambda (from)
1036 (setq to (funcall name-constructor from))
1037 (if (equal to from)
1038 (progn
1039 (setq to nil)
1040 (dired-log "Cannot %s to same file: %s\n"
1041 (downcase operation) from)))
1042 (if (not to)
1043 (setq skipped (cons (dired-make-relative from) skipped))
1044 (let* ((overwrite (file-exists-p to))
1045 (dired-overwrite-confirmed ; for dired-handle-overwrite
1046 (and overwrite
1047 (let ((help-form '(format "\
1048 Type SPC or `y' to overwrite file `%s',
1049 DEL or `n' to skip to next,
1050 ESC or `q' to not overwrite any of the remaining files,
1051 `!' to overwrite all remaining files with no more questions." to)))
1052 (dired-query 'overwrite-query
1053 "Overwrite `%s'?" to))))
1054 ;; must determine if FROM is marked before file-creator
1055 ;; gets a chance to delete it (in case of a move).
1056 (actual-marker-char
1057 (cond ((integerp marker-char) marker-char)
1058 (marker-char (dired-file-marker from)) ; slow
1059 (t nil))))
1060 (condition-case err
1061 (progn
1062 (funcall file-creator from to dired-overwrite-confirmed)
1063 (if overwrite
1064 ;; If we get here, file-creator hasn't been aborted
1065 ;; and the old entry (if any) has to be deleted
1066 ;; before adding the new entry.
1067 (dired-remove-file to))
1068 (setq success-count (1+ success-count))
1069 (message "%s: %d of %d" operation success-count total)
1070 (dired-add-file to actual-marker-char))
1071 (file-error ; FILE-CREATOR aborted
1072 (progn
1073 (setq failures (cons (dired-make-relative from) failures))
1074 (dired-log "%s `%s' to `%s' failed:\n%s\n"
1075 operation from to err))))))))
1076 fn-list))
1077 (cond
1078 (failures
1079 (dired-log-summary
1080 (format "%s failed for %d of %d file%s"
1081 operation (length failures) total
1082 (dired-plural-s total))
1083 failures))
1084 (skipped
1085 (dired-log-summary
1086 (format "%s: %d of %d file%s skipped"
1087 operation (length skipped) total
1088 (dired-plural-s total))
1089 skipped))
1091 (message "%s: %s file%s"
1092 operation success-count (dired-plural-s success-count)))))
1093 (dired-move-to-filename))
1095 (defun dired-do-create-files (op-symbol file-creator operation arg
1096 &optional marker-char op1
1097 how-to)
1098 ;; Create a new file for each marked file.
1099 ;; Prompts user for target, which is a directory in which to create
1100 ;; the new files. Target may be a plain file if only one marked
1101 ;; file exists.
1102 ;; OP-SYMBOL is the symbol for the operation. Function `dired-mark-pop-up'
1103 ;; will determine whether pop-ups are appropriate for this OP-SYMBOL.
1104 ;; FILE-CREATOR and OPERATION as in dired-create-files.
1105 ;; ARG as in dired-get-marked-files.
1106 ;; Optional arg OP1 is an alternate form for OPERATION if there is
1107 ;; only one file.
1108 ;; Optional arg MARKER-CHAR as in dired-create-files.
1109 ;; Optional arg HOW-TO determines how to treat target:
1110 ;; If HOW-TO is not given (or nil), and target is a directory, the
1111 ;; file(s) are created inside the target directory. If target
1112 ;; is not a directory, there must be exactly one marked file,
1113 ;; else error.
1114 ;; If HOW-TO is t, then target is not modified. There must be
1115 ;; exactly one marked file, else error.
1116 ;; Else HOW-TO is assumed to be a function of one argument, target,
1117 ;; that looks at target and returns a value for the into-dir
1118 ;; variable. The function dired-into-dir-with-symlinks is provided
1119 ;; for the case (common when creating symlinks) that symbolic
1120 ;; links to directories are not to be considered as directories
1121 ;; (as file-directory-p would if HOW-TO had been nil).
1122 (or op1 (setq op1 operation))
1123 (let* ((fn-list (dired-get-marked-files nil arg))
1124 (fn-count (length fn-list))
1125 (target (expand-file-name
1126 (dired-mark-read-file-name
1127 (concat (if (= 1 fn-count) op1 operation) " %s to: ")
1128 (dired-dwim-target-directory)
1129 op-symbol arg (mapcar (function dired-make-relative) fn-list))))
1130 (into-dir (cond ((null how-to) (file-directory-p target))
1131 ((eq how-to t) nil)
1132 (t (funcall how-to target)))))
1133 (if (and (> fn-count 1)
1134 (not into-dir))
1135 (error "Marked %s: target must be a directory: %s" operation target))
1136 ;; rename-file bombs when moving directories unless we do this:
1137 (or into-dir (setq target (directory-file-name target)))
1138 (dired-create-files
1139 file-creator operation fn-list
1140 (if into-dir ; target is a directory
1141 ;; This function uses fluid vars into-dir and target when called
1142 ;; inside dired-create-files:
1143 (function (lambda (from)
1144 (expand-file-name (file-name-nondirectory from) target)))
1145 (function (lambda (from) target)))
1146 marker-char)))
1148 ;; Read arguments for a marked-files command that wants a file name,
1149 ;; perhaps popping up the list of marked files.
1150 ;; ARG is the prefix arg and indicates whether the files came from
1151 ;; marks (ARG=nil) or a repeat factor (integerp ARG).
1152 ;; If the current file was used, the list has but one element and ARG
1153 ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)).
1155 (defun dired-mark-read-file-name (prompt dir op-symbol arg files)
1156 (dired-mark-pop-up
1157 nil op-symbol files
1158 (function read-file-name)
1159 (format prompt (dired-mark-prompt arg files)) dir))
1161 (defun dired-dwim-target-directory ()
1162 ;; Try to guess which target directory the user may want.
1163 ;; If there is a dired buffer displayed in the next window, use
1164 ;; its current subdir, else use current subdir of this dired buffer.
1165 (let ((this-dir (and (eq major-mode 'dired-mode)
1166 (dired-current-directory))))
1167 ;; non-dired buffer may want to profit from this function, e.g. vm-uudecode
1168 (if dired-dwim-target
1169 (let* ((other-buf (window-buffer (next-window)))
1170 (other-dir (save-excursion
1171 (set-buffer other-buf)
1172 (and (eq major-mode 'dired-mode)
1173 (dired-current-directory)))))
1174 (or other-dir this-dir))
1175 this-dir)))
1177 ;;;###autoload
1178 (defun dired-create-directory (directory)
1179 "Create a directory called DIRECTORY."
1180 (interactive
1181 (list (read-file-name "Create directory: " (dired-current-directory))))
1182 (let ((expanded (directory-file-name (expand-file-name directory))))
1183 (make-directory expanded)
1184 (dired-add-file expanded)
1185 (dired-move-to-filename)))
1187 (defun dired-into-dir-with-symlinks (target)
1188 (and (file-directory-p target)
1189 (not (file-symlink-p target))))
1190 ;; This may not always be what you want, especially if target is your
1191 ;; home directory and it happens to be a symbolic link, as is often the
1192 ;; case with NFS and automounters. Or if you want to make symlinks
1193 ;; into directories that themselves are only symlinks, also quite
1194 ;; common.
1196 ;; So we don't use this function as value for HOW-TO in
1197 ;; dired-do-symlink, which has the minor disadvantage of
1198 ;; making links *into* a symlinked-dir, when you really wanted to
1199 ;; *overwrite* that symlink. In that (rare, I guess) case, you'll
1200 ;; just have to remove that symlink by hand before making your marked
1201 ;; symlinks.
1203 ;;;###autoload
1204 (defun dired-do-copy (&optional arg)
1205 "Copy all marked (or next ARG) files, or copy the current file.
1206 This normally preserves the last-modified date when copying.
1207 When operating on just the current file, you specify the new name.
1208 When operating on multiple or marked files, you specify a directory
1209 and new symbolic links are made in that directory
1210 with the same names that the files currently have."
1211 (interactive "P")
1212 (dired-do-create-files 'copy (function dired-copy-file)
1213 (if dired-copy-preserve-time "Copy [-p]" "Copy")
1214 arg dired-keep-marker-copy))
1216 ;;;###autoload
1217 (defun dired-do-symlink (&optional arg)
1218 "Make symbolic links to current file or all marked (or next ARG) files.
1219 When operating on just the current file, you specify the new name.
1220 When operating on multiple or marked files, you specify a directory
1221 and new symbolic links are made in that directory
1222 with the same names that the files currently have."
1223 (interactive "P")
1224 (dired-do-create-files 'symlink (function make-symbolic-link)
1225 "Symlink" arg dired-keep-marker-symlink))
1227 ;;;###autoload
1228 (defun dired-do-hardlink (&optional arg)
1229 "Add names (hard links) current file or all marked (or next ARG) files.
1230 When operating on just the current file, you specify the new name.
1231 When operating on multiple or marked files, you specify a directory
1232 and new hard links are made in that directory
1233 with the same names that the files currently have."
1234 (interactive "P")
1235 (dired-do-create-files 'hardlink (function add-name-to-file)
1236 "Hardlink" arg dired-keep-marker-hardlink))
1238 ;;;###autoload
1239 (defun dired-do-rename (&optional arg)
1240 "Rename current file or all marked (or next ARG) files.
1241 When renaming just the current file, you specify the new name.
1242 When renaming multiple or marked files, you specify a directory."
1243 (interactive "P")
1244 (dired-do-create-files 'move (function dired-rename-file)
1245 "Move" arg dired-keep-marker-rename "Rename"))
1246 ;;;###end dired-cp.el
1248 ;;; 5K
1249 ;;;###begin dired-re.el
1250 (defun dired-do-create-files-regexp
1251 (file-creator operation arg regexp newname &optional whole-path marker-char)
1252 ;; Create a new file for each marked file using regexps.
1253 ;; FILE-CREATOR and OPERATION as in dired-create-files.
1254 ;; ARG as in dired-get-marked-files.
1255 ;; Matches each marked file against REGEXP and constructs the new
1256 ;; filename from NEWNAME (like in function replace-match).
1257 ;; Optional arg WHOLE-PATH means match/replace the whole pathname
1258 ;; instead of only the non-directory part of the file.
1259 ;; Optional arg MARKER-CHAR as in dired-create-files.
1260 (let* ((fn-list (dired-get-marked-files nil arg))
1261 (fn-count (length fn-list))
1262 (operation-prompt (concat operation " `%s' to `%s'?"))
1263 (rename-regexp-help-form (format "\
1264 Type SPC or `y' to %s one match, DEL or `n' to skip to next,
1265 `!' to %s all remaining matches with no more questions."
1266 (downcase operation)
1267 (downcase operation)))
1268 (regexp-name-constructor
1269 ;; Function to construct new filename using REGEXP and NEWNAME:
1270 (if whole-path ; easy (but rare) case
1271 (function
1272 (lambda (from)
1273 (let ((to (dired-string-replace-match regexp from newname))
1274 ;; must bind help-form directly around call to
1275 ;; dired-query
1276 (help-form rename-regexp-help-form))
1277 (if to
1278 (and (dired-query 'rename-regexp-query
1279 operation-prompt
1280 from
1283 (dired-log "%s: %s did not match regexp %s\n"
1284 operation from regexp)))))
1285 ;; not whole-path, replace non-directory part only
1286 (function
1287 (lambda (from)
1288 (let* ((new (dired-string-replace-match
1289 regexp (file-name-nondirectory from) newname))
1290 (to (and new ; nil means there was no match
1291 (expand-file-name new
1292 (file-name-directory from))))
1293 (help-form rename-regexp-help-form))
1294 (if to
1295 (and (dired-query 'rename-regexp-query
1296 operation-prompt
1297 (dired-make-relative from)
1298 (dired-make-relative to))
1300 (dired-log "%s: %s did not match regexp %s\n"
1301 operation (file-name-nondirectory from) regexp)))))))
1302 rename-regexp-query)
1303 (dired-create-files
1304 file-creator operation fn-list regexp-name-constructor marker-char)))
1306 (defun dired-mark-read-regexp (operation)
1307 ;; Prompt user about performing OPERATION.
1308 ;; Read and return list of: regexp newname arg whole-path.
1309 (let* ((whole-path
1310 (equal 0 (prefix-numeric-value current-prefix-arg)))
1311 (arg
1312 (if whole-path nil current-prefix-arg))
1313 (regexp
1314 (dired-read-regexp
1315 (concat (if whole-path "Path " "") operation " from (regexp): ")))
1316 (newname
1317 (read-string
1318 (concat (if whole-path "Path " "") operation " " regexp " to: "))))
1319 (list regexp newname arg whole-path)))
1321 ;;;###autoload
1322 (defun dired-do-rename-regexp (regexp newname &optional arg whole-path)
1323 "Rename marked files containing REGEXP to NEWNAME.
1324 As each match is found, the user must type a character saying
1325 what to do with it. For directions, type \\[help-command] at that time.
1326 NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
1327 REGEXP defaults to the last regexp used.
1328 With a zero prefix arg, renaming by regexp affects the complete
1329 pathname - usually only the non-directory part of file names is used
1330 and changed."
1331 (interactive (dired-mark-read-regexp "Rename"))
1332 (dired-do-create-files-regexp
1333 (function dired-rename-file)
1334 "Rename" arg regexp newname whole-path dired-keep-marker-rename))
1336 ;;;###autoload
1337 (defun dired-do-copy-regexp (regexp newname &optional arg whole-path)
1338 "Copy all marked files containing REGEXP to NEWNAME.
1339 See function `dired-rename-regexp' for more info."
1340 (interactive (dired-mark-read-regexp "Copy"))
1341 (dired-do-create-files-regexp
1342 (function dired-copy-file)
1343 (if dired-copy-preserve-time "Copy [-p]" "Copy")
1344 arg regexp newname whole-path dired-keep-marker-copy))
1346 ;;;###autoload
1347 (defun dired-do-hardlink-regexp (regexp newname &optional arg whole-path)
1348 "Hardlink all marked files containing REGEXP to NEWNAME.
1349 See function `dired-rename-regexp' for more info."
1350 (interactive (dired-mark-read-regexp "HardLink"))
1351 (dired-do-create-files-regexp
1352 (function add-name-to-file)
1353 "HardLink" arg regexp newname whole-path dired-keep-marker-hardlink))
1355 ;;;###autoload
1356 (defun dired-do-symlink-regexp (regexp newname &optional arg whole-path)
1357 "Symlink all marked files containing REGEXP to NEWNAME.
1358 See function `dired-rename-regexp' for more info."
1359 (interactive (dired-mark-read-regexp "SymLink"))
1360 (dired-do-create-files-regexp
1361 (function make-symbolic-link)
1362 "SymLink" arg regexp newname whole-path dired-keep-marker-symlink))
1364 (defun dired-create-files-non-directory
1365 (file-creator basename-constructor operation arg)
1366 ;; Perform FILE-CREATOR on the non-directory part of marked files
1367 ;; using function BASENAME-CONSTRUCTOR, with query for each file.
1368 ;; OPERATION like in dired-create-files, ARG as in dired-get-marked-files.
1369 (let (rename-non-directory-query)
1370 (dired-create-files
1371 file-creator
1372 operation
1373 (dired-get-marked-files nil arg)
1374 (function
1375 (lambda (from)
1376 (let ((to (concat (file-name-directory from)
1377 (funcall basename-constructor
1378 (file-name-nondirectory from)))))
1379 (and (let ((help-form (format "\
1380 Type SPC or `y' to %s one file, DEL or `n' to skip to next,
1381 `!' to %s all remaining matches with no more questions."
1382 (downcase operation)
1383 (downcase operation))))
1384 (dired-query 'rename-non-directory-query
1385 (concat operation " `%s' to `%s'")
1386 (dired-make-relative from)
1387 (dired-make-relative to)))
1388 to))))
1389 dired-keep-marker-rename)))
1391 (defun dired-rename-non-directory (basename-constructor operation arg)
1392 (dired-create-files-non-directory
1393 (function dired-rename-file)
1394 basename-constructor operation arg))
1396 ;;;###autoload
1397 (defun dired-upcase (&optional arg)
1398 "Rename all marked (or next ARG) files to upper case."
1399 (interactive "P")
1400 (dired-rename-non-directory (function upcase) "Rename upcase" arg))
1402 ;;;###autoload
1403 (defun dired-downcase (&optional arg)
1404 "Rename all marked (or next ARG) files to lower case."
1405 (interactive "P")
1406 (dired-rename-non-directory (function downcase) "Rename downcase" arg))
1408 ;;;###end dired-re.el
1410 ;;; 13K
1411 ;;;###begin dired-ins.el
1413 ;;;###autoload
1414 (defun dired-maybe-insert-subdir (dirname &optional
1415 switches no-error-if-not-dir-p)
1416 "Insert this subdirectory into the same dired buffer.
1417 If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
1418 else inserts it at its natural place (as `ls -lR' would have done).
1419 With a prefix arg, you may edit the ls switches used for this listing.
1420 You can add `R' to the switches to expand the whole tree starting at
1421 this subdirectory.
1422 This function takes some pains to conform to `ls -lR' output."
1423 (interactive
1424 (list (dired-get-filename)
1425 (if current-prefix-arg
1426 (read-string "Switches for listing: " dired-actual-switches))))
1427 (let ((opoint (point)))
1428 ;; We don't need a marker for opoint as the subdir is always
1429 ;; inserted *after* opoint.
1430 (setq dirname (file-name-as-directory dirname))
1431 (or (and (not switches)
1432 (dired-goto-subdir dirname))
1433 (dired-insert-subdir dirname switches no-error-if-not-dir-p))
1434 ;; Push mark so that it's easy to find back. Do this after the
1435 ;; insert message so that the user sees the `Mark set' message.
1436 (push-mark opoint)))
1438 (defun dired-insert-subdir (dirname &optional switches no-error-if-not-dir-p)
1439 "Insert this subdirectory into the same dired buffer.
1440 If it is already present, overwrites previous entry,
1441 else inserts it at its natural place (as `ls -lR' would have done).
1442 With a prefix arg, you may edit the `ls' switches used for this listing.
1443 You can add `R' to the switches to expand the whole tree starting at
1444 this subdirectory.
1445 This function takes some pains to conform to `ls -lR' output."
1446 ;; NO-ERROR-IF-NOT-DIR-P needed for special filesystems like
1447 ;; Prospero where dired-ls does the right thing, but
1448 ;; file-directory-p has not been redefined.
1449 (interactive
1450 (list (dired-get-filename)
1451 (if current-prefix-arg
1452 (read-string "Switches for listing: " dired-actual-switches))))
1453 (setq dirname (file-name-as-directory (expand-file-name dirname)))
1454 (dired-insert-subdir-validate dirname switches)
1455 (or no-error-if-not-dir-p
1456 (file-directory-p dirname)
1457 (error "Attempt to insert a non-directory: %s" dirname))
1458 (let ((elt (assoc dirname dired-subdir-alist))
1459 switches-have-R mark-alist case-fold-search buffer-read-only)
1460 ;; case-fold-search is nil now, so we can test for capital `R':
1461 (if (setq switches-have-R (and switches (string-match "R" switches)))
1462 ;; avoid duplicated subdirs
1463 (setq mark-alist (dired-kill-tree dirname t)))
1464 (if elt
1465 ;; If subdir is already present, remove it and remember its marks
1466 (setq mark-alist (nconc (dired-insert-subdir-del elt) mark-alist))
1467 (dired-insert-subdir-newpos dirname)) ; else compute new position
1468 (dired-insert-subdir-doupdate
1469 dirname elt (dired-insert-subdir-doinsert dirname switches))
1470 (if switches-have-R (dired-build-subdir-alist))
1471 (dired-initial-position dirname)
1472 (save-excursion (dired-mark-remembered mark-alist))))
1474 ;; This is a separate function for dired-vms.
1475 (defun dired-insert-subdir-validate (dirname &optional switches)
1476 ;; Check that it is valid to insert DIRNAME with SWITCHES.
1477 ;; Signal an error if invalid (e.g. user typed `i' on `..').
1478 (or (dired-in-this-tree dirname default-directory)
1479 (error "%s: not in this directory tree" dirname))
1480 (if switches
1481 (let (case-fold-search)
1482 (mapcar
1483 (function
1484 (lambda (x)
1485 (or (eq (null (string-match x switches))
1486 (null (string-match x dired-actual-switches)))
1487 (error "Can't have dirs with and without -%s switches together"
1488 x))))
1489 ;; all switches that make a difference to dired-get-filename:
1490 '("F" "b")))))
1492 (defun dired-alist-add (dir new-marker)
1493 ;; Add new DIR at NEW-MARKER. Sort alist.
1494 (dired-alist-add-1 dir new-marker)
1495 (dired-alist-sort))
1497 (defun dired-alist-sort ()
1498 ;; Keep the alist sorted on buffer position.
1499 (setq dired-subdir-alist
1500 (sort dired-subdir-alist
1501 (function (lambda (elt1 elt2)
1502 (> (dired-get-subdir-min elt1)
1503 (dired-get-subdir-min elt2)))))))
1505 (defun dired-kill-tree (dirname &optional remember-marks)
1506 ;;"Kill all proper subdirs of DIRNAME, excluding DIRNAME itself.
1507 ;; With optional arg REMEMBER-MARKS, return an alist of marked files."
1508 (interactive "DKill tree below directory: ")
1509 (let ((s-alist dired-subdir-alist) dir m-alist)
1510 (while s-alist
1511 (setq dir (car (car s-alist))
1512 s-alist (cdr s-alist))
1513 (if (and (not (string-equal dir dirname))
1514 (dired-in-this-tree dir dirname)
1515 (dired-goto-subdir dir))
1516 (setq m-alist (nconc (dired-kill-subdir remember-marks) m-alist))))
1517 m-alist))
1519 (defun dired-insert-subdir-newpos (new-dir)
1520 ;; Find pos for new subdir, according to tree order.
1521 ;;(goto-char (point-max))
1522 (let ((alist dired-subdir-alist) elt dir pos new-pos)
1523 (while alist
1524 (setq elt (car alist)
1525 alist (cdr alist)
1526 dir (car elt)
1527 pos (dired-get-subdir-min elt))
1528 (if (dired-tree-lessp dir new-dir)
1529 ;; Insert NEW-DIR after DIR
1530 (setq new-pos (dired-get-subdir-max elt)
1531 alist nil)))
1532 (goto-char new-pos))
1533 ;; want a separating newline between subdirs
1534 (or (eobp)
1535 (forward-line -1))
1536 (insert "\n")
1537 (point))
1539 (defun dired-insert-subdir-del (element)
1540 ;; Erase an already present subdir (given by ELEMENT) from buffer.
1541 ;; Move to that buffer position. Return a mark-alist.
1542 (let ((begin-marker (dired-get-subdir-min element)))
1543 (goto-char begin-marker)
1544 ;; Are at beginning of subdir (and inside it!). Now determine its end:
1545 (goto-char (dired-subdir-max))
1546 (or (eobp);; want a separating newline _between_ subdirs:
1547 (forward-char -1))
1548 (prog1
1549 (dired-remember-marks begin-marker (point))
1550 (delete-region begin-marker (point)))))
1552 (defun dired-insert-subdir-doinsert (dirname switches)
1553 ;; Insert ls output after point and put point on the correct
1554 ;; position for the subdir alist.
1555 ;; Return the boundary of the inserted text (as list of BEG and END).
1556 (let ((begin (point)) end)
1557 (message "Reading directory %s..." dirname)
1558 (let ((dired-actual-switches
1559 (or switches
1560 (dired-replace-in-string "R" "" dired-actual-switches))))
1561 (if (equal dirname (car (car (reverse dired-subdir-alist))))
1562 ;; top level directory may contain wildcards:
1563 (dired-readin-insert dired-directory)
1564 (insert-directory dirname dired-actual-switches nil t)))
1565 (message "Reading directory %s...done" dirname)
1566 (setq end (point-marker))
1567 (indent-rigidly begin end 2)
1568 ;; call dired-insert-headerline afterwards, as under VMS dired-ls
1569 ;; does insert the headerline itself and the insert function just
1570 ;; moves point.
1571 ;; Need a marker for END as this inserts text.
1572 (goto-char begin)
1573 (dired-insert-headerline dirname)
1574 ;; point is now like in dired-build-subdir-alist
1575 (prog1
1576 (list begin (marker-position end))
1577 (set-marker end nil))))
1579 (defun dired-insert-subdir-doupdate (dirname elt beg-end)
1580 ;; Point is at the correct subdir alist position for ELT,
1581 ;; BEG-END is the subdir-region (as list of begin and end).
1582 (if elt ; subdir was already present
1583 ;; update its position (should actually be unchanged)
1584 (set-marker (dired-get-subdir-min elt) (point-marker))
1585 (dired-alist-add dirname (point-marker)))
1586 ;; The hook may depend on the subdir-alist containing the just
1587 ;; inserted subdir, so run it after dired-alist-add:
1588 (if dired-after-readin-hook
1589 (save-excursion
1590 (let ((begin (nth 0 beg-end))
1591 (end (nth 1 beg-end)))
1592 (goto-char begin)
1593 (save-restriction
1594 (narrow-to-region begin end)
1595 ;; hook may add or delete lines, but the subdir boundary
1596 ;; marker floats
1597 (run-hooks 'dired-after-readin-hook))))))
1599 (defun dired-tree-lessp (dir1 dir2)
1600 ;; Lexicographic order on pathname components, like `ls -lR':
1601 ;; DIR1 < DIR2 iff DIR1 comes *before* DIR2 in an `ls -lR' listing,
1602 ;; i.e., iff DIR1 is a (grand)parent dir of DIR2,
1603 ;; or DIR1 and DIR2 are in the same parentdir and their last
1604 ;; components are string-lessp.
1605 ;; Thus ("/usr/" "/usr/bin") and ("/usr/a/" "/usr/b/") are tree-lessp.
1606 ;; string-lessp could arguably be replaced by file-newer-than-file-p
1607 ;; if dired-actual-switches contained `t'.
1608 (setq dir1 (file-name-as-directory dir1)
1609 dir2 (file-name-as-directory dir2))
1610 (let ((components-1 (dired-split "/" dir1))
1611 (components-2 (dired-split "/" dir2)))
1612 (while (and components-1
1613 components-2
1614 (equal (car components-1) (car components-2)))
1615 (setq components-1 (cdr components-1)
1616 components-2 (cdr components-2)))
1617 (let ((c1 (car components-1))
1618 (c2 (car components-2)))
1620 (cond ((and c1 c2)
1621 (string-lessp c1 c2))
1622 ((and (null c1) (null c2))
1623 nil) ; they are equal, not lessp
1624 ((null c1) ; c2 is a subdir of c1: c1<c2
1626 ((null c2) ; c1 is a subdir of c2: c1>c2
1627 nil)
1628 (t (error "This can't happen"))))))
1630 ;; There should be a builtin split function - inverse to mapconcat.
1631 (defun dired-split (pat str &optional limit)
1632 "Splitting on regexp PAT, turn string STR into a list of substrings.
1633 Optional third arg LIMIT (>= 1) is a limit to the length of the
1634 resulting list.
1635 Thus, if SEP is a regexp that only matches itself,
1637 (mapconcat 'identity (dired-split SEP STRING) SEP)
1639 is always equal to STRING."
1640 (let* ((start (string-match pat str))
1641 (result (list (substring str 0 start)))
1642 (count 1)
1643 (end (if start (match-end 0))))
1644 (if end ; else nothing left
1645 (while (and (or (not (integerp limit))
1646 (< count limit))
1647 (string-match pat str end))
1648 (setq start (match-beginning 0)
1649 count (1+ count)
1650 result (cons (substring str end start) result)
1651 end (match-end 0)
1652 start end)
1654 (if (and (or (not (integerp limit))
1655 (< count limit))
1656 end) ; else nothing left
1657 (setq result
1658 (cons (substring str end) result)))
1659 (nreverse result)))
1661 ;;; moving by subdirectories
1663 ;;;###autoload
1664 (defun dired-prev-subdir (arg &optional no-error-if-not-found no-skip)
1665 "Go to previous subdirectory, regardless of level.
1666 When called interactively and not on a subdir line, go to this subdir's line."
1667 ;;(interactive "p")
1668 (interactive
1669 (list (if current-prefix-arg
1670 (prefix-numeric-value current-prefix-arg)
1671 ;; if on subdir start already, don't stay there!
1672 (if (dired-get-subdir) 1 0))))
1673 (dired-next-subdir (- arg) no-error-if-not-found no-skip))
1675 (defun dired-subdir-min ()
1676 (save-excursion
1677 (if (not (dired-prev-subdir 0 t t))
1678 (error "Not in a subdir!")
1679 (point))))
1681 ;;;###autoload
1682 (defun dired-goto-subdir (dir)
1683 "Go to end of header line of DIR in this dired buffer.
1684 Return value of point on success, otherwise return nil.
1685 The next char is either \\n, or \\r if DIR is hidden."
1686 (interactive
1687 (prog1 ; let push-mark display its message
1688 (list (expand-file-name
1689 (completing-read "Goto in situ directory: " ; prompt
1690 dired-subdir-alist ; table
1691 nil ; predicate
1692 t ; require-match
1693 (dired-current-directory))))
1694 (push-mark)))
1695 (setq dir (file-name-as-directory dir))
1696 (let ((elt (assoc dir dired-subdir-alist)))
1697 (and elt
1698 (goto-char (dired-get-subdir-min elt))
1699 ;; dired-subdir-hidden-p and dired-add-entry depend on point being
1700 ;; at either \r or \n after this function succeeds.
1701 (progn (skip-chars-forward "^\r\n")
1702 (point)))))
1704 ;;;###autoload
1705 (defun dired-mark-subdir-files ()
1706 "Mark all files except `.' and `..'."
1707 (interactive "P")
1708 (let ((p-min (dired-subdir-min)))
1709 (dired-mark-files-in-region p-min (dired-subdir-max))))
1711 ;;;###autoload
1712 (defun dired-kill-subdir (&optional remember-marks)
1713 "Remove all lines of current subdirectory.
1714 Lower levels are unaffected."
1715 ;; With optional REMEMBER-MARKS, return a mark-alist.
1716 (interactive)
1717 (let ((beg (dired-subdir-min))
1718 (end (dired-subdir-max))
1719 buffer-read-only cur-dir)
1720 (setq cur-dir (dired-current-directory))
1721 (if (equal cur-dir default-directory)
1722 (error "Attempt to kill top level directory"))
1723 (prog1
1724 (if remember-marks (dired-remember-marks beg end))
1725 (delete-region beg end)
1726 (if (eobp) ; don't leave final blank line
1727 (delete-char -1))
1728 (dired-unsubdir cur-dir))))
1730 (defun dired-unsubdir (dir)
1731 ;; Remove DIR from the alist
1732 (setq dired-subdir-alist
1733 (delq (assoc dir dired-subdir-alist) dired-subdir-alist)))
1735 ;;;###autoload
1736 (defun dired-tree-up (arg)
1737 "Go up ARG levels in the dired tree."
1738 (interactive "p")
1739 (let ((dir (dired-current-directory)))
1740 (while (>= arg 1)
1741 (setq arg (1- arg)
1742 dir (file-name-directory (directory-file-name dir))))
1743 ;;(setq dir (expand-file-name dir))
1744 (or (dired-goto-subdir dir)
1745 (error "Cannot go up to %s - not in this tree." dir))))
1747 ;;;###autoload
1748 (defun dired-tree-down ()
1749 "Go down in the dired tree."
1750 (interactive)
1751 (let ((dir (dired-current-directory)) ; has slash
1752 pos case-fold-search) ; filenames are case sensitive
1753 (let ((rest (reverse dired-subdir-alist)) elt)
1754 (while rest
1755 (setq elt (car rest)
1756 rest (cdr rest))
1757 (if (dired-in-this-tree (directory-file-name (car elt)) dir)
1758 (setq rest nil
1759 pos (dired-goto-subdir (car elt))))))
1760 (if pos
1761 (goto-char pos)
1762 (error "At the bottom"))))
1764 ;;; hiding
1766 (defun dired-unhide-subdir ()
1767 (let (buffer-read-only)
1768 (subst-char-in-region (dired-subdir-min) (dired-subdir-max) ?\r ?\n)))
1770 (defun dired-hide-check ()
1771 (or selective-display
1772 (error "selective-display must be t for subdir hiding to work!")))
1774 (defun dired-subdir-hidden-p (dir)
1775 (and selective-display
1776 (save-excursion
1777 (dired-goto-subdir dir)
1778 (looking-at "\r"))))
1780 ;;;###autoload
1781 (defun dired-hide-subdir (arg)
1782 "Hide or unhide the current subdirectory and move to next directory.
1783 Optional prefix arg is a repeat factor.
1784 Use \\[dired-hide-all] to (un)hide all directories."
1785 (interactive "p")
1786 (dired-hide-check)
1787 (while (>= (setq arg (1- arg)) 0)
1788 (let* ((cur-dir (dired-current-directory))
1789 (hidden-p (dired-subdir-hidden-p cur-dir))
1790 (elt (assoc cur-dir dired-subdir-alist))
1791 (end-pos (1- (dired-get-subdir-max elt)))
1792 buffer-read-only)
1793 ;; keep header line visible, hide rest
1794 (goto-char (dired-get-subdir-min elt))
1795 (skip-chars-forward "^\n\r")
1796 (if hidden-p
1797 (subst-char-in-region (point) end-pos ?\r ?\n)
1798 (subst-char-in-region (point) end-pos ?\n ?\r)))
1799 (dired-next-subdir 1 t)))
1801 ;;;###autoload
1802 (defun dired-hide-all (arg)
1803 "Hide all subdirectories, leaving only their header lines.
1804 If there is already something hidden, make everything visible again.
1805 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
1806 (interactive "P")
1807 (dired-hide-check)
1808 (let (buffer-read-only)
1809 (if (save-excursion
1810 (goto-char (point-min))
1811 (search-forward "\r" nil t))
1812 ;; unhide - bombs on \r in filenames
1813 (subst-char-in-region (point-min) (point-max) ?\r ?\n)
1814 ;; hide
1815 (let ((pos (point-max)) ; pos of end of last directory
1816 (alist dired-subdir-alist))
1817 (while alist ; while there are dirs before pos
1818 (subst-char-in-region (dired-get-subdir-min (car alist)) ; pos of prev dir
1819 (save-excursion
1820 (goto-char pos) ; current dir
1821 ;; we're somewhere on current dir's line
1822 (forward-line -1)
1823 (point))
1824 ?\n ?\r)
1825 (setq pos (dired-get-subdir-min (car alist))) ; prev dir gets current dir
1826 (setq alist (cdr alist)))))))
1828 ;;;###end dired-ins.el
1830 ;;; dired-aux.el ends here