tooltip.el 13.3 KB
Newer Older
Pavel Janík's avatar
Pavel Janík committed
1
;;; tooltip.el --- show tooltip windows
Gerd Moellmann's avatar
Gerd Moellmann committed
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1997, 1999-2019 Free Software Foundation, Inc.
Gerd Moellmann's avatar
Gerd Moellmann committed
4 5 6

;; Author: Gerd Moellmann <gerd@acm.org>
;; Keywords: help c mouse tools
7
;; Package: emacs
Gerd Moellmann's avatar
Gerd Moellmann committed
8 9 10

;; This file is part of GNU Emacs.

11
;; GNU Emacs is free software: you can redistribute it and/or modify
Gerd Moellmann's avatar
Gerd Moellmann committed
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Gerd Moellmann's avatar
Gerd Moellmann committed
15 16 17 18 19 20 21

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
22
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Gerd Moellmann's avatar
Gerd Moellmann committed
23 24 25 26 27

;;; Commentary:

;;; Code:

28 29
(require 'syntax)

30 31
(defvar comint-prompt-regexp)

32 33 34 35 36 37 38 39 40
(defgroup tooltip nil
  "Customization group for the `tooltip' package."
  :group 'help
  :group 'gud
  :group 'mouse
  :group 'tools
  :version "21.1"
  :tag "Tool Tips")

41 42 43
;;; Switching tooltips on/off

(define-minor-mode tooltip-mode
44 45 46 47 48 49 50 51
  "Toggle Tooltip mode.

When this global minor mode is enabled, Emacs displays help
text (e.g. for buttons and menu items that you put the mouse on)
in a pop-up window.

When Tooltip mode is disabled, Emacs displays help text in the
echo area, instead of making a pop-up window."
52
  :global t
53 54 55
  ;; Even if we start on a text-only terminal, make this non-nil by
  ;; default because we can open a graphical frame later (multi-tty).
  :init-value t
56
  :initialize 'custom-initialize-delay
57
  :group 'tooltip
58
  (if (and tooltip-mode (fboundp 'x-show-tip))
59 60
      (progn
	(add-hook 'pre-command-hook 'tooltip-hide)
61
	(add-hook 'tooltip-functions 'tooltip-help-tips))
62 63
    (unless (and (boundp 'gud-tooltip-mode) gud-tooltip-mode)
      (remove-hook 'pre-command-hook 'tooltip-hide))
64
    (remove-hook 'tooltip-functions 'tooltip-help-tips))
65
  (setq show-help-function
66
	(if tooltip-mode 'tooltip-show-help 'tooltip-show-help-non-mode)))
Gerd Moellmann's avatar
Gerd Moellmann committed
67

68 69

;;; Customizable settings
Gerd Moellmann's avatar
Gerd Moellmann committed
70

71
(defcustom tooltip-delay 0.7
Gerd Moellmann's avatar
Gerd Moellmann committed
72 73 74 75 76 77 78 79 80 81
  "Seconds to wait before displaying a tooltip the first time."
  :type 'number
  :group 'tooltip)

(defcustom tooltip-short-delay 0.1
  "Seconds to wait between subsequent tooltips on different items."
  :type 'number
  :group 'tooltip)

(defcustom tooltip-recent-seconds 1
82 83
  "Display tooltips if changing tip items within this many seconds.
Do so after `tooltip-short-delay'."
Gerd Moellmann's avatar
Gerd Moellmann committed
84 85 86
  :type 'number
  :group 'tooltip)

87
(defcustom tooltip-hide-delay 10
88 89 90 91
  "Hide tooltips automatically after this many seconds."
  :type 'number
  :group 'tooltip)

92
(defcustom tooltip-x-offset 5
93
  "X offset, in pixels, for the display of tooltips.
94 95 96 97
The offset is the distance between the X position of the mouse and
the left border of the tooltip window.  It must be chosen so that the
tooltip window doesn't contain the mouse when it pops up, or it may
interfere with clicking where you wish.
98 99 100

If `tooltip-frame-parameters' includes the `left' parameter,
the value of `tooltip-x-offset' is ignored."
101
  :type 'integer
102 103
  :group 'tooltip)

104
(defcustom tooltip-y-offset +20
105
  "Y offset, in pixels, for the display of tooltips.
106 107 108 109
The offset is the distance between the Y position of the mouse and
the top border of the tooltip window.  It must be chosen so that the
tooltip window doesn't contain the mouse when it pops up, or it may
interfere with clicking where you wish.
110 111 112

If `tooltip-frame-parameters' includes the `top' parameter,
the value of `tooltip-y-offset' is ignored."
113
  :type 'integer
114 115
  :group 'tooltip)

Gerd Moellmann's avatar
Gerd Moellmann committed
116
(defcustom tooltip-frame-parameters
117
  '((name . "tooltip")
118
    (internal-border-width . 2)
119 120
    (border-width . 1)
    (no-special-glyphs . t))
121 122 123
  "Frame parameters used for tooltips.

If `left' or `top' parameters are included, they specify the absolute
124 125 126 127
position to pop up the tooltip.

Note that font and color parameters are ignored, and the attributes
of the `tooltip' face are used instead."
128 129 130
  :type '(repeat (cons :format "%v"
		       (symbol :tag "Parameter")
		       (sexp :tag "Value")))
131 132
  :group 'tooltip
  :version "26.1")
Gerd Moellmann's avatar
Gerd Moellmann committed
133

134 135
(defface tooltip
  '((((class color))
136 137 138 139 140
     :background "lightyellow"
     :foreground "black"
     :inherit variable-pitch)
    (t
     :inherit variable-pitch))
141
  "Face for tooltips."
142 143
  :group 'tooltip
  :group 'basic-faces)
144

145
(defcustom tooltip-use-echo-area nil
146
  "Use the echo area instead of tooltip frames for help and GUD tooltips.
147 148
This variable is obsolete; instead of setting it to t, disable
`tooltip-mode' (which has a similar effect)."
149 150 151
  :type 'boolean
  :group 'tooltip)

152
(make-obsolete-variable 'tooltip-use-echo-area
Glenn Morris's avatar
Glenn Morris committed
153
			"disable Tooltip mode instead" "24.1" 'set)
154

155 156 157 158 159 160 161 162 163 164 165 166
(defcustom tooltip-resize-echo-area nil
  "If non-nil, using the echo area for tooltips will resize the echo area.
By default, when the echo area is used for displaying tooltips,
the tooltip text is truncated if it exceeds a single screen line.
When this variable is non-nil, the text is not truncated; instead,
the echo area is resized as needed to accommodate the full text
of the tooltip.
This variable has effect only on GUI frames."
  :type 'boolean
  :group 'tooltip
  :version "27.1")

Gerd Moellmann's avatar
Gerd Moellmann committed
167 168 169

;;; Variables that are not customizable.

170 171
(define-obsolete-variable-alias 'tooltip-hook 'tooltip-functions "23.1")

172
(defvar tooltip-functions nil
Gerd Moellmann's avatar
Gerd Moellmann committed
173
  "Functions to call to display tooltips.
174 175 176 177 178
Each function is called with one argument EVENT which is a copy
of the last mouse movement event that occurred.  If one of these
functions displays the tooltip, it should return non-nil and the
rest are not called.")

Gerd Moellmann's avatar
Gerd Moellmann committed
179 180 181 182 183 184 185 186 187
(defvar tooltip-timeout-id nil
  "The id of the timeout started when Emacs becomes idle.")

(defvar tooltip-last-mouse-motion-event nil
  "A copy of the last mouse motion event seen.")

(defvar tooltip-hide-time nil
  "Time when the last tooltip was hidden.")

188 189
(defvar gud-tooltip-mode) ;; Prevent warning.

Gerd Moellmann's avatar
Gerd Moellmann committed
190 191 192 193 194 195 196 197 198 199 200 201 202
;;; Event accessors

(defun tooltip-event-buffer (event)
  "Return the buffer over which event EVENT occurred.
This might return nil if the event did not occur over a buffer."
  (let ((window (posn-window (event-end event))))
    (and window (window-buffer window))))


;;; Timeout for tooltip display

(defun tooltip-delay ()
  "Return the delay in seconds for the next tooltip."
203
  (if (and tooltip-hide-time
204 205
	   (time-less-p (time-since tooltip-hide-time)
			tooltip-recent-seconds))
206 207
      tooltip-short-delay
    tooltip-delay))
Gerd Moellmann's avatar
Gerd Moellmann committed
208

Gerd Moellmann's avatar
Gerd Moellmann committed
209
(defun tooltip-cancel-delayed-tip ()
Gerd Moellmann's avatar
Gerd Moellmann committed
210 211 212 213 214
  "Disable the tooltip timeout."
  (when tooltip-timeout-id
    (disable-timeout tooltip-timeout-id)
    (setq tooltip-timeout-id nil)))

Gerd Moellmann's avatar
Gerd Moellmann committed
215
(defun tooltip-start-delayed-tip ()
216
  "Add a one-shot timeout to call function `tooltip-timeout'."
Gerd Moellmann's avatar
Gerd Moellmann committed
217 218 219
  (setq tooltip-timeout-id
	(add-timeout (tooltip-delay) 'tooltip-timeout nil)))

220
(defun tooltip-timeout (_object)
221
  "Function called when timer with id `tooltip-timeout-id' fires."
222
  (run-hook-with-args-until-success 'tooltip-functions
Gerd Moellmann's avatar
Gerd Moellmann committed
223 224 225 226 227
				    tooltip-last-mouse-motion-event))


;;; Displaying tips

228
(defun tooltip-set-param (alist key value)
Eli Zaretskii's avatar
Eli Zaretskii committed
229
  "Change the value of KEY in alist ALIST to VALUE.
230
If there's no association for KEY in ALIST, add one, otherwise
231
change the existing association.  Value is the resulting alist."
232 233 234
  (declare (obsolete "use (setf (alist-get ..) ..) instead" "25.1"))
  (setf (alist-get key alist) value)
  alist)
235

236 237 238
(declare-function x-show-tip "xfns.c"
		  (string &optional frame parms timeout dx dy))

239
(defun tooltip-show (text &optional use-echo-area)
240 241
  "Show a tooltip window displaying TEXT.

242
Text larger than `x-max-tooltip-size' is clipped.
243 244 245 246 247

If the alist in `tooltip-frame-parameters' includes `left' and `top'
parameters, they determine the x and y position where the tooltip
is displayed.  Otherwise, the tooltip pops at offsets specified by
`tooltip-x-offset' and `tooltip-y-offset' from the current mouse
248 249
position.

250 251 252
Optional second arg USE-ECHO-AREA non-nil means to show tooltip
in echo area."
  (if use-echo-area
253
      (tooltip-show-help-non-mode text)
254
    (condition-case error
255 256 257
	(let ((params (copy-sequence tooltip-frame-parameters))
	      (fg (face-attribute 'tooltip :foreground))
	      (bg (face-attribute 'tooltip :background)))
Gerd Moellmann's avatar
Gerd Moellmann committed
258
	  (when (stringp fg)
259 260
	    (setf (alist-get 'foreground-color params) fg)
	    (setf (alist-get 'border-color params) fg))
Gerd Moellmann's avatar
Gerd Moellmann committed
261
	  (when (stringp bg)
262
	    (setf (alist-get 'background-color params) bg))
263 264
	  (x-show-tip (propertize text 'face 'tooltip)
		      (selected-frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
265
		      params
266
		      tooltip-hide-delay
267 268
		      tooltip-x-offset
		      tooltip-y-offset))
269
      (error
270 271 272 273
       (message "Error while displaying tooltip: %s" error)
       (sit-for 1)
       (message "%s" text)))))

274 275
(declare-function x-hide-tip "xfns.c" ())

276
(defun tooltip-hide (&optional _ignored-arg)
Gerd Moellmann's avatar
Gerd Moellmann committed
277 278
  "Hide a tooltip, if one is displayed.
Value is non-nil if tooltip was open."
Gerd Moellmann's avatar
Gerd Moellmann committed
279
  (tooltip-cancel-delayed-tip)
Gerd Moellmann's avatar
Gerd Moellmann committed
280
  (when (x-hide-tip)
Sam Steingold's avatar
Sam Steingold committed
281
    (setq tooltip-hide-time (float-time))))
Gerd Moellmann's avatar
Gerd Moellmann committed
282 283 284 285 286 287 288 289 290 291


;;; Debugger-related functions

(defun tooltip-identifier-from-point (point)
  "Extract the identifier at POINT, if any.
Value is nil if no identifier exists at point.  Identifier extraction
is based on the current syntax table."
  (save-excursion
    (goto-char point)
292 293 294 295 296
    (let* ((start (progn (skip-syntax-backward "w_") (point)))
	   (pstate (syntax-ppss)))
      (unless (or (looking-at "[0-9]")
		  (nth 3 pstate)
		  (nth 4 pstate))
Gerd Moellmann's avatar
Gerd Moellmann committed
297 298 299 300 301 302 303 304 305
	(skip-syntax-forward "w_")
	(when (> (point) start)
	  (buffer-substring start (point)))))))

(defun tooltip-expr-to-print (event)
  "Return an expression that should be printed for EVENT.
If a region is active and the mouse is inside the region, print
the region.  Otherwise, figure out the identifier around the point
where the mouse is."
306
  (with-current-buffer (tooltip-event-buffer event)
Gerd Moellmann's avatar
Gerd Moellmann committed
307
    (let ((point (posn-point (event-end event))))
308
      (if (use-region-p)
Gerd Moellmann's avatar
Gerd Moellmann committed
309 310 311 312 313 314
	  (when (and (<= (region-beginning) point) (<= point (region-end)))
	    (buffer-substring (region-beginning) (region-end)))
	(tooltip-identifier-from-point point)))))

(defun tooltip-process-prompt-regexp (process)
  "Return regexp matching the prompt of PROCESS at the end of a string.
315 316
The prompt is taken from the value of `comint-prompt-regexp' in
the buffer of PROCESS."
317
  (let ((prompt-regexp (with-current-buffer (process-buffer process)
Gerd Moellmann's avatar
Gerd Moellmann committed
318
			 comint-prompt-regexp)))
319 320 321 322 323 324 325
    (concat "\n*"
            ;; Most start with `^' but the one for `sdb' cannot be easily
            ;; stripped.  Code the prompt for `sdb' fixed here.
            (if (= (aref prompt-regexp 0) ?^)
                (substring prompt-regexp 1)
              "\\*")
            "$")))
Gerd Moellmann's avatar
Gerd Moellmann committed
326 327 328

(defun tooltip-strip-prompt (process output)
  "Return OUTPUT with any prompt of PROCESS stripped from its end."
329 330 331 332
  (save-match-data
    (if (string-match (tooltip-process-prompt-regexp process) output)
        (substring output 0 (match-beginning 0))
      output)))
Gerd Moellmann's avatar
Gerd Moellmann committed
333 334 335 336 337


;;; Tooltip help.

(defvar tooltip-help-message nil
338 339 340
  "The last help message received via `show-help-function'.
This is used by `tooltip-show-help' and
`tooltip-show-help-non-mode'.")
Gerd Moellmann's avatar
Gerd Moellmann committed
341

342 343 344 345
(defvar tooltip-previous-message nil
  "The previous content of the echo area.")

(defun tooltip-show-help-non-mode (help)
346 347
  "Function installed as `show-help-function' when Tooltip mode is off.
It is also called if Tooltip mode is on, for text-only displays."
348
  (when (and (not (window-minibuffer-p)) ;Don't overwrite minibuffer contents.
349
             (not cursor-in-echo-area))  ;Don't overwrite a prompt.
350 351
    (cond
     ((stringp help)
352 353
      (setq help (replace-regexp-in-string "\n" ", " help))
      (unless (or tooltip-previous-message
354
		  (equal-including-properties help (current-message))
355
		  (and (stringp tooltip-help-message)
356 357
		       (equal-including-properties tooltip-help-message
						   (current-message))))
358
        (setq tooltip-previous-message (current-message)))
359
      (setq tooltip-help-message help)
360 361
      (let ((message-truncate-lines
             (or (not (display-graphic-p)) (not tooltip-resize-echo-area)))
362
            (message-log-max nil))
363
        (message "%s" help)))
364 365 366 367 368 369
     ((stringp tooltip-previous-message)
      (let ((message-log-max nil))
        (message "%s" tooltip-previous-message)
        (setq tooltip-previous-message nil)))
     (t
      (message nil)))))
370

371
(defun tooltip-show-help (msg)
Gerd Moellmann's avatar
Gerd Moellmann committed
372 373
  "Function installed as `show-help-function'.
MSG is either a help string to display, or nil to cancel the display."
374 375 376 377 378 379 380
  (if (display-graphic-p)
      (let ((previous-help tooltip-help-message))
	(setq tooltip-help-message msg)
	(cond ((null msg)
	       ;; Cancel display.  This also cancels a delayed tip, if
	       ;; there is one.
	       (tooltip-hide))
381
	      ((equal-including-properties previous-help msg)
382 383 384 385 386 387 388 389 390 391
	       ;; Same help as before (but possibly the mouse has moved).
	       ;; Keep what we have.
	       )
	      (t
	       ;; A different help.  Remove a previous tooltip, and
	       ;; display a new one, with some delay.
	       (tooltip-hide)
	       (tooltip-start-delayed-tip))))
    ;; On text-only displays, try `tooltip-show-help-non-mode'.
    (tooltip-show-help-non-mode msg)))
Gerd Moellmann's avatar
Gerd Moellmann committed
392

393
(defun tooltip-help-tips (_event)
Gerd Moellmann's avatar
Gerd Moellmann committed
394
  "Hook function to display a help tooltip.
395 396
This is installed on the hook `tooltip-functions', which
is run when the timer with id `tooltip-timeout-id' fires.
Gerd Moellmann's avatar
Gerd Moellmann committed
397 398
Value is non-nil if this function handled the tip."
  (when (stringp tooltip-help-message)
399
    (tooltip-show tooltip-help-message tooltip-use-echo-area)
Gerd Moellmann's avatar
Gerd Moellmann committed
400 401
    t))

402
(provide 'tooltip)
Gerd Moellmann's avatar
Gerd Moellmann committed
403 404

;;; tooltip.el ends here