]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/eldoc.el
(eldoc-minor-mode-string): New variable.
[gnu-emacs] / lisp / emacs-lisp / eldoc.el
1 ;;; eldoc.el --- show function arglist or variable docstring in echo area
2
3 ;; Copyright (C) 1995 Noah S. Friedman
4
5 ;; Author: Noah Friedman <friedman@prep.ai.mit.edu>
6 ;; Maintainer: friedman@prep.ai.mit.edu
7 ;; Keywords: extensions
8 ;; Status: Works in Emacs 19 and XEmacs.
9 ;; Created: 1995-10-06
10
11 ;; LCD Archive Entry:
12 ;; eldoc|Noah Friedman|friedman@prep.ai.mit.edu|
13 ;; show function arglist or variable docstring in echo area|
14 ;; $Date: 1995/11/13 01:37:40 $|$Revision: 1.2 $|~/misc/eldoc.el.gz|
15
16 ;; $Id: eldoc.el,v 1.2 1995/11/13 01:37:40 friedman Exp friedman $
17
18 ;; This program is free software; you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; any later version.
22 ;;
23 ;; This program is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
27 ;;
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with this program; if not, you can either send email to this
30 ;; program's maintainer or write to: The Free Software Foundation,
31 ;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
32
33 ;;; Commentary:
34
35 ;; This program was inspired by the behavior of the Lisp Machine "mouse
36 ;; documentation window"; as you type a function's symbol name as part of a
37 ;; sexp, it will print the argument list for that function. However, this
38 ;; program's behavior is different in a couple of significant ways. For
39 ;; one, you need not actually type the function name; you need only move
40 ;; point around in a sexp that calls it. However, if point is over a
41 ;; documented variable, it will print the one-line documentation for that
42 ;; variable instead, to remind you of that variable's purpose.
43
44 ;; One useful way to enable this minor mode is to put the following in your
45 ;; .emacs:
46 ;;
47 ;; (autoload 'turn-on-eldoc-mode "eldoc" nil t)
48 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
49 ;; (add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
50
51 ;;; Code:
52
53 ;;;###autoload
54 (defvar eldoc-mode nil
55 "*If non-nil, show the defined parameters for the elisp function near point.
56
57 For the emacs lisp function at the beginning of the sexp which point is
58 within, show the defined parameters for the function in the echo area.
59 This information is extracted directly from the function or macro if it is
60 in pure lisp.
61
62 If the emacs function is a subr, the parameters are obtained from the
63 documentation string if possible.
64
65 If point is over a documented variable, print that variable's docstring
66 instead; see function `eldoc-print-var-docstring'.
67
68 This variable is buffer-local.")
69 (make-variable-buffer-local 'eldoc-mode)
70
71 (defvar eldoc-idle-delay 0.50
72 "*Number of seconds of idle time to wait before printing.
73 If user input arrives before this interval of time has elapsed after the
74 last input, no documentation will be printed.
75
76 If this variable is set to 0, no idle time is required.")
77
78 (defvar eldoc-argument-case 'upcase
79 "Case to display argument names of functions, as a symbol.
80 This has two preferred values: `upcase' or `downcase'.
81 Actually, any name of a function which takes a string as an argument and
82 returns another string is acceptable.")
83
84 (defvar eldoc-mode-message-commands nil
85 "*Obarray of command names where it is appropriate to print in the echo area.
86
87 This is not done for all commands since some print their own
88 messages in the echo area, and these functions would instantly overwrite
89 them. But self-insert-command as well as most motion commands are good
90 candidates.
91
92 It is probably best to manipulate this data structure with the commands
93 `eldoc-add-command' and `eldoc-remove-command'.")
94
95 (cond ((null eldoc-mode-message-commands)
96 ;; If you increase the number of buckets, keep it a prime number.
97 (setq eldoc-mode-message-commands (make-vector 31 0))
98 (let ((list '("self-insert-command"
99 "next-" "previous-"
100 "forward-" "backward-"
101 "beginning-of-" "end-of-"
102 "goto-"
103 "recenter"
104 "scroll-"))
105 (syms nil))
106 (while list
107 (setq syms (all-completions (car list) obarray 'fboundp))
108 (setq list (cdr list))
109 (while syms
110 (set (intern (car syms) eldoc-mode-message-commands) t)
111 (setq syms (cdr syms)))))))
112
113 ;; Bookkeeping; the car contains the last symbol read from the buffer.
114 ;; The cdr contains the string last displayed in the echo area, so it can
115 ;; be printed again if necessary without reconsing.
116 (defvar eldoc-last-data '(nil . nil))
117
118 (defvar eldoc-minor-mode-string " ElDoc"
119 "*String to display in mode line when Eldoc Mode is enabled.")
120
121 ;; Put this minor mode on the global minor-mode-alist.
122 (or (assq 'eldoc-mode (default-value 'minor-mode-alist))
123 (setq-default minor-mode-alist
124 (append (default-value 'minor-mode-alist)
125 '((eldoc-mode eldoc-minor-mode-string)))))
126
127 \f
128 ;;;###autoload
129 (defun eldoc-mode (&optional prefix)
130 "*If non-nil, then enable eldoc-mode (see variable docstring)."
131 (interactive "P")
132
133 ;; Make sure it's on the post-command-idle-hook if defined, otherwise put
134 ;; it on post-command-hook. The former first appeared in Emacs 19.30.
135 (add-hook (if (boundp 'post-command-idle-hook)
136 'post-command-idle-hook
137 'post-command-hook)
138 'eldoc-mode-print-current-symbol-info)
139
140 (setq eldoc-mode
141 (>= (prefix-numeric-value prefix) 0))
142 (and (interactive-p)
143 (if eldoc-mode
144 (message "eldoc-mode is enabled")
145 (message "eldoc-mode is disabled")))
146 eldoc-mode)
147
148 ;;;###autoload
149 (defun turn-on-eldoc-mode ()
150 "Unequivocally turn on eldoc-mode (see variable documentation)."
151 (interactive)
152 (eldoc-mode 1))
153
154 (defun eldoc-add-command (cmd)
155 "Add COMMAND to the list of commands which causes function arg display.
156 If called interactively, completion matches any bound function.
157
158 When point is in a sexp, the function args are not reprinted in the echo
159 area after every possible interactive command because some of them print
160 their own messages in the echo area; the eldoc functions would instantly
161 overwrite them unless it is more restrained."
162 (interactive "aAdd function to eldoc message commands list: ")
163 (and (fboundp cmd)
164 (set (intern (symbol-name cmd) eldoc-mode-message-commands) t)))
165
166 (defun eldoc-remove-command (cmd)
167 "Remove COMMAND from the list of commands which causes function arg display.
168 If called interactively, completion matches only those functions currently
169 in the list.
170
171 When point is in a sexp, the function args are not reprinted in the echo
172 area after every possible interactive command because some of them print
173 their own messages in the echo area; the eldoc functions would instantly
174 overwrite them unless it is more restrained."
175 (interactive (list (completing-read
176 "Remove function from eldoc message commands list: "
177 eldoc-mode-message-commands 'boundp t)))
178 (and (symbolp cmd)
179 (setq cmd (symbol-name cmd)))
180 (if (fboundp 'unintern)
181 (unintern cmd eldoc-mode-message-commands)
182 (let ((s (intern-soft cmd eldoc-mode-message-commands)))
183 (and s
184 (makunbound s)))))
185
186 (defun eldoc-mode-print-current-symbol-info ()
187 (and eldoc-mode
188 ;; Having this mode operate in the minibuffer makes it impossible to
189 ;; see what you're doing.
190 (not (eq (selected-window) (minibuffer-window)))
191 (sit-for eldoc-idle-delay)
192 (symbolp this-command)
193 (intern-soft (symbol-name this-command) eldoc-mode-message-commands)
194 (let ((current-symbol (eldoc-current-symbol))
195 (current-fnsym (eldoc-fnsym-in-current-sexp)))
196 (cond ((eq current-symbol current-fnsym)
197 (eldoc-print-fnsym-args current-fnsym))
198 (t
199 (or (eldoc-print-var-docstring current-symbol)
200 (eldoc-print-fnsym-args current-fnsym)))))))
201
202 \f
203 (defun eldoc-print-var-docstring (&optional sym)
204 "Print the brief (one-line) documentation string for the variable at point.
205 If called with no argument, print the first line of the variable
206 documentation string for the symbol at point in the echo area.
207 If called with a symbol, print the line for that symbol.
208
209 If the entire line cannot fit in the echo area, the variable name may be
210 truncated or eliminated entirely from the output to make room.
211 Any leading `*' in the docstring (which indicates the variable is a user
212 option) is not printed."
213 (interactive)
214 (let* ((s (or sym (eldoc-current-symbol)))
215 (name (symbol-name s))
216 (doc (and s (documentation-property s 'variable-documentation t))))
217 (and doc
218 (save-match-data
219 (and (string-match "\n" doc)
220 (setq doc (substring doc 0 (match-beginning 0))))
221 (and (string-match "^\\*" doc)
222 (setq doc (substring doc 1)))
223 (let* ((doclen (+ (length name) (length ": ") (length doc)))
224 ;; Subtract 1 from window width since emacs seems not to
225 ;; write any chars to the last column, at least for some
226 ;; terminal types.
227 (strip (- doclen (1- (window-width (minibuffer-window))))))
228 (cond ((> strip 0)
229 (let* ((len (length name)))
230 (cond ((>= strip len)
231 (message "%s" doc))
232 (t
233 (setq name (substring name 0 (- len strip)))
234 (message "%s: %s" name doc)))))
235 (t
236 (message "%s: %s" s doc))))
237 t))))
238
239 \f
240 ;;;###autoload
241 (defun eldoc-print-fnsym-args (&optional symbol)
242 "*Show the defined parameters for the function near point.
243 For the function at the beginning of the sexp which point is within, show
244 the defined parameters for the function in the echo area.
245 This information is extracted directly from the function or macro if it is
246 in pure lisp.
247 If the emacs function is a subr, the parameters are obtained from the
248 documentation string if possible."
249 (interactive)
250 (let ((sym (or symbol (eldoc-fnsym-in-current-sexp)))
251 (printit t)
252 (args nil))
253 (cond ((not (and (symbolp sym)
254 (fboundp sym))))
255 ((eq sym (car eldoc-last-data))
256 (setq printit nil)
257 (setq args (cdr eldoc-last-data)))
258 ((subrp (eldoc-symbol-function sym))
259 (setq args (eldoc-function-argstring-from-docstring sym))
260 (setcdr eldoc-last-data args))
261 (t
262 (setq args (eldoc-function-argstring sym))
263 (setcdr eldoc-last-data args)))
264 (and args
265 printit
266 ;; In emacs 19.29 and later, all messages are recorded in a log.
267 ;; Do not put eldoc messages in the log since they are Legion.
268 (let ((message-log-max nil))
269 (message "%s: %s" sym args)))))
270
271 (defun eldoc-fnsym-in-current-sexp ()
272 (let* ((p (point))
273 (sym (progn
274 (while (and (eldoc-forward-sexp-safe -1)
275 (> (point) (point-min))))
276 (cond ((or (= (point) (point-min))
277 (memq (or (char-after (point)) 0)
278 '(?\( ?\"))
279 ;; If we hit a quotation mark before a paren, we
280 ;; are inside a specific string, not a list of
281 ;; symbols.
282 (eq (or (char-after (1- (point))) 0) ?\"))
283 nil)
284 (t (condition-case nil
285 (read (current-buffer))
286 (error nil)))))))
287 (goto-char p)
288 (and (symbolp sym)
289 sym)))
290
291 (defun eldoc-function-argstring (fn)
292 (let* ((prelim-def (eldoc-symbol-function fn))
293 (def (if (eq (car-safe prelim-def) 'macro)
294 (cdr prelim-def)
295 prelim-def))
296 (arglist (cond ((null def) nil)
297 ((byte-code-function-p def)
298 (if (fboundp 'compiled-function-arglist)
299 (funcall 'compiled-function-arglist def)
300 (aref def 0)))
301 ((eq (car-safe def) 'lambda)
302 (nth 1 def))
303 (t t))))
304 (eldoc-function-argstring-format arglist)))
305
306 (defun eldoc-function-argstring-from-docstring (fn)
307 (let ((docstring (documentation fn 'raw))
308 (doc nil)
309 (doclist nil)
310 (end nil))
311 (save-match-data
312 (cond
313 ;; Try first searching for args starting with symbol name.
314 ;; This is to avoid matching parenthetical remarks in e.g. sit-for.
315 ((string-match (format "^(%s[^\n)]*)$" fn) docstring)
316 ;; end does not include trailing ")" sequence.
317 (setq end (- (match-end 0) 1))
318 (if (string-match " +" docstring (match-beginning 0))
319 (setq doc (substring docstring (match-end 0) end))
320 (setq doc "")))
321
322 ;; Try again not requiring this symbol name in the docstring.
323 ;; This will be the case when looking up aliases.
324 ((string-match (format "^([^\n)]+)$" fn) docstring)
325 ;; end does not include trailing ")" sequence.
326 (setq end (- (match-end 0) 1))
327 (if (string-match " +" docstring (match-beginning 0))
328 (setq doc (substring docstring (match-end 0) end))
329 (setq doc "")))
330
331 ;; Emacs subr docstring style:
332 ;; (fn arg1 arg2 ...): description...
333 ((string-match "^([^\n)]+):" docstring)
334 ;; end does not include trailing "):" sequence.
335 (setq end (- (match-end 0) 2))
336 (if (string-match " +" docstring (match-beginning 0))
337 (setq doc (substring docstring (match-end 0) end))
338 (setq doc "")))
339
340 ;; XEmacs subr docstring style:
341 ;; "arguments: (arg1 arg2 ...)
342 ((string-match "^arguments: (\\([^\n)]+\\))" docstring)
343 ;; Also, skip leading paren, but the first word is actually an
344 ;; argument, not the function name.
345 (setq doc (substring docstring
346 (match-beginning 1)
347 (match-end 1))))
348
349 ;; This finds the argstring for `condition-case'.
350 ;; I don't know if there are any others with the same pattern.
351 ((string-match (format "^Usage looks like \\((%s[^\n)]*)\\)\\.$" fn)
352 docstring)
353 ;; end does not include trailing ")" sequence.
354 (setq end (- (match-end 1) 1))
355 (if (string-match " +" docstring (match-beginning 1))
356 (setq doc (substring docstring (match-end 0) end))
357 (setq doc "")))
358
359 ;; This finds the argstring for `setq-default'.
360 ;; I don't know if there are any others with the same pattern.
361 ((string-match (format "^[ \t]+\\((%s[^\n)]*)\\)$" fn) docstring)
362 ;; end does not include trailing ")" sequence.
363 (setq end (- (match-end 1) 1))
364 (if (string-match " +" docstring (match-beginning 1))
365 (setq doc (substring docstring (match-end 0) end))
366 (setq doc ""))))
367
368 (cond ((not (stringp doc))
369 nil)
370 ((string-match "&" doc)
371 (let ((p 0)
372 (l (length doc)))
373 (while (< p l)
374 (cond ((string-match "[ \t\n]+" doc p)
375 (setq doclist
376 (cons (substring doc p (match-beginning 0))
377 doclist))
378 (setq p (match-end 0)))
379 (t
380 (setq doclist (cons (substring doc p) doclist))
381 (setq p l))))
382 (eldoc-function-argstring-format (nreverse doclist))))
383 (t
384 (concat "(" (funcall eldoc-argument-case doc) ")"))))))
385
386 (defun eldoc-function-argstring-format (arglist)
387 (cond ((not (listp arglist))
388 (setq arglist nil))
389 ((symbolp (car arglist))
390 (setq arglist
391 (mapcar (function (lambda (s)
392 (if (memq s '(&optional &rest))
393 (symbol-name s)
394 (funcall eldoc-argument-case
395 (symbol-name s)))))
396 arglist)))
397 ((stringp (car arglist))
398 (setq arglist
399 (mapcar (function (lambda (s)
400 (if (member s '("&optional" "&rest"))
401 s
402 (funcall eldoc-argument-case s))))
403 arglist))))
404 (concat "(" (mapconcat 'identity arglist " ") ")"))
405
406 \f
407 ;; forward-sexp calls scan-sexps, which returns an error if it hits the
408 ;; beginning or end of the sexp. This returns nil instead.
409 (defun eldoc-forward-sexp-safe (&optional count)
410 "Move forward across one balanced expression (sexp).
411 With argument, do it that many times. Negative arg -COUNT means
412 move backward across COUNT balanced expressions.
413 Return distance in buffer moved, or nil."
414 (or count (setq count 1))
415 (condition-case err
416 (- (- (point) (progn
417 (let ((parse-sexp-ignore-comments t))
418 (forward-sexp count))
419 (point))))
420 (error nil)))
421
422 ;; Do indirect function resolution if possible.
423 (defun eldoc-symbol-function (fsym)
424 (let ((defn (and (fboundp fsym)
425 (symbol-function fsym))))
426 (and (symbolp defn)
427 (condition-case err
428 (setq defn (indirect-function fsym))
429 (error (setq defn nil))))
430 defn))
431
432 (defun eldoc-current-symbol ()
433 (let ((c (char-after (point))))
434 (and c
435 (memq (char-syntax c) '(?w ?_))
436 (intern-soft (current-word)))))
437
438 (provide 'eldoc)
439
440 ;;; eldoc.el ends here