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