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