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)))
(let* ((type (car sexp))
(name (nth 1 sexp))
(arglist (nth 2 sexp))
(docstring (format "(Actually defined in %s)\n\n%s"
(abbreviate-file-name path)
(if (stringp (nth 3 sexp)) (nth 3 sexp) "No docstring")))
(body (cdddr (if (stringp (nth 3 sexp)) (cdr sexp) sexp))))
(cond ((not (memq type '(defun defmacro cl-defun cl-defmacro)))
(message "Ignoring invalid autodef %s (found %s)" (message "Ignoring invalid autodef %s (found %s)"
name type)) name type)
((and member-p (cl-destructuring-bind (type name arglist docstring &rest body) sexp
(or (null cond) (unless (stringp docstring)
(eval cond t))) (push docstring body)
(push (if (memq type '(defmacro cl-defmacro)) (setq docstring "No documentation."))
sexp (let ((origin (cond ((doom-module-from-path path))
(make-autoload sexp path)) ((file-in-directory-p path doom-private-dir)
forms)) `(:private . ,(intern (file-name-base path))))
(sexp ((file-in-directory-p path doom-emacs-dir)
(condition-case e `(:core . ,(intern (file-name-base path)))))))
(push (append (list 'defmacro name arglist docstring) (push (cond ((not (and member-p
(or (null pred)
(eval (read pred) t))))
(condition-case-unless-debug e
(append
(list 'defmacro name arglist
(format "THIS FUNCTION DOES NOTHING BECAUSE %s IS DISABLED\n\n%s"
origin
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)
forms) else if (listp arg)
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")
"")))))) ""))))))