1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-23 07:19:15 +00:00

Move buffer-local-set-state to subr because it's used at runtime

* lisp/subr.el (buffer-local-set-state)
(buffer-local-set-state--get, buffer-local-restore-state): Moved
from easy-mmode.el because they have to be available run-time.
This commit is contained in:
Lars Ingebrigtsen 2022-05-06 16:09:38 +02:00
parent 92bbe911e9
commit afc14e4f66
5 changed files with 46 additions and 60 deletions

View File

@ -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

View File

@ -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-"))
;;;***

View File

@ -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)),

View File

@ -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

View File

@ -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