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