1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
;;; -*- Mode: Emacs-Lisp -*-
;;; ilisp-mouse.el --
;;;
;;; This file is part of ILISP.
;;; Please refer to the file COPYING for copyrights and licensing information.
;;; Please refer to the file ACKNOWLEGDEMENTS for an (incomplete) list
;;; of present and past contributors.
;;; Unlike most other ilisp source files, ilisp-mouse is meant to be loadable by
;;; itself, in case you want to click M-left on a C definition name or URL, for
;;; which you don't need any of the Lisp support machinery. It also works with
;;; Franz Inc's "eli" interface to ACL via emacs.
;;;
;;; To use this independently of ilisp, ensure that this directory is on your
;;; search path, and put the following in your .emacs file:
;;;
;;; (require 'ilisp-mouse)
;;; ;; Need to nuke the 'down' event binding, or the 'up' gets swallowed.
;;; (global-set-key [M-down-mouse-1] nil)
;;; (global-set-key [M-mouse-1] 'ilisp-mouse-edit-thing)
;;;
;;; Meta-Left (aka Alt-Left aka M-mouse-1) is the traditional binding, but be
;;; aware that many window managers intercept this gesture to iconify the
;;; window, or some other stupid thing. If you find this is the case, you can
;;; either tell the WM not to do that, or pick another mouse gesture. (You
;;; should also be aware that Emacs by default uses M-mouse-1 to manipulate the
;;; secondary selection.)
;;;
;;; When this file is loaded without ilisp (or eli), of course, it can't use the
;;; Lisp to find Common Lisp definitions. However, starting a Lisp session via
;;; ilisp will automagically restore this functionality.
;;;
;; tags package fns are used by ilisp-edit-thing and ilisp-thing-around-point.
;; [I'd like to condition (require 'tags) on whether we actually need it, but
;; the funcall in ilisp-edit-thing is problematic. -- rgr, 12-Apr-94.]
(cond ((string-match "^Lucid" (emacs-version))
;; In lemacs, the "tags" feature is provided by the "etags" file.
(or (memq 'tags features)
(load "etags")))
;; End [arguable] of braindeath. -- rgr, 26-Oct-94.
((string-match emacs-version "^18\\.") (require 'tags))
(t (require 'etags)))
;;;; Variables and macros.
(defvar ilisp-mouse-use-ange-ftp-p t
"*Whether to use ange-ftp to open 'FILE:' and 'FTP:' URLs in emacs
when you mouse on them; the default is 'yes'.")
(defvar ilisp-url-regexp
"^<url:\\|^https?:\\|^gopher:\\|^telnet:\\|^wais:\\|^s?news:\\|^mailto:"
"*Matches URL's to be passed on to the browse-url machinery.
Set this to nil to prevent \\[ilisp-mouse-edit-thing] from attempting to
interpret URLs. Note that this doesn't match ftp: or file: URL's, so
they if you let this match ftp: or file: URL's, then you'll wind up
looking at them with Netscape, or whatever.")
(defmacro with-lisp-syntax (&rest body)
"Helper for functions (mostly mouse commands) that want Lisp syntax in
arbitrary buffers."
;; But keep TeX-mode syntax, so that clicking c-Middle on "{" gets the whole
;; environment. -- rgr, 27-Sep-94.
(let ((tmst (if (string-match emacs-version "^18\\.")
'TeX-mode-syntax-table
'tex-mode-syntax-table)))
` (let ((old-syntax-table (syntax-table)))
(unwind-protect
(progn
(if (not (and (boundp (quote ,tmst))
(eq old-syntax-table
(symbol-value (quote ,tmst)))))
;; (not (memq major-mode '(TeX-mode LaTeX-mode)))
(set-syntax-table
(or lisp-mode-syntax-table
;; lmst not defined until lisp-mode executed
emacs-lisp-mode-syntax-table)))
,@body)
(set-syntax-table old-syntax-table)))))
;;;; Code.
(defun ilisp-mouse-snarf-sexp-after-point (&optional end)
;; Common idiom.
(while (looking-at "\\s'")
(forward-char 1))
(buffer-substring-no-properties (point)
(or end
(progn
(forward-sexp 1)
(point)))))
(defun ilisp-thing-around-point ()
"Finds an interesting editable thing around point. This includes file
names, URLs, emacs lisp and Common Lisp definition names, and
identifiers in other languages that may be findable via\\[find-tag].
Recognizes ange-ftp and Lispm pathname syntax and expands pathnames so
that relativity works, but leaves URLs alone. Uses find-tag-default
and/or ffap-file-at-point but hacks the syntax table, since the default
text table doesn't like \".\", \"~\", and other constituent chars.
Note: find-tag is an obsolete function (as of 25.1);
use ‘xref-find-definitions’ instead"
(let* ((ffap-guess nil)
(thing (with-lisp-syntax
;; Special cases for clicking on an S-expression, which could
;; be Common Lisp definition names.
(cond ((eq (char-after) ?\()
(save-excursion
(ilisp-mouse-snarf-sexp-after-point)))
((eq (char-after) ?\))
(save-excursion
(forward-char 1)
(let ((end (point)))
(forward-sexp -1)
(ilisp-mouse-snarf-sexp-after-point end))))
((and (fboundp 'ffap-guesser)
;; this will return an existing file name or URL,
;; or nil. remember what we got, so we don't
;; second-guess the guesser.
(setq ffap-guess (ffap-guesser))))
(t (find-tag-default))))))
(cond ((or (not (stringp thing))
(eq (aref thing 0) ?\())
;; Thing is sometimes null; this happens (e.g.) in empty buffers.
thing)
(ffap-guess)
((and (or (file-name-absolute-p thing)
(= (aref thing 0) ?.))
(file-exists-p thing))
;; The old test just looked at the first character; this is lots
;; more expensive, but much more versatile. It's equivalent to the
;; much cleverer find-file-at-point algorithm. We still check the
;; first char anyway, because file-exists-p can be too expensive for
;; ange-ftp pathnames. And we must expand the file name here, in
;; order to handle relative pathnames correctly. -- rgr, 12-Sep-96.
(expand-file-name thing))
((and ilisp-url-regexp
(string-match ilisp-url-regexp thing))
;; Probably a URL; don't try to mung it. [better still, go back and
;; look again, since lisp syntax drops the #tag syntax. -- rgr,
;; 12-Sep-96.] [and *doesn't* do <url:thing> right, since this is
;; allowed to contain whitespace, which is supposed to be
;; eliminated. -- rgr, 3-Jan-00.]
(require 'browse-url)
(let ((better-thing (browse-url-url-at-point)))
(if (or (> (length better-thing) (length thing))
(string-match "^<url" thing))
better-thing
thing)))
((and ilisp-mouse-use-ange-ftp-p
(string-match "\\`\\(ftp\\|file\\)://\\([^:/]+\\):?\\(/.*\\)"
thing))
;; Convert URL-style ftp: or file: references to ange-ftp syntax.
;; Taken from ffap-fixup-url and ffap-host-to-path fns. -- rgr,
;; 22-Mar-95.
(require 'ange-ftp)
(let ((host (match-string 2 thing))
(rest (match-string 3 thing)))
(cond ((equal host "localhost") rest)
((string-match "@" host)
(concat "/" host ":" rest))
(t
(concat "/" (or ange-ftp-default-user "anonymous")
"@" host ":" rest)))))
((string-match "^\\([-a-zA-Z0-9._]+:\\)[~/.]" thing)
;; Looks like Lispm host:pathname syntax; the "~", "/", or "." means
;; it's probably not a package symbol. We rely on the fact that
;; Lispm syntax is a prefix of ange-ftp /user@host:pathname syntax.
;; [but downcase the host name because they are case-sensitive to
;; ange-ftp. -- rgr, 1-Feb-95.]
(expand-file-name
(concat "/" (user-login-name) "@"
(downcase (substring thing 0 (match-end 1)))
(substring thing (match-end 1)))))
(t
thing))))
(defvar lisp-definition-finders
'((edit-definitions-lisp ilisp-buffer)
(fi:lisp-find-definition fi::lep-open-connection-p))
"This is a search list of (definition-finder tester). We pick the
first entry for which the definition-finder symbol is defined in emacs
\(autoloading counts\), and either has a null tester function, or the
tester function returns non-nil.")
(defun ilisp-edit-function-spec (thing &optional prefer-lisp-p)
;; Given a string that is possibly a Lisp definition name, decide whether to
;; use find-tag or something more Lisp-competent such as edit-definitions-lisp
;; or fi:lisp-find-definition to find it. (Really we should try to find
;; whatever the current lisp-mode uses. But ilisp and LEP are too different
;; to make this clean & general.) [haven't figured out all of the package
;; issues for finding things in non-Lisp buffers. -- rgr, 19-May-00.]
(let ((edit-fn (key-binding "\M-.")))
(if (or (null edit-fn) ;; shoudn't happen.
(and (eq edit-fn 'find-tag)
(or prefer-lisp-p
;; could be "(method icc:draw-part (icc::scratch-dot t))",
;; or something like that, which fi:lisp-find-definition
;; and edit-definitions-lisp know how to deal with.
(and (string-match "[():]" thing)
;; eliminate "fi:random-elisp-fn" case.
(let ((symbol (intern-soft thing)))
(not (and symbol
(or (boundp symbol)
(fboundp symbol)))))))))
;; search for an inferior lisp definition finder if given a symbol that
;; is obviously Common Lisp, regardless of what the mode M-. might be.
(let ((tail lisp-definition-finders))
(while tail
(let* ((entry (car tail))
(fn (if (consp entry) (car entry) entry))
(tester (if (consp entry) (car (cdr entry)) nil)))
(if (and (fboundp fn)
(or (null tester)
(condition-case ignore (funcall tester)
(error nil))))
(setq edit-fn fn
tail nil)
(setq tail (cdr tail)))))))
;; (message "Using %S to edit %S." edit-fn thing)
;; assume find-tag compatibility.
(funcall edit-fn thing)))
;;;###autoload
(defun ilisp-edit-thing (thing)
"Like the Symbolics ed function: figures out how to edit thing generically."
;; The rules are different for GNU emacs, though, since emacs doesn't handle
;; #P, etc.
'(message "Got %S" thing)
(cond ((null thing) nil)
((symbolp thing)
;; emacs function/variable
(xref-find-definitions (symbol-name thing)))
((or (not (stringp thing)) (equal thing ""))
(error "Don't know how to edit %S." thing))
((or (file-name-absolute-p thing)
;; Weak pathname heuristic.
(= (aref thing 0) ?.))
;; Like find-file, but invokes dired if thing has wildcards. Note
;; that find-file is smart enough to enter dired if given a directory.
(if (string-match "^$\\|[[*?]" (file-name-nondirectory thing))
(dired thing)
(find-file thing)))
((and ilisp-url-regexp
(string-match ilisp-url-regexp thing))
(require 'browse-url)
(funcall browse-url-browser-function thing))
(t
(ilisp-edit-function-spec thing))))
;;; Mouse events.
;; [Oops; this is specific to fsf, v19 and later . . . -- rgr, 8-Apr-03.]
;;;###autoload
(defun ilisp-mouse-edit-thing (event)
"Find the source files for the thing under the mouse.
If it looks like a pathname, then do find-file or dired on it.
If it looks like a definition name, then do M-.
Whatever it is, it is found in the current window, regardless of where
you click. 'Looks like a pathname' means it starts with '.', '/', or
'~', or (as in a Lispm pathname) has a host: prefix followed by one of
these three characters. Lispm pathnames are converted to ange-ftp
pathnames, which generally works, though only for Unix syntax."
(interactive "e")
(ilisp-edit-thing (save-excursion
(save-window-excursion
(mouse-set-point event)
(ilisp-thing-around-point)))))
(provide 'ilisp-mouse)
;; some test data:
;; http://rgrjr.dyndns.org/linux/howto.html
;; http://bmerc-www.bu.edu/
;; mailto:rogers@rgrjr.dyndns.org
;; ftp://huxley.bu.edu/~rogers/queue
;; ./ilisp-mouse.el
;; /etc/passwd
;; file:///etc/passwd [but ffap doesn't rewrite this as a local pathname]
;; file://localhost/etc/passwd
;;; end of file -- ilisp-mouse.el --
|