From 1c0407a10aae4920084198341a547f8be467e073 Mon Sep 17 00:00:00 2001 From: Gerd Moellmann Date: Tue, 5 Sep 2023 08:39:06 +0200 Subject: [PATCH] Rework buffer-package --- lisp/emacs-lisp/pkg.el | 9 ++++----- src/eval.c | 35 ++++++++++++++++------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/lisp/emacs-lisp/pkg.el b/lisp/emacs-lisp/pkg.el index 9e09daf0617..a4be4f71d98 100644 --- a/lisp/emacs-lisp/pkg.el +++ b/lisp/emacs-lisp/pkg.el @@ -177,14 +177,13 @@ Otherwise assume that " (error "%s does not contain a symbol %s" (package-name package) name)))) +;;;###autoload (cl-defun buffer-package (&optional (buffer (current-buffer))) "Return the value of *package* set in BUFFER. BUFFER must be either a buffer object or the name of an existing buffer." - (let ((buffer (if (bufferp buffer) - buffer - (get-buffer buffer)))) - (with-current-buffer buffer - (default-buffer-local-value '*package*)))) + (let ((buffer (or (get-buffer buffer) + (error "Buffer not found: %s" buffer)))) + (default-buffer-local-value '*package* buffer))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Macros diff --git a/src/eval.c b/src/eval.c index f49a66b8cac..4d247556264 100644 --- a/src/eval.c +++ b/src/eval.c @@ -677,24 +677,16 @@ default_toplevel_binding (Lisp_Object symbol) } static union specbinding * -default_buffer_local_binding (Lisp_Object symbol) +default_buffer_local_binding (Lisp_Object symbol, Lisp_Object buffer) { union specbinding *binding = NULL; - union specbinding *pdl = specpdl_ptr; - while (pdl > specpdl) - { - switch ((--pdl)->kind) - { - case SPECPDL_LET_LOCAL: - case SPECPDL_LET_DEFAULT: - case SPECPDL_LET: - if (EQ (specpdl_symbol (pdl), symbol)) - binding = pdl; - break; - default: break; - } - } + for (union specbinding *pdl = specpdl_ptr - 1; pdl >= specpdl; --pdl) + if (pdl->kind == SPECPDL_LET_LOCAL + && EQ (specpdl_symbol (pdl), symbol) + && EQ (pdl->let.where, buffer)) + binding = pdl; + return binding; } @@ -725,6 +717,8 @@ lexbound_p (Lisp_Object symbol) return false; } +// clang-format off + DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0, doc: /* Return SYMBOL's toplevel default value. "Toplevel" means outside of any let binding. */) @@ -738,11 +732,12 @@ DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_valu xsignal1 (Qvoid_variable, symbol); } -DEFUN ("default-buffer-local-value", Fdefault_buffer_local_value, Sdefault_buffer_local_value, 1, 1, 0, - doc: /* Return SYMBOL's toplevel buffer-local value. */) - (Lisp_Object symbol) +DEFUN ("default-buffer-local-value", Fdefault_buffer_local_value, + Sdefault_buffer_local_value, 2, 2, 0, + doc: /* Return SYMBOL's toplevel buffer-local value in BUFFER. */) + (Lisp_Object symbol, Lisp_Object buffer) { - union specbinding *binding = default_buffer_local_binding (symbol); + union specbinding *binding = default_buffer_local_binding (symbol, buffer); Lisp_Object value = binding ? specpdl_old_value (binding) : Fdefault_value (symbol); if (!BASE_EQ (value, Qunbound)) @@ -794,6 +789,8 @@ value. */) return Qnil; } +// clang-format on + static Lisp_Object defvar (Lisp_Object sym, Lisp_Object initvalue, Lisp_Object docstring, bool eval) { -- 2.39.2