Require CL for `case'.
[bpt/emacs.git] / lisp / dired.el
CommitLineData
c8472948 1;;; dired.el --- directory-browsing commands
52041219 2
470fa6d1 3;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 1997, 2000, 2001, 2003
1b85bd12 4;; Free Software Foundation, Inc.
ab67260b 5
b36b40ae
RS
6;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
7;; Maintainer: FSF
565132a3 8;; Keywords: files
84fc2cfa
ER
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
52041219 14;; the Free Software Foundation; either version 2, or (at your option)
84fc2cfa
ER
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
84fc2cfa 26
52041219
RS
27;;; Commentary:
28
e41b2db1
ER
29;; This is a major mode for directory browsing and editing. It is
30;; documented in the Emacs manual.
31
492d2437
RS
32;; Rewritten in 1990/1991 to add tree features, file marking and
33;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
34;; Finished up by rms in 1992.
35
52041219 36;;; Code:
492d2437 37
492d2437
RS
38;;; Customizable variables
39
286c247d
RS
40(defgroup dired nil
41 "Directory editing."
ca66efd1 42 :group 'files)
286c247d
RS
43
44(defgroup dired-mark nil
68d2f12f 45 "Handling marks in Dired."
286c247d
RS
46 :prefix "dired-"
47 :group 'dired)
48
49
84fc2cfa 50;;;###autoload
286c247d 51(defcustom dired-listing-switches "-al"
492d2437
RS
52 "*Switches passed to `ls' for dired. MUST contain the `l' option.
53May contain all other options that don't contradict `-l';
b36b40ae 54may contain even `F', `b', `i' and `s'. See also the variable
48404d5a
EZ
55`dired-ls-F-marks-symlinks' concerning the `F' switch.
56On systems such as MS-DOS and MS-Windows, which use `ls' emulation in Lisp,
57some of the `ls' switches are not supported; see the doc string of
58`insert-directory' on ls-lisp.el for more details."
286c247d
RS
59 :type 'string
60 :group 'dired)
84fc2cfa 61
470fa6d1 62; Don't use absolute file names as /bin should be in any PATH and people
492d2437
RS
63; may prefer /usr/local/gnu/bin or whatever. However, chown is
64; usually not in PATH.
65
66;;;###autoload
84fc2cfa 67(defvar dired-chown-program
c60ee5e7 68 (if (memq system-type '(hpux dgux usg-unix-v irix linux gnu/linux cygwin))
6702f50d
RS
69 "chown"
70 (if (file-exists-p "/usr/sbin/chown")
71 "/usr/sbin/chown"
72 "/etc/chown"))
eb8c3be9 73 "Name of chown command (usually `chown' or `/etc/chown').")
492d2437 74
0b7bc76f
RS
75(defvar dired-use-ls-dired (not (not (string-match "gnu" system-configuration)))
76 "Non-nil means Dired should use `ls --dired'.")
77
912aace8
GV
78(defvar dired-chmod-program "chmod"
79 "Name of chmod command (usually `chmod').")
723ddb1f 80
492d2437 81;;;###autoload
286c247d 82(defcustom dired-ls-F-marks-symlinks nil
492d2437 83 "*Informs dired about how `ls -lF' marks symbolic links.
fc83abf7
RS
84Set this to t if `ls' (or whatever program is specified by
85`insert-directory-program') with `-lF' marks the symbolic link
492d2437 86itself with a trailing @ (usually the case under Ultrix).
84fc2cfa 87
492d2437
RS
88Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
89nil (the default), if it gives `bar@ -> foo', set it to t.
90
91Dired checks if there is really a @ appended. Thus, if you have a
92marking `ls' program on one host and a non-marking on another host, and
93don't care about symbolic links which really end in a @, you can
286c247d
RS
94always set this variable to t."
95 :type 'boolean
96 :group 'dired-mark)
492d2437
RS
97
98;;;###autoload
286c247d 99(defcustom dired-trivial-filenames "^\\.\\.?$\\|^#"
492d2437
RS
100 "*Regexp of files to skip when finding first file of a directory.
101A value of nil means move to the subdir line.
286c247d
RS
102A value of t means move to first file."
103 :type '(choice (const :tag "Move to subdir" nil)
104 (const :tag "Move to first" t)
105 regexp)
106 :group 'dired)
492d2437
RS
107
108;;;###autoload
286c247d 109(defcustom dired-keep-marker-rename t
492d2437
RS
110 ;; Use t as default so that moved files "take their markers with them".
111 "*Controls marking of renamed files.
112If t, files keep their previous marks when they are renamed.
113If a character, renamed files (whether previously marked or not)
286c247d
RS
114are afterward marked with that character."
115 :type '(choice (const :tag "Keep" t)
116 (character :tag "Mark"))
117 :group 'dired-mark)
492d2437
RS
118
119;;;###autoload
286c247d 120(defcustom dired-keep-marker-copy ?C
492d2437
RS
121 "*Controls marking of copied files.
122If t, copied files are marked if and as the corresponding original files were.
286c247d
RS
123If a character, copied files are unconditionally marked with that character."
124 :type '(choice (const :tag "Keep" t)
125 (character :tag "Mark"))
126 :group 'dired-mark)
492d2437
RS
127
128;;;###autoload
286c247d 129(defcustom dired-keep-marker-hardlink ?H
492d2437
RS
130 "*Controls marking of newly made hard links.
131If t, they are marked if and as the files linked to were marked.
286c247d
RS
132If a character, new links are unconditionally marked with that character."
133 :type '(choice (const :tag "Keep" t)
134 (character :tag "Mark"))
135 :group 'dired-mark)
492d2437
RS
136
137;;;###autoload
286c247d 138(defcustom dired-keep-marker-symlink ?Y
492d2437
RS
139 "*Controls marking of newly made symbolic links.
140If t, they are marked if and as the files linked to were marked.
286c247d
RS
141If a character, new links are unconditionally marked with that character."
142 :type '(choice (const :tag "Keep" t)
143 (character :tag "Mark"))
144 :group 'dired-mark)
492d2437
RS
145
146;;;###autoload
286c247d 147(defcustom dired-dwim-target nil
492d2437
RS
148 "*If non-nil, dired tries to guess a default target directory.
149This means: if there is a dired buffer displayed in the next window,
150use its current subdir, instead of the current subdir of this dired buffer.
151
286c247d
RS
152The target is used in the prompt for file copy, rename etc."
153 :type 'boolean
154 :group 'dired)
492d2437
RS
155
156;;;###autoload
286c247d 157(defcustom dired-copy-preserve-time t
492d2437 158 "*If non-nil, Dired preserves the last-modified time in a file copy.
286c247d
RS
159\(This works on only some systems.)"
160 :type 'boolean
161 :group 'dired)
492d2437 162
05b855f5
EZ
163; These variables were deleted and the replacements are on files.el.
164; We leave aliases behind for back-compatibility.
165(defvaralias 'dired-free-space-program 'directory-free-space-program)
166(defvaralias 'dired-free-space-args 'directory-free-space-args)
167
492d2437
RS
168;;; Hook variables
169
c2792945 170(defcustom dired-load-hook nil
492d2437 171 "Run after loading dired.
c2792945
EZ
172You can customize key bindings or load extensions with this."
173 :group 'dired
174 :type 'hook)
492d2437 175
c2792945
EZ
176(defcustom dired-mode-hook nil
177 "Run at the very end of dired-mode."
178 :group 'dired
179 :type 'hook)
492d2437 180
c2792945
EZ
181(defcustom dired-before-readin-hook nil
182 "This hook is run before a dired buffer is read in (created or reverted)."
183 :group 'dired
184 :type 'hook)
492d2437 185
c2792945 186(defcustom dired-after-readin-hook nil
492d2437
RS
187 "Hook run after each time a file or directory is read by Dired.
188After each listing of a file or directory, this hook is run
c2792945
EZ
189with the buffer narrowed to the listing."
190 :group 'dired
191 :type 'hook)
492d2437
RS
192;; Note this can't simply be run inside function `dired-ls' as the hook
193;; functions probably depend on the dired-subdir-alist to be OK.
194
55a87770
RS
195(defcustom dired-view-command-alist
196 '(("[.]\\(ps\\|ps_pages\\|eps\\)\\'" . "gv -spartan -color -watch %s")
197 ("[.]pdf\\'" . "xpdf %s")
198 ("[.]\\(jpe?g\\|gif\\|png\\)\\'" . "eog %s")
199 ("[.]dvi\\'" . "xdvi -sidemargin 0.5 -topmargin 1 %s"))
200 "Alist specifying how to view special types of files.
201Each element has the form (REGEXP . SHELL-COMMAND).
202When the file name matches REGEXP, `dired-view-file'
203invokes SHELL-COMMAND to view the file, processing it through `format'.
204Use `%s' in SHELL-COMMAND to specify where to put the file name."
205 :group 'dired
206 :type '(alist :key-type regexp :value-type string)
207 :version "21.4")
208
2b2059d8 209;; Internal variables
492d2437
RS
210
211(defvar dired-marker-char ?* ; the answer is 42
212 ;; so that you can write things like
213 ;; (let ((dired-marker-char ?X))
214 ;; ;; great code using X markers ...
215 ;; )
216 ;; For example, commands operating on two sets of files, A and B.
217 ;; Or marking files with digits 0-9. This could implicate
218 ;; concentric sets or an order for the marked files.
219 ;; The code depends on dynamic scoping on the marker char.
220 "In Dired, the current mark character.
221This is what the `do' commands look for and what the `mark' commands store.")
222
223(defvar dired-del-marker ?D
224 "Character used to flag files for deletion.")
225
226(defvar dired-shrink-to-fit
ab67260b
RS
227 t
228;; I see no reason ever to make this nil -- rms.
229;; (> baud-rate search-slow-speed)
492d2437
RS
230 "Non-nil means Dired shrinks the display buffer to fit the marked files.")
231
232(defvar dired-flagging-regexp nil);; Last regexp used to flag files.
233
ab67260b
RS
234(defvar dired-file-version-alist)
235
e956c609 236;;;###autoload
492d2437 237(defvar dired-directory nil
0b7bc76f 238 "The directory name or wildcard spec that this Dired directory lists.
8d23c16b 239Local to each dired buffer. May be a list, in which case the car is the
0b7bc76f
RS
240directory name and the cdr is the list of files to mention.
241The directory name must be absolute, but need not be fully expanded.")
492d2437
RS
242
243(defvar dired-actual-switches nil
244 "The value of `dired-listing-switches' used to make this buffer's text.")
245
246(defvar dired-re-inode-size "[0-9 \t]*"
247 "Regexp for optional initial inode and file size as made by `ls -i -s'.")
248
249;; These regexps must be tested at beginning-of-line, but are also
250;; used to search for next matches, so neither omitting "^" nor
251;; replacing "^" by "\n" (to make it slightly faster) will work.
252
253(defvar dired-re-mark "^[^ \n]")
254;; "Regexp matching a marked line.
255;; Important: the match ends just after the marker."
256(defvar dired-re-maybe-mark "^. ")
312a9e03
EZ
257;; The [^:] part after "d" and "l" is to avoid confusion with the
258;; DOS/Windows-style drive letters in directory names, like in "d:/foo".
259(defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d[^:]"))
260(defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l[^:]"))
492d2437
RS
261(defvar dired-re-exe;; match ls permission string of an executable file
262 (mapconcat (function
263 (lambda (x)
264 (concat dired-re-maybe-mark dired-re-inode-size x)))
265 '("-[-r][-w][xs][-r][-w].[-r][-w]."
266 "-[-r][-w].[-r][-w][xs][-r][-w]."
267 "-[-r][-w].[-r][-w].[-r][-w][xst]")
268 "\\|"))
e6cecd2b 269(defvar dired-re-perms "[-bcdlps][-r][-w].[-r][-w].[-r][-w].")
147e214c 270(defvar dired-re-dot "^.* \\.\\.?/?$")
492d2437 271
38819778 272;; The subdirectory names in this list are expanded.
492d2437
RS
273(defvar dired-subdir-alist nil
274 "Association list of subdirectories and their buffer positions.
275Each subdirectory has an element: (DIRNAME . STARTMARKER).
7b2469ae
RS
276The order of elements is the reverse of the order in the buffer.
277In simple cases, this list contains one element.")
492d2437 278
cdf156a9 279(defvar dired-subdir-regexp "^. \\([^\n\r]+\\)\\(:\\)[\n\r]"
492d2437
RS
280 "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
281Subexpression 1 is the subdirectory proper, no trailing colon.
282The match starts at the beginning of the line and ends after the end
283of the line (\\n or \\r).
284Subexpression 2 must end right before the \\n or \\r.")
285
2439570d
SM
286(defvar dired-font-lock-keywords
287 (list
288 ;;
289 ;; Directory headers.
290 (list dired-subdir-regexp '(1 font-lock-type-face))
291 ;;
292 ;; We make heavy use of MATCH-ANCHORED, since the regexps don't identify the
293 ;; file name itself. We search for Dired defined regexps, and then use the
294 ;; Dired defined function `dired-move-to-filename' before searching for the
295 ;; simple regexp ".+". It is that regexp which matches the file name.
296 ;;
297 ;; Dired marks.
298 (list dired-re-mark
883212ce 299 '(0 font-lock-constant-face)
6784714b 300 '(".+" (dired-move-to-filename) nil (0 font-lock-warning-face)))
4fdf389a
RS
301 ;; People who are paranoid about security would consider this more
302 ;; important than other things such as whether it is a directory.
303 ;; But we don't want to encourage paranoia, so our default
304 ;; should be what's most useful for non-paranoids. -- rms.
305;;; ;;
306;;; ;; Files that are group or world writable.
307;;; (list (concat dired-re-maybe-mark dired-re-inode-size
308;;; "\\([-d]\\(....w....\\|.......w.\\)\\)")
309;;; '(1 font-lock-comment-face)
310;;; '(".+" (dired-move-to-filename) nil (0 font-lock-comment-face)))
2439570d
SM
311 ;;
312 ;; Subdirectories.
313 (list dired-re-dir
6784714b 314 '(".+" (dired-move-to-filename) nil (0 font-lock-function-name-face)))
2439570d
SM
315 ;;
316 ;; Symbolic links.
c60ee5e7 317 (list dired-re-sym
6784714b 318 '(".+" (dired-move-to-filename) nil (0 font-lock-keyword-face)))
2439570d
SM
319 ;;
320 ;; Files suffixed with `completion-ignored-extensions'.
321 '(eval .
2b2059d8
SM
322 ;; It is quicker to first find just an extension, then go back to the
323 ;; start of that file name. So we do this complex MATCH-ANCHORED form.
324 (list (concat "\\(" (regexp-opt completion-ignored-extensions) "\\|#\\)$")
325 '(".+" (dired-move-to-filename) nil (0 font-lock-string-face)))))
2439570d 326 "Additional expressions to highlight in Dired mode.")
83fadedf 327\f
492d2437
RS
328;;; Macros must be defined before they are used, for the byte compiler.
329
330;; Mark all files for which CONDITION evals to non-nil.
331;; CONDITION is evaluated on each line, with point at beginning of line.
332;; MSG is a noun phrase for the type of files being marked.
333;; It should end with a noun that can be pluralized by adding `s'.
334;; Return value is the number of files marked, or nil if none were marked.
335(defmacro dired-mark-if (predicate msg)
8a946354
SS
336 `(let (buffer-read-only count)
337 (save-excursion
338 (setq count 0)
339 (if ,msg (message "Marking %ss..." ,msg))
340 (goto-char (point-min))
341 (while (not (eobp))
342 (if ,predicate
343 (progn
344 (delete-char 1)
345 (insert dired-marker-char)
346 (setq count (1+ count))))
347 (forward-line 1))
348 (if ,msg (message "%s %s%s %s%s."
349 count
350 ,msg
351 (dired-plural-s count)
352 (if (eq dired-marker-char ?\040) "un" "")
353 (if (eq dired-marker-char dired-del-marker)
354 "flagged" "marked"))))
355 (and (> count 0) count)))
492d2437
RS
356
357(defmacro dired-map-over-marks (body arg &optional show-progress)
91947235
SM
358 "Eval BODY with point on each marked line. Return a list of BODY's results.
359If no marked file could be found, execute BODY on the current line.
360 If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
361 files instead of the marked files.
362 In that case point is dragged along. This is so that commands on
363 the next ARG (instead of the marked) files can be chained easily.
364 If ARG is otherwise non-nil, use current file instead.
365If optional third arg SHOW-PROGRESS evaluates to non-nil,
366 redisplay the dired buffer after each file is processed.
367No guarantee is made about the position on the marked line.
368 BODY must ensure this itself if it depends on this.
369Search starts at the beginning of the buffer, thus the car of the list
370 corresponds to the line nearest to the buffer's bottom. This
371 is also true for (positive and negative) integer values of ARG.
372BODY should not be too long as it is expanded four times."
373 ;;
374 ;;Warning: BODY must not add new lines before point - this may cause an
375 ;;endless loop.
376 ;;This warning should not apply any longer, sk 2-Sep-1991 14:10.
377 `(prog1
378 (let (buffer-read-only case-fold-search found results)
379 (if ,arg
380 (if (integerp ,arg)
381 (progn ;; no save-excursion, want to move point.
382 (dired-repeat-over-lines
383 ,arg
384 (function (lambda ()
385 (if ,show-progress (sit-for 0))
386 (setq results (cons ,body results)))))
387 (if (< ,arg 0)
388 (nreverse results)
389 results))
390 ;; non-nil, non-integer ARG means use current file:
391 (list ,body))
392 (let ((regexp (dired-marker-regexp)) next-position)
393 (save-excursion
394 (goto-char (point-min))
395 ;; remember position of next marked file before BODY
396 ;; can insert lines before the just found file,
397 ;; confusing us by finding the same marked file again
398 ;; and again and...
399 (setq next-position (and (re-search-forward regexp nil t)
400 (point-marker))
401 found (not (null next-position)))
402 (while next-position
403 (goto-char next-position)
404 (if ,show-progress (sit-for 0))
405 (setq results (cons ,body results))
406 ;; move after last match
407 (goto-char next-position)
408 (forward-line 1)
409 (set-marker next-position nil)
492d2437 410 (setq next-position (and (re-search-forward regexp nil t)
91947235
SM
411 (point-marker)))))
412 (if found
413 results
414 (list ,body)))))
415 ;; save-excursion loses, again
416 (dired-move-to-filename)))
492d2437 417
1312bfc6 418(defun dired-get-marked-files (&optional localp arg filter)
492d2437
RS
419 "Return the marked files' names as list of strings.
420The list is in the same order as the buffer, that is, the car is the
421 first marked file.
1312bfc6 422Values returned are normally absolute file names.
492d2437 423Optional arg LOCALP as in `dired-get-filename'.
1312bfc6
RS
424Optional second argument ARG specifies files near point
425 instead of marked files. If ARG is an integer, use the next ARG files.
426 If ARG is otherwise non-nil, use file. Usually ARG comes from
427 the command's prefix arg.
428Optional third argument FILTER, if non-nil, is a function to select
429 some of the files--those for which (funcall FILTER FILENAME) is non-nil."
430 (let ((all-of-them
431 (save-excursion
432 (dired-map-over-marks (dired-get-filename localp) arg)))
433 result)
434 (if (not filter)
435 (nreverse all-of-them)
436 (dolist (file all-of-them)
437 (if (funcall filter file)
438 (push file result)))
439 result)))
83fadedf 440\f
492d2437
RS
441;; The dired command
442
443(defun dired-read-dir-and-switches (str)
444 ;; For use in interactive.
445 (reverse (list
446 (if current-prefix-arg
447 (read-string "Dired listing switches: "
448 dired-listing-switches))
449 (read-file-name (format "Dired %s(directory): " str)
450 nil default-directory nil))))
84fc2cfa 451
492d2437 452;;;###autoload (define-key ctl-x-map "d" 'dired)
84fc2cfa 453;;;###autoload
492d2437 454(defun dired (dirname &optional switches)
84fc2cfa 455 "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
492d2437
RS
456Optional second argument SWITCHES specifies the `ls' options used.
457\(Interactively, use a prefix argument to be able to specify SWITCHES.)
458Dired displays a list of files in DIRNAME (which may also have
8d23c16b 459shell wildcards appended to select certain files). If DIRNAME is a cons,
1015daba 460its first element is taken as the directory name and the rest as an explicit
8d23c16b 461list of files to make directory entries for.
52041219 462\\<dired-mode-map>\
492d2437 463You can move around in it with the usual commands.
8d23c16b
ER
464You can flag files for deletion with \\[dired-flag-file-deletion] and then
465delete them by typing \\[dired-do-flagged-delete].
492d2437 466Type \\[describe-mode] after entering dired for more info.
84fc2cfa 467
492d2437
RS
468If DIRNAME is already in a dired buffer, that buffer is used without refresh."
469 ;; Cannot use (interactive "D") because of wildcards.
470 (interactive (dired-read-dir-and-switches ""))
471 (switch-to-buffer (dired-noselect dirname switches)))
472
473;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
84fc2cfa 474;;;###autoload
492d2437 475(defun dired-other-window (dirname &optional switches)
84fc2cfa 476 "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
492d2437
RS
477 (interactive (dired-read-dir-and-switches "in other window "))
478 (switch-to-buffer-other-window (dired-noselect dirname switches)))
84fc2cfa 479
ec03e49c
RS
480;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
481;;;###autoload
482(defun dired-other-frame (dirname &optional switches)
483 "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame."
484 (interactive (dired-read-dir-and-switches "in other frame "))
485 (switch-to-buffer-other-frame (dired-noselect dirname switches)))
486
84fc2cfa 487;;;###autoload
8d23c16b 488(defun dired-noselect (dir-or-list &optional switches)
84fc2cfa 489 "Like `dired' but returns the dired buffer as value, does not select it."
8d23c16b 490 (or dir-or-list (setq dir-or-list default-directory))
492d2437
RS
491 ;; This loses the distinction between "/foo/*/" and "/foo/*" that
492 ;; some shells make:
54df7d9a 493 (let (dirname initially-was-dirname)
8d23c16b
ER
494 (if (consp dir-or-list)
495 (setq dirname (car dir-or-list))
496 (setq dirname dir-or-list))
54df7d9a
RS
497 (setq initially-was-dirname
498 (string= (file-name-as-directory dirname) dirname))
370b6149
RS
499 (setq dirname (abbreviate-file-name
500 (expand-file-name (directory-file-name dirname))))
cb2e51f8
RS
501 (if find-file-visit-truename
502 (setq dirname (file-truename dirname)))
54df7d9a
RS
503 ;; If the argument was syntactically a directory name not a file name,
504 ;; or if it happens to name a file that is a directory,
505 ;; convert it syntactically to a directory name.
506 ;; The reason for checking initially-was-dirname
507 ;; and not just file-directory-p
508 ;; is that file-directory-p is slow over ftp.
509 (if (or initially-was-dirname (file-directory-p dirname))
510 (setq dirname (file-name-as-directory dirname)))
8d23c16b
ER
511 (if (consp dir-or-list)
512 (setq dir-or-list (cons dirname (cdr dir-or-list)))
513 (setq dir-or-list dirname))
514 (dired-internal-noselect dir-or-list switches)))
492d2437 515
573e4d2d
LT
516;; The following is an internal dired function. It returns non-nil if
517;; the directory visited by the current dired buffer has changed on
518;; disk. DIRNAME should be the directory name of that directory.
519(defun dired-directory-changed-p (dirname)
520 (not (let ((attributes (file-attributes dirname))
521 (modtime (visited-file-modtime)))
522 (or (eq modtime 0)
523 (not (eq (car attributes) t))
524 (and (= (car (nth 5 attributes)) (car modtime))
525 (= (nth 1 (nth 5 attributes)) (cdr modtime)))))))
526
527(defun dired-buffer-stale-p (&optional noconfirm)
528 "Return non-nil if current dired buffer needs updating.
529If NOCONFIRM is non-nil, then this function always returns nil
530for a remote directory. This feature is used by Auto Revert Mode."
531 (let ((dirname
532 (if (consp dired-directory) (car dired-directory) dired-directory)))
533 (and (stringp dirname)
534 (not (when noconfirm (file-remote-p dirname)))
535 (file-readable-p dirname)
536 (dired-directory-changed-p dirname))))
537
492d2437 538;; Separate function from dired-noselect for the sake of dired-vms.el.
6a4cd605 539(defun dired-internal-noselect (dir-or-list &optional switches mode)
492d2437
RS
540 ;; If there is an existing dired buffer for DIRNAME, just leave
541 ;; buffer as it is (don't even call dired-revert).
542 ;; This saves time especially for deep trees or with ange-ftp.
573e4d2d 543 ;; The user can type `g' easily, and it is more consistent with find-file.
492d2437
RS
544 ;; But if SWITCHES are given they are probably different from the
545 ;; buffer's old value, so call dired-sort-other, which does
546 ;; revert the buffer.
547 ;; A pity we can't possibly do "Directory has changed - refresh? "
548 ;; like find-file does.
6a4cd605
RS
549 ;; Optional argument MODE is passed to dired-find-buffer-nocreate,
550 ;; see there.
0b7bc76f
RS
551 (let* (dirname
552 buffer
492d2437 553 ;; note that buffer already is in dired-mode, if found
0b7bc76f 554 new-buffer-p
492d2437 555 (old-buf (current-buffer)))
0b7bc76f
RS
556 (if (consp dir-or-list)
557 (setq dirname (car dir-or-list))
558 (setq dirname dir-or-list))
559 ;; Look for an existing buffer.
560 (setq buffer (dired-find-buffer-nocreate dirname mode)
561 new-buffer-p (null buffer))
492d2437
RS
562 (or buffer
563 (let ((default-major-mode 'fundamental-mode))
564 ;; We don't want default-major-mode to run hooks and set auto-fill
565 ;; or whatever, now that dired-mode does not
566 ;; kill-all-local-variables any longer.
567 (setq buffer (create-file-buffer (directory-file-name dirname)))))
568 (set-buffer buffer)
573e4d2d
LT
569 (if (not new-buffer-p) ; existing buffer ...
570 (cond (switches ; ... but new switches
6cfd24f9 571 ;; file list may have changed
0b7bc76f 572 (setq dired-directory dir-or-list)
6cfd24f9 573 ;; this calls dired-revert
c60ee5e7 574 (dired-sort-other switches))
6cfd24f9 575 ;; If directory has changed on disk, offer to revert.
573e4d2d 576 ((when (dired-directory-changed-p dirname)
952e8cb5 577 (message "%s"
212b2266
KH
578 (substitute-command-keys
579 "Directory has changed on disk; type \\[revert-buffer] to update Dired")))))
492d2437 580 ;; Else a new buffer
847aabce 581 (setq default-directory
e3cd4b53
RS
582 ;; We can do this unconditionally
583 ;; because dired-noselect ensures that the name
584 ;; is passed in directory name syntax
585 ;; if it was the name of a directory at all.
586 (file-name-directory dirname))
492d2437 587 (or switches (setq switches dired-listing-switches))
6e5d9ec5 588 (if mode (funcall mode)
0b7bc76f 589 (dired-mode dir-or-list switches))
492d2437
RS
590 ;; default-directory and dired-actual-switches are set now
591 ;; (buffer-local), so we can call dired-readin:
592 (let ((failed t))
593 (unwind-protect
0b7bc76f 594 (progn (dired-readin)
492d2437
RS
595 (setq failed nil))
596 ;; dired-readin can fail if parent directories are inaccessible.
597 ;; Don't leave an empty buffer around in that case.
598 (if failed (kill-buffer buffer))))
492d2437
RS
599 (goto-char (point-min))
600 (dired-initial-position dirname))
601 (set-buffer old-buf)
84fc2cfa
ER
602 buffer))
603
c5753a5d
GM
604(defvar dired-buffers nil
605 ;; Enlarged by dired-advertise
606 ;; Queried by function dired-buffers-for-dir. When this detects a
607 ;; killed buffer, it is removed from this list.
608 "Alist of expanded directories and their associated dired buffers.")
609
6a4cd605
RS
610(defun dired-find-buffer-nocreate (dirname &optional mode)
611 ;; This differs from dired-buffers-for-dir in that it does not consider
612 ;; subdirs of default-directory and searches for the first match only.
613 ;; Also, the major mode must be MODE.
0b7bc76f 614 (setq dirname (expand-file-name dirname))
f5b06a95 615 (let (found (blist dired-buffers)) ; was (buffer-list)
6a4cd605 616 (or mode (setq mode 'dired-mode))
492d2437 617 (while blist
6103c44e
KH
618 (if (null (buffer-name (cdr (car blist))))
619 (setq blist (cdr blist))
620 (save-excursion
621 (set-buffer (cdr (car blist)))
6a4cd605 622 (if (and (eq major-mode mode)
59759f1f 623 dired-directory ;; nil during find-alternate-file
0b7bc76f
RS
624 (equal dirname
625 (expand-file-name
626 (if (consp dired-directory)
627 (car dired-directory)
628 dired-directory))))
6103c44e
KH
629 (setq found (cdr (car blist))
630 blist nil)
631 (setq blist (cdr blist))))))
492d2437
RS
632 found))
633
83fadedf 634\f
492d2437
RS
635;; Read in a new dired buffer
636
637;; dired-readin differs from dired-insert-subdir in that it accepts
638;; wildcards, erases the buffer, and builds the subdir-alist anew
639;; (including making it buffer-local and clearing it first).
0b7bc76f 640(defun dired-readin ()
492d2437
RS
641 ;; default-directory and dired-actual-switches must be buffer-local
642 ;; and initialized by now.
0b7bc76f
RS
643 (let (dirname)
644 (if (consp dired-directory)
645 (setq dirname (car dired-directory))
646 (setq dirname dired-directory))
8d23c16b 647 (setq dirname (expand-file-name dirname))
8d23c16b 648 (save-excursion
0b7bc76f
RS
649 ;; This hook which may want to modify dired-actual-switches
650 ;; based on dired-directory, e.g. with ange-ftp to a SysV host
651 ;; where ls won't understand -Al switches.
652 (run-hooks 'dired-before-readin-hook)
0b7bc76f
RS
653 (if (consp buffer-undo-list)
654 (setq buffer-undo-list nil))
655 (let (buffer-read-only
656 ;; Don't make undo entries for readin.
657 (buffer-undo-list t))
8d23c16b
ER
658 (widen)
659 (erase-buffer)
0b7bc76f 660 (dired-readin-insert))
0b7bc76f 661 (goto-char (point-min))
8d23c16b
ER
662 ;; Must first make alist buffer local and set it to nil because
663 ;; dired-build-subdir-alist will call dired-clear-alist first
664 (set (make-local-variable 'dired-subdir-alist) nil)
8197b8bf 665 (dired-build-subdir-alist)
fa562dd5
RS
666 (let ((attributes (file-attributes dirname)))
667 (if (eq (car attributes) t)
668 (set-visited-file-modtime (nth 5 attributes))))
0b7bc76f
RS
669 (set-buffer-modified-p nil)
670 ;; No need to narrow since the whole buffer contains just
671 ;; dired-readin's output, nothing else. The hook can
672 ;; successfully use dired functions (e.g. dired-get-filename)
673 ;; as the subdir-alist has been built in dired-readin.
674 (run-hooks 'dired-after-readin-hook))))
492d2437
RS
675
676;; Subroutines of dired-readin
677
0b7bc76f
RS
678(defun dired-readin-insert ()
679 ;; Insert listing for the specified dir (and maybe file list)
680 ;; already in dired-directory, assuming a clean buffer.
681 (let (dir file-list)
682 (if (consp dired-directory)
683 (setq dir (car dired-directory)
684 file-list (cdr dired-directory))
685 (setq dir dired-directory
686 file-list nil))
34cd7b3c 687 (setq dir (expand-file-name dir))
0b7bc76f
RS
688 (if (and (equal "" (file-name-nondirectory dir))
689 (not file-list))
cf39fa9b 690 ;; If we are reading a whole single directory...
0b7bc76f 691 (dired-insert-directory dir dired-actual-switches nil nil t)
8d23c16b 692 (if (not (file-readable-p
0b7bc76f
RS
693 (directory-file-name (file-name-directory dir))))
694 (error "Directory %s inaccessible or nonexistent" dir)
695 ;; Else treat it as a wildcard spec
696 ;; unless we have an explicit list of files.
697 (dired-insert-directory dir dired-actual-switches
698 file-list (not file-list) t)))))
699
700(defun dired-insert-directory (dir switches &optional file-list wildcard hdr)
701 "Insert a directory listing of DIR, Dired style.
702Use SWITCHES to make the listings.
703If FILE-LIST is non-nil, list only those files.
704Otherwise, if WILDCARD is non-nil, expand wildcards;
705 in that case, DIR should be a file name that uses wildcards.
706In other cases, DIR should be a directory name or a directory filename.
707If HDR is non-nil, insert a header line with the directory name."
9888aa65 708 (let ((opoint (point))
02b5d79c 709 (process-environment (copy-sequence process-environment))
9888aa65 710 end)
1fc85dae 711 (if (or dired-use-ls-dired (file-remote-p dir))
0b7bc76f 712 (setq switches (concat "--dired " switches)))
6492483f
RS
713 ;; We used to specify the C locale here, to force English month names;
714 ;; but this should not be necessary any more,
715 ;; with the new value of dired-move-to-filename-regexp.
0b7bc76f
RS
716 (if file-list
717 (dolist (f file-list)
718 (insert-directory f switches nil nil))
719 (insert-directory dir switches wildcard (not wildcard)))
9888aa65 720 ;; Quote certain characters, unless ls quoted them for us.
ad632492
SM
721 (if (not (string-match "b" dired-actual-switches))
722 (save-excursion
723 (setq end (point-marker))
724 (goto-char opoint)
725 (while (search-forward "\\" end t)
2bb27597
AS
726 (replace-match (apply #'propertize
727 "\\\\"
728 (text-properties-at (match-beginning 0)))
729 nil t))
ad632492
SM
730 (goto-char opoint)
731 (while (search-forward "\^m" end t)
2bb27597
AS
732 (replace-match (apply #'propertize
733 "\\015"
734 (text-properties-at (match-beginning 0)))
735 nil t))
ad632492 736 (set-marker end nil)))
0b7bc76f
RS
737 (dired-insert-set-properties opoint (point))
738 ;; If we used --dired and it worked, the lines are already indented.
739 ;; Otherwise, indent them.
740 (unless (save-excursion
1ba6c0f2 741 (goto-char opoint)
0b7bc76f
RS
742 (looking-at " "))
743 (let ((indent-tabs-mode nil))
744 (indent-rigidly opoint (point) 2)))
745 ;; Insert text at the beginning to standardize things.
746 (save-excursion
747 (goto-char opoint)
748 (if (and (or hdr wildcard) (not (looking-at "^ /.*:$")))
749 ;; Note that dired-build-subdir-alist will replace the name
750 ;; by its expansion, so it does not matter whether what we insert
751 ;; here is fully expanded, but it should be absolute.
752 (insert " " (directory-file-name (file-name-directory dir)) ":\n"))
753 (when wildcard
754 ;; Insert "wildcard" line where "total" line would be for a full dir.
755 (insert " wildcard " (file-name-nondirectory dir) "\n")))))
492d2437 756
9888aa65 757;; Make the file names highlight when the mouse is on them.
cb88a3db
RS
758(defun dired-insert-set-properties (beg end)
759 (save-excursion
760 (goto-char beg)
761 (while (< (point) end)
7da0e9b4
RS
762 (condition-case nil
763 (if (dired-move-to-filename)
ac2d0299
EZ
764 (add-text-properties
765 (point)
766 (save-excursion
767 (dired-move-to-end-of-filename)
768 (point))
1b85bd12 769 '(mouse-face highlight
ac2d0299 770 help-echo "mouse-2: visit this file in other window")))
7da0e9b4 771 (error nil))
cb88a3db 772 (forward-line 1))))
83fadedf 773\f
492d2437
RS
774;; Reverting a dired buffer
775
84fc2cfa 776(defun dired-revert (&optional arg noconfirm)
492d2437
RS
777 ;; Reread the dired buffer. Must also be called after
778 ;; dired-actual-switches have changed.
779 ;; Should not fail even on completely garbaged buffers.
780 ;; Preserves old cursor, marks/flags, hidden-p.
781 (widen) ; just in case user narrowed
84fc2cfa 782 (let ((opoint (point))
492d2437
RS
783 (ofile (dired-get-filename nil t))
784 (mark-alist nil) ; save marked files
785 (hidden-subdirs (dired-remember-hidden))
786 (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
787 (case-fold-search nil) ; we check for upper case ls flags
788 buffer-read-only)
789 (goto-char (point-min))
790 (setq mark-alist;; only after dired-remember-hidden since this unhides:
791 (dired-remember-marks (point-min) (point-max)))
792 ;; treat top level dir extra (it may contain wildcards)
8d23c16b
ER
793 (dired-uncache
794 (if (consp dired-directory) (car dired-directory) dired-directory))
0b7bc76f 795 (dired-readin)
492d2437
RS
796 (let ((dired-after-readin-hook nil))
797 ;; don't run that hook for each subdir...
798 (dired-insert-old-subdirs old-subdir-alist))
799 (dired-mark-remembered mark-alist) ; mark files that were marked
800 ;; ... run the hook for the whole buffer, and only after markers
801 ;; have been reinserted (else omitting in dired-x would omit marked files)
802 (run-hooks 'dired-after-readin-hook) ; no need to narrow
803 (or (and ofile (dired-goto-file ofile)) ; move cursor to where it
804 (goto-char opoint)) ; was before
84fc2cfa 805 (dired-move-to-filename)
492d2437
RS
806 (save-excursion ; hide subdirs that were hidden
807 (mapcar (function (lambda (dir)
808 (if (dired-goto-subdir dir)
809 (dired-hide-subdir 1))))
810 hidden-subdirs)))
811 ;; outside of the let scope
812;;; Might as well not override the user if the user changed this.
813;;; (setq buffer-read-only t)
814 )
815
816;; Subroutines of dired-revert
817;; Some of these are also used when inserting subdirs.
818
819(defun dired-remember-marks (beg end)
820 ;; Return alist of files and their marks, from BEG to END.
821 (if selective-display ; must unhide to make this work.
822 (let (buffer-read-only)
823 (subst-char-in-region beg end ?\r ?\n)))
824 (let (fil chr alist)
825 (save-excursion
826 (goto-char beg)
827 (while (re-search-forward dired-re-mark end t)
828 (if (setq fil (dired-get-filename nil t))
829 (setq chr (preceding-char)
830 alist (cons (cons fil chr) alist)))))
831 alist))
832
833;; Mark all files remembered in ALIST.
834;; Each element of ALIST looks like (FILE . MARKERCHAR).
835(defun dired-mark-remembered (alist)
836 (let (elt fil chr)
837 (while alist
838 (setq elt (car alist)
839 alist (cdr alist)
840 fil (car elt)
841 chr (cdr elt))
842 (if (dired-goto-file fil)
843 (save-excursion
844 (beginning-of-line)
845 (delete-char 1)
846 (insert chr))))))
847
848;; Return a list of names of subdirs currently hidden.
849(defun dired-remember-hidden ()
850 (let ((l dired-subdir-alist) dir pos result)
851 (while l
852 (setq dir (car (car l))
853 pos (cdr (car l))
854 l (cdr l))
855 (goto-char pos)
856 (skip-chars-forward "^\r\n")
52041219 857 (if (eq (following-char) ?\r)
492d2437
RS
858 (setq result (cons dir result))))
859 result))
860
861;; Try to insert all subdirs that were displayed before,
862;; according to the former subdir alist OLD-SUBDIR-ALIST.
863(defun dired-insert-old-subdirs (old-subdir-alist)
864 (or (string-match "R" dired-actual-switches)
865 (let (elt dir)
866 (while old-subdir-alist
867 (setq elt (car old-subdir-alist)
868 old-subdir-alist (cdr old-subdir-alist)
869 dir (car elt))
870 (condition-case ()
715984d3
RS
871 (progn
872 (dired-uncache dir)
873 (dired-insert-subdir dir))
492d2437 874 (error nil))))))
715984d3
RS
875
876;; Remove directory DIR from any directory cache.
877(defun dired-uncache (dir)
6eaebaa2 878 (let ((handler (find-file-name-handler dir 'dired-uncache)))
715984d3
RS
879 (if handler
880 (funcall handler 'dired-uncache dir))))
83fadedf 881\f
492d2437 882;; dired mode key bindings and initialization
84fc2cfa
ER
883
884(defvar dired-mode-map nil "Local keymap for dired-mode buffers.")
885(if dired-mode-map
886 nil
492d2437
RS
887 ;; This looks ugly when substitute-command-keys uses C-d instead d:
888 ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
889
3e1fb00f
RS
890 (let ((map (make-keymap)))
891 (suppress-keymap map)
892 (define-key map [mouse-2] 'dired-mouse-find-file-other-window)
893 ;; Commands to mark or flag certain categories of files
894 (define-key map "#" 'dired-flag-auto-save-files)
3e1fb00f 895 (define-key map "." 'dired-clean-directory)
3e1fb00f 896 (define-key map "~" 'dired-flag-backup-files)
84d3f6e8 897 (define-key map "&" 'dired-flag-garbage-files)
3e1fb00f
RS
898 ;; Upper case keys (except !) for operating on the marked files
899 (define-key map "A" 'dired-do-search)
900 (define-key map "C" 'dired-do-copy)
901 (define-key map "B" 'dired-do-byte-compile)
902 (define-key map "D" 'dired-do-delete)
903 (define-key map "G" 'dired-do-chgrp)
904 (define-key map "H" 'dired-do-hardlink)
905 (define-key map "L" 'dired-do-load)
906 (define-key map "M" 'dired-do-chmod)
907 (define-key map "O" 'dired-do-chown)
908 (define-key map "P" 'dired-do-print)
ee93b692 909 (define-key map "Q" 'dired-do-query-replace-regexp)
3e1fb00f
RS
910 (define-key map "R" 'dired-do-rename)
911 (define-key map "S" 'dired-do-symlink)
912 (define-key map "X" 'dired-do-shell-command)
913 (define-key map "Z" 'dired-do-compress)
914 (define-key map "!" 'dired-do-shell-command)
915 ;; Comparison commands
916 (define-key map "=" 'dired-diff)
917 (define-key map "\M-=" 'dired-backup-diff)
918 ;; Tree Dired commands
919 (define-key map "\M-\C-?" 'dired-unmark-all-files)
920 (define-key map "\M-\C-d" 'dired-tree-down)
921 (define-key map "\M-\C-u" 'dired-tree-up)
922 (define-key map "\M-\C-n" 'dired-next-subdir)
923 (define-key map "\M-\C-p" 'dired-prev-subdir)
924 ;; move to marked files
925 (define-key map "\M-{" 'dired-prev-marked-file)
926 (define-key map "\M-}" 'dired-next-marked-file)
927 ;; Make all regexp commands share a `%' prefix:
928 ;; We used to get to the submap via a symbol dired-regexp-prefix,
929 ;; but that seems to serve little purpose, and copy-keymap
930 ;; does a better job without it.
931 (define-key map "%" nil)
932 (define-key map "%u" 'dired-upcase)
933 (define-key map "%l" 'dired-downcase)
934 (define-key map "%d" 'dired-flag-files-regexp)
e9b8e22d 935 (define-key map "%g" 'dired-mark-files-containing-regexp)
3e1fb00f
RS
936 (define-key map "%m" 'dired-mark-files-regexp)
937 (define-key map "%r" 'dired-do-rename-regexp)
938 (define-key map "%C" 'dired-do-copy-regexp)
939 (define-key map "%H" 'dired-do-hardlink-regexp)
940 (define-key map "%R" 'dired-do-rename-regexp)
941 (define-key map "%S" 'dired-do-symlink-regexp)
4a3ca6ed
RS
942 ;; Commands for marking and unmarking.
943 (define-key map "*" nil)
944 (define-key map "**" 'dired-mark-executables)
945 (define-key map "*/" 'dired-mark-directories)
946 (define-key map "*@" 'dired-mark-symlinks)
947 (define-key map "*%" 'dired-mark-files-regexp)
948 (define-key map "*c" 'dired-change-marks)
0609c998 949 (define-key map "*s" 'dired-mark-subdir-files)
4a3ca6ed
RS
950 (define-key map "*m" 'dired-mark)
951 (define-key map "*u" 'dired-unmark)
952 (define-key map "*?" 'dired-unmark-all-files)
534a6edf 953 (define-key map "*!" 'dired-unmark-all-marks)
4a3ca6ed
RS
954 (define-key map "*\177" 'dired-unmark-backward)
955 (define-key map "*\C-n" 'dired-next-marked-file)
956 (define-key map "*\C-p" 'dired-prev-marked-file)
d588eb90 957 (define-key map "*t" 'dired-toggle-marks)
3e1fb00f 958 ;; Lower keys for commands not operating on all the marked files
c5753a5d 959 (define-key map "a" 'dired-find-alternate-file)
3e1fb00f
RS
960 (define-key map "d" 'dired-flag-file-deletion)
961 (define-key map "e" 'dired-find-file)
962 (define-key map "f" 'dired-find-file)
963 (define-key map "\C-m" 'dired-advertised-find-file)
964 (define-key map "g" 'revert-buffer)
e906b0a2 965 (define-key map "\M-g" 'dired-goto-file)
3e1fb00f
RS
966 (define-key map "h" 'describe-mode)
967 (define-key map "i" 'dired-maybe-insert-subdir)
968 (define-key map "k" 'dired-do-kill-lines)
969 (define-key map "l" 'dired-do-redisplay)
970 (define-key map "m" 'dired-mark)
971 (define-key map "n" 'dired-next-line)
972 (define-key map "o" 'dired-find-file-other-window)
973 (define-key map "\C-o" 'dired-display-file)
974 (define-key map "p" 'dired-previous-line)
da8a8438 975 (define-key map "q" 'quit-window)
3e1fb00f 976 (define-key map "s" 'dired-sort-toggle-or-edit)
d588eb90 977 (define-key map "t" 'dired-toggle-marks)
3e1fb00f
RS
978 (define-key map "u" 'dired-unmark)
979 (define-key map "v" 'dired-view-file)
a3f4a3ef 980 (define-key map "w" 'dired-copy-filename-as-kill)
3e1fb00f 981 (define-key map "x" 'dired-do-flagged-delete)
d1e99fa5 982 (define-key map "y" 'dired-show-file-type)
3e1fb00f
RS
983 (define-key map "+" 'dired-create-directory)
984 ;; moving
985 (define-key map "<" 'dired-prev-dirline)
986 (define-key map ">" 'dired-next-dirline)
987 (define-key map "^" 'dired-up-directory)
988 (define-key map " " 'dired-next-line)
989 (define-key map "\C-n" 'dired-next-line)
990 (define-key map "\C-p" 'dired-previous-line)
991 (define-key map [down] 'dired-next-line)
992 (define-key map [up] 'dired-previous-line)
993 ;; hiding
994 (define-key map "$" 'dired-hide-subdir)
995 (define-key map "\M-$" 'dired-hide-all)
996 ;; misc
997 (define-key map "?" 'dired-summary)
998 (define-key map "\177" 'dired-unmark-backward)
999 (define-key map "\C-_" 'dired-undo)
1000 (define-key map "\C-xu" 'dired-undo)
1001
1002 ;; Make menu bar items.
1003
d066de8e
EZ
1004 ;; No need to fo this, now that top-level items are fewer.
1005 ;;;;
3e1fb00f 1006 ;; Get rid of the Edit menu bar item to save space.
d066de8e 1007 ;(define-key map [menu-bar edit] 'undefined)
3e1fb00f
RS
1008
1009 (define-key map [menu-bar subdir]
1010 (cons "Subdir" (make-sparse-keymap "Subdir")))
1011
1012 (define-key map [menu-bar subdir hide-all]
d066de8e
EZ
1013 '(menu-item "Hide All" dired-hide-all
1014 :help "Hide all subdirectories, leave only header lines"))
3e1fb00f 1015 (define-key map [menu-bar subdir hide-subdir]
d066de8e
EZ
1016 '(menu-item "Hide/UnHide Subdir" dired-hide-subdir
1017 :help "Hide or unhide current directory listing"))
3e1fb00f 1018 (define-key map [menu-bar subdir tree-down]
d066de8e
EZ
1019 '(menu-item "Tree Down" dired-tree-down
1020 :help "Go to first subdirectory header down the tree"))
3e1fb00f 1021 (define-key map [menu-bar subdir tree-up]
d066de8e
EZ
1022 '(menu-item "Tree Up" dired-tree-up
1023 :help "Go to first subdirectory header up the tree"))
3e1fb00f 1024 (define-key map [menu-bar subdir up]
d066de8e
EZ
1025 '(menu-item "Up Directory" dired-up-directory
1026 :help "Edit the parent directory"))
3e1fb00f 1027 (define-key map [menu-bar subdir prev-subdir]
d066de8e
EZ
1028 '(menu-item "Prev Subdir" dired-prev-subdir
1029 :help "Go to previous subdirectory header line"))
3e1fb00f 1030 (define-key map [menu-bar subdir next-subdir]
d066de8e
EZ
1031 '(menu-item "Next Subdir" dired-next-subdir
1032 :help "Go to next subdirectory header line"))
3e1fb00f 1033 (define-key map [menu-bar subdir prev-dirline]
d066de8e
EZ
1034 '(menu-item "Prev Dirline" dired-prev-dirline
1035 :help "Move to next directory-file line"))
3e1fb00f 1036 (define-key map [menu-bar subdir next-dirline]
d066de8e
EZ
1037 '(menu-item "Next Dirline" dired-next-dirline
1038 :help "Move to previous directory-file line"))
3e1fb00f 1039 (define-key map [menu-bar subdir insert]
d066de8e
EZ
1040 '(menu-item "Insert This Subdir" dired-maybe-insert-subdir
1041 :help "Insert contents of subdirectory"))
3e1fb00f
RS
1042
1043 (define-key map [menu-bar immediate]
1044 (cons "Immediate" (make-sparse-keymap "Immediate")))
1045
a3285795 1046 (define-key map [menu-bar immediate revert-buffer]
d066de8e
EZ
1047 '(menu-item "Refresh" revert-buffer
1048 :help "Update contents of shown directories"))
a3285795
RS
1049
1050 (define-key map [menu-bar immediate dashes]
1051 '("--"))
1052
3e1fb00f 1053 (define-key map [menu-bar immediate backup-diff]
d066de8e
EZ
1054 '(menu-item "Compare with Backup" dired-backup-diff
1055 :help "Diff file at cursor with its latest backup"))
3e1fb00f 1056 (define-key map [menu-bar immediate diff]
d066de8e
EZ
1057 '(menu-item "Diff..." dired-diff
1058 :help "Compare file at cursor with another file"))
3e1fb00f 1059 (define-key map [menu-bar immediate view]
d066de8e
EZ
1060 '(menu-item "View This File" dired-view-file
1061 :help "Examine file at cursor in read-only mode"))
3e1fb00f 1062 (define-key map [menu-bar immediate display]
d066de8e
EZ
1063 '(menu-item "Display in Other Window" dired-display-file
1064 :help "Display file at cursor in other window"))
3e1fb00f 1065 (define-key map [menu-bar immediate find-file-other-window]
d066de8e
EZ
1066 '(menu-item "Find in Other Window" dired-find-file-other-window
1067 :help "Edit file at cursor in other window"))
3e1fb00f 1068 (define-key map [menu-bar immediate find-file]
d066de8e
EZ
1069 '(menu-item "Find This File" dired-find-file
1070 :help "Edit file at cursor"))
3e1fb00f 1071 (define-key map [menu-bar immediate create-directory]
d066de8e 1072 '(menu-item "Create Directory..." dired-create-directory))
3e1fb00f
RS
1073
1074 (define-key map [menu-bar regexp]
1075 (cons "Regexp" (make-sparse-keymap "Regexp")))
1076
1077 (define-key map [menu-bar regexp downcase]
d066de8e
EZ
1078 '(menu-item "Downcase" dired-downcase
1079 ;; When running on plain MS-DOS, there's only one
1080 ;; letter-case for file names.
1081 :enable (or (not (fboundp 'msdos-long-file-names))
1082 (msdos-long-file-names))
1083 :help "Rename marked files to lower-case name"))
3e1fb00f 1084 (define-key map [menu-bar regexp upcase]
d066de8e
EZ
1085 '(menu-item "Upcase" dired-upcase
1086 :enable (or (not (fboundp 'msdos-long-file-names))
1087 (msdos-long-file-names))
1088 :help "Rename marked files to upper-case name"))
3e1fb00f 1089 (define-key map [menu-bar regexp hardlink]
d066de8e
EZ
1090 '(menu-item "Hardlink..." dired-do-hardlink-regexp
1091 :help "Make hard links for files matching regexp"))
3e1fb00f 1092 (define-key map [menu-bar regexp symlink]
d066de8e
EZ
1093 '(menu-item "Symlink..." dired-do-symlink-regexp
1094 :visible (fboundp 'make-symbolic-link)
1095 :help "Make symbolic links for files matching regexp"))
3e1fb00f 1096 (define-key map [menu-bar regexp rename]
d066de8e
EZ
1097 '(menu-item "Rename..." dired-do-rename-regexp
1098 :help "Rename marked files matching regexp"))
3e1fb00f 1099 (define-key map [menu-bar regexp copy]
d066de8e
EZ
1100 '(menu-item "Copy..." dired-do-copy-regexp
1101 :help "Copy marked files matching regexp"))
3e1fb00f 1102 (define-key map [menu-bar regexp flag]
d066de8e
EZ
1103 '(menu-item "Flag..." dired-flag-files-regexp
1104 :help "Flag files matching regexp for deletion"))
3e1fb00f 1105 (define-key map [menu-bar regexp mark]
d066de8e
EZ
1106 '(menu-item "Mark..." dired-mark-files-regexp
1107 :help "Mark files matching regexp for future operations"))
aa924deb 1108 (define-key map [menu-bar regexp mark-cont]
d066de8e
EZ
1109 '(menu-item "Mark Containing..." dired-mark-files-containing-regexp
1110 :help "Mark files whose contents matches regexp"))
3e1fb00f
RS
1111
1112 (define-key map [menu-bar mark]
1113 (cons "Mark" (make-sparse-keymap "Mark")))
1114
1115 (define-key map [menu-bar mark prev]
d066de8e
EZ
1116 '(menu-item "Previous Marked" dired-prev-marked-file
1117 :help "Move to previous marked file"))
3e1fb00f 1118 (define-key map [menu-bar mark next]
d066de8e
EZ
1119 '(menu-item "Next Marked" dired-next-marked-file
1120 :help "Move to next marked file"))
3e1fb00f 1121 (define-key map [menu-bar mark marks]
d066de8e
EZ
1122 '(menu-item "Change Marks..." dired-change-marks
1123 :help "Replace marker with another character"))
3e1fb00f 1124 (define-key map [menu-bar mark unmark-all]
d066de8e 1125 '(menu-item "Unmark All" dired-unmark-all-marks))
3e1fb00f 1126 (define-key map [menu-bar mark symlinks]
d066de8e
EZ
1127 '(menu-item "Mark Symlinks" dired-mark-symlinks
1128 :visible (fboundp 'make-symbolic-link)
1129 :help "Mark all symbolic links"))
3e1fb00f 1130 (define-key map [menu-bar mark directories]
d066de8e
EZ
1131 '(menu-item "Mark Directories" dired-mark-directories
1132 :help "Mark all directories except `.' and `..'"))
3e1fb00f 1133 (define-key map [menu-bar mark directory]
d066de8e
EZ
1134 '(menu-item "Mark Old Backups" dired-clean-directory
1135 :help "Flag old numbered backups for deletion"))
3e1fb00f 1136 (define-key map [menu-bar mark executables]
d066de8e
EZ
1137 '(menu-item "Mark Executables" dired-mark-executables
1138 :help "Mark all executable files"))
84d3f6e8 1139 (define-key map [menu-bar mark garbage-files]
d066de8e
EZ
1140 '(menu-item "Flag Garbage Files" dired-flag-garbage-files
1141 :help "Flag unneeded files for deletion"))
3e1fb00f 1142 (define-key map [menu-bar mark backup-files]
d066de8e
EZ
1143 '(menu-item "Flag Backup Files" dired-flag-backup-files
1144 :help "Flag all backup files for deletion"))
3e1fb00f 1145 (define-key map [menu-bar mark auto-save-files]
d066de8e
EZ
1146 '(menu-item "Flag Auto-save Files" dired-flag-auto-save-files
1147 :help "Flag auto-save files for deletion"))
3e1fb00f 1148 (define-key map [menu-bar mark deletion]
d066de8e
EZ
1149 '(menu-item "Flag" dired-flag-file-deletion
1150 :help "Flag current line's file for deletion"))
3e1fb00f 1151 (define-key map [menu-bar mark unmark]
d066de8e
EZ
1152 '(menu-item "Unmark" dired-unmark
1153 :help "Unmark or unflag current line's file"))
3e1fb00f 1154 (define-key map [menu-bar mark mark]
d066de8e
EZ
1155 '(menu-item "Mark" dired-mark
1156 :help "Mark current line's file for future operations"))
e5f0841e 1157 (define-key map [menu-bar mark toggle-marks]
d588eb90 1158 '(menu-item "Toggle Marks" dired-toggle-marks
d066de8e 1159 :help "Mark unmarked files, unmark marked ones"))
3e1fb00f
RS
1160
1161 (define-key map [menu-bar operate]
1162 (cons "Operate" (make-sparse-keymap "Operate")))
1163
1164 (define-key map [menu-bar operate query-replace]
ee93b692 1165 '(menu-item "Query Replace in Files..." dired-do-query-replace-regexp
d066de8e 1166 :help "Replace regexp in marked files"))
3e1fb00f 1167 (define-key map [menu-bar operate search]
d066de8e
EZ
1168 '(menu-item "Search Files..." dired-do-search
1169 :help "Search marked files for regexp"))
3e1fb00f 1170 (define-key map [menu-bar operate chown]
d066de8e
EZ
1171 '(menu-item "Change Owner..." dired-do-chown
1172 :visible (not (memq system-type '(ms-dos windows-nt)))
1173 :help "Change the owner of marked files"))
3e1fb00f 1174 (define-key map [menu-bar operate chgrp]
d066de8e
EZ
1175 '(menu-item "Change Group..." dired-do-chgrp
1176 :visible (not (memq system-type '(ms-dos windows-nt)))
1177 :help "Change the group of marked files"))
3e1fb00f 1178 (define-key map [menu-bar operate chmod]
d066de8e
EZ
1179 '(menu-item "Change Mode..." dired-do-chmod
1180 :help "Change mode (attributes) of marked files"))
3e1fb00f 1181 (define-key map [menu-bar operate load]
d066de8e
EZ
1182 '(menu-item "Load" dired-do-load
1183 :help "Load marked Emacs Lisp files"))
3e1fb00f 1184 (define-key map [menu-bar operate compile]
d066de8e
EZ
1185 '(menu-item "Byte-compile" dired-do-byte-compile
1186 :help "Byte-compile marked Emacs Lisp files"))
3e1fb00f 1187 (define-key map [menu-bar operate compress]
d066de8e
EZ
1188 '(menu-item "Compress" dired-do-compress
1189 :help "Compress/uncompress marked files"))
3e1fb00f 1190 (define-key map [menu-bar operate print]
d066de8e
EZ
1191 '(menu-item "Print..." dired-do-print
1192 :help "Ask for print command and print marked files"))
3e1fb00f 1193 (define-key map [menu-bar operate hardlink]
d066de8e
EZ
1194 '(menu-item "Hardlink to..." dired-do-hardlink
1195 :help "Make hard links for current or marked files"))
3e1fb00f 1196 (define-key map [menu-bar operate symlink]
d066de8e
EZ
1197 '(menu-item "Symlink to..." dired-do-symlink
1198 :visible (fboundp 'make-symbolic-link)
1199 :help "Make symbolic links for current or marked files"))
3e1fb00f 1200 (define-key map [menu-bar operate command]
d066de8e
EZ
1201 '(menu-item "Shell Command..." dired-do-shell-command
1202 :help "Run a shell command on each of marked files"))
3e1fb00f 1203 (define-key map [menu-bar operate delete]
d066de8e
EZ
1204 '(menu-item "Delete" dired-do-delete
1205 :help "Delete current file or all marked files"))
3e1fb00f 1206 (define-key map [menu-bar operate rename]
d066de8e
EZ
1207 '(menu-item "Rename to..." dired-do-rename
1208 :help "Rename current file or move marked files"))
3e1fb00f 1209 (define-key map [menu-bar operate copy]
d066de8e
EZ
1210 '(menu-item "Copy to..." dired-do-copy
1211 :help "Copy current file or all marked files"))
3e1fb00f
RS
1212
1213 (setq dired-mode-map map)))
83fadedf 1214\f
84fc2cfa
ER
1215;; Dired mode is suitable only for specially formatted data.
1216(put 'dired-mode 'mode-class 'special)
1217
573e4d2d
LT
1218(defvar buffer-stale-function)
1219
492d2437
RS
1220(defun dired-mode (&optional dirname switches)
1221 "\
1222Mode for \"editing\" directory listings.
68d2f12f 1223In Dired, you are \"editing\" a list of the files in a directory and
492d2437
RS
1224 \(optionally) its subdirectories, in the format of `ls -lR'.
1225 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
1226\"Editing\" means that you can run shell commands on files, visit,
1227 compress, load or byte-compile them, change their file attributes
1228 and insert subdirectories into the same buffer. You can \"mark\"
1229 files for later commands or \"flag\" them for deletion, either file
1230 by file or all files matching certain criteria.
1231You can move using the usual cursor motion commands.\\<dired-mode-map>
1232Letters no longer insert themselves. Digits are prefix arguments.
1233Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
1234Type \\[dired-mark] to Mark a file or subdirectory for later commands.
1235 Most commands operate on the marked files and use the current file
1236 if no files are marked. Use a numeric prefix argument to operate on
1237 the next ARG (or previous -ARG if ARG<0) files, or just `1'
1238 to operate on the current file only. Prefix arguments override marks.
1239 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
1240 to see why something went wrong.
1241Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
1242Type \\[dired-unmark-backward] to back up one line and unflag.
1243Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
1244Type \\[dired-advertised-find-file] to Find the current line's file
1245 (or dired it in another buffer, if it is a directory).
1246Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
1247Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
1248Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
1249Type \\[dired-do-copy] to Copy files.
1250Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
1251Type \\[revert-buffer] to read all currently expanded directories again.
1252 This retains all marks and hides subdirs again that were hidden before.
1253SPC and DEL can be used to move down and up by lines.
1254
1255If dired ever gets confused, you can either type \\[revert-buffer] \
1256to read the
1257directories again, type \\[dired-do-redisplay] \
1258to relist a single or the marked files or a
1259subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
1260again for the directory tree.
1261
1262Customization variables (rename this buffer and type \\[describe-variable] on each line
1263for more info):
1264
1265 dired-listing-switches
1266 dired-trivial-filenames
1267 dired-shrink-to-fit
1268 dired-marker-char
1269 dired-del-marker
1270 dired-keep-marker-rename
1271 dired-keep-marker-copy
1272 dired-keep-marker-hardlink
1273 dired-keep-marker-symlink
1274
1275Hooks (use \\[describe-variable] to see their documentation):
1276
1277 dired-before-readin-hook
1278 dired-after-readin-hook
1279 dired-mode-hook
1280 dired-load-hook
1281
1282Keybindings:
84fc2cfa 1283\\{dired-mode-map}"
492d2437
RS
1284 ;; Not to be called interactively (e.g. dired-directory will be set
1285 ;; to default-directory, which is wrong with wildcards).
84fc2cfa 1286 (kill-all-local-variables)
84fc2cfa 1287 (use-local-map dired-mode-map)
492d2437
RS
1288 (dired-advertise) ; default-directory is already set
1289 (setq major-mode 'dired-mode
1290 mode-name "Dired"
3222085e 1291;; case-fold-search nil
492d2437
RS
1292 buffer-read-only t
1293 selective-display t ; for subdirectory hiding
0acdf642
GM
1294 mode-line-buffer-identification
1295 (propertized-buffer-identification "%17b"))
492d2437
RS
1296 (set (make-local-variable 'revert-buffer-function)
1297 (function dired-revert))
573e4d2d
LT
1298 (set (make-local-variable 'buffer-stale-function)
1299 (function dired-buffer-stale-p))
492d2437
RS
1300 (set (make-local-variable 'page-delimiter)
1301 "\n\n")
1302 (set (make-local-variable 'dired-directory)
1303 (or dirname default-directory))
1304 ;; list-buffers uses this to display the dir being edited in this buffer.
1305 (set (make-local-variable 'list-buffers-directory)
86e6e4b3
RS
1306 (expand-file-name (if (listp dired-directory)
1307 (car dired-directory)
1308 dired-directory)))
492d2437
RS
1309 (set (make-local-variable 'dired-actual-switches)
1310 (or switches dired-listing-switches))
4481ae96 1311 (set (make-local-variable 'font-lock-defaults) '(dired-font-lock-keywords t))
492d2437 1312 (dired-sort-other dired-actual-switches t)
133aad74
JD
1313 (run-hooks 'dired-mode-hook)
1314 (when (featurep 'x-dnd)
1315 (make-variable-buffer-local 'x-dnd-test-function)
1316 (make-variable-buffer-local 'x-dnd-protocol-alist)
1317 (setq x-dnd-test-function 'dired-dnd-test-function)
1318 (setq x-dnd-protocol-alist
1319 (append '(("^file:///" . dired-dnd-handle-local-file)
1320 ("^file://" . dired-dnd-handle-file)
1321 ("^file:" . dired-dnd-handle-local-file))
1322 x-dnd-protocol-alist))))
83fadedf 1323\f
eb8c3be9 1324;; Idiosyncratic dired commands that don't deal with marks.
84fc2cfa 1325
84fc2cfa 1326(defun dired-summary ()
492d2437 1327 "Summarize basic Dired commands and show recent Dired errors."
84fc2cfa 1328 (interactive)
492d2437 1329 (dired-why)
84fc2cfa
ER
1330 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
1331 (message
50f74744 1332 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
84fc2cfa 1333
492d2437
RS
1334(defun dired-undo ()
1335 "Undo in a dired buffer.
1336This doesn't recover lost files, it just undoes changes in the buffer itself.
1337You can use it to recover marks, killed lines or subdirs.
1338In the latter case, you have to do \\[dired-build-subdir-alist] to
1339parse the buffer again."
1340 (interactive)
1341 (let (buffer-read-only)
16a2f75a
RS
1342 (undo)
1343 (message "Change in Dired buffer undone.
1344Actual changes in files cannot be undone by Emacs.")))
84fc2cfa
ER
1345
1346(defun dired-next-line (arg)
1347 "Move down lines then position at filename.
1348Optional prefix ARG says how many lines to move; default is one line."
1349 (interactive "p")
1350 (next-line arg)
1351 (dired-move-to-filename))
1352
1353(defun dired-previous-line (arg)
1354 "Move up lines then position at filename.
1355Optional prefix ARG says how many lines to move; default is one line."
1356 (interactive "p")
1357 (previous-line arg)
1358 (dired-move-to-filename))
1359
492d2437
RS
1360(defun dired-next-dirline (arg &optional opoint)
1361 "Goto ARG'th next directory file line."
1362 (interactive "p")
1363 (or opoint (setq opoint (point)))
1364 (if (if (> arg 0)
1365 (re-search-forward dired-re-dir nil t arg)
1366 (beginning-of-line)
1367 (re-search-backward dired-re-dir nil t (- arg)))
1368 (dired-move-to-filename) ; user may type `i' or `f'
1369 (goto-char opoint)
1370 (error "No more subdirectories")))
1371
1372(defun dired-prev-dirline (arg)
1373 "Goto ARG'th previous directory file line."
1374 (interactive "p")
1375 (dired-next-dirline (- arg)))
1376
ac1ce341 1377(defun dired-up-directory (&optional other-window)
68d2f12f 1378 "Run Dired on parent directory of current directory.
492d2437
RS
1379Find the parent directory either in this buffer or another buffer.
1380Creates a buffer if necessary."
ac1ce341 1381 (interactive "P")
492d2437
RS
1382 (let* ((dir (dired-current-directory))
1383 (up (file-name-directory (directory-file-name dir))))
1384 (or (dired-goto-file (directory-file-name dir))
7b2469ae
RS
1385 ;; Only try dired-goto-subdir if buffer has more than one dir.
1386 (and (cdr dired-subdir-alist)
492d2437
RS
1387 (dired-goto-subdir up))
1388 (progn
ac1ce341
EN
1389 (if other-window
1390 (dired-other-window up)
1391 (dired up))
492d2437 1392 (dired-goto-file dir)))))
84fc2cfa 1393
68d2f12f
RS
1394(defun dired-get-file-for-visit ()
1395 "Get the current line's file name, with an error if file does not exist."
53fd9d05 1396 (interactive)
e9407052
RS
1397 ;; We pass t for second arg so that we don't get error for `.' and `..'.
1398 (let ((raw (dired-get-filename nil t))
1399 file-name)
1400 (if (null raw)
1401 (error "No file on this line"))
1402 (setq file-name (file-name-sans-versions raw t))
c439687b 1403 (if (file-exists-p file-name)
68d2f12f 1404 file-name
55642aa4
RS
1405 (if (file-symlink-p file-name)
1406 (error "File is a symlink to a nonexistent target")
1407 (error "File no longer exists; type `g' to update Dired buffer")))))
84fc2cfa 1408
68d2f12f
RS
1409;; Force `f' rather than `e' in the mode doc:
1410(defalias 'dired-advertised-find-file 'dired-find-file)
1411(defun dired-find-file ()
1412 "In Dired, visit the file or directory named on this line."
1413 (interactive)
1d5ad120 1414 ;; Bind `find-file-run-dired' so that the command works on directories
59d18288
RS
1415 ;; too, independent of the user's setting.
1416 (let ((find-file-run-dired t))
1417 (find-file (dired-get-file-for-visit))))
68d2f12f 1418
c5753a5d 1419(defun dired-find-alternate-file ()
68d2f12f 1420 "In Dired, visit this file or directory instead of the dired buffer."
c5753a5d
GM
1421 (interactive)
1422 (set-buffer-modified-p nil)
68d2f12f 1423 (find-alternate-file (dired-get-file-for-visit)))
336cd99d 1424;; Don't override the setting from .emacs.
c21993d0 1425;;;###autoload (put 'dired-find-alternate-file 'disabled t)
c5753a5d 1426
dbcb9389 1427(defun dired-mouse-find-file-other-window (event)
68d2f12f 1428 "In Dired, visit the file or directory name you click on."
dbcb9389 1429 (interactive "e")
2aaa7f0a 1430 (let (window pos file)
dbcb9389 1431 (save-excursion
2aaa7f0a
RS
1432 (setq window (posn-window (event-end event))
1433 pos (posn-point (event-end event)))
1434 (if (not (windowp window))
1435 (error "No file chosen"))
1436 (set-buffer (window-buffer window))
1437 (goto-char pos)
1438 (setq file (dired-get-file-for-visit)))
55a87770
RS
1439 (if (file-directory-p file)
1440 (or (and (cdr dired-subdir-alist)
1441 (dired-goto-subdir file))
1442 (progn
1443 (select-window window)
1444 (dired-other-window file)))
1445 (let (cmd)
1446 ;; Look for some other way to view a certain file.
1447 (dolist (elt dired-view-command-alist)
1448 (if (string-match (car elt) file)
1449 (setq cmd (cdr elt))))
1450 (if cmd
1451 (call-process shell-file-name nil 0 nil
1452 "-c"
1453 (concat (format cmd (shell-quote-argument file))
1454 " &"))
1455 (select-window window)
1456 (find-file-other-window (file-name-sans-versions file t)))))))
b70ebe37 1457
84fc2cfa 1458(defun dired-view-file ()
68d2f12f 1459 "In Dired, examine a file in view mode, returning to dired when done.
b70ebe37
RS
1460When file is a directory, show it in this buffer if it is inserted.
1461Some kinds of files are displayed using external viewer programs;
1462see `dired-view-command-alist'. Otherwise, display it in another buffer."
84fc2cfa 1463 (interactive)
68d2f12f
RS
1464 (let ((file (dired-get-file-for-visit)))
1465 (if (file-directory-p file)
1466 (or (and (cdr dired-subdir-alist)
1467 (dired-goto-subdir file))
1468 (dired file))
b70ebe37
RS
1469 (let (cmd)
1470 ;; Look for some other way to view a certain file.
1471 (dolist (elt dired-view-command-alist)
1472 (if (string-match (car elt) file)
1473 (setq cmd (cdr elt))))
1474 (if cmd
90c1e36a
RS
1475 (call-process shell-file-name nil 0 nil
1476 "-c"
55a87770 1477 (concat (format cmd (shell-quote-argument file))
90c1e36a 1478 " &"))
b70ebe37 1479 (view-file file))))))
84fc2cfa
ER
1480
1481(defun dired-find-file-other-window ()
68d2f12f 1482 "In Dired, visit this file or directory in another window."
84fc2cfa 1483 (interactive)
68d2f12f 1484 (find-file-other-window (dired-get-file-for-visit)))
ab67260b
RS
1485
1486(defun dired-display-file ()
68d2f12f 1487 "In Dired, display this file or directory in another window."
ab67260b 1488 (interactive)
68d2f12f 1489 (display-buffer (find-file-noselect (dired-get-file-for-visit))))
83fadedf 1490\f
68d2f12f 1491;;; Functions for extracting and manipulating file names in Dired buffers.
84fc2cfa
ER
1492
1493(defun dired-get-filename (&optional localp no-error-if-not-filep)
68d2f12f 1494 "In Dired, return name of file mentioned on this line.
84fc2cfa 1495Value returned normally includes the directory name.
492d2437 1496Optional arg LOCALP with value `no-dir' means don't include directory
147e214c
LT
1497name in result. A value of `verbatim' means to return the name exactly as
1498it occurs in the buffer, and a value of t means construct name relative to
1499`default-directory', which still may contain slashes if in a subdirectory.
1500Optional arg NO-ERROR-IF-NOT-FILEP means treat `.' and `..' as
1501regular filenames and return nil if no filename on this line.
1502Otherwise, an error occurs in these cases."
a5e0e1a8 1503 (let (case-fold-search file p1 p2 already-absolute)
84fc2cfa 1504 (save-excursion
492d2437
RS
1505 (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
1506 (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
1507 ;; nil if no file on this line, but no-error-if-not-filep is t:
9888aa65
RS
1508 (if (setq file (and p1 p2 (buffer-substring p1 p2)))
1509 (progn
1510 ;; Get rid of the mouse-face property that file names have.
1511 (set-text-properties 0 (length file) nil file)
1512 ;; Unquote names quoted by ls or by dired-insert-directory.
1513 ;; Using read to unquote is much faster than substituting
1514 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
1515 (setq file
1516 (read
1517 (concat "\""
2b2059d8 1518 ;; Some ls -b don't escape quotes, argh!
9888aa65
RS
1519 ;; This is not needed for GNU ls, though.
1520 (or (dired-string-replace-match
bc07e5cb 1521 "\\([^\\]\\|\\`\\)\"" file "\\1\\\\\"" nil t)
9888aa65 1522 file)
038b5501
KH
1523 "\"")))
1524 ;; The above `read' will return a unibyte string if FILE
1525 ;; contains eight-bit-control/graphic characters.
1526 (if (and enable-multibyte-characters
1527 (not (multibyte-string-p file)))
1528 (setq file (string-to-multibyte file)))))
f6e2cbe3 1529 (and file (file-name-absolute-p file)
7994d91a
RS
1530 ;; A relative file name can start with ~.
1531 ;; Don't treat it as absolute in this context.
1532 (not (eq (aref file 0) ?~))
a5e0e1a8 1533 (setq already-absolute t))
a5e0e1a8 1534 (cond
b95ddab3
RS
1535 ((null file)
1536 nil)
1d5ad120
JB
1537 ((eq localp 'verbatim)
1538 file)
e9407052 1539 ((and (not no-error-if-not-filep)
147e214c
LT
1540 (save-excursion
1541 (beginning-of-line)
1542 (looking-at dired-re-dot)))
e9407052 1543 (error "Cannot operate on `.' or `..'"))
a5e0e1a8
EZ
1544 ((and (eq localp 'no-dir) already-absolute)
1545 (file-name-nondirectory file))
b95ddab3 1546 (already-absolute
ca66efd1
RS
1547 (let ((handler (find-file-name-handler file nil)))
1548 ;; check for safe-magic property so that we won't
1549 ;; put /: for names that don't really need them.
0b7bc76f 1550 ;; For instance, .gz files when auto-compression-mode is on.
ca66efd1
RS
1551 (if (and handler (not (get handler 'safe-magic)))
1552 (concat "/:" file)
1553 file)))
b95ddab3 1554 ((eq localp 'no-dir)
a5e0e1a8 1555 file)
b95ddab3
RS
1556 ((equal (dired-current-directory) "/")
1557 (setq file (concat (dired-current-directory localp) file))
ca66efd1
RS
1558 (let ((handler (find-file-name-handler file nil)))
1559 ;; check for safe-magic property so that we won't
1560 ;; put /: for names that don't really need them.
1561 ;; For instance, .gz files when auto-compression-mode is on.
1562 (if (and handler (not (get handler 'safe-magic)))
1563 (concat "/:" file)
1564 file)))
a5e0e1a8 1565 (t
b95ddab3 1566 (concat (dired-current-directory localp) file)))))
492d2437 1567
437fe5ae 1568(defun dired-string-replace-match (regexp string newtext
1d5ad120 1569 &optional literal global)
437fe5ae
RS
1570 "Replace first match of REGEXP in STRING with NEWTEXT.
1571If it does not match, nil is returned instead of the new string.
1572Optional arg LITERAL means to take NEWTEXT literally.
1573Optional arg GLOBAL means to replace all matches."
1574 (if global
b976e099 1575 (let ((start 0) ret)
856321e2
RS
1576 (while (string-match regexp string start)
1577 (let ((from-end (- (length string) (match-end 0))))
b976e099 1578 (setq ret (setq string (replace-match newtext t literal string)))
856321e2 1579 (setq start (- (length string) from-end))))
b976e099 1580 ret)
437fe5ae
RS
1581 (if (not (string-match regexp string 0))
1582 nil
856321e2 1583 (replace-match newtext t literal string))))
437fe5ae 1584
492d2437 1585(defun dired-make-absolute (file &optional dir)
470fa6d1 1586 ;;"Convert FILE (a file name relative to DIR) to an absolute file name."
492d2437
RS
1587 ;; We can't always use expand-file-name as this would get rid of `.'
1588 ;; or expand in / instead default-directory if DIR=="".
1589 ;; This should be good enough for ange-ftp, but might easily be
1590 ;; redefined (for VMS?).
1591 ;; It should be reasonably fast, though, as it is called in
1592 ;; dired-get-filename.
1593 (concat (or dir default-directory) file))
1594
827dc60d
RS
1595(defun dired-make-relative (file &optional dir ignore)
1596 "Convert FILE (an absolute file name) to a name relative to DIR.
1597If this is impossible, return FILE unchanged.
1598DIR must be a directory name, not a file name."
492d2437 1599 (or dir (setq dir default-directory))
7425bbff
RS
1600 ;; This case comes into play if default-directory is set to
1601 ;; use ~.
1602 (if (and (> (length dir) 0) (= (aref dir 0) ?~))
1603 (setq dir (expand-file-name dir)))
492d2437
RS
1604 (if (string-match (concat "^" (regexp-quote dir)) file)
1605 (substring file (match-end 0))
827dc60d
RS
1606;;; (or no-error
1607;;; (error "%s: not in directory tree growing at %s" file dir))
1608 file))
83fadedf 1609\f
492d2437
RS
1610;;; Functions for finding the file name in a dired buffer line.
1611
c4a7b2a4 1612(defvar dired-move-to-filename-regexp
2708e59e 1613 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
60389964 1614 (l-or-quote "\\([A-Za-z']\\|[^\0-\177]\\)")
6a5c00da 1615 ;; In some locales, month abbreviations are as short as 2 letters,
b4bbe888 1616 ;; and they can be followed by ".".
60389964
RS
1617 ;; In Breton, a month name can include a quote character.
1618 (month (concat l-or-quote l-or-quote "+\\.?"))
32184bdb
PE
1619 (s " ")
1620 (yyyy "[0-9][0-9][0-9][0-9]")
b4bbe888 1621 (dd "[ 0-3][0-9]")
32184bdb 1622 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
32df8b47
EZ
1623 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
1624 (zone "[-+][0-2][0-9][0-5][0-9]")
1625 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
1626 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
1627 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
b4bbe888
PE
1628 "\\|" yyyy "-" iso-mm-dd "\\)"))
1629 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
1630 s "+"
1631 "\\(" HH:MM "\\|" yyyy "\\)"))
1632 (western-comma (concat month s "+" dd "," s "+" yyyy))
1633 ;; Japanese MS-Windows ls-lisp has one-digit months, and
1634 ;; omits the Kanji characters after month and day-of-month.
1635 (mm "[ 0-1]?[0-9]")
501be301 1636 (japanese
b4bbe888
PE
1637 (concat mm l "?" s dd l "?" s "+"
1638 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
42afed7c 1639 ;; The "[0-9]" below requires the previous column to end in a digit.
6a5c00da
PE
1640 ;; This avoids recognizing `1 may 1997' as a date in the line:
1641 ;; -r--r--r-- 1 may 1997 1168 Oct 19 16:49 README
0952583c 1642 ;; The "[kKMGTPEZY]?" below supports "ls -alh" output.
42afed7c
GM
1643 ;; The ".*" below finds the last match if there are multiple matches.
1644 ;; This avoids recognizing `jservice 10 1024' as a date in the line:
02882bbc 1645 ;; drwxr-xr-x 3 jservice 10 1024 Jul 2 1997 esg-host
0952583c 1646 (concat ".*[0-9][kKMGTPEZY]?" s
b4bbe888
PE
1647 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
1648 s "+"))
6a5c00da 1649 "Regular expression to match up to the file name in a directory listing.
1f97e2fc
PE
1650The default value is designed to recognize dates and times
1651regardless of the language.")
9eabb7d5 1652
23e217f6
RS
1653(defvar dired-permission-flags-regexp
1654 "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
1655 "Regular expression to match the permission flags in `ls -l'.")
1656
492d2437
RS
1657;; Move to first char of filename on this line.
1658;; Returns position (point) or nil if no filename on this line."
1659(defun dired-move-to-filename (&optional raise-error eol)
1660 ;; This is the UNIX version.
2b2059d8 1661 (or eol (setq eol (line-end-position)))
492d2437 1662 (beginning-of-line)
0b7bc76f 1663 ;; First try assuming `ls --dired' was used.
2b2059d8
SM
1664 (let ((change (next-single-property-change (point) 'dired-filename nil eol)))
1665 (cond
1666 ((and change (< change eol))
1667 (goto-char change))
1668 ((re-search-forward dired-move-to-filename-regexp eol t)
1669 (goto-char (match-end 0)))
1670 ((re-search-forward dired-permission-flags-regexp eol t)
1671 ;; Ha! There *is* a file. Our regexp-from-hell just failed to find it.
1672 (funcall (if raise-error 'error 'message)
1673 "Unrecognized line! Check dired-move-to-filename-regexp"))
1674 (raise-error
1675 (error "No file on this line")))))
492d2437
RS
1676
1677(defun dired-move-to-end-of-filename (&optional no-error)
1678 ;; Assumes point is at beginning of filename,
1679 ;; thus the rwx bit re-search-backward below will succeed in *this*
1680 ;; line if at all. So, it should be called only after
1681 ;; (dired-move-to-filename t).
1682 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
1683 ;; This is the UNIX version.
0b7bc76f
RS
1684 (if (get-text-property (point) 'dired-filename)
1685 (goto-char (next-single-property-change (point) 'dired-filename))
1686 (let (opoint file-type executable symlink hidden case-fold-search used-F eol)
1687 ;; case-fold-search is nil now, so we can test for capital F:
1688 (setq used-F (string-match "F" dired-actual-switches)
1689 opoint (point)
1690 eol (save-excursion (end-of-line) (point))
1691 hidden (and selective-display
1692 (save-excursion (search-forward "\r" eol t))))
1693 (if hidden
1694 nil
1695 (save-excursion ;; Find out what kind of file this is:
1696 ;; Restrict perm bits to be non-blank,
1697 ;; otherwise this matches one char to early (looking backward):
1698 ;; "l---------" (some systems make symlinks that way)
1699 ;; "----------" (plain file with zero perms)
1700 (if (re-search-backward
1701 dired-permission-flags-regexp nil t)
1702 (setq file-type (char-after (match-beginning 1))
1703 symlink (eq file-type ?l)
1704 ;; Only with -F we need to know whether it's an executable
1705 executable (and
1706 used-F
1707 (string-match
1708 "[xst]" ;; execute bit set anywhere?
1709 (concat
1710 (buffer-substring (match-beginning 2)
1711 (match-end 2))
1712 (buffer-substring (match-beginning 3)
1713 (match-end 3))
1714 (buffer-substring (match-beginning 4)
1715 (match-end 4))))))
1716 (or no-error (error "No file on this line"))))
1717 ;; Move point to end of name:
1718 (if symlink
1719 (if (search-forward " ->" eol t)
1720 (progn
1721 (forward-char -3)
1722 (and used-F
1723 dired-ls-F-marks-symlinks
1724 (eq (preceding-char) ?@) ;; did ls really mark the link?
1725 (forward-char -1))))
1726 (goto-char eol) ;; else not a symbolic link
1727 ;; ls -lF marks dirs, sockets and executables with exactly one
1728 ;; trailing character. (Executable bits on symlinks ain't mean
1729 ;; a thing, even to ls, but we know it's not a symlink.)
1730 (and used-F
1731 (or (memq file-type '(?d ?s))
1732 executable)
1733 (forward-char -1))))
1734 (or no-error
1735 (not (eq opoint (point)))
1736 (error (if hidden
1737 (substitute-command-keys
1738 "File line is hidden, type \\[dired-hide-subdir] to unhide")
1739 "No file on this line")))
1740 (if (eq opoint (point))
1741 nil
1742 (point)))))
492d2437 1743
83fadedf 1744\f
a3f4a3ef
RS
1745;;; COPY NAMES OF MARKED FILES INTO KILL-RING.
1746
1747(defun dired-copy-filename-as-kill (&optional arg)
1748 "Copy names of marked (or next ARG) files into the kill ring.
1749The names are separated by a space.
470fa6d1
KS
1750With a zero prefix arg, use the absolute file name of each marked file.
1751With \\[universal-argument], use the file name sans directory of each marked file.
a3f4a3ef
RS
1752
1753If on a subdir headerline, use subdirname instead; prefix arg is ignored
1754in this case.
1755
1756You can then feed the file name(s) to other commands with \\[yank]."
1757 (interactive "P")
1758 (let ((string
1759 (or (dired-get-subdir)
1760 (mapconcat (function identity)
1761 (if arg
1762 (cond ((zerop (prefix-numeric-value arg))
1763 (dired-get-marked-files))
1764 ((integerp arg)
1765 (dired-get-marked-files 'no-dir arg))
1766 (t ; else a raw arg
1767 (dired-get-marked-files t)))
1768 (dired-get-marked-files 'no-dir))
1769 " "))))
4de547e4
RS
1770 (if (eq last-command 'kill-region)
1771 (kill-append string nil)
1772 (kill-new string))
a3f4a3ef
RS
1773 (message "%s" string)))
1774
1775\f
492d2437
RS
1776;; Keeping Dired buffers in sync with the filesystem and with each other
1777
ef746164 1778(defun dired-buffers-for-dir (dir &optional file)
492d2437 1779;; Return a list of buffers that dired DIR (top level or in-situ subdir).
ef746164
RS
1780;; If FILE is non-nil, include only those whose wildcard pattern (if any)
1781;; matches FILE.
492d2437
RS
1782;; The list is in reverse order of buffer creation, most recent last.
1783;; As a side effect, killed dired buffers for DIR are removed from
1784;; dired-buffers.
1785 (setq dir (file-name-as-directory dir))
ef746164 1786 (let ((alist dired-buffers) result elt buf pattern)
492d2437 1787 (while alist
38819778
RS
1788 (setq elt (car alist)
1789 buf (cdr elt))
1790 (if (buffer-name buf)
1791 (if (dired-in-this-tree dir (car elt))
ef746164
RS
1792 (with-current-buffer buf
1793 (and (assoc dir dired-subdir-alist)
1794 (or (null file)
1795 (let ((wildcards
1796 (file-name-nondirectory dired-directory)))
1797 (or (= 0 (length wildcards))
1798 (string-match (dired-glob-regexp wildcards)
1799 file))))
1800 (setq result (cons buf result)))))
38819778
RS
1801 ;; else buffer is killed - clean up:
1802 (setq dired-buffers (delq elt dired-buffers)))
492d2437
RS
1803 (setq alist (cdr alist)))
1804 result))
1805
ef746164
RS
1806(defun dired-glob-regexp (pattern)
1807 "Convert glob-pattern PATTERN to a regular expression."
1808 (let ((matched-in-pattern 0) ;; How many chars of PATTERN we've handled.
1809 regexp)
1810 (while (string-match "[[?*]" pattern matched-in-pattern)
1811 (let ((op-end (match-end 0))
1812 (next-op (aref pattern (match-beginning 0))))
1813 (setq regexp (concat regexp
1814 (regexp-quote
1815 (substring pattern matched-in-pattern
1816 (match-beginning 0)))))
1817 (cond ((= next-op ??)
1818 (setq regexp (concat regexp "."))
1819 (setq matched-in-pattern op-end))
1820 ((= next-op ?\[)
1821 ;; Fails to handle ^ yet ????
1822 (let* ((set-start (match-beginning 0))
1823 (set-cont
1824 (if (= (aref pattern (1+ set-start)) ?^)
1825 (+ 3 set-start)
1826 (+ 2 set-start)))
1827 (set-end (string-match "]" pattern set-cont))
1828 (set (substring pattern set-start (1+ set-end))))
1829 (setq regexp (concat regexp set))
1830 (setq matched-in-pattern (1+ set-end))))
1831 ((= next-op ?*)
1832 (setq regexp (concat regexp ".*"))
1833 (setq matched-in-pattern op-end)))))
1834 (concat "\\`"
1835 regexp
1836 (regexp-quote
1837 (substring pattern matched-in-pattern))
1838 "\\'")))
1839
c60ee5e7 1840
ef746164 1841
492d2437
RS
1842(defun dired-advertise ()
1843 ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
1844 ;; With wildcards we actually advertise too much.
38819778
RS
1845 (let ((expanded-default (expand-file-name default-directory)))
1846 (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
1847 t ; we have already advertised ourselves
1848 (setq dired-buffers
1849 (cons (cons expanded-default (current-buffer))
1850 dired-buffers)))))
492d2437
RS
1851
1852(defun dired-unadvertise (dir)
1853 ;; Remove DIR from the buffer alist in variable dired-buffers.
1854 ;; This has the effect of removing any buffer whose main directory is DIR.
1855 ;; It does not affect buffers in which DIR is a subdir.
1856 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
1857 (setq dired-buffers
38819778 1858 (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
83fadedf 1859\f
492d2437
RS
1860;; Tree Dired
1861
1862;;; utility functions
1863
1864(defun dired-in-this-tree (file dir)
1865 ;;"Is FILE part of the directory tree starting at DIR?"
1866 (let (case-fold-search)
38819778 1867 (string-match (concat "^" (regexp-quote dir)) file)))
492d2437
RS
1868
1869(defun dired-normalize-subdir (dir)
470fa6d1
KS
1870 ;; Prepend default-directory to DIR if relative file name.
1871 ;; dired-get-filename must be able to make a valid file name from a
492d2437
RS
1872 ;; file and its directory DIR.
1873 (file-name-as-directory
1874 (if (file-name-absolute-p dir)
1875 dir
1876 (expand-file-name dir default-directory))))
1877
1878(defun dired-get-subdir ()
1879 ;;"Return the subdir name on this line, or nil if not on a headerline."
1880 ;; Look up in the alist whether this is a headerline.
1881 (save-excursion
1882 (let ((cur-dir (dired-current-directory)))
1883 (beginning-of-line) ; alist stores b-o-l positions
1884 (and (zerop (- (point)
1885 (dired-get-subdir-min (assoc cur-dir
1886 dired-subdir-alist))))
1887 cur-dir))))
1888
1889;(defun dired-get-subdir-min (elt)
1890; (cdr elt))
1891;; can't use macro, must be redefinable for other alist format in dired-nstd.
62f61df0 1892(defalias 'dired-get-subdir-min 'cdr)
492d2437
RS
1893
1894(defun dired-get-subdir-max (elt)
84fc2cfa 1895 (save-excursion
492d2437
RS
1896 (goto-char (dired-get-subdir-min elt))
1897 (dired-subdir-max)))
1898
1899(defun dired-clear-alist ()
1900 (while dired-subdir-alist
1901 (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
1902 (setq dired-subdir-alist (cdr dired-subdir-alist))))
1903
c9d59fc2
RS
1904(defun dired-subdir-index (dir)
1905 ;; Return an index into alist for use with nth
1906 ;; for the sake of subdir moving commands.
1907 (let (found (index 0) (alist dired-subdir-alist))
1908 (while alist
1909 (if (string= dir (car (car alist)))
1910 (setq alist nil found t)
1911 (setq alist (cdr alist) index (1+ index))))
1912 (if found index nil)))
1913
1914(defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
1915 "Go to next subdirectory, regardless of level."
1916 ;; Use 0 arg to go to this directory's header line.
1917 ;; NO-SKIP prevents moving to end of header line, returning whatever
1918 ;; position was found in dired-subdir-alist.
1919 (interactive "p")
1920 (let ((this-dir (dired-current-directory))
1921 pos index)
1922 ;; nth with negative arg does not return nil but the first element
1923 (setq index (- (dired-subdir-index this-dir) arg))
1924 (setq pos (if (>= index 0)
1925 (dired-get-subdir-min (nth index dired-subdir-alist))))
1926 (if pos
1927 (progn
1928 (goto-char pos)
1929 (or no-skip (skip-chars-forward "^\n\r"))
1930 (point))
1931 (if no-error-if-not-found
1932 nil ; return nil if not found
1933 (error "%s directory" (if (> arg 0) "Last" "First"))))))
1934
3c13627a 1935(defun dired-build-subdir-alist (&optional switches)
492d2437 1936 "Build `dired-subdir-alist' by parsing the buffer.
3c13627a
EZ
1937Returns the new value of the alist.
1938If optional arg SWITCHES is non-nil, use its value
1939instead of `dired-actual-switches'."
492d2437
RS
1940 (interactive)
1941 (dired-clear-alist)
1942 (save-excursion
3c13627a
EZ
1943 (let* ((count 0)
1944 (buffer-read-only nil)
1945 (switches (or switches dired-actual-switches))
1946 new-dir-name
1947 (R-ftp-base-dir-regex
1948 ;; Used to expand subdirectory names correctly in recursive
1949 ;; ange-ftp listings.
1950 (and (string-match "R" switches)
1951 (string-match "\\`/.*:\\(/.*\\)" default-directory)
1952 (concat "\\`" (match-string 1 default-directory)))))
84fc2cfa 1953 (goto-char (point-min))
492d2437 1954 (setq dired-subdir-alist nil)
acf310c9
RS
1955 (while (and (re-search-forward dired-subdir-regexp nil t)
1956 ;; Avoid taking a file name ending in a colon
1957 ;; as a subdir name.
1958 (not (save-excursion
1959 (goto-char (match-beginning 0))
1960 (beginning-of-line)
1961 (forward-char 2)
46680faf 1962 (save-match-data (looking-at dired-re-perms)))))
3222085e
BF
1963 (save-excursion
1964 (goto-char (match-beginning 1))
1965 (setq new-dir-name
b4c01776
GM
1966 (buffer-substring-no-properties (point) (match-end 1))
1967 new-dir-name
1968 (save-match-data
1969 (if (and R-ftp-base-dir-regex
1970 (not (string= new-dir-name default-directory))
1971 (string-match R-ftp-base-dir-regex new-dir-name))
1972 (concat default-directory
1973 (substring new-dir-name (match-end 0)))
1974 (expand-file-name new-dir-name))))
3222085e
BF
1975 (delete-region (point) (match-end 1))
1976 (insert new-dir-name))
492d2437 1977 (setq count (1+ count))
3222085e
BF
1978 (dired-alist-add-1 new-dir-name
1979 ;; Place a sub directory boundary between lines.
1980 (save-excursion
1981 (goto-char (match-beginning 0))
1982 (beginning-of-line)
1983 (point-marker))))
c9d59fc2
RS
1984 (if (> count 1)
1985 (message "Buffer includes %d directories" count))
492d2437
RS
1986 ;; We don't need to sort it because it is in buffer order per
1987 ;; constructionem. Return new alist:
1988 dired-subdir-alist)))
1989
1990(defun dired-alist-add-1 (dir new-marker)
1991 ;; Add new DIR at NEW-MARKER. Don't sort.
1992 (setq dired-subdir-alist
1993 (cons (cons (dired-normalize-subdir dir) new-marker)
1994 dired-subdir-alist)))
1995
1996(defun dired-goto-next-nontrivial-file ()
1997 ;; Position point on first nontrivial file after point.
1998 (dired-goto-next-file);; so there is a file to compare with
1999 (if (stringp dired-trivial-filenames)
2000 (while (and (not (eobp))
2001 (string-match dired-trivial-filenames
2002 (file-name-nondirectory
2003 (or (dired-get-filename nil t) ""))))
2004 (forward-line 1)
2005 (dired-move-to-filename))))
2006
2007(defun dired-goto-next-file ()
2008 (let ((max (1- (dired-subdir-max))))
2009 (while (and (not (dired-move-to-filename)) (< (point) max))
2010 (forward-line 1))))
2011
2012(defun dired-goto-file (file)
2013 "Go to file line of FILE in this dired buffer."
2014 ;; Return value of point on success, else nil.
470fa6d1 2015 ;; FILE must be an absolute file name.
492d2437
RS
2016 ;; Loses if FILE contains control chars like "\007" for which ls
2017 ;; either inserts "?" or "\\007" into the buffer, so we won't find
2018 ;; it in the buffer.
2019 (interactive
2020 (prog1 ; let push-mark display its message
2021 (list (expand-file-name
2022 (read-file-name "Goto file: "
2023 (dired-current-directory))))
2024 (push-mark)))
2025 (setq file (directory-file-name file)) ; does no harm if no directory
2026 (let (found case-fold-search dir)
2027 (setq dir (or (file-name-directory file)
f808da9e 2028 (error "File name `%s' is not absolute" file)))
492d2437
RS
2029 (save-excursion
2030 ;; The hair here is to get the result of dired-goto-subdir
2031 ;; without really calling it if we don't have any subdirs.
233993a3 2032 (if (if (string= dir (expand-file-name default-directory))
492d2437 2033 (goto-char (point-min))
7b2469ae 2034 (and (cdr dired-subdir-alist)
492d2437
RS
2035 (dired-goto-subdir dir)))
2036 (let ((base (file-name-nondirectory file))
f808da9e 2037 search-string
492d2437 2038 (boundary (dired-subdir-max)))
f808da9e
RS
2039 (setq search-string
2040 (replace-regexp-in-string "\^m" "\\^m" base nil t))
2041 (setq search-string
2042 (replace-regexp-in-string "\\\\" "\\\\" search-string nil t))
492d2437
RS
2043 (while (and (not found)
2044 ;; filenames are preceded by SPC, this makes
2045 ;; the search faster (e.g. for the filename "-"!).
f808da9e
RS
2046 (search-forward (concat " " search-string)
2047 boundary 'move))
492d2437
RS
2048 ;; Match could have BASE just as initial substring or
2049 ;; or in permission bits or date or
2050 ;; not be a proper filename at all:
2051 (if (equal base (dired-get-filename 'no-dir t))
2052 ;; Must move to filename since an (actually
2053 ;; correct) match could have been elsewhere on the
2054 ;; ;; line (e.g. "-" would match somewhere in the
2055 ;; permission bits).
55e86af6
RS
2056 (setq found (dired-move-to-filename))
2057 ;; If this isn't the right line, move forward to avoid
2058 ;; trying this line again.
2059 (forward-line 1))))))
492d2437
RS
2060 (and found
2061 ;; return value of point (i.e., FOUND):
2062 (goto-char found))))
2063
2064(defun dired-initial-position (dirname)
2065 ;; Where point should go in a new listing of DIRNAME.
2066 ;; Point assumed at beginning of new subdir line.
2067 ;; You may redefine this function as you wish, e.g. like in dired-x.el.
2068 (end-of-line)
2069 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
83fadedf 2070\f
492d2437
RS
2071;; These are hooks which make tree dired work.
2072;; They are in this file because other parts of dired need to call them.
2073;; But they don't call the rest of tree dired unless there are subdirs loaded.
2074
2075;; This function is called for each retrieved filename.
2076;; It could stand to be faster, though it's mostly function call
2077;; overhead. Avoiding the function call seems to save about 10% in
2078;; dired-get-filename. Make it a defsubst?
2079(defun dired-current-directory (&optional localp)
2080 "Return the name of the subdirectory to which this line belongs.
2081This returns a string with trailing slash, like `default-directory'.
2082Optional argument means return a file name relative to `default-directory'."
2083 (let ((here (point))
2084 (alist (or dired-subdir-alist
2085 ;; probably because called in a non-dired buffer
2086 (error "No subdir-alist in %s" (current-buffer))))
2087 elt dir)
2088 (while alist
2089 (setq elt (car alist)
2090 dir (car elt)
2091 ;; use `<=' (not `<') as subdir line is part of subdir
2092 alist (if (<= (dired-get-subdir-min elt) here)
2093 nil ; found
2094 (cdr alist))))
2095 (if localp
2096 (dired-make-relative dir default-directory)
2097 dir)))
2098
2099;; Subdirs start at the beginning of their header lines and end just
2100;; before the beginning of the next header line (or end of buffer).
2101
2102(defun dired-subdir-max ()
2103 (save-excursion
7b2469ae 2104 (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
492d2437
RS
2105 (point-max)
2106 (point))))
83fadedf 2107\f
492d2437
RS
2108;; Deleting files
2109
f0628026
RS
2110(defcustom dired-recursive-deletes nil ; Default only delete empty directories.
2111 "*Decide whether recursive deletes are allowed.
f0529b5b 2112nil means no recursive deletes.
f0628026
RS
2113`always' means delete recursively without asking. This is DANGEROUS!
2114`top' means ask for each directory at top level, but delete its subdirectories
2115without asking.
2116Anything else means ask for each directory."
53ade5c7
RS
2117 :type '(choice :tag "Delete non-empty directories"
2118 (const :tag "Yes" always)
2119 (const :tag "No--only delete empty directories" nil)
2120 (const :tag "Confirm for each directory" t)
2121 (const :tag "Confirm for each top directory only" top))
f0628026
RS
2122 :group 'dired)
2123
c60ee5e7 2124;; Match anything but `.' and `..'.
f0628026
RS
2125(defvar dired-re-no-dot "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")
2126
2127;; Delete file, possibly delete a directory and all its files.
2128;; This function is usefull outside of dired. One could change it's name
2129;; to e.g. recursive-delete-file and put it somewhere else.
2130(defun dired-delete-file (file &optional recursive) "\
2131Delete FILE or directory (possibly recursively if optional RECURSIVE is true.)
2132RECURSIVE determines what to do with a non-empty directory. If RECURSIVE is:
2133Nil, do not delete.
2134`always', delete recursively without asking.
2135`top', ask for each directory at top level.
2136Anything else, ask for each sub-directory."
2137 (let (files)
2138 ;; This test is equivalent to
2139 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
2140 ;; but more efficient
2141 (if (not (eq t (car (file-attributes file))))
2142 (delete-file file)
2143 (when (and recursive
2144 (setq files
2145 (directory-files file t dired-re-no-dot)) ; Not empty.
2146 (or (eq recursive 'always)
2147 (yes-or-no-p (format "Recursive delete of %s "
2148 (dired-make-relative file)))))
2149 (if (eq recursive 'top) (setq recursive 'always)) ; Don't ask again.
2150 (while files ; Recursively delete (possibly asking).
2151 (dired-delete-file (car files) recursive)
2152 (setq files (cdr files))))
2153 (delete-directory file))))
2154
7da0e9b4 2155(defun dired-do-flagged-delete (&optional nomessage)
68d2f12f 2156 "In Dired, delete the files flagged for deletion.
7da0e9b4
RS
2157If NOMESSAGE is non-nil, we don't display any message
2158if there are no flagged files."
492d2437
RS
2159 (interactive)
2160 (let* ((dired-marker-char dired-del-marker)
2161 (regexp (dired-marker-regexp))
2162 case-fold-search)
2163 (if (save-excursion (goto-char (point-min))
2164 (re-search-forward regexp nil t))
2165 (dired-internal-do-deletions
2166 ;; this can't move point since ARG is nil
2167 (dired-map-over-marks (cons (dired-get-filename) (point))
2168 nil)
2169 nil)
7da0e9b4
RS
2170 (or nomessage
2171 (message "(No deletions requested)")))))
492d2437
RS
2172
2173(defun dired-do-delete (&optional arg)
2174 "Delete all marked (or next ARG) files."
2175 ;; This is more consistent with the file marking feature than
2176 ;; dired-do-flagged-delete.
2177 (interactive "P")
2178 (dired-internal-do-deletions
2179 ;; this may move point if ARG is an integer
2180 (dired-map-over-marks (cons (dired-get-filename) (point))
2181 arg)
2182 arg))
2183
2184(defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
2185
2186(defun dired-internal-do-deletions (l arg)
2187 ;; L is an alist of files to delete, with their buffer positions.
2188 ;; ARG is the prefix arg.
2189 ;; Filenames are absolute (VMS needs this for logical search paths).
2190 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
2191 ;; That way as changes are made in the buffer they do not shift the
2192 ;; lines still to be changed, so the (point) values in L stay valid.
2193 ;; Also, for subdirs in natural order, a subdir's files are deleted
2194 ;; before the subdir itself - the other way around would not work.
2195 (let ((files (mapcar (function car) l))
2196 (count (length l))
2197 (succ 0))
2198 ;; canonicalize file list for pop up
2199 (setq files (nreverse (mapcar (function dired-make-relative) files)))
2200 (if (dired-mark-pop-up
2201 " *Deletions*" 'delete files dired-deletion-confirmer
2202 (format "Delete %s " (dired-mark-prompt arg files)))
84fc2cfa 2203 (save-excursion
492d2437
RS
2204 (let (failures);; files better be in reverse order for this loop!
2205 (while l
2206 (goto-char (cdr (car l)))
2207 (let (buffer-read-only)
2208 (condition-case err
2209 (let ((fn (car (car l))))
f0628026 2210 (dired-delete-file fn dired-recursive-deletes)
492d2437
RS
2211 ;; if we get here, removing worked
2212 (setq succ (1+ succ))
2213 (message "%s of %s deletions" succ count)
bf989169
RS
2214 (dired-fun-in-all-buffers
2215 (file-name-directory fn) (file-name-nondirectory fn)
2216 (function dired-delete-entry) fn))
492d2437
RS
2217 (error;; catch errors from failed deletions
2218 (dired-log "%s\n" err)
2219 (setq failures (cons (car (car l)) failures)))))
2220 (setq l (cdr l)))
2221 (if (not failures)
2222 (message "%d deletion%s done" count (dired-plural-s count))
2223 (dired-log-summary
2224 (format "%d of %d deletion%s failed"
2225 (length failures) count
2226 (dired-plural-s count))
2227 failures))))
2228 (message "(No deletions performed)")))
2229 (dired-move-to-filename))
2230
bf989169
RS
2231(defun dired-fun-in-all-buffers (directory file fun &rest args)
2232 ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS.
2233 ;; If the buffer has a wildcard pattern, check that it matches FILE.
2234 ;; (FILE does not include a directory component.)
2235 ;; FILE may be nil, in which case ignore it.
2236 ;; Return list of buffers where FUN succeeded (i.e., returned non-nil).
2237 (let (success-list)
2238 (dolist (buf (dired-buffers-for-dir (expand-file-name directory)
2239 file))
2240 (with-current-buffer buf
2241 (if (apply fun args)
2242 (setq success-list (cons (buffer-name buf) success-list)))))
2243 success-list))
2244
c60ee5e7 2245;; Delete the entry for FILE from
bf989169
RS
2246(defun dired-delete-entry (file)
2247 (save-excursion
2248 (and (dired-goto-file file)
2249 (let (buffer-read-only)
2250 (delete-region (progn (beginning-of-line) (point))
2251 (save-excursion (forward-line 1) (point))))))
2252 (dired-clean-up-after-deletion file))
2253
492d2437
RS
2254;; This is a separate function for the sake of dired-x.el.
2255(defun dired-clean-up-after-deletion (fn)
2256 ;; Clean up after a deleted file or directory FN.
7b2469ae
RS
2257 (save-excursion (and (cdr dired-subdir-alist)
2258 (dired-goto-subdir fn)
492d2437 2259 (dired-kill-subdir))))
83fadedf 2260\f
492d2437
RS
2261;; Confirmation
2262
2263(defun dired-marker-regexp ()
2264 (concat "^" (regexp-quote (char-to-string dired-marker-char))))
2265
2266(defun dired-plural-s (count)
2267 (if (= 1 count) "" "s"))
2268
2269(defun dired-mark-prompt (arg files)
2270 ;; Return a string for use in a prompt, either the current file
2271 ;; name, or the marker and a count of marked files.
2272 (let ((count (length files)))
2273 (if (= count 1)
2274 (car files)
2275 ;; more than 1 file:
2276 (if (integerp arg)
2277 ;; abs(arg) = count
2278 ;; Perhaps this is nicer, but it also takes more screen space:
2279 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
2280 ;; count)
2281 (format "[next %d files]" arg)
2282 (format "%c [%d files]" dired-marker-char count)))))
2283
2284(defun dired-pop-to-buffer (buf)
2285 ;; Pop up buffer BUF.
2286 ;; If dired-shrink-to-fit is t, make its window fit its contents.
2287 (if (not dired-shrink-to-fit)
2288 (pop-to-buffer (get-buffer-create buf))
2289 ;; let window shrink to fit:
2290 (let ((window (selected-window))
2291 target-lines w2)
54f03575 2292 (cond ;; if split-height-threshold is enabled, use the largest window
492d2437
RS
2293 ((and (> (window-height (setq w2 (get-largest-window)))
2294 split-height-threshold)
f98955ea 2295 (= (frame-width) (window-width w2)))
492d2437
RS
2296 (setq window w2))
2297 ;; if the least-recently-used window is big enough, use it
2298 ((and (> (window-height (setq w2 (get-lru-window)))
2299 (* 2 window-min-height))
f98955ea 2300 (= (frame-width) (window-width w2)))
492d2437
RS
2301 (setq window w2)))
2302 (save-excursion
2303 (set-buffer buf)
2304 (goto-char (point-max))
2305 (skip-chars-backward "\n\r\t ")
63ea14a5
RS
2306 (setq target-lines (count-lines (point-min) (point)))
2307 ;; Don't forget to count the last line.
2308 (if (not (bolp))
2309 (setq target-lines (1+ target-lines))))
492d2437 2310 (if (<= (window-height window) (* 2 window-min-height))
f98955ea 2311 ;; At this point, every window on the frame is too small to split.
492d2437
RS
2312 (setq w2 (display-buffer buf))
2313 (setq w2 (split-window window
2314 (max window-min-height
2315 (- (window-height window)
2316 (1+ (max window-min-height target-lines)))))))
2317 (set-window-buffer w2 buf)
2318 (if (< (1- (window-height w2)) target-lines)
2319 (progn
2320 (select-window w2)
2321 (enlarge-window (- target-lines (1- (window-height w2))))))
2322 (set-window-start w2 1)
2323 )))
2324
2325(defvar dired-no-confirm nil
0347d069
SM
2326 "A list of symbols for commands dired should not confirm.
2327Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress',
2328`copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink' and
2329`uncompress'.")
492d2437
RS
2330
2331(defun dired-mark-pop-up (bufname op-symbol files function &rest args)
8421685f
RS
2332 "Return FUNCTION's result on ARGS after showing which files are marked.
2333Displays the file names in a buffer named BUFNAME;
2334 nil gives \" *Marked Files*\".
2335This uses function `dired-pop-to-buffer' to do that.
2336
2337FUNCTION should not manipulate files, just read input
2338 (an argument or confirmation).
89101e46
SM
2339The window is not shown if there is just one file or
2340 OP-SYMBOL is a member of the list in `dired-no-confirm'.
2341FILES is the list of marked files."
492d2437 2342 (or bufname (setq bufname " *Marked Files*"))
0347d069
SM
2343 (if (or (eq dired-no-confirm t)
2344 (memq op-symbol dired-no-confirm)
492d2437
RS
2345 (= (length files) 1))
2346 (apply function args)
89101e46 2347 (with-current-buffer (get-buffer-create bufname)
492d2437 2348 (erase-buffer)
a07e7c4a 2349 (dired-format-columns-of-files files)
1a0b9ae6
EZ
2350 (remove-text-properties (point-min) (point-max)
2351 '(mouse-face nil help-echo nil)))
492d2437
RS
2352 (save-window-excursion
2353 (dired-pop-to-buffer bufname)
2354 (apply function args))))
2355
2356(defun dired-format-columns-of-files (files)
2357 ;; Files should be in forward order for this loop.
2358 ;; i.e., (car files) = first file in buffer.
2359 ;; Returns the number of lines used.
2360 (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files))))
2361 (width (- (window-width (selected-window)) 2))
2362 (columns (max 1 (/ width maxlen)))
2363 (nfiles (length files))
2364 (rows (+ (/ nfiles columns)
2365 (if (zerop (% nfiles columns)) 0 1)))
2366 (i 0)
2367 (j 0))
2368 (setq files (nconc (copy-sequence files) ; fill up with empty fns
2369 (make-list (- (* columns rows) nfiles) "")))
2370 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
2371 (while (< j rows)
2372 (while (< i columns)
2373 (indent-to (* i maxlen))
2374 (insert (car files))
2375 (setq files (nthcdr rows files)
2376 i (1+ i)))
2377 (insert "\n")
2378 (setq i 0
2379 j (1+ j)
2380 files (cdr files)))
2381 rows))
83fadedf 2382\f
492d2437
RS
2383;; Commands to mark or flag file(s) at or near current line.
2384
2385(defun dired-repeat-over-lines (arg function)
2386 ;; This version skips non-file lines.
cfe89c4f 2387 (let ((pos (make-marker)))
492d2437 2388 (beginning-of-line)
cfe89c4f
RS
2389 (while (and (> arg 0) (not (eobp)))
2390 (setq arg (1- arg))
2391 (beginning-of-line)
2392 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
2393 (save-excursion
2394 (forward-line 1)
2395 (move-marker pos (1+ (point))))
2396 (save-excursion (funcall function))
2397 ;; Advance to the next line--actually, to the line that *was* next.
2398 ;; (If FUNCTION inserted some new lines in between, skip them.)
2399 (goto-char pos))
2400 (while (and (< arg 0) (not (bobp)))
2401 (setq arg (1+ arg))
2402 (forward-line -1)
2403 (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
2404 (beginning-of-line)
2405 (save-excursion (funcall function)))
2406 (move-marker pos nil)
2407 (dired-move-to-filename)))
492d2437
RS
2408
2409(defun dired-between-files ()
8a372755
MB
2410 ;; This used to be a regexp match of the `total ...' line output by
2411 ;; ls, which is slightly faster, but that is not very robust; notably,
2412 ;; it fails for non-english locales.
2413 (save-excursion (not (dired-move-to-filename))))
492d2437
RS
2414
2415(defun dired-next-marked-file (arg &optional wrap opoint)
2416 "Move to the next marked file, wrapping around the end of the buffer."
2417 (interactive "p\np")
2418 (or opoint (setq opoint (point)));; return to where interactively started
2419 (if (if (> arg 0)
2420 (re-search-forward dired-re-mark nil t arg)
2421 (beginning-of-line)
2422 (re-search-backward dired-re-mark nil t (- arg)))
2423 (dired-move-to-filename)
2424 (if (null wrap)
2425 (progn
2426 (goto-char opoint)
2427 (error "No next marked file"))
2428 (message "(Wraparound for next marked file)")
2429 (goto-char (if (> arg 0) (point-min) (point-max)))
2430 (dired-next-marked-file arg nil opoint))))
2431
2432(defun dired-prev-marked-file (arg &optional wrap)
2433 "Move to the previous marked file, wrapping around the end of the buffer."
2434 (interactive "p\np")
2435 (dired-next-marked-file (- arg) wrap))
2436
2437(defun dired-file-marker (file)
2438 ;; Return FILE's marker, or nil if unmarked.
2439 (save-excursion
2440 (and (dired-goto-file file)
2441 (progn
2442 (beginning-of-line)
2443 (if (not (equal ?\040 (following-char)))
2444 (following-char))))))
2445
2446(defun dired-mark-files-in-region (start end)
2447 (let (buffer-read-only)
2448 (if (> start end)
2449 (error "start > end"))
2450 (goto-char start) ; assumed at beginning of line
2451 (while (< (point) end)
2452 ;; Skip subdir line and following garbage like the `total' line:
2453 (while (and (< (point) end) (dired-between-files))
2454 (forward-line 1))
2455 (if (and (not (looking-at dired-re-dot))
2456 (dired-get-filename nil t))
2457 (progn
2458 (delete-char 1)
2459 (insert dired-marker-char)))
2460 (forward-line 1))))
2461
2462(defun dired-mark (arg)
2463 "Mark the current (or next ARG) files.
2464If on a subdir headerline, mark all its files except `.' and `..'.
2465
2466Use \\[dired-unmark-all-files] to remove all marks
2467and \\[dired-unmark] on a subdir to remove the marks in
2468this subdir."
2469 (interactive "P")
277568f2 2470 (if (dired-get-subdir)
492d2437
RS
2471 (save-excursion (dired-mark-subdir-files))
2472 (let (buffer-read-only)
2473 (dired-repeat-over-lines
52041219 2474 (prefix-numeric-value arg)
492d2437
RS
2475 (function (lambda () (delete-char 1) (insert dired-marker-char)))))))
2476
2477(defun dired-unmark (arg)
2478 "Unmark the current (or next ARG) files.
2479If looking at a subdir, unmark all its files except `.' and `..'."
2480 (interactive "P")
2481 (let ((dired-marker-char ?\040))
2482 (dired-mark arg)))
2483
2484(defun dired-flag-file-deletion (arg)
68d2f12f 2485 "In Dired, flag the current line's file for deletion.
492d2437
RS
2486With prefix arg, repeat over several lines.
2487
2488If on a subdir headerline, mark all its files except `.' and `..'."
2489 (interactive "P")
2490 (let ((dired-marker-char dired-del-marker))
2491 (dired-mark arg)))
2492
2493(defun dired-unmark-backward (arg)
68d2f12f 2494 "In Dired, move up lines and remove deletion flag there.
492d2437
RS
2495Optional prefix ARG says how many lines to unflag; default is one line."
2496 (interactive "p")
2497 (dired-unmark (- arg)))
e5f0841e 2498
d588eb90
RS
2499(defun dired-toggle-marks ()
2500 "Toggle marks: marked files become unmarked, and vice versa.
e5f0841e
KH
2501Files marked with other flags (such as `D') are not affected.
2502`.' and `..' are never toggled.
2503As always, hidden subdirs are not affected."
2504 (interactive)
2505 (save-excursion
2506 (goto-char (point-min))
2507 (let (buffer-read-only)
2508 (while (not (eobp))
2509 (or (dired-between-files)
2510 (looking-at dired-re-dot)
2511 ;; use subst instead of insdel because it does not move
2512 ;; the gap and thus should be faster and because
2513 ;; other characters are left alone automatically
2514 (apply 'subst-char-in-region
2515 (point) (1+ (point))
2516 (if (eq ?\040 (following-char)) ; SPC
2517 (list ?\040 dired-marker-char)
2518 (list dired-marker-char ?\040))))
2519 (forward-line 1)))))
83fadedf 2520\f
492d2437
RS
2521;;; Commands to mark or flag files based on their characteristics or names.
2522
d2cadc4b
RS
2523(defvar dired-regexp-history nil
2524 "History list of regular expressions used in Dired commands.")
2525
2526(defun dired-read-regexp (prompt)
2527 (read-from-minibuffer prompt nil nil nil 'dired-regexp-history))
492d2437
RS
2528
2529(defun dired-mark-files-regexp (regexp &optional marker-char)
2530 "Mark all files matching REGEXP for use in later commands.
2531A prefix argument means to unmark them instead.
2532`.' and `..' are never marked.
2533
2534REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
2535object files--just `.o' will mark more than you might think."
2536 (interactive
2537 (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
2538 " files (regexp): "))
2539 (if current-prefix-arg ?\040)))
2540 (let ((dired-marker-char (or marker-char dired-marker-char)))
2541 (dired-mark-if
2542 (and (not (looking-at dired-re-dot))
2543 (not (eolp)) ; empty line
2544 (let ((fn (dired-get-filename nil t)))
2545 (and fn (string-match regexp (file-name-nondirectory fn)))))
2546 "matching file")))
2547
e9b8e22d
RS
2548(defun dired-mark-files-containing-regexp (regexp &optional marker-char)
2549 "Mark all files with contents containing REGEXP for use in later commands.
2550A prefix argument means to unmark them instead.
2551`.' and `..' are never marked."
2552 (interactive
2553 (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
2554 " files containing (regexp): "))
2555 (if current-prefix-arg ?\040)))
2556 (let ((dired-marker-char (or marker-char dired-marker-char)))
2557 (dired-mark-if
2558 (and (not (looking-at dired-re-dot))
2559 (not (eolp)) ; empty line
2560 (let ((fn (dired-get-filename nil t)))
05a40373
KH
2561 (when (and fn (file-readable-p fn)
2562 (not (file-directory-p fn)))
2563 (let ((prebuf (get-file-buffer fn)))
2564 (message "Checking %s" fn)
2565 ;; For now we do it inside emacs
2566 ;; Grep might be better if there are a lot of files
2567 (if prebuf
2568 (with-current-buffer prebuf
2569 (save-excursion
2570 (goto-char (point-min))
2571 (re-search-forward regexp nil t)))
2572 (with-temp-buffer
2573 (insert-file-contents fn)
2574 (goto-char (point-min))
2575 (re-search-forward regexp nil t))))
0b2bb4d0 2576 )))
e9b8e22d
RS
2577 "matching file")))
2578
492d2437 2579(defun dired-flag-files-regexp (regexp)
68d2f12f 2580 "In Dired, flag all files containing the specified REGEXP for deletion.
492d2437
RS
2581The match is against the non-directory part of the filename. Use `^'
2582 and `$' to anchor matches. Exclude subdirs by hiding them.
2583`.' and `..' are never flagged."
2584 (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
2585 (dired-mark-files-regexp regexp dired-del-marker))
2586
2587(defun dired-mark-symlinks (unflag-p)
2588 "Mark all symbolic links.
2589With prefix argument, unflag all those files."
2590 (interactive "P")
2591 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2592 (dired-mark-if (looking-at dired-re-sym) "symbolic link")))
2593
2594(defun dired-mark-directories (unflag-p)
2595 "Mark all directory file lines except `.' and `..'.
2596With prefix argument, unflag all those files."
2597 (interactive "P")
2598 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2599 (dired-mark-if (and (looking-at dired-re-dir)
2600 (not (looking-at dired-re-dot)))
2601 "directory file")))
2602
2603(defun dired-mark-executables (unflag-p)
2604 "Mark all executable files.
2605With prefix argument, unflag all those files."
2606 (interactive "P")
2607 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
2608 (dired-mark-if (looking-at dired-re-exe) "executable file")))
2609
2610;; dired-x.el has a dired-mark-sexp interactive command: mark
2611;; files for which PREDICATE returns non-nil.
2612
2613(defun dired-flag-auto-save-files (&optional unflag-p)
84fc2cfa
ER
2614 "Flag for deletion files whose names suggest they are auto save files.
2615A prefix argument says to unflag those files instead."
2616 (interactive "P")
492d2437
RS
2617 (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
2618 (dired-mark-if
2a838614 2619 ;; It is less than general to check for # here,
a91526b9
RS
2620 ;; but it's the only way this runs fast enough.
2621 (and (save-excursion (end-of-line)
536ae2dd
RS
2622 (or
2623 (eq (preceding-char) ?#)
2624 ;; Handle executables in case of -F option.
2625 ;; We need not worry about the other kinds
2626 ;; of markings that -F makes, since they won't
2627 ;; appear on real auto-save files.
2628 (if (eq (preceding-char) ?*)
2629 (progn
2630 (forward-char -1)
2631 (eq (preceding-char) ?#)))))
a91526b9
RS
2632 (not (looking-at dired-re-dir))
2633 (let ((fn (dired-get-filename t t)))
2634 (if fn (auto-save-file-name-p
2635 (file-name-nondirectory fn)))))
2636 "auto save file")))
492d2437 2637
84d3f6e8 2638(defvar dired-garbage-files-regexp
d4aeef3b
KG
2639 (concat (regexp-opt
2640 '(".log" ".toc" ".dvi" ".bak" ".orig" ".rej" ".aux"))
c60ee5e7 2641 "\\'")
84d3f6e8
RS
2642 "*Regular expression to match \"garbage\" files for `dired-flag-garbage-files'.")
2643
2644(defun dired-flag-garbage-files ()
84d3f6e8 2645 "Flag for deletion all files that match `dired-garbage-files-regexp'."
f656ec48 2646 (interactive)
84d3f6e8
RS
2647 (dired-flag-files-regexp dired-garbage-files-regexp))
2648
492d2437
RS
2649(defun dired-flag-backup-files (&optional unflag-p)
2650 "Flag all backup files (names ending with `~') for deletion.
2651With prefix argument, unflag these files."
2652 (interactive "P")
fdee13ec 2653 (let ((dired-marker-char (if unflag-p ?\ dired-del-marker)))
492d2437 2654 (dired-mark-if
f13101e9
KH
2655 ;; Don't call backup-file-name-p unless the last character looks like
2656 ;; it might be the end of a backup file name. This isn't very general,
a91526b9
RS
2657 ;; but it's the only way this runs fast enough.
2658 (and (save-excursion (end-of-line)
f13101e9
KH
2659 ;; Handle executables in case of -F option.
2660 ;; We need not worry about the other kinds
2661 ;; of markings that -F makes, since they won't
2662 ;; appear on real backup files.
2663 (if (eq (preceding-char) ?*)
2664 (forward-char -1))
fdee13ec 2665 (eq (preceding-char) ?~))
a91526b9 2666 (not (looking-at dired-re-dir))
492d2437
RS
2667 (let ((fn (dired-get-filename t t)))
2668 (if fn (backup-file-name-p fn))))
2669 "backup file")))
2670
6482fcac
RS
2671(defun dired-change-marks (&optional old new)
2672 "Change all OLD marks to NEW marks.
2673OLD and NEW are both characters used to mark files."
2674 (interactive
2675 (let* ((cursor-in-echo-area t)
2676 (old (progn (message "Change (old mark): ") (read-char)))
2677 (new (progn (message "Change %c marks to (new mark): " old)
2678 (read-char))))
2679 (list old new)))
e4e02841
RS
2680 (if (or (eq old ?\r) (eq new ?\r))
2681 (ding)
2682 (let ((string (format "\n%c" old))
2683 (buffer-read-only))
2684 (save-excursion
2685 (goto-char (point-min))
2686 (while (search-forward string nil t)
a15a76f7
RS
2687 (if (if (= old ?\ )
2688 (save-match-data
2689 (dired-get-filename 'no-dir t))
2690 t)
2691 (subst-char-in-region (match-beginning 0)
2692 (match-end 0) old new)))))))
6482fcac 2693
534a6edf 2694(defun dired-unmark-all-marks ()
b1ecd9c6
RS
2695 "Remove all marks from all files in the Dired buffer."
2696 (interactive)
2697 (dired-unmark-all-files ?\r))
2698
8b87a301 2699(defun dired-unmark-all-files (mark &optional arg)
b602ba2f 2700 "Remove a specific mark (or any mark) from every file.
c60ee5e7 2701After this command, type the mark character to remove,
b602ba2f 2702or type RET to remove all marks.
3585916f 2703With prefix arg, query for each marked file.
492d2437 2704Type \\[help-command] at that time for help."
b602ba2f
RS
2705 (interactive "cRemove marks (RET means all): \nP")
2706 (save-excursion
2707 (let* ((count 0)
2708 buffer-read-only case-fold-search query
2709 (string (format "\n%c" mark))
2710 (help-form "\
8b87a301
RS
2711Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
2712`!' to unmark all remaining files with no more questions."))
b602ba2f
RS
2713 (goto-char (point-min))
2714 (while (if (eq mark ?\r)
2715 (re-search-forward dired-re-mark nil t)
2716 (search-forward string nil t))
2717 (if (or (not arg)
e9407052
RS
2718 (let ((file (dired-get-filename t t)))
2719 (and file
2720 (dired-query 'query "Unmark file `%s'? "
2721 file))))
b602ba2f
RS
2722 (progn (subst-char-in-region (1- (point)) (point)
2723 (preceding-char) ?\ )
2724 (setq count (1+ count)))))
2725 (message (if (= count 1) "1 mark removed"
2726 "%d marks removed")
2727 count))))
83fadedf 2728\f
492d2437 2729;; Logging failures operating on files, and showing the results.
84fc2cfa 2730
492d2437 2731(defvar dired-log-buffer "*Dired log*")
84fc2cfa 2732
492d2437
RS
2733(defun dired-why ()
2734 "Pop up a buffer with error log output from Dired.
2735A group of errors from a single command ends with a formfeed.
2736Thus, use \\[backward-page] to find the beginning of a group of errors."
2737 (interactive)
2738 (if (get-buffer dired-log-buffer)
2739 (let ((owindow (selected-window))
2740 (window (display-buffer (get-buffer dired-log-buffer))))
2741 (unwind-protect
6482fcac 2742 (progn
492d2437
RS
2743 (select-window window)
2744 (goto-char (point-max))
0b7bc76f
RS
2745 (forward-line -1)
2746 (backward-page 1)
2747 (recenter 0))
492d2437
RS
2748 (select-window owindow)))))
2749
2750(defun dired-log (log &rest args)
2751 ;; Log a message or the contents of a buffer.
2752 ;; If LOG is a string and there are more args, it is formatted with
2753 ;; those ARGS. Usually the LOG string ends with a \n.
c60ee5e7 2754 ;; End each bunch of errors with (dired-log t):
0b7bc76f
RS
2755 ;; this inserts the current time and buffer at the start of the page,
2756 ;; and \f (formfeed) at the end.
492d2437 2757 (let ((obuf (current-buffer)))
0b7bc76f
RS
2758 (with-current-buffer (get-buffer-create dired-log-buffer)
2759 (goto-char (point-max))
2760 (let ((inhibit-read-only t))
2761 (cond ((stringp log)
2762 (insert (if args
2763 (apply (function format) log args)
2764 log)))
2765 ((bufferp log)
2766 (insert-buffer log))
2767 ((eq t log)
2768 (backward-page 1)
2769 (unless (bolp)
2770 (insert "\n"))
2771 (insert (current-time-string)
2772 "\tBuffer `" (buffer-name obuf) "'\n")
2773 (goto-char (point-max))
2774 (insert "\f\n")))))))
492d2437
RS
2775
2776(defun dired-log-summary (string failures)
0b7bc76f
RS
2777 (if (= (length failures) 1)
2778 (message "%s"
2779 (with-current-buffer dired-log-buffer
2780 (goto-char (point-max))
2781 (backward-page 1)
2782 (if (eolp) (forward-line 1))
2783 (buffer-substring (point) (point-max))))
2784 (message (if failures "%s--type ? for details (%s)"
2785 "%s--type ? for details")
2786 string failures))
492d2437 2787 ;; Log a summary describing a bunch of errors.
0b7bc76f 2788 (dired-log (concat "\n" string "\n"))
492d2437 2789 (dired-log t))
83fadedf 2790\f
492d2437
RS
2791;;; Sorting
2792
2793;; Most ls can only sort by name or by date (with -t), nothing else.
2794;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
2795;; So anything that does not contain these is sort "by name".
2796
2797(defvar dired-ls-sorting-switches "SXU"
cfc8b264
EZ
2798 "String of `ls' switches \(single letters\) except `t' that influence sorting.
2799
2800This indicates to Dired which option switches to watch out for because they
2801will change the sorting order behavior of `ls'.
2802
2803To change the default sorting order \(e.g. add a `-v' option\), see the
2804variable `dired-listing-switches'. To temporarily override the listing
2805format, use `\\[universal-argument] \\[dired]'.")
492d2437
RS
2806
2807(defvar dired-sort-by-date-regexp
2808 (concat "^-[^" dired-ls-sorting-switches
2809 "]*t[^" dired-ls-sorting-switches "]*$")
2810 "Regexp recognized by dired to set `by date' mode.")
2811
2812(defvar dired-sort-by-name-regexp
2813 (concat "^-[^t" dired-ls-sorting-switches "]+$")
2814 "Regexp recognized by dired to set `by name' mode.")
2815
23fc67ea
RS
2816(defvar dired-sort-inhibit nil
2817 "Non-nil means the Dired sort command is disabled.
2818The idea is to set this buffer-locally in special Dired buffers.")
2819
492d2437
RS
2820(defun dired-sort-set-modeline ()
2821 ;; Set modeline display according to dired-actual-switches.
2822 ;; Modeline display of "by name" or "by date" guarantees the user a
2823 ;; match with the corresponding regexps. Non-matching switches are
2824 ;; shown literally.
1d673d85 2825 (setq mode-name
492d2437
RS
2826 (let (case-fold-search)
2827 (cond ((string-match dired-sort-by-name-regexp dired-actual-switches)
1d673d85 2828 "Dired by name")
492d2437 2829 ((string-match dired-sort-by-date-regexp dired-actual-switches)
1d673d85 2830 "Dired by date")
492d2437 2831 (t
1d673d85 2832 (concat "Dired " dired-actual-switches)))))
45d8d39f 2833 (force-mode-line-update))
492d2437
RS
2834
2835(defun dired-sort-toggle-or-edit (&optional arg)
2836 "Toggle between sort by date/name and refresh the dired buffer.
2837With a prefix argument you can edit the current listing switches instead."
84fc2cfa 2838 (interactive "P")
23fc67ea
RS
2839 (when dired-sort-inhibit
2840 (error "Cannot sort this Dired buffer"))
492d2437
RS
2841 (if arg
2842 (dired-sort-other
2843 (read-string "ls switches (must contain -l): " dired-actual-switches))
2844 (dired-sort-toggle)))
2845
2846(defun dired-sort-toggle ()
2847 ;; Toggle between sort by date/name. Reverts the buffer.
2848 (setq dired-actual-switches
2849 (let (case-fold-search)
7820b28f
RS
2850 (if (string-match " " dired-actual-switches)
2851 ;; New toggle scheme: add/remove a trailing " -t"
2852 (if (string-match " -t\\'" dired-actual-switches)
2b2059d8 2853 (substring dired-actual-switches 0 (match-beginning 0))
7820b28f
RS
2854 (concat dired-actual-switches " -t"))
2855 ;; old toggle scheme: look for some 't' switch and add/remove it
2856 (concat
2857 "-l"
83fadedf
DL
2858 (dired-replace-in-string (concat "[-lt"
2859 dired-ls-sorting-switches "]")
2860 ""
2861 dired-actual-switches)
7820b28f
RS
2862 (if (string-match (concat "[t" dired-ls-sorting-switches "]")
2863 dired-actual-switches)
2864 ""
2865 "t")))))
492d2437
RS
2866 (dired-sort-set-modeline)
2867 (revert-buffer))
2868
83fadedf 2869;; Some user code loads dired especially for this.
879fa8d0 2870;; Don't do that--use replace-regexp-in-string instead.
83fadedf
DL
2871(defun dired-replace-in-string (regexp newtext string)
2872 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
2873 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
2874 (let ((result "") (start 0) mb me)
2875 (while (string-match regexp string start)
2876 (setq mb (match-beginning 0)
2877 me (match-end 0)
2878 result (concat result (substring string start mb) newtext)
2879 start me))
2880 (concat result (substring string start))))
2881
492d2437
RS
2882(defun dired-sort-other (switches &optional no-revert)
2883 ;; Specify new ls SWITCHES for current dired buffer. Values matching
2884 ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
2885 ;; minor mode accordingly, others appear literally in the mode line.
2886 ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
e17dba1f 2887 (dired-sort-R-check switches)
492d2437 2888 (setq dired-actual-switches switches)
6cfd24f9 2889 (if (eq major-mode 'dired-mode) (dired-sort-set-modeline))
492d2437 2890 (or no-revert (revert-buffer)))
e17dba1f
GM
2891
2892(make-variable-buffer-local
2893 (defvar dired-subdir-alist-pre-R nil
2894 "Value of `dired-subdir-alist' before -R switch added."))
2895
2896(defun dired-sort-R-check (switches)
2897 "Additional processing of -R in ls option string SWITCHES.
2898Saves `dired-subdir-alist' when R is set and restores saved value
2899minus any directories explicitly deleted when R is cleared.
2900To be called first in body of `dired-sort-other', etc."
2901 (cond
2902 ((and (string-match "R" switches)
2903 (not (string-match "R" dired-actual-switches)))
2904 ;; Adding -R to ls switches -- save `dired-subdir-alist':
2905 (setq dired-subdir-alist-pre-R dired-subdir-alist))
2906 ((and (string-match "R" dired-actual-switches)
2907 (not (string-match "R" switches)))
2908 ;; Deleting -R from ls switches -- revert to pre-R subdirs
2909 ;; that are still present:
2910 (setq dired-subdir-alist
2911 (if dired-subdir-alist-pre-R
2912 (let (subdirs)
2913 (while dired-subdir-alist-pre-R
2914 (if (assoc (caar dired-subdir-alist-pre-R)
2915 dired-subdir-alist)
2916 ;; subdir still present...
2917 (setq subdirs
2918 (cons (car dired-subdir-alist-pre-R)
2919 subdirs)))
2920 (setq dired-subdir-alist-pre-R
2921 (cdr dired-subdir-alist-pre-R)))
2922 (reverse subdirs))
2923 ;; No pre-R subdir alist, so revert to main directory
2924 ;; listing:
2925 (list (car (reverse dired-subdir-alist))))))))
83fadedf 2926\f
492d2437
RS
2927;; To make this file smaller, the less common commands
2928;; go in a separate file. But autoload them here
2929;; to make the separation invisible.
2930
2931(autoload 'dired-diff "dired-aux"
2932 "Compare file at point with file FILE using `diff'.
a1af8dcf
EZ
2933FILE defaults to the file at the mark. (That's the mark set by
2934\\[set-mark-command], not by Dired's \\[dired-mark] command.)
ab67260b 2935The prompted-for file is the first file given to `diff'."
492d2437
RS
2936 t)
2937
2938(autoload 'dired-backup-diff "dired-aux"
2939 "Diff this file with its backup file or vice versa.
2940Uses the latest backup, if there are several numerical backups.
2941If this file is a backup, diff it with its original.
ab67260b 2942The backup file is the first file given to `diff'."
492d2437
RS
2943 t)
2944
2d051399
RS
2945(autoload 'dired-clean-directory "dired-aux"
2946 "Flag numerical backups for deletion.
2947Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
2948Positive prefix arg KEEP overrides `dired-kept-versions';
2949Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
2950
2951To clear the flags on these files, you can use \\[dired-flag-backup-files]
2952with a prefix argument."
2953 t)
2954
492d2437
RS
2955(autoload 'dired-do-chmod "dired-aux"
2956 "Change the mode of the marked (or next ARG) files.
2957This calls chmod, thus symbolic modes like `g+w' are allowed."
2958 t)
2959
2960(autoload 'dired-do-chgrp "dired-aux"
2961 "Change the group of the marked (or next ARG) files."
2962 t)
2963
2964(autoload 'dired-do-chown "dired-aux"
2965 "Change the owner of the marked (or next ARG) files."
2966 t)
2967
2968(autoload 'dired-do-print "dired-aux"
2969 "Print the marked (or next ARG) files.
2970Uses the shell command coming from variables `lpr-command' and
2971`lpr-switches' as default."
2972 t)
2973
2974(autoload 'dired-do-shell-command "dired-aux"
6482fcac
RS
2975 "Run a shell command COMMAND on the marked files.
2976If no files are marked or a specific numeric prefix arg is given,
2977the next ARG files are used. Just \\[universal-argument] means the current file.
2978The prompt mentions the file(s) or the marker, as appropriate.
2979
8462b390
RS
2980If there is a `*' in COMMAND, surrounded by whitespace, this runs
2981COMMAND just once with the entire file list substituted there.
6482fcac 2982
8462b390
RS
2983If there is no `*', but there is a `?' in COMMAND, surrounded by
2984whitespace, this runs COMMAND on each file individually with the
2985file name substituted for `?'.
492d2437 2986
8462b390
RS
2987Otherwise, this runs COMMAND on each file individually with the
2988file name added at the end of COMMAND (separated by a space).
492d2437 2989
8462b390
RS
2990`*' and `?' when not surrounded by whitespace have no special
2991significance for `dired-do-shell-command', and are passed through
2992normally to the shell, but you must confirm first. To pass `*' by
2993itself to the shell as a wildcard, type `*\"\"'.
2994
2995If COMMAND produces output, it goes to a separate buffer.
2996
2997This feature does not try to redisplay Dired buffers afterward, as
2998there's no telling what files COMMAND may have changed.
2999Type \\[dired-do-redisplay] to redisplay the marked files.
3000
3001When COMMAND runs, its working directory is the top-level directory of
3002the Dired buffer, so output files usually are created there instead of
3003in a subdir.
3004
3005In a noninteractive call (from Lisp code), you must specify
3006the list of file names explicitly with the FILE-LIST argument."
492d2437
RS
3007 t)
3008
492d2437
RS
3009(autoload 'dired-do-kill-lines "dired-aux"
3010 "Kill all marked lines (not the files).
3011With a prefix arg, kill all lines not marked or flagged."
3012 t)
3013
3014(autoload 'dired-do-compress "dired-aux"
3015 "Compress or uncompress marked (or next ARG) files."
3016 t)
3017
3018(autoload 'dired-do-byte-compile "dired-aux"
3019 "Byte compile marked (or next ARG) Emacs Lisp files."
3020 t)
3021
3022(autoload 'dired-do-load "dired-aux"
3023 "Load the marked (or next ARG) Emacs Lisp files."
3024 t)
3025
3026(autoload 'dired-do-redisplay "dired-aux"
3027 "Redisplay all marked (or next ARG) files.
3028If on a subdir line, redisplay that subdirectory. In that case,
3029a prefix arg lets you edit the `ls' switches used for the new listing."
3030 t)
3031
492d2437 3032(autoload 'dired-create-directory "dired-aux"
84fc2cfa 3033 "Create a directory called DIRECTORY."
492d2437 3034 t)
84fc2cfa 3035
492d2437
RS
3036(autoload 'dired-do-copy "dired-aux"
3037 "Copy all marked (or next ARG) files, or copy the current file.
3038Thus, a zero prefix argument copies nothing. But it toggles the
3039variable `dired-copy-preserve-time' (which see)."
3040 t)
3041
3042(autoload 'dired-do-symlink "dired-aux"
3043 "Make symbolic links to current file or all marked (or next ARG) files.
3044When operating on just the current file, you specify the new name.
3045When operating on multiple or marked files, you specify a directory
3046and new symbolic links are made in that directory
3047with the same names that the files currently have."
3048 t)
3049
3050(autoload 'dired-do-hardlink "dired-aux"
3051 "Add names (hard links) current file or all marked (or next ARG) files.
3052When operating on just the current file, you specify the new name.
3053When operating on multiple or marked files, you specify a directory
3054and new hard links are made in that directory
3055with the same names that the files currently have."
3056 t)
3057
3058(autoload 'dired-do-rename "dired-aux"
3059 "Rename current file or all marked (or next ARG) files.
3060When renaming just the current file, you specify the new name.
3061When renaming multiple or marked files, you specify a directory."
3062 t)
3063
3064(autoload 'dired-do-rename-regexp "dired-aux"
3065 "Rename marked files containing REGEXP to NEWNAME.
3066As each match is found, the user must type a character saying
3067 what to do with it. For directions, type \\[help-command] at that time.
3068NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
3069REGEXP defaults to the last regexp used.
470fa6d1
KS
3070With a zero prefix arg, renaming by regexp affects the full file name;
3071usually only the non-directory part of file names is used and changed."
492d2437
RS
3072 t)
3073
3074(autoload 'dired-do-copy-regexp "dired-aux"
3075 "Copy all marked files containing REGEXP to NEWNAME.
99c364e4 3076See function `dired-do-rename-regexp' for more info."
492d2437
RS
3077 t)
3078
3079(autoload 'dired-do-hardlink-regexp "dired-aux"
3080 "Hardlink all marked files containing REGEXP to NEWNAME.
99c364e4 3081See function `dired-do-rename-regexp' for more info."
492d2437
RS
3082 t)
3083
3084(autoload 'dired-do-symlink-regexp "dired-aux"
3085 "Symlink all marked files containing REGEXP to NEWNAME.
99c364e4 3086See function `dired-do-rename-regexp' for more info."
492d2437
RS
3087 t)
3088
3089(autoload 'dired-upcase "dired-aux"
3090 "Rename all marked (or next ARG) files to upper case."
3091 t)
3092
3093(autoload 'dired-downcase "dired-aux"
3094 "Rename all marked (or next ARG) files to lower case."
3095 t)
3096
3097(autoload 'dired-maybe-insert-subdir "dired-aux"
3098 "Insert this subdirectory into the same dired buffer.
3099If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
3100 else inserts it at its natural place (as `ls -lR' would have done).
3101With a prefix arg, you may edit the ls switches used for this listing.
3102 You can add `R' to the switches to expand the whole tree starting at
3103 this subdirectory.
3104This function takes some pains to conform to `ls -lR' output."
3105 t)
3106
3107(autoload 'dired-next-subdir "dired-aux"
3108 "Go to next subdirectory, regardless of level."
3109 t)
3110
3111(autoload 'dired-prev-subdir "dired-aux"
3112 "Go to previous subdirectory, regardless of level.
3113When called interactively and not on a subdir line, go to this subdir's line."
3114 t)
3115
3116(autoload 'dired-goto-subdir "dired-aux"
3117 "Go to end of header line of DIR in this dired buffer.
3118Return value of point on success, otherwise return nil.
3119The next char is either \\n, or \\r if DIR is hidden."
3120 t)
3121
3122(autoload 'dired-mark-subdir-files "dired-aux"
3123 "Mark all files except `.' and `..'."
3124 t)
3125
3126(autoload 'dired-kill-subdir "dired-aux"
3127 "Remove all lines of current subdirectory.
3128Lower levels are unaffected."
3129 t)
3130
3131(autoload 'dired-tree-up "dired-aux"
3132 "Go up ARG levels in the dired tree."
3133 t)
3134
3135(autoload 'dired-tree-down "dired-aux"
3136 "Go down in the dired tree."
3137 t)
3138
3139(autoload 'dired-hide-subdir "dired-aux"
3140 "Hide or unhide the current subdirectory and move to next directory.
3141Optional prefix arg is a repeat factor.
3142Use \\[dired-hide-all] to (un)hide all directories."
3143 t)
3144
3145(autoload 'dired-hide-all "dired-aux"
3146 "Hide all subdirectories, leaving only their header lines.
3147If there is already something hidden, make everything visible again.
3148Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
3149 t)
f01fd03b
MB
3150
3151(autoload 'dired-show-file-type "dired-aux"
3152 "Print the type of FILE, according to the `file' command.
3153If FILE is a symbolic link and the optional argument DEREF-SYMLINKS is
c60ee5e7 3154true then the type of the file linked to by FILE is printed instead."
f01fd03b 3155 t)
b70ebe37
RS
3156
3157(autoload 'dired-run-shell-command "dired-aux")
2aaa7f0a
RS
3158
3159(autoload 'dired-query "dired-aux")
83fadedf 3160\f
133aad74
JD
3161
3162;;;; Drag and drop support
3163
3164(defun dired-dnd-test-function (window action types)
3165 "The test function for drag and drop into dired buffers.
3166WINDOW is where the mouse is when this function is called. It may be a frame
3167if the mouse is over the menu bar, scroll bar or tool bar.
3168ACTION is the suggested action from the source, and TYPES are the
3169types the drop data can have. This function only accepts drops with
3170types in `x-dnd-known-types'. It returns the action suggested by the source."
3171 (let ((type (x-dnd-choose-type types)))
3172 (if type
3173 (cons action type)
3174 nil)))
3175
3176(defun dired-dnd-popup-notice ()
3177 (x-popup-dialog
3178 t
3179 '("Recursive copies not enabled.\nSee variable dired-recursive-copies."
3180 ("Ok" . nil))))
3181
3182
3183(defun dired-dnd-do-ask-action (uri)
3184 ;; No need to get actions and descriptions from the source,
3185 ;; we only have three actions anyway.
3186 (let ((action (x-popup-menu
3187 t
3188 (list "What action?"
3189 (cons ""
3190 '(("Copy here" . copy)
3191 ("Move here" . move)
3192 ("Link here" . link)
3193 "--"
3194 ("Cancel" . nil)))))))
3195 (if action
3196 (dired-dnd-handle-local-file uri action)
3197 nil)))
3198
3199(defun dired-dnd-handle-local-file (uri action)
3200 "Copy, move or link a file to the dired directory.
3201URI is the file to handle, ACTION is one of copy, move, link or ask.
3202Ask means pop up a menu for the user to select one of copy, move or link."
3203 (require 'dired-aux)
3204 (let* ((from (x-dnd-get-local-file-name uri t))
3205 (to (if from (concat (dired-current-directory)
3206 (file-name-nondirectory from))
3207 nil)))
3208 (if from
3209 (cond ((or (eq action 'copy)
3210 (eq action 'private)) ; Treat private as copy.
3211
3212 ;; If copying a directory and dired-recursive-copies is nil,
3213 ;; dired-copy-file silently fails. Pop up a notice.
3214 (if (and (file-directory-p from)
3215 (not dired-recursive-copies))
3216 (dired-dnd-popup-notice)
3217 (progn
3218 (dired-copy-file from to 1)
3219 (dired-relist-entry to)
3220 action)))
3221
3222 ((eq action 'move)
3223 (dired-rename-file from to 1)
3224 (dired-relist-entry to)
3225 action)
3226
3227 ((eq action 'link)
3228 (make-symbolic-link from to 1)
3229 (dired-relist-entry to)
3230 action)
3231
3232 ((eq action 'ask)
3233 (dired-dnd-do-ask-action uri))
3234
3235 (t nil)))))
3236
3237(defun dired-dnd-handle-file (uri action)
3238 "Copy, move or link a file to the dired directory if it is a local file.
3239URI is the file to handle. If the hostname in the URI isn't local, do nothing.
3240ACTION is one of copy, move, link or ask.
3241Ask means pop up a menu for the user to select one of copy, move or link."
3242 (let ((local-file (x-dnd-get-local-file-uri uri)))
3243 (if local-file (dired-dnd-handle-local-file local-file action)
3244 nil)))
3245
3246
3247\f
492d2437
RS
3248(if (eq system-type 'vax-vms)
3249 (load "dired-vms"))
84fc2cfa 3250
52041219
RS
3251(provide 'dired)
3252
57e15005
BF
3253(run-hooks 'dired-load-hook) ; for your customizations
3254
ab5796a9 3255;;; arch-tag: e1af7a8f-691c-41a0-aac1-ddd4d3c87517
52041219 3256;;; dired.el ends here