Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / tooltip.el
blob5987b00f92efeca06ef004d64b11ed0f5758a20a
1 ;;; tooltip.el --- show tooltip windows
3 ;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Gerd Moellmann <gerd@acm.org>
7 ;; Keywords: help c mouse tools
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 (defvar comint-prompt-regexp)
31 (defgroup tooltip nil
32 "Customization group for the `tooltip' package."
33 :group 'help
34 :group 'gud
35 :group 'mouse
36 :group 'tools
37 :version "21.1"
38 :tag "Tool Tips")
40 ;;; Switching tooltips on/off
42 (define-minor-mode tooltip-mode
43 "Toggle Tooltip mode.
44 With ARG, turn Tooltip mode on if and only if ARG is positive.
45 When this minor mode is enabled, Emacs displays help text
46 in a pop-up window for buttons and menu items that you put the mouse on.
47 \(However, if `tooltip-use-echo-area' is non-nil, this and
48 all pop-up help appears in the echo area.)
50 When Tooltip mode is disabled, Emacs displays one line of
51 the help text in the echo area, and does not make a pop-up window."
52 :global t
53 ;; Even if we start on a text-only terminal, make this non-nil by
54 ;; default because we can open a graphical frame later (multi-tty).
55 :init-value t
56 :initialize 'custom-initialize-delay
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-functions '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-functions 'tooltip-help-tips))
67 (setq show-help-function
68 (if tooltip-mode 'tooltip-show-help 'tooltip-show-help-non-mode)))
71 ;;; Customizable settings
73 (defcustom tooltip-delay 0.7
74 "Seconds to wait before displaying a tooltip the first time."
75 :type 'number
76 :group 'tooltip)
78 (defcustom tooltip-short-delay 0.1
79 "Seconds to wait between subsequent tooltips on different items."
80 :type 'number
81 :group 'tooltip)
83 (defcustom tooltip-recent-seconds 1
84 "Display tooltips if changing tip items within this many seconds.
85 Do so after `tooltip-short-delay'."
86 :type 'number
87 :group 'tooltip)
89 (defcustom tooltip-hide-delay 10
90 "Hide tooltips automatically after this many seconds."
91 :type 'number
92 :group 'tooltip)
94 (defcustom tooltip-x-offset 5
95 "X offset, in pixels, for the display of tooltips.
96 The offset is the distance between the X position of the mouse and
97 the left border of the tooltip window. It must be chosen so that the
98 tooltip window doesn't contain the mouse when it pops up, or it may
99 interfere with clicking where you wish.
101 If `tooltip-frame-parameters' includes the `left' parameter,
102 the value of `tooltip-x-offset' is ignored."
103 :type 'integer
104 :group 'tooltip)
106 (defcustom tooltip-y-offset +20
107 "Y offset, in pixels, for the display of tooltips.
108 The offset is the distance between the Y position of the mouse and
109 the top border of the tooltip window. It must be chosen so that the
110 tooltip window doesn't contain the mouse when it pops up, or it may
111 interfere with clicking where you wish.
113 If `tooltip-frame-parameters' includes the `top' parameter,
114 the value of `tooltip-y-offset' is ignored."
115 :type 'integer
116 :group 'tooltip)
118 (defcustom tooltip-frame-parameters
119 '((name . "tooltip")
120 (internal-border-width . 2)
121 (border-width . 1))
122 "Frame parameters used for tooltips.
124 If `left' or `top' parameters are included, they specify the absolute
125 position to pop up the tooltip.
127 Note that font and color parameters are ignored, and the attributes
128 of the `tooltip' face are used instead."
129 :type 'sexp
130 :group 'tooltip)
132 (defface tooltip
133 '((((class color))
134 :background "lightyellow"
135 :foreground "black"
136 :inherit variable-pitch)
138 :inherit variable-pitch))
139 "Face for tooltips."
140 :group 'tooltip
141 :group 'basic-faces)
143 (defcustom tooltip-use-echo-area nil
144 "Use the echo area instead of tooltip frames for help and GUD tooltips.
145 To display multi-line help text in the echo area, set this to t
146 and enable `tooltip-mode'."
147 :type 'boolean
148 :group 'tooltip)
151 ;;; Variables that are not customizable.
153 (defvar tooltip-functions nil
154 "Functions to call to display tooltips.
155 Each function is called with one argument EVENT which is a copy
156 of the last mouse movement event that occurred. If one of these
157 functions displays the tooltip, it should return non-nil and the
158 rest are not called.")
160 (define-obsolete-variable-alias 'tooltip-hook 'tooltip-functions "23.1")
162 (defvar tooltip-timeout-id nil
163 "The id of the timeout started when Emacs becomes idle.")
165 (defvar tooltip-last-mouse-motion-event nil
166 "A copy of the last mouse motion event seen.")
168 (defvar tooltip-hide-time nil
169 "Time when the last tooltip was hidden.")
171 (defvar gud-tooltip-mode) ;; Prevent warning.
173 ;;; Event accessors
175 (defun tooltip-event-buffer (event)
176 "Return the buffer over which event EVENT occurred.
177 This might return nil if the event did not occur over a buffer."
178 (let ((window (posn-window (event-end event))))
179 (and window (window-buffer window))))
182 ;;; Timeout for tooltip display
184 (defun tooltip-delay ()
185 "Return the delay in seconds for the next tooltip."
186 (if (and tooltip-hide-time
187 (< (- (float-time) tooltip-hide-time) tooltip-recent-seconds))
188 tooltip-short-delay
189 tooltip-delay))
191 (defun tooltip-cancel-delayed-tip ()
192 "Disable the tooltip timeout."
193 (when tooltip-timeout-id
194 (disable-timeout tooltip-timeout-id)
195 (setq tooltip-timeout-id nil)))
197 (defun tooltip-start-delayed-tip ()
198 "Add a one-shot timeout to call function `tooltip-timeout'."
199 (setq tooltip-timeout-id
200 (add-timeout (tooltip-delay) 'tooltip-timeout nil)))
202 (defun tooltip-timeout (object)
203 "Function called when timer with id `tooltip-timeout-id' fires."
204 (run-hook-with-args-until-success 'tooltip-functions
205 tooltip-last-mouse-motion-event))
208 ;;; Displaying tips
210 (defun tooltip-set-param (alist key value)
211 "Change the value of KEY in alist ALIST to VALUE.
212 If there's no association for KEY in ALIST, add one, otherwise
213 change the existing association. Value is the resulting alist."
214 (let ((param (assq key alist)))
215 (if (consp param)
216 (setcdr param value)
217 (push (cons key value) alist))
218 alist))
220 (declare-function x-show-tip "xfns.c"
221 (string &optional frame parms timeout dx dy))
223 (defun tooltip-show (text &optional use-echo-area)
224 "Show a tooltip window displaying TEXT.
226 Text larger than `x-max-tooltip-size' is clipped.
228 If the alist in `tooltip-frame-parameters' includes `left' and `top'
229 parameters, they determine the x and y position where the tooltip
230 is displayed. Otherwise, the tooltip pops at offsets specified by
231 `tooltip-x-offset' and `tooltip-y-offset' from the current mouse
232 position.
234 Optional second arg USE-ECHO-AREA non-nil means to show tooltip
235 in echo area."
236 (if use-echo-area
237 (tooltip-show-help-non-mode text)
238 (condition-case error
239 (let ((params (copy-sequence tooltip-frame-parameters))
240 (fg (face-attribute 'tooltip :foreground))
241 (bg (face-attribute 'tooltip :background)))
242 (when (stringp fg)
243 (setq params (tooltip-set-param params 'foreground-color fg))
244 (setq params (tooltip-set-param params 'border-color fg)))
245 (when (stringp bg)
246 (setq params (tooltip-set-param params 'background-color bg)))
247 (x-show-tip (propertize text 'face 'tooltip)
248 (selected-frame)
249 params
250 tooltip-hide-delay
251 tooltip-x-offset
252 tooltip-y-offset))
253 (error
254 (message "Error while displaying tooltip: %s" error)
255 (sit-for 1)
256 (message "%s" text)))))
258 (declare-function x-hide-tip "xfns.c" ())
260 (defun tooltip-hide (&optional ignored-arg)
261 "Hide a tooltip, if one is displayed.
262 Value is non-nil if tooltip was open."
263 (tooltip-cancel-delayed-tip)
264 (when (x-hide-tip)
265 (setq tooltip-hide-time (float-time))))
268 ;;; Debugger-related functions
270 (defun tooltip-identifier-from-point (point)
271 "Extract the identifier at POINT, if any.
272 Value is nil if no identifier exists at point. Identifier extraction
273 is based on the current syntax table."
274 (save-excursion
275 (goto-char point)
276 (let ((start (progn (skip-syntax-backward "w_") (point))))
277 (unless (looking-at "[0-9]")
278 (skip-syntax-forward "w_")
279 (when (> (point) start)
280 (buffer-substring start (point)))))))
282 (defmacro tooltip-region-active-p ()
283 "Value is non-nil if the region should override command actions."
284 `(use-region-p))
286 (defun tooltip-expr-to-print (event)
287 "Return an expression that should be printed for EVENT.
288 If a region is active and the mouse is inside the region, print
289 the region. Otherwise, figure out the identifier around the point
290 where the mouse is."
291 (with-current-buffer (tooltip-event-buffer event)
292 (let ((point (posn-point (event-end event))))
293 (if (tooltip-region-active-p)
294 (when (and (<= (region-beginning) point) (<= point (region-end)))
295 (buffer-substring (region-beginning) (region-end)))
296 (tooltip-identifier-from-point point)))))
298 (defun tooltip-process-prompt-regexp (process)
299 "Return regexp matching the prompt of PROCESS at the end of a string.
300 The prompt is taken from the value of `comint-prompt-regexp' in
301 the buffer of PROCESS."
302 (let ((prompt-regexp (with-current-buffer (process-buffer process)
303 comint-prompt-regexp)))
304 (concat "\n*"
305 ;; Most start with `^' but the one for `sdb' cannot be easily
306 ;; stripped. Code the prompt for `sdb' fixed here.
307 (if (= (aref prompt-regexp 0) ?^)
308 (substring prompt-regexp 1)
309 "\\*")
310 "$")))
312 (defun tooltip-strip-prompt (process output)
313 "Return OUTPUT with any prompt of PROCESS stripped from its end."
314 (save-match-data
315 (if (string-match (tooltip-process-prompt-regexp process) output)
316 (substring output 0 (match-beginning 0))
317 output)))
320 ;;; Tooltip help.
322 (defvar tooltip-help-message nil
323 "The last help message received via `show-help-function'.
324 This is used by `tooltip-show-help' and
325 `tooltip-show-help-non-mode'.")
327 (defvar tooltip-previous-message nil
328 "The previous content of the echo area.")
330 (defun tooltip-show-help-non-mode (help)
331 "Function installed as `show-help-function' when Tooltip mode is off.
332 It is also called if Tooltip mode is on, for text-only displays."
333 (when (and (not (window-minibuffer-p)) ;Don't overwrite minibuffer contents.
334 (not cursor-in-echo-area)) ;Don't overwrite a prompt.
335 (cond
336 ((stringp help)
337 (setq help (replace-regexp-in-string "\n" ", " help))
338 (unless (or tooltip-previous-message
339 (string-equal help (current-message))
340 (and (stringp tooltip-help-message)
341 (string-equal tooltip-help-message
342 (current-message))))
343 (setq tooltip-previous-message (current-message)))
344 (setq tooltip-help-message help)
345 (let ((message-truncate-lines t)
346 (message-log-max nil))
347 (message "%s" help)))
348 ((stringp tooltip-previous-message)
349 (let ((message-log-max nil))
350 (message "%s" tooltip-previous-message)
351 (setq tooltip-previous-message nil)))
353 (message nil)))))
355 (defun tooltip-show-help (msg)
356 "Function installed as `show-help-function'.
357 MSG is either a help string to display, or nil to cancel the display."
358 (if (display-graphic-p)
359 (let ((previous-help tooltip-help-message))
360 (setq tooltip-help-message msg)
361 (cond ((null msg)
362 ;; Cancel display. This also cancels a delayed tip, if
363 ;; there is one.
364 (tooltip-hide))
365 ((equal previous-help msg)
366 ;; Same help as before (but possibly the mouse has moved).
367 ;; Keep what we have.
370 ;; A different help. Remove a previous tooltip, and
371 ;; display a new one, with some delay.
372 (tooltip-hide)
373 (tooltip-start-delayed-tip))))
374 ;; On text-only displays, try `tooltip-show-help-non-mode'.
375 (tooltip-show-help-non-mode msg)))
377 (defun tooltip-help-tips (event)
378 "Hook function to display a help tooltip.
379 This is installed on the hook `tooltip-functions', which
380 is run when the timer with id `tooltip-timeout-id' fires.
381 Value is non-nil if this function handled the tip."
382 (when (stringp tooltip-help-message)
383 (tooltip-show tooltip-help-message tooltip-use-echo-area)
386 (provide 'tooltip)
388 ;; arch-tag: 3d61135e-4618-4a78-af28-183f6df5636f
389 ;;; tooltip.el ends here