Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / src / intervals.c
blob13ff9a9e3db8c2b8f9b6790aea9e6fde87a3b17d
1 /* Code for doing intervals.
2 Copyright (C) 1993-1995, 1997-1998, 2001-2014 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
10 (at 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 "character.h"
47 #include "buffer.h"
48 #include "puresize.h"
49 #include "keyboard.h"
50 #include "keymap.h"
52 /* Test for membership, allowing for t (actually any non-cons) to mean the
53 universal set. */
55 #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set))
57 static Lisp_Object merge_properties_sticky (Lisp_Object, Lisp_Object);
58 static INTERVAL merge_interval_right (INTERVAL);
59 static INTERVAL reproduce_tree (INTERVAL, INTERVAL);
61 /* Utility functions for intervals. */
63 /* Use these functions to set Lisp_Object
64 or pointer slots of struct interval. */
66 static void
67 set_interval_object (INTERVAL i, Lisp_Object obj)
69 eassert (BUFFERP (obj) || STRINGP (obj));
70 i->up_obj = 1;
71 i->up.obj = obj;
74 static void
75 set_interval_left (INTERVAL i, INTERVAL left)
77 i->left = left;
80 static void
81 set_interval_right (INTERVAL i, INTERVAL right)
83 i->right = right;
86 /* Make the parent of D be whatever the parent of S is, regardless
87 of the type. This is used when balancing an interval tree. */
89 static void
90 copy_interval_parent (INTERVAL d, INTERVAL s)
92 d->up = s->up;
93 d->up_obj = s->up_obj;
96 /* Create the root interval of some object, a buffer or string. */
98 INTERVAL
99 create_root_interval (Lisp_Object parent)
101 INTERVAL new;
103 CHECK_IMPURE (parent);
105 new = make_interval ();
107 if (BUFFERP (parent))
109 new->total_length = (BUF_Z (XBUFFER (parent))
110 - BUF_BEG (XBUFFER (parent)));
111 eassert (TOTAL_LENGTH (new) >= 0);
112 set_buffer_intervals (XBUFFER (parent), new);
113 new->position = BEG;
115 else if (STRINGP (parent))
117 new->total_length = SCHARS (parent);
118 eassert (TOTAL_LENGTH (new) >= 0);
119 set_string_intervals (parent, new);
120 new->position = 0;
123 set_interval_object (new, parent);
125 return new;
128 /* Make the interval TARGET have exactly the properties of SOURCE */
130 void
131 copy_properties (register INTERVAL source, register INTERVAL target)
133 if (DEFAULT_INTERVAL_P (source) && DEFAULT_INTERVAL_P (target))
134 return;
136 COPY_INTERVAL_CACHE (source, target);
137 set_interval_plist (target, Fcopy_sequence (source->plist));
140 /* Merge the properties of interval SOURCE into the properties
141 of interval TARGET. That is to say, each property in SOURCE
142 is added to TARGET if TARGET has no such property as yet. */
144 static void
145 merge_properties (register INTERVAL source, register INTERVAL target)
147 register Lisp_Object o, sym, val;
149 if (DEFAULT_INTERVAL_P (source) && DEFAULT_INTERVAL_P (target))
150 return;
152 MERGE_INTERVAL_CACHE (source, target);
154 o = source->plist;
155 while (CONSP (o))
157 sym = XCAR (o);
158 o = XCDR (o);
159 CHECK_CONS (o);
161 val = target->plist;
162 while (CONSP (val) && !EQ (XCAR (val), sym))
164 val = XCDR (val);
165 if (!CONSP (val))
166 break;
167 val = XCDR (val);
170 if (NILP (val))
172 val = XCAR (o);
173 set_interval_plist (target, Fcons (sym, Fcons (val, target->plist)));
175 o = XCDR (o);
179 /* Return true if the two intervals have the same properties. */
181 bool
182 intervals_equal (INTERVAL i0, INTERVAL i1)
184 Lisp_Object i0_cdr, i0_sym;
185 Lisp_Object i1_cdr, i1_val;
187 if (DEFAULT_INTERVAL_P (i0) && DEFAULT_INTERVAL_P (i1))
188 return 1;
190 if (DEFAULT_INTERVAL_P (i0) || DEFAULT_INTERVAL_P (i1))
191 return 0;
193 i0_cdr = i0->plist;
194 i1_cdr = i1->plist;
195 while (CONSP (i0_cdr) && CONSP (i1_cdr))
197 i0_sym = XCAR (i0_cdr);
198 i0_cdr = XCDR (i0_cdr);
199 if (!CONSP (i0_cdr))
200 return 0;
201 i1_val = i1->plist;
202 while (CONSP (i1_val) && !EQ (XCAR (i1_val), i0_sym))
204 i1_val = XCDR (i1_val);
205 if (!CONSP (i1_val))
206 return 0;
207 i1_val = XCDR (i1_val);
210 /* i0 has something i1 doesn't. */
211 if (EQ (i1_val, Qnil))
212 return 0;
214 /* i0 and i1 both have sym, but it has different values in each. */
215 if (!CONSP (i1_val)
216 || (i1_val = XCDR (i1_val), !CONSP (i1_val))
217 || !EQ (XCAR (i1_val), XCAR (i0_cdr)))
218 return 0;
220 i0_cdr = XCDR (i0_cdr);
222 i1_cdr = XCDR (i1_cdr);
223 if (!CONSP (i1_cdr))
224 return 0;
225 i1_cdr = XCDR (i1_cdr);
228 /* Lengths of the two plists were equal. */
229 return (NILP (i0_cdr) && NILP (i1_cdr));
233 /* Traverse an interval tree TREE, performing FUNCTION on each node.
234 No guarantee is made about the order of traversal.
235 Pass FUNCTION two args: an interval, and ARG. */
237 void
238 traverse_intervals_noorder (INTERVAL tree, void (*function) (INTERVAL, Lisp_Object), Lisp_Object arg)
240 /* Minimize stack usage. */
241 while (tree)
243 (*function) (tree, arg);
244 if (!tree->right)
245 tree = tree->left;
246 else
248 traverse_intervals_noorder (tree->left, function, arg);
249 tree = tree->right;
254 /* Traverse an interval tree TREE, performing FUNCTION on each node.
255 Pass FUNCTION two args: an interval, and ARG. */
257 void
258 traverse_intervals (INTERVAL tree, ptrdiff_t position,
259 void (*function) (INTERVAL, Lisp_Object), Lisp_Object arg)
261 while (tree)
263 traverse_intervals (tree->left, position, function, arg);
264 position += LEFT_TOTAL_LENGTH (tree);
265 tree->position = position;
266 (*function) (tree, arg);
267 position += LENGTH (tree); tree = tree->right;
271 #if 0
273 static int icount;
274 static int idepth;
275 static int zero_length;
277 /* These functions are temporary, for debugging purposes only. */
279 INTERVAL search_interval, found_interval;
281 void
282 check_for_interval (INTERVAL i)
284 if (i == search_interval)
286 found_interval = i;
287 icount++;
291 INTERVAL
292 search_for_interval (INTERVAL i, INTERVAL tree)
294 icount = 0;
295 search_interval = i;
296 found_interval = NULL;
297 traverse_intervals_noorder (tree, &check_for_interval, Qnil);
298 return found_interval;
301 static void
302 inc_interval_count (INTERVAL i)
304 icount++;
305 if (LENGTH (i) == 0)
306 zero_length++;
307 if (depth > idepth)
308 idepth = depth;
312 count_intervals (INTERVAL i)
314 icount = 0;
315 idepth = 0;
316 zero_length = 0;
317 traverse_intervals_noorder (i, &inc_interval_count, Qnil);
319 return icount;
322 static INTERVAL
323 root_interval (INTERVAL interval)
325 register INTERVAL i = interval;
327 while (! ROOT_INTERVAL_P (i))
328 i = INTERVAL_PARENT (i);
330 return i;
332 #endif
334 /* Assuming that a left child exists, perform the following operation:
337 / \ / \
338 B => A
339 / \ / \
343 static INTERVAL
344 rotate_right (INTERVAL interval)
346 INTERVAL i;
347 INTERVAL B = interval->left;
348 ptrdiff_t old_total = interval->total_length;
350 /* Deal with any Parent of A; make it point to B. */
351 if (! ROOT_INTERVAL_P (interval))
353 if (AM_LEFT_CHILD (interval))
354 set_interval_left (INTERVAL_PARENT (interval), B);
355 else
356 set_interval_right (INTERVAL_PARENT (interval), B);
358 copy_interval_parent (B, interval);
360 /* Make B the parent of A */
361 i = B->right;
362 set_interval_right (B, interval);
363 set_interval_parent (interval, B);
365 /* Make A point to c */
366 set_interval_left (interval, i);
367 if (i)
368 set_interval_parent (i, interval);
370 /* A's total length is decreased by the length of B and its left child. */
371 interval->total_length -= B->total_length - LEFT_TOTAL_LENGTH (interval);
372 eassert (TOTAL_LENGTH (interval) >= 0);
374 /* B must have the same total length of A. */
375 B->total_length = old_total;
376 eassert (TOTAL_LENGTH (B) >= 0);
378 return B;
381 /* Assuming that a right child exists, perform the following operation:
384 / \ / \
385 B => A
386 / \ / \
390 static INTERVAL
391 rotate_left (INTERVAL interval)
393 INTERVAL i;
394 INTERVAL B = interval->right;
395 ptrdiff_t old_total = interval->total_length;
397 /* Deal with any parent of A; make it point to B. */
398 if (! ROOT_INTERVAL_P (interval))
400 if (AM_LEFT_CHILD (interval))
401 set_interval_left (INTERVAL_PARENT (interval), B);
402 else
403 set_interval_right (INTERVAL_PARENT (interval), B);
405 copy_interval_parent (B, interval);
407 /* Make B the parent of A */
408 i = B->left;
409 set_interval_left (B, interval);
410 set_interval_parent (interval, B);
412 /* Make A point to c */
413 set_interval_right (interval, i);
414 if (i)
415 set_interval_parent (i, interval);
417 /* A's total length is decreased by the length of B and its right child. */
418 interval->total_length -= B->total_length - RIGHT_TOTAL_LENGTH (interval);
419 eassert (TOTAL_LENGTH (interval) >= 0);
421 /* B must have the same total length of A. */
422 B->total_length = old_total;
423 eassert (TOTAL_LENGTH (B) >= 0);
425 return B;
428 /* Balance an interval tree with the assumption that the subtrees
429 themselves are already balanced. */
431 static INTERVAL
432 balance_an_interval (INTERVAL i)
434 register ptrdiff_t old_diff, new_diff;
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 = 0;
474 if (!INTERVAL_HAS_OBJECT (interval) && !INTERVAL_HAS_PARENT (interval))
475 return interval;
477 if (INTERVAL_HAS_OBJECT (interval))
479 have_parent = 1;
480 GET_INTERVAL_OBJECT (parent, 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 (TOTAL_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 eassert (TOTAL_LENGTH (new) >= 0);
567 balance_an_interval (new);
570 balance_possible_root_interval (interval);
572 return new;
575 /* Split INTERVAL into two pieces, starting the second piece at
576 character position OFFSET (counting from 0), relative to INTERVAL.
577 INTERVAL becomes the right-hand piece, and the left-hand piece
578 (first, lexicographically) is returned.
580 The size and position fields of the two intervals are set based upon
581 those of the original interval. The property list of the new interval
582 is reset, thus it is up to the caller to do the right thing with the
583 result.
585 Note that this does not change the position of INTERVAL; if it is a root,
586 it is still a root after this operation. */
588 INTERVAL
589 split_interval_left (INTERVAL interval, ptrdiff_t offset)
591 INTERVAL new = make_interval ();
592 ptrdiff_t new_length = offset;
594 new->position = interval->position;
595 interval->position = interval->position + offset;
596 set_interval_parent (new, interval);
598 if (NULL_LEFT_CHILD (interval))
600 set_interval_left (interval, new);
601 new->total_length = new_length;
602 eassert (TOTAL_LENGTH (new) >= 0);
604 else
606 /* Insert the new node between INTERVAL and its left child. */
607 set_interval_left (new, interval->left);
608 set_interval_parent (new->left, new);
609 set_interval_left (interval, new);
610 new->total_length = new_length + new->left->total_length;
611 eassert (TOTAL_LENGTH (new) >= 0);
612 balance_an_interval (new);
615 balance_possible_root_interval (interval);
617 return new;
620 /* Return the proper position for the first character
621 described by the interval tree SOURCE.
622 This is 1 if the parent is a buffer,
623 0 if the parent is a string or if there is no parent.
625 Don't use this function on an interval which is the child
626 of another interval! */
628 static int
629 interval_start_pos (INTERVAL source)
631 Lisp_Object parent;
633 if (!source)
634 return 0;
636 if (! INTERVAL_HAS_OBJECT (source))
637 return 0;
638 GET_INTERVAL_OBJECT (parent, source);
639 if (BUFFERP (parent))
640 return BUF_BEG (XBUFFER (parent));
641 return 0;
644 /* Find the interval containing text position POSITION in the text
645 represented by the interval tree TREE. POSITION is a buffer
646 position (starting from 1) or a string index (starting from 0).
647 If POSITION is at the end of the buffer or string,
648 return the interval containing the last character.
650 The `position' field, which is a cache of an interval's position,
651 is updated in the interval found. Other functions (e.g., next_interval)
652 will update this cache based on the result of find_interval. */
654 INTERVAL
655 find_interval (register INTERVAL tree, register ptrdiff_t position)
657 /* The distance from the left edge of the subtree at TREE
658 to POSITION. */
659 register ptrdiff_t relative_position;
661 if (!tree)
662 return NULL;
664 relative_position = position;
665 if (INTERVAL_HAS_OBJECT (tree))
667 Lisp_Object parent;
668 GET_INTERVAL_OBJECT (parent, tree);
669 if (BUFFERP (parent))
670 relative_position -= BUF_BEG (XBUFFER (parent));
673 eassert (relative_position <= TOTAL_LENGTH (tree));
675 tree = balance_possible_root_interval (tree);
677 while (1)
679 eassert (tree);
680 if (relative_position < LEFT_TOTAL_LENGTH (tree))
682 tree = tree->left;
684 else if (! NULL_RIGHT_CHILD (tree)
685 && relative_position >= (TOTAL_LENGTH (tree)
686 - RIGHT_TOTAL_LENGTH (tree)))
688 relative_position -= (TOTAL_LENGTH (tree)
689 - RIGHT_TOTAL_LENGTH (tree));
690 tree = tree->right;
692 else
694 tree->position
695 = (position - relative_position /* left edge of *tree. */
696 + LEFT_TOTAL_LENGTH (tree)); /* left edge of this interval. */
698 return tree;
703 /* Find the succeeding interval (lexicographically) to INTERVAL.
704 Sets the `position' field based on that of INTERVAL (see
705 find_interval). */
707 INTERVAL
708 next_interval (register INTERVAL interval)
710 register INTERVAL i = interval;
711 register ptrdiff_t next_position;
713 if (!i)
714 return NULL;
715 next_position = interval->position + LENGTH (interval);
717 if (! NULL_RIGHT_CHILD (i))
719 i = i->right;
720 while (! NULL_LEFT_CHILD (i))
721 i = i->left;
723 i->position = next_position;
724 return i;
727 while (! NULL_PARENT (i))
729 if (AM_LEFT_CHILD (i))
731 i = INTERVAL_PARENT (i);
732 i->position = next_position;
733 return i;
736 i = INTERVAL_PARENT (i);
739 return NULL;
742 /* Find the preceding interval (lexicographically) to INTERVAL.
743 Sets the `position' field based on that of INTERVAL (see
744 find_interval). */
746 INTERVAL
747 previous_interval (register INTERVAL interval)
749 register INTERVAL i;
751 if (!interval)
752 return NULL;
754 if (! NULL_LEFT_CHILD (interval))
756 i = interval->left;
757 while (! NULL_RIGHT_CHILD (i))
758 i = i->right;
760 i->position = interval->position - LENGTH (i);
761 return i;
764 i = interval;
765 while (! NULL_PARENT (i))
767 if (AM_RIGHT_CHILD (i))
769 i = INTERVAL_PARENT (i);
771 i->position = interval->position - LENGTH (i);
772 return i;
774 i = INTERVAL_PARENT (i);
777 return NULL;
780 /* Find the interval containing POS given some non-NULL INTERVAL
781 in the same tree. Note that we need to update interval->position
782 if we go down the tree.
783 To speed up the process, we assume that the ->position of
784 I and all its parents is already uptodate. */
785 INTERVAL
786 update_interval (register INTERVAL i, ptrdiff_t pos)
788 if (!i)
789 return NULL;
791 while (1)
793 if (pos < i->position)
795 /* Move left. */
796 if (pos >= i->position - TOTAL_LENGTH (i->left))
798 i->left->position = i->position - TOTAL_LENGTH (i->left)
799 + LEFT_TOTAL_LENGTH (i->left);
800 i = i->left; /* Move to the left child */
802 else if (NULL_PARENT (i))
803 error ("Point before start of properties");
804 else
805 i = INTERVAL_PARENT (i);
806 continue;
808 else if (pos >= INTERVAL_LAST_POS (i))
810 /* Move right. */
811 if (pos < INTERVAL_LAST_POS (i) + TOTAL_LENGTH (i->right))
813 i->right->position = INTERVAL_LAST_POS (i)
814 + LEFT_TOTAL_LENGTH (i->right);
815 i = i->right; /* Move to the right child */
817 else if (NULL_PARENT (i))
818 error ("Point %"pD"d after end of properties", pos);
819 else
820 i = INTERVAL_PARENT (i);
821 continue;
823 else
824 return i;
828 /* Effect an adjustment corresponding to the addition of LENGTH characters
829 of text. Do this by finding the interval containing POSITION in the
830 interval tree TREE, and then adjusting all of its ancestors by adding
831 LENGTH to them.
833 If POSITION is the first character of an interval, meaning that point
834 is actually between the two intervals, make the new text belong to
835 the interval which is "sticky".
837 If both intervals are "sticky", then make them belong to the left-most
838 interval. Another possibility would be to create a new interval for
839 this text, and make it have the merged properties of both ends. */
841 static INTERVAL
842 adjust_intervals_for_insertion (INTERVAL tree,
843 ptrdiff_t position, ptrdiff_t length)
845 INTERVAL i;
846 INTERVAL temp;
847 bool eobp = 0;
848 Lisp_Object parent;
849 ptrdiff_t offset;
851 eassert (TOTAL_LENGTH (tree) > 0);
853 GET_INTERVAL_OBJECT (parent, tree);
854 offset = (BUFFERP (parent) ? BUF_BEG (XBUFFER (parent)) : 0);
856 /* If inserting at point-max of a buffer, that position will be out
857 of range. Remember that buffer positions are 1-based. */
858 if (position >= TOTAL_LENGTH (tree) + offset)
860 position = TOTAL_LENGTH (tree) + offset;
861 eobp = 1;
864 i = find_interval (tree, position);
866 /* If in middle of an interval which is not sticky either way,
867 we must not just give its properties to the insertion.
868 So split this interval at the insertion point.
870 Originally, the if condition here was this:
871 (! (position == i->position || eobp)
872 && END_NONSTICKY_P (i)
873 && FRONT_NONSTICKY_P (i))
874 But, these macros are now unreliable because of introduction of
875 Vtext_property_default_nonsticky. So, we always check properties
876 one by one if POSITION is in middle of an interval. */
877 if (! (position == i->position || eobp))
879 Lisp_Object tail;
880 Lisp_Object front, rear;
882 tail = i->plist;
884 /* Properties font-sticky and rear-nonsticky override
885 Vtext_property_default_nonsticky. So, if they are t, we can
886 skip one by one checking of properties. */
887 rear = textget (i->plist, Qrear_nonsticky);
888 if (! CONSP (rear) && ! NILP (rear))
890 /* All properties are nonsticky. We split the interval. */
891 goto check_done;
893 front = textget (i->plist, Qfront_sticky);
894 if (! CONSP (front) && ! NILP (front))
896 /* All properties are sticky. We don't split the interval. */
897 tail = Qnil;
898 goto check_done;
901 /* Does any actual property pose an actual problem? We break
902 the loop if we find a nonsticky property. */
903 for (; CONSP (tail); tail = Fcdr (XCDR (tail)))
905 Lisp_Object prop, tmp;
906 prop = XCAR (tail);
908 /* Is this particular property front-sticky? */
909 if (CONSP (front) && ! NILP (Fmemq (prop, front)))
910 continue;
912 /* Is this particular property rear-nonsticky? */
913 if (CONSP (rear) && ! NILP (Fmemq (prop, rear)))
914 break;
916 /* Is this particular property recorded as sticky or
917 nonsticky in Vtext_property_default_nonsticky? */
918 tmp = Fassq (prop, Vtext_property_default_nonsticky);
919 if (CONSP (tmp))
921 if (NILP (tmp))
922 continue;
923 break;
926 /* By default, a text property is rear-sticky, thus we
927 continue the loop. */
930 check_done:
931 /* If any property is a real problem, split the interval. */
932 if (! NILP (tail))
934 temp = split_interval_right (i, position - i->position);
935 copy_properties (i, temp);
936 i = temp;
940 /* If we are positioned between intervals, check the stickiness of
941 both of them. We have to do this too, if we are at BEG or Z. */
942 if (position == i->position || eobp)
944 register INTERVAL prev;
946 if (position == BEG)
947 prev = 0;
948 else if (eobp)
950 prev = i;
951 i = 0;
953 else
954 prev = previous_interval (i);
956 /* Even if we are positioned between intervals, we default
957 to the left one if it exists. We extend it now and split
958 off a part later, if stickiness demands it. */
959 for (temp = prev ? prev : i; temp; temp = INTERVAL_PARENT_OR_NULL (temp))
961 temp->total_length += length;
962 eassert (TOTAL_LENGTH (temp) >= 0);
963 temp = balance_possible_root_interval (temp);
966 /* If at least one interval has sticky properties,
967 we check the stickiness property by property.
969 Originally, the if condition here was this:
970 (END_NONSTICKY_P (prev) || FRONT_STICKY_P (i))
971 But, these macros are now unreliable because of introduction
972 of Vtext_property_default_nonsticky. So, we always have to
973 check stickiness of properties one by one. If cache of
974 stickiness is implemented in the future, we may be able to
975 use those macros again. */
976 if (1)
978 Lisp_Object pleft, pright;
979 struct interval newi;
981 RESET_INTERVAL (&newi);
982 pleft = prev ? prev->plist : Qnil;
983 pright = i ? i->plist : Qnil;
984 set_interval_plist (&newi, merge_properties_sticky (pleft, pright));
986 if (! prev) /* i.e. position == BEG */
988 if (! intervals_equal (i, &newi))
990 i = split_interval_left (i, length);
991 set_interval_plist (i, newi.plist);
994 else if (! intervals_equal (prev, &newi))
996 prev = split_interval_right (prev, position - prev->position);
997 set_interval_plist (prev, newi.plist);
998 if (i && intervals_equal (prev, i))
999 merge_interval_right (prev);
1002 /* We will need to update the cache here later. */
1004 else if (! prev && ! NILP (i->plist))
1006 /* Just split off a new interval at the left.
1007 Since I wasn't front-sticky, the empty plist is ok. */
1008 i = split_interval_left (i, length);
1012 /* Otherwise just extend the interval. */
1013 else
1015 for (temp = i; temp; temp = INTERVAL_PARENT_OR_NULL (temp))
1017 temp->total_length += length;
1018 eassert (TOTAL_LENGTH (temp) >= 0);
1019 temp = balance_possible_root_interval (temp);
1023 return tree;
1026 /* Any property might be front-sticky on the left, rear-sticky on the left,
1027 front-sticky on the right, or rear-sticky on the right; the 16 combinations
1028 can be arranged in a matrix with rows denoting the left conditions and
1029 columns denoting the right conditions:
1030 _ __ _
1031 _ FR FR FR FR
1032 FR__ 0 1 2 3
1033 _FR 4 5 6 7
1034 FR 8 9 A B
1035 FR C D E F
1037 left-props = '(front-sticky (p8 p9 pa pb pc pd pe pf)
1038 rear-nonsticky (p4 p5 p6 p7 p8 p9 pa pb)
1039 p0 L p1 L p2 L p3 L p4 L p5 L p6 L p7 L
1040 p8 L p9 L pa L pb L pc L pd L pe L pf L)
1041 right-props = '(front-sticky (p2 p3 p6 p7 pa pb pe pf)
1042 rear-nonsticky (p1 p2 p5 p6 p9 pa pd pe)
1043 p0 R p1 R p2 R p3 R p4 R p5 R p6 R p7 R
1044 p8 R p9 R pa R pb R pc R pd R pe R pf R)
1046 We inherit from whoever has a sticky side facing us. If both sides
1047 do (cases 2, 3, E, and F), then we inherit from whichever side has a
1048 non-nil value for the current property. If both sides do, then we take
1049 from the left.
1051 When we inherit a property, we get its stickiness as well as its value.
1052 So, when we merge the above two lists, we expect to get this:
1054 result = '(front-sticky (p6 p7 pa pb pc pd pe pf)
1055 rear-nonsticky (p6 pa)
1056 p0 L p1 L p2 L p3 L p6 R p7 R
1057 pa R pb R pc L pd L pe L pf L)
1059 The optimizable special cases are:
1060 left rear-nonsticky = nil, right front-sticky = nil (inherit left)
1061 left rear-nonsticky = t, right front-sticky = t (inherit right)
1062 left rear-nonsticky = t, right front-sticky = nil (inherit none)
1065 static Lisp_Object
1066 merge_properties_sticky (Lisp_Object pleft, Lisp_Object pright)
1068 Lisp_Object props, front, rear;
1069 Lisp_Object lfront, lrear, rfront, rrear;
1070 Lisp_Object tail1, tail2, sym, lval, rval, cat;
1071 bool use_left, use_right, lpresent;
1073 props = Qnil;
1074 front = Qnil;
1075 rear = Qnil;
1076 lfront = textget (pleft, Qfront_sticky);
1077 lrear = textget (pleft, Qrear_nonsticky);
1078 rfront = textget (pright, Qfront_sticky);
1079 rrear = textget (pright, Qrear_nonsticky);
1081 /* Go through each element of PRIGHT. */
1082 for (tail1 = pright; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
1084 Lisp_Object tmp;
1086 sym = XCAR (tail1);
1088 /* Sticky properties get special treatment. */
1089 if (EQ (sym, Qrear_nonsticky) || EQ (sym, Qfront_sticky))
1090 continue;
1092 rval = Fcar (XCDR (tail1));
1093 for (tail2 = pleft; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
1094 if (EQ (sym, XCAR (tail2)))
1095 break;
1097 /* Indicate whether the property is explicitly defined on the left.
1098 (We know it is defined explicitly on the right
1099 because otherwise we don't get here.) */
1100 lpresent = ! NILP (tail2);
1101 lval = (NILP (tail2) ? Qnil : Fcar (Fcdr (tail2)));
1103 /* Even if lrear or rfront say nothing about the stickiness of
1104 SYM, Vtext_property_default_nonsticky may give default
1105 stickiness to SYM. */
1106 tmp = Fassq (sym, Vtext_property_default_nonsticky);
1107 use_left = (lpresent
1108 && ! (TMEM (sym, lrear)
1109 || (CONSP (tmp) && ! NILP (XCDR (tmp)))));
1110 use_right = (TMEM (sym, rfront)
1111 || (CONSP (tmp) && NILP (XCDR (tmp))));
1112 if (use_left && use_right)
1114 if (NILP (lval))
1115 use_left = 0;
1116 else if (NILP (rval))
1117 use_right = 0;
1119 if (use_left)
1121 /* We build props as (value sym ...) rather than (sym value ...)
1122 because we plan to nreverse it when we're done. */
1123 props = Fcons (lval, Fcons (sym, props));
1124 if (TMEM (sym, lfront))
1125 front = Fcons (sym, front);
1126 if (TMEM (sym, lrear))
1127 rear = Fcons (sym, rear);
1129 else if (use_right)
1131 props = Fcons (rval, Fcons (sym, props));
1132 if (TMEM (sym, rfront))
1133 front = Fcons (sym, front);
1134 if (TMEM (sym, rrear))
1135 rear = Fcons (sym, rear);
1139 /* Now go through each element of PLEFT. */
1140 for (tail2 = pleft; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
1142 Lisp_Object tmp;
1144 sym = XCAR (tail2);
1146 /* Sticky properties get special treatment. */
1147 if (EQ (sym, Qrear_nonsticky) || EQ (sym, Qfront_sticky))
1148 continue;
1150 /* If sym is in PRIGHT, we've already considered it. */
1151 for (tail1 = pright; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
1152 if (EQ (sym, XCAR (tail1)))
1153 break;
1154 if (! NILP (tail1))
1155 continue;
1157 lval = Fcar (XCDR (tail2));
1159 /* Even if lrear or rfront say nothing about the stickiness of
1160 SYM, Vtext_property_default_nonsticky may give default
1161 stickiness to SYM. */
1162 tmp = Fassq (sym, Vtext_property_default_nonsticky);
1164 /* Since rval is known to be nil in this loop, the test simplifies. */
1165 if (! (TMEM (sym, lrear) || (CONSP (tmp) && ! NILP (XCDR (tmp)))))
1167 props = Fcons (lval, Fcons (sym, props));
1168 if (TMEM (sym, lfront))
1169 front = Fcons (sym, front);
1171 else if (TMEM (sym, rfront) || (CONSP (tmp) && NILP (XCDR (tmp))))
1173 /* The value is nil, but we still inherit the stickiness
1174 from the right. */
1175 front = Fcons (sym, front);
1176 if (TMEM (sym, rrear))
1177 rear = Fcons (sym, rear);
1180 props = Fnreverse (props);
1181 if (! NILP (rear))
1182 props = Fcons (Qrear_nonsticky, Fcons (Fnreverse (rear), props));
1184 cat = textget (props, Qcategory);
1185 if (! NILP (front)
1187 /* If we have inherited a front-stick category property that is t,
1188 we don't need to set up a detailed one. */
1189 ! (! NILP (cat) && SYMBOLP (cat)
1190 && EQ (Fget (cat, Qfront_sticky), Qt)))
1191 props = Fcons (Qfront_sticky, Fcons (Fnreverse (front), props));
1192 return props;
1196 /* Delete a node I from its interval tree by merging its subtrees
1197 into one subtree which is then returned. Caller is responsible for
1198 storing the resulting subtree into its parent. */
1200 static INTERVAL
1201 delete_node (register INTERVAL i)
1203 register INTERVAL migrate, this;
1204 register ptrdiff_t migrate_amt;
1206 if (!i->left)
1207 return i->right;
1208 if (!i->right)
1209 return i->left;
1211 migrate = i->left;
1212 migrate_amt = i->left->total_length;
1213 this = i->right;
1214 this->total_length += migrate_amt;
1215 while (this->left)
1217 this = this->left;
1218 this->total_length += migrate_amt;
1220 eassert (TOTAL_LENGTH (this) >= 0);
1221 set_interval_left (this, migrate);
1222 set_interval_parent (migrate, this);
1224 return i->right;
1227 /* Delete interval I from its tree by calling `delete_node'
1228 and properly connecting the resultant subtree.
1230 I is presumed to be empty; that is, no adjustments are made
1231 for the length of I. */
1233 static void
1234 delete_interval (register INTERVAL i)
1236 register INTERVAL parent;
1237 ptrdiff_t amt = LENGTH (i);
1239 eassert (amt == 0); /* Only used on zero-length intervals now. */
1241 if (ROOT_INTERVAL_P (i))
1243 Lisp_Object owner;
1244 GET_INTERVAL_OBJECT (owner, i);
1245 parent = delete_node (i);
1246 if (parent)
1247 set_interval_object (parent, owner);
1249 if (BUFFERP (owner))
1250 set_buffer_intervals (XBUFFER (owner), parent);
1251 else if (STRINGP (owner))
1252 set_string_intervals (owner, parent);
1253 else
1254 emacs_abort ();
1256 return;
1259 parent = INTERVAL_PARENT (i);
1260 if (AM_LEFT_CHILD (i))
1262 set_interval_left (parent, delete_node (i));
1263 if (parent->left)
1264 set_interval_parent (parent->left, parent);
1266 else
1268 set_interval_right (parent, delete_node (i));
1269 if (parent->right)
1270 set_interval_parent (parent->right, parent);
1274 /* Find the interval in TREE corresponding to the relative position
1275 FROM and delete as much as possible of AMOUNT from that interval.
1276 Return the amount actually deleted, and if the interval was
1277 zeroed-out, delete that interval node from the tree.
1279 Note that FROM is actually origin zero, aka relative to the
1280 leftmost edge of tree. This is appropriate since we call ourselves
1281 recursively on subtrees.
1283 Do this by recursing down TREE to the interval in question, and
1284 deleting the appropriate amount of text. */
1286 static ptrdiff_t
1287 interval_deletion_adjustment (register INTERVAL tree, register ptrdiff_t from,
1288 register ptrdiff_t amount)
1290 register ptrdiff_t relative_position = from;
1292 if (!tree)
1293 return 0;
1295 /* Left branch. */
1296 if (relative_position < LEFT_TOTAL_LENGTH (tree))
1298 ptrdiff_t subtract = interval_deletion_adjustment (tree->left,
1299 relative_position,
1300 amount);
1301 tree->total_length -= subtract;
1302 eassert (TOTAL_LENGTH (tree) >= 0);
1303 return subtract;
1305 /* Right branch. */
1306 else if (relative_position >= (TOTAL_LENGTH (tree)
1307 - RIGHT_TOTAL_LENGTH (tree)))
1309 ptrdiff_t subtract;
1311 relative_position -= (tree->total_length
1312 - RIGHT_TOTAL_LENGTH (tree));
1313 subtract = interval_deletion_adjustment (tree->right,
1314 relative_position,
1315 amount);
1316 tree->total_length -= subtract;
1317 eassert (TOTAL_LENGTH (tree) >= 0);
1318 return subtract;
1320 /* Here -- this node. */
1321 else
1323 /* How much can we delete from this interval? */
1324 ptrdiff_t my_amount = ((tree->total_length
1325 - RIGHT_TOTAL_LENGTH (tree))
1326 - relative_position);
1328 if (amount > my_amount)
1329 amount = my_amount;
1331 tree->total_length -= amount;
1332 eassert (TOTAL_LENGTH (tree) >= 0);
1333 if (LENGTH (tree) == 0)
1334 delete_interval (tree);
1336 return amount;
1339 /* Never reach here. */
1342 /* Effect the adjustments necessary to the interval tree of BUFFER to
1343 correspond to the deletion of LENGTH characters from that buffer
1344 text. The deletion is effected at position START (which is a
1345 buffer position, i.e. origin 1). */
1347 static void
1348 adjust_intervals_for_deletion (struct buffer *buffer,
1349 ptrdiff_t start, ptrdiff_t length)
1351 ptrdiff_t left_to_delete = length;
1352 INTERVAL tree = buffer_intervals (buffer);
1353 Lisp_Object parent;
1354 ptrdiff_t offset;
1356 GET_INTERVAL_OBJECT (parent, tree);
1357 offset = (BUFFERP (parent) ? BUF_BEG (XBUFFER (parent)) : 0);
1359 if (!tree)
1360 return;
1362 eassert (start <= offset + TOTAL_LENGTH (tree)
1363 && start + length <= offset + TOTAL_LENGTH (tree));
1365 if (length == TOTAL_LENGTH (tree))
1367 set_buffer_intervals (buffer, NULL);
1368 return;
1371 if (ONLY_INTERVAL_P (tree))
1373 tree->total_length -= length;
1374 eassert (TOTAL_LENGTH (tree) >= 0);
1375 return;
1378 if (start > offset + TOTAL_LENGTH (tree))
1379 start = offset + TOTAL_LENGTH (tree);
1380 while (left_to_delete > 0)
1382 left_to_delete -= interval_deletion_adjustment (tree, start - offset,
1383 left_to_delete);
1384 tree = buffer_intervals (buffer);
1385 if (left_to_delete == tree->total_length)
1387 set_buffer_intervals (buffer, NULL);
1388 return;
1393 /* Make the adjustments necessary to the interval tree of BUFFER to
1394 represent an addition or deletion of LENGTH characters starting
1395 at position START. Addition or deletion is indicated by the sign
1396 of LENGTH. */
1398 void
1399 offset_intervals (struct buffer *buffer, ptrdiff_t start, ptrdiff_t length)
1401 if (!buffer_intervals (buffer) || length == 0)
1402 return;
1404 if (length > 0)
1405 adjust_intervals_for_insertion (buffer_intervals (buffer),
1406 start, length);
1407 else
1408 adjust_intervals_for_deletion (buffer, start, -length);
1411 /* Merge interval I with its lexicographic successor. The resulting
1412 interval is returned, and has the properties of the original
1413 successor. The properties of I are lost. I is removed from the
1414 interval tree.
1416 IMPORTANT:
1417 The caller must verify that this is not the last (rightmost)
1418 interval. */
1420 static INTERVAL
1421 merge_interval_right (register INTERVAL i)
1423 register ptrdiff_t absorb = LENGTH (i);
1424 register INTERVAL successor;
1426 /* Find the succeeding interval. */
1427 if (! NULL_RIGHT_CHILD (i)) /* It's below us. Add absorb
1428 as we descend. */
1430 successor = i->right;
1431 while (! NULL_LEFT_CHILD (successor))
1433 successor->total_length += absorb;
1434 eassert (TOTAL_LENGTH (successor) >= 0);
1435 successor = successor->left;
1438 successor->total_length += absorb;
1439 eassert (TOTAL_LENGTH (successor) >= 0);
1440 delete_interval (i);
1441 return successor;
1444 /* Zero out this interval. */
1445 i->total_length -= absorb;
1446 eassert (TOTAL_LENGTH (i) >= 0);
1448 successor = i;
1449 while (! NULL_PARENT (successor)) /* It's above us. Subtract as
1450 we ascend. */
1452 if (AM_LEFT_CHILD (successor))
1454 successor = INTERVAL_PARENT (successor);
1455 delete_interval (i);
1456 return successor;
1459 successor = INTERVAL_PARENT (successor);
1460 successor->total_length -= absorb;
1461 eassert (TOTAL_LENGTH (successor) >= 0);
1464 /* This must be the rightmost or last interval and cannot
1465 be merged right. The caller should have known. */
1466 emacs_abort ();
1469 /* Merge interval I with its lexicographic predecessor. The resulting
1470 interval is returned, and has the properties of the original predecessor.
1471 The properties of I are lost. Interval node I is removed from the tree.
1473 IMPORTANT:
1474 The caller must verify that this is not the first (leftmost) interval. */
1476 INTERVAL
1477 merge_interval_left (register INTERVAL i)
1479 register ptrdiff_t absorb = LENGTH (i);
1480 register INTERVAL predecessor;
1482 /* Find the preceding interval. */
1483 if (! NULL_LEFT_CHILD (i)) /* It's below us. Go down,
1484 adding ABSORB as we go. */
1486 predecessor = i->left;
1487 while (! NULL_RIGHT_CHILD (predecessor))
1489 predecessor->total_length += absorb;
1490 eassert (TOTAL_LENGTH (predecessor) >= 0);
1491 predecessor = predecessor->right;
1494 predecessor->total_length += absorb;
1495 eassert (TOTAL_LENGTH (predecessor) >= 0);
1496 delete_interval (i);
1497 return predecessor;
1500 /* Zero out this interval. */
1501 i->total_length -= absorb;
1502 eassert (TOTAL_LENGTH (i) >= 0);
1504 predecessor = i;
1505 while (! NULL_PARENT (predecessor)) /* It's above us. Go up,
1506 subtracting ABSORB. */
1508 if (AM_RIGHT_CHILD (predecessor))
1510 predecessor = INTERVAL_PARENT (predecessor);
1511 delete_interval (i);
1512 return predecessor;
1515 predecessor = INTERVAL_PARENT (predecessor);
1516 predecessor->total_length -= absorb;
1517 eassert (TOTAL_LENGTH (predecessor) >= 0);
1520 /* This must be the leftmost or first interval and cannot
1521 be merged left. The caller should have known. */
1522 emacs_abort ();
1525 /* Create a copy of SOURCE but with the default value of UP. */
1527 static INTERVAL
1528 reproduce_interval (INTERVAL source)
1530 register INTERVAL target = make_interval ();
1532 target->total_length = source->total_length;
1533 target->position = source->position;
1535 copy_properties (source, target);
1537 if (! NULL_LEFT_CHILD (source))
1538 set_interval_left (target, reproduce_tree (source->left, target));
1539 if (! NULL_RIGHT_CHILD (source))
1540 set_interval_right (target, reproduce_tree (source->right, target));
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 if (XMARKER (marker)->buffer != current_buffer)
1825 signal_error ("Marker points into wrong buffer", marker);
1826 set_point_both
1827 (clip_to_bounds (BEGV, marker_position (marker), ZV),
1828 clip_to_bounds (BEGV_BYTE, marker_byte_position (marker), ZV_BYTE));
1831 /* If there's an invisible character at position POS + TEST_OFFS in the
1832 current buffer, and the invisible property has a `stickiness' such that
1833 inserting a character at position POS would inherit the property it,
1834 return POS + ADJ, otherwise return POS. If TEST_INTANG, intangibility
1835 is required as well as invisibility.
1837 TEST_OFFS should be either 0 or -1, and ADJ should be either 1 or -1.
1839 Note that `stickiness' is determined by overlay marker insertion types,
1840 if the invisible property comes from an overlay. */
1842 static ptrdiff_t
1843 adjust_for_invis_intang (ptrdiff_t pos, ptrdiff_t test_offs, ptrdiff_t adj,
1844 bool test_intang)
1846 Lisp_Object invis_propval, invis_overlay;
1847 Lisp_Object test_pos;
1849 if ((adj < 0 && pos + adj < BEGV) || (adj > 0 && pos + adj > ZV))
1850 /* POS + ADJ would be beyond the buffer bounds, so do no adjustment. */
1851 return pos;
1853 test_pos = make_number (pos + test_offs);
1855 invis_propval
1856 = get_char_property_and_overlay (test_pos, Qinvisible, Qnil,
1857 &invis_overlay);
1859 if ((!test_intang
1860 || ! NILP (Fget_char_property (test_pos, Qintangible, Qnil)))
1861 && TEXT_PROP_MEANS_INVISIBLE (invis_propval)
1862 /* This next test is true if the invisible property has a stickiness
1863 such that an insertion at POS would inherit it. */
1864 && (NILP (invis_overlay)
1865 /* Invisible property is from a text-property. */
1866 ? (text_property_stickiness (Qinvisible, make_number (pos), Qnil)
1867 == (test_offs == 0 ? 1 : -1))
1868 /* Invisible property is from an overlay. */
1869 : (test_offs == 0
1870 ? XMARKER (OVERLAY_START (invis_overlay))->insertion_type == 0
1871 : XMARKER (OVERLAY_END (invis_overlay))->insertion_type == 1)))
1872 pos += adj;
1874 return pos;
1877 /* Set point in BUFFER to CHARPOS, which corresponds to byte
1878 position BYTEPOS. If the target position is
1879 before an intangible character, move to an ok place. */
1881 void
1882 set_point_both (ptrdiff_t charpos, ptrdiff_t bytepos)
1884 register INTERVAL to, from, toprev, fromprev;
1885 ptrdiff_t buffer_point;
1886 ptrdiff_t old_position = PT;
1887 /* This ensures that we move forward past intangible text when the
1888 initial position is the same as the destination, in the rare
1889 instances where this is important, e.g. in line-move-finish
1890 (simple.el). */
1891 bool backwards = charpos < old_position;
1892 bool have_overlays;
1893 ptrdiff_t original_position;
1895 bset_point_before_scroll (current_buffer, Qnil);
1897 if (charpos == PT)
1898 return;
1900 /* In a single-byte buffer, the two positions must be equal. */
1901 eassert (ZV != ZV_BYTE || charpos == bytepos);
1903 /* Check this now, before checking if the buffer has any intervals.
1904 That way, we can catch conditions which break this sanity check
1905 whether or not there are intervals in the buffer. */
1906 eassert (charpos <= ZV && charpos >= BEGV);
1908 have_overlays = buffer_has_overlays ();
1910 /* If we have no text properties and overlays,
1911 then we can do it quickly. */
1912 if (!buffer_intervals (current_buffer) && ! have_overlays)
1914 temp_set_point_both (current_buffer, charpos, bytepos);
1915 return;
1918 /* Set TO to the interval containing the char after CHARPOS,
1919 and TOPREV to the interval containing the char before CHARPOS.
1920 Either one may be null. They may be equal. */
1921 to = find_interval (buffer_intervals (current_buffer), charpos);
1922 if (charpos == BEGV)
1923 toprev = 0;
1924 else if (to && to->position == charpos)
1925 toprev = previous_interval (to);
1926 else
1927 toprev = to;
1929 buffer_point = (PT == ZV ? ZV - 1 : PT);
1931 /* Set FROM to the interval containing the char after PT,
1932 and FROMPREV to the interval containing the char before PT.
1933 Either one may be null. They may be equal. */
1934 /* We could cache this and save time. */
1935 from = find_interval (buffer_intervals (current_buffer), buffer_point);
1936 if (buffer_point == BEGV)
1937 fromprev = 0;
1938 else if (from && from->position == PT)
1939 fromprev = previous_interval (from);
1940 else if (buffer_point != PT)
1941 fromprev = from, from = 0;
1942 else
1943 fromprev = from;
1945 /* Moving within an interval. */
1946 if (to == from && toprev == fromprev && INTERVAL_VISIBLE_P (to)
1947 && ! have_overlays)
1949 temp_set_point_both (current_buffer, charpos, bytepos);
1950 return;
1953 original_position = charpos;
1955 /* If the new position is between two intangible characters
1956 with the same intangible property value,
1957 move forward or backward until a change in that property. */
1958 if (NILP (Vinhibit_point_motion_hooks)
1959 && ((to && toprev)
1960 || have_overlays)
1961 /* Intangibility never stops us from positioning at the beginning
1962 or end of the buffer, so don't bother checking in that case. */
1963 && charpos != BEGV && charpos != ZV)
1965 Lisp_Object pos;
1966 Lisp_Object intangible_propval;
1968 if (backwards)
1970 /* If the preceding character is both intangible and invisible,
1971 and the invisible property is `rear-sticky', perturb it so
1972 that the search starts one character earlier -- this ensures
1973 that point can never move to the end of an invisible/
1974 intangible/rear-sticky region. */
1975 charpos = adjust_for_invis_intang (charpos, -1, -1, 1);
1977 XSETINT (pos, charpos);
1979 /* If following char is intangible,
1980 skip back over all chars with matching intangible property. */
1982 intangible_propval = Fget_char_property (pos, Qintangible, Qnil);
1984 if (! NILP (intangible_propval))
1986 while (XINT (pos) > BEGV
1987 && EQ (Fget_char_property (make_number (XINT (pos) - 1),
1988 Qintangible, Qnil),
1989 intangible_propval))
1990 pos = Fprevious_char_property_change (pos, Qnil);
1992 /* Set CHARPOS from POS, and if the final intangible character
1993 that we skipped over is also invisible, and the invisible
1994 property is `front-sticky', perturb it to be one character
1995 earlier -- this ensures that point can never move to the
1996 beginning of an invisible/intangible/front-sticky region. */
1997 charpos = adjust_for_invis_intang (XINT (pos), 0, -1, 0);
2000 else
2002 /* If the following character is both intangible and invisible,
2003 and the invisible property is `front-sticky', perturb it so
2004 that the search starts one character later -- this ensures
2005 that point can never move to the beginning of an
2006 invisible/intangible/front-sticky region. */
2007 charpos = adjust_for_invis_intang (charpos, 0, 1, 1);
2009 XSETINT (pos, charpos);
2011 /* If preceding char is intangible,
2012 skip forward over all chars with matching intangible property. */
2014 intangible_propval = Fget_char_property (make_number (charpos - 1),
2015 Qintangible, Qnil);
2017 if (! NILP (intangible_propval))
2019 while (XINT (pos) < ZV
2020 && EQ (Fget_char_property (pos, Qintangible, Qnil),
2021 intangible_propval))
2022 pos = Fnext_char_property_change (pos, Qnil);
2024 /* Set CHARPOS from POS, and if the final intangible character
2025 that we skipped over is also invisible, and the invisible
2026 property is `rear-sticky', perturb it to be one character
2027 later -- this ensures that point can never move to the
2028 end of an invisible/intangible/rear-sticky region. */
2029 charpos = adjust_for_invis_intang (XINT (pos), -1, 1, 0);
2033 bytepos = buf_charpos_to_bytepos (current_buffer, charpos);
2036 if (charpos != original_position)
2038 /* Set TO to the interval containing the char after CHARPOS,
2039 and TOPREV to the interval containing the char before CHARPOS.
2040 Either one may be null. They may be equal. */
2041 to = find_interval (buffer_intervals (current_buffer), charpos);
2042 if (charpos == BEGV)
2043 toprev = 0;
2044 else if (to && to->position == charpos)
2045 toprev = previous_interval (to);
2046 else
2047 toprev = to;
2050 /* Here TO is the interval after the stopping point
2051 and TOPREV is the interval before the stopping point.
2052 One or the other may be null. */
2054 temp_set_point_both (current_buffer, charpos, bytepos);
2056 /* We run point-left and point-entered hooks here, if the
2057 two intervals are not equivalent. These hooks take
2058 (old_point, new_point) as arguments. */
2059 if (NILP (Vinhibit_point_motion_hooks)
2060 && (! intervals_equal (from, to)
2061 || ! intervals_equal (fromprev, toprev)))
2063 Lisp_Object leave_after, leave_before, enter_after, enter_before;
2065 if (fromprev)
2066 leave_before = textget (fromprev->plist, Qpoint_left);
2067 else
2068 leave_before = Qnil;
2070 if (from)
2071 leave_after = textget (from->plist, Qpoint_left);
2072 else
2073 leave_after = Qnil;
2075 if (toprev)
2076 enter_before = textget (toprev->plist, Qpoint_entered);
2077 else
2078 enter_before = Qnil;
2080 if (to)
2081 enter_after = textget (to->plist, Qpoint_entered);
2082 else
2083 enter_after = Qnil;
2085 if (! EQ (leave_before, enter_before) && !NILP (leave_before))
2086 call2 (leave_before, make_number (old_position),
2087 make_number (charpos));
2088 if (! EQ (leave_after, enter_after) && !NILP (leave_after))
2089 call2 (leave_after, make_number (old_position),
2090 make_number (charpos));
2092 if (! EQ (enter_before, leave_before) && !NILP (enter_before))
2093 call2 (enter_before, make_number (old_position),
2094 make_number (charpos));
2095 if (! EQ (enter_after, leave_after) && !NILP (enter_after))
2096 call2 (enter_after, make_number (old_position),
2097 make_number (charpos));
2101 /* Move point to POSITION, unless POSITION is inside an intangible
2102 segment that reaches all the way to point. */
2104 void
2105 move_if_not_intangible (ptrdiff_t position)
2107 Lisp_Object pos;
2108 Lisp_Object intangible_propval;
2110 XSETINT (pos, position);
2112 if (! NILP (Vinhibit_point_motion_hooks))
2113 /* If intangible is inhibited, always move point to POSITION. */
2115 else if (PT < position && XINT (pos) < ZV)
2117 /* We want to move forward, so check the text before POSITION. */
2119 intangible_propval = Fget_char_property (pos,
2120 Qintangible, Qnil);
2122 /* If following char is intangible,
2123 skip back over all chars with matching intangible property. */
2124 if (! NILP (intangible_propval))
2125 while (XINT (pos) > BEGV
2126 && EQ (Fget_char_property (make_number (XINT (pos) - 1),
2127 Qintangible, Qnil),
2128 intangible_propval))
2129 pos = Fprevious_char_property_change (pos, Qnil);
2131 else if (XINT (pos) > BEGV)
2133 /* We want to move backward, so check the text after POSITION. */
2135 intangible_propval = Fget_char_property (make_number (XINT (pos) - 1),
2136 Qintangible, Qnil);
2138 /* If following char is intangible,
2139 skip forward over all chars with matching intangible property. */
2140 if (! NILP (intangible_propval))
2141 while (XINT (pos) < ZV
2142 && EQ (Fget_char_property (pos, Qintangible, Qnil),
2143 intangible_propval))
2144 pos = Fnext_char_property_change (pos, Qnil);
2147 else if (position < BEGV)
2148 position = BEGV;
2149 else if (position > ZV)
2150 position = ZV;
2152 /* If the whole stretch between PT and POSITION isn't intangible,
2153 try moving to POSITION (which means we actually move farther
2154 if POSITION is inside of intangible text). */
2156 if (XINT (pos) != PT)
2157 SET_PT (position);
2160 /* If text at position POS has property PROP, set *VAL to the property
2161 value, *START and *END to the beginning and end of a region that
2162 has the same property, and return true. Otherwise return false.
2164 OBJECT is the string or buffer to look for the property in;
2165 nil means the current buffer. */
2167 bool
2168 get_property_and_range (ptrdiff_t pos, Lisp_Object prop, Lisp_Object *val,
2169 ptrdiff_t *start, ptrdiff_t *end, Lisp_Object object)
2171 INTERVAL i, prev, next;
2173 if (NILP (object))
2174 i = find_interval (buffer_intervals (current_buffer), pos);
2175 else if (BUFFERP (object))
2176 i = find_interval (buffer_intervals (XBUFFER (object)), pos);
2177 else if (STRINGP (object))
2178 i = find_interval (string_intervals (object), pos);
2179 else
2180 emacs_abort ();
2182 if (!i || (i->position + LENGTH (i) <= pos))
2183 return 0;
2184 *val = textget (i->plist, prop);
2185 if (NILP (*val))
2186 return 0;
2188 next = i; /* remember it in advance */
2189 prev = previous_interval (i);
2190 while (prev
2191 && EQ (*val, textget (prev->plist, prop)))
2192 i = prev, prev = previous_interval (prev);
2193 *start = i->position;
2195 next = next_interval (i);
2196 while (next && EQ (*val, textget (next->plist, prop)))
2197 i = next, next = next_interval (next);
2198 *end = i->position + LENGTH (i);
2200 return 1;
2203 /* Return the proper local keymap TYPE for position POSITION in
2204 BUFFER; TYPE should be one of `keymap' or `local-map'. Use the map
2205 specified by the PROP property, if any. Otherwise, if TYPE is
2206 `local-map' use BUFFER's local map. */
2208 Lisp_Object
2209 get_local_map (ptrdiff_t position, struct buffer *buffer, Lisp_Object type)
2211 Lisp_Object prop, lispy_position, lispy_buffer;
2212 ptrdiff_t old_begv, old_zv, old_begv_byte, old_zv_byte;
2214 position = clip_to_bounds (BUF_BEGV (buffer), position, BUF_ZV (buffer));
2216 /* Ignore narrowing, so that a local map continues to be valid even if
2217 the visible region contains no characters and hence no properties. */
2218 old_begv = BUF_BEGV (buffer);
2219 old_zv = BUF_ZV (buffer);
2220 old_begv_byte = BUF_BEGV_BYTE (buffer);
2221 old_zv_byte = BUF_ZV_BYTE (buffer);
2223 SET_BUF_BEGV_BOTH (buffer, BUF_BEG (buffer), BUF_BEG_BYTE (buffer));
2224 SET_BUF_ZV_BOTH (buffer, BUF_Z (buffer), BUF_Z_BYTE (buffer));
2226 XSETFASTINT (lispy_position, position);
2227 XSETBUFFER (lispy_buffer, buffer);
2228 /* First check if the CHAR has any property. This is because when
2229 we click with the mouse, the mouse pointer is really pointing
2230 to the CHAR after POS. */
2231 prop = Fget_char_property (lispy_position, type, lispy_buffer);
2232 /* If not, look at the POS's properties. This is necessary because when
2233 editing a field with a `local-map' property, we want insertion at the end
2234 to obey the `local-map' property. */
2235 if (NILP (prop))
2236 prop = Fget_pos_property (lispy_position, type, lispy_buffer);
2238 SET_BUF_BEGV_BOTH (buffer, old_begv, old_begv_byte);
2239 SET_BUF_ZV_BOTH (buffer, old_zv, old_zv_byte);
2241 /* Use the local map only if it is valid. */
2242 prop = get_keymap (prop, 0, 0);
2243 if (CONSP (prop))
2244 return prop;
2246 if (EQ (type, Qkeymap))
2247 return Qnil;
2248 else
2249 return BVAR (buffer, keymap);
2252 /* Produce an interval tree reflecting the intervals in
2253 TREE from START to START + LENGTH.
2254 The new interval tree has no parent and has a starting-position of 0. */
2256 INTERVAL
2257 copy_intervals (INTERVAL tree, ptrdiff_t start, ptrdiff_t length)
2259 register INTERVAL i, new, t;
2260 register ptrdiff_t got, prevlen;
2262 if (!tree || length <= 0)
2263 return NULL;
2265 i = find_interval (tree, start);
2266 eassert (i && LENGTH (i) > 0);
2268 /* If there is only one interval and it's the default, return nil. */
2269 if ((start - i->position + 1 + length) < LENGTH (i)
2270 && DEFAULT_INTERVAL_P (i))
2271 return NULL;
2273 new = make_interval ();
2274 new->position = 0;
2275 got = (LENGTH (i) - (start - i->position));
2276 new->total_length = length;
2277 eassert (TOTAL_LENGTH (new) >= 0);
2278 copy_properties (i, new);
2280 t = new;
2281 prevlen = got;
2282 while (got < length)
2284 i = next_interval (i);
2285 t = split_interval_right (t, prevlen);
2286 copy_properties (i, t);
2287 prevlen = LENGTH (i);
2288 got += prevlen;
2291 return balance_an_interval (new);
2294 /* Give STRING the properties of BUFFER from POSITION to LENGTH. */
2296 void
2297 copy_intervals_to_string (Lisp_Object string, struct buffer *buffer,
2298 ptrdiff_t position, ptrdiff_t length)
2300 INTERVAL interval_copy = copy_intervals (buffer_intervals (buffer),
2301 position, length);
2302 if (!interval_copy)
2303 return;
2305 set_interval_object (interval_copy, string);
2306 set_string_intervals (string, interval_copy);
2309 /* Return true if strings S1 and S2 have identical properties.
2310 Assume they have identical characters. */
2312 bool
2313 compare_string_intervals (Lisp_Object s1, Lisp_Object s2)
2315 INTERVAL i1, i2;
2316 ptrdiff_t pos = 0;
2317 ptrdiff_t end = SCHARS (s1);
2319 i1 = find_interval (string_intervals (s1), 0);
2320 i2 = find_interval (string_intervals (s2), 0);
2322 while (pos < end)
2324 /* Determine how far we can go before we reach the end of I1 or I2. */
2325 ptrdiff_t len1 = (i1 != 0 ? INTERVAL_LAST_POS (i1) : end) - pos;
2326 ptrdiff_t len2 = (i2 != 0 ? INTERVAL_LAST_POS (i2) : end) - pos;
2327 ptrdiff_t distance = min (len1, len2);
2329 /* If we ever find a mismatch between the strings,
2330 they differ. */
2331 if (! intervals_equal (i1, i2))
2332 return 0;
2334 /* Advance POS till the end of the shorter interval,
2335 and advance one or both interval pointers for the new position. */
2336 pos += distance;
2337 if (len1 == distance)
2338 i1 = next_interval (i1);
2339 if (len2 == distance)
2340 i2 = next_interval (i2);
2342 return 1;
2345 /* Recursively adjust interval I in the current buffer
2346 for setting enable_multibyte_characters to MULTI_FLAG.
2347 The range of interval I is START ... END in characters,
2348 START_BYTE ... END_BYTE in bytes. */
2350 static void
2351 set_intervals_multibyte_1 (INTERVAL i, bool multi_flag,
2352 ptrdiff_t start, ptrdiff_t start_byte,
2353 ptrdiff_t end, ptrdiff_t end_byte)
2355 /* Fix the length of this interval. */
2356 if (multi_flag)
2357 i->total_length = end - start;
2358 else
2359 i->total_length = end_byte - start_byte;
2360 eassert (TOTAL_LENGTH (i) >= 0);
2362 if (TOTAL_LENGTH (i) == 0)
2364 delete_interval (i);
2365 return;
2368 /* Recursively fix the length of the subintervals. */
2369 if (i->left)
2371 ptrdiff_t left_end, left_end_byte;
2373 if (multi_flag)
2375 ptrdiff_t temp;
2376 left_end_byte = start_byte + LEFT_TOTAL_LENGTH (i);
2377 left_end = BYTE_TO_CHAR (left_end_byte);
2379 temp = CHAR_TO_BYTE (left_end);
2381 /* If LEFT_END_BYTE is in the middle of a character,
2382 adjust it and LEFT_END to a char boundary. */
2383 if (left_end_byte > temp)
2385 left_end_byte = temp;
2387 if (left_end_byte < temp)
2389 left_end--;
2390 left_end_byte = CHAR_TO_BYTE (left_end);
2393 else
2395 left_end = start + LEFT_TOTAL_LENGTH (i);
2396 left_end_byte = CHAR_TO_BYTE (left_end);
2399 set_intervals_multibyte_1 (i->left, multi_flag, start, start_byte,
2400 left_end, left_end_byte);
2402 if (i->right)
2404 ptrdiff_t right_start_byte, right_start;
2406 if (multi_flag)
2408 ptrdiff_t temp;
2410 right_start_byte = end_byte - RIGHT_TOTAL_LENGTH (i);
2411 right_start = BYTE_TO_CHAR (right_start_byte);
2413 /* If RIGHT_START_BYTE is in the middle of a character,
2414 adjust it and RIGHT_START to a char boundary. */
2415 temp = CHAR_TO_BYTE (right_start);
2417 if (right_start_byte < temp)
2419 right_start_byte = temp;
2421 if (right_start_byte > temp)
2423 right_start++;
2424 right_start_byte = CHAR_TO_BYTE (right_start);
2427 else
2429 right_start = end - RIGHT_TOTAL_LENGTH (i);
2430 right_start_byte = CHAR_TO_BYTE (right_start);
2433 set_intervals_multibyte_1 (i->right, multi_flag,
2434 right_start, right_start_byte,
2435 end, end_byte);
2438 /* Rounding to char boundaries can theoretically ake this interval
2439 spurious. If so, delete one child, and copy its property list
2440 to this interval. */
2441 if (LEFT_TOTAL_LENGTH (i) + RIGHT_TOTAL_LENGTH (i) >= TOTAL_LENGTH (i))
2443 if ((i)->left)
2445 set_interval_plist (i, i->left->plist);
2446 (i)->left->total_length = 0;
2447 delete_interval ((i)->left);
2449 else
2451 set_interval_plist (i, i->right->plist);
2452 (i)->right->total_length = 0;
2453 delete_interval ((i)->right);
2458 /* Update the intervals of the current buffer
2459 to fit the contents as multibyte (if MULTI_FLAG)
2460 or to fit them as non-multibyte (if not MULTI_FLAG). */
2462 void
2463 set_intervals_multibyte (bool multi_flag)
2465 INTERVAL i = buffer_intervals (current_buffer);
2467 if (i)
2468 set_intervals_multibyte_1 (i, multi_flag, BEG, BEG_BYTE, Z, Z_BYTE);