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