branch: externals/compat commit 28a1e8e377fb2884c7dbc564f6fed1e65a33f8ba Author: Daniel Mendler <m...@daniel-mendler.de> Commit: Daniel Mendler <m...@daniel-mendler.de>
compat-30: Add find-buffer and get-truename-buffer --- NEWS.org | 1 + compat-30.el | 15 +++++++++++++++ compat-tests.el | 27 +++++++++++++++++++++++++++ compat.texi | 11 +++++++++++ 4 files changed, 54 insertions(+) diff --git a/NEWS.org b/NEWS.org index 043d45ac15..b081c4c3e6 100644 --- a/NEWS.org +++ b/NEWS.org @@ -11,6 +11,7 @@ - compat-30: New variables =completion-lazy-hilit= and =completion-lazy-hilit-fn= and new function =completion-lazy-hilit=. - compat-30: New function =require-with-check=. +- compat-30: New functions =find-buffer= and =get-truename-buffer=. * Release of "Compat" Version 29.1.4.4 diff --git a/compat-30.el b/compat-30.el index 36e6a19d02..b68cc407e2 100644 --- a/compat-30.el +++ b/compat-30.el @@ -27,6 +27,21 @@ ;; TODO Update to 30.1 as soon as the Emacs emacs-30 branch version bumped (compat-version "30.0.50") +;;;; Defined in buffer.c + +(compat-defun find-buffer (variable value) ;; <compat-tests:find-buffer> + "Return the buffer with buffer-local VARIABLE equal to VALUE. +If there is no such live buffer, return nil." + (cl-loop for buffer the buffers + if (equal (buffer-local-value variable buffer) value) + return buffer)) + +(compat-defun get-truename-buffer (filename) ;; <compat-tests:get-truename-buffer> + "Return the buffer with `file-truename' equal to FILENAME (a string). +If there is no such live buffer, return nil. +See also `find-buffer-visiting'." + (find-buffer 'buffer-file-truename filename)) + ;;;; Defined in files.el (compat-defun require-with-check (feature &optional filename noerror) ;; <compat-tests:require-with-check> diff --git a/compat-tests.el b/compat-tests.el index 12e52ea370..88992f690a 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -3084,5 +3084,32 @@ (should-equal 'compat-reload (require-with-check 'compat-reload nil 'noerror)) (should-equal 'compat-reload (require-with-check 'compat-reload nil 'reload))))))) +(defvar compat-tests-find-buffer nil) +(ert-deftest compat-find-buffer () + ;; TODO enable test on Emacs 30 as soon as the CI supports it. + (static-if (< emacs-major-version 30) + (let ((buf1 (get-buffer-create "*compat-tests-buf1*")) + (buf2 (get-buffer-create "*compat-tests-buf2*"))) + (with-current-buffer buf1 + (setq-local compat-tests-find-buffer 1)) + (with-current-buffer buf2 + (setq-local compat-tests-find-buffer 2)) + (should-equal buf1 (find-buffer 'compat-tests-find-buffer 1)) + (should-equal buf2 (find-buffer 'compat-tests-find-buffer 2)) + (should-not (find-buffer 'compat-tests-find-buffer 3))))) + +(ert-deftest compat-get-truename-buffer () + ;; TODO enable test on Emacs 30 as soon as the CI supports it. + (static-if (< emacs-major-version 30) + (let ((buf1 (get-buffer-create "*compat-tests-buf1*")) + (buf2 (get-buffer-create "*compat-tests-buf2*"))) + (with-current-buffer buf1 + (setq-local buffer-file-truename "compat-tests-file1")) + (with-current-buffer buf2 + (setq-local buffer-file-truename "compat-tests-file2")) + (should-equal buf1 (get-truename-buffer "compat-tests-file1")) + (should-equal buf2 (get-truename-buffer "compat-tests-file2")) + (should-not (get-truename-buffer "compat-tests-file3"))))) + (provide 'compat-tests) ;;; compat-tests.el ends here diff --git a/compat.texi b/compat.texi index 95c9257b79..696418b08a 100644 --- a/compat.texi +++ b/compat.texi @@ -3348,6 +3348,17 @@ older than 30.1. Note that due to upstream changes, it might happen that there will be the need for changes, so use these functions with care. +@defun get-truename-buffer filename +Return the buffer with @code{file-truename} equal to @var{filename} (a string). +If there is no such live buffer, return nil. +See also @code{find-buffer-visiting}. +@end defun + +@defun find-buffer variable value +Return the buffer with buffer-local @var{variable} equal to @var{value}. +If there is no such live buffer, return nil. +@end defun + @c copied from lispref/loading.texi @defun require-with-check feature &optional filename noerror This function works like @code{require}, except if @var{feature} is