Mercurial > emacs
annotate lisp/ffap.el @ 14659:7669c19beda8
Comment change.
| author | Richard M. Stallman <rms@gnu.org> |
|---|---|
| date | Sat, 24 Feb 1996 04:43:05 +0000 |
| parents | 83f275dcd93a |
| children | cecaaaec6fd6 |
| rev | line source |
|---|---|
| 13531 | 1 ;;; ffap.el -- find-file-at-point, |
| 2 | |
| 14169 | 3 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc. |
| 4 | |
| 5 ;; Author: Michelangelo Grigni <mic@mathcs.emory.edu> | |
| 13531 | 6 |
| 7 ;; This file is part of GNU Emacs. | |
| 8 | |
| 9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
| 10 ;; it under the terms of the GNU General Public License as published by | |
| 11 ;; the Free Software Foundation; either version 2, or (at your option) | |
| 12 ;; any later version. | |
| 13 | |
| 14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
| 15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 ;; GNU General Public License for more details. | |
| 18 | |
| 19 ;; You should have received a copy of the GNU General Public License | |
| 14169 | 20 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
| 21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
| 22 ;; Boston, MA 02111-1307, USA. | |
| 13531 | 23 |
| 24 ;;; Commentary: | |
| 25 | |
| 26 ;; A replacement for find-file {C-x C-f}: finds file or URL, | |
| 27 ;; guessing default from text at point. Many features! | |
| 28 ;; Send bugs or suggestions with M-x ffap-bug. | |
| 29 | |
| 30 ;; See ftp://ftp.mathcs.emory.edu:/pub/mic/emacs/ for most recent version: | |
| 31 ;; ffap.el.gz -- this file, compressed with gzip | |
| 32 ;; ffap-xe.el -- support code for XEmacs 19.* | |
| 33 ;; COPYING.gz -- GNU General Public License, version 2 | |
| 34 ;; README -- description of these and other files | |
| 35 ;; | |
| 36 ;; For the last version sent to elisp-archive@cis.ohio-state.edu, see: | |
| 37 ;; ftp://ftp.cis.ohio-state.edu/pub/gnu/emacs/elisp-archive/misc/ffap.el.Z | |
| 38 ;; (mirrored in gatekeeper.dec.com:/pub/GNU/elisp-archive/misc/) | |
| 39 | |
| 40 ;;; Description: | |
| 41 ;; | |
| 42 ;; Command find-file-at-point (== ffap) replaces find-file. With a | |
| 43 ;; prefix, it behaves exactly like find-file. Without a prefix, it | |
| 44 ;; first tries to guess a default file or URL based on the text around | |
| 45 ;; the point (set `ffap-require-prefix' to swap these behaviors). | |
| 46 ;; This is a quick way to fetch URL and file references in many | |
| 47 ;; situations, such as in mail or news messages, README's, and | |
| 48 ;; MANIFEST's. | |
| 49 ;; | |
| 50 ;; Some related commands are ffap-at-mouse, ffap-next, ffap-menu, | |
| 51 ;; ffap-other-window, ffap-other-frame. | |
| 52 ;; | |
| 53 ;; This package is about user convenience. It adds nothing to the | |
| 54 ;; elisp programmer's repertoire. | |
| 55 | |
| 56 | |
| 57 ;;; Installation: | |
| 58 | |
| 59 ;; Quick Setup: | |
| 60 ;; | |
| 61 ;; For a basic installation, just install ffap.el somewhere in your | |
| 62 ;; `load-path', byte-compile it, and add the following two lines near | |
| 63 ;; the end of your ~/.emacs (or equivalent) file: | |
| 64 ;; | |
| 65 ;; (require 'ffap) ; load this file | |
| 66 ;; (global-set-key "\C-x\C-f" 'find-file-at-point) | |
| 67 ;; | |
| 68 ;; Other Packages: ffap notices the presence of several other packages | |
| 69 ;; when it is loaded. In particular, if you use ange-ftp, efs, w3, | |
| 70 ;; complete, or ff-paths (version < 3.00), it is best to load or | |
| 71 ;; autoload them before loading ffap (ffap does not need any of them). | |
| 72 ;; If you use ff-paths version >= 3.00, load it after ffap. | |
| 73 | |
| 74 ;; Fancier Setup: | |
| 75 ;; | |
| 76 ;; ffap has many options. The next comment block contains some | |
| 77 ;; fancier code that you might want to adapt for your .emacs. For | |
| 78 ;; even more features, look at the documentation (M-x apropos ffap), | |
| 79 ;; and perhaps check the comments in the "User Variables" and "Peanut | |
| 80 ;; Gallery" sections of this file. | |
| 81 | |
| 82 ;; ;; Before loading ffap: | |
| 83 ;; | |
| 84 ;; (setq ffap-url-regexp nil) ; to disable all URL features | |
| 85 ;; | |
| 86 ;; ;; Loading ffap: | |
| 87 ;; (require 'ffap) ; as in "Quick Setup" above | |
| 88 ;; | |
| 89 ;; After loading ffap: | |
| 90 ;; | |
| 91 ;; (global-set-key "\C-x\C-f" 'find-file-at-point) ; as in "Quick Setup" | |
| 92 ;; (global-set-key "\C-x4f" 'ffap-other-window) ; or \C-f | |
| 93 ;; (global-set-key "\C-x5f" 'ffap-other-frame) ; or \C-f | |
| 94 ;; | |
| 95 ;; (setq ffap-alist ; remove something in `ffap-alist' | |
| 96 ;; (delete (assoc 'c-mode ffap-alist) ffap-alist)) | |
| 97 ;; | |
| 98 ;; (setq ffap-alist ; add something to `ffap-alist' | |
| 99 ;; (cons | |
| 100 ;; (cons "^[Yy][Ss][Nn][0-9]+$" | |
| 101 ;; (defun ffap-ysn (name) | |
| 102 ;; (concat | |
| 103 ;; "http://snorri.chem.washington.edu/ysnarchive/issuefiles/" | |
| 104 ;; (substring name 3) ".html"))) | |
| 105 ;; ffap-alist)) | |
| 106 ;; | |
| 107 ;; | |
| 108 ;; Before or after loading ffap: | |
| 109 ;; | |
| 110 ;; (setq ffap-alist nil) ; disable all `ffap-alist' actions | |
| 111 ;; | |
| 112 ;; (setq ffap-require-prefix t) ; without prefix, ffap == find-file | |
| 113 ;; | |
| 114 ;; (setq ffap-machine-p-known 'accept) ; to avoid pinging | |
| 115 ;; | |
| 116 ;; ;; Choose a mouse binding appropriate for your emacs version: | |
| 117 ;; (global-set-key [S-mouse-1] 'ffap-at-mouse) ; Emacs 19 | |
| 118 ;; (global-set-key [(meta button1)] 'ffap-at-mouse) ; XEmacs | |
| 119 ;; (and window-system ; Emacs 18 (from .emacs) | |
| 120 ;; (setq window-setup-hook | |
| 121 ;; '(lambda nil (define-key mouse-map x-button-s-left | |
| 122 ;; 'ffap-at-mouse)))) | |
| 123 ;; | |
| 124 ;; ;; Use Netscape instead of w3 to fetch URL's. Mosaic is very similar. | |
| 125 ;; (if (eq window-system 'x) | |
| 126 ;; (progn | |
| 127 ;; ;; Get browse-url at http://wombat.doc.ic.ac.uk/emacs/browse-url.el, | |
| 128 ;; ;; or get a (probably out of date) copy from the ftp site above. | |
| 129 ;; (autoload 'browse-url-netscape "browse-url" nil t) | |
| 130 ;; (setq ffap-url-fetcher 'browse-url-netscape))) | |
| 131 ;; ;; Or for a hairier ffap-url-fetcher, get ffap-url.el (same ftp site). | |
| 132 ;; | |
| 133 ;; ;; Support for gnus, vm, rmail (see hook definitions for bindings): | |
| 134 ;; (add-hook 'gnus-summary-mode-hook 'ffap-gnus-hook) | |
| 135 ;; (add-hook 'gnus-article-mode-hook 'ffap-gnus-hook) | |
| 136 ;; (add-hook 'vm-mode-hook 'ffap-ro-mode-hook) | |
| 137 ;; (add-hook 'rmail-mode-hook 'ffap-ro-mode-hook) | |
| 138 | |
| 139 | |
| 140 ;;; Related packages: | |
| 141 ;; | |
| 142 ;; If you have hyperbole, you may not need this package, although ffap | |
| 143 ;; is smaller and smarter at this particular task. Also note that w3 | |
| 144 ;; (ftp.cs.indiana.edu:/pub/elisp/w3/README) offers a similar command | |
| 145 ;; w3-follow-url-at-point. | |
| 146 ;; | |
| 147 ;; The browse-url package (above) notices URL's and hands them off to | |
| 148 ;; w3 or an external WWW browser. Package |~/misc/goto-address.el.gz| | |
| 149 ;; by Eric J. Ding <ericding@mit.edu> notices URL's and mail | |
| 150 ;; addresses, and can pre-fontify a buffer to highlight them. Gnus5 | |
| 151 ;; (ding) and vm also provide similar support in their messages. | |
| 152 | |
| 153 | |
| 154 ;;; Examples: | |
| 155 ;; | |
| 156 ;; Try M-x find-file-at-point (maybe {C-x C-f}) on these examples. | |
| 157 ;; These local file examples use ordinary find-file: | |
| 158 ;; | |
| 159 ;; ffap.el, /etc/motd, $MAIL -- find local or absolute files | |
| 160 ;; .emacs book.sty info/cl pwd.h -- search paths depending on filename | |
| 161 ;; (require 'rmail) -- search paths depending on major-mode | |
| 162 ;; file:/etc/motd -- depends on `ffap-url-unwrap-local' | |
| 163 ;; | |
| 164 ;; These remote file examples work if you have ange-ftp or efs: | |
| 165 ;; | |
| 166 ;; ftp:/pub -- no ping (always works) | |
| 167 ;; ftp.x.org:README -- no ping, a nice recursive example | |
| 168 ;; anonymous@ftp.x.org:/README -- synonym | |
| 169 ;; ftp.x.org://README -- synonym | |
| 170 ;; ftp://ftp.x.org/README -- depends on `ffap-url-unwrap-remote' | |
| 171 ;; ftp.mathcs.emory.edu -- depends on `ffap-machine-p-known' | |
| 172 ;; mic@ftp:/ -- depends on `ffap-machine-p-local' | |
| 173 ;; ftp.mathcs.emory.edu:/ -- depends on `ffap-ftp-sans-slash-regexp' | |
| 174 ;; | |
| 175 ;; These URL examples use `ffap-url-fetcher' (default w3-fetch): | |
| 176 ;; | |
| 177 ;; http://www.cc.emory.edu | |
| 178 ;; http://www.cs.indiana.edu/elisp/w3/docs.html | |
| 179 ;; http://info.cern.ch/default.html | |
| 180 ;; news:news.newusers.questions | |
| 181 ;; mailto:mic@mathcs.emory.edu | |
| 182 ;; mic@mathcs.emory.edu -- same as previous | |
| 183 ;; <mic@mathcs.emory.edu> -- same as previous | |
| 184 ;; <root> -- mailto:root | |
| 185 ;; <mic.9@mathcs.emory.edu> -- see `ffap-foo@bar-prefix' | |
| 186 ;; file:/etc/motd -- see `ffap-url-unwrap-local' | |
| 187 ;; ftp://ftp.x.org/README -- see `ffap-url-unwrap-remote' | |
| 188 ;; | |
| 189 ;; Multiline gopher blocks (as in .gopherrc and usenet of yesteryear): | |
| 190 ;; | |
| 191 ;; Type=1 | |
| 192 ;; Name=Electronic Texts (ffap ignores this) | |
| 193 ;; Path= | |
| 194 ;; Host=etext.archive.umich.edu | |
| 195 ;; Port=70 | |
| 196 | |
| 197 | |
| 198 ;;; Code: | |
| 199 | |
| 200 (provide 'ffap) | |
| 201 | |
| 202 ;;; User Variables: | |
| 203 | |
| 204 ;; This function is used inside defvars: | |
| 205 (defun ffap-soft-value (name &optional default) | |
| 206 ;; Avoid interning. Bug: (ffap-soft-value "nil" 5) --> 5 | |
| 207 (let ((sym (intern-soft name))) | |
| 208 (if (and sym (boundp sym)) (symbol-value sym) default))) | |
| 209 | |
| 210 | |
| 211 (defvar ffap-ftp-regexp | |
| 212 (and | |
| 213 (or (featurep 'ange-ftp) | |
| 214 (featurep 'efs) | |
| 215 (and (boundp 'file-name-handler-alist) ; v19 | |
| 216 (or (rassq 'ange-ftp-hook-function file-name-handler-alist) | |
| 217 (rassq 'efs-file-handler-function file-name-handler-alist)))) | |
| 218 ;; Apparently this is good enough for both ange-ftp and efs: | |
| 219 "\\`/[^/:]+:") | |
| 220 "*Treat paths matching this as remote ftp paths. Nil to disable. | |
| 221 Nil also disables the generation of such paths by ffap.") | |
| 222 | |
| 223 (defvar ffap-url-unwrap-local t | |
| 224 "*If set, convert local \"file:\" URL to path before prompting.") | |
| 225 | |
| 226 (defvar ffap-url-unwrap-remote t | |
| 227 "*Convert remote \"file:\" or \"ftp:\" URL to path before prompting. | |
| 228 This is ignored if `ffap-ftp-regexp' is nil.") | |
| 229 | |
| 230 (defvar ffap-ftp-default-user | |
| 231 (if (or (equal (ffap-soft-value "ange-ftp-default-user") "anonymous") | |
| 232 (equal (ffap-soft-value "efs-default-user") "anonymous")) | |
| 233 nil | |
| 234 "anonymous") | |
| 235 "*User name in ftp paths generated by ffap (see host-to-ftp-path). | |
| 236 Nil to fall back on `efs-default-user' or `ange-ftp-default-user'.") | |
| 237 | |
| 238 (defvar ffap-rfs-regexp | |
| 239 ;; Remote file access built into file system? HP rfa or Andrew afs: | |
| 240 "\\`/\\(afs\\|net\\)/." | |
| 241 ;; afs only: (and (file-exists-p "/afs") "\\`/afs/.") | |
| 242 "*Paths matching this are remote file-system paths. Nil to disable.") | |
| 243 | |
| 244 (defvar ffap-url-regexp | |
| 245 ;; Could just use `url-nonrelative-link' of w3, if loaded. | |
| 246 ;; This regexp is not exhaustive, it just matches common cases. | |
| 247 (concat | |
| 248 "\\`\\(" | |
| 249 "news\\(post\\)?:\\|mailto:\\|file:" ; no host ok | |
| 250 "\\|" | |
| 251 "\\(ftp\\|http\\|telnet\\|gopher\\|www\\|wais\\)://" ; needs host | |
| 252 "\\)." ; require one more character | |
| 253 ) | |
| 254 "Regexp matching URL's, or nil to disable.") | |
| 255 | |
| 256 (defvar ffap-foo@bar-prefix "mailto" | |
| 257 "*Presumed url prefix type of strings like \"<foo.9z@bar>\". | |
| 258 Sensible values are nil, \"news\", or \"mailto\".") | |
| 259 | |
| 260 | |
| 261 ;;; Peanut Gallery: | |
| 262 | |
| 263 ;; Users of ffap occasionally suggest new features. If I consider | |
| 264 ;; those features interesting but not clear winners (a matter of | |
| 265 ;; personal taste) I try to leave options to enable them. Read | |
| 266 ;; through this section, and for any features you like, put an | |
| 267 ;; appropriate form in your ~/.emacs file. | |
| 268 | |
| 269 (defvar ffap-dired-wildcards nil ; "[*?][^/]*$" | |
| 270 ;; From RHOGEE, 07 Jul 1994. | |
| 271 ;; Disabled: dired is still available by "C-x C-d <pattern>", and | |
| 272 ;; valid filenames may contain wildcard characters. | |
| 273 "*A regexp matching filename wildcard characters, or nil. | |
| 274 If find-file-at-point gets a filename matching this pattern, | |
| 275 it passes it on to dired instead of find-file.") | |
| 276 | |
| 277 (defvar ffap-newfile-prompt nil ; t | |
| 278 ;; From RHOGEE, 11 Jul 1994. | |
| 279 ;; Disabled: this is better handled by `find-file-not-found-hooks'. | |
| 280 "*Whether find-file-at-point prompts about a nonexistent file.") | |
| 281 | |
| 282 (defvar ffap-require-prefix nil | |
| 283 ;; From RHOGEE, 20 Oct 1994. | |
| 284 ;; This is nil so that neophytes notice ffap. Experts instead may | |
| 285 ;; prefer to disable ffap most of the time. | |
| 286 "*If set, reverses the prefix argument to find-file-at-point.") | |
| 287 | |
| 288 (defvar ffap-file-finder | |
| 289 ;; From RHOGEE, 20 Oct 1994. | |
| 290 ;; This allows compatibility with ff-paths version < 3.00. | |
| 291 ;; For ff-paths version >= 3.00, just load it after ffap. | |
| 292 (if (commandp 'find-file-using-paths) | |
| 293 'find-file-using-paths | |
| 294 ;; Try to overcome load-order dependency: | |
| 295 (eval-after-load | |
| 296 "ff-paths" | |
| 297 '(and (commandp 'find-file-using-paths) | |
| 298 (setq ffap-file-finder find-file-using-paths))) | |
| 299 'find-file) | |
| 300 "*The command symbol called by find-file-at-point to find a file. | |
| 301 Probably find-file, or find-file-using-paths if you use ff-paths | |
| 302 with version < 3.00.") | |
| 303 (put 'ffap-file-finder 'risky-local-variable t) | |
| 304 | |
| 305 (defvar ffap-url-fetcher 'w3-fetch | |
| 306 "*A function of one argument, called by ffap to fetch URL's. | |
| 307 The default is w3-fetch from the w3 package. If you prefer Mosaic or | |
| 308 Netscape, install http://wombat.doc.ic.ac.uk/emacs/browse-url.el, and | |
| 309 add one of the following lines to your setup: | |
| 310 | |
| 311 \(setq ffap-url-fetcher 'browse-url-netscape\) | |
| 312 \(setq ffap-url-fetcher 'browse-url-mosaic\) | |
| 313 | |
| 314 Or for something hairier \(choose fetch method based on url type and | |
| 315 prompting\) get ffap-url.el wherever you ffap.el." | |
| 316 ;; Big old `lambda' examples deleted. Some remote-control references: | |
| 317 ;; http://www.ncsa.uiuc.edu/SDG/Software/XMosaic/remote-control.html | |
| 318 ;; http://home.netscape.com/newsref/std/x-remote.html | |
| 319 ) | |
| 320 (put 'ffap-url-fetcher 'risky-local-variable t) | |
| 321 | |
| 322 | |
| 323 ;;; Command ffap-next: | |
| 324 ;; | |
| 325 ;; Original ffap-next-url (URL's only) from RPECK 30 Mar 1995. | |
| 326 ;; Since then, broke up into ffap-next-guess (noninteractive) and | |
| 327 ;; ffap-next (a command), now work on files as well as url's. | |
| 328 | |
| 329 (defvar ffap-next-regexp | |
| 330 ;; If you want ffap-next to find URL's only, try this: | |
| 331 ;; (and ffap-url-regexp (string-match "\\\\`" ffap-url-regexp) | |
| 332 ;; (concat "\\<" (substring ffap-url-regexp 2)))) | |
| 333 ;; | |
| 334 ;; It pays to put a big fancy regexp here, since ffap-guesser is | |
| 335 ;; much more time-consuming than regexp searching: | |
| 336 "[/:.~a-zA-Z]/\\|@[a-zA-Z][-a-zA-Z0-9]*\\." | |
| 337 "*Regular expression governing search of ffap-next.") | |
| 338 | |
| 339 (defvar ffap-next-guess nil "Last value returned by `ffap-next-guess'.") | |
| 340 (defun ffap-next-guess (&optional back lim) | |
| 341 "Move point to next file or url, and return it as a string. | |
| 342 If nothing found, leaves point at limit and returns nil. | |
| 343 Optional BACK argument makes search backwards. | |
| 344 Optional LIM argument limits the search. | |
| 345 Only considers strings that match `ffap-next-regexp'." | |
| 346 (or lim (setq lim (if back (point-min) (point-max)))) | |
| 347 (let (guess) | |
| 348 (while (not (or guess (eq (point) lim))) | |
| 349 (funcall (if back 're-search-backward 're-search-forward) | |
| 350 ffap-next-regexp lim 'move) | |
| 351 (setq guess (ffap-guesser))) | |
| 352 ;; Go to end, so we do not get same guess twice: | |
| 353 (goto-char (nth (if back 0 1) ffap-string-at-point-region)) | |
| 354 (setq ffap-next-guess guess))) | |
| 355 | |
| 356 ;;;###autoload | |
| 357 (defun ffap-next (&optional back wrap) | |
| 358 "Search buffer for next file or url, and run ffap. | |
| 359 Optional argument BACK says to search backwards. | |
| 360 Optional argument WRAP says to try wrapping around if necessary. | |
| 361 Interactively: use a single prefix to search backwards, | |
| 362 double prefix to wrap forward, triple to wrap backwards. | |
| 363 Actual search is done by ffap-next-guess." | |
| 364 (interactive | |
| 365 (cdr (assq (prefix-numeric-value current-prefix-arg) | |
| 366 '((1) (4 t) (16 nil t) (64 t t))))) | |
| 367 (let ((pt (point)) | |
| 368 (guess (ffap-next-guess back))) | |
| 369 ;; Try wraparound if necessary: | |
| 370 (and (not guess) wrap | |
| 371 (goto-char (if back (point-max) (point-min))) | |
| 372 (setq guess (ffap-next-guess back pt))) | |
| 373 (if guess | |
| 374 (progn | |
| 375 (sit-for 0) ; display point movement | |
| 376 (find-file-at-point (ffap-prompter guess))) | |
| 377 (goto-char pt) ; restore point | |
| 378 (message "No %sfiles or URL's found." | |
| 379 (if wrap "" "more "))))) | |
| 380 | |
| 381 (defun ffap-next-url (&optional back wrap) | |
| 382 "Just like ffap-next, but searches with `ffap-url-regexp'." | |
| 383 (interactive) | |
| 384 (let ((ffap-next-regexp ffap-url-regexp)) | |
| 385 (if (interactive-p) | |
| 386 (call-interactively 'ffap-next) | |
| 387 (ffap-next back wrap)))) | |
| 388 | |
| 389 | |
| 390 ;;; Hooks for GNUS, VM, Rmail: | |
| 391 ;; | |
| 392 ;; See "Installation" above for suggested use of these hooks. | |
| 393 ;; If you do not like these bindings, just write hooks with | |
| 394 ;; whatever bindings you would prefer. | |
| 395 ;; | |
| 396 ;; Any suggestions of more "memorable" bindings? -- Mic | |
| 397 | |
| 398 (defun ffap-ro-mode-hook nil | |
| 399 "Binds ffap-gnus-next and ffap-gnus-menu to M-l and M-m, resp." | |
| 400 (local-set-key "\M-l" 'ffap-next) | |
| 401 (local-set-key "\M-m" 'ffap-menu) | |
| 402 ) | |
| 403 | |
| 404 (defun ffap-gnus-hook nil | |
| 405 "Binds ffap-gnus-next and ffap-gnus-menu to L and M, resp." | |
| 406 (set (make-local-variable 'ffap-foo@bar-prefix) "news") ; message-id's | |
| 407 ;; Note lowercase l and m are taken: | |
| 408 (local-set-key "L" 'ffap-gnus-next) | |
| 409 (local-set-key "M" 'ffap-gnus-menu)) | |
| 410 | |
| 411 (defun ffap-gnus-wrapper (form) ; used by both commands below | |
| 412 (and (eq (current-buffer) (get-buffer gnus-summary-buffer)) | |
| 413 (gnus-summary-select-article)) ; get article of current line | |
| 414 ;; Preserve selected buffer, but do not do save-window-excursion, | |
| 415 ;; since we want to see any window created by form. Temporarily | |
| 416 ;; select the article buffer, so we see any point movement. | |
| 417 (let ((sb (window-buffer (selected-window)))) | |
| 418 (gnus-configure-windows 'article) | |
| 419 (pop-to-buffer gnus-article-buffer) | |
| 420 (widen) | |
| 421 ;; Skip headers at first, for ffap-gnus-next (which wraps around) | |
| 422 (if (eq (point) (point-min)) (search-forward "\n\n" nil t)) | |
| 423 (unwind-protect | |
| 424 (eval form) | |
| 425 (pop-to-buffer sb)))) | |
| 426 | |
| 427 (defun ffap-gnus-next nil | |
| 428 "Run ffap-next in the GNUS article buffer." | |
| 429 (interactive) (ffap-gnus-wrapper '(ffap-next nil t))) | |
| 430 | |
| 431 (defun ffap-gnus-menu nil | |
| 432 "Run ffap-menu in the GNUS article buffer." | |
| 433 (interactive) (ffap-gnus-wrapper '(ffap-menu))) | |
| 434 | |
| 435 | |
| 436 ;;; Remote machines and paths: | |
| 437 | |
| 438 (fset 'ffap-replace-path-component | |
| 439 (if (or (featurep 'efs) | |
| 440 (and | |
| 441 (boundp 'file-name-handler-alist) ; v19 | |
| 442 (rassq 'efs-file-handler-function file-name-handler-alist))) | |
| 443 'efs-replace-path-component | |
| 444 'ange-ftp-replace-name-component)) | |
| 445 | |
| 446 (defun ffap-file-exists-string (file) | |
| 447 ;; With certain packages (ange-ftp, jka-compr?) file-exists-p | |
| 448 ;; sometimes returns a nicer string than it is given. Otherwise, it | |
| 449 ;; just returns nil or t. | |
| 450 "Return FILE \(maybe modified\) if it exists, else nil." | |
| 451 (let ((exists (file-exists-p file))) | |
| 452 (and exists (if (stringp exists) exists file)))) | |
| 453 | |
| 454 ;; I cannot decide a "best" strategy here, so these are variables. In | |
| 455 ;; particular, if `Pinging...' is broken or takes too long on your | |
| 456 ;; machine, try setting these all to accept or reject. | |
| 457 (defvar ffap-machine-p-local 'reject ; this happens often | |
| 458 "A symbol, one of: ping, accept, reject. | |
| 459 This is what ffap-machine-p does with hostnames that have no domain.") | |
| 460 (defvar ffap-machine-p-known 'ping ; 'accept for speed | |
| 461 "A symbol, one of: ping, accept, reject. | |
| 462 This is what ffap-machine-p does with hostnames that have a known domain | |
| 463 \(see lisp/mail-extr.el for the list of known domains\).") | |
| 464 (defvar ffap-machine-p-unknown 'reject | |
| 465 "A symbol, one of: ping, accept, reject. | |
| 466 This is what ffap-machine-p does with hostnames that have an unknown domain | |
| 467 \(see lisp/mail-extr.el for the list of known domains\).") | |
| 468 | |
| 469 (defvar ffap-machine-p-known-domains | |
| 470 '("com" "edu" "net" "org" "mil" "gov" "us" "arpa") ; USA USA... | |
| 471 ;; This variable is mainly for emacs18. | |
| 472 "Top-level domains known to ffap. Ignored if mail-extr is loadable.") | |
| 473 | |
| 474 (defun ffap-machine-p (host &optional service quiet) | |
| 475 "Indicate whether HOST is the name of a real machine. | |
| 476 The variables ffap-machine-p-local, ffap-machine-p-known, and ffap-machine-p-unknown | |
| 477 control ffap-machine-p depending on HOST's domain \(none/known/unknown\). | |
| 478 Pinging is done using open-network-stream to decide HOST existence. | |
| 479 Optional SERVICE specifies the service used \(default \"discard\"\). | |
| 480 Optional QUIET flag suppresses the \"Pinging...\" message. | |
| 481 Returned values: | |
| 482 A t value means that HOST answered. | |
| 483 A symbol \(accept\) means the relevant variable told us to accept. | |
| 484 A string means the machine exists, but does not respond for some reason." | |
| 485 ;; Try some: | |
| 486 ;; (ffap-machine-p "ftp") | |
| 487 ;; (ffap-machine-p "nonesuch") | |
| 488 ;; (ffap-machine-p "ftp.mathcs.emory.edu") | |
| 489 ;; (ffap-machine-p "foo.bonk") | |
| 490 ;; (ffap-machine-p "foo.bonk.com") | |
| 491 ;; (ffap-machine-p "cs" 5678) | |
| 492 ;; (ffap-machine-p "gopher.house.gov") | |
| 493 ;; Not known to 19.28 | |
| 494 ;; (ffap- | |
| 495 (if (or (string-match "[^-a-zA-Z0-9.]" host) ; Illegal chars (?) | |
| 496 (not (string-match "[^0-9]" host))) ; all numeric! reject it | |
| 497 nil | |
| 498 (let* ((domain | |
| 499 (and (string-match "\\.[^.]*$" host) | |
| 500 (downcase (substring host (1+ (match-beginning 0)))))) | |
| 501 (domain-name ; t, "Country", "Local", or nil | |
| 502 (cond | |
| 503 ((not domain) "Local") | |
| 504 ;; common non-country domains (some imply US though): | |
| 505 ;; t) | |
| 506 (t | |
| 507 ;; Use domain-name properties from v19 lisp/mail-extr.el; | |
| 508 ;; bbdb/mail-extr also puts this in `all-top-level-domains'. | |
| 509 (if (or (featurep 'mail-extr) | |
| 510 (and (load "mail-extr" t t) | |
| 511 ;; It became a feature between 19.22 and 19.28 | |
| 512 (provide 'mail-extr))) | |
| 513 (get (intern-soft | |
| 514 domain | |
| 515 (condition-case nil | |
| 516 mail-extr-all-top-level-domains | |
| 517 ;; Before 19.28, the symbols were in `obarray': | |
| 518 (error obarray))) | |
| 519 'domain-name) | |
| 520 ;; Emacs18 does not have mail-extr: | |
| 521 (and (member domain ffap-machine-p-known-domains) t)) | |
| 522 ))) | |
| 523 (strategy | |
| 524 (cond ((not domain) ffap-machine-p-local) | |
| 525 ((not domain-name) ffap-machine-p-unknown) | |
| 526 (ffap-machine-p-known)))) | |
| 527 (cond | |
| 528 ((eq strategy 'accept) 'accept) | |
| 529 ((eq strategy 'reject) nil) | |
| 530 ;; assume (eq strategy 'ping) | |
| 531 (t | |
| 532 (or quiet | |
| 533 (if (stringp domain-name) | |
| 534 (message "Pinging %s (%s)..." host domain-name) | |
| 535 (message "Pinging %s ..." host))) | |
| 536 (condition-case error | |
| 537 (progn | |
| 538 (delete-process | |
| 539 (open-network-stream | |
| 540 "ffap-machine-p" nil host (or service "discard"))) | |
| 541 t) | |
| 542 (error | |
| 543 (let ((mesg (car (cdr error)))) | |
| 544 (cond | |
| 545 ;; v18: | |
| 546 ((string-match "^Unknown host" mesg) nil) | |
| 547 ((string-match "not responding$" mesg) mesg) | |
| 548 ;; v19: | |
| 549 ;; (file-error "connection failed" "permission denied" | |
| 550 ;; "nonesuch" "ffap-machine-p") | |
| 551 ;; (file-error "connection failed" "host is unreachable" | |
| 552 ;; "gopher.house.gov" "ffap-machine-p") | |
| 553 ;; (file-error "connection failed" "address already in use" | |
| 554 ;; "ftp.uu.net" "ffap-machine-p") | |
| 555 ((equal mesg "connection failed") | |
| 556 (if (equal (nth 2 error) "permission denied") | |
| 557 nil ; host does not exist | |
| 558 ;; Other errors mean host exists: | |
| 559 (nth 2 error))) | |
| 560 ;; Could be "Unknown service": | |
| 561 (t (signal (car error) (cdr error)))))))))))) | |
| 562 | |
| 563 (defun ffap-file-remote-p (filename) | |
| 564 "If FILENAME looks remote, return it \(maybe slightly improved\)." | |
| 565 ;; (ffap-file-remote-p "/user@foo.bar.com:/pub") | |
| 566 ;; (ffap-file-remote-p "/foo.dom://path") | |
| 567 (or (and ffap-ftp-regexp | |
| 568 (string-match ffap-ftp-regexp filename) | |
| 13979 | 569 ;; Convert "/host://path" to "/host:/path", to handle a dying |
| 13531 | 570 ;; practice of advertising ftp paths as "host.dom://path". |
| 571 (if (string-match "//" filename) | |
| 572 (concat (substring filename 0 (match-beginning 0)) | |
| 573 (substring filename (1- (match-end 0)))) | |
| 574 filename)) | |
| 575 (and ffap-rfs-regexp | |
| 576 (string-match ffap-rfs-regexp filename) | |
| 577 filename))) | |
| 578 | |
| 579 (defun ffap-machine-at-point nil | |
| 580 "Return machine name from around point if it exists, or nil." | |
| 581 (let ((mach (ffap-string-at-point "-a-zA-Z0-9." nil "."))) | |
| 582 (and (ffap-machine-p mach) mach))) | |
| 583 | |
| 584 (defun ffap-fixup-machine (mach) | |
| 585 ;; Convert a machine into an URL, an ftp path, or nil. | |
| 586 (cond | |
| 587 ((not (and ffap-url-regexp (stringp mach))) nil) | |
| 588 ((string-match "\\`gopher[-.]" mach) ; or "info"? | |
| 589 (concat "gopher://" mach "/")) | |
| 590 ((and (string-match "\\`w\\(ww\\|eb\\)[-.]" mach)) | |
| 591 (concat "http://" mach "/")) | |
| 592 ;; More cases? Maybe "telnet:" for archie? | |
| 593 (ffap-ftp-regexp (ffap-host-to-path mach)) | |
| 594 )) | |
| 595 | |
| 596 (defun ffap-host-to-path (host) | |
| 597 "Convert \"HOST\" to \"/anonymous@HOST:\" (or \"\" for \"localhost\"). | |
| 598 Variable `ffap-ftp-default-user' overrides or suppresses \"anonymous\"." | |
| 599 (if (equal host "localhost") | |
| 600 "" | |
| 601 (if ffap-ftp-default-user | |
| 602 (concat "/" ffap-ftp-default-user "@" host ":") | |
| 603 (concat "/" host ":")))) | |
| 604 | |
| 605 (defun ffap-newsgroup-p (string) | |
| 606 "Return STRING if it looks like a newsgroup name, else nil." | |
| 607 (and | |
| 608 (string-match ffap-newsgroup-regexp string) | |
| 609 (let ((htbs '(gnus-active-hashtb gnus-newsrc-hashtb gnus-killed-hashtb)) | |
| 610 (heads ffap-newsgroup-heads) | |
| 611 htb ret) | |
| 612 (while htbs | |
| 613 (setq htb (car htbs) htbs (cdr htbs)) | |
| 614 (condition-case nil | |
| 615 (progn | |
| 616 ;; errs: htb symbol may be unbound, or not a hash-table. | |
| 617 ;; gnus-gethash is just a macro for intern-soft. | |
| 618 (and (intern-soft string (symbol-value htb)) | |
| 619 (setq ret string htbs nil)) | |
| 620 ;; If we made it this far, GNUS is running, so ignore "heads": | |
| 621 (setq heads nil)) | |
| 622 (error nil))) | |
| 623 (or ret (not heads) | |
| 624 (let ((head (string-match "\\`\\([a-z]+\\)\\." string))) | |
| 625 (and head (setq head (substring string 0 (match-end 1))) | |
| 626 (member head heads) | |
| 627 (setq ret string)))) | |
| 628 ;; Ever any need to modify string as a newsgroup name? | |
| 629 ret))) | |
| 630 (defvar ffap-newsgroup-regexp "^[a-z]+\\.[-+a-z_0-9.]+$" | |
| 631 "ffap-newsgroup-p quickly rejects strings that do not match this.") | |
| 632 (defvar ffap-newsgroup-heads ; entirely inadequate | |
| 633 '("alt" "comp" "gnu" "misc" "news" "sci" "soc" "talk") | |
| 634 "Used by ffap-newsgroup-p if GNUS is not running.") | |
| 635 | |
| 636 (defun ffap-url-p (string) | |
| 637 "If STRING looks like an URL, return it (maybe improved), else nil." | |
| 638 ;; Does it look like an URL? Ignore case. | |
| 639 (let ((case-fold-search t)) | |
| 640 (and ffap-url-regexp (string-match ffap-url-regexp string) | |
| 641 ;; I lied, no improvement: | |
| 642 string))) | |
| 643 | |
| 644 ;; Broke these two out of ffap-fixup-url, for sake of ffap-url package. | |
| 645 (defun ffap-url-unwrap-local (url) | |
| 646 "Return unwrapped local file URL, or nil. Ignores ffap-* variables." | |
| 647 (and (string-match "\\`\\(file\\|ftp\\):/?\\([^/]\\|\\'\\)" url) | |
| 648 (substring url (1+ (match-end 1))))) | |
| 649 (defun ffap-url-unwrap-remote (url) | |
| 650 "Return unwrapped remote file URL, or nil. Ignores ffap-* variables." | |
| 651 (and (string-match "\\`\\(ftp\\|file\\)://\\([^:/]+\\):?\\(/.*\\)" url) | |
| 652 (concat | |
| 653 (ffap-host-to-path (substring url (match-beginning 2) (match-end 2))) | |
| 654 (substring url (match-beginning 3) (match-end 3))))) | |
| 655 | |
| 656 (defun ffap-fixup-url (url) | |
| 657 "Given URL, clean it up and return it. May become a file name." | |
| 658 (cond | |
| 659 ((not (stringp url)) nil) | |
| 660 ((and ffap-url-unwrap-local (ffap-url-unwrap-local url))) | |
| 661 ((and ffap-url-unwrap-remote ffap-ftp-regexp | |
| 662 (ffap-url-unwrap-remote url))) | |
| 663 ;; Do not load w3 just for this: | |
| 664 (t (let ((normal (and (fboundp 'url-normalize-url) | |
| 665 (url-normalize-url url)))) | |
| 666 ;; In case url-normalize-url is confused: | |
| 667 (or (and normal (not (zerop (length normal))) normal) | |
| 668 url))))) | |
| 669 | |
| 670 | |
| 671 ;;; `ffap-alist': | |
| 672 ;; | |
| 673 ;; Search actions depending on the major-mode or extensions of the | |
| 674 ;; current name. Note all the little defun's could be broken out, at | |
| 675 ;; some loss of locality. I have had a vote for eliminating this | |
| 676 ;; from ffap (featuritis) | |
| 677 | |
| 678 ;; First, some helpers for functions in `ffap-alist': | |
| 679 | |
| 680 (defun ffap-list-env (env &optional empty) | |
| 681 ;; Replace this with parse-colon-path (lisp/files.el)? | |
| 13979 | 682 "Directory list parsed from \":\"-separated ENVironment variable. |
| 13531 | 683 Optional EMPTY is default if (getenv ENV) is undefined, and is also |
| 684 substituted for the first empty-string component, if there is one." | |
| 685 ;; Derived from psg-list-env in RHOGEE's ff-paths and | |
| 686 ;; bib-cite packages. The `empty' argument is intended to mimic | |
| 687 ;; the semantics of TeX/BibTeX variables, it is substituted for | |
| 688 ;; any empty string entry. | |
| 689 (if (or empty (getenv env)) ; should return something | |
| 690 (let ((start 0) match dir ret) | |
|
13905
7df0a9cb269a
(ffap-list-env): Use `path-separator' rather than ":".
Richard M. Stallman <rms@gnu.org>
parents:
13531
diff
changeset
|
691 (setq env (concat (getenv env) path-separator)) ; note undefined -> ":" |
|
7df0a9cb269a
(ffap-list-env): Use `path-separator' rather than ":".
Richard M. Stallman <rms@gnu.org>
parents:
13531
diff
changeset
|
692 (while (setq match (string-match path-separator env start)) |
| 13531 | 693 (setq dir (substring env start match) start (1+ match)) |
| 694 ;;(and (file-directory-p dir) (not (member dir ret)) ...) | |
| 695 (setq ret (cons dir ret))) | |
| 696 (setq ret (nreverse ret)) | |
| 697 (and empty (setq match (member "" ret)) | |
| 698 (progn | |
| 699 (setcdr match (append (cdr-safe empty) (cdr match))) | |
| 700 (setcar match (or (car-safe empty) empty)))) | |
| 701 ret))) | |
| 702 | |
| 703 (defun ffap-reduce-path (path) | |
| 704 "Remove duplicates or non-dirs from PATH." | |
| 705 (let (ret tem) | |
| 706 (while path | |
| 707 (setq tem path path (cdr path)) | |
| 708 (or (member (car tem) ret) | |
| 709 (not (file-directory-p (car tem))) | |
| 710 (progn (setcdr tem ret) (setq ret tem)))) | |
| 711 (nreverse ret))) | |
| 712 | |
| 713 (defun ffap-add-subdirs (path) | |
| 714 "Return PATH augmented with its immediate subdirectories." | |
| 715 ;; (ffap-add-subdirs '("/notexist" "~")) | |
| 716 (let (ret subs) | |
| 717 (while path | |
| 718 (mapcar | |
| 719 (function | |
| 720 (lambda (f) (and (file-directory-p f) (setq ret (cons f ret))))) | |
| 721 (condition-case nil | |
| 722 (directory-files (car path) t "[^.]") | |
| 723 (error nil))) | |
| 724 (setq ret (cons (car path) ret) | |
| 725 path (cdr path))) | |
| 726 (nreverse ret))) | |
| 727 | |
| 728 (defvar ffap-locate-jka-suffixes t | |
| 729 "List of compression suffixes that ffap-locate-file tries. | |
| 730 If not a list, it will be initialized by ffap-locate-file, | |
| 731 and it will become nil unless you are using jka-compr. | |
| 732 You might set this to nil or a list like '(\".gz\" \".z\" \".Z\").") | |
| 733 | |
| 734 (defun ffap-locate-file (file &optional nosuffix path) | |
| 735 ;; If this package is only working in v19 now, maybe should | |
| 736 ;; replace this with a quiet version of locate-library. | |
| 737 "A generic path-searching function, defaults mimic `load' behavior. | |
| 738 Returns path of an existing FILE that (load FILE) would load, or nil. | |
| 739 Optional second argument NOSUFFIX, if t, is like the fourth argument | |
| 740 for load, i.e. don't try adding suffixes \".elc\" and \".el\". | |
| 741 If a list, it is taken as a list of suffixes to try instead. | |
| 742 Optional third argument PATH specifies a different search path, it | |
| 743 defaults to `load-path'." | |
| 744 (or path (setq path load-path)) | |
| 745 (if (file-name-absolute-p file) | |
| 746 (setq path (list (file-name-directory file)) | |
| 747 file (file-name-nondirectory file))) | |
| 748 (let ((suffixes-to-try | |
| 749 (cond | |
| 750 ((consp nosuffix) nosuffix) | |
| 751 (nosuffix '("")) | |
| 752 (t '(".elc" ".el" ""))))) | |
| 753 ;; Compensate for modern (19.28) jka-compr, that no longer searches | |
| 754 ;; for foo.gz when you asked for foo: | |
| 755 (or (listp ffap-locate-jka-suffixes) | |
| 756 (setq ffap-locate-jka-suffixes | |
| 757 (and (featurep 'jka-compr) ; an early version was jka-compr19 | |
| 758 (not (featurep 'jka-aux)) | |
| 759 jka-compr-file-name-handler-entry | |
| 760 (not (string-match | |
| 761 (car jka-compr-file-name-handler-entry) | |
| 762 "foo")) | |
| 763 ;; Hard to do cleverly across various jka-compr versions: | |
| 764 '(".gz" ".Z")))) | |
| 765 (if ffap-locate-jka-suffixes | |
| 766 (setq suffixes-to-try | |
| 767 (apply | |
| 768 'nconc | |
| 769 (mapcar | |
| 770 (function | |
| 771 (lambda (suf) | |
| 772 (cons suf | |
| 773 (mapcar | |
| 774 (function (lambda (x) (concat suf x))) | |
| 775 ffap-locate-jka-suffixes)))) | |
| 776 suffixes-to-try)))) | |
| 777 (let (found suffixes) | |
| 778 (while (and path (not found)) | |
| 779 (setq suffixes suffixes-to-try) | |
| 780 (while (and suffixes (not found)) | |
| 781 (let ((try (expand-file-name | |
| 782 (concat file (car suffixes)) | |
| 783 (car path)))) | |
| 784 (if (and (file-exists-p try) (not (file-directory-p try))) | |
| 785 (setq found try))) | |
| 786 (setq suffixes (cdr suffixes))) | |
| 787 (setq path (cdr path))) | |
| 788 found))) | |
| 789 | |
| 790 (defvar ffap-alist | |
| 791 ;; A big mess! Parts are probably useless. | |
| 792 (list | |
| 793 (cons "\\.info\\'" | |
| 794 (defun ffap-info (name) | |
| 795 (ffap-locate-file | |
| 796 name '("" ".info") | |
| 797 (or (ffap-soft-value "Info-directory-list") | |
| 798 (ffap-soft-value "Info-default-directory-list") | |
| 799 ;; v18: | |
| 800 (list (ffap-soft-value "Info-directory" "~/info/")))))) | |
| 801 ;; Since so many info files do not have .info extension, also do this: | |
| 802 (cons "\\`info/" | |
| 803 (defun ffap-info-2 (name) (ffap-info (substring name 5)))) | |
| 804 (cons "\\`[-a-z]+\\'" | |
| 805 ;; This ignores the node! "(emacs)Top" same as "(emacs)Intro" | |
| 806 (defun ffap-info-3 (name) | |
| 807 (and (equal (ffap-string-around) "()") (ffap-info name)))) | |
| 808 (cons "\\.elc?\\'" | |
| 809 (defun ffap-el (name) (ffap-locate-file name t))) | |
| 810 (cons 'emacs-lisp-mode | |
| 811 (defun ffap-el-mode (name) | |
| 812 ;; We do not bother with "" here, since it was considered above. | |
| 813 ;; Also ignore "elc", for speed (who else reads elc files?) | |
| 814 (and (not (string-match "\\.el\\'" name)) | |
| 815 (ffap-locate-file name '(".el"))))) | |
| 816 '(finder-mode . ffap-el-mode) ; v19: {C-h p} | |
| 817 '(help-mode . ffap-el-mode) ; v19.29 | |
| 818 (cons 'c-mode | |
| 819 (progn | |
| 820 ;; Need better default here: | |
| 821 (defvar ffap-c-path '("/usr/include" "/usr/local/include")) | |
| 822 (defun ffap-c-mode (name) | |
| 823 (ffap-locate-file name t ffap-c-path)))) | |
| 824 '(c++-mode . ffap-c-mode) | |
| 825 '(cc-mode . ffap-c-mode) | |
| 826 '("\\.\\([chCH]\\|cc\\|hh\\)\\'" . ffap-c-mode) | |
| 827 (cons 'tex-mode | |
| 828 ;; Complicated because auctex may not be loaded yet. | |
| 829 (progn | |
| 830 (defvar ffap-tex-path | |
| 831 (ffap-reduce-path | |
| 832 (append | |
| 833 (list ".") | |
| 834 (ffap-list-env "TEXINPUTS") | |
| 835 ;; (ffap-list-env "BIBINPUTS") | |
| 836 (ffap-add-subdirs | |
| 837 (ffap-list-env "TEXINPUTS_SUBDIR" | |
| 838 (ffap-soft-value | |
| 839 "TeX-macro-global" | |
| 840 '("/usr/local/lib/tex/macros" | |
| 841 "/usr/local/lib/tex/inputs") | |
| 842 ))))) | |
| 843 "*Where ffap-tex-mode looks for tex files.") | |
| 844 (defun ffap-tex-mode (name) | |
| 845 (ffap-locate-file name '(".tex" "") ffap-tex-path)))) | |
| 846 (cons 'latex-mode | |
| 847 (defun ffap-latex-mode (name) | |
| 848 ;; Any real need for "" here? | |
| 849 (ffap-locate-file name '(".sty" ".tex" "") ffap-tex-path))) | |
| 850 (cons "\\.\\(tex\\|sty\\|doc\\)\\'" | |
| 851 (defun ffap-tex (name) | |
| 852 (ffap-locate-file name t ffap-tex-path))) | |
| 853 (cons "\\.bib\\'" | |
| 854 (defun ffap-bib (name) | |
| 855 (ffap-locate-file | |
| 856 name t | |
| 857 (ffap-list-env "BIBINPUTS" '("/usr/local/lib/tex/macros/bib"))))) | |
| 858 (cons 'math-mode | |
| 859 (defun ffap-math-mode (name) | |
| 860 (while (string-match "`" name) | |
| 861 (setq name (concat (substring name 0 (match-beginning 0)) | |
| 862 "/" | |
| 863 (substring name (match-end 0))))) | |
| 864 (ffap-locate-file | |
| 865 name '(".m" "") (ffap-soft-value "Mathematica-search-path")))) | |
| 866 (cons "\\`\\." (defun ffap-home (name) (ffap-locate-file name t '("~")))) | |
| 867 (cons "\\`~/" | |
| 868 ;; Maybe a "Lisp Code Directory" reference: | |
| 869 (defun ffap-lcd (name) | |
| 870 (and | |
| 871 (or | |
| 872 ;; lisp-dir-apropos output buffer: | |
| 873 (string-match "Lisp Code Dir" (buffer-name)) | |
| 874 ;; Inside an LCD entry like |~/misc/ffap.el.Z|, | |
| 875 ;; or maybe the holy LCD-Datafile itself: | |
| 876 (member (ffap-string-around) '("||" "|\n"))) | |
| 877 (concat | |
| 878 ;; lispdir.el may not be loaded yet: | |
| 879 (ffap-host-to-path | |
| 880 (ffap-soft-value "elisp-archive-host" | |
| 881 "archive.cis.ohio-state.edu")) | |
| 882 (file-name-as-directory | |
| 883 (ffap-soft-value "elisp-archive-directory" | |
| 884 "/pub/gnu/emacs/elisp-archive/")) | |
| 885 (substring name 2))))) | |
| 886 (cons "^[Rr][Ff][Cc][- #]?\\([0-9]+\\)" ; no $ | |
| 887 (progn | |
| 888 (defvar ffap-rfc-path | |
| 889 (concat (ffap-host-to-path "ds.internic.net") "/rfc/rfc%s.txt")) | |
| 890 (defun ffap-rfc (name) | |
| 891 (format ffap-rfc-path | |
| 892 (substring name (match-beginning 1) (match-end 1)))))) | |
| 893 ) | |
| 894 "Alist of \(KEY . FUNCTION\), applied to text around point. | |
| 895 | |
| 896 If ffap-file-at-point has a string NAME (maybe \"\") which is not an | |
| 897 existing filename, it looks for pairs with a matching KEY: | |
| 898 * if KEY is a symbol, it should equal `major-mode'. | |
| 899 * if KEY is a string, it should match NAME as a regular expression. | |
| 900 If KEY matches, ffap-file-at-point calls \(FUNCTION NAME\). | |
| 901 FUNCTION should return a file, url, or nil \(nil means keep looking | |
| 902 for more KEY matches\). Note URL's are ok despite the function name.") | |
| 903 (put 'ffap-alist 'risky-local-variable t) | |
| 904 | |
| 905 | |
| 906 ;;; At-Point Functions: | |
| 907 | |
| 908 (defvar ffap-string-at-point-mode-alist | |
| 909 '( | |
| 910 ;; Slightly controversial decisions: | |
| 911 ;; * strip trailing "@" and ":" | |
| 912 ;; * no commas (good for latex) | |
| 913 (t "--:$+<>@-Z_a-z~" "<@" "@>;.,!?:") | |
| 914 (math-mode ",-:$+<>@-Z_a-z~`" "<" "@>;.,!?`:") ; allow backquote | |
| 915 ;; Note: you are better off using "C-c C-c" in compilation buffers: | |
| 916 ;; Maybe handle "$HOME", or "$(HOME)/bin/foo" in makefile-mode? | |
| 917 ) | |
| 918 "Alist of \(MODE CHARS BEG END\), where MODE is a major-mode or t. | |
| 919 The data are arguments to ffap-string-at-point, used to guess the | |
| 920 filename at point. The `t' entry is the default.") | |
| 921 | |
| 922 (defvar ffap-string-at-point-region '(1 1) | |
| 923 "List (BEG END), last region returned by ffap-string-at-point.") | |
| 924 | |
| 925 (defvar ffap-string-at-point nil | |
| 926 ;; Added at suggestion of RHOGEE (for ff-paths), 7/24/95. | |
| 927 "Last string returned by ffap-string-at-point.") | |
| 928 (defun ffap-string-at-point (&optional chars begpunct endpunct) | |
| 929 "Return maximal string of CHARS (a string) around point. | |
| 930 Optional BEGPUNCT chars before point are stripped from the beginning; | |
| 931 Optional ENDPUNCT chars after point are stripped from the end. | |
| 932 Without arguments, uses `ffap-string-at-point-mode-alist'. | |
| 933 Also sets `ffap-string-at-point' and `ffap-string-at-point-region'." | |
| 934 (if chars | |
| 935 (let* ((pt (point)) | |
| 936 (str | |
| 937 (buffer-substring | |
| 938 (save-excursion | |
| 939 (skip-chars-backward chars) | |
| 940 (and begpunct (skip-chars-forward begpunct pt)) | |
| 941 (setcar ffap-string-at-point-region (point))) | |
| 942 (save-excursion | |
| 943 (skip-chars-forward chars) | |
| 944 (and endpunct (skip-chars-backward endpunct pt)) | |
| 945 (setcar (cdr ffap-string-at-point-region) (point)))))) | |
| 946 (set-text-properties 0 (length str) nil str) | |
| 947 (setq ffap-string-at-point str)) | |
| 948 ;; Get default args from `ffap-string-at-point-mode-alist' | |
| 949 (apply 'ffap-string-at-point | |
| 950 (cdr (or (assq major-mode ffap-string-at-point-mode-alist) | |
| 951 (assq t ffap-string-at-point-mode-alist) | |
| 952 ;; avoid infinite loop! | |
| 953 (error "ffap-string-at-point: bad alist") | |
| 954 ))))) | |
| 955 | |
| 956 (defun ffap-string-around nil | |
| 957 ;; Sometimes useful to decide how to treat a string. | |
| 958 "Return string of two characters around last ffap-string-at-point." | |
| 959 (save-excursion | |
| 960 (format "%c%c" | |
| 961 (progn | |
| 962 (goto-char (car ffap-string-at-point-region)) | |
| 963 (preceding-char)) ; maybe 0 | |
| 964 (progn | |
| 965 (goto-char (nth 1 ffap-string-at-point-region)) | |
| 966 (following-char)) ; maybe 0 | |
| 967 ))) | |
| 968 | |
| 969 (defun ffap-url-at-point nil | |
| 970 "Return URL from around point if it exists, or nil." | |
| 971 ;; Could use url-get-url-at-point instead ... how do they compare? | |
| 972 ;; Both handle "URL:", ignore non-relative links, trim punctuation. | |
| 973 ;; The other will actually look back if point is in whitespace, but | |
| 974 ;; I would rather ffap be non-rabid in such situations. | |
| 975 (and | |
| 976 ffap-url-regexp | |
| 977 (or | |
| 978 ;; In a w3 buffer button zone? | |
| 979 (let (tem) | |
| 980 (and (eq major-mode 'w3-mode) | |
| 981 ;; assume: (boundp 'w3-zone-at) (boundp 'w3-zone-data) | |
| 982 (setq tem (w3-zone-at (point))) | |
| 983 (consp (setq tem (w3-zone-data tem))) | |
| 984 (nth 2 tem))) | |
| 985 ;; Is there a reason not to strip trailing colon? | |
| 986 (let ((name (ffap-string-at-point | |
| 987 ;; Allow leading digits for email/news id's: | |
| 988 "--:?$+@-Z_a-z~#,%" "^A-Za-z0-9" ":;.,!?"))) | |
| 989 ;; (case-fold-search t), why? | |
| 990 (cond | |
| 991 ((string-match "^url:" name) (setq name (substring name 4))) | |
| 992 ((and (string-match "\\`[^:</>@]+@[^:</>@]+[a-zA-Z]\\'" name) | |
| 993 ;; "foo@bar": could be "mailto" or "news" (a Message-ID). | |
| 994 ;; If not adorned with "<>", it must be "mailto". | |
| 995 ;; Otherwise could be either, so consult `ffap-foo@bar-prefix'. | |
| 996 (let ((prefix (if (and (equal (ffap-string-around) "<>") | |
| 997 ;; At least a couple of odd characters: | |
| 998 (string-match "[$.0-9].*[$.0-9].*@" name)) | |
| 999 ;; Could be news: | |
| 1000 ffap-foo@bar-prefix | |
| 1001 "mailto"))) | |
| 1002 (and prefix (setq name (concat prefix ":" name)))))) | |
| 1003 ((ffap-newsgroup-p name) (setq name (concat "news:" name))) | |
| 1004 ((and (string-match "\\`[a-z0-9]+\\'" name) ; <mic> <root> <nobody> | |
| 1005 (equal (ffap-string-around) "<>") | |
| 1006 ;; (ffap-user-p name): | |
| 1007 (not (string-match "~" (expand-file-name (concat "~" name)))) | |
| 1008 ) | |
| 1009 (setq name (concat "mailto:" name))) | |
| 1010 ) | |
| 1011 (and (ffap-url-p name) name) | |
| 1012 )))) | |
| 1013 | |
| 1014 (defvar ffap-gopher-regexp | |
| 1015 "^.*\\<\\(Type\\|Name\\|Path\\|Host\\|Port\\) *= *\\(.*\\) *$" | |
| 1016 "Regexp Matching a line in a gopher bookmark (maybe indented). | |
| 1017 Two subexpressions are the KEY and VALUE.") | |
| 1018 | |
| 1019 (defun ffap-gopher-at-point nil | |
| 1020 "If point is inside a gopher bookmark block, return its url." | |
| 1021 ;; We could use gopher-parse-bookmark from gopher.el, but it is not | |
| 1022 ;; so robust, and w3 users are better off without gopher.el anyway. | |
| 1023 (save-excursion | |
| 1024 (beginning-of-line) | |
| 1025 (if (looking-at ffap-gopher-regexp) | |
| 1026 (progn | |
| 1027 (while (and (looking-at ffap-gopher-regexp) (not (bobp))) | |
| 1028 (forward-line -1)) | |
| 1029 (or (looking-at ffap-gopher-regexp) (forward-line 1)) | |
| 1030 (let ((type "1") name path host (port "70")) | |
| 1031 (while (looking-at ffap-gopher-regexp) | |
| 1032 (let ((var (intern | |
| 1033 (downcase | |
| 1034 (buffer-substring (match-beginning 1) | |
| 1035 (match-end 1))))) | |
| 1036 (val (buffer-substring (match-beginning 2) | |
| 1037 (match-end 2)))) | |
| 1038 (set var val) | |
| 1039 (forward-line 1))) | |
| 1040 (if (and path (string-match "^ftp:.*@" path)) | |
| 1041 (concat "ftp://" | |
| 1042 (substring path 4 (1- (match-end 0))) | |
| 1043 (substring path (match-end 0))) | |
| 1044 (and (= (length type) 1) | |
| 1045 host;; (ffap-machine-p host) | |
| 1046 (concat "gopher://" host | |
| 1047 (if (equal port "70") "" (concat ":" port)) | |
| 1048 "/" type path)))))))) | |
| 1049 | |
| 1050 (defvar ffap-ftp-sans-slash-regexp | |
| 1051 (and | |
| 1052 ffap-ftp-regexp | |
| 1053 ;; Note: by now, we know it is not an URL. | |
| 1054 ;; Icky regexp avoids: default: 123: foo::bar cs:pub | |
| 1055 ;; It does match on: mic@cs: cs:/pub mathcs.emory.edu: (point at end) | |
| 1056 ;; Todo: handle foo.com://path | |
| 1057 "\\`\\([^:@]+@[^:@]+:\\|[^@.:]+\\.[^@:]+:\\|[^:]+:[~/]\\)\\([^:]\\|\\'\\)") | |
| 1058 "Strings matching this are coerced to ftp paths by ffap. | |
| 1059 That is, ffap just prepends \"/\". Set to nil to disable.") | |
| 1060 | |
| 1061 (defun ffap-file-at-point nil | |
| 1062 "Return filename from around point if it exists, or nil. | |
| 1063 Existence test is skipped for names that look remote. | |
| 1064 If the filename is not obvious, it also tries `ffap-alist', | |
| 1065 which may actually result in an URL rather than a filename." | |
| 1066 ;; Note: this function does not need to look for URL's, just | |
| 1067 ;; filenames. On the other hand, it is responsible for converting | |
| 1068 ;; a pseudo-URL "site.dom://path" to an ftp path "/site.dom:/path" | |
| 1069 (let* ((case-fold-search t) ; url prefixes are case-insensitive | |
| 1070 (data (match-data)) | |
| 1071 (string (ffap-string-at-point)) ; use its mode-alist | |
| 1072 (name | |
| 1073 (condition-case nil | |
| 1074 (substitute-in-file-name string) | |
| 1075 (error string))) | |
| 1076 (abs (file-name-absolute-p name)) | |
| 1077 (default-directory default-directory)) | |
| 1078 (unwind-protect | |
| 1079 (cond | |
| 1080 ;; Immediate rejects (/ and // are too common in C++): | |
| 1081 ((member name '("" "/" "//")) nil) | |
| 1082 ;; Immediately test local filenames. If default-directory is | |
| 1083 ;; remote, you probably already have a connection. | |
| 1084 ((and (not abs) (ffap-file-exists-string name))) | |
| 1085 ;; Accept remote names without actual checking (too slow): | |
| 1086 ((if abs | |
| 1087 (ffap-file-remote-p name) | |
| 1088 ;; Try adding a leading "/" (common omission in ftp paths): | |
| 1089 (and | |
| 1090 ffap-ftp-sans-slash-regexp | |
| 1091 (string-match ffap-ftp-sans-slash-regexp name) | |
| 1092 (ffap-file-remote-p (concat "/" name))))) | |
| 1093 ;; Ok, not remote, try the existence test even if it is absolute: | |
| 1094 ((and abs (ffap-file-exists-string name))) | |
| 1095 ;; File does not exist, try the alist: | |
| 1096 ((let ((alist ffap-alist) tem try case-fold-search) | |
| 1097 (while (and alist (not try)) | |
| 1098 (setq tem (car alist) alist (cdr alist)) | |
| 1099 (if (or (eq major-mode (car tem)) | |
| 1100 (and (stringp (car tem)) | |
| 1101 (string-match (car tem) name))) | |
| 1102 (and (setq try (funcall (cdr tem) name)) | |
| 1103 (setq try (or | |
| 1104 (ffap-url-p try) ; not a file! | |
| 1105 (ffap-file-remote-p try) | |
| 1106 (ffap-file-exists-string try)))))) | |
| 1107 try)) | |
| 1108 ;; Alist failed? Try to guess an active remote connection | |
| 1109 ;; from buffer variables, and try once more, both as an | |
| 1110 ;; absolute and relative path on that remote host. | |
| 1111 ((let* (ffap-rfs-regexp ; suppress | |
| 1112 (remote-dir | |
| 1113 (cond | |
| 1114 ((ffap-file-remote-p default-directory)) | |
| 1115 ((and (eq major-mode 'internal-ange-ftp-mode) | |
| 1116 (string-match "^\\*ftp \\(.*\\)@\\(.*\\)\\*$" | |
| 1117 (buffer-name))) | |
| 1118 (concat "/" (substring (buffer-name) 5 -1) ":")) | |
| 1119 ;; This is too often a bad idea: | |
| 1120 ;;((and (eq major-mode 'w3-mode) | |
| 1121 ;; (stringp url-current-server)) | |
| 1122 ;; (host-to-ange-path url-current-server)) | |
| 1123 ))) | |
| 1124 (and remote-dir | |
| 1125 (or | |
| 1126 (and (string-match "\\`\\(/?~?ftp\\)/" name) | |
| 1127 (ffap-file-exists-string | |
| 1128 (ffap-replace-path-component | |
| 1129 remote-dir (substring name (match-end 1))))) | |
| 1130 (ffap-file-exists-string | |
| 1131 (ffap-replace-path-component remote-dir name)))))) | |
| 1132 ) | |
| 1133 (store-match-data data)))) | |
| 1134 | |
| 1135 | |
| 1136 ;;; ffap-read-file-or-url: | |
| 1137 ;; | |
| 1138 ;; Want to read filenames with completion as in read-file-name, but | |
| 1139 ;; also allow URL's which read-file-name-internal would truncate at | |
| 1140 ;; the "//" string. Solution here is to replace read-file-name-internal | |
| 1141 ;; with another function that does not attempt to complete url's. | |
| 1142 | |
| 1143 ;; We implement a pretty clean completion semantics to work with | |
| 1144 ;; packages like complete.el and exit-minibuffer.el. Even for | |
| 1145 ;; complete.el (v19.22), we still need to make a small patch (it has a | |
| 1146 ;; hardwired list of `minibuffer-completion-table' values which it | |
| 1147 ;; considers to deal with filenames, this ought to be a variable). | |
| 1148 | |
| 1149 (defun ffap-read-file-or-url (prompt guess) | |
| 1150 "Read a file or url from minibuffer, with PROMPT and initial GUESS." | |
| 1151 (or guess (setq guess default-directory)) | |
| 1152 (let ((filep (not (ffap-url-p guess))) dir) | |
| 1153 ;; Tricky: guess may have or be a local directory, like "w3/w3.elc" | |
| 1154 ;; or "w3/" or "../el/ffap.el" or "../../../" | |
| 1155 (if filep | |
| 1156 (progn | |
| 1157 (or (ffap-file-remote-p guess) | |
| 1158 (setq guess (abbreviate-file-name (expand-file-name guess)))) | |
| 1159 (setq dir (file-name-directory guess)))) | |
| 1160 (apply | |
| 1161 'completing-read | |
| 1162 prompt | |
| 1163 'ffap-read-file-or-url-internal | |
| 1164 dir | |
| 1165 nil | |
| 1166 (if (and dir) (cons guess (length dir)) guess) | |
| 1167 (list 'file-name-history) | |
| 1168 ))) | |
| 1169 | |
| 1170 (defvar url-global-history-completion-list nil) ; variable in w3/url.el | |
| 1171 | |
| 1172 (defun ffap-read-url-internal (string dir action) | |
| 1173 ;; Complete URL's from history, always treat given url as acceptable. | |
| 1174 (let ((hist url-global-history-completion-list)) | |
| 1175 (cond | |
| 1176 ((not action) | |
| 1177 (or (try-completion string hist) string)) | |
| 1178 ((eq action t) | |
| 1179 (or (all-completions string hist) (list string))) | |
| 1180 ;; lambda? | |
| 1181 (t string)))) | |
| 1182 | |
| 1183 (defun ffap-read-file-or-url-internal (string dir action) | |
| 1184 (if (ffap-url-p string) | |
| 1185 (ffap-read-url-internal string dir action) | |
| 1186 (read-file-name-internal string dir action))) | |
| 1187 | |
| 1188 ;; Unfortunately, for complete.el to work correctly, we need to vary | |
| 1189 ;; the value it sees of minibuffer-completion-table, depending on the | |
| 1190 ;; current minibuffer contents! It would be nice if it were written a | |
| 1191 ;; little more easily. I consider this a bug in complete.el, since | |
| 1192 ;; the builtin emacs functions do not have this problem. | |
| 1193 (and | |
| 1194 (featurep 'complete) | |
| 1195 (require 'advice) | |
| 1196 (defadvice PC-do-completion (around ffap-fix act) | |
| 1197 "Work with ffap.el." | |
| 1198 (let ((minibuffer-completion-table minibuffer-completion-table) | |
| 1199 ;; (minibuffer-completion-predicate minibuffer-completion-predicate) | |
| 1200 ) | |
| 1201 (and (eq minibuffer-completion-table 'ffap-read-file-or-url-internal) | |
| 1202 (setq minibuffer-completion-table | |
| 1203 (if (ffap-url-p (buffer-string)) | |
| 1204 ;; List would work better with icomplete ... | |
| 1205 'ffap-read-url-internal | |
| 1206 'read-file-name-internal))) | |
| 1207 ad-do-it))) | |
| 1208 | |
| 1209 | |
| 1210 ;;; Highlighting: | |
| 1211 ;; | |
| 1212 ;; Based on overlay highlighting in Emacs 19.28 isearch.el. | |
| 1213 | |
| 1214 (defvar ffap-highlight (and window-system t) | |
| 1215 "If non-nil, ffap highlights the current buffer substring.") | |
| 1216 | |
| 1217 (defvar ffap-overlay nil "Overlay used by ffap-highlight.") | |
| 1218 | |
| 1219 (defun ffap-highlight (&optional remove) | |
| 1220 "If `ffap-highlight' is set, highlight the guess in the buffer. | |
| 1221 That is, the last buffer substring found by ffap-string-at-point. | |
| 1222 Optional argument REMOVE means to remove any such highlighting. | |
| 1223 Uses the face `ffap' if it is defined, else `highlight'." | |
| 1224 (cond | |
| 1225 (remove (and ffap-overlay (delete-overlay ffap-overlay))) | |
| 1226 ((not ffap-highlight) nil) | |
| 1227 (ffap-overlay | |
| 1228 (move-overlay ffap-overlay | |
| 1229 (car ffap-string-at-point-region) | |
| 1230 (nth 1 ffap-string-at-point-region) | |
| 1231 (current-buffer))) | |
| 1232 (t | |
| 1233 (setq ffap-overlay (apply 'make-overlay ffap-string-at-point-region)) | |
| 1234 (overlay-put ffap-overlay 'face | |
| 1235 (if (internal-find-face 'ffap nil) | |
| 1236 'ffap 'highlight))))) | |
| 1237 | |
| 1238 ;;; The big enchilada: | |
| 1239 | |
| 1240 (defun ffap-guesser nil | |
| 1241 "Return file or URL or nil, guessed from text around point." | |
| 1242 (or (and ffap-url-regexp | |
| 1243 (ffap-fixup-url (or (ffap-url-at-point) | |
| 1244 (ffap-gopher-at-point)))) | |
| 1245 (ffap-file-at-point) ; may yield url! | |
| 1246 (ffap-fixup-machine (ffap-machine-at-point)))) | |
| 1247 | |
| 1248 (defun ffap-prompter (&optional guess) | |
| 1249 ;; Does guess and prompt step for find-file-at-point. | |
| 1250 ;; Extra complication just to do the temporary highlighting. | |
| 1251 (unwind-protect | |
| 1252 (ffap-read-file-or-url | |
| 1253 (if ffap-url-regexp "Find file or URL: " "Find file: ") | |
| 1254 (prog1 | |
| 1255 (setq guess (or guess (ffap-guesser))) | |
| 1256 (and guess (ffap-highlight)))) | |
| 1257 (ffap-highlight t))) | |
| 1258 | |
| 1259 ;;;###autoload | |
| 1260 (defun find-file-at-point (&optional filename) | |
| 1261 "Find FILENAME (or url), guessing default from text around point. | |
| 1262 If `ffap-dired-wildcards' is set, wildcard patterns are passed to dired. | |
| 1263 See also the functions ffap-file-at-point, ffap-url-at-point. | |
| 1264 With a prefix, this command behaves *exactly* like `ffap-file-finder'. | |
| 1265 If `ffap-require-prefix' is set, the prefix meaning is reversed. | |
| 1266 | |
| 1267 See ftp://ftp.mathcs.emory.edu/pub/mic/emacs/ for most recent version." | |
| 1268 (interactive) | |
| 1269 (if (and (interactive-p) | |
| 1270 (if ffap-require-prefix (not current-prefix-arg) | |
| 1271 current-prefix-arg)) | |
| 1272 ;; Do exactly the ffap-file-finder command, even the prompting: | |
| 1273 (call-interactively ffap-file-finder) | |
| 1274 (or filename (setq filename (ffap-prompter))) | |
| 1275 (cond | |
| 1276 ((ffap-url-p filename) | |
| 1277 (funcall ffap-url-fetcher filename)) | |
| 1278 ;; This junk more properly belongs in a modified ffap-file-finder: | |
| 1279 ((and ffap-dired-wildcards (string-match ffap-dired-wildcards filename)) | |
| 1280 (dired filename)) | |
| 1281 ((or (not ffap-newfile-prompt) | |
| 1282 (file-exists-p filename) | |
| 1283 (y-or-n-p "File does not exist, create buffer? ")) | |
| 1284 (funcall ffap-file-finder | |
| 1285 ;; expand-file-name fixes "~/~/.emacs" bug sent by CHUCKR. | |
| 1286 (expand-file-name filename))) | |
| 1287 ;; User does not want to find a non-existent file: | |
| 1288 ((signal 'file-error (list "Opening file buffer" | |
| 1289 "no such file or directory" | |
| 1290 filename)))))) | |
| 1291 | |
| 1292 ;; M-x shortcut: | |
| 1293 (fset 'ffap 'find-file-at-point) | |
| 1294 | |
| 1295 | |
| 1296 ;;; Menu support: | |
| 1297 ;; | |
| 1298 ;; Bind ffap-menu to a key if you want, since it also works in tty mode. | |
| 1299 ;; Or just use it through the ffap-at-mouse binding (next section). | |
| 1300 | |
| 1301 (defvar ffap-menu-regexp nil | |
| 1302 "*If non-nil, overrides `ffap-next-regexp' during ffap-menu. | |
| 1303 Make this more restrictive for faster menu building. | |
| 1304 For example, try \":/\" for url (and some ftp) references.") | |
| 1305 | |
| 1306 (defvar ffap-menu-alist nil | |
| 1307 "Buffer local menu of files and urls cached by ffap-menu.") | |
| 1308 (make-variable-buffer-local 'ffap-menu-alist) | |
| 1309 | |
| 1310 ;;;###autoload | |
| 1311 (defun ffap-menu (&optional rescan) | |
| 1312 "Puts up a menu of files and urls mentioned in the buffer. | |
| 1313 Sets mark, jumps to choice, and tries to fetch it. | |
| 1314 Menu is cached in `ffap-menu-alist', but will always be rebuilt | |
| 1315 with the optional RESCAN argument (a prefix interactively). | |
| 1316 Searches buffer with `ffap-menu-regexp' (see `ffap-next-regexp')." | |
| 1317 (interactive "P") | |
| 1318 ;; (require 'imenu) -- no longer used, but roughly emulated | |
| 1319 (if (or (not ffap-menu-alist) rescan | |
| 1320 ;; or if the first entry is wrong: | |
| 1321 (and ffap-menu-alist | |
| 1322 (let ((first (car ffap-menu-alist))) | |
| 1323 (save-excursion | |
| 1324 (goto-char (cdr first)) | |
| 1325 (not (equal (car first) (ffap-guesser))))))) | |
| 1326 (ffap-menu-rescan)) | |
| 1327 ;; Tail recursive: | |
| 1328 (ffap-menu-ask | |
| 1329 (if ffap-url-regexp "Find file or URL" "Find file") | |
| 1330 (cons (cons "*Rescan Buffer*" -1) ffap-menu-alist) | |
| 1331 'ffap-menu-cont)) | |
| 1332 | |
| 1333 (defun ffap-menu-cont (choice) ; continuation of ffap-menu | |
| 1334 (if (< (cdr choice) 0) | |
| 1335 (ffap-menu t) ; *Rescan* | |
| 1336 (push-mark) | |
| 1337 (goto-char (cdr choice)) | |
| 1338 ;; Momentary highlight: | |
| 1339 (unwind-protect | |
| 1340 (progn | |
| 1341 (and ffap-highlight (ffap-guesser) (ffap-highlight)) | |
| 1342 (sit-for 0) ; display | |
| 1343 (find-file-at-point (car choice))) | |
| 1344 (ffap-highlight t)))) | |
| 1345 | |
| 1346 (defun ffap-menu-ask (title alist cont) | |
| 1347 "Prompt from a menu of choices, and then apply some action. | |
| 1348 Arguments are TITLE, ALIST, and CONT (a continuation). | |
| 1349 This uses either a menu or the minibuffer depending on invocation. | |
| 1350 The TITLE string is used as either the prompt or menu title. | |
| 1351 Each (string . data) entry in ALIST defines a choice (data is ignored). | |
| 1352 Once the user makes a choice, function CONT is applied to the entry. | |
| 1353 Always returns nil." | |
| 1354 ;; Bug: minibuffer prompting assumes the strings are unique. | |
| 1355 ;; Todo: break up long menus into multiple panes (like imenu). | |
| 1356 (let ((choice | |
| 1357 (if (and (fboundp 'x-popup-menu) ; 19 or XEmacs 19.13 | |
| 1358 (boundp 'last-nonmenu-event) ; not in XEmacs 19.13 | |
| 1359 (listp last-nonmenu-event)) | |
| 1360 (x-popup-menu | |
| 1361 t | |
| 1362 (list "" | |
| 1363 (cons title | |
| 1364 (mapcar | |
| 1365 (function (lambda (i) (cons (car i) i))) | |
| 1366 alist)))) | |
| 1367 ;; Automatically popup completion help, one way or another: | |
| 1368 (let ((minibuffer-setup-hook 'minibuffer-completion-help) | |
| 1369 (unread-command-char -1)) | |
| 1370 ;; BUG: this code assumes that "" is not a valid choice | |
| 1371 (completing-read | |
| 1372 (format "%s (default %s): " title (car (car alist))) | |
| 1373 alist nil t | |
| 1374 ;; Let first be default: | |
| 1375 ;; (if ffap-v18 (car (car alist)) | |
| 1376 ;; (cons (car (car alist)) 0)) | |
| 1377 ;; No, then you do not get all completions! | |
| 1378 nil | |
| 1379 ))))) | |
| 1380 ;; Defaulting: convert "" to (car (car alist)) | |
| 1381 (and (equal choice "") (setq choice (car (car alist)))) | |
| 1382 (and (stringp choice) (setq choice (assoc choice alist))) | |
| 1383 (if choice (funcall cont choice) (message "No choice made!"))) | |
| 1384 nil) ; return nothing | |
| 1385 | |
| 1386 (defun ffap-menu-rescan nil | |
| 1387 (interactive) | |
| 1388 (let ((ffap-next-regexp (or ffap-menu-regexp ffap-next-regexp)) | |
| 1389 (range (- (point-max) (point-min))) item) | |
| 1390 (setq ffap-menu-alist nil) | |
| 1391 (save-excursion | |
| 1392 (goto-char (point-min)) | |
| 1393 (while (setq item (ffap-next-guess)) | |
| 1394 (setq ffap-menu-alist (cons (cons item (point)) ffap-menu-alist)) | |
| 1395 (message "Scanning...%2d%% <%s>" | |
| 1396 (/ (* 100 (- (point) (point-min))) range) item)))) | |
| 1397 (message "Scanning...done") | |
| 1398 ;; Remove duplicates. | |
| 1399 (setq ffap-menu-alist ; sort by item | |
| 1400 (sort ffap-menu-alist | |
| 1401 (function | |
| 1402 (lambda (a b) (string-lessp (car a) (car b)))))) | |
| 1403 (let ((ptr ffap-menu-alist)) | |
| 1404 (while (cdr ptr) | |
| 1405 (if (equal (car (car ptr)) (car (car (cdr ptr)))) | |
| 1406 (setcdr ptr (cdr (cdr ptr))) | |
| 1407 (setq ptr (cdr ptr))))) | |
| 1408 (setq ffap-menu-alist ; sort by position | |
| 1409 (sort ffap-menu-alist | |
| 1410 (function | |
| 1411 (lambda (a b) (< (cdr a) (cdr b))))))) | |
| 1412 | |
| 1413 | |
| 1414 ;;; Mouse Support: | |
| 1415 ;; | |
| 1416 ;; I suggest a mouse binding, something like: | |
| 1417 ;; (global-set-key [S-mouse-1] 'ffap-at-mouse) | |
| 1418 | |
| 1419 (defvar ffap-at-mouse-fallback 'ffap-menu | |
| 1420 "Invoked by ffap-at-mouse if no file or url found at point. | |
| 1421 A command symbol, or nil for nothing.") | |
| 1422 (put 'ffap-at-mouse-fallback 'risky-local-variable t) | |
| 1423 | |
| 1424 (defun ffap-at-mouse (e) | |
| 1425 "Find file or URL guessed from text around mouse point. | |
| 1426 If none is found, call `ffap-at-mouse-fallback'." | |
| 1427 (interactive "e") | |
| 1428 (let ((guess | |
| 1429 ;; Maybe less surprising without the save-excursion? | |
| 1430 (save-excursion | |
| 1431 (mouse-set-point e) | |
| 1432 ;; Would like to do nothing unless click was *on* text. How? | |
| 1433 ;; (cdr (posn-col-row (event-start e))) is always same as | |
| 1434 ;; current column. For posn-x-y, need pixel-width! | |
| 1435 (ffap-guesser)))) | |
| 1436 (cond | |
| 1437 (guess | |
| 1438 (ffap-highlight) | |
| 1439 (unwind-protect | |
| 1440 (progn | |
| 1441 (sit-for 0) ; display | |
| 1442 (message "Guessing `%s'" guess) | |
| 1443 (find-file-at-point guess)) | |
| 1444 (ffap-highlight t))) | |
| 1445 ((and (interactive-p) | |
| 1446 ffap-at-mouse-fallback) | |
| 1447 (call-interactively ffap-at-mouse-fallback)) | |
| 1448 ((message "No file or URL found at mouse click."))))) | |
| 1449 | |
| 1450 | |
| 1451 ;;; ffap-other-* commands | |
| 1452 ;; Suggested by KPC. Possible bindings for C-x 4 C-f, C-x 5 C-f. | |
| 1453 | |
| 1454 (defun ffap-other-window nil | |
| 1455 "Like ffap, but put buffer in another window." | |
| 1456 (interactive) | |
| 1457 (switch-to-buffer-other-window | |
| 1458 (save-window-excursion (call-interactively 'ffap) (current-buffer)))) | |
| 1459 | |
| 1460 (defun ffap-other-frame nil | |
| 1461 "Like ffap, but put buffer in another frame." | |
| 1462 (interactive) | |
| 1463 (switch-to-buffer-other-frame | |
| 1464 (save-window-excursion (call-interactively 'ffap) (current-buffer)))) | |
| 1465 | |
| 1466 | |
| 1467 ;;; ffap-bug: | |
| 1468 (defun ffap-bug nil | |
| 1469 ;; Tested with Emacs 19.28 reporter.el | |
| 1470 "Submit a bug report for ffap." | |
| 1471 (interactive) | |
| 1472 (require 'reporter) | |
| 1473 (let ((reporter-prompt-for-summary-p t)) | |
| 1474 (reporter-submit-bug-report | |
| 1475 "mic@mathcs.emory.edu" "ffap " | |
| 1476 (mapcar 'intern (all-completions "ffap-" obarray 'boundp)) | |
| 1477 ))) | |
| 1478 (fset 'ffap-submit-bug 'ffap-bug) ; another likely name | |
| 1479 | |
| 1480 | |
| 1481 ;;; Todo, End. | |
| 1482 ;; | |
| 1483 ;; * w3 may eventually make URL's part of the filesystem! | |
| 1484 ;; this package (prompt & completion) could become much simpler | |
| 1485 ;; * improve minibuffer-completion-help display of long completions | |
| 1486 ;; * notice "machine.dom blah blah blah path/file" (how?) | |
| 1487 ;; * check X selections (x-get-selection PRIMARY/SECONDARY LENGTH/TEXT) | |
| 1488 ;; * let "/path/file#key" jump to key (anchor or regexp) in /path/file | |
| 1489 ;; * notice node in "(dired)Virtual Dired" (how to handle space?) | |
| 1490 ;; * try find-tag on symbol if TAGS is loaded (need above) | |
| 1491 ;; | |
| 1492 ;; For information on URL/URI syntax, try: | |
| 1493 ;; <http://ds.internic.net/rfc/rfc1630.txt> | |
| 1494 ;; <http://www.w3.org/hypertext/WWW/Protocols/Overview.html> | |
| 1495 ;; <http://info.cern.ch/hypertext/WWW/Addressing/Addressing.html> | |
| 1496 | |
| 1497 ;; Local Variables? | |
| 1498 ;; foo: bar | |
| 1499 ;; End: | |
| 1500 | |
| 1501 | |
| 1502 ;;; ffap.el ends here |
