1 ;;; dired-aux.el --- less commonly used parts of dired
3 ;; Copyright (C) 1985-1986, 1992, 1994, 1998, 2000-2016 Free Software
6 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>.
7 ;; Maintainer: emacs-devel@gnu.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; The parts of dired mode not normally used. This is a space-saving hack
29 ;; to avoid having to load a large mode when all that's wanted are a few
32 ;; Rewritten in 1990/1991 to add tree features, file marking and
33 ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
34 ;; Finished up by rms in 1992.
38 ;; We need macros in dired.el to compile properly,
39 ;; and we call subroutines in it too.
42 (defvar dired-create-files-failures nil
43 "Variable where `dired-create-files' records failing file names.
44 Functions that operate recursively can store additional names
45 into this list; they also should call `dired-log' to log the errors.")
48 ;;;###begin dired-cmd.el
49 ;; Diffing and compressing
51 (defconst dired-star-subst-regexp
"\\(^\\|[ \t]\\)\\*\\([ \t]\\|$\\)")
52 (defconst dired-quark-subst-regexp
"\\(^\\|[ \t]\\)\\?\\([ \t]\\|$\\)")
55 (defun dired-diff (file &optional switches
)
56 "Compare file at point with file FILE using `diff'.
57 If called interactively, prompt for FILE. If the file at point
58 has a backup file, use that as the default. If the file at point
59 is a backup file, use its original. If the mark is active
60 in Transient Mark mode, use the file at the mark as the default.
61 \(That's the mark set by \\[set-mark-command], not by Dired's
62 \\[dired-mark] command.)
64 FILE is the first file given to `diff'. The file at point
65 is the second file given to `diff'.
67 With prefix arg, prompt for second argument SWITCHES, which is
68 the string of command switches for the third argument of `diff'."
70 (let* ((current (dired-get-filename t
))
71 ;; Get the latest existing backup file or its original.
72 (oldf (if (backup-file-name-p current
)
73 (file-name-sans-versions current
)
74 (diff-latest-backup-file current
)))
75 ;; Get the file at the mark.
76 (file-at-mark (if (and transient-mark-mode mark-active
)
77 (save-excursion (goto-char (mark t
))
78 (dired-get-filename t t
))))
79 (default-file (or file-at-mark
80 (and oldf
(file-name-nondirectory oldf
))))
81 ;; Use it as default if it's not the same as the current file,
82 ;; and the target dir is current or there is a default file.
83 (default (if (and (not (equal default-file current
))
84 (or (equal (dired-dwim-target-directory)
85 (dired-current-directory))
88 (target-dir (if default
89 (dired-current-directory)
90 (dired-dwim-target-directory)))
91 (defaults (dired-dwim-target-defaults (list current
) target-dir
)))
93 (minibuffer-with-setup-hook
95 (set (make-local-variable 'minibuffer-default-add-function
) nil
)
96 (setq minibuffer-default defaults
))
98 (format "Diff %s with%s: " current
99 (if default
(format " (default %s)" default
) ""))
100 target-dir default t
))
101 (if current-prefix-arg
102 (read-string "Options for diff: "
103 (if (stringp diff-switches
)
105 (mapconcat 'identity diff-switches
" ")))))))
106 (let ((current (dired-get-filename t
)))
107 (when (or (equal (expand-file-name file
)
108 (expand-file-name current
))
109 (and (file-directory-p file
)
110 (equal (expand-file-name current file
)
111 (expand-file-name current
))))
112 (error "Attempt to compare the file to itself"))
113 (if (and (backup-file-name-p current
)
114 (equal file
(file-name-sans-versions current
)))
115 (diff current file switches
)
116 (diff file current switches
))))
119 (defun dired-backup-diff (&optional switches
)
120 "Diff this file with its backup file or vice versa.
121 Uses the latest backup, if there are several numerical backups.
122 If this file is a backup, diff it with its original.
123 The backup file is the first file given to `diff'.
124 With prefix arg, prompt for argument SWITCHES which is options for `diff'."
126 (if current-prefix-arg
127 (list (read-string "Options for diff: "
128 (if (stringp diff-switches
)
130 (mapconcat 'identity diff-switches
" "))))
132 (diff-backup (dired-get-filename) switches
))
135 (defun dired-compare-directories (dir2 predicate
)
136 "Mark files with different file attributes in two dired buffers.
137 Compare file attributes of files in the current directory
138 with file attributes in directory DIR2 using PREDICATE on pairs of files
139 with the same name. Mark files for which PREDICATE returns non-nil.
140 Mark files with different names if PREDICATE is nil (or interactively
141 with empty input at the predicate prompt).
143 PREDICATE is a Lisp expression that can refer to the following variables:
145 size1, size2 - file size in bytes
146 mtime1, mtime2 - last modification time in seconds, as a float
147 fa1, fa2 - list of file attributes
148 returned by function `file-attributes'
150 where 1 refers to attribute of file in the current dired buffer
151 and 2 to attribute of file in second dired buffer.
153 Examples of PREDICATE:
155 (> mtime1 mtime2) - mark newer files
156 (not (= size1 size2)) - mark files with different sizes
157 (not (string= (nth 8 fa1) (nth 8 fa2))) - mark files with different modes
158 (not (and (= (nth 2 fa1) (nth 2 fa2)) - mark files with different UID
159 (= (nth 3 fa1) (nth 3 fa2)))) and GID."
162 (let* ((target-dir (dired-dwim-target-directory))
163 (defaults (dired-dwim-target-defaults nil target-dir
)))
164 (minibuffer-with-setup-hook
166 (set (make-local-variable 'minibuffer-default-add-function
) nil
)
167 (setq minibuffer-default defaults
))
168 (read-directory-name (format "Compare %s with: "
169 (dired-current-directory))
170 target-dir target-dir t
)))
171 (read-from-minibuffer "Mark if (lisp expr or RET): " nil nil t nil
"nil")))
172 (let* ((dir1 (dired-current-directory))
173 (file-alist1 (dired-files-attributes dir1
))
174 (file-alist2 (dired-files-attributes dir2
))
175 file-list1 file-list2
)
176 (setq file-alist1
(delq (assoc "." file-alist1
) file-alist1
))
177 (setq file-alist1
(delq (assoc ".." file-alist1
) file-alist1
))
178 (setq file-alist2
(delq (assoc "." file-alist2
) file-alist2
))
179 (setq file-alist2
(delq (assoc ".." file-alist2
) file-alist2
))
180 (setq file-list1
(mapcar
182 (dired-file-set-difference
183 file-alist1 file-alist2
187 (dired-file-set-difference
188 file-alist2 file-alist1
190 (dired-fun-in-all-buffers
194 (member (dired-get-filename nil t
) file-list1
) nil
)))
195 (dired-fun-in-all-buffers
199 (member (dired-get-filename nil t
) file-list2
) nil
)))
200 (message "Marked in dir1: %s files, in dir2: %s files"
202 (length file-list2
))))
204 (defun dired-file-set-difference (list1 list2 predicate
)
205 "Combine LIST1 and LIST2 using a set-difference operation.
206 The result list contains all file items that appear in LIST1 but not LIST2.
207 This is a non-destructive function; it makes a copy of the data if necessary
208 to avoid corrupting the original LIST1 and LIST2.
209 PREDICATE (see `dired-compare-directories') is an additional match
210 condition. Two file items are considered to match if they are equal
211 *and* PREDICATE evaluates to t."
212 (if (or (null list1
) (null list2
))
215 (dolist (file1 list1
)
216 (unless (let ((list list2
))
218 (let* ((file2 (car list
))
219 (fa1 (car (cddr file1
)))
220 (fa2 (car (cddr file2
))))
222 (not (equal (car file1
) (car file2
)))
226 (size1 .
,(nth 7 fa1
))
227 (size2 .
,(nth 7 fa2
))
229 .
,(float-time (nth 5 fa1
)))
231 .
,(float-time (nth 5 fa2
)))
233 (setq list
(cdr list
)))
238 (defun dired-files-attributes (dir)
239 "Return a list of all file names and attributes from DIR.
240 List has a form of (file-name full-file-name (attribute-list))."
243 (let ((full-file-name (expand-file-name file-name dir
)))
246 (file-attributes full-file-name
))))
247 (directory-files dir
)))
249 ;;; Change file attributes
251 (defun dired-do-chxxx (attribute-name program op-symbol arg
)
252 ;; Change file attributes (group, owner, timestamp) of marked files and
253 ;; refresh their file lines.
254 ;; ATTRIBUTE-NAME is a string describing the attribute to the user.
255 ;; PROGRAM is the program used to change the attribute.
256 ;; OP-SYMBOL is the type of operation (for use in `dired-mark-pop-up').
257 ;; ARG describes which files to use, as in `dired-get-marked-files'.
258 (let* ((files (dired-get-marked-files t arg
))
259 ;; The source of default file attributes is the file at point.
260 (default-file (dired-get-filename t t
))
261 (default (when default-file
262 (cond ((eq op-symbol
'touch
)
265 (nth 5 (file-attributes default-file
))))
266 ((eq op-symbol
'chown
)
267 (nth 2 (file-attributes default-file
'string
)))
268 ((eq op-symbol
'chgrp
)
269 (nth 3 (file-attributes default-file
'string
))))))
270 (prompt (concat "Change " attribute-name
" of %s to"
271 (if (eq op-symbol
'touch
)
274 (new-attribute (dired-mark-read-string prompt nil op-symbol
276 (cond ((eq op-symbol
'chown
)
278 ((eq op-symbol
'chgrp
)
280 (operation (concat program
" " new-attribute
))
283 (dired-bunch-files 10000
284 (function dired-check-process
)
286 (list operation program
)
287 (unless (or (string-equal new-attribute
"")
288 ;; Use `eq' instead of `equal'
289 ;; to detect empty input (bug#12399).
290 (eq new-attribute default
))
291 (if (eq op-symbol
'touch
)
292 (list "-t" new-attribute
)
293 (list new-attribute
)))
294 (if (string-match-p "gnu" system-configuration
)
297 (dired-do-redisplay arg
);; moves point if ARG is an integer
300 (format "%s: error" operation
)
304 (defun dired-do-chmod (&optional arg
)
305 "Change the mode of the marked (or next ARG) files.
306 Symbolic modes like `g+w' are allowed.
307 Type M-n to pull the file attributes of the file at point
308 into the minibuffer."
310 (let* ((files (dired-get-marked-files t arg
))
311 ;; The source of default file attributes is the file at point.
312 (default-file (dired-get-filename t t
))
313 (modestr (when default-file
314 (nth 8 (file-attributes default-file
))))
316 (and (stringp modestr
)
317 (string-match "^.\\(...\\)\\(...\\)\\(...\\)$" modestr
)
318 (replace-regexp-in-string
320 (format "u=%s,g=%s,o=%s"
321 (match-string 1 modestr
)
322 (match-string 2 modestr
)
323 (match-string 3 modestr
)))))
324 (modes (dired-mark-read-string
325 "Change mode of %s to: "
326 nil
'chmod arg files default
))
328 (cond ((or (equal modes
"")
329 ;; Use `eq' instead of `equal'
330 ;; to detect empty input (bug#12399).
332 ;; We used to treat empty input as DEFAULT, but that is not
333 ;; such a good idea (Bug#9361).
334 (error "No file mode specified"))
335 ((string-match-p "^[0-7]+" modes
)
336 (setq num-modes
(string-to-number modes
8))))
341 (if num-modes num-modes
342 (file-modes-symbolic-to-number modes
(file-modes file
)))))
343 (dired-do-redisplay arg
)))
346 (defun dired-do-chgrp (&optional arg
)
347 "Change the group of the marked (or next ARG) files.
348 Type M-n to pull the file attributes of the file at point
349 into the minibuffer."
351 (if (memq system-type
'(ms-dos windows-nt
))
352 (error "chgrp not supported on this system"))
353 (dired-do-chxxx "Group" "chgrp" 'chgrp arg
))
356 (defun dired-do-chown (&optional arg
)
357 "Change the owner of the marked (or next ARG) files.
358 Type M-n to pull the file attributes of the file at point
359 into the minibuffer."
361 (if (memq system-type
'(ms-dos windows-nt
))
362 (error "chown not supported on this system"))
363 (dired-do-chxxx "Owner" dired-chown-program
'chown arg
))
366 (defun dired-do-touch (&optional arg
)
367 "Change the timestamp of the marked (or next ARG) files.
369 Type M-n to pull the file attributes of the file at point
370 into the minibuffer."
372 (dired-do-chxxx "Timestamp" dired-touch-program
'touch arg
))
374 ;; Process all the files in FILES in batches of a convenient size,
375 ;; by means of (FUNCALL FUNCTION ARGS... SOME-FILES...).
376 ;; Batches are chosen to need less than MAX chars for the file names,
377 ;; allowing 3 extra characters of separator per file name.
378 (defun dired-bunch-files (max function args files
)
383 ;; Accumulate files as long as they fit in MAX chars,
384 ;; then process the ones accumulated so far.
386 (let* ((thisfile (car files
))
387 (thislength (+ (length thisfile
) 3))
389 ;; If we have at least 1 pending file
390 ;; and this file won't fit in the length limit, process now.
391 (if (and pending
(> (+ thislength pending-length
) max
))
392 (setq pending
(nreverse pending
)
393 ;; The elements of PENDING are now in forward order.
394 ;; Do the operation and record failures.
395 failures
(nconc (apply function
(append args pending
))
397 ;; Transfer the elements of PENDING onto PAST
398 ;; and clear it out. Now PAST contains the first N files
399 ;; specified (for some N), and FILES contains the rest.
400 past
(nconc past pending
)
403 ;; Do (setq pending (cons thisfile pending))
404 ;; but reuse the cons that was in `files'.
405 (setcdr files pending
)
407 (setq pending-length
(+ thislength pending-length
))
409 (setq pending
(nreverse pending
))
411 (nconc (apply function
(append args pending
))
413 ;; Now the original list FILES has been put back as it was.
414 (nconc past pending
))))
416 (defvar lpr-printer-switch
)
419 (defun dired-do-print (&optional arg
)
420 "Print the marked (or next ARG) files.
421 Uses the shell command coming from variables `lpr-command' and
422 `lpr-switches' as default."
425 (let* ((file-list (dired-get-marked-files t arg
))
427 (if (and (stringp printer-name
)
428 (string< "" printer-name
))
429 (cons (concat lpr-printer-switch printer-name
)
432 (command (dired-mark-read-string
436 (if (stringp lpr-switches
)
440 'print arg file-list
)))
441 (dired-run-shell-command (dired-shell-stuff-it command file-list nil
))))
443 (defun dired-mark-read-string (prompt initial op-symbol arg files
444 &optional default-value collection
)
445 "Read args for a Dired marked-files command, prompting with PROMPT.
446 Return the user input (a string).
448 INITIAL, if non-nil, is the initial minibuffer input.
449 OP-SYMBOL is an operation symbol (see `dired-no-confirm').
450 ARG is normally the prefix argument for the calling command;
451 it is passed as the first argument to `dired-mark-prompt'.
452 FILES should be a list of marked files' names.
454 Optional arg DEFAULT-VALUE is a default value or list of default
455 values, passed as the seventh arg to `completing-read'.
457 Optional arg COLLECTION is a collection of possible completions,
458 passed as the second arg to `completing-read'."
459 (dired-mark-pop-up nil op-symbol files
461 (format prompt
(dired-mark-prompt arg files
))
462 collection nil nil initial nil default-value nil
))
464 ;;; Cleaning a directory: flagging some backups for deletion.
466 (defvar dired-file-version-alist
)
469 (defun dired-clean-directory (keep)
470 "Flag numerical backups for deletion.
471 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
472 Positive prefix arg KEEP overrides `dired-kept-versions';
473 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
475 To clear the flags on these files, you can use \\[dired-flag-backup-files]
476 with a prefix argument."
478 (setq keep
(if keep
(prefix-numeric-value keep
) dired-kept-versions
))
479 (let ((early-retention (if (< keep
0) (- keep
) kept-old-versions
))
480 (late-retention (if (<= keep
0) dired-kept-versions keep
))
481 (dired-file-version-alist ()))
482 (message "Cleaning numerical backups (keeping %d late, %d old)..."
483 late-retention early-retention
)
484 ;; Look at each file.
485 ;; If the file has numeric backup versions,
486 ;; put on dired-file-version-alist an element of the form
487 ;; (FILENAME . VERSION-NUMBER-LIST)
488 (dired-map-dired-file-lines (function dired-collect-file-versions
))
489 ;; Sort each VERSION-NUMBER-LIST,
490 ;; and remove the versions not to be deleted.
491 (let ((fval dired-file-version-alist
))
493 (let* ((sorted-v-list (cons 'q
(sort (cdr (car fval
)) '<)))
494 (v-count (length sorted-v-list
)))
495 (if (> v-count
(+ early-retention late-retention
))
496 (rplacd (nthcdr early-retention sorted-v-list
)
497 (nthcdr (- v-count late-retention
)
500 (cdr sorted-v-list
)))
501 (setq fval
(cdr fval
))))
502 ;; Look at each file. If it is a numeric backup file,
503 ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
504 (dired-map-dired-file-lines (function dired-trample-file-versions
))
505 (message "Cleaning numerical backups...done")))
507 ;;; Subroutines of dired-clean-directory.
509 (defun dired-map-dired-file-lines (fun)
510 ;; Perform FUN with point at the end of each non-directory line.
511 ;; FUN takes one argument, the absolute filename.
513 (let (file buffer-read-only
)
514 (goto-char (point-min))
517 (and (not (looking-at-p dired-re-dir
))
519 (setq file
(dired-get-filename nil t
)) ; nil on non-file
521 (funcall fun file
))))
524 (defvar backup-extract-version-start
) ; used in backup-extract-version
526 (defun dired-collect-file-versions (fn)
527 (let ((fn (file-name-sans-versions fn
)))
528 ;; Only do work if this file is not already in the alist.
529 (if (assoc fn dired-file-version-alist
)
531 ;; If it looks like file FN has versions, return a list of the versions.
532 ;;That is a list of strings which are file names.
533 ;;The caller may want to flag some of these files for deletion.
534 (let* ((base-versions
535 (concat (file-name-nondirectory fn
) ".~"))
536 (backup-extract-version-start (length base-versions
))
537 (possibilities (file-name-all-completions
539 (file-name-directory fn
)))
540 (versions (mapcar 'backup-extract-version possibilities
)))
542 (setq dired-file-version-alist
543 (cons (cons fn versions
)
544 dired-file-version-alist
)))))))
546 (defun dired-trample-file-versions (fn)
547 (let* ((start-vn (string-match-p "\\.~[0-9]+~$" fn
))
550 (setq base-version-list
; there was a base version to which
551 (assoc (substring fn
0 start-vn
) ; this looks like a
552 dired-file-version-alist
)) ; subversion
553 (not (memq (string-to-number (substring fn
(+ 2 start-vn
)))
554 base-version-list
)) ; this one doesn't make the cut
555 (progn (beginning-of-line)
557 (insert dired-del-marker
)))))
561 (declare-function mailcap-file-default-commands
"mailcap" (files))
563 (defun minibuffer-default-add-dired-shell-commands ()
564 "Return a list of all commands associated with current dired files.
565 This function is used to add all related commands retrieved by `mailcap'
566 to the end of the list of defaults just after the default value."
568 (let ((commands (and (boundp 'files
) (require 'mailcap nil t
)
569 (mailcap-file-default-commands files
))))
570 (if (listp minibuffer-default
)
571 (append minibuffer-default commands
)
572 (cons minibuffer-default commands
))))
574 ;; This is an extra function so that you can redefine it, e.g., to use gmhist.
575 (defun dired-read-shell-command (prompt arg files
)
576 "Read a dired shell command.
577 PROMPT should be a format string with one \"%s\" format sequence,
578 which is replaced by the value returned by `dired-mark-prompt',
579 with ARG and FILES as its arguments. FILES should be a list of
580 file names. The result is used as the prompt.
582 This normally reads using `read-shell-command', but if the
583 `dired-x' package is loaded, use `dired-guess-shell-command' to
584 offer a smarter default choice of shell command."
585 (minibuffer-with-setup-hook
587 (set (make-local-variable 'minibuffer-default-add-function
)
588 'minibuffer-default-add-dired-shell-commands
))
589 (setq prompt
(format prompt
(dired-mark-prompt arg files
)))
590 (if (functionp 'dired-guess-shell-command
)
591 (dired-mark-pop-up nil
'shell files
592 'dired-guess-shell-command prompt files
)
593 (dired-mark-pop-up nil
'shell files
594 'read-shell-command prompt nil nil
))))
597 (defun dired-do-async-shell-command (command &optional arg file-list
)
598 "Run a shell command COMMAND on the marked files asynchronously.
600 Like `dired-do-shell-command', but adds `&' at the end of COMMAND
601 to execute it asynchronously.
603 When operating on multiple files, asynchronous commands
604 are executed in the background on each file in parallel.
605 In shell syntax this means separating the individual commands
606 with `&'. However, when COMMAND ends in `;' or `;&' then commands
607 are executed in the background on each file sequentially waiting
608 for each command to terminate before running the next command.
609 In shell syntax this means separating the individual commands with `;'.
611 The output appears in the buffer `*Async Shell Command*'."
613 (let ((files (dired-get-marked-files t current-prefix-arg
)))
615 ;; Want to give feedback whether this file or marked files are used:
616 (dired-read-shell-command "& on %s: " current-prefix-arg files
)
619 (unless (string-match-p "&[ \t]*\\'" command
)
620 (setq command
(concat command
" &")))
621 (dired-do-shell-command command arg file-list
))
624 (defun dired-do-shell-command (command &optional arg file-list
)
625 "Run a shell command COMMAND on the marked files.
626 If no files are marked or a numeric prefix arg is given,
627 the next ARG files are used. Just \\[universal-argument] means the current file.
628 The prompt mentions the file(s) or the marker, as appropriate.
630 If there is a `*' in COMMAND, surrounded by whitespace, this runs
631 COMMAND just once with the entire file list substituted there.
633 If there is no `*', but there is a `?' in COMMAND, surrounded by
634 whitespace, this runs COMMAND on each file individually with the
635 file name substituted for `?'.
637 Otherwise, this runs COMMAND on each file individually with the
638 file name added at the end of COMMAND (separated by a space).
640 `*' and `?' when not surrounded by whitespace have no special
641 significance for `dired-do-shell-command', and are passed through
642 normally to the shell, but you must confirm first.
644 If you want to use `*' as a shell wildcard with whitespace around
645 it, write `*\"\"' in place of just `*'. This is equivalent to just
646 `*' in the shell, but avoids Dired's special handling.
648 If COMMAND ends in `&', `;', or `;&', it is executed in the
649 background asynchronously, and the output appears in the buffer
650 `*Async Shell Command*'. When operating on multiple files and COMMAND
651 ends in `&', the shell command is executed on each file in parallel.
652 However, when COMMAND ends in `;' or `;&' then commands are executed
653 in the background on each file sequentially waiting for each command
654 to terminate before running the next command. You can also use
655 `dired-do-async-shell-command' that automatically adds `&'.
657 Otherwise, COMMAND is executed synchronously, and the output
658 appears in the buffer `*Shell Command Output*'.
660 This feature does not try to redisplay Dired buffers afterward, as
661 there's no telling what files COMMAND may have changed.
662 Type \\[dired-do-redisplay] to redisplay the marked files.
664 When COMMAND runs, its working directory is the top-level directory
665 of the Dired buffer, so output files usually are created there
666 instead of in a subdir.
668 In a noninteractive call (from Lisp code), you must specify
669 the list of file names explicitly with the FILE-LIST argument, which
670 can be produced by `dired-get-marked-files', for example."
671 ;;Functions dired-run-shell-command and dired-shell-stuff-it do the
672 ;;actual work and can be redefined for customization.
674 (let ((files (dired-get-marked-files t current-prefix-arg
)))
676 ;; Want to give feedback whether this file or marked files are used:
677 (dired-read-shell-command "! on %s: " current-prefix-arg files
)
680 (let* ((on-each (not (string-match-p dired-star-subst-regexp command
)))
681 (no-subst (not (string-match-p dired-quark-subst-regexp command
)))
682 (star (string-match-p "\\*" command
))
683 (qmark (string-match-p "\\?" command
)))
684 ;; Get confirmation for wildcards that may have been meant
685 ;; to control substitution of a file name or the file name list.
686 (if (cond ((not (or on-each no-subst
))
687 (error "You can not combine `*' and `?' substitution marks"))
689 (y-or-n-p (format-message
690 "Confirm--do you mean to use `*' as a wildcard? ")))
691 ((and qmark no-subst
)
692 (y-or-n-p (format-message
693 "Confirm--do you mean to use `?' as a wildcard? ")))
697 (- 10000 (length command
))
698 (function (lambda (&rest files
)
699 (dired-run-shell-command
700 (dired-shell-stuff-it command files t arg
))))
703 ;; execute the shell command
704 (dired-run-shell-command
705 (dired-shell-stuff-it command file-list nil arg
))))))
707 ;; Might use {,} for bash or csh:
708 (defvar dired-mark-prefix
""
709 "Prepended to marked files in dired shell commands.")
710 (defvar dired-mark-postfix
""
711 "Appended to marked files in dired shell commands.")
712 (defvar dired-mark-separator
" "
713 "Separates marked files in dired shell commands.")
715 (defun dired-shell-stuff-it (command file-list on-each
&optional _raw-arg
)
716 ;; "Make up a shell command line from COMMAND and FILE-LIST.
717 ;; If ON-EACH is t, COMMAND should be applied to each file, else
718 ;; simply concat all files and apply COMMAND to this.
719 ;; FILE-LIST's elements will be quoted for the shell."
720 ;; Might be redefined for smarter things and could then use RAW-ARG
721 ;; (coming from interactive P and currently ignored) to decide what to do.
722 ;; Smart would be a way to access basename or extension of file names.
723 (let* ((in-background (string-match "[ \t]*&[ \t]*\\'" command
))
724 (command (if in-background
725 (substring command
0 (match-beginning 0))
727 (sequentially (string-match "[ \t]*;[ \t]*\\'" command
))
728 (command (if sequentially
729 (substring command
0 (match-beginning 0))
732 (if (or (string-match-p dired-star-subst-regexp command
)
733 (string-match-p dired-quark-subst-regexp command
))
735 (let ((retval command
))
737 "\\(^\\|[ \t]\\)\\([*?]\\)\\([ \t]\\|$\\)" retval
)
738 (setq retval
(replace-match x t t retval
2)))
740 (lambda (x) (concat command dired-mark-separator x
)))))
743 (mapconcat stuff-it
(mapcar 'shell-quote-argument file-list
)
744 (if (and in-background
(not sequentially
)) "&" ";"))
745 (let ((files (mapconcat 'shell-quote-argument
746 file-list dired-mark-separator
)))
747 (if (> (length file-list
) 1)
748 (setq files
(concat dired-mark-prefix files dired-mark-postfix
)))
749 (funcall stuff-it files
)))
750 (if in-background
"&" ""))))
752 ;; This is an extra function so that it can be redefined by ange-ftp.
754 (defun dired-run-shell-command (command)
756 (find-file-name-handler (directory-file-name default-directory
)
758 (if handler
(apply handler
'shell-command
(list command
))
759 (shell-command command
)))
760 ;; Return nil for sake of nconc in dired-bunch-files.
764 (defun dired-check-process (msg program
&rest arguments
)
765 "Display MSG while running PROGRAM, and check for output.
766 Remaining arguments are strings passed as command arguments to PROGRAM.
767 On error, insert output
768 in a log buffer and return the offending ARGUMENTS or PROGRAM.
769 Caller can cons up a list of failed args.
770 Else returns nil for success."
771 (let (err-buffer err
(dir default-directory
))
772 (message "%s..." msg
)
774 ;; Get a clean buffer for error output:
775 (setq err-buffer
(get-buffer-create " *dired-check-process output*"))
776 (set-buffer err-buffer
)
778 (setq default-directory dir
; caller's default-directory
779 err
(not (eq 0 (apply 'process-file program nil t nil arguments
))))
782 (dired-log (concat program
" " (prin1-to-string arguments
) "\n"))
783 (dired-log err-buffer
)
784 (or arguments program t
))
785 (kill-buffer err-buffer
)
786 (message "%s...done" msg
)
789 (defun dired-shell-command (cmd)
790 "Run CMD, and check for output.
791 On error, pop up the log buffer.
792 Return the result of `process-file' - zero for success."
793 (let ((out-buffer " *dired-check-process output*")
794 (dir default-directory
))
795 (with-current-buffer (get-buffer-create out-buffer
)
797 (let* ((default-directory dir
)
806 (pop-to-buffer out-buffer
))
809 ;; Commands that delete or redisplay part of the dired buffer.
811 (defun dired-kill-line (&optional arg
)
812 "Kill the current line (not the files).
813 With a prefix argument, kill that many lines starting with the current line.
814 \(A negative argument kills backward.)"
816 (setq arg
(prefix-numeric-value arg
))
817 (let (buffer-read-only file
)
819 (setq file
(dired-get-filename nil t
))
821 (error "Can only kill file lines")
822 (save-excursion (and file
823 (dired-goto-subdir file
)
824 (dired-kill-subdir)))
825 (delete-region (line-beginning-position)
826 (progn (forward-line 1) (point)))
831 (dired-move-to-filename)))
834 (defun dired-do-kill-lines (&optional arg fmt
)
835 "Kill all marked lines (not the files).
836 With a prefix argument, kill that many lines starting with the current line.
837 \(A negative argument kills backward.)
838 If you use this command with a prefix argument to kill the line
839 for a file that is a directory, which you have inserted in the
840 Dired buffer as a subdirectory, then it deletes that subdirectory
841 from the buffer as well.
842 To kill an entire subdirectory \(without killing its line in the
843 parent directory), go to its directory header line and use this
844 command with a prefix argument (the value does not matter)."
845 ;; Returns count of killed lines. FMT="" suppresses message.
848 (if (dired-get-subdir)
850 (dired-kill-line arg
))
852 (goto-char (point-min))
853 (let (buffer-read-only
855 (regexp (dired-marker-regexp)))
856 (while (and (not (eobp))
857 (re-search-forward regexp nil t
))
858 (setq count
(1+ count
))
859 (delete-region (line-beginning-position)
860 (progn (forward-line 1) (point))))
862 (message (or fmt
"Killed %d line%s.") count
(dired-plural-s count
)))
865 ;;;###end dired-cmd.el
868 ;;;###begin dired-cp.el
870 (defun dired-compress ()
871 ;; Compress or uncompress the current file.
872 ;; Return nil for success, offending filename else.
873 (let* (buffer-read-only
874 (from-file (dired-get-filename))
875 (new-file (dired-compress-file from-file
)))
877 (let ((start (point)))
878 ;; Remove any preexisting entry for the name NEW-FILE.
879 (ignore-errors (dired-remove-entry new-file
))
881 ;; Now replace the current line with an entry for NEW-FILE.
882 (dired-update-file-line new-file
) nil
)
883 (dired-log (concat "Failed to compress" from-file
))
886 (defvar dired-compress-file-suffixes
888 ;; "tar -zxf" isn't used because it's not available on the
889 ;; Solaris10 version of tar. Solaris10 becomes obsolete in 2021.
890 ;; Same thing on AIX 7.1.
891 ("\\.tar\\.gz\\'" "" "gzip -dc %i | tar -xv")
892 ("\\.gz\\'" "" "gunzip")
893 ("\\.tgz\\'" ".tar" "gunzip")
894 ("\\.Z\\'" "" "uncompress")
895 ;; For .z, try gunzip. It might be an old gzip file,
896 ;; or it might be from compact? pack? (which?) but gunzip handles both.
897 ("\\.z\\'" "" "gunzip")
898 ("\\.dz\\'" "" "dictunzip")
899 ("\\.tbz\\'" ".tar" "bunzip2")
900 ("\\.bz2\\'" "" "bunzip2")
901 ("\\.xz\\'" "" "unxz")
902 ("\\.zip\\'" "" "unzip -o -d %o %i")
903 ;; This item controls naming for compression.
904 ("\\.tar\\'" ".tgz" nil
)
905 ;; This item controls the compression of directories
906 (":" ".tar.gz" "tar -c %i | gzip -c9 > %o"))
907 "Control changes in file name suffixes for compression and uncompression.
908 Each element specifies one transformation rule, and has the form:
909 (REGEXP NEW-SUFFIX PROGRAM)
910 The rule applies when the old file name matches REGEXP.
911 The new file name is computed by deleting the part that matches REGEXP
912 (as well as anything after that), then adding NEW-SUFFIX in its place.
913 If PROGRAM is non-nil, the rule is an uncompression rule,
914 and uncompression is done by running PROGRAM.
916 Within PROGRAM, %i denotes the input file, and %o denotes the
919 Otherwise, the rule is a compression rule, and compression is done with gzip.
920 ARGS are command switches passed to PROGRAM.")
922 (defvar dired-compress-files-alist
923 '(("\\.tar\\.gz\\'" .
"tar -c %i | gzip -c9 > %o")
924 ("\\.tar\\.bz2\\'" .
"tar -c %i | bzip2 -c9 > %o")
925 ("\\.tar\\.xz\\'" .
"tar -c %i | xz -c9 > %o")
926 ("\\.zip\\'" .
"zip %o -r --filesync %i"))
927 "Control the compression shell command for `dired-do-compress-to'.
929 Each element is (REGEXP . CMD), where REGEXP is the name of the
930 archive to which you want to compress, and CMD the the
931 corresponding command.
933 Within CMD, %i denotes the input file(s), and %o denotes the
934 output file. %i path(s) are relative, while %o is absolute.")
937 (defun dired-do-compress-to ()
938 "Compress selected files and directories to an archive.
939 You are prompted for the archive name.
940 The archiving command is chosen based on the archive name extension and
941 `dired-compress-files-alist'."
943 (let* ((in-files (dired-get-marked-files))
944 (out-file (read-file-name "Compress to: "))
947 (string-match (car x
) out-file
))
948 dired-compress-files-alist
)))
951 "No compression rule found for %s, see `dired-compress-files-alist'"
953 ((and (file-exists-p out-file
)
955 (format "%s exists, overwrite?"
956 (abbreviate-file-name out-file
)))))
957 (message "Compression aborted"))
961 (replace-regexp-in-string
963 (replace-regexp-in-string
964 "%i" (mapconcat #'file-name-nondirectory in-files
" ")
966 (message "Compressed %d file(s) to %s"
968 (file-name-nondirectory out-file
)))))))
971 (defun dired-compress-file (file)
972 "Compress or uncompress FILE.
973 Return the name of the compressed or uncompressed file.
974 Return nil if no change in files."
975 (let ((handler (find-file-name-handler file
'dired-compress-file
))
977 (suffixes dired-compress-file-suffixes
)
979 ;; See if any suffix rule matches this file name.
981 (let (case-fold-search)
982 (if (string-match (car (car suffixes
)) file
)
983 (setq suffix
(car suffixes
) suffixes nil
))
984 (setq suffixes
(cdr suffixes
))))
985 ;; If so, compute desired new name.
987 (setq newname
(concat (substring file
0 (match-beginning 0))
990 (funcall handler
'dired-compress-file file
))
991 ((file-symlink-p file
)
993 ((and suffix
(setq command
(nth 2 suffix
)))
994 (if (string-match "%[io]" command
)
995 (prog1 (setq newname
(file-name-as-directory newname
))
997 (replace-regexp-in-string
999 (replace-regexp-in-string
1002 ;; We found an uncompression rule.
1004 (dired-check-process
1005 (concat "Uncompressing " file
)
1010 ;; We don't recognize the file as compressed, so compress it.
1011 ;; Try gzip; if we don't have that, use compress.
1013 (if (file-directory-p file
)
1015 (setq suffix
(cdr (assoc ":" dired-compress-file-suffixes
)))
1017 (let ((out-name (concat file
(car suffix
)))
1018 (default-directory (file-name-directory file
)))
1019 (dired-shell-command
1020 (replace-regexp-in-string
1022 (replace-regexp-in-string
1023 "%i" (file-name-nondirectory file
)
1026 (let ((out-name (concat file
".gz")))
1027 (and (or (not (file-exists-p out-name
))
1029 (format "File %s already exists. Really compress? "
1032 (dired-check-process (concat "Compressing " file
)
1034 (or (file-exists-p out-name
)
1035 (setq out-name
(concat file
".z")))
1036 ;; Rename the compressed file to NEWNAME
1037 ;; if it hasn't got that name already.
1038 (if (and newname
(not (equal newname out-name
)))
1040 (rename-file out-name newname t
)
1044 (if (not (dired-check-process (concat "Compressing " file
)
1045 "compress" "-f" file
))
1046 ;; Don't use NEWNAME with `compress'.
1047 (concat file
".Z"))))))))
1049 (defun dired-mark-confirm (op-symbol arg
)
1050 ;; Request confirmation from the user that the operation described
1051 ;; by OP-SYMBOL is to be performed on the marked files.
1052 ;; Confirmation consists in a y-or-n question with a file list
1053 ;; pop-up unless OP-SYMBOL is a member of `dired-no-confirm'.
1054 ;; The files used are determined by ARG (as in dired-get-marked-files).
1055 (or (eq dired-no-confirm t
)
1056 (memq op-symbol dired-no-confirm
)
1057 ;; Pass t for DISTINGUISH-ONE-MARKED so that a single file which
1058 ;; is marked pops up a window. That will help the user see
1059 ;; it isn't the current line file.
1060 (let ((files (dired-get-marked-files t arg nil t
))
1061 (string (if (eq op-symbol
'compress
) "Compress or uncompress"
1062 (capitalize (symbol-name op-symbol
)))))
1063 (dired-mark-pop-up nil op-symbol files
(function y-or-n-p
)
1065 (dired-mark-prompt arg files
) "? ")))))
1067 (defun dired-map-over-marks-check (fun arg op-symbol
&optional show-progress
)
1068 ; "Map FUN over marked files (with second ARG like in dired-map-over-marks)
1069 ; and display failures.
1071 ; FUN takes zero args. It returns non-nil (the offending object, e.g.
1072 ; the short form of the filename) for a failure and probably logs a
1073 ; detailed error explanation using function `dired-log'.
1075 ; OP-SYMBOL is a symbol describing the operation performed (e.g.
1076 ; `compress'). It is used with `dired-mark-pop-up' to prompt the user
1077 ; (e.g. with `Compress * [2 files]? ') and to display errors (e.g.
1078 ; `Failed to compress 1 of 2 files - type W to see why ("foo")')
1080 ; SHOW-PROGRESS if non-nil means redisplay dired after each file."
1081 (if (dired-mark-confirm op-symbol arg
)
1082 (let* ((total-list;; all of FUN's return values
1083 (dired-map-over-marks (funcall fun
) arg show-progress
))
1084 (total (length total-list
))
1085 (failures (delq nil total-list
))
1086 (count (length failures
))
1087 (string (if (eq op-symbol
'compress
) "Compress or uncompress"
1088 (capitalize (symbol-name op-symbol
)))))
1090 (message "%s: %d file%s."
1091 string total
(dired-plural-s total
))
1092 ;; end this bunch of errors:
1094 (format "Failed to %s %d of %d file%s"
1095 (downcase string
) count total
(dired-plural-s total
))
1099 (defun dired-query (sym prompt
&rest args
)
1100 "Format PROMPT with ARGS, query user, and store the result in SYM.
1101 The return value is either nil or t.
1103 The user may type y or SPC to accept once; n or DEL to skip once;
1104 ! to accept this and subsequent queries; or q or ESC to decline
1105 this and subsequent queries.
1107 If SYM is already bound to a non-nil value, this function may
1108 return automatically without querying the user. If SYM is !,
1109 return t; if SYM is q or ESC, return nil."
1110 (let* ((char (symbol-value sym
))
1111 (char-choices '(?y ?\s ?n ?
\177 ?
! ?q ?\e
)))
1113 t
) ; accept, and don't ask again
1114 ((memq char
'(?q ?\e
))
1115 nil
) ; skip, and don't ask again
1116 (t ; no previous answer - ask now
1118 (concat (apply #'format-message prompt args
)
1120 (format " [Type yn!q or %s] "
1121 (key-description (vector help-char
)))
1122 " [Type y, n, q or !] ")))
1123 (set sym
(setq char
(read-char-choice prompt char-choices
)))
1124 (if (memq char
'(?y ?\s ?
!)) t
)))))
1128 (defun dired-do-compress (&optional arg
)
1129 "Compress or uncompress marked (or next ARG) files."
1131 (dired-map-over-marks-check (function dired-compress
) arg
'compress t
))
1133 ;; Commands for Emacs Lisp files - load and byte compile
1135 (defun dired-byte-compile ()
1136 ;; Return nil for success, offending file name else.
1137 (let* ((filename (dired-get-filename))
1138 elc-file buffer-read-only failure
)
1140 (save-excursion (byte-compile-file filename
))
1142 (setq failure err
)))
1143 (setq elc-file
(byte-compile-dest-file filename
))
1144 (or (file-exists-p elc-file
)
1148 (dired-log "Byte compile error for %s:\n%s\n" filename failure
)
1149 (dired-make-relative filename
))
1150 (dired-remove-file elc-file
)
1151 (forward-line) ; insert .elc after its .el file
1152 (dired-add-file elc-file
)
1156 (defun dired-do-byte-compile (&optional arg
)
1157 "Byte compile marked (or next ARG) Emacs Lisp files."
1159 (dired-map-over-marks-check (function dired-byte-compile
) arg
'byte-compile t
))
1161 (defun dired-load ()
1162 ;; Return nil for success, offending file name else.
1163 (let ((file (dired-get-filename)) failure
)
1165 (load file nil nil t
)
1166 (error (setq failure err
)))
1169 (dired-log "Load error for %s:\n%s\n" file failure
)
1170 (dired-make-relative file
))))
1173 (defun dired-do-load (&optional arg
)
1174 "Load the marked (or next ARG) Emacs Lisp files."
1176 (dired-map-over-marks-check (function dired-load
) arg
'load t
))
1179 (defun dired-do-redisplay (&optional arg test-for-subdir
)
1180 "Redisplay all marked (or next ARG) files.
1181 If on a subdir line, redisplay that subdirectory. In that case,
1182 a prefix arg lets you edit the `ls' switches used for the new listing.
1184 Dired remembers switches specified with a prefix arg, so that reverting
1185 the buffer will not reset them. However, using `dired-undo' to re-insert
1186 or delete subdirectories can bypass this machinery. Hence, you sometimes
1187 may have to reset some subdirectory switches after a `dired-undo'.
1188 You can reset all subdirectory switches to the default using
1189 \\<dired-mode-map>\\[dired-reset-subdir-switches].
1190 See Info node `(emacs)Subdir switches' for more details."
1191 ;; Moves point if the next ARG files are redisplayed.
1192 (interactive "P\np")
1193 (if (and test-for-subdir
(dired-get-subdir))
1194 (let* ((dir (dired-get-subdir))
1195 (switches (cdr (assoc-string dir dired-switches-alist
))))
1196 (dired-insert-subdir
1199 (read-string "Switches for listing: "
1201 dired-subdir-switches
1202 dired-actual-switches
)))))
1203 (message "Redisplaying...")
1204 ;; message much faster than making dired-map-over-marks show progress
1206 (if (consp dired-directory
) (car dired-directory
) dired-directory
))
1207 (dired-map-over-marks (let ((fname (dired-get-filename))
1208 ;; Postpone readin hook till we map
1209 ;; over all marked files (Bug#6810).
1210 (dired-after-readin-hook nil
))
1211 (message "Redisplaying... %s" fname
)
1212 (dired-update-file-line fname
))
1214 (run-hooks 'dired-after-readin-hook
)
1215 (dired-move-to-filename)
1216 (message "Redisplaying...done")))
1218 (defun dired-reset-subdir-switches ()
1219 "Set `dired-switches-alist' to nil and revert dired buffer."
1221 (setq dired-switches-alist nil
)
1224 (defun dired-update-file-line (file)
1225 ;; Delete the current line, and insert an entry for FILE.
1226 ;; If FILE is nil, then just delete the current line.
1227 ;; Keeps any marks that may be present in column one (doing this
1228 ;; here is faster than with dired-add-entry's optional arg).
1229 ;; Does not update other dired buffers. Use dired-relist-entry for that.
1230 (let* ((opoint (line-beginning-position))
1231 (char (char-after opoint
))
1233 (delete-region opoint
(progn (forward-line 1) (point)))
1236 (dired-add-entry file nil t
)
1237 ;; Replace space by old marker without moving point.
1238 ;; Faster than goto+insdel inside a save-excursion?
1240 (subst-char-in-region opoint
(1+ opoint
) ?
\040 char
)))))
1241 (dired-move-to-filename))
1244 (defun dired-add-file (filename &optional marker-char
)
1245 (dired-fun-in-all-buffers
1246 (file-name-directory filename
) (file-name-nondirectory filename
)
1247 (function dired-add-entry
) filename marker-char
))
1249 (defvar dired-omit-mode
)
1250 (declare-function dired-omit-regexp
"dired-x" ())
1251 (defvar dired-omit-localp
)
1253 (defun dired-add-entry (filename &optional marker-char relative
)
1254 "Add a new dired entry for FILENAME.
1255 Optionally mark it with MARKER-CHAR (a character, else uses
1256 `dired-marker-char'). Note that this adds the entry `out of order'
1257 if files are sorted by time, etc.
1258 Skips files that match `dired-trivial-filenames'.
1259 Exposes hidden subdirectories if a file is added there.
1261 If `dired-x' is loaded and `dired-omit-mode' is enabled, skips
1262 files matching `dired-omit-regexp'."
1263 (if (or (not (featurep 'dired-x
))
1264 (not dired-omit-mode
)
1265 ;; Avoid calling ls for files that are going to be omitted anyway.
1266 (let ((omit-re (dired-omit-regexp)))
1267 (or (string= omit-re
"")
1268 (not (string-match-p omit-re
1270 ((eq 'no-dir dired-omit-localp
)
1272 ((eq t dired-omit-localp
)
1273 (dired-make-relative filename
))
1275 (dired-make-absolute
1277 (file-name-directory filename
)))))))))
1280 (setq filename
(directory-file-name filename
))
1281 ;; Entry is always for files, even if they happen to also be directories
1282 (let* ((opoint (point))
1283 (cur-dir (dired-current-directory))
1284 (directory (if relative cur-dir
(file-name-directory filename
)))
1288 (file-relative-name filename directory
)
1289 (file-name-nondirectory filename
))
1292 (if (string= directory cur-dir
)
1294 (skip-chars-forward "^\r\n")
1295 (if (eq (following-char) ?
\r)
1296 (dired-unhide-subdir))
1297 ;; We are already where we should be, except when
1298 ;; point is before the subdir line or its total line.
1299 (let ((p (dired-after-subdir-garbage cur-dir
)))
1302 ;; else try to find correct place to insert
1303 (if (dired-goto-subdir directory
)
1304 (progn ;; unhide if necessary
1305 (if (looking-at-p "\r")
1306 ;; Point is at end of subdir line.
1307 (dired-unhide-subdir))
1308 ;; found - skip subdir and `total' line
1309 ;; and uninteresting files like . and ..
1310 ;; This better not move into the next subdir!
1311 (dired-goto-next-nontrivial-file))
1313 (throw 'not-found
"Subdir not found")))
1314 (let (buffer-read-only opoint
)
1316 (setq opoint
(point))
1317 ;; Don't expand `.'.
1318 ;; Show just the file name within directory.
1319 (let ((default-directory directory
))
1320 (dired-insert-directory
1322 (concat dired-actual-switches
" -d")
1325 ;; Put in desired marker char.
1327 (let ((dired-marker-char
1328 (if (integerp marker-char
) marker-char
1329 dired-marker-char
)))
1331 ;; Compensate for a bug in ange-ftp.
1332 ;; It inserts the file's absolute name, rather than
1333 ;; the relative one. That may be hard to fix since it
1334 ;; is probably controlled by something in ftp.
1336 (let ((inserted-name (dired-get-filename 'verbatim
)))
1337 (if (file-name-directory inserted-name
)
1340 (forward-char (- (length inserted-name
)))
1341 (setq props
(text-properties-at (point)))
1342 (delete-char (length inserted-name
))
1345 (set-text-properties pt
(point) props
))
1349 (if dired-after-readin-hook
1350 ;; The subdir-alist is not affected...
1351 (save-excursion ; ...so we can run it right now:
1354 (narrow-to-region (point)
1355 (line-beginning-position 2))
1356 (run-hooks 'dired-after-readin-hook
))))
1357 (dired-move-to-filename))
1358 ;; return nil if all went well
1360 (if reason
; don't move away on failure
1362 (not reason
))) ; return t on success, nil else
1363 ;; Don't do it (dired-omit-mode).
1364 ;; Return t for success (perhaps we should return file-exists-p).
1367 (defun dired-after-subdir-garbage (dir)
1368 ;; Return pos of first file line of DIR, skipping header and total
1369 ;; or wildcard lines.
1370 ;; Important: never moves into the next subdir.
1371 ;; DIR is assumed to be unhidden.
1373 (or (dired-goto-subdir dir
) (error "This cannot happen"))
1375 (while (and (not (eolp)) ; don't cross subdir boundary
1376 (not (dired-move-to-filename)))
1381 (defun dired-remove-file (file)
1382 (dired-fun-in-all-buffers
1383 (file-name-directory file
) (file-name-nondirectory file
)
1384 (function dired-remove-entry
) file
))
1386 (defun dired-remove-entry (file)
1388 (and (dired-goto-file file
)
1389 (let (buffer-read-only)
1390 (delete-region (progn (beginning-of-line) (point))
1391 (line-beginning-position 2))))))
1394 (defun dired-relist-file (file)
1395 "Create or update the line for FILE in all Dired buffers it would belong in."
1396 (dired-fun-in-all-buffers (file-name-directory file
)
1397 (file-name-nondirectory file
)
1398 (function dired-relist-entry
) file
))
1400 (defun dired-relist-entry (file)
1401 ;; Relist the line for FILE, or just add it if it did not exist.
1402 ;; FILE must be an absolute file name.
1403 (let (buffer-read-only marker
)
1404 ;; If cursor is already on FILE's line delete-region will cause
1405 ;; save-excursion to fail because of floating makers,
1406 ;; moving point to beginning of line. Sigh.
1408 (and (dired-goto-file file
)
1409 (delete-region (progn (beginning-of-line)
1410 (setq marker
(following-char))
1412 (line-beginning-position 2)))
1413 (setq file
(directory-file-name file
))
1414 (dired-add-entry file
(if (eq ?
\040 marker
) nil marker
)))))
1416 ;;; Copy, move/rename, making hard and symbolic links
1418 (defcustom dired-backup-overwrite nil
1419 "Non-nil if Dired should ask about making backups before overwriting files.
1420 Special value `always' suppresses confirmation."
1421 :type
'(choice (const :tag
"off" nil
)
1422 (const :tag
"suppress" always
)
1423 (other :tag
"ask" t
))
1426 ;; This is a fluid var used in dired-handle-overwrite. It should be
1427 ;; let-bound whenever dired-copy-file etc are called. See
1428 ;; dired-create-files for an example.
1429 (defvar dired-overwrite-confirmed
)
1431 (defun dired-handle-overwrite (to)
1432 ;; Save old version of file TO that is to be overwritten.
1433 ;; `dired-overwrite-confirmed' and `overwrite-backup-query' are fluid vars
1434 ;; from dired-create-files.
1436 (when (and dired-backup-overwrite
1437 dired-overwrite-confirmed
1438 (setq backup
(car (find-backup-file-name to
)))
1439 (or (eq 'always dired-backup-overwrite
)
1440 (dired-query 'overwrite-backup-query
1441 "Make backup for existing file `%s'? "
1443 (rename-file to backup
0) ; confirm overwrite of old backup
1444 (dired-relist-entry backup
))))
1447 (defun dired-copy-file (from to ok-flag
)
1448 (dired-handle-overwrite to
)
1449 (dired-copy-file-recursive from to ok-flag dired-copy-preserve-time t
1450 dired-recursive-copies
))
1452 (declare-function make-symbolic-link
"fileio.c")
1454 (defun dired-copy-file-recursive (from to ok-flag
&optional
1455 preserve-time top recursive
)
1456 (when (and (eq t
(car (file-attributes from
)))
1457 (file-in-directory-p to from
))
1458 (error "Cannot copy `%s' into its subdirectory `%s'" from to
))
1459 (let ((attrs (file-attributes from
)))
1462 (or (eq recursive
'always
)
1463 (yes-or-no-p (format "Recursive copies of %s? " from
))))
1464 (copy-directory from to preserve-time
)
1465 (or top
(dired-handle-overwrite to
))
1467 (if (stringp (car attrs
))
1469 (make-symbolic-link (car attrs
) to ok-flag
)
1470 (copy-file from to ok-flag preserve-time
))
1472 (push (dired-make-relative from
)
1473 dired-create-files-failures
)
1474 (dired-log "Can't set date on %s:\n%s\n" from err
))))))
1477 (defun dired-rename-file (file newname ok-if-already-exists
)
1478 (dired-handle-overwrite newname
)
1479 (rename-file file newname ok-if-already-exists
) ; error is caught in -create-files
1480 ;; Silently rename the visited file of any buffer visiting this file.
1481 (and (get-file-buffer file
)
1482 (with-current-buffer (get-file-buffer file
)
1483 (set-visited-file-name newname nil t
)))
1484 (dired-remove-file file
)
1485 ;; See if it's an inserted subdir, and rename that, too.
1486 (dired-rename-subdir file newname
))
1488 (defun dired-rename-subdir (from-dir to-dir
)
1489 (setq from-dir
(file-name-as-directory from-dir
)
1490 to-dir
(file-name-as-directory to-dir
))
1491 (dired-fun-in-all-buffers from-dir nil
1492 (function dired-rename-subdir-1
) from-dir to-dir
)
1493 ;; Update visited file name of all affected buffers
1494 (let ((expanded-from-dir (expand-file-name from-dir
))
1495 (blist (buffer-list)))
1497 (with-current-buffer (car blist
)
1498 (if (and buffer-file-name
1499 (dired-in-this-tree buffer-file-name expanded-from-dir
))
1500 (let ((modflag (buffer-modified-p))
1501 (to-file (dired-replace-in-string
1502 (concat "^" (regexp-quote from-dir
))
1505 (set-visited-file-name to-file
)
1506 (set-buffer-modified-p modflag
))))
1507 (setq blist
(cdr blist
)))))
1509 (defun dired-rename-subdir-1 (dir to
)
1510 ;; Rename DIR to TO in headerlines and dired-subdir-alist, if DIR or
1511 ;; one of its subdirectories is expanded in this buffer.
1512 (let ((expanded-dir (expand-file-name dir
))
1513 (alist dired-subdir-alist
)
1516 (setq elt
(car alist
)
1518 (if (dired-in-this-tree (car elt
) expanded-dir
)
1519 ;; ELT's subdir is affected by the rename
1520 (dired-rename-subdir-2 elt dir to
)))
1521 (if (equal dir default-directory
)
1522 ;; if top level directory was renamed, lots of things have to be
1525 (dired-unadvertise dir
) ; we no longer dired DIR...
1526 (setq default-directory to
1527 dired-directory
(expand-file-name;; this is correct
1528 ;; with and without wildcards
1529 (file-name-nondirectory dired-directory
)
1531 (let ((new-name (file-name-nondirectory
1532 (directory-file-name dired-directory
))))
1533 ;; try to rename buffer, but just leave old name if new
1534 ;; name would already exist (don't try appending "<%d>")
1535 (or (get-buffer new-name
)
1536 (rename-buffer new-name
)))
1537 ;; ... we dired TO now:
1538 (dired-advertise)))))
1540 (defun dired-rename-subdir-2 (elt dir to
)
1541 ;; Update the headerline and dired-subdir-alist element, as well as
1542 ;; dired-switches-alist element, of directory described by
1543 ;; alist-element ELT to reflect the moving of DIR to TO. Thus, ELT
1544 ;; describes either DIR itself or a subdir of DIR.
1546 (let ((regexp (regexp-quote (directory-file-name dir
)))
1547 (newtext (directory-file-name to
))
1549 (goto-char (dired-get-subdir-min elt
))
1550 ;; Update subdir headerline in buffer
1551 (if (not (looking-at dired-subdir-regexp
))
1552 (error "%s not found where expected - dired-subdir-alist broken?"
1554 (goto-char (match-beginning 1))
1555 (if (re-search-forward regexp
(match-end 1) t
)
1556 (replace-match newtext t t
)
1557 (error "Expected to find `%s' in headerline of %s" dir
(car elt
))))
1558 ;; Update buffer-local dired-subdir-alist and dired-switches-alist
1559 (let ((cons (assoc-string (car elt
) dired-switches-alist
))
1560 (cur-dir (dired-normalize-subdir
1561 (dired-replace-in-string regexp newtext
(car elt
)))))
1562 (setcar elt cur-dir
)
1563 (when cons
(setcar cons cur-dir
))))))
1565 ;; Bound in dired-create-files
1566 (defvar overwrite-query
)
1567 (defvar overwrite-backup-query
)
1569 ;; The basic function for half a dozen variations on cp/mv/ln/ln -s.
1570 (defun dired-create-files (file-creator operation fn-list name-constructor
1571 &optional marker-char
)
1572 "Create one or more new files from a list of existing files FN-LIST.
1573 This function also handles querying the user, updating Dired
1574 buffers, and displaying a success or failure message.
1576 FILE-CREATOR should be a function. It is called once for each
1577 file in FN-LIST, and must create a new file, querying the user
1578 and updating Dired buffers as necessary. It should accept three
1579 arguments: the old file name, the new name, and an argument
1580 OK-IF-ALREADY-EXISTS with the same meaning as in `copy-file'.
1582 OPERATION should be a capitalized string describing the operation
1583 performed (e.g. `Copy'). It is used for error logging.
1585 FN-LIST is the list of files to copy (full absolute file names).
1587 NAME-CONSTRUCTOR should be a function accepting a single
1588 argument, the name of an old file, and returning either the
1589 corresponding new file name or nil to skip.
1591 If optional argument MARKER-CHAR is non-nil, mark each
1592 newly-created file's Dired entry with the character MARKER-CHAR,
1593 or with the current marker character if MARKER-CHAR is t."
1594 (let (dired-create-files-failures failures
1595 skipped
(success-count 0) (total (length fn-list
)))
1596 (let (to overwrite-query
1597 overwrite-backup-query
) ; for dired-handle-overwrite
1598 (dolist (from fn-list
)
1599 (setq to
(funcall name-constructor from
))
1603 (dired-log "Cannot %s to same file: %s\n"
1604 (downcase operation
) from
)))
1606 (setq skipped
(cons (dired-make-relative from
) skipped
))
1607 (let* ((overwrite (file-exists-p to
))
1608 (dired-overwrite-confirmed ; for dired-handle-overwrite
1610 (let ((help-form '(format-message "\
1611 Type SPC or `y' to overwrite file `%s',
1612 DEL or `n' to skip to next,
1613 ESC or `q' to not overwrite any of the remaining files,
1614 `!' to overwrite all remaining files with no more questions." to
)))
1615 (dired-query 'overwrite-query
1616 "Overwrite `%s'?" to
))))
1617 ;; must determine if FROM is marked before file-creator
1618 ;; gets a chance to delete it (in case of a move).
1620 (cond ((integerp marker-char
) marker-char
)
1621 (marker-char (dired-file-marker from
)) ; slow
1623 ;; Handle the `dired-copy-file' file-creator specially
1624 ;; When copying a directory to another directory or
1625 ;; possibly to itself or one of its subdirectories.
1626 ;; e.g "~/foo/" => "~/test/"
1627 ;; or "~/foo/" =>"~/foo/"
1628 ;; or "~/foo/ => ~/foo/bar/")
1629 ;; In this case the 'name-constructor' have set the destination
1630 ;; TO to "~/test/foo" because the old emacs23 behavior
1631 ;; of `copy-directory' was to not create the subdirectory
1632 ;; and instead copy the contents.
1633 ;; With the new behavior of `copy-directory'
1634 ;; (similar to the `cp' shell command) we don't
1635 ;; need such a construction of the target directory,
1636 ;; so modify the destination TO to "~/test/" instead of "~/test/foo/".
1637 (let ((destname (file-name-directory to
)))
1638 (when (and (file-directory-p from
)
1639 (file-directory-p to
)
1640 (eq file-creator
'dired-copy-file
))
1642 ;; If DESTNAME is a subdirectory of FROM, not a symlink,
1643 ;; and the method in use is copying, signal an error.
1644 (and (eq t
(car (file-attributes destname
)))
1645 (eq file-creator
'dired-copy-file
)
1646 (file-in-directory-p destname from
)
1647 (error "Cannot copy `%s' into its subdirectory `%s'"
1651 (funcall file-creator from to dired-overwrite-confirmed
)
1653 ;; If we get here, file-creator hasn't been aborted
1654 ;; and the old entry (if any) has to be deleted
1655 ;; before adding the new entry.
1656 (dired-remove-file to
))
1657 (setq success-count
(1+ success-count
))
1658 (message "%s: %d of %d" operation success-count total
)
1659 (dired-add-file to actual-marker-char
))
1660 (file-error ; FILE-CREATOR aborted
1662 (push (dired-make-relative from
)
1664 (dired-log "%s `%s' to `%s' failed:\n%s\n"
1665 operation from to err
))))))))
1667 (dired-create-files-failures
1668 (setq failures
(nconc failures dired-create-files-failures
))
1670 (format "%s failed for %d file%s in %d requests"
1671 operation
(length failures
)
1672 (dired-plural-s (length failures
))
1677 (format "%s failed for %d of %d file%s"
1678 operation
(length failures
)
1679 total
(dired-plural-s total
))
1683 (format "%s: %d of %d file%s skipped"
1684 operation
(length skipped
) total
1685 (dired-plural-s total
))
1688 (message "%s: %s file%s"
1689 operation success-count
(dired-plural-s success-count
)))))
1690 (dired-move-to-filename))
1692 (defun dired-do-create-files (op-symbol file-creator operation arg
1693 &optional marker-char op1
1695 "Create a new file for each marked file.
1696 Prompt user for a target directory in which to create the new
1697 files. The target may also be a non-directory file, if only
1698 one file is marked. The initial suggestion for target is the
1699 Dired buffer's current directory (or, if `dired-dwim-target' is
1700 non-nil, the current directory of a neighboring Dired window).
1701 OP-SYMBOL is the symbol for the operation. Function `dired-mark-pop-up'
1702 will determine whether pop-ups are appropriate for this OP-SYMBOL.
1703 FILE-CREATOR and OPERATION as in `dired-create-files'.
1704 ARG as in `dired-get-marked-files'.
1705 Optional arg MARKER-CHAR as in `dired-create-files'.
1706 Optional arg OP1 is an alternate form for OPERATION if there is
1708 Optional arg HOW-TO determines how to treat the target.
1709 If HOW-TO is nil, use `file-directory-p' to determine if the
1710 target is a directory. If so, the marked file(s) are created
1711 inside that directory. Otherwise, the target is a plain file;
1712 an error is raised unless there is exactly one marked file.
1713 If HOW-TO is t, target is always treated as a plain file.
1714 Otherwise, HOW-TO should be a function of one argument, TARGET.
1715 If its return value is nil, TARGET is regarded as a plain file.
1716 If it return value is a list, TARGET is a generalized
1717 directory (e.g. some sort of archive). The first element of
1718 this list must be a function with at least four arguments:
1719 operation - as OPERATION above.
1720 rfn-list - list of the relative names for the marked files.
1721 fn-list - list of the absolute names for the marked files.
1722 target - the name of the target itself.
1723 The rest of into-dir are optional arguments.
1724 For any other return value, TARGET is treated as a directory."
1725 (or op1
(setq op1 operation
))
1726 (let* ((fn-list (dired-get-marked-files nil arg
))
1727 (rfn-list (mapcar (function dired-make-relative
) fn-list
))
1728 (dired-one-file ; fluid variable inside dired-create-files
1729 (and (consp fn-list
) (null (cdr fn-list
)) (car fn-list
)))
1730 (target-dir (dired-dwim-target-directory))
1731 (default (and dired-one-file
1732 (expand-file-name (file-name-nondirectory (car fn-list
))
1734 (defaults (dired-dwim-target-defaults fn-list target-dir
))
1735 (target (expand-file-name ; fluid variable inside dired-create-files
1736 (minibuffer-with-setup-hook
1738 (set (make-local-variable 'minibuffer-default-add-function
) nil
)
1739 (setq minibuffer-default defaults
))
1740 (dired-mark-read-file-name
1741 (concat (if dired-one-file op1 operation
) " %s to: ")
1742 target-dir op-symbol arg rfn-list default
))))
1743 (into-dir (cond ((null how-to
)
1744 ;; Allow DOS/Windows users to change the letter
1745 ;; case of a directory. If we don't test these
1746 ;; conditions up front, file-directory-p below
1747 ;; will return t because the filesystem is
1748 ;; case-insensitive, and Emacs will try to move
1749 ;; foo -> foo/foo, which fails.
1750 (if (and (memq system-type
'(ms-dos windows-nt cygwin
))
1751 (eq op-symbol
'move
)
1754 (expand-file-name (car fn-list
)))
1756 (expand-file-name target
)))
1758 (file-name-nondirectory (car fn-list
))
1759 (file-name-nondirectory target
))))
1761 (file-directory-p target
)))
1763 (t (funcall how-to target
)))))
1764 (if (and (consp into-dir
) (functionp (car into-dir
)))
1765 (apply (car into-dir
) operation rfn-list fn-list target
(cdr into-dir
))
1766 (if (not (or dired-one-file into-dir
))
1767 (error "Marked %s: target must be a directory: %s" operation target
))
1768 ;; rename-file bombs when moving directories unless we do this:
1769 (or into-dir
(setq target
(directory-file-name target
)))
1771 file-creator operation fn-list
1772 (if into-dir
; target is a directory
1773 ;; This function uses fluid variable target when called
1774 ;; inside dired-create-files:
1777 (expand-file-name (file-name-nondirectory from
) target
)))
1778 (function (lambda (_from) target
)))
1781 ;; Read arguments for a marked-files command that wants a file name,
1782 ;; perhaps popping up the list of marked files.
1783 ;; ARG is the prefix arg and indicates whether the files came from
1784 ;; marks (ARG=nil) or a repeat factor (integerp ARG).
1785 ;; If the current file was used, the list has but one element and ARG
1786 ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)).
1787 ;; DEFAULT is the default value to return if the user just hits RET;
1788 ;; if it is omitted or nil, then the name of the directory is used.
1790 (defun dired-mark-read-file-name (prompt dir op-symbol arg files
1794 (function read-file-name
)
1795 (format prompt
(dired-mark-prompt arg files
)) dir default
))
1797 (defun dired-dwim-target-directory ()
1798 ;; Try to guess which target directory the user may want.
1799 ;; If there is a dired buffer displayed in one of the next windows,
1800 ;; use its current subdir, else use current subdir of this dired buffer.
1801 (let ((this-dir (and (eq major-mode
'dired-mode
)
1802 (dired-current-directory))))
1803 ;; non-dired buffer may want to profit from this function, e.g. vm-uudecode
1804 (if dired-dwim-target
1805 (let* ((other-win (get-window-with-predicate
1807 (with-current-buffer (window-buffer window
)
1808 (eq major-mode
'dired-mode
)))))
1809 (other-dir (and other-win
1810 (with-current-buffer (window-buffer other-win
)
1811 (and (eq major-mode
'dired-mode
)
1812 (dired-current-directory))))))
1813 (or other-dir this-dir
))
1816 (defun dired-dwim-target-defaults (fn-list target-dir
)
1817 ;; Return a list of default values for file-reading functions in Dired.
1818 ;; This list may contain directories from Dired buffers in other windows.
1819 ;; `fn-list' is a list of file names used to build a list of defaults.
1820 ;; When nil or more than one element, a list of defaults will
1821 ;; contain only directory names. `target-dir' is a directory name
1822 ;; to exclude from the returned list, for the case when this
1823 ;; directory name is already presented in initial input.
1824 ;; For Dired operations that support `dired-dwim-target',
1825 ;; the argument `target-dir' should have the value returned
1826 ;; from `dired-dwim-target-directory'.
1827 (let ((dired-one-file
1828 (and (consp fn-list
) (null (cdr fn-list
)) (car fn-list
)))
1829 (current-dir (and (eq major-mode
'dired-mode
)
1830 (dired-current-directory)))
1832 ;; Get a list of directories of visible buffers in dired-mode.
1833 (walk-windows (lambda (w)
1834 (with-current-buffer (window-buffer w
)
1835 (and (eq major-mode
'dired-mode
)
1836 (push (dired-current-directory) dired-dirs
)))))
1837 ;; Force the current dir to be the first in the list.
1839 (delete-dups (delq nil
(cons current-dir
(nreverse dired-dirs
)))))
1840 ;; Remove the target dir (if specified) or the current dir from
1841 ;; default values, because it should be already in initial input.
1842 (setq dired-dirs
(delete (or target-dir current-dir
) dired-dirs
))
1843 ;; Return a list of default values.
1845 ;; For one file operation, provide a list that contains
1846 ;; other directories, other directories with the appended filename
1847 ;; and the current directory with the appended filename, e.g.
1849 ;; 2. /TARGET-DIR/FILENAME
1850 ;; 3. /CURRENT-DIR/FILENAME
1852 (mapcar (lambda (dir)
1854 (file-name-nondirectory (car fn-list
)) dir
))
1855 (reverse dired-dirs
))
1856 (list (expand-file-name
1857 (file-name-nondirectory (car fn-list
))
1858 (or target-dir current-dir
))))
1859 ;; For multi-file operation, return only a list of other directories.
1864 (defun dired-create-directory (directory)
1865 "Create a directory called DIRECTORY.
1866 If DIRECTORY already exists, signal an error."
1868 (list (read-file-name "Create directory: " (dired-current-directory))))
1869 (let* ((expanded (directory-file-name (expand-file-name directory
)))
1871 (if (file-exists-p expanded
)
1872 (error "Cannot create directory %s: file exists" expanded
))
1873 ;; Find the topmost nonexistent parent dir (variable `new')
1874 (while (and try
(not (file-exists-p try
)) (not (equal new try
)))
1876 try
(directory-file-name (file-name-directory try
))))
1877 (make-directory expanded t
)
1879 (dired-add-file new
)
1880 (dired-move-to-filename))))
1882 (defun dired-into-dir-with-symlinks (target)
1883 (and (file-directory-p target
)
1884 (not (file-symlink-p target
))))
1885 ;; This may not always be what you want, especially if target is your
1886 ;; home directory and it happens to be a symbolic link, as is often the
1887 ;; case with NFS and automounters. Or if you want to make symlinks
1888 ;; into directories that themselves are only symlinks, also quite
1891 ;; So we don't use this function as value for HOW-TO in
1892 ;; dired-do-symlink, which has the minor disadvantage of
1893 ;; making links *into* a symlinked-dir, when you really wanted to
1894 ;; *overwrite* that symlink. In that (rare, I guess) case, you'll
1895 ;; just have to remove that symlink by hand before making your marked
1898 (defvar dired-copy-how-to-fn nil
1899 "Either nil or a function used by `dired-do-copy' to determine target.
1900 See HOW-TO argument for `dired-do-create-files'.")
1903 (defun dired-do-copy (&optional arg
)
1904 "Copy all marked (or next ARG) files, or copy the current file.
1905 When operating on just the current file, prompt for the new name.
1907 When operating on multiple or marked files, prompt for a target
1908 directory, and make the new copies in that directory, with the
1909 same names as the original files. The initial suggestion for the
1910 target directory is the Dired buffer's current directory (or, if
1911 `dired-dwim-target' is non-nil, the current directory of a
1912 neighboring Dired window).
1914 If `dired-copy-preserve-time' is non-nil, this command preserves
1915 the modification time of each old file in the copy, similar to
1916 the \"-p\" option for the \"cp\" shell command.
1918 This command copies symbolic links by creating new ones, similar
1919 to the \"-d\" option for the \"cp\" shell command."
1921 (let ((dired-recursive-copies dired-recursive-copies
))
1922 (dired-do-create-files 'copy
(function dired-copy-file
)
1924 arg dired-keep-marker-copy
1925 nil dired-copy-how-to-fn
)))
1928 (defun dired-do-symlink (&optional arg
)
1929 "Make symbolic links to current file or all marked (or next ARG) files.
1930 When operating on just the current file, you specify the new name.
1931 When operating on multiple or marked files, you specify a directory
1932 and new symbolic links are made in that directory
1933 with the same names that the files currently have. The default
1934 suggested for the target directory depends on the value of
1935 `dired-dwim-target', which see.
1937 For relative symlinks, use \\[dired-do-relsymlink]."
1939 (dired-do-create-files 'symlink
(function make-symbolic-link
)
1940 "Symlink" arg dired-keep-marker-symlink
))
1943 (defun dired-do-hardlink (&optional arg
)
1944 "Add names (hard links) current file or all marked (or next ARG) files.
1945 When operating on just the current file, you specify the new name.
1946 When operating on multiple or marked files, you specify a directory
1947 and new hard links are made in that directory
1948 with the same names that the files currently have. The default
1949 suggested for the target directory depends on the value of
1950 `dired-dwim-target', which see."
1952 (dired-do-create-files 'hardlink
(function dired-hardlink
)
1953 "Hardlink" arg dired-keep-marker-hardlink
))
1955 (defun dired-hardlink (file newname
&optional ok-if-already-exists
)
1956 (dired-handle-overwrite newname
)
1957 ;; error is caught in -create-files
1958 (add-name-to-file file newname ok-if-already-exists
)
1959 ;; Update the link count
1960 (dired-relist-file file
))
1963 (defun dired-do-rename (&optional arg
)
1964 "Rename current file or all marked (or next ARG) files.
1965 When renaming just the current file, you specify the new name.
1966 When renaming multiple or marked files, you specify a directory.
1967 This command also renames any buffers that are visiting the files.
1968 The default suggested for the target directory depends on the value
1969 of `dired-dwim-target', which see."
1971 (dired-do-create-files 'move
(function dired-rename-file
)
1972 "Move" arg dired-keep-marker-rename
"Rename"))
1973 ;;;###end dired-cp.el
1976 ;;;###begin dired-re.el
1977 (defvar rename-regexp-query
)
1979 (defun dired-do-create-files-regexp
1980 (file-creator operation arg regexp newname
&optional whole-name marker-char
)
1981 ;; Create a new file for each marked file using regexps.
1982 ;; FILE-CREATOR and OPERATION as in dired-create-files.
1983 ;; ARG as in dired-get-marked-files.
1984 ;; Matches each marked file against REGEXP and constructs the new
1985 ;; filename from NEWNAME (like in function replace-match).
1986 ;; Optional arg WHOLE-NAME means match/replace the whole file name
1987 ;; instead of only the non-directory part of the file.
1988 ;; Optional arg MARKER-CHAR as in dired-create-files.
1989 (let* ((fn-list (dired-get-marked-files nil arg
))
1990 (operation-prompt (concat operation
" `%s' to `%s'?"))
1991 (rename-regexp-help-form (format-message "\
1992 Type SPC or `y' to %s one match, DEL or `n' to skip to next,
1993 `!' to %s all remaining matches with no more questions."
1994 (downcase operation
)
1995 (downcase operation
)))
1996 (regexp-name-constructor
1997 ;; Function to construct new filename using REGEXP and NEWNAME:
1998 (if whole-name
; easy (but rare) case
2001 (let ((to (dired-string-replace-match regexp from newname
))
2002 ;; must bind help-form directly around call to
2004 (help-form rename-regexp-help-form
))
2006 (and (dired-query 'rename-regexp-query
2011 (dired-log "%s: %s did not match regexp %s\n"
2012 operation from regexp
)))))
2013 ;; not whole-name, replace non-directory part only
2016 (let* ((new (dired-string-replace-match
2017 regexp
(file-name-nondirectory from
) newname
))
2018 (to (and new
; nil means there was no match
2019 (expand-file-name new
2020 (file-name-directory from
))))
2021 (help-form rename-regexp-help-form
))
2023 (and (dired-query 'rename-regexp-query
2025 (dired-make-relative from
)
2026 (dired-make-relative to
))
2028 (dired-log "%s: %s did not match regexp %s\n"
2029 operation
(file-name-nondirectory from
) regexp
)))))))
2030 rename-regexp-query
)
2032 file-creator operation fn-list regexp-name-constructor marker-char
)))
2034 (defun dired-mark-read-regexp (operation)
2035 ;; Prompt user about performing OPERATION.
2036 ;; Read and return list of: regexp newname arg whole-name.
2038 (equal 0 (prefix-numeric-value current-prefix-arg
)))
2040 (if whole-name nil current-prefix-arg
))
2043 (concat (if whole-name
"Abs. " "") operation
" from (regexp): ")
2044 nil
'dired-regexp-history
))
2047 (concat (if whole-name
"Abs. " "") operation
" " regexp
" to: "))))
2048 (list regexp newname arg whole-name
)))
2051 (defun dired-do-rename-regexp (regexp newname
&optional arg whole-name
)
2052 "Rename selected files whose names match REGEXP to NEWNAME.
2054 With non-zero prefix argument ARG, the command operates on the next ARG
2055 files. Otherwise, it operates on all the marked files, or the current
2056 file if none are marked.
2058 As each match is found, the user must type a character saying
2059 what to do with it. For directions, type \\[help-command] at that time.
2060 NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
2061 REGEXP defaults to the last regexp used.
2063 With a zero prefix arg, renaming by regexp affects the absolute file name.
2064 Normally, only the non-directory part of the file name is used and changed."
2065 (interactive (dired-mark-read-regexp "Rename"))
2066 (dired-do-create-files-regexp
2067 (function dired-rename-file
)
2068 "Rename" arg regexp newname whole-name dired-keep-marker-rename
))
2071 (defun dired-do-copy-regexp (regexp newname
&optional arg whole-name
)
2072 "Copy selected files whose names match REGEXP to NEWNAME.
2073 See function `dired-do-rename-regexp' for more info."
2074 (interactive (dired-mark-read-regexp "Copy"))
2075 (let ((dired-recursive-copies nil
)) ; No recursive copies.
2076 (dired-do-create-files-regexp
2077 (function dired-copy-file
)
2078 (if dired-copy-preserve-time
"Copy [-p]" "Copy")
2079 arg regexp newname whole-name dired-keep-marker-copy
)))
2082 (defun dired-do-hardlink-regexp (regexp newname
&optional arg whole-name
)
2083 "Hardlink selected files whose names match REGEXP to NEWNAME.
2084 See function `dired-do-rename-regexp' for more info."
2085 (interactive (dired-mark-read-regexp "HardLink"))
2086 (dired-do-create-files-regexp
2087 (function add-name-to-file
)
2088 "HardLink" arg regexp newname whole-name dired-keep-marker-hardlink
))
2091 (defun dired-do-symlink-regexp (regexp newname
&optional arg whole-name
)
2092 "Symlink selected files whose names match REGEXP to NEWNAME.
2093 See function `dired-do-rename-regexp' for more info."
2094 (interactive (dired-mark-read-regexp "SymLink"))
2095 (dired-do-create-files-regexp
2096 (function make-symbolic-link
)
2097 "SymLink" arg regexp newname whole-name dired-keep-marker-symlink
))
2099 (defvar rename-non-directory-query
)
2101 (defun dired-create-files-non-directory
2102 (file-creator basename-constructor operation arg
)
2103 ;; Perform FILE-CREATOR on the non-directory part of marked files
2104 ;; using function BASENAME-CONSTRUCTOR, with query for each file.
2105 ;; OPERATION like in dired-create-files, ARG as in dired-get-marked-files.
2106 (let (rename-non-directory-query)
2110 (dired-get-marked-files nil arg
)
2113 (let ((to (concat (file-name-directory from
)
2114 (funcall basename-constructor
2115 (file-name-nondirectory from
)))))
2116 (and (let ((help-form (format-message "\
2117 Type SPC or `y' to %s one file, DEL or `n' to skip to next,
2118 `!' to %s all remaining matches with no more questions."
2119 (downcase operation
)
2120 (downcase operation
))))
2121 (dired-query 'rename-non-directory-query
2122 (concat operation
" `%s' to `%s'")
2123 (dired-make-relative from
)
2124 (dired-make-relative to
)))
2126 dired-keep-marker-rename
)))
2128 (defun dired-rename-non-directory (basename-constructor operation arg
)
2129 (dired-create-files-non-directory
2130 (function dired-rename-file
)
2131 basename-constructor operation arg
))
2134 (defun dired-upcase (&optional arg
)
2135 "Rename all marked (or next ARG) files to upper case."
2137 (dired-rename-non-directory (function upcase
) "Rename upcase" arg
))
2140 (defun dired-downcase (&optional arg
)
2141 "Rename all marked (or next ARG) files to lower case."
2143 (dired-rename-non-directory (function downcase
) "Rename downcase" arg
))
2145 ;;;###end dired-re.el
2148 ;;;###begin dired-ins.el
2151 (defun dired-maybe-insert-subdir (dirname &optional
2152 switches no-error-if-not-dir-p
)
2153 "Insert this subdirectory into the same dired buffer.
2154 If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
2155 else inserts it at its natural place (as `ls -lR' would have done).
2156 With a prefix arg, you may edit the ls switches used for this listing.
2157 You can add `R' to the switches to expand the whole tree starting at
2159 This function takes some pains to conform to `ls -lR' output.
2161 Dired remembers switches specified with a prefix arg, so that reverting
2162 the buffer will not reset them. However, using `dired-undo' to re-insert
2163 or delete subdirectories can bypass this machinery. Hence, you sometimes
2164 may have to reset some subdirectory switches after a `dired-undo'.
2165 You can reset all subdirectory switches to the default using
2166 \\<dired-mode-map>\\[dired-reset-subdir-switches].
2167 See Info node `(emacs)Subdir switches' for more details."
2169 (list (dired-get-filename)
2170 (if current-prefix-arg
2171 (read-string "Switches for listing: "
2172 (or dired-subdir-switches dired-actual-switches
)))))
2173 (let ((opoint (point)))
2174 ;; We don't need a marker for opoint as the subdir is always
2175 ;; inserted *after* opoint.
2176 (setq dirname
(file-name-as-directory dirname
))
2177 (or (and (not switches
)
2178 (when (dired-goto-subdir dirname
)
2179 (unless (dired-subdir-hidden-p dirname
)
2180 (dired-initial-position dirname
))
2182 (dired-insert-subdir dirname switches no-error-if-not-dir-p
))
2183 ;; Push mark so that it's easy to find back. Do this after the
2184 ;; insert message so that the user sees the `Mark set' message.
2185 (push-mark opoint
)))
2188 (defun dired-insert-subdir (dirname &optional switches no-error-if-not-dir-p
)
2189 "Insert this subdirectory into the same Dired buffer.
2190 If it is already present, overwrite the previous entry;
2191 otherwise, insert it at its natural place (as `ls -lR' would
2193 With a prefix arg, you may edit the `ls' switches used for this listing.
2194 You can add `R' to the switches to expand the whole tree starting at
2196 This function takes some pains to conform to `ls -lR' output."
2197 ;; NO-ERROR-IF-NOT-DIR-P needed for special filesystems like
2198 ;; Prospero where dired-ls does the right thing, but
2199 ;; file-directory-p has not been redefined.
2201 (list (dired-get-filename)
2202 (if current-prefix-arg
2203 (read-string "Switches for listing: "
2204 (or dired-subdir-switches dired-actual-switches
)))))
2205 (setq dirname
(file-name-as-directory (expand-file-name dirname
)))
2206 (or no-error-if-not-dir-p
2207 (file-directory-p dirname
)
2208 (error "Attempt to insert a non-directory: %s" dirname
))
2209 (let ((elt (assoc dirname dired-subdir-alist
))
2210 (cons (assoc-string dirname dired-switches-alist
))
2211 (modflag (buffer-modified-p))
2212 (old-switches switches
)
2213 switches-have-R mark-alist case-fold-search buffer-read-only
)
2214 (and (not switches
) cons
(setq switches
(cdr cons
)))
2215 (dired-insert-subdir-validate dirname switches
)
2216 ;; case-fold-search is nil now, so we can test for capital `R':
2217 (if (setq switches-have-R
(and switches
(string-match-p "R" switches
)))
2218 ;; avoid duplicated subdirs
2219 (setq mark-alist
(dired-kill-tree dirname t
)))
2221 ;; If subdir is already present, remove it and remember its marks
2222 (setq mark-alist
(nconc (dired-insert-subdir-del elt
) mark-alist
))
2223 (dired-insert-subdir-newpos dirname
)) ; else compute new position
2224 (dired-insert-subdir-doupdate
2225 dirname elt
(dired-insert-subdir-doinsert dirname switches
))
2228 (setcdr cons switches
)
2229 (push (cons dirname switches
) dired-switches-alist
)))
2230 (when switches-have-R
2231 (dired-build-subdir-alist switches
)
2232 (setq switches
(dired-replace-in-string "R" "" switches
))
2233 (dolist (cur-ass dired-subdir-alist
)
2234 (let ((cur-dir (car cur-ass
)))
2235 (and (dired-in-this-tree cur-dir dirname
)
2236 (let ((cur-cons (assoc-string cur-dir dired-switches-alist
)))
2238 (setcdr cur-cons switches
)
2239 (push (cons cur-dir switches
) dired-switches-alist
)))))))
2240 (dired-initial-position dirname
)
2241 (save-excursion (dired-mark-remembered mark-alist
))
2242 (restore-buffer-modified-p modflag
)))
2244 (defun dired-insert-subdir-validate (dirname &optional switches
)
2245 ;; Check that it is valid to insert DIRNAME with SWITCHES.
2246 ;; Signal an error if invalid (e.g. user typed `i' on `..').
2247 (or (dired-in-this-tree dirname
(expand-file-name default-directory
))
2248 (error "%s: not in this directory tree" dirname
))
2249 (let ((real-switches (or switches dired-subdir-switches
)))
2251 (let (case-fold-search)
2255 (or (eq (null (string-match-p x real-switches
))
2256 (null (string-match-p x dired-actual-switches
)))
2258 "Can't have dirs with and without -%s switches together" x
))))
2259 ;; all switches that make a difference to dired-get-filename:
2262 (defun dired-alist-add (dir new-marker
)
2263 ;; Add new DIR at NEW-MARKER. Sort alist.
2264 (dired-alist-add-1 dir new-marker
)
2267 (defun dired-alist-sort ()
2268 ;; Keep the alist sorted on buffer position.
2269 (setq dired-subdir-alist
2270 (sort dired-subdir-alist
2271 (function (lambda (elt1 elt2
)
2272 (> (dired-get-subdir-min elt1
)
2273 (dired-get-subdir-min elt2
)))))))
2275 (defun dired-kill-tree (dirname &optional remember-marks kill-root
)
2276 "Kill all proper subdirs of DIRNAME, excluding DIRNAME itself.
2277 Interactively, you can kill DIRNAME as well by using a prefix argument.
2278 In interactive use, the command prompts for DIRNAME.
2280 When called from Lisp, if REMEMBER-MARKS is non-nil, return an alist
2281 of marked files. If KILL-ROOT is non-nil, kill DIRNAME as well."
2282 (interactive "DKill tree below directory: \ni\nP")
2283 (setq dirname
(file-name-as-directory (expand-file-name dirname
)))
2284 (let ((s-alist dired-subdir-alist
) dir m-alist
)
2286 (setq dir
(car (car s-alist
))
2287 s-alist
(cdr s-alist
))
2288 (and (or kill-root
(not (string-equal dir dirname
)))
2289 (dired-in-this-tree dir dirname
)
2290 (dired-goto-subdir dir
)
2291 (setq m-alist
(nconc (dired-kill-subdir remember-marks
) m-alist
))))
2294 (defun dired-insert-subdir-newpos (new-dir)
2295 ;; Find pos for new subdir, according to tree order.
2296 ;;(goto-char (point-max))
2297 (let ((alist dired-subdir-alist
) elt dir new-pos
)
2299 (setq elt
(car alist
)
2302 (if (dired-tree-lessp dir new-dir
)
2303 ;; Insert NEW-DIR after DIR
2304 (setq new-pos
(dired-get-subdir-max elt
)
2306 (goto-char new-pos
))
2307 ;; want a separating newline between subdirs
2313 (defun dired-insert-subdir-del (element)
2314 ;; Erase an already present subdir (given by ELEMENT) from buffer.
2315 ;; Move to that buffer position. Return a mark-alist.
2316 (let ((begin-marker (dired-get-subdir-min element
)))
2317 (goto-char begin-marker
)
2318 ;; Are at beginning of subdir (and inside it!). Now determine its end:
2319 (goto-char (dired-subdir-max))
2320 (or (eobp);; want a separating newline _between_ subdirs:
2323 (dired-remember-marks begin-marker
(point))
2324 (delete-region begin-marker
(point)))))
2326 (defun dired-insert-subdir-doinsert (dirname switches
)
2327 ;; Insert ls output after point.
2328 ;; Return the boundary of the inserted text (as list of BEG and END).
2330 (let ((begin (point)))
2331 (let ((dired-actual-switches
2333 dired-subdir-switches
2334 (dired-replace-in-string "R" "" dired-actual-switches
))))
2335 (if (equal dirname
(car (car (last dired-subdir-alist
))))
2336 ;; If doing the top level directory of the buffer,
2337 ;; redo it as specified in dired-directory.
2338 (dired-readin-insert)
2339 (dired-insert-directory dirname dired-actual-switches nil nil t
)))
2340 (list begin
(point)))))
2342 (defun dired-insert-subdir-doupdate (dirname elt beg-end
)
2343 ;; Point is at the correct subdir alist position for ELT,
2344 ;; BEG-END is the subdir-region (as list of begin and end).
2345 (if elt
; subdir was already present
2346 ;; update its position (should actually be unchanged)
2347 (set-marker (dired-get-subdir-min elt
) (point-marker))
2348 (dired-alist-add dirname
(point-marker)))
2349 ;; The hook may depend on the subdir-alist containing the just
2350 ;; inserted subdir, so run it after dired-alist-add:
2351 (if dired-after-readin-hook
2353 (let ((begin (nth 0 beg-end
))
2354 (end (nth 1 beg-end
)))
2357 (narrow-to-region begin end
)
2358 ;; hook may add or delete lines, but the subdir boundary
2360 (run-hooks 'dired-after-readin-hook
))))))
2362 (defun dired-tree-lessp (dir1 dir2
)
2363 ;; Lexicographic order on file name components, like `ls -lR':
2364 ;; DIR1 < DIR2 if DIR1 comes *before* DIR2 in an `ls -lR' listing,
2365 ;; i.e., if DIR1 is a (grand)parent dir of DIR2,
2366 ;; or DIR1 and DIR2 are in the same parentdir and their last
2367 ;; components are string-lessp.
2368 ;; Thus ("/usr/" "/usr/bin") and ("/usr/a/" "/usr/b/") are tree-lessp.
2369 ;; string-lessp could arguably be replaced by file-newer-than-file-p
2370 ;; if dired-actual-switches contained t.
2371 (setq dir1
(file-name-as-directory dir1
)
2372 dir2
(file-name-as-directory dir2
))
2373 (let ((components-1 (dired-split "/" dir1
))
2374 (components-2 (dired-split "/" dir2
)))
2375 (while (and components-1
2377 (equal (car components-1
) (car components-2
)))
2378 (setq components-1
(cdr components-1
)
2379 components-2
(cdr components-2
)))
2380 (let ((c1 (car components-1
))
2381 (c2 (car components-2
)))
2384 (string-lessp c1 c2
))
2385 ((and (null c1
) (null c2
))
2386 nil
) ; they are equal, not lessp
2387 ((null c1
) ; c2 is a subdir of c1: c1<c2
2389 ((null c2
) ; c1 is a subdir of c2: c1>c2
2391 (t (error "This can't happen"))))))
2393 ;; There should be a builtin split function - inverse to mapconcat.
2394 (defun dired-split (pat str
&optional limit
)
2395 "Splitting on regexp PAT, turn string STR into a list of substrings.
2396 Optional third arg LIMIT (>= 1) is a limit to the length of the
2398 Thus, if SEP is a regexp that only matches itself,
2400 (mapconcat 'identity (dired-split SEP STRING) SEP)
2402 is always equal to STRING."
2403 (let* ((start (string-match pat str
))
2404 (result (list (substring str
0 start
)))
2406 (end (if start
(match-end 0))))
2407 (if end
; else nothing left
2408 (while (and (or (not (integerp limit
))
2410 (string-match pat str end
))
2411 (setq start
(match-beginning 0)
2413 result
(cons (substring str end start
) result
)
2417 (if (and (or (not (integerp limit
))
2419 end
) ; else nothing left
2421 (cons (substring str end
) result
)))
2424 ;;; moving by subdirectories
2427 (defun dired-prev-subdir (arg &optional no-error-if-not-found no-skip
)
2428 "Go to previous subdirectory, regardless of level.
2429 When called interactively and not on a subdir line, go to this subdir's line."
2432 (list (if current-prefix-arg
2433 (prefix-numeric-value current-prefix-arg
)
2434 ;; if on subdir start already, don't stay there!
2435 (if (dired-get-subdir) 1 0))))
2436 (dired-next-subdir (- arg
) no-error-if-not-found no-skip
))
2438 (defun dired-subdir-min ()
2440 (if (not (dired-prev-subdir 0 t t
))
2441 (error "Not in a subdir!")
2445 (defun dired-goto-subdir (dir)
2446 "Go to end of header line of DIR in this dired buffer.
2447 Return value of point on success, otherwise return nil.
2448 The next char is either \\n, or \\r if DIR is hidden."
2450 (prog1 ; let push-mark display its message
2451 (list (expand-file-name
2452 (completing-read "Goto in situ directory: " ; prompt
2453 dired-subdir-alist
; table
2456 (dired-current-directory))))
2458 (setq dir
(file-name-as-directory dir
))
2459 (let ((elt (assoc dir dired-subdir-alist
)))
2461 (goto-char (dired-get-subdir-min elt
))
2462 ;; dired-subdir-hidden-p and dired-add-entry depend on point being
2463 ;; at either \r or \n after this function succeeds.
2464 (progn (skip-chars-forward "^\r\n")
2468 (defun dired-mark-subdir-files ()
2469 "Mark all files except `.' and `..' in current subdirectory.
2470 If the Dired buffer shows multiple directories, this command
2471 marks the files listed in the subdirectory that point is in."
2473 (let ((p-min (dired-subdir-min)))
2474 (dired-mark-files-in-region p-min
(dired-subdir-max))))
2477 (defun dired-kill-subdir (&optional remember-marks
)
2478 "Remove all lines of current subdirectory.
2479 Lower levels are unaffected."
2480 ;; With optional REMEMBER-MARKS, return a mark-alist.
2482 (let* ((beg (dired-subdir-min))
2483 (end (dired-subdir-max))
2484 (modflag (buffer-modified-p))
2485 (cur-dir (dired-current-directory))
2486 (cons (assoc-string cur-dir dired-switches-alist
))
2488 (if (equal cur-dir default-directory
)
2489 (error "Attempt to kill top level directory"))
2491 (if remember-marks
(dired-remember-marks beg end
))
2492 (delete-region beg end
)
2493 (if (eobp) ; don't leave final blank line
2495 (dired-unsubdir cur-dir
)
2497 (setq dired-switches-alist
(delete cons dired-switches-alist
)))
2498 (restore-buffer-modified-p modflag
))))
2500 (defun dired-unsubdir (dir)
2501 ;; Remove DIR from the alist
2502 (setq dired-subdir-alist
2503 (delq (assoc dir dired-subdir-alist
) dired-subdir-alist
)))
2506 (defun dired-tree-up (arg)
2507 "Go up ARG levels in the dired tree."
2509 (let ((dir (dired-current-directory)))
2512 dir
(file-name-directory (directory-file-name dir
))))
2513 ;;(setq dir (expand-file-name dir))
2514 (or (dired-goto-subdir dir
)
2515 (error "Cannot go up to %s - not in this tree" dir
))))
2518 (defun dired-tree-down ()
2519 "Go down in the dired tree."
2521 (let ((dir (dired-current-directory)) ; has slash
2522 pos case-fold-search
) ; filenames are case sensitive
2523 (let ((rest (reverse dired-subdir-alist
)) elt
)
2525 (setq elt
(car rest
)
2527 (if (dired-in-this-tree (directory-file-name (car elt
)) dir
)
2529 pos
(dired-goto-subdir (car elt
))))))
2532 (error "At the bottom"))))
2536 (defun dired-unhide-subdir ()
2537 (let (buffer-read-only)
2538 (subst-char-in-region (dired-subdir-min) (dired-subdir-max) ?
\r ?
\n)))
2540 (defun dired-hide-check ()
2541 (or selective-display
2542 (error "selective-display must be t for subdir hiding to work!")))
2544 (defun dired-subdir-hidden-p (dir)
2545 (and selective-display
2547 (dired-goto-subdir dir
)
2548 (looking-at-p "\r"))))
2551 (defun dired-hide-subdir (arg)
2552 "Hide or unhide the current subdirectory and move to next directory.
2553 Optional prefix arg is a repeat factor.
2554 Use \\[dired-hide-all] to (un)hide all directories."
2557 (let ((modflag (buffer-modified-p)))
2558 (while (>= (setq arg
(1- arg
)) 0)
2559 (let* ((cur-dir (dired-current-directory))
2560 (hidden-p (dired-subdir-hidden-p cur-dir
))
2561 (elt (assoc cur-dir dired-subdir-alist
))
2562 (end-pos (1- (dired-get-subdir-max elt
)))
2564 ;; keep header line visible, hide rest
2565 (goto-char (dired-get-subdir-min elt
))
2566 (skip-chars-forward "^\n\r")
2568 (subst-char-in-region (point) end-pos ?
\r ?
\n)
2569 (subst-char-in-region (point) end-pos ?
\n ?
\r)))
2570 (dired-next-subdir 1 t
))
2571 (restore-buffer-modified-p modflag
)))
2574 (defun dired-hide-all (&optional ignored
)
2575 "Hide all subdirectories, leaving only their header lines.
2576 If there is already something hidden, make everything visible again.
2577 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2580 (let ((modflag (buffer-modified-p))
2583 (goto-char (point-min))
2584 (search-forward "\r" nil t
))
2585 ;; unhide - bombs on \r in filenames
2586 (subst-char-in-region (point-min) (point-max) ?
\r ?
\n)
2588 (let ((pos (point-max)) ; pos of end of last directory
2589 (alist dired-subdir-alist
))
2590 (while alist
; while there are dirs before pos
2591 (subst-char-in-region (dired-get-subdir-min (car alist
)) ; pos of prev dir
2593 (goto-char pos
) ; current dir
2594 ;; we're somewhere on current dir's line
2598 (setq pos
(dired-get-subdir-min (car alist
))) ; prev dir gets current dir
2599 (setq alist
(cdr alist
)))))
2600 (restore-buffer-modified-p modflag
)))
2602 ;;;###end dired-ins.el
2605 ;; Search only in file names in the Dired buffer.
2607 (defcustom dired-isearch-filenames nil
2608 "Non-nil to Isearch in file names only.
2609 If t, Isearch in Dired always matches only file names.
2610 If `dwim', Isearch matches file names when initial point position is on
2611 a file name. Otherwise, it searches the whole buffer without restrictions."
2612 :type
'(choice (const :tag
"No restrictions" nil
)
2613 (const :tag
"When point is on a file name initially, search file names" dwim
)
2614 (const :tag
"Always search in file names" t
))
2618 (define-minor-mode dired-isearch-filenames-mode
2619 "Toggle file names searching on or off.
2620 When on, Isearch skips matches outside file names using the predicate
2621 `dired-isearch-filter-filenames' that matches only at file names.
2622 When off, it uses the original predicate."
2624 (if dired-isearch-filenames-mode
2625 (add-function :before-while
(local 'isearch-filter-predicate
)
2626 #'dired-isearch-filter-filenames
2627 '((isearch-message-prefix .
"filename ")))
2628 (remove-function (local 'isearch-filter-predicate
)
2629 #'dired-isearch-filter-filenames
))
2631 (setq isearch-success t isearch-adjusted t
)
2635 (defun dired-isearch-filenames-setup ()
2636 "Set up isearch to search in Dired file names.
2637 Intended to be added to `isearch-mode-hook'."
2638 (when (or (eq dired-isearch-filenames t
)
2639 (and (eq dired-isearch-filenames
'dwim
)
2640 (get-text-property (point) 'dired-filename
)))
2641 (define-key isearch-mode-map
"\M-sff" 'dired-isearch-filenames-mode
)
2642 (dired-isearch-filenames-mode 1)
2643 (add-hook 'isearch-mode-end-hook
'dired-isearch-filenames-end nil t
)))
2645 (defun dired-isearch-filenames-end ()
2646 "Clean up the Dired file name search after terminating isearch."
2647 (define-key isearch-mode-map
"\M-sff" nil
)
2648 (dired-isearch-filenames-mode -
1)
2649 (remove-hook 'isearch-mode-end-hook
'dired-isearch-filenames-end t
))
2651 (defun dired-isearch-filter-filenames (beg end
)
2652 "Test whether the current search hit is a file name.
2653 Return non-nil if the text from BEG to END is part of a file
2654 name (has the text property `dired-filename')."
2655 (text-property-not-all (min beg end
) (max beg end
)
2656 'dired-filename nil
))
2659 (defun dired-isearch-filenames ()
2660 "Search for a string using Isearch only in file names in the Dired buffer."
2662 (let ((dired-isearch-filenames t
))
2663 (isearch-forward nil t
)))
2666 (defun dired-isearch-filenames-regexp ()
2667 "Search for a regexp using Isearch only in file names in the Dired buffer."
2669 (let ((dired-isearch-filenames t
))
2670 (isearch-forward-regexp nil t
)))
2673 ;; Functions for searching in tags style among marked files.
2676 (defun dired-do-isearch ()
2677 "Search for a string through all marked files using Isearch."
2679 (multi-isearch-files
2680 (dired-get-marked-files nil nil
'dired-nondirectory-p
)))
2683 (defun dired-do-isearch-regexp ()
2684 "Search for a regexp through all marked files using Isearch."
2686 (multi-isearch-files-regexp
2687 (dired-get-marked-files nil nil
'dired-nondirectory-p
)))
2690 (defun dired-do-search (regexp)
2691 "Search through all marked files for a match for REGEXP.
2692 Stops when a match is found.
2693 To continue searching for next match, use command \\[tags-loop-continue]."
2694 (interactive "sSearch marked files (regexp): ")
2695 (tags-search regexp
'(dired-get-marked-files nil nil
'dired-nondirectory-p
)))
2698 (defun dired-do-query-replace-regexp (from to
&optional delimited
)
2699 "Do `query-replace-regexp' of FROM with TO, on all marked files.
2700 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
2701 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
2702 with the command \\[tags-loop-continue]."
2705 (query-replace-read-args
2706 "Query replace regexp in marked files" t t
)))
2707 (list (nth 0 common
) (nth 1 common
) (nth 2 common
))))
2708 (dolist (file (dired-get-marked-files nil nil
'dired-nondirectory-p
))
2709 (let ((buffer (get-file-buffer file
)))
2710 (if (and buffer
(with-current-buffer buffer
2712 (error "File `%s' is visited read-only" file
))))
2713 (tags-query-replace from to delimited
2714 '(dired-get-marked-files nil nil
'dired-nondirectory-p
)))
2716 (declare-function xref--show-xrefs
"xref")
2717 (declare-function xref-query-replace-in-results
"xref")
2720 (defun dired-do-find-regexp (regexp)
2721 "Find all matches for REGEXP in all marked files, recursively."
2722 (interactive "sSearch marked files (regexp): ")
2724 (defvar grep-find-ignored-files
)
2725 (let* ((files (dired-get-marked-files))
2726 (ignores (nconc (mapcar
2727 (lambda (s) (concat s
"/"))
2728 vc-directory-exclusion-list
)
2729 grep-find-ignored-files
))
2732 (xref-collect-matches regexp
"*" file
2733 (and (file-directory-p file
)
2737 (user-error "No matches for: %s" regexp
))
2738 (xref--show-xrefs xrefs nil t
)))
2741 (defun dired-do-find-regexp-and-replace (from to
)
2742 "Replace matches of FROM with TO, in all marked files, recursively."
2745 (query-replace-read-args
2746 "Query replace regexp in marked files" t t
)))
2747 (list (nth 0 common
) (nth 1 common
))))
2748 (with-current-buffer (dired-do-find-regexp from
)
2749 (xref-query-replace-in-results from to
)))
2751 (defun dired-nondirectory-p (file)
2752 (not (file-directory-p file
)))
2755 (defun dired-show-file-type (file &optional deref-symlinks
)
2756 "Print the type of FILE, according to the `file' command.
2757 If you give a prefix to this command, and FILE is a symbolic
2758 link, then the type of the file linked to by FILE is printed
2760 (interactive (list (dired-get-filename t
) current-prefix-arg
))
2761 (let (process-file-side-effects)
2764 (process-file "file" nil t t
"-L" "--" file
)
2765 (process-file "file" nil t t
"--" file
))
2767 (backward-delete-char 1))
2768 (message "%s" (buffer-string)))))
2770 (provide 'dired-aux
)
2773 ;; byte-compile-dynamic: t
2774 ;; generated-autoload-file: "dired.el"
2777 ;;; dired-aux.el ends here