Initial Commit
[packages] / xemacs-packages / hyperbole / kotl / knode.el
1 ;;; knode.el --- Generic nodes for use as elements in data structures.
2
3 ;; Copyright (C) 1993-1995, Free Software Foundation, Inc.
4 ;; Developed with support from Motorola Inc.
5
6 ;; Author: Bob Weiner, Brown U.
7 ;;         Kellie Clark
8 ;; Maintainer: Mats Lidell <matsl@contactor.se>
9 ;; Keywords: extensions, hypermedia, outlines
10
11 ;; This file is part of GNU Hyperbole.
12
13 ;; GNU Hyperbole is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 3, or (at
16 ;; your option) any later version.
17
18 ;; GNU Hyperbole is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 ;;;
33 ;;; Public functions
34 ;;;
35
36 ;;;
37 ;;; Knodes
38 ;;;
39
40 (defun knode:create (contents &optional prop-list)
41   "Return a new knode which stores CONTENTS and optional PROP-LIST."
42   (list   'knode
43           'contents contents
44           'plist prop-list))
45
46 (defun knode:contents (knode)
47    "Return KNODE's contents."
48    (if (knode:is-p knode)
49        (car (cdr (memq 'contents knode)))
50      (error "(knode:contents): Argument must be a knode.")))
51
52 (fset 'knode:copy 'copy-tree)
53
54 (defun knode:is-p (object)
55   "Is OBJECT a knode?"
56   (and (listp object) (eq (car object) 'knode)))
57
58 (defun knode:set-contents (knode contents)
59   "Set KNODE's CONTENTS."
60   (if (knode:is-p knode)
61       (setcar (cdr (memq 'contents knode)) contents)
62     (error "(knode:set-contents): First arg must be a knode.")))
63
64 ;;;
65 ;;; Private functions
66 ;;;
67
68 (defun knode:get-attr (obj attribute)
69   "Return the value of OBJECT's ATTRIBUTE."
70   (car (cdr (memq attribute obj))))
71
72 (defun knode:remove-attr (obj attribute)
73   "Remove OBJECT's ATTRIBUTE, if any, and return modified OBJECT.
74 Use (setq object (knode:remove-attr object attribute)) to ensure that OBJECT
75 is updated."
76   (let ((tail obj)
77         sym
78         prev)
79     (setq sym (car tail))
80     (while (and sym (eq sym attribute))
81       (setq tail (cdr (cdr tail))
82             sym (car tail)))
83     (setq obj tail
84           prev tail
85           tail (cdr (cdr tail)))
86     (while tail
87       (setq sym (car tail))
88       (if (eq sym attribute)
89           (setcdr (cdr prev) (cdr (cdr tail))))
90       (setq prev tail
91             tail (cdr (cdr tail))))
92     obj))
93
94 (defun knode:set-attr (obj attribute value)
95   "Set OBJECT's ATTRIBUTE to VALUE and return OBJECT."
96   (let ((attr (memq attribute obj)))
97     (if attr
98         (setcar (cdr attr) value)
99       (setq obj (nconc obj (list attribute value)))))
100   obj)
101
102 ;;;
103 ;;; Private variables
104 ;;;
105
106 (provide 'knode)
107
108 ;;; knode.el ends here