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