bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

add-to-plist, add-plist-to-plist functions


From: Karl Chen
Subject: add-to-plist, add-plist-to-plist functions
Date: Sun, 28 Apr 2002 16:26:56 -0700

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






reply via email to

[Prev in Thread] Current Thread [Next in Thread]