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