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