*** empty log message ***
[emacs.git] / lisp / tooltip.el
bloba7484dc7fd50e695976625b452c49246b7ce5513
1 ;;; tooltip.el --- Show tooltip windows
3 ;; Copyright (C) 1997, 1999 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-frame-parameters
80 '((name . "tooltip")
81 (foreground-color . "black")
82 (background-color . "lightyellow")
83 (internal-border-width . 5)
84 (border-color . "lightyellow")
85 (border-width . 1))
86 "Frame parameters used for tooltips."
87 :type 'sexp
88 :tag "Frame Parameters"
89 :group 'tooltip)
92 (defcustom tooltip-gud-tips-p nil
93 "*Non-nil means show tooltips in GUD sessions."
94 :type 'boolean
95 :tag "GUD"
96 :group 'tooltip)
99 (defcustom tooltip-gud-modes '(gud-mode c-mode c++-mode)
100 "List of modes for which to enable GUD tips."
101 :type 'sexp
102 :tag "GUD modes"
103 :group 'tooltip)
106 (defcustom tooltip-gud-display
107 '((eq (tooltip-event-buffer tooltip-gud-event)
108 (marker-buffer overlay-arrow-position)))
109 "List of forms determining where GUD tooltips are displayed.
111 Forms in the list are combined with AND. The default is to display
112 only tooltips in the buffer containing the overlay arrow."
113 :type 'sexp
114 :tag "GUD buffers predicate"
115 :group 'tooltip)
119 ;;; Variables that are not customizable.
121 (defvar tooltip-hook nil
122 "Functions to call to display tooltips.
123 Each function is called with one argument EVENT which is a copy of
124 the last mouse movement event that occurred.")
127 (defvar tooltip-timeout-id nil
128 "The id of the timeout started when Emacs becomes idle.")
131 (defvar tooltip-last-mouse-motion-event nil
132 "A copy of the last mouse motion event seen.")
135 (defvar tooltip-hide-time nil
136 "Time when the last tooltip was hidden.")
139 (defvar tooltip-gud-debugger nil
140 "The debugger for which we show tooltips.")
144 ;;; Event accessors
146 (defun tooltip-event-buffer (event)
147 "Return the buffer over which event EVENT occurred.
148 This might return nil if the event did not occur over a buffer."
149 (let ((window (posn-window (event-end event))))
150 (and window (window-buffer window))))
154 ;;; Switching tooltips on/off
156 ;; We don't set track-mouse globally because this is a big redisplay
157 ;; problem in buffers having a pre-command-hook or such installed,
158 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
159 ;; set-buffer prevents redisplay optimizations, so every mouse motion
160 ;; would be accompanied by a full redisplay.
162 ;;;###autoload
163 (defun tooltip-mode (&optional arg)
164 "Mode for tooltip display.
165 With ARG, turn tooltip mode on if and only if ARG is positive."
166 (interactive "P")
167 (let* ((on (if arg
168 (> (prefix-numeric-value arg) 0)
169 (not tooltip-mode)))
170 (hook-fn (if on 'add-hook 'remove-hook)))
171 (setq tooltip-mode on)
172 (funcall hook-fn 'change-major-mode-hook 'tooltip-change-major-mode)
173 (tooltip-activate-mouse-motions-if-enabled)
174 (funcall hook-fn 'pre-command-hook 'tooltip-hide)
175 (funcall hook-fn 'tooltip-hook 'tooltip-gud-tips)
176 (funcall hook-fn 'tooltip-hook 'tooltip-help-tips)
177 (setq show-help-function (if on 'tooltip-show-help-function nil))
178 ;; `ignore' is the default binding for mouse movements.
179 (define-key global-map [mouse-movement]
180 (if on 'tooltip-mouse-motion 'ignore))
181 (when (and on tooltip-gud-tips-p)
182 (global-set-key [S-mouse-3] 'tooltip-gud-toggle-dereference)
183 (add-hook 'gdb-mode-hook
184 #'(lambda () (setq tooltip-gud-debugger 'gdb)))
185 (add-hook 'sdb-mode-hook
186 #'(lambda () (setq tooltip-gud-debugger 'sdb)))
187 (add-hook 'dbx-mode-hook
188 #'(lambda () (setq tooltip-gud-debugger 'dbx)))
189 (add-hook 'xdb-mode-hook
190 #'(lambda () (setq tooltip-gud-debugger 'xdb)))
191 (add-hook 'perldb-mode-hook
192 #'(lambda () (setq tooltip-gud-debugger 'perldb))))))
196 ;;; Timeout for tooltip display
198 (defun tooltip-delay ()
199 "Return the delay in seconds for the next tooltip."
200 (let ((delay tooltip-delay)
201 (now (float-time)))
202 (when (and tooltip-hide-time
203 (< (- now tooltip-hide-time) tooltip-recent-seconds))
204 (setq delay tooltip-short-delay))
205 delay))
208 (defun tooltip-disable-timeout ()
209 "Disable the tooltip timeout."
210 (when tooltip-timeout-id
211 (disable-timeout tooltip-timeout-id)
212 (setq tooltip-timeout-id nil)))
215 (defun tooltip-add-timeout ()
216 "Add a one-shot timeout to call function tooltip-timeout."
217 (setq tooltip-timeout-id
218 (add-timeout (tooltip-delay) 'tooltip-timeout nil)))
221 (defun tooltip-timeout (object)
222 "Function called when timer with id tooltip-timeout-id fires."
223 (run-hook-with-args-until-success 'tooltip-hook
224 tooltip-last-mouse-motion-event))
228 ;;; Reacting on mouse movements
230 (defun tooltip-change-major-mode ()
231 "Function added to `change-major-mode-hook' when tooltip mode is on."
232 (add-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled))
235 (defun tooltip-activate-mouse-motions-if-enabled ()
236 "Reconsider for all buffers whether mouse motion events are desired."
237 (remove-hook 'post-command-hook 'tooltip-activate-mouse-motions-if-enabled)
238 (let ((buffers (buffer-list)))
239 (save-excursion
240 (while buffers
241 (set-buffer (car buffers))
242 (if (and tooltip-mode
243 tooltip-gud-tips-p
244 (memq major-mode tooltip-gud-modes))
245 (tooltip-activate-mouse-motions t)
246 (tooltip-activate-mouse-motions nil))
247 (setq buffers (cdr buffers))))))
250 (defun tooltip-activate-mouse-motions (activatep)
251 "Activate/deactivate mouse motion events for the current buffer.
252 ACTIVATEP non-nil means activate mouse motion events."
253 (if activatep
254 (progn
255 (make-local-variable 'track-mouse)
256 (setq track-mouse t))
257 (kill-local-variable 'track-mouse)))
260 (defun tooltip-mouse-motion (event)
261 "Command handler for mouse movement events in `global-map'."
262 (interactive "e")
263 (tooltip-hide)
264 (when (car (mouse-pixel-position))
265 (setq tooltip-last-mouse-motion-event (copy-sequence event))
266 (tooltip-add-timeout)))
270 ;;; Displaying tips
272 (defun tooltip-show (text)
273 "Show a tooltip window at the current mouse position displaying TEXT."
274 (x-show-tip text (selected-frame) tooltip-frame-parameters))
277 (defun tooltip-hide (&optional ignored-arg)
278 "Hide a tooltip, if one is displayed.
279 Value is non-nil if tooltip was open."
280 (tooltip-disable-timeout)
281 (when (x-hide-tip)
282 (setq tooltip-hide-time (float-time))))
286 ;;; Debugger-related functions
288 (defun tooltip-identifier-from-point (point)
289 "Extract the identifier at POINT, if any.
290 Value is nil if no identifier exists at point. Identifier extraction
291 is based on the current syntax table."
292 (save-excursion
293 (goto-char point)
294 (let ((start (progn (skip-syntax-backward "w_") (point))))
295 (unless (looking-at "[0-9]")
296 (skip-syntax-forward "w_")
297 (when (> (point) start)
298 (buffer-substring start (point)))))))
301 (defmacro tooltip-region-active-p ()
302 "Value is non-nil if the region is currently active."
303 (if (string-match "^GNU" (emacs-version))
304 `(and transient-mark-mode mark-active)
305 `(region-active-p)))
308 (defun tooltip-expr-to-print (event)
309 "Return an expression that should be printed for EVENT.
310 If a region is active and the mouse is inside the region, print
311 the region. Otherwise, figure out the identifier around the point
312 where the mouse is."
313 (save-excursion
314 (set-buffer (tooltip-event-buffer event))
315 (let ((point (posn-point (event-end event))))
316 (if (tooltip-region-active-p)
317 (when (and (<= (region-beginning) point) (<= point (region-end)))
318 (buffer-substring (region-beginning) (region-end)))
319 (tooltip-identifier-from-point point)))))
322 (defun tooltip-process-prompt-regexp (process)
323 "Return regexp matching the prompt of PROCESS at the end of a string.
324 The prompt is taken from the value of COMINT-PROMPT-REGEXP in the buffer
325 of PROCESS."
326 (let ((prompt-regexp (save-excursion
327 (set-buffer (process-buffer process))
328 comint-prompt-regexp)))
329 ;; Most start with `^' but the one for `sdb' cannot be easily
330 ;; stripped. Code the prompt for `sdb' fixed here.
331 (if (= (aref prompt-regexp 0) ?^)
332 (setq prompt-regexp (substring prompt-regexp 1))
333 (setq prompt-regexp "\\*"))
334 (concat "\n*" prompt-regexp "$")))
337 (defun tooltip-strip-prompt (process output)
338 "Return OUTPUT with any prompt of PROCESS stripped from its end."
339 (let ((prompt-regexp (tooltip-process-prompt-regexp process)))
340 (save-match-data
341 (when (string-match prompt-regexp output)
342 (setq output (substring output 0 (match-beginning 0)))))
343 output))
347 ;;; Tips for `gud'
349 (defvar tooltip-gud-original-filter nil
350 "Process filter to restore after GUD output has been received.")
353 (defvar tooltip-gud-dereference nil
354 "Non-nil means print expressions with a `*' in front of them.
355 For C this would dereference a pointer expression.")
358 (defvar tooltip-gud-event nil
359 "The mouse movement event that led to a tooltip display.
360 This event can be examined by forms in TOOLTIP-GUD-DISPLAY.")
363 (defvar tooltip-gud-debugger nil
364 "A symbol describing the debugger running under GUD.")
367 (defun tooltip-gud-toggle-dereference ()
368 "Toggle whether tooltips should show `* expr' or `expr'."
369 (interactive)
370 (setq tooltip-gud-dereference (not tooltip-gud-dereference))
371 (when (interactive-p)
372 (message "Dereferencing is now %s."
373 (if tooltip-gud-dereference "on" "off"))))
376 (defun tooltip-gud-process-output (process output)
377 "Process debugger output and show it in a tooltip window."
378 (set-process-filter process tooltip-gud-original-filter)
379 (tooltip-show (tooltip-strip-prompt process output)))
382 (defun tooltip-gud-print-command (expr)
383 "Return a suitable command to print the expression EXPR.
384 If TOOLTIP-GUD-DEREFERENCE is t, also prepend a `*' to EXPR."
385 (when tooltip-gud-dereference
386 (setq expr (concat "*" expr)))
387 (case tooltip-gud-debugger
388 ((gdb dbx) (concat "print " expr))
389 (xdb (concat "p " expr))
390 (sdb (concat expr "/"))
391 (perldb expr)))
394 (defun tooltip-gud-tips (event)
395 "Show tip for identifier or selection under the mouse.
396 The mouse must either point at an identifier or inside a selected
397 region for the tip window to be shown. If tooltip-gud-dereference is t,
398 add a `*' in front of the printed expression.
400 This function must return nil if it doesn't handle EVENT."
401 (let (gud-buffer process)
402 (when (and (eventp event)
403 tooltip-gud-tips-p
404 (boundp 'gud-comint-buffer)
405 (setq gud-buffer gud-comint-buffer)
406 (setq process (get-buffer-process gud-buffer))
407 (posn-point (event-end event))
408 (progn (setq tooltip-gud-event event)
409 (eval (cons 'and tooltip-gud-display))))
410 (let ((expr (tooltip-expr-to-print event)))
411 (when expr
412 (setq tooltip-gud-original-filter (process-filter process))
413 (set-process-filter process 'tooltip-gud-process-output)
414 (process-send-string
415 process (concat (tooltip-gud-print-command expr) "\n"))
416 expr)))))
420 ;;; Tooltip help.
422 (defvar tooltip-help-message nil
423 "The last help message received via `tooltip-show-help-function'.")
426 (defun tooltip-show-help-function (msg)
427 "Function installed as `show-help-function'.
428 MSG is either a help string to display, or nil to cancel the display."
429 (let ((previous-help tooltip-help-message))
430 (setq tooltip-help-message msg)
431 (cond ((null msg)
432 (tooltip-hide))
433 ((or (not (stringp previous-help))
434 (not (string= msg previous-help)))
435 (tooltip-hide)
436 (tooltip-add-timeout))
438 (tooltip-disable-timeout)
439 (tooltip-add-timeout)))))
442 (defun tooltip-help-tips (event)
443 "Hook function to display a help tooltip.
444 Value is non-nil if this function handled the tip."
445 (when (stringp tooltip-help-message)
446 (tooltip-show tooltip-help-message)
447 (setq tooltip-help-message nil)
452 ;;; Do this after all functions have been defined that are called
453 ;;; from `tooltip-mode'.
456 ;;;###autoload
457 (defcustom tooltip-mode nil
458 "Toggle tooltip-mode.
459 Setting this variable directly does not take effect;
460 use either \\[customize] or the function `tooltip-mode'."
461 :set (lambda (symbol value)
462 (tooltip-mode (or value 0)))
463 :initialize 'custom-initialize-default
464 :type 'boolean
465 :require 'tooltip
466 :group 'tooltip)
469 ;;; tooltip.el ends here