]> code.delx.au - gnu-emacs/blob - lisp/dired.el
Fix comments.
[gnu-emacs] / lisp / dired.el
1 ;;; dired.el --- directory-browsing commands
2
3 ;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 97, 2000, 01, 03, 2004
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
7 ;; Maintainer: FSF
8 ;; Keywords: files
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This is a major mode for directory browsing and editing. It is
30 ;; documented in the Emacs manual.
31
32 ;; Rewritten in 1990/1991 to add tree features, file marking and
33 ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
34 ;; Finished up by rms in 1992.
35
36 ;;; Code:
37
38 ;;; Customizable variables
39
40 (defgroup dired nil
41 "Directory editing."
42 :link '(custom-manual "(emacs)Dired")
43 :group 'files)
44
45 (defgroup dired-mark nil
46 "Handling marks in Dired."
47 :prefix "dired-"
48 :group 'dired)
49
50
51 ;;;###autoload
52 (defcustom dired-listing-switches "-al"
53 "*Switches passed to `ls' for dired. MUST contain the `l' option.
54 May contain all other options that don't contradict `-l';
55 may contain even `F', `b', `i' and `s'. See also the variable
56 `dired-ls-F-marks-symlinks' concerning the `F' switch.
57 On systems such as MS-DOS and MS-Windows, which use `ls' emulation in Lisp,
58 some of the `ls' switches are not supported; see the doc string of
59 `insert-directory' on ls-lisp.el for more details."
60 :type 'string
61 :group 'dired)
62
63 ; Don't use absolute file names as /bin should be in any PATH and people
64 ; may prefer /usr/local/gnu/bin or whatever. However, chown is
65 ; usually not in PATH.
66
67 ;;;###autoload
68 (defvar dired-chown-program
69 (if (memq system-type '(hpux dgux usg-unix-v irix linux gnu/linux cygwin))
70 "chown"
71 (if (file-exists-p "/usr/sbin/chown")
72 "/usr/sbin/chown"
73 "/etc/chown"))
74 "Name of chown command (usually `chown' or `/etc/chown').")
75
76 (defvar dired-use-ls-dired (not (not (string-match "gnu" system-configuration)))
77 "Non-nil means Dired should use `ls --dired'.")
78
79 (defvar dired-chmod-program "chmod"
80 "Name of chmod command (usually `chmod').")
81
82 ;;;###autoload
83 (defcustom dired-ls-F-marks-symlinks nil
84 "*Informs dired about how `ls -lF' marks symbolic links.
85 Set this to t if `ls' (or whatever program is specified by
86 `insert-directory-program') with `-lF' marks the symbolic link
87 itself with a trailing @ (usually the case under Ultrix).
88
89 Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
90 nil (the default), if it gives `bar@ -> foo', set it to t.
91
92 Dired checks if there is really a @ appended. Thus, if you have a
93 marking `ls' program on one host and a non-marking on another host, and
94 don't care about symbolic links which really end in a @, you can
95 always set this variable to t."
96 :type 'boolean
97 :group 'dired-mark)
98
99 ;;;###autoload
100 (defcustom dired-trivial-filenames "^\\.\\.?$\\|^#"
101 "*Regexp of files to skip when finding first file of a directory.
102 A value of nil means move to the subdir line.
103 A value of t means move to first file."
104 :type '(choice (const :tag "Move to subdir" nil)
105 (const :tag "Move to first" t)
106 regexp)
107 :group 'dired)
108
109 ;;;###autoload
110 (defcustom dired-keep-marker-rename t
111 ;; Use t as default so that moved files "take their markers with them".
112 "*Controls marking of renamed files.
113 If t, files keep their previous marks when they are renamed.
114 If a character, renamed files (whether previously marked or not)
115 are afterward marked with that character."
116 :type '(choice (const :tag "Keep" t)
117 (character :tag "Mark"))
118 :group 'dired-mark)
119
120 ;;;###autoload
121 (defcustom dired-keep-marker-copy ?C
122 "*Controls marking of copied files.
123 If t, copied files are marked if and as the corresponding original files were.
124 If a character, copied files are unconditionally marked with that character."
125 :type '(choice (const :tag "Keep" t)
126 (character :tag "Mark"))
127 :group 'dired-mark)
128
129 ;;;###autoload
130 (defcustom dired-keep-marker-hardlink ?H
131 "*Controls marking of newly made hard links.
132 If t, they are marked if and as the files linked to were marked.
133 If a character, new links are unconditionally marked with that character."
134 :type '(choice (const :tag "Keep" t)
135 (character :tag "Mark"))
136 :group 'dired-mark)
137
138 ;;;###autoload
139 (defcustom dired-keep-marker-symlink ?Y
140 "*Controls marking of newly made symbolic links.
141 If t, they are marked if and as the files linked to were marked.
142 If a character, new links are unconditionally marked with that character."
143 :type '(choice (const :tag "Keep" t)
144 (character :tag "Mark"))
145 :group 'dired-mark)
146
147 ;;;###autoload
148 (defcustom dired-dwim-target nil
149 "*If non-nil, dired tries to guess a default target directory.
150 This means: if there is a dired buffer displayed in the next window,
151 use its current subdir, instead of the current subdir of this dired buffer.
152
153 The target is used in the prompt for file copy, rename etc."
154 :type 'boolean
155 :group 'dired)
156
157 ;;;###autoload
158 (defcustom dired-copy-preserve-time t
159 "*If non-nil, Dired preserves the last-modified time in a file copy.
160 \(This works on only some systems.)"
161 :type 'boolean
162 :group 'dired)
163
164 ; These variables were deleted and the replacements are on files.el.
165 ; We leave aliases behind for back-compatibility.
166 (defvaralias 'dired-free-space-program 'directory-free-space-program)
167 (defvaralias 'dired-free-space-args 'directory-free-space-args)
168
169 ;;; Hook variables
170
171 (defcustom dired-load-hook nil
172 "Run after loading dired.
173 You can customize key bindings or load extensions with this."
174 :group 'dired
175 :type 'hook)
176
177 (defcustom dired-mode-hook nil
178 "Run at the very end of dired-mode."
179 :group 'dired
180 :type 'hook)
181
182 (defcustom dired-before-readin-hook nil
183 "This hook is run before a dired buffer is read in (created or reverted)."
184 :group 'dired
185 :type 'hook)
186
187 (defcustom dired-after-readin-hook nil
188 "Hook run after each time a file or directory is read by Dired.
189 After each listing of a file or directory, this hook is run
190 with the buffer narrowed to the listing."
191 :group 'dired
192 :type 'hook)
193 ;; Note this can't simply be run inside function `dired-ls' as the hook
194 ;; functions probably depend on the dired-subdir-alist to be OK.
195
196 ;; Fixme: This should use mailcap.
197 (defcustom dired-view-command-alist
198 '(("[.]\\(ps\\|ps_pages\\|eps\\)\\'" . "gv -spartan -color -watch %s")
199 ("[.]pdf\\'" . "xpdf %s")
200 ("[.]\\(jpe?g\\|gif\\|png\\)\\'" . "eog %s")
201 ("[.]dvi\\'" . "xdvi -sidemargin 0.5 -topmargin 1 %s"))
202 "Alist specifying how to view special types of files.
203 Each element has the form (REGEXP . SHELL-COMMAND).
204 When the file name matches REGEXP, `dired-view-file'
205 invokes SHELL-COMMAND to view the file, processing it through `format'.
206 Use `%s' in SHELL-COMMAND to specify where to put the file name."
207 :group 'dired
208 :type '(alist :key-type regexp :value-type string)
209 :version "21.4")
210
211 ;; Internal variables
212
213 (defvar dired-marker-char ?* ; the answer is 42
214 ;; so that you can write things like
215 ;; (let ((dired-marker-char ?X))
216 ;; ;; great code using X markers ...
217 ;; )
218 ;; For example, commands operating on two sets of files, A and B.
219 ;; Or marking files with digits 0-9. This could implicate
220 ;; concentric sets or an order for the marked files.
221 ;; The code depends on dynamic scoping on the marker char.
222 "In Dired, the current mark character.
223 This is what the `do' commands look for and what the `mark' commands store.")
224
225 (defvar dired-del-marker ?D
226 "Character used to flag files for deletion.")
227
228 (defvar dired-shrink-to-fit
229 t
230 ;; I see no reason ever to make this nil -- rms.
231 ;; (> baud-rate search-slow-speed)
232 "Non-nil means Dired shrinks the display buffer to fit the marked files.")
233
234 (defvar dired-flagging-regexp nil);; Last regexp used to flag files.
235
236 (defvar dired-file-version-alist)
237
238 ;;;###autoload
239 (defvar dired-directory nil
240 "The directory name or wildcard spec that this Dired directory lists.
241 Local to each dired buffer. May be a list, in which case the car is the
242 directory name and the cdr is the list of files to mention.
243 The directory name must be absolute, but need not be fully expanded.")
244
245 (defvar dired-actual-switches nil
246 "The value of `dired-listing-switches' used to make this buffer's text.")
247
248 (defvar dired-re-inode-size "[0-9 \t]*"
249 "Regexp for optional initial inode and file size as made by `ls -i -s'.")
250
251 ;; These regexps must be tested at beginning-of-line, but are also
252 ;; used to search for next matches, so neither omitting "^" nor
253 ;; replacing "^" by "\n" (to make it slightly faster) will work.
254
255 (defvar dired-re-mark "^[^ \n]")
256 ;; "Regexp matching a marked line.
257 ;; Important: the match ends just after the marker."
258 (defvar dired-re-maybe-mark "^. ")
259 ;; The [^:] part after "d" and "l" is to avoid confusion with the
260 ;; DOS/Windows-style drive letters in directory names, like in "d:/foo".
261 (defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d[^:]"))
262 (defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l[^:]"))
263 (defvar dired-re-exe;; match ls permission string of an executable file
264 (mapconcat (function
265 (lambda (x)
266 (concat dired-re-maybe-mark dired-re-inode-size x)))
267 '("-[-r][-w][xs][-r][-w].[-r][-w]."
268 "-[-r][-w].[-r][-w][xs][-r][-w]."
269 "-[-r][-w].[-r][-w].[-r][-w][xst]")
270 "\\|"))
271 (defvar dired-re-perms "[-bcdlps][-r][-w].[-r][-w].[-r][-w].")
272 (defvar dired-re-dot "^.* \\.\\.?/?$")
273
274 ;; The subdirectory names in this list are expanded.
275 (defvar dired-subdir-alist nil
276 "Association list of subdirectories and their buffer positions.
277 Each subdirectory has an element: (DIRNAME . STARTMARKER).
278 The order of elements is the reverse of the order in the buffer.
279 In simple cases, this list contains one element.")
280
281 (defvar dired-subdir-regexp "^. \\([^\n\r]+\\)\\(:\\)[\n\r]"
282 "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
283 Subexpression 1 is the subdirectory proper, no trailing colon.
284 The match starts at the beginning of the line and ends after the end
285 of the line (\\n or \\r).
286 Subexpression 2 must end right before the \\n or \\r.")
287
288 (defvar dired-font-lock-keywords
289 (list
290 ;;
291 ;; Directory headers.
292 (list dired-subdir-regexp '(1 font-lock-type-face))
293 ;;
294 ;; We make heavy use of MATCH-ANCHORED, since the regexps don't identify the
295 ;; file name itself. We search for Dired defined regexps, and then use the
296 ;; Dired defined function `dired-move-to-filename' before searching for the
297 ;; simple regexp ".+". It is that regexp which matches the file name.
298 ;;
299 ;; Dired marks.
300 (list dired-re-mark
301 '(0 font-lock-constant-face)
302 '(".+" (dired-move-to-filename) nil (0 font-lock-warning-face)))
303 ;; People who are paranoid about security would consider this more
304 ;; important than other things such as whether it is a directory.
305 ;; But we don't want to encourage paranoia, so our default
306 ;; should be what's most useful for non-paranoids. -- rms.
307 ;;; ;;
308 ;;; ;; Files that are group or world writable.
309 ;;; (list (concat dired-re-maybe-mark dired-re-inode-size
310 ;;; "\\([-d]\\(....w....\\|.......w.\\)\\)")
311 ;;; '(1 font-lock-comment-face)
312 ;;; '(".+" (dired-move-to-filename) nil (0 font-lock-comment-face)))
313 ;; However, we don't need to highlight the file name, only the
314 ;; permissions, to win generally. -- fx.
315 ;; Fixme: we could also put text properties on the permission
316 ;; fields with keymaps to frob the permissions, somewhat a la XEmacs.
317 (list (concat dired-re-maybe-mark dired-re-inode-size
318 "[-d]....\\(w\\)..\\(w\\).") ; group writable
319 '(1 font-lock-warning-face))
320 (list (concat dired-re-maybe-mark dired-re-inode-size
321 "[-d]....\\(w\\)....") ; world writable
322 '(1 font-lock-comment-face))
323 ;;
324 ;; Subdirectories.
325 (list dired-re-dir
326 '(".+" (dired-move-to-filename) nil (0 font-lock-function-name-face)))
327 ;;
328 ;; Symbolic links.
329 (list dired-re-sym
330 '(".+" (dired-move-to-filename) nil (0 font-lock-keyword-face)))
331 ;;
332 ;; Files suffixed with `completion-ignored-extensions'.
333 '(eval .
334 ;; It is quicker to first find just an extension, then go back to the
335 ;; start of that file name. So we do this complex MATCH-ANCHORED form.
336 (list (concat "\\(" (regexp-opt completion-ignored-extensions) "\\|#\\)$")
337 '(".+" (dired-move-to-filename) nil (0 font-lock-string-face)))))
338 "Additional expressions to highlight in Dired mode.")
339 \f
340 ;;; Macros must be defined before they are used, for the byte compiler.
341
342 (defmacro dired-mark-if (predicate msg)
343 "Mark all files for which PREDICATE evals to non-nil.
344 PREDICATE is evaluated on each line, with point at beginning of line.
345 MSG is a noun phrase for the type of files being marked.
346 It should end with a noun that can be pluralized by adding `s'.
347 Return value is the number of files marked, or nil if none were marked."
348 `(let (buffer-read-only count)
349 (save-excursion
350 (setq count 0)
351 (if ,msg (message "Marking %ss..." ,msg))
352 (goto-char (point-min))
353 (while (not (eobp))
354 (if ,predicate
355 (progn
356 (delete-char 1)
357 (insert dired-marker-char)
358 (setq count (1+ count))))
359 (forward-line 1))
360 (if ,msg (message "%s %s%s %s%s."
361 count
362 ,msg
363 (dired-plural-s count)
364 (if (eq dired-marker-char ?\040) "un" "")
365 (if (eq dired-marker-char dired-del-marker)
366 "flagged" "marked"))))
367 (and (> count 0) count)))
368
369 (defmacro dired-map-over-marks (body arg &optional show-progress)
370 "Eval BODY with point on each marked line. Return a list of BODY's results.
371 If no marked file could be found, execute BODY on the current line.
372 If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
373 files instead of the marked files.
374 In that case point is dragged along. This is so that commands on
375 the next ARG (instead of the marked) files can be chained easily.
376 If ARG is otherwise non-nil, use current file instead.
377 If optional third arg SHOW-PROGRESS evaluates to non-nil,
378 redisplay the dired buffer after each file is processed.
379 No guarantee is made about the position on the marked line.
380 BODY must ensure this itself if it depends on this.
381 Search starts at the beginning of the buffer, thus the car of the list
382 corresponds to the line nearest to the buffer's bottom. This
383 is also true for (positive and negative) integer values of ARG.
384 BODY should not be too long as it is expanded four times."
385 ;;
386 ;;Warning: BODY must not add new lines before point - this may cause an
387 ;;endless loop.
388 ;;This warning should not apply any longer, sk 2-Sep-1991 14:10.
389 `(prog1
390 (let (buffer-read-only case-fold-search found results)
391 (if ,arg
392 (if (integerp ,arg)
393 (progn ;; no save-excursion, want to move point.
394 (dired-repeat-over-lines
395 ,arg
396 (function (lambda ()
397 (if ,show-progress (sit-for 0))
398 (setq results (cons ,body results)))))
399 (if (< ,arg 0)
400 (nreverse results)
401 results))
402 ;; non-nil, non-integer ARG means use current file:
403 (list ,body))
404 (let ((regexp (dired-marker-regexp)) next-position)
405 (save-excursion
406 (goto-char (point-min))
407 ;; remember position of next marked file before BODY
408 ;; can insert lines before the just found file,
409 ;; confusing us by finding the same marked file again
410 ;; and again and...
411 (setq next-position (and (re-search-forward regexp nil t)
412 (point-marker))
413 found (not (null next-position)))
414 (while next-position
415 (goto-char next-position)
416 (if ,show-progress (sit-for 0))
417 (setq results (cons ,body results))
418 ;; move after last match
419 (goto-char next-position)
420 (forward-line 1)
421 (set-marker next-position nil)
422 (setq next-position (and (re-search-forward regexp nil t)
423 (point-marker)))))
424 (if found
425 results
426 (list ,body)))))
427 ;; save-excursion loses, again
428 (dired-move-to-filename)))
429
430 (defun dired-get-marked-files (&optional localp arg filter)
431 "Return the marked files' names as list of strings.
432 The list is in the same order as the buffer, that is, the car is the
433 first marked file.
434 Values returned are normally absolute file names.
435 Optional arg LOCALP as in `dired-get-filename'.
436 Optional second argument ARG specifies files near point
437 instead of marked files. If ARG is an integer, use the next ARG files.
438 If ARG is otherwise non-nil, use file. Usually ARG comes from
439 the command's prefix arg.
440 Optional third argument FILTER, if non-nil, is a function to select
441 some of the files--those for which (funcall FILTER FILENAME) is non-nil."
442 (let ((all-of-them
443 (save-excursion
444 (dired-map-over-marks (dired-get-filename localp) arg)))
445 result)
446 (if (not filter)
447 (nreverse all-of-them)
448 (dolist (file all-of-them)
449 (if (funcall filter file)
450 (push file result)))
451 result)))
452 \f
453 ;; The dired command
454
455 (defun dired-read-dir-and-switches (str)
456 ;; For use in interactive.
457 (reverse (list
458 (if current-prefix-arg
459 (read-string "Dired listing switches: "
460 dired-listing-switches))
461 (read-file-name (format "Dired %s(directory): " str)
462 nil default-directory nil))))
463
464 ;;;###autoload (define-key ctl-x-map "d" 'dired)
465 ;;;###autoload
466 (defun dired (dirname &optional switches)
467 "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
468 Optional second argument SWITCHES specifies the `ls' options used.
469 \(Interactively, use a prefix argument to be able to specify SWITCHES.)
470 Dired displays a list of files in DIRNAME (which may also have
471 shell wildcards appended to select certain files). If DIRNAME is a cons,
472 its first element is taken as the directory name and the rest as an explicit
473 list of files to make directory entries for.
474 \\<dired-mode-map>\
475 You can move around in it with the usual commands.
476 You can flag files for deletion with \\[dired-flag-file-deletion] and then
477 delete them by typing \\[dired-do-flagged-delete].
478 Type \\[describe-mode] after entering dired for more info.
479
480 If DIRNAME is already in a dired buffer, that buffer is used without refresh."
481 ;; Cannot use (interactive "D") because of wildcards.
482 (interactive (dired-read-dir-and-switches ""))
483 (switch-to-buffer (dired-noselect dirname switches)))
484
485 ;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
486 ;;;###autoload
487 (defun dired-other-window (dirname &optional switches)
488 "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
489 (interactive (dired-read-dir-and-switches "in other window "))
490 (switch-to-buffer-other-window (dired-noselect dirname switches)))
491
492 ;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
493 ;;;###autoload
494 (defun dired-other-frame (dirname &optional switches)
495 "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame."
496 (interactive (dired-read-dir-and-switches "in other frame "))
497 (switch-to-buffer-other-frame (dired-noselect dirname switches)))
498
499 ;;;###autoload
500 (defun dired-noselect (dir-or-list &optional switches)
501 "Like `dired' but returns the dired buffer as value, does not select it."
502 (or dir-or-list (setq dir-or-list default-directory))
503 ;; This loses the distinction between "/foo/*/" and "/foo/*" that
504 ;; some shells make:
505 (let (dirname initially-was-dirname)
506 (if (consp dir-or-list)
507 (setq dirname (car dir-or-list))
508 (setq dirname dir-or-list))
509 (setq initially-was-dirname
510 (string= (file-name-as-directory dirname) dirname))
511 (setq dirname (abbreviate-file-name
512 (expand-file-name (directory-file-name dirname))))
513 (if find-file-visit-truename
514 (setq dirname (file-truename dirname)))
515 ;; If the argument was syntactically a directory name not a file name,
516 ;; or if it happens to name a file that is a directory,
517 ;; convert it syntactically to a directory name.
518 ;; The reason for checking initially-was-dirname
519 ;; and not just file-directory-p
520 ;; is that file-directory-p is slow over ftp.
521 (if (or initially-was-dirname (file-directory-p dirname))
522 (setq dirname (file-name-as-directory dirname)))
523 (if (consp dir-or-list)
524 (setq dir-or-list (cons dirname (cdr dir-or-list)))
525 (setq dir-or-list dirname))
526 (dired-internal-noselect dir-or-list switches)))
527
528 ;; The following is an internal dired function. It returns non-nil if
529 ;; the directory visited by the current dired buffer has changed on
530 ;; disk. DIRNAME should be the directory name of that directory.
531 (defun dired-directory-changed-p (dirname)
532 (not (let ((attributes (file-attributes dirname))
533 (modtime (visited-file-modtime)))
534 (or (eq modtime 0)
535 (not (eq (car attributes) t))
536 (and (= (car (nth 5 attributes)) (car modtime))
537 (= (nth 1 (nth 5 attributes)) (cdr modtime)))))))
538
539 (defun dired-buffer-stale-p (&optional noconfirm)
540 "Return non-nil if current dired buffer needs updating.
541 If NOCONFIRM is non-nil, then this function always returns nil
542 for a remote directory. This feature is used by Auto Revert Mode."
543 (let ((dirname
544 (if (consp dired-directory) (car dired-directory) dired-directory)))
545 (and (stringp dirname)
546 (not (when noconfirm (file-remote-p dirname)))
547 (file-readable-p dirname)
548 (dired-directory-changed-p dirname))))
549
550 ;; Separate function from dired-noselect for the sake of dired-vms.el.
551 (defun dired-internal-noselect (dir-or-list &optional switches mode)
552 ;; If there is an existing dired buffer for DIRNAME, just leave
553 ;; buffer as it is (don't even call dired-revert).
554 ;; This saves time especially for deep trees or with ange-ftp.
555 ;; The user can type `g' easily, and it is more consistent with find-file.
556 ;; But if SWITCHES are given they are probably different from the
557 ;; buffer's old value, so call dired-sort-other, which does
558 ;; revert the buffer.
559 ;; A pity we can't possibly do "Directory has changed - refresh? "
560 ;; like find-file does.
561 ;; Optional argument MODE is passed to dired-find-buffer-nocreate,
562 ;; see there.
563 (let* (dirname
564 buffer
565 ;; note that buffer already is in dired-mode, if found
566 new-buffer-p
567 (old-buf (current-buffer)))
568 (if (consp dir-or-list)
569 (setq dirname (car dir-or-list))
570 (setq dirname dir-or-list))
571 ;; Look for an existing buffer.
572 (setq buffer (dired-find-buffer-nocreate dirname mode)
573 new-buffer-p (null buffer))
574 (or buffer
575 (let ((default-major-mode 'fundamental-mode))
576 ;; We don't want default-major-mode to run hooks and set auto-fill
577 ;; or whatever, now that dired-mode does not
578 ;; kill-all-local-variables any longer.
579 (setq buffer (create-file-buffer (directory-file-name dirname)))))
580 (set-buffer buffer)
581 (if (not new-buffer-p) ; existing buffer ...
582 (cond (switches ; ... but new switches
583 ;; file list may have changed
584 (setq dired-directory dir-or-list)
585 ;; this calls dired-revert
586 (dired-sort-other switches))
587 ;; If directory has changed on disk, offer to revert.
588 ((when (dired-directory-changed-p dirname)
589 (message "%s"
590 (substitute-command-keys
591 "Directory has changed on disk; type \\[revert-buffer] to update Dired")))))
592 ;; Else a new buffer
593 (setq default-directory
594 ;; We can do this unconditionally
595 ;; because dired-noselect ensures that the name
596 ;; is passed in directory name syntax
597 ;; if it was the name of a directory at all.
598 (file-name-directory dirname))
599 (or switches (setq switches dired-listing-switches))
600 (if mode (funcall mode)
601 (dired-mode dir-or-list switches))
602 ;; default-directory and dired-actual-switches are set now
603 ;; (buffer-local), so we can call dired-readin:
604 (let ((failed t))
605 (unwind-protect
606 (progn (dired-readin)
607 (setq failed nil))
608 ;; dired-readin can fail if parent directories are inaccessible.
609 ;; Don't leave an empty buffer around in that case.
610 (if failed (kill-buffer buffer))))
611 (goto-char (point-min))
612 (dired-initial-position dirname))
613 (set-buffer old-buf)
614 buffer))
615
616 (defvar dired-buffers nil
617 ;; Enlarged by dired-advertise
618 ;; Queried by function dired-buffers-for-dir. When this detects a
619 ;; killed buffer, it is removed from this list.
620 "Alist of expanded directories and their associated dired buffers.")
621
622 (defun dired-find-buffer-nocreate (dirname &optional mode)
623 ;; This differs from dired-buffers-for-dir in that it does not consider
624 ;; subdirs of default-directory and searches for the first match only.
625 ;; Also, the major mode must be MODE.
626 (setq dirname (expand-file-name dirname))
627 (let (found (blist dired-buffers)) ; was (buffer-list)
628 (or mode (setq mode 'dired-mode))
629 (while blist
630 (if (null (buffer-name (cdr (car blist))))
631 (setq blist (cdr blist))
632 (save-excursion
633 (set-buffer (cdr (car blist)))
634 (if (and (eq major-mode mode)
635 dired-directory ;; nil during find-alternate-file
636 (equal dirname
637 (expand-file-name
638 (if (consp dired-directory)
639 (car dired-directory)
640 dired-directory))))
641 (setq found (cdr (car blist))
642 blist nil)
643 (setq blist (cdr blist))))))
644 found))
645
646 \f
647 ;; Read in a new dired buffer
648
649 (defun dired-readin ()
650 "Read in a new dired buffer.
651 Differs from dired-insert-subdir in that it accepts
652 wildcards, erases the buffer, and builds the subdir-alist anew
653 \(including making it buffer-local and clearing it first)."
654
655 ;; default-directory and dired-actual-switches must be buffer-local
656 ;; and initialized by now.
657 (let (dirname)
658 (if (consp dired-directory)
659 (setq dirname (car dired-directory))
660 (setq dirname dired-directory))
661 (setq dirname (expand-file-name dirname))
662 (save-excursion
663 ;; This hook which may want to modify dired-actual-switches
664 ;; based on dired-directory, e.g. with ange-ftp to a SysV host
665 ;; where ls won't understand -Al switches.
666 (run-hooks 'dired-before-readin-hook)
667 (if (consp buffer-undo-list)
668 (setq buffer-undo-list nil))
669 (let (buffer-read-only
670 ;; Don't make undo entries for readin.
671 (buffer-undo-list t))
672 (widen)
673 (erase-buffer)
674 (dired-readin-insert))
675 (goto-char (point-min))
676 ;; Must first make alist buffer local and set it to nil because
677 ;; dired-build-subdir-alist will call dired-clear-alist first
678 (set (make-local-variable 'dired-subdir-alist) nil)
679 (dired-build-subdir-alist)
680 (let ((attributes (file-attributes dirname)))
681 (if (eq (car attributes) t)
682 (set-visited-file-modtime (nth 5 attributes))))
683 (set-buffer-modified-p nil)
684 ;; No need to narrow since the whole buffer contains just
685 ;; dired-readin's output, nothing else. The hook can
686 ;; successfully use dired functions (e.g. dired-get-filename)
687 ;; as the subdir-alist has been built in dired-readin.
688 (run-hooks 'dired-after-readin-hook))))
689
690 ;; Subroutines of dired-readin
691
692 (defun dired-readin-insert ()
693 ;; Insert listing for the specified dir (and maybe file list)
694 ;; already in dired-directory, assuming a clean buffer.
695 (let (dir file-list)
696 (if (consp dired-directory)
697 (setq dir (car dired-directory)
698 file-list (cdr dired-directory))
699 (setq dir dired-directory
700 file-list nil))
701 (setq dir (expand-file-name dir))
702 (if (and (equal "" (file-name-nondirectory dir))
703 (not file-list))
704 ;; If we are reading a whole single directory...
705 (dired-insert-directory dir dired-actual-switches nil nil t)
706 (if (not (file-readable-p
707 (directory-file-name (file-name-directory dir))))
708 (error "Directory %s inaccessible or nonexistent" dir)
709 ;; Else treat it as a wildcard spec
710 ;; unless we have an explicit list of files.
711 (dired-insert-directory dir dired-actual-switches
712 file-list (not file-list) t)))))
713
714 (defun dired-insert-directory (dir switches &optional file-list wildcard hdr)
715 "Insert a directory listing of DIR, Dired style.
716 Use SWITCHES to make the listings.
717 If FILE-LIST is non-nil, list only those files.
718 Otherwise, if WILDCARD is non-nil, expand wildcards;
719 in that case, DIR should be a file name that uses wildcards.
720 In other cases, DIR should be a directory name or a directory filename.
721 If HDR is non-nil, insert a header line with the directory name."
722 (let ((opoint (point))
723 (process-environment (copy-sequence process-environment))
724 end)
725 (if (or dired-use-ls-dired (file-remote-p dir))
726 (setq switches (concat "--dired " switches)))
727 ;; We used to specify the C locale here, to force English month names;
728 ;; but this should not be necessary any more,
729 ;; with the new value of dired-move-to-filename-regexp.
730 (if file-list
731 (dolist (f file-list)
732 (insert-directory f switches nil nil))
733 (insert-directory dir switches wildcard (not wildcard)))
734 ;; Quote certain characters, unless ls quoted them for us.
735 (if (not (string-match "b" dired-actual-switches))
736 (save-excursion
737 (setq end (point-marker))
738 (goto-char opoint)
739 (while (search-forward "\\" end t)
740 (replace-match (apply #'propertize
741 "\\\\"
742 (text-properties-at (match-beginning 0)))
743 nil t))
744 (goto-char opoint)
745 (while (search-forward "\^m" end t)
746 (replace-match (apply #'propertize
747 "\\015"
748 (text-properties-at (match-beginning 0)))
749 nil t))
750 (set-marker end nil)))
751 (dired-insert-set-properties opoint (point))
752 ;; If we used --dired and it worked, the lines are already indented.
753 ;; Otherwise, indent them.
754 (unless (save-excursion
755 (goto-char opoint)
756 (looking-at " "))
757 (let ((indent-tabs-mode nil))
758 (indent-rigidly opoint (point) 2)))
759 ;; Insert text at the beginning to standardize things.
760 (save-excursion
761 (goto-char opoint)
762 (if (and (or hdr wildcard) (not (looking-at "^ /.*:$")))
763 ;; Note that dired-build-subdir-alist will replace the name
764 ;; by its expansion, so it does not matter whether what we insert
765 ;; here is fully expanded, but it should be absolute.
766 (insert " " (directory-file-name (file-name-directory dir)) ":\n"))
767 (when wildcard
768 ;; Insert "wildcard" line where "total" line would be for a full dir.
769 (insert " wildcard " (file-name-nondirectory dir) "\n")))))
770
771 (defun dired-insert-set-properties (beg end)
772 "Make the file names highlight when the mouse is on them."
773 (save-excursion
774 (goto-char beg)
775 (while (< (point) end)
776 (condition-case nil
777 (if (dired-move-to-filename)
778 (add-text-properties
779 (point)
780 (save-excursion
781 (dired-move-to-end-of-filename)
782 (point))
783 '(mouse-face highlight
784 help-echo "mouse-2: visit this file in other window")))
785 (error nil))
786 (forward-line 1))))
787 \f
788 ;; Reverting a dired buffer
789
790 (defun dired-revert (&optional arg noconfirm)
791 "Reread the dired buffer.
792 Must also be called after dired-actual-switches have changed.
793 Should not fail even on completely garbaged buffers.
794 Preserves old cursor, marks/flags, hidden-p."
795 (widen) ; just in case user narrowed
796 (let ((opoint (point))
797 (ofile (dired-get-filename nil t))
798 (mark-alist nil) ; save marked files
799 (hidden-subdirs (dired-remember-hidden))
800 (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
801 (case-fold-search nil) ; we check for upper case ls flags
802 buffer-read-only)
803 (goto-char (point-min))
804 (setq mark-alist;; only after dired-remember-hidden since this unhides:
805 (dired-remember-marks (point-min) (point-max)))
806 ;; treat top level dir extra (it may contain wildcards)
807 (dired-uncache
808 (if (consp dired-directory) (car dired-directory) dired-directory))
809 (dired-readin)
810 (let ((dired-after-readin-hook nil))
811 ;; don't run that hook for each subdir...
812 (dired-insert-old-subdirs old-subdir-alist))
813 (dired-mark-remembered mark-alist) ; mark files that were marked
814 ;; ... run the hook for the whole buffer, and only after markers
815 ;; have been reinserted (else omitting in dired-x would omit marked files)
816 (run-hooks 'dired-after-readin-hook) ; no need to narrow
817 (or (and ofile (dired-goto-file ofile)) ; move cursor to where it
818 (goto-char opoint)) ; was before
819 (dired-move-to-filename)
820 (save-excursion ; hide subdirs that were hidden
821 (dolist (dir hidden-subdirs)
822 (if (dired-goto-subdir dir)
823 (dired-hide-subdir 1)))))
824 ;; outside of the let scope
825 ;;; Might as well not override the user if the user changed this.
826 ;;; (setq buffer-read-only t)
827 )
828
829 ;; Subroutines of dired-revert
830 ;; Some of these are also used when inserting subdirs.
831
832 (defun dired-remember-marks (beg end)
833 "Return alist of files and their marks, from BEG to END."
834 (if selective-display ; must unhide to make this work.
835 (let (buffer-read-only)
836 (subst-char-in-region beg end ?\r ?\n)))
837 (let (fil chr alist)
838 (save-excursion
839 (goto-char beg)
840 (while (re-search-forward dired-re-mark end t)
841 (if (setq fil (dired-get-filename nil t))
842 (setq chr (preceding-char)
843 alist (cons (cons fil chr) alist)))))
844 alist))
845
846 (defun dired-mark-remembered (alist)
847 "Mark all files remembered in ALIST.
848 Each element of ALIST looks like (FILE . MARKERCHAR)."
849 (let (elt fil chr)
850 (while alist
851 (setq elt (car alist)
852 alist (cdr alist)
853 fil (car elt)
854 chr (cdr elt))
855 (if (dired-goto-file fil)
856 (save-excursion
857 (beginning-of-line)
858 (delete-char 1)
859 (insert chr))))))
860
861 (defun dired-remember-hidden ()
862 "Return a list of names of subdirs currently hidden."
863 (let ((l dired-subdir-alist) dir pos result)
864 (while l
865 (setq dir (car (car l))
866 pos (cdr (car l))
867 l (cdr l))
868 (goto-char pos)
869 (skip-chars-forward "^\r\n")
870 (if (eq (following-char) ?\r)
871 (setq result (cons dir result))))
872 result))
873
874 (defun dired-insert-old-subdirs (old-subdir-alist)
875 "Try to insert all subdirs that were displayed before.
876 Do so according to the former subdir alist OLD-SUBDIR-ALIST."
877 (or (string-match "R" dired-actual-switches)
878 (let (elt dir)
879 (while old-subdir-alist
880 (setq elt (car old-subdir-alist)
881 old-subdir-alist (cdr old-subdir-alist)
882 dir (car elt))
883 (condition-case ()
884 (progn
885 (dired-uncache dir)
886 (dired-insert-subdir dir))
887 (error nil))))))
888
889 (defun dired-uncache (dir)
890 "Remove directory DIR from any directory cache."
891 (let ((handler (find-file-name-handler dir 'dired-uncache)))
892 (if handler
893 (funcall handler 'dired-uncache dir))))
894 \f
895 ;; dired mode key bindings and initialization
896
897 (defvar dired-mode-map
898 ;; This looks ugly when substitute-command-keys uses C-d instead d:
899 ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
900 (let ((map (make-keymap)))
901 (suppress-keymap map)
902 (define-key map [mouse-2] 'dired-mouse-find-file-other-window)
903 ;; Commands to mark or flag certain categories of files
904 (define-key map "#" 'dired-flag-auto-save-files)
905 (define-key map "." 'dired-clean-directory)
906 (define-key map "~" 'dired-flag-backup-files)
907 (define-key map "&" 'dired-flag-garbage-files)
908 ;; Upper case keys (except !) for operating on the marked files
909 (define-key map "A" 'dired-do-search)
910 (define-key map "C" 'dired-do-copy)
911 (define-key map "B" 'dired-do-byte-compile)
912 (define-key map "D" 'dired-do-delete)
913 (define-key map "G" 'dired-do-chgrp)
914 (define-key map "H" 'dired-do-hardlink)
915 (define-key map "L" 'dired-do-load)
916 (define-key map "M" 'dired-do-chmod)
917 (define-key map "O" 'dired-do-chown)
918 (define-key map "P" 'dired-do-print)
919 (define-key map "Q" 'dired-do-query-replace-regexp)
920 (define-key map "R" 'dired-do-rename)
921 (define-key map "S" 'dired-do-symlink)
922 (define-key map "X" 'dired-do-shell-command)
923 (define-key map "Z" 'dired-do-compress)
924 (define-key map "!" 'dired-do-shell-command)
925 ;; Comparison commands
926 (define-key map "=" 'dired-diff)
927 (define-key map "\M-=" 'dired-backup-diff)
928 ;; Tree Dired commands
929 (define-key map "\M-\C-?" 'dired-unmark-all-files)
930 (define-key map "\M-\C-d" 'dired-tree-down)
931 (define-key map "\M-\C-u" 'dired-tree-up)
932 (define-key map "\M-\C-n" 'dired-next-subdir)
933 (define-key map "\M-\C-p" 'dired-prev-subdir)
934 ;; move to marked files
935 (define-key map "\M-{" 'dired-prev-marked-file)
936 (define-key map "\M-}" 'dired-next-marked-file)
937 ;; Make all regexp commands share a `%' prefix:
938 ;; We used to get to the submap via a symbol dired-regexp-prefix,
939 ;; but that seems to serve little purpose, and copy-keymap
940 ;; does a better job without it.
941 (define-key map "%" nil)
942 (define-key map "%u" 'dired-upcase)
943 (define-key map "%l" 'dired-downcase)
944 (define-key map "%d" 'dired-flag-files-regexp)
945 (define-key map "%g" 'dired-mark-files-containing-regexp)
946 (define-key map "%m" 'dired-mark-files-regexp)
947 (define-key map "%r" 'dired-do-rename-regexp)
948 (define-key map "%C" 'dired-do-copy-regexp)
949 (define-key map "%H" 'dired-do-hardlink-regexp)
950 (define-key map "%R" 'dired-do-rename-regexp)
951 (define-key map "%S" 'dired-do-symlink-regexp)
952 ;; Commands for marking and unmarking.
953 (define-key map "*" nil)
954 (define-key map "**" 'dired-mark-executables)
955 (define-key map "*/" 'dired-mark-directories)
956 (define-key map "*@" 'dired-mark-symlinks)
957 (define-key map "*%" 'dired-mark-files-regexp)
958 (define-key map "*c" 'dired-change-marks)
959 (define-key map "*s" 'dired-mark-subdir-files)
960 (define-key map "*m" 'dired-mark)
961 (define-key map "*u" 'dired-unmark)
962 (define-key map "*?" 'dired-unmark-all-files)
963 (define-key map "*!" 'dired-unmark-all-marks)
964 (define-key map "U" 'dired-unmark-all-marks)
965 (define-key map "*\177" 'dired-unmark-backward)
966 (define-key map "*\C-n" 'dired-next-marked-file)
967 (define-key map "*\C-p" 'dired-prev-marked-file)
968 (define-key map "*t" 'dired-toggle-marks)
969 ;; Lower keys for commands not operating on all the marked files
970 (define-key map "a" 'dired-find-alternate-file)
971 (define-key map "d" 'dired-flag-file-deletion)
972 (define-key map "e" 'dired-find-file)
973 (define-key map "f" 'dired-find-file)
974 (define-key map "\C-m" 'dired-advertised-find-file)
975 (define-key map "g" 'revert-buffer)
976 (define-key map "\M-g" 'dired-goto-file)
977 (define-key map "h" 'describe-mode)
978 (define-key map "i" 'dired-maybe-insert-subdir)
979 (define-key map "k" 'dired-do-kill-lines)
980 (define-key map "l" 'dired-do-redisplay)
981 (define-key map "m" 'dired-mark)
982 (define-key map "n" 'dired-next-line)
983 (define-key map "o" 'dired-find-file-other-window)
984 (define-key map "\C-o" 'dired-display-file)
985 (define-key map "p" 'dired-previous-line)
986 (define-key map "q" 'quit-window)
987 (define-key map "s" 'dired-sort-toggle-or-edit)
988 (define-key map "t" 'dired-toggle-marks)
989 (define-key map "u" 'dired-unmark)
990 (define-key map "v" 'dired-view-file)
991 (define-key map "w" 'dired-copy-filename-as-kill)
992 (define-key map "x" 'dired-do-flagged-delete)
993 (define-key map "y" 'dired-show-file-type)
994 (define-key map "+" 'dired-create-directory)
995 ;; moving
996 (define-key map "<" 'dired-prev-dirline)
997 (define-key map ">" 'dired-next-dirline)
998 (define-key map "^" 'dired-up-directory)
999 (define-key map " " 'dired-next-line)
1000 (define-key map "\C-n" 'dired-next-line)
1001 (define-key map "\C-p" 'dired-previous-line)
1002 (define-key map [down] 'dired-next-line)
1003 (define-key map [up] 'dired-previous-line)
1004 ;; hiding
1005 (define-key map "$" 'dired-hide-subdir)
1006 (define-key map "\M-$" 'dired-hide-all)
1007 ;; misc
1008 (define-key map "?" 'dired-summary)
1009 (define-key map "\177" 'dired-unmark-backward)
1010 (define-key map "\C-_" 'dired-undo)
1011 (define-key map "\C-xu" 'dired-undo)
1012
1013 ;; Make menu bar items.
1014
1015 ;; No need to fo this, now that top-level items are fewer.
1016 ;;;;
1017 ;; Get rid of the Edit menu bar item to save space.
1018 ;(define-key map [menu-bar edit] 'undefined)
1019
1020 (define-key map [menu-bar subdir]
1021 (cons "Subdir" (make-sparse-keymap "Subdir")))
1022
1023 (define-key map [menu-bar subdir hide-all]
1024 '(menu-item "Hide All" dired-hide-all
1025 :help "Hide all subdirectories, leave only header lines"))
1026 (define-key map [menu-bar subdir hide-subdir]
1027 '(menu-item "Hide/UnHide Subdir" dired-hide-subdir
1028 :help "Hide or unhide current directory listing"))
1029 (define-key map [menu-bar subdir tree-down]
1030 '(menu-item "Tree Down" dired-tree-down
1031 :help "Go to first subdirectory header down the tree"))
1032 (define-key map [menu-bar subdir tree-up]
1033 '(menu-item "Tree Up" dired-tree-up
1034 :help "Go to first subdirectory header up the tree"))
1035 (define-key map [menu-bar subdir up]
1036 '(menu-item "Up Directory" dired-up-directory
1037 :help "Edit the parent directory"))
1038 (define-key map [menu-bar subdir prev-subdir]
1039 '(menu-item "Prev Subdir" dired-prev-subdir
1040 :help "Go to previous subdirectory header line"))
1041 (define-key map [menu-bar subdir next-subdir]
1042 '(menu-item "Next Subdir" dired-next-subdir
1043 :help "Go to next subdirectory header line"))
1044 (define-key map [menu-bar subdir prev-dirline]
1045 '(menu-item "Prev Dirline" dired-prev-dirline
1046 :help "Move to next directory-file line"))
1047 (define-key map [menu-bar subdir next-dirline]
1048 '(menu-item "Next Dirline" dired-next-dirline
1049 :help "Move to previous directory-file line"))
1050 (define-key map [menu-bar subdir insert]
1051 '(menu-item "Insert This Subdir" dired-maybe-insert-subdir
1052 :help "Insert contents of subdirectory"))
1053
1054 (define-key map [menu-bar immediate]
1055 (cons "Immediate" (make-sparse-keymap "Immediate")))
1056
1057 (define-key map [menu-bar immediate revert-buffer]
1058 '(menu-item "Refresh" revert-buffer
1059 :help "Update contents of shown directories"))
1060
1061 (define-key map [menu-bar immediate dashes]
1062 '("--"))
1063
1064 (define-key map [menu-bar immediate backup-diff]
1065 '(menu-item "Compare with Backup" dired-backup-diff
1066 :help "Diff file at cursor with its latest backup"))
1067 (define-key map [menu-bar immediate diff]
1068 '(menu-item "Diff..." dired-diff
1069 :help "Compare file at cursor with another file"))
1070 (define-key map [menu-bar immediate view]
1071 '(menu-item "View This File" dired-view-file
1072 :help "Examine file at cursor in read-only mode"))
1073 (define-key map [menu-bar immediate display]
1074 '(menu-item "Display in Other Window" dired-display-file
1075 :help "Display file at cursor in other window"))
1076 (define-key map [menu-bar immediate find-file-other-window]
1077 '(menu-item "Find in Other Window" dired-find-file-other-window
1078 :help "Edit file at cursor in other window"))
1079 (define-key map [menu-bar immediate find-file]
1080 '(menu-item "Find This File" dired-find-file
1081 :help "Edit file at cursor"))
1082 (define-key map [menu-bar immediate create-directory]
1083 '(menu-item "Create Directory..." dired-create-directory))
1084
1085 (define-key map [menu-bar regexp]
1086 (cons "Regexp" (make-sparse-keymap "Regexp")))
1087
1088 (define-key map [menu-bar regexp downcase]
1089 '(menu-item "Downcase" dired-downcase
1090 ;; When running on plain MS-DOS, there's only one
1091 ;; letter-case for file names.
1092 :enable (or (not (fboundp 'msdos-long-file-names))
1093 (msdos-long-file-names))
1094 :help "Rename marked files to lower-case name"))
1095 (define-key map [menu-bar regexp upcase]
1096 '(menu-item "Upcase" dired-upcase
1097 :enable (or (not (fboundp 'msdos-long-file-names))
1098 (msdos-long-file-names))
1099 :help "Rename marked files to upper-case name"))
1100 (define-key map [menu-bar regexp hardlink]
1101 '(menu-item "Hardlink..." dired-do-hardlink-regexp
1102 :help "Make hard links for files matching regexp"))
1103 (define-key map [menu-bar regexp symlink]
1104 '(menu-item "Symlink..." dired-do-symlink-regexp
1105 :visible (fboundp 'make-symbolic-link)
1106 :help "Make symbolic links for files matching regexp"))
1107 (define-key map [menu-bar regexp rename]
1108 '(menu-item "Rename..." dired-do-rename-regexp
1109 :help "Rename marked files matching regexp"))
1110 (define-key map [menu-bar regexp copy]
1111 '(menu-item "Copy..." dired-do-copy-regexp
1112 :help "Copy marked files matching regexp"))
1113 (define-key map [menu-bar regexp flag]
1114 '(menu-item "Flag..." dired-flag-files-regexp
1115 :help "Flag files matching regexp for deletion"))
1116 (define-key map [menu-bar regexp mark]
1117 '(menu-item "Mark..." dired-mark-files-regexp
1118 :help "Mark files matching regexp for future operations"))
1119 (define-key map [menu-bar regexp mark-cont]
1120 '(menu-item "Mark Containing..." dired-mark-files-containing-regexp
1121 :help "Mark files whose contents matches regexp"))
1122
1123 (define-key map [menu-bar mark]
1124 (cons "Mark" (make-sparse-keymap "Mark")))
1125
1126 (define-key map [menu-bar mark prev]
1127 '(menu-item "Previous Marked" dired-prev-marked-file
1128 :help "Move to previous marked file"))
1129 (define-key map [menu-bar mark next]
1130 '(menu-item "Next Marked" dired-next-marked-file
1131 :help "Move to next marked file"))
1132 (define-key map [menu-bar mark marks]
1133 '(menu-item "Change Marks..." dired-change-marks
1134 :help "Replace marker with another character"))
1135 (define-key map [menu-bar mark unmark-all]
1136 '(menu-item "Unmark All" dired-unmark-all-marks))
1137 (define-key map [menu-bar mark symlinks]
1138 '(menu-item "Mark Symlinks" dired-mark-symlinks
1139 :visible (fboundp 'make-symbolic-link)
1140 :help "Mark all symbolic links"))
1141 (define-key map [menu-bar mark directories]
1142 '(menu-item "Mark Directories" dired-mark-directories
1143 :help "Mark all directories except `.' and `..'"))
1144 (define-key map [menu-bar mark directory]
1145 '(menu-item "Mark Old Backups" dired-clean-directory
1146 :help "Flag old numbered backups for deletion"))
1147 (define-key map [menu-bar mark executables]
1148 '(menu-item "Mark Executables" dired-mark-executables
1149 :help "Mark all executable files"))
1150 (define-key map [menu-bar mark garbage-files]
1151 '(menu-item "Flag Garbage Files" dired-flag-garbage-files
1152 :help "Flag unneeded files for deletion"))
1153 (define-key map [menu-bar mark backup-files]
1154 '(menu-item "Flag Backup Files" dired-flag-backup-files
1155 :help "Flag all backup files for deletion"))
1156 (define-key map [menu-bar mark auto-save-files]
1157 '(menu-item "Flag Auto-save Files" dired-flag-auto-save-files
1158 :help "Flag auto-save files for deletion"))
1159 (define-key map [menu-bar mark deletion]
1160 '(menu-item "Flag" dired-flag-file-deletion
1161 :help "Flag current line's file for deletion"))
1162 (define-key map [menu-bar mark unmark]
1163 '(menu-item "Unmark" dired-unmark
1164 :help "Unmark or unflag current line's file"))
1165 (define-key map [menu-bar mark mark]
1166 '(menu-item "Mark" dired-mark
1167 :help "Mark current line's file for future operations"))
1168 (define-key map [menu-bar mark toggle-marks]
1169 '(menu-item "Toggle Marks" dired-toggle-marks
1170 :help "Mark unmarked files, unmark marked ones"))
1171
1172 (define-key map [menu-bar operate]
1173 (cons "Operate" (make-sparse-keymap "Operate")))
1174
1175 (define-key map [menu-bar operate query-replace]
1176 '(menu-item "Query Replace in Files..." dired-do-query-replace-regexp
1177 :help "Replace regexp in marked files"))
1178 (define-key map [menu-bar operate search]
1179 '(menu-item "Search Files..." dired-do-search
1180 :help "Search marked files for regexp"))
1181 (define-key map [menu-bar operate chown]
1182 '(menu-item "Change Owner..." dired-do-chown
1183 :visible (not (memq system-type '(ms-dos windows-nt)))
1184 :help "Change the owner of marked files"))
1185 (define-key map [menu-bar operate chgrp]
1186 '(menu-item "Change Group..." dired-do-chgrp
1187 :visible (not (memq system-type '(ms-dos windows-nt)))
1188 :help "Change the group of marked files"))
1189 (define-key map [menu-bar operate chmod]
1190 '(menu-item "Change Mode..." dired-do-chmod
1191 :help "Change mode (attributes) of marked files"))
1192 (define-key map [menu-bar operate load]
1193 '(menu-item "Load" dired-do-load
1194 :help "Load marked Emacs Lisp files"))
1195 (define-key map [menu-bar operate compile]
1196 '(menu-item "Byte-compile" dired-do-byte-compile
1197 :help "Byte-compile marked Emacs Lisp files"))
1198 (define-key map [menu-bar operate compress]
1199 '(menu-item "Compress" dired-do-compress
1200 :help "Compress/uncompress marked files"))
1201 (define-key map [menu-bar operate print]
1202 '(menu-item "Print..." dired-do-print
1203 :help "Ask for print command and print marked files"))
1204 (define-key map [menu-bar operate hardlink]
1205 '(menu-item "Hardlink to..." dired-do-hardlink
1206 :help "Make hard links for current or marked files"))
1207 (define-key map [menu-bar operate symlink]
1208 '(menu-item "Symlink to..." dired-do-symlink
1209 :visible (fboundp 'make-symbolic-link)
1210 :help "Make symbolic links for current or marked files"))
1211 (define-key map [menu-bar operate command]
1212 '(menu-item "Shell Command..." dired-do-shell-command
1213 :help "Run a shell command on each of marked files"))
1214 (define-key map [menu-bar operate delete]
1215 '(menu-item "Delete" dired-do-delete
1216 :help "Delete current file or all marked files"))
1217 (define-key map [menu-bar operate rename]
1218 '(menu-item "Rename to..." dired-do-rename
1219 :help "Rename current file or move marked files"))
1220 (define-key map [menu-bar operate copy]
1221 '(menu-item "Copy to..." dired-do-copy
1222 :help "Copy current file or all marked files"))
1223
1224 map)
1225 "Local keymap for `dired-mode' buffers.")
1226 \f
1227 ;; Dired mode is suitable only for specially formatted data.
1228 (put 'dired-mode 'mode-class 'special)
1229
1230 (defvar buffer-stale-function)
1231
1232 (defun dired-mode (&optional dirname switches)
1233 "\
1234 Mode for \"editing\" directory listings.
1235 In Dired, you are \"editing\" a list of the files in a directory and
1236 \(optionally) its subdirectories, in the format of `ls -lR'.
1237 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
1238 \"Editing\" means that you can run shell commands on files, visit,
1239 compress, load or byte-compile them, change their file attributes
1240 and insert subdirectories into the same buffer. You can \"mark\"
1241 files for later commands or \"flag\" them for deletion, either file
1242 by file or all files matching certain criteria.
1243 You can move using the usual cursor motion commands.\\<dired-mode-map>
1244 Letters no longer insert themselves. Digits are prefix arguments.
1245 Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
1246 Type \\[dired-mark] to Mark a file or subdirectory for later commands.
1247 Most commands operate on the marked files and use the current file
1248 if no files are marked. Use a numeric prefix argument to operate on
1249 the next ARG (or previous -ARG if ARG<0) files, or just `1'
1250 to operate on the current file only. Prefix arguments override marks.
1251 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
1252 to see why something went wrong.
1253 Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
1254 Type \\[dired-unmark-backward] to back up one line and unflag.
1255 Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
1256 Type \\[dired-advertised-find-file] to Find the current line's file
1257 (or dired it in another buffer, if it is a directory).
1258 Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
1259 Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
1260 Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
1261 Type \\[dired-do-copy] to Copy files.
1262 Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
1263 Type \\[revert-buffer] to read all currently expanded directories again.
1264 This retains all marks and hides subdirs again that were hidden before.
1265 SPC and DEL can be used to move down and up by lines.
1266
1267 If dired ever gets confused, you can either type \\[revert-buffer] \
1268 to read the
1269 directories again, type \\[dired-do-redisplay] \
1270 to relist a single or the marked files or a
1271 subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
1272 again for the directory tree.
1273
1274 Customization variables (rename this buffer and type \\[describe-variable] on each line
1275 for more info):
1276
1277 dired-listing-switches
1278 dired-trivial-filenames
1279 dired-shrink-to-fit
1280 dired-marker-char
1281 dired-del-marker
1282 dired-keep-marker-rename
1283 dired-keep-marker-copy
1284 dired-keep-marker-hardlink
1285 dired-keep-marker-symlink
1286
1287 Hooks (use \\[describe-variable] to see their documentation):
1288
1289 dired-before-readin-hook
1290 dired-after-readin-hook
1291 dired-mode-hook
1292 dired-load-hook
1293
1294 Keybindings:
1295 \\{dired-mode-map}"
1296 ;; Not to be called interactively (e.g. dired-directory will be set
1297 ;; to default-directory, which is wrong with wildcards).
1298 (kill-all-local-variables)
1299 (use-local-map dired-mode-map)
1300 (dired-advertise) ; default-directory is already set
1301 (setq major-mode 'dired-mode
1302 mode-name "Dired"
1303 ;; case-fold-search nil
1304 buffer-read-only t
1305 selective-display t ; for subdirectory hiding
1306 mode-line-buffer-identification
1307 (propertized-buffer-identification "%17b"))
1308 (set (make-local-variable 'revert-buffer-function)
1309 (function dired-revert))
1310 (set (make-local-variable 'buffer-stale-function)
1311 (function dired-buffer-stale-p))
1312 (set (make-local-variable 'page-delimiter)
1313 "\n\n")
1314 (set (make-local-variable 'dired-directory)
1315 (or dirname default-directory))
1316 ;; list-buffers uses this to display the dir being edited in this buffer.
1317 (set (make-local-variable 'list-buffers-directory)
1318 (expand-file-name (if (listp dired-directory)
1319 (car dired-directory)
1320 dired-directory)))
1321 (set (make-local-variable 'dired-actual-switches)
1322 (or switches dired-listing-switches))
1323 (set (make-local-variable 'font-lock-defaults)
1324 '(dired-font-lock-keywords t nil nil beginning-of-line))
1325 (dired-sort-other dired-actual-switches t)
1326 (run-hooks 'dired-mode-hook)
1327 (when (featurep 'x-dnd)
1328 (make-variable-buffer-local 'x-dnd-test-function)
1329 (make-variable-buffer-local 'x-dnd-protocol-alist)
1330 (setq x-dnd-test-function 'dired-dnd-test-function)
1331 (setq x-dnd-protocol-alist
1332 (append '(("^file:///" . dired-dnd-handle-local-file)
1333 ("^file://" . dired-dnd-handle-file)
1334 ("^file:" . dired-dnd-handle-local-file))
1335 x-dnd-protocol-alist))))
1336 \f
1337 ;; Idiosyncratic dired commands that don't deal with marks.
1338
1339 (defun dired-summary ()
1340 "Summarize basic Dired commands and show recent Dired errors."
1341 (interactive)
1342 (dired-why)
1343 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
1344 (message
1345 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
1346
1347 (defun dired-undo ()
1348 "Undo in a dired buffer.
1349 This doesn't recover lost files, it just undoes changes in the buffer itself.
1350 You can use it to recover marks, killed lines or subdirs.
1351 In the latter case, you have to do \\[dired-build-subdir-alist] to
1352 parse the buffer again."
1353 (interactive)
1354 (let (buffer-read-only)
1355 (undo)
1356 (message "Change in Dired buffer undone.
1357 Actual changes in files cannot be undone by Emacs.")))
1358
1359 (defun dired-next-line (arg)
1360 "Move down lines then position at filename.
1361 Optional prefix ARG says how many lines to move; default is one line."
1362 (interactive "p")
1363 (next-line arg)
1364 (dired-move-to-filename))
1365
1366 (defun dired-previous-line (arg)
1367 "Move up lines then position at filename.
1368 Optional prefix ARG says how many lines to move; default is one line."
1369 (interactive "p")
1370 (previous-line arg)
1371 (dired-move-to-filename))
1372
1373 (defun dired-next-dirline (arg &optional opoint)
1374 "Goto ARG'th next directory file line."
1375 (interactive "p")
1376 (or opoint (setq opoint (point)))
1377 (if (if (> arg 0)
1378 (re-search-forward dired-re-dir nil t arg)
1379 (beginning-of-line)
1380 (re-search-backward dired-re-dir nil t (- arg)))
1381 (dired-move-to-filename) ; user may type `i' or `f'
1382 (goto-char opoint)
1383 (error "No more subdirectories")))
1384
1385 (defun dired-prev-dirline (arg)
1386 "Goto ARG'th previous directory file line."
1387 (interactive "p")
1388 (dired-next-dirline (- arg)))
1389
1390 (defun dired-up-directory (&optional other-window)
1391 "Run Dired on parent directory of current directory.
1392 Find the parent directory either in this buffer or another buffer.
1393 Creates a buffer if necessary."
1394 (interactive "P")
1395 (let* ((dir (dired-current-directory))
1396 (up (file-name-directory (directory-file-name dir))))
1397 (or (dired-goto-file (directory-file-name dir))
1398 ;; Only try dired-goto-subdir if buffer has more than one dir.
1399 (and (cdr dired-subdir-alist)
1400 (dired-goto-subdir up))
1401 (progn
1402 (if other-window
1403 (dired-other-window up)
1404 (dired up))
1405 (dired-goto-file dir)))))
1406
1407 (defun dired-get-file-for-visit ()
1408 "Get the current line's file name, with an error if file does not exist."
1409 (interactive)
1410 ;; We pass t for second arg so that we don't get error for `.' and `..'.
1411 (let ((raw (dired-get-filename nil t))
1412 file-name)
1413 (if (null raw)
1414 (error "No file on this line"))
1415 (setq file-name (file-name-sans-versions raw t))
1416 (if (file-exists-p file-name)
1417 file-name
1418 (if (file-symlink-p file-name)
1419 (error "File is a symlink to a nonexistent target")
1420 (error "File no longer exists; type `g' to update Dired buffer")))))
1421
1422 ;; Force `f' rather than `e' in the mode doc:
1423 (defalias 'dired-advertised-find-file 'dired-find-file)
1424 (defun dired-find-file ()
1425 "In Dired, visit the file or directory named on this line."
1426 (interactive)
1427 ;; Bind `find-file-run-dired' so that the command works on directories
1428 ;; too, independent of the user's setting.
1429 (let ((find-file-run-dired t))
1430 (find-file (dired-get-file-for-visit))))
1431
1432 (defun dired-find-alternate-file ()
1433 "In Dired, visit this file or directory instead of the dired buffer."
1434 (interactive)
1435 (set-buffer-modified-p nil)
1436 (find-alternate-file (dired-get-file-for-visit)))
1437 ;; Don't override the setting from .emacs.
1438 ;;;###autoload (put 'dired-find-alternate-file 'disabled t)
1439
1440 (defun dired-mouse-find-file-other-window (event)
1441 "In Dired, visit the file or directory name you click on."
1442 (interactive "e")
1443 (let (window pos file)
1444 (save-excursion
1445 (setq window (posn-window (event-end event))
1446 pos (posn-point (event-end event)))
1447 (if (not (windowp window))
1448 (error "No file chosen"))
1449 (set-buffer (window-buffer window))
1450 (goto-char pos)
1451 (setq file (dired-get-file-for-visit)))
1452 (if (file-directory-p file)
1453 (or (and (cdr dired-subdir-alist)
1454 (dired-goto-subdir file))
1455 (progn
1456 (select-window window)
1457 (dired-other-window file)))
1458 (let (cmd)
1459 ;; Look for some other way to view a certain file.
1460 (dolist (elt dired-view-command-alist)
1461 (if (string-match (car elt) file)
1462 (setq cmd (cdr elt))))
1463 (if cmd
1464 (call-process shell-file-name nil 0 nil
1465 "-c"
1466 (concat (format cmd (shell-quote-argument file))
1467 " &"))
1468 (select-window window)
1469 (find-file-other-window (file-name-sans-versions file t)))))))
1470
1471 (defun dired-view-file ()
1472 "In Dired, examine a file in view mode, returning to dired when done.
1473 When file is a directory, show it in this buffer if it is inserted.
1474 Some kinds of files are displayed using external viewer programs;
1475 see `dired-view-command-alist'. Otherwise, display it in another buffer."
1476 (interactive)
1477 (let ((file (dired-get-file-for-visit)))
1478 (if (file-directory-p file)
1479 (or (and (cdr dired-subdir-alist)
1480 (dired-goto-subdir file))
1481 (dired file))
1482 (let (cmd)
1483 ;; Look for some other way to view a certain file.
1484 (dolist (elt dired-view-command-alist)
1485 (if (string-match (car elt) file)
1486 (setq cmd (cdr elt))))
1487 (if cmd
1488 (call-process shell-file-name nil 0 nil
1489 "-c"
1490 (concat (format cmd (shell-quote-argument file))
1491 " &"))
1492 (view-file file))))))
1493
1494 (defun dired-find-file-other-window ()
1495 "In Dired, visit this file or directory in another window."
1496 (interactive)
1497 (find-file-other-window (dired-get-file-for-visit)))
1498
1499 (defun dired-display-file ()
1500 "In Dired, display this file or directory in another window."
1501 (interactive)
1502 (display-buffer (find-file-noselect (dired-get-file-for-visit))))
1503 \f
1504 ;;; Functions for extracting and manipulating file names in Dired buffers.
1505
1506 (defun dired-get-filename (&optional localp no-error-if-not-filep)
1507 "In Dired, return name of file mentioned on this line.
1508 Value returned normally includes the directory name.
1509 Optional arg LOCALP with value `no-dir' means don't include directory
1510 name in result. A value of `verbatim' means to return the name exactly as
1511 it occurs in the buffer, and a value of t means construct name relative to
1512 `default-directory', which still may contain slashes if in a subdirectory.
1513 Optional arg NO-ERROR-IF-NOT-FILEP means treat `.' and `..' as
1514 regular filenames and return nil if no filename on this line.
1515 Otherwise, an error occurs in these cases."
1516 (let (case-fold-search file p1 p2 already-absolute)
1517 (save-excursion
1518 (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
1519 (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
1520 ;; nil if no file on this line, but no-error-if-not-filep is t:
1521 (if (setq file (and p1 p2 (buffer-substring p1 p2)))
1522 (progn
1523 ;; Get rid of the mouse-face property that file names have.
1524 (set-text-properties 0 (length file) nil file)
1525 ;; Unquote names quoted by ls or by dired-insert-directory.
1526 ;; Using read to unquote is much faster than substituting
1527 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
1528 (setq file
1529 (read
1530 (concat "\""
1531 ;; Some ls -b don't escape quotes, argh!
1532 ;; This is not needed for GNU ls, though.
1533 (or (dired-string-replace-match
1534 "\\([^\\]\\|\\`\\)\"" file "\\1\\\\\"" nil t)
1535 file)
1536 "\"")))
1537 ;; The above `read' will return a unibyte string if FILE
1538 ;; contains eight-bit-control/graphic characters.
1539 (if (and enable-multibyte-characters
1540 (not (multibyte-string-p file)))
1541 (setq file (string-to-multibyte file)))))
1542 (and file (file-name-absolute-p file)
1543 ;; A relative file name can start with ~.
1544 ;; Don't treat it as absolute in this context.
1545 (not (eq (aref file 0) ?~))
1546 (setq already-absolute t))
1547 (cond
1548 ((null file)
1549 nil)
1550 ((eq localp 'verbatim)
1551 file)
1552 ((and (not no-error-if-not-filep)
1553 (save-excursion
1554 (beginning-of-line)
1555 (looking-at dired-re-dot)))
1556 (error "Cannot operate on `.' or `..'"))
1557 ((and (eq localp 'no-dir) already-absolute)
1558 (file-name-nondirectory file))
1559 (already-absolute
1560 (let ((handler (find-file-name-handler file nil)))
1561 ;; check for safe-magic property so that we won't
1562 ;; put /: for names that don't really need them.
1563 ;; For instance, .gz files when auto-compression-mode is on.
1564 (if (and handler (not (get handler 'safe-magic)))
1565 (concat "/:" file)
1566 file)))
1567 ((eq localp 'no-dir)
1568 file)
1569 ((equal (dired-current-directory) "/")
1570 (setq file (concat (dired-current-directory localp) file))
1571 (let ((handler (find-file-name-handler file nil)))
1572 ;; check for safe-magic property so that we won't
1573 ;; put /: for names that don't really need them.
1574 ;; For instance, .gz files when auto-compression-mode is on.
1575 (if (and handler (not (get handler 'safe-magic)))
1576 (concat "/:" file)
1577 file)))
1578 (t
1579 (concat (dired-current-directory localp) file)))))
1580
1581 (defun dired-string-replace-match (regexp string newtext
1582 &optional literal global)
1583 "Replace first match of REGEXP in STRING with NEWTEXT.
1584 If it does not match, nil is returned instead of the new string.
1585 Optional arg LITERAL means to take NEWTEXT literally.
1586 Optional arg GLOBAL means to replace all matches."
1587 (if global
1588 (let ((start 0) ret)
1589 (while (string-match regexp string start)
1590 (let ((from-end (- (length string) (match-end 0))))
1591 (setq ret (setq string (replace-match newtext t literal string)))
1592 (setq start (- (length string) from-end))))
1593 ret)
1594 (if (not (string-match regexp string 0))
1595 nil
1596 (replace-match newtext t literal string))))
1597
1598 (defun dired-make-absolute (file &optional dir)
1599 ;;"Convert FILE (a file name relative to DIR) to an absolute file name."
1600 ;; We can't always use expand-file-name as this would get rid of `.'
1601 ;; or expand in / instead default-directory if DIR=="".
1602 ;; This should be good enough for ange-ftp, but might easily be
1603 ;; redefined (for VMS?).
1604 ;; It should be reasonably fast, though, as it is called in
1605 ;; dired-get-filename.
1606 (concat (or dir default-directory) file))
1607
1608 (defun dired-make-relative (file &optional dir ignore)
1609 "Convert FILE (an absolute file name) to a name relative to DIR.
1610 If this is impossible, return FILE unchanged.
1611 DIR must be a directory name, not a file name."
1612 (or dir (setq dir default-directory))
1613 ;; This case comes into play if default-directory is set to
1614 ;; use ~.
1615 (if (and (> (length dir) 0) (= (aref dir 0) ?~))
1616 (setq dir (expand-file-name dir)))
1617 (if (string-match (concat "^" (regexp-quote dir)) file)
1618 (substring file (match-end 0))
1619 ;;; (or no-error
1620 ;;; (error "%s: not in directory tree growing at %s" file dir))
1621 file))
1622 \f
1623 ;;; Functions for finding the file name in a dired buffer line.
1624
1625 (defvar dired-move-to-filename-regexp
1626 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
1627 (l-or-quote "\\([A-Za-z']\\|[^\0-\177]\\)")
1628 ;; In some locales, month abbreviations are as short as 2 letters,
1629 ;; and they can be followed by ".".
1630 ;; In Breton, a month name can include a quote character.
1631 (month (concat l-or-quote l-or-quote "+\\.?"))
1632 (s " ")
1633 (yyyy "[0-9][0-9][0-9][0-9]")
1634 (dd "[ 0-3][0-9]")
1635 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
1636 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
1637 (zone "[-+][0-2][0-9][0-5][0-9]")
1638 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
1639 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
1640 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
1641 "\\|" yyyy "-" iso-mm-dd "\\)"))
1642 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
1643 s "+"
1644 "\\(" HH:MM "\\|" yyyy "\\)"))
1645 (western-comma (concat month s "+" dd "," s "+" yyyy))
1646 ;; Japanese MS-Windows ls-lisp has one-digit months, and
1647 ;; omits the Kanji characters after month and day-of-month.
1648 (mm "[ 0-1]?[0-9]")
1649 (japanese
1650 (concat mm l "?" s dd l "?" s "+"
1651 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
1652 ;; The "[0-9]" below requires the previous column to end in a digit.
1653 ;; This avoids recognizing `1 may 1997' as a date in the line:
1654 ;; -r--r--r-- 1 may 1997 1168 Oct 19 16:49 README
1655 ;; The "[kKMGTPEZY]?" below supports "ls -alh" output.
1656 ;; The ".*" below finds the last match if there are multiple matches.
1657 ;; This avoids recognizing `jservice 10 1024' as a date in the line:
1658 ;; drwxr-xr-x 3 jservice 10 1024 Jul 2 1997 esg-host
1659 (concat ".*[0-9][kKMGTPEZY]?" s
1660 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
1661 s "+"))
1662 "Regular expression to match up to the file name in a directory listing.
1663 The default value is designed to recognize dates and times
1664 regardless of the language.")
1665
1666 (defvar dired-permission-flags-regexp
1667 "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
1668 "Regular expression to match the permission flags in `ls -l'.")
1669
1670 ;; Move to first char of filename on this line.
1671 ;; Returns position (point) or nil if no filename on this line."
1672 (defun dired-move-to-filename (&optional raise-error eol)
1673 ;; This is the UNIX version.
1674 (or eol (setq eol (line-end-position)))
1675 (beginning-of-line)
1676 ;; First try assuming `ls --dired' was used.
1677 (let ((change (next-single-property-change (point) 'dired-filename nil eol)))
1678 (cond
1679 ((and change (< change eol))
1680 (goto-char change))
1681 ((re-search-forward dired-move-to-filename-regexp eol t)
1682 (goto-char (match-end 0)))
1683 ((re-search-forward dired-permission-flags-regexp eol t)
1684 ;; Ha! There *is* a file. Our regexp-from-hell just failed to find it.
1685 (funcall (if raise-error 'error 'message)
1686 "Unrecognized line! Check dired-move-to-filename-regexp"))
1687 (raise-error
1688 (error "No file on this line")))))
1689
1690 (defun dired-move-to-end-of-filename (&optional no-error)
1691 ;; Assumes point is at beginning of filename,
1692 ;; thus the rwx bit re-search-backward below will succeed in *this*
1693 ;; line if at all. So, it should be called only after
1694 ;; (dired-move-to-filename t).
1695 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
1696 ;; This is the UNIX version.
1697 (if (get-text-property (point) 'dired-filename)
1698 (goto-char (next-single-property-change (point) 'dired-filename))
1699 (let (opoint file-type executable symlink hidden case-fold-search used-F eol)
1700 ;; case-fold-search is nil now, so we can test for capital F:
1701 (setq used-F (string-match "F" dired-actual-switches)
1702 opoint (point)
1703 eol (save-excursion (end-of-line) (point))
1704 hidden (and selective-display
1705 (save-excursion (search-forward "\r" eol t))))
1706 (if hidden
1707 nil
1708 (save-excursion ;; Find out what kind of file this is:
1709 ;; Restrict perm bits to be non-blank,
1710 ;; otherwise this matches one char to early (looking backward):
1711 ;; "l---------" (some systems make symlinks that way)
1712 ;; "----------" (plain file with zero perms)
1713 (if (re-search-backward
1714 dired-permission-flags-regexp nil t)
1715 (setq file-type (char-after (match-beginning 1))
1716 symlink (eq file-type ?l)
1717 ;; Only with -F we need to know whether it's an executable
1718 executable (and
1719 used-F
1720 (string-match
1721 "[xst]" ;; execute bit set anywhere?
1722 (concat
1723 (buffer-substring (match-beginning 2)
1724 (match-end 2))
1725 (buffer-substring (match-beginning 3)
1726 (match-end 3))
1727 (buffer-substring (match-beginning 4)
1728 (match-end 4))))))
1729 (or no-error (error "No file on this line"))))
1730 ;; Move point to end of name:
1731 (if symlink
1732 (if (search-forward " ->" eol t)
1733 (progn
1734 (forward-char -3)
1735 (and used-F
1736 dired-ls-F-marks-symlinks
1737 (eq (preceding-char) ?@) ;; did ls really mark the link?
1738 (forward-char -1))))
1739 (goto-char eol) ;; else not a symbolic link
1740 ;; ls -lF marks dirs, sockets and executables with exactly one
1741 ;; trailing character. (Executable bits on symlinks ain't mean
1742 ;; a thing, even to ls, but we know it's not a symlink.)
1743 (and used-F
1744 (or (memq file-type '(?d ?s))
1745 executable)
1746 (forward-char -1))))
1747 (or no-error
1748 (not (eq opoint (point)))
1749 (error (if hidden
1750 (substitute-command-keys
1751 "File line is hidden, type \\[dired-hide-subdir] to unhide")
1752 "No file on this line")))
1753 (if (eq opoint (point))
1754 nil
1755 (point)))))
1756
1757 \f
1758 ;;; COPY NAMES OF MARKED FILES INTO KILL-RING.
1759
1760 (defun dired-copy-filename-as-kill (&optional arg)
1761 "Copy names of marked (or next ARG) files into the kill ring.
1762 The names are separated by a space.
1763 With a zero prefix arg, use the absolute file name of each marked file.
1764 With \\[universal-argument], use the file name sans directory of each marked file.
1765
1766 If on a subdir headerline, use subdirname instead; prefix arg is ignored
1767 in this case.
1768
1769 You can then feed the file name(s) to other commands with \\[yank]."
1770 (interactive "P")
1771 (let ((string
1772 (or (dired-get-subdir)
1773 (mapconcat (function identity)
1774 (if arg
1775 (cond ((zerop (prefix-numeric-value arg))
1776 (dired-get-marked-files))
1777 ((integerp arg)
1778 (dired-get-marked-files 'no-dir arg))
1779 (t ; else a raw arg
1780 (dired-get-marked-files t)))
1781 (dired-get-marked-files 'no-dir))
1782 " "))))
1783 (if (eq last-command 'kill-region)
1784 (kill-append string nil)
1785 (kill-new string))
1786 (message "%s" string)))
1787
1788 \f
1789 ;; Keeping Dired buffers in sync with the filesystem and with each other
1790
1791 (defun dired-buffers-for-dir (dir &optional file)
1792 ;; Return a list of buffers that dired DIR (top level or in-situ subdir).
1793 ;; If FILE is non-nil, include only those whose wildcard pattern (if any)
1794 ;; matches FILE.
1795 ;; The list is in reverse order of buffer creation, most recent last.
1796 ;; As a side effect, killed dired buffers for DIR are removed from
1797 ;; dired-buffers.
1798 (setq dir (file-name-as-directory dir))
1799 (let ((alist dired-buffers) result elt buf pattern)
1800 (while alist
1801 (setq elt (car alist)
1802 buf (cdr elt))
1803 (if (buffer-name buf)
1804 (if (dired-in-this-tree dir (car elt))
1805 (with-current-buffer buf
1806 (and (assoc dir dired-subdir-alist)
1807 (or (null file)
1808 (let ((wildcards
1809 (file-name-nondirectory dired-directory)))
1810 (or (= 0 (length wildcards))
1811 (string-match (dired-glob-regexp wildcards)
1812 file))))
1813 (setq result (cons buf result)))))
1814 ;; else buffer is killed - clean up:
1815 (setq dired-buffers (delq elt dired-buffers)))
1816 (setq alist (cdr alist)))
1817 result))
1818
1819 (defun dired-glob-regexp (pattern)
1820 "Convert glob-pattern PATTERN to a regular expression."
1821 (let ((matched-in-pattern 0) ;; How many chars of PATTERN we've handled.
1822 regexp)
1823 (while (string-match "[[?*]" pattern matched-in-pattern)
1824 (let ((op-end (match-end 0))
1825 (next-op (aref pattern (match-beginning 0))))
1826 (setq regexp (concat regexp
1827 (regexp-quote
1828 (substring pattern matched-in-pattern
1829 (match-beginning 0)))))
1830 (cond ((= next-op ??)
1831 (setq regexp (concat regexp "."))
1832 (setq matched-in-pattern op-end))
1833 ((= next-op ?\[)
1834 ;; Fails to handle ^ yet ????
1835 (let* ((set-start (match-beginning 0))
1836 (set-cont
1837 (if (= (aref pattern (1+ set-start)) ?^)
1838 (+ 3 set-start)
1839 (+ 2 set-start)))
1840 (set-end (string-match "]" pattern set-cont))
1841 (set (substring pattern set-start (1+ set-end))))
1842 (setq regexp (concat regexp set))
1843 (setq matched-in-pattern (1+ set-end))))
1844 ((= next-op ?*)
1845 (setq regexp (concat regexp ".*"))
1846 (setq matched-in-pattern op-end)))))
1847 (concat "\\`"
1848 regexp
1849 (regexp-quote
1850 (substring pattern matched-in-pattern))
1851 "\\'")))
1852
1853
1854
1855 (defun dired-advertise ()
1856 ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
1857 ;; With wildcards we actually advertise too much.
1858 (let ((expanded-default (expand-file-name default-directory)))
1859 (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
1860 t ; we have already advertised ourselves
1861 (setq dired-buffers
1862 (cons (cons expanded-default (current-buffer))
1863 dired-buffers)))))
1864
1865 (defun dired-unadvertise (dir)
1866 ;; Remove DIR from the buffer alist in variable dired-buffers.
1867 ;; This has the effect of removing any buffer whose main directory is DIR.
1868 ;; It does not affect buffers in which DIR is a subdir.
1869 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
1870 (setq dired-buffers
1871 (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
1872 \f
1873 ;; Tree Dired
1874
1875 ;;; utility functions
1876
1877 (defun dired-in-this-tree (file dir)
1878 ;;"Is FILE part of the directory tree starting at DIR?"
1879 (let (case-fold-search)
1880 (string-match (concat "^" (regexp-quote dir)) file)))
1881
1882 (defun dired-normalize-subdir (dir)
1883 ;; Prepend default-directory to DIR if relative file name.
1884 ;; dired-get-filename must be able to make a valid file name from a
1885 ;; file and its directory DIR.
1886 (file-name-as-directory
1887 (if (file-name-absolute-p dir)
1888 dir
1889 (expand-file-name dir default-directory))))
1890
1891 (defun dired-get-subdir ()
1892 ;;"Return the subdir name on this line, or nil if not on a headerline."
1893 ;; Look up in the alist whether this is a headerline.
1894 (save-excursion
1895 (let ((cur-dir (dired-current-directory)))
1896 (beginning-of-line) ; alist stores b-o-l positions
1897 (and (zerop (- (point)
1898 (dired-get-subdir-min (assoc cur-dir
1899 dired-subdir-alist))))
1900 cur-dir))))
1901
1902 ;(defun dired-get-subdir-min (elt)
1903 ; (cdr elt))
1904 ;; can't use macro, must be redefinable for other alist format in dired-nstd.
1905 (defalias 'dired-get-subdir-min 'cdr)
1906
1907 (defun dired-get-subdir-max (elt)
1908 (save-excursion
1909 (goto-char (dired-get-subdir-min elt))
1910 (dired-subdir-max)))
1911
1912 (defun dired-clear-alist ()
1913 (while dired-subdir-alist
1914 (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
1915 (setq dired-subdir-alist (cdr dired-subdir-alist))))
1916
1917 (defun dired-subdir-index (dir)
1918 ;; Return an index into alist for use with nth
1919 ;; for the sake of subdir moving commands.
1920 (let (found (index 0) (alist dired-subdir-alist))
1921 (while alist
1922 (if (string= dir (car (car alist)))
1923 (setq alist nil found t)
1924 (setq alist (cdr alist) index (1+ index))))
1925 (if found index nil)))
1926
1927 (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
1928 "Go to next subdirectory, regardless of level."
1929 ;; Use 0 arg to go to this directory's header line.
1930 ;; NO-SKIP prevents moving to end of header line, returning whatever
1931 ;; position was found in dired-subdir-alist.
1932 (interactive "p")
1933 (let ((this-dir (dired-current-directory))
1934 pos index)
1935 ;; nth with negative arg does not return nil but the first element
1936 (setq index (- (dired-subdir-index this-dir) arg))
1937 (setq pos (if (>= index 0)
1938 (dired-get-subdir-min (nth index dired-subdir-alist))))
1939 (if pos
1940 (progn
1941 (goto-char pos)
1942 (or no-skip (skip-chars-forward "^\n\r"))
1943 (point))
1944 (if no-error-if-not-found
1945 nil ; return nil if not found
1946 (error "%s directory" (if (> arg 0) "Last" "First"))))))
1947
1948 (defun dired-build-subdir-alist (&optional switches)
1949 "Build `dired-subdir-alist' by parsing the buffer.
1950 Returns the new value of the alist.
1951 If optional arg SWITCHES is non-nil, use its value
1952 instead of `dired-actual-switches'."
1953 (interactive)
1954 (dired-clear-alist)
1955 (save-excursion
1956 (let* ((count 0)
1957 (buffer-read-only nil)
1958 (switches (or switches dired-actual-switches))
1959 new-dir-name
1960 (R-ftp-base-dir-regex
1961 ;; Used to expand subdirectory names correctly in recursive
1962 ;; ange-ftp listings.
1963 (and (string-match "R" switches)
1964 (string-match "\\`/.*:\\(/.*\\)" default-directory)
1965 (concat "\\`" (match-string 1 default-directory)))))
1966 (goto-char (point-min))
1967 (setq dired-subdir-alist nil)
1968 (while (and (re-search-forward dired-subdir-regexp nil t)
1969 ;; Avoid taking a file name ending in a colon
1970 ;; as a subdir name.
1971 (not (save-excursion
1972 (goto-char (match-beginning 0))
1973 (beginning-of-line)
1974 (forward-char 2)
1975 (save-match-data (looking-at dired-re-perms)))))
1976 (save-excursion
1977 (goto-char (match-beginning 1))
1978 (setq new-dir-name
1979 (buffer-substring-no-properties (point) (match-end 1))
1980 new-dir-name
1981 (save-match-data
1982 (if (and R-ftp-base-dir-regex
1983 (not (string= new-dir-name default-directory))
1984 (string-match R-ftp-base-dir-regex new-dir-name))
1985 (concat default-directory
1986 (substring new-dir-name (match-end 0)))
1987 (expand-file-name new-dir-name))))
1988 (delete-region (point) (match-end 1))
1989 (insert new-dir-name))
1990 (setq count (1+ count))
1991 (dired-alist-add-1 new-dir-name
1992 ;; Place a sub directory boundary between lines.
1993 (save-excursion
1994 (goto-char (match-beginning 0))
1995 (beginning-of-line)
1996 (point-marker))))
1997 (if (> count 1)
1998 (message "Buffer includes %d directories" count))
1999 ;; We don't need to sort it because it is in buffer order per
2000 ;; constructionem. Return new alist:
2001 dired-subdir-alist)))
2002
2003 (defun dired-alist-add-1 (dir new-marker)
2004 ;; Add new DIR at NEW-MARKER. Don't sort.
2005 (setq dired-subdir-alist
2006 (cons (cons (dired-normalize-subdir dir) new-marker)
2007 dired-subdir-alist)))
2008
2009 (defun dired-goto-next-nontrivial-file ()
2010 ;; Position point on first nontrivial file after point.
2011 (dired-goto-next-file);; so there is a file to compare with
2012 (if (stringp dired-trivial-filenames)
2013 (while (and (not (eobp))
2014 (string-match dired-trivial-filenames
2015 (file-name-nondirectory
2016 (or (dired-get-filename nil t) ""))))
2017 (forward-line 1)
2018 (dired-move-to-filename))))
2019
2020 (defun dired-goto-next-file ()
2021 (let ((max (1- (dired-subdir-max))))
2022 (while (and (not (dired-move-to-filename)) (< (point) max))
2023 (forward-line 1))))
2024
2025 (defun dired-goto-file (file)
2026 "Go to file line of FILE in this dired buffer."
2027 ;; Return value of point on success, else nil.
2028 ;; FILE must be an absolute file name.
2029 ;; Loses if FILE contains control chars like "\007" for which ls
2030 ;; either inserts "?" or "\\007" into the buffer, so we won't find
2031 ;; it in the buffer.
2032 (interactive
2033 (prog1 ; let push-mark display its message
2034 (list (expand-file-name
2035 (read-file-name "Goto file: "
2036 (dired-current-directory))))
2037 (push-mark)))
2038 (setq file (directory-file-name file)) ; does no harm if no directory
2039 (let (found case-fold-search dir)
2040 (setq dir (or (file-name-directory file)
2041 (error "File name `%s' is not absolute" file)))
2042 (save-excursion
2043 ;; The hair here is to get the result of dired-goto-subdir
2044 ;; without really calling it if we don't have any subdirs.
2045 (if (if (string= dir (expand-file-name default-directory))
2046 (goto-char (point-min))
2047 (and (cdr dired-subdir-alist)
2048 (dired-goto-subdir dir)))
2049 (let ((base (file-name-nondirectory file))
2050 search-string
2051 (boundary (dired-subdir-max)))
2052 (setq search-string
2053 (replace-regexp-in-string "\^m" "\\^m" base nil t))
2054 (setq search-string
2055 (replace-regexp-in-string "\\\\" "\\\\" search-string nil t))
2056 (while (and (not found)
2057 ;; filenames are preceded by SPC, this makes
2058 ;; the search faster (e.g. for the filename "-"!).
2059 (search-forward (concat " " search-string)
2060 boundary 'move))
2061 ;; Match could have BASE just as initial substring or
2062 ;; or in permission bits or date or
2063 ;; not be a proper filename at all:
2064 (if (equal base (dired-get-filename 'no-dir t))
2065 ;; Must move to filename since an (actually
2066 ;; correct) match could have been elsewhere on the
2067 ;; ;; line (e.g. "-" would match somewhere in the
2068 ;; permission bits).
2069 (setq found (dired-move-to-filename))
2070 ;; If this isn't the right line, move forward to avoid
2071 ;; trying this line again.
2072 (forward-line 1))))))
2073 (and found
2074 ;; return value of point (i.e., FOUND):
2075 (goto-char found))))
2076
2077 (defun dired-initial-position (dirname)
2078 ;; Where point should go in a new listing of DIRNAME.
2079 ;; Point assumed at beginning of new subdir line.
2080 ;; You may redefine this function as you wish, e.g. like in dired-x.el.
2081 (end-of-line)
2082 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
2083 \f
2084 ;; These are hooks which make tree dired work.
2085 ;; They are in this file because other parts of dired need to call them.
2086 ;; But they don't call the rest of tree dired unless there are subdirs loaded.
2087
2088 ;; This function is called for each retrieved filename.
2089 ;; It could stand to be faster, though it's mostly function call
2090 ;; overhead. Avoiding the function call seems to save about 10% in
2091 ;; dired-get-filename. Make it a defsubst?
2092 (defun dired-current-directory (&optional localp)
2093 "Return the name of the subdirectory to which this line belongs.
2094 This returns a string with trailing slash, like `default-directory'.
2095 Optional argument means return a file name relative to `default-directory'."
2096 (let ((here (point))
2097 (alist (or dired-subdir-alist
2098 ;; probably because called in a non-dired buffer
2099 (error "No subdir-alist in %s" (current-buffer))))
2100 elt dir)
2101 (while alist
2102 (setq elt (car alist)
2103 dir (car elt)
2104 ;; use `<=' (not `<') as subdir line is part of subdir
2105 alist (if (<= (dired-get-subdir-min elt) here)
2106 nil ; found
2107 (cdr alist))))
2108 (if localp
2109 (dired-make-relative dir default-directory)
2110 dir)))
2111
2112 ;; Subdirs start at the beginning of their header lines and end just
2113 ;; before the beginning of the next header line (or end of buffer).
2114
2115 (defun dired-subdir-max ()
2116 (save-excursion
2117 (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
2118 (point-max)
2119 (point))))
2120 \f
2121 ;; Deleting files
2122
2123 (defcustom dired-recursive-deletes nil ; Default only delete empty directories.
2124 "*Decide whether recursive deletes are allowed.
2125 nil means no recursive deletes.
2126 `always' means delete recursively without asking. This is DANGEROUS!
2127 `top' means ask for each directory at top level, but delete its subdirectories
2128 without asking.
2129 Anything else means ask for each directory."
2130 :type '(choice :tag "Delete non-empty directories"
2131 (const :tag "Yes" always)
2132 (const :tag "No--only delete empty directories" nil)
2133 (const :tag "Confirm for each directory" t)
2134 (const :tag "Confirm for each top directory only" top))
2135 :group 'dired)
2136
2137 ;; Match anything but `.' and `..'.
2138 (defvar dired-re-no-dot "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")
2139
2140 ;; Delete file, possibly delete a directory and all its files.
2141 ;; This function is usefull outside of dired. One could change it's name
2142 ;; to e.g. recursive-delete-file and put it somewhere else.
2143 (defun dired-delete-file (file &optional recursive) "\
2144 Delete FILE or directory (possibly recursively if optional RECURSIVE is true.)
2145 RECURSIVE determines what to do with a non-empty directory. If RECURSIVE is:
2146 Nil, do not delete.
2147 `always', delete recursively without asking.
2148 `top', ask for each directory at top level.
2149 Anything else, ask for each sub-directory."
2150 (let (files)
2151 ;; This test is equivalent to
2152 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
2153 ;; but more efficient
2154 (if (not (eq t (car (file-attributes file))))
2155 (delete-file file)
2156 (when (and recursive
2157 (setq files
2158 (directory-files file t dired-re-no-dot)) ; Not empty.
2159 (or (eq recursive 'always)
2160 (yes-or-no-p (format "Recursive delete of %s "
2161 (dired-make-relative file)))))
2162 (if (eq recursive 'top) (setq recursive 'always)) ; Don't ask again.
2163 (while files ; Recursively delete (possibly asking).
2164 (dired-delete-file (car files) recursive)
2165 (setq files (cdr files))))
2166 (delete-directory file))))
2167
2168 (defun dired-do-flagged-delete (&optional nomessage)
2169 "In Dired, delete the files flagged for deletion.
2170 If NOMESSAGE is non-nil, we don't display any message
2171 if there are no flagged files."
2172 (interactive)
2173 (let* ((dired-marker-char dired-del-marker)
2174 (regexp (dired-marker-regexp))
2175 case-fold-search)
2176 (if (save-excursion (goto-char (point-min))
2177 (re-search-forward regexp nil t))
2178 (dired-internal-do-deletions
2179 ;; this can't move point since ARG is nil
2180 (dired-map-over-marks (cons (dired-get-filename) (point))
2181 nil)
2182 nil)
2183 (or nomessage
2184 (message "(No deletions requested)")))))
2185
2186 (defun dired-do-delete (&optional arg)
2187 "Delete all marked (or next ARG) files."
2188 ;; This is more consistent with the file marking feature than
2189 ;; dired-do-flagged-delete.
2190 (interactive "P")
2191 (dired-internal-do-deletions
2192 ;; this may move point if ARG is an integer
2193 (dired-map-over-marks (cons (dired-get-filename) (point))
2194 arg)
2195 arg))
2196
2197 (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
2198
2199 (defun dired-internal-do-deletions (l arg)
2200 ;; L is an alist of files to delete, with their buffer positions.
2201 ;; ARG is the prefix arg.
2202 ;; Filenames are absolute (VMS needs this for logical search paths).
2203 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
2204 ;; That way as changes are made in the buffer they do not shift the
2205 ;; lines still to be changed, so the (point) values in L stay valid.
2206 ;; Also, for subdirs in natural order, a subdir's files are deleted
2207 ;; before the subdir itself - the other way around would not work.
2208 (let ((files (mapcar (function car) l))
2209 (count (length l))
2210 (succ 0))
2211 ;; canonicalize file list for pop up
2212 (setq files (nreverse (mapcar (function dired-make-relative) files)))
2213 (if (dired-mark-pop-up
2214 " *Deletions*" 'delete files dired-deletion-confirmer
2215 (format "Delete %s " (dired-mark-prompt arg files)))
2216 (save-excursion
2217 (let (failures);; files better be in reverse order for this loop!
2218 (while l
2219 (goto-char (cdr (car l)))
2220 (let (buffer-read-only)
2221 (condition-case err
2222 (let ((fn (car (car l))))
2223 (dired-delete-file fn dired-recursive-deletes)
2224 ;; if we get here, removing worked
2225 (setq succ (1+ succ))
2226 (message "%s of %s deletions" succ count)
2227 (dired-fun-in-all-buffers
2228 (file-name-directory fn) (file-name-nondirectory fn)
2229 (function dired-delete-entry) fn))
2230 (error;; catch errors from failed deletions
2231 (dired-log "%s\n" err)
2232 (setq failures (cons (car (car l)) failures)))))
2233 (setq l (cdr l)))
2234 (if (not failures)
2235 (message "%d deletion%s done" count (dired-plural-s count))
2236 (dired-log-summary
2237 (format "%d of %d deletion%s failed"
2238 (length failures) count
2239 (dired-plural-s count))
2240 failures))))
2241 (message "(No deletions performed)")))
2242 (dired-move-to-filename))
2243
2244 (defun dired-fun-in-all-buffers (directory file fun &rest args)
2245 ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS.
2246 ;; If the buffer has a wildcard pattern, check that it matches FILE.
2247 ;; (FILE does not include a directory component.)
2248 ;; FILE may be nil, in which case ignore it.
2249 ;; Return list of buffers where FUN succeeded (i.e., returned non-nil).
2250 (let (success-list)
2251 (dolist (buf (dired-buffers-for-dir (expand-file-name directory)
2252 file))
2253 (with-current-buffer buf
2254 (if (apply fun args)
2255 (setq success-list (cons (buffer-name buf) success-list)))))
2256 success-list))
2257
2258 ;; Delete the entry for FILE from
2259 (defun dired-delete-entry (file)
2260 (save-excursion
2261 (and (dired-goto-file file)
2262 (let (buffer-read-only)
2263 (delete-region (progn (beginning-of-line) (point))
2264 (save-excursion (forward-line 1) (point))))))
2265 (dired-clean-up-after-deletion file))
2266
2267 ;; This is a separate function for the sake of dired-x.el.
2268 (defun dired-clean-up-after-deletion (fn)
2269 ;; Clean up after a deleted file or directory FN.
2270 (save-excursion (and (cdr dired-subdir-alist)
2271 (dired-goto-subdir fn)
2272 (dired-kill-subdir))))
2273 \f
2274 ;; Confirmation
2275
2276 (defun dired-marker-regexp ()
2277 (concat "^" (regexp-quote (char-to-string dired-marker-char))))
2278
2279 (defun dired-plural-s (count)
2280 (if (= 1 count) "" "s"))
2281
2282 (defun dired-mark-prompt (arg files)
2283 ;; Return a string for use in a prompt, either the current file
2284 ;; name, or the marker and a count of marked files.
2285 (let ((count (length files)))
2286 (if (= count 1)
2287 (car files)
2288 ;; more than 1 file:
2289 (if (integerp arg)
2290 ;; abs(arg) = count
2291 ;; Perhaps this is nicer, but it also takes more screen space:
2292 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
2293 ;; count)
2294 (format "[next %d files]" arg)
2295 (format "%c [%d files]" dired-marker-char count)))))
2296
2297 (defun dired-pop-to-buffer (buf)
2298 ;; Pop up buffer BUF.
2299 ;; If dired-shrink-to-fit is t, make its window fit its contents.
2300 (if (not dired-shrink-to-fit)
2301 (pop-to-buffer (get-buffer-create buf))
2302 ;; let window shrink to fit:
2303 (let ((window (selected-window))
2304 target-lines w2)
2305 (cond ;; if split-height-threshold is enabled, use the largest window
2306 ((and (> (window-height (setq w2 (get-largest-window)))
2307 split-height-threshold)
2308 (= (frame-width) (window-width w2)))
2309 (setq window w2))
2310 ;; if the least-recently-used window is big enough, use it
2311 ((and (> (window-height (setq w2 (get-lru-window)))
2312 (* 2 window-min-height))
2313 (= (frame-width) (window-width w2)))
2314 (setq window w2)))
2315 (save-excursion
2316 (set-buffer buf)
2317 (goto-char (point-max))
2318 (skip-chars-backward "\n\r\t ")
2319 (setq target-lines (count-lines (point-min) (point)))
2320 ;; Don't forget to count the last line.
2321 (if (not (bolp))
2322 (setq target-lines (1+ target-lines))))
2323 (if (<= (window-height window) (* 2 window-min-height))
2324 ;; At this point, every window on the frame is too small to split.
2325 (setq w2 (display-buffer buf))
2326 (setq w2 (split-window window
2327 (max window-min-height
2328 (- (window-height window)
2329 (1+ (max window-min-height target-lines)))))))
2330 (set-window-buffer w2 buf)
2331 (if (< (1- (window-height w2)) target-lines)
2332 (progn
2333 (select-window w2)
2334 (enlarge-window (- target-lines (1- (window-height w2))))))
2335 (set-window-start w2 1)
2336 )))
2337
2338 (defvar dired-no-confirm nil
2339 "A list of symbols for commands dired should not confirm.
2340 Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress',
2341 `copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink' and
2342 `uncompress'.")
2343
2344 (defun dired-mark-pop-up (bufname op-symbol files function &rest args)
2345 "Return FUNCTION's result on ARGS after showing which files are marked.
2346 Displays the file names in a buffer named BUFNAME;
2347 nil gives \" *Marked Files*\".
2348 This uses function `dired-pop-to-buffer' to do that.
2349
2350 FUNCTION should not manipulate files, just read input
2351 (an argument or confirmation).
2352 The window is not shown if there is just one file or
2353 OP-SYMBOL is a member of the list in `dired-no-confirm'.
2354 FILES is the list of marked files."
2355 (or bufname (setq bufname " *Marked Files*"))
2356 (if (or (eq dired-no-confirm t)
2357 (memq op-symbol dired-no-confirm)
2358 (= (length files) 1))
2359 (apply function args)
2360 (with-current-buffer (get-buffer-create bufname)
2361 (erase-buffer)
2362 (dired-format-columns-of-files files)
2363 (remove-text-properties (point-min) (point-max)
2364 '(mouse-face nil help-echo nil)))
2365 (save-window-excursion
2366 (dired-pop-to-buffer bufname)
2367 (apply function args))))
2368
2369 (defun dired-format-columns-of-files (files)
2370 ;; Files should be in forward order for this loop.
2371 ;; i.e., (car files) = first file in buffer.
2372 ;; Returns the number of lines used.
2373 (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files))))
2374 (width (- (window-width (selected-window)) 2))
2375 (columns (max 1 (/ width maxlen)))
2376 (nfiles (length files))
2377 (rows (+ (/ nfiles columns)
2378 (if (zerop (% nfiles columns)) 0 1)))
2379 (i 0)
2380 (j 0))
2381 (setq files (nconc (copy-sequence files) ; fill up with empty fns
2382 (make-list (- (* columns rows) nfiles) "")))
2383 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
2384 (while (< j rows)
2385 (while (< i columns)
2386 (indent-to (* i maxlen))
2387 (insert (car files))
2388 (setq files (nthcdr rows files)
2389 i (1+ i)))
2390 (insert "\n")
2391 (setq i 0
2392 j (1+ j)
2393 files (cdr files)))
2394 rows))
2395 \f
2396 ;; Commands to mark or flag file(s) at or near current line.
2397
2398 (defun dired-repeat-over-lines (arg function)
2399 ;; This version skips non-file lines.
2400 (let ((pos (make-marker)))
2401 (beginning-of-line)
2402 (while (and (> arg 0) (not (eobp)))
2403 (setq arg (1- arg))
2404 (beginning-of-line)
2405 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
2406 (save-excursion
2407 (forward-line 1)
2408 (move-marker pos (1+ (point))))
2409 (save-excursion (funcall function))
2410 ;; Advance to the next line--actually, to the line that *was* next.
2411 ;; (If FUNCTION inserted some new lines in between, skip them.)
2412 (goto-char pos))
2413 (while (and (< arg 0) (not (bobp)))
2414 (setq arg (1+ arg))
2415 (forward-line -1)
2416 (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
2417 (beginning-of-line)
2418 (save-excursion (funcall function)))
2419 (move-marker pos nil)
2420 (dired-move-to-filename)))
2421
2422 (defun dired-between-files ()
2423 ;; This used to be a regexp match of the `total ...' line output by
2424 ;; ls, which is slightly faster, but that is not very robust; notably,
2425 ;; it fails for non-english locales.
2426 (save-excursion (not (dired-move-to-filename))))
2427
2428 (defun dired-next-marked-file (arg &optional wrap opoint)
2429 "Move to the next marked file, wrapping around the end of the buffer."
2430 (interactive "p\np")
2431 (or opoint (setq opoint (point)));; return to where interactively started
2432 (if (if (> arg 0)
2433 (re-search-forward dired-re-mark nil t arg)
2434 (beginning-of-line)
2435 (re-search-backward dired-re-mark nil t (- arg)))
2436 (dired-move-to-filename)
2437 (if (null wrap)
2438 (progn
2439 (goto-char opoint)
2440 (error "No next marked file"))
2441 (message "(Wraparound for next marked file)")
2442 (goto-char (if (> arg 0) (point-min) (point-max)))
2443 (dired-next-marked-file arg nil opoint))))
2444
2445 (defun dired-prev-marked-file (arg &optional wrap)
2446 "Move to the previous marked file, wrapping around the end of the buffer."
2447 (interactive "p\np")
2448 (dired-next-marked-file (- arg) wrap))
2449
2450 (defun dired-file-marker (file)
2451 ;; Return FILE's marker, or nil if unmarked.
2452 (save-excursion
2453 (and (dired-goto-file file)
2454 (progn
2455 (beginning-of-line)
2456 (if (not (equal ?\040 (following-char)))
2457 (following-char))))))
2458
2459 (defun dired-mark-files-in-region (start end)
2460 (let (buffer-read-only)
2461 (if (> start end)
2462 (error "start > end"))
2463 (goto-char start) ; assumed at beginning of line
2464 (while (< (point) end)
2465 ;; Skip subdir line and following garbage like the `total' line:
2466 (while (and (< (point) end) (dired-between-files))
2467 (forward-line 1))
2468 (if (and (not (looking-at dired-re-dot))
2469 (dired-get-filename nil t))
2470 (progn
2471 (delete-char 1)
2472 (insert dired-marker-char)))
2473 (forward-line 1))))
2474
2475 (defun dired-mark (arg)
2476 "Mark the current (or next ARG) files.
2477 If on a subdir headerline, mark all its files except `.' and `..'.
2478
2479 Use \\[dired-unmark-all-files] to remove all marks
2480 and \\[dired-unmark] on a subdir to remove the marks in
2481 this subdir."
2482 (interactive "P")
2483 (if (dired-get-subdir)
2484 (save-excursion (dired-mark-subdir-files))
2485 (let (buffer-read-only)
2486 (dired-repeat-over-lines
2487 (prefix-numeric-value arg)
2488 (function (lambda () (delete-char 1) (insert dired-marker-char)))))))
2489
2490 (defun dired-unmark (arg)
2491 "Unmark the current (or next ARG) files.
2492 If looking at a subdir, unmark all its files except `.' and `..'."
2493 (interactive "P")
2494 (let ((dired-marker-char ?\040))
2495 (dired-mark arg)))
2496
2497 (defun dired-flag-file-deletion (arg)
2498 "In Dired, flag the current line's file for deletion.
2499 With prefix arg, repeat over several lines.
2500
2501 If on a subdir headerline, mark all its files except `.' and `..'."
2502 (interactive "P")
2503 (let ((dired-marker-char dired-del-marker))
2504 (dired-mark arg)))
2505
2506 (defun dired-unmark-backward (arg)
2507 "In Dired, move up lines and remove deletion flag there.
2508 Optional prefix ARG says how many lines to unflag; default is one line."
2509 (interactive "p")
2510 (dired-unmark (- arg)))
2511
2512 (defun dired-toggle-marks ()
2513 "Toggle marks: marked files become unmarked, and vice versa.
2514 Files marked with other flags (such as `D') are not affected.
2515 `.' and `..' are never toggled.
2516 As always, hidden subdirs are not affected."
2517 (interactive)
2518 (save-excursion
2519 (goto-char (point-min))
2520 (let (buffer-read-only)
2521 (while (not (eobp))
2522 (or (dired-between-files)
2523 (looking-at dired-re-dot)
2524 ;; use subst instead of insdel because it does not move
2525 ;; the gap and thus should be faster and because
2526 ;; other characters are left alone automatically
2527 (apply 'subst-char-in-region
2528 (point) (1+ (point))
2529 (if (eq ?\040 (following-char)) ; SPC
2530 (list ?\040 dired-marker-char)
2531 (list dired-marker-char ?\040))))
2532 (forward-line 1)))))
2533 \f
2534 ;;; Commands to mark or flag files based on their characteristics or names.
2535
2536 (defvar dired-regexp-history nil
2537 "History list of regular expressions used in Dired commands.")
2538
2539 (defun dired-read-regexp (prompt)
2540 (read-from-minibuffer prompt nil nil nil 'dired-regexp-history))
2541
2542 (defun dired-mark-files-regexp (regexp &optional marker-char)
2543 "Mark all files matching REGEXP for use in later commands.
2544 A prefix argument means to unmark them instead.
2545 `.' and `..' are never marked.
2546
2547 REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
2548 object files--just `.o' will mark more than you might think."
2549 (interactive
2550 (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
2551 " files (regexp): "))
2552 (if current-prefix-arg ?\040)))
2553 (let ((dired-marker-char (or marker-char dired-marker-char)))
2554 (dired-mark-if
2555 (and (not (looking-at dired-re-dot))
2556 (not (eolp)) ; empty line
2557 (let ((fn (dired-get-filename nil t)))
2558 (and fn (string-match regexp (file-name-nondirectory fn)))))
2559 "matching file")))
2560
2561 (defun dired-mark-files-containing-regexp (regexp &optional marker-char)
2562 "Mark all files with contents containing REGEXP for use in later commands.
2563 A prefix argument means to unmark them instead.
2564 `.' and `..' are never marked."
2565 (interactive
2566 (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
2567 " files containing (regexp): "))
2568 (if current-prefix-arg ?\040)))
2569 (let ((dired-marker-char (or marker-char dired-marker-char)))
2570 (dired-mark-if
2571 (and (not (looking-at dired-re-dot))
2572 (not (eolp)) ; empty line
2573 (let ((fn (dired-get-filename nil t)))
2574 (when (and fn (file-readable-p fn)
2575 (not (file-directory-p fn)))
2576 (let ((prebuf (get-file-buffer fn)))
2577 (message "Checking %s" fn)
2578 ;; For now we do it inside emacs
2579 ;; Grep might be better if there are a lot of files
2580 (if prebuf
2581 (with-current-buffer prebuf
2582 (save-excursion
2583 (goto-char (point-min))
2584 (re-search-forward regexp nil t)))
2585 (with-temp-buffer
2586 (insert-file-contents fn)
2587 (goto-char (point-min))
2588 (re-search-forward regexp nil t))))
2589 )))
2590 "matching file")))
2591
2592 (defun dired-flag-files-regexp (regexp)
2593 "In Dired, flag all files containing the specified REGEXP for deletion.
2594 The match is against the non-directory part of the filename. Use `^'
2595 and `$' to anchor matches. Exclude subdirs by hiding them.
2596 `.' and `..' are never flagged."
2597 (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
2598 (dired-mark-files-regexp regexp dired-del-marker))
2599
2600 (defun dired-mark-symlinks (unflag-p)
2601 "Mark all symbolic links.
2602 With prefix argument, unflag all those files."
2603 (interactive "P")
2604 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2605 (dired-mark-if (looking-at dired-re-sym) "symbolic link")))
2606
2607 (defun dired-mark-directories (unflag-p)
2608 "Mark all directory file lines except `.' and `..'.
2609 With prefix argument, unflag all those files."
2610 (interactive "P")
2611 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2612 (dired-mark-if (and (looking-at dired-re-dir)
2613 (not (looking-at dired-re-dot)))
2614 "directory file")))
2615
2616 (defun dired-mark-executables (unflag-p)
2617 "Mark all executable files.
2618 With prefix argument, unflag all those files."
2619 (interactive "P")
2620 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2621 (dired-mark-if (looking-at dired-re-exe) "executable file")))
2622
2623 ;; dired-x.el has a dired-mark-sexp interactive command: mark
2624 ;; files for which PREDICATE returns non-nil.
2625
2626 (defun dired-flag-auto-save-files (&optional unflag-p)
2627 "Flag for deletion files whose names suggest they are auto save files.
2628 A prefix argument says to unflag those files instead."
2629 (interactive "P")
2630 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
2631 (dired-mark-if
2632 ;; It is less than general to check for # here,
2633 ;; but it's the only way this runs fast enough.
2634 (and (save-excursion (end-of-line)
2635 (or
2636 (eq (preceding-char) ?#)
2637 ;; Handle executables in case of -F option.
2638 ;; We need not worry about the other kinds
2639 ;; of markings that -F makes, since they won't
2640 ;; appear on real auto-save files.
2641 (if (eq (preceding-char) ?*)
2642 (progn
2643 (forward-char -1)
2644 (eq (preceding-char) ?#)))))
2645 (not (looking-at dired-re-dir))
2646 (let ((fn (dired-get-filename t t)))
2647 (if fn (auto-save-file-name-p
2648 (file-name-nondirectory fn)))))
2649 "auto save file")))
2650
2651 (defcustom dired-garbage-files-regexp
2652 ;; `log' here is dubious, since it's typically used for useful log
2653 ;; files, not just TeX stuff. -- fx
2654 (concat (regexp-opt
2655 '(".log" ".toc" ".dvi" ".bak" ".orig" ".rej" ".aux"))
2656 "\\'")
2657 "Regular expression to match \"garbage\" files for `dired-flag-garbage-files'."
2658 :type 'regexp
2659 :group 'dired)
2660
2661 (defun dired-flag-garbage-files ()
2662 "Flag for deletion all files that match `dired-garbage-files-regexp'."
2663 (interactive)
2664 (dired-flag-files-regexp dired-garbage-files-regexp))
2665
2666 (defun dired-flag-backup-files (&optional unflag-p)
2667 "Flag all backup files (names ending with `~') for deletion.
2668 With prefix argument, unflag these files."
2669 (interactive "P")
2670 (let ((dired-marker-char (if unflag-p ?\ dired-del-marker)))
2671 (dired-mark-if
2672 ;; Don't call backup-file-name-p unless the last character looks like
2673 ;; it might be the end of a backup file name. This isn't very general,
2674 ;; but it's the only way this runs fast enough.
2675 (and (save-excursion (end-of-line)
2676 ;; Handle executables in case of -F option.
2677 ;; We need not worry about the other kinds
2678 ;; of markings that -F makes, since they won't
2679 ;; appear on real backup files.
2680 (if (eq (preceding-char) ?*)
2681 (forward-char -1))
2682 (eq (preceding-char) ?~))
2683 (not (looking-at dired-re-dir))
2684 (let ((fn (dired-get-filename t t)))
2685 (if fn (backup-file-name-p fn))))
2686 "backup file")))
2687
2688 (defun dired-change-marks (&optional old new)
2689 "Change all OLD marks to NEW marks.
2690 OLD and NEW are both characters used to mark files."
2691 (interactive
2692 (let* ((cursor-in-echo-area t)
2693 (old (progn (message "Change (old mark): ") (read-char)))
2694 (new (progn (message "Change %c marks to (new mark): " old)
2695 (read-char))))
2696 (list old new)))
2697 (if (or (eq old ?\r) (eq new ?\r))
2698 (ding)
2699 (let ((string (format "\n%c" old))
2700 (buffer-read-only))
2701 (save-excursion
2702 (goto-char (point-min))
2703 (while (search-forward string nil t)
2704 (if (if (= old ?\ )
2705 (save-match-data
2706 (dired-get-filename 'no-dir t))
2707 t)
2708 (subst-char-in-region (match-beginning 0)
2709 (match-end 0) old new)))))))
2710
2711 (defun dired-unmark-all-marks ()
2712 "Remove all marks from all files in the Dired buffer."
2713 (interactive)
2714 (dired-unmark-all-files ?\r))
2715
2716 (defun dired-unmark-all-files (mark &optional arg)
2717 "Remove a specific mark (or any mark) from every file.
2718 After this command, type the mark character to remove,
2719 or type RET to remove all marks.
2720 With prefix arg, query for each marked file.
2721 Type \\[help-command] at that time for help."
2722 (interactive "cRemove marks (RET means all): \nP")
2723 (save-excursion
2724 (let* ((count 0)
2725 buffer-read-only case-fold-search query
2726 (string (format "\n%c" mark))
2727 (help-form "\
2728 Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
2729 `!' to unmark all remaining files with no more questions."))
2730 (goto-char (point-min))
2731 (while (if (eq mark ?\r)
2732 (re-search-forward dired-re-mark nil t)
2733 (search-forward string nil t))
2734 (if (or (not arg)
2735 (let ((file (dired-get-filename t t)))
2736 (and file
2737 (dired-query 'query "Unmark file `%s'? "
2738 file))))
2739 (progn (subst-char-in-region (1- (point)) (point)
2740 (preceding-char) ?\ )
2741 (setq count (1+ count)))))
2742 (message (if (= count 1) "1 mark removed"
2743 "%d marks removed")
2744 count))))
2745 \f
2746 ;; Logging failures operating on files, and showing the results.
2747
2748 (defvar dired-log-buffer "*Dired log*")
2749
2750 (defun dired-why ()
2751 "Pop up a buffer with error log output from Dired.
2752 A group of errors from a single command ends with a formfeed.
2753 Thus, use \\[backward-page] to find the beginning of a group of errors."
2754 (interactive)
2755 (if (get-buffer dired-log-buffer)
2756 (let ((owindow (selected-window))
2757 (window (display-buffer (get-buffer dired-log-buffer))))
2758 (unwind-protect
2759 (progn
2760 (select-window window)
2761 (goto-char (point-max))
2762 (forward-line -1)
2763 (backward-page 1)
2764 (recenter 0))
2765 (select-window owindow)))))
2766
2767 (defun dired-log (log &rest args)
2768 ;; Log a message or the contents of a buffer.
2769 ;; If LOG is a string and there are more args, it is formatted with
2770 ;; those ARGS. Usually the LOG string ends with a \n.
2771 ;; End each bunch of errors with (dired-log t):
2772 ;; this inserts the current time and buffer at the start of the page,
2773 ;; and \f (formfeed) at the end.
2774 (let ((obuf (current-buffer)))
2775 (with-current-buffer (get-buffer-create dired-log-buffer)
2776 (goto-char (point-max))
2777 (let ((inhibit-read-only t))
2778 (cond ((stringp log)
2779 (insert (if args
2780 (apply (function format) log args)
2781 log)))
2782 ((bufferp log)
2783 (insert-buffer log))
2784 ((eq t log)
2785 (backward-page 1)
2786 (unless (bolp)
2787 (insert "\n"))
2788 (insert (current-time-string)
2789 "\tBuffer `" (buffer-name obuf) "'\n")
2790 (goto-char (point-max))
2791 (insert "\f\n")))))))
2792
2793 (defun dired-log-summary (string failures)
2794 (if (= (length failures) 1)
2795 (message "%s"
2796 (with-current-buffer dired-log-buffer
2797 (goto-char (point-max))
2798 (backward-page 1)
2799 (if (eolp) (forward-line 1))
2800 (buffer-substring (point) (point-max))))
2801 (message (if failures "%s--type ? for details (%s)"
2802 "%s--type ? for details")
2803 string failures))
2804 ;; Log a summary describing a bunch of errors.
2805 (dired-log (concat "\n" string "\n"))
2806 (dired-log t))
2807 \f
2808 ;;; Sorting
2809
2810 ;; Most ls can only sort by name or by date (with -t), nothing else.
2811 ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
2812 ;; So anything that does not contain these is sort "by name".
2813
2814 (defvar dired-ls-sorting-switches "SXU"
2815 "String of `ls' switches \(single letters\) except `t' that influence sorting.
2816
2817 This indicates to Dired which option switches to watch out for because they
2818 will change the sorting order behavior of `ls'.
2819
2820 To change the default sorting order \(e.g. add a `-v' option\), see the
2821 variable `dired-listing-switches'. To temporarily override the listing
2822 format, use `\\[universal-argument] \\[dired]'.")
2823
2824 (defvar dired-sort-by-date-regexp
2825 (concat "^-[^" dired-ls-sorting-switches
2826 "]*t[^" dired-ls-sorting-switches "]*$")
2827 "Regexp recognized by dired to set `by date' mode.")
2828
2829 (defvar dired-sort-by-name-regexp
2830 (concat "^-[^t" dired-ls-sorting-switches "]+$")
2831 "Regexp recognized by dired to set `by name' mode.")
2832
2833 (defvar dired-sort-inhibit nil
2834 "Non-nil means the Dired sort command is disabled.
2835 The idea is to set this buffer-locally in special Dired buffers.")
2836
2837 (defun dired-sort-set-modeline ()
2838 ;; Set modeline display according to dired-actual-switches.
2839 ;; Modeline display of "by name" or "by date" guarantees the user a
2840 ;; match with the corresponding regexps. Non-matching switches are
2841 ;; shown literally.
2842 (setq mode-name
2843 (let (case-fold-search)
2844 (cond ((string-match dired-sort-by-name-regexp dired-actual-switches)
2845 "Dired by name")
2846 ((string-match dired-sort-by-date-regexp dired-actual-switches)
2847 "Dired by date")
2848 (t
2849 (concat "Dired " dired-actual-switches)))))
2850 (force-mode-line-update))
2851
2852 (defun dired-sort-toggle-or-edit (&optional arg)
2853 "Toggle between sort by date/name and refresh the dired buffer.
2854 With a prefix argument you can edit the current listing switches instead."
2855 (interactive "P")
2856 (when dired-sort-inhibit
2857 (error "Cannot sort this Dired buffer"))
2858 (if arg
2859 (dired-sort-other
2860 (read-string "ls switches (must contain -l): " dired-actual-switches))
2861 (dired-sort-toggle)))
2862
2863 (defun dired-sort-toggle ()
2864 ;; Toggle between sort by date/name. Reverts the buffer.
2865 (setq dired-actual-switches
2866 (let (case-fold-search)
2867 (if (string-match " " dired-actual-switches)
2868 ;; New toggle scheme: add/remove a trailing " -t"
2869 (if (string-match " -t\\'" dired-actual-switches)
2870 (substring dired-actual-switches 0 (match-beginning 0))
2871 (concat dired-actual-switches " -t"))
2872 ;; old toggle scheme: look for some 't' switch and add/remove it
2873 (concat
2874 "-l"
2875 (dired-replace-in-string (concat "[-lt"
2876 dired-ls-sorting-switches "]")
2877 ""
2878 dired-actual-switches)
2879 (if (string-match (concat "[t" dired-ls-sorting-switches "]")
2880 dired-actual-switches)
2881 ""
2882 "t")))))
2883 (dired-sort-set-modeline)
2884 (revert-buffer))
2885
2886 ;; Some user code loads dired especially for this.
2887 ;; Don't do that--use replace-regexp-in-string instead.
2888 (defun dired-replace-in-string (regexp newtext string)
2889 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
2890 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
2891 (let ((result "") (start 0) mb me)
2892 (while (string-match regexp string start)
2893 (setq mb (match-beginning 0)
2894 me (match-end 0)
2895 result (concat result (substring string start mb) newtext)
2896 start me))
2897 (concat result (substring string start))))
2898
2899 (defun dired-sort-other (switches &optional no-revert)
2900 ;; Specify new ls SWITCHES for current dired buffer. Values matching
2901 ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
2902 ;; minor mode accordingly, others appear literally in the mode line.
2903 ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
2904 (dired-sort-R-check switches)
2905 (setq dired-actual-switches switches)
2906 (if (eq major-mode 'dired-mode) (dired-sort-set-modeline))
2907 (or no-revert (revert-buffer)))
2908
2909 (make-variable-buffer-local
2910 (defvar dired-subdir-alist-pre-R nil
2911 "Value of `dired-subdir-alist' before -R switch added."))
2912
2913 (defun dired-sort-R-check (switches)
2914 "Additional processing of -R in ls option string SWITCHES.
2915 Saves `dired-subdir-alist' when R is set and restores saved value
2916 minus any directories explicitly deleted when R is cleared.
2917 To be called first in body of `dired-sort-other', etc."
2918 (cond
2919 ((and (string-match "R" switches)
2920 (not (string-match "R" dired-actual-switches)))
2921 ;; Adding -R to ls switches -- save `dired-subdir-alist':
2922 (setq dired-subdir-alist-pre-R dired-subdir-alist))
2923 ((and (string-match "R" dired-actual-switches)
2924 (not (string-match "R" switches)))
2925 ;; Deleting -R from ls switches -- revert to pre-R subdirs
2926 ;; that are still present:
2927 (setq dired-subdir-alist
2928 (if dired-subdir-alist-pre-R
2929 (let (subdirs)
2930 (while dired-subdir-alist-pre-R
2931 (if (assoc (caar dired-subdir-alist-pre-R)
2932 dired-subdir-alist)
2933 ;; subdir still present...
2934 (setq subdirs
2935 (cons (car dired-subdir-alist-pre-R)
2936 subdirs)))
2937 (setq dired-subdir-alist-pre-R
2938 (cdr dired-subdir-alist-pre-R)))
2939 (reverse subdirs))
2940 ;; No pre-R subdir alist, so revert to main directory
2941 ;; listing:
2942 (list (car (reverse dired-subdir-alist))))))))
2943 \f
2944 ;; To make this file smaller, the less common commands
2945 ;; go in a separate file. But autoload them here
2946 ;; to make the separation invisible.
2947
2948 (autoload 'dired-diff "dired-aux"
2949 "Compare file at point with file FILE using `diff'.
2950 FILE defaults to the file at the mark. (That's the mark set by
2951 \\[set-mark-command], not by Dired's \\[dired-mark] command.)
2952 The prompted-for file is the first file given to `diff'."
2953 t)
2954
2955 (autoload 'dired-backup-diff "dired-aux"
2956 "Diff this file with its backup file or vice versa.
2957 Uses the latest backup, if there are several numerical backups.
2958 If this file is a backup, diff it with its original.
2959 The backup file is the first file given to `diff'."
2960 t)
2961
2962 (autoload 'dired-clean-directory "dired-aux"
2963 "Flag numerical backups for deletion.
2964 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
2965 Positive prefix arg KEEP overrides `dired-kept-versions';
2966 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
2967
2968 To clear the flags on these files, you can use \\[dired-flag-backup-files]
2969 with a prefix argument."
2970 t)
2971
2972 (autoload 'dired-do-chmod "dired-aux"
2973 "Change the mode of the marked (or next ARG) files.
2974 This calls chmod, thus symbolic modes like `g+w' are allowed."
2975 t)
2976
2977 (autoload 'dired-do-chgrp "dired-aux"
2978 "Change the group of the marked (or next ARG) files."
2979 t)
2980
2981 (autoload 'dired-do-chown "dired-aux"
2982 "Change the owner of the marked (or next ARG) files."
2983 t)
2984
2985 (autoload 'dired-do-print "dired-aux"
2986 "Print the marked (or next ARG) files.
2987 Uses the shell command coming from variables `lpr-command' and
2988 `lpr-switches' as default."
2989 t)
2990
2991 (autoload 'dired-do-shell-command "dired-aux"
2992 "Run a shell command COMMAND on the marked files.
2993 If no files are marked or a specific numeric prefix arg is given,
2994 the next ARG files are used. Just \\[universal-argument] means the current file.
2995 The prompt mentions the file(s) or the marker, as appropriate.
2996
2997 If there is a `*' in COMMAND, surrounded by whitespace, this runs
2998 COMMAND just once with the entire file list substituted there.
2999
3000 If there is no `*', but there is a `?' in COMMAND, surrounded by
3001 whitespace, this runs COMMAND on each file individually with the
3002 file name substituted for `?'.
3003
3004 Otherwise, this runs COMMAND on each file individually with the
3005 file name added at the end of COMMAND (separated by a space).
3006
3007 `*' and `?' when not surrounded by whitespace have no special
3008 significance for `dired-do-shell-command', and are passed through
3009 normally to the shell, but you must confirm first. To pass `*' by
3010 itself to the shell as a wildcard, type `*\"\"'.
3011
3012 If COMMAND produces output, it goes to a separate buffer.
3013
3014 This feature does not try to redisplay Dired buffers afterward, as
3015 there's no telling what files COMMAND may have changed.
3016 Type \\[dired-do-redisplay] to redisplay the marked files.
3017
3018 When COMMAND runs, its working directory is the top-level directory of
3019 the Dired buffer, so output files usually are created there instead of
3020 in a subdir.
3021
3022 In a noninteractive call (from Lisp code), you must specify
3023 the list of file names explicitly with the FILE-LIST argument."
3024 t)
3025
3026 (autoload 'dired-do-kill-lines "dired-aux"
3027 "Kill all marked lines (not the files).
3028 With a prefix arg, kill all lines not marked or flagged."
3029 t)
3030
3031 (autoload 'dired-do-compress "dired-aux"
3032 "Compress or uncompress marked (or next ARG) files."
3033 t)
3034
3035 (autoload 'dired-do-byte-compile "dired-aux"
3036 "Byte compile marked (or next ARG) Emacs Lisp files."
3037 t)
3038
3039 (autoload 'dired-do-load "dired-aux"
3040 "Load the marked (or next ARG) Emacs Lisp files."
3041 t)
3042
3043 (autoload 'dired-do-redisplay "dired-aux"
3044 "Redisplay all marked (or next ARG) files.
3045 If on a subdir line, redisplay that subdirectory. In that case,
3046 a prefix arg lets you edit the `ls' switches used for the new listing."
3047 t)
3048
3049 (autoload 'dired-create-directory "dired-aux"
3050 "Create a directory called DIRECTORY."
3051 t)
3052
3053 (autoload 'dired-do-copy "dired-aux"
3054 "Copy all marked (or next ARG) files, or copy the current file.
3055 Thus, a zero prefix argument copies nothing. But it toggles the
3056 variable `dired-copy-preserve-time' (which see)."
3057 t)
3058
3059 (autoload 'dired-do-symlink "dired-aux"
3060 "Make symbolic links to current file or all marked (or next ARG) files.
3061 When operating on just the current file, you specify the new name.
3062 When operating on multiple or marked files, you specify a directory
3063 and new symbolic links are made in that directory
3064 with the same names that the files currently have."
3065 t)
3066
3067 (autoload 'dired-do-hardlink "dired-aux"
3068 "Add names (hard links) current file or all marked (or next ARG) files.
3069 When operating on just the current file, you specify the new name.
3070 When operating on multiple or marked files, you specify a directory
3071 and new hard links are made in that directory
3072 with the same names that the files currently have."
3073 t)
3074
3075 (autoload 'dired-do-rename "dired-aux"
3076 "Rename current file or all marked (or next ARG) files.
3077 When renaming just the current file, you specify the new name.
3078 When renaming multiple or marked files, you specify a directory."
3079 t)
3080
3081 (autoload 'dired-do-rename-regexp "dired-aux"
3082 "Rename marked files containing REGEXP to NEWNAME.
3083 As each match is found, the user must type a character saying
3084 what to do with it. For directions, type \\[help-command] at that time.
3085 NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
3086 REGEXP defaults to the last regexp used.
3087 With a zero prefix arg, renaming by regexp affects the full file name;
3088 usually only the non-directory part of file names is used and changed."
3089 t)
3090
3091 (autoload 'dired-do-copy-regexp "dired-aux"
3092 "Copy all marked files containing REGEXP to NEWNAME.
3093 See function `dired-do-rename-regexp' for more info."
3094 t)
3095
3096 (autoload 'dired-do-hardlink-regexp "dired-aux"
3097 "Hardlink all marked files containing REGEXP to NEWNAME.
3098 See function `dired-do-rename-regexp' for more info."
3099 t)
3100
3101 (autoload 'dired-do-symlink-regexp "dired-aux"
3102 "Symlink all marked files containing REGEXP to NEWNAME.
3103 See function `dired-do-rename-regexp' for more info."
3104 t)
3105
3106 (autoload 'dired-upcase "dired-aux"
3107 "Rename all marked (or next ARG) files to upper case."
3108 t)
3109
3110 (autoload 'dired-downcase "dired-aux"
3111 "Rename all marked (or next ARG) files to lower case."
3112 t)
3113
3114 (autoload 'dired-maybe-insert-subdir "dired-aux"
3115 "Insert this subdirectory into the same dired buffer.
3116 If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
3117 else inserts it at its natural place (as `ls -lR' would have done).
3118 With a prefix arg, you may edit the ls switches used for this listing.
3119 You can add `R' to the switches to expand the whole tree starting at
3120 this subdirectory.
3121 This function takes some pains to conform to `ls -lR' output."
3122 t)
3123
3124 (autoload 'dired-next-subdir "dired-aux"
3125 "Go to next subdirectory, regardless of level."
3126 t)
3127
3128 (autoload 'dired-prev-subdir "dired-aux"
3129 "Go to previous subdirectory, regardless of level.
3130 When called interactively and not on a subdir line, go to this subdir's line."
3131 t)
3132
3133 (autoload 'dired-goto-subdir "dired-aux"
3134 "Go to end of header line of DIR in this dired buffer.
3135 Return value of point on success, otherwise return nil.
3136 The next char is either \\n, or \\r if DIR is hidden."
3137 t)
3138
3139 (autoload 'dired-mark-subdir-files "dired-aux"
3140 "Mark all files except `.' and `..'."
3141 t)
3142
3143 (autoload 'dired-kill-subdir "dired-aux"
3144 "Remove all lines of current subdirectory.
3145 Lower levels are unaffected."
3146 t)
3147
3148 (autoload 'dired-tree-up "dired-aux"
3149 "Go up ARG levels in the dired tree."
3150 t)
3151
3152 (autoload 'dired-tree-down "dired-aux"
3153 "Go down in the dired tree."
3154 t)
3155
3156 (autoload 'dired-hide-subdir "dired-aux"
3157 "Hide or unhide the current subdirectory and move to next directory.
3158 Optional prefix arg is a repeat factor.
3159 Use \\[dired-hide-all] to (un)hide all directories."
3160 t)
3161
3162 (autoload 'dired-hide-all "dired-aux"
3163 "Hide all subdirectories, leaving only their header lines.
3164 If there is already something hidden, make everything visible again.
3165 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
3166 t)
3167
3168 (autoload 'dired-show-file-type "dired-aux"
3169 "Print the type of FILE, according to the `file' command.
3170 If FILE is a symbolic link and the optional argument DEREF-SYMLINKS is
3171 true then the type of the file linked to by FILE is printed instead."
3172 t)
3173
3174 (autoload 'dired-run-shell-command "dired-aux")
3175
3176 (autoload 'dired-query "dired-aux")
3177 \f
3178
3179 ;;;; Drag and drop support
3180
3181 (defun dired-dnd-test-function (window action types)
3182 "The test function for drag and drop into dired buffers.
3183 WINDOW is where the mouse is when this function is called. It may be a frame
3184 if the mouse is over the menu bar, scroll bar or tool bar.
3185 ACTION is the suggested action from the source, and TYPES are the
3186 types the drop data can have. This function only accepts drops with
3187 types in `x-dnd-known-types'. It returns the action suggested by the source."
3188 (let ((type (x-dnd-choose-type types)))
3189 (if type
3190 (cons action type)
3191 nil)))
3192
3193 (defun dired-dnd-popup-notice ()
3194 (x-popup-dialog
3195 t
3196 '("Recursive copies not enabled.\nSee variable dired-recursive-copies."
3197 ("Ok" . nil))))
3198
3199
3200 (defun dired-dnd-do-ask-action (uri)
3201 ;; No need to get actions and descriptions from the source,
3202 ;; we only have three actions anyway.
3203 (let ((action (x-popup-menu
3204 t
3205 (list "What action?"
3206 (cons ""
3207 '(("Copy here" . copy)
3208 ("Move here" . move)
3209 ("Link here" . link)
3210 "--"
3211 ("Cancel" . nil)))))))
3212 (if action
3213 (dired-dnd-handle-local-file uri action)
3214 nil)))
3215
3216 (defun dired-dnd-handle-local-file (uri action)
3217 "Copy, move or link a file to the dired directory.
3218 URI is the file to handle, ACTION is one of copy, move, link or ask.
3219 Ask means pop up a menu for the user to select one of copy, move or link."
3220 (require 'dired-aux)
3221 (let* ((from (x-dnd-get-local-file-name uri t))
3222 (to (if from (concat (dired-current-directory)
3223 (file-name-nondirectory from))
3224 nil)))
3225 (if from
3226 (cond ((or (eq action 'copy)
3227 (eq action 'private)) ; Treat private as copy.
3228
3229 ;; If copying a directory and dired-recursive-copies is nil,
3230 ;; dired-copy-file silently fails. Pop up a notice.
3231 (if (and (file-directory-p from)
3232 (not dired-recursive-copies))
3233 (dired-dnd-popup-notice)
3234 (progn
3235 (dired-copy-file from to 1)
3236 (dired-relist-entry to)
3237 action)))
3238
3239 ((eq action 'move)
3240 (dired-rename-file from to 1)
3241 (dired-relist-entry to)
3242 action)
3243
3244 ((eq action 'link)
3245 (make-symbolic-link from to 1)
3246 (dired-relist-entry to)
3247 action)
3248
3249 ((eq action 'ask)
3250 (dired-dnd-do-ask-action uri))
3251
3252 (t nil)))))
3253
3254 (defun dired-dnd-handle-file (uri action)
3255 "Copy, move or link a file to the dired directory if it is a local file.
3256 URI is the file to handle. If the hostname in the URI isn't local, do nothing.
3257 ACTION is one of copy, move, link or ask.
3258 Ask means pop up a menu for the user to select one of copy, move or link."
3259 (let ((local-file (x-dnd-get-local-file-uri uri)))
3260 (if local-file (dired-dnd-handle-local-file local-file action)
3261 nil)))
3262
3263
3264 \f
3265 (if (eq system-type 'vax-vms)
3266 (load "dired-vms"))
3267
3268 (provide 'dired)
3269
3270 (run-hooks 'dired-load-hook) ; for your customizations
3271
3272 ;;; arch-tag: e1af7a8f-691c-41a0-aac1-ddd4d3c87517
3273 ;;; dired.el ends here