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