1 ;;; tooltip.el --- show tooltip windows
3 ;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 ;; 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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl)) ; for case macro
33 ;;; Customizable settings
36 "Customization group for the `tooltip' package."
44 (defcustom tooltip-delay 0.7
45 "Seconds to wait before displaying a tooltip the first time."
50 (defcustom tooltip-short-delay 0.1
51 "Seconds to wait between subsequent tooltips on different items."
56 (defcustom tooltip-recent-seconds 1
57 "Display tooltips if changing tip items within this many seconds.
58 Do so after `tooltip-short-delay'."
63 (defcustom tooltip-hide-delay 10
64 "Hide tooltips automatically after this many seconds."
69 (defcustom tooltip-x-offset nil
70 "X offset, in pixels, for the display of tooltips.
71 The offset is relative to the position of the mouse. It must
72 be chosen so that the tooltip window doesn't contain the mouse
73 when it pops up. If the value is nil, the default offset is 5
76 If `tooltip-frame-parameters' includes the `left' parameter,
77 the value of `tooltip-x-offset' is ignored."
79 :type '(choice (const :tag "Default" nil)
80 (integer :tag "Offset" :value 1))
83 (defcustom tooltip-y-offset nil
84 "Y offset, in pixels, for the display of tooltips.
85 The offset is relative to the position of the mouse. It must
86 be chosen so that the tooltip window doesn't contain the mouse
87 when it pops up. If the value is nil, the default offset is -10
90 If `tooltip-frame-parameters' includes the `top' parameter,
91 the value of `tooltip-y-offset' is ignored."
93 :type '(choice (const :tag "Default" nil)
94 (integer :tag "Offset" :value 1))
97 (defcustom tooltip-frame-parameters
99 (internal-border-width . 5)
101 "Frame parameters used for tooltips.
103 If `left' or `top' parameters are included, they specify the absolute
104 position to pop up the tooltip."
106 :tag "Frame Parameters"
111 :background "lightyellow"
113 :inherit variable-pitch)
115 :inherit variable-pitch))
119 (defcustom tooltip-gud-tips-p nil
120 "*Non-nil means show tooltips in GUD sessions.
122 This allows you to display a variable's value in a tooltip simply
123 by pointing at it with the mouse. In the case of a C program
124 controlled by GDB, it shows the associated #define directives
125 when program is not executing."
130 (defcustom tooltip-gud-modes '(gud-mode c-mode c++-mode fortran-mode)
131 "List of modes for which to enable GUD tips."
136 (defcustom tooltip-gud-display
137 '((eq (tooltip-event-buffer tooltip-gud-event)
138 (marker-buffer gud-overlay-arrow-position)))
139 "List of forms determining where GUD tooltips are displayed.
141 Forms in the list are combined with AND. The default is to display
142 only tooltips in the buffer containing the overlay arrow."
144 :tag "GUD buffers predicate"
147 (defcustom tooltip-gud-echo-area nil
148 "Use the echo area instead of frames for GUD tooltips."
153 (defvaralias 'tooltip-use-echo-area 'tooltip-gud-echo-area)
154 (make-obsolete-variable 'tooltip-use-echo-area 'tooltip-gud-echo-area "22.1")
156 ;;; Variables that are not customizable.
158 (defvar tooltip-hook nil
159 "Functions to call to display tooltips.
160 Each function is called with one argument EVENT which is a copy of
161 the last mouse movement event that occurred.")
163 (defvar tooltip-timeout-id nil
164 "The id of the timeout started when Emacs becomes idle.")
166 (defvar tooltip-last-mouse-motion-event nil
167 "A copy of the last mouse motion event seen.")
169 (defvar tooltip-hide-time nil
170 "Time when the last tooltip was hidden.")
175 (defun tooltip-event-buffer (event)
176 "Return the buffer over which event EVENT occurred.
177 This might return nil if the event did not occur over a buffer."
178 (let ((window (posn-window (event-end event))))
179 (and window (window-buffer window))))
182 ;;; Switching tooltips on/off
184 ;; We don't set track-mouse globally because this is a big redisplay
185 ;; problem in buffers having a pre-command-hook or such installed,
186 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
187 ;; set-buffer prevents redisplay optimizations, so every mouse motion
188 ;; would be accompanied by a full redisplay.
191 (define-minor-mode tooltip-mode
192 "Toggle Tooltip display.
193 With ARG, turn tooltip mode on if and only if ARG is positive."
195 ;; If you change the :init-value below, you also need to change the
196 ;; corresponding code in startup.el.
197 :init-value (not (or noninteractive
198 (and (boundp 'emacs-quick-startup) emacs-quick-startup)
199 (not (and (fboundp 'display-graphic-p)
200 (display-graphic-p)))
201 (not (fboundp 'x-show-tip))))
203 (unless (or (null tooltip-mode) (fboundp 'x-show-tip))
204 (error "Sorry, tooltips are not yet available on this system"))
205 (let ((hook-fn (if tooltip-mode 'add-hook 'remove-hook)))
206 (funcall hook-fn 'change-major-mode-hook 'tooltip-change-major-mode)
207 (tooltip-activate-mouse-motions-if-enabled)
208 (funcall hook-fn 'pre-command-hook 'tooltip-hide)
209 (funcall hook-fn 'tooltip-hook 'tooltip-gud-tips)
210 (funcall hook-fn 'tooltip-hook 'tooltip-help-tips)
211 (setq show-help-function (if tooltip-mode 'tooltip-show-help-function nil))
212 ;; `ignore' is the default binding for mouse movements.
213 (define-key global-map [mouse-movement]
214 (if tooltip-mode 'tooltip-mouse-motion 'ignore))))
217 ;;; Timeout for tooltip display
219 (defun tooltip-delay ()
220 "Return the delay in seconds for the next tooltip."
221 (let ((delay tooltip-delay)
223 (when (and tooltip-hide-time
224 (< (- now tooltip-hide-time) tooltip-recent-seconds))
225 (setq delay tooltip-short-delay))
228 (defun tooltip-cancel-delayed-tip ()
229 "Disable the tooltip timeout."
230 (when tooltip-timeout-id
231 (disable-timeout tooltip-timeout-id)
232 (setq tooltip-timeout-id nil)))
234 (defun tooltip-start-delayed-tip ()
235 "Add a one-shot timeout to call function tooltip-timeout."
236 (setq tooltip-timeout-id
237 (add-timeout (tooltip-delay) 'tooltip-timeout nil)))
239 (defun tooltip-timeout (object)
240 "Function called when timer with id tooltip-timeout-id fires."
241 (run-hook-with-args-until-success 'tooltip-hook
242 tooltip-last-mouse-motion-event))
245 ;;; Reacting on mouse movements
247 (defun tooltip-change-major-mode ()
248 "Function added to `change-major-mode-hook' when tooltip mode is on."
249 (add-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled))
251 (defun tooltip-activate-mouse-motions-if-enabled ()
252 "Reconsider for all buffers whether mouse motion events are desired."
253 (remove-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled)
254 (dolist (buffer (buffer-list))
257 (if (and tooltip-mode
259 (memq major-mode tooltip-gud-modes))
260 (tooltip-activate-mouse-motions t)
261 (tooltip-activate-mouse-motions nil)))))
263 (defvar tooltip-mouse-motions-active nil
264 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
266 (defun tooltip-activate-mouse-motions (activatep)
267 "Activate/deactivate mouse motion events for the current buffer.
268 ACTIVATEP non-nil means activate mouse motion events."
271 (make-local-variable 'tooltip-mouse-motions-active)
272 (setq tooltip-mouse-motions-active t)
273 (make-local-variable 'track-mouse)
274 (setq track-mouse t))
275 (when tooltip-mouse-motions-active
276 (kill-local-variable 'tooltip-mouse-motions-active)
277 (kill-local-variable 'track-mouse))))
279 (defun tooltip-mouse-motion (event)
280 "Command handler for mouse movement events in `global-map'."
283 (when (car (mouse-pixel-position))
284 (setq tooltip-last-mouse-motion-event (copy-sequence event))
285 (tooltip-start-delayed-tip)))
290 (defun tooltip-set-param (alist key value)
291 "Change the value of KEY in alist ALIST to VALUE.
292 If there's no association for KEY in ALIST, add one, otherwise
293 change the existing association. Value is the resulting alist."
294 (let ((param (assq key alist)))
297 (push (cons key value) alist))
300 (defun tooltip-show (text &optional use-echo-area)
301 "Show a tooltip window displaying TEXT.
303 Text larger than `x-max-tooltip-size' is clipped.
305 If the alist in `tooltip-frame-parameters' includes `left' and `top'
306 parameters, they determine the x and y position where the tooltip
307 is displayed. Otherwise, the tooltip pops at offsets specified by
308 `tooltip-x-offset' and `tooltip-y-offset' from the current mouse
311 Optional second arg USE-ECHO-AREA non-nil means to show tooltip
315 (condition-case error
316 (let ((params (copy-sequence tooltip-frame-parameters))
317 (fg (face-attribute 'tooltip :foreground))
318 (bg (face-attribute 'tooltip :background)))
320 (setq params (tooltip-set-param params 'foreground-color fg))
321 (setq params (tooltip-set-param params 'border-color fg)))
323 (setq params (tooltip-set-param params 'background-color bg)))
324 (x-show-tip (propertize text 'face 'tooltip)
331 (message "Error while displaying tooltip: %s" error)
333 (message "%s" text)))))
335 (defun tooltip-hide (&optional ignored-arg)
336 "Hide a tooltip, if one is displayed.
337 Value is non-nil if tooltip was open."
338 (tooltip-cancel-delayed-tip)
340 (setq tooltip-hide-time (float-time))))
343 ;;; Debugger-related functions
345 (defun tooltip-identifier-from-point (point)
346 "Extract the identifier at POINT, if any.
347 Value is nil if no identifier exists at point. Identifier extraction
348 is based on the current syntax table."
351 (let ((start (progn (skip-syntax-backward "w_") (point))))
352 (unless (looking-at "[0-9]")
353 (skip-syntax-forward "w_")
354 (when (> (point) start)
355 (buffer-substring start (point)))))))
357 (defmacro tooltip-region-active-p ()
358 "Value is non-nil if the region is currently active."
359 (if (string-match "^GNU" (emacs-version))
360 `(and transient-mark-mode mark-active)
363 (defun tooltip-expr-to-print (event)
364 "Return an expression that should be printed for EVENT.
365 If a region is active and the mouse is inside the region, print
366 the region. Otherwise, figure out the identifier around the point
369 (set-buffer (tooltip-event-buffer event))
370 (let ((point (posn-point (event-end event))))
371 (if (tooltip-region-active-p)
372 (when (and (<= (region-beginning) point) (<= point (region-end)))
373 (buffer-substring (region-beginning) (region-end)))
374 (tooltip-identifier-from-point point)))))
376 (defun tooltip-process-prompt-regexp (process)
377 "Return regexp matching the prompt of PROCESS at the end of a string.
378 The prompt is taken from the value of COMINT-PROMPT-REGEXP in the buffer
380 (let ((prompt-regexp (save-excursion
381 (set-buffer (process-buffer process))
382 comint-prompt-regexp)))
383 ;; Most start with `^' but the one for `sdb' cannot be easily
384 ;; stripped. Code the prompt for `sdb' fixed here.
385 (if (= (aref prompt-regexp 0) ?^)
386 (setq prompt-regexp (substring prompt-regexp 1))
387 (setq prompt-regexp "\\*"))
388 (concat "\n*" prompt-regexp "$")))
390 (defun tooltip-strip-prompt (process output)
391 "Return OUTPUT with any prompt of PROCESS stripped from its end."
392 (let ((prompt-regexp (tooltip-process-prompt-regexp process)))
394 (when (string-match prompt-regexp output)
395 (setq output (substring output 0 (match-beginning 0)))))
401 (defvar tooltip-gud-original-filter nil
402 "Process filter to restore after GUD output has been received.")
404 (defvar tooltip-gud-dereference nil
405 "Non-nil means print expressions with a `*' in front of them.
406 For C this would dereference a pointer expression.")
408 (defvar tooltip-gud-event nil
409 "The mouse movement event that led to a tooltip display.
410 This event can be examined by forms in TOOLTIP-GUD-DISPLAY.")
412 (defun tooltip-gud-toggle-dereference ()
413 "Toggle whether tooltips should show `* expr' or `expr'."
415 (setq tooltip-gud-dereference (not tooltip-gud-dereference))
416 (when (interactive-p)
417 (message "Dereferencing is now %s."
418 (if tooltip-gud-dereference "on" "off"))))
420 (defun tooltip-toggle-gud-tips ()
421 "Toggle the display of GUD tooltips."
423 (setq tooltip-gud-tips-p (not tooltip-gud-tips-p))
424 ;; Reconsider for all buffers whether mouse motion events are desired.
425 (tooltip-change-major-mode)
426 (when (interactive-p)
427 (message (format "GUD tooltips %sabled"
428 (if tooltip-gud-tips-p "en" "dis")))))
430 ; This will only display data that comes in one chunk.
431 ; Larger arrays (say 400 elements) are displayed in
432 ; the tootip incompletely and spill over into the gud buffer.
433 ; Switching the process-filter creates timing problems and
434 ; it may be difficult to do better. Using annotations as in
435 ; gdb-ui.el gets round this problem.
436 (defun tooltip-gud-process-output (process output)
437 "Process debugger output and show it in a tooltip window."
438 (set-process-filter process tooltip-gud-original-filter)
439 (tooltip-show (tooltip-strip-prompt process output)
440 tooltip-gud-echo-area))
442 (defun tooltip-gud-print-command (expr)
443 "Return a suitable command to print the expression EXPR.
444 If TOOLTIP-GUD-DEREFERENCE is t, also prepend a `*' to EXPR."
445 (when tooltip-gud-dereference
446 (setq expr (concat "*" expr)))
448 ((gdb gdba) (concat "server print " expr))
449 (dbx (concat "print " expr))
450 (xdb (concat "p " expr))
451 (sdb (concat expr "/"))
454 (defun tooltip-gud-tips (event)
455 "Show tip for identifier or selection under the mouse.
456 The mouse must either point at an identifier or inside a selected
457 region for the tip window to be shown. If tooltip-gud-dereference is t,
458 add a `*' in front of the printed expression. In the case of a C program
459 controlled by GDB, show the associated #define directives when program is
462 This function must return nil if it doesn't handle EVENT."
464 (when (and (eventp event)
466 (boundp 'gud-comint-buffer)
468 (buffer-name gud-comint-buffer); gud-comint-buffer might be killed
469 (setq process (get-buffer-process gud-comint-buffer))
470 (posn-point (event-end event))
471 (or (eq gud-minor-mode 'gdba)
472 (progn (setq tooltip-gud-event event)
473 (eval (cons 'and tooltip-gud-display)))))
474 (let ((expr (tooltip-expr-to-print event)))
476 (if (and (eq gud-minor-mode 'gdba)
477 (not gdb-active-process))
480 (window-buffer (let ((mouse (mouse-position)))
481 (window-at (cadr mouse)
483 (let ((define-elt (assoc expr gdb-define-alist)))
484 (unless (null define-elt)
485 (tooltip-show (cdr define-elt))
487 (let ((cmd (tooltip-gud-print-command expr)))
488 (unless (null cmd) ; CMD can be nil if unknown debugger
490 (gdba (gdb-enqueue-input
491 (list (concat cmd "\n") 'gdb-tooltip-print)))
493 (setq tooltip-gud-original-filter (process-filter process))
494 (set-process-filter process 'tooltip-gud-process-output)
495 (gud-basic-call cmd)))
498 (defun gdb-tooltip-print ()
500 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
501 (let ((string (buffer-string)))
502 ;; remove newline for tooltip-gud-echo-area
503 (substring string 0 (- (length string) 1))))
504 tooltip-gud-echo-area))
509 (defvar tooltip-help-message nil
510 "The last help message received via `tooltip-show-help-function'.")
512 (defun tooltip-show-help-function (msg)
513 "Function installed as `show-help-function'.
514 MSG is either a help string to display, or nil to cancel the display."
515 (let ((previous-help tooltip-help-message))
516 (setq tooltip-help-message msg)
518 ;; Cancel display. This also cancels a delayed tip, if
521 ((equal previous-help msg)
522 ;; Same help as before (but possibly the mouse has moved).
523 ;; Keep what we have.
526 ;; A different help. Remove a previous tooltip, and
527 ;; display a new one, with some delay.
529 (tooltip-start-delayed-tip)))))
531 (defun tooltip-help-tips (event)
532 "Hook function to display a help tooltip.
533 This is installed on the hook `tooltip-hook', which is run when
534 the timer with ID `tooltip-timeout-id' fires.
535 Value is non-nil if this function handled the tip."
536 (when (stringp tooltip-help-message)
537 (tooltip-show tooltip-help-message)
542 ;; arch-tag: 3d61135e-4618-4a78-af28-183f6df5636f
543 ;;; tooltip.el ends here