[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#32029: [PATCH] xref-find-definitions-at-mouse

From: Tobias Gerdin
Subject: bug#32029: [PATCH] xref-find-definitions-at-mouse
Date: Tue, 3 Jul 2018 23:37:23 +0200
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.8.0

Den 2018-07-03 kl. 15:15, skrev Dmitry Gutov:

On 7/2/18 2:18 AM, Tobias Gerdin wrote:

+(defun xref-find-definitions-at-mouse (event)
+  "Find the definition of identifier around mouse click."
+  (interactive "e")
+  (let* ((backend (xref-find-backend))
+     (identifier (save-excursion
+              (mouse-set-point event)
+              (xref-backend-identifier-at-point backend))))
+    (if identifier
+    (xref--find-definitions identifier nil)
+      (user-error "No identifier here"))))

You should call `mouse-set-point` before `xref-find-backend`, because the latter might conceivably depend on the value of point.

I think this can be written much shorter (call mouse-set-point, then interactively call xref-find-definitions), but I'm not sure how.
A new version below. The initial version was modeled after `ffap-at-point`. Make using of call-interactively would be neat, but since we want to avoid making the call to xref-find-definitions inside the body of save-excursion I do not see how either (unless one woud actually move the point which I find a bit aggressive). I am not an overly experienced Elisp programmer so if you happen to come up with a way to do this I'm all ears. If not the below would do the job until then.

diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 9a437b6f69..befebbb426 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -873,6 +873,16 @@ With prefix argument, prompt for the identifier."
   (interactive (list (xref--read-identifier "Find references of: ")))
   (xref--find-xrefs identifier 'references identifier nil))

+(defun xref-find-definitions-at-mouse (event)
+  "Find the definition of identifier around mouse click."
+  (interactive "e")
+  (if-let ((identifier (save-excursion
+             (mouse-set-point event)
+             (xref-backend-identifier-at-point (xref-find-backend)))))
+      (xref-find-definitions identifier)
+    (user-error "No identifier here")))
 (declare-function apropos-parse-pattern "apropos" (pattern))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]