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