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