eldoc.el (43408B)
1 ;;; eldoc.el --- Show function arglist or variable docstring in echo area -*- lexical-binding:t; -*- 2 3 ;; Copyright (C) 1996-2023 Free Software Foundation, Inc. 4 5 ;; Author: Noah Friedman <friedman@splode.com> 6 ;; Keywords: extensions 7 ;; Created: 1995-10-06 8 ;; Version: 1.14.0 9 ;; Package-Requires: ((emacs "26.3")) 10 11 ;; This is a GNU ELPA :core package. Avoid functionality that is not 12 ;; compatible with the version of Emacs recorded above. 13 14 ;; This file is part of GNU Emacs. 15 16 ;; GNU Emacs is free software: you can redistribute it and/or modify 17 ;; it under the terms of the GNU General Public License as published by 18 ;; the Free Software Foundation, either version 3 of the License, or 19 ;; (at your option) any later version. 20 21 ;; GNU Emacs is distributed in the hope that it will be useful, 22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 ;; GNU General Public License for more details. 25 26 ;; You should have received a copy of the GNU General Public License 27 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 28 29 ;;; Commentary: 30 31 ;; This program was inspired by the behavior of the "mouse documentation 32 ;; window" on many Lisp Machine systems; as you type a function's symbol 33 ;; name as part of a sexp, it will print the argument list for that 34 ;; function. Behavior is not identical; for example, you need not actually 35 ;; type the function name, you need only move point around in a sexp that 36 ;; calls it. Also, if point is over a documented variable, it will print 37 ;; the one-line documentation for that variable instead, to remind you of 38 ;; that variable's meaning. 39 40 ;; This mode is now enabled by default in all major modes that provide 41 ;; support for it, such as `emacs-lisp-mode'. 42 ;; This is controlled by `global-eldoc-mode'. 43 44 ;; Major modes for other languages may use ElDoc by adding an 45 ;; appropriate function to the buffer-local value of 46 ;; `eldoc-documentation-functions'. 47 48 ;;; Code: 49 50 (eval-when-compile (require 'cl-lib)) 51 52 (defgroup eldoc nil 53 "Show function arglist or variable docstring in echo area." 54 :group 'lisp 55 :group 'extensions) 56 57 (defcustom eldoc-idle-delay 0.50 58 "Number of seconds of idle time to wait before displaying documentation. 59 If user input arrives before this interval of time has elapsed after the 60 last input event, no documentation will be displayed. 61 62 If this variable is set to 0, display the documentation without any delay." 63 :type 'number) 64 65 (defcustom eldoc-print-after-edit nil 66 "If non-nil, eldoc info is only shown after editing commands. 67 Changing the value requires toggling `eldoc-mode'." 68 :type 'boolean) 69 70 (defcustom eldoc-echo-area-display-truncation-message t 71 "If non-nil, provide verbose help when a message has been truncated. 72 When this is non-nil, and the documentation string was truncated to 73 fit in the echo-area, the documentation will be followed by an 74 explanation of how to display the full documentation text. 75 If nil, truncated messages will just have \"...\" to indicate truncation." 76 :type 'boolean 77 :version "28.1") 78 79 ;;;###autoload 80 (defcustom eldoc-minor-mode-string (purecopy " ElDoc") 81 "String to display in mode line when ElDoc Mode is enabled; nil for none." 82 :type '(choice string (const :tag "None" nil))) 83 84 (defcustom eldoc-argument-case #'identity 85 "Case to display argument names of functions, as a symbol. 86 This has two preferred values: `upcase' or `downcase'. 87 Actually, any name of a function which takes a string as an argument and 88 returns another string is acceptable. 89 90 Note that this variable has no effect, unless 91 `eldoc-documentation-strategy' handles it explicitly." 92 :type '(radio (function-item upcase) 93 (function-item downcase) 94 function)) 95 (make-obsolete-variable 'eldoc-argument-case nil "25.1") 96 97 (defcustom eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit 98 "Allow long ElDoc doc strings to resize echo area display. 99 If the value is t, never attempt to truncate messages, even if the 100 echo area must be resized to fit. In that case, Emacs will resize 101 the mini-window up to the limit set by `max-mini-window-height'. 102 103 If the value is a positive number, it is used to calculate a 104 number of screen lines of documentation that ElDoc is allowed to 105 put in the echo area. A positive integer specifies the maximum 106 number of lines directly, while a floating-point number specifies 107 the number of screen lines as a fraction of the echo area frame's 108 height. 109 110 If the value is the symbol `truncate-sym-name-if-fit', the part of 111 the doc string that represents a symbol's name may be truncated 112 if it will enable the rest of the doc string to fit on a single 113 line, without resizing the echo area. 114 115 If the value is nil, a doc string is always truncated to fit in a 116 single screen line of echo-area display. 117 118 Any resizing of the echo area additionally respects 119 `max-mini-window-height'." 120 :type '(radio (const :tag "Always" t) 121 (float :tag "Fraction of frame height" 0.25) 122 (integer :tag "Number of lines" 5) 123 (const :tag "Never" nil) 124 (const :tag "Yes, but ask major-mode to truncate 125 symbol names if it will\ enable argument list to fit on one 126 line" truncate-sym-name-if-fit))) 127 128 (defcustom eldoc-echo-area-prefer-doc-buffer nil 129 "Prefer ElDoc's documentation buffer if it is displayed in some window. 130 If this variable's value is t, ElDoc will skip showing 131 documentation in the echo area if the dedicated documentation 132 buffer (displayed by `eldoc-doc-buffer') is already displayed in 133 some window. If the value is the symbol `maybe', then the echo area 134 is only skipped if the documentation needs to be truncated there." 135 :type 'boolean) 136 137 (defface eldoc-highlight-function-argument 138 '((t (:inherit bold))) 139 "Face used for the argument at point in a function's argument list. 140 Note that this face has no effect unless the `eldoc-documentation-strategy' 141 handles it explicitly.") 142 143 ;;; No user options below here. 144 145 (defvar eldoc-message-commands-table-size 31 146 "Used by `eldoc-add-command' to initialize `eldoc-message-commands' obarray. 147 It should probably never be necessary to do so, but if you 148 choose to increase the number of buckets, you must do so before loading 149 this file since the obarray is initialized at load time. 150 Remember to keep it a prime number to improve hash performance.") 151 152 (defvar eldoc-message-commands 153 ;; Don't define as `defconst' since it would then go to (read-only) purespace. 154 (make-vector eldoc-message-commands-table-size 0) 155 "Commands after which it is appropriate to print in the echo area. 156 ElDoc does not try to print function arglists, etc., after just any command, 157 because some commands print their own messages in the echo area and these 158 functions would instantly overwrite them. But `self-insert-command' as well 159 as most motion commands are good candidates. 160 This variable contains an obarray of symbols; do not manipulate it 161 directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.") 162 163 ;; Not a constant. 164 (defvar eldoc-last-data (make-vector 3 nil) 165 ;; Don't define as `defconst' since it would then go to (read-only) purespace. 166 "Bookkeeping; elements are as follows: 167 0 - contains the last symbol read from the buffer. 168 1 - contains the string last displayed in the echo area for variables, 169 or argument string for functions. 170 2 - `function' if function args, `variable' if variable documentation.") 171 (make-obsolete-variable 'eldoc-last-data "use your own instead" "25.1") 172 173 (defvar eldoc-last-message nil) 174 175 (defvar eldoc-timer nil "ElDoc's timer object.") 176 177 (defvar eldoc-current-idle-delay eldoc-idle-delay 178 "Idle time delay currently in use by timer. 179 This is used to determine if `eldoc-idle-delay' is changed by the user.") 180 181 (defvar eldoc-message-function #'eldoc-minibuffer-message 182 "The function used by `eldoc--message' to display messages. 183 It should receive the same arguments as `message'.") 184 185 (defun eldoc-edit-message-commands () 186 "Return an obarray containing common editing commands. 187 188 When `eldoc-print-after-edit' is non-nil, ElDoc messages are only 189 printed after commands contained in this obarray." 190 (let ((cmds (make-vector 31 0)) 191 (re (regexp-opt '("delete" "insert" "edit" "electric" "newline")))) 192 (mapatoms (lambda (s) 193 (and (commandp s) 194 (string-match-p re (symbol-name s)) 195 (intern (symbol-name s) cmds))) 196 obarray) 197 cmds)) 198 199 200 ;;;###autoload 201 (define-minor-mode eldoc-mode 202 "Toggle echo area display of Lisp objects at point (ElDoc mode). 203 204 ElDoc mode is a buffer-local minor mode. When enabled, the echo 205 area displays information about a function or variable in the 206 text where point is. If point is on a documented variable, it 207 displays the first line of that variable's doc string. Otherwise 208 it displays the argument list of the function called in the 209 expression point is on." :lighter eldoc-minor-mode-string 210 (setq eldoc-last-message nil) 211 (cond 212 ((not (eldoc--supported-p)) 213 (when (called-interactively-p 'any) 214 (message "There is no ElDoc support in this buffer")) 215 (setq eldoc-mode nil)) 216 (eldoc-mode 217 (when eldoc-print-after-edit 218 (setq-local eldoc-message-commands (eldoc-edit-message-commands))) 219 (add-hook 'post-command-hook #'eldoc-schedule-timer nil t) 220 (add-hook 'pre-command-hook #'eldoc-pre-command-refresh-echo-area nil t)) 221 (t 222 (kill-local-variable 'eldoc-message-commands) 223 (remove-hook 'post-command-hook #'eldoc-schedule-timer t) 224 (remove-hook 'pre-command-hook #'eldoc-pre-command-refresh-echo-area t) 225 (when eldoc-timer 226 (cancel-timer eldoc-timer) 227 (setq eldoc-timer nil))))) 228 229 ;;;###autoload 230 (define-globalized-minor-mode global-eldoc-mode eldoc-mode turn-on-eldoc-mode 231 :initialize 'custom-initialize-delay 232 :init-value t 233 ;; For `read--expression', the usual global mode mechanism of 234 ;; `change-major-mode-hook' runs in the minibuffer before 235 ;; `eldoc-documentation-strategy' is set, so `turn-on-eldoc-mode' 236 ;; does nothing. Configure and enable eldoc from 237 ;; `eval-expression-minibuffer-setup-hook' instead. 238 (if global-eldoc-mode 239 (add-hook 'eval-expression-minibuffer-setup-hook 240 #'eldoc--eval-expression-setup) 241 (remove-hook 'eval-expression-minibuffer-setup-hook 242 #'eldoc--eval-expression-setup))) 243 244 (defun eldoc--eval-expression-setup () 245 ;; Setup `eldoc', similar to `emacs-lisp-mode'. FIXME: Call 246 ;; `emacs-lisp-mode' itself? 247 (cond ((<= emacs-major-version 27) 248 (declare-function elisp-eldoc-documentation-function "elisp-mode") 249 (with-no-warnings 250 (add-function :before-until (local 'eldoc-documentation-function) 251 #'elisp-eldoc-documentation-function))) 252 (t (add-hook 'eldoc-documentation-functions 253 #'elisp-eldoc-var-docstring nil t) 254 (add-hook 'eldoc-documentation-functions 255 #'elisp-eldoc-funcall nil t) 256 (setq-local eldoc-documentation-strategy 257 'eldoc-documentation-default))) 258 (eldoc-mode +1)) 259 260 ;;;###autoload 261 (defun turn-on-eldoc-mode () 262 "Turn on `eldoc-mode' if the buffer has ElDoc support enabled. 263 See `eldoc-documentation-strategy' for more detail." 264 (when (eldoc--supported-p) 265 (eldoc-mode 1))) 266 267 268 (defun eldoc-schedule-timer () 269 "Ensure `eldoc-timer' is running. 270 271 If the user has changed `eldoc-idle-delay', update the timer to 272 reflect the change." 273 (or (and eldoc-timer 274 (memq eldoc-timer timer-idle-list)) ;FIXME: Why? 275 (setq eldoc-timer 276 (run-with-idle-timer 277 eldoc-idle-delay nil 278 (lambda () 279 (when (or eldoc-mode 280 (and global-eldoc-mode 281 (eldoc--supported-p))) 282 ;; Don't ignore, but also don't full-on signal errors 283 (with-demoted-errors "eldoc error: %s" 284 (eldoc-print-current-symbol-info)) ))))) 285 286 ;; If user has changed the idle delay, update the timer. 287 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay)) 288 (setq eldoc-current-idle-delay eldoc-idle-delay) 289 (timer-set-idle-time eldoc-timer eldoc-idle-delay t)))) 290 291 (defvar eldoc-mode-line-string nil) 292 (put 'eldoc-mode-line-string 'risky-local-variable t) 293 294 (defun eldoc-minibuffer-message (format-string &rest args) 295 "Display message specified by FORMAT-STRING and ARGS on the mode-line as needed. 296 This function displays the message produced by formatting ARGS 297 with FORMAT-STRING on the mode line when the current buffer is a minibuffer. 298 Otherwise, it displays the message like `message' would." 299 (if (or (bound-and-true-p edebug-mode) (minibufferp)) 300 (progn 301 (add-hook 'post-command-hook #'eldoc-minibuffer--cleanup) 302 (with-current-buffer 303 (window-buffer 304 (or (window-in-direction 'above (minibuffer-window)) 305 (minibuffer-selected-window) 306 (get-largest-window))) 307 (when (and mode-line-format 308 (not (and (listp mode-line-format) 309 (assq 'eldoc-mode-line-string mode-line-format)))) 310 (setq mode-line-format 311 (list "" '(eldoc-mode-line-string 312 (" " eldoc-mode-line-string " ")) 313 mode-line-format))) 314 (setq eldoc-mode-line-string 315 (when (stringp format-string) 316 (apply #'format-message format-string args))) 317 (force-mode-line-update))) 318 (apply #'message format-string args))) 319 320 (defun eldoc-minibuffer--cleanup () 321 (unless (or (bound-and-true-p edebug-mode) (minibufferp)) 322 (setq eldoc-mode-line-string nil 323 ;; https://debbugs.gnu.org/16920 324 eldoc-last-message nil) 325 (remove-hook 'post-command-hook #'eldoc-minibuffer--cleanup))) 326 327 (make-obsolete 328 'eldoc-message "use `eldoc-documentation-functions' instead." "eldoc-1.1.0") 329 (defun eldoc-message (&optional string) (eldoc--message string)) 330 (defun eldoc--message (&optional string) 331 "Display STRING as an ElDoc message if it's non-nil. 332 333 Also store it in `eldoc-last-message' and return that value." 334 (let ((omessage eldoc-last-message)) 335 (setq eldoc-last-message string) 336 ;; Do not put eldoc messages in the log since they are Legion. 337 ;; Emacs way of preventing log messages. 338 (let ((message-log-max nil)) 339 (cond (eldoc-last-message 340 (funcall eldoc-message-function "%s" eldoc-last-message)) 341 (omessage (funcall eldoc-message-function nil))))) 342 eldoc-last-message) 343 344 (defun eldoc--message-command-p (command) 345 "Return non-nil if COMMAND is in `eldoc-message-commands'." 346 (and (symbolp command) 347 (intern-soft (symbol-name command) eldoc-message-commands))) 348 349 ;; This function goes on pre-command-hook. 350 ;; Motion commands clear the echo area for some reason, 351 ;; which make eldoc messages flicker or disappear just before motion 352 ;; begins. This function reprints the last eldoc message immediately 353 ;; before the next command executes, which does away with the flicker. 354 ;; This doesn't seem to be required for Emacs 19.28 and earlier. 355 ;; FIXME: The above comment suggests we don't really understand why 356 ;; this is needed. Maybe it's not needed any more, but if it is 357 ;; we should figure out why. 358 (defun eldoc-pre-command-refresh-echo-area () 359 "Reprint `eldoc-last-message' in the echo area." 360 (and eldoc-last-message 361 (not (minibufferp)) ;We don't use the echo area when in minibuffer. 362 (if (and (eldoc-display-message-no-interference-p) 363 (eldoc--message-command-p this-command)) 364 (eldoc--message eldoc-last-message) 365 ;; No need to call eldoc--message since the echo area will be cleared 366 ;; for us, but do note that the last-message will be gone. 367 (setq eldoc-last-message nil)))) 368 369 ;; The point of `eldoc--request-state' is not to over-request, which 370 ;; can happen if the idle timer is restarted on execution of command 371 ;; which is guaranteed not to change the conditions that warrant a new 372 ;; request for documentation. 373 (defvar eldoc--last-request-state nil 374 "Tuple containing information about last ElDoc request.") 375 (defun eldoc--request-state () 376 "Compute information to store in `eldoc--last-request-state'." 377 (list (current-buffer) (buffer-modified-tick) (point))) 378 379 (defun eldoc-display-message-p () 380 "Tell if ElDoc can use the echo area." 381 (and (eldoc-display-message-no-interference-p) 382 (not this-command) 383 (eldoc--message-command-p last-command))) 384 385 (make-obsolete 'eldoc-display-message-p 386 "Use `eldoc-documentation-functions' instead." 387 "eldoc-1.6.0") 388 389 ;; Check various conditions about the current environment that might make 390 ;; it undesirable to print eldoc messages right this instant. 391 (defun eldoc-display-message-no-interference-p () 392 "Return nil if displaying a message would cause interference." 393 (not (or executing-kbd-macro 394 ;; The following configuration shows "Matches..." in the 395 ;; echo area when point is after a closing bracket, which 396 ;; conflicts with eldoc. 397 (and (boundp 'show-paren-context-when-offscreen) 398 show-paren-context-when-offscreen 399 ;; There's no conflict with the child-frame and 400 ;; overlay versions. 401 (not (memq show-paren-context-when-offscreen 402 '(child-frame overlay))) 403 (not (pos-visible-in-window-p 404 (overlay-end show-paren--overlay))))))) 405 406 407 (defvar eldoc-documentation-functions nil 408 "Hook of functions that produce doc strings. 409 410 A doc string is typically relevant if point is on a function-like 411 name, inside its arg list, or on any object with some associated 412 information. 413 414 Each hook function is called with at least one argument CALLBACK, 415 a function, and decides whether to display a short doc string 416 about the context around point. 417 418 - If that decision can be taken quickly, the hook function may 419 call CALLBACK immediately, following the protocol described 420 below. Alternatively, it may ignore CALLBACK entirely and 421 return either the doc string, or nil if there's no doc 422 appropriate for the context. 423 424 - If the computation of said doc string (or the decision whether 425 there is one at all) is expensive or can't be performed 426 directly, the hook function should return a non-nil, non-string 427 value and arrange for CALLBACK to be called at a later time, 428 using asynchronous processes or other asynchronous mechanisms. 429 430 To call the CALLBACK function, the hook function must pass it an 431 obligatory argument DOCSTRING, a string containing the 432 documentation, followed by an optional list of arbitrary 433 keyword-value pairs of the form (:KEY VALUE :KEY2 VALUE2...). 434 The information contained in these pairs is understood by members 435 of `eldoc-display-functions', allowing the 436 documentation-producing backend to cooperate with specific 437 documentation-displaying frontends. For example, KEY can be: 438 439 * `:thing', VALUE being a short string or symbol designating what 440 DOCSTRING reports on. It can, for example be the name of the 441 function whose signature is being documented, or the name of 442 the variable whose docstring is being documented. 443 `eldoc-display-in-echo-area', a member of 444 `eldoc-display-functions', sometimes omits this information 445 depending on space constraints; 446 447 * `:face', VALUE being a symbol designating a face which both 448 `eldoc-display-in-echo-area' and `eldoc-display-in-buffer' will 449 use when displaying `:thing''s value. 450 451 * `:echo', controlling how `eldoc-display-in-echo-area' should 452 present this documentation item in the echo area, to save 453 space. If VALUE is a string, echo it instead of DOCSTRING. If 454 a number, only echo DOCSTRING up to that character position. 455 If `skip', don't echo DOCSTRING at all. 456 457 The additional KEY `:origin' is always added by ElDoc, its VALUE 458 being the member of `eldoc-documentation-functions' where 459 DOCSTRING originated. `eldoc-display-functions' may use this 460 information to organize display of multiple docstrings. 461 462 Finally, major modes should modify this hook locally, for 463 example: 464 (add-hook \\='eldoc-documentation-functions #\\='foo-mode-eldoc nil t) 465 so that the global value (i.e. the default value of the hook) is 466 taken into account if the major mode specific function does not 467 return any documentation.") 468 469 (defvar eldoc-display-functions 470 '(eldoc-display-in-echo-area eldoc-display-in-buffer) 471 "Hook of functions tasked with displaying ElDoc results. 472 Each function is passed two arguments: DOCS and INTERACTIVE. DOCS 473 is a list (DOC ...) where DOC looks like (STRING :KEY VALUE :KEY2 474 VALUE2 ...). STRING is a string containing the documentation's 475 text and the remainder of DOC is an optional list of 476 keyword-value pairs denoting additional properties of that 477 documentation. For commonly recognized properties, see 478 `eldoc-documentation-functions'. 479 480 INTERACTIVE says if the request to display doc strings came 481 directly from the user or from ElDoc's automatic mechanisms'.") 482 483 (defvar eldoc--doc-buffer nil "Buffer displaying latest ElDoc-produced docs.") 484 485 (defun eldoc-doc-buffer (&optional interactive) 486 "Get or display ElDoc documentation buffer. 487 488 The buffer holds the results of the last documentation request. 489 If INTERACTIVE, display it. Else, return said buffer." 490 (interactive (list t)) 491 (unless (buffer-live-p eldoc--doc-buffer) 492 (user-error (format 493 "ElDoc buffer doesn't exist, maybe `%s' to produce one." 494 (substitute-command-keys "\\[eldoc]")))) 495 (with-current-buffer eldoc--doc-buffer 496 (cond (interactive 497 (rename-buffer (replace-regexp-in-string "^ *" "" 498 (buffer-name))) 499 (display-buffer (current-buffer))) 500 (t (current-buffer))))) 501 502 (defvar eldoc-doc-buffer-separator 503 (concat "\n" (propertize "\n" 'face '(:inherit separator-line :extend t)) "\n") 504 "String used to separate items in Eldoc documentation buffer.") 505 506 (defun eldoc--format-doc-buffer (docs) 507 "Ensure DOCS are displayed in an *eldoc* buffer." 508 (with-current-buffer (if (buffer-live-p eldoc--doc-buffer) 509 eldoc--doc-buffer 510 (setq eldoc--doc-buffer 511 (get-buffer-create " *eldoc*"))) 512 (let ((inhibit-read-only t) 513 (things-reported-on)) 514 (special-mode) 515 (erase-buffer) 516 (setq-local nobreak-char-display nil) 517 (cl-loop for (docs . rest) on docs 518 for (this-doc . plist) = docs 519 for thing = (plist-get plist :thing) 520 when thing do 521 (cl-pushnew thing things-reported-on) 522 (setq this-doc 523 (concat 524 (propertize (format "%s" thing) 525 'face (plist-get plist :face)) 526 ": " 527 this-doc)) 528 do (insert this-doc) 529 when rest do 530 (insert eldoc-doc-buffer-separator) 531 finally (goto-char (point-min))) 532 ;; Rename the buffer, taking into account whether it was 533 ;; hidden or not 534 (rename-buffer (format "%s*eldoc%s*" 535 (if (string-match "^ " (buffer-name)) " " "") 536 (if things-reported-on 537 (format " for %s" 538 (mapconcat 539 (lambda (s) (format "%s" s)) 540 things-reported-on 541 ", ")) 542 ""))))) 543 eldoc--doc-buffer) 544 545 (defun eldoc--echo-area-render (docs) 546 "Similar to `eldoc--format-doc-buffer', but for echo area. 547 Helper for `eldoc-display-in-echo-area'." 548 (cl-loop for (item . rest) on docs 549 for (this-doc . plist) = item 550 for echo = (plist-get plist :echo) 551 for thing = (plist-get plist :thing) 552 unless (eq echo 'skip) do 553 (setq this-doc 554 (cond ((integerp echo) (substring this-doc 0 echo)) 555 ((stringp echo) echo) 556 (t this-doc))) 557 (when thing (setq this-doc 558 (concat 559 (propertize (format "%s" thing) 560 'face (plist-get plist :face)) 561 ": " 562 this-doc))) 563 (insert this-doc) 564 (when rest (insert "\n")))) 565 566 (defun eldoc--echo-area-substring (available) 567 "Given AVAILABLE lines, get buffer substring to display in echo area. 568 Helper for `eldoc-display-in-echo-area'." 569 (let ((start (prog1 (progn 570 (goto-char (point-min)) 571 (skip-chars-forward " \t\n") 572 (point)) 573 (forward-visible-line (1- available)) 574 (end-of-visible-line) 575 (skip-chars-backward " \t\n"))) 576 (truncated (save-excursion 577 (skip-chars-forward " \t\n") 578 (not (eobp))))) 579 (cond ((eldoc--echo-area-prefer-doc-buffer-p truncated) 580 nil) 581 ((and truncated 582 (> available 1) 583 eldoc-echo-area-display-truncation-message) 584 (forward-visible-line -1) 585 (end-of-visible-line) 586 (concat (buffer-substring start (point)) 587 (format 588 "\n(Documentation truncated. Use `%s' to see rest)" 589 (substitute-command-keys "\\[eldoc-doc-buffer]")))) 590 (t 591 (buffer-substring start (point)))))) 592 593 (defun eldoc--echo-area-prefer-doc-buffer-p (truncatedp) 594 "Tell if display in the echo area should be skipped. 595 Helper for `eldoc-display-in-echo-area'. If TRUNCATEDP the 596 documentation to potentially appear in the echo area is 597 known to be truncated." 598 (and (or (eq eldoc-echo-area-prefer-doc-buffer t) 599 (and truncatedp 600 (eq eldoc-echo-area-prefer-doc-buffer 601 'maybe))) 602 (get-buffer-window eldoc--doc-buffer t))) 603 604 (defun eldoc-display-in-echo-area (docs _interactive) 605 "Display DOCS in echo area. 606 Honor `eldoc-echo-area-use-multiline-p' and 607 `eldoc-echo-area-prefer-doc-buffer'." 608 (cond 609 (;; Check if we have permission to mess with echo area at all. For 610 ;; example, if this-command is non-nil while running via an idle 611 ;; timer, we're still in the middle of executing a command, e.g. a 612 ;; query-replace where it would be annoying to overwrite the echo 613 ;; area. 614 (or 615 (not (eldoc-display-message-no-interference-p)) 616 this-command 617 (not (eldoc--message-command-p last-command)))) 618 (;; If we do but nothing to report, clear the echo area. 619 (null docs) 620 (eldoc--message nil)) 621 (t 622 ;; Otherwise, establish some parameters. 623 (let* 624 ((width (1- (window-width (minibuffer-window)))) 625 (val (if (and (symbolp eldoc-echo-area-use-multiline-p) 626 eldoc-echo-area-use-multiline-p) 627 max-mini-window-height 628 eldoc-echo-area-use-multiline-p)) 629 (available (cl-typecase val 630 (float (truncate (* (frame-height) val))) 631 (integer val) 632 (t 'just-one-line))) 633 single-doc single-doc-sym) 634 (let ((echo-area-message 635 (cond 636 (;; To output to the echo area, we handle the 637 ;; `truncate-sym-name-if-fit' special case first, by 638 ;; checking for a lot of special conditions. 639 (and 640 (eq 'truncate-sym-name-if-fit eldoc-echo-area-use-multiline-p) 641 (null (cdr docs)) 642 (setq single-doc (caar docs)) 643 (setq single-doc-sym 644 (format "%s" (plist-get (cdar docs) :thing))) 645 (< (length single-doc) width) 646 (not (string-match "\n" single-doc)) 647 (> (+ (length single-doc) (length single-doc-sym) 2) width)) 648 single-doc) 649 ((and (numberp available) 650 (cl-plusp available)) 651 ;; Else, given a positive number of logical lines, grab 652 ;; as many as we can. 653 (with-temp-buffer 654 (eldoc--echo-area-render docs) 655 (eldoc--echo-area-substring available))) 656 (t ;; this is the "truncate brutally" situation 657 (let ((string 658 (with-temp-buffer 659 (eldoc--echo-area-render docs) 660 (buffer-substring (goto-char (point-min)) 661 (progn (end-of-visible-line) 662 (point)))))) 663 (if (> (length string) width) ; truncation to happen 664 (unless (eldoc--echo-area-prefer-doc-buffer-p t) 665 (truncate-string-to-width string width)) 666 (unless (eldoc--echo-area-prefer-doc-buffer-p nil) 667 string))))))) 668 (when echo-area-message 669 (eldoc--message echo-area-message))))))) 670 671 (defun eldoc-display-in-buffer (docs interactive) 672 "Display DOCS in a dedicated buffer. 673 If INTERACTIVE is t, also display the buffer." 674 (eldoc--format-doc-buffer docs) 675 (when interactive (eldoc-doc-buffer t))) 676 677 (defun eldoc-documentation-default () 678 "Show the first non-nil documentation string for item at point. 679 This is the default value for `eldoc-documentation-strategy'." 680 (run-hook-wrapped 'eldoc-documentation-functions 681 (lambda (f) 682 (funcall f (eldoc--make-callback :eager f))))) 683 684 (defun eldoc--documentation-compose-1 (eagerlyp) 685 "Helper function for composing multiple doc strings. 686 If EAGERLYP is non-nil show documentation as soon as possible, 687 else wait for all doc strings." 688 (run-hook-wrapped 'eldoc-documentation-functions 689 (lambda (f) 690 (let* ((callback (eldoc--make-callback 691 (if eagerlyp :eager :patient) 692 f)) 693 (str (funcall f callback))) 694 (if (or (null str) (stringp str)) (funcall callback str)) 695 nil))) 696 t) 697 698 (defun eldoc-documentation-compose () 699 "Show multiple documentation strings together after waiting for all of them. 700 This is meant to be used as a value for `eldoc-documentation-strategy'." 701 (eldoc--documentation-compose-1 nil)) 702 703 (defun eldoc-documentation-compose-eagerly () 704 "Show multiple documentation strings one by one as soon as possible. 705 This is meant to be used as a value for `eldoc-documentation-strategy'." 706 (eldoc--documentation-compose-1 t)) 707 708 (defun eldoc-documentation-enthusiast () 709 "Show most important documentation string produced so far. 710 This is meant to be used as a value for `eldoc-documentation-strategy'." 711 (run-hook-wrapped 'eldoc-documentation-functions 712 (lambda (f) 713 (let* ((callback (eldoc--make-callback :enthusiast f)) 714 (str (funcall f callback))) 715 (if (stringp str) (funcall callback str)) 716 nil))) 717 t) 718 719 ;; JT@2020-07-10: ElDoc is pre-loaded, so in Emacs < 28 we can't 720 ;; make the "old" `eldoc-documentation-function' point to the new 721 ;; `eldoc-documentation-strategy', so we do the reverse. This allows 722 ;; for ElDoc to be loaded in those older Emacs versions and work with 723 ;; whomever (major-modes, extensions, user) sets one or the other 724 ;; variable. 725 (defmacro eldoc--documentation-strategy-defcustom 726 (main secondary value docstring &rest more) 727 "Defcustom helper macro for sorting `eldoc-documentation-strategy'." 728 (declare (indent 2)) 729 `(if (< emacs-major-version 28) 730 (progn 731 (defcustom ,secondary ,value ,docstring ,@more) 732 (define-obsolete-variable-alias ',main ',secondary "eldoc-1.1.0")) 733 (progn 734 (defcustom ,main ,value ,docstring ,@more) 735 (defvaralias ',secondary ',main ,docstring)))) 736 737 (eldoc--documentation-strategy-defcustom eldoc-documentation-strategy 738 eldoc-documentation-function 739 #'eldoc-documentation-default 740 "How to collect and display results of `eldoc-documentation-functions'. 741 742 This variable controls how to call the functions in the special hook 743 `eldoc-documentation-functions', and how to organize their results 744 for display to the user. The functions in `eldoc-documentation-functions' 745 are the source of documentation, and act as back-end for ElDoc. 746 747 The following values are supported: 748 749 - `eldoc-documentation-default': Call functions in the special 750 hook in order, until one of them returns a non-nil string 751 value. Display only that string. 752 753 - `eldoc-documentation-compose': Call all the functions in the 754 special hook and display all of the resulting strings together, 755 after all of the functions were called, and in the order of the 756 functions in the hook. 757 758 - `eldoc-documentation-compose-eagerly': Call all the functions in 759 the special hook, and display each non-nil string as soon as it 760 is returned by a function, before calling the next function. 761 762 - `eldoc-documentation-enthusiast': Call all the functions in the 763 special hook, and display only the most important resulting 764 string at any given time. A function appearing first in 765 the special hook is considered more important than those which 766 appear after it. 767 768 This variable can also be set to a function of no arguments that 769 returns something other than a string or nil, and allows for some 770 or all of the special hook `eldoc-documentation-functions' to be 771 run. In that case, the strategy function should follow that 772 other variable's protocol closely and display the resulting doc 773 strings itself. 774 775 For backward compatibility with the \"old\" protocol, this variable 776 can also be set to a function that returns nil or a doc string, 777 depending whether or not there is documentation to display at 778 all." 779 :link '(info-link "(emacs) Lisp Doc") 780 :type '(radio (function-item eldoc-documentation-default) 781 (function-item eldoc-documentation-compose) 782 (function-item eldoc-documentation-compose-eagerly) 783 (function-item eldoc-documentation-enthusiast) 784 (function :tag "Other function")) 785 :version "28.1") 786 787 (defun eldoc--supported-p () 788 "Non-nil if an ElDoc function is set for this buffer." 789 (and (not (memq eldoc-documentation-strategy '(nil ignore))) 790 (or eldoc-documentation-functions 791 ;; The old API had major modes set `eldoc-documentation-function' 792 ;; to provide eldoc support. It's impossible now to determine 793 ;; reliably whether the `eldoc-documentation-strategy' provides 794 ;; eldoc support (as in the old API) or whether it just provides 795 ;; a way to combine the results of the 796 ;; `eldoc-documentation-functions' (as in the new API). 797 ;; But at least if it's set buffer-locally it's a good hint that 798 ;; there's some eldoc support in the current buffer. 799 (local-variable-p 'eldoc-documentation-strategy)))) 800 801 (defvar eldoc--enthusiasm-curbing-timer nil 802 "Timer used by the `eldoc-documentation-enthusiast' strategy. 803 When a doc string is encountered, it must endure a certain amount 804 of time unchallenged until it is displayed to the user. This 805 prevents blinking if a lower priority docstring comes in shortly 806 before a higher priority one.") 807 808 (defalias 'eldoc #'eldoc-print-current-symbol-info) 809 810 ;; This variable should be unbound, but that confuses 811 ;; `describe-symbol' for some reason. 812 (defvar eldoc--make-callback nil "Helper for function `eldoc--make-callback'.") 813 814 ;; JT@2020-07-08: the below docstring for the internal function 815 ;; `eldoc--invoke-strategy' could be moved to 816 ;; `eldoc-documentation-strategy' or thereabouts if/when we decide to 817 ;; extend or publish the `make-callback' protocol. 818 (defun eldoc--make-callback (method origin) 819 "Make callback suitable for `eldoc-documentation-functions'. 820 The return value is a function FN whose lambda list is (STRING 821 &rest PLIST) and can be called by those functions. Its 822 responsibility is always to register the docstring STRING along 823 with options specified in PLIST as the documentation to display 824 for each particular situation. 825 826 METHOD specifies how the callback behaves relative to other 827 competing elements in `eldoc-documentation-functions'. It can 828 have the following values: 829 830 - `:enthusiast' says to display STRING as soon as possible if 831 there's no higher priority doc string; 832 833 - `:patient' says to display STRING along with all other 834 competing strings but only when all of all 835 `eldoc-documentation-functions' have been collected; 836 837 - `:eager' says to display STRING along with all other competing 838 strings so far, as soon as possible. 839 840 ORIGIN is the member of `eldoc-documentation-functions' which 841 will be responsible for eventually calling the FN." 842 (funcall eldoc--make-callback method origin)) 843 844 (defun eldoc--invoke-strategy (interactive) 845 "Invoke `eldoc-documentation-strategy' function. 846 847 If INTERACTIVE is non-nil, the request came directly from a user 848 command, otherwise it came from ElDoc's idle 849 timer, `eldoc-timer'. 850 851 That function's job is to run the `eldoc-documentation-functions' 852 special hook, using the `run-hook' family of functions. ElDoc's 853 built-in strategy functions play along with the 854 `eldoc--make-callback' protocol, using it to produce a callback 855 argument to feed the functions that the user places in 856 `eldoc-documentation-functions'. Whenever the strategy 857 determines it has information to display to the user, this 858 function passes responsibility to the functions in 859 `eldoc-display-functions'. 860 861 Other third-party values of `eldoc-documentation-strategy' should 862 not use `eldoc--make-callback'. They must find some alternate 863 way to produce callbacks to feed to 864 `eldoc-documentation-functions' and should endeavor to display 865 the docstrings eventually produced, using 866 `eldoc-display-functions'." 867 (let* (;; How many callbacks have been created by the strategy 868 ;; function and passed to elements of 869 ;; `eldoc-documentation-functions'. 870 (howmany 0) 871 ;; How many calls to callbacks we're still waiting on. Used 872 ;; by `:patient'. 873 (want 0) 874 ;; The doc strings and corresponding options registered so 875 ;; far. 876 (docs-registered '())) 877 (cl-labels 878 ((register-doc 879 (pos string plist origin) 880 (when (and string (> (length string) 0)) 881 (push (cons pos (cons string `(:origin ,origin ,@plist))) 882 docs-registered))) 883 (display-doc 884 () 885 (run-hook-with-args 886 'eldoc-display-functions (mapcar #'cdr 887 (setq docs-registered 888 (sort docs-registered 889 (lambda (a b) (< (car a) (car b)))))) 890 interactive)) 891 (make-callback 892 (method origin) 893 (let ((pos (prog1 howmany (cl-incf howmany)))) 894 (cl-ecase method 895 (:enthusiast 896 (lambda (string &rest plist) 897 (when (and string (cl-loop for (p) in docs-registered 898 never (< p pos))) 899 (setq docs-registered '()) 900 (register-doc pos string plist origin)) 901 (when (and (timerp eldoc--enthusiasm-curbing-timer) 902 (memq eldoc--enthusiasm-curbing-timer 903 timer-list)) 904 (cancel-timer eldoc--enthusiasm-curbing-timer)) 905 (setq eldoc--enthusiasm-curbing-timer 906 (run-at-time (unless (zerop pos) 0.3) 907 nil #'display-doc)) 908 t)) 909 (:patient 910 (cl-incf want) 911 (lambda (string &rest plist) 912 (register-doc pos string plist origin) 913 (when (zerop (cl-decf want)) (display-doc)) 914 t)) 915 (:eager 916 (lambda (string &rest plist) 917 (register-doc pos string plist origin) 918 (display-doc) 919 t)))))) 920 (let* ((eldoc--make-callback #'make-callback) 921 (res (funcall eldoc-documentation-strategy))) 922 ;; Observe the old and the new protocol: 923 (cond (;; Old protocol: got string, e-d-strategy is iself the 924 ;; origin function, and we output immediately; 925 (stringp res) 926 (register-doc 0 res nil eldoc-documentation-strategy) 927 (display-doc)) 928 (;; Old protocol: got nil, clear the echo area; 929 (null res) (eldoc--message nil)) 930 (;; New protocol: trust callback will be called; 931 t)))))) 932 933 (defun eldoc-print-current-symbol-info (&optional interactive) 934 "Document thing at point." 935 (interactive '(t)) 936 (let (token) 937 (cond (interactive 938 (eldoc--invoke-strategy t)) 939 ((not (equal (setq token (eldoc--request-state)) 940 eldoc--last-request-state)) 941 (let ((non-essential t)) 942 (setq eldoc--last-request-state token) 943 (eldoc--invoke-strategy nil)))))) 944 945 946 ;; This section only affects ElDoc output to the echo area, as in 947 ;; `eldoc-display-in-echo-area'. 948 ;; 949 ;; When point is in a sexp, the function args are not reprinted in the echo 950 ;; area after every possible interactive command because some of them print 951 ;; their own messages in the echo area; the eldoc functions would instantly 952 ;; overwrite them unless it is more restrained. 953 ;; These functions do display-command table management. 954 955 (defun eldoc-add-command (&rest cmds) 956 "Add each of CMDS to the obarray `eldoc-message-commands'." 957 (dolist (name cmds) 958 (and (symbolp name) 959 (setq name (symbol-name name))) 960 (set (intern name eldoc-message-commands) t))) 961 962 (defun eldoc-add-command-completions (&rest names) 963 "Pass every prefix completion of NAMES to `eldoc-add-command'." 964 (dolist (name names) 965 (apply #'eldoc-add-command (all-completions name obarray 'commandp)))) 966 967 (defun eldoc-remove-command (&rest cmds) 968 "Remove each of CMDS from the obarray `eldoc-message-commands'." 969 (dolist (name cmds) 970 (and (symbolp name) 971 (setq name (symbol-name name))) 972 (unintern name eldoc-message-commands))) 973 974 (defun eldoc-remove-command-completions (&rest names) 975 "Pass every prefix completion of NAMES to `eldoc-remove-command'." 976 (dolist (name names) 977 (apply #'eldoc-remove-command 978 (all-completions name eldoc-message-commands)))) 979 980 ;; Prime the command list. 981 (eldoc-add-command-completions 982 "back-to-indentation" 983 "backward-" "beginning-of-" "delete-other-windows" "delete-window" 984 "down-list" "end-of-" "exchange-point-and-mark" "forward-" "goto-" 985 "handle-select-window" "indent-for-tab-command" "left-" "mark-page" 986 "mark-paragraph" "mouse-set-point" "move-" "move-beginning-of-" 987 "move-end-of-" "newline" "next-" "other-window" "pop-global-mark" 988 "previous-" "recenter" "right-" "scroll-" "self-insert-command" 989 "split-window-" "up-list") 990 991 (provide 'eldoc) 992 993 ;;; eldoc.el ends here