Commit | Line | Data |
---|---|---|
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. |
43 | With ARG, turn Tooltip mode on if and only if ARG is positive. | |
4c08ab31 | 44 | When this minor mode is enabled, Emacs displays help text |
42cc79e6 RS |
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." | |
e85dcd95 | 51 | :global t |
8431201e CY |
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 | |
e85dcd95 NR |
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-hook '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-hook 'tooltip-help-tips)) | |
66 | (setq show-help-function | |
14c0a34d | 67 | (if tooltip-mode 'tooltip-show-help 'tooltip-show-help-non-mode))) |
7840ced1 | 68 | |
e3947513 JL |
69 | \f |
70 | ;;; Customizable settings | |
7840ced1 | 71 | |
88751b11 | 72 | (defcustom tooltip-delay 0.7 |
7840ced1 | 73 | "Seconds to wait before displaying a tooltip the first time." |
7840ced1 GM |
74 | :type 'number |
75 | :group 'tooltip) | |
76 | ||
7840ced1 GM |
77 | (defcustom tooltip-short-delay 0.1 |
78 | "Seconds to wait between subsequent tooltips on different items." | |
7840ced1 GM |
79 | :type 'number |
80 | :group 'tooltip) | |
81 | ||
7840ced1 | 82 | (defcustom tooltip-recent-seconds 1 |
01b23b99 DL |
83 | "Display tooltips if changing tip items within this many seconds. |
84 | Do so after `tooltip-short-delay'." | |
7840ced1 GM |
85 | :type 'number |
86 | :group 'tooltip) | |
87 | ||
88751b11 | 88 | (defcustom tooltip-hide-delay 10 |
a93c8ba8 | 89 | "Hide tooltips automatically after this many seconds." |
a93c8ba8 GM |
90 | :type 'number |
91 | :group 'tooltip) | |
92 | ||
5fddd8e6 | 93 | (defcustom tooltip-x-offset 5 |
37a29d46 | 94 | "X offset, in pixels, for the display of tooltips. |
e3947513 JL |
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. | |
7142670a EZ |
99 | |
100 | If `tooltip-frame-parameters' includes the `left' parameter, | |
101 | the value of `tooltip-x-offset' is ignored." | |
5fddd8e6 | 102 | :type 'integer |
e4df9b40 GM |
103 | :group 'tooltip) |
104 | ||
48736093 | 105 | (defcustom tooltip-y-offset +20 |
37a29d46 | 106 | "Y offset, in pixels, for the display of tooltips. |
e3947513 JL |
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. | |
7142670a EZ |
111 | |
112 | If `tooltip-frame-parameters' includes the `top' parameter, | |
113 | the value of `tooltip-y-offset' is ignored." | |
5fddd8e6 | 114 | :type 'integer |
e4df9b40 GM |
115 | :group 'tooltip) |
116 | ||
7840ced1 GM |
117 | (defcustom tooltip-frame-parameters |
118 | '((name . "tooltip") | |
5fddd8e6 | 119 | (internal-border-width . 2) |
7840ced1 | 120 | (border-width . 1)) |
7142670a EZ |
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." | |
7840ced1 | 125 | :type 'sexp |
7840ced1 GM |
126 | :group 'tooltip) |
127 | ||
2621f5a9 GM |
128 | (defface tooltip |
129 | '((((class color)) | |
39440204 JPW |
130 | :background "lightyellow" |
131 | :foreground "black" | |
132 | :inherit variable-pitch) | |
133 | (t | |
134 | :inherit variable-pitch)) | |
2621f5a9 | 135 | "Face for tooltips." |
daf96a41 JL |
136 | :group 'tooltip |
137 | :group 'basic-faces) | |
2621f5a9 | 138 | |
90aff7c6 | 139 | (defcustom tooltip-use-echo-area nil |
42cc79e6 RS |
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'." | |
90aff7c6 | 143 | :type 'boolean |
90aff7c6 NR |
144 | :group 'tooltip) |
145 | ||
7840ced1 GM |
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 | ||
7840ced1 GM |
154 | (defvar tooltip-timeout-id nil |
155 | "The id of the timeout started when Emacs becomes idle.") | |
156 | ||
7840ced1 GM |
157 | (defvar tooltip-last-mouse-motion-event nil |
158 | "A copy of the last mouse motion event seen.") | |
159 | ||
7840ced1 GM |
160 | (defvar tooltip-hide-time nil |
161 | "Time when the last tooltip was hidden.") | |
162 | ||
1af98f07 RS |
163 | (defvar gud-tooltip-mode) ;; Prevent warning. |
164 | ||
7840ced1 GM |
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 | ||
7840ced1 GM |
173 | \f |
174 | ;;; Timeout for tooltip display | |
175 | ||
7840ced1 GM |
176 | (defun tooltip-delay () |
177 | "Return the delay in seconds for the next tooltip." | |
ece5f847 SM |
178 | (if (and tooltip-hide-time |
179 | (< (- (float-time) tooltip-hide-time) tooltip-recent-seconds)) | |
180 | tooltip-short-delay | |
181 | tooltip-delay)) | |
7840ced1 | 182 | |
06f76f9f | 183 | (defun tooltip-cancel-delayed-tip () |
7840ced1 GM |
184 | "Disable the tooltip timeout." |
185 | (when tooltip-timeout-id | |
186 | (disable-timeout tooltip-timeout-id) | |
187 | (setq tooltip-timeout-id nil))) | |
188 | ||
06f76f9f | 189 | (defun tooltip-start-delayed-tip () |
04cedb11 | 190 | "Add a one-shot timeout to call function `tooltip-timeout'." |
7840ced1 GM |
191 | (setq tooltip-timeout-id |
192 | (add-timeout (tooltip-delay) 'tooltip-timeout nil))) | |
193 | ||
7840ced1 | 194 | (defun tooltip-timeout (object) |
04cedb11 | 195 | "Function called when timer with id `tooltip-timeout-id' fires." |
7840ced1 GM |
196 | (run-hook-with-args-until-success 'tooltip-hook |
197 | tooltip-last-mouse-motion-event)) | |
198 | ||
7840ced1 | 199 | \f |
7840ced1 GM |
200 | ;;; Displaying tips |
201 | ||
2621f5a9 | 202 | (defun tooltip-set-param (alist key value) |
4b2bb9be | 203 | "Change the value of KEY in alist ALIST to VALUE. |
f1180544 | 204 | If there's no association for KEY in ALIST, add one, otherwise |
2621f5a9 GM |
205 | change the existing association. Value is the resulting alist." |
206 | (let ((param (assq key alist))) | |
207 | (if (consp param) | |
208 | (setcdr param value) | |
209 | (push (cons key value) alist)) | |
210 | alist)) | |
211 | ||
aa360da1 GM |
212 | (declare-function x-show-tip "xfns.c" |
213 | (string &optional frame parms timeout dx dy)) | |
214 | ||
cae07240 | 215 | (defun tooltip-show (text &optional use-echo-area) |
7142670a EZ |
216 | "Show a tooltip window displaying TEXT. |
217 | ||
1d6197fb | 218 | Text larger than `x-max-tooltip-size' is clipped. |
7142670a EZ |
219 | |
220 | If the alist in `tooltip-frame-parameters' includes `left' and `top' | |
221 | parameters, they determine the x and y position where the tooltip | |
222 | is displayed. Otherwise, the tooltip pops at offsets specified by | |
223 | `tooltip-x-offset' and `tooltip-y-offset' from the current mouse | |
1d6197fb NR |
224 | position. |
225 | ||
cae07240 KS |
226 | Optional second arg USE-ECHO-AREA non-nil means to show tooltip |
227 | in echo area." | |
228 | (if use-echo-area | |
14c0a34d | 229 | (tooltip-show-help-non-mode text) |
e5603149 | 230 | (condition-case error |
2621f5a9 GM |
231 | (let ((params (copy-sequence tooltip-frame-parameters)) |
232 | (fg (face-attribute 'tooltip :foreground)) | |
233 | (bg (face-attribute 'tooltip :background))) | |
06f76f9f GM |
234 | (when (stringp fg) |
235 | (setq params (tooltip-set-param params 'foreground-color fg)) | |
236 | (setq params (tooltip-set-param params 'border-color fg))) | |
237 | (when (stringp bg) | |
238 | (setq params (tooltip-set-param params 'background-color bg))) | |
2621f5a9 GM |
239 | (x-show-tip (propertize text 'face 'tooltip) |
240 | (selected-frame) | |
f3b05e99 | 241 | params |
a93c8ba8 | 242 | tooltip-hide-delay |
2621f5a9 GM |
243 | tooltip-x-offset |
244 | tooltip-y-offset)) | |
f1180544 | 245 | (error |
e5603149 GM |
246 | (message "Error while displaying tooltip: %s" error) |
247 | (sit-for 1) | |
248 | (message "%s" text))))) | |
249 | ||
aa360da1 GM |
250 | (declare-function x-hide-tip "xfns.c" ()) |
251 | ||
7840ced1 GM |
252 | (defun tooltip-hide (&optional ignored-arg) |
253 | "Hide a tooltip, if one is displayed. | |
254 | Value is non-nil if tooltip was open." | |
06f76f9f | 255 | (tooltip-cancel-delayed-tip) |
7840ced1 | 256 | (when (x-hide-tip) |
a1f84f6d | 257 | (setq tooltip-hide-time (float-time)))) |
7840ced1 | 258 | |
7840ced1 GM |
259 | \f |
260 | ;;; Debugger-related functions | |
261 | ||
262 | (defun tooltip-identifier-from-point (point) | |
263 | "Extract the identifier at POINT, if any. | |
264 | Value is nil if no identifier exists at point. Identifier extraction | |
265 | is based on the current syntax table." | |
266 | (save-excursion | |
267 | (goto-char point) | |
268 | (let ((start (progn (skip-syntax-backward "w_") (point)))) | |
269 | (unless (looking-at "[0-9]") | |
270 | (skip-syntax-forward "w_") | |
271 | (when (> (point) start) | |
272 | (buffer-substring start (point))))))) | |
273 | ||
7840ced1 | 274 | (defmacro tooltip-region-active-p () |
8af7e4bd | 275 | "Value is non-nil if the region should override command actions." |
71dffedd | 276 | `(use-region-p)) |
7840ced1 | 277 | |
7840ced1 GM |
278 | (defun tooltip-expr-to-print (event) |
279 | "Return an expression that should be printed for EVENT. | |
280 | If a region is active and the mouse is inside the region, print | |
281 | the region. Otherwise, figure out the identifier around the point | |
282 | where the mouse is." | |
ece5f847 | 283 | (with-current-buffer (tooltip-event-buffer event) |
7840ced1 GM |
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 | ||
7840ced1 GM |
290 | (defun tooltip-process-prompt-regexp (process) |
291 | "Return regexp matching the prompt of PROCESS at the end of a string. | |
04cedb11 JB |
292 | The prompt is taken from the value of `comint-prompt-regexp' in |
293 | the buffer of PROCESS." | |
ece5f847 | 294 | (let ((prompt-regexp (with-current-buffer (process-buffer process) |
7840ced1 | 295 | comint-prompt-regexp))) |
ece5f847 SM |
296 | (concat "\n*" |
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 | (substring prompt-regexp 1) | |
301 | "\\*") | |
302 | "$"))) | |
7840ced1 | 303 | |
7840ced1 GM |
304 | (defun tooltip-strip-prompt (process output) |
305 | "Return OUTPUT with any prompt of PROCESS stripped from its end." | |
ece5f847 SM |
306 | (save-match-data |
307 | (if (string-match (tooltip-process-prompt-regexp process) output) | |
308 | (substring output 0 (match-beginning 0)) | |
309 | output))) | |
7840ced1 | 310 | |
7840ced1 | 311 | \f |
7840ced1 GM |
312 | ;;; Tooltip help. |
313 | ||
314 | (defvar tooltip-help-message nil | |
90aff7c6 | 315 | "The last help message received via `tooltip-show-help'.") |
7840ced1 | 316 | |
ece5f847 SM |
317 | (defvar tooltip-previous-message nil |
318 | "The previous content of the echo area.") | |
319 | ||
320 | (defun tooltip-show-help-non-mode (help) | |
14c0a34d | 321 | "Function installed as `show-help-function' when tooltip is off." |
ece5f847 SM |
322 | (when (and (not (window-minibuffer-p)) ;Don't overwrite minibuffer contents. |
323 | ;; Don't know how to reproduce it in Elisp: | |
324 | ;; Don't overwrite a keystroke echo. | |
325 | ;; (NILP (echo_message_buffer) || ok_to_overwrite_keystroke_echo) | |
326 | (not cursor-in-echo-area)) ;Don't overwrite a prompt. | |
327 | (cond | |
328 | ((stringp help) | |
329 | (unless tooltip-previous-message | |
330 | (setq tooltip-previous-message (current-message))) | |
331 | (let ((message-truncate-lines t) | |
332 | (message-log-max nil)) | |
333 | (message "%s" (replace-regexp-in-string "\n" ", " help)))) | |
334 | ((stringp tooltip-previous-message) | |
335 | (let ((message-log-max nil)) | |
336 | (message "%s" tooltip-previous-message) | |
337 | (setq tooltip-previous-message nil))) | |
338 | (t | |
339 | (message nil))))) | |
340 | ||
14c0a34d | 341 | |
90aff7c6 | 342 | (defun tooltip-show-help (msg) |
7840ced1 GM |
343 | "Function installed as `show-help-function'. |
344 | MSG is either a help string to display, or nil to cancel the display." | |
86f0d417 | 345 | (let ((previous-help tooltip-help-message)) |
7840ced1 GM |
346 | (setq tooltip-help-message msg) |
347 | (cond ((null msg) | |
06f76f9f GM |
348 | ;; Cancel display. This also cancels a delayed tip, if |
349 | ;; there is one. | |
7840ced1 | 350 | (tooltip-hide)) |
06f76f9f GM |
351 | ((equal previous-help msg) |
352 | ;; Same help as before (but possibly the mouse has moved). | |
353 | ;; Keep what we have. | |
354 | ) | |
7840ced1 | 355 | (t |
f1180544 | 356 | ;; A different help. Remove a previous tooltip, and |
06f76f9f GM |
357 | ;; display a new one, with some delay. |
358 | (tooltip-hide) | |
359 | (tooltip-start-delayed-tip))))) | |
7840ced1 | 360 | |
7840ced1 GM |
361 | (defun tooltip-help-tips (event) |
362 | "Hook function to display a help tooltip. | |
06f76f9f | 363 | This is installed on the hook `tooltip-hook', which is run when |
04cedb11 | 364 | the timer with id `tooltip-timeout-id' fires. |
7840ced1 GM |
365 | Value is non-nil if this function handled the tip." |
366 | (when (stringp tooltip-help-message) | |
90aff7c6 | 367 | (tooltip-show tooltip-help-message tooltip-use-echo-area) |
7840ced1 GM |
368 | t)) |
369 | ||
5ce0fb91 | 370 | (provide 'tooltip) |
7840ced1 | 371 | |
18385083 | 372 | ;; arch-tag: 3d61135e-4618-4a78-af28-183f6df5636f |
7840ced1 | 373 | ;;; tooltip.el ends here |