]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/eldoc.el
Replace eldoc-documentation-function with a hook
[gnu-emacs] / lisp / emacs-lisp / eldoc.el
1 ;;; eldoc.el --- Show function arglist or variable docstring in echo area -*- lexical-binding:t; -*-
2
3 ;; Copyright (C) 1996-2016 Free Software Foundation, Inc.
4
5 ;; Author: Noah Friedman <friedman@splode.com>
6 ;; Maintainer: friedman@splode.com
7 ;; Keywords: extensions
8 ;; Created: 1995-10-06
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
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/>.
24
25 ;;; Commentary:
26
27 ;; This program was inspired by the behavior of the "mouse documentation
28 ;; window" on many Lisp Machine systems; as you type a function's symbol
29 ;; name as part of a sexp, it will print the argument list for that
30 ;; function. Behavior is not identical; for example, you need not actually
31 ;; type the function name, you need only move point around in a sexp that
32 ;; calls it. Also, if point is over a documented variable, it will print
33 ;; the one-line documentation for that variable instead, to remind you of
34 ;; that variable's meaning.
35
36 ;; One useful way to enable this minor mode is to put the following in your
37 ;; .emacs:
38 ;;
39 ;; (add-hook 'emacs-lisp-mode-hook 'eldoc-mode)
40 ;; (add-hook 'lisp-interaction-mode-hook 'eldoc-mode)
41 ;; (add-hook 'ielm-mode-hook 'eldoc-mode)
42 ;; (add-hook 'eval-expression-minibuffer-setup-hook 'eldoc-mode)
43
44 ;; Major modes for other languages may use ElDoc by defining an
45 ;; appropriate function as the buffer-local value of
46 ;; `eldoc-documentation-functions'.
47
48 ;;; Code:
49
50 (defgroup eldoc nil
51 "Show function arglist or variable docstring in echo area."
52 :group 'lisp
53 :group 'extensions)
54
55 (defcustom eldoc-idle-delay 0.50
56 "Number of seconds of idle time to wait before printing.
57 If user input arrives before this interval of time has elapsed after the
58 last input, no documentation will be printed.
59
60 If this variable is set to 0, no idle time is required."
61 :type 'number
62 :group 'eldoc)
63
64 (defcustom eldoc-print-after-edit nil
65 "If non-nil eldoc info is only shown when editing.
66 Changing the value requires toggling `eldoc-mode'."
67 :type 'boolean
68 :group 'eldoc)
69
70 ;;;###autoload
71 (defcustom eldoc-minor-mode-string (purecopy " ElDoc")
72 "String to display in mode line when ElDoc Mode is enabled; nil for none."
73 :type '(choice string (const :tag "None" nil))
74 :group 'eldoc)
75
76 (defcustom eldoc-argument-case #'identity
77 "Case to display argument names of functions, as a symbol.
78 This has two preferred values: `upcase' or `downcase'.
79 Actually, any name of a function which takes a string as an argument and
80 returns another string is acceptable.
81
82 Note that this variable has no effect, unless
83 `eldoc-documentation-functions' handle it explicitly."
84 :type '(radio (function-item upcase)
85 (function-item downcase)
86 function)
87 :group 'eldoc)
88 (make-obsolete-variable 'eldoc-argument-case nil "25.1")
89
90 (defcustom eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit
91 "Allow long ElDoc messages to resize echo area display.
92 If value is t, never attempt to truncate messages; complete symbol name
93 and function arglist or 1-line variable documentation will be displayed
94 even if echo area must be resized to fit.
95
96 If value is any non-nil value other than t, symbol name may be truncated
97 if it will enable the function arglist or documentation string to fit on a
98 single line without resizing window. Otherwise, behavior is just like
99 former case.
100
101 If value is nil, messages are always truncated to fit in a single line of
102 display in the echo area. Function or variable symbol name may be
103 truncated to make more of the arglist or documentation string visible.
104
105 Note that this variable has no effect, unless
106 `eldoc-documentation-functions' handle it explicitly."
107 :type '(radio (const :tag "Always" t)
108 (const :tag "Never" nil)
109 (const :tag "Yes, but truncate symbol names if it will\
110 enable argument list to fit on one line" truncate-sym-name-if-fit))
111 :group 'eldoc)
112
113 (defface eldoc-highlight-function-argument
114 '((t (:inherit bold)))
115 "Face used for the argument at point in a function's argument list.
116 Note that this face has no effect unless the `eldoc-documentation-functions'
117 handle it explicitly."
118 :group 'eldoc)
119
120 ;;; No user options below here.
121
122 (defvar eldoc-message-commands-table-size 31
123 "Used by `eldoc-add-command' to initialize `eldoc-message-commands' obarray.
124 It should probably never be necessary to do so, but if you
125 choose to increase the number of buckets, you must do so before loading
126 this file since the obarray is initialized at load time.
127 Remember to keep it a prime number to improve hash performance.")
128
129 (defvar eldoc-message-commands
130 ;; Don't define as `defconst' since it would then go to (read-only) purespace.
131 (make-vector eldoc-message-commands-table-size 0)
132 "Commands after which it is appropriate to print in the echo area.
133 ElDoc does not try to print function arglists, etc., after just any command,
134 because some commands print their own messages in the echo area and these
135 functions would instantly overwrite them. But `self-insert-command' as well
136 as most motion commands are good candidates.
137 This variable contains an obarray of symbols; do not manipulate it
138 directly. Instead, use `eldoc-add-command' and `eldoc-remove-command'.")
139
140 ;; Not a constant.
141 (defvar eldoc-last-data (make-vector 3 nil)
142 ;; Don't define as `defconst' since it would then go to (read-only) purespace.
143 "Bookkeeping; elements are as follows:
144 0 - contains the last symbol read from the buffer.
145 1 - contains the string last displayed in the echo area for variables,
146 or argument string for functions.
147 2 - `function' if function args, `variable' if variable documentation.")
148 (make-obsolete-variable 'eldoc-last-data "use your own instead" "25.1")
149
150 (defvar eldoc-last-message nil)
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.")
157
158 (defvar eldoc-message-function #'eldoc-minibuffer-message
159 "The function used by `eldoc-message' to display messages.
160 It should receive the same arguments as `message'.")
161
162 (defun eldoc-edit-message-commands ()
163 (let ((cmds (make-vector 31 0))
164 (re (regexp-opt '("delete" "insert" "edit" "electric" "newline"))))
165 (mapatoms (lambda (s)
166 (and (commandp s)
167 (string-match-p re (symbol-name s))
168 (intern (symbol-name s) cmds)))
169 obarray)
170 cmds))
171
172 \f
173 ;;;###autoload
174 (define-minor-mode eldoc-mode
175 "Toggle echo area display of Lisp objects at point (ElDoc mode).
176 With a prefix argument ARG, enable ElDoc mode if ARG is positive,
177 and disable it otherwise. If called from Lisp, enable ElDoc mode
178 if ARG is omitted or nil.
179
180 ElDoc mode is a buffer-local minor mode. When enabled, the echo
181 area displays information about a function or variable in the
182 text where point is. If point is on a documented variable, it
183 displays the first line of that variable's doc string. Otherwise
184 it displays the argument list of the function called in the
185 expression point is on."
186 :group 'eldoc :lighter eldoc-minor-mode-string
187 (setq eldoc-last-message nil)
188 (cond
189 ((not (eldoc-supported-p))
190 (message "There is no ElDoc support in this buffer")
191 (setq eldoc-mode nil))
192 (eldoc-mode
193 (when eldoc-print-after-edit
194 (setq-local eldoc-message-commands (eldoc-edit-message-commands)))
195 (add-hook 'post-command-hook 'eldoc-schedule-timer nil t)
196 (add-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area nil t))
197 (t
198 (kill-local-variable 'eldoc-message-commands)
199 (remove-hook 'post-command-hook 'eldoc-schedule-timer t)
200 (remove-hook 'pre-command-hook 'eldoc-pre-command-refresh-echo-area t)
201 (when eldoc-timer
202 (cancel-timer eldoc-timer)
203 (setq eldoc-timer nil)))))
204
205 ;;;###autoload
206 (define-minor-mode global-eldoc-mode
207 "Toggle Global Eldoc mode on or off.
208 With a prefix argument ARG, enable Global Eldoc mode if ARG is
209 positive, and disable it otherwise. If called from Lisp, enable
210 the mode if ARG is omitted or nil, and toggle it if ARG is ‘toggle’.
211
212 If Global Eldoc mode is on, `eldoc-mode' will be enabled in all
213 buffers where it's applicable. These are buffers that have modes
214 that have enabled eldoc support. See `eldoc-documentation-functions'."
215 :group 'eldoc
216 :global t
217 :initialize 'custom-initialize-delay
218 :init-value t
219 (setq eldoc-last-message nil)
220 (if global-eldoc-mode
221 (progn
222 (add-hook 'post-command-hook #'eldoc-schedule-timer)
223 (add-hook 'pre-command-hook #'eldoc-pre-command-refresh-echo-area))
224 (remove-hook 'post-command-hook #'eldoc-schedule-timer)
225 (remove-hook 'pre-command-hook #'eldoc-pre-command-refresh-echo-area)))
226
227 ;;;###autoload
228 (define-obsolete-function-alias 'turn-on-eldoc-mode 'eldoc-mode "24.4")
229
230 \f
231 (defun eldoc-schedule-timer ()
232 (or (and eldoc-timer
233 (memq eldoc-timer timer-idle-list)) ;FIXME: Why?
234 (setq eldoc-timer
235 (run-with-idle-timer
236 eldoc-idle-delay nil
237 (lambda ()
238 (when (or eldoc-mode
239 (and global-eldoc-mode (eldoc-supported-p)))
240 (eldoc-print-current-symbol-info))))))
241
242 ;; If user has changed the idle delay, update the timer.
243 (cond ((not (= eldoc-idle-delay eldoc-current-idle-delay))
244 (setq eldoc-current-idle-delay eldoc-idle-delay)
245 (timer-set-idle-time eldoc-timer eldoc-idle-delay t))))
246
247 (defvar eldoc-mode-line-string nil)
248 (put 'eldoc-mode-line-string 'risky-local-variable t)
249
250 (defun eldoc-minibuffer-message (format-string &rest args)
251 "Display messages in the mode-line when in the minibuffer.
252 Otherwise work like `message'."
253 (if (minibufferp)
254 (progn
255 (add-hook 'minibuffer-exit-hook
256 (lambda () (setq eldoc-mode-line-string nil
257 ;; http://debbugs.gnu.org/16920
258 eldoc-last-message nil))
259 nil t)
260 (with-current-buffer
261 (window-buffer
262 (or (window-in-direction 'above (minibuffer-window))
263 (minibuffer-selected-window)
264 (get-largest-window)))
265 (unless (and (listp mode-line-format)
266 (assq 'eldoc-mode-line-string mode-line-format))
267 (setq mode-line-format
268 (list "" '(eldoc-mode-line-string
269 (" " eldoc-mode-line-string " "))
270 mode-line-format)))
271 (setq eldoc-mode-line-string
272 (when (stringp format-string)
273 (apply #'format-message format-string args)))
274 (force-mode-line-update)))
275 (apply 'message format-string args)))
276
277 (defun eldoc-message (&rest args)
278 (let ((omessage eldoc-last-message))
279 (setq eldoc-last-message
280 (cond ((eq (car args) eldoc-last-message) eldoc-last-message)
281 ((null (car args)) nil)
282 ;; If only one arg, no formatting to do, so put it in
283 ;; eldoc-last-message so eq test above might succeed on
284 ;; subsequent calls.
285 ((null (cdr args)) (car args))
286 (t (apply #'format-message args))))
287 ;; In emacs 19.29 and later, and XEmacs 19.13 and later, all messages
288 ;; are recorded in a log. Do not put eldoc messages in that log since
289 ;; they are Legion.
290 ;; Emacs way of preventing log messages.
291 (let ((message-log-max nil))
292 (cond (eldoc-last-message
293 (funcall eldoc-message-function "%s" eldoc-last-message))
294 (omessage (funcall eldoc-message-function nil)))))
295 eldoc-last-message)
296
297 (defun eldoc--message-command-p (command)
298 (and (symbolp command)
299 (intern-soft (symbol-name command) eldoc-message-commands)))
300
301 ;; This function goes on pre-command-hook for XEmacs or when using idle
302 ;; timers in Emacs. Motion commands clear the echo area for some reason,
303 ;; which make eldoc messages flicker or disappear just before motion
304 ;; begins. This function reprints the last eldoc message immediately
305 ;; before the next command executes, which does away with the flicker.
306 ;; This doesn't seem to be required for Emacs 19.28 and earlier.
307 (defun eldoc-pre-command-refresh-echo-area ()
308 (and eldoc-last-message
309 (not (minibufferp)) ;We don't use the echo area when in minibuffer.
310 (if (and (eldoc-display-message-no-interference-p)
311 (eldoc--message-command-p this-command))
312 (eldoc-message eldoc-last-message)
313 ;; No need to call eldoc-message since the echo area will be cleared
314 ;; for us, but do note that the last-message will be gone.
315 (setq eldoc-last-message nil))))
316
317 ;; Decide whether now is a good time to display a message.
318 (defun eldoc-display-message-p ()
319 (and (eldoc-display-message-no-interference-p)
320 ;; If this-command is non-nil while running via an idle
321 ;; timer, we're still in the middle of executing a command,
322 ;; e.g. a query-replace where it would be annoying to
323 ;; overwrite the echo area.
324 (not this-command)
325 (eldoc--message-command-p last-command)))
326
327
328 ;; Check various conditions about the current environment that might make
329 ;; it undesirable to print eldoc messages right this instant.
330 (defun eldoc-display-message-no-interference-p ()
331 (not (or executing-kbd-macro (bound-and-true-p edebug-active))))
332
333 \f
334 ;;;###autoload
335 (defvar eldoc-documentation-functions #'ignore
336 "Hook to run to return doc string.
337 A function in this hook should accept no args and return a
338 one-line string for displaying documentation of a function,
339 variable, etc. appropriate to the context around point.
340 It should return nil if there's no doc appropriate for the context.
341 Typically doc is returned if point is on a function-like name or in its
342 arg list.
343
344 The result is used as is, so the function must explicitly handle
345 the variables `eldoc-argument-case' and `eldoc-echo-area-use-multiline-p',
346 and the face `eldoc-highlight-function-argument', if they are to have any
347 effect.
348
349 Major modes should modify this variable using `add-hook', for example:
350 (add-hook \\='eldoc-documentation-functions #\\='foo-eldoc nil t)
351 so that the global documentation function (i.e. the default value of the
352 variable) is taken into account if the major mode specific function does not
353 return any documentation.")
354
355 (define-obsolete-variable-alias 'eldoc-documentation-function
356 'eldoc-documentation-functions "25.2")
357
358 (defun eldoc-print-current-symbol-info ()
359 ;; This is run from post-command-hook or some idle timer thing,
360 ;; so we need to be careful that errors aren't ignored.
361 (with-demoted-errors "eldoc error: %s"
362 (and (or (eldoc-display-message-p)
363 ;; Erase the last message if we won't display a new one.
364 (when eldoc-last-message
365 (eldoc-message nil)
366 nil))
367 (eldoc-message
368 (run-hook-with-args-until-success 'eldoc-documentation-functions)))))
369
370 (defun eldoc-supported-p ()
371 "Return t if `eldoc-documentation-functions' has non-null elements."
372 (if (listp eldoc-documentation-functions)
373 (catch :eldoc-supported
374 (mapc
375 (lambda (fun)
376 (when (not (memq fun '(nil ignore)))
377 (throw :eldoc-supported t)))
378 eldoc-documentation-functions)
379 nil)
380 (not (memq eldoc-documentation-functions '(nil ignore)))))
381
382 ;; If the entire line cannot fit in the echo area, the symbol name may be
383 ;; truncated or eliminated entirely from the output to make room for the
384 ;; description.
385 (defun eldoc-docstring-format-sym-doc (prefix doc &optional face)
386 (when (symbolp prefix)
387 (setq prefix (concat (propertize (symbol-name prefix) 'face face) ": ")))
388 (let* ((ea-multi eldoc-echo-area-use-multiline-p)
389 ;; Subtract 1 from window width since emacs will not write
390 ;; any chars to the last column, or in later versions, will
391 ;; cause a wraparound and resize of the echo area.
392 (ea-width (1- (window-width (minibuffer-window))))
393 (strip (- (+ (length prefix) (length doc)) ea-width)))
394 (cond ((or (<= strip 0)
395 (eq ea-multi t)
396 (and ea-multi (> (length doc) ea-width)))
397 (concat prefix doc))
398 ((> (length doc) ea-width)
399 (substring (format "%s" doc) 0 ea-width))
400 ((>= strip (string-match-p ":? *\\'" prefix))
401 doc)
402 (t
403 ;; Show the end of the partial symbol name, rather
404 ;; than the beginning, since the former is more likely
405 ;; to be unique given package namespace conventions.
406 (concat (substring prefix strip) doc)))))
407
408 ;; When point is in a sexp, the function args are not reprinted in the echo
409 ;; area after every possible interactive command because some of them print
410 ;; their own messages in the echo area; the eldoc functions would instantly
411 ;; overwrite them unless it is more restrained.
412 ;; These functions do display-command table management.
413
414 (defun eldoc-add-command (&rest cmds)
415 (dolist (name cmds)
416 (and (symbolp name)
417 (setq name (symbol-name name)))
418 (set (intern name eldoc-message-commands) t)))
419
420 (defun eldoc-add-command-completions (&rest names)
421 (dolist (name names)
422 (apply #'eldoc-add-command (all-completions name obarray 'commandp))))
423
424 (defun eldoc-remove-command (&rest cmds)
425 (dolist (name cmds)
426 (and (symbolp name)
427 (setq name (symbol-name name)))
428 (unintern name eldoc-message-commands)))
429
430 (defun eldoc-remove-command-completions (&rest names)
431 (dolist (name names)
432 (apply #'eldoc-remove-command
433 (all-completions name eldoc-message-commands))))
434
435 \f
436 ;; Prime the command list.
437 (eldoc-add-command-completions
438 "back-to-indentation"
439 "backward-" "beginning-of-" "delete-other-windows" "delete-window"
440 "down-list" "end-of-" "exchange-point-and-mark" "forward-" "goto-"
441 "handle-select-window" "indent-for-tab-command" "left-" "mark-page"
442 "mark-paragraph" "mouse-set-point" "move-" "move-beginning-of-"
443 "move-end-of-" "next-" "other-window" "pop-global-mark" "previous-"
444 "recenter" "right-" "scroll-" "self-insert-command" "split-window-"
445 "up-list")
446
447 (provide 'eldoc)
448
449 ;;; eldoc.el ends here