From cfe89a74a63e8205782b7217ae01145b54a27fcc Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Mon, 12 May 2025 23:58:22 +0200 Subject: compat-31: New function hash-table-contains-p --- NEWS.org | 1 + compat-31.el | 6 ++++++ compat-tests.el | 8 ++++++++ compat.texi | 5 +++++ 4 files changed, 20 insertions(+) diff --git a/NEWS.org b/NEWS.org index e51f4f2..086de05 100644 --- a/NEWS.org +++ b/NEWS.org @@ -15,6 +15,7 @@ - compat-31: New macro =with-work-buffer=. - compat-31: New function =unbuttonize-region=. - compat-31: New extended function =seconds-to-string=. +- compat-31: New function =hash-table-contains-p=. - Drop support for Emacs 24.x. Emacs 25.1 is required now. In case Emacs 24.x support is still needed, Compat 30 can be used. diff --git a/compat-31.el b/compat-31.el index b6aa5e3..e27ef0f 100644 --- a/compat-31.el +++ b/compat-31.el @@ -29,6 +29,12 @@ ;;;; Defined in subr.el +(compat-defun hash-table-contains-p (key table) ;; + "Return non-nil if TABLE has an element with KEY." + (declare (side-effect-free t)) + (let ((missing '#:missing)) + (not (eq (gethash key table missing) missing)))) + (compat-defmacro static-when (condition &rest body) ;; "A conditional compilation macro. Evaluate CONDITION at macro-expansion time. If it is non-nil, diff --git a/compat-tests.el b/compat-tests.el index 5d70ac5..8bfc502 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -3012,6 +3012,14 @@ (with-temp-buffer (should-equal (take 3 (widget-create 'key)) '(key :value "")))) +(ert-deftest compat-hash-table-contains-p () + (let ((h (make-hash-table :test #'equal))) + (puthash :foo t h) + (should (hash-table-contains-p :foo h)) + (should-not (hash-table-contains-p :bar h)) + (should-not (hash-table-contains-p 'missing h)) + (should-not (hash-table-contains-p '#:missing h)))) + (ert-deftest compat-copy-tree () ;; Adapted from Emacs /test/lisp/subr-tests.el ;; Check that values other than conses, vectors and records are diff --git a/compat.texi b/compat.texi index 2b81e7e..d9f9358 100644 --- a/compat.texi +++ b/compat.texi @@ -3452,6 +3452,11 @@ older than 31.1. Note that due to upstream changes, it might happen that there will be the need for changes, so use these functions with care. +@c based on lisp/subr.el +@defmac hash-table-contains-p key table +Return non-nil if @var{table} has an element with @var{key}. +@end defmac + @c copied from lispref/control.texi @defmac static-when condition body... Test @var{condition} at macro-expansion time. If its value is -- cgit v1.0