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