1 ;;; dired-aux.el --- less commonly used parts of dired
3 ;; Copyright (C) 1985-1986, 1992, 1994, 1998, 2000-2015 Free Software
6 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>.
7 ;; Maintainer: emacs-devel@gnu.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; The parts of dired mode not normally used. This is a space-saving hack
29 ;; to avoid having to load a large mode when all that's wanted are a few
32 ;; Rewritten in 1990/1991 to add tree features, file marking and
33 ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
34 ;; Finished up by rms in 1992.
38 ;; We need macros in dired.el to compile properly,
39 ;; and we call subroutines in it too.
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.")
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]\\|$\\)")
55 (defun dired-diff (file &optional switches
)
56 "Compare file at point with file FILE using `diff'.
57 If called interactively, prompt for FILE. If the file at point
58 has a backup file, use that as the default. If the file at point
59 is a backup file, use its original. If the mark is active
60 in Transient Mark mode, use the file at the mark as the default.
61 \(That's the mark set by \\[set-mark-command], not by Dired's
62 \\[dired-mark] command.)
64 FILE is the first file given to `diff'. The file at point
65 is the second file given to `diff'.
67 With prefix arg, prompt for second argument SWITCHES, which is
68 the string of command switches for the third argument of `diff'."
70 (let* ((current (dired-get-filename t
))
71 ;; Get the latest existing backup file or its original.
72 (oldf (if (backup-file-name-p current
)
73 (file-name-sans-versions current
)
74 (diff-latest-backup-file current
)))
75 ;; Get the file at the mark.
76 (file-at-mark (if (and transient-mark-mode mark-active
)
77 (save-excursion (goto-char (mark t
))
78 (dired-get-filename t t
))))
79 (default-file (or file-at-mark
80 (and oldf
(file-name-nondirectory oldf
))))
81 ;; Use it as default if it's not the same as the current file,
82 ;; and the target dir is current or there is a default file.
83 (default (if (and (not (equal default-file current
))
84 (or (equal (dired-dwim-target-directory)
85 (dired-current-directory))
88 (target-dir (if default
89 (dired-current-directory)
90 (dired-dwim-target-directory)))
91 (defaults (dired-dwim-target-defaults (list current
) target-dir
)))
93 (minibuffer-with-setup-hook
95 (set (make-local-variable 'minibuffer-default-add-function
) nil
)
96 (setq minibuffer-default defaults
))
98 (format "Diff %s with%s: " current
99 (if default
(format " (default %s)" default
) ""))
100 target-dir default t
))
101 (if current-prefix-arg
102 (read-string "Options for diff: "
103 (if (stringp diff-switches
)
105 (mapconcat 'identity diff-switches
" ")))))))
106 (let ((current (dired-get-filename t
)))
107 (when (or (equal (expand-file-name file
)
108 (expand-file-name current
))
109 (and (file-directory-p file
)
110 (equal (expand-file-name current file
)
111 (expand-file-name current
))))
112 (error "Attempt to compare the file to itself"))
113 (if (and (backup-file-name-p current
)
114 (equal file
(file-name-sans-versions current
)))
115 (diff current file switches
)
116 (diff file current switches
))))
119 (defun dired-backup-diff (&optional switches
)
120 "Diff this file with its backup file or vice versa.
121 Uses the latest backup, if there are several numerical backups.
122 If this file is a backup, diff it with its original.
123 The backup file is the first file given to `diff'.
124 With prefix arg, prompt for argument SWITCHES which is options for `diff'."
126 (if current-prefix-arg
127 (list (read-string "Options for diff: "
128 (if (stringp diff-switches
)
130 (mapconcat 'identity diff-switches
" "))))
132 (diff-backup (dired-get-filename) switches
))
135 (defun dired-compare-directories (dir2 predicate
)
136 "Mark files with different file attributes in two dired buffers.
137 Compare file attributes of files in the current directory
138 with file attributes in directory DIR2 using PREDICATE on pairs of files
139 with the same name. Mark files for which PREDICATE returns non-nil.
140 Mark files with different names if PREDICATE is nil (or interactively
141 with empty input at the predicate prompt).
143 PREDICATE is a Lisp expression that can refer to the following variables:
145 size1, size2 - file size in bytes
146 mtime1, mtime2 - last modification time in seconds, as a float
147 fa1, fa2 - list of file attributes
148 returned by function `file-attributes'
150 where 1 refers to attribute of file in the current dired buffer
151 and 2 to attribute of file in second dired buffer.
153 Examples of PREDICATE:
155 (> mtime1 mtime2) - mark newer files
156 (not (= size1 size2)) - mark files with different sizes
157 (not (string= (nth 8 fa1) (nth 8 fa2))) - mark files with different modes
158 (not (and (= (nth 2 fa1) (nth 2 fa2)) - mark files with different UID
159 (= (nth 3 fa1) (nth 3 fa2)))) and GID."
162 (let* ((target-dir (dired-dwim-target-directory))
163 (defaults (dired-dwim-target-defaults nil target-dir
)))
164 (minibuffer-with-setup-hook
166 (set (make-local-variable 'minibuffer-default-add-function
) nil
)
167 (setq minibuffer-default defaults
))
168 (read-directory-name (format "Compare %s with: "
169 (dired-current-directory))
170 target-dir target-dir t
)))
171 (read-from-minibuffer "Mark if (lisp expr or RET): " nil nil t nil
"nil")))
172 (let* ((dir1 (dired-current-directory))
173 (file-alist1 (dired-files-attributes dir1
))
174 (file-alist2 (dired-files-attributes dir2
))
175 file-list1 file-list2
)
176 (setq file-alist1
(delq (assoc "." file-alist1
) file-alist1
))
177 (setq file-alist1
(delq (assoc ".." file-alist1
) file-alist1
))
178 (setq file-alist2
(delq (assoc "." file-alist2
) file-alist2
))
179 (setq file-alist2
(delq (assoc ".." file-alist2
) file-alist2
))
180 (setq file-list1
(mapcar
182 (dired-file-set-difference
183 file-alist1 file-alist2
187 (dired-file-set-difference
188 file-alist2 file-alist1
190 (dired-fun-in-all-buffers
194 (member (dired-get-filename nil t
) file-list1
) nil
)))
195 (dired-fun-in-all-buffers
199 (member (dired-get-filename nil t
) file-list2
) nil
)))
200 (message "Marked in dir1: %s files, in dir2: %s files"
202 (length file-list2
))))
204 (defun dired-file-set-difference (list1 list2 predicate
)
205 "Combine LIST1 and LIST2 using a set-difference operation.
206 The result list contains all file items that appear in LIST1 but not LIST2.
207 This is a non-destructive function; it makes a copy of the data if necessary
208 to avoid corrupting the original LIST1 and LIST2.
209 PREDICATE (see `dired-compare-directories') is an additional match
210 condition. Two file items are considered to match if they are equal
211 *and* PREDICATE evaluates to t."
212 (if (or (null list1
) (null list2
))
215 (dolist (file1 list1
)
216 (unless (let ((list list2
))
218 (not (let* ((file2 (car list
))
219 (fa1 (car (cddr file1
)))
220 (fa2 (car (cddr file2
)))
223 (mtime1 (float-time (nth 5 fa1
)))
224 (mtime2 (float-time (nth 5 fa2
))))
226 (equal (car file1
) (car file2
))
227 (not (eval predicate
))))))
228 (setq list
(cdr list
)))
230 (setq res
(cons file1 res
))))
233 (defun dired-files-attributes (dir)
234 "Return a list of all file names and attributes from DIR.
235 List has a form of (file-name full-file-name (attribute-list))."
238 (let ((full-file-name (expand-file-name file-name dir
)))
241 (file-attributes full-file-name
))))
242 (directory-files dir
)))
244 ;;; Change file attributes
246 (defun dired-do-chxxx (attribute-name program op-symbol arg
)
247 ;; Change file attributes (group, owner, timestamp) of marked files and
248 ;; refresh their file lines.
249 ;; ATTRIBUTE-NAME is a string describing the attribute to the user.
250 ;; PROGRAM is the program used to change the attribute.
251 ;; OP-SYMBOL is the type of operation (for use in `dired-mark-pop-up').
252 ;; ARG describes which files to use, as in `dired-get-marked-files'.
253 (let* ((files (dired-get-marked-files t arg
))
254 ;; The source of default file attributes is the file at point.
255 (default-file (dired-get-filename t t
))
256 (default (when default-file
257 (cond ((eq op-symbol
'touch
)
260 (nth 5 (file-attributes default-file
))))
261 ((eq op-symbol
'chown
)
262 (nth 2 (file-attributes default-file
'string
)))
263 ((eq op-symbol
'chgrp
)
264 (nth 3 (file-attributes default-file
'string
))))))
265 (prompt (concat "Change " attribute-name
" of %s to"
266 (if (eq op-symbol
'touch
)
269 (new-attribute (dired-mark-read-string prompt nil op-symbol
271 (cond ((eq op-symbol
'chown
)
273 ((eq op-symbol
'chgrp
)
275 (operation (concat program
" " new-attribute
))
278 (dired-bunch-files 10000
279 (function dired-check-process
)
281 (list operation program
)
282 (unless (or (string-equal new-attribute
"")
283 ;; Use `eq' instead of `equal'
284 ;; to detect empty input (bug#12399).
285 (eq new-attribute default
))
286 (if (eq op-symbol
'touch
)
287 (list "-t" new-attribute
)
288 (list new-attribute
)))
289 (if (string-match-p "gnu" system-configuration
)
292 (dired-do-redisplay arg
);; moves point if ARG is an integer
295 (format "%s: error" operation
)
299 (defun dired-do-chmod (&optional arg
)
300 "Change the mode of the marked (or next ARG) files.
301 Symbolic modes like `g+w' are allowed.
302 Type M-n to pull the file attributes of the file at point
303 into the minibuffer."
305 (let* ((files (dired-get-marked-files t arg
))
306 ;; The source of default file attributes is the file at point.
307 (default-file (dired-get-filename t t
))
308 (modestr (when default-file
309 (nth 8 (file-attributes default-file
))))
311 (and (stringp modestr
)
312 (string-match "^.\\(...\\)\\(...\\)\\(...\\)$" modestr
)
313 (replace-regexp-in-string
315 (format "u=%s,g=%s,o=%s"
316 (match-string 1 modestr
)
317 (match-string 2 modestr
)
318 (match-string 3 modestr
)))))
319 (modes (dired-mark-read-string
320 "Change mode of %s to: "
321 nil
'chmod arg files default
))
323 (cond ((or (equal modes
"")
324 ;; Use `eq' instead of `equal'
325 ;; to detect empty input (bug#12399).
327 ;; We used to treat empty input as DEFAULT, but that is not
328 ;; such a good idea (Bug#9361).
329 (error "No file mode specified"))
330 ((string-match-p "^[0-7]+" modes
)
331 (setq num-modes
(string-to-number modes
8))))
336 (if num-modes num-modes
337 (file-modes-symbolic-to-number modes
(file-modes file
)))))
338 (dired-do-redisplay arg
)))
341 (defun dired-do-chgrp (&optional arg
)
342 "Change the group of the marked (or next ARG) files.
343 Type M-n to pull the file attributes of the file at point
344 into the minibuffer."
346 (if (memq system-type
'(ms-dos windows-nt
))
347 (error "chgrp not supported on this system"))
348 (dired-do-chxxx "Group" "chgrp" 'chgrp arg
))
351 (defun dired-do-chown (&optional arg
)
352 "Change the owner of the marked (or next ARG) files.
353 Type M-n to pull the file attributes of the file at point
354 into the minibuffer."
356 (if (memq system-type
'(ms-dos windows-nt
))
357 (error "chown not supported on this system"))
358 (dired-do-chxxx "Owner" dired-chown-program
'chown arg
))
361 (defun dired-do-touch (&optional arg
)
362 "Change the timestamp of the marked (or next ARG) files.
364 Type M-n to pull the file attributes of the file at point
365 into the minibuffer."
367 (dired-do-chxxx "Timestamp" dired-touch-program
'touch arg
))
369 ;; Process all the files in FILES in batches of a convenient size,
370 ;; by means of (FUNCALL FUNCTION ARGS... SOME-FILES...).
371 ;; Batches are chosen to need less than MAX chars for the file names,
372 ;; allowing 3 extra characters of separator per file name.
373 (defun dired-bunch-files (max function args files
)
378 ;; Accumulate files as long as they fit in MAX chars,
379 ;; then process the ones accumulated so far.
381 (let* ((thisfile (car files
))
382 (thislength (+ (length thisfile
) 3))
384 ;; If we have at least 1 pending file
385 ;; and this file won't fit in the length limit, process now.
386 (if (and pending
(> (+ thislength pending-length
) max
))
387 (setq pending
(nreverse pending
)
388 ;; The elements of PENDING are now in forward order.
389 ;; Do the operation and record failures.
390 failures
(nconc (apply function
(append args pending
))
392 ;; Transfer the elements of PENDING onto PAST
393 ;; and clear it out. Now PAST contains the first N files
394 ;; specified (for some N), and FILES contains the rest.
395 past
(nconc past pending
)
398 ;; Do (setq pending (cons thisfile pending))
399 ;; but reuse the cons that was in `files'.
400 (setcdr files pending
)
402 (setq pending-length
(+ thislength pending-length
))
404 (setq pending
(nreverse pending
))
406 (nconc (apply function
(append args pending
))
408 ;; Now the original list FILES has been put back as it was.
409 (nconc past pending
))))
412 (defun dired-do-print (&optional arg
)
413 "Print the marked (or next ARG) files.
414 Uses the shell command coming from variables `lpr-command' and
415 `lpr-switches' as default."
417 (let* ((file-list (dired-get-marked-files t arg
))
419 (if (and (stringp printer-name
)
420 (string< "" printer-name
))
421 (cons (concat lpr-printer-switch printer-name
)
424 (command (dired-mark-read-string
428 (if (stringp lpr-switches
)
432 'print arg file-list
)))
433 (dired-run-shell-command (dired-shell-stuff-it command file-list nil
))))
435 (defun dired-mark-read-string (prompt initial op-symbol arg files
436 &optional default-value collection
)
437 "Read args for a Dired marked-files command, prompting with PROMPT.
438 Return the user input (a string).
440 INITIAL, if non-nil, is the initial minibuffer input.
441 OP-SYMBOL is an operation symbol (see `dired-no-confirm').
442 ARG is normally the prefix argument for the calling command;
443 it is passed as the first argument to `dired-mark-prompt'.
444 FILES should be a list of marked files' names.
446 Optional arg DEFAULT-VALUE is a default value or list of default
447 values, passed as the seventh arg to `completing-read'.
449 Optional arg COLLECTION is a collection of possible completions,
450 passed as the second arg to `completing-read'."
451 (dired-mark-pop-up nil op-symbol files
453 (format prompt
(dired-mark-prompt arg files
))
454 collection nil nil initial nil default-value nil
))
456 ;;; Cleaning a directory: flagging some backups for deletion.
458 (defvar dired-file-version-alist
)
461 (defun dired-clean-directory (keep)
462 "Flag numerical backups for deletion.
463 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
464 Positive prefix arg KEEP overrides `dired-kept-versions';
465 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
467 To clear the flags on these files, you can use \\[dired-flag-backup-files]
468 with a prefix argument."
470 (setq keep
(if keep
(prefix-numeric-value keep
) dired-kept-versions
))
471 (let ((early-retention (if (< keep
0) (- keep
) kept-old-versions
))
472 (late-retention (if (<= keep
0) dired-kept-versions keep
))
473 (dired-file-version-alist ()))
474 (message "Cleaning numerical backups (keeping %d late, %d old)..."
475 late-retention early-retention
)
476 ;; Look at each file.
477 ;; If the file has numeric backup versions,
478 ;; put on dired-file-version-alist an element of the form
479 ;; (FILENAME . VERSION-NUMBER-LIST)
480 (dired-map-dired-file-lines (function dired-collect-file-versions
))
481 ;; Sort each VERSION-NUMBER-LIST,
482 ;; and remove the versions not to be deleted.
483 (let ((fval dired-file-version-alist
))
485 (let* ((sorted-v-list (cons 'q
(sort (cdr (car fval
)) '<)))
486 (v-count (length sorted-v-list
)))
487 (if (> v-count
(+ early-retention late-retention
))
488 (rplacd (nthcdr early-retention sorted-v-list
)
489 (nthcdr (- v-count late-retention
)
492 (cdr sorted-v-list
)))
493 (setq fval
(cdr fval
))))
494 ;; Look at each file. If it is a numeric backup file,
495 ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
496 (dired-map-dired-file-lines (function dired-trample-file-versions
))
497 (message "Cleaning numerical backups...done")))
499 ;;; Subroutines of dired-clean-directory.
501 (defun dired-map-dired-file-lines (fun)
502 ;; Perform FUN with point at the end of each non-directory line.
503 ;; FUN takes one argument, the absolute filename.
505 (let (file buffer-read-only
)
506 (goto-char (point-min))
509 (and (not (looking-at-p dired-re-dir
))
511 (setq file
(dired-get-filename nil t
)) ; nil on non-file
513 (funcall fun file
))))
516 (defvar backup-extract-version-start
) ; used in backup-extract-version
518 (defun dired-collect-file-versions (fn)
519 (let ((fn (file-name-sans-versions fn
)))
520 ;; Only do work if this file is not already in the alist.
521 (if (assoc fn dired-file-version-alist
)
523 ;; If it looks like file FN has versions, return a list of the versions.
524 ;;That is a list of strings which are file names.
525 ;;The caller may want to flag some of these files for deletion.
526 (let* ((base-versions
527 (concat (file-name-nondirectory fn
) ".~"))
528 (backup-extract-version-start (length base-versions
))
529 (possibilities (file-name-all-completions
531 (file-name-directory fn
)))
532 (versions (mapcar 'backup-extract-version possibilities
)))
534 (setq dired-file-version-alist
535 (cons (cons fn versions
)
536 dired-file-version-alist
)))))))
538 (defun dired-trample-file-versions (fn)
539 (let* ((start-vn (string-match-p "\\.~[0-9]+~$" fn
))
542 (setq base-version-list
; there was a base version to which
543 (assoc (substring fn
0 start-vn
) ; this looks like a
544 dired-file-version-alist
)) ; subversion
545 (not (memq (string-to-number (substring fn
(+ 2 start-vn
)))
546 base-version-list
)) ; this one doesn't make the cut
547 (progn (beginning-of-line)
549 (insert dired-del-marker
)))))
553 (declare-function mailcap-file-default-commands
"mailcap" (files))
555 (defun minibuffer-default-add-dired-shell-commands ()
556 "Return a list of all commands associated with current dired files.
557 This function is used to add all related commands retrieved by `mailcap'
558 to the end of the list of defaults just after the default value."
560 (let ((commands (and (boundp 'files
) (require 'mailcap nil t
)
561 (mailcap-file-default-commands files
))))
562 (if (listp minibuffer-default
)
563 (append minibuffer-default commands
)
564 (cons minibuffer-default commands
))))
566 ;; This is an extra function so that you can redefine it, e.g., to use gmhist.
567 (defun dired-read-shell-command (prompt arg files
)
568 "Read a dired shell command.
569 PROMPT should be a format string with one \"%s\" format sequence,
570 which is replaced by the value returned by `dired-mark-prompt',
571 with ARG and FILES as its arguments. FILES should be a list of
572 file names. The result is used as the prompt.
574 This normally reads using `read-shell-command', but if the
575 `dired-x' package is loaded, use `dired-guess-shell-command' to
576 offer a smarter default choice of shell command."
577 (minibuffer-with-setup-hook
579 (set (make-local-variable 'minibuffer-default-add-function
)
580 'minibuffer-default-add-dired-shell-commands
))
581 (setq prompt
(format prompt
(dired-mark-prompt arg files
)))
582 (if (functionp 'dired-guess-shell-command
)
583 (dired-mark-pop-up nil
'shell files
584 'dired-guess-shell-command prompt files
)
585 (dired-mark-pop-up nil
'shell files
586 'read-shell-command prompt nil nil
))))
589 (defun dired-do-async-shell-command (command &optional arg file-list
)
590 "Run a shell command COMMAND on the marked files asynchronously.
592 Like `dired-do-shell-command', but adds `&' at the end of COMMAND
593 to execute it asynchronously.
595 When operating on multiple files, asynchronous commands
596 are executed in the background on each file in parallel.
597 In shell syntax this means separating the individual commands
598 with `&'. However, when COMMAND ends in `;' or `;&' then commands
599 are executed in the background on each file sequentially waiting
600 for each command to terminate before running the next command.
601 In shell syntax this means separating the individual commands with `;'.
603 The output appears in the buffer `*Async Shell Command*'."
605 (let ((files (dired-get-marked-files t current-prefix-arg
)))
607 ;; Want to give feedback whether this file or marked files are used:
608 (dired-read-shell-command "& on %s: " current-prefix-arg files
)
611 (unless (string-match-p "&[ \t]*\\'" command
)
612 (setq command
(concat command
" &")))
613 (dired-do-shell-command command arg file-list
))
616 (defun dired-do-shell-command (command &optional arg file-list
)
617 "Run a shell command COMMAND on the marked files.
618 If no files are marked or a numeric prefix arg is given,
619 the next ARG files are used. Just \\[universal-argument] means the current file.
620 The prompt mentions the file(s) or the marker, as appropriate.
622 If there is a `*' in COMMAND, surrounded by whitespace, this runs
623 COMMAND just once with the entire file list substituted there.
625 If there is no `*', but there is a `?' in COMMAND, surrounded by
626 whitespace, this runs COMMAND on each file individually with the
627 file name substituted for `?'.
629 Otherwise, this runs COMMAND on each file individually with the
630 file name added at the end of COMMAND (separated by a space).
632 `*' and `?' when not surrounded by whitespace have no special
633 significance for `dired-do-shell-command', and are passed through
634 normally to the shell, but you must confirm first.
636 If you want to use `*' as a shell wildcard with whitespace around
637 it, write `*\"\"' in place of just `*'. This is equivalent to just
638 `*' in the shell, but avoids Dired's special handling.
640 If COMMAND ends in `&', `;', or `;&', it is executed in the
641 background asynchronously, and the output appears in the buffer
642 `*Async Shell Command*'. When operating on multiple files and COMMAND
643 ends in `&', the shell command is executed on each file in parallel.
644 However, when COMMAND ends in `;' or `;&' then commands are executed
645 in the background on each file sequentially waiting for each command
646 to terminate before running the next command. You can also use
647 `dired-do-async-shell-command' that automatically adds `&'.
649 Otherwise, COMMAND is executed synchronously, and the output
650 appears in the buffer `*Shell Command Output*'.
652 This feature does not try to redisplay Dired buffers afterward, as
653 there's no telling what files COMMAND may have changed.
654 Type \\[dired-do-redisplay] to redisplay the marked files.
656 When COMMAND runs, its working directory is the top-level directory
657 of the Dired buffer, so output files usually are created there
658 instead of in a subdir.
660 In a noninteractive call (from Lisp code), you must specify
661 the list of file names explicitly with the FILE-LIST argument, which
662 can be produced by `dired-get-marked-files', for example."
663 ;;Functions dired-run-shell-command and dired-shell-stuff-it do the
664 ;;actual work and can be redefined for customization.
666 (let ((files (dired-get-marked-files t current-prefix-arg
)))
668 ;; Want to give feedback whether this file or marked files are used:
669 (dired-read-shell-command "! on %s: " current-prefix-arg files
)
672 (let* ((on-each (not (string-match-p dired-star-subst-regexp command
)))
673 (no-subst (not (string-match-p dired-quark-subst-regexp command
)))
674 (star (string-match-p "\\*" command
))
675 (qmark (string-match-p "\\?" command
)))
676 ;; Get confirmation for wildcards that may have been meant
677 ;; to control substitution of a file name or the file name list.
678 (if (cond ((not (or on-each no-subst
))
679 (error "You can not combine `*' and `?' substitution marks"))
681 (y-or-n-p "Confirm--do you mean to use `*' as a wildcard? "))
682 ((and qmark no-subst
)
683 (y-or-n-p "Confirm--do you mean to use `?' as a wildcard? "))
687 (- 10000 (length command
))
688 (function (lambda (&rest files
)
689 (dired-run-shell-command
690 (dired-shell-stuff-it command files t arg
))))
693 ;; execute the shell command
694 (dired-run-shell-command
695 (dired-shell-stuff-it command file-list nil arg
))))))
697 ;; Might use {,} for bash or csh:
698 (defvar dired-mark-prefix
""
699 "Prepended to marked files in dired shell commands.")
700 (defvar dired-mark-postfix
""
701 "Appended to marked files in dired shell commands.")
702 (defvar dired-mark-separator
" "
703 "Separates marked files in dired shell commands.")
705 (defun dired-shell-stuff-it (command file-list on-each
&optional _raw-arg
)
706 ;; "Make up a shell command line from COMMAND and FILE-LIST.
707 ;; If ON-EACH is t, COMMAND should be applied to each file, else
708 ;; simply concat all files and apply COMMAND to this.
709 ;; FILE-LIST's elements will be quoted for the shell."
710 ;; Might be redefined for smarter things and could then use RAW-ARG
711 ;; (coming from interactive P and currently ignored) to decide what to do.
712 ;; Smart would be a way to access basename or extension of file names.
713 (let* ((in-background (string-match "[ \t]*&[ \t]*\\'" command
))
714 (command (if in-background
715 (substring command
0 (match-beginning 0))
717 (sequentially (string-match "[ \t]*;[ \t]*\\'" command
))
718 (command (if sequentially
719 (substring command
0 (match-beginning 0))
722 (if (or (string-match-p dired-star-subst-regexp command
)
723 (string-match-p dired-quark-subst-regexp command
))
725 (let ((retval command
))
727 "\\(^\\|[ \t]\\)\\([*?]\\)\\([ \t]\\|$\\)" retval
)
728 (setq retval
(replace-match x t t retval
2)))
730 (lambda (x) (concat command dired-mark-separator x
)))))
733 (mapconcat stuff-it
(mapcar 'shell-quote-argument file-list
)
734 (if (and in-background
(not sequentially
)) "&" ";"))
735 (let ((files (mapconcat 'shell-quote-argument
736 file-list dired-mark-separator
)))
737 (if (> (length file-list
) 1)
738 (setq files
(concat dired-mark-prefix files dired-mark-postfix
)))
739 (funcall stuff-it files
)))
740 (if in-background
"&" ""))))
742 ;; This is an extra function so that it can be redefined by ange-ftp.
744 (defun dired-run-shell-command (command)
746 (find-file-name-handler (directory-file-name default-directory
)
748 (if handler
(apply handler
'shell-command
(list command
))
749 (shell-command command
)))
750 ;; Return nil for sake of nconc in dired-bunch-files.
754 (defun dired-check-process (msg program
&rest arguments
)
755 ; "Display MSG while running PROGRAM, and check for output.
756 ;Remaining arguments are strings passed as command arguments to PROGRAM.
757 ; On error, insert output
758 ; in a log buffer and return the offending ARGUMENTS or PROGRAM.
759 ; Caller can cons up a list of failed args.
760 ;Else returns nil for success."
761 (let (err-buffer err
(dir default-directory
))
762 (message "%s..." msg
)
764 ;; Get a clean buffer for error output:
765 (setq err-buffer
(get-buffer-create " *dired-check-process output*"))
766 (set-buffer err-buffer
)
768 (setq default-directory dir
; caller's default-directory
769 err
(not (eq 0 (apply 'process-file program nil t nil arguments
))))
772 (dired-log (concat program
" " (prin1-to-string arguments
) "\n"))
773 (dired-log err-buffer
)
774 (or arguments program t
))
775 (kill-buffer err-buffer
)
776 (message "%s...done" msg
)
779 ;; Commands that delete or redisplay part of the dired buffer.
781 (defun dired-kill-line (&optional arg
)
782 "Kill the current line (not the files).
783 With a prefix argument, kill that many lines starting with the current line.
784 \(A negative argument kills backward.)"
786 (setq arg
(prefix-numeric-value arg
))
787 (let (buffer-read-only file
)
789 (setq file
(dired-get-filename nil t
))
791 (error "Can only kill file lines")
792 (save-excursion (and file
793 (dired-goto-subdir file
)
794 (dired-kill-subdir)))
795 (delete-region (line-beginning-position)
796 (progn (forward-line 1) (point)))
801 (dired-move-to-filename)))
804 (defun dired-do-kill-lines (&optional arg fmt
)
805 "Kill all marked lines (not the files).
806 With a prefix argument, kill that many lines starting with the current line.
807 \(A negative argument kills backward.)
808 If you use this command with a prefix argument to kill the line
809 for a file that is a directory, which you have inserted in the
810 Dired buffer as a subdirectory, then it deletes that subdirectory
811 from the buffer as well.
812 To kill an entire subdirectory \(without killing its line in the
813 parent directory), go to its directory header line and use this
814 command with a prefix argument (the value does not matter)."
815 ;; Returns count of killed lines. FMT="" suppresses message.
818 (if (dired-get-subdir)
820 (dired-kill-line arg
))
822 (goto-char (point-min))
823 (let (buffer-read-only
825 (regexp (dired-marker-regexp)))
826 (while (and (not (eobp))
827 (re-search-forward regexp nil t
))
828 (setq count
(1+ count
))
829 (delete-region (line-beginning-position)
830 (progn (forward-line 1) (point))))
832 (message (or fmt
"Killed %d line%s.") count
(dired-plural-s count
)))
835 ;;;###end dired-cmd.el
838 ;;;###begin dired-cp.el
840 (defun dired-compress ()
841 ;; Compress or uncompress the current file.
842 ;; Return nil for success, offending filename else.
843 (let* (buffer-read-only
844 (from-file (dired-get-filename))
845 (new-file (dired-compress-file from-file
)))
847 (let ((start (point)))
848 ;; Remove any preexisting entry for the name NEW-FILE.
849 (ignore-errors (dired-remove-entry new-file
))
851 ;; Now replace the current line with an entry for NEW-FILE.
852 (dired-update-file-line new-file
) nil
)
853 (dired-log (concat "Failed to compress" from-file
))
856 (defvar dired-compress-file-suffixes
857 '(("\\.gz\\'" "" "gunzip")
858 ("\\.tgz\\'" ".tar" "gunzip")
859 ("\\.Z\\'" "" "uncompress")
860 ;; For .z, try gunzip. It might be an old gzip file,
861 ;; or it might be from compact? pack? (which?) but gunzip handles both.
862 ("\\.z\\'" "" "gunzip")
863 ("\\.dz\\'" "" "dictunzip")
864 ("\\.tbz\\'" ".tar" "bunzip2")
865 ("\\.bz2\\'" "" "bunzip2")
866 ("\\.xz\\'" "" "unxz")
867 ;; This item controls naming for compression.
868 ("\\.tar\\'" ".tgz" nil
))
869 "Control changes in file name suffixes for compression and uncompression.
870 Each element specifies one transformation rule, and has the form:
871 (REGEXP NEW-SUFFIX PROGRAM)
872 The rule applies when the old file name matches REGEXP.
873 The new file name is computed by deleting the part that matches REGEXP
874 (as well as anything after that), then adding NEW-SUFFIX in its place.
875 If PROGRAM is non-nil, the rule is an uncompression rule,
876 and uncompression is done by running PROGRAM.
877 Otherwise, the rule is a compression rule, and compression is done with gzip.")
880 (defun dired-compress-file (file)
881 ;; Compress or uncompress FILE.
882 ;; Return the name of the compressed or uncompressed file.
883 ;; Return nil if no change in files.
884 (let ((handler (find-file-name-handler file
'dired-compress-file
))
886 (suffixes dired-compress-file-suffixes
))
887 ;; See if any suffix rule matches this file name.
889 (let (case-fold-search)
890 (if (string-match (car (car suffixes
)) file
)
891 (setq suffix
(car suffixes
) suffixes nil
))
892 (setq suffixes
(cdr suffixes
))))
893 ;; If so, compute desired new name.
895 (setq newname
(concat (substring file
0 (match-beginning 0))
898 (funcall handler
'dired-compress-file file
))
899 ((file-symlink-p file
)
901 ((and suffix
(nth 2 suffix
))
902 ;; We found an uncompression rule.
903 (if (not (dired-check-process (concat "Uncompressing " file
)
904 (nth 2 suffix
) file
))
907 ;;; We don't recognize the file as compressed, so compress it.
908 ;;; Try gzip; if we don't have that, use compress.
910 (let ((out-name (concat file
".gz")))
911 (and (or (not (file-exists-p out-name
))
913 (format "File %s already exists. Really compress? "
915 (not (dired-check-process (concat "Compressing " file
)
917 (or (file-exists-p out-name
)
918 (setq out-name
(concat file
".z")))
919 ;; Rename the compressed file to NEWNAME
920 ;; if it hasn't got that name already.
921 (if (and newname
(not (equal newname out-name
)))
923 (rename-file out-name newname t
)
927 (if (not (dired-check-process (concat "Compressing " file
)
928 "compress" "-f" file
))
929 ;; Don't use NEWNAME with `compress'.
930 (concat file
".Z"))))))))
932 (defun dired-mark-confirm (op-symbol arg
)
933 ;; Request confirmation from the user that the operation described
934 ;; by OP-SYMBOL is to be performed on the marked files.
935 ;; Confirmation consists in a y-or-n question with a file list
936 ;; pop-up unless OP-SYMBOL is a member of `dired-no-confirm'.
937 ;; The files used are determined by ARG (as in dired-get-marked-files).
938 (or (eq dired-no-confirm t
)
939 (memq op-symbol dired-no-confirm
)
940 ;; Pass t for DISTINGUISH-ONE-MARKED so that a single file which
941 ;; is marked pops up a window. That will help the user see
942 ;; it isn't the current line file.
943 (let ((files (dired-get-marked-files t arg nil t
))
944 (string (if (eq op-symbol
'compress
) "Compress or uncompress"
945 (capitalize (symbol-name op-symbol
)))))
946 (dired-mark-pop-up nil op-symbol files
(function y-or-n-p
)
948 (dired-mark-prompt arg files
) "? ")))))
950 (defun dired-map-over-marks-check (fun arg op-symbol
&optional show-progress
)
951 ; "Map FUN over marked files (with second ARG like in dired-map-over-marks)
952 ; and display failures.
954 ; FUN takes zero args. It returns non-nil (the offending object, e.g.
955 ; the short form of the filename) for a failure and probably logs a
956 ; detailed error explanation using function `dired-log'.
958 ; OP-SYMBOL is a symbol describing the operation performed (e.g.
959 ; `compress'). It is used with `dired-mark-pop-up' to prompt the user
960 ; (e.g. with `Compress * [2 files]? ') and to display errors (e.g.
961 ; `Failed to compress 1 of 2 files - type W to see why ("foo")')
963 ; SHOW-PROGRESS if non-nil means redisplay dired after each file."
964 (if (dired-mark-confirm op-symbol arg
)
965 (let* ((total-list;; all of FUN's return values
966 (dired-map-over-marks (funcall fun
) arg show-progress
))
967 (total (length total-list
))
968 (failures (delq nil total-list
))
969 (count (length failures
))
970 (string (if (eq op-symbol
'compress
) "Compress or uncompress"
971 (capitalize (symbol-name op-symbol
)))))
973 (message "%s: %d file%s."
974 string total
(dired-plural-s total
))
975 ;; end this bunch of errors:
977 (format "Failed to %s %d of %d file%s"
978 (downcase string
) count total
(dired-plural-s total
))
982 (defun dired-query (sym prompt
&rest args
)
983 "Format PROMPT with ARGS, query user, and store the result in SYM.
984 The return value is either nil or t.
986 The user may type y or SPC to accept once; n or DEL to skip once;
987 ! to accept this and subsequent queries; or q or ESC to decline
988 this and subsequent queries.
990 If SYM is already bound to a non-nil value, this function may
991 return automatically without querying the user. If SYM is !,
992 return t; if SYM is q or ESC, return nil."
993 (let* ((char (symbol-value sym
))
994 (char-choices '(?y ?\s ?n ?
\177 ?
! ?q ?\e
)))
996 t
) ; accept, and don't ask again
997 ((memq char
'(?q ?\e
))
998 nil
) ; skip, and don't ask again
999 (t ; no previous answer - ask now
1001 (concat (apply 'format prompt args
)
1003 (format " [Type yn!q or %s] "
1004 (key-description (vector help-char
)))
1005 " [Type y, n, q or !] ")))
1006 (set sym
(setq char
(read-char-choice prompt char-choices
)))
1007 (if (memq char
'(?y ?\s ?
!)) t
)))))
1011 (defun dired-do-compress (&optional arg
)
1012 "Compress or uncompress marked (or next ARG) files."
1014 (dired-map-over-marks-check (function dired-compress
) arg
'compress t
))
1016 ;; Commands for Emacs Lisp files - load and byte compile
1018 (defun dired-byte-compile ()
1019 ;; Return nil for success, offending file name else.
1020 (let* ((filename (dired-get-filename))
1021 elc-file buffer-read-only failure
)
1023 (save-excursion (byte-compile-file filename
))
1025 (setq failure err
)))
1026 (setq elc-file
(byte-compile-dest-file filename
))
1027 (or (file-exists-p elc-file
)
1031 (dired-log "Byte compile error for %s:\n%s\n" filename failure
)
1032 (dired-make-relative filename
))
1033 (dired-remove-file elc-file
)
1034 (forward-line) ; insert .elc after its .el file
1035 (dired-add-file elc-file
)
1039 (defun dired-do-byte-compile (&optional arg
)
1040 "Byte compile marked (or next ARG) Emacs Lisp files."
1042 (dired-map-over-marks-check (function dired-byte-compile
) arg
'byte-compile t
))
1044 (defun dired-load ()
1045 ;; Return nil for success, offending file name else.
1046 (let ((file (dired-get-filename)) failure
)
1048 (load file nil nil t
)
1049 (error (setq failure err
)))
1052 (dired-log "Load error for %s:\n%s\n" file failure
)
1053 (dired-make-relative file
))))
1056 (defun dired-do-load (&optional arg
)
1057 "Load the marked (or next ARG) Emacs Lisp files."
1059 (dired-map-over-marks-check (function dired-load
) arg
'load t
))
1062 (defun dired-do-redisplay (&optional arg test-for-subdir
)
1063 "Redisplay all marked (or next ARG) files.
1064 If on a subdir line, redisplay that subdirectory. In that case,
1065 a prefix arg lets you edit the `ls' switches used for the new listing.
1067 Dired remembers switches specified with a prefix arg, so that reverting
1068 the buffer will not reset them. However, using `dired-undo' to re-insert
1069 or delete subdirectories can bypass this machinery. Hence, you sometimes
1070 may have to reset some subdirectory switches after a `dired-undo'.
1071 You can reset all subdirectory switches to the default using
1072 \\<dired-mode-map>\\[dired-reset-subdir-switches].
1073 See Info node `(emacs)Subdir switches' for more details."
1074 ;; Moves point if the next ARG files are redisplayed.
1075 (interactive "P\np")
1076 (if (and test-for-subdir
(dired-get-subdir))
1077 (let* ((dir (dired-get-subdir))
1078 (switches (cdr (assoc-string dir dired-switches-alist
))))
1079 (dired-insert-subdir
1082 (read-string "Switches for listing: "
1084 dired-subdir-switches
1085 dired-actual-switches
)))))
1086 (message "Redisplaying...")
1087 ;; message much faster than making dired-map-over-marks show progress
1089 (if (consp dired-directory
) (car dired-directory
) dired-directory
))
1090 (dired-map-over-marks (let ((fname (dired-get-filename))
1091 ;; Postpone readin hook till we map
1092 ;; over all marked files (Bug#6810).
1093 (dired-after-readin-hook nil
))
1094 (message "Redisplaying... %s" fname
)
1095 (dired-update-file-line fname
))
1097 (run-hooks 'dired-after-readin-hook
)
1098 (dired-move-to-filename)
1099 (message "Redisplaying...done")))
1101 (defun dired-reset-subdir-switches ()
1102 "Set `dired-switches-alist' to nil and revert dired buffer."
1104 (setq dired-switches-alist nil
)
1107 (defun dired-update-file-line (file)
1108 ;; Delete the current line, and insert an entry for FILE.
1109 ;; If FILE is nil, then just delete the current line.
1110 ;; Keeps any marks that may be present in column one (doing this
1111 ;; here is faster than with dired-add-entry's optional arg).
1112 ;; Does not update other dired buffers. Use dired-relist-entry for that.
1113 (let* ((opoint (line-beginning-position))
1114 (char (char-after opoint
))
1116 (delete-region opoint
(progn (forward-line 1) (point)))
1119 (dired-add-entry file nil t
)
1120 ;; Replace space by old marker without moving point.
1121 ;; Faster than goto+insdel inside a save-excursion?
1122 (subst-char-in-region opoint
(1+ opoint
) ?
\040 char
))))
1123 (dired-move-to-filename))
1126 (defun dired-add-file (filename &optional marker-char
)
1127 (dired-fun-in-all-buffers
1128 (file-name-directory filename
) (file-name-nondirectory filename
)
1129 (function dired-add-entry
) filename marker-char
))
1131 (defvar dired-omit-mode
)
1132 (declare-function dired-omit-regexp
"dired-x" ())
1133 (defvar dired-omit-localp
)
1135 (defun dired-add-entry (filename &optional marker-char relative
)
1136 "Add a new dired entry for FILENAME.
1137 Optionally mark it with MARKER-CHAR (a character, else uses
1138 `dired-marker-char'). Note that this adds the entry `out of order'
1139 if files are sorted by time, etc.
1140 Skips files that match `dired-trivial-filenames'.
1141 Exposes hidden subdirectories if a file is added there.
1143 If `dired-x' is loaded and `dired-omit-mode' is enabled, skips
1144 files matching `dired-omit-regexp'."
1145 (if (or (not (featurep 'dired-x
))
1146 (not dired-omit-mode
)
1147 ;; Avoid calling ls for files that are going to be omitted anyway.
1148 (let ((omit-re (dired-omit-regexp)))
1149 (or (string= omit-re
"")
1150 (not (string-match-p omit-re
1152 ((eq 'no-dir dired-omit-localp
)
1154 ((eq t dired-omit-localp
)
1155 (dired-make-relative filename
))
1157 (dired-make-absolute
1159 (file-name-directory filename
)))))))))
1162 (setq filename
(directory-file-name filename
))
1163 ;; Entry is always for files, even if they happen to also be directories
1164 (let* ((opoint (point))
1165 (cur-dir (dired-current-directory))
1166 (directory (if relative cur-dir
(file-name-directory filename
)))
1170 (file-relative-name filename directory
)
1171 (file-name-nondirectory filename
))
1174 (if (string= directory cur-dir
)
1176 (skip-chars-forward "^\r\n")
1177 (if (eq (following-char) ?
\r)
1178 (dired-unhide-subdir))
1179 ;; We are already where we should be, except when
1180 ;; point is before the subdir line or its total line.
1181 (let ((p (dired-after-subdir-garbage cur-dir
)))
1184 ;; else try to find correct place to insert
1185 (if (dired-goto-subdir directory
)
1186 (progn ;; unhide if necessary
1187 (if (looking-at-p "\r")
1188 ;; Point is at end of subdir line.
1189 (dired-unhide-subdir))
1190 ;; found - skip subdir and `total' line
1191 ;; and uninteresting files like . and ..
1192 ;; This better not move into the next subdir!
1193 (dired-goto-next-nontrivial-file))
1195 (throw 'not-found
"Subdir not found")))
1196 (let (buffer-read-only opoint
)
1198 (setq opoint
(point))
1199 ;; Don't expand `.'.
1200 ;; Show just the file name within directory.
1201 (let ((default-directory directory
))
1202 (dired-insert-directory
1204 (concat dired-actual-switches
" -d")
1207 ;; Put in desired marker char.
1209 (let ((dired-marker-char
1210 (if (integerp marker-char
) marker-char
1211 dired-marker-char
)))
1213 ;; Compensate for a bug in ange-ftp.
1214 ;; It inserts the file's absolute name, rather than
1215 ;; the relative one. That may be hard to fix since it
1216 ;; is probably controlled by something in ftp.
1218 (let ((inserted-name (dired-get-filename 'verbatim
)))
1219 (if (file-name-directory inserted-name
)
1222 (forward-char (- (length inserted-name
)))
1223 (setq props
(text-properties-at (point)))
1224 (delete-char (length inserted-name
))
1227 (set-text-properties pt
(point) props
))
1231 (if dired-after-readin-hook
1232 ;; The subdir-alist is not affected...
1233 (save-excursion ; ...so we can run it right now:
1236 (narrow-to-region (point)
1237 (line-beginning-position 2))
1238 (run-hooks 'dired-after-readin-hook
))))
1239 (dired-move-to-filename))
1240 ;; return nil if all went well
1242 (if reason
; don't move away on failure
1244 (not reason
))) ; return t on success, nil else
1245 ;; Don't do it (dired-omit-mode).
1246 ;; Return t for success (perhaps we should return file-exists-p).
1249 (defun dired-after-subdir-garbage (dir)
1250 ;; Return pos of first file line of DIR, skipping header and total
1251 ;; or wildcard lines.
1252 ;; Important: never moves into the next subdir.
1253 ;; DIR is assumed to be unhidden.
1255 (or (dired-goto-subdir dir
) (error "This cannot happen"))
1257 (while (and (not (eolp)) ; don't cross subdir boundary
1258 (not (dired-move-to-filename)))
1263 (defun dired-remove-file (file)
1264 (dired-fun-in-all-buffers
1265 (file-name-directory file
) (file-name-nondirectory file
)
1266 (function dired-remove-entry
) file
))
1268 (defun dired-remove-entry (file)
1270 (and (dired-goto-file file
)
1271 (let (buffer-read-only)
1272 (delete-region (progn (beginning-of-line) (point))
1273 (line-beginning-position 2))))))
1276 (defun dired-relist-file (file)
1277 "Create or update the line for FILE in all Dired buffers it would belong in."
1278 (dired-fun-in-all-buffers (file-name-directory file
)
1279 (file-name-nondirectory file
)
1280 (function dired-relist-entry
) file
))
1282 (defun dired-relist-entry (file)
1283 ;; Relist the line for FILE, or just add it if it did not exist.
1284 ;; FILE must be an absolute file name.
1285 (let (buffer-read-only marker
)
1286 ;; If cursor is already on FILE's line delete-region will cause
1287 ;; save-excursion to fail because of floating makers,
1288 ;; moving point to beginning of line. Sigh.
1290 (and (dired-goto-file file
)
1291 (delete-region (progn (beginning-of-line)
1292 (setq marker
(following-char))
1294 (line-beginning-position 2)))
1295 (setq file
(directory-file-name file
))
1296 (dired-add-entry file
(if (eq ?
\040 marker
) nil marker
)))))
1298 ;;; Copy, move/rename, making hard and symbolic links
1300 (defcustom dired-backup-overwrite nil
1301 "Non-nil if Dired should ask about making backups before overwriting files.
1302 Special value `always' suppresses confirmation."
1303 :type
'(choice (const :tag
"off" nil
)
1304 (const :tag
"suppress" always
)
1305 (other :tag
"ask" t
))
1308 ;; This is a fluid var used in dired-handle-overwrite. It should be
1309 ;; let-bound whenever dired-copy-file etc are called. See
1310 ;; dired-create-files for an example.
1311 (defvar dired-overwrite-confirmed
)
1313 (defun dired-handle-overwrite (to)
1314 ;; Save old version of file TO that is to be overwritten.
1315 ;; `dired-overwrite-confirmed' and `overwrite-backup-query' are fluid vars
1316 ;; from dired-create-files.
1318 (when (and dired-backup-overwrite
1319 dired-overwrite-confirmed
1320 (setq backup
(car (find-backup-file-name to
)))
1321 (or (eq 'always dired-backup-overwrite
)
1322 (dired-query 'overwrite-backup-query
1323 "Make backup for existing file `%s'? "
1325 (rename-file to backup
0) ; confirm overwrite of old backup
1326 (dired-relist-entry backup
))))
1329 (defun dired-copy-file (from to ok-flag
)
1330 (dired-handle-overwrite to
)
1331 (dired-copy-file-recursive from to ok-flag dired-copy-preserve-time t
1332 dired-recursive-copies
))
1334 (declare-function make-symbolic-link
"fileio.c")
1336 (defun dired-copy-file-recursive (from to ok-flag
&optional
1337 preserve-time top recursive
)
1338 (when (and (eq t
(car (file-attributes from
)))
1339 (file-in-directory-p to from
))
1340 (error "Cannot copy `%s' into its subdirectory `%s'" from to
))
1341 (let ((attrs (file-attributes from
)))
1344 (or (eq recursive
'always
)
1345 (yes-or-no-p (format "Recursive copies of %s? " from
))))
1346 (copy-directory from to preserve-time
)
1347 (or top
(dired-handle-overwrite to
))
1349 (if (stringp (car attrs
))
1351 (make-symbolic-link (car attrs
) to ok-flag
)
1352 (copy-file from to ok-flag preserve-time
))
1354 (push (dired-make-relative from
)
1355 dired-create-files-failures
)
1356 (dired-log "Can't set date on %s:\n%s\n" from err
))))))
1359 (defun dired-rename-file (file newname ok-if-already-exists
)
1360 (dired-handle-overwrite newname
)
1361 (rename-file file newname ok-if-already-exists
) ; error is caught in -create-files
1362 ;; Silently rename the visited file of any buffer visiting this file.
1363 (and (get-file-buffer file
)
1364 (with-current-buffer (get-file-buffer file
)
1365 (set-visited-file-name newname nil t
)))
1366 (dired-remove-file file
)
1367 ;; See if it's an inserted subdir, and rename that, too.
1368 (dired-rename-subdir file newname
))
1370 (defun dired-rename-subdir (from-dir to-dir
)
1371 (setq from-dir
(file-name-as-directory from-dir
)
1372 to-dir
(file-name-as-directory to-dir
))
1373 (dired-fun-in-all-buffers from-dir nil
1374 (function dired-rename-subdir-1
) from-dir to-dir
)
1375 ;; Update visited file name of all affected buffers
1376 (let ((expanded-from-dir (expand-file-name from-dir
))
1377 (blist (buffer-list)))
1379 (with-current-buffer (car blist
)
1380 (if (and buffer-file-name
1381 (dired-in-this-tree buffer-file-name expanded-from-dir
))
1382 (let ((modflag (buffer-modified-p))
1383 (to-file (dired-replace-in-string
1384 (concat "^" (regexp-quote from-dir
))
1387 (set-visited-file-name to-file
)
1388 (set-buffer-modified-p modflag
))))
1389 (setq blist
(cdr blist
)))))
1391 (defun dired-rename-subdir-1 (dir to
)
1392 ;; Rename DIR to TO in headerlines and dired-subdir-alist, if DIR or
1393 ;; one of its subdirectories is expanded in this buffer.
1394 (let ((expanded-dir (expand-file-name dir
))
1395 (alist dired-subdir-alist
)
1398 (setq elt
(car alist
)
1400 (if (dired-in-this-tree (car elt
) expanded-dir
)
1401 ;; ELT's subdir is affected by the rename
1402 (dired-rename-subdir-2 elt dir to
)))
1403 (if (equal dir default-directory
)
1404 ;; if top level directory was renamed, lots of things have to be
1407 (dired-unadvertise dir
) ; we no longer dired DIR...
1408 (setq default-directory to
1409 dired-directory
(expand-file-name;; this is correct
1410 ;; with and without wildcards
1411 (file-name-nondirectory dired-directory
)
1413 (let ((new-name (file-name-nondirectory
1414 (directory-file-name dired-directory
))))
1415 ;; try to rename buffer, but just leave old name if new
1416 ;; name would already exist (don't try appending "<%d>")
1417 (or (get-buffer new-name
)
1418 (rename-buffer new-name
)))
1419 ;; ... we dired TO now:
1420 (dired-advertise)))))
1422 (defun dired-rename-subdir-2 (elt dir to
)
1423 ;; Update the headerline and dired-subdir-alist element, as well as
1424 ;; dired-switches-alist element, of directory described by
1425 ;; alist-element ELT to reflect the moving of DIR to TO. Thus, ELT
1426 ;; describes either DIR itself or a subdir of DIR.
1428 (let ((regexp (regexp-quote (directory-file-name dir
)))
1429 (newtext (directory-file-name to
))
1431 (goto-char (dired-get-subdir-min elt
))
1432 ;; Update subdir headerline in buffer
1433 (if (not (looking-at dired-subdir-regexp
))
1434 (error "%s not found where expected - dired-subdir-alist broken?"
1436 (goto-char (match-beginning 1))
1437 (if (re-search-forward regexp
(match-end 1) t
)
1438 (replace-match newtext t t
)
1439 (error "Expected to find `%s' in headerline of %s" dir
(car elt
))))
1440 ;; Update buffer-local dired-subdir-alist and dired-switches-alist
1441 (let ((cons (assoc-string (car elt
) dired-switches-alist
))
1442 (cur-dir (dired-normalize-subdir
1443 (dired-replace-in-string regexp newtext
(car elt
)))))
1444 (setcar elt cur-dir
)
1445 (when cons
(setcar cons cur-dir
))))))
1447 ;; Bound in dired-create-files
1448 (defvar overwrite-query
)
1449 (defvar overwrite-backup-query
)
1451 ;; The basic function for half a dozen variations on cp/mv/ln/ln -s.
1452 (defun dired-create-files (file-creator operation fn-list name-constructor
1453 &optional marker-char
)
1454 "Create one or more new files from a list of existing files FN-LIST.
1455 This function also handles querying the user, updating Dired
1456 buffers, and displaying a success or failure message.
1458 FILE-CREATOR should be a function. It is called once for each
1459 file in FN-LIST, and must create a new file, querying the user
1460 and updating Dired buffers as necessary. It should accept three
1461 arguments: the old file name, the new name, and an argument
1462 OK-IF-ALREADY-EXISTS with the same meaning as in `copy-file'.
1464 OPERATION should be a capitalized string describing the operation
1465 performed (e.g. `Copy'). It is used for error logging.
1467 FN-LIST is the list of files to copy (full absolute file names).
1469 NAME-CONSTRUCTOR should be a function accepting a single
1470 argument, the name of an old file, and returning either the
1471 corresponding new file name or nil to skip.
1473 If optional argument MARKER-CHAR is non-nil, mark each
1474 newly-created file's Dired entry with the character MARKER-CHAR,
1475 or with the current marker character if MARKER-CHAR is t."
1476 (let (dired-create-files-failures failures
1477 skipped
(success-count 0) (total (length fn-list
)))
1478 (let (to overwrite-query
1479 overwrite-backup-query
) ; for dired-handle-overwrite
1480 (dolist (from fn-list
)
1481 (setq to
(funcall name-constructor from
))
1485 (dired-log "Cannot %s to same file: %s\n"
1486 (downcase operation
) from
)))
1488 (setq skipped
(cons (dired-make-relative from
) skipped
))
1489 (let* ((overwrite (file-exists-p to
))
1490 (dired-overwrite-confirmed ; for dired-handle-overwrite
1492 (let ((help-form '(format "\
1493 Type SPC or `y' to overwrite file `%s',
1494 DEL or `n' to skip to next,
1495 ESC or `q' to not overwrite any of the remaining files,
1496 `!' to overwrite all remaining files with no more questions." to
)))
1497 (dired-query 'overwrite-query
1498 "Overwrite `%s'?" to
))))
1499 ;; must determine if FROM is marked before file-creator
1500 ;; gets a chance to delete it (in case of a move).
1502 (cond ((integerp marker-char
) marker-char
)
1503 (marker-char (dired-file-marker from
)) ; slow
1505 ;; Handle the `dired-copy-file' file-creator specially
1506 ;; When copying a directory to another directory or
1507 ;; possibly to itself or one of its subdirectories.
1508 ;; e.g "~/foo/" => "~/test/"
1509 ;; or "~/foo/" =>"~/foo/"
1510 ;; or "~/foo/ => ~/foo/bar/")
1511 ;; In this case the 'name-constructor' have set the destination
1512 ;; TO to "~/test/foo" because the old emacs23 behavior
1513 ;; of `copy-directory' was to not create the subdirectory
1514 ;; and instead copy the contents.
1515 ;; With the new behavior of `copy-directory'
1516 ;; (similar to the `cp' shell command) we don't
1517 ;; need such a construction of the target directory,
1518 ;; so modify the destination TO to "~/test/" instead of "~/test/foo/".
1519 (let ((destname (file-name-directory to
)))
1520 (when (and (file-directory-p from
)
1521 (file-directory-p to
)
1522 (eq file-creator
'dired-copy-file
))
1524 ;; If DESTNAME is a subdirectory of FROM, not a symlink,
1525 ;; and the method in use is copying, signal an error.
1526 (and (eq t
(car (file-attributes destname
)))
1527 (eq file-creator
'dired-copy-file
)
1528 (file-in-directory-p destname from
)
1529 (error "Cannot copy `%s' into its subdirectory `%s'"
1533 (funcall file-creator from to dired-overwrite-confirmed
)
1535 ;; If we get here, file-creator hasn't been aborted
1536 ;; and the old entry (if any) has to be deleted
1537 ;; before adding the new entry.
1538 (dired-remove-file to
))
1539 (setq success-count
(1+ success-count
))
1540 (message "%s: %d of %d" operation success-count total
)
1541 (dired-add-file to actual-marker-char
))
1542 (file-error ; FILE-CREATOR aborted
1544 (push (dired-make-relative from
)
1546 (dired-log "%s `%s' to `%s' failed:\n%s\n"
1547 operation from to err
))))))))
1549 (dired-create-files-failures
1550 (setq failures
(nconc failures dired-create-files-failures
))
1552 (format "%s failed for %d file%s in %d requests"
1553 operation
(length failures
)
1554 (dired-plural-s (length failures
))
1559 (format "%s failed for %d of %d file%s"
1560 operation
(length failures
)
1561 total
(dired-plural-s total
))
1565 (format "%s: %d of %d file%s skipped"
1566 operation
(length skipped
) total
1567 (dired-plural-s total
))
1570 (message "%s: %s file%s"
1571 operation success-count
(dired-plural-s success-count
)))))
1572 (dired-move-to-filename))
1574 (defun dired-do-create-files (op-symbol file-creator operation arg
1575 &optional marker-char op1
1577 "Create a new file for each marked file.
1578 Prompt user for a target directory in which to create the new
1579 files. The target may also be a non-directory file, if only
1580 one file is marked. The initial suggestion for target is the
1581 Dired buffer's current directory (or, if `dired-dwim-target' is
1582 non-nil, the current directory of a neighboring Dired window).
1583 OP-SYMBOL is the symbol for the operation. Function `dired-mark-pop-up'
1584 will determine whether pop-ups are appropriate for this OP-SYMBOL.
1585 FILE-CREATOR and OPERATION as in `dired-create-files'.
1586 ARG as in `dired-get-marked-files'.
1587 Optional arg MARKER-CHAR as in `dired-create-files'.
1588 Optional arg OP1 is an alternate form for OPERATION if there is
1590 Optional arg HOW-TO determines how to treat the target.
1591 If HOW-TO is nil, use `file-directory-p' to determine if the
1592 target is a directory. If so, the marked file(s) are created
1593 inside that directory. Otherwise, the target is a plain file;
1594 an error is raised unless there is exactly one marked file.
1595 If HOW-TO is t, target is always treated as a plain file.
1596 Otherwise, HOW-TO should be a function of one argument, TARGET.
1597 If its return value is nil, TARGET is regarded as a plain file.
1598 If it return value is a list, TARGET is a generalized
1599 directory (e.g. some sort of archive). The first element of
1600 this list must be a function with at least four arguments:
1601 operation - as OPERATION above.
1602 rfn-list - list of the relative names for the marked files.
1603 fn-list - list of the absolute names for the marked files.
1604 target - the name of the target itself.
1605 The rest of into-dir are optional arguments.
1606 For any other return value, TARGET is treated as a directory."
1607 (or op1
(setq op1 operation
))
1608 (let* ((fn-list (dired-get-marked-files nil arg
))
1609 (rfn-list (mapcar (function dired-make-relative
) fn-list
))
1610 (dired-one-file ; fluid variable inside dired-create-files
1611 (and (consp fn-list
) (null (cdr fn-list
)) (car fn-list
)))
1612 (target-dir (dired-dwim-target-directory))
1613 (default (and dired-one-file
1614 (expand-file-name (file-name-nondirectory (car fn-list
))
1616 (defaults (dired-dwim-target-defaults fn-list target-dir
))
1617 (target (expand-file-name ; fluid variable inside dired-create-files
1618 (minibuffer-with-setup-hook
1620 (set (make-local-variable 'minibuffer-default-add-function
) nil
)
1621 (setq minibuffer-default defaults
))
1622 (dired-mark-read-file-name
1623 (concat (if dired-one-file op1 operation
) " %s to: ")
1624 target-dir op-symbol arg rfn-list default
))))
1625 (into-dir (cond ((null how-to
)
1626 ;; Allow DOS/Windows users to change the letter
1627 ;; case of a directory. If we don't test these
1628 ;; conditions up front, file-directory-p below
1629 ;; will return t because the filesystem is
1630 ;; case-insensitive, and Emacs will try to move
1631 ;; foo -> foo/foo, which fails.
1632 (if (and (memq system-type
'(ms-dos windows-nt cygwin
))
1633 (eq op-symbol
'move
)
1636 (expand-file-name (car fn-list
)))
1638 (expand-file-name target
)))
1640 (file-name-nondirectory (car fn-list
))
1641 (file-name-nondirectory target
))))
1643 (file-directory-p target
)))
1645 (t (funcall how-to target
)))))
1646 (if (and (consp into-dir
) (functionp (car into-dir
)))
1647 (apply (car into-dir
) operation rfn-list fn-list target
(cdr into-dir
))
1648 (if (not (or dired-one-file into-dir
))
1649 (error "Marked %s: target must be a directory: %s" operation target
))
1650 ;; rename-file bombs when moving directories unless we do this:
1651 (or into-dir
(setq target
(directory-file-name target
)))
1653 file-creator operation fn-list
1654 (if into-dir
; target is a directory
1655 ;; This function uses fluid variable target when called
1656 ;; inside dired-create-files:
1659 (expand-file-name (file-name-nondirectory from
) target
)))
1660 (function (lambda (_from) target
)))
1663 ;; Read arguments for a marked-files command that wants a file name,
1664 ;; perhaps popping up the list of marked files.
1665 ;; ARG is the prefix arg and indicates whether the files came from
1666 ;; marks (ARG=nil) or a repeat factor (integerp ARG).
1667 ;; If the current file was used, the list has but one element and ARG
1668 ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)).
1669 ;; DEFAULT is the default value to return if the user just hits RET;
1670 ;; if it is omitted or nil, then the name of the directory is used.
1672 (defun dired-mark-read-file-name (prompt dir op-symbol arg files
1676 (function read-file-name
)
1677 (format prompt
(dired-mark-prompt arg files
)) dir default
))
1679 (defun dired-dwim-target-directory ()
1680 ;; Try to guess which target directory the user may want.
1681 ;; If there is a dired buffer displayed in one of the next windows,
1682 ;; use its current subdir, else use current subdir of this dired buffer.
1683 (let ((this-dir (and (eq major-mode
'dired-mode
)
1684 (dired-current-directory))))
1685 ;; non-dired buffer may want to profit from this function, e.g. vm-uudecode
1686 (if dired-dwim-target
1687 (let* ((other-win (get-window-with-predicate
1689 (with-current-buffer (window-buffer window
)
1690 (eq major-mode
'dired-mode
)))))
1691 (other-dir (and other-win
1692 (with-current-buffer (window-buffer other-win
)
1693 (and (eq major-mode
'dired-mode
)
1694 (dired-current-directory))))))
1695 (or other-dir this-dir
))
1698 (defun dired-dwim-target-defaults (fn-list target-dir
)
1699 ;; Return a list of default values for file-reading functions in Dired.
1700 ;; This list may contain directories from Dired buffers in other windows.
1701 ;; `fn-list' is a list of file names used to build a list of defaults.
1702 ;; When nil or more than one element, a list of defaults will
1703 ;; contain only directory names. `target-dir' is a directory name
1704 ;; to exclude from the returned list, for the case when this
1705 ;; directory name is already presented in initial input.
1706 ;; For Dired operations that support `dired-dwim-target',
1707 ;; the argument `target-dir' should have the value returned
1708 ;; from `dired-dwim-target-directory'.
1709 (let ((dired-one-file
1710 (and (consp fn-list
) (null (cdr fn-list
)) (car fn-list
)))
1711 (current-dir (and (eq major-mode
'dired-mode
)
1712 (dired-current-directory)))
1714 ;; Get a list of directories of visible buffers in dired-mode.
1715 (walk-windows (lambda (w)
1716 (with-current-buffer (window-buffer w
)
1717 (and (eq major-mode
'dired-mode
)
1718 (push (dired-current-directory) dired-dirs
)))))
1719 ;; Force the current dir to be the first in the list.
1721 (delete-dups (delq nil
(cons current-dir
(nreverse dired-dirs
)))))
1722 ;; Remove the target dir (if specified) or the current dir from
1723 ;; default values, because it should be already in initial input.
1724 (setq dired-dirs
(delete (or target-dir current-dir
) dired-dirs
))
1725 ;; Return a list of default values.
1727 ;; For one file operation, provide a list that contains
1728 ;; other directories, other directories with the appended filename
1729 ;; and the current directory with the appended filename, e.g.
1731 ;; 2. /TARGET-DIR/FILENAME
1732 ;; 3. /CURRENT-DIR/FILENAME
1734 (mapcar (lambda (dir)
1736 (file-name-nondirectory (car fn-list
)) dir
))
1737 (reverse dired-dirs
))
1738 (list (expand-file-name
1739 (file-name-nondirectory (car fn-list
))
1740 (or target-dir current-dir
))))
1741 ;; For multi-file operation, return only a list of other directories.
1746 (defun dired-create-directory (directory)
1747 "Create a directory called DIRECTORY.
1748 If DIRECTORY already exists, signal an error."
1750 (list (read-file-name "Create directory: " (dired-current-directory))))
1751 (let* ((expanded (directory-file-name (expand-file-name directory
)))
1753 (if (file-exists-p expanded
)
1754 (error "Cannot create directory %s: file exists" expanded
))
1755 ;; Find the topmost nonexistent parent dir (variable `new')
1756 (while (and try
(not (file-exists-p try
)) (not (equal new try
)))
1758 try
(directory-file-name (file-name-directory try
))))
1759 (make-directory expanded t
)
1761 (dired-add-file new
)
1762 (dired-move-to-filename))))
1764 (defun dired-into-dir-with-symlinks (target)
1765 (and (file-directory-p target
)
1766 (not (file-symlink-p target
))))
1767 ;; This may not always be what you want, especially if target is your
1768 ;; home directory and it happens to be a symbolic link, as is often the
1769 ;; case with NFS and automounters. Or if you want to make symlinks
1770 ;; into directories that themselves are only symlinks, also quite
1773 ;; So we don't use this function as value for HOW-TO in
1774 ;; dired-do-symlink, which has the minor disadvantage of
1775 ;; making links *into* a symlinked-dir, when you really wanted to
1776 ;; *overwrite* that symlink. In that (rare, I guess) case, you'll
1777 ;; just have to remove that symlink by hand before making your marked
1780 (defvar dired-copy-how-to-fn nil
1781 "Either nil or a function used by `dired-do-copy' to determine target.
1782 See HOW-TO argument for `dired-do-create-files'.")
1785 (defun dired-do-copy (&optional arg
)
1786 "Copy all marked (or next ARG) files, or copy the current file.
1787 When operating on just the current file, prompt for the new name.
1789 When operating on multiple or marked files, prompt for a target
1790 directory, and make the new copies in that directory, with the
1791 same names as the original files. The initial suggestion for the
1792 target directory is the Dired buffer's current directory (or, if
1793 `dired-dwim-target' is non-nil, the current directory of a
1794 neighboring Dired window).
1796 If `dired-copy-preserve-time' is non-nil, this command preserves
1797 the modification time of each old file in the copy, similar to
1798 the \"-p\" option for the \"cp\" shell command.
1800 This command copies symbolic links by creating new ones, similar
1801 to the \"-d\" option for the \"cp\" shell command."
1803 (let ((dired-recursive-copies dired-recursive-copies
))
1804 (dired-do-create-files 'copy
(function dired-copy-file
)
1806 arg dired-keep-marker-copy
1807 nil dired-copy-how-to-fn
)))
1810 (defun dired-do-symlink (&optional arg
)
1811 "Make symbolic links to current file or all marked (or next ARG) files.
1812 When operating on just the current file, you specify the new name.
1813 When operating on multiple or marked files, you specify a directory
1814 and new symbolic links are made in that directory
1815 with the same names that the files currently have. The default
1816 suggested for the target directory depends on the value of
1817 `dired-dwim-target', which see.
1819 For relative symlinks, use \\[dired-do-relsymlink]."
1821 (dired-do-create-files 'symlink
(function make-symbolic-link
)
1822 "Symlink" arg dired-keep-marker-symlink
))
1825 (defun dired-do-hardlink (&optional arg
)
1826 "Add names (hard links) current file or all marked (or next ARG) files.
1827 When operating on just the current file, you specify the new name.
1828 When operating on multiple or marked files, you specify a directory
1829 and new hard links are made in that directory
1830 with the same names that the files currently have. The default
1831 suggested for the target directory depends on the value of
1832 `dired-dwim-target', which see."
1834 (dired-do-create-files 'hardlink
(function dired-hardlink
)
1835 "Hardlink" arg dired-keep-marker-hardlink
))
1837 (defun dired-hardlink (file newname
&optional ok-if-already-exists
)
1838 (dired-handle-overwrite newname
)
1839 ;; error is caught in -create-files
1840 (add-name-to-file file newname ok-if-already-exists
)
1841 ;; Update the link count
1842 (dired-relist-file file
))
1845 (defun dired-do-rename (&optional arg
)
1846 "Rename current file or all marked (or next ARG) files.
1847 When renaming just the current file, you specify the new name.
1848 When renaming multiple or marked files, you specify a directory.
1849 This command also renames any buffers that are visiting the files.
1850 The default suggested for the target directory depends on the value
1851 of `dired-dwim-target', which see."
1853 (dired-do-create-files 'move
(function dired-rename-file
)
1854 "Move" arg dired-keep-marker-rename
"Rename"))
1855 ;;;###end dired-cp.el
1858 ;;;###begin dired-re.el
1859 (defvar rename-regexp-query
)
1861 (defun dired-do-create-files-regexp
1862 (file-creator operation arg regexp newname
&optional whole-name marker-char
)
1863 ;; Create a new file for each marked file using regexps.
1864 ;; FILE-CREATOR and OPERATION as in dired-create-files.
1865 ;; ARG as in dired-get-marked-files.
1866 ;; Matches each marked file against REGEXP and constructs the new
1867 ;; filename from NEWNAME (like in function replace-match).
1868 ;; Optional arg WHOLE-NAME means match/replace the whole file name
1869 ;; instead of only the non-directory part of the file.
1870 ;; Optional arg MARKER-CHAR as in dired-create-files.
1871 (let* ((fn-list (dired-get-marked-files nil arg
))
1872 (operation-prompt (concat operation
" `%s' to `%s'?"))
1873 (rename-regexp-help-form (format "\
1874 Type SPC or `y' to %s one match, DEL or `n' to skip to next,
1875 `!' to %s all remaining matches with no more questions."
1876 (downcase operation
)
1877 (downcase operation
)))
1878 (regexp-name-constructor
1879 ;; Function to construct new filename using REGEXP and NEWNAME:
1880 (if whole-name
; easy (but rare) case
1883 (let ((to (dired-string-replace-match regexp from newname
))
1884 ;; must bind help-form directly around call to
1886 (help-form rename-regexp-help-form
))
1888 (and (dired-query 'rename-regexp-query
1893 (dired-log "%s: %s did not match regexp %s\n"
1894 operation from regexp
)))))
1895 ;; not whole-name, replace non-directory part only
1898 (let* ((new (dired-string-replace-match
1899 regexp
(file-name-nondirectory from
) newname
))
1900 (to (and new
; nil means there was no match
1901 (expand-file-name new
1902 (file-name-directory from
))))
1903 (help-form rename-regexp-help-form
))
1905 (and (dired-query 'rename-regexp-query
1907 (dired-make-relative from
)
1908 (dired-make-relative to
))
1910 (dired-log "%s: %s did not match regexp %s\n"
1911 operation
(file-name-nondirectory from
) regexp
)))))))
1912 rename-regexp-query
)
1914 file-creator operation fn-list regexp-name-constructor marker-char
)))
1916 (defun dired-mark-read-regexp (operation)
1917 ;; Prompt user about performing OPERATION.
1918 ;; Read and return list of: regexp newname arg whole-name.
1920 (equal 0 (prefix-numeric-value current-prefix-arg
)))
1922 (if whole-name nil current-prefix-arg
))
1925 (concat (if whole-name
"Abs. " "") operation
" from (regexp): ")
1926 nil
'dired-regexp-history
))
1929 (concat (if whole-name
"Abs. " "") operation
" " regexp
" to: "))))
1930 (list regexp newname arg whole-name
)))
1933 (defun dired-do-rename-regexp (regexp newname
&optional arg whole-name
)
1934 "Rename selected files whose names match REGEXP to NEWNAME.
1936 With non-zero prefix argument ARG, the command operates on the next ARG
1937 files. Otherwise, it operates on all the marked files, or the current
1938 file if none are marked.
1940 As each match is found, the user must type a character saying
1941 what to do with it. For directions, type \\[help-command] at that time.
1942 NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
1943 REGEXP defaults to the last regexp used.
1945 With a zero prefix arg, renaming by regexp affects the absolute file name.
1946 Normally, only the non-directory part of the file name is used and changed."
1947 (interactive (dired-mark-read-regexp "Rename"))
1948 (dired-do-create-files-regexp
1949 (function dired-rename-file
)
1950 "Rename" arg regexp newname whole-name dired-keep-marker-rename
))
1953 (defun dired-do-copy-regexp (regexp newname
&optional arg whole-name
)
1954 "Copy selected files whose names match REGEXP to NEWNAME.
1955 See function `dired-do-rename-regexp' for more info."
1956 (interactive (dired-mark-read-regexp "Copy"))
1957 (let ((dired-recursive-copies nil
)) ; No recursive copies.
1958 (dired-do-create-files-regexp
1959 (function dired-copy-file
)
1960 (if dired-copy-preserve-time
"Copy [-p]" "Copy")
1961 arg regexp newname whole-name dired-keep-marker-copy
)))
1964 (defun dired-do-hardlink-regexp (regexp newname
&optional arg whole-name
)
1965 "Hardlink selected files whose names match REGEXP to NEWNAME.
1966 See function `dired-do-rename-regexp' for more info."
1967 (interactive (dired-mark-read-regexp "HardLink"))
1968 (dired-do-create-files-regexp
1969 (function add-name-to-file
)
1970 "HardLink" arg regexp newname whole-name dired-keep-marker-hardlink
))
1973 (defun dired-do-symlink-regexp (regexp newname
&optional arg whole-name
)
1974 "Symlink selected files whose names match REGEXP to NEWNAME.
1975 See function `dired-do-rename-regexp' for more info."
1976 (interactive (dired-mark-read-regexp "SymLink"))
1977 (dired-do-create-files-regexp
1978 (function make-symbolic-link
)
1979 "SymLink" arg regexp newname whole-name dired-keep-marker-symlink
))
1981 (defvar rename-non-directory-query
)
1983 (defun dired-create-files-non-directory
1984 (file-creator basename-constructor operation arg
)
1985 ;; Perform FILE-CREATOR on the non-directory part of marked files
1986 ;; using function BASENAME-CONSTRUCTOR, with query for each file.
1987 ;; OPERATION like in dired-create-files, ARG as in dired-get-marked-files.
1988 (let (rename-non-directory-query)
1992 (dired-get-marked-files nil arg
)
1995 (let ((to (concat (file-name-directory from
)
1996 (funcall basename-constructor
1997 (file-name-nondirectory from
)))))
1998 (and (let ((help-form (format "\
1999 Type SPC or `y' to %s one file, DEL or `n' to skip to next,
2000 `!' to %s all remaining matches with no more questions."
2001 (downcase operation
)
2002 (downcase operation
))))
2003 (dired-query 'rename-non-directory-query
2004 (concat operation
" `%s' to `%s'")
2005 (dired-make-relative from
)
2006 (dired-make-relative to
)))
2008 dired-keep-marker-rename
)))
2010 (defun dired-rename-non-directory (basename-constructor operation arg
)
2011 (dired-create-files-non-directory
2012 (function dired-rename-file
)
2013 basename-constructor operation arg
))
2016 (defun dired-upcase (&optional arg
)
2017 "Rename all marked (or next ARG) files to upper case."
2019 (dired-rename-non-directory (function upcase
) "Rename upcase" arg
))
2022 (defun dired-downcase (&optional arg
)
2023 "Rename all marked (or next ARG) files to lower case."
2025 (dired-rename-non-directory (function downcase
) "Rename downcase" arg
))
2027 ;;;###end dired-re.el
2030 ;;;###begin dired-ins.el
2033 (defun dired-maybe-insert-subdir (dirname &optional
2034 switches no-error-if-not-dir-p
)
2035 "Insert this subdirectory into the same dired buffer.
2036 If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
2037 else inserts it at its natural place (as `ls -lR' would have done).
2038 With a prefix arg, you may edit the ls switches used for this listing.
2039 You can add `R' to the switches to expand the whole tree starting at
2041 This function takes some pains to conform to `ls -lR' output.
2043 Dired remembers switches specified with a prefix arg, so that reverting
2044 the buffer will not reset them. However, using `dired-undo' to re-insert
2045 or delete subdirectories can bypass this machinery. Hence, you sometimes
2046 may have to reset some subdirectory switches after a `dired-undo'.
2047 You can reset all subdirectory switches to the default using
2048 \\<dired-mode-map>\\[dired-reset-subdir-switches].
2049 See Info node `(emacs)Subdir switches' for more details."
2051 (list (dired-get-filename)
2052 (if current-prefix-arg
2053 (read-string "Switches for listing: "
2054 (or dired-subdir-switches dired-actual-switches
)))))
2055 (let ((opoint (point)))
2056 ;; We don't need a marker for opoint as the subdir is always
2057 ;; inserted *after* opoint.
2058 (setq dirname
(file-name-as-directory dirname
))
2059 (or (and (not switches
)
2060 (dired-goto-subdir dirname
))
2061 (dired-insert-subdir dirname switches no-error-if-not-dir-p
))
2062 ;; Push mark so that it's easy to find back. Do this after the
2063 ;; insert message so that the user sees the `Mark set' message.
2064 (push-mark opoint
)))
2067 (defun dired-insert-subdir (dirname &optional switches no-error-if-not-dir-p
)
2068 "Insert this subdirectory into the same Dired buffer.
2069 If it is already present, overwrite the previous entry;
2070 otherwise, insert it at its natural place (as `ls -lR' would
2072 With a prefix arg, you may edit the `ls' switches used for this listing.
2073 You can add `R' to the switches to expand the whole tree starting at
2075 This function takes some pains to conform to `ls -lR' output."
2076 ;; NO-ERROR-IF-NOT-DIR-P needed for special filesystems like
2077 ;; Prospero where dired-ls does the right thing, but
2078 ;; file-directory-p has not been redefined.
2080 (list (dired-get-filename)
2081 (if current-prefix-arg
2082 (read-string "Switches for listing: "
2083 (or dired-subdir-switches dired-actual-switches
)))))
2084 (setq dirname
(file-name-as-directory (expand-file-name dirname
)))
2085 (or no-error-if-not-dir-p
2086 (file-directory-p dirname
)
2087 (error "Attempt to insert a non-directory: %s" dirname
))
2088 (let ((elt (assoc dirname dired-subdir-alist
))
2089 (cons (assoc-string dirname dired-switches-alist
))
2090 (modflag (buffer-modified-p))
2091 (old-switches switches
)
2092 switches-have-R mark-alist case-fold-search buffer-read-only
)
2093 (and (not switches
) cons
(setq switches
(cdr cons
)))
2094 (dired-insert-subdir-validate dirname switches
)
2095 ;; case-fold-search is nil now, so we can test for capital `R':
2096 (if (setq switches-have-R
(and switches
(string-match-p "R" switches
)))
2097 ;; avoid duplicated subdirs
2098 (setq mark-alist
(dired-kill-tree dirname t
)))
2100 ;; If subdir is already present, remove it and remember its marks
2101 (setq mark-alist
(nconc (dired-insert-subdir-del elt
) mark-alist
))
2102 (dired-insert-subdir-newpos dirname
)) ; else compute new position
2103 (dired-insert-subdir-doupdate
2104 dirname elt
(dired-insert-subdir-doinsert dirname switches
))
2107 (setcdr cons switches
)
2108 (push (cons dirname switches
) dired-switches-alist
)))
2109 (when switches-have-R
2110 (dired-build-subdir-alist switches
)
2111 (setq switches
(dired-replace-in-string "R" "" switches
))
2112 (dolist (cur-ass dired-subdir-alist
)
2113 (let ((cur-dir (car cur-ass
)))
2114 (and (dired-in-this-tree cur-dir dirname
)
2115 (let ((cur-cons (assoc-string cur-dir dired-switches-alist
)))
2117 (setcdr cur-cons switches
)
2118 (push (cons cur-dir switches
) dired-switches-alist
)))))))
2119 (dired-initial-position dirname
)
2120 (save-excursion (dired-mark-remembered mark-alist
))
2121 (restore-buffer-modified-p modflag
)))
2123 (defun dired-insert-subdir-validate (dirname &optional switches
)
2124 ;; Check that it is valid to insert DIRNAME with SWITCHES.
2125 ;; Signal an error if invalid (e.g. user typed `i' on `..').
2126 (or (dired-in-this-tree dirname
(expand-file-name default-directory
))
2127 (error "%s: not in this directory tree" dirname
))
2128 (let ((real-switches (or switches dired-subdir-switches
)))
2130 (let (case-fold-search)
2134 (or (eq (null (string-match-p x real-switches
))
2135 (null (string-match-p x dired-actual-switches
)))
2137 "Can't have dirs with and without -%s switches together" x
))))
2138 ;; all switches that make a difference to dired-get-filename:
2141 (defun dired-alist-add (dir new-marker
)
2142 ;; Add new DIR at NEW-MARKER. Sort alist.
2143 (dired-alist-add-1 dir new-marker
)
2146 (defun dired-alist-sort ()
2147 ;; Keep the alist sorted on buffer position.
2148 (setq dired-subdir-alist
2149 (sort dired-subdir-alist
2150 (function (lambda (elt1 elt2
)
2151 (> (dired-get-subdir-min elt1
)
2152 (dired-get-subdir-min elt2
)))))))
2154 (defun dired-kill-tree (dirname &optional remember-marks kill-root
)
2155 "Kill all proper subdirs of DIRNAME, excluding DIRNAME itself.
2156 Interactively, you can kill DIRNAME as well by using a prefix argument.
2157 In interactive use, the command prompts for DIRNAME.
2159 When called from Lisp, if REMEMBER-MARKS is non-nil, return an alist
2160 of marked files. If KILL-ROOT is non-nil, kill DIRNAME as well."
2161 (interactive "DKill tree below directory: \ni\nP")
2162 (setq dirname
(file-name-as-directory (expand-file-name dirname
)))
2163 (let ((s-alist dired-subdir-alist
) dir m-alist
)
2165 (setq dir
(car (car s-alist
))
2166 s-alist
(cdr s-alist
))
2167 (and (or kill-root
(not (string-equal dir dirname
)))
2168 (dired-in-this-tree dir dirname
)
2169 (dired-goto-subdir dir
)
2170 (setq m-alist
(nconc (dired-kill-subdir remember-marks
) m-alist
))))
2173 (defun dired-insert-subdir-newpos (new-dir)
2174 ;; Find pos for new subdir, according to tree order.
2175 ;;(goto-char (point-max))
2176 (let ((alist dired-subdir-alist
) elt dir new-pos
)
2178 (setq elt
(car alist
)
2181 (if (dired-tree-lessp dir new-dir
)
2182 ;; Insert NEW-DIR after DIR
2183 (setq new-pos
(dired-get-subdir-max elt
)
2185 (goto-char new-pos
))
2186 ;; want a separating newline between subdirs
2192 (defun dired-insert-subdir-del (element)
2193 ;; Erase an already present subdir (given by ELEMENT) from buffer.
2194 ;; Move to that buffer position. Return a mark-alist.
2195 (let ((begin-marker (dired-get-subdir-min element
)))
2196 (goto-char begin-marker
)
2197 ;; Are at beginning of subdir (and inside it!). Now determine its end:
2198 (goto-char (dired-subdir-max))
2199 (or (eobp);; want a separating newline _between_ subdirs:
2202 (dired-remember-marks begin-marker
(point))
2203 (delete-region begin-marker
(point)))))
2205 (defun dired-insert-subdir-doinsert (dirname switches
)
2206 ;; Insert ls output after point.
2207 ;; Return the boundary of the inserted text (as list of BEG and END).
2209 (let ((begin (point)))
2210 (let ((dired-actual-switches
2212 dired-subdir-switches
2213 (dired-replace-in-string "R" "" dired-actual-switches
))))
2214 (if (equal dirname
(car (car (last dired-subdir-alist
))))
2215 ;; If doing the top level directory of the buffer,
2216 ;; redo it as specified in dired-directory.
2217 (dired-readin-insert)
2218 (dired-insert-directory dirname dired-actual-switches nil nil t
)))
2219 (list begin
(point)))))
2221 (defun dired-insert-subdir-doupdate (dirname elt beg-end
)
2222 ;; Point is at the correct subdir alist position for ELT,
2223 ;; BEG-END is the subdir-region (as list of begin and end).
2224 (if elt
; subdir was already present
2225 ;; update its position (should actually be unchanged)
2226 (set-marker (dired-get-subdir-min elt
) (point-marker))
2227 (dired-alist-add dirname
(point-marker)))
2228 ;; The hook may depend on the subdir-alist containing the just
2229 ;; inserted subdir, so run it after dired-alist-add:
2230 (if dired-after-readin-hook
2232 (let ((begin (nth 0 beg-end
))
2233 (end (nth 1 beg-end
)))
2236 (narrow-to-region begin end
)
2237 ;; hook may add or delete lines, but the subdir boundary
2239 (run-hooks 'dired-after-readin-hook
))))))
2241 (defun dired-tree-lessp (dir1 dir2
)
2242 ;; Lexicographic order on file name components, like `ls -lR':
2243 ;; DIR1 < DIR2 if DIR1 comes *before* DIR2 in an `ls -lR' listing,
2244 ;; i.e., if DIR1 is a (grand)parent dir of DIR2,
2245 ;; or DIR1 and DIR2 are in the same parentdir and their last
2246 ;; components are string-lessp.
2247 ;; Thus ("/usr/" "/usr/bin") and ("/usr/a/" "/usr/b/") are tree-lessp.
2248 ;; string-lessp could arguably be replaced by file-newer-than-file-p
2249 ;; if dired-actual-switches contained `t'.
2250 (setq dir1
(file-name-as-directory dir1
)
2251 dir2
(file-name-as-directory dir2
))
2252 (let ((components-1 (dired-split "/" dir1
))
2253 (components-2 (dired-split "/" dir2
)))
2254 (while (and components-1
2256 (equal (car components-1
) (car components-2
)))
2257 (setq components-1
(cdr components-1
)
2258 components-2
(cdr components-2
)))
2259 (let ((c1 (car components-1
))
2260 (c2 (car components-2
)))
2263 (string-lessp c1 c2
))
2264 ((and (null c1
) (null c2
))
2265 nil
) ; they are equal, not lessp
2266 ((null c1
) ; c2 is a subdir of c1: c1<c2
2268 ((null c2
) ; c1 is a subdir of c2: c1>c2
2270 (t (error "This can't happen"))))))
2272 ;; There should be a builtin split function - inverse to mapconcat.
2273 (defun dired-split (pat str
&optional limit
)
2274 "Splitting on regexp PAT, turn string STR into a list of substrings.
2275 Optional third arg LIMIT (>= 1) is a limit to the length of the
2277 Thus, if SEP is a regexp that only matches itself,
2279 (mapconcat 'identity (dired-split SEP STRING) SEP)
2281 is always equal to STRING."
2282 (let* ((start (string-match pat str
))
2283 (result (list (substring str
0 start
)))
2285 (end (if start
(match-end 0))))
2286 (if end
; else nothing left
2287 (while (and (or (not (integerp limit
))
2289 (string-match pat str end
))
2290 (setq start
(match-beginning 0)
2292 result
(cons (substring str end start
) result
)
2296 (if (and (or (not (integerp limit
))
2298 end
) ; else nothing left
2300 (cons (substring str end
) result
)))
2303 ;;; moving by subdirectories
2306 (defun dired-prev-subdir (arg &optional no-error-if-not-found no-skip
)
2307 "Go to previous subdirectory, regardless of level.
2308 When called interactively and not on a subdir line, go to this subdir's line."
2311 (list (if current-prefix-arg
2312 (prefix-numeric-value current-prefix-arg
)
2313 ;; if on subdir start already, don't stay there!
2314 (if (dired-get-subdir) 1 0))))
2315 (dired-next-subdir (- arg
) no-error-if-not-found no-skip
))
2317 (defun dired-subdir-min ()
2319 (if (not (dired-prev-subdir 0 t t
))
2320 (error "Not in a subdir!")
2324 (defun dired-goto-subdir (dir)
2325 "Go to end of header line of DIR in this dired buffer.
2326 Return value of point on success, otherwise return nil.
2327 The next char is either \\n, or \\r if DIR is hidden."
2329 (prog1 ; let push-mark display its message
2330 (list (expand-file-name
2331 (completing-read "Goto in situ directory: " ; prompt
2332 dired-subdir-alist
; table
2335 (dired-current-directory))))
2337 (setq dir
(file-name-as-directory dir
))
2338 (let ((elt (assoc dir dired-subdir-alist
)))
2340 (goto-char (dired-get-subdir-min elt
))
2341 ;; dired-subdir-hidden-p and dired-add-entry depend on point being
2342 ;; at either \r or \n after this function succeeds.
2343 (progn (skip-chars-forward "^\r\n")
2347 (defun dired-mark-subdir-files ()
2348 "Mark all files except `.' and `..' in current subdirectory.
2349 If the Dired buffer shows multiple directories, this command
2350 marks the files listed in the subdirectory that point is in."
2352 (let ((p-min (dired-subdir-min)))
2353 (dired-mark-files-in-region p-min
(dired-subdir-max))))
2356 (defun dired-kill-subdir (&optional remember-marks
)
2357 "Remove all lines of current subdirectory.
2358 Lower levels are unaffected."
2359 ;; With optional REMEMBER-MARKS, return a mark-alist.
2361 (let* ((beg (dired-subdir-min))
2362 (end (dired-subdir-max))
2363 (modflag (buffer-modified-p))
2364 (cur-dir (dired-current-directory))
2365 (cons (assoc-string cur-dir dired-switches-alist
))
2367 (if (equal cur-dir default-directory
)
2368 (error "Attempt to kill top level directory"))
2370 (if remember-marks
(dired-remember-marks beg end
))
2371 (delete-region beg end
)
2372 (if (eobp) ; don't leave final blank line
2374 (dired-unsubdir cur-dir
)
2376 (setq dired-switches-alist
(delete cons dired-switches-alist
)))
2377 (restore-buffer-modified-p modflag
))))
2379 (defun dired-unsubdir (dir)
2380 ;; Remove DIR from the alist
2381 (setq dired-subdir-alist
2382 (delq (assoc dir dired-subdir-alist
) dired-subdir-alist
)))
2385 (defun dired-tree-up (arg)
2386 "Go up ARG levels in the dired tree."
2388 (let ((dir (dired-current-directory)))
2391 dir
(file-name-directory (directory-file-name dir
))))
2392 ;;(setq dir (expand-file-name dir))
2393 (or (dired-goto-subdir dir
)
2394 (error "Cannot go up to %s - not in this tree" dir
))))
2397 (defun dired-tree-down ()
2398 "Go down in the dired tree."
2400 (let ((dir (dired-current-directory)) ; has slash
2401 pos case-fold-search
) ; filenames are case sensitive
2402 (let ((rest (reverse dired-subdir-alist
)) elt
)
2404 (setq elt
(car rest
)
2406 (if (dired-in-this-tree (directory-file-name (car elt
)) dir
)
2408 pos
(dired-goto-subdir (car elt
))))))
2411 (error "At the bottom"))))
2415 (defun dired-unhide-subdir ()
2416 (let (buffer-read-only)
2417 (subst-char-in-region (dired-subdir-min) (dired-subdir-max) ?
\r ?
\n)))
2419 (defun dired-hide-check ()
2420 (or selective-display
2421 (error "selective-display must be t for subdir hiding to work!")))
2423 (defun dired-subdir-hidden-p (dir)
2424 (and selective-display
2426 (dired-goto-subdir dir
)
2427 (looking-at-p "\r"))))
2430 (defun dired-hide-subdir (arg)
2431 "Hide or unhide the current subdirectory and move to next directory.
2432 Optional prefix arg is a repeat factor.
2433 Use \\[dired-hide-all] to (un)hide all directories."
2436 (let ((modflag (buffer-modified-p)))
2437 (while (>= (setq arg
(1- arg
)) 0)
2438 (let* ((cur-dir (dired-current-directory))
2439 (hidden-p (dired-subdir-hidden-p cur-dir
))
2440 (elt (assoc cur-dir dired-subdir-alist
))
2441 (end-pos (1- (dired-get-subdir-max elt
)))
2443 ;; keep header line visible, hide rest
2444 (goto-char (dired-get-subdir-min elt
))
2445 (skip-chars-forward "^\n\r")
2447 (subst-char-in-region (point) end-pos ?
\r ?
\n)
2448 (subst-char-in-region (point) end-pos ?
\n ?
\r)))
2449 (dired-next-subdir 1 t
))
2450 (restore-buffer-modified-p modflag
)))
2453 (defun dired-hide-all (&optional ignored
)
2454 "Hide all subdirectories, leaving only their header lines.
2455 If there is already something hidden, make everything visible again.
2456 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2459 (let ((modflag (buffer-modified-p))
2462 (goto-char (point-min))
2463 (search-forward "\r" nil t
))
2464 ;; unhide - bombs on \r in filenames
2465 (subst-char-in-region (point-min) (point-max) ?
\r ?
\n)
2467 (let ((pos (point-max)) ; pos of end of last directory
2468 (alist dired-subdir-alist
))
2469 (while alist
; while there are dirs before pos
2470 (subst-char-in-region (dired-get-subdir-min (car alist
)) ; pos of prev dir
2472 (goto-char pos
) ; current dir
2473 ;; we're somewhere on current dir's line
2477 (setq pos
(dired-get-subdir-min (car alist
))) ; prev dir gets current dir
2478 (setq alist
(cdr alist
)))))
2479 (restore-buffer-modified-p modflag
)))
2481 ;;;###end dired-ins.el
2484 ;; Search only in file names in the Dired buffer.
2486 (defcustom dired-isearch-filenames nil
2487 "Non-nil to Isearch in file names only.
2488 If t, Isearch in Dired always matches only file names.
2489 If `dwim', Isearch matches file names when initial point position is on
2490 a file name. Otherwise, it searches the whole buffer without restrictions."
2491 :type
'(choice (const :tag
"No restrictions" nil
)
2492 (const :tag
"When point is on a file name initially, search file names" dwim
)
2493 (const :tag
"Always search in file names" t
))
2497 (define-minor-mode dired-isearch-filenames-mode
2498 "Toggle file names searching on or off.
2499 When on, Isearch skips matches outside file names using the predicate
2500 `dired-isearch-filter-filenames' that matches only at file names.
2501 When off, it uses the original predicate."
2503 (if dired-isearch-filenames-mode
2504 (add-function :before-while
(local 'isearch-filter-predicate
)
2505 #'dired-isearch-filter-filenames
2506 '((isearch-message-prefix .
"filename ")))
2507 (remove-function (local 'isearch-filter-predicate
)
2508 #'dired-isearch-filter-filenames
))
2510 (setq isearch-success t isearch-adjusted t
)
2514 (defun dired-isearch-filenames-setup ()
2515 "Set up isearch to search in Dired file names.
2516 Intended to be added to `isearch-mode-hook'."
2517 (when (or (eq dired-isearch-filenames t
)
2518 (and (eq dired-isearch-filenames
'dwim
)
2519 (get-text-property (point) 'dired-filename
)))
2520 (define-key isearch-mode-map
"\M-sff" 'dired-isearch-filenames-mode
)
2521 (dired-isearch-filenames-mode 1)
2522 (add-hook 'isearch-mode-end-hook
'dired-isearch-filenames-end nil t
)))
2524 (defun dired-isearch-filenames-end ()
2525 "Clean up the Dired file name search after terminating isearch."
2526 (define-key isearch-mode-map
"\M-sff" nil
)
2527 (dired-isearch-filenames-mode -
1)
2528 (remove-hook 'isearch-mode-end-hook
'dired-isearch-filenames-end t
))
2530 (defun dired-isearch-filter-filenames (beg end
)
2531 "Test whether the current search hit is a file name.
2532 Return non-nil if the text from BEG to END is part of a file
2533 name (has the text property `dired-filename')."
2534 (text-property-not-all (min beg end
) (max beg end
)
2535 'dired-filename nil
))
2538 (defun dired-isearch-filenames ()
2539 "Search for a string using Isearch only in file names in the Dired buffer."
2541 (let ((dired-isearch-filenames t
))
2542 (isearch-forward nil t
)))
2545 (defun dired-isearch-filenames-regexp ()
2546 "Search for a regexp using Isearch only in file names in the Dired buffer."
2548 (let ((dired-isearch-filenames t
))
2549 (isearch-forward-regexp nil t
)))
2552 ;; Functions for searching in tags style among marked files.
2555 (defun dired-do-isearch ()
2556 "Search for a string through all marked files using Isearch."
2558 (multi-isearch-files
2559 (dired-get-marked-files nil nil
'dired-nondirectory-p
)))
2562 (defun dired-do-isearch-regexp ()
2563 "Search for a regexp through all marked files using Isearch."
2565 (multi-isearch-files-regexp
2566 (dired-get-marked-files nil nil
'dired-nondirectory-p
)))
2569 (defun dired-do-search (regexp)
2570 "Search through all marked files for a match for REGEXP.
2571 Stops when a match is found.
2572 To continue searching for next match, use command \\[tags-loop-continue]."
2573 (interactive "sSearch marked files (regexp): ")
2574 (tags-search regexp
'(dired-get-marked-files nil nil
'dired-nondirectory-p
)))
2577 (defun dired-do-query-replace-regexp (from to
&optional delimited
)
2578 "Do `query-replace-regexp' of FROM with TO, on all marked files.
2579 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
2580 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
2581 with the command \\[tags-loop-continue]."
2584 (query-replace-read-args
2585 "Query replace regexp in marked files" t t
)))
2586 (list (nth 0 common
) (nth 1 common
) (nth 2 common
))))
2587 (dolist (file (dired-get-marked-files nil nil
'dired-nondirectory-p
))
2588 (let ((buffer (get-file-buffer file
)))
2589 (if (and buffer
(with-current-buffer buffer
2591 (error "File `%s' is visited read-only" file
))))
2592 (tags-query-replace from to delimited
2593 '(dired-get-marked-files nil nil
'dired-nondirectory-p
)))
2595 (defun dired-nondirectory-p (file)
2596 (not (file-directory-p file
)))
2599 (defun dired-show-file-type (file &optional deref-symlinks
)
2600 "Print the type of FILE, according to the `file' command.
2601 If you give a prefix to this command, and FILE is a symbolic
2602 link, then the type of the file linked to by FILE is printed
2604 (interactive (list (dired-get-filename t
) current-prefix-arg
))
2605 (let (process-file-side-effects)
2608 (process-file "file" nil t t
"-L" "--" file
)
2609 (process-file "file" nil t t
"--" file
))
2611 (backward-delete-char 1))
2612 (message "%s" (buffer-string)))))
2614 (provide 'dired-aux
)
2617 ;; byte-compile-dynamic: t
2618 ;; generated-autoload-file: "dired.el"
2621 ;;; dired-aux.el ends here