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