mu4e-helpers: tweak plist filtering

A little refactoring
This commit is contained in:
Dirk-Jan C. Binnema
2025-04-17 00:11:50 +03:00
parent e79874aafb
commit faea3c35ad

View File

@ -588,37 +588,30 @@ Mu4e's version of Emacs 28's `string-replace'."
(defun mu4e-plistp (object) (defun mu4e-plistp (object)
"Non-nil if and only if OBJECT is a valid plist. "Non-nil if and only if OBJECT is a valid plist.
This is mu4e's version of Emacs 29's `plistp'." This is mu4e's version of Emacs 29's `plistp'."
(let ((len (proper-list-p object))) (let ((len (proper-list-p object)))
(and len (zerop (% len 2))))) (and len (zerop (% len 2)))))
(defun mu4e-plist-do (func plist) (defun mu4e-plist-filter (plist pred)
"Apply FUNC to each element in PLIST. "Return a plist for which a call to pred is non-nil.
FUNC receives to arguments: the key and its value." PLIST is some plist, and PRED is a function taking a key and
(when plist value."
(funcall func (car plist) (cadr plist)) (let (p)
(mu4e-plist-do func (cddr plist)))) (while plist
(let ((key (car plist)) (val (cadr plist)))
(when (funcall pred key val)
(setq p (plist-put p key val))))
(setq plist (cddr plist)))
p))
(defun mu4e-plist-remove (plist prop) (defun mu4e-plist-remove (plist prop)
"Remove PROP from PLIST. "Remove PROP from PLIST.
Returns the updated PLIST." Returns the updated PLIST."
;; inspired by org-plist-delete (mu4e-plist-filter plist (lambda (k _v) (not (eq k prop)))))
(let (p)
(while plist
(if (not (eq prop (car plist)))
(setq p (plist-put p (car plist) (nth 1 plist))))
(setq plist (cddr plist)))
p))
(defun mu4e-plist-remove-nils (plist) (defun mu4e-plist-remove-nils (plist)
"Remove all properties with value nil from PLIST." "Remove all properties with value nil from PLIST."
(let (p) (mu4e-plist-filter plist (lambda (_k v) v)))
(while plist
(when (cadr plist)
(setq p (plist-put p (car plist) (cadr plist))))
(setq plist (cddr plist)))
p))
(defun mu4e-plist-put-many (plist &rest keyvals) (defun mu4e-plist-put-many (plist &rest keyvals)
"Like `plist-put', but allow for multiple key-value pairs. "Like `plist-put', but allow for multiple key-value pairs.