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