Commit | Line | Data |
---|---|---|
295fb2ac | 1 | ;;; dired.el --- directory-browsing commands -*- lexical-binding: t -*- |
52041219 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1985-1986, 1992-1997, 2000-2011 |
f143d380 | 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 |
bd78fa1d | 9 | ;; Package: emacs |
84fc2cfa ER |
10 | |
11 | ;; This file is part of GNU Emacs. | |
12 | ||
eb3fa2cf | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
84fc2cfa | 14 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
84fc2cfa ER |
17 | |
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
84fc2cfa | 25 | |
52041219 RS |
26 | ;;; Commentary: |
27 | ||
bff7c1ad GM |
28 | ;; This is a major mode for directory browsing and editing. |
29 | ;; It is documented in the Emacs manual. | |
e41b2db1 | 30 | |
492d2437 RS |
31 | ;; Rewritten in 1990/1991 to add tree features, file marking and |
32 | ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>. | |
33 | ;; Finished up by rms in 1992. | |
34 | ||
52041219 | 35 | ;;; Code: |
492d2437 | 36 | |
2f0adb0b SM |
37 | (eval-when-compile (require 'cl)) |
38 | ||
492d2437 RS |
39 | ;;; Customizable variables |
40 | ||
286c247d RS |
41 | (defgroup dired nil |
42 | "Directory editing." | |
d7aed37c | 43 | :link '(custom-manual "(emacs)Dired") |
ca66efd1 | 44 | :group 'files) |
286c247d RS |
45 | |
46 | (defgroup dired-mark nil | |
68d2f12f | 47 | "Handling marks in Dired." |
286c247d RS |
48 | :prefix "dired-" |
49 | :group 'dired) | |
50 | ||
51 | ||
84fc2cfa | 52 | ;;;###autoload |
6bdad9ae | 53 | (defcustom dired-listing-switches (purecopy "-al") |
9201cc28 | 54 | "Switches passed to `ls' for Dired. MUST contain the `l' option. |
492d2437 | 55 | May contain all other options that don't contradict `-l'; |
b36b40ae | 56 | may contain even `F', `b', `i' and `s'. See also the variable |
48404d5a EZ |
57 | `dired-ls-F-marks-symlinks' concerning the `F' switch. |
58 | On systems such as MS-DOS and MS-Windows, which use `ls' emulation in Lisp, | |
59 | some of the `ls' switches are not supported; see the doc string of | |
a6d231bb | 60 | `insert-directory' in `ls-lisp.el' for more details." |
286c247d RS |
61 | :type 'string |
62 | :group 'dired) | |
84fc2cfa | 63 | |
bff7c1ad | 64 | (defcustom dired-subdir-switches nil |
5553077c | 65 | "If non-nil, switches passed to `ls' for inserting subdirectories. |
bff7c1ad GM |
66 | If nil, `dired-listing-switches' is used." |
67 | :group 'dired | |
68 | :type '(choice (const :tag "Use dired-listing-switches" nil) | |
69 | (string :tag "Switches"))) | |
492d2437 | 70 | |
bff7c1ad GM |
71 | (defcustom dired-chown-program |
72 | (purecopy (cond ((executable-find "chown") "chown") | |
73 | ((file-executable-p "/usr/sbin/chown") "/usr/sbin/chown") | |
74 | ((file-executable-p "/etc/chown") "/etc/chown") | |
75 | (t "chown"))) | |
76 | "Name of chown command (usually `chown')." | |
77 | :group 'dired | |
78 | :type 'file) | |
79 | ||
80 | (defcustom dired-use-ls-dired 'unspecified | |
8c44f097 CY |
81 | "Non-nil means Dired should use \"ls --dired\". |
82 | The special value of `unspecified' means to check explicitly, and | |
83 | save the result in this variable. This is performed the first | |
bff7c1ad GM |
84 | time `dired-insert-directory' is called." |
85 | :group 'dired | |
86 | :type '(choice (const :tag "Check for --dired support" unspecified) | |
87 | (const :tag "Do not use --dired" nil) | |
88 | (other :tag "Use --dired" t))) | |
89 | ||
90 | (defcustom dired-chmod-program "chmod" | |
91 | "Name of chmod command (usually `chmod')." | |
92 | :group 'dired | |
93 | :type 'file) | |
94 | ||
95 | (defcustom dired-touch-program "touch" | |
96 | "Name of touch command (usually `touch')." | |
97 | :group 'dired | |
98 | :type 'file) | |
57654b8c | 99 | |
286c247d | 100 | (defcustom dired-ls-F-marks-symlinks nil |
9201cc28 | 101 | "Informs Dired about how `ls -lF' marks symbolic links. |
fc83abf7 RS |
102 | Set this to t if `ls' (or whatever program is specified by |
103 | `insert-directory-program') with `-lF' marks the symbolic link | |
492d2437 | 104 | itself with a trailing @ (usually the case under Ultrix). |
84fc2cfa | 105 | |
492d2437 RS |
106 | Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to |
107 | nil (the default), if it gives `bar@ -> foo', set it to t. | |
108 | ||
109 | Dired checks if there is really a @ appended. Thus, if you have a | |
110 | marking `ls' program on one host and a non-marking on another host, and | |
111 | don't care about symbolic links which really end in a @, you can | |
286c247d RS |
112 | always set this variable to t." |
113 | :type 'boolean | |
114 | :group 'dired-mark) | |
492d2437 | 115 | |
aaa448c9 | 116 | (defcustom dired-trivial-filenames (purecopy "^\\.\\.?$\\|^#") |
9201cc28 | 117 | "Regexp of files to skip when finding first file of a directory. |
492d2437 | 118 | A value of nil means move to the subdir line. |
286c247d RS |
119 | A value of t means move to first file." |
120 | :type '(choice (const :tag "Move to subdir" nil) | |
121 | (const :tag "Move to first" t) | |
122 | regexp) | |
123 | :group 'dired) | |
492d2437 | 124 | |
286c247d | 125 | (defcustom dired-keep-marker-rename t |
492d2437 | 126 | ;; Use t as default so that moved files "take their markers with them". |
9201cc28 | 127 | "Controls marking of renamed files. |
492d2437 RS |
128 | If t, files keep their previous marks when they are renamed. |
129 | If a character, renamed files (whether previously marked or not) | |
286c247d RS |
130 | are afterward marked with that character." |
131 | :type '(choice (const :tag "Keep" t) | |
132 | (character :tag "Mark")) | |
133 | :group 'dired-mark) | |
492d2437 | 134 | |
286c247d | 135 | (defcustom dired-keep-marker-copy ?C |
9201cc28 | 136 | "Controls marking of copied files. |
492d2437 | 137 | If t, copied files are marked if and as the corresponding original files were. |
286c247d RS |
138 | If a character, copied files are unconditionally marked with that character." |
139 | :type '(choice (const :tag "Keep" t) | |
140 | (character :tag "Mark")) | |
141 | :group 'dired-mark) | |
492d2437 | 142 | |
286c247d | 143 | (defcustom dired-keep-marker-hardlink ?H |
9201cc28 | 144 | "Controls marking of newly made hard links. |
492d2437 | 145 | If t, they are marked if and as the files linked to were marked. |
286c247d RS |
146 | If a character, new links are unconditionally marked with that character." |
147 | :type '(choice (const :tag "Keep" t) | |
148 | (character :tag "Mark")) | |
149 | :group 'dired-mark) | |
492d2437 | 150 | |
286c247d | 151 | (defcustom dired-keep-marker-symlink ?Y |
9201cc28 | 152 | "Controls marking of newly made symbolic links. |
492d2437 | 153 | If t, they are marked if and as the files linked to were marked. |
286c247d RS |
154 | If a character, new links are unconditionally marked with that character." |
155 | :type '(choice (const :tag "Keep" t) | |
156 | (character :tag "Mark")) | |
157 | :group 'dired-mark) | |
492d2437 | 158 | |
286c247d | 159 | (defcustom dired-dwim-target nil |
9201cc28 | 160 | "If non-nil, Dired tries to guess a default target directory. |
492d2437 RS |
161 | This means: if there is a dired buffer displayed in the next window, |
162 | use its current subdir, instead of the current subdir of this dired buffer. | |
163 | ||
286c247d RS |
164 | The target is used in the prompt for file copy, rename etc." |
165 | :type 'boolean | |
166 | :group 'dired) | |
492d2437 | 167 | |
286c247d | 168 | (defcustom dired-copy-preserve-time t |
9201cc28 | 169 | "If non-nil, Dired preserves the last-modified time in a file copy. |
286c247d RS |
170 | \(This works on only some systems.)" |
171 | :type 'boolean | |
172 | :group 'dired) | |
492d2437 | 173 | |
05b855f5 EZ |
174 | ; These variables were deleted and the replacements are on files.el. |
175 | ; We leave aliases behind for back-compatibility. | |
176 | (defvaralias 'dired-free-space-program 'directory-free-space-program) | |
177 | (defvaralias 'dired-free-space-args 'directory-free-space-args) | |
178 | ||
492d2437 RS |
179 | ;;; Hook variables |
180 | ||
c2792945 | 181 | (defcustom dired-load-hook nil |
ee680b2b | 182 | "Run after loading Dired. |
c2792945 EZ |
183 | You can customize key bindings or load extensions with this." |
184 | :group 'dired | |
185 | :type 'hook) | |
492d2437 | 186 | |
c2792945 | 187 | (defcustom dired-mode-hook nil |
ee680b2b | 188 | "Run at the very end of `dired-mode'." |
c2792945 EZ |
189 | :group 'dired |
190 | :type 'hook) | |
492d2437 | 191 | |
c2792945 EZ |
192 | (defcustom dired-before-readin-hook nil |
193 | "This hook is run before a dired buffer is read in (created or reverted)." | |
194 | :group 'dired | |
195 | :type 'hook) | |
492d2437 | 196 | |
c2792945 | 197 | (defcustom dired-after-readin-hook nil |
492d2437 RS |
198 | "Hook run after each time a file or directory is read by Dired. |
199 | After each listing of a file or directory, this hook is run | |
c2792945 EZ |
200 | with the buffer narrowed to the listing." |
201 | :group 'dired | |
202 | :type 'hook) | |
492d2437 RS |
203 | ;; Note this can't simply be run inside function `dired-ls' as the hook |
204 | ;; functions probably depend on the dired-subdir-alist to be OK. | |
205 | ||
1ed8284d RS |
206 | (defcustom dired-dnd-protocol-alist |
207 | '(("^file:///" . dired-dnd-handle-local-file) | |
208 | ("^file://" . dired-dnd-handle-file) | |
209 | ("^file:" . dired-dnd-handle-local-file)) | |
210 | "The functions to call when a drop in `dired-mode' is made. | |
211 | See `dnd-protocol-alist' for more information. When nil, behave | |
3db7bf54 | 212 | as in other buffers. Changing this option is effective only for |
f7a8a965 | 213 | new dired buffers." |
1ed8284d RS |
214 | :type '(choice (repeat (cons (regexp) (function))) |
215 | (const :tag "Behave as in other buffers" nil)) | |
216 | :version "22.1" | |
217 | :group 'dired) | |
218 | ||
2b2059d8 | 219 | ;; Internal variables |
492d2437 RS |
220 | |
221 | (defvar dired-marker-char ?* ; the answer is 42 | |
222 | ;; so that you can write things like | |
223 | ;; (let ((dired-marker-char ?X)) | |
224 | ;; ;; great code using X markers ... | |
225 | ;; ) | |
226 | ;; For example, commands operating on two sets of files, A and B. | |
227 | ;; Or marking files with digits 0-9. This could implicate | |
228 | ;; concentric sets or an order for the marked files. | |
229 | ;; The code depends on dynamic scoping on the marker char. | |
230 | "In Dired, the current mark character. | |
884b7f9d | 231 | This is what the do-commands look for, and what the mark-commands store.") |
492d2437 RS |
232 | |
233 | (defvar dired-del-marker ?D | |
234 | "Character used to flag files for deletion.") | |
235 | ||
ee680b2b | 236 | (defvar dired-shrink-to-fit t |
ab67260b RS |
237 | ;; I see no reason ever to make this nil -- rms. |
238 | ;; (> baud-rate search-slow-speed) | |
492d2437 RS |
239 | "Non-nil means Dired shrinks the display buffer to fit the marked files.") |
240 | ||
ab67260b RS |
241 | (defvar dired-file-version-alist) |
242 | ||
e956c609 | 243 | ;;;###autoload |
492d2437 | 244 | (defvar dired-directory nil |
ee680b2b | 245 | "The directory name or wildcard spec that this dired directory lists. |
8d23c16b | 246 | Local to each dired buffer. May be a list, in which case the car is the |
0b7bc76f RS |
247 | directory name and the cdr is the list of files to mention. |
248 | The directory name must be absolute, but need not be fully expanded.") | |
492d2437 | 249 | |
9903d828 | 250 | ;; Beware of "-l;reboot" etc. See bug#3230. |
75da6eb9 | 251 | (defun dired-safe-switches-p (switches) |
9903d828 GM |
252 | "Return non-nil if string SWITCHES does not look risky for dired." |
253 | (or (not switches) | |
254 | (and (stringp switches) | |
255 | (< (length switches) 100) ; arbitrary | |
256 | (string-match "\\` *-[- [:alnum:]]+\\'" switches)))) | |
75da6eb9 | 257 | |
492d2437 RS |
258 | (defvar dired-actual-switches nil |
259 | "The value of `dired-listing-switches' used to make this buffer's text.") | |
260 | ||
9903d828 GM |
261 | (put 'dired-actual-switches 'safe-local-variable 'dired-safe-switches-p) |
262 | ||
492d2437 RS |
263 | (defvar dired-re-inode-size "[0-9 \t]*" |
264 | "Regexp for optional initial inode and file size as made by `ls -i -s'.") | |
265 | ||
266 | ;; These regexps must be tested at beginning-of-line, but are also | |
267 | ;; used to search for next matches, so neither omitting "^" nor | |
268 | ;; replacing "^" by "\n" (to make it slightly faster) will work. | |
269 | ||
270 | (defvar dired-re-mark "^[^ \n]") | |
271 | ;; "Regexp matching a marked line. | |
272 | ;; Important: the match ends just after the marker." | |
273 | (defvar dired-re-maybe-mark "^. ") | |
312a9e03 EZ |
274 | ;; The [^:] part after "d" and "l" is to avoid confusion with the |
275 | ;; DOS/Windows-style drive letters in directory names, like in "d:/foo". | |
276 | (defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d[^:]")) | |
277 | (defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l[^:]")) | |
492d2437 RS |
278 | (defvar dired-re-exe;; match ls permission string of an executable file |
279 | (mapconcat (function | |
280 | (lambda (x) | |
281 | (concat dired-re-maybe-mark dired-re-inode-size x))) | |
282 | '("-[-r][-w][xs][-r][-w].[-r][-w]." | |
283 | "-[-r][-w].[-r][-w][xs][-r][-w]." | |
284 | "-[-r][-w].[-r][-w].[-r][-w][xst]") | |
285 | "\\|")) | |
e6cecd2b | 286 | (defvar dired-re-perms "[-bcdlps][-r][-w].[-r][-w].[-r][-w].") |
147e214c | 287 | (defvar dired-re-dot "^.* \\.\\.?/?$") |
492d2437 | 288 | |
5553077c | 289 | ;; The subdirectory names in the next two lists are expanded. |
492d2437 RS |
290 | (defvar dired-subdir-alist nil |
291 | "Association list of subdirectories and their buffer positions. | |
292 | Each subdirectory has an element: (DIRNAME . STARTMARKER). | |
7b2469ae RS |
293 | The order of elements is the reverse of the order in the buffer. |
294 | In simple cases, this list contains one element.") | |
492d2437 | 295 | |
5553077c LT |
296 | (defvar dired-switches-alist nil |
297 | "Keeps track of which switches to use for inserted subdirectories. | |
298 | This is an alist of the form (SUBDIR . SWITCHES).") | |
299 | (make-variable-buffer-local 'dired-switches-alist) | |
300 | ||
5afea280 RS |
301 | (defvaralias 'dired-move-to-filename-regexp |
302 | 'directory-listing-before-filename-regexp) | |
303 | ||
cdf156a9 | 304 | (defvar dired-subdir-regexp "^. \\([^\n\r]+\\)\\(:\\)[\n\r]" |
492d2437 RS |
305 | "Regexp matching a maybe hidden subdirectory line in `ls -lR' output. |
306 | Subexpression 1 is the subdirectory proper, no trailing colon. | |
307 | The match starts at the beginning of the line and ends after the end | |
308 | of the line (\\n or \\r). | |
309 | Subexpression 2 must end right before the \\n or \\r.") | |
310 | ||
0b0e6651 | 311 | (defgroup dired-faces nil |
ee680b2b | 312 | "Faces used by Dired." |
0b0e6651 JL |
313 | :group 'dired |
314 | :group 'faces) | |
315 | ||
316 | (defface dired-header | |
317 | '((t (:inherit font-lock-type-face))) | |
318 | "Face used for directory headers." | |
319 | :group 'dired-faces | |
bf247b6e | 320 | :version "22.1") |
0b0e6651 JL |
321 | (defvar dired-header-face 'dired-header |
322 | "Face name used for directory headers.") | |
323 | ||
324 | (defface dired-mark | |
325 | '((t (:inherit font-lock-constant-face))) | |
326 | "Face used for dired marks." | |
327 | :group 'dired-faces | |
bf247b6e | 328 | :version "22.1") |
0b0e6651 JL |
329 | (defvar dired-mark-face 'dired-mark |
330 | "Face name used for dired marks.") | |
331 | ||
332 | (defface dired-marked | |
333 | '((t (:inherit font-lock-warning-face))) | |
334 | "Face used for marked files." | |
335 | :group 'dired-faces | |
bf247b6e | 336 | :version "22.1") |
0b0e6651 JL |
337 | (defvar dired-marked-face 'dired-marked |
338 | "Face name used for marked files.") | |
339 | ||
340 | (defface dired-flagged | |
341 | '((t (:inherit font-lock-warning-face))) | |
342 | "Face used for flagged files." | |
343 | :group 'dired-faces | |
bf247b6e | 344 | :version "22.1") |
0b0e6651 JL |
345 | (defvar dired-flagged-face 'dired-flagged |
346 | "Face name used for flagged files.") | |
347 | ||
348 | (defface dired-warning | |
0f0c86fc MR |
349 | ;; Inherit from font-lock-warning-face since with min-colors 8 |
350 | ;; font-lock-comment-face is not colored any more. | |
351 | '((t (:inherit font-lock-warning-face))) | |
0b0e6651 JL |
352 | "Face used to highlight a part of a buffer that needs user attention." |
353 | :group 'dired-faces | |
bf247b6e | 354 | :version "22.1") |
0b0e6651 JL |
355 | (defvar dired-warning-face 'dired-warning |
356 | "Face name used for a part of a buffer that needs user attention.") | |
357 | ||
0199e765 | 358 | (defface dired-perm-write |
38838c80 | 359 | '((((type w32 pc)) :inherit default) ;; These default to rw-rw-rw. |
e1dab605 JL |
360 | ;; Inherit from font-lock-comment-delimiter-face since with min-colors 8 |
361 | ;; font-lock-comment-face is not colored any more. | |
0199e765 | 362 | (t (:inherit font-lock-comment-delimiter-face))) |
38838c80 EZ |
363 | "Face used to highlight permissions of group- and world-writable files." |
364 | :group 'dired-faces | |
365 | :version "22.2") | |
0199e765 | 366 | (defvar dired-perm-write-face 'dired-perm-write |
38838c80 EZ |
367 | "Face name used for permissions of group- and world-writable files.") |
368 | ||
0b0e6651 JL |
369 | (defface dired-directory |
370 | '((t (:inherit font-lock-function-name-face))) | |
371 | "Face used for subdirectories." | |
372 | :group 'dired-faces | |
bf247b6e | 373 | :version "22.1") |
0b0e6651 JL |
374 | (defvar dired-directory-face 'dired-directory |
375 | "Face name used for subdirectories.") | |
376 | ||
377 | (defface dired-symlink | |
378 | '((t (:inherit font-lock-keyword-face))) | |
379 | "Face used for symbolic links." | |
380 | :group 'dired-faces | |
bf247b6e | 381 | :version "22.1") |
0b0e6651 JL |
382 | (defvar dired-symlink-face 'dired-symlink |
383 | "Face name used for symbolic links.") | |
384 | ||
385 | (defface dired-ignored | |
cf78a467 | 386 | '((t (:inherit shadow))) |
0b0e6651 JL |
387 | "Face used for files suffixed with `completion-ignored-extensions'." |
388 | :group 'dired-faces | |
bf247b6e | 389 | :version "22.1") |
0b0e6651 JL |
390 | (defvar dired-ignored-face 'dired-ignored |
391 | "Face name used for files suffixed with `completion-ignored-extensions'.") | |
392 | ||
2439570d SM |
393 | (defvar dired-font-lock-keywords |
394 | (list | |
0b0e6651 JL |
395 | ;; |
396 | ;; Dired marks. | |
397 | (list dired-re-mark '(0 dired-mark-face)) | |
2439570d SM |
398 | ;; |
399 | ;; We make heavy use of MATCH-ANCHORED, since the regexps don't identify the | |
400 | ;; file name itself. We search for Dired defined regexps, and then use the | |
401 | ;; Dired defined function `dired-move-to-filename' before searching for the | |
402 | ;; simple regexp ".+". It is that regexp which matches the file name. | |
403 | ;; | |
0b0e6651 JL |
404 | ;; Marked files. |
405 | (list (concat "^[" (char-to-string dired-marker-char) "]") | |
406 | '(".+" (dired-move-to-filename) nil (0 dired-marked-face))) | |
407 | ;; | |
408 | ;; Flagged files. | |
409 | (list (concat "^[" (char-to-string dired-del-marker) "]") | |
410 | '(".+" (dired-move-to-filename) nil (0 dired-flagged-face))) | |
4fdf389a RS |
411 | ;; People who are paranoid about security would consider this more |
412 | ;; important than other things such as whether it is a directory. | |
413 | ;; But we don't want to encourage paranoia, so our default | |
414 | ;; should be what's most useful for non-paranoids. -- rms. | |
415 | ;;; ;; | |
416 | ;;; ;; Files that are group or world writable. | |
417 | ;;; (list (concat dired-re-maybe-mark dired-re-inode-size | |
418 | ;;; "\\([-d]\\(....w....\\|.......w.\\)\\)") | |
0b0e6651 JL |
419 | ;;; '(1 dired-warning-face) |
420 | ;;; '(".+" (dired-move-to-filename) nil (0 dired-warning-face))) | |
d7aed37c SM |
421 | ;; However, we don't need to highlight the file name, only the |
422 | ;; permissions, to win generally. -- fx. | |
423 | ;; Fixme: we could also put text properties on the permission | |
424 | ;; fields with keymaps to frob the permissions, somewhat a la XEmacs. | |
425 | (list (concat dired-re-maybe-mark dired-re-inode-size | |
f16bcc9a | 426 | "[-d]....\\(w\\)....") ; group writable |
0199e765 | 427 | '(1 dired-perm-write-face)) |
d7aed37c | 428 | (list (concat dired-re-maybe-mark dired-re-inode-size |
f16bcc9a | 429 | "[-d].......\\(w\\).") ; world writable |
0199e765 | 430 | '(1 dired-perm-write-face)) |
2439570d SM |
431 | ;; |
432 | ;; Subdirectories. | |
433 | (list dired-re-dir | |
0b0e6651 | 434 | '(".+" (dired-move-to-filename) nil (0 dired-directory-face))) |
2439570d SM |
435 | ;; |
436 | ;; Symbolic links. | |
c60ee5e7 | 437 | (list dired-re-sym |
0b0e6651 | 438 | '(".+" (dired-move-to-filename) nil (0 dired-symlink-face))) |
2439570d SM |
439 | ;; |
440 | ;; Files suffixed with `completion-ignored-extensions'. | |
441 | '(eval . | |
2b2059d8 SM |
442 | ;; It is quicker to first find just an extension, then go back to the |
443 | ;; start of that file name. So we do this complex MATCH-ANCHORED form. | |
444 | (list (concat "\\(" (regexp-opt completion-ignored-extensions) "\\|#\\)$") | |
2225a196 RS |
445 | '(".+" (dired-move-to-filename) nil (0 dired-ignored-face)))) |
446 | ;; | |
447 | ;; Files suffixed with `completion-ignored-extensions' | |
448 | ;; plus a character put in by -F. | |
449 | '(eval . | |
450 | (list (concat "\\(" (regexp-opt completion-ignored-extensions) | |
451 | "\\|#\\)[*=|]$") | |
452 | '(".+" (progn | |
453 | (end-of-line) | |
454 | ;; If the last character is not part of the filename, | |
455 | ;; move back to the start of the filename | |
456 | ;; so it can be fontified. | |
457 | ;; Otherwise, leave point at the end of the line; | |
458 | ;; that way, nothing is fontified. | |
459 | (unless (get-text-property (1- (point)) 'mouse-face) | |
460 | (dired-move-to-filename))) | |
461 | nil (0 dired-ignored-face)))) | |
a5dad58a AS |
462 | ;; |
463 | ;; Explicitly put the default face on file names ending in a colon to | |
464 | ;; avoid fontifying them as directory header. | |
465 | (list (concat dired-re-maybe-mark dired-re-inode-size dired-re-perms ".*:$") | |
fc7da30b | 466 | '(".+" (dired-move-to-filename) nil (0 'default))) |
a5dad58a AS |
467 | ;; |
468 | ;; Directory headers. | |
469 | (list dired-subdir-regexp '(1 dired-header-face)) | |
2225a196 | 470 | ) |
2439570d | 471 | "Additional expressions to highlight in Dired mode.") |
65536d1d RS |
472 | |
473 | (defvar dnd-protocol-alist) | |
83fadedf | 474 | \f |
492d2437 RS |
475 | ;;; Macros must be defined before they are used, for the byte compiler. |
476 | ||
492d2437 | 477 | (defmacro dired-mark-if (predicate msg) |
d7aed37c SM |
478 | "Mark all files for which PREDICATE evals to non-nil. |
479 | PREDICATE is evaluated on each line, with point at beginning of line. | |
480 | MSG is a noun phrase for the type of files being marked. | |
481 | It should end with a noun that can be pluralized by adding `s'. | |
482 | Return value is the number of files marked, or nil if none were marked." | |
f836b98e | 483 | `(let ((inhibit-read-only t) count) |
8a946354 SS |
484 | (save-excursion |
485 | (setq count 0) | |
e17d05e2 LMI |
486 | (when ,msg |
487 | (message "%s %ss%s..." | |
488 | (cond ((eq dired-marker-char ?\040) "Unmarking") | |
489 | ((eq dired-del-marker dired-marker-char) | |
490 | "Flagging") | |
491 | (t "Marking")) | |
492 | ,msg | |
493 | (if (eq dired-del-marker dired-marker-char) | |
494 | " for deletion" | |
495 | ""))) | |
8a946354 SS |
496 | (goto-char (point-min)) |
497 | (while (not (eobp)) | |
498 | (if ,predicate | |
499 | (progn | |
500 | (delete-char 1) | |
501 | (insert dired-marker-char) | |
502 | (setq count (1+ count)))) | |
503 | (forward-line 1)) | |
504 | (if ,msg (message "%s %s%s %s%s." | |
505 | count | |
506 | ,msg | |
507 | (dired-plural-s count) | |
508 | (if (eq dired-marker-char ?\040) "un" "") | |
509 | (if (eq dired-marker-char dired-del-marker) | |
510 | "flagged" "marked")))) | |
511 | (and (> count 0) count))) | |
492d2437 | 512 | |
09b092ad RS |
513 | (defmacro dired-map-over-marks (body arg &optional show-progress |
514 | distinguish-one-marked) | |
91947235 | 515 | "Eval BODY with point on each marked line. Return a list of BODY's results. |
1ca0da0e LMI |
516 | If no marked file could be found, execute BODY on the current |
517 | line. ARG, if non-nil, specifies the files to use instead of the | |
518 | marked files. | |
519 | ||
520 | If ARG is an integer, use the next ARG (or previous -ARG, if | |
521 | ARG<0) files. In that case, point is dragged along. This is so | |
522 | that commands on the next ARG (instead of the marked) files can | |
523 | be chained easily. | |
524 | For any other non-nil value of ARG, use the current file. | |
525 | ||
91947235 | 526 | If optional third arg SHOW-PROGRESS evaluates to non-nil, |
1ca0da0e LMI |
527 | redisplay the dired buffer after each file is processed. |
528 | ||
529 | No guarantee is made about the position on the marked line. BODY | |
530 | must ensure this itself if it depends on this. | |
531 | ||
532 | Search starts at the beginning of the buffer, thus the car of the | |
533 | list corresponds to the line nearest to the buffer's bottom. | |
534 | This is also true for (positive and negative) integer values of | |
535 | ARG. | |
536 | ||
09b092ad RS |
537 | BODY should not be too long as it is expanded four times. |
538 | ||
1ca0da0e LMI |
539 | If DISTINGUISH-ONE-MARKED is non-nil, then if we find just one |
540 | marked file, return (t FILENAME) instead of (FILENAME)." | |
91947235 SM |
541 | ;; |
542 | ;;Warning: BODY must not add new lines before point - this may cause an | |
543 | ;;endless loop. | |
544 | ;;This warning should not apply any longer, sk 2-Sep-1991 14:10. | |
545 | `(prog1 | |
f836b98e | 546 | (let ((inhibit-read-only t) case-fold-search found results) |
91947235 SM |
547 | (if ,arg |
548 | (if (integerp ,arg) | |
549 | (progn ;; no save-excursion, want to move point. | |
550 | (dired-repeat-over-lines | |
551 | ,arg | |
552 | (function (lambda () | |
553 | (if ,show-progress (sit-for 0)) | |
554 | (setq results (cons ,body results))))) | |
555 | (if (< ,arg 0) | |
556 | (nreverse results) | |
557 | results)) | |
558 | ;; non-nil, non-integer ARG means use current file: | |
559 | (list ,body)) | |
560 | (let ((regexp (dired-marker-regexp)) next-position) | |
561 | (save-excursion | |
562 | (goto-char (point-min)) | |
563 | ;; remember position of next marked file before BODY | |
564 | ;; can insert lines before the just found file, | |
565 | ;; confusing us by finding the same marked file again | |
566 | ;; and again and... | |
567 | (setq next-position (and (re-search-forward regexp nil t) | |
568 | (point-marker)) | |
569 | found (not (null next-position))) | |
570 | (while next-position | |
571 | (goto-char next-position) | |
572 | (if ,show-progress (sit-for 0)) | |
573 | (setq results (cons ,body results)) | |
574 | ;; move after last match | |
575 | (goto-char next-position) | |
576 | (forward-line 1) | |
577 | (set-marker next-position nil) | |
492d2437 | 578 | (setq next-position (and (re-search-forward regexp nil t) |
91947235 | 579 | (point-marker))))) |
09b092ad RS |
580 | (if (and ,distinguish-one-marked (= (length results) 1)) |
581 | (setq results (cons t results))) | |
91947235 SM |
582 | (if found |
583 | results | |
584 | (list ,body))))) | |
585 | ;; save-excursion loses, again | |
586 | (dired-move-to-filename))) | |
492d2437 | 587 | |
09b092ad | 588 | (defun dired-get-marked-files (&optional localp arg filter distinguish-one-marked) |
492d2437 RS |
589 | "Return the marked files' names as list of strings. |
590 | The list is in the same order as the buffer, that is, the car is the | |
591 | first marked file. | |
1312bfc6 | 592 | Values returned are normally absolute file names. |
492d2437 | 593 | Optional arg LOCALP as in `dired-get-filename'. |
7ada496c CY |
594 | Optional second argument ARG, if non-nil, specifies files near |
595 | point instead of marked files. It usually comes from the prefix | |
596 | argument. | |
597 | If ARG is an integer, use the next ARG files. | |
598 | Any other non-nil value means to use the current file instead. | |
1312bfc6 | 599 | Optional third argument FILTER, if non-nil, is a function to select |
09b092ad RS |
600 | some of the files--those for which (funcall FILTER FILENAME) is non-nil. |
601 | ||
602 | If DISTINGUISH-ONE-MARKED is non-nil, then if we find just one marked file, | |
603 | return (t FILENAME) instead of (FILENAME). | |
604 | Don't use that together with FILTER." | |
605 | (let* ((all-of-them | |
606 | (save-excursion | |
607 | (dired-map-over-marks | |
608 | (dired-get-filename localp) | |
609 | arg nil distinguish-one-marked))) | |
610 | result) | |
1312bfc6 | 611 | (if (not filter) |
09b092ad RS |
612 | (if (and distinguish-one-marked (eq (car all-of-them) t)) |
613 | all-of-them | |
614 | (nreverse all-of-them)) | |
1312bfc6 RS |
615 | (dolist (file all-of-them) |
616 | (if (funcall filter file) | |
617 | (push file result))) | |
618 | result))) | |
83fadedf | 619 | \f |
492d2437 RS |
620 | ;; The dired command |
621 | ||
622 | (defun dired-read-dir-and-switches (str) | |
623 | ;; For use in interactive. | |
6f6c43ff CY |
624 | (reverse (list |
625 | (if current-prefix-arg | |
626 | (read-string "Dired listing switches: " | |
627 | dired-listing-switches)) | |
7e27ce9c AL |
628 | ;; If a dialog is used, call `read-directory-name' so the |
629 | ;; dialog code knows we want directories. Some dialogs | |
630 | ;; can only select directories or files when popped up, | |
631 | ;; not both. If no dialog is used, call `read-file-name' | |
632 | ;; because the user may want completion of file names for | |
633 | ;; use in a wildcard pattern. | |
6f6c43ff CY |
634 | (if (next-read-file-uses-dialog-p) |
635 | (read-directory-name (format "Dired %s(directory): " str) | |
636 | nil default-directory nil) | |
7d371eac JL |
637 | (read-file-name (format "Dired %s(directory): " str) |
638 | nil default-directory nil))))) | |
6f6c43ff CY |
639 | |
640 | ;; We want to switch to a more sophisticated version of | |
641 | ;; dired-read-dir-and-switches like the following, if there is a way | |
642 | ;; to make it more intuitive. See bug#1285. | |
643 | ||
644 | ;; (defun dired-read-dir-and-switches (str) | |
645 | ;; ;; For use in interactive. | |
646 | ;; (reverse | |
647 | ;; (list | |
648 | ;; (if current-prefix-arg | |
649 | ;; (read-string "Dired listing switches: " | |
650 | ;; dired-listing-switches)) | |
651 | ;; ;; If a dialog is about to be used, call read-directory-name so | |
652 | ;; ;; the dialog code knows we want directories. Some dialogs can | |
653 | ;; ;; only select directories or files when popped up, not both. | |
654 | ;; (if (next-read-file-uses-dialog-p) | |
655 | ;; (read-directory-name (format "Dired %s(directory): " str) | |
656 | ;; nil default-directory nil) | |
657 | ;; (let ((cie ())) | |
658 | ;; (dolist (ext completion-ignored-extensions) | |
659 | ;; (if (eq ?/ (aref ext (1- (length ext)))) (push ext cie))) | |
660 | ;; (setq cie (concat (regexp-opt cie "\\(?:") "\\'")) | |
661 | ;; (lexical-let* ((default (and buffer-file-name | |
662 | ;; (abbreviate-file-name buffer-file-name))) | |
663 | ;; (cie cie) | |
664 | ;; (completion-table | |
665 | ;; ;; We need a mix of read-file-name and | |
666 | ;; ;; read-directory-name so that completion to directories | |
667 | ;; ;; is preferred, but if the user wants to enter a global | |
668 | ;; ;; pattern, he can still use completion on filenames to | |
669 | ;; ;; help him write the pattern. | |
670 | ;; ;; Essentially, we want to use | |
671 | ;; ;; (completion-table-with-predicate | |
672 | ;; ;; 'read-file-name-internal 'file-directory-p nil) | |
673 | ;; ;; but that doesn't work because read-file-name-internal | |
674 | ;; ;; does not obey its `predicate' argument. | |
675 | ;; (completion-table-in-turn | |
676 | ;; (lambda (str pred action) | |
677 | ;; (let ((read-file-name-predicate | |
678 | ;; (lambda (f) | |
679 | ;; (and (not (member f '("./" "../"))) | |
680 | ;; ;; Hack! Faster than file-directory-p! | |
681 | ;; (eq (aref f (1- (length f))) ?/) | |
682 | ;; (not (string-match cie f)))))) | |
683 | ;; (complete-with-action | |
684 | ;; action 'read-file-name-internal str nil))) | |
685 | ;; 'read-file-name-internal))) | |
686 | ;; (minibuffer-with-setup-hook | |
687 | ;; (lambda () | |
688 | ;; (setq minibuffer-default default) | |
689 | ;; (setq minibuffer-completion-table completion-table)) | |
690 | ;; (read-file-name (format "Dired %s(directory): " str) | |
691 | ;; nil default-directory nil)))))))) | |
84fc2cfa | 692 | |
7d371eac JL |
693 | (defun dired-file-name-at-point () |
694 | "Try to get a file name at point in the current dired buffer. | |
695 | This hook is inteneded to be put in `file-name-at-point-functions'." | |
696 | (let ((filename (dired-get-filename nil t))) | |
697 | (when filename | |
698 | (if (file-directory-p filename) | |
699 | (file-name-as-directory (abbreviate-file-name filename)) | |
700 | (abbreviate-file-name filename))))) | |
701 | ||
492d2437 | 702 | ;;;###autoload (define-key ctl-x-map "d" 'dired) |
84fc2cfa | 703 | ;;;###autoload |
492d2437 | 704 | (defun dired (dirname &optional switches) |
84fc2cfa | 705 | "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it. |
492d2437 RS |
706 | Optional second argument SWITCHES specifies the `ls' options used. |
707 | \(Interactively, use a prefix argument to be able to specify SWITCHES.) | |
708 | Dired displays a list of files in DIRNAME (which may also have | |
8d23c16b | 709 | shell wildcards appended to select certain files). If DIRNAME is a cons, |
1015daba | 710 | its first element is taken as the directory name and the rest as an explicit |
8d23c16b | 711 | list of files to make directory entries for. |
52041219 | 712 | \\<dired-mode-map>\ |
8d23c16b ER |
713 | You can flag files for deletion with \\[dired-flag-file-deletion] and then |
714 | delete them by typing \\[dired-do-flagged-delete]. | |
ee680b2b | 715 | Type \\[describe-mode] after entering Dired for more info. |
84fc2cfa | 716 | |
492d2437 RS |
717 | If DIRNAME is already in a dired buffer, that buffer is used without refresh." |
718 | ;; Cannot use (interactive "D") because of wildcards. | |
719 | (interactive (dired-read-dir-and-switches "")) | |
720 | (switch-to-buffer (dired-noselect dirname switches))) | |
721 | ||
722 | ;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window) | |
84fc2cfa | 723 | ;;;###autoload |
492d2437 | 724 | (defun dired-other-window (dirname &optional switches) |
84fc2cfa | 725 | "\"Edit\" directory DIRNAME. Like `dired' but selects in another window." |
492d2437 RS |
726 | (interactive (dired-read-dir-and-switches "in other window ")) |
727 | (switch-to-buffer-other-window (dired-noselect dirname switches))) | |
84fc2cfa | 728 | |
ec03e49c RS |
729 | ;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame) |
730 | ;;;###autoload | |
731 | (defun dired-other-frame (dirname &optional switches) | |
732 | "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame." | |
733 | (interactive (dired-read-dir-and-switches "in other frame ")) | |
734 | (switch-to-buffer-other-frame (dired-noselect dirname switches))) | |
735 | ||
84fc2cfa | 736 | ;;;###autoload |
8d23c16b | 737 | (defun dired-noselect (dir-or-list &optional switches) |
84fc2cfa | 738 | "Like `dired' but returns the dired buffer as value, does not select it." |
8d23c16b | 739 | (or dir-or-list (setq dir-or-list default-directory)) |
492d2437 RS |
740 | ;; This loses the distinction between "/foo/*/" and "/foo/*" that |
741 | ;; some shells make: | |
54df7d9a | 742 | (let (dirname initially-was-dirname) |
8d23c16b ER |
743 | (if (consp dir-or-list) |
744 | (setq dirname (car dir-or-list)) | |
745 | (setq dirname dir-or-list)) | |
54df7d9a RS |
746 | (setq initially-was-dirname |
747 | (string= (file-name-as-directory dirname) dirname)) | |
370b6149 RS |
748 | (setq dirname (abbreviate-file-name |
749 | (expand-file-name (directory-file-name dirname)))) | |
cb2e51f8 RS |
750 | (if find-file-visit-truename |
751 | (setq dirname (file-truename dirname))) | |
54df7d9a RS |
752 | ;; If the argument was syntactically a directory name not a file name, |
753 | ;; or if it happens to name a file that is a directory, | |
754 | ;; convert it syntactically to a directory name. | |
755 | ;; The reason for checking initially-was-dirname | |
756 | ;; and not just file-directory-p | |
757 | ;; is that file-directory-p is slow over ftp. | |
758 | (if (or initially-was-dirname (file-directory-p dirname)) | |
759 | (setq dirname (file-name-as-directory dirname))) | |
8d23c16b ER |
760 | (if (consp dir-or-list) |
761 | (setq dir-or-list (cons dirname (cdr dir-or-list))) | |
762 | (setq dir-or-list dirname)) | |
763 | (dired-internal-noselect dir-or-list switches))) | |
492d2437 | 764 | |
573e4d2d LT |
765 | ;; The following is an internal dired function. It returns non-nil if |
766 | ;; the directory visited by the current dired buffer has changed on | |
767 | ;; disk. DIRNAME should be the directory name of that directory. | |
768 | (defun dired-directory-changed-p (dirname) | |
769 | (not (let ((attributes (file-attributes dirname)) | |
e09cd94f LT |
770 | (modtime (visited-file-modtime))) |
771 | (or (eq modtime 0) | |
772 | (not (eq (car attributes) t)) | |
ca02a726 | 773 | (equal (nth 5 attributes) modtime))))) |
573e4d2d LT |
774 | |
775 | (defun dired-buffer-stale-p (&optional noconfirm) | |
776 | "Return non-nil if current dired buffer needs updating. | |
777 | If NOCONFIRM is non-nil, then this function always returns nil | |
778 | for a remote directory. This feature is used by Auto Revert Mode." | |
779 | (let ((dirname | |
780 | (if (consp dired-directory) (car dired-directory) dired-directory))) | |
781 | (and (stringp dirname) | |
782 | (not (when noconfirm (file-remote-p dirname))) | |
783 | (file-readable-p dirname) | |
85a3be15 MR |
784 | ;; Do not auto-revert when the dired buffer can be currently |
785 | ;; written by the user as in `wdired-mode'. | |
786 | buffer-read-only | |
573e4d2d LT |
787 | (dired-directory-changed-p dirname)))) |
788 | ||
2b3489a7 JL |
789 | (defcustom dired-auto-revert-buffer nil |
790 | "Automatically revert dired buffer on revisiting. | |
791 | If t, revisiting an existing dired buffer automatically reverts it. | |
792 | If its value is a function, call this function with the directory | |
793 | name as single argument and revert the buffer if it returns non-nil. | |
794 | Otherwise, a message offering to revert the changed dired buffer | |
795 | is displayed. | |
796 | Note that this is not the same as `auto-revert-mode' that | |
797 | periodically reverts at specified time intervals." | |
798 | :type '(choice | |
799 | (const :tag "Don't revert" nil) | |
800 | (const :tag "Always revert visited dired buffer" t) | |
801 | (const :tag "Revert changed dired buffer" dired-directory-changed-p) | |
802 | (function :tag "Predicate function")) | |
803 | :group 'dired | |
804 | :version "23.2") | |
805 | ||
6a4cd605 | 806 | (defun dired-internal-noselect (dir-or-list &optional switches mode) |
492d2437 RS |
807 | ;; If there is an existing dired buffer for DIRNAME, just leave |
808 | ;; buffer as it is (don't even call dired-revert). | |
809 | ;; This saves time especially for deep trees or with ange-ftp. | |
573e4d2d | 810 | ;; The user can type `g' easily, and it is more consistent with find-file. |
492d2437 RS |
811 | ;; But if SWITCHES are given they are probably different from the |
812 | ;; buffer's old value, so call dired-sort-other, which does | |
813 | ;; revert the buffer. | |
814 | ;; A pity we can't possibly do "Directory has changed - refresh? " | |
815 | ;; like find-file does. | |
6a4cd605 RS |
816 | ;; Optional argument MODE is passed to dired-find-buffer-nocreate, |
817 | ;; see there. | |
72f16325 SM |
818 | (let* ((old-buf (current-buffer)) |
819 | (dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list)) | |
820 | ;; Look for an existing buffer. | |
821 | (buffer (dired-find-buffer-nocreate dirname mode)) | |
822 | ;; Note that buffer already is in dired-mode, if found. | |
823 | (new-buffer-p (null buffer))) | |
492d2437 | 824 | (or buffer |
72f16325 | 825 | (setq buffer (create-file-buffer (directory-file-name dirname)))) |
492d2437 | 826 | (set-buffer buffer) |
573e4d2d LT |
827 | (if (not new-buffer-p) ; existing buffer ... |
828 | (cond (switches ; ... but new switches | |
6cfd24f9 | 829 | ;; file list may have changed |
0b7bc76f | 830 | (setq dired-directory dir-or-list) |
6cfd24f9 | 831 | ;; this calls dired-revert |
c60ee5e7 | 832 | (dired-sort-other switches)) |
2b3489a7 JL |
833 | ;; Always revert regardless of whether it has changed or not. |
834 | ((eq dired-auto-revert-buffer t) | |
835 | (revert-buffer)) | |
836 | ;; Revert when predicate function returns non-nil. | |
837 | ((functionp dired-auto-revert-buffer) | |
838 | (when (funcall dired-auto-revert-buffer dirname) | |
839 | (revert-buffer) | |
840 | (message "Changed directory automatically updated"))) | |
6cfd24f9 | 841 | ;; If directory has changed on disk, offer to revert. |
573e4d2d | 842 | ((when (dired-directory-changed-p dirname) |
952e8cb5 | 843 | (message "%s" |
212b2266 KH |
844 | (substitute-command-keys |
845 | "Directory has changed on disk; type \\[revert-buffer] to update Dired"))))) | |
492d2437 | 846 | ;; Else a new buffer |
847aabce | 847 | (setq default-directory |
e3cd4b53 RS |
848 | ;; We can do this unconditionally |
849 | ;; because dired-noselect ensures that the name | |
850 | ;; is passed in directory name syntax | |
851 | ;; if it was the name of a directory at all. | |
852 | (file-name-directory dirname)) | |
492d2437 | 853 | (or switches (setq switches dired-listing-switches)) |
6e5d9ec5 | 854 | (if mode (funcall mode) |
0b7bc76f | 855 | (dired-mode dir-or-list switches)) |
492d2437 RS |
856 | ;; default-directory and dired-actual-switches are set now |
857 | ;; (buffer-local), so we can call dired-readin: | |
858 | (let ((failed t)) | |
859 | (unwind-protect | |
0b7bc76f | 860 | (progn (dired-readin) |
492d2437 RS |
861 | (setq failed nil)) |
862 | ;; dired-readin can fail if parent directories are inaccessible. | |
863 | ;; Don't leave an empty buffer around in that case. | |
864 | (if failed (kill-buffer buffer)))) | |
492d2437 RS |
865 | (goto-char (point-min)) |
866 | (dired-initial-position dirname)) | |
867 | (set-buffer old-buf) | |
84fc2cfa ER |
868 | buffer)) |
869 | ||
c5753a5d GM |
870 | (defvar dired-buffers nil |
871 | ;; Enlarged by dired-advertise | |
872 | ;; Queried by function dired-buffers-for-dir. When this detects a | |
873 | ;; killed buffer, it is removed from this list. | |
874 | "Alist of expanded directories and their associated dired buffers.") | |
875 | ||
bea584fa GM |
876 | (defvar dired-find-subdir) |
877 | ||
878 | ;; FIXME add a doc-string, and document dired-x extensions. | |
6a4cd605 RS |
879 | (defun dired-find-buffer-nocreate (dirname &optional mode) |
880 | ;; This differs from dired-buffers-for-dir in that it does not consider | |
881 | ;; subdirs of default-directory and searches for the first match only. | |
882 | ;; Also, the major mode must be MODE. | |
bea584fa GM |
883 | (if (and (featurep 'dired-x) |
884 | dired-find-subdir | |
885 | ;; Don't try to find a wildcard as a subdirectory. | |
886 | (string-equal dirname (file-name-directory dirname))) | |
887 | (let* ((cur-buf (current-buffer)) | |
888 | (buffers (nreverse | |
889 | (dired-buffers-for-dir (expand-file-name dirname)))) | |
890 | (cur-buf-matches (and (memq cur-buf buffers) | |
891 | ;; Wildcards must match, too: | |
892 | (equal dired-directory dirname)))) | |
893 | ;; We don't want to switch to the same buffer--- | |
894 | (setq buffers (delq cur-buf buffers)) | |
895 | (or (car (sort buffers #'dired-buffer-more-recently-used-p)) | |
896 | ;; ---unless it's the only possibility: | |
897 | (and cur-buf-matches cur-buf))) | |
898 | ;; No dired-x, or dired-find-subdir nil. | |
899 | (setq dirname (expand-file-name dirname)) | |
900 | (let (found (blist dired-buffers)) ; was (buffer-list) | |
901 | (or mode (setq mode 'dired-mode)) | |
902 | (while blist | |
903 | (if (null (buffer-name (cdr (car blist)))) | |
904 | (setq blist (cdr blist)) | |
905 | (with-current-buffer (cdr (car blist)) | |
906 | (if (and (eq major-mode mode) | |
907 | dired-directory ;; nil during find-alternate-file | |
908 | (equal dirname | |
909 | (expand-file-name | |
910 | (if (consp dired-directory) | |
911 | (car dired-directory) | |
912 | dired-directory)))) | |
913 | (setq found (cdr (car blist)) | |
914 | blist nil) | |
915 | (setq blist (cdr blist)))))) | |
916 | found))) | |
492d2437 | 917 | |
83fadedf | 918 | \f |
492d2437 RS |
919 | ;; Read in a new dired buffer |
920 | ||
0b7bc76f | 921 | (defun dired-readin () |
d7aed37c | 922 | "Read in a new dired buffer. |
87ae59e3 | 923 | Differs from `dired-insert-subdir' in that it accepts |
d7aed37c SM |
924 | wildcards, erases the buffer, and builds the subdir-alist anew |
925 | \(including making it buffer-local and clearing it first)." | |
926 | ||
492d2437 RS |
927 | ;; default-directory and dired-actual-switches must be buffer-local |
928 | ;; and initialized by now. | |
a1037423 RS |
929 | (let (dirname |
930 | ;; This makes readin much much faster. | |
931 | ;; In particular, it prevents the font lock hook from running | |
932 | ;; until the directory is all read in. | |
933 | (inhibit-modification-hooks t)) | |
0b7bc76f RS |
934 | (if (consp dired-directory) |
935 | (setq dirname (car dired-directory)) | |
936 | (setq dirname dired-directory)) | |
8d23c16b | 937 | (setq dirname (expand-file-name dirname)) |
8d23c16b | 938 | (save-excursion |
0b7bc76f RS |
939 | ;; This hook which may want to modify dired-actual-switches |
940 | ;; based on dired-directory, e.g. with ange-ftp to a SysV host | |
941 | ;; where ls won't understand -Al switches. | |
942 | (run-hooks 'dired-before-readin-hook) | |
0b7bc76f RS |
943 | (if (consp buffer-undo-list) |
944 | (setq buffer-undo-list nil)) | |
1419c7fc RS |
945 | (make-local-variable 'file-name-coding-system) |
946 | (setq file-name-coding-system | |
947 | (or coding-system-for-read file-name-coding-system)) | |
f836b98e | 948 | (let ((inhibit-read-only t) |
0b7bc76f RS |
949 | ;; Don't make undo entries for readin. |
950 | (buffer-undo-list t)) | |
8d23c16b ER |
951 | (widen) |
952 | (erase-buffer) | |
0b7bc76f | 953 | (dired-readin-insert)) |
0b7bc76f | 954 | (goto-char (point-min)) |
8d23c16b ER |
955 | ;; Must first make alist buffer local and set it to nil because |
956 | ;; dired-build-subdir-alist will call dired-clear-alist first | |
957 | (set (make-local-variable 'dired-subdir-alist) nil) | |
8197b8bf | 958 | (dired-build-subdir-alist) |
fa562dd5 RS |
959 | (let ((attributes (file-attributes dirname))) |
960 | (if (eq (car attributes) t) | |
961 | (set-visited-file-modtime (nth 5 attributes)))) | |
0b7bc76f RS |
962 | (set-buffer-modified-p nil) |
963 | ;; No need to narrow since the whole buffer contains just | |
964 | ;; dired-readin's output, nothing else. The hook can | |
965 | ;; successfully use dired functions (e.g. dired-get-filename) | |
966 | ;; as the subdir-alist has been built in dired-readin. | |
967 | (run-hooks 'dired-after-readin-hook)))) | |
492d2437 RS |
968 | |
969 | ;; Subroutines of dired-readin | |
970 | ||
0b7bc76f RS |
971 | (defun dired-readin-insert () |
972 | ;; Insert listing for the specified dir (and maybe file list) | |
973 | ;; already in dired-directory, assuming a clean buffer. | |
974 | (let (dir file-list) | |
975 | (if (consp dired-directory) | |
976 | (setq dir (car dired-directory) | |
977 | file-list (cdr dired-directory)) | |
978 | (setq dir dired-directory | |
979 | file-list nil)) | |
34cd7b3c | 980 | (setq dir (expand-file-name dir)) |
0b7bc76f RS |
981 | (if (and (equal "" (file-name-nondirectory dir)) |
982 | (not file-list)) | |
cf39fa9b | 983 | ;; If we are reading a whole single directory... |
0b7bc76f | 984 | (dired-insert-directory dir dired-actual-switches nil nil t) |
8d23c16b | 985 | (if (not (file-readable-p |
0b7bc76f RS |
986 | (directory-file-name (file-name-directory dir)))) |
987 | (error "Directory %s inaccessible or nonexistent" dir) | |
988 | ;; Else treat it as a wildcard spec | |
989 | ;; unless we have an explicit list of files. | |
990 | (dired-insert-directory dir dired-actual-switches | |
991 | file-list (not file-list) t))))) | |
992 | ||
a12c6dca SM |
993 | (defun dired-align-file (beg end) |
994 | "Align the fields of a file to the ones of surrounding lines. | |
995 | BEG..END is the line where the file info is located." | |
996 | ;; Some versions of ls try to adjust the size of each field so as to just | |
997 | ;; hold the largest element ("largest" in the current invocation, of | |
998 | ;; course). So when a single line is output, the size of each field is | |
999 | ;; just big enough for that one output. Thus when dired refreshes one | |
1000 | ;; line, the alignment if this line w.r.t the rest is messed up because | |
1001 | ;; the fields of that one line will generally be smaller. | |
1002 | ;; | |
9bc260cf MA |
1003 | ;; To work around this problem, we here add spaces to try and |
1004 | ;; re-align the fields as needed. Since this is purely aesthetic, | |
1005 | ;; it is of utmost importance that it doesn't mess up anything like | |
1006 | ;; `dired-move-to-filename'. To this end, we limit ourselves to | |
1007 | ;; adding spaces only, and to only add them at places where there | |
1008 | ;; was already at least one space. This way, as long as | |
1009 | ;; `directory-listing-before-filename-regexp' always matches spaces | |
1010 | ;; with "*" or "+", we know we haven't made anything worse. There | |
1011 | ;; is one spot where the exact number of spaces is important, which | |
1012 | ;; is just before the actual filename, so we refrain from adding | |
1013 | ;; spaces there (and within the filename as well, of course). | |
a12c6dca SM |
1014 | (save-excursion |
1015 | (let (file file-col other other-col) | |
b4dc7d98 | 1016 | ;; Check that there is indeed a file, and that there is anoter adjacent |
a12c6dca SM |
1017 | ;; file with which to align, and that additional spaces are needed to |
1018 | ;; align the filenames. | |
1019 | (when (and (setq file (progn (goto-char beg) | |
1020 | (dired-move-to-filename nil end))) | |
1021 | (setq file-col (current-column)) | |
1022 | (setq other | |
1023 | (or (and (goto-char beg) | |
1024 | (zerop (forward-line -1)) | |
1025 | (dired-move-to-filename)) | |
1026 | (and (goto-char beg) | |
1027 | (zerop (forward-line 1)) | |
1028 | (dired-move-to-filename)))) | |
1029 | (setq other-col (current-column)) | |
1030 | (/= file other) | |
1031 | ;; Make sure there is some work left to do. | |
1032 | (> other-col file-col)) | |
1033 | ;; If we've only looked at the line above, check to see if the line | |
1034 | ;; below exists as well and if so, align with the shorter one. | |
1035 | (when (and (< other file) | |
1036 | (goto-char beg) | |
1037 | (zerop (forward-line 1)) | |
1038 | (dired-move-to-filename)) | |
1039 | (let ((alt-col (current-column))) | |
1040 | (when (< alt-col other-col) | |
1041 | (setq other-col alt-col) | |
1042 | (setq other (point))))) | |
1043 | ;; Keep positions uptodate when we insert stuff. | |
1044 | (if (> other file) (setq other (copy-marker other))) | |
1045 | (setq file (copy-marker file)) | |
1046 | ;; Main loop. | |
1047 | (goto-char beg) | |
87ae59e3 | 1048 | (skip-chars-forward " ") ;Skip to the first field. |
a12c6dca | 1049 | (while (and (> other-col file-col) |
a12c6dca SM |
1050 | ;; Don't touch anything just before (and after) the |
1051 | ;; beginning of the filename. | |
1052 | (> file (point))) | |
1053 | ;; We're now just in front of a field, with a space behind us. | |
1054 | (let* ((curcol (current-column)) | |
1055 | ;; Nums are right-aligned. | |
1056 | (num-align (looking-at "[0-9]")) | |
1057 | ;; Let's look at the other line, in the same column: we | |
1058 | ;; should be either near the end of the previous field, or | |
1059 | ;; in the space between that field and the next. | |
1060 | ;; [ Of course, it's also possible that we're already within | |
1061 | ;; the next field or even past it, but that's unlikely since | |
1062 | ;; other-col > file-col. ] | |
1063 | ;; Let's find the distance to the alignment-point (either | |
1064 | ;; the beginning or the end of the next field, depending on | |
1065 | ;; whether this field is left or right aligned). | |
1066 | (align-pt-offset | |
1067 | (save-excursion | |
1068 | (goto-char other) | |
1069 | (move-to-column curcol) | |
1070 | (when (looking-at | |
1071 | (concat | |
ee680b2b | 1072 | (if (eq (char-before) ?\s) " *" "[^ ]* *") |
a12c6dca SM |
1073 | (if num-align "[0-9][^ ]*"))) |
1074 | (- (match-end 0) (match-beginning 0))))) | |
1075 | ;; Now, the number of spaces to insert is align-pt-offset | |
1076 | ;; minus the distance to the equivalent point on the | |
1077 | ;; current line. | |
1078 | (spaces | |
1079 | (if (not num-align) | |
1080 | align-pt-offset | |
1081 | (and align-pt-offset | |
1082 | (save-excursion | |
1083 | (skip-chars-forward "^ ") | |
1084 | (- align-pt-offset (- (current-column) curcol))))))) | |
1085 | (when (and spaces (> spaces 0)) | |
1086 | (setq file-col (+ spaces file-col)) | |
1087 | (if (> file-col other-col) | |
1088 | (setq spaces (- spaces (- file-col other-col)))) | |
1089 | (insert-char ?\s spaces) | |
1090 | ;; Let's just make really sure we did not mess up. | |
1091 | (unless (save-excursion | |
87ae59e3 | 1092 | (eq (dired-move-to-filename) (marker-position file))) |
a12c6dca | 1093 | ;; Damn! We messed up: let's revert the change. |
87ae59e3 SM |
1094 | (delete-char (- spaces))))) |
1095 | ;; Now skip to next field. | |
1096 | (skip-chars-forward "^ ") (skip-chars-forward " ")) | |
a12c6dca | 1097 | (set-marker file nil))))) |
87ae59e3 | 1098 | |
a12c6dca | 1099 | |
bc35ff32 EZ |
1100 | (defvar ls-lisp-use-insert-directory-program) |
1101 | ||
0b7bc76f RS |
1102 | (defun dired-insert-directory (dir switches &optional file-list wildcard hdr) |
1103 | "Insert a directory listing of DIR, Dired style. | |
1104 | Use SWITCHES to make the listings. | |
1105 | If FILE-LIST is non-nil, list only those files. | |
1106 | Otherwise, if WILDCARD is non-nil, expand wildcards; | |
1107 | in that case, DIR should be a file name that uses wildcards. | |
1108 | In other cases, DIR should be a directory name or a directory filename. | |
1109 | If HDR is non-nil, insert a header line with the directory name." | |
9888aa65 | 1110 | (let ((opoint (point)) |
02b5d79c | 1111 | (process-environment (copy-sequence process-environment)) |
9888aa65 | 1112 | end) |
bc35ff32 EZ |
1113 | (if (and |
1114 | ;; Don't try to invoke `ls' if we are on DOS/Windows where | |
1115 | ;; ls-lisp emulation is used, except if they want to use `ls' | |
1116 | ;; as indicated by `ls-lisp-use-insert-directory-program'. | |
1117 | (not (and (featurep 'ls-lisp) | |
1118 | (null ls-lisp-use-insert-directory-program))) | |
1119 | (or (if (eq dired-use-ls-dired 'unspecified) | |
1120 | ;; Check whether "ls --dired" gives exit code 0, and | |
1121 | ;; save the answer in `dired-use-ls-dired'. | |
1122 | (setq dired-use-ls-dired | |
1123 | (eq (call-process insert-directory-program nil nil nil "--dired") | |
1124 | 0)) | |
1125 | dired-use-ls-dired) | |
1126 | (file-remote-p dir))) | |
0b7bc76f | 1127 | (setq switches (concat "--dired " switches))) |
6492483f RS |
1128 | ;; We used to specify the C locale here, to force English month names; |
1129 | ;; but this should not be necessary any more, | |
9bc260cf | 1130 | ;; with the new value of `directory-listing-before-filename-regexp'. |
0b7bc76f RS |
1131 | (if file-list |
1132 | (dolist (f file-list) | |
a12c6dca SM |
1133 | (let ((beg (point))) |
1134 | (insert-directory f switches nil nil) | |
1135 | ;; Re-align fields, if necessary. | |
1136 | (dired-align-file beg (point)))) | |
0b7bc76f | 1137 | (insert-directory dir switches wildcard (not wildcard))) |
9888aa65 | 1138 | ;; Quote certain characters, unless ls quoted them for us. |
ad632492 SM |
1139 | (if (not (string-match "b" dired-actual-switches)) |
1140 | (save-excursion | |
1141 | (setq end (point-marker)) | |
1142 | (goto-char opoint) | |
1143 | (while (search-forward "\\" end t) | |
2bb27597 AS |
1144 | (replace-match (apply #'propertize |
1145 | "\\\\" | |
1146 | (text-properties-at (match-beginning 0))) | |
1147 | nil t)) | |
ad632492 SM |
1148 | (goto-char opoint) |
1149 | (while (search-forward "\^m" end t) | |
2bb27597 AS |
1150 | (replace-match (apply #'propertize |
1151 | "\\015" | |
1152 | (text-properties-at (match-beginning 0))) | |
1153 | nil t)) | |
ad632492 | 1154 | (set-marker end nil))) |
0b7bc76f RS |
1155 | (dired-insert-set-properties opoint (point)) |
1156 | ;; If we used --dired and it worked, the lines are already indented. | |
1157 | ;; Otherwise, indent them. | |
1158 | (unless (save-excursion | |
1ba6c0f2 | 1159 | (goto-char opoint) |
0b7bc76f RS |
1160 | (looking-at " ")) |
1161 | (let ((indent-tabs-mode nil)) | |
1162 | (indent-rigidly opoint (point) 2))) | |
1163 | ;; Insert text at the beginning to standardize things. | |
1164 | (save-excursion | |
1165 | (goto-char opoint) | |
a7f07c36 SM |
1166 | (if (and (or hdr wildcard) |
1167 | (not (and (looking-at "^ \\(.*\\):$") | |
1168 | (file-name-absolute-p (match-string 1))))) | |
0b7bc76f RS |
1169 | ;; Note that dired-build-subdir-alist will replace the name |
1170 | ;; by its expansion, so it does not matter whether what we insert | |
1171 | ;; here is fully expanded, but it should be absolute. | |
1172 | (insert " " (directory-file-name (file-name-directory dir)) ":\n")) | |
1173 | (when wildcard | |
1174 | ;; Insert "wildcard" line where "total" line would be for a full dir. | |
1175 | (insert " wildcard " (file-name-nondirectory dir) "\n"))))) | |
492d2437 | 1176 | |
cb88a3db | 1177 | (defun dired-insert-set-properties (beg end) |
7e9505c5 | 1178 | "Add various text properties to the lines in the region." |
cb88a3db RS |
1179 | (save-excursion |
1180 | (goto-char beg) | |
1181 | (while (< (point) end) | |
7da0e9b4 RS |
1182 | (condition-case nil |
1183 | (if (dired-move-to-filename) | |
ac2d0299 EZ |
1184 | (add-text-properties |
1185 | (point) | |
1186 | (save-excursion | |
1187 | (dired-move-to-end-of-filename) | |
1188 | (point)) | |
1b85bd12 | 1189 | '(mouse-face highlight |
eae2c85e | 1190 | dired-filename t |
ac2d0299 | 1191 | help-echo "mouse-2: visit this file in other window"))) |
7da0e9b4 | 1192 | (error nil)) |
cb88a3db | 1193 | (forward-line 1)))) |
83fadedf | 1194 | \f |
492d2437 RS |
1195 | ;; Reverting a dired buffer |
1196 | ||
d032d5e7 | 1197 | (defun dired-revert (&optional _arg _noconfirm) |
d7aed37c | 1198 | "Reread the dired buffer. |
ee680b2b | 1199 | Must also be called after `dired-actual-switches' have changed. |
d7aed37c | 1200 | Should not fail even on completely garbaged buffers. |
b14f16ad CY |
1201 | Preserves old cursor, marks/flags, hidden-p. |
1202 | ||
1203 | Dired sets `revert-buffer-function' to this function. The args | |
1204 | ARG and NOCONFIRM, passed from `revert-buffer', are ignored." | |
492d2437 | 1205 | (widen) ; just in case user narrowed |
7805cdbd | 1206 | (let ((modflag (buffer-modified-p)) |
05404988 | 1207 | (positions (dired-save-positions)) |
492d2437 RS |
1208 | (mark-alist nil) ; save marked files |
1209 | (hidden-subdirs (dired-remember-hidden)) | |
1210 | (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd | |
1211 | (case-fold-search nil) ; we check for upper case ls flags | |
f836b98e | 1212 | (inhibit-read-only t)) |
492d2437 RS |
1213 | (goto-char (point-min)) |
1214 | (setq mark-alist;; only after dired-remember-hidden since this unhides: | |
1215 | (dired-remember-marks (point-min) (point-max))) | |
1216 | ;; treat top level dir extra (it may contain wildcards) | |
d5b3979c MA |
1217 | (if (not (consp dired-directory)) |
1218 | (dired-uncache dired-directory) | |
1219 | (dired-uncache (car dired-directory)) | |
1220 | (dolist (dir (cdr dired-directory)) | |
1221 | (if (file-name-absolute-p dir) | |
1222 | (dired-uncache dir)))) | |
6a80f50e | 1223 | ;; Run dired-after-readin-hook just once, below. |
492d2437 | 1224 | (let ((dired-after-readin-hook nil)) |
6a80f50e | 1225 | (dired-readin) |
492d2437 RS |
1226 | (dired-insert-old-subdirs old-subdir-alist)) |
1227 | (dired-mark-remembered mark-alist) ; mark files that were marked | |
1228 | ;; ... run the hook for the whole buffer, and only after markers | |
1229 | ;; have been reinserted (else omitting in dired-x would omit marked files) | |
1230 | (run-hooks 'dired-after-readin-hook) ; no need to narrow | |
05404988 | 1231 | (dired-restore-positions positions) |
492d2437 | 1232 | (save-excursion ; hide subdirs that were hidden |
d7aed37c SM |
1233 | (dolist (dir hidden-subdirs) |
1234 | (if (dired-goto-subdir dir) | |
7805cdbd LT |
1235 | (dired-hide-subdir 1)))) |
1236 | (unless modflag (restore-buffer-modified-p nil))) | |
492d2437 | 1237 | ;; outside of the let scope |
7805cdbd | 1238 | ;;; Might as well not override the user if the user changed this. |
492d2437 RS |
1239 | ;;; (setq buffer-read-only t) |
1240 | ) | |
1241 | ||
1242 | ;; Subroutines of dired-revert | |
1243 | ;; Some of these are also used when inserting subdirs. | |
1244 | ||
05404988 | 1245 | (defun dired-save-positions () |
065543e7 JL |
1246 | "Return current positions in the buffer and all windows with this directory. |
1247 | The positions have the form (BUFFER-POSITION WINDOW-POSITIONS). | |
1248 | ||
1249 | BUFFER-POSITION is the point position in the current dired buffer. | |
53ad04fc | 1250 | It has the form (BUFFER DIRED-FILENAME BUFFER-POINT). |
065543e7 JL |
1251 | |
1252 | WINDOW-POSITIONS are current positions in all windows displaying | |
1253 | this dired buffer. The window positions have the form (WINDOW | |
1254 | DIRED-FILENAME WINDOW-POINT)." | |
1255 | (list | |
1256 | (list (current-buffer) (dired-get-filename nil t) (point)) | |
1257 | (mapcar (lambda (w) | |
1258 | (list w | |
1259 | (with-selected-window w | |
1260 | (dired-get-filename nil t)) | |
1261 | (window-point w))) | |
1262 | (get-buffer-window-list nil 0 t)))) | |
05404988 SM |
1263 | |
1264 | (defun dired-restore-positions (positions) | |
1265 | "Restore POSITIONS saved with `dired-save-positions'." | |
065543e7 JL |
1266 | (let* ((buf-file-pos (nth 0 positions)) |
1267 | (buffer (nth 0 buf-file-pos))) | |
1268 | (unless (and (nth 1 buf-file-pos) | |
1269 | (dired-goto-file (nth 1 buf-file-pos))) | |
1270 | (goto-char (nth 2 buf-file-pos)) | |
1271 | (dired-move-to-filename)) | |
1272 | (dolist (win-file-pos (nth 1 positions)) | |
1273 | ;; Ensure that window still displays the original buffer. | |
1274 | (when (eq (window-buffer (nth 0 win-file-pos)) buffer) | |
1275 | (with-selected-window (nth 0 win-file-pos) | |
1276 | (unless (and (nth 1 win-file-pos) | |
1277 | (dired-goto-file (nth 1 win-file-pos))) | |
1278 | (goto-char (nth 2 win-file-pos)) | |
1279 | (dired-move-to-filename))))))) | |
05404988 | 1280 | |
492d2437 | 1281 | (defun dired-remember-marks (beg end) |
d7aed37c | 1282 | "Return alist of files and their marks, from BEG to END." |
492d2437 | 1283 | (if selective-display ; must unhide to make this work. |
f836b98e | 1284 | (let ((inhibit-read-only t)) |
492d2437 RS |
1285 | (subst-char-in-region beg end ?\r ?\n))) |
1286 | (let (fil chr alist) | |
1287 | (save-excursion | |
1288 | (goto-char beg) | |
1289 | (while (re-search-forward dired-re-mark end t) | |
1290 | (if (setq fil (dired-get-filename nil t)) | |
1291 | (setq chr (preceding-char) | |
1292 | alist (cons (cons fil chr) alist))))) | |
1293 | alist)) | |
1294 | ||
492d2437 | 1295 | (defun dired-mark-remembered (alist) |
d7aed37c SM |
1296 | "Mark all files remembered in ALIST. |
1297 | Each element of ALIST looks like (FILE . MARKERCHAR)." | |
492d2437 RS |
1298 | (let (elt fil chr) |
1299 | (while alist | |
1300 | (setq elt (car alist) | |
1301 | alist (cdr alist) | |
1302 | fil (car elt) | |
1303 | chr (cdr elt)) | |
1304 | (if (dired-goto-file fil) | |
1305 | (save-excursion | |
1306 | (beginning-of-line) | |
1307 | (delete-char 1) | |
1308 | (insert chr)))))) | |
1309 | ||
492d2437 | 1310 | (defun dired-remember-hidden () |
d7aed37c | 1311 | "Return a list of names of subdirs currently hidden." |
492d2437 RS |
1312 | (let ((l dired-subdir-alist) dir pos result) |
1313 | (while l | |
1314 | (setq dir (car (car l)) | |
1315 | pos (cdr (car l)) | |
1316 | l (cdr l)) | |
1317 | (goto-char pos) | |
1318 | (skip-chars-forward "^\r\n") | |
52041219 | 1319 | (if (eq (following-char) ?\r) |
492d2437 RS |
1320 | (setq result (cons dir result)))) |
1321 | result)) | |
1322 | ||
492d2437 | 1323 | (defun dired-insert-old-subdirs (old-subdir-alist) |
d7aed37c SM |
1324 | "Try to insert all subdirs that were displayed before. |
1325 | Do so according to the former subdir alist OLD-SUBDIR-ALIST." | |
492d2437 RS |
1326 | (or (string-match "R" dired-actual-switches) |
1327 | (let (elt dir) | |
1328 | (while old-subdir-alist | |
1329 | (setq elt (car old-subdir-alist) | |
1330 | old-subdir-alist (cdr old-subdir-alist) | |
1331 | dir (car elt)) | |
1332 | (condition-case () | |
715984d3 RS |
1333 | (progn |
1334 | (dired-uncache dir) | |
f817c0fb | 1335 | (dired-insert-subdir dir)) |
492d2437 | 1336 | (error nil)))))) |
715984d3 | 1337 | |
715984d3 | 1338 | (defun dired-uncache (dir) |
d7aed37c | 1339 | "Remove directory DIR from any directory cache." |
6eaebaa2 | 1340 | (let ((handler (find-file-name-handler dir 'dired-uncache))) |
715984d3 RS |
1341 | (if handler |
1342 | (funcall handler 'dired-uncache dir)))) | |
83fadedf | 1343 | \f |
492d2437 | 1344 | ;; dired mode key bindings and initialization |
84fc2cfa | 1345 | |
d7aed37c | 1346 | (defvar dired-mode-map |
492d2437 RS |
1347 | ;; This looks ugly when substitute-command-keys uses C-d instead d: |
1348 | ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion) | |
3e1fb00f | 1349 | (let ((map (make-keymap))) |
abef340a | 1350 | (set-keymap-parent map special-mode-map) |
3e1fb00f | 1351 | (define-key map [mouse-2] 'dired-mouse-find-file-other-window) |
bb87fbc3 | 1352 | (define-key map [follow-link] 'mouse-face) |
3e1fb00f RS |
1353 | ;; Commands to mark or flag certain categories of files |
1354 | (define-key map "#" 'dired-flag-auto-save-files) | |
3e1fb00f | 1355 | (define-key map "." 'dired-clean-directory) |
3e1fb00f RS |
1356 | (define-key map "~" 'dired-flag-backup-files) |
1357 | ;; Upper case keys (except !) for operating on the marked files | |
1358 | (define-key map "A" 'dired-do-search) | |
1359 | (define-key map "C" 'dired-do-copy) | |
1360 | (define-key map "B" 'dired-do-byte-compile) | |
1361 | (define-key map "D" 'dired-do-delete) | |
1362 | (define-key map "G" 'dired-do-chgrp) | |
1363 | (define-key map "H" 'dired-do-hardlink) | |
1364 | (define-key map "L" 'dired-do-load) | |
1365 | (define-key map "M" 'dired-do-chmod) | |
1366 | (define-key map "O" 'dired-do-chown) | |
1367 | (define-key map "P" 'dired-do-print) | |
ee93b692 | 1368 | (define-key map "Q" 'dired-do-query-replace-regexp) |
3e1fb00f RS |
1369 | (define-key map "R" 'dired-do-rename) |
1370 | (define-key map "S" 'dired-do-symlink) | |
57654b8c | 1371 | (define-key map "T" 'dired-do-touch) |
3e1fb00f RS |
1372 | (define-key map "X" 'dired-do-shell-command) |
1373 | (define-key map "Z" 'dired-do-compress) | |
1374 | (define-key map "!" 'dired-do-shell-command) | |
48e740bf | 1375 | (define-key map "&" 'dired-do-async-shell-command) |
3e1fb00f RS |
1376 | ;; Comparison commands |
1377 | (define-key map "=" 'dired-diff) | |
1378 | (define-key map "\M-=" 'dired-backup-diff) | |
1379 | ;; Tree Dired commands | |
1380 | (define-key map "\M-\C-?" 'dired-unmark-all-files) | |
1381 | (define-key map "\M-\C-d" 'dired-tree-down) | |
1382 | (define-key map "\M-\C-u" 'dired-tree-up) | |
1383 | (define-key map "\M-\C-n" 'dired-next-subdir) | |
1384 | (define-key map "\M-\C-p" 'dired-prev-subdir) | |
1385 | ;; move to marked files | |
1386 | (define-key map "\M-{" 'dired-prev-marked-file) | |
1387 | (define-key map "\M-}" 'dired-next-marked-file) | |
1388 | ;; Make all regexp commands share a `%' prefix: | |
1389 | ;; We used to get to the submap via a symbol dired-regexp-prefix, | |
1390 | ;; but that seems to serve little purpose, and copy-keymap | |
1391 | ;; does a better job without it. | |
1392 | (define-key map "%" nil) | |
1393 | (define-key map "%u" 'dired-upcase) | |
1394 | (define-key map "%l" 'dired-downcase) | |
1395 | (define-key map "%d" 'dired-flag-files-regexp) | |
e9b8e22d | 1396 | (define-key map "%g" 'dired-mark-files-containing-regexp) |
3e1fb00f RS |
1397 | (define-key map "%m" 'dired-mark-files-regexp) |
1398 | (define-key map "%r" 'dired-do-rename-regexp) | |
1399 | (define-key map "%C" 'dired-do-copy-regexp) | |
1400 | (define-key map "%H" 'dired-do-hardlink-regexp) | |
1401 | (define-key map "%R" 'dired-do-rename-regexp) | |
1402 | (define-key map "%S" 'dired-do-symlink-regexp) | |
48e740bf | 1403 | (define-key map "%&" 'dired-flag-garbage-files) |
4a3ca6ed RS |
1404 | ;; Commands for marking and unmarking. |
1405 | (define-key map "*" nil) | |
1406 | (define-key map "**" 'dired-mark-executables) | |
1407 | (define-key map "*/" 'dired-mark-directories) | |
1408 | (define-key map "*@" 'dired-mark-symlinks) | |
1409 | (define-key map "*%" 'dired-mark-files-regexp) | |
1410 | (define-key map "*c" 'dired-change-marks) | |
0609c998 | 1411 | (define-key map "*s" 'dired-mark-subdir-files) |
4a3ca6ed RS |
1412 | (define-key map "*m" 'dired-mark) |
1413 | (define-key map "*u" 'dired-unmark) | |
1414 | (define-key map "*?" 'dired-unmark-all-files) | |
534a6edf | 1415 | (define-key map "*!" 'dired-unmark-all-marks) |
d7aed37c | 1416 | (define-key map "U" 'dired-unmark-all-marks) |
4a3ca6ed RS |
1417 | (define-key map "*\177" 'dired-unmark-backward) |
1418 | (define-key map "*\C-n" 'dired-next-marked-file) | |
1419 | (define-key map "*\C-p" 'dired-prev-marked-file) | |
d588eb90 | 1420 | (define-key map "*t" 'dired-toggle-marks) |
3e1fb00f | 1421 | ;; Lower keys for commands not operating on all the marked files |
c5753a5d | 1422 | (define-key map "a" 'dired-find-alternate-file) |
3e1fb00f RS |
1423 | (define-key map "d" 'dired-flag-file-deletion) |
1424 | (define-key map "e" 'dired-find-file) | |
1425 | (define-key map "f" 'dired-find-file) | |
8cb95edf SM |
1426 | (define-key map "\C-m" 'dired-find-file) |
1427 | (put 'dired-find-file :advertised-binding "\C-m") | |
3e1fb00f | 1428 | (define-key map "g" 'revert-buffer) |
3e1fb00f | 1429 | (define-key map "i" 'dired-maybe-insert-subdir) |
e19d2d64 | 1430 | (define-key map "j" 'dired-goto-file) |
3e1fb00f RS |
1431 | (define-key map "k" 'dired-do-kill-lines) |
1432 | (define-key map "l" 'dired-do-redisplay) | |
1433 | (define-key map "m" 'dired-mark) | |
1434 | (define-key map "n" 'dired-next-line) | |
1435 | (define-key map "o" 'dired-find-file-other-window) | |
1436 | (define-key map "\C-o" 'dired-display-file) | |
1437 | (define-key map "p" 'dired-previous-line) | |
3e1fb00f | 1438 | (define-key map "s" 'dired-sort-toggle-or-edit) |
d588eb90 | 1439 | (define-key map "t" 'dired-toggle-marks) |
3e1fb00f RS |
1440 | (define-key map "u" 'dired-unmark) |
1441 | (define-key map "v" 'dired-view-file) | |
a3f4a3ef | 1442 | (define-key map "w" 'dired-copy-filename-as-kill) |
3e1fb00f | 1443 | (define-key map "x" 'dired-do-flagged-delete) |
d1e99fa5 | 1444 | (define-key map "y" 'dired-show-file-type) |
3e1fb00f RS |
1445 | (define-key map "+" 'dired-create-directory) |
1446 | ;; moving | |
1447 | (define-key map "<" 'dired-prev-dirline) | |
1448 | (define-key map ">" 'dired-next-dirline) | |
1449 | (define-key map "^" 'dired-up-directory) | |
1450 | (define-key map " " 'dired-next-line) | |
aac818a8 TL |
1451 | (define-key map [remap next-line] 'dired-next-line) |
1452 | (define-key map [remap previous-line] 'dired-previous-line) | |
3e1fb00f RS |
1453 | ;; hiding |
1454 | (define-key map "$" 'dired-hide-subdir) | |
1455 | (define-key map "\M-$" 'dired-hide-all) | |
b89b46cf JL |
1456 | ;; isearch |
1457 | (define-key map (kbd "M-s a C-s") 'dired-do-isearch) | |
1458 | (define-key map (kbd "M-s a M-C-s") 'dired-do-isearch-regexp) | |
eae2c85e JL |
1459 | (define-key map (kbd "M-s f C-s") 'dired-isearch-filenames) |
1460 | (define-key map (kbd "M-s f M-C-s") 'dired-isearch-filenames-regexp) | |
3e1fb00f | 1461 | ;; misc |
aac818a8 | 1462 | (define-key map [remap toggle-read-only] 'dired-toggle-read-only) |
3e1fb00f RS |
1463 | (define-key map "?" 'dired-summary) |
1464 | (define-key map "\177" 'dired-unmark-backward) | |
5e5a3412 RS |
1465 | (define-key map [remap undo] 'dired-undo) |
1466 | (define-key map [remap advertised-undo] 'dired-undo) | |
aa1a7612 CY |
1467 | ;; thumbnail manipulation (image-dired) |
1468 | (define-key map "\C-td" 'image-dired-display-thumbs) | |
1469 | (define-key map "\C-tt" 'image-dired-tag-files) | |
1470 | (define-key map "\C-tr" 'image-dired-delete-tag) | |
1471 | (define-key map "\C-tj" 'image-dired-jump-thumbnail-buffer) | |
1472 | (define-key map "\C-ti" 'image-dired-dired-display-image) | |
1473 | (define-key map "\C-tx" 'image-dired-dired-display-external) | |
1474 | (define-key map "\C-ta" 'image-dired-display-thumbs-append) | |
1475 | (define-key map "\C-t." 'image-dired-display-thumb) | |
1476 | (define-key map "\C-tc" 'image-dired-dired-comment-files) | |
1477 | (define-key map "\C-tf" 'image-dired-mark-tagged-files) | |
84fb0956 | 1478 | (define-key map "\C-t\C-t" 'image-dired-dired-toggle-marked-thumbs) |
aa1a7612 | 1479 | (define-key map "\C-te" 'image-dired-dired-edit-comment-and-tags) |
bfeee9d1 DN |
1480 | ;; encryption and decryption (epa-dired) |
1481 | (define-key map ":d" 'epa-dired-do-decrypt) | |
1482 | (define-key map ":v" 'epa-dired-do-verify) | |
1483 | (define-key map ":s" 'epa-dired-do-sign) | |
1484 | (define-key map ":e" 'epa-dired-do-encrypt) | |
3e1fb00f RS |
1485 | |
1486 | ;; Make menu bar items. | |
1487 | ||
d066de8e EZ |
1488 | ;; No need to fo this, now that top-level items are fewer. |
1489 | ;;;; | |
3e1fb00f | 1490 | ;; Get rid of the Edit menu bar item to save space. |
d066de8e | 1491 | ;(define-key map [menu-bar edit] 'undefined) |
3e1fb00f RS |
1492 | |
1493 | (define-key map [menu-bar subdir] | |
1494 | (cons "Subdir" (make-sparse-keymap "Subdir"))) | |
1495 | ||
1496 | (define-key map [menu-bar subdir hide-all] | |
d066de8e EZ |
1497 | '(menu-item "Hide All" dired-hide-all |
1498 | :help "Hide all subdirectories, leave only header lines")) | |
3e1fb00f | 1499 | (define-key map [menu-bar subdir hide-subdir] |
d066de8e EZ |
1500 | '(menu-item "Hide/UnHide Subdir" dired-hide-subdir |
1501 | :help "Hide or unhide current directory listing")) | |
3e1fb00f | 1502 | (define-key map [menu-bar subdir tree-down] |
d066de8e EZ |
1503 | '(menu-item "Tree Down" dired-tree-down |
1504 | :help "Go to first subdirectory header down the tree")) | |
3e1fb00f | 1505 | (define-key map [menu-bar subdir tree-up] |
d066de8e EZ |
1506 | '(menu-item "Tree Up" dired-tree-up |
1507 | :help "Go to first subdirectory header up the tree")) | |
3e1fb00f | 1508 | (define-key map [menu-bar subdir up] |
d066de8e EZ |
1509 | '(menu-item "Up Directory" dired-up-directory |
1510 | :help "Edit the parent directory")) | |
3e1fb00f | 1511 | (define-key map [menu-bar subdir prev-subdir] |
d066de8e EZ |
1512 | '(menu-item "Prev Subdir" dired-prev-subdir |
1513 | :help "Go to previous subdirectory header line")) | |
3e1fb00f | 1514 | (define-key map [menu-bar subdir next-subdir] |
d066de8e EZ |
1515 | '(menu-item "Next Subdir" dired-next-subdir |
1516 | :help "Go to next subdirectory header line")) | |
3e1fb00f | 1517 | (define-key map [menu-bar subdir prev-dirline] |
d066de8e EZ |
1518 | '(menu-item "Prev Dirline" dired-prev-dirline |
1519 | :help "Move to next directory-file line")) | |
3e1fb00f | 1520 | (define-key map [menu-bar subdir next-dirline] |
d066de8e EZ |
1521 | '(menu-item "Next Dirline" dired-next-dirline |
1522 | :help "Move to previous directory-file line")) | |
3e1fb00f | 1523 | (define-key map [menu-bar subdir insert] |
d066de8e | 1524 | '(menu-item "Insert This Subdir" dired-maybe-insert-subdir |
bedd8a58 CY |
1525 | :help "Insert contents of subdirectory" |
1526 | :enable (let ((f (dired-get-filename nil t))) | |
1527 | (and f (file-directory-p f))))) | |
3e1fb00f RS |
1528 | (define-key map [menu-bar immediate] |
1529 | (cons "Immediate" (make-sparse-keymap "Immediate"))) | |
1530 | ||
635abd82 | 1531 | (define-key map |
aa1a7612 CY |
1532 | [menu-bar immediate image-dired-dired-display-external] |
1533 | '(menu-item "Display Image Externally" image-dired-dired-display-external | |
bc4dbed5 | 1534 | :help "Display image in external viewer")) |
635abd82 | 1535 | (define-key map |
aa1a7612 CY |
1536 | [menu-bar immediate image-dired-dired-display-image] |
1537 | '(menu-item "Display Image" image-dired-dired-display-image | |
bc4dbed5 | 1538 | :help "Display sized image in a separate window")) |
36938994 | 1539 | |
836788c9 MD |
1540 | (define-key map [menu-bar immediate revert-buffer] |
1541 | '(menu-item "Refresh" revert-buffer | |
1542 | :help "Update contents of shown directories")) | |
1543 | ||
1544 | (define-key map [menu-bar immediate dashes] | |
1545 | '("--")) | |
635abd82 | 1546 | |
eae2c85e JL |
1547 | (define-key map [menu-bar immediate isearch-filenames-regexp] |
1548 | '(menu-item "Isearch Regexp in File Names..." dired-isearch-filenames-regexp | |
1549 | :help "Incrementally search for regexp in file names only")) | |
1550 | (define-key map [menu-bar immediate isearch-filenames] | |
1551 | '(menu-item "Isearch in File Names..." dired-isearch-filenames | |
1552 | :help "Incrementally search for string in file names only.")) | |
0e2aaf98 | 1553 | (define-key map [menu-bar immediate compare-directories] |
27af5d58 | 1554 | '(menu-item "Compare Directories..." dired-compare-directories |
0e2aaf98 | 1555 | :help "Mark files with different attributes in two dired buffers")) |
3e1fb00f | 1556 | (define-key map [menu-bar immediate backup-diff] |
d066de8e EZ |
1557 | '(menu-item "Compare with Backup" dired-backup-diff |
1558 | :help "Diff file at cursor with its latest backup")) | |
3e1fb00f | 1559 | (define-key map [menu-bar immediate diff] |
d066de8e EZ |
1560 | '(menu-item "Diff..." dired-diff |
1561 | :help "Compare file at cursor with another file")) | |
3e1fb00f | 1562 | (define-key map [menu-bar immediate view] |
d066de8e EZ |
1563 | '(menu-item "View This File" dired-view-file |
1564 | :help "Examine file at cursor in read-only mode")) | |
3e1fb00f | 1565 | (define-key map [menu-bar immediate display] |
d066de8e EZ |
1566 | '(menu-item "Display in Other Window" dired-display-file |
1567 | :help "Display file at cursor in other window")) | |
3e1fb00f | 1568 | (define-key map [menu-bar immediate find-file-other-window] |
d066de8e EZ |
1569 | '(menu-item "Find in Other Window" dired-find-file-other-window |
1570 | :help "Edit file at cursor in other window")) | |
3e1fb00f | 1571 | (define-key map [menu-bar immediate find-file] |
d066de8e EZ |
1572 | '(menu-item "Find This File" dired-find-file |
1573 | :help "Edit file at cursor")) | |
3e1fb00f | 1574 | (define-key map [menu-bar immediate create-directory] |
4b33a64a DN |
1575 | '(menu-item "Create Directory..." dired-create-directory |
1576 | :help "Create a directory")) | |
2ecab840 | 1577 | (define-key map [menu-bar immediate wdired-mode] |
40aa8257 | 1578 | '(menu-item "Edit File Names" wdired-change-to-wdired-mode |
4b33a64a | 1579 | :help "Put a dired buffer in a mode in which filenames are editable" |
89e87059 | 1580 | :keys "C-x C-q" |
40aa8257 | 1581 | :filter (lambda (x) (if (eq major-mode 'dired-mode) x)))) |
3e1fb00f RS |
1582 | |
1583 | (define-key map [menu-bar regexp] | |
1584 | (cons "Regexp" (make-sparse-keymap "Regexp"))) | |
1585 | ||
836788c9 | 1586 | (define-key map |
aa1a7612 CY |
1587 | [menu-bar regexp image-dired-mark-tagged-files] |
1588 | '(menu-item "Mark From Image Tag..." image-dired-mark-tagged-files | |
098bab71 JB |
1589 | :help "Mark files whose image tags matches regexp")) |
1590 | ||
836788c9 | 1591 | (define-key map [menu-bar regexp dashes-1] |
098bab71 | 1592 | '("--")) |
836788c9 | 1593 | |
3e1fb00f | 1594 | (define-key map [menu-bar regexp downcase] |
d066de8e EZ |
1595 | '(menu-item "Downcase" dired-downcase |
1596 | ;; When running on plain MS-DOS, there's only one | |
1597 | ;; letter-case for file names. | |
1598 | :enable (or (not (fboundp 'msdos-long-file-names)) | |
1599 | (msdos-long-file-names)) | |
1600 | :help "Rename marked files to lower-case name")) | |
3e1fb00f | 1601 | (define-key map [menu-bar regexp upcase] |
d066de8e EZ |
1602 | '(menu-item "Upcase" dired-upcase |
1603 | :enable (or (not (fboundp 'msdos-long-file-names)) | |
1604 | (msdos-long-file-names)) | |
1605 | :help "Rename marked files to upper-case name")) | |
3e1fb00f | 1606 | (define-key map [menu-bar regexp hardlink] |
d066de8e EZ |
1607 | '(menu-item "Hardlink..." dired-do-hardlink-regexp |
1608 | :help "Make hard links for files matching regexp")) | |
3e1fb00f | 1609 | (define-key map [menu-bar regexp symlink] |
d066de8e EZ |
1610 | '(menu-item "Symlink..." dired-do-symlink-regexp |
1611 | :visible (fboundp 'make-symbolic-link) | |
1612 | :help "Make symbolic links for files matching regexp")) | |
3e1fb00f | 1613 | (define-key map [menu-bar regexp rename] |
d066de8e EZ |
1614 | '(menu-item "Rename..." dired-do-rename-regexp |
1615 | :help "Rename marked files matching regexp")) | |
3e1fb00f | 1616 | (define-key map [menu-bar regexp copy] |
d066de8e EZ |
1617 | '(menu-item "Copy..." dired-do-copy-regexp |
1618 | :help "Copy marked files matching regexp")) | |
3e1fb00f | 1619 | (define-key map [menu-bar regexp flag] |
d066de8e EZ |
1620 | '(menu-item "Flag..." dired-flag-files-regexp |
1621 | :help "Flag files matching regexp for deletion")) | |
3e1fb00f | 1622 | (define-key map [menu-bar regexp mark] |
d066de8e EZ |
1623 | '(menu-item "Mark..." dired-mark-files-regexp |
1624 | :help "Mark files matching regexp for future operations")) | |
aa924deb | 1625 | (define-key map [menu-bar regexp mark-cont] |
d066de8e EZ |
1626 | '(menu-item "Mark Containing..." dired-mark-files-containing-regexp |
1627 | :help "Mark files whose contents matches regexp")) | |
3e1fb00f RS |
1628 | |
1629 | (define-key map [menu-bar mark] | |
1630 | (cons "Mark" (make-sparse-keymap "Mark"))) | |
1631 | ||
1632 | (define-key map [menu-bar mark prev] | |
d066de8e EZ |
1633 | '(menu-item "Previous Marked" dired-prev-marked-file |
1634 | :help "Move to previous marked file")) | |
3e1fb00f | 1635 | (define-key map [menu-bar mark next] |
d066de8e EZ |
1636 | '(menu-item "Next Marked" dired-next-marked-file |
1637 | :help "Move to next marked file")) | |
3e1fb00f | 1638 | (define-key map [menu-bar mark marks] |
d066de8e EZ |
1639 | '(menu-item "Change Marks..." dired-change-marks |
1640 | :help "Replace marker with another character")) | |
3e1fb00f | 1641 | (define-key map [menu-bar mark unmark-all] |
d066de8e | 1642 | '(menu-item "Unmark All" dired-unmark-all-marks)) |
3e1fb00f | 1643 | (define-key map [menu-bar mark symlinks] |
d066de8e EZ |
1644 | '(menu-item "Mark Symlinks" dired-mark-symlinks |
1645 | :visible (fboundp 'make-symbolic-link) | |
1646 | :help "Mark all symbolic links")) | |
3e1fb00f | 1647 | (define-key map [menu-bar mark directories] |
d066de8e EZ |
1648 | '(menu-item "Mark Directories" dired-mark-directories |
1649 | :help "Mark all directories except `.' and `..'")) | |
3e1fb00f | 1650 | (define-key map [menu-bar mark directory] |
d066de8e EZ |
1651 | '(menu-item "Mark Old Backups" dired-clean-directory |
1652 | :help "Flag old numbered backups for deletion")) | |
3e1fb00f | 1653 | (define-key map [menu-bar mark executables] |
d066de8e EZ |
1654 | '(menu-item "Mark Executables" dired-mark-executables |
1655 | :help "Mark all executable files")) | |
84d3f6e8 | 1656 | (define-key map [menu-bar mark garbage-files] |
d066de8e EZ |
1657 | '(menu-item "Flag Garbage Files" dired-flag-garbage-files |
1658 | :help "Flag unneeded files for deletion")) | |
3e1fb00f | 1659 | (define-key map [menu-bar mark backup-files] |
d066de8e EZ |
1660 | '(menu-item "Flag Backup Files" dired-flag-backup-files |
1661 | :help "Flag all backup files for deletion")) | |
3e1fb00f | 1662 | (define-key map [menu-bar mark auto-save-files] |
d066de8e EZ |
1663 | '(menu-item "Flag Auto-save Files" dired-flag-auto-save-files |
1664 | :help "Flag auto-save files for deletion")) | |
3e1fb00f | 1665 | (define-key map [menu-bar mark deletion] |
d066de8e EZ |
1666 | '(menu-item "Flag" dired-flag-file-deletion |
1667 | :help "Flag current line's file for deletion")) | |
3e1fb00f | 1668 | (define-key map [menu-bar mark unmark] |
d066de8e EZ |
1669 | '(menu-item "Unmark" dired-unmark |
1670 | :help "Unmark or unflag current line's file")) | |
3e1fb00f | 1671 | (define-key map [menu-bar mark mark] |
d066de8e EZ |
1672 | '(menu-item "Mark" dired-mark |
1673 | :help "Mark current line's file for future operations")) | |
e5f0841e | 1674 | (define-key map [menu-bar mark toggle-marks] |
d588eb90 | 1675 | '(menu-item "Toggle Marks" dired-toggle-marks |
d066de8e | 1676 | :help "Mark unmarked files, unmark marked ones")) |
3e1fb00f RS |
1677 | |
1678 | (define-key map [menu-bar operate] | |
1679 | (cons "Operate" (make-sparse-keymap "Operate"))) | |
1680 | ||
836788c9 | 1681 | (define-key map |
aa1a7612 CY |
1682 | [menu-bar operate image-dired-delete-tag] |
1683 | '(menu-item "Delete Image Tag..." image-dired-delete-tag | |
b305952d | 1684 | :help "Delete image tag from current or marked files")) |
836788c9 | 1685 | (define-key map |
aa1a7612 CY |
1686 | [menu-bar operate image-dired-tag-files] |
1687 | '(menu-item "Add Image Tags..." image-dired-tag-files | |
836788c9 MD |
1688 | :help "Add image tags to current or marked files")) |
1689 | (define-key map | |
aa1a7612 CY |
1690 | [menu-bar operate image-dired-dired-comment-files] |
1691 | '(menu-item "Add Image Comment..." image-dired-dired-comment-files | |
836788c9 MD |
1692 | :help "Add image comment to current or marked files")) |
1693 | (define-key map | |
aa1a7612 | 1694 | [menu-bar operate image-dired-display-thumbs] |
a41925a2 JL |
1695 | '(menu-item "Display image thumbnails" image-dired-display-thumbs |
1696 | :help "Display image thumbnails for current or marked image files")) | |
098bab71 | 1697 | |
7cce3c91 CY |
1698 | (define-key map [menu-bar operate dashes-4] |
1699 | '("--")) | |
1700 | ||
1701 | (define-key map | |
1702 | [menu-bar operate epa-dired-do-decrypt] | |
1703 | '(menu-item "Decrypt" epa-dired-do-decrypt | |
1704 | :help "Decrypt file at cursor")) | |
1705 | ||
1706 | (define-key map | |
1707 | [menu-bar operate epa-dired-do-verify] | |
1708 | '(menu-item "Verify" epa-dired-do-verify | |
1709 | :help "Verify digital signature of file at cursor")) | |
1710 | ||
1711 | (define-key map | |
1712 | [menu-bar operate epa-dired-do-sign] | |
1713 | '(menu-item "Sign" epa-dired-do-sign | |
1714 | :help "Create digital signature of file at cursor")) | |
1715 | ||
1716 | (define-key map | |
1717 | [menu-bar operate epa-dired-do-encrypt] | |
1718 | '(menu-item "Encrypt" epa-dired-do-encrypt | |
1719 | :help "Encrypt file at cursor")) | |
1720 | ||
836788c9 | 1721 | (define-key map [menu-bar operate dashes-3] |
098bab71 | 1722 | '("--")) |
836788c9 | 1723 | |
3e1fb00f | 1724 | (define-key map [menu-bar operate query-replace] |
ee93b692 | 1725 | '(menu-item "Query Replace in Files..." dired-do-query-replace-regexp |
d066de8e | 1726 | :help "Replace regexp in marked files")) |
3e1fb00f | 1727 | (define-key map [menu-bar operate search] |
d066de8e EZ |
1728 | '(menu-item "Search Files..." dired-do-search |
1729 | :help "Search marked files for regexp")) | |
b89b46cf JL |
1730 | (define-key map [menu-bar operate isearch-regexp] |
1731 | '(menu-item "Isearch Regexp Files..." dired-do-isearch-regexp | |
1732 | :help "Incrementally search marked files for regexp")) | |
1733 | (define-key map [menu-bar operate isearch] | |
1734 | '(menu-item "Isearch Files..." dired-do-isearch | |
1735 | :help "Incrementally search marked files for string")) | |
3e1fb00f | 1736 | (define-key map [menu-bar operate chown] |
d066de8e EZ |
1737 | '(menu-item "Change Owner..." dired-do-chown |
1738 | :visible (not (memq system-type '(ms-dos windows-nt))) | |
1739 | :help "Change the owner of marked files")) | |
3e1fb00f | 1740 | (define-key map [menu-bar operate chgrp] |
d066de8e EZ |
1741 | '(menu-item "Change Group..." dired-do-chgrp |
1742 | :visible (not (memq system-type '(ms-dos windows-nt))) | |
1743 | :help "Change the group of marked files")) | |
3e1fb00f | 1744 | (define-key map [menu-bar operate chmod] |
d066de8e EZ |
1745 | '(menu-item "Change Mode..." dired-do-chmod |
1746 | :help "Change mode (attributes) of marked files")) | |
57654b8c JL |
1747 | (define-key map [menu-bar operate touch] |
1748 | '(menu-item "Change Timestamp..." dired-do-touch | |
1749 | :help "Change timestamp of marked files")) | |
3e1fb00f | 1750 | (define-key map [menu-bar operate load] |
d066de8e EZ |
1751 | '(menu-item "Load" dired-do-load |
1752 | :help "Load marked Emacs Lisp files")) | |
3e1fb00f | 1753 | (define-key map [menu-bar operate compile] |
d066de8e EZ |
1754 | '(menu-item "Byte-compile" dired-do-byte-compile |
1755 | :help "Byte-compile marked Emacs Lisp files")) | |
3e1fb00f | 1756 | (define-key map [menu-bar operate compress] |
d066de8e EZ |
1757 | '(menu-item "Compress" dired-do-compress |
1758 | :help "Compress/uncompress marked files")) | |
3e1fb00f | 1759 | (define-key map [menu-bar operate print] |
d066de8e EZ |
1760 | '(menu-item "Print..." dired-do-print |
1761 | :help "Ask for print command and print marked files")) | |
3e1fb00f | 1762 | (define-key map [menu-bar operate hardlink] |
d066de8e EZ |
1763 | '(menu-item "Hardlink to..." dired-do-hardlink |
1764 | :help "Make hard links for current or marked files")) | |
3e1fb00f | 1765 | (define-key map [menu-bar operate symlink] |
d066de8e EZ |
1766 | '(menu-item "Symlink to..." dired-do-symlink |
1767 | :visible (fboundp 'make-symbolic-link) | |
1768 | :help "Make symbolic links for current or marked files")) | |
077bf69b GM |
1769 | (define-key map [menu-bar operate async-command] |
1770 | '(menu-item "Asynchronous Shell Command..." dired-do-async-shell-command | |
1771 | :help "Run a shell command asynchronously on current or marked files")) | |
3e1fb00f | 1772 | (define-key map [menu-bar operate command] |
d066de8e | 1773 | '(menu-item "Shell Command..." dired-do-shell-command |
077bf69b | 1774 | :help "Run a shell command on current or marked files")) |
3e1fb00f | 1775 | (define-key map [menu-bar operate delete] |
d066de8e EZ |
1776 | '(menu-item "Delete" dired-do-delete |
1777 | :help "Delete current file or all marked files")) | |
3e1fb00f | 1778 | (define-key map [menu-bar operate rename] |
d066de8e EZ |
1779 | '(menu-item "Rename to..." dired-do-rename |
1780 | :help "Rename current file or move marked files")) | |
3e1fb00f | 1781 | (define-key map [menu-bar operate copy] |
d066de8e EZ |
1782 | '(menu-item "Copy to..." dired-do-copy |
1783 | :help "Copy current file or all marked files")) | |
3e1fb00f | 1784 | |
d7aed37c SM |
1785 | map) |
1786 | "Local keymap for `dired-mode' buffers.") | |
83fadedf | 1787 | \f |
84fc2cfa ER |
1788 | ;; Dired mode is suitable only for specially formatted data. |
1789 | (put 'dired-mode 'mode-class 'special) | |
1790 | ||
a515788d LH |
1791 | ;; Autoload cookie needed by desktop.el |
1792 | ;;;###autoload | |
492d2437 RS |
1793 | (defun dired-mode (&optional dirname switches) |
1794 | "\ | |
1795 | Mode for \"editing\" directory listings. | |
68d2f12f | 1796 | In Dired, you are \"editing\" a list of the files in a directory and |
492d2437 RS |
1797 | \(optionally) its subdirectories, in the format of `ls -lR'. |
1798 | Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise. | |
1799 | \"Editing\" means that you can run shell commands on files, visit, | |
1800 | compress, load or byte-compile them, change their file attributes | |
1801 | and insert subdirectories into the same buffer. You can \"mark\" | |
1802 | files for later commands or \"flag\" them for deletion, either file | |
1803 | by file or all files matching certain criteria. | |
1804 | You can move using the usual cursor motion commands.\\<dired-mode-map> | |
191e2bed LMI |
1805 | The buffer is read-only. Digits are prefix arguments. |
1806 | Type \\[dired-flag-file-deletion] to flag a file `D' for deletion. | |
492d2437 RS |
1807 | Type \\[dired-mark] to Mark a file or subdirectory for later commands. |
1808 | Most commands operate on the marked files and use the current file | |
1809 | if no files are marked. Use a numeric prefix argument to operate on | |
1810 | the next ARG (or previous -ARG if ARG<0) files, or just `1' | |
1811 | to operate on the current file only. Prefix arguments override marks. | |
1812 | Mark-using commands display a list of failures afterwards. Type \\[dired-summary] | |
1813 | to see why something went wrong. | |
191e2bed | 1814 | Type \\[dired-unmark] to Unmark a file or all files of an inserted subdirectory. |
a48868a7 | 1815 | Type \\[dired-unmark-backward] to back up one line and unmark or unflag. |
191e2bed | 1816 | Type \\[dired-do-flagged-delete] to delete (eXecute) the files flagged `D'. |
8cb95edf | 1817 | Type \\[dired-find-file] to Find the current line's file |
492d2437 RS |
1818 | (or dired it in another buffer, if it is a directory). |
1819 | Type \\[dired-find-file-other-window] to find file or dired directory in Other window. | |
1820 | Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer. | |
1821 | Type \\[dired-do-rename] to Rename a file or move the marked files to another directory. | |
1822 | Type \\[dired-do-copy] to Copy files. | |
ee680b2b JB |
1823 | Type \\[dired-sort-toggle-or-edit] to toggle Sorting by name/date or change the `ls' switches. |
1824 | Type \\[revert-buffer] to read all currently expanded directories aGain. | |
492d2437 | 1825 | This retains all marks and hides subdirs again that were hidden before. |
191e2bed | 1826 | Use `SPC' and `DEL' to move down and up by lines. |
492d2437 | 1827 | |
ee680b2b | 1828 | If Dired ever gets confused, you can either type \\[revert-buffer] \ |
492d2437 RS |
1829 | to read the |
1830 | directories again, type \\[dired-do-redisplay] \ | |
191e2bed | 1831 | to relist the file at point or the marked files or a |
492d2437 RS |
1832 | subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer |
1833 | again for the directory tree. | |
1834 | ||
1835 | Customization variables (rename this buffer and type \\[describe-variable] on each line | |
1836 | for more info): | |
1837 | ||
87ae59e3 SM |
1838 | `dired-listing-switches' |
1839 | `dired-trivial-filenames' | |
1840 | `dired-shrink-to-fit' | |
1841 | `dired-marker-char' | |
1842 | `dired-del-marker' | |
1843 | `dired-keep-marker-rename' | |
1844 | `dired-keep-marker-copy' | |
1845 | `dired-keep-marker-hardlink' | |
1846 | `dired-keep-marker-symlink' | |
492d2437 RS |
1847 | |
1848 | Hooks (use \\[describe-variable] to see their documentation): | |
1849 | ||
87ae59e3 SM |
1850 | `dired-before-readin-hook' |
1851 | `dired-after-readin-hook' | |
1852 | `dired-mode-hook' | |
1853 | `dired-load-hook' | |
492d2437 RS |
1854 | |
1855 | Keybindings: | |
84fc2cfa | 1856 | \\{dired-mode-map}" |
492d2437 RS |
1857 | ;; Not to be called interactively (e.g. dired-directory will be set |
1858 | ;; to default-directory, which is wrong with wildcards). | |
84fc2cfa | 1859 | (kill-all-local-variables) |
84fc2cfa | 1860 | (use-local-map dired-mode-map) |
492d2437 RS |
1861 | (dired-advertise) ; default-directory is already set |
1862 | (setq major-mode 'dired-mode | |
1863 | mode-name "Dired" | |
87ae59e3 | 1864 | ;; case-fold-search nil |
492d2437 RS |
1865 | buffer-read-only t |
1866 | selective-display t ; for subdirectory hiding | |
0acdf642 GM |
1867 | mode-line-buffer-identification |
1868 | (propertized-buffer-identification "%17b")) | |
492d2437 RS |
1869 | (set (make-local-variable 'revert-buffer-function) |
1870 | (function dired-revert)) | |
573e4d2d LT |
1871 | (set (make-local-variable 'buffer-stale-function) |
1872 | (function dired-buffer-stale-p)) | |
492d2437 RS |
1873 | (set (make-local-variable 'page-delimiter) |
1874 | "\n\n") | |
1875 | (set (make-local-variable 'dired-directory) | |
1876 | (or dirname default-directory)) | |
1877 | ;; list-buffers uses this to display the dir being edited in this buffer. | |
11ee8d90 JB |
1878 | (setq list-buffers-directory |
1879 | (expand-file-name (if (listp dired-directory) | |
1880 | (car dired-directory) | |
1881 | dired-directory))) | |
492d2437 RS |
1882 | (set (make-local-variable 'dired-actual-switches) |
1883 | (or switches dired-listing-switches)) | |
d7aed37c SM |
1884 | (set (make-local-variable 'font-lock-defaults) |
1885 | '(dired-font-lock-keywords t nil nil beginning-of-line)) | |
cb5f1f67 | 1886 | (set (make-local-variable 'desktop-save-buffer) |
31b4c848 | 1887 | 'dired-desktop-buffer-misc-data) |
5553077c | 1888 | (setq dired-switches-alist nil) |
817b48a7 | 1889 | (hack-dir-local-variables-non-file-buffer) ; before sorting |
492d2437 | 1890 | (dired-sort-other dired-actual-switches t) |
361eee8f | 1891 | (when (featurep 'dnd) |
6ae53dc1 RS |
1892 | (set (make-local-variable 'dnd-protocol-alist) |
1893 | (append dired-dnd-protocol-alist dnd-protocol-alist))) | |
7d371eac | 1894 | (add-hook 'file-name-at-point-functions 'dired-file-name-at-point nil t) |
eae2c85e | 1895 | (add-hook 'isearch-mode-hook 'dired-isearch-filenames-setup nil t) |
1ed8284d | 1896 | (run-mode-hooks 'dired-mode-hook)) |
83fadedf | 1897 | \f |
eb8c3be9 | 1898 | ;; Idiosyncratic dired commands that don't deal with marks. |
84fc2cfa | 1899 | |
84fc2cfa | 1900 | (defun dired-summary () |
ee680b2b | 1901 | "Summarize basic Dired commands and show recent dired errors." |
84fc2cfa | 1902 | (interactive) |
492d2437 | 1903 | (dired-why) |
84fc2cfa ER |
1904 | ;>> this should check the key-bindings and use substitute-command-keys if non-standard |
1905 | (message | |
50f74744 | 1906 | "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp")) |
84fc2cfa | 1907 | |
492d2437 RS |
1908 | (defun dired-undo () |
1909 | "Undo in a dired buffer. | |
1910 | This doesn't recover lost files, it just undoes changes in the buffer itself. | |
487327a9 | 1911 | You can use it to recover marks, killed lines or subdirs." |
492d2437 | 1912 | (interactive) |
f836b98e | 1913 | (let ((inhibit-read-only t)) |
487327a9 LT |
1914 | (undo)) |
1915 | (dired-build-subdir-alist) | |
ee680b2b | 1916 | (message "Change in dired buffer undone. |
487327a9 | 1917 | Actual changes in files cannot be undone by Emacs.")) |
84fc2cfa | 1918 | |
40aa8257 JL |
1919 | (defun dired-toggle-read-only () |
1920 | "Edit dired buffer with Wdired, or set it read-only. | |
1921 | Call `wdired-change-to-wdired-mode' in dired buffers whose editing is | |
1922 | supported by Wdired (the major mode of the dired buffer is `dired-mode'). | |
1923 | Otherwise, for buffers inheriting from dired-mode, call `toggle-read-only'." | |
1924 | (interactive) | |
1925 | (if (eq major-mode 'dired-mode) | |
1926 | (wdired-change-to-wdired-mode) | |
1927 | (toggle-read-only))) | |
1928 | ||
84fc2cfa ER |
1929 | (defun dired-next-line (arg) |
1930 | "Move down lines then position at filename. | |
1931 | Optional prefix ARG says how many lines to move; default is one line." | |
1932 | (interactive "p") | |
e82a724f | 1933 | (forward-line arg) |
84fc2cfa ER |
1934 | (dired-move-to-filename)) |
1935 | ||
1936 | (defun dired-previous-line (arg) | |
1937 | "Move up lines then position at filename. | |
1938 | Optional prefix ARG says how many lines to move; default is one line." | |
1939 | (interactive "p") | |
e82a724f | 1940 | (forward-line (- arg)) |
84fc2cfa ER |
1941 | (dired-move-to-filename)) |
1942 | ||
492d2437 RS |
1943 | (defun dired-next-dirline (arg &optional opoint) |
1944 | "Goto ARG'th next directory file line." | |
1945 | (interactive "p") | |
1946 | (or opoint (setq opoint (point))) | |
1947 | (if (if (> arg 0) | |
1948 | (re-search-forward dired-re-dir nil t arg) | |
1949 | (beginning-of-line) | |
1950 | (re-search-backward dired-re-dir nil t (- arg))) | |
1951 | (dired-move-to-filename) ; user may type `i' or `f' | |
1952 | (goto-char opoint) | |
1953 | (error "No more subdirectories"))) | |
1954 | ||
1955 | (defun dired-prev-dirline (arg) | |
1956 | "Goto ARG'th previous directory file line." | |
1957 | (interactive "p") | |
1958 | (dired-next-dirline (- arg))) | |
1959 | ||
ac1ce341 | 1960 | (defun dired-up-directory (&optional other-window) |
68d2f12f | 1961 | "Run Dired on parent directory of current directory. |
492d2437 RS |
1962 | Find the parent directory either in this buffer or another buffer. |
1963 | Creates a buffer if necessary." | |
ac1ce341 | 1964 | (interactive "P") |
492d2437 RS |
1965 | (let* ((dir (dired-current-directory)) |
1966 | (up (file-name-directory (directory-file-name dir)))) | |
1967 | (or (dired-goto-file (directory-file-name dir)) | |
7b2469ae RS |
1968 | ;; Only try dired-goto-subdir if buffer has more than one dir. |
1969 | (and (cdr dired-subdir-alist) | |
492d2437 RS |
1970 | (dired-goto-subdir up)) |
1971 | (progn | |
ac1ce341 EN |
1972 | (if other-window |
1973 | (dired-other-window up) | |
1974 | (dired up)) | |
492d2437 | 1975 | (dired-goto-file dir))))) |
84fc2cfa | 1976 | |
68d2f12f RS |
1977 | (defun dired-get-file-for-visit () |
1978 | "Get the current line's file name, with an error if file does not exist." | |
53fd9d05 | 1979 | (interactive) |
e9407052 RS |
1980 | ;; We pass t for second arg so that we don't get error for `.' and `..'. |
1981 | (let ((raw (dired-get-filename nil t)) | |
1982 | file-name) | |
1983 | (if (null raw) | |
1984 | (error "No file on this line")) | |
1985 | (setq file-name (file-name-sans-versions raw t)) | |
c439687b | 1986 | (if (file-exists-p file-name) |
68d2f12f | 1987 | file-name |
55642aa4 RS |
1988 | (if (file-symlink-p file-name) |
1989 | (error "File is a symlink to a nonexistent target") | |
ee680b2b | 1990 | (error "File no longer exists; type `g' to update dired buffer"))))) |
84fc2cfa | 1991 | |
f143d380 | 1992 | ;; Force C-m keybinding rather than `f' or `e' in the mode doc: |
8cb95edf | 1993 | (define-obsolete-function-alias 'dired-advertised-find-file 'dired-find-file "23.2") |
68d2f12f RS |
1994 | (defun dired-find-file () |
1995 | "In Dired, visit the file or directory named on this line." | |
1996 | (interactive) | |
1d5ad120 | 1997 | ;; Bind `find-file-run-dired' so that the command works on directories |
59d18288 RS |
1998 | ;; too, independent of the user's setting. |
1999 | (let ((find-file-run-dired t)) | |
2000 | (find-file (dired-get-file-for-visit)))) | |
68d2f12f | 2001 | |
c5753a5d | 2002 | (defun dired-find-alternate-file () |
68d2f12f | 2003 | "In Dired, visit this file or directory instead of the dired buffer." |
c5753a5d GM |
2004 | (interactive) |
2005 | (set-buffer-modified-p nil) | |
68d2f12f | 2006 | (find-alternate-file (dired-get-file-for-visit))) |
336cd99d | 2007 | ;; Don't override the setting from .emacs. |
c21993d0 | 2008 | ;;;###autoload (put 'dired-find-alternate-file 'disabled t) |
c5753a5d | 2009 | |
dbcb9389 | 2010 | (defun dired-mouse-find-file-other-window (event) |
68d2f12f | 2011 | "In Dired, visit the file or directory name you click on." |
dbcb9389 | 2012 | (interactive "e") |
2aaa7f0a | 2013 | (let (window pos file) |
dbcb9389 | 2014 | (save-excursion |
2aaa7f0a RS |
2015 | (setq window (posn-window (event-end event)) |
2016 | pos (posn-point (event-end event))) | |
2017 | (if (not (windowp window)) | |
2018 | (error "No file chosen")) | |
2019 | (set-buffer (window-buffer window)) | |
2020 | (goto-char pos) | |
2021 | (setq file (dired-get-file-for-visit))) | |
55a87770 RS |
2022 | (if (file-directory-p file) |
2023 | (or (and (cdr dired-subdir-alist) | |
2024 | (dired-goto-subdir file)) | |
2025 | (progn | |
2026 | (select-window window) | |
2027 | (dired-other-window file))) | |
3f68d7c8 RS |
2028 | (select-window window) |
2029 | (find-file-other-window (file-name-sans-versions file t))))) | |
b70ebe37 | 2030 | |
84fc2cfa | 2031 | (defun dired-view-file () |
ee680b2b | 2032 | "In Dired, examine a file in view mode, returning to Dired when done. |
b70ebe37 | 2033 | When file is a directory, show it in this buffer if it is inserted. |
3f68d7c8 | 2034 | Otherwise, display it in another buffer." |
84fc2cfa | 2035 | (interactive) |
68d2f12f RS |
2036 | (let ((file (dired-get-file-for-visit))) |
2037 | (if (file-directory-p file) | |
2038 | (or (and (cdr dired-subdir-alist) | |
2039 | (dired-goto-subdir file)) | |
2040 | (dired file)) | |
3f68d7c8 | 2041 | (view-file file)))) |
84fc2cfa ER |
2042 | |
2043 | (defun dired-find-file-other-window () | |
68d2f12f | 2044 | "In Dired, visit this file or directory in another window." |
84fc2cfa | 2045 | (interactive) |
68d2f12f | 2046 | (find-file-other-window (dired-get-file-for-visit))) |
ab67260b RS |
2047 | |
2048 | (defun dired-display-file () | |
68d2f12f | 2049 | "In Dired, display this file or directory in another window." |
ab67260b | 2050 | (interactive) |
68d2f12f | 2051 | (display-buffer (find-file-noselect (dired-get-file-for-visit)))) |
83fadedf | 2052 | \f |
68d2f12f | 2053 | ;;; Functions for extracting and manipulating file names in Dired buffers. |
84fc2cfa ER |
2054 | |
2055 | (defun dired-get-filename (&optional localp no-error-if-not-filep) | |
68d2f12f | 2056 | "In Dired, return name of file mentioned on this line. |
84fc2cfa | 2057 | Value returned normally includes the directory name. |
492d2437 | 2058 | Optional arg LOCALP with value `no-dir' means don't include directory |
147e214c LT |
2059 | name in result. A value of `verbatim' means to return the name exactly as |
2060 | it occurs in the buffer, and a value of t means construct name relative to | |
2061 | `default-directory', which still may contain slashes if in a subdirectory. | |
2062 | Optional arg NO-ERROR-IF-NOT-FILEP means treat `.' and `..' as | |
2063 | regular filenames and return nil if no filename on this line. | |
2064 | Otherwise, an error occurs in these cases." | |
a5e0e1a8 | 2065 | (let (case-fold-search file p1 p2 already-absolute) |
84fc2cfa | 2066 | (save-excursion |
492d2437 RS |
2067 | (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep))) |
2068 | (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep)))) | |
2069 | ;; nil if no file on this line, but no-error-if-not-filep is t: | |
9888aa65 RS |
2070 | (if (setq file (and p1 p2 (buffer-substring p1 p2))) |
2071 | (progn | |
2072 | ;; Get rid of the mouse-face property that file names have. | |
2073 | (set-text-properties 0 (length file) nil file) | |
2074 | ;; Unquote names quoted by ls or by dired-insert-directory. | |
1be9bd1e CY |
2075 | ;; This code was written using `read' to unquote, because |
2076 | ;; it's faster than substituting \007 (4 chars) -> ^G (1 | |
2077 | ;; char) etc. in a lisp loop. Unfortunately, this decision | |
2078 | ;; has necessitated hacks such as dealing with filenames | |
2079 | ;; with quotation marks in their names. | |
2080 | (while (string-match "\\(?:[^\\]\\|\\`\\)\\(\"\\)" file) | |
2081 | (setq file (replace-match "\\\"" nil t file 1))) | |
abef340a | 2082 | |
d1882ac7 EW |
2083 | (when (eq system-type 'windows-nt) |
2084 | (save-match-data | |
2085 | (let ((start 0)) | |
2086 | (while (string-match "\\\\" file start) | |
2087 | (aset file (match-beginning 0) ?/) | |
2088 | (setq start (match-end 0)))))) | |
2089 | ||
1be9bd1e | 2090 | (setq file (read (concat "\"" file "\""))) |
038b5501 KH |
2091 | ;; The above `read' will return a unibyte string if FILE |
2092 | ;; contains eight-bit-control/graphic characters. | |
2093 | (if (and enable-multibyte-characters | |
2094 | (not (multibyte-string-p file))) | |
2095 | (setq file (string-to-multibyte file))))) | |
f6e2cbe3 | 2096 | (and file (file-name-absolute-p file) |
7994d91a RS |
2097 | ;; A relative file name can start with ~. |
2098 | ;; Don't treat it as absolute in this context. | |
2099 | (not (eq (aref file 0) ?~)) | |
a5e0e1a8 | 2100 | (setq already-absolute t)) |
a5e0e1a8 | 2101 | (cond |
b95ddab3 RS |
2102 | ((null file) |
2103 | nil) | |
1d5ad120 JB |
2104 | ((eq localp 'verbatim) |
2105 | file) | |
e9407052 | 2106 | ((and (not no-error-if-not-filep) |
5709c1a0 | 2107 | (member file '("." ".."))) |
e9407052 | 2108 | (error "Cannot operate on `.' or `..'")) |
a5e0e1a8 EZ |
2109 | ((and (eq localp 'no-dir) already-absolute) |
2110 | (file-name-nondirectory file)) | |
b95ddab3 | 2111 | (already-absolute |
ca66efd1 RS |
2112 | (let ((handler (find-file-name-handler file nil))) |
2113 | ;; check for safe-magic property so that we won't | |
2114 | ;; put /: for names that don't really need them. | |
0b7bc76f | 2115 | ;; For instance, .gz files when auto-compression-mode is on. |
ca66efd1 RS |
2116 | (if (and handler (not (get handler 'safe-magic))) |
2117 | (concat "/:" file) | |
2118 | file))) | |
b95ddab3 | 2119 | ((eq localp 'no-dir) |
a5e0e1a8 | 2120 | file) |
b95ddab3 RS |
2121 | ((equal (dired-current-directory) "/") |
2122 | (setq file (concat (dired-current-directory localp) file)) | |
ca66efd1 RS |
2123 | (let ((handler (find-file-name-handler file nil))) |
2124 | ;; check for safe-magic property so that we won't | |
2125 | ;; put /: for names that don't really need them. | |
2126 | ;; For instance, .gz files when auto-compression-mode is on. | |
2127 | (if (and handler (not (get handler 'safe-magic))) | |
2128 | (concat "/:" file) | |
2129 | file))) | |
a5e0e1a8 | 2130 | (t |
b95ddab3 | 2131 | (concat (dired-current-directory localp) file))))) |
492d2437 | 2132 | |
437fe5ae | 2133 | (defun dired-string-replace-match (regexp string newtext |
1d5ad120 | 2134 | &optional literal global) |
437fe5ae RS |
2135 | "Replace first match of REGEXP in STRING with NEWTEXT. |
2136 | If it does not match, nil is returned instead of the new string. | |
2137 | Optional arg LITERAL means to take NEWTEXT literally. | |
2138 | Optional arg GLOBAL means to replace all matches." | |
2139 | (if global | |
b976e099 | 2140 | (let ((start 0) ret) |
856321e2 RS |
2141 | (while (string-match regexp string start) |
2142 | (let ((from-end (- (length string) (match-end 0)))) | |
b976e099 | 2143 | (setq ret (setq string (replace-match newtext t literal string))) |
856321e2 | 2144 | (setq start (- (length string) from-end)))) |
b976e099 | 2145 | ret) |
437fe5ae RS |
2146 | (if (not (string-match regexp string 0)) |
2147 | nil | |
856321e2 | 2148 | (replace-match newtext t literal string)))) |
437fe5ae | 2149 | |
492d2437 | 2150 | (defun dired-make-absolute (file &optional dir) |
470fa6d1 | 2151 | ;;"Convert FILE (a file name relative to DIR) to an absolute file name." |
492d2437 RS |
2152 | ;; We can't always use expand-file-name as this would get rid of `.' |
2153 | ;; or expand in / instead default-directory if DIR=="". | |
7c2fb837 | 2154 | ;; This should be good enough for ange-ftp. |
492d2437 RS |
2155 | ;; It should be reasonably fast, though, as it is called in |
2156 | ;; dired-get-filename. | |
2157 | (concat (or dir default-directory) file)) | |
2158 | ||
d032d5e7 | 2159 | (defun dired-make-relative (file &optional dir _ignore) |
827dc60d RS |
2160 | "Convert FILE (an absolute file name) to a name relative to DIR. |
2161 | If this is impossible, return FILE unchanged. | |
2162 | DIR must be a directory name, not a file name." | |
492d2437 | 2163 | (or dir (setq dir default-directory)) |
7425bbff RS |
2164 | ;; This case comes into play if default-directory is set to |
2165 | ;; use ~. | |
2166 | (if (and (> (length dir) 0) (= (aref dir 0) ?~)) | |
2167 | (setq dir (expand-file-name dir))) | |
492d2437 RS |
2168 | (if (string-match (concat "^" (regexp-quote dir)) file) |
2169 | (substring file (match-end 0)) | |
7805cdbd | 2170 | ;;; (or no-error |
827dc60d RS |
2171 | ;;; (error "%s: not in directory tree growing at %s" file dir)) |
2172 | file)) | |
83fadedf | 2173 | \f |
492d2437 RS |
2174 | ;;; Functions for finding the file name in a dired buffer line. |
2175 | ||
23e217f6 RS |
2176 | (defvar dired-permission-flags-regexp |
2177 | "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)" | |
2178 | "Regular expression to match the permission flags in `ls -l'.") | |
2179 | ||
492d2437 RS |
2180 | ;; Move to first char of filename on this line. |
2181 | ;; Returns position (point) or nil if no filename on this line." | |
2182 | (defun dired-move-to-filename (&optional raise-error eol) | |
a12c6dca SM |
2183 | "Move to the beginning of the filename on the current line. |
2184 | Return the position of the beginning of the filename, or nil if none found." | |
492d2437 | 2185 | ;; This is the UNIX version. |
2b2059d8 | 2186 | (or eol (setq eol (line-end-position))) |
492d2437 | 2187 | (beginning-of-line) |
0b7bc76f | 2188 | ;; First try assuming `ls --dired' was used. |
2b2059d8 SM |
2189 | (let ((change (next-single-property-change (point) 'dired-filename nil eol))) |
2190 | (cond | |
2191 | ((and change (< change eol)) | |
2192 | (goto-char change)) | |
9bc260cf | 2193 | ((re-search-forward directory-listing-before-filename-regexp eol t) |
2b2059d8 SM |
2194 | (goto-char (match-end 0))) |
2195 | ((re-search-forward dired-permission-flags-regexp eol t) | |
2196 | ;; Ha! There *is* a file. Our regexp-from-hell just failed to find it. | |
1f3b4d04 | 2197 | (if raise-error |
9bc260cf | 2198 | (error "Unrecognized line! Check directory-listing-before-filename-regexp")) |
1f3b4d04 SM |
2199 | (beginning-of-line) |
2200 | nil) | |
2b2059d8 SM |
2201 | (raise-error |
2202 | (error "No file on this line"))))) | |
492d2437 RS |
2203 | |
2204 | (defun dired-move-to-end-of-filename (&optional no-error) | |
2205 | ;; Assumes point is at beginning of filename, | |
2206 | ;; thus the rwx bit re-search-backward below will succeed in *this* | |
2207 | ;; line if at all. So, it should be called only after | |
2208 | ;; (dired-move-to-filename t). | |
2209 | ;; On failure, signals an error (with non-nil NO-ERROR just returns nil). | |
2210 | ;; This is the UNIX version. | |
0b7bc76f RS |
2211 | (if (get-text-property (point) 'dired-filename) |
2212 | (goto-char (next-single-property-change (point) 'dired-filename)) | |
2213 | (let (opoint file-type executable symlink hidden case-fold-search used-F eol) | |
2214 | ;; case-fold-search is nil now, so we can test for capital F: | |
2215 | (setq used-F (string-match "F" dired-actual-switches) | |
2216 | opoint (point) | |
5ed619e0 | 2217 | eol (line-end-position) |
0b7bc76f RS |
2218 | hidden (and selective-display |
2219 | (save-excursion (search-forward "\r" eol t)))) | |
2220 | (if hidden | |
2221 | nil | |
2222 | (save-excursion ;; Find out what kind of file this is: | |
2223 | ;; Restrict perm bits to be non-blank, | |
2224 | ;; otherwise this matches one char to early (looking backward): | |
2225 | ;; "l---------" (some systems make symlinks that way) | |
2226 | ;; "----------" (plain file with zero perms) | |
2227 | (if (re-search-backward | |
2228 | dired-permission-flags-regexp nil t) | |
2229 | (setq file-type (char-after (match-beginning 1)) | |
2230 | symlink (eq file-type ?l) | |
2231 | ;; Only with -F we need to know whether it's an executable | |
2232 | executable (and | |
2233 | used-F | |
2234 | (string-match | |
2235 | "[xst]" ;; execute bit set anywhere? | |
2236 | (concat | |
ff3d76aa SM |
2237 | (match-string 2) |
2238 | (match-string 3) | |
2239 | (match-string 4))))) | |
0b7bc76f RS |
2240 | (or no-error (error "No file on this line")))) |
2241 | ;; Move point to end of name: | |
2242 | (if symlink | |
a12c6dca | 2243 | (if (search-forward " -> " eol t) |
0b7bc76f | 2244 | (progn |
a12c6dca | 2245 | (forward-char -4) |
0b7bc76f RS |
2246 | (and used-F |
2247 | dired-ls-F-marks-symlinks | |
2248 | (eq (preceding-char) ?@) ;; did ls really mark the link? | |
2249 | (forward-char -1)))) | |
2250 | (goto-char eol) ;; else not a symbolic link | |
dd52fff6 TTN |
2251 | ;; ls -lF marks dirs, sockets, fifos and executables with exactly |
2252 | ;; one trailing character. (Executable bits on symlinks ain't mean | |
0b7bc76f RS |
2253 | ;; a thing, even to ls, but we know it's not a symlink.) |
2254 | (and used-F | |
dd52fff6 | 2255 | (or (memq file-type '(?d ?s ?p)) |
0b7bc76f RS |
2256 | executable) |
2257 | (forward-char -1)))) | |
2258 | (or no-error | |
2259 | (not (eq opoint (point))) | |
8c16bd8c | 2260 | (error "%s" (if hidden |
0b7bc76f RS |
2261 | (substitute-command-keys |
2262 | "File line is hidden, type \\[dired-hide-subdir] to unhide") | |
2263 | "No file on this line"))) | |
2264 | (if (eq opoint (point)) | |
2265 | nil | |
2266 | (point))))) | |
492d2437 | 2267 | |
83fadedf | 2268 | \f |
a3f4a3ef RS |
2269 | ;;; COPY NAMES OF MARKED FILES INTO KILL-RING. |
2270 | ||
2271 | (defun dired-copy-filename-as-kill (&optional arg) | |
2272 | "Copy names of marked (or next ARG) files into the kill ring. | |
2273 | The names are separated by a space. | |
470fa6d1 | 2274 | With a zero prefix arg, use the absolute file name of each marked file. |
ee680b2b | 2275 | With \\[universal-argument], use the file name relative to the dired buffer's |
afda9919 | 2276 | `default-directory'. (This still may contain slashes if in a subdirectory.) |
a3f4a3ef | 2277 | |
afda9919 LT |
2278 | If on a subdir headerline, use absolute subdirname instead; |
2279 | prefix arg and marked files are ignored in this case. | |
a3f4a3ef RS |
2280 | |
2281 | You can then feed the file name(s) to other commands with \\[yank]." | |
2282 | (interactive "P") | |
2283 | (let ((string | |
2284 | (or (dired-get-subdir) | |
2285 | (mapconcat (function identity) | |
2286 | (if arg | |
2287 | (cond ((zerop (prefix-numeric-value arg)) | |
2288 | (dired-get-marked-files)) | |
afda9919 LT |
2289 | ((consp arg) |
2290 | (dired-get-marked-files t)) | |
2291 | (t | |
2292 | (dired-get-marked-files | |
2293 | 'no-dir (prefix-numeric-value arg)))) | |
a3f4a3ef RS |
2294 | (dired-get-marked-files 'no-dir)) |
2295 | " ")))) | |
4de547e4 RS |
2296 | (if (eq last-command 'kill-region) |
2297 | (kill-append string nil) | |
2298 | (kill-new string)) | |
a3f4a3ef RS |
2299 | (message "%s" string))) |
2300 | ||
2301 | \f | |
492d2437 RS |
2302 | ;; Keeping Dired buffers in sync with the filesystem and with each other |
2303 | ||
ef746164 | 2304 | (defun dired-buffers-for-dir (dir &optional file) |
dad7c716 | 2305 | ;; Return a list of buffers for DIR (top level or in-situ subdir). |
ef746164 RS |
2306 | ;; If FILE is non-nil, include only those whose wildcard pattern (if any) |
2307 | ;; matches FILE. | |
492d2437 RS |
2308 | ;; The list is in reverse order of buffer creation, most recent last. |
2309 | ;; As a side effect, killed dired buffers for DIR are removed from | |
2310 | ;; dired-buffers. | |
2311 | (setq dir (file-name-as-directory dir)) | |
dad7c716 CY |
2312 | (let (result buf) |
2313 | (dolist (elt dired-buffers) | |
2314 | (setq buf (cdr elt)) | |
2315 | (cond | |
2316 | ((null (buffer-name buf)) | |
2317 | ;; Buffer is killed - clean up: | |
38819778 | 2318 | (setq dired-buffers (delq elt dired-buffers))) |
dad7c716 CY |
2319 | ((dired-in-this-tree dir (car elt)) |
2320 | (with-current-buffer buf | |
2321 | (and (assoc dir dired-subdir-alist) | |
2322 | (or (null file) | |
2323 | (if (stringp dired-directory) | |
2324 | (let ((wildcards (file-name-nondirectory | |
2325 | dired-directory))) | |
2326 | (or (= 0 (length wildcards)) | |
2327 | (string-match (dired-glob-regexp wildcards) | |
2328 | file))) | |
2329 | (member (expand-file-name file dir) | |
2330 | (cdr dired-directory)))) | |
2331 | (setq result (cons buf result))))))) | |
492d2437 RS |
2332 | result)) |
2333 | ||
ef746164 RS |
2334 | (defun dired-glob-regexp (pattern) |
2335 | "Convert glob-pattern PATTERN to a regular expression." | |
2336 | (let ((matched-in-pattern 0) ;; How many chars of PATTERN we've handled. | |
2337 | regexp) | |
2338 | (while (string-match "[[?*]" pattern matched-in-pattern) | |
2339 | (let ((op-end (match-end 0)) | |
2340 | (next-op (aref pattern (match-beginning 0)))) | |
2341 | (setq regexp (concat regexp | |
2342 | (regexp-quote | |
2343 | (substring pattern matched-in-pattern | |
2344 | (match-beginning 0))))) | |
2345 | (cond ((= next-op ??) | |
2346 | (setq regexp (concat regexp ".")) | |
2347 | (setq matched-in-pattern op-end)) | |
2348 | ((= next-op ?\[) | |
2349 | ;; Fails to handle ^ yet ???? | |
2350 | (let* ((set-start (match-beginning 0)) | |
2351 | (set-cont | |
2352 | (if (= (aref pattern (1+ set-start)) ?^) | |
2353 | (+ 3 set-start) | |
2354 | (+ 2 set-start))) | |
2355 | (set-end (string-match "]" pattern set-cont)) | |
2356 | (set (substring pattern set-start (1+ set-end)))) | |
2357 | (setq regexp (concat regexp set)) | |
2358 | (setq matched-in-pattern (1+ set-end)))) | |
2359 | ((= next-op ?*) | |
2360 | (setq regexp (concat regexp ".*")) | |
2361 | (setq matched-in-pattern op-end))))) | |
2362 | (concat "\\`" | |
2363 | regexp | |
2364 | (regexp-quote | |
2365 | (substring pattern matched-in-pattern)) | |
2366 | "\\'"))) | |
2367 | ||
c60ee5e7 | 2368 | |
ef746164 | 2369 | |
492d2437 RS |
2370 | (defun dired-advertise () |
2371 | ;;"Advertise in variable `dired-buffers' that we dired `default-directory'." | |
2372 | ;; With wildcards we actually advertise too much. | |
38819778 RS |
2373 | (let ((expanded-default (expand-file-name default-directory))) |
2374 | (if (memq (current-buffer) (dired-buffers-for-dir expanded-default)) | |
2375 | t ; we have already advertised ourselves | |
2376 | (setq dired-buffers | |
2377 | (cons (cons expanded-default (current-buffer)) | |
2378 | dired-buffers))))) | |
492d2437 RS |
2379 | |
2380 | (defun dired-unadvertise (dir) | |
2381 | ;; Remove DIR from the buffer alist in variable dired-buffers. | |
2382 | ;; This has the effect of removing any buffer whose main directory is DIR. | |
2383 | ;; It does not affect buffers in which DIR is a subdir. | |
2384 | ;; Removing is also done as a side-effect in dired-buffer-for-dir. | |
2385 | (setq dired-buffers | |
38819778 | 2386 | (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers))) |
83fadedf | 2387 | \f |
492d2437 RS |
2388 | ;; Tree Dired |
2389 | ||
2390 | ;;; utility functions | |
2391 | ||
2392 | (defun dired-in-this-tree (file dir) | |
2393 | ;;"Is FILE part of the directory tree starting at DIR?" | |
2394 | (let (case-fold-search) | |
38819778 | 2395 | (string-match (concat "^" (regexp-quote dir)) file))) |
492d2437 RS |
2396 | |
2397 | (defun dired-normalize-subdir (dir) | |
470fa6d1 KS |
2398 | ;; Prepend default-directory to DIR if relative file name. |
2399 | ;; dired-get-filename must be able to make a valid file name from a | |
492d2437 RS |
2400 | ;; file and its directory DIR. |
2401 | (file-name-as-directory | |
2402 | (if (file-name-absolute-p dir) | |
2403 | dir | |
2404 | (expand-file-name dir default-directory)))) | |
2405 | ||
2406 | (defun dired-get-subdir () | |
2407 | ;;"Return the subdir name on this line, or nil if not on a headerline." | |
2408 | ;; Look up in the alist whether this is a headerline. | |
2409 | (save-excursion | |
2410 | (let ((cur-dir (dired-current-directory))) | |
2411 | (beginning-of-line) ; alist stores b-o-l positions | |
2412 | (and (zerop (- (point) | |
2413 | (dired-get-subdir-min (assoc cur-dir | |
2414 | dired-subdir-alist)))) | |
2415 | cur-dir)))) | |
2416 | ||
2417 | ;(defun dired-get-subdir-min (elt) | |
2418 | ; (cdr elt)) | |
2419 | ;; can't use macro, must be redefinable for other alist format in dired-nstd. | |
62f61df0 | 2420 | (defalias 'dired-get-subdir-min 'cdr) |
492d2437 RS |
2421 | |
2422 | (defun dired-get-subdir-max (elt) | |
84fc2cfa | 2423 | (save-excursion |
492d2437 RS |
2424 | (goto-char (dired-get-subdir-min elt)) |
2425 | (dired-subdir-max))) | |
2426 | ||
2427 | (defun dired-clear-alist () | |
2428 | (while dired-subdir-alist | |
2429 | (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil) | |
2430 | (setq dired-subdir-alist (cdr dired-subdir-alist)))) | |
2431 | ||
c9d59fc2 RS |
2432 | (defun dired-subdir-index (dir) |
2433 | ;; Return an index into alist for use with nth | |
2434 | ;; for the sake of subdir moving commands. | |
2435 | (let (found (index 0) (alist dired-subdir-alist)) | |
2436 | (while alist | |
2437 | (if (string= dir (car (car alist))) | |
2438 | (setq alist nil found t) | |
2439 | (setq alist (cdr alist) index (1+ index)))) | |
2440 | (if found index nil))) | |
2441 | ||
2442 | (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip) | |
2443 | "Go to next subdirectory, regardless of level." | |
2444 | ;; Use 0 arg to go to this directory's header line. | |
2445 | ;; NO-SKIP prevents moving to end of header line, returning whatever | |
2446 | ;; position was found in dired-subdir-alist. | |
2447 | (interactive "p") | |
2448 | (let ((this-dir (dired-current-directory)) | |
2449 | pos index) | |
2450 | ;; nth with negative arg does not return nil but the first element | |
2451 | (setq index (- (dired-subdir-index this-dir) arg)) | |
2452 | (setq pos (if (>= index 0) | |
2453 | (dired-get-subdir-min (nth index dired-subdir-alist)))) | |
2454 | (if pos | |
2455 | (progn | |
2456 | (goto-char pos) | |
2457 | (or no-skip (skip-chars-forward "^\n\r")) | |
2458 | (point)) | |
2459 | (if no-error-if-not-found | |
2460 | nil ; return nil if not found | |
2461 | (error "%s directory" (if (> arg 0) "Last" "First")))))) | |
2462 | ||
3c13627a | 2463 | (defun dired-build-subdir-alist (&optional switches) |
492d2437 | 2464 | "Build `dired-subdir-alist' by parsing the buffer. |
3c13627a EZ |
2465 | Returns the new value of the alist. |
2466 | If optional arg SWITCHES is non-nil, use its value | |
2467 | instead of `dired-actual-switches'." | |
492d2437 RS |
2468 | (interactive) |
2469 | (dired-clear-alist) | |
2470 | (save-excursion | |
3c13627a | 2471 | (let* ((count 0) |
f836b98e | 2472 | (inhibit-read-only t) |
784a48b2 | 2473 | (buffer-undo-list t) |
3c13627a EZ |
2474 | (switches (or switches dired-actual-switches)) |
2475 | new-dir-name | |
2476 | (R-ftp-base-dir-regex | |
2477 | ;; Used to expand subdirectory names correctly in recursive | |
2478 | ;; ange-ftp listings. | |
2479 | (and (string-match "R" switches) | |
2480 | (string-match "\\`/.*:\\(/.*\\)" default-directory) | |
2481 | (concat "\\`" (match-string 1 default-directory))))) | |
84fc2cfa | 2482 | (goto-char (point-min)) |
492d2437 | 2483 | (setq dired-subdir-alist nil) |
a3d76960 RS |
2484 | (while (re-search-forward dired-subdir-regexp nil t) |
2485 | ;; Avoid taking a file name ending in a colon | |
2486 | ;; as a subdir name. | |
2487 | (unless (save-excursion | |
2488 | (goto-char (match-beginning 0)) | |
2489 | (beginning-of-line) | |
2490 | (forward-char 2) | |
2491 | (save-match-data (looking-at dired-re-perms))) | |
2492 | (save-excursion | |
2493 | (goto-char (match-beginning 1)) | |
2494 | (setq new-dir-name | |
2495 | (buffer-substring-no-properties (point) (match-end 1)) | |
2496 | new-dir-name | |
2497 | (save-match-data | |
2498 | (if (and R-ftp-base-dir-regex | |
2499 | (not (string= new-dir-name default-directory)) | |
2500 | (string-match R-ftp-base-dir-regex new-dir-name)) | |
2501 | (concat default-directory | |
2502 | (substring new-dir-name (match-end 0))) | |
2503 | (expand-file-name new-dir-name)))) | |
2504 | (delete-region (point) (match-end 1)) | |
7de3f9a4 RS |
2505 | (insert new-dir-name)) |
2506 | (setq count (1+ count)) | |
2507 | (dired-alist-add-1 new-dir-name | |
2508 | ;; Place a sub directory boundary between lines. | |
2509 | (save-excursion | |
2510 | (goto-char (match-beginning 0)) | |
2511 | (beginning-of-line) | |
2512 | (point-marker))))) | |
32226619 | 2513 | (if (and (> count 1) (called-interactively-p 'interactive)) |
a3d76960 RS |
2514 | (message "Buffer includes %d directories" count))) |
2515 | ;; We don't need to sort it because it is in buffer order per | |
2516 | ;; constructionem. Return new alist: | |
2517 | dired-subdir-alist)) | |
492d2437 RS |
2518 | |
2519 | (defun dired-alist-add-1 (dir new-marker) | |
2520 | ;; Add new DIR at NEW-MARKER. Don't sort. | |
2521 | (setq dired-subdir-alist | |
2522 | (cons (cons (dired-normalize-subdir dir) new-marker) | |
2523 | dired-subdir-alist))) | |
2524 | ||
2525 | (defun dired-goto-next-nontrivial-file () | |
2526 | ;; Position point on first nontrivial file after point. | |
2527 | (dired-goto-next-file);; so there is a file to compare with | |
2528 | (if (stringp dired-trivial-filenames) | |
2529 | (while (and (not (eobp)) | |
2530 | (string-match dired-trivial-filenames | |
2531 | (file-name-nondirectory | |
2532 | (or (dired-get-filename nil t) "")))) | |
2533 | (forward-line 1) | |
2534 | (dired-move-to-filename)))) | |
2535 | ||
2536 | (defun dired-goto-next-file () | |
2537 | (let ((max (1- (dired-subdir-max)))) | |
2538 | (while (and (not (dired-move-to-filename)) (< (point) max)) | |
2539 | (forward-line 1)))) | |
2540 | ||
2541 | (defun dired-goto-file (file) | |
e19d2d64 | 2542 | "Go to line describing file FILE in this dired buffer." |
492d2437 | 2543 | ;; Return value of point on success, else nil. |
470fa6d1 | 2544 | ;; FILE must be an absolute file name. |
492d2437 RS |
2545 | ;; Loses if FILE contains control chars like "\007" for which ls |
2546 | ;; either inserts "?" or "\\007" into the buffer, so we won't find | |
2547 | ;; it in the buffer. | |
2548 | (interactive | |
2549 | (prog1 ; let push-mark display its message | |
2550 | (list (expand-file-name | |
2551 | (read-file-name "Goto file: " | |
2552 | (dired-current-directory)))) | |
2553 | (push-mark))) | |
2554 | (setq file (directory-file-name file)) ; does no harm if no directory | |
2555 | (let (found case-fold-search dir) | |
2556 | (setq dir (or (file-name-directory file) | |
f808da9e | 2557 | (error "File name `%s' is not absolute" file))) |
492d2437 RS |
2558 | (save-excursion |
2559 | ;; The hair here is to get the result of dired-goto-subdir | |
2560 | ;; without really calling it if we don't have any subdirs. | |
233993a3 | 2561 | (if (if (string= dir (expand-file-name default-directory)) |
492d2437 | 2562 | (goto-char (point-min)) |
7b2469ae | 2563 | (and (cdr dired-subdir-alist) |
492d2437 RS |
2564 | (dired-goto-subdir dir))) |
2565 | (let ((base (file-name-nondirectory file)) | |
f808da9e | 2566 | search-string |
492d2437 | 2567 | (boundary (dired-subdir-max))) |
f808da9e RS |
2568 | (setq search-string |
2569 | (replace-regexp-in-string "\^m" "\\^m" base nil t)) | |
2570 | (setq search-string | |
2571 | (replace-regexp-in-string "\\\\" "\\\\" search-string nil t)) | |
492d2437 RS |
2572 | (while (and (not found) |
2573 | ;; filenames are preceded by SPC, this makes | |
2574 | ;; the search faster (e.g. for the filename "-"!). | |
f808da9e RS |
2575 | (search-forward (concat " " search-string) |
2576 | boundary 'move)) | |
492d2437 RS |
2577 | ;; Match could have BASE just as initial substring or |
2578 | ;; or in permission bits or date or | |
2579 | ;; not be a proper filename at all: | |
2580 | (if (equal base (dired-get-filename 'no-dir t)) | |
2581 | ;; Must move to filename since an (actually | |
2582 | ;; correct) match could have been elsewhere on the | |
2583 | ;; ;; line (e.g. "-" would match somewhere in the | |
2584 | ;; permission bits). | |
55e86af6 RS |
2585 | (setq found (dired-move-to-filename)) |
2586 | ;; If this isn't the right line, move forward to avoid | |
2587 | ;; trying this line again. | |
2588 | (forward-line 1)))))) | |
492d2437 RS |
2589 | (and found |
2590 | ;; return value of point (i.e., FOUND): | |
2591 | (goto-char found)))) | |
2592 | ||
203784cc GM |
2593 | (defvar dired-find-subdir) |
2594 | ||
2595 | ;; FIXME document whatever dired-x is doing. | |
492d2437 | 2596 | (defun dired-initial-position (dirname) |
203784cc GM |
2597 | "Where point should go in a new listing of DIRNAME. |
2598 | Point assumed at beginning of new subdir line." | |
492d2437 | 2599 | (end-of-line) |
203784cc GM |
2600 | (and (featurep 'dired-x) dired-find-subdir |
2601 | (dired-goto-subdir dirname)) | |
492d2437 | 2602 | (if dired-trivial-filenames (dired-goto-next-nontrivial-file))) |
83fadedf | 2603 | \f |
492d2437 RS |
2604 | ;; These are hooks which make tree dired work. |
2605 | ;; They are in this file because other parts of dired need to call them. | |
2606 | ;; But they don't call the rest of tree dired unless there are subdirs loaded. | |
2607 | ||
2608 | ;; This function is called for each retrieved filename. | |
2609 | ;; It could stand to be faster, though it's mostly function call | |
2610 | ;; overhead. Avoiding the function call seems to save about 10% in | |
2611 | ;; dired-get-filename. Make it a defsubst? | |
2612 | (defun dired-current-directory (&optional localp) | |
2613 | "Return the name of the subdirectory to which this line belongs. | |
2614 | This returns a string with trailing slash, like `default-directory'. | |
2615 | Optional argument means return a file name relative to `default-directory'." | |
2616 | (let ((here (point)) | |
2617 | (alist (or dired-subdir-alist | |
2618 | ;; probably because called in a non-dired buffer | |
2619 | (error "No subdir-alist in %s" (current-buffer)))) | |
2620 | elt dir) | |
2621 | (while alist | |
2622 | (setq elt (car alist) | |
2623 | dir (car elt) | |
2624 | ;; use `<=' (not `<') as subdir line is part of subdir | |
2625 | alist (if (<= (dired-get-subdir-min elt) here) | |
2626 | nil ; found | |
2627 | (cdr alist)))) | |
2628 | (if localp | |
2629 | (dired-make-relative dir default-directory) | |
2630 | dir))) | |
2631 | ||
2632 | ;; Subdirs start at the beginning of their header lines and end just | |
2633 | ;; before the beginning of the next header line (or end of buffer). | |
2634 | ||
2635 | (defun dired-subdir-max () | |
2636 | (save-excursion | |
7b2469ae | 2637 | (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t))) |
492d2437 RS |
2638 | (point-max) |
2639 | (point)))) | |
83fadedf | 2640 | \f |
492d2437 RS |
2641 | ;; Deleting files |
2642 | ||
40aa8257 | 2643 | (defcustom dired-recursive-deletes 'top |
9201cc28 | 2644 | "Decide whether recursive deletes are allowed. |
098bab71 | 2645 | A value of nil means no recursive deletes. |
f0628026 RS |
2646 | `always' means delete recursively without asking. This is DANGEROUS! |
2647 | `top' means ask for each directory at top level, but delete its subdirectories | |
2648 | without asking. | |
2649 | Anything else means ask for each directory." | |
53ade5c7 RS |
2650 | :type '(choice :tag "Delete non-empty directories" |
2651 | (const :tag "Yes" always) | |
2652 | (const :tag "No--only delete empty directories" nil) | |
2653 | (const :tag "Confirm for each directory" t) | |
2654 | (const :tag "Confirm for each top directory only" top)) | |
f0628026 RS |
2655 | :group 'dired) |
2656 | ||
c60ee5e7 | 2657 | ;; Match anything but `.' and `..'. |
f0628026 RS |
2658 | (defvar dired-re-no-dot "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*") |
2659 | ||
2660 | ;; Delete file, possibly delete a directory and all its files. | |
2661 | ;; This function is usefull outside of dired. One could change it's name | |
2662 | ;; to e.g. recursive-delete-file and put it somewhere else. | |
f1a5d776 | 2663 | (defun dired-delete-file (file &optional recursive trash) "\ |
f0628026 RS |
2664 | Delete FILE or directory (possibly recursively if optional RECURSIVE is true.) |
2665 | RECURSIVE determines what to do with a non-empty directory. If RECURSIVE is: | |
ee680b2b | 2666 | nil, do not delete. |
f0628026 RS |
2667 | `always', delete recursively without asking. |
2668 | `top', ask for each directory at top level. | |
2669 | Anything else, ask for each sub-directory." | |
56808ea0 MA |
2670 | ;; This test is equivalent to |
2671 | ;; (and (file-directory-p fn) (not (file-symlink-p fn))) | |
2672 | ;; but more efficient | |
2673 | (if (not (eq t (car (file-attributes file)))) | |
f1a5d776 | 2674 | (delete-file file trash) |
56808ea0 MA |
2675 | (if (and recursive |
2676 | (directory-files file t dired-re-no-dot) ; Not empty. | |
2677 | (or (eq recursive 'always) | |
f1a5d776 CY |
2678 | (yes-or-no-p (format "Recursively %s %s? " |
2679 | (if (and trash | |
2680 | delete-by-moving-to-trash) | |
2681 | "trash" | |
2682 | "delete") | |
56808ea0 | 2683 | (dired-make-relative file))))) |
f0628026 | 2684 | (if (eq recursive 'top) (setq recursive 'always)) ; Don't ask again. |
56808ea0 | 2685 | (setq recursive nil)) |
f1a5d776 | 2686 | (delete-directory file recursive trash))) |
f0628026 | 2687 | |
7da0e9b4 | 2688 | (defun dired-do-flagged-delete (&optional nomessage) |
68d2f12f | 2689 | "In Dired, delete the files flagged for deletion. |
7da0e9b4 | 2690 | If NOMESSAGE is non-nil, we don't display any message |
bce1acc3 | 2691 | if there are no flagged files. |
40aa8257 JL |
2692 | `dired-recursive-deletes' controls whether deletion of |
2693 | non-empty directories is allowed." | |
492d2437 RS |
2694 | (interactive) |
2695 | (let* ((dired-marker-char dired-del-marker) | |
2696 | (regexp (dired-marker-regexp)) | |
2697 | case-fold-search) | |
2698 | (if (save-excursion (goto-char (point-min)) | |
2699 | (re-search-forward regexp nil t)) | |
2700 | (dired-internal-do-deletions | |
2701 | ;; this can't move point since ARG is nil | |
2702 | (dired-map-over-marks (cons (dired-get-filename) (point)) | |
2703 | nil) | |
f1a5d776 | 2704 | nil t) |
7da0e9b4 RS |
2705 | (or nomessage |
2706 | (message "(No deletions requested)"))))) | |
492d2437 RS |
2707 | |
2708 | (defun dired-do-delete (&optional arg) | |
bce1acc3 | 2709 | "Delete all marked (or next ARG) files. |
40aa8257 JL |
2710 | `dired-recursive-deletes' controls whether deletion of |
2711 | non-empty directories is allowed." | |
492d2437 RS |
2712 | ;; This is more consistent with the file marking feature than |
2713 | ;; dired-do-flagged-delete. | |
2714 | (interactive "P") | |
2715 | (dired-internal-do-deletions | |
2716 | ;; this may move point if ARG is an integer | |
2717 | (dired-map-over-marks (cons (dired-get-filename) (point)) | |
2718 | arg) | |
f1a5d776 | 2719 | arg t)) |
492d2437 RS |
2720 | |
2721 | (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p? | |
2722 | ||
f1a5d776 | 2723 | (defun dired-internal-do-deletions (l arg &optional trash) |
492d2437 RS |
2724 | ;; L is an alist of files to delete, with their buffer positions. |
2725 | ;; ARG is the prefix arg. | |
7c2fb837 | 2726 | ;; Filenames are absolute. |
492d2437 RS |
2727 | ;; (car L) *must* be the *last* (bottommost) file in the dired buffer. |
2728 | ;; That way as changes are made in the buffer they do not shift the | |
2729 | ;; lines still to be changed, so the (point) values in L stay valid. | |
2730 | ;; Also, for subdirs in natural order, a subdir's files are deleted | |
2731 | ;; before the subdir itself - the other way around would not work. | |
f1a5d776 CY |
2732 | (let* ((files (mapcar (function car) l)) |
2733 | (count (length l)) | |
2734 | (succ 0) | |
2735 | (trashing (and trash delete-by-moving-to-trash)) | |
2736 | (progress-reporter | |
2737 | (make-progress-reporter | |
2738 | (if trashing "Trashing..." "Deleting...") | |
2739 | succ count))) | |
492d2437 RS |
2740 | ;; canonicalize file list for pop up |
2741 | (setq files (nreverse (mapcar (function dired-make-relative) files))) | |
2742 | (if (dired-mark-pop-up | |
2743 | " *Deletions*" 'delete files dired-deletion-confirmer | |
f1a5d776 CY |
2744 | (format "%s %s " |
2745 | (if trashing "Trash" "Delete") | |
2746 | (dired-mark-prompt arg files))) | |
84fc2cfa | 2747 | (save-excursion |
492d2437 RS |
2748 | (let (failures);; files better be in reverse order for this loop! |
2749 | (while l | |
2750 | (goto-char (cdr (car l))) | |
f836b98e | 2751 | (let ((inhibit-read-only t)) |
492d2437 RS |
2752 | (condition-case err |
2753 | (let ((fn (car (car l)))) | |
f1a5d776 | 2754 | (dired-delete-file fn dired-recursive-deletes trash) |
492d2437 RS |
2755 | ;; if we get here, removing worked |
2756 | (setq succ (1+ succ)) | |
f1a5d776 | 2757 | (progress-reporter-update progress-reporter succ) |
bf989169 RS |
2758 | (dired-fun-in-all-buffers |
2759 | (file-name-directory fn) (file-name-nondirectory fn) | |
2760 | (function dired-delete-entry) fn)) | |
492d2437 RS |
2761 | (error;; catch errors from failed deletions |
2762 | (dired-log "%s\n" err) | |
2763 | (setq failures (cons (car (car l)) failures))))) | |
2764 | (setq l (cdr l))) | |
2765 | (if (not failures) | |
f1a5d776 | 2766 | (progress-reporter-done progress-reporter) |
492d2437 RS |
2767 | (dired-log-summary |
2768 | (format "%d of %d deletion%s failed" | |
2769 | (length failures) count | |
2770 | (dired-plural-s count)) | |
2771 | failures)))) | |
2772 | (message "(No deletions performed)"))) | |
2773 | (dired-move-to-filename)) | |
2774 | ||
bf989169 RS |
2775 | (defun dired-fun-in-all-buffers (directory file fun &rest args) |
2776 | ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS. | |
2777 | ;; If the buffer has a wildcard pattern, check that it matches FILE. | |
2778 | ;; (FILE does not include a directory component.) | |
2779 | ;; FILE may be nil, in which case ignore it. | |
2780 | ;; Return list of buffers where FUN succeeded (i.e., returned non-nil). | |
2781 | (let (success-list) | |
2782 | (dolist (buf (dired-buffers-for-dir (expand-file-name directory) | |
2783 | file)) | |
2784 | (with-current-buffer buf | |
2785 | (if (apply fun args) | |
2786 | (setq success-list (cons (buffer-name buf) success-list))))) | |
2787 | success-list)) | |
2788 | ||
c60ee5e7 | 2789 | ;; Delete the entry for FILE from |
bf989169 RS |
2790 | (defun dired-delete-entry (file) |
2791 | (save-excursion | |
2792 | (and (dired-goto-file file) | |
f836b98e | 2793 | (let ((inhibit-read-only t)) |
bf989169 RS |
2794 | (delete-region (progn (beginning-of-line) (point)) |
2795 | (save-excursion (forward-line 1) (point)))))) | |
2796 | (dired-clean-up-after-deletion file)) | |
2797 | ||
42924231 GM |
2798 | (defvar dired-clean-up-buffers-too) |
2799 | ||
492d2437 | 2800 | (defun dired-clean-up-after-deletion (fn) |
42924231 GM |
2801 | "Clean up after a deleted file or directory FN. |
2802 | Removes any expanded subdirectory of deleted directory. | |
2803 | If `dired-x' is loaded and `dired-clean-up-buffers-too' is non-nil, | |
2804 | also offers to kill buffers visiting deleted files and directories." | |
7b2469ae RS |
2805 | (save-excursion (and (cdr dired-subdir-alist) |
2806 | (dired-goto-subdir fn) | |
42924231 GM |
2807 | (dired-kill-subdir))) |
2808 | ;; Offer to kill buffer of deleted file FN. | |
2809 | (when (and (featurep 'dired-x) dired-clean-up-buffers-too) | |
2810 | (let ((buf (get-file-buffer fn))) | |
2811 | (and buf | |
2812 | (funcall #'y-or-n-p | |
2813 | (format "Kill buffer of %s, too? " | |
2814 | (file-name-nondirectory fn))) | |
2815 | (kill-buffer buf))) | |
2816 | (let ((buf-list (dired-buffers-for-dir (expand-file-name fn)))) | |
2817 | (and buf-list | |
2818 | (y-or-n-p (format "Kill dired buffer%s of %s, too? " | |
2819 | (dired-plural-s (length buf-list)) | |
2820 | (file-name-nondirectory fn))) | |
2821 | (dolist (buf buf-list) | |
2822 | (kill-buffer buf)))))) | |
2823 | ||
83fadedf | 2824 | \f |
492d2437 RS |
2825 | ;; Confirmation |
2826 | ||
2827 | (defun dired-marker-regexp () | |
2828 | (concat "^" (regexp-quote (char-to-string dired-marker-char)))) | |
2829 | ||
2830 | (defun dired-plural-s (count) | |
2831 | (if (= 1 count) "" "s")) | |
2832 | ||
2833 | (defun dired-mark-prompt (arg files) | |
f69fd0d2 CY |
2834 | "Return a string suitable for use in a Dired prompt. |
2835 | ARG is normally the prefix argument for the calling command. | |
2836 | FILES should be a list of file names. | |
2837 | ||
2838 | The return value has a form like \"foo.txt\", \"[next 3 files]\", | |
2839 | or \"* [3 files]\"." | |
bb5527ce SM |
2840 | ;; distinguish-one-marked can cause the first element to be just t. |
2841 | (if (eq (car files) t) (setq files (cdr files))) | |
492d2437 RS |
2842 | (let ((count (length files))) |
2843 | (if (= count 1) | |
2844 | (car files) | |
2845 | ;; more than 1 file: | |
2846 | (if (integerp arg) | |
2847 | ;; abs(arg) = count | |
2848 | ;; Perhaps this is nicer, but it also takes more screen space: | |
2849 | ;;(format "[%s %d files]" (if (> arg 0) "next" "previous") | |
2850 | ;; count) | |
2851 | (format "[next %d files]" arg) | |
2852 | (format "%c [%d files]" dired-marker-char count))))) | |
2853 | ||
2854 | (defun dired-pop-to-buffer (buf) | |
7f0995a1 | 2855 | "Pop up buffer BUF in a way suitable for Dired." |
19998f14 MR |
2856 | (let ((split-window-preferred-function |
2857 | (lambda (window) | |
b50e9ccf MR |
2858 | (or (and (let ((split-height-threshold 0)) |
2859 | (window-splittable-p (selected-window))) | |
19998f14 MR |
2860 | ;; Try to split the selected window vertically if |
2861 | ;; that's possible. (Bug#1806) | |
2862 | (split-window-vertically)) | |
2863 | ;; Otherwise, try to split WINDOW sensibly. | |
6ed96c33 MR |
2864 | (split-window-sensibly window)))) |
2865 | pop-up-frames) | |
7f0995a1 | 2866 | (pop-to-buffer (get-buffer-create buf))) |
492d2437 | 2867 | ;; If dired-shrink-to-fit is t, make its window fit its contents. |
d7e78187 | 2868 | (when dired-shrink-to-fit |
4d0e7fe3 MR |
2869 | ;; Try to not delete window when we want to display less than |
2870 | ;; `window-min-height' lines. | |
2871 | (fit-window-to-buffer (get-buffer-window buf) nil 1))) | |
492d2437 | 2872 | |
5afea280 | 2873 | (defcustom dired-no-confirm nil |
9c0deccb | 2874 | "A list of symbols for commands Dired should not confirm, or t. |
0347d069 | 2875 | Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress', |
57654b8c | 2876 | `copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink', |
9c0deccb JB |
2877 | `touch' and `uncompress'. |
2878 | If t, confirmation is never needed." | |
5afea280 | 2879 | :group 'dired |
9c0deccb JB |
2880 | :type '(choice (const :tag "Confirmation never needed" t) |
2881 | (set (const byte-compile) (const chgrp) | |
2882 | (const chmod) (const chown) (const compress) | |
2883 | (const copy) (const delete) (const hardlink) | |
2884 | (const load) (const move) (const print) | |
2885 | (const shell) (const symlink) (const touch) | |
2886 | (const uncompress)))) | |
492d2437 RS |
2887 | |
2888 | (defun dired-mark-pop-up (bufname op-symbol files function &rest args) | |
8421685f RS |
2889 | "Return FUNCTION's result on ARGS after showing which files are marked. |
2890 | Displays the file names in a buffer named BUFNAME; | |
2891 | nil gives \" *Marked Files*\". | |
2892 | This uses function `dired-pop-to-buffer' to do that. | |
2893 | ||
2894 | FUNCTION should not manipulate files, just read input | |
2895 | (an argument or confirmation). | |
89101e46 SM |
2896 | The window is not shown if there is just one file or |
2897 | OP-SYMBOL is a member of the list in `dired-no-confirm'. | |
09b092ad RS |
2898 | FILES is the list of marked files. It can also be (t FILENAME) |
2899 | in the case of one marked file, to distinguish that from using | |
2900 | just the current file." | |
492d2437 | 2901 | (or bufname (setq bufname " *Marked Files*")) |
0347d069 SM |
2902 | (if (or (eq dired-no-confirm t) |
2903 | (memq op-symbol dired-no-confirm) | |
09b092ad | 2904 | ;; If FILES defaulted to the current line's file. |
492d2437 RS |
2905 | (= (length files) 1)) |
2906 | (apply function args) | |
89101e46 | 2907 | (with-current-buffer (get-buffer-create bufname) |
492d2437 | 2908 | (erase-buffer) |
09b092ad RS |
2909 | ;; Handle (t FILE) just like (FILE), here. |
2910 | ;; That value is used (only in some cases), to mean | |
2911 | ;; just one file that was marked, rather than the current line file. | |
2912 | (dired-format-columns-of-files (if (eq (car files) t) (cdr files) files)) | |
1a0b9ae6 EZ |
2913 | (remove-text-properties (point-min) (point-max) |
2914 | '(mouse-face nil help-echo nil))) | |
492d2437 RS |
2915 | (save-window-excursion |
2916 | (dired-pop-to-buffer bufname) | |
2917 | (apply function args)))) | |
2918 | ||
2919 | (defun dired-format-columns-of-files (files) | |
991ae4e4 SM |
2920 | (let ((beg (point))) |
2921 | (completion--insert-strings files) | |
2922 | (put-text-property beg (point) 'mouse-face nil))) | |
83fadedf | 2923 | \f |
492d2437 RS |
2924 | ;; Commands to mark or flag file(s) at or near current line. |
2925 | ||
2926 | (defun dired-repeat-over-lines (arg function) | |
2927 | ;; This version skips non-file lines. | |
cfe89c4f | 2928 | (let ((pos (make-marker))) |
492d2437 | 2929 | (beginning-of-line) |
cfe89c4f RS |
2930 | (while (and (> arg 0) (not (eobp))) |
2931 | (setq arg (1- arg)) | |
2932 | (beginning-of-line) | |
2933 | (while (and (not (eobp)) (dired-between-files)) (forward-line 1)) | |
2934 | (save-excursion | |
2935 | (forward-line 1) | |
2936 | (move-marker pos (1+ (point)))) | |
2937 | (save-excursion (funcall function)) | |
2938 | ;; Advance to the next line--actually, to the line that *was* next. | |
2939 | ;; (If FUNCTION inserted some new lines in between, skip them.) | |
2940 | (goto-char pos)) | |
2941 | (while (and (< arg 0) (not (bobp))) | |
2942 | (setq arg (1+ arg)) | |
2943 | (forward-line -1) | |
2944 | (while (and (not (bobp)) (dired-between-files)) (forward-line -1)) | |
2945 | (beginning-of-line) | |
2946 | (save-excursion (funcall function))) | |
2947 | (move-marker pos nil) | |
2948 | (dired-move-to-filename))) | |
492d2437 RS |
2949 | |
2950 | (defun dired-between-files () | |
8a372755 MB |
2951 | ;; This used to be a regexp match of the `total ...' line output by |
2952 | ;; ls, which is slightly faster, but that is not very robust; notably, | |
2953 | ;; it fails for non-english locales. | |
2954 | (save-excursion (not (dired-move-to-filename)))) | |
492d2437 RS |
2955 | |
2956 | (defun dired-next-marked-file (arg &optional wrap opoint) | |
2957 | "Move to the next marked file, wrapping around the end of the buffer." | |
2958 | (interactive "p\np") | |
2959 | (or opoint (setq opoint (point)));; return to where interactively started | |
2960 | (if (if (> arg 0) | |
2961 | (re-search-forward dired-re-mark nil t arg) | |
2962 | (beginning-of-line) | |
2963 | (re-search-backward dired-re-mark nil t (- arg))) | |
2964 | (dired-move-to-filename) | |
2965 | (if (null wrap) | |
2966 | (progn | |
2967 | (goto-char opoint) | |
2968 | (error "No next marked file")) | |
2969 | (message "(Wraparound for next marked file)") | |
2970 | (goto-char (if (> arg 0) (point-min) (point-max))) | |
2971 | (dired-next-marked-file arg nil opoint)))) | |
2972 | ||
2973 | (defun dired-prev-marked-file (arg &optional wrap) | |
2974 | "Move to the previous marked file, wrapping around the end of the buffer." | |
2975 | (interactive "p\np") | |
2976 | (dired-next-marked-file (- arg) wrap)) | |
2977 | ||
2978 | (defun dired-file-marker (file) | |
2979 | ;; Return FILE's marker, or nil if unmarked. | |
2980 | (save-excursion | |
2981 | (and (dired-goto-file file) | |
2982 | (progn | |
2983 | (beginning-of-line) | |
2984 | (if (not (equal ?\040 (following-char))) | |
2985 | (following-char)))))) | |
2986 | ||
2987 | (defun dired-mark-files-in-region (start end) | |
f836b98e | 2988 | (let ((inhibit-read-only t)) |
492d2437 RS |
2989 | (if (> start end) |
2990 | (error "start > end")) | |
2991 | (goto-char start) ; assumed at beginning of line | |
2992 | (while (< (point) end) | |
2993 | ;; Skip subdir line and following garbage like the `total' line: | |
2994 | (while (and (< (point) end) (dired-between-files)) | |
2995 | (forward-line 1)) | |
2996 | (if (and (not (looking-at dired-re-dot)) | |
2997 | (dired-get-filename nil t)) | |
2998 | (progn | |
2999 | (delete-char 1) | |
3000 | (insert dired-marker-char))) | |
3001 | (forward-line 1)))) | |
3002 | ||
3003 | (defun dired-mark (arg) | |
3004 | "Mark the current (or next ARG) files. | |
3005 | If on a subdir headerline, mark all its files except `.' and `..'. | |
3006 | ||
3007 | Use \\[dired-unmark-all-files] to remove all marks | |
3008 | and \\[dired-unmark] on a subdir to remove the marks in | |
3009 | this subdir." | |
3010 | (interactive "P") | |
277568f2 | 3011 | (if (dired-get-subdir) |
492d2437 | 3012 | (save-excursion (dired-mark-subdir-files)) |
f836b98e | 3013 | (let ((inhibit-read-only t)) |
492d2437 | 3014 | (dired-repeat-over-lines |
52041219 | 3015 | (prefix-numeric-value arg) |
492d2437 RS |
3016 | (function (lambda () (delete-char 1) (insert dired-marker-char))))))) |
3017 | ||
3018 | (defun dired-unmark (arg) | |
3019 | "Unmark the current (or next ARG) files. | |
3020 | If looking at a subdir, unmark all its files except `.' and `..'." | |
3021 | (interactive "P") | |
3022 | (let ((dired-marker-char ?\040)) | |
3023 | (dired-mark arg))) | |
3024 | ||
3025 | (defun dired-flag-file-deletion (arg) | |
68d2f12f | 3026 | "In Dired, flag the current line's file for deletion. |
492d2437 RS |
3027 | With prefix arg, repeat over several lines. |
3028 | ||
3029 | If on a subdir headerline, mark all its files except `.' and `..'." | |
3030 | (interactive "P") | |
3031 | (let ((dired-marker-char dired-del-marker)) | |
3032 | (dired-mark arg))) | |
3033 | ||
3034 | (defun dired-unmark-backward (arg) | |
a48868a7 LMI |
3035 | "In Dired, move up lines and remove marks or deletion flags there. |
3036 | Optional prefix ARG says how many lines to unmark/unflag; default | |
3037 | is one line." | |
492d2437 RS |
3038 | (interactive "p") |
3039 | (dired-unmark (- arg))) | |
e5f0841e | 3040 | |
d588eb90 RS |
3041 | (defun dired-toggle-marks () |
3042 | "Toggle marks: marked files become unmarked, and vice versa. | |
e5f0841e KH |
3043 | Files marked with other flags (such as `D') are not affected. |
3044 | `.' and `..' are never toggled. | |
3045 | As always, hidden subdirs are not affected." | |
3046 | (interactive) | |
3047 | (save-excursion | |
3048 | (goto-char (point-min)) | |
f836b98e | 3049 | (let ((inhibit-read-only t)) |
e5f0841e KH |
3050 | (while (not (eobp)) |
3051 | (or (dired-between-files) | |
3052 | (looking-at dired-re-dot) | |
3053 | ;; use subst instead of insdel because it does not move | |
3054 | ;; the gap and thus should be faster and because | |
3055 | ;; other characters are left alone automatically | |
3056 | (apply 'subst-char-in-region | |
3057 | (point) (1+ (point)) | |
3058 | (if (eq ?\040 (following-char)) ; SPC | |
3059 | (list ?\040 dired-marker-char) | |
3060 | (list dired-marker-char ?\040)))) | |
3061 | (forward-line 1))))) | |
83fadedf | 3062 | \f |
492d2437 RS |
3063 | ;;; Commands to mark or flag files based on their characteristics or names. |
3064 | ||
d2cadc4b RS |
3065 | (defvar dired-regexp-history nil |
3066 | "History list of regular expressions used in Dired commands.") | |
3067 | ||
3068 | (defun dired-read-regexp (prompt) | |
3069 | (read-from-minibuffer prompt nil nil nil 'dired-regexp-history)) | |
492d2437 RS |
3070 | |
3071 | (defun dired-mark-files-regexp (regexp &optional marker-char) | |
3072 | "Mark all files matching REGEXP for use in later commands. | |
3073 | A prefix argument means to unmark them instead. | |
3074 | `.' and `..' are never marked. | |
3075 | ||
3076 | REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for | |
3077 | object files--just `.o' will mark more than you might think." | |
3078 | (interactive | |
3079 | (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark") | |
3080 | " files (regexp): ")) | |
3081 | (if current-prefix-arg ?\040))) | |
3082 | (let ((dired-marker-char (or marker-char dired-marker-char))) | |
3083 | (dired-mark-if | |
3084 | (and (not (looking-at dired-re-dot)) | |
3085 | (not (eolp)) ; empty line | |
3086 | (let ((fn (dired-get-filename nil t))) | |
3087 | (and fn (string-match regexp (file-name-nondirectory fn))))) | |
3088 | "matching file"))) | |
3089 | ||
e9b8e22d RS |
3090 | (defun dired-mark-files-containing-regexp (regexp &optional marker-char) |
3091 | "Mark all files with contents containing REGEXP for use in later commands. | |
3092 | A prefix argument means to unmark them instead. | |
3093 | `.' and `..' are never marked." | |
3094 | (interactive | |
3095 | (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark") | |
3096 | " files containing (regexp): ")) | |
3097 | (if current-prefix-arg ?\040))) | |
3098 | (let ((dired-marker-char (or marker-char dired-marker-char))) | |
3099 | (dired-mark-if | |
3100 | (and (not (looking-at dired-re-dot)) | |
3101 | (not (eolp)) ; empty line | |
3102 | (let ((fn (dired-get-filename nil t))) | |
05a40373 KH |
3103 | (when (and fn (file-readable-p fn) |
3104 | (not (file-directory-p fn))) | |
3105 | (let ((prebuf (get-file-buffer fn))) | |
3106 | (message "Checking %s" fn) | |
3107 | ;; For now we do it inside emacs | |
3108 | ;; Grep might be better if there are a lot of files | |
3109 | (if prebuf | |
3110 | (with-current-buffer prebuf | |
3111 | (save-excursion | |
3112 | (goto-char (point-min)) | |
3113 | (re-search-forward regexp nil t))) | |
3114 | (with-temp-buffer | |
3115 | (insert-file-contents fn) | |
3116 | (goto-char (point-min)) | |
3117 | (re-search-forward regexp nil t)))) | |
0b2bb4d0 | 3118 | ))) |
e9b8e22d RS |
3119 | "matching file"))) |
3120 | ||
492d2437 | 3121 | (defun dired-flag-files-regexp (regexp) |
68d2f12f | 3122 | "In Dired, flag all files containing the specified REGEXP for deletion. |
492d2437 RS |
3123 | The match is against the non-directory part of the filename. Use `^' |
3124 | and `$' to anchor matches. Exclude subdirs by hiding them. | |
3125 | `.' and `..' are never flagged." | |
3126 | (interactive (list (dired-read-regexp "Flag for deletion (regexp): "))) | |
3127 | (dired-mark-files-regexp regexp dired-del-marker)) | |
3128 | ||
3129 | (defun dired-mark-symlinks (unflag-p) | |
3130 | "Mark all symbolic links. | |
a48868a7 | 3131 | With prefix argument, unmark or unflag all those files." |
492d2437 RS |
3132 | (interactive "P") |
3133 | (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))) | |
3134 | (dired-mark-if (looking-at dired-re-sym) "symbolic link"))) | |
3135 | ||
3136 | (defun dired-mark-directories (unflag-p) | |
3137 | "Mark all directory file lines except `.' and `..'. | |
a48868a7 | 3138 | With prefix argument, unmark or unflag all those files." |
492d2437 RS |
3139 | (interactive "P") |
3140 | (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))) | |
3141 | (dired-mark-if (and (looking-at dired-re-dir) | |
3142 | (not (looking-at dired-re-dot))) | |
3143 | "directory file"))) | |
3144 | ||
3145 | (defun dired-mark-executables (unflag-p) | |
3146 | "Mark all executable files. | |
a48868a7 | 3147 | With prefix argument, unmark or unflag all those files." |
492d2437 RS |
3148 | (interactive "P") |
3149 | (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))) | |
3150 | (dired-mark-if (looking-at dired-re-exe) "executable file"))) | |
3151 | ||
3152 | ;; dired-x.el has a dired-mark-sexp interactive command: mark | |
3153 | ;; files for which PREDICATE returns non-nil. | |
3154 | ||
3155 | (defun dired-flag-auto-save-files (&optional unflag-p) | |
84fc2cfa | 3156 | "Flag for deletion files whose names suggest they are auto save files. |
a48868a7 | 3157 | A prefix argument says to unmark or unflag those files instead." |
84fc2cfa | 3158 | (interactive "P") |
492d2437 RS |
3159 | (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker))) |
3160 | (dired-mark-if | |
2a838614 | 3161 | ;; It is less than general to check for # here, |
a91526b9 RS |
3162 | ;; but it's the only way this runs fast enough. |
3163 | (and (save-excursion (end-of-line) | |
536ae2dd RS |
3164 | (or |
3165 | (eq (preceding-char) ?#) | |
3166 | ;; Handle executables in case of -F option. | |
3167 | ;; We need not worry about the other kinds | |
3168 | ;; of markings that -F makes, since they won't | |
3169 | ;; appear on real auto-save files. | |
3170 | (if (eq (preceding-char) ?*) | |
3171 | (progn | |
3172 | (forward-char -1) | |
3173 | (eq (preceding-char) ?#))))) | |
a91526b9 RS |
3174 | (not (looking-at dired-re-dir)) |
3175 | (let ((fn (dired-get-filename t t))) | |
3176 | (if fn (auto-save-file-name-p | |
3177 | (file-name-nondirectory fn))))) | |
3178 | "auto save file"))) | |
492d2437 | 3179 | |
d7aed37c | 3180 | (defcustom dired-garbage-files-regexp |
6736ab5a | 3181 | ;; `log' here is dubious, since it's typically used for useful log |
d7aed37c | 3182 | ;; files, not just TeX stuff. -- fx |
d4aeef3b KG |
3183 | (concat (regexp-opt |
3184 | '(".log" ".toc" ".dvi" ".bak" ".orig" ".rej" ".aux")) | |
c60ee5e7 | 3185 | "\\'") |
d7aed37c SM |
3186 | "Regular expression to match \"garbage\" files for `dired-flag-garbage-files'." |
3187 | :type 'regexp | |
3188 | :group 'dired) | |
84d3f6e8 RS |
3189 | |
3190 | (defun dired-flag-garbage-files () | |
84d3f6e8 | 3191 | "Flag for deletion all files that match `dired-garbage-files-regexp'." |
f656ec48 | 3192 | (interactive) |
84d3f6e8 RS |
3193 | (dired-flag-files-regexp dired-garbage-files-regexp)) |
3194 | ||
492d2437 RS |
3195 | (defun dired-flag-backup-files (&optional unflag-p) |
3196 | "Flag all backup files (names ending with `~') for deletion. | |
a48868a7 | 3197 | With prefix argument, unmark or unflag these files." |
492d2437 | 3198 | (interactive "P") |
ee680b2b | 3199 | (let ((dired-marker-char (if unflag-p ?\s dired-del-marker))) |
492d2437 | 3200 | (dired-mark-if |
f13101e9 KH |
3201 | ;; Don't call backup-file-name-p unless the last character looks like |
3202 | ;; it might be the end of a backup file name. This isn't very general, | |
a91526b9 RS |
3203 | ;; but it's the only way this runs fast enough. |
3204 | (and (save-excursion (end-of-line) | |
f13101e9 KH |
3205 | ;; Handle executables in case of -F option. |
3206 | ;; We need not worry about the other kinds | |
3207 | ;; of markings that -F makes, since they won't | |
3208 | ;; appear on real backup files. | |
3209 | (if (eq (preceding-char) ?*) | |
3210 | (forward-char -1)) | |
fdee13ec | 3211 | (eq (preceding-char) ?~)) |
a91526b9 | 3212 | (not (looking-at dired-re-dir)) |
492d2437 RS |
3213 | (let ((fn (dired-get-filename t t))) |
3214 | (if fn (backup-file-name-p fn)))) | |
3215 | "backup file"))) | |
3216 | ||
6482fcac RS |
3217 | (defun dired-change-marks (&optional old new) |
3218 | "Change all OLD marks to NEW marks. | |
3219 | OLD and NEW are both characters used to mark files." | |
3220 | (interactive | |
3221 | (let* ((cursor-in-echo-area t) | |
3222 | (old (progn (message "Change (old mark): ") (read-char))) | |
3223 | (new (progn (message "Change %c marks to (new mark): " old) | |
3224 | (read-char)))) | |
3225 | (list old new))) | |
e4e02841 RS |
3226 | (if (or (eq old ?\r) (eq new ?\r)) |
3227 | (ding) | |
3228 | (let ((string (format "\n%c" old)) | |
f836b98e | 3229 | (inhibit-read-only t)) |
e4e02841 RS |
3230 | (save-excursion |
3231 | (goto-char (point-min)) | |
3232 | (while (search-forward string nil t) | |
ee680b2b | 3233 | (if (if (= old ?\s) |
a15a76f7 RS |
3234 | (save-match-data |
3235 | (dired-get-filename 'no-dir t)) | |
3236 | t) | |
3237 | (subst-char-in-region (match-beginning 0) | |
3238 | (match-end 0) old new))))))) | |
6482fcac | 3239 | |
534a6edf | 3240 | (defun dired-unmark-all-marks () |
ee680b2b | 3241 | "Remove all marks from all files in the dired buffer." |
b1ecd9c6 RS |
3242 | (interactive) |
3243 | (dired-unmark-all-files ?\r)) | |
3244 | ||
8b87a301 | 3245 | (defun dired-unmark-all-files (mark &optional arg) |
b602ba2f | 3246 | "Remove a specific mark (or any mark) from every file. |
c60ee5e7 | 3247 | After this command, type the mark character to remove, |
b602ba2f | 3248 | or type RET to remove all marks. |
3585916f | 3249 | With prefix arg, query for each marked file. |
492d2437 | 3250 | Type \\[help-command] at that time for help." |
b602ba2f RS |
3251 | (interactive "cRemove marks (RET means all): \nP") |
3252 | (save-excursion | |
3253 | (let* ((count 0) | |
d032d5e7 | 3254 | (inhibit-read-only t) case-fold-search |
b602ba2f RS |
3255 | (string (format "\n%c" mark)) |
3256 | (help-form "\ | |
8b87a301 RS |
3257 | Type SPC or `y' to unmark one file, DEL or `n' to skip to next, |
3258 | `!' to unmark all remaining files with no more questions.")) | |
b602ba2f RS |
3259 | (goto-char (point-min)) |
3260 | (while (if (eq mark ?\r) | |
3261 | (re-search-forward dired-re-mark nil t) | |
3262 | (search-forward string nil t)) | |
3263 | (if (or (not arg) | |
e9407052 RS |
3264 | (let ((file (dired-get-filename t t))) |
3265 | (and file | |
3266 | (dired-query 'query "Unmark file `%s'? " | |
3267 | file)))) | |
b602ba2f | 3268 | (progn (subst-char-in-region (1- (point)) (point) |
ee680b2b | 3269 | (preceding-char) ?\s) |
b602ba2f RS |
3270 | (setq count (1+ count))))) |
3271 | (message (if (= count 1) "1 mark removed" | |
3272 | "%d marks removed") | |
3273 | count)))) | |
83fadedf | 3274 | \f |
492d2437 | 3275 | ;; Logging failures operating on files, and showing the results. |
84fc2cfa | 3276 | |
492d2437 | 3277 | (defvar dired-log-buffer "*Dired log*") |
84fc2cfa | 3278 | |
492d2437 RS |
3279 | (defun dired-why () |
3280 | "Pop up a buffer with error log output from Dired. | |
3281 | A group of errors from a single command ends with a formfeed. | |
3282 | Thus, use \\[backward-page] to find the beginning of a group of errors." | |
3283 | (interactive) | |
3284 | (if (get-buffer dired-log-buffer) | |
3285 | (let ((owindow (selected-window)) | |
3286 | (window (display-buffer (get-buffer dired-log-buffer)))) | |
3287 | (unwind-protect | |
6482fcac | 3288 | (progn |
492d2437 RS |
3289 | (select-window window) |
3290 | (goto-char (point-max)) | |
0b7bc76f RS |
3291 | (forward-line -1) |
3292 | (backward-page 1) | |
3293 | (recenter 0)) | |
492d2437 RS |
3294 | (select-window owindow))))) |
3295 | ||
3296 | (defun dired-log (log &rest args) | |
3297 | ;; Log a message or the contents of a buffer. | |
3298 | ;; If LOG is a string and there are more args, it is formatted with | |
3299 | ;; those ARGS. Usually the LOG string ends with a \n. | |
c60ee5e7 | 3300 | ;; End each bunch of errors with (dired-log t): |
0b7bc76f RS |
3301 | ;; this inserts the current time and buffer at the start of the page, |
3302 | ;; and \f (formfeed) at the end. | |
492d2437 | 3303 | (let ((obuf (current-buffer))) |
0b7bc76f RS |
3304 | (with-current-buffer (get-buffer-create dired-log-buffer) |
3305 | (goto-char (point-max)) | |
3306 | (let ((inhibit-read-only t)) | |
3307 | (cond ((stringp log) | |
3308 | (insert (if args | |
3309 | (apply (function format) log args) | |
3310 | log))) | |
3311 | ((bufferp log) | |
88cb5d3c | 3312 | (insert-buffer-substring log)) |
0b7bc76f RS |
3313 | ((eq t log) |
3314 | (backward-page 1) | |
3315 | (unless (bolp) | |
3316 | (insert "\n")) | |
3317 | (insert (current-time-string) | |
3318 | "\tBuffer `" (buffer-name obuf) "'\n") | |
3319 | (goto-char (point-max)) | |
3320 | (insert "\f\n"))))))) | |
492d2437 RS |
3321 | |
3322 | (defun dired-log-summary (string failures) | |
ddcfebf1 RS |
3323 | "State a summary of a command's failures, in echo area and log buffer. |
3324 | STRING is an overall summary of the failures. | |
3325 | FAILURES is a list of file names that we failed to operate on, | |
3326 | or nil if file names are not applicable." | |
0b7bc76f RS |
3327 | (if (= (length failures) 1) |
3328 | (message "%s" | |
3329 | (with-current-buffer dired-log-buffer | |
3330 | (goto-char (point-max)) | |
3331 | (backward-page 1) | |
3332 | (if (eolp) (forward-line 1)) | |
3333 | (buffer-substring (point) (point-max)))) | |
3334 | (message (if failures "%s--type ? for details (%s)" | |
3335 | "%s--type ? for details") | |
3336 | string failures)) | |
492d2437 | 3337 | ;; Log a summary describing a bunch of errors. |
0b7bc76f | 3338 | (dired-log (concat "\n" string "\n")) |
492d2437 | 3339 | (dired-log t)) |
83fadedf | 3340 | \f |
492d2437 RS |
3341 | ;;; Sorting |
3342 | ||
3343 | ;; Most ls can only sort by name or by date (with -t), nothing else. | |
3344 | ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U. | |
3345 | ;; So anything that does not contain these is sort "by name". | |
3346 | ||
3347 | (defvar dired-ls-sorting-switches "SXU" | |
87ae59e3 | 3348 | "String of `ls' switches \(single letters\) except \"t\" that influence sorting. |
cfc8b264 EZ |
3349 | |
3350 | This indicates to Dired which option switches to watch out for because they | |
3351 | will change the sorting order behavior of `ls'. | |
3352 | ||
3353 | To change the default sorting order \(e.g. add a `-v' option\), see the | |
3354 | variable `dired-listing-switches'. To temporarily override the listing | |
3355 | format, use `\\[universal-argument] \\[dired]'.") | |
492d2437 RS |
3356 | |
3357 | (defvar dired-sort-by-date-regexp | |
8d76af4a CY |
3358 | (concat "\\(\\`\\| \\)-[^- ]*t" |
3359 | ;; `dired-ls-sorting-switches' after -t overrides -t. | |
3360 | "[^ " dired-ls-sorting-switches "]*" | |
3361 | "\\(\\(\\`\\| +\\)\\(--[^ ]+\\|-[^- t" | |
3362 | dired-ls-sorting-switches "]+\\)\\)* *$") | |
ee680b2b | 3363 | "Regexp recognized by Dired to set `by date' mode.") |
492d2437 RS |
3364 | |
3365 | (defvar dired-sort-by-name-regexp | |
8d76af4a CY |
3366 | (concat "\\`\\(\\(\\`\\| +\\)\\(--[^ ]+\\|" |
3367 | "-[^- t" dired-ls-sorting-switches "]+\\)\\)* *$") | |
ee680b2b | 3368 | "Regexp recognized by Dired to set `by name' mode.") |
492d2437 | 3369 | |
23fc67ea RS |
3370 | (defvar dired-sort-inhibit nil |
3371 | "Non-nil means the Dired sort command is disabled. | |
ee680b2b | 3372 | The idea is to set this buffer-locally in special dired buffers.") |
23fc67ea | 3373 | |
492d2437 RS |
3374 | (defun dired-sort-set-modeline () |
3375 | ;; Set modeline display according to dired-actual-switches. | |
3376 | ;; Modeline display of "by name" or "by date" guarantees the user a | |
3377 | ;; match with the corresponding regexps. Non-matching switches are | |
3378 | ;; shown literally. | |
5641671f NR |
3379 | (when (eq major-mode 'dired-mode) |
3380 | (setq mode-name | |
3381 | (let (case-fold-search) | |
098bab71 | 3382 | (cond ((string-match |
5641671f NR |
3383 | dired-sort-by-name-regexp dired-actual-switches) |
3384 | "Dired by name") | |
3385 | ((string-match | |
3386 | dired-sort-by-date-regexp dired-actual-switches) | |
3387 | "Dired by date") | |
3388 | (t | |
3389 | (concat "Dired " dired-actual-switches))))) | |
3390 | (force-mode-line-update))) | |
492d2437 RS |
3391 | |
3392 | (defun dired-sort-toggle-or-edit (&optional arg) | |
8d76af4a CY |
3393 | "Toggle sorting by date, and refresh the Dired buffer. |
3394 | With a prefix argument, edit the current listing switches instead." | |
84fc2cfa | 3395 | (interactive "P") |
23fc67ea | 3396 | (when dired-sort-inhibit |
ee680b2b | 3397 | (error "Cannot sort this dired buffer")) |
492d2437 RS |
3398 | (if arg |
3399 | (dired-sort-other | |
3400 | (read-string "ls switches (must contain -l): " dired-actual-switches)) | |
3401 | (dired-sort-toggle))) | |
3402 | ||
3403 | (defun dired-sort-toggle () | |
3404 | ;; Toggle between sort by date/name. Reverts the buffer. | |
8d76af4a CY |
3405 | (let ((sorting-by-date (string-match dired-sort-by-date-regexp |
3406 | dired-actual-switches)) | |
3407 | ;; Regexp for finding (possibly embedded) -t switches. | |
3408 | (switch-regexp "\\(\\`\\| \\)-\\([a-su-zA-Z]*\\)\\(t\\)\\([^ ]*\\)") | |
3409 | case-fold-search) | |
3410 | ;; Remove the -t switch. | |
3411 | (while (string-match switch-regexp dired-actual-switches) | |
3412 | (if (and (equal (match-string 2 dired-actual-switches) "") | |
3413 | (equal (match-string 4 dired-actual-switches) "")) | |
3414 | ;; Remove a stand-alone -t switch. | |
3415 | (setq dired-actual-switches | |
3416 | (replace-match "" t t dired-actual-switches)) | |
3417 | ;; Remove a switch of the form -XtY for some X and Y. | |
3418 | (setq dired-actual-switches | |
3419 | (replace-match "" t t dired-actual-switches 3)))) | |
3420 | ;; Now, if we weren't sorting by date before, add the -t switch. | |
3421 | (unless sorting-by-date | |
3422 | (setq dired-actual-switches (concat dired-actual-switches " -t")))) | |
492d2437 RS |
3423 | (dired-sort-set-modeline) |
3424 | (revert-buffer)) | |
3425 | ||
83fadedf | 3426 | ;; Some user code loads dired especially for this. |
879fa8d0 | 3427 | ;; Don't do that--use replace-regexp-in-string instead. |
83fadedf DL |
3428 | (defun dired-replace-in-string (regexp newtext string) |
3429 | ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result. | |
3430 | ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized. | |
3431 | (let ((result "") (start 0) mb me) | |
3432 | (while (string-match regexp string start) | |
3433 | (setq mb (match-beginning 0) | |
3434 | me (match-end 0) | |
3435 | result (concat result (substring string start mb) newtext) | |
3436 | start me)) | |
3437 | (concat result (substring string start)))) | |
3438 | ||
492d2437 | 3439 | (defun dired-sort-other (switches &optional no-revert) |
ee680b2b | 3440 | "Specify new `ls' SWITCHES for current dired buffer. |
ff3d76aa SM |
3441 | Values matching `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' |
3442 | set the minor mode accordingly, others appear literally in the mode line. | |
3443 | With optional second arg NO-REVERT, don't refresh the listing afterwards." | |
e17dba1f | 3444 | (dired-sort-R-check switches) |
492d2437 | 3445 | (setq dired-actual-switches switches) |
5641671f | 3446 | (dired-sort-set-modeline) |
492d2437 | 3447 | (or no-revert (revert-buffer))) |
e17dba1f | 3448 | |
5afea280 RS |
3449 | (defvar dired-subdir-alist-pre-R nil |
3450 | "Value of `dired-subdir-alist' before -R switch added.") | |
75461997 | 3451 | (make-variable-buffer-local 'dired-subdir-alist-pre-R) |
e17dba1f GM |
3452 | |
3453 | (defun dired-sort-R-check (switches) | |
3454 | "Additional processing of -R in ls option string SWITCHES. | |
3455 | Saves `dired-subdir-alist' when R is set and restores saved value | |
3456 | minus any directories explicitly deleted when R is cleared. | |
3457 | To be called first in body of `dired-sort-other', etc." | |
3458 | (cond | |
3459 | ((and (string-match "R" switches) | |
3460 | (not (string-match "R" dired-actual-switches))) | |
3461 | ;; Adding -R to ls switches -- save `dired-subdir-alist': | |
3462 | (setq dired-subdir-alist-pre-R dired-subdir-alist)) | |
3463 | ((and (string-match "R" dired-actual-switches) | |
3464 | (not (string-match "R" switches))) | |
3465 | ;; Deleting -R from ls switches -- revert to pre-R subdirs | |
3466 | ;; that are still present: | |
3467 | (setq dired-subdir-alist | |
3468 | (if dired-subdir-alist-pre-R | |
3469 | (let (subdirs) | |
3470 | (while dired-subdir-alist-pre-R | |
3471 | (if (assoc (caar dired-subdir-alist-pre-R) | |
3472 | dired-subdir-alist) | |
3473 | ;; subdir still present... | |
3474 | (setq subdirs | |
3475 | (cons (car dired-subdir-alist-pre-R) | |
3476 | subdirs))) | |
3477 | (setq dired-subdir-alist-pre-R | |
3478 | (cdr dired-subdir-alist-pre-R))) | |
3479 | (reverse subdirs)) | |
3480 | ;; No pre-R subdir alist, so revert to main directory | |
3481 | ;; listing: | |
3482 | (list (car (reverse dired-subdir-alist)))))))) | |
83fadedf | 3483 | \f |
133aad74 JD |
3484 | |
3485 | ;;;; Drag and drop support | |
3486 | ||
773933d3 | 3487 | (defcustom dired-recursive-copies 'top |
9201cc28 | 3488 | "Decide whether recursive copies are allowed. |
098bab71 | 3489 | A value of nil means no recursive copies. |
fe02ba07 RS |
3490 | `always' means copy recursively without asking. |
3491 | `top' means ask for each directory at top level. | |
3492 | Anything else means ask for each directory." | |
3493 | :type '(choice :tag "Copy directories" | |
3494 | (const :tag "No recursive copies" nil) | |
3495 | (const :tag "Ask for each directory" t) | |
3496 | (const :tag "Ask for each top directory only" top) | |
3497 | (const :tag "Copy directories without asking" always)) | |
3498 | :group 'dired) | |
3499 | ||
133aad74 | 3500 | (defun dired-dnd-popup-notice () |
fcaed7ce | 3501 | (message-box |
3b1b11e9 | 3502 | "Dired recursive copies are currently disabled.\nSee the variable `dired-recursive-copies'.")) |
133aad74 | 3503 | |
e8a11b22 | 3504 | (declare-function x-popup-menu "menu.c" (position menu)) |
133aad74 JD |
3505 | |
3506 | (defun dired-dnd-do-ask-action (uri) | |
3507 | ;; No need to get actions and descriptions from the source, | |
3508 | ;; we only have three actions anyway. | |
5553077c | 3509 | (let ((action (x-popup-menu |
133aad74 JD |
3510 | t |
3511 | (list "What action?" | |
3512 | (cons "" | |
3513 | '(("Copy here" . copy) | |
3514 | ("Move here" . move) | |
3515 | ("Link here" . link) | |
3516 | "--" | |
3517 | ("Cancel" . nil))))))) | |
3518 | (if action | |
3519 | (dired-dnd-handle-local-file uri action) | |
3520 | nil))) | |
3521 | ||
5c4fa70f | 3522 | (declare-function dired-relist-entry "dired-aux" (file)) |
f36d1cdc | 3523 | (declare-function make-symbolic-link "fileio.c") |
5c4fa70f | 3524 | |
aa360da1 GM |
3525 | ;; Only used when (featurep 'dnd). |
3526 | (declare-function dnd-get-local-file-name "dnd" (uri &optional must-exist)) | |
3527 | (declare-function dnd-get-local-file-uri "dnd" (uri)) | |
3528 | ||
d032d5e7 SM |
3529 | (defvar dired-overwrite-confirmed) ;Defined in dired-aux. |
3530 | ||
133aad74 JD |
3531 | (defun dired-dnd-handle-local-file (uri action) |
3532 | "Copy, move or link a file to the dired directory. | |
3533 | URI is the file to handle, ACTION is one of copy, move, link or ask. | |
3534 | Ask means pop up a menu for the user to select one of copy, move or link." | |
3535 | (require 'dired-aux) | |
361eee8f | 3536 | (let* ((from (dnd-get-local-file-name uri t)) |
93ab4de3 CY |
3537 | (to (when from |
3538 | (concat (dired-current-directory) | |
3539 | (file-name-nondirectory from))))) | |
61bfceb6 | 3540 | (when from |
93ab4de3 | 3541 | (cond ((eq action 'ask) |
61bfceb6 | 3542 | (dired-dnd-do-ask-action uri)) |
93ab4de3 CY |
3543 | ;; If copying a directory and dired-recursive-copies is |
3544 | ;; nil, dired-copy-file fails. Pop up a notice. | |
3545 | ((and (memq action '(copy private)) | |
3546 | (file-directory-p from) | |
3547 | (not dired-recursive-copies)) | |
3548 | (dired-dnd-popup-notice)) | |
3549 | ((memq action '(copy private move link)) | |
3550 | (let ((overwrite (and (file-exists-p to) | |
3551 | (y-or-n-p | |
3552 | (format "Overwrite existing file `%s'? " to)))) | |
3553 | ;; Binding dired-overwrite-confirmed to nil makes | |
3554 | ;; dired-handle-overwrite a no-op. We instead use | |
3555 | ;; y-or-n-p, which pops a graphical menu. | |
3556 | dired-overwrite-confirmed backup-file) | |
3557 | (when (and overwrite | |
3558 | ;; d-b-o is defined in dired-aux. | |
3559 | (boundp 'dired-backup-overwrite) | |
3560 | dired-backup-overwrite | |
3561 | (setq backup-file | |
3562 | (car (find-backup-file-name to))) | |
3563 | (or (eq dired-backup-overwrite 'always) | |
3564 | (y-or-n-p | |
3565 | (format | |
3566 | "Make backup for existing file `%s'? " to)))) | |
3567 | (rename-file to backup-file 0) | |
3568 | (dired-relist-entry backup-file)) | |
3569 | (cond ((memq action '(copy private)) | |
3570 | (dired-copy-file from to overwrite)) | |
3571 | ((eq action 'move) | |
3572 | (dired-rename-file from to overwrite)) | |
3573 | ((eq action 'link) | |
3574 | (make-symbolic-link from to overwrite))) | |
3575 | (dired-relist-entry to) | |
3576 | action)))))) | |
133aad74 JD |
3577 | |
3578 | (defun dired-dnd-handle-file (uri action) | |
3579 | "Copy, move or link a file to the dired directory if it is a local file. | |
3580 | URI is the file to handle. If the hostname in the URI isn't local, do nothing. | |
3581 | ACTION is one of copy, move, link or ask. | |
3582 | Ask means pop up a menu for the user to select one of copy, move or link." | |
361eee8f | 3583 | (let ((local-file (dnd-get-local-file-uri uri))) |
133aad74 JD |
3584 | (if local-file (dired-dnd-handle-local-file local-file action) |
3585 | nil))) | |
31b4c848 LH |
3586 | \f |
3587 | ||
3588 | ;;;; Desktop support | |
3589 | ||
3590 | (eval-when-compile (require 'desktop)) | |
3591 | ||
295fb2ac | 3592 | (defun dired-desktop-buffer-misc-data (dirname) |
31b4c848 LH |
3593 | "Auxiliary information to be saved in desktop file." |
3594 | (cons | |
3595 | ;; Value of `dired-directory'. | |
3596 | (if (consp dired-directory) | |
3597 | ;; Directory name followed by list of files. | |
295fb2ac | 3598 | (cons (desktop-file-name (car dired-directory) dirname) |
31b4c848 | 3599 | (cdr dired-directory)) |
9d9ee410 | 3600 | ;; Directory name, optionally with shell wildcard. |
295fb2ac | 3601 | (desktop-file-name dired-directory dirname)) |
31b4c848 LH |
3602 | ;; Subdirectories in `dired-subdir-alist'. |
3603 | (cdr | |
3604 | (nreverse | |
3605 | (mapcar | |
295fb2ac | 3606 | (function (lambda (f) (desktop-file-name (car f) dirname))) |
31b4c848 | 3607 | dired-subdir-alist))))) |
133aad74 | 3608 | |
d032d5e7 SM |
3609 | (defun dired-restore-desktop-buffer (_file-name |
3610 | _buffer-name | |
3611 | misc-data) | |
31b4c848 | 3612 | "Restore a dired buffer specified in a desktop file." |
d032d5e7 | 3613 | ;; First element of `misc-data' is the value of `dired-directory'. |
9d9ee410 | 3614 | ;; This value is a directory name, optionally with shell wildcard or |
31b4c848 | 3615 | ;; a directory name followed by list of files. |
d032d5e7 | 3616 | (let* ((dired-dir (car misc-data)) |
31b4c848 LH |
3617 | (dir (if (consp dired-dir) (car dired-dir) dired-dir))) |
3618 | (if (file-directory-p (file-name-directory dir)) | |
3619 | (progn | |
3620 | (dired dired-dir) | |
d032d5e7 | 3621 | ;; The following elements of `misc-data' are the keys |
31b4c848 | 3622 | ;; from `dired-subdir-alist'. |
d032d5e7 | 3623 | (mapc 'dired-maybe-insert-subdir (cdr misc-data)) |
31b4c848 LH |
3624 | (current-buffer)) |
3625 | (message "Desktop: Directory %s no longer exists." dir) | |
3626 | (when desktop-missing-file-warning (sit-for 1)) | |
3627 | nil))) | |
133aad74 | 3628 | |
9e7357b0 AS |
3629 | (add-to-list 'desktop-buffer-mode-handlers |
3630 | '(dired-mode . dired-restore-desktop-buffer)) | |
a515788d | 3631 | |
133aad74 | 3632 | \f |
7d72f6f1 GM |
3633 | ;;; Start of automatically extracted autoloads. |
3634 | \f | |
3635 | ;;;### (autoloads (dired-show-file-type dired-do-query-replace-regexp | |
3636 | ;;;;;; dired-do-search dired-do-isearch-regexp dired-do-isearch | |
3637 | ;;;;;; dired-isearch-filenames-regexp dired-isearch-filenames dired-isearch-filenames-setup | |
3638 | ;;;;;; dired-hide-all dired-hide-subdir dired-tree-down dired-tree-up | |
3639 | ;;;;;; dired-kill-subdir dired-mark-subdir-files dired-goto-subdir | |
3640 | ;;;;;; dired-prev-subdir dired-insert-subdir dired-maybe-insert-subdir | |
3641 | ;;;;;; dired-downcase dired-upcase dired-do-symlink-regexp dired-do-hardlink-regexp | |
3642 | ;;;;;; dired-do-copy-regexp dired-do-rename-regexp dired-do-rename | |
3643 | ;;;;;; dired-do-hardlink dired-do-symlink dired-do-copy dired-create-directory | |
3644 | ;;;;;; dired-rename-file dired-copy-file dired-relist-file dired-remove-file | |
3645 | ;;;;;; dired-add-file dired-do-redisplay dired-do-load dired-do-byte-compile | |
3646 | ;;;;;; dired-do-compress dired-query dired-compress-file dired-do-kill-lines | |
3647 | ;;;;;; dired-run-shell-command dired-do-shell-command dired-do-async-shell-command | |
3648 | ;;;;;; dired-clean-directory dired-do-print dired-do-touch dired-do-chown | |
3649 | ;;;;;; dired-do-chgrp dired-do-chmod dired-compare-directories dired-backup-diff | |
854e5fa8 | 3650 | ;;;;;; dired-diff) "dired-aux" "dired-aux.el" "ab62f310329f404f96a29e4f0ab8df73") |
7d72f6f1 GM |
3651 | ;;; Generated autoloads from dired-aux.el |
3652 | ||
3653 | (autoload 'dired-diff "dired-aux" "\ | |
3654 | Compare file at point with file FILE using `diff'. | |
3655 | FILE defaults to the file at the mark. (That's the mark set by | |
3656 | \\[set-mark-command], not by Dired's \\[dired-mark] command.) | |
a9e2a8db | 3657 | The prompted-for FILE is the first file given to `diff'. |
7d72f6f1 | 3658 | With prefix arg, prompt for second argument SWITCHES, |
a9e2a8db | 3659 | which is the string of command switches for `diff'. |
7d72f6f1 GM |
3660 | |
3661 | \(fn FILE &optional SWITCHES)" t nil) | |
3662 | ||
3663 | (autoload 'dired-backup-diff "dired-aux" "\ | |
3664 | Diff this file with its backup file or vice versa. | |
3665 | Uses the latest backup, if there are several numerical backups. | |
3666 | If this file is a backup, diff it with its original. | |
3667 | The backup file is the first file given to `diff'. | |
3668 | With prefix arg, prompt for argument SWITCHES which is options for `diff'. | |
3669 | ||
3670 | \(fn &optional SWITCHES)" t nil) | |
3671 | ||
3672 | (autoload 'dired-compare-directories "dired-aux" "\ | |
3673 | Mark files with different file attributes in two dired buffers. | |
3674 | Compare file attributes of files in the current directory | |
3675 | with file attributes in directory DIR2 using PREDICATE on pairs of files | |
3676 | with the same name. Mark files for which PREDICATE returns non-nil. | |
3677 | Mark files with different names if PREDICATE is nil (or interactively | |
3678 | with empty input at the predicate prompt). | |
3679 | ||
3680 | PREDICATE is a Lisp expression that can refer to the following variables: | |
3681 | ||
3682 | size1, size2 - file size in bytes | |
3683 | mtime1, mtime2 - last modification time in seconds, as a float | |
3684 | fa1, fa2 - list of file attributes | |
3685 | returned by function `file-attributes' | |
3686 | ||
3687 | where 1 refers to attribute of file in the current dired buffer | |
3688 | and 2 to attribute of file in second dired buffer. | |
3689 | ||
3690 | Examples of PREDICATE: | |
3691 | ||
3692 | (> mtime1 mtime2) - mark newer files | |
3693 | (not (= size1 size2)) - mark files with different sizes | |
3694 | (not (string= (nth 8 fa1) (nth 8 fa2))) - mark files with different modes | |
3695 | (not (and (= (nth 2 fa1) (nth 2 fa2)) - mark files with different UID | |
3696 | (= (nth 3 fa1) (nth 3 fa2)))) and GID. | |
3697 | ||
3698 | \(fn DIR2 PREDICATE)" t nil) | |
3699 | ||
3700 | (autoload 'dired-do-chmod "dired-aux" "\ | |
3701 | Change the mode of the marked (or next ARG) files. | |
3702 | Symbolic modes like `g+w' are allowed. | |
3703 | ||
3704 | \(fn &optional ARG)" t nil) | |
3705 | ||
3706 | (autoload 'dired-do-chgrp "dired-aux" "\ | |
3707 | Change the group of the marked (or next ARG) files. | |
3708 | ||
3709 | \(fn &optional ARG)" t nil) | |
3710 | ||
3711 | (autoload 'dired-do-chown "dired-aux" "\ | |
3712 | Change the owner of the marked (or next ARG) files. | |
3713 | ||
3714 | \(fn &optional ARG)" t nil) | |
3715 | ||
3716 | (autoload 'dired-do-touch "dired-aux" "\ | |
3717 | Change the timestamp of the marked (or next ARG) files. | |
3718 | This calls touch. | |
3719 | ||
3720 | \(fn &optional ARG)" t nil) | |
3721 | ||
3722 | (autoload 'dired-do-print "dired-aux" "\ | |
3723 | Print the marked (or next ARG) files. | |
3724 | Uses the shell command coming from variables `lpr-command' and | |
3725 | `lpr-switches' as default. | |
3726 | ||
3727 | \(fn &optional ARG)" t nil) | |
3728 | ||
3729 | (autoload 'dired-clean-directory "dired-aux" "\ | |
3730 | Flag numerical backups for deletion. | |
3731 | Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest. | |
3732 | Positive prefix arg KEEP overrides `dired-kept-versions'; | |
3733 | Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive. | |
3734 | ||
3735 | To clear the flags on these files, you can use \\[dired-flag-backup-files] | |
3736 | with a prefix argument. | |
3737 | ||
3738 | \(fn KEEP)" t nil) | |
3739 | ||
3740 | (autoload 'dired-do-async-shell-command "dired-aux" "\ | |
3741 | Run a shell command COMMAND on the marked files asynchronously. | |
3742 | ||
3743 | Like `dired-do-shell-command' but if COMMAND doesn't end in ampersand, | |
3744 | adds `* &' surrounded by whitespace and executes the command asynchronously. | |
3745 | The output appears in the buffer `*Async Shell Command*'. | |
3746 | ||
3747 | \(fn COMMAND &optional ARG FILE-LIST)" t nil) | |
3748 | ||
3749 | (autoload 'dired-do-shell-command "dired-aux" "\ | |
3750 | Run a shell command COMMAND on the marked files. | |
3751 | If no files are marked or a specific numeric prefix arg is given, | |
3752 | the next ARG files are used. Just \\[universal-argument] means the current file. | |
3753 | The prompt mentions the file(s) or the marker, as appropriate. | |
3754 | ||
3755 | If there is a `*' in COMMAND, surrounded by whitespace, this runs | |
3756 | COMMAND just once with the entire file list substituted there. | |
3757 | ||
3758 | If there is no `*', but there is a `?' in COMMAND, surrounded by | |
3759 | whitespace, this runs COMMAND on each file individually with the | |
3760 | file name substituted for `?'. | |
3761 | ||
3762 | Otherwise, this runs COMMAND on each file individually with the | |
3763 | file name added at the end of COMMAND (separated by a space). | |
3764 | ||
3765 | `*' and `?' when not surrounded by whitespace have no special | |
3766 | significance for `dired-do-shell-command', and are passed through | |
3767 | normally to the shell, but you must confirm first. To pass `*' by | |
3768 | itself to the shell as a wildcard, type `*\"\"'. | |
3769 | ||
3770 | If COMMAND produces output, it goes to a separate buffer. | |
3771 | ||
3772 | This feature does not try to redisplay Dired buffers afterward, as | |
3773 | there's no telling what files COMMAND may have changed. | |
3774 | Type \\[dired-do-redisplay] to redisplay the marked files. | |
3775 | ||
5a0c3f56 JB |
3776 | When COMMAND runs, its working directory is the top-level directory |
3777 | of the Dired buffer, so output files usually are created there | |
3778 | instead of in a subdir. | |
7d72f6f1 GM |
3779 | |
3780 | In a noninteractive call (from Lisp code), you must specify | |
3781 | the list of file names explicitly with the FILE-LIST argument, which | |
3782 | can be produced by `dired-get-marked-files', for example. | |
3783 | ||
3784 | \(fn COMMAND &optional ARG FILE-LIST)" t nil) | |
3785 | ||
3786 | (autoload 'dired-run-shell-command "dired-aux" "\ | |
6a56b5bd | 3787 | |
7d72f6f1 GM |
3788 | |
3789 | \(fn COMMAND)" nil nil) | |
3790 | ||
3791 | (autoload 'dired-do-kill-lines "dired-aux" "\ | |
3792 | Kill all marked lines (not the files). | |
3793 | With a prefix argument, kill that many lines starting with the current line. | |
3794 | \(A negative argument kills backward.) | |
3795 | If you use this command with a prefix argument to kill the line | |
3796 | for a file that is a directory, which you have inserted in the | |
3797 | Dired buffer as a subdirectory, then it deletes that subdirectory | |
3798 | from the buffer as well. | |
3799 | To kill an entire subdirectory (without killing its line in the | |
3800 | parent directory), go to its directory header line and use this | |
3801 | command with a prefix argument (the value does not matter). | |
3802 | ||
3803 | \(fn &optional ARG FMT)" t nil) | |
3804 | ||
3805 | (autoload 'dired-compress-file "dired-aux" "\ | |
6a56b5bd | 3806 | |
7d72f6f1 GM |
3807 | |
3808 | \(fn FILE)" nil nil) | |
3809 | ||
3810 | (autoload 'dired-query "dired-aux" "\ | |
3ef01959 CY |
3811 | Format PROMPT with ARGS, query user, and store the result in SYM. |
3812 | The return value is either nil or t. | |
7d72f6f1 | 3813 | |
3ef01959 CY |
3814 | The user may type y or SPC to accept once; n or DEL to skip once; |
3815 | ! to accept this and subsequent queries; or q or ESC to decline | |
3816 | this and subsequent queries. | |
3817 | ||
3818 | If SYM is already bound to a non-nil value, this function may | |
3819 | return automatically without querying the user. If SYM is !, | |
3820 | return t; if SYM is q or ESC, return nil. | |
3821 | ||
3822 | \(fn SYM PROMPT &rest ARGS)" nil nil) | |
7d72f6f1 GM |
3823 | |
3824 | (autoload 'dired-do-compress "dired-aux" "\ | |
3825 | Compress or uncompress marked (or next ARG) files. | |
3826 | ||
3827 | \(fn &optional ARG)" t nil) | |
3828 | ||
3829 | (autoload 'dired-do-byte-compile "dired-aux" "\ | |
3830 | Byte compile marked (or next ARG) Emacs Lisp files. | |
3831 | ||
3832 | \(fn &optional ARG)" t nil) | |
3833 | ||
3834 | (autoload 'dired-do-load "dired-aux" "\ | |
3835 | Load the marked (or next ARG) Emacs Lisp files. | |
3836 | ||
3837 | \(fn &optional ARG)" t nil) | |
3838 | ||
3839 | (autoload 'dired-do-redisplay "dired-aux" "\ | |
3840 | Redisplay all marked (or next ARG) files. | |
3841 | If on a subdir line, redisplay that subdirectory. In that case, | |
3842 | a prefix arg lets you edit the `ls' switches used for the new listing. | |
3843 | ||
3844 | Dired remembers switches specified with a prefix arg, so that reverting | |
3845 | the buffer will not reset them. However, using `dired-undo' to re-insert | |
3846 | or delete subdirectories can bypass this machinery. Hence, you sometimes | |
3847 | may have to reset some subdirectory switches after a `dired-undo'. | |
3848 | You can reset all subdirectory switches to the default using | |
3849 | \\<dired-mode-map>\\[dired-reset-subdir-switches]. | |
3850 | See Info node `(emacs)Subdir switches' for more details. | |
3851 | ||
3852 | \(fn &optional ARG TEST-FOR-SUBDIR)" t nil) | |
3853 | ||
3854 | (autoload 'dired-add-file "dired-aux" "\ | |
6a56b5bd | 3855 | |
7d72f6f1 GM |
3856 | |
3857 | \(fn FILENAME &optional MARKER-CHAR)" nil nil) | |
3858 | ||
3859 | (autoload 'dired-remove-file "dired-aux" "\ | |
6a56b5bd | 3860 | |
7d72f6f1 GM |
3861 | |
3862 | \(fn FILE)" nil nil) | |
3863 | ||
3864 | (autoload 'dired-relist-file "dired-aux" "\ | |
3865 | Create or update the line for FILE in all Dired buffers it would belong in. | |
3866 | ||
3867 | \(fn FILE)" nil nil) | |
3868 | ||
3869 | (autoload 'dired-copy-file "dired-aux" "\ | |
6a56b5bd | 3870 | |
7d72f6f1 GM |
3871 | |
3872 | \(fn FROM TO OK-FLAG)" nil nil) | |
3873 | ||
3874 | (autoload 'dired-rename-file "dired-aux" "\ | |
6a56b5bd | 3875 | |
7d72f6f1 GM |
3876 | |
3877 | \(fn FILE NEWNAME OK-IF-ALREADY-EXISTS)" nil nil) | |
3878 | ||
3879 | (autoload 'dired-create-directory "dired-aux" "\ | |
3880 | Create a directory called DIRECTORY. | |
ff854b0b | 3881 | If DIRECTORY already exists, signal an error. |
7d72f6f1 GM |
3882 | |
3883 | \(fn DIRECTORY)" t nil) | |
3884 | ||
3885 | (autoload 'dired-do-copy "dired-aux" "\ | |
3886 | Copy all marked (or next ARG) files, or copy the current file. | |
3887 | This normally preserves the last-modified date when copying. | |
3888 | When operating on just the current file, you specify the new name. | |
3889 | When operating on multiple or marked files, you specify a directory, | |
3890 | and new copies of these files are made in that directory | |
3891 | with the same names that the files currently have. The default | |
3892 | suggested for the target directory depends on the value of | |
3893 | `dired-dwim-target', which see. | |
3894 | ||
3895 | This command copies symbolic links by creating new ones, | |
3896 | like `cp -d'. | |
3897 | ||
3898 | \(fn &optional ARG)" t nil) | |
3899 | ||
3900 | (autoload 'dired-do-symlink "dired-aux" "\ | |
3901 | Make symbolic links to current file or all marked (or next ARG) files. | |
3902 | When operating on just the current file, you specify the new name. | |
3903 | When operating on multiple or marked files, you specify a directory | |
3904 | and new symbolic links are made in that directory | |
3905 | with the same names that the files currently have. The default | |
3906 | suggested for the target directory depends on the value of | |
3907 | `dired-dwim-target', which see. | |
3908 | ||
3909 | For relative symlinks, use \\[dired-do-relsymlink]. | |
3910 | ||
3911 | \(fn &optional ARG)" t nil) | |
3912 | ||
3913 | (autoload 'dired-do-hardlink "dired-aux" "\ | |
3914 | Add names (hard links) current file or all marked (or next ARG) files. | |
3915 | When operating on just the current file, you specify the new name. | |
3916 | When operating on multiple or marked files, you specify a directory | |
3917 | and new hard links are made in that directory | |
3918 | with the same names that the files currently have. The default | |
3919 | suggested for the target directory depends on the value of | |
3920 | `dired-dwim-target', which see. | |
3921 | ||
3922 | \(fn &optional ARG)" t nil) | |
3923 | ||
3924 | (autoload 'dired-do-rename "dired-aux" "\ | |
3925 | Rename current file or all marked (or next ARG) files. | |
3926 | When renaming just the current file, you specify the new name. | |
3927 | When renaming multiple or marked files, you specify a directory. | |
3928 | This command also renames any buffers that are visiting the files. | |
3929 | The default suggested for the target directory depends on the value | |
3930 | of `dired-dwim-target', which see. | |
3931 | ||
3932 | \(fn &optional ARG)" t nil) | |
3933 | ||
3934 | (autoload 'dired-do-rename-regexp "dired-aux" "\ | |
3935 | Rename selected files whose names match REGEXP to NEWNAME. | |
3936 | ||
3937 | With non-zero prefix argument ARG, the command operates on the next ARG | |
3938 | files. Otherwise, it operates on all the marked files, or the current | |
3939 | file if none are marked. | |
3940 | ||
3941 | As each match is found, the user must type a character saying | |
3942 | what to do with it. For directions, type \\[help-command] at that time. | |
3943 | NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'. | |
3944 | REGEXP defaults to the last regexp used. | |
3945 | ||
3946 | With a zero prefix arg, renaming by regexp affects the absolute file name. | |
3947 | Normally, only the non-directory part of the file name is used and changed. | |
3948 | ||
3949 | \(fn REGEXP NEWNAME &optional ARG WHOLE-NAME)" t nil) | |
3950 | ||
3951 | (autoload 'dired-do-copy-regexp "dired-aux" "\ | |
3952 | Copy selected files whose names match REGEXP to NEWNAME. | |
3953 | See function `dired-do-rename-regexp' for more info. | |
3954 | ||
3955 | \(fn REGEXP NEWNAME &optional ARG WHOLE-NAME)" t nil) | |
3956 | ||
3957 | (autoload 'dired-do-hardlink-regexp "dired-aux" "\ | |
3958 | Hardlink selected files whose names match REGEXP to NEWNAME. | |
3959 | See function `dired-do-rename-regexp' for more info. | |
3960 | ||
3961 | \(fn REGEXP NEWNAME &optional ARG WHOLE-NAME)" t nil) | |
3962 | ||
3963 | (autoload 'dired-do-symlink-regexp "dired-aux" "\ | |
3964 | Symlink selected files whose names match REGEXP to NEWNAME. | |
3965 | See function `dired-do-rename-regexp' for more info. | |
3966 | ||
3967 | \(fn REGEXP NEWNAME &optional ARG WHOLE-NAME)" t nil) | |
3968 | ||
3969 | (autoload 'dired-upcase "dired-aux" "\ | |
3970 | Rename all marked (or next ARG) files to upper case. | |
3971 | ||
3972 | \(fn &optional ARG)" t nil) | |
3973 | ||
3974 | (autoload 'dired-downcase "dired-aux" "\ | |
3975 | Rename all marked (or next ARG) files to lower case. | |
3976 | ||
3977 | \(fn &optional ARG)" t nil) | |
3978 | ||
3979 | (autoload 'dired-maybe-insert-subdir "dired-aux" "\ | |
3980 | Insert this subdirectory into the same dired buffer. | |
3981 | If it is already present, just move to it (type \\[dired-do-redisplay] to refresh), | |
3982 | else inserts it at its natural place (as `ls -lR' would have done). | |
3983 | With a prefix arg, you may edit the ls switches used for this listing. | |
3984 | You can add `R' to the switches to expand the whole tree starting at | |
3985 | this subdirectory. | |
3986 | This function takes some pains to conform to `ls -lR' output. | |
3987 | ||
3988 | Dired remembers switches specified with a prefix arg, so that reverting | |
3989 | the buffer will not reset them. However, using `dired-undo' to re-insert | |
3990 | or delete subdirectories can bypass this machinery. Hence, you sometimes | |
3991 | may have to reset some subdirectory switches after a `dired-undo'. | |
3992 | You can reset all subdirectory switches to the default using | |
3993 | \\<dired-mode-map>\\[dired-reset-subdir-switches]. | |
3994 | See Info node `(emacs)Subdir switches' for more details. | |
3995 | ||
3996 | \(fn DIRNAME &optional SWITCHES NO-ERROR-IF-NOT-DIR-P)" t nil) | |
3997 | ||
3998 | (autoload 'dired-insert-subdir "dired-aux" "\ | |
3999 | Insert this subdirectory into the same dired buffer. | |
4000 | If it is already present, overwrites previous entry, | |
4001 | else inserts it at its natural place (as `ls -lR' would have done). | |
4002 | With a prefix arg, you may edit the `ls' switches used for this listing. | |
4003 | You can add `R' to the switches to expand the whole tree starting at | |
4004 | this subdirectory. | |
4005 | This function takes some pains to conform to `ls -lR' output. | |
4006 | ||
4007 | \(fn DIRNAME &optional SWITCHES NO-ERROR-IF-NOT-DIR-P)" t nil) | |
4008 | ||
4009 | (autoload 'dired-prev-subdir "dired-aux" "\ | |
4010 | Go to previous subdirectory, regardless of level. | |
4011 | When called interactively and not on a subdir line, go to this subdir's line. | |
4012 | ||
4013 | \(fn ARG &optional NO-ERROR-IF-NOT-FOUND NO-SKIP)" t nil) | |
4014 | ||
4015 | (autoload 'dired-goto-subdir "dired-aux" "\ | |
4016 | Go to end of header line of DIR in this dired buffer. | |
4017 | Return value of point on success, otherwise return nil. | |
4018 | The next char is either \\n, or \\r if DIR is hidden. | |
4019 | ||
4020 | \(fn DIR)" t nil) | |
4021 | ||
4022 | (autoload 'dired-mark-subdir-files "dired-aux" "\ | |
4023 | Mark all files except `.' and `..' in current subdirectory. | |
4024 | If the Dired buffer shows multiple directories, this command | |
4025 | marks the files listed in the subdirectory that point is in. | |
4026 | ||
4027 | \(fn)" t nil) | |
4028 | ||
4029 | (autoload 'dired-kill-subdir "dired-aux" "\ | |
4030 | Remove all lines of current subdirectory. | |
4031 | Lower levels are unaffected. | |
4032 | ||
4033 | \(fn &optional REMEMBER-MARKS)" t nil) | |
4034 | ||
4035 | (autoload 'dired-tree-up "dired-aux" "\ | |
4036 | Go up ARG levels in the dired tree. | |
4037 | ||
4038 | \(fn ARG)" t nil) | |
4039 | ||
4040 | (autoload 'dired-tree-down "dired-aux" "\ | |
4041 | Go down in the dired tree. | |
4042 | ||
4043 | \(fn)" t nil) | |
4044 | ||
4045 | (autoload 'dired-hide-subdir "dired-aux" "\ | |
4046 | Hide or unhide the current subdirectory and move to next directory. | |
4047 | Optional prefix arg is a repeat factor. | |
4048 | Use \\[dired-hide-all] to (un)hide all directories. | |
4049 | ||
4050 | \(fn ARG)" t nil) | |
4051 | ||
4052 | (autoload 'dired-hide-all "dired-aux" "\ | |
4053 | Hide all subdirectories, leaving only their header lines. | |
4054 | If there is already something hidden, make everything visible again. | |
4055 | Use \\[dired-hide-subdir] to (un)hide a particular subdirectory. | |
4056 | ||
8ae41cbc | 4057 | \(fn &optional IGNORED)" t nil) |
7d72f6f1 GM |
4058 | |
4059 | (autoload 'dired-isearch-filenames-setup "dired-aux" "\ | |
4060 | Set up isearch to search in Dired file names. | |
4061 | Intended to be added to `isearch-mode-hook'. | |
4062 | ||
4063 | \(fn)" nil nil) | |
4064 | ||
4065 | (autoload 'dired-isearch-filenames "dired-aux" "\ | |
4066 | Search for a string using Isearch only in file names in the Dired buffer. | |
4067 | ||
4068 | \(fn)" t nil) | |
4069 | ||
4070 | (autoload 'dired-isearch-filenames-regexp "dired-aux" "\ | |
4071 | Search for a regexp using Isearch only in file names in the Dired buffer. | |
4072 | ||
4073 | \(fn)" t nil) | |
4074 | ||
4075 | (autoload 'dired-do-isearch "dired-aux" "\ | |
4076 | Search for a string through all marked files using Isearch. | |
4077 | ||
4078 | \(fn)" t nil) | |
4079 | ||
4080 | (autoload 'dired-do-isearch-regexp "dired-aux" "\ | |
4081 | Search for a regexp through all marked files using Isearch. | |
4082 | ||
4083 | \(fn)" t nil) | |
4084 | ||
4085 | (autoload 'dired-do-search "dired-aux" "\ | |
4086 | Search through all marked files for a match for REGEXP. | |
4087 | Stops when a match is found. | |
4088 | To continue searching for next match, use command \\[tags-loop-continue]. | |
4089 | ||
4090 | \(fn REGEXP)" t nil) | |
4091 | ||
4092 | (autoload 'dired-do-query-replace-regexp "dired-aux" "\ | |
4093 | Do `query-replace-regexp' of FROM with TO, on all marked files. | |
4094 | Third arg DELIMITED (prefix arg) means replace only word-delimited matches. | |
4095 | If you exit (\\[keyboard-quit], RET or q), you can resume the query replace | |
4096 | with the command \\[tags-loop-continue]. | |
4097 | ||
4098 | \(fn FROM TO &optional DELIMITED)" t nil) | |
4099 | ||
4100 | (autoload 'dired-show-file-type "dired-aux" "\ | |
4101 | Print the type of FILE, according to the `file' command. | |
a9e2a8db GM |
4102 | If you give a prefix to this command, and FILE is a symbolic |
4103 | link, then the type of the file linked to by FILE is printed | |
4104 | instead. | |
7d72f6f1 GM |
4105 | |
4106 | \(fn FILE &optional DEREF-SYMLINKS)" t nil) | |
4107 | ||
4108 | ;;;*** | |
4109 | \f | |
42eabe38 | 4110 | ;;;### (autoloads (dired-do-relsymlink dired-jump-other-window dired-jump) |
854e5fa8 | 4111 | ;;;;;; "dired-x" "dired-x.el" "219648338c42c7912fa336680b434db0") |
7d72f6f1 GM |
4112 | ;;; Generated autoloads from dired-x.el |
4113 | ||
4114 | (autoload 'dired-jump "dired-x" "\ | |
4115 | Jump to dired buffer corresponding to current buffer. | |
4116 | If in a file, dired the current directory and move to file's line. | |
4117 | If in Dired already, pop up a level and goto old directory's line. | |
4118 | In case the proper dired file line cannot be found, refresh the dired | |
4119 | buffer and try again. | |
f5d6548a JL |
4120 | When OTHER-WINDOW is non-nil, jump to dired buffer in other window. |
4121 | Interactively with prefix argument, read FILE-NAME and | |
4122 | move to its line in dired. | |
7d72f6f1 | 4123 | |
f5d6548a | 4124 | \(fn &optional OTHER-WINDOW FILE-NAME)" t nil) |
7d72f6f1 | 4125 | |
42eabe38 AS |
4126 | (autoload 'dired-jump-other-window "dired-x" "\ |
4127 | Like \\[dired-jump] (`dired-jump') but in other window. | |
4128 | ||
4129 | \(fn &optional FILE-NAME)" t nil) | |
4130 | ||
7d72f6f1 GM |
4131 | (autoload 'dired-do-relsymlink "dired-x" "\ |
4132 | Relative symlink all marked (or next ARG) files into a directory. | |
4133 | Otherwise make a relative symbolic link to the current file. | |
4134 | This creates relative symbolic links like | |
4135 | ||
4136 | foo -> ../bar/foo | |
4137 | ||
4138 | not absolute ones like | |
4139 | ||
4140 | foo -> /ugly/file/name/that/may/change/any/day/bar/foo | |
4141 | ||
4142 | For absolute symlinks, use \\[dired-do-symlink]. | |
4143 | ||
4144 | \(fn &optional ARG)" t nil) | |
4145 | ||
4146 | ;;;*** | |
4147 | \f | |
4148 | ;;; End of automatically extracted autoloads. | |
4149 | ||
52041219 RS |
4150 | (provide 'dired) |
4151 | ||
57e15005 BF |
4152 | (run-hooks 'dired-load-hook) ; for your customizations |
4153 | ||
52041219 | 4154 | ;;; dired.el ends here |