* etc/NEWS: Adjust to match previous patch.
[emacs.git] / src / intervals.c
blobd17d80ac86502901329590794e551ff5dbaa7b21
1 /* Code for doing intervals.
2 Copyright (C) 1993-1995, 1997-1998, 2001-2017 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 <http://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, Lisp_Object), Lisp_Object arg)
229 /* Minimize stack usage. */
230 while (tree)
232 (*function) (tree, arg);
233 if (!tree->right)
234 tree = tree->left;
235 else
237 traverse_intervals_noorder (tree->left, function, arg);
238 tree = tree->right;
243 /* Traverse an interval tree TREE, performing FUNCTION on each node.
244 Pass FUNCTION two args: an interval, and ARG. */
246 void
247 traverse_intervals (INTERVAL tree, ptrdiff_t position,
248 void (*function) (INTERVAL, Lisp_Object), Lisp_Object arg)
250 while (tree)
252 traverse_intervals (tree->left, position, function, arg);
253 position += LEFT_TOTAL_LENGTH (tree);
254 tree->position = position;
255 (*function) (tree, arg);
256 position += LENGTH (tree); tree = tree->right;
260 #if 0
262 static int icount;
263 static int idepth;
264 static int zero_length;
266 /* These functions are temporary, for debugging purposes only. */
268 INTERVAL search_interval, found_interval;
270 void
271 check_for_interval (INTERVAL i)
273 if (i == search_interval)
275 found_interval = i;
276 icount++;
280 INTERVAL
281 search_for_interval (INTERVAL i, INTERVAL tree)
283 icount = 0;
284 search_interval = i;
285 found_interval = NULL;
286 traverse_intervals_noorder (tree, &check_for_interval, Qnil);
287 return found_interval;
290 static void
291 inc_interval_count (INTERVAL i)
293 icount++;
294 if (LENGTH (i) == 0)
295 zero_length++;
296 if (depth > idepth)
297 idepth = depth;
301 count_intervals (INTERVAL i)
303 icount = 0;
304 idepth = 0;
305 zero_length = 0;
306 traverse_intervals_noorder (i, &inc_interval_count, Qnil);
308 return icount;
311 static INTERVAL
312 root_interval (INTERVAL interval)
314 register INTERVAL i = interval;
316 while (! ROOT_INTERVAL_P (i))
317 i = INTERVAL_PARENT (i);
319 return i;
321 #endif
323 /* Assuming that a left child exists, perform the following operation:
326 / \ / \
327 B => A
328 / \ / \
332 static INTERVAL
333 rotate_right (INTERVAL A)
335 INTERVAL B = A->left;
336 INTERVAL c = B->right;
337 ptrdiff_t old_total = A->total_length;
339 eassert (old_total > 0);
340 eassert (LENGTH (A) > 0);
341 eassert (LENGTH (B) > 0);
343 /* Deal with any Parent of A; make it point to B. */
344 if (! ROOT_INTERVAL_P (A))
346 if (AM_LEFT_CHILD (A))
347 set_interval_left (INTERVAL_PARENT (A), B);
348 else
349 set_interval_right (INTERVAL_PARENT (A), B);
351 copy_interval_parent (B, A);
353 /* Make B the parent of A. */
354 set_interval_right (B, A);
355 set_interval_parent (A, B);
357 /* Make A point to c. */
358 set_interval_left (A, c);
359 if (c)
360 set_interval_parent (c, A);
362 /* A's total length is decreased by the length of B and its left child. */
363 A->total_length -= B->total_length - TOTAL_LENGTH (c);
364 eassert (TOTAL_LENGTH (A) > 0);
365 eassert (LENGTH (A) > 0);
367 /* B must have the same total length of A. */
368 B->total_length = old_total;
369 eassert (LENGTH (B) > 0);
371 return B;
374 /* Assuming that a right child exists, perform the following operation:
377 / \ / \
378 B => A
379 / \ / \
383 static INTERVAL
384 rotate_left (INTERVAL A)
386 INTERVAL B = A->right;
387 INTERVAL c = B->left;
388 ptrdiff_t old_total = A->total_length;
390 eassert (old_total > 0);
391 eassert (LENGTH (A) > 0);
392 eassert (LENGTH (B) > 0);
394 /* Deal with any parent of A; make it point to B. */
395 if (! ROOT_INTERVAL_P (A))
397 if (AM_LEFT_CHILD (A))
398 set_interval_left (INTERVAL_PARENT (A), B);
399 else
400 set_interval_right (INTERVAL_PARENT (A), B);
402 copy_interval_parent (B, A);
404 /* Make B the parent of A. */
405 set_interval_left (B, A);
406 set_interval_parent (A, B);
408 /* Make A point to c. */
409 set_interval_right (A, c);
410 if (c)
411 set_interval_parent (c, A);
413 /* A's total length is decreased by the length of B and its right child. */
414 A->total_length -= B->total_length - TOTAL_LENGTH (c);
415 eassert (TOTAL_LENGTH (A) > 0);
416 eassert (LENGTH (A) > 0);
418 /* B must have the same total length of A. */
419 B->total_length = old_total;
420 eassert (LENGTH (B) > 0);
422 return B;
425 /* Balance an interval tree with the assumption that the subtrees
426 themselves are already balanced. */
428 static INTERVAL
429 balance_an_interval (INTERVAL i)
431 register ptrdiff_t old_diff, new_diff;
433 eassert (LENGTH (i) > 0);
434 eassert (TOTAL_LENGTH (i) >= LENGTH (i));
436 while (1)
438 old_diff = LEFT_TOTAL_LENGTH (i) - RIGHT_TOTAL_LENGTH (i);
439 if (old_diff > 0)
441 /* Since the left child is longer, there must be one. */
442 new_diff = i->total_length - i->left->total_length
443 + RIGHT_TOTAL_LENGTH (i->left) - LEFT_TOTAL_LENGTH (i->left);
444 if (eabs (new_diff) >= old_diff)
445 break;
446 i = rotate_right (i);
447 balance_an_interval (i->right);
449 else if (old_diff < 0)
451 /* Since the right child is longer, there must be one. */
452 new_diff = i->total_length - i->right->total_length
453 + LEFT_TOTAL_LENGTH (i->right) - RIGHT_TOTAL_LENGTH (i->right);
454 if (eabs (new_diff) >= -old_diff)
455 break;
456 i = rotate_left (i);
457 balance_an_interval (i->left);
459 else
460 break;
462 return i;
465 /* Balance INTERVAL, potentially stuffing it back into its parent
466 Lisp Object. */
468 static INTERVAL
469 balance_possible_root_interval (INTERVAL interval)
471 Lisp_Object parent;
472 bool have_parent = false;
474 if (INTERVAL_HAS_OBJECT (interval))
476 have_parent = true;
477 GET_INTERVAL_OBJECT (parent, interval);
479 else if (!INTERVAL_HAS_PARENT (interval))
480 return interval;
482 interval = balance_an_interval (interval);
484 if (have_parent)
486 if (BUFFERP (parent))
487 set_buffer_intervals (XBUFFER (parent), interval);
488 else if (STRINGP (parent))
489 set_string_intervals (parent, interval);
492 return interval;
495 /* Balance the interval tree TREE. Balancing is by weight
496 (the amount of text). */
498 static INTERVAL
499 balance_intervals_internal (register INTERVAL tree)
501 /* Balance within each side. */
502 if (tree->left)
503 balance_intervals_internal (tree->left);
504 if (tree->right)
505 balance_intervals_internal (tree->right);
506 return balance_an_interval (tree);
509 /* Advertised interface to balance intervals. */
511 INTERVAL
512 balance_intervals (INTERVAL tree)
514 return tree ? balance_intervals_internal (tree) : NULL;
517 /* Rebalance text properties of B. */
519 static void
520 buffer_balance_intervals (struct buffer *b)
522 INTERVAL i;
524 eassert (b != NULL);
525 i = buffer_intervals (b);
526 if (i)
527 set_buffer_intervals (b, balance_an_interval (i));
530 /* Split INTERVAL into two pieces, starting the second piece at
531 character position OFFSET (counting from 0), relative to INTERVAL.
532 INTERVAL becomes the left-hand piece, and the right-hand piece
533 (second, lexicographically) is returned.
535 The size and position fields of the two intervals are set based upon
536 those of the original interval. The property list of the new interval
537 is reset, thus it is up to the caller to do the right thing with the
538 result.
540 Note that this does not change the position of INTERVAL; if it is a root,
541 it is still a root after this operation. */
543 INTERVAL
544 split_interval_right (INTERVAL interval, ptrdiff_t offset)
546 INTERVAL new = make_interval ();
547 ptrdiff_t position = interval->position;
548 ptrdiff_t new_length = LENGTH (interval) - offset;
550 new->position = position + offset;
551 set_interval_parent (new, interval);
553 if (NULL_RIGHT_CHILD (interval))
555 set_interval_right (interval, new);
556 new->total_length = new_length;
557 eassert (LENGTH (new) > 0);
559 else
561 /* Insert the new node between INTERVAL and its right child. */
562 set_interval_right (new, interval->right);
563 set_interval_parent (interval->right, new);
564 set_interval_right (interval, new);
565 new->total_length = new_length + new->right->total_length;
566 balance_an_interval (new);
569 balance_possible_root_interval (interval);
571 return new;
574 /* Split INTERVAL into two pieces, starting the second piece at
575 character position OFFSET (counting from 0), relative to INTERVAL.
576 INTERVAL becomes the right-hand piece, and the left-hand piece
577 (first, lexicographically) is returned.
579 The size and position fields of the two intervals are set based upon
580 those of the original interval. The property list of the new interval
581 is reset, thus it is up to the caller to do the right thing with the
582 result.
584 Note that this does not change the position of INTERVAL; if it is a root,
585 it is still a root after this operation. */
587 INTERVAL
588 split_interval_left (INTERVAL interval, ptrdiff_t offset)
590 INTERVAL new = make_interval ();
591 ptrdiff_t new_length = offset;
593 new->position = interval->position;
594 interval->position = interval->position + offset;
595 set_interval_parent (new, interval);
597 if (NULL_LEFT_CHILD (interval))
599 set_interval_left (interval, new);
600 new->total_length = new_length;
601 eassert (LENGTH (new) > 0);
603 else
605 /* Insert the new node between INTERVAL and its left child. */
606 set_interval_left (new, interval->left);
607 set_interval_parent (new->left, new);
608 set_interval_left (interval, new);
609 new->total_length = new_length + new->left->total_length;
610 balance_an_interval (new);
613 balance_possible_root_interval (interval);
615 return new;
618 /* Return the proper position for the first character
619 described by the interval tree SOURCE.
620 This is 1 if the parent is a buffer,
621 0 if the parent is a string or if there is no parent.
623 Don't use this function on an interval which is the child
624 of another interval! */
626 static int
627 interval_start_pos (INTERVAL source)
629 Lisp_Object parent;
631 if (!source)
632 return 0;
634 if (! INTERVAL_HAS_OBJECT (source))
635 return 0;
636 GET_INTERVAL_OBJECT (parent, source);
637 if (BUFFERP (parent))
638 return BUF_BEG (XBUFFER (parent));
639 return 0;
642 /* Find the interval containing text position POSITION in the text
643 represented by the interval tree TREE. POSITION is a buffer
644 position (starting from 1) or a string index (starting from 0).
645 If POSITION is at the end of the buffer or string,
646 return the interval containing the last character.
648 The `position' field, which is a cache of an interval's position,
649 is updated in the interval found. Other functions (e.g., next_interval)
650 will update this cache based on the result of find_interval. */
652 INTERVAL
653 find_interval (register INTERVAL tree, register ptrdiff_t position)
655 /* The distance from the left edge of the subtree at TREE
656 to POSITION. */
657 register ptrdiff_t relative_position;
659 if (!tree)
660 return NULL;
662 relative_position = position;
663 if (INTERVAL_HAS_OBJECT (tree))
665 Lisp_Object parent;
666 GET_INTERVAL_OBJECT (parent, tree);
667 if (BUFFERP (parent))
668 relative_position -= BUF_BEG (XBUFFER (parent));
671 eassert (relative_position <= TOTAL_LENGTH (tree));
673 tree = balance_possible_root_interval (tree);
675 while (1)
677 eassert (tree);
678 if (relative_position < LEFT_TOTAL_LENGTH (tree))
680 tree = tree->left;
682 else if (! NULL_RIGHT_CHILD (tree)
683 && relative_position >= (TOTAL_LENGTH (tree)
684 - RIGHT_TOTAL_LENGTH (tree)))
686 relative_position -= (TOTAL_LENGTH (tree)
687 - RIGHT_TOTAL_LENGTH (tree));
688 tree = tree->right;
690 else
692 tree->position
693 = (position - relative_position /* left edge of *tree. */
694 + LEFT_TOTAL_LENGTH (tree)); /* left edge of this interval. */
696 return tree;
701 /* Find the succeeding interval (lexicographically) to INTERVAL.
702 Sets the `position' field based on that of INTERVAL (see
703 find_interval). */
705 INTERVAL
706 next_interval (register INTERVAL interval)
708 register INTERVAL i = interval;
709 register ptrdiff_t next_position;
711 if (!i)
712 return NULL;
713 next_position = interval->position + LENGTH (interval);
715 if (! NULL_RIGHT_CHILD (i))
717 i = i->right;
718 while (! NULL_LEFT_CHILD (i))
719 i = i->left;
721 i->position = next_position;
722 return i;
725 while (! NULL_PARENT (i))
727 if (AM_LEFT_CHILD (i))
729 i = INTERVAL_PARENT (i);
730 i->position = next_position;
731 return i;
734 i = INTERVAL_PARENT (i);
737 return NULL;
740 /* Find the preceding interval (lexicographically) to INTERVAL.
741 Sets the `position' field based on that of INTERVAL (see
742 find_interval). */
744 INTERVAL
745 previous_interval (register INTERVAL interval)
747 register INTERVAL i;
749 if (!interval)
750 return NULL;
752 if (! NULL_LEFT_CHILD (interval))
754 i = interval->left;
755 while (! NULL_RIGHT_CHILD (i))
756 i = i->right;
758 i->position = interval->position - LENGTH (i);
759 return i;
762 i = interval;
763 while (! NULL_PARENT (i))
765 if (AM_RIGHT_CHILD (i))
767 i = INTERVAL_PARENT (i);
769 i->position = interval->position - LENGTH (i);
770 return i;
772 i = INTERVAL_PARENT (i);
775 return NULL;
778 /* Find the interval containing POS given some non-NULL INTERVAL
779 in the same tree. Note that we need to update interval->position
780 if we go down the tree.
781 To speed up the process, we assume that the ->position of
782 I and all its parents is already uptodate. */
783 INTERVAL
784 update_interval (register INTERVAL i, ptrdiff_t pos)
786 if (!i)
787 return NULL;
789 while (1)
791 if (pos < i->position)
793 /* Move left. */
794 if (pos >= i->position - TOTAL_LENGTH (i->left))
796 i->left->position = i->position - TOTAL_LENGTH (i->left)
797 + LEFT_TOTAL_LENGTH (i->left);
798 i = i->left; /* Move to the left child. */
800 else if (NULL_PARENT (i))
801 error ("Point before start of properties");
802 else
803 i = INTERVAL_PARENT (i);
804 continue;
806 else if (pos >= INTERVAL_LAST_POS (i))
808 /* Move right. */
809 if (pos < INTERVAL_LAST_POS (i) + TOTAL_LENGTH (i->right))
811 i->right->position = INTERVAL_LAST_POS (i)
812 + LEFT_TOTAL_LENGTH (i->right);
813 i = i->right; /* Move to the right child. */
815 else if (NULL_PARENT (i))
816 error ("Point %"pD"d after end of properties", pos);
817 else
818 i = INTERVAL_PARENT (i);
819 continue;
821 else
822 return i;
826 /* Effect an adjustment corresponding to the addition of LENGTH characters
827 of text. Do this by finding the interval containing POSITION in the
828 interval tree TREE, and then adjusting all of its ancestors by adding
829 LENGTH to them.
831 If POSITION is the first character of an interval, meaning that point
832 is actually between the two intervals, make the new text belong to
833 the interval which is "sticky".
835 If both intervals are "sticky", then make them belong to the left-most
836 interval. Another possibility would be to create a new interval for
837 this text, and make it have the merged properties of both ends. */
839 static INTERVAL
840 adjust_intervals_for_insertion (INTERVAL tree,
841 ptrdiff_t position, ptrdiff_t length)
843 INTERVAL i;
844 INTERVAL temp;
845 bool eobp = 0;
846 Lisp_Object parent;
847 ptrdiff_t offset;
849 eassert (TOTAL_LENGTH (tree) > 0);
851 GET_INTERVAL_OBJECT (parent, tree);
852 offset = (BUFFERP (parent) ? BUF_BEG (XBUFFER (parent)) : 0);
854 /* If inserting at point-max of a buffer, that position will be out
855 of range. Remember that buffer positions are 1-based. */
856 if (position >= TOTAL_LENGTH (tree) + offset)
858 position = TOTAL_LENGTH (tree) + offset;
859 eobp = 1;
862 i = find_interval (tree, position);
864 /* If in middle of an interval which is not sticky either way,
865 we must not just give its properties to the insertion.
866 So split this interval at the insertion point.
868 Originally, the if condition here was this:
869 (! (position == i->position || eobp)
870 && END_NONSTICKY_P (i)
871 && FRONT_NONSTICKY_P (i))
872 But, these macros are now unreliable because of introduction of
873 Vtext_property_default_nonsticky. So, we always check properties
874 one by one if POSITION is in middle of an interval. */
875 if (! (position == i->position || eobp))
877 Lisp_Object tail;
878 Lisp_Object front, rear;
880 tail = i->plist;
882 /* Properties font-sticky and rear-nonsticky override
883 Vtext_property_default_nonsticky. So, if they are t, we can
884 skip one by one checking of properties. */
885 rear = textget (i->plist, Qrear_nonsticky);
886 if (! CONSP (rear) && ! NILP (rear))
888 /* All properties are nonsticky. We split the interval. */
889 goto check_done;
891 front = textget (i->plist, Qfront_sticky);
892 if (! CONSP (front) && ! NILP (front))
894 /* All properties are sticky. We don't split the interval. */
895 tail = Qnil;
896 goto check_done;
899 /* Does any actual property pose an actual problem? We break
900 the loop if we find a nonsticky property. */
901 for (; CONSP (tail); tail = Fcdr (XCDR (tail)))
903 Lisp_Object prop, tmp;
904 prop = XCAR (tail);
906 /* Is this particular property front-sticky? */
907 if (CONSP (front) && ! NILP (Fmemq (prop, front)))
908 continue;
910 /* Is this particular property rear-nonsticky? */
911 if (CONSP (rear) && ! NILP (Fmemq (prop, rear)))
912 break;
914 /* Is this particular property recorded as sticky or
915 nonsticky in Vtext_property_default_nonsticky? */
916 tmp = Fassq (prop, Vtext_property_default_nonsticky);
917 if (CONSP (tmp))
919 if (NILP (tmp))
920 continue;
921 break;
924 /* By default, a text property is rear-sticky, thus we
925 continue the loop. */
928 check_done:
929 /* If any property is a real problem, split the interval. */
930 if (! NILP (tail))
932 temp = split_interval_right (i, position - i->position);
933 copy_properties (i, temp);
934 i = temp;
938 /* If we are positioned between intervals, check the stickiness of
939 both of them. We have to do this too, if we are at BEG or Z. */
940 if (position == i->position || eobp)
942 register INTERVAL prev;
944 if (position == BEG)
945 prev = 0;
946 else if (eobp)
948 prev = i;
949 i = 0;
951 else
952 prev = previous_interval (i);
954 /* Even if we are positioned between intervals, we default
955 to the left one if it exists. We extend it now and split
956 off a part later, if stickiness demands it. */
957 for (temp = prev ? prev : i; temp; temp = INTERVAL_PARENT_OR_NULL (temp))
959 temp->total_length += length;
960 temp = balance_possible_root_interval (temp);
963 /* If at least one interval has sticky properties,
964 we check the stickiness property by property.
966 Originally, the if condition here was this:
967 (END_NONSTICKY_P (prev) || FRONT_STICKY_P (i))
968 But, these macros are now unreliable because of introduction
969 of Vtext_property_default_nonsticky. So, we always have to
970 check stickiness of properties one by one. If cache of
971 stickiness is implemented in the future, we may be able to
972 use those macros again. */
973 if (1)
975 Lisp_Object pleft, pright;
976 struct interval newi;
978 RESET_INTERVAL (&newi);
979 pleft = prev ? prev->plist : Qnil;
980 pright = i ? i->plist : Qnil;
981 set_interval_plist (&newi, merge_properties_sticky (pleft, pright));
983 if (! prev) /* i.e. position == BEG */
985 if (! intervals_equal (i, &newi))
987 i = split_interval_left (i, length);
988 set_interval_plist (i, newi.plist);
991 else if (! intervals_equal (prev, &newi))
993 prev = split_interval_right (prev, position - prev->position);
994 set_interval_plist (prev, newi.plist);
995 if (i && intervals_equal (prev, i))
996 merge_interval_right (prev);
999 /* We will need to update the cache here later. */
1001 else if (! prev && ! NILP (i->plist))
1003 /* Just split off a new interval at the left.
1004 Since I wasn't front-sticky, the empty plist is ok. */
1005 i = split_interval_left (i, length);
1009 /* Otherwise just extend the interval. */
1010 else
1012 for (temp = i; temp; temp = INTERVAL_PARENT_OR_NULL (temp))
1014 temp->total_length += length;
1015 temp = balance_possible_root_interval (temp);
1019 return tree;
1022 /* Any property might be front-sticky on the left, rear-sticky on the left,
1023 front-sticky on the right, or rear-sticky on the right; the 16 combinations
1024 can be arranged in a matrix with rows denoting the left conditions and
1025 columns denoting the right conditions:
1026 _ __ _
1027 _ FR FR FR FR
1028 FR__ 0 1 2 3
1029 _FR 4 5 6 7
1030 FR 8 9 A B
1031 FR C D E F
1033 left-props = '(front-sticky (p8 p9 pa pb pc pd pe pf)
1034 rear-nonsticky (p4 p5 p6 p7 p8 p9 pa pb)
1035 p0 L p1 L p2 L p3 L p4 L p5 L p6 L p7 L
1036 p8 L p9 L pa L pb L pc L pd L pe L pf L)
1037 right-props = '(front-sticky (p2 p3 p6 p7 pa pb pe pf)
1038 rear-nonsticky (p1 p2 p5 p6 p9 pa pd pe)
1039 p0 R p1 R p2 R p3 R p4 R p5 R p6 R p7 R
1040 p8 R p9 R pa R pb R pc R pd R pe R pf R)
1042 We inherit from whoever has a sticky side facing us. If both sides
1043 do (cases 2, 3, E, and F), then we inherit from whichever side has a
1044 non-nil value for the current property. If both sides do, then we take
1045 from the left.
1047 When we inherit a property, we get its stickiness as well as its value.
1048 So, when we merge the above two lists, we expect to get this:
1050 result = '(front-sticky (p6 p7 pa pb pc pd pe pf)
1051 rear-nonsticky (p6 pa)
1052 p0 L p1 L p2 L p3 L p6 R p7 R
1053 pa R pb R pc L pd L pe L pf L)
1055 The optimizable special cases are:
1056 left rear-nonsticky = nil, right front-sticky = nil (inherit left)
1057 left rear-nonsticky = t, right front-sticky = t (inherit right)
1058 left rear-nonsticky = t, right front-sticky = nil (inherit none)
1061 static Lisp_Object
1062 merge_properties_sticky (Lisp_Object pleft, Lisp_Object pright)
1064 Lisp_Object props, front, rear;
1065 Lisp_Object lfront, lrear, rfront, rrear;
1066 Lisp_Object tail1, tail2, sym, lval, rval, cat;
1067 bool use_left, use_right, lpresent;
1069 props = Qnil;
1070 front = Qnil;
1071 rear = Qnil;
1072 lfront = textget (pleft, Qfront_sticky);
1073 lrear = textget (pleft, Qrear_nonsticky);
1074 rfront = textget (pright, Qfront_sticky);
1075 rrear = textget (pright, Qrear_nonsticky);
1077 /* Go through each element of PRIGHT. */
1078 for (tail1 = pright; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
1080 Lisp_Object tmp;
1082 sym = XCAR (tail1);
1084 /* Sticky properties get special treatment. */
1085 if (EQ (sym, Qrear_nonsticky) || EQ (sym, Qfront_sticky))
1086 continue;
1088 rval = Fcar (XCDR (tail1));
1089 for (tail2 = pleft; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
1090 if (EQ (sym, XCAR (tail2)))
1091 break;
1093 /* Indicate whether the property is explicitly defined on the left.
1094 (We know it is defined explicitly on the right
1095 because otherwise we don't get here.) */
1096 lpresent = ! NILP (tail2);
1097 lval = (NILP (tail2) ? Qnil : Fcar (Fcdr (tail2)));
1099 /* Even if lrear or rfront say nothing about the stickiness of
1100 SYM, Vtext_property_default_nonsticky may give default
1101 stickiness to SYM. */
1102 tmp = Fassq (sym, Vtext_property_default_nonsticky);
1103 use_left = (lpresent
1104 && ! (TMEM (sym, lrear)
1105 || (CONSP (tmp) && ! NILP (XCDR (tmp)))));
1106 use_right = (TMEM (sym, rfront)
1107 || (CONSP (tmp) && NILP (XCDR (tmp))));
1108 if (use_left && use_right)
1110 if (NILP (lval))
1111 use_left = 0;
1112 else if (NILP (rval))
1113 use_right = 0;
1115 if (use_left)
1117 /* We build props as (value sym ...) rather than (sym value ...)
1118 because we plan to nreverse it when we're done. */
1119 props = Fcons (lval, Fcons (sym, props));
1120 if (TMEM (sym, lfront))
1121 front = Fcons (sym, front);
1122 if (TMEM (sym, lrear))
1123 rear = Fcons (sym, rear);
1125 else if (use_right)
1127 props = Fcons (rval, Fcons (sym, props));
1128 if (TMEM (sym, rfront))
1129 front = Fcons (sym, front);
1130 if (TMEM (sym, rrear))
1131 rear = Fcons (sym, rear);
1135 /* Now go through each element of PLEFT. */
1136 for (tail2 = pleft; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
1138 Lisp_Object tmp;
1140 sym = XCAR (tail2);
1142 /* Sticky properties get special treatment. */
1143 if (EQ (sym, Qrear_nonsticky) || EQ (sym, Qfront_sticky))
1144 continue;
1146 /* If sym is in PRIGHT, we've already considered it. */
1147 for (tail1 = pright; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
1148 if (EQ (sym, XCAR (tail1)))
1149 break;
1150 if (! NILP (tail1))
1151 continue;
1153 lval = Fcar (XCDR (tail2));
1155 /* Even if lrear or rfront say nothing about the stickiness of
1156 SYM, Vtext_property_default_nonsticky may give default
1157 stickiness to SYM. */
1158 tmp = Fassq (sym, Vtext_property_default_nonsticky);
1160 /* Since rval is known to be nil in this loop, the test simplifies. */
1161 if (! (TMEM (sym, lrear) || (CONSP (tmp) && ! NILP (XCDR (tmp)))))
1163 props = Fcons (lval, Fcons (sym, props));
1164 if (TMEM (sym, lfront))
1165 front = Fcons (sym, front);
1167 else if (TMEM (sym, rfront) || (CONSP (tmp) && NILP (XCDR (tmp))))
1169 /* The value is nil, but we still inherit the stickiness
1170 from the right. */
1171 front = Fcons (sym, front);
1172 if (TMEM (sym, rrear))
1173 rear = Fcons (sym, rear);
1176 props = Fnreverse (props);
1177 if (! NILP (rear))
1178 props = Fcons (Qrear_nonsticky, Fcons (Fnreverse (rear), props));
1180 cat = textget (props, Qcategory);
1181 if (! NILP (front)
1183 /* If we have inherited a front-stick category property that is t,
1184 we don't need to set up a detailed one. */
1185 ! (! NILP (cat) && SYMBOLP (cat)
1186 && EQ (Fget (cat, Qfront_sticky), Qt)))
1187 props = Fcons (Qfront_sticky, Fcons (Fnreverse (front), props));
1188 return props;
1192 /* Delete a node I from its interval tree by merging its subtrees
1193 into one subtree which is then returned. Caller is responsible for
1194 storing the resulting subtree into its parent. */
1196 static INTERVAL
1197 delete_node (register INTERVAL i)
1199 register INTERVAL migrate, this;
1200 register ptrdiff_t migrate_amt;
1202 if (!i->left)
1203 return i->right;
1204 if (!i->right)
1205 return i->left;
1207 migrate = i->left;
1208 migrate_amt = i->left->total_length;
1209 this = i->right;
1210 this->total_length += migrate_amt;
1211 while (this->left)
1213 this = this->left;
1214 this->total_length += migrate_amt;
1216 set_interval_left (this, migrate);
1217 set_interval_parent (migrate, this);
1218 eassert (LENGTH (this) > 0);
1219 eassert (LENGTH (i->right) > 0);
1221 return i->right;
1224 /* Delete interval I from its tree by calling `delete_node'
1225 and properly connecting the resultant subtree.
1227 I is presumed to be empty; that is, no adjustments are made
1228 for the length of I. */
1230 static void
1231 delete_interval (register INTERVAL i)
1233 register INTERVAL parent;
1234 ptrdiff_t amt = LENGTH (i);
1236 eassert (amt == 0); /* Only used on zero-length intervals now. */
1238 if (ROOT_INTERVAL_P (i))
1240 Lisp_Object owner;
1241 GET_INTERVAL_OBJECT (owner, i);
1242 parent = delete_node (i);
1243 if (parent)
1244 set_interval_object (parent, owner);
1246 if (BUFFERP (owner))
1247 set_buffer_intervals (XBUFFER (owner), parent);
1248 else if (STRINGP (owner))
1249 set_string_intervals (owner, parent);
1250 else
1251 emacs_abort ();
1253 return;
1256 parent = INTERVAL_PARENT (i);
1257 if (AM_LEFT_CHILD (i))
1259 set_interval_left (parent, delete_node (i));
1260 if (parent->left)
1261 set_interval_parent (parent->left, parent);
1263 else
1265 set_interval_right (parent, delete_node (i));
1266 if (parent->right)
1267 set_interval_parent (parent->right, parent);
1271 /* Find the interval in TREE corresponding to the relative position
1272 FROM and delete as much as possible of AMOUNT from that interval.
1273 Return the amount actually deleted, and if the interval was
1274 zeroed-out, delete that interval node from the tree.
1276 Note that FROM is actually origin zero, aka relative to the
1277 leftmost edge of tree. This is appropriate since we call ourselves
1278 recursively on subtrees.
1280 Do this by recursing down TREE to the interval in question, and
1281 deleting the appropriate amount of text. */
1283 static ptrdiff_t
1284 interval_deletion_adjustment (register INTERVAL tree, register ptrdiff_t from,
1285 register ptrdiff_t amount)
1287 register ptrdiff_t relative_position = from;
1289 if (!tree)
1290 return 0;
1292 /* Left branch. */
1293 if (relative_position < LEFT_TOTAL_LENGTH (tree))
1295 ptrdiff_t subtract = interval_deletion_adjustment (tree->left,
1296 relative_position,
1297 amount);
1298 tree->total_length -= subtract;
1299 eassert (LENGTH (tree) > 0);
1300 return subtract;
1302 /* Right branch. */
1303 else if (relative_position >= (TOTAL_LENGTH (tree)
1304 - RIGHT_TOTAL_LENGTH (tree)))
1306 ptrdiff_t subtract;
1308 relative_position -= (tree->total_length
1309 - RIGHT_TOTAL_LENGTH (tree));
1310 subtract = interval_deletion_adjustment (tree->right,
1311 relative_position,
1312 amount);
1313 tree->total_length -= subtract;
1314 eassert (LENGTH (tree) > 0);
1315 return subtract;
1317 /* Here -- this node. */
1318 else
1320 /* How much can we delete from this interval? */
1321 ptrdiff_t my_amount = ((tree->total_length
1322 - RIGHT_TOTAL_LENGTH (tree))
1323 - relative_position);
1325 if (amount > my_amount)
1326 amount = my_amount;
1328 tree->total_length -= amount;
1329 eassert (LENGTH (tree) >= 0);
1330 if (LENGTH (tree) == 0)
1331 delete_interval (tree);
1333 return amount;
1336 /* Never reach here. */
1339 /* Effect the adjustments necessary to the interval tree of BUFFER to
1340 correspond to the deletion of LENGTH characters from that buffer
1341 text. The deletion is effected at position START (which is a
1342 buffer position, i.e. origin 1). */
1344 static void
1345 adjust_intervals_for_deletion (struct buffer *buffer,
1346 ptrdiff_t start, ptrdiff_t length)
1348 ptrdiff_t left_to_delete = length;
1349 INTERVAL tree = buffer_intervals (buffer);
1350 Lisp_Object parent;
1351 ptrdiff_t offset;
1353 GET_INTERVAL_OBJECT (parent, tree);
1354 offset = (BUFFERP (parent) ? BUF_BEG (XBUFFER (parent)) : 0);
1356 if (!tree)
1357 return;
1359 eassert (start <= offset + TOTAL_LENGTH (tree)
1360 && start + length <= offset + TOTAL_LENGTH (tree));
1362 if (length == TOTAL_LENGTH (tree))
1364 set_buffer_intervals (buffer, NULL);
1365 return;
1368 if (ONLY_INTERVAL_P (tree))
1370 tree->total_length -= length;
1371 eassert (LENGTH (tree) > 0);
1372 return;
1375 if (start > offset + TOTAL_LENGTH (tree))
1376 start = offset + TOTAL_LENGTH (tree);
1377 while (left_to_delete > 0)
1379 left_to_delete -= interval_deletion_adjustment (tree, start - offset,
1380 left_to_delete);
1381 tree = buffer_intervals (buffer);
1382 if (left_to_delete == tree->total_length)
1384 set_buffer_intervals (buffer, NULL);
1385 return;
1390 /* Make the adjustments necessary to the interval tree of BUFFER to
1391 represent an addition or deletion of LENGTH characters starting
1392 at position START. Addition or deletion is indicated by the sign
1393 of LENGTH. */
1395 void
1396 offset_intervals (struct buffer *buffer, ptrdiff_t start, ptrdiff_t length)
1398 if (!buffer_intervals (buffer) || length == 0)
1399 return;
1401 if (length > 0)
1402 adjust_intervals_for_insertion (buffer_intervals (buffer),
1403 start, length);
1404 else
1405 adjust_intervals_for_deletion (buffer, start, -length);
1408 /* Merge interval I with its lexicographic successor. The resulting
1409 interval is returned, and has the properties of the original
1410 successor. The properties of I are lost. I is removed from the
1411 interval tree.
1413 IMPORTANT:
1414 The caller must verify that this is not the last (rightmost)
1415 interval. */
1417 static INTERVAL
1418 merge_interval_right (register INTERVAL i)
1420 register ptrdiff_t absorb = LENGTH (i);
1421 register INTERVAL successor;
1423 /* Find the succeeding interval. */
1424 if (! NULL_RIGHT_CHILD (i)) /* It's below us. Add absorb
1425 as we descend. */
1427 successor = i->right;
1428 while (! NULL_LEFT_CHILD (successor))
1430 successor->total_length += absorb;
1431 eassert (LENGTH (successor) > 0);
1432 successor = successor->left;
1435 successor->total_length += absorb;
1436 eassert (LENGTH (successor) > 0);
1437 delete_interval (i);
1438 return successor;
1441 /* Zero out this interval. */
1442 i->total_length -= absorb;
1443 eassert (TOTAL_LENGTH (i) >= 0);
1445 successor = i;
1446 while (! NULL_PARENT (successor)) /* It's above us. Subtract as
1447 we ascend. */
1449 if (AM_LEFT_CHILD (successor))
1451 successor = INTERVAL_PARENT (successor);
1452 delete_interval (i);
1453 return successor;
1456 successor = INTERVAL_PARENT (successor);
1457 successor->total_length -= absorb;
1458 eassert (LENGTH (successor) > 0);
1461 /* This must be the rightmost or last interval and cannot
1462 be merged right. The caller should have known. */
1463 emacs_abort ();
1466 /* Merge interval I with its lexicographic predecessor. The resulting
1467 interval is returned, and has the properties of the original predecessor.
1468 The properties of I are lost. Interval node I is removed from the tree.
1470 IMPORTANT:
1471 The caller must verify that this is not the first (leftmost) interval. */
1473 INTERVAL
1474 merge_interval_left (register INTERVAL i)
1476 register ptrdiff_t absorb = LENGTH (i);
1477 register INTERVAL predecessor;
1479 /* Find the preceding interval. */
1480 if (! NULL_LEFT_CHILD (i)) /* It's below us. Go down,
1481 adding ABSORB as we go. */
1483 predecessor = i->left;
1484 while (! NULL_RIGHT_CHILD (predecessor))
1486 predecessor->total_length += absorb;
1487 eassert (LENGTH (predecessor) > 0);
1488 predecessor = predecessor->right;
1491 predecessor->total_length += absorb;
1492 eassert (LENGTH (predecessor) > 0);
1493 delete_interval (i);
1494 return predecessor;
1497 /* Zero out this interval. */
1498 i->total_length -= absorb;
1499 eassert (TOTAL_LENGTH (i) >= 0);
1501 predecessor = i;
1502 while (! NULL_PARENT (predecessor)) /* It's above us. Go up,
1503 subtracting ABSORB. */
1505 if (AM_RIGHT_CHILD (predecessor))
1507 predecessor = INTERVAL_PARENT (predecessor);
1508 delete_interval (i);
1509 return predecessor;
1512 predecessor = INTERVAL_PARENT (predecessor);
1513 predecessor->total_length -= absorb;
1514 eassert (LENGTH (predecessor) > 0);
1517 /* This must be the leftmost or first interval and cannot
1518 be merged left. The caller should have known. */
1519 emacs_abort ();
1522 /* Create a copy of SOURCE but with the default value of UP. */
1524 static INTERVAL
1525 reproduce_interval (INTERVAL source)
1527 register INTERVAL target = make_interval ();
1529 eassert (LENGTH (source) > 0);
1531 target->total_length = source->total_length;
1532 target->position = source->position;
1534 copy_properties (source, target);
1536 if (! NULL_LEFT_CHILD (source))
1537 set_interval_left (target, reproduce_tree (source->left, target));
1538 if (! NULL_RIGHT_CHILD (source))
1539 set_interval_right (target, reproduce_tree (source->right, target));
1541 eassert (LENGTH (target) > 0);
1542 return target;
1545 /* Make an exact copy of interval tree SOURCE which descends from
1546 PARENT. This is done by recursing through SOURCE, copying
1547 the current interval and its properties, and then adjusting
1548 the pointers of the copy. */
1550 static INTERVAL
1551 reproduce_tree (INTERVAL source, INTERVAL parent)
1553 INTERVAL target = reproduce_interval (source);
1554 set_interval_parent (target, parent);
1555 return target;
1558 static INTERVAL
1559 reproduce_tree_obj (INTERVAL source, Lisp_Object parent)
1561 INTERVAL target = reproduce_interval (source);
1562 set_interval_object (target, parent);
1563 return target;
1566 /* Insert the intervals of SOURCE into BUFFER at POSITION.
1567 LENGTH is the length of the text in SOURCE.
1569 The `position' field of the SOURCE intervals is assumed to be
1570 consistent with its parent; therefore, SOURCE must be an
1571 interval tree made with copy_interval or must be the whole
1572 tree of a buffer or a string.
1574 This is used in insdel.c when inserting Lisp_Strings into the
1575 buffer. The text corresponding to SOURCE is already in the buffer
1576 when this is called. The intervals of new tree are a copy of those
1577 belonging to the string being inserted; intervals are never
1578 shared.
1580 If the inserted text had no intervals associated, and we don't
1581 want to inherit the surrounding text's properties, this function
1582 simply returns -- offset_intervals should handle placing the
1583 text in the correct interval, depending on the sticky bits.
1585 If the inserted text had properties (intervals), then there are two
1586 cases -- either insertion happened in the middle of some interval,
1587 or between two intervals.
1589 If the text goes into the middle of an interval, then new intervals
1590 are created in the middle, and new text has the union of its properties
1591 and those of the text into which it was inserted.
1593 If the text goes between two intervals, then if neither interval
1594 had its appropriate sticky property set (front_sticky, rear_sticky),
1595 the new text has only its properties. If one of the sticky properties
1596 is set, then the new text "sticks" to that region and its properties
1597 depend on merging as above. If both the preceding and succeeding
1598 intervals to the new text are "sticky", then the new text retains
1599 only its properties, as if neither sticky property were set. Perhaps
1600 we should consider merging all three sets of properties onto the new
1601 text... */
1603 void
1604 graft_intervals_into_buffer (INTERVAL source, ptrdiff_t position,
1605 ptrdiff_t length, struct buffer *buffer,
1606 bool inherit)
1608 INTERVAL tree = buffer_intervals (buffer);
1609 INTERVAL under, over, this;
1610 ptrdiff_t over_used;
1612 /* If the new text has no properties, then with inheritance it
1613 becomes part of whatever interval it was inserted into.
1614 To prevent inheritance, we must clear out the properties
1615 of the newly inserted text. */
1616 if (!source)
1618 Lisp_Object buf;
1619 if (!inherit && tree && length > 0)
1621 XSETBUFFER (buf, buffer);
1622 set_text_properties_1 (make_number (position),
1623 make_number (position + length),
1624 Qnil, buf,
1625 find_interval (tree, position));
1627 /* Shouldn't be necessary. --Stef */
1628 buffer_balance_intervals (buffer);
1629 return;
1632 eassert (length == TOTAL_LENGTH (source));
1634 if ((BUF_Z (buffer) - BUF_BEG (buffer)) == length)
1636 /* The inserted text constitutes the whole buffer, so
1637 simply copy over the interval structure. */
1638 Lisp_Object buf;
1640 XSETBUFFER (buf, buffer);
1641 set_buffer_intervals (buffer, reproduce_tree_obj (source, buf));
1642 buffer_intervals (buffer)->position = BUF_BEG (buffer);
1643 eassert (buffer_intervals (buffer)->up_obj == 1);
1644 return;
1646 else if (!tree)
1648 /* Create an interval tree in which to place a copy
1649 of the intervals of the inserted string. */
1650 Lisp_Object buf;
1652 XSETBUFFER (buf, buffer);
1653 tree = create_root_interval (buf);
1655 /* Paranoia -- the text has already been added, so
1656 this buffer should be of non-zero length. */
1657 eassert (TOTAL_LENGTH (tree) > 0);
1659 this = under = find_interval (tree, position);
1660 eassert (under);
1661 over = find_interval (source, interval_start_pos (source));
1663 /* Here for insertion in the middle of an interval.
1664 Split off an equivalent interval to the right,
1665 then don't bother with it any more. */
1667 if (position > under->position)
1669 INTERVAL end_unchanged
1670 = split_interval_left (this, position - under->position);
1671 copy_properties (under, end_unchanged);
1672 under->position = position;
1674 else
1676 /* This call may have some effect because previous_interval may
1677 update `position' fields of intervals. Thus, don't ignore it
1678 for the moment. Someone please tell me the truth (K.Handa). */
1679 INTERVAL prev = previous_interval (under);
1680 (void) prev;
1681 #if 0
1682 /* But, this code surely has no effect. And, anyway,
1683 END_NONSTICKY_P is unreliable now. */
1684 if (prev && !END_NONSTICKY_P (prev))
1685 prev = 0;
1686 #endif /* 0 */
1689 /* Insertion is now at beginning of UNDER. */
1691 /* The inserted text "sticks" to the interval `under',
1692 which means it gets those properties.
1693 The properties of under are the result of
1694 adjust_intervals_for_insertion, so stickiness has
1695 already been taken care of. */
1697 /* OVER is the interval we are copying from next.
1698 OVER_USED says how many characters' worth of OVER
1699 have already been copied into target intervals.
1700 UNDER is the next interval in the target. */
1701 over_used = 0;
1702 while (over)
1704 /* If UNDER is longer than OVER, split it. */
1705 if (LENGTH (over) - over_used < LENGTH (under))
1707 this = split_interval_left (under, LENGTH (over) - over_used);
1708 copy_properties (under, this);
1710 else
1711 this = under;
1713 /* THIS is now the interval to copy or merge into.
1714 OVER covers all of it. */
1715 if (inherit)
1716 merge_properties (over, this);
1717 else
1718 copy_properties (over, this);
1720 /* If THIS and OVER end at the same place,
1721 advance OVER to a new source interval. */
1722 if (LENGTH (this) == LENGTH (over) - over_used)
1724 over = next_interval (over);
1725 over_used = 0;
1727 else
1728 /* Otherwise just record that more of OVER has been used. */
1729 over_used += LENGTH (this);
1731 /* Always advance to a new target interval. */
1732 under = next_interval (this);
1735 buffer_balance_intervals (buffer);
1738 /* Get the value of property PROP from PLIST,
1739 which is the plist of an interval.
1740 We check for direct properties, for categories with property PROP,
1741 and for PROP appearing on the default-text-properties list. */
1743 Lisp_Object
1744 textget (Lisp_Object plist, register Lisp_Object prop)
1746 return lookup_char_property (plist, prop, 1);
1749 Lisp_Object
1750 lookup_char_property (Lisp_Object plist, Lisp_Object prop, bool textprop)
1752 Lisp_Object tail, fallback = Qnil;
1754 for (tail = plist; CONSP (tail); tail = Fcdr (XCDR (tail)))
1756 register Lisp_Object tem;
1757 tem = XCAR (tail);
1758 if (EQ (prop, tem))
1759 return Fcar (XCDR (tail));
1760 if (EQ (tem, Qcategory))
1762 tem = Fcar (XCDR (tail));
1763 if (SYMBOLP (tem))
1764 fallback = Fget (tem, prop);
1768 if (! NILP (fallback))
1769 return fallback;
1770 /* Check for alternative properties. */
1771 tail = Fassq (prop, Vchar_property_alias_alist);
1772 if (! NILP (tail))
1774 tail = XCDR (tail);
1775 for (; NILP (fallback) && CONSP (tail); tail = XCDR (tail))
1776 fallback = Fplist_get (plist, XCAR (tail));
1779 if (textprop && NILP (fallback) && CONSP (Vdefault_text_properties))
1780 fallback = Fplist_get (Vdefault_text_properties, prop);
1781 return fallback;
1785 /* Set point in BUFFER "temporarily" to CHARPOS, which corresponds to
1786 byte position BYTEPOS. */
1788 void
1789 temp_set_point_both (struct buffer *buffer,
1790 ptrdiff_t charpos, ptrdiff_t bytepos)
1792 /* In a single-byte buffer, the two positions must be equal. */
1793 eassert (BUF_ZV (buffer) != BUF_ZV_BYTE (buffer) || charpos == bytepos);
1795 eassert (charpos <= bytepos);
1796 eassert (charpos <= BUF_ZV (buffer) || BUF_BEGV (buffer) <= charpos);
1798 SET_BUF_PT_BOTH (buffer, charpos, bytepos);
1801 /* Set point "temporarily", without checking any text properties. */
1803 void
1804 temp_set_point (struct buffer *buffer, ptrdiff_t charpos)
1806 temp_set_point_both (buffer, charpos,
1807 buf_charpos_to_bytepos (buffer, charpos));
1810 /* Set point in BUFFER to CHARPOS. If the target position is
1811 before an intangible character, move to an ok place. */
1813 void
1814 set_point (ptrdiff_t charpos)
1816 set_point_both (charpos, buf_charpos_to_bytepos (current_buffer, charpos));
1819 /* Set PT from MARKER's clipped position. */
1821 void
1822 set_point_from_marker (Lisp_Object marker)
1824 ptrdiff_t charpos = clip_to_bounds (BEGV, marker_position (marker), ZV);
1825 ptrdiff_t bytepos = marker_byte_position (marker);
1827 /* Don't trust the byte position if the marker belongs to a
1828 different buffer. */
1829 if (XMARKER (marker)->buffer != current_buffer)
1830 bytepos = buf_charpos_to_bytepos (current_buffer, charpos);
1831 else
1832 bytepos = clip_to_bounds (BEGV_BYTE, bytepos, ZV_BYTE);
1833 set_point_both (charpos, bytepos);
1836 /* If there's an invisible character at position POS + TEST_OFFS in the
1837 current buffer, and the invisible property has a `stickiness' such that
1838 inserting a character at position POS would inherit the property it,
1839 return POS + ADJ, otherwise return POS. If TEST_INTANG, intangibility
1840 is required as well as invisibility.
1842 TEST_OFFS should be either 0 or -1, and ADJ should be either 1 or -1.
1844 Note that `stickiness' is determined by overlay marker insertion types,
1845 if the invisible property comes from an overlay. */
1847 static ptrdiff_t
1848 adjust_for_invis_intang (ptrdiff_t pos, ptrdiff_t test_offs, ptrdiff_t adj,
1849 bool test_intang)
1851 Lisp_Object invis_propval, invis_overlay;
1852 Lisp_Object test_pos;
1854 if ((adj < 0 && pos + adj < BEGV) || (adj > 0 && pos + adj > ZV))
1855 /* POS + ADJ would be beyond the buffer bounds, so do no adjustment. */
1856 return pos;
1858 test_pos = make_number (pos + test_offs);
1860 invis_propval
1861 = get_char_property_and_overlay (test_pos, Qinvisible, Qnil,
1862 &invis_overlay);
1864 if ((!test_intang
1865 || ! NILP (Fget_char_property (test_pos, Qintangible, Qnil)))
1866 && TEXT_PROP_MEANS_INVISIBLE (invis_propval)
1867 /* This next test is true if the invisible property has a stickiness
1868 such that an insertion at POS would inherit it. */
1869 && (NILP (invis_overlay)
1870 /* Invisible property is from a text-property. */
1871 ? (text_property_stickiness (Qinvisible, make_number (pos), Qnil)
1872 == (test_offs == 0 ? 1 : -1))
1873 /* Invisible property is from an overlay. */
1874 : (test_offs == 0
1875 ? XMARKER (OVERLAY_START (invis_overlay))->insertion_type == 0
1876 : XMARKER (OVERLAY_END (invis_overlay))->insertion_type == 1)))
1877 pos += adj;
1879 return pos;
1882 /* Set point in BUFFER to CHARPOS, which corresponds to byte
1883 position BYTEPOS. If the target position is
1884 before an intangible character, move to an ok place. */
1886 void
1887 set_point_both (ptrdiff_t charpos, ptrdiff_t bytepos)
1889 register INTERVAL to, from, toprev, fromprev;
1890 ptrdiff_t buffer_point;
1891 ptrdiff_t old_position = PT;
1892 /* This ensures that we move forward past intangible text when the
1893 initial position is the same as the destination, in the rare
1894 instances where this is important, e.g. in line-move-finish
1895 (simple.el). */
1896 bool backwards = charpos < old_position;
1897 bool have_overlays;
1898 ptrdiff_t original_position;
1900 bset_point_before_scroll (current_buffer, Qnil);
1902 if (charpos == PT)
1903 return;
1905 /* In a single-byte buffer, the two positions must be equal. */
1906 eassert (ZV != ZV_BYTE || charpos == bytepos);
1908 /* Check this now, before checking if the buffer has any intervals.
1909 That way, we can catch conditions which break this sanity check
1910 whether or not there are intervals in the buffer. */
1911 eassert (charpos <= ZV && charpos >= BEGV);
1913 have_overlays = buffer_has_overlays ();
1915 /* If we have no text properties and overlays,
1916 then we can do it quickly. */
1917 if (!buffer_intervals (current_buffer) && ! have_overlays)
1919 temp_set_point_both (current_buffer, charpos, bytepos);
1920 return;
1923 /* Set TO to the interval containing the char after CHARPOS,
1924 and TOPREV to the interval containing the char before CHARPOS.
1925 Either one may be null. They may be equal. */
1926 to = find_interval (buffer_intervals (current_buffer), charpos);
1927 if (charpos == BEGV)
1928 toprev = 0;
1929 else if (to && to->position == charpos)
1930 toprev = previous_interval (to);
1931 else
1932 toprev = to;
1934 buffer_point = (PT == ZV ? ZV - 1 : PT);
1936 /* Set FROM to the interval containing the char after PT,
1937 and FROMPREV to the interval containing the char before PT.
1938 Either one may be null. They may be equal. */
1939 /* We could cache this and save time. */
1940 from = find_interval (buffer_intervals (current_buffer), buffer_point);
1941 if (buffer_point == BEGV)
1942 fromprev = 0;
1943 else if (from && from->position == PT)
1944 fromprev = previous_interval (from);
1945 else if (buffer_point != PT)
1946 fromprev = from, from = 0;
1947 else
1948 fromprev = from;
1950 /* Moving within an interval. */
1951 if (to == from && toprev == fromprev && INTERVAL_VISIBLE_P (to)
1952 && ! have_overlays)
1954 temp_set_point_both (current_buffer, charpos, bytepos);
1955 return;
1958 original_position = charpos;
1960 /* If the new position is between two intangible characters
1961 with the same intangible property value,
1962 move forward or backward until a change in that property. */
1963 if (NILP (Vinhibit_point_motion_hooks)
1964 && ((to && toprev)
1965 || have_overlays)
1966 /* Intangibility never stops us from positioning at the beginning
1967 or end of the buffer, so don't bother checking in that case. */
1968 && charpos != BEGV && charpos != ZV)
1970 Lisp_Object pos;
1971 Lisp_Object intangible_propval;
1973 if (backwards)
1975 /* If the preceding character is both intangible and invisible,
1976 and the invisible property is `rear-sticky', perturb it so
1977 that the search starts one character earlier -- this ensures
1978 that point can never move to the end of an invisible/
1979 intangible/rear-sticky region. */
1980 charpos = adjust_for_invis_intang (charpos, -1, -1, 1);
1982 XSETINT (pos, charpos);
1984 /* If following char is intangible,
1985 skip back over all chars with matching intangible property. */
1987 intangible_propval = Fget_char_property (pos, Qintangible, Qnil);
1989 if (! NILP (intangible_propval))
1991 while (XINT (pos) > BEGV
1992 && EQ (Fget_char_property (make_number (XINT (pos) - 1),
1993 Qintangible, Qnil),
1994 intangible_propval))
1995 pos = Fprevious_char_property_change (pos, Qnil);
1997 /* Set CHARPOS from POS, and if the final intangible character
1998 that we skipped over is also invisible, and the invisible
1999 property is `front-sticky', perturb it to be one character
2000 earlier -- this ensures that point can never move to the
2001 beginning of an invisible/intangible/front-sticky region. */
2002 charpos = adjust_for_invis_intang (XINT (pos), 0, -1, 0);
2005 else
2007 /* If the following character is both intangible and invisible,
2008 and the invisible property is `front-sticky', perturb it so
2009 that the search starts one character later -- this ensures
2010 that point can never move to the beginning of an
2011 invisible/intangible/front-sticky region. */
2012 charpos = adjust_for_invis_intang (charpos, 0, 1, 1);
2014 XSETINT (pos, charpos);
2016 /* If preceding char is intangible,
2017 skip forward over all chars with matching intangible property. */
2019 intangible_propval = Fget_char_property (make_number (charpos - 1),
2020 Qintangible, Qnil);
2022 if (! NILP (intangible_propval))
2024 while (XINT (pos) < ZV
2025 && EQ (Fget_char_property (pos, Qintangible, Qnil),
2026 intangible_propval))
2027 pos = Fnext_char_property_change (pos, Qnil);
2029 /* Set CHARPOS from POS, and if the final intangible character
2030 that we skipped over is also invisible, and the invisible
2031 property is `rear-sticky', perturb it to be one character
2032 later -- this ensures that point can never move to the
2033 end of an invisible/intangible/rear-sticky region. */
2034 charpos = adjust_for_invis_intang (XINT (pos), -1, 1, 0);
2038 bytepos = buf_charpos_to_bytepos (current_buffer, charpos);
2041 if (charpos != original_position)
2043 /* Set TO to the interval containing the char after CHARPOS,
2044 and TOPREV to the interval containing the char before CHARPOS.
2045 Either one may be null. They may be equal. */
2046 to = find_interval (buffer_intervals (current_buffer), charpos);
2047 if (charpos == BEGV)
2048 toprev = 0;
2049 else if (to && to->position == charpos)
2050 toprev = previous_interval (to);
2051 else
2052 toprev = to;
2055 /* Here TO is the interval after the stopping point
2056 and TOPREV is the interval before the stopping point.
2057 One or the other may be null. */
2059 temp_set_point_both (current_buffer, charpos, bytepos);
2061 /* We run point-left and point-entered hooks here, if the
2062 two intervals are not equivalent. These hooks take
2063 (old_point, new_point) as arguments. */
2064 if (NILP (Vinhibit_point_motion_hooks)
2065 && (! intervals_equal (from, to)
2066 || ! intervals_equal (fromprev, toprev)))
2068 Lisp_Object leave_after, leave_before, enter_after, enter_before;
2070 if (fromprev)
2071 leave_before = textget (fromprev->plist, Qpoint_left);
2072 else
2073 leave_before = Qnil;
2075 if (from)
2076 leave_after = textget (from->plist, Qpoint_left);
2077 else
2078 leave_after = Qnil;
2080 if (toprev)
2081 enter_before = textget (toprev->plist, Qpoint_entered);
2082 else
2083 enter_before = Qnil;
2085 if (to)
2086 enter_after = textget (to->plist, Qpoint_entered);
2087 else
2088 enter_after = Qnil;
2090 if (! EQ (leave_before, enter_before) && !NILP (leave_before))
2091 call2 (leave_before, make_number (old_position),
2092 make_number (charpos));
2093 if (! EQ (leave_after, enter_after) && !NILP (leave_after))
2094 call2 (leave_after, make_number (old_position),
2095 make_number (charpos));
2097 if (! EQ (enter_before, leave_before) && !NILP (enter_before))
2098 call2 (enter_before, make_number (old_position),
2099 make_number (charpos));
2100 if (! EQ (enter_after, leave_after) && !NILP (enter_after))
2101 call2 (enter_after, make_number (old_position),
2102 make_number (charpos));
2106 /* Move point to POSITION, unless POSITION is inside an intangible
2107 segment that reaches all the way to point. */
2109 void
2110 move_if_not_intangible (ptrdiff_t position)
2112 Lisp_Object pos;
2113 Lisp_Object intangible_propval;
2115 XSETINT (pos, position);
2117 if (! NILP (Vinhibit_point_motion_hooks))
2118 /* If intangible is inhibited, always move point to POSITION. */
2120 else if (PT < position && XINT (pos) < ZV)
2122 /* We want to move forward, so check the text before POSITION. */
2124 intangible_propval = Fget_char_property (pos,
2125 Qintangible, Qnil);
2127 /* If following char is intangible,
2128 skip back over all chars with matching intangible property. */
2129 if (! NILP (intangible_propval))
2130 while (XINT (pos) > BEGV
2131 && EQ (Fget_char_property (make_number (XINT (pos) - 1),
2132 Qintangible, Qnil),
2133 intangible_propval))
2134 pos = Fprevious_char_property_change (pos, Qnil);
2136 else if (XINT (pos) > BEGV)
2138 /* We want to move backward, so check the text after POSITION. */
2140 intangible_propval = Fget_char_property (make_number (XINT (pos) - 1),
2141 Qintangible, Qnil);
2143 /* If following char is intangible,
2144 skip forward over all chars with matching intangible property. */
2145 if (! NILP (intangible_propval))
2146 while (XINT (pos) < ZV
2147 && EQ (Fget_char_property (pos, Qintangible, Qnil),
2148 intangible_propval))
2149 pos = Fnext_char_property_change (pos, Qnil);
2152 else if (position < BEGV)
2153 position = BEGV;
2154 else if (position > ZV)
2155 position = ZV;
2157 /* If the whole stretch between PT and POSITION isn't intangible,
2158 try moving to POSITION (which means we actually move farther
2159 if POSITION is inside of intangible text). */
2161 if (XINT (pos) != PT)
2162 SET_PT (position);
2165 /* If text at position POS has property PROP, set *VAL to the property
2166 value, *START and *END to the beginning and end of a region that
2167 has the same property, and return true. Otherwise return false.
2169 OBJECT is the string or buffer to look for the property in;
2170 nil means the current buffer. */
2172 bool
2173 get_property_and_range (ptrdiff_t pos, Lisp_Object prop, Lisp_Object *val,
2174 ptrdiff_t *start, ptrdiff_t *end, Lisp_Object object)
2176 INTERVAL i, prev, next;
2178 if (NILP (object))
2179 i = find_interval (buffer_intervals (current_buffer), pos);
2180 else if (BUFFERP (object))
2181 i = find_interval (buffer_intervals (XBUFFER (object)), pos);
2182 else if (STRINGP (object))
2183 i = find_interval (string_intervals (object), pos);
2184 else
2185 emacs_abort ();
2187 if (!i || (i->position + LENGTH (i) <= pos))
2188 return 0;
2189 *val = textget (i->plist, prop);
2190 if (NILP (*val))
2191 return 0;
2193 next = i; /* remember it in advance */
2194 prev = previous_interval (i);
2195 while (prev
2196 && EQ (*val, textget (prev->plist, prop)))
2197 i = prev, prev = previous_interval (prev);
2198 *start = i->position;
2200 next = next_interval (i);
2201 while (next && EQ (*val, textget (next->plist, prop)))
2202 i = next, next = next_interval (next);
2203 *end = i->position + LENGTH (i);
2205 return 1;
2208 /* Return the proper local keymap TYPE for position POSITION in
2209 BUFFER; TYPE should be one of `keymap' or `local-map'. Use the map
2210 specified by the PROP property, if any. Otherwise, if TYPE is
2211 `local-map' use BUFFER's local map. */
2213 Lisp_Object
2214 get_local_map (ptrdiff_t position, struct buffer *buffer, Lisp_Object type)
2216 Lisp_Object prop, lispy_position, lispy_buffer;
2217 ptrdiff_t old_begv, old_zv, old_begv_byte, old_zv_byte;
2219 position = clip_to_bounds (BUF_BEGV (buffer), position, BUF_ZV (buffer));
2221 /* Ignore narrowing, so that a local map continues to be valid even if
2222 the visible region contains no characters and hence no properties. */
2223 old_begv = BUF_BEGV (buffer);
2224 old_zv = BUF_ZV (buffer);
2225 old_begv_byte = BUF_BEGV_BYTE (buffer);
2226 old_zv_byte = BUF_ZV_BYTE (buffer);
2228 SET_BUF_BEGV_BOTH (buffer, BUF_BEG (buffer), BUF_BEG_BYTE (buffer));
2229 SET_BUF_ZV_BOTH (buffer, BUF_Z (buffer), BUF_Z_BYTE (buffer));
2231 XSETFASTINT (lispy_position, position);
2232 XSETBUFFER (lispy_buffer, buffer);
2233 /* First check if the CHAR has any property. This is because when
2234 we click with the mouse, the mouse pointer is really pointing
2235 to the CHAR after POS. */
2236 prop = Fget_char_property (lispy_position, type, lispy_buffer);
2237 /* If not, look at the POS's properties. This is necessary because when
2238 editing a field with a `local-map' property, we want insertion at the end
2239 to obey the `local-map' property. */
2240 if (NILP (prop))
2241 prop = Fget_pos_property (lispy_position, type, lispy_buffer);
2243 SET_BUF_BEGV_BOTH (buffer, old_begv, old_begv_byte);
2244 SET_BUF_ZV_BOTH (buffer, old_zv, old_zv_byte);
2246 /* Use the local map only if it is valid. */
2247 prop = get_keymap (prop, 0, 0);
2248 if (CONSP (prop))
2249 return prop;
2251 if (EQ (type, Qkeymap))
2252 return Qnil;
2253 else
2254 return BVAR (buffer, keymap);
2257 /* Produce an interval tree reflecting the intervals in
2258 TREE from START to START + LENGTH.
2259 The new interval tree has no parent and has a starting-position of 0. */
2261 INTERVAL
2262 copy_intervals (INTERVAL tree, ptrdiff_t start, ptrdiff_t length)
2264 register INTERVAL i, new, t;
2265 register ptrdiff_t got, prevlen;
2267 if (!tree || length <= 0)
2268 return NULL;
2270 i = find_interval (tree, start);
2271 eassert (i && LENGTH (i) > 0);
2273 /* If there is only one interval and it's the default, return nil. */
2274 if ((start - i->position + 1 + length) < LENGTH (i)
2275 && DEFAULT_INTERVAL_P (i))
2276 return NULL;
2278 new = make_interval ();
2279 new->position = 0;
2280 got = (LENGTH (i) - (start - i->position));
2281 new->total_length = length;
2282 eassert (TOTAL_LENGTH (new) >= 0);
2283 copy_properties (i, new);
2285 t = new;
2286 prevlen = got;
2287 while (got < length)
2289 i = next_interval (i);
2290 t = split_interval_right (t, prevlen);
2291 copy_properties (i, t);
2292 prevlen = LENGTH (i);
2293 got += prevlen;
2296 return balance_an_interval (new);
2299 /* Give STRING the properties of BUFFER from POSITION to LENGTH. */
2301 void
2302 copy_intervals_to_string (Lisp_Object string, struct buffer *buffer,
2303 ptrdiff_t position, ptrdiff_t length)
2305 INTERVAL interval_copy = copy_intervals (buffer_intervals (buffer),
2306 position, length);
2307 if (!interval_copy)
2308 return;
2310 set_interval_object (interval_copy, string);
2311 set_string_intervals (string, interval_copy);
2314 /* Return true if strings S1 and S2 have identical properties.
2315 Assume they have identical characters. */
2317 bool
2318 compare_string_intervals (Lisp_Object s1, Lisp_Object s2)
2320 INTERVAL i1, i2;
2321 ptrdiff_t pos = 0;
2322 ptrdiff_t end = SCHARS (s1);
2324 i1 = find_interval (string_intervals (s1), 0);
2325 i2 = find_interval (string_intervals (s2), 0);
2327 while (pos < end)
2329 /* Determine how far we can go before we reach the end of I1 or I2. */
2330 ptrdiff_t len1 = (i1 != 0 ? INTERVAL_LAST_POS (i1) : end) - pos;
2331 ptrdiff_t len2 = (i2 != 0 ? INTERVAL_LAST_POS (i2) : end) - pos;
2332 ptrdiff_t distance = min (len1, len2);
2334 /* If we ever find a mismatch between the strings,
2335 they differ. */
2336 if (! intervals_equal (i1, i2))
2337 return 0;
2339 /* Advance POS till the end of the shorter interval,
2340 and advance one or both interval pointers for the new position. */
2341 pos += distance;
2342 if (len1 == distance)
2343 i1 = next_interval (i1);
2344 if (len2 == distance)
2345 i2 = next_interval (i2);
2347 return 1;
2350 /* Recursively adjust interval I in the current buffer
2351 for setting enable_multibyte_characters to MULTI_FLAG.
2352 The range of interval I is START ... END in characters,
2353 START_BYTE ... END_BYTE in bytes. */
2355 static void
2356 set_intervals_multibyte_1 (INTERVAL i, bool multi_flag,
2357 ptrdiff_t start, ptrdiff_t start_byte,
2358 ptrdiff_t end, ptrdiff_t end_byte)
2360 /* Fix the length of this interval. */
2361 if (multi_flag)
2362 i->total_length = end - start;
2363 else
2364 i->total_length = end_byte - start_byte;
2365 eassert (TOTAL_LENGTH (i) >= 0);
2367 if (TOTAL_LENGTH (i) == 0)
2369 delete_interval (i);
2370 return;
2373 /* Recursively fix the length of the subintervals. */
2374 if (i->left)
2376 ptrdiff_t left_end, left_end_byte;
2378 if (multi_flag)
2380 ptrdiff_t temp;
2381 left_end_byte = start_byte + LEFT_TOTAL_LENGTH (i);
2382 left_end = BYTE_TO_CHAR (left_end_byte);
2384 temp = CHAR_TO_BYTE (left_end);
2386 /* If LEFT_END_BYTE is in the middle of a character,
2387 adjust it and LEFT_END to a char boundary. */
2388 if (left_end_byte > temp)
2390 left_end_byte = temp;
2392 if (left_end_byte < temp)
2394 left_end--;
2395 left_end_byte = CHAR_TO_BYTE (left_end);
2398 else
2400 left_end = start + LEFT_TOTAL_LENGTH (i);
2401 left_end_byte = CHAR_TO_BYTE (left_end);
2404 set_intervals_multibyte_1 (i->left, multi_flag, start, start_byte,
2405 left_end, left_end_byte);
2407 if (i->right)
2409 ptrdiff_t right_start_byte, right_start;
2411 if (multi_flag)
2413 ptrdiff_t temp;
2415 right_start_byte = end_byte - RIGHT_TOTAL_LENGTH (i);
2416 right_start = BYTE_TO_CHAR (right_start_byte);
2418 /* If RIGHT_START_BYTE is in the middle of a character,
2419 adjust it and RIGHT_START to a char boundary. */
2420 temp = CHAR_TO_BYTE (right_start);
2422 if (right_start_byte < temp)
2424 right_start_byte = temp;
2426 if (right_start_byte > temp)
2428 right_start++;
2429 right_start_byte = CHAR_TO_BYTE (right_start);
2432 else
2434 right_start = end - RIGHT_TOTAL_LENGTH (i);
2435 right_start_byte = CHAR_TO_BYTE (right_start);
2438 set_intervals_multibyte_1 (i->right, multi_flag,
2439 right_start, right_start_byte,
2440 end, end_byte);
2443 /* Rounding to char boundaries can theoretically make this interval
2444 spurious. If so, delete one child, and copy its property list
2445 to this interval. */
2446 if (LEFT_TOTAL_LENGTH (i) + RIGHT_TOTAL_LENGTH (i) >= TOTAL_LENGTH (i))
2448 if ((i)->left)
2450 set_interval_plist (i, i->left->plist);
2451 (i)->left->total_length = 0;
2452 delete_interval ((i)->left);
2454 else
2456 set_interval_plist (i, i->right->plist);
2457 (i)->right->total_length = 0;
2458 delete_interval ((i)->right);
2463 /* Update the intervals of the current buffer
2464 to fit the contents as multibyte (if MULTI_FLAG)
2465 or to fit them as non-multibyte (if not MULTI_FLAG). */
2467 void
2468 set_intervals_multibyte (bool multi_flag)
2470 INTERVAL i = buffer_intervals (current_buffer);
2472 if (i)
2473 set_intervals_multibyte_1 (i, multi_flag, BEG, BEG_BYTE, Z, Z_BYTE);