1 ;;; tooltip.el --- show tooltip windows
3 ;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Gerd Moellmann <gerd@acm.org>
7 ;; 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 :init-value
(not (or noninteractive
54 (not (display-graphic-p))
55 (not (fboundp 'x-show-tip
))))
56 :initialize
'custom-initialize-safe-default
58 (unless (or (null tooltip-mode
) (fboundp 'x-show-tip
))
59 (error "Sorry, tooltips are not yet available on this system"))
62 (add-hook 'pre-command-hook
'tooltip-hide
)
63 (add-hook 'tooltip-hook
'tooltip-help-tips
))
64 (unless (and (boundp 'gud-tooltip-mode
) gud-tooltip-mode
)
65 (remove-hook 'pre-command-hook
'tooltip-hide
))
66 (remove-hook 'tooltip-hook
'tooltip-help-tips
))
67 (setq show-help-function
68 (if tooltip-mode
'tooltip-show-help
'tooltip-show-help-non-mode
)))
71 ;;; Customizable settings
73 (defcustom tooltip-delay
0.7
74 "Seconds to wait before displaying a tooltip the first time."
78 (defcustom tooltip-short-delay
0.1
79 "Seconds to wait between subsequent tooltips on different items."
83 (defcustom tooltip-recent-seconds
1
84 "Display tooltips if changing tip items within this many seconds.
85 Do so after `tooltip-short-delay'."
89 (defcustom tooltip-hide-delay
10
90 "Hide tooltips automatically after this many seconds."
94 (defcustom tooltip-x-offset
5
95 "X offset, in pixels, for the display of tooltips.
96 The offset is the distance between the X position of the mouse and
97 the left border of the tooltip window. It must be chosen so that the
98 tooltip window doesn't contain the mouse when it pops up, or it may
99 interfere with clicking where you wish.
101 If `tooltip-frame-parameters' includes the `left' parameter,
102 the value of `tooltip-x-offset' is ignored."
106 (defcustom tooltip-y-offset
+20
107 "Y offset, in pixels, for the display of tooltips.
108 The offset is the distance between the Y position of the mouse and
109 the top border of the tooltip window. It must be chosen so that the
110 tooltip window doesn't contain the mouse when it pops up, or it may
111 interfere with clicking where you wish.
113 If `tooltip-frame-parameters' includes the `top' parameter,
114 the value of `tooltip-y-offset' is ignored."
118 (defcustom tooltip-frame-parameters
120 (internal-border-width .
2)
122 "Frame parameters used for tooltips.
124 If `left' or `top' parameters are included, they specify the absolute
125 position to pop up the tooltip."
131 :background
"lightyellow"
133 :inherit variable-pitch
)
135 :inherit variable-pitch
))
140 (defcustom tooltip-use-echo-area nil
141 "Use the echo area instead of tooltip frames for help and GUD tooltips.
142 To display multi-line help text in the echo area, set this to t
143 and enable `tooltip-mode'."
148 ;;; Variables that are not customizable.
150 (defvar tooltip-hook nil
151 "Functions to call to display tooltips.
152 Each function is called with one argument EVENT which is a copy of
153 the last mouse movement event that occurred.")
155 (defvar tooltip-timeout-id nil
156 "The id of the timeout started when Emacs becomes idle.")
158 (defvar tooltip-last-mouse-motion-event nil
159 "A copy of the last mouse motion event seen.")
161 (defvar tooltip-hide-time nil
162 "Time when the last tooltip was hidden.")
164 (defvar gud-tooltip-mode
) ;; Prevent warning.
168 (defun tooltip-event-buffer (event)
169 "Return the buffer over which event EVENT occurred.
170 This might return nil if the event did not occur over a buffer."
171 (let ((window (posn-window (event-end event
))))
172 (and window
(window-buffer window
))))
175 ;;; Timeout for tooltip display
177 (defun tooltip-delay ()
178 "Return the delay in seconds for the next tooltip."
179 (if (and tooltip-hide-time
180 (< (- (float-time) tooltip-hide-time
) tooltip-recent-seconds
))
184 (defun tooltip-cancel-delayed-tip ()
185 "Disable the tooltip timeout."
186 (when tooltip-timeout-id
187 (disable-timeout tooltip-timeout-id
)
188 (setq tooltip-timeout-id nil
)))
190 (defun tooltip-start-delayed-tip ()
191 "Add a one-shot timeout to call function `tooltip-timeout'."
192 (setq tooltip-timeout-id
193 (add-timeout (tooltip-delay) 'tooltip-timeout nil
)))
195 (defun tooltip-timeout (object)
196 "Function called when timer with id `tooltip-timeout-id' fires."
197 (run-hook-with-args-until-success 'tooltip-hook
198 tooltip-last-mouse-motion-event
))
203 (defun tooltip-set-param (alist key value
)
204 "Change the value of KEY in alist ALIST to VALUE.
205 If there's no association for KEY in ALIST, add one, otherwise
206 change the existing association. Value is the resulting alist."
207 (let ((param (assq key alist
)))
210 (push (cons key value
) alist
))
213 (defun tooltip-show (text &optional use-echo-area
)
214 "Show a tooltip window displaying TEXT.
216 Text larger than `x-max-tooltip-size' is clipped.
218 If the alist in `tooltip-frame-parameters' includes `left' and `top'
219 parameters, they determine the x and y position where the tooltip
220 is displayed. Otherwise, the tooltip pops at offsets specified by
221 `tooltip-x-offset' and `tooltip-y-offset' from the current mouse
224 Optional second arg USE-ECHO-AREA non-nil means to show tooltip
227 (tooltip-show-help-non-mode text
)
228 (condition-case error
229 (let ((params (copy-sequence tooltip-frame-parameters
))
230 (fg (face-attribute 'tooltip
:foreground
))
231 (bg (face-attribute 'tooltip
:background
)))
233 (setq params
(tooltip-set-param params
'foreground-color fg
))
234 (setq params
(tooltip-set-param params
'border-color fg
)))
236 (setq params
(tooltip-set-param params
'background-color bg
)))
237 (x-show-tip (propertize text
'face
'tooltip
)
244 (message "Error while displaying tooltip: %s" error
)
246 (message "%s" text
)))))
248 (defun tooltip-hide (&optional ignored-arg
)
249 "Hide a tooltip, if one is displayed.
250 Value is non-nil if tooltip was open."
251 (tooltip-cancel-delayed-tip)
253 (setq tooltip-hide-time
(float-time))))
256 ;;; Debugger-related functions
258 (defun tooltip-identifier-from-point (point)
259 "Extract the identifier at POINT, if any.
260 Value is nil if no identifier exists at point. Identifier extraction
261 is based on the current syntax table."
264 (let ((start (progn (skip-syntax-backward "w_") (point))))
265 (unless (looking-at "[0-9]")
266 (skip-syntax-forward "w_")
267 (when (> (point) start
)
268 (buffer-substring start
(point)))))))
270 (defmacro tooltip-region-active-p
()
271 "Value is non-nil if the region should override command actions."
274 (defun tooltip-expr-to-print (event)
275 "Return an expression that should be printed for EVENT.
276 If a region is active and the mouse is inside the region, print
277 the region. Otherwise, figure out the identifier around the point
279 (with-current-buffer (tooltip-event-buffer event
)
280 (let ((point (posn-point (event-end event
))))
281 (if (tooltip-region-active-p)
282 (when (and (<= (region-beginning) point
) (<= point
(region-end)))
283 (buffer-substring (region-beginning) (region-end)))
284 (tooltip-identifier-from-point point
)))))
286 (defun tooltip-process-prompt-regexp (process)
287 "Return regexp matching the prompt of PROCESS at the end of a string.
288 The prompt is taken from the value of `comint-prompt-regexp' in
289 the buffer of PROCESS."
290 (let ((prompt-regexp (with-current-buffer (process-buffer process
)
291 comint-prompt-regexp
)))
293 ;; Most start with `^' but the one for `sdb' cannot be easily
294 ;; stripped. Code the prompt for `sdb' fixed here.
295 (if (= (aref prompt-regexp
0) ?^
)
296 (substring prompt-regexp
1)
300 (defun tooltip-strip-prompt (process output
)
301 "Return OUTPUT with any prompt of PROCESS stripped from its end."
303 (if (string-match (tooltip-process-prompt-regexp process
) output
)
304 (substring output
0 (match-beginning 0))
310 (defvar tooltip-help-message nil
311 "The last help message received via `tooltip-show-help'.")
313 (defvar tooltip-previous-message nil
314 "The previous content of the echo area.")
316 (defun tooltip-show-help-non-mode (help)
317 "Function installed as `show-help-function' when tooltip is off."
318 (when (and (not (window-minibuffer-p)) ;Don't overwrite minibuffer contents.
319 ;; Don't know how to reproduce it in Elisp:
320 ;; Don't overwrite a keystroke echo.
321 ;; (NILP (echo_message_buffer) || ok_to_overwrite_keystroke_echo)
322 (not cursor-in-echo-area
)) ;Don't overwrite a prompt.
325 (unless tooltip-previous-message
326 (setq tooltip-previous-message
(current-message)))
327 (let ((message-truncate-lines t
)
328 (message-log-max nil
))
329 (message "%s" (replace-regexp-in-string "\n" ", " help
))))
330 ((stringp tooltip-previous-message
)
331 (let ((message-log-max nil
))
332 (message "%s" tooltip-previous-message
)
333 (setq tooltip-previous-message nil
)))
338 (defun tooltip-show-help (msg)
339 "Function installed as `show-help-function'.
340 MSG is either a help string to display, or nil to cancel the display."
341 (let ((previous-help tooltip-help-message
))
342 (setq tooltip-help-message msg
)
344 ;; Cancel display. This also cancels a delayed tip, if
347 ((equal previous-help msg
)
348 ;; Same help as before (but possibly the mouse has moved).
349 ;; Keep what we have.
352 ;; A different help. Remove a previous tooltip, and
353 ;; display a new one, with some delay.
355 (tooltip-start-delayed-tip)))))
357 (defun tooltip-help-tips (event)
358 "Hook function to display a help tooltip.
359 This is installed on the hook `tooltip-hook', which is run when
360 the timer with id `tooltip-timeout-id' fires.
361 Value is non-nil if this function handled the tip."
362 (when (stringp tooltip-help-message
)
363 (tooltip-show tooltip-help-message tooltip-use-echo-area
)
368 ;; arch-tag: 3d61135e-4618-4a78-af28-183f6df5636f
369 ;;; tooltip.el ends here