3
$\begingroup$

How can I get the symbol which is under the mouse pointer at any time?

I know that I can get the mouse position with MousePosition[] and dynamically with Dynamic[MousePosition[]]. However I was not able to find a way to get the symbol at this position.

My final goal is to create a second notebook which always shows the current value of the symbol which is under the mouse pointer.

$\endgroup$
2
  • $\begingroup$ Is this the sort-of thing you have in mind? a = 4; Mouseover[HoldForm[a], a] ? $\endgroup$ – George Varnavides Jan 30 at 23:55
  • $\begingroup$ @GeorgeVarnavides Unfortunately not. I would like it to work without Mouseover. Currently I'm also looking into SelectionMove. $\endgroup$ – Qbyte Jan 31 at 0:04
5
$\begingroup$

Here's an attempt at something like this:

https://i.imgur.com/5rOeOzK.gif

Dynamic@Row@{$sym, " = ", Symbol@$sym}

Unprotect@FE`CacheTemplateAndUsage;
FE`CacheTemplateAndUsage[sym_String] /; Context@arg == "Global`" :=
 (
  $sym = sym;
  MathLink`CallFrontEnd@
   FrontEnd`CacheTemplateAndUsagePacket[FrontEnd`InputNotebook[], sym,
     Null]
  )

The symbol name and its value is displayed in the top cell after hovering over a symbol for a few seconds. Some notes:

  • This is using undocumented functionality, so use it at your own risk (it might also break at any moment)
  • The idea is to intercept the call the front-end makes to get the usage information for a symbol. After this is successful once, it will never be done again, so we need to make sure to always return an empty usage
  • Currently it is limited to symbols in the global context, since for system symbols you probably want to see the info dropdown
  • The front-end needs to think that a value is assigned to the symbol (or something like that, not quite sure on the specifics). Usually any symbol with a definition should fall into this category, but in some circumstances the front-end is not aware of existing symbol definitions.
$\endgroup$
5
  • $\begingroup$ Clever usage of FE`CacheTemplateAndUsage! I would never have thought to intercept that $\endgroup$ – b3m2a1 Feb 2 at 21:05
  • $\begingroup$ I also had the same idea but I did not find the right functions to override. Did you use LinkSnooper? Can you also add the code to your text for easy copy pasting, please? $\endgroup$ – Qbyte Feb 2 at 22:09
  • $\begingroup$ This is a great answer. However it is unfortunate that we lose the usage documentation. I don't think that writing a custom code completion overlay is worth the effort?! (suggestions are welcome). Currently I'm using code selection and Dynamic for my purposes. $\endgroup$ – Qbyte Feb 2 at 22:13
  • $\begingroup$ @Qbyte Completely forgot about the code, sorry! I have added it now. And yes, I used LinkSnooper for this - I remembered seeing something in there when hovering over symbols, from there it was quite easy to find the exact call. $\endgroup$ – Lukas Lang Feb 2 at 22:48
  • 1
    $\begingroup$ As for the documentation: Yes, that's unfortunately a side-effect of this approach, and seems impossible to fix (essentially, you only get the call if the front-end doesn't know the usage, and you can have the popup if the front-end doesn't know the usage). You might be able to use ResourceFunction["InformationPopup"] to recover the lost functionality however (or at least something comparable) $\endgroup$ – Lukas Lang Feb 2 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.