Commit | Line | Data |
---|---|---|
b670783a | 1 | ;;; tooltip.el --- show tooltip windows |
7840ced1 | 2 | |
0d30b337 TTN |
3 | ;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004, |
4 | ;; 2005 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 | ||
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 2, or (at your option) | |
14 | ;; 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; see the file COPYING. If not, write to the | |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
7840ced1 GM |
25 | |
26 | ;;; Commentary: | |
27 | ||
7840ced1 GM |
28 | ;;; Code: |
29 | ||
5673b222 JB |
30 | (defvar comint-prompt-regexp) |
31 | ||
e3947513 JL |
32 | (defgroup tooltip nil |
33 | "Customization group for the `tooltip' package." | |
34 | :group 'help | |
35 | :group 'gud | |
36 | :group 'mouse | |
37 | :group 'tools | |
38 | :version "21.1" | |
39 | :tag "Tool Tips") | |
40 | \f | |
e85dcd95 NR |
41 | ;;; Switching tooltips on/off |
42 | ||
43 | ;; We don't set track-mouse globally because this is a big redisplay | |
44 | ;; problem in buffers having a pre-command-hook or such installed, | |
45 | ;; which does a set-buffer, like the summary buffer of Gnus. Calling | |
46 | ;; set-buffer prevents redisplay optimizations, so every mouse motion | |
47 | ;; would be accompanied by a full redisplay. | |
48 | ||
49 | (define-minor-mode tooltip-mode | |
50 | "Toggle Tooltip display. | |
51 | With ARG, turn tooltip mode on if and only if ARG is positive." | |
52 | :global t | |
53 | :init-value (not (or noninteractive | |
54 | emacs-basic-display | |
55 | (not (display-graphic-p)) | |
56 | (not (fboundp 'x-show-tip)))) | |
57 | :initialize 'custom-initialize-safe-default | |
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) | |
64 | (add-hook 'tooltip-hook 'tooltip-help-tips)) | |
65 | (unless (and (boundp 'gud-tooltip-mode) gud-tooltip-mode) | |
66 | (remove-hook 'pre-command-hook 'tooltip-hide)) | |
67 | (remove-hook 'tooltip-hook 'tooltip-help-tips)) | |
68 | (setq show-help-function | |
69 | (if tooltip-mode 'tooltip-show-help nil))) | |
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. |
86 | Do 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 |
97 | The offset is the distance between the X position of the mouse and |
98 | the left border of the tooltip window. It must be chosen so that the | |
99 | tooltip window doesn't contain the mouse when it pops up, or it may | |
100 | interfere with clicking where you wish. | |
7142670a EZ |
101 | |
102 | If `tooltip-frame-parameters' includes the `left' parameter, | |
103 | the 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 |
109 | The offset is the distance between the Y position of the mouse and |
110 | the top border of the tooltip window. It must be chosen so that the | |
111 | tooltip window doesn't contain the mouse when it pops up, or it may | |
112 | interfere with clicking where you wish. | |
7142670a EZ |
113 | |
114 | If `tooltip-frame-parameters' includes the `top' parameter, | |
115 | the value of `tooltip-y-offset' is ignored." | |
5fddd8e6 | 116 | :type 'integer |
e4df9b40 GM |
117 | :group 'tooltip) |
118 | ||
7840ced1 GM |
119 | (defcustom tooltip-frame-parameters |
120 | '((name . "tooltip") | |
5fddd8e6 | 121 | (internal-border-width . 2) |
7840ced1 | 122 | (border-width . 1)) |
7142670a EZ |
123 | "Frame parameters used for tooltips. |
124 | ||
125 | If `left' or `top' parameters are included, they specify the absolute | |
126 | position to pop up the tooltip." | |
7840ced1 | 127 | :type 'sexp |
7840ced1 GM |
128 | :group 'tooltip) |
129 | ||
2621f5a9 GM |
130 | (defface tooltip |
131 | '((((class color)) | |
39440204 JPW |
132 | :background "lightyellow" |
133 | :foreground "black" | |
134 | :inherit variable-pitch) | |
135 | (t | |
136 | :inherit variable-pitch)) | |
2621f5a9 | 137 | "Face for tooltips." |
daf96a41 JL |
138 | :group 'tooltip |
139 | :group 'basic-faces) | |
2621f5a9 | 140 | |
90aff7c6 NR |
141 | (defcustom tooltip-use-echo-area nil |
142 | "Use the echo area instead of tooltip frames for help and GUD tooltips." | |
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." | |
178 | (let ((delay tooltip-delay) | |
a1f84f6d | 179 | (now (float-time))) |
7840ced1 GM |
180 | (when (and tooltip-hide-time |
181 | (< (- now tooltip-hide-time) tooltip-recent-seconds)) | |
182 | (setq delay tooltip-short-delay)) | |
183 | delay)) | |
184 | ||
06f76f9f | 185 | (defun tooltip-cancel-delayed-tip () |
7840ced1 GM |
186 | "Disable the tooltip timeout." |
187 | (when tooltip-timeout-id | |
188 | (disable-timeout tooltip-timeout-id) | |
189 | (setq tooltip-timeout-id nil))) | |
190 | ||
06f76f9f | 191 | (defun tooltip-start-delayed-tip () |
04cedb11 | 192 | "Add a one-shot timeout to call function `tooltip-timeout'." |
7840ced1 GM |
193 | (setq tooltip-timeout-id |
194 | (add-timeout (tooltip-delay) 'tooltip-timeout nil))) | |
195 | ||
7840ced1 | 196 | (defun tooltip-timeout (object) |
04cedb11 | 197 | "Function called when timer with id `tooltip-timeout-id' fires." |
7840ced1 GM |
198 | (run-hook-with-args-until-success 'tooltip-hook |
199 | tooltip-last-mouse-motion-event)) | |
200 | ||
7840ced1 | 201 | \f |
7840ced1 GM |
202 | ;;; Displaying tips |
203 | ||
2621f5a9 | 204 | (defun tooltip-set-param (alist key value) |
4b2bb9be | 205 | "Change the value of KEY in alist ALIST to VALUE. |
f1180544 | 206 | If there's no association for KEY in ALIST, add one, otherwise |
2621f5a9 GM |
207 | change the existing association. Value is the resulting alist." |
208 | (let ((param (assq key alist))) | |
209 | (if (consp param) | |
210 | (setcdr param value) | |
211 | (push (cons key value) alist)) | |
212 | alist)) | |
213 | ||
cae07240 | 214 | (defun tooltip-show (text &optional use-echo-area) |
7142670a EZ |
215 | "Show a tooltip window displaying TEXT. |
216 | ||
1d6197fb | 217 | Text larger than `x-max-tooltip-size' is clipped. |
7142670a EZ |
218 | |
219 | If the alist in `tooltip-frame-parameters' includes `left' and `top' | |
220 | parameters, they determine the x and y position where the tooltip | |
221 | is displayed. Otherwise, the tooltip pops at offsets specified by | |
222 | `tooltip-x-offset' and `tooltip-y-offset' from the current mouse | |
1d6197fb NR |
223 | position. |
224 | ||
cae07240 KS |
225 | Optional second arg USE-ECHO-AREA non-nil means to show tooltip |
226 | in echo area." | |
227 | (if use-echo-area | |
72200f89 | 228 | (message "%s" text) |
e5603149 | 229 | (condition-case error |
2621f5a9 GM |
230 | (let ((params (copy-sequence tooltip-frame-parameters)) |
231 | (fg (face-attribute 'tooltip :foreground)) | |
232 | (bg (face-attribute 'tooltip :background))) | |
06f76f9f GM |
233 | (when (stringp fg) |
234 | (setq params (tooltip-set-param params 'foreground-color fg)) | |
235 | (setq params (tooltip-set-param params 'border-color fg))) | |
236 | (when (stringp bg) | |
237 | (setq params (tooltip-set-param params 'background-color bg))) | |
2621f5a9 GM |
238 | (x-show-tip (propertize text 'face 'tooltip) |
239 | (selected-frame) | |
f3b05e99 | 240 | params |
a93c8ba8 | 241 | tooltip-hide-delay |
2621f5a9 GM |
242 | tooltip-x-offset |
243 | tooltip-y-offset)) | |
f1180544 | 244 | (error |
e5603149 GM |
245 | (message "Error while displaying tooltip: %s" error) |
246 | (sit-for 1) | |
247 | (message "%s" text))))) | |
248 | ||
7840ced1 GM |
249 | (defun tooltip-hide (&optional ignored-arg) |
250 | "Hide a tooltip, if one is displayed. | |
251 | Value is non-nil if tooltip was open." | |
06f76f9f | 252 | (tooltip-cancel-delayed-tip) |
7840ced1 | 253 | (when (x-hide-tip) |
a1f84f6d | 254 | (setq tooltip-hide-time (float-time)))) |
7840ced1 | 255 | |
7840ced1 GM |
256 | \f |
257 | ;;; Debugger-related functions | |
258 | ||
259 | (defun tooltip-identifier-from-point (point) | |
260 | "Extract the identifier at POINT, if any. | |
261 | Value is nil if no identifier exists at point. Identifier extraction | |
262 | is based on the current syntax table." | |
263 | (save-excursion | |
264 | (goto-char point) | |
265 | (let ((start (progn (skip-syntax-backward "w_") (point)))) | |
266 | (unless (looking-at "[0-9]") | |
267 | (skip-syntax-forward "w_") | |
268 | (when (> (point) start) | |
269 | (buffer-substring start (point))))))) | |
270 | ||
7840ced1 GM |
271 | (defmacro tooltip-region-active-p () |
272 | "Value is non-nil if the region is currently active." | |
273 | (if (string-match "^GNU" (emacs-version)) | |
274 | `(and transient-mark-mode mark-active) | |
275 | `(region-active-p))) | |
276 | ||
7840ced1 GM |
277 | (defun tooltip-expr-to-print (event) |
278 | "Return an expression that should be printed for EVENT. | |
279 | If a region is active and the mouse is inside the region, print | |
280 | the region. Otherwise, figure out the identifier around the point | |
281 | where the mouse is." | |
282 | (save-excursion | |
283 | (set-buffer (tooltip-event-buffer event)) | |
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." | |
7840ced1 GM |
294 | (let ((prompt-regexp (save-excursion |
295 | (set-buffer (process-buffer process)) | |
296 | comint-prompt-regexp))) | |
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 | (setq prompt-regexp (substring prompt-regexp 1)) | |
301 | (setq prompt-regexp "\\*")) | |
302 | (concat "\n*" prompt-regexp "$"))) | |
303 | ||
7840ced1 GM |
304 | (defun tooltip-strip-prompt (process output) |
305 | "Return OUTPUT with any prompt of PROCESS stripped from its end." | |
306 | (let ((prompt-regexp (tooltip-process-prompt-regexp process))) | |
307 | (save-match-data | |
308 | (when (string-match prompt-regexp output) | |
309 | (setq output (substring output 0 (match-beginning 0))))) | |
310 | output)) | |
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 | |
90aff7c6 | 318 | (defun tooltip-show-help (msg) |
7840ced1 GM |
319 | "Function installed as `show-help-function'. |
320 | MSG is either a help string to display, or nil to cancel the display." | |
86f0d417 | 321 | (let ((previous-help tooltip-help-message)) |
7840ced1 GM |
322 | (setq tooltip-help-message msg) |
323 | (cond ((null msg) | |
06f76f9f GM |
324 | ;; Cancel display. This also cancels a delayed tip, if |
325 | ;; there is one. | |
7840ced1 | 326 | (tooltip-hide)) |
06f76f9f GM |
327 | ((equal previous-help msg) |
328 | ;; Same help as before (but possibly the mouse has moved). | |
329 | ;; Keep what we have. | |
330 | ) | |
7840ced1 | 331 | (t |
f1180544 | 332 | ;; A different help. Remove a previous tooltip, and |
06f76f9f GM |
333 | ;; display a new one, with some delay. |
334 | (tooltip-hide) | |
335 | (tooltip-start-delayed-tip))))) | |
7840ced1 | 336 | |
7840ced1 GM |
337 | (defun tooltip-help-tips (event) |
338 | "Hook function to display a help tooltip. | |
06f76f9f | 339 | This is installed on the hook `tooltip-hook', which is run when |
04cedb11 | 340 | the timer with id `tooltip-timeout-id' fires. |
7840ced1 GM |
341 | Value is non-nil if this function handled the tip." |
342 | (when (stringp tooltip-help-message) | |
90aff7c6 | 343 | (tooltip-show tooltip-help-message tooltip-use-echo-area) |
7840ced1 GM |
344 | t)) |
345 | ||
5ce0fb91 | 346 | (provide 'tooltip) |
7840ced1 | 347 | |
18385083 | 348 | ;; arch-tag: 3d61135e-4618-4a78-af28-183f6df5636f |
7840ced1 | 349 | ;;; tooltip.el ends here |