From: Lars Ingebrigtsen Date: Mon, 31 May 2021 05:21:09 +0000 (+0200) Subject: Add new convenience function `buffer-local-boundp' X-Git-Tag: emacs-28.0.90~2254 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=77f67d12f68a9fba210337b9ad38f049ec601fcb;p=emacs.git 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. --- diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi index 36abc316cbb..62c76f09c0d 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 c6e7084118f..6622861aaf1 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 88740159b93..e49c2773357 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 d547f5da5e0..059f31e514b 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 1e146732163..375251cffc5 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