unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* add-to-plist, add-plist-to-plist functions
@ 2002-04-28 23:26 Karl Chen
  2002-04-28 23:38 ` Miles Bader
  0 siblings, 1 reply; 3+ messages in thread
From: Karl Chen @ 2002-04-28 23:26 UTC (permalink / raw)


Hi, I wrote these two functions which may be useful to others:

(defun add-to-plist (plist-var p)
  "Add an element to a plist symbol.

If the element is a cons:
  If there exists an element in the value of plist-var whose car matches the
new element's car, replace its cdr. Else prepend the whole element to the
list.

If the element is not a cons:
  If the element does not already exist, prepend it. (Equivalent to
`add-to-list' in this case.)"
  (if (consp p)
      (let ((plist (symbol-value plist-var))
            (pname (car p)))
        (when pname
          (while plist
            (let ((p0 (car plist)))
              (setq plist (cdr plist))
              (when (and (consp p0) (eq (car p0) pname))
                (setcdr p0 (cdr p))
            (setq plist nil)
            (setq pname nil))))
          (when pname
            (set plist-var (cons p (symbol-value plist-var)))
            )))
    (add-to-list plist-var p)))

(defun add-plist-to-plist (plist new-plist)
  "Apply NEW-PLIST on PLIST by calling `add-to-plist' on each entry."
  (while new-plist
    (add-to-plist 'plist (car new-plist))
    (setq new-plist (cdr new-plist))
    )
  plist)

--
Karl Chen

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-04-28 23:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-04-28 23:26 add-to-plist, add-plist-to-plist functions Karl Chen
2002-04-28 23:38 ` Miles Bader
2002-04-28 23:45   ` Karl Chen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).