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

* Re: add-to-plist, add-plist-to-plist functions
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Miles Bader @ 2002-04-28 23:38 UTC (permalink / raw)


"Karl Chen" <quarl.newsgroup@REMOVE_ME_quarl.org> writes:
> Hi, I wrote these two functions which may be useful to others:

Your terminology is incorrect -- looking at your code, it seems to be
manipulating `alists', not `plists'.

[An alist is a list of cons cells, with the car of each being the key,
and the cdr the value; a plist is a list of alternating keys and values.]

-Miles
-- 
Run away!  Run away!

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

* Re: add-to-plist, add-plist-to-plist functions
  2002-04-28 23:38 ` Miles Bader
@ 2002-04-28 23:45   ` Karl Chen
  0 siblings, 0 replies; 3+ messages in thread
From: Karl Chen @ 2002-04-28 23:45 UTC (permalink / raw)


Oops! You're right, thanks. Here's the same code with names changed.

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

If the element is a cons:
  If there exists an element in the value of ALIST-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 ((alist (symbol-value alist-var))
            (pname (car p)))
        (when pname
          (while alist
            (let ((p0 (car alist)))
              (setq alist (cdr alist))
              (when (and (consp p0) (eq (car p0) pname))
                (setcdr p0 (cdr p))
            (setq alist nil)
            (setq pname nil))))
          (when pname
            (set alist-var (cons p (symbol-value alist-var)))
            )))
    (add-to-list alist-var p)))

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


"Miles Bader" <miles@gnu.org> wrote in message
news:877kmr2y2c.fsf@tc-1-100.kawasaki.gol.ne.jp...
> "Karl Chen" <quarl.newsgroup@REMOVE_ME_quarl.org> writes:
> > Hi, I wrote these two functions which may be useful to others:
>
> Your terminology is incorrect -- looking at your code, it seems to be
> manipulating `alists', not `plists'.
>
> [An alist is a list of cons cells, with the car of each being the key,
> and the cdr the value; a plist is a list of alternating keys and values.]
>
> -Miles
> --
> Run away!  Run away!

^ 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).