1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Use 'hash-table-contains-p' in a few places

This replaces open coded versions of the common idiom
    (not (eq (gethash key table 'missing) 'missing))
with
    (hash-table-contains-p key table)
in files where we can rely on features in Emacs 31.

* lisp/emacs-lisp/map.el (map-contains-key):
* lisp/external-completion.el (external-completion-table):
* lisp/mh-e/mh-utils.el (mh-sub-folders)
(mh-remove-from-sub-folders-cache):
* lisp/net/ange-ftp.el (ange-ftp-hash-entry-exists-p):
* lisp/password-cache.el (password-in-cache-p, password-cache-add):
* lisp/pcmpl-x.el (pcmpl-x-tlmgr-action-options):
* lisp/xdg.el (xdg-mime-apps): Use 'hash-table-contains-p'.
This commit is contained in:
Stefan Kangas 2025-03-29 13:50:21 +01:00
parent dd0dd87e3a
commit f60fc1287d
7 changed files with 16 additions and 22 deletions

View file

@ -403,8 +403,7 @@ If MAP is a plist, TESTFN defaults to `eq'."
(cl-defmethod map-contains-key ((map hash-table) key &optional _testfn) (cl-defmethod map-contains-key ((map hash-table) key &optional _testfn)
"Return non-nil if MAP contains KEY, ignoring TESTFN." "Return non-nil if MAP contains KEY, ignoring TESTFN."
(let ((v '(nil))) (hash-table-contains-p key map))
(not (eq v (gethash key map v)))))
(cl-defgeneric map-some (pred map) (cl-defgeneric map-some (pred map)
"Return the first non-nil value from applying PRED to elements of MAP. "Return the first non-nil value from applying PRED to elements of MAP.

View file

@ -117,11 +117,10 @@ EXPANDED-PATTERN."
completion-category-defaults))) completion-category-defaults)))
(let ((cache (make-hash-table :test #'equal))) (let ((cache (make-hash-table :test #'equal)))
(cl-flet ((lookup-internal (string point) (cl-flet ((lookup-internal (string point)
(let* ((key (cons string point)) (let ((key (cons string point)))
(probe (gethash key cache 'external--notfound))) (if (hash-table-contains-p key cache)
(if (eq probe 'external--notfound) (gethash key cache)
(puthash key (funcall lookup string point) cache) (puthash key (funcall lookup string point) cache)))))
probe))))
(lambda (string pred action) (lambda (string pred action)
(pcase action (pcase action
(`metadata (`metadata

View file

@ -528,11 +528,10 @@ nested folders within them."
(let* ((folder (mh-normalize-folder-name folder nil (let* ((folder (mh-normalize-folder-name folder nil
(string= folder "+/") (string= folder "+/")
t)) t))
(match (gethash folder mh-sub-folders-cache 'no-result)) (sub-folders (if (hash-table-contains-p folder mh-sub-folders-cache)
(sub-folders (cond ((eq match 'no-result) (gethash folder mh-sub-folders-cache)
(setf (gethash folder mh-sub-folders-cache) (setf (gethash folder mh-sub-folders-cache)
(mh-sub-folders-actual folder))) (mh-sub-folders-actual folder)))))
(t match))))
(if add-trailing-slash-flag (if add-trailing-slash-flag
(mapcar (lambda (x) (mapcar (lambda (x)
(if (cdr x) (cons (concat (car x) "/") (cdr x)) x)) (if (cdr x) (cons (concat (car x) "/") (cdr x)) x))
@ -629,7 +628,7 @@ otherwise completion on +foo won't tell us about the option
last-slash) last-slash)
(while (setq last-slash (mh-search-from-end ?/ parent)) (while (setq last-slash (mh-search-from-end ?/ parent))
(setq parent (substring parent 0 last-slash)) (setq parent (substring parent 0 last-slash))
(unless (eq (gethash parent mh-sub-folders-cache 'none) 'none) (when (hash-table-contains-p parent mh-sub-folders-cache)
(remhash parent mh-sub-folders-cache) (remhash parent mh-sub-folders-cache)
(if one-ancestor-found (if one-ancestor-found
(cl-return-from ancestor-found) (cl-return-from ancestor-found)

View file

@ -1004,7 +1004,7 @@ or nil meaning don't change it."
(defun ange-ftp-hash-entry-exists-p (key tbl) (defun ange-ftp-hash-entry-exists-p (key tbl)
"Return whether there is an association for KEY in table TBL." "Return whether there is an association for KEY in table TBL."
(and tbl (not (eq (gethash key tbl 'unknown) 'unknown)))) (and tbl (hash-table-contains-p key tbl)))
(defun ange-ftp-hash-table-keys (tbl) (defun ange-ftp-hash-table-keys (tbl)
"Return a sorted list of all the active keys in table TBL, as strings." "Return a sorted list of all the active keys in table TBL, as strings."

View file

@ -82,8 +82,7 @@ regulate cache behavior."
"Check if KEY is in the cache." "Check if KEY is in the cache."
(and password-cache (and password-cache
key key
(not (eq (gethash key password-data 'password-cache-no-data) (hash-table-contains-p key password-data)))
'password-cache-no-data))))
(defun password-read (prompt &optional key) (defun password-read (prompt &optional key)
"Read password, for use with KEY, from user, or from cache if wanted. "Read password, for use with KEY, from user, or from cache if wanted.
@ -110,8 +109,7 @@ user again."
"Add password to cache. "Add password to cache.
The password is removed by a timer after `password-cache-expiry' seconds." The password is removed by a timer after `password-cache-expiry' seconds."
(when (and password-cache-expiry (when (and password-cache-expiry
(eq (gethash key password-data 'password-cache-no-data) (not (hash-table-contains-p key password-data)))
'password-cache-no-data))
(run-at-time password-cache-expiry nil (run-at-time password-cache-expiry nil
#'password-cache-remove #'password-cache-remove
key)) key))

View file

@ -121,7 +121,7 @@
(defun pcmpl-x-tlmgr-action-options (action) (defun pcmpl-x-tlmgr-action-options (action)
"Get the list of long options for ACTION." "Get the list of long options for ACTION."
(if (eq (gethash action pcmpl-x-tlmgr-options-cache 'missing) 'missing) (if (not (hash-table-contains-p action pcmpl-x-tlmgr-options-cache))
(with-temp-buffer (with-temp-buffer
(when (zerop (when (zerop
(call-process pcmpl-x-tlmgr-program nil t nil action "-h")) (call-process pcmpl-x-tlmgr-program nil t nil action "-h"))

View file

@ -384,9 +384,8 @@ Results are cached in `xdg-mime-table'."
(setq xdg-mime-table nil))) (setq xdg-mime-table nil)))
(when (null (assoc type xdg-mime-table)) (when (null (assoc type xdg-mime-table))
(push (cons type (make-hash-table :test #'equal)) xdg-mime-table)) (push (cons type (make-hash-table :test #'equal)) xdg-mime-table))
(if (let ((def (make-symbol "def")) (if (let ((table (cdr (assoc type xdg-mime-table))))
(table (cdr (assoc type xdg-mime-table)))) (hash-table-contains-p subtype table))
(not (eq (setq files (gethash subtype table def)) def)))
files files
(and files (setq files nil)) (and files (setq files nil))
(let ((dirs (mapcar (lambda (dir) (expand-file-name "applications" dir)) (let ((dirs (mapcar (lambda (dir) (expand-file-name "applications" dir))