Refactor autodef generator; put metadata in autodefs

Adds the doom-module and doom-file symbol properties to autodefs,
allowing functions like doom/describe-setting some insight into the
origin of the autodef.
This commit is contained in:
Henrik Lissner 2018-06-15 14:56:45 +02:00
parent bbda434365
commit da5e2d54ca
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -438,43 +438,50 @@ even if it doesn't need reloading!"
(insert-file-contents path) (insert-file-contents path)
(let ((member-p (or (member path enabled-targets) (let ((member-p (or (member path enabled-targets)
(file-in-directory-p path doom-core-dir))) (file-in-directory-p path doom-core-dir)))
forms sexp cond) forms)
(while (re-search-forward "^;;;###autodef *\\([^\n]+\\)?\n" nil t) (while (re-search-forward "^;;;###autodef *\\([^\n]+\\)?\n" nil t)
(setq sexp (sexp-at-point) (let ((sexp (sexp-at-point))
cond (match-string 1)) (pred (match-string 1)))
(when cond (if (not (memq (car sexp) '(defun defmacro cl-defun cl-defmacro def-setting!)))
(setq cond (read cond))) (message "Ignoring invalid autodef %s (found %s)"
(let* ((type (car sexp)) name type)
(name (nth 1 sexp)) (cl-destructuring-bind (type name arglist docstring &rest body) sexp
(arglist (nth 2 sexp)) (unless (stringp docstring)
(docstring (format "(Actually defined in %s)\n\n%s" (push docstring body)
(abbreviate-file-name path) (setq docstring "No documentation."))
(if (stringp (nth 3 sexp)) (nth 3 sexp) "No docstring"))) (let ((origin (cond ((doom-module-from-path path))
(body (cdddr (if (stringp (nth 3 sexp)) (cdr sexp) sexp)))) ((file-in-directory-p path doom-private-dir)
(cond ((not (memq type '(defun defmacro cl-defun cl-defmacro))) `(:private . ,(intern (file-name-base path))))
(message "Ignoring invalid autodef %s (found %s)" ((file-in-directory-p path doom-emacs-dir)
name type)) `(:core . ,(intern (file-name-base path)))))))
((and member-p (push (cond ((not (and member-p
(or (null cond) (or (null pred)
(eval cond t))) (eval (read pred) t))))
(push (if (memq type '(defmacro cl-defmacro)) (condition-case-unless-debug e
sexp (append
(make-autoload sexp path)) (list 'defmacro name arglist
forms)) (format "THIS FUNCTION DOES NOTHING BECAUSE %s IS DISABLED\n\n%s"
(sexp origin
(condition-case e docstring))
(push (append (list 'defmacro name arglist docstring) (cl-loop for arg in arglist
(cl-loop for arg in arglist if (and (symbolp arg)
if (and (symbolp arg) (not (keywordp arg))
(not (keywordp arg)) (not (memq arg cl--lambda-list-keywords)))
(not (memq arg cl--lambda-list-keywords))) collect (list 'ignore arg)
collect (list 'ignore arg))) else if (listp arg)
forms) collect (list 'ignore (car arg))))
('error ('error
(message "Ignoring autodef %s (%s)" (message "Ignoring autodef %s (%s)"
name e))))))) name e)
nil)))
((memq type '(defmacro cl-defmacro def-setting!))
sexp)
((make-autoload sexp path)))
forms)
(push `(put ',name 'doom-file ',(abbreviate-file-name path)) forms)
(push `(put ',name 'doom-module ',origin) forms))))))
(if forms (if forms
(concat (string-join (mapcar #'prin1-to-string forms) "\n") (concat (string-join (mapcar #'prin1-to-string (reverse forms)) "\n")
"\n") "\n")
"")))))) ""))))))