Move src/objects.c to src/ui
[sxemacs] / src / ui / widget.c
1 /* Primitives for work of the "widget" library.
2    Copyright (C) 1997 Free Software Foundation, Inc.
3
4 This file is part of SXEmacs
5
6 SXEmacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 SXEmacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19
20 /* Synched up with: Not in FSF. */
21
22 /* In an ideal world, this file would not have been necessary.
23    However, elisp function calls being as slow as they are, it turns
24    out that some functions in the widget library (wid-edit.el) are the
25    bottleneck of Widget operation.  Here is their translation to C,
26    for the sole reason of efficiency.  */
27
28 #include <config.h>
29 #include "lisp.h"
30 #include "buffer.h"
31
32 Lisp_Object Qwidget_type;
33
34 DEFUN("widget-plist-member", Fwidget_plist_member, 2, 2, 0,     /*
35 Like `plist-get', but returns the tail of PLIST whose car is PROP.
36 */
37       (plist, prop))
38 {
39         while (!NILP(plist) && !EQ(Fcar(plist), prop)) {
40                 /* Check for QUIT, so a circular plist doesn't lock up the
41                    editor. */
42                 QUIT;
43                 plist = Fcdr(Fcdr(plist));
44         }
45         return plist;
46 }
47
48 DEFUN("widget-put", Fwidget_put, 3, 3, 0,       /*
49 In WIDGET set PROPERTY to VALUE.
50 The value can later be retrieved with `widget-get'.
51 */
52       (widget, property, value))
53 {
54         CHECK_CONS(widget);
55         XCDR(widget) = Fplist_put(XCDR(widget), property, value);
56         return widget;
57 }
58
59 DEFUN("widget-get", Fwidget_get, 2, 2, 0,       /*
60 In WIDGET, get the value of PROPERTY.
61 The value could either be specified when the widget was created, or
62 later with `widget-put'.
63 */
64       (widget, property))
65 {
66         Lisp_Object value = Qnil;
67
68         while (1) {
69                 Lisp_Object tmp = Fwidget_plist_member(Fcdr(widget), property);
70                 if (!NILP(tmp)) {
71                         value = Fcar(Fcdr(tmp));
72                         break;
73                 }
74                 tmp = Fcar(widget);
75                 if (!NILP(tmp)) {
76                         widget = Fget(tmp, Qwidget_type, Qnil);
77                         continue;
78                 }
79                 break;
80         }
81         return value;
82 }
83
84 DEFUN("widget-apply", Fwidget_apply, 2, MANY, 0,        /*
85 Apply the value of WIDGET's PROPERTY to the widget itself.
86 ARGS are passed as extra arguments to the function.
87 */
88       (int nargs, Lisp_Object * args))
89 {
90         /* This function can GC */
91         Lisp_Object newargs[3];
92         struct gcpro gcpro1;
93
94         newargs[0] = Fwidget_get(args[0], args[1]);
95         newargs[1] = args[0];
96         newargs[2] = Flist(nargs - 2, args + 2);
97         GCPRO1(newargs[2]);
98         RETURN_UNGCPRO(Fapply(3, newargs));
99 }
100
101 void syms_of_widget(void)
102 {
103         defsymbol(&Qwidget_type, "widget-type");
104
105         DEFSUBR(Fwidget_plist_member);
106         DEFSUBR(Fwidget_put);
107         DEFSUBR(Fwidget_get);
108         DEFSUBR(Fwidget_apply);
109 }