# Get symbol at mouse position

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.

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

Here's an attempt at something like this:

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

Unprotect@FECacheTemplateAndUsage;
FECacheTemplateAndUsage[sym_String] /; Context@arg == "Global" :=
(
\$sym = sym;
FrontEndCacheTemplateAndUsagePacket[FrontEndInputNotebook[], 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.
• Clever usage of FECacheTemplateAndUsage! I would never have thought to intercept that – b3m2a1 Feb 2 at 21:05
• 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? – Qbyte Feb 2 at 22:09
• 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. – Qbyte Feb 2 at 22:13
• @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. – Lukas Lang Feb 2 at 22:48
• 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) – Lukas Lang Feb 2 at 22:48