Merged from miles@gnu.org--gnu-2005 (patch 67, 270-278)
[bpt/emacs.git] / lisp / tooltip.el
1 ;;; tooltip.el --- show tooltip windows
2
3 ;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Gerd Moellmann <gerd@acm.org>
7 ;; Keywords: help c mouse tools
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl)) ; for case macro
31
32 \f
33 ;;; Customizable settings
34
35 (defgroup tooltip nil
36 "Customization group for the `tooltip' package."
37 :group 'help
38 :group 'gud
39 :group 'mouse
40 :group 'tools
41 :version "21.1"
42 :tag "Tool Tips")
43
44 (defcustom tooltip-delay 0.7
45 "Seconds to wait before displaying a tooltip the first time."
46 :tag "Delay"
47 :type 'number
48 :group 'tooltip)
49
50 (defcustom tooltip-short-delay 0.1
51 "Seconds to wait between subsequent tooltips on different items."
52 :tag "Short delay"
53 :type 'number
54 :group 'tooltip)
55
56 (defcustom tooltip-recent-seconds 1
57 "Display tooltips if changing tip items within this many seconds.
58 Do so after `tooltip-short-delay'."
59 :tag "Recent seconds"
60 :type 'number
61 :group 'tooltip)
62
63 (defcustom tooltip-hide-delay 10
64 "Hide tooltips automatically after this many seconds."
65 :tag "Hide delay"
66 :type 'number
67 :group 'tooltip)
68
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
74 pixels.
75
76 If `tooltip-frame-parameters' includes the `left' parameter,
77 the value of `tooltip-x-offset' is ignored."
78 :tag "X offset"
79 :type '(choice (const :tag "Default" nil)
80 (integer :tag "Offset" :value 1))
81 :group 'tooltip)
82
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
88 pixels.
89
90 If `tooltip-frame-parameters' includes the `top' parameter,
91 the value of `tooltip-y-offset' is ignored."
92 :tag "Y offset"
93 :type '(choice (const :tag "Default" nil)
94 (integer :tag "Offset" :value 1))
95 :group 'tooltip)
96
97 (defcustom tooltip-frame-parameters
98 '((name . "tooltip")
99 (internal-border-width . 5)
100 (border-width . 1))
101 "Frame parameters used for tooltips.
102
103 If `left' or `top' parameters are included, they specify the absolute
104 position to pop up the tooltip."
105 :type 'sexp
106 :tag "Frame Parameters"
107 :group 'tooltip)
108
109 (defface tooltip
110 '((((class color))
111 :background "lightyellow"
112 :foreground "black"
113 :inherit variable-pitch)
114 (t
115 :inherit variable-pitch))
116 "Face for tooltips."
117 :group 'tooltip)
118
119 (defcustom tooltip-gud-tips-p nil
120 "*Non-nil means show tooltips in GUD sessions.
121
122 This allows you to display a variable's value in a tooltip simply by
123 pointing at it with the mouse."
124 :type 'boolean
125 :tag "GUD"
126 :group 'tooltip)
127
128 (defcustom tooltip-gud-modes '(gud-mode c-mode c++-mode fortran-mode)
129 "List of modes for which to enable GUD tips."
130 :type 'sexp
131 :tag "GUD modes"
132 :group 'tooltip)
133
134 (defcustom tooltip-gud-display
135 '((eq (tooltip-event-buffer tooltip-gud-event)
136 (marker-buffer gud-overlay-arrow-position)))
137 "List of forms determining where GUD tooltips are displayed.
138
139 Forms in the list are combined with AND. The default is to display
140 only tooltips in the buffer containing the overlay arrow."
141 :type 'sexp
142 :tag "GUD buffers predicate"
143 :group 'tooltip)
144
145 (defcustom tooltip-gud-echo-area nil
146 "Use the echo area instead of frames for GUD tooltips."
147 :type 'boolean
148 :tag "Use echo area"
149 :group 'tooltip)
150
151 (defvaralias 'tooltip-use-echo-area 'tooltip-gud-echo-area)
152 (make-obsolete-variable 'tooltip-use-echo-area 'tooltip-gud-echo-area "22.1")
153 \f
154 ;;; Variables that are not customizable.
155
156 (defvar tooltip-hook nil
157 "Functions to call to display tooltips.
158 Each function is called with one argument EVENT which is a copy of
159 the last mouse movement event that occurred.")
160
161 (defvar tooltip-timeout-id nil
162 "The id of the timeout started when Emacs becomes idle.")
163
164 (defvar tooltip-last-mouse-motion-event nil
165 "A copy of the last mouse motion event seen.")
166
167 (defvar tooltip-hide-time nil
168 "Time when the last tooltip was hidden.")
169
170 \f
171 ;;; Event accessors
172
173 (defun tooltip-event-buffer (event)
174 "Return the buffer over which event EVENT occurred.
175 This might return nil if the event did not occur over a buffer."
176 (let ((window (posn-window (event-end event))))
177 (and window (window-buffer window))))
178
179 \f
180 ;;; Switching tooltips on/off
181
182 ;; We don't set track-mouse globally because this is a big redisplay
183 ;; problem in buffers having a pre-command-hook or such installed,
184 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
185 ;; set-buffer prevents redisplay optimizations, so every mouse motion
186 ;; would be accompanied by a full redisplay.
187
188 ;;;###autoload
189 (define-minor-mode tooltip-mode
190 "Toggle Tooltip display.
191 With ARG, turn tooltip mode on if and only if ARG is positive."
192 :global t
193 ;; If you change the :init-value below, you also need to change the
194 ;; corresponding code in startup.el.
195 :init-value (not (or noninteractive
196 (and (boundp 'emacs-quick-startup) emacs-quick-startup)
197 (not (and (fboundp 'display-graphic-p)
198 (display-graphic-p)))
199 (not (fboundp 'x-show-tip))))
200 :group 'tooltip
201 (unless (or (null tooltip-mode) (fboundp 'x-show-tip))
202 (error "Sorry, tooltips are not yet available on this system"))
203 (let ((hook-fn (if tooltip-mode 'add-hook 'remove-hook)))
204 (funcall hook-fn 'change-major-mode-hook 'tooltip-change-major-mode)
205 (tooltip-activate-mouse-motions-if-enabled)
206 (funcall hook-fn 'pre-command-hook 'tooltip-hide)
207 (funcall hook-fn 'tooltip-hook 'tooltip-gud-tips)
208 (funcall hook-fn 'tooltip-hook 'tooltip-help-tips)
209 (setq show-help-function (if tooltip-mode 'tooltip-show-help-function nil))
210 ;; `ignore' is the default binding for mouse movements.
211 (define-key global-map [mouse-movement]
212 (if tooltip-mode 'tooltip-mouse-motion 'ignore))))
213
214 \f
215 ;;; Timeout for tooltip display
216
217 (defun tooltip-delay ()
218 "Return the delay in seconds for the next tooltip."
219 (let ((delay tooltip-delay)
220 (now (float-time)))
221 (when (and tooltip-hide-time
222 (< (- now tooltip-hide-time) tooltip-recent-seconds))
223 (setq delay tooltip-short-delay))
224 delay))
225
226 (defun tooltip-cancel-delayed-tip ()
227 "Disable the tooltip timeout."
228 (when tooltip-timeout-id
229 (disable-timeout tooltip-timeout-id)
230 (setq tooltip-timeout-id nil)))
231
232 (defun tooltip-start-delayed-tip ()
233 "Add a one-shot timeout to call function tooltip-timeout."
234 (setq tooltip-timeout-id
235 (add-timeout (tooltip-delay) 'tooltip-timeout nil)))
236
237 (defun tooltip-timeout (object)
238 "Function called when timer with id tooltip-timeout-id fires."
239 (run-hook-with-args-until-success 'tooltip-hook
240 tooltip-last-mouse-motion-event))
241
242 \f
243 ;;; Reacting on mouse movements
244
245 (defun tooltip-change-major-mode ()
246 "Function added to `change-major-mode-hook' when tooltip mode is on."
247 (add-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled))
248
249 (defun tooltip-activate-mouse-motions-if-enabled ()
250 "Reconsider for all buffers whether mouse motion events are desired."
251 (remove-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled)
252 (dolist (buffer (buffer-list))
253 (save-excursion
254 (set-buffer buffer)
255 (if (and tooltip-mode
256 tooltip-gud-tips-p
257 (memq major-mode tooltip-gud-modes))
258 (tooltip-activate-mouse-motions t)
259 (tooltip-activate-mouse-motions nil)))))
260
261 (defvar tooltip-mouse-motions-active nil
262 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
263
264 (defun tooltip-activate-mouse-motions (activatep)
265 "Activate/deactivate mouse motion events for the current buffer.
266 ACTIVATEP non-nil means activate mouse motion events."
267 (if activatep
268 (progn
269 (make-local-variable 'tooltip-mouse-motions-active)
270 (setq tooltip-mouse-motions-active t)
271 (make-local-variable 'track-mouse)
272 (setq track-mouse t))
273 (when tooltip-mouse-motions-active
274 (kill-local-variable 'tooltip-mouse-motions-active)
275 (kill-local-variable 'track-mouse))))
276
277 (defun tooltip-mouse-motion (event)
278 "Command handler for mouse movement events in `global-map'."
279 (interactive "e")
280 (tooltip-hide)
281 (when (car (mouse-pixel-position))
282 (setq tooltip-last-mouse-motion-event (copy-sequence event))
283 (tooltip-start-delayed-tip)))
284
285 \f
286 ;;; Displaying tips
287
288 (defun tooltip-set-param (alist key value)
289 "Change the value of KEY in alist ALIST to VALUE.
290 If there's no association for KEY in ALIST, add one, otherwise
291 change the existing association. Value is the resulting alist."
292 (let ((param (assq key alist)))
293 (if (consp param)
294 (setcdr param value)
295 (push (cons key value) alist))
296 alist))
297
298 (defun tooltip-show (text &optional use-echo-area)
299 "Show a tooltip window displaying TEXT.
300
301 Text larger than `x-max-tooltip-size' is clipped.
302
303 If the alist in `tooltip-frame-parameters' includes `left' and `top'
304 parameters, they determine the x and y position where the tooltip
305 is displayed. Otherwise, the tooltip pops at offsets specified by
306 `tooltip-x-offset' and `tooltip-y-offset' from the current mouse
307 position.
308
309 Optional second arg USE-ECHO-AREA non-nil means to show tooltip
310 in echo area."
311 (if use-echo-area
312 (message "%s" text)
313 (condition-case error
314 (let ((params (copy-sequence tooltip-frame-parameters))
315 (fg (face-attribute 'tooltip :foreground))
316 (bg (face-attribute 'tooltip :background)))
317 (when (stringp fg)
318 (setq params (tooltip-set-param params 'foreground-color fg))
319 (setq params (tooltip-set-param params 'border-color fg)))
320 (when (stringp bg)
321 (setq params (tooltip-set-param params 'background-color bg)))
322 (x-show-tip (propertize text 'face 'tooltip)
323 (selected-frame)
324 params
325 tooltip-hide-delay
326 tooltip-x-offset
327 tooltip-y-offset))
328 (error
329 (message "Error while displaying tooltip: %s" error)
330 (sit-for 1)
331 (message "%s" text)))))
332
333 (defun tooltip-hide (&optional ignored-arg)
334 "Hide a tooltip, if one is displayed.
335 Value is non-nil if tooltip was open."
336 (tooltip-cancel-delayed-tip)
337 (when (x-hide-tip)
338 (setq tooltip-hide-time (float-time))))
339
340 \f
341 ;;; Debugger-related functions
342
343 (defun tooltip-identifier-from-point (point)
344 "Extract the identifier at POINT, if any.
345 Value is nil if no identifier exists at point. Identifier extraction
346 is based on the current syntax table."
347 (save-excursion
348 (goto-char point)
349 (let ((start (progn (skip-syntax-backward "w_") (point))))
350 (unless (looking-at "[0-9]")
351 (skip-syntax-forward "w_")
352 (when (> (point) start)
353 (buffer-substring start (point)))))))
354
355 (defmacro tooltip-region-active-p ()
356 "Value is non-nil if the region is currently active."
357 (if (string-match "^GNU" (emacs-version))
358 `(and transient-mark-mode mark-active)
359 `(region-active-p)))
360
361 (defun tooltip-expr-to-print (event)
362 "Return an expression that should be printed for EVENT.
363 If a region is active and the mouse is inside the region, print
364 the region. Otherwise, figure out the identifier around the point
365 where the mouse is."
366 (save-excursion
367 (set-buffer (tooltip-event-buffer event))
368 (let ((point (posn-point (event-end event))))
369 (if (tooltip-region-active-p)
370 (when (and (<= (region-beginning) point) (<= point (region-end)))
371 (buffer-substring (region-beginning) (region-end)))
372 (tooltip-identifier-from-point point)))))
373
374 (defun tooltip-process-prompt-regexp (process)
375 "Return regexp matching the prompt of PROCESS at the end of a string.
376 The prompt is taken from the value of COMINT-PROMPT-REGEXP in the buffer
377 of PROCESS."
378 (let ((prompt-regexp (save-excursion
379 (set-buffer (process-buffer process))
380 comint-prompt-regexp)))
381 ;; Most start with `^' but the one for `sdb' cannot be easily
382 ;; stripped. Code the prompt for `sdb' fixed here.
383 (if (= (aref prompt-regexp 0) ?^)
384 (setq prompt-regexp (substring prompt-regexp 1))
385 (setq prompt-regexp "\\*"))
386 (concat "\n*" prompt-regexp "$")))
387
388 (defun tooltip-strip-prompt (process output)
389 "Return OUTPUT with any prompt of PROCESS stripped from its end."
390 (let ((prompt-regexp (tooltip-process-prompt-regexp process)))
391 (save-match-data
392 (when (string-match prompt-regexp output)
393 (setq output (substring output 0 (match-beginning 0)))))
394 output))
395
396 \f
397 ;;; Tips for `gud'
398
399 (defvar tooltip-gud-original-filter nil
400 "Process filter to restore after GUD output has been received.")
401
402 (defvar tooltip-gud-dereference nil
403 "Non-nil means print expressions with a `*' in front of them.
404 For C this would dereference a pointer expression.")
405
406 (defvar tooltip-gud-event nil
407 "The mouse movement event that led to a tooltip display.
408 This event can be examined by forms in TOOLTIP-GUD-DISPLAY.")
409
410 (defun tooltip-gud-toggle-dereference ()
411 "Toggle whether tooltips should show `* expr' or `expr'."
412 (interactive)
413 (setq tooltip-gud-dereference (not tooltip-gud-dereference))
414 (when (interactive-p)
415 (message "Dereferencing is now %s."
416 (if tooltip-gud-dereference "on" "off"))))
417
418 (defun tooltip-toggle-gud-tips ()
419 "Toggle the display of GUD tooltips."
420 (interactive)
421 (setq tooltip-gud-tips-p (not tooltip-gud-tips-p))
422 ;; Reconsider for all buffers whether mouse motion events are desired.
423 (tooltip-change-major-mode)
424 (when (interactive-p)
425 (message (format "GUD tooltips %sabled"
426 (if tooltip-gud-tips-p "en" "dis")))))
427
428 ; This will only display data that comes in one chunk.
429 ; Larger arrays (say 400 elements) are displayed in
430 ; the tootip incompletely and spill over into the gud buffer.
431 ; Switching the process-filter creates timing problems and
432 ; it may be difficult to do better. Using annotations as in
433 ; gdb-ui.el gets round this problem.
434 (defun tooltip-gud-process-output (process output)
435 "Process debugger output and show it in a tooltip window."
436 (set-process-filter process tooltip-gud-original-filter)
437 (tooltip-show (tooltip-strip-prompt process output)
438 tooltip-gud-echo-area))
439
440 (defun tooltip-gud-print-command (expr)
441 "Return a suitable command to print the expression EXPR.
442 If TOOLTIP-GUD-DEREFERENCE is t, also prepend a `*' to EXPR."
443 (when tooltip-gud-dereference
444 (setq expr (concat "*" expr)))
445 (case gud-minor-mode
446 ((gdb gdba) (concat "server print " expr))
447 (dbx (concat "print " expr))
448 (xdb (concat "p " expr))
449 (sdb (concat expr "/"))
450 (perldb expr)))
451
452 (defun tooltip-gud-tips (event)
453 "Show tip for identifier or selection under the mouse.
454 The mouse must either point at an identifier or inside a selected
455 region for the tip window to be shown. If tooltip-gud-dereference is t,
456 add a `*' in front of the printed expression.
457
458 This function must return nil if it doesn't handle EVENT."
459 (let (process)
460 (when (and (eventp event)
461 tooltip-gud-tips-p
462 (boundp 'gud-comint-buffer)
463 (setq process (get-buffer-process gud-comint-buffer))
464 (posn-point (event-end event))
465 (progn (setq tooltip-gud-event event)
466 (eval (cons 'and tooltip-gud-display))))
467 (let ((expr (tooltip-expr-to-print event)))
468 (when expr
469 (let ((cmd (tooltip-gud-print-command expr)))
470 (unless (null cmd) ; CMD can be nil if unknown debugger
471 (case gud-minor-mode
472 (gdba (gdb-enqueue-input
473 (list (concat cmd "\n") 'gdb-tooltip-print)))
474 (t
475 (setq tooltip-gud-original-filter (process-filter process))
476 (set-process-filter process 'tooltip-gud-process-output)
477 (gud-basic-call cmd)))
478 expr)))))))
479
480 (defun gdb-tooltip-print ()
481 (tooltip-show
482 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
483 (let ((string (buffer-string)))
484 ;; remove newline for tooltip-gud-echo-area
485 (substring string 0 (- (length string) 1))))
486 tooltip-gud-echo-area))
487
488 \f
489 ;;; Tooltip help.
490
491 (defvar tooltip-help-message nil
492 "The last help message received via `tooltip-show-help-function'.")
493
494 (defun tooltip-show-help-function (msg)
495 "Function installed as `show-help-function'.
496 MSG is either a help string to display, or nil to cancel the display."
497 (let ((previous-help tooltip-help-message))
498 (setq tooltip-help-message msg)
499 (cond ((null msg)
500 ;; Cancel display. This also cancels a delayed tip, if
501 ;; there is one.
502 (tooltip-hide))
503 ((equal previous-help msg)
504 ;; Same help as before (but possibly the mouse has moved).
505 ;; Keep what we have.
506 )
507 (t
508 ;; A different help. Remove a previous tooltip, and
509 ;; display a new one, with some delay.
510 (tooltip-hide)
511 (tooltip-start-delayed-tip)))))
512
513 (defun tooltip-help-tips (event)
514 "Hook function to display a help tooltip.
515 This is installed on the hook `tooltip-hook', which is run when
516 the timer with ID `tooltip-timeout-id' fires.
517 Value is non-nil if this function handled the tip."
518 (when (stringp tooltip-help-message)
519 (tooltip-show tooltip-help-message)
520 t))
521
522 (provide 'tooltip)
523
524 ;; arch-tag: 3d61135e-4618-4a78-af28-183f6df5636f
525 ;;; tooltip.el ends here