Simplify @def-setting and @set macros

This commit is contained in:
Henrik Lissner 2017-02-13 04:44:05 -05:00
parent d2a0e40ca4
commit c845a47ecf

View file

@ -3,17 +3,12 @@
;; This provides a centralized configuration system that a) won't evaluate its ;; This provides a centralized configuration system that a) won't evaluate its
;; arguments if it doesn't need to (performance), b) won't complain if the ;; arguments if it doesn't need to (performance), b) won't complain if the
;; setting doesn't exist and c) is more elegant than a bunch of `after!' blocks, ;; setting doesn't exist and c) is more elegant than a bunch of `@after' blocks,
;; which can cause intermittent stuttering in large quantities. I'm a fan of ;; which can cause intermittent stuttering in large quantities. I'm a fan of
;; concise, do-what-I-mean front-facing configuration, believe it or not. ;; concise, do-what-I-mean front-facing configuration, believe it or not.
;; ;;
;; Plus, it can benefit from byte-compilation. ;; Plus, it can benefit from byte-compilation.
;;;###autoload
(defvar doom-settings nil
"An alist of settings, mapping setting keywords to setter functions, which can
be a lambda or symbol.")
;;;###autoload ;;;###autoload
(defmacro @def-setting (keyword arglist &optional docstring &rest forms) (defmacro @def-setting (keyword arglist &optional docstring &rest forms)
"Define a setting macro. Like `defmacro', this should return a form to be "Define a setting macro. Like `defmacro', this should return a form to be
@ -21,20 +16,9 @@ executed when called with `@set'. FORMS are not evaluated until `@set' calls it.
(declare (indent defun) (doc-string 3)) (declare (indent defun) (doc-string 3))
(unless (keywordp keyword) (unless (keywordp keyword)
(error "Not a valid property name: %s" keyword)) (error "Not a valid property name: %s" keyword))
(let ((sym (intern (format "doom-setting--setter%s" keyword)))) `(defun ,(intern (format "doom-setting--setter%s" keyword)) ,arglist
(setq doom-settings (assq-delete-all keyword doom-settings)) ,docstring
(prog1 ,@forms))
`(progn
(defun ,sym ,arglist
,docstring
,@forms)
(push ',(list keyword
:source (__FILE__)
:docstring docstring
:fn sym)
doom-settings))
(let (byte-compile-warnings)
(byte-compile sym)))))
;;;###autoload ;;;###autoload
(defmacro @set (keyword &rest values) (defmacro @set (keyword &rest values)
@ -42,16 +26,15 @@ executed when called with `@set'. FORMS are not evaluated until `@set' calls it.
(declare (indent defun)) (declare (indent defun))
(unless values (unless values
(error "Empty @set for %s" keyword)) (error "Empty @set for %s" keyword))
(cond ((not values) (let ((fn (intern (format "doom-setting--setter%s" keyword))))
(error "Empty @set for %s" keyword)) (if (functionp fn)
((not (assq keyword doom-settings)) (apply fn (eval `(list ,@values)))
(when doom-debug-mode (when doom-debug-mode
(warn "No setting found for %s" keyword))) (warn "No setting found for %s" keyword)))))
(t
(let* ((plist (cdr (assq keyword doom-settings)))
(fn (plist-get plist :fn))) ;; (defun describe-setting ()
(if fn ;; (interactive)
(let ((values (eval `(list ,@values)))) ;; ;; TODO
(apply fn values)) ;; (error "Not implemented yet"))
(error "No setting function where one was expected for %s" keyword))))))