nXML is done.
[emacs.git] / lisp / dired-aux.el
blob6bef35963cd811bee219ba0962aa92d5c6df5b97
1 ;;; dired-aux.el --- less commonly used parts of dired -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1998, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>.
7 ;; Maintainer: FSF
8 ;; Keywords: files
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; The parts of dired mode not normally used. This is a space-saving hack
30 ;; to avoid having to load a large mode when all that's wanted are a few
31 ;; functions.
33 ;; Rewritten in 1990/1991 to add tree features, file marking and
34 ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
35 ;; Finished up by rms in 1992.
37 ;;; Code:
39 ;; We need macros in dired.el to compile properly.
40 (eval-when-compile (require 'dired))
42 (defvar dired-create-files-failures nil
43 "Variable where `dired-create-files' records failing file names.
44 Functions that operate recursively can store additional names
45 into this list; they also should call `dired-log' to log the errors.")
47 ;;; 15K
48 ;;;###begin dired-cmd.el
49 ;; Diffing and compressing
51 (defconst dired-star-subst-regexp "\\(^\\|[ \t]\\)\\*\\([ \t]\\|$\\)")
52 (defconst dired-quark-subst-regexp "\\(^\\|[ \t]\\)\\?\\([ \t]\\|$\\)")
54 ;;;###autoload
55 (defun dired-diff (file &optional switches)
56 "Compare file at point with file FILE using `diff'.
57 FILE defaults to the file at the mark. (That's the mark set by
58 \\[set-mark-command], not by Dired's \\[dired-mark] command.)
59 The prompted-for file is the first file given to `diff'.
60 With prefix arg, prompt for second argument SWITCHES,
61 which is options for `diff'."
62 (interactive
63 (let ((current (dired-get-filename t))
64 (default (if (mark t)
65 (save-excursion (goto-char (mark t))
66 (dired-get-filename t t)))))
67 (if (or (equal default current)
68 (and (not (equal (dired-dwim-target-directory)
69 (dired-current-directory)))
70 (not mark-active)))
71 (setq default nil))
72 (require 'diff)
73 (list (read-file-name (format "Diff %s with%s: "
74 current
75 (if default
76 (concat " (default " default ")")
77 ""))
78 (if default
79 (dired-current-directory)
80 (dired-dwim-target-directory))
81 default t)
82 (if current-prefix-arg
83 (read-string "Options for diff: "
84 (if (stringp diff-switches)
85 diff-switches
86 (mapconcat 'identity diff-switches " ")))))))
87 (diff file (dired-get-filename t) switches))
89 ;;;###autoload
90 (defun dired-backup-diff (&optional switches)
91 "Diff this file with its backup file or vice versa.
92 Uses the latest backup, if there are several numerical backups.
93 If this file is a backup, diff it with its original.
94 The backup file is the first file given to `diff'.
95 With prefix arg, prompt for argument SWITCHES which is options for `diff'."
96 (interactive
97 (if current-prefix-arg
98 (list (read-string "Options for diff: "
99 (if (stringp diff-switches)
100 diff-switches
101 (mapconcat 'identity diff-switches " "))))
102 nil))
103 (diff-backup (dired-get-filename) switches))
105 ;;;###autoload
106 (defun dired-compare-directories (dir2 predicate)
107 "Mark files with different file attributes in two dired buffers.
108 Compare file attributes of files in the current directory
109 with file attributes in directory DIR2 using PREDICATE on pairs of files
110 with the same name. Mark files for which PREDICATE returns non-nil.
111 Mark files with different names if PREDICATE is nil (or interactively
112 with empty input at the predicate prompt).
114 PREDICATE is a Lisp expression that can refer to the following variables:
116 size1, size2 - file size in bytes
117 mtime1, mtime2 - last modification time in seconds, as a float
118 fa1, fa2 - list of file attributes
119 returned by function `file-attributes'
121 where 1 refers to attribute of file in the current dired buffer
122 and 2 to attribute of file in second dired buffer.
124 Examples of PREDICATE:
126 (> mtime1 mtime2) - mark newer files
127 (not (= size1 size2)) - mark files with different sizes
128 (not (string= (nth 8 fa1) (nth 8 fa2))) - mark files with different modes
129 (not (and (= (nth 2 fa1) (nth 2 fa2)) - mark files with different UID
130 (= (nth 3 fa1) (nth 3 fa2)))) and GID."
131 (interactive
132 (list (read-directory-name (format "Compare %s with: "
133 (dired-current-directory))
134 (dired-dwim-target-directory)
135 (dired-dwim-target-directory))
136 (read-from-minibuffer "Mark if (lisp expr or RET): " nil nil t nil "nil")))
137 (let* ((dir1 (dired-current-directory))
138 (file-alist1 (dired-files-attributes dir1))
139 (file-alist2 (dired-files-attributes dir2))
140 file-list1 file-list2)
141 (setq file-alist1 (delq (assoc "." file-alist1) file-alist1))
142 (setq file-alist1 (delq (assoc ".." file-alist1) file-alist1))
143 (setq file-alist2 (delq (assoc "." file-alist2) file-alist2))
144 (setq file-alist2 (delq (assoc ".." file-alist2) file-alist2))
145 (setq file-list1 (mapcar
146 'cadr
147 (dired-file-set-difference
148 file-alist1 file-alist2
149 predicate))
150 file-list2 (mapcar
151 'cadr
152 (dired-file-set-difference
153 file-alist2 file-alist1
154 predicate)))
155 (dired-fun-in-all-buffers
156 dir1 nil
157 (lambda ()
158 (dired-mark-if
159 (member (dired-get-filename nil t) file-list1) nil)))
160 (dired-fun-in-all-buffers
161 dir2 nil
162 (lambda ()
163 (dired-mark-if
164 (member (dired-get-filename nil t) file-list2) nil)))
165 (message "Marked in dir1: %s files, in dir2: %s files"
166 (length file-list1)
167 (length file-list2))))
169 (defun dired-file-set-difference (list1 list2 predicate)
170 "Combine LIST1 and LIST2 using a set-difference operation.
171 The result list contains all file items that appear in LIST1 but not LIST2.
172 This is a non-destructive function; it makes a copy of the data if necessary
173 to avoid corrupting the original LIST1 and LIST2.
174 PREDICATE (see `dired-compare-directories') is an additional match
175 condition. Two file items are considered to match if they are equal
176 *and* PREDICATE evaluates to t."
177 (if (or (null list1) (null list2))
178 list1
179 (let (res)
180 (dolist (file1 list1)
181 (unless (let ((list list2))
182 (while (and list
183 (not (let* ((file2 (car list))
184 (fa1 (car (cddr file1)))
185 (fa2 (car (cddr file2)))
186 (size1 (nth 7 fa1))
187 (size2 (nth 7 fa2))
188 (mtime1 (float-time (nth 5 fa1)))
189 (mtime2 (float-time (nth 5 fa2))))
190 (and
191 (equal (car file1) (car file2))
192 (not (eval predicate))))))
193 (setq list (cdr list)))
194 list)
195 (setq res (cons file1 res))))
196 (nreverse res))))
198 (defun dired-files-attributes (dir)
199 "Return a list of all file names and attributes from DIR.
200 List has a form of (file-name full-file-name (attribute-list))"
201 (mapcar
202 (lambda (file-name)
203 (let ((full-file-name (expand-file-name file-name dir)))
204 (list file-name
205 full-file-name
206 (file-attributes full-file-name))))
207 (directory-files dir)))
210 (defun dired-touch-initial (files)
211 "Create initial input value for `touch' command."
212 (let (initial)
213 (while files
214 (let ((current (nth 5 (file-attributes (car files)))))
215 (if (and initial (not (equal initial current)))
216 (setq initial (current-time) files nil)
217 (setq initial current))
218 (setq files (cdr files))))
219 (format-time-string "%Y%m%d%H%M.%S" initial)))
221 (defun dired-do-chxxx (attribute-name program op-symbol arg)
222 ;; Change file attributes (mode, group, owner, timestamp) of marked files and
223 ;; refresh their file lines.
224 ;; ATTRIBUTE-NAME is a string describing the attribute to the user.
225 ;; PROGRAM is the program used to change the attribute.
226 ;; OP-SYMBOL is the type of operation (for use in dired-mark-pop-up).
227 ;; ARG describes which files to use, as in dired-get-marked-files.
228 (let* ((files (dired-get-marked-files t arg))
229 (new-attribute
230 (dired-mark-read-string
231 (concat "Change " attribute-name " of %s to: ")
232 (if (eq op-symbol 'touch) (dired-touch-initial files))
233 op-symbol arg files))
234 (operation (concat program " " new-attribute))
235 failures)
236 (setq failures
237 (dired-bunch-files 10000
238 (function dired-check-process)
239 (append
240 (list operation program)
241 (if (eq op-symbol 'touch)
242 '("-t") nil)
243 (list new-attribute)
244 (if (string-match "gnu" system-configuration)
245 '("--") nil))
246 files))
247 (dired-do-redisplay arg);; moves point if ARG is an integer
248 (if failures
249 (dired-log-summary
250 (format "%s: error" operation)
251 nil))))
253 ;;;###autoload
254 (defun dired-do-chmod (&optional arg)
255 "Change the mode of the marked (or next ARG) files.
256 Symbolic modes like `g+w' are allowed."
257 (interactive "P")
258 (let* ((files (dired-get-marked-files t arg))
259 (modes (dired-mark-read-string
260 "Change mode of %s to: " nil
261 'chmod arg files))
262 (num-modes (if (string-match "^[0-7]+" modes)
263 (string-to-number modes 8))))
264 (dolist (file files)
265 (set-file-modes
266 file
267 (if num-modes num-modes
268 (file-modes-symbolic-to-number modes (file-modes file)))))
269 (dired-do-redisplay arg)))
271 ;;;###autoload
272 (defun dired-do-chgrp (&optional arg)
273 "Change the group of the marked (or next ARG) files."
274 (interactive "P")
275 (if (memq system-type '(ms-dos windows-nt))
276 (error "chgrp not supported on this system"))
277 (dired-do-chxxx "Group" "chgrp" 'chgrp arg))
279 ;;;###autoload
280 (defun dired-do-chown (&optional arg)
281 "Change the owner of the marked (or next ARG) files."
282 (interactive "P")
283 (if (memq system-type '(ms-dos windows-nt))
284 (error "chown not supported on this system"))
285 (dired-do-chxxx "Owner" dired-chown-program 'chown arg))
287 ;;;###autoload
288 (defun dired-do-touch (&optional arg)
289 "Change the timestamp of the marked (or next ARG) files.
290 This calls touch."
291 (interactive "P")
292 (dired-do-chxxx "Timestamp" dired-touch-program 'touch arg))
294 ;; Process all the files in FILES in batches of a convenient size,
295 ;; by means of (FUNCALL FUNCTION ARGS... SOME-FILES...).
296 ;; Batches are chosen to need less than MAX chars for the file names,
297 ;; allowing 3 extra characters of separator per file name.
298 (defun dired-bunch-files (max function args files)
299 (let (pending
300 past
301 (pending-length 0)
302 failures)
303 ;; Accumulate files as long as they fit in MAX chars,
304 ;; then process the ones accumulated so far.
305 (while files
306 (let* ((thisfile (car files))
307 (thislength (+ (length thisfile) 3))
308 (rest (cdr files)))
309 ;; If we have at least 1 pending file
310 ;; and this file won't fit in the length limit, process now.
311 (if (and pending (> (+ thislength pending-length) max))
312 (setq pending (nreverse pending)
313 ;; The elements of PENDING are now in forward order.
314 ;; Do the operation and record failures.
315 failures (nconc (apply function (append args pending))
316 failures)
317 ;; Transfer the elemens of PENDING onto PAST
318 ;; and clear it out. Now PAST contains the first N files
319 ;; specified (for some N), and FILES contains the rest.
320 past (nconc past pending)
321 pending nil
322 pending-length 0))
323 ;; Do (setq pending (cons thisfile pending))
324 ;; but reuse the cons that was in `files'.
325 (setcdr files pending)
326 (setq pending files)
327 (setq pending-length (+ thislength pending-length))
328 (setq files rest)))
329 (setq pending (nreverse pending))
330 (prog1
331 (nconc (apply function (append args pending))
332 failures)
333 ;; Now the original list FILES has been put back as it was.
334 (nconc past pending))))
336 ;;;###autoload
337 (defun dired-do-print (&optional arg)
338 "Print the marked (or next ARG) files.
339 Uses the shell command coming from variables `lpr-command' and
340 `lpr-switches' as default."
341 (interactive "P")
342 (let* ((file-list (dired-get-marked-files t arg))
343 (command (dired-mark-read-string
344 "Print %s with: "
345 (mapconcat 'identity
346 (cons lpr-command
347 (if (stringp lpr-switches)
348 (list lpr-switches)
349 lpr-switches))
350 " ")
351 'print arg file-list)))
352 (dired-run-shell-command (dired-shell-stuff-it command file-list nil))))
354 ;; Read arguments for a marked-files command that wants a string
355 ;; that is not a file name,
356 ;; perhaps popping up the list of marked files.
357 ;; ARG is the prefix arg and indicates whether the files came from
358 ;; marks (ARG=nil) or a repeat factor (integerp ARG).
359 ;; If the current file was used, the list has but one element and ARG
360 ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)).
362 (defun dired-mark-read-string (prompt initial op-symbol arg files)
363 ;; PROMPT for a string, with INITIAL input.
364 ;; Other args are used to give user feedback and pop-up:
365 ;; OP-SYMBOL of command, prefix ARG, marked FILES.
366 (dired-mark-pop-up
367 nil op-symbol files
368 (function read-string)
369 (format prompt (dired-mark-prompt arg files)) initial))
371 ;;; Cleaning a directory: flagging some backups for deletion.
373 (defvar dired-file-version-alist)
375 ;;;###autoload
376 (defun dired-clean-directory (keep)
377 "Flag numerical backups for deletion.
378 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
379 Positive prefix arg KEEP overrides `dired-kept-versions';
380 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
382 To clear the flags on these files, you can use \\[dired-flag-backup-files]
383 with a prefix argument."
384 (interactive "P")
385 (setq keep (if keep (prefix-numeric-value keep) dired-kept-versions))
386 (let ((early-retention (if (< keep 0) (- keep) kept-old-versions))
387 (late-retention (if (<= keep 0) dired-kept-versions keep))
388 (dired-file-version-alist ()))
389 (message "Cleaning numerical backups (keeping %d late, %d old)..."
390 late-retention early-retention)
391 ;; Look at each file.
392 ;; If the file has numeric backup versions,
393 ;; put on dired-file-version-alist an element of the form
394 ;; (FILENAME . VERSION-NUMBER-LIST)
395 (dired-map-dired-file-lines (function dired-collect-file-versions))
396 ;; Sort each VERSION-NUMBER-LIST,
397 ;; and remove the versions not to be deleted.
398 (let ((fval dired-file-version-alist))
399 (while fval
400 (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
401 (v-count (length sorted-v-list)))
402 (if (> v-count (+ early-retention late-retention))
403 (rplacd (nthcdr early-retention sorted-v-list)
404 (nthcdr (- v-count late-retention)
405 sorted-v-list)))
406 (rplacd (car fval)
407 (cdr sorted-v-list)))
408 (setq fval (cdr fval))))
409 ;; Look at each file. If it is a numeric backup file,
410 ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
411 (dired-map-dired-file-lines (function dired-trample-file-versions))
412 (message "Cleaning numerical backups...done")))
414 ;;; Subroutines of dired-clean-directory.
416 (defun dired-map-dired-file-lines (fun)
417 ;; Perform FUN with point at the end of each non-directory line.
418 ;; FUN takes one argument, the absolute filename.
419 (save-excursion
420 (let (file buffer-read-only)
421 (goto-char (point-min))
422 (while (not (eobp))
423 (save-excursion
424 (and (not (looking-at dired-re-dir))
425 (not (eolp))
426 (setq file (dired-get-filename nil t)) ; nil on non-file
427 (progn (end-of-line)
428 (funcall fun file))))
429 (forward-line 1)))))
431 (defun dired-collect-file-versions (fn)
432 (let ((fn (file-name-sans-versions fn)))
433 ;; Only do work if this file is not already in the alist.
434 (if (assoc fn dired-file-version-alist)
436 ;; If it looks like file FN has versions, return a list of the versions.
437 ;;That is a list of strings which are file names.
438 ;;The caller may want to flag some of these files for deletion.
439 (let* ((base-versions
440 (concat (file-name-nondirectory fn) ".~"))
441 (backup-extract-version-start (length base-versions))
442 (possibilities (file-name-all-completions
443 base-versions
444 (file-name-directory fn)))
445 (versions (mapcar 'backup-extract-version possibilities)))
446 (if versions
447 (setq dired-file-version-alist
448 (cons (cons fn versions)
449 dired-file-version-alist)))))))
451 (defun dired-trample-file-versions (fn)
452 (let* ((start-vn (string-match "\\.~[0-9]+~$" fn))
453 base-version-list)
454 (and start-vn
455 (setq base-version-list ; there was a base version to which
456 (assoc (substring fn 0 start-vn) ; this looks like a
457 dired-file-version-alist)) ; subversion
458 (not (memq (string-to-number (substring fn (+ 2 start-vn)))
459 base-version-list)) ; this one doesn't make the cut
460 (progn (beginning-of-line)
461 (delete-char 1)
462 (insert dired-del-marker)))))
464 ;;; Shell commands
466 (declare-function mailcap-parse-mailcaps "gnus/mailcap" (&optional path force))
467 (declare-function mailcap-parse-mimetypes "gnus/mailcap" (&optional path force))
468 (declare-function mailcap-extension-to-mime "gnus/mailcap" (extn))
469 (declare-function mailcap-mime-info "gnus/mailcap" (string &optional request))
471 (defun dired-read-shell-command-default (files)
472 "Return a list of default commands for `dired-read-shell-command'."
473 (require 'mailcap)
474 (mailcap-parse-mailcaps)
475 (mailcap-parse-mimetypes)
476 (let* ((all-mime-type
477 ;; All unique MIME types from file extensions
478 (delete-dups (mapcar (lambda (file)
479 (mailcap-extension-to-mime
480 (file-name-extension file t)))
481 files)))
482 (all-mime-info
483 ;; All MIME info lists
484 (delete-dups (mapcar (lambda (mime-type)
485 (mailcap-mime-info mime-type 'all))
486 all-mime-type)))
487 (common-mime-info
488 ;; Intersection of mime-infos from different mime-types;
489 ;; or just the first MIME info for a single MIME type
490 (if (cdr all-mime-info)
491 (delq nil (mapcar (lambda (mi1)
492 (unless (memq nil (mapcar
493 (lambda (mi2)
494 (member mi1 mi2))
495 (cdr all-mime-info)))
496 mi1))
497 (car all-mime-info)))
498 (car all-mime-info)))
499 (commands
500 ;; Command strings from `viewer' field of the MIME info
501 (delq nil (mapcar (lambda (mime-info)
502 (let ((command (cdr (assoc 'viewer mime-info))))
503 (if (stringp command)
504 (replace-regexp-in-string
505 ;; Replace mailcap's `%s' placeholder
506 ;; with dired's `?' placeholder
507 "%s" "?"
508 (replace-regexp-in-string
509 ;; Remove the final filename placeholder
510 "\s*\\('\\)?%s\\1?\s*\\'" "" command nil t)
511 nil t))))
512 common-mime-info))))
513 commands))
515 (defun dired-read-shell-command (prompt arg files)
516 ;; "Read a dired shell command prompting with PROMPT (using read-string).
517 ;;ARG is the prefix arg and may be used to indicate in the prompt which
518 ;; files are affected.
519 ;;This is an extra function so that you can redefine it, e.g., to use gmhist."
520 (dired-mark-pop-up
521 nil 'shell files
522 (function read-string)
523 (format prompt (dired-mark-prompt arg files))
524 nil 'shell-command-history
525 (dired-read-shell-command-default files)))
527 ;; The in-background argument is only needed in Emacs 18 where
528 ;; shell-command doesn't understand an appended ampersand `&'.
529 ;;;###autoload
530 (defun dired-do-shell-command (command &optional arg file-list)
531 "Run a shell command COMMAND on the marked files.
532 If no files are marked or a specific numeric prefix arg is given,
533 the next ARG files are used. Just \\[universal-argument] means the current file.
534 The prompt mentions the file(s) or the marker, as appropriate.
536 If there is a `*' in COMMAND, surrounded by whitespace, this runs
537 COMMAND just once with the entire file list substituted there.
539 If there is no `*', but there is a `?' in COMMAND, surrounded by
540 whitespace, this runs COMMAND on each file individually with the
541 file name substituted for `?'.
543 Otherwise, this runs COMMAND on each file individually with the
544 file name added at the end of COMMAND (separated by a space).
546 `*' and `?' when not surrounded by whitespace have no special
547 significance for `dired-do-shell-command', and are passed through
548 normally to the shell, but you must confirm first. To pass `*' by
549 itself to the shell as a wildcard, type `*\"\"'.
551 If COMMAND produces output, it goes to a separate buffer.
553 This feature does not try to redisplay Dired buffers afterward, as
554 there's no telling what files COMMAND may have changed.
555 Type \\[dired-do-redisplay] to redisplay the marked files.
557 When COMMAND runs, its working directory is the top-level directory of
558 the Dired buffer, so output files usually are created there instead of
559 in a subdir.
561 In a noninteractive call (from Lisp code), you must specify
562 the list of file names explicitly with the FILE-LIST argument, which
563 can be produced by `dired-get-marked-files', for example."
564 ;;Functions dired-run-shell-command and dired-shell-stuff-it do the
565 ;;actual work and can be redefined for customization.
566 (interactive
567 (let ((files (dired-get-marked-files t current-prefix-arg)))
568 (list
569 ;; Want to give feedback whether this file or marked files are used:
570 (dired-read-shell-command (concat "! on "
571 "%s: ")
572 current-prefix-arg
573 files)
574 current-prefix-arg
575 files)))
576 (let* ((on-each (not (string-match dired-star-subst-regexp command)))
577 (subst (not (string-match dired-quark-subst-regexp command)))
578 (star (not (string-match "\\*" command)))
579 (qmark (not (string-match "\\?" command))))
580 ;; Get confirmation for wildcards that may have been meant
581 ;; to control substitution of a file name or the file name list.
582 (if (cond ((not (or on-each subst))
583 (error "You can not combine `*' and `?' substitution marks"))
584 ((and star (not on-each))
585 (y-or-n-p "Confirm--do you mean to use `*' as a wildcard? "))
586 ((and qmark (not subst))
587 (y-or-n-p "Confirm--do you mean to use `?' as a wildcard? "))
588 (t))
589 (if on-each
590 (dired-bunch-files
591 (- 10000 (length command))
592 (function (lambda (&rest files)
593 (dired-run-shell-command
594 (dired-shell-stuff-it command files t arg))))
596 file-list)
597 ;; execute the shell command
598 (dired-run-shell-command
599 (dired-shell-stuff-it command file-list nil arg))))))
601 ;; Might use {,} for bash or csh:
602 (defvar dired-mark-prefix ""
603 "Prepended to marked files in dired shell commands.")
604 (defvar dired-mark-postfix ""
605 "Appended to marked files in dired shell commands.")
606 (defvar dired-mark-separator " "
607 "Separates marked files in dired shell commands.")
609 (defun dired-shell-stuff-it (command file-list on-each &optional raw-arg)
610 ;; "Make up a shell command line from COMMAND and FILE-LIST.
611 ;; If ON-EACH is t, COMMAND should be applied to each file, else
612 ;; simply concat all files and apply COMMAND to this.
613 ;; FILE-LIST's elements will be quoted for the shell."
614 ;; Might be redefined for smarter things and could then use RAW-ARG
615 ;; (coming from interactive P and currently ignored) to decide what to do.
616 ;; Smart would be a way to access basename or extension of file names.
617 (let ((stuff-it
618 (if (or (string-match dired-star-subst-regexp command)
619 (string-match dired-quark-subst-regexp command))
620 (lambda (x)
621 (let ((retval command))
622 (while (string-match
623 "\\(^\\|[ \t]\\)\\([*?]\\)\\([ \t]\\|$\\)" retval)
624 (setq retval (replace-match x t t retval 2)))
625 retval))
626 (lambda (x) (concat command dired-mark-separator x)))))
627 (if on-each
628 (mapconcat stuff-it (mapcar 'shell-quote-argument file-list) ";")
629 (let ((files (mapconcat 'shell-quote-argument
630 file-list dired-mark-separator)))
631 (if (> (length file-list) 1)
632 (setq files (concat dired-mark-prefix files dired-mark-postfix)))
633 (funcall stuff-it files)))))
635 ;; This is an extra function so that it can be redefined by ange-ftp.
636 ;;;###autoload
637 (defun dired-run-shell-command (command)
638 (let ((handler
639 (find-file-name-handler (directory-file-name default-directory)
640 'shell-command)))
641 (if handler (apply handler 'shell-command (list command))
642 (shell-command command)))
643 ;; Return nil for sake of nconc in dired-bunch-files.
644 nil)
647 (defun dired-check-process (msg program &rest arguments)
648 ; "Display MSG while running PROGRAM, and check for output.
649 ;Remaining arguments are strings passed as command arguments to PROGRAM.
650 ; On error, insert output
651 ; in a log buffer and return the offending ARGUMENTS or PROGRAM.
652 ; Caller can cons up a list of failed args.
653 ;Else returns nil for success."
654 (let (err-buffer err (dir default-directory))
655 (message "%s..." msg)
656 (save-excursion
657 ;; Get a clean buffer for error output:
658 (setq err-buffer (get-buffer-create " *dired-check-process output*"))
659 (set-buffer err-buffer)
660 (erase-buffer)
661 (setq default-directory dir ; caller's default-directory
662 err (not (eq 0 (apply 'process-file program nil t nil arguments))))
663 (if err
664 (progn
665 (dired-log (concat program " " (prin1-to-string arguments) "\n"))
666 (dired-log err-buffer)
667 (or arguments program t))
668 (kill-buffer err-buffer)
669 (message "%s...done" msg)
670 nil))))
672 ;; Commands that delete or redisplay part of the dired buffer.
674 (defun dired-kill-line (&optional arg)
675 (interactive "P")
676 (setq arg (prefix-numeric-value arg))
677 (let (buffer-read-only file)
678 (while (/= 0 arg)
679 (setq file (dired-get-filename nil t))
680 (if (not file)
681 (error "Can only kill file lines")
682 (save-excursion (and file
683 (dired-goto-subdir file)
684 (dired-kill-subdir)))
685 (delete-region (progn (beginning-of-line) (point))
686 (progn (forward-line 1) (point)))
687 (if (> arg 0)
688 (setq arg (1- arg))
689 (setq arg (1+ arg))
690 (forward-line -1))))
691 (dired-move-to-filename)))
693 ;;;###autoload
694 (defun dired-do-kill-lines (&optional arg fmt)
695 "Kill all marked lines (not the files).
696 With a prefix argument, kill that many lines starting with the current line.
697 \(A negative argument kills backward.)
698 If you use this command with a prefix argument to kill the line
699 for a file that is a directory, which you have inserted in the
700 Dired buffer as a subdirectory, then it deletes that subdirectory
701 from the buffer as well.
702 To kill an entire subdirectory \(without killing its line in the
703 parent directory), go to its directory header line and use this
704 command with a prefix argument (the value does not matter)."
705 ;; Returns count of killed lines. FMT="" suppresses message.
706 (interactive "P")
707 (if arg
708 (if (dired-get-subdir)
709 (dired-kill-subdir)
710 (dired-kill-line arg))
711 (save-excursion
712 (goto-char (point-min))
713 (let (buffer-read-only
714 (count 0)
715 (regexp (dired-marker-regexp)))
716 (while (and (not (eobp))
717 (re-search-forward regexp nil t))
718 (setq count (1+ count))
719 (delete-region (progn (beginning-of-line) (point))
720 (progn (forward-line 1) (point))))
721 (or (equal "" fmt)
722 (message (or fmt "Killed %d line%s.") count (dired-plural-s count)))
723 count))))
725 ;;;###end dired-cmd.el
727 ;;; 30K
728 ;;;###begin dired-cp.el
730 (defun dired-compress ()
731 ;; Compress or uncompress the current file.
732 ;; Return nil for success, offending filename else.
733 (let* (buffer-read-only
734 (from-file (dired-get-filename))
735 (new-file (dired-compress-file from-file)))
736 (if new-file
737 (let ((start (point)))
738 ;; Remove any preexisting entry for the name NEW-FILE.
739 (condition-case nil
740 (dired-remove-entry new-file)
741 (error nil))
742 (goto-char start)
743 ;; Now replace the current line with an entry for NEW-FILE.
744 (dired-update-file-line new-file) nil)
745 (dired-log (concat "Failed to compress" from-file))
746 from-file)))
748 (defvar dired-compress-file-suffixes
749 '(("\\.gz\\'" "" "gunzip")
750 ("\\.tgz\\'" ".tar" "gunzip")
751 ("\\.Z\\'" "" "uncompress")
752 ;; For .z, try gunzip. It might be an old gzip file,
753 ;; or it might be from compact? pack? (which?) but gunzip handles both.
754 ("\\.z\\'" "" "gunzip")
755 ("\\.dz\\'" "" "dictunzip")
756 ("\\.tbz\\'" ".tar" "bunzip2")
757 ("\\.bz2\\'" "" "bunzip2")
758 ;; This item controls naming for compression.
759 ("\\.tar\\'" ".tgz" nil))
760 "Control changes in file name suffixes for compression and uncompression.
761 Each element specifies one transformation rule, and has the form:
762 (REGEXP NEW-SUFFIX PROGRAM)
763 The rule applies when the old file name matches REGEXP.
764 The new file name is computed by deleting the part that matches REGEXP
765 (as well as anything after that), then adding NEW-SUFFIX in its place.
766 If PROGRAM is non-nil, the rule is an uncompression rule,
767 and uncompression is done by running PROGRAM.
768 Otherwise, the rule is a compression rule, and compression is done with gzip.")
770 ;;;###autoload
771 (defun dired-compress-file (file)
772 ;; Compress or uncompress FILE.
773 ;; Return the name of the compressed or uncompressed file.
774 ;; Return nil if no change in files.
775 (let ((handler (find-file-name-handler file 'dired-compress-file))
776 suffix newname
777 (suffixes dired-compress-file-suffixes))
778 ;; See if any suffix rule matches this file name.
779 (while suffixes
780 (let (case-fold-search)
781 (if (string-match (car (car suffixes)) file)
782 (setq suffix (car suffixes) suffixes nil))
783 (setq suffixes (cdr suffixes))))
784 ;; If so, compute desired new name.
785 (if suffix
786 (setq newname (concat (substring file 0 (match-beginning 0))
787 (nth 1 suffix))))
788 (cond (handler
789 (funcall handler 'dired-compress-file file))
790 ((file-symlink-p file)
791 nil)
792 ((and suffix (nth 2 suffix))
793 ;; We found an uncompression rule.
794 (if (not (dired-check-process (concat "Uncompressing " file)
795 (nth 2 suffix) file))
796 newname))
798 ;;; We don't recognize the file as compressed, so compress it.
799 ;;; Try gzip; if we don't have that, use compress.
800 (condition-case nil
801 (let ((out-name (concat file ".gz")))
802 (and (or (not (file-exists-p out-name))
803 (y-or-n-p
804 (format "File %s already exists. Really compress? "
805 out-name)))
806 (not (dired-check-process (concat "Compressing " file)
807 "gzip" "-f" file))
808 (or (file-exists-p out-name)
809 (setq out-name (concat file ".z")))
810 ;; Rename the compressed file to NEWNAME
811 ;; if it hasn't got that name already.
812 (if (and newname (not (equal newname out-name)))
813 (progn
814 (rename-file out-name newname t)
815 newname)
816 out-name)))
817 (file-error
818 (if (not (dired-check-process (concat "Compressing " file)
819 "compress" "-f" file))
820 ;; Don't use NEWNAME with `compress'.
821 (concat file ".Z"))))))))
823 (defun dired-mark-confirm (op-symbol arg)
824 ;; Request confirmation from the user that the operation described
825 ;; by OP-SYMBOL is to be performed on the marked files.
826 ;; Confirmation consists in a y-or-n question with a file list
827 ;; pop-up unless OP-SYMBOL is a member of `dired-no-confirm'.
828 ;; The files used are determined by ARG (as in dired-get-marked-files).
829 (or (eq dired-no-confirm t)
830 (memq op-symbol dired-no-confirm)
831 ;; Pass t for DISTINGUISH-ONE-MARKED so that a single file which
832 ;; is marked pops up a window. That will help the user see
833 ;; it isn't the current line file.
834 (let ((files (dired-get-marked-files t arg nil t))
835 (string (if (eq op-symbol 'compress) "Compress or uncompress"
836 (capitalize (symbol-name op-symbol)))))
837 (dired-mark-pop-up nil op-symbol files (function y-or-n-p)
838 (concat string " "
839 (dired-mark-prompt arg files) "? ")))))
841 (defun dired-map-over-marks-check (fun arg op-symbol &optional show-progress)
842 ; "Map FUN over marked files (with second ARG like in dired-map-over-marks)
843 ; and display failures.
845 ; FUN takes zero args. It returns non-nil (the offending object, e.g.
846 ; the short form of the filename) for a failure and probably logs a
847 ; detailed error explanation using function `dired-log'.
849 ; OP-SYMBOL is a symbol describing the operation performed (e.g.
850 ; `compress'). It is used with `dired-mark-pop-up' to prompt the user
851 ; (e.g. with `Compress * [2 files]? ') and to display errors (e.g.
852 ; `Failed to compress 1 of 2 files - type W to see why ("foo")')
854 ; SHOW-PROGRESS if non-nil means redisplay dired after each file."
855 (if (dired-mark-confirm op-symbol arg)
856 (let* ((total-list;; all of FUN's return values
857 (dired-map-over-marks (funcall fun) arg show-progress))
858 (total (length total-list))
859 (failures (delq nil total-list))
860 (count (length failures))
861 (string (if (eq op-symbol 'compress) "Compress or uncompress"
862 (capitalize (symbol-name op-symbol)))))
863 (if (not failures)
864 (message "%s: %d file%s."
865 string total (dired-plural-s total))
866 ;; end this bunch of errors:
867 (dired-log-summary
868 (format "Failed to %s %d of %d file%s"
869 (downcase string) count total (dired-plural-s total))
870 failures)))))
872 (defvar dired-query-alist
873 '((?\y . y) (?\040 . y) ; `y' or SPC means accept once
874 (?n . n) (?\177 . n) ; `n' or DEL skips once
875 (?! . yes) ; `!' accepts rest
876 (?q . no) (?\e . no) ; `q' or ESC skips rest
877 ;; None of these keys quit - use C-g for that.
880 ;;;###autoload
881 (defun dired-query (qs-var qs-prompt &rest qs-args)
882 ;; Query user and return nil or t.
883 ;; Store answer in symbol VAR (which must initially be bound to nil).
884 ;; Format PROMPT with ARGS.
885 ;; Binding variable help-form will help the user who types the help key.
886 (let* ((char (symbol-value qs-var))
887 (action (cdr (assoc char dired-query-alist))))
888 (cond ((eq 'yes action)
889 t) ; accept, and don't ask again
890 ((eq 'no action)
891 nil) ; skip, and don't ask again
892 (t;; no lasting effects from last time we asked - ask now
893 (let ((qprompt (concat qs-prompt
894 (if help-form
895 (format " [Type yn!q or %s] "
896 (key-description
897 (char-to-string help-char)))
898 " [Type y, n, q or !] ")))
899 result elt)
900 ;; Actually it looks nicer without cursor-in-echo-area - you can
901 ;; look at the dired buffer instead of at the prompt to decide.
902 (apply 'message qprompt qs-args)
903 (setq char (set qs-var (read-char)))
904 (while (not (setq elt (assoc char dired-query-alist)))
905 (message "Invalid char - type %c for help." help-char)
906 (ding)
907 (sit-for 1)
908 (apply 'message qprompt qs-args)
909 (setq char (set qs-var (read-char))))
910 ;; Display the question with the answer.
911 (message "%s" (concat (apply 'format qprompt qs-args)
912 (char-to-string char)))
913 (memq (cdr elt) '(t y yes)))))))
915 ;;;###autoload
916 (defun dired-do-compress (&optional arg)
917 "Compress or uncompress marked (or next ARG) files."
918 (interactive "P")
919 (dired-map-over-marks-check (function dired-compress) arg 'compress t))
921 ;; Commands for Emacs Lisp files - load and byte compile
923 (defun dired-byte-compile ()
924 ;; Return nil for success, offending file name else.
925 (let* ((filename (dired-get-filename))
926 elc-file buffer-read-only failure)
927 (condition-case err
928 (save-excursion (byte-compile-file filename))
929 (error
930 (setq failure err)))
931 (setq elc-file (byte-compile-dest-file filename))
932 (or (file-exists-p elc-file)
933 (setq failure t))
934 (if failure
935 (progn
936 (dired-log "Byte compile error for %s:\n%s\n" filename failure)
937 (dired-make-relative filename))
938 (dired-remove-file elc-file)
939 (forward-line) ; insert .elc after its .el file
940 (dired-add-file elc-file)
941 nil)))
943 ;;;###autoload
944 (defun dired-do-byte-compile (&optional arg)
945 "Byte compile marked (or next ARG) Emacs Lisp files."
946 (interactive "P")
947 (dired-map-over-marks-check (function dired-byte-compile) arg 'byte-compile t))
949 (defun dired-load ()
950 ;; Return nil for success, offending file name else.
951 (let ((file (dired-get-filename)) failure)
952 (condition-case err
953 (load file nil nil t)
954 (error (setq failure err)))
955 (if (not failure)
957 (dired-log "Load error for %s:\n%s\n" file failure)
958 (dired-make-relative file))))
960 ;;;###autoload
961 (defun dired-do-load (&optional arg)
962 "Load the marked (or next ARG) Emacs Lisp files."
963 (interactive "P")
964 (dired-map-over-marks-check (function dired-load) arg 'load t))
966 ;;;###autoload
967 (defun dired-do-redisplay (&optional arg test-for-subdir)
968 "Redisplay all marked (or next ARG) files.
969 If on a subdir line, redisplay that subdirectory. In that case,
970 a prefix arg lets you edit the `ls' switches used for the new listing.
972 Dired remembers switches specified with a prefix arg, so that reverting
973 the buffer will not reset them. However, using `dired-undo' to re-insert
974 or delete subdirectories can bypass this machinery. Hence, you sometimes
975 may have to reset some subdirectory switches after a `dired-undo'.
976 You can reset all subdirectory switches to the default using
977 \\<dired-mode-map>\\[dired-reset-subdir-switches].
978 See Info node `(emacs)Subdir switches' for more details."
979 ;; Moves point if the next ARG files are redisplayed.
980 (interactive "P\np")
981 (if (and test-for-subdir (dired-get-subdir))
982 (let* ((dir (dired-get-subdir))
983 (switches (cdr (assoc-string dir dired-switches-alist))))
984 (dired-insert-subdir
986 (when arg
987 (read-string "Switches for listing: "
988 (or switches
989 dired-subdir-switches
990 dired-actual-switches)))))
991 (message "Redisplaying...")
992 ;; message much faster than making dired-map-over-marks show progress
993 (dired-uncache
994 (if (consp dired-directory) (car dired-directory) dired-directory))
995 (dired-map-over-marks (let ((fname (dired-get-filename)))
996 (message "Redisplaying... %s" fname)
997 (dired-update-file-line fname))
998 arg)
999 (dired-move-to-filename)
1000 (message "Redisplaying...done")))
1002 (defun dired-reset-subdir-switches ()
1003 "Set `dired-switches-alist' to nil and revert dired buffer."
1004 (interactive)
1005 (setq dired-switches-alist nil)
1006 (revert-buffer))
1008 (defun dired-update-file-line (file)
1009 ;; Delete the current line, and insert an entry for FILE.
1010 ;; If FILE is nil, then just delete the current line.
1011 ;; Keeps any marks that may be present in column one (doing this
1012 ;; here is faster than with dired-add-entry's optional arg).
1013 ;; Does not update other dired buffers. Use dired-relist-entry for that.
1014 (beginning-of-line)
1015 (let ((char (following-char)) (opoint (point))
1016 (buffer-read-only))
1017 (delete-region (point) (progn (forward-line 1) (point)))
1018 (if file
1019 (progn
1020 (dired-add-entry file nil t)
1021 ;; Replace space by old marker without moving point.
1022 ;; Faster than goto+insdel inside a save-excursion?
1023 (subst-char-in-region opoint (1+ opoint) ?\040 char))))
1024 (dired-move-to-filename))
1026 ;;;###autoload
1027 (defun dired-add-file (filename &optional marker-char)
1028 (dired-fun-in-all-buffers
1029 (file-name-directory filename) (file-name-nondirectory filename)
1030 (function dired-add-entry) filename marker-char))
1032 (defun dired-add-entry (filename &optional marker-char relative)
1033 ;; Add a new entry for FILENAME, optionally marking it
1034 ;; with MARKER-CHAR (a character, else dired-marker-char is used).
1035 ;; Note that this adds the entry `out of order' if files sorted by
1036 ;; time, etc.
1037 ;; At least this version inserts in the right subdirectory (if present).
1038 ;; And it skips "." or ".." (see `dired-trivial-filenames').
1039 ;; Hidden subdirs are exposed if a file is added there.
1040 (setq filename (directory-file-name filename))
1041 ;; Entry is always for files, even if they happen to also be directories
1042 (let* ((opoint (point))
1043 (cur-dir (dired-current-directory))
1044 (orig-file-name filename)
1045 (directory (if relative cur-dir (file-name-directory filename)))
1046 reason)
1047 (setq filename
1048 (if relative
1049 (file-relative-name filename directory)
1050 (file-name-nondirectory filename))
1051 reason
1052 (catch 'not-found
1053 (if (string= directory cur-dir)
1054 (progn
1055 (skip-chars-forward "^\r\n")
1056 (if (eq (following-char) ?\r)
1057 (dired-unhide-subdir))
1058 ;; We are already where we should be, except when
1059 ;; point is before the subdir line or its total line.
1060 (let ((p (dired-after-subdir-garbage cur-dir)))
1061 (if (< (point) p)
1062 (goto-char p))))
1063 ;; else try to find correct place to insert
1064 (if (dired-goto-subdir directory)
1065 (progn ;; unhide if necessary
1066 (if (looking-at "\r") ;; point is at end of subdir line
1067 (dired-unhide-subdir))
1068 ;; found - skip subdir and `total' line
1069 ;; and uninteresting files like . and ..
1070 ;; This better not moves into the next subdir!
1071 (dired-goto-next-nontrivial-file))
1072 ;; not found
1073 (throw 'not-found "Subdir not found")))
1074 (let (buffer-read-only opoint)
1075 (beginning-of-line)
1076 (setq opoint (point))
1077 ;; Don't expand `.'. Show just the file name within directory.
1078 (let ((default-directory directory))
1079 (dired-insert-directory directory
1080 (concat dired-actual-switches "d")
1081 (list filename)))
1082 (goto-char opoint)
1083 ;; Put in desired marker char.
1084 (when marker-char
1085 (let ((dired-marker-char
1086 (if (integerp marker-char) marker-char dired-marker-char)))
1087 (dired-mark nil)))
1088 ;; Compensate for a bug in ange-ftp.
1089 ;; It inserts the file's absolute name, rather than
1090 ;; the relative one. That may be hard to fix since it
1091 ;; is probably controlled by something in ftp.
1092 (goto-char opoint)
1093 (let ((inserted-name (dired-get-filename 'verbatim)))
1094 (if (file-name-directory inserted-name)
1095 (let (props)
1096 (end-of-line)
1097 (forward-char (- (length inserted-name)))
1098 (setq props (text-properties-at (point)))
1099 (delete-char (length inserted-name))
1100 (let ((pt (point)))
1101 (insert filename)
1102 (set-text-properties pt (point) props))
1103 (forward-char 1))
1104 (forward-line 1)))
1105 (forward-line -1)
1106 (if dired-after-readin-hook ;; the subdir-alist is not affected...
1107 (save-excursion ;; ...so we can run it right now:
1108 (save-restriction
1109 (beginning-of-line)
1110 (narrow-to-region (point) (save-excursion
1111 (forward-line 1) (point)))
1112 (run-hooks 'dired-after-readin-hook))))
1113 (dired-move-to-filename))
1114 ;; return nil if all went well
1115 nil))
1116 (if reason ; don't move away on failure
1117 (goto-char opoint))
1118 (not reason))) ; return t on success, nil else
1120 (defun dired-after-subdir-garbage (dir)
1121 ;; Return pos of first file line of DIR, skipping header and total
1122 ;; or wildcard lines.
1123 ;; Important: never moves into the next subdir.
1124 ;; DIR is assumed to be unhidden.
1125 ;; Will probably be redefined for VMS etc.
1126 (save-excursion
1127 (or (dired-goto-subdir dir) (error "This cannot happen"))
1128 (forward-line 1)
1129 (while (and (not (eolp)) ; don't cross subdir boundary
1130 (not (dired-move-to-filename)))
1131 (forward-line 1))
1132 (point)))
1134 ;;;###autoload
1135 (defun dired-remove-file (file)
1136 (dired-fun-in-all-buffers
1137 (file-name-directory file) (file-name-nondirectory file)
1138 (function dired-remove-entry) file))
1140 (defun dired-remove-entry (file)
1141 (save-excursion
1142 (and (dired-goto-file file)
1143 (let (buffer-read-only)
1144 (delete-region (progn (beginning-of-line) (point))
1145 (save-excursion (forward-line 1) (point)))))))
1147 ;;;###autoload
1148 (defun dired-relist-file (file)
1149 "Create or update the line for FILE in all Dired buffers it would belong in."
1150 (dired-fun-in-all-buffers (file-name-directory file)
1151 (file-name-nondirectory file)
1152 (function dired-relist-entry) file))
1154 (defun dired-relist-entry (file)
1155 ;; Relist the line for FILE, or just add it if it did not exist.
1156 ;; FILE must be an absolute file name.
1157 (let (buffer-read-only marker)
1158 ;; If cursor is already on FILE's line delete-region will cause
1159 ;; save-excursion to fail because of floating makers,
1160 ;; moving point to beginning of line. Sigh.
1161 (save-excursion
1162 (and (dired-goto-file file)
1163 (delete-region (progn (beginning-of-line)
1164 (setq marker (following-char))
1165 (point))
1166 (save-excursion (forward-line 1) (point))))
1167 (setq file (directory-file-name file))
1168 (dired-add-entry file (if (eq ?\040 marker) nil marker)))))
1170 ;;; Copy, move/rename, making hard and symbolic links
1172 (defcustom dired-backup-overwrite nil
1173 "*Non-nil if Dired should ask about making backups before overwriting files.
1174 Special value `always' suppresses confirmation."
1175 :type '(choice (const :tag "off" nil)
1176 (const :tag "suppress" always)
1177 (other :tag "ask" t))
1178 :group 'dired)
1180 (defvar dired-overwrite-confirmed)
1182 (defun dired-handle-overwrite (to)
1183 ;; Save old version of file TO that is to be overwritten.
1184 ;; `dired-overwrite-confirmed' and `overwrite-backup-query' are fluid vars
1185 ;; from dired-create-files.
1186 (let (backup)
1187 (if (and dired-backup-overwrite
1188 dired-overwrite-confirmed
1189 (setq backup (car (find-backup-file-name to)))
1190 (or (eq 'always dired-backup-overwrite)
1191 (dired-query 'overwrite-backup-query
1192 "Make backup for existing file `%s'? "
1193 to)))
1194 (progn
1195 (rename-file to backup 0) ; confirm overwrite of old backup
1196 (dired-relist-entry backup)))))
1198 ;;;###autoload
1199 (defun dired-copy-file (from to ok-flag)
1200 (dired-handle-overwrite to)
1201 (dired-copy-file-recursive from to ok-flag dired-copy-preserve-time t
1202 dired-recursive-copies))
1204 (declare-function make-symbolic-link "fileio.c")
1206 (defun dired-copy-file-recursive (from to ok-flag &optional
1207 preserve-time top recursive)
1208 (let ((attrs (file-attributes from))
1209 dirfailed)
1210 (if (and recursive
1211 (eq t (car attrs))
1212 (or (eq recursive 'always)
1213 (yes-or-no-p (format "Recursive copies of %s? " from))))
1214 ;; This is a directory.
1215 (let ((mode (file-modes from))
1216 (files
1217 (condition-case err
1218 (directory-files from nil dired-re-no-dot)
1219 (file-error
1220 (push (dired-make-relative from)
1221 dired-create-files-failures)
1222 (dired-log "Copying error for %s:\n%s\n" from err)
1223 (setq dirfailed t)
1224 nil))))
1225 (if (eq recursive 'top) (setq recursive 'always)) ; Don't ask any more.
1226 (unless dirfailed
1227 (if (file-exists-p to)
1228 (or top (dired-handle-overwrite to))
1229 (condition-case err
1230 (progn
1231 (make-directory to)
1232 (set-file-modes to #o700))
1233 (file-error
1234 (push (dired-make-relative from)
1235 dired-create-files-failures)
1236 (setq files nil)
1237 (dired-log "Copying error for %s:\n%s\n" from err)))))
1238 (dolist (file files)
1239 (let ((thisfrom (expand-file-name file from))
1240 (thisto (expand-file-name file to)))
1241 ;; Catch errors copying within a directory,
1242 ;; and report them through the dired log mechanism
1243 ;; just as our caller will do for the top level files.
1244 (condition-case err
1245 (dired-copy-file-recursive
1246 thisfrom thisto
1247 ok-flag preserve-time nil recursive)
1248 (file-error
1249 (push (dired-make-relative thisfrom)
1250 dired-create-files-failures)
1251 (dired-log "Copying error for %s:\n%s\n" thisfrom err)))))
1252 (when (file-directory-p to)
1253 (set-file-modes to mode)))
1254 ;; Not a directory.
1255 (or top (dired-handle-overwrite to))
1256 (condition-case err
1257 (if (stringp (car attrs))
1258 ;; It is a symlink
1259 (make-symbolic-link (car attrs) to ok-flag)
1260 (copy-file from to ok-flag dired-copy-preserve-time))
1261 (file-date-error
1262 (push (dired-make-relative from)
1263 dired-create-files-failures)
1264 (dired-log "Can't set date on %s:\n%s\n" from err))))))
1266 ;;;###autoload
1267 (defun dired-rename-file (file newname ok-if-already-exists)
1268 (dired-handle-overwrite newname)
1269 (rename-file file newname ok-if-already-exists) ; error is caught in -create-files
1270 ;; Silently rename the visited file of any buffer visiting this file.
1271 (and (get-file-buffer file)
1272 (with-current-buffer (get-file-buffer file)
1273 (set-visited-file-name newname nil t)))
1274 (dired-remove-file file)
1275 ;; See if it's an inserted subdir, and rename that, too.
1276 (dired-rename-subdir file newname))
1278 (defun dired-rename-subdir (from-dir to-dir)
1279 (setq from-dir (file-name-as-directory from-dir)
1280 to-dir (file-name-as-directory to-dir))
1281 (dired-fun-in-all-buffers from-dir nil
1282 (function dired-rename-subdir-1) from-dir to-dir)
1283 ;; Update visited file name of all affected buffers
1284 (let ((expanded-from-dir (expand-file-name from-dir))
1285 (blist (buffer-list)))
1286 (while blist
1287 (save-excursion
1288 (set-buffer (car blist))
1289 (if (and buffer-file-name
1290 (dired-in-this-tree buffer-file-name expanded-from-dir))
1291 (let ((modflag (buffer-modified-p))
1292 (to-file (dired-replace-in-string
1293 (concat "^" (regexp-quote from-dir))
1294 to-dir
1295 buffer-file-name)))
1296 (set-visited-file-name to-file)
1297 (set-buffer-modified-p modflag))))
1298 (setq blist (cdr blist)))))
1300 (defun dired-rename-subdir-1 (dir to)
1301 ;; Rename DIR to TO in headerlines and dired-subdir-alist, if DIR or
1302 ;; one of its subdirectories is expanded in this buffer.
1303 (let ((expanded-dir (expand-file-name dir))
1304 (alist dired-subdir-alist)
1305 (elt nil))
1306 (while alist
1307 (setq elt (car alist)
1308 alist (cdr alist))
1309 (if (dired-in-this-tree (car elt) expanded-dir)
1310 ;; ELT's subdir is affected by the rename
1311 (dired-rename-subdir-2 elt dir to)))
1312 (if (equal dir default-directory)
1313 ;; if top level directory was renamed, lots of things have to be
1314 ;; updated:
1315 (progn
1316 (dired-unadvertise dir) ; we no longer dired DIR...
1317 (setq default-directory to
1318 dired-directory (expand-file-name;; this is correct
1319 ;; with and without wildcards
1320 (file-name-nondirectory dired-directory)
1321 to))
1322 (let ((new-name (file-name-nondirectory
1323 (directory-file-name dired-directory))))
1324 ;; try to rename buffer, but just leave old name if new
1325 ;; name would already exist (don't try appending "<%d>")
1326 (or (get-buffer new-name)
1327 (rename-buffer new-name)))
1328 ;; ... we dired TO now:
1329 (dired-advertise)))))
1331 (defun dired-rename-subdir-2 (elt dir to)
1332 ;; Update the headerline and dired-subdir-alist element, as well as
1333 ;; dired-switches-alist element, of directory described by
1334 ;; alist-element ELT to reflect the moving of DIR to TO. Thus, ELT
1335 ;; describes either DIR itself or a subdir of DIR.
1336 (save-excursion
1337 (let ((regexp (regexp-quote (directory-file-name dir)))
1338 (newtext (directory-file-name to))
1339 buffer-read-only)
1340 (goto-char (dired-get-subdir-min elt))
1341 ;; Update subdir headerline in buffer
1342 (if (not (looking-at dired-subdir-regexp))
1343 (error "%s not found where expected - dired-subdir-alist broken?"
1344 dir)
1345 (goto-char (match-beginning 1))
1346 (if (re-search-forward regexp (match-end 1) t)
1347 (replace-match newtext t t)
1348 (error "Expected to find `%s' in headerline of %s" dir (car elt))))
1349 ;; Update buffer-local dired-subdir-alist and dired-switches-alist
1350 (let ((cons (assoc-string (car elt) dired-switches-alist))
1351 (cur-dir (dired-normalize-subdir
1352 (dired-replace-in-string regexp newtext (car elt)))))
1353 (setcar elt cur-dir)
1354 (when cons (setcar cons cur-dir))))))
1356 ;; The basic function for half a dozen variations on cp/mv/ln/ln -s.
1357 (defun dired-create-files (file-creator operation fn-list name-constructor
1358 &optional marker-char)
1360 ;; Create a new file for each from a list of existing files. The user
1361 ;; is queried, dired buffers are updated, and at the end a success or
1362 ;; failure message is displayed
1364 ;; FILE-CREATOR must accept three args: oldfile newfile ok-if-already-exists
1366 ;; It is called for each file and must create newfile, the entry of
1367 ;; which will be added. The user will be queried if the file already
1368 ;; exists. If oldfile is removed by FILE-CREATOR (i.e, it is a
1369 ;; rename), it is FILE-CREATOR's responsibility to update dired
1370 ;; buffers. FILE-CREATOR must abort by signaling a file-error if it
1371 ;; could not create newfile. The error is caught and logged.
1373 ;; OPERATION (a capitalized string, e.g. `Copy') describes the
1374 ;; operation performed. It is used for error logging.
1376 ;; FN-LIST is the list of files to copy (full absolute file names).
1378 ;; NAME-CONSTRUCTOR returns a newfile for every oldfile, or nil to
1379 ;; skip. If it skips files for other reasons than a direct user
1380 ;; query, it is supposed to tell why (using dired-log).
1382 ;; Optional MARKER-CHAR is a character with which to mark every
1383 ;; newfile's entry, or t to use the current marker character if the
1384 ;; oldfile was marked.
1386 (let (dired-create-files-failures failures
1387 skipped (success-count 0) (total (length fn-list)))
1388 (let (to overwrite-query
1389 overwrite-backup-query) ; for dired-handle-overwrite
1390 (mapc
1391 (function
1392 (lambda (from)
1393 (setq to (funcall name-constructor from))
1394 (if (equal to from)
1395 (progn
1396 (setq to nil)
1397 (dired-log "Cannot %s to same file: %s\n"
1398 (downcase operation) from)))
1399 (if (not to)
1400 (setq skipped (cons (dired-make-relative from) skipped))
1401 (let* ((overwrite (file-exists-p to))
1402 (dired-overwrite-confirmed ; for dired-handle-overwrite
1403 (and overwrite
1404 (let ((help-form '(format "\
1405 Type SPC or `y' to overwrite file `%s',
1406 DEL or `n' to skip to next,
1407 ESC or `q' to not overwrite any of the remaining files,
1408 `!' to overwrite all remaining files with no more questions." to)))
1409 (dired-query 'overwrite-query
1410 "Overwrite `%s'?" to))))
1411 ;; must determine if FROM is marked before file-creator
1412 ;; gets a chance to delete it (in case of a move).
1413 (actual-marker-char
1414 (cond ((integerp marker-char) marker-char)
1415 (marker-char (dired-file-marker from)) ; slow
1416 (t nil))))
1417 (condition-case err
1418 (progn
1419 (funcall file-creator from to dired-overwrite-confirmed)
1420 (if overwrite
1421 ;; If we get here, file-creator hasn't been aborted
1422 ;; and the old entry (if any) has to be deleted
1423 ;; before adding the new entry.
1424 (dired-remove-file to))
1425 (setq success-count (1+ success-count))
1426 (message "%s: %d of %d" operation success-count total)
1427 (dired-add-file to actual-marker-char))
1428 (file-error ; FILE-CREATOR aborted
1429 (progn
1430 (push (dired-make-relative from)
1431 failures)
1432 (dired-log "%s `%s' to `%s' failed:\n%s\n"
1433 operation from to err))))))))
1434 fn-list))
1435 (cond
1436 (dired-create-files-failures
1437 (setq failures (nconc failures dired-create-files-failures))
1438 (dired-log-summary
1439 (format "%s failed for %d file%s in %d requests"
1440 operation (length failures)
1441 (dired-plural-s (length failures))
1442 total)
1443 failures))
1444 (failures
1445 (dired-log-summary
1446 (format "%s failed for %d of %d file%s"
1447 operation (length failures)
1448 total (dired-plural-s total))
1449 failures))
1450 (skipped
1451 (dired-log-summary
1452 (format "%s: %d of %d file%s skipped"
1453 operation (length skipped) total
1454 (dired-plural-s total))
1455 skipped))
1457 (message "%s: %s file%s"
1458 operation success-count (dired-plural-s success-count)))))
1459 (dired-move-to-filename))
1461 (defun dired-do-create-files (op-symbol file-creator operation arg
1462 &optional marker-char op1
1463 how-to)
1464 "Create a new file for each marked file.
1465 Prompts user for target, which is a directory in which to create
1466 the new files. Target may be a plain file if only one marked
1467 file exists. The way the default for the target directory is
1468 computed depends on the value of `dired-dwim-target-directory'.
1469 OP-SYMBOL is the symbol for the operation. Function `dired-mark-pop-up'
1470 will determine whether pop-ups are appropriate for this OP-SYMBOL.
1471 FILE-CREATOR and OPERATION as in `dired-create-files'.
1472 ARG as in `dired-get-marked-files'.
1473 Optional arg MARKER-CHAR as in `dired-create-files'.
1474 Optional arg OP1 is an alternate form for OPERATION if there is
1475 only one file.
1476 Optional arg HOW-TO is used to set the value of the into-dir variable
1477 which determines how to treat target.
1478 If into-dir is set to nil then target is not regarded as a directory,
1479 there must be exactly one marked file, else error.
1480 Else if into-dir is set to a list, then target is a generalized
1481 directory (e.g. some sort of archive). The first element of into-dir
1482 must be a function with at least four arguments:
1483 operation as OPERATION above.
1484 rfn-list a list of the relative names for the marked files.
1485 fn-list a list of the absolute names for the marked files.
1486 target.
1487 The rest of into-dir are optional arguments.
1488 Else into-dir is not a list. Target is a directory.
1489 The marked file(s) are created inside the target directory.
1491 If HOW-TO is not given (or nil), then into-dir is set to true if
1492 target is a directory and otherwise to nil.
1493 Else if HOW-TO is t, then into-dir is set to nil.
1494 Else HOW-TO is assumed to be a function of one argument, target,
1495 that looks at target and returns a value for the into-dir
1496 variable. The function `dired-into-dir-with-symlinks' is provided
1497 for the case (common when creating symlinks) that symbolic
1498 links to directories are not to be considered as directories
1499 (as `file-directory-p' would if HOW-TO had been nil)."
1500 (or op1 (setq op1 operation))
1501 (let* ((fn-list (dired-get-marked-files nil arg))
1502 (rfn-list (mapcar (function dired-make-relative) fn-list))
1503 (dired-one-file ; fluid variable inside dired-create-files
1504 (and (consp fn-list) (null (cdr fn-list)) (car fn-list)))
1505 (target-dir (dired-dwim-target-directory))
1506 (default (and dired-one-file
1507 (expand-file-name (file-name-nondirectory (car fn-list))
1508 target-dir)))
1509 (target (expand-file-name ; fluid variable inside dired-create-files
1510 (dired-mark-read-file-name
1511 (concat (if dired-one-file op1 operation) " %s to: ")
1512 target-dir op-symbol arg rfn-list default)))
1513 (into-dir (cond ((null how-to)
1514 ;; Allow DOS/Windows users to change the letter
1515 ;; case of a directory. If we don't test these
1516 ;; conditions up front, file-directory-p below
1517 ;; will return t because the filesystem is
1518 ;; case-insensitive, and Emacs will try to move
1519 ;; foo -> foo/foo, which fails.
1520 (if (and (memq system-type '(ms-dos windows-nt cygwin))
1521 (eq op-symbol 'move)
1522 dired-one-file
1523 (string= (downcase
1524 (expand-file-name (car fn-list)))
1525 (downcase
1526 (expand-file-name target)))
1527 (not (string=
1528 (file-name-nondirectory (car fn-list))
1529 (file-name-nondirectory target))))
1531 (file-directory-p target)))
1532 ((eq how-to t) nil)
1533 (t (funcall how-to target)))))
1534 (if (and (consp into-dir) (functionp (car into-dir)))
1535 (apply (car into-dir) operation rfn-list fn-list target (cdr into-dir))
1536 (if (not (or dired-one-file into-dir))
1537 (error "Marked %s: target must be a directory: %s" operation target))
1538 ;; rename-file bombs when moving directories unless we do this:
1539 (or into-dir (setq target (directory-file-name target)))
1540 (dired-create-files
1541 file-creator operation fn-list
1542 (if into-dir ; target is a directory
1543 ;; This function uses fluid variable target when called
1544 ;; inside dired-create-files:
1545 (function
1546 (lambda (from)
1547 (expand-file-name (file-name-nondirectory from) target)))
1548 (function (lambda (from) target)))
1549 marker-char))))
1551 ;; Read arguments for a marked-files command that wants a file name,
1552 ;; perhaps popping up the list of marked files.
1553 ;; ARG is the prefix arg and indicates whether the files came from
1554 ;; marks (ARG=nil) or a repeat factor (integerp ARG).
1555 ;; If the current file was used, the list has but one element and ARG
1556 ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)).
1557 ;; DEFAULT is the default value to return if the user just hits RET;
1558 ;; if it is omitted or nil, then the name of the directory is used.
1560 (defun dired-mark-read-file-name (prompt dir op-symbol arg files
1561 &optional default)
1562 (dired-mark-pop-up
1563 nil op-symbol files
1564 (function read-file-name)
1565 (format prompt (dired-mark-prompt arg files)) dir default))
1567 (defun dired-dwim-target-directory ()
1568 ;; Try to guess which target directory the user may want.
1569 ;; If there is a dired buffer displayed in the next window, use
1570 ;; its current subdir, else use current subdir of this dired buffer.
1571 (let ((this-dir (and (eq major-mode 'dired-mode)
1572 (dired-current-directory))))
1573 ;; non-dired buffer may want to profit from this function, e.g. vm-uudecode
1574 (if dired-dwim-target
1575 (let* ((other-buf (window-buffer (next-window)))
1576 (other-dir (save-excursion
1577 (set-buffer other-buf)
1578 (and (eq major-mode 'dired-mode)
1579 (dired-current-directory)))))
1580 (or other-dir this-dir))
1581 this-dir)))
1583 ;;;###autoload
1584 (defun dired-create-directory (directory)
1585 "Create a directory called DIRECTORY."
1586 (interactive
1587 (list (read-file-name "Create directory: " (dired-current-directory))))
1588 (let* ((expanded (directory-file-name (expand-file-name directory)))
1589 (try expanded) new)
1590 ;; Find the topmost nonexistent parent dir (variable `new')
1591 (while (and try (not (file-exists-p try)) (not (equal new try)))
1592 (setq new try
1593 try (directory-file-name (file-name-directory try))))
1594 (make-directory expanded t)
1595 (when new
1596 (dired-add-file new)
1597 (dired-move-to-filename))))
1599 (defun dired-into-dir-with-symlinks (target)
1600 (and (file-directory-p target)
1601 (not (file-symlink-p target))))
1602 ;; This may not always be what you want, especially if target is your
1603 ;; home directory and it happens to be a symbolic link, as is often the
1604 ;; case with NFS and automounters. Or if you want to make symlinks
1605 ;; into directories that themselves are only symlinks, also quite
1606 ;; common.
1608 ;; So we don't use this function as value for HOW-TO in
1609 ;; dired-do-symlink, which has the minor disadvantage of
1610 ;; making links *into* a symlinked-dir, when you really wanted to
1611 ;; *overwrite* that symlink. In that (rare, I guess) case, you'll
1612 ;; just have to remove that symlink by hand before making your marked
1613 ;; symlinks.
1615 (defvar dired-copy-how-to-fn nil
1616 "nil or a function used by `dired-do-copy' to determine target.
1617 See HOW-TO argument for `dired-do-create-files'.")
1619 ;;;###autoload
1620 (defun dired-do-copy (&optional arg)
1621 "Copy all marked (or next ARG) files, or copy the current file.
1622 This normally preserves the last-modified date when copying.
1623 When operating on just the current file, you specify the new name.
1624 When operating on multiple or marked files, you specify a directory,
1625 and new copies of these files are made in that directory
1626 with the same names that the files currently have. The default
1627 suggested for the target directory depends on the value of
1628 `dired-dwim-target', which see.
1630 This command copies symbolic links by creating new ones,
1631 like `cp -d'."
1632 (interactive "P")
1633 (let ((dired-recursive-copies dired-recursive-copies))
1634 (dired-do-create-files 'copy (function dired-copy-file)
1635 "Copy"
1636 arg dired-keep-marker-copy
1637 nil dired-copy-how-to-fn)))
1639 ;;;###autoload
1640 (defun dired-do-symlink (&optional arg)
1641 "Make symbolic links to current file or all marked (or next ARG) files.
1642 When operating on just the current file, you specify the new name.
1643 When operating on multiple or marked files, you specify a directory
1644 and new symbolic links are made in that directory
1645 with the same names that the files currently have. The default
1646 suggested for the target directory depends on the value of
1647 `dired-dwim-target', which see.
1649 For relative symlinks, use \\[dired-do-relsymlink]."
1650 (interactive "P")
1651 (dired-do-create-files 'symlink (function make-symbolic-link)
1652 "Symlink" arg dired-keep-marker-symlink))
1654 ;;;###autoload
1655 (defun dired-do-hardlink (&optional arg)
1656 "Add names (hard links) current file or all marked (or next ARG) files.
1657 When operating on just the current file, you specify the new name.
1658 When operating on multiple or marked files, you specify a directory
1659 and new hard links are made in that directory
1660 with the same names that the files currently have. The default
1661 suggested for the target directory depends on the value of
1662 `dired-dwim-target', which see."
1663 (interactive "P")
1664 (dired-do-create-files 'hardlink (function dired-hardlink)
1665 "Hardlink" arg dired-keep-marker-hardlink))
1667 (defun dired-hardlink (file newname &optional ok-if-already-exists)
1668 (dired-handle-overwrite newname)
1669 ;; error is caught in -create-files
1670 (add-name-to-file file newname ok-if-already-exists)
1671 ;; Update the link count
1672 (dired-relist-file file))
1674 ;;;###autoload
1675 (defun dired-do-rename (&optional arg)
1676 "Rename current file or all marked (or next ARG) files.
1677 When renaming just the current file, you specify the new name.
1678 When renaming multiple or marked files, you specify a directory.
1679 This command also renames any buffers that are visiting the files.
1680 The default suggested for the target directory depends on the value
1681 of `dired-dwim-target', which see."
1682 (interactive "P")
1683 (dired-do-create-files 'move (function dired-rename-file)
1684 "Move" arg dired-keep-marker-rename "Rename"))
1685 ;;;###end dired-cp.el
1687 ;;; 5K
1688 ;;;###begin dired-re.el
1689 (defun dired-do-create-files-regexp
1690 (file-creator operation arg regexp newname &optional whole-name marker-char)
1691 ;; Create a new file for each marked file using regexps.
1692 ;; FILE-CREATOR and OPERATION as in dired-create-files.
1693 ;; ARG as in dired-get-marked-files.
1694 ;; Matches each marked file against REGEXP and constructs the new
1695 ;; filename from NEWNAME (like in function replace-match).
1696 ;; Optional arg WHOLE-NAME means match/replace the whole file name
1697 ;; instead of only the non-directory part of the file.
1698 ;; Optional arg MARKER-CHAR as in dired-create-files.
1699 (let* ((fn-list (dired-get-marked-files nil arg))
1700 (fn-count (length fn-list))
1701 (operation-prompt (concat operation " `%s' to `%s'?"))
1702 (rename-regexp-help-form (format "\
1703 Type SPC or `y' to %s one match, DEL or `n' to skip to next,
1704 `!' to %s all remaining matches with no more questions."
1705 (downcase operation)
1706 (downcase operation)))
1707 (regexp-name-constructor
1708 ;; Function to construct new filename using REGEXP and NEWNAME:
1709 (if whole-name ; easy (but rare) case
1710 (function
1711 (lambda (from)
1712 (let ((to (dired-string-replace-match regexp from newname))
1713 ;; must bind help-form directly around call to
1714 ;; dired-query
1715 (help-form rename-regexp-help-form))
1716 (if to
1717 (and (dired-query 'rename-regexp-query
1718 operation-prompt
1719 from
1722 (dired-log "%s: %s did not match regexp %s\n"
1723 operation from regexp)))))
1724 ;; not whole-name, replace non-directory part only
1725 (function
1726 (lambda (from)
1727 (let* ((new (dired-string-replace-match
1728 regexp (file-name-nondirectory from) newname))
1729 (to (and new ; nil means there was no match
1730 (expand-file-name new
1731 (file-name-directory from))))
1732 (help-form rename-regexp-help-form))
1733 (if to
1734 (and (dired-query 'rename-regexp-query
1735 operation-prompt
1736 (dired-make-relative from)
1737 (dired-make-relative to))
1739 (dired-log "%s: %s did not match regexp %s\n"
1740 operation (file-name-nondirectory from) regexp)))))))
1741 rename-regexp-query)
1742 (dired-create-files
1743 file-creator operation fn-list regexp-name-constructor marker-char)))
1745 (defun dired-mark-read-regexp (operation)
1746 ;; Prompt user about performing OPERATION.
1747 ;; Read and return list of: regexp newname arg whole-name.
1748 (let* ((whole-name
1749 (equal 0 (prefix-numeric-value current-prefix-arg)))
1750 (arg
1751 (if whole-name nil current-prefix-arg))
1752 (regexp
1753 (dired-read-regexp
1754 (concat (if whole-name "Abs. " "") operation " from (regexp): ")))
1755 (newname
1756 (read-string
1757 (concat (if whole-name "Abs. " "") operation " " regexp " to: "))))
1758 (list regexp newname arg whole-name)))
1760 ;;;###autoload
1761 (defun dired-do-rename-regexp (regexp newname &optional arg whole-name)
1762 "Rename selected files whose names match REGEXP to NEWNAME.
1764 With non-zero prefix argument ARG, the command operates on the next ARG
1765 files. Otherwise, it operates on all the marked files, or the current
1766 file if none are marked.
1768 As each match is found, the user must type a character saying
1769 what to do with it. For directions, type \\[help-command] at that time.
1770 NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
1771 REGEXP defaults to the last regexp used.
1773 With a zero prefix arg, renaming by regexp affects the absolute file name.
1774 Normally, only the non-directory part of the file name is used and changed."
1775 (interactive (dired-mark-read-regexp "Rename"))
1776 (dired-do-create-files-regexp
1777 (function dired-rename-file)
1778 "Rename" arg regexp newname whole-name dired-keep-marker-rename))
1780 ;;;###autoload
1781 (defun dired-do-copy-regexp (regexp newname &optional arg whole-name)
1782 "Copy selected files whose names match REGEXP to NEWNAME.
1783 See function `dired-do-rename-regexp' for more info."
1784 (interactive (dired-mark-read-regexp "Copy"))
1785 (let ((dired-recursive-copies nil)) ; No recursive copies.
1786 (dired-do-create-files-regexp
1787 (function dired-copy-file)
1788 (if dired-copy-preserve-time "Copy [-p]" "Copy")
1789 arg regexp newname whole-name dired-keep-marker-copy)))
1791 ;;;###autoload
1792 (defun dired-do-hardlink-regexp (regexp newname &optional arg whole-name)
1793 "Hardlink selected files whose names match REGEXP to NEWNAME.
1794 See function `dired-do-rename-regexp' for more info."
1795 (interactive (dired-mark-read-regexp "HardLink"))
1796 (dired-do-create-files-regexp
1797 (function add-name-to-file)
1798 "HardLink" arg regexp newname whole-name dired-keep-marker-hardlink))
1800 ;;;###autoload
1801 (defun dired-do-symlink-regexp (regexp newname &optional arg whole-name)
1802 "Symlink selected files whose names match REGEXP to NEWNAME.
1803 See function `dired-do-rename-regexp' for more info."
1804 (interactive (dired-mark-read-regexp "SymLink"))
1805 (dired-do-create-files-regexp
1806 (function make-symbolic-link)
1807 "SymLink" arg regexp newname whole-name dired-keep-marker-symlink))
1809 (defun dired-create-files-non-directory
1810 (file-creator basename-constructor operation arg)
1811 ;; Perform FILE-CREATOR on the non-directory part of marked files
1812 ;; using function BASENAME-CONSTRUCTOR, with query for each file.
1813 ;; OPERATION like in dired-create-files, ARG as in dired-get-marked-files.
1814 (let (rename-non-directory-query)
1815 (dired-create-files
1816 file-creator
1817 operation
1818 (dired-get-marked-files nil arg)
1819 (function
1820 (lambda (from)
1821 (let ((to (concat (file-name-directory from)
1822 (funcall basename-constructor
1823 (file-name-nondirectory from)))))
1824 (and (let ((help-form (format "\
1825 Type SPC or `y' to %s one file, DEL or `n' to skip to next,
1826 `!' to %s all remaining matches with no more questions."
1827 (downcase operation)
1828 (downcase operation))))
1829 (dired-query 'rename-non-directory-query
1830 (concat operation " `%s' to `%s'")
1831 (dired-make-relative from)
1832 (dired-make-relative to)))
1833 to))))
1834 dired-keep-marker-rename)))
1836 (defun dired-rename-non-directory (basename-constructor operation arg)
1837 (dired-create-files-non-directory
1838 (function dired-rename-file)
1839 basename-constructor operation arg))
1841 ;;;###autoload
1842 (defun dired-upcase (&optional arg)
1843 "Rename all marked (or next ARG) files to upper case."
1844 (interactive "P")
1845 (dired-rename-non-directory (function upcase) "Rename upcase" arg))
1847 ;;;###autoload
1848 (defun dired-downcase (&optional arg)
1849 "Rename all marked (or next ARG) files to lower case."
1850 (interactive "P")
1851 (dired-rename-non-directory (function downcase) "Rename downcase" arg))
1853 ;;;###end dired-re.el
1855 ;;; 13K
1856 ;;;###begin dired-ins.el
1858 ;;;###autoload
1859 (defun dired-maybe-insert-subdir (dirname &optional
1860 switches no-error-if-not-dir-p)
1861 "Insert this subdirectory into the same dired buffer.
1862 If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
1863 else inserts it at its natural place (as `ls -lR' would have done).
1864 With a prefix arg, you may edit the ls switches used for this listing.
1865 You can add `R' to the switches to expand the whole tree starting at
1866 this subdirectory.
1867 This function takes some pains to conform to `ls -lR' output.
1869 Dired remembers switches specified with a prefix arg, so that reverting
1870 the buffer will not reset them. However, using `dired-undo' to re-insert
1871 or delete subdirectories can bypass this machinery. Hence, you sometimes
1872 may have to reset some subdirectory switches after a `dired-undo'.
1873 You can reset all subdirectory switches to the default using
1874 \\<dired-mode-map>\\[dired-reset-subdir-switches].
1875 See Info node `(emacs)Subdir switches' for more details."
1876 (interactive
1877 (list (dired-get-filename)
1878 (if current-prefix-arg
1879 (read-string "Switches for listing: "
1880 (or dired-subdir-switches dired-actual-switches)))))
1881 (let ((opoint (point)))
1882 ;; We don't need a marker for opoint as the subdir is always
1883 ;; inserted *after* opoint.
1884 (setq dirname (file-name-as-directory dirname))
1885 (or (and (not switches)
1886 (dired-goto-subdir dirname))
1887 (dired-insert-subdir dirname switches no-error-if-not-dir-p))
1888 ;; Push mark so that it's easy to find back. Do this after the
1889 ;; insert message so that the user sees the `Mark set' message.
1890 (push-mark opoint)))
1892 ;;;###autoload
1893 (defun dired-insert-subdir (dirname &optional switches no-error-if-not-dir-p)
1894 "Insert this subdirectory into the same dired buffer.
1895 If it is already present, overwrites previous entry,
1896 else inserts it at its natural place (as `ls -lR' would have done).
1897 With a prefix arg, you may edit the `ls' switches used for this listing.
1898 You can add `R' to the switches to expand the whole tree starting at
1899 this subdirectory.
1900 This function takes some pains to conform to `ls -lR' output."
1901 ;; NO-ERROR-IF-NOT-DIR-P needed for special filesystems like
1902 ;; Prospero where dired-ls does the right thing, but
1903 ;; file-directory-p has not been redefined.
1904 (interactive
1905 (list (dired-get-filename)
1906 (if current-prefix-arg
1907 (read-string "Switches for listing: "
1908 (or dired-subdir-switches dired-actual-switches)))))
1909 (setq dirname (file-name-as-directory (expand-file-name dirname)))
1910 (or no-error-if-not-dir-p
1911 (file-directory-p dirname)
1912 (error "Attempt to insert a non-directory: %s" dirname))
1913 (let ((elt (assoc dirname dired-subdir-alist))
1914 (cons (assoc-string dirname dired-switches-alist))
1915 (modflag (buffer-modified-p))
1916 (old-switches switches)
1917 switches-have-R mark-alist case-fold-search buffer-read-only)
1918 (and (not switches) cons (setq switches (cdr cons)))
1919 (dired-insert-subdir-validate dirname switches)
1920 ;; case-fold-search is nil now, so we can test for capital `R':
1921 (if (setq switches-have-R (and switches (string-match "R" switches)))
1922 ;; avoid duplicated subdirs
1923 (setq mark-alist (dired-kill-tree dirname t)))
1924 (if elt
1925 ;; If subdir is already present, remove it and remember its marks
1926 (setq mark-alist (nconc (dired-insert-subdir-del elt) mark-alist))
1927 (dired-insert-subdir-newpos dirname)) ; else compute new position
1928 (dired-insert-subdir-doupdate
1929 dirname elt (dired-insert-subdir-doinsert dirname switches))
1930 (when old-switches
1931 (if cons
1932 (setcdr cons switches)
1933 (push (cons dirname switches) dired-switches-alist)))
1934 (when switches-have-R
1935 (dired-build-subdir-alist switches)
1936 (setq switches (dired-replace-in-string "R" "" switches))
1937 (dolist (cur-ass dired-subdir-alist)
1938 (let ((cur-dir (car cur-ass)))
1939 (and (dired-in-this-tree cur-dir dirname)
1940 (let ((cur-cons (assoc-string cur-dir dired-switches-alist)))
1941 (if cur-cons
1942 (setcdr cur-cons switches)
1943 (push (cons cur-dir switches) dired-switches-alist)))))))
1944 (dired-initial-position dirname)
1945 (save-excursion (dired-mark-remembered mark-alist))
1946 (restore-buffer-modified-p modflag)))
1948 ;; This is a separate function for dired-vms.
1949 (defun dired-insert-subdir-validate (dirname &optional switches)
1950 ;; Check that it is valid to insert DIRNAME with SWITCHES.
1951 ;; Signal an error if invalid (e.g. user typed `i' on `..').
1952 (or (dired-in-this-tree dirname (expand-file-name default-directory))
1953 (error "%s: not in this directory tree" dirname))
1954 (let ((real-switches (or switches dired-subdir-switches)))
1955 (when real-switches
1956 (let (case-fold-search)
1957 (mapcar
1958 (function
1959 (lambda (x)
1960 (or (eq (null (string-match x real-switches))
1961 (null (string-match x dired-actual-switches)))
1962 (error
1963 "Can't have dirs with and without -%s switches together" x))))
1964 ;; all switches that make a difference to dired-get-filename:
1965 '("F" "b"))))))
1967 (defun dired-alist-add (dir new-marker)
1968 ;; Add new DIR at NEW-MARKER. Sort alist.
1969 (dired-alist-add-1 dir new-marker)
1970 (dired-alist-sort))
1972 (defun dired-alist-sort ()
1973 ;; Keep the alist sorted on buffer position.
1974 (setq dired-subdir-alist
1975 (sort dired-subdir-alist
1976 (function (lambda (elt1 elt2)
1977 (> (dired-get-subdir-min elt1)
1978 (dired-get-subdir-min elt2)))))))
1980 (defun dired-kill-tree (dirname &optional remember-marks kill-root)
1981 "Kill all proper subdirs of DIRNAME, excluding DIRNAME itself.
1982 Interactively, you can kill DIRNAME as well by using a prefix argument.
1983 In interactive use, the command prompts for DIRNAME.
1985 When called from Lisp, if REMEMBER-MARKS is non-nil, return an alist
1986 of marked files. If KILL-ROOT is non-nil, kill DIRNAME as well."
1987 (interactive "DKill tree below directory: \ni\nP")
1988 (setq dirname (file-name-as-directory (expand-file-name dirname)))
1989 (let ((s-alist dired-subdir-alist) dir m-alist)
1990 (while s-alist
1991 (setq dir (car (car s-alist))
1992 s-alist (cdr s-alist))
1993 (and (or kill-root (not (string-equal dir dirname)))
1994 (dired-in-this-tree dir dirname)
1995 (dired-goto-subdir dir)
1996 (setq m-alist (nconc (dired-kill-subdir remember-marks) m-alist))))
1997 m-alist))
1999 (defun dired-insert-subdir-newpos (new-dir)
2000 ;; Find pos for new subdir, according to tree order.
2001 ;;(goto-char (point-max))
2002 (let ((alist dired-subdir-alist) elt dir pos new-pos)
2003 (while alist
2004 (setq elt (car alist)
2005 alist (cdr alist)
2006 dir (car elt)
2007 pos (dired-get-subdir-min elt))
2008 (if (dired-tree-lessp dir new-dir)
2009 ;; Insert NEW-DIR after DIR
2010 (setq new-pos (dired-get-subdir-max elt)
2011 alist nil)))
2012 (goto-char new-pos))
2013 ;; want a separating newline between subdirs
2014 (or (eobp)
2015 (forward-line -1))
2016 (insert "\n")
2017 (point))
2019 (defun dired-insert-subdir-del (element)
2020 ;; Erase an already present subdir (given by ELEMENT) from buffer.
2021 ;; Move to that buffer position. Return a mark-alist.
2022 (let ((begin-marker (dired-get-subdir-min element)))
2023 (goto-char begin-marker)
2024 ;; Are at beginning of subdir (and inside it!). Now determine its end:
2025 (goto-char (dired-subdir-max))
2026 (or (eobp);; want a separating newline _between_ subdirs:
2027 (forward-char -1))
2028 (prog1
2029 (dired-remember-marks begin-marker (point))
2030 (delete-region begin-marker (point)))))
2032 (defun dired-insert-subdir-doinsert (dirname switches)
2033 ;; Insert ls output after point.
2034 ;; Return the boundary of the inserted text (as list of BEG and END).
2035 (save-excursion
2036 (let ((begin (point)))
2037 (let ((dired-actual-switches
2038 (or switches
2039 dired-subdir-switches
2040 (dired-replace-in-string "R" "" dired-actual-switches))))
2041 (if (equal dirname (car (car (last dired-subdir-alist))))
2042 ;; If doing the top level directory of the buffer,
2043 ;; redo it as specified in dired-directory.
2044 (dired-readin-insert)
2045 (dired-insert-directory dirname dired-actual-switches nil nil t)))
2046 (list begin (point)))))
2048 (defun dired-insert-subdir-doupdate (dirname elt beg-end)
2049 ;; Point is at the correct subdir alist position for ELT,
2050 ;; BEG-END is the subdir-region (as list of begin and end).
2051 (if elt ; subdir was already present
2052 ;; update its position (should actually be unchanged)
2053 (set-marker (dired-get-subdir-min elt) (point-marker))
2054 (dired-alist-add dirname (point-marker)))
2055 ;; The hook may depend on the subdir-alist containing the just
2056 ;; inserted subdir, so run it after dired-alist-add:
2057 (if dired-after-readin-hook
2058 (save-excursion
2059 (let ((begin (nth 0 beg-end))
2060 (end (nth 1 beg-end)))
2061 (goto-char begin)
2062 (save-restriction
2063 (narrow-to-region begin end)
2064 ;; hook may add or delete lines, but the subdir boundary
2065 ;; marker floats
2066 (run-hooks 'dired-after-readin-hook))))))
2068 (defun dired-tree-lessp (dir1 dir2)
2069 ;; Lexicographic order on file name components, like `ls -lR':
2070 ;; DIR1 < DIR2 if DIR1 comes *before* DIR2 in an `ls -lR' listing,
2071 ;; i.e., if DIR1 is a (grand)parent dir of DIR2,
2072 ;; or DIR1 and DIR2 are in the same parentdir and their last
2073 ;; components are string-lessp.
2074 ;; Thus ("/usr/" "/usr/bin") and ("/usr/a/" "/usr/b/") are tree-lessp.
2075 ;; string-lessp could arguably be replaced by file-newer-than-file-p
2076 ;; if dired-actual-switches contained `t'.
2077 (setq dir1 (file-name-as-directory dir1)
2078 dir2 (file-name-as-directory dir2))
2079 (let ((components-1 (dired-split "/" dir1))
2080 (components-2 (dired-split "/" dir2)))
2081 (while (and components-1
2082 components-2
2083 (equal (car components-1) (car components-2)))
2084 (setq components-1 (cdr components-1)
2085 components-2 (cdr components-2)))
2086 (let ((c1 (car components-1))
2087 (c2 (car components-2)))
2089 (cond ((and c1 c2)
2090 (string-lessp c1 c2))
2091 ((and (null c1) (null c2))
2092 nil) ; they are equal, not lessp
2093 ((null c1) ; c2 is a subdir of c1: c1<c2
2095 ((null c2) ; c1 is a subdir of c2: c1>c2
2096 nil)
2097 (t (error "This can't happen"))))))
2099 ;; There should be a builtin split function - inverse to mapconcat.
2100 (defun dired-split (pat str &optional limit)
2101 "Splitting on regexp PAT, turn string STR into a list of substrings.
2102 Optional third arg LIMIT (>= 1) is a limit to the length of the
2103 resulting list.
2104 Thus, if SEP is a regexp that only matches itself,
2106 (mapconcat 'identity (dired-split SEP STRING) SEP)
2108 is always equal to STRING."
2109 (let* ((start (string-match pat str))
2110 (result (list (substring str 0 start)))
2111 (count 1)
2112 (end (if start (match-end 0))))
2113 (if end ; else nothing left
2114 (while (and (or (not (integerp limit))
2115 (< count limit))
2116 (string-match pat str end))
2117 (setq start (match-beginning 0)
2118 count (1+ count)
2119 result (cons (substring str end start) result)
2120 end (match-end 0)
2121 start end)
2123 (if (and (or (not (integerp limit))
2124 (< count limit))
2125 end) ; else nothing left
2126 (setq result
2127 (cons (substring str end) result)))
2128 (nreverse result)))
2130 ;;; moving by subdirectories
2132 ;;;###autoload
2133 (defun dired-prev-subdir (arg &optional no-error-if-not-found no-skip)
2134 "Go to previous subdirectory, regardless of level.
2135 When called interactively and not on a subdir line, go to this subdir's line."
2136 ;;(interactive "p")
2137 (interactive
2138 (list (if current-prefix-arg
2139 (prefix-numeric-value current-prefix-arg)
2140 ;; if on subdir start already, don't stay there!
2141 (if (dired-get-subdir) 1 0))))
2142 (dired-next-subdir (- arg) no-error-if-not-found no-skip))
2144 (defun dired-subdir-min ()
2145 (save-excursion
2146 (if (not (dired-prev-subdir 0 t t))
2147 (error "Not in a subdir!")
2148 (point))))
2150 ;;;###autoload
2151 (defun dired-goto-subdir (dir)
2152 "Go to end of header line of DIR in this dired buffer.
2153 Return value of point on success, otherwise return nil.
2154 The next char is either \\n, or \\r if DIR is hidden."
2155 (interactive
2156 (prog1 ; let push-mark display its message
2157 (list (expand-file-name
2158 (completing-read "Goto in situ directory: " ; prompt
2159 dired-subdir-alist ; table
2160 nil ; predicate
2161 t ; require-match
2162 (dired-current-directory))))
2163 (push-mark)))
2164 (setq dir (file-name-as-directory dir))
2165 (let ((elt (assoc dir dired-subdir-alist)))
2166 (and elt
2167 (goto-char (dired-get-subdir-min elt))
2168 ;; dired-subdir-hidden-p and dired-add-entry depend on point being
2169 ;; at either \r or \n after this function succeeds.
2170 (progn (skip-chars-forward "^\r\n")
2171 (point)))))
2173 ;;;###autoload
2174 (defun dired-mark-subdir-files ()
2175 "Mark all files except `.' and `..' in current subdirectory.
2176 If the Dired buffer shows multiple directories, this command
2177 marks the files listed in the subdirectory that point is in."
2178 (interactive)
2179 (let ((p-min (dired-subdir-min)))
2180 (dired-mark-files-in-region p-min (dired-subdir-max))))
2182 ;;;###autoload
2183 (defun dired-kill-subdir (&optional remember-marks)
2184 "Remove all lines of current subdirectory.
2185 Lower levels are unaffected."
2186 ;; With optional REMEMBER-MARKS, return a mark-alist.
2187 (interactive)
2188 (let* ((beg (dired-subdir-min))
2189 (end (dired-subdir-max))
2190 (modflag (buffer-modified-p))
2191 (cur-dir (dired-current-directory))
2192 (cons (assoc-string cur-dir dired-switches-alist))
2193 buffer-read-only)
2194 (if (equal cur-dir default-directory)
2195 (error "Attempt to kill top level directory"))
2196 (prog1
2197 (if remember-marks (dired-remember-marks beg end))
2198 (delete-region beg end)
2199 (if (eobp) ; don't leave final blank line
2200 (delete-char -1))
2201 (dired-unsubdir cur-dir)
2202 (when cons
2203 (setq dired-switches-alist (delete cons dired-switches-alist)))
2204 (restore-buffer-modified-p modflag))))
2206 (defun dired-unsubdir (dir)
2207 ;; Remove DIR from the alist
2208 (setq dired-subdir-alist
2209 (delq (assoc dir dired-subdir-alist) dired-subdir-alist)))
2211 ;;;###autoload
2212 (defun dired-tree-up (arg)
2213 "Go up ARG levels in the dired tree."
2214 (interactive "p")
2215 (let ((dir (dired-current-directory)))
2216 (while (>= arg 1)
2217 (setq arg (1- arg)
2218 dir (file-name-directory (directory-file-name dir))))
2219 ;;(setq dir (expand-file-name dir))
2220 (or (dired-goto-subdir dir)
2221 (error "Cannot go up to %s - not in this tree" dir))))
2223 ;;;###autoload
2224 (defun dired-tree-down ()
2225 "Go down in the dired tree."
2226 (interactive)
2227 (let ((dir (dired-current-directory)) ; has slash
2228 pos case-fold-search) ; filenames are case sensitive
2229 (let ((rest (reverse dired-subdir-alist)) elt)
2230 (while rest
2231 (setq elt (car rest)
2232 rest (cdr rest))
2233 (if (dired-in-this-tree (directory-file-name (car elt)) dir)
2234 (setq rest nil
2235 pos (dired-goto-subdir (car elt))))))
2236 (if pos
2237 (goto-char pos)
2238 (error "At the bottom"))))
2240 ;;; hiding
2242 (defun dired-unhide-subdir ()
2243 (let (buffer-read-only)
2244 (subst-char-in-region (dired-subdir-min) (dired-subdir-max) ?\r ?\n)))
2246 (defun dired-hide-check ()
2247 (or selective-display
2248 (error "selective-display must be t for subdir hiding to work!")))
2250 (defun dired-subdir-hidden-p (dir)
2251 (and selective-display
2252 (save-excursion
2253 (dired-goto-subdir dir)
2254 (looking-at "\r"))))
2256 ;;;###autoload
2257 (defun dired-hide-subdir (arg)
2258 "Hide or unhide the current subdirectory and move to next directory.
2259 Optional prefix arg is a repeat factor.
2260 Use \\[dired-hide-all] to (un)hide all directories."
2261 (interactive "p")
2262 (dired-hide-check)
2263 (let ((modflag (buffer-modified-p)))
2264 (while (>= (setq arg (1- arg)) 0)
2265 (let* ((cur-dir (dired-current-directory))
2266 (hidden-p (dired-subdir-hidden-p cur-dir))
2267 (elt (assoc cur-dir dired-subdir-alist))
2268 (end-pos (1- (dired-get-subdir-max elt)))
2269 buffer-read-only)
2270 ;; keep header line visible, hide rest
2271 (goto-char (dired-get-subdir-min elt))
2272 (skip-chars-forward "^\n\r")
2273 (if hidden-p
2274 (subst-char-in-region (point) end-pos ?\r ?\n)
2275 (subst-char-in-region (point) end-pos ?\n ?\r)))
2276 (dired-next-subdir 1 t))
2277 (restore-buffer-modified-p modflag)))
2279 ;;;###autoload
2280 (defun dired-hide-all (arg)
2281 "Hide all subdirectories, leaving only their header lines.
2282 If there is already something hidden, make everything visible again.
2283 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2284 (interactive "P")
2285 (dired-hide-check)
2286 (let ((modflag (buffer-modified-p))
2287 buffer-read-only)
2288 (if (save-excursion
2289 (goto-char (point-min))
2290 (search-forward "\r" nil t))
2291 ;; unhide - bombs on \r in filenames
2292 (subst-char-in-region (point-min) (point-max) ?\r ?\n)
2293 ;; hide
2294 (let ((pos (point-max)) ; pos of end of last directory
2295 (alist dired-subdir-alist))
2296 (while alist ; while there are dirs before pos
2297 (subst-char-in-region (dired-get-subdir-min (car alist)) ; pos of prev dir
2298 (save-excursion
2299 (goto-char pos) ; current dir
2300 ;; we're somewhere on current dir's line
2301 (forward-line -1)
2302 (point))
2303 ?\n ?\r)
2304 (setq pos (dired-get-subdir-min (car alist))) ; prev dir gets current dir
2305 (setq alist (cdr alist)))))
2306 (restore-buffer-modified-p modflag)))
2308 ;;;###end dired-ins.el
2311 ;; Functions for searching in tags style among marked files.
2313 ;;;###autoload
2314 (defun dired-do-search (regexp)
2315 "Search through all marked files for a match for REGEXP.
2316 Stops when a match is found.
2317 To continue searching for next match, use command \\[tags-loop-continue]."
2318 (interactive "sSearch marked files (regexp): ")
2319 (tags-search regexp '(dired-get-marked-files nil nil 'dired-nondirectory-p)))
2321 ;;;###autoload
2322 (defun dired-do-query-replace-regexp (from to &optional delimited)
2323 "Do `query-replace-regexp' of FROM with TO, on all marked files.
2324 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
2325 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
2326 with the command \\[tags-loop-continue]."
2327 (interactive
2328 (let ((common
2329 (query-replace-read-args
2330 "Query replace regexp in marked files" t t)))
2331 (list (nth 0 common) (nth 1 common) (nth 2 common))))
2332 (dolist (file (dired-get-marked-files nil nil 'dired-nondirectory-p))
2333 (let ((buffer (get-file-buffer file)))
2334 (if (and buffer (with-current-buffer buffer
2335 buffer-read-only))
2336 (error "File `%s' is visited read-only" file))))
2337 (tags-query-replace from to delimited
2338 '(dired-get-marked-files nil nil 'dired-nondirectory-p)))
2340 (defun dired-nondirectory-p (file)
2341 (not (file-directory-p file)))
2343 ;;;###autoload
2344 (defun dired-show-file-type (file &optional deref-symlinks)
2345 "Print the type of FILE, according to the `file' command.
2346 If FILE is a symbolic link and the optional argument DEREF-SYMLINKS is
2347 true then the type of the file linked to by FILE is printed instead."
2348 (interactive (list (dired-get-filename t) current-prefix-arg))
2349 (with-temp-buffer
2350 (if deref-symlinks
2351 (call-process "file" nil t t "-L" "--" file)
2352 (call-process "file" nil t t "--" file))
2353 (when (bolp)
2354 (backward-delete-char 1))
2355 (message "%s" (buffer-string))))
2357 (provide 'dired-aux)
2359 ;;; arch-tag: 4b508de9-a153-423d-8d3f-a1bbd86f4f60
2360 ;;; dired-aux.el ends here