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 rest 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 mode)
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 ;; Optional argument MODE is passed to dired-find-buffer-nocreate,
407 (let* ((dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
408 ;; The following line used to use dir-or-list.
409 ;; That never found an existing buffer, in the case
410 ;; where it is a list.
411 (buffer (dired-find-buffer-nocreate dirname mode))
412 ;; note that buffer already is in dired-mode, if found
413 (new-buffer-p (not buffer))
414 (old-buf (current-buffer)))
416 (let ((default-major-mode 'fundamental-mode))
417 ;; We don't want default-major-mode to run hooks and set auto-fill
418 ;; or whatever, now that dired-mode does not
419 ;; kill-all-local-variables any longer.
420 (setq buffer (create-file-buffer (directory-file-name dirname)))))
422 (if (not new-buffer-p) ; existing buffer ...
423 (cond (switches ; ... but new switches
424 ;; file list may have changed
425 (if (consp dir-or-list)
426 (setq dired-directory dir-or-list))
427 ;; this calls dired-revert
428 (dired-sort-other switches))
429 ;; If directory has changed on disk, offer to revert.
430 ((if (let ((attributes (file-attributes dirname))
431 (modtime (visited-file-modtime)))
433 (not (eq (car attributes) t))
434 (and (= (car (nth 5 attributes)) (car modtime))
435 (= (nth 1 (nth 5 attributes)) (cdr modtime)))))
438 (substitute-command-keys
439 "Directory has changed on disk; type \\[revert-buffer] to update Dired")))))
441 (setq default-directory
442 (if (file-directory-p dirname)
444 (file-name-directory dirname)))
445 (or switches (setq switches dired-listing-switches))
446 (dired-mode dirname switches)
447 (if mode (funcall mode))
448 ;; default-directory and dired-actual-switches are set now
449 ;; (buffer-local), so we can call dired-readin:
452 (progn (dired-readin dir-or-list buffer)
454 ;; dired-readin can fail if parent directories are inaccessible.
455 ;; Don't leave an empty buffer around in that case.
456 (if failed (kill-buffer buffer))))
457 ;; No need to narrow since the whole buffer contains just
458 ;; dired-readin's output, nothing else. The hook can
459 ;; successfully use dired functions (e.g. dired-get-filename)
460 ;; as the subdir-alist has been built in dired-readin.
461 (run-hooks 'dired-after-readin-hook)
462 (goto-char (point-min))
463 (dired-initial-position dirname))
467 (defun dired-find-buffer-nocreate (dirname &optional mode)
468 ;; This differs from dired-buffers-for-dir in that it does not consider
469 ;; subdirs of default-directory and searches for the first match only.
470 ;; Also, the major mode must be MODE.
471 (let (found (blist dired-buffers)) ; was (buffer-list)
472 (or mode (setq mode 'dired-mode))
474 (if (null (buffer-name (cdr (car blist))))
475 (setq blist (cdr blist))
477 (set-buffer (cdr (car blist)))
478 (if (and (eq major-mode mode)
479 (if (consp dired-directory)
480 (equal (car dired-directory) dirname)
481 (equal dired-directory dirname)))
482 (setq found (cdr (car blist))
484 (setq blist (cdr blist))))))
488 ;; Read in a new dired buffer
490 ;; dired-readin differs from dired-insert-subdir in that it accepts
491 ;; wildcards, erases the buffer, and builds the subdir-alist anew
492 ;; (including making it buffer-local and clearing it first).
493 (defun dired-readin (dir-or-list buffer)
494 ;; default-directory and dired-actual-switches must be buffer-local
495 ;; and initialized by now.
496 ;; Thus we can test (equal default-directory dirname) instead of
497 ;; (file-directory-p dirname) and save a filesystem transaction.
498 ;; Also, we can run this hook which may want to modify the switches
499 ;; based on default-directory, e.g. with ange-ftp to a SysV host
500 ;; where ls won't understand -Al switches.
502 (if (consp dir-or-list)
503 (setq dirname (car dir-or-list))
504 (setq dirname dir-or-list))
505 (setq dirname (expand-file-name dirname))
506 (if (consp dir-or-list)
507 (setq dir-or-list (cons dirname (cdr dir-or-list))))
508 (run-hooks 'dired-before-readin-hook)
510 (message "Reading directory %s..." dirname)
512 (let (buffer-read-only (failed t))
515 (dired-readin-insert dir-or-list)
516 (indent-rigidly (point-min) (point-max) 2)
517 ;; We need this to make the root dir have a header line as all
518 ;; other subdirs have:
519 (goto-char (point-min))
520 (dired-insert-headerline default-directory)
521 ;; can't run dired-after-readin-hook here, it may depend on the subdir
524 (message "Reading directory %s...done" dirname)
525 ;; Must first make alist buffer local and set it to nil because
526 ;; dired-build-subdir-alist will call dired-clear-alist first
527 (set (make-local-variable 'dired-subdir-alist) nil)
528 (dired-build-subdir-alist)
529 (let ((attributes (file-attributes dirname)))
530 (if (eq (car attributes) t)
531 (set-visited-file-modtime (nth 5 attributes))))
532 (set-buffer-modified-p nil))))
534 ;; Subroutines of dired-readin
536 (defun dired-readin-insert (dir-or-list)
537 ;; Just insert listing for the passed-in directory or
538 ;; directory-and-file list, assuming a clean buffer.
540 (if (consp dir-or-list)
541 (setq dirname (car dir-or-list))
542 (setq dirname dir-or-list))
543 ;; Expand before comparing in case one or both have been abbreviated.
544 (if (and (equal (expand-file-name default-directory)
545 (expand-file-name dirname))
546 (not (consp dir-or-list)))
547 ;; If we are reading a whole single directory...
548 (dired-insert-directory dir-or-list dired-actual-switches nil t)
549 (if (not (file-readable-p
550 (directory-file-name (file-name-directory dirname))))
551 (error "Directory %s inaccessible or nonexistent" dirname)
552 ;; Else assume it contains wildcards,
553 ;; unless it is an explicit list of files.
554 (dired-insert-directory dir-or-list dired-actual-switches
555 (not (listp dir-or-list)))
556 (save-excursion ;; insert wildcard instead of total line:
557 (goto-char (point-min))
558 (insert "wildcard " (file-name-nondirectory dirname) "\n"))))))
560 (defun dired-insert-directory (dir-or-list switches &optional wildcard full-p)
561 ;; Do the right thing whether dir-or-list is atomic or not. If it is,
562 ;; inset all files listed in the cdr (the car is the passed-in directory
564 (let ((opoint (point))
565 (process-environment (copy-sequence process-environment))
567 ;; This makes sure that month names come out in English
568 ;; so we can find the start of the file name.
569 (setenv "LC_ALL" "C")
570 (if (consp dir-or-list)
571 ;; In this case, use the file names in the cdr
572 ;; exactly as originally given to dired-noselect.
574 (function (lambda (x) (insert-directory x switches wildcard full-p)))
576 ;; Expand the file name here because it may have been abbreviated
577 ;; in dired-noselect.
578 (insert-directory (expand-file-name dir-or-list) switches wildcard full-p))
579 ;; Quote certain characters, unless ls quoted them for us.
580 (if (not (string-match "b" dired-actual-switches))
582 (setq end (point-marker))
584 (while (search-forward "\\" end t)
585 (replace-match "\\\\" nil t))
587 (while (search-forward "\^m" end t)
588 (replace-match "\\015" nil t))
589 (set-marker end nil)))
590 (dired-insert-set-properties opoint (point)))
591 (setq dired-directory dir-or-list))
593 ;; Make the file names highlight when the mouse is on them.
594 (defun dired-insert-set-properties (beg end)
597 (while (< (point) end)
599 (if (dired-move-to-filename)
600 (put-text-property (point)
602 (dired-move-to-end-of-filename)
604 'mouse-face 'highlight))
608 (defun dired-insert-headerline (dir);; also used by dired-insert-subdir
609 ;; Insert DIR's headerline with no trailing slash, exactly like ls
610 ;; would, and put cursor where dired-build-subdir-alist puts subdir
612 (save-excursion (insert " " (directory-file-name dir) ":\n")))
615 ;; Reverting a dired buffer
617 (defun dired-revert (&optional arg noconfirm)
618 ;; Reread the dired buffer. Must also be called after
619 ;; dired-actual-switches have changed.
620 ;; Should not fail even on completely garbaged buffers.
621 ;; Preserves old cursor, marks/flags, hidden-p.
622 (widen) ; just in case user narrowed
623 (let ((opoint (point))
624 (ofile (dired-get-filename nil t))
625 (mark-alist nil) ; save marked files
626 (hidden-subdirs (dired-remember-hidden))
627 (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
628 (case-fold-search nil) ; we check for upper case ls flags
630 (goto-char (point-min))
631 (setq mark-alist;; only after dired-remember-hidden since this unhides:
632 (dired-remember-marks (point-min) (point-max)))
633 ;; treat top level dir extra (it may contain wildcards)
635 (if (consp dired-directory) (car dired-directory) dired-directory))
636 (dired-readin dired-directory (current-buffer))
637 (let ((dired-after-readin-hook nil))
638 ;; don't run that hook for each subdir...
639 (dired-insert-old-subdirs old-subdir-alist))
640 (dired-mark-remembered mark-alist) ; mark files that were marked
641 ;; ... run the hook for the whole buffer, and only after markers
642 ;; have been reinserted (else omitting in dired-x would omit marked files)
643 (run-hooks 'dired-after-readin-hook) ; no need to narrow
644 (or (and ofile (dired-goto-file ofile)) ; move cursor to where it
645 (goto-char opoint)) ; was before
646 (dired-move-to-filename)
647 (save-excursion ; hide subdirs that were hidden
648 (mapcar (function (lambda (dir)
649 (if (dired-goto-subdir dir)
650 (dired-hide-subdir 1))))
652 ;; outside of the let scope
653 ;;; Might as well not override the user if the user changed this.
654 ;;; (setq buffer-read-only t)
657 ;; Subroutines of dired-revert
658 ;; Some of these are also used when inserting subdirs.
660 (defun dired-remember-marks (beg end)
661 ;; Return alist of files and their marks, from BEG to END.
662 (if selective-display ; must unhide to make this work.
663 (let (buffer-read-only)
664 (subst-char-in-region beg end ?\r ?\n)))
668 (while (re-search-forward dired-re-mark end t)
669 (if (setq fil (dired-get-filename nil t))
670 (setq chr (preceding-char)
671 alist (cons (cons fil chr) alist)))))
674 ;; Mark all files remembered in ALIST.
675 ;; Each element of ALIST looks like (FILE . MARKERCHAR).
676 (defun dired-mark-remembered (alist)
679 (setq elt (car alist)
683 (if (dired-goto-file fil)
689 ;; Return a list of names of subdirs currently hidden.
690 (defun dired-remember-hidden ()
691 (let ((l dired-subdir-alist) dir pos result)
693 (setq dir (car (car l))
697 (skip-chars-forward "^\r\n")
698 (if (eq (following-char) ?\r)
699 (setq result (cons dir result))))
702 ;; Try to insert all subdirs that were displayed before,
703 ;; according to the former subdir alist OLD-SUBDIR-ALIST.
704 (defun dired-insert-old-subdirs (old-subdir-alist)
705 (or (string-match "R" dired-actual-switches)
707 (while old-subdir-alist
708 (setq elt (car old-subdir-alist)
709 old-subdir-alist (cdr old-subdir-alist)
714 (dired-insert-subdir dir))
717 ;; Remove directory DIR from any directory cache.
718 (defun dired-uncache (dir)
719 (let ((handler (find-file-name-handler dir 'dired-uncache)))
721 (funcall handler 'dired-uncache dir))))
723 ;; dired mode key bindings and initialization
725 (defvar dired-mode-map nil "Local keymap for dired-mode buffers.")
728 ;; This looks ugly when substitute-command-keys uses C-d instead d:
729 ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
731 (let ((map (make-keymap)))
732 (suppress-keymap map)
733 (define-key map [mouse-2] 'dired-mouse-find-file-other-window)
734 ;; Commands to mark or flag certain categories of files
735 (define-key map "#" 'dired-flag-auto-save-files)
736 (define-key map "*" 'dired-mark-executables)
737 (define-key map "." 'dired-clean-directory)
738 (define-key map "/" 'dired-mark-directories)
739 (define-key map "@" 'dired-mark-symlinks)
740 (define-key map "~" 'dired-flag-backup-files)
741 ;; Upper case keys (except !) for operating on the marked files
742 (define-key map "A" 'dired-do-search)
743 (define-key map "C" 'dired-do-copy)
744 (define-key map "B" 'dired-do-byte-compile)
745 (define-key map "D" 'dired-do-delete)
746 (define-key map "G" 'dired-do-chgrp)
747 (define-key map "H" 'dired-do-hardlink)
748 (define-key map "L" 'dired-do-load)
749 (define-key map "M" 'dired-do-chmod)
750 (define-key map "O" 'dired-do-chown)
751 (define-key map "P" 'dired-do-print)
752 (define-key map "Q" 'dired-do-query-replace)
753 (define-key map "R" 'dired-do-rename)
754 (define-key map "S" 'dired-do-symlink)
755 (define-key map "X" 'dired-do-shell-command)
756 (define-key map "Z" 'dired-do-compress)
757 (define-key map "!" 'dired-do-shell-command)
758 ;; Comparison commands
759 (define-key map "=" 'dired-diff)
760 (define-key map "\M-=" 'dired-backup-diff)
761 ;; Tree Dired commands
762 (define-key map "\M-\C-?" 'dired-unmark-all-files)
763 (define-key map "\M-\C-d" 'dired-tree-down)
764 (define-key map "\M-\C-u" 'dired-tree-up)
765 (define-key map "\M-\C-n" 'dired-next-subdir)
766 (define-key map "\M-\C-p" 'dired-prev-subdir)
767 ;; move to marked files
768 (define-key map "\M-{" 'dired-prev-marked-file)
769 (define-key map "\M-}" 'dired-next-marked-file)
770 ;; Make all regexp commands share a `%' prefix:
771 ;; We used to get to the submap via a symbol dired-regexp-prefix,
772 ;; but that seems to serve little purpose, and copy-keymap
773 ;; does a better job without it.
774 (define-key map "%" nil)
775 (define-key map "%u" 'dired-upcase)
776 (define-key map "%l" 'dired-downcase)
777 (define-key map "%d" 'dired-flag-files-regexp)
778 (define-key map "%m" 'dired-mark-files-regexp)
779 (define-key map "%r" 'dired-do-rename-regexp)
780 (define-key map "%C" 'dired-do-copy-regexp)
781 (define-key map "%H" 'dired-do-hardlink-regexp)
782 (define-key map "%R" 'dired-do-rename-regexp)
783 (define-key map "%S" 'dired-do-symlink-regexp)
784 ;; Lower keys for commands not operating on all the marked files
785 (define-key map "c" 'dired-change-marks)
786 (define-key map "d" 'dired-flag-file-deletion)
787 (define-key map "e" 'dired-find-file)
788 (define-key map "f" 'dired-find-file)
789 (define-key map "\C-m" 'dired-advertised-find-file)
790 (define-key map "g" 'revert-buffer)
791 (define-key map "h" 'describe-mode)
792 (define-key map "i" 'dired-maybe-insert-subdir)
793 (define-key map "k" 'dired-do-kill-lines)
794 (define-key map "l" 'dired-do-redisplay)
795 (define-key map "m" 'dired-mark)
796 (define-key map "n" 'dired-next-line)
797 (define-key map "o" 'dired-find-file-other-window)
798 (define-key map "\C-o" 'dired-display-file)
799 (define-key map "p" 'dired-previous-line)
800 (define-key map "q" 'dired-quit)
801 (define-key map "s" 'dired-sort-toggle-or-edit)
802 (define-key map "u" 'dired-unmark)
803 (define-key map "v" 'dired-view-file)
804 (define-key map "x" 'dired-do-flagged-delete)
805 (define-key map "+" 'dired-create-directory)
807 (define-key map "<" 'dired-prev-dirline)
808 (define-key map ">" 'dired-next-dirline)
809 (define-key map "^" 'dired-up-directory)
810 (define-key map " " 'dired-next-line)
811 (define-key map "\C-n" 'dired-next-line)
812 (define-key map "\C-p" 'dired-previous-line)
813 (define-key map [down] 'dired-next-line)
814 (define-key map [up] 'dired-previous-line)
816 (define-key map "$" 'dired-hide-subdir)
817 (define-key map "\M-$" 'dired-hide-all)
819 (define-key map "?" 'dired-summary)
820 (define-key map "\177" 'dired-unmark-backward)
821 (define-key map "\C-_" 'dired-undo)
822 (define-key map "\C-xu" 'dired-undo)
824 ;; Make menu bar items.
826 ;; Get rid of the Edit menu bar item to save space.
827 (define-key map [menu-bar edit] 'undefined)
829 (define-key map [menu-bar subdir]
830 (cons "Subdir" (make-sparse-keymap "Subdir")))
832 (define-key map [menu-bar subdir hide-all]
833 '("Hide All" . dired-hide-all))
834 (define-key map [menu-bar subdir hide-subdir]
835 '("Hide Subdir" . dired-hide-subdir))
836 (define-key map [menu-bar subdir tree-down]
837 '("Tree Down" . dired-tree-down))
838 (define-key map [menu-bar subdir tree-up]
839 '("Tree Up" . dired-tree-up))
840 (define-key map [menu-bar subdir up]
841 '("Up Directory" . dired-up-directory))
842 (define-key map [menu-bar subdir prev-subdir]
843 '("Prev Subdir" . dired-prev-subdir))
844 (define-key map [menu-bar subdir next-subdir]
845 '("Next Subdir" . dired-next-subdir))
846 (define-key map [menu-bar subdir prev-dirline]
847 '("Prev Dirline" . dired-prev-dirline))
848 (define-key map [menu-bar subdir next-dirline]
849 '("Next Dirline" . dired-next-dirline))
850 (define-key map [menu-bar subdir insert]
851 '("Insert This Subdir" . dired-maybe-insert-subdir))
853 (define-key map [menu-bar immediate]
854 (cons "Immediate" (make-sparse-keymap "Immediate")))
856 (define-key map [menu-bar immediate backup-diff]
857 '("Compare with Backup" . dired-backup-diff))
858 (define-key map [menu-bar immediate diff]
859 '("Diff" . dired-diff))
860 (define-key map [menu-bar immediate view]
861 '("View This File" . dired-view-file))
862 (define-key map [menu-bar immediate display]
863 '("Display in Other Window" . dired-display-file))
864 (define-key map [menu-bar immediate find-file-other-window]
865 '("Find in Other Window" . dired-find-file-other-window))
866 (define-key map [menu-bar immediate find-file]
867 '("Find This File" . dired-find-file))
868 (define-key map [menu-bar immediate create-directory]
869 '("Create Directory..." . dired-create-directory))
871 (define-key map [menu-bar regexp]
872 (cons "Regexp" (make-sparse-keymap "Regexp")))
874 (define-key map [menu-bar regexp downcase]
875 '("Downcase" . dired-downcase))
876 (define-key map [menu-bar regexp upcase]
877 '("Upcase" . dired-upcase))
878 (define-key map [menu-bar regexp hardlink]
879 '("Hardlink..." . dired-do-hardlink-regexp))
880 (define-key map [menu-bar regexp symlink]
881 '("Symlink..." . dired-do-symlink-regexp))
882 (define-key map [menu-bar regexp rename]
883 '("Rename..." . dired-do-rename-regexp))
884 (define-key map [menu-bar regexp copy]
885 '("Copy..." . dired-do-copy-regexp))
886 (define-key map [menu-bar regexp flag]
887 '("Flag..." . dired-flag-files-regexp))
888 (define-key map [menu-bar regexp mark]
889 '("Mark..." . dired-mark-files-regexp))
891 (define-key map [menu-bar mark]
892 (cons "Mark" (make-sparse-keymap "Mark")))
894 (define-key map [menu-bar mark prev]
895 '("Previous Marked" . dired-prev-marked-file))
896 (define-key map [menu-bar mark next]
897 '("Next Marked" . dired-next-marked-file))
898 (define-key map [menu-bar mark marks]
899 '("Change Marks..." . dired-change-marks))
900 (define-key map [menu-bar mark unmark-all]
901 '("Unmark All" . dired-unmark-all-files-no-query))
902 (define-key map [menu-bar mark symlinks]
903 '("Mark Symlinks" . dired-mark-symlinks))
904 (define-key map [menu-bar mark directories]
905 '("Mark Directories" . dired-mark-directories))
906 (define-key map [menu-bar mark directory]
907 '("Mark Old Backups" . dired-clean-directory))
908 (define-key map [menu-bar mark executables]
909 '("Mark Executables" . dired-mark-executables))
910 (define-key map [menu-bar mark backup-files]
911 '("Flag Backup Files" . dired-flag-backup-files))
912 (define-key map [menu-bar mark auto-save-files]
913 '("Flag Auto-save Files" . dired-flag-auto-save-files))
914 (define-key map [menu-bar mark deletion]
915 '("Flag" . dired-flag-file-deletion))
916 (define-key map [menu-bar mark unmark]
917 '("Unmark" . dired-unmark))
918 (define-key map [menu-bar mark mark]
919 '("Mark" . dired-mark))
921 (define-key map [menu-bar operate]
922 (cons "Operate" (make-sparse-keymap "Operate")))
924 (define-key map [menu-bar operate query-replace]
925 '("Query Replace in Files..." . dired-do-query-replace))
926 (define-key map [menu-bar operate search]
927 '("Search Files..." . dired-do-search))
928 (define-key map [menu-bar operate chown]
929 '("Change Owner..." . dired-do-chown))
930 (define-key map [menu-bar operate chgrp]
931 '("Change Group..." . dired-do-chgrp))
932 (define-key map [menu-bar operate chmod]
933 '("Change Mode..." . dired-do-chmod))
934 (define-key map [menu-bar operate load]
935 '("Load" . dired-do-load))
936 (define-key map [menu-bar operate compile]
937 '("Byte-compile" . dired-do-byte-compile))
938 (define-key map [menu-bar operate compress]
939 '("Compress" . dired-do-compress))
940 (define-key map [menu-bar operate print]
941 '("Print" . dired-do-print))
942 (define-key map [menu-bar operate hardlink]
943 '("Hardlink to..." . dired-do-hardlink))
944 (define-key map [menu-bar operate symlink]
945 '("Symlink to..." . dired-do-symlink))
946 (define-key map [menu-bar operate command]
947 '("Shell Command..." . dired-do-shell-command))
948 (define-key map [menu-bar operate delete]
949 '("Delete" . dired-do-delete))
950 (define-key map [menu-bar operate rename]
951 '("Rename to..." . dired-do-rename))
952 (define-key map [menu-bar operate copy]
953 '("Copy to..." . dired-do-copy))
955 (setq dired-mode-map map)))
957 ;; Dired mode is suitable only for specially formatted data.
958 (put 'dired-mode 'mode-class 'special)
960 (defun dired-mode (&optional dirname switches)
962 Mode for \"editing\" directory listings.
963 In dired, you are \"editing\" a list of the files in a directory and
964 \(optionally) its subdirectories, in the format of `ls -lR'.
965 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
966 \"Editing\" means that you can run shell commands on files, visit,
967 compress, load or byte-compile them, change their file attributes
968 and insert subdirectories into the same buffer. You can \"mark\"
969 files for later commands or \"flag\" them for deletion, either file
970 by file or all files matching certain criteria.
971 You can move using the usual cursor motion commands.\\<dired-mode-map>
972 Letters no longer insert themselves. Digits are prefix arguments.
973 Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
974 Type \\[dired-mark] to Mark a file or subdirectory for later commands.
975 Most commands operate on the marked files and use the current file
976 if no files are marked. Use a numeric prefix argument to operate on
977 the next ARG (or previous -ARG if ARG<0) files, or just `1'
978 to operate on the current file only. Prefix arguments override marks.
979 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
980 to see why something went wrong.
981 Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
982 Type \\[dired-unmark-backward] to back up one line and unflag.
983 Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
984 Type \\[dired-advertised-find-file] to Find the current line's file
985 (or dired it in another buffer, if it is a directory).
986 Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
987 Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
988 Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
989 Type \\[dired-do-copy] to Copy files.
990 Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
991 Type \\[revert-buffer] to read all currently expanded directories again.
992 This retains all marks and hides subdirs again that were hidden before.
993 SPC and DEL can be used to move down and up by lines.
995 If dired ever gets confused, you can either type \\[revert-buffer] \
997 directories again, type \\[dired-do-redisplay] \
998 to relist a single or the marked files or a
999 subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
1000 again for the directory tree.
1002 Customization variables (rename this buffer and type \\[describe-variable] on each line
1005 dired-listing-switches
1006 dired-trivial-filenames
1010 dired-keep-marker-rename
1011 dired-keep-marker-copy
1012 dired-keep-marker-hardlink
1013 dired-keep-marker-symlink
1015 Hooks (use \\[describe-variable] to see their documentation):
1017 dired-before-readin-hook
1018 dired-after-readin-hook
1024 ;; Not to be called interactively (e.g. dired-directory will be set
1025 ;; to default-directory, which is wrong with wildcards).
1026 (kill-all-local-variables)
1027 (use-local-map dired-mode-map)
1028 (dired-advertise) ; default-directory is already set
1029 (setq major-mode 'dired-mode
1031 ;; case-fold-search nil
1033 selective-display t ; for subdirectory hiding
1034 mode-line-buffer-identification '("Dired: %17b"))
1035 (set (make-local-variable 'revert-buffer-function)
1036 (function dired-revert))
1037 (set (make-local-variable 'page-delimiter)
1039 (set (make-local-variable 'dired-directory)
1040 (or dirname default-directory))
1041 ;; list-buffers uses this to display the dir being edited in this buffer.
1042 (set (make-local-variable 'list-buffers-directory)
1043 (expand-file-name dired-directory))
1044 (set (make-local-variable 'dired-actual-switches)
1045 (or switches dired-listing-switches))
1046 (set (make-local-variable 'font-lock-defaults) '(dired-font-lock-keywords t))
1047 (dired-sort-other dired-actual-switches t)
1048 (run-hooks 'dired-mode-hook))
1050 ;; Idiosyncratic dired commands that don't deal with marks.
1052 (defun dired-quit ()
1053 "Bury the current dired buffer."
1057 (defun dired-summary ()
1058 "Summarize basic Dired commands and show recent Dired errors."
1061 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
1063 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
1065 (defun dired-undo ()
1066 "Undo in a dired buffer.
1067 This doesn't recover lost files, it just undoes changes in the buffer itself.
1068 You can use it to recover marks, killed lines or subdirs.
1069 In the latter case, you have to do \\[dired-build-subdir-alist] to
1070 parse the buffer again."
1072 (let (buffer-read-only)
1075 (defun dired-next-line (arg)
1076 "Move down lines then position at filename.
1077 Optional prefix ARG says how many lines to move; default is one line."
1080 (dired-move-to-filename))
1082 (defun dired-previous-line (arg)
1083 "Move up lines then position at filename.
1084 Optional prefix ARG says how many lines to move; default is one line."
1087 (dired-move-to-filename))
1089 (defun dired-next-dirline (arg &optional opoint)
1090 "Goto ARG'th next directory file line."
1092 (or opoint (setq opoint (point)))
1094 (re-search-forward dired-re-dir nil t arg)
1096 (re-search-backward dired-re-dir nil t (- arg)))
1097 (dired-move-to-filename) ; user may type `i' or `f'
1099 (error "No more subdirectories")))
1101 (defun dired-prev-dirline (arg)
1102 "Goto ARG'th previous directory file line."
1104 (dired-next-dirline (- arg)))
1106 (defun dired-up-directory (&optional other-window)
1107 "Run dired on parent directory of current directory.
1108 Find the parent directory either in this buffer or another buffer.
1109 Creates a buffer if necessary."
1111 (let* ((dir (dired-current-directory))
1112 (up (file-name-directory (directory-file-name dir))))
1113 (or (dired-goto-file (directory-file-name dir))
1114 ;; Only try dired-goto-subdir if buffer has more than one dir.
1115 (and (cdr dired-subdir-alist)
1116 (dired-goto-subdir up))
1119 (dired-other-window up)
1121 (dired-goto-file dir)))))
1123 ;; Force `f' rather than `e' in the mode doc:
1124 (defalias 'dired-advertised-find-file 'dired-find-file)
1125 (defun dired-find-file ()
1126 "In dired, visit the file or directory named on this line."
1128 (find-file (file-name-sans-versions (dired-get-filename) t)))
1130 (defun dired-mouse-find-file-other-window (event)
1131 "In dired, visit the file or directory name you click on."
1135 (set-buffer (window-buffer (posn-window (event-end event))))
1137 (goto-char (posn-point (event-end event)))
1138 (setq file (dired-get-filename))))
1139 (select-window (posn-window (event-end event)))
1140 (find-file-other-window (file-name-sans-versions file t))))
1142 (defun dired-view-file ()
1143 "In dired, examine a file in view mode, returning to dired when done.
1144 When file is a directory, show it in this buffer if it is inserted;
1145 otherwise, display it in another buffer."
1147 (if (file-directory-p (dired-get-filename))
1148 (or (and (cdr dired-subdir-alist)
1149 (dired-goto-subdir (dired-get-filename)))
1150 (dired (dired-get-filename)))
1151 (view-file (dired-get-filename))))
1153 (defun dired-find-file-other-window ()
1154 "In dired, visit this file or directory in another window."
1156 (find-file-other-window (file-name-sans-versions (dired-get-filename) t)))
1158 (defun dired-display-file ()
1159 "In dired, display this file or directory in another window."
1161 (let ((file (file-name-sans-versions (dired-get-filename) t)))
1162 (display-buffer (find-file-noselect file))))
1164 ;;; Functions for extracting and manipulating file names in dired buffers.
1166 (defun dired-get-filename (&optional localp no-error-if-not-filep)
1167 "In dired, return name of file mentioned on this line.
1168 Value returned normally includes the directory name.
1169 Optional arg LOCALP with value `no-dir' means don't include directory
1170 name in result. A value of t means construct name relative to
1171 `default-directory', which still may contain slashes if in a subdirectory.
1172 Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
1173 this line, otherwise an error occurs."
1174 (let (case-fold-search file p1 p2)
1176 (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
1177 (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
1178 ;; nil if no file on this line, but no-error-if-not-filep is t:
1179 (if (setq file (and p1 p2 (buffer-substring p1 p2)))
1181 ;; Get rid of the mouse-face property that file names have.
1182 (set-text-properties 0 (length file) nil file)
1183 ;; Unquote names quoted by ls or by dired-insert-directory.
1184 ;; Using read to unquote is much faster than substituting
1185 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
1189 ;; some ls -b don't escape quotes, argh!
1190 ;; This is not needed for GNU ls, though.
1191 (or (dired-string-replace-match
1192 "\\([^\\]\\)\"" file "\\1\\\\\"")
1195 (if (eq localp 'no-dir)
1197 (and file (concat (dired-current-directory localp) file)))))
1199 (defun dired-string-replace-match (regexp string newtext
1200 &optional literal global)
1201 "Replace first match of REGEXP in STRING with NEWTEXT.
1202 If it does not match, nil is returned instead of the new string.
1203 Optional arg LITERAL means to take NEWTEXT literally.
1204 Optional arg GLOBAL means to replace all matches."
1207 (while (string-match regexp string start)
1208 (let ((from-end (- (length string) (match-end 0))))
1209 (setq string (replace-match newtext t literal string))
1210 (setq start (- (length string) from-end))))
1212 (if (not (string-match regexp string 0))
1214 (replace-match newtext t literal string))))
1216 (defun dired-make-absolute (file &optional dir)
1217 ;;"Convert FILE (a pathname relative to DIR) to an absolute pathname."
1218 ;; We can't always use expand-file-name as this would get rid of `.'
1219 ;; or expand in / instead default-directory if DIR=="".
1220 ;; This should be good enough for ange-ftp, but might easily be
1221 ;; redefined (for VMS?).
1222 ;; It should be reasonably fast, though, as it is called in
1223 ;; dired-get-filename.
1224 (concat (or dir default-directory) file))
1226 (defun dired-make-relative (file &optional dir no-error)
1227 ;;"Convert FILE (an absolute pathname) to a pathname relative to DIR.
1228 ;; Else error (unless NO-ERROR is non-nil, then FILE is returned unchanged)
1229 ;;DIR defaults to default-directory."
1230 ;; DIR must be file-name-as-directory, as with all directory args in
1232 (or dir (setq dir default-directory))
1233 ;; This case comes into play if default-directory is set to
1235 (if (and (> (length dir) 0) (= (aref dir 0) ?~))
1236 (setq dir (expand-file-name dir)))
1237 (if (string-match (concat "^" (regexp-quote dir)) file)
1238 (substring file (match-end 0))
1241 (error "%s: not in directory tree growing at %s" file dir))))
1243 ;;; Functions for finding the file name in a dired buffer line.
1245 (defvar dired-move-to-filename-regexp
1246 "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)[ ]+[0-9]+ [ 0-9][0-9][:0-9][0-9][ 0-9] "
1247 "Regular expression to match a month abbreviation followed by a number.")
1249 ;; Move to first char of filename on this line.
1250 ;; Returns position (point) or nil if no filename on this line."
1251 (defun dired-move-to-filename (&optional raise-error eol)
1252 ;; This is the UNIX version.
1253 (or eol (setq eol (progn (end-of-line) (point))))
1255 (if (re-search-forward dired-move-to-filename-regexp eol t)
1256 (goto-char (match-end 0))
1258 (error "No file on this line"))))
1260 (defun dired-move-to-end-of-filename (&optional no-error)
1261 ;; Assumes point is at beginning of filename,
1262 ;; thus the rwx bit re-search-backward below will succeed in *this*
1263 ;; line if at all. So, it should be called only after
1264 ;; (dired-move-to-filename t).
1265 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
1266 ;; This is the UNIX version.
1267 (let (opoint file-type executable symlink hidden case-fold-search used-F eol)
1268 ;; case-fold-search is nil now, so we can test for capital F:
1269 (setq used-F (string-match "F" dired-actual-switches)
1271 eol (save-excursion (end-of-line) (point))
1272 hidden (and selective-display
1273 (save-excursion (search-forward "\r" eol t))))
1276 (save-excursion;; Find out what kind of file this is:
1277 ;; Restrict perm bits to be non-blank,
1278 ;; otherwise this matches one char to early (looking backward):
1279 ;; "l---------" (some systems make symlinks that way)
1280 ;; "----------" (plain file with zero perms)
1281 (if (re-search-backward
1282 "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
1284 (setq file-type (char-after (match-beginning 1))
1285 symlink (eq file-type ?l)
1286 ;; Only with -F we need to know whether it's an executable
1290 "[xst]";; execute bit set anywhere?
1292 (buffer-substring (match-beginning 2)
1294 (buffer-substring (match-beginning 3)
1296 (buffer-substring (match-beginning 4)
1298 (or no-error (error "No file on this line"))))
1299 ;; Move point to end of name:
1301 (if (search-forward " ->" eol t)
1305 dired-ls-F-marks-symlinks
1306 (eq (preceding-char) ?@);; did ls really mark the link?
1307 (forward-char -1))))
1308 (goto-char eol);; else not a symbolic link
1309 ;; ls -lF marks dirs, sockets and executables with exactly one
1310 ;; trailing character. (Executable bits on symlinks ain't mean
1311 ;; a thing, even to ls, but we know it's not a symlink.)
1313 (or (memq file-type '(?d ?s))
1315 (forward-char -1))))
1317 (not (eq opoint (point)))
1319 (substitute-command-keys
1320 "File line is hidden, type \\[dired-hide-subdir] to unhide")
1321 "No file on this line")))
1322 (if (eq opoint (point))
1327 ;; Keeping Dired buffers in sync with the filesystem and with each other
1329 (defvar dired-buffers nil
1330 ;; Enlarged by dired-advertise
1331 ;; Queried by function dired-buffers-for-dir. When this detects a
1332 ;; killed buffer, it is removed from this list.
1333 "Alist of expanded directories and their associated dired buffers.")
1335 (defun dired-buffers-for-dir (dir)
1336 ;; Return a list of buffers that dired DIR (top level or in-situ subdir).
1337 ;; The list is in reverse order of buffer creation, most recent last.
1338 ;; As a side effect, killed dired buffers for DIR are removed from
1340 (setq dir (file-name-as-directory dir))
1341 (let ((alist dired-buffers) result elt buf)
1343 (setq elt (car alist)
1345 (if (buffer-name buf)
1346 (if (dired-in-this-tree dir (car elt))
1347 (if (assoc dir (save-excursion
1349 dired-subdir-alist))
1350 (setq result (cons buf result))))
1351 ;; else buffer is killed - clean up:
1352 (setq dired-buffers (delq elt dired-buffers)))
1353 (setq alist (cdr alist)))
1356 (defun dired-advertise ()
1357 ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
1358 ;; With wildcards we actually advertise too much.
1359 (let ((expanded-default (expand-file-name default-directory)))
1360 (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
1361 t ; we have already advertised ourselves
1363 (cons (cons expanded-default (current-buffer))
1366 (defun dired-unadvertise (dir)
1367 ;; Remove DIR from the buffer alist in variable dired-buffers.
1368 ;; This has the effect of removing any buffer whose main directory is DIR.
1369 ;; It does not affect buffers in which DIR is a subdir.
1370 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
1372 (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
1376 ;;; utility functions
1378 (defun dired-in-this-tree (file dir)
1379 ;;"Is FILE part of the directory tree starting at DIR?"
1380 (let (case-fold-search)
1381 (string-match (concat "^" (regexp-quote dir)) file)))
1383 (defun dired-normalize-subdir (dir)
1384 ;; Prepend default-directory to DIR if relative path name.
1385 ;; dired-get-filename must be able to make a valid filename from a
1386 ;; file and its directory DIR.
1387 (file-name-as-directory
1388 (if (file-name-absolute-p dir)
1390 (expand-file-name dir default-directory))))
1392 (defun dired-get-subdir ()
1393 ;;"Return the subdir name on this line, or nil if not on a headerline."
1394 ;; Look up in the alist whether this is a headerline.
1396 (let ((cur-dir (dired-current-directory)))
1397 (beginning-of-line) ; alist stores b-o-l positions
1398 (and (zerop (- (point)
1399 (dired-get-subdir-min (assoc cur-dir
1400 dired-subdir-alist))))
1403 ;(defun dired-get-subdir-min (elt)
1405 ;; can't use macro, must be redefinable for other alist format in dired-nstd.
1406 (defalias 'dired-get-subdir-min 'cdr)
1408 (defun dired-get-subdir-max (elt)
1410 (goto-char (dired-get-subdir-min elt))
1411 (dired-subdir-max)))
1413 (defun dired-clear-alist ()
1414 (while dired-subdir-alist
1415 (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
1416 (setq dired-subdir-alist (cdr dired-subdir-alist))))
1418 (defun dired-subdir-index (dir)
1419 ;; Return an index into alist for use with nth
1420 ;; for the sake of subdir moving commands.
1421 (let (found (index 0) (alist dired-subdir-alist))
1423 (if (string= dir (car (car alist)))
1424 (setq alist nil found t)
1425 (setq alist (cdr alist) index (1+ index))))
1426 (if found index nil)))
1428 (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
1429 "Go to next subdirectory, regardless of level."
1430 ;; Use 0 arg to go to this directory's header line.
1431 ;; NO-SKIP prevents moving to end of header line, returning whatever
1432 ;; position was found in dired-subdir-alist.
1434 (let ((this-dir (dired-current-directory))
1436 ;; nth with negative arg does not return nil but the first element
1437 (setq index (- (dired-subdir-index this-dir) arg))
1438 (setq pos (if (>= index 0)
1439 (dired-get-subdir-min (nth index dired-subdir-alist))))
1443 (or no-skip (skip-chars-forward "^\n\r"))
1445 (if no-error-if-not-found
1446 nil ; return nil if not found
1447 (error "%s directory" (if (> arg 0) "Last" "First"))))))
1449 (defun dired-build-subdir-alist ()
1450 "Build `dired-subdir-alist' by parsing the buffer.
1451 Returns the new value of the alist."
1456 (buffer-read-only nil)
1458 (goto-char (point-min))
1459 (setq dired-subdir-alist nil)
1460 (while (and (re-search-forward dired-subdir-regexp nil t)
1461 ;; Avoid taking a file name ending in a colon
1462 ;; as a subdir name.
1463 (not (save-excursion
1464 (goto-char (match-beginning 0))
1467 (save-match-data (looking-at dired-re-perms)))))
1469 (goto-char (match-beginning 1))
1471 (expand-file-name (buffer-substring (point) (match-end 1))))
1472 (delete-region (point) (match-end 1))
1473 (insert new-dir-name))
1474 (setq count (1+ count))
1475 (dired-alist-add-1 new-dir-name
1476 ;; Place a sub directory boundary between lines.
1478 (goto-char (match-beginning 0))
1482 (message "Buffer includes %d directories" count))
1483 ;; We don't need to sort it because it is in buffer order per
1484 ;; constructionem. Return new alist:
1485 dired-subdir-alist)))
1487 (defun dired-alist-add-1 (dir new-marker)
1488 ;; Add new DIR at NEW-MARKER. Don't sort.
1489 (setq dired-subdir-alist
1490 (cons (cons (dired-normalize-subdir dir) new-marker)
1491 dired-subdir-alist)))
1493 (defun dired-goto-next-nontrivial-file ()
1494 ;; Position point on first nontrivial file after point.
1495 (dired-goto-next-file);; so there is a file to compare with
1496 (if (stringp dired-trivial-filenames)
1497 (while (and (not (eobp))
1498 (string-match dired-trivial-filenames
1499 (file-name-nondirectory
1500 (or (dired-get-filename nil t) ""))))
1502 (dired-move-to-filename))))
1504 (defun dired-goto-next-file ()
1505 (let ((max (1- (dired-subdir-max))))
1506 (while (and (not (dired-move-to-filename)) (< (point) max))
1509 (defun dired-goto-file (file)
1510 "Go to file line of FILE in this dired buffer."
1511 ;; Return value of point on success, else nil.
1512 ;; FILE must be an absolute pathname.
1513 ;; Loses if FILE contains control chars like "\007" for which ls
1514 ;; either inserts "?" or "\\007" into the buffer, so we won't find
1515 ;; it in the buffer.
1517 (prog1 ; let push-mark display its message
1518 (list (expand-file-name
1519 (read-file-name "Goto file: "
1520 (dired-current-directory))))
1522 (setq file (directory-file-name file)) ; does no harm if no directory
1523 (let (found case-fold-search dir)
1524 (setq dir (or (file-name-directory file)
1525 (error "Need absolute pathname for %s" file)))
1527 ;; The hair here is to get the result of dired-goto-subdir
1528 ;; without really calling it if we don't have any subdirs.
1529 (if (if (string= dir (expand-file-name default-directory))
1530 (goto-char (point-min))
1531 (and (cdr dired-subdir-alist)
1532 (dired-goto-subdir dir)))
1533 (let ((base (file-name-nondirectory file))
1534 (boundary (dired-subdir-max)))
1535 (while (and (not found)
1536 ;; filenames are preceded by SPC, this makes
1537 ;; the search faster (e.g. for the filename "-"!).
1538 (search-forward (concat " " base) boundary 'move))
1539 ;; Match could have BASE just as initial substring or
1540 ;; or in permission bits or date or
1541 ;; not be a proper filename at all:
1542 (if (equal base (dired-get-filename 'no-dir t))
1543 ;; Must move to filename since an (actually
1544 ;; correct) match could have been elsewhere on the
1545 ;; ;; line (e.g. "-" would match somewhere in the
1546 ;; permission bits).
1547 (setq found (dired-move-to-filename))
1548 ;; If this isn't the right line, move forward to avoid
1549 ;; trying this line again.
1550 (forward-line 1))))))
1552 ;; return value of point (i.e., FOUND):
1553 (goto-char found))))
1555 (defun dired-initial-position (dirname)
1556 ;; Where point should go in a new listing of DIRNAME.
1557 ;; Point assumed at beginning of new subdir line.
1558 ;; You may redefine this function as you wish, e.g. like in dired-x.el.
1560 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
1562 ;; These are hooks which make tree dired work.
1563 ;; They are in this file because other parts of dired need to call them.
1564 ;; But they don't call the rest of tree dired unless there are subdirs loaded.
1566 ;; This function is called for each retrieved filename.
1567 ;; It could stand to be faster, though it's mostly function call
1568 ;; overhead. Avoiding the function call seems to save about 10% in
1569 ;; dired-get-filename. Make it a defsubst?
1570 (defun dired-current-directory (&optional localp)
1571 "Return the name of the subdirectory to which this line belongs.
1572 This returns a string with trailing slash, like `default-directory'.
1573 Optional argument means return a file name relative to `default-directory'."
1574 (let ((here (point))
1575 (alist (or dired-subdir-alist
1576 ;; probably because called in a non-dired buffer
1577 (error "No subdir-alist in %s" (current-buffer))))
1580 (setq elt (car alist)
1582 ;; use `<=' (not `<') as subdir line is part of subdir
1583 alist (if (<= (dired-get-subdir-min elt) here)
1587 (dired-make-relative dir default-directory)
1590 ;; Subdirs start at the beginning of their header lines and end just
1591 ;; before the beginning of the next header line (or end of buffer).
1593 (defun dired-subdir-max ()
1595 (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
1601 (defun dired-do-flagged-delete (&optional nomessage)
1602 "In dired, delete the files flagged for deletion.
1603 If NOMESSAGE is non-nil, we don't display any message
1604 if there are no flagged files."
1606 (let* ((dired-marker-char dired-del-marker)
1607 (regexp (dired-marker-regexp))
1609 (if (save-excursion (goto-char (point-min))
1610 (re-search-forward regexp nil t))
1611 (dired-internal-do-deletions
1612 ;; this can't move point since ARG is nil
1613 (dired-map-over-marks (cons (dired-get-filename) (point))
1617 (message "(No deletions requested)")))))
1619 (defun dired-do-delete (&optional arg)
1620 "Delete all marked (or next ARG) files."
1621 ;; This is more consistent with the file marking feature than
1622 ;; dired-do-flagged-delete.
1624 (dired-internal-do-deletions
1625 ;; this may move point if ARG is an integer
1626 (dired-map-over-marks (cons (dired-get-filename) (point))
1630 (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
1632 (defun dired-internal-do-deletions (l arg)
1633 ;; L is an alist of files to delete, with their buffer positions.
1634 ;; ARG is the prefix arg.
1635 ;; Filenames are absolute (VMS needs this for logical search paths).
1636 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
1637 ;; That way as changes are made in the buffer they do not shift the
1638 ;; lines still to be changed, so the (point) values in L stay valid.
1639 ;; Also, for subdirs in natural order, a subdir's files are deleted
1640 ;; before the subdir itself - the other way around would not work.
1641 (let ((files (mapcar (function car) l))
1644 ;; canonicalize file list for pop up
1645 (setq files (nreverse (mapcar (function dired-make-relative) files)))
1646 (if (dired-mark-pop-up
1647 " *Deletions*" 'delete files dired-deletion-confirmer
1648 (format "Delete %s " (dired-mark-prompt arg files)))
1650 (let (failures);; files better be in reverse order for this loop!
1652 (goto-char (cdr (car l)))
1653 (let (buffer-read-only)
1655 (let ((fn (car (car l))))
1656 ;; This test is equivalent to
1657 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
1658 ;; but more efficient
1659 (if (eq t (car (file-attributes fn)))
1660 (delete-directory fn)
1662 ;; if we get here, removing worked
1663 (setq succ (1+ succ))
1664 (message "%s of %s deletions" succ count)
1665 (delete-region (progn (beginning-of-line) (point))
1666 (progn (forward-line 1) (point)))
1667 (dired-clean-up-after-deletion fn))
1668 (error;; catch errors from failed deletions
1669 (dired-log "%s\n" err)
1670 (setq failures (cons (car (car l)) failures)))))
1673 (message "%d deletion%s done" count (dired-plural-s count))
1675 (format "%d of %d deletion%s failed"
1676 (length failures) count
1677 (dired-plural-s count))
1679 (message "(No deletions performed)")))
1680 (dired-move-to-filename))
1682 ;; This is a separate function for the sake of dired-x.el.
1683 (defun dired-clean-up-after-deletion (fn)
1684 ;; Clean up after a deleted file or directory FN.
1685 (save-excursion (and (cdr dired-subdir-alist)
1686 (dired-goto-subdir fn)
1687 (dired-kill-subdir))))
1691 (defun dired-marker-regexp ()
1692 (concat "^" (regexp-quote (char-to-string dired-marker-char))))
1694 (defun dired-plural-s (count)
1695 (if (= 1 count) "" "s"))
1697 (defun dired-mark-prompt (arg files)
1698 ;; Return a string for use in a prompt, either the current file
1699 ;; name, or the marker and a count of marked files.
1700 (let ((count (length files)))
1703 ;; more than 1 file:
1706 ;; Perhaps this is nicer, but it also takes more screen space:
1707 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
1709 (format "[next %d files]" arg)
1710 (format "%c [%d files]" dired-marker-char count)))))
1712 (defun dired-pop-to-buffer (buf)
1713 ;; Pop up buffer BUF.
1714 ;; If dired-shrink-to-fit is t, make its window fit its contents.
1715 (if (not dired-shrink-to-fit)
1716 (pop-to-buffer (get-buffer-create buf))
1717 ;; let window shrink to fit:
1718 (let ((window (selected-window))
1720 (cond ;; if split-window-threshold is enabled, use the largest window
1721 ((and (> (window-height (setq w2 (get-largest-window)))
1722 split-height-threshold)
1723 (= (frame-width) (window-width w2)))
1725 ;; if the least-recently-used window is big enough, use it
1726 ((and (> (window-height (setq w2 (get-lru-window)))
1727 (* 2 window-min-height))
1728 (= (frame-width) (window-width w2)))
1732 (goto-char (point-max))
1733 (skip-chars-backward "\n\r\t ")
1734 (setq target-lines (count-lines (point-min) (point)))
1735 ;; Don't forget to count the last line.
1737 (setq target-lines (1+ target-lines))))
1738 (if (<= (window-height window) (* 2 window-min-height))
1739 ;; At this point, every window on the frame is too small to split.
1740 (setq w2 (display-buffer buf))
1741 (setq w2 (split-window window
1742 (max window-min-height
1743 (- (window-height window)
1744 (1+ (max window-min-height target-lines)))))))
1745 (set-window-buffer w2 buf)
1746 (if (< (1- (window-height w2)) target-lines)
1749 (enlarge-window (- target-lines (1- (window-height w2))))))
1750 (set-window-start w2 1)
1753 (defvar dired-no-confirm nil
1754 ;; "If non-nil, list of symbols for commands dired should not confirm.
1755 ;;It can be a sublist of
1757 ;; '(byte-compile chgrp chmod chown compress copy delete hardlink load
1758 ;; move print shell symlink uncompress)"
1761 (defun dired-mark-pop-up (bufname op-symbol files function &rest args)
1762 ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
1763 ;;Return FUNCTION's result on ARGS after popping up a window (in a buffer
1764 ;;named BUFNAME, nil gives \" *Marked Files*\") showing the marked
1765 ;;files. Uses function `dired-pop-to-buffer' to do that.
1766 ;; FUNCTION should not manipulate files.
1767 ;; It should only read input (an argument or confirmation).
1768 ;;The window is not shown if there is just one file or
1769 ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
1770 ;;FILES is the list of marked files."
1771 (or bufname (setq bufname " *Marked Files*"))
1772 (if (or (memq op-symbol dired-no-confirm)
1773 (= (length files) 1))
1774 (apply function args)
1776 (set-buffer (get-buffer-create bufname))
1778 (dired-format-columns-of-files files)
1779 (remove-text-properties (point-min) (point-max) '(mouse-face)))
1780 (save-window-excursion
1781 (dired-pop-to-buffer bufname)
1782 (apply function args))))
1784 (defun dired-format-columns-of-files (files)
1785 ;; Files should be in forward order for this loop.
1786 ;; i.e., (car files) = first file in buffer.
1787 ;; Returns the number of lines used.
1788 (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files))))
1789 (width (- (window-width (selected-window)) 2))
1790 (columns (max 1 (/ width maxlen)))
1791 (nfiles (length files))
1792 (rows (+ (/ nfiles columns)
1793 (if (zerop (% nfiles columns)) 0 1)))
1796 (setq files (nconc (copy-sequence files) ; fill up with empty fns
1797 (make-list (- (* columns rows) nfiles) "")))
1798 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
1800 (while (< i columns)
1801 (indent-to (* i maxlen))
1802 (insert (car files))
1803 (setq files (nthcdr rows files)
1811 ;; Commands to mark or flag file(s) at or near current line.
1813 (defun dired-repeat-over-lines (arg function)
1814 ;; This version skips non-file lines.
1815 (let ((pos (make-marker)))
1817 (while (and (> arg 0) (not (eobp)))
1820 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
1823 (move-marker pos (1+ (point))))
1824 (save-excursion (funcall function))
1825 ;; Advance to the next line--actually, to the line that *was* next.
1826 ;; (If FUNCTION inserted some new lines in between, skip them.)
1828 (while (and (< arg 0) (not (bobp)))
1831 (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
1833 (save-excursion (funcall function)))
1834 (move-marker pos nil)
1835 (dired-move-to-filename)))
1837 (defun dired-between-files ()
1838 ;; Point must be at beginning of line
1839 ;; Should be equivalent to (save-excursion (not (dired-move-to-filename)))
1840 ;; but is about 1.5..2.0 times as fast. (Actually that's not worth it)
1841 (or (looking-at "^$\\|^. *$\\|^. total\\|^. wildcard")
1842 (and (looking-at dired-subdir-regexp)
1843 (save-excursion (not (dired-move-to-filename))))))
1845 (defun dired-next-marked-file (arg &optional wrap opoint)
1846 "Move to the next marked file, wrapping around the end of the buffer."
1847 (interactive "p\np")
1848 (or opoint (setq opoint (point)));; return to where interactively started
1850 (re-search-forward dired-re-mark nil t arg)
1852 (re-search-backward dired-re-mark nil t (- arg)))
1853 (dired-move-to-filename)
1857 (error "No next marked file"))
1858 (message "(Wraparound for next marked file)")
1859 (goto-char (if (> arg 0) (point-min) (point-max)))
1860 (dired-next-marked-file arg nil opoint))))
1862 (defun dired-prev-marked-file (arg &optional wrap)
1863 "Move to the previous marked file, wrapping around the end of the buffer."
1864 (interactive "p\np")
1865 (dired-next-marked-file (- arg) wrap))
1867 (defun dired-file-marker (file)
1868 ;; Return FILE's marker, or nil if unmarked.
1870 (and (dired-goto-file file)
1873 (if (not (equal ?\040 (following-char)))
1874 (following-char))))))
1876 (defun dired-mark-files-in-region (start end)
1877 (let (buffer-read-only)
1879 (error "start > end"))
1880 (goto-char start) ; assumed at beginning of line
1881 (while (< (point) end)
1882 ;; Skip subdir line and following garbage like the `total' line:
1883 (while (and (< (point) end) (dired-between-files))
1885 (if (and (not (looking-at dired-re-dot))
1886 (dired-get-filename nil t))
1889 (insert dired-marker-char)))
1892 (defun dired-mark (arg)
1893 "Mark the current (or next ARG) files.
1894 If on a subdir headerline, mark all its files except `.' and `..'.
1896 Use \\[dired-unmark-all-files] to remove all marks
1897 and \\[dired-unmark] on a subdir to remove the marks in
1900 (if (dired-get-subdir)
1901 (save-excursion (dired-mark-subdir-files))
1902 (let (buffer-read-only)
1903 (dired-repeat-over-lines
1904 (prefix-numeric-value arg)
1905 (function (lambda () (delete-char 1) (insert dired-marker-char)))))))
1907 (defun dired-unmark (arg)
1908 "Unmark the current (or next ARG) files.
1909 If looking at a subdir, unmark all its files except `.' and `..'."
1911 (let ((dired-marker-char ?\040))
1914 (defun dired-flag-file-deletion (arg)
1915 "In dired, flag the current line's file for deletion.
1916 With prefix arg, repeat over several lines.
1918 If on a subdir headerline, mark all its files except `.' and `..'."
1920 (let ((dired-marker-char dired-del-marker))
1923 (defun dired-unmark-backward (arg)
1924 "In dired, move up lines and remove deletion flag there.
1925 Optional prefix ARG says how many lines to unflag; default is one line."
1927 (dired-unmark (- arg)))
1929 ;;; Commands to mark or flag files based on their characteristics or names.
1931 (defvar dired-regexp-history nil
1932 "History list of regular expressions used in Dired commands.")
1934 (defun dired-read-regexp (prompt)
1935 (read-from-minibuffer prompt nil nil nil 'dired-regexp-history))
1937 (defun dired-mark-files-regexp (regexp &optional marker-char)
1938 "Mark all files matching REGEXP for use in later commands.
1939 A prefix argument means to unmark them instead.
1940 `.' and `..' are never marked.
1942 REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
1943 object files--just `.o' will mark more than you might think."
1945 (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
1946 " files (regexp): "))
1947 (if current-prefix-arg ?\040)))
1948 (let ((dired-marker-char (or marker-char dired-marker-char)))
1950 (and (not (looking-at dired-re-dot))
1951 (not (eolp)) ; empty line
1952 (let ((fn (dired-get-filename nil t)))
1953 (and fn (string-match regexp (file-name-nondirectory fn)))))
1956 (defun dired-flag-files-regexp (regexp)
1957 "In dired, flag all files containing the specified REGEXP for deletion.
1958 The match is against the non-directory part of the filename. Use `^'
1959 and `$' to anchor matches. Exclude subdirs by hiding them.
1960 `.' and `..' are never flagged."
1961 (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
1962 (dired-mark-files-regexp regexp dired-del-marker))
1964 (defun dired-mark-symlinks (unflag-p)
1965 "Mark all symbolic links.
1966 With prefix argument, unflag all those files."
1968 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
1969 (dired-mark-if (looking-at dired-re-sym) "symbolic link")))
1971 (defun dired-mark-directories (unflag-p)
1972 "Mark all directory file lines except `.' and `..'.
1973 With prefix argument, unflag all those files."
1975 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
1976 (dired-mark-if (and (looking-at dired-re-dir)
1977 (not (looking-at dired-re-dot)))
1980 (defun dired-mark-executables (unflag-p)
1981 "Mark all executable files.
1982 With prefix argument, unflag all those files."
1984 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
1985 (dired-mark-if (looking-at dired-re-exe) "executable file")))
1987 ;; dired-x.el has a dired-mark-sexp interactive command: mark
1988 ;; files for which PREDICATE returns non-nil.
1990 (defun dired-flag-auto-save-files (&optional unflag-p)
1991 "Flag for deletion files whose names suggest they are auto save files.
1992 A prefix argument says to unflag those files instead."
1994 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
1996 ;; It is less than general to check for # here,
1997 ;; but it's the only way this runs fast enough.
1998 (and (save-excursion (end-of-line)
2000 (eq (preceding-char) ?#)
2001 ;; Handle executables in case of -F option.
2002 ;; We need not worry about the other kinds
2003 ;; of markings that -F makes, since they won't
2004 ;; appear on real auto-save files.
2005 (if (eq (preceding-char) ?*)
2008 (eq (preceding-char) ?#)))))
2009 (not (looking-at dired-re-dir))
2010 (let ((fn (dired-get-filename t t)))
2011 (if fn (auto-save-file-name-p
2012 (file-name-nondirectory fn)))))
2015 (defun dired-flag-backup-files (&optional unflag-p)
2016 "Flag all backup files (names ending with `~') for deletion.
2017 With prefix argument, unflag these files."
2019 (let ((dired-marker-char (if unflag-p ?\ dired-del-marker)))
2021 ;; Don't call backup-file-name-p unless the last character looks like
2022 ;; it might be the end of a backup file name. This isn't very general,
2023 ;; but it's the only way this runs fast enough.
2024 (and (save-excursion (end-of-line)
2025 ;; Handle executables in case of -F option.
2026 ;; We need not worry about the other kinds
2027 ;; of markings that -F makes, since they won't
2028 ;; appear on real backup files.
2029 (if (eq (preceding-char) ?*)
2031 (eq (preceding-char) ?~))
2032 (not (looking-at dired-re-dir))
2033 (let ((fn (dired-get-filename t t)))
2034 (if fn (backup-file-name-p fn))))
2037 (defun dired-change-marks (&optional old new)
2038 "Change all OLD marks to NEW marks.
2039 OLD and NEW are both characters used to mark files."
2041 (let* ((cursor-in-echo-area t)
2042 (old (progn (message "Change (old mark): ") (read-char)))
2043 (new (progn (message "Change %c marks to (new mark): " old)
2046 (if (or (eq old ?\r) (eq new ?\r))
2048 (let ((string (format "\n%c" old))
2051 (goto-char (point-min))
2052 (while (search-forward string nil t)
2055 (dired-get-filename 'no-dir t))
2057 (subst-char-in-region (match-beginning 0)
2058 (match-end 0) old new)))))))
2060 (defun dired-unmark-all-files-no-query ()
2061 "Remove all marks from all files in the Dired buffer."
2063 (dired-unmark-all-files ?\r))
2065 (defun dired-unmark-all-files (mark &optional arg)
2066 "Remove a specific mark (or any mark) from every file.
2067 After this command, type the mark character to remove,
2068 or type RET to remove all marks.
2069 With prefix arg, query for each marked file.
2070 Type \\[help-command] at that time for help."
2071 (interactive "cRemove marks (RET means all): \nP")
2074 buffer-read-only case-fold-search query
2075 (string (format "\n%c" mark))
2077 Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
2078 `!' to unmark all remaining files with no more questions."))
2079 (goto-char (point-min))
2080 (while (if (eq mark ?\r)
2081 (re-search-forward dired-re-mark nil t)
2082 (search-forward string nil t))
2084 (dired-query 'query "Unmark file `%s'? "
2085 (dired-get-filename t)))
2086 (progn (subst-char-in-region (1- (point)) (point)
2087 (preceding-char) ?\ )
2088 (setq count (1+ count)))))
2089 (message (if (= count 1) "1 mark removed"
2093 ;; Logging failures operating on files, and showing the results.
2095 (defvar dired-log-buffer "*Dired log*")
2098 "Pop up a buffer with error log output from Dired.
2099 A group of errors from a single command ends with a formfeed.
2100 Thus, use \\[backward-page] to find the beginning of a group of errors."
2102 (if (get-buffer dired-log-buffer)
2103 (let ((owindow (selected-window))
2104 (window (display-buffer (get-buffer dired-log-buffer))))
2107 (select-window window)
2108 (goto-char (point-max))
2110 (select-window owindow)))))
2112 (defun dired-log (log &rest args)
2113 ;; Log a message or the contents of a buffer.
2114 ;; If LOG is a string and there are more args, it is formatted with
2115 ;; those ARGS. Usually the LOG string ends with a \n.
2116 ;; End each bunch of errors with (dired-log t): this inserts
2117 ;; current time and buffer, and a \f (formfeed).
2118 (let ((obuf (current-buffer)))
2119 (unwind-protect ; want to move point
2121 (set-buffer (get-buffer-create dired-log-buffer))
2122 (goto-char (point-max))
2123 (let (buffer-read-only)
2124 (cond ((stringp log)
2126 (apply (function format) log args)
2129 (insert-buffer log))
2131 (insert "\n\t" (current-time-string)
2132 "\tBuffer `" (buffer-name obuf) "'\n\f\n")))))
2133 (set-buffer obuf))))
2135 (defun dired-log-summary (string failures)
2136 (message (if failures "%s--type ? for details (%s)"
2137 "%s--type ? for details")
2139 ;; Log a summary describing a bunch of errors.
2140 (dired-log (concat "\n" string))
2145 ;; Most ls can only sort by name or by date (with -t), nothing else.
2146 ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
2147 ;; So anything that does not contain these is sort "by name".
2149 (defvar dired-ls-sorting-switches "SXU"
2150 "String of `ls' switches (single letters) except `t' that influence sorting.")
2152 (defvar dired-sort-by-date-regexp
2153 (concat "^-[^" dired-ls-sorting-switches
2154 "]*t[^" dired-ls-sorting-switches "]*$")
2155 "Regexp recognized by dired to set `by date' mode.")
2157 (defvar dired-sort-by-name-regexp
2158 (concat "^-[^t" dired-ls-sorting-switches "]+$")
2159 "Regexp recognized by dired to set `by name' mode.")
2161 (defun dired-sort-set-modeline ()
2162 ;; Set modeline display according to dired-actual-switches.
2163 ;; Modeline display of "by name" or "by date" guarantees the user a
2164 ;; match with the corresponding regexps. Non-matching switches are
2167 (let (case-fold-search)
2168 (cond ((string-match dired-sort-by-name-regexp dired-actual-switches)
2170 ((string-match dired-sort-by-date-regexp dired-actual-switches)
2173 (concat "Dired " dired-actual-switches)))))
2174 (force-mode-line-update))
2176 (defun dired-sort-toggle-or-edit (&optional arg)
2177 "Toggle between sort by date/name and refresh the dired buffer.
2178 With a prefix argument you can edit the current listing switches instead."
2182 (read-string "ls switches (must contain -l): " dired-actual-switches))
2183 (dired-sort-toggle)))
2185 (defun dired-sort-toggle ()
2186 ;; Toggle between sort by date/name. Reverts the buffer.
2187 (setq dired-actual-switches
2188 (let (case-fold-search)
2191 (dired-replace-in-string (concat "[-lt"
2192 dired-ls-sorting-switches "]")
2194 dired-actual-switches)
2195 (if (string-match (concat "[t" dired-ls-sorting-switches "]")
2196 dired-actual-switches)
2199 (dired-sort-set-modeline)
2202 (defun dired-replace-in-string (regexp newtext string)
2203 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
2204 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
2205 (let ((result "") (start 0) mb me)
2206 (while (string-match regexp string start)
2207 (setq mb (match-beginning 0)
2209 result (concat result (substring string start mb) newtext)
2211 (concat result (substring string start))))
2213 (defun dired-sort-other (switches &optional no-revert)
2214 ;; Specify new ls SWITCHES for current dired buffer. Values matching
2215 ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
2216 ;; minor mode accordingly, others appear literally in the mode line.
2217 ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
2218 (setq dired-actual-switches switches)
2219 (if (eq major-mode 'dired-mode) (dired-sort-set-modeline))
2220 (or no-revert (revert-buffer)))
2222 ;; To make this file smaller, the less common commands
2223 ;; go in a separate file. But autoload them here
2224 ;; to make the separation invisible.
2226 (autoload 'dired-diff "dired-aux"
2227 "Compare file at point with file FILE using `diff'.
2228 FILE defaults to the file at the mark.
2229 The prompted-for file is the first file given to `diff'."
2232 (autoload 'dired-backup-diff "dired-aux"
2233 "Diff this file with its backup file or vice versa.
2234 Uses the latest backup, if there are several numerical backups.
2235 If this file is a backup, diff it with its original.
2236 The backup file is the first file given to `diff'."
2239 (autoload 'dired-clean-directory "dired-aux"
2240 "Flag numerical backups for deletion.
2241 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
2242 Positive prefix arg KEEP overrides `dired-kept-versions';
2243 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
2245 To clear the flags on these files, you can use \\[dired-flag-backup-files]
2246 with a prefix argument."
2249 (autoload 'dired-do-chmod "dired-aux"
2250 "Change the mode of the marked (or next ARG) files.
2251 This calls chmod, thus symbolic modes like `g+w' are allowed."
2254 (autoload 'dired-do-chgrp "dired-aux"
2255 "Change the group of the marked (or next ARG) files."
2258 (autoload 'dired-do-chown "dired-aux"
2259 "Change the owner of the marked (or next ARG) files."
2262 (autoload 'dired-do-print "dired-aux"
2263 "Print the marked (or next ARG) files.
2264 Uses the shell command coming from variables `lpr-command' and
2265 `lpr-switches' as default."
2268 (autoload 'dired-do-shell-command "dired-aux"
2269 "Run a shell command COMMAND on the marked files.
2270 If no files are marked or a specific numeric prefix arg is given,
2271 the next ARG files are used. Just \\[universal-argument] means the current file.
2272 The prompt mentions the file(s) or the marker, as appropriate.
2274 If there is output, it goes to a separate buffer.
2276 Normally the command is run on each file individually.
2277 However, if there is a `*' in the command then it is run
2278 just once with the entire file list substituted there.
2280 No automatic redisplay of dired buffers is attempted, as there's no
2281 telling what files the command may have changed. Type
2282 \\[dired-do-redisplay] to redisplay the marked files.
2284 The shell command has the top level directory as working directory, so
2285 output files usually are created there instead of in a subdir."
2288 (autoload 'dired-do-kill-lines "dired-aux"
2289 "Kill all marked lines (not the files).
2290 With a prefix arg, kill all lines not marked or flagged."
2293 (autoload 'dired-do-compress "dired-aux"
2294 "Compress or uncompress marked (or next ARG) files."
2297 (autoload 'dired-do-byte-compile "dired-aux"
2298 "Byte compile marked (or next ARG) Emacs Lisp files."
2301 (autoload 'dired-do-load "dired-aux"
2302 "Load the marked (or next ARG) Emacs Lisp files."
2305 (autoload 'dired-do-redisplay "dired-aux"
2306 "Redisplay all marked (or next ARG) files.
2307 If on a subdir line, redisplay that subdirectory. In that case,
2308 a prefix arg lets you edit the `ls' switches used for the new listing."
2311 (autoload 'dired-string-replace-match "dired-aux"
2312 "Replace first match of REGEXP in STRING with NEWTEXT.
2313 If it does not match, nil is returned instead of the new string.
2314 Optional arg LITERAL means to take NEWTEXT literally.
2315 Optional arg GLOBAL means to replace all matches."
2318 (autoload 'dired-create-directory "dired-aux"
2319 "Create a directory called DIRECTORY."
2322 (autoload 'dired-do-copy "dired-aux"
2323 "Copy all marked (or next ARG) files, or copy the current file.
2324 Thus, a zero prefix argument copies nothing. But it toggles the
2325 variable `dired-copy-preserve-time' (which see)."
2328 (autoload 'dired-do-symlink "dired-aux"
2329 "Make symbolic links to current file or all marked (or next ARG) files.
2330 When operating on just the current file, you specify the new name.
2331 When operating on multiple or marked files, you specify a directory
2332 and new symbolic links are made in that directory
2333 with the same names that the files currently have."
2336 (autoload 'dired-do-hardlink "dired-aux"
2337 "Add names (hard links) current file or all marked (or next ARG) files.
2338 When operating on just the current file, you specify the new name.
2339 When operating on multiple or marked files, you specify a directory
2340 and new hard links are made in that directory
2341 with the same names that the files currently have."
2344 (autoload 'dired-do-rename "dired-aux"
2345 "Rename current file or all marked (or next ARG) files.
2346 When renaming just the current file, you specify the new name.
2347 When renaming multiple or marked files, you specify a directory."
2350 (autoload 'dired-do-rename-regexp "dired-aux"
2351 "Rename marked files containing REGEXP to NEWNAME.
2352 As each match is found, the user must type a character saying
2353 what to do with it. For directions, type \\[help-command] at that time.
2354 NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
2355 REGEXP defaults to the last regexp used.
2356 With a zero prefix arg, renaming by regexp affects the complete
2357 pathname - usually only the non-directory part of file names is used
2361 (autoload 'dired-do-copy-regexp "dired-aux"
2362 "Copy all marked files containing REGEXP to NEWNAME.
2363 See function `dired-rename-regexp' for more info."
2366 (autoload 'dired-do-hardlink-regexp "dired-aux"
2367 "Hardlink all marked files containing REGEXP to NEWNAME.
2368 See function `dired-rename-regexp' for more info."
2371 (autoload 'dired-do-symlink-regexp "dired-aux"
2372 "Symlink all marked files containing REGEXP to NEWNAME.
2373 See function `dired-rename-regexp' for more info."
2376 (autoload 'dired-upcase "dired-aux"
2377 "Rename all marked (or next ARG) files to upper case."
2380 (autoload 'dired-downcase "dired-aux"
2381 "Rename all marked (or next ARG) files to lower case."
2384 (autoload 'dired-maybe-insert-subdir "dired-aux"
2385 "Insert this subdirectory into the same dired buffer.
2386 If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
2387 else inserts it at its natural place (as `ls -lR' would have done).
2388 With a prefix arg, you may edit the ls switches used for this listing.
2389 You can add `R' to the switches to expand the whole tree starting at
2391 This function takes some pains to conform to `ls -lR' output."
2394 (autoload 'dired-next-subdir "dired-aux"
2395 "Go to next subdirectory, regardless of level."
2398 (autoload 'dired-prev-subdir "dired-aux"
2399 "Go to previous subdirectory, regardless of level.
2400 When called interactively and not on a subdir line, go to this subdir's line."
2403 (autoload 'dired-goto-subdir "dired-aux"
2404 "Go to end of header line of DIR in this dired buffer.
2405 Return value of point on success, otherwise return nil.
2406 The next char is either \\n, or \\r if DIR is hidden."
2409 (autoload 'dired-mark-subdir-files "dired-aux"
2410 "Mark all files except `.' and `..'."
2413 (autoload 'dired-kill-subdir "dired-aux"
2414 "Remove all lines of current subdirectory.
2415 Lower levels are unaffected."
2418 (autoload 'dired-tree-up "dired-aux"
2419 "Go up ARG levels in the dired tree."
2422 (autoload 'dired-tree-down "dired-aux"
2423 "Go down in the dired tree."
2426 (autoload 'dired-hide-subdir "dired-aux"
2427 "Hide or unhide the current subdirectory and move to next directory.
2428 Optional prefix arg is a repeat factor.
2429 Use \\[dired-hide-all] to (un)hide all directories."
2432 (autoload 'dired-hide-all "dired-aux"
2433 "Hide all subdirectories, leaving only their header lines.
2434 If there is already something hidden, make everything visible again.
2435 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2438 (if (eq system-type 'vax-vms)
2443 (run-hooks 'dired-load-hook) ; for your customizations
2445 ;;; dired.el ends here