diff --git a/lisp/emacs-lisp/easy-mmode.el b/lisp/emacs-lisp/easy-mmode.el index 2568eaeb76a..54cac116168 100644 --- a/lisp/emacs-lisp/easy-mmode.el +++ b/lisp/emacs-lisp/easy-mmode.el @@ -825,42 +825,6 @@ Interactively, COUNT is the prefix numeric argument, and defaults to 1." ,@body)) (put ',prev-sym 'definition-name ',base)))) - -(defmacro buffer-local-set-state (&rest pairs) - "Like `setq-local', but allow restoring the previous state of locals later. -This macro returns an object that can be passed to `buffer-local-restore-state' -in order to restore the state of the local variables set via this macro. - -\(fn [VARIABLE VALUE]...)" - (declare (debug setq)) - (unless (zerop (mod (length pairs) 2)) - (error "PAIRS must have an even number of variable/value members")) - `(prog1 - (buffer-local-set-state--get ',pairs) - (setq-local ,@pairs))) - -;;;###autoload -(defun buffer-local-set-state--get (pairs) - (let ((states nil)) - (while pairs - (push (list (car pairs) - (and (boundp (car pairs)) - (local-variable-p (car pairs))) - (and (boundp (car pairs)) - (symbol-value (car pairs)))) - states) - (setq pairs (cddr pairs))) - (nreverse states))) - -;;;###autoload -(defun buffer-local-restore-state (states) - "Restore values of buffer-local variables recorded in STATES. -STATES should be an object returned by `buffer-local-set-state'." - (pcase-dolist (`(,variable ,local ,value) states) - (if local - (set variable value) - (kill-local-variable variable)))) - (provide 'easy-mmode) ;;; easy-mmode.el ends here diff --git a/lisp/ldefs-boot.el b/lisp/ldefs-boot.el index c0a16f9198a..b79c6b2a08b 100644 --- a/lisp/ldefs-boot.el +++ b/lisp/ldefs-boot.el @@ -8852,18 +8852,7 @@ CSS contains a list of syntax specifications of the form (CHAR . SYNTAX). (function-put 'easy-mmode-defsyntax 'lisp-indent-function '1) -(autoload 'buffer-local-set-state--get "easy-mmode" "\ - - -\(fn PAIRS)" nil nil) - -(autoload 'buffer-local-restore-state "easy-mmode" "\ -Restore buffer local variable values in STATES. -STATES is an object returned by `buffer-local-set-state'. - -\(fn STATES)" nil nil) - -(register-definition-prefixes "easy-mmode" '("buffer-local-set-state" "easy-mmode-")) +(register-definition-prefixes "easy-mmode" '("easy-mmode-")) ;;;*** diff --git a/lisp/subr.el b/lisp/subr.el index dec3b9190ed..5af802fa18d 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -207,6 +207,39 @@ Also see `local-variable-p'." (:success t) (void-variable nil))) +(defmacro buffer-local-set-state (&rest pairs) + "Like `setq-local', but allow restoring the previous state of locals later. +This macro returns an object that can be passed to `buffer-local-restore-state' +in order to restore the state of the local variables set via this macro. + +\(fn [VARIABLE VALUE]...)" + (declare (debug setq)) + (unless (zerop (mod (length pairs) 2)) + (error "PAIRS must have an even number of variable/value members")) + `(prog1 + (buffer-local-set-state--get ',pairs) + (setq-local ,@pairs))) + +(defun buffer-local-set-state--get (pairs) + (let ((states nil)) + (while pairs + (push (list (car pairs) + (and (boundp (car pairs)) + (local-variable-p (car pairs))) + (and (boundp (car pairs)) + (symbol-value (car pairs)))) + states) + (setq pairs (cddr pairs))) + (nreverse states))) + +(defun buffer-local-restore-state (states) + "Restore values of buffer-local variables recorded in STATES. +STATES should be an object returned by `buffer-local-set-state'." + (pcase-dolist (`(,variable ,local ,value) states) + (if local + (set variable value) + (kill-local-variable variable)))) + (defmacro push (newelt place) "Add NEWELT to the list stored in the generalized variable PLACE. This is morally equivalent to (setf PLACE (cons NEWELT PLACE)), diff --git a/test/lisp/emacs-lisp/easy-mmode-tests.el b/test/lisp/emacs-lisp/easy-mmode-tests.el index 697bf6c2152..f6d07196727 100644 --- a/test/lisp/emacs-lisp/easy-mmode-tests.el +++ b/test/lisp/emacs-lisp/easy-mmode-tests.el @@ -60,16 +60,4 @@ (easy-mmode-test-mode 'toggle) (should (eq easy-mmode-test-mode t)))) -(ert-deftest test-local-set-state () - (setq global 1) - (with-temp-buffer - (setq-local local 2) - (let ((state (buffer-local-set-state global 10 - local 20 - unexist 30))) - (buffer-local-restore-state state) - (should (= global 1)) - (should (= local 2)) - (should-not (boundp 'unexist))))) - ;;; easy-mmode-tests.el ends here diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index 3725f180f3a..6bcac2a5eba 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -1058,5 +1058,17 @@ final or penultimate step during initialization.")) (should (equal (kbd "C-x ( C-d C-x )") "")) (should (equal (kbd "C-x ( C-x )") ""))) +(ert-deftest test-local-set-state () + (setq global 1) + (with-temp-buffer + (setq-local local 2) + (let ((state (buffer-local-set-state global 10 + local 20 + unexist 30))) + (buffer-local-restore-state state) + (should (= global 1)) + (should (= local 2)) + (should-not (boundp 'unexist))))) + (provide 'subr-tests) ;;; subr-tests.el ends here