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