Commit | Line | Data |
---|---|---|
2f14b48d | 1 | ;; dired-aux.el --- directory browsing command support |
dd87891b | 2 | |
3a801d0c ER |
3 | ;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc. |
4 | ||
2f14b48d ER |
5 | ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>. |
6 | ;; Version: 5.234 | |
e5167999 | 7 | |
dd87891b RS |
8 | ;; This file is part of GNU Emacs. |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
dd87891b RS |
13 | ;; any later version. |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
22 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | ||
2f14b48d ER |
24 | ;;; Commentary: |
25 | ||
dd87891b RS |
26 | ;; Rewritten in 1990/1991 to add tree features, file marking and |
27 | ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>. | |
28 | ;; Finished up by rms in 1992. | |
29 | ||
2f14b48d ER |
30 | ;;; Code: |
31 | ||
dd87891b RS |
32 | ;;; 15K |
33 | ;;;###begin dired-cmd.el | |
34 | ;; Diffing and compressing | |
35 | ||
36 | ;;;###autoload | |
37 | (defun dired-diff (file &optional switches) | |
38 | "Compare file at point with file FILE using `diff'. | |
39 | FILE defaults to the file at the mark. | |
ab67260b | 40 | The prompted-for file is the first file given to `diff'." |
dd87891b RS |
41 | (interactive |
42 | (let ((default (if (mark) | |
43 | (save-excursion (goto-char (mark)) | |
44 | (dired-get-filename t t))))) | |
45 | (list (read-file-name (format "Diff %s with: %s" | |
46 | (dired-get-filename t) | |
47 | (if default | |
48 | (concat "(default " default ") ") | |
49 | "")) | |
50 | (dired-current-directory) default t) | |
51 | (if (fboundp 'diff-read-switches) | |
52 | (diff-read-switches "Options for diff: "))))) | |
53 | (if switches ; Emacs 19's diff has but two | |
54 | (diff file (dired-get-filename t) switches) ; args (yet ;-) | |
55 | (diff file (dired-get-filename t)))) | |
56 | ||
57 | ;;;###autoload | |
58 | (defun dired-backup-diff (&optional switches) | |
59 | "Diff this file with its backup file or vice versa. | |
60 | Uses the latest backup, if there are several numerical backups. | |
61 | If this file is a backup, diff it with its original. | |
ab67260b | 62 | The backup file is the first file given to `diff'." |
dd87891b RS |
63 | (interactive (list (if (fboundp 'diff-read-switches) |
64 | (diff-read-switches "Diff with switches: ")))) | |
ab67260b RS |
65 | (if switches |
66 | (diff-backup (dired-get-filename) switches) | |
67 | (diff-backup (dired-get-filename)))) | |
dd87891b RS |
68 | |
69 | (defun dired-do-chxxx (attribute-name program op-symbol arg) | |
70 | ;; Change file attributes (mode, group, owner) of marked files and | |
71 | ;; refresh their file lines. | |
72 | ;; ATTRIBUTE-NAME is a string describing the attribute to the user. | |
73 | ;; PROGRAM is the program used to change the attribute. | |
74 | ;; OP-SYMBOL is the type of operation (for use in dired-mark-pop-up). | |
75 | ;; ARG describes which files to use, as in dired-get-marked-files. | |
76 | (let* ((files (dired-get-marked-files t arg)) | |
77 | (new-attribute | |
78 | (dired-mark-read-string | |
79 | (concat "Change " attribute-name " of %s to: ") | |
80 | nil op-symbol arg files)) | |
81 | (operation (concat program " " new-attribute)) | |
82 | failures) | |
83 | (setq failures | |
84 | (dired-bunch-files 10000 | |
85 | (function dired-check-process) | |
86 | (list operation program new-attribute) | |
87 | files)) | |
88 | (dired-do-redisplay arg);; moves point if ARG is an integer | |
89 | (if failures | |
90 | (dired-log-summary | |
91 | (format "%s: error" operation) | |
92 | nil)))) | |
93 | ||
94 | ;;;###autoload | |
95 | (defun dired-do-chmod (&optional arg) | |
96 | "Change the mode of the marked (or next ARG) files. | |
97 | This calls chmod, thus symbolic modes like `g+w' are allowed." | |
98 | (interactive "P") | |
99 | (dired-do-chxxx "Mode" "chmod" 'chmod arg)) | |
100 | ||
101 | ;;;###autoload | |
102 | (defun dired-do-chgrp (&optional arg) | |
103 | "Change the group of the marked (or next ARG) files." | |
104 | (interactive "P") | |
105 | (dired-do-chxxx "Group" "chgrp" 'chgrp arg)) | |
106 | ||
107 | ;;;###autoload | |
108 | (defun dired-do-chown (&optional arg) | |
109 | "Change the owner of the marked (or next ARG) files." | |
110 | (interactive "P") | |
111 | (dired-do-chxxx "Owner" dired-chown-program 'chown arg)) | |
112 | ||
113 | ;; Process all the files in FILES in batches of a convenient size, | |
114 | ;; by means of (FUNCALL FUNCTION ARGS... SOME-FILES...). | |
115 | ;; Batches are chosen to need less than MAX chars for the file names, | |
116 | ;; allowing 3 extra characters of separator per file name. | |
117 | (defun dired-bunch-files (max function args files) | |
118 | (let (pending | |
119 | (pending-length 0) | |
120 | failures) | |
121 | ;; Accumulate files as long as they fit in MAX chars, | |
122 | ;; then process the ones accumulated so far. | |
123 | (while files | |
124 | (let* ((thisfile (car files)) | |
125 | (thislength (+ (length thisfile) 3)) | |
126 | (rest (cdr files))) | |
127 | ;; If we have at least 1 pending file | |
128 | ;; and this file won't fit in the length limit, process now. | |
129 | (if (and pending (> (+ thislength pending-length) max)) | |
130 | (setq failures | |
131 | (nconc (apply function (append args pending) pending) | |
132 | failures) | |
133 | pending nil | |
134 | pending-length 0)) | |
135 | ;; Do (setq pending (cons thisfile pending)) | |
136 | ;; but reuse the cons that was in `files'. | |
137 | (setcdr files pending) | |
138 | (setq pending files) | |
139 | (setq pending-length (+ thislength pending-length)) | |
140 | (setq files rest))) | |
141 | (nconc (apply function (append args pending) pending) | |
142 | failures))) | |
143 | ||
144 | ;;;###autoload | |
145 | (defun dired-do-print (&optional arg) | |
146 | "Print the marked (or next ARG) files. | |
147 | Uses the shell command coming from variables `lpr-command' and | |
148 | `lpr-switches' as default." | |
149 | (interactive "P") | |
150 | (let* ((file-list (dired-get-marked-files t arg)) | |
151 | (command (dired-mark-read-string | |
152 | "Print %s with: " | |
153 | (apply 'concat lpr-command " " lpr-switches) | |
154 | 'print arg file-list))) | |
155 | (dired-run-shell-command (dired-shell-stuff-it command file-list nil)))) | |
156 | ||
157 | ;; Read arguments for a marked-files command that wants a string | |
158 | ;; that is not a file name, | |
159 | ;; perhaps popping up the list of marked files. | |
160 | ;; ARG is the prefix arg and indicates whether the files came from | |
161 | ;; marks (ARG=nil) or a repeat factor (integerp ARG). | |
162 | ;; If the current file was used, the list has but one element and ARG | |
163 | ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)). | |
164 | ||
165 | (defun dired-mark-read-string (prompt initial op-symbol arg files) | |
166 | ;; PROMPT for a string, with INITIAL input. | |
167 | ;; Other args are used to give user feedback and pop-up: | |
168 | ;; OP-SYMBOL of command, prefix ARG, marked FILES. | |
169 | (dired-mark-pop-up | |
170 | nil op-symbol files | |
171 | (function read-string) | |
172 | (format prompt (dired-mark-prompt arg files)) initial)) | |
173 | \f | |
174 | ;;; Shell commands | |
175 | ;;>>> install (move this function into simple.el) | |
176 | (defun dired-shell-quote (filename) | |
177 | "Quote a file name for inferior shell (see variable `shell-file-name')." | |
178 | ;; Quote everything except POSIX filename characters. | |
179 | ;; This should be safe enough even for really wierd shells. | |
180 | (let ((result "") (start 0) end) | |
181 | (while (string-match "[^---0-9a-zA-Z_./]" filename start) | |
182 | (setq end (match-beginning 0) | |
183 | result (concat result (substring filename start end) | |
184 | "\\" (substring filename end (1+ end))) | |
185 | start (1+ end))) | |
186 | (concat result (substring filename start)))) | |
187 | ||
188 | (defun dired-read-shell-command (prompt arg files) | |
189 | ;; "Read a dired shell command prompting with PROMPT (using read-string). | |
190 | ;;ARG is the prefix arg and may be used to indicate in the prompt which | |
191 | ;; files are affected. | |
192 | ;;This is an extra function so that you can redefine it, e.g., to use gmhist." | |
193 | (dired-mark-pop-up | |
194 | nil 'shell files | |
195 | (function read-string) | |
196 | (format prompt (dired-mark-prompt arg files)))) | |
197 | ||
198 | ;; The in-background argument is only needed in Emacs 18 where | |
199 | ;; shell-command doesn't understand an appended ampersand `&'. | |
200 | ;;;###autoload | |
201 | (defun dired-do-shell-command (&optional arg in-background) | |
202 | "Run a shell command on the marked files. | |
203 | If there is output, it goes to a separate buffer. | |
204 | Normally the command is run on each file individually. | |
205 | However, if there is a `*' in the command then it is run | |
206 | just once with the entire file list substituted there. | |
207 | ||
208 | If no files are marked or a specific numeric prefix arg is given, | |
209 | the next ARG files are used. Just \\[universal-argument] means the current file. | |
210 | The prompt mentions the file(s) or the marker, as appropriate. | |
211 | ||
212 | No automatic redisplay is attempted, as the file names may have | |
213 | changed. Type \\[dired-do-redisplay] to redisplay the marked files. | |
214 | ||
215 | The shell command has the top level directory as working directory, so | |
216 | output files usually are created there instead of in a subdir." | |
217 | ;;Functions dired-run-shell-command and dired-shell-stuff-it do the | |
218 | ;;actual work and can be redefined for customization. | |
219 | (interactive "P") | |
220 | (let* ((on-each (not (string-match "\\*" command))) | |
221 | (prompt (concat (if in-background "& on " "! on ") | |
222 | (if on-each "each " "") | |
223 | "%s: ")) | |
224 | (file-list (dired-get-marked-files t arg)) | |
225 | ;; Want to give feedback whether this file or marked files are used: | |
226 | (command (dired-read-shell-command | |
227 | prompt arg file-list))) | |
228 | (if on-each | |
229 | (dired-bunch-files | |
230 | (- 10000 (length command)) | |
231 | (function (lambda (&rest files) | |
232 | (dired-run-shell-command | |
233 | (dired-shell-stuff-it command files t arg)) | |
234 | in-background)) | |
235 | nil | |
236 | file-list) | |
237 | ;; execute the shell command | |
238 | (dired-run-shell-command | |
239 | (dired-shell-stuff-it command file-list nil arg) | |
240 | in-background)))) | |
241 | ||
242 | ;; Might use {,} for bash or csh: | |
243 | (defvar dired-mark-prefix "" | |
244 | "Prepended to marked files in dired shell commands.") | |
245 | (defvar dired-mark-postfix "" | |
246 | "Appended to marked files in dired shell commands.") | |
247 | (defvar dired-mark-separator " " | |
248 | "Separates marked files in dired shell commands.") | |
249 | ||
250 | (defun dired-shell-stuff-it (command file-list on-each &optional raw-arg) | |
251 | ;; "Make up a shell command line from COMMAND and FILE-LIST. | |
252 | ;; If ON-EACH is t, COMMAND should be applied to each file, else | |
253 | ;; simply concat all files and apply COMMAND to this. | |
254 | ;; FILE-LIST's elements will be quoted for the shell." | |
255 | ;; Might be redefined for smarter things and could then use RAW-ARG | |
256 | ;; (coming from interactive P and currently ignored) to decide what to do. | |
257 | ;; Smart would be a way to access basename or extension of file names. | |
258 | ;; See dired-trns.el for an approach to this. | |
259 | ;; Bug: There is no way to quote a * | |
260 | ;; On the other hand, you can never accidentally get a * into your cmd. | |
261 | (let ((stuff-it | |
262 | (if (string-match "\\*" command) | |
263 | (function (lambda (x) | |
264 | (dired-replace-in-string "\\*" x command))) | |
265 | (function (lambda (x) (concat command " " x)))))) | |
266 | (if on-each | |
267 | (mapconcat stuff-it (mapcar 'dired-shell-quote file-list) ";") | |
268 | (let ((fns (mapconcat 'dired-shell-quote | |
269 | file-list dired-mark-separator))) | |
270 | (if (> (length file-list) 1) | |
271 | (setq fns (concat dired-mark-prefix fns dired-mark-postfix))) | |
272 | (funcall stuff-it fns))))) | |
273 | ||
274 | ;; This is an extra function so that it can be redefined by ange-ftp. | |
275 | (defun dired-run-shell-command (command &optional in-background) | |
276 | (if (not in-background) | |
277 | (shell-command command) | |
278 | ;; We need this only in Emacs 18 (19's shell command has `&'). | |
279 | ;; comint::background is defined in emacs-19.el. | |
280 | (comint::background command))) | |
281 | \f | |
282 | ;; In Emacs 19 this will return program's exit status. | |
283 | ;; This is a separate function so that ange-ftp can redefine it. | |
284 | (defun dired-call-process (program discard &rest arguments) | |
285 | ; "Run PROGRAM with output to current buffer unless DISCARD is t. | |
286 | ;Remaining arguments are strings passed as command arguments to PROGRAM." | |
287 | (apply 'call-process program nil (not discard) nil arguments)) | |
288 | ||
289 | (defun dired-check-process (msg program &rest arguments) | |
290 | ; "Display MSG while running PROGRAM, and check for output. | |
291 | ;Remaining arguments are strings passed as command arguments to PROGRAM. | |
292 | ; On error, insert output | |
293 | ; in a log buffer and return the offending ARGUMENTS or PROGRAM. | |
294 | ; Caller can cons up a list of failed args. | |
295 | ;Else returns nil for success." | |
296 | (let (err-buffer err (dir default-directory)) | |
297 | (message "%s..." msg) | |
298 | (save-excursion | |
299 | ;; Get a clean buffer for error output: | |
300 | (setq err-buffer (get-buffer-create " *dired-check-process output*")) | |
301 | (set-buffer err-buffer) | |
302 | (erase-buffer) | |
303 | (setq default-directory dir ; caller's default-directory | |
304 | err (/= 0 | |
305 | (apply (function dired-call-process) program nil arguments))) | |
306 | (if err | |
307 | (progn | |
308 | (dired-log (concat program " " (prin1-to-string arguments) "\n")) | |
309 | (dired-log err-buffer) | |
310 | (or arguments program t)) | |
311 | (kill-buffer err-buffer) | |
312 | (message "%s...done" msg) | |
313 | nil)))) | |
314 | \f | |
315 | ;; Commands that delete or redisplay part of the dired buffer. | |
316 | ||
317 | ;;;###autoload | |
318 | (defun dired-kill-line-or-subdir (&optional arg) | |
319 | "Kill this line (but don't delete its file). | |
320 | Optional prefix argument is a repeat factor. | |
321 | If file is displayed as in situ subdir, kill that as well. | |
322 | If on a subdir headerline, kill whole subdir." | |
323 | (interactive "p") | |
324 | (if (dired-get-subdir) | |
325 | (dired-kill-subdir) | |
326 | (dired-kill-line arg))) | |
327 | ||
328 | (defun dired-kill-line (&optional arg) | |
329 | (interactive "P") | |
330 | (setq arg (prefix-numeric-value arg)) | |
331 | (let (buffer-read-only file) | |
332 | (while (/= 0 arg) | |
333 | (setq file (dired-get-filename nil t)) | |
334 | (if (not file) | |
335 | (error "Can only kill file lines.") | |
336 | (save-excursion (and file | |
337 | (dired-goto-subdir file) | |
338 | (dired-kill-subdir))) | |
339 | (delete-region (progn (beginning-of-line) (point)) | |
340 | (progn (forward-line 1) (point))) | |
341 | (if (> arg 0) | |
342 | (setq arg (1- arg)) | |
343 | (setq arg (1+ arg)) | |
344 | (forward-line -1)))) | |
345 | (dired-move-to-filename))) | |
346 | ||
347 | ;;;###autoload | |
348 | (defun dired-do-kill-lines (&optional arg fmt) | |
349 | "Kill all marked lines (not the files). | |
350 | With a prefix arg, kill all lines not marked or flagged." | |
351 | ;; Returns count of killed lines. FMT="" suppresses message. | |
352 | (interactive "P") | |
353 | (save-excursion | |
354 | (goto-char (point-min)) | |
355 | (let (buffer-read-only (count 0)) | |
356 | (if (not arg) ; kill marked lines | |
357 | (let ((regexp (dired-marker-regexp))) | |
358 | (while (and (not (eobp)) | |
359 | (re-search-forward regexp nil t)) | |
360 | (setq count (1+ count)) | |
361 | (delete-region (progn (beginning-of-line) (point)) | |
362 | (progn (forward-line 1) (point))))) | |
363 | ;; else kill unmarked lines | |
364 | (while (not (eobp)) | |
365 | (if (or (dired-between-files) | |
366 | (not (looking-at "^ "))) | |
367 | (forward-line 1) | |
368 | (setq count (1+ count)) | |
369 | (delete-region (point) (save-excursion | |
370 | (forward-line 1) | |
371 | (point)))))) | |
372 | (or (equal "" fmt) | |
373 | (message (or fmt "Killed %d line%s.") count (dired-plural-s count))) | |
374 | count))) | |
375 | ||
376 | ;;;###end dired-cmd.el | |
377 | \f | |
378 | ;;; 30K | |
379 | ;;;###begin dired-cp.el | |
380 | ||
381 | (defun dired-compress () | |
382 | ;; Compress or uncompress the current file. | |
383 | ;; Return nil for success, offending filename else. | |
384 | (let* (buffer-read-only | |
385 | (from-file (dired-get-filename))) | |
386 | (cond ((save-excursion (beginning-of-line) | |
387 | (looking-at dired-re-sym)) | |
388 | (dired-log (concat "Attempt to compress a symbolic link:\n" | |
389 | from-file)) | |
390 | (dired-make-relative from-file)) | |
391 | ((string-match "\\.Z$" from-file) | |
392 | (if (dired-check-process (concat "Uncompressing " from-file) | |
393 | "uncompress" from-file) | |
394 | (dired-make-relative from-file) | |
395 | (dired-update-file-line (substring from-file 0 -2)))) | |
396 | (t | |
397 | (if (dired-check-process (concat "Compressing " from-file) | |
398 | "compress" "-f" from-file) | |
399 | ;; Errors from the process are already logged. | |
400 | (dired-make-relative from-file) | |
401 | (dired-update-file-line (concat from-file ".Z"))))) | |
402 | nil)) | |
403 | \f | |
404 | (defun dired-mark-confirm (op-symbol arg) | |
405 | ;; Request confirmation from the user that the operation described | |
406 | ;; by OP-SYMBOL is to be performed on the marked files. | |
407 | ;; Confirmation consists in a y-or-n question with a file list | |
408 | ;; pop-up unless OP-SYMBOL is a member of `dired-no-confirm'. | |
409 | ;; The files used are determined by ARG (as in dired-get-marked-files). | |
410 | (or (memq op-symbol dired-no-confirm) | |
411 | (let ((files (dired-get-marked-files t arg))) | |
412 | (dired-mark-pop-up nil op-symbol files (function y-or-n-p) | |
413 | (concat (capitalize (symbol-name op-symbol)) " " | |
414 | (dired-mark-prompt arg files) "? "))))) | |
415 | ||
416 | (defun dired-map-over-marks-check (fun arg op-symbol &optional show-progress) | |
417 | ; "Map FUN over marked files (with second ARG like in dired-map-over-marks) | |
418 | ; and display failures. | |
419 | ||
420 | ; FUN takes zero args. It returns non-nil (the offending object, e.g. | |
421 | ; the short form of the filename) for a failure and probably logs a | |
422 | ; detailed error explanation using function `dired-log'. | |
423 | ||
424 | ; OP-SYMBOL is a symbol describing the operation performed (e.g. | |
425 | ; `compress'). It is used with `dired-mark-pop-up' to prompt the user | |
426 | ; (e.g. with `Compress * [2 files]? ') and to display errors (e.g. | |
427 | ; `Failed to compress 1 of 2 files - type W to see why ("foo")') | |
428 | ||
429 | ; SHOW-PROGRESS if non-nil means redisplay dired after each file." | |
430 | (if (dired-mark-confirm op-symbol arg) | |
431 | (let* ((total-list;; all of FUN's return values | |
432 | (dired-map-over-marks (funcall fun) arg show-progress)) | |
433 | (total (length total-list)) | |
434 | (failures (delq nil total-list)) | |
435 | (count (length failures))) | |
436 | (if (not failures) | |
437 | (message "%s: %d file%s." | |
438 | (capitalize (symbol-name op-symbol)) | |
439 | total (dired-plural-s total)) | |
440 | ;; end this bunch of errors: | |
441 | (dired-log-summary | |
442 | (format "Failed to %s %d of %d file%s" | |
443 | (symbol-name op-symbol) count total (dired-plural-s total)) | |
444 | failures))))) | |
445 | ||
446 | (defvar dired-query-alist | |
447 | '((?\y . y) (?\040 . y) ; `y' or SPC means accept once | |
448 | (?n . n) (?\177 . n) ; `n' or DEL skips once | |
449 | (?! . yes) ; `!' accepts rest | |
450 | (?q. no) (?\e . no) ; `q' or ESC skips rest | |
451 | ;; None of these keys quit - use C-g for that. | |
452 | )) | |
453 | ||
454 | (defun dired-query (qs-var qs-prompt &rest qs-args) | |
455 | ;; Query user and return nil or t. | |
456 | ;; Store answer in symbol VAR (which must initially be bound to nil). | |
457 | ;; Format PROMPT with ARGS. | |
458 | ;; Binding variable help-form will help the user who types C-h. | |
459 | (let* ((char (symbol-value qs-var)) | |
460 | (action (cdr (assoc char dired-query-alist)))) | |
461 | (cond ((eq 'yes action) | |
462 | t) ; accept, and don't ask again | |
463 | ((eq 'no action) | |
464 | nil) ; skip, and don't ask again | |
465 | (t;; no lasting effects from last time we asked - ask now | |
466 | (let ((qprompt (concat qs-prompt | |
467 | (if help-form | |
468 | (format " [Type yn!q or %s] " | |
469 | (key-description | |
470 | (char-to-string help-char))) | |
471 | " [Type y, n, q or !] "))) | |
472 | result elt) | |
473 | ;; Actually it looks nicer without cursor-in-echo-area - you can | |
474 | ;; look at the dired buffer instead of at the prompt to decide. | |
475 | (apply 'message qprompt qs-args) | |
476 | (setq char (set qs-var (read-char))) | |
477 | (while (not (setq elt (assoc char dired-query-alist))) | |
478 | (message "Invalid char - type %c for help." help-char) | |
479 | (ding) | |
480 | (sit-for 1) | |
481 | (apply 'message qprompt qs-args) | |
482 | (setq char (set qs-var (read-char)))) | |
483 | (memq (cdr elt) '(t y yes))))))) | |
484 | \f | |
485 | ;;;###autoload | |
486 | (defun dired-do-compress (&optional arg) | |
487 | "Compress or uncompress marked (or next ARG) files." | |
488 | (interactive "P") | |
489 | (dired-map-over-marks-check (function dired-compress) arg 'compress t)) | |
490 | ||
491 | ;; Commands for Emacs Lisp files - load and byte compile | |
492 | ||
493 | (defun dired-byte-compile () | |
494 | ;; Return nil for success, offending file name else. | |
495 | (let* ((filename (dired-get-filename)) | |
496 | (elc-file | |
497 | (if (eq system-type 'vax-vms) | |
498 | (concat (substring filename 0 (string-match ";" filename)) "c") | |
499 | (concat filename "c"))) | |
500 | buffer-read-only failure) | |
501 | (condition-case err | |
502 | (save-excursion (byte-compile-file filename)) | |
503 | (error | |
504 | (setq failure err))) | |
505 | (if failure | |
506 | (progn | |
507 | (dired-log "Byte compile error for %s:\n%s\n" filename failure) | |
508 | (dired-make-relative filename)) | |
509 | (dired-remove-file elc-file) | |
510 | (forward-line) ; insert .elc after its .el file | |
511 | (dired-add-file elc-file) | |
512 | nil))) | |
513 | ||
514 | ;;;###autoload | |
515 | (defun dired-do-byte-compile (&optional arg) | |
516 | "Byte compile marked (or next ARG) Emacs Lisp files." | |
517 | (interactive "P") | |
518 | (dired-map-over-marks-check (function dired-byte-compile) arg 'byte-compile t)) | |
519 | ||
520 | (defun dired-load () | |
521 | ;; Return nil for success, offending file name else. | |
522 | (let ((file (dired-get-filename)) failure) | |
523 | (condition-case err | |
524 | (load file nil nil t) | |
525 | (error (setq failure err))) | |
526 | (if (not failure) | |
527 | nil | |
528 | (dired-log "Load error for %s:\n%s\n" file failure) | |
529 | (dired-make-relative file)))) | |
530 | ||
531 | ;;;###autoload | |
532 | (defun dired-do-load (&optional arg) | |
533 | "Load the marked (or next ARG) Emacs Lisp files." | |
534 | (interactive "P") | |
535 | (dired-map-over-marks-check (function dired-load) arg 'load t)) | |
536 | ||
537 | ;;;###autoload | |
538 | (defun dired-do-redisplay (&optional arg test-for-subdir) | |
539 | "Redisplay all marked (or next ARG) files. | |
540 | If on a subdir line, redisplay that subdirectory. In that case, | |
541 | a prefix arg lets you edit the `ls' switches used for the new listing." | |
542 | ;; Moves point if the next ARG files are redisplayed. | |
543 | (interactive "P\np") | |
544 | (if (and test-for-subdir (dired-get-subdir)) | |
545 | (dired-insert-subdir | |
546 | (dired-get-subdir) | |
547 | (if arg (read-string "Switches for listing: " dired-actual-switches))) | |
548 | (message "Redisplaying...") | |
549 | ;; message much faster than making dired-map-over-marks show progress | |
550 | (dired-map-over-marks (let ((fname (dired-get-filename))) | |
551 | (message "Redisplaying... %s" fname) | |
552 | (dired-update-file-line fname)) | |
553 | arg) | |
554 | (dired-move-to-filename) | |
555 | (message "Redisplaying...done"))) | |
556 | \f | |
557 | (defun dired-update-file-line (file) | |
558 | ;; Delete the current line, and insert an entry for FILE. | |
559 | ;; If FILE is nil, then just delete the current line. | |
560 | ;; Keeps any marks that may be present in column one (doing this | |
561 | ;; here is faster than with dired-add-entry's optional arg). | |
562 | ;; Does not update other dired buffers. Use dired-relist-entry for that. | |
563 | (beginning-of-line) | |
564 | (let ((char (following-char)) (opoint (point))) | |
565 | (delete-region (point) (progn (forward-line 1) (point))) | |
566 | (if file | |
567 | (progn | |
568 | (dired-add-entry file) | |
569 | ;; Replace space by old marker without moving point. | |
570 | ;; Faster than goto+insdel inside a save-excursion? | |
571 | (subst-char-in-region opoint (1+ opoint) ?\040 char)))) | |
572 | (dired-move-to-filename)) | |
573 | ||
574 | (defun dired-fun-in-all-buffers (directory fun &rest args) | |
575 | ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS. | |
576 | ;; Return list of buffers where FUN succeeded (i.e., returned non-nil). | |
577 | (let ((buf-list (dired-buffers-for-dir directory)) | |
578 | (obuf (current-buffer)) | |
579 | buf success-list) | |
580 | (while buf-list | |
581 | (setq buf (car buf-list) | |
582 | buf-list (cdr buf-list)) | |
583 | (unwind-protect | |
584 | (progn | |
585 | (set-buffer buf) | |
586 | (if (apply fun args) | |
587 | (setq success-list (cons (buffer-name buf) success-list)))) | |
588 | (set-buffer obuf))) | |
589 | success-list)) | |
590 | ||
591 | (defun dired-add-file (filename &optional marker-char) | |
592 | (dired-fun-in-all-buffers | |
593 | (file-name-directory filename) | |
594 | (function dired-add-entry) filename marker-char)) | |
595 | ||
596 | (defun dired-add-entry (filename &optional marker-char) | |
597 | ;; Add a new entry for FILENAME, optionally marking it | |
598 | ;; with MARKER-CHAR (a character, else dired-marker-char is used). | |
599 | ;; Note that this adds the entry `out of order' if files sorted by | |
600 | ;; time, etc. | |
601 | ;; At least this version inserts in the right subdirectory (if present). | |
602 | ;; And it skips "." or ".." (see `dired-trivial-filenames'). | |
603 | ;; Hidden subdirs are exposed if a file is added there. | |
604 | (setq filename (directory-file-name filename)) | |
605 | ;; Entry is always for files, even if they happen to also be directories | |
606 | (let ((opoint (point)) | |
607 | (cur-dir (dired-current-directory)) | |
608 | (directory (file-name-directory filename)) | |
609 | reason) | |
610 | (setq filename (file-name-nondirectory filename) | |
611 | reason | |
612 | (catch 'not-found | |
613 | (if (string= directory cur-dir) | |
614 | (progn | |
615 | (skip-chars-forward "^\r\n") | |
616 | (if (eq (following-char) ?\r) | |
617 | (dired-unhide-subdir)) | |
618 | ;; We are already where we should be, except when | |
619 | ;; point is before the subdir line or its total line. | |
620 | (let ((p (dired-after-subdir-garbage cur-dir))) | |
621 | (if (< (point) p) | |
622 | (goto-char p)))) | |
623 | ;; else try to find correct place to insert | |
624 | (if (dired-goto-subdir directory) | |
625 | (progn;; unhide if necessary | |
626 | (if (looking-at "\r");; point is at end of subdir line | |
627 | (dired-unhide-subdir)) | |
628 | ;; found - skip subdir and `total' line | |
629 | ;; and uninteresting files like . and .. | |
630 | ;; This better not moves into the next subdir! | |
631 | (dired-goto-next-nontrivial-file)) | |
632 | ;; not found | |
633 | (throw 'not-found "Subdir not found"))) | |
634 | ;; found and point is at The Right Place: | |
635 | (let (buffer-read-only) | |
636 | (beginning-of-line) | |
637 | (dired-add-entry-do-indentation marker-char) | |
638 | (dired-ls (dired-make-absolute filename directory);; don't expand `.' ! | |
639 | (concat dired-actual-switches "d")) | |
640 | (forward-line -1) | |
641 | ;; We want to have the non-directory part, only: | |
642 | (let* ((beg (dired-move-to-filename t)) ; error for strange output | |
643 | (end (dired-move-to-end-of-filename))) | |
644 | (setq filename (buffer-substring beg end)) | |
645 | (delete-region beg end) | |
646 | (insert (file-name-nondirectory filename))) | |
647 | (if dired-after-readin-hook;; the subdir-alist is not affected... | |
648 | (save-excursion;; ...so we can run it right now: | |
649 | (save-restriction | |
650 | (beginning-of-line) | |
651 | (narrow-to-region (point) (save-excursion | |
652 | (forward-line 1) (point))) | |
653 | (run-hooks 'dired-after-readin-hook)))) | |
654 | (dired-move-to-filename)) | |
655 | ;; return nil if all went well | |
656 | nil)) | |
657 | (if reason ; don't move away on failure | |
658 | (goto-char opoint)) | |
659 | (not reason))) ; return t on succes, nil else | |
660 | ||
661 | ;; This is a separate function for the sake of nested dired format. | |
662 | (defun dired-add-entry-do-indentation (marker-char) | |
663 | ;; two spaces or a marker plus a space: | |
664 | (insert (if marker-char | |
665 | (if (integerp marker-char) marker-char dired-marker-char) | |
666 | ?\040) | |
667 | ?\040)) | |
668 | ||
669 | (defun dired-after-subdir-garbage (dir) | |
670 | ;; Return pos of first file line of DIR, skipping header and total | |
671 | ;; or wildcard lines. | |
672 | ;; Important: never moves into the next subdir. | |
673 | ;; DIR is assumed to be unhidden. | |
674 | ;; Will probably be redefined for VMS etc. | |
675 | (save-excursion | |
676 | (or (dired-goto-subdir dir) (error "This cannot happen")) | |
677 | (forward-line 1) | |
678 | (while (and (not (eolp)) ; don't cross subdir boundary | |
679 | (not (dired-move-to-filename))) | |
680 | (forward-line 1)) | |
681 | (point))) | |
682 | ||
683 | (defun dired-remove-file (file) | |
684 | (dired-fun-in-all-buffers | |
685 | (file-name-directory file) (function dired-remove-entry) file)) | |
686 | ||
687 | (defun dired-remove-entry (file) | |
688 | (save-excursion | |
689 | (and (dired-goto-file file) | |
690 | (let (buffer-read-only) | |
691 | (delete-region (progn (beginning-of-line) (point)) | |
692 | (save-excursion (forward-line 1) (point))))))) | |
693 | ||
694 | (defun dired-relist-file (file) | |
695 | (dired-fun-in-all-buffers (file-name-directory file) | |
696 | (function dired-relist-entry) file)) | |
697 | ||
698 | (defun dired-relist-entry (file) | |
699 | ;; Relist the line for FILE, or just add it if it did not exist. | |
700 | ;; FILE must be an absolute pathname. | |
701 | (let (buffer-read-only marker) | |
702 | ;; If cursor is already on FILE's line delete-region will cause | |
703 | ;; save-excursion to fail because of floating makers, | |
704 | ;; moving point to beginning of line. Sigh. | |
705 | (save-excursion | |
706 | (and (dired-goto-file file) | |
707 | (delete-region (progn (beginning-of-line) | |
708 | (setq marker (following-char)) | |
709 | (point)) | |
710 | (save-excursion (forward-line 1) (point)))) | |
711 | (setq file (directory-file-name file)) | |
712 | (dired-add-entry file (if (eq ?\040 marker) nil marker))))) | |
713 | \f | |
714 | ;;; Copy, move/rename, making hard and symbolic links | |
715 | ||
716 | (defvar dired-backup-overwrite nil | |
717 | "*Non-nil if Dired should ask about making backups before overwriting files. | |
718 | Special value `always' suppresses confirmation.") | |
719 | ||
720 | (defun dired-handle-overwrite (to) | |
721 | ;; Save old version of a to be overwritten file TO. | |
722 | ;; `overwrite-confirmed' and `overwrite-backup-query' are fluid vars | |
723 | ;; from dired-create-files. | |
724 | (if (and dired-backup-overwrite | |
725 | overwrite-confirmed | |
726 | (or (eq 'always dired-backup-overwrite) | |
727 | (dired-query 'overwrite-backup-query | |
728 | (format "Make backup for existing file `%s'? " to)))) | |
729 | (let ((backup (car (find-backup-file-name to)))) | |
730 | (rename-file to backup 0) ; confirm overwrite of old backup | |
731 | (dired-relist-entry backup)))) | |
732 | ||
733 | (defun dired-copy-file (from to ok-flag) | |
734 | (dired-handle-overwrite to) | |
735 | (copy-file from to ok-flag dired-copy-preserve-time)) | |
736 | ||
737 | (defun dired-rename-file (from to ok-flag) | |
738 | (dired-handle-overwrite to) | |
739 | (rename-file from to ok-flag) ; error is caught in -create-files | |
740 | ;; Silently rename the visited file of any buffer visiting this file. | |
741 | (and (get-file-buffer from) | |
742 | (save-excursion | |
743 | (set-buffer (get-file-buffer from)) | |
744 | (let ((modflag (buffer-modified-p))) | |
745 | (set-visited-file-name to) | |
746 | (set-buffer-modified-p modflag)))) | |
747 | (dired-remove-file from) | |
748 | ;; See if it's an inserted subdir, and rename that, too. | |
749 | (dired-rename-subdir from to)) | |
750 | ||
751 | (defun dired-rename-subdir (from-dir to-dir) | |
752 | (setq from-dir (file-name-as-directory from-dir) | |
753 | to-dir (file-name-as-directory to-dir)) | |
754 | (dired-fun-in-all-buffers from-dir | |
755 | (function dired-rename-subdir-1) from-dir to-dir) | |
756 | ;; Update visited file name of all affected buffers | |
757 | (let ((blist (buffer-list))) | |
758 | (while blist | |
759 | (save-excursion | |
760 | (set-buffer (car blist)) | |
761 | (if (and buffer-file-name | |
762 | (dired-in-this-tree buffer-file-name from-dir)) | |
763 | (let ((modflag (buffer-modified-p)) | |
764 | (to-file (dired-replace-in-string | |
765 | (concat "^" (regexp-quote from-dir)) | |
766 | to-dir | |
767 | buffer-file-name))) | |
768 | (set-visited-file-name to-file) | |
769 | (set-buffer-modified-p modflag)))) | |
770 | (setq blist (cdr blist))))) | |
771 | ||
772 | (defun dired-rename-subdir-1 (dir to) | |
773 | ;; Rename DIR to TO in headerlines and dired-subdir-alist, if DIR or | |
774 | ;; one of its subdirectories is expanded in this buffer. | |
775 | (let ((alist dired-subdir-alist) | |
776 | (elt nil)) | |
777 | (while alist | |
778 | (setq elt (car alist) | |
779 | alist (cdr alist)) | |
780 | (if (dired-in-this-tree (car elt) dir) | |
781 | ;; ELT's subdir is affected by the rename | |
782 | (dired-rename-subdir-2 elt dir to))) | |
783 | (if (equal dir default-directory) | |
784 | ;; if top level directory was renamed, lots of things have to be | |
785 | ;; updated: | |
786 | (progn | |
787 | (dired-unadvertise dir) ; we no longer dired DIR... | |
788 | (setq default-directory to | |
789 | dired-directory (expand-file-name;; this is correct | |
790 | ;; with and without wildcards | |
791 | (file-name-nondirectory dired-directory) | |
792 | to)) | |
793 | (let ((new-name (file-name-nondirectory | |
794 | (directory-file-name dired-directory)))) | |
795 | ;; try to rename buffer, but just leave old name if new | |
796 | ;; name would already exist (don't try appending "<%d>") | |
797 | (or (get-buffer new-name) | |
798 | (rename-buffer new-name))) | |
799 | ;; ... we dired TO now: | |
800 | (dired-advertise))))) | |
801 | ||
802 | (defun dired-rename-subdir-2 (elt dir to) | |
803 | ;; Update the headerline and dired-subdir-alist element of directory | |
804 | ;; described by alist-element ELT to reflect the moving of DIR to TO. | |
805 | ;; Thus, ELT describes either DIR itself or a subdir of DIR. | |
806 | (save-excursion | |
807 | (let ((regexp (regexp-quote (directory-file-name dir))) | |
808 | (newtext (directory-file-name to)) | |
809 | buffer-read-only) | |
810 | (goto-char (dired-get-subdir-min elt)) | |
811 | ;; Update subdir headerline in buffer | |
812 | (if (not (looking-at dired-subdir-regexp)) | |
813 | (error "%s not found where expected - dired-subdir-alist broken?" | |
814 | dir) | |
815 | (goto-char (match-beginning 1)) | |
816 | (if (re-search-forward regexp (match-end 1) t) | |
817 | (replace-match newtext t t) | |
818 | (error "Expected to find `%s' in headerline of %s" dir (car elt)))) | |
819 | ;; Update buffer-local dired-subdir-alist | |
820 | (setcar elt | |
821 | (dired-normalize-subdir | |
822 | (dired-replace-in-string regexp newtext (car elt))))))) | |
823 | \f | |
824 | ;; Cloning replace-match to work on strings instead of in buffer: | |
825 | ;; The FIXEDCASE parameter of replace-match is not implemented. | |
826 | ;;;###autoload | |
827 | (defun dired-string-replace-match (regexp string newtext | |
828 | &optional literal global) | |
829 | "Replace first match of REGEXP in STRING with NEWTEXT. | |
830 | If it does not match, nil is returned instead of the new string. | |
831 | Optional arg LITERAL means to take NEWTEXT literally. | |
832 | Optional arg GLOBAL means to replace all matches." | |
833 | (if global | |
834 | (let ((result "") (start 0) mb me) | |
835 | (while (string-match regexp string start) | |
836 | (setq mb (match-beginning 0) | |
837 | me (match-end 0) | |
838 | result (concat result | |
839 | (substring string start mb) | |
840 | (if literal | |
841 | newtext | |
842 | (dired-expand-newtext string newtext))) | |
843 | start me)) | |
844 | (if mb ; matched at least once | |
845 | (concat result (substring string start)) | |
846 | nil)) | |
847 | ;; not GLOBAL | |
848 | (if (not (string-match regexp string 0)) | |
849 | nil | |
850 | (concat (substring string 0 (match-beginning 0)) | |
851 | (if literal newtext (dired-expand-newtext string newtext)) | |
852 | (substring string (match-end 0)))))) | |
853 | ||
854 | (defun dired-expand-newtext (string newtext) | |
855 | ;; Expand \& and \1..\9 (referring to STRING) in NEWTEXT, using match data. | |
856 | ;; Note that in Emacs 18 match data are clipped to current buffer | |
857 | ;; size...so the buffer should better not be smaller than STRING. | |
858 | (let ((pos 0) | |
859 | (len (length newtext)) | |
860 | (expanded-newtext "")) | |
861 | (while (< pos len) | |
862 | (setq expanded-newtext | |
863 | (concat expanded-newtext | |
864 | (let ((c (aref newtext pos))) | |
865 | (if (= ?\\ c) | |
866 | (cond ((= ?\& (setq c | |
867 | (aref newtext | |
868 | (setq pos (1+ pos))))) | |
869 | (substring string | |
870 | (match-beginning 0) | |
871 | (match-end 0))) | |
872 | ((and (>= c ?1) (<= c ?9)) | |
873 | ;; return empty string if N'th | |
874 | ;; sub-regexp did not match: | |
875 | (let ((n (- c ?0))) | |
876 | (if (match-beginning n) | |
877 | (substring string | |
878 | (match-beginning n) | |
879 | (match-end n)) | |
880 | ""))) | |
881 | (t | |
882 | (char-to-string c))) | |
883 | (char-to-string c))))) | |
884 | (setq pos (1+ pos))) | |
885 | expanded-newtext)) | |
886 | \f | |
887 | ;; The basic function for half a dozen variations on cp/mv/ln/ln -s. | |
888 | (defun dired-create-files (file-creator operation fn-list name-constructor | |
889 | &optional marker-char) | |
890 | ||
891 | ;; Create a new file for each from a list of existing files. The user | |
892 | ;; is queried, dired buffers are updated, and at the end a success or | |
893 | ;; failure message is displayed | |
894 | ||
895 | ;; FILE-CREATOR must accept three args: oldfile newfile ok-if-already-exists | |
896 | ||
897 | ;; It is called for each file and must create newfile, the entry of | |
898 | ;; which will be added. The user will be queried if the file already | |
899 | ;; exists. If oldfile is removed by FILE-CREATOR (i.e, it is a | |
900 | ;; rename), it is FILE-CREATOR's responsibility to update dired | |
901 | ;; buffers. FILE-CREATOR must abort by signalling a file-error if it | |
902 | ;; could not create newfile. The error is caught and logged. | |
903 | ||
904 | ;; OPERATION (a capitalized string, e.g. `Copy') describes the | |
905 | ;; operation performed. It is used for error logging. | |
906 | ||
907 | ;; FN-LIST is the list of files to copy (full absolute pathnames). | |
908 | ||
909 | ;; NAME-CONSTRUCTOR returns a newfile for every oldfile, or nil to | |
910 | ;; skip. If it skips files for other reasons than a direct user | |
911 | ;; query, it is supposed to tell why (using dired-log). | |
912 | ||
913 | ;; Optional MARKER-CHAR is a character with which to mark every | |
914 | ;; newfile's entry, or t to use the current marker character if the | |
915 | ;; oldfile was marked. | |
916 | ||
917 | (let (failures skipped (success-count 0) (total (length fn-list))) | |
918 | (let (to overwrite-query | |
919 | overwrite-backup-query) ; for dired-handle-overwrite | |
920 | (mapcar | |
921 | (function | |
922 | (lambda (from) | |
923 | (setq to (funcall name-constructor from)) | |
924 | (if (equal to from) | |
925 | (progn | |
926 | (setq to nil) | |
927 | (dired-log "Cannot %s to same file: %s\n" | |
928 | (downcase operation) from))) | |
929 | (if (not to) | |
930 | (setq skipped (cons (dired-make-relative from) skipped)) | |
931 | (let* ((overwrite (file-exists-p to)) | |
932 | (overwrite-confirmed ; for dired-handle-overwrite | |
933 | (and overwrite | |
934 | (let ((help-form '(format "\ | |
935 | Type SPC or `y' to overwrite file `%s', | |
936 | DEL or `n' to skip to next, | |
937 | ESC or `q' to not overwrite any of the remaining files, | |
938 | `!' to overwrite all remaining files with no more questions." to))) | |
939 | (dired-query 'overwrite-query | |
940 | "Overwrite `%s'?" to)))) | |
941 | ;; must determine if FROM is marked before file-creator | |
942 | ;; gets a chance to delete it (in case of a move). | |
943 | (actual-marker-char | |
944 | (cond ((integerp marker-char) marker-char) | |
945 | (marker-char (dired-file-marker from)) ; slow | |
946 | (t nil)))) | |
947 | (condition-case err | |
948 | (progn | |
949 | (funcall file-creator from to overwrite-confirmed) | |
950 | (if overwrite | |
951 | ;; If we get here, file-creator hasn't been aborted | |
952 | ;; and the old entry (if any) has to be deleted | |
953 | ;; before adding the new entry. | |
954 | (dired-remove-file to)) | |
955 | (setq success-count (1+ success-count)) | |
956 | (message "%s: %d of %d" operation success-count total) | |
957 | (dired-add-file to actual-marker-char)) | |
958 | (file-error ; FILE-CREATOR aborted | |
959 | (progn | |
960 | (setq failures (cons (dired-make-relative from) failures)) | |
961 | (dired-log "%s `%s' to `%s' failed:\n%s\n" | |
962 | operation from to err)))))))) | |
963 | fn-list)) | |
964 | (cond | |
965 | (failures | |
966 | (dired-log-summary | |
967 | (format "%s failed for %d of %d file%s" | |
968 | operation (length failures) total | |
969 | (dired-plural-s total)) | |
970 | failures)) | |
971 | (skipped | |
972 | (dired-log-summary | |
973 | (format "%s: %d of %d file%s skipped" | |
974 | operation (length skipped) total | |
975 | (dired-plural-s total)) | |
976 | skipped)) | |
977 | (t | |
978 | (message "%s: %s file%s" | |
979 | operation success-count (dired-plural-s success-count))))) | |
980 | (dired-move-to-filename)) | |
981 | \f | |
982 | (defun dired-do-create-files (op-symbol file-creator operation arg | |
983 | &optional marker-char op1 | |
984 | how-to) | |
985 | ;; Create a new file for each marked file. | |
986 | ;; Prompts user for target, which is a directory in which to create | |
987 | ;; the new files. Target may be a plain file if only one marked | |
988 | ;; file exists. | |
989 | ;; OP-SYMBOL is the symbol for the operation. Function `dired-mark-pop-up' | |
990 | ;; will determine wether pop-ups are appropriate for this OP-SYMBOL. | |
991 | ;; FILE-CREATOR and OPERATION as in dired-create-files. | |
992 | ;; ARG as in dired-get-marked-files. | |
993 | ;; Optional arg OP1 is an alternate form for OPERATION if there is | |
994 | ;; only one file. | |
995 | ;; Optional arg MARKER-CHAR as in dired-create-files. | |
996 | ;; Optional arg HOW-TO determines how to treat target: | |
997 | ;; If HOW-TO is not given (or nil), and target is a directory, the | |
998 | ;; file(s) are created inside the target directory. If target | |
999 | ;; is not a directory, there must be exactly one marked file, | |
1000 | ;; else error. | |
1001 | ;; If HOW-TO is t, then target is not modified. There must be | |
1002 | ;; exactly one marked file, else error. | |
1003 | ;; Else HOW-TO is assumed to be a function of one argument, target, | |
1004 | ;; that looks at target and returns a value for the into-dir | |
1005 | ;; variable. The function dired-into-dir-with-symlinks is provided | |
1006 | ;; for the case (common when creating symlinks) that symbolic | |
1007 | ;; links to directories are not to be considered as directories | |
1008 | ;; (as file-directory-p would if HOW-TO had been nil). | |
1009 | (or op1 (setq op1 operation)) | |
1010 | (let* ((fn-list (dired-get-marked-files nil arg)) | |
1011 | (fn-count (length fn-list)) | |
1012 | (target (expand-file-name | |
1013 | (dired-mark-read-file-name | |
1014 | (concat (if (= 1 fn-count) op1 operation) " %s to: ") | |
1015 | (dired-dwim-target-directory) | |
1016 | op-symbol arg (mapcar (function dired-make-relative) fn-list)))) | |
1017 | (into-dir (cond ((null how-to) (file-directory-p target)) | |
1018 | ((eq how-to t) nil) | |
1019 | (t (funcall how-to target))))) | |
1020 | (if (and (> fn-count 1) | |
1021 | (not into-dir)) | |
1022 | (error "Marked %s: target must be a directory: %s" operation target)) | |
1023 | ;; rename-file bombs when moving directories unless we do this: | |
1024 | (or into-dir (setq target (directory-file-name target))) | |
1025 | (dired-create-files | |
1026 | file-creator operation fn-list | |
1027 | (if into-dir ; target is a directory | |
1028 | ;; This function uses fluid vars into-dir and target when called | |
1029 | ;; inside dired-create-files: | |
1030 | (function (lambda (from) | |
1031 | (expand-file-name (file-name-nondirectory from) target))) | |
1032 | (function (lambda (from) target))) | |
1033 | marker-char))) | |
1034 | ||
1035 | ;; Read arguments for a marked-files command that wants a file name, | |
1036 | ;; perhaps popping up the list of marked files. | |
1037 | ;; ARG is the prefix arg and indicates whether the files came from | |
1038 | ;; marks (ARG=nil) or a repeat factor (integerp ARG). | |
1039 | ;; If the current file was used, the list has but one element and ARG | |
1040 | ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)). | |
1041 | ||
1042 | (defun dired-mark-read-file-name (prompt dir op-symbol arg files) | |
1043 | (dired-mark-pop-up | |
1044 | nil op-symbol files | |
1045 | (function read-file-name) | |
1046 | (format prompt (dired-mark-prompt arg files)) dir)) | |
1047 | ||
1048 | (defun dired-dwim-target-directory () | |
1049 | ;; Try to guess which target directory the user may want. | |
1050 | ;; If there is a dired buffer displayed in the next window, use | |
1051 | ;; its current subdir, else use current subdir of this dired buffer. | |
1052 | (let ((this-dir (and (eq major-mode 'dired-mode) | |
1053 | (dired-current-directory)))) | |
1054 | ;; non-dired buffer may want to profit from this function, e.g. vm-uudecode | |
1055 | (if dired-dwim-target | |
1056 | (let* ((other-buf (window-buffer (next-window))) | |
1057 | (other-dir (save-excursion | |
1058 | (set-buffer other-buf) | |
1059 | (and (eq major-mode 'dired-mode) | |
1060 | (dired-current-directory))))) | |
1061 | (or other-dir this-dir)) | |
1062 | this-dir))) | |
1063 | \f | |
1064 | ;;;###autoload | |
1065 | (defun dired-create-directory (directory) | |
1066 | "Create a directory called DIRECTORY." | |
1067 | (interactive | |
1068 | (list (read-file-name "Create directory: " (dired-current-directory)))) | |
1069 | (let ((expanded (directory-file-name (expand-file-name directory)))) | |
1070 | (make-directory expanded) | |
1071 | (dired-add-file expanded) | |
1072 | (dired-move-to-filename))) | |
1073 | ||
1074 | (defun dired-into-dir-with-symlinks (target) | |
1075 | (and (file-directory-p target) | |
1076 | (not (file-symlink-p target)))) | |
1077 | ;; This may not always be what you want, especially if target is your | |
1078 | ;; home directory and it happens to be a symbolic link, as is often the | |
1079 | ;; case with NFS and automounters. Or if you want to make symlinks | |
1080 | ;; into directories that themselves are only symlinks, also quite | |
1081 | ;; common. | |
1082 | ||
1083 | ;; So we don't use this function as value for HOW-TO in | |
1084 | ;; dired-do-symlink, which has the minor disadvantage of | |
1085 | ;; making links *into* a symlinked-dir, when you really wanted to | |
1086 | ;; *overwrite* that symlink. In that (rare, I guess) case, you'll | |
1087 | ;; just have to remove that symlink by hand before making your marked | |
1088 | ;; symlinks. | |
1089 | ||
1090 | ;;;###autoload | |
1091 | (defun dired-do-copy (&optional arg) | |
1092 | "Copy all marked (or next ARG) files, or copy the current file. | |
1093 | This normally preserves the last-modified date when copying. | |
1094 | When operating on just the current file, you specify the new name. | |
1095 | When operating on multiple or marked files, you specify a directory | |
1096 | and new symbolic links are made in that directory | |
1097 | with the same names that the files currently have." | |
1098 | (interactive "P") | |
1099 | (dired-do-create-files 'copy (function dired-copy-file) | |
1100 | (if dired-copy-preserve-time "Copy [-p]" "Copy") | |
1101 | arg dired-keep-marker-copy)) | |
1102 | ||
1103 | ;;;###autoload | |
1104 | (defun dired-do-symlink (&optional arg) | |
1105 | "Make symbolic links to current file or all marked (or next ARG) files. | |
1106 | When operating on just the current file, you specify the new name. | |
1107 | When operating on multiple or marked files, you specify a directory | |
1108 | and new symbolic links are made in that directory | |
1109 | with the same names that the files currently have." | |
1110 | (interactive "P") | |
1111 | (dired-do-create-files 'symlink (function make-symbolic-link) | |
1112 | "Symlink" arg dired-keep-marker-symlink)) | |
1113 | ||
1114 | ;;;###autoload | |
1115 | (defun dired-do-hardlink (&optional arg) | |
1116 | "Add names (hard links) current file or all marked (or next ARG) files. | |
1117 | When operating on just the current file, you specify the new name. | |
1118 | When operating on multiple or marked files, you specify a directory | |
1119 | and new hard links are made in that directory | |
1120 | with the same names that the files currently have." | |
1121 | (interactive "P") | |
1122 | (dired-do-create-files 'hardlink (function add-name-to-file) | |
1123 | "Hardlink" arg dired-keep-marker-hardlink)) | |
1124 | ||
1125 | ;;;###autoload | |
1126 | (defun dired-do-rename (&optional arg) | |
1127 | "Rename current file or all marked (or next ARG) files. | |
1128 | When renaming just the current file, you specify the new name. | |
1129 | When renaming multiple or marked files, you specify a directory." | |
1130 | (interactive "P") | |
1131 | (dired-do-create-files 'move (function dired-rename-file) | |
1132 | "Move" arg dired-keep-marker-rename "Rename")) | |
1133 | ;;;###end dired-cp.el | |
1134 | \f | |
1135 | ;;; 5K | |
1136 | ;;;###begin dired-re.el | |
1137 | (defun dired-do-create-files-regexp | |
1138 | (file-creator operation arg regexp newname &optional whole-path marker-char) | |
1139 | ;; Create a new file for each marked file using regexps. | |
1140 | ;; FILE-CREATOR and OPERATION as in dired-create-files. | |
1141 | ;; ARG as in dired-get-marked-files. | |
1142 | ;; Matches each marked file against REGEXP and constructs the new | |
1143 | ;; filename from NEWNAME (like in function replace-match). | |
1144 | ;; Optional arg WHOLE-PATH means match/replace the whole pathname | |
1145 | ;; instead of only the non-directory part of the file. | |
1146 | ;; Optional arg MARKER-CHAR as in dired-create-files. | |
1147 | (let* ((fn-list (dired-get-marked-files nil arg)) | |
1148 | (fn-count (length fn-list)) | |
1149 | (operation-prompt (concat operation " `%s' to `%s'?")) | |
1150 | (rename-regexp-help-form (format "\ | |
1151 | Type SPC or `y' to %s one match, DEL or `n' to skip to next, | |
1152 | `!' to %s all remaining matches with no more questions." | |
1153 | (downcase operation) | |
1154 | (downcase operation))) | |
1155 | (regexp-name-constructor | |
1156 | ;; Function to construct new filename using REGEXP and NEWNAME: | |
1157 | (if whole-path ; easy (but rare) case | |
1158 | (function | |
1159 | (lambda (from) | |
1160 | (let ((to (dired-string-replace-match regexp from newname)) | |
1161 | ;; must bind help-form directly around call to | |
1162 | ;; dired-query | |
1163 | (help-form rename-regexp-help-form)) | |
1164 | (if to | |
1165 | (and (dired-query 'rename-regexp-query | |
1166 | operation-prompt | |
1167 | from | |
1168 | to) | |
1169 | to) | |
1170 | (dired-log "%s: %s did not match regexp %s\n" | |
1171 | operation from regexp))))) | |
1172 | ;; not whole-path, replace non-directory part only | |
1173 | (function | |
1174 | (lambda (from) | |
1175 | (let* ((new (dired-string-replace-match | |
1176 | regexp (file-name-nondirectory from) newname)) | |
1177 | (to (and new ; nil means there was no match | |
1178 | (expand-file-name new | |
1179 | (file-name-directory from)))) | |
1180 | (help-form rename-regexp-help-form)) | |
1181 | (if to | |
1182 | (and (dired-query 'rename-regexp-query | |
1183 | operation-prompt | |
1184 | (dired-make-relative from) | |
1185 | (dired-make-relative to)) | |
1186 | to) | |
1187 | (dired-log "%s: %s did not match regexp %s\n" | |
1188 | operation (file-name-nondirectory from) regexp))))))) | |
1189 | rename-regexp-query) | |
1190 | (dired-create-files | |
1191 | file-creator operation fn-list regexp-name-constructor marker-char))) | |
1192 | ||
1193 | (defun dired-mark-read-regexp (operation) | |
1194 | ;; Prompt user about performing OPERATION. | |
1195 | ;; Read and return list of: regexp newname arg whole-path. | |
1196 | (let* ((whole-path | |
1197 | (equal 0 (prefix-numeric-value current-prefix-arg))) | |
1198 | (arg | |
1199 | (if whole-path nil current-prefix-arg)) | |
1200 | (regexp | |
1201 | (dired-read-regexp | |
1202 | (concat (if whole-path "Path " "") operation " from (regexp): ") | |
1203 | dired-flagging-regexp)) | |
1204 | (newname | |
1205 | (read-string | |
1206 | (concat (if whole-path "Path " "") operation " " regexp " to: ")))) | |
1207 | (list regexp newname arg whole-path))) | |
1208 | ||
1209 | ;;;###autoload | |
1210 | (defun dired-do-rename-regexp (regexp newname &optional arg whole-path) | |
1211 | "Rename marked files containing REGEXP to NEWNAME. | |
1212 | As each match is found, the user must type a character saying | |
1213 | what to do with it. For directions, type \\[help-command] at that time. | |
1214 | NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'. | |
1215 | REGEXP defaults to the last regexp used. | |
1216 | With a zero prefix arg, renaming by regexp affects the complete | |
1217 | pathname - usually only the non-directory part of file names is used | |
1218 | and changed." | |
1219 | (interactive (dired-mark-read-regexp "Rename")) | |
1220 | (dired-do-create-files-regexp | |
1221 | (function dired-rename-file) | |
1222 | "Rename" arg regexp newname whole-path dired-keep-marker-rename)) | |
1223 | ||
1224 | ;;;###autoload | |
1225 | (defun dired-do-copy-regexp (regexp newname &optional arg whole-path) | |
1226 | "Copy all marked files containing REGEXP to NEWNAME. | |
1227 | See function `dired-rename-regexp' for more info." | |
1228 | (interactive (dired-mark-read-regexp "Copy")) | |
1229 | (dired-do-create-files-regexp | |
1230 | (function dired-copy-file) | |
1231 | (if dired-copy-preserve-time "Copy [-p]" "Copy") | |
1232 | arg regexp newname whole-path dired-keep-marker-copy)) | |
1233 | ||
1234 | ;;;###autoload | |
1235 | (defun dired-do-hardlink-regexp (regexp newname &optional arg whole-path) | |
1236 | "Hardlink all marked files containing REGEXP to NEWNAME. | |
1237 | See function `dired-rename-regexp' for more info." | |
1238 | (interactive (dired-mark-read-regexp "HardLink")) | |
1239 | (dired-do-create-files-regexp | |
1240 | (function add-name-to-file) | |
1241 | "HardLink" arg regexp newname whole-path dired-keep-marker-hardlink)) | |
1242 | ||
1243 | ;;;###autoload | |
1244 | (defun dired-do-symlink-regexp (regexp newname &optional arg whole-path) | |
1245 | "Symlink all marked files containing REGEXP to NEWNAME. | |
1246 | See function `dired-rename-regexp' for more info." | |
1247 | (interactive (dired-mark-read-regexp "SymLink")) | |
1248 | (dired-do-create-files-regexp | |
1249 | (function make-symbolic-link) | |
1250 | "SymLink" arg regexp newname whole-path dired-keep-marker-symlink)) | |
1251 | ||
1252 | (defun dired-create-files-non-directory | |
1253 | (file-creator basename-constructor operation arg) | |
1254 | ;; Perform FILE-CREATOR on the non-directory part of marked files | |
1255 | ;; using function BASENAME-CONSTRUCTOR, with query for each file. | |
1256 | ;; OPERATION like in dired-create-files, ARG as in dired-get-marked-files. | |
1257 | (let (rename-non-directory-query) | |
1258 | (dired-create-files | |
1259 | file-creator | |
1260 | operation | |
1261 | (dired-get-marked-files nil arg) | |
1262 | (function | |
1263 | (lambda (from) | |
1264 | (let ((to (concat (file-name-directory from) | |
1265 | (funcall basename-constructor | |
1266 | (file-name-nondirectory from))))) | |
1267 | (and (let ((help-form (format "\ | |
1268 | Type SPC or `y' to %s one file, DEL or `n' to skip to next, | |
1269 | `!' to %s all remaining matches with no more questions." | |
1270 | (downcase operation) | |
1271 | (downcase operation)))) | |
1272 | (dired-query 'rename-non-directory-query | |
1273 | (concat operation " `%s' to `%s'") | |
1274 | (dired-make-relative from) | |
1275 | (dired-make-relative to))) | |
1276 | to)))) | |
1277 | dired-keep-marker-rename))) | |
1278 | ||
1279 | (defun dired-rename-non-directory (basename-constructor operation arg) | |
1280 | (dired-create-files-non-directory | |
1281 | (function dired-rename-file) | |
1282 | basename-constructor operation arg)) | |
1283 | ||
1284 | ;;;###autoload | |
1285 | (defun dired-upcase (&optional arg) | |
1286 | "Rename all marked (or next ARG) files to upper case." | |
1287 | (interactive "P") | |
1288 | (dired-rename-non-directory (function upcase) "Rename upcase" arg)) | |
1289 | ||
1290 | ;;;###autoload | |
1291 | (defun dired-downcase (&optional arg) | |
1292 | "Rename all marked (or next ARG) files to lower case." | |
1293 | (interactive "P") | |
1294 | (dired-rename-non-directory (function downcase) "Rename downcase" arg)) | |
1295 | ||
1296 | ;;;###end dired-re.el | |
1297 | \f | |
1298 | ;;; 13K | |
1299 | ;;;###begin dired-ins.el | |
1300 | ||
1301 | ;;;###autoload | |
1302 | (defun dired-maybe-insert-subdir (dirname &optional | |
1303 | switches no-error-if-not-dir-p) | |
1304 | "Insert this subdirectory into the same dired buffer. | |
1305 | If it is already present, just move to it (type \\[dired-do-redisplay] to refresh), | |
1306 | else inserts it at its natural place (as `ls -lR' would have done). | |
1307 | With a prefix arg, you may edit the ls switches used for this listing. | |
1308 | You can add `R' to the switches to expand the whole tree starting at | |
1309 | this subdirectory. | |
1310 | This function takes some pains to conform to `ls -lR' output." | |
1311 | (interactive | |
1312 | (list (dired-get-filename) | |
1313 | (if current-prefix-arg | |
1314 | (read-string "Switches for listing: " dired-actual-switches)))) | |
1315 | (let ((opoint (point))) | |
1316 | ;; We don't need a marker for opoint as the subdir is always | |
1317 | ;; inserted *after* opoint. | |
1318 | (setq dirname (file-name-as-directory dirname)) | |
1319 | (or (and (not switches) | |
1320 | (dired-goto-subdir dirname)) | |
1321 | (dired-insert-subdir dirname switches no-error-if-not-dir-p)) | |
1322 | ;; Push mark so that it's easy to find back. Do this after the | |
1323 | ;; insert message so that the user sees the `Mark set' message. | |
1324 | (push-mark opoint))) | |
1325 | ||
1326 | (defun dired-insert-subdir (dirname &optional switches no-error-if-not-dir-p) | |
1327 | "Insert this subdirectory into the same dired buffer. | |
1328 | If it is already present, overwrites previous entry, | |
1329 | else inserts it at its natural place (as `ls -lR' would have done). | |
1330 | With a prefix arg, you may edit the `ls' switches used for this listing. | |
1331 | You can add `R' to the switches to expand the whole tree starting at | |
1332 | this subdirectory. | |
1333 | This function takes some pains to conform to `ls -lR' output." | |
1334 | ;; NO-ERROR-IF-NOT-DIR-P needed for special filesystems like | |
1335 | ;; Prospero where dired-ls does the right thing, but | |
1336 | ;; file-directory-p has not been redefined. | |
1337 | (interactive | |
1338 | (list (dired-get-filename) | |
1339 | (if current-prefix-arg | |
1340 | (read-string "Switches for listing: " dired-actual-switches)))) | |
1341 | (setq dirname (file-name-as-directory (expand-file-name dirname))) | |
1342 | (dired-insert-subdir-validate dirname switches) | |
1343 | (or no-error-if-not-dir-p | |
1344 | (file-directory-p dirname) | |
1345 | (error "Attempt to insert a non-directory: %s" dirname)) | |
1346 | (let ((elt (assoc dirname dired-subdir-alist)) | |
1347 | switches-have-R mark-alist case-fold-search buffer-read-only) | |
1348 | ;; case-fold-search is nil now, so we can test for capital `R': | |
1349 | (if (setq switches-have-R (and switches (string-match "R" switches))) | |
1350 | ;; avoid duplicated subdirs | |
1351 | (setq mark-alist (dired-kill-tree dirname t))) | |
1352 | (if elt | |
1353 | ;; If subdir is already present, remove it and remember its marks | |
1354 | (setq mark-alist (nconc (dired-insert-subdir-del elt) mark-alist)) | |
1355 | (dired-insert-subdir-newpos dirname)) ; else compute new position | |
1356 | (dired-insert-subdir-doupdate | |
1357 | dirname elt (dired-insert-subdir-doinsert dirname switches)) | |
1358 | (if switches-have-R (dired-build-subdir-alist)) | |
1359 | (dired-initial-position dirname) | |
1360 | (save-excursion (dired-mark-remembered mark-alist)))) | |
1361 | ||
1362 | ;; This is a separate function for dired-vms. | |
1363 | (defun dired-insert-subdir-validate (dirname &optional switches) | |
1364 | ;; Check that it is valid to insert DIRNAME with SWITCHES. | |
1365 | ;; Signal an error if invalid (e.g. user typed `i' on `..'). | |
1366 | (or (dired-in-this-tree dirname default-directory) | |
1367 | (error "%s: not in this directory tree" dirname)) | |
1368 | (if switches | |
1369 | (let (case-fold-search) | |
1370 | (mapcar | |
1371 | (function | |
1372 | (lambda (x) | |
1373 | (or (eq (null (string-match x switches)) | |
1374 | (null (string-match x dired-actual-switches))) | |
1375 | (error "Can't have dirs with and without -%s switches together" | |
1376 | x)))) | |
1377 | ;; all switches that make a difference to dired-get-filename: | |
1378 | '("F" "b"))))) | |
1379 | ||
1380 | (defun dired-alist-add (dir new-marker) | |
1381 | ;; Add new DIR at NEW-MARKER. Sort alist. | |
1382 | (dired-alist-add-1 dir new-marker) | |
1383 | (dired-alist-sort)) | |
1384 | ||
1385 | (defun dired-alist-sort () | |
1386 | ;; Keep the alist sorted on buffer position. | |
1387 | (setq dired-subdir-alist | |
1388 | (sort dired-subdir-alist | |
1389 | (function (lambda (elt1 elt2) | |
1390 | (> (dired-get-subdir-min elt1) | |
1391 | (dired-get-subdir-min elt2))))))) | |
1392 | ||
1393 | (defun dired-kill-tree (dirname &optional remember-marks) | |
1394 | ;;"Kill all proper subdirs of DIRNAME, excluding DIRNAME itself. | |
1395 | ;; With optional arg REMEMBER-MARKS, return an alist of marked files." | |
1396 | (interactive "DKill tree below directory: ") | |
1397 | (let ((s-alist dired-subdir-alist) dir m-alist) | |
1398 | (while s-alist | |
1399 | (setq dir (car (car s-alist)) | |
1400 | s-alist (cdr s-alist)) | |
1401 | (if (and (not (string-equal dir dirname)) | |
1402 | (dired-in-this-tree dir dirname) | |
1403 | (dired-goto-subdir dir)) | |
1404 | (setq m-alist (nconc (dired-kill-subdir remember-marks) m-alist)))) | |
1405 | m-alist)) | |
1406 | ||
1407 | (defun dired-insert-subdir-newpos (new-dir) | |
1408 | ;; Find pos for new subdir, according to tree order. | |
1409 | ;;(goto-char (point-max)) | |
1410 | (let ((alist dired-subdir-alist) elt dir pos new-pos) | |
1411 | (while alist | |
1412 | (setq elt (car alist) | |
1413 | alist (cdr alist) | |
1414 | dir (car elt) | |
1415 | pos (dired-get-subdir-min elt)) | |
1416 | (if (dired-tree-lessp dir new-dir) | |
1417 | ;; Insert NEW-DIR after DIR | |
1418 | (setq new-pos (dired-get-subdir-max elt) | |
1419 | alist nil))) | |
1420 | (goto-char new-pos)) | |
1421 | ;; want a separating newline between subdirs | |
1422 | (or (eobp) | |
1423 | (forward-line -1)) | |
1424 | (insert "\n") | |
1425 | (point)) | |
1426 | ||
1427 | (defun dired-insert-subdir-del (element) | |
1428 | ;; Erase an already present subdir (given by ELEMENT) from buffer. | |
1429 | ;; Move to that buffer position. Return a mark-alist. | |
1430 | (let ((begin-marker (dired-get-subdir-min element))) | |
1431 | (goto-char begin-marker) | |
1432 | ;; Are at beginning of subdir (and inside it!). Now determine its end: | |
1433 | (goto-char (dired-subdir-max)) | |
1434 | (or (eobp);; want a separating newline _between_ subdirs: | |
1435 | (forward-char -1)) | |
1436 | (prog1 | |
1437 | (dired-remember-marks begin-marker (point)) | |
1438 | (delete-region begin-marker (point))))) | |
1439 | ||
1440 | (defun dired-insert-subdir-doinsert (dirname switches) | |
1441 | ;; Insert ls output after point and put point on the correct | |
1442 | ;; position for the subdir alist. | |
1443 | ;; Return the boundary of the inserted text (as list of BEG and END). | |
1444 | (let ((begin (point)) end) | |
1445 | (message "Reading directory %s..." dirname) | |
1446 | (let ((dired-actual-switches | |
1447 | (or switches | |
1448 | (dired-replace-in-string "R" "" dired-actual-switches)))) | |
1449 | (if (equal dirname (car (car (reverse dired-subdir-alist)))) | |
1450 | ;; top level directory may contain wildcards: | |
1451 | (dired-readin-insert dired-directory) | |
1452 | (dired-ls dirname dired-actual-switches nil t))) | |
1453 | (message "Reading directory %s...done" dirname) | |
1454 | (setq end (point-marker)) | |
1455 | (indent-rigidly begin end 2) | |
1456 | ;; call dired-insert-headerline afterwards, as under VMS dired-ls | |
1457 | ;; does insert the headerline itself and the insert function just | |
1458 | ;; moves point. | |
1459 | ;; Need a marker for END as this inserts text. | |
1460 | (goto-char begin) | |
1461 | (dired-insert-headerline dirname) | |
1462 | ;; point is now like in dired-build-subdir-alist | |
1463 | (prog1 | |
1464 | (list begin (marker-position end)) | |
1465 | (set-marker end nil)))) | |
1466 | ||
1467 | (defun dired-insert-subdir-doupdate (dirname elt beg-end) | |
1468 | ;; Point is at the correct subdir alist position for ELT, | |
1469 | ;; BEG-END is the subdir-region (as list of begin and end). | |
1470 | (if elt ; subdir was already present | |
1471 | ;; update its position (should actually be unchanged) | |
1472 | (set-marker (dired-get-subdir-min elt) (point-marker)) | |
1473 | (dired-alist-add dirname (point-marker))) | |
1474 | ;; The hook may depend on the subdir-alist containing the just | |
1475 | ;; inserted subdir, so run it after dired-alist-add: | |
1476 | (if dired-after-readin-hook | |
1477 | (save-excursion | |
1478 | (let ((begin (nth 0 beg-end)) | |
1479 | (end (nth 1 beg-end))) | |
1480 | (goto-char begin) | |
1481 | (save-restriction | |
1482 | (narrow-to-region begin end) | |
1483 | ;; hook may add or delete lines, but the subdir boundary | |
1484 | ;; marker floats | |
1485 | (run-hooks 'dired-after-readin-hook)))))) | |
1486 | ||
1487 | (defun dired-tree-lessp (dir1 dir2) | |
1488 | ;; Lexicographic order on pathname components, like `ls -lR': | |
1489 | ;; DIR1 < DIR2 iff DIR1 comes *before* DIR2 in an `ls -lR' listing, | |
1490 | ;; i.e., iff DIR1 is a (grand)parent dir of DIR2, | |
1491 | ;; or DIR1 and DIR2 are in the same parentdir and their last | |
1492 | ;; components are string-lessp. | |
1493 | ;; Thus ("/usr/" "/usr/bin") and ("/usr/a/" "/usr/b/") are tree-lessp. | |
1494 | ;; string-lessp could arguably be replaced by file-newer-than-file-p | |
1495 | ;; if dired-actual-switches contained `t'. | |
1496 | (setq dir1 (file-name-as-directory dir1) | |
1497 | dir2 (file-name-as-directory dir2)) | |
1498 | (let ((components-1 (dired-split "/" dir1)) | |
1499 | (components-2 (dired-split "/" dir2))) | |
1500 | (while (and components-1 | |
1501 | components-2 | |
1502 | (equal (car components-1) (car components-2))) | |
1503 | (setq components-1 (cdr components-1) | |
1504 | components-2 (cdr components-2))) | |
1505 | (let ((c1 (car components-1)) | |
1506 | (c2 (car components-2))) | |
1507 | ||
1508 | (cond ((and c1 c2) | |
1509 | (string-lessp c1 c2)) | |
1510 | ((and (null c1) (null c2)) | |
1511 | nil) ; they are equal, not lessp | |
1512 | ((null c1) ; c2 is a subdir of c1: c1<c2 | |
1513 | t) | |
1514 | ((null c2) ; c1 is a subdir of c2: c1>c2 | |
1515 | nil) | |
1516 | (t (error "This can't happen")))))) | |
1517 | ||
1518 | ;; There should be a builtin split function - inverse to mapconcat. | |
1519 | (defun dired-split (pat str &optional limit) | |
1520 | "Splitting on regexp PAT, turn string STR into a list of substrings. | |
1521 | Optional third arg LIMIT (>= 1) is a limit to the length of the | |
1522 | resulting list. | |
1523 | Thus, if SEP is a regexp that only matches itself, | |
1524 | ||
1525 | (mapconcat 'identity (dired-split SEP STRING) SEP) | |
1526 | ||
1527 | is always equal to STRING." | |
1528 | (let* ((start (string-match pat str)) | |
1529 | (result (list (substring str 0 start))) | |
1530 | (count 1) | |
1531 | (end (if start (match-end 0)))) | |
1532 | (if end ; else nothing left | |
1533 | (while (and (or (not (integerp limit)) | |
1534 | (< count limit)) | |
1535 | (string-match pat str end)) | |
1536 | (setq start (match-beginning 0) | |
1537 | count (1+ count) | |
1538 | result (cons (substring str end start) result) | |
1539 | end (match-end 0) | |
1540 | start end) | |
1541 | )) | |
1542 | (if (and (or (not (integerp limit)) | |
1543 | (< count limit)) | |
1544 | end) ; else nothing left | |
1545 | (setq result | |
1546 | (cons (substring str end) result))) | |
1547 | (nreverse result))) | |
1548 | \f | |
1549 | ;;; moving by subdirectories | |
1550 | ||
1551 | (defun dired-subdir-index (dir) | |
1552 | ;; Return an index into alist for use with nth | |
1553 | ;; for the sake of subdir moving commands. | |
1554 | (let (found (index 0) (alist dired-subdir-alist)) | |
1555 | (while alist | |
1556 | (if (string= dir (car (car alist))) | |
1557 | (setq alist nil found t) | |
1558 | (setq alist (cdr alist) index (1+ index)))) | |
1559 | (if found index nil))) | |
1560 | ||
1561 | ;;;###autoload | |
1562 | (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip) | |
1563 | "Go to next subdirectory, regardless of level." | |
1564 | ;; Use 0 arg to go to this directory's header line. | |
1565 | ;; NO-SKIP prevents moving to end of header line, returning whatever | |
1566 | ;; position was found in dired-subdir-alist. | |
1567 | (interactive "p") | |
1568 | (let ((this-dir (dired-current-directory)) | |
1569 | pos index) | |
1570 | ;; nth with negative arg does not return nil but the first element | |
1571 | (setq index (- (dired-subdir-index this-dir) arg)) | |
1572 | (setq pos (if (>= index 0) | |
1573 | (dired-get-subdir-min (nth index dired-subdir-alist)))) | |
1574 | (if pos | |
1575 | (progn | |
1576 | (goto-char pos) | |
1577 | (or no-skip (skip-chars-forward "^\n\r")) | |
1578 | (point)) | |
1579 | (if no-error-if-not-found | |
1580 | nil ; return nil if not found | |
1581 | (error "%s directory" (if (> arg 0) "Last" "First")))))) | |
1582 | ||
1583 | ;;;###autoload | |
1584 | (defun dired-prev-subdir (arg &optional no-error-if-not-found no-skip) | |
1585 | "Go to previous subdirectory, regardless of level. | |
1586 | When called interactively and not on a subdir line, go to this subdir's line." | |
1587 | ;;(interactive "p") | |
1588 | (interactive | |
1589 | (list (if current-prefix-arg | |
1590 | (prefix-numeric-value current-prefix-arg) | |
1591 | ;; if on subdir start already, don't stay there! | |
1592 | (if (dired-get-subdir) 1 0)))) | |
1593 | (dired-next-subdir (- arg) no-error-if-not-found no-skip)) | |
1594 | ||
1595 | (defun dired-subdir-min () | |
1596 | (save-excursion | |
1597 | (if (not (dired-prev-subdir 0 t t)) | |
1598 | (error "Not in a subdir!") | |
1599 | (point)))) | |
1600 | ||
1601 | ;;;###autoload | |
1602 | (defun dired-goto-subdir (dir) | |
1603 | "Go to end of header line of DIR in this dired buffer. | |
1604 | Return value of point on success, otherwise return nil. | |
1605 | The next char is either \\n, or \\r if DIR is hidden." | |
1606 | (interactive | |
1607 | (prog1 ; let push-mark display its message | |
1608 | (list (expand-file-name | |
1609 | (completing-read "Goto in situ directory: " ; prompt | |
1610 | dired-subdir-alist ; table | |
1611 | nil ; predicate | |
1612 | t ; require-match | |
1613 | (dired-current-directory)))) | |
1614 | (push-mark))) | |
1615 | (setq dir (file-name-as-directory dir)) | |
1616 | (let ((elt (assoc dir dired-subdir-alist))) | |
1617 | (and elt | |
1618 | (goto-char (dired-get-subdir-min elt)) | |
1619 | ;; dired-subdir-hidden-p and dired-add-entry depend on point being | |
1620 | ;; at either \r or \n after this function succeeds. | |
1621 | (progn (skip-chars-forward "^\r\n") | |
1622 | (point))))) | |
1623 | \f | |
1624 | ;;;###autoload | |
1625 | (defun dired-mark-subdir-files () | |
1626 | "Mark all files except `.' and `..'." | |
1627 | (interactive "P") | |
1628 | (let ((p-min (dired-subdir-min))) | |
1629 | (dired-mark-files-in-region p-min (dired-subdir-max)))) | |
1630 | ||
1631 | ;;;###autoload | |
1632 | (defun dired-kill-subdir (&optional remember-marks) | |
1633 | "Remove all lines of current subdirectory. | |
1634 | Lower levels are unaffected." | |
1635 | ;; With optional REMEMBER-MARKS, return a mark-alist. | |
1636 | (interactive) | |
1637 | (let ((beg (dired-subdir-min)) | |
1638 | (end (dired-subdir-max)) | |
1639 | buffer-read-only cur-dir) | |
1640 | (setq cur-dir (dired-current-directory)) | |
1641 | (if (equal cur-dir default-directory) | |
1642 | (error "Attempt to kill top level directory")) | |
1643 | (prog1 | |
1644 | (if remember-marks (dired-remember-marks beg end)) | |
1645 | (delete-region beg end) | |
1646 | (if (eobp) ; don't leave final blank line | |
1647 | (delete-char -1)) | |
1648 | (dired-unsubdir cur-dir)))) | |
1649 | ||
1650 | (defun dired-unsubdir (dir) | |
1651 | ;; Remove DIR from the alist | |
1652 | (setq dired-subdir-alist | |
1653 | (delq (assoc dir dired-subdir-alist) dired-subdir-alist))) | |
1654 | ||
1655 | ;;;###autoload | |
1656 | (defun dired-tree-up (arg) | |
1657 | "Go up ARG levels in the dired tree." | |
1658 | (interactive "p") | |
1659 | (let ((dir (dired-current-directory))) | |
1660 | (while (>= arg 1) | |
1661 | (setq arg (1- arg) | |
1662 | dir (file-name-directory (directory-file-name dir)))) | |
1663 | ;;(setq dir (expand-file-name dir)) | |
1664 | (or (dired-goto-subdir dir) | |
1665 | (error "Cannot go up to %s - not in this tree." dir)))) | |
1666 | ||
1667 | ;;;###autoload | |
1668 | (defun dired-tree-down () | |
1669 | "Go down in the dired tree." | |
1670 | (interactive) | |
1671 | (let ((dir (dired-current-directory)) ; has slash | |
1672 | pos case-fold-search) ; filenames are case sensitive | |
1673 | (let ((rest (reverse dired-subdir-alist)) elt) | |
1674 | (while rest | |
1675 | (setq elt (car rest) | |
1676 | rest (cdr rest)) | |
1677 | (if (dired-in-this-tree (directory-file-name (car elt)) dir) | |
1678 | (setq rest nil | |
1679 | pos (dired-goto-subdir (car elt)))))) | |
1680 | (if pos | |
1681 | (goto-char pos) | |
1682 | (error "At the bottom")))) | |
1683 | \f | |
1684 | ;;; hiding | |
1685 | ||
1686 | (defun dired-unhide-subdir () | |
1687 | (let (buffer-read-only) | |
1688 | (subst-char-in-region (dired-subdir-min) (dired-subdir-max) ?\r ?\n))) | |
1689 | ||
1690 | (defun dired-hide-check () | |
1691 | (or selective-display | |
1692 | (error "selective-display must be t for subdir hiding to work!"))) | |
1693 | ||
1694 | (defun dired-subdir-hidden-p (dir) | |
1695 | (and selective-display | |
1696 | (save-excursion | |
1697 | (dired-goto-subdir dir) | |
1698 | (looking-at "\r")))) | |
1699 | ||
1700 | ;;;###autoload | |
1701 | (defun dired-hide-subdir (arg) | |
1702 | "Hide or unhide the current subdirectory and move to next directory. | |
1703 | Optional prefix arg is a repeat factor. | |
1704 | Use \\[dired-hide-all] to (un)hide all directories." | |
1705 | (interactive "p") | |
1706 | (dired-hide-check) | |
1707 | (while (>= (setq arg (1- arg)) 0) | |
1708 | (let* ((cur-dir (dired-current-directory)) | |
1709 | (hidden-p (dired-subdir-hidden-p cur-dir)) | |
1710 | (elt (assoc cur-dir dired-subdir-alist)) | |
1711 | (end-pos (1- (dired-get-subdir-max elt))) | |
1712 | buffer-read-only) | |
1713 | ;; keep header line visible, hide rest | |
1714 | (goto-char (dired-get-subdir-min elt)) | |
1715 | (skip-chars-forward "^\n\r") | |
1716 | (if hidden-p | |
1717 | (subst-char-in-region (point) end-pos ?\r ?\n) | |
1718 | (subst-char-in-region (point) end-pos ?\n ?\r))) | |
1719 | (dired-next-subdir 1 t))) | |
1720 | ||
1721 | ;;;###autoload | |
1722 | (defun dired-hide-all (arg) | |
1723 | "Hide all subdirectories, leaving only their header lines. | |
1724 | If there is already something hidden, make everything visible again. | |
1725 | Use \\[dired-hide-subdir] to (un)hide a particular subdirectory." | |
1726 | (interactive "P") | |
1727 | (dired-hide-check) | |
1728 | (let (buffer-read-only) | |
1729 | (if (save-excursion | |
1730 | (goto-char (point-min)) | |
1731 | (search-forward "\r" nil t)) | |
1732 | ;; unhide - bombs on \r in filenames | |
1733 | (subst-char-in-region (point-min) (point-max) ?\r ?\n) | |
1734 | ;; hide | |
1735 | (let ((pos (point-max)) ; pos of end of last directory | |
1736 | (alist dired-subdir-alist)) | |
1737 | (while alist ; while there are dirs before pos | |
1738 | (subst-char-in-region (dired-get-subdir-min (car alist)) ; pos of prev dir | |
1739 | (save-excursion | |
1740 | (goto-char pos) ; current dir | |
1741 | ;; we're somewhere on current dir's line | |
1742 | (forward-line -1) | |
1743 | (point)) | |
1744 | ?\n ?\r) | |
1745 | (setq pos (dired-get-subdir-min (car alist))) ; prev dir gets current dir | |
1746 | (setq alist (cdr alist))))))) | |
1747 | ||
1748 | ;;;###end dired-ins.el | |
2f14b48d | 1749 | |
e5167999 | 1750 | ;;; dired-aux.el ends here |