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