(gc_sweep): Add comment.
[emacs.git] / lisp / tooltip.el
blob9ad338418c2764f7ad06ce7778d53ab7e3ba3dc2
1 ;;; tooltip.el --- Show tooltip windows
3 ;; Copyright (C) 1997, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Gerd Moellmann <gerd@acm.org>
6 ;; Keywords: help c mouse tools
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
27 ;; Put into your `.emacs'
29 ;; (require 'tooltip)
30 ;; (tooltip-mode 1)
34 ;;; Code:
36 (eval-when-compile
37 (require 'cl)
38 (require 'comint)
39 (require 'gud))
41 (provide 'tooltip)
44 ;;; Customizable settings
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")
55 (defvar tooltip-mode)
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)
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)
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)
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)
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)
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)
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)
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)
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.
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)
143 (defcustom tooltip-use-echo-area nil
144 "Use the echo area instead of tooltip frames.
145 This is only relevant GUD display, since otherwise it is equivalent to
146 turning off Tooltip mode."
147 :type 'boolean
148 :tag "Use echo area"
149 :group 'tooltip)
152 ;;; Variables that are not customizable.
154 (defvar tooltip-hook nil
155 "Functions to call to display tooltips.
156 Each function is called with one argument EVENT which is a copy of
157 the last mouse movement event that occurred.")
160 (defvar tooltip-timeout-id nil
161 "The id of the timeout started when Emacs becomes idle.")
164 (defvar tooltip-last-mouse-motion-event nil
165 "A copy of the last mouse motion event seen.")
168 (defvar tooltip-hide-time nil
169 "Time when the last tooltip was hidden.")
172 (defvar tooltip-gud-debugger nil
173 "The debugger for which we show tooltips.")
177 ;;; Event accessors
179 (defun tooltip-event-buffer (event)
180 "Return the buffer over which event EVENT occurred.
181 This might return nil if the event did not occur over a buffer."
182 (let ((window (posn-window (event-end event))))
183 (and window (window-buffer window))))
187 ;;; Switching tooltips on/off
189 ;; We don't set track-mouse globally because this is a big redisplay
190 ;; problem in buffers having a pre-command-hook or such installed,
191 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
192 ;; set-buffer prevents redisplay optimizations, so every mouse motion
193 ;; would be accompanied by a full redisplay.
195 ;;;###autoload
196 (defun tooltip-mode (&optional arg)
197 "Mode for tooltip display.
198 With ARG, turn tooltip mode on if and only if ARG is positive."
199 (interactive "P")
200 (let* ((on (if arg
201 (> (prefix-numeric-value arg) 0)
202 (not tooltip-mode)))
203 (hook-fn (if on 'add-hook 'remove-hook)))
204 (setq tooltip-mode on)
205 (funcall hook-fn 'change-major-mode-hook 'tooltip-change-major-mode)
206 (tooltip-activate-mouse-motions-if-enabled)
207 (funcall hook-fn 'pre-command-hook 'tooltip-hide)
208 (funcall hook-fn 'tooltip-hook 'tooltip-gud-tips)
209 (funcall hook-fn 'tooltip-hook 'tooltip-help-tips)
210 (setq show-help-function (if on 'tooltip-show-help-function nil))
211 ;; `ignore' is the default binding for mouse movements.
212 (define-key global-map [mouse-movement]
213 (if on 'tooltip-mouse-motion 'ignore))
214 (tooltip-gud-tips-setup)))
216 (defun tooltip-gud-tips-setup ()
217 "Setup debugger mode-hooks for tooltips."
218 (when (and tooltip-mode tooltip-gud-tips-p)
219 (global-set-key [S-mouse-3] 'tooltip-gud-toggle-dereference)
220 (add-hook 'gdb-mode-hook
221 #'(lambda () (setq tooltip-gud-debugger 'gdb)))
222 (add-hook 'sdb-mode-hook
223 #'(lambda () (setq tooltip-gud-debugger 'sdb)))
224 (add-hook 'dbx-mode-hook
225 #'(lambda () (setq tooltip-gud-debugger 'dbx)))
226 (add-hook 'xdb-mode-hook
227 #'(lambda () (setq tooltip-gud-debugger 'xdb)))
228 (add-hook 'perldb-mode-hook
229 #'(lambda () (setq tooltip-gud-debugger 'perldb)))))
231 ;;; Timeout for tooltip display
233 (defun tooltip-delay ()
234 "Return the delay in seconds for the next tooltip."
235 (let ((delay tooltip-delay)
236 (now (float-time)))
237 (when (and tooltip-hide-time
238 (< (- now tooltip-hide-time) tooltip-recent-seconds))
239 (setq delay tooltip-short-delay))
240 delay))
243 (defun tooltip-disable-timeout ()
244 "Disable the tooltip timeout."
245 (when tooltip-timeout-id
246 (disable-timeout tooltip-timeout-id)
247 (setq tooltip-timeout-id nil)))
250 (defun tooltip-add-timeout ()
251 "Add a one-shot timeout to call function tooltip-timeout."
252 (setq tooltip-timeout-id
253 (add-timeout (tooltip-delay) 'tooltip-timeout nil)))
256 (defun tooltip-timeout (object)
257 "Function called when timer with id tooltip-timeout-id fires."
258 (run-hook-with-args-until-success 'tooltip-hook
259 tooltip-last-mouse-motion-event))
263 ;;; Reacting on mouse movements
265 (defun tooltip-change-major-mode ()
266 "Function added to `change-major-mode-hook' when tooltip mode is on."
267 (add-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled))
270 (defun tooltip-activate-mouse-motions-if-enabled ()
271 "Reconsider for all buffers whether mouse motion events are desired."
272 (remove-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled)
273 (let ((buffers (buffer-list)))
274 (save-excursion
275 (while buffers
276 (set-buffer (car buffers))
277 (if (and tooltip-mode
278 tooltip-gud-tips-p
279 (memq major-mode tooltip-gud-modes))
280 (tooltip-activate-mouse-motions t)
281 (tooltip-activate-mouse-motions nil))
282 (setq buffers (cdr buffers))))))
285 (defun tooltip-activate-mouse-motions (activatep)
286 "Activate/deactivate mouse motion events for the current buffer.
287 ACTIVATEP non-nil means activate mouse motion events."
288 (if activatep
289 (progn
290 (make-local-variable 'track-mouse)
291 (setq track-mouse t))
292 (kill-local-variable 'track-mouse)))
295 (defun tooltip-mouse-motion (event)
296 "Command handler for mouse movement events in `global-map'."
297 (interactive "e")
298 (tooltip-hide)
299 (when (car (mouse-pixel-position))
300 (setq tooltip-last-mouse-motion-event (copy-sequence event))
301 (tooltip-add-timeout)))
305 ;;; Displaying tips
307 (defun tooltip-show (text)
308 "Show a tooltip window at the current mouse position displaying TEXT."
309 (if tooltip-use-echo-area
310 (message "%s" text)
311 (x-show-tip text
312 (selected-frame)
313 tooltip-frame-parameters
315 tooltip-x-offset
316 tooltip-y-offset)))
318 (defun tooltip-hide (&optional ignored-arg)
319 "Hide a tooltip, if one is displayed.
320 Value is non-nil if tooltip was open."
321 (tooltip-disable-timeout)
322 (when (x-hide-tip)
323 (setq tooltip-hide-time (float-time))))
327 ;;; Debugger-related functions
329 (defun tooltip-identifier-from-point (point)
330 "Extract the identifier at POINT, if any.
331 Value is nil if no identifier exists at point. Identifier extraction
332 is based on the current syntax table."
333 (save-excursion
334 (goto-char point)
335 (let ((start (progn (skip-syntax-backward "w_") (point))))
336 (unless (looking-at "[0-9]")
337 (skip-syntax-forward "w_")
338 (when (> (point) start)
339 (buffer-substring start (point)))))))
342 (defmacro tooltip-region-active-p ()
343 "Value is non-nil if the region is currently active."
344 (if (string-match "^GNU" (emacs-version))
345 `(and transient-mark-mode mark-active)
346 `(region-active-p)))
349 (defun tooltip-expr-to-print (event)
350 "Return an expression that should be printed for EVENT.
351 If a region is active and the mouse is inside the region, print
352 the region. Otherwise, figure out the identifier around the point
353 where the mouse is."
354 (save-excursion
355 (set-buffer (tooltip-event-buffer event))
356 (let ((point (posn-point (event-end event))))
357 (if (tooltip-region-active-p)
358 (when (and (<= (region-beginning) point) (<= point (region-end)))
359 (buffer-substring (region-beginning) (region-end)))
360 (tooltip-identifier-from-point point)))))
363 (defun tooltip-process-prompt-regexp (process)
364 "Return regexp matching the prompt of PROCESS at the end of a string.
365 The prompt is taken from the value of COMINT-PROMPT-REGEXP in the buffer
366 of PROCESS."
367 (let ((prompt-regexp (save-excursion
368 (set-buffer (process-buffer process))
369 comint-prompt-regexp)))
370 ;; Most start with `^' but the one for `sdb' cannot be easily
371 ;; stripped. Code the prompt for `sdb' fixed here.
372 (if (= (aref prompt-regexp 0) ?^)
373 (setq prompt-regexp (substring prompt-regexp 1))
374 (setq prompt-regexp "\\*"))
375 (concat "\n*" prompt-regexp "$")))
378 (defun tooltip-strip-prompt (process output)
379 "Return OUTPUT with any prompt of PROCESS stripped from its end."
380 (let ((prompt-regexp (tooltip-process-prompt-regexp process)))
381 (save-match-data
382 (when (string-match prompt-regexp output)
383 (setq output (substring output 0 (match-beginning 0)))))
384 output))
388 ;;; Tips for `gud'
390 (defvar tooltip-gud-original-filter nil
391 "Process filter to restore after GUD output has been received.")
394 (defvar tooltip-gud-dereference nil
395 "Non-nil means print expressions with a `*' in front of them.
396 For C this would dereference a pointer expression.")
399 (defvar tooltip-gud-event nil
400 "The mouse movement event that led to a tooltip display.
401 This event can be examined by forms in TOOLTIP-GUD-DISPLAY.")
404 (defvar tooltip-gud-debugger nil
405 "A symbol describing the debugger running under GUD.")
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"))))
417 (defun tooltip-gud-process-output (process output)
418 "Process debugger output and show it in a tooltip window."
419 (set-process-filter process tooltip-gud-original-filter)
420 (tooltip-show (tooltip-strip-prompt process output)))
423 (defun tooltip-gud-print-command (expr)
424 "Return a suitable command to print the expression EXPR.
425 If TOOLTIP-GUD-DEREFERENCE is t, also prepend a `*' to EXPR."
426 (when tooltip-gud-dereference
427 (setq expr (concat "*" expr)))
428 (case tooltip-gud-debugger
429 ((gdb dbx) (concat "print " expr))
430 (xdb (concat "p " expr))
431 (sdb (concat expr "/"))
432 (perldb expr)))
435 (defun tooltip-gud-tips (event)
436 "Show tip for identifier or selection under the mouse.
437 The mouse must either point at an identifier or inside a selected
438 region for the tip window to be shown. If tooltip-gud-dereference is t,
439 add a `*' in front of the printed expression.
441 This function must return nil if it doesn't handle EVENT."
442 (let (gud-buffer process)
443 (when (and (eventp event)
444 tooltip-gud-tips-p
445 (boundp 'gud-comint-buffer)
446 (setq gud-buffer gud-comint-buffer)
447 (setq process (get-buffer-process gud-buffer))
448 (posn-point (event-end event))
449 (progn (setq tooltip-gud-event event)
450 (eval (cons 'and tooltip-gud-display))))
451 (let ((expr (tooltip-expr-to-print event)))
452 (when expr
453 (let ((cmd (tooltip-gud-print-command expr)))
454 (unless (null cmd) ; CMD can be nil if unknown debugger
455 (setq tooltip-gud-original-filter (process-filter process))
456 (set-process-filter process 'tooltip-gud-process-output)
457 (gud-basic-call cmd)
458 expr)))))))
461 ;;; Tooltip help.
463 (defvar tooltip-help-message nil
464 "The last help message received via `tooltip-show-help-function'.")
467 (defun tooltip-show-help-function (msg)
468 "Function installed as `show-help-function'.
469 MSG is either a help string to display, or nil to cancel the display."
470 (let ((previous-help tooltip-help-message))
471 (setq tooltip-help-message msg)
472 (cond ((null msg)
473 (tooltip-hide))
474 ((or (not (stringp previous-help))
475 (not (string= msg previous-help)))
476 (tooltip-hide)
477 (tooltip-add-timeout))
479 (tooltip-disable-timeout)
480 (tooltip-add-timeout)))))
483 (defun tooltip-help-tips (event)
484 "Hook function to display a help tooltip.
485 Value is non-nil if this function handled the tip."
486 (when (stringp tooltip-help-message)
487 (tooltip-show tooltip-help-message)
488 (setq tooltip-help-message nil)
493 ;;; Do this after all functions have been defined that are called from
494 ;;; `tooltip-mode'. The actual default value of `tooltip-mode' is set
495 ;;; in startup.el.
497 ;;;###autoload
498 (defcustom tooltip-mode nil
499 "Toggle tooltip-mode.
500 Setting this variable directly does not take effect;
501 use either \\[customize] or the function `tooltip-mode'."
502 :set (lambda (symbol value)
503 (tooltip-mode (or value 0)))
504 :initialize 'custom-initialize-default
505 :type 'boolean
506 :require 'tooltip
507 :group 'tooltip)
510 ;;; tooltip.el ends here