]> code.delx.au - gnu-emacs/blob - lisp/tooltip.el
(tooltip-gud-tips): Use gdb-define-alist.
[gnu-emacs] / 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
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."
126 :type 'boolean
127 :tag "GUD"
128 :group 'tooltip)
129
130 (defcustom tooltip-gud-modes '(gud-mode c-mode c++-mode fortran-mode)
131 "List of modes for which to enable GUD tips."
132 :type 'sexp
133 :tag "GUD modes"
134 :group 'tooltip)
135
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.
140
141 Forms in the list are combined with AND. The default is to display
142 only tooltips in the buffer containing the overlay arrow."
143 :type 'sexp
144 :tag "GUD buffers predicate"
145 :group 'tooltip)
146
147 (defcustom tooltip-gud-echo-area nil
148 "Use the echo area instead of frames for GUD tooltips."
149 :type 'boolean
150 :tag "Use echo area"
151 :group 'tooltip)
152
153 (defvaralias 'tooltip-use-echo-area 'tooltip-gud-echo-area)
154 (make-obsolete-variable 'tooltip-use-echo-area 'tooltip-gud-echo-area "22.1")
155 \f
156 ;;; Variables that are not customizable.
157
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.")
162
163 (defvar tooltip-timeout-id nil
164 "The id of the timeout started when Emacs becomes idle.")
165
166 (defvar tooltip-last-mouse-motion-event nil
167 "A copy of the last mouse motion event seen.")
168
169 (defvar tooltip-hide-time nil
170 "Time when the last tooltip was hidden.")
171
172 \f
173 ;;; Event accessors
174
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))))
180
181 \f
182 ;;; Switching tooltips on/off
183
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.
189
190 ;;;###autoload
191 (define-minor-mode tooltip-mode
192 "Toggle Tooltip display.
193 With ARG, turn tooltip mode on if and only if ARG is positive."
194 :global t
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))))
202 :group 'tooltip
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))))
215
216 \f
217 ;;; Timeout for tooltip display
218
219 (defun tooltip-delay ()
220 "Return the delay in seconds for the next tooltip."
221 (let ((delay tooltip-delay)
222 (now (float-time)))
223 (when (and tooltip-hide-time
224 (< (- now tooltip-hide-time) tooltip-recent-seconds))
225 (setq delay tooltip-short-delay))
226 delay))
227
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)))
233
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)))
238
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))
243
244 \f
245 ;;; Reacting on mouse movements
246
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))
250
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))
255 (save-excursion
256 (set-buffer buffer)
257 (if (and tooltip-mode
258 tooltip-gud-tips-p
259 (memq major-mode tooltip-gud-modes))
260 (tooltip-activate-mouse-motions t)
261 (tooltip-activate-mouse-motions nil)))))
262
263 (defvar tooltip-mouse-motions-active nil
264 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
265
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."
269 (if activatep
270 (progn
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))))
278
279 (defun tooltip-mouse-motion (event)
280 "Command handler for mouse movement events in `global-map'."
281 (interactive "e")
282 (tooltip-hide)
283 (when (car (mouse-pixel-position))
284 (setq tooltip-last-mouse-motion-event (copy-sequence event))
285 (tooltip-start-delayed-tip)))
286
287 \f
288 ;;; Displaying tips
289
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)))
295 (if (consp param)
296 (setcdr param value)
297 (push (cons key value) alist))
298 alist))
299
300 (defun tooltip-show (text &optional use-echo-area)
301 "Show a tooltip window displaying TEXT.
302
303 Text larger than `x-max-tooltip-size' is clipped.
304
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
309 position.
310
311 Optional second arg USE-ECHO-AREA non-nil means to show tooltip
312 in echo area."
313 (if use-echo-area
314 (message "%s" text)
315 (condition-case error
316 (let ((params (copy-sequence tooltip-frame-parameters))
317 (fg (face-attribute 'tooltip :foreground))
318 (bg (face-attribute 'tooltip :background)))
319 (when (stringp fg)
320 (setq params (tooltip-set-param params 'foreground-color fg))
321 (setq params (tooltip-set-param params 'border-color fg)))
322 (when (stringp bg)
323 (setq params (tooltip-set-param params 'background-color bg)))
324 (x-show-tip (propertize text 'face 'tooltip)
325 (selected-frame)
326 params
327 tooltip-hide-delay
328 tooltip-x-offset
329 tooltip-y-offset))
330 (error
331 (message "Error while displaying tooltip: %s" error)
332 (sit-for 1)
333 (message "%s" text)))))
334
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)
339 (when (x-hide-tip)
340 (setq tooltip-hide-time (float-time))))
341
342 \f
343 ;;; Debugger-related functions
344
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."
349 (save-excursion
350 (goto-char point)
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)))))))
356
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)
361 `(region-active-p)))
362
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
367 where the mouse is."
368 (save-excursion
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)))))
375
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
379 of PROCESS."
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 "$")))
389
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)))
393 (save-match-data
394 (when (string-match prompt-regexp output)
395 (setq output (substring output 0 (match-beginning 0)))))
396 output))
397
398 \f
399 ;;; Tips for `gud'
400
401 (defvar tooltip-gud-original-filter nil
402 "Process filter to restore after GUD output has been received.")
403
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.")
407
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.")
411
412 (defun tooltip-gud-toggle-dereference ()
413 "Toggle whether tooltips should show `* expr' or `expr'."
414 (interactive)
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"))))
419
420 (defun tooltip-toggle-gud-tips ()
421 "Toggle the display of GUD tooltips."
422 (interactive)
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")))))
429
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))
441
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)))
447 (case gud-minor-mode
448 ((gdb gdba) (concat "server print " expr))
449 (dbx (concat "print " expr))
450 (xdb (concat "p " expr))
451 (sdb (concat expr "/"))
452 (perldb expr)))
453
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
460 not executing.
461
462 This function must return nil if it doesn't handle EVENT."
463 (let (process)
464 (when (and (eventp event)
465 tooltip-gud-tips-p
466 (boundp 'gud-comint-buffer)
467 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)))
475 (when expr
476 (if (and (eq gud-minor-mode 'gdba)
477 (not gdb-active-process))
478 (progn
479 (with-current-buffer
480 (window-buffer (let ((mouse (mouse-position)))
481 (window-at (cadr mouse)
482 (cddr mouse))))
483 (let ((define-elt (assoc expr gdb-define-alist)))
484 (unless (null define-elt)
485 (tooltip-show (cdr define-elt))
486 expr))))
487 (let ((cmd (tooltip-gud-print-command expr)))
488 (unless (null cmd) ; CMD can be nil if unknown debugger
489 (case gud-minor-mode
490 (gdba (gdb-enqueue-input
491 (list (concat cmd "\n") 'gdb-tooltip-print)))
492 (t
493 (setq tooltip-gud-original-filter (process-filter process))
494 (set-process-filter process 'tooltip-gud-process-output)
495 (gud-basic-call cmd)))
496 expr))))))))
497
498 (defun gdb-tooltip-print ()
499 (tooltip-show
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))
505
506 \f
507 ;;; Tooltip help.
508
509 (defvar tooltip-help-message nil
510 "The last help message received via `tooltip-show-help-function'.")
511
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)
517 (cond ((null msg)
518 ;; Cancel display. This also cancels a delayed tip, if
519 ;; there is one.
520 (tooltip-hide))
521 ((equal previous-help msg)
522 ;; Same help as before (but possibly the mouse has moved).
523 ;; Keep what we have.
524 )
525 (t
526 ;; A different help. Remove a previous tooltip, and
527 ;; display a new one, with some delay.
528 (tooltip-hide)
529 (tooltip-start-delayed-tip)))))
530
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)
538 t))
539
540 (provide 'tooltip)
541
542 ;; arch-tag: 3d61135e-4618-4a78-af28-183f6df5636f
543 ;;; tooltip.el ends here