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