| 1 | /* Definitions and global variables for intervals. |
| 2 | Copyright (C) 1993-1994, 2000-2011 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 | #define NULL_INTERVAL ((INTERVAL)0) |
| 22 | #define INTERVAL_DEFAULT NULL_INTERVAL |
| 23 | |
| 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 | |
| 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. */ |
| 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. */ |
| 59 | |
| 60 | /* Properties of this interval. |
| 61 | The mark bit on this field says whether this particular interval |
| 62 | tree node has been visited. Since intervals should never be |
| 63 | shared, GC aborts if it seems to have visited an interval twice. */ |
| 64 | Lisp_Object plist; |
| 65 | }; |
| 66 | |
| 67 | /* These are macros for dealing with the interval tree. */ |
| 68 | |
| 69 | /* Size of the structure used to represent an interval */ |
| 70 | #define INTERVAL_SIZE (sizeof (struct interval)) |
| 71 | |
| 72 | /* Size of a pointer to an interval structure */ |
| 73 | #define INTERVAL_PTR_SIZE (sizeof (struct interval *)) |
| 74 | |
| 75 | #define NULL_INTERVAL_P(i) ((i) == NULL_INTERVAL) |
| 76 | |
| 77 | /* True if this interval has no right child. */ |
| 78 | #define NULL_RIGHT_CHILD(i) ((i)->right == NULL_INTERVAL) |
| 79 | |
| 80 | /* True if this interval has no left child. */ |
| 81 | #define NULL_LEFT_CHILD(i) ((i)->left == NULL_INTERVAL) |
| 82 | |
| 83 | /* True if this interval has no parent. */ |
| 84 | #define NULL_PARENT(i) ((i)->up_obj || (i)->up.interval == 0) |
| 85 | |
| 86 | /* True if this interval is the left child of some other interval. */ |
| 87 | #define AM_LEFT_CHILD(i) (! NULL_PARENT (i) \ |
| 88 | && INTERVAL_PARENT (i)->left == (i)) |
| 89 | |
| 90 | /* True if this interval is the right child of some other interval. */ |
| 91 | #define AM_RIGHT_CHILD(i) (! NULL_PARENT (i) \ |
| 92 | && INTERVAL_PARENT (i)->right == (i)) |
| 93 | |
| 94 | /* True if this interval has no children. */ |
| 95 | #define LEAF_INTERVAL_P(i) ((i)->left == NULL_INTERVAL \ |
| 96 | && (i)->right == NULL_INTERVAL) |
| 97 | |
| 98 | /* True if this interval has no parent and is therefore the root. */ |
| 99 | #define ROOT_INTERVAL_P(i) (NULL_PARENT (i)) |
| 100 | |
| 101 | /* True if this interval is the only interval in the interval tree. */ |
| 102 | #define ONLY_INTERVAL_P(i) (ROOT_INTERVAL_P ((i)) && LEAF_INTERVAL_P ((i))) |
| 103 | |
| 104 | /* True if this interval has both left and right children. */ |
| 105 | #define BOTH_KIDS_P(i) ((i)->left != NULL_INTERVAL \ |
| 106 | && (i)->right != NULL_INTERVAL) |
| 107 | |
| 108 | /* The total size of all text represented by this interval and all its |
| 109 | children in the tree. This is zero if the interval is null. */ |
| 110 | #define TOTAL_LENGTH(i) ((i) == NULL_INTERVAL ? 0 : (i)->total_length) |
| 111 | |
| 112 | /* The size of text represented by this interval alone. */ |
| 113 | #define LENGTH(i) ((i) == NULL_INTERVAL ? 0 : (TOTAL_LENGTH ((i)) \ |
| 114 | - TOTAL_LENGTH ((i)->right) \ |
| 115 | - TOTAL_LENGTH ((i)->left))) |
| 116 | |
| 117 | /* The position of the character just past the end of I. Note that |
| 118 | the position cache i->position must be valid for this to work. */ |
| 119 | #define INTERVAL_LAST_POS(i) ((i)->position + LENGTH ((i))) |
| 120 | |
| 121 | /* The total size of the left subtree of this interval. */ |
| 122 | #define LEFT_TOTAL_LENGTH(i) ((i)->left ? (i)->left->total_length : 0) |
| 123 | |
| 124 | /* The total size of the right subtree of this interval. */ |
| 125 | #define RIGHT_TOTAL_LENGTH(i) ((i)->right ? (i)->right->total_length : 0) |
| 126 | |
| 127 | |
| 128 | /* These macros are for dealing with the interval properties. */ |
| 129 | |
| 130 | /* True if this is a default interval, which is the same as being null |
| 131 | or having no properties. */ |
| 132 | #define DEFAULT_INTERVAL_P(i) (NULL_INTERVAL_P (i) || EQ ((i)->plist, Qnil)) |
| 133 | |
| 134 | /* Test what type of parent we have. Three possibilities: another |
| 135 | interval, a buffer or string object, or NULL_INTERVAL. */ |
| 136 | #define INTERVAL_HAS_PARENT(i) ((i)->up_obj == 0 && (i)->up.interval != 0) |
| 137 | #define INTERVAL_HAS_OBJECT(i) ((i)->up_obj) |
| 138 | |
| 139 | /* Set/get parent of an interval. |
| 140 | |
| 141 | The choice of macros is dependent on the type needed. Don't add |
| 142 | casts to get around this, it will break some development work in |
| 143 | progress. */ |
| 144 | #define SET_INTERVAL_PARENT(i,p) \ |
| 145 | ((i)->up_obj = 0, (i)->up.interval = (p)) |
| 146 | #define SET_INTERVAL_OBJECT(i,o) \ |
| 147 | (eassert (BUFFERP (o) || STRINGP (o)), (i)->up_obj = 1, (i)->up.obj = (o)) |
| 148 | #define INTERVAL_PARENT(i) \ |
| 149 | (eassert ((i) != 0 && (i)->up_obj == 0),(i)->up.interval) |
| 150 | #define GET_INTERVAL_OBJECT(d,s) (eassert((s)->up_obj == 1), (d) = (s)->up.obj) |
| 151 | |
| 152 | /* Make the parent of D be whatever the parent of S is, regardless of |
| 153 | type. This is used when balancing an interval tree. */ |
| 154 | #define COPY_INTERVAL_PARENT(d,s) \ |
| 155 | ((d)->up = (s)->up, (d)->up_obj = (s)->up_obj) |
| 156 | |
| 157 | /* Get the parent interval, if any, otherwise a null pointer. Useful |
| 158 | for walking up to the root in a "for" loop; use this to get the |
| 159 | "next" value, and test the result to see if it's NULL_INTERVAL. */ |
| 160 | #define INTERVAL_PARENT_OR_NULL(i) \ |
| 161 | (INTERVAL_HAS_PARENT (i) ? INTERVAL_PARENT (i) : 0) |
| 162 | |
| 163 | /* Abort if interval I's size is negative. */ |
| 164 | #define CHECK_TOTAL_LENGTH(i) \ |
| 165 | do \ |
| 166 | { \ |
| 167 | if ((i)->total_length < 0) \ |
| 168 | abort (); \ |
| 169 | } \ |
| 170 | while (0) |
| 171 | |
| 172 | /* Reset this interval to its vanilla, or no-property state. */ |
| 173 | #define RESET_INTERVAL(i) \ |
| 174 | { \ |
| 175 | (i)->total_length = (i)->position = 0; \ |
| 176 | (i)->left = (i)->right = NULL_INTERVAL; \ |
| 177 | SET_INTERVAL_PARENT (i, NULL_INTERVAL); \ |
| 178 | (i)->write_protect = 0; \ |
| 179 | (i)->visible = 0; \ |
| 180 | (i)->front_sticky = (i)->rear_sticky = 0; \ |
| 181 | (i)->plist = Qnil; \ |
| 182 | } |
| 183 | |
| 184 | /* Copy the cached property values of interval FROM to interval TO. */ |
| 185 | #define COPY_INTERVAL_CACHE(from,to) \ |
| 186 | { \ |
| 187 | (to)->write_protect = (from)->write_protect; \ |
| 188 | (to)->visible = (from)->visible; \ |
| 189 | (to)->front_sticky = (from)->front_sticky; \ |
| 190 | (to)->rear_sticky = (from)->rear_sticky; \ |
| 191 | } |
| 192 | |
| 193 | /* Copy only the set bits of FROM's cache. */ |
| 194 | #define MERGE_INTERVAL_CACHE(from,to) \ |
| 195 | { \ |
| 196 | if ((from)->write_protect) (to)->write_protect = 1; \ |
| 197 | if ((from)->visible) (to)->visible = 1; \ |
| 198 | if ((from)->front_sticky) (to)->front_sticky = 1; \ |
| 199 | if ((from)->rear_sticky) (to)->rear_sticky = 1; \ |
| 200 | } |
| 201 | |
| 202 | /* Macro determining whether the properties of an interval being |
| 203 | inserted should be merged with the properties of the text where |
| 204 | they are being inserted. */ |
| 205 | #define MERGE_INSERTIONS(i) 1 |
| 206 | |
| 207 | /* Macro determining if an invisible interval should be displayed |
| 208 | as a special glyph, or not at all. */ |
| 209 | #define DISPLAY_INVISIBLE_GLYPH(i) 0 |
| 210 | |
| 211 | /* Is this interval visible? Replace later with cache access */ |
| 212 | #define INTERVAL_VISIBLE_P(i) \ |
| 213 | (! NULL_INTERVAL_P (i) && NILP (textget ((i)->plist, Qinvisible))) |
| 214 | |
| 215 | /* Is this interval writable? Replace later with cache access */ |
| 216 | #define INTERVAL_WRITABLE_P(i) \ |
| 217 | (! NULL_INTERVAL_P (i) \ |
| 218 | && (NILP (textget ((i)->plist, Qread_only)) \ |
| 219 | || ((CONSP (Vinhibit_read_only) \ |
| 220 | ? !NILP (Fmemq (textget ((i)->plist, Qread_only), \ |
| 221 | Vinhibit_read_only)) \ |
| 222 | : !NILP (Vinhibit_read_only))))) \ |
| 223 | |
| 224 | /* Macros to tell whether insertions before or after this interval |
| 225 | should stick to it. */ |
| 226 | /* Replace later with cache access */ |
| 227 | /*#define FRONT_STICKY_P(i) ((i)->front_sticky != 0) |
| 228 | #define END_STICKY_P(i) ((i)->rear_sticky != 0)*/ |
| 229 | /* As we now have Vtext_property_default_nonsticky, these macros are |
| 230 | unreliable now. Currently, they are never used. */ |
| 231 | #define FRONT_STICKY_P(i) \ |
| 232 | (! NULL_INTERVAL_P (i) && ! NILP (textget ((i)->plist, Qfront_sticky))) |
| 233 | #define END_NONSTICKY_P(i) \ |
| 234 | (! NULL_INTERVAL_P (i) && ! NILP (textget ((i)->plist, Qrear_nonsticky))) |
| 235 | #define FRONT_NONSTICKY_P(i) \ |
| 236 | (! NULL_INTERVAL_P (i) && ! EQ (Qt, textget ((i)->plist, Qfront_sticky))) |
| 237 | |
| 238 | |
| 239 | /* If PROP is the `invisible' property of a character, |
| 240 | this is 1 if the character should be treated as invisible, |
| 241 | and 2 if it is invisible but with an ellipsis. */ |
| 242 | |
| 243 | #define TEXT_PROP_MEANS_INVISIBLE(prop) \ |
| 244 | (EQ (BVAR (current_buffer, invisibility_spec), Qt) \ |
| 245 | ? !NILP (prop) \ |
| 246 | : invisible_p (prop, BVAR (current_buffer, invisibility_spec))) |
| 247 | |
| 248 | /* Declared in alloc.c */ |
| 249 | |
| 250 | extern INTERVAL make_interval (void); |
| 251 | |
| 252 | /* Declared in intervals.c */ |
| 253 | |
| 254 | extern INTERVAL create_root_interval (Lisp_Object); |
| 255 | extern void copy_properties (INTERVAL, INTERVAL); |
| 256 | extern int intervals_equal (INTERVAL, INTERVAL); |
| 257 | extern void traverse_intervals (INTERVAL, ptrdiff_t, |
| 258 | void (*) (INTERVAL, Lisp_Object), |
| 259 | Lisp_Object); |
| 260 | extern void traverse_intervals_noorder (INTERVAL, |
| 261 | void (*) (INTERVAL, Lisp_Object), |
| 262 | Lisp_Object); |
| 263 | extern INTERVAL split_interval_right (INTERVAL, ptrdiff_t); |
| 264 | extern INTERVAL split_interval_left (INTERVAL, ptrdiff_t); |
| 265 | extern INTERVAL find_interval (INTERVAL, ptrdiff_t); |
| 266 | extern INTERVAL next_interval (INTERVAL); |
| 267 | extern INTERVAL previous_interval (INTERVAL); |
| 268 | extern INTERVAL merge_interval_left (INTERVAL); |
| 269 | extern void offset_intervals (struct buffer *, ptrdiff_t, ptrdiff_t); |
| 270 | extern void graft_intervals_into_buffer (INTERVAL, ptrdiff_t, ptrdiff_t, |
| 271 | struct buffer *, int); |
| 272 | extern void verify_interval_modification (struct buffer *, |
| 273 | ptrdiff_t, ptrdiff_t); |
| 274 | extern INTERVAL balance_intervals (INTERVAL); |
| 275 | extern void copy_intervals_to_string (Lisp_Object, struct buffer *, |
| 276 | ptrdiff_t, ptrdiff_t); |
| 277 | extern INTERVAL copy_intervals (INTERVAL, ptrdiff_t, ptrdiff_t); |
| 278 | extern int compare_string_intervals (Lisp_Object, Lisp_Object); |
| 279 | extern Lisp_Object textget (Lisp_Object, Lisp_Object); |
| 280 | extern Lisp_Object lookup_char_property (Lisp_Object, Lisp_Object, int); |
| 281 | extern void move_if_not_intangible (ptrdiff_t); |
| 282 | extern int get_property_and_range (ptrdiff_t, Lisp_Object, Lisp_Object *, |
| 283 | ptrdiff_t *, ptrdiff_t *, Lisp_Object); |
| 284 | extern Lisp_Object get_local_map (ptrdiff_t, struct buffer *, Lisp_Object); |
| 285 | extern INTERVAL update_interval (INTERVAL, ptrdiff_t); |
| 286 | extern void set_intervals_multibyte (int); |
| 287 | extern INTERVAL validate_interval_range (Lisp_Object, Lisp_Object *, |
| 288 | Lisp_Object *, int); |
| 289 | extern INTERVAL interval_of (ptrdiff_t, Lisp_Object); |
| 290 | |
| 291 | /* Defined in xdisp.c */ |
| 292 | extern int invisible_p (Lisp_Object, Lisp_Object); |
| 293 | |
| 294 | /* Declared in textprop.c */ |
| 295 | |
| 296 | /* Types of hooks. */ |
| 297 | extern Lisp_Object Qpoint_left; |
| 298 | extern Lisp_Object Qpoint_entered; |
| 299 | extern Lisp_Object Qmodification_hooks; |
| 300 | extern Lisp_Object Qcategory; |
| 301 | extern Lisp_Object Qlocal_map; |
| 302 | extern Lisp_Object Qkeymap; |
| 303 | |
| 304 | /* Visual properties text (including strings) may have. */ |
| 305 | extern Lisp_Object Qfont; |
| 306 | extern Lisp_Object Qinvisible, Qintangible; |
| 307 | |
| 308 | /* Sticky properties */ |
| 309 | extern Lisp_Object Qfront_sticky, Qrear_nonsticky; |
| 310 | |
| 311 | EXFUN (Fget_char_property, 3); |
| 312 | EXFUN (Fget_text_property, 3); |
| 313 | EXFUN (Ftext_properties_at, 2); |
| 314 | EXFUN (Fnext_property_change, 3); |
| 315 | EXFUN (Fadd_text_properties, 4); |
| 316 | EXFUN (Fset_text_properties, 4); |
| 317 | EXFUN (Fremove_text_properties, 4); |
| 318 | EXFUN (Fremove_list_of_text_properties, 4); |
| 319 | EXFUN (Ftext_property_any, 5); |
| 320 | EXFUN (Fprevious_single_char_property_change, 4); |
| 321 | extern Lisp_Object copy_text_properties (Lisp_Object, Lisp_Object, |
| 322 | Lisp_Object, Lisp_Object, |
| 323 | Lisp_Object, Lisp_Object); |
| 324 | extern Lisp_Object set_text_properties (Lisp_Object, Lisp_Object, |
| 325 | Lisp_Object, Lisp_Object, |
| 326 | Lisp_Object); |
| 327 | extern void set_text_properties_1 (Lisp_Object, Lisp_Object, |
| 328 | Lisp_Object, Lisp_Object, INTERVAL); |
| 329 | |
| 330 | Lisp_Object text_property_list (Lisp_Object, Lisp_Object, Lisp_Object, |
| 331 | Lisp_Object); |
| 332 | int add_text_properties_from_list (Lisp_Object, Lisp_Object, Lisp_Object); |
| 333 | Lisp_Object extend_property_ranges (Lisp_Object, Lisp_Object); |
| 334 | Lisp_Object get_char_property_and_overlay (Lisp_Object, Lisp_Object, |
| 335 | Lisp_Object, Lisp_Object*); |
| 336 | extern int text_property_stickiness (Lisp_Object prop, Lisp_Object pos, |
| 337 | Lisp_Object buffer); |
| 338 | extern Lisp_Object get_pos_property (Lisp_Object pos, Lisp_Object prop, |
| 339 | Lisp_Object object); |
| 340 | |
| 341 | extern void syms_of_textprop (void); |
| 342 | |
| 343 | #include "composite.h" |