*** empty log message ***
[bpt/emacs.git] / lisp / dired.el
CommitLineData
c8472948 1;;; dired.el --- directory-browsing commands
52041219 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
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
37;;; The funny comments are for autoload.el, to automagically update
38;;; loaddefs.
84fc2cfa
ER
39
40;;;###autoload
492d2437
RS
41(defvar dired-listing-switches "-al"
42 "*Switches passed to `ls' for dired. MUST contain the `l' option.
43May contain all other options that don't contradict `-l';
44may contain even `F', `b', `i' and `s'.")
84fc2cfa 45
492d2437
RS
46; Don't use absolute paths as /bin should be in any PATH and people
47; may prefer /usr/local/gnu/bin or whatever. However, chown is
48; usually not in PATH.
49
50;;;###autoload
84fc2cfa 51(defvar dired-chown-program
194ff7c1
RS
52 (if (memq system-type '(hpux dgux usg-unix-v silicon-graphics-unix))
53 "chown" "/etc/chown")
492d2437
RS
54 "Name of chown command (usully `chown' or `/etc/chown').")
55
492d2437
RS
56;;;###autoload
57(defvar dired-ls-F-marks-symlinks nil
58 "*Informs dired about how `ls -lF' marks symbolic links.
c3554e95 59Set this to t if `insert-directory-program' with `-lF' marks the symbolic link
492d2437 60itself with a trailing @ (usually the case under Ultrix).
84fc2cfa 61
492d2437
RS
62Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
63nil (the default), if it gives `bar@ -> foo', set it to t.
64
65Dired checks if there is really a @ appended. Thus, if you have a
66marking `ls' program on one host and a non-marking on another host, and
67don't care about symbolic links which really end in a @, you can
68always set this variable to t.")
69
70;;;###autoload
71(defvar dired-trivial-filenames "^\\.\\.?$\\|^#"
72 "*Regexp of files to skip when finding first file of a directory.
73A value of nil means move to the subdir line.
74A value of t means move to first file.")
75
76;;;###autoload
77(defvar dired-keep-marker-rename t
78 ;; Use t as default so that moved files "take their markers with them".
79 "*Controls marking of renamed files.
80If t, files keep their previous marks when they are renamed.
81If a character, renamed files (whether previously marked or not)
82are afterward marked with that character.")
83
84;;;###autoload
85(defvar dired-keep-marker-copy ?C
86 "*Controls marking of copied files.
87If t, copied files are marked if and as the corresponding original files were.
88If a character, copied files are unconditionally marked with that character.")
89
90;;;###autoload
91(defvar dired-keep-marker-hardlink ?H
92 "*Controls marking of newly made hard links.
93If t, they are marked if and as the files linked to were marked.
94If a character, new links are unconditionally marked with that character.")
95
96;;;###autoload
97(defvar dired-keep-marker-symlink ?Y
98 "*Controls marking of newly made symbolic links.
99If t, they are marked if and as the files linked to were marked.
100If a character, new links are unconditionally marked with that character.")
101
102;;;###autoload
103(defvar dired-dwim-target nil
104 "*If non-nil, dired tries to guess a default target directory.
105This means: if there is a dired buffer displayed in the next window,
106use its current subdir, instead of the current subdir of this dired buffer.
107
108The target is used in the prompt for file copy, rename etc.")
109
110;;;###autoload
111(defvar dired-copy-preserve-time t
112 "*If non-nil, Dired preserves the last-modified time in a file copy.
113\(This works on only some systems.)")
114
115;;; Hook variables
116
117(defvar dired-load-hook nil
118 "Run after loading dired.
119You can customize key bindings or load extensions with this.")
120
121(defvar dired-mode-hook nil
122 "Run at the very end of dired-mode.")
123
124(defvar dired-before-readin-hook nil
125 "This hook is run before a dired buffer is read in (created or reverted).")
126
127(defvar dired-after-readin-hook nil
128 "Hook run after each time a file or directory is read by Dired.
129After each listing of a file or directory, this hook is run
130with the buffer narrowed to the listing.")
131;; Note this can't simply be run inside function `dired-ls' as the hook
132;; functions probably depend on the dired-subdir-alist to be OK.
133
134;;; Internal variables
135
136(defvar dired-marker-char ?* ; the answer is 42
137 ;; so that you can write things like
138 ;; (let ((dired-marker-char ?X))
139 ;; ;; great code using X markers ...
140 ;; )
141 ;; For example, commands operating on two sets of files, A and B.
142 ;; Or marking files with digits 0-9. This could implicate
143 ;; concentric sets or an order for the marked files.
144 ;; The code depends on dynamic scoping on the marker char.
145 "In Dired, the current mark character.
146This is what the `do' commands look for and what the `mark' commands store.")
147
148(defvar dired-del-marker ?D
149 "Character used to flag files for deletion.")
150
151(defvar dired-shrink-to-fit
ab67260b
RS
152 t
153;; I see no reason ever to make this nil -- rms.
154;; (> baud-rate search-slow-speed)
492d2437
RS
155 "Non-nil means Dired shrinks the display buffer to fit the marked files.")
156
157(defvar dired-flagging-regexp nil);; Last regexp used to flag files.
158
ab67260b
RS
159(defvar dired-file-version-alist)
160
492d2437
RS
161(defvar dired-directory nil
162 "The directory name or shell wildcard that was used as argument to `ls'.
163Local to each dired buffer.")
164
165(defvar dired-actual-switches nil
166 "The value of `dired-listing-switches' used to make this buffer's text.")
167
168(defvar dired-re-inode-size "[0-9 \t]*"
169 "Regexp for optional initial inode and file size as made by `ls -i -s'.")
170
171;; These regexps must be tested at beginning-of-line, but are also
172;; used to search for next matches, so neither omitting "^" nor
173;; replacing "^" by "\n" (to make it slightly faster) will work.
174
175(defvar dired-re-mark "^[^ \n]")
176;; "Regexp matching a marked line.
177;; Important: the match ends just after the marker."
178(defvar dired-re-maybe-mark "^. ")
179(defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d"))
180(defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l"))
181(defvar dired-re-exe;; match ls permission string of an executable file
182 (mapconcat (function
183 (lambda (x)
184 (concat dired-re-maybe-mark dired-re-inode-size x)))
185 '("-[-r][-w][xs][-r][-w].[-r][-w]."
186 "-[-r][-w].[-r][-w][xs][-r][-w]."
187 "-[-r][-w].[-r][-w].[-r][-w][xst]")
188 "\\|"))
189(defvar dired-re-dot "^.* \\.\\.?$")
190
191(defvar dired-subdir-alist nil
192 "Association list of subdirectories and their buffer positions.
193Each subdirectory has an element: (DIRNAME . STARTMARKER).
7b2469ae
RS
194The order of elements is the reverse of the order in the buffer.
195In simple cases, this list contains one element.")
492d2437
RS
196
197(defvar dired-subdir-regexp "^. \\([^ \n\r]+\\)\\(:\\)[\n\r]"
198 "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
199Subexpression 1 is the subdirectory proper, no trailing colon.
200The match starts at the beginning of the line and ends after the end
201of the line (\\n or \\r).
202Subexpression 2 must end right before the \\n or \\r.")
203
204\f
205;;; Macros must be defined before they are used, for the byte compiler.
206
207;; Mark all files for which CONDITION evals to non-nil.
208;; CONDITION is evaluated on each line, with point at beginning of line.
209;; MSG is a noun phrase for the type of files being marked.
210;; It should end with a noun that can be pluralized by adding `s'.
211;; Return value is the number of files marked, or nil if none were marked.
212(defmacro dired-mark-if (predicate msg)
213 (` (let (buffer-read-only count)
214 (save-excursion
215 (setq count 0)
216 (if (, msg) (message "Marking %ss..." (, msg)))
217 (goto-char (point-min))
218 (while (not (eobp))
219 (if (, predicate)
220 (progn
221 (delete-char 1)
222 (insert dired-marker-char)
223 (setq count (1+ count))))
224 (forward-line 1))
225 (if (, msg) (message "%s %s%s %s%s."
226 count
227 (, msg)
228 (dired-plural-s count)
229 (if (eq dired-marker-char ?\040) "un" "")
230 (if (eq dired-marker-char dired-del-marker)
231 "flagged" "marked"))))
232 (and (> count 0) count))))
233
234(defmacro dired-map-over-marks (body arg &optional show-progress)
235;; "Macro: Perform BODY with point somewhere on each marked line
236;;and return a list of BODY's results.
237;;If no marked file could be found, execute BODY on the current line.
238;; If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
239;; files instead of the marked files.
240;; In that case point is dragged along. This is so that commands on
241;; the next ARG (instead of the marked) files can be chained easily.
242;; If ARG is otherwise non-nil, use current file instead.
243;;If optional third arg SHOW-PROGRESS evaluates to non-nil,
244;; redisplay the dired buffer after each file is processed.
245;;No guarantee is made about the position on the marked line.
246;; BODY must ensure this itself if it depends on this.
247;;Search starts at the beginning of the buffer, thus the car of the list
248;; corresponds to the line nearest to the buffer's bottom. This
249;; is also true for (positive and negative) integer values of ARG.
250;;BODY should not be too long as it is expanded four times."
251;;
252;;Warning: BODY must not add new lines before point - this may cause an
253;;endless loop.
254;;This warning should not apply any longer, sk 2-Sep-1991 14:10.
255 (` (prog1
256 (let (buffer-read-only case-fold-search found results)
257 (if (, arg)
258 (if (integerp (, arg))
259 (progn;; no save-excursion, want to move point.
260 (dired-repeat-over-lines
261 (, arg)
262 (function (lambda ()
263 (if (, show-progress) (sit-for 0))
264 (setq results (cons (, body) results)))))
265 (if (< (, arg) 0)
266 (nreverse results)
267 results))
268 ;; non-nil, non-integer ARG means use current file:
269 (list (, body)))
270 (let ((regexp (dired-marker-regexp)) next-position)
271 (save-excursion
272 (goto-char (point-min))
273 ;; remember position of next marked file before BODY
274 ;; can insert lines before the just found file,
275 ;; confusing us by finding the same marked file again
276 ;; and again and...
277 (setq next-position (and (re-search-forward regexp nil t)
278 (point-marker))
279 found (not (null next-position)))
280 (while next-position
281 (goto-char next-position)
282 (if (, show-progress) (sit-for 0))
283 (setq results (cons (, body) results))
284 ;; move after last match
285 (goto-char next-position)
286 (forward-line 1)
287 (set-marker next-position nil)
288 (setq next-position (and (re-search-forward regexp nil t)
289 (point-marker)))))
290 (if found
291 results
292 (list (, body))))))
293 ;; save-excursion loses, again
294 (dired-move-to-filename))))
295
296(defun dired-get-marked-files (&optional localp arg)
297 "Return the marked files' names as list of strings.
298The list is in the same order as the buffer, that is, the car is the
299 first marked file.
300Values returned are normally absolute pathnames.
301Optional arg LOCALP as in `dired-get-filename'.
302Optional second argument ARG forces to use other files. If ARG is an
303 integer, use the next ARG files. If ARG is otherwise non-nil, use
304 current file. Usually ARG comes from the current prefix arg."
84fc2cfa 305 (save-excursion
492d2437 306 (nreverse (dired-map-over-marks (dired-get-filename localp) arg))))
84fc2cfa 307
492d2437
RS
308\f
309;; Function dired-ls is redefinable for VMS, ange-ftp, Prospero or
310;; other special applications.
492d2437
RS
311\f
312;; The dired command
313
314(defun dired-read-dir-and-switches (str)
315 ;; For use in interactive.
316 (reverse (list
317 (if current-prefix-arg
318 (read-string "Dired listing switches: "
319 dired-listing-switches))
320 (read-file-name (format "Dired %s(directory): " str)
321 nil default-directory nil))))
84fc2cfa 322
492d2437 323;;;###autoload (define-key ctl-x-map "d" 'dired)
84fc2cfa 324;;;###autoload
492d2437 325(defun dired (dirname &optional switches)
84fc2cfa 326 "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
492d2437
RS
327Optional second argument SWITCHES specifies the `ls' options used.
328\(Interactively, use a prefix argument to be able to specify SWITCHES.)
329Dired displays a list of files in DIRNAME (which may also have
330 shell wildcards appended to select certain files).
52041219 331\\<dired-mode-map>\
492d2437 332You can move around in it with the usual commands.
52041219 333You can flag files for deletion with \\[dired-flag-file-deletion] and then delete them by
492d2437
RS
334 typing \\[dired-do-flagged-delete].
335Type \\[describe-mode] after entering dired for more info.
84fc2cfa 336
492d2437
RS
337If DIRNAME is already in a dired buffer, that buffer is used without refresh."
338 ;; Cannot use (interactive "D") because of wildcards.
339 (interactive (dired-read-dir-and-switches ""))
340 (switch-to-buffer (dired-noselect dirname switches)))
341
342;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
84fc2cfa 343;;;###autoload
492d2437 344(defun dired-other-window (dirname &optional switches)
84fc2cfa 345 "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
492d2437
RS
346 (interactive (dired-read-dir-and-switches "in other window "))
347 (switch-to-buffer-other-window (dired-noselect dirname switches)))
84fc2cfa
ER
348
349;;;###autoload
492d2437 350(defun dired-noselect (dirname &optional switches)
84fc2cfa
ER
351 "Like `dired' but returns the dired buffer as value, does not select it."
352 (or dirname (setq dirname default-directory))
492d2437
RS
353 ;; This loses the distinction between "/foo/*/" and "/foo/*" that
354 ;; some shells make:
84fc2cfa
ER
355 (setq dirname (expand-file-name (directory-file-name dirname)))
356 (if (file-directory-p dirname)
357 (setq dirname (file-name-as-directory dirname)))
492d2437
RS
358 (dired-internal-noselect dirname switches))
359
360;; Separate function from dired-noselect for the sake of dired-vms.el.
361(defun dired-internal-noselect (dirname &optional switches)
362 ;; If there is an existing dired buffer for DIRNAME, just leave
363 ;; buffer as it is (don't even call dired-revert).
364 ;; This saves time especially for deep trees or with ange-ftp.
365 ;; The user can type `g'easily, and it is more consistent with find-file.
366 ;; But if SWITCHES are given they are probably different from the
367 ;; buffer's old value, so call dired-sort-other, which does
368 ;; revert the buffer.
369 ;; A pity we can't possibly do "Directory has changed - refresh? "
370 ;; like find-file does.
371 (let* ((buffer (dired-find-buffer-nocreate dirname))
372 ;; note that buffer already is in dired-mode, if found
373 (new-buffer-p (not buffer))
374 (old-buf (current-buffer)))
375 (or buffer
376 (let ((default-major-mode 'fundamental-mode))
377 ;; We don't want default-major-mode to run hooks and set auto-fill
378 ;; or whatever, now that dired-mode does not
379 ;; kill-all-local-variables any longer.
380 (setq buffer (create-file-buffer (directory-file-name dirname)))))
381 (set-buffer buffer)
382 (if (not new-buffer-p) ; existing buffer ...
383 (if switches ; ... but new switches
384 (dired-sort-other switches)) ; this calls dired-revert
385 ;; Else a new buffer
386 (setq default-directory (if (file-directory-p dirname)
387 dirname
388 (file-name-directory dirname)))
389 (or switches (setq switches dired-listing-switches))
390 (dired-mode dirname switches)
391 ;; default-directory and dired-actual-switches are set now
392 ;; (buffer-local), so we can call dired-readin:
393 (let ((failed t))
394 (unwind-protect
395 (progn (dired-readin dirname buffer)
396 (setq failed nil))
397 ;; dired-readin can fail if parent directories are inaccessible.
398 ;; Don't leave an empty buffer around in that case.
399 (if failed (kill-buffer buffer))))
400 ;; No need to narrow since the whole buffer contains just
401 ;; dired-readin's output, nothing else. The hook can
402 ;; successfully use dired functions (e.g. dired-get-filename)
403 ;; as the subdir-alist has been built in dired-readin.
404 (run-hooks 'dired-after-readin-hook)
405 (goto-char (point-min))
406 (dired-initial-position dirname))
407 (set-buffer old-buf)
84fc2cfa
ER
408 buffer))
409
492d2437
RS
410;; This differs from dired-buffers-for-dir in that it does not consider
411;; subdirs of default-directory and searches for the first match only
412(defun dired-find-buffer-nocreate (dirname)
413 (let (found (blist (buffer-list)))
414 (while blist
415 (save-excursion
416 (set-buffer (car blist))
417 (if (and (eq major-mode 'dired-mode)
418 (equal dired-directory dirname))
419 (setq found (car blist)
420 blist nil)
421 (setq blist (cdr blist)))))
422 found))
423
424\f
425;; Read in a new dired buffer
426
427;; dired-readin differs from dired-insert-subdir in that it accepts
428;; wildcards, erases the buffer, and builds the subdir-alist anew
429;; (including making it buffer-local and clearing it first).
430(defun dired-readin (dirname buffer)
431 ;; default-directory and dired-actual-switches must be buffer-local
432 ;; and initialized by now.
433 ;; Thus we can test (equal default-directory dirname) instead of
434 ;; (file-directory-p dirname) and save a filesystem transaction.
435 ;; Also, we can run this hook which may want to modify the switches
436 ;; based on default-directory, e.g. with ange-ftp to a SysV host
437 ;; where ls won't understand -Al switches.
438 (setq dirname (expand-file-name dirname))
439 (run-hooks 'dired-before-readin-hook)
440 (save-excursion
441 (message "Reading directory %s..." dirname)
442 (set-buffer buffer)
443 (let (buffer-read-only (failed t))
444 (widen)
445 (erase-buffer)
446 (dired-readin-insert dirname)
447 (indent-rigidly (point-min) (point-max) 2)
448 ;; We need this to make the root dir have a header line as all
449 ;; other subdirs have:
450 (goto-char (point-min))
451 (dired-insert-headerline default-directory)
452 ;; can't run dired-after-readin-hook here, it may depend on the subdir
453 ;; alist to be OK.
454 )
455 (message "Reading directory %s...done" dirname)
456 (set-buffer-modified-p nil)
457 ;; Must first make alist buffer local and set it to nil because
458 ;; dired-build-subdir-alist will call dired-clear-alist first
459 (set (make-local-variable 'dired-subdir-alist) nil)
460 (dired-build-subdir-alist)))
461
462;; Subroutines of dired-readin
463
464(defun dired-readin-insert (dirname)
465 ;; Just insert listing for DIRNAME, assuming a clean buffer.
466 (if (equal default-directory dirname);; i.e., (file-directory-p dirname)
c3554e95 467 (insert-directory dirname dired-actual-switches nil t)
492d2437
RS
468 (if (not (file-readable-p
469 (directory-file-name (file-name-directory dirname))))
470 (error "Directory %s inaccessible or nonexistent" dirname)
471 ;; else assume it contains wildcards:
c3554e95 472 (insert-directory dirname dired-actual-switches t)
492d2437
RS
473 (save-excursion;; insert wildcard instead of total line:
474 (goto-char (point-min))
475 (insert "wildcard " (file-name-nondirectory dirname) "\n")))))
476
477(defun dired-insert-headerline (dir);; also used by dired-insert-subdir
478 ;; Insert DIR's headerline with no trailing slash, exactly like ls
479 ;; would, and put cursor where dired-build-subdir-alist puts subdir
480 ;; boundaries.
481 (save-excursion (insert " " (directory-file-name dir) ":\n")))
482
483\f
484;; Reverting a dired buffer
485
84fc2cfa 486(defun dired-revert (&optional arg noconfirm)
492d2437
RS
487 ;; Reread the dired buffer. Must also be called after
488 ;; dired-actual-switches have changed.
489 ;; Should not fail even on completely garbaged buffers.
490 ;; Preserves old cursor, marks/flags, hidden-p.
491 (widen) ; just in case user narrowed
84fc2cfa 492 (let ((opoint (point))
492d2437
RS
493 (ofile (dired-get-filename nil t))
494 (mark-alist nil) ; save marked files
495 (hidden-subdirs (dired-remember-hidden))
496 (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
497 (case-fold-search nil) ; we check for upper case ls flags
498 buffer-read-only)
499 (goto-char (point-min))
500 (setq mark-alist;; only after dired-remember-hidden since this unhides:
501 (dired-remember-marks (point-min) (point-max)))
502 ;; treat top level dir extra (it may contain wildcards)
715984d3 503 (dired-uncache dired-directory)
84fc2cfa 504 (dired-readin dired-directory (current-buffer))
492d2437
RS
505 (let ((dired-after-readin-hook nil))
506 ;; don't run that hook for each subdir...
507 (dired-insert-old-subdirs old-subdir-alist))
508 (dired-mark-remembered mark-alist) ; mark files that were marked
509 ;; ... run the hook for the whole buffer, and only after markers
510 ;; have been reinserted (else omitting in dired-x would omit marked files)
511 (run-hooks 'dired-after-readin-hook) ; no need to narrow
512 (or (and ofile (dired-goto-file ofile)) ; move cursor to where it
513 (goto-char opoint)) ; was before
84fc2cfa 514 (dired-move-to-filename)
492d2437
RS
515 (save-excursion ; hide subdirs that were hidden
516 (mapcar (function (lambda (dir)
517 (if (dired-goto-subdir dir)
518 (dired-hide-subdir 1))))
519 hidden-subdirs)))
520 ;; outside of the let scope
521;;; Might as well not override the user if the user changed this.
522;;; (setq buffer-read-only t)
523 )
524
525;; Subroutines of dired-revert
526;; Some of these are also used when inserting subdirs.
527
528(defun dired-remember-marks (beg end)
529 ;; Return alist of files and their marks, from BEG to END.
530 (if selective-display ; must unhide to make this work.
531 (let (buffer-read-only)
532 (subst-char-in-region beg end ?\r ?\n)))
533 (let (fil chr alist)
534 (save-excursion
535 (goto-char beg)
536 (while (re-search-forward dired-re-mark end t)
537 (if (setq fil (dired-get-filename nil t))
538 (setq chr (preceding-char)
539 alist (cons (cons fil chr) alist)))))
540 alist))
541
542;; Mark all files remembered in ALIST.
543;; Each element of ALIST looks like (FILE . MARKERCHAR).
544(defun dired-mark-remembered (alist)
545 (let (elt fil chr)
546 (while alist
547 (setq elt (car alist)
548 alist (cdr alist)
549 fil (car elt)
550 chr (cdr elt))
551 (if (dired-goto-file fil)
552 (save-excursion
553 (beginning-of-line)
554 (delete-char 1)
555 (insert chr))))))
556
557;; Return a list of names of subdirs currently hidden.
558(defun dired-remember-hidden ()
559 (let ((l dired-subdir-alist) dir pos result)
560 (while l
561 (setq dir (car (car l))
562 pos (cdr (car l))
563 l (cdr l))
564 (goto-char pos)
565 (skip-chars-forward "^\r\n")
52041219 566 (if (eq (following-char) ?\r)
492d2437
RS
567 (setq result (cons dir result))))
568 result))
569
570;; Try to insert all subdirs that were displayed before,
571;; according to the former subdir alist OLD-SUBDIR-ALIST.
572(defun dired-insert-old-subdirs (old-subdir-alist)
573 (or (string-match "R" dired-actual-switches)
574 (let (elt dir)
575 (while old-subdir-alist
576 (setq elt (car old-subdir-alist)
577 old-subdir-alist (cdr old-subdir-alist)
578 dir (car elt))
579 (condition-case ()
715984d3
RS
580 (progn
581 (dired-uncache dir)
582 (dired-insert-subdir dir))
492d2437 583 (error nil))))))
715984d3
RS
584
585;; Remove directory DIR from any directory cache.
586(defun dired-uncache (dir)
5dbfdacd 587 (let ((handler (find-file-name-handler dir)))
715984d3
RS
588 (if handler
589 (funcall handler 'dired-uncache dir))))
492d2437
RS
590\f
591;; dired mode key bindings and initialization
84fc2cfa
ER
592
593(defvar dired-mode-map nil "Local keymap for dired-mode buffers.")
594(if dired-mode-map
595 nil
492d2437
RS
596 ;; Force `f' rather than `e' in the mode doc:
597 (fset 'dired-advertised-find-file 'dired-find-file)
598 ;; This looks ugly when substitute-command-keys uses C-d instead d:
599 ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
600
84fc2cfa
ER
601 (setq dired-mode-map (make-keymap))
602 (suppress-keymap dired-mode-map)
492d2437 603 ;; Commands to mark or flag certain categories of files
84fc2cfa 604 (define-key dired-mode-map "#" 'dired-flag-auto-save-files)
492d2437 605 (define-key dired-mode-map "*" 'dired-mark-executables)
84fc2cfa 606 (define-key dired-mode-map "." 'dired-clean-directory)
492d2437
RS
607 (define-key dired-mode-map "/" 'dired-mark-directories)
608 (define-key dired-mode-map "@" 'dired-mark-symlinks)
609 (define-key dired-mode-map "~" 'dired-flag-backup-files)
6482fcac 610 ;; Upper case keys (except !) for operating on the marked files
492d2437
RS
611 (define-key dired-mode-map "C" 'dired-do-copy)
612 (define-key dired-mode-map "B" 'dired-do-byte-compile)
613 (define-key dired-mode-map "D" 'dired-do-delete)
614 (define-key dired-mode-map "G" 'dired-do-chgrp)
615 (define-key dired-mode-map "H" 'dired-do-hardlink)
616 (define-key dired-mode-map "L" 'dired-do-load)
617 (define-key dired-mode-map "M" 'dired-do-chmod)
618 (define-key dired-mode-map "O" 'dired-do-chown)
619 (define-key dired-mode-map "P" 'dired-do-print)
620 (define-key dired-mode-map "R" 'dired-do-rename)
621 (define-key dired-mode-map "S" 'dired-do-symlink)
622 (define-key dired-mode-map "X" 'dired-do-shell-command)
623 (define-key dired-mode-map "Z" 'dired-do-compress)
624 (define-key dired-mode-map "!" 'dired-do-shell-command)
625 ;; Comparison commands
626 (define-key dired-mode-map "=" 'dired-diff)
627 (define-key dired-mode-map "\M-=" 'dired-backup-diff)
628 ;; Tree Dired commands
629 (define-key dired-mode-map "\M-\C-?" 'dired-unmark-all-files)
630 (define-key dired-mode-map "\M-\C-d" 'dired-tree-down)
631 (define-key dired-mode-map "\M-\C-u" 'dired-tree-up)
632 (define-key dired-mode-map "\M-\C-n" 'dired-next-subdir)
633 (define-key dired-mode-map "\M-\C-p" 'dired-prev-subdir)
634 ;; move to marked files
635 (define-key dired-mode-map "\M-{" 'dired-prev-marked-file)
636 (define-key dired-mode-map "\M-}" 'dired-next-marked-file)
492d2437
RS
637 ;; Make all regexp commands share a `%' prefix:
638 (fset 'dired-regexp-prefix (make-sparse-keymap))
639 (define-key dired-mode-map "%" 'dired-regexp-prefix)
640 (define-key dired-mode-map "%u" 'dired-upcase)
641 (define-key dired-mode-map "%l" 'dired-downcase)
642 (define-key dired-mode-map "%d" 'dired-flag-files-regexp)
643 (define-key dired-mode-map "%m" 'dired-mark-files-regexp)
644 (define-key dired-mode-map "%r" 'dired-do-rename-regexp)
645 (define-key dired-mode-map "%C" 'dired-do-copy-regexp)
646 (define-key dired-mode-map "%H" 'dired-do-hardlink-regexp)
647 (define-key dired-mode-map "%R" 'dired-do-rename-regexp)
648 (define-key dired-mode-map "%S" 'dired-do-symlink-regexp)
649 ;; Lower keys for commands not operating on all the marked files
6482fcac 650 (define-key dired-mode-map "c" 'dired-change-marks)
492d2437
RS
651 (define-key dired-mode-map "d" 'dired-flag-file-deletion)
652 (define-key dired-mode-map "e" 'dired-find-file)
653 (define-key dired-mode-map "f" 'dired-advertised-find-file)
654 (define-key dired-mode-map "g" 'revert-buffer)
84fc2cfa 655 (define-key dired-mode-map "h" 'describe-mode)
492d2437 656 (define-key dired-mode-map "i" 'dired-maybe-insert-subdir)
6482fcac 657 (define-key dired-mode-map "k" 'dired-do-kill-lines)
492d2437
RS
658 (define-key dired-mode-map "l" 'dired-do-redisplay)
659 (define-key dired-mode-map "m" 'dired-mark)
660 (define-key dired-mode-map "n" 'dired-next-line)
661 (define-key dired-mode-map "o" 'dired-find-file-other-window)
ab67260b 662 (define-key dired-mode-map "\C-o" 'dired-display-file)
492d2437
RS
663 (define-key dired-mode-map "p" 'dired-previous-line)
664 (define-key dired-mode-map "q" 'dired-quit)
665 (define-key dired-mode-map "s" 'dired-sort-toggle-or-edit)
666 (define-key dired-mode-map "u" 'dired-unmark)
667 (define-key dired-mode-map "v" 'dired-view-file)
668 (define-key dired-mode-map "x" 'dired-do-flagged-delete)
669 (define-key dired-mode-map "+" 'dired-create-directory)
670 ;; moving
671 (define-key dired-mode-map "<" 'dired-prev-dirline)
672 (define-key dired-mode-map ">" 'dired-next-dirline)
673 (define-key dired-mode-map "^" 'dired-up-directory)
84fc2cfa
ER
674 (define-key dired-mode-map " " 'dired-next-line)
675 (define-key dired-mode-map "\C-n" 'dired-next-line)
676 (define-key dired-mode-map "\C-p" 'dired-previous-line)
492d2437
RS
677 ;; hiding
678 (define-key dired-mode-map "$" 'dired-hide-subdir)
679 (define-key dired-mode-map "\M-$" 'dired-hide-all)
680 ;; misc
681 (define-key dired-mode-map "?" 'dired-summary)
682 (define-key dired-mode-map "\177" 'dired-unmark-backward)
683 (define-key dired-mode-map "\C-_" 'dired-undo)
684 (define-key dired-mode-map "\C-xu" 'dired-undo)
685 )
84fc2cfa 686
492d2437
RS
687(or (member '(dired-sort-mode dired-sort-mode) minor-mode-alist)
688 ;; Test whether this has already been done in case dired is reloaded
689 ;; There may be several elements with dired-sort-mode as car.
690 (setq minor-mode-alist
691 (cons '(dired-sort-mode dired-sort-mode)
692 ;; dired-sort-mode is nil outside dired
693 minor-mode-alist)))
694\f
84fc2cfa
ER
695;; Dired mode is suitable only for specially formatted data.
696(put 'dired-mode 'mode-class 'special)
697
492d2437
RS
698(defun dired-mode (&optional dirname switches)
699 "\
700Mode for \"editing\" directory listings.
701In dired, you are \"editing\" a list of the files in a directory and
702 \(optionally) its subdirectories, in the format of `ls -lR'.
703 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
704\"Editing\" means that you can run shell commands on files, visit,
705 compress, load or byte-compile them, change their file attributes
706 and insert subdirectories into the same buffer. You can \"mark\"
707 files for later commands or \"flag\" them for deletion, either file
708 by file or all files matching certain criteria.
709You can move using the usual cursor motion commands.\\<dired-mode-map>
710Letters no longer insert themselves. Digits are prefix arguments.
711Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
712Type \\[dired-mark] to Mark a file or subdirectory for later commands.
713 Most commands operate on the marked files and use the current file
714 if no files are marked. Use a numeric prefix argument to operate on
715 the next ARG (or previous -ARG if ARG<0) files, or just `1'
716 to operate on the current file only. Prefix arguments override marks.
717 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
718 to see why something went wrong.
719Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
720Type \\[dired-unmark-backward] to back up one line and unflag.
721Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
722Type \\[dired-advertised-find-file] to Find the current line's file
723 (or dired it in another buffer, if it is a directory).
724Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
725Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
726Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
727Type \\[dired-do-copy] to Copy files.
728Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
729Type \\[revert-buffer] to read all currently expanded directories again.
730 This retains all marks and hides subdirs again that were hidden before.
731SPC and DEL can be used to move down and up by lines.
732
733If dired ever gets confused, you can either type \\[revert-buffer] \
734to read the
735directories again, type \\[dired-do-redisplay] \
736to relist a single or the marked files or a
737subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
738again for the directory tree.
739
740Customization variables (rename this buffer and type \\[describe-variable] on each line
741for more info):
742
743 dired-listing-switches
744 dired-trivial-filenames
745 dired-shrink-to-fit
746 dired-marker-char
747 dired-del-marker
748 dired-keep-marker-rename
749 dired-keep-marker-copy
750 dired-keep-marker-hardlink
751 dired-keep-marker-symlink
752
753Hooks (use \\[describe-variable] to see their documentation):
754
755 dired-before-readin-hook
756 dired-after-readin-hook
757 dired-mode-hook
758 dired-load-hook
759
760Keybindings:
84fc2cfa 761\\{dired-mode-map}"
492d2437
RS
762 ;; Not to be called interactively (e.g. dired-directory will be set
763 ;; to default-directory, which is wrong with wildcards).
84fc2cfa 764 (kill-all-local-variables)
84fc2cfa 765 (use-local-map dired-mode-map)
492d2437
RS
766 (dired-advertise) ; default-directory is already set
767 (setq major-mode 'dired-mode
768 mode-name "Dired"
769 case-fold-search nil
770 buffer-read-only t
771 selective-display t ; for subdirectory hiding
772 mode-line-buffer-identification '("Dired: %17b"))
773 (set (make-local-variable 'revert-buffer-function)
774 (function dired-revert))
775 (set (make-local-variable 'page-delimiter)
776 "\n\n")
777 (set (make-local-variable 'dired-directory)
778 (or dirname default-directory))
779 ;; list-buffers uses this to display the dir being edited in this buffer.
780 (set (make-local-variable 'list-buffers-directory)
781 dired-directory)
782 (set (make-local-variable 'dired-actual-switches)
783 (or switches dired-listing-switches))
784 (make-local-variable 'dired-sort-mode)
785 (dired-sort-other dired-actual-switches t)
84fc2cfa
ER
786 (run-hooks 'dired-mode-hook))
787\f
492d2437 788;; Ideosyncratic dired commands that don't deal with marks.
84fc2cfa 789
492d2437
RS
790(defun dired-quit ()
791 "Bury the current dired buffer."
792 (interactive)
793 (bury-buffer))
84fc2cfa
ER
794
795(defun dired-summary ()
492d2437 796 "Summarize basic Dired commands and show recent Dired errors."
84fc2cfa 797 (interactive)
492d2437 798 (dired-why)
84fc2cfa
ER
799 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
800 (message
492d2437 801 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, r-ename, C-opy, h-elp"))
84fc2cfa 802
492d2437
RS
803(defun dired-undo ()
804 "Undo in a dired buffer.
805This doesn't recover lost files, it just undoes changes in the buffer itself.
806You can use it to recover marks, killed lines or subdirs.
807In the latter case, you have to do \\[dired-build-subdir-alist] to
808parse the buffer again."
809 (interactive)
810 (let (buffer-read-only)
811 (undo)))
84fc2cfa
ER
812
813(defun dired-next-line (arg)
814 "Move down lines then position at filename.
815Optional prefix ARG says how many lines to move; default is one line."
816 (interactive "p")
817 (next-line arg)
818 (dired-move-to-filename))
819
820(defun dired-previous-line (arg)
821 "Move up lines then position at filename.
822Optional prefix ARG says how many lines to move; default is one line."
823 (interactive "p")
824 (previous-line arg)
825 (dired-move-to-filename))
826
492d2437
RS
827(defun dired-next-dirline (arg &optional opoint)
828 "Goto ARG'th next directory file line."
829 (interactive "p")
830 (or opoint (setq opoint (point)))
831 (if (if (> arg 0)
832 (re-search-forward dired-re-dir nil t arg)
833 (beginning-of-line)
834 (re-search-backward dired-re-dir nil t (- arg)))
835 (dired-move-to-filename) ; user may type `i' or `f'
836 (goto-char opoint)
837 (error "No more subdirectories")))
838
839(defun dired-prev-dirline (arg)
840 "Goto ARG'th previous directory file line."
841 (interactive "p")
842 (dired-next-dirline (- arg)))
843
84fc2cfa 844(defun dired-up-directory ()
492d2437
RS
845 "Run dired on parent directory of current directory.
846Find the parent directory either in this buffer or another buffer.
847Creates a buffer if necessary."
84fc2cfa 848 (interactive)
492d2437
RS
849 (let* ((dir (dired-current-directory))
850 (up (file-name-directory (directory-file-name dir))))
851 (or (dired-goto-file (directory-file-name dir))
7b2469ae
RS
852 ;; Only try dired-goto-subdir if buffer has more than one dir.
853 (and (cdr dired-subdir-alist)
492d2437
RS
854 (dired-goto-subdir up))
855 (progn
856 (dired up)
857 (dired-goto-file dir)))))
84fc2cfa
ER
858
859(defun dired-find-file ()
860 "In dired, visit the file or directory named on this line."
861 (interactive)
c3554e95 862 (find-file (file-name-sans-versions (dired-get-filename) t)))
84fc2cfa
ER
863
864(defun dired-view-file ()
492d2437
RS
865 "In dired, examine a file in view mode, returning to dired when done.
866When file is a directory, show it in this buffer if it is inserted;
867otherwise, display it in another buffer."
84fc2cfa
ER
868 (interactive)
869 (if (file-directory-p (dired-get-filename))
7b2469ae
RS
870 (or (and (cdr dired-subdir-alist)
871 (dired-goto-subdir (dired-get-filename)))
492d2437 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))
7b2469ae 1228 (and (cdr dired-subdir-alist)
492d2437
RS
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
7b2469ae 1289 (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
492d2437
RS
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.
7b2469ae
RS
1376 (save-excursion (and (cdr dired-subdir-alist)
1377 (dired-goto-subdir fn)
492d2437
RS
1378 (dired-kill-subdir))))
1379\f
1380;; Confirmation
1381
1382(defun dired-marker-regexp ()
1383 (concat "^" (regexp-quote (char-to-string dired-marker-char))))
1384
1385(defun dired-plural-s (count)
1386 (if (= 1 count) "" "s"))
1387
1388(defun dired-mark-prompt (arg files)
1389 ;; Return a string for use in a prompt, either the current file
1390 ;; name, or the marker and a count of marked files.
1391 (let ((count (length files)))
1392 (if (= count 1)
1393 (car files)
1394 ;; more than 1 file:
1395 (if (integerp arg)
1396 ;; abs(arg) = count
1397 ;; Perhaps this is nicer, but it also takes more screen space:
1398 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
1399 ;; count)
1400 (format "[next %d files]" arg)
1401 (format "%c [%d files]" dired-marker-char count)))))
1402
1403(defun dired-pop-to-buffer (buf)
1404 ;; Pop up buffer BUF.
1405 ;; If dired-shrink-to-fit is t, make its window fit its contents.
1406 (if (not dired-shrink-to-fit)
1407 (pop-to-buffer (get-buffer-create buf))
1408 ;; let window shrink to fit:
1409 (let ((window (selected-window))
1410 target-lines w2)
1411 (cond ;; if split-window-threshold is enabled, use the largest window
1412 ((and (> (window-height (setq w2 (get-largest-window)))
1413 split-height-threshold)
f98955ea 1414 (= (frame-width) (window-width w2)))
492d2437
RS
1415 (setq window w2))
1416 ;; if the least-recently-used window is big enough, use it
1417 ((and (> (window-height (setq w2 (get-lru-window)))
1418 (* 2 window-min-height))
f98955ea 1419 (= (frame-width) (window-width w2)))
492d2437
RS
1420 (setq window w2)))
1421 (save-excursion
1422 (set-buffer buf)
1423 (goto-char (point-max))
1424 (skip-chars-backward "\n\r\t ")
1425 (setq target-lines (count-lines (point-min) (point))))
1426 (if (<= (window-height window) (* 2 window-min-height))
f98955ea 1427 ;; At this point, every window on the frame is too small to split.
492d2437
RS
1428 (setq w2 (display-buffer buf))
1429 (setq w2 (split-window window
1430 (max window-min-height
1431 (- (window-height window)
1432 (1+ (max window-min-height target-lines)))))))
1433 (set-window-buffer w2 buf)
1434 (if (< (1- (window-height w2)) target-lines)
1435 (progn
1436 (select-window w2)
1437 (enlarge-window (- target-lines (1- (window-height w2))))))
1438 (set-window-start w2 1)
1439 )))
1440
1441(defvar dired-no-confirm nil
1442;; "If non-nil, list of symbols for commands dired should not confirm.
1443;;It can be a sublist of
1444;;
1445;; '(byte-compile chgrp chmod chown compress copy delete hardlink load
1446;; move print shell symlink uncompress)"
1447 )
1448
1449(defun dired-mark-pop-up (bufname op-symbol files function &rest args)
1450 ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
1451 ;;Return FUNCTION's result on ARGS after popping up a window (in a buffer
1452 ;;named BUFNAME, nil gives \" *Marked Files*\") showing the marked
1453 ;;files. Uses function `dired-pop-to-buffer' to do that.
1454 ;; FUNCTION should not manipulate files.
1455 ;; It should only read input (an argument or confirmation).
1456 ;;The window is not shown if there is just one file or
1457 ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
1458 ;;FILES is the list of marked files."
1459 (or bufname (setq bufname " *Marked Files*"))
1460 (if (or (memq op-symbol dired-no-confirm)
1461 (= (length files) 1))
1462 (apply function args)
1463 (save-excursion
1464 (set-buffer (get-buffer-create bufname))
1465 (erase-buffer)
1466 (dired-format-columns-of-files files))
1467 (save-window-excursion
1468 (dired-pop-to-buffer bufname)
1469 (apply function args))))
1470
1471(defun dired-format-columns-of-files (files)
1472 ;; Files should be in forward order for this loop.
1473 ;; i.e., (car files) = first file in buffer.
1474 ;; Returns the number of lines used.
1475 (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files))))
1476 (width (- (window-width (selected-window)) 2))
1477 (columns (max 1 (/ width maxlen)))
1478 (nfiles (length files))
1479 (rows (+ (/ nfiles columns)
1480 (if (zerop (% nfiles columns)) 0 1)))
1481 (i 0)
1482 (j 0))
1483 (setq files (nconc (copy-sequence files) ; fill up with empty fns
1484 (make-list (- (* columns rows) nfiles) "")))
1485 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
1486 (while (< j rows)
1487 (while (< i columns)
1488 (indent-to (* i maxlen))
1489 (insert (car files))
1490 (setq files (nthcdr rows files)
1491 i (1+ i)))
1492 (insert "\n")
1493 (setq i 0
1494 j (1+ j)
1495 files (cdr files)))
1496 rows))
1497\f
1498;; Commands to mark or flag file(s) at or near current line.
1499
1500(defun dired-repeat-over-lines (arg function)
1501 ;; This version skips non-file lines.
1502 (beginning-of-line)
1503 (while (and (> arg 0) (not (eobp)))
1504 (setq arg (1- arg))
1505 (beginning-of-line)
1506 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
1507 (save-excursion (funcall function))
1508 (forward-line 1))
1509 (while (and (< arg 0) (not (bobp)))
1510 (setq arg (1+ arg))
1511 (forward-line -1)
1512 (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
1513 (beginning-of-line)
1514 (save-excursion (funcall function))
1515 (dired-move-to-filename))
1516 (dired-move-to-filename))
1517
1518(defun dired-between-files ()
1519 ;; Point must be at beginning of line
1520 ;; Should be equivalent to (save-excursion (not (dired-move-to-filename)))
1521 ;; but is about 1.5..2.0 times as fast. (Actually that's not worth it)
1522 (or (looking-at "^$\\|^. *$\\|^. total\\|^. wildcard")
1523 (looking-at dired-subdir-regexp)))
1524
1525(defun dired-next-marked-file (arg &optional wrap opoint)
1526 "Move to the next marked file, wrapping around the end of the buffer."
1527 (interactive "p\np")
1528 (or opoint (setq opoint (point)));; return to where interactively started
1529 (if (if (> arg 0)
1530 (re-search-forward dired-re-mark nil t arg)
1531 (beginning-of-line)
1532 (re-search-backward dired-re-mark nil t (- arg)))
1533 (dired-move-to-filename)
1534 (if (null wrap)
1535 (progn
1536 (goto-char opoint)
1537 (error "No next marked file"))
1538 (message "(Wraparound for next marked file)")
1539 (goto-char (if (> arg 0) (point-min) (point-max)))
1540 (dired-next-marked-file arg nil opoint))))
1541
1542(defun dired-prev-marked-file (arg &optional wrap)
1543 "Move to the previous marked file, wrapping around the end of the buffer."
1544 (interactive "p\np")
1545 (dired-next-marked-file (- arg) wrap))
1546
1547(defun dired-file-marker (file)
1548 ;; Return FILE's marker, or nil if unmarked.
1549 (save-excursion
1550 (and (dired-goto-file file)
1551 (progn
1552 (beginning-of-line)
1553 (if (not (equal ?\040 (following-char)))
1554 (following-char))))))
1555
1556(defun dired-mark-files-in-region (start end)
1557 (let (buffer-read-only)
1558 (if (> start end)
1559 (error "start > end"))
1560 (goto-char start) ; assumed at beginning of line
1561 (while (< (point) end)
1562 ;; Skip subdir line and following garbage like the `total' line:
1563 (while (and (< (point) end) (dired-between-files))
1564 (forward-line 1))
1565 (if (and (not (looking-at dired-re-dot))
1566 (dired-get-filename nil t))
1567 (progn
1568 (delete-char 1)
1569 (insert dired-marker-char)))
1570 (forward-line 1))))
1571
1572(defun dired-mark (arg)
1573 "Mark the current (or next ARG) files.
1574If on a subdir headerline, mark all its files except `.' and `..'.
1575
1576Use \\[dired-unmark-all-files] to remove all marks
1577and \\[dired-unmark] on a subdir to remove the marks in
1578this subdir."
1579 (interactive "P")
7b2469ae 1580 (if (and (cdr dired-subdir-alist) (dired-get-subdir))
492d2437
RS
1581 (save-excursion (dired-mark-subdir-files))
1582 (let (buffer-read-only)
1583 (dired-repeat-over-lines
52041219 1584 (prefix-numeric-value arg)
492d2437
RS
1585 (function (lambda () (delete-char 1) (insert dired-marker-char)))))))
1586
1587(defun dired-unmark (arg)
1588 "Unmark the current (or next ARG) files.
1589If looking at a subdir, unmark all its files except `.' and `..'."
1590 (interactive "P")
1591 (let ((dired-marker-char ?\040))
1592 (dired-mark arg)))
1593
1594(defun dired-flag-file-deletion (arg)
1595 "In dired, flag the current line's file for deletion.
1596With prefix arg, repeat over several lines.
1597
1598If on a subdir headerline, mark all its files except `.' and `..'."
1599 (interactive "P")
1600 (let ((dired-marker-char dired-del-marker))
1601 (dired-mark arg)))
1602
1603(defun dired-unmark-backward (arg)
1604 "In dired, move up lines and remove deletion flag there.
1605Optional prefix ARG says how many lines to unflag; default is one line."
1606 (interactive "p")
1607 (dired-unmark (- arg)))
84fc2cfa 1608\f
492d2437
RS
1609;;; Commands to mark or flag files based on their characteristics or names.
1610
d2cadc4b
RS
1611(defvar dired-regexp-history nil
1612 "History list of regular expressions used in Dired commands.")
1613
1614(defun dired-read-regexp (prompt)
1615 (read-from-minibuffer prompt nil nil nil 'dired-regexp-history))
492d2437
RS
1616
1617(defun dired-mark-files-regexp (regexp &optional marker-char)
1618 "Mark all files matching REGEXP for use in later commands.
1619A prefix argument means to unmark them instead.
1620`.' and `..' are never marked.
1621
1622REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
1623object files--just `.o' will mark more than you might think."
1624 (interactive
1625 (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
1626 " files (regexp): "))
1627 (if current-prefix-arg ?\040)))
1628 (let ((dired-marker-char (or marker-char dired-marker-char)))
1629 (dired-mark-if
1630 (and (not (looking-at dired-re-dot))
1631 (not (eolp)) ; empty line
1632 (let ((fn (dired-get-filename nil t)))
1633 (and fn (string-match regexp (file-name-nondirectory fn)))))
1634 "matching file")))
1635
1636(defun dired-flag-files-regexp (regexp)
1637 "In dired, flag all files containing the specified REGEXP for deletion.
1638The match is against the non-directory part of the filename. Use `^'
1639 and `$' to anchor matches. Exclude subdirs by hiding them.
1640`.' and `..' are never flagged."
1641 (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
1642 (dired-mark-files-regexp regexp dired-del-marker))
1643
1644(defun dired-mark-symlinks (unflag-p)
1645 "Mark all symbolic links.
1646With prefix argument, unflag all those files."
1647 (interactive "P")
1648 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
1649 (dired-mark-if (looking-at dired-re-sym) "symbolic link")))
1650
1651(defun dired-mark-directories (unflag-p)
1652 "Mark all directory file lines except `.' and `..'.
1653With prefix argument, unflag all those files."
1654 (interactive "P")
1655 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
1656 (dired-mark-if (and (looking-at dired-re-dir)
1657 (not (looking-at dired-re-dot)))
1658 "directory file")))
1659
1660(defun dired-mark-executables (unflag-p)
1661 "Mark all executable files.
1662With prefix argument, unflag all those files."
1663 (interactive "P")
1664 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
1665 (dired-mark-if (looking-at dired-re-exe) "executable file")))
1666
1667;; dired-x.el has a dired-mark-sexp interactive command: mark
1668;; files for which PREDICATE returns non-nil.
1669
1670(defun dired-flag-auto-save-files (&optional unflag-p)
84fc2cfa
ER
1671 "Flag for deletion files whose names suggest they are auto save files.
1672A prefix argument says to unflag those files instead."
1673 (interactive "P")
492d2437
RS
1674 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
1675 (dired-mark-if
1676 (and (not (looking-at dired-re-dir))
1677 (let ((fn (dired-get-filename t t)))
1678 (if fn (auto-save-file-name-p
1679 (file-name-nondirectory fn)))))
1680 "auto save file")))
1681
1682(defun dired-flag-backup-files (&optional unflag-p)
1683 "Flag all backup files (names ending with `~') for deletion.
1684With prefix argument, unflag these files."
1685 (interactive "P")
1686 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
1687 (dired-mark-if
1688 (and (not (looking-at dired-re-dir))
1689 (let ((fn (dired-get-filename t t)))
1690 (if fn (backup-file-name-p fn))))
1691 "backup file")))
1692
6482fcac
RS
1693(defun dired-change-marks (&optional old new)
1694 "Change all OLD marks to NEW marks.
1695OLD and NEW are both characters used to mark files."
1696 (interactive
1697 (let* ((cursor-in-echo-area t)
1698 (old (progn (message "Change (old mark): ") (read-char)))
1699 (new (progn (message "Change %c marks to (new mark): " old)
1700 (read-char))))
1701 (list old new)))
1702 (let ((regexp (format "^%s" (regexp-quote old)))
1703 (buffer-read-only))
1704 (save-excursion
1705 (goto-char (point-min))
1706 (while (re-search-forward regexp nil t)
1707 (beginning-of-line)
1708 (delete-region (point) (1+ (point)))
1709 (insert-char new 1)))))
1710
492d2437
RS
1711(defun dired-unmark-all-files (flag &optional arg)
1712 "Remove a specific mark or any mark from every file.
3585916f 1713With prefix arg, query for each marked file.
492d2437 1714Type \\[help-command] at that time for help."
3585916f
RS
1715 (interactive
1716 (let* ((cursor-in-echo-area t))
1717 (list (progn (message "Remove marks (RET means all): ") (read-char))
1718 current-prefix-arg)))
492d2437
RS
1719 (let ((count 0)
1720 (re (if (zerop (length flag)) dired-re-mark
1721 (concat "^" (regexp-quote flag)))))
1722 (save-excursion
1723 (let (buffer-read-only case-fold-search query
1724 (help-form "\
1725Type SPC or `y' to unflag one file, DEL or `n' to skip to next,
1726`!' to unflag all remaining files with no more questions."))
1727 (goto-char (point-min))
1728 (while (re-search-forward re nil t)
1729 (if (or (not arg)
1730 (dired-query 'query "Unmark file `%s'? "
1731 (dired-get-filename t)))
1732 (progn (delete-char -1) (insert " ") (setq count (1+ count))))
1733 (forward-line 1))))
1734 (message "%s" (format "Flags removed: %d %s" count flag) )))
1735\f
492d2437 1736;; Logging failures operating on files, and showing the results.
84fc2cfa 1737
492d2437 1738(defvar dired-log-buffer "*Dired log*")
84fc2cfa 1739
492d2437
RS
1740(defun dired-why ()
1741 "Pop up a buffer with error log output from Dired.
1742A group of errors from a single command ends with a formfeed.
1743Thus, use \\[backward-page] to find the beginning of a group of errors."
1744 (interactive)
1745 (if (get-buffer dired-log-buffer)
1746 (let ((owindow (selected-window))
1747 (window (display-buffer (get-buffer dired-log-buffer))))
1748 (unwind-protect
6482fcac 1749 (progn
492d2437
RS
1750 (select-window window)
1751 (goto-char (point-max))
1752 (recenter -1))
1753 (select-window owindow)))))
1754
1755(defun dired-log (log &rest args)
1756 ;; Log a message or the contents of a buffer.
1757 ;; If LOG is a string and there are more args, it is formatted with
1758 ;; those ARGS. Usually the LOG string ends with a \n.
1759 ;; End each bunch of errors with (dired-log t): this inserts
1760 ;; current time and buffer, and a \f (formfeed).
1761 (let ((obuf (current-buffer)))
1762 (unwind-protect ; want to move point
1763 (progn
1764 (set-buffer (get-buffer-create dired-log-buffer))
1765 (goto-char (point-max))
1766 (let (buffer-read-only)
1767 (cond ((stringp log)
1768 (insert (if args
1769 (apply (function format) log args)
1770 log)))
1771 ((bufferp log)
1772 (insert-buffer log))
1773 ((eq t log)
1774 (insert "\n\t" (current-time-string)
1775 "\tBuffer `" (buffer-name obuf) "'\n\f\n")))))
1776 (set-buffer obuf))))
1777
1778(defun dired-log-summary (string failures)
1779 (message (if failures "%s--type ? for details (%s)"
1780 "%s--type ? for details")
1781 string failures)
1782 ;; Log a summary describing a bunch of errors.
1783 (dired-log (concat "\n" string))
1784 (dired-log t))
1785\f
1786;;; Sorting
1787
1788;; Most ls can only sort by name or by date (with -t), nothing else.
1789;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
1790;; So anything that does not contain these is sort "by name".
1791
1792(defvar dired-ls-sorting-switches "SXU"
1793 "String of `ls' switches (single letters) except `t' that influence sorting.")
1794
1795(defvar dired-sort-by-date-regexp
1796 (concat "^-[^" dired-ls-sorting-switches
1797 "]*t[^" dired-ls-sorting-switches "]*$")
1798 "Regexp recognized by dired to set `by date' mode.")
1799
1800(defvar dired-sort-by-name-regexp
1801 (concat "^-[^t" dired-ls-sorting-switches "]+$")
1802 "Regexp recognized by dired to set `by name' mode.")
1803
1804(defvar dired-sort-mode nil
1805 "Whether Dired sorts by name, date etc. (buffer-local).")
1806;; This is nil outside dired buffers so it can be used in the modeline
1807
1808(defun dired-sort-set-modeline ()
1809 ;; Set modeline display according to dired-actual-switches.
1810 ;; Modeline display of "by name" or "by date" guarantees the user a
1811 ;; match with the corresponding regexps. Non-matching switches are
1812 ;; shown literally.
1813 (setq dired-sort-mode
1814 (let (case-fold-search)
1815 (cond ((string-match dired-sort-by-name-regexp dired-actual-switches)
1816 " by name")
1817 ((string-match dired-sort-by-date-regexp dired-actual-switches)
1818 " by date")
1819 (t
1820 (concat " " dired-actual-switches)))))
1821 ;; update mode line:
1822 (set-buffer-modified-p (buffer-modified-p)))
1823
1824(defun dired-sort-toggle-or-edit (&optional arg)
1825 "Toggle between sort by date/name and refresh the dired buffer.
1826With a prefix argument you can edit the current listing switches instead."
84fc2cfa 1827 (interactive "P")
492d2437
RS
1828 (if arg
1829 (dired-sort-other
1830 (read-string "ls switches (must contain -l): " dired-actual-switches))
1831 (dired-sort-toggle)))
1832
1833(defun dired-sort-toggle ()
1834 ;; Toggle between sort by date/name. Reverts the buffer.
1835 (setq dired-actual-switches
1836 (let (case-fold-search)
1837 (concat
1838 "-l"
1839 (dired-replace-in-string (concat "[---lt"
1840 dired-ls-sorting-switches "]")
1841 ""
1842 dired-actual-switches)
1843 (if (string-match (concat "[t" dired-ls-sorting-switches "]")
1844 dired-actual-switches)
1845 ""
1846 "t"))))
1847 (dired-sort-set-modeline)
1848 (revert-buffer))
1849
1850(defun dired-replace-in-string (regexp newtext string)
1851 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
1852 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
1853 (let ((result "") (start 0) mb me)
1854 (while (string-match regexp string start)
1855 (setq mb (match-beginning 0)
1856 me (match-end 0)
1857 result (concat result (substring string start mb) newtext)
1858 start me))
1859 (concat result (substring string start))))
1860
1861(defun dired-sort-other (switches &optional no-revert)
1862 ;; Specify new ls SWITCHES for current dired buffer. Values matching
1863 ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
1864 ;; minor mode accordingly, others appear literally in the mode line.
1865 ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
1866 (setq dired-actual-switches switches)
1867 (dired-sort-set-modeline)
1868 (or no-revert (revert-buffer)))
84fc2cfa 1869\f
492d2437
RS
1870;; To make this file smaller, the less common commands
1871;; go in a separate file. But autoload them here
1872;; to make the separation invisible.
1873
1874(autoload 'dired-diff "dired-aux"
1875 "Compare file at point with file FILE using `diff'.
1876FILE defaults to the file at the mark.
ab67260b 1877The prompted-for file is the first file given to `diff'."
492d2437
RS
1878 t)
1879
1880(autoload 'dired-backup-diff "dired-aux"
1881 "Diff this file with its backup file or vice versa.
1882Uses the latest backup, if there are several numerical backups.
1883If this file is a backup, diff it with its original.
ab67260b 1884The backup file is the first file given to `diff'."
492d2437
RS
1885 t)
1886
2d051399
RS
1887(autoload 'dired-clean-directory "dired-aux"
1888 "Flag numerical backups for deletion.
1889Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
1890Positive prefix arg KEEP overrides `dired-kept-versions';
1891Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
1892
1893To clear the flags on these files, you can use \\[dired-flag-backup-files]
1894with a prefix argument."
1895 t)
1896
492d2437
RS
1897(autoload 'dired-do-chmod "dired-aux"
1898 "Change the mode of the marked (or next ARG) files.
1899This calls chmod, thus symbolic modes like `g+w' are allowed."
1900 t)
1901
1902(autoload 'dired-do-chgrp "dired-aux"
1903 "Change the group of the marked (or next ARG) files."
1904 t)
1905
1906(autoload 'dired-do-chown "dired-aux"
1907 "Change the owner of the marked (or next ARG) files."
1908 t)
1909
1910(autoload 'dired-do-print "dired-aux"
1911 "Print the marked (or next ARG) files.
1912Uses the shell command coming from variables `lpr-command' and
1913`lpr-switches' as default."
1914 t)
1915
1916(autoload 'dired-do-shell-command "dired-aux"
6482fcac
RS
1917 "Run a shell command COMMAND on the marked files.
1918If no files are marked or a specific numeric prefix arg is given,
1919the next ARG files are used. Just \\[universal-argument] means the current file.
1920The prompt mentions the file(s) or the marker, as appropriate.
1921
492d2437 1922If there is output, it goes to a separate buffer.
6482fcac 1923
492d2437
RS
1924Normally the command is run on each file individually.
1925However, if there is a `*' in the command then it is run
1926just once with the entire file list substituted there.
1927
6482fcac
RS
1928No automatic redisplay of dired buffers is attempted, as there's no
1929telling what files the command may have changed. Type
1930\\[dired-do-redisplay] to redisplay the marked files.
492d2437
RS
1931
1932The shell command has the top level directory as working directory, so
1933output files usually are created there instead of in a subdir."
1934 t)
1935
492d2437
RS
1936(autoload 'dired-do-kill-lines "dired-aux"
1937 "Kill all marked lines (not the files).
1938With a prefix arg, kill all lines not marked or flagged."
1939 t)
1940
1941(autoload 'dired-do-compress "dired-aux"
1942 "Compress or uncompress marked (or next ARG) files."
1943 t)
1944
1945(autoload 'dired-do-byte-compile "dired-aux"
1946 "Byte compile marked (or next ARG) Emacs Lisp files."
1947 t)
1948
1949(autoload 'dired-do-load "dired-aux"
1950 "Load the marked (or next ARG) Emacs Lisp files."
1951 t)
1952
1953(autoload 'dired-do-redisplay "dired-aux"
1954 "Redisplay all marked (or next ARG) files.
1955If on a subdir line, redisplay that subdirectory. In that case,
1956a prefix arg lets you edit the `ls' switches used for the new listing."
1957 t)
1958
1959(autoload 'dired-string-replace-match "dired-aux"
1960 "Replace first match of REGEXP in STRING with NEWTEXT.
1961If it does not match, nil is returned instead of the new string.
1962Optional arg LITERAL means to take NEWTEXT literally.
1963Optional arg GLOBAL means to replace all matches."
1964 t)
1965
1966(autoload 'dired-create-directory "dired-aux"
84fc2cfa 1967 "Create a directory called DIRECTORY."
492d2437 1968 t)
84fc2cfa 1969
492d2437
RS
1970(autoload 'dired-do-copy "dired-aux"
1971 "Copy all marked (or next ARG) files, or copy the current file.
1972Thus, a zero prefix argument copies nothing. But it toggles the
1973variable `dired-copy-preserve-time' (which see)."
1974 t)
1975
1976(autoload 'dired-do-symlink "dired-aux"
1977 "Make symbolic links to current file or all marked (or next ARG) files.
1978When operating on just the current file, you specify the new name.
1979When operating on multiple or marked files, you specify a directory
1980and new symbolic links are made in that directory
1981with the same names that the files currently have."
1982 t)
1983
1984(autoload 'dired-do-hardlink "dired-aux"
1985 "Add names (hard links) current file or all marked (or next ARG) files.
1986When operating on just the current file, you specify the new name.
1987When operating on multiple or marked files, you specify a directory
1988and new hard links are made in that directory
1989with the same names that the files currently have."
1990 t)
1991
1992(autoload 'dired-do-rename "dired-aux"
1993 "Rename current file or all marked (or next ARG) files.
1994When renaming just the current file, you specify the new name.
1995When renaming multiple or marked files, you specify a directory."
1996 t)
1997
1998(autoload 'dired-do-rename-regexp "dired-aux"
1999 "Rename marked files containing REGEXP to NEWNAME.
2000As each match is found, the user must type a character saying
2001 what to do with it. For directions, type \\[help-command] at that time.
2002NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
2003REGEXP defaults to the last regexp used.
2004With a zero prefix arg, renaming by regexp affects the complete
2005 pathname - usually only the non-directory part of file names is used
2006 and changed."
2007 t)
2008
2009(autoload 'dired-do-copy-regexp "dired-aux"
2010 "Copy all marked files containing REGEXP to NEWNAME.
2011See function `dired-rename-regexp' for more info."
2012 t)
2013
2014(autoload 'dired-do-hardlink-regexp "dired-aux"
2015 "Hardlink all marked files containing REGEXP to NEWNAME.
2016See function `dired-rename-regexp' for more info."
2017 t)
2018
2019(autoload 'dired-do-symlink-regexp "dired-aux"
2020 "Symlink all marked files containing REGEXP to NEWNAME.
2021See function `dired-rename-regexp' for more info."
2022 t)
2023
2024(autoload 'dired-upcase "dired-aux"
2025 "Rename all marked (or next ARG) files to upper case."
2026 t)
2027
2028(autoload 'dired-downcase "dired-aux"
2029 "Rename all marked (or next ARG) files to lower case."
2030 t)
2031
2032(autoload 'dired-maybe-insert-subdir "dired-aux"
2033 "Insert this subdirectory into the same dired buffer.
2034If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
2035 else inserts it at its natural place (as `ls -lR' would have done).
2036With a prefix arg, you may edit the ls switches used for this listing.
2037 You can add `R' to the switches to expand the whole tree starting at
2038 this subdirectory.
2039This function takes some pains to conform to `ls -lR' output."
2040 t)
2041
2042(autoload 'dired-next-subdir "dired-aux"
2043 "Go to next subdirectory, regardless of level."
2044 t)
2045
2046(autoload 'dired-prev-subdir "dired-aux"
2047 "Go to previous subdirectory, regardless of level.
2048When called interactively and not on a subdir line, go to this subdir's line."
2049 t)
2050
2051(autoload 'dired-goto-subdir "dired-aux"
2052 "Go to end of header line of DIR in this dired buffer.
2053Return value of point on success, otherwise return nil.
2054The next char is either \\n, or \\r if DIR is hidden."
2055 t)
2056
2057(autoload 'dired-mark-subdir-files "dired-aux"
2058 "Mark all files except `.' and `..'."
2059 t)
2060
2061(autoload 'dired-kill-subdir "dired-aux"
2062 "Remove all lines of current subdirectory.
2063Lower levels are unaffected."
2064 t)
2065
2066(autoload 'dired-tree-up "dired-aux"
2067 "Go up ARG levels in the dired tree."
2068 t)
2069
2070(autoload 'dired-tree-down "dired-aux"
2071 "Go down in the dired tree."
2072 t)
2073
2074(autoload 'dired-hide-subdir "dired-aux"
2075 "Hide or unhide the current subdirectory and move to next directory.
2076Optional prefix arg is a repeat factor.
2077Use \\[dired-hide-all] to (un)hide all directories."
2078 t)
2079
2080(autoload 'dired-hide-all "dired-aux"
2081 "Hide all subdirectories, leaving only their header lines.
2082If there is already something hidden, make everything visible again.
2083Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2084 t)
84fc2cfa 2085\f
492d2437
RS
2086(if (eq system-type 'vax-vms)
2087 (load "dired-vms"))
84fc2cfa 2088
492d2437 2089(run-hooks 'dired-load-hook) ; for your customizations
84fc2cfa 2090
52041219
RS
2091(provide 'dired)
2092
2093;;; dired.el ends here