1 /* Code for doing intervals.
2 Copyright (C) 1993-1995, 1997-1998, 2001-2013 Free Software
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/>. */
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
43 #define INTERVALS_INLINE EXTERN_INLINE
47 #include "intervals.h"
48 #include "character.h"
54 /* Test for membership, allowing for t (actually any non-cons) to mean the
57 #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set))
59 static Lisp_Object
merge_properties_sticky (Lisp_Object
, Lisp_Object
);
60 static INTERVAL
merge_interval_right (INTERVAL
);
61 static INTERVAL
reproduce_tree (INTERVAL
, INTERVAL
);
63 /* Utility functions for intervals. */
65 /* Use these functions to set Lisp_Object
66 or pointer slots of struct interval. */
69 set_interval_object (INTERVAL i
, Lisp_Object obj
)
71 eassert (BUFFERP (obj
) || STRINGP (obj
));
77 set_interval_left (INTERVAL i
, INTERVAL left
)
83 set_interval_right (INTERVAL i
, INTERVAL right
)
88 /* Make the parent of D be whatever the parent of S is, regardless
89 of the type. This is used when balancing an interval tree. */
92 copy_interval_parent (INTERVAL d
, INTERVAL s
)
95 d
->up_obj
= s
->up_obj
;
98 /* Create the root interval of some object, a buffer or string. */
101 create_root_interval (Lisp_Object parent
)
105 CHECK_IMPURE (parent
);
107 new = make_interval ();
109 if (BUFFERP (parent
))
111 new->total_length
= (BUF_Z (XBUFFER (parent
))
112 - BUF_BEG (XBUFFER (parent
)));
113 eassert (0 <= TOTAL_LENGTH (new));
114 set_buffer_intervals (XBUFFER (parent
), new);
117 else if (STRINGP (parent
))
119 new->total_length
= SCHARS (parent
);
120 eassert (0 <= TOTAL_LENGTH (new));
121 set_string_intervals (parent
, new);
125 set_interval_object (new, parent
);
130 /* Make the interval TARGET have exactly the properties of SOURCE */
133 copy_properties (register INTERVAL source
, register INTERVAL target
)
135 if (DEFAULT_INTERVAL_P (source
) && DEFAULT_INTERVAL_P (target
))
138 COPY_INTERVAL_CACHE (source
, target
);
139 set_interval_plist (target
, Fcopy_sequence (source
->plist
));
142 /* Merge the properties of interval SOURCE into the properties
143 of interval TARGET. That is to say, each property in SOURCE
144 is added to TARGET if TARGET has no such property as yet. */
147 merge_properties (register INTERVAL source
, register INTERVAL target
)
149 register Lisp_Object o
, sym
, val
;
151 if (DEFAULT_INTERVAL_P (source
) && DEFAULT_INTERVAL_P (target
))
154 MERGE_INTERVAL_CACHE (source
, target
);
164 while (CONSP (val
) && !EQ (XCAR (val
), sym
))
175 set_interval_plist (target
, Fcons (sym
, Fcons (val
, target
->plist
)));
181 /* Return true if the two intervals have the same properties. */
184 intervals_equal (INTERVAL i0
, INTERVAL i1
)
186 Lisp_Object i0_cdr
, i0_sym
;
187 Lisp_Object i1_cdr
, i1_val
;
189 if (DEFAULT_INTERVAL_P (i0
) && DEFAULT_INTERVAL_P (i1
))
192 if (DEFAULT_INTERVAL_P (i0
) || DEFAULT_INTERVAL_P (i1
))
197 while (CONSP (i0_cdr
) && CONSP (i1_cdr
))
199 i0_sym
= XCAR (i0_cdr
);
200 i0_cdr
= XCDR (i0_cdr
);
204 while (CONSP (i1_val
) && !EQ (XCAR (i1_val
), i0_sym
))
206 i1_val
= XCDR (i1_val
);
209 i1_val
= XCDR (i1_val
);
212 /* i0 has something i1 doesn't. */
213 if (EQ (i1_val
, Qnil
))
216 /* i0 and i1 both have sym, but it has different values in each. */
218 || (i1_val
= XCDR (i1_val
), !CONSP (i1_val
))
219 || !EQ (XCAR (i1_val
), XCAR (i0_cdr
)))
222 i0_cdr
= XCDR (i0_cdr
);
224 i1_cdr
= XCDR (i1_cdr
);
227 i1_cdr
= XCDR (i1_cdr
);
230 /* Lengths of the two plists were equal. */
231 return (NILP (i0_cdr
) && NILP (i1_cdr
));
235 /* Traverse an interval tree TREE, performing FUNCTION on each node.
236 No guarantee is made about the order of traversal.
237 Pass FUNCTION two args: an interval, and ARG. */
240 traverse_intervals_noorder (INTERVAL tree
, void (*function
) (INTERVAL
, Lisp_Object
), Lisp_Object arg
)
242 /* Minimize stack usage. */
245 (*function
) (tree
, arg
);
250 traverse_intervals_noorder (tree
->left
, function
, arg
);
256 /* Traverse an interval tree TREE, performing FUNCTION on each node.
257 Pass FUNCTION two args: an interval, and ARG. */
260 traverse_intervals (INTERVAL tree
, ptrdiff_t position
,
261 void (*function
) (INTERVAL
, Lisp_Object
), Lisp_Object arg
)
265 traverse_intervals (tree
->left
, position
, function
, arg
);
266 position
+= LEFT_TOTAL_LENGTH (tree
);
267 tree
->position
= position
;
268 (*function
) (tree
, arg
);
269 position
+= LENGTH (tree
); tree
= tree
->right
;
277 static int zero_length
;
279 /* These functions are temporary, for debugging purposes only. */
281 INTERVAL search_interval
, found_interval
;
284 check_for_interval (INTERVAL i
)
286 if (i
== search_interval
)
294 search_for_interval (INTERVAL i
, INTERVAL tree
)
298 found_interval
= NULL
;
299 traverse_intervals_noorder (tree
, &check_for_interval
, Qnil
);
300 return found_interval
;
304 inc_interval_count (INTERVAL i
)
314 count_intervals (INTERVAL i
)
319 traverse_intervals_noorder (i
, &inc_interval_count
, Qnil
);
325 root_interval (INTERVAL interval
)
327 register INTERVAL i
= interval
;
329 while (! ROOT_INTERVAL_P (i
))
330 i
= INTERVAL_PARENT (i
);
336 /* Assuming that a left child exists, perform the following operation:
346 rotate_right (INTERVAL interval
)
349 INTERVAL B
= interval
->left
;
350 ptrdiff_t old_total
= interval
->total_length
;
352 /* Deal with any Parent of A; make it point to B. */
353 if (! ROOT_INTERVAL_P (interval
))
355 if (AM_LEFT_CHILD (interval
))
356 set_interval_left (INTERVAL_PARENT (interval
), B
);
358 set_interval_right (INTERVAL_PARENT (interval
), B
);
360 copy_interval_parent (B
, interval
);
362 /* Make B the parent of A */
364 set_interval_right (B
, interval
);
365 set_interval_parent (interval
, B
);
367 /* Make A point to c */
368 set_interval_left (interval
, i
);
370 set_interval_parent (i
, interval
);
372 /* A's total length is decreased by the length of B and its left child. */
373 interval
->total_length
-= B
->total_length
- LEFT_TOTAL_LENGTH (interval
);
374 eassert (0 <= TOTAL_LENGTH (interval
));
376 /* B must have the same total length of A. */
377 B
->total_length
= old_total
;
378 eassert (0 <= TOTAL_LENGTH (B
));
383 /* Assuming that a right child exists, perform the following operation:
393 rotate_left (INTERVAL interval
)
396 INTERVAL B
= interval
->right
;
397 ptrdiff_t old_total
= interval
->total_length
;
399 /* Deal with any parent of A; make it point to B. */
400 if (! ROOT_INTERVAL_P (interval
))
402 if (AM_LEFT_CHILD (interval
))
403 set_interval_left (INTERVAL_PARENT (interval
), B
);
405 set_interval_right (INTERVAL_PARENT (interval
), B
);
407 copy_interval_parent (B
, interval
);
409 /* Make B the parent of A */
411 set_interval_left (B
, interval
);
412 set_interval_parent (interval
, B
);
414 /* Make A point to c */
415 set_interval_right (interval
, i
);
417 set_interval_parent (i
, interval
);
419 /* A's total length is decreased by the length of B and its right child. */
420 interval
->total_length
-= B
->total_length
- RIGHT_TOTAL_LENGTH (interval
);
421 eassert (0 <= TOTAL_LENGTH (interval
));
423 /* B must have the same total length of A. */
424 B
->total_length
= old_total
;
425 eassert (0 <= TOTAL_LENGTH (B
));
430 /* Balance an interval tree with the assumption that the subtrees
431 themselves are already balanced. */
434 balance_an_interval (INTERVAL i
)
436 register ptrdiff_t old_diff
, new_diff
;
440 old_diff
= LEFT_TOTAL_LENGTH (i
) - RIGHT_TOTAL_LENGTH (i
);
443 /* Since the left child is longer, there must be one. */
444 new_diff
= i
->total_length
- i
->left
->total_length
445 + RIGHT_TOTAL_LENGTH (i
->left
) - LEFT_TOTAL_LENGTH (i
->left
);
446 if (eabs (new_diff
) >= old_diff
)
448 i
= rotate_right (i
);
449 balance_an_interval (i
->right
);
451 else if (old_diff
< 0)
453 /* Since the right child is longer, there must be one. */
454 new_diff
= i
->total_length
- i
->right
->total_length
455 + LEFT_TOTAL_LENGTH (i
->right
) - RIGHT_TOTAL_LENGTH (i
->right
);
456 if (eabs (new_diff
) >= -old_diff
)
459 balance_an_interval (i
->left
);
467 /* Balance INTERVAL, potentially stuffing it back into its parent
471 balance_possible_root_interval (INTERVAL interval
)
474 bool have_parent
= 0;
476 if (!INTERVAL_HAS_OBJECT (interval
) && !INTERVAL_HAS_PARENT (interval
))
479 if (INTERVAL_HAS_OBJECT (interval
))
482 GET_INTERVAL_OBJECT (parent
, interval
);
484 interval
= balance_an_interval (interval
);
488 if (BUFFERP (parent
))
489 set_buffer_intervals (XBUFFER (parent
), interval
);
490 else if (STRINGP (parent
))
491 set_string_intervals (parent
, interval
);
497 /* Balance the interval tree TREE. Balancing is by weight
498 (the amount of text). */
501 balance_intervals_internal (register INTERVAL tree
)
503 /* Balance within each side. */
505 balance_intervals_internal (tree
->left
);
507 balance_intervals_internal (tree
->right
);
508 return balance_an_interval (tree
);
511 /* Advertised interface to balance intervals. */
514 balance_intervals (INTERVAL tree
)
516 return tree
? balance_intervals_internal (tree
) : NULL
;
519 /* Rebalance text properties of B. */
522 buffer_balance_intervals (struct buffer
*b
)
527 i
= buffer_intervals (b
);
529 set_buffer_intervals (b
, balance_an_interval (i
));
532 /* Split INTERVAL into two pieces, starting the second piece at
533 character position OFFSET (counting from 0), relative to INTERVAL.
534 INTERVAL becomes the left-hand piece, and the right-hand piece
535 (second, lexicographically) is returned.
537 The size and position fields of the two intervals are set based upon
538 those of the original interval. The property list of the new interval
539 is reset, thus it is up to the caller to do the right thing with the
542 Note that this does not change the position of INTERVAL; if it is a root,
543 it is still a root after this operation. */
546 split_interval_right (INTERVAL interval
, ptrdiff_t offset
)
548 INTERVAL
new = make_interval ();
549 ptrdiff_t position
= interval
->position
;
550 ptrdiff_t new_length
= LENGTH (interval
) - offset
;
552 new->position
= position
+ offset
;
553 set_interval_parent (new, interval
);
555 if (NULL_RIGHT_CHILD (interval
))
557 set_interval_right (interval
, new);
558 new->total_length
= new_length
;
559 eassert (0 <= TOTAL_LENGTH (new));
563 /* Insert the new node between INTERVAL and its right child. */
564 set_interval_right (new, interval
->right
);
565 set_interval_parent (interval
->right
, new);
566 set_interval_right (interval
, new);
567 new->total_length
= new_length
+ new->right
->total_length
;
568 eassert (0 <= TOTAL_LENGTH (new));
569 balance_an_interval (new);
572 balance_possible_root_interval (interval
);
577 /* Split INTERVAL into two pieces, starting the second piece at
578 character position OFFSET (counting from 0), relative to INTERVAL.
579 INTERVAL becomes the right-hand piece, and the left-hand piece
580 (first, lexicographically) is returned.
582 The size and position fields of the two intervals are set based upon
583 those of the original interval. The property list of the new interval
584 is reset, thus it is up to the caller to do the right thing with the
587 Note that this does not change the position of INTERVAL; if it is a root,
588 it is still a root after this operation. */
591 split_interval_left (INTERVAL interval
, ptrdiff_t offset
)
593 INTERVAL
new = make_interval ();
594 ptrdiff_t new_length
= offset
;
596 new->position
= interval
->position
;
597 interval
->position
= interval
->position
+ offset
;
598 set_interval_parent (new, interval
);
600 if (NULL_LEFT_CHILD (interval
))
602 set_interval_left (interval
, new);
603 new->total_length
= new_length
;
604 eassert (0 <= TOTAL_LENGTH (new));
608 /* Insert the new node between INTERVAL and its left child. */
609 set_interval_left (new, interval
->left
);
610 set_interval_parent (new->left
, new);
611 set_interval_left (interval
, new);
612 new->total_length
= new_length
+ new->left
->total_length
;
613 eassert (0 <= TOTAL_LENGTH (new));
614 balance_an_interval (new);
617 balance_possible_root_interval (interval
);
622 /* Return the proper position for the first character
623 described by the interval tree SOURCE.
624 This is 1 if the parent is a buffer,
625 0 if the parent is a string or if there is no parent.
627 Don't use this function on an interval which is the child
628 of another interval! */
631 interval_start_pos (INTERVAL source
)
638 if (! INTERVAL_HAS_OBJECT (source
))
640 GET_INTERVAL_OBJECT (parent
, source
);
641 if (BUFFERP (parent
))
642 return BUF_BEG (XBUFFER (parent
));
646 /* Find the interval containing text position POSITION in the text
647 represented by the interval tree TREE. POSITION is a buffer
648 position (starting from 1) or a string index (starting from 0).
649 If POSITION is at the end of the buffer or string,
650 return the interval containing the last character.
652 The `position' field, which is a cache of an interval's position,
653 is updated in the interval found. Other functions (e.g., next_interval)
654 will update this cache based on the result of find_interval. */
657 find_interval (register INTERVAL tree
, register ptrdiff_t position
)
659 /* The distance from the left edge of the subtree at TREE
661 register ptrdiff_t relative_position
;
666 relative_position
= position
;
667 if (INTERVAL_HAS_OBJECT (tree
))
670 GET_INTERVAL_OBJECT (parent
, tree
);
671 if (BUFFERP (parent
))
672 relative_position
-= BUF_BEG (XBUFFER (parent
));
675 eassert (relative_position
<= TOTAL_LENGTH (tree
));
677 tree
= balance_possible_root_interval (tree
);
681 if (relative_position
< LEFT_TOTAL_LENGTH (tree
))
685 else if (! NULL_RIGHT_CHILD (tree
)
686 && relative_position
>= (TOTAL_LENGTH (tree
)
687 - RIGHT_TOTAL_LENGTH (tree
)))
689 relative_position
-= (TOTAL_LENGTH (tree
)
690 - RIGHT_TOTAL_LENGTH (tree
));
696 = (position
- relative_position
/* left edge of *tree. */
697 + LEFT_TOTAL_LENGTH (tree
)); /* left edge of this interval. */
704 /* Find the succeeding interval (lexicographically) to INTERVAL.
705 Sets the `position' field based on that of INTERVAL (see
709 next_interval (register INTERVAL interval
)
711 register INTERVAL i
= interval
;
712 register ptrdiff_t next_position
;
716 next_position
= interval
->position
+ LENGTH (interval
);
718 if (! NULL_RIGHT_CHILD (i
))
721 while (! NULL_LEFT_CHILD (i
))
724 i
->position
= next_position
;
728 while (! NULL_PARENT (i
))
730 if (AM_LEFT_CHILD (i
))
732 i
= INTERVAL_PARENT (i
);
733 i
->position
= next_position
;
737 i
= INTERVAL_PARENT (i
);
743 /* Find the preceding interval (lexicographically) to INTERVAL.
744 Sets the `position' field based on that of INTERVAL (see
748 previous_interval (register INTERVAL interval
)
755 if (! NULL_LEFT_CHILD (interval
))
758 while (! NULL_RIGHT_CHILD (i
))
761 i
->position
= interval
->position
- LENGTH (i
);
766 while (! NULL_PARENT (i
))
768 if (AM_RIGHT_CHILD (i
))
770 i
= INTERVAL_PARENT (i
);
772 i
->position
= interval
->position
- LENGTH (i
);
775 i
= INTERVAL_PARENT (i
);
781 /* Find the interval containing POS given some non-NULL INTERVAL
782 in the same tree. Note that we need to update interval->position
783 if we go down the tree.
784 To speed up the process, we assume that the ->position of
785 I and all its parents is already uptodate. */
787 update_interval (register INTERVAL i
, ptrdiff_t pos
)
794 if (pos
< i
->position
)
797 if (pos
>= i
->position
- TOTAL_LENGTH (i
->left
))
799 i
->left
->position
= i
->position
- TOTAL_LENGTH (i
->left
)
800 + LEFT_TOTAL_LENGTH (i
->left
);
801 i
= i
->left
; /* Move to the left child */
803 else if (NULL_PARENT (i
))
804 error ("Point before start of properties");
806 i
= INTERVAL_PARENT (i
);
809 else if (pos
>= INTERVAL_LAST_POS (i
))
812 if (pos
< INTERVAL_LAST_POS (i
) + TOTAL_LENGTH (i
->right
))
814 i
->right
->position
= INTERVAL_LAST_POS (i
)
815 + LEFT_TOTAL_LENGTH (i
->right
);
816 i
= i
->right
; /* Move to the right child */
818 else if (NULL_PARENT (i
))
819 error ("Point %"pD
"d after end of properties", pos
);
821 i
= INTERVAL_PARENT (i
);
829 /* Effect an adjustment corresponding to the addition of LENGTH characters
830 of text. Do this by finding the interval containing POSITION in the
831 interval tree TREE, and then adjusting all of its ancestors by adding
834 If POSITION is the first character of an interval, meaning that point
835 is actually between the two intervals, make the new text belong to
836 the interval which is "sticky".
838 If both intervals are "sticky", then make them belong to the left-most
839 interval. Another possibility would be to create a new interval for
840 this text, and make it have the merged properties of both ends. */
843 adjust_intervals_for_insertion (INTERVAL tree
,
844 ptrdiff_t position
, ptrdiff_t length
)
852 eassert (TOTAL_LENGTH (tree
) > 0);
854 GET_INTERVAL_OBJECT (parent
, tree
);
855 offset
= (BUFFERP (parent
) ? BUF_BEG (XBUFFER (parent
)) : 0);
857 /* If inserting at point-max of a buffer, that position will be out
858 of range. Remember that buffer positions are 1-based. */
859 if (position
>= TOTAL_LENGTH (tree
) + offset
)
861 position
= TOTAL_LENGTH (tree
) + offset
;
865 i
= find_interval (tree
, position
);
867 /* If in middle of an interval which is not sticky either way,
868 we must not just give its properties to the insertion.
869 So split this interval at the insertion point.
871 Originally, the if condition here was this:
872 (! (position == i->position || eobp)
873 && END_NONSTICKY_P (i)
874 && FRONT_NONSTICKY_P (i))
875 But, these macros are now unreliable because of introduction of
876 Vtext_property_default_nonsticky. So, we always check properties
877 one by one if POSITION is in middle of an interval. */
878 if (! (position
== i
->position
|| eobp
))
881 Lisp_Object front
, rear
;
885 /* Properties font-sticky and rear-nonsticky override
886 Vtext_property_default_nonsticky. So, if they are t, we can
887 skip one by one checking of properties. */
888 rear
= textget (i
->plist
, Qrear_nonsticky
);
889 if (! CONSP (rear
) && ! NILP (rear
))
891 /* All properties are nonsticky. We split the interval. */
894 front
= textget (i
->plist
, Qfront_sticky
);
895 if (! CONSP (front
) && ! NILP (front
))
897 /* All properties are sticky. We don't split the interval. */
902 /* Does any actual property pose an actual problem? We break
903 the loop if we find a nonsticky property. */
904 for (; CONSP (tail
); tail
= Fcdr (XCDR (tail
)))
906 Lisp_Object prop
, tmp
;
909 /* Is this particular property front-sticky? */
910 if (CONSP (front
) && ! NILP (Fmemq (prop
, front
)))
913 /* Is this particular property rear-nonsticky? */
914 if (CONSP (rear
) && ! NILP (Fmemq (prop
, rear
)))
917 /* Is this particular property recorded as sticky or
918 nonsticky in Vtext_property_default_nonsticky? */
919 tmp
= Fassq (prop
, Vtext_property_default_nonsticky
);
927 /* By default, a text property is rear-sticky, thus we
928 continue the loop. */
932 /* If any property is a real problem, split the interval. */
935 temp
= split_interval_right (i
, position
- i
->position
);
936 copy_properties (i
, temp
);
941 /* If we are positioned between intervals, check the stickiness of
942 both of them. We have to do this too, if we are at BEG or Z. */
943 if (position
== i
->position
|| eobp
)
945 register INTERVAL prev
;
955 prev
= previous_interval (i
);
957 /* Even if we are positioned between intervals, we default
958 to the left one if it exists. We extend it now and split
959 off a part later, if stickiness demands it. */
960 for (temp
= prev
? prev
: i
; temp
; temp
= INTERVAL_PARENT_OR_NULL (temp
))
962 temp
->total_length
+= length
;
963 eassert (0 <= TOTAL_LENGTH (temp
));
964 temp
= balance_possible_root_interval (temp
);
967 /* If at least one interval has sticky properties,
968 we check the stickiness property by property.
970 Originally, the if condition here was this:
971 (END_NONSTICKY_P (prev) || FRONT_STICKY_P (i))
972 But, these macros are now unreliable because of introduction
973 of Vtext_property_default_nonsticky. So, we always have to
974 check stickiness of properties one by one. If cache of
975 stickiness is implemented in the future, we may be able to
976 use those macros again. */
979 Lisp_Object pleft
, pright
;
980 struct interval newi
;
982 RESET_INTERVAL (&newi
);
983 pleft
= prev
? prev
->plist
: Qnil
;
984 pright
= i
? i
->plist
: Qnil
;
985 set_interval_plist (&newi
, merge_properties_sticky (pleft
, pright
));
987 if (! prev
) /* i.e. position == BEG */
989 if (! intervals_equal (i
, &newi
))
991 i
= split_interval_left (i
, length
);
992 set_interval_plist (i
, newi
.plist
);
995 else if (! intervals_equal (prev
, &newi
))
997 prev
= split_interval_right (prev
, position
- prev
->position
);
998 set_interval_plist (prev
, newi
.plist
);
999 if (i
&& intervals_equal (prev
, i
))
1000 merge_interval_right (prev
);
1003 /* We will need to update the cache here later. */
1005 else if (! prev
&& ! NILP (i
->plist
))
1007 /* Just split off a new interval at the left.
1008 Since I wasn't front-sticky, the empty plist is ok. */
1009 i
= split_interval_left (i
, length
);
1013 /* Otherwise just extend the interval. */
1016 for (temp
= i
; temp
; temp
= INTERVAL_PARENT_OR_NULL (temp
))
1018 temp
->total_length
+= length
;
1019 eassert (0 <= TOTAL_LENGTH (temp
));
1020 temp
= balance_possible_root_interval (temp
);
1027 /* Any property might be front-sticky on the left, rear-sticky on the left,
1028 front-sticky on the right, or rear-sticky on the right; the 16 combinations
1029 can be arranged in a matrix with rows denoting the left conditions and
1030 columns denoting the right conditions:
1038 left-props = '(front-sticky (p8 p9 pa pb pc pd pe pf)
1039 rear-nonsticky (p4 p5 p6 p7 p8 p9 pa pb)
1040 p0 L p1 L p2 L p3 L p4 L p5 L p6 L p7 L
1041 p8 L p9 L pa L pb L pc L pd L pe L pf L)
1042 right-props = '(front-sticky (p2 p3 p6 p7 pa pb pe pf)
1043 rear-nonsticky (p1 p2 p5 p6 p9 pa pd pe)
1044 p0 R p1 R p2 R p3 R p4 R p5 R p6 R p7 R
1045 p8 R p9 R pa R pb R pc R pd R pe R pf R)
1047 We inherit from whoever has a sticky side facing us. If both sides
1048 do (cases 2, 3, E, and F), then we inherit from whichever side has a
1049 non-nil value for the current property. If both sides do, then we take
1052 When we inherit a property, we get its stickiness as well as its value.
1053 So, when we merge the above two lists, we expect to get this:
1055 result = '(front-sticky (p6 p7 pa pb pc pd pe pf)
1056 rear-nonsticky (p6 pa)
1057 p0 L p1 L p2 L p3 L p6 R p7 R
1058 pa R pb R pc L pd L pe L pf L)
1060 The optimizable special cases are:
1061 left rear-nonsticky = nil, right front-sticky = nil (inherit left)
1062 left rear-nonsticky = t, right front-sticky = t (inherit right)
1063 left rear-nonsticky = t, right front-sticky = nil (inherit none)
1067 merge_properties_sticky (Lisp_Object pleft
, Lisp_Object pright
)
1069 Lisp_Object props
, front
, rear
;
1070 Lisp_Object lfront
, lrear
, rfront
, rrear
;
1071 Lisp_Object tail1
, tail2
, sym
, lval
, rval
, cat
;
1072 bool use_left
, use_right
, lpresent
;
1077 lfront
= textget (pleft
, Qfront_sticky
);
1078 lrear
= textget (pleft
, Qrear_nonsticky
);
1079 rfront
= textget (pright
, Qfront_sticky
);
1080 rrear
= textget (pright
, Qrear_nonsticky
);
1082 /* Go through each element of PRIGHT. */
1083 for (tail1
= pright
; CONSP (tail1
); tail1
= Fcdr (XCDR (tail1
)))
1089 /* Sticky properties get special treatment. */
1090 if (EQ (sym
, Qrear_nonsticky
) || EQ (sym
, Qfront_sticky
))
1093 rval
= Fcar (XCDR (tail1
));
1094 for (tail2
= pleft
; CONSP (tail2
); tail2
= Fcdr (XCDR (tail2
)))
1095 if (EQ (sym
, XCAR (tail2
)))
1098 /* Indicate whether the property is explicitly defined on the left.
1099 (We know it is defined explicitly on the right
1100 because otherwise we don't get here.) */
1101 lpresent
= ! NILP (tail2
);
1102 lval
= (NILP (tail2
) ? Qnil
: Fcar (Fcdr (tail2
)));
1104 /* Even if lrear or rfront say nothing about the stickiness of
1105 SYM, Vtext_property_default_nonsticky may give default
1106 stickiness to SYM. */
1107 tmp
= Fassq (sym
, Vtext_property_default_nonsticky
);
1108 use_left
= (lpresent
1109 && ! (TMEM (sym
, lrear
)
1110 || (CONSP (tmp
) && ! NILP (XCDR (tmp
)))));
1111 use_right
= (TMEM (sym
, rfront
)
1112 || (CONSP (tmp
) && NILP (XCDR (tmp
))));
1113 if (use_left
&& use_right
)
1117 else if (NILP (rval
))
1122 /* We build props as (value sym ...) rather than (sym value ...)
1123 because we plan to nreverse it when we're done. */
1124 props
= Fcons (lval
, Fcons (sym
, props
));
1125 if (TMEM (sym
, lfront
))
1126 front
= Fcons (sym
, front
);
1127 if (TMEM (sym
, lrear
))
1128 rear
= Fcons (sym
, rear
);
1132 props
= Fcons (rval
, Fcons (sym
, props
));
1133 if (TMEM (sym
, rfront
))
1134 front
= Fcons (sym
, front
);
1135 if (TMEM (sym
, rrear
))
1136 rear
= Fcons (sym
, rear
);
1140 /* Now go through each element of PLEFT. */
1141 for (tail2
= pleft
; CONSP (tail2
); tail2
= Fcdr (XCDR (tail2
)))
1147 /* Sticky properties get special treatment. */
1148 if (EQ (sym
, Qrear_nonsticky
) || EQ (sym
, Qfront_sticky
))
1151 /* If sym is in PRIGHT, we've already considered it. */
1152 for (tail1
= pright
; CONSP (tail1
); tail1
= Fcdr (XCDR (tail1
)))
1153 if (EQ (sym
, XCAR (tail1
)))
1158 lval
= Fcar (XCDR (tail2
));
1160 /* Even if lrear or rfront say nothing about the stickiness of
1161 SYM, Vtext_property_default_nonsticky may give default
1162 stickiness to SYM. */
1163 tmp
= Fassq (sym
, Vtext_property_default_nonsticky
);
1165 /* Since rval is known to be nil in this loop, the test simplifies. */
1166 if (! (TMEM (sym
, lrear
) || (CONSP (tmp
) && ! NILP (XCDR (tmp
)))))
1168 props
= Fcons (lval
, Fcons (sym
, props
));
1169 if (TMEM (sym
, lfront
))
1170 front
= Fcons (sym
, front
);
1172 else if (TMEM (sym
, rfront
) || (CONSP (tmp
) && NILP (XCDR (tmp
))))
1174 /* The value is nil, but we still inherit the stickiness
1176 front
= Fcons (sym
, front
);
1177 if (TMEM (sym
, rrear
))
1178 rear
= Fcons (sym
, rear
);
1181 props
= Fnreverse (props
);
1183 props
= Fcons (Qrear_nonsticky
, Fcons (Fnreverse (rear
), props
));
1185 cat
= textget (props
, Qcategory
);
1188 /* If we have inherited a front-stick category property that is t,
1189 we don't need to set up a detailed one. */
1190 ! (! NILP (cat
) && SYMBOLP (cat
)
1191 && EQ (Fget (cat
, Qfront_sticky
), Qt
)))
1192 props
= Fcons (Qfront_sticky
, Fcons (Fnreverse (front
), props
));
1197 /* Delete a node I from its interval tree by merging its subtrees
1198 into one subtree which is then returned. Caller is responsible for
1199 storing the resulting subtree into its parent. */
1202 delete_node (register INTERVAL i
)
1204 register INTERVAL migrate
, this;
1205 register ptrdiff_t migrate_amt
;
1213 migrate_amt
= i
->left
->total_length
;
1215 this->total_length
+= migrate_amt
;
1219 this->total_length
+= migrate_amt
;
1221 eassert (0 <= TOTAL_LENGTH (this));
1222 set_interval_left (this, migrate
);
1223 set_interval_parent (migrate
, this);
1228 /* Delete interval I from its tree by calling `delete_node'
1229 and properly connecting the resultant subtree.
1231 I is presumed to be empty; that is, no adjustments are made
1232 for the length of I. */
1235 delete_interval (register INTERVAL i
)
1237 register INTERVAL parent
;
1238 ptrdiff_t amt
= LENGTH (i
);
1240 eassert (amt
== 0); /* Only used on zero-length intervals now. */
1242 if (ROOT_INTERVAL_P (i
))
1245 GET_INTERVAL_OBJECT (owner
, i
);
1246 parent
= delete_node (i
);
1248 set_interval_object (parent
, owner
);
1250 if (BUFFERP (owner
))
1251 set_buffer_intervals (XBUFFER (owner
), parent
);
1252 else if (STRINGP (owner
))
1253 set_string_intervals (owner
, parent
);
1260 parent
= INTERVAL_PARENT (i
);
1261 if (AM_LEFT_CHILD (i
))
1263 set_interval_left (parent
, delete_node (i
));
1265 set_interval_parent (parent
->left
, parent
);
1269 set_interval_right (parent
, delete_node (i
));
1271 set_interval_parent (parent
->right
, parent
);
1275 /* Find the interval in TREE corresponding to the relative position
1276 FROM and delete as much as possible of AMOUNT from that interval.
1277 Return the amount actually deleted, and if the interval was
1278 zeroed-out, delete that interval node from the tree.
1280 Note that FROM is actually origin zero, aka relative to the
1281 leftmost edge of tree. This is appropriate since we call ourselves
1282 recursively on subtrees.
1284 Do this by recursing down TREE to the interval in question, and
1285 deleting the appropriate amount of text. */
1288 interval_deletion_adjustment (register INTERVAL tree
, register ptrdiff_t from
,
1289 register ptrdiff_t amount
)
1291 register ptrdiff_t relative_position
= from
;
1297 if (relative_position
< LEFT_TOTAL_LENGTH (tree
))
1299 ptrdiff_t subtract
= interval_deletion_adjustment (tree
->left
,
1302 tree
->total_length
-= subtract
;
1303 eassert (0 <= TOTAL_LENGTH (tree
));
1307 else if (relative_position
>= (TOTAL_LENGTH (tree
)
1308 - RIGHT_TOTAL_LENGTH (tree
)))
1312 relative_position
-= (tree
->total_length
1313 - RIGHT_TOTAL_LENGTH (tree
));
1314 subtract
= interval_deletion_adjustment (tree
->right
,
1317 tree
->total_length
-= subtract
;
1318 eassert (0 <= TOTAL_LENGTH (tree
));
1321 /* Here -- this node. */
1324 /* How much can we delete from this interval? */
1325 ptrdiff_t my_amount
= ((tree
->total_length
1326 - RIGHT_TOTAL_LENGTH (tree
))
1327 - relative_position
);
1329 if (amount
> my_amount
)
1332 tree
->total_length
-= amount
;
1333 eassert (0 <= TOTAL_LENGTH (tree
));
1334 if (LENGTH (tree
) == 0)
1335 delete_interval (tree
);
1340 /* Never reach here. */
1343 /* Effect the adjustments necessary to the interval tree of BUFFER to
1344 correspond to the deletion of LENGTH characters from that buffer
1345 text. The deletion is effected at position START (which is a
1346 buffer position, i.e. origin 1). */
1349 adjust_intervals_for_deletion (struct buffer
*buffer
,
1350 ptrdiff_t start
, ptrdiff_t length
)
1352 ptrdiff_t left_to_delete
= length
;
1353 INTERVAL tree
= buffer_intervals (buffer
);
1357 GET_INTERVAL_OBJECT (parent
, tree
);
1358 offset
= (BUFFERP (parent
) ? BUF_BEG (XBUFFER (parent
)) : 0);
1363 eassert (start
<= offset
+ TOTAL_LENGTH (tree
)
1364 && start
+ length
<= offset
+ TOTAL_LENGTH (tree
));
1366 if (length
== TOTAL_LENGTH (tree
))
1368 set_buffer_intervals (buffer
, NULL
);
1372 if (ONLY_INTERVAL_P (tree
))
1374 tree
->total_length
-= length
;
1375 eassert (0 <= TOTAL_LENGTH (tree
));
1379 if (start
> offset
+ TOTAL_LENGTH (tree
))
1380 start
= offset
+ TOTAL_LENGTH (tree
);
1381 while (left_to_delete
> 0)
1383 left_to_delete
-= interval_deletion_adjustment (tree
, start
- offset
,
1385 tree
= buffer_intervals (buffer
);
1386 if (left_to_delete
== tree
->total_length
)
1388 set_buffer_intervals (buffer
, NULL
);
1394 /* Make the adjustments necessary to the interval tree of BUFFER to
1395 represent an addition or deletion of LENGTH characters starting
1396 at position START. Addition or deletion is indicated by the sign
1400 offset_intervals (struct buffer
*buffer
, ptrdiff_t start
, ptrdiff_t length
)
1402 if (!buffer_intervals (buffer
) || length
== 0)
1406 adjust_intervals_for_insertion (buffer_intervals (buffer
),
1410 lint_assume (- TYPE_MAXIMUM (ptrdiff_t) <= length
);
1411 adjust_intervals_for_deletion (buffer
, start
, -length
);
1415 /* Merge interval I with its lexicographic successor. The resulting
1416 interval is returned, and has the properties of the original
1417 successor. The properties of I are lost. I is removed from the
1421 The caller must verify that this is not the last (rightmost)
1425 merge_interval_right (register INTERVAL i
)
1427 register ptrdiff_t absorb
= LENGTH (i
);
1428 register INTERVAL successor
;
1430 /* Find the succeeding interval. */
1431 if (! NULL_RIGHT_CHILD (i
)) /* It's below us. Add absorb
1434 successor
= i
->right
;
1435 while (! NULL_LEFT_CHILD (successor
))
1437 successor
->total_length
+= absorb
;
1438 eassert (0 <= TOTAL_LENGTH (successor
));
1439 successor
= successor
->left
;
1442 successor
->total_length
+= absorb
;
1443 eassert (0 <= TOTAL_LENGTH (successor
));
1444 delete_interval (i
);
1448 /* Zero out this interval. */
1449 i
->total_length
-= absorb
;
1450 eassert (0 <= TOTAL_LENGTH (i
));
1453 while (! NULL_PARENT (successor
)) /* It's above us. Subtract as
1456 if (AM_LEFT_CHILD (successor
))
1458 successor
= INTERVAL_PARENT (successor
);
1459 delete_interval (i
);
1463 successor
= INTERVAL_PARENT (successor
);
1464 successor
->total_length
-= absorb
;
1465 eassert (0 <= TOTAL_LENGTH (successor
));
1468 /* This must be the rightmost or last interval and cannot
1469 be merged right. The caller should have known. */
1473 /* Merge interval I with its lexicographic predecessor. The resulting
1474 interval is returned, and has the properties of the original predecessor.
1475 The properties of I are lost. Interval node I is removed from the tree.
1478 The caller must verify that this is not the first (leftmost) interval. */
1481 merge_interval_left (register INTERVAL i
)
1483 register ptrdiff_t absorb
= LENGTH (i
);
1484 register INTERVAL predecessor
;
1486 /* Find the preceding interval. */
1487 if (! NULL_LEFT_CHILD (i
)) /* It's below us. Go down,
1488 adding ABSORB as we go. */
1490 predecessor
= i
->left
;
1491 while (! NULL_RIGHT_CHILD (predecessor
))
1493 predecessor
->total_length
+= absorb
;
1494 eassert (0 <= TOTAL_LENGTH (predecessor
));
1495 predecessor
= predecessor
->right
;
1498 predecessor
->total_length
+= absorb
;
1499 eassert (0 <= TOTAL_LENGTH (predecessor
));
1500 delete_interval (i
);
1504 /* Zero out this interval. */
1505 i
->total_length
-= absorb
;
1506 eassert (0 <= TOTAL_LENGTH (i
));
1509 while (! NULL_PARENT (predecessor
)) /* It's above us. Go up,
1510 subtracting ABSORB. */
1512 if (AM_RIGHT_CHILD (predecessor
))
1514 predecessor
= INTERVAL_PARENT (predecessor
);
1515 delete_interval (i
);
1519 predecessor
= INTERVAL_PARENT (predecessor
);
1520 predecessor
->total_length
-= absorb
;
1521 eassert (0 <= TOTAL_LENGTH (predecessor
));
1524 /* This must be the leftmost or first interval and cannot
1525 be merged left. The caller should have known. */
1529 /* Create a copy of SOURCE but with the default value of UP. */
1532 reproduce_interval (INTERVAL source
)
1534 register INTERVAL target
= make_interval ();
1536 target
->total_length
= source
->total_length
;
1537 target
->position
= source
->position
;
1539 copy_properties (source
, target
);
1541 if (! NULL_LEFT_CHILD (source
))
1542 set_interval_left (target
, reproduce_tree (source
->left
, target
));
1543 if (! NULL_RIGHT_CHILD (source
))
1544 set_interval_right (target
, reproduce_tree (source
->right
, target
));
1549 /* Make an exact copy of interval tree SOURCE which descends from
1550 PARENT. This is done by recursing through SOURCE, copying
1551 the current interval and its properties, and then adjusting
1552 the pointers of the copy. */
1555 reproduce_tree (INTERVAL source
, INTERVAL parent
)
1557 INTERVAL target
= reproduce_interval (source
);
1558 set_interval_parent (target
, parent
);
1563 reproduce_tree_obj (INTERVAL source
, Lisp_Object parent
)
1565 INTERVAL target
= reproduce_interval (source
);
1566 set_interval_object (target
, parent
);
1570 /* Insert the intervals of SOURCE into BUFFER at POSITION.
1571 LENGTH is the length of the text in SOURCE.
1573 The `position' field of the SOURCE intervals is assumed to be
1574 consistent with its parent; therefore, SOURCE must be an
1575 interval tree made with copy_interval or must be the whole
1576 tree of a buffer or a string.
1578 This is used in insdel.c when inserting Lisp_Strings into the
1579 buffer. The text corresponding to SOURCE is already in the buffer
1580 when this is called. The intervals of new tree are a copy of those
1581 belonging to the string being inserted; intervals are never
1584 If the inserted text had no intervals associated, and we don't
1585 want to inherit the surrounding text's properties, this function
1586 simply returns -- offset_intervals should handle placing the
1587 text in the correct interval, depending on the sticky bits.
1589 If the inserted text had properties (intervals), then there are two
1590 cases -- either insertion happened in the middle of some interval,
1591 or between two intervals.
1593 If the text goes into the middle of an interval, then new intervals
1594 are created in the middle, and new text has the union of its properties
1595 and those of the text into which it was inserted.
1597 If the text goes between two intervals, then if neither interval
1598 had its appropriate sticky property set (front_sticky, rear_sticky),
1599 the new text has only its properties. If one of the sticky properties
1600 is set, then the new text "sticks" to that region and its properties
1601 depend on merging as above. If both the preceding and succeeding
1602 intervals to the new text are "sticky", then the new text retains
1603 only its properties, as if neither sticky property were set. Perhaps
1604 we should consider merging all three sets of properties onto the new
1608 graft_intervals_into_buffer (INTERVAL source
, ptrdiff_t position
,
1609 ptrdiff_t length
, struct buffer
*buffer
,
1612 INTERVAL tree
= buffer_intervals (buffer
);
1613 INTERVAL under
, over
, this;
1614 ptrdiff_t over_used
;
1616 /* If the new text has no properties, then with inheritance it
1617 becomes part of whatever interval it was inserted into.
1618 To prevent inheritance, we must clear out the properties
1619 of the newly inserted text. */
1623 if (!inherit
&& tree
&& length
> 0)
1625 XSETBUFFER (buf
, buffer
);
1626 set_text_properties_1 (make_number (position
),
1627 make_number (position
+ length
),
1630 /* Shouldn't be necessary. --Stef */
1631 buffer_balance_intervals (buffer
);
1635 eassert (length
== TOTAL_LENGTH (source
));
1637 if ((BUF_Z (buffer
) - BUF_BEG (buffer
)) == length
)
1639 /* The inserted text constitutes the whole buffer, so
1640 simply copy over the interval structure. */
1643 XSETBUFFER (buf
, buffer
);
1644 set_buffer_intervals (buffer
, reproduce_tree_obj (source
, buf
));
1645 buffer_intervals (buffer
)->position
= BUF_BEG (buffer
);
1646 eassert (buffer_intervals (buffer
)->up_obj
== 1);
1651 /* Create an interval tree in which to place a copy
1652 of the intervals of the inserted string. */
1655 XSETBUFFER (buf
, buffer
);
1656 tree
= create_root_interval (buf
);
1658 /* Paranoia -- the text has already been added, so
1659 this buffer should be of non-zero length. */
1660 eassert (TOTAL_LENGTH (tree
) > 0);
1662 this = under
= find_interval (tree
, position
);
1664 over
= find_interval (source
, interval_start_pos (source
));
1666 /* Here for insertion in the middle of an interval.
1667 Split off an equivalent interval to the right,
1668 then don't bother with it any more. */
1670 if (position
> under
->position
)
1672 INTERVAL end_unchanged
1673 = split_interval_left (this, position
- under
->position
);
1674 copy_properties (under
, end_unchanged
);
1675 under
->position
= position
;
1679 /* This call may have some effect because previous_interval may
1680 update `position' fields of intervals. Thus, don't ignore it
1681 for the moment. Someone please tell me the truth (K.Handa). */
1682 INTERVAL prev
= previous_interval (under
);
1685 /* But, this code surely has no effect. And, anyway,
1686 END_NONSTICKY_P is unreliable now. */
1687 if (prev
&& !END_NONSTICKY_P (prev
))
1692 /* Insertion is now at beginning of UNDER. */
1694 /* The inserted text "sticks" to the interval `under',
1695 which means it gets those properties.
1696 The properties of under are the result of
1697 adjust_intervals_for_insertion, so stickiness has
1698 already been taken care of. */
1700 /* OVER is the interval we are copying from next.
1701 OVER_USED says how many characters' worth of OVER
1702 have already been copied into target intervals.
1703 UNDER is the next interval in the target. */
1707 /* If UNDER is longer than OVER, split it. */
1708 if (LENGTH (over
) - over_used
< LENGTH (under
))
1710 this = split_interval_left (under
, LENGTH (over
) - over_used
);
1711 copy_properties (under
, this);
1716 /* THIS is now the interval to copy or merge into.
1717 OVER covers all of it. */
1719 merge_properties (over
, this);
1721 copy_properties (over
, this);
1723 /* If THIS and OVER end at the same place,
1724 advance OVER to a new source interval. */
1725 if (LENGTH (this) == LENGTH (over
) - over_used
)
1727 over
= next_interval (over
);
1731 /* Otherwise just record that more of OVER has been used. */
1732 over_used
+= LENGTH (this);
1734 /* Always advance to a new target interval. */
1735 under
= next_interval (this);
1738 buffer_balance_intervals (buffer
);
1741 /* Get the value of property PROP from PLIST,
1742 which is the plist of an interval.
1743 We check for direct properties, for categories with property PROP,
1744 and for PROP appearing on the default-text-properties list. */
1747 textget (Lisp_Object plist
, register Lisp_Object prop
)
1749 return lookup_char_property (plist
, prop
, 1);
1753 lookup_char_property (Lisp_Object plist
, Lisp_Object prop
, bool textprop
)
1755 Lisp_Object tail
, fallback
= Qnil
;
1757 for (tail
= plist
; CONSP (tail
); tail
= Fcdr (XCDR (tail
)))
1759 register Lisp_Object tem
;
1762 return Fcar (XCDR (tail
));
1763 if (EQ (tem
, Qcategory
))
1765 tem
= Fcar (XCDR (tail
));
1767 fallback
= Fget (tem
, prop
);
1771 if (! NILP (fallback
))
1773 /* Check for alternative properties */
1774 tail
= Fassq (prop
, Vchar_property_alias_alist
);
1778 for (; NILP (fallback
) && CONSP (tail
); tail
= XCDR (tail
))
1779 fallback
= Fplist_get (plist
, XCAR (tail
));
1782 if (textprop
&& NILP (fallback
) && CONSP (Vdefault_text_properties
))
1783 fallback
= Fplist_get (Vdefault_text_properties
, prop
);
1788 /* Set point in BUFFER "temporarily" to CHARPOS, which corresponds to
1789 byte position BYTEPOS. */
1792 temp_set_point_both (struct buffer
*buffer
,
1793 ptrdiff_t charpos
, ptrdiff_t bytepos
)
1795 /* In a single-byte buffer, the two positions must be equal. */
1796 if (BUF_ZV (buffer
) == BUF_ZV_BYTE (buffer
))
1797 eassert (charpos
== bytepos
);
1799 eassert (charpos
<= bytepos
);
1800 eassert (charpos
<= BUF_ZV (buffer
) || BUF_BEGV (buffer
) <= charpos
);
1802 SET_BUF_PT_BOTH (buffer
, charpos
, bytepos
);
1805 /* Set point "temporarily", without checking any text properties. */
1808 temp_set_point (struct buffer
*buffer
, ptrdiff_t charpos
)
1810 temp_set_point_both (buffer
, charpos
,
1811 buf_charpos_to_bytepos (buffer
, charpos
));
1814 /* Set point in BUFFER to CHARPOS. If the target position is
1815 before an intangible character, move to an ok place. */
1818 set_point (ptrdiff_t charpos
)
1820 set_point_both (charpos
, buf_charpos_to_bytepos (current_buffer
, charpos
));
1823 /* If there's an invisible character at position POS + TEST_OFFS in the
1824 current buffer, and the invisible property has a `stickiness' such that
1825 inserting a character at position POS would inherit the property it,
1826 return POS + ADJ, otherwise return POS. If TEST_INTANG, intangibility
1827 is required as well as invisibility.
1829 TEST_OFFS should be either 0 or -1, and ADJ should be either 1 or -1.
1831 Note that `stickiness' is determined by overlay marker insertion types,
1832 if the invisible property comes from an overlay. */
1835 adjust_for_invis_intang (ptrdiff_t pos
, ptrdiff_t test_offs
, ptrdiff_t adj
,
1838 Lisp_Object invis_propval
, invis_overlay
;
1839 Lisp_Object test_pos
;
1841 if ((adj
< 0 && pos
+ adj
< BEGV
) || (adj
> 0 && pos
+ adj
> ZV
))
1842 /* POS + ADJ would be beyond the buffer bounds, so do no adjustment. */
1845 test_pos
= make_number (pos
+ test_offs
);
1848 = get_char_property_and_overlay (test_pos
, Qinvisible
, Qnil
,
1852 || ! NILP (Fget_char_property (test_pos
, Qintangible
, Qnil
)))
1853 && TEXT_PROP_MEANS_INVISIBLE (invis_propval
)
1854 /* This next test is true if the invisible property has a stickiness
1855 such that an insertion at POS would inherit it. */
1856 && (NILP (invis_overlay
)
1857 /* Invisible property is from a text-property. */
1858 ? (text_property_stickiness (Qinvisible
, make_number (pos
), Qnil
)
1859 == (test_offs
== 0 ? 1 : -1))
1860 /* Invisible property is from an overlay. */
1862 ? XMARKER (OVERLAY_START (invis_overlay
))->insertion_type
== 0
1863 : XMARKER (OVERLAY_END (invis_overlay
))->insertion_type
== 1)))
1869 /* Set point in BUFFER to CHARPOS, which corresponds to byte
1870 position BYTEPOS. If the target position is
1871 before an intangible character, move to an ok place. */
1874 set_point_both (ptrdiff_t charpos
, ptrdiff_t bytepos
)
1876 register INTERVAL to
, from
, toprev
, fromprev
;
1877 ptrdiff_t buffer_point
;
1878 ptrdiff_t old_position
= PT
;
1879 /* This ensures that we move forward past intangible text when the
1880 initial position is the same as the destination, in the rare
1881 instances where this is important, e.g. in line-move-finish
1883 bool backwards
= charpos
< old_position
;
1885 ptrdiff_t original_position
;
1887 bset_point_before_scroll (current_buffer
, Qnil
);
1892 /* In a single-byte buffer, the two positions must be equal. */
1893 eassert (ZV
!= ZV_BYTE
|| charpos
== bytepos
);
1895 /* Check this now, before checking if the buffer has any intervals.
1896 That way, we can catch conditions which break this sanity check
1897 whether or not there are intervals in the buffer. */
1898 eassert (charpos
<= ZV
&& charpos
>= BEGV
);
1900 have_overlays
= buffer_has_overlays ();
1902 /* If we have no text properties and overlays,
1903 then we can do it quickly. */
1904 if (!buffer_intervals (current_buffer
) && ! have_overlays
)
1906 temp_set_point_both (current_buffer
, charpos
, bytepos
);
1910 /* Set TO to the interval containing the char after CHARPOS,
1911 and TOPREV to the interval containing the char before CHARPOS.
1912 Either one may be null. They may be equal. */
1913 to
= find_interval (buffer_intervals (current_buffer
), charpos
);
1914 if (charpos
== BEGV
)
1916 else if (to
&& to
->position
== charpos
)
1917 toprev
= previous_interval (to
);
1921 buffer_point
= (PT
== ZV
? ZV
- 1 : PT
);
1923 /* Set FROM to the interval containing the char after PT,
1924 and FROMPREV to the interval containing the char before PT.
1925 Either one may be null. They may be equal. */
1926 /* We could cache this and save time. */
1927 from
= find_interval (buffer_intervals (current_buffer
), buffer_point
);
1928 if (buffer_point
== BEGV
)
1930 else if (from
&& from
->position
== PT
)
1931 fromprev
= previous_interval (from
);
1932 else if (buffer_point
!= PT
)
1933 fromprev
= from
, from
= 0;
1937 /* Moving within an interval. */
1938 if (to
== from
&& toprev
== fromprev
&& INTERVAL_VISIBLE_P (to
)
1941 temp_set_point_both (current_buffer
, charpos
, bytepos
);
1945 original_position
= charpos
;
1947 /* If the new position is between two intangible characters
1948 with the same intangible property value,
1949 move forward or backward until a change in that property. */
1950 if (NILP (Vinhibit_point_motion_hooks
)
1953 /* Intangibility never stops us from positioning at the beginning
1954 or end of the buffer, so don't bother checking in that case. */
1955 && charpos
!= BEGV
&& charpos
!= ZV
)
1958 Lisp_Object intangible_propval
;
1962 /* If the preceding character is both intangible and invisible,
1963 and the invisible property is `rear-sticky', perturb it so
1964 that the search starts one character earlier -- this ensures
1965 that point can never move to the end of an invisible/
1966 intangible/rear-sticky region. */
1967 charpos
= adjust_for_invis_intang (charpos
, -1, -1, 1);
1969 XSETINT (pos
, charpos
);
1971 /* If following char is intangible,
1972 skip back over all chars with matching intangible property. */
1974 intangible_propval
= Fget_char_property (pos
, Qintangible
, Qnil
);
1976 if (! NILP (intangible_propval
))
1978 while (XINT (pos
) > BEGV
1979 && EQ (Fget_char_property (make_number (XINT (pos
) - 1),
1981 intangible_propval
))
1982 pos
= Fprevious_char_property_change (pos
, Qnil
);
1984 /* Set CHARPOS from POS, and if the final intangible character
1985 that we skipped over is also invisible, and the invisible
1986 property is `front-sticky', perturb it to be one character
1987 earlier -- this ensures that point can never move to the
1988 beginning of an invisible/intangible/front-sticky region. */
1989 charpos
= adjust_for_invis_intang (XINT (pos
), 0, -1, 0);
1994 /* If the following character is both intangible and invisible,
1995 and the invisible property is `front-sticky', perturb it so
1996 that the search starts one character later -- this ensures
1997 that point can never move to the beginning of an
1998 invisible/intangible/front-sticky region. */
1999 charpos
= adjust_for_invis_intang (charpos
, 0, 1, 1);
2001 XSETINT (pos
, charpos
);
2003 /* If preceding char is intangible,
2004 skip forward over all chars with matching intangible property. */
2006 intangible_propval
= Fget_char_property (make_number (charpos
- 1),
2009 if (! NILP (intangible_propval
))
2011 while (XINT (pos
) < ZV
2012 && EQ (Fget_char_property (pos
, Qintangible
, Qnil
),
2013 intangible_propval
))
2014 pos
= Fnext_char_property_change (pos
, Qnil
);
2016 /* Set CHARPOS from POS, and if the final intangible character
2017 that we skipped over is also invisible, and the invisible
2018 property is `rear-sticky', perturb it to be one character
2019 later -- this ensures that point can never move to the
2020 end of an invisible/intangible/rear-sticky region. */
2021 charpos
= adjust_for_invis_intang (XINT (pos
), -1, 1, 0);
2025 bytepos
= buf_charpos_to_bytepos (current_buffer
, charpos
);
2028 if (charpos
!= original_position
)
2030 /* Set TO to the interval containing the char after CHARPOS,
2031 and TOPREV to the interval containing the char before CHARPOS.
2032 Either one may be null. They may be equal. */
2033 to
= find_interval (buffer_intervals (current_buffer
), charpos
);
2034 if (charpos
== BEGV
)
2036 else if (to
&& to
->position
== charpos
)
2037 toprev
= previous_interval (to
);
2042 /* Here TO is the interval after the stopping point
2043 and TOPREV is the interval before the stopping point.
2044 One or the other may be null. */
2046 temp_set_point_both (current_buffer
, charpos
, bytepos
);
2048 /* We run point-left and point-entered hooks here, if the
2049 two intervals are not equivalent. These hooks take
2050 (old_point, new_point) as arguments. */
2051 if (NILP (Vinhibit_point_motion_hooks
)
2052 && (! intervals_equal (from
, to
)
2053 || ! intervals_equal (fromprev
, toprev
)))
2055 Lisp_Object leave_after
, leave_before
, enter_after
, enter_before
;
2058 leave_before
= textget (fromprev
->plist
, Qpoint_left
);
2060 leave_before
= Qnil
;
2063 leave_after
= textget (from
->plist
, Qpoint_left
);
2068 enter_before
= textget (toprev
->plist
, Qpoint_entered
);
2070 enter_before
= Qnil
;
2073 enter_after
= textget (to
->plist
, Qpoint_entered
);
2077 if (! EQ (leave_before
, enter_before
) && !NILP (leave_before
))
2078 call2 (leave_before
, make_number (old_position
),
2079 make_number (charpos
));
2080 if (! EQ (leave_after
, enter_after
) && !NILP (leave_after
))
2081 call2 (leave_after
, make_number (old_position
),
2082 make_number (charpos
));
2084 if (! EQ (enter_before
, leave_before
) && !NILP (enter_before
))
2085 call2 (enter_before
, make_number (old_position
),
2086 make_number (charpos
));
2087 if (! EQ (enter_after
, leave_after
) && !NILP (enter_after
))
2088 call2 (enter_after
, make_number (old_position
),
2089 make_number (charpos
));
2093 /* Move point to POSITION, unless POSITION is inside an intangible
2094 segment that reaches all the way to point. */
2097 move_if_not_intangible (ptrdiff_t position
)
2100 Lisp_Object intangible_propval
;
2102 XSETINT (pos
, position
);
2104 if (! NILP (Vinhibit_point_motion_hooks
))
2105 /* If intangible is inhibited, always move point to POSITION. */
2107 else if (PT
< position
&& XINT (pos
) < ZV
)
2109 /* We want to move forward, so check the text before POSITION. */
2111 intangible_propval
= Fget_char_property (pos
,
2114 /* If following char is intangible,
2115 skip back over all chars with matching intangible property. */
2116 if (! NILP (intangible_propval
))
2117 while (XINT (pos
) > BEGV
2118 && EQ (Fget_char_property (make_number (XINT (pos
) - 1),
2120 intangible_propval
))
2121 pos
= Fprevious_char_property_change (pos
, Qnil
);
2123 else if (XINT (pos
) > BEGV
)
2125 /* We want to move backward, so check the text after POSITION. */
2127 intangible_propval
= Fget_char_property (make_number (XINT (pos
) - 1),
2130 /* If following char is intangible,
2131 skip forward over all chars with matching intangible property. */
2132 if (! NILP (intangible_propval
))
2133 while (XINT (pos
) < ZV
2134 && EQ (Fget_char_property (pos
, Qintangible
, Qnil
),
2135 intangible_propval
))
2136 pos
= Fnext_char_property_change (pos
, Qnil
);
2139 else if (position
< BEGV
)
2141 else if (position
> ZV
)
2144 /* If the whole stretch between PT and POSITION isn't intangible,
2145 try moving to POSITION (which means we actually move farther
2146 if POSITION is inside of intangible text). */
2148 if (XINT (pos
) != PT
)
2152 /* If text at position POS has property PROP, set *VAL to the property
2153 value, *START and *END to the beginning and end of a region that
2154 has the same property, and return true. Otherwise return false.
2156 OBJECT is the string or buffer to look for the property in;
2157 nil means the current buffer. */
2160 get_property_and_range (ptrdiff_t pos
, Lisp_Object prop
, Lisp_Object
*val
,
2161 ptrdiff_t *start
, ptrdiff_t *end
, Lisp_Object object
)
2163 INTERVAL i
, prev
, next
;
2166 i
= find_interval (buffer_intervals (current_buffer
), pos
);
2167 else if (BUFFERP (object
))
2168 i
= find_interval (buffer_intervals (XBUFFER (object
)), pos
);
2169 else if (STRINGP (object
))
2170 i
= find_interval (string_intervals (object
), pos
);
2174 if (!i
|| (i
->position
+ LENGTH (i
) <= pos
))
2176 *val
= textget (i
->plist
, prop
);
2180 next
= i
; /* remember it in advance */
2181 prev
= previous_interval (i
);
2183 && EQ (*val
, textget (prev
->plist
, prop
)))
2184 i
= prev
, prev
= previous_interval (prev
);
2185 *start
= i
->position
;
2187 next
= next_interval (i
);
2188 while (next
&& EQ (*val
, textget (next
->plist
, prop
)))
2189 i
= next
, next
= next_interval (next
);
2190 *end
= i
->position
+ LENGTH (i
);
2195 /* Return the proper local keymap TYPE for position POSITION in
2196 BUFFER; TYPE should be one of `keymap' or `local-map'. Use the map
2197 specified by the PROP property, if any. Otherwise, if TYPE is
2198 `local-map' use BUFFER's local map.
2200 POSITION must be in the accessible part of BUFFER. */
2203 get_local_map (register ptrdiff_t position
, register struct buffer
*buffer
,
2206 Lisp_Object prop
, lispy_position
, lispy_buffer
;
2207 ptrdiff_t old_begv
, old_zv
, old_begv_byte
, old_zv_byte
;
2209 /* Perhaps we should just change `position' to the limit. */
2210 if (position
> BUF_ZV (buffer
) || position
< BUF_BEGV (buffer
))
2213 /* Ignore narrowing, so that a local map continues to be valid even if
2214 the visible region contains no characters and hence no properties. */
2215 old_begv
= BUF_BEGV (buffer
);
2216 old_zv
= BUF_ZV (buffer
);
2217 old_begv_byte
= BUF_BEGV_BYTE (buffer
);
2218 old_zv_byte
= BUF_ZV_BYTE (buffer
);
2220 SET_BUF_BEGV_BOTH (buffer
, BUF_BEG (buffer
), BUF_BEG_BYTE (buffer
));
2221 SET_BUF_ZV_BOTH (buffer
, BUF_Z (buffer
), BUF_Z_BYTE (buffer
));
2223 XSETFASTINT (lispy_position
, position
);
2224 XSETBUFFER (lispy_buffer
, buffer
);
2225 /* First check if the CHAR has any property. This is because when
2226 we click with the mouse, the mouse pointer is really pointing
2227 to the CHAR after POS. */
2228 prop
= Fget_char_property (lispy_position
, type
, lispy_buffer
);
2229 /* If not, look at the POS's properties. This is necessary because when
2230 editing a field with a `local-map' property, we want insertion at the end
2231 to obey the `local-map' property. */
2233 prop
= get_pos_property (lispy_position
, type
, lispy_buffer
);
2235 SET_BUF_BEGV_BOTH (buffer
, old_begv
, old_begv_byte
);
2236 SET_BUF_ZV_BOTH (buffer
, old_zv
, old_zv_byte
);
2238 /* Use the local map only if it is valid. */
2239 prop
= get_keymap (prop
, 0, 0);
2243 if (EQ (type
, Qkeymap
))
2246 return BVAR (buffer
, keymap
);
2249 /* Produce an interval tree reflecting the intervals in
2250 TREE from START to START + LENGTH.
2251 The new interval tree has no parent and has a starting-position of 0. */
2254 copy_intervals (INTERVAL tree
, ptrdiff_t start
, ptrdiff_t length
)
2256 register INTERVAL i
, new, t
;
2257 register ptrdiff_t got
, prevlen
;
2259 if (!tree
|| length
<= 0)
2262 i
= find_interval (tree
, start
);
2263 eassert (i
&& LENGTH (i
) > 0);
2265 /* If there is only one interval and it's the default, return nil. */
2266 if ((start
- i
->position
+ 1 + length
) < LENGTH (i
)
2267 && DEFAULT_INTERVAL_P (i
))
2270 new = make_interval ();
2272 got
= (LENGTH (i
) - (start
- i
->position
));
2273 new->total_length
= length
;
2274 eassert (0 <= TOTAL_LENGTH (new));
2275 copy_properties (i
, new);
2279 while (got
< length
)
2281 i
= next_interval (i
);
2282 t
= split_interval_right (t
, prevlen
);
2283 copy_properties (i
, t
);
2284 prevlen
= LENGTH (i
);
2288 return balance_an_interval (new);
2291 /* Give STRING the properties of BUFFER from POSITION to LENGTH. */
2294 copy_intervals_to_string (Lisp_Object string
, struct buffer
*buffer
,
2295 ptrdiff_t position
, ptrdiff_t length
)
2297 INTERVAL interval_copy
= copy_intervals (buffer_intervals (buffer
),
2302 set_interval_object (interval_copy
, string
);
2303 set_string_intervals (string
, interval_copy
);
2306 /* Return true if strings S1 and S2 have identical properties.
2307 Assume they have identical characters. */
2310 compare_string_intervals (Lisp_Object s1
, Lisp_Object s2
)
2314 ptrdiff_t end
= SCHARS (s1
);
2316 i1
= find_interval (string_intervals (s1
), 0);
2317 i2
= find_interval (string_intervals (s2
), 0);
2321 /* Determine how far we can go before we reach the end of I1 or I2. */
2322 ptrdiff_t len1
= (i1
!= 0 ? INTERVAL_LAST_POS (i1
) : end
) - pos
;
2323 ptrdiff_t len2
= (i2
!= 0 ? INTERVAL_LAST_POS (i2
) : end
) - pos
;
2324 ptrdiff_t distance
= min (len1
, len2
);
2326 /* If we ever find a mismatch between the strings,
2328 if (! intervals_equal (i1
, i2
))
2331 /* Advance POS till the end of the shorter interval,
2332 and advance one or both interval pointers for the new position. */
2334 if (len1
== distance
)
2335 i1
= next_interval (i1
);
2336 if (len2
== distance
)
2337 i2
= next_interval (i2
);
2342 /* Recursively adjust interval I in the current buffer
2343 for setting enable_multibyte_characters to MULTI_FLAG.
2344 The range of interval I is START ... END in characters,
2345 START_BYTE ... END_BYTE in bytes. */
2348 set_intervals_multibyte_1 (INTERVAL i
, bool multi_flag
,
2349 ptrdiff_t start
, ptrdiff_t start_byte
,
2350 ptrdiff_t end
, ptrdiff_t end_byte
)
2352 /* Fix the length of this interval. */
2354 i
->total_length
= end
- start
;
2356 i
->total_length
= end_byte
- start_byte
;
2357 eassert (0 <= TOTAL_LENGTH (i
));
2359 if (TOTAL_LENGTH (i
) == 0)
2361 delete_interval (i
);
2365 /* Recursively fix the length of the subintervals. */
2368 ptrdiff_t left_end
, left_end_byte
;
2373 left_end_byte
= start_byte
+ LEFT_TOTAL_LENGTH (i
);
2374 left_end
= BYTE_TO_CHAR (left_end_byte
);
2376 temp
= CHAR_TO_BYTE (left_end
);
2378 /* If LEFT_END_BYTE is in the middle of a character,
2379 adjust it and LEFT_END to a char boundary. */
2380 if (left_end_byte
> temp
)
2382 left_end_byte
= temp
;
2384 if (left_end_byte
< temp
)
2387 left_end_byte
= CHAR_TO_BYTE (left_end
);
2392 left_end
= start
+ LEFT_TOTAL_LENGTH (i
);
2393 left_end_byte
= CHAR_TO_BYTE (left_end
);
2396 set_intervals_multibyte_1 (i
->left
, multi_flag
, start
, start_byte
,
2397 left_end
, left_end_byte
);
2401 ptrdiff_t right_start_byte
, right_start
;
2407 right_start_byte
= end_byte
- RIGHT_TOTAL_LENGTH (i
);
2408 right_start
= BYTE_TO_CHAR (right_start_byte
);
2410 /* If RIGHT_START_BYTE is in the middle of a character,
2411 adjust it and RIGHT_START to a char boundary. */
2412 temp
= CHAR_TO_BYTE (right_start
);
2414 if (right_start_byte
< temp
)
2416 right_start_byte
= temp
;
2418 if (right_start_byte
> temp
)
2421 right_start_byte
= CHAR_TO_BYTE (right_start
);
2426 right_start
= end
- RIGHT_TOTAL_LENGTH (i
);
2427 right_start_byte
= CHAR_TO_BYTE (right_start
);
2430 set_intervals_multibyte_1 (i
->right
, multi_flag
,
2431 right_start
, right_start_byte
,
2435 /* Rounding to char boundaries can theoretically ake this interval
2436 spurious. If so, delete one child, and copy its property list
2437 to this interval. */
2438 if (LEFT_TOTAL_LENGTH (i
) + RIGHT_TOTAL_LENGTH (i
) >= TOTAL_LENGTH (i
))
2442 set_interval_plist (i
, i
->left
->plist
);
2443 (i
)->left
->total_length
= 0;
2444 delete_interval ((i
)->left
);
2448 set_interval_plist (i
, i
->right
->plist
);
2449 (i
)->right
->total_length
= 0;
2450 delete_interval ((i
)->right
);
2455 /* Update the intervals of the current buffer
2456 to fit the contents as multibyte (if MULTI_FLAG)
2457 or to fit them as non-multibyte (if not MULTI_FLAG). */
2460 set_intervals_multibyte (bool multi_flag
)
2462 INTERVAL i
= buffer_intervals (current_buffer
);
2465 set_intervals_multibyte_1 (i
, multi_flag
, BEG
, BEG_BYTE
, Z
, Z_BYTE
);