emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 77f67d1 1/2: Add new convenience function `buffer-local-boundp'


From: Lars Ingebrigtsen
Subject: master 77f67d1 1/2: Add new convenience function `buffer-local-boundp'
Date: Mon, 31 May 2021 01:22:31 -0400 (EDT)

branch: master
commit 77f67d12f68a9fba210337b9ad38f049ec601fcb
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Add new convenience function `buffer-local-boundp'
    
    * doc/lispref/variables.texi (Creating Buffer-Local): Document it.
    * lisp/subr.el (buffer-local-boundp): New function.
    
    * src/data.c (Flocal_variable_p): Mention it.
---
 doc/lispref/variables.texi |  6 ++++++
 etc/NEWS                   |  4 ++++
 lisp/subr.el               |  8 ++++++++
 src/data.c                 |  4 +++-
 test/lisp/subr-tests.el    | 10 ++++++++++
 5 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi
index 36abc31..62c76f0 100644
--- a/doc/lispref/variables.texi
+++ b/doc/lispref/variables.texi
@@ -1582,6 +1582,12 @@ buffer-local binding in buffer @var{buffer}, it returns 
the default
 value (@pxref{Default Value}) of @var{variable} instead.
 @end defun
 
+@defun buffer-local-boundp variable buffer
+This returns non-@code{nil} if there's either a buffer-local binding
+of @var{variable} (a symbol) in buffer @var{buffer}, or @var{variable}
+has a global binding.
+@end defun
+
 @defun buffer-local-variables &optional buffer
 This function returns a list describing the buffer-local variables in
 buffer @var{buffer}.  (If @var{buffer} is omitted, the current buffer
diff --git a/etc/NEWS b/etc/NEWS
index c6e7084..6622861 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2726,6 +2726,10 @@ customize them.
 
 * Lisp Changes in Emacs 28.1
 
++++
+** New function 'buffer-local-boundp'.
+This predicate says whether a symbol is bound in a specific buffer.
+
 ---
 ** Emacs now attempts to test for high-rate subprocess output more fairly.
 When several subprocesses produce output simultaneously at high rate,
diff --git a/lisp/subr.el b/lisp/subr.el
index 8874015..e49c277 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -195,6 +195,14 @@ buffer-local wherever it is set."
   (list 'progn (list 'defvar var val docstring)
         (list 'make-variable-buffer-local (list 'quote var))))
 
+(defun buffer-local-boundp (symbol buffer)
+  "Return non-nil if SYMBOL is bound in BUFFER.
+Also see `local-variable-p'."
+  (condition-case nil
+      (buffer-local-value symbol buffer)
+    (:success t)
+    (void-variable nil)))
+
 (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/src/data.c b/src/data.c
index d547f5d..059f31e 100644
--- a/src/data.c
+++ b/src/data.c
@@ -2200,7 +2200,9 @@ From now on the default value will apply in this buffer.  
Return VARIABLE.  */)
 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
        1, 2, 0,
        doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
-BUFFER defaults to the current buffer.  */)
+BUFFER defaults to the current buffer.
+
+Also see `buffer-local-boundp'.*/)
   (Lisp_Object variable, Lisp_Object buffer)
 {
   struct buffer *buf = decode_buffer (buffer);
diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el
index 1e14673..375251c 100644
--- a/test/lisp/subr-tests.el
+++ b/test/lisp/subr-tests.el
@@ -684,5 +684,15 @@ See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19350.";
   (should (>= (length (apropos-internal "^help" #'commandp)) 15))
   (should-not (apropos-internal "^next-line$" #'keymapp)))
 
+
+(ert-deftest test-buffer-local-boundp ()
+  (let ((buf (generate-new-buffer "boundp")))
+    (with-current-buffer buf
+      (setq-local test-boundp t))
+    (setq test-global-boundp t)
+    (should (buffer-local-boundp 'test-boundp buf))
+    (should-not (buffer-local-boundp 'test-not-boundp buf))
+    (should (buffer-local-boundp 'test-global-boundp buf))))
+
 (provide 'subr-tests)
 ;;; subr-tests.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]