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