eldoc.el 15.5 KB
Newer Older
1
;;; eldoc.el --- Show function arglist or variable docstring in echo area  -*- lexical-binding:t; -*-
Noah Friedman's avatar
Noah Friedman committed
2

3
;; Copyright (C) 1996-2014 Free Software Foundation, Inc.
Noah Friedman's avatar
Noah Friedman committed
4

5 6
;; Author: Noah Friedman <friedman@splode.com>
;; Maintainer: friedman@splode.com
Noah Friedman's avatar
Noah Friedman committed
7 8 9
;; Keywords: extensions
;; Created: 1995-10-06

10
;; This file is part of GNU Emacs.
Noah Friedman's avatar
Noah Friedman committed
11

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

;; GNU Emacs is distributed in the hope that it will be useful,
Noah Friedman's avatar
Noah Friedman committed
18 19 20
;; 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.
21

Noah Friedman's avatar
Noah Friedman committed
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/>.
Noah Friedman's avatar
Noah Friedman committed
24 25 26

;;; Commentary:

27 28 29 30 31 32 33 34
;; This program was inspired by the behavior of the "mouse documentation
;; window" on many Lisp Machine systems; as you type a function's symbol
;; name as part of a sexp, it will print the argument list for that
;; function.  Behavior is not identical; for example, you need not actually
;; type the function name, you need only move point around in a sexp that
;; calls it.  Also, if point is over a documented variable, it will print
;; the one-line documentation for that variable instead, to remind you of
;; that variable's meaning.
Noah Friedman's avatar
Noah Friedman committed
35 36 37 38

;; One useful way to enable this minor mode is to put the following in your
;; .emacs:
;;
39 40 41
;;      (add-hook 'emacs-lisp-mode-hook 'eldoc-mode)
;;      (add-hook 'lisp-interaction-mode-hook 'eldoc-mode)
;;      (add-hook 'ielm-mode-hook 'eldoc-mode)
Glenn Morris's avatar
Glenn Morris committed
42
;;      (add-hook 'eval-expression-minibuffer-setup-hook 'eldoc-mode)
Noah Friedman's avatar
Noah Friedman committed
43

44
;; Major modes for other languages may use ElDoc by defining an
45
;; appropriate function as the buffer-local value of
Richard M. Stallman's avatar
Richard M. Stallman committed
46
;; `eldoc-documentation-function'.
47

Noah Friedman's avatar
Noah Friedman committed
48 49
;;; Code:

50
(require 'help-fns)		       ;For fundoc-usage handling functions.
51
(require 'cl-lib)
52

53 54
(defgroup eldoc nil
  "Show function arglist or variable docstring in echo area."
55
  :group 'lisp
56 57 58
  :group 'extensions)

(defcustom eldoc-idle-delay 0.50
59
  "Number of seconds of idle time to wait before printing.
Noah Friedman's avatar
Noah Friedman committed
60 61 62
If user input arrives before this interval of time has elapsed after the
last input, no documentation will be printed.

63 64 65
If this variable is set to 0, no idle time is required."
  :type 'number
  :group 'eldoc)
Noah Friedman's avatar
Noah Friedman committed
66

67 68 69 70 71 72
(defcustom eldoc-print-after-edit nil
  "If non-nil eldoc info is only shown when editing.
Changing the value requires toggling `eldoc-mode'."
  :type 'boolean
  :group 'eldoc)

73
;;;###autoload
74
(defcustom eldoc-minor-mode-string (purecopy " ElDoc")
75
  "String to display in mode line when ElDoc Mode is enabled; nil for none."
76
  :type '(choice string (const :tag "None" nil))
77
  :group 'eldoc)
78

79
(defcustom eldoc-argument-case #'identity
Noah Friedman's avatar
Noah Friedman committed
80 81 82
  "Case to display argument names of functions, as a symbol.
This has two preferred values: `upcase' or `downcase'.
Actually, any name of a function which takes a string as an argument and
83 84 85 86
returns another string is acceptable.

Note that if `eldoc-documentation-function' is non-nil, this variable
has no effect, unless the function handles it explicitly."
87 88 89
  :type '(radio (function-item upcase)
		(function-item downcase)
                function)
90
  :group 'eldoc)
Stefan Monnier's avatar
Stefan Monnier committed
91
(make-obsolete-variable 'eldoc-argument-case nil "25.1")
Noah Friedman's avatar
Noah Friedman committed
92

93
(defcustom eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit
94
  "Allow long ElDoc messages to resize echo area display.
95
If value is t, never attempt to truncate messages; complete symbol name
96 97 98
and function arglist or 1-line variable documentation will be displayed
even if echo area must be resized to fit.

99
If value is any non-nil value other than t, symbol name may be truncated
100 101 102 103 104 105
if it will enable the function arglist or documentation string to fit on a
single line without resizing window.  Otherwise, behavior is just like
former case.

If value is nil, messages are always truncated to fit in a single line of
display in the echo area.  Function or variable symbol name may be
106 107 108 109
truncated to make more of the arglist or documentation string visible.

Note that if `eldoc-documentation-function' is non-nil, this variable
has no effect, unless the function handles it explicitly."
110 111 112 113 114 115
  :type '(radio (const :tag "Always" t)
                (const :tag "Never" nil)
                (const :tag "Yes, but truncate symbol names if it will\
 enable argument list to fit on one line" truncate-sym-name-if-fit))
  :group 'eldoc)

116 117
(defface eldoc-highlight-function-argument
  '((t (:inherit bold)))
118
  "Face used for the argument at point in a function's argument list.
119 120
Note that this face has no effect unless the `eldoc-documentation-function'
handles it explicitly."
121 122
  :group 'eldoc)

123 124
;;; No user options below here.

125
(defvar eldoc-message-commands-table-size 31
126
  "Used by `eldoc-add-command' to initialize `eldoc-message-commands' obarray.
127 128 129 130 131 132 133 134
It should probably never be necessary to do so, but if you
choose to increase the number of buckets, you must do so before loading
this file since the obarray is initialized at load time.
Remember to keep it a prime number to improve hash performance.")

(defconst eldoc-message-commands
  (make-vector eldoc-message-commands-table-size 0)
  "Commands after which it is appropriate to print in the echo area.
135
ElDoc does not try to print function arglists, etc., after just any command,
136
because some commands print their own messages in the echo area and these
137
functions would instantly overwrite them.  But `self-insert-command' as well
138 139 140 141
as most motion commands are good candidates.
This variable contains an obarray of symbols; do not manipulate it
directly.  Instead, use `eldoc-add-command' and `eldoc-remove-command'.")

142
;; Not a constant.
143 144 145
(defconst eldoc-last-data (make-vector 3 nil)
  "Bookkeeping; elements are as follows:
  0 - contains the last symbol read from the buffer.
146 147
  1 - contains the string last displayed in the echo area for variables,
      or argument string for functions.
148
  2 - 'function if function args, 'variable if variable documentation.")
149

150
(defvar eldoc-last-message nil)
Noah Friedman's avatar
Noah Friedman committed
151

152
(defvar eldoc-timer nil "ElDoc's timer object.")
153

154
(defvar eldoc-current-idle-delay eldoc-idle-delay
155
  "Idle time delay currently in use by timer.
156
This is used to determine if `eldoc-idle-delay' is changed by the user.")
Noah Friedman's avatar
Noah Friedman committed
157

158
(defvar eldoc-message-function #'eldoc-minibuffer-message
159 160 161
  "The function used by `eldoc-message' to display messages.
It should receive the same arguments as `message'.")

162 163 164 165 166 167 168 169 170 171
(defun eldoc-edit-message-commands ()
  (let ((cmds (make-vector 31 0))
	(re (regexp-opt '("delete" "insert" "edit" "electric" "newline"))))
    (mapatoms (lambda (s)
		(and (commandp s)
		     (string-match-p re (symbol-name s))
		     (intern (symbol-name s) cmds)))
	      obarray)
    cmds))

Noah Friedman's avatar
Noah Friedman committed
172 173

;;;###autoload
174
(define-minor-mode eldoc-mode
Chong Yidong's avatar
Chong Yidong committed
175 176 177 178 179 180 181 182 183 184 185
  "Toggle echo area display of Lisp objects at point (ElDoc mode).
With a prefix argument ARG, enable ElDoc mode if ARG is positive,
and disable it otherwise.  If called from Lisp, enable ElDoc mode
if ARG is omitted or nil.

ElDoc mode is a buffer-local minor mode.  When enabled, the echo
area displays information about a function or variable in the
text where point is.  If point is on a documented variable, it
displays the first line of that variable's doc string.  Otherwise
it displays the argument list of the function called in the
expression point is on."
Lute Kamstra's avatar
Lute Kamstra committed
186
  :group 'eldoc :lighter eldoc-minor-mode-string
187
  (setq eldoc-last-message nil)
188
  (cond
189
   ((not eldoc-documentation-function)
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    (message "There is no ElDoc support in this buffer")
    (setq eldoc-mode nil))
   (eldoc-mode
    (when eldoc-print-after-edit
      (setq-local eldoc-message-commands (eldoc-edit-message-commands)))
    (add-hook 'post-command-hook 'eldoc-schedule-timer nil t)
    (add-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area nil t))
   (t
    (kill-local-variable 'eldoc-message-commands)
    (remove-hook 'post-command-hook 'eldoc-schedule-timer t)
    (remove-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area t))))

;;;###autoload
(define-minor-mode global-eldoc-mode
  "Enable `eldoc-mode' in all buffers where it's applicable."
  :group 'eldoc :global t
  (setq eldoc-last-message nil)
  (if global-eldoc-mode
208
      (progn
209 210
	(when eldoc-print-after-edit
	  (setq-local eldoc-message-commands (eldoc-edit-message-commands)))
211 212
	(add-hook 'post-command-hook #'eldoc-schedule-timer)
	(add-hook 'pre-command-hook #'eldoc-pre-command-refresh-echo-area))
213
    (kill-local-variable 'eldoc-message-commands)
214 215
    (remove-hook 'post-command-hook #'eldoc-schedule-timer)
    (remove-hook 'pre-command-hook #'eldoc-pre-command-refresh-echo-area)))
216

Noah Friedman's avatar
Noah Friedman committed
217
;;;###autoload
218
(define-obsolete-function-alias 'turn-on-eldoc-mode 'eldoc-mode "24.4")
Noah Friedman's avatar
Noah Friedman committed
219

220

221 222
(defun eldoc-schedule-timer ()
  (or (and eldoc-timer
223
           (memq eldoc-timer timer-idle-list)) ;FIXME: Why?
224
      (setq eldoc-timer
225 226
            (run-with-idle-timer
	     eldoc-idle-delay t
227 228 229 230
	     (lambda ()
               (when (or eldoc-mode
                         (and global-eldoc-mode eldoc-documentation-function))
                 (eldoc-print-current-symbol-info))))))
231 232 233 234 235 236

  ;; If user has changed the idle delay, update the timer.
  (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay))
         (setq eldoc-current-idle-delay eldoc-idle-delay)
         (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))

237 238 239 240 241 242 243 244
(defvar eldoc-mode-line-string nil)
(put 'eldoc-mode-line-string 'risky-local-variable t)

(defun eldoc-minibuffer-message (format-string &rest args)
  "Display messages in the mode-line when in the minibuffer.
Otherwise work like `message'."
  (if (minibufferp)
      (progn
245
	(add-hook 'minibuffer-exit-hook
246 247 248
		  (lambda () (setq eldoc-mode-line-string nil
			      ;; http://debbugs.gnu.org/16920
			      eldoc-last-message nil))
249
		  nil t)
250 251 252 253 254 255 256 257 258 259
	(with-current-buffer
	    (window-buffer
	     (or (window-in-direction 'above (minibuffer-window))
		 (minibuffer-selected-window)
		 (get-largest-window)))
	  (unless (and (listp mode-line-format)
		       (assq 'eldoc-mode-line-string mode-line-format))
	    (setq mode-line-format
		  (list "" '(eldoc-mode-line-string
			     (" " eldoc-mode-line-string " "))
260 261 262 263 264
			mode-line-format)))
          (setq eldoc-mode-line-string
                (when (stringp format-string)
                  (apply 'format format-string args)))
          (force-mode-line-update)))
265 266
    (apply 'message format-string args)))

267 268
(defun eldoc-message (&rest args)
  (let ((omessage eldoc-last-message))
269
    (setq eldoc-last-message
270 271 272 273 274 275 276
	  (cond ((eq (car args) eldoc-last-message) eldoc-last-message)
		((null (car args)) nil)
		;; If only one arg, no formatting to do, so put it in
		;; eldoc-last-message so eq test above might succeed on
		;; subsequent calls.
		((null (cdr args)) (car args))
		(t (apply 'format args))))
277 278 279
    ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages
    ;; are recorded in a log.  Do not put eldoc messages in that log since
    ;; they are Legion.
280 281
    ;; Emacs way of preventing log messages.
    (let ((message-log-max nil))
282 283 284
      (cond (eldoc-last-message
	     (funcall eldoc-message-function "%s" eldoc-last-message))
	    (omessage (funcall eldoc-message-function nil)))))
285 286
  eldoc-last-message)

287 288 289 290
(defun eldoc--message-command-p (command)
  (and (symbolp command)
       (intern-soft (symbol-name command) eldoc-message-commands)))

291 292 293 294 295 296 297 298
;; This function goes on pre-command-hook for XEmacs or when using idle
;; timers in Emacs.  Motion commands clear the echo area for some reason,
;; which make eldoc messages flicker or disappear just before motion
;; begins.  This function reprints the last eldoc message immediately
;; before the next command executes, which does away with the flicker.
;; This doesn't seem to be required for Emacs 19.28 and earlier.
(defun eldoc-pre-command-refresh-echo-area ()
  (and eldoc-last-message
299
       (not (minibufferp))      ;We don't use the echo area when in minibuffer.
300
       (if (and (eldoc-display-message-no-interference-p)
301
		(eldoc--message-command-p this-command))
302
	   (eldoc-message eldoc-last-message)
303 304
         ;; No need to call eldoc-message since the echo area will be cleared
         ;; for us, but do note that the last-message will be gone.
305
         (setq eldoc-last-message nil))))
306 307 308

;; Decide whether now is a good time to display a message.
(defun eldoc-display-message-p ()
309
  (and (eldoc-display-message-no-interference-p)
310 311 312 313
       ;; If this-command is non-nil while running via an idle
       ;; timer, we're still in the middle of executing a command,
       ;; e.g. a query-replace where it would be annoying to
       ;; overwrite the echo area.
314 315 316
       (not this-command)
       (eldoc--message-command-p last-command)))

Noah Friedman's avatar
Noah Friedman committed
317

318 319
;; Check various conditions about the current environment that might make
;; it undesirable to print eldoc messages right this instant.
320
(defun eldoc-display-message-no-interference-p ()
321
  (not (or executing-kbd-macro (bound-and-true-p edebug-active))))
322

323

324
;;;###autoload
325
(defvar eldoc-documentation-function nil
326
  "Function to call to return doc string.
327 328 329 330 331 332
The function of no args should return a one-line string for displaying
doc about a function etc. appropriate to the context around point.
It should return nil if there's no doc appropriate for the context.
Typically doc is returned if point is on a function-like name or in its
arg list.

333 334 335 336 337
The result is used as is, so the function must explicitly handle
the variables `eldoc-argument-case' and `eldoc-echo-area-use-multiline-p',
and the face `eldoc-highlight-function-argument', if they are to have any
effect.

338
This variable is expected to be set buffer-locally by modes that support ElDoc.")
339

340
(defun eldoc-print-current-symbol-info ()
341 342 343
  ;; This is run from post-command-hook or some idle timer thing,
  ;; so we need to be careful that errors aren't ignored.
  (with-demoted-errors "eldoc error: %s"
344 345 346 347 348
    (and (or (eldoc-display-message-p)
             ;; Erase the last message if we won't display a new one.
             (when eldoc-last-message
               (eldoc-message nil)
               nil))
349 350
	 (eldoc-message (funcall eldoc-documentation-function)))))

Noah Friedman's avatar
Noah Friedman committed
351

352 353 354 355 356 357 358
;; When point is in a sexp, the function args are not reprinted in the echo
;; area after every possible interactive command because some of them print
;; their own messages in the echo area; the eldoc functions would instantly
;; overwrite them unless it is more restrained.
;; These functions do display-command table management.

(defun eldoc-add-command (&rest cmds)
359 360 361 362
  (dolist (name cmds)
    (and (symbolp name)
         (setq name (symbol-name name)))
    (set (intern name eldoc-message-commands) t)))
363 364

(defun eldoc-add-command-completions (&rest names)
365
  (dolist (name names)
366
    (apply #'eldoc-add-command (all-completions name obarray 'commandp))))
367 368

(defun eldoc-remove-command (&rest cmds)
369 370 371 372
  (dolist (name cmds)
    (and (symbolp name)
         (setq name (symbol-name name)))
    (unintern name eldoc-message-commands)))
373 374

(defun eldoc-remove-command-completions (&rest names)
375
  (dolist (name names)
376
    (apply #'eldoc-remove-command
377
           (all-completions name eldoc-message-commands))))
378

379

380 381
;; Prime the command list.
(eldoc-add-command-completions
382 383 384 385 386 387 388
 "backward-" "beginning-of-" "delete-other-windows" "delete-window"
 "down-list" "end-of-" "exchange-point-and-mark" "forward-" "goto-"
 "handle-select-window" "indent-for-tab-command" "left-" "mark-page"
 "mark-paragraph" "mouse-set-point" "move-" "move-beginning-of-"
 "move-end-of-" "next-" "other-window" "pop-global-mark" "previous-"
 "recenter" "right-" "scroll-" "self-insert-command" "split-window-"
 "up-list")
Noah Friedman's avatar
Noah Friedman committed
389 390 391 392

(provide 'eldoc)

;;; eldoc.el ends here