Ibuffer: 'w' and 'B' default to buffer at current line
[emacs.git] / src / intervals.h
blob9a38d849b88bd7be00df503c3f6a35164113eb41
1 /* Definitions and global variables for intervals.
2 Copyright (C) 1993-1994, 2000-2016 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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 (at
9 your option) any later version.
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.
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/>. */
19 #ifndef EMACS_INTERVALS_H
20 #define EMACS_INTERVALS_H
22 INLINE_HEADER_BEGIN
24 /* Basic data type for use of intervals. */
26 struct interval
28 /* The first group of entries deal with the tree structure. */
30 ptrdiff_t total_length; /* Length of myself and both children. */
31 ptrdiff_t position; /* Cache of interval's character position. */
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. */
40 /* Parent in the tree, or the Lisp_Object containing this interval tree. */
41 union
43 struct interval *interval;
44 Lisp_Object obj;
45 } up;
46 bool_bf up_obj : 1;
48 bool_bf gcmarkbit : 1;
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. */
54 bool_bf write_protect : 1; /* True means can't modify. */
55 bool_bf visible : 1; /* False means don't display. */
56 bool_bf front_sticky : 1; /* True means text inserted just
57 before this interval goes into it. */
58 bool_bf rear_sticky : 1; /* Likewise for just after it. */
59 Lisp_Object plist; /* Other properties. */
62 /* These are macros for dealing with the interval tree. */
64 /* True if this interval has no right child. */
65 #define NULL_RIGHT_CHILD(i) ((i)->right == NULL)
67 /* True if this interval has no left child. */
68 #define NULL_LEFT_CHILD(i) ((i)->left == NULL)
70 /* True if this interval has no parent. */
71 #define NULL_PARENT(i) ((i)->up_obj || (i)->up.interval == 0)
73 /* True if this interval is the left child of some other interval. */
74 #define AM_LEFT_CHILD(i) \
75 (! NULL_PARENT (i) && INTERVAL_PARENT (i)->left == (i))
77 /* True if this interval is the right child of some other interval. */
78 #define AM_RIGHT_CHILD(i) \
79 (! NULL_PARENT (i) && INTERVAL_PARENT (i)->right == (i))
81 /* True if this interval has no children. */
82 #define LEAF_INTERVAL_P(i) ((i)->left == NULL && (i)->right == NULL)
84 /* True if this interval has no parent and is therefore the root. */
85 #define ROOT_INTERVAL_P(i) (NULL_PARENT (i))
87 /* True if this interval is the only interval in the interval tree. */
88 #define ONLY_INTERVAL_P(i) (ROOT_INTERVAL_P ((i)) && LEAF_INTERVAL_P ((i)))
90 /* True if this interval has both left and right children. */
91 #define BOTH_KIDS_P(i) ((i)->left != NULL && (i)->right != NULL)
93 /* The total size of all text represented by this interval and all its
94 children in the tree. This is zero if the interval is null. */
95 #define TOTAL_LENGTH(i) ((i) == NULL ? 0 : (i)->total_length)
97 /* The size of text represented by this interval alone. */
98 #define LENGTH(i) ((i) == NULL ? 0 : (TOTAL_LENGTH ((i)) \
99 - TOTAL_LENGTH ((i)->right) \
100 - TOTAL_LENGTH ((i)->left)))
102 /* The position of the character just past the end of I. Note that
103 the position cache i->position must be valid for this to work. */
104 #define INTERVAL_LAST_POS(i) ((i)->position + LENGTH ((i)))
106 /* The total size of the left subtree of this interval. */
107 #define LEFT_TOTAL_LENGTH(i) ((i)->left ? (i)->left->total_length : 0)
109 /* The total size of the right subtree of this interval. */
110 #define RIGHT_TOTAL_LENGTH(i) ((i)->right ? (i)->right->total_length : 0)
112 /* These macros are for dealing with the interval properties. */
114 /* True if this is a default interval, which is the same as being null
115 or having no properties. */
116 #define DEFAULT_INTERVAL_P(i) (!i || EQ ((i)->plist, Qnil))
118 /* Test what type of parent we have. Three possibilities: another
119 interval, a buffer or string object, or NULL. */
120 #define INTERVAL_HAS_PARENT(i) (! (i)->up_obj && (i)->up.interval != 0)
121 #define INTERVAL_HAS_OBJECT(i) ((i)->up_obj)
123 /* Use these macros to get parent of an interval.
125 The choice of macros is dependent on the type needed. Don't add
126 casts to get around this, it will break some development work in
127 progress. */
129 #define INTERVAL_PARENT(i) \
130 (eassert ((i) != 0 && ! (i)->up_obj), (i)->up.interval)
132 #define GET_INTERVAL_OBJECT(d,s) (eassert ((s)->up_obj), (d) = (s)->up.obj)
134 /* Use these functions to set Lisp_Object
135 or pointer slots of struct interval. */
137 INLINE void
138 set_interval_object (INTERVAL i, Lisp_Object obj)
140 eassert (BUFFERP (obj) || STRINGP (obj));
141 i->up_obj = 1;
142 i->up.obj = obj;
145 INLINE void
146 set_interval_parent (INTERVAL i, INTERVAL parent)
148 i->up_obj = false;
149 i->up.interval = parent;
152 INLINE void
153 set_interval_plist (INTERVAL i, Lisp_Object plist)
155 i->plist = plist;
158 /* Get the parent interval, if any, otherwise a null pointer. Useful
159 for walking up to the root in a "for" loop; use this to get the
160 "next" value, and test the result to see if it's NULL. */
161 #define INTERVAL_PARENT_OR_NULL(i) \
162 (INTERVAL_HAS_PARENT (i) ? INTERVAL_PARENT (i) : 0)
164 /* Reset this interval to its vanilla, or no-property state. */
165 #define RESET_INTERVAL(i) \
166 do { \
167 (i)->total_length = (i)->position = 0; \
168 (i)->left = (i)->right = NULL; \
169 set_interval_parent (i, NULL); \
170 (i)->write_protect = false; \
171 (i)->visible = false; \
172 (i)->front_sticky = (i)->rear_sticky = false; \
173 set_interval_plist (i, Qnil); \
174 } while (false)
176 /* Copy the cached property values of interval FROM to interval TO. */
177 #define COPY_INTERVAL_CACHE(from,to) \
178 do { \
179 (to)->write_protect = (from)->write_protect; \
180 (to)->visible = (from)->visible; \
181 (to)->front_sticky = (from)->front_sticky; \
182 (to)->rear_sticky = (from)->rear_sticky; \
183 } while (false)
185 /* Copy only the set bits of FROM's cache. */
186 #define MERGE_INTERVAL_CACHE(from,to) \
187 do { \
188 if ((from)->write_protect) (to)->write_protect = true; \
189 if ((from)->visible) (to)->visible = true; \
190 if ((from)->front_sticky) (to)->front_sticky = true; \
191 if ((from)->rear_sticky) (to)->rear_sticky = true; \
192 } while (false)
194 /* Is this interval visible? Replace later with cache access. */
195 #define INTERVAL_VISIBLE_P(i) \
196 (i && NILP (textget ((i)->plist, Qinvisible)))
198 /* Is this interval writable? Replace later with cache access. */
199 #define INTERVAL_WRITABLE_P(i) \
200 (NILP (textget ((i)->plist, Qread_only)) \
201 || !NILP (textget ((i)->plist, Qinhibit_read_only)) \
202 || ((CONSP (Vinhibit_read_only) \
203 ? !NILP (Fmemq (textget ((i)->plist, Qread_only), \
204 Vinhibit_read_only)) \
205 : !NILP (Vinhibit_read_only))))
207 /* Macros to tell whether insertions before or after this interval
208 should stick to it. Now we have Vtext_property_default_nonsticky,
209 so these macros are unreliable now and never used. */
211 #if false
212 #define FRONT_STICKY_P(i) \
213 (i && ! NILP (textget ((i)->plist, Qfront_sticky)))
214 #define END_NONSTICKY_P(i) \
215 (i && ! NILP (textget ((i)->plist, Qrear_nonsticky)))
216 #define FRONT_NONSTICKY_P(i) \
217 (i && ! EQ (Qt, textget ((i)->plist, Qfront_sticky)))
218 #endif
220 /* If PROP is the `invisible' property of a character,
221 this is 1 if the character should be treated as invisible,
222 and 2 if it is invisible but with an ellipsis. */
224 #define TEXT_PROP_MEANS_INVISIBLE(prop) \
225 (EQ (BVAR (current_buffer, invisibility_spec), Qt) \
226 ? !NILP (prop) \
227 : invisible_prop (prop, BVAR (current_buffer, invisibility_spec)))
229 /* Declared in alloc.c. */
231 extern INTERVAL make_interval (void);
233 /* Declared in intervals.c. */
235 extern INTERVAL create_root_interval (Lisp_Object);
236 extern void copy_properties (INTERVAL, INTERVAL);
237 extern bool intervals_equal (INTERVAL, INTERVAL);
238 extern void traverse_intervals (INTERVAL, ptrdiff_t,
239 void (*) (INTERVAL, Lisp_Object),
240 Lisp_Object);
241 extern void traverse_intervals_noorder (INTERVAL,
242 void (*) (INTERVAL, Lisp_Object),
243 Lisp_Object);
244 extern INTERVAL split_interval_right (INTERVAL, ptrdiff_t);
245 extern INTERVAL split_interval_left (INTERVAL, ptrdiff_t);
246 extern INTERVAL find_interval (INTERVAL, ptrdiff_t);
247 extern INTERVAL next_interval (INTERVAL);
248 extern INTERVAL previous_interval (INTERVAL);
249 extern INTERVAL merge_interval_left (INTERVAL);
250 extern void offset_intervals (struct buffer *, ptrdiff_t, ptrdiff_t);
251 extern void graft_intervals_into_buffer (INTERVAL, ptrdiff_t, ptrdiff_t,
252 struct buffer *, bool);
253 extern void verify_interval_modification (struct buffer *,
254 ptrdiff_t, ptrdiff_t);
255 extern INTERVAL balance_intervals (INTERVAL);
256 extern void copy_intervals_to_string (Lisp_Object, struct buffer *,
257 ptrdiff_t, ptrdiff_t);
258 extern INTERVAL copy_intervals (INTERVAL, ptrdiff_t, ptrdiff_t);
259 extern bool compare_string_intervals (Lisp_Object, Lisp_Object);
260 extern Lisp_Object textget (Lisp_Object, Lisp_Object);
261 extern Lisp_Object lookup_char_property (Lisp_Object, Lisp_Object, bool);
262 extern void move_if_not_intangible (ptrdiff_t);
263 extern bool get_property_and_range (ptrdiff_t, Lisp_Object, Lisp_Object *,
264 ptrdiff_t *, ptrdiff_t *, Lisp_Object);
265 extern Lisp_Object get_local_map (ptrdiff_t, struct buffer *, Lisp_Object);
266 extern INTERVAL update_interval (INTERVAL, ptrdiff_t);
267 extern void set_intervals_multibyte (bool);
268 extern INTERVAL validate_interval_range (Lisp_Object, Lisp_Object *,
269 Lisp_Object *, bool);
270 extern INTERVAL interval_of (ptrdiff_t, Lisp_Object);
272 /* Defined in xdisp.c. */
273 extern int invisible_prop (Lisp_Object, Lisp_Object);
275 /* Defined in textprop.c. */
276 extern Lisp_Object copy_text_properties (Lisp_Object, Lisp_Object,
277 Lisp_Object, Lisp_Object,
278 Lisp_Object, Lisp_Object);
279 extern Lisp_Object set_text_properties (Lisp_Object, Lisp_Object,
280 Lisp_Object, Lisp_Object,
281 Lisp_Object);
282 extern void set_text_properties_1 (Lisp_Object, Lisp_Object,
283 Lisp_Object, Lisp_Object, INTERVAL);
285 Lisp_Object text_property_list (Lisp_Object, Lisp_Object, Lisp_Object,
286 Lisp_Object);
287 void add_text_properties_from_list (Lisp_Object, Lisp_Object, Lisp_Object);
288 Lisp_Object extend_property_ranges (Lisp_Object, Lisp_Object, Lisp_Object);
289 Lisp_Object get_char_property_and_overlay (Lisp_Object, Lisp_Object,
290 Lisp_Object, Lisp_Object*);
291 extern int text_property_stickiness (Lisp_Object prop, Lisp_Object pos,
292 Lisp_Object buffer);
294 extern void syms_of_textprop (void);
296 INLINE_HEADER_END
298 #endif /* EMACS_INTERVALS_H */