Fix openssl support
[sxemacs] / src / extents.h
1 /* Copyright (c) 1994, 1995 Free Software Foundation.
2    Copyright (c) 1995 Ben Wing.
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 #ifndef INCLUDED_extents_h_
23 #define INCLUDED_extents_h_
24
25 DECLARE_LRECORD(extent, struct extent);
26 #define XEXTENT(x) XRECORD (x, extent, struct extent)
27 #define XSETEXTENT(x, p) XSETRECORD (x, p, extent)
28 #define EXTENTP(x) RECORDP (x, extent)
29 #define CHECK_EXTENT(x) CHECK_RECORD (x, extent)
30 #define CONCHECK_EXTENT(x) CONCHECK_RECORD (x, extent)
31
32 /* the layouts for glyphs (extent->flags.glyph_layout).  Must fit in 2 bits. */
33 typedef enum glyph_layout {
34         GL_TEXT,
35         GL_OUTSIDE_MARGIN,
36         GL_INSIDE_MARGIN,
37         GL_WHITESPACE
38 } glyph_layout;
39
40 struct extent {
41         struct lrecord_header lheader;
42
43         Memind start;
44         Memind end;
45         Lisp_Object object;     /* A buffer, string, Qnil (extent detached from no
46                                    buffer), Qt (destroyed extent) */
47
48         /* Extent properties are conceptually a plist, but the most common
49            props are implemented as bits instead of conses.  */
50         struct {
51                 Lisp_Object face;
52
53                 /* These flags are simply an optimization for common
54                    boolean properties which go onto the extent's
55                    property list.  Any of them would work if done in
56                    the normal way, but the space savings of doing
57                    these in this way is significant.  Note that if you
58                    add a flag, there are numerous places in extents.c
59                    that need to know about it.
60
61                    Another consideration is that some of these
62                    properties are accessed during redisplay, so it's
63                    good for access to them to be fast (a bit reference
64                    instead of a search down a plist).
65
66                    `begin_glyph_layout' and `end_glyph_layout' are
67                    unusual in that they have 4 states instead of 2.
68
69                    Other special extent properties are stored in an
70                    auxiliary structure that sits at the beginning of
71                    the plist.  The has_aux flag indicates whether this
72                    structure exists.  The has_parent flag is an
73                    optimization indicating whether the extent has a
74                    parent (this could also be determined by looking in
75                    the aux structure). */
76
77                  enum_field(glyph_layout) begin_glyph_layout:2;
78                 /*  2 text, margins, or whitespace */
79                  enum_field(glyph_layout) end_glyph_layout:2;
80                 /*  4 text, margins, or whitespace */
81                 unsigned int has_parent:1;      /*  5 extent has a parent          */
82                 unsigned int has_aux:1; /*  6 extent has an aux. structure */
83                 unsigned int start_open:1;      /*  7 insertion behavior at start  */
84                 unsigned int end_open:1;        /*  8 insertion behavior at end    */
85                 unsigned int unique:1;  /*  9 there may be only one attached  */
86                 unsigned int duplicable:1;      /* 10 copied to strings by kill/undo  */
87                 unsigned int detachable:1;      /* 11 extent detaches if text deleted */
88                 unsigned int internal:1;        /* 12 used by map-extents etc.        */
89                 unsigned int in_red_event:1;    /* 13 An event has been spawned for
90                                                    initial redisplay.
91                                                    (not exported to lisp) */
92                 unsigned int unused16:1;        /* 16 unused bits                   */
93                 /* --- Adding more flags will cause the extent struct to grow by another
94                    word.  It's not clear that this would make a difference, however,
95                    because on 32-bit machines things tend to get allocated in chunks
96                    of 4 bytes. */
97         } flags;
98         /* The plist may have an auxiliary structure as its first element */
99         Lisp_Object plist;
100 };
101
102 /* Basic properties of an extent (not affected by the extent's parent) */
103 #define extent_object(e) ((e)->object)
104 #define extent_start(e) ((e)->start + 0)
105 #define extent_end(e) ((e)->end + 0)
106 #define set_extent_start(e, val) ((void) ((e)->start = (val)))
107 #define set_extent_end(e, val) ((void) ((e)->end = (val)))
108 #define extent_endpoint(e, endp) ((endp) ? extent_end (e) : extent_start (e))
109 #define set_extent_endpoint(e, val, endp) \
110   ((endp) ? set_extent_end (e, val) : set_extent_start (e, val))
111 #define extent_detached_p(e) (extent_start (e) < 0)
112
113 /* Additional information that may be present in an extent.  The idea is
114    that fast access is provided to this information, but since (hopefully)
115    most extents won't have this set on them, we usually don't need to
116    have this structure around and thus the size of an extent is smaller. */
117
118 typedef struct extent_auxiliary extent_auxiliary;
119 struct extent_auxiliary {
120         struct lcrecord_header header;
121
122         Lisp_Object begin_glyph;
123         Lisp_Object end_glyph;
124         Lisp_Object parent;
125         /* We use a weak list here.  Originally I didn't do this and
126            depended on having the extent's finalization method remove
127            itself from its parent's children list.  This runs into
128            lots and lots of problems though because everything is in
129            a really really bizarre state when an extent's finalization
130            method is called (it happens in sweep_extents() by way of
131            ADDITIONAL_FREE_extent()) and it's extremely difficult to
132            avoid getting hosed by just-freed objects. */
133         Lisp_Object children;
134         Lisp_Object invisible;
135         Lisp_Object read_only;
136         Lisp_Object mouse_face;
137         Lisp_Object initial_redisplay_function;
138         Lisp_Object before_change_functions, after_change_functions;
139         int priority;
140 };
141
142 extern struct extent_auxiliary extent_auxiliary_defaults;
143
144 DECLARE_LRECORD(extent_auxiliary, struct extent_auxiliary);
145 #define XEXTENT_AUXILIARY(x) \
146   XRECORD (x, extent_auxiliary, struct extent_auxiliary)
147 #define XSETEXTENT_AUXILIARY(x, p) XSETRECORD (x, p, extent_auxiliary)
148 #define EXTENT_AUXILIARYP(x) RECORDP (x, extent_auxiliary)
149 #define CHECK_EXTENT_AUXILIARY(x) CHECK_RECORD (x, extent_auxiliary)
150 #define CONCHECK_EXTENT_AUXILIARY(x) CONCHECK_RECORD (x, extent_auxiliary)
151
152 struct extent_info {
153         struct lcrecord_header header;
154
155         struct extent_list_s *extents;
156         struct extent_stack_s *soe;
157 };
158
159 DECLARE_LRECORD(extent_info, struct extent_info);
160 #define XEXTENT_INFO(x) XRECORD (x, extent_info, struct extent_info)
161 #define XSETEXTENT_INFO(x, p) XSETRECORD (x, p, extent_info)
162 #define EXTENT_INFOP(x) RECORDP (x, extent_info)
163 #define CHECK_EXTENT_INFO(x) CHECK_RECORD (x, extent_info)
164 #define CONCHECK_EXTENT_INFO(x) CONCHECK_RECORD (x, extent_info)
165
166 void flush_cached_extent_info(Lisp_Object extent_info);
167
168 /* A "normal" field is one that is stored in the `struct flags' structure
169    in an extent.  an "aux" field is one that is stored in the extent's
170    auxiliary structure.
171
172    The functions below that have `extent_no_chase' in their name operate
173    on an extent directly (ignoring its parent), and should normally
174    only be used on extents known not to have a parent.  The other
175    versions chase down any parent links. */
176
177 #define extent_no_chase_normal_field(e, field) ((e)->flags.field)
178
179 extern_inline struct extent_auxiliary *extent_aux_or_default(EXTENT e);
180 extern_inline struct extent_auxiliary *extent_aux_or_default(EXTENT e)
181 {
182         return e->flags.has_aux ?
183             XEXTENT_AUXILIARY(XCAR(e->plist)) : &extent_auxiliary_defaults;
184 }
185
186 #define extent_no_chase_aux_field(e, field) (extent_aux_or_default(e)->field)
187
188 #define extent_normal_field(e, field)                           \
189   extent_no_chase_normal_field (extent_ancestor (e), field)
190
191 #define extent_aux_field(e, field)                              \
192   extent_no_chase_aux_field (extent_ancestor (e), field)
193
194 #define set_extent_no_chase_aux_field(e, field, value) do {     \
195   EXTENT sencaf_e = (e);                                        \
196   if (! sencaf_e->flags.has_aux)                                \
197     allocate_extent_auxiliary (sencaf_e);                       \
198   XEXTENT_AUXILIARY (XCAR (sencaf_e->plist))->field = (value);\
199 } while (0)
200
201 #define set_extent_no_chase_normal_field(e, field, value)       \
202   extent_no_chase_normal_field (e, field) = (value)
203
204 #define set_extent_aux_field(e, field, value)                   \
205   set_extent_no_chase_aux_field (extent_ancestor (e), field, value)
206
207 #define set_extent_normal_field(e, field, value)                \
208   set_extent_ancestor_normal_field (extent_no_chase (e), field, value)
209
210 /* The `parent' and `children' fields are not affected by any
211    parent links.  We don't provide any settors for these fields
212    because they need special handling and it's cleaner just to
213    do this in the particular functions that need to do this. */
214
215 #define extent_parent(e)        extent_no_chase_aux_field (e, parent)
216 #define extent_children(e)      extent_no_chase_aux_field (e, children)
217
218 #define extent_begin_glyph(e)   extent_aux_field (e, begin_glyph)
219 #define extent_end_glyph(e)     extent_aux_field (e, end_glyph)
220 #define extent_priority(e)      extent_aux_field (e, priority)
221 #define extent_invisible(e)     extent_aux_field (e, invisible)
222 #define extent_read_only(e)     extent_aux_field (e, read_only)
223 #define extent_mouse_face(e)    extent_aux_field (e, mouse_face)
224 #define extent_initial_redisplay_function(e)    extent_aux_field (e, initial_redisplay_function)
225 #define extent_before_change_functions(e) extent_aux_field (e, before_change_functions)
226 #define extent_after_change_functions(e)  extent_aux_field (e, after_change_functions)
227
228 #define set_extent_begin_glyph(e, value)        \
229   set_extent_aux_field (e, begin_glyph, value)
230 #define set_extent_end_glyph(e, value)          \
231   set_extent_aux_field (e, end_glyph, value)
232 #define set_extent_priority(e, value)           \
233   set_extent_aux_field (e, priority, value)
234 #define set_extent_invisible_1(e, value)        \
235   set_extent_aux_field (e, invisible, value)
236 #define set_extent_read_only(e, value)          \
237   set_extent_aux_field (e, read_only, value)
238 #define set_extent_mouse_face(e, value)         \
239   set_extent_aux_field (e, mouse_face, value)
240 /* Use Fset_extent_initial_redisplay_function unless you know what you're doing */
241 #define set_extent_initial_redisplay_function(e, value) \
242   set_extent_aux_field (e, initial_redisplay_function, value)
243 #define set_extent_before_change_functions(e, value)    \
244   set_extent_aux_field (e, before_change_functions, value)
245 #define set_extent_after_change_functions(e, value)     \
246   set_extent_aux_field (e, after_change_functions, value)
247
248 #define extent_face(e)               extent_normal_field (e, face)
249 #define extent_begin_glyph_layout(e) extent_normal_field (e, begin_glyph_layout)
250 #define extent_end_glyph_layout(e)   extent_normal_field (e, end_glyph_layout)
251 #define extent_start_open_p(e)       extent_normal_field (e, start_open)
252 #define extent_end_open_p(e)         extent_normal_field (e, end_open)
253 #define extent_unique_p(e)           extent_normal_field (e, unique)
254 #define extent_duplicable_p(e)       extent_normal_field (e, duplicable)
255 #define extent_detachable_p(e)       extent_normal_field (e, detachable)
256 #define extent_internal_p(e)         extent_normal_field (e, internal)
257 #define extent_in_red_event_p(e)     extent_normal_field (e, in_red_event)
258
259 extern_inline Lisp_Object *extent_no_chase_plist_addr(EXTENT e);
260 extern_inline Lisp_Object *extent_no_chase_plist_addr(EXTENT e)
261 {
262         return e->flags.has_aux ? &XCDR(e->plist) : &e->plist;
263 }
264
265 #define extent_no_chase_plist(e) (*extent_no_chase_plist_addr (e))
266
267 #define extent_plist_addr(e) extent_no_chase_plist_addr (extent_ancestor (e))
268 #define extent_plist_slot(e) extent_no_chase_plist (extent_ancestor (e))
269
270 /* flags for map_extents() and friends */
271 #define ME_END_CLOSED (1 << 0)
272 #define ME_START_OPEN (1 << 1)
273 #define ME_ALL_EXTENTS_CLOSED (1 << 2)
274 #define ME_ALL_EXTENTS_OPEN (2 << 2)
275 #define ME_ALL_EXTENTS_CLOSED_OPEN (3 << 2)
276 #define ME_ALL_EXTENTS_OPEN_CLOSED (4 << 2)
277 #define ME_ALL_EXTENTS_MASK (7 << 2)
278 #define ME_START_IN_REGION (1 << 5)
279 #define ME_END_IN_REGION (2 << 5)
280 #define ME_START_AND_END_IN_REGION (3 << 5)
281 #define ME_START_OR_END_IN_REGION (4 << 5)
282 #define ME_IN_REGION_MASK (7 << 5)
283 #define ME_NEGATE_IN_REGION (1 << 8)
284 /* the following flags are internal-only */
285 #define ME_INCLUDE_INTERNAL (1 << 9)
286 #define ME_MIGHT_THROW (1 << 10)
287 #define ME_MIGHT_MODIFY_TEXT (1 << 11)
288 #define ME_MIGHT_MODIFY_EXTENTS (1 << 12)
289 #define ME_MIGHT_MOVE_SOE (1 << 13)
290 #define ME_MIGHT_CALL_ELISP (ME_MIGHT_THROW | ME_MIGHT_MODIFY_TEXT | \
291                              ME_MIGHT_MODIFY_EXTENTS | ME_MIGHT_MOVE_SOE)
292 \f
293 #define EXTENT_LIVE_P(e)        (!EQ (extent_object (e), Qt))
294
295 #define CHECK_LIVE_EXTENT(x) do {                       \
296   CHECK_EXTENT (x);                                     \
297   if (!EXTENT_LIVE_P (XEXTENT (x)))                     \
298     dead_wrong_type_argument (Qextent_live_p, (x));     \
299 } while (0)
300 #define CONCHECK_LIVE_EXTENT(x) do {                    \
301   CONCHECK_EXTENT (x);                                  \
302   if (!EXTENT_LIVE_P (XEXTENT (x)))                     \
303     x = wrong_type_argument (Qextent_live_p, (x));      \
304 } while (0)
305
306 EXFUN(Fdetach_extent, 1);
307 EXFUN(Fextent_end_position, 1);
308 EXFUN(Fextent_object, 1);
309 EXFUN(Fextent_start_position, 1);
310 EXFUN(Fmake_extent, 3);
311 EXFUN(Fnext_single_property_change, 4);
312 EXFUN(Fprevious_single_property_change, 4);
313 EXFUN(Fset_extent_endpoints, 4);
314 EXFUN(Fnext_extent_change, 2);
315 EXFUN(Fprevious_extent_change, 2);
316 EXFUN(Fset_extent_parent, 2);
317 EXFUN(Fget_char_property, 4);
318
319 extern int inside_undo;
320 extern int in_modeline_generation;
321
322 struct extent_fragment *extent_fragment_new(Lisp_Object buffer_or_string,
323                                             struct frame *frm);
324 face_index extent_fragment_update(struct window *w, struct extent_fragment *ef,
325                                   /* Note this is in Bytinds */
326                                   Bytind pos, Lisp_Object last_glyph);
327 void extent_fragment_delete(struct extent_fragment *ef);
328 \f
329 #ifdef emacs                    /* things other than emacs want the structs */
330
331 /* from alloc.c */
332 struct extent *allocate_extent(void);
333
334 /* from extents.c */
335 EXTENT extent_ancestor_1(EXTENT e);
336
337 /* extent_ancestor() chases all the parent links until there aren't any
338    more.  extent_ancestor_1() does the same thing but it a function;
339    the following optimizes the most common case. */
340 extern_inline EXTENT extent_ancestor(EXTENT e);
341 extern_inline EXTENT extent_ancestor(EXTENT e)
342 {
343         return e->flags.has_parent ? extent_ancestor_1(e) : e;
344 }
345
346 void allocate_extent_auxiliary(EXTENT ext);
347 void init_buffer_extents(struct buffer *b);
348 void uninit_buffer_extents(struct buffer *b);
349 typedef int (*map_extents_fun) (EXTENT extent, void *arg);
350 void map_extents(Bufpos from, Bufpos to, map_extents_fun fn,
351                  void *arg, Lisp_Object obj, EXTENT after, unsigned int flags);
352
353 /* Note the following five functions are NOT in Bufpos's */
354 void adjust_extents(Lisp_Object object, Memind from, Memind to, int amount);
355 void adjust_extents_for_deletion(Lisp_Object object, Bytind from,
356                                  Bytind to, int gapsize,
357                                  int numdel, int movegapsize);
358 void verify_extent_modification(Lisp_Object object, Bytind from,
359                                 Bytind to, Lisp_Object inhibit_read_only_value);
360 void process_extents_for_insertion(Lisp_Object object,
361                                    Bytind opoint, Bytecount length);
362 void process_extents_for_deletion(Lisp_Object object, Bytind from,
363                                   Bytind to, int destroy_them);
364 void report_extent_modification(Lisp_Object, Bufpos, Bufpos, int);
365
366 void set_extent_glyph(EXTENT extent, Lisp_Object glyph, int endp,
367                       glyph_layout layout);
368
369 void add_string_extents(Lisp_Object string, struct buffer *buf,
370                         Bytind opoint, Bytecount length);
371 void splice_in_string_extents(Lisp_Object string, struct buffer *buf,
372                               Bytind opoint, Bytecount length, Bytecount pos);
373 void copy_string_extents(Lisp_Object new_string,
374                          Lisp_Object old_string,
375                          Bytecount new_pos, Bytecount old_pos,
376                          Bytecount length);
377
378 void detach_all_extents(Lisp_Object object);
379 void set_extent_endpoints(EXTENT extent, Bytind s, Bytind e,
380                           Lisp_Object object);
381
382 #ifdef ERROR_CHECK_EXTENTS
383 void sledgehammer_extent_check(Lisp_Object obj);
384 #endif
385
386 #ifdef MEMORY_USAGE_STATS
387 int compute_buffer_extent_usage(struct buffer *b,
388                                 struct overhead_stats *ovstats);
389 #endif
390
391 #endif                          /* emacs */
392
393 #endif                          /* INCLUDED_extents_h_ */