Defining new tooltips in Emacs - emacs

I would like to add custom tooltips to emacs.
More specifically, whenever I hover on a symbol (function/variable) name
with my mouse of I would like to see a tooltip with the symbol's definition.
I know that I can find this kind of info with a tool like cscope but I have no idea how
to attach the output of cscope to a tooltip.
does anyone have a partial (how to link a callback to a tooltip in emacs in general) or
a full (how do I actually link the output of cscope to a tooltip) solution to this?
Thanks,
Nir

Your Emacs installation should include the Elisp reference manual (if not, download it now - you're going to need it when developing your mode). To access it, go to Info (C-h i) and look for a node labeled "Elisp", sometimes in a separate "Emacs" menu. Type i for index and tooltip to look for information on tooltips. You should find node 32.19.4 Properties with Special Meanings, which tells you that the content of the help-echo property is a string that is the tooltip content, or a function that can construct the tooltip dynamically. Explore the manual around that node to find out more about text properties and how to set them.
Here's a simple example:
(insert (propertize "foo\n" 'help-echo "Tooltip!"))
Type this into your *scratch* buffer and press C-j to run the code. Then point your mouse at the word "foo" and you should see the tooltip.

There is an AutoOverlay package that can automatically add overlays, and tooltips associated with those overlays, based on a regex match of the buffer text.

Related

Defining properties (docstrings) for Emacs key bindings

I [currently] don't use the menu feature for want of screen real estate, so it's possible the answer lies there.
Is there any way to associate a docstring with a keybinding such that it'll show up in the C-c C-h (or related) help displays? Right now all I get is either Prefix Command or the name of the function/lambda to which the key is bound. I'd like to have something about the purpose of the prefixed keymap for the former, and explanatory text for the more obscure functions for the latter.
Yes, I know that clicking on the function name will bring up a help window with details; I'm just looking for a one-liner property that can be shown in the original *Help* pane without disrupting the screen layout with new panes.
I suspect the answer is 'no,' but it seems a fairly obvious feature..
Thanks!

Key binding to change the face of a character

I'm curious as to whether or not there's a simple way to edit the face of a block of text using a function.
Specifically, I'm working on a calendar major mode that I created, and I want to define a function that will be bound to a keystroke within this major mode. I want to be able to mark a group of text, and then change its face using this keybinding to mark it as "done", etc.
I did some research and wasn't able to find this exact issue. Also, I know I can basically do this exact thing in org-mode, but I really want to create my own mode to enable more flexibility on my end.
If you are using the 3-month mini-calendar as a basis to create your major-mode, then there is already a facility to do this. To see how this looks like, type M-x calendar and then M-x calendar-mark-holidays. To see how this works, you can type M-x find-function RET calendar-mark-holidays RET and see that it uses the function calendar-mark-visible-date -- so there you have it, that is your function of interest. Drew Adams has a fancier calendar mark date that has some additional options: https://www.emacswiki.org/emacs/calendar%2B.el . These markings are overlays and can be controlled with priorities to have one supersede another so that it is not actually necessary to remove a color unless you want to.
Here is a quick example of a keyboard shortcut/function that uses the F5 key: (define-key calendar-mode-map [f5] (lambda () (interactive) (calendar-mark-visible-date (calendar-cursor-to-date 'signal-error) '(:background "yellow" :foreground "black"))))
Sounds like ad hoc, not syntax, highlighting: You want to manually choose specific arbitrary text to highlight, and you want to choose the highlighting face to use for this or that chosen bit of text.
You can use library Highlight (highlight.el) to do that.
Command hlt-choose-default-face chooses the face to use for subsequent highlighting (it reads a face name or a color name, with completion).
Another way to choose the highlighting face is to use command
hlt-next-face or hlt-previous-face. These cycle among a
smaller set of faces and background colors, the elements in the
list value of option hlt-auto-face-backgrounds. You can use a
numeric prefix argument with these commands to choose any of the
elements by its absolute position in the list.
There are commands (e.g., hlt-highlight) to highlight or unhighlight the active region, or you can drag the mouse to highlight (or unhighlight). By default they use the last default face you chose.
For persistent highlighting, see Temporary or Permanent Highlighting.

Change default EMACS mouse highlight behaviour

In EMACS the default seems to be to 'copy' any text highlighted with the mouse. I'm attempting to replicate the functionality of modern text editors, where I can highlight a section of text and press 'paste' to replace it. I have so far added
(delete-selection-mode 1)
to my init.el
The problem is, if I copy something, then highlight to paste in its place, I end up pasting what I had just highlighted, changing nothing.
What do I need to change to fix that behaviour?
The most powerful element of emacs is its introspection features, lets have a look at how we can use them to try and solve this problem. We must use the power of the source.
One of the most important tools for introspection in emacs is the describe-key command which is bound to C-h k. It brings up the documentation of whatever keystroke is called after it. So in our case if we press C-h k and then click and drag we will see the documentation for <down-mouse-1> and more importantly for <drag-mouse-1>. The documentation states that "<drag-mouse-1> at that spot runs the command mouse-set-region". Below it then gives some documentation for this command. It says
Set the region to the text dragged over, and copy to kill ring.
This should be bound to a mouse drag event.
See the ‘mouse-drag-copy-region’ variable to control whether this
command alters the kill ring or not.
Now we know that somehow mouse-drag-copy-region controls whether or not the highlighted text is copied.
If we follow the link of that variable it tells us the default value and some documentation:
If non-nil, copy to kill-ring upon mouse adjustments of the region.
Now all we have to do is set the variable to be nil to get the effect that you want. Place the following code at the end of your init file and you should be all set
(setq mouse-drag-copy-region nil)
I hope that this helps you with this problem and that more importantly it helps you with further issues.
By default, selecting a region with the mouse does not copy the text to the kill ring. If your Emacs does this, you probably have set the variable mouse-drag-copy-region.
In a fresh Emacs (24.5 started using -Q), you can do the following:
Start delete-selection-mode.
Mark a region using the mouse. Copy it using M-w.
Mark a second region. Replace it with the first using C-y.
I see two alternatives, neither of which does exactly what you request. (For both, yes, turn on delete-selection-mode.)
Use the secondary selection for the text to copy, and use the primary selection (the region) for the text to be replaced.
You copy text into the secondary selection using the Meta key plus the mouse - for example, press and hold Meta (the Alt key, usually) while dragging or double-clicking mouse-1.
You paste the secondary selection using Meta plus mouse-2.
Select text with the mouse, then copy it to the kill-ring using M-w. Then select the text to replace with the mouse and use C-y to paste the copied text to replace it.

show repeated selected text in emacs

I don't know which text editor have it but when you select text it show up where the same text appear in text with a box. It's usefull when you select variable and it show up where it's use in the code.
The text can show up with different backgorund when I copy the text and then call the function and dissapear when position of the cursor change.
Is it possible to do this in Emacs (probably is but how)?
Yes, Emacs has that (highlight matches of a given symbol or other pattern). Sounds like any of these correspond to what you are looking for:
Incremental search (aka isearch): C-s or C-M-s, then type what you want to match (or use C-w... to pick it up from the buffer).
Library highlight-symbol.
Library highlight, command hlt-highlight-symbol. (And see option `hlt-auto-faces-flag.) Does what library highlight-symbol does, and more.
Emacs 24.4 (i.e., current development snapshot), command hi-lock-face-symbol-at-point.
If you use library mouse3.el then right-clicking the mouse gives you the last two alternatives for the symbol under the mouse pointer.
You can maybe try to use cedet with ECB on Emacs.
You can install it with Meta+x list-packages or you can try another IDE like Kdevelop.

Code folding for LaTeX in Emacs

Is there an Emacs minor-mode (or piece of elisp code) that lets you selectively hide/show environments while in LaTeX mode? For instance, I would like to move to the beginning of a long \begin{figure} block, hit a keystroke, and have the contents of that figure environment hidden from view. Similarly with \begin{proof} and so on, and ideally even with \subsections.
Is this possible? I just tried hs-minor-mode, allout-mode, and outline-minor-mode, but most of them don't recognize LaTeX's environments, e.g. hs-minor-mode fails with "scan error: unbalanced parentheses". I would prefer not to have to enter explicit folding marks like {{{ as in folding-mode.
[Ideally it would be great if the folding were persistent, but I see that that question doesn't have an accepted answer yet.]
AUCTeX does folding: http://www.gnu.org/software/auctex/manual/auctex.html#Folding
A popular complaint about markup languages like TeX and LaTeX is that there is too much clutter in the source text and that one cannot focus well on the content. There are macros where you are only interested in the content they are enclosing, like font specifiers where the content might already be fontified in a special way by font locking. Or macros the content of which you only want to see when actually editing it, like footnotes or citations. Similarly you might find certain environments or comments distracting when trying to concentrate on the body of your document.
With AUCTeX’s folding functionality you can collapse those items and replace them by a fixed string, the content of one of their arguments, or a mixture of both. If you want to make the original text visible again in order to view or edit it, move point sideways onto the placeholder (also called display string) or left-click with the mouse pointer on it. (The latter is currently only supported on Emacs.) The macro or environment will unfold automatically, stay open as long as point is inside of it and collapse again once you move point out of it. (Note that folding of environments currently does not work in every AUCTeX mode.)
In order to use this feature, you have to activate TeX-fold-mode which will activate the auto-reveal feature and the necessary commands to hide and show macros and environments. You can activate the mode in a certain buffer by typing the command M-x TeX-fold-mode RET or using the keyboard shortcut C-c C-o C-f. If you want to use it every time you edit a LaTeX document, add it to a hook:
(add-hook 'LaTeX-mode-hook (lambda ()
(TeX-fold-mode 1)))
If it should be activated in all AUCTeX modes, use TeX-mode-hook instead of LaTeX-mode-hook.
Once the mode is active there are several commands available to hide and show macros, environments and comments...