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