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