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