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