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