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