Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / lisp / dired-aux.el
blobe8b5e6755ea8da339f2887f9e1925f8c4b4de60d
1 ;;; dired-aux.el --- less commonly used parts of dired -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1992, 1994, 1998, 2000-2018 Free Software
4 ;; Foundation, Inc.
6 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>.
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: files
9 ;; Package: emacs
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 <https://www.gnu.org/licenses/>.
26 ;;; Commentary:
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
30 ;; functions.
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.
36 ;;; Code:
38 (require 'cl-lib)
39 ;; We need macros in dired.el to compile properly,
40 ;; and we call subroutines in it too.
41 (require 'dired)
43 (defvar dired-create-files-failures nil
44 "Variable where `dired-create-files' records failing file names.
45 Functions that operate recursively can store additional names
46 into this list; they also should call `dired-log' to log the errors.")
48 ;;; 15K
49 ;;;###begin dired-cmd.el
50 ;; Diffing and compressing
52 (defconst dired-star-subst-regexp "\\(^\\|[ \t]\\)\\*\\([ \t]\\|$\\)")
53 (defconst dired-quark-subst-regexp "\\(^\\|[ \t]\\)\\?\\([ \t]\\|$\\)")
54 (make-obsolete-variable 'dired-star-subst-regexp nil "26.1")
55 (make-obsolete-variable 'dired-quark-subst-regexp nil "26.1")
57 (defun dired-isolated-string-re (string)
58 "Return a regexp to match STRING isolated.
59 Isolated means that STRING is surrounded by spaces or at the beginning/end
60 of a string followed/prefixed with an space.
61 The regexp capture the preceding blank, STRING and the following blank as
62 the groups 1, 2 and 3 respectively."
63 (format "\\(\\`\\|[ \t]\\)\\(%s\\)\\([ \t]\\|\\'\\)" string))
65 (defun dired--star-or-qmark-p (string match &optional keep)
66 "Return non-nil if STRING contains isolated MATCH or `\\=`?\\=`'.
67 MATCH should be the strings \"?\", `\\=`?\\=`', \"*\" or nil. The latter
68 means STRING contains either \"?\" or `\\=`?\\=`' or \"*\".
69 If optional arg KEEP is non-nil, then preserve the match data. Otherwise,
70 this function changes it and saves MATCH as the second match group.
72 Isolated means that MATCH is surrounded by spaces or at the beginning/end
73 of STRING followed/prefixed with an space. A match to `\\=`?\\=`',
74 isolated or not, is also valid."
75 (let ((regexps (list (dired-isolated-string-re (if match (regexp-quote match) "[*?]")))))
76 (when (or (null match) (equal match "?"))
77 (setq regexps (append (list "\\(\\)\\(`\\?`\\)\\(\\)") regexps)))
78 (cl-some (lambda (x)
79 (funcall (if keep #'string-match-p #'string-match) x string))
80 regexps)))
82 ;;;###autoload
83 (defun dired-diff (file &optional switches)
84 "Compare file at point with FILE using `diff'.
85 If called interactively, prompt for FILE.
86 If the mark is active in Transient Mark mode, use the file at the mark
87 as the default for FILE. (That's the mark set by \\[set-mark-command],
88 not by Dired's \\[dired-mark] command.)
89 If the file at point has a backup file, use that as the default FILE.
90 If the file at point is a backup file, use its original, if that exists
91 and can be found. Note that customizations of `backup-directory-alist'
92 and `make-backup-file-name-function' change where this function searches
93 for the backup file, and affect its ability to find the original of a
94 backup file.
96 FILE is the first argument given to the `diff' function. The file at
97 point is the second argument given to `diff'.
99 With prefix arg, prompt for second argument SWITCHES, which is
100 the string of command switches used as the third argument of `diff'."
101 (interactive
102 (let* ((current (dired-get-filename t))
103 ;; Get the latest existing backup file or its original.
104 (oldf (if (backup-file-name-p current)
105 (file-name-sans-versions current)
106 (diff-latest-backup-file current)))
107 ;; Get the file at the mark.
108 (file-at-mark (if (and transient-mark-mode mark-active)
109 (save-excursion (goto-char (mark t))
110 (dired-get-filename t t))))
111 (separate-dir (and oldf
112 (not (equal (file-name-directory oldf)
113 (dired-current-directory)))))
114 (default-file (or file-at-mark
115 ;; If the file with which to compare
116 ;; doesn't exist, or we cannot intuit it,
117 ;; we forget that name and don't show it
118 ;; as the default, as an indication to the
119 ;; user that she should type the file
120 ;; name.
121 (and (if (and oldf (file-readable-p oldf)) oldf)
122 (if separate-dir
123 oldf
124 (file-name-nondirectory oldf)))))
125 ;; Use it as default if it's not the same as the current file,
126 ;; and the target dir is current or there is a default file.
127 (default (if (and (not (equal default-file current))
128 (or (equal (dired-dwim-target-directory)
129 (dired-current-directory))
130 default-file))
131 default-file))
132 (target-dir (if default
133 (if separate-dir
134 (file-name-directory default)
135 (dired-current-directory))
136 (dired-dwim-target-directory)))
137 (defaults (dired-dwim-target-defaults (list current) target-dir)))
138 (list
139 (minibuffer-with-setup-hook
140 (lambda ()
141 (set (make-local-variable 'minibuffer-default-add-function) nil)
142 (setq minibuffer-default defaults))
143 (read-file-name
144 (format "Diff %s with%s: " current
145 (if default (format " (default %s)" default) ""))
146 target-dir default t))
147 (if current-prefix-arg
148 (read-string "Options for diff: "
149 (if (stringp diff-switches)
150 diff-switches
151 (mapconcat 'identity diff-switches " ")))))))
152 (let ((current (dired-get-filename t)))
153 (when (or (equal (expand-file-name file)
154 (expand-file-name current))
155 (and (file-directory-p file)
156 (equal (expand-file-name current file)
157 (expand-file-name current))))
158 (error "Attempt to compare the file to itself"))
159 (if (and (backup-file-name-p current)
160 (equal file (file-name-sans-versions current)))
161 (diff current file switches)
162 (diff file current switches))))
164 ;;;###autoload
165 (defun dired-backup-diff (&optional switches)
166 "Diff this file with its backup file or vice versa.
167 Uses the latest backup, if there are several numerical backups.
168 If this file is a backup, diff it with its original.
169 The backup file is the first file given to `diff'.
170 With prefix arg, prompt for argument SWITCHES which is options for `diff'."
171 (interactive
172 (if current-prefix-arg
173 (list (read-string "Options for diff: "
174 (if (stringp diff-switches)
175 diff-switches
176 (mapconcat 'identity diff-switches " "))))
177 nil))
178 (diff-backup (dired-get-filename) switches))
180 ;;;###autoload
181 (defun dired-compare-directories (dir2 predicate)
182 "Mark files with different file attributes in two dired buffers.
183 Compare file attributes of files in the current directory
184 with file attributes in directory DIR2 using PREDICATE on pairs of files
185 with the same name. Mark files for which PREDICATE returns non-nil.
186 Mark files with different names if PREDICATE is nil (or interactively
187 with empty input at the predicate prompt).
189 PREDICATE is a Lisp expression that can refer to the following variables:
191 size1, size2 - file size in bytes
192 mtime1, mtime2 - last modification time in seconds, as a float
193 fa1, fa2 - list of file attributes
194 returned by function `file-attributes'
196 where 1 refers to attribute of file in the current dired buffer
197 and 2 to attribute of file in second dired buffer.
199 Examples of PREDICATE:
201 (> mtime1 mtime2) - mark newer files
202 (not (= size1 size2)) - mark files with different sizes
203 (not (string= (nth 8 fa1) (nth 8 fa2))) - mark files with different modes
204 (not (and (= (nth 2 fa1) (nth 2 fa2)) - mark files with different UID
205 (= (nth 3 fa1) (nth 3 fa2)))) and GID."
206 (interactive
207 (list
208 (let* ((target-dir (dired-dwim-target-directory))
209 (defaults (dired-dwim-target-defaults nil target-dir)))
210 (minibuffer-with-setup-hook
211 (lambda ()
212 (set (make-local-variable 'minibuffer-default-add-function) nil)
213 (setq minibuffer-default defaults))
214 (read-directory-name (format "Compare %s with: "
215 (dired-current-directory))
216 target-dir target-dir t)))
217 (read-from-minibuffer "Mark if (lisp expr or RET): " nil nil t nil "nil")))
218 (let* ((dir1 (dired-current-directory))
219 (file-alist1 (dired-files-attributes dir1))
220 (file-alist2 (dired-files-attributes dir2))
221 file-list1 file-list2)
222 (setq file-alist1 (delq (assoc "." file-alist1) file-alist1))
223 (setq file-alist1 (delq (assoc ".." file-alist1) file-alist1))
224 (setq file-alist2 (delq (assoc "." file-alist2) file-alist2))
225 (setq file-alist2 (delq (assoc ".." file-alist2) file-alist2))
226 (setq file-list1 (mapcar
227 'cadr
228 (dired-file-set-difference
229 file-alist1 file-alist2
230 predicate))
231 file-list2 (mapcar
232 'cadr
233 (dired-file-set-difference
234 file-alist2 file-alist1
235 predicate)))
236 (dired-fun-in-all-buffers
237 dir1 nil
238 (lambda ()
239 (dired-mark-if
240 (member (dired-get-filename nil t) file-list1) nil)))
241 (dired-fun-in-all-buffers
242 dir2 nil
243 (lambda ()
244 (dired-mark-if
245 (member (dired-get-filename nil t) file-list2) nil)))
246 (message "Marked in dir1: %s files, in dir2: %s files"
247 (length file-list1)
248 (length file-list2))))
250 (defun dired-file-set-difference (list1 list2 predicate)
251 "Combine LIST1 and LIST2 using a set-difference operation.
252 The result list contains all file items that appear in LIST1 but not LIST2.
253 This is a non-destructive function; it makes a copy of the data if necessary
254 to avoid corrupting the original LIST1 and LIST2.
255 PREDICATE (see `dired-compare-directories') is an additional match
256 condition. Two file items are considered to match if they are equal
257 *and* PREDICATE evaluates to t."
258 (if (or (null list1) (null list2))
259 list1
260 (let (res)
261 (dolist (file1 list1)
262 (unless (let ((list list2))
263 (while (and list
264 (let* ((file2 (car list))
265 (fa1 (car (cddr file1)))
266 (fa2 (car (cddr file2))))
268 (not (equal (car file1) (car file2)))
269 (eval predicate
270 `((fa1 . ,fa1)
271 (fa2 . ,fa2)
272 (size1 . ,(nth 7 fa1))
273 (size2 . ,(nth 7 fa2))
274 (mtime1
275 . ,(float-time (nth 5 fa1)))
276 (mtime2
277 . ,(float-time (nth 5 fa2)))
278 )))))
279 (setq list (cdr list)))
280 list)
281 (push file1 res)))
282 (nreverse res))))
284 (defun dired-files-attributes (dir)
285 "Return a list of all file names and attributes from DIR.
286 List has a form of (file-name full-file-name (attribute-list))."
287 (mapcar
288 (lambda (file-name)
289 (let ((full-file-name (expand-file-name file-name dir)))
290 (list file-name
291 full-file-name
292 (file-attributes full-file-name))))
293 (directory-files dir)))
295 ;;; Change file attributes
297 (defun dired-do-chxxx (attribute-name program op-symbol arg)
298 ;; Change file attributes (group, owner, timestamp) of marked files and
299 ;; refresh their file lines.
300 ;; ATTRIBUTE-NAME is a string describing the attribute to the user.
301 ;; PROGRAM is the program used to change the attribute.
302 ;; OP-SYMBOL is the type of operation (for use in `dired-mark-pop-up').
303 ;; ARG describes which files to use, as in `dired-get-marked-files'.
304 (let* ((files (dired-get-marked-files t arg nil nil t))
305 ;; The source of default file attributes is the file at point.
306 (default-file (dired-get-filename t t))
307 (default (when default-file
308 (cond ((eq op-symbol 'touch)
309 (format-time-string
310 "%Y%m%d%H%M.%S"
311 (nth 5 (file-attributes default-file))))
312 ((eq op-symbol 'chown)
313 (nth 2 (file-attributes default-file 'string)))
314 ((eq op-symbol 'chgrp)
315 (nth 3 (file-attributes default-file 'string))))))
316 (prompt (concat "Change " attribute-name " of %s to"
317 (if (eq op-symbol 'touch)
318 " (default now): "
319 ": ")))
320 (new-attribute (dired-mark-read-string prompt nil op-symbol
321 arg files default
322 (cond ((eq op-symbol 'chown)
323 (system-users))
324 ((eq op-symbol 'chgrp)
325 (system-groups)))))
326 (operation (concat program " " new-attribute))
327 ;; When file-name-coding-system is set to something different
328 ;; from locale-coding-system, leaving the encoding
329 ;; determination to call-process will do the wrong thing,
330 ;; because the arguments in this case are file names, not
331 ;; just some arbitrary text. (This must be bound last, to
332 ;; avoid adverse effects on any of the preceding forms.)
333 (coding-system-for-write (or file-name-coding-system
334 default-file-name-coding-system))
335 failures)
336 (setq failures
337 (dired-bunch-files 10000
338 #'dired-check-process
339 (append
340 (list operation program)
341 (unless (or (string-equal new-attribute "")
342 ;; Use `eq' instead of `equal'
343 ;; to detect empty input (bug#12399).
344 (eq new-attribute default))
345 (if (eq op-symbol 'touch)
346 (list "-t" new-attribute)
347 (list new-attribute)))
348 (if (string-match-p "gnu" system-configuration)
349 '("--") nil))
350 files))
351 (dired-do-redisplay arg);; moves point if ARG is an integer
352 (if failures
353 (dired-log-summary
354 (format "%s: error" operation)
355 nil))))
357 ;;;###autoload
358 (defun dired-do-chmod (&optional arg)
359 "Change the mode of the marked (or next ARG) files.
360 Symbolic modes like `g+w' are allowed.
361 Type M-n to pull the file attributes of the file at point
362 into the minibuffer."
363 (interactive "P")
364 (let* ((files (dired-get-marked-files t arg nil nil t))
365 ;; The source of default file attributes is the file at point.
366 (default-file (dired-get-filename t t))
367 (modestr (when default-file
368 (nth 8 (file-attributes default-file))))
369 (default
370 (and (stringp modestr)
371 (string-match "^.\\(...\\)\\(...\\)\\(...\\)$" modestr)
372 (replace-regexp-in-string
373 "-" ""
374 (format "u=%s,g=%s,o=%s"
375 (match-string 1 modestr)
376 (match-string 2 modestr)
377 (match-string 3 modestr)))))
378 (modes (dired-mark-read-string
379 "Change mode of %s to: "
380 nil 'chmod arg files default))
381 num-modes)
382 (cond ((or (equal modes "")
383 ;; Use `eq' instead of `equal'
384 ;; to detect empty input (bug#12399).
385 (eq modes default))
386 ;; We used to treat empty input as DEFAULT, but that is not
387 ;; such a good idea (Bug#9361).
388 (error "No file mode specified"))
389 ((string-match-p "^[0-7]+" modes)
390 (setq num-modes (string-to-number modes 8))))
392 (dolist (file files)
393 (set-file-modes
394 file
395 (if num-modes num-modes
396 (file-modes-symbolic-to-number modes (file-modes file)))))
397 (dired-do-redisplay arg)))
399 ;;;###autoload
400 (defun dired-do-chgrp (&optional arg)
401 "Change the group of the marked (or next ARG) files.
402 Type M-n to pull the file attributes of the file at point
403 into the minibuffer."
404 (interactive "P")
405 (if (memq system-type '(ms-dos windows-nt))
406 (error "chgrp not supported on this system"))
407 (dired-do-chxxx "Group" "chgrp" 'chgrp arg))
409 ;;;###autoload
410 (defun dired-do-chown (&optional arg)
411 "Change the owner of the marked (or next ARG) files.
412 Type M-n to pull the file attributes of the file at point
413 into the minibuffer."
414 (interactive "P")
415 (if (memq system-type '(ms-dos windows-nt))
416 (error "chown not supported on this system"))
417 (dired-do-chxxx "Owner" dired-chown-program 'chown arg))
419 ;;;###autoload
420 (defun dired-do-touch (&optional arg)
421 "Change the timestamp of the marked (or next ARG) files.
422 This calls touch.
423 Type M-n to pull the file attributes of the file at point
424 into the minibuffer."
425 (interactive "P")
426 (dired-do-chxxx "Timestamp" dired-touch-program 'touch arg))
428 ;; Process all the files in FILES in batches of a convenient size,
429 ;; by means of (FUNCALL FUNCTION ARGS... SOME-FILES...).
430 ;; Batches are chosen to need less than MAX chars for the file names,
431 ;; allowing 3 extra characters of separator per file name.
432 (defun dired-bunch-files (max function args files)
433 (let (pending
434 past
435 (pending-length 0)
436 failures)
437 ;; Accumulate files as long as they fit in MAX chars,
438 ;; then process the ones accumulated so far.
439 (while files
440 (let* ((thisfile (car files))
441 (thislength (+ (length thisfile) 3))
442 (rest (cdr files)))
443 ;; If we have at least 1 pending file
444 ;; and this file won't fit in the length limit, process now.
445 (if (and pending (> (+ thislength pending-length) max))
446 (setq pending (nreverse pending)
447 ;; The elements of PENDING are now in forward order.
448 ;; Do the operation and record failures.
449 failures (nconc (apply function (append args pending))
450 failures)
451 ;; Transfer the elements of PENDING onto PAST
452 ;; and clear it out. Now PAST contains the first N files
453 ;; specified (for some N), and FILES contains the rest.
454 past (nconc past pending)
455 pending nil
456 pending-length 0))
457 ;; Do (setq pending (cons thisfile pending))
458 ;; but reuse the cons that was in `files'.
459 (setcdr files pending)
460 (setq pending files)
461 (setq pending-length (+ thislength pending-length))
462 (setq files rest)))
463 (setq pending (nreverse pending))
464 (prog1
465 (nconc (apply function (append args pending))
466 failures)
467 ;; Now the original list FILES has been put back as it was.
468 (nconc past pending))))
470 (defvar lpr-printer-switch)
472 ;;;###autoload
473 (defun dired-do-print (&optional arg)
474 "Print the marked (or next ARG) files.
475 Uses the shell command coming from variables `lpr-command' and
476 `lpr-switches' as default."
477 (interactive "P")
478 (require 'lpr)
479 (let* ((file-list (dired-get-marked-files t arg nil nil t))
480 (lpr-switches
481 (if (and (stringp printer-name)
482 (string< "" printer-name))
483 (cons (concat lpr-printer-switch printer-name)
484 lpr-switches)
485 lpr-switches))
486 (command (dired-mark-read-string
487 "Print %s with: "
488 (mapconcat 'identity
489 (cons lpr-command
490 (if (stringp lpr-switches)
491 (list lpr-switches)
492 lpr-switches))
493 " ")
494 'print arg file-list)))
495 (dired-run-shell-command (dired-shell-stuff-it command file-list nil))))
497 (defun dired-mark-read-string (prompt initial op-symbol arg files
498 &optional default-value collection)
499 "Read args for a Dired marked-files command, prompting with PROMPT.
500 Return the user input (a string).
502 INITIAL, if non-nil, is the initial minibuffer input.
503 OP-SYMBOL is an operation symbol (see `dired-no-confirm').
504 ARG is normally the prefix argument for the calling command;
505 it is passed as the first argument to `dired-mark-prompt'.
506 FILES should be a list of marked files' names.
508 Optional arg DEFAULT-VALUE is a default value or list of default
509 values, passed as the seventh arg to `completing-read'.
511 Optional arg COLLECTION is a collection of possible completions,
512 passed as the second arg to `completing-read'."
513 (dired-mark-pop-up nil op-symbol files
514 'completing-read
515 (format prompt (dired-mark-prompt arg files))
516 collection nil nil initial nil default-value nil))
518 ;;; Cleaning a directory: flagging some backups for deletion.
520 (defvar dired-file-version-alist)
522 ;;;###autoload
523 (defun dired-clean-directory (keep)
524 "Flag numerical backups for deletion.
525 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
526 Positive prefix arg KEEP overrides `dired-kept-versions';
527 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
529 To clear the flags on these files, you can use \\[dired-flag-backup-files]
530 with a prefix argument."
531 (interactive "P")
532 (setq keep (if keep (prefix-numeric-value keep) dired-kept-versions))
533 (let ((early-retention (if (< keep 0) (- keep) kept-old-versions))
534 (late-retention (if (<= keep 0) dired-kept-versions keep))
535 (dired-file-version-alist ()))
536 (message "Cleaning numerical backups (keeping %d late, %d old)..."
537 late-retention early-retention)
538 ;; Look at each file.
539 ;; If the file has numeric backup versions,
540 ;; put on dired-file-version-alist an element of the form
541 ;; (FILENAME . VERSION-NUMBER-LIST)
542 (dired-map-dired-file-lines #'dired-collect-file-versions)
543 ;; Sort each VERSION-NUMBER-LIST,
544 ;; and remove the versions not to be deleted.
545 (let ((fval dired-file-version-alist))
546 (while fval
547 (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
548 (v-count (length sorted-v-list)))
549 (if (> v-count (+ early-retention late-retention))
550 (rplacd (nthcdr early-retention sorted-v-list)
551 (nthcdr (- v-count late-retention)
552 sorted-v-list)))
553 (rplacd (car fval)
554 (cdr sorted-v-list)))
555 (setq fval (cdr fval))))
556 ;; Look at each file. If it is a numeric backup file,
557 ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
558 (dired-map-dired-file-lines #'dired-trample-file-versions)
559 (message "Cleaning numerical backups...done")))
561 ;;; Subroutines of dired-clean-directory.
563 (defun dired-map-dired-file-lines (fun)
564 ;; Perform FUN with point at the end of each non-directory line.
565 ;; FUN takes one argument, the absolute filename.
566 (save-excursion
567 (let (file buffer-read-only)
568 (goto-char (point-min))
569 (while (not (eobp))
570 (save-excursion
571 (and (not (looking-at-p dired-re-dir))
572 (not (eolp))
573 (setq file (dired-get-filename nil t)) ; nil on non-file
574 (progn (end-of-line)
575 (funcall fun file))))
576 (forward-line 1)))))
578 (defvar backup-extract-version-start) ; used in backup-extract-version
580 (defun dired-collect-file-versions (fn)
581 (let ((fn (file-name-sans-versions fn)))
582 ;; Only do work if this file is not already in the alist.
583 (if (assoc fn dired-file-version-alist)
585 ;; If it looks like file FN has versions, return a list of the versions.
586 ;;That is a list of strings which are file names.
587 ;;The caller may want to flag some of these files for deletion.
588 (let* ((base-versions
589 (concat (file-name-nondirectory fn) ".~"))
590 (backup-extract-version-start (length base-versions))
591 (possibilities (file-name-all-completions
592 base-versions
593 (file-name-directory fn)))
594 (versions (mapcar 'backup-extract-version possibilities)))
595 (if versions
596 (setq dired-file-version-alist
597 (cons (cons fn versions)
598 dired-file-version-alist)))))))
600 (defun dired-trample-file-versions (fn)
601 (let* ((start-vn (string-match-p "\\.~[0-9]+~$" fn))
602 base-version-list)
603 (and start-vn
604 (setq base-version-list ; there was a base version to which
605 (assoc (substring fn 0 start-vn) ; this looks like a
606 dired-file-version-alist)) ; subversion
607 (not (memq (string-to-number (substring fn (+ 2 start-vn)))
608 base-version-list)) ; this one doesn't make the cut
609 (progn (beginning-of-line)
610 (delete-char 1)
611 (insert dired-del-marker)))))
613 ;;; Shell commands
615 (declare-function mailcap-file-default-commands "mailcap" (files))
617 (defun minibuffer-default-add-dired-shell-commands ()
618 "Return a list of all commands associated with current dired files.
619 This function is used to add all related commands retrieved by `mailcap'
620 to the end of the list of defaults just after the default value."
621 (interactive)
622 (let* ((files minibuffer-completion-table)
623 (commands (and (require 'mailcap nil t)
624 (mailcap-file-default-commands files))))
625 (if (listp minibuffer-default)
626 (append minibuffer-default commands)
627 (cons minibuffer-default commands))))
629 ;; This is an extra function so that you can redefine it, e.g., to use gmhist.
630 (defun dired-read-shell-command (prompt arg files)
631 "Read a dired shell command.
632 PROMPT should be a format string with one \"%s\" format sequence,
633 which is replaced by the value returned by `dired-mark-prompt',
634 with ARG and FILES as its arguments. FILES should be a list of
635 file names. The result is used as the prompt.
637 This normally reads using `read-shell-command', but if the
638 `dired-x' package is loaded, use `dired-guess-shell-command' to
639 offer a smarter default choice of shell command."
640 (minibuffer-with-setup-hook
641 (lambda ()
642 (set (make-local-variable 'minibuffer-completion-table) files)
643 (set (make-local-variable 'minibuffer-default-add-function)
644 'minibuffer-default-add-dired-shell-commands))
645 (setq prompt (format prompt (dired-mark-prompt arg files)))
646 (if (functionp 'dired-guess-shell-command)
647 (dired-mark-pop-up nil 'shell files
648 'dired-guess-shell-command prompt files)
649 (dired-mark-pop-up nil 'shell files
650 'read-shell-command prompt nil nil))))
652 ;;;###autoload
653 (defun dired-do-async-shell-command (command &optional arg file-list)
654 "Run a shell command COMMAND on the marked files asynchronously.
656 Like `dired-do-shell-command', but adds `&' at the end of COMMAND
657 to execute it asynchronously.
659 When operating on multiple files, asynchronous commands
660 are executed in the background on each file in parallel.
661 In shell syntax this means separating the individual commands
662 with `&'. However, when COMMAND ends in `;' or `;&' then commands
663 are executed in the background on each file sequentially waiting
664 for each command to terminate before running the next command.
665 In shell syntax this means separating the individual commands with `;'.
667 The output appears in the buffer `*Async Shell Command*'."
668 (interactive
669 (let ((files (dired-get-marked-files t current-prefix-arg nil nil t)))
670 (list
671 ;; Want to give feedback whether this file or marked files are used:
672 (dired-read-shell-command "& on %s: " current-prefix-arg files)
673 current-prefix-arg
674 files)))
675 (unless (string-match-p "&[ \t]*\\'" command)
676 (setq command (concat command " &")))
677 (dired-do-shell-command command arg file-list))
679 ;;;###autoload
680 (defun dired-do-shell-command (command &optional arg file-list)
681 "Run a shell command COMMAND on the marked files.
682 If no files are marked or a numeric prefix arg is given,
683 the next ARG files are used. Just \\[universal-argument] means the current file.
684 The prompt mentions the file(s) or the marker, as appropriate.
686 If there is a `*' in COMMAND, surrounded by whitespace, this runs
687 COMMAND just once with the entire file list substituted there.
689 If there is no `*', but there is a `?' in COMMAND, surrounded by
690 whitespace, or a `\\=`?\\=`' this runs COMMAND on each file
691 individually with the file name substituted for `?' or `\\=`?\\=`'.
693 Otherwise, this runs COMMAND on each file individually with the
694 file name added at the end of COMMAND (separated by a space).
696 `*' and `?' when not surrounded by whitespace nor `\\=`' have no special
697 significance for `dired-do-shell-command', and are passed through
698 normally to the shell, but you must confirm first.
700 If you want to use `*' as a shell wildcard with whitespace around
701 it, write `*\"\"' in place of just `*'. This is equivalent to just
702 `*' in the shell, but avoids Dired's special handling.
704 If COMMAND ends in `&', `;', or `;&', it is executed in the
705 background asynchronously, and the output appears in the buffer
706 `*Async Shell Command*'. When operating on multiple files and COMMAND
707 ends in `&', the shell command is executed on each file in parallel.
708 However, when COMMAND ends in `;' or `;&' then commands are executed
709 in the background on each file sequentially waiting for each command
710 to terminate before running the next command. You can also use
711 `dired-do-async-shell-command' that automatically adds `&'.
713 Otherwise, COMMAND is executed synchronously, and the output
714 appears in the buffer `*Shell Command Output*'.
716 This feature does not try to redisplay Dired buffers afterward, as
717 there's no telling what files COMMAND may have changed.
718 Type \\[dired-do-redisplay] to redisplay the marked files.
720 When COMMAND runs, its working directory is the top-level directory
721 of the Dired buffer, so output files usually are created there
722 instead of in a subdir.
724 In a noninteractive call (from Lisp code), you must specify
725 the list of file names explicitly with the FILE-LIST argument, which
726 can be produced by `dired-get-marked-files', for example."
727 ;;Functions dired-run-shell-command and dired-shell-stuff-it do the
728 ;;actual work and can be redefined for customization.
729 (interactive
730 (let ((files (dired-get-marked-files t current-prefix-arg nil nil t)))
731 (list
732 ;; Want to give feedback whether this file or marked files are used:
733 (dired-read-shell-command "! on %s: " current-prefix-arg files)
734 current-prefix-arg
735 files)))
736 (cl-flet ((need-confirm-p
737 (cmd str)
738 (let ((res cmd)
739 (regexp (regexp-quote str)))
740 ;; Drop all ? and * surrounded by spaces and `?`.
741 (while (and (string-match regexp res)
742 (dired--star-or-qmark-p res str))
743 (setq res (replace-match "" t t res 2)))
744 (string-match regexp res))))
745 (let* ((on-each (not (dired--star-or-qmark-p command "*" 'keep)))
746 (no-subst (not (dired--star-or-qmark-p command "?" 'keep)))
747 ;; Get confirmation for wildcards that may have been meant
748 ;; to control substitution of a file name or the file name list.
749 (ok (cond ((not (or on-each no-subst))
750 (error "You can not combine `*' and `?' substitution marks"))
751 ((need-confirm-p command "*")
752 (y-or-n-p (format-message
753 "Confirm--do you mean to use `*' as a wildcard? ")))
754 ((need-confirm-p command "?")
755 (y-or-n-p (format-message
756 "Confirm--do you mean to use `?' as a wildcard? ")))
757 (t))))
758 (when ok
759 (if on-each
760 (dired-bunch-files (- 10000 (length command))
761 (lambda (&rest files)
762 (dired-run-shell-command
763 (dired-shell-stuff-it command files t arg)))
764 nil file-list)
765 ;; execute the shell command
766 (dired-run-shell-command
767 (dired-shell-stuff-it command file-list nil arg)))))))
769 ;; Might use {,} for bash or csh:
770 (defvar dired-mark-prefix ""
771 "Prepended to marked files in dired shell commands.")
772 (defvar dired-mark-postfix ""
773 "Appended to marked files in dired shell commands.")
774 (defvar dired-mark-separator " "
775 "Separates marked files in dired shell commands.")
777 (defun dired-shell-stuff-it (command file-list on-each &optional _raw-arg)
778 ;; "Make up a shell command line from COMMAND and FILE-LIST.
779 ;; If ON-EACH is t, COMMAND should be applied to each file, else
780 ;; simply concat all files and apply COMMAND to this.
781 ;; FILE-LIST's elements will be quoted for the shell."
782 ;; Might be redefined for smarter things and could then use RAW-ARG
783 ;; (coming from interactive P and currently ignored) to decide what to do.
784 ;; Smart would be a way to access basename or extension of file names.
785 (let* ((in-background (string-match "[ \t]*&[ \t]*\\'" command))
786 (command (if in-background
787 (substring command 0 (match-beginning 0))
788 command))
789 (sequentially (string-match "[ \t]*;[ \t]*\\'" command))
790 (command (if sequentially
791 (substring command 0 (match-beginning 0))
792 command))
793 (parallel-in-background
794 (and in-background (not sequentially) (not (eq system-type 'ms-dos))))
795 (w32-shell (and (fboundp 'w32-shell-dos-semantics)
796 (w32-shell-dos-semantics)))
797 (file-remote (file-remote-p default-directory))
798 ;; The way to run a command in background in Windows shells
799 ;; is to use the START command. The /B switch means not to
800 ;; create a new window for the command.
801 (cmd-prefix (if (and w32-shell (not file-remote)) "start /b " ""))
802 ;; Windows shells don't support chaining with ";", they use
803 ;; "&" instead.
804 (cmd-sep (if (and (or (not w32-shell) file-remote)
805 (not parallel-in-background))
806 ";" "&"))
807 (stuff-it
808 (if (dired--star-or-qmark-p command nil 'keep)
809 (lambda (x)
810 (let ((retval (concat cmd-prefix command)))
811 (while (dired--star-or-qmark-p retval nil)
812 (setq retval (replace-match x t t retval 2)))
813 retval))
814 (lambda (x) (concat cmd-prefix command dired-mark-separator x)))))
815 (concat
816 (cond (on-each
817 (format "%s%s"
818 (mapconcat stuff-it (mapcar 'shell-quote-argument file-list)
819 cmd-sep)
820 ;; POSIX shells running a list of commands in the background
821 ;; (LIST = cmd_1 & [cmd_2 & ... cmd_i & ... cmd_N &])
822 ;; return once cmd_N ends, i.e., the shell does not
823 ;; wait for cmd_i to finish before executing cmd_i+1.
824 ;; That means, running (shell-command LIST) may not show
825 ;; the output of all the commands (Bug#23206).
826 ;; Add 'wait' to force those POSIX shells to wait until
827 ;; all commands finish.
828 (or (and parallel-in-background (not w32-shell)
829 "&wait")
830 "")))
832 (let ((files (mapconcat 'shell-quote-argument
833 file-list dired-mark-separator)))
834 (when (cdr file-list)
835 (setq files (concat dired-mark-prefix files dired-mark-postfix)))
836 (funcall stuff-it files))))
837 (or (and in-background "&") ""))))
839 ;; This is an extra function so that it can be redefined by ange-ftp.
840 ;;;###autoload
841 (defun dired-run-shell-command (command)
842 (let ((handler
843 (find-file-name-handler (directory-file-name default-directory)
844 'shell-command)))
845 (if handler (apply handler 'shell-command (list command))
846 (shell-command command)))
847 ;; Return nil for sake of nconc in dired-bunch-files.
848 nil)
851 (defun dired-check-process (msg program &rest arguments)
852 "Display MSG while running PROGRAM, and check for output.
853 Remaining arguments are strings passed as command arguments to PROGRAM.
854 On error, insert output
855 in a log buffer and return the offending ARGUMENTS or PROGRAM.
856 Caller can cons up a list of failed args.
857 Else returns nil for success."
858 (let (err-buffer err (dir default-directory))
859 (message "%s..." msg)
860 (save-excursion
861 ;; Get a clean buffer for error output:
862 (setq err-buffer (get-buffer-create " *dired-check-process output*"))
863 (set-buffer err-buffer)
864 (erase-buffer)
865 (setq default-directory dir ; caller's default-directory
866 err (not (eq 0 (apply 'process-file program nil t nil arguments))))
867 (if err
868 (progn
869 (dired-log (concat program " " (prin1-to-string arguments) "\n"))
870 (dired-log err-buffer)
871 (or arguments program t))
872 (kill-buffer err-buffer)
873 (message "%s...done" msg)
874 nil))))
876 (defun dired-shell-command (cmd)
877 "Run CMD, and check for output.
878 On error, pop up the log buffer.
879 Return the result of `process-file' - zero for success."
880 (let ((out-buffer " *dired-check-process output*")
881 (dir default-directory))
882 (with-current-buffer (get-buffer-create out-buffer)
883 (erase-buffer)
884 (let* ((default-directory dir)
885 (res (process-file
886 shell-file-name
890 shell-command-switch
891 cmd)))
892 (unless (zerop res)
893 (pop-to-buffer out-buffer))
894 res))))
896 ;; Commands that delete or redisplay part of the dired buffer.
898 (defun dired-kill-line (&optional arg)
899 "Kill the current line (not the files).
900 With a prefix argument, kill that many lines starting with the current line.
901 \(A negative argument kills backward.)"
902 (interactive "P")
903 (setq arg (prefix-numeric-value arg))
904 (let (buffer-read-only file)
905 (while (/= 0 arg)
906 (setq file (dired-get-filename nil t))
907 (if (not file)
908 (error "Can only kill file lines")
909 (save-excursion (and file
910 (dired-goto-subdir file)
911 (dired-kill-subdir)))
912 (delete-region (line-beginning-position)
913 (progn (forward-line 1) (point)))
914 (if (> arg 0)
915 (setq arg (1- arg))
916 (setq arg (1+ arg))
917 (forward-line -1))))
918 (dired-move-to-filename)))
920 ;;;###autoload
921 (defun dired-do-kill-lines (&optional arg fmt)
922 "Kill all marked lines (not the files).
923 With a prefix argument, kill that many lines starting with the current line.
924 \(A negative argument kills backward.)
925 If you use this command with a prefix argument to kill the line
926 for a file that is a directory, which you have inserted in the
927 Dired buffer as a subdirectory, then it deletes that subdirectory
928 from the buffer as well.
929 To kill an entire subdirectory \(without killing its line in the
930 parent directory), go to its directory header line and use this
931 command with a prefix argument (the value does not matter)."
932 ;; Returns count of killed lines. FMT="" suppresses message.
933 (interactive "P")
934 (if arg
935 (if (dired-get-subdir)
936 (dired-kill-subdir)
937 (dired-kill-line arg))
938 (save-excursion
939 (goto-char (point-min))
940 (let (buffer-read-only
941 (count 0)
942 (regexp (dired-marker-regexp)))
943 (while (and (not (eobp))
944 (re-search-forward regexp nil t))
945 (setq count (1+ count))
946 (delete-region (line-beginning-position)
947 (progn (forward-line 1) (point))))
948 (or (equal "" fmt)
949 (message (or fmt "Killed %d line%s.") count (dired-plural-s count)))
950 count))))
952 ;;;###end dired-cmd.el
954 ;;; 30K
955 ;;;###begin dired-cp.el
957 (defun dired-compress ()
958 ;; Compress or uncompress the current file.
959 ;; Return nil for success, offending filename else.
960 (let* (buffer-read-only
961 (from-file (dired-get-filename))
962 (new-file (dired-compress-file from-file)))
963 (if new-file
964 (let ((start (point)))
965 ;; Remove any preexisting entry for the name NEW-FILE.
966 (ignore-errors (dired-remove-entry new-file))
967 (goto-char start)
968 ;; Now replace the current line with an entry for NEW-FILE.
969 (dired-update-file-line new-file) nil)
970 (dired-log (concat "Failed to compress" from-file))
971 from-file)))
973 (defvar dired-compress-file-suffixes
975 ;; "tar -zxf" isn't used because it's not available on the
976 ;; Solaris10 version of tar. Solaris10 becomes obsolete in 2021.
977 ;; Same thing on AIX 7.1.
978 ("\\.tar\\.gz\\'" "" "gzip -dc %i | tar -xf -")
979 ("\\.tgz\\'" "" "gzip -dc %i | tar -xf -")
980 ("\\.gz\\'" "" "gunzip")
981 ("\\.Z\\'" "" "uncompress")
982 ;; For .z, try gunzip. It might be an old gzip file,
983 ;; or it might be from compact? pack? (which?) but gunzip handles both.
984 ("\\.z\\'" "" "gunzip")
985 ("\\.dz\\'" "" "dictunzip")
986 ("\\.tbz\\'" ".tar" "bunzip2")
987 ("\\.bz2\\'" "" "bunzip2")
988 ("\\.xz\\'" "" "unxz")
989 ("\\.zip\\'" "" "unzip -o -d %o %i")
990 ("\\.7z\\'" "" "7z x -aoa -o%o %i")
991 ;; This item controls naming for compression.
992 ("\\.tar\\'" ".tgz" nil)
993 ;; This item controls the compression of directories
994 (":" ".tar.gz" "tar -cf - %i | gzip -c9 > %o"))
995 "Control changes in file name suffixes for compression and uncompression.
996 Each element specifies one transformation rule, and has the form:
997 (REGEXP NEW-SUFFIX PROGRAM)
998 The rule applies when the old file name matches REGEXP.
999 The new file name is computed by deleting the part that matches REGEXP
1000 (as well as anything after that), then adding NEW-SUFFIX in its place.
1001 If PROGRAM is non-nil, the rule is an uncompression rule,
1002 and uncompression is done by running PROGRAM.
1004 Within PROGRAM, %i denotes the input file, and %o denotes the
1005 output file.
1007 Otherwise, the rule is a compression rule, and compression is done with gzip.
1008 ARGS are command switches passed to PROGRAM.")
1010 (defvar dired-compress-files-alist
1011 '(("\\.tar\\.gz\\'" . "tar -cf - %i | gzip -c9 > %o")
1012 ("\\.tar\\.bz2\\'" . "tar -cf - %i | bzip2 -c9 > %o")
1013 ("\\.tar\\.xz\\'" . "tar -cf - %i | xz -c9 > %o")
1014 ("\\.zip\\'" . "zip %o -r --filesync %i"))
1015 "Control the compression shell command for `dired-do-compress-to'.
1017 Each element is (REGEXP . CMD), where REGEXP is the name of the
1018 archive to which you want to compress, and CMD is the
1019 corresponding command.
1021 Within CMD, %i denotes the input file(s), and %o denotes the
1022 output file. %i path(s) are relative, while %o is absolute.")
1024 (declare-function format-spec "format-spec.el" (format specification))
1026 ;;;###autoload
1027 (defun dired-do-compress-to ()
1028 "Compress selected files and directories to an archive.
1029 Prompt for the archive file name.
1030 Choose the archiving command based on the archive file-name extension
1031 and `dired-compress-files-alist'."
1032 (interactive)
1033 (let* ((in-files (dired-get-marked-files nil nil nil nil t))
1034 (out-file (expand-file-name (read-file-name "Compress to: ")))
1035 (rule (cl-find-if
1036 (lambda (x)
1037 (string-match (car x) out-file))
1038 dired-compress-files-alist)))
1039 (cond ((not rule)
1040 (error
1041 "No compression rule found for %s, see `dired-compress-files-alist'"
1042 out-file))
1043 ((and (file-exists-p out-file)
1044 (not (y-or-n-p
1045 (format "%s exists, overwrite?"
1046 (abbreviate-file-name out-file)))))
1047 (message "Compression aborted"))
1049 (when (zerop
1050 (dired-shell-command
1051 (format-spec (cdr rule)
1052 `((?\o . ,(shell-quote-argument out-file))
1053 (?\i . ,(mapconcat
1054 (lambda (file-desc)
1055 (shell-quote-argument (file-name-nondirectory
1056 file-desc)))
1057 in-files " "))))))
1058 (message "Compressed %d file(s) to %s"
1059 (length in-files)
1060 (file-name-nondirectory out-file)))))))
1062 ;;;###autoload
1063 (defun dired-compress-file (file)
1064 "Compress or uncompress FILE.
1065 Return the name of the compressed or uncompressed file.
1066 Return nil if no change in files."
1067 (let ((handler (find-file-name-handler file 'dired-compress-file))
1068 suffix newname
1069 (suffixes dired-compress-file-suffixes)
1070 command)
1071 ;; See if any suffix rule matches this file name.
1072 (while suffixes
1073 (let (case-fold-search)
1074 (if (string-match (car (car suffixes)) file)
1075 (setq suffix (car suffixes) suffixes nil))
1076 (setq suffixes (cdr suffixes))))
1077 ;; If so, compute desired new name.
1078 (if suffix
1079 (setq newname (concat (substring file 0 (match-beginning 0))
1080 (nth 1 suffix))))
1081 (cond (handler
1082 (funcall handler 'dired-compress-file file))
1083 ((file-symlink-p file)
1084 nil)
1085 ((and suffix (setq command (nth 2 suffix)))
1086 (if (string-match "%[io]" command)
1087 (prog1 (setq newname (file-name-as-directory newname))
1088 (dired-shell-command
1089 (replace-regexp-in-string
1090 "%o" (shell-quote-argument newname)
1091 (replace-regexp-in-string
1092 "%i" (shell-quote-argument file)
1093 command
1094 nil t)
1095 nil t)))
1096 ;; We found an uncompression rule.
1097 (when (not
1098 (dired-check-process
1099 (concat "Uncompressing " file)
1100 command
1101 file))
1102 newname)))
1104 ;; We don't recognize the file as compressed, so compress it.
1105 ;; Try gzip; if we don't have that, use compress.
1106 (condition-case nil
1107 (if (file-directory-p file)
1108 (progn
1109 (setq suffix (cdr (assoc ":" dired-compress-file-suffixes)))
1110 (when suffix
1111 (let ((out-name (concat file (car suffix)))
1112 (default-directory (file-name-directory file)))
1113 (dired-shell-command
1114 (replace-regexp-in-string
1115 "%o" (shell-quote-argument out-name)
1116 (replace-regexp-in-string
1117 "%i" (shell-quote-argument (file-name-nondirectory file))
1118 (cadr suffix)
1119 nil t)
1120 nil t))
1121 out-name)))
1122 (let ((out-name (concat file ".gz")))
1123 (and (or (not (file-exists-p out-name))
1124 (y-or-n-p
1125 (format "File %s already exists. Really compress? "
1126 out-name)))
1127 (not
1128 (dired-check-process (concat "Compressing " file)
1129 "gzip" "-f" file))
1130 (or (file-exists-p out-name)
1131 (setq out-name (concat file ".z")))
1132 ;; Rename the compressed file to NEWNAME
1133 ;; if it hasn't got that name already.
1134 (if (and newname (not (equal newname out-name)))
1135 (progn
1136 (rename-file out-name newname t)
1137 newname)
1138 out-name))))
1139 (file-error
1140 (if (not (dired-check-process (concat "Compressing " file)
1141 "compress" "-f" file))
1142 ;; Don't use NEWNAME with `compress'.
1143 (concat file ".Z"))))))))
1145 (defun dired-mark-confirm (op-symbol arg)
1146 ;; Request confirmation from the user that the operation described
1147 ;; by OP-SYMBOL is to be performed on the marked files.
1148 ;; Confirmation consists in a y-or-n question with a file list
1149 ;; pop-up unless OP-SYMBOL is a member of `dired-no-confirm'.
1150 ;; The files used are determined by ARG (as in dired-get-marked-files).
1151 (or (eq dired-no-confirm t)
1152 (memq op-symbol dired-no-confirm)
1153 ;; Pass t for DISTINGUISH-ONE-MARKED so that a single file which
1154 ;; is marked pops up a window. That will help the user see
1155 ;; it isn't the current line file.
1156 (let ((files (dired-get-marked-files t arg nil t t))
1157 (string (if (eq op-symbol 'compress) "Compress or uncompress"
1158 (capitalize (symbol-name op-symbol)))))
1159 (dired-mark-pop-up nil op-symbol files #'y-or-n-p
1160 (concat string " "
1161 (dired-mark-prompt arg files) "? ")))))
1163 (defun dired-map-over-marks-check (fun arg op-symbol &optional show-progress)
1164 ; "Map FUN over marked files (with second ARG like in dired-map-over-marks)
1165 ; and display failures.
1167 ; FUN takes zero args. It returns non-nil (the offending object, e.g.
1168 ; the short form of the filename) for a failure and probably logs a
1169 ; detailed error explanation using function `dired-log'.
1171 ; OP-SYMBOL is a symbol describing the operation performed (e.g.
1172 ; `compress'). It is used with `dired-mark-pop-up' to prompt the user
1173 ; (e.g. with `Compress * [2 files]? ') and to display errors (e.g.
1174 ; `Failed to compress 1 of 2 files - type W to see why ("foo")')
1176 ; SHOW-PROGRESS if non-nil means redisplay dired after each file."
1177 (if (dired-mark-confirm op-symbol arg)
1178 (let* ((total-list;; all of FUN's return values
1179 (dired-map-over-marks (funcall fun) arg show-progress))
1180 (total (length total-list))
1181 (failures (delq nil total-list))
1182 (count (length failures))
1183 (string (if (eq op-symbol 'compress) "Compress or uncompress"
1184 (capitalize (symbol-name op-symbol)))))
1185 (if (not failures)
1186 (message "%s: %d file%s."
1187 string total (dired-plural-s total))
1188 ;; end this bunch of errors:
1189 (dired-log-summary
1190 (format "Failed to %s %d of %d file%s"
1191 (downcase string) count total (dired-plural-s total))
1192 failures)))))
1194 ;;;###autoload
1195 (defun dired-query (sym prompt &rest args)
1196 "Format PROMPT with ARGS, query user, and store the result in SYM.
1197 The return value is either nil or t.
1199 The user may type y or SPC to accept once; n or DEL to skip once;
1200 ! to accept this and subsequent queries; or q or ESC to decline
1201 this and subsequent queries.
1203 If SYM is already bound to a non-nil value, this function may
1204 return automatically without querying the user. If SYM is !,
1205 return t; if SYM is q or ESC, return nil."
1206 (let* ((char (symbol-value sym))
1207 (char-choices '(?y ?\s ?n ?\177 ?! ?q ?\e)))
1208 (cond ((eq char ?!)
1209 t) ; accept, and don't ask again
1210 ((memq char '(?q ?\e))
1211 nil) ; skip, and don't ask again
1212 (t ; no previous answer - ask now
1213 (setq prompt
1214 (concat (apply #'format-message prompt args)
1215 (if help-form
1216 (format " [Type yn!q or %s] "
1217 (key-description (vector help-char)))
1218 " [Type y, n, q or !] ")))
1219 (set sym (setq char (read-char-choice prompt char-choices)))
1220 (if (memq char '(?y ?\s ?!)) t)))))
1223 ;;;###autoload
1224 (defun dired-do-compress (&optional arg)
1225 "Compress or uncompress marked (or next ARG) files."
1226 (interactive "P")
1227 (dired-map-over-marks-check #'dired-compress arg 'compress t))
1229 ;; Commands for Emacs Lisp files - load and byte compile
1231 (defun dired-byte-compile ()
1232 ;; Return nil for success, offending file name else.
1233 (let* ((filename (dired-get-filename))
1234 elc-file buffer-read-only failure)
1235 (condition-case err
1236 (save-excursion (byte-compile-file filename))
1237 (error
1238 (setq failure err)))
1239 (setq elc-file (byte-compile-dest-file filename))
1240 (or (file-exists-p elc-file)
1241 (setq failure t))
1242 (if failure
1243 (progn
1244 (dired-log "Byte compile error for %s:\n%s\n" filename failure)
1245 (dired-make-relative filename))
1246 (dired-remove-file elc-file)
1247 (forward-line) ; insert .elc after its .el file
1248 (dired-add-file elc-file)
1249 nil)))
1251 ;;;###autoload
1252 (defun dired-do-byte-compile (&optional arg)
1253 "Byte compile marked (or next ARG) Emacs Lisp files."
1254 (interactive "P")
1255 (dired-map-over-marks-check #'dired-byte-compile arg 'byte-compile t))
1257 (defun dired-load ()
1258 ;; Return nil for success, offending file name else.
1259 (let ((file (dired-get-filename)) failure)
1260 (condition-case err
1261 (load file nil nil t)
1262 (error (setq failure err)))
1263 (if (not failure)
1265 (dired-log "Load error for %s:\n%s\n" file failure)
1266 (dired-make-relative file))))
1268 ;;;###autoload
1269 (defun dired-do-load (&optional arg)
1270 "Load the marked (or next ARG) Emacs Lisp files."
1271 (interactive "P")
1272 (dired-map-over-marks-check #'dired-load arg 'load t))
1274 ;;;###autoload
1275 (defun dired-do-redisplay (&optional arg test-for-subdir)
1276 "Redisplay all marked (or next ARG) files.
1277 If on a subdir line, redisplay that subdirectory. In that case,
1278 a prefix arg lets you edit the `ls' switches used for the new listing.
1280 Dired remembers switches specified with a prefix arg, so that reverting
1281 the buffer will not reset them. However, using `dired-undo' to re-insert
1282 or delete subdirectories can bypass this machinery. Hence, you sometimes
1283 may have to reset some subdirectory switches after a `dired-undo'.
1284 You can reset all subdirectory switches to the default using
1285 \\<dired-mode-map>\\[dired-reset-subdir-switches].
1286 See Info node `(emacs)Subdir switches' for more details."
1287 ;; Moves point if the next ARG files are redisplayed.
1288 (interactive "P\np")
1289 (if (and test-for-subdir (dired-get-subdir))
1290 (let* ((dir (dired-get-subdir))
1291 (switches (cdr (assoc-string dir dired-switches-alist))))
1292 (dired-insert-subdir
1294 (when arg
1295 (read-string "Switches for listing: "
1296 (or switches
1297 dired-subdir-switches
1298 dired-actual-switches)))))
1299 (message "Redisplaying...")
1300 ;; message much faster than making dired-map-over-marks show progress
1301 (dired-uncache
1302 (if (consp dired-directory) (car dired-directory) dired-directory))
1303 (dired-map-over-marks (let ((fname (dired-get-filename nil t))
1304 ;; Postpone readin hook till we map
1305 ;; over all marked files (Bug#6810).
1306 (dired-after-readin-hook nil))
1307 (if (not fname)
1308 (error "No file on this line")
1309 (message "Redisplaying... %s" fname)
1310 (dired-update-file-line fname)))
1311 arg)
1312 (run-hooks 'dired-after-readin-hook)
1313 (dired-move-to-filename)
1314 (message "Redisplaying...done")))
1316 (defun dired-reset-subdir-switches ()
1317 "Set `dired-switches-alist' to nil and revert dired buffer."
1318 (interactive)
1319 (setq dired-switches-alist nil)
1320 (revert-buffer))
1322 (defun dired-update-file-line (file)
1323 ;; Delete the current line, and insert an entry for FILE.
1324 ;; If FILE is nil, then just delete the current line.
1325 ;; Keeps any marks that may be present in column one (doing this
1326 ;; here is faster than with dired-add-entry's optional arg).
1327 ;; Does not update other dired buffers. Use dired-relist-entry for that.
1328 (let* ((opoint (line-beginning-position))
1329 (char (char-after opoint))
1330 (buffer-read-only))
1331 (delete-region opoint (progn (forward-line 1) (point)))
1332 (if file
1333 (progn
1334 (dired-add-entry file nil t)
1335 ;; Replace space by old marker without moving point.
1336 ;; Faster than goto+insdel inside a save-excursion?
1337 (when char
1338 (subst-char-in-region opoint (1+ opoint) ?\040 char)))))
1339 (dired-move-to-filename))
1341 ;;;###autoload
1342 (defun dired-add-file (filename &optional marker-char)
1343 (dired-fun-in-all-buffers
1344 (file-name-directory filename) (file-name-nondirectory filename)
1345 #'dired-add-entry filename marker-char))
1347 (defvar dired-omit-mode)
1348 (declare-function dired-omit-regexp "dired-x" ())
1349 (defvar dired-omit-localp)
1351 (defun dired-add-entry (filename &optional marker-char relative)
1352 "Add a new dired entry for FILENAME.
1353 Optionally mark it with MARKER-CHAR (a character, else uses
1354 `dired-marker-char'). Note that this adds the entry `out of order'
1355 if files are sorted by time, etc.
1356 Skips files that match `dired-trivial-filenames'.
1357 Exposes hidden subdirectories if a file is added there.
1359 If `dired-x' is loaded and `dired-omit-mode' is enabled, skips
1360 files matching `dired-omit-regexp'."
1361 (if (or (not (featurep 'dired-x))
1362 (not dired-omit-mode)
1363 ;; Avoid calling ls for files that are going to be omitted anyway.
1364 (let ((omit-re (dired-omit-regexp)))
1365 (or (string= omit-re "")
1366 (not (string-match-p omit-re
1367 (cond
1368 ((eq 'no-dir dired-omit-localp)
1369 filename)
1370 ((eq t dired-omit-localp)
1371 (dired-make-relative filename))
1373 (dired-make-absolute
1374 filename
1375 (file-name-directory filename)))))))))
1376 ;; Do it!
1377 (progn
1378 (setq filename (directory-file-name filename))
1379 ;; Entry is always for files, even if they happen to also be directories
1380 (let* ((opoint (point))
1381 (cur-dir (dired-current-directory))
1382 (directory (if relative cur-dir (file-name-directory filename)))
1383 reason)
1384 (setq filename
1385 (if relative
1386 (file-relative-name filename directory)
1387 (file-name-nondirectory filename))
1388 reason
1389 (catch 'not-found
1390 (if (string= directory cur-dir)
1391 (progn
1392 (skip-chars-forward "^\r\n")
1393 (if (eq (following-char) ?\r)
1394 (dired-unhide-subdir))
1395 ;; We are already where we should be, except when
1396 ;; point is before the subdir line or its total line.
1397 (let ((p (dired-after-subdir-garbage cur-dir)))
1398 (if (< (point) p)
1399 (goto-char p))))
1400 ;; else try to find correct place to insert
1401 (if (dired-goto-subdir directory)
1402 (progn ;; unhide if necessary
1403 (if (= (following-char) ?\r)
1404 ;; Point is at end of subdir line.
1405 (dired-unhide-subdir))
1406 ;; found - skip subdir and `total' line
1407 ;; and uninteresting files like . and ..
1408 ;; This better not move into the next subdir!
1409 (dired-goto-next-nontrivial-file))
1410 ;; not found
1411 (throw 'not-found "Subdir not found")))
1412 (let (buffer-read-only opoint)
1413 (beginning-of-line)
1414 (setq opoint (point))
1415 ;; Don't expand `.'.
1416 ;; Show just the file name within directory.
1417 (let ((default-directory directory))
1418 (dired-insert-directory
1419 directory
1420 (concat dired-actual-switches " -d")
1421 (list filename)))
1422 (goto-char opoint)
1423 ;; Put in desired marker char.
1424 (when marker-char
1425 (let ((dired-marker-char
1426 (if (integerp marker-char) marker-char
1427 dired-marker-char)))
1428 (dired-mark nil)))
1429 ;; Compensate for a bug in ange-ftp.
1430 ;; It inserts the file's absolute name, rather than
1431 ;; the relative one. That may be hard to fix since it
1432 ;; is probably controlled by something in ftp.
1433 (goto-char opoint)
1434 (let ((inserted-name (dired-get-filename 'verbatim)))
1435 (if (file-name-directory inserted-name)
1436 (let (props)
1437 (end-of-line)
1438 (forward-char (- (length inserted-name)))
1439 (setq props (text-properties-at (point)))
1440 (delete-char (length inserted-name))
1441 (let ((pt (point)))
1442 (insert filename)
1443 (set-text-properties pt (point) props))
1444 (forward-char 1))
1445 (forward-line 1)))
1446 (forward-line -1)
1447 (if dired-after-readin-hook
1448 ;; The subdir-alist is not affected...
1449 (save-excursion ; ...so we can run it right now:
1450 (save-restriction
1451 (beginning-of-line)
1452 (narrow-to-region (point)
1453 (line-beginning-position 2))
1454 (run-hooks 'dired-after-readin-hook))))
1455 (dired-move-to-filename))
1456 ;; return nil if all went well
1457 nil))
1458 (if reason ; don't move away on failure
1459 (goto-char opoint))
1460 (not reason))) ; return t on success, nil else
1461 ;; Don't do it (dired-omit-mode).
1462 ;; Return t for success (perhaps we should return file-exists-p).
1465 (defun dired-after-subdir-garbage (dir)
1466 ;; Return pos of first file line of DIR, skipping header and total
1467 ;; or wildcard lines.
1468 ;; Important: never moves into the next subdir.
1469 ;; DIR is assumed to be unhidden.
1470 (save-excursion
1471 (or (dired-goto-subdir dir) (error "This cannot happen"))
1472 (forward-line 1)
1473 (while (and (not (eolp)) ; don't cross subdir boundary
1474 (not (dired-move-to-filename)))
1475 (forward-line 1))
1476 (point)))
1478 ;;;###autoload
1479 (defun dired-remove-file (file)
1480 (dired-fun-in-all-buffers
1481 (file-name-directory file) (file-name-nondirectory file)
1482 #'dired-remove-entry file))
1484 (defun dired-remove-entry (file)
1485 (save-excursion
1486 (and (dired-goto-file file)
1487 (let (buffer-read-only)
1488 (delete-region (progn (beginning-of-line) (point))
1489 (line-beginning-position 2))))))
1491 ;;;###autoload
1492 (defun dired-relist-file (file)
1493 "Create or update the line for FILE in all Dired buffers it would belong in."
1494 (dired-fun-in-all-buffers (file-name-directory file)
1495 (file-name-nondirectory file)
1496 #'dired-relist-entry file))
1498 (defun dired-relist-entry (file)
1499 ;; Relist the line for FILE, or just add it if it did not exist.
1500 ;; FILE must be an absolute file name.
1501 (let (buffer-read-only marker)
1502 ;; If cursor is already on FILE's line delete-region will cause
1503 ;; save-excursion to fail because of floating makers,
1504 ;; moving point to beginning of line. Sigh.
1505 (save-excursion
1506 (and (dired-goto-file file)
1507 (delete-region (progn (beginning-of-line)
1508 (setq marker (following-char))
1509 (point))
1510 (line-beginning-position 2)))
1511 (setq file (directory-file-name file))
1512 (dired-add-entry file (if (eq ?\040 marker) nil marker)))))
1514 ;;; Copy, move/rename, making hard and symbolic links
1516 (defcustom dired-backup-overwrite nil
1517 "Non-nil if Dired should ask about making backups before overwriting files.
1518 Special value `always' suppresses confirmation."
1519 :type '(choice (const :tag "off" nil)
1520 (const :tag "suppress" always)
1521 (other :tag "ask" t))
1522 :group 'dired)
1524 ;; This is a fluid var used in dired-handle-overwrite. It should be
1525 ;; let-bound whenever dired-copy-file etc are called. See
1526 ;; dired-create-files for an example.
1527 (defvar dired-overwrite-confirmed)
1529 (defun dired-handle-overwrite (to)
1530 ;; Save old version of file TO that is to be overwritten.
1531 ;; `dired-overwrite-confirmed' and `overwrite-backup-query' are fluid vars
1532 ;; from dired-create-files.
1533 (let (backup)
1534 (when (and dired-backup-overwrite
1535 dired-overwrite-confirmed
1536 (setq backup (car (find-backup-file-name to)))
1537 (or (eq 'always dired-backup-overwrite)
1538 (dired-query 'overwrite-backup-query
1539 "Make backup for existing file `%s'? "
1540 to)))
1541 (rename-file to backup 0) ; confirm overwrite of old backup
1542 (dired-relist-entry backup))))
1544 ;;;###autoload
1545 (defun dired-copy-file (from to ok-flag)
1546 (dired-handle-overwrite to)
1547 (dired-copy-file-recursive from to ok-flag dired-copy-preserve-time t
1548 dired-recursive-copies))
1550 (declare-function make-symbolic-link "fileio.c")
1552 (defcustom dired-create-destination-dirs nil
1553 "Whether Dired should create destination dirs when copying/removing files.
1554 If nil, don't create them.
1555 If `always', create them without asking.
1556 If `ask', ask for user confirmation."
1557 :type '(choice (const :tag "Never create non-existent dirs" nil)
1558 (const :tag "Always create non-existent dirs" always)
1559 (const :tag "Ask for user confirmation" ask))
1560 :group 'dired
1561 :version "27.1")
1563 (defun dired-maybe-create-dirs (dir)
1564 "Create DIR if doesn't exist according to `dired-create-destination-dirs'."
1565 (when (and dired-create-destination-dirs (not (file-exists-p dir)))
1566 (if (or (eq dired-create-destination-dirs 'always)
1567 (yes-or-no-p (format "Create destination dir `%s'? " dir)))
1568 (dired-create-directory dir))))
1570 (defun dired-copy-file-recursive (from to ok-flag &optional
1571 preserve-time top recursive)
1572 (when (and (eq t (car (file-attributes from)))
1573 (file-in-directory-p to from))
1574 (error "Cannot copy `%s' into its subdirectory `%s'" from to))
1575 (let ((attrs (file-attributes from)))
1576 (if (and recursive
1577 (eq t (car attrs))
1578 (or (eq recursive 'always)
1579 (yes-or-no-p (format "Recursive copies of %s? " from))))
1580 (copy-directory from to preserve-time)
1581 (or top (dired-handle-overwrite to))
1582 (condition-case err
1583 (if (stringp (car attrs))
1584 ;; It is a symlink
1585 (make-symbolic-link (car attrs) to ok-flag)
1586 (dired-maybe-create-dirs (file-name-directory to))
1587 (copy-file from to ok-flag preserve-time))
1588 (file-date-error
1589 (push (dired-make-relative from)
1590 dired-create-files-failures)
1591 (dired-log "Can't set date on %s:\n%s\n" from err))))))
1593 ;;;###autoload
1594 (defun dired-rename-file (file newname ok-if-already-exists)
1595 (dired-handle-overwrite newname)
1596 (dired-maybe-create-dirs (file-name-directory newname))
1597 (rename-file file newname ok-if-already-exists) ; error is caught in -create-files
1598 ;; Silently rename the visited file of any buffer visiting this file.
1599 (and (get-file-buffer file)
1600 (with-current-buffer (get-file-buffer file)
1601 (set-visited-file-name newname nil t)))
1602 (dired-remove-file file)
1603 ;; See if it's an inserted subdir, and rename that, too.
1604 (dired-rename-subdir file newname))
1606 (defun dired-rename-subdir (from-dir to-dir)
1607 (setq from-dir (file-name-as-directory from-dir)
1608 to-dir (file-name-as-directory to-dir))
1609 (dired-fun-in-all-buffers from-dir nil
1610 #'dired-rename-subdir-1 from-dir to-dir)
1611 ;; Update visited file name of all affected buffers
1612 (let ((expanded-from-dir (expand-file-name from-dir))
1613 (blist (buffer-list)))
1614 (while blist
1615 (with-current-buffer (car blist)
1616 (if (and buffer-file-name
1617 (dired-in-this-tree buffer-file-name expanded-from-dir))
1618 (let ((modflag (buffer-modified-p))
1619 (to-file (dired-replace-in-string
1620 (concat "^" (regexp-quote from-dir))
1621 to-dir
1622 buffer-file-name)))
1623 (set-visited-file-name to-file)
1624 (set-buffer-modified-p modflag))))
1625 (setq blist (cdr blist)))))
1627 (defun dired-rename-subdir-1 (dir to)
1628 ;; Rename DIR to TO in headerlines and dired-subdir-alist, if DIR or
1629 ;; one of its subdirectories is expanded in this buffer.
1630 (let ((expanded-dir (expand-file-name dir))
1631 (alist dired-subdir-alist)
1632 (elt nil))
1633 (while alist
1634 (setq elt (car alist)
1635 alist (cdr alist))
1636 (if (dired-in-this-tree (car elt) expanded-dir)
1637 ;; ELT's subdir is affected by the rename
1638 (dired-rename-subdir-2 elt dir to)))
1639 (if (equal dir default-directory)
1640 ;; if top level directory was renamed, lots of things have to be
1641 ;; updated:
1642 (progn
1643 (dired-unadvertise dir) ; we no longer dired DIR...
1644 (setq default-directory to
1645 dired-directory (expand-file-name;; this is correct
1646 ;; with and without wildcards
1647 (file-name-nondirectory (if (stringp dired-directory)
1648 dired-directory
1649 (car dired-directory)))
1650 to))
1651 (let ((new-name (file-name-nondirectory
1652 (directory-file-name (if (stringp dired-directory)
1653 dired-directory
1654 (car dired-directory))))))
1655 ;; try to rename buffer, but just leave old name if new
1656 ;; name would already exist (don't try appending "<%d>")
1657 (or (get-buffer new-name)
1658 (rename-buffer new-name)))
1659 ;; ... we dired TO now:
1660 (dired-advertise)))))
1662 (defun dired-rename-subdir-2 (elt dir to)
1663 ;; Update the headerline and dired-subdir-alist element, as well as
1664 ;; dired-switches-alist element, of directory described by
1665 ;; alist-element ELT to reflect the moving of DIR to TO. Thus, ELT
1666 ;; describes either DIR itself or a subdir of DIR.
1667 (save-excursion
1668 (let ((regexp (regexp-quote (directory-file-name dir)))
1669 (newtext (directory-file-name to))
1670 buffer-read-only)
1671 (goto-char (dired-get-subdir-min elt))
1672 ;; Update subdir headerline in buffer
1673 (if (not (looking-at dired-subdir-regexp))
1674 (error "%s not found where expected - dired-subdir-alist broken?"
1675 dir)
1676 (goto-char (match-beginning 1))
1677 (if (re-search-forward regexp (match-end 1) t)
1678 (replace-match newtext t t)
1679 (error "Expected to find `%s' in headerline of %s" dir (car elt))))
1680 ;; Update buffer-local dired-subdir-alist and dired-switches-alist
1681 (let ((cons (assoc-string (car elt) dired-switches-alist))
1682 (cur-dir (dired-normalize-subdir
1683 (dired-replace-in-string regexp newtext (car elt)))))
1684 (setcar elt cur-dir)
1685 (when cons (setcar cons cur-dir))))))
1687 ;; Bound in dired-create-files
1688 (defvar overwrite-query)
1689 (defvar overwrite-backup-query)
1691 ;; The basic function for half a dozen variations on cp/mv/ln/ln -s.
1692 (defun dired-create-files (file-creator operation fn-list name-constructor
1693 &optional marker-char)
1694 "Create one or more new files from a list of existing files FN-LIST.
1695 This function also handles querying the user, updating Dired
1696 buffers, and displaying a success or failure message.
1698 FILE-CREATOR should be a function. It is called once for each
1699 file in FN-LIST, and must create a new file, querying the user
1700 and updating Dired buffers as necessary. It should accept three
1701 arguments: the old file name, the new name, and an argument
1702 OK-IF-ALREADY-EXISTS with the same meaning as in `copy-file'.
1704 OPERATION should be a capitalized string describing the operation
1705 performed (e.g. `Copy'). It is used for error logging.
1707 FN-LIST is the list of files to copy (full absolute file names).
1709 NAME-CONSTRUCTOR should be a function accepting a single
1710 argument, the name of an old file, and returning either the
1711 corresponding new file name or nil to skip.
1713 If optional argument MARKER-CHAR is non-nil, mark each
1714 newly-created file's Dired entry with the character MARKER-CHAR,
1715 or with the current marker character if MARKER-CHAR is t."
1716 (let (dired-create-files-failures failures
1717 skipped (success-count 0) (total (length fn-list)))
1718 (let (to overwrite-query
1719 overwrite-backup-query) ; for dired-handle-overwrite
1720 (dolist (from fn-list)
1721 (setq to (funcall name-constructor from))
1722 (if (equal to from)
1723 (progn
1724 (setq to nil)
1725 (dired-log "Cannot %s to same file: %s\n"
1726 (downcase operation) from)))
1727 (if (not to)
1728 (setq skipped (cons (dired-make-relative from) skipped))
1729 (let* ((overwrite (file-exists-p to))
1730 (dired-overwrite-confirmed ; for dired-handle-overwrite
1731 (and overwrite
1732 (let ((help-form '(format-message "\
1733 Type SPC or `y' to overwrite file `%s',
1734 DEL or `n' to skip to next,
1735 ESC or `q' to not overwrite any of the remaining files,
1736 `!' to overwrite all remaining files with no more questions." to)))
1737 (dired-query 'overwrite-query
1738 "Overwrite `%s'?" to))))
1739 ;; must determine if FROM is marked before file-creator
1740 ;; gets a chance to delete it (in case of a move).
1741 (actual-marker-char
1742 (cond ((integerp marker-char) marker-char)
1743 (marker-char (dired-file-marker from)) ; slow
1744 (t nil))))
1745 ;; Handle the `dired-copy-file' file-creator specially
1746 ;; When copying a directory to another directory or
1747 ;; possibly to itself or one of its subdirectories.
1748 ;; e.g "~/foo/" => "~/test/"
1749 ;; or "~/foo/" =>"~/foo/"
1750 ;; or "~/foo/ => ~/foo/bar/")
1751 ;; In this case the 'name-constructor' have set the destination
1752 ;; TO to "~/test/foo" because the old emacs23 behavior
1753 ;; of `copy-directory' was to not create the subdirectory
1754 ;; and instead copy the contents.
1755 ;; With the new behavior of `copy-directory'
1756 ;; (similar to the `cp' shell command) we don't
1757 ;; need such a construction of the target directory,
1758 ;; so modify the destination TO to "~/test/" instead of "~/test/foo/".
1759 (let ((destname (file-name-directory to)))
1760 (when (and (file-directory-p from)
1761 (file-directory-p to)
1762 (eq file-creator 'dired-copy-file))
1763 (setq to destname))
1764 ;; If DESTNAME is a subdirectory of FROM, not a symlink,
1765 ;; and the method in use is copying, signal an error.
1766 (and (eq t (car (file-attributes destname)))
1767 (eq file-creator 'dired-copy-file)
1768 (file-in-directory-p destname from)
1769 (error "Cannot copy `%s' into its subdirectory `%s'"
1770 from to)))
1771 (condition-case err
1772 (progn
1773 (funcall file-creator from to dired-overwrite-confirmed)
1774 (if overwrite
1775 ;; If we get here, file-creator hasn't been aborted
1776 ;; and the old entry (if any) has to be deleted
1777 ;; before adding the new entry.
1778 (dired-remove-file to))
1779 (setq success-count (1+ success-count))
1780 (message "%s: %d of %d" operation success-count total)
1781 (dired-add-file to actual-marker-char))
1782 (file-error ; FILE-CREATOR aborted
1783 (progn
1784 (push (dired-make-relative from)
1785 failures)
1786 (dired-log "%s `%s' to `%s' failed:\n%s\n"
1787 operation from to err))))))))
1788 (cond
1789 (dired-create-files-failures
1790 (setq failures (nconc failures dired-create-files-failures))
1791 (dired-log-summary
1792 (format "%s failed for %d file%s in %d requests"
1793 operation (length failures)
1794 (dired-plural-s (length failures))
1795 total)
1796 failures))
1797 (failures
1798 (dired-log-summary
1799 (format "%s failed for %d of %d file%s"
1800 operation (length failures)
1801 total (dired-plural-s total))
1802 failures))
1803 (skipped
1804 (dired-log-summary
1805 (format "%s: %d of %d file%s skipped"
1806 operation (length skipped) total
1807 (dired-plural-s total))
1808 skipped))
1810 (message "%s: %s file%s"
1811 operation success-count (dired-plural-s success-count)))))
1812 (dired-move-to-filename))
1814 (defun dired-do-create-files (op-symbol file-creator operation arg
1815 &optional marker-char op1
1816 how-to)
1817 "Create a new file for each marked file.
1818 Prompt user for a target directory in which to create the new
1819 files. The target may also be a non-directory file, if only
1820 one file is marked. The initial suggestion for target is the
1821 Dired buffer's current directory (or, if `dired-dwim-target' is
1822 non-nil, the current directory of a neighboring Dired window).
1823 OP-SYMBOL is the symbol for the operation. Function `dired-mark-pop-up'
1824 will determine whether pop-ups are appropriate for this OP-SYMBOL.
1825 FILE-CREATOR and OPERATION as in `dired-create-files'.
1826 ARG as in `dired-get-marked-files'.
1827 Optional arg MARKER-CHAR as in `dired-create-files'.
1828 Optional arg OP1 is an alternate form for OPERATION if there is
1829 only one file.
1830 Optional arg HOW-TO determines how to treat the target.
1831 If HOW-TO is nil, use `file-directory-p' to determine if the
1832 target is a directory. If so, the marked file(s) are created
1833 inside that directory. Otherwise, the target is a plain file;
1834 an error is raised unless there is exactly one marked file.
1835 If HOW-TO is t, target is always treated as a plain file.
1836 Otherwise, HOW-TO should be a function of one argument, TARGET.
1837 If its return value is nil, TARGET is regarded as a plain file.
1838 If it return value is a list, TARGET is a generalized
1839 directory (e.g. some sort of archive). The first element of
1840 this list must be a function with at least four arguments:
1841 operation - as OPERATION above.
1842 rfn-list - list of the relative names for the marked files.
1843 fn-list - list of the absolute names for the marked files.
1844 target - the name of the target itself.
1845 The rest of elements of the list returned by HOW-TO are optional
1846 arguments for the function that is the first element of the list.
1847 For any other return value, TARGET is treated as a directory."
1848 (or op1 (setq op1 operation))
1849 (let* ((fn-list (dired-get-marked-files nil arg nil nil t))
1850 (rfn-list (mapcar #'dired-make-relative fn-list))
1851 (dired-one-file ; fluid variable inside dired-create-files
1852 (and (consp fn-list) (null (cdr fn-list)) (car fn-list)))
1853 (target-dir (dired-dwim-target-directory))
1854 (default (and dired-one-file
1855 (not dired-dwim-target) ; Bug#25609
1856 (expand-file-name (file-name-nondirectory (car fn-list))
1857 target-dir)))
1858 (defaults (dired-dwim-target-defaults fn-list target-dir))
1859 (target (expand-file-name ; fluid variable inside dired-create-files
1860 (minibuffer-with-setup-hook
1861 (lambda ()
1862 (set (make-local-variable 'minibuffer-default-add-function) nil)
1863 (setq minibuffer-default defaults))
1864 (dired-mark-read-file-name
1865 (concat (if dired-one-file op1 operation) " %s to: ")
1866 target-dir op-symbol arg rfn-list default))))
1867 (into-dir (cond ((null how-to)
1868 ;; Allow users to change the letter case of
1869 ;; a directory on a case-insensitive
1870 ;; filesystem. If we don't test these
1871 ;; conditions up front, file-directory-p
1872 ;; below will return t on a case-insensitive
1873 ;; filesystem, and Emacs will try to move
1874 ;; foo -> foo/foo, which fails.
1875 (if (and (file-name-case-insensitive-p (car fn-list))
1876 (eq op-symbol 'move)
1877 dired-one-file
1878 (string= (downcase
1879 (expand-file-name (car fn-list)))
1880 (downcase
1881 (expand-file-name target)))
1882 (not (string=
1883 (file-name-nondirectory (car fn-list))
1884 (file-name-nondirectory target))))
1886 (file-directory-p target)))
1887 ((eq how-to t) nil)
1888 (t (funcall how-to target)))))
1889 (if (and (consp into-dir) (functionp (car into-dir)))
1890 (apply (car into-dir) operation rfn-list fn-list target (cdr into-dir))
1891 (if (not (or dired-one-file into-dir))
1892 (error "Marked %s: target must be a directory: %s" operation target))
1893 ;; rename-file bombs when moving directories unless we do this:
1894 (or into-dir (setq target (directory-file-name target)))
1895 (dired-create-files
1896 file-creator operation fn-list
1897 (if into-dir ; target is a directory
1898 ;; This function uses fluid variable target when called
1899 ;; inside dired-create-files:
1900 (lambda (from)
1901 (expand-file-name (file-name-nondirectory from) target))
1902 (lambda (_from) target))
1903 marker-char))))
1905 ;; Read arguments for a marked-files command that wants a file name,
1906 ;; perhaps popping up the list of marked files.
1907 ;; ARG is the prefix arg and indicates whether the files came from
1908 ;; marks (ARG=nil) or a repeat factor (integerp ARG).
1909 ;; If the current file was used, the list has but one element and ARG
1910 ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)).
1911 ;; DEFAULT is the default value to return if the user just hits RET;
1912 ;; if it is omitted or nil, then the name of the directory is used.
1914 (defun dired-mark-read-file-name (prompt dir op-symbol arg files
1915 &optional default)
1916 (dired-mark-pop-up
1917 nil op-symbol files
1918 #'read-file-name
1919 (format prompt (dired-mark-prompt arg files)) dir default))
1921 (defun dired-dwim-target-directory ()
1922 ;; Try to guess which target directory the user may want.
1923 ;; If there is a dired buffer displayed in one of the next windows,
1924 ;; use its current subdir, else use current subdir of this dired buffer.
1925 (let ((this-dir (and (eq major-mode 'dired-mode)
1926 (dired-current-directory))))
1927 ;; non-dired buffer may want to profit from this function, e.g. vm-uudecode
1928 (if dired-dwim-target
1929 (let* ((other-win (get-window-with-predicate
1930 (lambda (window)
1931 (with-current-buffer (window-buffer window)
1932 (eq major-mode 'dired-mode)))))
1933 (other-dir (and other-win
1934 (with-current-buffer (window-buffer other-win)
1935 (and (eq major-mode 'dired-mode)
1936 (dired-current-directory))))))
1937 (or other-dir this-dir))
1938 this-dir)))
1940 (defun dired-dwim-target-defaults (fn-list target-dir)
1941 ;; Return a list of default values for file-reading functions in Dired.
1942 ;; This list may contain directories from Dired buffers in other windows.
1943 ;; `fn-list' is a list of file names used to build a list of defaults.
1944 ;; When nil or more than one element, a list of defaults will
1945 ;; contain only directory names. `target-dir' is a directory name
1946 ;; to exclude from the returned list, for the case when this
1947 ;; directory name is already presented in initial input.
1948 ;; For Dired operations that support `dired-dwim-target',
1949 ;; the argument `target-dir' should have the value returned
1950 ;; from `dired-dwim-target-directory'.
1951 (let ((dired-one-file
1952 (and (consp fn-list) (null (cdr fn-list)) (car fn-list)))
1953 (current-dir (and (eq major-mode 'dired-mode)
1954 (dired-current-directory)))
1955 dired-dirs)
1956 ;; Get a list of directories of visible buffers in dired-mode.
1957 (walk-windows (lambda (w)
1958 (with-current-buffer (window-buffer w)
1959 (and (eq major-mode 'dired-mode)
1960 (push (dired-current-directory) dired-dirs)))))
1961 ;; Force the current dir to be the first in the list.
1962 (setq dired-dirs
1963 (delete-dups (delq nil (cons current-dir (nreverse dired-dirs)))))
1964 ;; Remove the target dir (if specified) or the current dir from
1965 ;; default values, because it should be already in initial input.
1966 (setq dired-dirs (delete (or target-dir current-dir) dired-dirs))
1967 ;; Return a list of default values.
1968 (if dired-one-file
1969 ;; For one file operation, provide a list that contains
1970 ;; other directories, other directories with the appended filename
1971 ;; and the current directory with the appended filename, e.g.
1972 ;; 1. /TARGET-DIR/
1973 ;; 2. /TARGET-DIR/FILENAME
1974 ;; 3. /CURRENT-DIR/FILENAME
1975 (append dired-dirs
1976 (mapcar (lambda (dir)
1977 (expand-file-name
1978 (file-name-nondirectory (car fn-list)) dir))
1979 (reverse dired-dirs))
1980 (list (expand-file-name
1981 (file-name-nondirectory (car fn-list))
1982 (or target-dir current-dir))))
1983 ;; For multi-file operation, return only a list of other directories.
1984 dired-dirs)))
1987 ;;;###autoload
1988 (defun dired-create-directory (directory)
1989 "Create a directory called DIRECTORY.
1990 Parent directories of DIRECTORY are created as needed.
1991 If DIRECTORY already exists, signal an error."
1992 (interactive
1993 (list (read-file-name "Create directory: " (dired-current-directory))))
1994 (let* ((expanded (directory-file-name (expand-file-name directory)))
1995 (try expanded) new)
1996 (if (file-exists-p expanded)
1997 (error "Cannot create directory %s: file exists" expanded))
1998 ;; Find the topmost nonexistent parent dir (variable `new')
1999 (while (and try (not (file-exists-p try)) (not (equal new try)))
2000 (setq new try
2001 try (directory-file-name (file-name-directory try))))
2002 (make-directory expanded t)
2003 (when new
2004 (dired-add-file new)
2005 (dired-move-to-filename))))
2007 (defun dired-into-dir-with-symlinks (target)
2008 (and (file-directory-p target)
2009 (not (file-symlink-p target))))
2010 ;; This may not always be what you want, especially if target is your
2011 ;; home directory and it happens to be a symbolic link, as is often the
2012 ;; case with NFS and automounters. Or if you want to make symlinks
2013 ;; into directories that themselves are only symlinks, also quite
2014 ;; common.
2016 ;; So we don't use this function as value for HOW-TO in
2017 ;; dired-do-symlink, which has the minor disadvantage of
2018 ;; making links *into* a symlinked-dir, when you really wanted to
2019 ;; *overwrite* that symlink. In that (rare, I guess) case, you'll
2020 ;; just have to remove that symlink by hand before making your marked
2021 ;; symlinks.
2023 (defvar dired-copy-how-to-fn nil
2024 "Either nil or a function used by `dired-do-copy' to determine target.
2025 See HOW-TO argument for `dired-do-create-files'.")
2027 ;;;###autoload
2028 (defun dired-do-copy (&optional arg)
2029 "Copy all marked (or next ARG) files, or copy the current file.
2030 When operating on just the current file, prompt for the new name.
2032 When operating on multiple or marked files, prompt for a target
2033 directory, and make the new copies in that directory, with the
2034 same names as the original files. The initial suggestion for the
2035 target directory is the Dired buffer's current directory (or, if
2036 `dired-dwim-target' is non-nil, the current directory of a
2037 neighboring Dired window).
2039 If `dired-copy-preserve-time' is non-nil, this command preserves
2040 the modification time of each old file in the copy, similar to
2041 the \"-p\" option for the \"cp\" shell command.
2043 This command copies symbolic links by creating new ones, similar
2044 to the \"-d\" option for the \"cp\" shell command."
2045 (interactive "P")
2046 (let ((dired-recursive-copies dired-recursive-copies))
2047 (dired-do-create-files 'copy #'dired-copy-file
2048 "Copy"
2049 arg dired-keep-marker-copy
2050 nil dired-copy-how-to-fn)))
2052 ;;;###autoload
2053 (defun dired-do-symlink (&optional arg)
2054 "Make symbolic links to current file or all marked (or next ARG) files.
2055 When operating on just the current file, you specify the new name.
2056 When operating on multiple or marked files, you specify a directory
2057 and new symbolic links are made in that directory
2058 with the same names that the files currently have. The default
2059 suggested for the target directory depends on the value of
2060 `dired-dwim-target', which see.
2062 For relative symlinks, use \\[dired-do-relsymlink]."
2063 (interactive "P")
2064 (dired-do-create-files 'symlink #'make-symbolic-link
2065 "Symlink" arg dired-keep-marker-symlink))
2067 ;;;###autoload
2068 (defun dired-do-hardlink (&optional arg)
2069 "Add names (hard links) current file or all marked (or next ARG) files.
2070 When operating on just the current file, you specify the new name.
2071 When operating on multiple or marked files, you specify a directory
2072 and new hard links are made in that directory
2073 with the same names that the files currently have. The default
2074 suggested for the target directory depends on the value of
2075 `dired-dwim-target', which see."
2076 (interactive "P")
2077 (dired-do-create-files 'hardlink #'dired-hardlink
2078 "Hardlink" arg dired-keep-marker-hardlink))
2080 (defun dired-hardlink (file newname &optional ok-if-already-exists)
2081 (dired-handle-overwrite newname)
2082 ;; error is caught in -create-files
2083 (add-name-to-file file newname ok-if-already-exists)
2084 ;; Update the link count
2085 (dired-relist-file file))
2087 ;;;###autoload
2088 (defun dired-do-rename (&optional arg)
2089 "Rename current file or all marked (or next ARG) files.
2090 When renaming just the current file, you specify the new name.
2091 When renaming multiple or marked files, you specify a directory.
2092 This command also renames any buffers that are visiting the files.
2093 The default suggested for the target directory depends on the value
2094 of `dired-dwim-target', which see."
2095 (interactive "P")
2096 (dired-do-create-files 'move #'dired-rename-file
2097 "Move" arg dired-keep-marker-rename "Rename"))
2098 ;;;###end dired-cp.el
2100 ;;; 5K
2101 ;;;###begin dired-re.el
2102 (defvar rename-regexp-query)
2104 (defun dired-do-create-files-regexp
2105 (file-creator operation arg regexp newname &optional whole-name marker-char)
2106 ;; Create a new file for each marked file using regexps.
2107 ;; FILE-CREATOR and OPERATION as in dired-create-files.
2108 ;; ARG as in dired-get-marked-files.
2109 ;; Matches each marked file against REGEXP and constructs the new
2110 ;; filename from NEWNAME (like in function replace-match).
2111 ;; Optional arg WHOLE-NAME means match/replace the whole file name
2112 ;; instead of only the non-directory part of the file.
2113 ;; Optional arg MARKER-CHAR as in dired-create-files.
2114 (let* ((fn-list (dired-get-marked-files nil arg))
2115 (operation-prompt (concat operation " `%s' to `%s'?"))
2116 (rename-regexp-help-form (format-message "\
2117 Type SPC or `y' to %s one match, DEL or `n' to skip to next,
2118 `!' to %s all remaining matches with no more questions."
2119 (downcase operation)
2120 (downcase operation)))
2121 (regexp-name-constructor
2122 ;; Function to construct new filename using REGEXP and NEWNAME:
2123 (if whole-name ; easy (but rare) case
2124 (lambda (from)
2125 (let ((to (dired-string-replace-match regexp from newname))
2126 ;; must bind help-form directly around call to
2127 ;; dired-query
2128 (help-form rename-regexp-help-form))
2129 (if to
2130 (and (dired-query 'rename-regexp-query
2131 operation-prompt
2132 from
2135 (dired-log "%s: %s did not match regexp %s\n"
2136 operation from regexp))))
2137 ;; not whole-name, replace non-directory part only
2138 (lambda (from)
2139 (let* ((new (dired-string-replace-match
2140 regexp (file-name-nondirectory from) newname))
2141 (to (and new ; nil means there was no match
2142 (expand-file-name new
2143 (file-name-directory from))))
2144 (help-form rename-regexp-help-form))
2145 (if to
2146 (and (dired-query 'rename-regexp-query
2147 operation-prompt
2148 (dired-make-relative from)
2149 (dired-make-relative to))
2151 (dired-log "%s: %s did not match regexp %s\n"
2152 operation (file-name-nondirectory from) regexp))))))
2153 rename-regexp-query)
2154 (dired-create-files
2155 file-creator operation fn-list regexp-name-constructor marker-char)))
2157 (defun dired-mark-read-regexp (operation)
2158 ;; Prompt user about performing OPERATION.
2159 ;; Read and return list of: regexp newname arg whole-name.
2160 (let* ((whole-name
2161 (equal 0 (prefix-numeric-value current-prefix-arg)))
2162 (arg
2163 (if whole-name nil current-prefix-arg))
2164 (regexp
2165 (read-regexp
2166 (concat (if whole-name "Abs. " "") operation " from (regexp): ")
2167 nil 'dired-regexp-history))
2168 (newname
2169 (read-string
2170 (concat (if whole-name "Abs. " "") operation " " regexp " to: "))))
2171 (list regexp newname arg whole-name)))
2173 ;;;###autoload
2174 (defun dired-do-rename-regexp (regexp newname &optional arg whole-name)
2175 "Rename selected files whose names match REGEXP to NEWNAME.
2177 With non-zero prefix argument ARG, the command operates on the next ARG
2178 files. Otherwise, it operates on all the marked files, or the current
2179 file if none are marked.
2181 As each match is found, the user must type a character saying
2182 what to do with it. For directions, type \\[help-command] at that time.
2183 NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
2184 REGEXP defaults to the last regexp used.
2186 With a zero prefix arg, renaming by regexp affects the absolute file name.
2187 Normally, only the non-directory part of the file name is used and changed."
2188 (interactive (dired-mark-read-regexp "Rename"))
2189 (dired-do-create-files-regexp
2190 #'dired-rename-file
2191 "Rename" arg regexp newname whole-name dired-keep-marker-rename))
2193 ;;;###autoload
2194 (defun dired-do-copy-regexp (regexp newname &optional arg whole-name)
2195 "Copy selected files whose names match REGEXP to NEWNAME.
2196 See function `dired-do-rename-regexp' for more info."
2197 (interactive (dired-mark-read-regexp "Copy"))
2198 (let ((dired-recursive-copies nil)) ; No recursive copies.
2199 (dired-do-create-files-regexp
2200 #'dired-copy-file
2201 (if dired-copy-preserve-time "Copy [-p]" "Copy")
2202 arg regexp newname whole-name dired-keep-marker-copy)))
2204 ;;;###autoload
2205 (defun dired-do-hardlink-regexp (regexp newname &optional arg whole-name)
2206 "Hardlink selected files whose names match REGEXP to NEWNAME.
2207 See function `dired-do-rename-regexp' for more info."
2208 (interactive (dired-mark-read-regexp "HardLink"))
2209 (dired-do-create-files-regexp
2210 #'add-name-to-file
2211 "HardLink" arg regexp newname whole-name dired-keep-marker-hardlink))
2213 ;;;###autoload
2214 (defun dired-do-symlink-regexp (regexp newname &optional arg whole-name)
2215 "Symlink selected files whose names match REGEXP to NEWNAME.
2216 See function `dired-do-rename-regexp' for more info."
2217 (interactive (dired-mark-read-regexp "SymLink"))
2218 (dired-do-create-files-regexp
2219 #'make-symbolic-link
2220 "SymLink" arg regexp newname whole-name dired-keep-marker-symlink))
2222 (defvar rename-non-directory-query)
2224 (defun dired-create-files-non-directory
2225 (file-creator basename-constructor operation arg)
2226 ;; Perform FILE-CREATOR on the non-directory part of marked files
2227 ;; using function BASENAME-CONSTRUCTOR, with query for each file.
2228 ;; OPERATION like in dired-create-files, ARG as in dired-get-marked-files.
2229 (let (rename-non-directory-query)
2230 (dired-create-files
2231 file-creator
2232 operation
2233 (dired-get-marked-files nil arg)
2234 (lambda (from)
2235 (let ((to (concat (file-name-directory from)
2236 (funcall basename-constructor
2237 (file-name-nondirectory from)))))
2238 (and (let ((help-form (format-message "\
2239 Type SPC or `y' to %s one file, DEL or `n' to skip to next,
2240 `!' to %s all remaining matches with no more questions."
2241 (downcase operation)
2242 (downcase operation))))
2243 (dired-query 'rename-non-directory-query
2244 (concat operation " `%s' to `%s'")
2245 (dired-make-relative from)
2246 (dired-make-relative to)))
2247 to)))
2248 dired-keep-marker-rename)))
2250 (defun dired-rename-non-directory (basename-constructor operation arg)
2251 (dired-create-files-non-directory
2252 #'dired-rename-file
2253 basename-constructor operation arg))
2255 ;;;###autoload
2256 (defun dired-upcase (&optional arg)
2257 "Rename all marked (or next ARG) files to upper case."
2258 (interactive "P")
2259 (dired-rename-non-directory #'upcase "Rename upcase" arg))
2261 ;;;###autoload
2262 (defun dired-downcase (&optional arg)
2263 "Rename all marked (or next ARG) files to lower case."
2264 (interactive "P")
2265 (dired-rename-non-directory #'downcase "Rename downcase" arg))
2267 ;;;###end dired-re.el
2269 ;;; 13K
2270 ;;;###begin dired-ins.el
2272 ;;;###autoload
2273 (defun dired-maybe-insert-subdir (dirname &optional
2274 switches no-error-if-not-dir-p)
2275 "Insert this subdirectory into the same dired buffer.
2276 If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
2277 else inserts it at its natural place (as `ls -lR' would have done).
2278 With a prefix arg, you may edit the ls switches used for this listing.
2279 You can add `R' to the switches to expand the whole tree starting at
2280 this subdirectory.
2281 This function takes some pains to conform to `ls -lR' output.
2283 Dired remembers switches specified with a prefix arg, so that reverting
2284 the buffer will not reset them. However, using `dired-undo' to re-insert
2285 or delete subdirectories can bypass this machinery. Hence, you sometimes
2286 may have to reset some subdirectory switches after a `dired-undo'.
2287 You can reset all subdirectory switches to the default using
2288 \\<dired-mode-map>\\[dired-reset-subdir-switches].
2289 See Info node `(emacs)Subdir switches' for more details."
2290 (interactive
2291 (list (dired-get-filename)
2292 (if current-prefix-arg
2293 (read-string "Switches for listing: "
2294 (or dired-subdir-switches dired-actual-switches)))))
2295 (let ((opoint (point)))
2296 ;; We don't need a marker for opoint as the subdir is always
2297 ;; inserted *after* opoint.
2298 (setq dirname (file-name-as-directory dirname))
2299 (or (and (not switches)
2300 (when (dired-goto-subdir dirname)
2301 (unless (dired-subdir-hidden-p dirname)
2302 (dired-initial-position dirname))
2304 (dired-insert-subdir dirname switches no-error-if-not-dir-p))
2305 ;; Push mark so that it's easy to find back. Do this after the
2306 ;; insert message so that the user sees the `Mark set' message.
2307 (push-mark opoint)))
2309 ;;;###autoload
2310 (defun dired-insert-subdir (dirname &optional switches no-error-if-not-dir-p)
2311 "Insert this subdirectory into the same Dired buffer.
2312 If it is already present, overwrite the previous entry;
2313 otherwise, insert it at its natural place (as `ls -lR' would
2314 have done).
2315 With a prefix arg, you may edit the `ls' switches used for this listing.
2316 You can add `R' to the switches to expand the whole tree starting at
2317 this subdirectory.
2318 This function takes some pains to conform to `ls -lR' output."
2319 ;; NO-ERROR-IF-NOT-DIR-P needed for special filesystems like
2320 ;; Prospero where dired-ls does the right thing, but
2321 ;; file-directory-p has not been redefined.
2322 (interactive
2323 (list (dired-get-filename)
2324 (if current-prefix-arg
2325 (read-string "Switches for listing: "
2326 (or dired-subdir-switches dired-actual-switches)))))
2327 (setq dirname (file-name-as-directory (expand-file-name dirname)))
2328 (or no-error-if-not-dir-p
2329 (file-directory-p dirname)
2330 (error "Attempt to insert a non-directory: %s" dirname))
2331 (let ((elt (assoc dirname dired-subdir-alist))
2332 (cons (assoc-string dirname dired-switches-alist))
2333 (modflag (buffer-modified-p))
2334 (old-switches switches)
2335 switches-have-R mark-alist case-fold-search buffer-read-only)
2336 (and (not switches) cons (setq switches (cdr cons)))
2337 (dired-insert-subdir-validate dirname switches)
2338 ;; case-fold-search is nil now, so we can test for capital `R':
2339 (if (setq switches-have-R (and switches (string-match-p "R" switches)))
2340 ;; avoid duplicated subdirs
2341 (setq mark-alist (dired-kill-tree dirname t)))
2342 (if elt
2343 ;; If subdir is already present, remove it and remember its marks
2344 (setq mark-alist (nconc (dired-insert-subdir-del elt) mark-alist))
2345 (dired-insert-subdir-newpos dirname)) ; else compute new position
2346 (dired-insert-subdir-doupdate
2347 dirname elt (dired-insert-subdir-doinsert dirname switches))
2348 (when old-switches
2349 (if cons
2350 (setcdr cons switches)
2351 (push (cons dirname switches) dired-switches-alist)))
2352 (when switches-have-R
2353 (dired-build-subdir-alist switches)
2354 (setq switches (dired-replace-in-string "R" "" switches))
2355 (dolist (cur-ass dired-subdir-alist)
2356 (let ((cur-dir (car cur-ass)))
2357 (and (dired-in-this-tree cur-dir dirname)
2358 (let ((cur-cons (assoc-string cur-dir dired-switches-alist)))
2359 (if cur-cons
2360 (setcdr cur-cons switches)
2361 (push (cons cur-dir switches) dired-switches-alist)))))))
2362 (dired-initial-position dirname)
2363 (save-excursion (dired-mark-remembered mark-alist))
2364 (restore-buffer-modified-p modflag)))
2366 (defun dired-insert-subdir-validate (dirname &optional switches)
2367 ;; Check that it is valid to insert DIRNAME with SWITCHES.
2368 ;; Signal an error if invalid (e.g. user typed `i' on `..').
2369 (or (dired-in-this-tree dirname (expand-file-name default-directory))
2370 (error "%s: not in this directory tree" dirname))
2371 (let ((real-switches (or switches dired-subdir-switches)))
2372 (when real-switches
2373 (let (case-fold-search)
2374 (mapcar
2375 (lambda (x)
2376 (or (eq (null (string-match-p x real-switches))
2377 (null (string-match-p x dired-actual-switches)))
2378 (error
2379 "Can't have dirs with and without -%s switches together" x)))
2380 ;; all switches that make a difference to dired-get-filename:
2381 '("F" "b"))))))
2383 (defun dired-alist-add (dir new-marker)
2384 ;; Add new DIR at NEW-MARKER. Sort alist.
2385 (dired-alist-add-1 dir new-marker)
2386 (dired-alist-sort))
2388 (defun dired-alist-sort ()
2389 ;; Keep the alist sorted on buffer position.
2390 (setq dired-subdir-alist
2391 (sort dired-subdir-alist
2392 (lambda (elt1 elt2)
2393 (> (dired-get-subdir-min elt1)
2394 (dired-get-subdir-min elt2))))))
2396 (defun dired-kill-tree (dirname &optional remember-marks kill-root)
2397 "Kill all proper subdirs of DIRNAME, excluding DIRNAME itself.
2398 Interactively, you can kill DIRNAME as well by using a prefix argument.
2399 In interactive use, the command prompts for DIRNAME.
2401 When called from Lisp, if REMEMBER-MARKS is non-nil, return an alist
2402 of marked files. If KILL-ROOT is non-nil, kill DIRNAME as well."
2403 (interactive "DKill tree below directory: \ni\nP")
2404 (setq dirname (file-name-as-directory (expand-file-name dirname)))
2405 (let ((s-alist dired-subdir-alist) dir m-alist)
2406 (while s-alist
2407 (setq dir (car (car s-alist))
2408 s-alist (cdr s-alist))
2409 (and (or kill-root (not (string-equal dir dirname)))
2410 (dired-in-this-tree dir dirname)
2411 (dired-goto-subdir dir)
2412 (setq m-alist (nconc (dired-kill-subdir remember-marks) m-alist))))
2413 m-alist))
2415 (defun dired-insert-subdir-newpos (new-dir)
2416 ;; Find pos for new subdir, according to tree order.
2417 ;;(goto-char (point-max))
2418 (let ((alist dired-subdir-alist) elt dir new-pos)
2419 (while alist
2420 (setq elt (car alist)
2421 alist (cdr alist)
2422 dir (car elt))
2423 (if (dired-tree-lessp dir new-dir)
2424 ;; Insert NEW-DIR after DIR
2425 (setq new-pos (dired-get-subdir-max elt)
2426 alist nil)))
2427 (goto-char new-pos))
2428 ;; want a separating newline between subdirs
2429 (or (eobp)
2430 (forward-line -1))
2431 (insert "\n")
2432 (point))
2434 (defun dired-insert-subdir-del (element)
2435 ;; Erase an already present subdir (given by ELEMENT) from buffer.
2436 ;; Move to that buffer position. Return a mark-alist.
2437 (let ((begin-marker (dired-get-subdir-min element)))
2438 (goto-char begin-marker)
2439 ;; Are at beginning of subdir (and inside it!). Now determine its end:
2440 (goto-char (dired-subdir-max))
2441 (or (eobp);; want a separating newline _between_ subdirs:
2442 (forward-char -1))
2443 (prog1
2444 (dired-remember-marks begin-marker (point))
2445 (delete-region begin-marker (point)))))
2447 (defun dired-insert-subdir-doinsert (dirname switches)
2448 ;; Insert ls output after point.
2449 ;; Return the boundary of the inserted text (as list of BEG and END).
2450 (save-excursion
2451 (let ((begin (point)))
2452 (let ((dired-actual-switches
2453 (or switches
2454 dired-subdir-switches
2455 (dired-replace-in-string "R" "" dired-actual-switches))))
2456 (if (equal dirname (car (car (last dired-subdir-alist))))
2457 ;; If doing the top level directory of the buffer,
2458 ;; redo it as specified in dired-directory.
2459 (dired-readin-insert)
2460 (dired-insert-directory dirname dired-actual-switches nil nil t)))
2461 (list begin (point)))))
2463 (defun dired-insert-subdir-doupdate (dirname elt beg-end)
2464 ;; Point is at the correct subdir alist position for ELT,
2465 ;; BEG-END is the subdir-region (as list of begin and end).
2466 (if elt ; subdir was already present
2467 ;; update its position (should actually be unchanged)
2468 (set-marker (dired-get-subdir-min elt) (point-marker))
2469 (dired-alist-add dirname (point-marker)))
2470 ;; The hook may depend on the subdir-alist containing the just
2471 ;; inserted subdir, so run it after dired-alist-add:
2472 (if dired-after-readin-hook
2473 (save-excursion
2474 (let ((begin (nth 0 beg-end))
2475 (end (nth 1 beg-end)))
2476 (goto-char begin)
2477 (save-restriction
2478 (narrow-to-region begin end)
2479 ;; hook may add or delete lines, but the subdir boundary
2480 ;; marker floats
2481 (run-hooks 'dired-after-readin-hook))))))
2483 (defun dired-tree-lessp (dir1 dir2)
2484 ;; Lexicographic order on file name components, like `ls -lR':
2485 ;; DIR1 < DIR2 if DIR1 comes *before* DIR2 in an `ls -lR' listing,
2486 ;; i.e., if DIR1 is a (grand)parent dir of DIR2,
2487 ;; or DIR1 and DIR2 are in the same parentdir and their last
2488 ;; components are string-lessp.
2489 ;; Thus ("/usr/" "/usr/bin") and ("/usr/a/" "/usr/b/") are tree-lessp.
2490 ;; string-lessp could arguably be replaced by file-newer-than-file-p
2491 ;; if dired-actual-switches contained t.
2492 (setq dir1 (file-name-as-directory dir1)
2493 dir2 (file-name-as-directory dir2))
2494 (let ((components-1 (dired-split "/" dir1))
2495 (components-2 (dired-split "/" dir2)))
2496 (while (and components-1
2497 components-2
2498 (equal (car components-1) (car components-2)))
2499 (setq components-1 (cdr components-1)
2500 components-2 (cdr components-2)))
2501 (let ((c1 (car components-1))
2502 (c2 (car components-2)))
2504 (cond ((and c1 c2)
2505 (string-lessp c1 c2))
2506 ((and (null c1) (null c2))
2507 nil) ; they are equal, not lessp
2508 ((null c1) ; c2 is a subdir of c1: c1<c2
2510 ((null c2) ; c1 is a subdir of c2: c1>c2
2511 nil)
2512 (t (error "This can't happen"))))))
2514 ;; There should be a builtin split function - inverse to mapconcat.
2515 (defun dired-split (pat str &optional limit)
2516 "Splitting on regexp PAT, turn string STR into a list of substrings.
2517 Optional third arg LIMIT (>= 1) is a limit to the length of the
2518 resulting list.
2519 Thus, if SEP is a regexp that only matches itself,
2521 (mapconcat 'identity (dired-split SEP STRING) SEP)
2523 is always equal to STRING."
2524 (let* ((start (string-match pat str))
2525 (result (list (substring str 0 start)))
2526 (count 1)
2527 (end (if start (match-end 0))))
2528 (if end ; else nothing left
2529 (while (and (or (not (integerp limit))
2530 (< count limit))
2531 (string-match pat str end))
2532 (setq start (match-beginning 0)
2533 count (1+ count)
2534 result (cons (substring str end start) result)
2535 end (match-end 0)
2536 start end)
2538 (if (and (or (not (integerp limit))
2539 (< count limit))
2540 end) ; else nothing left
2541 (setq result
2542 (cons (substring str end) result)))
2543 (nreverse result)))
2545 ;;; moving by subdirectories
2547 ;;;###autoload
2548 (defun dired-prev-subdir (arg &optional no-error-if-not-found no-skip)
2549 "Go to previous subdirectory, regardless of level.
2550 When called interactively and not on a subdir line, go to this subdir's line."
2551 ;;(interactive "p")
2552 (interactive
2553 (list (if current-prefix-arg
2554 (prefix-numeric-value current-prefix-arg)
2555 ;; if on subdir start already, don't stay there!
2556 (if (dired-get-subdir) 1 0))))
2557 (dired-next-subdir (- arg) no-error-if-not-found no-skip))
2559 (defun dired-subdir-min ()
2560 (save-excursion
2561 (if (not (dired-prev-subdir 0 t t))
2562 (error "Not in a subdir!")
2563 (point))))
2565 ;;;###autoload
2566 (defun dired-goto-subdir (dir)
2567 "Go to end of header line of DIR in this dired buffer.
2568 Return value of point on success, otherwise return nil.
2569 The next char is either \\n, or \\r if DIR is hidden."
2570 (interactive
2571 (prog1 ; let push-mark display its message
2572 (list (expand-file-name
2573 (completing-read "Goto in situ directory: " ; prompt
2574 dired-subdir-alist ; table
2575 nil ; predicate
2576 t ; require-match
2577 (dired-current-directory))))
2578 (push-mark)))
2579 (setq dir (file-name-as-directory dir))
2580 (let ((elt (assoc dir dired-subdir-alist)))
2581 (and elt
2582 (goto-char (dired-get-subdir-min elt))
2583 ;; dired-subdir-hidden-p and dired-add-entry depend on point being
2584 ;; at either \r or \n after this function succeeds.
2585 (progn (skip-chars-forward "^\r\n")
2586 (point)))))
2588 ;;;###autoload
2589 (defun dired-mark-subdir-files ()
2590 "Mark all files except `.' and `..' in current subdirectory.
2591 If the Dired buffer shows multiple directories, this command
2592 marks the files listed in the subdirectory that point is in."
2593 (interactive)
2594 (let ((p-min (dired-subdir-min)))
2595 (dired-mark-files-in-region p-min (dired-subdir-max))))
2597 ;;;###autoload
2598 (defun dired-kill-subdir (&optional remember-marks)
2599 "Remove all lines of current subdirectory.
2600 Lower levels are unaffected."
2601 ;; With optional REMEMBER-MARKS, return a mark-alist.
2602 (interactive)
2603 (let* ((beg (dired-subdir-min))
2604 (end (dired-subdir-max))
2605 (modflag (buffer-modified-p))
2606 (cur-dir (dired-current-directory))
2607 (cons (assoc-string cur-dir dired-switches-alist))
2608 buffer-read-only)
2609 (when (equal cur-dir (expand-file-name default-directory))
2610 (error "Attempt to kill top level directory"))
2611 (prog1
2612 (if remember-marks (dired-remember-marks beg end))
2613 (delete-region beg end)
2614 (if (eobp) ; don't leave final blank line
2615 (delete-char -1))
2616 (dired-unsubdir cur-dir)
2617 (when cons
2618 (setq dired-switches-alist (delete cons dired-switches-alist)))
2619 (restore-buffer-modified-p modflag))))
2621 (defun dired-unsubdir (dir)
2622 ;; Remove DIR from the alist
2623 (setq dired-subdir-alist
2624 (delq (assoc dir dired-subdir-alist) dired-subdir-alist)))
2626 ;;;###autoload
2627 (defun dired-tree-up (arg)
2628 "Go up ARG levels in the dired tree."
2629 (interactive "p")
2630 (let ((dir (dired-current-directory)))
2631 (while (>= arg 1)
2632 (setq arg (1- arg)
2633 dir (file-name-directory (directory-file-name dir))))
2634 ;;(setq dir (expand-file-name dir))
2635 (or (dired-goto-subdir dir)
2636 (error "Cannot go up to %s - not in this tree" dir))))
2638 ;;;###autoload
2639 (defun dired-tree-down ()
2640 "Go down in the dired tree."
2641 (interactive)
2642 (let ((dir (dired-current-directory)) ; has slash
2643 pos case-fold-search) ; filenames are case sensitive
2644 (let ((rest (reverse dired-subdir-alist)) elt)
2645 (while rest
2646 (setq elt (car rest)
2647 rest (cdr rest))
2648 (if (dired-in-this-tree (directory-file-name (car elt)) dir)
2649 (setq rest nil
2650 pos (dired-goto-subdir (car elt))))))
2651 (if pos
2652 (goto-char pos)
2653 (error "At the bottom"))))
2655 ;;; hiding
2657 (defun dired-unhide-subdir ()
2658 (let (buffer-read-only)
2659 (subst-char-in-region (dired-subdir-min) (dired-subdir-max) ?\r ?\n)))
2661 (defun dired-hide-check ()
2662 (or selective-display
2663 (error "selective-display must be t for subdir hiding to work!")))
2665 (defun dired-subdir-hidden-p (dir)
2666 (and selective-display
2667 (save-excursion
2668 (dired-goto-subdir dir)
2669 (= (following-char) ?\r))))
2671 ;;;###autoload
2672 (defun dired-hide-subdir (arg)
2673 "Hide or unhide the current subdirectory and move to next directory.
2674 Optional prefix arg is a repeat factor.
2675 Use \\[dired-hide-all] to (un)hide all directories."
2676 (interactive "p")
2677 (dired-hide-check)
2678 (let ((modflag (buffer-modified-p)))
2679 (while (>= (setq arg (1- arg)) 0)
2680 (let* ((cur-dir (dired-current-directory))
2681 (hidden-p (dired-subdir-hidden-p cur-dir))
2682 (elt (assoc cur-dir dired-subdir-alist))
2683 (end-pos (1- (dired-get-subdir-max elt)))
2684 buffer-read-only)
2685 ;; keep header line visible, hide rest
2686 (goto-char (dired-get-subdir-min elt))
2687 (skip-chars-forward "^\n\r")
2688 (if hidden-p
2689 (subst-char-in-region (point) end-pos ?\r ?\n)
2690 (subst-char-in-region (point) end-pos ?\n ?\r)))
2691 (dired-next-subdir 1 t))
2692 (restore-buffer-modified-p modflag)))
2694 ;;;###autoload
2695 (defun dired-hide-all (&optional ignored)
2696 "Hide all subdirectories, leaving only their header lines.
2697 If there is already something hidden, make everything visible again.
2698 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2699 (interactive "P")
2700 (dired-hide-check)
2701 (let ((modflag (buffer-modified-p))
2702 buffer-read-only)
2703 (if (save-excursion
2704 (goto-char (point-min))
2705 (search-forward "\r" nil t))
2706 ;; unhide - bombs on \r in filenames
2707 (subst-char-in-region (point-min) (point-max) ?\r ?\n)
2708 ;; hide
2709 (let ((pos (point-max)) ; pos of end of last directory
2710 (alist dired-subdir-alist))
2711 (while alist ; while there are dirs before pos
2712 (subst-char-in-region (dired-get-subdir-min (car alist)) ; pos of prev dir
2713 (save-excursion
2714 (goto-char pos) ; current dir
2715 ;; we're somewhere on current dir's line
2716 (forward-line -1)
2717 (point))
2718 ?\n ?\r)
2719 (setq pos (dired-get-subdir-min (car alist))) ; prev dir gets current dir
2720 (setq alist (cdr alist)))))
2721 (restore-buffer-modified-p modflag)))
2723 ;;;###end dired-ins.el
2726 ;; Search only in file names in the Dired buffer.
2728 (defcustom dired-isearch-filenames nil
2729 "Non-nil to Isearch in file names only.
2730 If t, Isearch in Dired always matches only file names.
2731 If `dwim', Isearch matches file names when initial point position is on
2732 a file name. Otherwise, it searches the whole buffer without restrictions."
2733 :type '(choice (const :tag "No restrictions" nil)
2734 (const :tag "When point is on a file name initially, search file names" dwim)
2735 (const :tag "Always search in file names" t))
2736 :group 'dired
2737 :version "23.1")
2739 (define-minor-mode dired-isearch-filenames-mode
2740 "Toggle file names searching on or off.
2741 When on, Isearch skips matches outside file names using the predicate
2742 `dired-isearch-filter-filenames' that matches only at file names.
2743 When off, it uses the original predicate."
2744 nil nil nil
2745 (if dired-isearch-filenames-mode
2746 (add-function :before-while (local 'isearch-filter-predicate)
2747 #'dired-isearch-filter-filenames
2748 '((isearch-message-prefix . "filename ")))
2749 (remove-function (local 'isearch-filter-predicate)
2750 #'dired-isearch-filter-filenames))
2751 (when isearch-mode
2752 (setq isearch-success t isearch-adjusted t)
2753 (isearch-update)))
2755 ;;;###autoload
2756 (defun dired-isearch-filenames-setup ()
2757 "Set up isearch to search in Dired file names.
2758 Intended to be added to `isearch-mode-hook'."
2759 (when (or (eq dired-isearch-filenames t)
2760 (and (eq dired-isearch-filenames 'dwim)
2761 (get-text-property (point) 'dired-filename)))
2762 (define-key isearch-mode-map "\M-sff" 'dired-isearch-filenames-mode)
2763 (dired-isearch-filenames-mode 1)
2764 (add-hook 'isearch-mode-end-hook 'dired-isearch-filenames-end nil t)))
2766 (defun dired-isearch-filenames-end ()
2767 "Clean up the Dired file name search after terminating isearch."
2768 (define-key isearch-mode-map "\M-sff" nil)
2769 (dired-isearch-filenames-mode -1)
2770 (remove-hook 'isearch-mode-end-hook 'dired-isearch-filenames-end t)
2771 (unless isearch-suspended
2772 (custom-reevaluate-setting 'dired-isearch-filenames)))
2774 (defun dired-isearch-filter-filenames (beg end)
2775 "Test whether some part of the current search match is inside a file name.
2776 This function returns non-nil if some part of the text between BEG and END
2777 is part of a file name (i.e., has the text property `dired-filename')."
2778 (text-property-not-all (min beg end) (max beg end)
2779 'dired-filename nil))
2781 ;;;###autoload
2782 (defun dired-isearch-filenames ()
2783 "Search for a string using Isearch only in file names in the Dired buffer."
2784 (interactive)
2785 (setq dired-isearch-filenames t)
2786 (isearch-forward nil t))
2788 ;;;###autoload
2789 (defun dired-isearch-filenames-regexp ()
2790 "Search for a regexp using Isearch only in file names in the Dired buffer."
2791 (interactive)
2792 (setq dired-isearch-filenames t)
2793 (isearch-forward-regexp nil t))
2796 ;; Functions for searching in tags style among marked files.
2798 ;;;###autoload
2799 (defun dired-do-isearch ()
2800 "Search for a string through all marked files using Isearch."
2801 (interactive)
2802 (multi-isearch-files
2803 (dired-get-marked-files nil nil 'dired-nondirectory-p nil t)))
2805 ;;;###autoload
2806 (defun dired-do-isearch-regexp ()
2807 "Search for a regexp through all marked files using Isearch."
2808 (interactive)
2809 (multi-isearch-files-regexp
2810 (dired-get-marked-files nil nil 'dired-nondirectory-p nil t)))
2812 ;;;###autoload
2813 (defun dired-do-search (regexp)
2814 "Search through all marked files for a match for REGEXP.
2815 Stops when a match is found.
2816 To continue searching for next match, use command \\[tags-loop-continue]."
2817 (interactive "sSearch marked files (regexp): ")
2818 (tags-search regexp '(dired-get-marked-files nil nil 'dired-nondirectory-p)))
2820 ;;;###autoload
2821 (defun dired-do-query-replace-regexp (from to &optional delimited)
2822 "Do `query-replace-regexp' of FROM with TO, on all marked files.
2823 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
2824 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
2825 with the command \\[tags-loop-continue]."
2826 (interactive
2827 (let ((common
2828 (query-replace-read-args
2829 "Query replace regexp in marked files" t t)))
2830 (list (nth 0 common) (nth 1 common) (nth 2 common))))
2831 (dolist (file (dired-get-marked-files nil nil 'dired-nondirectory-p nil t))
2832 (let ((buffer (get-file-buffer file)))
2833 (if (and buffer (with-current-buffer buffer
2834 buffer-read-only))
2835 (error "File `%s' is visited read-only" file))))
2836 (tags-query-replace from to delimited
2837 '(dired-get-marked-files nil nil 'dired-nondirectory-p)))
2839 (declare-function xref--show-xrefs "xref")
2840 (declare-function xref-query-replace-in-results "xref")
2842 ;;;###autoload
2843 (defun dired-do-find-regexp (regexp)
2844 "Find all matches for REGEXP in all marked files.
2845 For any marked directory, all of its files are searched recursively.
2846 However, files matching `grep-find-ignored-files' and subdirectories
2847 matching `grep-find-ignored-directories' are skipped in the marked
2848 directories.
2850 REGEXP should use constructs supported by your local `grep' command."
2851 (interactive "sSearch marked files (regexp): ")
2852 (require 'grep)
2853 (defvar grep-find-ignored-files)
2854 (defvar grep-find-ignored-directories)
2855 (let* ((files (dired-get-marked-files nil nil nil nil t))
2856 (ignores (nconc (mapcar
2857 (lambda (s) (concat s "/"))
2858 grep-find-ignored-directories)
2859 grep-find-ignored-files))
2860 (xrefs (mapcan
2861 (lambda (file)
2862 (xref-collect-matches regexp "*" file
2863 (and (file-directory-p file)
2864 ignores)))
2865 files)))
2866 (unless xrefs
2867 (user-error "No matches for: %s" regexp))
2868 (xref--show-xrefs xrefs nil t)))
2870 ;;;###autoload
2871 (defun dired-do-find-regexp-and-replace (from to)
2872 "Replace matches of FROM with TO, in all marked files.
2873 For any marked directory, matches in all of its files are replaced,
2874 recursively. However, files matching `grep-find-ignored-files'
2875 and subdirectories matching `grep-find-ignored-directories' are skipped
2876 in the marked directories.
2878 REGEXP should use constructs supported by your local `grep' command."
2879 (interactive
2880 (let ((common
2881 (query-replace-read-args
2882 "Query replace regexp in marked files" t t)))
2883 (list (nth 0 common) (nth 1 common))))
2884 (with-current-buffer (dired-do-find-regexp from)
2885 (xref-query-replace-in-results from to)))
2887 (defun dired-nondirectory-p (file)
2888 (not (file-directory-p file)))
2890 ;;;###autoload
2891 (defun dired-show-file-type (file &optional deref-symlinks)
2892 "Print the type of FILE, according to the `file' command.
2893 If you give a prefix to this command, and FILE is a symbolic
2894 link, then the type of the file linked to by FILE is printed
2895 instead."
2896 (interactive (list (dired-get-filename t) current-prefix-arg))
2897 (let (process-file-side-effects)
2898 (with-temp-buffer
2899 (if deref-symlinks
2900 (process-file "file" nil t t "-L" "--" file)
2901 (process-file "file" nil t t "--" file))
2902 (when (bolp)
2903 (backward-delete-char 1))
2904 (message "%s" (buffer-string)))))
2906 (provide 'dired-aux)
2908 ;; Local Variables:
2909 ;; byte-compile-dynamic: t
2910 ;; generated-autoload-file: "dired-loaddefs.el"
2911 ;; End:
2913 ;;; dired-aux.el ends here