core/autoload/mplist: document

This commit is contained in:
Henrik Lissner 2017-03-01 22:25:13 -05:00
parent 978e121ed0
commit d4e25d3f0d

View file

@ -2,18 +2,18 @@
(provide 'doom-lib-mplist)
;; The built-in plist library won't work when the list is a malformed plist,
;; which is a plist who has multiple (or no) forms following its properties. For
;; example: (:x 5 :y 1 2 3 :z).
;; which is a plist that has multiple (or no) forms following its properties.
;; For example: (:x 5 :y 1 2 3 :z).
;;
;; This library was made for mplists, so use the standard plist-* functions for
;; regular plists.
;; This library was written for mplists, but use the standard plist-* functions
;; for regular plists -- they're faster.
;;
;; (let ((a '(abc :commands 1 :config 3 4 5)))
;; (mplist! a &delete :commands :config))
;;;###autoload
(defmacro mplist! (var action &rest args)
"A helper macro that makes dealing with doom-mplist-* functions a little more concise.
"A convenience macro for dealing with doom-mplist-* functions.
Examples:
(mplist! plist &delete :x :y)
@ -26,40 +26,43 @@ Examples:
`(,fn-sym ,var ,@args)))))
;;;###autoload
(defun doom-mplist-delete (plist &rest keys)
"TODO"
(if (apply 'doom-mplist-member plist keys)
(defun doom-mplist-delete (mplist &rest keys)
"Thoroughly removes a property and its values from an mplist."
(if (apply 'doom-mplist-member mplist keys)
(let (forms)
(while plist
(if (not (and (keywordp (car plist)) (memq (car plist) keys)))
(setq forms (append forms (list (pop plist))))
(pop plist)
(while (and plist (not (keywordp (car plist))))
(pop plist))))
(while mplist
(if (not (and (keywordp (car mplist)) (memq (car mplist) keys)))
(setq forms (append forms (list (pop mplist))))
(pop mplist)
(while (and mplist (not (keywordp (car mplist))))
(pop mplist))))
forms)
plist))
mplist))
;;;###autoload
(defun doom-mplist-put (plist key value)
"TODO"
(when (doom-mplist-member plist key)
(doom-mplist-delete plist key))
(setq plist (append plist (list key value))))
(defun doom-mplist-put (mplist key value)
"Like `plist-put', but for mplists."
(when (doom-mplist-member mplist key)
(doom-mplist-delete mplist key))
(setq mplist (append mplist (list key value))))
;;;###autoload
(defun doom-mplist-get (plist &rest keys)
"TODO"
(defun doom-mplist-get (mplist &rest keys)
"Like `plist-get', but for mplists. If KEYS is only one keyword, return the
mplist associated with it. If KEYS is multiple, return a list of associated
mplists."
(if (cdr keys)
(let ((keys (mapcar (lambda (key) (memq key plist)) keys))
(let ((keys (mapcar (lambda (key) (memq key mplist)) keys))
values)
(dolist (forms keys)
(when forms
(push (cdr forms) values)))
values)
(cdr (memq (car keys) plist))))
(cdr (memq (car keys) mplist))))
;;;###autoload
(defun doom-mplist-member (plist &rest keys)
(cl-some (lambda (key) (memq key plist)) keys))
(defun doom-mplist-member (mplist &rest keys)
"Return t if any of KEYS (keywords) are in MPLIST."
(cl-some (lambda (key) (memq key mplist)) keys))