Minor improvement in section "Pages" of the usere manual
[emacs.git] / src / intervals.c
blob4c624ea79c189435213d02bd1f8a7505ee416cdf
1 /* Code for doing intervals.
2 Copyright (C) 1993-1995, 1997-1998, 2001-2018 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
21 /* NOTES:
23 Have to ensure that we can't put symbol nil on a plist, or some
24 functions may work incorrectly.
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.
29 Need to call *_left_hook when buffer is killed.
31 Scan for zero-length, or 0-length to see notes about handling
32 zero length interval-markers.
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.
41 #include <config.h>
43 #include <intprops.h>
44 #include "lisp.h"
45 #include "intervals.h"
46 #include "buffer.h"
47 #include "puresize.h"
48 #include "keymap.h"
50 /* Test for membership, allowing for t (actually any non-cons) to mean the
51 universal set. */
53 #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set))
55 static Lisp_Object merge_properties_sticky (Lisp_Object, Lisp_Object);
56 static INTERVAL merge_interval_right (INTERVAL);
57 static INTERVAL reproduce_tree (INTERVAL, INTERVAL);
59 /* Utility functions for intervals. */
61 /* Use these functions to set pointer slots of struct interval. */
63 static void
64 set_interval_left (INTERVAL i, INTERVAL left)
66 i->left = left;
69 static void
70 set_interval_right (INTERVAL i, INTERVAL right)
72 i->right = right;
75 /* Make the parent of D be whatever the parent of S is, regardless
76 of the type. This is used when balancing an interval tree. */
78 static void
79 copy_interval_parent (INTERVAL d, INTERVAL s)
81 d->up = s->up;
82 d->up_obj = s->up_obj;
85 /* Create the root interval of some object, a buffer or string. */
87 INTERVAL
88 create_root_interval (Lisp_Object parent)
90 INTERVAL new;
92 new = make_interval ();
94 if (! STRINGP (parent))
96 new->total_length = (BUF_Z (XBUFFER (parent))
97 - BUF_BEG (XBUFFER (parent)));
98 eassert (TOTAL_LENGTH (new) >= 0);
99 set_buffer_intervals (XBUFFER (parent), new);
100 new->position = BEG;
102 else
104 CHECK_IMPURE (parent, XSTRING (parent));
105 new->total_length = SCHARS (parent);
106 eassert (TOTAL_LENGTH (new) >= 0);
107 set_string_intervals (parent, new);
108 new->position = 0;
110 eassert (LENGTH (new) > 0);
112 set_interval_object (new, parent);
114 return new;
117 /* Make the interval TARGET have exactly the properties of SOURCE. */
119 void
120 copy_properties (register INTERVAL source, register INTERVAL target)
122 if (DEFAULT_INTERVAL_P (source) && DEFAULT_INTERVAL_P (target))
123 return;
125 COPY_INTERVAL_CACHE (source, target);
126 set_interval_plist (target, Fcopy_sequence (source->plist));
129 /* Merge the properties of interval SOURCE into the properties
130 of interval TARGET. That is to say, each property in SOURCE
131 is added to TARGET if TARGET has no such property as yet. */
133 static void
134 merge_properties (register INTERVAL source, register INTERVAL target)
136 register Lisp_Object o, sym, val;
138 if (DEFAULT_INTERVAL_P (source) && DEFAULT_INTERVAL_P (target))
139 return;
141 MERGE_INTERVAL_CACHE (source, target);
143 o = source->plist;
144 while (CONSP (o))
146 sym = XCAR (o);
147 o = XCDR (o);
148 CHECK_CONS (o);
150 val = target->plist;
151 while (CONSP (val) && !EQ (XCAR (val), sym))
153 val = XCDR (val);
154 if (!CONSP (val))
155 break;
156 val = XCDR (val);
159 if (NILP (val))
161 val = XCAR (o);
162 set_interval_plist (target, Fcons (sym, Fcons (val, target->plist)));
164 o = XCDR (o);
168 /* Return true if the two intervals have the same properties. */
170 bool
171 intervals_equal (INTERVAL i0, INTERVAL i1)
173 Lisp_Object i0_cdr, i0_sym;
174 Lisp_Object i1_cdr, i1_val;
176 if (DEFAULT_INTERVAL_P (i0) && DEFAULT_INTERVAL_P (i1))
177 return true;
179 if (DEFAULT_INTERVAL_P (i0) || DEFAULT_INTERVAL_P (i1))
180 return false;
182 i0_cdr = i0->plist;
183 i1_cdr = i1->plist;
184 while (CONSP (i0_cdr) && CONSP (i1_cdr))
186 i0_sym = XCAR (i0_cdr);
187 i0_cdr = XCDR (i0_cdr);
188 if (!CONSP (i0_cdr))
189 return false;
190 i1_val = i1->plist;
191 while (CONSP (i1_val) && !EQ (XCAR (i1_val), i0_sym))
193 i1_val = XCDR (i1_val);
194 if (!CONSP (i1_val))
195 return false;
196 i1_val = XCDR (i1_val);
199 /* i0 has something i1 doesn't. */
200 if (EQ (i1_val, Qnil))
201 return false;
203 /* i0 and i1 both have sym, but it has different values in each. */
204 if (!CONSP (i1_val)
205 || (i1_val = XCDR (i1_val), !CONSP (i1_val))
206 || !EQ (XCAR (i1_val), XCAR (i0_cdr)))
207 return false;
209 i0_cdr = XCDR (i0_cdr);
211 i1_cdr = XCDR (i1_cdr);
212 if (!CONSP (i1_cdr))
213 return false;
214 i1_cdr = XCDR (i1_cdr);
217 /* Lengths of the two plists were equal. */
218 return (NILP (i0_cdr) && NILP (i1_cdr));
222 /* Traverse an interval tree TREE, performing FUNCTION on each node.
223 No guarantee is made about the order of traversal.
224 Pass FUNCTION two args: an interval, and ARG. */
226 void
227 traverse_intervals_noorder (INTERVAL tree, void (*function) (INTERVAL, void *),
228 void *arg)
230 /* Minimize stack usage. */
231 while (tree)
233 (*function) (tree, arg);
234 if (!tree->right)
235 tree = tree->left;
236 else
238 traverse_intervals_noorder (tree->left, function, arg);
239 tree = tree->right;
244 /* Traverse an interval tree TREE, performing FUNCTION on each node.
245 Pass FUNCTION two args: an interval, and ARG. */
247 void
248 traverse_intervals (INTERVAL tree, ptrdiff_t position,
249 void (*function) (INTERVAL, Lisp_Object), Lisp_Object arg)
251 while (tree)
253 traverse_intervals (tree->left, position, function, arg);
254 position += LEFT_TOTAL_LENGTH (tree);
255 tree->position = position;
256 (*function) (tree, arg);
257 position += LENGTH (tree); tree = tree->right;
261 /* Assuming that a left child exists, perform the following operation:
264 / \ / \
265 B => A
266 / \ / \
270 static INTERVAL
271 rotate_right (INTERVAL A)
273 INTERVAL B = A->left;
274 INTERVAL c = B->right;
275 ptrdiff_t old_total = A->total_length;
277 eassert (old_total > 0);
278 eassert (LENGTH (A) > 0);
279 eassert (LENGTH (B) > 0);
281 /* Deal with any Parent of A; make it point to B. */
282 if (! ROOT_INTERVAL_P (A))
284 if (AM_LEFT_CHILD (A))
285 set_interval_left (INTERVAL_PARENT (A), B);
286 else
287 set_interval_right (INTERVAL_PARENT (A), B);
289 copy_interval_parent (B, A);
291 /* Make B the parent of A. */
292 set_interval_right (B, A);
293 set_interval_parent (A, B);
295 /* Make A point to c. */
296 set_interval_left (A, c);
297 if (c)
298 set_interval_parent (c, A);
300 /* A's total length is decreased by the length of B and its left child. */
301 A->total_length -= B->total_length - TOTAL_LENGTH (c);
302 eassert (TOTAL_LENGTH (A) > 0);
303 eassert (LENGTH (A) > 0);
305 /* B must have the same total length of A. */
306 B->total_length = old_total;
307 eassert (LENGTH (B) > 0);
309 return B;
312 /* Assuming that a right child exists, perform the following operation:
315 / \ / \
316 B => A
317 / \ / \
321 static INTERVAL
322 rotate_left (INTERVAL A)
324 INTERVAL B = A->right;
325 INTERVAL c = B->left;
326 ptrdiff_t old_total = A->total_length;
328 eassert (old_total > 0);
329 eassert (LENGTH (A) > 0);
330 eassert (LENGTH (B) > 0);
332 /* Deal with any parent of A; make it point to B. */
333 if (! ROOT_INTERVAL_P (A))
335 if (AM_LEFT_CHILD (A))
336 set_interval_left (INTERVAL_PARENT (A), B);
337 else
338 set_interval_right (INTERVAL_PARENT (A), B);
340 copy_interval_parent (B, A);
342 /* Make B the parent of A. */
343 set_interval_left (B, A);
344 set_interval_parent (A, B);
346 /* Make A point to c. */
347 set_interval_right (A, c);
348 if (c)
349 set_interval_parent (c, A);
351 /* A's total length is decreased by the length of B and its right child. */
352 A->total_length -= B->total_length - TOTAL_LENGTH (c);
353 eassert (TOTAL_LENGTH (A) > 0);
354 eassert (LENGTH (A) > 0);
356 /* B must have the same total length of A. */
357 B->total_length = old_total;
358 eassert (LENGTH (B) > 0);
360 return B;
363 /* Balance an interval tree with the assumption that the subtrees
364 themselves are already balanced. */
366 static INTERVAL
367 balance_an_interval (INTERVAL i)
369 register ptrdiff_t old_diff, new_diff;
371 eassert (LENGTH (i) > 0);
372 eassert (TOTAL_LENGTH (i) >= LENGTH (i));
374 while (1)
376 old_diff = LEFT_TOTAL_LENGTH (i) - RIGHT_TOTAL_LENGTH (i);
377 if (old_diff > 0)
379 /* Since the left child is longer, there must be one. */
380 new_diff = i->total_length - i->left->total_length
381 + RIGHT_TOTAL_LENGTH (i->left) - LEFT_TOTAL_LENGTH (i->left);
382 if (eabs (new_diff) >= old_diff)
383 break;
384 i = rotate_right (i);
385 balance_an_interval (i->right);
387 else if (old_diff < 0)
389 /* Since the right child is longer, there must be one. */
390 new_diff = i->total_length - i->right->total_length
391 + LEFT_TOTAL_LENGTH (i->right) - RIGHT_TOTAL_LENGTH (i->right);
392 if (eabs (new_diff) >= -old_diff)
393 break;
394 i = rotate_left (i);
395 balance_an_interval (i->left);
397 else
398 break;
400 return i;
403 /* Balance INTERVAL, potentially stuffing it back into its parent
404 Lisp Object. */
406 static INTERVAL
407 balance_possible_root_interval (INTERVAL interval)
409 Lisp_Object parent;
410 bool have_parent = false;
412 if (INTERVAL_HAS_OBJECT (interval))
414 have_parent = true;
415 GET_INTERVAL_OBJECT (parent, interval);
417 else if (!INTERVAL_HAS_PARENT (interval))
418 return interval;
420 interval = balance_an_interval (interval);
422 if (have_parent)
424 if (BUFFERP (parent))
425 set_buffer_intervals (XBUFFER (parent), interval);
426 else if (STRINGP (parent))
427 set_string_intervals (parent, interval);
430 return interval;
433 /* Balance the interval tree TREE. Balancing is by weight
434 (the amount of text). */
436 static INTERVAL
437 balance_intervals_internal (register INTERVAL tree)
439 /* Balance within each side. */
440 if (tree->left)
441 balance_intervals_internal (tree->left);
442 if (tree->right)
443 balance_intervals_internal (tree->right);
444 return balance_an_interval (tree);
447 /* Advertised interface to balance intervals. */
449 INTERVAL
450 balance_intervals (INTERVAL tree)
452 return tree ? balance_intervals_internal (tree) : NULL;
455 /* Rebalance text properties of B. */
457 static void
458 buffer_balance_intervals (struct buffer *b)
460 INTERVAL i;
462 eassert (b != NULL);
463 i = buffer_intervals (b);
464 if (i)
465 set_buffer_intervals (b, balance_an_interval (i));
468 /* Split INTERVAL into two pieces, starting the second piece at
469 character position OFFSET (counting from 0), relative to INTERVAL.
470 INTERVAL becomes the left-hand piece, and the right-hand piece
471 (second, lexicographically) is returned.
473 The size and position fields of the two intervals are set based upon
474 those of the original interval. The property list of the new interval
475 is reset, thus it is up to the caller to do the right thing with the
476 result.
478 Note that this does not change the position of INTERVAL; if it is a root,
479 it is still a root after this operation. */
481 INTERVAL
482 split_interval_right (INTERVAL interval, ptrdiff_t offset)
484 INTERVAL new = make_interval ();
485 ptrdiff_t position = interval->position;
486 ptrdiff_t new_length = LENGTH (interval) - offset;
488 new->position = position + offset;
489 set_interval_parent (new, interval);
491 if (NULL_RIGHT_CHILD (interval))
493 set_interval_right (interval, new);
494 new->total_length = new_length;
495 eassert (LENGTH (new) > 0);
497 else
499 /* Insert the new node between INTERVAL and its right child. */
500 set_interval_right (new, interval->right);
501 set_interval_parent (interval->right, new);
502 set_interval_right (interval, new);
503 new->total_length = new_length + new->right->total_length;
504 balance_an_interval (new);
507 balance_possible_root_interval (interval);
509 return new;
512 /* Split INTERVAL into two pieces, starting the second piece at
513 character position OFFSET (counting from 0), relative to INTERVAL.
514 INTERVAL becomes the right-hand piece, and the left-hand piece
515 (first, lexicographically) is returned.
517 The size and position fields of the two intervals are set based upon
518 those of the original interval. The property list of the new interval
519 is reset, thus it is up to the caller to do the right thing with the
520 result.
522 Note that this does not change the position of INTERVAL; if it is a root,
523 it is still a root after this operation. */
525 INTERVAL
526 split_interval_left (INTERVAL interval, ptrdiff_t offset)
528 INTERVAL new = make_interval ();
529 ptrdiff_t new_length = offset;
531 new->position = interval->position;
532 interval->position = interval->position + offset;
533 set_interval_parent (new, interval);
535 if (NULL_LEFT_CHILD (interval))
537 set_interval_left (interval, new);
538 new->total_length = new_length;
539 eassert (LENGTH (new) > 0);
541 else
543 /* Insert the new node between INTERVAL and its left child. */
544 set_interval_left (new, interval->left);
545 set_interval_parent (new->left, new);
546 set_interval_left (interval, new);
547 new->total_length = new_length + new->left->total_length;
548 balance_an_interval (new);
551 balance_possible_root_interval (interval);
553 return new;
556 /* Return the proper position for the first character
557 described by the interval tree SOURCE.
558 This is 1 if the parent is a buffer,
559 0 if the parent is a string or if there is no parent.
561 Don't use this function on an interval which is the child
562 of another interval! */
564 static int
565 interval_start_pos (INTERVAL source)
567 Lisp_Object parent;
569 if (!source)
570 return 0;
572 if (! INTERVAL_HAS_OBJECT (source))
573 return 0;
574 GET_INTERVAL_OBJECT (parent, source);
575 if (BUFFERP (parent))
576 return BUF_BEG (XBUFFER (parent));
577 return 0;
580 /* Find the interval containing text position POSITION in the text
581 represented by the interval tree TREE. POSITION is a buffer
582 position (starting from 1) or a string index (starting from 0).
583 If POSITION is at the end of the buffer or string,
584 return the interval containing the last character.
586 The `position' field, which is a cache of an interval's position,
587 is updated in the interval found. Other functions (e.g., next_interval)
588 will update this cache based on the result of find_interval. */
590 INTERVAL
591 find_interval (register INTERVAL tree, register ptrdiff_t position)
593 /* The distance from the left edge of the subtree at TREE
594 to POSITION. */
595 register ptrdiff_t relative_position;
597 if (!tree)
598 return NULL;
600 relative_position = position;
601 if (INTERVAL_HAS_OBJECT (tree))
603 Lisp_Object parent;
604 GET_INTERVAL_OBJECT (parent, tree);
605 if (BUFFERP (parent))
606 relative_position -= BUF_BEG (XBUFFER (parent));
609 eassert (relative_position <= TOTAL_LENGTH (tree));
611 tree = balance_possible_root_interval (tree);
613 while (1)
615 eassert (tree);
616 if (relative_position < LEFT_TOTAL_LENGTH (tree))
618 tree = tree->left;
620 else if (! NULL_RIGHT_CHILD (tree)
621 && relative_position >= (TOTAL_LENGTH (tree)
622 - RIGHT_TOTAL_LENGTH (tree)))
624 relative_position -= (TOTAL_LENGTH (tree)
625 - RIGHT_TOTAL_LENGTH (tree));
626 tree = tree->right;
628 else
630 tree->position
631 = (position - relative_position /* left edge of *tree. */
632 + LEFT_TOTAL_LENGTH (tree)); /* left edge of this interval. */
634 return tree;
639 /* Find the succeeding interval (lexicographically) to INTERVAL.
640 Sets the `position' field based on that of INTERVAL (see
641 find_interval). */
643 INTERVAL
644 next_interval (register INTERVAL interval)
646 register INTERVAL i = interval;
647 register ptrdiff_t next_position;
649 if (!i)
650 return NULL;
651 next_position = interval->position + LENGTH (interval);
653 if (! NULL_RIGHT_CHILD (i))
655 i = i->right;
656 while (! NULL_LEFT_CHILD (i))
657 i = i->left;
659 i->position = next_position;
660 return i;
663 while (! NULL_PARENT (i))
665 if (AM_LEFT_CHILD (i))
667 i = INTERVAL_PARENT (i);
668 i->position = next_position;
669 return i;
672 i = INTERVAL_PARENT (i);
675 return NULL;
678 /* Find the preceding interval (lexicographically) to INTERVAL.
679 Sets the `position' field based on that of INTERVAL (see
680 find_interval). */
682 INTERVAL
683 previous_interval (register INTERVAL interval)
685 register INTERVAL i;
687 if (!interval)
688 return NULL;
690 if (! NULL_LEFT_CHILD (interval))
692 i = interval->left;
693 while (! NULL_RIGHT_CHILD (i))
694 i = i->right;
696 i->position = interval->position - LENGTH (i);
697 return i;
700 i = interval;
701 while (! NULL_PARENT (i))
703 if (AM_RIGHT_CHILD (i))
705 i = INTERVAL_PARENT (i);
707 i->position = interval->position - LENGTH (i);
708 return i;
710 i = INTERVAL_PARENT (i);
713 return NULL;
716 /* Find the interval containing POS given some non-NULL INTERVAL
717 in the same tree. Note that we need to update interval->position
718 if we go down the tree.
719 To speed up the process, we assume that the ->position of
720 I and all its parents is already uptodate. */
721 INTERVAL
722 update_interval (register INTERVAL i, ptrdiff_t pos)
724 if (!i)
725 return NULL;
727 while (1)
729 if (pos < i->position)
731 /* Move left. */
732 if (pos >= i->position - TOTAL_LENGTH (i->left))
734 i->left->position = i->position - TOTAL_LENGTH (i->left)
735 + LEFT_TOTAL_LENGTH (i->left);
736 i = i->left; /* Move to the left child. */
738 else if (NULL_PARENT (i))
739 error ("Point before start of properties");
740 else
741 i = INTERVAL_PARENT (i);
742 continue;
744 else if (pos >= INTERVAL_LAST_POS (i))
746 /* Move right. */
747 if (pos < INTERVAL_LAST_POS (i) + TOTAL_LENGTH (i->right))
749 i->right->position = INTERVAL_LAST_POS (i)
750 + LEFT_TOTAL_LENGTH (i->right);
751 i = i->right; /* Move to the right child. */
753 else if (NULL_PARENT (i))
754 error ("Point %"pD"d after end of properties", pos);
755 else
756 i = INTERVAL_PARENT (i);
757 continue;
759 else
760 return i;
764 /* Effect an adjustment corresponding to the addition of LENGTH characters
765 of text. Do this by finding the interval containing POSITION in the
766 interval tree TREE, and then adjusting all of its ancestors by adding
767 LENGTH to them.
769 If POSITION is the first character of an interval, meaning that point
770 is actually between the two intervals, make the new text belong to
771 the interval which is "sticky".
773 If both intervals are "sticky", then make them belong to the left-most
774 interval. Another possibility would be to create a new interval for
775 this text, and make it have the merged properties of both ends. */
777 static INTERVAL
778 adjust_intervals_for_insertion (INTERVAL tree,
779 ptrdiff_t position, ptrdiff_t length)
781 INTERVAL i;
782 INTERVAL temp;
783 bool eobp = 0;
784 Lisp_Object parent;
785 ptrdiff_t offset;
787 eassert (TOTAL_LENGTH (tree) > 0);
789 GET_INTERVAL_OBJECT (parent, tree);
790 offset = (BUFFERP (parent) ? BUF_BEG (XBUFFER (parent)) : 0);
792 /* If inserting at point-max of a buffer, that position will be out
793 of range. Remember that buffer positions are 1-based. */
794 if (position >= TOTAL_LENGTH (tree) + offset)
796 position = TOTAL_LENGTH (tree) + offset;
797 eobp = 1;
800 i = find_interval (tree, position);
802 /* If in middle of an interval which is not sticky either way,
803 we must not just give its properties to the insertion.
804 So split this interval at the insertion point.
806 Originally, the if condition here was this:
807 (! (position == i->position || eobp)
808 && END_NONSTICKY_P (i)
809 && FRONT_NONSTICKY_P (i))
810 But, these macros are now unreliable because of introduction of
811 Vtext_property_default_nonsticky. So, we always check properties
812 one by one if POSITION is in middle of an interval. */
813 if (! (position == i->position || eobp))
815 Lisp_Object tail;
816 Lisp_Object front, rear;
818 tail = i->plist;
820 /* Properties font-sticky and rear-nonsticky override
821 Vtext_property_default_nonsticky. So, if they are t, we can
822 skip one by one checking of properties. */
823 rear = textget (i->plist, Qrear_nonsticky);
824 if (! CONSP (rear) && ! NILP (rear))
826 /* All properties are nonsticky. We split the interval. */
827 goto check_done;
829 front = textget (i->plist, Qfront_sticky);
830 if (! CONSP (front) && ! NILP (front))
832 /* All properties are sticky. We don't split the interval. */
833 tail = Qnil;
834 goto check_done;
837 /* Does any actual property pose an actual problem? We break
838 the loop if we find a nonsticky property. */
839 for (; CONSP (tail); tail = Fcdr (XCDR (tail)))
841 Lisp_Object prop, tmp;
842 prop = XCAR (tail);
844 /* Is this particular property front-sticky? */
845 if (CONSP (front) && ! NILP (Fmemq (prop, front)))
846 continue;
848 /* Is this particular property rear-nonsticky? */
849 if (CONSP (rear) && ! NILP (Fmemq (prop, rear)))
850 break;
852 /* Is this particular property recorded as sticky or
853 nonsticky in Vtext_property_default_nonsticky? */
854 tmp = Fassq (prop, Vtext_property_default_nonsticky);
855 if (CONSP (tmp))
857 if (NILP (tmp))
858 continue;
859 break;
862 /* By default, a text property is rear-sticky, thus we
863 continue the loop. */
866 check_done:
867 /* If any property is a real problem, split the interval. */
868 if (! NILP (tail))
870 temp = split_interval_right (i, position - i->position);
871 copy_properties (i, temp);
872 i = temp;
876 /* If we are positioned between intervals, check the stickiness of
877 both of them. We have to do this too, if we are at BEG or Z. */
878 if (position == i->position || eobp)
880 register INTERVAL prev;
882 if (position == BEG)
883 prev = 0;
884 else if (eobp)
886 prev = i;
887 i = 0;
889 else
890 prev = previous_interval (i);
892 /* Even if we are positioned between intervals, we default
893 to the left one if it exists. We extend it now and split
894 off a part later, if stickiness demands it. */
895 for (temp = prev ? prev : i; temp; temp = INTERVAL_PARENT_OR_NULL (temp))
897 temp->total_length += length;
898 temp = balance_possible_root_interval (temp);
901 /* If at least one interval has sticky properties,
902 we check the stickiness property by property.
904 Originally, the if condition here was this:
905 (END_NONSTICKY_P (prev) || FRONT_STICKY_P (i))
906 But, these macros are now unreliable because of introduction
907 of Vtext_property_default_nonsticky. So, we always have to
908 check stickiness of properties one by one. If cache of
909 stickiness is implemented in the future, we may be able to
910 use those macros again. */
911 if (1)
913 Lisp_Object pleft, pright;
914 struct interval newi;
916 RESET_INTERVAL (&newi);
917 pleft = prev ? prev->plist : Qnil;
918 pright = i ? i->plist : Qnil;
919 set_interval_plist (&newi, merge_properties_sticky (pleft, pright));
921 if (! prev) /* i.e. position == BEG */
923 if (! intervals_equal (i, &newi))
925 i = split_interval_left (i, length);
926 set_interval_plist (i, newi.plist);
929 else if (! intervals_equal (prev, &newi))
931 prev = split_interval_right (prev, position - prev->position);
932 set_interval_plist (prev, newi.plist);
933 if (i && intervals_equal (prev, i))
934 merge_interval_right (prev);
937 /* We will need to update the cache here later. */
939 else if (! prev && ! NILP (i->plist))
941 /* Just split off a new interval at the left.
942 Since I wasn't front-sticky, the empty plist is ok. */
943 i = split_interval_left (i, length);
947 /* Otherwise just extend the interval. */
948 else
950 for (temp = i; temp; temp = INTERVAL_PARENT_OR_NULL (temp))
952 temp->total_length += length;
953 temp = balance_possible_root_interval (temp);
957 return tree;
960 /* Any property might be front-sticky on the left, rear-sticky on the left,
961 front-sticky on the right, or rear-sticky on the right; the 16 combinations
962 can be arranged in a matrix with rows denoting the left conditions and
963 columns denoting the right conditions:
964 _ __ _
965 _ FR FR FR FR
966 FR__ 0 1 2 3
967 _FR 4 5 6 7
968 FR 8 9 A B
969 FR C D E F
971 left-props = '(front-sticky (p8 p9 pa pb pc pd pe pf)
972 rear-nonsticky (p4 p5 p6 p7 p8 p9 pa pb)
973 p0 L p1 L p2 L p3 L p4 L p5 L p6 L p7 L
974 p8 L p9 L pa L pb L pc L pd L pe L pf L)
975 right-props = '(front-sticky (p2 p3 p6 p7 pa pb pe pf)
976 rear-nonsticky (p1 p2 p5 p6 p9 pa pd pe)
977 p0 R p1 R p2 R p3 R p4 R p5 R p6 R p7 R
978 p8 R p9 R pa R pb R pc R pd R pe R pf R)
980 We inherit from whoever has a sticky side facing us. If both sides
981 do (cases 2, 3, E, and F), then we inherit from whichever side has a
982 non-nil value for the current property. If both sides do, then we take
983 from the left.
985 When we inherit a property, we get its stickiness as well as its value.
986 So, when we merge the above two lists, we expect to get this:
988 result = '(front-sticky (p6 p7 pa pb pc pd pe pf)
989 rear-nonsticky (p6 pa)
990 p0 L p1 L p2 L p3 L p6 R p7 R
991 pa R pb R pc L pd L pe L pf L)
993 The optimizable special cases are:
994 left rear-nonsticky = nil, right front-sticky = nil (inherit left)
995 left rear-nonsticky = t, right front-sticky = t (inherit right)
996 left rear-nonsticky = t, right front-sticky = nil (inherit none)
999 static Lisp_Object
1000 merge_properties_sticky (Lisp_Object pleft, Lisp_Object pright)
1002 Lisp_Object props, front, rear;
1003 Lisp_Object lfront, lrear, rfront, rrear;
1004 Lisp_Object tail1, tail2, sym, lval, rval, cat;
1005 bool use_left, use_right, lpresent;
1007 props = Qnil;
1008 front = Qnil;
1009 rear = Qnil;
1010 lfront = textget (pleft, Qfront_sticky);
1011 lrear = textget (pleft, Qrear_nonsticky);
1012 rfront = textget (pright, Qfront_sticky);
1013 rrear = textget (pright, Qrear_nonsticky);
1015 /* Go through each element of PRIGHT. */
1016 for (tail1 = pright; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
1018 Lisp_Object tmp;
1020 sym = XCAR (tail1);
1022 /* Sticky properties get special treatment. */
1023 if (EQ (sym, Qrear_nonsticky) || EQ (sym, Qfront_sticky))
1024 continue;
1026 rval = Fcar (XCDR (tail1));
1027 for (tail2 = pleft; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
1028 if (EQ (sym, XCAR (tail2)))
1029 break;
1031 /* Indicate whether the property is explicitly defined on the left.
1032 (We know it is defined explicitly on the right
1033 because otherwise we don't get here.) */
1034 lpresent = ! NILP (tail2);
1035 lval = (NILP (tail2) ? Qnil : Fcar (Fcdr (tail2)));
1037 /* Even if lrear or rfront say nothing about the stickiness of
1038 SYM, Vtext_property_default_nonsticky may give default
1039 stickiness to SYM. */
1040 tmp = Fassq (sym, Vtext_property_default_nonsticky);
1041 use_left = (lpresent
1042 && ! (TMEM (sym, lrear)
1043 || (CONSP (tmp) && ! NILP (XCDR (tmp)))));
1044 use_right = (TMEM (sym, rfront)
1045 || (CONSP (tmp) && NILP (XCDR (tmp))));
1046 if (use_left && use_right)
1048 if (NILP (lval))
1049 use_left = 0;
1050 else if (NILP (rval))
1051 use_right = 0;
1053 if (use_left)
1055 /* We build props as (value sym ...) rather than (sym value ...)
1056 because we plan to nreverse it when we're done. */
1057 props = Fcons (lval, Fcons (sym, props));
1058 if (TMEM (sym, lfront))
1059 front = Fcons (sym, front);
1060 if (TMEM (sym, lrear))
1061 rear = Fcons (sym, rear);
1063 else if (use_right)
1065 props = Fcons (rval, Fcons (sym, props));
1066 if (TMEM (sym, rfront))
1067 front = Fcons (sym, front);
1068 if (TMEM (sym, rrear))
1069 rear = Fcons (sym, rear);
1073 /* Now go through each element of PLEFT. */
1074 for (tail2 = pleft; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
1076 Lisp_Object tmp;
1078 sym = XCAR (tail2);
1080 /* Sticky properties get special treatment. */
1081 if (EQ (sym, Qrear_nonsticky) || EQ (sym, Qfront_sticky))
1082 continue;
1084 /* If sym is in PRIGHT, we've already considered it. */
1085 for (tail1 = pright; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
1086 if (EQ (sym, XCAR (tail1)))
1087 break;
1088 if (! NILP (tail1))
1089 continue;
1091 lval = Fcar (XCDR (tail2));
1093 /* Even if lrear or rfront say nothing about the stickiness of
1094 SYM, Vtext_property_default_nonsticky may give default
1095 stickiness to SYM. */
1096 tmp = Fassq (sym, Vtext_property_default_nonsticky);
1098 /* Since rval is known to be nil in this loop, the test simplifies. */
1099 if (! (TMEM (sym, lrear) || (CONSP (tmp) && ! NILP (XCDR (tmp)))))
1101 props = Fcons (lval, Fcons (sym, props));
1102 if (TMEM (sym, lfront))
1103 front = Fcons (sym, front);
1105 else if (TMEM (sym, rfront) || (CONSP (tmp) && NILP (XCDR (tmp))))
1107 /* The value is nil, but we still inherit the stickiness
1108 from the right. */
1109 front = Fcons (sym, front);
1110 if (TMEM (sym, rrear))
1111 rear = Fcons (sym, rear);
1114 props = Fnreverse (props);
1115 if (! NILP (rear))
1116 props = Fcons (Qrear_nonsticky, Fcons (Fnreverse (rear), props));
1118 cat = textget (props, Qcategory);
1119 if (! NILP (front)
1121 /* If we have inherited a front-stick category property that is t,
1122 we don't need to set up a detailed one. */
1123 ! (! NILP (cat) && SYMBOLP (cat)
1124 && EQ (Fget (cat, Qfront_sticky), Qt)))
1125 props = Fcons (Qfront_sticky, Fcons (Fnreverse (front), props));
1126 return props;
1130 /* Delete a node I from its interval tree by merging its subtrees
1131 into one subtree which is then returned. Caller is responsible for
1132 storing the resulting subtree into its parent. */
1134 static INTERVAL
1135 delete_node (register INTERVAL i)
1137 register INTERVAL migrate, this;
1138 register ptrdiff_t migrate_amt;
1140 if (!i->left)
1141 return i->right;
1142 if (!i->right)
1143 return i->left;
1145 migrate = i->left;
1146 migrate_amt = i->left->total_length;
1147 this = i->right;
1148 this->total_length += migrate_amt;
1149 while (this->left)
1151 this = this->left;
1152 this->total_length += migrate_amt;
1154 set_interval_left (this, migrate);
1155 set_interval_parent (migrate, this);
1156 eassert (LENGTH (this) > 0);
1157 eassert (LENGTH (i->right) > 0);
1159 return i->right;
1162 /* Delete interval I from its tree by calling `delete_node'
1163 and properly connecting the resultant subtree.
1165 I is presumed to be empty; that is, no adjustments are made
1166 for the length of I. */
1168 static void
1169 delete_interval (register INTERVAL i)
1171 register INTERVAL parent;
1172 ptrdiff_t amt = LENGTH (i);
1174 eassert (amt == 0); /* Only used on zero-length intervals now. */
1176 if (ROOT_INTERVAL_P (i))
1178 Lisp_Object owner;
1179 GET_INTERVAL_OBJECT (owner, i);
1180 parent = delete_node (i);
1181 if (parent)
1182 set_interval_object (parent, owner);
1184 if (BUFFERP (owner))
1185 set_buffer_intervals (XBUFFER (owner), parent);
1186 else if (STRINGP (owner))
1187 set_string_intervals (owner, parent);
1188 else
1189 emacs_abort ();
1191 return;
1194 parent = INTERVAL_PARENT (i);
1195 if (AM_LEFT_CHILD (i))
1197 set_interval_left (parent, delete_node (i));
1198 if (parent->left)
1199 set_interval_parent (parent->left, parent);
1201 else
1203 set_interval_right (parent, delete_node (i));
1204 if (parent->right)
1205 set_interval_parent (parent->right, parent);
1209 /* Find the interval in TREE corresponding to the relative position
1210 FROM and delete as much as possible of AMOUNT from that interval.
1211 Return the amount actually deleted, and if the interval was
1212 zeroed-out, delete that interval node from the tree.
1214 Note that FROM is actually origin zero, aka relative to the
1215 leftmost edge of tree. This is appropriate since we call ourselves
1216 recursively on subtrees.
1218 Do this by recursing down TREE to the interval in question, and
1219 deleting the appropriate amount of text. */
1221 static ptrdiff_t
1222 interval_deletion_adjustment (register INTERVAL tree, register ptrdiff_t from,
1223 register ptrdiff_t amount)
1225 register ptrdiff_t relative_position = from;
1227 if (!tree)
1228 return 0;
1230 /* Left branch. */
1231 if (relative_position < LEFT_TOTAL_LENGTH (tree))
1233 ptrdiff_t subtract = interval_deletion_adjustment (tree->left,
1234 relative_position,
1235 amount);
1236 tree->total_length -= subtract;
1237 eassert (LENGTH (tree) > 0);
1238 return subtract;
1240 /* Right branch. */
1241 else if (relative_position >= (TOTAL_LENGTH (tree)
1242 - RIGHT_TOTAL_LENGTH (tree)))
1244 ptrdiff_t subtract;
1246 relative_position -= (tree->total_length
1247 - RIGHT_TOTAL_LENGTH (tree));
1248 subtract = interval_deletion_adjustment (tree->right,
1249 relative_position,
1250 amount);
1251 tree->total_length -= subtract;
1252 eassert (LENGTH (tree) > 0);
1253 return subtract;
1255 /* Here -- this node. */
1256 else
1258 /* How much can we delete from this interval? */
1259 ptrdiff_t my_amount = ((tree->total_length
1260 - RIGHT_TOTAL_LENGTH (tree))
1261 - relative_position);
1263 if (amount > my_amount)
1264 amount = my_amount;
1266 tree->total_length -= amount;
1267 eassert (LENGTH (tree) >= 0);
1268 if (LENGTH (tree) == 0)
1269 delete_interval (tree);
1271 return amount;
1274 /* Never reach here. */
1277 /* Effect the adjustments necessary to the interval tree of BUFFER to
1278 correspond to the deletion of LENGTH characters from that buffer
1279 text. The deletion is effected at position START (which is a
1280 buffer position, i.e. origin 1). */
1282 static void
1283 adjust_intervals_for_deletion (struct buffer *buffer,
1284 ptrdiff_t start, ptrdiff_t length)
1286 ptrdiff_t left_to_delete = length;
1287 INTERVAL tree = buffer_intervals (buffer);
1288 Lisp_Object parent;
1289 ptrdiff_t offset;
1291 GET_INTERVAL_OBJECT (parent, tree);
1292 offset = (BUFFERP (parent) ? BUF_BEG (XBUFFER (parent)) : 0);
1294 if (!tree)
1295 return;
1297 eassert (start <= offset + TOTAL_LENGTH (tree)
1298 && start + length <= offset + TOTAL_LENGTH (tree));
1300 if (length == TOTAL_LENGTH (tree))
1302 set_buffer_intervals (buffer, NULL);
1303 return;
1306 if (ONLY_INTERVAL_P (tree))
1308 tree->total_length -= length;
1309 eassert (LENGTH (tree) > 0);
1310 return;
1313 if (start > offset + TOTAL_LENGTH (tree))
1314 start = offset + TOTAL_LENGTH (tree);
1315 while (left_to_delete > 0)
1317 left_to_delete -= interval_deletion_adjustment (tree, start - offset,
1318 left_to_delete);
1319 tree = buffer_intervals (buffer);
1320 if (left_to_delete == tree->total_length)
1322 set_buffer_intervals (buffer, NULL);
1323 return;
1328 /* Make the adjustments necessary to the interval tree of BUFFER to
1329 represent an addition or deletion of LENGTH characters starting
1330 at position START. Addition or deletion is indicated by the sign
1331 of LENGTH. */
1333 void
1334 offset_intervals (struct buffer *buffer, ptrdiff_t start, ptrdiff_t length)
1336 if (!buffer_intervals (buffer) || length == 0)
1337 return;
1339 if (length > 0)
1340 adjust_intervals_for_insertion (buffer_intervals (buffer),
1341 start, length);
1342 else
1343 adjust_intervals_for_deletion (buffer, start, -length);
1346 /* Merge interval I with its lexicographic successor. The resulting
1347 interval is returned, and has the properties of the original
1348 successor. The properties of I are lost. I is removed from the
1349 interval tree.
1351 IMPORTANT:
1352 The caller must verify that this is not the last (rightmost)
1353 interval. */
1355 static INTERVAL
1356 merge_interval_right (register INTERVAL i)
1358 register ptrdiff_t absorb = LENGTH (i);
1359 register INTERVAL successor;
1361 /* Find the succeeding interval. */
1362 if (! NULL_RIGHT_CHILD (i)) /* It's below us. Add absorb
1363 as we descend. */
1365 successor = i->right;
1366 while (! NULL_LEFT_CHILD (successor))
1368 successor->total_length += absorb;
1369 eassert (LENGTH (successor) > 0);
1370 successor = successor->left;
1373 successor->total_length += absorb;
1374 eassert (LENGTH (successor) > 0);
1375 delete_interval (i);
1376 return successor;
1379 /* Zero out this interval. */
1380 i->total_length -= absorb;
1381 eassert (TOTAL_LENGTH (i) >= 0);
1383 successor = i;
1384 while (! NULL_PARENT (successor)) /* It's above us. Subtract as
1385 we ascend. */
1387 if (AM_LEFT_CHILD (successor))
1389 successor = INTERVAL_PARENT (successor);
1390 delete_interval (i);
1391 return successor;
1394 successor = INTERVAL_PARENT (successor);
1395 successor->total_length -= absorb;
1396 eassert (LENGTH (successor) > 0);
1399 /* This must be the rightmost or last interval and cannot
1400 be merged right. The caller should have known. */
1401 emacs_abort ();
1404 /* Merge interval I with its lexicographic predecessor. The resulting
1405 interval is returned, and has the properties of the original predecessor.
1406 The properties of I are lost. Interval node I is removed from the tree.
1408 IMPORTANT:
1409 The caller must verify that this is not the first (leftmost) interval. */
1411 INTERVAL
1412 merge_interval_left (register INTERVAL i)
1414 register ptrdiff_t absorb = LENGTH (i);
1415 register INTERVAL predecessor;
1417 /* Find the preceding interval. */
1418 if (! NULL_LEFT_CHILD (i)) /* It's below us. Go down,
1419 adding ABSORB as we go. */
1421 predecessor = i->left;
1422 while (! NULL_RIGHT_CHILD (predecessor))
1424 predecessor->total_length += absorb;
1425 eassert (LENGTH (predecessor) > 0);
1426 predecessor = predecessor->right;
1429 predecessor->total_length += absorb;
1430 eassert (LENGTH (predecessor) > 0);
1431 delete_interval (i);
1432 return predecessor;
1435 /* Zero out this interval. */
1436 i->total_length -= absorb;
1437 eassert (TOTAL_LENGTH (i) >= 0);
1439 predecessor = i;
1440 while (! NULL_PARENT (predecessor)) /* It's above us. Go up,
1441 subtracting ABSORB. */
1443 if (AM_RIGHT_CHILD (predecessor))
1445 predecessor = INTERVAL_PARENT (predecessor);
1446 delete_interval (i);
1447 return predecessor;
1450 predecessor = INTERVAL_PARENT (predecessor);
1451 predecessor->total_length -= absorb;
1452 eassert (LENGTH (predecessor) > 0);
1455 /* This must be the leftmost or first interval and cannot
1456 be merged left. The caller should have known. */
1457 emacs_abort ();
1460 /* Create a copy of SOURCE but with the default value of UP. */
1462 static INTERVAL
1463 reproduce_interval (INTERVAL source)
1465 register INTERVAL target = make_interval ();
1467 eassert (LENGTH (source) > 0);
1469 target->total_length = source->total_length;
1470 target->position = source->position;
1472 copy_properties (source, target);
1474 if (! NULL_LEFT_CHILD (source))
1475 set_interval_left (target, reproduce_tree (source->left, target));
1476 if (! NULL_RIGHT_CHILD (source))
1477 set_interval_right (target, reproduce_tree (source->right, target));
1479 eassert (LENGTH (target) > 0);
1480 return target;
1483 /* Make an exact copy of interval tree SOURCE which descends from
1484 PARENT. This is done by recursing through SOURCE, copying
1485 the current interval and its properties, and then adjusting
1486 the pointers of the copy. */
1488 static INTERVAL
1489 reproduce_tree (INTERVAL source, INTERVAL parent)
1491 INTERVAL target = reproduce_interval (source);
1492 set_interval_parent (target, parent);
1493 return target;
1496 static INTERVAL
1497 reproduce_tree_obj (INTERVAL source, Lisp_Object parent)
1499 INTERVAL target = reproduce_interval (source);
1500 set_interval_object (target, parent);
1501 return target;
1504 /* Insert the intervals of SOURCE into BUFFER at POSITION.
1505 LENGTH is the length of the text in SOURCE.
1507 The `position' field of the SOURCE intervals is assumed to be
1508 consistent with its parent; therefore, SOURCE must be an
1509 interval tree made with copy_interval or must be the whole
1510 tree of a buffer or a string.
1512 This is used in insdel.c when inserting Lisp_Strings into the
1513 buffer. The text corresponding to SOURCE is already in the buffer
1514 when this is called. The intervals of new tree are a copy of those
1515 belonging to the string being inserted; intervals are never
1516 shared.
1518 If the inserted text had no intervals associated, and we don't
1519 want to inherit the surrounding text's properties, this function
1520 simply returns -- offset_intervals should handle placing the
1521 text in the correct interval, depending on the sticky bits.
1523 If the inserted text had properties (intervals), then there are two
1524 cases -- either insertion happened in the middle of some interval,
1525 or between two intervals.
1527 If the text goes into the middle of an interval, then new intervals
1528 are created in the middle, and new text has the union of its properties
1529 and those of the text into which it was inserted.
1531 If the text goes between two intervals, then if neither interval
1532 had its appropriate sticky property set (front_sticky, rear_sticky),
1533 the new text has only its properties. If one of the sticky properties
1534 is set, then the new text "sticks" to that region and its properties
1535 depend on merging as above. If both the preceding and succeeding
1536 intervals to the new text are "sticky", then the new text retains
1537 only its properties, as if neither sticky property were set. Perhaps
1538 we should consider merging all three sets of properties onto the new
1539 text... */
1541 void
1542 graft_intervals_into_buffer (INTERVAL source, ptrdiff_t position,
1543 ptrdiff_t length, struct buffer *buffer,
1544 bool inherit)
1546 INTERVAL tree = buffer_intervals (buffer);
1547 INTERVAL under, over, this;
1548 ptrdiff_t over_used;
1550 /* If the new text has no properties, then with inheritance it
1551 becomes part of whatever interval it was inserted into.
1552 To prevent inheritance, we must clear out the properties
1553 of the newly inserted text. */
1554 if (!source)
1556 Lisp_Object buf;
1557 if (!inherit && tree && length > 0)
1559 XSETBUFFER (buf, buffer);
1560 set_text_properties_1 (make_number (position),
1561 make_number (position + length),
1562 Qnil, buf,
1563 find_interval (tree, position));
1565 /* Shouldn't be necessary. --Stef */
1566 buffer_balance_intervals (buffer);
1567 return;
1570 eassert (length == TOTAL_LENGTH (source));
1572 if ((BUF_Z (buffer) - BUF_BEG (buffer)) == length)
1574 /* The inserted text constitutes the whole buffer, so
1575 simply copy over the interval structure. */
1576 Lisp_Object buf;
1578 XSETBUFFER (buf, buffer);
1579 set_buffer_intervals (buffer, reproduce_tree_obj (source, buf));
1580 buffer_intervals (buffer)->position = BUF_BEG (buffer);
1581 eassert (buffer_intervals (buffer)->up_obj == 1);
1582 return;
1584 else if (!tree)
1586 /* Create an interval tree in which to place a copy
1587 of the intervals of the inserted string. */
1588 Lisp_Object buf;
1590 XSETBUFFER (buf, buffer);
1591 tree = create_root_interval (buf);
1593 /* Paranoia -- the text has already been added, so
1594 this buffer should be of non-zero length. */
1595 eassert (TOTAL_LENGTH (tree) > 0);
1597 this = under = find_interval (tree, position);
1598 eassert (under);
1599 over = find_interval (source, interval_start_pos (source));
1601 /* Here for insertion in the middle of an interval.
1602 Split off an equivalent interval to the right,
1603 then don't bother with it any more. */
1605 if (position > under->position)
1607 INTERVAL end_unchanged
1608 = split_interval_left (this, position - under->position);
1609 copy_properties (under, end_unchanged);
1610 under->position = position;
1612 else
1614 /* This call may have some effect because previous_interval may
1615 update `position' fields of intervals. Thus, don't ignore it
1616 for the moment. Someone please tell me the truth (K.Handa). */
1617 INTERVAL prev = previous_interval (under);
1618 (void) prev;
1619 #if 0
1620 /* But, this code surely has no effect. And, anyway,
1621 END_NONSTICKY_P is unreliable now. */
1622 if (prev && !END_NONSTICKY_P (prev))
1623 prev = 0;
1624 #endif /* 0 */
1627 /* Insertion is now at beginning of UNDER. */
1629 /* The inserted text "sticks" to the interval `under',
1630 which means it gets those properties.
1631 The properties of under are the result of
1632 adjust_intervals_for_insertion, so stickiness has
1633 already been taken care of. */
1635 /* OVER is the interval we are copying from next.
1636 OVER_USED says how many characters' worth of OVER
1637 have already been copied into target intervals.
1638 UNDER is the next interval in the target. */
1639 over_used = 0;
1640 while (over)
1642 /* If UNDER is longer than OVER, split it. */
1643 if (LENGTH (over) - over_used < LENGTH (under))
1645 this = split_interval_left (under, LENGTH (over) - over_used);
1646 copy_properties (under, this);
1648 else
1649 this = under;
1651 /* THIS is now the interval to copy or merge into.
1652 OVER covers all of it. */
1653 if (inherit)
1654 merge_properties (over, this);
1655 else
1656 copy_properties (over, this);
1658 /* If THIS and OVER end at the same place,
1659 advance OVER to a new source interval. */
1660 if (LENGTH (this) == LENGTH (over) - over_used)
1662 over = next_interval (over);
1663 over_used = 0;
1665 else
1666 /* Otherwise just record that more of OVER has been used. */
1667 over_used += LENGTH (this);
1669 /* Always advance to a new target interval. */
1670 under = next_interval (this);
1673 buffer_balance_intervals (buffer);
1676 /* Get the value of property PROP from PLIST,
1677 which is the plist of an interval.
1678 We check for direct properties, for categories with property PROP,
1679 and for PROP appearing on the default-text-properties list. */
1681 Lisp_Object
1682 textget (Lisp_Object plist, register Lisp_Object prop)
1684 return lookup_char_property (plist, prop, 1);
1687 Lisp_Object
1688 lookup_char_property (Lisp_Object plist, Lisp_Object prop, bool textprop)
1690 Lisp_Object tail, fallback = Qnil;
1692 for (tail = plist; CONSP (tail); tail = Fcdr (XCDR (tail)))
1694 register Lisp_Object tem;
1695 tem = XCAR (tail);
1696 if (EQ (prop, tem))
1697 return Fcar (XCDR (tail));
1698 if (EQ (tem, Qcategory))
1700 tem = Fcar (XCDR (tail));
1701 if (SYMBOLP (tem))
1702 fallback = Fget (tem, prop);
1706 if (! NILP (fallback))
1707 return fallback;
1708 /* Check for alternative properties. */
1709 tail = Fassq (prop, Vchar_property_alias_alist);
1710 if (! NILP (tail))
1712 tail = XCDR (tail);
1713 for (; NILP (fallback) && CONSP (tail); tail = XCDR (tail))
1714 fallback = Fplist_get (plist, XCAR (tail));
1717 if (textprop && NILP (fallback) && CONSP (Vdefault_text_properties))
1718 fallback = Fplist_get (Vdefault_text_properties, prop);
1719 return fallback;
1723 /* Set point in BUFFER "temporarily" to CHARPOS, which corresponds to
1724 byte position BYTEPOS. */
1726 void
1727 temp_set_point_both (struct buffer *buffer,
1728 ptrdiff_t charpos, ptrdiff_t bytepos)
1730 /* In a single-byte buffer, the two positions must be equal. */
1731 eassert (BUF_ZV (buffer) != BUF_ZV_BYTE (buffer) || charpos == bytepos);
1733 eassert (charpos <= bytepos);
1734 eassert (charpos <= BUF_ZV (buffer) || BUF_BEGV (buffer) <= charpos);
1736 SET_BUF_PT_BOTH (buffer, charpos, bytepos);
1739 /* Set point "temporarily", without checking any text properties. */
1741 void
1742 temp_set_point (struct buffer *buffer, ptrdiff_t charpos)
1744 temp_set_point_both (buffer, charpos,
1745 buf_charpos_to_bytepos (buffer, charpos));
1748 /* Set point in BUFFER to CHARPOS. If the target position is
1749 before an intangible character, move to an ok place. */
1751 void
1752 set_point (ptrdiff_t charpos)
1754 set_point_both (charpos, buf_charpos_to_bytepos (current_buffer, charpos));
1757 /* Set PT from MARKER's clipped position. */
1759 void
1760 set_point_from_marker (Lisp_Object marker)
1762 ptrdiff_t charpos = clip_to_bounds (BEGV, marker_position (marker), ZV);
1763 ptrdiff_t bytepos = marker_byte_position (marker);
1765 /* Don't trust the byte position if the marker belongs to a
1766 different buffer. */
1767 if (XMARKER (marker)->buffer != current_buffer)
1768 bytepos = buf_charpos_to_bytepos (current_buffer, charpos);
1769 else
1770 bytepos = clip_to_bounds (BEGV_BYTE, bytepos, ZV_BYTE);
1771 set_point_both (charpos, bytepos);
1774 /* If there's an invisible character at position POS + TEST_OFFS in the
1775 current buffer, and the invisible property has a `stickiness' such that
1776 inserting a character at position POS would inherit the property it,
1777 return POS + ADJ, otherwise return POS. If TEST_INTANG, intangibility
1778 is required as well as invisibility.
1780 TEST_OFFS should be either 0 or -1, and ADJ should be either 1 or -1.
1782 Note that `stickiness' is determined by overlay marker insertion types,
1783 if the invisible property comes from an overlay. */
1785 static ptrdiff_t
1786 adjust_for_invis_intang (ptrdiff_t pos, ptrdiff_t test_offs, ptrdiff_t adj,
1787 bool test_intang)
1789 Lisp_Object invis_propval, invis_overlay;
1790 Lisp_Object test_pos;
1792 if ((adj < 0 && pos + adj < BEGV) || (adj > 0 && pos + adj > ZV))
1793 /* POS + ADJ would be beyond the buffer bounds, so do no adjustment. */
1794 return pos;
1796 test_pos = make_number (pos + test_offs);
1798 invis_propval
1799 = get_char_property_and_overlay (test_pos, Qinvisible, Qnil,
1800 &invis_overlay);
1802 if ((!test_intang
1803 || ! NILP (Fget_char_property (test_pos, Qintangible, Qnil)))
1804 && TEXT_PROP_MEANS_INVISIBLE (invis_propval)
1805 /* This next test is true if the invisible property has a stickiness
1806 such that an insertion at POS would inherit it. */
1807 && (NILP (invis_overlay)
1808 /* Invisible property is from a text-property. */
1809 ? (text_property_stickiness (Qinvisible, make_number (pos), Qnil)
1810 == (test_offs == 0 ? 1 : -1))
1811 /* Invisible property is from an overlay. */
1812 : (test_offs == 0
1813 ? XMARKER (OVERLAY_START (invis_overlay))->insertion_type == 0
1814 : XMARKER (OVERLAY_END (invis_overlay))->insertion_type == 1)))
1815 pos += adj;
1817 return pos;
1820 /* Set point in BUFFER to CHARPOS, which corresponds to byte
1821 position BYTEPOS. If the target position is
1822 before an intangible character, move to an ok place. */
1824 void
1825 set_point_both (ptrdiff_t charpos, ptrdiff_t bytepos)
1827 register INTERVAL to, from, toprev, fromprev;
1828 ptrdiff_t buffer_point;
1829 ptrdiff_t old_position = PT;
1830 /* This ensures that we move forward past intangible text when the
1831 initial position is the same as the destination, in the rare
1832 instances where this is important, e.g. in line-move-finish
1833 (simple.el). */
1834 bool backwards = charpos < old_position;
1835 bool have_overlays;
1836 ptrdiff_t original_position;
1838 bset_point_before_scroll (current_buffer, Qnil);
1840 if (charpos == PT)
1841 return;
1843 /* In a single-byte buffer, the two positions must be equal. */
1844 eassert (ZV != ZV_BYTE || charpos == bytepos);
1846 /* Check this now, before checking if the buffer has any intervals.
1847 That way, we can catch conditions which break this sanity check
1848 whether or not there are intervals in the buffer. */
1849 eassert (charpos <= ZV && charpos >= BEGV);
1851 have_overlays = buffer_has_overlays ();
1853 /* If we have no text properties and overlays,
1854 then we can do it quickly. */
1855 if (!buffer_intervals (current_buffer) && ! have_overlays)
1857 temp_set_point_both (current_buffer, charpos, bytepos);
1858 return;
1861 /* Set TO to the interval containing the char after CHARPOS,
1862 and TOPREV to the interval containing the char before CHARPOS.
1863 Either one may be null. They may be equal. */
1864 to = find_interval (buffer_intervals (current_buffer), charpos);
1865 if (charpos == BEGV)
1866 toprev = 0;
1867 else if (to && to->position == charpos)
1868 toprev = previous_interval (to);
1869 else
1870 toprev = to;
1872 buffer_point = (PT == ZV ? ZV - 1 : PT);
1874 /* Set FROM to the interval containing the char after PT,
1875 and FROMPREV to the interval containing the char before PT.
1876 Either one may be null. They may be equal. */
1877 /* We could cache this and save time. */
1878 from = find_interval (buffer_intervals (current_buffer), buffer_point);
1879 if (buffer_point == BEGV)
1880 fromprev = 0;
1881 else if (from && from->position == PT)
1882 fromprev = previous_interval (from);
1883 else if (buffer_point != PT)
1884 fromprev = from, from = 0;
1885 else
1886 fromprev = from;
1888 /* Moving within an interval. */
1889 if (to == from && toprev == fromprev && INTERVAL_VISIBLE_P (to)
1890 && ! have_overlays)
1892 temp_set_point_both (current_buffer, charpos, bytepos);
1893 return;
1896 original_position = charpos;
1898 /* If the new position is between two intangible characters
1899 with the same intangible property value,
1900 move forward or backward until a change in that property. */
1901 if (NILP (Vinhibit_point_motion_hooks)
1902 && ((to && toprev)
1903 || have_overlays)
1904 /* Intangibility never stops us from positioning at the beginning
1905 or end of the buffer, so don't bother checking in that case. */
1906 && charpos != BEGV && charpos != ZV)
1908 Lisp_Object pos;
1909 Lisp_Object intangible_propval;
1911 if (backwards)
1913 /* If the preceding character is both intangible and invisible,
1914 and the invisible property is `rear-sticky', perturb it so
1915 that the search starts one character earlier -- this ensures
1916 that point can never move to the end of an invisible/
1917 intangible/rear-sticky region. */
1918 charpos = adjust_for_invis_intang (charpos, -1, -1, 1);
1920 XSETINT (pos, charpos);
1922 /* If following char is intangible,
1923 skip back over all chars with matching intangible property. */
1925 intangible_propval = Fget_char_property (pos, Qintangible, Qnil);
1927 if (! NILP (intangible_propval))
1929 while (XINT (pos) > BEGV
1930 && EQ (Fget_char_property (make_number (XINT (pos) - 1),
1931 Qintangible, Qnil),
1932 intangible_propval))
1933 pos = Fprevious_char_property_change (pos, Qnil);
1935 /* Set CHARPOS from POS, and if the final intangible character
1936 that we skipped over is also invisible, and the invisible
1937 property is `front-sticky', perturb it to be one character
1938 earlier -- this ensures that point can never move to the
1939 beginning of an invisible/intangible/front-sticky region. */
1940 charpos = adjust_for_invis_intang (XINT (pos), 0, -1, 0);
1943 else
1945 /* If the following character is both intangible and invisible,
1946 and the invisible property is `front-sticky', perturb it so
1947 that the search starts one character later -- this ensures
1948 that point can never move to the beginning of an
1949 invisible/intangible/front-sticky region. */
1950 charpos = adjust_for_invis_intang (charpos, 0, 1, 1);
1952 XSETINT (pos, charpos);
1954 /* If preceding char is intangible,
1955 skip forward over all chars with matching intangible property. */
1957 intangible_propval = Fget_char_property (make_number (charpos - 1),
1958 Qintangible, Qnil);
1960 if (! NILP (intangible_propval))
1962 while (XINT (pos) < ZV
1963 && EQ (Fget_char_property (pos, Qintangible, Qnil),
1964 intangible_propval))
1965 pos = Fnext_char_property_change (pos, Qnil);
1967 /* Set CHARPOS from POS, and if the final intangible character
1968 that we skipped over is also invisible, and the invisible
1969 property is `rear-sticky', perturb it to be one character
1970 later -- this ensures that point can never move to the
1971 end of an invisible/intangible/rear-sticky region. */
1972 charpos = adjust_for_invis_intang (XINT (pos), -1, 1, 0);
1976 bytepos = buf_charpos_to_bytepos (current_buffer, charpos);
1979 if (charpos != original_position)
1981 /* Set TO to the interval containing the char after CHARPOS,
1982 and TOPREV to the interval containing the char before CHARPOS.
1983 Either one may be null. They may be equal. */
1984 to = find_interval (buffer_intervals (current_buffer), charpos);
1985 if (charpos == BEGV)
1986 toprev = 0;
1987 else if (to && to->position == charpos)
1988 toprev = previous_interval (to);
1989 else
1990 toprev = to;
1993 /* Here TO is the interval after the stopping point
1994 and TOPREV is the interval before the stopping point.
1995 One or the other may be null. */
1997 temp_set_point_both (current_buffer, charpos, bytepos);
1999 /* We run point-left and point-entered hooks here, if the
2000 two intervals are not equivalent. These hooks take
2001 (old_point, new_point) as arguments. */
2002 if (NILP (Vinhibit_point_motion_hooks)
2003 && (! intervals_equal (from, to)
2004 || ! intervals_equal (fromprev, toprev)))
2006 Lisp_Object leave_after, leave_before, enter_after, enter_before;
2008 if (fromprev)
2009 leave_before = textget (fromprev->plist, Qpoint_left);
2010 else
2011 leave_before = Qnil;
2013 if (from)
2014 leave_after = textget (from->plist, Qpoint_left);
2015 else
2016 leave_after = Qnil;
2018 if (toprev)
2019 enter_before = textget (toprev->plist, Qpoint_entered);
2020 else
2021 enter_before = Qnil;
2023 if (to)
2024 enter_after = textget (to->plist, Qpoint_entered);
2025 else
2026 enter_after = Qnil;
2028 if (! EQ (leave_before, enter_before) && !NILP (leave_before))
2029 call2 (leave_before, make_number (old_position),
2030 make_number (charpos));
2031 if (! EQ (leave_after, enter_after) && !NILP (leave_after))
2032 call2 (leave_after, make_number (old_position),
2033 make_number (charpos));
2035 if (! EQ (enter_before, leave_before) && !NILP (enter_before))
2036 call2 (enter_before, make_number (old_position),
2037 make_number (charpos));
2038 if (! EQ (enter_after, leave_after) && !NILP (enter_after))
2039 call2 (enter_after, make_number (old_position),
2040 make_number (charpos));
2044 /* Move point to POSITION, unless POSITION is inside an intangible
2045 segment that reaches all the way to point. */
2047 void
2048 move_if_not_intangible (ptrdiff_t position)
2050 Lisp_Object pos;
2051 Lisp_Object intangible_propval;
2053 XSETINT (pos, position);
2055 if (! NILP (Vinhibit_point_motion_hooks))
2056 /* If intangible is inhibited, always move point to POSITION. */
2058 else if (PT < position && XINT (pos) < ZV)
2060 /* We want to move forward, so check the text before POSITION. */
2062 intangible_propval = Fget_char_property (pos,
2063 Qintangible, Qnil);
2065 /* If following char is intangible,
2066 skip back over all chars with matching intangible property. */
2067 if (! NILP (intangible_propval))
2068 while (XINT (pos) > BEGV
2069 && EQ (Fget_char_property (make_number (XINT (pos) - 1),
2070 Qintangible, Qnil),
2071 intangible_propval))
2072 pos = Fprevious_char_property_change (pos, Qnil);
2074 else if (XINT (pos) > BEGV)
2076 /* We want to move backward, so check the text after POSITION. */
2078 intangible_propval = Fget_char_property (make_number (XINT (pos) - 1),
2079 Qintangible, Qnil);
2081 /* If following char is intangible,
2082 skip forward over all chars with matching intangible property. */
2083 if (! NILP (intangible_propval))
2084 while (XINT (pos) < ZV
2085 && EQ (Fget_char_property (pos, Qintangible, Qnil),
2086 intangible_propval))
2087 pos = Fnext_char_property_change (pos, Qnil);
2090 else if (position < BEGV)
2091 position = BEGV;
2092 else if (position > ZV)
2093 position = ZV;
2095 /* If the whole stretch between PT and POSITION isn't intangible,
2096 try moving to POSITION (which means we actually move farther
2097 if POSITION is inside of intangible text). */
2099 if (XINT (pos) != PT)
2100 SET_PT (position);
2103 /* If text at position POS has property PROP, set *VAL to the property
2104 value, *START and *END to the beginning and end of a region that
2105 has the same property, and return true. Otherwise return false.
2107 OBJECT is the string or buffer to look for the property in;
2108 nil means the current buffer. */
2110 bool
2111 get_property_and_range (ptrdiff_t pos, Lisp_Object prop, Lisp_Object *val,
2112 ptrdiff_t *start, ptrdiff_t *end, Lisp_Object object)
2114 INTERVAL i, prev, next;
2116 if (NILP (object))
2117 i = find_interval (buffer_intervals (current_buffer), pos);
2118 else if (BUFFERP (object))
2119 i = find_interval (buffer_intervals (XBUFFER (object)), pos);
2120 else if (STRINGP (object))
2121 i = find_interval (string_intervals (object), pos);
2122 else
2123 emacs_abort ();
2125 if (!i || (i->position + LENGTH (i) <= pos))
2126 return 0;
2127 *val = textget (i->plist, prop);
2128 if (NILP (*val))
2129 return 0;
2131 next = i; /* remember it in advance */
2132 prev = previous_interval (i);
2133 while (prev
2134 && EQ (*val, textget (prev->plist, prop)))
2135 i = prev, prev = previous_interval (prev);
2136 *start = i->position;
2138 next = next_interval (i);
2139 while (next && EQ (*val, textget (next->plist, prop)))
2140 i = next, next = next_interval (next);
2141 *end = i->position + LENGTH (i);
2143 return 1;
2146 /* Return the proper local keymap TYPE for position POSITION in
2147 BUFFER; TYPE should be one of `keymap' or `local-map'. Use the map
2148 specified by the PROP property, if any. Otherwise, if TYPE is
2149 `local-map' use BUFFER's local map. */
2151 Lisp_Object
2152 get_local_map (ptrdiff_t position, struct buffer *buffer, Lisp_Object type)
2154 Lisp_Object prop, lispy_position, lispy_buffer;
2155 ptrdiff_t old_begv, old_zv, old_begv_byte, old_zv_byte;
2156 ptrdiff_t count = SPECPDL_INDEX ();
2158 position = clip_to_bounds (BUF_BEGV (buffer), position, BUF_ZV (buffer));
2160 /* Ignore narrowing, so that a local map continues to be valid even if
2161 the visible region contains no characters and hence no properties. */
2162 old_begv = BUF_BEGV (buffer);
2163 old_zv = BUF_ZV (buffer);
2164 old_begv_byte = BUF_BEGV_BYTE (buffer);
2165 old_zv_byte = BUF_ZV_BYTE (buffer);
2167 specbind (Qinhibit_quit, Qt);
2168 SET_BUF_BEGV_BOTH (buffer, BUF_BEG (buffer), BUF_BEG_BYTE (buffer));
2169 SET_BUF_ZV_BOTH (buffer, BUF_Z (buffer), BUF_Z_BYTE (buffer));
2171 XSETFASTINT (lispy_position, position);
2172 XSETBUFFER (lispy_buffer, buffer);
2173 /* First check if the CHAR has any property. This is because when
2174 we click with the mouse, the mouse pointer is really pointing
2175 to the CHAR after POS. */
2176 prop = Fget_char_property (lispy_position, type, lispy_buffer);
2177 /* If not, look at the POS's properties. This is necessary because when
2178 editing a field with a `local-map' property, we want insertion at the end
2179 to obey the `local-map' property. */
2180 if (NILP (prop))
2181 prop = Fget_pos_property (lispy_position, type, lispy_buffer);
2183 SET_BUF_BEGV_BOTH (buffer, old_begv, old_begv_byte);
2184 SET_BUF_ZV_BOTH (buffer, old_zv, old_zv_byte);
2185 unbind_to (count, Qnil);
2187 /* Use the local map only if it is valid. */
2188 prop = get_keymap (prop, 0, 0);
2189 if (CONSP (prop))
2190 return prop;
2192 if (EQ (type, Qkeymap))
2193 return Qnil;
2194 else
2195 return BVAR (buffer, keymap);
2198 /* Produce an interval tree reflecting the intervals in
2199 TREE from START to START + LENGTH.
2200 The new interval tree has no parent and has a starting-position of 0. */
2202 INTERVAL
2203 copy_intervals (INTERVAL tree, ptrdiff_t start, ptrdiff_t length)
2205 register INTERVAL i, new, t;
2206 register ptrdiff_t got, prevlen;
2208 if (!tree || length <= 0)
2209 return NULL;
2211 i = find_interval (tree, start);
2212 eassert (i && LENGTH (i) > 0);
2214 /* If there is only one interval and it's the default, return nil. */
2215 if ((start - i->position + 1 + length) < LENGTH (i)
2216 && DEFAULT_INTERVAL_P (i))
2217 return NULL;
2219 new = make_interval ();
2220 new->position = 0;
2221 got = (LENGTH (i) - (start - i->position));
2222 new->total_length = length;
2223 eassert (TOTAL_LENGTH (new) >= 0);
2224 copy_properties (i, new);
2226 t = new;
2227 prevlen = got;
2228 while (got < length)
2230 i = next_interval (i);
2231 t = split_interval_right (t, prevlen);
2232 copy_properties (i, t);
2233 prevlen = LENGTH (i);
2234 got += prevlen;
2237 return balance_an_interval (new);
2240 /* Give STRING the properties of BUFFER from POSITION to LENGTH. */
2242 void
2243 copy_intervals_to_string (Lisp_Object string, struct buffer *buffer,
2244 ptrdiff_t position, ptrdiff_t length)
2246 INTERVAL interval_copy = copy_intervals (buffer_intervals (buffer),
2247 position, length);
2248 if (!interval_copy)
2249 return;
2251 set_interval_object (interval_copy, string);
2252 set_string_intervals (string, interval_copy);
2255 /* Return true if strings S1 and S2 have identical properties.
2256 Assume they have identical characters. */
2258 bool
2259 compare_string_intervals (Lisp_Object s1, Lisp_Object s2)
2261 INTERVAL i1, i2;
2262 ptrdiff_t pos = 0;
2263 ptrdiff_t end = SCHARS (s1);
2265 i1 = find_interval (string_intervals (s1), 0);
2266 i2 = find_interval (string_intervals (s2), 0);
2268 while (pos < end)
2270 /* Determine how far we can go before we reach the end of I1 or I2. */
2271 ptrdiff_t len1 = (i1 != 0 ? INTERVAL_LAST_POS (i1) : end) - pos;
2272 ptrdiff_t len2 = (i2 != 0 ? INTERVAL_LAST_POS (i2) : end) - pos;
2273 ptrdiff_t distance = min (len1, len2);
2275 /* If we ever find a mismatch between the strings,
2276 they differ. */
2277 if (! intervals_equal (i1, i2))
2278 return 0;
2280 /* Advance POS till the end of the shorter interval,
2281 and advance one or both interval pointers for the new position. */
2282 pos += distance;
2283 if (len1 == distance)
2284 i1 = next_interval (i1);
2285 if (len2 == distance)
2286 i2 = next_interval (i2);
2288 return 1;
2291 /* Recursively adjust interval I in the current buffer
2292 for setting enable_multibyte_characters to MULTI_FLAG.
2293 The range of interval I is START ... END in characters,
2294 START_BYTE ... END_BYTE in bytes. */
2296 static void
2297 set_intervals_multibyte_1 (INTERVAL i, bool multi_flag,
2298 ptrdiff_t start, ptrdiff_t start_byte,
2299 ptrdiff_t end, ptrdiff_t end_byte)
2301 /* Fix the length of this interval. */
2302 if (multi_flag)
2303 i->total_length = end - start;
2304 else
2305 i->total_length = end_byte - start_byte;
2306 eassert (TOTAL_LENGTH (i) >= 0);
2308 if (TOTAL_LENGTH (i) == 0)
2310 delete_interval (i);
2311 return;
2314 /* Recursively fix the length of the subintervals. */
2315 if (i->left)
2317 ptrdiff_t left_end, left_end_byte;
2319 if (multi_flag)
2321 ptrdiff_t temp;
2322 left_end_byte = start_byte + LEFT_TOTAL_LENGTH (i);
2323 left_end = BYTE_TO_CHAR (left_end_byte);
2325 temp = CHAR_TO_BYTE (left_end);
2327 /* If LEFT_END_BYTE is in the middle of a character,
2328 adjust it and LEFT_END to a char boundary. */
2329 if (left_end_byte > temp)
2331 left_end_byte = temp;
2333 if (left_end_byte < temp)
2335 left_end--;
2336 left_end_byte = CHAR_TO_BYTE (left_end);
2339 else
2341 left_end = start + LEFT_TOTAL_LENGTH (i);
2342 left_end_byte = CHAR_TO_BYTE (left_end);
2345 set_intervals_multibyte_1 (i->left, multi_flag, start, start_byte,
2346 left_end, left_end_byte);
2348 if (i->right)
2350 ptrdiff_t right_start_byte, right_start;
2352 if (multi_flag)
2354 ptrdiff_t temp;
2356 right_start_byte = end_byte - RIGHT_TOTAL_LENGTH (i);
2357 right_start = BYTE_TO_CHAR (right_start_byte);
2359 /* If RIGHT_START_BYTE is in the middle of a character,
2360 adjust it and RIGHT_START to a char boundary. */
2361 temp = CHAR_TO_BYTE (right_start);
2363 if (right_start_byte < temp)
2365 right_start_byte = temp;
2367 if (right_start_byte > temp)
2369 right_start++;
2370 right_start_byte = CHAR_TO_BYTE (right_start);
2373 else
2375 right_start = end - RIGHT_TOTAL_LENGTH (i);
2376 right_start_byte = CHAR_TO_BYTE (right_start);
2379 set_intervals_multibyte_1 (i->right, multi_flag,
2380 right_start, right_start_byte,
2381 end, end_byte);
2384 /* Rounding to char boundaries can theoretically make this interval
2385 spurious. If so, delete one child, and copy its property list
2386 to this interval. */
2387 if (LEFT_TOTAL_LENGTH (i) + RIGHT_TOTAL_LENGTH (i) >= TOTAL_LENGTH (i))
2389 if ((i)->left)
2391 set_interval_plist (i, i->left->plist);
2392 (i)->left->total_length = 0;
2393 delete_interval ((i)->left);
2395 else
2397 set_interval_plist (i, i->right->plist);
2398 (i)->right->total_length = 0;
2399 delete_interval ((i)->right);
2404 /* Update the intervals of the current buffer
2405 to fit the contents as multibyte (if MULTI_FLAG)
2406 or to fit them as non-multibyte (if not MULTI_FLAG). */
2408 void
2409 set_intervals_multibyte (bool multi_flag)
2411 INTERVAL i = buffer_intervals (current_buffer);
2413 if (i)
2414 set_intervals_multibyte_1 (i, multi_flag, BEG, BEG_BYTE, Z, Z_BYTE);