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