1 ;;; tooltip.el --- show tooltip windows
3 ;; Copyright (C) 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
5 ;; Author: Gerd Moellmann <gerd@acm.org>
6 ;; Keywords: help c mouse tools
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
37 ;;; Customizable settings
40 "Customization group for the `tooltip' package."
50 (defcustom tooltip-delay
0.7
51 "Seconds to wait before displaying a tooltip the first time."
57 (defcustom tooltip-short-delay
0.1
58 "Seconds to wait between subsequent tooltips on different items."
64 (defcustom tooltip-recent-seconds
1
65 "Display tooltips if changing tip items within this many seconds.
66 Do so after `tooltip-short-delay'."
72 (defcustom tooltip-hide-delay
10
73 "Hide tooltips automatically after this many seconds."
79 (defcustom tooltip-x-offset nil
80 "X offset, in pixels, for the display of tooltips.
81 The offset is relative to the position of the mouse. It must
82 be chosen so that the tooltip window doesn't contain the mouse
83 when it pops up. If the value is nil, the default offset is 5
86 If `tooltip-frame-parameters' includes the `left' parameter,
87 the value of `tooltip-x-offset' is ignored."
89 :type
'(choice (const :tag
"Default" nil
)
90 (integer :tag
"Offset" :value
1))
94 (defcustom tooltip-y-offset nil
95 "Y offset, in pixels, for the display of tooltips.
96 The offset is relative to the position of the mouse. It must
97 be chosen so that the tooltip window doesn't contain the mouse
98 when it pops up. If the value is nil, the default offset is -10
101 If `tooltip-frame-parameters' includes the `top' parameter,
102 the value of `tooltip-y-offset' is ignored."
104 :type
'(choice (const :tag
"Default" nil
)
105 (integer :tag
"Offset" :value
1))
109 (defcustom tooltip-frame-parameters
111 (internal-border-width .
5)
113 "Frame parameters used for tooltips.
115 If `left' or `top' parameters are included, they specify the absolute
116 position to pop up the tooltip."
118 :tag
"Frame Parameters"
124 (:background
"lightyellow" :foreground
"black"))
130 (defcustom tooltip-gud-tips-p nil
131 "*Non-nil means show tooltips in GUD sessions."
134 :set
#'(lambda (symbol on
)
135 (setq tooltip-gud-tips-p on
))
139 (defcustom tooltip-gud-modes
'(gud-mode c-mode c
++-mode
)
140 "List of modes for which to enable GUD tips."
146 (defcustom tooltip-gud-display
147 '((eq (tooltip-event-buffer tooltip-gud-event
)
148 (marker-buffer overlay-arrow-position
)))
149 "List of forms determining where GUD tooltips are displayed.
151 Forms in the list are combined with AND. The default is to display
152 only tooltips in the buffer containing the overlay arrow."
154 :tag
"GUD buffers predicate"
158 (defcustom tooltip-use-echo-area nil
159 "Use the echo area instead of tooltip frames.
160 This is only relevant GUD display, since otherwise it is equivalent to
161 turning off Tooltip mode."
167 ;;; Variables that are not customizable.
169 (defvar tooltip-hook nil
170 "Functions to call to display tooltips.
171 Each function is called with one argument EVENT which is a copy of
172 the last mouse movement event that occurred.")
175 (defvar tooltip-timeout-id nil
176 "The id of the timeout started when Emacs becomes idle.")
179 (defvar tooltip-last-mouse-motion-event nil
180 "A copy of the last mouse motion event seen.")
183 (defvar tooltip-hide-time nil
184 "Time when the last tooltip was hidden.")
190 (defun tooltip-event-buffer (event)
191 "Return the buffer over which event EVENT occurred.
192 This might return nil if the event did not occur over a buffer."
193 (let ((window (posn-window (event-end event
))))
194 (and window
(window-buffer window
))))
198 ;;; Switching tooltips on/off
200 ;; We don't set track-mouse globally because this is a big redisplay
201 ;; problem in buffers having a pre-command-hook or such installed,
202 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
203 ;; set-buffer prevents redisplay optimizations, so every mouse motion
204 ;; would be accompanied by a full redisplay.
207 (defun tooltip-mode (&optional arg
)
208 "Mode for tooltip display.
209 With ARG, turn tooltip mode on if and only if ARG is positive."
211 (unless (fboundp 'x-show-tip
)
212 (error "Sorry, tooltips are not yet available on this system"))
214 (> (prefix-numeric-value arg
) 0)
216 (hook-fn (if on
'add-hook
'remove-hook
)))
217 (setq tooltip-mode on
)
218 (funcall hook-fn
'change-major-mode-hook
'tooltip-change-major-mode
)
219 (tooltip-activate-mouse-motions-if-enabled)
220 (funcall hook-fn
'pre-command-hook
'tooltip-hide
)
221 (funcall hook-fn
'tooltip-hook
'tooltip-gud-tips
)
222 (funcall hook-fn
'tooltip-hook
'tooltip-help-tips
)
223 (setq show-help-function
(if on
'tooltip-show-help-function nil
))
224 ;; `ignore' is the default binding for mouse movements.
225 (define-key global-map
[mouse-movement
]
226 (if on
'tooltip-mouse-motion
'ignore
))))
230 ;;; Timeout for tooltip display
232 (defun tooltip-delay ()
233 "Return the delay in seconds for the next tooltip."
234 (let ((delay tooltip-delay
)
236 (when (and tooltip-hide-time
237 (< (- now tooltip-hide-time
) tooltip-recent-seconds
))
238 (setq delay tooltip-short-delay
))
242 (defun tooltip-cancel-delayed-tip ()
243 "Disable the tooltip timeout."
244 (when tooltip-timeout-id
245 (disable-timeout tooltip-timeout-id
)
246 (setq tooltip-timeout-id nil
)))
249 (defun tooltip-start-delayed-tip ()
250 "Add a one-shot timeout to call function tooltip-timeout."
251 (setq tooltip-timeout-id
252 (add-timeout (tooltip-delay) 'tooltip-timeout nil
)))
255 (defun tooltip-timeout (object)
256 "Function called when timer with id tooltip-timeout-id fires."
257 (run-hook-with-args-until-success 'tooltip-hook
258 tooltip-last-mouse-motion-event
))
262 ;;; Reacting on mouse movements
264 (defun tooltip-change-major-mode ()
265 "Function added to `change-major-mode-hook' when tooltip mode is on."
266 (add-hook 'post-command-hook
'tooltip-activate-mouse-motions-if-enabled
))
269 (defun tooltip-activate-mouse-motions-if-enabled ()
270 "Reconsider for all buffers whether mouse motion events are desired."
271 (remove-hook 'post-command-hook
'tooltip-activate-mouse-motions-if-enabled
)
272 (let ((buffers (buffer-list)))
275 (set-buffer (car buffers
))
276 (if (and tooltip-mode
278 (memq major-mode tooltip-gud-modes
))
279 (tooltip-activate-mouse-motions t
)
280 (tooltip-activate-mouse-motions nil
))
281 (setq buffers
(cdr buffers
))))))
283 (defvar tooltip-mouse-motions-active nil
284 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
286 (defun tooltip-activate-mouse-motions (activatep)
287 "Activate/deactivate mouse motion events for the current buffer.
288 ACTIVATEP non-nil means activate mouse motion events."
291 (make-local-variable 'tooltip-mouse-motions-active
)
292 (setq tooltip-mouse-motions-active t
)
293 (make-local-variable 'track-mouse
)
294 (setq track-mouse t
))
295 (when tooltip-mouse-motions-active
296 (kill-local-variable 'tooltip-mouse-motions-active
)
297 (kill-local-variable 'track-mouse
))))
299 (defun tooltip-mouse-motion (event)
300 "Command handler for mouse movement events in `global-map'."
303 (when (car (mouse-pixel-position))
304 (setq tooltip-last-mouse-motion-event
(copy-sequence event
))
305 (tooltip-start-delayed-tip)))
311 (defun tooltip-set-param (alist key value
)
312 "Change the value of KEY in alist ALIST to VALUE.
313 If there's no association for KEY in ALIST, add one, otherwise
314 change the existing association. Value is the resulting alist."
315 (let ((param (assq key alist
)))
318 (push (cons key value
) alist
))
322 (defun tooltip-show (text)
323 "Show a tooltip window displaying TEXT.
325 Text larger than `x-max-tooltip-size' (which see) is clipped.
327 If the alist in `tooltip-frame-parameters' includes `left' and `top'
328 parameters, they determine the x and y position where the tooltip
329 is displayed. Otherwise, the tooltip pops at offsets specified by
330 `tooltip-x-offset' and `tooltip-y-offset' from the current mouse
332 (if tooltip-use-echo-area
334 (condition-case error
335 (let ((params (copy-sequence tooltip-frame-parameters
))
336 (fg (face-attribute 'tooltip
:foreground
))
337 (bg (face-attribute 'tooltip
:background
)))
339 (setq params
(tooltip-set-param params
'foreground-color fg
))
340 (setq params
(tooltip-set-param params
'border-color fg
)))
342 (setq params
(tooltip-set-param params
'background-color bg
)))
343 (x-show-tip (propertize text
'face
'tooltip
)
350 (message "Error while displaying tooltip: %s" error
)
352 (message "%s" text
)))))
355 (defun tooltip-hide (&optional ignored-arg
)
356 "Hide a tooltip, if one is displayed.
357 Value is non-nil if tooltip was open."
358 (tooltip-cancel-delayed-tip)
360 (setq tooltip-hide-time
(float-time))))
364 ;;; Debugger-related functions
366 (defun tooltip-identifier-from-point (point)
367 "Extract the identifier at POINT, if any.
368 Value is nil if no identifier exists at point. Identifier extraction
369 is based on the current syntax table."
372 (let ((start (progn (skip-syntax-backward "w_") (point))))
373 (unless (looking-at "[0-9]")
374 (skip-syntax-forward "w_")
375 (when (> (point) start
)
376 (buffer-substring start
(point)))))))
379 (defmacro tooltip-region-active-p
()
380 "Value is non-nil if the region is currently active."
381 (if (string-match "^GNU" (emacs-version))
382 `(and transient-mark-mode mark-active
)
386 (defun tooltip-expr-to-print (event)
387 "Return an expression that should be printed for EVENT.
388 If a region is active and the mouse is inside the region, print
389 the region. Otherwise, figure out the identifier around the point
392 (set-buffer (tooltip-event-buffer event
))
393 (let ((point (posn-point (event-end event
))))
394 (if (tooltip-region-active-p)
395 (when (and (<= (region-beginning) point
) (<= point
(region-end)))
396 (buffer-substring (region-beginning) (region-end)))
397 (tooltip-identifier-from-point point
)))))
400 (defun tooltip-process-prompt-regexp (process)
401 "Return regexp matching the prompt of PROCESS at the end of a string.
402 The prompt is taken from the value of COMINT-PROMPT-REGEXP in the buffer
404 (let ((prompt-regexp (save-excursion
405 (set-buffer (process-buffer process
))
406 comint-prompt-regexp
)))
407 ;; Most start with `^' but the one for `sdb' cannot be easily
408 ;; stripped. Code the prompt for `sdb' fixed here.
409 (if (= (aref prompt-regexp
0) ?^
)
410 (setq prompt-regexp
(substring prompt-regexp
1))
411 (setq prompt-regexp
"\\*"))
412 (concat "\n*" prompt-regexp
"$")))
415 (defun tooltip-strip-prompt (process output
)
416 "Return OUTPUT with any prompt of PROCESS stripped from its end."
417 (let ((prompt-regexp (tooltip-process-prompt-regexp process
)))
419 (when (string-match prompt-regexp output
)
420 (setq output
(substring output
0 (match-beginning 0)))))
427 (defvar tooltip-gud-original-filter nil
428 "Process filter to restore after GUD output has been received.")
431 (defvar tooltip-gud-dereference nil
432 "Non-nil means print expressions with a `*' in front of them.
433 For C this would dereference a pointer expression.")
436 (defvar tooltip-gud-event nil
437 "The mouse movement event that led to a tooltip display.
438 This event can be examined by forms in TOOLTIP-GUD-DISPLAY.")
441 (defun tooltip-gud-toggle-dereference ()
442 "Toggle whether tooltips should show `* expr' or `expr'."
444 (setq tooltip-gud-dereference
(not tooltip-gud-dereference
))
445 (when (interactive-p)
446 (message "Dereferencing is now %s."
447 (if tooltip-gud-dereference
"on" "off"))))
450 (defun tooltip-gud-process-output (process output
)
451 "Process debugger output and show it in a tooltip window."
452 (set-process-filter process tooltip-gud-original-filter
)
453 (tooltip-show (tooltip-strip-prompt process output
)))
456 (defun tooltip-gud-print-command (expr)
457 "Return a suitable command to print the expression EXPR.
458 If TOOLTIP-GUD-DEREFERENCE is t, also prepend a `*' to EXPR."
459 (when tooltip-gud-dereference
460 (setq expr
(concat "*" expr
)))
462 ((gdb gdba
) (concat "server print " expr
))
463 (dbx (concat "print " expr
))
464 (xdb (concat "p " expr
))
465 (sdb (concat expr
"/"))
469 (defun tooltip-gud-tips (event)
470 "Show tip for identifier or selection under the mouse.
471 The mouse must either point at an identifier or inside a selected
472 region for the tip window to be shown. If tooltip-gud-dereference is t,
473 add a `*' in front of the printed expression.
475 This function must return nil if it doesn't handle EVENT."
476 (let (gud-buffer process
)
477 (when (and (eventp event
)
479 (boundp 'gud-comint-buffer
)
480 (setq gud-buffer gud-comint-buffer
)
481 (setq process
(get-buffer-process gud-buffer
))
482 (posn-point (event-end event
))
483 (progn (setq tooltip-gud-event event
)
484 (eval (cons 'and tooltip-gud-display
))))
485 (let ((expr (tooltip-expr-to-print event
)))
487 (let ((cmd (tooltip-gud-print-command expr
)))
488 (unless (null cmd
) ; CMD can be nil if unknown debugger
489 (setq tooltip-gud-original-filter
(process-filter process
))
490 (set-process-filter process
'tooltip-gud-process-output
)
497 (defvar tooltip-help-message nil
498 "The last help message received via `tooltip-show-help-function'.")
501 (defun tooltip-show-help-function (msg)
502 "Function installed as `show-help-function'.
503 MSG is either a help string to display, or nil to cancel the display."
504 (let ((previous-help tooltip-help-message
))
505 (setq tooltip-help-message msg
)
507 ;; Cancel display. This also cancels a delayed tip, if
510 ((equal previous-help msg
)
511 ;; Same help as before (but possibly the mouse has moved).
512 ;; Keep what we have.
515 ;; A different help. Remove a previous tooltip, and
516 ;; display a new one, with some delay.
518 (tooltip-start-delayed-tip)))))
521 (defun tooltip-help-tips (event)
522 "Hook function to display a help tooltip.
523 This is installed on the hook `tooltip-hook', which is run when
524 the timer with ID `tooltip-timeout-id' fires.
525 Value is non-nil if this function handled the tip."
526 (when (stringp tooltip-help-message
)
527 (tooltip-show tooltip-help-message
)
532 ;;; Do this after all functions have been defined that are called from
533 ;;; `tooltip-mode'. The actual default value of `tooltip-mode' is set
537 (defcustom tooltip-mode nil
538 "Toggle tooltip-mode.
539 Setting this variable directly does not take effect;
540 use either \\[customize] or the function `tooltip-mode'."
541 :set
(lambda (symbol value
)
542 (tooltip-mode (or value
0)))
543 :initialize
'custom-initialize-default
549 ;;; tooltip.el ends here