1 ;;; tooltip.el --- show tooltip windows
3 ;; Copyright (C) 1997, 1999-2018 Free Software Foundation, Inc.
5 ;; Author: Gerd Moellmann <gerd@acm.org>
6 ;; Keywords: help c mouse tools
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
30 (defvar comint-prompt-regexp
)
33 "Customization group for the `tooltip' package."
41 ;;; Switching tooltips on/off
43 (define-minor-mode tooltip-mode
45 With a prefix argument ARG, enable Tooltip mode if ARG is positive,
46 and disable it otherwise. If called from Lisp, enable the mode
47 if ARG is omitted or nil.
49 When this global minor mode is enabled, Emacs displays help
50 text (e.g. for buttons and menu items that you put the mouse on)
53 When Tooltip mode is disabled, Emacs displays help text in the
54 echo area, instead of making a pop-up window."
56 ;; Even if we start on a text-only terminal, make this non-nil by
57 ;; default because we can open a graphical frame later (multi-tty).
59 :initialize
'custom-initialize-delay
61 (if (and tooltip-mode
(fboundp 'x-show-tip
))
63 (add-hook 'pre-command-hook
'tooltip-hide
)
64 (add-hook 'tooltip-functions
'tooltip-help-tips
))
65 (unless (and (boundp 'gud-tooltip-mode
) gud-tooltip-mode
)
66 (remove-hook 'pre-command-hook
'tooltip-hide
))
67 (remove-hook 'tooltip-functions
'tooltip-help-tips
))
68 (setq show-help-function
69 (if tooltip-mode
'tooltip-show-help
'tooltip-show-help-non-mode
)))
72 ;;; Customizable settings
74 (defcustom tooltip-delay
0.7
75 "Seconds to wait before displaying a tooltip the first time."
79 (defcustom tooltip-short-delay
0.1
80 "Seconds to wait between subsequent tooltips on different items."
84 (defcustom tooltip-recent-seconds
1
85 "Display tooltips if changing tip items within this many seconds.
86 Do so after `tooltip-short-delay'."
90 (defcustom tooltip-hide-delay
10
91 "Hide tooltips automatically after this many seconds."
95 (defcustom tooltip-x-offset
5
96 "X offset, in pixels, for the display of tooltips.
97 The offset is the distance between the X position of the mouse and
98 the left border of the tooltip window. It must be chosen so that the
99 tooltip window doesn't contain the mouse when it pops up, or it may
100 interfere with clicking where you wish.
102 If `tooltip-frame-parameters' includes the `left' parameter,
103 the value of `tooltip-x-offset' is ignored."
107 (defcustom tooltip-y-offset
+20
108 "Y offset, in pixels, for the display of tooltips.
109 The offset is the distance between the Y position of the mouse and
110 the top border of the tooltip window. It must be chosen so that the
111 tooltip window doesn't contain the mouse when it pops up, or it may
112 interfere with clicking where you wish.
114 If `tooltip-frame-parameters' includes the `top' parameter,
115 the value of `tooltip-y-offset' is ignored."
119 (defcustom tooltip-frame-parameters
121 (internal-border-width .
2)
123 (no-special-glyphs . t
))
124 "Frame parameters used for tooltips.
126 If `left' or `top' parameters are included, they specify the absolute
127 position to pop up the tooltip.
129 Note that font and color parameters are ignored, and the attributes
130 of the `tooltip' face are used instead."
131 :type
'(repeat (cons :format
"%v"
132 (symbol :tag
"Parameter")
133 (sexp :tag
"Value")))
139 :background
"lightyellow"
141 :inherit variable-pitch
)
143 :inherit variable-pitch
))
148 (defcustom tooltip-use-echo-area nil
149 "Use the echo area instead of tooltip frames for help and GUD tooltips.
150 This variable is obsolete; instead of setting it to t, disable
151 `tooltip-mode' (which has a similar effect)."
155 (make-obsolete-variable 'tooltip-use-echo-area
156 "disable Tooltip mode instead" "24.1" 'set
)
158 (defcustom tooltip-resize-echo-area nil
159 "If non-nil, using the echo area for tooltips will resize the echo area.
160 By default, when the echo area is used for displaying tooltips,
161 the tooltip text is truncated if it exceeds a single screen line.
162 When this variable is non-nil, the text is not truncated; instead,
163 the echo area is resized as needed to accommodate the full text
165 This variable has effect only on GUI frames."
171 ;;; Variables that are not customizable.
173 (define-obsolete-variable-alias 'tooltip-hook
'tooltip-functions
"23.1")
175 (defvar tooltip-functions nil
176 "Functions to call to display tooltips.
177 Each function is called with one argument EVENT which is a copy
178 of the last mouse movement event that occurred. If one of these
179 functions displays the tooltip, it should return non-nil and the
180 rest are not called.")
182 (defvar tooltip-timeout-id nil
183 "The id of the timeout started when Emacs becomes idle.")
185 (defvar tooltip-last-mouse-motion-event nil
186 "A copy of the last mouse motion event seen.")
188 (defvar tooltip-hide-time nil
189 "Time when the last tooltip was hidden.")
191 (defvar gud-tooltip-mode
) ;; Prevent warning.
195 (defun tooltip-event-buffer (event)
196 "Return the buffer over which event EVENT occurred.
197 This might return nil if the event did not occur over a buffer."
198 (let ((window (posn-window (event-end event
))))
199 (and window
(window-buffer window
))))
202 ;;; Timeout for tooltip display
204 (defun tooltip-delay ()
205 "Return the delay in seconds for the next tooltip."
206 (if (and tooltip-hide-time
207 (< (- (float-time) tooltip-hide-time
) tooltip-recent-seconds
))
211 (defun tooltip-cancel-delayed-tip ()
212 "Disable the tooltip timeout."
213 (when tooltip-timeout-id
214 (disable-timeout tooltip-timeout-id
)
215 (setq tooltip-timeout-id nil
)))
217 (defun tooltip-start-delayed-tip ()
218 "Add a one-shot timeout to call function `tooltip-timeout'."
219 (setq tooltip-timeout-id
220 (add-timeout (tooltip-delay) 'tooltip-timeout nil
)))
222 (defun tooltip-timeout (_object)
223 "Function called when timer with id `tooltip-timeout-id' fires."
224 (run-hook-with-args-until-success 'tooltip-functions
225 tooltip-last-mouse-motion-event
))
230 (defun tooltip-set-param (alist key value
)
231 "Change the value of KEY in alist ALIST to VALUE.
232 If there's no association for KEY in ALIST, add one, otherwise
233 change the existing association. Value is the resulting alist."
234 (declare (obsolete "use (setf (alist-get ..) ..) instead" "25.1"))
235 (setf (alist-get key alist
) value
)
238 (declare-function x-show-tip
"xfns.c"
239 (string &optional frame parms timeout dx dy
))
241 (defun tooltip-show (text &optional use-echo-area
)
242 "Show a tooltip window displaying TEXT.
244 Text larger than `x-max-tooltip-size' is clipped.
246 If the alist in `tooltip-frame-parameters' includes `left' and `top'
247 parameters, they determine the x and y position where the tooltip
248 is displayed. Otherwise, the tooltip pops at offsets specified by
249 `tooltip-x-offset' and `tooltip-y-offset' from the current mouse
252 Optional second arg USE-ECHO-AREA non-nil means to show tooltip
255 (tooltip-show-help-non-mode text
)
256 (condition-case error
257 (let ((params (copy-sequence tooltip-frame-parameters
))
258 (fg (face-attribute 'tooltip
:foreground
))
259 (bg (face-attribute 'tooltip
:background
)))
261 (setf (alist-get 'foreground-color params
) fg
)
262 (setf (alist-get 'border-color params
) fg
))
264 (setf (alist-get 'background-color params
) bg
))
265 (x-show-tip (propertize text
'face
'tooltip
)
272 (message "Error while displaying tooltip: %s" error
)
274 (message "%s" text
)))))
276 (declare-function x-hide-tip
"xfns.c" ())
278 (defun tooltip-hide (&optional _ignored-arg
)
279 "Hide a tooltip, if one is displayed.
280 Value is non-nil if tooltip was open."
281 (tooltip-cancel-delayed-tip)
283 (setq tooltip-hide-time
(float-time))))
286 ;;; Debugger-related functions
288 (defun tooltip-identifier-from-point (point)
289 "Extract the identifier at POINT, if any.
290 Value is nil if no identifier exists at point. Identifier extraction
291 is based on the current syntax table."
294 (let* ((start (progn (skip-syntax-backward "w_") (point)))
295 (pstate (syntax-ppss)))
296 (unless (or (looking-at "[0-9]")
299 (skip-syntax-forward "w_")
300 (when (> (point) start
)
301 (buffer-substring start
(point)))))))
303 (defun tooltip-expr-to-print (event)
304 "Return an expression that should be printed for EVENT.
305 If a region is active and the mouse is inside the region, print
306 the region. Otherwise, figure out the identifier around the point
308 (with-current-buffer (tooltip-event-buffer event
)
309 (let ((point (posn-point (event-end event
))))
311 (when (and (<= (region-beginning) point
) (<= point
(region-end)))
312 (buffer-substring (region-beginning) (region-end)))
313 (tooltip-identifier-from-point point
)))))
315 (defun tooltip-process-prompt-regexp (process)
316 "Return regexp matching the prompt of PROCESS at the end of a string.
317 The prompt is taken from the value of `comint-prompt-regexp' in
318 the buffer of PROCESS."
319 (let ((prompt-regexp (with-current-buffer (process-buffer process
)
320 comint-prompt-regexp
)))
322 ;; Most start with `^' but the one for `sdb' cannot be easily
323 ;; stripped. Code the prompt for `sdb' fixed here.
324 (if (= (aref prompt-regexp
0) ?^
)
325 (substring prompt-regexp
1)
329 (defun tooltip-strip-prompt (process output
)
330 "Return OUTPUT with any prompt of PROCESS stripped from its end."
332 (if (string-match (tooltip-process-prompt-regexp process
) output
)
333 (substring output
0 (match-beginning 0))
339 (defvar tooltip-help-message nil
340 "The last help message received via `show-help-function'.
341 This is used by `tooltip-show-help' and
342 `tooltip-show-help-non-mode'.")
344 (defvar tooltip-previous-message nil
345 "The previous content of the echo area.")
347 (defun tooltip-show-help-non-mode (help)
348 "Function installed as `show-help-function' when Tooltip mode is off.
349 It is also called if Tooltip mode is on, for text-only displays."
350 (when (and (not (window-minibuffer-p)) ;Don't overwrite minibuffer contents.
351 (not cursor-in-echo-area
)) ;Don't overwrite a prompt.
354 (setq help
(replace-regexp-in-string "\n" ", " help
))
355 (unless (or tooltip-previous-message
356 (equal-including-properties help
(current-message))
357 (and (stringp tooltip-help-message
)
358 (equal-including-properties tooltip-help-message
360 (setq tooltip-previous-message
(current-message)))
361 (setq tooltip-help-message help
)
362 (let ((message-truncate-lines
363 (or (not (display-graphic-p)) (not tooltip-resize-echo-area
)))
364 (message-log-max nil
))
365 (message "%s" help
)))
366 ((stringp tooltip-previous-message
)
367 (let ((message-log-max nil
))
368 (message "%s" tooltip-previous-message
)
369 (setq tooltip-previous-message nil
)))
373 (defun tooltip-show-help (msg)
374 "Function installed as `show-help-function'.
375 MSG is either a help string to display, or nil to cancel the display."
376 (if (display-graphic-p)
377 (let ((previous-help tooltip-help-message
))
378 (setq tooltip-help-message msg
)
380 ;; Cancel display. This also cancels a delayed tip, if
383 ((equal-including-properties previous-help msg
)
384 ;; Same help as before (but possibly the mouse has moved).
385 ;; Keep what we have.
388 ;; A different help. Remove a previous tooltip, and
389 ;; display a new one, with some delay.
391 (tooltip-start-delayed-tip))))
392 ;; On text-only displays, try `tooltip-show-help-non-mode'.
393 (tooltip-show-help-non-mode msg
)))
395 (defun tooltip-help-tips (_event)
396 "Hook function to display a help tooltip.
397 This is installed on the hook `tooltip-functions', which
398 is run when the timer with id `tooltip-timeout-id' fires.
399 Value is non-nil if this function handled the tip."
400 (when (stringp tooltip-help-message
)
401 (tooltip-show tooltip-help-message tooltip-use-echo-area
)
406 ;;; tooltip.el ends here