(tooltip-gud-tips-setup): Remove.
[bpt/emacs.git] / lisp / tooltip.el
1 ;;; tooltip.el --- show tooltip windows
2
3 ;; Copyright (C) 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 ;; Author: Gerd Moellmann <gerd@acm.org>
6 ;; Keywords: help c mouse tools
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30 (require 'cl)
31 (require 'comint)
32 (require 'gud))
33
34 (provide 'tooltip)
35
36 \f
37 ;;; Customizable settings
38
39 (defgroup tooltip nil
40 "Customization group for the `tooltip' package."
41 :group 'help
42 :group 'gud
43 :group 'mouse
44 :group 'tools
45 :version "21.1"
46 :tag "Tool Tips")
47
48 (defvar tooltip-mode)
49
50 (defcustom tooltip-delay 0.7
51 "Seconds to wait before displaying a tooltip the first time."
52 :tag "Delay"
53 :type 'number
54 :group 'tooltip)
55
56
57 (defcustom tooltip-short-delay 0.1
58 "Seconds to wait between subsequent tooltips on different items."
59 :tag "Short delay"
60 :type 'number
61 :group 'tooltip)
62
63
64 (defcustom tooltip-recent-seconds 1
65 "Display tooltips if changing tip items within this many seconds.
66 Do so after `tooltip-short-delay'."
67 :tag "Recent seconds"
68 :type 'number
69 :group 'tooltip)
70
71
72 (defcustom tooltip-hide-delay 10
73 "Hide tooltips automatically after this many seconds."
74 :tag "Hide delay"
75 :type 'number
76 :group 'tooltip)
77
78
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
84 pixels.
85
86 If `tooltip-frame-parameters' includes the `left' parameter,
87 the value of `tooltip-x-offset' is ignored."
88 :tag "X offset"
89 :type '(choice (const :tag "Default" nil)
90 (integer :tag "Offset" :value 1))
91 :group 'tooltip)
92
93
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
99 pixels.
100
101 If `tooltip-frame-parameters' includes the `top' parameter,
102 the value of `tooltip-y-offset' is ignored."
103 :tag "Y offset"
104 :type '(choice (const :tag "Default" nil)
105 (integer :tag "Offset" :value 1))
106 :group 'tooltip)
107
108
109 (defcustom tooltip-frame-parameters
110 '((name . "tooltip")
111 (internal-border-width . 5)
112 (border-width . 1))
113 "Frame parameters used for tooltips.
114
115 If `left' or `top' parameters are included, they specify the absolute
116 position to pop up the tooltip."
117 :type 'sexp
118 :tag "Frame Parameters"
119 :group 'tooltip)
120
121
122 (defface tooltip
123 '((((class color))
124 (:background "lightyellow" :foreground "black"))
125 (t ()))
126 "Face for tooltips."
127 :group 'tooltip)
128
129
130 (defcustom tooltip-gud-tips-p nil
131 "*Non-nil means show tooltips in GUD sessions."
132 :type 'boolean
133 :tag "GUD"
134 :set #'(lambda (symbol on)
135 (setq tooltip-gud-tips-p on))
136 :group 'tooltip)
137
138
139 (defcustom tooltip-gud-modes '(gud-mode c-mode c++-mode)
140 "List of modes for which to enable GUD tips."
141 :type 'sexp
142 :tag "GUD modes"
143 :group 'tooltip)
144
145
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.
150
151 Forms in the list are combined with AND. The default is to display
152 only tooltips in the buffer containing the overlay arrow."
153 :type 'sexp
154 :tag "GUD buffers predicate"
155 :group 'tooltip)
156
157
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."
162 :type 'boolean
163 :tag "Use echo area"
164 :group 'tooltip)
165
166 \f
167 ;;; Variables that are not customizable.
168
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.")
173
174
175 (defvar tooltip-timeout-id nil
176 "The id of the timeout started when Emacs becomes idle.")
177
178
179 (defvar tooltip-last-mouse-motion-event nil
180 "A copy of the last mouse motion event seen.")
181
182
183 (defvar tooltip-hide-time nil
184 "Time when the last tooltip was hidden.")
185
186
187 \f
188 ;;; Event accessors
189
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))))
195
196
197 \f
198 ;;; Switching tooltips on/off
199
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.
205
206 ;;;###autoload
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."
210 (interactive "P")
211 (unless (fboundp 'x-show-tip)
212 (error "Sorry, tooltips are not yet available on this system"))
213 (let* ((on (if arg
214 (> (prefix-numeric-value arg) 0)
215 (not tooltip-mode)))
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))))
227
228
229 \f
230 ;;; Timeout for tooltip display
231
232 (defun tooltip-delay ()
233 "Return the delay in seconds for the next tooltip."
234 (let ((delay tooltip-delay)
235 (now (float-time)))
236 (when (and tooltip-hide-time
237 (< (- now tooltip-hide-time) tooltip-recent-seconds))
238 (setq delay tooltip-short-delay))
239 delay))
240
241
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)))
247
248
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)))
253
254
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))
259
260
261 \f
262 ;;; Reacting on mouse movements
263
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))
267
268
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)))
273 (save-excursion
274 (while buffers
275 (set-buffer (car buffers))
276 (if (and tooltip-mode
277 tooltip-gud-tips-p
278 (memq major-mode tooltip-gud-modes))
279 (tooltip-activate-mouse-motions t)
280 (tooltip-activate-mouse-motions nil))
281 (setq buffers (cdr buffers))))))
282
283 (defvar tooltip-mouse-motions-active nil
284 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
285
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."
289 (if activatep
290 (progn
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))))
298
299 (defun tooltip-mouse-motion (event)
300 "Command handler for mouse movement events in `global-map'."
301 (interactive "e")
302 (tooltip-hide)
303 (when (car (mouse-pixel-position))
304 (setq tooltip-last-mouse-motion-event (copy-sequence event))
305 (tooltip-start-delayed-tip)))
306
307
308 \f
309 ;;; Displaying tips
310
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)))
316 (if (consp param)
317 (setcdr param value)
318 (push (cons key value) alist))
319 alist))
320
321
322 (defun tooltip-show (text)
323 "Show a tooltip window displaying TEXT.
324
325 Text larger than `x-max-tooltip-size' (which see) is clipped.
326
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
331 position."
332 (if tooltip-use-echo-area
333 (message "%s" text)
334 (condition-case error
335 (let ((params (copy-sequence tooltip-frame-parameters))
336 (fg (face-attribute 'tooltip :foreground))
337 (bg (face-attribute 'tooltip :background)))
338 (when (stringp fg)
339 (setq params (tooltip-set-param params 'foreground-color fg))
340 (setq params (tooltip-set-param params 'border-color fg)))
341 (when (stringp bg)
342 (setq params (tooltip-set-param params 'background-color bg)))
343 (x-show-tip (propertize text 'face 'tooltip)
344 (selected-frame)
345 params
346 tooltip-hide-delay
347 tooltip-x-offset
348 tooltip-y-offset))
349 (error
350 (message "Error while displaying tooltip: %s" error)
351 (sit-for 1)
352 (message "%s" text)))))
353
354
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)
359 (when (x-hide-tip)
360 (setq tooltip-hide-time (float-time))))
361
362
363 \f
364 ;;; Debugger-related functions
365
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."
370 (save-excursion
371 (goto-char point)
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)))))))
377
378
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)
383 `(region-active-p)))
384
385
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
390 where the mouse is."
391 (save-excursion
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)))))
398
399
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
403 of PROCESS."
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 "$")))
413
414
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)))
418 (save-match-data
419 (when (string-match prompt-regexp output)
420 (setq output (substring output 0 (match-beginning 0)))))
421 output))
422
423
424 \f
425 ;;; Tips for `gud'
426
427 (defvar tooltip-gud-original-filter nil
428 "Process filter to restore after GUD output has been received.")
429
430
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.")
434
435
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.")
439
440
441 (defun tooltip-gud-toggle-dereference ()
442 "Toggle whether tooltips should show `* expr' or `expr'."
443 (interactive)
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"))))
448
449
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)))
454
455
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)))
461 (case gud-minor-mode
462 ((gdb gdba) (concat "server print " expr))
463 (dbx (concat "print " expr))
464 (xdb (concat "p " expr))
465 (sdb (concat expr "/"))
466 (perldb expr)))
467
468
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.
474
475 This function must return nil if it doesn't handle EVENT."
476 (let (gud-buffer process)
477 (when (and (eventp event)
478 tooltip-gud-tips-p
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)))
486 (when expr
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)
491 (gud-basic-call cmd)
492 expr)))))))
493
494 \f
495 ;;; Tooltip help.
496
497 (defvar tooltip-help-message nil
498 "The last help message received via `tooltip-show-help-function'.")
499
500
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)
506 (cond ((null msg)
507 ;; Cancel display. This also cancels a delayed tip, if
508 ;; there is one.
509 (tooltip-hide))
510 ((equal previous-help msg)
511 ;; Same help as before (but possibly the mouse has moved).
512 ;; Keep what we have.
513 )
514 (t
515 ;; A different help. Remove a previous tooltip, and
516 ;; display a new one, with some delay.
517 (tooltip-hide)
518 (tooltip-start-delayed-tip)))))
519
520
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)
528 t))
529
530
531 \f
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
534 ;;; in startup.el.
535
536 ;;;###autoload
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
544 :type 'boolean
545 :require 'tooltip
546 :group 'tooltip)
547
548
549 ;;; tooltip.el ends here