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.
35 ;;; Customizable settings
38 "Customization group for the `tooltip' package."
48 (defcustom tooltip-delay 0.7
49 "Seconds to wait before displaying a tooltip the first time."
54 (defcustom tooltip-short-delay 0.1
55 "Seconds to wait between subsequent tooltips on different items."
60 (defcustom tooltip-recent-seconds 1
61 "Display tooltips if changing tip items within this many seconds.
62 Do so after `tooltip-short-delay'."
67 (defcustom tooltip-hide-delay 10
68 "Hide tooltips automatically after this many seconds."
73 (defcustom tooltip-x-offset nil
74 "X offset, in pixels, for the display of tooltips.
75 The offset is relative to the position of the mouse. It must
76 be chosen so that the tooltip window doesn't contain the mouse
77 when it pops up. If the value is nil, the default offset is 5
80 If `tooltip-frame-parameters' includes the `left' parameter,
81 the value of `tooltip-x-offset' is ignored."
83 :type '(choice (const :tag "Default" nil)
84 (integer :tag "Offset" :value 1))
87 (defcustom tooltip-y-offset nil
88 "Y offset, in pixels, for the display of tooltips.
89 The offset is relative to the position of the mouse. It must
90 be chosen so that the tooltip window doesn't contain the mouse
91 when it pops up. If the value is nil, the default offset is -10
94 If `tooltip-frame-parameters' includes the `top' parameter,
95 the value of `tooltip-y-offset' is ignored."
97 :type '(choice (const :tag "Default" nil)
98 (integer :tag "Offset" :value 1))
101 (defcustom tooltip-frame-parameters
103 (internal-border-width . 5)
105 "Frame parameters used for tooltips.
107 If `left' or `top' parameters are included, they specify the absolute
108 position to pop up the tooltip."
110 :tag "Frame Parameters"
115 (:background "lightyellow" :foreground "black"))
120 (defcustom tooltip-gud-tips-p nil
121 "*Non-nil means show tooltips in GUD sessions.
122 Setting this variable directly with `setq' has no effect;
123 you should either set it with the Customization facility
124 or use the function `tooltip-gud-tips-activate'."
127 :set #'(lambda (symbol on)
128 (setq tooltip-gud-tips-p on))
131 (defcustom tooltip-gud-modes '(gud-mode c-mode c++-mode)
132 "List of modes for which to enable GUD tips."
137 (defcustom tooltip-gud-display
138 '((eq (tooltip-event-buffer tooltip-gud-event)
139 (marker-buffer overlay-arrow-position)))
140 "List of forms determining where GUD tooltips are displayed.
142 Forms in the list are combined with AND. The default is to display
143 only tooltips in the buffer containing the overlay arrow."
145 :tag "GUD buffers predicate"
148 (defcustom tooltip-use-echo-area nil
149 "Use the echo area instead of tooltip frames.
150 This is only relevant GUD display, since otherwise it is equivalent to
151 turning off Tooltip mode."
157 ;;; Variables that are not customizable.
159 (defvar tooltip-hook nil
160 "Functions to call to display tooltips.
161 Each function is called with one argument EVENT which is a copy of
162 the last mouse movement event that occurred.")
164 (defvar tooltip-timeout-id nil
165 "The id of the timeout started when Emacs becomes idle.")
167 (defvar tooltip-last-mouse-motion-event nil
168 "A copy of the last mouse motion event seen.")
170 (defvar tooltip-hide-time nil
171 "Time when the last tooltip was hidden.")
176 (defun tooltip-event-buffer (event)
177 "Return the buffer over which event EVENT occurred.
178 This might return nil if the event did not occur over a buffer."
179 (let ((window (posn-window (event-end event))))
180 (and window (window-buffer window))))
183 ;;; Switching tooltips on/off
185 ;; We don't set track-mouse globally because this is a big redisplay
186 ;; problem in buffers having a pre-command-hook or such installed,
187 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
188 ;; set-buffer prevents redisplay optimizations, so every mouse motion
189 ;; would be accompanied by a full redisplay.
192 (defun tooltip-mode (&optional arg)
193 "Mode for tooltip display.
194 With ARG, turn tooltip mode on if and only if ARG is positive."
196 (unless (fboundp 'x-show-tip)
197 (error "Sorry, tooltips are not yet available on this system"))
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))))
214 ;;; Timeout for tooltip display
216 (defun tooltip-delay ()
217 "Return the delay in seconds for the next tooltip."
218 (let ((delay tooltip-delay)
220 (when (and tooltip-hide-time
221 (< (- now tooltip-hide-time) tooltip-recent-seconds))
222 (setq delay tooltip-short-delay))
225 (defun tooltip-cancel-delayed-tip ()
226 "Disable the tooltip timeout."
227 (when tooltip-timeout-id
228 (disable-timeout tooltip-timeout-id)
229 (setq tooltip-timeout-id nil)))
231 (defun tooltip-start-delayed-tip ()
232 "Add a one-shot timeout to call function tooltip-timeout."
233 (setq tooltip-timeout-id
234 (add-timeout (tooltip-delay) 'tooltip-timeout nil)))
236 (defun tooltip-timeout (object)
237 "Function called when timer with id tooltip-timeout-id fires."
238 (run-hook-with-args-until-success 'tooltip-hook
239 tooltip-last-mouse-motion-event))
242 ;;; Reacting on mouse movements
244 (defun tooltip-change-major-mode ()
245 "Function added to `change-major-mode-hook' when tooltip mode is on."
246 (add-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled))
248 (defun tooltip-activate-mouse-motions-if-enabled ()
249 "Reconsider for all buffers whether mouse motion events are desired."
250 (remove-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled)
251 (let ((buffers (buffer-list)))
254 (set-buffer (car buffers))
255 (if (and tooltip-mode
257 (memq major-mode tooltip-gud-modes))
258 (tooltip-activate-mouse-motions t)
259 (tooltip-activate-mouse-motions nil))
260 (setq buffers (cdr buffers))))))
262 (defvar tooltip-mouse-motions-active nil
263 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
265 (defun tooltip-activate-mouse-motions (activatep)
266 "Activate/deactivate mouse motion events for the current buffer.
267 ACTIVATEP non-nil means activate mouse motion events."
270 (make-local-variable 'tooltip-mouse-motions-active)
271 (setq tooltip-mouse-motions-active t)
272 (make-local-variable 'track-mouse)
273 (setq track-mouse t))
274 (when tooltip-mouse-motions-active
275 (kill-local-variable 'tooltip-mouse-motions-active)
276 (kill-local-variable 'track-mouse))))
278 (defun tooltip-mouse-motion (event)
279 "Command handler for mouse movement events in `global-map'."
282 (when (car (mouse-pixel-position))
283 (setq tooltip-last-mouse-motion-event (copy-sequence event))
284 (tooltip-start-delayed-tip)))
289 (defun tooltip-set-param (alist key value)
290 "Change the value of KEY in alist ALIST to VALUE.
291 If there's no association for KEY in ALIST, add one, otherwise
292 change the existing association. Value is the resulting alist."
293 (let ((param (assq key alist)))
296 (push (cons key value) alist))
299 (defun tooltip-show (text)
300 "Show a tooltip window displaying TEXT.
302 Text larger than `x-max-tooltip-size' (which see) is clipped.
304 If the alist in `tooltip-frame-parameters' includes `left' and `top'
305 parameters, they determine the x and y position where the tooltip
306 is displayed. Otherwise, the tooltip pops at offsets specified by
307 `tooltip-x-offset' and `tooltip-y-offset' from the current mouse
309 (if tooltip-use-echo-area
311 (condition-case error
312 (let ((params (copy-sequence tooltip-frame-parameters))
313 (fg (face-attribute 'tooltip :foreground))
314 (bg (face-attribute 'tooltip :background)))
316 (setq params (tooltip-set-param params 'foreground-color fg))
317 (setq params (tooltip-set-param params 'border-color fg)))
319 (setq params (tooltip-set-param params 'background-color bg)))
320 (x-show-tip (propertize text 'face 'tooltip)
327 (message "Error while displaying tooltip: %s" error)
329 (message "%s" text)))))
331 (defun tooltip-hide (&optional ignored-arg)
332 "Hide a tooltip, if one is displayed.
333 Value is non-nil if tooltip was open."
334 (tooltip-cancel-delayed-tip)
336 (setq tooltip-hide-time (float-time))))
339 ;;; Debugger-related functions
341 (defun tooltip-identifier-from-point (point)
342 "Extract the identifier at POINT, if any.
343 Value is nil if no identifier exists at point. Identifier extraction
344 is based on the current syntax table."
347 (let ((start (progn (skip-syntax-backward "w_") (point))))
348 (unless (looking-at "[0-9]")
349 (skip-syntax-forward "w_")
350 (when (> (point) start)
351 (buffer-substring start (point)))))))
353 (defmacro tooltip-region-active-p ()
354 "Value is non-nil if the region is currently active."
355 (if (string-match "^GNU" (emacs-version))
356 `(and transient-mark-mode mark-active)
359 (defun tooltip-expr-to-print (event)
360 "Return an expression that should be printed for EVENT.
361 If a region is active and the mouse is inside the region, print
362 the region. Otherwise, figure out the identifier around the point
365 (set-buffer (tooltip-event-buffer event))
366 (let ((point (posn-point (event-end event))))
367 (if (tooltip-region-active-p)
368 (when (and (<= (region-beginning) point) (<= point (region-end)))
369 (buffer-substring (region-beginning) (region-end)))
370 (tooltip-identifier-from-point point)))))
372 (defun tooltip-process-prompt-regexp (process)
373 "Return regexp matching the prompt of PROCESS at the end of a string.
374 The prompt is taken from the value of COMINT-PROMPT-REGEXP in the buffer
376 (let ((prompt-regexp (save-excursion
377 (set-buffer (process-buffer process))
378 comint-prompt-regexp)))
379 ;; Most start with `^' but the one for `sdb' cannot be easily
380 ;; stripped. Code the prompt for `sdb' fixed here.
381 (if (= (aref prompt-regexp 0) ?^)
382 (setq prompt-regexp (substring prompt-regexp 1))
383 (setq prompt-regexp "\\*"))
384 (concat "\n*" prompt-regexp "$")))
386 (defun tooltip-strip-prompt (process output)
387 "Return OUTPUT with any prompt of PROCESS stripped from its end."
388 (let ((prompt-regexp (tooltip-process-prompt-regexp process)))
390 (when (string-match prompt-regexp output)
391 (setq output (substring output 0 (match-beginning 0)))))
397 (defvar tooltip-gud-original-filter nil
398 "Process filter to restore after GUD output has been received.")
400 (defvar tooltip-gud-dereference nil
401 "Non-nil means print expressions with a `*' in front of them.
402 For C this would dereference a pointer expression.")
404 (defvar tooltip-gud-event nil
405 "The mouse movement event that led to a tooltip display.
406 This event can be examined by forms in TOOLTIP-GUD-DISPLAY.")
408 (defun tooltip-gud-toggle-dereference ()
409 "Toggle whether tooltips should show `* expr' or `expr'."
411 (setq tooltip-gud-dereference (not tooltip-gud-dereference))
412 (when (interactive-p)
413 (message "Dereferencing is now %s."
414 (if tooltip-gud-dereference "on" "off"))))
416 ; This will only display data that comes in one chunk.
417 ; The prompt is lost in first chunk so needn't be stripped.
418 ; Larger arrays (say 400 elements) are displayed in
419 ; the tootip incompletely (only the last chunk is displayed
420 ; with the rest going to the gud buffer).
421 ; Switching the process-filter creates timing problems and
422 ; it may be difficult to do better.
423 (defun tooltip-gud-process-output (process output)
424 "Process debugger output and show it in a tooltip window."
425 (set-process-filter process tooltip-gud-original-filter)
427 (gdba (tooltip-show (tooltip-strip-annotations output)))
428 (t (tooltip-show (tooltip-strip-prompt process output)))))
430 ; this is derived from gdb-output-burst in gdb-ui.el. It
431 ; also only processes data that comes in one chunk properly.
432 (defun tooltip-strip-annotations (string)
433 "Strip annotations from the output of the gdb process."
437 ;; Process all the complete markers in this chunk.
438 (while (string-match "\n\032\032\\(.*\\)\n" string)
440 ;; Stuff prior to the match is just ordinary output.
441 ;; It is either concatenated to OUTPUT or directed
446 (substring string 0 (match-beginning 0))))
448 ;; Take that stuff off the string.
449 (setq string (substring string (match-end 0))))
452 (defun tooltip-gud-print-command (expr)
453 "Return a suitable command to print the expression EXPR.
454 If TOOLTIP-GUD-DEREFERENCE is t, also prepend a `*' to EXPR."
455 (when tooltip-gud-dereference
456 (setq expr (concat "*" expr)))
458 ((gdb gdba) (concat "server print " expr))
459 (dbx (concat "print " expr))
460 (xdb (concat "p " expr))
461 (sdb (concat expr "/"))
464 (defun tooltip-gud-tips (event)
465 "Show tip for identifier or selection under the mouse.
466 The mouse must either point at an identifier or inside a selected
467 region for the tip window to be shown. If tooltip-gud-dereference is t,
468 add a `*' in front of the printed expression.
470 This function must return nil if it doesn't handle EVENT."
471 (let (gud-buffer process)
472 (when (and (eventp event)
474 (boundp 'gud-comint-buffer)
475 (setq gud-buffer gud-comint-buffer)
476 (setq process (get-buffer-process gud-buffer))
477 (posn-point (event-end event))
478 (progn (setq tooltip-gud-event event)
479 (eval (cons 'and tooltip-gud-display))))
480 (let ((expr (tooltip-expr-to-print event)))
482 (let ((cmd (tooltip-gud-print-command expr)))
483 (unless (null cmd) ; CMD can be nil if unknown debugger
484 (setq tooltip-gud-original-filter (process-filter process))
485 (set-process-filter process 'tooltip-gud-process-output)
492 (defvar tooltip-help-message nil
493 "The last help message received via `tooltip-show-help-function'.")
495 (defun tooltip-show-help-function (msg)
496 "Function installed as `show-help-function'.
497 MSG is either a help string to display, or nil to cancel the display."
498 (let ((previous-help tooltip-help-message))
499 (setq tooltip-help-message msg)
501 ;; Cancel display. This also cancels a delayed tip, if
504 ((equal previous-help msg)
505 ;; Same help as before (but possibly the mouse has moved).
506 ;; Keep what we have.
509 ;; A different help. Remove a previous tooltip, and
510 ;; display a new one, with some delay.
512 (tooltip-start-delayed-tip)))))
514 (defun tooltip-help-tips (event)
515 "Hook function to display a help tooltip.
516 This is installed on the hook `tooltip-hook', which is run when
517 the timer with ID `tooltip-timeout-id' fires.
518 Value is non-nil if this function handled the tip."
519 (when (stringp tooltip-help-message)
520 (tooltip-show tooltip-help-message)
524 ;;; Do this after all functions have been defined that are called from
525 ;;; `tooltip-mode'. The actual default value of `tooltip-mode' is set
529 (defcustom tooltip-mode nil
530 "Toggle tooltip-mode.
531 Setting this variable directly does not take effect;
532 use either \\[customize] or the function `tooltip-mode'."
533 :set (lambda (symbol value)
534 (tooltip-mode (or value 0)))
535 :initialize 'custom-initialize-default
542 ;;; tooltip.el ends here