(abort): Omit this definition, if NO_ABORT.
[bpt/emacs.git] / lisp / dired.el
CommitLineData
c8472948 1;;; dired.el --- directory-browsing commands
52041219 2
ef746164 3;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
ab67260b 4
b36b40ae
RS
5;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
6;; Maintainer: FSF
84fc2cfa
ER
7
8;; This file is part of GNU Emacs.
9
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
52041219 12;; the Free Software Foundation; either version 2, or (at your option)
84fc2cfa
ER
13;; any later version.
14
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.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
84fc2cfa 24
52041219
RS
25;;; Commentary:
26
e41b2db1
ER
27;; This is a major mode for directory browsing and editing. It is
28;; documented in the Emacs manual.
29
492d2437
RS
30;; Rewritten in 1990/1991 to add tree features, file marking and
31;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
32;; Finished up by rms in 1992.
33
52041219 34;;; Code:
492d2437 35
492d2437
RS
36;;; Customizable variables
37
286c247d
RS
38(defgroup dired nil
39 "Directory editing."
40 :group 'environment)
41
42(defgroup dired-mark nil
43 "Handling marks in dired."
44 :prefix "dired-"
45 :group 'dired)
46
47
84fc2cfa 48;;;###autoload
286c247d 49(defcustom dired-listing-switches "-al"
492d2437
RS
50 "*Switches passed to `ls' for dired. MUST contain the `l' option.
51May contain all other options that don't contradict `-l';
b36b40ae 52may contain even `F', `b', `i' and `s'. See also the variable
286c247d
RS
53`dired-ls-F-marks-symlinks' concerning the `F' switch."
54 :type 'string
55 :group 'dired)
84fc2cfa 56
492d2437
RS
57; Don't use absolute paths as /bin should be in any PATH and people
58; may prefer /usr/local/gnu/bin or whatever. However, chown is
59; usually not in PATH.
60
61;;;###autoload
84fc2cfa 62(defvar dired-chown-program
64202797 63 (if (memq system-type '(hpux dgux usg-unix-v irix linux gnu/linux))
6702f50d
RS
64 "chown"
65 (if (file-exists-p "/usr/sbin/chown")
66 "/usr/sbin/chown"
67 "/etc/chown"))
eb8c3be9 68 "Name of chown command (usually `chown' or `/etc/chown').")
492d2437 69
912aace8
GV
70(defvar dired-chmod-program "chmod"
71 "Name of chmod command (usually `chmod').")
723ddb1f 72
492d2437 73;;;###autoload
286c247d 74(defcustom dired-ls-F-marks-symlinks nil
492d2437 75 "*Informs dired about how `ls -lF' marks symbolic links.
fc83abf7
RS
76Set this to t if `ls' (or whatever program is specified by
77`insert-directory-program') with `-lF' marks the symbolic link
492d2437 78itself with a trailing @ (usually the case under Ultrix).
84fc2cfa 79
492d2437
RS
80Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
81nil (the default), if it gives `bar@ -> foo', set it to t.
82
83Dired checks if there is really a @ appended. Thus, if you have a
84marking `ls' program on one host and a non-marking on another host, and
85don't care about symbolic links which really end in a @, you can
286c247d
RS
86always set this variable to t."
87 :type 'boolean
88 :group 'dired-mark)
492d2437
RS
89
90;;;###autoload
286c247d 91(defcustom dired-trivial-filenames "^\\.\\.?$\\|^#"
492d2437
RS
92 "*Regexp of files to skip when finding first file of a directory.
93A value of nil means move to the subdir line.
286c247d
RS
94A value of t means move to first file."
95 :type '(choice (const :tag "Move to subdir" nil)
96 (const :tag "Move to first" t)
97 regexp)
98 :group 'dired)
492d2437
RS
99
100;;;###autoload
286c247d 101(defcustom dired-keep-marker-rename t
492d2437
RS
102 ;; Use t as default so that moved files "take their markers with them".
103 "*Controls marking of renamed files.
104If t, files keep their previous marks when they are renamed.
105If a character, renamed files (whether previously marked or not)
286c247d
RS
106are afterward marked with that character."
107 :type '(choice (const :tag "Keep" t)
108 (character :tag "Mark"))
109 :group 'dired-mark)
492d2437
RS
110
111;;;###autoload
286c247d 112(defcustom dired-keep-marker-copy ?C
492d2437
RS
113 "*Controls marking of copied files.
114If t, copied files are marked if and as the corresponding original files were.
286c247d
RS
115If a character, copied files are unconditionally marked with that character."
116 :type '(choice (const :tag "Keep" t)
117 (character :tag "Mark"))
118 :group 'dired-mark)
492d2437
RS
119
120;;;###autoload
286c247d 121(defcustom dired-keep-marker-hardlink ?H
492d2437
RS
122 "*Controls marking of newly made hard links.
123If t, they are marked if and as the files linked to were marked.
286c247d
RS
124If a character, new links are unconditionally marked with that character."
125 :type '(choice (const :tag "Keep" t)
126 (character :tag "Mark"))
127 :group 'dired-mark)
492d2437
RS
128
129;;;###autoload
286c247d 130(defcustom dired-keep-marker-symlink ?Y
492d2437
RS
131 "*Controls marking of newly made symbolic links.
132If t, they are marked if and as the files linked to were marked.
286c247d
RS
133If a character, new links are unconditionally marked with that character."
134 :type '(choice (const :tag "Keep" t)
135 (character :tag "Mark"))
136 :group 'dired-mark)
492d2437
RS
137
138;;;###autoload
286c247d 139(defcustom dired-dwim-target nil
492d2437
RS
140 "*If non-nil, dired tries to guess a default target directory.
141This means: if there is a dired buffer displayed in the next window,
142use its current subdir, instead of the current subdir of this dired buffer.
143
286c247d
RS
144The target is used in the prompt for file copy, rename etc."
145 :type 'boolean
146 :group 'dired)
492d2437
RS
147
148;;;###autoload
286c247d 149(defcustom dired-copy-preserve-time t
492d2437 150 "*If non-nil, Dired preserves the last-modified time in a file copy.
286c247d
RS
151\(This works on only some systems.)"
152 :type 'boolean
153 :group 'dired)
492d2437
RS
154
155;;; Hook variables
156
157(defvar dired-load-hook nil
158 "Run after loading dired.
159You can customize key bindings or load extensions with this.")
160
161(defvar dired-mode-hook nil
162 "Run at the very end of dired-mode.")
163
164(defvar dired-before-readin-hook nil
165 "This hook is run before a dired buffer is read in (created or reverted).")
166
167(defvar dired-after-readin-hook nil
168 "Hook run after each time a file or directory is read by Dired.
169After each listing of a file or directory, this hook is run
170with the buffer narrowed to the listing.")
171;; Note this can't simply be run inside function `dired-ls' as the hook
172;; functions probably depend on the dired-subdir-alist to be OK.
173
174;;; Internal variables
175
176(defvar dired-marker-char ?* ; the answer is 42
177 ;; so that you can write things like
178 ;; (let ((dired-marker-char ?X))
179 ;; ;; great code using X markers ...
180 ;; )
181 ;; For example, commands operating on two sets of files, A and B.
182 ;; Or marking files with digits 0-9. This could implicate
183 ;; concentric sets or an order for the marked files.
184 ;; The code depends on dynamic scoping on the marker char.
185 "In Dired, the current mark character.
186This is what the `do' commands look for and what the `mark' commands store.")
187
188(defvar dired-del-marker ?D
189 "Character used to flag files for deletion.")
190
191(defvar dired-shrink-to-fit
ab67260b
RS
192 t
193;; I see no reason ever to make this nil -- rms.
194;; (> baud-rate search-slow-speed)
492d2437
RS
195 "Non-nil means Dired shrinks the display buffer to fit the marked files.")
196
197(defvar dired-flagging-regexp nil);; Last regexp used to flag files.
198
ab67260b
RS
199(defvar dired-file-version-alist)
200
492d2437
RS
201(defvar dired-directory nil
202 "The directory name or shell wildcard that was used as argument to `ls'.
8d23c16b
ER
203Local to each dired buffer. May be a list, in which case the car is the
204directory name and the cdr is the actual files to list.")
492d2437
RS
205
206(defvar dired-actual-switches nil
207 "The value of `dired-listing-switches' used to make this buffer's text.")
208
209(defvar dired-re-inode-size "[0-9 \t]*"
210 "Regexp for optional initial inode and file size as made by `ls -i -s'.")
211
212;; These regexps must be tested at beginning-of-line, but are also
213;; used to search for next matches, so neither omitting "^" nor
214;; replacing "^" by "\n" (to make it slightly faster) will work.
215
216(defvar dired-re-mark "^[^ \n]")
217;; "Regexp matching a marked line.
218;; Important: the match ends just after the marker."
219(defvar dired-re-maybe-mark "^. ")
220(defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d"))
221(defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l"))
222(defvar dired-re-exe;; match ls permission string of an executable file
223 (mapconcat (function
224 (lambda (x)
225 (concat dired-re-maybe-mark dired-re-inode-size x)))
226 '("-[-r][-w][xs][-r][-w].[-r][-w]."
227 "-[-r][-w].[-r][-w][xs][-r][-w]."
228 "-[-r][-w].[-r][-w].[-r][-w][xst]")
229 "\\|"))
e6cecd2b 230(defvar dired-re-perms "[-bcdlps][-r][-w].[-r][-w].[-r][-w].")
492d2437
RS
231(defvar dired-re-dot "^.* \\.\\.?$")
232
38819778 233;; The subdirectory names in this list are expanded.
492d2437
RS
234(defvar dired-subdir-alist nil
235 "Association list of subdirectories and their buffer positions.
236Each subdirectory has an element: (DIRNAME . STARTMARKER).
7b2469ae
RS
237The order of elements is the reverse of the order in the buffer.
238In simple cases, this list contains one element.")
492d2437 239
cdf156a9 240(defvar dired-subdir-regexp "^. \\([^\n\r]+\\)\\(:\\)[\n\r]"
492d2437
RS
241 "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
242Subexpression 1 is the subdirectory proper, no trailing colon.
243The match starts at the beginning of the line and ends after the end
244of the line (\\n or \\r).
245Subexpression 2 must end right before the \\n or \\r.")
246
2439570d
SM
247(defvar dired-font-lock-keywords
248 (list
249 ;;
250 ;; Directory headers.
251 (list dired-subdir-regexp '(1 font-lock-type-face))
252 ;;
253 ;; We make heavy use of MATCH-ANCHORED, since the regexps don't identify the
254 ;; file name itself. We search for Dired defined regexps, and then use the
255 ;; Dired defined function `dired-move-to-filename' before searching for the
256 ;; simple regexp ".+". It is that regexp which matches the file name.
257 ;;
258 ;; Dired marks.
259 (list dired-re-mark
883212ce 260 '(0 font-lock-constant-face)
2439570d 261 '(".+" (dired-move-to-filename) nil (0 font-lock-warning-face)))
4fdf389a
RS
262 ;; People who are paranoid about security would consider this more
263 ;; important than other things such as whether it is a directory.
264 ;; But we don't want to encourage paranoia, so our default
265 ;; should be what's most useful for non-paranoids. -- rms.
266;;; ;;
267;;; ;; Files that are group or world writable.
268;;; (list (concat dired-re-maybe-mark dired-re-inode-size
269;;; "\\([-d]\\(....w....\\|.......w.\\)\\)")
270;;; '(1 font-lock-comment-face)
271;;; '(".+" (dired-move-to-filename) nil (0 font-lock-comment-face)))
2439570d
SM
272 ;;
273 ;; Subdirectories.
274 (list dired-re-dir
275 '(".+" (dired-move-to-filename) nil (0 font-lock-function-name-face)))
276 ;;
277 ;; Symbolic links.
278 (list dired-re-sym
279 '(".+" (dired-move-to-filename) nil (0 font-lock-keyword-face)))
280 ;;
281 ;; Files suffixed with `completion-ignored-extensions'.
282 '(eval .
283 (let ((extensions (mapcar 'regexp-quote completion-ignored-extensions)))
284 ;; It is quicker to first find just an extension, then go back to the
285 ;; start of that file name. So we do this complex MATCH-ANCHORED form.
286 (list (concat "\\(" (mapconcat 'identity extensions "\\|") "\\|#\\)$")
287 '(".+" (dired-move-to-filename) nil (0 font-lock-string-face))))))
288 "Additional expressions to highlight in Dired mode.")
492d2437
RS
289\f
290;;; Macros must be defined before they are used, for the byte compiler.
291
292;; Mark all files for which CONDITION evals to non-nil.
293;; CONDITION is evaluated on each line, with point at beginning of line.
294;; MSG is a noun phrase for the type of files being marked.
295;; It should end with a noun that can be pluralized by adding `s'.
296;; Return value is the number of files marked, or nil if none were marked.
297(defmacro dired-mark-if (predicate msg)
298 (` (let (buffer-read-only count)
299 (save-excursion
300 (setq count 0)
301 (if (, msg) (message "Marking %ss..." (, msg)))
302 (goto-char (point-min))
303 (while (not (eobp))
304 (if (, predicate)
305 (progn
306 (delete-char 1)
307 (insert dired-marker-char)
308 (setq count (1+ count))))
309 (forward-line 1))
310 (if (, msg) (message "%s %s%s %s%s."
311 count
312 (, msg)
313 (dired-plural-s count)
314 (if (eq dired-marker-char ?\040) "un" "")
315 (if (eq dired-marker-char dired-del-marker)
316 "flagged" "marked"))))
317 (and (> count 0) count))))
318
319(defmacro dired-map-over-marks (body arg &optional show-progress)
320;; "Macro: Perform BODY with point somewhere on each marked line
321;;and return a list of BODY's results.
322;;If no marked file could be found, execute BODY on the current line.
323;; If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
324;; files instead of the marked files.
325;; In that case point is dragged along. This is so that commands on
326;; the next ARG (instead of the marked) files can be chained easily.
327;; If ARG is otherwise non-nil, use current file instead.
328;;If optional third arg SHOW-PROGRESS evaluates to non-nil,
329;; redisplay the dired buffer after each file is processed.
330;;No guarantee is made about the position on the marked line.
331;; BODY must ensure this itself if it depends on this.
332;;Search starts at the beginning of the buffer, thus the car of the list
333;; corresponds to the line nearest to the buffer's bottom. This
334;; is also true for (positive and negative) integer values of ARG.
335;;BODY should not be too long as it is expanded four times."
336;;
337;;Warning: BODY must not add new lines before point - this may cause an
338;;endless loop.
339;;This warning should not apply any longer, sk 2-Sep-1991 14:10.
340 (` (prog1
341 (let (buffer-read-only case-fold-search found results)
342 (if (, arg)
343 (if (integerp (, arg))
344 (progn;; no save-excursion, want to move point.
345 (dired-repeat-over-lines
346 (, arg)
347 (function (lambda ()
348 (if (, show-progress) (sit-for 0))
349 (setq results (cons (, body) results)))))
350 (if (< (, arg) 0)
351 (nreverse results)
352 results))
353 ;; non-nil, non-integer ARG means use current file:
354 (list (, body)))
355 (let ((regexp (dired-marker-regexp)) next-position)
356 (save-excursion
357 (goto-char (point-min))
358 ;; remember position of next marked file before BODY
359 ;; can insert lines before the just found file,
360 ;; confusing us by finding the same marked file again
361 ;; and again and...
362 (setq next-position (and (re-search-forward regexp nil t)
363 (point-marker))
364 found (not (null next-position)))
365 (while next-position
366 (goto-char next-position)
367 (if (, show-progress) (sit-for 0))
368 (setq results (cons (, body) results))
369 ;; move after last match
370 (goto-char next-position)
371 (forward-line 1)
372 (set-marker next-position nil)
373 (setq next-position (and (re-search-forward regexp nil t)
374 (point-marker)))))
375 (if found
376 results
377 (list (, body))))))
378 ;; save-excursion loses, again
379 (dired-move-to-filename))))
380
381(defun dired-get-marked-files (&optional localp arg)
382 "Return the marked files' names as list of strings.
383The list is in the same order as the buffer, that is, the car is the
384 first marked file.
385Values returned are normally absolute pathnames.
386Optional arg LOCALP as in `dired-get-filename'.
387Optional second argument ARG forces to use other files. If ARG is an
388 integer, use the next ARG files. If ARG is otherwise non-nil, use
389 current file. Usually ARG comes from the current prefix arg."
84fc2cfa 390 (save-excursion
492d2437 391 (nreverse (dired-map-over-marks (dired-get-filename localp) arg))))
84fc2cfa 392
492d2437
RS
393\f
394;; Function dired-ls is redefinable for VMS, ange-ftp, Prospero or
395;; other special applications.
492d2437
RS
396\f
397;; The dired command
398
399(defun dired-read-dir-and-switches (str)
400 ;; For use in interactive.
401 (reverse (list
402 (if current-prefix-arg
403 (read-string "Dired listing switches: "
404 dired-listing-switches))
405 (read-file-name (format "Dired %s(directory): " str)
406 nil default-directory nil))))
84fc2cfa 407
492d2437 408;;;###autoload (define-key ctl-x-map "d" 'dired)
84fc2cfa 409;;;###autoload
492d2437 410(defun dired (dirname &optional switches)
84fc2cfa 411 "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
492d2437
RS
412Optional second argument SWITCHES specifies the `ls' options used.
413\(Interactively, use a prefix argument to be able to specify SWITCHES.)
414Dired displays a list of files in DIRNAME (which may also have
8d23c16b 415shell wildcards appended to select certain files). If DIRNAME is a cons,
1015daba 416its first element is taken as the directory name and the rest as an explicit
8d23c16b 417list of files to make directory entries for.
52041219 418\\<dired-mode-map>\
492d2437 419You can move around in it with the usual commands.
8d23c16b
ER
420You can flag files for deletion with \\[dired-flag-file-deletion] and then
421delete them by typing \\[dired-do-flagged-delete].
492d2437 422Type \\[describe-mode] after entering dired for more info.
84fc2cfa 423
492d2437
RS
424If DIRNAME is already in a dired buffer, that buffer is used without refresh."
425 ;; Cannot use (interactive "D") because of wildcards.
426 (interactive (dired-read-dir-and-switches ""))
427 (switch-to-buffer (dired-noselect dirname switches)))
428
429;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
84fc2cfa 430;;;###autoload
492d2437 431(defun dired-other-window (dirname &optional switches)
84fc2cfa 432 "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
492d2437
RS
433 (interactive (dired-read-dir-and-switches "in other window "))
434 (switch-to-buffer-other-window (dired-noselect dirname switches)))
84fc2cfa 435
ec03e49c
RS
436;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
437;;;###autoload
438(defun dired-other-frame (dirname &optional switches)
439 "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame."
440 (interactive (dired-read-dir-and-switches "in other frame "))
441 (switch-to-buffer-other-frame (dired-noselect dirname switches)))
442
84fc2cfa 443;;;###autoload
8d23c16b 444(defun dired-noselect (dir-or-list &optional switches)
84fc2cfa 445 "Like `dired' but returns the dired buffer as value, does not select it."
8d23c16b 446 (or dir-or-list (setq dir-or-list default-directory))
492d2437
RS
447 ;; This loses the distinction between "/foo/*/" and "/foo/*" that
448 ;; some shells make:
54df7d9a 449 (let (dirname initially-was-dirname)
8d23c16b
ER
450 (if (consp dir-or-list)
451 (setq dirname (car dir-or-list))
452 (setq dirname dir-or-list))
54df7d9a
RS
453 (setq initially-was-dirname
454 (string= (file-name-as-directory dirname) dirname))
370b6149
RS
455 (setq dirname (abbreviate-file-name
456 (expand-file-name (directory-file-name dirname))))
cb2e51f8
RS
457 (if find-file-visit-truename
458 (setq dirname (file-truename dirname)))
54df7d9a
RS
459 ;; If the argument was syntactically a directory name not a file name,
460 ;; or if it happens to name a file that is a directory,
461 ;; convert it syntactically to a directory name.
462 ;; The reason for checking initially-was-dirname
463 ;; and not just file-directory-p
464 ;; is that file-directory-p is slow over ftp.
465 (if (or initially-was-dirname (file-directory-p dirname))
466 (setq dirname (file-name-as-directory dirname)))
8d23c16b
ER
467 (if (consp dir-or-list)
468 (setq dir-or-list (cons dirname (cdr dir-or-list)))
469 (setq dir-or-list dirname))
470 (dired-internal-noselect dir-or-list switches)))
492d2437
RS
471
472;; Separate function from dired-noselect for the sake of dired-vms.el.
6a4cd605 473(defun dired-internal-noselect (dir-or-list &optional switches mode)
492d2437
RS
474 ;; If there is an existing dired buffer for DIRNAME, just leave
475 ;; buffer as it is (don't even call dired-revert).
476 ;; This saves time especially for deep trees or with ange-ftp.
477 ;; The user can type `g'easily, and it is more consistent with find-file.
478 ;; But if SWITCHES are given they are probably different from the
479 ;; buffer's old value, so call dired-sort-other, which does
480 ;; revert the buffer.
481 ;; A pity we can't possibly do "Directory has changed - refresh? "
482 ;; like find-file does.
6a4cd605
RS
483 ;; Optional argument MODE is passed to dired-find-buffer-nocreate,
484 ;; see there.
8d23c16b 485 (let* ((dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
6a4cd605
RS
486 ;; The following line used to use dir-or-list.
487 ;; That never found an existing buffer, in the case
488 ;; where it is a list.
489 (buffer (dired-find-buffer-nocreate dirname mode))
492d2437
RS
490 ;; note that buffer already is in dired-mode, if found
491 (new-buffer-p (not buffer))
492 (old-buf (current-buffer)))
493 (or buffer
494 (let ((default-major-mode 'fundamental-mode))
495 ;; We don't want default-major-mode to run hooks and set auto-fill
496 ;; or whatever, now that dired-mode does not
497 ;; kill-all-local-variables any longer.
498 (setq buffer (create-file-buffer (directory-file-name dirname)))))
499 (set-buffer buffer)
6cfd24f9
AS
500 (if (not new-buffer-p) ; existing buffer ...
501 (cond (switches ; ... but new switches
502 ;; file list may have changed
503 (if (consp dir-or-list)
504 (setq dired-directory dir-or-list))
505 ;; this calls dired-revert
506 (dired-sort-other switches))
507 ;; If directory has changed on disk, offer to revert.
508 ((if (let ((attributes (file-attributes dirname))
509 (modtime (visited-file-modtime)))
510 (or (eq modtime 0)
511 (not (eq (car attributes) t))
512 (and (= (car (nth 5 attributes)) (car modtime))
513 (= (nth 1 (nth 5 attributes)) (cdr modtime)))))
514 nil
952e8cb5 515 (message "%s"
212b2266
KH
516 (substitute-command-keys
517 "Directory has changed on disk; type \\[revert-buffer] to update Dired")))))
492d2437 518 ;; Else a new buffer
847aabce 519 (setq default-directory
e3cd4b53
RS
520 ;; We can do this unconditionally
521 ;; because dired-noselect ensures that the name
522 ;; is passed in directory name syntax
523 ;; if it was the name of a directory at all.
524 (file-name-directory dirname))
492d2437
RS
525 (or switches (setq switches dired-listing-switches))
526 (dired-mode dirname switches)
6a4cd605 527 (if mode (funcall mode))
492d2437
RS
528 ;; default-directory and dired-actual-switches are set now
529 ;; (buffer-local), so we can call dired-readin:
530 (let ((failed t))
531 (unwind-protect
8d23c16b 532 (progn (dired-readin dir-or-list buffer)
492d2437
RS
533 (setq failed nil))
534 ;; dired-readin can fail if parent directories are inaccessible.
535 ;; Don't leave an empty buffer around in that case.
536 (if failed (kill-buffer buffer))))
537 ;; No need to narrow since the whole buffer contains just
538 ;; dired-readin's output, nothing else. The hook can
539 ;; successfully use dired functions (e.g. dired-get-filename)
540 ;; as the subdir-alist has been built in dired-readin.
541 (run-hooks 'dired-after-readin-hook)
542 (goto-char (point-min))
543 (dired-initial-position dirname))
544 (set-buffer old-buf)
84fc2cfa
ER
545 buffer))
546
6a4cd605
RS
547(defun dired-find-buffer-nocreate (dirname &optional mode)
548 ;; This differs from dired-buffers-for-dir in that it does not consider
549 ;; subdirs of default-directory and searches for the first match only.
550 ;; Also, the major mode must be MODE.
f5b06a95 551 (let (found (blist dired-buffers)) ; was (buffer-list)
6a4cd605 552 (or mode (setq mode 'dired-mode))
492d2437 553 (while blist
6103c44e
KH
554 (if (null (buffer-name (cdr (car blist))))
555 (setq blist (cdr blist))
556 (save-excursion
557 (set-buffer (cdr (car blist)))
6a4cd605 558 (if (and (eq major-mode mode)
6cfd24f9
AS
559 (if (consp dired-directory)
560 (equal (car dired-directory) dirname)
561 (equal dired-directory dirname)))
6103c44e
KH
562 (setq found (cdr (car blist))
563 blist nil)
564 (setq blist (cdr blist))))))
492d2437
RS
565 found))
566
567\f
568;; Read in a new dired buffer
569
570;; dired-readin differs from dired-insert-subdir in that it accepts
571;; wildcards, erases the buffer, and builds the subdir-alist anew
572;; (including making it buffer-local and clearing it first).
8d23c16b 573(defun dired-readin (dir-or-list buffer)
492d2437
RS
574 ;; default-directory and dired-actual-switches must be buffer-local
575 ;; and initialized by now.
576 ;; Thus we can test (equal default-directory dirname) instead of
577 ;; (file-directory-p dirname) and save a filesystem transaction.
578 ;; Also, we can run this hook which may want to modify the switches
579 ;; based on default-directory, e.g. with ange-ftp to a SysV host
580 ;; where ls won't understand -Al switches.
8d23c16b
ER
581 (let (dirname)
582 (if (consp dir-or-list)
583 (setq dirname (car dir-or-list))
584 (setq dirname dir-or-list))
585 (setq dirname (expand-file-name dirname))
586 (if (consp dir-or-list)
587 (setq dir-or-list (cons dirname (cdr dir-or-list))))
588 (run-hooks 'dired-before-readin-hook)
589 (save-excursion
590 (message "Reading directory %s..." dirname)
591 (set-buffer buffer)
592 (let (buffer-read-only (failed t))
593 (widen)
594 (erase-buffer)
595 (dired-readin-insert dir-or-list)
596 (indent-rigidly (point-min) (point-max) 2)
597 ;; We need this to make the root dir have a header line as all
598 ;; other subdirs have:
599 (goto-char (point-min))
600 (dired-insert-headerline default-directory)
601 ;; can't run dired-after-readin-hook here, it may depend on the subdir
602 ;; alist to be OK.
603 )
604 (message "Reading directory %s...done" dirname)
8d23c16b
ER
605 ;; Must first make alist buffer local and set it to nil because
606 ;; dired-build-subdir-alist will call dired-clear-alist first
607 (set (make-local-variable 'dired-subdir-alist) nil)
8197b8bf 608 (dired-build-subdir-alist)
fa562dd5
RS
609 (let ((attributes (file-attributes dirname)))
610 (if (eq (car attributes) t)
611 (set-visited-file-modtime (nth 5 attributes))))
8197b8bf 612 (set-buffer-modified-p nil))))
492d2437
RS
613
614;; Subroutines of dired-readin
615
8d23c16b
ER
616(defun dired-readin-insert (dir-or-list)
617 ;; Just insert listing for the passed-in directory or
618 ;; directory-and-file list, assuming a clean buffer.
619 (let (dirname)
620 (if (consp dir-or-list)
621 (setq dirname (car dir-or-list))
622 (setq dirname dir-or-list))
f45da5d1
RS
623 ;; Expand before comparing in case one or both have been abbreviated.
624 (if (and (equal (expand-file-name default-directory)
625 (expand-file-name dirname))
cf39fa9b
RS
626 (not (consp dir-or-list)))
627 ;; If we are reading a whole single directory...
8d23c16b
ER
628 (dired-insert-directory dir-or-list dired-actual-switches nil t)
629 (if (not (file-readable-p
630 (directory-file-name (file-name-directory dirname))))
631 (error "Directory %s inaccessible or nonexistent" dirname)
cf39fa9b
RS
632 ;; Else assume it contains wildcards,
633 ;; unless it is an explicit list of files.
634 (dired-insert-directory dir-or-list dired-actual-switches
635 (not (listp dir-or-list)))
8d23c16b
ER
636 (save-excursion ;; insert wildcard instead of total line:
637 (goto-char (point-min))
638 (insert "wildcard " (file-name-nondirectory dirname) "\n"))))))
639
640(defun dired-insert-directory (dir-or-list switches &optional wildcard full-p)
641 ;; Do the right thing whether dir-or-list is atomic or not. If it is,
642 ;; inset all files listed in the cdr (the car is the passed-in directory
f726257e 643 ;; list).
9888aa65 644 (let ((opoint (point))
02b5d79c 645 (process-environment (copy-sequence process-environment))
9888aa65 646 end)
6492483f
RS
647 ;; We used to specify the C locale here, to force English month names;
648 ;; but this should not be necessary any more,
649 ;; with the new value of dired-move-to-filename-regexp.
cb88a3db 650 (if (consp dir-or-list)
bc9c99bc
RS
651 ;; In this case, use the file names in the cdr
652 ;; exactly as originally given to dired-noselect.
653 (mapcar
654 (function (lambda (x) (insert-directory x switches wildcard full-p)))
655 (cdr dir-or-list))
656 ;; Expand the file name here because it may have been abbreviated
657 ;; in dired-noselect.
f150bd2b 658 (insert-directory (expand-file-name dir-or-list) switches wildcard full-p))
9888aa65 659 ;; Quote certain characters, unless ls quoted them for us.
ad632492
SM
660 (if (not (string-match "b" dired-actual-switches))
661 (save-excursion
662 (setq end (point-marker))
663 (goto-char opoint)
664 (while (search-forward "\\" end t)
665 (replace-match "\\\\" nil t))
666 (goto-char opoint)
667 (while (search-forward "\^m" end t)
668 (replace-match "\\015" nil t))
669 (set-marker end nil)))
cb88a3db 670 (dired-insert-set-properties opoint (point)))
8d23c16b 671 (setq dired-directory dir-or-list))
492d2437 672
9888aa65 673;; Make the file names highlight when the mouse is on them.
cb88a3db
RS
674(defun dired-insert-set-properties (beg end)
675 (save-excursion
676 (goto-char beg)
677 (while (< (point) end)
7da0e9b4
RS
678 (condition-case nil
679 (if (dired-move-to-filename)
680 (put-text-property (point)
681 (save-excursion
682 (dired-move-to-end-of-filename)
683 (point))
684 'mouse-face 'highlight))
685 (error nil))
cb88a3db
RS
686 (forward-line 1))))
687
492d2437
RS
688(defun dired-insert-headerline (dir);; also used by dired-insert-subdir
689 ;; Insert DIR's headerline with no trailing slash, exactly like ls
690 ;; would, and put cursor where dired-build-subdir-alist puts subdir
691 ;; boundaries.
692 (save-excursion (insert " " (directory-file-name dir) ":\n")))
693
694\f
695;; Reverting a dired buffer
696
84fc2cfa 697(defun dired-revert (&optional arg noconfirm)
492d2437
RS
698 ;; Reread the dired buffer. Must also be called after
699 ;; dired-actual-switches have changed.
700 ;; Should not fail even on completely garbaged buffers.
701 ;; Preserves old cursor, marks/flags, hidden-p.
702 (widen) ; just in case user narrowed
84fc2cfa 703 (let ((opoint (point))
492d2437
RS
704 (ofile (dired-get-filename nil t))
705 (mark-alist nil) ; save marked files
706 (hidden-subdirs (dired-remember-hidden))
707 (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
708 (case-fold-search nil) ; we check for upper case ls flags
709 buffer-read-only)
710 (goto-char (point-min))
711 (setq mark-alist;; only after dired-remember-hidden since this unhides:
712 (dired-remember-marks (point-min) (point-max)))
713 ;; treat top level dir extra (it may contain wildcards)
8d23c16b
ER
714 (dired-uncache
715 (if (consp dired-directory) (car dired-directory) dired-directory))
84fc2cfa 716 (dired-readin dired-directory (current-buffer))
492d2437
RS
717 (let ((dired-after-readin-hook nil))
718 ;; don't run that hook for each subdir...
719 (dired-insert-old-subdirs old-subdir-alist))
720 (dired-mark-remembered mark-alist) ; mark files that were marked
721 ;; ... run the hook for the whole buffer, and only after markers
722 ;; have been reinserted (else omitting in dired-x would omit marked files)
723 (run-hooks 'dired-after-readin-hook) ; no need to narrow
724 (or (and ofile (dired-goto-file ofile)) ; move cursor to where it
725 (goto-char opoint)) ; was before
84fc2cfa 726 (dired-move-to-filename)
492d2437
RS
727 (save-excursion ; hide subdirs that were hidden
728 (mapcar (function (lambda (dir)
729 (if (dired-goto-subdir dir)
730 (dired-hide-subdir 1))))
731 hidden-subdirs)))
732 ;; outside of the let scope
733;;; Might as well not override the user if the user changed this.
734;;; (setq buffer-read-only t)
735 )
736
737;; Subroutines of dired-revert
738;; Some of these are also used when inserting subdirs.
739
740(defun dired-remember-marks (beg end)
741 ;; Return alist of files and their marks, from BEG to END.
742 (if selective-display ; must unhide to make this work.
743 (let (buffer-read-only)
744 (subst-char-in-region beg end ?\r ?\n)))
745 (let (fil chr alist)
746 (save-excursion
747 (goto-char beg)
748 (while (re-search-forward dired-re-mark end t)
749 (if (setq fil (dired-get-filename nil t))
750 (setq chr (preceding-char)
751 alist (cons (cons fil chr) alist)))))
752 alist))
753
754;; Mark all files remembered in ALIST.
755;; Each element of ALIST looks like (FILE . MARKERCHAR).
756(defun dired-mark-remembered (alist)
757 (let (elt fil chr)
758 (while alist
759 (setq elt (car alist)
760 alist (cdr alist)
761 fil (car elt)
762 chr (cdr elt))
763 (if (dired-goto-file fil)
764 (save-excursion
765 (beginning-of-line)
766 (delete-char 1)
767 (insert chr))))))
768
769;; Return a list of names of subdirs currently hidden.
770(defun dired-remember-hidden ()
771 (let ((l dired-subdir-alist) dir pos result)
772 (while l
773 (setq dir (car (car l))
774 pos (cdr (car l))
775 l (cdr l))
776 (goto-char pos)
777 (skip-chars-forward "^\r\n")
52041219 778 (if (eq (following-char) ?\r)
492d2437
RS
779 (setq result (cons dir result))))
780 result))
781
782;; Try to insert all subdirs that were displayed before,
783;; according to the former subdir alist OLD-SUBDIR-ALIST.
784(defun dired-insert-old-subdirs (old-subdir-alist)
785 (or (string-match "R" dired-actual-switches)
786 (let (elt dir)
787 (while old-subdir-alist
788 (setq elt (car old-subdir-alist)
789 old-subdir-alist (cdr old-subdir-alist)
790 dir (car elt))
791 (condition-case ()
715984d3
RS
792 (progn
793 (dired-uncache dir)
794 (dired-insert-subdir dir))
492d2437 795 (error nil))))))
715984d3
RS
796
797;; Remove directory DIR from any directory cache.
798(defun dired-uncache (dir)
6eaebaa2 799 (let ((handler (find-file-name-handler dir 'dired-uncache)))
715984d3
RS
800 (if handler
801 (funcall handler 'dired-uncache dir))))
492d2437
RS
802\f
803;; dired mode key bindings and initialization
84fc2cfa
ER
804
805(defvar dired-mode-map nil "Local keymap for dired-mode buffers.")
806(if dired-mode-map
807 nil
492d2437
RS
808 ;; This looks ugly when substitute-command-keys uses C-d instead d:
809 ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
810
3e1fb00f
RS
811 (let ((map (make-keymap)))
812 (suppress-keymap map)
813 (define-key map [mouse-2] 'dired-mouse-find-file-other-window)
814 ;; Commands to mark or flag certain categories of files
815 (define-key map "#" 'dired-flag-auto-save-files)
3e1fb00f 816 (define-key map "." 'dired-clean-directory)
3e1fb00f 817 (define-key map "~" 'dired-flag-backup-files)
84d3f6e8 818 (define-key map "&" 'dired-flag-garbage-files)
3e1fb00f
RS
819 ;; Upper case keys (except !) for operating on the marked files
820 (define-key map "A" 'dired-do-search)
821 (define-key map "C" 'dired-do-copy)
822 (define-key map "B" 'dired-do-byte-compile)
823 (define-key map "D" 'dired-do-delete)
824 (define-key map "G" 'dired-do-chgrp)
825 (define-key map "H" 'dired-do-hardlink)
826 (define-key map "L" 'dired-do-load)
827 (define-key map "M" 'dired-do-chmod)
828 (define-key map "O" 'dired-do-chown)
829 (define-key map "P" 'dired-do-print)
830 (define-key map "Q" 'dired-do-query-replace)
831 (define-key map "R" 'dired-do-rename)
832 (define-key map "S" 'dired-do-symlink)
833 (define-key map "X" 'dired-do-shell-command)
834 (define-key map "Z" 'dired-do-compress)
835 (define-key map "!" 'dired-do-shell-command)
836 ;; Comparison commands
837 (define-key map "=" 'dired-diff)
838 (define-key map "\M-=" 'dired-backup-diff)
839 ;; Tree Dired commands
840 (define-key map "\M-\C-?" 'dired-unmark-all-files)
841 (define-key map "\M-\C-d" 'dired-tree-down)
842 (define-key map "\M-\C-u" 'dired-tree-up)
843 (define-key map "\M-\C-n" 'dired-next-subdir)
844 (define-key map "\M-\C-p" 'dired-prev-subdir)
845 ;; move to marked files
846 (define-key map "\M-{" 'dired-prev-marked-file)
847 (define-key map "\M-}" 'dired-next-marked-file)
848 ;; Make all regexp commands share a `%' prefix:
849 ;; We used to get to the submap via a symbol dired-regexp-prefix,
850 ;; but that seems to serve little purpose, and copy-keymap
851 ;; does a better job without it.
852 (define-key map "%" nil)
853 (define-key map "%u" 'dired-upcase)
854 (define-key map "%l" 'dired-downcase)
855 (define-key map "%d" 'dired-flag-files-regexp)
856 (define-key map "%m" 'dired-mark-files-regexp)
857 (define-key map "%r" 'dired-do-rename-regexp)
858 (define-key map "%C" 'dired-do-copy-regexp)
859 (define-key map "%H" 'dired-do-hardlink-regexp)
860 (define-key map "%R" 'dired-do-rename-regexp)
861 (define-key map "%S" 'dired-do-symlink-regexp)
4a3ca6ed
RS
862 ;; Commands for marking and unmarking.
863 (define-key map "*" nil)
864 (define-key map "**" 'dired-mark-executables)
865 (define-key map "*/" 'dired-mark-directories)
866 (define-key map "*@" 'dired-mark-symlinks)
867 (define-key map "*%" 'dired-mark-files-regexp)
868 (define-key map "*c" 'dired-change-marks)
0609c998 869 (define-key map "*s" 'dired-mark-subdir-files)
4a3ca6ed
RS
870 (define-key map "*m" 'dired-mark)
871 (define-key map "*u" 'dired-unmark)
872 (define-key map "*?" 'dired-unmark-all-files)
534a6edf 873 (define-key map "*!" 'dired-unmark-all-marks)
4a3ca6ed
RS
874 (define-key map "*\177" 'dired-unmark-backward)
875 (define-key map "*\C-n" 'dired-next-marked-file)
876 (define-key map "*\C-p" 'dired-prev-marked-file)
3e1fb00f 877 ;; Lower keys for commands not operating on all the marked files
3e1fb00f
RS
878 (define-key map "d" 'dired-flag-file-deletion)
879 (define-key map "e" 'dired-find-file)
880 (define-key map "f" 'dired-find-file)
881 (define-key map "\C-m" 'dired-advertised-find-file)
882 (define-key map "g" 'revert-buffer)
883 (define-key map "h" 'describe-mode)
884 (define-key map "i" 'dired-maybe-insert-subdir)
885 (define-key map "k" 'dired-do-kill-lines)
886 (define-key map "l" 'dired-do-redisplay)
887 (define-key map "m" 'dired-mark)
888 (define-key map "n" 'dired-next-line)
889 (define-key map "o" 'dired-find-file-other-window)
890 (define-key map "\C-o" 'dired-display-file)
891 (define-key map "p" 'dired-previous-line)
da8a8438 892 (define-key map "q" 'quit-window)
3e1fb00f 893 (define-key map "s" 'dired-sort-toggle-or-edit)
e5f0841e 894 (define-key map "t" 'dired-do-toggle)
3e1fb00f
RS
895 (define-key map "u" 'dired-unmark)
896 (define-key map "v" 'dired-view-file)
897 (define-key map "x" 'dired-do-flagged-delete)
898 (define-key map "+" 'dired-create-directory)
899 ;; moving
900 (define-key map "<" 'dired-prev-dirline)
901 (define-key map ">" 'dired-next-dirline)
902 (define-key map "^" 'dired-up-directory)
903 (define-key map " " 'dired-next-line)
904 (define-key map "\C-n" 'dired-next-line)
905 (define-key map "\C-p" 'dired-previous-line)
906 (define-key map [down] 'dired-next-line)
907 (define-key map [up] 'dired-previous-line)
908 ;; hiding
909 (define-key map "$" 'dired-hide-subdir)
910 (define-key map "\M-$" 'dired-hide-all)
911 ;; misc
912 (define-key map "?" 'dired-summary)
913 (define-key map "\177" 'dired-unmark-backward)
914 (define-key map "\C-_" 'dired-undo)
915 (define-key map "\C-xu" 'dired-undo)
916
917 ;; Make menu bar items.
918
919 ;; Get rid of the Edit menu bar item to save space.
920 (define-key map [menu-bar edit] 'undefined)
921
922 (define-key map [menu-bar subdir]
923 (cons "Subdir" (make-sparse-keymap "Subdir")))
924
925 (define-key map [menu-bar subdir hide-all]
926 '("Hide All" . dired-hide-all))
927 (define-key map [menu-bar subdir hide-subdir]
928 '("Hide Subdir" . dired-hide-subdir))
929 (define-key map [menu-bar subdir tree-down]
930 '("Tree Down" . dired-tree-down))
931 (define-key map [menu-bar subdir tree-up]
932 '("Tree Up" . dired-tree-up))
933 (define-key map [menu-bar subdir up]
934 '("Up Directory" . dired-up-directory))
935 (define-key map [menu-bar subdir prev-subdir]
936 '("Prev Subdir" . dired-prev-subdir))
937 (define-key map [menu-bar subdir next-subdir]
938 '("Next Subdir" . dired-next-subdir))
939 (define-key map [menu-bar subdir prev-dirline]
940 '("Prev Dirline" . dired-prev-dirline))
941 (define-key map [menu-bar subdir next-dirline]
942 '("Next Dirline" . dired-next-dirline))
943 (define-key map [menu-bar subdir insert]
944 '("Insert This Subdir" . dired-maybe-insert-subdir))
945
946 (define-key map [menu-bar immediate]
947 (cons "Immediate" (make-sparse-keymap "Immediate")))
948
a3285795
RS
949 (define-key map [menu-bar immediate revert-buffer]
950 '("Update Buffer" . revert-buffer))
951
952 (define-key map [menu-bar immediate dashes]
953 '("--"))
954
3e1fb00f
RS
955 (define-key map [menu-bar immediate backup-diff]
956 '("Compare with Backup" . dired-backup-diff))
957 (define-key map [menu-bar immediate diff]
958 '("Diff" . dired-diff))
959 (define-key map [menu-bar immediate view]
960 '("View This File" . dired-view-file))
961 (define-key map [menu-bar immediate display]
962 '("Display in Other Window" . dired-display-file))
963 (define-key map [menu-bar immediate find-file-other-window]
964 '("Find in Other Window" . dired-find-file-other-window))
965 (define-key map [menu-bar immediate find-file]
966 '("Find This File" . dired-find-file))
967 (define-key map [menu-bar immediate create-directory]
968 '("Create Directory..." . dired-create-directory))
969
970 (define-key map [menu-bar regexp]
971 (cons "Regexp" (make-sparse-keymap "Regexp")))
972
973 (define-key map [menu-bar regexp downcase]
974 '("Downcase" . dired-downcase))
975 (define-key map [menu-bar regexp upcase]
976 '("Upcase" . dired-upcase))
977 (define-key map [menu-bar regexp hardlink]
978 '("Hardlink..." . dired-do-hardlink-regexp))
979 (define-key map [menu-bar regexp symlink]
980 '("Symlink..." . dired-do-symlink-regexp))
981 (define-key map [menu-bar regexp rename]
982 '("Rename..." . dired-do-rename-regexp))
983 (define-key map [menu-bar regexp copy]
984 '("Copy..." . dired-do-copy-regexp))
985 (define-key map [menu-bar regexp flag]
986 '("Flag..." . dired-flag-files-regexp))
987 (define-key map [menu-bar regexp mark]
988 '("Mark..." . dired-mark-files-regexp))
989
990 (define-key map [menu-bar mark]
991 (cons "Mark" (make-sparse-keymap "Mark")))
992
993 (define-key map [menu-bar mark prev]
994 '("Previous Marked" . dired-prev-marked-file))
995 (define-key map [menu-bar mark next]
996 '("Next Marked" . dired-next-marked-file))
997 (define-key map [menu-bar mark marks]
998 '("Change Marks..." . dired-change-marks))
999 (define-key map [menu-bar mark unmark-all]
534a6edf 1000 '("Unmark All" . dired-unmark-all-marks))
3e1fb00f
RS
1001 (define-key map [menu-bar mark symlinks]
1002 '("Mark Symlinks" . dired-mark-symlinks))
1003 (define-key map [menu-bar mark directories]
1004 '("Mark Directories" . dired-mark-directories))
1005 (define-key map [menu-bar mark directory]
1006 '("Mark Old Backups" . dired-clean-directory))
1007 (define-key map [menu-bar mark executables]
1008 '("Mark Executables" . dired-mark-executables))
84d3f6e8
RS
1009 (define-key map [menu-bar mark garbage-files]
1010 '("Flag Garbage Files" . dired-flag-garbage-files))
3e1fb00f
RS
1011 (define-key map [menu-bar mark backup-files]
1012 '("Flag Backup Files" . dired-flag-backup-files))
1013 (define-key map [menu-bar mark auto-save-files]
1014 '("Flag Auto-save Files" . dired-flag-auto-save-files))
1015 (define-key map [menu-bar mark deletion]
1016 '("Flag" . dired-flag-file-deletion))
1017 (define-key map [menu-bar mark unmark]
1018 '("Unmark" . dired-unmark))
1019 (define-key map [menu-bar mark mark]
1020 '("Mark" . dired-mark))
e5f0841e
KH
1021 (define-key map [menu-bar mark toggle-marks]
1022 '("Toggle Marks" . dired-do-toggle))
3e1fb00f
RS
1023
1024 (define-key map [menu-bar operate]
1025 (cons "Operate" (make-sparse-keymap "Operate")))
1026
1027 (define-key map [menu-bar operate query-replace]
1028 '("Query Replace in Files..." . dired-do-query-replace))
1029 (define-key map [menu-bar operate search]
1030 '("Search Files..." . dired-do-search))
1031 (define-key map [menu-bar operate chown]
1032 '("Change Owner..." . dired-do-chown))
1033 (define-key map [menu-bar operate chgrp]
1034 '("Change Group..." . dired-do-chgrp))
1035 (define-key map [menu-bar operate chmod]
1036 '("Change Mode..." . dired-do-chmod))
1037 (define-key map [menu-bar operate load]
1038 '("Load" . dired-do-load))
1039 (define-key map [menu-bar operate compile]
1040 '("Byte-compile" . dired-do-byte-compile))
1041 (define-key map [menu-bar operate compress]
1042 '("Compress" . dired-do-compress))
1043 (define-key map [menu-bar operate print]
1044 '("Print" . dired-do-print))
1045 (define-key map [menu-bar operate hardlink]
1046 '("Hardlink to..." . dired-do-hardlink))
1047 (define-key map [menu-bar operate symlink]
1048 '("Symlink to..." . dired-do-symlink))
1049 (define-key map [menu-bar operate command]
1050 '("Shell Command..." . dired-do-shell-command))
1051 (define-key map [menu-bar operate delete]
1052 '("Delete" . dired-do-delete))
1053 (define-key map [menu-bar operate rename]
1054 '("Rename to..." . dired-do-rename))
1055 (define-key map [menu-bar operate copy]
1056 '("Copy to..." . dired-do-copy))
1057
1058 (setq dired-mode-map map)))
fc53efda 1059\f
84fc2cfa
ER
1060;; Dired mode is suitable only for specially formatted data.
1061(put 'dired-mode 'mode-class 'special)
1062
492d2437
RS
1063(defun dired-mode (&optional dirname switches)
1064 "\
1065Mode for \"editing\" directory listings.
1066In dired, you are \"editing\" a list of the files in a directory and
1067 \(optionally) its subdirectories, in the format of `ls -lR'.
1068 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
1069\"Editing\" means that you can run shell commands on files, visit,
1070 compress, load or byte-compile them, change their file attributes
1071 and insert subdirectories into the same buffer. You can \"mark\"
1072 files for later commands or \"flag\" them for deletion, either file
1073 by file or all files matching certain criteria.
1074You can move using the usual cursor motion commands.\\<dired-mode-map>
1075Letters no longer insert themselves. Digits are prefix arguments.
1076Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
1077Type \\[dired-mark] to Mark a file or subdirectory for later commands.
1078 Most commands operate on the marked files and use the current file
1079 if no files are marked. Use a numeric prefix argument to operate on
1080 the next ARG (or previous -ARG if ARG<0) files, or just `1'
1081 to operate on the current file only. Prefix arguments override marks.
1082 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
1083 to see why something went wrong.
1084Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
1085Type \\[dired-unmark-backward] to back up one line and unflag.
1086Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
1087Type \\[dired-advertised-find-file] to Find the current line's file
1088 (or dired it in another buffer, if it is a directory).
1089Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
1090Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
1091Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
1092Type \\[dired-do-copy] to Copy files.
1093Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
1094Type \\[revert-buffer] to read all currently expanded directories again.
1095 This retains all marks and hides subdirs again that were hidden before.
1096SPC and DEL can be used to move down and up by lines.
1097
1098If dired ever gets confused, you can either type \\[revert-buffer] \
1099to read the
1100directories again, type \\[dired-do-redisplay] \
1101to relist a single or the marked files or a
1102subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
1103again for the directory tree.
1104
1105Customization variables (rename this buffer and type \\[describe-variable] on each line
1106for more info):
1107
1108 dired-listing-switches
1109 dired-trivial-filenames
1110 dired-shrink-to-fit
1111 dired-marker-char
1112 dired-del-marker
1113 dired-keep-marker-rename
1114 dired-keep-marker-copy
1115 dired-keep-marker-hardlink
1116 dired-keep-marker-symlink
1117
1118Hooks (use \\[describe-variable] to see their documentation):
1119
1120 dired-before-readin-hook
1121 dired-after-readin-hook
1122 dired-mode-hook
1123 dired-load-hook
1124
1125Keybindings:
84fc2cfa 1126\\{dired-mode-map}"
492d2437
RS
1127 ;; Not to be called interactively (e.g. dired-directory will be set
1128 ;; to default-directory, which is wrong with wildcards).
84fc2cfa 1129 (kill-all-local-variables)
84fc2cfa 1130 (use-local-map dired-mode-map)
492d2437
RS
1131 (dired-advertise) ; default-directory is already set
1132 (setq major-mode 'dired-mode
1133 mode-name "Dired"
3222085e 1134;; case-fold-search nil
492d2437
RS
1135 buffer-read-only t
1136 selective-display t ; for subdirectory hiding
982fa4e5 1137 mode-line-buffer-identification '("%17b"))
492d2437
RS
1138 (set (make-local-variable 'revert-buffer-function)
1139 (function dired-revert))
1140 (set (make-local-variable 'page-delimiter)
1141 "\n\n")
1142 (set (make-local-variable 'dired-directory)
1143 (or dirname default-directory))
1144 ;; list-buffers uses this to display the dir being edited in this buffer.
1145 (set (make-local-variable 'list-buffers-directory)
474ac5bb 1146 (expand-file-name dired-directory))
492d2437
RS
1147 (set (make-local-variable 'dired-actual-switches)
1148 (or switches dired-listing-switches))
4481ae96 1149 (set (make-local-variable 'font-lock-defaults) '(dired-font-lock-keywords t))
492d2437 1150 (dired-sort-other dired-actual-switches t)
84fc2cfa
ER
1151 (run-hooks 'dired-mode-hook))
1152\f
eb8c3be9 1153;; Idiosyncratic dired commands that don't deal with marks.
84fc2cfa 1154
84fc2cfa 1155(defun dired-summary ()
492d2437 1156 "Summarize basic Dired commands and show recent Dired errors."
84fc2cfa 1157 (interactive)
492d2437 1158 (dired-why)
84fc2cfa
ER
1159 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
1160 (message
50f74744 1161 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
84fc2cfa 1162
492d2437
RS
1163(defun dired-undo ()
1164 "Undo in a dired buffer.
1165This doesn't recover lost files, it just undoes changes in the buffer itself.
1166You can use it to recover marks, killed lines or subdirs.
1167In the latter case, you have to do \\[dired-build-subdir-alist] to
1168parse the buffer again."
1169 (interactive)
1170 (let (buffer-read-only)
1171 (undo)))
84fc2cfa
ER
1172
1173(defun dired-next-line (arg)
1174 "Move down lines then position at filename.
1175Optional prefix ARG says how many lines to move; default is one line."
1176 (interactive "p")
1177 (next-line arg)
1178 (dired-move-to-filename))
1179
1180(defun dired-previous-line (arg)
1181 "Move up lines then position at filename.
1182Optional prefix ARG says how many lines to move; default is one line."
1183 (interactive "p")
1184 (previous-line arg)
1185 (dired-move-to-filename))
1186
492d2437
RS
1187(defun dired-next-dirline (arg &optional opoint)
1188 "Goto ARG'th next directory file line."
1189 (interactive "p")
1190 (or opoint (setq opoint (point)))
1191 (if (if (> arg 0)
1192 (re-search-forward dired-re-dir nil t arg)
1193 (beginning-of-line)
1194 (re-search-backward dired-re-dir nil t (- arg)))
1195 (dired-move-to-filename) ; user may type `i' or `f'
1196 (goto-char opoint)
1197 (error "No more subdirectories")))
1198
1199(defun dired-prev-dirline (arg)
1200 "Goto ARG'th previous directory file line."
1201 (interactive "p")
1202 (dired-next-dirline (- arg)))
1203
ac1ce341 1204(defun dired-up-directory (&optional other-window)
492d2437
RS
1205 "Run dired on parent directory of current directory.
1206Find the parent directory either in this buffer or another buffer.
1207Creates a buffer if necessary."
ac1ce341 1208 (interactive "P")
492d2437
RS
1209 (let* ((dir (dired-current-directory))
1210 (up (file-name-directory (directory-file-name dir))))
1211 (or (dired-goto-file (directory-file-name dir))
7b2469ae
RS
1212 ;; Only try dired-goto-subdir if buffer has more than one dir.
1213 (and (cdr dired-subdir-alist)
492d2437
RS
1214 (dired-goto-subdir up))
1215 (progn
ac1ce341
EN
1216 (if other-window
1217 (dired-other-window up)
1218 (dired up))
492d2437 1219 (dired-goto-file dir)))))
84fc2cfa 1220
dbcb9389
RS
1221;; Force `f' rather than `e' in the mode doc:
1222(defalias 'dired-advertised-find-file 'dired-find-file)
53fd9d05 1223(defun dired-find-file ()
84fc2cfa 1224 "In dired, visit the file or directory named on this line."
53fd9d05 1225 (interactive)
c439687b
RS
1226 (let ((file-name (file-name-sans-versions (dired-get-filename) t)))
1227 (if (file-exists-p file-name)
53fd9d05 1228 (find-file file-name)
55642aa4
RS
1229 (if (file-symlink-p file-name)
1230 (error "File is a symlink to a nonexistent target")
1231 (error "File no longer exists; type `g' to update Dired buffer")))))
84fc2cfa 1232
dbcb9389
RS
1233(defun dired-mouse-find-file-other-window (event)
1234 "In dired, visit the file or directory name you click on."
1235 (interactive "e")
1236 (let (file)
1237 (save-excursion
1238 (set-buffer (window-buffer (posn-window (event-end event))))
1239 (save-excursion
1240 (goto-char (posn-point (event-end event)))
1241 (setq file (dired-get-filename))))
1242 (select-window (posn-window (event-end event)))
1243 (find-file-other-window (file-name-sans-versions file t))))
1244
84fc2cfa 1245(defun dired-view-file ()
492d2437
RS
1246 "In dired, examine a file in view mode, returning to dired when done.
1247When file is a directory, show it in this buffer if it is inserted;
1248otherwise, display it in another buffer."
84fc2cfa
ER
1249 (interactive)
1250 (if (file-directory-p (dired-get-filename))
7b2469ae
RS
1251 (or (and (cdr dired-subdir-alist)
1252 (dired-goto-subdir (dired-get-filename)))
492d2437 1253 (dired (dired-get-filename)))
715984d3 1254 (view-file (dired-get-filename))))
84fc2cfa
ER
1255
1256(defun dired-find-file-other-window ()
1257 "In dired, visit this file or directory in another window."
1258 (interactive)
c3554e95 1259 (find-file-other-window (file-name-sans-versions (dired-get-filename) t)))
ab67260b
RS
1260
1261(defun dired-display-file ()
1262 "In dired, display this file or directory in another window."
1263 (interactive)
c3554e95
RS
1264 (let ((file (file-name-sans-versions (dired-get-filename) t)))
1265 (display-buffer (find-file-noselect file))))
492d2437
RS
1266\f
1267;;; Functions for extracting and manipulating file names in dired buffers.
84fc2cfa
ER
1268
1269(defun dired-get-filename (&optional localp no-error-if-not-filep)
1270 "In dired, return name of file mentioned on this line.
1271Value returned normally includes the directory name.
492d2437
RS
1272Optional arg LOCALP with value `no-dir' means don't include directory
1273 name in result. A value of t means construct name relative to
1274 `default-directory', which still may contain slashes if in a subdirectory.
1275Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
1276 this line, otherwise an error occurs."
1277 (let (case-fold-search file p1 p2)
84fc2cfa 1278 (save-excursion
492d2437
RS
1279 (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
1280 (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
1281 ;; nil if no file on this line, but no-error-if-not-filep is t:
9888aa65
RS
1282 (if (setq file (and p1 p2 (buffer-substring p1 p2)))
1283 (progn
1284 ;; Get rid of the mouse-face property that file names have.
1285 (set-text-properties 0 (length file) nil file)
1286 ;; Unquote names quoted by ls or by dired-insert-directory.
1287 ;; Using read to unquote is much faster than substituting
1288 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
1289 (setq file
1290 (read
1291 (concat "\""
1292 ;; some ls -b don't escape quotes, argh!
1293 ;; This is not needed for GNU ls, though.
1294 (or (dired-string-replace-match
bc07e5cb 1295 "\\([^\\]\\|\\`\\)\"" file "\\1\\\\\"" nil t)
9888aa65
RS
1296 file)
1297 "\"")))))
be417b22 1298 (and file buffer-file-coding-system
05dd46c2 1299 (not file-name-coding-system)
0457b5e4 1300 (not default-file-name-coding-system)
be417b22 1301 (setq file (encode-coding-string file buffer-file-coding-system)))
492d2437
RS
1302 (if (eq localp 'no-dir)
1303 file
1304 (and file (concat (dired-current-directory localp) file)))))
1305
437fe5ae
RS
1306(defun dired-string-replace-match (regexp string newtext
1307 &optional literal global)
1308 "Replace first match of REGEXP in STRING with NEWTEXT.
1309If it does not match, nil is returned instead of the new string.
1310Optional arg LITERAL means to take NEWTEXT literally.
1311Optional arg GLOBAL means to replace all matches."
1312 (if global
856321e2
RS
1313 (let ((start 0))
1314 (while (string-match regexp string start)
1315 (let ((from-end (- (length string) (match-end 0))))
1316 (setq string (replace-match newtext t literal string))
1317 (setq start (- (length string) from-end))))
1318 string)
437fe5ae
RS
1319 (if (not (string-match regexp string 0))
1320 nil
856321e2 1321 (replace-match newtext t literal string))))
437fe5ae 1322
492d2437
RS
1323(defun dired-make-absolute (file &optional dir)
1324 ;;"Convert FILE (a pathname relative to DIR) to an absolute pathname."
1325 ;; We can't always use expand-file-name as this would get rid of `.'
1326 ;; or expand in / instead default-directory if DIR=="".
1327 ;; This should be good enough for ange-ftp, but might easily be
1328 ;; redefined (for VMS?).
1329 ;; It should be reasonably fast, though, as it is called in
1330 ;; dired-get-filename.
1331 (concat (or dir default-directory) file))
1332
827dc60d
RS
1333(defun dired-make-relative (file &optional dir ignore)
1334 "Convert FILE (an absolute file name) to a name relative to DIR.
1335If this is impossible, return FILE unchanged.
1336DIR must be a directory name, not a file name."
492d2437 1337 (or dir (setq dir default-directory))
7425bbff
RS
1338 ;; This case comes into play if default-directory is set to
1339 ;; use ~.
1340 (if (and (> (length dir) 0) (= (aref dir 0) ?~))
1341 (setq dir (expand-file-name dir)))
492d2437
RS
1342 (if (string-match (concat "^" (regexp-quote dir)) file)
1343 (substring file (match-end 0))
827dc60d
RS
1344;;; (or no-error
1345;;; (error "%s: not in directory tree growing at %s" file dir))
1346 file))
492d2437
RS
1347\f
1348;;; Functions for finding the file name in a dired buffer line.
1349
c4a7b2a4 1350(defvar dired-move-to-filename-regexp
2708e59e 1351 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
6a5c00da
PE
1352 ;; In some locales, month abbreviations are as short as 2 letters,
1353 ;; and they can be padded on the right with spaces.
1354 (month (concat l l "+ *"))
2708e59e 1355 ;; Recognize any non-ASCII character.
33165c24 1356 ;; The purpose is to match a Kanji character.
2708e59e
RS
1357 (k "[^\0-\177]")
1358 ;; (k "[^\x00-\x7f\x80-\xff]")
32184bdb
PE
1359 (s " ")
1360 (yyyy "[0-9][0-9][0-9][0-9]")
1361 (mm "[ 0-1][0-9]")
1362 (dd "[ 0-3][0-9]")
1363 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
33165c24 1364 (western (concat "\\(" month s dd "\\|" dd s month "\\)"
001632ab 1365 s "\\(" HH:MM "\\|" s yyyy "\\|" yyyy s "\\)"))
6a5c00da
PE
1366 (japanese (concat mm k s dd k s "\\(" s HH:MM "\\|" yyyy k "\\)")))
1367 ;; Require the previous column to end in a digit.
1368 ;; This avoids recognizing `1 may 1997' as a date in the line:
1369 ;; -r--r--r-- 1 may 1997 1168 Oct 19 16:49 README
1370 (concat "[0-9]" s "\\(" western "\\|" japanese "\\)" s))
1371 "Regular expression to match up to the file name in a directory listing.
1f97e2fc
PE
1372The default value is designed to recognize dates and times
1373regardless of the language.")
9eabb7d5 1374
23e217f6
RS
1375(defvar dired-permission-flags-regexp
1376 "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
1377 "Regular expression to match the permission flags in `ls -l'.")
1378
492d2437
RS
1379;; Move to first char of filename on this line.
1380;; Returns position (point) or nil if no filename on this line."
1381(defun dired-move-to-filename (&optional raise-error eol)
1382 ;; This is the UNIX version.
1383 (or eol (setq eol (progn (end-of-line) (point))))
1384 (beginning-of-line)
d90490d8
RS
1385 (if (re-search-forward dired-move-to-filename-regexp eol t)
1386 (goto-char (match-end 0))
1387 (if raise-error
1388 (error "No file on this line"))))
492d2437
RS
1389
1390(defun dired-move-to-end-of-filename (&optional no-error)
1391 ;; Assumes point is at beginning of filename,
1392 ;; thus the rwx bit re-search-backward below will succeed in *this*
1393 ;; line if at all. So, it should be called only after
1394 ;; (dired-move-to-filename t).
1395 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
1396 ;; This is the UNIX version.
1397 (let (opoint file-type executable symlink hidden case-fold-search used-F eol)
1398 ;; case-fold-search is nil now, so we can test for capital F:
1399 (setq used-F (string-match "F" dired-actual-switches)
1400 opoint (point)
1401 eol (save-excursion (end-of-line) (point))
1402 hidden (and selective-display
1403 (save-excursion (search-forward "\r" eol t))))
1404 (if hidden
1405 nil
1406 (save-excursion;; Find out what kind of file this is:
1407 ;; Restrict perm bits to be non-blank,
1408 ;; otherwise this matches one char to early (looking backward):
1409 ;; "l---------" (some systems make symlinks that way)
1410 ;; "----------" (plain file with zero perms)
1411 (if (re-search-backward
23e217f6 1412 dired-permission-flags-regexp nil t)
492d2437
RS
1413 (setq file-type (char-after (match-beginning 1))
1414 symlink (eq file-type ?l)
1415 ;; Only with -F we need to know whether it's an executable
1416 executable (and
1417 used-F
1418 (string-match
1419 "[xst]";; execute bit set anywhere?
1420 (concat
1421 (buffer-substring (match-beginning 2)
1422 (match-end 2))
1423 (buffer-substring (match-beginning 3)
1424 (match-end 3))
1425 (buffer-substring (match-beginning 4)
1426 (match-end 4))))))
1427 (or no-error (error "No file on this line"))))
1428 ;; Move point to end of name:
1429 (if symlink
1430 (if (search-forward " ->" eol t)
1431 (progn
1432 (forward-char -3)
1433 (and used-F
1434 dired-ls-F-marks-symlinks
1435 (eq (preceding-char) ?@);; did ls really mark the link?
1436 (forward-char -1))))
1437 (goto-char eol);; else not a symbolic link
1438 ;; ls -lF marks dirs, sockets and executables with exactly one
1439 ;; trailing character. (Executable bits on symlinks ain't mean
1440 ;; a thing, even to ls, but we know it's not a symlink.)
1441 (and used-F
1442 (or (memq file-type '(?d ?s))
1443 executable)
1444 (forward-char -1))))
1445 (or no-error
1446 (not (eq opoint (point)))
1447 (error (if hidden
1448 (substitute-command-keys
1449 "File line is hidden, type \\[dired-hide-subdir] to unhide")
1450 "No file on this line")))
1451 (if (eq opoint (point))
1452 nil
1453 (point))))
1454
1455\f
1456;; Keeping Dired buffers in sync with the filesystem and with each other
1457
1458(defvar dired-buffers nil
1459 ;; Enlarged by dired-advertise
1460 ;; Queried by function dired-buffers-for-dir. When this detects a
1461 ;; killed buffer, it is removed from this list.
38819778 1462 "Alist of expanded directories and their associated dired buffers.")
492d2437 1463
ef746164 1464(defun dired-buffers-for-dir (dir &optional file)
492d2437 1465;; Return a list of buffers that dired DIR (top level or in-situ subdir).
ef746164
RS
1466;; If FILE is non-nil, include only those whose wildcard pattern (if any)
1467;; matches FILE.
492d2437
RS
1468;; The list is in reverse order of buffer creation, most recent last.
1469;; As a side effect, killed dired buffers for DIR are removed from
1470;; dired-buffers.
1471 (setq dir (file-name-as-directory dir))
ef746164 1472 (let ((alist dired-buffers) result elt buf pattern)
492d2437 1473 (while alist
38819778
RS
1474 (setq elt (car alist)
1475 buf (cdr elt))
1476 (if (buffer-name buf)
1477 (if (dired-in-this-tree dir (car elt))
ef746164
RS
1478 (with-current-buffer buf
1479 (and (assoc dir dired-subdir-alist)
1480 (or (null file)
1481 (let ((wildcards
1482 (file-name-nondirectory dired-directory)))
1483 (or (= 0 (length wildcards))
1484 (string-match (dired-glob-regexp wildcards)
1485 file))))
1486 (setq result (cons buf result)))))
38819778
RS
1487 ;; else buffer is killed - clean up:
1488 (setq dired-buffers (delq elt dired-buffers)))
492d2437
RS
1489 (setq alist (cdr alist)))
1490 result))
1491
ef746164
RS
1492(defun dired-glob-regexp (pattern)
1493 "Convert glob-pattern PATTERN to a regular expression."
1494 (let ((matched-in-pattern 0) ;; How many chars of PATTERN we've handled.
1495 regexp)
1496 (while (string-match "[[?*]" pattern matched-in-pattern)
1497 (let ((op-end (match-end 0))
1498 (next-op (aref pattern (match-beginning 0))))
1499 (setq regexp (concat regexp
1500 (regexp-quote
1501 (substring pattern matched-in-pattern
1502 (match-beginning 0)))))
1503 (cond ((= next-op ??)
1504 (setq regexp (concat regexp "."))
1505 (setq matched-in-pattern op-end))
1506 ((= next-op ?\[)
1507 ;; Fails to handle ^ yet ????
1508 (let* ((set-start (match-beginning 0))
1509 (set-cont
1510 (if (= (aref pattern (1+ set-start)) ?^)
1511 (+ 3 set-start)
1512 (+ 2 set-start)))
1513 (set-end (string-match "]" pattern set-cont))
1514 (set (substring pattern set-start (1+ set-end))))
1515 (setq regexp (concat regexp set))
1516 (setq matched-in-pattern (1+ set-end))))
1517 ((= next-op ?*)
1518 (setq regexp (concat regexp ".*"))
1519 (setq matched-in-pattern op-end)))))
1520 (concat "\\`"
1521 regexp
1522 (regexp-quote
1523 (substring pattern matched-in-pattern))
1524 "\\'")))
1525
1526
1527
492d2437
RS
1528(defun dired-advertise ()
1529 ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
1530 ;; With wildcards we actually advertise too much.
38819778
RS
1531 (let ((expanded-default (expand-file-name default-directory)))
1532 (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
1533 t ; we have already advertised ourselves
1534 (setq dired-buffers
1535 (cons (cons expanded-default (current-buffer))
1536 dired-buffers)))))
492d2437
RS
1537
1538(defun dired-unadvertise (dir)
1539 ;; Remove DIR from the buffer alist in variable dired-buffers.
1540 ;; This has the effect of removing any buffer whose main directory is DIR.
1541 ;; It does not affect buffers in which DIR is a subdir.
1542 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
1543 (setq dired-buffers
38819778 1544 (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
492d2437
RS
1545\f
1546;; Tree Dired
1547
1548;;; utility functions
1549
1550(defun dired-in-this-tree (file dir)
1551 ;;"Is FILE part of the directory tree starting at DIR?"
1552 (let (case-fold-search)
38819778 1553 (string-match (concat "^" (regexp-quote dir)) file)))
492d2437
RS
1554
1555(defun dired-normalize-subdir (dir)
1556 ;; Prepend default-directory to DIR if relative path name.
1557 ;; dired-get-filename must be able to make a valid filename from a
1558 ;; file and its directory DIR.
1559 (file-name-as-directory
1560 (if (file-name-absolute-p dir)
1561 dir
1562 (expand-file-name dir default-directory))))
1563
1564(defun dired-get-subdir ()
1565 ;;"Return the subdir name on this line, or nil if not on a headerline."
1566 ;; Look up in the alist whether this is a headerline.
1567 (save-excursion
1568 (let ((cur-dir (dired-current-directory)))
1569 (beginning-of-line) ; alist stores b-o-l positions
1570 (and (zerop (- (point)
1571 (dired-get-subdir-min (assoc cur-dir
1572 dired-subdir-alist))))
1573 cur-dir))))
1574
1575;(defun dired-get-subdir-min (elt)
1576; (cdr elt))
1577;; can't use macro, must be redefinable for other alist format in dired-nstd.
62f61df0 1578(defalias 'dired-get-subdir-min 'cdr)
492d2437
RS
1579
1580(defun dired-get-subdir-max (elt)
84fc2cfa 1581 (save-excursion
492d2437
RS
1582 (goto-char (dired-get-subdir-min elt))
1583 (dired-subdir-max)))
1584
1585(defun dired-clear-alist ()
1586 (while dired-subdir-alist
1587 (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
1588 (setq dired-subdir-alist (cdr dired-subdir-alist))))
1589
c9d59fc2
RS
1590(defun dired-subdir-index (dir)
1591 ;; Return an index into alist for use with nth
1592 ;; for the sake of subdir moving commands.
1593 (let (found (index 0) (alist dired-subdir-alist))
1594 (while alist
1595 (if (string= dir (car (car alist)))
1596 (setq alist nil found t)
1597 (setq alist (cdr alist) index (1+ index))))
1598 (if found index nil)))
1599
1600(defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
1601 "Go to next subdirectory, regardless of level."
1602 ;; Use 0 arg to go to this directory's header line.
1603 ;; NO-SKIP prevents moving to end of header line, returning whatever
1604 ;; position was found in dired-subdir-alist.
1605 (interactive "p")
1606 (let ((this-dir (dired-current-directory))
1607 pos index)
1608 ;; nth with negative arg does not return nil but the first element
1609 (setq index (- (dired-subdir-index this-dir) arg))
1610 (setq pos (if (>= index 0)
1611 (dired-get-subdir-min (nth index dired-subdir-alist))))
1612 (if pos
1613 (progn
1614 (goto-char pos)
1615 (or no-skip (skip-chars-forward "^\n\r"))
1616 (point))
1617 (if no-error-if-not-found
1618 nil ; return nil if not found
1619 (error "%s directory" (if (> arg 0) "Last" "First"))))))
1620
492d2437
RS
1621(defun dired-build-subdir-alist ()
1622 "Build `dired-subdir-alist' by parsing the buffer.
1623Returns the new value of the alist."
1624 (interactive)
1625 (dired-clear-alist)
1626 (save-excursion
3222085e
BF
1627 (let ((count 0)
1628 (buffer-read-only nil)
1629 new-dir-name)
84fc2cfa 1630 (goto-char (point-min))
492d2437 1631 (setq dired-subdir-alist nil)
acf310c9
RS
1632 (while (and (re-search-forward dired-subdir-regexp nil t)
1633 ;; Avoid taking a file name ending in a colon
1634 ;; as a subdir name.
1635 (not (save-excursion
1636 (goto-char (match-beginning 0))
1637 (beginning-of-line)
1638 (forward-char 2)
46680faf 1639 (save-match-data (looking-at dired-re-perms)))))
3222085e
BF
1640 (save-excursion
1641 (goto-char (match-beginning 1))
1642 (setq new-dir-name
1643 (expand-file-name (buffer-substring (point) (match-end 1))))
1644 (delete-region (point) (match-end 1))
1645 (insert new-dir-name))
492d2437 1646 (setq count (1+ count))
3222085e
BF
1647 (dired-alist-add-1 new-dir-name
1648 ;; Place a sub directory boundary between lines.
1649 (save-excursion
1650 (goto-char (match-beginning 0))
1651 (beginning-of-line)
1652 (point-marker))))
c9d59fc2
RS
1653 (if (> count 1)
1654 (message "Buffer includes %d directories" count))
492d2437
RS
1655 ;; We don't need to sort it because it is in buffer order per
1656 ;; constructionem. Return new alist:
1657 dired-subdir-alist)))
1658
1659(defun dired-alist-add-1 (dir new-marker)
1660 ;; Add new DIR at NEW-MARKER. Don't sort.
1661 (setq dired-subdir-alist
1662 (cons (cons (dired-normalize-subdir dir) new-marker)
1663 dired-subdir-alist)))
1664
1665(defun dired-goto-next-nontrivial-file ()
1666 ;; Position point on first nontrivial file after point.
1667 (dired-goto-next-file);; so there is a file to compare with
1668 (if (stringp dired-trivial-filenames)
1669 (while (and (not (eobp))
1670 (string-match dired-trivial-filenames
1671 (file-name-nondirectory
1672 (or (dired-get-filename nil t) ""))))
1673 (forward-line 1)
1674 (dired-move-to-filename))))
1675
1676(defun dired-goto-next-file ()
1677 (let ((max (1- (dired-subdir-max))))
1678 (while (and (not (dired-move-to-filename)) (< (point) max))
1679 (forward-line 1))))
1680
1681(defun dired-goto-file (file)
1682 "Go to file line of FILE in this dired buffer."
1683 ;; Return value of point on success, else nil.
1684 ;; FILE must be an absolute pathname.
1685 ;; Loses if FILE contains control chars like "\007" for which ls
1686 ;; either inserts "?" or "\\007" into the buffer, so we won't find
1687 ;; it in the buffer.
1688 (interactive
1689 (prog1 ; let push-mark display its message
1690 (list (expand-file-name
1691 (read-file-name "Goto file: "
1692 (dired-current-directory))))
1693 (push-mark)))
1694 (setq file (directory-file-name file)) ; does no harm if no directory
1695 (let (found case-fold-search dir)
1696 (setq dir (or (file-name-directory file)
1697 (error "Need absolute pathname for %s" file)))
1698 (save-excursion
1699 ;; The hair here is to get the result of dired-goto-subdir
1700 ;; without really calling it if we don't have any subdirs.
233993a3 1701 (if (if (string= dir (expand-file-name default-directory))
492d2437 1702 (goto-char (point-min))
7b2469ae 1703 (and (cdr dired-subdir-alist)
492d2437
RS
1704 (dired-goto-subdir dir)))
1705 (let ((base (file-name-nondirectory file))
1706 (boundary (dired-subdir-max)))
1707 (while (and (not found)
1708 ;; filenames are preceded by SPC, this makes
1709 ;; the search faster (e.g. for the filename "-"!).
1710 (search-forward (concat " " base) boundary 'move))
1711 ;; Match could have BASE just as initial substring or
1712 ;; or in permission bits or date or
1713 ;; not be a proper filename at all:
1714 (if (equal base (dired-get-filename 'no-dir t))
1715 ;; Must move to filename since an (actually
1716 ;; correct) match could have been elsewhere on the
1717 ;; ;; line (e.g. "-" would match somewhere in the
1718 ;; permission bits).
55e86af6
RS
1719 (setq found (dired-move-to-filename))
1720 ;; If this isn't the right line, move forward to avoid
1721 ;; trying this line again.
1722 (forward-line 1))))))
492d2437
RS
1723 (and found
1724 ;; return value of point (i.e., FOUND):
1725 (goto-char found))))
1726
1727(defun dired-initial-position (dirname)
1728 ;; Where point should go in a new listing of DIRNAME.
1729 ;; Point assumed at beginning of new subdir line.
1730 ;; You may redefine this function as you wish, e.g. like in dired-x.el.
1731 (end-of-line)
1732 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
1733\f
1734;; These are hooks which make tree dired work.
1735;; They are in this file because other parts of dired need to call them.
1736;; But they don't call the rest of tree dired unless there are subdirs loaded.
1737
1738;; This function is called for each retrieved filename.
1739;; It could stand to be faster, though it's mostly function call
1740;; overhead. Avoiding the function call seems to save about 10% in
1741;; dired-get-filename. Make it a defsubst?
1742(defun dired-current-directory (&optional localp)
1743 "Return the name of the subdirectory to which this line belongs.
1744This returns a string with trailing slash, like `default-directory'.
1745Optional argument means return a file name relative to `default-directory'."
1746 (let ((here (point))
1747 (alist (or dired-subdir-alist
1748 ;; probably because called in a non-dired buffer
1749 (error "No subdir-alist in %s" (current-buffer))))
1750 elt dir)
1751 (while alist
1752 (setq elt (car alist)
1753 dir (car elt)
1754 ;; use `<=' (not `<') as subdir line is part of subdir
1755 alist (if (<= (dired-get-subdir-min elt) here)
1756 nil ; found
1757 (cdr alist))))
1758 (if localp
1759 (dired-make-relative dir default-directory)
1760 dir)))
1761
1762;; Subdirs start at the beginning of their header lines and end just
1763;; before the beginning of the next header line (or end of buffer).
1764
1765(defun dired-subdir-max ()
1766 (save-excursion
7b2469ae 1767 (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
492d2437
RS
1768 (point-max)
1769 (point))))
1770\f
1771;; Deleting files
1772
7da0e9b4
RS
1773(defun dired-do-flagged-delete (&optional nomessage)
1774 "In dired, delete the files flagged for deletion.
1775If NOMESSAGE is non-nil, we don't display any message
1776if there are no flagged files."
492d2437
RS
1777 (interactive)
1778 (let* ((dired-marker-char dired-del-marker)
1779 (regexp (dired-marker-regexp))
1780 case-fold-search)
1781 (if (save-excursion (goto-char (point-min))
1782 (re-search-forward regexp nil t))
1783 (dired-internal-do-deletions
1784 ;; this can't move point since ARG is nil
1785 (dired-map-over-marks (cons (dired-get-filename) (point))
1786 nil)
1787 nil)
7da0e9b4
RS
1788 (or nomessage
1789 (message "(No deletions requested)")))))
492d2437
RS
1790
1791(defun dired-do-delete (&optional arg)
1792 "Delete all marked (or next ARG) files."
1793 ;; This is more consistent with the file marking feature than
1794 ;; dired-do-flagged-delete.
1795 (interactive "P")
1796 (dired-internal-do-deletions
1797 ;; this may move point if ARG is an integer
1798 (dired-map-over-marks (cons (dired-get-filename) (point))
1799 arg)
1800 arg))
1801
1802(defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
1803
1804(defun dired-internal-do-deletions (l arg)
1805 ;; L is an alist of files to delete, with their buffer positions.
1806 ;; ARG is the prefix arg.
1807 ;; Filenames are absolute (VMS needs this for logical search paths).
1808 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
1809 ;; That way as changes are made in the buffer they do not shift the
1810 ;; lines still to be changed, so the (point) values in L stay valid.
1811 ;; Also, for subdirs in natural order, a subdir's files are deleted
1812 ;; before the subdir itself - the other way around would not work.
1813 (let ((files (mapcar (function car) l))
1814 (count (length l))
1815 (succ 0))
1816 ;; canonicalize file list for pop up
1817 (setq files (nreverse (mapcar (function dired-make-relative) files)))
1818 (if (dired-mark-pop-up
1819 " *Deletions*" 'delete files dired-deletion-confirmer
1820 (format "Delete %s " (dired-mark-prompt arg files)))
84fc2cfa 1821 (save-excursion
492d2437
RS
1822 (let (failures);; files better be in reverse order for this loop!
1823 (while l
1824 (goto-char (cdr (car l)))
1825 (let (buffer-read-only)
1826 (condition-case err
1827 (let ((fn (car (car l))))
1828 ;; This test is equivalent to
1829 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
1830 ;; but more efficient
1831 (if (eq t (car (file-attributes fn)))
ab67260b 1832 (delete-directory fn)
492d2437
RS
1833 (delete-file fn))
1834 ;; if we get here, removing worked
1835 (setq succ (1+ succ))
1836 (message "%s of %s deletions" succ count)
1837 (delete-region (progn (beginning-of-line) (point))
1838 (progn (forward-line 1) (point)))
1839 (dired-clean-up-after-deletion fn))
1840 (error;; catch errors from failed deletions
1841 (dired-log "%s\n" err)
1842 (setq failures (cons (car (car l)) failures)))))
1843 (setq l (cdr l)))
1844 (if (not failures)
1845 (message "%d deletion%s done" count (dired-plural-s count))
1846 (dired-log-summary
1847 (format "%d of %d deletion%s failed"
1848 (length failures) count
1849 (dired-plural-s count))
1850 failures))))
1851 (message "(No deletions performed)")))
1852 (dired-move-to-filename))
1853
1854;; This is a separate function for the sake of dired-x.el.
1855(defun dired-clean-up-after-deletion (fn)
1856 ;; Clean up after a deleted file or directory FN.
7b2469ae
RS
1857 (save-excursion (and (cdr dired-subdir-alist)
1858 (dired-goto-subdir fn)
492d2437
RS
1859 (dired-kill-subdir))))
1860\f
1861;; Confirmation
1862
1863(defun dired-marker-regexp ()
1864 (concat "^" (regexp-quote (char-to-string dired-marker-char))))
1865
1866(defun dired-plural-s (count)
1867 (if (= 1 count) "" "s"))
1868
1869(defun dired-mark-prompt (arg files)
1870 ;; Return a string for use in a prompt, either the current file
1871 ;; name, or the marker and a count of marked files.
1872 (let ((count (length files)))
1873 (if (= count 1)
1874 (car files)
1875 ;; more than 1 file:
1876 (if (integerp arg)
1877 ;; abs(arg) = count
1878 ;; Perhaps this is nicer, but it also takes more screen space:
1879 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
1880 ;; count)
1881 (format "[next %d files]" arg)
1882 (format "%c [%d files]" dired-marker-char count)))))
1883
1884(defun dired-pop-to-buffer (buf)
1885 ;; Pop up buffer BUF.
1886 ;; If dired-shrink-to-fit is t, make its window fit its contents.
1887 (if (not dired-shrink-to-fit)
1888 (pop-to-buffer (get-buffer-create buf))
1889 ;; let window shrink to fit:
1890 (let ((window (selected-window))
1891 target-lines w2)
1892 (cond ;; if split-window-threshold is enabled, use the largest window
1893 ((and (> (window-height (setq w2 (get-largest-window)))
1894 split-height-threshold)
f98955ea 1895 (= (frame-width) (window-width w2)))
492d2437
RS
1896 (setq window w2))
1897 ;; if the least-recently-used window is big enough, use it
1898 ((and (> (window-height (setq w2 (get-lru-window)))
1899 (* 2 window-min-height))
f98955ea 1900 (= (frame-width) (window-width w2)))
492d2437
RS
1901 (setq window w2)))
1902 (save-excursion
1903 (set-buffer buf)
1904 (goto-char (point-max))
1905 (skip-chars-backward "\n\r\t ")
63ea14a5
RS
1906 (setq target-lines (count-lines (point-min) (point)))
1907 ;; Don't forget to count the last line.
1908 (if (not (bolp))
1909 (setq target-lines (1+ target-lines))))
492d2437 1910 (if (<= (window-height window) (* 2 window-min-height))
f98955ea 1911 ;; At this point, every window on the frame is too small to split.
492d2437
RS
1912 (setq w2 (display-buffer buf))
1913 (setq w2 (split-window window
1914 (max window-min-height
1915 (- (window-height window)
1916 (1+ (max window-min-height target-lines)))))))
1917 (set-window-buffer w2 buf)
1918 (if (< (1- (window-height w2)) target-lines)
1919 (progn
1920 (select-window w2)
1921 (enlarge-window (- target-lines (1- (window-height w2))))))
1922 (set-window-start w2 1)
1923 )))
1924
1925(defvar dired-no-confirm nil
0347d069
SM
1926 "A list of symbols for commands dired should not confirm.
1927Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress',
1928`copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink' and
1929`uncompress'.")
492d2437
RS
1930
1931(defun dired-mark-pop-up (bufname op-symbol files function &rest args)
1932 ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
1933 ;;Return FUNCTION's result on ARGS after popping up a window (in a buffer
1934 ;;named BUFNAME, nil gives \" *Marked Files*\") showing the marked
1935 ;;files. Uses function `dired-pop-to-buffer' to do that.
1936 ;; FUNCTION should not manipulate files.
1937 ;; It should only read input (an argument or confirmation).
1938 ;;The window is not shown if there is just one file or
1939 ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
1940 ;;FILES is the list of marked files."
1941 (or bufname (setq bufname " *Marked Files*"))
0347d069
SM
1942 (if (or (eq dired-no-confirm t)
1943 (memq op-symbol dired-no-confirm)
492d2437
RS
1944 (= (length files) 1))
1945 (apply function args)
1946 (save-excursion
1947 (set-buffer (get-buffer-create bufname))
1948 (erase-buffer)
a07e7c4a
RS
1949 (dired-format-columns-of-files files)
1950 (remove-text-properties (point-min) (point-max) '(mouse-face)))
492d2437
RS
1951 (save-window-excursion
1952 (dired-pop-to-buffer bufname)
1953 (apply function args))))
1954
1955(defun dired-format-columns-of-files (files)
1956 ;; Files should be in forward order for this loop.
1957 ;; i.e., (car files) = first file in buffer.
1958 ;; Returns the number of lines used.
1959 (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files))))
1960 (width (- (window-width (selected-window)) 2))
1961 (columns (max 1 (/ width maxlen)))
1962 (nfiles (length files))
1963 (rows (+ (/ nfiles columns)
1964 (if (zerop (% nfiles columns)) 0 1)))
1965 (i 0)
1966 (j 0))
1967 (setq files (nconc (copy-sequence files) ; fill up with empty fns
1968 (make-list (- (* columns rows) nfiles) "")))
1969 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
1970 (while (< j rows)
1971 (while (< i columns)
1972 (indent-to (* i maxlen))
1973 (insert (car files))
1974 (setq files (nthcdr rows files)
1975 i (1+ i)))
1976 (insert "\n")
1977 (setq i 0
1978 j (1+ j)
1979 files (cdr files)))
1980 rows))
1981\f
1982;; Commands to mark or flag file(s) at or near current line.
1983
1984(defun dired-repeat-over-lines (arg function)
1985 ;; This version skips non-file lines.
cfe89c4f 1986 (let ((pos (make-marker)))
492d2437 1987 (beginning-of-line)
cfe89c4f
RS
1988 (while (and (> arg 0) (not (eobp)))
1989 (setq arg (1- arg))
1990 (beginning-of-line)
1991 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
1992 (save-excursion
1993 (forward-line 1)
1994 (move-marker pos (1+ (point))))
1995 (save-excursion (funcall function))
1996 ;; Advance to the next line--actually, to the line that *was* next.
1997 ;; (If FUNCTION inserted some new lines in between, skip them.)
1998 (goto-char pos))
1999 (while (and (< arg 0) (not (bobp)))
2000 (setq arg (1+ arg))
2001 (forward-line -1)
2002 (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
2003 (beginning-of-line)
2004 (save-excursion (funcall function)))
2005 (move-marker pos nil)
2006 (dired-move-to-filename)))
492d2437
RS
2007
2008(defun dired-between-files ()
2009 ;; Point must be at beginning of line
2010 ;; Should be equivalent to (save-excursion (not (dired-move-to-filename)))
2011 ;; but is about 1.5..2.0 times as fast. (Actually that's not worth it)
2012 (or (looking-at "^$\\|^. *$\\|^. total\\|^. wildcard")
a8004e4b
RS
2013 (and (looking-at dired-subdir-regexp)
2014 (save-excursion (not (dired-move-to-filename))))))
492d2437
RS
2015
2016(defun dired-next-marked-file (arg &optional wrap opoint)
2017 "Move to the next marked file, wrapping around the end of the buffer."
2018 (interactive "p\np")
2019 (or opoint (setq opoint (point)));; return to where interactively started
2020 (if (if (> arg 0)
2021 (re-search-forward dired-re-mark nil t arg)
2022 (beginning-of-line)
2023 (re-search-backward dired-re-mark nil t (- arg)))
2024 (dired-move-to-filename)
2025 (if (null wrap)
2026 (progn
2027 (goto-char opoint)
2028 (error "No next marked file"))
2029 (message "(Wraparound for next marked file)")
2030 (goto-char (if (> arg 0) (point-min) (point-max)))
2031 (dired-next-marked-file arg nil opoint))))
2032
2033(defun dired-prev-marked-file (arg &optional wrap)
2034 "Move to the previous marked file, wrapping around the end of the buffer."
2035 (interactive "p\np")
2036 (dired-next-marked-file (- arg) wrap))
2037
2038(defun dired-file-marker (file)
2039 ;; Return FILE's marker, or nil if unmarked.
2040 (save-excursion
2041 (and (dired-goto-file file)
2042 (progn
2043 (beginning-of-line)
2044 (if (not (equal ?\040 (following-char)))
2045 (following-char))))))
2046
2047(defun dired-mark-files-in-region (start end)
2048 (let (buffer-read-only)
2049 (if (> start end)
2050 (error "start > end"))
2051 (goto-char start) ; assumed at beginning of line
2052 (while (< (point) end)
2053 ;; Skip subdir line and following garbage like the `total' line:
2054 (while (and (< (point) end) (dired-between-files))
2055 (forward-line 1))
2056 (if (and (not (looking-at dired-re-dot))
2057 (dired-get-filename nil t))
2058 (progn
2059 (delete-char 1)
2060 (insert dired-marker-char)))
2061 (forward-line 1))))
2062
2063(defun dired-mark (arg)
2064 "Mark the current (or next ARG) files.
2065If on a subdir headerline, mark all its files except `.' and `..'.
2066
2067Use \\[dired-unmark-all-files] to remove all marks
2068and \\[dired-unmark] on a subdir to remove the marks in
2069this subdir."
2070 (interactive "P")
277568f2 2071 (if (dired-get-subdir)
492d2437
RS
2072 (save-excursion (dired-mark-subdir-files))
2073 (let (buffer-read-only)
2074 (dired-repeat-over-lines
52041219 2075 (prefix-numeric-value arg)
492d2437
RS
2076 (function (lambda () (delete-char 1) (insert dired-marker-char)))))))
2077
2078(defun dired-unmark (arg)
2079 "Unmark the current (or next ARG) files.
2080If looking at a subdir, unmark all its files except `.' and `..'."
2081 (interactive "P")
2082 (let ((dired-marker-char ?\040))
2083 (dired-mark arg)))
2084
2085(defun dired-flag-file-deletion (arg)
2086 "In dired, flag the current line's file for deletion.
2087With prefix arg, repeat over several lines.
2088
2089If on a subdir headerline, mark all its files except `.' and `..'."
2090 (interactive "P")
2091 (let ((dired-marker-char dired-del-marker))
2092 (dired-mark arg)))
2093
2094(defun dired-unmark-backward (arg)
2095 "In dired, move up lines and remove deletion flag there.
2096Optional prefix ARG says how many lines to unflag; default is one line."
2097 (interactive "p")
2098 (dired-unmark (- arg)))
e5f0841e
KH
2099
2100(defun dired-do-toggle ()
2101 "Toggle marks.
2102That is, currently marked files become unmarked and vice versa.
2103Files marked with other flags (such as `D') are not affected.
2104`.' and `..' are never toggled.
2105As always, hidden subdirs are not affected."
2106 (interactive)
2107 (save-excursion
2108 (goto-char (point-min))
2109 (let (buffer-read-only)
2110 (while (not (eobp))
2111 (or (dired-between-files)
2112 (looking-at dired-re-dot)
2113 ;; use subst instead of insdel because it does not move
2114 ;; the gap and thus should be faster and because
2115 ;; other characters are left alone automatically
2116 (apply 'subst-char-in-region
2117 (point) (1+ (point))
2118 (if (eq ?\040 (following-char)) ; SPC
2119 (list ?\040 dired-marker-char)
2120 (list dired-marker-char ?\040))))
2121 (forward-line 1)))))
84fc2cfa 2122\f
492d2437
RS
2123;;; Commands to mark or flag files based on their characteristics or names.
2124
d2cadc4b
RS
2125(defvar dired-regexp-history nil
2126 "History list of regular expressions used in Dired commands.")
2127
2128(defun dired-read-regexp (prompt)
2129 (read-from-minibuffer prompt nil nil nil 'dired-regexp-history))
492d2437
RS
2130
2131(defun dired-mark-files-regexp (regexp &optional marker-char)
2132 "Mark all files matching REGEXP for use in later commands.
2133A prefix argument means to unmark them instead.
2134`.' and `..' are never marked.
2135
2136REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
2137object files--just `.o' will mark more than you might think."
2138 (interactive
2139 (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
2140 " files (regexp): "))
2141 (if current-prefix-arg ?\040)))
2142 (let ((dired-marker-char (or marker-char dired-marker-char)))
2143 (dired-mark-if
2144 (and (not (looking-at dired-re-dot))
2145 (not (eolp)) ; empty line
2146 (let ((fn (dired-get-filename nil t)))
2147 (and fn (string-match regexp (file-name-nondirectory fn)))))
2148 "matching file")))
2149
2150(defun dired-flag-files-regexp (regexp)
2151 "In dired, flag all files containing the specified REGEXP for deletion.
2152The match is against the non-directory part of the filename. Use `^'
2153 and `$' to anchor matches. Exclude subdirs by hiding them.
2154`.' and `..' are never flagged."
2155 (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
2156 (dired-mark-files-regexp regexp dired-del-marker))
2157
2158(defun dired-mark-symlinks (unflag-p)
2159 "Mark all symbolic links.
2160With prefix argument, unflag all those files."
2161 (interactive "P")
2162 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2163 (dired-mark-if (looking-at dired-re-sym) "symbolic link")))
2164
2165(defun dired-mark-directories (unflag-p)
2166 "Mark all directory file lines except `.' and `..'.
2167With prefix argument, unflag all those files."
2168 (interactive "P")
2169 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2170 (dired-mark-if (and (looking-at dired-re-dir)
2171 (not (looking-at dired-re-dot)))
2172 "directory file")))
2173
2174(defun dired-mark-executables (unflag-p)
2175 "Mark all executable files.
2176With prefix argument, unflag all those files."
2177 (interactive "P")
2178 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2179 (dired-mark-if (looking-at dired-re-exe) "executable file")))
2180
2181;; dired-x.el has a dired-mark-sexp interactive command: mark
2182;; files for which PREDICATE returns non-nil.
2183
2184(defun dired-flag-auto-save-files (&optional unflag-p)
84fc2cfa
ER
2185 "Flag for deletion files whose names suggest they are auto save files.
2186A prefix argument says to unflag those files instead."
2187 (interactive "P")
492d2437
RS
2188 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
2189 (dired-mark-if
2a838614 2190 ;; It is less than general to check for # here,
a91526b9
RS
2191 ;; but it's the only way this runs fast enough.
2192 (and (save-excursion (end-of-line)
536ae2dd
RS
2193 (or
2194 (eq (preceding-char) ?#)
2195 ;; Handle executables in case of -F option.
2196 ;; We need not worry about the other kinds
2197 ;; of markings that -F makes, since they won't
2198 ;; appear on real auto-save files.
2199 (if (eq (preceding-char) ?*)
2200 (progn
2201 (forward-char -1)
2202 (eq (preceding-char) ?#)))))
a91526b9
RS
2203 (not (looking-at dired-re-dir))
2204 (let ((fn (dired-get-filename t t)))
2205 (if fn (auto-save-file-name-p
2206 (file-name-nondirectory fn)))))
2207 "auto save file")))
492d2437 2208
84d3f6e8
RS
2209(defvar dired-garbage-files-regexp
2210 "\\.log$\\|\\.toc$\\|.dvi$|\\.bak$\\|\\.orig$\\|\\.rej$"
2211 "*Regular expression to match \"garbage\" files for `dired-flag-garbage-files'.")
2212
2213(defun dired-flag-garbage-files ()
84d3f6e8 2214 "Flag for deletion all files that match `dired-garbage-files-regexp'."
f656ec48 2215 (interactive)
84d3f6e8
RS
2216 (dired-flag-files-regexp dired-garbage-files-regexp))
2217
492d2437
RS
2218(defun dired-flag-backup-files (&optional unflag-p)
2219 "Flag all backup files (names ending with `~') for deletion.
2220With prefix argument, unflag these files."
2221 (interactive "P")
fdee13ec 2222 (let ((dired-marker-char (if unflag-p ?\ dired-del-marker)))
492d2437 2223 (dired-mark-if
f13101e9
KH
2224 ;; Don't call backup-file-name-p unless the last character looks like
2225 ;; it might be the end of a backup file name. This isn't very general,
a91526b9
RS
2226 ;; but it's the only way this runs fast enough.
2227 (and (save-excursion (end-of-line)
f13101e9
KH
2228 ;; Handle executables in case of -F option.
2229 ;; We need not worry about the other kinds
2230 ;; of markings that -F makes, since they won't
2231 ;; appear on real backup files.
2232 (if (eq (preceding-char) ?*)
2233 (forward-char -1))
fdee13ec 2234 (eq (preceding-char) ?~))
a91526b9 2235 (not (looking-at dired-re-dir))
492d2437
RS
2236 (let ((fn (dired-get-filename t t)))
2237 (if fn (backup-file-name-p fn))))
2238 "backup file")))
2239
6482fcac
RS
2240(defun dired-change-marks (&optional old new)
2241 "Change all OLD marks to NEW marks.
2242OLD and NEW are both characters used to mark files."
2243 (interactive
2244 (let* ((cursor-in-echo-area t)
2245 (old (progn (message "Change (old mark): ") (read-char)))
2246 (new (progn (message "Change %c marks to (new mark): " old)
2247 (read-char))))
2248 (list old new)))
e4e02841
RS
2249 (if (or (eq old ?\r) (eq new ?\r))
2250 (ding)
2251 (let ((string (format "\n%c" old))
2252 (buffer-read-only))
2253 (save-excursion
2254 (goto-char (point-min))
2255 (while (search-forward string nil t)
a15a76f7
RS
2256 (if (if (= old ?\ )
2257 (save-match-data
2258 (dired-get-filename 'no-dir t))
2259 t)
2260 (subst-char-in-region (match-beginning 0)
2261 (match-end 0) old new)))))))
6482fcac 2262
534a6edf 2263(defun dired-unmark-all-marks ()
b1ecd9c6
RS
2264 "Remove all marks from all files in the Dired buffer."
2265 (interactive)
2266 (dired-unmark-all-files ?\r))
2267
8b87a301 2268(defun dired-unmark-all-files (mark &optional arg)
b602ba2f
RS
2269 "Remove a specific mark (or any mark) from every file.
2270After this command, type the mark character to remove,
2271or type RET to remove all marks.
3585916f 2272With prefix arg, query for each marked file.
492d2437 2273Type \\[help-command] at that time for help."
b602ba2f
RS
2274 (interactive "cRemove marks (RET means all): \nP")
2275 (save-excursion
2276 (let* ((count 0)
2277 buffer-read-only case-fold-search query
2278 (string (format "\n%c" mark))
2279 (help-form "\
8b87a301
RS
2280Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
2281`!' to unmark all remaining files with no more questions."))
b602ba2f
RS
2282 (goto-char (point-min))
2283 (while (if (eq mark ?\r)
2284 (re-search-forward dired-re-mark nil t)
2285 (search-forward string nil t))
2286 (if (or (not arg)
2287 (dired-query 'query "Unmark file `%s'? "
2288 (dired-get-filename t)))
2289 (progn (subst-char-in-region (1- (point)) (point)
2290 (preceding-char) ?\ )
2291 (setq count (1+ count)))))
2292 (message (if (= count 1) "1 mark removed"
2293 "%d marks removed")
2294 count))))
492d2437 2295\f
492d2437 2296;; Logging failures operating on files, and showing the results.
84fc2cfa 2297
492d2437 2298(defvar dired-log-buffer "*Dired log*")
84fc2cfa 2299
492d2437
RS
2300(defun dired-why ()
2301 "Pop up a buffer with error log output from Dired.
2302A group of errors from a single command ends with a formfeed.
2303Thus, use \\[backward-page] to find the beginning of a group of errors."
2304 (interactive)
2305 (if (get-buffer dired-log-buffer)
2306 (let ((owindow (selected-window))
2307 (window (display-buffer (get-buffer dired-log-buffer))))
2308 (unwind-protect
6482fcac 2309 (progn
492d2437
RS
2310 (select-window window)
2311 (goto-char (point-max))
2312 (recenter -1))
2313 (select-window owindow)))))
2314
2315(defun dired-log (log &rest args)
2316 ;; Log a message or the contents of a buffer.
2317 ;; If LOG is a string and there are more args, it is formatted with
2318 ;; those ARGS. Usually the LOG string ends with a \n.
2319 ;; End each bunch of errors with (dired-log t): this inserts
2320 ;; current time and buffer, and a \f (formfeed).
2321 (let ((obuf (current-buffer)))
2322 (unwind-protect ; want to move point
2323 (progn
2324 (set-buffer (get-buffer-create dired-log-buffer))
2325 (goto-char (point-max))
2326 (let (buffer-read-only)
2327 (cond ((stringp log)
2328 (insert (if args
2329 (apply (function format) log args)
2330 log)))
2331 ((bufferp log)
2332 (insert-buffer log))
2333 ((eq t log)
2334 (insert "\n\t" (current-time-string)
2335 "\tBuffer `" (buffer-name obuf) "'\n\f\n")))))
2336 (set-buffer obuf))))
2337
2338(defun dired-log-summary (string failures)
2339 (message (if failures "%s--type ? for details (%s)"
2340 "%s--type ? for details")
2341 string failures)
2342 ;; Log a summary describing a bunch of errors.
2343 (dired-log (concat "\n" string))
2344 (dired-log t))
2345\f
2346;;; Sorting
2347
2348;; Most ls can only sort by name or by date (with -t), nothing else.
2349;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
2350;; So anything that does not contain these is sort "by name".
2351
2352(defvar dired-ls-sorting-switches "SXU"
2353 "String of `ls' switches (single letters) except `t' that influence sorting.")
2354
2355(defvar dired-sort-by-date-regexp
2356 (concat "^-[^" dired-ls-sorting-switches
2357 "]*t[^" dired-ls-sorting-switches "]*$")
2358 "Regexp recognized by dired to set `by date' mode.")
2359
2360(defvar dired-sort-by-name-regexp
2361 (concat "^-[^t" dired-ls-sorting-switches "]+$")
2362 "Regexp recognized by dired to set `by name' mode.")
2363
492d2437
RS
2364(defun dired-sort-set-modeline ()
2365 ;; Set modeline display according to dired-actual-switches.
2366 ;; Modeline display of "by name" or "by date" guarantees the user a
2367 ;; match with the corresponding regexps. Non-matching switches are
2368 ;; shown literally.
1d673d85 2369 (setq mode-name
492d2437
RS
2370 (let (case-fold-search)
2371 (cond ((string-match dired-sort-by-name-regexp dired-actual-switches)
1d673d85 2372 "Dired by name")
492d2437 2373 ((string-match dired-sort-by-date-regexp dired-actual-switches)
1d673d85 2374 "Dired by date")
492d2437 2375 (t
1d673d85 2376 (concat "Dired " dired-actual-switches)))))
45d8d39f 2377 (force-mode-line-update))
492d2437
RS
2378
2379(defun dired-sort-toggle-or-edit (&optional arg)
2380 "Toggle between sort by date/name and refresh the dired buffer.
2381With a prefix argument you can edit the current listing switches instead."
84fc2cfa 2382 (interactive "P")
492d2437
RS
2383 (if arg
2384 (dired-sort-other
2385 (read-string "ls switches (must contain -l): " dired-actual-switches))
2386 (dired-sort-toggle)))
2387
2388(defun dired-sort-toggle ()
2389 ;; Toggle between sort by date/name. Reverts the buffer.
2390 (setq dired-actual-switches
2391 (let (case-fold-search)
2392 (concat
2393 "-l"
26add1bf 2394 (dired-replace-in-string (concat "[-lt"
492d2437
RS
2395 dired-ls-sorting-switches "]")
2396 ""
2397 dired-actual-switches)
2398 (if (string-match (concat "[t" dired-ls-sorting-switches "]")
2399 dired-actual-switches)
2400 ""
2401 "t"))))
2402 (dired-sort-set-modeline)
2403 (revert-buffer))
2404
2405(defun dired-replace-in-string (regexp newtext string)
2406 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
2407 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
2408 (let ((result "") (start 0) mb me)
2409 (while (string-match regexp string start)
2410 (setq mb (match-beginning 0)
2411 me (match-end 0)
2412 result (concat result (substring string start mb) newtext)
2413 start me))
2414 (concat result (substring string start))))
2415
2416(defun dired-sort-other (switches &optional no-revert)
2417 ;; Specify new ls SWITCHES for current dired buffer. Values matching
2418 ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
2419 ;; minor mode accordingly, others appear literally in the mode line.
2420 ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
2421 (setq dired-actual-switches switches)
6cfd24f9 2422 (if (eq major-mode 'dired-mode) (dired-sort-set-modeline))
492d2437 2423 (or no-revert (revert-buffer)))
84fc2cfa 2424\f
492d2437
RS
2425;; To make this file smaller, the less common commands
2426;; go in a separate file. But autoload them here
2427;; to make the separation invisible.
2428
2429(autoload 'dired-diff "dired-aux"
2430 "Compare file at point with file FILE using `diff'.
2431FILE defaults to the file at the mark.
ab67260b 2432The prompted-for file is the first file given to `diff'."
492d2437
RS
2433 t)
2434
2435(autoload 'dired-backup-diff "dired-aux"
2436 "Diff this file with its backup file or vice versa.
2437Uses the latest backup, if there are several numerical backups.
2438If this file is a backup, diff it with its original.
ab67260b 2439The backup file is the first file given to `diff'."
492d2437
RS
2440 t)
2441
2d051399
RS
2442(autoload 'dired-clean-directory "dired-aux"
2443 "Flag numerical backups for deletion.
2444Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
2445Positive prefix arg KEEP overrides `dired-kept-versions';
2446Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
2447
2448To clear the flags on these files, you can use \\[dired-flag-backup-files]
2449with a prefix argument."
2450 t)
2451
492d2437
RS
2452(autoload 'dired-do-chmod "dired-aux"
2453 "Change the mode of the marked (or next ARG) files.
2454This calls chmod, thus symbolic modes like `g+w' are allowed."
2455 t)
2456
2457(autoload 'dired-do-chgrp "dired-aux"
2458 "Change the group of the marked (or next ARG) files."
2459 t)
2460
2461(autoload 'dired-do-chown "dired-aux"
2462 "Change the owner of the marked (or next ARG) files."
2463 t)
2464
2465(autoload 'dired-do-print "dired-aux"
2466 "Print the marked (or next ARG) files.
2467Uses the shell command coming from variables `lpr-command' and
2468`lpr-switches' as default."
2469 t)
2470
2471(autoload 'dired-do-shell-command "dired-aux"
6482fcac
RS
2472 "Run a shell command COMMAND on the marked files.
2473If no files are marked or a specific numeric prefix arg is given,
2474the next ARG files are used. Just \\[universal-argument] means the current file.
2475The prompt mentions the file(s) or the marker, as appropriate.
2476
492d2437 2477If there is output, it goes to a separate buffer.
6482fcac 2478
492d2437
RS
2479Normally the command is run on each file individually.
2480However, if there is a `*' in the command then it is run
2481just once with the entire file list substituted there.
2482
6482fcac
RS
2483No automatic redisplay of dired buffers is attempted, as there's no
2484telling what files the command may have changed. Type
2485\\[dired-do-redisplay] to redisplay the marked files.
492d2437
RS
2486
2487The shell command has the top level directory as working directory, so
2488output files usually are created there instead of in a subdir."
2489 t)
2490
492d2437
RS
2491(autoload 'dired-do-kill-lines "dired-aux"
2492 "Kill all marked lines (not the files).
2493With a prefix arg, kill all lines not marked or flagged."
2494 t)
2495
2496(autoload 'dired-do-compress "dired-aux"
2497 "Compress or uncompress marked (or next ARG) files."
2498 t)
2499
2500(autoload 'dired-do-byte-compile "dired-aux"
2501 "Byte compile marked (or next ARG) Emacs Lisp files."
2502 t)
2503
2504(autoload 'dired-do-load "dired-aux"
2505 "Load the marked (or next ARG) Emacs Lisp files."
2506 t)
2507
2508(autoload 'dired-do-redisplay "dired-aux"
2509 "Redisplay all marked (or next ARG) files.
2510If on a subdir line, redisplay that subdirectory. In that case,
2511a prefix arg lets you edit the `ls' switches used for the new listing."
2512 t)
2513
492d2437 2514(autoload 'dired-create-directory "dired-aux"
84fc2cfa 2515 "Create a directory called DIRECTORY."
492d2437 2516 t)
84fc2cfa 2517
492d2437
RS
2518(autoload 'dired-do-copy "dired-aux"
2519 "Copy all marked (or next ARG) files, or copy the current file.
2520Thus, a zero prefix argument copies nothing. But it toggles the
2521variable `dired-copy-preserve-time' (which see)."
2522 t)
2523
2524(autoload 'dired-do-symlink "dired-aux"
2525 "Make symbolic links to current file or all marked (or next ARG) files.
2526When operating on just the current file, you specify the new name.
2527When operating on multiple or marked files, you specify a directory
2528and new symbolic links are made in that directory
2529with the same names that the files currently have."
2530 t)
2531
2532(autoload 'dired-do-hardlink "dired-aux"
2533 "Add names (hard links) current file or all marked (or next ARG) files.
2534When operating on just the current file, you specify the new name.
2535When operating on multiple or marked files, you specify a directory
2536and new hard links are made in that directory
2537with the same names that the files currently have."
2538 t)
2539
2540(autoload 'dired-do-rename "dired-aux"
2541 "Rename current file or all marked (or next ARG) files.
2542When renaming just the current file, you specify the new name.
2543When renaming multiple or marked files, you specify a directory."
2544 t)
2545
2546(autoload 'dired-do-rename-regexp "dired-aux"
2547 "Rename marked files containing REGEXP to NEWNAME.
2548As each match is found, the user must type a character saying
2549 what to do with it. For directions, type \\[help-command] at that time.
2550NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
2551REGEXP defaults to the last regexp used.
2552With a zero prefix arg, renaming by regexp affects the complete
2553 pathname - usually only the non-directory part of file names is used
2554 and changed."
2555 t)
2556
2557(autoload 'dired-do-copy-regexp "dired-aux"
2558 "Copy all marked files containing REGEXP to NEWNAME.
2559See function `dired-rename-regexp' for more info."
2560 t)
2561
2562(autoload 'dired-do-hardlink-regexp "dired-aux"
2563 "Hardlink all marked files containing REGEXP to NEWNAME.
2564See function `dired-rename-regexp' for more info."
2565 t)
2566
2567(autoload 'dired-do-symlink-regexp "dired-aux"
2568 "Symlink all marked files containing REGEXP to NEWNAME.
2569See function `dired-rename-regexp' for more info."
2570 t)
2571
2572(autoload 'dired-upcase "dired-aux"
2573 "Rename all marked (or next ARG) files to upper case."
2574 t)
2575
2576(autoload 'dired-downcase "dired-aux"
2577 "Rename all marked (or next ARG) files to lower case."
2578 t)
2579
2580(autoload 'dired-maybe-insert-subdir "dired-aux"
2581 "Insert this subdirectory into the same dired buffer.
2582If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
2583 else inserts it at its natural place (as `ls -lR' would have done).
2584With a prefix arg, you may edit the ls switches used for this listing.
2585 You can add `R' to the switches to expand the whole tree starting at
2586 this subdirectory.
2587This function takes some pains to conform to `ls -lR' output."
2588 t)
2589
2590(autoload 'dired-next-subdir "dired-aux"
2591 "Go to next subdirectory, regardless of level."
2592 t)
2593
2594(autoload 'dired-prev-subdir "dired-aux"
2595 "Go to previous subdirectory, regardless of level.
2596When called interactively and not on a subdir line, go to this subdir's line."
2597 t)
2598
2599(autoload 'dired-goto-subdir "dired-aux"
2600 "Go to end of header line of DIR in this dired buffer.
2601Return value of point on success, otherwise return nil.
2602The next char is either \\n, or \\r if DIR is hidden."
2603 t)
2604
2605(autoload 'dired-mark-subdir-files "dired-aux"
2606 "Mark all files except `.' and `..'."
2607 t)
2608
2609(autoload 'dired-kill-subdir "dired-aux"
2610 "Remove all lines of current subdirectory.
2611Lower levels are unaffected."
2612 t)
2613
2614(autoload 'dired-tree-up "dired-aux"
2615 "Go up ARG levels in the dired tree."
2616 t)
2617
2618(autoload 'dired-tree-down "dired-aux"
2619 "Go down in the dired tree."
2620 t)
2621
2622(autoload 'dired-hide-subdir "dired-aux"
2623 "Hide or unhide the current subdirectory and move to next directory.
2624Optional prefix arg is a repeat factor.
2625Use \\[dired-hide-all] to (un)hide all directories."
2626 t)
2627
2628(autoload 'dired-hide-all "dired-aux"
2629 "Hide all subdirectories, leaving only their header lines.
2630If there is already something hidden, make everything visible again.
2631Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2632 t)
84fc2cfa 2633\f
492d2437
RS
2634(if (eq system-type 'vax-vms)
2635 (load "dired-vms"))
84fc2cfa 2636
52041219
RS
2637(provide 'dired)
2638
57e15005
BF
2639(run-hooks 'dired-load-hook) ; for your customizations
2640
52041219 2641;;; dired.el ends here