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