1 ;;; elint.el --- Lint Emacs Lisp
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009 Free Software Foundation, Inc.
6 ;; Author: Peter Liljenberg <petli@lysator.liu.se>
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 ;; This is a linter for Emacs Lisp. Currently, it mainly catches
28 ;; misspellings and undefined variables, although it can also catch
29 ;; function calls with the wrong number of arguments.
31 ;; To use, call elint-current-buffer or elint-defun to lint a buffer
32 ;; or defun. The first call runs `elint-initialize' to set up some
33 ;; argument data, which may take a while.
35 ;; The linter will try to "include" any require'd libraries to find
36 ;; the variables defined in those. There is a fair amount of voodoo
37 ;; involved in this, but it seems to work in normal situations.
41 ;; * List of variables and functions defined in dumped lisp files.
42 ;; * Adding type checking. (Stop that sniggering!)
43 ;; * Handle eval-when-compile (eg for requires, being sensitive to the
44 ;; difference between funcs and macros).
45 ;; * Requires within function bodies.
49 (defvar elint-log-buffer
"*Elint*"
50 "*The buffer to insert lint messages in.")
57 ;; FIXME does this serve any useful purpose now elint-builtin-variables exists?
58 (defconst elint-standard-variables
'(local-write-file-hooks vc-mode
)
59 "Standard buffer local variables, excluding `elint-builtin-variables'.")
61 (defvar elint-builtin-variables nil
62 "List of built-in variables. Set by `elint-initialize'.")
64 (defvar elint-autoloaded-variables nil
65 "List of `loaddefs.el' variables. Set by `elint-initialize'.")
67 ;; FIXME dumped variables and functions.
69 (defconst elint-unknown-builtin-args nil
70 "Those built-ins for which we can't find arguments, if any.")
72 (defconst elint-extra-errors
'(file-locked file-supersession ftp-error
)
73 "Errors without error-message or error-confitions properties.")
79 (defsubst elint-make-top-form
(form pos
)
81 FORM is the form, and POS is the point where it starts in the buffer."
84 (defsubst elint-top-form-form
(top-form)
85 "Extract the form from a TOP-FORM."
88 (defsubst elint-top-form-pos
(top-form)
89 "Extract the position from a TOP-FORM."
96 (defsubst elint-make-env
()
97 "Create an empty environment."
98 (list (list nil
) nil nil
))
100 (defsubst elint-env-add-env
(env newenv
)
101 "Augment ENV with NEWENV.
102 None of them is modified, and the new env is returned."
103 (list (append (car env
) (car newenv
))
104 (append (cadr env
) (cadr newenv
))
105 (append (car (cdr (cdr env
))) (car (cdr (cdr newenv
))))))
107 (defsubst elint-env-add-var
(env var
)
108 "Augment ENV with the variable VAR.
109 The new environment is returned, the old is unmodified."
110 (cons (cons (list var
) (car env
)) (cdr env
)))
112 (defsubst elint-env-add-global-var
(env var
)
113 "Augment ENV with the variable VAR.
114 ENV is modified so VAR is seen everywhere.
116 (nconc (car env
) (list (list var
)))
119 (defsubst elint-env-find-var
(env var
)
120 "Non-nil if ENV contains the variable VAR.
121 Actually, a list with VAR as a single element is returned."
122 (assq var
(car env
)))
124 (defsubst elint-env-add-func
(env func args
)
125 "Augment ENV with the function FUNC, which has the arguments ARGS.
126 The new environment is returned, the old is unmodified."
128 (cons (list func args
) (cadr env
))
129 (car (cdr (cdr env
)))))
131 (defsubst elint-env-find-func
(env func
)
132 "Non-nil if ENV contains the function FUNC.
133 Actually, a list of (FUNC ARGS) is returned."
134 (assq func
(cadr env
)))
136 (defsubst elint-env-add-macro
(env macro def
)
137 "Augment ENV with the macro named MACRO.
138 DEF is the macro definition (a lambda expression or similar).
139 The new environment is returned, the old is unmodified."
142 (cons (cons macro def
) (car (cdr (cdr env
))))))
144 (defsubst elint-env-macro-env
(env)
145 "Return the macro environment of ENV.
146 This environment can be passed to `macroexpand'."
147 (car (cdr (cdr env
))))
149 (defsubst elint-env-macrop
(env macro
)
150 "Non-nil if ENV contains MACRO."
151 (assq macro
(elint-env-macro-env env
)))
158 (defun elint-file (file)
159 "Lint the file FILE."
160 (interactive "fElint file: ")
161 (setq file
(expand-file-name file
))
162 (or elint-builtin-variables
164 (let ((dir (file-name-directory file
)))
165 (let ((default-directory dir
))
167 (elint-set-mode-line t
)
168 (with-current-buffer elint-log-buffer
169 (unless (string-equal default-directory dir
)
170 (elint-log-message (format "\f\nLeaving directory `%s'"
171 default-directory
) t
)
172 (elint-log-message (format "Entering directory `%s'" dir
) t
)
173 (setq default-directory dir
))))
174 (let ((str (format "Linting file %s" file
)))
175 (message "%s..." str
)
177 (elint-log-message (format "\f\n%s at %s" str
(current-time-string)) t
))
178 ;; elint-current-buffer clears log.
180 (insert-file-contents file
)
181 (let ((buffer-file-name file
)
182 (max-lisp-eval-depth (max 1000 max-lisp-eval-depth
)))
183 (with-syntax-table emacs-lisp-mode-syntax-table
184 (mapc 'elint-top-form
(elint-update-env)))))
185 (elint-set-mode-line)
186 (message "%s...done" str
)))
188 ;; cf byte-recompile-directory.
190 (defun elint-directory (directory)
191 "Lint all the .el files in DIRECTORY."
192 (interactive "DElint directory: ")
193 (let ((elint-running t
))
194 (dolist (file (directory-files directory t
))
195 ;; Bytecomp has emacs-lisp-file-regexp.
196 (when (and (string-match "\\.el\\'" file
)
197 (file-readable-p file
)
198 (not (auto-save-file-name-p file
)))
200 (elint-set-mode-line))
203 (defun elint-current-buffer ()
204 "Lint the current buffer.
205 If necessary, this first calls `elint-initalize'."
207 (or elint-builtin-variables
209 (elint-clear-log (format "Linting %s" (or (buffer-file-name)
212 (elint-set-mode-line t
)
213 (mapc 'elint-top-form
(elint-update-env))
214 ;; Tell the user we're finished. This is terribly klugy: we set
215 ;; elint-top-form-logged so elint-log-message doesn't print the
216 ;; ** top form ** header...
217 (elint-set-mode-line)
218 (elint-log-message "\nLinting finished.\n" t
))
222 (defun elint-defun ()
223 "Lint the function at point.
224 If necessary, this first calls `elint-initalize'."
226 (or elint-builtin-variables
229 (or (beginning-of-defun) (error "Lint what?"))
231 (def (read (current-buffer))))
234 (elint-top-form (elint-make-top-form def pos
)))))
237 ;;; Top form functions
240 (defvar elint-buffer-env nil
241 "The environment of a elisp buffer.
242 Will be local in linted buffers.")
244 (defvar elint-buffer-forms nil
245 "The top forms in a buffer.
246 Will be local in linted buffers.")
248 (defvar elint-last-env-time nil
249 "The last time the buffers env was updated.
250 Is measured in buffer-modified-ticks and is local in linted buffers.")
252 (defun elint-update-env ()
253 "Update the elint environment in the current buffer.
254 Don't do anything if the buffer hasn't been changed since this
255 function was called the last time.
257 (if (and (local-variable-p 'elint-buffer-env
(current-buffer))
258 (local-variable-p 'elint-buffer-forms
(current-buffer))
259 (local-variable-p 'elint-last-env-time
(current-buffer))
260 (= (buffer-modified-tick) elint-last-env-time
))
264 (set (make-local-variable 'elint-buffer-forms
) (elint-get-top-forms))
265 (set (make-local-variable 'elint-buffer-env
)
266 (elint-init-env elint-buffer-forms
))
267 (set (make-local-variable 'elint-last-env-time
) (buffer-modified-tick))
270 (defun elint-get-top-forms ()
271 "Collect all the top forms in the current buffer."
274 (goto-char (point-min))
275 (while (elint-find-next-top-form)
279 (elint-make-top-form (read (current-buffer)) pos
)
283 (error "Missing ')' in top form: %s"
284 (buffer-substring pos
(line-end-position)))))))
287 (defun elint-find-next-top-form ()
288 "Find the next top form from point.
289 Return nil if there are no more forms, t otherwise."
290 (parse-partial-sexp (point) (point-max) nil t
)
293 (defun elint-init-env (forms)
294 "Initialize the environment from FORMS."
295 (let ((env (elint-make-env))
298 (setq form
(elint-top-form-form (car forms
))
301 ;; Eg nnmaildir seems to use [] as a form of comment syntax.
303 (elint-error "Skipping non-list form `%s'" form
))
304 ;; Add defined variable
305 ((memq (car form
) '(defvar defconst defcustom
))
306 (setq env
(elint-env-add-var env
(cadr form
))))
308 ((memq (car form
) '(defun defsubst))
309 (setq env
(elint-env-add-func env
(cadr form
) (nth 2 form
))))
310 ((eq (car form
) 'define-derived-mode
)
311 (setq env
(elint-env-add-func env
(cadr form
) ())
312 env
(elint-env-add-var env
(cadr form
))))
313 ;; FIXME it would be nice to check the autoloads are correct.
314 ((eq (car form
) 'autoload
)
315 (setq env
(elint-env-add-func env
(cadr (cadr form
)) 'unknown
)))
316 ((eq (car form
) 'declare-function
)
317 (setq env
(elint-env-add-func env
(cadr form
)
318 (if (or (< (length form
) 4)
322 ((and (eq (car form
) 'defalias
) (listp (nth 2 form
)))
323 ;; If the alias points to something already in the environment,
324 ;; add the alias to the environment with the same arguments.
325 (let ((def (elint-env-find-func env
(cadr (nth 2 form
)))))
326 ;; FIXME warn if the alias target is unknown.
327 (setq env
(elint-env-add-func env
(cadr (cadr form
))
328 (if def
(cadr def
) 'unknown
)))))
329 ;; Add macro, both as a macro and as a function
330 ((eq (car form
) 'defmacro
)
331 (setq env
(elint-env-add-macro env
(cadr form
)
332 (cons 'lambda
(cddr form
)))
333 env
(elint-env-add-func env
(cadr form
) (nth 2 form
))))
334 ;; Import variable definitions
335 ((eq (car form
) 'require
)
336 (let ((name (eval (cadr form
)))
337 (file (eval (nth 2 form
))))
338 (setq env
(elint-add-required-env env name file
))))))
341 (defun elint-add-required-env (env name file
)
342 "Augment ENV with the variables defined by feature NAME in FILE."
344 (let* ((libname (if (stringp file
)
348 ;; First try to find .el files, then the raw name
349 (lib1 (locate-library (concat libname
".el") t
))
350 (lib (or lib1
(locate-library libname t
))))
351 ;; Clear the messages :-/
355 ;; FIXME this doesn't use a temp buffer, because it
356 ;; stores the result in buffer-local variables so that
358 (set-buffer (find-file-noselect lib
))
360 (setq env
(elint-env-add-env env elint-buffer-env
)))
361 ;;; (with-temp-buffer
362 ;;; (insert-file-contents lib)
363 ;;; (with-syntax-table emacs-lisp-mode-syntax-table
364 ;;; (elint-update-env))
365 ;;; (setq env (elint-env-add-env env elint-buffer-env))))
366 ;;(message "Elint processed (require '%s)" name))
367 (error "Unable to find require'd library %s" name
)))
369 (message "Can't get variables from require'd library %s" name
)))
372 (defvar elint-top-form nil
373 "The currently linted top form, or nil.")
375 (defvar elint-top-form-logged nil
376 "T if the currently linted top form has been mentioned in the log buffer.")
378 (defun elint-top-form (form)
380 (let ((elint-top-form form
)
381 (elint-top-form-logged nil
)
382 (elint-current-pos (elint-top-form-pos form
)))
383 (elint-form (elint-top-form-form form
) elint-buffer-env
)))
386 ;;; General form linting functions
389 (defconst elint-special-forms
390 '((let . elint-check-let-form
)
391 (let* . elint-check-let-form
)
392 (setq . elint-check-setq-form
)
393 (quote . elint-check-quote-form
)
394 (cond . elint-check-cond-form
)
395 (lambda . elint-check-defun-form
)
396 (function . elint-check-function-form
)
397 (setq-default . elint-check-setq-form
)
398 (defun . elint-check-defun-form
)
399 (defsubst . elint-check-defun-form
)
400 (defmacro . elint-check-defun-form
)
401 (defvar . elint-check-defvar-form
)
402 (defconst . elint-check-defvar-form
)
403 (defcustom . elint-check-defcustom-form
)
404 (macro . elint-check-macro-form
)
405 (condition-case . elint-check-condition-case-form
))
406 "Functions to call when some special form should be linted.")
408 (defun elint-form (form env
)
409 "Lint FORM in the environment ENV.
410 The environment created by the form is returned."
413 (let ((func (cdr (assq (car form
) elint-special-forms
))))
416 (funcall func form env
)
418 (let* ((func (car form
))
419 (args (elint-get-args func env
))
422 ((eq args
'undefined
)
424 (elint-error "Call to undefined function: %s" func
))
426 ((eq args
'unknown
) nil
)
428 (t (setq argsok
(elint-match-args form args
))))
431 (if (elint-env-macrop env func
)
432 ;; Macro defined in buffer, expand it
434 ;; FIXME error if macro uses macro, eg bytecomp.el.
437 (macroexpand form
(elint-env-macro-env env
)) env
)
439 (elint-error "Elint failed to expand macro: %s" func
)
443 (let ((fcode (if (symbolp func
)
445 (indirect-function func
))
447 (if (and (listp fcode
) (eq (car fcode
) 'macro
))
448 ;; Macro defined outside buffer
450 (elint-form (macroexpand form
) env
)
452 ;; Function, lint its parameters
453 (elint-forms (cdr form
) env
))))))))
455 ;; :foo variables are quoted
456 (if (and (/= (aref (symbol-name form
) 0) ?
:)
457 (elint-unbound-variable form env
))
458 (elint-warning "Reference to unbound symbol: %s" form
))
463 (defun elint-forms (forms env
)
464 "Lint the FORMS, accumulating an environment, starting with ENV."
465 ;; grumblegrumbletailrecursiongrumblegrumble
467 (dolist (f forms env
)
468 (setq env
(elint-form f env
)))
470 (elint-error "Elint failed to parse form: %s" forms
)
473 (defun elint-unbound-variable (var env
)
474 "T if VAR is unbound in ENV."
475 (not (or (memq var
'(nil t
))
476 (elint-env-find-var env var
)
477 (memq var elint-builtin-variables
)
478 (memq var elint-autoloaded-variables
)
479 (memq var elint-standard-variables
))))
482 ;;; Function argument checking
485 (defun elint-match-args (arglist argpattern
)
486 "Match ARGLIST against ARGPATTERN."
493 ((and (null al
) (null ap
)) nil
)
494 ((eq (car ap
) '&optional
)
495 (setq state
'optional
)
498 ((eq (car ap
) '&rest
)
500 ((or (and (eq state
'all
) (or (null al
) (null ap
)))
501 (and (eq state
'optional
) (and al
(null ap
))))
502 (elint-error "Wrong number of args: %s, %s" arglist argpattern
)
505 ((and (eq state
'optional
) (null al
))
512 (defun elint-get-args (func env
)
513 "Find the args of FUNC in ENV.
514 Returns `unknown' if we couldn't find arguments."
515 (let ((f (elint-env-find-func env func
)))
520 (let ((fcode (indirect-function func
)))
522 ;; FIXME builtins with no args have args = nil.
523 (or (get func
'elint-args
) 'unknown
)
524 (elint-find-args-in-code fcode
)))
526 (elint-find-args-in-code func
)))))
528 (defun elint-find-args-in-code (code)
529 "Extract the arguments from CODE.
530 CODE can be a lambda expression, a macro, or byte-compiled code."
532 ((byte-code-function-p code
)
534 ((and (listp code
) (eq (car code
) 'lambda
))
536 ((and (listp code
) (eq (car code
) 'macro
))
537 (elint-find-args-in-code (cdr code
)))
541 ;;; Functions to check some special forms
544 (defun elint-check-cond-form (form env
)
545 "Lint a cond FORM in ENV."
546 (dolist (f (cdr form
) env
)
549 (elint-error "cond clause should be a list: %s" f
))))
551 (defun elint-check-defun-form (form env
)
552 "Lint a defun/defmacro/lambda FORM in ENV."
553 (setq form
(if (eq (car form
) 'lambda
) (cdr form
) (cddr form
)))
555 (or (memq p
'(&optional
&rest
))
556 (setq env
(elint-env-add-var env p
))))
558 (elint-forms (cdr form
) env
))
560 (defun elint-check-let-form (form env
)
561 "Lint the let/let* FORM in ENV."
562 (let ((varlist (cadr form
)))
565 (elint-error "Missing varlist in let: %s" form
)
567 ;; Check for (let (a (car b)) ...) type of error
568 (if (and (= (length varlist
) 2)
569 (symbolp (car varlist
))
570 (listp (car (cdr varlist
)))
571 (fboundp (car (car (cdr varlist
)))))
572 (elint-warning "Suspect varlist: %s" form
))
573 ;; Add variables to environment, and check the init values
578 (setq newenv
(elint-env-add-var newenv s
)))
579 ((and (consp s
) (<= (length s
) 2))
581 (if (eq (car form
) 'let
)
585 (elint-env-add-var newenv
(car s
))))
587 "Malformed `let' declaration: %s" s
))))
590 ;; Lint the body forms
591 (elint-forms (cddr form
) newenv
)))))
593 (defun elint-check-setq-form (form env
)
594 "Lint the setq FORM in ENV."
595 (or (= (mod (length form
) 2) 1)
596 (elint-error "Missing value in setq: %s" form
))
599 (setq form
(cdr form
))
603 form
(cdr (cdr form
)))
605 (if (elint-unbound-variable sym newenv
)
606 (elint-warning "Setting previously unbound symbol: %s" sym
))
607 (elint-error "Setting non-symbol in setq: %s" sym
))
608 (elint-form val newenv
)
610 (setq newenv
(elint-env-add-var newenv sym
))))
613 (defun elint-check-defvar-form (form env
)
614 "Lint the defvar/defconst FORM in ENV."
615 (if (or (= (length form
) 2)
617 (and (= (length form
) 4) (stringp (nth 3 form
))))
618 (elint-env-add-global-var (elint-form (nth 2 form
) env
)
620 (elint-error "Malformed variable declaration: %s" form
)
623 (defun elint-check-defcustom-form (form env
)
624 "Lint the defcustom FORM in ENV."
625 (if (and (> (length form
) 3)
626 ;; even no. of keyword/value args ?
627 (zerop (logand (length form
) 1)))
628 (elint-env-add-global-var (elint-form (nth 2 form
) env
)
630 (elint-error "Malformed variable declaration: %s" form
)
633 (defun elint-check-function-form (form env
)
634 "Lint the function FORM in ENV."
635 (let ((func (car (cdr-safe form
))))
638 (or (elint-env-find-func env func
)
640 (elint-warning "Reference to undefined function: %s" form
))
642 ((and (consp func
) (memq (car func
) '(lambda macro
)))
643 (elint-form func env
))
645 (t (elint-error "Not a function object: %s" form
)
648 (defun elint-check-quote-form (form env
)
649 "Lint the quote FORM in ENV."
652 (defun elint-check-macro-form (form env
)
653 "Check the macro FORM in ENV."
654 (elint-check-function-form (list (car form
) (cdr form
)) env
))
656 (defun elint-check-condition-case-form (form env
)
657 "Check the `condition-case' FORM in ENV."
659 (if (< (length form
) 3)
660 (elint-error "Malformed condition-case: %s" form
)
661 (or (symbolp (cadr form
))
662 (elint-warning "First parameter should be a symbol: %s" form
))
663 (setq resenv
(elint-form (nth 2 form
) env
))
664 (let ((newenv (elint-env-add-var env
(cadr form
)))
666 (dolist (err (nthcdr 3 form
))
667 (setq errlist
(car err
))
669 (or (get s
'error-conditions
)
670 (get s
'error-message
)
671 (memq s elint-extra-errors
)
673 "Not an error symbol in error handler: %s" s
)))
675 ((symbolp errlist
) (list errlist
))
676 ((listp errlist
) errlist
)
677 (t (elint-error "Bad error list in error handler: %s"
680 (elint-forms (cdr err
) newenv
))))
684 ;;; Message functions
687 (defvar elint-current-pos
) ; dynamically bound in elint-top-form
689 (defun elint-log (type string args
)
690 (elint-log-message (format "%s:%d:%s: %s"
691 (let ((f (buffer-file-name)))
693 (file-name-nondirectory f
)
695 (if (boundp 'elint-current-pos
)
697 (goto-char elint-current-pos
)
698 (1+ (count-lines (point-min)
699 (line-beginning-position))))
700 0) ; unknown position
702 (apply 'format string args
))))
704 (defun elint-error (string &rest args
)
705 "Report a linting error.
706 STRING and ARGS are thrown on `format' to get the message."
707 (elint-log "Error" string args
))
709 (defun elint-warning (string &rest args
)
710 "Report a linting warning.
712 (elint-log "Warning" string args
))
714 (defun elint-output (string)
715 "Print or insert STRING, depending on value of `noninteractive'."
717 (message "%s" string
)
718 (insert string
"\n")))
720 (defun elint-log-message (errstr &optional top
)
721 "Insert ERRSTR last in the lint log buffer.
722 Optional argument TOP non-nil means pretend `elint-top-form-logged' is non-nil."
723 (with-current-buffer (elint-get-log-buffer)
724 (goto-char (point-max))
725 (let ((inhibit-read-only t
))
726 (or (bolp) (newline))
727 ;; Do we have to say where we are?
728 (unless (or elint-top-form-logged top
)
729 (let* ((form (elint-top-form-form elint-top-form
))
732 ((memq top
'(defun defsubst))
733 (format "\nIn function %s:" (cadr form
)))
735 (format "\nIn macro %s:" (cadr form
)))
736 ((memq top
'(defvar defconst
))
737 (format "\nIn variable %s:" (cadr form
)))
738 (t "\nIn top level expression:"))))
739 (setq elint-top-form-logged t
))
740 (elint-output errstr
))))
742 (defun elint-clear-log (&optional header
)
743 "Clear the lint log buffer.
744 Insert HEADER followed by a blank line if non-nil."
745 (let ((dir default-directory
))
746 (with-current-buffer (elint-get-log-buffer)
747 (setq default-directory dir
)
748 (let ((inhibit-read-only t
))
750 (if header
(insert header
"\n"))))))
752 (defun elint-display-log ()
753 "Display the lint log buffer."
754 (let ((pop-up-windows t
))
755 (display-buffer (elint-get-log-buffer))
758 (defvar elint-running
)
760 (defun elint-set-mode-line (&optional on
)
761 "Set the mode-line-process of the Elint log buffer."
762 (with-current-buffer (elint-get-log-buffer)
763 (and (eq major-mode
'compilation-mode
)
764 (setq mode-line-process
765 (list (if (or on
(bound-and-true-p elint-running
))
766 (propertize ":run" 'face
'compilation-warning
)
767 (propertize ":finished" 'face
'compilation-info
)))))))
769 (defun elint-get-log-buffer ()
770 "Return a log buffer for elint."
771 (or (get-buffer elint-log-buffer
)
772 (with-current-buffer (get-buffer-create elint-log-buffer
)
773 (or (eq major-mode
'compilation-mode
)
775 (setq buffer-undo-list t
)
779 ;;; Initializing code
783 (defun elint-initialize (&optional reinit
)
785 If elint is already initialized, this does nothing, unless
786 optional prefix argument REINIT is non-nil."
788 (if (and elint-builtin-variables
(not reinit
))
789 (message "Elint is already initialized")
790 (message "Initializing elint...")
791 (setq elint-builtin-variables
(elint-find-builtin-variables)
792 elint-autoloaded-variables
(elint-find-autoloaded-variables))
793 (mapc (lambda (x) (or (not (symbolp (car x
)))
794 (eq (cdr x
) 'unknown
)
795 (put (car x
) 'elint-args
(cdr x
))))
796 (elint-find-builtin-args))
797 (if elint-unknown-builtin-args
798 (mapc (lambda (x) (put (car x
) 'elint-args
(cdr x
)))
799 elint-unknown-builtin-args
))
800 (message "Initializing elint...done")))
803 (defun elint-find-builtin-variables ()
804 "Return a list of all built-in variables."
805 ;; Cribbed from help-fns.el.
806 (let ((docbuf " *DOC*")
809 (if (get-buffer docbuf
)
812 (goto-char (point-min)))
813 (set-buffer (get-buffer-create docbuf
))
814 (insert-file-contents-literally
815 (expand-file-name internal-doc-file-name doc-directory
)))
816 (while (search-forward "\1fV" nil t
)
817 (and (setq var
(intern-soft
818 (buffer-substring (point) (line-end-position))))
820 (setq vars
(cons var vars
))))
823 (defun elint-find-autoloaded-variables ()
824 "Return a list of all autoloaded variables."
827 (insert-file-contents (locate-library "loaddefs.el"))
828 (while (re-search-forward "^(defvar \\([[:alnum:]_-]+\\)" nil t
)
829 (and (setq var
(intern-soft (match-string 1)))
831 (setq vars
(cons var vars
)))))
834 (defun elint-find-builtins ()
835 "Return a list of all built-in functions."
837 (mapatoms (lambda (s) (and (fboundp s
) (subrp (symbol-function s
))
838 (setq subrs
(cons s subrs
)))))
841 (defun elint-find-builtin-args (&optional list
)
842 "Return a list of the built-in functions and their arguments.
843 If LIST is nil, call `elint-find-builtins' to get a list of all built-in
844 functions, otherwise use LIST.
846 Each function is represented by a cons cell:
847 \(function-symbol . args)
848 If no documentation could be found args will be `unknown'."
850 (let ((doc (documentation f t
)))
852 (string-match "\n\n(fn\\(.*)\\)\\'" doc
)
854 ;; "BODY...)" -> "&rest BODY)".
855 (read (replace-regexp-in-string
856 "\\([^ ]+\\)\\.\\.\\.)\\'" "&rest \\1)"
857 (format "(%s %s" f
(match-string 1 doc
)) t
))))
859 (or list
(elint-find-builtins))))
863 ;; arch-tag: b2f061e2-af84-4ddc-8e39-f5e969ac228f
864 ;;; elint.el ends here