]> code.delx.au - gnu-emacs/blob - lisp/tooltip.el
* cus-theme.el (custom-new-theme-mode): Use cus-edit faces.
[gnu-emacs] / lisp / tooltip.el
1 ;;; tooltip.el --- show tooltip windows
2
3 ;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
5
6 ;; Author: Gerd Moellmann <gerd@acm.org>
7 ;; Keywords: help c mouse tools
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (defvar comint-prompt-regexp)
31
32 (defgroup tooltip nil
33 "Customization group for the `tooltip' package."
34 :group 'help
35 :group 'gud
36 :group 'mouse
37 :group 'tools
38 :version "21.1"
39 :tag "Tool Tips")
40 \f
41 ;;; Switching tooltips on/off
42
43 ;; We don't set track-mouse globally because this is a big redisplay
44 ;; problem in buffers having a pre-command-hook or such installed,
45 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
46 ;; set-buffer prevents redisplay optimizations, so every mouse motion
47 ;; would be accompanied by a full redisplay.
48
49 (define-minor-mode tooltip-mode
50 "Toggle Tooltip display.
51 With ARG, turn tooltip mode on if and only if ARG is positive."
52 :global t
53 :init-value (not (or noninteractive
54 emacs-basic-display
55 (not (display-graphic-p))
56 (not (fboundp 'x-show-tip))))
57 :initialize 'custom-initialize-safe-default
58 :group 'tooltip
59 (unless (or (null tooltip-mode) (fboundp 'x-show-tip))
60 (error "Sorry, tooltips are not yet available on this system"))
61 (if tooltip-mode
62 (progn
63 (add-hook 'pre-command-hook 'tooltip-hide)
64 (add-hook 'tooltip-hook 'tooltip-help-tips))
65 (unless (and (boundp 'gud-tooltip-mode) gud-tooltip-mode)
66 (remove-hook 'pre-command-hook 'tooltip-hide))
67 (remove-hook 'tooltip-hook 'tooltip-help-tips))
68 (setq show-help-function
69 (if tooltip-mode 'tooltip-show-help nil)))
70
71 \f
72 ;;; Customizable settings
73
74 (defcustom tooltip-delay 0.7
75 "Seconds to wait before displaying a tooltip the first time."
76 :type 'number
77 :group 'tooltip)
78
79 (defcustom tooltip-short-delay 0.1
80 "Seconds to wait between subsequent tooltips on different items."
81 :type 'number
82 :group 'tooltip)
83
84 (defcustom tooltip-recent-seconds 1
85 "Display tooltips if changing tip items within this many seconds.
86 Do so after `tooltip-short-delay'."
87 :type 'number
88 :group 'tooltip)
89
90 (defcustom tooltip-hide-delay 10
91 "Hide tooltips automatically after this many seconds."
92 :type 'number
93 :group 'tooltip)
94
95 (defcustom tooltip-x-offset 5
96 "X offset, in pixels, for the display of tooltips.
97 The offset is the distance between the X position of the mouse and
98 the left border of the tooltip window. It must be chosen so that the
99 tooltip window doesn't contain the mouse when it pops up, or it may
100 interfere with clicking where you wish.
101
102 If `tooltip-frame-parameters' includes the `left' parameter,
103 the value of `tooltip-x-offset' is ignored."
104 :type 'integer
105 :group 'tooltip)
106
107 (defcustom tooltip-y-offset +20
108 "Y offset, in pixels, for the display of tooltips.
109 The offset is the distance between the Y position of the mouse and
110 the top border of the tooltip window. It must be chosen so that the
111 tooltip window doesn't contain the mouse when it pops up, or it may
112 interfere with clicking where you wish.
113
114 If `tooltip-frame-parameters' includes the `top' parameter,
115 the value of `tooltip-y-offset' is ignored."
116 :type 'integer
117 :group 'tooltip)
118
119 (defcustom tooltip-frame-parameters
120 '((name . "tooltip")
121 (internal-border-width . 2)
122 (border-width . 1))
123 "Frame parameters used for tooltips.
124
125 If `left' or `top' parameters are included, they specify the absolute
126 position to pop up the tooltip."
127 :type 'sexp
128 :group 'tooltip)
129
130 (defface tooltip
131 '((((class color))
132 :background "lightyellow"
133 :foreground "black"
134 :inherit variable-pitch)
135 (t
136 :inherit variable-pitch))
137 "Face for tooltips."
138 :group 'tooltip
139 :group 'basic-faces)
140
141 (defcustom tooltip-use-echo-area nil
142 "Use the echo area instead of tooltip frames for help and GUD tooltips."
143 :type 'boolean
144 :group 'tooltip)
145
146 \f
147 ;;; Variables that are not customizable.
148
149 (defvar tooltip-hook nil
150 "Functions to call to display tooltips.
151 Each function is called with one argument EVENT which is a copy of
152 the last mouse movement event that occurred.")
153
154 (defvar tooltip-timeout-id nil
155 "The id of the timeout started when Emacs becomes idle.")
156
157 (defvar tooltip-last-mouse-motion-event nil
158 "A copy of the last mouse motion event seen.")
159
160 (defvar tooltip-hide-time nil
161 "Time when the last tooltip was hidden.")
162
163 (defvar gud-tooltip-mode) ;; Prevent warning.
164
165 ;;; Event accessors
166
167 (defun tooltip-event-buffer (event)
168 "Return the buffer over which event EVENT occurred.
169 This might return nil if the event did not occur over a buffer."
170 (let ((window (posn-window (event-end event))))
171 (and window (window-buffer window))))
172
173 \f
174 ;;; Timeout for tooltip display
175
176 (defun tooltip-delay ()
177 "Return the delay in seconds for the next tooltip."
178 (let ((delay tooltip-delay)
179 (now (float-time)))
180 (when (and tooltip-hide-time
181 (< (- now tooltip-hide-time) tooltip-recent-seconds))
182 (setq delay tooltip-short-delay))
183 delay))
184
185 (defun tooltip-cancel-delayed-tip ()
186 "Disable the tooltip timeout."
187 (when tooltip-timeout-id
188 (disable-timeout tooltip-timeout-id)
189 (setq tooltip-timeout-id nil)))
190
191 (defun tooltip-start-delayed-tip ()
192 "Add a one-shot timeout to call function `tooltip-timeout'."
193 (setq tooltip-timeout-id
194 (add-timeout (tooltip-delay) 'tooltip-timeout nil)))
195
196 (defun tooltip-timeout (object)
197 "Function called when timer with id `tooltip-timeout-id' fires."
198 (run-hook-with-args-until-success 'tooltip-hook
199 tooltip-last-mouse-motion-event))
200
201 \f
202 ;;; Displaying tips
203
204 (defun tooltip-set-param (alist key value)
205 "Change the value of KEY in alist ALIST to VALUE.
206 If there's no association for KEY in ALIST, add one, otherwise
207 change the existing association. Value is the resulting alist."
208 (let ((param (assq key alist)))
209 (if (consp param)
210 (setcdr param value)
211 (push (cons key value) alist))
212 alist))
213
214 (defun tooltip-show (text &optional use-echo-area)
215 "Show a tooltip window displaying TEXT.
216
217 Text larger than `x-max-tooltip-size' is clipped.
218
219 If the alist in `tooltip-frame-parameters' includes `left' and `top'
220 parameters, they determine the x and y position where the tooltip
221 is displayed. Otherwise, the tooltip pops at offsets specified by
222 `tooltip-x-offset' and `tooltip-y-offset' from the current mouse
223 position.
224
225 Optional second arg USE-ECHO-AREA non-nil means to show tooltip
226 in echo area."
227 (if use-echo-area
228 (message "%s" text)
229 (condition-case error
230 (let ((params (copy-sequence tooltip-frame-parameters))
231 (fg (face-attribute 'tooltip :foreground))
232 (bg (face-attribute 'tooltip :background)))
233 (when (stringp fg)
234 (setq params (tooltip-set-param params 'foreground-color fg))
235 (setq params (tooltip-set-param params 'border-color fg)))
236 (when (stringp bg)
237 (setq params (tooltip-set-param params 'background-color bg)))
238 (x-show-tip (propertize text 'face 'tooltip)
239 (selected-frame)
240 params
241 tooltip-hide-delay
242 tooltip-x-offset
243 tooltip-y-offset))
244 (error
245 (message "Error while displaying tooltip: %s" error)
246 (sit-for 1)
247 (message "%s" text)))))
248
249 (defun tooltip-hide (&optional ignored-arg)
250 "Hide a tooltip, if one is displayed.
251 Value is non-nil if tooltip was open."
252 (tooltip-cancel-delayed-tip)
253 (when (x-hide-tip)
254 (setq tooltip-hide-time (float-time))))
255
256 \f
257 ;;; Debugger-related functions
258
259 (defun tooltip-identifier-from-point (point)
260 "Extract the identifier at POINT, if any.
261 Value is nil if no identifier exists at point. Identifier extraction
262 is based on the current syntax table."
263 (save-excursion
264 (goto-char point)
265 (let ((start (progn (skip-syntax-backward "w_") (point))))
266 (unless (looking-at "[0-9]")
267 (skip-syntax-forward "w_")
268 (when (> (point) start)
269 (buffer-substring start (point)))))))
270
271 (defmacro tooltip-region-active-p ()
272 "Value is non-nil if the region is currently active."
273 (if (string-match "^GNU" (emacs-version))
274 `(and transient-mark-mode mark-active)
275 `(region-active-p)))
276
277 (defun tooltip-expr-to-print (event)
278 "Return an expression that should be printed for EVENT.
279 If a region is active and the mouse is inside the region, print
280 the region. Otherwise, figure out the identifier around the point
281 where the mouse is."
282 (save-excursion
283 (set-buffer (tooltip-event-buffer event))
284 (let ((point (posn-point (event-end event))))
285 (if (tooltip-region-active-p)
286 (when (and (<= (region-beginning) point) (<= point (region-end)))
287 (buffer-substring (region-beginning) (region-end)))
288 (tooltip-identifier-from-point point)))))
289
290 (defun tooltip-process-prompt-regexp (process)
291 "Return regexp matching the prompt of PROCESS at the end of a string.
292 The prompt is taken from the value of `comint-prompt-regexp' in
293 the buffer of PROCESS."
294 (let ((prompt-regexp (save-excursion
295 (set-buffer (process-buffer process))
296 comint-prompt-regexp)))
297 ;; Most start with `^' but the one for `sdb' cannot be easily
298 ;; stripped. Code the prompt for `sdb' fixed here.
299 (if (= (aref prompt-regexp 0) ?^)
300 (setq prompt-regexp (substring prompt-regexp 1))
301 (setq prompt-regexp "\\*"))
302 (concat "\n*" prompt-regexp "$")))
303
304 (defun tooltip-strip-prompt (process output)
305 "Return OUTPUT with any prompt of PROCESS stripped from its end."
306 (let ((prompt-regexp (tooltip-process-prompt-regexp process)))
307 (save-match-data
308 (when (string-match prompt-regexp output)
309 (setq output (substring output 0 (match-beginning 0)))))
310 output))
311
312 \f
313 ;;; Tooltip help.
314
315 (defvar tooltip-help-message nil
316 "The last help message received via `tooltip-show-help'.")
317
318 (defun tooltip-show-help (msg)
319 "Function installed as `show-help-function'.
320 MSG is either a help string to display, or nil to cancel the display."
321 (let ((previous-help tooltip-help-message))
322 (setq tooltip-help-message msg)
323 (cond ((null msg)
324 ;; Cancel display. This also cancels a delayed tip, if
325 ;; there is one.
326 (tooltip-hide))
327 ((equal previous-help msg)
328 ;; Same help as before (but possibly the mouse has moved).
329 ;; Keep what we have.
330 )
331 (t
332 ;; A different help. Remove a previous tooltip, and
333 ;; display a new one, with some delay.
334 (tooltip-hide)
335 (tooltip-start-delayed-tip)))))
336
337 (defun tooltip-help-tips (event)
338 "Hook function to display a help tooltip.
339 This is installed on the hook `tooltip-hook', which is run when
340 the timer with id `tooltip-timeout-id' fires.
341 Value is non-nil if this function handled the tip."
342 (when (stringp tooltip-help-message)
343 (tooltip-show tooltip-help-message tooltip-use-echo-area)
344 t))
345
346 (provide 'tooltip)
347
348 ;; arch-tag: 3d61135e-4618-4a78-af28-183f6df5636f
349 ;;; tooltip.el ends here