1 /* Main types and definitions
2 
3    Copyright (c) 1997-2014 Free Software Foundation, Inc.
4 
5    This file is part of GNU Zile.
6 
7    GNU Zile is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11 
12    GNU Zile is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with GNU Zile; see the file COPYING.  If not, write to the
19    Free Software Foundation, Fifth Floor, 51 Franklin Street, Boston,
20    MA 02111-1301, USA.  */
21 
22 #ifndef ZILE_H
23 #define ZILE_H
24 
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <limits.h>
28 #include <string.h>
29 #include <gc/gc.h>
30 #include "xalloc.h"
31 #include "xvasprintf.h"
32 #include "size_max.h"
33 #include "minmax.h"
34 #include "hash.h"
35 #include "gl_xlist.h"
36 #include "unused-parameter.h"
37 
38 #include "astr.h"
39 #include "estr.h"
40 #include "lists.h"
41 
42 #define ZILE_VERSION_STRING	"GNU " PACKAGE_NAME " " VERSION
43 
44 /*--------------------------------------------------------------------------
45  * Main editor structures.
46  *--------------------------------------------------------------------------*/
47 
48 /* Opaque types. */
49 typedef struct Region *Region;
50 typedef struct Marker *Marker;
51 typedef struct History *History;
52 typedef struct Undo *Undo;
53 typedef struct Macro *Macro;
54 typedef struct Binding *Binding;
55 typedef struct Buffer *Buffer;
56 typedef struct Window *Window;
57 typedef struct Completion *Completion;
58 
59 typedef enum
60 {
61   completion_notmatched,
62   completion_matched,
63   completion_matchednonunique,
64   completion_nonunique,
65 } completion_code;
66 
67 /* Completion flags */
68 #define CFLAG_POPPEDUP	0000001	/* Completion window has been popped up. */
69 #define CFLAG_CLOSE	0000002	/* The completion window should be closed. */
70 #define CFLAG_FILENAME	0000004	/* This is a filename completion. */
71 
72 /*--------------------------------------------------------------------------
73  * Object field getter and setter generators.
74  *--------------------------------------------------------------------------*/
75 
76 #define GETTER(Obj, name, ty, field)            \
77   _GL_ATTRIBUTE_PURE ty                         \
78   get_ ## name ## _ ## field (const Obj p)      \
79   {                                             \
80     return p->field;                            \
81   }                                             \
82 
83 #define SETTER(Obj, name, ty, field)            \
84   void                                          \
85   set_ ## name ## _ ## field (Obj p, ty field)  \
86   {                                             \
87     p->field = field;                           \
88   }
89 
90 #define STR_SETTER(Obj, name, field)                            \
91   void                                                          \
92   set_ ## name ## _ ## field (Obj p, const char *field)         \
93   {                                                             \
94     p->field = field ? xstrdup (field) : NULL;                  \
95   }
96 
97 /*--------------------------------------------------------------------------
98  * Zile commands to C bindings.
99  *--------------------------------------------------------------------------*/
100 
101 /*
102  * The type of a Zile exported function.
103  * `uniarg' is the universal argument, if any, whose presence is
104  * indicated by `is_uniarg'.
105  */
106 typedef le * (*Function) (long uniarg, bool is_uniarg, le * list);
107 
108 /* Turn a bool into a Lisp boolean */
109 #define bool_to_lisp(b) ((b) ? leT : leNIL)
110 
111 /* Define an interactive function. */
112 #define DEFUN(zile_func, c_func) \
113   DEFUN_ARGS(zile_func, c_func, )
114 #define DEFUN_ARGS(zile_func, c_func, args) \
115   le * F_ ## c_func (long uniarg _GL_UNUSED_PARAMETER, bool is_uniarg _GL_UNUSED_PARAMETER, le *arglist _GL_UNUSED_PARAMETER) \
116   {                                                                     \
117     le * ok = leT;                                                      \
118     args
119 #define END_DEFUN    \
120     return ok;       \
121   }
122 
123 /* Define a non-user-visible function. */
124 #define DEFUN_NONINTERACTIVE(zile_func, c_func) \
125   DEFUN(zile_func, c_func)
126 #define DEFUN_NONINTERACTIVE_ARGS(zile_func, c_func, args) \
127   DEFUN_ARGS(zile_func, c_func, args)
128 
129 /* String argument. */
130 #define STR_ARG(name)                           \
131   const_astr name = NULL;
132 #define STR_INIT(name)                                  \
133   if (arglist && arglist->next)                         \
134     {                                                   \
135       if (arglist->next->data)                          \
136         name = astr_new_cstr (arglist->next->data);     \
137       arglist = arglist->next;                          \
138     }
139 
140 /* Integer argument. */
141 #define INT_ARG(name) \
142   long name = 1;
143 #define INT_INIT(name) \
144   if (arglist && arglist->next)                 \
145     {                                           \
146       const char *s = arglist->next->data;      \
147       arglist = arglist->next;                  \
148       if (s != NULL)                            \
149         name = strtol (s, NULL, 10);            \
150       if (name == LONG_MAX)                     \
151         ok = leNIL;                             \
152     }
153 
154 /* Integer argument which can either be argument or uniarg. */
155 #define INT_OR_UNIARG(name) \
156   long name = 1;            \
157   _GL_UNUSED bool noarg = false;
158 #define INT_OR_UNIARG_INIT(name)                             \
159   INT_INIT (name)                                            \
160   else                                                       \
161     {                                                        \
162       if (!(lastflag & FLAG_SET_UNIARG) && !is_uniarg &&     \
163           (arglist == NULL || arglist->next == NULL))        \
164         noarg = true;                                        \
165       name = uniarg;                                         \
166     }
167 
168 /* Boolean argument. */
169 #define BOOL_ARG(name)                          \
170   bool name = true;
171 #define BOOL_INIT(name)                         \
172   if (arglist && arglist->next)                 \
173     {                                           \
174       const char *s = arglist->next->data;      \
175       arglist = arglist->next;                  \
176       if (s != NULL && STREQ (s, "nil"))        \
177         name = false;                           \
178     }
179 
180 /* Call an interactive function. */
181 #define FUNCALL(c_func)                         \
182   F_ ## c_func (1, false, leNIL)
183 
184 /* Call an interactive function with a universal argument. */
185 #define FUNCALL_ARG(c_func, uniarg)             \
186   F_ ## c_func (uniarg, true, leNIL)
187 
188 /*--------------------------------------------------------------------------
189  * Keyboard handling.
190  *--------------------------------------------------------------------------*/
191 
192 /* Standard pauses in ds */
193 #define GETKEY_DEFAULT                  -1
194 #define GETKEY_DELAYED                  2000
195 
196 /* Special value returned for invalid key codes, or when no key is pressed. */
197 #define KBD_NOKEY                       SIZE_MAX
198 
199 /* Key modifiers. */
200 #define KBD_CTRL                        01000
201 #define KBD_META                        02000
202 
203 /* Common non-alphanumeric keys. */
204 #define KBD_CANCEL                      (KBD_CTRL | 'g')
205 #define KBD_TAB                         00402
206 #define KBD_RET                         00403
207 #define KBD_PGUP                        00404
208 #define KBD_PGDN                        00405
209 #define KBD_HOME                        00406
210 #define KBD_END                         00407
211 #define KBD_DEL                         00410
212 #define KBD_BS                          00411
213 #define KBD_INS                         00412
214 #define KBD_LEFT                        00413
215 #define KBD_RIGHT                       00414
216 #define KBD_UP                          00415
217 #define KBD_DOWN                        00416
218 #define KBD_F1                          00420
219 #define KBD_F2                          00421
220 #define KBD_F3                          00422
221 #define KBD_F4                          00423
222 #define KBD_F5                          00424
223 #define KBD_F6                          00425
224 #define KBD_F7                          00426
225 #define KBD_F8                          00427
226 #define KBD_F9                          00430
227 #define KBD_F10                         00431
228 #define KBD_F11                         00432
229 #define KBD_F12                         00433
230 
231 /*--------------------------------------------------------------------------
232  * Miscellaneous stuff.
233  *--------------------------------------------------------------------------*/
234 
235 /* Global flags, stored in thisflag and lastflag. */
236 #define FLAG_NEED_RESYNC	0001	/* A resync is required. */
237 #define FLAG_QUIT		0002	/* The user has asked to quit. */
238 #define FLAG_SET_UNIARG		0004	/* The last command modified the
239                                            universal arg variable `uniarg'. */
240 #define FLAG_UNIARG_EMPTY	0010	/* Current universal arg is just C-u's
241                                            with no number. */
242 #define FLAG_DEFINING_MACRO	0020	/* We are defining a macro. */
243 
244 /* Zile font codes */
245 #define FONT_NORMAL		0000
246 #define FONT_REVERSE		0001
247 #define FONT_UNDERLINE		0002
248 
249 /* Custom exit code */
250 #define EXIT_CRASH	2
251 
252 
253 /*--------------------------------------------------------------------------
254  * Things that should really be in gnulib.
255  *--------------------------------------------------------------------------*/
256 
257 /* String comparison */
258 #define STREQ(a, b) (strcmp (a, b) == 0)
259 
260 #endif /* !ZILE_H */
261