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