1 ;;; tooltip.el --- Show tooltip windows
3 ;; Copyright (C) 1997, 1999, 2000 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.
27 ;; Put into your `.emacs'
44 ;;; Customizable settings
47 "Customization group for the `tooltip' package."
57 (defcustom tooltip-delay
1.0
58 "Seconds to wait before displaying a tooltip the first time."
64 (defcustom tooltip-short-delay
0.1
65 "Seconds to wait between subsequent tooltips on different items."
71 (defcustom tooltip-recent-seconds
1
72 "Display tooltips if changing tip items within this many seconds.
73 Do so after `tooltip-short-delay'."
79 (defcustom tooltip-x-offset nil
80 "Specify an X offset 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
85 :type
'(choice (const :tag
"Default" nil
)
86 (integer :tag
"Offset" :value
1))
90 (defcustom tooltip-y-offset nil
91 "Specify an Y offset for the display of tooltips.
92 The offset is relative to the position of the mouse. It must
93 be chosen so that the tooltip window doesn't contain the mouse
96 :type
'(choice (const :tag
"Default" nil
)
97 (integer :tag
"Offset" :value
1))
101 (defcustom tooltip-frame-parameters
103 (foreground-color .
"black")
104 (background-color .
"lightyellow")
105 (internal-border-width .
5)
106 (border-color .
"lightyellow")
108 "Frame parameters used for tooltips."
110 :tag
"Frame Parameters"
114 (defcustom tooltip-gud-tips-p nil
115 "*Non-nil means show tooltips in GUD sessions."
118 :set
#'(lambda (symbol on
)
119 (setq tooltip-gud-tips-p on
)
120 (if on
(tooltip-gud-tips-setup)))
124 (defcustom tooltip-gud-modes
'(gud-mode c-mode c
++-mode
)
125 "List of modes for which to enable GUD tips."
131 (defcustom tooltip-gud-display
132 '((eq (tooltip-event-buffer tooltip-gud-event
)
133 (marker-buffer overlay-arrow-position
)))
134 "List of forms determining where GUD tooltips are displayed.
136 Forms in the list are combined with AND. The default is to display
137 only tooltips in the buffer containing the overlay arrow."
139 :tag
"GUD buffers predicate"
143 (defcustom tooltip-use-echo-area nil
144 "Use the echo area instead of the actual tooltip windows."
150 ;;; Variables that are not customizable.
152 (defvar tooltip-hook nil
153 "Functions to call to display tooltips.
154 Each function is called with one argument EVENT which is a copy of
155 the last mouse movement event that occurred.")
158 (defvar tooltip-timeout-id nil
159 "The id of the timeout started when Emacs becomes idle.")
162 (defvar tooltip-last-mouse-motion-event nil
163 "A copy of the last mouse motion event seen.")
166 (defvar tooltip-hide-time nil
167 "Time when the last tooltip was hidden.")
170 (defvar tooltip-gud-debugger nil
171 "The debugger for which we show tooltips.")
177 (defun tooltip-event-buffer (event)
178 "Return the buffer over which event EVENT occurred.
179 This might return nil if the event did not occur over a buffer."
180 (let ((window (posn-window (event-end event
))))
181 (and window
(window-buffer window
))))
185 ;;; Switching tooltips on/off
187 ;; We don't set track-mouse globally because this is a big redisplay
188 ;; problem in buffers having a pre-command-hook or such installed,
189 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
190 ;; set-buffer prevents redisplay optimizations, so every mouse motion
191 ;; would be accompanied by a full redisplay.
194 (defun tooltip-mode (&optional arg
)
195 "Mode for tooltip display.
196 With ARG, turn tooltip mode on if and only if ARG is positive."
199 (> (prefix-numeric-value arg
) 0)
201 (hook-fn (if on
'add-hook
'remove-hook
)))
202 (setq tooltip-mode on
)
203 (funcall hook-fn
'change-major-mode-hook
'tooltip-change-major-mode
)
204 (tooltip-activate-mouse-motions-if-enabled)
205 (funcall hook-fn
'pre-command-hook
'tooltip-hide
)
206 (funcall hook-fn
'tooltip-hook
'tooltip-gud-tips
)
207 (funcall hook-fn
'tooltip-hook
'tooltip-help-tips
)
208 (setq show-help-function
(if on
'tooltip-show-help-function nil
))
209 ;; `ignore' is the default binding for mouse movements.
210 (define-key global-map
[mouse-movement
]
211 (if on
'tooltip-mouse-motion
'ignore
))
212 (tooltip-gud-tips-setup)))
214 (defun tooltip-gud-tips-setup ()
215 "Setup debugger mode-hooks for tooltips."
216 (when (and tooltip-mode tooltip-gud-tips-p
)
217 (global-set-key [S-mouse-3
] 'tooltip-gud-toggle-dereference
)
218 (add-hook 'gdb-mode-hook
219 #'(lambda () (setq tooltip-gud-debugger
'gdb
)))
220 (add-hook 'sdb-mode-hook
221 #'(lambda () (setq tooltip-gud-debugger
'sdb
)))
222 (add-hook 'dbx-mode-hook
223 #'(lambda () (setq tooltip-gud-debugger
'dbx
)))
224 (add-hook 'xdb-mode-hook
225 #'(lambda () (setq tooltip-gud-debugger
'xdb
)))
226 (add-hook 'perldb-mode-hook
227 #'(lambda () (setq tooltip-gud-debugger
'perldb
)))))
229 ;;; Timeout for tooltip display
231 (defun tooltip-delay ()
232 "Return the delay in seconds for the next tooltip."
233 (let ((delay tooltip-delay
)
235 (when (and tooltip-hide-time
236 (< (- now tooltip-hide-time
) tooltip-recent-seconds
))
237 (setq delay tooltip-short-delay
))
241 (defun tooltip-disable-timeout ()
242 "Disable the tooltip timeout."
243 (when tooltip-timeout-id
244 (disable-timeout tooltip-timeout-id
)
245 (setq tooltip-timeout-id nil
)))
248 (defun tooltip-add-timeout ()
249 "Add a one-shot timeout to call function tooltip-timeout."
250 (setq tooltip-timeout-id
251 (add-timeout (tooltip-delay) 'tooltip-timeout nil
)))
254 (defun tooltip-timeout (object)
255 "Function called when timer with id tooltip-timeout-id fires."
256 (run-hook-with-args-until-success 'tooltip-hook
257 tooltip-last-mouse-motion-event
))
261 ;;; Reacting on mouse movements
263 (defun tooltip-change-major-mode ()
264 "Function added to `change-major-mode-hook' when tooltip mode is on."
265 (add-hook 'post-command-hook
'tooltip-activate-mouse-motions-if-enabled
))
268 (defun tooltip-activate-mouse-motions-if-enabled ()
269 "Reconsider for all buffers whether mouse motion events are desired."
270 (remove-hook 'post-command-hook
'tooltip-activate-mouse-motions-if-enabled
)
271 (let ((buffers (buffer-list)))
274 (set-buffer (car buffers
))
275 (if (and tooltip-mode
277 (memq major-mode tooltip-gud-modes
))
278 (tooltip-activate-mouse-motions t
)
279 (tooltip-activate-mouse-motions nil
))
280 (setq buffers
(cdr buffers
))))))
283 (defun tooltip-activate-mouse-motions (activatep)
284 "Activate/deactivate mouse motion events for the current buffer.
285 ACTIVATEP non-nil means activate mouse motion events."
288 (make-local-variable 'track-mouse
)
289 (setq track-mouse t
))
290 (kill-local-variable 'track-mouse
)))
293 (defun tooltip-mouse-motion (event)
294 "Command handler for mouse movement events in `global-map'."
297 (when (car (mouse-pixel-position))
298 (setq tooltip-last-mouse-motion-event
(copy-sequence event
))
299 (tooltip-add-timeout)))
305 (defun tooltip-show (text)
306 "Show a tooltip window at the current mouse position displaying TEXT."
307 (if tooltip-use-echo-area
311 tooltip-frame-parameters
316 (defun tooltip-hide (&optional ignored-arg
)
317 "Hide a tooltip, if one is displayed.
318 Value is non-nil if tooltip was open."
319 (tooltip-disable-timeout)
321 (setq tooltip-hide-time
(float-time))))
325 ;;; Debugger-related functions
327 (defun tooltip-identifier-from-point (point)
328 "Extract the identifier at POINT, if any.
329 Value is nil if no identifier exists at point. Identifier extraction
330 is based on the current syntax table."
333 (let ((start (progn (skip-syntax-backward "w_") (point))))
334 (unless (looking-at "[0-9]")
335 (skip-syntax-forward "w_")
336 (when (> (point) start
)
337 (buffer-substring start
(point)))))))
340 (defmacro tooltip-region-active-p
()
341 "Value is non-nil if the region is currently active."
342 (if (string-match "^GNU" (emacs-version))
343 `(and transient-mark-mode mark-active
)
347 (defun tooltip-expr-to-print (event)
348 "Return an expression that should be printed for EVENT.
349 If a region is active and the mouse is inside the region, print
350 the region. Otherwise, figure out the identifier around the point
353 (set-buffer (tooltip-event-buffer event
))
354 (let ((point (posn-point (event-end event
))))
355 (if (tooltip-region-active-p)
356 (when (and (<= (region-beginning) point
) (<= point
(region-end)))
357 (buffer-substring (region-beginning) (region-end)))
358 (tooltip-identifier-from-point point
)))))
361 (defun tooltip-process-prompt-regexp (process)
362 "Return regexp matching the prompt of PROCESS at the end of a string.
363 The prompt is taken from the value of COMINT-PROMPT-REGEXP in the buffer
365 (let ((prompt-regexp (save-excursion
366 (set-buffer (process-buffer process
))
367 comint-prompt-regexp
)))
368 ;; Most start with `^' but the one for `sdb' cannot be easily
369 ;; stripped. Code the prompt for `sdb' fixed here.
370 (if (= (aref prompt-regexp
0) ?^
)
371 (setq prompt-regexp
(substring prompt-regexp
1))
372 (setq prompt-regexp
"\\*"))
373 (concat "\n*" prompt-regexp
"$")))
376 (defun tooltip-strip-prompt (process output
)
377 "Return OUTPUT with any prompt of PROCESS stripped from its end."
378 (let ((prompt-regexp (tooltip-process-prompt-regexp process
)))
380 (when (string-match prompt-regexp output
)
381 (setq output
(substring output
0 (match-beginning 0)))))
388 (defvar tooltip-gud-original-filter nil
389 "Process filter to restore after GUD output has been received.")
392 (defvar tooltip-gud-dereference nil
393 "Non-nil means print expressions with a `*' in front of them.
394 For C this would dereference a pointer expression.")
397 (defvar tooltip-gud-event nil
398 "The mouse movement event that led to a tooltip display.
399 This event can be examined by forms in TOOLTIP-GUD-DISPLAY.")
402 (defvar tooltip-gud-debugger nil
403 "A symbol describing the debugger running under GUD.")
406 (defun tooltip-gud-toggle-dereference ()
407 "Toggle whether tooltips should show `* expr' or `expr'."
409 (setq tooltip-gud-dereference
(not tooltip-gud-dereference
))
410 (when (interactive-p)
411 (message "Dereferencing is now %s."
412 (if tooltip-gud-dereference
"on" "off"))))
415 (defun tooltip-gud-process-output (process output
)
416 "Process debugger output and show it in a tooltip window."
417 (set-process-filter process tooltip-gud-original-filter
)
418 (tooltip-show (tooltip-strip-prompt process output
)))
421 (defun tooltip-gud-print-command (expr)
422 "Return a suitable command to print the expression EXPR.
423 If TOOLTIP-GUD-DEREFERENCE is t, also prepend a `*' to EXPR."
424 (when tooltip-gud-dereference
425 (setq expr
(concat "*" expr
)))
426 (case tooltip-gud-debugger
427 ((gdb dbx
) (concat "print " expr
))
428 (xdb (concat "p " expr
))
429 (sdb (concat expr
"/"))
433 (defun tooltip-gud-tips (event)
434 "Show tip for identifier or selection under the mouse.
435 The mouse must either point at an identifier or inside a selected
436 region for the tip window to be shown. If tooltip-gud-dereference is t,
437 add a `*' in front of the printed expression.
439 This function must return nil if it doesn't handle EVENT."
440 (let (gud-buffer process
)
441 (when (and (eventp event
)
443 (boundp 'gud-comint-buffer
)
444 (setq gud-buffer gud-comint-buffer
)
445 (setq process
(get-buffer-process gud-buffer
))
446 (posn-point (event-end event
))
447 (progn (setq tooltip-gud-event event
)
448 (eval (cons 'and tooltip-gud-display
))))
449 (let ((expr (tooltip-expr-to-print event
)))
451 (let ((cmd (tooltip-gud-print-command expr
)))
452 (unless (null cmd
) ; CMD can be nil if unknown debugger
453 (setq tooltip-gud-original-filter
(process-filter process
))
454 (set-process-filter process
'tooltip-gud-process-output
)
461 (defvar tooltip-help-message nil
462 "The last help message received via `tooltip-show-help-function'.")
465 (defun tooltip-show-help-function (msg)
466 "Function installed as `show-help-function'.
467 MSG is either a help string to display, or nil to cancel the display."
468 (let ((previous-help tooltip-help-message
))
469 (setq tooltip-help-message msg
)
472 ((or (not (stringp previous-help
))
473 (not (string= msg previous-help
)))
475 (tooltip-add-timeout))
477 (tooltip-disable-timeout)
478 (tooltip-add-timeout)))))
481 (defun tooltip-help-tips (event)
482 "Hook function to display a help tooltip.
483 Value is non-nil if this function handled the tip."
484 (when (stringp tooltip-help-message
)
485 (tooltip-show tooltip-help-message
)
486 (setq tooltip-help-message nil
)
491 ;;; Do this after all functions have been defined that are called from
492 ;;; `tooltip-mode'. The actual default value of `tooltip-mode' is set
496 (defcustom tooltip-mode nil
497 "Toggle tooltip-mode.
498 Setting this variable directly does not take effect;
499 use either \\[customize] or the function `tooltip-mode'."
500 :set
(lambda (symbol value
)
501 (tooltip-mode (or value
0)))
502 :initialize
'custom-initialize-default
508 ;;; tooltip.el ends here