Merge from trunk
[bpt/emacs.git] / lisp / dired-x.el
1 ;;; dired-x.el --- extra Dired functionality
2
3 ;; Copyright (C) 1993-1994, 1997, 2001-2011 Free Software Foundation, Inc.
4
5 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
6 ;; Lawrence R. Dodd <dodd@roebling.poly.edu>
7 ;; Maintainer: Romain Francoise <rfrancoise@gnu.org>
8 ;; Keywords: dired extensions files
9 ;; Package: emacs
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
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
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; This is based on Sebastian Kremer's excellent dired-x.el (Dired Extra),
29 ;; version 1.191, adapted for GNU Emacs. See the `dired-x' info pages.
30
31 ;; USAGE: In your ~/.emacs,
32 ;;
33 ;; (add-hook 'dired-load-hook
34 ;; (lambda ()
35 ;; (load "dired-x")
36 ;; ;; Set global variables here. For example:
37 ;; ;; (setq dired-guess-shell-gnutar "gtar")
38 ;; ))
39 ;; (add-hook 'dired-mode-hook
40 ;; (lambda ()
41 ;; ;; Set buffer-local variables here. For example:
42 ;; ;; (dired-omit-mode 1)
43 ;; ))
44 ;;
45 ;; At load time dired-x.el will install itself and bind some dired keys.
46 ;; Some dired.el and dired-aux.el functions have extra features if
47 ;; dired-x is loaded.
48
49 ;; User customization: M-x customize-group RET dired-x RET.
50
51 ;; *Please* see the `dired-x' info pages for more details.
52
53 \f
54 ;;; Code:
55
56 ;; This is a no-op if dired-x is being loaded via `dired-load-hook',
57 ;; but maybe not if a dired-x function is being autoloaded.
58 (require 'dired)
59
60 ;;; User-defined variables.
61
62 (defgroup dired-x nil
63 "Extended directory editing (dired-x)."
64 :group 'dired)
65
66 (defgroup dired-keys nil
67 "Dired keys customizations."
68 :prefix "dired-"
69 :group 'dired-x)
70
71 (defcustom dired-bind-vm nil
72 "Non-nil means \"V\" runs `dired-vm', otherwise \"V\" runs `dired-rmail'.
73 RMAIL files in the old Babyl format (used before before Emacs 23.1)
74 contain \"-*- rmail -*-\" at the top, so `dired-find-file'
75 will run `rmail' on these files. New RMAIL files use the standard
76 mbox format, and so cannot be distinguished in this way."
77 :type 'boolean
78 :group 'dired-keys)
79
80 (defcustom dired-bind-jump t
81 "Non-nil means bind `dired-jump' to C-x C-j, otherwise do not.
82 Setting this variable directly after dired-x is loaded has no effect -
83 use \\[customize]."
84 :type 'boolean
85 :set (lambda (sym val)
86 (if (set sym val)
87 (progn
88 (define-key global-map "\C-x\C-j" 'dired-jump)
89 (define-key global-map "\C-x4\C-j" 'dired-jump-other-window))
90 (if (eq 'dired-jump (lookup-key global-map "\C-x\C-j"))
91 (define-key global-map "\C-x\C-j" nil))
92 (if (eq 'dired-jump-other-window (lookup-key global-map "\C-x4\C-j"))
93 (define-key global-map "\C-x4\C-j" nil))))
94 :group 'dired-keys)
95
96 (defcustom dired-bind-man t
97 "Non-nil means bind `dired-man' to \"N\" in dired-mode, otherwise do not.
98 Setting this variable directly after dired-x is loaded has no effect -
99 use \\[customize]."
100 :type 'boolean
101 :set (lambda (sym val)
102 (if (set sym val)
103 (define-key dired-mode-map "N" 'dired-man)
104 (if (eq 'dired-man (lookup-key dired-mode-map "N"))
105 (define-key dired-mode-map "N" nil))))
106 :group 'dired-keys)
107
108 (defcustom dired-bind-info t
109 "Non-nil means bind `dired-info' to \"I\" in dired-mode, otherwise do not.
110 Setting this variable directly after dired-x is loaded has no effect -
111 use \\[customize]."
112 :type 'boolean
113 :set (lambda (sym val)
114 (if (set sym val)
115 (define-key dired-mode-map "I" 'dired-info)
116 (if (eq 'dired-info (lookup-key dired-mode-map "I"))
117 (define-key dired-mode-map "I" nil))))
118 :group 'dired-keys)
119
120 (defcustom dired-vm-read-only-folders nil
121 "If non-nil, \\[dired-vm] will visit all folders read-only.
122 If neither nil nor t, e.g. the symbol `if-file-read-only', only
123 files not writable by you are visited read-only."
124 :type '(choice (const :tag "off" nil)
125 (const :tag "on" t)
126 (other :tag "non-writable only" if-file-read-only))
127 :group 'dired-x)
128
129 (define-minor-mode dired-omit-mode
130 "Toggle Dired-Omit mode.
131 With numeric ARG, enable Dired-Omit mode if ARG is positive, disable
132 otherwise. Enabling and disabling is buffer-local.
133 If enabled, \"uninteresting\" files are not listed.
134 Uninteresting files are those whose filenames match regexp `dired-omit-files',
135 plus those ending with extensions in `dired-omit-extensions'.
136
137 To enable omitting in every Dired buffer, you can put in your ~/.emacs
138
139 (add-hook 'dired-mode-hook (lambda () (dired-omit-mode 1)))
140
141 See Info node `(dired-x) Omitting Variables' for more information."
142 :group 'dired-x
143 (if dired-omit-mode
144 ;; This will mention how many lines were omitted:
145 (let ((dired-omit-size-limit nil)) (dired-omit-expunge))
146 (revert-buffer)))
147
148 ;; For backward compatibility
149 (define-obsolete-variable-alias 'dired-omit-files-p 'dired-omit-mode "22.1")
150
151 (defcustom dired-omit-files "^\\.?#\\|^\\.$\\|^\\.\\.$"
152 "Filenames matching this regexp will not be displayed.
153 This only has effect when `dired-omit-mode' is t. See interactive function
154 `dired-omit-mode' \(\\[dired-omit-mode]\) and variable
155 `dired-omit-extensions'. The default is to omit `.', `..', auto-save
156 files and lock files."
157 :type 'regexp
158 :group 'dired-x)
159
160 (defcustom dired-omit-verbose t
161 "When non-nil, show messages when omitting files.
162 When nil, don't show messages."
163 :type 'boolean
164 :group 'dired-x)
165
166 (defcustom dired-find-subdir nil ; t is pretty near to DWIM...
167 "If non-nil, Dired always finds a directory in a buffer of its own.
168 If nil, Dired finds the directory as a subdirectory in some other buffer
169 if it is present as one.
170
171 If there are several dired buffers for a directory, the most recently
172 used is chosen.
173
174 Dired avoids switching to the current buffer, so that if you have
175 a normal and a wildcard buffer for the same directory, \\[dired] will
176 toggle between those two."
177 :type 'boolean
178 :group 'dired-x)
179
180 (defcustom dired-omit-size-limit 30000
181 "Maximum size for the \"omitting\" feature.
182 If nil, there is no maximum size."
183 :type '(choice (const :tag "no maximum" nil) integer)
184 :group 'dired-x)
185
186 (defcustom dired-enable-local-variables t
187 "Control use of local-variables lists in Dired.
188 The value can be t, nil or something else.
189 A value of t means local-variables lists are obeyed;
190 nil means they are ignored; anything else means query.
191
192 This temporarily overrides the value of `enable-local-variables' when
193 listing a directory. See also `dired-local-variables-file'."
194 :type 'boolean
195 :group 'dired-x)
196
197 (defcustom dired-guess-shell-gnutar
198 (catch 'found
199 (dolist (exe '("tar" "gtar"))
200 (if (with-temp-buffer
201 (ignore-errors (call-process exe nil t nil "--version"))
202 (and (re-search-backward "GNU tar" nil t) t))
203 (throw 'found exe))))
204 "If non-nil, name of GNU tar executable.
205 \(E.g., \"tar\" or \"gtar\"). The `z' switch will be used with it for
206 compressed or gzip'ed tar files. If you don't have GNU tar, set this
207 to nil: a pipe using `zcat' or `gunzip -c' will be used."
208 ;; Changed from system-type test to testing --version output.
209 ;; Maybe test --help for -z instead?
210 :version "24.1"
211 :type '(choice (const :tag "Not GNU tar" nil)
212 (string :tag "Command name"))
213 :group 'dired-x)
214
215 (defcustom dired-guess-shell-gzip-quiet t
216 "Non-nil says pass -q to gzip overriding verbose GZIP environment."
217 :type 'boolean
218 :group 'dired-x)
219
220 (defcustom dired-guess-shell-znew-switches nil
221 "If non-nil, then string of switches passed to `znew', example: \"-K\"."
222 :type '(choice (const :tag "None" nil)
223 (string :tag "Switches"))
224 :group 'dired-x)
225
226 (defcustom dired-clean-up-buffers-too t
227 "Non-nil means offer to kill buffers visiting files and dirs deleted in Dired."
228 :type 'boolean
229 :group 'dired-x)
230
231 ;;; KEY BINDINGS.
232
233 (define-key dired-mode-map "\M-o" 'dired-omit-mode)
234 (define-key dired-mode-map "*O" 'dired-mark-omitted)
235 (define-key dired-mode-map "\M-(" 'dired-mark-sexp)
236 (define-key dired-mode-map "*(" 'dired-mark-sexp)
237 (define-key dired-mode-map "*." 'dired-mark-extension)
238 (define-key dired-mode-map "\M-!" 'dired-smart-shell-command)
239 (define-key dired-mode-map "\M-G" 'dired-goto-subdir)
240 (define-key dired-mode-map "F" 'dired-do-find-marked-files)
241 (define-key dired-mode-map "Y" 'dired-do-relsymlink)
242 (define-key dired-mode-map "%Y" 'dired-do-relsymlink-regexp)
243 (define-key dired-mode-map "V" 'dired-do-run-mail)
244
245 ;;; MENU BINDINGS
246
247 (require 'easymenu)
248
249 (let ((menu (lookup-key dired-mode-map [menu-bar])))
250 (easy-menu-add-item menu '("Operate")
251 ["Find Files" dired-do-find-marked-files
252 :help "Find current or marked files"]
253 "Shell Command...")
254 (easy-menu-add-item menu '("Operate")
255 ["Relative Symlink to..." dired-do-relsymlink
256 :visible (fboundp 'make-symbolic-link)
257 :help "Make relative symbolic links for current or \
258 marked files"]
259 "Hardlink to...")
260 (easy-menu-add-item menu '("Mark")
261 ["Flag Extension..." dired-flag-extension
262 :help "Flag files with a certain extension for deletion"]
263 "Mark Executables")
264 (easy-menu-add-item menu '("Mark")
265 ["Mark Extension..." dired-mark-extension
266 :help "Mark files with a certain extension"]
267 "Unmark All")
268 (easy-menu-add-item menu '("Mark")
269 ["Mark Omitted" dired-mark-omitted
270 :help "Mark files matching `dired-omit-files' \
271 and `dired-omit-extensions'"]
272 "Unmark All")
273 (easy-menu-add-item menu '("Regexp")
274 ["Relative Symlink..." dired-do-relsymlink-regexp
275 :visible (fboundp 'make-symbolic-link)
276 :help "Make relative symbolic links for files \
277 matching regexp"]
278 "Hardlink...")
279 (easy-menu-add-item menu '("Immediate")
280 ["Omit Mode" dired-omit-mode
281 :style toggle :selected dired-omit-mode
282 :help "Enable or disable omitting \"uninteresting\" \
283 files"]
284 "Refresh"))
285
286 \f
287 ;; Install into appropriate hooks.
288
289 (add-hook 'dired-mode-hook 'dired-extra-startup)
290 (add-hook 'dired-after-readin-hook 'dired-omit-expunge)
291
292 (defun dired-extra-startup ()
293 "Automatically put on `dired-mode-hook' to get extra Dired features:
294 \\<dired-mode-map>
295 \\[dired-do-run-mail]\t-- run mail on folder (see `dired-bind-vm')
296 \\[dired-info]\t-- run info on file
297 \\[dired-man]\t-- run man on file
298 \\[dired-do-find-marked-files]\t-- visit all marked files simultaneously
299 \\[dired-omit-mode]\t-- toggle omitting of files
300 \\[dired-mark-sexp]\t-- mark by Lisp expression
301
302 To see the options you can set, use M-x customize-group RET dired-x RET.
303 See also the functions:
304 `dired-flag-extension'
305 `dired-virtual'
306 `dired-jump'
307 `dired-man'
308 `dired-vm'
309 `dired-rmail'
310 `dired-info'
311 `dired-do-find-marked-files'"
312 (interactive)
313 ;; These must be done in each new dired buffer.
314 (dired-hack-local-variables)
315 (dired-omit-startup))
316
317 \f
318 ;;; EXTENSION MARKING FUNCTIONS.
319
320 ;; Mark files with some extension.
321 (defun dired-mark-extension (extension &optional marker-char)
322 "Mark all files with a certain EXTENSION for use in later commands.
323 A `.' is *not* automatically prepended to the string entered."
324 ;; EXTENSION may also be a list of extensions instead of a single one.
325 ;; Optional MARKER-CHAR is marker to use.
326 (interactive "sMarking extension: \nP")
327 (or (listp extension)
328 (setq extension (list extension)))
329 (dired-mark-files-regexp
330 (concat ".";; don't match names with nothing but an extension
331 "\\("
332 (mapconcat 'regexp-quote extension "\\|")
333 "\\)$")
334 marker-char))
335
336 (defun dired-flag-extension (extension)
337 "In dired, flag all files with a certain EXTENSION for deletion.
338 A `.' is *not* automatically prepended to the string entered."
339 (interactive "sFlagging extension: ")
340 (dired-mark-extension extension dired-del-marker))
341
342 ;; Define some unpopular file extensions. Used for cleaning and omitting.
343
344 (defvar dired-patch-unclean-extensions
345 '(".rej" ".orig")
346 "List of extensions of dispensable files created by the `patch' program.")
347
348 (defvar dired-tex-unclean-extensions
349 '(".toc" ".log" ".aux");; these are already in completion-ignored-extensions
350 "List of extensions of dispensable files created by TeX.")
351
352 (defvar dired-latex-unclean-extensions
353 '(".idx" ".lof" ".lot" ".glo")
354 "List of extensions of dispensable files created by LaTeX.")
355
356 (defvar dired-bibtex-unclean-extensions
357 '(".blg" ".bbl")
358 "List of extensions of dispensable files created by BibTeX.")
359
360 (defvar dired-texinfo-unclean-extensions
361 '(".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
362 ".tp" ".tps" ".vr" ".vrs")
363 "List of extensions of dispensable files created by texinfo.")
364
365 (defun dired-clean-patch ()
366 "Flag dispensable files created by patch for deletion.
367 See variable `dired-patch-unclean-extensions'."
368 (interactive)
369 (dired-flag-extension dired-patch-unclean-extensions))
370
371 (defun dired-clean-tex ()
372 "Flag dispensable files created by [La]TeX etc. for deletion.
373 See variables `dired-tex-unclean-extensions',
374 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
375 `dired-texinfo-unclean-extensions'."
376 (interactive)
377 (dired-flag-extension (append dired-texinfo-unclean-extensions
378 dired-latex-unclean-extensions
379 dired-bibtex-unclean-extensions
380 dired-tex-unclean-extensions)))
381
382 (defun dired-very-clean-tex ()
383 "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
384 See variables `dired-texinfo-unclean-extensions',
385 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
386 `dired-texinfo-unclean-extensions'."
387 (interactive)
388 (dired-flag-extension (append dired-texinfo-unclean-extensions
389 dired-latex-unclean-extensions
390 dired-bibtex-unclean-extensions
391 dired-tex-unclean-extensions
392 (list ".dvi"))))
393 \f
394 ;;; JUMP.
395
396 ;;;###autoload
397 (defun dired-jump (&optional other-window file-name)
398 "Jump to dired buffer corresponding to current buffer.
399 If in a file, dired the current directory and move to file's line.
400 If in Dired already, pop up a level and goto old directory's line.
401 In case the proper dired file line cannot be found, refresh the dired
402 buffer and try again.
403 When OTHER-WINDOW is non-nil, jump to dired buffer in other window.
404 Interactively with prefix argument, read FILE-NAME and
405 move to its line in dired."
406 (interactive
407 (list nil (and current-prefix-arg
408 (read-file-name "Jump to dired file: "))))
409 (let* ((file (or file-name buffer-file-name))
410 (dir (if file (file-name-directory file) default-directory)))
411 (if (and (eq major-mode 'dired-mode) (null file-name))
412 (progn
413 (setq dir (dired-current-directory))
414 (dired-up-directory other-window)
415 (unless (dired-goto-file dir)
416 ;; refresh and try again
417 (dired-insert-subdir (file-name-directory dir))
418 (dired-goto-file dir)))
419 (if other-window
420 (dired-other-window dir)
421 (dired dir))
422 (if file
423 (or (dired-goto-file file)
424 ;; refresh and try again
425 (progn
426 (dired-insert-subdir (file-name-directory file))
427 (dired-goto-file file))
428 ;; Toggle omitting, if it is on, and try again.
429 (when dired-omit-mode
430 (dired-omit-mode)
431 (dired-goto-file file)))))))
432
433 (defun dired-jump-other-window (&optional file-name)
434 "Like \\[dired-jump] (`dired-jump') but in other window."
435 (interactive
436 (list (and current-prefix-arg
437 (read-file-name "Jump to dired file: "))))
438 (dired-jump t file-name))
439 \f
440 ;;; OMITTING.
441
442 ;; Enhanced omitting of lines from directory listings.
443 ;; Marked files are never omitted.
444
445 ;; should probably get rid of this and always use 'no-dir.
446 ;; sk 28-Aug-1991 09:37
447 (defvar dired-omit-localp 'no-dir
448 "The LOCALP argument `dired-omit-expunge' passes to `dired-get-filename'.
449 If it is `no-dir', omitting is much faster, but you can only match
450 against the non-directory part of the file name. Set it to nil if you
451 need to match the entire file name.")
452
453 ;; \017=^O for Omit - other packages can chose other control characters.
454 (defvar dired-omit-marker-char ?\017
455 "Temporary marker used by Dired-Omit.
456 Should never be used as marker by the user or other packages.")
457
458 (defun dired-omit-startup ()
459 (or (assq 'dired-omit-mode minor-mode-alist)
460 (setq minor-mode-alist
461 (append '((dired-omit-mode
462 (:eval (if (eq major-mode 'dired-mode)
463 " Omit" ""))))
464 minor-mode-alist))))
465
466 (defun dired-mark-omitted ()
467 "Mark files matching `dired-omit-files' and `dired-omit-extensions'."
468 (interactive)
469 (let ((dired-omit-mode nil)) (revert-buffer)) ;; Show omitted files
470 (dired-mark-unmarked-files (dired-omit-regexp) nil nil dired-omit-localp))
471
472 (defcustom dired-omit-extensions
473 (append completion-ignored-extensions
474 dired-latex-unclean-extensions
475 dired-bibtex-unclean-extensions
476 dired-texinfo-unclean-extensions)
477 "If non-nil, a list of extensions \(strings\) to omit from Dired listings.
478 Defaults to elements of `completion-ignored-extensions',
479 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
480 `dired-texinfo-unclean-extensions'.
481
482 See interactive function `dired-omit-mode' \(\\[dired-omit-mode]\) and
483 variables `dired-omit-mode' and `dired-omit-files'."
484 :type '(repeat string)
485 :group 'dired-x)
486
487 (defun dired-omit-expunge (&optional regexp)
488 "Erases all unmarked files matching REGEXP.
489 Does nothing if global variable `dired-omit-mode' is nil, or if called
490 non-interactively and buffer is bigger than `dired-omit-size-limit'.
491 If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
492 filenames ending in `dired-omit-extensions'.
493 If REGEXP is the empty string, this function is a no-op.
494
495 This functions works by temporarily binding `dired-marker-char' to
496 `dired-omit-marker-char' and calling `dired-do-kill-lines'."
497 (interactive "sOmit files (regexp): ")
498 (if (and dired-omit-mode
499 (or (called-interactively-p 'interactive)
500 (not dired-omit-size-limit)
501 (< (buffer-size) dired-omit-size-limit)
502 (progn
503 (when dired-omit-verbose
504 (message "Not omitting: directory larger than %d characters."
505 dired-omit-size-limit))
506 (setq dired-omit-mode nil)
507 nil)))
508 (let ((omit-re (or regexp (dired-omit-regexp)))
509 (old-modified-p (buffer-modified-p))
510 count)
511 (or (string= omit-re "")
512 (let ((dired-marker-char dired-omit-marker-char))
513 (when dired-omit-verbose (message "Omitting..."))
514 (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp)
515 (progn
516 (setq count (dired-do-kill-lines
517 nil
518 (if dired-omit-verbose "Omitted %d line%s." "")))
519 (force-mode-line-update))
520 (when dired-omit-verbose (message "(Nothing to omit)")))))
521 ;; Try to preserve modified state of buffer. So `%*' doesn't appear
522 ;; in mode-line of omitted buffers.
523 (set-buffer-modified-p (and old-modified-p
524 (save-excursion
525 (goto-char (point-min))
526 (re-search-forward dired-re-mark nil t))))
527 count)))
528
529 (defun dired-omit-regexp ()
530 (concat (if dired-omit-files (concat "\\(" dired-omit-files "\\)") "")
531 (if (and dired-omit-files dired-omit-extensions) "\\|" "")
532 (if dired-omit-extensions
533 (concat ".";; a non-extension part should exist
534 "\\("
535 (mapconcat 'regexp-quote dired-omit-extensions "\\|")
536 "\\)$")
537 "")))
538
539 ;; Returns t if any work was done, nil otherwise.
540 (defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp)
541 "Mark unmarked files matching REGEXP, displaying MSG.
542 REGEXP is matched against the entire file name.
543 Does not re-mark files which already have a mark.
544 With prefix argument, unflag all those files.
545 Optional fourth argument LOCALP is as in `dired-get-filename'."
546 (interactive "P")
547 (let ((dired-marker-char (if unflag-p ?\s dired-marker-char)))
548 (dired-mark-if
549 (and
550 ;; not already marked
551 (looking-at " ")
552 ;; uninteresting
553 (let ((fn (dired-get-filename localp t)))
554 (and fn (string-match regexp fn))))
555 msg)))
556
557 \f
558 ;;; VIRTUAL DIRED MODE.
559
560 ;; For browsing `ls -lR' listings in a dired-like fashion.
561
562 (defalias 'virtual-dired 'dired-virtual)
563 (defun dired-virtual (dirname &optional switches)
564 "Put this buffer into Virtual Dired mode.
565
566 In Virtual Dired mode, all commands that do not actually consult the
567 filesystem will work.
568
569 This is useful if you want to peruse and move around in an ls -lR
570 output file, for example one you got from an ftp server. With
571 ange-ftp, you can even dired a directory containing an ls-lR file,
572 visit that file and turn on virtual dired mode. But don't try to save
573 this file, as dired-virtual indents the listing and thus changes the
574 buffer.
575
576 If you have save a Dired buffer in a file you can use \\[dired-virtual] to
577 resume it in a later session.
578
579 Type \\<dired-mode-map>\\[revert-buffer] \
580 in the Virtual Dired buffer and answer `y' to convert
581 the virtual to a real dired buffer again. You don't have to do this, though:
582 you can relist single subdirs using \\[dired-do-redisplay]."
583
584 ;; DIRNAME is the top level directory of the buffer. It will become
585 ;; its `default-directory'. If nil, the old value of
586 ;; default-directory is used.
587
588 ;; Optional SWITCHES are the ls switches to use.
589
590 ;; Shell wildcards will be used if there already is a `wildcard'
591 ;; line in the buffer (thus it is a saved Dired buffer), but there
592 ;; is no other way to get wildcards. Insert a `wildcard' line by
593 ;; hand if you want them.
594
595 (interactive
596 (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
597 (goto-char (point-min))
598 (or (looking-at " ")
599 ;; if not already indented, do it now:
600 (indent-region (point-min) (point-max) 2))
601 (or dirname (setq dirname default-directory))
602 (setq dirname (expand-file-name (file-name-as-directory dirname)))
603 (setq default-directory dirname) ; contains no wildcards
604 (let ((wildcard (save-excursion
605 (goto-char (point-min))
606 (forward-line 1)
607 (and (looking-at "^ wildcard ")
608 (buffer-substring (match-end 0)
609 (line-end-position))))))
610 (if wildcard
611 (setq dirname (expand-file-name wildcard default-directory))))
612 ;; If raw ls listing (not a saved old dired buffer), give it a
613 ;; decent subdir headerline:
614 (goto-char (point-min))
615 (or (looking-at dired-subdir-regexp)
616 (insert " "
617 (directory-file-name (file-name-directory default-directory))
618 ":\n"))
619 (dired-mode dirname (or switches dired-listing-switches))
620 (setq mode-name "Virtual Dired"
621 revert-buffer-function 'dired-virtual-revert)
622 (set (make-local-variable 'dired-subdir-alist) nil)
623 (dired-build-subdir-alist)
624 (goto-char (point-min))
625 (dired-initial-position dirname))
626
627 (defun dired-virtual-guess-dir ()
628 "Guess and return appropriate working directory of this buffer.
629 The buffer is assumed to be in Dired or ls -lR format. The guess is
630 based upon buffer contents. If nothing could be guessed, returns
631 nil."
632
633 (let ((regexp "^\\( \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
634 (subexpr 2))
635 (goto-char (point-min))
636 (cond ((looking-at regexp)
637 ;; If a saved dired buffer, look to which dir and
638 ;; perhaps wildcard it belongs:
639 (let ((dir (buffer-substring (match-beginning subexpr)
640 (match-end subexpr))))
641 (file-name-as-directory dir)))
642 ;; Else no match for headerline found. It's a raw ls listing.
643 ;; In raw ls listings the directory does not have a headerline
644 ;; try parent of first subdir, if any
645 ((re-search-forward regexp nil t)
646 (file-name-directory
647 (directory-file-name
648 (file-name-as-directory
649 (buffer-substring (match-beginning subexpr)
650 (match-end subexpr))))))
651 (t ; if all else fails
652 nil))))
653
654
655 (defun dired-virtual-revert (&optional arg noconfirm)
656 (if (not
657 (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
658 (error "Cannot revert a Virtual Dired buffer")
659 (setq mode-name "Dired"
660 revert-buffer-function 'dired-revert)
661 (revert-buffer)))
662
663 ;; A zero-arg version of dired-virtual.
664 (defun dired-virtual-mode ()
665 "Put current buffer into Virtual Dired mode (see `dired-virtual').
666 Useful on `magic-mode-alist' with the regexp
667
668 \"^ \\\\(/[^ /]+\\\\)+/?:$\"
669
670 to put saved dired buffers automatically into Virtual Dired mode.
671
672 Also useful for `auto-mode-alist' like this:
673
674 (add-to-list 'auto-mode-alist
675 '(\"[^/]\\\\.dired\\\\'\" . dired-virtual-mode))"
676 (interactive)
677 (dired-virtual (dired-virtual-guess-dir)))
678
679 \f
680 ;;; SMART SHELL.
681
682 ;; An Emacs buffer can have but one working directory, stored in the
683 ;; buffer-local variable `default-directory'. A Dired buffer may have
684 ;; several subdirectories inserted, but still has but one working directory:
685 ;; that of the top level Dired directory in that buffer. For some commands
686 ;; it is appropriate that they use the current Dired directory instead of
687 ;; `default-directory', e.g., `find-file' and `compile'. This is a general
688 ;; mechanism is provided for special handling of the working directory in
689 ;; special major modes.
690
691 (define-obsolete-variable-alias 'default-directory-alist
692 'dired-default-directory-alist "24.1")
693
694 ;; It's easier to add to this alist than redefine function
695 ;; default-directory while keeping the old information.
696 (defconst dired-default-directory-alist
697 '((dired-mode . (if (fboundp 'dired-current-directory)
698 (dired-current-directory)
699 default-directory)))
700 "Alist of major modes and their opinion on `default-directory'.
701 This is given as a Lisp expression to evaluate. A resulting value of
702 nil is ignored in favor of `default-directory'.")
703
704 (defun dired-default-directory ()
705 "Usage like variable `default-directory'.
706 Knows about the special cases in variable `dired-default-directory-alist'."
707 (or (eval (cdr (assq major-mode dired-default-directory-alist)))
708 default-directory))
709
710 (defun dired-smart-shell-command (command &optional output-buffer error-buffer)
711 "Like function `shell-command', but in the current Virtual Dired directory."
712 (interactive
713 (list
714 (read-shell-command "Shell command: " nil nil
715 (cond
716 (buffer-file-name (file-relative-name buffer-file-name))
717 ((eq major-mode 'dired-mode) (dired-get-filename t t))))
718 current-prefix-arg
719 shell-command-default-error-buffer))
720 (let ((default-directory (dired-default-directory)))
721 (shell-command command output-buffer error-buffer)))
722
723 \f
724 ;;; LOCAL VARIABLES FOR DIRED BUFFERS.
725
726 ;; Brief Description:
727 ;;;
728 ;; * `dired-extra-startup' is part of the `dired-mode-hook'.
729 ;;;
730 ;; * `dired-extra-startup' calls `dired-hack-local-variables'
731 ;;;
732 ;; * `dired-hack-local-variables' checks the value of
733 ;;; `dired-local-variables-file'
734 ;;;
735 ;; * Check if `dired-local-variables-file' is a non-nil string and is a
736 ;;; filename found in the directory of the Dired Buffer being created.
737 ;;;
738 ;; * If `dired-local-variables-file' satisfies the above, then temporarily
739 ;;; include it in the Dired Buffer at the bottom.
740 ;;;
741 ;; * Set `enable-local-variables' temporarily to the user variable
742 ;;; `dired-enable-local-variables' and run `hack-local-variables' on the
743 ;;; Dired Buffer.
744
745 ;; FIXME do standard dir-locals obsolete this?
746 (defcustom dired-local-variables-file (convert-standard-filename ".dired")
747 "Filename, as string, containing local dired buffer variables to be hacked.
748 If this file found in current directory, then it will be inserted into dired
749 buffer and `hack-local-variables' will be run. See Info node
750 `(emacs)File Variables' for more information on local variables.
751 See also `dired-enable-local-variables'."
752 :type 'file
753 :group 'dired)
754
755 (defun dired-hack-local-variables ()
756 "Evaluate local variables in `dired-local-variables-file' for dired buffer."
757 (and (stringp dired-local-variables-file)
758 (file-exists-p dired-local-variables-file)
759 (let ((opoint (point-max))
760 (inhibit-read-only t)
761 ;; In case user has `enable-local-variables' set to nil we
762 ;; override it locally with dired's variable.
763 (enable-local-variables dired-enable-local-variables))
764 ;; Insert 'em.
765 (save-excursion
766 (goto-char opoint)
767 (insert "\^L\n")
768 (insert-file-contents dired-local-variables-file))
769 ;; Hack 'em.
770 (let ((buffer-file-name dired-local-variables-file))
771 (hack-local-variables))
772 ;; Make sure that the modeline shows the proper information.
773 (dired-sort-set-modeline)
774 ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
775 (delete-region opoint (point-max)))))
776
777 (defun dired-omit-here-always ()
778 "Create `dired-local-variables-file' for omitting and reverts directory.
779 Sets `dired-omit-mode' to t in a local variables file that is readable by
780 dired."
781 (interactive)
782 (if (file-exists-p dired-local-variables-file)
783 (message "File `./%s' already exists." dired-local-variables-file)
784 ;; Create `dired-local-variables-file'.
785 (with-current-buffer (get-buffer-create " *dot-dired*")
786 (erase-buffer)
787 (insert "Local Variables:\ndired-omit-mode: t\nEnd:\n")
788 (write-file dired-local-variables-file)
789 (kill-buffer))
790 ;; Run extra-hooks and revert directory.
791 (dired-extra-startup)
792 (dired-revert)))
793
794 \f
795 ;;; GUESS SHELL COMMAND.
796
797 ;; Brief Description:
798 ;;;
799 ;; * `dired-do-shell-command' is bound to `!' by dired.el.
800 ;;;
801 ;; * `dired-guess-shell-command' provides smarter defaults for
802 ;;; dired-aux.el's `dired-read-shell-command'.
803 ;;;
804 ;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
805 ;;; marked files.
806 ;;;
807 ;; * Parse `dired-guess-shell-alist-user' and
808 ;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
809 ;;; that matches the first file in the file list.
810 ;;;
811 ;; * If the REGEXP matches all the entries of the file list then evaluate
812 ;;; COMMAND, which is either a string or a Lisp expression returning a
813 ;;; string. COMMAND may be a list of commands.
814 ;;;
815 ;; * Return this command to `dired-guess-shell-command' which prompts user
816 ;;; with it. The list of commands is put into the list of default values.
817 ;;; If a command is used successfully then it is stored permanently in
818 ;;; `dired-shell-command-history'.
819
820 ;; Guess what shell command to apply to a file.
821 (defvar dired-shell-command-history nil
822 "History list for commands that read dired-shell commands.")
823
824 ;; Default list of shell commands.
825
826 ;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
827 ;; install GNU zip's version of zcat.
828
829 (declare-function Man-support-local-filenames "man" ())
830
831 (defvar dired-guess-shell-alist-default
832 (list
833 (list "\\.tar$"
834 '(if dired-guess-shell-gnutar
835 (concat dired-guess-shell-gnutar " xvf")
836 "tar xvf")
837 ;; Extract files into a separate subdirectory
838 '(if dired-guess-shell-gnutar
839 (concat "mkdir " (file-name-sans-extension file)
840 "; " dired-guess-shell-gnutar " -C "
841 (file-name-sans-extension file) " -xvf")
842 (concat "mkdir " (file-name-sans-extension file)
843 "; tar -C " (file-name-sans-extension file) " -xvf"))
844 ;; List archive contents.
845 '(if dired-guess-shell-gnutar
846 (concat dired-guess-shell-gnutar " tvf")
847 "tar tvf"))
848
849 ;; REGEXPS for compressed archives must come before the .Z rule to
850 ;; be recognized:
851 (list "\\.tar\\.Z$"
852 ;; Untar it.
853 '(if dired-guess-shell-gnutar
854 (concat dired-guess-shell-gnutar " zxvf")
855 (concat "zcat * | tar xvf -"))
856 ;; Optional conversion to gzip format.
857 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
858 " " dired-guess-shell-znew-switches))
859
860 ;; gzip'ed archives
861 (list "\\.t\\(ar\\.\\)?gz$"
862 '(if dired-guess-shell-gnutar
863 (concat dired-guess-shell-gnutar " zxvf")
864 (concat "gunzip -qc * | tar xvf -"))
865 ;; Extract files into a separate subdirectory
866 '(if dired-guess-shell-gnutar
867 (concat "mkdir " (file-name-sans-extension file)
868 "; " dired-guess-shell-gnutar " -C "
869 (file-name-sans-extension file) " -zxvf")
870 (concat "mkdir " (file-name-sans-extension file)
871 "; gunzip -qc * | tar -C "
872 (file-name-sans-extension file) " -xvf -"))
873 ;; Optional decompression.
874 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" ""))
875 ;; List archive contents.
876 '(if dired-guess-shell-gnutar
877 (concat dired-guess-shell-gnutar " ztvf")
878 (concat "gunzip -qc * | tar tvf -")))
879
880 ;; bzip2'ed archives
881 (list "\\.t\\(ar\\.bz2\\|bz\\)$"
882 "bunzip2 -c * | tar xvf -"
883 ;; Extract files into a separate subdirectory
884 '(concat "mkdir " (file-name-sans-extension file)
885 "; bunzip2 -c * | tar -C "
886 (file-name-sans-extension file) " -xvf -")
887 ;; Optional decompression.
888 "bunzip2")
889
890 ;; xz'ed archives
891 (list "\\.t\\(ar\\.\\)?xz$"
892 "unxz -c * | tar xvf -"
893 ;; Extract files into a separate subdirectory
894 '(concat "mkdir " (file-name-sans-extension file)
895 "; unxz -c * | tar -C "
896 (file-name-sans-extension file) " -xvf -")
897 ;; Optional decompression.
898 "unxz")
899
900 '("\\.shar\\.Z$" "zcat * | unshar")
901 '("\\.shar\\.g?z$" "gunzip -qc * | unshar")
902
903 '("\\.e?ps$" "ghostview" "xloadimage" "lpr")
904 (list "\\.e?ps\\.g?z$" "gunzip -qc * | ghostview -"
905 ;; Optional decompression.
906 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
907 (list "\\.e?ps\\.Z$" "zcat * | ghostview -"
908 ;; Optional conversion to gzip format.
909 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
910 " " dired-guess-shell-znew-switches))
911
912 '("\\.patch$" "cat * | patch")
913 (list "\\.patch\\.g?z$" "gunzip -qc * | patch"
914 ;; Optional decompression.
915 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
916 (list "\\.patch\\.Z$" "zcat * | patch"
917 ;; Optional conversion to gzip format.
918 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
919 " " dired-guess-shell-znew-switches))
920
921 ;; The following four extensions are useful with dired-man ("N" key)
922 (list "\\.\\(?:[0-9]\\|man\\)$" '(progn (require 'man)
923 (if (Man-support-local-filenames)
924 "man -l"
925 "cat * | tbl | nroff -man -h")))
926 (list "\\.\\(?:[0-9]\\|man\\)\\.g?z$" '(progn (require 'man)
927 (if (Man-support-local-filenames)
928 "man -l"
929 "gunzip -qc * | tbl | nroff -man -h"))
930 ;; Optional decompression.
931 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
932 (list "\\.[0-9]\\.Z$" '(progn (require 'man)
933 (if (Man-support-local-filenames)
934 "man -l"
935 "zcat * | tbl | nroff -man -h"))
936 ;; Optional conversion to gzip format.
937 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
938 " " dired-guess-shell-znew-switches))
939 '("\\.pod$" "perldoc" "pod2man * | nroff -man")
940
941 '("\\.dvi$" "xdvi" "dvips") ; preview and printing
942 '("\\.au$" "play") ; play Sun audiofiles
943 '("\\.mpe?g$\\|\\.avi$" "xine -p")
944 '("\\.ogg$" "ogg123")
945 '("\\.mp3$" "mpg123")
946 '("\\.wav$" "play")
947 '("\\.uu$" "uudecode") ; for uudecoded files
948 '("\\.hqx$" "mcvert")
949 '("\\.sh$" "sh") ; execute shell scripts
950 '("\\.xbm$" "bitmap") ; view X11 bitmaps
951 '("\\.gp$" "gnuplot")
952 '("\\.p[bgpn]m$" "xloadimage")
953 '("\\.gif$" "xloadimage") ; view gif pictures
954 '("\\.tif$" "xloadimage")
955 '("\\.png$" "display") ; xloadimage 4.1 doesn't grok PNG
956 '("\\.jpe?g$" "xloadimage")
957 '("\\.fig$" "xfig") ; edit fig pictures
958 '("\\.out$" "xgraph") ; for plotting purposes.
959 '("\\.tex$" "latex" "tex")
960 '("\\.texi\\(nfo\\)?$" "makeinfo" "texi2dvi")
961 '("\\.pdf$" "xpdf")
962 '("\\.doc$" "antiword" "strings")
963 '("\\.rpm$" "rpm -qilp" "rpm -ivh")
964 '("\\.dia$" "dia")
965 '("\\.mgp$" "mgp")
966
967 ;; Some other popular archivers.
968 (list "\\.zip$" "unzip" "unzip -l"
969 ;; Extract files into a separate subdirectory
970 '(concat "unzip" (if dired-guess-shell-gzip-quiet " -q")
971 " -d " (file-name-sans-extension file)))
972 '("\\.zoo$" "zoo x//")
973 '("\\.lzh$" "lharc x")
974 '("\\.arc$" "arc x")
975 '("\\.shar$" "unshar")
976
977 ;; Compression.
978 (list "\\.g?z$" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
979 (list "\\.dz$" "dictunzip")
980 (list "\\.bz2$" "bunzip2")
981 (list "\\.xz$" "unxz")
982 (list "\\.Z$" "uncompress"
983 ;; Optional conversion to gzip format.
984 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
985 " " dired-guess-shell-znew-switches))
986
987 '("\\.sign?$" "gpg --verify"))
988
989 "Default alist used for shell command guessing.
990 See `dired-guess-shell-alist-user'.")
991
992 (defcustom dired-guess-shell-alist-user nil
993 "User-defined alist of rules for suggested commands.
994 These rules take precedence over the predefined rules in the variable
995 `dired-guess-shell-alist-default' (to which they are prepended).
996
997 Each element of this list looks like
998
999 \(REGEXP COMMAND...\)
1000
1001 where each COMMAND can either be a string or a Lisp expression that evaluates
1002 to a string. If several COMMANDs are given, the first one will be the default
1003 and the rest will be added temporarily to the history and can be retrieved
1004 with \\[previous-history-element] (M-p) .
1005
1006 The variable `dired-guess-shell-case-fold-search' controls whether
1007 REGEXP is matched case-sensitively.
1008
1009 You can set this variable in your ~/.emacs. For example, to add rules for
1010 `.foo' and `.bar' files, write
1011
1012 \(setq dired-guess-shell-alist-user
1013 (list (list \"\\\\.foo\\\\'\" \"FOO-COMMAND\");; fixed rule
1014 ;; possibly more rules ...
1015 (list \"\\\\.bar\\\\'\";; rule with condition test
1016 '(if condition
1017 \"BAR-COMMAND-1\"
1018 \"BAR-COMMAND-2\")))\)"
1019 :group 'dired-x
1020 :type '(alist :key-type regexp :value-type (repeat sexp)))
1021
1022 (defcustom dired-guess-shell-case-fold-search t
1023 "If non-nil, `dired-guess-shell-alist-default' and
1024 `dired-guess-shell-alist-user' are matched case-insensitively."
1025 :group 'dired-x
1026 :type 'boolean)
1027
1028 (defun dired-guess-default (files)
1029 "Guess a shell commands for FILES. Return command or list of commands.
1030 See `dired-guess-shell-alist-user'."
1031
1032 (let* ((case-fold-search dired-guess-shell-case-fold-search)
1033 ;; Prepend the user's alist to the default alist.
1034 (alist (append dired-guess-shell-alist-user
1035 dired-guess-shell-alist-default))
1036 (file (car files))
1037 (flist (cdr files))
1038 elt regexp cmds)
1039
1040 ;; Find the first match in the alist for first file in FILES.
1041 (while alist
1042 (setq elt (car alist)
1043 regexp (car elt)
1044 alist (cdr alist))
1045 (if (string-match regexp file)
1046 (setq cmds (cdr elt)
1047 alist nil)))
1048
1049 ;; If more than one file, see if all of FILES match regular expression.
1050 (while (and flist
1051 (string-match regexp (car flist)))
1052 (setq flist (cdr flist)))
1053
1054 ;; If flist is still non-nil, then do not guess since this means that not
1055 ;; all the files in FILES were matched by the regexp.
1056 (setq cmds (and (not flist) cmds))
1057
1058 ;; Return commands or nil if flist is still non-nil.
1059 ;; Evaluate the commands in order that any logical testing will be done.
1060 (if (cdr cmds)
1061 (mapcar #'eval cmds)
1062 (eval (car cmds))))) ; single command
1063
1064 (defun dired-guess-shell-command (prompt files)
1065 "Ask user with PROMPT for a shell command, guessing a default from FILES."
1066 (let ((default (dired-guess-default files))
1067 default-list val)
1068 (if (null default)
1069 ;; Nothing to guess
1070 (read-shell-command prompt nil 'dired-shell-command-history)
1071 (if (listp default)
1072 ;; More than one guess
1073 (setq default-list default
1074 default (car default)
1075 prompt (concat
1076 prompt
1077 (format "{%d guesses} " (length default-list))))
1078 ;; Just one guess
1079 (setq default-list (list default)))
1080 ;; Put the first guess in the prompt but not in the initial value.
1081 (setq prompt (concat prompt (format "[%s] " default)))
1082 ;; All guesses can be retrieved with M-n
1083 (setq val (read-shell-command prompt nil
1084 'dired-shell-command-history
1085 default-list))
1086 ;; If we got a return, then return default.
1087 (if (equal val "") default val))))
1088
1089 \f
1090 ;;; RELATIVE SYMBOLIC LINKS.
1091
1092 (declare-function make-symbolic-link "fileio.c")
1093
1094 (defvar dired-keep-marker-relsymlink ?S
1095 "See variable `dired-keep-marker-move'.")
1096
1097 (defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
1098 "Make a symbolic link (pointing to FILE1) in FILE2.
1099 The link is relative (if possible), for example
1100
1101 \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
1102
1103 results in
1104
1105 \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
1106 (interactive "FRelSymLink: \nFRelSymLink %s: \np")
1107 (let (name1 name2 len1 len2 (index 0) sub)
1108 (setq file1 (expand-file-name file1)
1109 file2 (expand-file-name file2)
1110 len1 (length file1)
1111 len2 (length file2))
1112 ;; Find common initial file name components:
1113 (let (next)
1114 (while (and (setq next (string-match "/" file1 index))
1115 (< (setq next (1+ next)) (min len1 len2))
1116 ;; For the comparison, both substrings must end in
1117 ;; `/', so NEXT is *one plus* the result of the
1118 ;; string-match.
1119 ;; E.g., consider the case of linking "/tmp/a/abc"
1120 ;; to "/tmp/abc" erroneously giving "/tmp/a" instead
1121 ;; of "/tmp/" as common initial component
1122 (string-equal (substring file1 0 next)
1123 (substring file2 0 next)))
1124 (setq index next))
1125 (setq name2 file2
1126 sub (substring file1 0 index)
1127 name1 (substring file1 index)))
1128 (if (string-equal sub "/")
1129 ;; No common initial file name found
1130 (setq name1 file1)
1131 ;; Else they have a common parent directory
1132 (let ((tem (substring file2 index))
1133 (start 0)
1134 (count 0))
1135 ;; Count number of slashes we must compensate for ...
1136 (while (setq start (string-match "/" tem start))
1137 (setq count (1+ count)
1138 start (1+ start)))
1139 ;; ... and prepend a "../" for each slash found:
1140 (dotimes (n count)
1141 (setq name1 (concat "../" name1)))))
1142 (make-symbolic-link
1143 (directory-file-name name1) ; must not link to foo/
1144 ; (trailing slash!)
1145 name2 ok-if-already-exists)))
1146
1147 (autoload 'dired-do-create-files "dired-aux")
1148
1149 ;;;###autoload
1150 (defun dired-do-relsymlink (&optional arg)
1151 "Relative symlink all marked (or next ARG) files into a directory.
1152 Otherwise make a relative symbolic link to the current file.
1153 This creates relative symbolic links like
1154
1155 foo -> ../bar/foo
1156
1157 not absolute ones like
1158
1159 foo -> /ugly/file/name/that/may/change/any/day/bar/foo
1160
1161 For absolute symlinks, use \\[dired-do-symlink]."
1162 (interactive "P")
1163 (dired-do-create-files 'relsymlink #'dired-make-relative-symlink
1164 "RelSymLink" arg dired-keep-marker-relsymlink))
1165
1166 (autoload 'dired-mark-read-regexp "dired-aux")
1167 (autoload 'dired-do-create-files-regexp "dired-aux")
1168
1169 (defun dired-do-relsymlink-regexp (regexp newname &optional arg whole-name)
1170 "RelSymlink all marked files containing REGEXP to NEWNAME.
1171 See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
1172 for more info."
1173 (interactive (dired-mark-read-regexp "RelSymLink"))
1174 (dired-do-create-files-regexp
1175 #'dired-make-relative-symlink
1176 "RelSymLink" arg regexp newname whole-name dired-keep-marker-relsymlink))
1177
1178 \f
1179 ;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
1180
1181 ;; Brief Description:
1182 ;;;
1183 ;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
1184 ;;;
1185 ;; * Use `dired-get-marked-files' to collect the marked files in the current
1186 ;;; Dired Buffer into a list of filenames `FILE-LIST'.
1187 ;;;
1188 ;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
1189 ;;; `dired-do-find-marked-files''s prefix argument NOSELECT.
1190 ;;;
1191 ;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
1192 ;;; list each time.
1193 ;;;
1194 ;; * If NOSELECT is non-nil then just run `find-file-noselect' on each
1195 ;;; element of FILE-LIST.
1196 ;;;
1197 ;; * If NOSELECT is nil then calculate the `size' of the window for each file
1198 ;;; by dividing the `window-height' by length of FILE-LIST. Thus, `size' is
1199 ;;; cognizant of the window-configuration.
1200 ;;;
1201 ;; * If `size' is too small abort, otherwise run `find-file' on each element
1202 ;;; of FILE-LIST giving each a window of height `size'.
1203
1204 (defun dired-do-find-marked-files (&optional noselect)
1205 "Find all marked files displaying all of them simultaneously.
1206 With optional NOSELECT just find files but do not select them.
1207
1208 The current window is split across all files marked, as evenly as possible.
1209 Remaining lines go to bottom-most window. The number of files that can be
1210 displayed this way is restricted by the height of the current window and
1211 `window-min-height'.
1212
1213 To keep dired buffer displayed, type \\[split-window-vertically] first.
1214 To display just marked files, type \\[delete-other-windows] first."
1215 (interactive "P")
1216 (dired-simultaneous-find-file (dired-get-marked-files) noselect))
1217
1218 (defun dired-simultaneous-find-file (file-list noselect)
1219 "Visit all files in FILE-LIST and display them simultaneously.
1220 The current window is split across all files in FILE-LIST, as evenly as
1221 possible. Remaining lines go to the bottom-most window. The number of
1222 files that can be displayed this way is restricted by the height of the
1223 current window and the variable `window-min-height'. With non-nil
1224 NOSELECT the files are merely found but not selected."
1225 ;; We don't make this function interactive because it is usually too clumsy
1226 ;; to specify FILE-LIST interactively unless via dired.
1227 (let (size)
1228 (if noselect
1229 ;; Do not select the buffer.
1230 (find-file-noselect (car file-list))
1231 ;; We will have to select the buffer. Calculate and check window size.
1232 (setq size (/ (window-height) (length file-list)))
1233 (or (<= window-min-height size)
1234 (error "Too many files to visit simultaneously. Try C-u prefix"))
1235 (find-file (car file-list)))
1236 ;; Decrement.
1237 (dolist (file (cdr file-list))
1238 (if noselect
1239 ;; Do not select the buffer.
1240 (find-file-noselect file)
1241 ;; Vertically split off a window of desired size. Upper window will
1242 ;; have SIZE lines. Select lower (larger) window. We split it again.
1243 (select-window (split-window nil size))
1244 (find-file file)))))
1245
1246 \f
1247 ;;; MISCELLANEOUS COMMANDS.
1248
1249 ;; Run man on files.
1250
1251 (declare-function Man-getpage-in-background "man" (topic))
1252
1253 (defun dired-man ()
1254 "Run `man' on this file."
1255 ;; Used also to say: "Display old buffer if buffer name matches filename."
1256 ;; but I have no idea what that means.
1257 (interactive)
1258 (require 'man)
1259 (let* ((file (dired-get-filename))
1260 (manual-program (replace-regexp-in-string "\\*" "%s"
1261 (dired-guess-shell-command
1262 "Man command: " (list file)))))
1263 (Man-getpage-in-background file)))
1264
1265 ;; Run Info on files.
1266
1267 (defun dired-info ()
1268 "Run `info' on this file."
1269 (interactive)
1270 (info (dired-get-filename)))
1271
1272 ;; Run mail on mail folders.
1273
1274 (declare-function vm-visit-folder "ext:vm" (folder &optional read-only))
1275 (defvar vm-folder-directory)
1276
1277 (defun dired-vm (&optional read-only)
1278 "Run VM on this file.
1279 With optional prefix argument, visits the folder read-only.
1280 Otherwise obeys the value of `dired-vm-read-only-folders'."
1281 (interactive "P")
1282 (let ((dir (dired-current-directory))
1283 (fil (dired-get-filename)))
1284 (vm-visit-folder fil (or read-only
1285 (eq t dired-vm-read-only-folders)
1286 (and dired-vm-read-only-folders
1287 (not (file-writable-p fil)))))
1288 ;; So that pressing `v' inside VM does prompt within current directory:
1289 (set (make-local-variable 'vm-folder-directory) dir)))
1290
1291 (defun dired-rmail ()
1292 "Run RMAIL on this file."
1293 (interactive)
1294 (rmail (dired-get-filename)))
1295
1296 (defun dired-do-run-mail ()
1297 "If `dired-bind-vm' is non-nil, call `dired-vm', else call `dired-rmail'."
1298 (interactive)
1299 (if dired-bind-vm
1300 ;; Read mail folder using vm.
1301 (dired-vm)
1302 ;; Read mail folder using rmail.
1303 (dired-rmail)))
1304
1305 \f
1306 ;;; MISCELLANEOUS INTERNAL FUNCTIONS.
1307
1308 ;; This should be a builtin
1309 (defun dired-buffer-more-recently-used-p (buffer1 buffer2)
1310 "Return t if BUFFER1 is more recently used than BUFFER2.
1311 Considers buffers closer to the car of `buffer-list' to be more recent."
1312 (and (not (equal buffer1 buffer2))
1313 (memq buffer1 (buffer-list))
1314 (not (memq buffer1 (memq buffer2 (buffer-list))))))
1315
1316 ;; Same thing as `dired-buffers-for-dir' of dired.el? - lrd 11/23/93
1317 ;; (defun dired-buffers-for-dir-exact (dir)
1318 ;; ;; Return a list of buffers that dired DIR (a directory or wildcard)
1319 ;; ;; at top level, or as subdirectory.
1320 ;; ;; Top level matches must match the wildcard part too, if any.
1321 ;; ;; The list is in reverse order of buffer creation, most recent last.
1322 ;; ;; As a side effect, killed dired buffers for DIR are removed from
1323 ;; ;; dired-buffers.
1324 ;; (let ((alist dired-buffers) result elt)
1325 ;; (while alist
1326 ;; (setq elt (car alist)
1327 ;; alist (cdr alist))
1328 ;; (let ((buf (cdr elt)))
1329 ;; (if (buffer-name buf)
1330 ;; ;; Top level must match exactly against dired-directory in
1331 ;; ;; case one of them is a wildcard.
1332 ;; (if (or (equal dir (with-current-buffer buf dired-directory))
1333 ;; (assoc dir (with-current-buffer buf dired-subdir-alist)))
1334 ;; (setq result (cons buf result)))
1335 ;; ;; else buffer is killed - clean up:
1336 ;; (setq dired-buffers (delq elt dired-buffers)))))
1337 ;; result))
1338
1339 \f
1340 ;; Does anyone use this? - lrd 6/29/93.
1341 ;; Apparently people do use it. - lrd 12/22/97.
1342 (defun dired-mark-sexp (predicate &optional unflag-p)
1343 "Mark files for which PREDICATE returns non-nil.
1344 With a prefix arg, unflag those files instead.
1345
1346 PREDICATE is a lisp expression that can refer to the following symbols:
1347
1348 inode [integer] the inode of the file (only for ls -i output)
1349 s [integer] the size of the file for ls -s output
1350 (usually in blocks or, with -k, in KByte)
1351 mode [string] file permission bits, e.g. \"-rw-r--r--\"
1352 nlink [integer] number of links to file
1353 uid [string] owner
1354 gid [string] group (If the gid is not displayed by ls,
1355 this will still be set (to the same as uid))
1356 size [integer] file size in bytes
1357 time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
1358 name [string] the name of the file
1359 sym [string] if file is a symbolic link, the linked-to name, else \"\"
1360
1361 For example, use
1362
1363 (equal 0 size)
1364
1365 to mark all zero length files."
1366 ;; Using sym="" instead of nil avoids the trap of
1367 ;; (string-match "foo" sym) into which a user would soon fall.
1368 ;; Give `equal' instead of `=' in the example, as this works on
1369 ;; integers and strings.
1370 (interactive "xMark if (lisp expr): \nP")
1371 (message "%s" predicate)
1372 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))
1373 inode s mode nlink uid gid size time name sym)
1374 (dired-mark-if
1375 (save-excursion
1376 (and
1377 ;; Sets vars
1378 ;; inode s mode nlink uid gid size time name sym
1379
1380 ;; according to current file line. Returns t for success, nil if
1381 ;; there is no file line. Upon success, all variables are set, either
1382 ;; to nil or the appropriate value, so they need not be initialized.
1383 ;; Moves point within the current line.
1384 (dired-move-to-filename)
1385 (let (pos
1386 (mode-len 10) ; length of mode string
1387 ;; like in dired.el, but with subexpressions \1=inode, \2=s:
1388 (dired-re-inode-size "\\s *\\([0-9]*\\)\\s *\\([0-9]*\\) ?"))
1389 (beginning-of-line)
1390 (forward-char 2)
1391 (if (looking-at dired-re-inode-size)
1392 (progn
1393 (goto-char (match-end 0))
1394 (setq inode (string-to-number
1395 (buffer-substring (match-beginning 1)
1396 (match-end 1)))
1397 s (string-to-number
1398 (buffer-substring (match-beginning 2)
1399 (match-end 2)))))
1400 (setq inode nil
1401 s nil))
1402 (setq mode (buffer-substring (point) (+ mode-len (point))))
1403 (forward-char mode-len)
1404 (setq nlink (read (current-buffer)))
1405 ;; Karsten Wenger <kw@cis.uni-muenchen.de> fixed uid.
1406 (setq uid (buffer-substring (1+ (point))
1407 (progn (forward-word 1) (point))))
1408 (re-search-forward directory-listing-before-filename-regexp)
1409 (goto-char (match-beginning 1))
1410 (forward-char -1)
1411 (setq size (string-to-number
1412 (buffer-substring (save-excursion
1413 (backward-word 1)
1414 (setq pos (point)))
1415 (point))))
1416 (goto-char pos)
1417 (backward-word 1)
1418 ;; if no gid is displayed, gid will be set to uid
1419 ;; but user will then not reference it anyway in PREDICATE.
1420 (setq gid (buffer-substring (save-excursion
1421 (forward-word 1) (point))
1422 (point))
1423 time (buffer-substring (match-beginning 1)
1424 (1- (dired-move-to-filename)))
1425 name (buffer-substring (point)
1426 (or
1427 (dired-move-to-end-of-filename t)
1428 (point)))
1429 sym (if (looking-at " -> ")
1430 (buffer-substring (progn (forward-char 4) (point))
1431 (line-end-position))
1432 ""))
1433 t)
1434 (eval predicate)))
1435 (format "'%s file" predicate))))
1436
1437 \f
1438 ;;; FIND FILE AT POINT.
1439
1440 (defcustom dired-x-hands-off-my-keys t
1441 "Non-nil means don't remap `find-file' to `dired-x-find-file'.
1442 Similarly for `find-file-other-window' and `dired-x-find-file-other-window'.
1443 If you change this variable without using \\[customize] after `dired-x.el'
1444 is loaded then call \\[dired-x-bind-find-file]."
1445 :type 'boolean
1446 :initialize 'custom-initialize-default
1447 :set (lambda (sym val)
1448 (set sym val)
1449 (dired-x-bind-find-file))
1450 :group 'dired-x)
1451
1452 (defun dired-x-bind-find-file ()
1453 "Bind `dired-x-find-file' in place of `find-file' (or vice-versa).
1454 Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
1455 Binding direction based on `dired-x-hands-off-my-keys'."
1456 (interactive)
1457 (if (called-interactively-p 'interactive)
1458 (setq dired-x-hands-off-my-keys
1459 (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
1460 (define-key (current-global-map) [remap find-file]
1461 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file))
1462 (define-key (current-global-map) [remap find-file-other-window]
1463 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file-other-window)))
1464
1465 ;; Now call it so binding is correct. This could go in the :initialize
1466 ;; slot, but then dired-x-bind-find-file has to be defined before the
1467 ;; defcustom, and we get free variable warnings.
1468 (dired-x-bind-find-file)
1469
1470 (defun dired-x-find-file (filename)
1471 "Edit file FILENAME.
1472 Like `find-file', except that when called interactively with a
1473 prefix argument, it offers the filename near point as a default."
1474 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1475 (find-file filename))
1476
1477 (defun dired-x-find-file-other-window (filename)
1478 "Edit file FILENAME, in another window.
1479 Like `find-file-other-window', except that when called interactively with
1480 a prefix argument, when it offers the filename near point as a default."
1481 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1482 (find-file-other-window filename))
1483
1484 ;;; Internal functions.
1485
1486 ;; Fixme: This should probably use `thing-at-point'. -- fx
1487 (defun dired-filename-at-point ()
1488 "Return the filename closest to point, expanded.
1489 Point should be in or after a filename."
1490 (save-excursion
1491 ;; First see if just past a filename.
1492 (or (eobp) ; why?
1493 (when (looking-at "[] \t\n[{}()]") ; whitespace or some parens
1494 (skip-chars-backward " \n\t\r({[]})")
1495 (or (bobp) (backward-char 1))))
1496 (let ((filename-chars "-.[:alnum:]_/:$+@")
1497 start prefix)
1498 (if (looking-at (format "[%s]" filename-chars))
1499 (progn
1500 (skip-chars-backward filename-chars)
1501 (setq start (point)
1502 prefix
1503 ;; This is something to do with ange-ftp filenames.
1504 ;; It convert foo@bar to /foo@bar.
1505 ;; But when does the former occur in dired buffers?
1506 (and (string-match
1507 "^\\w+@"
1508 (buffer-substring start (line-end-position)))
1509 "/"))
1510 (if (string-match "[/~]" (char-to-string (preceding-char)))
1511 (setq start (1- start)))
1512 (skip-chars-forward filename-chars))
1513 (error "No file found around point!"))
1514 ;; Return string.
1515 (expand-file-name (concat prefix (buffer-substring start (point)))))))
1516
1517 (defun dired-x-read-filename-at-point (prompt)
1518 "Return filename prompting with PROMPT with completion.
1519 If `current-prefix-arg' is non-nil, uses name at point as guess."
1520 (if current-prefix-arg
1521 (let ((guess (dired-filename-at-point)))
1522 (read-file-name prompt
1523 (file-name-directory guess)
1524 guess
1525 nil (file-name-nondirectory guess)))
1526 (read-file-name prompt default-directory)))
1527
1528 (define-obsolete-function-alias 'read-filename-at-point
1529 'dired-x-read-filename-at-point "24.1") ; is this even needed?
1530 \f
1531 ;;; BUG REPORTS
1532
1533 (define-obsolete-function-alias 'dired-x-submit-report 'report-emacs-bug "24.1")
1534
1535 \f
1536 ;; As Barry Warsaw would say: "This might be useful..."
1537 (provide 'dired-x)
1538
1539 ;; Local Variables:
1540 ;; byte-compile-dynamic: t
1541 ;; generated-autoload-file: "dired.el"
1542 ;; End:
1543
1544 ;;; dired-x.el ends here