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