1 ;;; tooltip.el --- Show tooltip windows
3 ;; Copyright (C) 1997, 1999 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-frame-parameters
81 (foreground-color .
"black")
82 (background-color .
"lightyellow")
83 (internal-border-width .
5)
84 (border-color .
"lightyellow")
86 "Frame parameters used for tooltips."
88 :tag
"Frame Parameters"
92 (defcustom tooltip-gud-tips-p nil
93 "*Non-nil means show tooltips in GUD sessions."
99 (defcustom tooltip-gud-modes
'(gud-mode c-mode c
++-mode
)
100 "List of modes for which to enable GUD tips."
106 (defcustom tooltip-gud-display
107 '((eq (tooltip-event-buffer tooltip-gud-event
)
108 (marker-buffer overlay-arrow-position
)))
109 "List of forms determining where GUD tooltips are displayed.
111 Forms in the list are combined with AND. The default is to display
112 only tooltips in the buffer containing the overlay arrow."
114 :tag
"GUD buffers predicate"
119 ;;; Variables that are not customizable.
121 (defvar tooltip-hook nil
122 "Functions to call to display tooltips.
123 Each function is called with one argument EVENT which is a copy of
124 the last mouse movement event that occurred.")
127 (defvar tooltip-timeout-id nil
128 "The id of the timeout started when Emacs becomes idle.")
131 (defvar tooltip-last-mouse-motion-event nil
132 "A copy of the last mouse motion event seen.")
135 (defvar tooltip-hide-time nil
136 "Time when the last tooltip was hidden.")
139 (defvar tooltip-gud-debugger nil
140 "The debugger for which we show tooltips.")
146 (defun tooltip-event-buffer (event)
147 "Return the buffer over which event EVENT occurred.
148 This might return nil if the event did not occur over a buffer."
149 (let ((window (posn-window (event-end event
))))
150 (and window
(window-buffer window
))))
154 ;;; Switching tooltips on/off
156 ;; We don't set track-mouse globally because this is a big redisplay
157 ;; problem in buffers having a pre-command-hook or such installed,
158 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
159 ;; set-buffer prevents redisplay optimizations, so every mouse motion
160 ;; would be accompanied by a full redisplay.
163 (defun tooltip-mode (&optional arg
)
164 "Mode for tooltip display.
165 With ARG, turn tooltip mode on if and only if ARG is positive."
168 (> (prefix-numeric-value arg
) 0)
170 (hook-fn (if on
'add-hook
'remove-hook
)))
171 (setq tooltip-mode on
)
172 (funcall hook-fn
'change-major-mode-hook
'tooltip-change-major-mode
)
173 (tooltip-activate-mouse-motions-if-enabled)
174 (funcall hook-fn
'pre-command-hook
'tooltip-hide
)
175 (funcall hook-fn
'tooltip-hook
'tooltip-gud-tips
)
176 (funcall hook-fn
'tooltip-hook
'tooltip-help-tips
)
177 (setq show-help-function
(if on
'tooltip-show-help-function nil
))
178 ;; `ignore' is the default binding for mouse movements.
179 (define-key global-map
[mouse-movement
]
180 (if on
'tooltip-mouse-motion
'ignore
))
181 (when (and on tooltip-gud-tips-p
)
182 (global-set-key [S-mouse-3
] 'tooltip-gud-toggle-dereference
)
183 (add-hook 'gdb-mode-hook
184 #'(lambda () (setq tooltip-gud-debugger
'gdb
)))
185 (add-hook 'sdb-mode-hook
186 #'(lambda () (setq tooltip-gud-debugger
'sdb
)))
187 (add-hook 'dbx-mode-hook
188 #'(lambda () (setq tooltip-gud-debugger
'dbx
)))
189 (add-hook 'xdb-mode-hook
190 #'(lambda () (setq tooltip-gud-debugger
'xdb
)))
191 (add-hook 'perldb-mode-hook
192 #'(lambda () (setq tooltip-gud-debugger
'perldb
))))))
196 ;;; Timeout for tooltip display
198 (defun tooltip-delay ()
199 "Return the delay in seconds for the next tooltip."
200 (let ((delay tooltip-delay
)
202 (when (and tooltip-hide-time
203 (< (- now tooltip-hide-time
) tooltip-recent-seconds
))
204 (setq delay tooltip-short-delay
))
208 (defun tooltip-disable-timeout ()
209 "Disable the tooltip timeout."
210 (when tooltip-timeout-id
211 (disable-timeout tooltip-timeout-id
)
212 (setq tooltip-timeout-id nil
)))
215 (defun tooltip-add-timeout ()
216 "Add a one-shot timeout to call function tooltip-timeout."
217 (setq tooltip-timeout-id
218 (add-timeout (tooltip-delay) 'tooltip-timeout nil
)))
221 (defun tooltip-timeout (object)
222 "Function called when timer with id tooltip-timeout-id fires."
223 (run-hook-with-args-until-success 'tooltip-hook
224 tooltip-last-mouse-motion-event
))
228 ;;; Reacting on mouse movements
230 (defun tooltip-change-major-mode ()
231 "Function added to `change-major-mode-hook' when tooltip mode is on."
232 (add-hook 'post-command-hook
'tooltip-activate-mouse-motions-if-enabled
))
235 (defun tooltip-activate-mouse-motions-if-enabled ()
236 "Reconsider for all buffers whether mouse motion events are desired."
237 (remove-hook 'post-command-hook
'tooltip-activate-mouse-motions-if-enabled
)
238 (let ((buffers (buffer-list)))
241 (set-buffer (car buffers
))
242 (if (and tooltip-mode
244 (memq major-mode tooltip-gud-modes
))
245 (tooltip-activate-mouse-motions t
)
246 (tooltip-activate-mouse-motions nil
))
247 (setq buffers
(cdr buffers
))))))
250 (defun tooltip-activate-mouse-motions (activatep)
251 "Activate/deactivate mouse motion events for the current buffer.
252 ACTIVATEP non-nil means activate mouse motion events."
255 (make-local-variable 'track-mouse
)
256 (setq track-mouse t
))
257 (kill-local-variable 'track-mouse
)))
260 (defun tooltip-mouse-motion (event)
261 "Command handler for mouse movement events in `global-map'."
264 (when (car (mouse-pixel-position))
265 (setq tooltip-last-mouse-motion-event
(copy-sequence event
))
266 (tooltip-add-timeout)))
272 (defun tooltip-show (text)
273 "Show a tooltip window at the current mouse position displaying TEXT."
274 (x-show-tip text
(selected-frame) tooltip-frame-parameters
))
277 (defun tooltip-hide (&optional ignored-arg
)
278 "Hide a tooltip, if one is displayed.
279 Value is non-nil if tooltip was open."
280 (tooltip-disable-timeout)
282 (setq tooltip-hide-time
(float-time))))
286 ;;; Debugger-related functions
288 (defun tooltip-identifier-from-point (point)
289 "Extract the identifier at POINT, if any.
290 Value is nil if no identifier exists at point. Identifier extraction
291 is based on the current syntax table."
294 (let ((start (progn (skip-syntax-backward "w_") (point))))
295 (unless (looking-at "[0-9]")
296 (skip-syntax-forward "w_")
297 (when (> (point) start
)
298 (buffer-substring start
(point)))))))
301 (defmacro tooltip-region-active-p
()
302 "Value is non-nil if the region is currently active."
303 (if (string-match "^GNU" (emacs-version))
304 `(and transient-mark-mode mark-active
)
308 (defun tooltip-expr-to-print (event)
309 "Return an expression that should be printed for EVENT.
310 If a region is active and the mouse is inside the region, print
311 the region. Otherwise, figure out the identifier around the point
314 (set-buffer (tooltip-event-buffer event
))
315 (let ((point (posn-point (event-end event
))))
316 (if (tooltip-region-active-p)
317 (when (and (<= (region-beginning) point
) (<= point
(region-end)))
318 (buffer-substring (region-beginning) (region-end)))
319 (tooltip-identifier-from-point point
)))))
322 (defun tooltip-process-prompt-regexp (process)
323 "Return regexp matching the prompt of PROCESS at the end of a string.
324 The prompt is taken from the value of COMINT-PROMPT-REGEXP in the buffer
326 (let ((prompt-regexp (save-excursion
327 (set-buffer (process-buffer process
))
328 comint-prompt-regexp
)))
329 ;; Most start with `^' but the one for `sdb' cannot be easily
330 ;; stripped. Code the prompt for `sdb' fixed here.
331 (if (= (aref prompt-regexp
0) ?^
)
332 (setq prompt-regexp
(substring prompt-regexp
1))
333 (setq prompt-regexp
"\\*"))
334 (concat "\n*" prompt-regexp
"$")))
337 (defun tooltip-strip-prompt (process output
)
338 "Return OUTPUT with any prompt of PROCESS stripped from its end."
339 (let ((prompt-regexp (tooltip-process-prompt-regexp process
)))
341 (when (string-match prompt-regexp output
)
342 (setq output
(substring output
0 (match-beginning 0)))))
349 (defvar tooltip-gud-original-filter nil
350 "Process filter to restore after GUD output has been received.")
353 (defvar tooltip-gud-dereference nil
354 "Non-nil means print expressions with a `*' in front of them.
355 For C this would dereference a pointer expression.")
358 (defvar tooltip-gud-event nil
359 "The mouse movement event that led to a tooltip display.
360 This event can be examined by forms in TOOLTIP-GUD-DISPLAY.")
363 (defvar tooltip-gud-debugger nil
364 "A symbol describing the debugger running under GUD.")
367 (defun tooltip-gud-toggle-dereference ()
368 "Toggle whether tooltips should show `* expr' or `expr'."
370 (setq tooltip-gud-dereference
(not tooltip-gud-dereference
))
371 (when (interactive-p)
372 (message "Dereferencing is now %s."
373 (if tooltip-gud-dereference
"on" "off"))))
376 (defun tooltip-gud-process-output (process output
)
377 "Process debugger output and show it in a tooltip window."
378 (set-process-filter process tooltip-gud-original-filter
)
379 (tooltip-show (tooltip-strip-prompt process output
)))
382 (defun tooltip-gud-print-command (expr)
383 "Return a suitable command to print the expression EXPR.
384 If TOOLTIP-GUD-DEREFERENCE is t, also prepend a `*' to EXPR."
385 (when tooltip-gud-dereference
386 (setq expr
(concat "*" expr
)))
387 (case tooltip-gud-debugger
388 ((gdb dbx
) (concat "print " expr
))
389 (xdb (concat "p " expr
))
390 (sdb (concat expr
"/"))
394 (defun tooltip-gud-tips (event)
395 "Show tip for identifier or selection under the mouse.
396 The mouse must either point at an identifier or inside a selected
397 region for the tip window to be shown. If tooltip-gud-dereference is t,
398 add a `*' in front of the printed expression.
400 This function must return nil if it doesn't handle EVENT."
401 (let (gud-buffer process
)
402 (when (and (eventp event
)
404 (boundp 'gud-comint-buffer
)
405 (setq gud-buffer gud-comint-buffer
)
406 (setq process
(get-buffer-process gud-buffer
))
407 (posn-point (event-end event
))
408 (progn (setq tooltip-gud-event event
)
409 (eval (cons 'and tooltip-gud-display
))))
410 (let ((expr (tooltip-expr-to-print event
)))
412 (setq tooltip-gud-original-filter
(process-filter process
))
413 (set-process-filter process
'tooltip-gud-process-output
)
415 process
(concat (tooltip-gud-print-command expr
) "\n"))
422 (defvar tooltip-help-message nil
423 "The last help message received via `tooltip-show-help-function'.")
426 (defun tooltip-show-help-function (msg)
427 "Function installed as `show-help-function'.
428 MSG is either a help string to display, or nil to cancel the display."
429 (let ((previous-help tooltip-help-message
))
430 (setq tooltip-help-message msg
)
433 ((or (not (stringp previous-help
))
434 (not (string= msg previous-help
)))
436 (tooltip-add-timeout))
438 (tooltip-disable-timeout)
439 (tooltip-add-timeout)))))
442 (defun tooltip-help-tips (event)
443 "Hook function to display a help tooltip.
444 Value is non-nil if this function handled the tip."
445 (when (stringp tooltip-help-message
)
446 (tooltip-show tooltip-help-message
)
447 (setq tooltip-help-message nil
)
452 ;;; Do this after all functions have been defined that are called
453 ;;; from `tooltip-mode'.
457 (defcustom tooltip-mode nil
458 "Toggle tooltip-mode.
459 Setting this variable directly does not take effect;
460 use either \\[customize] or the function `tooltip-mode'."
461 :set
(lambda (symbol value
)
462 (tooltip-mode (or value
0)))
463 :initialize
'custom-initialize-default
469 ;;; tooltip.el ends here