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