Commit | Line | Data |
---|---|---|
a50699fd | 1 | /* Code for doing intervals. |
ab422c4d PE |
2 | Copyright (C) 1993-1995, 1997-1998, 2001-2013 Free Software |
3 | Foundation, Inc. | |
a50699fd JA |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
a50699fd | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
a50699fd JA |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
a50699fd JA |
19 | |
20 | ||
21 | /* NOTES: | |
22 | ||
23 | Have to ensure that we can't put symbol nil on a plist, or some | |
24 | functions may work incorrectly. | |
25 | ||
26 | An idea: Have the owner of the tree keep count of splits and/or | |
27 | insertion lengths (in intervals), and balance after every N. | |
28 | ||
29 | Need to call *_left_hook when buffer is killed. | |
30 | ||
31 | Scan for zero-length, or 0-length to see notes about handling | |
32 | zero length interval-markers. | |
33 | ||
34 | There are comments around about freeing intervals. It might be | |
35 | faster to explicitly free them (put them on the free list) than | |
36 | to GC them. | |
37 | ||
38 | */ | |
39 | ||
40 | ||
18160b98 | 41 | #include <config.h> |
7961135c DA |
42 | |
43 | #define INTERVALS_INLINE EXTERN_INLINE | |
44 | ||
37aa2f85 | 45 | #include <intprops.h> |
a50699fd JA |
46 | #include "lisp.h" |
47 | #include "intervals.h" | |
e5560ff7 | 48 | #include "character.h" |
a50699fd | 49 | #include "buffer.h" |
328c0f1f | 50 | #include "puresize.h" |
f54a8c1a | 51 | #include "keyboard.h" |
8feddab4 | 52 | #include "keymap.h" |
a50699fd | 53 | |
45d82bdc KH |
54 | /* Test for membership, allowing for t (actually any non-cons) to mean the |
55 | universal set. */ | |
56 | ||
57 | #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set)) | |
58 | ||
93ea6e8f PE |
59 | static Lisp_Object merge_properties_sticky (Lisp_Object, Lisp_Object); |
60 | static INTERVAL merge_interval_right (INTERVAL); | |
f57e2426 | 61 | static INTERVAL reproduce_tree (INTERVAL, INTERVAL); |
a50699fd | 62 | \f |
7ce503fd | 63 | /* Utility functions for intervals. */ |
a50699fd | 64 | |
0c94c8d6 PE |
65 | /* Use these functions to set Lisp_Object |
66 | or pointer slots of struct interval. */ | |
67 | ||
b0ab8123 | 68 | static void |
0c94c8d6 PE |
69 | set_interval_object (INTERVAL i, Lisp_Object obj) |
70 | { | |
71 | eassert (BUFFERP (obj) || STRINGP (obj)); | |
72 | i->up_obj = 1; | |
73 | i->up.obj = obj; | |
74 | } | |
75 | ||
b0ab8123 | 76 | static void |
0c94c8d6 PE |
77 | set_interval_left (INTERVAL i, INTERVAL left) |
78 | { | |
79 | i->left = left; | |
80 | } | |
81 | ||
b0ab8123 | 82 | static void |
0c94c8d6 PE |
83 | set_interval_right (INTERVAL i, INTERVAL right) |
84 | { | |
85 | i->right = right; | |
86 | } | |
87 | ||
88 | /* Make the parent of D be whatever the parent of S is, regardless | |
89 | of the type. This is used when balancing an interval tree. */ | |
90 | ||
b0ab8123 | 91 | static void |
0c94c8d6 PE |
92 | copy_interval_parent (INTERVAL d, INTERVAL s) |
93 | { | |
94 | d->up = s->up; | |
95 | d->up_obj = s->up_obj; | |
96 | } | |
a50699fd | 97 | |
7ce503fd | 98 | /* Create the root interval of some object, a buffer or string. */ |
a50699fd JA |
99 | |
100 | INTERVAL | |
971de7fb | 101 | create_root_interval (Lisp_Object parent) |
a50699fd | 102 | { |
328c0f1f RS |
103 | INTERVAL new; |
104 | ||
105 | CHECK_IMPURE (parent); | |
106 | ||
107 | new = make_interval (); | |
a50699fd | 108 | |
b629dd47 | 109 | if (BUFFERP (parent)) |
a50699fd | 110 | { |
2bc7a79b JB |
111 | new->total_length = (BUF_Z (XBUFFER (parent)) |
112 | - BUF_BEG (XBUFFER (parent))); | |
908589fd | 113 | eassert (TOTAL_LENGTH (new) >= 0); |
0c94c8d6 | 114 | set_buffer_intervals (XBUFFER (parent), new); |
4867a283 | 115 | new->position = BEG; |
a50699fd | 116 | } |
b629dd47 | 117 | else if (STRINGP (parent)) |
a50699fd | 118 | { |
d5db4077 | 119 | new->total_length = SCHARS (parent); |
908589fd | 120 | eassert (TOTAL_LENGTH (new) >= 0); |
0c94c8d6 | 121 | set_string_intervals (parent, new); |
944d4e4b | 122 | new->position = 0; |
a50699fd JA |
123 | } |
124 | ||
0c94c8d6 | 125 | set_interval_object (new, parent); |
a50699fd JA |
126 | |
127 | return new; | |
128 | } | |
129 | ||
130 | /* Make the interval TARGET have exactly the properties of SOURCE */ | |
131 | ||
132 | void | |
971de7fb | 133 | copy_properties (register INTERVAL source, register INTERVAL target) |
a50699fd JA |
134 | { |
135 | if (DEFAULT_INTERVAL_P (source) && DEFAULT_INTERVAL_P (target)) | |
136 | return; | |
137 | ||
138 | COPY_INTERVAL_CACHE (source, target); | |
0c94c8d6 | 139 | set_interval_plist (target, Fcopy_sequence (source->plist)); |
a50699fd JA |
140 | } |
141 | ||
142 | /* Merge the properties of interval SOURCE into the properties | |
323a7ad4 RS |
143 | of interval TARGET. That is to say, each property in SOURCE |
144 | is added to TARGET if TARGET has no such property as yet. */ | |
a50699fd JA |
145 | |
146 | static void | |
971de7fb | 147 | merge_properties (register INTERVAL source, register INTERVAL target) |
a50699fd JA |
148 | { |
149 | register Lisp_Object o, sym, val; | |
150 | ||
151 | if (DEFAULT_INTERVAL_P (source) && DEFAULT_INTERVAL_P (target)) | |
152 | return; | |
153 | ||
154 | MERGE_INTERVAL_CACHE (source, target); | |
155 | ||
156 | o = source->plist; | |
4867a283 | 157 | while (CONSP (o)) |
a50699fd | 158 | { |
4867a283 | 159 | sym = XCAR (o); |
4028a938 KS |
160 | o = XCDR (o); |
161 | CHECK_CONS (o); | |
162 | ||
163 | val = target->plist; | |
164 | while (CONSP (val) && !EQ (XCAR (val), sym)) | |
165 | { | |
166 | val = XCDR (val); | |
167 | if (!CONSP (val)) | |
168 | break; | |
169 | val = XCDR (val); | |
170 | } | |
a50699fd JA |
171 | |
172 | if (NILP (val)) | |
173 | { | |
4867a283 | 174 | val = XCAR (o); |
0c94c8d6 | 175 | set_interval_plist (target, Fcons (sym, Fcons (val, target->plist))); |
a50699fd | 176 | } |
4028a938 | 177 | o = XCDR (o); |
a50699fd JA |
178 | } |
179 | } | |
180 | ||
a08d4ba7 | 181 | /* Return true if the two intervals have the same properties. */ |
a50699fd | 182 | |
a08d4ba7 | 183 | bool |
971de7fb | 184 | intervals_equal (INTERVAL i0, INTERVAL i1) |
a50699fd | 185 | { |
a08d4ba7 PE |
186 | Lisp_Object i0_cdr, i0_sym; |
187 | Lisp_Object i1_cdr, i1_val; | |
a50699fd JA |
188 | |
189 | if (DEFAULT_INTERVAL_P (i0) && DEFAULT_INTERVAL_P (i1)) | |
190 | return 1; | |
191 | ||
323a7ad4 RS |
192 | if (DEFAULT_INTERVAL_P (i0) || DEFAULT_INTERVAL_P (i1)) |
193 | return 0; | |
194 | ||
a50699fd | 195 | i0_cdr = i0->plist; |
4028a938 KS |
196 | i1_cdr = i1->plist; |
197 | while (CONSP (i0_cdr) && CONSP (i1_cdr)) | |
a50699fd | 198 | { |
4867a283 | 199 | i0_sym = XCAR (i0_cdr); |
4028a938 KS |
200 | i0_cdr = XCDR (i0_cdr); |
201 | if (!CONSP (i0_cdr)) | |
1088b922 | 202 | return 0; |
4028a938 KS |
203 | i1_val = i1->plist; |
204 | while (CONSP (i1_val) && !EQ (XCAR (i1_val), i0_sym)) | |
205 | { | |
206 | i1_val = XCDR (i1_val); | |
207 | if (!CONSP (i1_val)) | |
1088b922 | 208 | return 0; |
4028a938 KS |
209 | i1_val = XCDR (i1_val); |
210 | } | |
a50699fd | 211 | |
7ce503fd | 212 | /* i0 has something i1 doesn't. */ |
a50699fd JA |
213 | if (EQ (i1_val, Qnil)) |
214 | return 0; | |
215 | ||
7ce503fd | 216 | /* i0 and i1 both have sym, but it has different values in each. */ |
4028a938 KS |
217 | if (!CONSP (i1_val) |
218 | || (i1_val = XCDR (i1_val), !CONSP (i1_val)) | |
219 | || !EQ (XCAR (i1_val), XCAR (i0_cdr))) | |
a50699fd JA |
220 | return 0; |
221 | ||
4867a283 | 222 | i0_cdr = XCDR (i0_cdr); |
a50699fd | 223 | |
4028a938 KS |
224 | i1_cdr = XCDR (i1_cdr); |
225 | if (!CONSP (i1_cdr)) | |
1088b922 | 226 | return 0; |
4028a938 KS |
227 | i1_cdr = XCDR (i1_cdr); |
228 | } | |
a50699fd | 229 | |
4028a938 KS |
230 | /* Lengths of the two plists were equal. */ |
231 | return (NILP (i0_cdr) && NILP (i1_cdr)); | |
a50699fd JA |
232 | } |
233 | \f | |
a50699fd | 234 | |
19d4e9a7 SM |
235 | /* Traverse an interval tree TREE, performing FUNCTION on each node. |
236 | No guarantee is made about the order of traversal. | |
237 | Pass FUNCTION two args: an interval, and ARG. */ | |
238 | ||
239 | void | |
971de7fb | 240 | traverse_intervals_noorder (INTERVAL tree, void (*function) (INTERVAL, Lisp_Object), Lisp_Object arg) |
19d4e9a7 SM |
241 | { |
242 | /* Minimize stack usage. */ | |
77c7bcb1 | 243 | while (tree) |
19d4e9a7 SM |
244 | { |
245 | (*function) (tree, arg); | |
77c7bcb1 | 246 | if (!tree->right) |
19d4e9a7 SM |
247 | tree = tree->left; |
248 | else | |
249 | { | |
250 | traverse_intervals_noorder (tree->left, function, arg); | |
251 | tree = tree->right; | |
252 | } | |
253 | } | |
254 | } | |
255 | ||
a50699fd | 256 | /* Traverse an interval tree TREE, performing FUNCTION on each node. |
4a93c905 | 257 | Pass FUNCTION two args: an interval, and ARG. */ |
a50699fd JA |
258 | |
259 | void | |
d311d28c | 260 | traverse_intervals (INTERVAL tree, ptrdiff_t position, |
e79123aa | 261 | void (*function) (INTERVAL, Lisp_Object), Lisp_Object arg) |
a50699fd | 262 | { |
77c7bcb1 | 263 | while (tree) |
19d4e9a7 | 264 | { |
42005513 | 265 | traverse_intervals (tree->left, position, function, arg); |
19d4e9a7 SM |
266 | position += LEFT_TOTAL_LENGTH (tree); |
267 | tree->position = position; | |
268 | (*function) (tree, arg); | |
42005513 | 269 | position += LENGTH (tree); tree = tree->right; |
19d4e9a7 | 270 | } |
a50699fd JA |
271 | } |
272 | \f | |
273 | #if 0 | |
e39adcda GM |
274 | |
275 | static int icount; | |
276 | static int idepth; | |
277 | static int zero_length; | |
278 | ||
7ce503fd | 279 | /* These functions are temporary, for debugging purposes only. */ |
a50699fd JA |
280 | |
281 | INTERVAL search_interval, found_interval; | |
282 | ||
283 | void | |
1dae0f0a | 284 | check_for_interval (INTERVAL i) |
a50699fd JA |
285 | { |
286 | if (i == search_interval) | |
287 | { | |
288 | found_interval = i; | |
289 | icount++; | |
290 | } | |
291 | } | |
292 | ||
293 | INTERVAL | |
1dae0f0a | 294 | search_for_interval (INTERVAL i, INTERVAL tree) |
a50699fd JA |
295 | { |
296 | icount = 0; | |
297 | search_interval = i; | |
77c7bcb1 | 298 | found_interval = NULL; |
19d4e9a7 | 299 | traverse_intervals_noorder (tree, &check_for_interval, Qnil); |
a50699fd JA |
300 | return found_interval; |
301 | } | |
302 | ||
303 | static void | |
1dae0f0a | 304 | inc_interval_count (INTERVAL i) |
a50699fd JA |
305 | { |
306 | icount++; | |
307 | if (LENGTH (i) == 0) | |
308 | zero_length++; | |
309 | if (depth > idepth) | |
310 | idepth = depth; | |
311 | } | |
312 | ||
313 | int | |
1dae0f0a | 314 | count_intervals (INTERVAL i) |
a50699fd JA |
315 | { |
316 | icount = 0; | |
317 | idepth = 0; | |
318 | zero_length = 0; | |
19d4e9a7 | 319 | traverse_intervals_noorder (i, &inc_interval_count, Qnil); |
a50699fd JA |
320 | |
321 | return icount; | |
322 | } | |
323 | ||
324 | static INTERVAL | |
1dae0f0a | 325 | root_interval (INTERVAL interval) |
a50699fd JA |
326 | { |
327 | register INTERVAL i = interval; | |
328 | ||
329 | while (! ROOT_INTERVAL_P (i)) | |
439d5cb4 | 330 | i = INTERVAL_PARENT (i); |
a50699fd JA |
331 | |
332 | return i; | |
333 | } | |
334 | #endif | |
335 | \f | |
336 | /* Assuming that a left child exists, perform the following operation: | |
337 | ||
338 | A B | |
339 | / \ / \ | |
340 | B => A | |
341 | / \ / \ | |
342 | c c | |
343 | */ | |
344 | ||
b0ab8123 | 345 | static INTERVAL |
971de7fb | 346 | rotate_right (INTERVAL interval) |
a50699fd JA |
347 | { |
348 | INTERVAL i; | |
349 | INTERVAL B = interval->left; | |
d311d28c | 350 | ptrdiff_t old_total = interval->total_length; |
a50699fd | 351 | |
7ce503fd | 352 | /* Deal with any Parent of A; make it point to B. */ |
a50699fd | 353 | if (! ROOT_INTERVAL_P (interval)) |
e39adcda GM |
354 | { |
355 | if (AM_LEFT_CHILD (interval)) | |
0c94c8d6 | 356 | set_interval_left (INTERVAL_PARENT (interval), B); |
e39adcda | 357 | else |
0c94c8d6 | 358 | set_interval_right (INTERVAL_PARENT (interval), B); |
e39adcda | 359 | } |
0c94c8d6 | 360 | copy_interval_parent (B, interval); |
a50699fd | 361 | |
4314dea4 RS |
362 | /* Make B the parent of A */ |
363 | i = B->right; | |
0c94c8d6 PE |
364 | set_interval_right (B, interval); |
365 | set_interval_parent (interval, B); | |
a50699fd | 366 | |
4314dea4 | 367 | /* Make A point to c */ |
0c94c8d6 | 368 | set_interval_left (interval, i); |
77c7bcb1 | 369 | if (i) |
0c94c8d6 | 370 | set_interval_parent (i, interval); |
4314dea4 | 371 | |
550bd63a | 372 | /* A's total length is decreased by the length of B and its left child. */ |
4314dea4 | 373 | interval->total_length -= B->total_length - LEFT_TOTAL_LENGTH (interval); |
908589fd | 374 | eassert (TOTAL_LENGTH (interval) >= 0); |
4314dea4 RS |
375 | |
376 | /* B must have the same total length of A. */ | |
377 | B->total_length = old_total; | |
908589fd | 378 | eassert (TOTAL_LENGTH (B) >= 0); |
a50699fd JA |
379 | |
380 | return B; | |
381 | } | |
4314dea4 | 382 | |
a50699fd JA |
383 | /* Assuming that a right child exists, perform the following operation: |
384 | ||
7d0393cf JB |
385 | A B |
386 | / \ / \ | |
a50699fd | 387 | B => A |
7d0393cf | 388 | / \ / \ |
a50699fd JA |
389 | c c |
390 | */ | |
391 | ||
b0ab8123 | 392 | static INTERVAL |
971de7fb | 393 | rotate_left (INTERVAL interval) |
a50699fd JA |
394 | { |
395 | INTERVAL i; | |
396 | INTERVAL B = interval->right; | |
d311d28c | 397 | ptrdiff_t old_total = interval->total_length; |
a50699fd | 398 | |
4314dea4 | 399 | /* Deal with any parent of A; make it point to B. */ |
a50699fd | 400 | if (! ROOT_INTERVAL_P (interval)) |
e39adcda GM |
401 | { |
402 | if (AM_LEFT_CHILD (interval)) | |
0c94c8d6 | 403 | set_interval_left (INTERVAL_PARENT (interval), B); |
e39adcda | 404 | else |
0c94c8d6 | 405 | set_interval_right (INTERVAL_PARENT (interval), B); |
e39adcda | 406 | } |
0c94c8d6 | 407 | copy_interval_parent (B, interval); |
a50699fd JA |
408 | |
409 | /* Make B the parent of A */ | |
4314dea4 | 410 | i = B->left; |
0c94c8d6 PE |
411 | set_interval_left (B, interval); |
412 | set_interval_parent (interval, B); | |
a50699fd JA |
413 | |
414 | /* Make A point to c */ | |
0c94c8d6 | 415 | set_interval_right (interval, i); |
77c7bcb1 | 416 | if (i) |
0c94c8d6 | 417 | set_interval_parent (i, interval); |
4314dea4 | 418 | |
550bd63a | 419 | /* A's total length is decreased by the length of B and its right child. */ |
4314dea4 | 420 | interval->total_length -= B->total_length - RIGHT_TOTAL_LENGTH (interval); |
908589fd | 421 | eassert (TOTAL_LENGTH (interval) >= 0); |
4314dea4 RS |
422 | |
423 | /* B must have the same total length of A. */ | |
424 | B->total_length = old_total; | |
908589fd | 425 | eassert (TOTAL_LENGTH (B) >= 0); |
a50699fd JA |
426 | |
427 | return B; | |
428 | } | |
429 | \f | |
4314dea4 RS |
430 | /* Balance an interval tree with the assumption that the subtrees |
431 | themselves are already balanced. */ | |
432 | ||
433 | static INTERVAL | |
971de7fb | 434 | balance_an_interval (INTERVAL i) |
4314dea4 | 435 | { |
d311d28c | 436 | register ptrdiff_t old_diff, new_diff; |
4314dea4 RS |
437 | |
438 | while (1) | |
439 | { | |
440 | old_diff = LEFT_TOTAL_LENGTH (i) - RIGHT_TOTAL_LENGTH (i); | |
441 | if (old_diff > 0) | |
442 | { | |
727fec2d | 443 | /* Since the left child is longer, there must be one. */ |
4314dea4 RS |
444 | new_diff = i->total_length - i->left->total_length |
445 | + RIGHT_TOTAL_LENGTH (i->left) - LEFT_TOTAL_LENGTH (i->left); | |
1ea40aa2 | 446 | if (eabs (new_diff) >= old_diff) |
4314dea4 RS |
447 | break; |
448 | i = rotate_right (i); | |
449 | balance_an_interval (i->right); | |
450 | } | |
451 | else if (old_diff < 0) | |
452 | { | |
727fec2d | 453 | /* Since the right child is longer, there must be one. */ |
4314dea4 RS |
454 | new_diff = i->total_length - i->right->total_length |
455 | + LEFT_TOTAL_LENGTH (i->right) - RIGHT_TOTAL_LENGTH (i->right); | |
1ea40aa2 | 456 | if (eabs (new_diff) >= -old_diff) |
4314dea4 RS |
457 | break; |
458 | i = rotate_left (i); | |
459 | balance_an_interval (i->left); | |
460 | } | |
461 | else | |
462 | break; | |
463 | } | |
464 | return i; | |
465 | } | |
466 | ||
467 | /* Balance INTERVAL, potentially stuffing it back into its parent | |
468 | Lisp Object. */ | |
469 | ||
b0ab8123 | 470 | static INTERVAL |
a08d4ba7 | 471 | balance_possible_root_interval (INTERVAL interval) |
4314dea4 RS |
472 | { |
473 | Lisp_Object parent; | |
a08d4ba7 | 474 | bool have_parent = 0; |
4314dea4 | 475 | |
439d5cb4 | 476 | if (!INTERVAL_HAS_OBJECT (interval) && !INTERVAL_HAS_PARENT (interval)) |
4314dea4 RS |
477 | return interval; |
478 | ||
439d5cb4 KR |
479 | if (INTERVAL_HAS_OBJECT (interval)) |
480 | { | |
481 | have_parent = 1; | |
482 | GET_INTERVAL_OBJECT (parent, interval); | |
483 | } | |
4314dea4 RS |
484 | interval = balance_an_interval (interval); |
485 | ||
439d5cb4 KR |
486 | if (have_parent) |
487 | { | |
488 | if (BUFFERP (parent)) | |
0c94c8d6 | 489 | set_buffer_intervals (XBUFFER (parent), interval); |
439d5cb4 | 490 | else if (STRINGP (parent)) |
0c94c8d6 | 491 | set_string_intervals (parent, interval); |
439d5cb4 | 492 | } |
4314dea4 RS |
493 | |
494 | return interval; | |
495 | } | |
496 | ||
497 | /* Balance the interval tree TREE. Balancing is by weight | |
498 | (the amount of text). */ | |
499 | ||
500 | static INTERVAL | |
971de7fb | 501 | balance_intervals_internal (register INTERVAL tree) |
4314dea4 RS |
502 | { |
503 | /* Balance within each side. */ | |
504 | if (tree->left) | |
8f3b9b95 | 505 | balance_intervals_internal (tree->left); |
4314dea4 | 506 | if (tree->right) |
8f3b9b95 | 507 | balance_intervals_internal (tree->right); |
4314dea4 RS |
508 | return balance_an_interval (tree); |
509 | } | |
510 | ||
511 | /* Advertised interface to balance intervals. */ | |
512 | ||
513 | INTERVAL | |
971de7fb | 514 | balance_intervals (INTERVAL tree) |
4314dea4 | 515 | { |
77c7bcb1 | 516 | return tree ? balance_intervals_internal (tree) : NULL; |
4314dea4 | 517 | } |
77c7bcb1 | 518 | |
8707c1e5 DA |
519 | /* Rebalance text properties of B. */ |
520 | ||
521 | static void | |
522 | buffer_balance_intervals (struct buffer *b) | |
523 | { | |
524 | INTERVAL i; | |
525 | ||
526 | eassert (b != NULL); | |
0c94c8d6 | 527 | i = buffer_intervals (b); |
8707c1e5 | 528 | if (i) |
0c94c8d6 | 529 | set_buffer_intervals (b, balance_an_interval (i)); |
8707c1e5 DA |
530 | } |
531 | ||
2bc7a79b JB |
532 | /* Split INTERVAL into two pieces, starting the second piece at |
533 | character position OFFSET (counting from 0), relative to INTERVAL. | |
534 | INTERVAL becomes the left-hand piece, and the right-hand piece | |
535 | (second, lexicographically) is returned. | |
90ba40fc JA |
536 | |
537 | The size and position fields of the two intervals are set based upon | |
538 | those of the original interval. The property list of the new interval | |
539 | is reset, thus it is up to the caller to do the right thing with the | |
540 | result. | |
a50699fd JA |
541 | |
542 | Note that this does not change the position of INTERVAL; if it is a root, | |
7ce503fd | 543 | it is still a root after this operation. */ |
a50699fd JA |
544 | |
545 | INTERVAL | |
d311d28c | 546 | split_interval_right (INTERVAL interval, ptrdiff_t offset) |
a50699fd JA |
547 | { |
548 | INTERVAL new = make_interval (); | |
d311d28c PE |
549 | ptrdiff_t position = interval->position; |
550 | ptrdiff_t new_length = LENGTH (interval) - offset; | |
a50699fd | 551 | |
2bc7a79b | 552 | new->position = position + offset; |
0c94c8d6 | 553 | set_interval_parent (new, interval); |
a50699fd | 554 | |
4314dea4 | 555 | if (NULL_RIGHT_CHILD (interval)) |
a50699fd | 556 | { |
0c94c8d6 | 557 | set_interval_right (interval, new); |
a50699fd | 558 | new->total_length = new_length; |
908589fd | 559 | eassert (TOTAL_LENGTH (new) >= 0); |
a50699fd | 560 | } |
cc6e2aaa RS |
561 | else |
562 | { | |
563 | /* Insert the new node between INTERVAL and its right child. */ | |
0c94c8d6 PE |
564 | set_interval_right (new, interval->right); |
565 | set_interval_parent (interval->right, new); | |
566 | set_interval_right (interval, new); | |
cc6e2aaa | 567 | new->total_length = new_length + new->right->total_length; |
908589fd | 568 | eassert (TOTAL_LENGTH (new) >= 0); |
cc6e2aaa RS |
569 | balance_an_interval (new); |
570 | } | |
7d0393cf | 571 | |
4314dea4 RS |
572 | balance_possible_root_interval (interval); |
573 | ||
a50699fd JA |
574 | return new; |
575 | } | |
576 | ||
2bc7a79b JB |
577 | /* Split INTERVAL into two pieces, starting the second piece at |
578 | character position OFFSET (counting from 0), relative to INTERVAL. | |
579 | INTERVAL becomes the right-hand piece, and the left-hand piece | |
580 | (first, lexicographically) is returned. | |
a50699fd | 581 | |
90ba40fc JA |
582 | The size and position fields of the two intervals are set based upon |
583 | those of the original interval. The property list of the new interval | |
584 | is reset, thus it is up to the caller to do the right thing with the | |
585 | result. | |
586 | ||
587 | Note that this does not change the position of INTERVAL; if it is a root, | |
7ce503fd | 588 | it is still a root after this operation. */ |
a50699fd JA |
589 | |
590 | INTERVAL | |
d311d28c | 591 | split_interval_left (INTERVAL interval, ptrdiff_t offset) |
a50699fd JA |
592 | { |
593 | INTERVAL new = make_interval (); | |
d311d28c | 594 | ptrdiff_t new_length = offset; |
a50699fd | 595 | |
a50699fd | 596 | new->position = interval->position; |
2bc7a79b | 597 | interval->position = interval->position + offset; |
0c94c8d6 | 598 | set_interval_parent (new, interval); |
a50699fd JA |
599 | |
600 | if (NULL_LEFT_CHILD (interval)) | |
601 | { | |
0c94c8d6 | 602 | set_interval_left (interval, new); |
a50699fd | 603 | new->total_length = new_length; |
908589fd | 604 | eassert (TOTAL_LENGTH (new) >= 0); |
a50699fd | 605 | } |
cc6e2aaa RS |
606 | else |
607 | { | |
608 | /* Insert the new node between INTERVAL and its left child. */ | |
0c94c8d6 PE |
609 | set_interval_left (new, interval->left); |
610 | set_interval_parent (new->left, new); | |
611 | set_interval_left (interval, new); | |
cc6e2aaa | 612 | new->total_length = new_length + new->left->total_length; |
908589fd | 613 | eassert (TOTAL_LENGTH (new) >= 0); |
cc6e2aaa RS |
614 | balance_an_interval (new); |
615 | } | |
7d0393cf | 616 | |
4314dea4 | 617 | balance_possible_root_interval (interval); |
a50699fd JA |
618 | |
619 | return new; | |
620 | } | |
621 | \f | |
944d4e4b KH |
622 | /* Return the proper position for the first character |
623 | described by the interval tree SOURCE. | |
624 | This is 1 if the parent is a buffer, | |
625 | 0 if the parent is a string or if there is no parent. | |
626 | ||
627 | Don't use this function on an interval which is the child | |
628 | of another interval! */ | |
629 | ||
fc7bf025 | 630 | static int |
971de7fb | 631 | interval_start_pos (INTERVAL source) |
944d4e4b KH |
632 | { |
633 | Lisp_Object parent; | |
634 | ||
77c7bcb1 | 635 | if (!source) |
944d4e4b KH |
636 | return 0; |
637 | ||
e0b8c689 KR |
638 | if (! INTERVAL_HAS_OBJECT (source)) |
639 | return 0; | |
439d5cb4 | 640 | GET_INTERVAL_OBJECT (parent, source); |
944d4e4b KH |
641 | if (BUFFERP (parent)) |
642 | return BUF_BEG (XBUFFER (parent)); | |
643 | return 0; | |
644 | } | |
645 | ||
90ba40fc | 646 | /* Find the interval containing text position POSITION in the text |
24e3d3bf | 647 | represented by the interval tree TREE. POSITION is a buffer |
944d4e4b KH |
648 | position (starting from 1) or a string index (starting from 0). |
649 | If POSITION is at the end of the buffer or string, | |
650 | return the interval containing the last character. | |
a50699fd | 651 | |
90ba40fc JA |
652 | The `position' field, which is a cache of an interval's position, |
653 | is updated in the interval found. Other functions (e.g., next_interval) | |
7ce503fd | 654 | will update this cache based on the result of find_interval. */ |
90ba40fc | 655 | |
1863bbf8 | 656 | INTERVAL |
d311d28c | 657 | find_interval (register INTERVAL tree, register ptrdiff_t position) |
a50699fd | 658 | { |
24e3d3bf JB |
659 | /* The distance from the left edge of the subtree at TREE |
660 | to POSITION. */ | |
d311d28c | 661 | register ptrdiff_t relative_position; |
a50699fd | 662 | |
77c7bcb1 DA |
663 | if (!tree) |
664 | return NULL; | |
a50699fd | 665 | |
944d4e4b | 666 | relative_position = position; |
439d5cb4 KR |
667 | if (INTERVAL_HAS_OBJECT (tree)) |
668 | { | |
669 | Lisp_Object parent; | |
670 | GET_INTERVAL_OBJECT (parent, tree); | |
671 | if (BUFFERP (parent)) | |
672 | relative_position -= BUF_BEG (XBUFFER (parent)); | |
673 | } | |
944d4e4b | 674 | |
cce7fefc | 675 | eassert (relative_position <= TOTAL_LENGTH (tree)); |
a50699fd | 676 | |
0caaedb1 | 677 | tree = balance_possible_root_interval (tree); |
4314dea4 | 678 | |
a50699fd JA |
679 | while (1) |
680 | { | |
24e3d3bf | 681 | if (relative_position < LEFT_TOTAL_LENGTH (tree)) |
a50699fd JA |
682 | { |
683 | tree = tree->left; | |
684 | } | |
24e3d3bf JB |
685 | else if (! NULL_RIGHT_CHILD (tree) |
686 | && relative_position >= (TOTAL_LENGTH (tree) | |
687 | - RIGHT_TOTAL_LENGTH (tree))) | |
a50699fd JA |
688 | { |
689 | relative_position -= (TOTAL_LENGTH (tree) | |
690 | - RIGHT_TOTAL_LENGTH (tree)); | |
691 | tree = tree->right; | |
692 | } | |
693 | else | |
694 | { | |
944d4e4b | 695 | tree->position |
4867a283 SM |
696 | = (position - relative_position /* left edge of *tree. */ |
697 | + LEFT_TOTAL_LENGTH (tree)); /* left edge of this interval. */ | |
24e3d3bf | 698 | |
a50699fd JA |
699 | return tree; |
700 | } | |
701 | } | |
702 | } | |
703 | \f | |
704 | /* Find the succeeding interval (lexicographically) to INTERVAL. | |
90ba40fc | 705 | Sets the `position' field based on that of INTERVAL (see |
7ce503fd | 706 | find_interval). */ |
a50699fd JA |
707 | |
708 | INTERVAL | |
971de7fb | 709 | next_interval (register INTERVAL interval) |
a50699fd JA |
710 | { |
711 | register INTERVAL i = interval; | |
d311d28c | 712 | register ptrdiff_t next_position; |
a50699fd | 713 | |
77c7bcb1 DA |
714 | if (!i) |
715 | return NULL; | |
a50699fd JA |
716 | next_position = interval->position + LENGTH (interval); |
717 | ||
718 | if (! NULL_RIGHT_CHILD (i)) | |
719 | { | |
720 | i = i->right; | |
721 | while (! NULL_LEFT_CHILD (i)) | |
722 | i = i->left; | |
723 | ||
724 | i->position = next_position; | |
725 | return i; | |
726 | } | |
727 | ||
728 | while (! NULL_PARENT (i)) | |
729 | { | |
730 | if (AM_LEFT_CHILD (i)) | |
731 | { | |
439d5cb4 | 732 | i = INTERVAL_PARENT (i); |
a50699fd JA |
733 | i->position = next_position; |
734 | return i; | |
735 | } | |
736 | ||
439d5cb4 | 737 | i = INTERVAL_PARENT (i); |
a50699fd JA |
738 | } |
739 | ||
77c7bcb1 | 740 | return NULL; |
a50699fd JA |
741 | } |
742 | ||
743 | /* Find the preceding interval (lexicographically) to INTERVAL. | |
90ba40fc | 744 | Sets the `position' field based on that of INTERVAL (see |
7ce503fd | 745 | find_interval). */ |
a50699fd JA |
746 | |
747 | INTERVAL | |
971de7fb | 748 | previous_interval (register INTERVAL interval) |
a50699fd JA |
749 | { |
750 | register INTERVAL i; | |
a50699fd | 751 | |
77c7bcb1 DA |
752 | if (!interval) |
753 | return NULL; | |
a50699fd JA |
754 | |
755 | if (! NULL_LEFT_CHILD (interval)) | |
756 | { | |
757 | i = interval->left; | |
758 | while (! NULL_RIGHT_CHILD (i)) | |
759 | i = i->right; | |
760 | ||
761 | i->position = interval->position - LENGTH (i); | |
762 | return i; | |
763 | } | |
764 | ||
765 | i = interval; | |
766 | while (! NULL_PARENT (i)) | |
767 | { | |
768 | if (AM_RIGHT_CHILD (i)) | |
769 | { | |
439d5cb4 | 770 | i = INTERVAL_PARENT (i); |
a50699fd JA |
771 | |
772 | i->position = interval->position - LENGTH (i); | |
773 | return i; | |
774 | } | |
439d5cb4 | 775 | i = INTERVAL_PARENT (i); |
a50699fd JA |
776 | } |
777 | ||
77c7bcb1 | 778 | return NULL; |
a50699fd | 779 | } |
25eeac41 RS |
780 | |
781 | /* Find the interval containing POS given some non-NULL INTERVAL | |
75167cd4 | 782 | in the same tree. Note that we need to update interval->position |
52283633 SM |
783 | if we go down the tree. |
784 | To speed up the process, we assume that the ->position of | |
785 | I and all its parents is already uptodate. */ | |
25eeac41 | 786 | INTERVAL |
d311d28c | 787 | update_interval (register INTERVAL i, ptrdiff_t pos) |
25eeac41 | 788 | { |
77c7bcb1 DA |
789 | if (!i) |
790 | return NULL; | |
25eeac41 | 791 | |
7d0393cf | 792 | while (1) |
25eeac41 | 793 | { |
7d0393cf | 794 | if (pos < i->position) |
25eeac41 RS |
795 | { |
796 | /* Move left. */ | |
7d0393cf | 797 | if (pos >= i->position - TOTAL_LENGTH (i->left)) |
75167cd4 RS |
798 | { |
799 | i->left->position = i->position - TOTAL_LENGTH (i->left) | |
800 | + LEFT_TOTAL_LENGTH (i->left); | |
801 | i = i->left; /* Move to the left child */ | |
802 | } | |
7d0393cf | 803 | else if (NULL_PARENT (i)) |
25eeac41 | 804 | error ("Point before start of properties"); |
7d0393cf | 805 | else |
439d5cb4 | 806 | i = INTERVAL_PARENT (i); |
25eeac41 RS |
807 | continue; |
808 | } | |
809 | else if (pos >= INTERVAL_LAST_POS (i)) | |
810 | { | |
811 | /* Move right. */ | |
7d0393cf | 812 | if (pos < INTERVAL_LAST_POS (i) + TOTAL_LENGTH (i->right)) |
75167cd4 | 813 | { |
70cc95c4 SM |
814 | i->right->position = INTERVAL_LAST_POS (i) |
815 | + LEFT_TOTAL_LENGTH (i->right); | |
75167cd4 RS |
816 | i = i->right; /* Move to the right child */ |
817 | } | |
7d0393cf | 818 | else if (NULL_PARENT (i)) |
7c85f529 | 819 | error ("Point %"pD"d after end of properties", pos); |
7d0393cf | 820 | else |
70cc95c4 | 821 | i = INTERVAL_PARENT (i); |
25eeac41 RS |
822 | continue; |
823 | } | |
7d0393cf | 824 | else |
25eeac41 RS |
825 | return i; |
826 | } | |
827 | } | |
828 | ||
90ba40fc JA |
829 | /* Effect an adjustment corresponding to the addition of LENGTH characters |
830 | of text. Do this by finding the interval containing POSITION in the | |
550bd63a | 831 | interval tree TREE, and then adjusting all of its ancestors by adding |
90ba40fc JA |
832 | LENGTH to them. |
833 | ||
834 | If POSITION is the first character of an interval, meaning that point | |
835 | is actually between the two intervals, make the new text belong to | |
836 | the interval which is "sticky". | |
837 | ||
1d1d7ba0 | 838 | If both intervals are "sticky", then make them belong to the left-most |
90ba40fc | 839 | interval. Another possibility would be to create a new interval for |
7ce503fd | 840 | this text, and make it have the merged properties of both ends. */ |
90ba40fc JA |
841 | |
842 | static INTERVAL | |
e79123aa | 843 | adjust_intervals_for_insertion (INTERVAL tree, |
d311d28c | 844 | ptrdiff_t position, ptrdiff_t length) |
90ba40fc | 845 | { |
a08d4ba7 PE |
846 | INTERVAL i; |
847 | INTERVAL temp; | |
848 | bool eobp = 0; | |
944d4e4b | 849 | Lisp_Object parent; |
d311d28c | 850 | ptrdiff_t offset; |
7d0393cf | 851 | |
cce7fefc | 852 | eassert (TOTAL_LENGTH (tree) > 0); |
90ba40fc | 853 | |
439d5cb4 | 854 | GET_INTERVAL_OBJECT (parent, tree); |
944d4e4b KH |
855 | offset = (BUFFERP (parent) ? BUF_BEG (XBUFFER (parent)) : 0); |
856 | ||
24e3d3bf JB |
857 | /* If inserting at point-max of a buffer, that position will be out |
858 | of range. Remember that buffer positions are 1-based. */ | |
944d4e4b KH |
859 | if (position >= TOTAL_LENGTH (tree) + offset) |
860 | { | |
861 | position = TOTAL_LENGTH (tree) + offset; | |
862 | eobp = 1; | |
863 | } | |
90ba40fc JA |
864 | |
865 | i = find_interval (tree, position); | |
7ce503fd | 866 | |
2313b945 RS |
867 | /* If in middle of an interval which is not sticky either way, |
868 | we must not just give its properties to the insertion. | |
f56b42ac KH |
869 | So split this interval at the insertion point. |
870 | ||
871 | Originally, the if condition here was this: | |
872 | (! (position == i->position || eobp) | |
873 | && END_NONSTICKY_P (i) | |
874 | && FRONT_NONSTICKY_P (i)) | |
875 | But, these macros are now unreliable because of introduction of | |
876 | Vtext_property_default_nonsticky. So, we always check properties | |
877 | one by one if POSITION is in middle of an interval. */ | |
878 | if (! (position == i->position || eobp)) | |
2313b945 | 879 | { |
ca41a64d RS |
880 | Lisp_Object tail; |
881 | Lisp_Object front, rear; | |
882 | ||
f56b42ac KH |
883 | tail = i->plist; |
884 | ||
885 | /* Properties font-sticky and rear-nonsticky override | |
886 | Vtext_property_default_nonsticky. So, if they are t, we can | |
887 | skip one by one checking of properties. */ | |
888 | rear = textget (i->plist, Qrear_nonsticky); | |
889 | if (! CONSP (rear) && ! NILP (rear)) | |
890 | { | |
891 | /* All properties are nonsticky. We split the interval. */ | |
892 | goto check_done; | |
893 | } | |
ca41a64d | 894 | front = textget (i->plist, Qfront_sticky); |
f56b42ac KH |
895 | if (! CONSP (front) && ! NILP (front)) |
896 | { | |
897 | /* All properties are sticky. We don't split the interval. */ | |
898 | tail = Qnil; | |
899 | goto check_done; | |
900 | } | |
ca41a64d | 901 | |
f56b42ac KH |
902 | /* Does any actual property pose an actual problem? We break |
903 | the loop if we find a nonsticky property. */ | |
904 | for (; CONSP (tail); tail = Fcdr (XCDR (tail))) | |
ca41a64d | 905 | { |
f56b42ac | 906 | Lisp_Object prop, tmp; |
03699b14 | 907 | prop = XCAR (tail); |
ca41a64d | 908 | |
f56b42ac | 909 | /* Is this particular property front-sticky? */ |
ca41a64d RS |
910 | if (CONSP (front) && ! NILP (Fmemq (prop, front))) |
911 | continue; | |
912 | ||
f56b42ac KH |
913 | /* Is this particular property rear-nonsticky? */ |
914 | if (CONSP (rear) && ! NILP (Fmemq (prop, rear))) | |
915 | break; | |
916 | ||
917 | /* Is this particular property recorded as sticky or | |
918 | nonsticky in Vtext_property_default_nonsticky? */ | |
919 | tmp = Fassq (prop, Vtext_property_default_nonsticky); | |
920 | if (CONSP (tmp)) | |
921 | { | |
922 | if (NILP (tmp)) | |
923 | continue; | |
924 | break; | |
925 | } | |
926 | ||
927 | /* By default, a text property is rear-sticky, thus we | |
928 | continue the loop. */ | |
ca41a64d RS |
929 | } |
930 | ||
f56b42ac | 931 | check_done: |
ca41a64d RS |
932 | /* If any property is a real problem, split the interval. */ |
933 | if (! NILP (tail)) | |
934 | { | |
935 | temp = split_interval_right (i, position - i->position); | |
936 | copy_properties (i, temp); | |
937 | i = temp; | |
938 | } | |
2313b945 RS |
939 | } |
940 | ||
90ba40fc | 941 | /* If we are positioned between intervals, check the stickiness of |
7ce503fd RS |
942 | both of them. We have to do this too, if we are at BEG or Z. */ |
943 | if (position == i->position || eobp) | |
90ba40fc | 944 | { |
7ce503fd RS |
945 | register INTERVAL prev; |
946 | ||
947 | if (position == BEG) | |
948 | prev = 0; | |
949 | else if (eobp) | |
950 | { | |
951 | prev = i; | |
952 | i = 0; | |
953 | } | |
954 | else | |
955 | prev = previous_interval (i); | |
90ba40fc | 956 | |
7ce503fd RS |
957 | /* Even if we are positioned between intervals, we default |
958 | to the left one if it exists. We extend it now and split | |
8e6208c5 | 959 | off a part later, if stickiness demands it. */ |
439d5cb4 | 960 | for (temp = prev ? prev : i; temp; temp = INTERVAL_PARENT_OR_NULL (temp)) |
4314dea4 RS |
961 | { |
962 | temp->total_length += length; | |
908589fd | 963 | eassert (TOTAL_LENGTH (temp) >= 0); |
4314dea4 RS |
964 | temp = balance_possible_root_interval (temp); |
965 | } | |
7d0393cf | 966 | |
7ce503fd | 967 | /* If at least one interval has sticky properties, |
f56b42ac KH |
968 | we check the stickiness property by property. |
969 | ||
970 | Originally, the if condition here was this: | |
971 | (END_NONSTICKY_P (prev) || FRONT_STICKY_P (i)) | |
972 | But, these macros are now unreliable because of introduction | |
973 | of Vtext_property_default_nonsticky. So, we always have to | |
974 | check stickiness of properties one by one. If cache of | |
975 | stickiness is implemented in the future, we may be able to | |
976 | use those macros again. */ | |
977 | if (1) | |
7ce503fd | 978 | { |
dd675b05 | 979 | Lisp_Object pleft, pright; |
7ce503fd RS |
980 | struct interval newi; |
981 | ||
0c16dfed | 982 | RESET_INTERVAL (&newi); |
77c7bcb1 DA |
983 | pleft = prev ? prev->plist : Qnil; |
984 | pright = i ? i->plist : Qnil; | |
0c94c8d6 | 985 | set_interval_plist (&newi, merge_properties_sticky (pleft, pright)); |
7ce503fd | 986 | |
ef1900f3 | 987 | if (! prev) /* i.e. position == BEG */ |
7ce503fd RS |
988 | { |
989 | if (! intervals_equal (i, &newi)) | |
990 | { | |
991 | i = split_interval_left (i, length); | |
0c94c8d6 | 992 | set_interval_plist (i, newi.plist); |
7ce503fd RS |
993 | } |
994 | } | |
995 | else if (! intervals_equal (prev, &newi)) | |
996 | { | |
6a3d20cc | 997 | prev = split_interval_right (prev, position - prev->position); |
0c94c8d6 | 998 | set_interval_plist (prev, newi.plist); |
77c7bcb1 | 999 | if (i && intervals_equal (prev, i)) |
7ce503fd RS |
1000 | merge_interval_right (prev); |
1001 | } | |
1002 | ||
1003 | /* We will need to update the cache here later. */ | |
1004 | } | |
1005 | else if (! prev && ! NILP (i->plist)) | |
1006 | { | |
1007 | /* Just split off a new interval at the left. | |
1008 | Since I wasn't front-sticky, the empty plist is ok. */ | |
1009 | i = split_interval_left (i, length); | |
1010 | } | |
90ba40fc JA |
1011 | } |
1012 | ||
7ce503fd RS |
1013 | /* Otherwise just extend the interval. */ |
1014 | else | |
90ba40fc | 1015 | { |
439d5cb4 | 1016 | for (temp = i; temp; temp = INTERVAL_PARENT_OR_NULL (temp)) |
4314dea4 RS |
1017 | { |
1018 | temp->total_length += length; | |
908589fd | 1019 | eassert (TOTAL_LENGTH (temp) >= 0); |
4314dea4 RS |
1020 | temp = balance_possible_root_interval (temp); |
1021 | } | |
90ba40fc | 1022 | } |
7d0393cf | 1023 | |
90ba40fc JA |
1024 | return tree; |
1025 | } | |
7ce503fd | 1026 | |
45d82bdc KH |
1027 | /* Any property might be front-sticky on the left, rear-sticky on the left, |
1028 | front-sticky on the right, or rear-sticky on the right; the 16 combinations | |
1029 | can be arranged in a matrix with rows denoting the left conditions and | |
1030 | columns denoting the right conditions: | |
1031 | _ __ _ | |
1032 | _ FR FR FR FR | |
1033 | FR__ 0 1 2 3 | |
1034 | _FR 4 5 6 7 | |
1035 | FR 8 9 A B | |
1036 | FR C D E F | |
1037 | ||
1038 | left-props = '(front-sticky (p8 p9 pa pb pc pd pe pf) | |
1039 | rear-nonsticky (p4 p5 p6 p7 p8 p9 pa pb) | |
1040 | p0 L p1 L p2 L p3 L p4 L p5 L p6 L p7 L | |
1041 | p8 L p9 L pa L pb L pc L pd L pe L pf L) | |
1042 | right-props = '(front-sticky (p2 p3 p6 p7 pa pb pe pf) | |
1043 | rear-nonsticky (p1 p2 p5 p6 p9 pa pd pe) | |
1044 | p0 R p1 R p2 R p3 R p4 R p5 R p6 R p7 R | |
1045 | p8 R p9 R pa R pb R pc R pd R pe R pf R) | |
1046 | ||
1047 | We inherit from whoever has a sticky side facing us. If both sides | |
1048 | do (cases 2, 3, E, and F), then we inherit from whichever side has a | |
1049 | non-nil value for the current property. If both sides do, then we take | |
1050 | from the left. | |
1051 | ||
1052 | When we inherit a property, we get its stickiness as well as its value. | |
1053 | So, when we merge the above two lists, we expect to get this: | |
1054 | ||
1055 | result = '(front-sticky (p6 p7 pa pb pc pd pe pf) | |
1056 | rear-nonsticky (p6 pa) | |
1057 | p0 L p1 L p2 L p3 L p6 R p7 R | |
1058 | pa R pb R pc L pd L pe L pf L) | |
1059 | ||
1060 | The optimizable special cases are: | |
1061 | left rear-nonsticky = nil, right front-sticky = nil (inherit left) | |
1062 | left rear-nonsticky = t, right front-sticky = t (inherit right) | |
1063 | left rear-nonsticky = t, right front-sticky = nil (inherit none) | |
1064 | */ | |
1065 | ||
93ea6e8f | 1066 | static Lisp_Object |
971de7fb | 1067 | merge_properties_sticky (Lisp_Object pleft, Lisp_Object pright) |
7ce503fd | 1068 | { |
a08d4ba7 | 1069 | Lisp_Object props, front, rear; |
dd675b05 | 1070 | Lisp_Object lfront, lrear, rfront, rrear; |
a08d4ba7 PE |
1071 | Lisp_Object tail1, tail2, sym, lval, rval, cat; |
1072 | bool use_left, use_right, lpresent; | |
7ce503fd | 1073 | |
dd675b05 KH |
1074 | props = Qnil; |
1075 | front = Qnil; | |
1076 | rear = Qnil; | |
1077 | lfront = textget (pleft, Qfront_sticky); | |
1078 | lrear = textget (pleft, Qrear_nonsticky); | |
1079 | rfront = textget (pright, Qfront_sticky); | |
1080 | rrear = textget (pright, Qrear_nonsticky); | |
1081 | ||
45d82bdc | 1082 | /* Go through each element of PRIGHT. */ |
4867a283 | 1083 | for (tail1 = pright; CONSP (tail1); tail1 = Fcdr (XCDR (tail1))) |
7ce503fd | 1084 | { |
f56b42ac KH |
1085 | Lisp_Object tmp; |
1086 | ||
4867a283 | 1087 | sym = XCAR (tail1); |
7ce503fd RS |
1088 | |
1089 | /* Sticky properties get special treatment. */ | |
1090 | if (EQ (sym, Qrear_nonsticky) || EQ (sym, Qfront_sticky)) | |
1091 | continue; | |
45d82bdc | 1092 | |
4867a283 SM |
1093 | rval = Fcar (XCDR (tail1)); |
1094 | for (tail2 = pleft; CONSP (tail2); tail2 = Fcdr (XCDR (tail2))) | |
1095 | if (EQ (sym, XCAR (tail2))) | |
45d82bdc | 1096 | break; |
45d82bdc | 1097 | |
4ab19eb3 RS |
1098 | /* Indicate whether the property is explicitly defined on the left. |
1099 | (We know it is defined explicitly on the right | |
1100 | because otherwise we don't get here.) */ | |
1101 | lpresent = ! NILP (tail2); | |
1102 | lval = (NILP (tail2) ? Qnil : Fcar (Fcdr (tail2))); | |
1103 | ||
f56b42ac KH |
1104 | /* Even if lrear or rfront say nothing about the stickiness of |
1105 | SYM, Vtext_property_default_nonsticky may give default | |
1106 | stickiness to SYM. */ | |
1107 | tmp = Fassq (sym, Vtext_property_default_nonsticky); | |
1108 | use_left = (lpresent | |
1109 | && ! (TMEM (sym, lrear) | |
3a62677f | 1110 | || (CONSP (tmp) && ! NILP (XCDR (tmp))))); |
f56b42ac KH |
1111 | use_right = (TMEM (sym, rfront) |
1112 | || (CONSP (tmp) && NILP (XCDR (tmp)))); | |
45d82bdc KH |
1113 | if (use_left && use_right) |
1114 | { | |
4ab19eb3 RS |
1115 | if (NILP (lval)) |
1116 | use_left = 0; | |
1117 | else if (NILP (rval)) | |
1118 | use_right = 0; | |
45d82bdc KH |
1119 | } |
1120 | if (use_left) | |
7ce503fd | 1121 | { |
45d82bdc KH |
1122 | /* We build props as (value sym ...) rather than (sym value ...) |
1123 | because we plan to nreverse it when we're done. */ | |
4ab19eb3 | 1124 | props = Fcons (lval, Fcons (sym, props)); |
45d82bdc | 1125 | if (TMEM (sym, lfront)) |
7ce503fd | 1126 | front = Fcons (sym, front); |
45d82bdc KH |
1127 | if (TMEM (sym, lrear)) |
1128 | rear = Fcons (sym, rear); | |
7ce503fd | 1129 | } |
45d82bdc | 1130 | else if (use_right) |
7ce503fd | 1131 | { |
4ab19eb3 | 1132 | props = Fcons (rval, Fcons (sym, props)); |
45d82bdc KH |
1133 | if (TMEM (sym, rfront)) |
1134 | front = Fcons (sym, front); | |
1135 | if (TMEM (sym, rrear)) | |
1136 | rear = Fcons (sym, rear); | |
7ce503fd RS |
1137 | } |
1138 | } | |
45d82bdc KH |
1139 | |
1140 | /* Now go through each element of PLEFT. */ | |
4867a283 | 1141 | for (tail2 = pleft; CONSP (tail2); tail2 = Fcdr (XCDR (tail2))) |
7ce503fd | 1142 | { |
f56b42ac KH |
1143 | Lisp_Object tmp; |
1144 | ||
4867a283 | 1145 | sym = XCAR (tail2); |
7ce503fd RS |
1146 | |
1147 | /* Sticky properties get special treatment. */ | |
1148 | if (EQ (sym, Qrear_nonsticky) || EQ (sym, Qfront_sticky)) | |
1149 | continue; | |
1150 | ||
45d82bdc | 1151 | /* If sym is in PRIGHT, we've already considered it. */ |
4867a283 SM |
1152 | for (tail1 = pright; CONSP (tail1); tail1 = Fcdr (XCDR (tail1))) |
1153 | if (EQ (sym, XCAR (tail1))) | |
7ce503fd | 1154 | break; |
45d82bdc KH |
1155 | if (! NILP (tail1)) |
1156 | continue; | |
1157 | ||
4867a283 | 1158 | lval = Fcar (XCDR (tail2)); |
45d82bdc | 1159 | |
f56b42ac KH |
1160 | /* Even if lrear or rfront say nothing about the stickiness of |
1161 | SYM, Vtext_property_default_nonsticky may give default | |
1162 | stickiness to SYM. */ | |
1163 | tmp = Fassq (sym, Vtext_property_default_nonsticky); | |
1164 | ||
45d82bdc | 1165 | /* Since rval is known to be nil in this loop, the test simplifies. */ |
f56b42ac | 1166 | if (! (TMEM (sym, lrear) || (CONSP (tmp) && ! NILP (XCDR (tmp))))) |
7ce503fd | 1167 | { |
4ab19eb3 | 1168 | props = Fcons (lval, Fcons (sym, props)); |
45d82bdc KH |
1169 | if (TMEM (sym, lfront)) |
1170 | front = Fcons (sym, front); | |
1171 | } | |
f56b42ac | 1172 | else if (TMEM (sym, rfront) || (CONSP (tmp) && NILP (XCDR (tmp)))) |
45d82bdc KH |
1173 | { |
1174 | /* The value is nil, but we still inherit the stickiness | |
1175 | from the right. */ | |
7ce503fd | 1176 | front = Fcons (sym, front); |
45d82bdc | 1177 | if (TMEM (sym, rrear)) |
7ce503fd RS |
1178 | rear = Fcons (sym, rear); |
1179 | } | |
1180 | } | |
550bd63a | 1181 | props = Fnreverse (props); |
7ce503fd | 1182 | if (! NILP (rear)) |
550bd63a | 1183 | props = Fcons (Qrear_nonsticky, Fcons (Fnreverse (rear), props)); |
4ab19eb3 RS |
1184 | |
1185 | cat = textget (props, Qcategory); | |
1186 | if (! NILP (front) | |
7d0393cf | 1187 | && |
4ab19eb3 RS |
1188 | /* If we have inherited a front-stick category property that is t, |
1189 | we don't need to set up a detailed one. */ | |
1190 | ! (! NILP (cat) && SYMBOLP (cat) | |
1191 | && EQ (Fget (cat, Qfront_sticky), Qt))) | |
45d82bdc | 1192 | props = Fcons (Qfront_sticky, Fcons (Fnreverse (front), props)); |
7ce503fd | 1193 | return props; |
7ce503fd RS |
1194 | } |
1195 | ||
a50699fd | 1196 | \f |
7d0393cf | 1197 | /* Delete a node I from its interval tree by merging its subtrees |
90ba40fc | 1198 | into one subtree which is then returned. Caller is responsible for |
7ce503fd | 1199 | storing the resulting subtree into its parent. */ |
a50699fd JA |
1200 | |
1201 | static INTERVAL | |
971de7fb | 1202 | delete_node (register INTERVAL i) |
a50699fd JA |
1203 | { |
1204 | register INTERVAL migrate, this; | |
d311d28c | 1205 | register ptrdiff_t migrate_amt; |
a50699fd | 1206 | |
77c7bcb1 | 1207 | if (!i->left) |
a50699fd | 1208 | return i->right; |
77c7bcb1 | 1209 | if (!i->right) |
a50699fd JA |
1210 | return i->left; |
1211 | ||
1212 | migrate = i->left; | |
1213 | migrate_amt = i->left->total_length; | |
1214 | this = i->right; | |
1215 | this->total_length += migrate_amt; | |
77c7bcb1 | 1216 | while (this->left) |
a50699fd JA |
1217 | { |
1218 | this = this->left; | |
1219 | this->total_length += migrate_amt; | |
1220 | } | |
908589fd | 1221 | eassert (TOTAL_LENGTH (this) >= 0); |
0c94c8d6 PE |
1222 | set_interval_left (this, migrate); |
1223 | set_interval_parent (migrate, this); | |
a50699fd JA |
1224 | |
1225 | return i->right; | |
1226 | } | |
1227 | ||
1228 | /* Delete interval I from its tree by calling `delete_node' | |
1229 | and properly connecting the resultant subtree. | |
1230 | ||
1231 | I is presumed to be empty; that is, no adjustments are made | |
7ce503fd | 1232 | for the length of I. */ |
a50699fd | 1233 | |
93ea6e8f | 1234 | static void |
971de7fb | 1235 | delete_interval (register INTERVAL i) |
a50699fd JA |
1236 | { |
1237 | register INTERVAL parent; | |
d311d28c | 1238 | ptrdiff_t amt = LENGTH (i); |
a50699fd | 1239 | |
cce7fefc | 1240 | eassert (amt == 0); /* Only used on zero-length intervals now. */ |
a50699fd JA |
1241 | |
1242 | if (ROOT_INTERVAL_P (i)) | |
1243 | { | |
dd675b05 | 1244 | Lisp_Object owner; |
439d5cb4 | 1245 | GET_INTERVAL_OBJECT (owner, i); |
a50699fd | 1246 | parent = delete_node (i); |
77c7bcb1 | 1247 | if (parent) |
0c94c8d6 | 1248 | set_interval_object (parent, owner); |
a50699fd | 1249 | |
b629dd47 | 1250 | if (BUFFERP (owner)) |
0c94c8d6 | 1251 | set_buffer_intervals (XBUFFER (owner), parent); |
b629dd47 | 1252 | else if (STRINGP (owner)) |
0c94c8d6 | 1253 | set_string_intervals (owner, parent); |
a50699fd | 1254 | else |
1088b922 | 1255 | emacs_abort (); |
a50699fd JA |
1256 | |
1257 | return; | |
1258 | } | |
1259 | ||
439d5cb4 | 1260 | parent = INTERVAL_PARENT (i); |
a50699fd JA |
1261 | if (AM_LEFT_CHILD (i)) |
1262 | { | |
0c94c8d6 | 1263 | set_interval_left (parent, delete_node (i)); |
77c7bcb1 | 1264 | if (parent->left) |
0c94c8d6 | 1265 | set_interval_parent (parent->left, parent); |
a50699fd JA |
1266 | } |
1267 | else | |
1268 | { | |
0c94c8d6 | 1269 | set_interval_right (parent, delete_node (i)); |
77c7bcb1 | 1270 | if (parent->right) |
0c94c8d6 | 1271 | set_interval_parent (parent->right, parent); |
a50699fd JA |
1272 | } |
1273 | } | |
1274 | \f | |
24e3d3bf JB |
1275 | /* Find the interval in TREE corresponding to the relative position |
1276 | FROM and delete as much as possible of AMOUNT from that interval. | |
1277 | Return the amount actually deleted, and if the interval was | |
1278 | zeroed-out, delete that interval node from the tree. | |
1279 | ||
1280 | Note that FROM is actually origin zero, aka relative to the | |
1281 | leftmost edge of tree. This is appropriate since we call ourselves | |
1282 | recursively on subtrees. | |
a50699fd | 1283 | |
1d1d7ba0 | 1284 | Do this by recursing down TREE to the interval in question, and |
7ce503fd | 1285 | deleting the appropriate amount of text. */ |
a50699fd | 1286 | |
d311d28c PE |
1287 | static ptrdiff_t |
1288 | interval_deletion_adjustment (register INTERVAL tree, register ptrdiff_t from, | |
1289 | register ptrdiff_t amount) | |
a50699fd | 1290 | { |
d311d28c | 1291 | register ptrdiff_t relative_position = from; |
a50699fd | 1292 | |
77c7bcb1 | 1293 | if (!tree) |
a50699fd JA |
1294 | return 0; |
1295 | ||
b50a28de | 1296 | /* Left branch. */ |
24e3d3bf | 1297 | if (relative_position < LEFT_TOTAL_LENGTH (tree)) |
a50699fd | 1298 | { |
d311d28c | 1299 | ptrdiff_t subtract = interval_deletion_adjustment (tree->left, |
c20db43f PE |
1300 | relative_position, |
1301 | amount); | |
a50699fd | 1302 | tree->total_length -= subtract; |
908589fd | 1303 | eassert (TOTAL_LENGTH (tree) >= 0); |
a50699fd JA |
1304 | return subtract; |
1305 | } | |
b50a28de | 1306 | /* Right branch. */ |
24e3d3bf JB |
1307 | else if (relative_position >= (TOTAL_LENGTH (tree) |
1308 | - RIGHT_TOTAL_LENGTH (tree))) | |
a50699fd | 1309 | { |
d311d28c | 1310 | ptrdiff_t subtract; |
a50699fd JA |
1311 | |
1312 | relative_position -= (tree->total_length | |
1313 | - RIGHT_TOTAL_LENGTH (tree)); | |
1314 | subtract = interval_deletion_adjustment (tree->right, | |
1315 | relative_position, | |
1316 | amount); | |
1317 | tree->total_length -= subtract; | |
908589fd | 1318 | eassert (TOTAL_LENGTH (tree) >= 0); |
a50699fd JA |
1319 | return subtract; |
1320 | } | |
7ce503fd | 1321 | /* Here -- this node. */ |
a50699fd JA |
1322 | else |
1323 | { | |
24e3d3bf | 1324 | /* How much can we delete from this interval? */ |
d311d28c | 1325 | ptrdiff_t my_amount = ((tree->total_length |
e79123aa EZ |
1326 | - RIGHT_TOTAL_LENGTH (tree)) |
1327 | - relative_position); | |
24e3d3bf JB |
1328 | |
1329 | if (amount > my_amount) | |
1330 | amount = my_amount; | |
1331 | ||
1332 | tree->total_length -= amount; | |
908589fd | 1333 | eassert (TOTAL_LENGTH (tree) >= 0); |
24e3d3bf JB |
1334 | if (LENGTH (tree) == 0) |
1335 | delete_interval (tree); | |
7d0393cf | 1336 | |
24e3d3bf | 1337 | return amount; |
a50699fd JA |
1338 | } |
1339 | ||
7ce503fd | 1340 | /* Never reach here. */ |
a50699fd JA |
1341 | } |
1342 | ||
24e3d3bf JB |
1343 | /* Effect the adjustments necessary to the interval tree of BUFFER to |
1344 | correspond to the deletion of LENGTH characters from that buffer | |
1345 | text. The deletion is effected at position START (which is a | |
7ce503fd | 1346 | buffer position, i.e. origin 1). */ |
1d1d7ba0 | 1347 | |
a50699fd | 1348 | static void |
e79123aa | 1349 | adjust_intervals_for_deletion (struct buffer *buffer, |
d311d28c | 1350 | ptrdiff_t start, ptrdiff_t length) |
a50699fd | 1351 | { |
0c94c8d6 PE |
1352 | ptrdiff_t left_to_delete = length; |
1353 | INTERVAL tree = buffer_intervals (buffer); | |
944d4e4b | 1354 | Lisp_Object parent; |
d311d28c | 1355 | ptrdiff_t offset; |
944d4e4b | 1356 | |
439d5cb4 | 1357 | GET_INTERVAL_OBJECT (parent, tree); |
944d4e4b | 1358 | offset = (BUFFERP (parent) ? BUF_BEG (XBUFFER (parent)) : 0); |
a50699fd | 1359 | |
77c7bcb1 | 1360 | if (!tree) |
a50699fd JA |
1361 | return; |
1362 | ||
cce7fefc DA |
1363 | eassert (start <= offset + TOTAL_LENGTH (tree) |
1364 | && start + length <= offset + TOTAL_LENGTH (tree)); | |
24e3d3bf | 1365 | |
a50699fd JA |
1366 | if (length == TOTAL_LENGTH (tree)) |
1367 | { | |
0c94c8d6 | 1368 | set_buffer_intervals (buffer, NULL); |
a50699fd JA |
1369 | return; |
1370 | } | |
1371 | ||
1372 | if (ONLY_INTERVAL_P (tree)) | |
1373 | { | |
1374 | tree->total_length -= length; | |
908589fd | 1375 | eassert (TOTAL_LENGTH (tree) >= 0); |
a50699fd JA |
1376 | return; |
1377 | } | |
1378 | ||
944d4e4b KH |
1379 | if (start > offset + TOTAL_LENGTH (tree)) |
1380 | start = offset + TOTAL_LENGTH (tree); | |
a50699fd JA |
1381 | while (left_to_delete > 0) |
1382 | { | |
944d4e4b | 1383 | left_to_delete -= interval_deletion_adjustment (tree, start - offset, |
a50699fd | 1384 | left_to_delete); |
0c94c8d6 | 1385 | tree = buffer_intervals (buffer); |
a50699fd JA |
1386 | if (left_to_delete == tree->total_length) |
1387 | { | |
0c94c8d6 | 1388 | set_buffer_intervals (buffer, NULL); |
a50699fd JA |
1389 | return; |
1390 | } | |
1391 | } | |
1392 | } | |
1393 | \f | |
eb8c3be9 | 1394 | /* Make the adjustments necessary to the interval tree of BUFFER to |
1d1d7ba0 JA |
1395 | represent an addition or deletion of LENGTH characters starting |
1396 | at position START. Addition or deletion is indicated by the sign | |
56120d6f | 1397 | of LENGTH. */ |
a9041e6c | 1398 | |
09db192c | 1399 | void |
9898bd0e | 1400 | offset_intervals (struct buffer *buffer, ptrdiff_t start, ptrdiff_t length) |
a50699fd | 1401 | { |
0c94c8d6 | 1402 | if (!buffer_intervals (buffer) || length == 0) |
a50699fd JA |
1403 | return; |
1404 | ||
1405 | if (length > 0) | |
0c94c8d6 | 1406 | adjust_intervals_for_insertion (buffer_intervals (buffer), |
8707c1e5 | 1407 | start, length); |
a50699fd | 1408 | else |
37aa2f85 | 1409 | { |
1088b922 | 1410 | lint_assume (- TYPE_MAXIMUM (ptrdiff_t) <= length); |
37aa2f85 PE |
1411 | adjust_intervals_for_deletion (buffer, start, -length); |
1412 | } | |
a50699fd | 1413 | } |
9c79dd1b JA |
1414 | \f |
1415 | /* Merge interval I with its lexicographic successor. The resulting | |
1416 | interval is returned, and has the properties of the original | |
1417 | successor. The properties of I are lost. I is removed from the | |
1418 | interval tree. | |
1419 | ||
1420 | IMPORTANT: | |
1421 | The caller must verify that this is not the last (rightmost) | |
7ce503fd | 1422 | interval. */ |
9c79dd1b | 1423 | |
93ea6e8f | 1424 | static INTERVAL |
971de7fb | 1425 | merge_interval_right (register INTERVAL i) |
9c79dd1b | 1426 | { |
d311d28c | 1427 | register ptrdiff_t absorb = LENGTH (i); |
9c79dd1b JA |
1428 | register INTERVAL successor; |
1429 | ||
7ce503fd | 1430 | /* Find the succeeding interval. */ |
9c79dd1b | 1431 | if (! NULL_RIGHT_CHILD (i)) /* It's below us. Add absorb |
7ce503fd | 1432 | as we descend. */ |
9c79dd1b JA |
1433 | { |
1434 | successor = i->right; | |
1435 | while (! NULL_LEFT_CHILD (successor)) | |
1436 | { | |
1437 | successor->total_length += absorb; | |
908589fd | 1438 | eassert (TOTAL_LENGTH (successor) >= 0); |
9c79dd1b JA |
1439 | successor = successor->left; |
1440 | } | |
1441 | ||
1442 | successor->total_length += absorb; | |
908589fd | 1443 | eassert (TOTAL_LENGTH (successor) >= 0); |
9c79dd1b JA |
1444 | delete_interval (i); |
1445 | return successor; | |
1446 | } | |
1447 | ||
60cfd278 AS |
1448 | /* Zero out this interval. */ |
1449 | i->total_length -= absorb; | |
908589fd | 1450 | eassert (TOTAL_LENGTH (i) >= 0); |
60cfd278 | 1451 | |
9c79dd1b JA |
1452 | successor = i; |
1453 | while (! NULL_PARENT (successor)) /* It's above us. Subtract as | |
7ce503fd | 1454 | we ascend. */ |
9c79dd1b JA |
1455 | { |
1456 | if (AM_LEFT_CHILD (successor)) | |
1457 | { | |
439d5cb4 | 1458 | successor = INTERVAL_PARENT (successor); |
9c79dd1b JA |
1459 | delete_interval (i); |
1460 | return successor; | |
1461 | } | |
1462 | ||
439d5cb4 | 1463 | successor = INTERVAL_PARENT (successor); |
9c79dd1b | 1464 | successor->total_length -= absorb; |
908589fd | 1465 | eassert (TOTAL_LENGTH (successor) >= 0); |
9c79dd1b JA |
1466 | } |
1467 | ||
1468 | /* This must be the rightmost or last interval and cannot | |
7ce503fd | 1469 | be merged right. The caller should have known. */ |
1088b922 | 1470 | emacs_abort (); |
9c79dd1b JA |
1471 | } |
1472 | \f | |
1473 | /* Merge interval I with its lexicographic predecessor. The resulting | |
1474 | interval is returned, and has the properties of the original predecessor. | |
1475 | The properties of I are lost. Interval node I is removed from the tree. | |
1476 | ||
1477 | IMPORTANT: | |
7ce503fd | 1478 | The caller must verify that this is not the first (leftmost) interval. */ |
9c79dd1b JA |
1479 | |
1480 | INTERVAL | |
971de7fb | 1481 | merge_interval_left (register INTERVAL i) |
9c79dd1b | 1482 | { |
d311d28c | 1483 | register ptrdiff_t absorb = LENGTH (i); |
9c79dd1b JA |
1484 | register INTERVAL predecessor; |
1485 | ||
7ce503fd | 1486 | /* Find the preceding interval. */ |
9c79dd1b | 1487 | if (! NULL_LEFT_CHILD (i)) /* It's below us. Go down, |
7ce503fd | 1488 | adding ABSORB as we go. */ |
9c79dd1b JA |
1489 | { |
1490 | predecessor = i->left; | |
1491 | while (! NULL_RIGHT_CHILD (predecessor)) | |
1492 | { | |
1493 | predecessor->total_length += absorb; | |
908589fd | 1494 | eassert (TOTAL_LENGTH (predecessor) >= 0); |
9c79dd1b JA |
1495 | predecessor = predecessor->right; |
1496 | } | |
1497 | ||
1498 | predecessor->total_length += absorb; | |
908589fd | 1499 | eassert (TOTAL_LENGTH (predecessor) >= 0); |
9c79dd1b JA |
1500 | delete_interval (i); |
1501 | return predecessor; | |
1502 | } | |
1503 | ||
60cfd278 AS |
1504 | /* Zero out this interval. */ |
1505 | i->total_length -= absorb; | |
908589fd | 1506 | eassert (TOTAL_LENGTH (i) >= 0); |
60cfd278 | 1507 | |
9c79dd1b JA |
1508 | predecessor = i; |
1509 | while (! NULL_PARENT (predecessor)) /* It's above us. Go up, | |
60cfd278 | 1510 | subtracting ABSORB. */ |
9c79dd1b JA |
1511 | { |
1512 | if (AM_RIGHT_CHILD (predecessor)) | |
1513 | { | |
439d5cb4 | 1514 | predecessor = INTERVAL_PARENT (predecessor); |
9c79dd1b JA |
1515 | delete_interval (i); |
1516 | return predecessor; | |
1517 | } | |
1518 | ||
439d5cb4 | 1519 | predecessor = INTERVAL_PARENT (predecessor); |
9c79dd1b | 1520 | predecessor->total_length -= absorb; |
908589fd | 1521 | eassert (TOTAL_LENGTH (predecessor) >= 0); |
9c79dd1b | 1522 | } |
a50699fd | 1523 | |
9c79dd1b | 1524 | /* This must be the leftmost or first interval and cannot |
7ce503fd | 1525 | be merged left. The caller should have known. */ |
1088b922 | 1526 | emacs_abort (); |
9c79dd1b JA |
1527 | } |
1528 | \f | |
44386687 DA |
1529 | /* Create a copy of SOURCE but with the default value of UP. */ |
1530 | ||
1531 | static INTERVAL | |
1532 | reproduce_interval (INTERVAL source) | |
1533 | { | |
1534 | register INTERVAL target = make_interval (); | |
1535 | ||
1536 | target->total_length = source->total_length; | |
1537 | target->position = source->position; | |
1538 | ||
1539 | copy_properties (source, target); | |
1540 | ||
1541 | if (! NULL_LEFT_CHILD (source)) | |
0c94c8d6 | 1542 | set_interval_left (target, reproduce_tree (source->left, target)); |
44386687 | 1543 | if (! NULL_RIGHT_CHILD (source)) |
0c94c8d6 | 1544 | set_interval_right (target, reproduce_tree (source->right, target)); |
44386687 DA |
1545 | |
1546 | return target; | |
1547 | } | |
1548 | ||
1d1d7ba0 JA |
1549 | /* Make an exact copy of interval tree SOURCE which descends from |
1550 | PARENT. This is done by recursing through SOURCE, copying | |
1551 | the current interval and its properties, and then adjusting | |
7ce503fd | 1552 | the pointers of the copy. */ |
1d1d7ba0 | 1553 | |
a50699fd | 1554 | static INTERVAL |
971de7fb | 1555 | reproduce_tree (INTERVAL source, INTERVAL parent) |
a50699fd | 1556 | { |
0c94c8d6 PE |
1557 | INTERVAL target = reproduce_interval (source); |
1558 | set_interval_parent (target, parent); | |
44386687 | 1559 | return target; |
439d5cb4 KR |
1560 | } |
1561 | ||
1562 | static INTERVAL | |
971de7fb | 1563 | reproduce_tree_obj (INTERVAL source, Lisp_Object parent) |
439d5cb4 | 1564 | { |
0c94c8d6 PE |
1565 | INTERVAL target = reproduce_interval (source); |
1566 | set_interval_object (target, parent); | |
44386687 | 1567 | return target; |
a50699fd | 1568 | } |
294efdbe | 1569 | \f |
9c79dd1b | 1570 | /* Insert the intervals of SOURCE into BUFFER at POSITION. |
0b79989f | 1571 | LENGTH is the length of the text in SOURCE. |
a50699fd | 1572 | |
944d4e4b KH |
1573 | The `position' field of the SOURCE intervals is assumed to be |
1574 | consistent with its parent; therefore, SOURCE must be an | |
1575 | interval tree made with copy_interval or must be the whole | |
1576 | tree of a buffer or a string. | |
1577 | ||
2bc7a79b JB |
1578 | This is used in insdel.c when inserting Lisp_Strings into the |
1579 | buffer. The text corresponding to SOURCE is already in the buffer | |
1580 | when this is called. The intervals of new tree are a copy of those | |
1581 | belonging to the string being inserted; intervals are never | |
1582 | shared. | |
a50699fd | 1583 | |
0b79989f RS |
1584 | If the inserted text had no intervals associated, and we don't |
1585 | want to inherit the surrounding text's properties, this function | |
a50699fd | 1586 | simply returns -- offset_intervals should handle placing the |
90ba40fc | 1587 | text in the correct interval, depending on the sticky bits. |
a50699fd JA |
1588 | |
1589 | If the inserted text had properties (intervals), then there are two | |
1590 | cases -- either insertion happened in the middle of some interval, | |
1591 | or between two intervals. | |
1592 | ||
65e8ee52 DA |
1593 | If the text goes into the middle of an interval, then new intervals |
1594 | are created in the middle, and new text has the union of its properties | |
1595 | and those of the text into which it was inserted. | |
a50699fd JA |
1596 | |
1597 | If the text goes between two intervals, then if neither interval | |
90ba40fc JA |
1598 | had its appropriate sticky property set (front_sticky, rear_sticky), |
1599 | the new text has only its properties. If one of the sticky properties | |
a50699fd | 1600 | is set, then the new text "sticks" to that region and its properties |
eb8c3be9 | 1601 | depend on merging as above. If both the preceding and succeeding |
90ba40fc JA |
1602 | intervals to the new text are "sticky", then the new text retains |
1603 | only its properties, as if neither sticky property were set. Perhaps | |
a50699fd | 1604 | we should consider merging all three sets of properties onto the new |
7ce503fd | 1605 | text... */ |
a50699fd JA |
1606 | |
1607 | void | |
d311d28c PE |
1608 | graft_intervals_into_buffer (INTERVAL source, ptrdiff_t position, |
1609 | ptrdiff_t length, struct buffer *buffer, | |
a08d4ba7 | 1610 | bool inherit) |
a50699fd | 1611 | { |
0c94c8d6 PE |
1612 | INTERVAL tree = buffer_intervals (buffer); |
1613 | INTERVAL under, over, this; | |
d311d28c | 1614 | ptrdiff_t over_used; |
a50699fd | 1615 | |
34e096ed RS |
1616 | /* If the new text has no properties, then with inheritance it |
1617 | becomes part of whatever interval it was inserted into. | |
1618 | To prevent inheritance, we must clear out the properties | |
1619 | of the newly inserted text. */ | |
77c7bcb1 | 1620 | if (!source) |
0b79989f RS |
1621 | { |
1622 | Lisp_Object buf; | |
77c7bcb1 | 1623 | if (!inherit && tree && length > 0) |
0b79989f | 1624 | { |
55cfc731 | 1625 | XSETBUFFER (buf, buffer); |
34e096ed RS |
1626 | set_text_properties_1 (make_number (position), |
1627 | make_number (position + length), | |
1687fb14 DA |
1628 | Qnil, buf, |
1629 | find_interval (tree, position)); | |
0b79989f | 1630 | } |
8707c1e5 DA |
1631 | /* Shouldn't be necessary. --Stef */ |
1632 | buffer_balance_intervals (buffer); | |
0b79989f RS |
1633 | return; |
1634 | } | |
a50699fd | 1635 | |
b50a28de SM |
1636 | eassert (length == TOTAL_LENGTH (source)); |
1637 | ||
1638 | if ((BUF_Z (buffer) - BUF_BEG (buffer)) == length) | |
56120d6f | 1639 | { |
8707c1e5 | 1640 | /* The inserted text constitutes the whole buffer, so |
7ce503fd | 1641 | simply copy over the interval structure. */ |
8707c1e5 DA |
1642 | Lisp_Object buf; |
1643 | ||
1644 | XSETBUFFER (buf, buffer); | |
0c94c8d6 PE |
1645 | set_buffer_intervals (buffer, reproduce_tree_obj (source, buf)); |
1646 | buffer_intervals (buffer)->position = BUF_BEG (buffer); | |
1647 | eassert (buffer_intervals (buffer)->up_obj == 1); | |
8707c1e5 DA |
1648 | return; |
1649 | } | |
77c7bcb1 | 1650 | else if (!tree) |
8707c1e5 DA |
1651 | { |
1652 | /* Create an interval tree in which to place a copy | |
7ce503fd | 1653 | of the intervals of the inserted string. */ |
249a6da9 | 1654 | Lisp_Object buf; |
8707c1e5 | 1655 | |
55cfc731 | 1656 | XSETBUFFER (buf, buffer); |
323a7ad4 | 1657 | tree = create_root_interval (buf); |
8707c1e5 | 1658 | } |
cce7fefc DA |
1659 | /* Paranoia -- the text has already been added, so |
1660 | this buffer should be of non-zero length. */ | |
1661 | eassert (TOTAL_LENGTH (tree) > 0); | |
a50699fd JA |
1662 | |
1663 | this = under = find_interval (tree, position); | |
77c7bcb1 | 1664 | eassert (under); |
944d4e4b | 1665 | over = find_interval (source, interval_start_pos (source)); |
a50699fd | 1666 | |
323a7ad4 RS |
1667 | /* Here for insertion in the middle of an interval. |
1668 | Split off an equivalent interval to the right, | |
1669 | then don't bother with it any more. */ | |
a50699fd | 1670 | |
323a7ad4 | 1671 | if (position > under->position) |
a50699fd JA |
1672 | { |
1673 | INTERVAL end_unchanged | |
2bc7a79b | 1674 | = split_interval_left (this, position - under->position); |
a50699fd | 1675 | copy_properties (under, end_unchanged); |
323a7ad4 | 1676 | under->position = position; |
a50699fd | 1677 | } |
323a7ad4 RS |
1678 | else |
1679 | { | |
f56b42ac KH |
1680 | /* This call may have some effect because previous_interval may |
1681 | update `position' fields of intervals. Thus, don't ignore it | |
1682 | for the moment. Someone please tell me the truth (K.Handa). */ | |
dc6c6455 PE |
1683 | INTERVAL prev = previous_interval (under); |
1684 | (void) prev; | |
f56b42ac KH |
1685 | #if 0 |
1686 | /* But, this code surely has no effect. And, anyway, | |
1687 | END_NONSTICKY_P is unreliable now. */ | |
7ce503fd | 1688 | if (prev && !END_NONSTICKY_P (prev)) |
323a7ad4 | 1689 | prev = 0; |
f56b42ac | 1690 | #endif /* 0 */ |
323a7ad4 RS |
1691 | } |
1692 | ||
1693 | /* Insertion is now at beginning of UNDER. */ | |
a50699fd | 1694 | |
323a7ad4 | 1695 | /* The inserted text "sticks" to the interval `under', |
7ce503fd RS |
1696 | which means it gets those properties. |
1697 | The properties of under are the result of | |
8e6208c5 | 1698 | adjust_intervals_for_insertion, so stickiness has |
7ce503fd | 1699 | already been taken care of. */ |
7d0393cf | 1700 | |
6b61353c KH |
1701 | /* OVER is the interval we are copying from next. |
1702 | OVER_USED says how many characters' worth of OVER | |
1703 | have already been copied into target intervals. | |
1704 | UNDER is the next interval in the target. */ | |
1705 | over_used = 0; | |
77c7bcb1 | 1706 | while (over) |
a50699fd | 1707 | { |
6b61353c KH |
1708 | /* If UNDER is longer than OVER, split it. */ |
1709 | if (LENGTH (over) - over_used < LENGTH (under)) | |
7ce503fd | 1710 | { |
6b61353c | 1711 | this = split_interval_left (under, LENGTH (over) - over_used); |
7ce503fd RS |
1712 | copy_properties (under, this); |
1713 | } | |
323a7ad4 RS |
1714 | else |
1715 | this = under; | |
6b61353c KH |
1716 | |
1717 | /* THIS is now the interval to copy or merge into. | |
1718 | OVER covers all of it. */ | |
7ea69158 | 1719 | if (inherit) |
7ce503fd RS |
1720 | merge_properties (over, this); |
1721 | else | |
1722 | copy_properties (over, this); | |
6b61353c KH |
1723 | |
1724 | /* If THIS and OVER end at the same place, | |
1725 | advance OVER to a new source interval. */ | |
1726 | if (LENGTH (this) == LENGTH (over) - over_used) | |
1727 | { | |
1728 | over = next_interval (over); | |
1729 | over_used = 0; | |
1730 | } | |
1731 | else | |
1732 | /* Otherwise just record that more of OVER has been used. */ | |
1733 | over_used += LENGTH (this); | |
1734 | ||
1735 | /* Always advance to a new target interval. */ | |
1736 | under = next_interval (this); | |
a50699fd JA |
1737 | } |
1738 | ||
8707c1e5 | 1739 | buffer_balance_intervals (buffer); |
a50699fd JA |
1740 | } |
1741 | ||
5cae0ec6 RS |
1742 | /* Get the value of property PROP from PLIST, |
1743 | which is the plist of an interval. | |
7d0393cf | 1744 | We check for direct properties, for categories with property PROP, |
06d92327 | 1745 | and for PROP appearing on the default-text-properties list. */ |
5cae0ec6 RS |
1746 | |
1747 | Lisp_Object | |
971de7fb | 1748 | textget (Lisp_Object plist, register Lisp_Object prop) |
323a7ad4 | 1749 | { |
91b53904 | 1750 | return lookup_char_property (plist, prop, 1); |
948fe32d CW |
1751 | } |
1752 | ||
1753 | Lisp_Object | |
a08d4ba7 | 1754 | lookup_char_property (Lisp_Object plist, Lisp_Object prop, bool textprop) |
948fe32d | 1755 | { |
a08d4ba7 | 1756 | Lisp_Object tail, fallback = Qnil; |
323a7ad4 | 1757 | |
91b53904 | 1758 | for (tail = plist; CONSP (tail); tail = Fcdr (XCDR (tail))) |
323a7ad4 RS |
1759 | { |
1760 | register Lisp_Object tem; | |
91b53904 | 1761 | tem = XCAR (tail); |
323a7ad4 | 1762 | if (EQ (prop, tem)) |
91b53904 | 1763 | return Fcar (XCDR (tail)); |
5cae0ec6 | 1764 | if (EQ (tem, Qcategory)) |
5dd6606e | 1765 | { |
91b53904 | 1766 | tem = Fcar (XCDR (tail)); |
5dd6606e RS |
1767 | if (SYMBOLP (tem)) |
1768 | fallback = Fget (tem, prop); | |
1769 | } | |
323a7ad4 | 1770 | } |
5cae0ec6 | 1771 | |
70743ff1 BG |
1772 | if (! NILP (fallback)) |
1773 | return fallback; | |
948fe32d CW |
1774 | /* Check for alternative properties */ |
1775 | tail = Fassq (prop, Vchar_property_alias_alist); | |
931285e2 LT |
1776 | if (! NILP (tail)) |
1777 | { | |
1778 | tail = XCDR (tail); | |
1779 | for (; NILP (fallback) && CONSP (tail); tail = XCDR (tail)) | |
1780 | fallback = Fplist_get (plist, XCAR (tail)); | |
1781 | } | |
1782 | ||
948fe32d CW |
1783 | if (textprop && NILP (fallback) && CONSP (Vdefault_text_properties)) |
1784 | fallback = Fplist_get (Vdefault_text_properties, prop); | |
1785 | return fallback; | |
323a7ad4 | 1786 | } |
7ce503fd | 1787 | |
294efdbe | 1788 | \f |
ef1900f3 RS |
1789 | /* Set point in BUFFER "temporarily" to CHARPOS, which corresponds to |
1790 | byte position BYTEPOS. */ | |
1791 | ||
09db192c | 1792 | void |
6ba7f443 | 1793 | temp_set_point_both (struct buffer *buffer, |
d311d28c | 1794 | ptrdiff_t charpos, ptrdiff_t bytepos) |
ef1900f3 RS |
1795 | { |
1796 | /* In a single-byte buffer, the two positions must be equal. */ | |
cce7fefc DA |
1797 | if (BUF_ZV (buffer) == BUF_ZV_BYTE (buffer)) |
1798 | eassert (charpos == bytepos); | |
ef1900f3 | 1799 | |
cce7fefc DA |
1800 | eassert (charpos <= bytepos); |
1801 | eassert (charpos <= BUF_ZV (buffer) || BUF_BEGV (buffer) <= charpos); | |
ef1900f3 | 1802 | |
cffc6f3b | 1803 | SET_BUF_PT_BOTH (buffer, charpos, bytepos); |
ef1900f3 RS |
1804 | } |
1805 | ||
f8ab8c1f EZ |
1806 | /* Set point "temporarily", without checking any text properties. */ |
1807 | ||
09db192c | 1808 | void |
d311d28c | 1809 | temp_set_point (struct buffer *buffer, ptrdiff_t charpos) |
f8ab8c1f EZ |
1810 | { |
1811 | temp_set_point_both (buffer, charpos, | |
1812 | buf_charpos_to_bytepos (buffer, charpos)); | |
1813 | } | |
1814 | ||
7d0393cf | 1815 | /* Set point in BUFFER to CHARPOS. If the target position is |
f65013b0 | 1816 | before an intangible character, move to an ok place. */ |
a50699fd JA |
1817 | |
1818 | void | |
d311d28c | 1819 | set_point (ptrdiff_t charpos) |
ef1900f3 | 1820 | { |
6ba7f443 | 1821 | set_point_both (charpos, buf_charpos_to_bytepos (current_buffer, charpos)); |
ef1900f3 RS |
1822 | } |
1823 | ||
f0dcf801 MB |
1824 | /* If there's an invisible character at position POS + TEST_OFFS in the |
1825 | current buffer, and the invisible property has a `stickiness' such that | |
1826 | inserting a character at position POS would inherit the property it, | |
a08d4ba7 PE |
1827 | return POS + ADJ, otherwise return POS. If TEST_INTANG, intangibility |
1828 | is required as well as invisibility. | |
f0dcf801 MB |
1829 | |
1830 | TEST_OFFS should be either 0 or -1, and ADJ should be either 1 or -1. | |
1831 | ||
1832 | Note that `stickiness' is determined by overlay marker insertion types, | |
7d0393cf | 1833 | if the invisible property comes from an overlay. */ |
f0dcf801 | 1834 | |
d311d28c PE |
1835 | static ptrdiff_t |
1836 | adjust_for_invis_intang (ptrdiff_t pos, ptrdiff_t test_offs, ptrdiff_t adj, | |
a08d4ba7 | 1837 | bool test_intang) |
f0dcf801 MB |
1838 | { |
1839 | Lisp_Object invis_propval, invis_overlay; | |
1840 | Lisp_Object test_pos; | |
1841 | ||
1842 | if ((adj < 0 && pos + adj < BEGV) || (adj > 0 && pos + adj > ZV)) | |
1843 | /* POS + ADJ would be beyond the buffer bounds, so do no adjustment. */ | |
1844 | return pos; | |
1845 | ||
1846 | test_pos = make_number (pos + test_offs); | |
1847 | ||
1848 | invis_propval | |
1849 | = get_char_property_and_overlay (test_pos, Qinvisible, Qnil, | |
1850 | &invis_overlay); | |
1851 | ||
1852 | if ((!test_intang | |
1853 | || ! NILP (Fget_char_property (test_pos, Qintangible, Qnil))) | |
1854 | && TEXT_PROP_MEANS_INVISIBLE (invis_propval) | |
1855 | /* This next test is true if the invisible property has a stickiness | |
1856 | such that an insertion at POS would inherit it. */ | |
1857 | && (NILP (invis_overlay) | |
1858 | /* Invisible property is from a text-property. */ | |
4867a283 | 1859 | ? (text_property_stickiness (Qinvisible, make_number (pos), Qnil) |
f0dcf801 MB |
1860 | == (test_offs == 0 ? 1 : -1)) |
1861 | /* Invisible property is from an overlay. */ | |
1862 | : (test_offs == 0 | |
1863 | ? XMARKER (OVERLAY_START (invis_overlay))->insertion_type == 0 | |
1864 | : XMARKER (OVERLAY_END (invis_overlay))->insertion_type == 1))) | |
1865 | pos += adj; | |
1866 | ||
1867 | return pos; | |
1868 | } | |
1869 | ||
ef1900f3 | 1870 | /* Set point in BUFFER to CHARPOS, which corresponds to byte |
7d0393cf | 1871 | position BYTEPOS. If the target position is |
ef1900f3 RS |
1872 | before an intangible character, move to an ok place. */ |
1873 | ||
1874 | void | |
d311d28c | 1875 | set_point_both (ptrdiff_t charpos, ptrdiff_t bytepos) |
a50699fd | 1876 | { |
e39adcda | 1877 | register INTERVAL to, from, toprev, fromprev; |
d311d28c PE |
1878 | ptrdiff_t buffer_point; |
1879 | ptrdiff_t old_position = PT; | |
594a1605 CY |
1880 | /* This ensures that we move forward past intangible text when the |
1881 | initial position is the same as the destination, in the rare | |
1882 | instances where this is important, e.g. in line-move-finish | |
1883 | (simple.el). */ | |
a08d4ba7 PE |
1884 | bool backwards = charpos < old_position; |
1885 | bool have_overlays; | |
d311d28c | 1886 | ptrdiff_t original_position; |
a50699fd | 1887 | |
39eb03f1 | 1888 | bset_point_before_scroll (current_buffer, Qnil); |
b6a0ebc3 | 1889 | |
6ba7f443 | 1890 | if (charpos == PT) |
a50699fd JA |
1891 | return; |
1892 | ||
ef1900f3 | 1893 | /* In a single-byte buffer, the two positions must be equal. */ |
6ba7f443 | 1894 | eassert (ZV != ZV_BYTE || charpos == bytepos); |
ef1900f3 | 1895 | |
62056764 JB |
1896 | /* Check this now, before checking if the buffer has any intervals. |
1897 | That way, we can catch conditions which break this sanity check | |
1898 | whether or not there are intervals in the buffer. */ | |
6ba7f443 | 1899 | eassert (charpos <= ZV && charpos >= BEGV); |
62056764 | 1900 | |
4cb3e6b3 | 1901 | have_overlays = buffer_has_overlays (); |
580fae94 RS |
1902 | |
1903 | /* If we have no text properties and overlays, | |
1904 | then we can do it quickly. */ | |
0c94c8d6 | 1905 | if (!buffer_intervals (current_buffer) && ! have_overlays) |
a50699fd | 1906 | { |
6ba7f443 | 1907 | temp_set_point_both (current_buffer, charpos, bytepos); |
a50699fd JA |
1908 | return; |
1909 | } | |
1910 | ||
ef1900f3 RS |
1911 | /* Set TO to the interval containing the char after CHARPOS, |
1912 | and TOPREV to the interval containing the char before CHARPOS. | |
323a7ad4 | 1913 | Either one may be null. They may be equal. */ |
0c94c8d6 | 1914 | to = find_interval (buffer_intervals (current_buffer), charpos); |
6ba7f443 | 1915 | if (charpos == BEGV) |
294efdbe | 1916 | toprev = 0; |
ef1900f3 | 1917 | else if (to && to->position == charpos) |
323a7ad4 | 1918 | toprev = previous_interval (to); |
323a7ad4 RS |
1919 | else |
1920 | toprev = to; | |
1921 | ||
6ba7f443 | 1922 | buffer_point = (PT == ZV ? ZV - 1 : PT); |
9c79dd1b | 1923 | |
323a7ad4 RS |
1924 | /* Set FROM to the interval containing the char after PT, |
1925 | and FROMPREV to the interval containing the char before PT. | |
1926 | Either one may be null. They may be equal. */ | |
7ce503fd | 1927 | /* We could cache this and save time. */ |
0c94c8d6 | 1928 | from = find_interval (buffer_intervals (current_buffer), buffer_point); |
6ba7f443 | 1929 | if (buffer_point == BEGV) |
294efdbe | 1930 | fromprev = 0; |
6ba7f443 | 1931 | else if (from && from->position == PT) |
323a7ad4 | 1932 | fromprev = previous_interval (from); |
6ba7f443 | 1933 | else if (buffer_point != PT) |
323a7ad4 RS |
1934 | fromprev = from, from = 0; |
1935 | else | |
1936 | fromprev = from; | |
a50699fd | 1937 | |
7ce503fd | 1938 | /* Moving within an interval. */ |
580fae94 RS |
1939 | if (to == from && toprev == fromprev && INTERVAL_VISIBLE_P (to) |
1940 | && ! have_overlays) | |
a50699fd | 1941 | { |
6ba7f443 | 1942 | temp_set_point_both (current_buffer, charpos, bytepos); |
a50699fd JA |
1943 | return; |
1944 | } | |
1945 | ||
ef1900f3 | 1946 | original_position = charpos; |
580fae94 | 1947 | |
5eabb4e7 RS |
1948 | /* If the new position is between two intangible characters |
1949 | with the same intangible property value, | |
1950 | move forward or backward until a change in that property. */ | |
580fae94 | 1951 | if (NILP (Vinhibit_point_motion_hooks) |
77c7bcb1 | 1952 | && ((to && toprev) |
b827a9e3 RS |
1953 | || have_overlays) |
1954 | /* Intangibility never stops us from positioning at the beginning | |
1955 | or end of the buffer, so don't bother checking in that case. */ | |
ef1900f3 | 1956 | && charpos != BEGV && charpos != ZV) |
a50699fd | 1957 | { |
580fae94 | 1958 | Lisp_Object pos; |
f0dcf801 | 1959 | Lisp_Object intangible_propval; |
580fae94 | 1960 | |
d5219de5 RS |
1961 | if (backwards) |
1962 | { | |
0270b877 | 1963 | /* If the preceding character is both intangible and invisible, |
f0dcf801 MB |
1964 | and the invisible property is `rear-sticky', perturb it so |
1965 | that the search starts one character earlier -- this ensures | |
1966 | that point can never move to the end of an invisible/ | |
1967 | intangible/rear-sticky region. */ | |
1968 | charpos = adjust_for_invis_intang (charpos, -1, -1, 1); | |
1d14d232 | 1969 | |
f0dcf801 | 1970 | XSETINT (pos, charpos); |
5eabb4e7 RS |
1971 | |
1972 | /* If following char is intangible, | |
1973 | skip back over all chars with matching intangible property. */ | |
1d14d232 RS |
1974 | |
1975 | intangible_propval = Fget_char_property (pos, Qintangible, Qnil); | |
1976 | ||
5eabb4e7 | 1977 | if (! NILP (intangible_propval)) |
1d14d232 | 1978 | { |
6ba7f443 | 1979 | while (XINT (pos) > BEGV |
1d14d232 RS |
1980 | && EQ (Fget_char_property (make_number (XINT (pos) - 1), |
1981 | Qintangible, Qnil), | |
1982 | intangible_propval)) | |
1983 | pos = Fprevious_char_property_change (pos, Qnil); | |
f0dcf801 MB |
1984 | |
1985 | /* Set CHARPOS from POS, and if the final intangible character | |
1986 | that we skipped over is also invisible, and the invisible | |
1987 | property is `front-sticky', perturb it to be one character | |
1988 | earlier -- this ensures that point can never move to the | |
1989 | beginning of an invisible/intangible/front-sticky region. */ | |
1990 | charpos = adjust_for_invis_intang (XINT (pos), 0, -1, 0); | |
1d14d232 | 1991 | } |
d5219de5 | 1992 | } |
0df8950e | 1993 | else |
d5219de5 | 1994 | { |
f0dcf801 MB |
1995 | /* If the following character is both intangible and invisible, |
1996 | and the invisible property is `front-sticky', perturb it so | |
1997 | that the search starts one character later -- this ensures | |
1998 | that point can never move to the beginning of an | |
1999 | invisible/intangible/front-sticky region. */ | |
2000 | charpos = adjust_for_invis_intang (charpos, 0, 1, 1); | |
2001 | ||
2002 | XSETINT (pos, charpos); | |
2003 | ||
1d14d232 RS |
2004 | /* If preceding char is intangible, |
2005 | skip forward over all chars with matching intangible property. */ | |
2006 | ||
ef1900f3 | 2007 | intangible_propval = Fget_char_property (make_number (charpos - 1), |
580fae94 | 2008 | Qintangible, Qnil); |
5eabb4e7 | 2009 | |
5eabb4e7 | 2010 | if (! NILP (intangible_propval)) |
1d14d232 | 2011 | { |
6ba7f443 | 2012 | while (XINT (pos) < ZV |
1d14d232 RS |
2013 | && EQ (Fget_char_property (pos, Qintangible, Qnil), |
2014 | intangible_propval)) | |
2015 | pos = Fnext_char_property_change (pos, Qnil); | |
580fae94 | 2016 | |
f0dcf801 MB |
2017 | /* Set CHARPOS from POS, and if the final intangible character |
2018 | that we skipped over is also invisible, and the invisible | |
2019 | property is `rear-sticky', perturb it to be one character | |
2020 | later -- this ensures that point can never move to the | |
2021 | end of an invisible/intangible/rear-sticky region. */ | |
2022 | charpos = adjust_for_invis_intang (XINT (pos), -1, 1, 0); | |
1d14d232 | 2023 | } |
d5219de5 | 2024 | } |
580fae94 | 2025 | |
6ba7f443 | 2026 | bytepos = buf_charpos_to_bytepos (current_buffer, charpos); |
580fae94 RS |
2027 | } |
2028 | ||
ef1900f3 | 2029 | if (charpos != original_position) |
580fae94 | 2030 | { |
ef1900f3 RS |
2031 | /* Set TO to the interval containing the char after CHARPOS, |
2032 | and TOPREV to the interval containing the char before CHARPOS. | |
580fae94 | 2033 | Either one may be null. They may be equal. */ |
0c94c8d6 | 2034 | to = find_interval (buffer_intervals (current_buffer), charpos); |
6ba7f443 | 2035 | if (charpos == BEGV) |
580fae94 | 2036 | toprev = 0; |
ef1900f3 | 2037 | else if (to && to->position == charpos) |
580fae94 RS |
2038 | toprev = previous_interval (to); |
2039 | else | |
2040 | toprev = to; | |
a50699fd | 2041 | } |
323a7ad4 | 2042 | |
5eabb4e7 RS |
2043 | /* Here TO is the interval after the stopping point |
2044 | and TOPREV is the interval before the stopping point. | |
2045 | One or the other may be null. */ | |
2046 | ||
6ba7f443 | 2047 | temp_set_point_both (current_buffer, charpos, bytepos); |
a50699fd | 2048 | |
e0f24100 | 2049 | /* We run point-left and point-entered hooks here, if the |
d7e3e52b | 2050 | two intervals are not equivalent. These hooks take |
323a7ad4 | 2051 | (old_point, new_point) as arguments. */ |
ddd931ff RS |
2052 | if (NILP (Vinhibit_point_motion_hooks) |
2053 | && (! intervals_equal (from, to) | |
2054 | || ! intervals_equal (fromprev, toprev))) | |
9c79dd1b | 2055 | { |
323a7ad4 RS |
2056 | Lisp_Object leave_after, leave_before, enter_after, enter_before; |
2057 | ||
2058 | if (fromprev) | |
4e8f005c | 2059 | leave_before = textget (fromprev->plist, Qpoint_left); |
323a7ad4 | 2060 | else |
4e8f005c CY |
2061 | leave_before = Qnil; |
2062 | ||
323a7ad4 | 2063 | if (from) |
4e8f005c | 2064 | leave_after = textget (from->plist, Qpoint_left); |
323a7ad4 | 2065 | else |
4e8f005c | 2066 | leave_after = Qnil; |
323a7ad4 RS |
2067 | |
2068 | if (toprev) | |
4e8f005c | 2069 | enter_before = textget (toprev->plist, Qpoint_entered); |
323a7ad4 | 2070 | else |
4e8f005c CY |
2071 | enter_before = Qnil; |
2072 | ||
323a7ad4 | 2073 | if (to) |
4e8f005c | 2074 | enter_after = textget (to->plist, Qpoint_entered); |
323a7ad4 | 2075 | else |
4e8f005c | 2076 | enter_after = Qnil; |
9c79dd1b | 2077 | |
323a7ad4 | 2078 | if (! EQ (leave_before, enter_before) && !NILP (leave_before)) |
4e8f005c CY |
2079 | call2 (leave_before, make_number (old_position), |
2080 | make_number (charpos)); | |
323a7ad4 | 2081 | if (! EQ (leave_after, enter_after) && !NILP (leave_after)) |
4e8f005c CY |
2082 | call2 (leave_after, make_number (old_position), |
2083 | make_number (charpos)); | |
9c79dd1b | 2084 | |
323a7ad4 | 2085 | if (! EQ (enter_before, leave_before) && !NILP (enter_before)) |
4e8f005c CY |
2086 | call2 (enter_before, make_number (old_position), |
2087 | make_number (charpos)); | |
323a7ad4 | 2088 | if (! EQ (enter_after, leave_after) && !NILP (enter_after)) |
4e8f005c CY |
2089 | call2 (enter_after, make_number (old_position), |
2090 | make_number (charpos)); | |
9c79dd1b | 2091 | } |
a50699fd | 2092 | } |
294efdbe | 2093 | \f |
a7fa233f RS |
2094 | /* Move point to POSITION, unless POSITION is inside an intangible |
2095 | segment that reaches all the way to point. */ | |
2096 | ||
2097 | void | |
d311d28c | 2098 | move_if_not_intangible (ptrdiff_t position) |
a7fa233f RS |
2099 | { |
2100 | Lisp_Object pos; | |
2101 | Lisp_Object intangible_propval; | |
2102 | ||
2103 | XSETINT (pos, position); | |
2104 | ||
2105 | if (! NILP (Vinhibit_point_motion_hooks)) | |
2106 | /* If intangible is inhibited, always move point to POSITION. */ | |
2107 | ; | |
2e34157c | 2108 | else if (PT < position && XINT (pos) < ZV) |
a7fa233f RS |
2109 | { |
2110 | /* We want to move forward, so check the text before POSITION. */ | |
2111 | ||
2112 | intangible_propval = Fget_char_property (pos, | |
2113 | Qintangible, Qnil); | |
2114 | ||
2115 | /* If following char is intangible, | |
2116 | skip back over all chars with matching intangible property. */ | |
2117 | if (! NILP (intangible_propval)) | |
2118 | while (XINT (pos) > BEGV | |
2119 | && EQ (Fget_char_property (make_number (XINT (pos) - 1), | |
2120 | Qintangible, Qnil), | |
2121 | intangible_propval)) | |
2122 | pos = Fprevious_char_property_change (pos, Qnil); | |
2123 | } | |
2e34157c | 2124 | else if (XINT (pos) > BEGV) |
a7fa233f RS |
2125 | { |
2126 | /* We want to move backward, so check the text after POSITION. */ | |
2127 | ||
2128 | intangible_propval = Fget_char_property (make_number (XINT (pos) - 1), | |
2129 | Qintangible, Qnil); | |
2130 | ||
2131 | /* If following char is intangible, | |
887f2a2d | 2132 | skip forward over all chars with matching intangible property. */ |
a7fa233f RS |
2133 | if (! NILP (intangible_propval)) |
2134 | while (XINT (pos) < ZV | |
2135 | && EQ (Fget_char_property (pos, Qintangible, Qnil), | |
2136 | intangible_propval)) | |
2137 | pos = Fnext_char_property_change (pos, Qnil); | |
2138 | ||
2139 | } | |
97d8f112 RS |
2140 | else if (position < BEGV) |
2141 | position = BEGV; | |
2142 | else if (position > ZV) | |
2143 | position = ZV; | |
a7fa233f | 2144 | |
7d0393cf | 2145 | /* If the whole stretch between PT and POSITION isn't intangible, |
a7fa233f RS |
2146 | try moving to POSITION (which means we actually move farther |
2147 | if POSITION is inside of intangible text). */ | |
2148 | ||
2149 | if (XINT (pos) != PT) | |
2150 | SET_PT (position); | |
2151 | } | |
2152 | \f | |
f56b42ac KH |
2153 | /* If text at position POS has property PROP, set *VAL to the property |
2154 | value, *START and *END to the beginning and end of a region that | |
a08d4ba7 | 2155 | has the same property, and return true. Otherwise return false. |
f56b42ac KH |
2156 | |
2157 | OBJECT is the string or buffer to look for the property in; | |
2158 | nil means the current buffer. */ | |
2159 | ||
a08d4ba7 | 2160 | bool |
d311d28c PE |
2161 | get_property_and_range (ptrdiff_t pos, Lisp_Object prop, Lisp_Object *val, |
2162 | ptrdiff_t *start, ptrdiff_t *end, Lisp_Object object) | |
f56b42ac KH |
2163 | { |
2164 | INTERVAL i, prev, next; | |
2165 | ||
2166 | if (NILP (object)) | |
0c94c8d6 | 2167 | i = find_interval (buffer_intervals (current_buffer), pos); |
f56b42ac | 2168 | else if (BUFFERP (object)) |
0c94c8d6 | 2169 | i = find_interval (buffer_intervals (XBUFFER (object)), pos); |
f56b42ac | 2170 | else if (STRINGP (object)) |
0c94c8d6 | 2171 | i = find_interval (string_intervals (object), pos); |
f56b42ac | 2172 | else |
1088b922 | 2173 | emacs_abort (); |
f56b42ac | 2174 | |
77c7bcb1 | 2175 | if (!i || (i->position + LENGTH (i) <= pos)) |
f56b42ac KH |
2176 | return 0; |
2177 | *val = textget (i->plist, prop); | |
2178 | if (NILP (*val)) | |
2179 | return 0; | |
2180 | ||
2181 | next = i; /* remember it in advance */ | |
2182 | prev = previous_interval (i); | |
77c7bcb1 | 2183 | while (prev |
f56b42ac KH |
2184 | && EQ (*val, textget (prev->plist, prop))) |
2185 | i = prev, prev = previous_interval (prev); | |
2186 | *start = i->position; | |
2187 | ||
2188 | next = next_interval (i); | |
77c7bcb1 | 2189 | while (next && EQ (*val, textget (next->plist, prop))) |
f56b42ac KH |
2190 | i = next, next = next_interval (next); |
2191 | *end = i->position + LENGTH (i); | |
2192 | ||
2193 | return 1; | |
2194 | } | |
2195 | \f | |
2b4b027f GM |
2196 | /* Return the proper local keymap TYPE for position POSITION in |
2197 | BUFFER; TYPE should be one of `keymap' or `local-map'. Use the map | |
2198 | specified by the PROP property, if any. Otherwise, if TYPE is | |
5d1c3286 | 2199 | `local-map' use BUFFER's local map. */ |
5cae0ec6 RS |
2200 | |
2201 | Lisp_Object | |
d311d28c | 2202 | get_local_map (register ptrdiff_t position, register struct buffer *buffer, |
e79123aa | 2203 | Lisp_Object type) |
5cae0ec6 | 2204 | { |
f94ecad1 | 2205 | Lisp_Object prop, lispy_position, lispy_buffer; |
d311d28c | 2206 | ptrdiff_t old_begv, old_zv, old_begv_byte, old_zv_byte; |
5cae0ec6 | 2207 | |
5d1c3286 | 2208 | clip_to_bounds (BUF_BEGV (buffer), position, BUF_ZV (buffer)); |
5cae0ec6 | 2209 | |
0f7a5fda KH |
2210 | /* Ignore narrowing, so that a local map continues to be valid even if |
2211 | the visible region contains no characters and hence no properties. */ | |
2212 | old_begv = BUF_BEGV (buffer); | |
2213 | old_zv = BUF_ZV (buffer); | |
ef1900f3 RS |
2214 | old_begv_byte = BUF_BEGV_BYTE (buffer); |
2215 | old_zv_byte = BUF_ZV_BYTE (buffer); | |
cffc6f3b CY |
2216 | |
2217 | SET_BUF_BEGV_BOTH (buffer, BUF_BEG (buffer), BUF_BEG_BYTE (buffer)); | |
2218 | SET_BUF_ZV_BOTH (buffer, BUF_Z (buffer), BUF_Z_BYTE (buffer)); | |
0f7a5fda | 2219 | |
0f7a5fda KH |
2220 | XSETFASTINT (lispy_position, position); |
2221 | XSETBUFFER (lispy_buffer, buffer); | |
4867a283 SM |
2222 | /* First check if the CHAR has any property. This is because when |
2223 | we click with the mouse, the mouse pointer is really pointing | |
2224 | to the CHAR after POS. */ | |
2b4b027f | 2225 | prop = Fget_char_property (lispy_position, type, lispy_buffer); |
4867a283 SM |
2226 | /* If not, look at the POS's properties. This is necessary because when |
2227 | editing a field with a `local-map' property, we want insertion at the end | |
2228 | to obey the `local-map' property. */ | |
2229 | if (NILP (prop)) | |
2230 | prop = get_pos_property (lispy_position, type, lispy_buffer); | |
0f7a5fda | 2231 | |
cffc6f3b CY |
2232 | SET_BUF_BEGV_BOTH (buffer, old_begv, old_begv_byte); |
2233 | SET_BUF_ZV_BOTH (buffer, old_zv, old_zv_byte); | |
5cae0ec6 RS |
2234 | |
2235 | /* Use the local map only if it is valid. */ | |
02067692 SM |
2236 | prop = get_keymap (prop, 0, 0); |
2237 | if (CONSP (prop)) | |
5cae0ec6 RS |
2238 | return prop; |
2239 | ||
2b4b027f | 2240 | if (EQ (type, Qkeymap)) |
6a7dccef DL |
2241 | return Qnil; |
2242 | else | |
4b4deea2 | 2243 | return BVAR (buffer, keymap); |
5cae0ec6 RS |
2244 | } |
2245 | \f | |
9c79dd1b | 2246 | /* Produce an interval tree reflecting the intervals in |
944d4e4b KH |
2247 | TREE from START to START + LENGTH. |
2248 | The new interval tree has no parent and has a starting-position of 0. */ | |
a50699fd | 2249 | |
7b1d5b85 | 2250 | INTERVAL |
d311d28c | 2251 | copy_intervals (INTERVAL tree, ptrdiff_t start, ptrdiff_t length) |
a50699fd JA |
2252 | { |
2253 | register INTERVAL i, new, t; | |
d311d28c | 2254 | register ptrdiff_t got, prevlen; |
a50699fd | 2255 | |
77c7bcb1 DA |
2256 | if (!tree || length <= 0) |
2257 | return NULL; | |
a50699fd JA |
2258 | |
2259 | i = find_interval (tree, start); | |
77c7bcb1 | 2260 | eassert (i && LENGTH (i) > 0); |
a50699fd | 2261 | |
7ce503fd | 2262 | /* If there is only one interval and it's the default, return nil. */ |
a50699fd JA |
2263 | if ((start - i->position + 1 + length) < LENGTH (i) |
2264 | && DEFAULT_INTERVAL_P (i)) | |
77c7bcb1 | 2265 | return NULL; |
a50699fd JA |
2266 | |
2267 | new = make_interval (); | |
944d4e4b | 2268 | new->position = 0; |
a50699fd | 2269 | got = (LENGTH (i) - (start - i->position)); |
9c79dd1b | 2270 | new->total_length = length; |
908589fd | 2271 | eassert (TOTAL_LENGTH (new) >= 0); |
a50699fd JA |
2272 | copy_properties (i, new); |
2273 | ||
2274 | t = new; | |
95e3e1ef | 2275 | prevlen = got; |
a50699fd JA |
2276 | while (got < length) |
2277 | { | |
2278 | i = next_interval (i); | |
2bc7a79b | 2279 | t = split_interval_right (t, prevlen); |
a50699fd | 2280 | copy_properties (i, t); |
95e3e1ef RS |
2281 | prevlen = LENGTH (i); |
2282 | got += prevlen; | |
a50699fd JA |
2283 | } |
2284 | ||
4314dea4 | 2285 | return balance_an_interval (new); |
a50699fd JA |
2286 | } |
2287 | ||
7ce503fd | 2288 | /* Give STRING the properties of BUFFER from POSITION to LENGTH. */ |
a50699fd | 2289 | |
09db192c | 2290 | void |
e79123aa | 2291 | copy_intervals_to_string (Lisp_Object string, struct buffer *buffer, |
d311d28c | 2292 | ptrdiff_t position, ptrdiff_t length) |
a50699fd | 2293 | { |
0c94c8d6 | 2294 | INTERVAL interval_copy = copy_intervals (buffer_intervals (buffer), |
a50699fd | 2295 | position, length); |
77c7bcb1 | 2296 | if (!interval_copy) |
a50699fd JA |
2297 | return; |
2298 | ||
0c94c8d6 PE |
2299 | set_interval_object (interval_copy, string); |
2300 | set_string_intervals (string, interval_copy); | |
a50699fd | 2301 | } |
d8638d30 | 2302 | \f |
a08d4ba7 | 2303 | /* Return true if strings S1 and S2 have identical properties. |
d8638d30 RS |
2304 | Assume they have identical characters. */ |
2305 | ||
a08d4ba7 | 2306 | bool |
971de7fb | 2307 | compare_string_intervals (Lisp_Object s1, Lisp_Object s2) |
d8638d30 RS |
2308 | { |
2309 | INTERVAL i1, i2; | |
d311d28c PE |
2310 | ptrdiff_t pos = 0; |
2311 | ptrdiff_t end = SCHARS (s1); | |
d8638d30 | 2312 | |
0c94c8d6 PE |
2313 | i1 = find_interval (string_intervals (s1), 0); |
2314 | i2 = find_interval (string_intervals (s2), 0); | |
d8638d30 RS |
2315 | |
2316 | while (pos < end) | |
2317 | { | |
2318 | /* Determine how far we can go before we reach the end of I1 or I2. */ | |
d311d28c PE |
2319 | ptrdiff_t len1 = (i1 != 0 ? INTERVAL_LAST_POS (i1) : end) - pos; |
2320 | ptrdiff_t len2 = (i2 != 0 ? INTERVAL_LAST_POS (i2) : end) - pos; | |
2321 | ptrdiff_t distance = min (len1, len2); | |
d8638d30 RS |
2322 | |
2323 | /* If we ever find a mismatch between the strings, | |
2324 | they differ. */ | |
2325 | if (! intervals_equal (i1, i2)) | |
2326 | return 0; | |
2327 | ||
2328 | /* Advance POS till the end of the shorter interval, | |
2329 | and advance one or both interval pointers for the new position. */ | |
2330 | pos += distance; | |
2331 | if (len1 == distance) | |
2332 | i1 = next_interval (i1); | |
2333 | if (len2 == distance) | |
2334 | i2 = next_interval (i2); | |
2335 | } | |
2336 | return 1; | |
2337 | } | |
37f26f3c | 2338 | \f |
37f26f3c RS |
2339 | /* Recursively adjust interval I in the current buffer |
2340 | for setting enable_multibyte_characters to MULTI_FLAG. | |
2341 | The range of interval I is START ... END in characters, | |
2342 | START_BYTE ... END_BYTE in bytes. */ | |
2343 | ||
2344 | static void | |
a08d4ba7 | 2345 | set_intervals_multibyte_1 (INTERVAL i, bool multi_flag, |
d311d28c PE |
2346 | ptrdiff_t start, ptrdiff_t start_byte, |
2347 | ptrdiff_t end, ptrdiff_t end_byte) | |
37f26f3c | 2348 | { |
37f26f3c RS |
2349 | /* Fix the length of this interval. */ |
2350 | if (multi_flag) | |
2351 | i->total_length = end - start; | |
2352 | else | |
2353 | i->total_length = end_byte - start_byte; | |
908589fd | 2354 | eassert (TOTAL_LENGTH (i) >= 0); |
727fec2d RS |
2355 | |
2356 | if (TOTAL_LENGTH (i) == 0) | |
2357 | { | |
2358 | delete_interval (i); | |
2359 | return; | |
2360 | } | |
37f26f3c RS |
2361 | |
2362 | /* Recursively fix the length of the subintervals. */ | |
2363 | if (i->left) | |
2364 | { | |
d311d28c | 2365 | ptrdiff_t left_end, left_end_byte; |
37f26f3c RS |
2366 | |
2367 | if (multi_flag) | |
2368 | { | |
d311d28c | 2369 | ptrdiff_t temp; |
37f26f3c RS |
2370 | left_end_byte = start_byte + LEFT_TOTAL_LENGTH (i); |
2371 | left_end = BYTE_TO_CHAR (left_end_byte); | |
727fec2d RS |
2372 | |
2373 | temp = CHAR_TO_BYTE (left_end); | |
2374 | ||
2375 | /* If LEFT_END_BYTE is in the middle of a character, | |
f813361d | 2376 | adjust it and LEFT_END to a char boundary. */ |
727fec2d RS |
2377 | if (left_end_byte > temp) |
2378 | { | |
2379 | left_end_byte = temp; | |
2380 | } | |
2381 | if (left_end_byte < temp) | |
2382 | { | |
2383 | left_end--; | |
2384 | left_end_byte = CHAR_TO_BYTE (left_end); | |
2385 | } | |
37f26f3c RS |
2386 | } |
2387 | else | |
2388 | { | |
2389 | left_end = start + LEFT_TOTAL_LENGTH (i); | |
2390 | left_end_byte = CHAR_TO_BYTE (left_end); | |
2391 | } | |
2392 | ||
2393 | set_intervals_multibyte_1 (i->left, multi_flag, start, start_byte, | |
2394 | left_end, left_end_byte); | |
2395 | } | |
2396 | if (i->right) | |
2397 | { | |
d311d28c | 2398 | ptrdiff_t right_start_byte, right_start; |
37f26f3c RS |
2399 | |
2400 | if (multi_flag) | |
2401 | { | |
d311d28c | 2402 | ptrdiff_t temp; |
727fec2d | 2403 | |
37f26f3c RS |
2404 | right_start_byte = end_byte - RIGHT_TOTAL_LENGTH (i); |
2405 | right_start = BYTE_TO_CHAR (right_start_byte); | |
727fec2d RS |
2406 | |
2407 | /* If RIGHT_START_BYTE is in the middle of a character, | |
f813361d | 2408 | adjust it and RIGHT_START to a char boundary. */ |
727fec2d RS |
2409 | temp = CHAR_TO_BYTE (right_start); |
2410 | ||
2411 | if (right_start_byte < temp) | |
2412 | { | |
2413 | right_start_byte = temp; | |
2414 | } | |
2415 | if (right_start_byte > temp) | |
2416 | { | |
2417 | right_start++; | |
2418 | right_start_byte = CHAR_TO_BYTE (right_start); | |
2419 | } | |
37f26f3c RS |
2420 | } |
2421 | else | |
2422 | { | |
2423 | right_start = end - RIGHT_TOTAL_LENGTH (i); | |
2424 | right_start_byte = CHAR_TO_BYTE (right_start); | |
2425 | } | |
2426 | ||
2427 | set_intervals_multibyte_1 (i->right, multi_flag, | |
2428 | right_start, right_start_byte, | |
2429 | end, end_byte); | |
2430 | } | |
727fec2d RS |
2431 | |
2432 | /* Rounding to char boundaries can theoretically ake this interval | |
2433 | spurious. If so, delete one child, and copy its property list | |
2434 | to this interval. */ | |
2435 | if (LEFT_TOTAL_LENGTH (i) + RIGHT_TOTAL_LENGTH (i) >= TOTAL_LENGTH (i)) | |
2436 | { | |
2437 | if ((i)->left) | |
2438 | { | |
0c94c8d6 | 2439 | set_interval_plist (i, i->left->plist); |
727fec2d RS |
2440 | (i)->left->total_length = 0; |
2441 | delete_interval ((i)->left); | |
2442 | } | |
2443 | else | |
2444 | { | |
0c94c8d6 | 2445 | set_interval_plist (i, i->right->plist); |
727fec2d RS |
2446 | (i)->right->total_length = 0; |
2447 | delete_interval ((i)->right); | |
2448 | } | |
2449 | } | |
37f26f3c | 2450 | } |
d2f7a802 | 2451 | |
24cef261 | 2452 | /* Update the intervals of the current buffer |
a08d4ba7 PE |
2453 | to fit the contents as multibyte (if MULTI_FLAG) |
2454 | or to fit them as non-multibyte (if not MULTI_FLAG). */ | |
24cef261 RS |
2455 | |
2456 | void | |
a08d4ba7 | 2457 | set_intervals_multibyte (bool multi_flag) |
24cef261 | 2458 | { |
0c94c8d6 | 2459 | INTERVAL i = buffer_intervals (current_buffer); |
8707c1e5 DA |
2460 | |
2461 | if (i) | |
2462 | set_intervals_multibyte_1 (i, multi_flag, BEG, BEG_BYTE, Z, Z_BYTE); | |
24cef261 | 2463 | } |