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