Honor nil values in erc--restore-initialize-priors

* lisp/erc/erc.el (erc--restore-initialize-priors): Don't produce
invalid "empty" `setq' form when parameter VARS contains defaults that
initialize to nil.  This function is mainly used by local modules,
which were first made usable in ERC 5.5 (bug#57955).
* test/lisp/erc/erc-tests.el (erc--restore-initialize-priors): Fix
expected expansion, which is now slightly leaner.  (Bug#60936)
This commit is contained in:
F. Jason Park 2023-10-05 00:16:46 -07:00
parent 328a985651
commit 9120d7a32e
2 changed files with 15 additions and 19 deletions

View file

@ -1366,16 +1366,15 @@ buffer during local-module setup and `erc-mode-hook' activation.")
(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."

View file

@ -796,18 +796,15 @@
(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 ()