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