Change purpose of autodef cookie argument

;;;###autodef FORM

FORM was used as a predicate for inclusion as an autodef. Now it is used
as the replacement sexp in case the module is disabled.

Oh, you don't know what autdefs are? Well let me explain (thanks for
asking, by the way). An autdef'ed function, macro, or function alias is
always available to be called, anywhere in Doom, even if its containing
module is disabled. For instance:

  ;;;###autodef
  (defun say-hello! (name)  ; the trailing ! denotes an autodef
    (message "Hello %s" name))

This makes it safe to call `do-something` without a check whether it
exists (or if its module is enabled). When the module is enabled, an
autoload entry is added to the Doom autoloads file:

  (autoload 'do-something "path/to/some/modules/autoloads")

And it is autoloaded as normal when it is first used. However, if the
module is disabled, then this is inserted instead:

  (defmacro do-something (&rest _))

This no-ops; it does nothing and doesn't evaluate its arguments. If FORM
above was provided, that is used instead of a noop macro.

It's a little smarter than simple substitution, but that's the gist of
it.
This commit is contained in:
Henrik Lissner 2019-03-01 15:16:25 -05:00
parent 8832737671
commit 8903eebdc4
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -156,7 +156,7 @@ even if it doesn't need reloading!"
forms) forms)
(while (re-search-forward "^;;;###autodef *\\([^\n]+\\)?\n" nil t) (while (re-search-forward "^;;;###autodef *\\([^\n]+\\)?\n" nil t)
(let* ((sexp (sexp-at-point)) (let* ((sexp (sexp-at-point))
(pred (match-string 1)) (alt-sexp (match-string 1))
(type (car sexp)) (type (car sexp))
(name (doom-unquote (cadr sexp))) (name (doom-unquote (cadr sexp)))
(origin (cond ((doom-module-from-path path)) (origin (cond ((doom-module-from-path path))
@ -166,37 +166,39 @@ even if it doesn't need reloading!"
`(:core . ,(intern (file-name-base path)))))) `(:core . ,(intern (file-name-base path))))))
(doom-file-form (doom-file-form
`(put ',name 'doom-file ,(abbreviate-file-name path)))) `(put ',name 'doom-file ,(abbreviate-file-name path))))
(cond ((memq type '(defun defmacro cl-defun cl-defmacro)) (cond ((and (not member-p) alt-sexp)
(push (read alt-sexp) forms))
((memq type '(defun defmacro cl-defun cl-defmacro))
(cl-destructuring-bind (_ name arglist &rest body) sexp (cl-destructuring-bind (_ name arglist &rest body) sexp
(let ((docstring (if (stringp (car body)) (let ((docstring (if (stringp (car body))
(pop body) (pop body)
"No documentation."))) "No documentation.")))
(push (cond ((not (and member-p (push (if member-p
(or (null pred) (make-autoload sexp (abbreviate-file-name (file-name-sans-extension path)))
(let ((load-file-name path)) (push doom-file-form forms)
(eval (read pred) t))))) (setq docstring (format "THIS FUNCTION DOES NOTHING BECAUSE %s IS DISABLED\n\n%s"
(push doom-file-form forms) origin docstring))
(setq docstring (format "THIS FUNCTION DOES NOTHING BECAUSE %s IS DISABLED\n\n%s" (condition-case-unless-debug e
origin docstring)) (if alt-sexp
(condition-case-unless-debug e (read alt-sexp)
(append (list (pcase type (append (list (pcase type
(`defun 'defmacro) (`defun 'defmacro)
(`cl-defun `cl-defmacro) (`cl-defun `cl-defmacro)
(_ type)) (_ type))
name arglist docstring) 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 arg into syms collect arg into syms
else if (listp arg) else if (listp arg)
collect (car arg) into syms collect (car arg) into syms
finally return (if syms `((ignore ,@syms))))) finally return (if syms `((ignore ,@syms))))))
('error ('error
(message "Ignoring autodef %s (%s)" (message "Ignoring autodef %s (%s)"
name e) name e)
nil))) nil)))
((make-autoload sexp (abbreviate-file-name (file-name-sans-extension path)))))
forms) forms)
(push `(put ',name 'doom-module ',origin) forms)))) (push `(put ',name 'doom-module ',origin) forms))))
@ -204,19 +206,16 @@ even if it doesn't need reloading!"
(cl-destructuring-bind (_type name target &optional docstring) sexp (cl-destructuring-bind (_type name target &optional docstring) sexp
(let ((name (doom-unquote name)) (let ((name (doom-unquote name))
(target (doom-unquote target))) (target (doom-unquote target)))
(unless (and member-p (unless member-p
(or (null pred) (setq docstring (format "THIS FUNCTION DOES NOTHING BECAUSE %s IS DISABLED\n\n%s"
(let ((load-file-name path)) origin docstring))
(eval (read pred) t))))
(setq target #'ignore)) (setq target #'ignore))
(push doom-file-form forms) (push doom-file-form forms)
(push `(put ',name 'doom-module ',origin) forms) (push `(put ',name 'doom-module ',origin) forms)
(push `(defalias ',name #',target ,docstring) (push `(defalias ',name #',target ,docstring)
forms)))) forms))))
((and member-p (member-p
(or (null pred)
(eval (read pred) t)))
(push sexp forms))))) (push sexp forms)))))
(if forms (if forms
(concat (string-join (mapcar #'prin1-to-string (reverse forms)) "\n") (concat (string-join (mapcar #'prin1-to-string (reverse forms)) "\n")