If the goal is to get information about functions and variables already in the environment:
For docstrings of functions and macros, see the documentation
function.
For variable docstrings, use documentation-property
; for example:
(documentation-property
'user-init-file 'variable-documentation)
For function arity and the argument list, see this Emacs.SE question, the answer, and comments to the question.
(I found this by pressing C-h k C-h f
and skimming the source code of describe-function
(same for variable docstrings, but studying describe-variable
).)
To analyze an Emacs Lisp source code file, assuming that the goal is to get information about top-level def.*
forms, one can do something similar to the following.
(defun get-defun-info (buffer)
"Get information about all `defun' top-level sexps in a buffer
BUFFER. Returns a list with elements of the form (symbol args docstring)."
(with-current-buffer buffer
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(let (result)
;; keep going while reading succeeds
(while (condition-case nil
(progn
(read (current-buffer))
(forward-sexp -1)
t)
(error nil))
(let ((form (read (current-buffer))))
(cond
((not (listp form)) ; if it's not a list, skip it
nil)
((eq (nth 0 form) 'defun) ; if it's a defun, collect info
(let ((sym (nth 1 form))
(args (nth 2 form))
(doc (when (stringp (nth 3 form)) (nth 3 form))))
(push (list sym args doc) result))))))
result)))))
This can be easily extended to defvar
, defconst
, etc.
To handle defun
appearing inside top-level forms one would have to descend into these forms, possibly using recursion.