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