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