1 ;;; dired.el --- directory-browsing commands
3 ;; Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This is a major mode for directory browsing and editing. It is
27 ;; documented in the Emacs manual.
29 ;; Rewritten in 1990/1991 to add tree features, file marking and
30 ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
31 ;; Finished up by rms in 1992.
35 ;;; Customizable variables
38 (defvar dired-listing-switches "-al"
39 "*Switches passed to `ls' for dired. MUST contain the `l' option.
40 May contain all other options that don't contradict `-l';
41 may contain even `F', `b', `i' and `s'. See also the variable
42 `dired-ls-F-marks-symlinks' concerning the `F' switch.")
44 ; Don't use absolute paths as /bin should be in any PATH and people
45 ; may prefer /usr/local/gnu/bin or whatever. However, chown is
46 ; usually not in PATH.
49 (defvar dired-chown-program
50 (if (memq system-type '(hpux dgux usg-unix-v irix linux))
52 "Name of chown command (usually `chown' or `/etc/chown').")
54 (defvar dired-chmod-program
55 (if (eq system-type 'windows-nt)
57 "Name of chmod command (usually `chmod' or `chmode').")
60 (defvar dired-ls-F-marks-symlinks nil
61 "*Informs dired about how `ls -lF' marks symbolic links.
62 Set this to t if `insert-directory-program' with `-lF' marks the symbolic link
63 itself with a trailing @ (usually the case under Ultrix).
65 Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
66 nil (the default), if it gives `bar@ -> foo', set it to t.
68 Dired checks if there is really a @ appended. Thus, if you have a
69 marking `ls' program on one host and a non-marking on another host, and
70 don't care about symbolic links which really end in a @, you can
71 always set this variable to t.")
74 (defvar dired-trivial-filenames "^\\.\\.?$\\|^#"
75 "*Regexp of files to skip when finding first file of a directory.
76 A value of nil means move to the subdir line.
77 A value of t means move to first file.")
80 (defvar dired-keep-marker-rename t
81 ;; Use t as default so that moved files "take their markers with them".
82 "*Controls marking of renamed files.
83 If t, files keep their previous marks when they are renamed.
84 If a character, renamed files (whether previously marked or not)
85 are afterward marked with that character.")
88 (defvar dired-keep-marker-copy ?C
89 "*Controls marking of copied files.
90 If t, copied files are marked if and as the corresponding original files were.
91 If a character, copied files are unconditionally marked with that character.")
94 (defvar dired-keep-marker-hardlink ?H
95 "*Controls marking of newly made hard links.
96 If t, they are marked if and as the files linked to were marked.
97 If a character, new links are unconditionally marked with that character.")
100 (defvar dired-keep-marker-symlink ?Y
101 "*Controls marking of newly made symbolic links.
102 If t, they are marked if and as the files linked to were marked.
103 If a character, new links are unconditionally marked with that character.")
106 (defvar dired-dwim-target nil
107 "*If non-nil, dired tries to guess a default target directory.
108 This means: if there is a dired buffer displayed in the next window,
109 use its current subdir, instead of the current subdir of this dired buffer.
111 The target is used in the prompt for file copy, rename etc.")
114 (defvar dired-copy-preserve-time t
115 "*If non-nil, Dired preserves the last-modified time in a file copy.
116 \(This works on only some systems.)")
118 (defvar dired-font-lock-keywords
119 '(;; Put directory headers in italics.
120 ("^ \\(/.+\\)" 1 font-lock-type-face)
121 ;; Put symlinks in bold italics.
122 ("\\([^ ]+\\) -> [^ ]+$" . font-lock-function-name-face)
123 ;; Put marks in bold.
124 ("^[^ ]" . font-lock-reference-face)
125 ;; Put files that are subdirectories in bold.
126 ("^..d.* \\([^ ]+\\)$" 1 font-lock-keyword-face))
127 "Additional expressions to highlight in Dired mode.")
131 (defvar dired-load-hook nil
132 "Run after loading dired.
133 You can customize key bindings or load extensions with this.")
135 (defvar dired-mode-hook nil
136 "Run at the very end of dired-mode.")
138 (defvar dired-before-readin-hook nil
139 "This hook is run before a dired buffer is read in (created or reverted).")
141 (defvar dired-after-readin-hook nil
142 "Hook run after each time a file or directory is read by Dired.
143 After each listing of a file or directory, this hook is run
144 with the buffer narrowed to the listing.")
145 ;; Note this can't simply be run inside function `dired-ls' as the hook
146 ;; functions probably depend on the dired-subdir-alist to be OK.
148 ;;; Internal variables
150 (defvar dired-marker-char ?* ; the answer is 42
151 ;; so that you can write things like
152 ;; (let ((dired-marker-char ?X))
153 ;; ;; great code using X markers ...
155 ;; For example, commands operating on two sets of files, A and B.
156 ;; Or marking files with digits 0-9. This could implicate
157 ;; concentric sets or an order for the marked files.
158 ;; The code depends on dynamic scoping on the marker char.
159 "In Dired, the current mark character.
160 This is what the `do' commands look for and what the `mark' commands store.")
162 (defvar dired-del-marker ?D
163 "Character used to flag files for deletion.")
165 (defvar dired-shrink-to-fit
167 ;; I see no reason ever to make this nil -- rms.
168 ;; (> baud-rate search-slow-speed)
169 "Non-nil means Dired shrinks the display buffer to fit the marked files.")
171 (defvar dired-flagging-regexp nil);; Last regexp used to flag files.
173 (defvar dired-file-version-alist)
175 (defvar dired-directory nil
176 "The directory name or shell wildcard that was used as argument to `ls'.
177 Local to each dired buffer. May be a list, in which case the car is the
178 directory name and the cdr is the actual files to list.")
180 (defvar dired-actual-switches nil
181 "The value of `dired-listing-switches' used to make this buffer's text.")
183 (defvar dired-re-inode-size "[0-9 \t]*"
184 "Regexp for optional initial inode and file size as made by `ls -i -s'.")
186 ;; These regexps must be tested at beginning-of-line, but are also
187 ;; used to search for next matches, so neither omitting "^" nor
188 ;; replacing "^" by "\n" (to make it slightly faster) will work.
190 (defvar dired-re-mark "^[^ \n]")
191 ;; "Regexp matching a marked line.
192 ;; Important: the match ends just after the marker."
193 (defvar dired-re-maybe-mark "^. ")
194 (defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d"))
195 (defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l"))
196 (defvar dired-re-exe;; match ls permission string of an executable file
199 (concat dired-re-maybe-mark dired-re-inode-size x)))
200 '("-[-r][-w][xs][-r][-w].[-r][-w]."
201 "-[-r][-w].[-r][-w][xs][-r][-w]."
202 "-[-r][-w].[-r][-w].[-r][-w][xst]")
204 (defvar dired-re-perms "[-bcdlps][-r][-w].[-r][-w].[-r][-w].")
205 (defvar dired-re-dot "^.* \\.\\.?$")
207 ;; The subdirectory names in this list are expanded.
208 (defvar dired-subdir-alist nil
209 "Association list of subdirectories and their buffer positions.
210 Each subdirectory has an element: (DIRNAME . STARTMARKER).
211 The order of elements is the reverse of the order in the buffer.
212 In simple cases, this list contains one element.")
214 (defvar dired-subdir-regexp "^. \\([^\n\r]+\\)\\(:\\)[\n\r]"
215 "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
216 Subexpression 1 is the subdirectory proper, no trailing colon.
217 The match starts at the beginning of the line and ends after the end
218 of the line (\\n or \\r).
219 Subexpression 2 must end right before the \\n or \\r.")
222 ;;; Macros must be defined before they are used, for the byte compiler.
224 ;; Mark all files for which CONDITION evals to non-nil.
225 ;; CONDITION is evaluated on each line, with point at beginning of line.
226 ;; MSG is a noun phrase for the type of files being marked.
227 ;; It should end with a noun that can be pluralized by adding `s'.
228 ;; Return value is the number of files marked, or nil if none were marked.
229 (defmacro dired-mark-if (predicate msg)
230 (` (let (buffer-read-only count)
233 (if (, msg) (message "Marking %ss..." (, msg)))
234 (goto-char (point-min))
239 (insert dired-marker-char)
240 (setq count (1+ count))))
242 (if (, msg) (message "%s %s%s %s%s."
245 (dired-plural-s count)
246 (if (eq dired-marker-char ?\040) "un" "")
247 (if (eq dired-marker-char dired-del-marker)
248 "flagged" "marked"))))
249 (and (> count 0) count))))
251 (defmacro dired-map-over-marks (body arg &optional show-progress)
252 ;; "Macro: Perform BODY with point somewhere on each marked line
253 ;;and return a list of BODY's results.
254 ;;If no marked file could be found, execute BODY on the current line.
255 ;; If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
256 ;; files instead of the marked files.
257 ;; In that case point is dragged along. This is so that commands on
258 ;; the next ARG (instead of the marked) files can be chained easily.
259 ;; If ARG is otherwise non-nil, use current file instead.
260 ;;If optional third arg SHOW-PROGRESS evaluates to non-nil,
261 ;; redisplay the dired buffer after each file is processed.
262 ;;No guarantee is made about the position on the marked line.
263 ;; BODY must ensure this itself if it depends on this.
264 ;;Search starts at the beginning of the buffer, thus the car of the list
265 ;; corresponds to the line nearest to the buffer's bottom. This
266 ;; is also true for (positive and negative) integer values of ARG.
267 ;;BODY should not be too long as it is expanded four times."
269 ;;Warning: BODY must not add new lines before point - this may cause an
271 ;;This warning should not apply any longer, sk 2-Sep-1991 14:10.
273 (let (buffer-read-only case-fold-search found results)
275 (if (integerp (, arg))
276 (progn;; no save-excursion, want to move point.
277 (dired-repeat-over-lines
280 (if (, show-progress) (sit-for 0))
281 (setq results (cons (, body) results)))))
285 ;; non-nil, non-integer ARG means use current file:
287 (let ((regexp (dired-marker-regexp)) next-position)
289 (goto-char (point-min))
290 ;; remember position of next marked file before BODY
291 ;; can insert lines before the just found file,
292 ;; confusing us by finding the same marked file again
294 (setq next-position (and (re-search-forward regexp nil t)
296 found (not (null next-position)))
298 (goto-char next-position)
299 (if (, show-progress) (sit-for 0))
300 (setq results (cons (, body) results))
301 ;; move after last match
302 (goto-char next-position)
304 (set-marker next-position nil)
305 (setq next-position (and (re-search-forward regexp nil t)
310 ;; save-excursion loses, again
311 (dired-move-to-filename))))
313 (defun dired-get-marked-files (&optional localp arg)
314 "Return the marked files' names as list of strings.
315 The list is in the same order as the buffer, that is, the car is the
317 Values returned are normally absolute pathnames.
318 Optional arg LOCALP as in `dired-get-filename'.
319 Optional second argument ARG forces to use other files. If ARG is an
320 integer, use the next ARG files. If ARG is otherwise non-nil, use
321 current file. Usually ARG comes from the current prefix arg."
323 (nreverse (dired-map-over-marks (dired-get-filename localp) arg))))
326 ;; Function dired-ls is redefinable for VMS, ange-ftp, Prospero or
327 ;; other special applications.
331 (defun dired-read-dir-and-switches (str)
332 ;; For use in interactive.
334 (if current-prefix-arg
335 (read-string "Dired listing switches: "
336 dired-listing-switches))
337 (read-file-name (format "Dired %s(directory): " str)
338 nil default-directory nil))))
340 ;;;###autoload (define-key ctl-x-map "d" 'dired)
342 (defun dired (dirname &optional switches)
343 "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
344 Optional second argument SWITCHES specifies the `ls' options used.
345 \(Interactively, use a prefix argument to be able to specify SWITCHES.)
346 Dired displays a list of files in DIRNAME (which may also have
347 shell wildcards appended to select certain files). If DIRNAME is a cons,
348 its first element is taken as the directory name and the resr as an explicit
349 list of files to make directory entries for.
351 You can move around in it with the usual commands.
352 You can flag files for deletion with \\[dired-flag-file-deletion] and then
353 delete them by typing \\[dired-do-flagged-delete].
354 Type \\[describe-mode] after entering dired for more info.
356 If DIRNAME is already in a dired buffer, that buffer is used without refresh."
357 ;; Cannot use (interactive "D") because of wildcards.
358 (interactive (dired-read-dir-and-switches ""))
359 (switch-to-buffer (dired-noselect dirname switches)))
361 ;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
363 (defun dired-other-window (dirname &optional switches)
364 "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
365 (interactive (dired-read-dir-and-switches "in other window "))
366 (switch-to-buffer-other-window (dired-noselect dirname switches)))
368 ;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
370 (defun dired-other-frame (dirname &optional switches)
371 "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame."
372 (interactive (dired-read-dir-and-switches "in other frame "))
373 (switch-to-buffer-other-frame (dired-noselect dirname switches)))
376 (defun dired-noselect (dir-or-list &optional switches)
377 "Like `dired' but returns the dired buffer as value, does not select it."
378 (or dir-or-list (setq dir-or-list default-directory))
379 ;; This loses the distinction between "/foo/*/" and "/foo/*" that
382 (if (consp dir-or-list)
383 (setq dirname (car dir-or-list))
384 (setq dirname dir-or-list))
385 (setq dirname (abbreviate-file-name
386 (expand-file-name (directory-file-name dirname))))
387 (if (file-directory-p dirname)
388 (setq dirname (file-name-as-directory dirname)))
389 (if (consp dir-or-list)
390 (setq dir-or-list (cons dirname (cdr dir-or-list)))
391 (setq dir-or-list dirname))
392 (dired-internal-noselect dir-or-list switches)))
394 ;; Separate function from dired-noselect for the sake of dired-vms.el.
395 (defun dired-internal-noselect (dir-or-list &optional switches)
396 ;; If there is an existing dired buffer for DIRNAME, just leave
397 ;; buffer as it is (don't even call dired-revert).
398 ;; This saves time especially for deep trees or with ange-ftp.
399 ;; The user can type `g'easily, and it is more consistent with find-file.
400 ;; But if SWITCHES are given they are probably different from the
401 ;; buffer's old value, so call dired-sort-other, which does
402 ;; revert the buffer.
403 ;; A pity we can't possibly do "Directory has changed - refresh? "
404 ;; like find-file does.
405 (let* ((dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
406 (buffer (dired-find-buffer-nocreate dir-or-list))
407 ;; note that buffer already is in dired-mode, if found
408 (new-buffer-p (not buffer))
409 (old-buf (current-buffer)))
411 (let ((default-major-mode 'fundamental-mode))
412 ;; We don't want default-major-mode to run hooks and set auto-fill
413 ;; or whatever, now that dired-mode does not
414 ;; kill-all-local-variables any longer.
415 (setq buffer (create-file-buffer (directory-file-name dirname)))))
417 (if (not new-buffer-p) ; existing buffer ...
418 (if switches ; ... but new switches
419 (dired-sort-other switches) ; this calls dired-revert
420 ;; If directory has changed on disk, offer to revert.
421 (if (let ((attributes (file-attributes dirname))
422 (modtime (visited-file-modtime)))
424 (not (eq (car attributes) t))
425 (and (= (car (nth 5 attributes)) (car modtime))
426 (= (nth 1 (nth 5 attributes)) (cdr modtime)))))
429 (substitute-command-keys
430 "Directory has changed on disk; type \\[revert-buffer] to update Dired"))))
432 (setq default-directory
433 (if (file-directory-p dirname)
435 (file-name-directory dirname)))
436 (or switches (setq switches dired-listing-switches))
437 (dired-mode dirname switches)
438 ;; default-directory and dired-actual-switches are set now
439 ;; (buffer-local), so we can call dired-readin:
442 (progn (dired-readin dir-or-list buffer)
444 ;; dired-readin can fail if parent directories are inaccessible.
445 ;; Don't leave an empty buffer around in that case.
446 (if failed (kill-buffer buffer))))
447 ;; No need to narrow since the whole buffer contains just
448 ;; dired-readin's output, nothing else. The hook can
449 ;; successfully use dired functions (e.g. dired-get-filename)
450 ;; as the subdir-alist has been built in dired-readin.
451 (run-hooks 'dired-after-readin-hook)
452 (goto-char (point-min))
453 (dired-initial-position dirname))
457 ;; This differs from dired-buffers-for-dir in that it does not consider
458 ;; subdirs of default-directory and searches for the first match only
459 (defun dired-find-buffer-nocreate (dirname)
460 (let (found (blist dired-buffers)) ; was (buffer-list)
463 (set-buffer (cdr (car blist)))
464 (if (and (eq major-mode 'dired-mode)
465 (equal dired-directory dirname))
466 (setq found (cdr (car blist))
468 (setq blist (cdr blist)))))
472 ;; Read in a new dired buffer
474 ;; dired-readin differs from dired-insert-subdir in that it accepts
475 ;; wildcards, erases the buffer, and builds the subdir-alist anew
476 ;; (including making it buffer-local and clearing it first).
477 (defun dired-readin (dir-or-list buffer)
478 ;; default-directory and dired-actual-switches must be buffer-local
479 ;; and initialized by now.
480 ;; Thus we can test (equal default-directory dirname) instead of
481 ;; (file-directory-p dirname) and save a filesystem transaction.
482 ;; Also, we can run this hook which may want to modify the switches
483 ;; based on default-directory, e.g. with ange-ftp to a SysV host
484 ;; where ls won't understand -Al switches.
486 (if (consp dir-or-list)
487 (setq dirname (car dir-or-list))
488 (setq dirname dir-or-list))
489 (setq dirname (expand-file-name dirname))
490 (if (consp dir-or-list)
491 (setq dir-or-list (cons dirname (cdr dir-or-list))))
492 (run-hooks 'dired-before-readin-hook)
494 (message "Reading directory %s..." dirname)
496 (let (buffer-read-only (failed t))
499 (dired-readin-insert dir-or-list)
500 (indent-rigidly (point-min) (point-max) 2)
501 ;; We need this to make the root dir have a header line as all
502 ;; other subdirs have:
503 (goto-char (point-min))
504 (dired-insert-headerline default-directory)
505 ;; can't run dired-after-readin-hook here, it may depend on the subdir
508 (message "Reading directory %s...done" dirname)
509 ;; Must first make alist buffer local and set it to nil because
510 ;; dired-build-subdir-alist will call dired-clear-alist first
511 (set (make-local-variable 'dired-subdir-alist) nil)
512 (dired-build-subdir-alist)
513 (let ((attributes (file-attributes dirname)))
514 (if (eq (car attributes) t)
515 (set-visited-file-modtime (nth 5 attributes))))
516 (set-buffer-modified-p nil))))
518 ;; Subroutines of dired-readin
520 (defun dired-readin-insert (dir-or-list)
521 ;; Just insert listing for the passed-in directory or
522 ;; directory-and-file list, assuming a clean buffer.
524 (if (consp dir-or-list)
525 (setq dirname (car dir-or-list))
526 (setq dirname dir-or-list))
527 ;; Expand before comparing in case one or both have been abbreviated.
528 (if (and (equal (expand-file-name default-directory)
529 (expand-file-name dirname))
530 (not (consp dir-or-list)))
531 ;; If we are reading a whole single directory...
532 (dired-insert-directory dir-or-list dired-actual-switches nil t)
533 (if (not (file-readable-p
534 (directory-file-name (file-name-directory dirname))))
535 (error "Directory %s inaccessible or nonexistent" dirname)
536 ;; Else assume it contains wildcards,
537 ;; unless it is an explicit list of files.
538 (dired-insert-directory dir-or-list dired-actual-switches
539 (not (listp dir-or-list)))
540 (save-excursion ;; insert wildcard instead of total line:
541 (goto-char (point-min))
542 (insert "wildcard " (file-name-nondirectory dirname) "\n"))))))
544 (defun dired-insert-directory (dir-or-list switches &optional wildcard full-p)
545 ;; Do the right thing whether dir-or-list is atomic or not. If it is,
546 ;; inset all files listed in the cdr (the car is the passed-in directory
548 (let ((opoint (point))
550 (if (consp dir-or-list)
551 ;; In this case, use the file names in the cdr
552 ;; exactly as originally given to dired-noselect.
554 (function (lambda (x) (insert-directory x switches wildcard full-p)))
556 ;; Expand the file name here because it may have been abbreviated
557 ;; in dired-noselect.
558 (insert-directory (expand-file-name dir-or-list) switches wildcard full-p))
559 ;; Quote certain characters, unless ls quoted them for us.
560 (if (not (string-match "b" dired-actual-switches))
562 (setq end (point-marker))
564 (while (search-forward "\\" end t)
565 (replace-match "\\\\" nil t))
567 (while (search-forward "\^m" end t)
568 (replace-match "\\015" nil t))
569 (set-marker end nil)))
570 (dired-insert-set-properties opoint (point)))
571 (setq dired-directory dir-or-list))
573 ;; Make the file names highlight when the mouse is on them.
574 (defun dired-insert-set-properties (beg end)
577 (while (< (point) end)
578 (if (dired-move-to-filename)
579 (put-text-property (point)
581 (dired-move-to-end-of-filename)
583 'mouse-face 'highlight))
586 (defun dired-insert-headerline (dir);; also used by dired-insert-subdir
587 ;; Insert DIR's headerline with no trailing slash, exactly like ls
588 ;; would, and put cursor where dired-build-subdir-alist puts subdir
590 (save-excursion (insert " " (directory-file-name dir) ":\n")))
593 ;; Reverting a dired buffer
595 (defun dired-revert (&optional arg noconfirm)
596 ;; Reread the dired buffer. Must also be called after
597 ;; dired-actual-switches have changed.
598 ;; Should not fail even on completely garbaged buffers.
599 ;; Preserves old cursor, marks/flags, hidden-p.
600 (widen) ; just in case user narrowed
601 (let ((opoint (point))
602 (ofile (dired-get-filename nil t))
603 (mark-alist nil) ; save marked files
604 (hidden-subdirs (dired-remember-hidden))
605 (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
606 (case-fold-search nil) ; we check for upper case ls flags
608 (goto-char (point-min))
609 (setq mark-alist;; only after dired-remember-hidden since this unhides:
610 (dired-remember-marks (point-min) (point-max)))
611 ;; treat top level dir extra (it may contain wildcards)
613 (if (consp dired-directory) (car dired-directory) dired-directory))
614 (dired-readin dired-directory (current-buffer))
615 (let ((dired-after-readin-hook nil))
616 ;; don't run that hook for each subdir...
617 (dired-insert-old-subdirs old-subdir-alist))
618 (dired-mark-remembered mark-alist) ; mark files that were marked
619 ;; ... run the hook for the whole buffer, and only after markers
620 ;; have been reinserted (else omitting in dired-x would omit marked files)
621 (run-hooks 'dired-after-readin-hook) ; no need to narrow
622 (or (and ofile (dired-goto-file ofile)) ; move cursor to where it
623 (goto-char opoint)) ; was before
624 (dired-move-to-filename)
625 (save-excursion ; hide subdirs that were hidden
626 (mapcar (function (lambda (dir)
627 (if (dired-goto-subdir dir)
628 (dired-hide-subdir 1))))
630 ;; outside of the let scope
631 ;;; Might as well not override the user if the user changed this.
632 ;;; (setq buffer-read-only t)
635 ;; Subroutines of dired-revert
636 ;; Some of these are also used when inserting subdirs.
638 (defun dired-remember-marks (beg end)
639 ;; Return alist of files and their marks, from BEG to END.
640 (if selective-display ; must unhide to make this work.
641 (let (buffer-read-only)
642 (subst-char-in-region beg end ?\r ?\n)))
646 (while (re-search-forward dired-re-mark end t)
647 (if (setq fil (dired-get-filename nil t))
648 (setq chr (preceding-char)
649 alist (cons (cons fil chr) alist)))))
652 ;; Mark all files remembered in ALIST.
653 ;; Each element of ALIST looks like (FILE . MARKERCHAR).
654 (defun dired-mark-remembered (alist)
657 (setq elt (car alist)
661 (if (dired-goto-file fil)
667 ;; Return a list of names of subdirs currently hidden.
668 (defun dired-remember-hidden ()
669 (let ((l dired-subdir-alist) dir pos result)
671 (setq dir (car (car l))
675 (skip-chars-forward "^\r\n")
676 (if (eq (following-char) ?\r)
677 (setq result (cons dir result))))
680 ;; Try to insert all subdirs that were displayed before,
681 ;; according to the former subdir alist OLD-SUBDIR-ALIST.
682 (defun dired-insert-old-subdirs (old-subdir-alist)
683 (or (string-match "R" dired-actual-switches)
685 (while old-subdir-alist
686 (setq elt (car old-subdir-alist)
687 old-subdir-alist (cdr old-subdir-alist)
692 (dired-insert-subdir dir))
695 ;; Remove directory DIR from any directory cache.
696 (defun dired-uncache (dir)
697 (let ((handler (find-file-name-handler dir 'dired-uncache)))
699 (funcall handler 'dired-uncache dir))))
701 ;; dired mode key bindings and initialization
703 (defvar dired-mode-map nil "Local keymap for dired-mode buffers.")
706 ;; This looks ugly when substitute-command-keys uses C-d instead d:
707 ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
709 (setq dired-mode-map (make-keymap))
710 (suppress-keymap dired-mode-map)
711 (define-key dired-mode-map [mouse-2] 'dired-mouse-find-file-other-window)
712 ;; Commands to mark or flag certain categories of files
713 (define-key dired-mode-map "#" 'dired-flag-auto-save-files)
714 (define-key dired-mode-map "*" 'dired-mark-executables)
715 (define-key dired-mode-map "." 'dired-clean-directory)
716 (define-key dired-mode-map "/" 'dired-mark-directories)
717 (define-key dired-mode-map "@" 'dired-mark-symlinks)
718 (define-key dired-mode-map "~" 'dired-flag-backup-files)
719 ;; Upper case keys (except !) for operating on the marked files
720 (define-key dired-mode-map "A" 'dired-do-search)
721 (define-key dired-mode-map "C" 'dired-do-copy)
722 (define-key dired-mode-map "B" 'dired-do-byte-compile)
723 (define-key dired-mode-map "D" 'dired-do-delete)
724 (define-key dired-mode-map "G" 'dired-do-chgrp)
725 (define-key dired-mode-map "H" 'dired-do-hardlink)
726 (define-key dired-mode-map "L" 'dired-do-load)
727 (define-key dired-mode-map "M" 'dired-do-chmod)
728 (define-key dired-mode-map "O" 'dired-do-chown)
729 (define-key dired-mode-map "P" 'dired-do-print)
730 (define-key dired-mode-map "Q" 'dired-do-query-replace)
731 (define-key dired-mode-map "R" 'dired-do-rename)
732 (define-key dired-mode-map "S" 'dired-do-symlink)
733 (define-key dired-mode-map "X" 'dired-do-shell-command)
734 (define-key dired-mode-map "Z" 'dired-do-compress)
735 (define-key dired-mode-map "!" 'dired-do-shell-command)
736 ;; Comparison commands
737 (define-key dired-mode-map "=" 'dired-diff)
738 (define-key dired-mode-map "\M-=" 'dired-backup-diff)
739 ;; Tree Dired commands
740 (define-key dired-mode-map "\M-\C-?" 'dired-unmark-all-files)
741 (define-key dired-mode-map "\M-\C-d" 'dired-tree-down)
742 (define-key dired-mode-map "\M-\C-u" 'dired-tree-up)
743 (define-key dired-mode-map "\M-\C-n" 'dired-next-subdir)
744 (define-key dired-mode-map "\M-\C-p" 'dired-prev-subdir)
745 ;; move to marked files
746 (define-key dired-mode-map "\M-{" 'dired-prev-marked-file)
747 (define-key dired-mode-map "\M-}" 'dired-next-marked-file)
748 ;; Make all regexp commands share a `%' prefix:
749 ;; We used to get to the submap via a symbol dired-regexp-prefix,
750 ;; but that seems to serve little purpose, and copy-keymap
751 ;; does a better job without it.
752 (define-key dired-mode-map "%" nil)
753 (define-key dired-mode-map "%u" 'dired-upcase)
754 (define-key dired-mode-map "%l" 'dired-downcase)
755 (define-key dired-mode-map "%d" 'dired-flag-files-regexp)
756 (define-key dired-mode-map "%m" 'dired-mark-files-regexp)
757 (define-key dired-mode-map "%r" 'dired-do-rename-regexp)
758 (define-key dired-mode-map "%C" 'dired-do-copy-regexp)
759 (define-key dired-mode-map "%H" 'dired-do-hardlink-regexp)
760 (define-key dired-mode-map "%R" 'dired-do-rename-regexp)
761 (define-key dired-mode-map "%S" 'dired-do-symlink-regexp)
762 ;; Lower keys for commands not operating on all the marked files
763 (define-key dired-mode-map "c" 'dired-change-marks)
764 (define-key dired-mode-map "d" 'dired-flag-file-deletion)
765 (define-key dired-mode-map "e" 'dired-find-file)
766 (define-key dired-mode-map "f" 'dired-find-file)
767 (define-key dired-mode-map "\C-m" 'dired-advertised-find-file)
768 (define-key dired-mode-map "g" 'revert-buffer)
769 (define-key dired-mode-map "h" 'describe-mode)
770 (define-key dired-mode-map "i" 'dired-maybe-insert-subdir)
771 (define-key dired-mode-map "k" 'dired-do-kill-lines)
772 (define-key dired-mode-map "l" 'dired-do-redisplay)
773 (define-key dired-mode-map "m" 'dired-mark)
774 (define-key dired-mode-map "n" 'dired-next-line)
775 (define-key dired-mode-map "o" 'dired-find-file-other-window)
776 (define-key dired-mode-map "\C-o" 'dired-display-file)
777 (define-key dired-mode-map "p" 'dired-previous-line)
778 (define-key dired-mode-map "q" 'dired-quit)
779 (define-key dired-mode-map "s" 'dired-sort-toggle-or-edit)
780 (define-key dired-mode-map "u" 'dired-unmark)
781 (define-key dired-mode-map "v" 'dired-view-file)
782 (define-key dired-mode-map "x" 'dired-do-flagged-delete)
783 (define-key dired-mode-map "+" 'dired-create-directory)
785 (define-key dired-mode-map "<" 'dired-prev-dirline)
786 (define-key dired-mode-map ">" 'dired-next-dirline)
787 (define-key dired-mode-map "^" 'dired-up-directory)
788 (define-key dired-mode-map " " 'dired-next-line)
789 (define-key dired-mode-map "\C-n" 'dired-next-line)
790 (define-key dired-mode-map "\C-p" 'dired-previous-line)
791 (define-key dired-mode-map [down] 'dired-next-line)
792 (define-key dired-mode-map [up] 'dired-previous-line)
794 (define-key dired-mode-map "$" 'dired-hide-subdir)
795 (define-key dired-mode-map "\M-$" 'dired-hide-all)
797 (define-key dired-mode-map "?" 'dired-summary)
798 (define-key dired-mode-map "\177" 'dired-unmark-backward)
799 (define-key dired-mode-map "\C-_" 'dired-undo)
800 (define-key dired-mode-map "\C-xu" 'dired-undo)
803 ;; Make menu bar items.
805 ;; Get rid of the Edit menu bar item to save space.
806 (define-key dired-mode-map [menu-bar edit] 'undefined)
808 (define-key dired-mode-map [menu-bar subdir]
809 (cons "Subdir" (make-sparse-keymap "Subdir")))
811 (define-key dired-mode-map [menu-bar subdir hide-all]
812 '("Hide All" . dired-hide-all))
813 (define-key dired-mode-map [menu-bar subdir hide-subdir]
814 '("Hide Subdir" . dired-hide-subdir))
815 (define-key dired-mode-map [menu-bar subdir tree-down]
816 '("Tree Down" . dired-tree-down))
817 (define-key dired-mode-map [menu-bar subdir tree-up]
818 '("Tree Up" . dired-tree-up))
819 (define-key dired-mode-map [menu-bar subdir up]
820 '("Up Directory" . dired-up-directory))
821 (define-key dired-mode-map [menu-bar subdir prev-subdir]
822 '("Prev Subdir" . dired-prev-subdir))
823 (define-key dired-mode-map [menu-bar subdir next-subdir]
824 '("Next Subdir" . dired-next-subdir))
825 (define-key dired-mode-map [menu-bar subdir prev-dirline]
826 '("Prev Dirline" . dired-prev-dirline))
827 (define-key dired-mode-map [menu-bar subdir next-dirline]
828 '("Next Dirline" . dired-next-dirline))
829 (define-key dired-mode-map [menu-bar subdir insert]
830 '("Insert This Subdir" . dired-maybe-insert-subdir))
832 (define-key dired-mode-map [menu-bar immediate]
833 (cons "Immediate" (make-sparse-keymap "Immediate")))
835 (define-key dired-mode-map [menu-bar immediate backup-diff]
836 '("Compare with Backup" . dired-backup-diff))
837 (define-key dired-mode-map [menu-bar immediate diff]
838 '("Diff" . dired-diff))
839 (define-key dired-mode-map [menu-bar immediate view]
840 '("View This File" . dired-view-file))
841 (define-key dired-mode-map [menu-bar immediate display]
842 '("Display in Other Window" . dired-display-file))
843 (define-key dired-mode-map [menu-bar immediate find-file-other-window]
844 '("Find in Other Window" . dired-find-file-other-window))
845 (define-key dired-mode-map [menu-bar immediate find-file]
846 '("Find This File" . dired-find-file))
847 (define-key dired-mode-map [menu-bar immediate create-directory]
848 '("Create Directory..." . dired-create-directory))
850 (define-key dired-mode-map [menu-bar regexp]
851 (cons "Regexp" (make-sparse-keymap "Regexp")))
853 (define-key dired-mode-map [menu-bar regexp downcase]
854 '("Downcase" . dired-downcase))
855 (define-key dired-mode-map [menu-bar regexp upcase]
856 '("Upcase" . dired-upcase))
857 (define-key dired-mode-map [menu-bar regexp hardlink]
858 '("Hardlink..." . dired-do-hardlink-regexp))
859 (define-key dired-mode-map [menu-bar regexp symlink]
860 '("Symlink..." . dired-do-symlink-regexp))
861 (define-key dired-mode-map [menu-bar regexp rename]
862 '("Rename..." . dired-do-rename-regexp))
863 (define-key dired-mode-map [menu-bar regexp copy]
864 '("Copy..." . dired-do-copy-regexp))
865 (define-key dired-mode-map [menu-bar regexp flag]
866 '("Flag..." . dired-flag-files-regexp))
867 (define-key dired-mode-map [menu-bar regexp mark]
868 '("Mark..." . dired-mark-files-regexp))
870 (define-key dired-mode-map [menu-bar mark]
871 (cons "Mark" (make-sparse-keymap "Mark")))
873 (define-key dired-mode-map [menu-bar mark prev]
874 '("Previous Marked" . dired-prev-marked-file))
875 (define-key dired-mode-map [menu-bar mark next]
876 '("Next Marked" . dired-next-marked-file))
877 (define-key dired-mode-map [menu-bar mark marks]
878 '("Change Marks..." . dired-change-marks))
879 (define-key dired-mode-map [menu-bar mark unmark-all]
880 '("Unmark All" . dired-unmark-all-files-no-query))
881 (define-key dired-mode-map [menu-bar mark symlinks]
882 '("Mark Symlinks" . dired-mark-symlinks))
883 (define-key dired-mode-map [menu-bar mark directories]
884 '("Mark Directories" . dired-mark-directories))
885 (define-key dired-mode-map [menu-bar mark directory]
886 '("Mark Old Backups" . dired-clean-directory))
887 (define-key dired-mode-map [menu-bar mark executables]
888 '("Mark Executables" . dired-mark-executables))
889 (define-key dired-mode-map [menu-bar mark backup-files]
890 '("Flag Backup Files" . dired-flag-backup-files))
891 (define-key dired-mode-map [menu-bar mark auto-save-files]
892 '("Flag Auto-save Files" . dired-flag-auto-save-files))
893 (define-key dired-mode-map [menu-bar mark deletion]
894 '("Flag" . dired-flag-file-deletion))
895 (define-key dired-mode-map [menu-bar mark unmark]
896 '("Unmark" . dired-unmark))
897 (define-key dired-mode-map [menu-bar mark mark]
898 '("Mark" . dired-mark))
900 (define-key dired-mode-map [menu-bar operate]
901 (cons "Operate" (make-sparse-keymap "Operate")))
903 (define-key dired-mode-map [menu-bar operate query-replace]
904 '("Query Replace in Files..." . dired-do-query-replace))
905 (define-key dired-mode-map [menu-bar operate search]
906 '("Search Files..." . dired-do-search))
907 (define-key dired-mode-map [menu-bar operate chown]
908 '("Change Owner..." . dired-do-chown))
909 (define-key dired-mode-map [menu-bar operate chgrp]
910 '("Change Group..." . dired-do-chgrp))
911 (define-key dired-mode-map [menu-bar operate chmod]
912 '("Change Mode..." . dired-do-chmod))
913 (define-key dired-mode-map [menu-bar operate load]
914 '("Load" . dired-do-load))
915 (define-key dired-mode-map [menu-bar operate compile]
916 '("Byte-compile" . dired-do-byte-compile))
917 (define-key dired-mode-map [menu-bar operate compress]
918 '("Compress" . dired-do-compress))
919 (define-key dired-mode-map [menu-bar operate print]
920 '("Print" . dired-do-print))
921 (define-key dired-mode-map [menu-bar operate hardlink]
922 '("Hardlink to..." . dired-do-hardlink))
923 (define-key dired-mode-map [menu-bar operate symlink]
924 '("Symlink to..." . dired-do-symlink))
925 (define-key dired-mode-map [menu-bar operate command]
926 '("Shell Command..." . dired-do-shell-command))
927 (define-key dired-mode-map [menu-bar operate delete]
928 '("Delete" . dired-do-delete))
929 (define-key dired-mode-map [menu-bar operate rename]
930 '("Rename to..." . dired-do-rename))
931 (define-key dired-mode-map [menu-bar operate copy]
932 '("Copy to..." . dired-do-copy))
934 ;; Dired mode is suitable only for specially formatted data.
935 (put 'dired-mode 'mode-class 'special)
937 (defun dired-mode (&optional dirname switches)
939 Mode for \"editing\" directory listings.
940 In dired, you are \"editing\" a list of the files in a directory and
941 \(optionally) its subdirectories, in the format of `ls -lR'.
942 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
943 \"Editing\" means that you can run shell commands on files, visit,
944 compress, load or byte-compile them, change their file attributes
945 and insert subdirectories into the same buffer. You can \"mark\"
946 files for later commands or \"flag\" them for deletion, either file
947 by file or all files matching certain criteria.
948 You can move using the usual cursor motion commands.\\<dired-mode-map>
949 Letters no longer insert themselves. Digits are prefix arguments.
950 Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
951 Type \\[dired-mark] to Mark a file or subdirectory for later commands.
952 Most commands operate on the marked files and use the current file
953 if no files are marked. Use a numeric prefix argument to operate on
954 the next ARG (or previous -ARG if ARG<0) files, or just `1'
955 to operate on the current file only. Prefix arguments override marks.
956 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
957 to see why something went wrong.
958 Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
959 Type \\[dired-unmark-backward] to back up one line and unflag.
960 Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
961 Type \\[dired-advertised-find-file] to Find the current line's file
962 (or dired it in another buffer, if it is a directory).
963 Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
964 Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
965 Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
966 Type \\[dired-do-copy] to Copy files.
967 Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
968 Type \\[revert-buffer] to read all currently expanded directories again.
969 This retains all marks and hides subdirs again that were hidden before.
970 SPC and DEL can be used to move down and up by lines.
972 If dired ever gets confused, you can either type \\[revert-buffer] \
974 directories again, type \\[dired-do-redisplay] \
975 to relist a single or the marked files or a
976 subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
977 again for the directory tree.
979 Customization variables (rename this buffer and type \\[describe-variable] on each line
982 dired-listing-switches
983 dired-trivial-filenames
987 dired-keep-marker-rename
988 dired-keep-marker-copy
989 dired-keep-marker-hardlink
990 dired-keep-marker-symlink
992 Hooks (use \\[describe-variable] to see their documentation):
994 dired-before-readin-hook
995 dired-after-readin-hook
1001 ;; Not to be called interactively (e.g. dired-directory will be set
1002 ;; to default-directory, which is wrong with wildcards).
1003 (kill-all-local-variables)
1004 (use-local-map dired-mode-map)
1005 (dired-advertise) ; default-directory is already set
1006 (setq major-mode 'dired-mode
1008 ;; case-fold-search nil
1010 selective-display t ; for subdirectory hiding
1011 mode-line-buffer-identification '("Dired: %17b"))
1012 (set (make-local-variable 'revert-buffer-function)
1013 (function dired-revert))
1014 (set (make-local-variable 'page-delimiter)
1016 (set (make-local-variable 'dired-directory)
1017 (or dirname default-directory))
1018 ;; list-buffers uses this to display the dir being edited in this buffer.
1019 (set (make-local-variable 'list-buffers-directory)
1020 (expand-file-name dired-directory))
1021 (set (make-local-variable 'dired-actual-switches)
1022 (or switches dired-listing-switches))
1023 (set (make-local-variable 'font-lock-defaults) '(dired-font-lock-keywords t))
1024 (dired-sort-other dired-actual-switches t)
1025 (run-hooks 'dired-mode-hook))
1027 ;; Idiosyncratic dired commands that don't deal with marks.
1029 (defun dired-quit ()
1030 "Bury the current dired buffer."
1034 (defun dired-summary ()
1035 "Summarize basic Dired commands and show recent Dired errors."
1038 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
1040 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
1042 (defun dired-undo ()
1043 "Undo in a dired buffer.
1044 This doesn't recover lost files, it just undoes changes in the buffer itself.
1045 You can use it to recover marks, killed lines or subdirs.
1046 In the latter case, you have to do \\[dired-build-subdir-alist] to
1047 parse the buffer again."
1049 (let (buffer-read-only)
1052 (defun dired-next-line (arg)
1053 "Move down lines then position at filename.
1054 Optional prefix ARG says how many lines to move; default is one line."
1057 (dired-move-to-filename))
1059 (defun dired-previous-line (arg)
1060 "Move up lines then position at filename.
1061 Optional prefix ARG says how many lines to move; default is one line."
1064 (dired-move-to-filename))
1066 (defun dired-next-dirline (arg &optional opoint)
1067 "Goto ARG'th next directory file line."
1069 (or opoint (setq opoint (point)))
1071 (re-search-forward dired-re-dir nil t arg)
1073 (re-search-backward dired-re-dir nil t (- arg)))
1074 (dired-move-to-filename) ; user may type `i' or `f'
1076 (error "No more subdirectories")))
1078 (defun dired-prev-dirline (arg)
1079 "Goto ARG'th previous directory file line."
1081 (dired-next-dirline (- arg)))
1083 (defun dired-up-directory ()
1084 "Run dired on parent directory of current directory.
1085 Find the parent directory either in this buffer or another buffer.
1086 Creates a buffer if necessary."
1088 (let* ((dir (dired-current-directory))
1089 (up (file-name-directory (directory-file-name dir))))
1090 (or (dired-goto-file (directory-file-name dir))
1091 ;; Only try dired-goto-subdir if buffer has more than one dir.
1092 (and (cdr dired-subdir-alist)
1093 (dired-goto-subdir up))
1097 (dired-goto-file dir)))))
1099 ;; Force `f' rather than `e' in the mode doc:
1100 (defalias 'dired-advertised-find-file 'dired-find-file)
1101 (defun dired-find-file ()
1102 "In dired, visit the file or directory named on this line."
1104 (find-file (file-name-sans-versions (dired-get-filename) t)))
1106 (defun dired-mouse-find-file-other-window (event)
1107 "In dired, visit the file or directory name you click on."
1111 (set-buffer (window-buffer (posn-window (event-end event))))
1113 (goto-char (posn-point (event-end event)))
1114 (setq file (dired-get-filename))))
1115 (select-window (posn-window (event-end event)))
1116 (find-file-other-window (file-name-sans-versions file t))))
1118 (defun dired-view-file ()
1119 "In dired, examine a file in view mode, returning to dired when done.
1120 When file is a directory, show it in this buffer if it is inserted;
1121 otherwise, display it in another buffer."
1123 (if (file-directory-p (dired-get-filename))
1124 (or (and (cdr dired-subdir-alist)
1125 (dired-goto-subdir (dired-get-filename)))
1126 (dired (dired-get-filename)))
1127 (view-file (dired-get-filename))))
1129 (defun dired-find-file-other-window ()
1130 "In dired, visit this file or directory in another window."
1132 (find-file-other-window (file-name-sans-versions (dired-get-filename) t)))
1134 (defun dired-display-file ()
1135 "In dired, display this file or directory in another window."
1137 (let ((file (file-name-sans-versions (dired-get-filename) t)))
1138 (display-buffer (find-file-noselect file))))
1140 ;;; Functions for extracting and manipulating file names in dired buffers.
1142 (defun dired-get-filename (&optional localp no-error-if-not-filep)
1143 "In dired, return name of file mentioned on this line.
1144 Value returned normally includes the directory name.
1145 Optional arg LOCALP with value `no-dir' means don't include directory
1146 name in result. A value of t means construct name relative to
1147 `default-directory', which still may contain slashes if in a subdirectory.
1148 Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
1149 this line, otherwise an error occurs."
1150 (let (case-fold-search file p1 p2)
1152 (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
1153 (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
1154 ;; nil if no file on this line, but no-error-if-not-filep is t:
1155 (if (setq file (and p1 p2 (buffer-substring p1 p2)))
1157 ;; Get rid of the mouse-face property that file names have.
1158 (set-text-properties 0 (length file) nil file)
1159 ;; Unquote names quoted by ls or by dired-insert-directory.
1160 ;; Using read to unquote is much faster than substituting
1161 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
1165 ;; some ls -b don't escape quotes, argh!
1166 ;; This is not needed for GNU ls, though.
1167 (or (dired-string-replace-match
1168 "\\([^\\]\\)\"" file "\\1\\\\\"")
1171 (if (eq localp 'no-dir)
1173 (and file (concat (dired-current-directory localp) file)))))
1175 ;; Cloning replace-match to work on strings instead of in buffer:
1176 ;; The FIXEDCASE parameter of replace-match is not implemented.
1177 (defun dired-string-replace-match (regexp string newtext
1178 &optional literal global)
1179 "Replace first match of REGEXP in STRING with NEWTEXT.
1180 If it does not match, nil is returned instead of the new string.
1181 Optional arg LITERAL means to take NEWTEXT literally.
1182 Optional arg GLOBAL means to replace all matches."
1184 (let ((result "") (start 0) mb me)
1185 (while (string-match regexp string start)
1186 (setq mb (match-beginning 0)
1188 result (concat result
1189 (substring string start mb)
1192 (dired-expand-newtext string newtext)))
1194 (if mb ; matched at least once
1195 (concat result (substring string start))
1198 (if (not (string-match regexp string 0))
1200 (concat (substring string 0 (match-beginning 0))
1201 (if literal newtext (dired-expand-newtext string newtext))
1202 (substring string (match-end 0))))))
1204 (defun dired-make-absolute (file &optional dir)
1205 ;;"Convert FILE (a pathname relative to DIR) to an absolute pathname."
1206 ;; We can't always use expand-file-name as this would get rid of `.'
1207 ;; or expand in / instead default-directory if DIR=="".
1208 ;; This should be good enough for ange-ftp, but might easily be
1209 ;; redefined (for VMS?).
1210 ;; It should be reasonably fast, though, as it is called in
1211 ;; dired-get-filename.
1212 (concat (or dir default-directory) file))
1214 (defun dired-make-relative (file &optional dir no-error)
1215 ;;"Convert FILE (an absolute pathname) to a pathname relative to DIR.
1216 ;; Else error (unless NO-ERROR is non-nil, then FILE is returned unchanged)
1217 ;;DIR defaults to default-directory."
1218 ;; DIR must be file-name-as-directory, as with all directory args in
1220 (or dir (setq dir default-directory))
1221 ;; This case comes into play if default-directory is set to
1223 (if (and (> (length dir) 0) (= (aref dir 0) ?~))
1224 (setq dir (expand-file-name dir)))
1225 (if (string-match (concat "^" (regexp-quote dir)) file)
1226 (substring file (match-end 0))
1229 (error "%s: not in directory tree growing at %s" file dir))))
1231 ;;; Functions for finding the file name in a dired buffer line.
1233 (defvar dired-move-to-filename-regexp
1234 "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)[ ]+[0-9]+ [ 0-9][0-9][:0-9][0-9][ 0-9] "
1235 "Regular expression to match a month abbreviation followed by a number.")
1237 ;; Move to first char of filename on this line.
1238 ;; Returns position (point) or nil if no filename on this line."
1239 (defun dired-move-to-filename (&optional raise-error eol)
1240 ;; This is the UNIX version.
1241 (or eol (setq eol (progn (end-of-line) (point))))
1243 (if (re-search-forward dired-move-to-filename-regexp eol t)
1244 (goto-char (match-end 0))
1246 (error "No file on this line"))))
1248 (defun dired-move-to-end-of-filename (&optional no-error)
1249 ;; Assumes point is at beginning of filename,
1250 ;; thus the rwx bit re-search-backward below will succeed in *this*
1251 ;; line if at all. So, it should be called only after
1252 ;; (dired-move-to-filename t).
1253 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
1254 ;; This is the UNIX version.
1255 (let (opoint file-type executable symlink hidden case-fold-search used-F eol)
1256 ;; case-fold-search is nil now, so we can test for capital F:
1257 (setq used-F (string-match "F" dired-actual-switches)
1259 eol (save-excursion (end-of-line) (point))
1260 hidden (and selective-display
1261 (save-excursion (search-forward "\r" eol t))))
1264 (save-excursion;; Find out what kind of file this is:
1265 ;; Restrict perm bits to be non-blank,
1266 ;; otherwise this matches one char to early (looking backward):
1267 ;; "l---------" (some systems make symlinks that way)
1268 ;; "----------" (plain file with zero perms)
1269 (if (re-search-backward
1270 "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
1272 (setq file-type (char-after (match-beginning 1))
1273 symlink (eq file-type ?l)
1274 ;; Only with -F we need to know whether it's an executable
1278 "[xst]";; execute bit set anywhere?
1280 (buffer-substring (match-beginning 2)
1282 (buffer-substring (match-beginning 3)
1284 (buffer-substring (match-beginning 4)
1286 (or no-error (error "No file on this line"))))
1287 ;; Move point to end of name:
1289 (if (search-forward " ->" eol t)
1293 dired-ls-F-marks-symlinks
1294 (eq (preceding-char) ?@);; did ls really mark the link?
1295 (forward-char -1))))
1296 (goto-char eol);; else not a symbolic link
1297 ;; ls -lF marks dirs, sockets and executables with exactly one
1298 ;; trailing character. (Executable bits on symlinks ain't mean
1299 ;; a thing, even to ls, but we know it's not a symlink.)
1301 (or (memq file-type '(?d ?s))
1303 (forward-char -1))))
1305 (not (eq opoint (point)))
1307 (substitute-command-keys
1308 "File line is hidden, type \\[dired-hide-subdir] to unhide")
1309 "No file on this line")))
1310 (if (eq opoint (point))
1315 ;; Keeping Dired buffers in sync with the filesystem and with each other
1317 (defvar dired-buffers nil
1318 ;; Enlarged by dired-advertise
1319 ;; Queried by function dired-buffers-for-dir. When this detects a
1320 ;; killed buffer, it is removed from this list.
1321 "Alist of expanded directories and their associated dired buffers.")
1323 (defun dired-buffers-for-dir (dir)
1324 ;; Return a list of buffers that dired DIR (top level or in-situ subdir).
1325 ;; The list is in reverse order of buffer creation, most recent last.
1326 ;; As a side effect, killed dired buffers for DIR are removed from
1328 (setq dir (file-name-as-directory dir))
1329 (let ((alist dired-buffers) result elt buf)
1331 (setq elt (car alist)
1333 (if (buffer-name buf)
1334 (if (dired-in-this-tree dir (car elt))
1335 (if (assoc dir (save-excursion
1337 dired-subdir-alist))
1338 (setq result (cons buf result))))
1339 ;; else buffer is killed - clean up:
1340 (setq dired-buffers (delq elt dired-buffers)))
1341 (setq alist (cdr alist)))
1344 (defun dired-advertise ()
1345 ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
1346 ;; With wildcards we actually advertise too much.
1347 (let ((expanded-default (expand-file-name default-directory)))
1348 (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
1349 t ; we have already advertised ourselves
1351 (cons (cons expanded-default (current-buffer))
1354 (defun dired-unadvertise (dir)
1355 ;; Remove DIR from the buffer alist in variable dired-buffers.
1356 ;; This has the effect of removing any buffer whose main directory is DIR.
1357 ;; It does not affect buffers in which DIR is a subdir.
1358 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
1360 (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
1364 ;;; utility functions
1366 (defun dired-in-this-tree (file dir)
1367 ;;"Is FILE part of the directory tree starting at DIR?"
1368 (let (case-fold-search)
1369 (string-match (concat "^" (regexp-quote dir)) file)))
1371 (defun dired-normalize-subdir (dir)
1372 ;; Prepend default-directory to DIR if relative path name.
1373 ;; dired-get-filename must be able to make a valid filename from a
1374 ;; file and its directory DIR.
1375 (file-name-as-directory
1376 (if (file-name-absolute-p dir)
1378 (expand-file-name dir default-directory))))
1380 (defun dired-get-subdir ()
1381 ;;"Return the subdir name on this line, or nil if not on a headerline."
1382 ;; Look up in the alist whether this is a headerline.
1384 (let ((cur-dir (dired-current-directory)))
1385 (beginning-of-line) ; alist stores b-o-l positions
1386 (and (zerop (- (point)
1387 (dired-get-subdir-min (assoc cur-dir
1388 dired-subdir-alist))))
1391 ;(defun dired-get-subdir-min (elt)
1393 ;; can't use macro, must be redefinable for other alist format in dired-nstd.
1394 (defalias 'dired-get-subdir-min 'cdr)
1396 (defun dired-get-subdir-max (elt)
1398 (goto-char (dired-get-subdir-min elt))
1399 (dired-subdir-max)))
1401 (defun dired-clear-alist ()
1402 (while dired-subdir-alist
1403 (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
1404 (setq dired-subdir-alist (cdr dired-subdir-alist))))
1406 (defun dired-subdir-index (dir)
1407 ;; Return an index into alist for use with nth
1408 ;; for the sake of subdir moving commands.
1409 (let (found (index 0) (alist dired-subdir-alist))
1411 (if (string= dir (car (car alist)))
1412 (setq alist nil found t)
1413 (setq alist (cdr alist) index (1+ index))))
1414 (if found index nil)))
1416 (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
1417 "Go to next subdirectory, regardless of level."
1418 ;; Use 0 arg to go to this directory's header line.
1419 ;; NO-SKIP prevents moving to end of header line, returning whatever
1420 ;; position was found in dired-subdir-alist.
1422 (let ((this-dir (dired-current-directory))
1424 ;; nth with negative arg does not return nil but the first element
1425 (setq index (- (dired-subdir-index this-dir) arg))
1426 (setq pos (if (>= index 0)
1427 (dired-get-subdir-min (nth index dired-subdir-alist))))
1431 (or no-skip (skip-chars-forward "^\n\r"))
1433 (if no-error-if-not-found
1434 nil ; return nil if not found
1435 (error "%s directory" (if (> arg 0) "Last" "First"))))))
1437 (defun dired-build-subdir-alist ()
1438 "Build `dired-subdir-alist' by parsing the buffer.
1439 Returns the new value of the alist."
1444 (buffer-read-only nil)
1446 (goto-char (point-min))
1447 (setq dired-subdir-alist nil)
1448 (while (and (re-search-forward dired-subdir-regexp nil t)
1449 ;; Avoid taking a file name ending in a colon
1450 ;; as a subdir name.
1451 (not (save-excursion
1452 (goto-char (match-beginning 0))
1455 (save-match-data (looking-at dired-re-perms)))))
1457 (goto-char (match-beginning 1))
1459 (expand-file-name (buffer-substring (point) (match-end 1))))
1460 (delete-region (point) (match-end 1))
1461 (insert new-dir-name))
1462 (setq count (1+ count))
1463 (dired-alist-add-1 new-dir-name
1464 ;; Place a sub directory boundary between lines.
1466 (goto-char (match-beginning 0))
1470 (message "Buffer includes %d directories" count))
1471 ;; We don't need to sort it because it is in buffer order per
1472 ;; constructionem. Return new alist:
1473 dired-subdir-alist)))
1475 (defun dired-alist-add-1 (dir new-marker)
1476 ;; Add new DIR at NEW-MARKER. Don't sort.
1477 (setq dired-subdir-alist
1478 (cons (cons (dired-normalize-subdir dir) new-marker)
1479 dired-subdir-alist)))
1481 (defun dired-goto-next-nontrivial-file ()
1482 ;; Position point on first nontrivial file after point.
1483 (dired-goto-next-file);; so there is a file to compare with
1484 (if (stringp dired-trivial-filenames)
1485 (while (and (not (eobp))
1486 (string-match dired-trivial-filenames
1487 (file-name-nondirectory
1488 (or (dired-get-filename nil t) ""))))
1490 (dired-move-to-filename))))
1492 (defun dired-goto-next-file ()
1493 (let ((max (1- (dired-subdir-max))))
1494 (while (and (not (dired-move-to-filename)) (< (point) max))
1497 (defun dired-goto-file (file)
1498 "Go to file line of FILE in this dired buffer."
1499 ;; Return value of point on success, else nil.
1500 ;; FILE must be an absolute pathname.
1501 ;; Loses if FILE contains control chars like "\007" for which ls
1502 ;; either inserts "?" or "\\007" into the buffer, so we won't find
1503 ;; it in the buffer.
1505 (prog1 ; let push-mark display its message
1506 (list (expand-file-name
1507 (read-file-name "Goto file: "
1508 (dired-current-directory))))
1510 (setq file (directory-file-name file)) ; does no harm if no directory
1511 (let (found case-fold-search dir)
1512 (setq dir (or (file-name-directory file)
1513 (error "Need absolute pathname for %s" file)))
1515 ;; The hair here is to get the result of dired-goto-subdir
1516 ;; without really calling it if we don't have any subdirs.
1517 (if (if (string= dir (expand-file-name default-directory))
1518 (goto-char (point-min))
1519 (and (cdr dired-subdir-alist)
1520 (dired-goto-subdir dir)))
1521 (let ((base (file-name-nondirectory file))
1522 (boundary (dired-subdir-max)))
1523 (while (and (not found)
1524 ;; filenames are preceded by SPC, this makes
1525 ;; the search faster (e.g. for the filename "-"!).
1526 (search-forward (concat " " base) boundary 'move))
1527 ;; Match could have BASE just as initial substring or
1528 ;; or in permission bits or date or
1529 ;; not be a proper filename at all:
1530 (if (equal base (dired-get-filename 'no-dir t))
1531 ;; Must move to filename since an (actually
1532 ;; correct) match could have been elsewhere on the
1533 ;; ;; line (e.g. "-" would match somewhere in the
1534 ;; permission bits).
1535 (setq found (dired-move-to-filename))
1536 ;; If this isn't the right line, move forward to avoid
1537 ;; trying this line again.
1538 (forward-line 1))))))
1540 ;; return value of point (i.e., FOUND):
1541 (goto-char found))))
1543 (defun dired-initial-position (dirname)
1544 ;; Where point should go in a new listing of DIRNAME.
1545 ;; Point assumed at beginning of new subdir line.
1546 ;; You may redefine this function as you wish, e.g. like in dired-x.el.
1548 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
1550 ;; These are hooks which make tree dired work.
1551 ;; They are in this file because other parts of dired need to call them.
1552 ;; But they don't call the rest of tree dired unless there are subdirs loaded.
1554 ;; This function is called for each retrieved filename.
1555 ;; It could stand to be faster, though it's mostly function call
1556 ;; overhead. Avoiding the function call seems to save about 10% in
1557 ;; dired-get-filename. Make it a defsubst?
1558 (defun dired-current-directory (&optional localp)
1559 "Return the name of the subdirectory to which this line belongs.
1560 This returns a string with trailing slash, like `default-directory'.
1561 Optional argument means return a file name relative to `default-directory'."
1562 (let ((here (point))
1563 (alist (or dired-subdir-alist
1564 ;; probably because called in a non-dired buffer
1565 (error "No subdir-alist in %s" (current-buffer))))
1568 (setq elt (car alist)
1570 ;; use `<=' (not `<') as subdir line is part of subdir
1571 alist (if (<= (dired-get-subdir-min elt) here)
1575 (dired-make-relative dir default-directory)
1578 ;; Subdirs start at the beginning of their header lines and end just
1579 ;; before the beginning of the next header line (or end of buffer).
1581 (defun dired-subdir-max ()
1583 (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
1589 (defun dired-do-flagged-delete ()
1590 "In dired, delete the files flagged for deletion."
1592 (let* ((dired-marker-char dired-del-marker)
1593 (regexp (dired-marker-regexp))
1595 (if (save-excursion (goto-char (point-min))
1596 (re-search-forward regexp nil t))
1597 (dired-internal-do-deletions
1598 ;; this can't move point since ARG is nil
1599 (dired-map-over-marks (cons (dired-get-filename) (point))
1602 (message "(No deletions requested)"))))
1604 (defun dired-do-delete (&optional arg)
1605 "Delete all marked (or next ARG) files."
1606 ;; This is more consistent with the file marking feature than
1607 ;; dired-do-flagged-delete.
1609 (dired-internal-do-deletions
1610 ;; this may move point if ARG is an integer
1611 (dired-map-over-marks (cons (dired-get-filename) (point))
1615 (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
1617 (defun dired-internal-do-deletions (l arg)
1618 ;; L is an alist of files to delete, with their buffer positions.
1619 ;; ARG is the prefix arg.
1620 ;; Filenames are absolute (VMS needs this for logical search paths).
1621 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
1622 ;; That way as changes are made in the buffer they do not shift the
1623 ;; lines still to be changed, so the (point) values in L stay valid.
1624 ;; Also, for subdirs in natural order, a subdir's files are deleted
1625 ;; before the subdir itself - the other way around would not work.
1626 (let ((files (mapcar (function car) l))
1629 ;; canonicalize file list for pop up
1630 (setq files (nreverse (mapcar (function dired-make-relative) files)))
1631 (if (dired-mark-pop-up
1632 " *Deletions*" 'delete files dired-deletion-confirmer
1633 (format "Delete %s " (dired-mark-prompt arg files)))
1635 (let (failures);; files better be in reverse order for this loop!
1637 (goto-char (cdr (car l)))
1638 (let (buffer-read-only)
1640 (let ((fn (car (car l))))
1641 ;; This test is equivalent to
1642 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
1643 ;; but more efficient
1644 (if (eq t (car (file-attributes fn)))
1645 (delete-directory fn)
1647 ;; if we get here, removing worked
1648 (setq succ (1+ succ))
1649 (message "%s of %s deletions" succ count)
1650 (delete-region (progn (beginning-of-line) (point))
1651 (progn (forward-line 1) (point)))
1652 (dired-clean-up-after-deletion fn))
1653 (error;; catch errors from failed deletions
1654 (dired-log "%s\n" err)
1655 (setq failures (cons (car (car l)) failures)))))
1658 (message "%d deletion%s done" count (dired-plural-s count))
1660 (format "%d of %d deletion%s failed"
1661 (length failures) count
1662 (dired-plural-s count))
1664 (message "(No deletions performed)")))
1665 (dired-move-to-filename))
1667 ;; This is a separate function for the sake of dired-x.el.
1668 (defun dired-clean-up-after-deletion (fn)
1669 ;; Clean up after a deleted file or directory FN.
1670 (save-excursion (and (cdr dired-subdir-alist)
1671 (dired-goto-subdir fn)
1672 (dired-kill-subdir))))
1676 (defun dired-marker-regexp ()
1677 (concat "^" (regexp-quote (char-to-string dired-marker-char))))
1679 (defun dired-plural-s (count)
1680 (if (= 1 count) "" "s"))
1682 (defun dired-mark-prompt (arg files)
1683 ;; Return a string for use in a prompt, either the current file
1684 ;; name, or the marker and a count of marked files.
1685 (let ((count (length files)))
1688 ;; more than 1 file:
1691 ;; Perhaps this is nicer, but it also takes more screen space:
1692 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
1694 (format "[next %d files]" arg)
1695 (format "%c [%d files]" dired-marker-char count)))))
1697 (defun dired-pop-to-buffer (buf)
1698 ;; Pop up buffer BUF.
1699 ;; If dired-shrink-to-fit is t, make its window fit its contents.
1700 (if (not dired-shrink-to-fit)
1701 (pop-to-buffer (get-buffer-create buf))
1702 ;; let window shrink to fit:
1703 (let ((window (selected-window))
1705 (cond ;; if split-window-threshold is enabled, use the largest window
1706 ((and (> (window-height (setq w2 (get-largest-window)))
1707 split-height-threshold)
1708 (= (frame-width) (window-width w2)))
1710 ;; if the least-recently-used window is big enough, use it
1711 ((and (> (window-height (setq w2 (get-lru-window)))
1712 (* 2 window-min-height))
1713 (= (frame-width) (window-width w2)))
1717 (goto-char (point-max))
1718 (skip-chars-backward "\n\r\t ")
1719 (setq target-lines (count-lines (point-min) (point)))
1720 ;; Don't forget to count the last line.
1722 (setq target-lines (1+ target-lines))))
1723 (if (<= (window-height window) (* 2 window-min-height))
1724 ;; At this point, every window on the frame is too small to split.
1725 (setq w2 (display-buffer buf))
1726 (setq w2 (split-window window
1727 (max window-min-height
1728 (- (window-height window)
1729 (1+ (max window-min-height target-lines)))))))
1730 (set-window-buffer w2 buf)
1731 (if (< (1- (window-height w2)) target-lines)
1734 (enlarge-window (- target-lines (1- (window-height w2))))))
1735 (set-window-start w2 1)
1738 (defvar dired-no-confirm nil
1739 ;; "If non-nil, list of symbols for commands dired should not confirm.
1740 ;;It can be a sublist of
1742 ;; '(byte-compile chgrp chmod chown compress copy delete hardlink load
1743 ;; move print shell symlink uncompress)"
1746 (defun dired-mark-pop-up (bufname op-symbol files function &rest args)
1747 ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
1748 ;;Return FUNCTION's result on ARGS after popping up a window (in a buffer
1749 ;;named BUFNAME, nil gives \" *Marked Files*\") showing the marked
1750 ;;files. Uses function `dired-pop-to-buffer' to do that.
1751 ;; FUNCTION should not manipulate files.
1752 ;; It should only read input (an argument or confirmation).
1753 ;;The window is not shown if there is just one file or
1754 ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
1755 ;;FILES is the list of marked files."
1756 (or bufname (setq bufname " *Marked Files*"))
1757 (if (or (memq op-symbol dired-no-confirm)
1758 (= (length files) 1))
1759 (apply function args)
1761 (set-buffer (get-buffer-create bufname))
1763 (dired-format-columns-of-files files)
1764 (remove-text-properties (point-min) (point-max) '(mouse-face)))
1765 (save-window-excursion
1766 (dired-pop-to-buffer bufname)
1767 (apply function args))))
1769 (defun dired-format-columns-of-files (files)
1770 ;; Files should be in forward order for this loop.
1771 ;; i.e., (car files) = first file in buffer.
1772 ;; Returns the number of lines used.
1773 (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files))))
1774 (width (- (window-width (selected-window)) 2))
1775 (columns (max 1 (/ width maxlen)))
1776 (nfiles (length files))
1777 (rows (+ (/ nfiles columns)
1778 (if (zerop (% nfiles columns)) 0 1)))
1781 (setq files (nconc (copy-sequence files) ; fill up with empty fns
1782 (make-list (- (* columns rows) nfiles) "")))
1783 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
1785 (while (< i columns)
1786 (indent-to (* i maxlen))
1787 (insert (car files))
1788 (setq files (nthcdr rows files)
1796 ;; Commands to mark or flag file(s) at or near current line.
1798 (defun dired-repeat-over-lines (arg function)
1799 ;; This version skips non-file lines.
1800 (let ((pos (make-marker)))
1802 (while (and (> arg 0) (not (eobp)))
1805 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
1808 (move-marker pos (1+ (point))))
1809 (save-excursion (funcall function))
1810 ;; Advance to the next line--actually, to the line that *was* next.
1811 ;; (If FUNCTION inserted some new lines in between, skip them.)
1813 (while (and (< arg 0) (not (bobp)))
1816 (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
1818 (save-excursion (funcall function)))
1819 (move-marker pos nil)
1820 (dired-move-to-filename)))
1822 (defun dired-between-files ()
1823 ;; Point must be at beginning of line
1824 ;; Should be equivalent to (save-excursion (not (dired-move-to-filename)))
1825 ;; but is about 1.5..2.0 times as fast. (Actually that's not worth it)
1826 (or (looking-at "^$\\|^. *$\\|^. total\\|^. wildcard")
1827 (and (looking-at dired-subdir-regexp)
1828 (save-excursion (not (dired-move-to-filename))))))
1830 (defun dired-next-marked-file (arg &optional wrap opoint)
1831 "Move to the next marked file, wrapping around the end of the buffer."
1832 (interactive "p\np")
1833 (or opoint (setq opoint (point)));; return to where interactively started
1835 (re-search-forward dired-re-mark nil t arg)
1837 (re-search-backward dired-re-mark nil t (- arg)))
1838 (dired-move-to-filename)
1842 (error "No next marked file"))
1843 (message "(Wraparound for next marked file)")
1844 (goto-char (if (> arg 0) (point-min) (point-max)))
1845 (dired-next-marked-file arg nil opoint))))
1847 (defun dired-prev-marked-file (arg &optional wrap)
1848 "Move to the previous marked file, wrapping around the end of the buffer."
1849 (interactive "p\np")
1850 (dired-next-marked-file (- arg) wrap))
1852 (defun dired-file-marker (file)
1853 ;; Return FILE's marker, or nil if unmarked.
1855 (and (dired-goto-file file)
1858 (if (not (equal ?\040 (following-char)))
1859 (following-char))))))
1861 (defun dired-mark-files-in-region (start end)
1862 (let (buffer-read-only)
1864 (error "start > end"))
1865 (goto-char start) ; assumed at beginning of line
1866 (while (< (point) end)
1867 ;; Skip subdir line and following garbage like the `total' line:
1868 (while (and (< (point) end) (dired-between-files))
1870 (if (and (not (looking-at dired-re-dot))
1871 (dired-get-filename nil t))
1874 (insert dired-marker-char)))
1877 (defun dired-mark (arg)
1878 "Mark the current (or next ARG) files.
1879 If on a subdir headerline, mark all its files except `.' and `..'.
1881 Use \\[dired-unmark-all-files] to remove all marks
1882 and \\[dired-unmark] on a subdir to remove the marks in
1885 (if (dired-get-subdir)
1886 (save-excursion (dired-mark-subdir-files))
1887 (let (buffer-read-only)
1888 (dired-repeat-over-lines
1889 (prefix-numeric-value arg)
1890 (function (lambda () (delete-char 1) (insert dired-marker-char)))))))
1892 (defun dired-unmark (arg)
1893 "Unmark the current (or next ARG) files.
1894 If looking at a subdir, unmark all its files except `.' and `..'."
1896 (let ((dired-marker-char ?\040))
1899 (defun dired-flag-file-deletion (arg)
1900 "In dired, flag the current line's file for deletion.
1901 With prefix arg, repeat over several lines.
1903 If on a subdir headerline, mark all its files except `.' and `..'."
1905 (let ((dired-marker-char dired-del-marker))
1908 (defun dired-unmark-backward (arg)
1909 "In dired, move up lines and remove deletion flag there.
1910 Optional prefix ARG says how many lines to unflag; default is one line."
1912 (dired-unmark (- arg)))
1914 ;;; Commands to mark or flag files based on their characteristics or names.
1916 (defvar dired-regexp-history nil
1917 "History list of regular expressions used in Dired commands.")
1919 (defun dired-read-regexp (prompt)
1920 (read-from-minibuffer prompt nil nil nil 'dired-regexp-history))
1922 (defun dired-mark-files-regexp (regexp &optional marker-char)
1923 "Mark all files matching REGEXP for use in later commands.
1924 A prefix argument means to unmark them instead.
1925 `.' and `..' are never marked.
1927 REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
1928 object files--just `.o' will mark more than you might think."
1930 (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
1931 " files (regexp): "))
1932 (if current-prefix-arg ?\040)))
1933 (let ((dired-marker-char (or marker-char dired-marker-char)))
1935 (and (not (looking-at dired-re-dot))
1936 (not (eolp)) ; empty line
1937 (let ((fn (dired-get-filename nil t)))
1938 (and fn (string-match regexp (file-name-nondirectory fn)))))
1941 (defun dired-flag-files-regexp (regexp)
1942 "In dired, flag all files containing the specified REGEXP for deletion.
1943 The match is against the non-directory part of the filename. Use `^'
1944 and `$' to anchor matches. Exclude subdirs by hiding them.
1945 `.' and `..' are never flagged."
1946 (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
1947 (dired-mark-files-regexp regexp dired-del-marker))
1949 (defun dired-mark-symlinks (unflag-p)
1950 "Mark all symbolic links.
1951 With prefix argument, unflag all those files."
1953 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
1954 (dired-mark-if (looking-at dired-re-sym) "symbolic link")))
1956 (defun dired-mark-directories (unflag-p)
1957 "Mark all directory file lines except `.' and `..'.
1958 With prefix argument, unflag all those files."
1960 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
1961 (dired-mark-if (and (looking-at dired-re-dir)
1962 (not (looking-at dired-re-dot)))
1965 (defun dired-mark-executables (unflag-p)
1966 "Mark all executable files.
1967 With prefix argument, unflag all those files."
1969 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
1970 (dired-mark-if (looking-at dired-re-exe) "executable file")))
1972 ;; dired-x.el has a dired-mark-sexp interactive command: mark
1973 ;; files for which PREDICATE returns non-nil.
1975 (defun dired-flag-auto-save-files (&optional unflag-p)
1976 "Flag for deletion files whose names suggest they are auto save files.
1977 A prefix argument says to unflag those files instead."
1979 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
1981 ;; It is less than general to check for # here,
1982 ;; but it's the only way this runs fast enough.
1983 (and (save-excursion (end-of-line)
1985 (eq (preceding-char) ?#)
1986 ;; Handle executables in case of -F option.
1987 ;; We need not worry about the other kinds
1988 ;; of markings that -F makes, since they won't
1989 ;; appear on real auto-save files.
1990 (if (eq (preceding-char) ?*)
1993 (eq (preceding-char) ?#)))))
1994 (not (looking-at dired-re-dir))
1995 (let ((fn (dired-get-filename t t)))
1996 (if fn (auto-save-file-name-p
1997 (file-name-nondirectory fn)))))
2000 (defun dired-flag-backup-files (&optional unflag-p)
2001 "Flag all backup files (names ending with `~') for deletion.
2002 With prefix argument, unflag these files."
2004 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
2006 ;; It is less than general to check for ~ here,
2007 ;; but it's the only way this runs fast enough.
2008 (and (save-excursion (end-of-line)
2010 (eq (preceding-char) ?~)
2011 ;; Handle executables in case of -F option.
2012 ;; We need not worry about the other kinds
2013 ;; of markings that -F makes, since they won't
2014 ;; appear on real backup files.
2015 (if (eq (preceding-char) ?*)
2018 (eq (preceding-char) ?~)))))
2019 (not (looking-at dired-re-dir))
2020 (let ((fn (dired-get-filename t t)))
2021 (if fn (backup-file-name-p fn))))
2024 (defun dired-change-marks (&optional old new)
2025 "Change all OLD marks to NEW marks.
2026 OLD and NEW are both characters used to mark files."
2028 (let* ((cursor-in-echo-area t)
2029 (old (progn (message "Change (old mark): ") (read-char)))
2030 (new (progn (message "Change %c marks to (new mark): " old)
2033 (if (or (eq old ?\r) (eq new ?\r))
2035 (let ((string (format "\n%c" old))
2038 (goto-char (point-min))
2039 (while (search-forward string nil t)
2040 (subst-char-in-region (match-beginning 0)
2041 (match-end 0) old new))))))
2043 (defun dired-unmark-all-files-no-query ()
2044 "Remove all marks from all files in the Dired buffer."
2046 (dired-unmark-all-files ?\r))
2048 (defun dired-unmark-all-files (mark &optional arg)
2049 "Remove a specific mark (or any mark) from every file.
2050 After this command, type the mark character to remove,
2051 or type RET to remove all marks.
2052 With prefix arg, query for each marked file.
2053 Type \\[help-command] at that time for help."
2054 (interactive "cRemove marks (RET means all): \nP")
2057 buffer-read-only case-fold-search query
2058 (string (format "\n%c" mark))
2060 Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
2061 `!' to unmark all remaining files with no more questions."))
2062 (goto-char (point-min))
2063 (while (if (eq mark ?\r)
2064 (re-search-forward dired-re-mark nil t)
2065 (search-forward string nil t))
2067 (dired-query 'query "Unmark file `%s'? "
2068 (dired-get-filename t)))
2069 (progn (subst-char-in-region (1- (point)) (point)
2070 (preceding-char) ?\ )
2071 (setq count (1+ count)))))
2072 (message (if (= count 1) "1 mark removed"
2076 ;; Logging failures operating on files, and showing the results.
2078 (defvar dired-log-buffer "*Dired log*")
2081 "Pop up a buffer with error log output from Dired.
2082 A group of errors from a single command ends with a formfeed.
2083 Thus, use \\[backward-page] to find the beginning of a group of errors."
2085 (if (get-buffer dired-log-buffer)
2086 (let ((owindow (selected-window))
2087 (window (display-buffer (get-buffer dired-log-buffer))))
2090 (select-window window)
2091 (goto-char (point-max))
2093 (select-window owindow)))))
2095 (defun dired-log (log &rest args)
2096 ;; Log a message or the contents of a buffer.
2097 ;; If LOG is a string and there are more args, it is formatted with
2098 ;; those ARGS. Usually the LOG string ends with a \n.
2099 ;; End each bunch of errors with (dired-log t): this inserts
2100 ;; current time and buffer, and a \f (formfeed).
2101 (let ((obuf (current-buffer)))
2102 (unwind-protect ; want to move point
2104 (set-buffer (get-buffer-create dired-log-buffer))
2105 (goto-char (point-max))
2106 (let (buffer-read-only)
2107 (cond ((stringp log)
2109 (apply (function format) log args)
2112 (insert-buffer log))
2114 (insert "\n\t" (current-time-string)
2115 "\tBuffer `" (buffer-name obuf) "'\n\f\n")))))
2116 (set-buffer obuf))))
2118 (defun dired-log-summary (string failures)
2119 (message (if failures "%s--type ? for details (%s)"
2120 "%s--type ? for details")
2122 ;; Log a summary describing a bunch of errors.
2123 (dired-log (concat "\n" string))
2128 ;; Most ls can only sort by name or by date (with -t), nothing else.
2129 ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
2130 ;; So anything that does not contain these is sort "by name".
2132 (defvar dired-ls-sorting-switches "SXU"
2133 "String of `ls' switches (single letters) except `t' that influence sorting.")
2135 (defvar dired-sort-by-date-regexp
2136 (concat "^-[^" dired-ls-sorting-switches
2137 "]*t[^" dired-ls-sorting-switches "]*$")
2138 "Regexp recognized by dired to set `by date' mode.")
2140 (defvar dired-sort-by-name-regexp
2141 (concat "^-[^t" dired-ls-sorting-switches "]+$")
2142 "Regexp recognized by dired to set `by name' mode.")
2144 (defun dired-sort-set-modeline ()
2145 ;; Set modeline display according to dired-actual-switches.
2146 ;; Modeline display of "by name" or "by date" guarantees the user a
2147 ;; match with the corresponding regexps. Non-matching switches are
2150 (let (case-fold-search)
2151 (cond ((string-match dired-sort-by-name-regexp dired-actual-switches)
2153 ((string-match dired-sort-by-date-regexp dired-actual-switches)
2156 (concat "Dired " dired-actual-switches)))))
2157 (force-mode-line-update))
2159 (defun dired-sort-toggle-or-edit (&optional arg)
2160 "Toggle between sort by date/name and refresh the dired buffer.
2161 With a prefix argument you can edit the current listing switches instead."
2165 (read-string "ls switches (must contain -l): " dired-actual-switches))
2166 (dired-sort-toggle)))
2168 (defun dired-sort-toggle ()
2169 ;; Toggle between sort by date/name. Reverts the buffer.
2170 (setq dired-actual-switches
2171 (let (case-fold-search)
2174 (dired-replace-in-string (concat "[-lt"
2175 dired-ls-sorting-switches "]")
2177 dired-actual-switches)
2178 (if (string-match (concat "[t" dired-ls-sorting-switches "]")
2179 dired-actual-switches)
2182 (dired-sort-set-modeline)
2185 (defun dired-replace-in-string (regexp newtext string)
2186 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
2187 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
2188 (let ((result "") (start 0) mb me)
2189 (while (string-match regexp string start)
2190 (setq mb (match-beginning 0)
2192 result (concat result (substring string start mb) newtext)
2194 (concat result (substring string start))))
2196 (defun dired-sort-other (switches &optional no-revert)
2197 ;; Specify new ls SWITCHES for current dired buffer. Values matching
2198 ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
2199 ;; minor mode accordingly, others appear literally in the mode line.
2200 ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
2201 (setq dired-actual-switches switches)
2202 (dired-sort-set-modeline)
2203 (or no-revert (revert-buffer)))
2205 ;; To make this file smaller, the less common commands
2206 ;; go in a separate file. But autoload them here
2207 ;; to make the separation invisible.
2209 (autoload 'dired-diff "dired-aux"
2210 "Compare file at point with file FILE using `diff'.
2211 FILE defaults to the file at the mark.
2212 The prompted-for file is the first file given to `diff'."
2215 (autoload 'dired-backup-diff "dired-aux"
2216 "Diff this file with its backup file or vice versa.
2217 Uses the latest backup, if there are several numerical backups.
2218 If this file is a backup, diff it with its original.
2219 The backup file is the first file given to `diff'."
2222 (autoload 'dired-clean-directory "dired-aux"
2223 "Flag numerical backups for deletion.
2224 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
2225 Positive prefix arg KEEP overrides `dired-kept-versions';
2226 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
2228 To clear the flags on these files, you can use \\[dired-flag-backup-files]
2229 with a prefix argument."
2232 (autoload 'dired-do-chmod "dired-aux"
2233 "Change the mode of the marked (or next ARG) files.
2234 This calls chmod, thus symbolic modes like `g+w' are allowed."
2237 (autoload 'dired-do-chgrp "dired-aux"
2238 "Change the group of the marked (or next ARG) files."
2241 (autoload 'dired-do-chown "dired-aux"
2242 "Change the owner of the marked (or next ARG) files."
2245 (autoload 'dired-do-print "dired-aux"
2246 "Print the marked (or next ARG) files.
2247 Uses the shell command coming from variables `lpr-command' and
2248 `lpr-switches' as default."
2251 (autoload 'dired-do-shell-command "dired-aux"
2252 "Run a shell command COMMAND on the marked files.
2253 If no files are marked or a specific numeric prefix arg is given,
2254 the next ARG files are used. Just \\[universal-argument] means the current file.
2255 The prompt mentions the file(s) or the marker, as appropriate.
2257 If there is output, it goes to a separate buffer.
2259 Normally the command is run on each file individually.
2260 However, if there is a `*' in the command then it is run
2261 just once with the entire file list substituted there.
2263 No automatic redisplay of dired buffers is attempted, as there's no
2264 telling what files the command may have changed. Type
2265 \\[dired-do-redisplay] to redisplay the marked files.
2267 The shell command has the top level directory as working directory, so
2268 output files usually are created there instead of in a subdir."
2271 (autoload 'dired-do-kill-lines "dired-aux"
2272 "Kill all marked lines (not the files).
2273 With a prefix arg, kill all lines not marked or flagged."
2276 (autoload 'dired-do-compress "dired-aux"
2277 "Compress or uncompress marked (or next ARG) files."
2280 (autoload 'dired-do-byte-compile "dired-aux"
2281 "Byte compile marked (or next ARG) Emacs Lisp files."
2284 (autoload 'dired-do-load "dired-aux"
2285 "Load the marked (or next ARG) Emacs Lisp files."
2288 (autoload 'dired-do-redisplay "dired-aux"
2289 "Redisplay all marked (or next ARG) files.
2290 If on a subdir line, redisplay that subdirectory. In that case,
2291 a prefix arg lets you edit the `ls' switches used for the new listing."
2294 (autoload 'dired-string-replace-match "dired-aux"
2295 "Replace first match of REGEXP in STRING with NEWTEXT.
2296 If it does not match, nil is returned instead of the new string.
2297 Optional arg LITERAL means to take NEWTEXT literally.
2298 Optional arg GLOBAL means to replace all matches."
2301 (autoload 'dired-create-directory "dired-aux"
2302 "Create a directory called DIRECTORY."
2305 (autoload 'dired-do-copy "dired-aux"
2306 "Copy all marked (or next ARG) files, or copy the current file.
2307 Thus, a zero prefix argument copies nothing. But it toggles the
2308 variable `dired-copy-preserve-time' (which see)."
2311 (autoload 'dired-do-symlink "dired-aux"
2312 "Make symbolic links to current file or all marked (or next ARG) files.
2313 When operating on just the current file, you specify the new name.
2314 When operating on multiple or marked files, you specify a directory
2315 and new symbolic links are made in that directory
2316 with the same names that the files currently have."
2319 (autoload 'dired-do-hardlink "dired-aux"
2320 "Add names (hard links) current file or all marked (or next ARG) files.
2321 When operating on just the current file, you specify the new name.
2322 When operating on multiple or marked files, you specify a directory
2323 and new hard links are made in that directory
2324 with the same names that the files currently have."
2327 (autoload 'dired-do-rename "dired-aux"
2328 "Rename current file or all marked (or next ARG) files.
2329 When renaming just the current file, you specify the new name.
2330 When renaming multiple or marked files, you specify a directory."
2333 (autoload 'dired-do-rename-regexp "dired-aux"
2334 "Rename marked files containing REGEXP to NEWNAME.
2335 As each match is found, the user must type a character saying
2336 what to do with it. For directions, type \\[help-command] at that time.
2337 NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
2338 REGEXP defaults to the last regexp used.
2339 With a zero prefix arg, renaming by regexp affects the complete
2340 pathname - usually only the non-directory part of file names is used
2344 (autoload 'dired-do-copy-regexp "dired-aux"
2345 "Copy all marked files containing REGEXP to NEWNAME.
2346 See function `dired-rename-regexp' for more info."
2349 (autoload 'dired-do-hardlink-regexp "dired-aux"
2350 "Hardlink all marked files containing REGEXP to NEWNAME.
2351 See function `dired-rename-regexp' for more info."
2354 (autoload 'dired-do-symlink-regexp "dired-aux"
2355 "Symlink all marked files containing REGEXP to NEWNAME.
2356 See function `dired-rename-regexp' for more info."
2359 (autoload 'dired-upcase "dired-aux"
2360 "Rename all marked (or next ARG) files to upper case."
2363 (autoload 'dired-downcase "dired-aux"
2364 "Rename all marked (or next ARG) files to lower case."
2367 (autoload 'dired-maybe-insert-subdir "dired-aux"
2368 "Insert this subdirectory into the same dired buffer.
2369 If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
2370 else inserts it at its natural place (as `ls -lR' would have done).
2371 With a prefix arg, you may edit the ls switches used for this listing.
2372 You can add `R' to the switches to expand the whole tree starting at
2374 This function takes some pains to conform to `ls -lR' output."
2377 (autoload 'dired-next-subdir "dired-aux"
2378 "Go to next subdirectory, regardless of level."
2381 (autoload 'dired-prev-subdir "dired-aux"
2382 "Go to previous subdirectory, regardless of level.
2383 When called interactively and not on a subdir line, go to this subdir's line."
2386 (autoload 'dired-goto-subdir "dired-aux"
2387 "Go to end of header line of DIR in this dired buffer.
2388 Return value of point on success, otherwise return nil.
2389 The next char is either \\n, or \\r if DIR is hidden."
2392 (autoload 'dired-mark-subdir-files "dired-aux"
2393 "Mark all files except `.' and `..'."
2396 (autoload 'dired-kill-subdir "dired-aux"
2397 "Remove all lines of current subdirectory.
2398 Lower levels are unaffected."
2401 (autoload 'dired-tree-up "dired-aux"
2402 "Go up ARG levels in the dired tree."
2405 (autoload 'dired-tree-down "dired-aux"
2406 "Go down in the dired tree."
2409 (autoload 'dired-hide-subdir "dired-aux"
2410 "Hide or unhide the current subdirectory and move to next directory.
2411 Optional prefix arg is a repeat factor.
2412 Use \\[dired-hide-all] to (un)hide all directories."
2415 (autoload 'dired-hide-all "dired-aux"
2416 "Hide all subdirectories, leaving only their header lines.
2417 If there is already something hidden, make everything visible again.
2418 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2421 (if (eq system-type 'vax-vms)
2426 (run-hooks 'dired-load-hook) ; for your customizations
2428 ;;; dired.el ends here