tooltip.el 13 KB

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