98
|
1 ; -*- Emacs-Lisp -*-
|
|
2 ;; DIRED commands for Emacs.
|
|
3 ;; Copyright (C) 1985, 1986, 1991 Free Software Foundation, Inc.
|
|
4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
5 ;;
|
|
6 ;; File: dired.el
|
|
7 ;; RCS:
|
114
|
8 ;; Dired Version: $Revision: 1.4 $
|
98
|
9 ;; Description: The DIRectory EDitor is for manipulating, and running
|
|
10 ;; commands on files in a directory.
|
|
11 ;; Authors: FSF,
|
|
12 ;; Sebastian Kremer <sk@thp.uni-koeln.de>,
|
|
13 ;; Sandy Rutherford <sandy@ibm550.sissa.it>
|
|
14 ;; Cast of thousands...
|
|
15 ;;
|
|
16 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
17
|
|
18 ;; This program is free software; you can redistribute it and/or modify
|
|
19 ;; it under the terms of the GNU General Public License as published by
|
|
20 ;; the Free Software Foundation; either version 1, or (at your option)
|
|
21 ;; any later version.
|
|
22
|
|
23 ;; This program is distributed in the hope that it will be useful,
|
|
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
26 ;; GNU General Public License for more details.
|
|
27
|
|
28 ;; You should have received a copy of the GNU General Public License
|
|
29 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
30 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
31
|
|
32 ;; Rewritten in 1990/1991 to add tree features, file marking and
|
|
33 ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
|
|
34 ;; 7-1993: Added special features for efs interaction and upgraded to Emacs 19.
|
|
35 ;; Sandy Rutherford <sandy@ibm550.sissa.it>
|
|
36
|
|
37 ;;; Dired Version
|
|
38
|
114
|
39 (defconst dired-version (substring "$Revision: 1.4 $" 11 -2)
|
98
|
40 "The revision number of Tree Dired (as a string).
|
|
41
|
|
42 Don't forget to mention this when reporting bugs to:
|
|
43
|
|
44 efs-bugs@cuckoo.hpl.hp.com")
|
|
45
|
|
46 ;; Global key bindings:
|
|
47 ;; --------------------
|
|
48 ;;
|
|
49 ;; By convention, dired uses the following global key-bindings.
|
|
50 ;; These may or may not already be set up in your local emacs. If not
|
|
51 ;; then you will need to add them to your .emacs file, or the system
|
|
52 ;; default.el file. We don't set them automatically here, as users may
|
|
53 ;; have individual preferences.
|
|
54 ;;
|
|
55 ;; (define-key ctl-x-map "d" 'dired)
|
|
56 ;; (define-key ctl-x-4-map "d" 'dired-other-window)
|
|
57 ;; (define-key ctl-x-map "\C-j" 'dired-jump-back)
|
|
58 ;; (define-key ctl-x-4-map "\C-j" 'dired-jump-back-other-window)
|
|
59 ;;
|
|
60 ;; For V19 emacs only. (Make sure that the ctl-x-5-map exists.)
|
|
61 ;; (define-key ctl-x-5-map "d" 'dired-other-frame)
|
|
62 ;; (define-key Ctl-x-5-map "\C-j" 'dired-jump-back-other-frame)
|
|
63
|
|
64
|
|
65 ;;; Grok the current emacs version
|
|
66 ;;
|
|
67 ;; Hopefully these two variables provide us with enough version sensitivity.
|
|
68
|
|
69 ;; Make sure that we have a frame-width function
|
|
70 (or (fboundp 'frame-width) (fset 'frame-width 'screen-width))
|
|
71
|
|
72 ;;; Requirements and provisions
|
|
73
|
|
74 (provide 'dired)
|
|
75 (require 'backquote) ; For macros.
|
|
76
|
|
77 ;; Compatibility requirements for the file-name-handler-alist.
|
|
78 (let ((lucid-p (string-match "Lucid" emacs-version))
|
|
79 ver subver)
|
|
80 (or (string-match "^\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version)
|
|
81 (error "dired does not work with emacs version %s" emacs-version))
|
|
82 (setq ver (string-to-int (substring emacs-version (match-beginning 1)
|
|
83 (match-end 1)))
|
|
84 subver (string-to-int (substring emacs-version (match-beginning 2)
|
|
85 (match-end 2))))
|
|
86 (cond
|
|
87 ((= ver 18)
|
|
88 (require 'emacs-19)
|
|
89 (require 'fn-handler))
|
|
90 ((and (= ver 19) (if lucid-p (< subver 10) (< subver 23)))
|
|
91 (require 'fn-handler))
|
|
92 ((< ver 18)
|
|
93 (error "dired does not work with emacs version %s" emacs-version))))
|
|
94
|
114
|
95 ;; We duplicate default-dir stuff to avoid its overwrites unless
|
|
96 ;; they are explicitly requested.
|
|
97
|
|
98 (defvar default-directory-function nil
|
|
99 "A function to call to compute the default-directory for the current buffer.
|
|
100 If this is nil, the function default-directory will return the value of the
|
|
101 variable default-directory.
|
|
102 Buffer local.")
|
|
103 (make-variable-buffer-local 'default-directory-function)
|
|
104
|
|
105 (defun default-directory ()
|
|
106 " Returns the default-directory for the current buffer.
|
|
107 Will use the variable default-directory-function if it non-nil."
|
|
108 (if default-directory-function
|
|
109 (funcall default-directory-function)
|
|
110 (if (string-match "Lucid" emacs-version)
|
|
111 (abbreviate-file-name default-directory t)
|
|
112 (abbreviate-file-name default-directory))))
|
98
|
113
|
|
114 ;;;;----------------------------------------------------------------
|
|
115 ;;;; Customizable variables
|
|
116 ;;;;----------------------------------------------------------------
|
|
117 ;;
|
|
118 ;; The funny comments are for autoload.el, to automagically update
|
|
119 ;; loaddefs.
|
|
120
|
|
121 ;;; Variables for compressing files.
|
|
122
|
|
123 ;;;###autoload
|
|
124 (defvar dired-compression-method 'compress
|
|
125 "*Type of compression program to use.
|
|
126 Give as a symbol.
|
|
127 Currently-recognized methods are: gzip pack compact compress.
|
|
128 To change this variable use \\[dired-do-compress] with a zero prefix.")
|
|
129
|
|
130 ;;;###autoload
|
|
131 (defvar dired-compression-method-alist
|
|
132 '((gzip ".gz" ("gzip") ("gzip" "-d") "-f")
|
|
133 ;; Put compress before pack, so that it wins out if we are using
|
|
134 ;; efs to work on a case insensitive OS. The -f flag does
|
|
135 ;; two things in compress. No harm in giving it twice.
|
|
136 (compress ".Z" ("compress" "-f") ("compress" "-d") "-f")
|
|
137 ;; pack support may not work well. pack is too chatty and there is no way
|
|
138 ;; to force overwrites.
|
|
139 (pack ".z" ("pack" "-f") ("unpack"))
|
|
140 (compact ".C" ("compact") ("uncompact")))
|
|
141
|
|
142 "*Association list of compression method descriptions.
|
|
143 Each element of the table should be a list of the form
|
|
144
|
|
145 \(compress-type extension (compress-args) (decompress-args) force-flag\)
|
|
146
|
|
147 where
|
|
148 `compress-type' is a unique symbol in the alist to which
|
|
149 `dired-compression-method' can be set;
|
|
150 `extension' is the file extension (as a string) used by files compressed
|
|
151 by this method;
|
|
152 `compress-args' is a list of the path of the compression program and
|
|
153 flags to pass as separate arguments;
|
|
154 `decompress-args' is a list of the path of the decompression
|
|
155 program and flags to pass as separate arguments.
|
|
156 `force-flag' is the switch to pass to the command to force overwriting
|
|
157 of existing files.
|
|
158
|
|
159 For example:
|
|
160
|
114
|
161 \(setq dired-compression-method-alist
|
98
|
162 \(cons '\(frobnicate \".frob\" \(\"frob\"\) \(\"frob\" \"-d\"\) \"-f\"\)
|
|
163 dired-compression-method-alist\)\)
|
|
164 => \(\(frobnicate \".frob\" \(\"frob\"\) \(\"frob\" \"-d\"\)\)
|
|
165 \(gzip \".gz\" \(\"gzip\"\) \(\"gunzip\"\)\)
|
|
166 ...\)
|
|
167
|
|
168 See also: dired-compression-method <V>")
|
|
169
|
|
170 ;;; Variables for the ls program.
|
|
171
|
|
172 ;;;###autoload
|
|
173 (defvar dired-ls-program "ls"
|
|
174 "*Absolute or relative name of the ls program used by dired.")
|
|
175
|
|
176 ;;;###autoload
|
|
177 (defvar dired-listing-switches "-al"
|
|
178 "*Switches passed to ls for dired. MUST contain the `l' option.
|
|
179 Can contain even `F', `b', `i' and `s'.")
|
|
180
|
|
181 (defvar dired-ls-F-marks-symlinks
|
|
182 (memq system-type '(aix-v3 hpux silicon-graphics-unix))
|
|
183 ;; Both SunOS and Ultrix have system-type berkeley-unix. But
|
|
184 ;; SunOS doesn't mark symlinks, but Ultrix does. Therefore,
|
|
185 ;; can't grok this case.
|
|
186 "*Informs dired about how ls -lF marks symbolic links.
|
|
187 Set this to t if `dired-ls-program' with -lF marks the name of the symbolic
|
|
188 link itself with a trailing @.
|
|
189
|
|
190 For example: If foo is a link pointing to bar, and \"ls -F bar\" gives
|
|
191
|
|
192 ... bar -> foo
|
|
193
|
|
194 set this variable to nil. If it gives
|
|
195
|
|
196 ... bar@ -> foo
|
|
197
|
|
198 set this variable to t.
|
|
199
|
|
200 Dired checks if there is really a @ appended. Thus, if you have a
|
|
201 marking ls program on one host and a non-marking on another host, and
|
|
202 don't care about symbolic links which really end in a @, you can
|
|
203 always set this variable to t.
|
|
204
|
|
205 If you use efs, it will make this variable buffer-local, and control
|
|
206 it according to its assessment of how the remote host marks symbolic
|
|
207 links.")
|
|
208
|
|
209 (defvar dired-show-ls-switches nil
|
|
210 "*If non-nil dired will show the dired ls switches on the modeline.
|
|
211 If nil, it will indicate how the files are sorted by either \"by name\" or
|
|
212 \"by date\". If it is unable to recognize the sorting defined by the switches,
|
|
213 then the switches will be shown explicitly on the modeline, regardless of the
|
|
214 setting of this variable.")
|
|
215
|
|
216 ;;; Variables for other unix utility programs.
|
|
217
|
|
218 ;; For most program names, don't use absolute paths so that dired
|
|
219 ;; uses the user's value of the environment variable PATH. chown is
|
|
220 ;; an exception as it is not always in the PATH.
|
|
221
|
|
222 ;;;###autoload
|
|
223 (defvar dired-chown-program
|
|
224 (if (memq system-type '(hpux dgux usg-unix-v)) "chown" "/etc/chown")
|
114
|
225 "*Name of chown command (usually `chown' or `/etc/chown').")
|
98
|
226
|
|
227 ;;;###autoload
|
|
228 (defvar dired-gnutar-program nil
|
|
229 "*If non-nil, name of the GNU tar executable (e.g. \"tar\" or \"gnutar\").
|
|
230 GNU tar's `z' switch is used for compressed tar files.
|
|
231 If you don't have GNU tar, set this to nil: a pipe using `zcat' is then used.")
|
|
232
|
|
233 ;;;###autoload
|
|
234 (defvar dired-unshar-program nil
|
|
235 "*Set to the name of the unshar program, if you have it.")
|
|
236
|
|
237 ;;; Markers
|
|
238
|
|
239 (defvar dired-keep-marker-rename t
|
|
240 ;; Use t as default so that moved files `take their markers with them'
|
|
241 "*Controls marking of renamed files.
|
|
242 If t, files keep their previous marks when they are renamed.
|
|
243 If a character, renamed files (whether previously marked or not)
|
|
244 are afterward marked with that character.")
|
|
245
|
|
246 (defvar dired-keep-marker-compress t
|
|
247 "*Controls marking of compressed or uncompressed files.
|
|
248 If t, files keep their previous marks when they are compressed.
|
|
249 If a character, compressed or uncompressed files (whether previously
|
|
250 marked or not) are afterward marked with that character.")
|
|
251
|
|
252 (defvar dired-keep-marker-uucode ?U
|
|
253 "*Controls marking of uuencoded or uudecoded files.
|
|
254 If t, files keep their previous marks when they are uuencoded.
|
|
255 If a character, uuencoded or uudecoded files (whether previously
|
|
256 marked or not) are afterward marked with that character.")
|
|
257
|
|
258 (defvar dired-keep-marker-copy ?C
|
|
259 "*Controls marking of copied files.
|
|
260 If t, copied files are marked if and as the corresponding original files were.
|
|
261 If a character, copied files are unconditionally marked with that character.")
|
|
262
|
|
263 (defvar dired-keep-marker-hardlink ?H
|
|
264 "*Controls marking of newly made hard links.
|
|
265 If t, they are marked if and as the files linked to were marked.
|
|
266 If a character, new links are unconditionally marked with that character.")
|
|
267
|
|
268 (defvar dired-keep-marker-symlink ?S
|
|
269 "*Controls marking of newly made symbolic links.
|
|
270 If t, they are marked if and as the files linked to were marked.
|
|
271 If a character, new links are unconditionally marked with that character.")
|
|
272
|
|
273 (defvar dired-keep-marker-kill ?K
|
|
274 "*When killed file lines are redisplayed, they will have this marker.
|
|
275 Setting this to nil means that they will not have any marker.")
|
|
276
|
|
277 (defvar dired-failed-marker-shell ?!
|
|
278 "*If non-nil, a character with which to mark files of failed shell commands.
|
|
279 Applies to the command `dired-do-shell-command'. Files for which the shell
|
|
280 command has a nonzero exit status will be marked with this character")
|
|
281
|
|
282 ;;; Behavioral Variables
|
|
283
|
|
284 ;;;###autoload
|
|
285 (defvar dired-local-variables-file ".dired"
|
|
286 "*If non-nil, filename for local variables for Dired.
|
|
287 If Dired finds a file with that name in the current directory, it will
|
|
288 temporarily insert it into the dired buffer and run `hack-local-variables'.
|
|
289
|
|
290 Type \\[info] and `g' `(emacs)File Variables' `RET' for more info on
|
|
291 local variables.")
|
|
292
|
|
293 ;; Usually defined in files.el. Define here anyway, to be safe.
|
|
294 ;;;###autoload
|
|
295 (defvar dired-kept-versions 2
|
|
296 "*When cleaning directory, number of versions to keep.")
|
|
297
|
|
298 ;;;###autoload
|
|
299 (defvar dired-find-subdir nil
|
|
300 "*Determines whether dired tries to lookup a subdir in existing buffers.
|
|
301 If non-nil, dired does not make a new buffer for a directory if it can be
|
|
302 found (perhaps as subdir) in some existing dired buffer. If there are several
|
|
303 dired buffers for a directory, then the most recently used one is chosen.
|
|
304
|
|
305 Dired avoids switching to the current buffer, so that if you have
|
|
306 a normal and a wildcard buffer for the same directory, C-x d RET will
|
|
307 toggle between those two.")
|
|
308
|
|
309 ;;;###autoload
|
|
310 (defvar dired-use-file-transformers t
|
|
311 "*Determines whether dired uses file transformers.
|
|
312 If non-nil `dired-do-shell-command' will apply file transformers to file names.
|
|
313 See \\[describe-function] for dired-do-shell-command for more information.")
|
|
314
|
|
315 ;;;###autoload
|
|
316 (defvar dired-dwim-target nil
|
|
317 "*If non-nil, dired tries to guess a default target directory.
|
|
318 This means that if there is a dired buffer displayed in the next window,
|
|
319 use its current subdir, instead of the current subdir of this dired buffer.
|
|
320 The target is put in the prompt for file copy, rename, etc.")
|
|
321
|
|
322 ;;;###autoload
|
|
323 (defvar dired-copy-preserve-time nil
|
|
324 "*If non-nil, Dired preserves the last-modified time in a file copy.
|
|
325 \(This works on only some systems.)\\<dired-mode-map>
|
|
326 Use `\\[dired-do-copy]' with a zero prefix argument to toggle its value.")
|
|
327
|
|
328 ;;;###autoload
|
|
329 (defvar dired-no-confirm nil
|
|
330 "*If non-nil, a list of symbols for commands dired should not confirm.
|
|
331 It can be a sublist of
|
|
332
|
|
333 '(byte-compile chgrp chmod chown compress copy delete hardlink load
|
|
334 move print shell symlink uncompress recursive-delete kill-file-buffer
|
|
335 kill-dired-buffer patch create-top-dir revert-subdirs)
|
|
336
|
|
337 The meanings of most of the symbols are obvious. A few exceptions:
|
|
338
|
|
339 'compress applies to compression or decompression by any of the
|
|
340 compression program in `dired-compression-method-alist'.
|
|
341
|
|
342 'kill-dired-buffer applies to offering to kill dired buffers for
|
|
343 directories which have been deleted.
|
|
344
|
|
345 'kill-file-buffer applies to offering to kill buffers visiting files
|
|
346 which have been deleted.
|
|
347
|
|
348 'recursive-delete applies to recursively deleting non-empty
|
|
349 directories, and all of their contents.
|
|
350
|
|
351 'create-top-dir applies to `dired-up-directory' creating a new top level
|
|
352 directory for the dired buffer.
|
|
353
|
|
354 'revert-subdirs applies to re-reading subdirectories which have
|
|
355 been modified on disk.
|
|
356
|
|
357 Note that this list also applies to remote files accessed with efs
|
|
358 or ange-ftp.")
|
|
359
|
|
360 ;;;###autoload
|
|
361 (defvar dired-backup-if-overwrite nil
|
|
362 "*Non-nil if Dired should ask about making backups before overwriting files.
|
|
363 Special value 'always suppresses confirmation.")
|
|
364
|
|
365 ;;;###autoload
|
|
366 (defvar dired-omit-files nil
|
|
367 "*If non-nil un-interesting files will be omitted from this dired buffer.
|
|
368 Use \\[dired-omit-toggle] to see these files. (buffer local)")
|
|
369 (make-variable-buffer-local 'dired-omit-files)
|
|
370
|
|
371 ;;;###autoload
|
|
372 (defvar dired-mail-reader 'rmail
|
|
373 "*Mail reader used by dired for dired-read-mail \(\\[dired-read-mail]\).
|
|
374 The symbols 'rmail and 'vm are the only two allowed values.")
|
|
375
|
|
376 (defvar dired-verify-modtimes t
|
|
377 "*If non-nil dired will revert dired buffers for modified subdirectories.
|
|
378 See also dired-no-confirm <V>.")
|
|
379
|
|
380 ;;; File name regular expressions and extensions.
|
|
381
|
|
382 (defvar dired-trivial-filenames "^\\.\\.?$\\|^#"
|
|
383 "*Regexp of files to skip when finding first file of a directory listing.
|
|
384 A value of nil means move to the subdir line.
|
|
385 A value of t means move to first file.")
|
|
386
|
|
387 (defvar dired-cleanup-alist
|
|
388 (list
|
|
389 '("tex" ".toc" ".log" ".aux" ".dvi")
|
|
390 '("latex" ".toc" ".log" ".aux" ".idx" ".lof" ".lot" ".glo" ".dvi")
|
|
391 '("bibtex" ".blg" ".bbl")
|
|
392 '("texinfo" ".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
|
|
393 ".tp" ".tps" ".vr" ".vrs")
|
|
394 '("patch" ".rej" ".orig")
|
|
395 '("backups" "~")
|
|
396 (cons "completion-ignored-extensions" completion-ignored-extensions))
|
|
397 "*Alist of extensions for temporary files created by various programs.
|
|
398 Used by `dired-cleanup'.")
|
|
399
|
|
400 (defvar dired-omit-extensions
|
|
401 (let ((alist dired-cleanup-alist)
|
|
402 x result)
|
|
403 (while alist
|
|
404 (setq x (cdr (car alist))
|
|
405 alist (cdr alist))
|
|
406 (while x
|
|
407 (or (member (car x) result)
|
|
408 (setq result (cons (car x) result)))
|
|
409 (setq x (cdr x))))
|
|
410 result)
|
|
411 "*List of extensions for file names that will be omitted (buffer-local).
|
|
412 This only has effect when the subdirectory is in omission mode.
|
|
413 To make omission mode the default, set `dired-omit-files' to t.
|
|
414 See also `dired-omit-extensions'.")
|
|
415 (make-variable-buffer-local 'dired-omit-extensions)
|
|
416
|
|
417 (defvar dired-omit-regexps '("^#" "^\\.")
|
|
418 "*File names matching these regexp may be omitted (buffer-local).
|
|
419 This only has effect when the subdirectory is in omission mode.
|
|
420 To make omission mode the default, set `dired-omit-files' to t.
|
|
421 This only has effect when `dired-omit-files-p' is t.
|
|
422 See also `dired-omit-extensions'.")
|
|
423 (make-variable-buffer-local 'dired-omit-files-regexp)
|
|
424
|
|
425 (defvar dired-filename-re-ext "\\..+$" ; start from the first dot. last dot?
|
|
426 "*Defines what is the extension of a file name.
|
|
427 \(match-beginning 0\) for this regexp in the file name without directory will
|
|
428 be taken to be the start of the extension.")
|
|
429
|
|
430 ;;; Hook variables
|
|
431
|
|
432 (defvar dired-load-hook nil
|
|
433 "Run after loading dired.
|
|
434 You can customize key bindings or load extensions with this.")
|
|
435
|
|
436 (defvar dired-grep-load-hook nil
|
|
437 "Run after loading dired-grep.")
|
|
438
|
|
439 (defvar dired-mode-hook nil
|
|
440 "Run at the very end of dired-mode.")
|
|
441
|
|
442 (defvar dired-before-readin-hook nil
|
|
443 "Hook run before a dired buffer is newly read in, created,or reverted.")
|
|
444
|
|
445 (defvar dired-after-readin-hook nil
|
|
446 "Hook run after each listing of a file or directory.
|
|
447 The buffer is narrowed to the new listing.")
|
|
448
|
|
449 (defvar dired-setup-keys-hook nil
|
|
450 "Hook run when dired sets up its keymap.
|
|
451 This happens the first time that `dired-mode' is called, and runs after
|
|
452 `dired-mode-hook'. This hook can be used to make alterations to the
|
|
453 dired keymap.")
|
|
454
|
|
455 ;;; Internal variables
|
|
456 ;;
|
|
457 ;; If you set these, know what you are doing.
|
|
458
|
|
459 ;;; Marker chars.
|
|
460
|
|
461 (defvar dired-marker-char ?* ; the answer is 42
|
|
462 ; life the universe and everything
|
|
463 ;; so that you can write things like
|
|
464 ;; (let ((dired-marker-char ?X))
|
|
465 ;; ;; great code using X markers ...
|
|
466 ;; )
|
|
467 ;; For example, commands operating on two sets of files, A and B.
|
|
468 ;; Or marking files with digits 0-9. This could implicate
|
|
469 ;; concentric sets or an order for the marked files.
|
|
470 ;; The code depends on dynamic scoping on the marker char.
|
|
471 "In dired, character used to mark files for later commands.")
|
|
472 (make-variable-buffer-local 'dired-marker-char)
|
|
473
|
|
474 (defconst dired-default-marker dired-marker-char)
|
|
475 ;; Stores the default value of dired-marker-char when dynamic markers
|
|
476 ;; are being used.
|
|
477
|
|
478 (defvar dired-del-marker ?D
|
|
479 "Character used to flag files for deletion.")
|
|
480
|
|
481 ;; \017=^O for Omit - other packages can chose other control characters.
|
|
482 (defvar dired-omit-marker-char ?\017)
|
|
483 ;; Marker used for omitted files. Shouldn't be used by anything else.
|
|
484
|
|
485 (defvar dired-kill-marker-char ?\C-k)
|
|
486 ;; Marker used by dired-do-kill. Shouldn't be used by anything else.
|
|
487
|
|
488 ;;; State variables
|
|
489
|
|
490 (defvar dired-mode-line-modified "-%s%s%s-"
|
|
491 "*Format string to show the modification status of the buffer.")
|
|
492
|
|
493 (defvar dired-del-flags-number 0)
|
|
494 (make-variable-buffer-local 'dired-del-flags-number)
|
|
495 (defvar dired-marks-number 0)
|
|
496 (make-variable-buffer-local 'dired-marks-number)
|
|
497 (defvar dired-other-marks-number 0)
|
|
498 (make-variable-buffer-local 'dired-other-marks-number)
|
|
499
|
|
500 (defvar dired-marked-files nil
|
|
501 "List of filenames from last `dired-copy-filename-as-kill' call.")
|
|
502
|
|
503 (defvar dired-directory nil
|
|
504 "The directory name or shell wildcard that was used as argument to `ls'.
|
|
505 Local to each dired buffer. May be a list, in which case the car is the
|
|
506 directory name and the cdr is the actual files to list.")
|
|
507 (make-variable-buffer-local 'dired-directory)
|
|
508
|
|
509 (defvar dired-internal-switches nil
|
|
510 "The actual (buffer-local) value of `dired-listing-switches'.
|
|
511 The switches are represented as a list of characters.")
|
|
512 (make-variable-buffer-local 'dired-internal-switches)
|
|
513
|
|
514 (defvar dired-subdir-alist nil
|
|
515 "Association list of subdirectories and their buffer positions.
|
|
516 Each subdirectory has an element: (DIRNAME . STARTMARKER).
|
|
517 The order of elements is the reverse of the order in the buffer.")
|
|
518 (make-variable-buffer-local 'dired-subdir-alist)
|
|
519
|
|
520 (defvar dired-curr-subdir-min 0)
|
|
521 ;; Cache for modeline tracking of the cursor
|
|
522 (make-variable-buffer-local 'dired-curr-subdir-min)
|
|
523
|
|
524 (defvar dired-curr-subdir-max 0)
|
|
525 ;; Cache for modeline tracking of the cursor
|
|
526 (make-variable-buffer-local 'dired-curr-subdir-max)
|
|
527
|
|
528 (defvar dired-subdir-omit nil)
|
|
529 ;; Controls whether the modeline shows Omit.
|
|
530 (make-variable-buffer-local 'dired-subdir-omit)
|
|
531
|
|
532 (defvar dired-in-query nil)
|
|
533 ;; let-bound to t when dired is in the process of querying the user.
|
|
534 ;; This is to keep asynch messaging from clobbering the query prompt.
|
|
535
|
|
536 (defvar dired-overwrite-confirmed nil)
|
|
537 ;; Fluid variable used to remember if a bunch of overwrites have been
|
|
538 ;; confirmed.
|
|
539
|
|
540 (defvar dired-overwrite-backup-query nil)
|
|
541 ;; Fluid var used to remember if backups have been requested for overwrites.
|
|
542
|
|
543 (defvar dired-file-creator-query nil)
|
|
544 ;; Fluid var to remember responses to file-creator queries.
|
|
545
|
|
546 (defvar dired-omit-silent nil)
|
|
547 ;; This is sometimes let-bound to t if messages would be annoying,
|
|
548 ;; e.g., in dired-awrh.el. Binding to 0, only suppresses
|
|
549 ;; \"(Nothing to omit)\" message.
|
|
550
|
|
551 (defvar dired-buffers nil
|
|
552 ;; Enlarged by dired-advertise
|
|
553 ;; Queried by function dired-buffers-for-dir. When this detects a
|
|
554 ;; killed buffer, it is removed from this list.
|
|
555 "Alist of directories and their associated dired buffers.")
|
|
556
|
|
557 (defvar dired-sort-mode nil
|
|
558 "Whether Dired sorts by name, date, etc.
|
|
559 \(buffer-local\)")
|
|
560 ;; This is nil outside dired buffers so it can be used in the modeline
|
|
561 (make-variable-buffer-local 'dired-sort-mode)
|
|
562
|
|
563 (defvar dired-marker-stack nil
|
|
564 "List of previously used dired marker characters.")
|
|
565 (make-variable-buffer-local 'dired-marker-stack)
|
|
566
|
|
567 (defvar dired-marker-stack-pointer 0)
|
|
568 ;; Points to the current marker in the stack
|
|
569 (make-variable-buffer-local 'dired-marker-stack-pointer)
|
|
570
|
|
571 (defvar dired-marker-stack-cursor ?\ ; space
|
|
572 "Character to use as a cursor in the dired marker stack.")
|
|
573
|
|
574 (defconst dired-marker-string ""
|
|
575 "String version of `dired-marker-stack'.")
|
|
576 (make-variable-buffer-local 'dired-marker-string)
|
|
577
|
|
578 (defvar dired-modeline-tracking-cmds nil)
|
|
579 ;; List of commands after which the modeline gets updated.
|
|
580
|
|
581 ;;; Config. variables not usually considered fair game for the user.
|
|
582
|
|
583 (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
|
|
584
|
|
585 (defvar dired-log-buffer "*Dired log*")
|
|
586 ;; Name of buffer used to log dired messages and errors.
|
|
587
|
|
588 ;;; Assoc. lists
|
|
589
|
|
590 ;; For pop ups and user input for file marking
|
|
591 (defvar dired-query-alist
|
|
592 '((?\y . y) (?\040 . y) ; `y' or SPC means accept once
|
|
593 (?n . n) (?\177 . n) ; `n' or DEL skips once
|
|
594 (?! . yes) ; `!' accepts rest
|
|
595 (?q. no) (?\e . no) ; `q' or ESC skips rest
|
|
596 ;; None of these keys quit - use C-g for that.
|
|
597 ))
|
|
598
|
|
599 (defvar dired-sort-type-alist
|
|
600 ;; alist of sort flags, and the sort type, as a symbol.
|
|
601 ;; Don't put ?r in here. It's handled separately.
|
|
602 '((?t . date) (?S . size) (?U . unsort) (?X . ext)))
|
|
603
|
|
604 ;;; Internal regexps for examining ls listings.
|
|
605 ;;
|
|
606 ;; Many of these regexps must be tested at beginning-of-line, but are also
|
|
607 ;; used to search for next matches, so neither omitting "^" nor
|
|
608 ;; replacing "^" by "\n" (to make it slightly faster) will work.
|
|
609
|
|
610 (defvar dired-re-inode-size "[ \t0-9]*")
|
|
611 ;; Regexp for optional initial inode and file size.
|
|
612 ;; Must match output produced by ls' -i and -s flags.
|
|
613
|
|
614 (defvar dired-re-mark "^[^ \n\r]")
|
|
615 ;; Regexp matching a marked line.
|
|
616 ;; Important: the match ends just after the marker.
|
|
617
|
|
618 (defvar dired-re-maybe-mark "^. ")
|
|
619
|
|
620 (defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d"))
|
|
621 ;; Matches directory lines
|
|
622
|
|
623 (defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l"))
|
|
624 ;; Matches symlink lines
|
|
625
|
|
626 (defvar dired-re-exe;; match ls permission string of an executable file
|
|
627 (mapconcat (function
|
|
628 (lambda (x)
|
|
629 (concat dired-re-maybe-mark dired-re-inode-size x)))
|
|
630 '("-[-r][-w][xs][-r][-w].[-r][-w]."
|
|
631 "-[-r][-w].[-r][-w][xs][-r][-w]."
|
|
632 "-[-r][-w].[-r][-w].[-r][-w][xst]")
|
|
633 "\\|"))
|
|
634
|
|
635 (defvar dired-re-dot "^.* \\.\\.?/?$") ; with -F, might end in `/'
|
|
636 ;; . and .. files
|
|
637
|
|
638 (defvar dired-re-month-and-time
|
|
639 (concat
|
|
640 " \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|June?\\|July?\\|Aug\\|Sep\\|Oct\\|Nov\\|"
|
|
641 ; June and July are for HP-UX 9.0
|
|
642 "Dec\\) [ 0-3][0-9]\\("
|
|
643 " [012][0-9]:[0-6][0-9] \\|" ; time
|
|
644 " [12][90][0-9][0-9] \\|" ; year on IRIX, NeXT, SunOS, ULTRIX, Apollo,
|
|
645 ; HP-UX, A/UX
|
|
646 " [12][90][0-9][0-9] \\)" ; year on AIX
|
|
647 ))
|
|
648 ;; This regexp MUST match all the way to first character of the filename.
|
|
649 ;; You can loosen it to taste, but then you might bomb on filenames starting
|
|
650 ;; with a space. This will have to be modified for non-english month names.
|
|
651
|
|
652 (defvar dired-subdir-regexp
|
|
653 "\\([\n\r]\n\\|\\`\\). \\([^\n\r]+\\)\\(:\\)\\(\\.\\.\\.\r\\|[\n\r]\\)")
|
|
654 ;; Regexp matching a maybe hidden subdirectory line in ls -lR output.
|
|
655 ;; Subexpression 2 is the subdirectory proper, no trailing colon.
|
|
656 ;; Subexpression 3 must end right before the \n or \r at the end of
|
|
657 ;; the subdir heading. Matches headings after indentation has been done.
|
|
658
|
|
659 (defvar dired-unhandle-add-files nil)
|
|
660 ;; List of files that the dired handler function need not add to dired buffers.
|
|
661 ;; This is because they have already been added, most likely in
|
|
662 ;; dired-create-files. This is because dired-create-files add files with
|
|
663 ;; special markers.
|
|
664
|
|
665 ;;; history variables
|
|
666
|
|
667 (defvar dired-regexp-history nil
|
|
668 "History list of regular expressions used in Dired commands.")
|
|
669
|
|
670 (defvar dired-chmod-history nil
|
|
671 "History of arguments to chmod in dired.")
|
|
672
|
|
673 (defvar dired-chown-history nil
|
|
674 "History of arguments to chown in dired.")
|
|
675
|
|
676 (defvar dired-chgrp-history nil
|
|
677 "History of arguments to chgrp in dired.")
|
|
678
|
|
679 (defvar dired-cleanup-history nil
|
|
680 "History of arguments to dired-cleanup.")
|
|
681
|
|
682 (defvar dired-goto-file-history nil)
|
|
683 ;; History for dired-goto-file and dired-goto-subdir
|
|
684 (put 'dired-goto-file-history 'cursor-end t) ; for gmhist
|
|
685
|
|
686 (defvar dired-history nil)
|
|
687 ;; Catch-all history variable for dired file ops without
|
|
688 ;; their own history.
|
|
689
|
|
690 (defvar dired-op-history-alist
|
|
691 ;; alist of dired file operations and history symbols
|
|
692 '((chgrp . dired-chgrp-history) (chown . dired-chown-history)
|
|
693 (chmod . dired-chmod-history) ))
|
|
694
|
|
695 ;;; Tell the byte-compiler that we know what we're doing.
|
|
696 ;;; Do we?
|
|
697
|
|
698 (defvar file-name-handler-alist)
|
|
699 (defvar inhibit-file-name-operation)
|
|
700 (defvar inhibit-file-name-handlers)
|
|
701 (defvar efs-dired-host-type)
|
|
702
|
|
703
|
|
704 ;;;;------------------------------------------------------------------
|
|
705 ;;;; Utilities
|
|
706 ;;;;------------------------------------------------------------------
|
|
707
|
|
708 ;;; Macros
|
|
709 ;;
|
|
710 ;; Macros must be defined before they are used - for the byte compiler.
|
|
711
|
|
712 (defmacro dired-get-subdir-min (elt)
|
|
713 ;; Returns the value of the subdir minumum for subdir with entry ELT in
|
|
714 ;; dired-subdir-alist.
|
|
715 (list 'nth 1 elt))
|
|
716
|
|
717 (defmacro dired-save-excursion (&rest body)
|
|
718 ;; Saves excursions of the point (not buffer) in dired buffers.
|
|
719 ;; It tries to be robust against deletion of the region about the point.
|
|
720 ;; Note that this assumes only dired-style deletions.
|
|
721 (let ((temp-bolm (make-symbol "bolm"))
|
|
722 (temp-fnlp (make-symbol "fnlp"))
|
|
723 (temp-offset-bol (make-symbol "offset-bol")))
|
|
724 (` (let (((, temp-bolm) (make-marker))
|
|
725 (, temp-fnlp) (, temp-offset-bol))
|
|
726 (let ((bol (save-excursion (skip-chars-backward "^\n\r") (point))))
|
|
727 (set-marker (, temp-bolm) bol)
|
|
728 (setq (, temp-offset-bol) (- (point) bol)
|
|
729 (, temp-fnlp) (memq (char-after bol) '(?\n\ ?\r))))
|
|
730 (unwind-protect
|
|
731 (progn
|
|
732 (,@ body))
|
|
733 ;; Use the marker to try to find the right line, then move to
|
|
734 ;; the proper column.
|
|
735 (goto-char (, temp-bolm))
|
|
736 (and (not (, temp-fnlp))
|
|
737 (not (eq (following-char) 0)) (memq (following-char) '(?\n ?\r))
|
|
738 ;; The line containing the point got deleted. Note that this
|
|
739 ;; logic only works if we don't delete null lines, but we never
|
|
740 ;; do.
|
|
741 (forward-line 1)) ; don't move into a hidden line.
|
|
742 (skip-chars-forward "^\n\r" (+ (point) (, temp-offset-bol))))))))
|
|
743
|
|
744 (put 'dired-save-excursion 'lisp-indent-hook 0)
|
|
745
|
|
746 (defun dired-substitute-marker (pos old new)
|
|
747 ;; Change marker, re-fontify
|
|
748 (subst-char-in-region pos (1+ pos) old new)
|
|
749 (dired-move-to-filename))
|
|
750
|
|
751 (defmacro dired-mark-if (predicate msg)
|
|
752 ;; Mark all files for which CONDITION evals to non-nil.
|
|
753 ;; CONDITION is evaluated on each line, with point at beginning of line.
|
|
754 ;; MSG is a noun phrase for the type of files being marked.
|
|
755 ;; It should end with a noun that can be pluralized by adding `s'.
|
|
756 ;; Return value is the number of files marked, or nil if none were marked.
|
|
757 (let ((temp-pt (make-symbol "pt"))
|
|
758 (temp-count (make-symbol "count"))
|
|
759 (temp-msg (make-symbol "msg")))
|
|
760 (` (let (((, temp-msg) (, msg))
|
|
761 ((, temp-count) 0)
|
|
762 (, temp-pt) buffer-read-only)
|
|
763 (save-excursion
|
|
764 (if (, temp-msg) (message "Marking %ss..." (, temp-msg)))
|
|
765 (goto-char (point-min))
|
|
766 (while (not (eobp))
|
|
767 (if (and (, predicate)
|
|
768 (not (char-equal (following-char) dired-marker-char)))
|
|
769 (progn
|
|
770 ;; Doing this rather than delete-char, insert
|
|
771 ;; avoids re-computing markers
|
|
772 (setq (, temp-pt) (point))
|
|
773 (dired-substitute-marker
|
|
774 (, temp-pt)
|
|
775 (following-char) dired-marker-char)
|
|
776 (setq (, temp-count) (1+ (, temp-count)))))
|
|
777 (forward-line 1))
|
|
778 (if (, temp-msg)
|
|
779 (message "%s %s%s %s%s."
|
|
780 (, temp-count)
|
|
781 (, temp-msg)
|
|
782 (dired-plural-s (, temp-count))
|
|
783 (if (eq dired-marker-char ?\040) "un" "")
|
|
784 (if (eq dired-marker-char dired-del-marker)
|
|
785 "flagged" "marked"))))
|
|
786 (and (> (, temp-count) 0) (, temp-count))))))
|
|
787
|
|
788 (defmacro dired-map-over-marks (body arg &optional show-progress)
|
|
789 ;; Perform BODY with point somewhere on each marked line
|
|
790 ;; and return a list of BODY's results.
|
|
791 ;; If no marked file could be found, execute BODY on the current line.
|
|
792 ;; If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
|
|
793 ;; files instead of the marked files.
|
|
794 ;; If ARG is t, only apply to marked files. If there are no marked files,
|
|
795 ;; the result is a noop.
|
|
796 ;; If ARG is otherwise non-nil, use current file instead.
|
|
797 ;; If optional third arg SHOW-PROGRESS evaluates to non-nil,
|
|
798 ;; redisplay the dired buffer after each file is processed.
|
|
799 ;; No guarantee is made about the position on the marked line.
|
|
800 ;; BODY must ensure this itself if it depends on this.
|
|
801 ;; Search starts at the beginning of the buffer, thus the car of the list
|
|
802 ;; corresponds to the line nearest to the buffer's bottom. This
|
|
803 ;; is also true for (positive and negative) integer values of ARG.
|
|
804 ;; To avoid code explosion, BODY should not be too long as it is
|
|
805 ;; expanded four times.
|
|
806 ;;
|
|
807 ;; Warning: BODY must not add new lines before point - this may cause an
|
|
808 ;; endless loop.
|
|
809 ;; This warning should not apply any longer, sk 2-Sep-1991 14:10.
|
|
810 (let ((temp-found (make-symbol "found"))
|
|
811 (temp-results (make-symbol "results"))
|
|
812 (temp-regexp (make-symbol "regexp"))
|
|
813 (temp-curr-pt (make-symbol "curr-pt"))
|
|
814 (temp-next-position (make-symbol "next-position")))
|
|
815 (` (let (buffer-read-only case-fold-search (, temp-found) (, temp-results))
|
|
816 (dired-save-excursion
|
|
817 (if (and (, arg) (not (eq (, arg) t)))
|
|
818 (if (integerp (, arg))
|
|
819 (and (not (zerop (, arg)))
|
|
820 (progn;; no save-excursion, want to move point.
|
|
821 (dired-repeat-over-lines
|
|
822 arg
|
|
823 (function (lambda ()
|
|
824 (if (, show-progress) (sit-for 0))
|
|
825 (setq (, temp-results)
|
|
826 (cons (, body)
|
|
827 (, temp-results))))))
|
|
828 (if (< (, arg) 0)
|
|
829 (nreverse (, temp-results))
|
|
830 (, temp-results))))
|
|
831 ;; non-nil, non-integer ARG means use current file:
|
|
832 (list (, body)))
|
|
833 (let (((, temp-regexp)
|
|
834 (concat "^" (regexp-quote (char-to-string
|
|
835 dired-marker-char))))
|
|
836 (, temp-curr-pt) (, temp-next-position))
|
|
837 (save-excursion
|
|
838 (goto-char (point-min))
|
|
839 ;; remember position of next marked file before BODY
|
|
840 ;; can insert lines before the just found file,
|
|
841 ;; confusing us by finding the same marked file again
|
|
842 ;; and again and...
|
|
843 (setq (, temp-next-position)
|
|
844 (and (re-search-forward (, temp-regexp) nil t)
|
|
845 (point-marker))
|
|
846 (, temp-found) (not (null (, temp-next-position))))
|
|
847 (while (, temp-next-position)
|
|
848 (setq (, temp-curr-pt) (goto-char (, temp-next-position))
|
|
849 ;; need to get next position BEFORE body
|
|
850 (, temp-next-position)
|
|
851 (and (re-search-forward (, temp-regexp) nil t)
|
|
852 (point-marker)))
|
|
853 (goto-char (, temp-curr-pt))
|
|
854 (if (, show-progress) (sit-for 0))
|
|
855 (setq (, temp-results) (cons (, body) (, temp-results)))))
|
|
856 (if (, temp-found)
|
|
857 (, temp-results)
|
|
858 ;; Do current file, unless arg is t
|
|
859 (and (not (eq (, arg) t))
|
|
860 (list (, body)))))))))))
|
|
861
|
|
862 ;;; General utility functions
|
|
863
|
|
864 (defun dired-buffer-more-recently-used-p (buffer1 buffer2)
|
|
865 "Return t if BUFFER1 is more recently used than BUFFER2."
|
|
866 (if (equal buffer1 buffer2)
|
|
867 nil
|
|
868 (let ((more-recent nil)
|
|
869 (list (buffer-list)))
|
|
870 (while (and list
|
|
871 (not (setq more-recent (equal buffer1 (car list))))
|
|
872 (not (equal buffer2 (car list))))
|
|
873 (setq list (cdr list)))
|
|
874 more-recent)))
|
|
875
|
|
876 (defun dired-file-modtime (file)
|
|
877 ;; Return the modtime of FILE, which is assumed to be already expanded
|
|
878 ;; by expand-file-name.
|
|
879 (let ((handler (find-file-name-handler file 'dired-file-modtime)))
|
|
880 (if handler
|
|
881 (funcall handler 'dired-file-modtime file)
|
|
882 (nth 5 (file-attributes file)))))
|
|
883
|
|
884 (defun dired-set-file-modtime (file alist)
|
|
885 ;; Set the modtime for FILE in the subdir alist ALIST.
|
|
886 (let ((handler (find-file-name-handler file 'dired-set-file-modtime)))
|
|
887 (if handler
|
|
888 (funcall handler 'dired-set-file-modtime file alist)
|
|
889 (let ((elt (assoc file alist)))
|
|
890 (if elt
|
|
891 (setcar (nthcdr 4 elt) (nth 5 (file-attributes file))))))))
|
|
892
|
|
893 (defun dired-map-over-marks-check (fun arg op-symbol operation
|
|
894 &optional show-progress no-confirm)
|
|
895 ;; Map FUN over marked files (with second ARG like in dired-map-over-marks)
|
|
896 ;; and display failures.
|
|
897
|
|
898 ;; FUN takes zero args. It returns non-nil (the offending object, e.g.
|
|
899 ;; the short form of the filename) for a failure and probably logs a
|
|
900 ;; detailed error explanation using function `dired-log'.
|
|
901
|
|
902 ;; OP-SYMBOL is s symbol representing the operation.
|
|
903 ;; eg. 'compress
|
|
904
|
|
905 ;; OPERATION is a string describing the operation performed (e.g.
|
|
906 ;; "Compress"). It is used with `dired-mark-pop-up' to prompt the user
|
|
907 ;; (e.g. with `Compress * [2 files]? ') and to display errors (e.g.
|
|
908 ;; `Failed to compress 1 of 2 files - type y to see why ("foo")')
|
|
909
|
|
910 ;; SHOW-PROGRESS if non-nil means redisplay dired after each file.
|
|
911
|
|
912 (if (or no-confirm (dired-mark-confirm op-symbol operation arg))
|
|
913 (let* ((total-list;; all of FUN's return values
|
|
914 (dired-map-over-marks (funcall fun) arg show-progress))
|
|
915 (total (length total-list))
|
|
916 (failures (delq nil total-list))
|
|
917 (count (length failures)))
|
|
918 (if (not failures)
|
|
919 (message "%s: %d file%s." operation total (dired-plural-s total))
|
|
920 (message "Failed to %s %d of %d file%s - type y to see why %s"
|
|
921 operation count total (dired-plural-s total)
|
|
922 ;; this gives a short list of failed files in parens
|
|
923 ;; which may be sufficient for the user even
|
|
924 ;; without typing `W' for the process' diagnostics
|
|
925 failures)
|
|
926 ;; end this bunch of errors:
|
|
927 (dired-log-summary
|
|
928 (buffer-name (current-buffer))
|
|
929 (format
|
|
930 "Failed to %s %d of %d file%s"
|
|
931 operation count total (dired-plural-s total))
|
|
932 failures)))))
|
|
933
|
|
934 (defun dired-make-switches-string (list)
|
|
935 ;; Converts a list of cracters to a string suitable for passing to ls.
|
|
936 (concat "-" (mapconcat 'char-to-string list "")))
|
|
937
|
|
938 (defun dired-make-switches-list (string)
|
|
939 ;; Converts a string of ls switches to a list of characters.
|
|
940 (delq ?- (mapcar 'identity string)))
|
|
941
|
|
942 ;; Cloning replace-match to work on strings instead of in buffer:
|
|
943 ;; The FIXEDCASE parameter of replace-match is not implemented.
|
|
944 (defun dired-string-replace-match (regexp string newtext
|
|
945 &optional literal global)
|
|
946 ;; Replace first match of REGEXP in STRING with NEWTEXT.
|
|
947 ;; If it does not match, nil is returned instead of the new string.
|
|
948 ;; Optional arg LITERAL means to take NEWTEXT literally.
|
|
949 ;; Optional arg GLOBAL means to replace all matches.
|
|
950 (if global
|
|
951 (let ((result "") (start 0) mb me)
|
|
952 (while (string-match regexp string start)
|
|
953 (setq mb (match-beginning 0)
|
|
954 me (match-end 0)
|
|
955 result (concat result
|
|
956 (substring string start mb)
|
|
957 (if literal
|
|
958 newtext
|
|
959 (dired-expand-newtext string newtext)))
|
|
960 start me))
|
|
961 (if mb ; matched at least once
|
|
962 (concat result (substring string start))
|
|
963 nil))
|
|
964 ;; not GLOBAL
|
|
965 (if (not (string-match regexp string 0))
|
|
966 nil
|
|
967 (concat (substring string 0 (match-beginning 0))
|
|
968 (if literal newtext (dired-expand-newtext string newtext))
|
|
969 (substring string (match-end 0))))))
|
|
970
|
|
971 (defun dired-expand-newtext (string newtext)
|
|
972 ;; Expand \& and \1..\9 (referring to STRING) in NEWTEXT, using match data.
|
|
973 ;; Note that in Emacs 18 match data are clipped to current buffer
|
|
974 ;; size...so the buffer should better not be smaller than STRING.
|
|
975 (let ((pos 0)
|
|
976 (len (length newtext))
|
|
977 (expanded-newtext ""))
|
|
978 (while (< pos len)
|
|
979 (setq expanded-newtext
|
|
980 (concat expanded-newtext
|
|
981 (let ((c (aref newtext pos)))
|
|
982 (if (= ?\\ c)
|
|
983 (cond ((= ?\& (setq c
|
|
984 (aref newtext
|
|
985 (setq pos (1+ pos)))))
|
|
986 (substring string
|
|
987 (match-beginning 0)
|
|
988 (match-end 0)))
|
|
989 ((and (>= c ?1) (<= c ?9))
|
|
990 ;; return empty string if N'th
|
|
991 ;; sub-regexp did not match:
|
|
992 (let ((n (- c ?0)))
|
|
993 (if (match-beginning n)
|
|
994 (substring string
|
|
995 (match-beginning n)
|
|
996 (match-end n))
|
|
997 "")))
|
|
998 (t
|
|
999 (char-to-string c)))
|
|
1000 (char-to-string c)))))
|
|
1001 (setq pos (1+ pos)))
|
|
1002 expanded-newtext))
|
|
1003
|
|
1004 (defun dired-in-this-tree (file dir)
|
|
1005 ;;Is FILE part of the directory tree starting at DIR?
|
|
1006 (let ((len (length dir)))
|
|
1007 (and (>= (length file) len)
|
|
1008 (string-equal (substring file 0 len) dir))))
|
|
1009
|
|
1010 (defun dired-tree-lessp (dir1 dir2)
|
|
1011 ;; Lexicographic order on pathname components, like `ls -lR':
|
|
1012 ;; DIR1 < DIR2 iff DIR1 comes *before* DIR2 in an `ls -lR' listing,
|
|
1013 ;; i.e., iff DIR1 is a (grand)parent dir of DIR2,
|
|
1014 ;; or DIR1 and DIR2 are in the same parentdir and their last
|
|
1015 ;; components are string-lessp.
|
|
1016 ;; Thus ("/usr/" "/usr/bin") and ("/usr/a/" "/usr/b/") are tree-lessp.
|
|
1017 ;; string-lessp could arguably be replaced by file-newer-than-file-p
|
|
1018 ;; if dired-internal-switches contained `t'.
|
|
1019 (let ((dir1 (file-name-as-directory dir1))
|
|
1020 (dir2 (file-name-as-directory dir2))
|
|
1021 (start1 1)
|
|
1022 (start2 1)
|
|
1023 comp1 comp2 end1 end2)
|
|
1024 (while (progn
|
|
1025 (setq end1 (string-match "/" dir1 start1)
|
|
1026 comp1 (substring dir1 start1 end1)
|
|
1027 end2 (string-match "/" dir2 start2)
|
|
1028 comp2 (substring dir2 start2 end2))
|
|
1029 (and end1 end2 (string-equal comp1 comp2)))
|
|
1030 (setq start1 (1+ end1)
|
|
1031 start2 (1+ end2)))
|
|
1032 (if (eq (null end1) (null end2))
|
|
1033 (string-lessp comp1 comp2)
|
|
1034 (null end1))))
|
|
1035
|
|
1036 ;; So that we can support case-insensitive systems.
|
|
1037 (fset 'dired-file-name-lessp 'string-lessp)
|
|
1038
|
|
1039
|
|
1040 ;;;; ------------------------------------------------------------------
|
|
1041 ;;;; Initializing Dired
|
|
1042 ;;;; ------------------------------------------------------------------
|
|
1043
|
|
1044 ;;; Set the minor mode alist
|
|
1045
|
|
1046 (or (equal (assq 'dired-sort-mode minor-mode-alist)
|
|
1047 '(dired-sort-mode dired-sort-mode))
|
|
1048 ;; Test whether this has already been done in case dired is reloaded
|
|
1049 ;; There may be several elements with dired-sort-mode as car.
|
|
1050 (setq minor-mode-alist
|
|
1051 ;; cons " Omit" in first, so that it doesn't
|
|
1052 ;; get stuck between the directory and sort mode on the
|
|
1053 ;; mode line.
|
|
1054 (cons '(dired-sort-mode dired-sort-mode)
|
|
1055 (cons '(dired-subdir-omit " Omit")
|
|
1056 (cons '(dired-marker-stack dired-marker-string)
|
|
1057 minor-mode-alist)))))
|
|
1058
|
|
1059 ;;; Keymaps
|
|
1060
|
|
1061 (defvar dired-mode-map nil
|
|
1062 "Local keymap for dired-mode buffers.")
|
|
1063 (defvar dired-regexp-map nil
|
|
1064 "Dired keymap for commands that use regular expressions.")
|
|
1065 (defvar dired-diff-map nil
|
|
1066 "Dired keymap for diff and related commands.")
|
|
1067 (defvar dired-subdir-map nil
|
|
1068 "Dired keymap for commands that act on subdirs, or the files within them.")
|
|
1069
|
|
1070 (defvar dired-keymap-grokked nil
|
|
1071 "Set to t after dired has grokked the global keymap.")
|
|
1072
|
|
1073 (defun dired-key-description (cmd &rest prefixes)
|
|
1074 ;; Return a key description string for a menu. If prefixes are given,
|
|
1075 ;; they should be either strings, integers, or 'universal-argument.
|
|
1076 (let ((key (where-is-internal cmd dired-mode-map t)))
|
|
1077 (if key
|
|
1078 (key-description
|
|
1079 (apply 'vconcat
|
|
1080 (append
|
|
1081 (mapcar
|
|
1082 (function
|
|
1083 (lambda (x)
|
|
1084 (cond ((eq x 'universal-argument)
|
|
1085 (where-is-internal 'universal-argument
|
|
1086 dired-mode-map t))
|
|
1087 ((integerp x) (int-to-string x))
|
|
1088 (t x))))
|
|
1089 prefixes)
|
|
1090 (list key))))
|
|
1091 "")))
|
|
1092
|
|
1093 (defun dired-grok-keys (to-command from-command)
|
|
1094 ;; Assigns to TO-COMMAND the keys for the global binding of FROM-COMMAND.
|
|
1095 ;; Does not clobber anything in the local keymap. In emacs 19 should
|
|
1096 ;; use substitute-key-definition, but I believe that this will
|
|
1097 ;; clobber things in the local map.
|
|
1098 (let ((keys (where-is-internal from-command)))
|
|
1099 (while keys
|
|
1100 (condition-case nil
|
|
1101 (if (eq (global-key-binding (car keys)) (key-binding (car keys)))
|
|
1102 (local-set-key (car keys) to-command))
|
|
1103 (error nil))
|
|
1104 (setq keys (cdr keys)))))
|
|
1105
|
|
1106 (defun dired-grok-keymap ()
|
|
1107 ;; Initialize the dired keymaps.
|
|
1108 ;; This is actually done the first time that dired-mode runs.
|
|
1109 ;; We do it this late, to be sure that the user's global-keymap has
|
|
1110 ;; stabilized.
|
|
1111 (if dired-keymap-grokked
|
|
1112 () ; we've done it
|
|
1113 ;; Watch out for dired being invoked from the command line.
|
|
1114 ;; This is a bit kludgy, but so is the emacs startup sequence IMHO.
|
|
1115 (if (and term-setup-hook (boundp 'command-line-args-left))
|
|
1116 (progn
|
|
1117 (if (string-equal "18." (substring emacs-version 0 3))
|
|
1118 (funcall term-setup-hook)
|
|
1119 (run-hooks 'term-setup-hook))
|
|
1120 (setq term-setup-hook nil)))
|
|
1121 (setq dired-keymap-grokked t)
|
|
1122 (run-hooks 'dired-setup-keys-hook)
|
|
1123 (dired-grok-keys 'dired-next-line 'next-line)
|
|
1124 (dired-grok-keys 'dired-previous-line 'previous-line)
|
|
1125 (dired-grok-keys 'dired-undo 'undo)
|
|
1126 (dired-grok-keys 'dired-undo 'advertised-undo)
|
|
1127 (dired-grok-keys 'dired-scroll-up 'scroll-up)
|
|
1128 (dired-grok-keys 'dired-scroll-down 'scroll-down)
|
|
1129 (dired-grok-keys 'dired-beginning-of-buffer 'beginning-of-buffer)
|
|
1130 (dired-grok-keys 'dired-end-of-buffer 'end-of-buffer)
|
|
1131 (dired-grok-keys 'dired-next-subdir 'forward-paragraph)
|
|
1132 (dired-grok-keys 'dired-prev-subdir 'backward-paragraph)))
|
|
1133
|
|
1134 ;; The regexp-map is used for commands using regexp's.
|
|
1135 (if dired-regexp-map
|
|
1136 ()
|
|
1137 (setq dired-regexp-map (make-sparse-keymap))
|
|
1138 (define-key dired-regexp-map "C" 'dired-do-copy-regexp)
|
|
1139 ;; Not really a regexp, but does transform file names.
|
|
1140 (define-key dired-regexp-map "D" 'dired-downcase)
|
|
1141 (define-key dired-regexp-map "H" 'dired-do-hardlink-regexp)
|
|
1142 (define-key dired-regexp-map "R" 'dired-do-rename-regexp)
|
|
1143 (define-key dired-regexp-map "S" 'dired-do-symlink-regexp)
|
|
1144 (define-key dired-regexp-map "U" 'dired-upcase)
|
|
1145 (define-key dired-regexp-map "Y" 'dired-do-relsymlink-regexp)
|
|
1146 (define-key dired-regexp-map "c" 'dired-cleanup)
|
|
1147 (define-key dired-regexp-map "d" 'dired-flag-files-regexp)
|
|
1148 (define-key dired-regexp-map "e" 'dired-mark-extension)
|
|
1149 (define-key dired-regexp-map "m" 'dired-mark-files-regexp)
|
|
1150 (define-key dired-regexp-map "o" 'dired-add-omit-regexp)
|
|
1151 (define-key dired-regexp-map "x" 'dired-flag-extension)) ; a string, rather
|
|
1152 ; than a regexp.
|
|
1153
|
|
1154 (if dired-diff-map
|
|
1155 ()
|
|
1156 (setq dired-diff-map (make-sparse-keymap))
|
|
1157 (define-key dired-diff-map "d" 'dired-diff)
|
|
1158 (define-key dired-diff-map "b" 'dired-backup-diff)
|
|
1159 (define-key dired-diff-map "m" 'dired-emerge)
|
|
1160 (define-key dired-diff-map "a" 'dired-emerge-with-ancestor)
|
|
1161 (define-key dired-diff-map "e" 'dired-ediff)
|
|
1162 (define-key dired-diff-map "p" 'dired-epatch))
|
|
1163
|
|
1164 (if dired-subdir-map
|
|
1165 ()
|
|
1166 (setq dired-subdir-map (make-sparse-keymap))
|
|
1167 (define-key dired-subdir-map "n" 'dired-redisplay-subdir)
|
|
1168 (define-key dired-subdir-map "m" 'dired-mark-subdir-files)
|
|
1169 (define-key dired-subdir-map "d" 'dired-flag-subdir-files)
|
|
1170 (define-key dired-subdir-map "z" 'dired-compress-subdir-files))
|
|
1171
|
|
1172 (fset 'dired-regexp-prefix dired-regexp-map)
|
|
1173 (fset 'dired-diff-prefix dired-diff-map)
|
|
1174 (fset 'dired-subdir-prefix dired-subdir-map)
|
|
1175 (fset 'efs-dired-prefix (function (lambda ()
|
|
1176 (interactive)
|
|
1177 (error "efs-dired not loaded yet"))))
|
|
1178
|
|
1179 ;; the main map
|
|
1180 (if dired-mode-map
|
|
1181 nil
|
|
1182 ;; Force `f' rather than `e' in the mode doc:
|
|
1183 (fset 'dired-advertised-find-file 'dired-find-file)
|
|
1184 (fset 'dired-advertised-next-subdir 'dired-next-subdir)
|
|
1185 (fset 'dired-advertised-prev-subdir 'dired-prev-subdir)
|
|
1186 (setq dired-mode-map (make-keymap))
|
|
1187 (suppress-keymap dired-mode-map)
|
|
1188 ;; Commands to mark certain categories of files
|
|
1189 (define-key dired-mode-map "~" 'dired-flag-backup-files)
|
|
1190 (define-key dired-mode-map "#" 'dired-flag-auto-save-files)
|
|
1191 (define-key dired-mode-map "*" 'dired-mark-executables)
|
|
1192 (define-key dired-mode-map "." 'dired-clean-directory)
|
|
1193 (define-key dired-mode-map "/" 'dired-mark-directories)
|
|
1194 (define-key dired-mode-map "@" 'dired-mark-symlinks)
|
|
1195 (define-key dired-mode-map "," 'dired-mark-rcs-files)
|
|
1196 (define-key dired-mode-map "\M-(" 'dired-mark-sexp)
|
|
1197 (define-key dired-mode-map "\M-d" 'dired-mark-files-from-other-dired-buffer)
|
|
1198 (define-key dired-mode-map "\M-c" 'dired-mark-files-compilation-buffer)
|
|
1199 ;; Upper case keys (except ! and &) for operating on the marked files
|
|
1200 (define-key dired-mode-map "A" 'dired-do-tags-search)
|
|
1201 (define-key dired-mode-map "B" 'dired-do-byte-compile)
|
|
1202 (define-key dired-mode-map "C" 'dired-do-copy)
|
|
1203 (define-key dired-mode-map "E" 'dired-do-grep)
|
|
1204 (define-key dired-mode-map "F" 'dired-do-find-file)
|
|
1205 (define-key dired-mode-map "G" 'dired-do-chgrp)
|
|
1206 (define-key dired-mode-map "H" 'dired-do-hardlink)
|
|
1207 (define-key dired-mode-map "I" 'dired-do-insert-subdir)
|
|
1208 (define-key dired-mode-map "K" 'dired-do-kill-file-lines)
|
|
1209 (define-key dired-mode-map "L" 'dired-do-load)
|
|
1210 (define-key dired-mode-map "M" 'dired-do-chmod)
|
|
1211 (define-key dired-mode-map "N" 'dired-do-redisplay)
|
|
1212 (define-key dired-mode-map "O" 'dired-do-chown)
|
|
1213 (define-key dired-mode-map "P" 'dired-do-print)
|
|
1214 (define-key dired-mode-map "Q" 'dired-do-tags-query-replace)
|
|
1215 (define-key dired-mode-map "R" 'dired-do-rename)
|
|
1216 (define-key dired-mode-map "S" 'dired-do-symlink)
|
|
1217 (define-key dired-mode-map "T" 'dired-do-total-size)
|
|
1218 (define-key dired-mode-map "U" 'dired-do-uucode)
|
|
1219 (define-key dired-mode-map "W" 'dired-copy-filenames-as-kill)
|
|
1220 (define-key dired-mode-map "X" 'dired-do-delete)
|
|
1221 (define-key dired-mode-map "Y" 'dired-do-relsymlink)
|
|
1222 (define-key dired-mode-map "Z" 'dired-do-compress)
|
|
1223 (define-key dired-mode-map "!" 'dired-do-shell-command)
|
|
1224 (define-key dired-mode-map "&" 'dired-do-background-shell-command)
|
|
1225 ;; Make all regexp commands share a `%' prefix:
|
|
1226 (define-key dired-mode-map "%" 'dired-regexp-prefix)
|
|
1227 ;; Lower keys for commands not operating on all the marked files
|
|
1228 (define-key dired-mode-map "a" 'dired-apropos)
|
|
1229 (define-key dired-mode-map "c" 'dired-change-marks)
|
|
1230 (define-key dired-mode-map "d" 'dired-flag-file-deletion)
|
|
1231 (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion-backup)
|
|
1232 (define-key dired-mode-map "e" 'dired-find-file)
|
|
1233 (define-key dired-mode-map "f" 'dired-advertised-find-file)
|
|
1234 (define-key dired-mode-map "g" 'revert-buffer)
|
|
1235 (define-key dired-mode-map "h" 'dired-describe-mode)
|
|
1236 (define-key dired-mode-map "i" 'dired-maybe-insert-subdir)
|
|
1237 (define-key dired-mode-map "k" 'dired-kill-subdir)
|
|
1238 (define-key dired-mode-map "m" 'dired-mark)
|
|
1239 (define-key dired-mode-map "o" 'dired-find-file-other-window)
|
|
1240 (define-key dired-mode-map "q" 'dired-quit)
|
|
1241 (define-key dired-mode-map "r" 'dired-read-mail)
|
|
1242 (define-key dired-mode-map "s" 'dired-sort-toggle-or-edit)
|
|
1243 (define-key dired-mode-map "t" 'dired-get-target-directory)
|
|
1244 (define-key dired-mode-map "u" 'dired-unmark)
|
|
1245 (define-key dired-mode-map "v" 'dired-view-file)
|
|
1246 (define-key dired-mode-map "w" (if (fboundp 'find-file-other-frame)
|
|
1247 'dired-find-file-other-frame
|
|
1248 'dired-find-file-other-window))
|
|
1249 (define-key dired-mode-map "x" 'dired-expunge-deletions)
|
|
1250 (define-key dired-mode-map "y" 'dired-why)
|
|
1251 (define-key dired-mode-map "+" 'dired-create-directory)
|
|
1252 (define-key dired-mode-map "`" 'dired-recover-file)
|
|
1253 ;; dired-jump-back Should be in the global map, but put them here
|
|
1254 ;; too anyway.
|
|
1255 (define-key dired-mode-map "\C-x\C-j" 'dired-jump-back)
|
|
1256 (define-key dired-mode-map "\C-x4\C-j" 'dired-jump-back-other-window)
|
|
1257 (define-key dired-mode-map "\C-x5\C-j" 'dired-jump-back-other-frame)
|
|
1258 ;; Comparison commands
|
|
1259 (define-key dired-mode-map "=" 'dired-diff-prefix)
|
|
1260 ;; moving
|
|
1261 (define-key dired-mode-map "<" 'dired-prev-dirline)
|
|
1262 (define-key dired-mode-map ">" 'dired-next-dirline)
|
|
1263 (define-key dired-mode-map " " 'dired-next-line)
|
|
1264 (define-key dired-mode-map "n" 'dired-next-line)
|
|
1265 (define-key dired-mode-map "\C-n" 'dired-next-line)
|
|
1266 (define-key dired-mode-map "p" 'dired-previous-line)
|
|
1267 (define-key dired-mode-map "\C-p" 'dired-previous-line)
|
|
1268 (define-key dired-mode-map "\C-v" 'dired-scroll-up)
|
|
1269 (define-key dired-mode-map "\M-v" 'dired-scroll-down)
|
|
1270 (define-key dired-mode-map "\M-<" 'dired-beginning-of-buffer)
|
|
1271 (define-key dired-mode-map "\M->" 'dired-end-of-buffer)
|
|
1272 (define-key dired-mode-map "\C-m" 'dired-advertised-find-file)
|
|
1273 ;; motion by subdirectories
|
|
1274 (define-key dired-mode-map "^" 'dired-up-directory)
|
|
1275 (define-key dired-mode-map "\M-\C-u" 'dired-up-directory)
|
|
1276 (define-key dired-mode-map "\M-\C-d" 'dired-down-directory)
|
|
1277 (define-key dired-mode-map "\M-\C-n" 'dired-advertised-next-subdir)
|
|
1278 (define-key dired-mode-map "\M-\C-p" 'dired-advertised-prev-subdir)
|
|
1279 (define-key dired-mode-map "\C-j" 'dired-goto-subdir)
|
|
1280 ;; move to marked files
|
|
1281 (define-key dired-mode-map "\M-p" 'dired-prev-marked-file)
|
|
1282 (define-key dired-mode-map "\M-n" 'dired-next-marked-file)
|
|
1283 ;; hiding
|
|
1284 (define-key dired-mode-map "$" 'dired-hide-subdir)
|
|
1285 (define-key dired-mode-map "\M-$" 'dired-hide-all)
|
|
1286 ;; omitting
|
|
1287 (define-key dired-mode-map "\C-o" 'dired-omit-toggle)
|
|
1288 ;; markers
|
|
1289 (define-key dired-mode-map "\(" 'dired-set-marker-char)
|
|
1290 (define-key dired-mode-map "\)" 'dired-restore-marker-char)
|
|
1291 (define-key dired-mode-map "'" 'dired-marker-stack-left)
|
|
1292 (define-key dired-mode-map "\\" 'dired-marker-stack-right)
|
|
1293 ;; misc
|
|
1294 (define-key dired-mode-map "\C-i" 'dired-mark-prefix)
|
|
1295 (define-key dired-mode-map "?" 'dired-summary)
|
|
1296 (define-key dired-mode-map "\177" 'dired-backup-unflag)
|
|
1297 (define-key dired-mode-map "\C-_" 'dired-undo)
|
|
1298 (define-key dired-mode-map "\C-xu" 'dired-undo)
|
|
1299 (define-key dired-mode-map "\M-\C-?" 'dired-unmark-all-files)
|
|
1300 ;; The subdir map
|
|
1301 (define-key dired-mode-map "|" 'dired-subdir-prefix)
|
|
1302 ;; efs submap
|
|
1303 (define-key dired-mode-map "\M-e" 'efs-dired-prefix))
|
|
1304
|
|
1305
|
|
1306
|
|
1307 ;;;;------------------------------------------------------------------
|
|
1308 ;;;; The dired command
|
|
1309 ;;;;------------------------------------------------------------------
|
|
1310
|
|
1311 ;;; User commands:
|
|
1312 ;;; All of these commands should have a binding in the global keymap.
|
|
1313
|
|
1314 ;;;###autoload (define-key ctl-x-map "d" 'dired)
|
|
1315 ;;;###autoload
|
|
1316 (defun dired (dirname &optional switches)
|
|
1317 "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
|
|
1318 Optional second argument SWITCHES specifies the `ls' options used.
|
|
1319 \(Interactively, use a prefix argument to be able to specify SWITCHES.)
|
|
1320 Dired displays a list of files in DIRNAME (which may also have
|
|
1321 shell wildcards appended to select certain files). If DIRNAME is a cons,
|
|
1322 its first element is taken as the directory name and the resr as an explicit
|
|
1323 list of files to make directory entries for.
|
|
1324 \\<dired-mode-map>\
|
|
1325 You can move around in it with the usual commands.
|
|
1326 You can flag files for deletion with \\[dired-flag-file-deletion] and then
|
|
1327 delete them by typing \\[dired-expunge-deletions].
|
|
1328 Type \\[dired-describe-mode] after entering dired for more info.
|
|
1329
|
|
1330 If DIRNAME is already in a dired buffer, that buffer is used without refresh."
|
|
1331 ;; Cannot use (interactive "D") because of wildcards.
|
|
1332 (interactive (dired-read-dir-and-switches ""))
|
|
1333 (switch-to-buffer (dired-noselect dirname switches)))
|
|
1334
|
|
1335 ;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
|
|
1336 ;;;###autoload
|
|
1337 (defun dired-other-window (dirname &optional switches)
|
|
1338 "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
|
|
1339 (interactive (dired-read-dir-and-switches "in other window "))
|
|
1340 (switch-to-buffer-other-window (dired-noselect dirname switches)))
|
|
1341
|
|
1342 ;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
|
|
1343 ;;;###autoload
|
|
1344 (defun dired-other-frame (dirname &optional switches)
|
|
1345 "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame."
|
|
1346 (interactive (dired-read-dir-and-switches "in other frame "))
|
|
1347 (switch-to-buffer-other-frame (dired-noselect dirname switches)))
|
|
1348
|
|
1349 ;;;###autoload
|
|
1350 (defun dired-noselect (dir-or-list &optional switches)
|
|
1351 "Like `dired' but returns the dired buffer as value, does not select it."
|
|
1352 (or dir-or-list (setq dir-or-list (expand-file-name default-directory)))
|
|
1353 ;; This loses the distinction between "/foo/*/" and "/foo/*" that
|
|
1354 ;; some shells make:
|
|
1355 (let (dirname)
|
|
1356 (if (consp dir-or-list)
|
|
1357 (setq dirname (car dir-or-list))
|
|
1358 (setq dirname dir-or-list))
|
|
1359 (setq dirname (expand-file-name (directory-file-name dirname)))
|
|
1360 (if (file-directory-p dirname)
|
|
1361 (setq dirname (file-name-as-directory dirname)))
|
|
1362 (if (consp dir-or-list)
|
|
1363 (setq dir-or-list (cons dirname (cdr dir-or-list)))
|
|
1364 (setq dir-or-list dirname))
|
|
1365 (dired-internal-noselect dir-or-list switches)))
|
|
1366
|
|
1367 ;; Adapted from code by wurgler@zippysun.math.uakron.edu (Tom Wurgler).
|
|
1368 ;;;###autoload (define-key ctl-x-map "\C-j" 'dired-jump-back)
|
|
1369 ;;;###autoload
|
|
1370 (defun dired-jump-back ()
|
|
1371 "Jump back to dired.
|
|
1372 If in a file, dired the current directory and move to file's line.
|
|
1373 If in dired already, pop up a level and goto old directory's line.
|
|
1374 In case the proper dired file line cannot be found, refresh the dired
|
|
1375 buffer and try again."
|
|
1376 (interactive)
|
|
1377 (let* ((file (if (eq major-mode 'dired-mode)
|
|
1378 (directory-file-name (dired-current-directory))
|
|
1379 buffer-file-name))
|
|
1380 (dir (if file
|
|
1381 (file-name-directory file)
|
|
1382 default-directory)))
|
|
1383 (dired dir)
|
|
1384 (if file (dired-really-goto-file file))))
|
|
1385
|
|
1386 ;;;###autoload (define-key ctl-x-4-map "\C-j" 'dired-jump-back-other-window)
|
|
1387 ;;;###autoload
|
|
1388 (defun dired-jump-back-other-window ()
|
|
1389 "Like \\[dired-jump-back], but to other window."
|
|
1390 (interactive)
|
|
1391 (let* ((file (if (eq major-mode 'dired-mode)
|
|
1392 (directory-file-name (dired-current-directory))
|
|
1393 buffer-file-name))
|
|
1394 (dir (if file
|
|
1395 (file-name-directory file)
|
|
1396 default-directory)))
|
|
1397 (dired-other-window dir)
|
|
1398 (if file (dired-really-goto-file file))))
|
|
1399
|
|
1400 ;;;###autoload (define-key ctl-x-5-map "\C-j" 'dired-jump-back-other-frame)
|
|
1401 ;;;###autoload
|
|
1402 (defun dired-jump-back-other-frame ()
|
|
1403 "Like \\[dired-jump-back], but in another frame."
|
|
1404 (interactive)
|
|
1405 (let* ((file (if (eq major-mode 'dired-mode)
|
|
1406 (directory-file-name (dired-current-directory))
|
|
1407 buffer-file-name))
|
|
1408 (dir (if file
|
|
1409 (file-name-directory file)
|
|
1410 default-directory)))
|
|
1411 (dired-other-frame dir)
|
|
1412 (if file (dired-really-goto-file file))))
|
|
1413
|
|
1414 ;;; Dired mode
|
|
1415
|
|
1416 ;; Dired mode is suitable only for specially formatted data.
|
|
1417 (put 'dired-mode 'mode-class 'special)
|
|
1418
|
|
1419 (defun dired-mode (&optional dirname switches)
|
|
1420 "\\<dired-mode-map>Dired mode is for \"editing\" directory trees.
|
|
1421
|
|
1422 For a simple one-line help message, type \\[dired-summary]
|
|
1423 For a moderately detailed description of dired mode, type \\[dired-describe-mode]
|
|
1424 For the full dired info tree, type \\[universal-argument] \\[dired-describe-mode]"
|
|
1425 ;; Not to be called interactively (e.g. dired-directory will be set
|
|
1426 ;; to default-directory, which is wrong with wildcards).
|
|
1427 (kill-all-local-variables)
|
|
1428 (use-local-map dired-mode-map)
|
|
1429 (setq major-mode 'dired-mode
|
|
1430 mode-name "Dired"
|
|
1431 case-fold-search nil
|
|
1432 buffer-read-only t
|
|
1433 selective-display t ; for subdirectory hiding
|
|
1434 selective-display-ellipses nil ; for omit toggling
|
|
1435 mode-line-buffer-identification '("Dired: %12b")
|
|
1436 mode-line-modified (format dired-mode-line-modified "--" "--" "-")
|
|
1437 dired-directory (expand-file-name (or dirname default-directory))
|
|
1438 dired-internal-switches (dired-make-switches-list
|
|
1439 (or switches dired-listing-switches)))
|
|
1440 (dired-advertise) ; default-directory is already set
|
|
1441 (set (make-local-variable 'revert-buffer-function)
|
|
1442 (function dired-revert))
|
|
1443 (set (make-local-variable 'default-directory-function)
|
|
1444 'dired-current-directory)
|
|
1445 (set (make-local-variable 'page-delimiter)
|
|
1446 "\n\n")
|
|
1447 (set (make-local-variable 'list-buffers-directory)
|
|
1448 dired-directory)
|
|
1449 ;; Will only do something in Emacs 19.
|
|
1450 (add-hook (make-local-variable 'kill-buffer-hook)
|
|
1451 'dired-unadvertise-current-buffer)
|
|
1452 ;; Same here
|
|
1453 (if window-system
|
|
1454 (add-hook (make-local-variable 'post-command-hook)
|
|
1455 (function
|
|
1456 (lambda ()
|
|
1457 (if (memq this-command dired-modeline-tracking-cmds)
|
|
1458 (dired-update-mode-line t))))))
|
|
1459 (dired-sort-other dired-internal-switches t)
|
|
1460 (dired-hack-local-variables)
|
|
1461 (run-hooks 'dired-mode-hook)
|
|
1462 ;; Run this after dired-mode-hook, in case that hook makes changes to
|
|
1463 ;; the keymap.
|
|
1464 (dired-grok-keymap))
|
|
1465
|
|
1466 ;;; Internal functions for starting dired
|
|
1467
|
|
1468 (defun dired-read-dir-and-switches (str)
|
|
1469 ;; For use in interactive.
|
|
1470 (reverse (list
|
|
1471 (if current-prefix-arg
|
|
1472 (read-string "Dired listing switches: "
|
|
1473 dired-listing-switches))
|
|
1474 (let ((default-directory (default-directory)))
|
|
1475 (read-file-name (format "Dired %s(directory): " str)
|
|
1476 nil default-directory nil)))))
|
|
1477
|
|
1478 (defun dired-hack-local-variables ()
|
|
1479 "Parse, bind or evaluate any local variables for current dired buffer.
|
|
1480 See variable `dired-local-variables-file'."
|
|
1481 (if (and dired-local-variables-file
|
|
1482 (file-exists-p dired-local-variables-file))
|
|
1483 (let (buffer-read-only opoint )
|
|
1484 (save-excursion
|
|
1485 (goto-char (point-max))
|
|
1486 (setq opoint (point-marker))
|
|
1487 (insert "\^L\n")
|
|
1488 (insert-file-contents dired-local-variables-file))
|
|
1489 (let ((buffer-file-name dired-local-variables-file))
|
|
1490 (condition-case err
|
|
1491 (hack-local-variables)
|
|
1492 (error (message "Error in dired-local-variables-file: %s" err)
|
|
1493 (sit-for 1))))
|
|
1494 ;; Must delete it as (eobp) is often used as test for last
|
|
1495 ;; subdir in dired.el.
|
|
1496 (delete-region opoint (point-max))
|
|
1497 (set-marker opoint nil))))
|
|
1498
|
|
1499 ;; Separate function from dired-noselect for the sake of dired-vms.el.
|
|
1500 (defun dired-internal-noselect (dir-or-list &optional switches mode)
|
|
1501 ;; If there is an existing dired buffer for DIRNAME, just leave
|
|
1502 ;; buffer as it is (don't even call dired-revert).
|
|
1503 ;; This saves time especially for deep trees or with efs.
|
|
1504 ;; The user can type `g'easily, and it is more consistent with find-file.
|
|
1505 ;; But if SWITCHES are given they are probably different from the
|
|
1506 ;; buffer's old value, so call dired-sort-other, which does
|
|
1507 ;; revert the buffer.
|
|
1508 ;; If the user specifies a directory with emacs startup, eg.
|
|
1509 ;; emacs ~, dir-or-list may be unexpanded at this point.
|
|
1510
|
|
1511 (let* ((dirname (expand-file-name (if (consp dir-or-list)
|
|
1512 (car dir-or-list)
|
|
1513 dir-or-list)))
|
|
1514 (buffer (dired-find-buffer-nocreate dir-or-list mode))
|
|
1515 ;; note that buffer already is in dired-mode, if found
|
|
1516 (new-buffer-p (not buffer))
|
|
1517 (old-buf (current-buffer))
|
|
1518 wildcard)
|
|
1519 (or buffer
|
|
1520 (let ((default-major-mode 'fundamental-mode))
|
|
1521 ;; We don't want default-major-mode to run hooks and set auto-fill
|
|
1522 ;; or whatever, now that dired-mode does not
|
|
1523 ;; kill-all-local-variables any longer.
|
|
1524 (setq buffer (create-file-buffer (directory-file-name dirname)))))
|
|
1525 (set-buffer buffer)
|
|
1526 (if (not new-buffer-p) ; existing buffer ...
|
|
1527 (progn
|
|
1528 (if switches
|
|
1529 (dired-sort-other
|
|
1530 (if (stringp switches)
|
|
1531 (dired-make-switches-list switches)
|
|
1532 switches)))
|
|
1533 (if dired-verify-modtimes (dired-verify-modtimes))
|
|
1534 (if (and dired-find-subdir
|
|
1535 (not (string-equal (dired-current-directory)
|
|
1536 (file-name-as-directory dirname))))
|
|
1537 (dired-initial-position dirname)))
|
|
1538 ;; Else a new buffer
|
|
1539 (if (file-directory-p dirname)
|
|
1540 (setq default-directory dirname
|
|
1541 wildcard (consp dir-or-list))
|
|
1542 (setq default-directory (file-name-directory dirname)
|
|
1543 wildcard t))
|
|
1544 (or switches (setq switches dired-listing-switches))
|
|
1545 (dired-mode dirname switches)
|
|
1546 ;; default-directory and dired-internal-switches are set now
|
|
1547 ;; (buffer-local), so we can call dired-readin:
|
|
1548 (let ((failed t))
|
|
1549 (unwind-protect
|
|
1550 (progn (dired-readin dir-or-list buffer wildcard)
|
|
1551 (setq failed nil))
|
|
1552 ;; dired-readin can fail if parent directories are inaccessible.
|
|
1553 ;; Don't leave an empty buffer around in that case.
|
|
1554 (if failed (kill-buffer buffer))))
|
|
1555 ;; No need to narrow since the whole buffer contains just
|
|
1556 ;; dired-readin's output, nothing else. The hook can
|
|
1557 ;; successfully use dired functions (e.g. dired-get-filename)
|
|
1558 ;; as the subdir-alist has been built in dired-readin.
|
|
1559 (run-hooks 'dired-after-readin-hook)
|
|
1560 ;; I put omit-expunge after the dired-after-readin-hook
|
|
1561 ;; in case that hook marks files. Does this make sense? Also, users
|
|
1562 ;; might want to set dired-omit-files-p in some incredibly clever
|
|
1563 ;; way depending on the contents of the directory... I don't know...
|
|
1564 (if dired-omit-files
|
|
1565 (dired-omit-expunge nil t))
|
|
1566 (goto-char (point-min))
|
|
1567 (dired-initial-position dirname))
|
|
1568 (set-buffer old-buf)
|
|
1569 buffer))
|
|
1570
|
|
1571 (defun dired-find-buffer-nocreate (dir-or-list &optional mode)
|
|
1572 ;; Returns a dired buffer for DIR-OR-LIST. DIR-OR-LIST may be wildcard,
|
|
1573 ;; or a directory and alist of files.
|
|
1574 ;; If dired-find-subdir is non-nil, is satisfied with a dired
|
|
1575 ;; buffer containing DIR-OR-LIST as a subdirectory. If there is more
|
|
1576 ;; than one candidate, returns the most recently used.
|
|
1577 (if dired-find-subdir
|
|
1578 (let ((buffers (sort (delq (current-buffer)
|
|
1579 (dired-buffers-for-dir dir-or-list t))
|
|
1580 (function dired-buffer-more-recently-used-p))))
|
|
1581 (or (car buffers)
|
|
1582 ;; Couldn't find another buffer. Will the current one do?
|
|
1583 ;; It is up dired-initial-position to actually go to the subdir.
|
|
1584 (and (or (equal dir-or-list dired-directory) ; covers wildcards
|
|
1585 (and (stringp dir-or-list)
|
|
1586 (not (string-equal
|
|
1587 dir-or-list
|
|
1588 (expand-file-name default-directory)))
|
|
1589 (assoc (file-name-as-directory dir-or-list)
|
|
1590 dired-subdir-alist)))
|
|
1591 (current-buffer))))
|
|
1592 ;; Else just look through the buffer list.
|
|
1593 (let (found (blist (buffer-list)))
|
|
1594 (or mode (setq mode 'dired-mode))
|
|
1595 (save-excursion
|
|
1596 (while blist
|
|
1597 (set-buffer (car blist))
|
|
1598 (if (and (eq major-mode mode)
|
|
1599 (equal dired-directory dir-or-list))
|
|
1600 (setq found (car blist)
|
|
1601 blist nil)
|
|
1602 (setq blist (cdr blist)))))
|
|
1603 found)))
|
|
1604
|
|
1605 (defun dired-initial-position (dirname)
|
|
1606 ;; Where point should go in a new listing of DIRNAME.
|
|
1607 ;; Point assumed at beginning of new subdir line.
|
|
1608 (end-of-line)
|
|
1609 (if dired-find-subdir (dired-goto-subdir dirname))
|
|
1610 (if dired-trivial-filenames (dired-goto-next-nontrivial-file))
|
|
1611 (dired-update-mode-line t))
|
|
1612
|
|
1613 (defun dired-readin (dir-or-list buffer &optional wildcard)
|
|
1614 ;; Read in a new dired buffer
|
|
1615 ;; dired-readin differs from dired-insert-subdir in that it accepts
|
|
1616 ;; wildcards, erases the buffer, and builds the subdir-alist anew
|
|
1617 ;; (including making it buffer-local and clearing it first).
|
|
1618 ;; default-directory and dired-internal-switches must be buffer-local
|
|
1619 ;; and initialized by now.
|
|
1620 ;; Thus we can test (equal default-directory dirname) instead of
|
|
1621 ;; (file-directory-p dirname) and save a filesystem transaction.
|
|
1622 ;; This is wrong, if dired-before-readin-hook changes default-directory
|
|
1623 ;; Also, we can run this hook which may want to modify the switches
|
|
1624 ;; based on default-directory, e.g. with efs to a SysV host
|
|
1625 ;; where ls won't understand -Al switches.
|
|
1626 (let (dirname other-dirs)
|
|
1627 (if (consp dir-or-list)
|
|
1628 (setq dir-or-list (dired-frob-dir-list dir-or-list)
|
|
1629 other-dirs (cdr dir-or-list)
|
|
1630 dir-or-list (car dir-or-list)
|
|
1631 dirname (car dir-or-list))
|
|
1632 (setq dirname dir-or-list))
|
|
1633 (setq dirname (expand-file-name dirname))
|
|
1634 (if (consp dir-or-list)
|
|
1635 (setq dir-or-list (cons dirname (cdr dir-or-list))))
|
|
1636 (save-excursion
|
|
1637 (set-buffer buffer)
|
|
1638 (run-hooks 'dired-before-readin-hook)
|
|
1639 (message "Reading directory %s..." dirname)
|
|
1640 (let (buffer-read-only)
|
|
1641 (widen)
|
|
1642 (erase-buffer)
|
|
1643 (dired-readin-insert dir-or-list wildcard)
|
|
1644 (dired-indent-listing (point-min) (point-max))
|
|
1645 ;; We need this to make the root dir have a header line as all
|
|
1646 ;; other subdirs have:
|
|
1647 (goto-char (point-min))
|
|
1648 (dired-insert-headerline (expand-file-name default-directory)))
|
|
1649 (message "Reading directory %s...done" dirname)
|
|
1650 (set-buffer-modified-p nil)
|
|
1651 ;; Must first make alist buffer local and set it to nil because
|
|
1652 ;; dired-build-subdir-alist will call dired-clear-alist first
|
|
1653 (setq dired-subdir-alist nil)
|
|
1654 (if (memq ?R dired-internal-switches)
|
|
1655 (dired-build-subdir-alist)
|
|
1656 ;; no need to parse the buffer if listing is not recursive
|
|
1657 (dired-simple-subdir-alist))
|
|
1658 (if other-dirs
|
|
1659 (mapcar
|
|
1660 (function
|
|
1661 (lambda (x)
|
|
1662 (if (dired-in-this-tree (car x) dirname)
|
|
1663 (dired-insert-subdir x))))
|
|
1664 other-dirs)))))
|
|
1665
|
|
1666 ;;; Subroutines of dired-readin
|
|
1667
|
|
1668 (defun dired-readin-insert (dir-or-list &optional wildcard)
|
|
1669 ;; Just insert listing for the passed-in directory or
|
|
1670 ;; directory-and-file list, assuming a clean buffer.
|
|
1671 (let* ((switches (dired-make-switches-string dired-internal-switches))
|
|
1672 (dir-is-list (consp dir-or-list))
|
|
1673 (dirname (if dir-is-list (car dir-or-list) dir-or-list)))
|
|
1674 (if wildcard
|
|
1675 (progn
|
|
1676 (or (file-readable-p
|
|
1677 (if dir-is-list
|
|
1678 dirname
|
|
1679 (directory-file-name (file-name-directory dirname))))
|
|
1680 (error "Directory %s inaccessible or nonexistent" dirname))
|
|
1681 ;; else assume it contains wildcards
|
|
1682 (dired-insert-directory dir-or-list switches t)
|
|
1683 (save-excursion
|
|
1684 ;; insert wildcard instead of total line:
|
|
1685 (goto-char (point-min))
|
|
1686 (if dir-is-list
|
|
1687 (insert "list wildcard\n")
|
|
1688 (insert "wildcard " (file-name-nondirectory dirname) "\n"))))
|
|
1689 (dired-insert-directory dir-or-list switches nil t))))
|
|
1690
|
|
1691 (defun dired-insert-directory (dir-or-list switches &optional wildcard full-p)
|
|
1692 ;; Do the right thing whether dir-or-list is atomic or not. If it is,
|
|
1693 ;; insert all files listed in the cdr -- the car is the passed-in directory
|
|
1694 ;; list.
|
|
1695 (let ((opoint (point))
|
|
1696 (insert-directory-program dired-ls-program))
|
|
1697 (if (consp dir-or-list)
|
|
1698 (mapcar
|
|
1699 (function
|
|
1700 (lambda (x)
|
|
1701 (insert-directory x switches wildcard)))
|
|
1702 (cdr dir-or-list))
|
|
1703 (insert-directory dir-or-list switches wildcard full-p))
|
|
1704 (dired-insert-set-properties opoint (point)))
|
|
1705 (setq dired-directory dir-or-list))
|
|
1706
|
|
1707 (defun dired-frob-dir-list (dir-list)
|
|
1708 (let* ((top (file-name-as-directory (expand-file-name (car dir-list))))
|
|
1709 (tail (cdr dir-list))
|
|
1710 (result (list (list top)))
|
|
1711 elt dir)
|
|
1712 (setq tail
|
|
1713 (mapcar
|
|
1714 (function
|
|
1715 (lambda (x)
|
|
1716 (directory-file-name (expand-file-name x top))))
|
|
1717 tail))
|
|
1718 (while tail
|
|
1719 (setq dir (file-name-directory (car tail)))
|
|
1720 (if (setq elt (assoc dir result))
|
|
1721 (nconc elt (list (car tail)))
|
|
1722 (nconc result (list (list dir (car tail)))))
|
|
1723 (setq tail (cdr tail)))
|
|
1724 result))
|
|
1725
|
|
1726 (defun dired-insert-headerline (dir);; also used by dired-insert-subdir
|
|
1727 ;; Insert DIR's headerline with no trailing slash, exactly like ls
|
|
1728 ;; would, and put cursor where dired-build-subdir-alist puts subdir
|
|
1729 ;; boundaries.
|
|
1730 (save-excursion (insert " " (directory-file-name dir) ":\n")))
|
|
1731
|
|
1732 (defun dired-verify-modtimes ()
|
|
1733 ;; Check the modtimes of all subdirs.
|
|
1734 (let ((alist dired-subdir-alist)
|
|
1735 on-disk in-mem badies)
|
|
1736 (while alist
|
|
1737 (and (setq in-mem (nth 4 (car alist)))
|
|
1738 (setq on-disk (dired-file-modtime (car (car alist))))
|
|
1739 (not (equal in-mem on-disk))
|
|
1740 (setq badies (cons (cons (car (car alist))
|
|
1741 (nth 3 (car alist)))
|
|
1742 badies)))
|
|
1743 (setq alist (cdr alist)))
|
|
1744 (and badies
|
|
1745 (let* ((ofile (dired-get-filename nil t))
|
|
1746 (osub (and (null ofile) (dired-get-subdir)))
|
|
1747 (opoint (point))
|
|
1748 (ocol (current-column)))
|
|
1749 (unwind-protect
|
|
1750 (and
|
|
1751 (or (memq 'revert-subdirs dired-no-confirm)
|
|
1752 (save-window-excursion
|
|
1753 (let ((flist (mapcar
|
|
1754 (function
|
|
1755 (lambda (f)
|
|
1756 (dired-abbreviate-file-name (car f))))
|
|
1757 badies)))
|
|
1758 (switch-to-buffer (current-buffer))
|
|
1759 (dired-mark-pop-up
|
|
1760 "*Stale Subdirectories*" 'revert-subdirs
|
|
1761 flist 'y-or-n-p
|
|
1762 (if (= (length flist) 1)
|
|
1763 (concat "Subdirectory " (car flist)
|
|
1764 " has changed on disk. Re-list? ")
|
|
1765 "Subdirectories have changed on disk. Re-list? "))
|
|
1766 )))
|
|
1767 (while badies
|
|
1768 (dired-insert-subdir (car (car badies))
|
|
1769 (cdr (car badies)) nil t)
|
|
1770 (setq badies (cdr badies))))
|
|
1771 ;; We can't use dired-save-excursion here, because we are
|
|
1772 ;; rewriting the entire listing, and not just changing a single
|
|
1773 ;; file line.
|
|
1774 (or (if ofile
|
|
1775 (dired-goto-file ofile)
|
|
1776 (if osub
|
|
1777 (dired-goto-subdir osub)))
|
|
1778 (progn
|
|
1779 (goto-char opoint)
|
|
1780 (beginning-of-line)
|
|
1781 (skip-chars-forward "^\n\r" (+ (point) ocol))))
|
|
1782 (dired-update-mode-line t)
|
|
1783 (dired-update-mode-line-modified t))))))
|
|
1784
|
|
1785 (defun dired-indent-listing (start end)
|
|
1786 ;; Indent a dired listing.
|
|
1787 (let (indent-tabs-mode)
|
|
1788 (indent-rigidly start end 2)
|
|
1789 ;; Quote any null lines that shouldn't be.
|
|
1790 (save-excursion
|
|
1791 (goto-char start)
|
|
1792 (while (search-forward "\n\n" end t)
|
|
1793 (forward-char -2)
|
|
1794 (if (looking-at dired-subdir-regexp)
|
|
1795 (goto-char (match-end 3))
|
|
1796 (progn
|
|
1797 (forward-char 1)
|
|
1798 (insert " ")))))))
|
|
1799
|
|
1800
|
|
1801 ;;;; ------------------------------------------------------------
|
|
1802 ;;;; Reverting a dired buffer, or specific file lines within it.
|
|
1803 ;;;; ------------------------------------------------------------
|
|
1804
|
|
1805 (defun dired-revert (&optional arg noconfirm)
|
|
1806 ;; Reread the dired buffer. Must also be called after
|
|
1807 ;; dired-internal-switches have changed.
|
|
1808 ;; Should not fail even on completely garbaged buffers.
|
|
1809 ;; Preserves old cursor, marks/flags, hidden-p.
|
|
1810 (widen) ; just in case user narrowed
|
|
1811 (let ((opoint (point))
|
|
1812 (ofile (dired-get-filename nil t))
|
|
1813 (hidden-subdirs (dired-remember-hidden))
|
|
1814 ;; switches for top-level dir
|
|
1815 (oswitches (or (nth 3 (nth (1- (length dired-subdir-alist))
|
|
1816 dired-subdir-alist))
|
|
1817 (delq ?R (copy-sequence dired-internal-switches))))
|
|
1818 ;; all other subdirs
|
|
1819 (old-subdir-alist (cdr (reverse dired-subdir-alist)))
|
|
1820 (omitted-subdirs (dired-remember-omitted))
|
|
1821 ;; do this after dired-remember-hidden, since this unhides
|
|
1822 (mark-alist (dired-remember-marks (point-min) (point-max)))
|
|
1823 (kill-files-p (save-excursion
|
|
1824 (goto-char (point))
|
|
1825 (search-forward
|
|
1826 (concat (char-to-string ?\r)
|
|
1827 (regexp-quote
|
|
1828 (char-to-string
|
|
1829 dired-kill-marker-char)))
|
|
1830 nil t)))
|
|
1831 buffer-read-only)
|
|
1832 ;; This is bogus, as it will not handle all the ways that efs uses cache.
|
|
1833 ;; Better to just use the fact that revert-buffer-function is a
|
|
1834 ;; buffer-local variable, and reset it to something that knows about
|
|
1835 ;; cache.
|
|
1836 ;; (dired-uncache
|
|
1837 ;; (if (consp dired-directory) (car dired-directory) dired-directory))
|
|
1838 ;; treat top level dir extra (it may contain wildcards)
|
|
1839 (let ((dired-after-readin-hook nil)
|
|
1840 ;; don't run that hook for each subdir...
|
|
1841 (dired-omit-files nil)
|
|
1842 (dired-internal-switches oswitches))
|
|
1843 (dired-readin dired-directory (current-buffer)
|
|
1844 ;; Don't test for wildcards by checking string=
|
|
1845 ;; default-directory and dired-directory
|
|
1846 ;; in case default-directory got munged.
|
|
1847 (or (consp dired-directory)
|
|
1848 (null (file-directory-p dired-directory))))
|
|
1849 ;; The R-switch will clobber sorting of subdirs.
|
|
1850 ;; What is the right thing to do here?
|
|
1851 (dired-insert-old-subdirs old-subdir-alist))
|
|
1852 (dired-mark-remembered mark-alist) ; mark files that were marked
|
|
1853 (if kill-files-p (dired-do-hide dired-kill-marker-char))
|
|
1854 (run-hooks 'dired-after-readin-hook) ; no need to narrow
|
|
1855 ;; omit-expunge after the readin hook
|
|
1856 (save-excursion
|
|
1857 (mapcar (function (lambda (dir)
|
|
1858 (if (dired-goto-subdir dir)
|
|
1859 (dired-omit-expunge))))
|
|
1860 omitted-subdirs))
|
|
1861 ;; hide subdirs that were hidden
|
|
1862 (save-excursion
|
|
1863 (mapcar (function (lambda (dir)
|
|
1864 (if (dired-goto-subdir dir)
|
|
1865 (dired-hide-subdir 1))))
|
|
1866 hidden-subdirs))
|
|
1867 ;; Try to get back to where we were
|
|
1868 (or (and ofile (dired-goto-file ofile))
|
|
1869 (goto-char opoint))
|
|
1870 (dired-move-to-filename)
|
|
1871 (dired-update-mode-line t)
|
|
1872 (dired-update-mode-line-modified t)))
|
|
1873
|
|
1874 (defun dired-do-redisplay (&optional arg)
|
|
1875 "Redisplay all marked (or next ARG) files."
|
|
1876 (interactive "P")
|
|
1877 ;; message instead of making dired-map-over-marks show-progress is
|
|
1878 ;; much faster
|
|
1879 (dired-map-over-marks (let ((fname (dired-get-filename)))
|
|
1880 (dired-uncache fname nil)
|
|
1881 (message "Redisplaying %s..." fname)
|
|
1882 (dired-update-file-line fname))
|
|
1883 arg)
|
|
1884 (dired-update-mode-line-modified t)
|
|
1885 (message "Redisplaying...done"))
|
|
1886
|
|
1887 (defun dired-redisplay-subdir (&optional arg)
|
|
1888 "Redisplay the current subdirectory.
|
|
1889 With a prefix prompts for listing switches."
|
|
1890 (interactive "P")
|
|
1891 (let ((switches (and arg (dired-make-switches-list
|
|
1892 (read-string "Switches for listing: "
|
|
1893 (dired-make-switches-string
|
|
1894 dired-internal-switches)))))
|
|
1895 (dir (dired-current-directory))
|
|
1896 (opoint (point))
|
|
1897 (ofile (dired-get-filename nil t)))
|
|
1898 (or switches
|
|
1899 (setq switches (nth 3 (assoc dir dired-subdir-alist))))
|
|
1900 (or switches
|
|
1901 (setq switches (delq ?R (copy-sequence dired-internal-switches))))
|
|
1902 (message "Redisplaying %s..." dir)
|
|
1903 (dired-uncache dir t)
|
|
1904 (dired-insert-subdir dir switches)
|
|
1905 (dired-update-mode-line-modified t)
|
|
1906 (or (and ofile (dired-goto-file ofile)) (goto-char opoint))
|
|
1907 (message "Redisplaying %s... done" dir)))
|
|
1908
|
|
1909 (defun dired-update-file-line (file)
|
|
1910 ;; Delete the current line, and insert an entry for FILE.
|
|
1911 ;; Does not update other dired buffers. Use dired-relist-file for that.
|
|
1912 (let* ((start (save-excursion (skip-chars-backward "^\n\r") (point)))
|
|
1913 (char (char-after start)))
|
|
1914 (dired-save-excursion
|
|
1915 ;; don't remember omit marks
|
|
1916 (if (memq char (list ?\040 dired-omit-marker-char))
|
|
1917 (setq char nil))
|
|
1918 ;; Delete the current-line. Even though dired-add-entry will not
|
|
1919 ;; insert duplicates, the file for the current line may not be the same as
|
|
1920 ;; FILE. eg. dired-do-compress
|
|
1921 (delete-region (save-excursion (skip-chars-backward "^\n\r") (1- (point)))
|
|
1922 (progn (skip-chars-forward "^\n\r") (point)))
|
|
1923 ;; dired-add-entry inserts at the end of the previous line.
|
|
1924 (forward-char 1)
|
|
1925 (dired-add-entry file char t))))
|
|
1926
|
|
1927 ;;; Subroutines of dired-revert
|
|
1928 ;;; Some of these are also used when inserting subdirs.
|
|
1929
|
|
1930 ;; Don't want to remember omit marks, in case omission regexps
|
|
1931 ;; were changed, before the dired-revert. If we don't unhide
|
|
1932 ;; omitted files, we won't see their marks. Therefore we use
|
|
1933 ;; dired-omit-unhide-region.
|
|
1934
|
|
1935 (defun dired-remember-marks (beg end)
|
|
1936 ;; Return alist of files and their marks, from BEG to END.
|
|
1937 (if selective-display ; must unhide to make this work.
|
|
1938 (let (buffer-read-only)
|
|
1939 (subst-char-in-region (point-min) (point-max) ?\r ?\n)
|
|
1940 (dired-do-hide dired-omit-marker-char)))
|
|
1941 (let (fil chr alist)
|
|
1942 (save-excursion
|
|
1943 (goto-char beg)
|
|
1944 (while (re-search-forward dired-re-mark end t)
|
|
1945 (if (setq fil (dired-get-filename nil t))
|
|
1946 (setq chr (preceding-char)
|
|
1947 alist (cons (cons fil chr) alist)))))
|
|
1948 alist))
|
|
1949
|
|
1950 (defun dired-mark-remembered (alist)
|
|
1951 ;; Mark all files remembered in ALIST.
|
|
1952 (let (elt fil chr)
|
|
1953 (while alist
|
|
1954 (setq elt (car alist)
|
|
1955 alist (cdr alist)
|
|
1956 fil (car elt)
|
|
1957 chr (cdr elt))
|
|
1958 (if (dired-goto-file fil)
|
|
1959 (save-excursion
|
|
1960 (beginning-of-line)
|
|
1961 (dired-substitute-marker (point) (following-char) chr))))))
|
|
1962
|
|
1963 (defun dired-remember-hidden ()
|
|
1964 ;; Return a list of all hidden subdirs.
|
|
1965 (let ((l dired-subdir-alist) dir result min)
|
|
1966 (while l
|
|
1967 (setq dir (car (car l))
|
|
1968 min (dired-get-subdir-min (car l))
|
|
1969 l (cdr l))
|
|
1970 (if (and (>= min (point-min)) (<= min (point-max))
|
|
1971 (dired-subdir-hidden-p dir))
|
|
1972 (setq result (cons dir result))))
|
|
1973 result))
|
|
1974
|
|
1975 (defun dired-insert-old-subdirs (old-subdir-alist)
|
|
1976 ;; Try to insert all subdirs that were displayed before
|
|
1977 (let (elt dir switches)
|
|
1978 (while old-subdir-alist
|
|
1979 (setq elt (car old-subdir-alist)
|
|
1980 old-subdir-alist (cdr old-subdir-alist)
|
|
1981 dir (car elt)
|
|
1982 switches (or (nth 3 elt) dired-internal-switches))
|
|
1983 (condition-case ()
|
|
1984 (dired-insert-subdir dir switches)
|
|
1985 (error nil)))))
|
|
1986
|
|
1987 (defun dired-uncache (file dir-p)
|
|
1988 ;; Remove directory DIR from any directory cache.
|
|
1989 ;; If DIR-P is non-nil, then FILE is a directory
|
|
1990 (let ((handler (find-file-name-handler file 'dired-uncache)))
|
|
1991 (if handler
|
|
1992 (funcall handler 'dired-uncache file dir-p))))
|
|
1993
|
|
1994
|
|
1995 ;;;; -------------------------------------------------------------
|
|
1996 ;;;; Inserting subdirectories
|
|
1997 ;;;; -------------------------------------------------------------
|
|
1998
|
|
1999 (defun dired-maybe-insert-subdir (dirname &optional
|
|
2000 switches no-error-if-not-dir-p)
|
|
2001 "Insert this subdirectory into the same dired buffer.
|
|
2002 If it is already present, just move to it (type \\[dired-do-redisplay] to
|
|
2003 refresh), else inserts it at its natural place (as ls -lR would have done).
|
|
2004 With a prefix arg, you may edit the ls switches used for this listing.
|
|
2005 You can add `R' to the switches to expand the whole tree starting at
|
|
2006 this subdirectory.
|
|
2007 This function takes some pains to conform to ls -lR output."
|
|
2008 (interactive
|
|
2009 (list (dired-get-filename)
|
|
2010 (if current-prefix-arg
|
|
2011 (dired-make-switches-list
|
|
2012 (read-string "Switches for listing: "
|
|
2013 (dired-make-switches-string
|
|
2014 dired-internal-switches))))))
|
|
2015 (let ((opoint (point)))
|
|
2016 ;; We don't need a marker for opoint as the subdir is always
|
|
2017 ;; inserted *after* opoint.
|
|
2018 (setq dirname (file-name-as-directory dirname))
|
|
2019 (or (and (not switches)
|
|
2020 (dired-goto-subdir dirname))
|
|
2021 (dired-insert-subdir dirname switches no-error-if-not-dir-p))
|
|
2022 ;; Push mark so that it's easy to find back. Do this after the
|
|
2023 ;; insert message so that the user sees the `Mark set' message.
|
|
2024 (push-mark opoint)))
|
|
2025
|
|
2026 (defun dired-insert-subdir (dir-or-list &optional
|
|
2027 switches no-error-if-not-dir-p no-posn)
|
|
2028 "Insert this subdirectory into the same dired buffer.
|
|
2029 If it is already present, overwrites previous entry,
|
|
2030 else inserts it at its natural place (as ls -lR would have done).
|
|
2031 With a prefix arg, you may edit the ls switches used for this listing.
|
|
2032 You can add `R' to the switches to expand the whole tree starting at
|
|
2033 this subdirectory.
|
|
2034 This function takes some pains to conform to ls -lR output."
|
|
2035 ;; NO-ERROR-IF-NOT-DIR-P needed for special filesystems like
|
|
2036 ;; Prospero where dired-ls does the right thing, but
|
|
2037 ;; file-directory-p has not been redefined.
|
|
2038 ;; SWITCHES should be a list.
|
|
2039 ;; If NO-POSN is non-nil, doesn't bother position the point at
|
|
2040 ;; the first nontrivial file line. This can be used as an efficiency
|
|
2041 ;; hack when calling this from a program.
|
|
2042 (interactive
|
|
2043 (list (dired-get-filename)
|
|
2044 (if current-prefix-arg
|
|
2045 (dired-make-switches-list
|
|
2046 (read-string "Switches for listing: "
|
|
2047 (dired-make-switches-string
|
|
2048 dired-internal-switches))))))
|
|
2049 (let ((dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list)))
|
|
2050 (setq dirname (file-name-as-directory (expand-file-name dirname)))
|
|
2051 (or (dired-in-this-tree dirname (expand-file-name default-directory))
|
|
2052 (error "%s: not in this directory tree" dirname))
|
|
2053 (or no-error-if-not-dir-p
|
|
2054 (file-directory-p dirname)
|
|
2055 (error "Attempt to insert a non-directory: %s" dirname))
|
|
2056 (if switches
|
|
2057 (or (dired-compatible-switches-p dired-internal-switches switches)
|
|
2058 (error "Cannot have subdirs with %s and %s switches together."
|
|
2059 (dired-make-switches-string dired-internal-switches)
|
|
2060 (dired-make-switches-string switches)))
|
|
2061 (setq switches dired-internal-switches))
|
|
2062 (let ((elt (assoc dirname dired-subdir-alist))
|
|
2063 mark-alist opoint-max buffer-read-only)
|
|
2064 (if (memq ?R switches)
|
|
2065 ;; avoid duplicated subdirs
|
|
2066 (progn
|
|
2067 (setq mark-alist (dired-kill-tree dirname t))
|
|
2068 (dired-insert-subdir-newpos dirname))
|
|
2069 (if elt
|
|
2070 ;; If subdir is already present, remove it and remember its marks
|
|
2071 (setq mark-alist (dired-insert-subdir-del elt))
|
|
2072 ;; else move to new position
|
|
2073 (dired-insert-subdir-newpos dirname)))
|
|
2074 (setq opoint-max (point-max))
|
|
2075 (condition-case nil
|
|
2076 (dired-insert-subdir-doupdate
|
|
2077 dirname (dired-insert-subdir-doinsert dir-or-list switches)
|
|
2078 switches elt mark-alist)
|
|
2079 (quit ; watch out for aborted inserts
|
|
2080 (and (= opoint-max (point-max))
|
|
2081 (null elt)
|
|
2082 (= (preceding-char) ?\n)
|
|
2083 (delete-char -1))
|
|
2084 (signal 'quit nil))))
|
|
2085 (or no-posn (dired-initial-position dirname))))
|
|
2086
|
|
2087 (defun dired-do-insert-subdir ()
|
|
2088 "Insert all marked subdirectories in situ that are not yet inserted.
|
|
2089 Non-directories are silently ignored."
|
|
2090 (interactive)
|
|
2091 (let ((files (or (dired-get-marked-files)
|
|
2092 (error "No files marked."))))
|
|
2093 (while files
|
|
2094 (if (file-directory-p (car files))
|
|
2095 (save-excursion (dired-maybe-insert-subdir (car files))))
|
|
2096 (setq files (cdr files)))))
|
|
2097
|
|
2098 ;;; Utilities for inserting subdirectories
|
|
2099
|
|
2100 (defun dired-insert-subdir-newpos (new-dir)
|
|
2101 ;; Find pos for new subdir, according to tree order.
|
|
2102 (let ((alist dired-subdir-alist) elt dir new-pos)
|
|
2103 (while alist
|
|
2104 (setq elt (car alist)
|
|
2105 alist (cdr alist)
|
|
2106 dir (car elt))
|
|
2107 (if (dired-tree-lessp dir new-dir)
|
|
2108 ;; Insert NEW-DIR after DIR
|
|
2109 (setq new-pos (dired-get-subdir-max elt)
|
|
2110 alist nil)))
|
|
2111 (goto-char new-pos))
|
|
2112 (insert "\n")
|
|
2113 (point))
|
|
2114
|
|
2115 (defun dired-insert-subdir-del (element)
|
|
2116 ;; Erase an already present subdir (given by ELEMENT) from buffer.
|
|
2117 ;; Move to that buffer position. Return a mark-alist.
|
|
2118 (let ((begin-marker (dired-get-subdir-min element)))
|
|
2119 (goto-char begin-marker)
|
|
2120 ;; Are at beginning of subdir (and inside it!). Now determine its end:
|
|
2121 (goto-char (dired-subdir-max))
|
|
2122 (prog1
|
|
2123 (dired-remember-marks begin-marker (point))
|
|
2124 (delete-region begin-marker (point)))))
|
|
2125
|
|
2126 (defun dired-insert-subdir-doinsert (dir-or-list switches)
|
|
2127 ;; Insert ls output after point and put point on the correct
|
|
2128 ;; position for the subdir alist.
|
|
2129 ;; Return the boundary of the inserted text (as list of BEG and END).
|
|
2130 ;; SWITCHES should be a non-nil list.
|
|
2131 (let ((begin (point))
|
|
2132 (dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
|
|
2133 end)
|
|
2134 (message "Reading directory %s..." dirname)
|
|
2135 (if (string-equal dirname (car (car (reverse dired-subdir-alist))))
|
|
2136 ;; top level directory may contain wildcards:
|
|
2137 (let ((dired-internal-switches switches))
|
|
2138 (dired-readin-insert dired-directory
|
|
2139 (null (file-directory-p dired-directory))))
|
|
2140 (let ((switches (dired-make-switches-string switches))
|
|
2141 (insert-directory-program dired-ls-program))
|
|
2142 (if (consp dir-or-list)
|
|
2143 (progn
|
|
2144 (insert "list wildcard\n")
|
|
2145 (mapcar
|
|
2146 (function
|
|
2147 (lambda (x)
|
|
2148 (insert-directory x switches t)))
|
|
2149 (cdr dir-or-list)))
|
|
2150 (insert-directory dirname switches nil t))))
|
|
2151 (message "Reading directory %s...done" dirname)
|
|
2152 (setq end (point-marker))
|
|
2153 (dired-indent-listing begin end)
|
|
2154 (dired-insert-set-properties begin end)
|
|
2155 ;; call dired-insert-headerline afterwards, as under VMS dired-ls
|
|
2156 ;; does insert the headerline itself and the insert function just
|
|
2157 ;; moves point.
|
|
2158 ;; Need a marker for END as this inserts text.
|
|
2159 (goto-char begin)
|
|
2160 (dired-insert-headerline dirname)
|
|
2161 ;; point is now like in dired-build-subdir-alist
|
|
2162 (prog1
|
|
2163 (list begin (marker-position end))
|
|
2164 (set-marker end nil))))
|
|
2165
|
|
2166 (defun dired-insert-subdir-doupdate (dirname beg-end switches elt mark-alist)
|
|
2167 ;; Point is at the correct subdir alist position for ELT,
|
|
2168 ;; BEG-END is the subdir-region (as list of begin and end).
|
|
2169 ;; SWITCHES must be a non-nil list.
|
|
2170 (if (memq ?R switches)
|
|
2171 ;; This will remove ?R from switches on purpose.
|
|
2172 (let ((dired-internal-switches (delq ?R switches)))
|
|
2173 (dired-build-subdir-alist))
|
|
2174 (if elt
|
|
2175 (progn
|
|
2176 (set-marker (dired-get-subdir-min elt) (point-marker))
|
|
2177 (setcar (nthcdr 3 elt) switches)
|
|
2178 (if dired-verify-modtimes
|
|
2179 (dired-set-file-modtime dirname dired-subdir-alist)))
|
|
2180 (dired-alist-add dirname (point-marker) dired-omit-files switches)))
|
|
2181 (save-excursion
|
|
2182 (let ((begin (nth 0 beg-end))
|
|
2183 (end (nth 1 beg-end)))
|
|
2184 (goto-char begin)
|
|
2185 (save-restriction
|
|
2186 (narrow-to-region begin end)
|
|
2187 ;; hook may add or delete lines, but the subdir boundary
|
|
2188 ;; marker floats
|
|
2189 (run-hooks 'dired-after-readin-hook)
|
|
2190 (if mark-alist (dired-mark-remembered mark-alist))
|
|
2191 (dired-do-hide dired-kill-marker-char)
|
|
2192 (if (if elt (nth 2 elt) dired-omit-files)
|
|
2193 (dired-omit-expunge nil t))))))
|
|
2194
|
|
2195
|
|
2196 ;;;; --------------------------------------------------------------
|
|
2197 ;;;; Dired motion commands -- moving around in the dired buffer.
|
|
2198 ;;;; --------------------------------------------------------------
|
|
2199
|
|
2200 (defun dired-next-line (arg)
|
|
2201 "Move down lines then position at filename.
|
|
2202 Optional prefix ARG says how many lines to move; default is one line."
|
|
2203 (interactive "p")
|
|
2204 (condition-case err
|
|
2205 (next-line arg)
|
|
2206 (error
|
|
2207 (if (eobp)
|
|
2208 (error "End of buffer")
|
|
2209 (error "%s" err))))
|
|
2210 (dired-move-to-filename)
|
|
2211 (dired-update-mode-line))
|
|
2212
|
|
2213 (defun dired-previous-line (arg)
|
|
2214 "Move up lines then position at filename.
|
|
2215 Optional prefix ARG says how many lines to move; default is one line."
|
|
2216 (interactive "p")
|
|
2217 (previous-line arg)
|
|
2218 (dired-move-to-filename)
|
|
2219 (dired-update-mode-line))
|
|
2220
|
|
2221 (defun dired-scroll-up (arg)
|
|
2222 "Dired version of scroll up.
|
|
2223 Scroll text of current window upward ARG lines; or near full screen if no ARG.
|
|
2224 When calling from a program, supply a number as argument or nil."
|
|
2225 (interactive "P")
|
|
2226 (scroll-up arg)
|
|
2227 (dired-move-to-filename)
|
|
2228 (dired-update-mode-line))
|
|
2229
|
|
2230 (defun dired-scroll-down (arg)
|
|
2231 "Dired version of scroll-down.
|
|
2232 Scroll text of current window down ARG lines; or near full screen if no ARG.
|
|
2233 When calling from a program, supply a number as argument or nil."
|
|
2234 (interactive "P")
|
|
2235 (scroll-down arg)
|
|
2236 (dired-move-to-filename)
|
|
2237 (dired-update-mode-line))
|
|
2238
|
|
2239 (defun dired-beginning-of-buffer (arg)
|
|
2240 "Dired version of `beginning of buffer'."
|
|
2241 (interactive "P")
|
|
2242 (beginning-of-buffer arg)
|
|
2243 (dired-update-mode-line))
|
|
2244
|
|
2245 (defun dired-end-of-buffer (arg)
|
|
2246 "Dired version of `end-of-buffer'."
|
|
2247 (interactive "P")
|
|
2248 (end-of-buffer arg)
|
|
2249 (while (not (or (dired-move-to-filename) (dired-get-subdir) (bobp)))
|
|
2250 (forward-line -1))
|
|
2251 (dired-update-mode-line t))
|
|
2252
|
|
2253 (defun dired-next-dirline (arg &optional opoint)
|
|
2254 "Goto ARG'th next directory file line."
|
|
2255 (interactive "p")
|
|
2256 (if dired-re-dir
|
|
2257 (progn
|
|
2258 (dired-check-ls-l)
|
|
2259 (or opoint (setq opoint (point)))
|
|
2260 (if (if (> arg 0)
|
|
2261 (re-search-forward dired-re-dir nil t arg)
|
|
2262 (beginning-of-line)
|
|
2263 (re-search-backward dired-re-dir nil t (- arg)))
|
|
2264 (progn
|
|
2265 (dired-move-to-filename) ; user may type `i' or `f'
|
|
2266 (dired-update-mode-line))
|
|
2267 (goto-char opoint)
|
|
2268 (error "No more subdirectories")))))
|
|
2269
|
|
2270 (defun dired-prev-dirline (arg)
|
|
2271 "Goto ARG'th previous directory file line."
|
|
2272 (interactive "p")
|
|
2273 (dired-next-dirline (- arg)))
|
|
2274
|
|
2275 (defun dired-next-marked-file (arg &optional wrap opoint)
|
|
2276 "Move to the next marked file, wrapping around the end of the buffer."
|
|
2277 (interactive "p\np")
|
|
2278 (or opoint (setq opoint (point))) ; return to where interactively started
|
|
2279 (if (if (> arg 0)
|
|
2280 (re-search-forward dired-re-mark nil t arg)
|
|
2281 (beginning-of-line)
|
|
2282 (re-search-backward dired-re-mark nil t (- arg)))
|
|
2283 (dired-move-to-filename)
|
|
2284 (if (null wrap)
|
|
2285 (progn
|
|
2286 (goto-char opoint)
|
|
2287 (error "No next marked file"))
|
|
2288 (message "(Wraparound for next marked file)")
|
|
2289 (goto-char (if (> arg 0) (point-min) (point-max)))
|
|
2290 (dired-next-marked-file arg nil opoint)))
|
|
2291 (dired-update-mode-line))
|
|
2292
|
|
2293 (defun dired-prev-marked-file (arg &optional wrap)
|
|
2294 "Move to the previous marked file, wrapping around the end of the buffer."
|
|
2295 (interactive "p\np")
|
|
2296 (dired-next-marked-file (- arg) wrap)
|
|
2297 (dired-update-mode-line))
|
|
2298
|
|
2299 (defun dired-goto-file (file)
|
|
2300 "Goto file line of FILE in this dired buffer."
|
|
2301 ;; Return value of point on success, else nil.
|
|
2302 ;; FILE must be an absolute pathname.
|
|
2303 ;; Loses if FILE contains control chars like "\007" for which ls
|
|
2304 ;; either inserts "?" or "\\007" into the buffer, so we won't find
|
|
2305 ;; it in the buffer.
|
|
2306 (interactive
|
|
2307 (prog1 ; let push-mark display its message
|
|
2308 (list
|
|
2309 (let* ((dired-completer-buffer (current-buffer))
|
|
2310 (dired-completer-switches dired-internal-switches)
|
|
2311 (stack (reverse
|
|
2312 (mapcar (function
|
|
2313 (lambda (x)
|
|
2314 (dired-abbreviate-file-name (car x))))
|
|
2315 dired-subdir-alist)))
|
|
2316 (initial (car stack))
|
|
2317 (dired-goto-file-history (cdr stack))
|
|
2318 dired-completer-cache)
|
|
2319 (expand-file-name
|
|
2320 (dired-completing-read "Goto file: "
|
|
2321 'dired-goto-file-completer
|
|
2322 nil t initial 'dired-goto-file-history))))
|
|
2323 (push-mark)))
|
|
2324 (setq file (directory-file-name file)) ; does no harm if no directory
|
|
2325 (let (found case-fold-search)
|
|
2326 (save-excursion
|
|
2327 (if (dired-goto-subdir (or (file-name-directory file)
|
|
2328 (error "Need absolute pathname for %s"
|
|
2329 file)))
|
|
2330 (let* ((base (file-name-nondirectory file))
|
|
2331 ;; filenames are preceded by SPC, this makes
|
|
2332 ;; the search faster (e.g. for the filename "-"!).
|
|
2333 (search (concat " " (dired-make-filename-string base t)))
|
|
2334 (boundary (dired-subdir-max))
|
|
2335 fn)
|
|
2336 (while (and (not found) (search-forward search boundary 'move))
|
|
2337 ;; Match could have BASE just as initial substring or
|
|
2338 ;; or in permission bits or date or
|
|
2339 ;; not be a proper filename at all:
|
|
2340 (if (and (setq fn (dired-get-filename 'no-dir t))
|
|
2341 (string-equal fn base))
|
|
2342 ;; Must move to filename since an (actually
|
|
2343 ;; correct) match could have been elsewhere on the
|
|
2344 ;; line (e.g. "-" would match somewhere in the
|
|
2345 ;; permission bits).
|
|
2346 (setq found (dired-move-to-filename)))))))
|
|
2347 (and found
|
|
2348 ;; return value of point (i.e., FOUND):
|
|
2349 (prog1
|
|
2350 (goto-char found)
|
|
2351 (dired-update-mode-line)))))
|
|
2352
|
|
2353 ;;; Moving by subdirectories
|
|
2354
|
|
2355 (defun dired-up-directory (arg)
|
|
2356 "Move to the ARG'th (prefix arg) parent directory of current directory.
|
|
2357 Always stays within the current tree dired buffer. Will insert new
|
|
2358 subdirectories if necessary."
|
|
2359 (interactive "p")
|
|
2360 (if (< arg 0) (error "Can't go up a negative number of directories!"))
|
|
2361 (or (zerop arg)
|
|
2362 (let* ((dir (dired-current-directory))
|
|
2363 (n arg)
|
|
2364 (up dir))
|
|
2365 (while (> n 0)
|
|
2366 (setq up (file-name-directory (directory-file-name up))
|
|
2367 n (1- n)))
|
|
2368 (if (and (< (length up) (length dired-directory))
|
|
2369 (dired-in-this-tree dired-directory up))
|
|
2370 (if (or (memq 'create-top-dir dired-no-confirm)
|
|
2371 (y-or-n-p
|
|
2372 (format "Insert new top dir %s and rename buffer? "
|
|
2373 (dired-abbreviate-file-name up))))
|
|
2374 (let ((newname (let (buff)
|
|
2375 (unwind-protect
|
|
2376 (buffer-name
|
|
2377 (setq buff
|
|
2378 (create-file-buffer
|
|
2379 (directory-file-name up))))
|
|
2380 (kill-buffer buff))))
|
|
2381 (buffer-read-only nil))
|
|
2382 (push-mark)
|
|
2383 (widen)
|
|
2384 (goto-char (point-min))
|
|
2385 (insert-before-markers "\n")
|
|
2386 (forward-char -1)
|
|
2387 (dired-insert-subdir-doupdate
|
|
2388 up (dired-insert-subdir-doinsert up dired-internal-switches)
|
|
2389 dired-internal-switches nil nil)
|
|
2390 (dired-initial-position up)
|
|
2391 (rename-buffer newname)
|
|
2392 (dired-unadvertise default-directory)
|
|
2393 (setq default-directory up
|
|
2394 dired-directory up)
|
|
2395 (dired-advertise)))
|
|
2396 (dired-maybe-insert-subdir up)))))
|
|
2397
|
|
2398 (defun dired-down-directory ()
|
|
2399 "Go down in the dired tree.
|
|
2400 Moves to the first subdirectory of the current directory, which exists in
|
|
2401 the dired buffer. Does not take a prefix argument."
|
|
2402 ;; What would a prefix mean here?
|
|
2403 (interactive)
|
|
2404 (let ((dir (dired-current-directory)) ; has slash
|
|
2405 (rest (reverse dired-subdir-alist))
|
|
2406 pos elt)
|
|
2407 (while rest
|
|
2408 (setq elt (car rest))
|
|
2409 (if (dired-in-this-tree (directory-file-name (car elt)) dir)
|
|
2410 (setq rest nil
|
|
2411 pos (dired-goto-subdir (car elt)))
|
|
2412 (setq rest (cdr rest))))
|
|
2413 (prog1
|
|
2414 (if pos
|
|
2415 (progn
|
|
2416 (push-mark)
|
|
2417 (goto-char pos))
|
|
2418 (error "At the bottom"))
|
|
2419 (dired-update-mode-line t))))
|
|
2420
|
|
2421 (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
|
|
2422 "Go to next subdirectory, regardless of level."
|
|
2423 ;; Use 0 arg to go to this directory's header line.
|
|
2424 ;; NO-SKIP prevents moving to end of header line, returning whatever
|
|
2425 ;; position was found in dired-subdir-alist.
|
|
2426 (interactive "p")
|
|
2427 (let ((this-dir (dired-current-directory))
|
|
2428 pos index)
|
|
2429 ;; nth with negative arg does not return nil but the first element
|
|
2430 (setq index (- (length dired-subdir-alist)
|
|
2431 (length (memq (assoc this-dir dired-subdir-alist)
|
|
2432 dired-subdir-alist))
|
|
2433 arg))
|
|
2434 (setq pos (if (>= index 0)
|
|
2435 (dired-get-subdir-min (nth index dired-subdir-alist))))
|
|
2436 (if pos
|
|
2437 (if no-skip
|
|
2438 (goto-char pos)
|
|
2439 (goto-char pos)
|
|
2440 (skip-chars-forward "^\r\n")
|
|
2441 (if (= (following-char) ?\r)
|
|
2442 (skip-chars-backward "." (- (point) 3)))
|
|
2443 (dired-update-mode-line t)
|
|
2444 (point))
|
|
2445 (if no-error-if-not-found
|
|
2446 nil ; return nil if not found
|
|
2447 (error "%s directory" (if (> arg 0) "Last" "First"))))))
|
|
2448
|
|
2449 (defun dired-prev-subdir (arg &optional no-error-if-not-found no-skip)
|
|
2450 "Go to previous subdirectory, regardless of level.
|
|
2451 When called interactively and not on a subdir line, go to this subdir's line."
|
|
2452 (interactive
|
|
2453 (list (if current-prefix-arg
|
|
2454 (prefix-numeric-value current-prefix-arg)
|
|
2455 ;; if on subdir start already, don't stay there!
|
|
2456 (if (dired-get-subdir) 1 0))))
|
|
2457 (dired-next-subdir (- arg) no-error-if-not-found no-skip))
|
|
2458
|
|
2459 (defun dired-goto-subdir (dir)
|
|
2460 "Goto end of header line of DIR in this dired buffer.
|
|
2461 Return value of point on success, otherwise return nil.
|
|
2462 The next char is either \\n, or \\r if DIR is hidden."
|
|
2463 (interactive
|
|
2464 (prog1 ; let push-mark display its message
|
|
2465 (list
|
|
2466 (let* ((table (mapcar
|
|
2467 (function
|
|
2468 (lambda (x)
|
|
2469 (list (dired-abbreviate-file-name
|
|
2470 (car x)))))
|
|
2471 dired-subdir-alist))
|
|
2472 (stack (reverse (mapcar 'car table)))
|
|
2473 (initial (car stack))
|
|
2474 (dired-goto-file-history (cdr stack)))
|
|
2475 (expand-file-name
|
|
2476 (dired-completing-read "Goto subdirectory " table nil t
|
|
2477 initial 'dired-goto-file-history))))
|
|
2478 (push-mark)))
|
|
2479 (setq dir (file-name-as-directory dir))
|
|
2480 (let ((elt (assoc dir dired-subdir-alist)))
|
|
2481 (and elt
|
|
2482 ;; need to make sure that we get where we're going.
|
|
2483 ;; beware: narrowing might be in effect
|
|
2484 (eq (goto-char (dired-get-subdir-min elt)) (point))
|
|
2485 (progn
|
|
2486 ;; dired-subdir-hidden-p and dired-add-entry depend on point being
|
|
2487 ;; at either \n or looking-at ...\r after this function succeeds.
|
|
2488 (skip-chars-forward "^\r\n")
|
|
2489 (if (= (preceding-char) ?.)
|
|
2490 (skip-chars-backward "." (- (point) 3)))
|
|
2491 (if (interactive-p) (dired-update-mode-line))
|
|
2492 (point)))))
|
|
2493
|
|
2494 ;;; Internals for motion commands
|
|
2495
|
|
2496 (defun dired-update-mode-line (&optional force)
|
|
2497 "Updates the mode line in dired according to the position of the point.
|
|
2498 Normally this uses a cache of the boundaries of the current subdirectory,
|
|
2499 but if the optional argument FORCE is non-nil, then modeline is always
|
|
2500 updated and the cache is recomputed."
|
|
2501 (if (or force
|
|
2502 (>= (point) dired-curr-subdir-max)
|
|
2503 (< (point) dired-curr-subdir-min))
|
|
2504 (let ((alist dired-subdir-alist)
|
|
2505 min max)
|
|
2506 (while (and alist (< (point)
|
|
2507 (setq min (dired-get-subdir-min (car alist)))))
|
|
2508 (setq alist (cdr alist)
|
|
2509 max min))
|
|
2510 (setq dired-curr-subdir-max (or max (point-max-marker))
|
|
2511 dired-curr-subdir-min (or min (point-min-marker))
|
|
2512 dired-subdir-omit (nth 2 (car alist)))
|
|
2513 (dired-sort-set-modeline (nth 3 (car alist))))))
|
|
2514
|
|
2515 (defun dired-manual-move-to-filename (&optional raise-error bol eol)
|
|
2516 "In dired, move to first char of filename on this line.
|
|
2517 Returns position (point) or nil if no filename on this line."
|
|
2518 ;; This is the UNIX version.
|
|
2519 ;; have to be careful that we don't move to omitted files
|
|
2520 (let (case-fold-search)
|
|
2521
|
|
2522 (or eol (setq eol (save-excursion (skip-chars-forward "^\r\n") (point))))
|
|
2523 (or bol (setq bol (progn (skip-chars-backward "^\r\n") (point))))
|
|
2524
|
|
2525 (if (or (memq ?l dired-internal-switches)
|
|
2526 (memq ?g dired-internal-switches))
|
|
2527 (if (and
|
|
2528 (> (- eol bol) 17) ; a valid file line must have at least
|
|
2529 ; 17 chars. 2 leading, 10 perms,
|
|
2530 ; separator, node #, separator, owner,
|
|
2531 ; separator
|
|
2532 (goto-char (+ bol 17))
|
|
2533 (re-search-forward dired-re-month-and-time eol t))
|
|
2534 (point)
|
|
2535 (goto-char bol)
|
|
2536 (if raise-error
|
|
2537 (error "No file on this line")
|
|
2538 nil))
|
|
2539 ;; else ls switches don't contain -l.
|
|
2540 ;; Note that even if we make dired-move-to-filename and
|
|
2541 ;; dired-move-to-end-of-filename (and thus dired-get-filename)
|
|
2542 ;; work, all commands that gleaned information from the permission
|
|
2543 ;; bits (like dired-mark-directories) will cease to work properly.
|
|
2544 (if (= bol eol)
|
|
2545 (if raise-error
|
|
2546 (error "No file on this line")
|
|
2547 nil)
|
|
2548 ;; skip marker, if any
|
|
2549 (goto-char bol)
|
|
2550 (forward-char))
|
|
2551 ;; If we not going to use the l switch, and use nstd listings,
|
|
2552 ;; then we must bomb on files starting with spaces.
|
|
2553 (skip-chars-forward " \t")
|
|
2554 (point))))
|
|
2555
|
|
2556 (defun dired-manual-move-to-end-of-filename (&optional no-error bol eol)
|
|
2557 ;; Assumes point is at beginning of filename,
|
|
2558 ;; thus the rwx bit re-search-backward below will succeed in *this*
|
|
2559 ;; line if at all. So, it should be called only after
|
|
2560 ;; (dired-move-to-filename t).
|
|
2561 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
|
|
2562 ;; This is the UNIX version.
|
|
2563 (let ((bof (point))
|
|
2564 file-type modes-start case-fold-search)
|
|
2565 (or eol (setq eol (save-excursion (skip-chars-forward "^\r\n") (point))))
|
|
2566 (or bol (setq bol (save-excursion (skip-chars-backward "^\r\n") (point))))
|
|
2567 (and
|
|
2568 (null no-error)
|
|
2569 selective-display
|
|
2570 (eq (char-after (1- bol)) ?\r)
|
|
2571 (cond
|
|
2572 ((dired-subdir-hidden-p (dired-current-directory))
|
|
2573 (error
|
|
2574 (substitute-command-keys
|
|
2575 "File line is hidden. Type \\[dired-hide-subdir] to unhide.")))
|
|
2576 ((error
|
|
2577 (substitute-command-keys
|
|
2578 "File line is omitted. Type \\[dired-omit-toggle] to un-omit.")))))
|
|
2579 (if (or (memq ?l dired-internal-switches)
|
|
2580 (memq ?g dired-internal-switches))
|
|
2581 (if (save-excursion
|
|
2582 (goto-char bol)
|
|
2583 (re-search-forward
|
|
2584 "[^ ][-r][-w][^ ][-r][-w][^ ][-r][-w][^ ][-+ 0-9+]"
|
|
2585 bof t))
|
|
2586 (progn
|
|
2587 (setq modes-start (match-beginning 0)
|
|
2588 file-type (char-after modes-start))
|
|
2589 ;; Move point to end of name:
|
|
2590 (if (eq file-type ?l) ; symlink
|
|
2591 (progn
|
|
2592 (if (search-forward " -> " eol t)
|
|
2593 (goto-char (match-beginning 0))
|
|
2594 (goto-char eol))
|
|
2595 (and dired-ls-F-marks-symlinks
|
|
2596 (eq (preceding-char) ?@) ; link really marked?
|
|
2597 (memq ?F dired-internal-switches)
|
|
2598 (forward-char -1))
|
|
2599 (point))
|
|
2600 ;; else not a symbolic link
|
|
2601 (goto-char eol)
|
|
2602 ;; ls -lF marks dirs, sockets and executables with exactly
|
|
2603 ;; one trailing character. -F may not actually be honored,
|
|
2604 ;; e.g. by an FTP ls in efs
|
|
2605 (and
|
|
2606 (memq ?F dired-internal-switches)
|
|
2607 (let ((char (preceding-char)))
|
|
2608 (or (and (eq char ?*) (or
|
|
2609 (memq
|
|
2610 (char-after (+ modes-start 3))
|
|
2611 '(?x ?s ?t))
|
|
2612 (memq
|
|
2613 (char-after (+ modes-start 6))
|
|
2614 '(?x ?s ?t))
|
|
2615 (memq
|
|
2616 (char-after (+ modes-start 9))
|
|
2617 '(?x ?s ?t))))
|
|
2618 (and (eq char ?=) (eq file-type ?s))))
|
|
2619 (forward-char -1))
|
|
2620 ;; Skip back over /'s unconditionally. It's not a valid
|
|
2621 ;; file name character.
|
|
2622 (skip-chars-backward "/")
|
|
2623 (point)))
|
|
2624 (and (null no-error)
|
|
2625 (error "No file on this line")))
|
|
2626
|
|
2627 ;; A brief listing
|
|
2628 (if (eq (point) eol)
|
|
2629 (and (null no-error)
|
|
2630 (error "No file on this line"))
|
|
2631 (goto-char eol)
|
|
2632 (if (and (memq (preceding-char) '(?@ ?* ?=))
|
|
2633 (memq ?F dired-internal-switches))
|
|
2634 ;; A guess, since without a long listing, we can't be sure.
|
|
2635 (forward-char -1))
|
|
2636 (skip-chars-backward "/")
|
|
2637 (point)))))
|
|
2638
|
|
2639 (defun dired-goto-next-nontrivial-file ()
|
|
2640 ;; Position point on first nontrivial file after point.
|
|
2641 ;; Does not move into the next sudir.
|
|
2642 ;; If point is on a file line, moves to that file.
|
|
2643 ;; This does not move to omitted files.
|
|
2644 (skip-chars-backward "^\n\r")
|
|
2645 (if (= (preceding-char) ?\r)
|
|
2646 (forward-line 1))
|
|
2647 (let ((max (dired-subdir-max))
|
|
2648 file)
|
|
2649 (while (and (or (not (setq file (dired-get-filename 'no-dir t)))
|
|
2650 (string-match dired-trivial-filenames file))
|
|
2651 (< (point) max))
|
|
2652 (forward-line 1)))
|
|
2653 (dired-move-to-filename))
|
|
2654
|
|
2655 (defun dired-goto-next-file ()
|
|
2656 ;; Doesn't move out of current subdir. Does go to omitted files.
|
|
2657 ;; Returns the starting position of the file, or nil if none found.
|
|
2658 (let ((max (dired-subdir-max))
|
|
2659 found)
|
|
2660 (while (and (null (setq found (dired-move-to-filename))) (< (point) max))
|
|
2661 (skip-chars-forward "^\n\r")
|
|
2662 (forward-char 1))
|
|
2663 found))
|
|
2664
|
|
2665 ;; fluid vars used by dired-goto-file-completer
|
|
2666 (defvar dired-completer-buffer nil)
|
|
2667 (defvar dired-completer-switches nil)
|
|
2668 (defvar dired-completer-cache nil)
|
|
2669
|
|
2670 (defun dired-goto-file-completer (string pred action)
|
|
2671 (save-excursion
|
|
2672 (set-buffer dired-completer-buffer)
|
|
2673 (let* ((saved-md (match-data))
|
|
2674 (file (file-name-nondirectory string))
|
|
2675 (dir (file-name-directory string))
|
|
2676 (xstring (expand-file-name string))
|
|
2677 (xdir (file-name-directory xstring))
|
|
2678 (exact (dired-goto-file xstring)))
|
|
2679 (unwind-protect
|
|
2680 (if (dired-goto-subdir xdir)
|
|
2681 (let ((table (cdr (assoc xdir dired-completer-cache)))
|
|
2682 fn result max)
|
|
2683 (or table
|
|
2684 (progn
|
|
2685 (setq table (make-vector 37 0))
|
|
2686 (mapcar (function
|
|
2687 (lambda (ent)
|
|
2688 (setq ent (directory-file-name
|
|
2689 (car ent)))
|
|
2690 (if (string-equal
|
|
2691 (file-name-directory ent) xdir)
|
|
2692 (intern
|
|
2693 (concat
|
|
2694 (file-name-nondirectory ent) "/")
|
|
2695 table))))
|
|
2696 dired-subdir-alist)
|
|
2697 (or (looking-at "\\.\\.\\.\n\r")
|
|
2698 (progn
|
|
2699 (setq max (dired-subdir-max))
|
|
2700 (while (and
|
|
2701 (< (point) max)
|
|
2702 (not
|
|
2703 (setq fn
|
|
2704 (dired-get-filename 'no-dir t))))
|
|
2705 (forward-line 1))
|
|
2706 (if fn
|
|
2707 (progn
|
|
2708 (or (intern-soft (concat fn "/") table)
|
|
2709 (intern fn table))
|
|
2710 (forward-line 1)
|
|
2711 (while (setq fn
|
|
2712 (dired-get-filename 'no-dir t))
|
|
2713 (or (intern-soft (concat fn "/") table)
|
|
2714 (intern fn table))
|
|
2715 (forward-line 1))))))
|
|
2716 (setq dired-completer-cache (cons
|
|
2717 (cons xdir table)
|
|
2718 dired-completer-cache))))
|
|
2719 (cond
|
|
2720 ((null action)
|
|
2721 (setq result (try-completion file table))
|
|
2722 (if exact
|
|
2723 (if (stringp result)
|
|
2724 string
|
|
2725 t)
|
|
2726 (if (stringp result)
|
|
2727 (concat dir result)
|
|
2728 result)))
|
|
2729 ((eq action t)
|
|
2730 (setq result (all-completions file table))
|
|
2731 (if exact (cons "." result) result))
|
|
2732 ((eq 'lambda action)
|
|
2733 (and (or exact (intern-soft file table)))))))
|
|
2734 (store-match-data saved-md)))))
|
|
2735
|
|
2736 (defun dired-really-goto-file (file)
|
|
2737 ;; Goes to a file, even if it needs to insert it parent directory.
|
|
2738 (or (dired-goto-file file)
|
|
2739 (progn ; refresh and try again
|
|
2740 (dired-insert-subdir (file-name-directory file))
|
|
2741 (dired-goto-file file))))
|
|
2742
|
|
2743 (defun dired-between-files ()
|
|
2744 ;; Point must be at beginning of line
|
|
2745 (save-excursion (not (dired-move-to-filename nil (point)))))
|
|
2746
|
|
2747 (defun dired-repeat-over-lines (arg function)
|
|
2748 ;; This version skips non-file lines.
|
|
2749 ;; Skips file lines hidden with selective display.
|
|
2750 ;; BACKWARDS means move backwards after each action. This is not the same
|
|
2751 ;; as a negative arg, as that skips the current line.
|
|
2752 (beginning-of-line)
|
|
2753 (let* ((advance (cond ((> arg 0) 1) ((< arg 0) -1) (t nil)))
|
|
2754 (check-fun (if (eq advance 1) 'eobp 'bobp))
|
|
2755 (n (if (< arg 0) (- arg) arg))
|
|
2756 (wall (funcall check-fun))
|
|
2757 (done wall))
|
|
2758 (while (not done)
|
|
2759 (if advance
|
|
2760 (progn
|
|
2761 (while (not (or (save-excursion (dired-move-to-filename))
|
|
2762 (setq wall (funcall check-fun))))
|
|
2763 (forward-line advance))
|
|
2764 (or wall
|
|
2765 (progn
|
|
2766 (save-excursion (funcall function))
|
|
2767 (forward-line advance)
|
|
2768 (while (not (or (save-excursion (dired-move-to-filename))
|
|
2769 (setq wall (funcall check-fun))))
|
|
2770 (forward-line advance))
|
|
2771 (setq done (or (zerop (setq n (1- n))) wall)))))
|
|
2772 (if (save-excursion (dired-move-to-filename))
|
|
2773 (save-excursion (funcall function)))
|
|
2774 (setq done t))))
|
|
2775 (dired-move-to-filename)
|
|
2776 ;; Note that if possible the point has now been moved to the beginning of
|
|
2777 ;; the file name.
|
|
2778 (dired-update-mode-line))
|
|
2779
|
|
2780
|
|
2781 ;;;; ----------------------------------------------------------------
|
|
2782 ;;;; Miscellaneous dired commands
|
|
2783 ;;;; ----------------------------------------------------------------
|
|
2784
|
|
2785 (defun dired-quit ()
|
|
2786 "Bury the current dired buffer."
|
|
2787 (interactive)
|
|
2788 (bury-buffer))
|
|
2789
|
|
2790 (defun dired-undo ()
|
|
2791 "Undo in a dired buffer.
|
|
2792 This doesn't recover lost files, it is just normal undo with temporarily
|
|
2793 writeable buffer. You can use it to recover marks, killed lines or subdirs."
|
|
2794 (interactive)
|
|
2795 (let ((lines (count-lines (point-min) (point-max)))
|
|
2796 buffer-read-only)
|
|
2797 (undo)
|
|
2798 ;; reset dired-subdir-alist, if a dir may have been affected
|
|
2799 ;; Is there a better way to guess this?
|
|
2800 (setq lines (- (count-lines (point-min) (point-max)) lines))
|
|
2801 (if (or (>= lines 2) (<= lines -2))
|
|
2802 (dired-build-subdir-alist)))
|
|
2803 (dired-update-mode-line-modified t)
|
|
2804 (dired-update-mode-line t))
|
|
2805
|
|
2806
|
|
2807 ;;;; --------------------------------------------------------
|
|
2808 ;;;; Immediate actions on files: visiting, viewing, etc.
|
|
2809 ;;;; --------------------------------------------------------
|
|
2810
|
|
2811 (defun dired-find-file ()
|
|
2812 "In dired, visit the file or directory named on this line."
|
|
2813 (interactive)
|
|
2814 (find-file (dired-get-filename)))
|
|
2815
|
|
2816 (defun dired-view-file ()
|
|
2817 "In dired, examine a file in view mode, returning to dired when done.
|
|
2818 When file is a directory, show it in this buffer if it is inserted;
|
|
2819 otherwise, display it in another buffer."
|
|
2820 (interactive)
|
|
2821 (let ((file (dired-get-filename)))
|
|
2822 (if (file-directory-p file)
|
|
2823 (or (dired-goto-subdir file)
|
|
2824 (dired file))
|
|
2825 (view-file file))))
|
|
2826
|
|
2827 (defun dired-find-file-other-window (&optional display)
|
|
2828 "In dired, visit this file or directory in another window.
|
|
2829 With a prefix, the file is displayed, but the window is not selected."
|
|
2830 (interactive "P")
|
|
2831 (if display
|
|
2832 (dired-display-file)
|
|
2833 (find-file-other-window (dired-get-filename))))
|
|
2834
|
|
2835 ;; Only for Emacs 19
|
|
2836 (defun dired-find-file-other-frame ()
|
|
2837 "In dired, visit this file or directory in another frame."
|
|
2838 (interactive)
|
|
2839 (find-file-other-frame (dired-get-filename)))
|
|
2840
|
|
2841 (defun dired-display-file ()
|
|
2842 "In dired, displays this file or directory in the other window."
|
|
2843 (interactive)
|
|
2844 (display-buffer (find-file-noselect (dired-get-filename))))
|
|
2845
|
|
2846 ;; After an idea by wurgler@zippysun.math.uakron.edu (Tom Wurgler).
|
|
2847 (defun dired-do-find-file (&optional arg)
|
|
2848 "Visit all marked files at once, and display them simultaneously.
|
|
2849 See also function `simultaneous-find-file'.
|
|
2850 If you want to keep the dired buffer displayed, type \\[split-window-vertically] first.
|
|
2851 If you want just the marked files displayed and nothing else, type \\[delete-other-windows] first."
|
|
2852 (interactive "P")
|
|
2853 (dired-simultaneous-find-file (dired-get-marked-files nil arg)))
|
|
2854
|
|
2855 (defun dired-simultaneous-find-file (file-list)
|
|
2856 "Visit all files in FILE-LIST and display them simultaneously.
|
|
2857
|
|
2858 The current window is split across all files in FILE-LIST, as evenly
|
|
2859 as possible. Remaining lines go to the bottommost window.
|
|
2860
|
|
2861 The number of files that can be displayed this way is restricted by
|
|
2862 the height of the current window and the variable `window-min-height'."
|
|
2863 ;; It is usually too clumsy to specify FILE-LIST interactively
|
|
2864 ;; unless via dired (dired-do-find-file).
|
|
2865 (let ((size (/ (window-height) (length file-list))))
|
|
2866 (or (<= window-min-height size)
|
|
2867 (error "Too many files to visit simultaneously"))
|
|
2868 (find-file (car file-list))
|
|
2869 (setq file-list (cdr file-list))
|
|
2870 (while file-list
|
|
2871 ;; Split off vertically a window of the desired size
|
|
2872 ;; The upper window will have SIZE lines. We select the lower
|
|
2873 ;; (larger) window because we want to split that again.
|
|
2874 (select-window (split-window nil size))
|
|
2875 (find-file (car file-list))
|
|
2876 (setq file-list (cdr file-list)))))
|
|
2877
|
|
2878 (defun dired-create-directory (directory)
|
|
2879 "Create a directory called DIRECTORY."
|
|
2880 (interactive
|
|
2881 (list (read-file-name "Create directory: "
|
|
2882 (dired-abbreviate-file-name
|
|
2883 (dired-current-directory)))))
|
|
2884 (let ((expanded (expand-file-name directory)))
|
|
2885 (make-directory expanded)
|
|
2886 ;; Because this function is meant to be called interactively, it moves
|
|
2887 ;; the point.
|
|
2888 (dired-goto-file expanded)))
|
|
2889
|
|
2890 (defun dired-recover-file ()
|
|
2891 "Recovers file from its autosave file.
|
|
2892 If the file is an autosave file, then recovers its associated file instead."
|
|
2893 (interactive)
|
|
2894 (let* ((file (dired-get-filename))
|
|
2895 (name (file-name-nondirectory file))
|
|
2896 (asp (auto-save-file-name-p name))
|
|
2897 (orig (and
|
|
2898 asp
|
|
2899 (if (fboundp 'auto-save-original-name)
|
|
2900 (auto-save-original-name file)
|
|
2901 (error
|
|
2902 "Need auto-save package to compute original file name."))))
|
|
2903 (buff (if asp
|
|
2904 (and orig (get-file-buffer orig))
|
|
2905 (get-file-buffer file))))
|
|
2906 (and
|
|
2907 buff
|
|
2908 (buffer-modified-p buff)
|
|
2909 (or
|
|
2910 (yes-or-no-p
|
|
2911 (format
|
|
2912 "Recover file will erase the modified buffer %s. Do it? "
|
|
2913 (buffer-name buff)))
|
|
2914 (error "Recover file aborted.")))
|
|
2915 (if asp
|
|
2916 (if orig
|
|
2917 (recover-file orig)
|
|
2918 (find-file file))
|
|
2919 (recover-file file))))
|
|
2920
|
|
2921
|
|
2922 ;;;; --------------------------------------------------------------------
|
|
2923 ;;;; Functions for extracting and manipulating file names
|
|
2924 ;;;; --------------------------------------------------------------------
|
|
2925
|
|
2926 (defun dired-make-filename-string (filename &optional reverse)
|
|
2927 ;; Translates the way that a file name appears in a buffer, to
|
|
2928 ;; how it is used in a path name. This is useful for non-unix
|
|
2929 ;; support in efs.
|
|
2930 filename)
|
|
2931
|
|
2932 (defun dired-get-filename (&optional localp no-error-if-not-filep)
|
|
2933 "In dired, return name of file mentioned on this line.
|
|
2934 Value returned normally includes the directory name.
|
|
2935 Optional arg LOCALP with value `no-dir' means don't include directory
|
|
2936 name in result. A value of t means use path name relative to
|
|
2937 `default-directory', which still may contain slashes if in a subdirectory.
|
|
2938 Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
|
|
2939 this line, otherwise an error occurs."
|
|
2940
|
|
2941 ;; Compute bol & eol once, rather than twice inside move-to-filename
|
|
2942 ;; and move-to-end-of-filename
|
|
2943 (let ((eol (save-excursion (skip-chars-forward "^\n\r") (point)))
|
|
2944 (bol (save-excursion (skip-chars-backward "^\r\n") (point)))
|
|
2945 case-fold-search file p1 p2)
|
|
2946 (save-excursion
|
|
2947 (and
|
|
2948 (setq p1 (dired-move-to-filename (not no-error-if-not-filep) bol eol))
|
|
2949 (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep bol eol))
|
|
2950 (setq file (buffer-substring p1 p2))
|
|
2951 ;; Check if ls quoted the names, and unquote them.
|
|
2952 ;; Using read to unquote is much faster than substituting
|
|
2953 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
|
|
2954 (cond ((memq ?b dired-internal-switches) ; System V ls
|
|
2955 ;; This case is about 20% slower than without -b.
|
|
2956 (setq file
|
|
2957 (read
|
|
2958 (concat "\""
|
|
2959 ;; some ls -b don't escape quotes, argh!
|
|
2960 ;; This is not needed for GNU ls, though.
|
|
2961 (or (dired-string-replace-match
|
|
2962 "\\([^\\]\\)\"" file "\\1\\\\\"")
|
|
2963 file)
|
|
2964 "\""))))
|
|
2965 ;; If you do this, update dired-compatible-switches-p
|
|
2966 ;; ((memq ?Q dired-internal-switches) ; GNU ls
|
|
2967 ;; (setq file (read file)))
|
|
2968 )))
|
|
2969 (and file
|
|
2970 (if (eq localp 'no-dir)
|
|
2971 (dired-make-filename-string file)
|
|
2972 (concat (dired-current-directory localp)
|
|
2973 (dired-make-filename-string file))))))
|
|
2974
|
|
2975 (defun dired-make-relative (file &optional dir no-error)
|
|
2976 ;; Convert FILE (an *absolute* pathname) to a pathname relative to DIR.
|
|
2977 ;; FILE must be absolute, or this function will return nonsense.
|
|
2978 ;; If FILE is not in a subdir of DIR, an error is signalled,
|
|
2979 ;; unless NO-ERROR is t. Then, ".."'s are inserted to give
|
|
2980 ;; a relative representation of FILE wrto DIR
|
|
2981 ;; eg. FILE = /vol/tex/bin/foo DIR = /vol/local/bin/
|
|
2982 ;; results in ../../tex/bin/foo
|
|
2983 ;; DIR must be expanded.
|
|
2984 ;; DIR defaults to default-directory.
|
|
2985 ;; DIR must be file-name-as-directory, as with all directory args in
|
|
2986 ;; elisp code.
|
|
2987 (or dir (setq dir (expand-file-name default-directory)))
|
|
2988 (let ((flen (length file))
|
|
2989 (dlen (length dir)))
|
|
2990 (if (and (> flen dlen)
|
|
2991 (string-equal (substring file 0 dlen) dir))
|
|
2992 (substring file dlen)
|
|
2993 ;; Need to insert ..'s
|
|
2994 (or no-error (error "%s: not in directory tree growing at %s" file dir))
|
|
2995 (if (string-equal file dir)
|
|
2996 "./"
|
|
2997 (let ((index 1)
|
|
2998 (count 0))
|
|
2999 (while (and (string-match "/" dir index)
|
|
3000 (<= (match-end 0) flen)
|
|
3001 (string-equal (substring file index (match-end 0))
|
|
3002 (substring dir index (match-end 0))))
|
|
3003 (setq index (match-end 0)))
|
|
3004 (setq file (substring file index))
|
|
3005 (if (and (/= flen index)
|
|
3006 (not (string-match "/" file))
|
|
3007 (< flen dlen)
|
|
3008 (string-equal file (substring dir index flen))
|
|
3009 (= (aref dir flen) ?/))
|
|
3010 (setq file "."
|
|
3011 count -1))
|
|
3012 ;; count how many slashes remain in dir.
|
|
3013 (while (string-match "/" dir index)
|
|
3014 (setq index (match-end 0)
|
|
3015 count (1+ count)))
|
|
3016 (apply 'concat (nconc (make-list count "../") (list file))))))))
|
|
3017
|
|
3018 ;;; Functions for manipulating file names.
|
|
3019 ;;
|
|
3020 ;; Used by file tranformers.
|
|
3021 ;; Define here rather than in dired-shell.el, as it wouldn't be
|
|
3022 ;; unreasonable to use these elsewhere.
|
|
3023
|
|
3024 (defun dired-file-name-base (fn)
|
|
3025 "Returns the base name of FN.
|
|
3026 This is the file without directory part, and extension. See the variable
|
|
3027 `dired-filename-re-ext'."
|
|
3028 (setq fn (file-name-nondirectory fn))
|
|
3029 (if (string-match dired-filename-re-ext fn 1)
|
|
3030 (substring fn 0 (match-beginning 0))
|
|
3031 fn))
|
|
3032
|
|
3033 (defun dired-file-name-extension (fn)
|
|
3034 "Returns the extension for file name FN.
|
|
3035 See the variable dired-filename-re-ext'."
|
|
3036 (setq fn (file-name-nondirectory fn))
|
|
3037 (if (string-match dired-filename-re-ext fn 1)
|
|
3038 (substring fn (match-beginning 0))
|
|
3039 ""))
|
|
3040
|
|
3041 (defun dired-file-name-sans-rcs-extension (fn)
|
|
3042 "Returns the file name FN without its RCS extension \",v\"."
|
|
3043 (setq fn (file-name-nondirectory fn))
|
|
3044 (if (string-match ",v$" fn 1)
|
|
3045 (substring fn 0 (match-beginning 0))
|
|
3046 fn))
|
|
3047
|
|
3048 (defun dired-file-name-sans-compress-extension (fn)
|
|
3049 "Returns the file name FN without the extension from compress or gzip."
|
|
3050 (setq fn (file-name-nondirectory fn))
|
|
3051 (if (string-match "\\.\\([zZ]\\|gz\\)$" fn 1)
|
|
3052 (substring fn (match-beginning 0))
|
|
3053 fn))
|
|
3054
|
|
3055
|
|
3056 ;;;; ---------------------------------------------------------------------
|
|
3057 ;;;; Working with directory trees.
|
|
3058 ;;;; ---------------------------------------------------------------------
|
|
3059 ;;;
|
|
3060 ;;; This where code for the dired-subdir-alist is.
|
|
3061
|
|
3062 ;;; Utility functions for dired-subdir-alist
|
|
3063
|
|
3064 (defun dired-normalize-subdir (dir)
|
|
3065 ;; Prepend default-directory to DIR if relative path name.
|
|
3066 ;; dired-get-filename must be able to make a valid filename from a
|
|
3067 ;; file and its directory DIR.
|
|
3068 ;; Fully expand everything.
|
|
3069 (file-name-as-directory
|
|
3070 (if (file-name-absolute-p dir)
|
|
3071 (expand-file-name dir)
|
|
3072 (expand-file-name dir (expand-file-name default-directory)))))
|
|
3073
|
|
3074 (defun dired-get-subdir ()
|
|
3075 ;;"Return the subdir name on this line, or nil if not on a headerline."
|
|
3076 ;; Look up in the alist whether this is a headerline.
|
|
3077 (save-excursion
|
|
3078 (let ((cur-dir (dired-current-directory)))
|
|
3079 (beginning-of-line) ; alist stores b-o-l positions
|
|
3080 (and (zerop (- (point)
|
|
3081 (dired-get-subdir-min (assoc cur-dir
|
|
3082 dired-subdir-alist))))
|
|
3083 cur-dir))))
|
|
3084
|
|
3085 (defun dired-get-subdir-max (elt)
|
|
3086 ;; returns subdir max.
|
|
3087 (let ((pos (- (length dired-subdir-alist)
|
|
3088 (length (member elt dired-subdir-alist)))))
|
|
3089 (if (zerop pos)
|
|
3090 (point-max)
|
|
3091 (1- (dired-get-subdir-min (nth (1- pos) dired-subdir-alist))))))
|
|
3092
|
|
3093 (defun dired-clear-alist ()
|
|
3094 ;; Set all markers in dired-subdir-alist to nil. Set the alist to nil too.
|
|
3095 (while dired-subdir-alist
|
|
3096 (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
|
|
3097 (setq dired-subdir-alist (cdr dired-subdir-alist))))
|
|
3098
|
|
3099 (defun dired-unsubdir (dir)
|
|
3100 ;; Remove DIR from the alist
|
|
3101 (setq dired-subdir-alist
|
|
3102 (delq (assoc dir dired-subdir-alist) dired-subdir-alist)))
|
|
3103
|
|
3104 (defun dired-simple-subdir-alist ()
|
|
3105 ;; Build and return `dired-subdir-alist' assuming just the top level
|
|
3106 ;; directory to be inserted. Don't parse the buffer.
|
|
3107 (setq dired-subdir-alist
|
|
3108 (list (list (expand-file-name default-directory)
|
|
3109 (point-min-marker) dired-omit-files
|
|
3110 dired-internal-switches nil)))
|
|
3111 (if dired-verify-modtimes
|
|
3112 (dired-set-file-modtime (expand-file-name default-directory)
|
|
3113 dired-subdir-alist)))
|
|
3114
|
|
3115 (defun dired-build-subdir-alist ()
|
|
3116 "Build `dired-subdir-alist' by parsing the buffer and return its new value."
|
|
3117 (interactive)
|
|
3118 (let ((o-alist dired-subdir-alist)
|
|
3119 (count 0)
|
|
3120 subdir)
|
|
3121 (dired-clear-alist)
|
|
3122 (save-excursion
|
|
3123 (goto-char (point-min))
|
|
3124 (while (re-search-forward dired-subdir-regexp nil t)
|
|
3125 (setq count (1+ count))
|
|
3126 (apply 'dired-alist-add-1
|
|
3127 (setq subdir (buffer-substring (match-beginning 2)
|
|
3128 (match-end 2)))
|
|
3129 ;; Put subdir boundary between lines.
|
|
3130 (set-marker (make-marker) (match-end 1))
|
|
3131 (let ((elt (assoc subdir o-alist)))
|
|
3132 (if elt
|
|
3133 (list (nth 2 elt) (nth 3 elt))
|
|
3134 (list dired-omit-files dired-internal-switches)))))
|
|
3135 (if (interactive-p)
|
|
3136 (message "%d director%s." count (if (= 1 count) "y" "ies")))
|
|
3137 ;; We don't need to sort it because it is in buffer order per
|
|
3138 ;; constructionem. Return new alist:
|
|
3139 ;; pointers for current-subdir may be stale
|
|
3140 dired-subdir-alist)))
|
|
3141
|
|
3142 (defun dired-alist-add (dir new-marker &optional omit switches)
|
|
3143 ;; Add new DIR at NEW-MARKER. Sort alist.
|
|
3144 (dired-alist-add-1 dir new-marker omit switches)
|
|
3145 (dired-alist-sort))
|
|
3146
|
|
3147 (defun dired-alist-add-1 (dir new-marker &optional omit switches)
|
|
3148 ;; Add new DIR at NEW-MARKER. Don't sort.
|
|
3149 (let ((dir (dired-normalize-subdir dir)))
|
|
3150 (setq dired-subdir-alist
|
|
3151 (cons (list dir new-marker omit switches nil) dired-subdir-alist))
|
|
3152 (if dired-verify-modtimes
|
|
3153 (dired-set-file-modtime dir dired-subdir-alist))))
|
|
3154
|
|
3155 (defun dired-alist-sort ()
|
|
3156 ;; Keep the alist sorted on buffer position.
|
|
3157 (setq dired-subdir-alist
|
|
3158 (sort dired-subdir-alist
|
|
3159 (function (lambda (elt1 elt2)
|
|
3160 (> (dired-get-subdir-min elt1)
|
|
3161 (dired-get-subdir-min elt2)))))))
|
|
3162
|
|
3163 ;;; Utilities for working with subdirs in the dired buffer
|
|
3164
|
|
3165 ;; This function is the heart of tree dired.
|
|
3166 ;; It is called for each retrieved filename.
|
|
3167 ;; It could stand to be faster, though it's mostly function call
|
|
3168 ;; overhead. Avoiding to funcall seems to save about 10% in
|
|
3169 ;; dired-get-filename. Make it a defsubst?
|
|
3170 (defun dired-current-directory (&optional localp)
|
|
3171 "Return the name of the subdirectory to which this line belongs.
|
|
3172 This returns a string with trailing slash, like `default-directory'.
|
|
3173 Optional argument means return a file name relative to `default-directory'.
|
|
3174 In this it returns \"\" for the top directory."
|
|
3175 (let* ((here (point))
|
|
3176 (dir (catch 'done
|
|
3177 (mapcar (function
|
|
3178 (lambda (x)
|
|
3179 (if (<= (dired-get-subdir-min x) here)
|
|
3180 (throw 'done (car x)))))
|
|
3181 dired-subdir-alist))))
|
|
3182 (if (listp dir) (error "dired-subdir-alist seems to be mangled"))
|
|
3183 (if localp
|
|
3184 (let ((def-dir (expand-file-name default-directory)))
|
|
3185 (if (string-equal dir def-dir)
|
|
3186 ""
|
|
3187 (dired-make-relative dir def-dir)))
|
|
3188 dir)))
|
|
3189
|
|
3190 ;; Subdirs start at the beginning of their header lines and end just
|
|
3191 ;; before the beginning of the next header line (or end of buffer).
|
|
3192
|
|
3193 (defun dired-subdir-min ()
|
|
3194 ;; Returns the minimum position of the current subdir
|
|
3195 (save-excursion
|
|
3196 (if (not (dired-prev-subdir 0 t t))
|
|
3197 (error "Not in a subdir!")
|
|
3198 (point))))
|
|
3199
|
|
3200 (defun dired-subdir-max ()
|
|
3201 ;; Returns the maximum position of the current subdir
|
|
3202 (save-excursion
|
|
3203 (if (dired-next-subdir 1 t t)
|
|
3204 (1- (point)) ; Do not include separating empty line.
|
|
3205 (point-max))))
|
|
3206
|
|
3207
|
|
3208 ;;;; --------------------------------------------------------
|
|
3209 ;;;; Deleting files
|
|
3210 ;;;; --------------------------------------------------------
|
|
3211
|
|
3212 (defun dired-flag-file-deletion (arg)
|
|
3213 "In dired, flag the current line's file for deletion.
|
|
3214 With prefix arg, repeat over several lines.
|
|
3215
|
|
3216 If on a subdir headerline, mark all its files except `.' and `..'."
|
|
3217 (interactive "p")
|
|
3218 (dired-mark arg dired-del-marker))
|
|
3219
|
|
3220 (defun dired-flag-file-deletion-backup (arg)
|
|
3221 "Flag current file for deletion, and move to previous line.
|
|
3222 With a prefix ARG, repeats this ARG times."
|
|
3223 (interactive "p")
|
|
3224 ;; Use dired-mark-file and not dired-mark, as this function
|
|
3225 ;; should do nothing special on subdir headers.
|
|
3226 (dired-mark-file (- arg) dired-del-marker))
|
|
3227
|
|
3228 (defun dired-flag-subdir-files ()
|
|
3229 "Flag all the files in the current subdirectory for deletion."
|
|
3230 (interactive)
|
|
3231 (dired-mark-subdir-files dired-del-marker))
|
|
3232
|
|
3233 (defun dired-unflag (arg)
|
|
3234 "In dired, remove a deletion flag from the current line's file.
|
|
3235 Optional prefix ARG says how many lines to unflag."
|
|
3236 (interactive "p")
|
|
3237 (let (buffer-read-only)
|
|
3238 (dired-repeat-over-lines
|
|
3239 arg
|
|
3240 (function
|
|
3241 (lambda ()
|
|
3242 (if (char-equal (following-char) dired-del-marker)
|
|
3243 (progn
|
|
3244 (setq dired-del-flags-number (max (1- dired-del-flags-number) 0))
|
|
3245 (dired-substitute-marker (point) dired-del-marker ?\ )))))))
|
|
3246 (dired-update-mode-line-modified))
|
|
3247
|
|
3248 (defun dired-backup-unflag (arg)
|
|
3249 "In dired, move up lines and remove deletion flag there.
|
|
3250 Optional prefix ARG says how many lines to unflag; default is one line."
|
|
3251 (interactive "p")
|
|
3252 (dired-unflag (- arg)))
|
|
3253
|
|
3254 (defun dired-update-marker-counters (char &optional remove)
|
|
3255 (or (memq char '(?\ ?\n ?\r))
|
|
3256 (let ((counter (cond
|
|
3257 ((char-equal char dired-del-marker)
|
|
3258 'dired-del-flags-number)
|
|
3259 ((char-equal char dired-marker-char)
|
|
3260 'dired-marks-number)
|
|
3261 ('dired-other-marks-number))))
|
|
3262 (if remove
|
|
3263 (set counter (max (1- (symbol-value counter)) 0))
|
|
3264 (set counter (1+ (symbol-value counter)))))))
|
|
3265
|
|
3266 (defun dired-update-mode-line-modified (&optional check)
|
|
3267 ;; Updates the value of mode-line-modified in dired.
|
|
3268 ;; Currently assumes that it's of the form "-%%-", where % sometimes
|
|
3269 ;; gets replaced by %. Should allow some sort of config flag.
|
|
3270 ;; SET is t to set to -DD-, nil to set to -%%-, and 'check means
|
|
3271 ;; examine the buffer to find out.
|
|
3272 (if check
|
|
3273 (save-excursion
|
|
3274 (let (char)
|
|
3275 (goto-char (point-min))
|
|
3276 (setq dired-del-flags-number 0
|
|
3277 dired-marks-number 0
|
|
3278 dired-other-marks-number 0)
|
|
3279 (while (not (eobp))
|
|
3280 (setq char (following-char))
|
|
3281 (cond
|
|
3282 ((char-equal char dired-del-marker)
|
|
3283 (setq dired-del-flags-number (1+ dired-del-flags-number)))
|
|
3284 ((char-equal char dired-marker-char)
|
|
3285 (setq dired-marks-number (1+ dired-marks-number)))
|
|
3286 ((memq char '(?\ ?\n ?\r))
|
|
3287 nil)
|
|
3288 ((setq dired-other-marks-number (1+ dired-other-marks-number))))
|
|
3289 (forward-line 1)))))
|
|
3290 (setq mode-line-modified
|
|
3291 (format dired-mode-line-modified
|
|
3292 (if (zerop dired-del-flags-number)
|
|
3293 "--"
|
|
3294 (format "%d%c" dired-del-flags-number dired-del-marker))
|
|
3295 (if (zerop dired-marks-number)
|
|
3296 "--"
|
|
3297 (format "%d%c" dired-marks-number dired-marker-char))
|
|
3298 (if (zerop dired-other-marks-number)
|
|
3299 "-"
|
|
3300 (int-to-string dired-other-marks-number))))
|
|
3301 (set-buffer-modified-p (buffer-modified-p)))
|
|
3302
|
|
3303 (defun dired-do-deletions (&optional nomessage)
|
|
3304 (dired-expunge-deletions))
|
|
3305
|
|
3306 (defun dired-expunge-deletions ()
|
|
3307 "In dired, delete the files flagged for deletion."
|
|
3308 (interactive)
|
|
3309 (let ((files (let ((dired-marker-char dired-del-marker))
|
|
3310 (dired-map-over-marks (cons (dired-get-filename) (point))
|
|
3311 t))))
|
|
3312 (if files
|
|
3313 (progn
|
|
3314 (dired-internal-do-deletions files nil dired-del-marker)
|
|
3315 ;; In case the point gets left somewhere strange -- hope that
|
|
3316 ;; this doesn't cause asynch troubles later.
|
|
3317 (beginning-of-line)
|
|
3318 (dired-goto-next-nontrivial-file)
|
|
3319 (dired-update-mode-line-modified t)) ; play safe, it's cheap
|
|
3320 (message "(No deletions requested)"))))
|
|
3321
|
|
3322 (defun dired-do-delete (&optional arg)
|
|
3323 "Delete all marked (or next ARG) files."
|
|
3324 ;; This is more consistent with the file marking feature than
|
|
3325 ;; dired-expunge-deletions.
|
|
3326 (interactive "P")
|
|
3327 (dired-internal-do-deletions
|
|
3328 ;; this may move point if ARG is an integer
|
|
3329 (dired-map-over-marks (cons (dired-get-filename) (point))
|
|
3330 arg)
|
|
3331 arg)
|
|
3332 (beginning-of-line)
|
|
3333 (dired-goto-next-nontrivial-file))
|
|
3334
|
|
3335 (defun dired-internal-do-deletions (l arg &optional marker-char)
|
|
3336 ;; L is an alist of files to delete, with their buffer positions.
|
|
3337 ;; ARG is the prefix arg.
|
|
3338 ;; Filenames are absolute (VMS needs this for logical search paths).
|
|
3339 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
|
|
3340 ;; That way as changes are made in the buffer they do not shift the
|
|
3341 ;; lines still to be changed, so the (point) values in L stay valid.
|
|
3342 ;; Also, for subdirs in natural order, a subdir's files are deleted
|
|
3343 ;; before the subdir itself - the other way around would not work.
|
|
3344 (save-excursion
|
|
3345 (let ((files (mapcar (function car) l))
|
|
3346 (count (length l))
|
|
3347 (succ 0)
|
|
3348 (cdir (dired-current-directory))
|
|
3349 failures)
|
|
3350 ;; canonicalize file list for pop up
|
|
3351 (setq files (nreverse (mapcar (function
|
|
3352 (lambda (fn)
|
|
3353 (dired-make-relative fn cdir t)))
|
|
3354 files)))
|
|
3355 (if (or (memq 'delete dired-no-confirm)
|
|
3356 (dired-mark-pop-up
|
|
3357 " *Files Flagged for Deletion*" 'delete files
|
|
3358 dired-deletion-confirmer
|
|
3359 (format "Delete %s "
|
|
3360 (dired-mark-prompt arg files marker-char))))
|
|
3361 (save-excursion
|
|
3362 ;; files better be in reverse order for this loop!
|
|
3363 (while l
|
|
3364 (goto-char (cdr (car l)))
|
|
3365 (condition-case err
|
|
3366 (let ((fn (car (car l))))
|
|
3367 ;; This test is equivalent to
|
|
3368 ;; (and (file-directory-p fn)
|
|
3369 ;; (not (file-symlink-p fn)))
|
|
3370 ;; but more efficient
|
|
3371 (if (if (eq t (car (file-attributes fn)))
|
|
3372 (if (<= (length (directory-files fn)) 2)
|
|
3373 (progn (delete-directory fn) t)
|
|
3374 (and (or
|
|
3375 (memq 'recursive-delete dired-no-confirm)
|
|
3376 (funcall
|
|
3377 dired-deletion-confirmer
|
|
3378 (format "\
|
|
3379 Recursively delete directory and files within %s? "
|
|
3380 (dired-make-relative fn))))
|
|
3381 (progn
|
|
3382 (dired-recursive-delete-directory fn)
|
|
3383 t)))
|
|
3384 (progn (delete-file fn) t))
|
|
3385 (progn
|
|
3386 (setq succ (1+ succ))
|
|
3387 (message "%s of %s deletions" succ count)
|
|
3388 (dired-clean-up-after-deletion fn))))
|
|
3389 (error;; catch errors from failed deletions
|
|
3390 (dired-log (buffer-name (current-buffer)) "%s\n" err)
|
|
3391 (setq failures (cons (car (car l)) failures))))
|
|
3392 (setq l (cdr l)))))
|
|
3393 (if failures
|
|
3394 (dired-log-summary
|
|
3395 (buffer-name (current-buffer))
|
|
3396 (format "%d of %d deletion%s failed:" (length failures) count
|
|
3397 (dired-plural-s count))
|
|
3398 failures)
|
|
3399 (if (zerop succ)
|
|
3400 (message "(No deletions performed)")
|
|
3401 (message "%d deletion%s done" succ (dired-plural-s succ)))))))
|
|
3402
|
|
3403 (defun dired-recursive-delete-directory (fn)
|
|
3404 ;; Recursively deletes directory FN, and all of its contents.
|
|
3405 (let* ((fn (expand-file-name fn))
|
|
3406 (handler (find-file-name-handler
|
|
3407 fn 'dired-recursive-delete-directory)))
|
|
3408 (if handler
|
|
3409 (funcall handler 'dired-recursive-delete-directory fn)
|
|
3410 (progn
|
|
3411 (or (file-exists-p fn)
|
|
3412 (signal
|
|
3413 'file-error
|
|
3414 (list "Removing old file name" "no such directory" fn)))
|
|
3415 ;; Which is better, -r or -R?
|
|
3416 (call-process "rm" nil nil nil "-r" (directory-file-name fn))
|
|
3417 (and (file-exists-p fn)
|
|
3418 (error "Failed to recusively delete %s" fn))))))
|
|
3419
|
|
3420 (defun dired-clean-up-after-deletion (fn)
|
|
3421 ;; Offer to kill buffer of deleted file FN.
|
|
3422 (let ((buf (get-file-buffer fn)))
|
|
3423 (and buf
|
|
3424 (or (memq 'kill-file-buffer dired-no-confirm)
|
|
3425 (funcall (function yes-or-no-p)
|
|
3426 (format "Kill buffer of %s, too? "
|
|
3427 (file-name-nondirectory fn))))
|
|
3428 (save-excursion ; you never know where kill-buffer leaves you
|
|
3429 (kill-buffer buf)))))
|
|
3430
|
|
3431 ;;; Cleaning a directory -- flagging backups for deletion
|
|
3432
|
|
3433 (defun dired-clean-directory (keep &optional marker msg)
|
|
3434 "Flag numerical backups for deletion.
|
|
3435 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
|
|
3436 Positive prefix arg KEEP overrides `dired-kept-versions';
|
|
3437 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
|
|
3438
|
|
3439 To clear the flags on these files, you can use \\[dired-flag-backup-files]
|
|
3440 with a prefix argument."
|
|
3441 (interactive "P")
|
|
3442 (setq keep (if keep (prefix-numeric-value keep) dired-kept-versions))
|
|
3443 (let* ((early-retention (if (< keep 0) (- keep) kept-old-versions))
|
|
3444 (late-retention (if (<= keep 0) dired-kept-versions keep))
|
|
3445 (msg (or msg
|
|
3446 (format
|
|
3447 "Cleaning numerical backups (keeping %d late, %d old)"
|
|
3448 late-retention early-retention)))
|
|
3449 (trample-marker (or marker dired-del-marker))
|
|
3450 (file-version-assoc-list))
|
|
3451 (message "%s..." msg)
|
|
3452 ;; Do this after messaging, as it may take a while.
|
|
3453 (setq file-version-assoc-list (dired-collect-file-versions))
|
|
3454 ;; Sort each VERSION-NUMBER-LIST,
|
|
3455 ;; and remove the versions to be deleted.
|
|
3456 (let ((fval file-version-assoc-list))
|
|
3457 (while fval
|
|
3458 (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
|
|
3459 (v-count (length sorted-v-list)))
|
|
3460 (if (> v-count (+ early-retention late-retention))
|
|
3461 (rplacd (nthcdr early-retention sorted-v-list)
|
|
3462 (nthcdr (- v-count late-retention)
|
|
3463 sorted-v-list)))
|
|
3464 (rplacd (car fval)
|
|
3465 (cdr sorted-v-list)))
|
|
3466 (setq fval (cdr fval))))
|
|
3467 ;; Look at each file. If it is a numeric backup file,
|
|
3468 ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
|
|
3469 (dired-map-dired-file-lines (function
|
|
3470 (lambda (fn)
|
|
3471 (dired-trample-file-versions
|
|
3472 fn file-version-assoc-list
|
|
3473 trample-marker))))
|
|
3474 (message "%s...done" msg)))
|
|
3475
|
|
3476 (defun dired-collect-file-versions ()
|
|
3477 ;; If it looks like a file has versions, return a list of the versions.
|
|
3478 ;; The return value is ((FILENAME . (VERSION1 VERSION2 ...)) ...)
|
|
3479 (let (result)
|
|
3480 (dired-map-dired-file-lines
|
|
3481 (function
|
|
3482 (lambda (fn)
|
|
3483 (let* ((base-versions
|
|
3484 (concat (file-name-nondirectory fn) ".~"))
|
|
3485 (bv-length (length base-versions))
|
|
3486 (possibilities (file-name-all-completions
|
|
3487 base-versions
|
|
3488 (file-name-directory fn))))
|
|
3489 (if possibilities
|
|
3490 (setq result (cons (cons fn
|
|
3491 (mapcar 'backup-extract-version
|
|
3492 possibilities)) result)))))))
|
|
3493 result))
|
|
3494
|
|
3495 (defun dired-trample-file-versions (fn alist marker)
|
|
3496 ;; ALIST is an alist of filenames and versions used to determine
|
|
3497 ;; if each file should be flagged for deletion.
|
|
3498 ;; This version using file-name-sans-versions is probably a lot slower
|
|
3499 ;; than Sebastian's original, but it is more easily adaptable to non-unix.
|
|
3500 (let ((base (file-name-sans-versions fn))
|
|
3501 base-version-list bv-length)
|
|
3502 (and (not (string-equal base fn))
|
|
3503 (setq base-version-list (assoc base alist))
|
|
3504 (setq bv-length (string-match "[0-9]" fn (length base)))
|
|
3505 (not (memq (backup-extract-version fn) base-version-list))
|
|
3506 (progn (skip-chars-backward "^\n\r")
|
|
3507 (bolp)) ; make sure the preceding char isn't \r.
|
|
3508 (dired-substitute-marker (point) (following-char) marker))))
|
|
3509
|
|
3510 (defun dired-map-dired-file-lines (fun)
|
|
3511 ;; Perform FUN with point at the end of each non-directory line.
|
|
3512 ;; FUN takes one argument, the filename (complete pathname).
|
|
3513 (dired-check-ls-l)
|
|
3514 (save-excursion
|
|
3515 (let (file buffer-read-only)
|
|
3516 (goto-char (point-min))
|
|
3517 (while (not (eobp))
|
|
3518 (save-excursion
|
|
3519 (and (not (and dired-re-dir (looking-at dired-re-dir)))
|
|
3520 (not (memq (following-char) '(?\n ?\n)))
|
|
3521 (setq file (dired-get-filename nil t)) ; nil on non-file
|
|
3522 (progn (skip-chars-forward "^\n\r")
|
|
3523 (funcall fun file))))
|
|
3524 (forward-line 1))))) ; this guarantees that we don't
|
|
3525 ; operate on omitted files.
|
|
3526
|
|
3527
|
|
3528 ;;;; -----------------------------------------------------------
|
|
3529 ;;;; Confirmations and prompting the user.
|
|
3530 ;;;; -----------------------------------------------------------
|
|
3531
|
|
3532 (defun dired-plural-s (count)
|
|
3533 (if (= 1 count) "" "s"))
|
|
3534
|
|
3535 (defun dired-mark-prompt (arg files &optional marker-char)
|
|
3536 ;; Return a string for use in a prompt, either the current file
|
|
3537 ;; name, or the marker and a count of marked files.
|
|
3538 (let ((count (length files)))
|
|
3539 (if (= count 1)
|
|
3540 (car files)
|
|
3541 ;; more than 1 file:
|
|
3542 (if (integerp arg)
|
|
3543 (cond ((zerop arg) "[no files]")
|
|
3544 ((> arg 0) "[following]")
|
|
3545 ((< arg 0) "[preceding]"))
|
|
3546 (char-to-string (or marker-char dired-marker-char))))))
|
|
3547
|
|
3548 (defun dired-pop-to-buffer (buf)
|
|
3549 ;; Pop up buffer BUF.
|
|
3550 ;; Make its window fit its contents.
|
|
3551 (let ((window (selected-window))
|
|
3552 target-lines w2)
|
|
3553 (cond ;; if split-window-threshold is enabled, use the largest window
|
|
3554 ((and (> (window-height (setq w2 (get-largest-window)))
|
|
3555 split-height-threshold)
|
|
3556 (= (frame-width) (window-width w2)))
|
|
3557 (setq window w2))
|
|
3558 ;; if the least-recently-used window is big enough, use it
|
|
3559 ((and (> (window-height (setq w2 (get-lru-window)))
|
|
3560 (* 2 window-min-height))
|
|
3561 (= (frame-width) (window-width w2)))
|
|
3562 (setq window w2)))
|
|
3563 (save-excursion
|
|
3564 (set-buffer buf)
|
|
3565 (goto-char (point-max))
|
|
3566 (skip-chars-backward "\n\r\t ")
|
|
3567 (setq target-lines (count-lines (point-min) (point)))
|
|
3568 ;; Don't forget to count the last line.
|
|
3569 (if (not (bolp))
|
|
3570 (setq target-lines (1+ target-lines))))
|
|
3571 (if (<= (window-height window) (* 2 window-min-height))
|
|
3572 ;; At this point, every window on the frame is too small to split.
|
|
3573 (setq w2 (display-buffer buf))
|
|
3574 (setq w2 (split-window
|
|
3575 window
|
|
3576 (max window-min-height
|
|
3577 (- (window-height window)
|
|
3578 (1+ (max window-min-height target-lines)))))))
|
|
3579 (set-window-buffer w2 buf)
|
|
3580 (if (< (1- (window-height w2)) target-lines)
|
|
3581 (progn
|
|
3582 (select-window w2)
|
|
3583 (enlarge-window (- target-lines (1- (window-height w2))))))
|
|
3584 (set-window-start w2 1)))
|
|
3585
|
|
3586 (defun dired-mark-pop-up (bufname op-symbol files function &rest args)
|
|
3587 ;; Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
|
|
3588 ;; Return FUNCTION's result on ARGS after popping up a window (in a buffer
|
|
3589 ;; named BUFNAME, nil gives \" *Marked Files*\") showing the marked
|
|
3590 ;; files. Uses function `dired-pop-to-buffer' to do that.
|
|
3591 ;; FUNCTION should not manipulate files.
|
|
3592 ;; It should only read input (an argument or confirmation).
|
|
3593 ;; The window is not shown if there is just one file or
|
|
3594 ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
|
|
3595 ;; FILES is the list of marked files.
|
|
3596 (if (memq op-symbol dired-no-confirm)
|
|
3597 (apply function args)
|
|
3598 (or bufname (setq bufname " *Marked Files*"))
|
|
3599 (if (<= (length files) 1)
|
|
3600 (apply function args)
|
|
3601 (save-excursion
|
|
3602 (let ((standard-output (set-buffer (get-buffer-create bufname))))
|
|
3603 (erase-buffer)
|
|
3604 (dired-format-columns-of-files files)
|
|
3605 (dired-remove-text-properties (point-min) (point-max))
|
|
3606 (setq mode-line-format (format " %s [%d files]"
|
|
3607 bufname (length files)))))
|
|
3608 (save-window-excursion
|
|
3609 (dired-pop-to-buffer bufname)
|
|
3610 (apply function args)))))
|
|
3611
|
|
3612 (defun dired-column-widths (columns list &optional across)
|
|
3613 ;; Returns the column widths for breaking LIST into
|
|
3614 ;; COLUMNS number of columns.
|
|
3615 (cond
|
|
3616 ((null list)
|
|
3617 nil)
|
|
3618 ((= columns 1)
|
|
3619 (list (apply 'max (mapcar 'length list))))
|
|
3620 ((let* ((len (length list))
|
|
3621 (col-length (/ len columns))
|
|
3622 (remainder (% len columns))
|
|
3623 (i 0)
|
|
3624 (j 0)
|
|
3625 (max-width 0)
|
|
3626 widths padding)
|
|
3627 (if (zerop remainder)
|
|
3628 (setq padding 0)
|
|
3629 (setq col-length (1+ col-length)
|
|
3630 padding (- columns remainder)))
|
|
3631 (setq list (nconc (copy-sequence list) (make-list padding nil)))
|
|
3632 (setcdr (nthcdr (1- (+ len padding)) list) list)
|
|
3633 (while (< i columns)
|
|
3634 (while (< j col-length)
|
|
3635 (setq max-width (max max-width (length (car list)))
|
|
3636 list (if across (nthcdr columns list) (cdr list))
|
|
3637 j (1+ j)))
|
|
3638 (setq widths (cons (+ max-width 2) widths)
|
|
3639 max-width 0
|
|
3640 j 0
|
|
3641 i (1+ i))
|
|
3642 (if across (setq list (cdr list))))
|
|
3643 (setcar widths (- (car widths) 2))
|
|
3644 (nreverse widths)))))
|
|
3645
|
|
3646 (defun dired-calculate-columns (list &optional across)
|
|
3647 ;; Returns a list of integers which are the column widths that best pack
|
|
3648 ;; LIST, a list of strings, onto the screen.
|
|
3649 (and list
|
|
3650 (let* ((width (1- (window-width)))
|
|
3651 (columns (max 1 (/ width
|
|
3652 (+ 2 (apply 'max (mapcar 'length list))))))
|
|
3653 col-list last-col-list)
|
|
3654 (while (<= (apply '+ (setq col-list
|
|
3655 (dired-column-widths columns list across)))
|
|
3656 width)
|
|
3657 (setq columns (1+ columns)
|
|
3658 last-col-list col-list))
|
|
3659 (or last-col-list col-list))))
|
|
3660
|
|
3661 (defun dired-format-columns-of-files (files &optional across)
|
|
3662 ;; Returns the number of lines used.
|
|
3663 ;; If ACROSS is non-nil, sorts across rather than down the buffer, like
|
|
3664 ;; ls -x
|
|
3665 (and files
|
|
3666 (let* ((columns (dired-calculate-columns files across))
|
|
3667 (ncols (length columns))
|
|
3668 (ncols1 (1- ncols))
|
|
3669 (nfiles (length files))
|
|
3670 (nrows (+ (/ nfiles ncols)
|
|
3671 (if (zerop (% nfiles ncols)) 0 1)))
|
|
3672 (space-left (- (window-width) (apply '+ columns) 1))
|
|
3673 (i 0)
|
|
3674 (j 0)
|
|
3675 file padding stretch float-stretch)
|
|
3676 (if (zerop ncols1)
|
|
3677 (setq stretch 0
|
|
3678 float-stretch 0)
|
|
3679 (setq stretch (/ space-left ncols1)
|
|
3680 float-stretch (% space-left ncols1)))
|
|
3681 (setq files (nconc (copy-sequence files) ; fill up with empty fns
|
|
3682 (make-list (- (* ncols nrows) nfiles) "")))
|
|
3683 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
|
|
3684 (while (< j nrows)
|
|
3685 (while (< i ncols)
|
|
3686 (princ (setq file (car files)))
|
|
3687 (setq padding (- (nth i columns) (length file)))
|
|
3688 (or (= i ncols1)
|
|
3689 (progn
|
|
3690 (setq padding (+ padding stretch))
|
|
3691 (if (< i float-stretch) (setq padding (1+ padding)))))
|
|
3692 (princ (make-string padding ?\ ))
|
|
3693 (setq files (if across (cdr files) (nthcdr nrows files))
|
|
3694 i (1+ i)))
|
|
3695 (princ "\n")
|
|
3696 (setq i 0
|
|
3697 j (1+ j))
|
|
3698 (or across (setq files (cdr files))))
|
|
3699 nrows)))
|
|
3700
|
|
3701 (defun dired-query (qs-var qs-prompt &rest qs-args)
|
|
3702 ;; Query user and return nil or t.
|
|
3703 ;; Store answer in symbol VAR (which must initially be bound to nil).
|
|
3704 ;; Format PROMPT with ARGS.
|
|
3705 ;; Binding variable help-form will help the user who types C-h.
|
|
3706 (let* ((char (symbol-value qs-var))
|
|
3707 (action (cdr (assoc char dired-query-alist))))
|
|
3708 (cond ((eq 'yes action)
|
|
3709 t) ; accept, and don't ask again
|
|
3710 ((eq 'no action)
|
|
3711 nil) ; skip, and don't ask again
|
|
3712 (t;; no lasting effects from last time we asked - ask now
|
|
3713 (let ((qprompt (concat qs-prompt
|
|
3714 (if help-form
|
|
3715 (format " [yn!q or %s] "
|
|
3716 (key-description
|
|
3717 (char-to-string help-char)))
|
|
3718 " [ynq or !] ")))
|
|
3719 (dired-in-query t)
|
|
3720 elt)
|
|
3721 ;; Actually it looks nicer without cursor-in-echo-area - you can
|
|
3722 ;; look at the dired buffer instead of at the prompt to decide.
|
|
3723 (apply 'message qprompt qs-args)
|
|
3724 (setq char (set qs-var (read-char)))
|
|
3725 (while (not (setq elt (assoc char dired-query-alist)))
|
|
3726 (message "Invalid char - type %c for help." help-char)
|
|
3727 (ding)
|
|
3728 (sit-for 1)
|
|
3729 (apply 'message qprompt qs-args)
|
|
3730 (setq char (set qs-var (read-char))))
|
|
3731 (memq (cdr elt) '(t y yes)))))))
|
|
3732
|
|
3733 (defun dired-mark-confirm (op-symbol operation arg)
|
|
3734 ;; Request confirmation from the user that the operation described
|
|
3735 ;; by OP-SYMBOL is to be performed on the marked files.
|
|
3736 ;; Confirmation consists in a y-or-n question with a file list
|
|
3737 ;; pop-up unless OP-SYMBOL is a member of `dired-no-confirm'.
|
|
3738 ;; OPERATION is a string describing the operation. Used for prompting
|
|
3739 ;; the user.
|
|
3740 ;; The files used are determined by ARG (like in dired-get-marked-files).
|
|
3741 (or (memq op-symbol dired-no-confirm)
|
|
3742 (let ((files (dired-get-marked-files t arg)))
|
|
3743 (dired-mark-pop-up nil op-symbol files (function y-or-n-p)
|
|
3744 (concat operation " "
|
|
3745 (dired-mark-prompt arg files) "? ")))))
|
|
3746
|
|
3747 (defun dired-mark-read-file-name (prompt dir op-symbol arg files)
|
|
3748 (dired-mark-pop-up
|
|
3749 nil op-symbol files
|
|
3750 (function read-file-name)
|
|
3751 (format prompt (dired-mark-prompt arg files)) dir))
|
|
3752
|
|
3753 (defun dired-mark-read-string (prompt initial op-symbol arg files
|
|
3754 &optional history-sym)
|
|
3755 ;; Reading arguments with history.
|
|
3756 ;; Read arguments for a mark command of type OP-SYMBOL,
|
|
3757 ;; perhaps popping up the list of marked files.
|
|
3758 ;; ARG is the prefix arg and indicates whether the files came from
|
|
3759 ;; marks (ARG=nil) or a repeat factor (integerp ARG).
|
|
3760 ;; If the current file was used, the list has but one element and ARG
|
|
3761 ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)).
|
|
3762 ;; PROMPT for a string, with INITIAL input.
|
|
3763 (dired-mark-pop-up
|
|
3764 nil op-symbol files
|
|
3765 (function
|
|
3766 (lambda (prompt initial)
|
|
3767 (let ((hist (or history-sym
|
|
3768 (cdr (assq op-symbol dired-op-history-alist))
|
|
3769 'dired-history)))
|
|
3770 (dired-read-with-history prompt initial hist))))
|
|
3771 (format prompt (dired-mark-prompt arg files)) initial))
|
|
3772
|
|
3773
|
|
3774 ;;;; ----------------------------------------------------------
|
|
3775 ;;;; Marking files.
|
|
3776 ;;;; ----------------------------------------------------------
|
|
3777
|
|
3778 (defun dired-mark (arg &optional char)
|
|
3779 "Mark the current (or next ARG) files.
|
|
3780 If on a subdir headerline, mark all its files except `.' and `..'.
|
|
3781
|
|
3782 Use \\[dired-unmark-all-files] to remove all marks,
|
|
3783 and \\[dired-unmark] to remove the mark of the current file."
|
|
3784 (interactive "p")
|
|
3785 (if (dired-get-subdir)
|
|
3786 (dired-mark-subdir-files char)
|
|
3787 (dired-mark-file arg char)))
|
|
3788
|
|
3789 (defun dired-mark-file (arg &optional char)
|
|
3790 "Mark ARG files starting from the current file line.
|
|
3791 Optional CHAR indicates a marker character to use."
|
|
3792 (let (buffer-read-only)
|
|
3793 (if (memq (or char dired-marker-char) '(?\ ?\n ?\r))
|
114
|
3794 (error "Invalid marker character %c" dired-marker-char))
|
98
|
3795 (or char (setq char dired-marker-char))
|
|
3796 (dired-repeat-over-lines
|
|
3797 arg
|
|
3798 (function
|
|
3799 (lambda ()
|
|
3800 (dired-update-marker-counters (following-char) t)
|
|
3801 (dired-substitute-marker (point) (following-char) char)
|
|
3802 (dired-update-marker-counters char))))
|
|
3803 (dired-update-mode-line-modified)))
|
|
3804
|
|
3805 (defun dired-mark-subdir-files (&optional char)
|
|
3806 "Mark all files except `.' and `..'."
|
|
3807 (interactive)
|
|
3808 (save-excursion
|
|
3809 (dired-mark-files-in-region (dired-subdir-min) (dired-subdir-max) char)))
|
|
3810
|
|
3811 (defun dired-unmark (arg)
|
|
3812 "Unmark the current (or next ARG) files.
|
|
3813 If looking at a subdir, unmark all its files except `.' and `..'."
|
|
3814 (interactive "p")
|
|
3815 (let (buffer-read-only)
|
|
3816 (dired-repeat-over-lines
|
|
3817 arg
|
|
3818 (function
|
|
3819 (lambda ()
|
|
3820 (let ((char (following-char)))
|
|
3821 (or (memq char '(?\ ?\n ?\r))
|
|
3822 (progn
|
|
3823 (cond
|
|
3824 ((char-equal char dired-marker-char)
|
|
3825 (setq dired-marks-number (max (1- dired-marks-number) 0)))
|
|
3826 ((char-equal char dired-del-marker)
|
|
3827 (setq dired-del-flags-number
|
|
3828 (max (1- dired-del-flags-number) 0)))
|
|
3829 ((setq dired-other-marks-number
|
|
3830 (max (1- dired-other-marks-number) 0))))
|
|
3831 (dired-substitute-marker (point) char ?\ )))))))
|
|
3832 (dired-update-mode-line-modified)))
|
|
3833
|
|
3834 (defun dired-mark-prefix (&optional arg)
|
|
3835 "Mark the next ARG files with the next character typed.
|
|
3836 If ARG is negative, marks the previous files."
|
|
3837 (interactive "p")
|
|
3838 (if (sit-for echo-keystrokes)
|
|
3839 (cond
|
|
3840 ((or (= arg 1) (zerop arg))
|
|
3841 (message "Mark with character?"))
|
|
3842 ((< arg 0)
|
|
3843 (message "Mark %d file%s moving backwards?"
|
|
3844 (- arg) (dired-plural-s (- arg))))
|
|
3845 ((> arg 1)
|
|
3846 (message "Mark %d following files with character?" arg))))
|
|
3847 (dired-mark arg (read-char)))
|
|
3848
|
|
3849 (defun dired-change-marks (old new)
|
|
3850 "Change all OLD marks to NEW marks.
|
|
3851 OLD and NEW are both characters used to mark files.
|
|
3852 With a prefix, prompts for a mark to toggle. In other words, all unmarked
|
|
3853 files receive that mark, and all files currently marked with that mark become
|
|
3854 unmarked."
|
|
3855 ;; When used in a lisp program, setting NEW to nil means toggle the mark OLD.
|
|
3856 (interactive
|
|
3857 (let* ((cursor-in-echo-area t)
|
|
3858 (old nil)
|
|
3859 (new nil)
|
|
3860 (markers (dired-mark-list))
|
|
3861 (default (cond ((null markers)
|
|
3862 (error "No markers in buffer"))
|
|
3863 ((= (length markers) 1)
|
|
3864 (setq old (car markers)))
|
|
3865 ((memq dired-marker-char markers)
|
|
3866 dired-marker-char)
|
|
3867 ;; picks the last one in the buffer. reasonable?
|
|
3868 ((car markers)))))
|
|
3869 (or old (setq old
|
|
3870 (progn
|
|
3871 (if current-prefix-arg
|
|
3872 (message "Toggle mark (default %c): " default)
|
|
3873 (message "Change old mark (default %c): " default))
|
|
3874 (read-char))))
|
|
3875 (if (memq old '(?\ ?\n ?\r)) (setq old default))
|
|
3876 (or current-prefix-arg
|
|
3877 (setq new (progn
|
|
3878 (message
|
|
3879 "Change %c marks to new mark (RET means abort): " old)
|
|
3880 (read-char))))
|
|
3881 (list old new)))
|
|
3882 (let ((old-count (cond
|
|
3883 ((char-equal old dired-marker-char)
|
|
3884 'dired-marks-number)
|
|
3885 ((char-equal old dired-del-marker)
|
|
3886 'dired-del-flags-number)
|
|
3887 ('dired-other-marks-number))))
|
|
3888 (if new
|
|
3889 (or (memq new '(?\ ?\n ?\r))
|
|
3890 ;; \n and \r aren't valid marker chars. Assume that if the
|
|
3891 ;; user hits return, he meant to abort the command.
|
|
3892 (let ((string (format "\n%c" old))
|
|
3893 (new-count (cond
|
|
3894 ((char-equal new dired-marker-char)
|
|
3895 'dired-marks-number)
|
|
3896 ((char-equal new dired-del-marker)
|
|
3897 'dired-del-flags-number)
|
|
3898 ('dired-other-marks-number)))
|
|
3899 (buffer-read-only nil))
|
|
3900 (save-excursion
|
|
3901 (goto-char (point-min))
|
|
3902 (while (search-forward string nil t)
|
|
3903 (if (char-equal (preceding-char) old)
|
|
3904 (progn
|
|
3905 (dired-substitute-marker (1- (point)) old new)
|
|
3906 (set new-count (1+ (symbol-value new-count)))
|
|
3907 (set old-count (max (1- (symbol-value old-count)) 0))))
|
|
3908 ))))
|
|
3909 (save-excursion
|
|
3910 (let ((ucount 0)
|
|
3911 (mcount 0)
|
|
3912 (buffer-read-only nil))
|
|
3913 (goto-char (point-min))
|
|
3914 (while (not (eobp))
|
|
3915 (or (dired-between-files)
|
|
3916 (looking-at dired-re-dot)
|
|
3917 (cond
|
|
3918 ((= (following-char) ?\ )
|
|
3919 (setq mcount (1+ mcount))
|
|
3920 (set old-count (1+ (symbol-value old-count)))
|
|
3921 (dired-substitute-marker (point) ?\ old))
|
|
3922 ((= (following-char) old)
|
|
3923 (setq ucount (1+ ucount))
|
|
3924 (set old-count (max (1- (symbol-value old-count)) 0))
|
|
3925 (dired-substitute-marker (point) old ?\ ))))
|
|
3926 (forward-line 1))
|
|
3927 (message "Unmarked %d file%s; marked %d file%s with %c."
|
|
3928 ucount (dired-plural-s ucount) mcount
|
|
3929 (dired-plural-s mcount) old)))))
|
|
3930 (dired-update-mode-line-modified))
|
|
3931
|
|
3932 (defun dired-unmark-all-files (flag &optional arg)
|
|
3933 "Remove a specific mark or any mark from every file.
|
|
3934 With prefix arg, query for each marked file.
|
|
3935 Type \\[help-command] at that time for help.
|
|
3936 With a zero prefix, only counts the number of marks."
|
|
3937 (interactive
|
|
3938 (let* ((cursor-in-echo-area t)
|
|
3939 executing-kbd-macro) ; for XEmacs
|
|
3940 (list (and (not (eq current-prefix-arg 0))
|
|
3941 (progn (message "Remove marks (RET means all): ") (read-char)))
|
|
3942 current-prefix-arg)))
|
|
3943 (save-excursion
|
|
3944 (let* ((help-form "\
|
|
3945 Type SPC or `y' to unflag one file, DEL or `n' to skip to next,
|
|
3946 `!' to unflag all remaining files with no more questions.")
|
|
3947 (allp (memq flag '(?\n ?\r)))
|
|
3948 (count-p (eq arg 0))
|
|
3949 (count (if (or allp count-p)
|
|
3950 (mapcar
|
|
3951 (function
|
|
3952 (lambda (elt)
|
|
3953 (cons elt 0)))
|
|
3954 (nreverse (dired-mark-list)))
|
|
3955 0))
|
|
3956 (msg "")
|
|
3957 (no-query (or (not arg) count-p))
|
|
3958 buffer-read-only case-fold-search query)
|
|
3959 (goto-char (point-min))
|
|
3960 (if (or allp count-p)
|
|
3961 (while (re-search-forward dired-re-mark nil t)
|
|
3962 (if (or no-query
|
|
3963 (dired-query 'query "Unmark file `%s'? "
|
|
3964 (dired-get-filename t)))
|
|
3965 (let ((ent (assq (preceding-char) count)))
|
|
3966 (if ent (setcdr ent (1+ (cdr ent))))
|
|
3967 (or count-p (dired-substitute-marker
|
|
3968 (- (point) 1) (preceding-char) ?\ ))))
|
|
3969 (forward-line 1))
|
|
3970 (while (search-forward (format "\n%c" flag) nil t)
|
|
3971 (if (or no-query
|
|
3972 (dired-query 'query "Unmark file `%s'? "
|
|
3973 (dired-get-filename t)))
|
|
3974 (progn
|
|
3975 (dired-substitute-marker (match-beginning 0) flag ?\ )
|
|
3976 (setq count (1+ count))))))
|
|
3977 (if (or allp count-p)
|
|
3978 (mapcar
|
|
3979 (function
|
|
3980 (lambda (elt)
|
|
3981 (or (zerop (cdr elt))
|
|
3982 (setq msg (format "%s%s%d %c%s"
|
|
3983 msg
|
|
3984 (if (zerop (length msg))
|
|
3985 " "
|
|
3986 ", ")
|
|
3987 (cdr elt)
|
|
3988 (car elt)
|
|
3989 (if (= 1 (cdr elt)) "" "'s"))))))
|
|
3990 count)
|
|
3991 (or (zerop count)
|
|
3992 (setq msg (format " %d %c%s"
|
|
3993 count flag (if (= 1 count) "" "'s")))))
|
|
3994 (if (zerop (length msg))
|
|
3995 (setq msg " none")
|
|
3996 (or count-p (dired-update-mode-line-modified t)))
|
|
3997 (message "%s:%s" (if count-p "Number of marks" "Marks removed") msg))))
|
|
3998
|
|
3999 (defun dired-get-marked-files (&optional localp arg)
|
|
4000 "Return the marked files' names as list of strings.
|
|
4001 The list is in the same order as the buffer, that is, the car is the
|
|
4002 first marked file.
|
|
4003 Values returned are normally absolute pathnames.
|
|
4004 Optional arg LOCALP as in `dired-get-filename'.
|
|
4005 Optional second argument ARG forces to use other files. If ARG is an
|
|
4006 integer, use the next ARG files. If ARG is otherwise non-nil, use
|
|
4007 current file. Usually ARG comes from the current prefix arg."
|
|
4008 (save-excursion
|
|
4009 (nreverse (dired-map-over-marks (dired-get-filename localp) arg))))
|
|
4010
|
|
4011 ;;; Utility functions for marking files
|
|
4012
|
|
4013 (defun dired-mark-files-in-region (start end &optional char)
|
|
4014 (let (buffer-read-only)
|
|
4015 (if (> start end)
|
|
4016 (error "start > end"))
|
|
4017 (goto-char start) ; assumed at beginning of line
|
|
4018 (or char (setq char dired-marker-char))
|
|
4019 (while (< (point) end)
|
|
4020 ;; Skip subdir line and following garbage like the `total' line:
|
|
4021 (while (and (< (point) end) (dired-between-files))
|
|
4022 (forward-line 1))
|
|
4023 (if (and (/= (following-char) char)
|
|
4024 (not (looking-at dired-re-dot))
|
|
4025 (save-excursion
|
|
4026 (dired-move-to-filename nil (point))))
|
|
4027 (progn
|
|
4028 (dired-update-marker-counters (following-char) t)
|
|
4029 (dired-substitute-marker (point) (following-char) char)
|
|
4030 (dired-update-marker-counters char)))
|
|
4031 (forward-line 1)))
|
|
4032 (dired-update-mode-line-modified))
|
|
4033
|
|
4034 (defun dired-mark-list ()
|
|
4035 ;; Returns a list of all marks currently used in the buffer.
|
|
4036 (let ((result nil)
|
|
4037 char)
|
|
4038 (save-excursion
|
|
4039 (goto-char (point-min))
|
|
4040 (while (not (eobp))
|
|
4041 (and (not (memq (setq char (following-char)) '(?\ ?\n ?\r)))
|
|
4042 (not (memq char result))
|
|
4043 (setq result (cons char result)))
|
|
4044 (forward-line 1)))
|
|
4045 result))
|
|
4046
|
|
4047 ;;; Dynamic markers
|
|
4048
|
|
4049 (defun dired-set-current-marker-string ()
|
|
4050 "Computes and returns `dired-marker-string'."
|
|
4051 (prog1
|
|
4052 (setq dired-marker-string
|
|
4053 (if dired-marker-stack
|
|
4054 (let* ((n (+ (length dired-marker-stack) 5))
|
|
4055 (str (make-string n ?\ ))
|
|
4056 (list dired-marker-stack)
|
|
4057 (pointer dired-marker-stack-pointer))
|
|
4058 (setq n (1- n))
|
|
4059 (aset str n ?\])
|
|
4060 (setq n (1- n))
|
|
4061 (while list
|
|
4062 (aset str n (car list))
|
|
4063 (if (zerop pointer)
|
|
4064 (progn
|
|
4065 (setq n (1- n))
|
|
4066 (aset str n dired-marker-stack-cursor)))
|
|
4067 (setq n (1- n)
|
|
4068 pointer (1- pointer)
|
|
4069 list (cdr list)))
|
|
4070 (aset str n dired-default-marker)
|
|
4071 (if (zerop pointer)
|
|
4072 (aset str 2 dired-marker-stack-cursor))
|
|
4073 (aset str 1 ?\[)
|
|
4074 str)
|
|
4075 ""))
|
|
4076 (set-buffer-modified-p (buffer-modified-p))))
|
|
4077
|
|
4078 (defun dired-set-marker-char (c)
|
|
4079 "Set the marker character to something else.
|
|
4080 Use \\[dired-restore-marker-char] to restore the previous value."
|
|
4081 (interactive "cNew marker character: ")
|
|
4082 (and (memq c '(?\ ?\n ?\r)) (error "invalid marker char %c" c))
|
|
4083 (setq dired-marker-stack (cons c dired-marker-stack)
|
|
4084 dired-marker-stack-pointer 0
|
|
4085 dired-marker-char c)
|
|
4086 (dired-update-mode-line-modified t)
|
|
4087 (dired-set-current-marker-string))
|
|
4088
|
|
4089 (defun dired-restore-marker-char ()
|
|
4090 "Restore the marker character to its previous value.
|
|
4091 Uses `dired-default-marker' if the marker stack is empty."
|
|
4092 (interactive)
|
|
4093 (setq dired-marker-stack (cdr dired-marker-stack)
|
|
4094 dired-marker-char (car dired-marker-stack)
|
|
4095 dired-marker-stack-pointer (min dired-marker-stack-pointer
|
|
4096 (length dired-marker-stack)))
|
|
4097 (or dired-marker-char
|
|
4098 (setq dired-marker-char dired-default-marker))
|
|
4099 (dired-set-current-marker-string)
|
|
4100 (dired-update-mode-line-modified t)
|
|
4101 (or dired-marker-stack (message "Marker is %c" dired-marker-char)))
|
|
4102
|
|
4103 (defun dired-marker-stack-left (n)
|
|
4104 "Moves the marker stack cursor to the left."
|
|
4105 (interactive "p")
|
|
4106 (let ((len (1+ (length dired-marker-stack))))
|
|
4107 (or dired-marker-stack (error "Dired marker stack is empty."))
|
|
4108 (setq dired-marker-stack-pointer
|
|
4109 (% (+ dired-marker-stack-pointer n) len))
|
|
4110 (if (< dired-marker-stack-pointer 0)
|
|
4111 (setq dired-marker-stack-pointer (+ dired-marker-stack-pointer
|
|
4112 len)))
|
|
4113 (dired-set-current-marker-string)
|
|
4114 (setq dired-marker-char
|
|
4115 (if (= dired-marker-stack-pointer (1- len))
|
|
4116 dired-default-marker
|
|
4117 (nth dired-marker-stack-pointer dired-marker-stack))))
|
|
4118 (dired-update-mode-line-modified t))
|
|
4119
|
|
4120 (defun dired-marker-stack-right (n)
|
|
4121 "Moves the marker stack cursor to the right."
|
|
4122 (interactive "p")
|
|
4123 (dired-marker-stack-left (- n)))
|
|
4124
|
|
4125 ;;; Commands to mark or flag files based on their characteristics or names.
|
|
4126
|
|
4127 (defun dired-mark-symlinks (&optional unflag-p)
|
|
4128 "Mark all symbolic links.
|
|
4129 With prefix argument, unflag all those files."
|
|
4130 (interactive "P")
|
|
4131 (dired-check-ls-l)
|
|
4132 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
|
|
4133 (dired-mark-if (looking-at dired-re-sym) "symbolic link"))
|
|
4134 (dired-update-mode-line-modified t))
|
|
4135
|
|
4136 (defun dired-mark-directories (&optional unflag-p)
|
|
4137 "Mark all directory file lines except `.' and `..'.
|
|
4138 With prefix argument, unflag all those files."
|
|
4139 (interactive "P")
|
|
4140 (if dired-re-dir
|
|
4141 (progn
|
|
4142 (dired-check-ls-l)
|
|
4143 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
|
|
4144 (dired-mark-if (and (looking-at dired-re-dir)
|
|
4145 (not (looking-at dired-re-dot)))
|
|
4146 "directory file"))))
|
|
4147 (dired-update-mode-line-modified t))
|
|
4148
|
|
4149 (defun dired-mark-executables (&optional unflag-p)
|
|
4150 "Mark all executable files.
|
|
4151 With prefix argument, unflag all those files."
|
|
4152 (interactive "P")
|
|
4153 (if dired-re-exe
|
|
4154 (progn
|
|
4155 (dired-check-ls-l)
|
|
4156 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
|
|
4157 (dired-mark-if (looking-at dired-re-exe) "executable file"))))
|
|
4158 (dired-update-mode-line-modified t))
|
|
4159
|
|
4160 (defun dired-flag-backup-files (&optional unflag-p)
|
|
4161 "Flag all backup files (names ending with `~') for deletion.
|
|
4162 With prefix argument, unflag these files."
|
|
4163 (interactive "P")
|
|
4164 (dired-check-ls-l)
|
|
4165 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
|
|
4166 (dired-mark-if
|
|
4167 (and (not (and dired-re-dir (looking-at dired-re-dir)))
|
|
4168 (let ((fn (dired-get-filename t t)))
|
|
4169 (if fn (backup-file-name-p fn))))
|
|
4170 "backup file"))
|
|
4171 (dired-update-mode-line-modified t))
|
|
4172
|
|
4173 (defun dired-flag-auto-save-files (&optional unflag-p)
|
|
4174 "Flag for deletion files whose names suggest they are auto save files.
|
|
4175 A prefix argument says to unflag those files instead."
|
|
4176 (interactive "P")
|
|
4177 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
|
|
4178 (dired-mark-if
|
|
4179 ;; It is less than general to check for ~ here,
|
|
4180 ;; but it's the only way this runs fast enough.
|
|
4181 (and (save-excursion (end-of-line)
|
|
4182 (eq (preceding-char) ?#))
|
|
4183 (not (and dired-re-dir (looking-at dired-re-dir)))
|
|
4184 (let ((fn (dired-get-filename t t)))
|
|
4185 (if fn (auto-save-file-name-p
|
|
4186 (file-name-nondirectory fn)))))
|
|
4187 "auto save file"))
|
|
4188 (dired-update-mode-line-modified t))
|
|
4189
|
|
4190 (defun dired-mark-rcs-files (&optional unflag-p)
|
|
4191 "Mark all files that are under RCS control.
|
|
4192 With prefix argument, unflag all those files.
|
|
4193 Mentions RCS files for which a working file was not found in this buffer.
|
|
4194 Type \\[dired-why] to see them again."
|
|
4195 ;; Returns failures, or nil on success.
|
|
4196 ;; Finding those with locks would require to peek into the ,v file,
|
|
4197 ;; depends slightly on the RCS version used and should be done
|
|
4198 ;; together with the Emacs RCS interface.
|
|
4199 ;; Unfortunately, there is no definitive RCS interface yet.
|
|
4200 (interactive "P")
|
|
4201 (message "%sarking RCS controlled files..." (if unflag-p "Unm" "M"))
|
|
4202 (let ((dired-marker-char (if unflag-p ?\ dired-marker-char))
|
|
4203 rcs-files wf failures count total)
|
|
4204 (mapcar ; loop over subdirs
|
|
4205 (function
|
|
4206 (lambda (dir)
|
|
4207 (or (equal (file-name-nondirectory (directory-file-name dir))
|
|
4208 "RCS")
|
|
4209 ;; skip inserted RCS subdirs
|
|
4210 (setq rcs-files
|
|
4211 (append (directory-files dir t ",v$") ; *,v and RCS/*,v
|
|
4212 (let ((rcs-dir (expand-file-name "RCS" dir)))
|
|
4213 (if (file-directory-p rcs-dir)
|
|
4214 (mapcar ; working files from ./RCS are in ./
|
|
4215 (function
|
|
4216 (lambda (x)
|
|
4217 (expand-file-name x dir)))
|
|
4218 (directory-files
|
|
4219 (file-name-as-directory rcs-dir)
|
|
4220 nil ",v$"))))
|
|
4221 rcs-files)))))
|
|
4222 (mapcar (function car) dired-subdir-alist))
|
|
4223 (setq total (length rcs-files))
|
|
4224 (while rcs-files
|
|
4225 (setq wf (substring (car rcs-files) 0 -2)
|
|
4226 rcs-files (cdr rcs-files))
|
|
4227 (save-excursion (if (dired-goto-file wf)
|
|
4228 (dired-mark 1) ; giving a prefix avoids checking
|
|
4229 ; for subdir line.
|
|
4230 (setq failures (cons wf failures)))))
|
|
4231 (dired-update-mode-line-modified t)
|
|
4232 (if (null failures)
|
|
4233 (message "%d RCS file%s %smarked."
|
|
4234 total (dired-plural-s total) (if unflag-p "un" ""))
|
|
4235 (setq count (length failures))
|
|
4236 (dired-log-summary (buffer-name (current-buffer))
|
|
4237 "RCS working file not found %s" failures)
|
|
4238 (message "%d RCS file%s: %d %smarked - %d not found %s."
|
|
4239 total (dired-plural-s total) (- total count)
|
|
4240 (if unflag-p "un" "") count failures))
|
|
4241 failures))
|
|
4242
|
|
4243
|
|
4244 ;;;; ------------------------------------------------------------
|
|
4245 ;;;; Logging failures
|
|
4246 ;;;; ------------------------------------------------------------
|
|
4247
|
|
4248 (defun dired-why ()
|
|
4249 "Pop up a buffer with error log output from Dired.
|
|
4250 A group of errors from a single command ends with a formfeed.
|
|
4251 Thus, use \\[backward-page] to find the beginning of a group of errors."
|
|
4252 (interactive)
|
|
4253 (if (get-buffer dired-log-buffer)
|
|
4254 (let ((owindow (selected-window))
|
|
4255 (window (display-buffer (get-buffer dired-log-buffer))))
|
|
4256 (unwind-protect
|
|
4257 (progn
|
|
4258 (select-window window)
|
|
4259 (goto-char (point-max))
|
|
4260 (recenter -1))
|
|
4261 (select-window owindow)))))
|
|
4262
|
|
4263 (defun dired-log (buffer-name log &rest args)
|
|
4264 ;; Log a message or the contents of a buffer.
|
|
4265 ;; BUFFER-NAME is the name of the dired buffer to which the message applies.
|
|
4266 ;; If LOG is a string and there are more args, it is formatted with
|
|
4267 ;; those ARGS. Usually the LOG string ends with a \n.
|
|
4268 ;; End each bunch of errors with (dired-log t): this inserts
|
|
4269 ;; current time and buffer, and a \f (formfeed).
|
|
4270 (or (stringp buffer-name) (setq buffer-name (buffer-name buffer-name)))
|
|
4271 (let ((obuf (current-buffer)))
|
|
4272 (unwind-protect ; want to move point
|
|
4273 (progn
|
|
4274 (set-buffer (get-buffer-create dired-log-buffer))
|
|
4275 (goto-char (point-max))
|
|
4276 (let (buffer-read-only)
|
|
4277 (cond ((stringp log)
|
|
4278 (insert (if args
|
|
4279 (apply (function format) log args)
|
|
4280 log)))
|
|
4281 ((bufferp log)
|
|
4282 (insert-buffer log))
|
|
4283 ((eq t log)
|
|
4284 (insert "\n\t" (current-time-string)
|
|
4285 "\tBuffer `" buffer-name "'\n\f\n")))))
|
|
4286 (set-buffer obuf))))
|
|
4287
|
|
4288 (defun dired-log-summary (buffer-name string failures)
|
|
4289 (message (if failures "%s--type y for details %s"
|
|
4290 "%s--type y for details")
|
|
4291 string failures)
|
|
4292 ;; Log a summary describing a bunch of errors.
|
|
4293 (dired-log buffer-name (concat "\n" string))
|
|
4294 (if failures (dired-log buffer-name "\n%s" failures))
|
|
4295 (dired-log buffer-name t))
|
|
4296
|
|
4297
|
|
4298 ;;;; -------------------------------------------------------
|
|
4299 ;;;; Sort mode of dired buffers.
|
|
4300 ;;;; -------------------------------------------------------
|
|
4301
|
|
4302 (defun dired-sort-type (list)
|
|
4303 ;; Returns the sort type of LIST, as a symbol.
|
|
4304 (let* ((list (reverse list))
|
|
4305 (alist (sort
|
|
4306 (mapcar (function
|
|
4307 (lambda (x)
|
|
4308 (cons (length (memq (car x) list)) (cdr x))))
|
|
4309 dired-sort-type-alist)
|
|
4310 (function
|
|
4311 (lambda (x y)
|
|
4312 (> (car x) (car y))))))
|
|
4313 (winner (car alist)))
|
|
4314 (if (zerop (car winner))
|
|
4315 'name
|
|
4316 (cdr winner))))
|
|
4317
|
|
4318 (defun dired-sort-set-modeline (&optional switches)
|
|
4319 ;; Set modeline display according to dired-internal-switches.
|
|
4320 ;; Modeline display of "by name" or "by date" guarantees the user a
|
|
4321 ;; match with the corresponding regexps. Non-matching switches are
|
|
4322 ;; shown literally.
|
|
4323 (or switches (setq switches dired-internal-switches))
|
|
4324 (setq dired-sort-mode
|
|
4325 (if dired-show-ls-switches
|
|
4326 (concat " " (dired-make-switches-string
|
|
4327 (or switches dired-internal-switches)))
|
|
4328 (concat " by " (and (memq ?r switches) "rev-")
|
|
4329 (symbol-name (dired-sort-type switches)))))
|
|
4330 ;; update mode line
|
|
4331 (set-buffer-modified-p (buffer-modified-p)))
|
|
4332
|
|
4333 (defun dired-sort-toggle-or-edit (&optional arg)
|
|
4334 "Toggle between sort by date/name for the current subdirectory.
|
|
4335
|
|
4336 With a 0 prefix argument, simply reports on the current switches.
|
|
4337
|
|
4338 With a prefix 1 allows the ls switches for the current subdirectory to be
|
|
4339 edited.
|
|
4340
|
|
4341 With a prefix 2 allows the default ls switches for newly inserted
|
|
4342 subdirectories to be edited.
|
|
4343
|
|
4344 With a prefix \\[universal-argument] allows you to sort the entire
|
|
4345 buffer by either name or date.
|
|
4346
|
|
4347 With a prefix \\[universal-argument] \\[universal-argument] allows the default switches
|
|
4348 for the entire buffer to be edited, and then reverts the buffer so that all
|
|
4349 subdirectories are sorted according to these switches.
|
|
4350
|
|
4351 Note that although dired allows different ls switches to be used for
|
|
4352 different subdirectories, certain combinations of ls switches are incompatible.
|
|
4353 If incompatible switches are detected, dired will offer to revert the buffer
|
|
4354 to force the ls switches for all subdirectories to a single value. If you
|
|
4355 refuse to revert the buffer, any change of ls switches will be aborted."
|
|
4356 (interactive "P")
|
|
4357 (cond
|
|
4358 ((eq arg 0)
|
|
4359 ;; Report on switches
|
|
4360 (message "Switches for current subdir: %s. Default for buffer: %s."
|
|
4361 (dired-make-switches-string
|
|
4362 (nth 3 (assoc (dired-current-directory) dired-subdir-alist)))
|
|
4363 (dired-make-switches-string dired-internal-switches)))
|
|
4364 ((null arg)
|
|
4365 ;; Toggle between sort by date/name.
|
|
4366 (let* ((dir (dired-current-directory))
|
|
4367 (curr (nth 3 (assoc dir dired-subdir-alist))))
|
|
4368 (dired-sort-other
|
|
4369 (if (eq (dired-sort-type curr) 'name)
|
|
4370 (cons ?t curr)
|
|
4371 (mapcar (function
|
|
4372 (lambda (x)
|
|
4373 (setq curr
|
|
4374 (delq (car x) curr))))
|
|
4375 dired-sort-type-alist)
|
|
4376 curr)
|
|
4377 nil dir)))
|
|
4378 ((eq arg 1)
|
|
4379 ;; Edit switches for current subdir.
|
|
4380 (let* ((dir (dired-current-directory))
|
|
4381 (switch-string
|
|
4382 (read-string
|
|
4383 "New ls switches for current subdir (must contain -l): "
|
|
4384 (dired-make-switches-string
|
|
4385 (nth 3 (assoc dir dired-subdir-alist)))))
|
|
4386 (switches (dired-make-switches-list switch-string)))
|
|
4387 (if (dired-compatible-switches-p switches dired-internal-switches)
|
|
4388 (dired-sort-other switches nil dir)
|
|
4389 (if (or
|
|
4390 (memq 'sort-revert dired-no-confirm)
|
|
4391 (y-or-n-p
|
|
4392 (format
|
|
4393 "Switches %s incompatible with default %s. Revert buffer? "
|
|
4394 switch-string
|
|
4395 (dired-make-switches-string dired-internal-switches))))
|
|
4396 (dired-sort-other switches nil nil)
|
|
4397 (error "Switches unchanged. Remain as %s." switch-string)))))
|
|
4398 ((eq arg 2)
|
|
4399 ;; Set new defaults for subdirs inserted in the future.
|
|
4400 (let* ((switch-string
|
|
4401 (read-string
|
|
4402 "Default ls switches for new subdirs (must contain -l): "
|
|
4403 (dired-make-switches-string dired-internal-switches)))
|
|
4404 (switches (dired-make-switches-list switch-string))
|
|
4405 (alist dired-subdir-alist)
|
|
4406 x bad-switches)
|
|
4407 (while alist
|
|
4408 (setq x (nth 3 (car alist))
|
|
4409 alist (cdr alist))
|
|
4410 (or (dired-compatible-switches-p x switches)
|
|
4411 (member x bad-switches)
|
|
4412 (setq bad-switches (cons x bad-switches))))
|
|
4413 (if bad-switches
|
|
4414 (if (or (memq 'sort-revert dired-no-confirm)
|
|
4415 (y-or-n-p
|
|
4416 (format
|
|
4417 "Switches %s incompatible with %s. Revert buffer? "
|
|
4418 switch-string (mapconcat 'dired-make-switches-string
|
|
4419 bad-switches ", "))))
|
|
4420 (dired-sort-other switches nil nil)
|
|
4421 (error "Default switches unchanged. Remain as %s."
|
|
4422 (dired-make-switches-string dired-internal-switches)))
|
|
4423 (dired-sort-other switches t nil))))
|
|
4424 ((or (equal arg '(4)) (eq arg 'date) (eq arg 'name))
|
|
4425 ;; Toggle the entire buffer name/data.
|
|
4426 (let ((cursor-in-echo-area t)
|
|
4427 (switches (copy-sequence dired-internal-switches))
|
|
4428 (type (and (symbolp arg) arg))
|
|
4429 char)
|
|
4430 (while (null type)
|
|
4431 (message "Sort entire buffer according to (n)ame or (d)ate? ")
|
|
4432 (setq char (read-char)
|
|
4433 type (cond
|
|
4434 ((char-equal char ?d) 'date)
|
|
4435 ((char-equal char ?n) 'name)
|
|
4436 (t (message "Type one of n or d.") (sit-for 1) nil))))
|
|
4437 (mapcar (function
|
|
4438 (lambda (x)
|
|
4439 (setq switches
|
|
4440 (delq (car x) switches))))
|
|
4441 dired-sort-type-alist)
|
|
4442 (dired-sort-other
|
|
4443 (if (eq type 'date) (cons ?t switches) switches) nil nil)))
|
|
4444 ((equal arg '(16))
|
|
4445 ;; Edit the switches for the entire buffer.
|
|
4446 (dired-sort-other
|
|
4447 (dired-make-switches-list
|
|
4448 (read-string
|
|
4449 "Change ls switches for entire buffer to (must contain -l): "
|
|
4450 (dired-make-switches-string dired-internal-switches)))
|
|
4451 nil nil))
|
|
4452 (t
|
|
4453 ;; No idea what's going on.
|
|
4454 (error
|
|
4455 "Invalid prefix. See %s dired-sort-toggle-or-edit."
|
|
4456 (substitute-command-keys
|
|
4457 (if (featurep 'ehelp)
|
|
4458 "\\[electric-describe-function]"
|
|
4459 "\\[describe-function]"))))))
|
|
4460
|
|
4461 (defun dired-sort-other (switches &optional no-revert subdir)
|
|
4462 ;; Specify new ls SWITCHES for current dired buffer.
|
|
4463 ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
|
|
4464 ;; If subdir is non-nil, only changes the switches for the
|
|
4465 ;; sudirectory.
|
|
4466 (if subdir
|
|
4467 (let ((elt (assoc subdir dired-subdir-alist)))
|
|
4468 (if elt (setcar (nthcdr 3 elt) switches)))
|
|
4469 (setq dired-internal-switches switches))
|
|
4470 (or no-revert
|
|
4471 (cond
|
|
4472
|
|
4473 (subdir
|
|
4474 (let ((ofile (dired-get-filename nil t))
|
|
4475 (opoint (point)))
|
|
4476 (message "Relisting %s..." subdir)
|
|
4477 (dired-insert-subdir subdir switches)
|
|
4478 (message "Relisting %s... done" subdir)
|
|
4479 (or (and ofile (dired-goto-file ofile)) (goto-char opoint))))
|
|
4480
|
|
4481 ((memq ?R switches)
|
|
4482 ;; We are replacing a buffer with a giant recursive listing.
|
|
4483 (let ((opoint (point))
|
|
4484 (ofile (dired-get-filename nil t))
|
|
4485 (hidden-subdirs (dired-remember-hidden))
|
|
4486 (mark-alist (dired-remember-marks (point-min) (point-max)))
|
|
4487 (kill-files-p (save-excursion
|
|
4488 (goto-char (point))
|
|
4489 (search-forward
|
|
4490 (concat (char-to-string ?\r)
|
|
4491 (regexp-quote
|
|
4492 (char-to-string
|
|
4493 dired-kill-marker-char)))
|
|
4494 nil t)))
|
|
4495 (omit-files (nth 2 (nth (1- (length dired-subdir-alist))
|
|
4496 dired-subdir-alist)))
|
|
4497 buffer-read-only)
|
|
4498 (dired-readin dired-directory (current-buffer)
|
|
4499 (or (consp dired-directory)
|
|
4500 (null (file-directory-p dired-directory))))
|
|
4501 (dired-mark-remembered mark-alist) ; mark files that were marked
|
|
4502 (if kill-files-p (dired-do-hide dired-kill-marker-char))
|
|
4503 (if omit-files
|
|
4504 (dired-omit-expunge nil t))
|
|
4505 ;; hide subdirs that were hidden
|
|
4506 (save-excursion
|
|
4507 (mapcar (function (lambda (dir)
|
|
4508 (if (dired-goto-subdir dir)
|
|
4509 (dired-hide-subdir 1))))
|
|
4510 hidden-subdirs))
|
|
4511 ;; Try to get back to where we were
|
|
4512 (or (and ofile (dired-goto-file ofile))
|
|
4513 (goto-char opoint))
|
|
4514 (dired-move-to-filename)))
|
|
4515
|
|
4516 (t
|
|
4517 ;; Clear all switches in the subdir alist
|
|
4518 (setq dired-subdir-alist
|
|
4519 (mapcar (function
|
|
4520 (lambda (x)
|
|
4521 (setcar (nthcdr 3 x) nil)
|
|
4522 x))
|
|
4523 dired-subdir-alist))
|
|
4524 (revert-buffer nil t))))
|
|
4525 (dired-update-mode-line t))
|
|
4526
|
|
4527 (defun dired-compatible-switches-p (list1 list2)
|
|
4528 ;; Returns t if list1 and list2 are allowed as switches in the same
|
|
4529 ;; dired buffer.
|
|
4530 (and (eq (null (or (memq ?l list1) (memq ?o list1) (memq ?g list1)))
|
|
4531 (null (or (memq ?l list2) (memq ?o list2) (memq ?g list2))))
|
|
4532 (eq (null (memq ?F list1)) (null (memq ?F list2)))
|
|
4533 (eq (null (memq ?p list1)) (null (memq ?p list2)))
|
|
4534 (eq (null (memq ?b list1)) (null (memq ?b list2)))))
|
|
4535
|
|
4536 (defun dired-check-ls-l (&optional switches)
|
|
4537 ;; Check for long-style listings
|
|
4538 (let ((switches (or switches dired-internal-switches)))
|
|
4539 (or (memq ?l switches) (memq ?o switches) (memq ?g switches)
|
|
4540 (error "Dired needs -l, -o, or -g in ls switches"))))
|
|
4541
|
|
4542
|
|
4543 ;;;; --------------------------------------------------------------
|
|
4544 ;;;; Creating new files.
|
|
4545 ;;;; --------------------------------------------------------------
|
|
4546 ;;;
|
|
4547 ;;; The dired-create-files paradigm is used for copying, renaming,
|
|
4548 ;;; compressing, and making hard and soft links.
|
|
4549
|
|
4550 (defun dired-file-marker (file)
|
|
4551 ;; Return FILE's marker, or nil if unmarked.
|
|
4552 (save-excursion
|
|
4553 (and (dired-goto-file file)
|
|
4554 (progn
|
|
4555 (skip-chars-backward "^\n\r")
|
|
4556 (and (not (= ?\040 (following-char)))
|
|
4557 (following-char))))))
|
|
4558
|
|
4559 ;; The basic function for half a dozen variations on cp/mv/ln/ln -s.
|
|
4560 (defun dired-create-files (file-creator operation fn-list name-constructor
|
|
4561 &optional marker-char query
|
|
4562 implicit-to)
|
|
4563 ;; Create a new file for each from a list of existing files. The user
|
|
4564 ;; is queried, dired buffers are updated, and at the end a success or
|
|
4565 ;; failure message is displayed
|
|
4566
|
|
4567 ;; FILE-CREATOR must accept three args: oldfile newfile ok-if-already-exists
|
|
4568 ;; It is called for each file and must create newfile, the entry of
|
|
4569 ;; which will be added. The user will be queried if the file already
|
|
4570 ;; exists. If oldfile is removed by FILE-CREATOR (i.e, it is a
|
|
4571 ;; rename), it is FILE-CREATOR's responsibility to update dired
|
|
4572 ;; buffers. FILE-CREATOR must abort by signalling a file-error if it
|
|
4573 ;; could not create newfile. The error is caught and logged.
|
|
4574
|
|
4575 ;; OPERATION (a capitalized string, e.g. `Copy') describes the
|
|
4576 ;; operation performed. It is used for error logging.
|
|
4577
|
|
4578 ;; FN-LIST is the list of files to copy (full absolute pathnames).
|
|
4579
|
|
4580 ;; NAME-CONSTRUCTOR returns a newfile for every oldfile, or nil to
|
|
4581 ;; skip. If it skips files, it is supposed to tell why (using dired-log).
|
|
4582
|
|
4583 ;; Optional MARKER-CHAR is a character with which to mark every
|
|
4584 ;; newfile's entry, or t to use the current marker character if the
|
|
4585 ;; oldfile was marked.
|
|
4586
|
|
4587 ;; QUERY is a function to use to prompt the user about creating a file.
|
|
4588 ;; It accepts two args, the from and to files,
|
|
4589 ;; and must return nil or t. If QUERY is nil, then no user
|
|
4590 ;; confirmation will be requested.
|
|
4591
|
|
4592 ;; If IMPLICIT-TO is non-nil, then the file constructor does not take
|
|
4593 ;; a to-file arg. e.g. compress.
|
|
4594
|
|
4595 (let ((success-count 0)
|
|
4596 (total (length fn-list))
|
|
4597 failures skipped overwrite-query)
|
|
4598 ;; Fluid vars used for storing responses of previous queries must be
|
|
4599 ;; initialized.
|
|
4600 (dired-save-excursion
|
|
4601 (setq dired-overwrite-backup-query nil
|
|
4602 dired-file-creator-query nil)
|
|
4603 (mapcar
|
|
4604 (function
|
|
4605 (lambda (from)
|
|
4606 (let ((to (funcall name-constructor from)))
|
|
4607 (if to
|
|
4608 (if (equal to from)
|
|
4609 (progn
|
|
4610 (dired-log (buffer-name (current-buffer))
|
|
4611 "Cannot %s to same file: %s\n"
|
|
4612 (downcase operation) from)
|
|
4613 (setq skipped (cons (dired-make-relative from) skipped)))
|
|
4614 (if (or (null query)
|
|
4615 (funcall query from to))
|
|
4616 (let* ((overwrite (let (jka-compr-enabled)
|
|
4617 ;; Don't let jka-compr fool us.
|
|
4618 (file-exists-p to)))
|
|
4619 ;; for dired-handle-overwrite
|
|
4620 (dired-overwrite-confirmed
|
|
4621 (and overwrite
|
|
4622 (let ((help-form '(format "\
|
|
4623 Type SPC or `y' to overwrite file `%s',
|
|
4624 DEL or `n' to skip to next,
|
|
4625 ESC or `q' to not overwrite any of the remaining files,
|
|
4626 `!' to overwrite all remaining files with no more questions." to)))
|
|
4627 (dired-query 'overwrite-query
|
|
4628 "Overwrite %s?" to))))
|
|
4629 ;; must determine if FROM is marked before
|
|
4630 ;; file-creator gets a chance to delete it
|
|
4631 ;; (in case of a move).
|
|
4632 (actual-marker-char
|
|
4633 (cond ((integerp marker-char) marker-char)
|
|
4634 (marker-char (dired-file-marker from))
|
|
4635 (t nil))))
|
|
4636 (if (and overwrite (null dired-overwrite-confirmed))
|
|
4637 (setq skipped (cons (dired-make-relative from)
|
|
4638 skipped))
|
|
4639 (condition-case err
|
|
4640 (let ((dired-unhandle-add-files
|
|
4641 (cons to dired-unhandle-add-files)))
|
|
4642 (if implicit-to
|
|
4643 (funcall file-creator from
|
|
4644 dired-overwrite-confirmed)
|
|
4645 (funcall file-creator from to
|
|
4646 dired-overwrite-confirmed))
|
|
4647 (setq success-count (1+ success-count))
|
|
4648 (message "%s: %d of %d"
|
|
4649 operation success-count total)
|
|
4650 (dired-add-file to actual-marker-char))
|
|
4651 (file-error ; FILE-CREATOR aborted
|
|
4652 (progn
|
|
4653 (setq failures (cons (dired-make-relative from)
|
|
4654 failures))
|
|
4655 (dired-log (buffer-name (current-buffer))
|
|
4656 "%s `%s' to `%s' failed:\n%s\n"
|
|
4657 operation from to err))))))
|
|
4658 (setq skipped (cons (dired-make-relative from) skipped))))
|
|
4659 (setq skipped (cons (dired-make-relative from) skipped))))))
|
|
4660 fn-list)
|
|
4661 (cond
|
|
4662 (failures
|
|
4663 (dired-log-summary
|
|
4664 (buffer-name (current-buffer))
|
|
4665 (format "%s failed for %d of %d file%s"
|
|
4666 operation (length failures) total
|
|
4667 (dired-plural-s total)) failures))
|
|
4668 (skipped
|
|
4669 (dired-log-summary
|
|
4670 (buffer-name (current-buffer))
|
|
4671 (format "%s: %d of %d file%s skipped"
|
|
4672 operation (length skipped) total
|
|
4673 (dired-plural-s total)) skipped))
|
|
4674 (t
|
|
4675 (message "%s: %s file%s."
|
|
4676 operation success-count (dired-plural-s success-count)))))))
|
|
4677
|
|
4678 (defun dired-do-create-files (op-symbol file-creator operation arg
|
|
4679 &optional marker-char
|
|
4680 prompter how-to)
|
|
4681 ;; Create a new file for each marked file.
|
|
4682 ;; Prompts user for target, which is a directory in which to create
|
|
4683 ;; the new files. Target may be a plain file if only one marked
|
|
4684 ;; file exists.
|
|
4685 ;; OP-SYMBOL is the symbol for the operation. Function `dired-mark-pop-up'
|
|
4686 ;; will determine wether pop-ups are appropriate for this OP-SYMBOL.
|
|
4687 ;; FILE-CREATOR and OPERATION as in dired-create-files.
|
|
4688 ;; ARG as in dired-get-marked-files.
|
|
4689 ;; PROMPTER is a function of one-arg, the list of files, to return a prompt
|
|
4690 ;; to use for dired-read-file-name. If it is nil, then a default prompt
|
|
4691 ;; will be used.
|
|
4692 ;; Optional arg MARKER-CHAR as in dired-create-files.
|
|
4693 ;; Optional arg HOW-TO determines how to treat target:
|
|
4694 ;; If HOW-TO is not given (or nil), and target is a directory, the
|
|
4695 ;; file(s) are created inside the target directory. If target
|
|
4696 ;; is not a directory, there must be exactly one marked file,
|
|
4697 ;; else error.
|
|
4698 ;; If HOW-TO is t, then target is not modified. There must be
|
|
4699 ;; exactly one marked file, else error.
|
|
4700 ;; Else HOW-TO is assumed to be a function of one argument, target,
|
|
4701 ;; that looks at target and returns a value for the into-dir
|
|
4702 ;; variable. The function dired-into-dir-with-symlinks is provided
|
|
4703 ;; for the case (common when creating symlinks) that symbolic
|
|
4704 ;; links to directories are not to be considered as directories
|
|
4705 ;; (as file-directory-p would if HOW-TO had been nil).
|
|
4706
|
|
4707 (let* ((fn-list (dired-get-marked-files nil arg))
|
|
4708 (fn-count (length fn-list))
|
|
4709 (cdir (dired-current-directory))
|
|
4710 (target (expand-file-name
|
|
4711 (dired-mark-read-file-name
|
|
4712 (if prompter
|
|
4713 (funcall prompter fn-list)
|
|
4714 (concat operation " %s to: "))
|
|
4715 (dired-dwim-target-directory)
|
|
4716 op-symbol arg (mapcar (function
|
|
4717 (lambda (fn)
|
|
4718 (dired-make-relative fn cdir t)))
|
|
4719 fn-list))))
|
|
4720 (into-dir (cond ((null how-to) (file-directory-p target))
|
|
4721 ((eq how-to t) nil)
|
|
4722 (t (funcall how-to target)))))
|
|
4723 (if (and (> fn-count 1)
|
|
4724 (not into-dir))
|
|
4725 (error "Marked %s: target must be a directory: %s" operation target))
|
|
4726 ;; rename-file bombs when moving directories unless we do this:
|
|
4727 (or into-dir (setq target (directory-file-name target)))
|
|
4728 (dired-create-files
|
|
4729 file-creator operation fn-list
|
|
4730 (if into-dir ; target is a directory
|
|
4731 (list 'lambda '(from)
|
|
4732 (list 'expand-file-name '(file-name-nondirectory from) target))
|
|
4733 (list 'lambda '(from) target))
|
|
4734 marker-char)))
|
|
4735
|
|
4736 (defun dired-into-dir-with-symlinks (target)
|
|
4737 (and (file-directory-p target)
|
|
4738 (not (file-symlink-p target))))
|
|
4739 ;; This may not always be what you want, especially if target is your
|
|
4740 ;; home directory and it happens to be a symbolic link, as is often the
|
|
4741 ;; case with NFS and automounters. Or if you want to make symlinks
|
|
4742 ;; into directories that themselves are only symlinks, also quite
|
|
4743 ;; common.
|
|
4744 ;; So we don't use this function as value for HOW-TO in
|
|
4745 ;; dired-do-symlink, which has the minor disadvantage of
|
|
4746 ;; making links *into* a symlinked-dir, when you really wanted to
|
|
4747 ;; *overwrite* that symlink. In that (rare, I guess) case, you'll
|
|
4748 ;; just have to remove that symlink by hand before making your marked
|
|
4749 ;; symlinks.
|
|
4750
|
|
4751 (defun dired-handle-overwrite (to)
|
|
4752 ;; Save old version of a to be overwritten file TO.
|
|
4753 ;; `dired-overwrite-confirmed' and `dired-overwrite-backup-query'
|
|
4754 ;; are fluid vars from dired-create-files.
|
|
4755 (if (and dired-backup-if-overwrite
|
|
4756 dired-overwrite-confirmed
|
|
4757 (or (eq 'always dired-backup-if-overwrite)
|
|
4758 (dired-query 'dired-overwrite-backup-query
|
|
4759 (format "Make backup for existing file `%s'? " to))))
|
|
4760 (let ((backup (car (find-backup-file-name to))))
|
|
4761 (rename-file to backup 0)))) ; confirm overwrite of old backup
|
|
4762
|
|
4763 (defun dired-dwim-target-directory ()
|
|
4764 ;; Try to guess which target directory the user may want.
|
|
4765 ;; If there is a dired buffer displayed in the next window, use
|
|
4766 ;; its current subdir, else use current subdir of this dired buffer.
|
|
4767 ;; non-dired buffer may want to profit from this function, e.g. vm-uudecode
|
|
4768 (let* ((this-dir (and (eq major-mode 'dired-mode)
|
|
4769 (dired-current-directory)))
|
|
4770 (dwimmed
|
|
4771 (if dired-dwim-target
|
|
4772 (let* ((other-buf (window-buffer (next-window)))
|
|
4773 (other-dir (save-excursion
|
|
4774 (set-buffer other-buf)
|
|
4775 (and (eq major-mode 'dired-mode)
|
|
4776 (dired-current-directory)))))
|
|
4777 (or other-dir this-dir))
|
|
4778 this-dir)))
|
|
4779 (and dwimmed (dired-abbreviate-file-name dwimmed))))
|
|
4780
|
|
4781 (defun dired-get-target-directory ()
|
|
4782 "Writes a copy of the current subdirectory into an active minibuffer."
|
|
4783 (interactive)
|
|
4784 (let ((mb (dired-get-active-minibuffer-window)))
|
|
4785 (if mb
|
|
4786 (let ((dir (dired-current-directory)))
|
|
4787 (select-window mb)
|
|
4788 (set-buffer (window-buffer mb))
|
|
4789 (erase-buffer)
|
|
4790 (insert dir))
|
|
4791 (error "No active minibuffer"))))
|
|
4792
|
|
4793 ;;; Copying files
|
|
4794
|
|
4795 (defun dired-do-copy (&optional arg)
|
|
4796 "Copy all marked (or next ARG) files, or copy the current file.
|
|
4797 When operating on just the current file, you specify the new name.
|
|
4798 When operating on multiple or marked files, you specify a directory
|
|
4799 and the files are copied into that directory, retaining the same file names.
|
|
4800
|
|
4801 A zero prefix argument copies nothing. But it toggles the
|
|
4802 variable `dired-copy-preserve-time' (which see)."
|
|
4803 (interactive "P")
|
|
4804 (if (not (zerop (prefix-numeric-value arg)))
|
|
4805 (dired-do-create-files 'copy (function dired-copy-file)
|
|
4806 (if dired-copy-preserve-time "Copy [-p]" "Copy")
|
|
4807 arg dired-keep-marker-copy)
|
|
4808 (setq dired-copy-preserve-time (not dired-copy-preserve-time))
|
|
4809 (if dired-copy-preserve-time
|
|
4810 (message "Copy will preserve time.")
|
|
4811 (message "Copied files will get current date."))))
|
|
4812
|
|
4813 (defun dired-copy-file (from to ok-flag)
|
|
4814 (dired-handle-overwrite to)
|
|
4815 (copy-file from to ok-flag dired-copy-preserve-time))
|
|
4816
|
|
4817 ;;; Renaming/moving files
|
|
4818
|
|
4819 (defun dired-do-rename (&optional arg)
|
|
4820 "Rename current file or all marked (or next ARG) files.
|
|
4821 When renaming just the current file, you specify the new name.
|
|
4822 When renaming multiple or marked files, you specify a directory.
|
|
4823
|
|
4824 A zero ARG moves no files but toggles `dired-dwim-target' (which see)."
|
|
4825 (interactive "P")
|
|
4826 (if (not (zerop (prefix-numeric-value arg)))
|
|
4827 (dired-do-create-files 'move (function dired-rename-file)
|
|
4828 "Move" arg dired-keep-marker-rename
|
|
4829 (function
|
|
4830 (lambda (list)
|
|
4831 (if (= (length list) 1)
|
|
4832 "Rename %s to: "
|
|
4833 "Move %s to: "))))
|
|
4834 (setq dired-dwim-target (not dired-dwim-target))
|
|
4835 (message "dired-dwim-target is %s." (if dired-dwim-target "ON" "OFF"))))
|
|
4836
|
|
4837 (defun dired-rename-file (from to ok-flag)
|
|
4838 (dired-handle-overwrite to)
|
|
4839 (let ((insert (assoc (file-name-as-directory from) dired-subdir-alist)))
|
|
4840 (rename-file from to ok-flag) ; error is caught in -create-files
|
|
4841 ;; Silently rename the visited file of any buffer visiting this file.
|
|
4842 (dired-rename-update-buffers from to insert)))
|
|
4843
|
|
4844 (defun dired-rename-update-buffers (from to &optional insert)
|
|
4845 (if (get-file-buffer from)
|
|
4846 (save-excursion
|
|
4847 (set-buffer (get-file-buffer from))
|
|
4848 (let ((modflag (buffer-modified-p)))
|
|
4849 (set-visited-file-name to) ; kills write-file-hooks
|
|
4850 (set-buffer-modified-p modflag)))
|
|
4851 ;; It's a directory. More work to do.
|
|
4852 (let ((blist (buffer-list))
|
|
4853 (from-dir (file-name-as-directory from))
|
|
4854 (to-dir (file-name-as-directory to)))
|
|
4855 (save-excursion
|
|
4856 (while blist
|
|
4857 (set-buffer (car blist))
|
|
4858 (setq blist (cdr blist))
|
|
4859 (cond
|
|
4860 (buffer-file-name
|
|
4861 (if (dired-in-this-tree buffer-file-name from-dir)
|
|
4862 (let ((modflag (buffer-modified-p)))
|
|
4863 (unwind-protect
|
|
4864 (set-visited-file-name
|
|
4865 (concat to-dir (substring buffer-file-name
|
|
4866 (length from-dir))))
|
|
4867 (set-buffer-modified-p modflag)))))
|
|
4868 (dired-directory
|
|
4869 (if (string-equal from-dir (expand-file-name default-directory))
|
|
4870 ;; If top level directory was renamed, lots of things
|
|
4871 ;; have to be updated.
|
|
4872 (progn
|
|
4873 (dired-unadvertise from-dir)
|
|
4874 (setq default-directory to-dir
|
|
4875 dired-directory
|
|
4876 ;; Need to beware of wildcards.
|
|
4877 (expand-file-name
|
|
4878 (file-name-nondirectory dired-directory)
|
|
4879 to-dir))
|
|
4880 (let ((new-name (file-name-nondirectory
|
|
4881 (directory-file-name dired-directory))))
|
|
4882 ;; Try to rename buffer, but just leave old name if new
|
|
4883 ;; name would already exist (don't try appending "<%d>")
|
|
4884 ;; Why? --sandy 19-8-94
|
|
4885 (or (get-buffer new-name)
|
|
4886 (rename-buffer new-name)))
|
|
4887 (dired-advertise))
|
|
4888 (and insert
|
|
4889 (assoc (file-name-directory (directory-file-name to))
|
|
4890 dired-subdir-alist)
|
|
4891 (dired-insert-subdir to))))))))))
|
|
4892
|
|
4893 ;;; Making symbolic links
|
|
4894
|
|
4895 (defun dired-do-symlink (&optional arg)
|
|
4896 "Make symbolic links to current file or all marked (or next ARG) files.
|
|
4897 When operating on just the current file, you specify the new name.
|
|
4898 When operating on multiple or marked files, you specify a directory
|
|
4899 and new symbolic links are made in that directory
|
|
4900 with the same names that the files currently have."
|
|
4901 (interactive "P")
|
|
4902 (dired-do-create-files 'symlink (function make-symbolic-link)
|
|
4903 "SymLink" arg dired-keep-marker-symlink))
|
|
4904
|
|
4905 ;; Relative symlinks:
|
|
4906 ;; make-symbolic no longer expands targets (as of at least 18.57),
|
|
4907 ;; so the code to call ln has been removed.
|
|
4908
|
|
4909 (defun dired-do-relsymlink (&optional arg)
|
|
4910 "Symlink all marked (or next ARG) files into a directory,
|
|
4911 or make a symbolic link to the current file.
|
|
4912 This creates relative symbolic links like
|
|
4913
|
|
4914 foo -> ../bar/foo
|
|
4915
|
|
4916 not absolute ones like
|
|
4917
|
|
4918 foo -> /ugly/path/that/may/change/any/day/bar/foo"
|
|
4919 (interactive "P")
|
|
4920 (dired-do-create-files 'relsymlink (function dired-make-relative-symlink)
|
|
4921 "RelSymLink" arg dired-keep-marker-symlink))
|
|
4922
|
|
4923 (defun dired-make-relative-symlink (target linkname
|
|
4924 &optional ok-if-already-exists)
|
|
4925 "Make a relative symbolic link pointing to TARGET with name LINKNAME.
|
|
4926 Three arguments: FILE1 FILE2 &optional OK-IF-ALREADY-EXISTS
|
|
4927 The link is relative (if possible), for example
|
|
4928
|
|
4929 \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
|
|
4930
|
|
4931 results in
|
|
4932
|
|
4933 \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
|
|
4934 (interactive
|
|
4935 (let ((target (read-string "Make relative symbolic link to file: ")))
|
|
4936 (list
|
|
4937 target
|
|
4938 (read-file-name (format "Make relsymlink to file %s: " target))
|
|
4939 0)))
|
|
4940 (let* ((target (expand-file-name target))
|
|
4941 (linkname (expand-file-name linkname))
|
|
4942 (handler (or (find-file-name-handler
|
|
4943 linkname 'dired-make-relative-symlink)
|
|
4944 (find-file-name-handler
|
|
4945 target 'dired-make-relative-symlink))))
|
|
4946 (if handler
|
|
4947 (funcall handler 'dired-make-relative-symlink target linkname
|
|
4948 ok-if-already-exists)
|
|
4949 (setq target (directory-file-name target)
|
|
4950 linkname (directory-file-name linkname))
|
|
4951 (make-symbolic-link
|
|
4952 (dired-make-relative target (file-name-directory linkname) t)
|
|
4953 linkname ok-if-already-exists))))
|
|
4954
|
|
4955 ;;; Hard links -- adding names to files
|
|
4956
|
|
4957 (defun dired-do-hardlink (&optional arg)
|
|
4958 "Add names (hard links) current file or all marked (or next ARG) files.
|
|
4959 When operating on just the current file, you specify the new name.
|
|
4960 When operating on multiple or marked files, you specify a directory
|
|
4961 and new hard links are made in that directory
|
|
4962 with the same names that the files currently have."
|
|
4963 (interactive "P")
|
|
4964 (dired-do-create-files 'hardlink (function add-name-to-file)
|
|
4965 "HardLink" arg dired-keep-marker-hardlink))
|
|
4966
|
|
4967
|
|
4968 ;;;; ---------------------------------------------------------------
|
|
4969 ;;;; Running process on marked files
|
|
4970 ;;;; ---------------------------------------------------------------
|
|
4971 ;;;
|
|
4972 ;;; Commands for shell processes are in dired-shell.el.
|
|
4973
|
|
4974 ;;; Internal functions for running subprocesses,
|
|
4975 ;;; checking and logging of their errors.
|
|
4976
|
|
4977 (defun dired-call-process (program discard &rest arguments)
|
|
4978 ;; Run PROGRAM with output to current buffer unless DISCARD is t.
|
|
4979 ;; Remaining arguments are strings passed as command arguments to PROGRAM.
|
|
4980 ;; Returns program's exit status, as an integer.
|
|
4981 ;; This is a separate function so that efs can redefine it.
|
|
4982 (let ((return
|
|
4983 (apply 'call-process program nil (not discard) nil arguments)))
|
|
4984 (if (and (not (equal shell-file-name program))
|
|
4985 (integerp return))
|
|
4986 return
|
|
4987 ;; Fudge return code by looking for errors in current buffer.
|
|
4988 (if (zerop (buffer-size)) 0 1))))
|
|
4989
|
|
4990 (defun dired-check-process (msg program &rest arguments)
|
|
4991 ;; Display MSG while running PROGRAM, and check for output.
|
|
4992 ;; Remaining arguments are strings passed as command arguments to PROGRAM.
|
|
4993 ;; On error, insert output in a log buffer and return the
|
|
4994 ;; offending ARGUMENTS or PROGRAM.
|
|
4995 ;; Caller can cons up a list of failed args.
|
|
4996 ;; Else returns nil for success.
|
|
4997 (let ((err-buffer (get-buffer-create " *dired-check-process output*"))
|
|
4998 (dir default-directory))
|
|
4999 (message "%s..." msg)
|
|
5000 (save-excursion
|
|
5001 ;; Get a clean buffer for error output:
|
|
5002 (set-buffer err-buffer)
|
|
5003 (erase-buffer)
|
|
5004 (setq default-directory dir) ; caller's default-directory
|
|
5005 (if (not
|
|
5006 (eq 0 (apply (function dired-call-process) program nil arguments)))
|
|
5007 (progn
|
|
5008 (dired-log (buffer-name (current-buffer))
|
|
5009 (concat program " " (prin1-to-string arguments) "\n"))
|
|
5010 (dired-log (buffer-name (current-buffer)) err-buffer)
|
|
5011 (or arguments program t))
|
|
5012 (kill-buffer err-buffer)
|
|
5013 (message "%s...done" msg)
|
|
5014 nil))))
|
|
5015
|
|
5016 ;;; Changing file attributes
|
|
5017
|
|
5018 (defun dired-do-chxxx (attribute-name program op-symbol arg)
|
|
5019 ;; Change file attributes (mode, group, owner) of marked files and
|
|
5020 ;; refresh their file lines.
|
|
5021 ;; ATTRIBUTE-NAME is a string describing the attribute to the user.
|
|
5022 ;; PROGRAM is the program used to change the attribute.
|
|
5023 ;; OP-SYMBOL is the type of operation (for use in dired-mark-pop-up).
|
|
5024 ;; ARG describes which files to use, like in dired-get-marked-files.
|
|
5025 (let* ((files (dired-get-marked-files t arg))
|
|
5026 (new-attribute
|
|
5027 (dired-mark-read-string
|
|
5028 (concat "Change " attribute-name " of %s to: ")
|
|
5029 nil op-symbol arg files))
|
|
5030 (operation (concat program " " new-attribute))
|
|
5031 (failures
|
|
5032 (dired-bunch-files 10000 (function dired-check-process)
|
|
5033 (list operation program new-attribute)
|
|
5034 files)))
|
|
5035 (dired-do-redisplay arg);; moves point if ARG is an integer
|
|
5036 (if failures
|
|
5037 (dired-log-summary (buffer-name (current-buffer))
|
|
5038 (format "%s: error" operation) nil))))
|
|
5039
|
|
5040 (defun dired-do-chmod (&optional arg)
|
|
5041 "Change the mode of the marked (or next ARG) files.
|
|
5042 This calls chmod, thus symbolic modes like `g+w' are allowed."
|
|
5043 (interactive "P")
|
|
5044 (dired-do-chxxx "Mode" "chmod" 'chmod arg))
|
|
5045
|
|
5046 (defun dired-do-chgrp (&optional arg)
|
|
5047 "Change the group of the marked (or next ARG) files."
|
|
5048 (interactive "P")
|
|
5049 (dired-do-chxxx "Group" "chgrp" 'chgrp arg))
|
|
5050
|
|
5051 (defun dired-do-chown (&optional arg)
|
|
5052 "Change the owner of the marked (or next ARG) files."
|
|
5053 (interactive "P")
|
|
5054 (dired-do-chxxx "Owner" dired-chown-program 'chown arg))
|
|
5055
|
|
5056 ;;; Utilities for running processes on marked files.
|
|
5057
|
|
5058 ;; Process all the files in FILES in batches of a convenient size,
|
|
5059 ;; by means of (FUNCALL FUNCTION ARGS... SOME-FILES...).
|
|
5060 ;; Batches are chosen to need less than MAX chars for the file names,
|
|
5061 ;; allowing 3 extra characters of separator per file name.
|
|
5062 (defun dired-bunch-files (max function args files)
|
|
5063 (let (pending
|
|
5064 (pending-length 0)
|
|
5065 failures)
|
|
5066 ;; Accumulate files as long as they fit in MAX chars,
|
|
5067 ;; then process the ones accumulated so far.
|
|
5068 (while files
|
|
5069 (let* ((thisfile (car files))
|
|
5070 (thislength (+ (length thisfile) 3))
|
|
5071 (rest (cdr files)))
|
|
5072 ;; If we have at least 1 pending file
|
|
5073 ;; and this file won't fit in the length limit, process now.
|
|
5074 (if (and pending (> (+ thislength pending-length) max))
|
|
5075 (setq failures
|
|
5076 (nconc (apply function (append args pending))
|
|
5077 failures)
|
|
5078 pending nil
|
|
5079 pending-length 0))
|
|
5080 ;; Do (setq pending (cons thisfile pending))
|
|
5081 ;; but reuse the cons that was in `files'.
|
|
5082 (setcdr files pending)
|
|
5083 (setq pending files)
|
|
5084 (setq pending-length (+ thislength pending-length))
|
|
5085 (setq files rest)))
|
|
5086 (nconc (apply function (append args pending))
|
|
5087 failures)))
|
|
5088
|
|
5089
|
|
5090 ;;;; ---------------------------------------------------------------
|
|
5091 ;;;; Calculating data or properties for marked files.
|
|
5092 ;;;; ---------------------------------------------------------------
|
|
5093
|
|
5094 (defun dired-do-total-size (&optional arg)
|
|
5095 "Show total size of all marked (or next ARG) files."
|
|
5096 (interactive "P")
|
|
5097 (let* ((result (dired-map-over-marks (dired-get-file-size) arg))
|
|
5098 (total (apply (function +) result))
|
|
5099 (num (length result)))
|
|
5100 (message "%d bytes (%d kB) in %s file%s"
|
|
5101 total (/ total 1024) num (dired-plural-s num))
|
|
5102 total))
|
|
5103
|
|
5104 (defun dired-get-file-size ()
|
|
5105 ;; Returns the file size in bytes of the current file, as an integer.
|
|
5106 ;; Assumes that it is on a valid file line. It's the caller's responsibility
|
|
5107 ;; to ensure this. Assumes that match 0 for dired-re-month-and-time is
|
|
5108 ;; at the end of the file size.
|
|
5109 (dired-move-to-filename t)
|
|
5110 ;; dired-move-to-filename must leave match-beginning 0 at the start of
|
|
5111 ;; the date.
|
|
5112 (goto-char (match-beginning 0))
|
|
5113 (skip-chars-backward " ")
|
|
5114 (string-to-int (buffer-substring (point)
|
|
5115 (progn (skip-chars-backward "0-9")
|
|
5116 (point)))))
|
|
5117
|
|
5118 (defun dired-copy-filenames-as-kill (&optional arg)
|
|
5119 "Copy names of marked (or next ARG) files into the kill ring.
|
|
5120 The names are separated by a space, and may be copied into other buffers
|
|
5121 with \\[yank]. The list of names is also stored in the variable
|
|
5122 `dired-marked-files' for possible manipulation in the *scratch* buffer.
|
|
5123
|
|
5124 With a 0 prefix argument, use the pathname relative to the top-level dired
|
|
5125 directory for each marked file.
|
|
5126
|
|
5127 With a prefix \\[universal-argument], use the complete pathname of each
|
|
5128 marked file.
|
|
5129
|
|
5130 With a prefix \\[universal-argument] \\[universal-argument], copy the complete
|
|
5131 file line. In this case, the lines are separated by newlines.
|
|
5132
|
|
5133 If on a subdirectory headerline and no prefix argument given, use the
|
|
5134 subdirectory name instead."
|
|
5135 (interactive "P")
|
|
5136 (let (res)
|
|
5137 (cond
|
|
5138 ((and (null arg) (setq res (dired-get-subdir)))
|
|
5139 (kill-new res)
|
|
5140 (message "Copied %s into kill ring." res))
|
|
5141 ((equal arg '(16))
|
|
5142 (setq dired-marked-files
|
|
5143 (dired-map-over-marks
|
|
5144 (concat " " ; Don't copy the mark.
|
|
5145 (buffer-substring
|
|
5146 (progn (beginning-of-line) (1+ (point)))
|
|
5147 (progn (skip-chars-forward "^\n\r") (point))))
|
|
5148 nil))
|
|
5149 (let ((len (length dired-marked-files)))
|
|
5150 (kill-new (concat
|
|
5151 (mapconcat 'identity dired-marked-files "\n")
|
|
5152 "\n"))
|
|
5153 (message "Copied %d file line%s into kill ring."
|
|
5154 len (dired-plural-s len))))
|
|
5155 (t
|
|
5156 (setq dired-marked-files
|
|
5157 (cond
|
|
5158 ((null arg)
|
|
5159 (dired-get-marked-files 'no-dir))
|
|
5160 ((eq arg 0)
|
|
5161 (dired-get-marked-files t))
|
|
5162 ((integerp arg)
|
|
5163 (dired-get-marked-files 'no-dir arg))
|
|
5164 ((equal arg '(4))
|
|
5165 (dired-get-marked-files))
|
|
5166 (t (error "Invalid prefix %s" arg))))
|
|
5167 (let ((len (length dired-marked-files)))
|
|
5168 (kill-new (mapconcat 'identity dired-marked-files " "))
|
|
5169 (message "Copied %d file name%s into kill ring."
|
|
5170 len (dired-plural-s len)))))))
|
|
5171
|
|
5172
|
|
5173 ;;;; -----------------------------------------------------------
|
|
5174 ;;;; Killing subdirectories
|
|
5175 ;;;; -----------------------------------------------------------
|
|
5176 ;;;
|
|
5177 ;;; These commands actually remove text from the dired buffer.
|
|
5178
|
|
5179 (defun dired-kill-subdir (&optional remember-marks tree)
|
|
5180 "Remove all lines of current subdirectory.
|
|
5181 Lower levels are unaffected. If given a prefix when called interactively,
|
|
5182 kills the entire directory tree below the current subdirectory."
|
|
5183 ;; With optional REMEMBER-MARKS, return a mark-alist.
|
|
5184 (interactive (list nil current-prefix-arg))
|
|
5185 (let ((cur-dir (dired-current-directory)))
|
|
5186 (if (string-equal cur-dir (expand-file-name default-directory))
|
|
5187 (error "Attempt to kill top level directory"))
|
|
5188 (if tree
|
|
5189 (dired-kill-tree cur-dir remember-marks)
|
|
5190 (let ((beg (dired-subdir-min))
|
|
5191 (end (dired-subdir-max))
|
|
5192 buffer-read-only)
|
|
5193 (prog1
|
|
5194 (if remember-marks (dired-remember-marks beg end))
|
|
5195 (goto-char beg)
|
|
5196 (or (bobp) (forward-char -1)) ; gobble separator
|
|
5197 (delete-region (point) end)
|
|
5198 (dired-unsubdir cur-dir)
|
|
5199 (dired-update-mode-line)
|
|
5200 (dired-update-mode-line-modified t))))))
|
|
5201
|
|
5202 (defun dired-kill-tree (dirname &optional remember-marks)
|
|
5203 "Kill all proper subdirs of DIRNAME, excluding DIRNAME itself.
|
|
5204 With optional arg REMEMBER-MARKS, return an alist of marked files."
|
|
5205 (interactive "DKill tree below directory: ")
|
|
5206 (let ((s-alist dired-subdir-alist) dir m-alist)
|
|
5207 (while s-alist
|
|
5208 (setq dir (car (car s-alist))
|
|
5209 s-alist (cdr s-alist))
|
|
5210 (if (and (not (string-equal dir dirname))
|
|
5211 (dired-in-this-tree dir dirname)
|
|
5212 (dired-goto-subdir dir))
|
|
5213 (setq m-alist (nconc (dired-kill-subdir remember-marks) m-alist))))
|
|
5214 (dired-update-mode-line)
|
|
5215 (dired-update-mode-line-modified t)
|
|
5216 m-alist))
|
|
5217
|
|
5218
|
|
5219 ;;;; ------------------------------------------------------------
|
|
5220 ;;;; Killing file lines
|
|
5221 ;;;; ------------------------------------------------------------
|
|
5222 ;;;
|
|
5223 ;;; Uses selective diplay, rather than removing lines from the buffer.
|
|
5224
|
|
5225 (defun dired-do-kill-file-lines (&optional arg)
|
|
5226 "Kill all marked file lines, or those indicated by the prefix argument.
|
|
5227 Killing file lines means hiding them with selective display. Giving
|
|
5228 a zero prefix redisplays all killed file lines."
|
|
5229 (interactive "P")
|
|
5230 (or selective-display
|
|
5231 (error "selective-display must be t for file line killing to work!"))
|
|
5232 (if (eq arg 0)
|
|
5233 (dired-do-unhide dired-kill-marker-char
|
|
5234 "Successfully resuscitated %d file line%s."
|
|
5235 dired-keep-marker-kill)
|
|
5236 (let ((files
|
|
5237 (length
|
|
5238 (dired-map-over-marks
|
|
5239 (progn
|
|
5240 (beginning-of-line)
|
|
5241 (subst-char-in-region (1- (point)) (point) ?\n ?\r)
|
|
5242 (dired-substitute-marker (point) (following-char)
|
|
5243 dired-kill-marker-char)
|
|
5244 (dired-update-marker-counters dired-marker-char t)
|
|
5245 t)
|
|
5246 arg))))
|
|
5247 ;; Beware of extreme apparent save-excursion lossage here.
|
|
5248 (let ((opoint (point)))
|
|
5249 (skip-chars-backward "^\n\r")
|
|
5250 (if (= (preceding-char) ?\n)
|
|
5251 (goto-char opoint)
|
|
5252 (setq opoint (- opoint (point)))
|
|
5253 (beginning-of-line)
|
|
5254 (skip-chars-forward "^\n\r" (+ (point) opoint))))
|
|
5255 (dired-update-mode-line-modified)
|
|
5256 (message "Killed %d file line%s." files (dired-plural-s files)))))
|
|
5257
|
|
5258
|
|
5259 ;;;; ----------------------------------------------------------------
|
|
5260 ;;;; Omitting files.
|
|
5261 ;;;; ----------------------------------------------------------------
|
|
5262
|
|
5263 ;; Marked files are never omitted.
|
|
5264 ;; Adapted from code submitted by:
|
|
5265 ;; Michael D. Ernst, mernst@theory.lcs.mit.edu, 1/11/91
|
|
5266 ;; Changed to work with selective display by Sandy Rutherford, 13/12/92.
|
|
5267 ;; For historical reasons, we still use the term expunge, although nothing
|
|
5268 ;; is expunged from the buffer.
|
|
5269
|
|
5270 (defun dired-omit-toggle (&optional arg)
|
|
5271 "Toggle between displaying and omitting files matching
|
|
5272 `dired-omit-files-regexp' in the current subdirectory.
|
|
5273 With a positive prefix, omits files in the entire tree dired buffer.
|
|
5274 With a negative prefix, forces all files in the tree dired buffer to be
|
|
5275 displayed."
|
|
5276 (interactive "P")
|
|
5277 (if arg
|
|
5278 (let ((arg (prefix-numeric-value arg)))
|
|
5279 (if (>= arg 0)
|
|
5280 (dired-omit-expunge nil t)
|
|
5281 (dired-do-unhide dired-omit-marker-char "")
|
|
5282 (mapcar
|
|
5283 (function
|
|
5284 (lambda (elt)
|
|
5285 (setcar (nthcdr 2 elt) nil)))
|
|
5286 dired-subdir-alist)))
|
|
5287 (if (dired-current-subdir-omitted-p)
|
|
5288 (save-restriction
|
|
5289 (narrow-to-region (dired-subdir-min) (dired-subdir-max))
|
|
5290 (dired-do-unhide dired-omit-marker-char "")
|
|
5291 (setcar (nthcdr 2 (assoc
|
|
5292 (dired-current-directory) dired-subdir-alist))
|
|
5293 nil)
|
|
5294 (setq dired-subdir-omit nil))
|
|
5295 (dired-omit-expunge)
|
|
5296 (setq dired-subdir-omit t)))
|
|
5297 (dired-update-mode-line t))
|
|
5298
|
|
5299 (defun dired-current-subdir-omitted-p ()
|
|
5300 ;; Returns t if the current subdirectory is omited.
|
|
5301 (nth 2 (assoc (dired-current-directory) dired-subdir-alist)))
|
|
5302
|
|
5303 (defun dired-remember-omitted ()
|
|
5304 ;; Returns a list of omitted subdirs.
|
|
5305 (let ((alist dired-subdir-alist)
|
|
5306 result elt)
|
|
5307 (while alist
|
|
5308 (setq elt (car alist)
|
|
5309 alist (cdr alist))
|
|
5310 (if (nth 2 elt)
|
|
5311 (setq result (cons (car elt) result))))
|
|
5312 result))
|
|
5313
|
|
5314 (defun dired-omit-expunge (&optional regexp full-buffer)
|
|
5315 ;; Hides all unmarked files matching REGEXP.
|
|
5316 ;; If REGEXP is nil or not specified, uses `dired-omit-files-regexp',
|
|
5317 ;; and also omits filenames ending in `dired-omit-extensions'.
|
|
5318 ;; If REGEXP is the empty string, this function is a no-op.
|
|
5319 (let ((omit-re (or regexp (dired-omit-regexp)))
|
|
5320 (alist dired-subdir-alist)
|
|
5321 elt min)
|
|
5322 (if (null omit-re)
|
|
5323 0
|
|
5324 (if full-buffer
|
|
5325 (prog1
|
|
5326 (dired-omit-region (point-min) (point-max) omit-re)
|
|
5327 ;; Set omit property in dired-subdir-alist
|
|
5328 (while alist
|
|
5329 (setq elt (car alist)
|
|
5330 min (dired-get-subdir-min elt)
|
|
5331 alist (cdr alist))
|
|
5332 (if (and (<= (point-min) min) (>= (point-max) min))
|
|
5333 (setcar (nthcdr 2 elt) t))))
|
|
5334 (prog1
|
|
5335 (dired-omit-region (dired-subdir-min) (dired-subdir-max) omit-re)
|
|
5336 (setcar
|
|
5337 (nthcdr 2 (assoc (dired-current-directory)
|
|
5338 dired-subdir-alist))
|
|
5339 t))))))
|
|
5340
|
|
5341 (defun dired-omit-region (start end regexp)
|
|
5342 ;; Omits files matching regexp in region. Returns count.
|
|
5343 (save-restriction
|
|
5344 (narrow-to-region start end)
|
|
5345 (let ((hidden-subdirs (dired-remember-hidden))
|
|
5346 buffer-read-only count)
|
|
5347 (or selective-display
|
|
5348 (error "selective-display must be t for file omission to work!"))
|
|
5349 (dired-omit-unhide-region start end)
|
|
5350 (let ((dired-marker-char dired-omit-marker-char)
|
|
5351 ;; since all subdirs are now unhidden, this fakes
|
|
5352 ;; dired-move-to-end-of-filename into working faster
|
|
5353 (selective-display nil))
|
|
5354 (or dired-omit-silent
|
|
5355 dired-in-query (message "Omitting..."))
|
|
5356 (if (dired-mark-unmarked-files regexp nil nil 'no-dir)
|
|
5357 (setq count (dired-do-hide
|
|
5358 dired-marker-char
|
|
5359 (and (memq dired-omit-silent '(nil 0))
|
|
5360 (not dired-in-query)
|
|
5361 "Omitted %d line%s.")))
|
|
5362 (or dired-omit-silent dired-in-query
|
|
5363 (message "(Nothing to omit)"))))
|
|
5364 (save-excursion ;hide subdirs that were hidden
|
|
5365 (mapcar (function (lambda (dir)
|
|
5366 (if (dired-goto-subdir dir)
|
|
5367 (dired-hide-subdir 1))))
|
|
5368 hidden-subdirs))
|
|
5369 count)))
|
|
5370
|
|
5371 (defun dired-omit-unhide-region (beg end)
|
|
5372 ;; Unhides hidden, but not marked files in the region.
|
|
5373 (save-excursion
|
|
5374 (save-restriction
|
|
5375 (narrow-to-region beg end)
|
|
5376 (goto-char (point-min))
|
|
5377 (while (search-forward "\r" nil t)
|
|
5378 (and (char-equal (following-char) ?\ )
|
|
5379 (subst-char-in-region (1- (point)) (point) ?\r ?\n))))))
|
|
5380
|
|
5381 (defun dired-do-unhide (char &optional fmt marker)
|
|
5382 ;; Unhides files marked with CHAR. Optional FMT is a message
|
|
5383 ;; to be displayed. Note that after unhiding, we will need to re-hide
|
|
5384 ;; files belonging to hidden subdirs.
|
|
5385 (save-excursion
|
|
5386 (goto-char (point-min))
|
|
5387 (let ((count 0)
|
|
5388 (string (concat "\r" (char-to-string char)))
|
|
5389 (hidden-subdirs (dired-remember-hidden))
|
|
5390 (new (if marker (concat "\n" (char-to-string marker)) "\n "))
|
|
5391 buffer-read-only)
|
|
5392 (while (search-forward string nil t)
|
|
5393 (replace-match new)
|
|
5394 (setq count (1+ count)))
|
|
5395 (or (equal "" fmt)
|
|
5396 (message (or fmt "Unhid %d line%s.") count (dired-plural-s count)))
|
|
5397 (goto-char (point-min))
|
|
5398 (mapcar (function (lambda (dir)
|
|
5399 (if (dired-goto-subdir dir)
|
|
5400 (dired-hide-subdir 1 t))))
|
|
5401 hidden-subdirs)
|
|
5402 (if marker (dired-update-mode-line-modified t))
|
|
5403 count)))
|
|
5404
|
|
5405 (defun dired-do-hide (char &optional fmt)
|
|
5406 ;; Hides files marked with CHAR. Otional FMT is a message
|
|
5407 ;; to be displayed. FMT is a format string taking args the number
|
|
5408 ;; of hidden file lines, and dired-plural-s.
|
|
5409 (save-excursion
|
|
5410 (goto-char (point-min))
|
|
5411 (let ((count 0)
|
|
5412 (string (concat "\n" (char-to-string char)))
|
|
5413 buffer-read-only)
|
|
5414 (while (search-forward string nil t)
|
|
5415 (subst-char-in-region (match-beginning 0)
|
|
5416 (1+ (match-beginning 0)) ?\n ?\r t)
|
|
5417 (setq count (1+ count)))
|
|
5418 (if fmt
|
|
5419 (message fmt count (dired-plural-s count)))
|
|
5420 count)))
|
|
5421
|
|
5422 (defun dired-omit-regexp ()
|
|
5423 (let (rgxp)
|
|
5424 (if dired-omit-extensions
|
|
5425 (setq rgxp (concat
|
|
5426 ".\\("
|
|
5427 (mapconcat 'regexp-quote dired-omit-extensions "\\|")
|
|
5428 "\\)$")))
|
|
5429 (if dired-omit-regexps
|
|
5430 (setq rgxp
|
|
5431 (concat
|
|
5432 rgxp
|
|
5433 (and rgxp "\\|")
|
|
5434 (mapconcat 'identity dired-omit-regexps "\\|"))))
|
|
5435 rgxp))
|
|
5436
|
|
5437 (defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp)
|
|
5438 ;; Marks unmarked files matching REGEXP, displaying MSG.
|
|
5439 ;; REGEXP is matched against the complete pathname, unless localp is
|
|
5440 ;; specified.
|
|
5441 ;; Does not re-mark files which already have a mark.
|
|
5442 ;; Returns t if any work was done, nil otherwise.
|
|
5443 (let ((dired-marker-char (if unflag-p ?\ dired-marker-char))
|
|
5444 fn)
|
|
5445 (dired-mark-if
|
|
5446 (and
|
|
5447 ;; not already marked
|
|
5448 (eq (following-char) ?\ )
|
|
5449 ;; uninteresting
|
|
5450 (setq fn (dired-get-filename localp t))
|
|
5451 (string-match regexp fn))
|
|
5452 msg)))
|
|
5453
|
|
5454 (defun dired-add-omit-regexp (rgxp &optional how)
|
|
5455 "Adds a new regular expression to the list of omit regular expresions.
|
|
5456 With a non-zero numeric prefix argument, deletes a regular expresion from
|
|
5457 the list.
|
|
5458
|
|
5459 With a prefix argument \\[universal-argument], adds a new extension to
|
|
5460 the list of file name extensions omitted.
|
|
5461 With a prefix argument \\[universal-argument] \\[universal-argument], deletes
|
|
5462 a file name extension from the list.
|
|
5463
|
|
5464 With a prefix 0, reports on the current omit regular expressions and
|
|
5465 extensions."
|
|
5466 (interactive
|
|
5467 (list
|
|
5468 (cond
|
|
5469 ((null current-prefix-arg)
|
|
5470 (read-string "New omit regular expression: "))
|
|
5471 ((equal '(4) current-prefix-arg)
|
|
5472 (read-string "New omit extension (\".\" is not implicit): "))
|
|
5473 ((equal '(16) current-prefix-arg)
|
|
5474 (completing-read
|
|
5475 "Remove from omit extensions (type SPACE for options): "
|
|
5476 (mapcar 'list dired-omit-extensions) nil t))
|
|
5477 ((eq 0 current-prefix-arg)
|
|
5478 nil)
|
|
5479 (t
|
|
5480 (completing-read
|
|
5481 "Remove from omit regexps (type SPACE for options): "
|
|
5482 (mapcar 'list dired-omit-regexps) nil t)))
|
|
5483 current-prefix-arg))
|
|
5484 (let (remove)
|
|
5485 (cond
|
|
5486 ((null how)
|
|
5487 (if (member rgxp dired-omit-regexps)
|
|
5488 (progn
|
|
5489 (describe-variable 'dired-omit-regexps)
|
|
5490 (error "%s is already included in the list." rgxp))
|
|
5491 (setq dired-omit-regexps (cons rgxp dired-omit-regexps))))
|
|
5492 ((equal how '(4))
|
|
5493 (if (member rgxp dired-omit-extensions)
|
|
5494 (progn
|
|
5495 (describe-variable 'dired-omit-extensions)
|
|
5496 (error "%s is already included in list." rgxp))
|
|
5497 (setq dired-omit-extensions (cons rgxp dired-omit-extensions))))
|
|
5498 ((equal how '(16))
|
|
5499 (let ((tail (member rgxp dired-omit-extensions)))
|
|
5500 (if tail
|
|
5501 (setq dired-omit-extensions
|
|
5502 (delq (car tail) dired-omit-extensions)
|
|
5503 remove t)
|
|
5504 (setq remove 'ignore))))
|
|
5505 ((eq 0 how)
|
|
5506 (setq remove 'ignore)
|
|
5507 (if (featurep 'ehelp)
|
|
5508 (with-electric-help
|
|
5509 (function
|
|
5510 (lambda ()
|
|
5511 (princ "Omit extensions (dired-omit-extensions <V>):\n")
|
|
5512 (dired-format-columns-of-files dired-omit-extensions)
|
|
5513 (princ "\n")
|
|
5514 (princ "Omit regular expressions (dired-omit-regexps <V>):\n")
|
|
5515 (dired-format-columns-of-files dired-omit-regexps)
|
|
5516 nil)))
|
|
5517 (with-output-to-temp-buffer "*Help*"
|
|
5518 (princ "Omit extensions (dired-omit-extensions <V>):\n")
|
|
5519 (dired-format-columns-of-files dired-omit-extensions)
|
|
5520 (princ "\n")
|
|
5521 (princ "Omit regular expressions (dired-omit-regexps <V>):\n")
|
|
5522 (dired-format-columns-of-files dired-omit-regexps)
|
|
5523 (print-help-return-message))))
|
|
5524 (t
|
|
5525 (let ((tail (member rgxp dired-omit-regexps)))
|
|
5526 (if tail
|
|
5527 (setq dired-omit-regexps (delq (car tail) dired-omit-regexps)
|
|
5528 remove t)
|
|
5529 (setq remove 'ignore)))))
|
|
5530 (or (eq remove 'ignore)
|
|
5531 (save-excursion
|
|
5532 (mapcar
|
|
5533 (function
|
|
5534 (lambda (dir)
|
|
5535 (if (dired-goto-subdir dir)
|
|
5536 (progn
|
|
5537 (if remove
|
|
5538 (save-restriction
|
|
5539 (narrow-to-region
|
|
5540 (dired-subdir-min) (dired-subdir-max))
|
|
5541 (dired-do-unhide dired-omit-marker-char "")))
|
|
5542 (dired-omit-expunge)))))
|
|
5543 (dired-remember-omitted))))))
|
|
5544
|
|
5545
|
|
5546
|
|
5547 ;;;; ----------------------------------------------------------------
|
|
5548 ;;;; Directory hiding.
|
|
5549 ;;;; ----------------------------------------------------------------
|
|
5550 ;;;
|
|
5551 ;;; To indicate a hidden subdir, we actually insert "..." in the buffer.
|
|
5552 ;;; Aside from giving the look of ellipses (even though
|
|
5553 ;;; selective-display-ellipses is nil), it allows us to tell the difference
|
|
5554 ;;; between a dir with a single omitted file, and a hidden subdir with one
|
|
5555 ;;; file.
|
|
5556
|
|
5557 (defun dired-subdir-hidden-p (dir)
|
|
5558 (save-excursion
|
|
5559 (and selective-display
|
|
5560 (dired-goto-subdir dir)
|
|
5561 (looking-at "\\.\\.\\.\r"))))
|
|
5562
|
|
5563 (defun dired-unhide-subdir ()
|
|
5564 (let (buffer-read-only)
|
|
5565 (goto-char (dired-subdir-min))
|
|
5566 (skip-chars-forward "^\n\r")
|
|
5567 (skip-chars-backward "." (- (point) 3))
|
|
5568 (if (looking-at "\\.\\.\\.\r") (delete-char 4))
|
|
5569 (dired-omit-unhide-region (point) (dired-subdir-max))))
|
|
5570
|
|
5571 (defun dired-hide-check ()
|
|
5572 (or selective-display
|
|
5573 (error "selective-display must be t for subdir hiding to work!")))
|
|
5574
|
|
5575 (defun dired-hide-subdir (arg &optional really)
|
|
5576 "Hide or unhide the current subdirectory and move to next directory.
|
|
5577 Optional prefix arg is a repeat factor.
|
|
5578 Use \\[dired-hide-all] to (un)hide all directories.
|
|
5579 With the optional argument REALLY, we always hide
|
|
5580 the subdir, regardless of dired-subdir-hidden-p."
|
|
5581 ;; The arg REALLY is needed because when we unhide
|
|
5582 ;; omitted files in a hidden subdir, we want to
|
|
5583 ;; re-hide the subdir, regardless of whether dired
|
|
5584 ;; thinks it's already hidden.
|
|
5585 (interactive "p")
|
|
5586 (dired-hide-check)
|
|
5587 (dired-save-excursion
|
|
5588 (while (>= (setq arg (1- arg)) 0)
|
|
5589 (let* ((cur-dir (dired-current-directory))
|
|
5590 (hidden-p (and (null really)
|
|
5591 (dired-subdir-hidden-p cur-dir)))
|
|
5592 (elt (assoc cur-dir dired-subdir-alist))
|
|
5593 (end-pos (1- (dired-get-subdir-max elt)))
|
|
5594 buffer-read-only)
|
|
5595 ;; keep header line visible, hide rest
|
|
5596 (goto-char (dired-get-subdir-min elt))
|
|
5597 (skip-chars-forward "^\n\r")
|
|
5598 (skip-chars-backward "." (- (point) 3))
|
|
5599 (if hidden-p
|
|
5600 (progn
|
|
5601 (if (looking-at "\\.\\.\\.\r")
|
|
5602 (progn
|
|
5603 (delete-char 3)
|
|
5604 (setq end-pos (- end-pos 3))))
|
|
5605 (dired-omit-unhide-region (point) end-pos))
|
|
5606 (if (looking-at "\\.\\.\\.\r")
|
|
5607 (goto-char (match-end 0))
|
|
5608 (insert "...")
|
|
5609 (setq end-pos (+ end-pos 3)))
|
|
5610 (subst-char-in-region (point) end-pos ?\n ?\r)))
|
|
5611 (dired-next-subdir 1 t))))
|
|
5612
|
|
5613 (defun dired-hide-all (arg)
|
|
5614 "Hide all subdirectories, leaving only their header lines.
|
|
5615 If there is already something hidden, make everything visible again.
|
|
5616 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
|
|
5617 (interactive "P")
|
|
5618 (dired-hide-check)
|
|
5619 (let (buffer-read-only)
|
|
5620 (dired-save-excursion
|
|
5621 (if (let ((alist dired-subdir-alist)
|
|
5622 (hidden nil))
|
|
5623 (while (and alist (null hidden))
|
|
5624 (setq hidden (dired-subdir-hidden-p (car (car alist)))
|
|
5625 alist (cdr alist)))
|
|
5626 hidden)
|
|
5627 ;; unhide
|
|
5628 (let ((alist dired-subdir-alist))
|
|
5629 (while alist
|
|
5630 (goto-char (dired-get-subdir-min (car alist)))
|
|
5631 (skip-chars-forward "^\n\r")
|
|
5632 (delete-region (point) (progn (skip-chars-backward ".") (point)))
|
|
5633 (setq alist (cdr alist)))
|
|
5634 (dired-omit-unhide-region (point-min) (point-max)))
|
|
5635 ;; hide
|
|
5636 (let ((alist dired-subdir-alist))
|
|
5637 (while alist
|
|
5638 (dired-goto-subdir (car (car alist)))
|
|
5639 (dired-hide-subdir 1 t)
|
|
5640 (setq alist (cdr alist))))))))
|
|
5641
|
|
5642
|
|
5643 ;;;; -----------------------------------------------------------------
|
|
5644 ;;;; Automatic dired buffer maintenance.
|
|
5645 ;;;; -----------------------------------------------------------------
|
|
5646 ;;;
|
|
5647 ;;; Keeping Dired buffers in sync with the filesystem and with each
|
|
5648 ;;; other.
|
|
5649 ;;; When used with efs on remote directories, buffer maintainence is
|
|
5650 ;;; done asynch.
|
|
5651
|
|
5652 (defun dired-buffers-for-dir (dir-or-list &optional check-wildcard)
|
|
5653 ;; Return a list of buffers that dired DIR-OR-LIST
|
|
5654 ;; (top level or in-situ subdir).
|
|
5655 ;; The list is in reverse order of buffer creation, most recent last.
|
|
5656 ;; As a side effect, killed dired buffers for DIR are removed from
|
|
5657 ;; dired-buffers. If DIR-OR-LIST is a wildcard or list, returns any
|
|
5658 ;; dired buffers for which DIR-OR-LIST is equal to `dired-directory'.
|
|
5659 ;; If check-wildcard is non-nil, only returns buffers which contain dir-or-list
|
|
5660 ;; exactly, including the wildcard part.
|
|
5661 (let ((alist dired-buffers)
|
|
5662 (as-dir (and (stringp dir-or-list)
|
|
5663 (file-name-as-directory dir-or-list)))
|
|
5664 result buff elt)
|
|
5665 (while alist
|
|
5666 (setq buff (cdr (setq elt (car alist)))
|
|
5667 alist (cdr alist))
|
|
5668 ;; dired-in-this-tree is not fast. It doesn't pay to use this to check
|
|
5669 ;; whether the buffer is a good candidate.
|
|
5670 (if (buffer-name buff)
|
|
5671 (save-excursion
|
|
5672 (set-buffer buff)
|
|
5673 (if (or (equal dir-or-list dired-directory) ; the wildcard case.
|
|
5674 (and as-dir
|
|
5675 (not (and check-wildcard
|
|
5676 (string-equal
|
|
5677 as-dir
|
|
5678 (expand-file-name default-directory))))
|
|
5679 (assoc as-dir dired-subdir-alist)))
|
|
5680 (setq result (cons buff result))))
|
|
5681 ;; else buffer is killed - clean up:
|
|
5682 (setq dired-buffers (delq elt dired-buffers))))
|
|
5683 (or dired-buffers (dired-remove-from-file-name-handler-alist))
|
|
5684 result))
|
|
5685
|
|
5686 (defun dired-advertise ()
|
|
5687 ;; Advertise in variable `dired-buffers' that we dired `default-directory'.
|
|
5688 ;; With wildcards we actually advertise too much.
|
|
5689 ;; Also makes sure that we are installed in the file-name-handler-alist
|
|
5690 (prog1
|
|
5691 (let ((ddir (expand-file-name default-directory)))
|
|
5692 (if (memq (current-buffer) (dired-buffers-for-dir ddir))
|
|
5693 t ; we have already advertised ourselves
|
|
5694 (setq dired-buffers
|
|
5695 (cons (cons ddir (current-buffer))
|
|
5696 dired-buffers))))
|
|
5697 ;; Do this last, otherwise the call to dired-buffers-for-dir will
|
|
5698 ;; remove dired-handler-fn from the file-name-handler-alist.
|
|
5699 ;; Strictly speaking, we only need to do this in th else branch of
|
|
5700 ;; the if statement. We do it unconditionally as a sanity check.
|
|
5701 (dired-check-file-name-handler-alist)))
|
|
5702
|
|
5703 (defun dired-unadvertise (dir)
|
|
5704 ;; Remove DIR from the buffer alist in variable dired-buffers.
|
|
5705 ;; This has the effect of removing any buffer whose main directory is DIR.
|
|
5706 ;; It does not affect buffers in which DIR is a subdir.
|
|
5707 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
|
|
5708 (setq dired-buffers
|
|
5709 (delq (assoc dir dired-buffers) dired-buffers))
|
|
5710 ;; If there are no more dired buffers, we are no longer needed in the
|
|
5711 ;; file-name-handler-alist.
|
|
5712 (or dired-buffers (dired-remove-from-file-name-handler-alist)))
|
|
5713
|
|
5714 (defun dired-unadvertise-current-buffer ()
|
|
5715 ;; Remove all references to the current buffer in dired-buffers.
|
|
5716 (setq dired-buffers
|
|
5717 (delq nil
|
|
5718 (mapcar
|
|
5719 (function
|
|
5720 (lambda (x)
|
|
5721 (and (not (eq (current-buffer) (cdr x))) x)))
|
|
5722 dired-buffers))))
|
|
5723
|
|
5724 (defun dired-fun-in-all-buffers (directory fun &rest args)
|
|
5725 ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS.
|
|
5726 ;; Return list of buffers where FUN succeeded (i.e., returned non-nil).
|
|
5727 (let* ((buf-list (dired-buffers-for-dir directory))
|
|
5728 (obuf (current-buffer))
|
|
5729 (owin (selected-window))
|
|
5730 (win owin)
|
|
5731 buf windows success-list)
|
|
5732 (if buf-list
|
|
5733 (unwind-protect
|
|
5734 (progn
|
|
5735 (while (not (eq (setq win (next-window win)) owin))
|
|
5736 (and (memq (setq buf (window-buffer win)) buf-list)
|
|
5737 (progn
|
|
5738 (set-buffer buf)
|
|
5739 (= (point) (window-point win)))
|
|
5740 (setq windows (cons win windows))))
|
|
5741 (while buf-list
|
|
5742 (setq buf (car buf-list)
|
|
5743 buf-list (cdr buf-list))
|
|
5744 (set-buffer buf)
|
|
5745 (if (apply fun args)
|
|
5746 (setq success-list (cons (buffer-name buf) success-list))))
|
|
5747 ;; dired-save-excursion prevents lossage of save-excursion
|
|
5748 ;; for point. However, if dired buffers are displayed in
|
|
5749 ;; other windows, the setting of window-point loses, and
|
|
5750 ;; drags the point with it. This should fix this.
|
|
5751 (while windows
|
|
5752 (condition-case nil
|
|
5753 (progn
|
|
5754 (set-buffer (window-buffer (setq win (car windows))))
|
|
5755 (set-window-point win (point)))
|
|
5756 (error nil))
|
|
5757 (setq windows (cdr windows))))
|
|
5758 (set-buffer obuf)))
|
|
5759 success-list))
|
|
5760
|
|
5761 (defun dired-find-file-place (subdir file)
|
|
5762 ;; Finds a position to insert in SUBDIR FILE. If it can't find SUBDIR,
|
|
5763 ;; returns nil.
|
|
5764 (let ((sort (dired-sort-type dired-internal-switches))
|
|
5765 (rev (memq ?r (nth 3 (assoc subdir dired-subdir-alist)))))
|
|
5766 (cond
|
|
5767 ((eq sort 'name)
|
|
5768 (if (dired-goto-subdir subdir)
|
|
5769 (let ((max (dired-subdir-max))
|
|
5770 start end found)
|
|
5771 (if (dired-goto-next-file)
|
|
5772 (progn
|
|
5773 (skip-chars-forward "^\n\r")
|
|
5774 (setq start (point))
|
|
5775 (goto-char (setq end max))
|
|
5776 (forward-char -1)
|
|
5777 (skip-chars-backward "^\n\r")
|
|
5778 ;; This loop must find a file. At the very least, it will
|
|
5779 ;; find the one found previously.
|
|
5780 (while (not found)
|
|
5781 (if (save-excursion (dired-move-to-filename nil (point)))
|
|
5782 (setq found t)
|
|
5783 (setq end (point))
|
|
5784 (forward-char -1)
|
|
5785 (skip-chars-backward "^\n\r")))
|
|
5786 (if rev
|
|
5787 (while (< start end)
|
|
5788 (goto-char (/ (+ start end) 2))
|
|
5789 (if (dired-file-name-lessp
|
|
5790 (or (dired-get-filename 'no-dir t)
|
|
5791 (error
|
|
5792 "Error in dired-find-file-place"))
|
|
5793 file)
|
|
5794 (setq end (progn
|
|
5795 (skip-chars-backward "^\n\r")
|
|
5796 (point)))
|
|
5797 (setq start (progn
|
|
5798 (skip-chars-forward "^\n\r")
|
|
5799 (forward-char 1)
|
|
5800 (skip-chars-forward "^\n\r")
|
|
5801 (point)))))
|
|
5802 (while (< start end)
|
|
5803 (goto-char (/ (+ start end) 2))
|
|
5804 (if (dired-file-name-lessp
|
|
5805 file
|
|
5806 (or (dired-get-filename 'no-dir t)
|
|
5807 (error
|
|
5808 "Error in dired-find-file-place")))
|
|
5809 (setq end (progn
|
|
5810 (skip-chars-backward "^\n\r")
|
|
5811 (point)))
|
|
5812 (setq start (progn
|
|
5813 (skip-chars-forward "^\n\r")
|
|
5814 (forward-char 1)
|
|
5815 (skip-chars-forward "^\n\r")
|
|
5816 (point))))))
|
|
5817 (goto-char end))
|
|
5818 (goto-char max))
|
|
5819 t)))
|
|
5820 ((eq sort 'date)
|
|
5821 (if (dired-goto-subdir subdir)
|
|
5822 (if rev
|
|
5823 (goto-char (dired-subdir-max))
|
|
5824 (dired-goto-next-file)
|
|
5825 t)))
|
|
5826 ;; Put in support for other sorting types.
|
|
5827 (t
|
|
5828 (if (string-equal (dired-current-directory) subdir)
|
|
5829 (progn
|
|
5830 ;; We are already where we should be, except when
|
|
5831 ;; point is before the subdir line or its total line.
|
|
5832 (or (save-excursion (beginning-of-line) (dired-move-to-filename))
|
|
5833 (dired-goto-next-nontrivial-file)) ; in the header somewhere
|
|
5834 t) ; return t, for found.
|
|
5835 (if (dired-goto-subdir subdir)
|
|
5836 (progn
|
|
5837 (dired-goto-next-nontrivial-file)
|
|
5838 t)))))))
|
|
5839
|
|
5840 (defun dired-add-entry (filename &optional marker-char inplace)
|
|
5841 ;; Add a new entry for FILENAME, optionally marking it
|
|
5842 ;; with MARKER-CHAR (a character, else dired-marker-char is used).
|
|
5843 ;; Hidden subdirs are exposed if a file is added there.
|
|
5844 ;;
|
|
5845 ;; This function now adds the new entry at the END of the previous line,
|
|
5846 ;; not the beginning of the current line.
|
|
5847 ;; Logically, we now think of the `newline' associated
|
|
5848 ;; with a fileline, as the one at the beginning of the line, not the end.
|
|
5849 ;; This makes it easier to keep track of omitted files.
|
|
5850 ;;
|
|
5851 ;; Uses dired-save-excursion, so that it doesn't move the
|
|
5852 ;; point around. Especially important when it runs asynch.
|
|
5853 ;;
|
|
5854 ;; If there is already an entry, delete the existing one before adding a
|
|
5855 ;; new one. In this case, doesn't remember its mark. Use
|
|
5856 ;; dired-update-file-line for that.
|
|
5857 ;;
|
|
5858 ;; If INPLACE eq 'relist, then the new entry is put in the
|
|
5859 ;; same place as the old, if there was an old entry.
|
|
5860 ;; If INPLACE is t, then the file entry is put on the line
|
|
5861 ;; currently containing the point. Otherwise, dired-find-file-place
|
|
5862 ;; attempts to determine where to put the file.
|
|
5863
|
|
5864 (setq filename (directory-file-name filename))
|
|
5865 (dired-save-excursion
|
|
5866 (let ((oentry (save-excursion (dired-goto-file filename)))
|
|
5867 (directory (file-name-directory filename))
|
|
5868 (file-nodir (file-name-nondirectory filename))
|
|
5869 buffer-read-only)
|
|
5870 (if oentry
|
|
5871 ;; Remove old entry
|
|
5872 (let ((opoint (point)))
|
|
5873 (goto-char oentry)
|
|
5874 (delete-region (save-excursion
|
|
5875 (skip-chars-backward "^\r\n")
|
|
5876 (dired-update-marker-counters (following-char) t)
|
|
5877 (1- (point)))
|
|
5878 (progn
|
|
5879 (skip-chars-forward "^\r\n")
|
|
5880 (point)))
|
|
5881 ;; Move to right place to replace deleted line.
|
|
5882 (cond ((eq inplace 'relist) (forward-char 1))
|
|
5883 ((eq inplace t) (goto-char opoint)))
|
|
5884 (dired-update-mode-line-modified)))
|
|
5885 (if (or (eq inplace t)
|
|
5886 (and oentry (eq inplace 'relist))
|
|
5887 ;; Tries to move the point to the right place.
|
|
5888 ;; Returns t on success.
|
|
5889 (dired-find-file-place directory file-nodir))
|
|
5890 (let ((switches (dired-make-switches-string
|
|
5891 (cons ?d dired-internal-switches)))
|
|
5892 b-of-l)
|
|
5893 ;; Bind marker-char now, in case we are working asynch and
|
|
5894 ;; dired-marker-char changes in the meantime.
|
|
5895 (if (and marker-char (not (integerp marker-char)))
|
|
5896 (setq marker-char dired-marker-char))
|
|
5897 ;; since we insert at the end of a line,
|
|
5898 ;; backup to the end of the previous line.
|
|
5899 (skip-chars-backward "^\n\r")
|
|
5900 (forward-char -1)
|
|
5901 (setq b-of-l (point))
|
|
5902 (if (and (featurep 'efs-dired) efs-dired-host-type)
|
|
5903 ;; insert asynch
|
|
5904 ;; we call the efs version explicitly here,
|
|
5905 ;; rather than let the handler-alist work for us
|
|
5906 ;; because we want to pass extra args.
|
|
5907 ;; Is there a cleaner way to do this?
|
|
5908 (efs-insert-directory filename ; don't expand `.' !
|
|
5909 switches nil nil
|
|
5910 t ; nowait
|
|
5911 marker-char)
|
|
5912 (let ((insert-directory-program dired-ls-program))
|
|
5913 (insert-directory filename switches nil nil))
|
|
5914 (dired-after-add-entry b-of-l marker-char))
|
|
5915 (if dired-verify-modtimes
|
|
5916 (dired-set-file-modtime directory dired-subdir-alist))
|
|
5917 t))))) ; return t on success, else nil.
|
|
5918
|
|
5919 (defun dired-after-add-entry (start marker-char)
|
|
5920 ;; Does the cleanup of a dired entry after listing it.
|
|
5921 ;; START is the start of the new listing-line.
|
|
5922 ;; This is a separate function for the sake of efs.
|
|
5923 (save-excursion
|
|
5924 (goto-char start)
|
|
5925 ;; we make sure that the new line is bracketted by new-lines
|
|
5926 ;; so the user doesn't need to use voodoo in the
|
|
5927 ;; after-readin-hook.
|
|
5928 (insert ?\n)
|
|
5929 (dired-add-entry-do-indentation marker-char)
|
|
5930 (let* ((beg (dired-manual-move-to-filename t))
|
|
5931 ;; error for strange output
|
|
5932 (end (dired-manual-move-to-end-of-filename))
|
|
5933 (filename (buffer-substring beg end)))
|
|
5934 ;; We want to have the non-directory part only.
|
|
5935 (delete-region beg end)
|
|
5936 ;; Any markers pointing to the beginning of the filename, will
|
|
5937 ;; still point there after this insertion. Should keep
|
|
5938 ;; save-excursion from losing.
|
|
5939 (setq beg (point))
|
|
5940 (insert (file-name-nondirectory filename))
|
|
5941 (dired-insert-set-properties beg (point))
|
|
5942 (dired-move-to-filename))
|
|
5943 ;; The subdir-alist is not affected so we can run it right now.
|
|
5944 (let ((omit (dired-current-subdir-omitted-p))
|
|
5945 (hide (dired-subdir-hidden-p (dired-current-directory))))
|
|
5946 (if (or dired-after-readin-hook omit hide)
|
|
5947 (save-excursion
|
|
5948 (save-restriction
|
|
5949 ;; Use start so that we get the new-line at
|
|
5950 ;; the beginning of the line in case we want
|
|
5951 ;; to hide the file. Don't need to test (bobp)
|
|
5952 ;; here, since we never add a file at
|
|
5953 ;; the beginning of the buffer.
|
|
5954 (narrow-to-region start
|
|
5955 (save-excursion (forward-line 1) (point)))
|
|
5956 (run-hooks 'dired-after-readin-hook)
|
|
5957 (if omit
|
|
5958 (let ((dired-omit-silent (or dired-omit-silent 0)))
|
|
5959 (dired-omit-region (point-min) (point-max)
|
|
5960 (dired-omit-regexp))))
|
|
5961 (if hide
|
|
5962 (subst-char-in-region (point-min) (1- (point-max))
|
|
5963 ?\n ?\r))))))
|
|
5964 ;; clobber the extra newline at the end of the line
|
|
5965 (end-of-line)
|
|
5966 (delete-char 1)))
|
|
5967
|
|
5968 ;; This is a separate function for the sake of nested dired format.
|
|
5969 (defun dired-add-entry-do-indentation (marker-char)
|
|
5970 ;; two spaces or a marker plus a space:
|
|
5971 (insert (if marker-char
|
|
5972 (let ((char (if (integerp marker-char)
|
|
5973 marker-char
|
|
5974 dired-marker-char)))
|
|
5975 (dired-update-marker-counters char)
|
|
5976 (dired-update-mode-line-modified)
|
|
5977 char)
|
|
5978 ?\040)
|
|
5979 ?\040))
|
|
5980
|
|
5981 (defun dired-remove-file (file)
|
|
5982 (let ((alist dired-buffers)
|
|
5983 buff)
|
|
5984 (save-excursion
|
|
5985 (while alist
|
|
5986 (setq buff (cdr (car alist)))
|
|
5987 (if (buffer-name buff)
|
|
5988 (progn
|
|
5989 (set-buffer buff)
|
|
5990 (dired-remove-entry file))
|
|
5991 (setq dired-buffers (delq (car alist) dired-buffers)))
|
|
5992 (setq alist (cdr alist))))
|
|
5993 (or dired-buffers (dired-remove-from-file-name-handler-alist))))
|
|
5994
|
|
5995 (defun dired-remove-entry (file)
|
|
5996 (let ((ddir (expand-file-name default-directory))
|
|
5997 (dirname (file-name-as-directory file)))
|
|
5998 (if (dired-in-this-tree ddir dirname)
|
|
5999 (if (or (memq 'kill-dired-buffer dired-no-confirm)
|
|
6000 (y-or-n-p (format "Kill dired buffer %s for %s, too? "
|
|
6001 (buffer-name) dired-directory)))
|
|
6002 (kill-buffer (current-buffer)))
|
|
6003 (if (dired-in-this-tree file ddir)
|
|
6004 (let ((alist dired-subdir-alist))
|
|
6005 (while alist
|
|
6006 (if (dired-in-this-tree (car (car alist)) dirname)
|
|
6007 (save-excursion
|
|
6008 (goto-char (dired-get-subdir-min (car alist)))
|
|
6009 (dired-kill-subdir)))
|
|
6010 (setq alist (cdr alist)))
|
|
6011 (dired-save-excursion
|
|
6012 (and (dired-goto-file file)
|
|
6013 (let (buffer-read-only)
|
|
6014 (delete-region
|
|
6015 (progn (skip-chars-backward "^\n\r")
|
|
6016 (or (memq (following-char) '(\n \r ?\ ))
|
|
6017 (progn
|
|
6018 (dired-update-marker-counters
|
|
6019 (following-char) t)
|
|
6020 (dired-update-mode-line-modified)))
|
|
6021 (1- (point)))
|
|
6022 (progn (skip-chars-forward "^\n\r") (point)))
|
|
6023 (if dired-verify-modtimes
|
|
6024 (dired-set-file-modtime
|
|
6025 (file-name-directory (directory-file-name file))
|
|
6026 dired-subdir-alist))))))))))
|
|
6027
|
|
6028 (defun dired-add-file (filename &optional marker-char)
|
|
6029 (dired-fun-in-all-buffers
|
|
6030 (file-name-directory filename)
|
|
6031 (function dired-add-entry) filename marker-char))
|
|
6032
|
|
6033 (defun dired-relist-file (file)
|
|
6034 (dired-uncache file nil)
|
|
6035 (dired-fun-in-all-buffers (file-name-directory file)
|
|
6036 (function dired-relist-entry) file))
|
|
6037
|
|
6038 (defun dired-relist-entry (file)
|
|
6039 ;; Relist the line for FILE, or just add it if it did not exist.
|
|
6040 ;; FILE must be an absolute pathname.
|
|
6041 (let* ((file (directory-file-name file))
|
|
6042 (directory (file-name-directory file))
|
|
6043 (dd (expand-file-name default-directory)))
|
|
6044 (if (assoc directory dired-subdir-alist)
|
|
6045 (if (or
|
|
6046 ;; Not a wildcard
|
|
6047 (equal dd dired-directory)
|
|
6048 ;; Not top-level
|
|
6049 (not (string-equal directory dd))
|
|
6050 (and (string-equal directory
|
|
6051 (if (consp dired-directory)
|
|
6052 (file-name-as-directory
|
|
6053 (car dired-directory))
|
|
6054 (file-name-directory dired-directory)))
|
|
6055 (dired-file-in-wildcard-p dired-directory file)))
|
|
6056 (let ((marker (save-excursion
|
|
6057 (and (dired-goto-file file)
|
|
6058 (dired-file-marker file)))))
|
|
6059 ;; recompute omission
|
|
6060 (if (eq marker dired-omit-marker-char)
|
|
6061 (setq marker nil))
|
|
6062 (dired-add-entry file marker 'relist))
|
|
6063 ;; At least tell dired that we considered updating the buffer.
|
|
6064 (if dired-verify-modtimes
|
|
6065 (dired-set-file-modtime directory dired-subdir-alist))))))
|
|
6066
|
|
6067 (defun dired-file-in-wildcard-p (wildcard file)
|
|
6068 ;; Return t if a file is part of the listing for wildcard.
|
|
6069 ;; File should be the non-directory part only.
|
|
6070 ;; This version is slow, but meticulously correct. Is it worth it?
|
|
6071 (if (consp wildcard)
|
|
6072 (let ((files (cdr wildcard))
|
|
6073 (dir (car wildcard))
|
|
6074 yep)
|
|
6075 (while (and files (not yep))
|
|
6076 (setq yep (string-equal file (expand-file-name (car files) dir))
|
|
6077 files (cdr files)))
|
|
6078 yep)
|
|
6079 (let ((err-buff
|
|
6080 (let ((default-major-mode 'fundamental-mode))
|
|
6081 (get-buffer-create " *dired-check-process output*")))
|
|
6082 (dir default-directory)
|
|
6083 (process-connection-type nil))
|
|
6084 (save-excursion
|
|
6085 (set-buffer err-buff)
|
|
6086 (erase-buffer)
|
|
6087 (setq default-directory dir)
|
|
6088 (call-process shell-file-name nil t nil "-c"
|
|
6089 (concat dired-ls-program " -d " wildcard " | "
|
|
6090 "egrep '(^|/)" file "$'"))
|
|
6091 (/= (buffer-size) 0)))))
|
|
6092
|
|
6093 ;; The difference between dired-add-file and dired-relist-file is that
|
|
6094 ;; the former creates the entry with a specific marker. The later preserves
|
|
6095 ;; existing markers on a per buffer basis. This is not the same as
|
|
6096 ;; giving dired-create-files a marker of t, which uses a marker in a specific
|
|
6097 ;; buffer to determine the marker for file line creation in all buffers.
|
|
6098
|
|
6099
|
|
6100 ;;;; ----------------------------------------------------------------
|
|
6101 ;;;; Applying Lisp functions to marked files.
|
|
6102 ;;;; ----------------------------------------------------------------
|
|
6103
|
|
6104 ;;; Running tags commands on marked files.
|
|
6105 ;;
|
|
6106 ;; Written 8/30/93 by Roland McGrath <roland@gnu.ai.mit.edu>.
|
|
6107 ;; Requires tags.el as distributed with GNU Emacs 19.23, or later.
|
|
6108
|
|
6109 (defun dired-do-tags-search (regexp)
|
|
6110 "Search through all marked files for a match for REGEXP.
|
|
6111 Stops when a match is found.
|
|
6112 To continue searching for next match, use command \\[tags-loop-continue]."
|
|
6113 (interactive "sSearch marked files (regexp): ")
|
|
6114 (tags-search regexp '(dired-get-marked-files)))
|
|
6115
|
|
6116 (defun dired-do-tags-query-replace (from to &optional delimited)
|
|
6117 "Query-replace-regexp FROM with TO through all marked files.
|
|
6118 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
|
|
6119 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
|
|
6120 with the command \\[tags-loop-continue]."
|
|
6121 (interactive
|
|
6122 "sQuery replace in marked files (regexp): \nsQuery replace %s by: \nP")
|
|
6123 (tags-query-replace from to delimited '(dired-get-marked-files)))
|
|
6124
|
|
6125 ;;; byte compiling
|
|
6126
|
|
6127 (defun dired-byte-compile ()
|
|
6128 ;; Return nil for success, offending file name else.
|
|
6129 (let* ((filename (dired-get-filename))
|
|
6130 buffer-read-only failure)
|
|
6131 (condition-case err
|
|
6132 (save-excursion (byte-compile-file filename))
|
|
6133 (error
|
|
6134 (setq failure err)))
|
|
6135 ;; We should not need to update any file lines, as this will have
|
|
6136 ;; already been done by after-write-region-hook.
|
|
6137 (and failure
|
|
6138 (progn
|
|
6139 (dired-log (buffer-name (current-buffer))
|
|
6140 "Byte compile error for %s:\n%s\n" filename failure)
|
|
6141 (dired-make-relative filename)))))
|
|
6142
|
|
6143 (defun dired-do-byte-compile (&optional arg)
|
|
6144 "Byte compile marked (or next ARG) Emacs lisp files."
|
|
6145 (interactive "P")
|
|
6146 (dired-map-over-marks-check (function dired-byte-compile) arg
|
|
6147 'byte-compile "byte-compile" t))
|
|
6148
|
|
6149 ;;; loading
|
|
6150
|
|
6151 (defun dired-load ()
|
|
6152 ;; Return nil for success, offending file name else.
|
|
6153 (let ((file (dired-get-filename)) failure)
|
|
6154 (condition-case err
|
|
6155 (load file nil nil t)
|
|
6156 (error (setq failure err)))
|
|
6157 (if (not failure)
|
|
6158 nil
|
|
6159 (dired-log (buffer-name (current-buffer))
|
|
6160 "Load error for %s:\n%s\n" file failure)
|
|
6161 (dired-make-relative file))))
|
|
6162
|
|
6163 (defun dired-do-load (&optional arg)
|
|
6164 "Load the marked (or next ARG) Emacs lisp files."
|
|
6165 (interactive "P")
|
|
6166 (dired-map-over-marks-check (function dired-load) arg 'load "load" t))
|
|
6167
|
|
6168
|
|
6169 ;;;; ----------------------------------------------------------------
|
|
6170 ;;;; File Name Handler Alist
|
|
6171 ;;;; ----------------------------------------------------------------
|
|
6172 ;;;
|
|
6173 ;;; Make sure that I/O functions maintain dired buffers.
|
|
6174
|
|
6175 (defun dired-remove-from-file-name-handler-alist ()
|
|
6176 ;; Remove dired from the file-name-handler-alist
|
|
6177 (setq file-name-handler-alist
|
|
6178 (delq nil
|
|
6179 (mapcar
|
|
6180 (function
|
|
6181 (lambda (x)
|
|
6182 (and (not (eq (cdr x) 'dired-handler-fn))
|
|
6183 x)))
|
|
6184 file-name-handler-alist))))
|
|
6185
|
|
6186 (defun dired-check-file-name-handler-alist ()
|
|
6187 ;; Verify that dired is installed as the first item in the alist
|
|
6188 (or (eq (cdr (car file-name-handler-alist)) 'dired-handler-fn)
|
|
6189 (setq file-name-handler-alist
|
|
6190 (cons
|
|
6191 '("." . dired-handler-fn)
|
|
6192 (dired-remove-from-file-name-handler-alist)))))
|
|
6193
|
|
6194 (defun dired-handler-fn (op &rest args)
|
|
6195 ;; Function to update dired buffers after I/O.
|
|
6196 (prog1
|
|
6197 (let ((inhibit-file-name-handlers
|
|
6198 (cons 'dired-handler-fn
|
|
6199 (and (eq inhibit-file-name-operation op)
|
|
6200 inhibit-file-name-handlers)))
|
|
6201 (inhibit-file-name-operation op))
|
|
6202 (apply op args))
|
|
6203 (let ((dired-omit-silent t)
|
|
6204 (hf (get op 'dired)))
|
|
6205 (and hf (funcall hf args)))))
|
|
6206
|
|
6207 (defun dired-handler-fn-1 (args)
|
|
6208 (let ((to (expand-file-name (nth 1 args))))
|
|
6209 (or (member to dired-unhandle-add-files)
|
|
6210 (dired-relist-file to))))
|
|
6211
|
|
6212 (defun dired-handler-fn-2 (args)
|
|
6213 (let ((from (expand-file-name (car args)))
|
|
6214 (to (expand-file-name (nth 1 args))))
|
|
6215 ;; Don't remove the original entry if making backups.
|
|
6216 ;; Otherwise we lose marks. I'm not completely happy with the
|
|
6217 ;; logic here.
|
|
6218 (or (and
|
|
6219 (eq (nth 2 args) t) ; backups always have OK-IF-OVERWRITE t
|
|
6220 (string-equal (car (find-backup-file-name from)) to))
|
|
6221 (dired-remove-file from))
|
|
6222 (or (member to dired-unhandle-add-files)
|
|
6223 (dired-relist-file to))))
|
|
6224
|
|
6225 (defun dired-handler-fn-3 (args)
|
|
6226 (let ((to (expand-file-name (nth 2 args))))
|
|
6227 (or (member to dired-unhandle-add-files)
|
|
6228 (dired-relist-file to))))
|
|
6229
|
|
6230 (defun dired-handler-fn-4 (args)
|
|
6231 (dired-remove-file (expand-file-name (car args))))
|
|
6232
|
|
6233 (defun dired-handler-fn-5 (args)
|
|
6234 (let ((to (expand-file-name (car args))))
|
|
6235 (or (member to dired-unhandle-add-files)
|
|
6236 (dired-relist-file to))))
|
|
6237
|
|
6238 (defun dired-handler-fn-6 (args)
|
|
6239 (let ((to (expand-file-name (nth 1 args)))
|
|
6240 (old (expand-file-name (car args))))
|
|
6241 (or (member to dired-unhandle-add-files)
|
|
6242 (dired-relist-file to))
|
|
6243 (dired-relist-file old)))
|
|
6244
|
|
6245 (put 'copy-file 'dired 'dired-handler-fn-1)
|
|
6246 (put 'dired-make-relative-symlink 'dired 'dired-handler-fn-1)
|
|
6247 (put 'make-symbolic-link 'dired 'dired-handler-fn-1)
|
|
6248 (put 'add-name-to-file 'dired 'dired-handler-fn-6)
|
|
6249 (put 'rename-file 'dired 'dired-handler-fn-2)
|
|
6250 (put 'write-region 'dired 'dired-handler-fn-3)
|
|
6251 (put 'delete-file 'dired 'dired-handler-fn-4)
|
|
6252 (put 'delete-directory 'dired 'dired-handler-fn-4)
|
|
6253 (put 'dired-recursive-delete-directory 'dired 'dired-handler-fn-4)
|
|
6254 (put 'make-directory-internal 'dired 'dired-handler-fn-5)
|
|
6255 (put 'set-file-modes 'dired 'dired-handler-fn-5)
|
|
6256
|
|
6257 ;;;; ------------------------------------------------------------
|
|
6258 ;;;; Autoload land.
|
|
6259 ;;;; ------------------------------------------------------------
|
|
6260
|
|
6261 ;;; Reading mail (dired-xy)
|
|
6262
|
|
6263 (autoload 'dired-read-mail "dired-xy"
|
|
6264 "Reads the current file as a mail folder." t)
|
|
6265 (autoload 'dired-vm "dired-xy" "Run VM on this file." t)
|
|
6266 (autoload 'dired-rmail "dired-xy" "Run RMAIL on this file." t)
|
|
6267
|
|
6268 ;;; Virtual dired (dired-vir)
|
|
6269
|
|
6270 (autoload 'dired-virtual "dired-vir"
|
|
6271 "Put this buffer into virtual dired mode." t)
|
|
6272
|
|
6273 ;;; Grep (dired-grep)
|
|
6274
|
|
6275 (autoload 'dired-do-grep "dired-grep" "Grep marked files for a pattern." t)
|
|
6276
|
|
6277 ;;; Doing diffs (dired-diff)
|
|
6278
|
|
6279 (autoload 'dired-diff "dired-diff"
|
|
6280 "Compare file at point with FILE using `diff'." t)
|
|
6281 (autoload 'dired-backup-diff "dired-diff"
|
|
6282 "Diff this file with its backup file or vice versa." t)
|
|
6283 (autoload 'dired-emerge "dired-diff"
|
|
6284 "Merge file at point with FILE using `emerge'." t)
|
|
6285 (autoload 'dired-emerge-with-ancestor "dired-diff"
|
|
6286 "Merge file at point with FILE, using a common ANCESTOR file." t)
|
|
6287 (autoload 'dired-ediff "dired-diff" "Ediff file at point with FILE." t)
|
|
6288 (autoload 'dired-epatch "dired-diff" "Patch file at point using `epatch'." t)
|
|
6289
|
|
6290 ;;; Shell commands (dired-shell)
|
|
6291
|
|
6292 (autoload 'dired-do-print "dired-shell" "Print the marked (next ARG) files." t)
|
|
6293 (autoload 'dired-run-shell-command "dired-shell" nil)
|
|
6294 (autoload 'dired-do-shell-command "dired-shell"
|
|
6295 "Run a shell command on the marked (or next ARG) files." t)
|
|
6296 (autoload 'dired-do-background-shell-command "dired-shell"
|
|
6297 "Run a background shell command on marked (or next ARG) files." t)
|
|
6298
|
|
6299 ;;; Commands using regular expressions (dired-rgxp)
|
|
6300
|
|
6301 (autoload 'dired-mark-files-regexp "dired-rgxp"
|
|
6302 "Mark all files whose names match REGEXP." t)
|
|
6303 (autoload 'dired-flag-files-regexp "dired-rgxp"
|
|
6304 "Flag for deletion all files whose names match REGEXP." t)
|
|
6305 (autoload 'dired-mark-extension "dired-rgxp"
|
|
6306 "Mark all files whose names have a given extension." t)
|
|
6307 (autoload 'dired-flag-extension "dired-rgxp"
|
|
6308 "Flag for deletion all files whose names have a given extension." t)
|
|
6309 (autoload 'dired-cleanup "dired-rgxp"
|
|
6310 "Flag for deletion dispensable files files created by PROGRAM." t)
|
|
6311 (autoload 'dired-do-rename-regexp "dired-rgxp"
|
|
6312 "Rename marked files whose names match a given regexp." t)
|
|
6313 (autoload 'dired-do-copy-regexp "dired-rgxp"
|
|
6314 "Copy marked files whose names match a given regexp." t)
|
|
6315 (autoload 'dired-do-hardlink-regexp "dired-rgxp"
|
|
6316 "Hardlink all marked files whose names match a regexp." t)
|
|
6317 (autoload 'dired-do-symlink "dired-rgxp"
|
|
6318 "Make a symbolic link to all files whose names match a regexp." t)
|
|
6319 (autoload
|
|
6320 'dired-do-relsymlink-regexp "dired-rgxp"
|
|
6321 "Make a relative symbolic link to all files whose names match a regexp." t)
|
|
6322 (autoload 'dired-upcase "dired-rgxp"
|
|
6323 "Rename all marked (or next ARG) files to upper case." t)
|
|
6324 (autoload 'dired-downcase "dired-rgxp"
|
|
6325 "Rename all marked (or next ARG) files to lower case." t)
|
|
6326
|
|
6327 ;;; Marking files from other buffers (dired-mob)
|
|
6328
|
|
6329 (autoload 'dired-mark-files-from-other-dired-buffer "dired-mob"
|
|
6330 "Mark files which are marked in another dired buffer." t)
|
|
6331 (autoload 'dired-mark-files-compilation-buffer "dired-mob"
|
|
6332 "Mark the files mentioned in the compilation buffer." t)
|
|
6333
|
|
6334 ;;; uuencoding (dired-uu)
|
|
6335
|
|
6336 (autoload 'dired-do-uucode "dired-uu" "Uuencode or uudecode marked files." t)
|
|
6337
|
|
6338 ;;; Compressing (dired-cmpr)
|
|
6339
|
|
6340 (autoload 'dired-do-compress "dired-cmpr"
|
|
6341 "Compress or uncompress marked files." t)
|
|
6342 (autoload 'dired-compress-subdir-files "dired-cmpr"
|
|
6343 "Compress uncompressed files in the current subdirectory." t)
|
|
6344
|
|
6345
|
|
6346 ;;; Marking files according to sexps
|
|
6347
|
|
6348 (autoload 'dired-mark-sexp "dired-sex"
|
|
6349 "Mark files according to an sexpression." t)
|
|
6350
|
|
6351 ;;; Help!
|
|
6352
|
|
6353 (autoload 'dired-summary "dired-help"
|
|
6354 "Display summary of basic dired commands in the minibuffer." t)
|
|
6355 (autoload 'dired-describe-mode "dired-help"
|
|
6356 "Detailed description of dired mode.
|
|
6357 With a prefix, runs the info documentation browser for dired." t)
|
|
6358 (autoload 'dired-apropos "dired-help"
|
|
6359 "Do command apropos help for dired commands.
|
|
6360 With prefix does apropos help for dired variables." t)
|
|
6361 (autoload 'dired-report-bug "dired-help" "Report a bug for dired." t)
|
|
6362
|
|
6363 ;;;; --------------------------------------------------------------
|
|
6364 ;;;; Multi-flavour Emacs support
|
|
6365 ;;;; --------------------------------------------------------------
|
|
6366
|
|
6367 (let ((lucid-p (string-match "Lucid" emacs-version))
|
|
6368 ver)
|
|
6369 (or (string-match "^\\([0-9]+\\)\\." emacs-version)
|
|
6370 (error "Weird emacs version %s" emacs-version))
|
|
6371 (setq ver (string-to-int (substring emacs-version (match-beginning 1)
|
|
6372 (match-end 1))))
|
|
6373
|
|
6374 ;; Reading with history.
|
|
6375 (if (>= ver 19)
|
|
6376
|
|
6377 (defun dired-read-with-history (prompt initial history)
|
|
6378 (read-from-minibuffer prompt initial nil nil history))
|
|
6379
|
|
6380 (defun dired-read-with-history (prompt initial history)
|
|
6381 (let ((minibuffer-history-symbol history)) ; for gmhist
|
|
6382 (read-string prompt initial))))
|
|
6383
|
|
6384 ;; Completing read with history.
|
|
6385 (if (>= ver 19)
|
|
6386
|
|
6387 (fset 'dired-completing-read 'completing-read)
|
|
6388
|
|
6389 (defun dired-completing-read (prompt table &optional predicate
|
|
6390 require-match initial-input history)
|
|
6391 (let ((minibuffer-history-symbol history)) ; for gmhist
|
|
6392 (completing-read prompt table predicate require-match
|
|
6393 initial-input))))
|
|
6394
|
|
6395 ;; Abbreviating file names.
|
|
6396 (if lucid-p
|
|
6397 (fset 'dired-abbreviate-file-name
|
|
6398 ;; Lemacs has this extra hack-homedir arg
|
|
6399 (function
|
|
6400 (lambda (fn)
|
|
6401 (abbreviate-file-name fn t))))
|
|
6402 (fset 'dired-abbreviate-file-name 'abbreviate-file-name))
|
|
6403
|
|
6404 ;; Deleting directories
|
|
6405 ;; Check for pre 19.8 versions of lucid emacs.
|
|
6406 (if lucid-p
|
|
6407 (or (fboundp 'delete-directory)
|
|
6408 (fset 'delete-directory 'remove-directory)))
|
|
6409
|
|
6410 ;; Minibuffers
|
|
6411 (if (= ver 18)
|
|
6412
|
|
6413 (defun dired-get-active-minibuffer-window ()
|
|
6414 (and (> (minibuffer-depth) 0)
|
|
6415 (minibuffer-window)))
|
|
6416
|
|
6417 (defun dired-get-active-minibuffer-window ()
|
|
6418 (let ((frames (frame-list))
|
|
6419 win found)
|
|
6420 (while frames
|
|
6421 (if (and (setq win (minibuffer-window (car frames)))
|
|
6422 (minibuffer-window-active-p win))
|
|
6423 (setq found win
|
|
6424 frames nil)
|
|
6425 (setq frames (cdr frames))))
|
|
6426 found)))
|
|
6427
|
|
6428 ;; Text properties and menus.
|
|
6429
|
|
6430 (cond
|
|
6431 (lucid-p
|
|
6432 (require 'dired-xemacs))
|
|
6433 ((>= ver 19)
|
|
6434 (require 'dired-fsf))
|
|
6435 (t
|
|
6436 ;; text property stuff doesn't work in V18
|
|
6437 (fset 'dired-insert-set-properties 'ignore)
|
|
6438 (fset 'dired-remove-text-properties 'ignore)
|
|
6439 (fset 'dired-set-text-properties 'ignore)
|
|
6440 (fset 'dired-move-to-filename 'dired-manual-move-to-filename)
|
|
6441 (fset 'dired-move-to-end-of-filename
|
|
6442 'dired-manual-move-to-end-of-filename))))
|
|
6443
|
|
6444 ;;; MULE
|
|
6445
|
|
6446 (if (or (boundp 'MULE) (featurep 'mule)) (load "dired-mule"))
|
|
6447
|
|
6448
|
|
6449 ;; Run load hook for user customization.
|
|
6450 (run-hooks 'dired-load-hook)
|
|
6451
|
|
6452 ;;; end of dired.el
|