Check total length of intervals with eassert.
[bpt/emacs.git] / src / intervals.h
1 /* Definitions and global variables for intervals.
2 Copyright (C) 1993-1994, 2000-2012 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs 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 GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "dispextern.h"
20
21 INLINE_HEADER_BEGIN
22
23 #define NULL_INTERVAL ((INTERVAL)0)
24 #define INTERVAL_DEFAULT NULL_INTERVAL
25
26 /* Basic data type for use of intervals. */
27
28 struct interval
29 {
30 /* The first group of entries deal with the tree structure. */
31
32 ptrdiff_t total_length; /* Length of myself and both children. */
33 ptrdiff_t position; /* Cache of interval's character position. */
34 /* This field is usually updated
35 simultaneously with an interval
36 traversal, there is no guarantee
37 that it is valid for a random
38 interval. */
39 struct interval *left; /* Intervals which precede me. */
40 struct interval *right; /* Intervals which succeed me. */
41
42 /* Parent in the tree, or the Lisp_Object containing this interval tree. */
43 union
44 {
45 struct interval *interval;
46 Lisp_Object obj;
47 } up;
48 unsigned int up_obj : 1;
49
50 unsigned gcmarkbit : 1;
51
52 /* The remaining components are `properties' of the interval.
53 The first four are duplicates for things which can be on the list,
54 for purposes of speed. */
55
56 unsigned int write_protect : 1; /* Non-zero means can't modify. */
57 unsigned int visible : 1; /* Zero means don't display. */
58 unsigned int front_sticky : 1; /* Non-zero means text inserted just
59 before this interval goes into it. */
60 unsigned int rear_sticky : 1; /* Likewise for just after it. */
61 Lisp_Object plist; /* Other properties. */
62 };
63
64 /* These are macros for dealing with the interval tree. */
65
66 #define NULL_INTERVAL_P(i) ((i) == NULL_INTERVAL)
67
68 /* True if this interval has no right child. */
69 #define NULL_RIGHT_CHILD(i) ((i)->right == NULL_INTERVAL)
70
71 /* True if this interval has no left child. */
72 #define NULL_LEFT_CHILD(i) ((i)->left == NULL_INTERVAL)
73
74 /* True if this interval has no parent. */
75 #define NULL_PARENT(i) ((i)->up_obj || (i)->up.interval == 0)
76
77 /* True if this interval is the left child of some other interval. */
78 #define AM_LEFT_CHILD(i) (! NULL_PARENT (i) \
79 && INTERVAL_PARENT (i)->left == (i))
80
81 /* True if this interval is the right child of some other interval. */
82 #define AM_RIGHT_CHILD(i) (! NULL_PARENT (i) \
83 && INTERVAL_PARENT (i)->right == (i))
84
85 /* True if this interval has no children. */
86 #define LEAF_INTERVAL_P(i) ((i)->left == NULL_INTERVAL \
87 && (i)->right == NULL_INTERVAL)
88
89 /* True if this interval has no parent and is therefore the root. */
90 #define ROOT_INTERVAL_P(i) (NULL_PARENT (i))
91
92 /* True if this interval is the only interval in the interval tree. */
93 #define ONLY_INTERVAL_P(i) (ROOT_INTERVAL_P ((i)) && LEAF_INTERVAL_P ((i)))
94
95 /* True if this interval has both left and right children. */
96 #define BOTH_KIDS_P(i) ((i)->left != NULL_INTERVAL \
97 && (i)->right != NULL_INTERVAL)
98
99 /* The total size of all text represented by this interval and all its
100 children in the tree. This is zero if the interval is null. */
101 #define TOTAL_LENGTH(i) ((i) == NULL_INTERVAL ? 0 : (i)->total_length)
102
103 /* The size of text represented by this interval alone. */
104 #define LENGTH(i) ((i) == NULL_INTERVAL ? 0 : (TOTAL_LENGTH ((i)) \
105 - TOTAL_LENGTH ((i)->right) \
106 - TOTAL_LENGTH ((i)->left)))
107
108 /* The position of the character just past the end of I. Note that
109 the position cache i->position must be valid for this to work. */
110 #define INTERVAL_LAST_POS(i) ((i)->position + LENGTH ((i)))
111
112 /* The total size of the left subtree of this interval. */
113 #define LEFT_TOTAL_LENGTH(i) ((i)->left ? (i)->left->total_length : 0)
114
115 /* The total size of the right subtree of this interval. */
116 #define RIGHT_TOTAL_LENGTH(i) ((i)->right ? (i)->right->total_length : 0)
117
118
119 /* These macros are for dealing with the interval properties. */
120
121 /* True if this is a default interval, which is the same as being null
122 or having no properties. */
123 #define DEFAULT_INTERVAL_P(i) (NULL_INTERVAL_P (i) || EQ ((i)->plist, Qnil))
124
125 /* Test what type of parent we have. Three possibilities: another
126 interval, a buffer or string object, or NULL_INTERVAL. */
127 #define INTERVAL_HAS_PARENT(i) ((i)->up_obj == 0 && (i)->up.interval != 0)
128 #define INTERVAL_HAS_OBJECT(i) ((i)->up_obj)
129
130 /* Use these macros to get parent of an interval.
131
132 The choice of macros is dependent on the type needed. Don't add
133 casts to get around this, it will break some development work in
134 progress. */
135
136 #define INTERVAL_PARENT(i) \
137 (eassert ((i) != 0 && (i)->up_obj == 0), (i)->up.interval)
138
139 #define GET_INTERVAL_OBJECT(d,s) (eassert ((s)->up_obj == 1), (d) = (s)->up.obj)
140
141 /* Use these functions to set Lisp_Object
142 or pointer slots of struct interval. */
143
144 LISP_INLINE void
145 interval_set_parent (INTERVAL i, INTERVAL parent)
146 {
147 i->up_obj = 0;
148 i->up.interval = parent;
149 }
150
151 LISP_INLINE void
152 interval_set_object (INTERVAL i, Lisp_Object obj)
153 {
154 eassert (BUFFERP (obj) || STRINGP (obj));
155 i->up_obj = 1;
156 i->up.obj = obj;
157 }
158
159 LISP_INLINE void
160 interval_set_left (INTERVAL i, INTERVAL left)
161 {
162 i->left = left;
163 }
164
165 LISP_INLINE void
166 interval_set_right (INTERVAL i, INTERVAL right)
167 {
168 i->right = right;
169 }
170
171 LISP_INLINE Lisp_Object
172 interval_set_plist (INTERVAL i, Lisp_Object plist)
173 {
174 i->plist = plist;
175 return plist;
176 }
177
178 /* Make the parent of D be whatever the parent of S is, regardless
179 of the type. This is used when balancing an interval tree. */
180
181 LISP_INLINE void
182 interval_copy_parent (INTERVAL d, INTERVAL s)
183 {
184 d->up = s->up;
185 d->up_obj = s->up_obj;
186 }
187
188 /* Get the parent interval, if any, otherwise a null pointer. Useful
189 for walking up to the root in a "for" loop; use this to get the
190 "next" value, and test the result to see if it's NULL_INTERVAL. */
191 #define INTERVAL_PARENT_OR_NULL(i) \
192 (INTERVAL_HAS_PARENT (i) ? INTERVAL_PARENT (i) : 0)
193
194 /* Reset this interval to its vanilla, or no-property state. */
195 #define RESET_INTERVAL(i) \
196 { \
197 (i)->total_length = (i)->position = 0; \
198 (i)->left = (i)->right = NULL_INTERVAL; \
199 interval_set_parent (i, NULL_INTERVAL); \
200 (i)->write_protect = 0; \
201 (i)->visible = 0; \
202 (i)->front_sticky = (i)->rear_sticky = 0; \
203 interval_set_plist (i, Qnil); \
204 }
205
206 /* Copy the cached property values of interval FROM to interval TO. */
207 #define COPY_INTERVAL_CACHE(from,to) \
208 { \
209 (to)->write_protect = (from)->write_protect; \
210 (to)->visible = (from)->visible; \
211 (to)->front_sticky = (from)->front_sticky; \
212 (to)->rear_sticky = (from)->rear_sticky; \
213 }
214
215 /* Copy only the set bits of FROM's cache. */
216 #define MERGE_INTERVAL_CACHE(from,to) \
217 { \
218 if ((from)->write_protect) (to)->write_protect = 1; \
219 if ((from)->visible) (to)->visible = 1; \
220 if ((from)->front_sticky) (to)->front_sticky = 1; \
221 if ((from)->rear_sticky) (to)->rear_sticky = 1; \
222 }
223
224 /* Macro determining whether the properties of an interval being
225 inserted should be merged with the properties of the text where
226 they are being inserted. */
227 #define MERGE_INSERTIONS(i) 1
228
229 /* Macro determining if an invisible interval should be displayed
230 as a special glyph, or not at all. */
231 #define DISPLAY_INVISIBLE_GLYPH(i) 0
232
233 /* Is this interval visible? Replace later with cache access. */
234 #define INTERVAL_VISIBLE_P(i) \
235 (! NULL_INTERVAL_P (i) && NILP (textget ((i)->plist, Qinvisible)))
236
237 /* Is this interval writable? Replace later with cache access. */
238 #define INTERVAL_WRITABLE_P(i) \
239 (! NULL_INTERVAL_P (i) \
240 && (NILP (textget ((i)->plist, Qread_only)) \
241 || ((CONSP (Vinhibit_read_only) \
242 ? !NILP (Fmemq (textget ((i)->plist, Qread_only), \
243 Vinhibit_read_only)) \
244 : !NILP (Vinhibit_read_only))))) \
245
246 /* Macros to tell whether insertions before or after this interval
247 should stick to it. */
248 /* Replace later with cache access */
249 /*#define FRONT_STICKY_P(i) ((i)->front_sticky != 0)
250 #define END_STICKY_P(i) ((i)->rear_sticky != 0)*/
251 /* As we now have Vtext_property_default_nonsticky, these macros are
252 unreliable now. Currently, they are never used. */
253 #define FRONT_STICKY_P(i) \
254 (! NULL_INTERVAL_P (i) && ! NILP (textget ((i)->plist, Qfront_sticky)))
255 #define END_NONSTICKY_P(i) \
256 (! NULL_INTERVAL_P (i) && ! NILP (textget ((i)->plist, Qrear_nonsticky)))
257 #define FRONT_NONSTICKY_P(i) \
258 (! NULL_INTERVAL_P (i) && ! EQ (Qt, textget ((i)->plist, Qfront_sticky)))
259
260
261 /* If PROP is the `invisible' property of a character,
262 this is 1 if the character should be treated as invisible,
263 and 2 if it is invisible but with an ellipsis. */
264
265 #define TEXT_PROP_MEANS_INVISIBLE(prop) \
266 (EQ (BVAR (current_buffer, invisibility_spec), Qt) \
267 ? !NILP (prop) \
268 : invisible_p (prop, BVAR (current_buffer, invisibility_spec)))
269
270 /* Declared in alloc.c. */
271
272 extern INTERVAL make_interval (void);
273
274 /* Declared in intervals.c. */
275
276 extern INTERVAL create_root_interval (Lisp_Object);
277 extern void copy_properties (INTERVAL, INTERVAL);
278 extern int intervals_equal (INTERVAL, INTERVAL);
279 extern void traverse_intervals (INTERVAL, ptrdiff_t,
280 void (*) (INTERVAL, Lisp_Object),
281 Lisp_Object);
282 extern void traverse_intervals_noorder (INTERVAL,
283 void (*) (INTERVAL, Lisp_Object),
284 Lisp_Object);
285 extern INTERVAL split_interval_right (INTERVAL, ptrdiff_t);
286 extern INTERVAL split_interval_left (INTERVAL, ptrdiff_t);
287 extern INTERVAL find_interval (INTERVAL, ptrdiff_t);
288 extern INTERVAL next_interval (INTERVAL);
289 extern INTERVAL previous_interval (INTERVAL);
290 extern INTERVAL merge_interval_left (INTERVAL);
291 extern void offset_intervals (struct buffer *, ptrdiff_t, ptrdiff_t);
292 extern void graft_intervals_into_buffer (INTERVAL, ptrdiff_t, ptrdiff_t,
293 struct buffer *, int);
294 extern void verify_interval_modification (struct buffer *,
295 ptrdiff_t, ptrdiff_t);
296 extern INTERVAL balance_intervals (INTERVAL);
297 extern void copy_intervals_to_string (Lisp_Object, struct buffer *,
298 ptrdiff_t, ptrdiff_t);
299 extern INTERVAL copy_intervals (INTERVAL, ptrdiff_t, ptrdiff_t);
300 extern int compare_string_intervals (Lisp_Object, Lisp_Object);
301 extern Lisp_Object textget (Lisp_Object, Lisp_Object);
302 extern Lisp_Object lookup_char_property (Lisp_Object, Lisp_Object, int);
303 extern void move_if_not_intangible (ptrdiff_t);
304 extern int get_property_and_range (ptrdiff_t, Lisp_Object, Lisp_Object *,
305 ptrdiff_t *, ptrdiff_t *, Lisp_Object);
306 extern Lisp_Object get_local_map (ptrdiff_t, struct buffer *, Lisp_Object);
307 extern INTERVAL update_interval (INTERVAL, ptrdiff_t);
308 extern void set_intervals_multibyte (int);
309 extern INTERVAL validate_interval_range (Lisp_Object, Lisp_Object *,
310 Lisp_Object *, int);
311 extern INTERVAL interval_of (ptrdiff_t, Lisp_Object);
312
313 /* Defined in xdisp.c. */
314 extern int invisible_p (Lisp_Object, Lisp_Object);
315
316 /* Declared in textprop.c. */
317
318 /* Types of hooks. */
319 extern Lisp_Object Qpoint_left;
320 extern Lisp_Object Qpoint_entered;
321 extern Lisp_Object Qmodification_hooks;
322 extern Lisp_Object Qcategory;
323 extern Lisp_Object Qlocal_map;
324 extern Lisp_Object Qkeymap;
325
326 /* Visual properties text (including strings) may have. */
327 extern Lisp_Object Qfont;
328 extern Lisp_Object Qinvisible, Qintangible;
329
330 /* Sticky properties. */
331 extern Lisp_Object Qfront_sticky, Qrear_nonsticky;
332
333 extern Lisp_Object copy_text_properties (Lisp_Object, Lisp_Object,
334 Lisp_Object, Lisp_Object,
335 Lisp_Object, Lisp_Object);
336 extern Lisp_Object set_text_properties (Lisp_Object, Lisp_Object,
337 Lisp_Object, Lisp_Object,
338 Lisp_Object);
339 extern void set_text_properties_1 (Lisp_Object, Lisp_Object,
340 Lisp_Object, Lisp_Object, INTERVAL);
341
342 Lisp_Object text_property_list (Lisp_Object, Lisp_Object, Lisp_Object,
343 Lisp_Object);
344 int add_text_properties_from_list (Lisp_Object, Lisp_Object, Lisp_Object);
345 Lisp_Object extend_property_ranges (Lisp_Object, Lisp_Object);
346 Lisp_Object get_char_property_and_overlay (Lisp_Object, Lisp_Object,
347 Lisp_Object, Lisp_Object*);
348 extern int text_property_stickiness (Lisp_Object prop, Lisp_Object pos,
349 Lisp_Object buffer);
350 extern Lisp_Object get_pos_property (Lisp_Object pos, Lisp_Object prop,
351 Lisp_Object object);
352
353 extern void syms_of_textprop (void);
354
355 #include "composite.h"
356
357 INLINE_HEADER_END