1 ;;; tooltip.el --- show tooltip windows
3 ;; Copyright (C) 1997, 1999-2011 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 <http://www.gnu.org/licenses/>.
28 (defvar comint-prompt-regexp
)
31 "Customization group for the `tooltip' package."
39 ;;; Switching tooltips on/off
41 (define-minor-mode tooltip-mode
43 With ARG, turn Tooltip mode on if and only if ARG is positive.
44 When this minor mode is enabled, Emacs displays help text
45 in a pop-up window for buttons and menu items that you put the mouse on.
46 \(However, if `tooltip-use-echo-area' is non-nil, this and
47 all pop-up help appears in the echo area.)
49 When Tooltip mode is disabled, Emacs displays one line of
50 the help text in the echo area, and does not make a pop-up window."
52 ;; Even if we start on a text-only terminal, make this non-nil by
53 ;; default because we can open a graphical frame later (multi-tty).
55 :initialize
'custom-initialize-delay
57 (unless (or (null tooltip-mode
) (fboundp 'x-show-tip
))
58 (error "Sorry, tooltips are not yet available on this system"))
61 (add-hook 'pre-command-hook
'tooltip-hide
)
62 (add-hook 'tooltip-functions
'tooltip-help-tips
))
63 (unless (and (boundp 'gud-tooltip-mode
) gud-tooltip-mode
)
64 (remove-hook 'pre-command-hook
'tooltip-hide
))
65 (remove-hook 'tooltip-functions
'tooltip-help-tips
))
66 (setq show-help-function
67 (if tooltip-mode
'tooltip-show-help
'tooltip-show-help-non-mode
)))
70 ;;; Customizable settings
72 (defcustom tooltip-delay
0.7
73 "Seconds to wait before displaying a tooltip the first time."
77 (defcustom tooltip-short-delay
0.1
78 "Seconds to wait between subsequent tooltips on different items."
82 (defcustom tooltip-recent-seconds
1
83 "Display tooltips if changing tip items within this many seconds.
84 Do so after `tooltip-short-delay'."
88 (defcustom tooltip-hide-delay
10
89 "Hide tooltips automatically after this many seconds."
93 (defcustom tooltip-x-offset
5
94 "X offset, in pixels, for the display of tooltips.
95 The offset is the distance between the X position of the mouse and
96 the left border of the tooltip window. It must be chosen so that the
97 tooltip window doesn't contain the mouse when it pops up, or it may
98 interfere with clicking where you wish.
100 If `tooltip-frame-parameters' includes the `left' parameter,
101 the value of `tooltip-x-offset' is ignored."
105 (defcustom tooltip-y-offset
+20
106 "Y offset, in pixels, for the display of tooltips.
107 The offset is the distance between the Y position of the mouse and
108 the top border of the tooltip window. It must be chosen so that the
109 tooltip window doesn't contain the mouse when it pops up, or it may
110 interfere with clicking where you wish.
112 If `tooltip-frame-parameters' includes the `top' parameter,
113 the value of `tooltip-y-offset' is ignored."
117 (defcustom tooltip-frame-parameters
119 (internal-border-width .
2)
121 "Frame parameters used for tooltips.
123 If `left' or `top' parameters are included, they specify the absolute
124 position to pop up the tooltip.
126 Note that font and color parameters are ignored, and the attributes
127 of the `tooltip' face are used instead."
133 :background
"lightyellow"
135 :inherit variable-pitch
)
137 :inherit variable-pitch
))
142 (defcustom tooltip-use-echo-area nil
143 "Use the echo area instead of tooltip frames for help and GUD tooltips.
144 To display multi-line help text in the echo area, set this to t
145 and enable `tooltip-mode'."
150 ;;; Variables that are not customizable.
152 (defvar tooltip-functions nil
153 "Functions to call to display tooltips.
154 Each function is called with one argument EVENT which is a copy
155 of the last mouse movement event that occurred. If one of these
156 functions displays the tooltip, it should return non-nil and the
157 rest are not called.")
159 (define-obsolete-variable-alias 'tooltip-hook
'tooltip-functions
"23.1")
161 (defvar tooltip-timeout-id nil
162 "The id of the timeout started when Emacs becomes idle.")
164 (defvar tooltip-last-mouse-motion-event nil
165 "A copy of the last mouse motion event seen.")
167 (defvar tooltip-hide-time nil
168 "Time when the last tooltip was hidden.")
170 (defvar gud-tooltip-mode
) ;; Prevent warning.
174 (defun tooltip-event-buffer (event)
175 "Return the buffer over which event EVENT occurred.
176 This might return nil if the event did not occur over a buffer."
177 (let ((window (posn-window (event-end event
))))
178 (and window
(window-buffer window
))))
181 ;;; Timeout for tooltip display
183 (defun tooltip-delay ()
184 "Return the delay in seconds for the next tooltip."
185 (if (and tooltip-hide-time
186 (< (- (float-time) tooltip-hide-time
) tooltip-recent-seconds
))
190 (defun tooltip-cancel-delayed-tip ()
191 "Disable the tooltip timeout."
192 (when tooltip-timeout-id
193 (disable-timeout tooltip-timeout-id
)
194 (setq tooltip-timeout-id nil
)))
196 (defun tooltip-start-delayed-tip ()
197 "Add a one-shot timeout to call function `tooltip-timeout'."
198 (setq tooltip-timeout-id
199 (add-timeout (tooltip-delay) 'tooltip-timeout nil
)))
201 (defun tooltip-timeout (object)
202 "Function called when timer with id `tooltip-timeout-id' fires."
203 (run-hook-with-args-until-success 'tooltip-functions
204 tooltip-last-mouse-motion-event
))
209 (defun tooltip-set-param (alist key value
)
210 "Change the value of KEY in alist ALIST to VALUE.
211 If there's no association for KEY in ALIST, add one, otherwise
212 change the existing association. Value is the resulting alist."
213 (let ((param (assq key alist
)))
216 (push (cons key value
) alist
))
219 (declare-function x-show-tip
"xfns.c"
220 (string &optional frame parms timeout dx dy
))
222 (defun tooltip-show (text &optional use-echo-area
)
223 "Show a tooltip window displaying TEXT.
225 Text larger than `x-max-tooltip-size' is clipped.
227 If the alist in `tooltip-frame-parameters' includes `left' and `top'
228 parameters, they determine the x and y position where the tooltip
229 is displayed. Otherwise, the tooltip pops at offsets specified by
230 `tooltip-x-offset' and `tooltip-y-offset' from the current mouse
233 Optional second arg USE-ECHO-AREA non-nil means to show tooltip
236 (tooltip-show-help-non-mode text
)
237 (condition-case error
238 (let ((params (copy-sequence tooltip-frame-parameters
))
239 (fg (face-attribute 'tooltip
:foreground
))
240 (bg (face-attribute 'tooltip
:background
)))
242 (setq params
(tooltip-set-param params
'foreground-color fg
))
243 (setq params
(tooltip-set-param params
'border-color fg
)))
245 (setq params
(tooltip-set-param params
'background-color bg
)))
246 (x-show-tip (propertize text
'face
'tooltip
)
253 (message "Error while displaying tooltip: %s" error
)
255 (message "%s" text
)))))
257 (declare-function x-hide-tip
"xfns.c" ())
259 (defun tooltip-hide (&optional ignored-arg
)
260 "Hide a tooltip, if one is displayed.
261 Value is non-nil if tooltip was open."
262 (tooltip-cancel-delayed-tip)
264 (setq tooltip-hide-time
(float-time))))
267 ;;; Debugger-related functions
269 (defun tooltip-identifier-from-point (point)
270 "Extract the identifier at POINT, if any.
271 Value is nil if no identifier exists at point. Identifier extraction
272 is based on the current syntax table."
275 (let ((start (progn (skip-syntax-backward "w_") (point))))
276 (unless (looking-at "[0-9]")
277 (skip-syntax-forward "w_")
278 (when (> (point) start
)
279 (buffer-substring start
(point)))))))
281 (defmacro tooltip-region-active-p
()
282 "Value is non-nil if the region should override command actions."
285 (defun tooltip-expr-to-print (event)
286 "Return an expression that should be printed for EVENT.
287 If a region is active and the mouse is inside the region, print
288 the region. Otherwise, figure out the identifier around the point
290 (with-current-buffer (tooltip-event-buffer event
)
291 (let ((point (posn-point (event-end event
))))
292 (if (tooltip-region-active-p)
293 (when (and (<= (region-beginning) point
) (<= point
(region-end)))
294 (buffer-substring (region-beginning) (region-end)))
295 (tooltip-identifier-from-point point
)))))
297 (defun tooltip-process-prompt-regexp (process)
298 "Return regexp matching the prompt of PROCESS at the end of a string.
299 The prompt is taken from the value of `comint-prompt-regexp' in
300 the buffer of PROCESS."
301 (let ((prompt-regexp (with-current-buffer (process-buffer process
)
302 comint-prompt-regexp
)))
304 ;; Most start with `^' but the one for `sdb' cannot be easily
305 ;; stripped. Code the prompt for `sdb' fixed here.
306 (if (= (aref prompt-regexp
0) ?^
)
307 (substring prompt-regexp
1)
311 (defun tooltip-strip-prompt (process output
)
312 "Return OUTPUT with any prompt of PROCESS stripped from its end."
314 (if (string-match (tooltip-process-prompt-regexp process
) output
)
315 (substring output
0 (match-beginning 0))
321 (defvar tooltip-help-message nil
322 "The last help message received via `show-help-function'.
323 This is used by `tooltip-show-help' and
324 `tooltip-show-help-non-mode'.")
326 (defvar tooltip-previous-message nil
327 "The previous content of the echo area.")
329 (defun tooltip-show-help-non-mode (help)
330 "Function installed as `show-help-function' when Tooltip mode is off.
331 It is also called if Tooltip mode is on, for text-only displays."
332 (when (and (not (window-minibuffer-p)) ;Don't overwrite minibuffer contents.
333 (not cursor-in-echo-area
)) ;Don't overwrite a prompt.
336 (setq help
(replace-regexp-in-string "\n" ", " help
))
337 (unless (or tooltip-previous-message
338 (string-equal help
(current-message))
339 (and (stringp tooltip-help-message
)
340 (string-equal tooltip-help-message
342 (setq tooltip-previous-message
(current-message)))
343 (setq tooltip-help-message help
)
344 (let ((message-truncate-lines t
)
345 (message-log-max nil
))
346 (message "%s" help
)))
347 ((stringp tooltip-previous-message
)
348 (let ((message-log-max nil
))
349 (message "%s" tooltip-previous-message
)
350 (setq tooltip-previous-message nil
)))
354 (defun tooltip-show-help (msg)
355 "Function installed as `show-help-function'.
356 MSG is either a help string to display, or nil to cancel the display."
357 (if (display-graphic-p)
358 (let ((previous-help tooltip-help-message
))
359 (setq tooltip-help-message msg
)
361 ;; Cancel display. This also cancels a delayed tip, if
364 ((equal previous-help msg
)
365 ;; Same help as before (but possibly the mouse has moved).
366 ;; Keep what we have.
369 ;; A different help. Remove a previous tooltip, and
370 ;; display a new one, with some delay.
372 (tooltip-start-delayed-tip))))
373 ;; On text-only displays, try `tooltip-show-help-non-mode'.
374 (tooltip-show-help-non-mode msg
)))
376 (defun tooltip-help-tips (event)
377 "Hook function to display a help tooltip.
378 This is installed on the hook `tooltip-functions', which
379 is run when the timer with id `tooltip-timeout-id' fires.
380 Value is non-nil if this function handled the tip."
381 (when (stringp tooltip-help-message
)
382 (tooltip-show tooltip-help-message tooltip-use-echo-area
)
387 ;;; tooltip.el ends here