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