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