(defmacro erc--restore-initialize-priors (mode &rest vars)
"Restore local VARS for MODE from a previous session."
(declare (indent 1))
- (let ((existing (make-symbol "existing"))
+ (let ((priors (make-symbol "priors"))
+ (initp (make-symbol "initp"))
;;
- restore initialize)
- (while-let ((k (pop vars)) (v (pop vars)))
- (push `(,k (alist-get ',k ,existing)) restore)
- (push `(,k ,v) initialize))
- `(if-let* ((,existing (or erc--server-reconnecting erc--target-priors))
- ((alist-get ',mode ,existing)))
- (setq ,@(mapcan #'identity (nreverse restore)))
- (setq ,@(mapcan #'identity (nreverse initialize))))))
+ forms)
+ (while-let ((k (pop vars)))
+ (push `(,k (if ,initp (alist-get ',k ,priors) ,(pop vars))) forms))
+ `(let* ((,priors (or erc--server-reconnecting erc--target-priors))
+ (,initp (and ,priors (alist-get ',mode ,priors))))
+ (setq ,@(mapcan #'identity (nreverse forms))))))
(defun erc--target-from-string (string)
"Construct an `erc--target' variant from STRING."
(should (erc--valid-local-channel-p "&local")))))
(ert-deftest erc--restore-initialize-priors ()
- ;; This `pcase' expands to 100+k. Guess we could do something like
- ;; (and `(,_ ((,e . ,_) . ,_) . ,_) v) first and then return a
- ;; (equal `(if-let* ((,e ...)...)...) v) to cut it down to < 1k.
(should (pcase (macroexpand-1 '(erc--restore-initialize-priors erc-my-mode
foo (ignore 1 2 3)
- bar #'spam))
- (`(if-let* ((,e (or erc--server-reconnecting erc--target-priors))
- ((alist-get 'erc-my-mode ,e)))
- (setq foo (alist-get 'foo ,e)
- bar (alist-get 'bar ,e))
- (setq foo (ignore 1 2 3)
- bar #'spam))
+ bar #'spam
+ baz nil))
+ (`(let* ((,p (or erc--server-reconnecting erc--target-priors))
+ (,q (and ,p (alist-get 'erc-my-mode ,p))))
+ (setq foo (if ,q (alist-get 'foo ,p) (ignore 1 2 3))
+ bar (if ,q (alist-get 'bar ,p) #'spam)
+ baz (if ,q (alist-get 'baz ,p) nil)))
t))))
(ert-deftest erc--target-from-string ()