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
45 #include "intervals.h"
46 #include "character.h"
52 /* Test for membership, allowing for t (actually any non-cons) to mean the
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. */
67 set_interval_object (INTERVAL i
, Lisp_Object obj
)
69 eassert (BUFFERP (obj
) || STRINGP (obj
));
75 set_interval_left (INTERVAL i
, INTERVAL left
)
81 set_interval_right (INTERVAL i
, INTERVAL 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. */
90 copy_interval_parent (INTERVAL d
, INTERVAL s
)
93 d
->up_obj
= s
->up_obj
;
96 /* Create the root interval of some object, a buffer or string. */
99 create_root_interval (Lisp_Object parent
)
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);
115 else if (STRINGP (parent
))
117 new->total_length
= SCHARS (parent
);
118 eassert (TOTAL_LENGTH (new) >= 0);
119 set_string_intervals (parent
, new);
123 set_interval_object (new, parent
);
128 /* Make the interval TARGET have exactly the properties of SOURCE */
131 copy_properties (register INTERVAL source
, register INTERVAL target
)
133 if (DEFAULT_INTERVAL_P (source
) && DEFAULT_INTERVAL_P (target
))
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. */
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
))
152 MERGE_INTERVAL_CACHE (source
, target
);
162 while (CONSP (val
) && !EQ (XCAR (val
), sym
))
173 set_interval_plist (target
, Fcons (sym
, Fcons (val
, target
->plist
)));
179 /* Return true if the two intervals have the same properties. */
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
))
190 if (DEFAULT_INTERVAL_P (i0
) || DEFAULT_INTERVAL_P (i1
))
195 while (CONSP (i0_cdr
) && CONSP (i1_cdr
))
197 i0_sym
= XCAR (i0_cdr
);
198 i0_cdr
= XCDR (i0_cdr
);
202 while (CONSP (i1_val
) && !EQ (XCAR (i1_val
), i0_sym
))
204 i1_val
= XCDR (i1_val
);
207 i1_val
= XCDR (i1_val
);
210 /* i0 has something i1 doesn't. */
211 if (EQ (i1_val
, Qnil
))
214 /* i0 and i1 both have sym, but it has different values in each. */
216 || (i1_val
= XCDR (i1_val
), !CONSP (i1_val
))
217 || !EQ (XCAR (i1_val
), XCAR (i0_cdr
)))
220 i0_cdr
= XCDR (i0_cdr
);
222 i1_cdr
= XCDR (i1_cdr
);
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. */
238 traverse_intervals_noorder (INTERVAL tree
, void (*function
) (INTERVAL
, Lisp_Object
), Lisp_Object arg
)
240 /* Minimize stack usage. */
243 (*function
) (tree
, arg
);
248 traverse_intervals_noorder (tree
->left
, function
, arg
);
254 /* Traverse an interval tree TREE, performing FUNCTION on each node.
255 Pass FUNCTION two args: an interval, and ARG. */
258 traverse_intervals (INTERVAL tree
, ptrdiff_t position
,
259 void (*function
) (INTERVAL
, Lisp_Object
), Lisp_Object arg
)
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
;
275 static int zero_length
;
277 /* These functions are temporary, for debugging purposes only. */
279 INTERVAL search_interval
, found_interval
;
282 check_for_interval (INTERVAL i
)
284 if (i
== search_interval
)
292 search_for_interval (INTERVAL i
, INTERVAL tree
)
296 found_interval
= NULL
;
297 traverse_intervals_noorder (tree
, &check_for_interval
, Qnil
);
298 return found_interval
;
302 inc_interval_count (INTERVAL i
)
312 count_intervals (INTERVAL i
)
317 traverse_intervals_noorder (i
, &inc_interval_count
, Qnil
);
323 root_interval (INTERVAL interval
)
325 register INTERVAL i
= interval
;
327 while (! ROOT_INTERVAL_P (i
))
328 i
= INTERVAL_PARENT (i
);
334 /* Assuming that a left child exists, perform the following operation:
344 rotate_right (INTERVAL interval
)
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
);
356 set_interval_right (INTERVAL_PARENT (interval
), B
);
358 copy_interval_parent (B
, interval
);
360 /* Make B the parent of A */
362 set_interval_right (B
, interval
);
363 set_interval_parent (interval
, B
);
365 /* Make A point to c */
366 set_interval_left (interval
, 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);
381 /* Assuming that a right child exists, perform the following operation:
391 rotate_left (INTERVAL interval
)
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
);
403 set_interval_right (INTERVAL_PARENT (interval
), B
);
405 copy_interval_parent (B
, interval
);
407 /* Make B the parent of A */
409 set_interval_left (B
, interval
);
410 set_interval_parent (interval
, B
);
412 /* Make A point to c */
413 set_interval_right (interval
, 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);
428 /* Balance an interval tree with the assumption that the subtrees
429 themselves are already balanced. */
432 balance_an_interval (INTERVAL i
)
434 register ptrdiff_t old_diff
, new_diff
;
438 old_diff
= LEFT_TOTAL_LENGTH (i
) - RIGHT_TOTAL_LENGTH (i
);
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
)
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
)
457 balance_an_interval (i
->left
);
465 /* Balance INTERVAL, potentially stuffing it back into its parent
469 balance_possible_root_interval (INTERVAL interval
)
472 bool have_parent
= 0;
474 if (!INTERVAL_HAS_OBJECT (interval
) && !INTERVAL_HAS_PARENT (interval
))
477 if (INTERVAL_HAS_OBJECT (interval
))
480 GET_INTERVAL_OBJECT (parent
, interval
);
482 interval
= balance_an_interval (interval
);
486 if (BUFFERP (parent
))
487 set_buffer_intervals (XBUFFER (parent
), interval
);
488 else if (STRINGP (parent
))
489 set_string_intervals (parent
, interval
);
495 /* Balance the interval tree TREE. Balancing is by weight
496 (the amount of text). */
499 balance_intervals_internal (register INTERVAL tree
)
501 /* Balance within each side. */
503 balance_intervals_internal (tree
->left
);
505 balance_intervals_internal (tree
->right
);
506 return balance_an_interval (tree
);
509 /* Advertised interface to balance intervals. */
512 balance_intervals (INTERVAL tree
)
514 return tree
? balance_intervals_internal (tree
) : NULL
;
517 /* Rebalance text properties of B. */
520 buffer_balance_intervals (struct buffer
*b
)
525 i
= buffer_intervals (b
);
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
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. */
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);
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
);
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
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. */
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);
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
);
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! */
629 interval_start_pos (INTERVAL source
)
636 if (! INTERVAL_HAS_OBJECT (source
))
638 GET_INTERVAL_OBJECT (parent
, source
);
639 if (BUFFERP (parent
))
640 return BUF_BEG (XBUFFER (parent
));
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. */
655 find_interval (register INTERVAL tree
, register ptrdiff_t position
)
657 /* The distance from the left edge of the subtree at TREE
659 register ptrdiff_t relative_position
;
664 relative_position
= position
;
665 if (INTERVAL_HAS_OBJECT (tree
))
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
);
679 if (relative_position
< LEFT_TOTAL_LENGTH (tree
))
683 else if (! NULL_RIGHT_CHILD (tree
)
684 && relative_position
>= (TOTAL_LENGTH (tree
)
685 - RIGHT_TOTAL_LENGTH (tree
)))
687 relative_position
-= (TOTAL_LENGTH (tree
)
688 - RIGHT_TOTAL_LENGTH (tree
));
694 = (position
- relative_position
/* left edge of *tree. */
695 + LEFT_TOTAL_LENGTH (tree
)); /* left edge of this interval. */
702 /* Find the succeeding interval (lexicographically) to INTERVAL.
703 Sets the `position' field based on that of INTERVAL (see
707 next_interval (register INTERVAL interval
)
709 register INTERVAL i
= interval
;
710 register ptrdiff_t next_position
;
714 next_position
= interval
->position
+ LENGTH (interval
);
716 if (! NULL_RIGHT_CHILD (i
))
719 while (! NULL_LEFT_CHILD (i
))
722 i
->position
= next_position
;
726 while (! NULL_PARENT (i
))
728 if (AM_LEFT_CHILD (i
))
730 i
= INTERVAL_PARENT (i
);
731 i
->position
= next_position
;
735 i
= INTERVAL_PARENT (i
);
741 /* Find the preceding interval (lexicographically) to INTERVAL.
742 Sets the `position' field based on that of INTERVAL (see
746 previous_interval (register INTERVAL interval
)
753 if (! NULL_LEFT_CHILD (interval
))
756 while (! NULL_RIGHT_CHILD (i
))
759 i
->position
= interval
->position
- LENGTH (i
);
764 while (! NULL_PARENT (i
))
766 if (AM_RIGHT_CHILD (i
))
768 i
= INTERVAL_PARENT (i
);
770 i
->position
= interval
->position
- LENGTH (i
);
773 i
= INTERVAL_PARENT (i
);
779 /* Find the interval containing POS given some non-NULL INTERVAL
780 in the same tree. Note that we need to update interval->position
781 if we go down the tree.
782 To speed up the process, we assume that the ->position of
783 I and all its parents is already uptodate. */
785 update_interval (register INTERVAL i
, ptrdiff_t pos
)
792 if (pos
< i
->position
)
795 if (pos
>= i
->position
- TOTAL_LENGTH (i
->left
))
797 i
->left
->position
= i
->position
- TOTAL_LENGTH (i
->left
)
798 + LEFT_TOTAL_LENGTH (i
->left
);
799 i
= i
->left
; /* Move to the left child */
801 else if (NULL_PARENT (i
))
802 error ("Point before start of properties");
804 i
= INTERVAL_PARENT (i
);
807 else if (pos
>= INTERVAL_LAST_POS (i
))
810 if (pos
< INTERVAL_LAST_POS (i
) + TOTAL_LENGTH (i
->right
))
812 i
->right
->position
= INTERVAL_LAST_POS (i
)
813 + LEFT_TOTAL_LENGTH (i
->right
);
814 i
= i
->right
; /* Move to the right child */
816 else if (NULL_PARENT (i
))
817 error ("Point %"pD
"d after end of properties", pos
);
819 i
= INTERVAL_PARENT (i
);
827 /* Effect an adjustment corresponding to the addition of LENGTH characters
828 of text. Do this by finding the interval containing POSITION in the
829 interval tree TREE, and then adjusting all of its ancestors by adding
832 If POSITION is the first character of an interval, meaning that point
833 is actually between the two intervals, make the new text belong to
834 the interval which is "sticky".
836 If both intervals are "sticky", then make them belong to the left-most
837 interval. Another possibility would be to create a new interval for
838 this text, and make it have the merged properties of both ends. */
841 adjust_intervals_for_insertion (INTERVAL tree
,
842 ptrdiff_t position
, ptrdiff_t length
)
850 eassert (TOTAL_LENGTH (tree
) > 0);
852 GET_INTERVAL_OBJECT (parent
, tree
);
853 offset
= (BUFFERP (parent
) ? BUF_BEG (XBUFFER (parent
)) : 0);
855 /* If inserting at point-max of a buffer, that position will be out
856 of range. Remember that buffer positions are 1-based. */
857 if (position
>= TOTAL_LENGTH (tree
) + offset
)
859 position
= TOTAL_LENGTH (tree
) + offset
;
863 i
= find_interval (tree
, position
);
865 /* If in middle of an interval which is not sticky either way,
866 we must not just give its properties to the insertion.
867 So split this interval at the insertion point.
869 Originally, the if condition here was this:
870 (! (position == i->position || eobp)
871 && END_NONSTICKY_P (i)
872 && FRONT_NONSTICKY_P (i))
873 But, these macros are now unreliable because of introduction of
874 Vtext_property_default_nonsticky. So, we always check properties
875 one by one if POSITION is in middle of an interval. */
876 if (! (position
== i
->position
|| eobp
))
879 Lisp_Object front
, rear
;
883 /* Properties font-sticky and rear-nonsticky override
884 Vtext_property_default_nonsticky. So, if they are t, we can
885 skip one by one checking of properties. */
886 rear
= textget (i
->plist
, Qrear_nonsticky
);
887 if (! CONSP (rear
) && ! NILP (rear
))
889 /* All properties are nonsticky. We split the interval. */
892 front
= textget (i
->plist
, Qfront_sticky
);
893 if (! CONSP (front
) && ! NILP (front
))
895 /* All properties are sticky. We don't split the interval. */
900 /* Does any actual property pose an actual problem? We break
901 the loop if we find a nonsticky property. */
902 for (; CONSP (tail
); tail
= Fcdr (XCDR (tail
)))
904 Lisp_Object prop
, tmp
;
907 /* Is this particular property front-sticky? */
908 if (CONSP (front
) && ! NILP (Fmemq (prop
, front
)))
911 /* Is this particular property rear-nonsticky? */
912 if (CONSP (rear
) && ! NILP (Fmemq (prop
, rear
)))
915 /* Is this particular property recorded as sticky or
916 nonsticky in Vtext_property_default_nonsticky? */
917 tmp
= Fassq (prop
, Vtext_property_default_nonsticky
);
925 /* By default, a text property is rear-sticky, thus we
926 continue the loop. */
930 /* If any property is a real problem, split the interval. */
933 temp
= split_interval_right (i
, position
- i
->position
);
934 copy_properties (i
, temp
);
939 /* If we are positioned between intervals, check the stickiness of
940 both of them. We have to do this too, if we are at BEG or Z. */
941 if (position
== i
->position
|| eobp
)
943 register INTERVAL prev
;
953 prev
= previous_interval (i
);
955 /* Even if we are positioned between intervals, we default
956 to the left one if it exists. We extend it now and split
957 off a part later, if stickiness demands it. */
958 for (temp
= prev
? prev
: i
; temp
; temp
= INTERVAL_PARENT_OR_NULL (temp
))
960 temp
->total_length
+= length
;
961 eassert (TOTAL_LENGTH (temp
) >= 0);
962 temp
= balance_possible_root_interval (temp
);
965 /* If at least one interval has sticky properties,
966 we check the stickiness property by property.
968 Originally, the if condition here was this:
969 (END_NONSTICKY_P (prev) || FRONT_STICKY_P (i))
970 But, these macros are now unreliable because of introduction
971 of Vtext_property_default_nonsticky. So, we always have to
972 check stickiness of properties one by one. If cache of
973 stickiness is implemented in the future, we may be able to
974 use those macros again. */
977 Lisp_Object pleft
, pright
;
978 struct interval newi
;
980 RESET_INTERVAL (&newi
);
981 pleft
= prev
? prev
->plist
: Qnil
;
982 pright
= i
? i
->plist
: Qnil
;
983 set_interval_plist (&newi
, merge_properties_sticky (pleft
, pright
));
985 if (! prev
) /* i.e. position == BEG */
987 if (! intervals_equal (i
, &newi
))
989 i
= split_interval_left (i
, length
);
990 set_interval_plist (i
, newi
.plist
);
993 else if (! intervals_equal (prev
, &newi
))
995 prev
= split_interval_right (prev
, position
- prev
->position
);
996 set_interval_plist (prev
, newi
.plist
);
997 if (i
&& intervals_equal (prev
, i
))
998 merge_interval_right (prev
);
1001 /* We will need to update the cache here later. */
1003 else if (! prev
&& ! NILP (i
->plist
))
1005 /* Just split off a new interval at the left.
1006 Since I wasn't front-sticky, the empty plist is ok. */
1007 i
= split_interval_left (i
, length
);
1011 /* Otherwise just extend the interval. */
1014 for (temp
= i
; temp
; temp
= INTERVAL_PARENT_OR_NULL (temp
))
1016 temp
->total_length
+= length
;
1017 eassert (TOTAL_LENGTH (temp
) >= 0);
1018 temp
= balance_possible_root_interval (temp
);
1025 /* Any property might be front-sticky on the left, rear-sticky on the left,
1026 front-sticky on the right, or rear-sticky on the right; the 16 combinations
1027 can be arranged in a matrix with rows denoting the left conditions and
1028 columns denoting the right conditions:
1036 left-props = '(front-sticky (p8 p9 pa pb pc pd pe pf)
1037 rear-nonsticky (p4 p5 p6 p7 p8 p9 pa pb)
1038 p0 L p1 L p2 L p3 L p4 L p5 L p6 L p7 L
1039 p8 L p9 L pa L pb L pc L pd L pe L pf L)
1040 right-props = '(front-sticky (p2 p3 p6 p7 pa pb pe pf)
1041 rear-nonsticky (p1 p2 p5 p6 p9 pa pd pe)
1042 p0 R p1 R p2 R p3 R p4 R p5 R p6 R p7 R
1043 p8 R p9 R pa R pb R pc R pd R pe R pf R)
1045 We inherit from whoever has a sticky side facing us. If both sides
1046 do (cases 2, 3, E, and F), then we inherit from whichever side has a
1047 non-nil value for the current property. If both sides do, then we take
1050 When we inherit a property, we get its stickiness as well as its value.
1051 So, when we merge the above two lists, we expect to get this:
1053 result = '(front-sticky (p6 p7 pa pb pc pd pe pf)
1054 rear-nonsticky (p6 pa)
1055 p0 L p1 L p2 L p3 L p6 R p7 R
1056 pa R pb R pc L pd L pe L pf L)
1058 The optimizable special cases are:
1059 left rear-nonsticky = nil, right front-sticky = nil (inherit left)
1060 left rear-nonsticky = t, right front-sticky = t (inherit right)
1061 left rear-nonsticky = t, right front-sticky = nil (inherit none)
1065 merge_properties_sticky (Lisp_Object pleft
, Lisp_Object pright
)
1067 Lisp_Object props
, front
, rear
;
1068 Lisp_Object lfront
, lrear
, rfront
, rrear
;
1069 Lisp_Object tail1
, tail2
, sym
, lval
, rval
, cat
;
1070 bool use_left
, use_right
, lpresent
;
1075 lfront
= textget (pleft
, Qfront_sticky
);
1076 lrear
= textget (pleft
, Qrear_nonsticky
);
1077 rfront
= textget (pright
, Qfront_sticky
);
1078 rrear
= textget (pright
, Qrear_nonsticky
);
1080 /* Go through each element of PRIGHT. */
1081 for (tail1
= pright
; CONSP (tail1
); tail1
= Fcdr (XCDR (tail1
)))
1087 /* Sticky properties get special treatment. */
1088 if (EQ (sym
, Qrear_nonsticky
) || EQ (sym
, Qfront_sticky
))
1091 rval
= Fcar (XCDR (tail1
));
1092 for (tail2
= pleft
; CONSP (tail2
); tail2
= Fcdr (XCDR (tail2
)))
1093 if (EQ (sym
, XCAR (tail2
)))
1096 /* Indicate whether the property is explicitly defined on the left.
1097 (We know it is defined explicitly on the right
1098 because otherwise we don't get here.) */
1099 lpresent
= ! NILP (tail2
);
1100 lval
= (NILP (tail2
) ? Qnil
: Fcar (Fcdr (tail2
)));
1102 /* Even if lrear or rfront say nothing about the stickiness of
1103 SYM, Vtext_property_default_nonsticky may give default
1104 stickiness to SYM. */
1105 tmp
= Fassq (sym
, Vtext_property_default_nonsticky
);
1106 use_left
= (lpresent
1107 && ! (TMEM (sym
, lrear
)
1108 || (CONSP (tmp
) && ! NILP (XCDR (tmp
)))));
1109 use_right
= (TMEM (sym
, rfront
)
1110 || (CONSP (tmp
) && NILP (XCDR (tmp
))));
1111 if (use_left
&& use_right
)
1115 else if (NILP (rval
))
1120 /* We build props as (value sym ...) rather than (sym value ...)
1121 because we plan to nreverse it when we're done. */
1122 props
= Fcons (lval
, Fcons (sym
, props
));
1123 if (TMEM (sym
, lfront
))
1124 front
= Fcons (sym
, front
);
1125 if (TMEM (sym
, lrear
))
1126 rear
= Fcons (sym
, rear
);
1130 props
= Fcons (rval
, Fcons (sym
, props
));
1131 if (TMEM (sym
, rfront
))
1132 front
= Fcons (sym
, front
);
1133 if (TMEM (sym
, rrear
))
1134 rear
= Fcons (sym
, rear
);
1138 /* Now go through each element of PLEFT. */
1139 for (tail2
= pleft
; CONSP (tail2
); tail2
= Fcdr (XCDR (tail2
)))
1145 /* Sticky properties get special treatment. */
1146 if (EQ (sym
, Qrear_nonsticky
) || EQ (sym
, Qfront_sticky
))
1149 /* If sym is in PRIGHT, we've already considered it. */
1150 for (tail1
= pright
; CONSP (tail1
); tail1
= Fcdr (XCDR (tail1
)))
1151 if (EQ (sym
, XCAR (tail1
)))
1156 lval
= Fcar (XCDR (tail2
));
1158 /* Even if lrear or rfront say nothing about the stickiness of
1159 SYM, Vtext_property_default_nonsticky may give default
1160 stickiness to SYM. */
1161 tmp
= Fassq (sym
, Vtext_property_default_nonsticky
);
1163 /* Since rval is known to be nil in this loop, the test simplifies. */
1164 if (! (TMEM (sym
, lrear
) || (CONSP (tmp
) && ! NILP (XCDR (tmp
)))))
1166 props
= Fcons (lval
, Fcons (sym
, props
));
1167 if (TMEM (sym
, lfront
))
1168 front
= Fcons (sym
, front
);
1170 else if (TMEM (sym
, rfront
) || (CONSP (tmp
) && NILP (XCDR (tmp
))))
1172 /* The value is nil, but we still inherit the stickiness
1174 front
= Fcons (sym
, front
);
1175 if (TMEM (sym
, rrear
))
1176 rear
= Fcons (sym
, rear
);
1179 props
= Fnreverse (props
);
1181 props
= Fcons (Qrear_nonsticky
, Fcons (Fnreverse (rear
), props
));
1183 cat
= textget (props
, Qcategory
);
1186 /* If we have inherited a front-stick category property that is t,
1187 we don't need to set up a detailed one. */
1188 ! (! NILP (cat
) && SYMBOLP (cat
)
1189 && EQ (Fget (cat
, Qfront_sticky
), Qt
)))
1190 props
= Fcons (Qfront_sticky
, Fcons (Fnreverse (front
), props
));
1195 /* Delete a node I from its interval tree by merging its subtrees
1196 into one subtree which is then returned. Caller is responsible for
1197 storing the resulting subtree into its parent. */
1200 delete_node (register INTERVAL i
)
1202 register INTERVAL migrate
, this;
1203 register ptrdiff_t migrate_amt
;
1211 migrate_amt
= i
->left
->total_length
;
1213 this->total_length
+= migrate_amt
;
1217 this->total_length
+= migrate_amt
;
1219 eassert (TOTAL_LENGTH (this) >= 0);
1220 set_interval_left (this, migrate
);
1221 set_interval_parent (migrate
, this);
1226 /* Delete interval I from its tree by calling `delete_node'
1227 and properly connecting the resultant subtree.
1229 I is presumed to be empty; that is, no adjustments are made
1230 for the length of I. */
1233 delete_interval (register INTERVAL i
)
1235 register INTERVAL parent
;
1236 ptrdiff_t amt
= LENGTH (i
);
1238 eassert (amt
== 0); /* Only used on zero-length intervals now. */
1240 if (ROOT_INTERVAL_P (i
))
1243 GET_INTERVAL_OBJECT (owner
, i
);
1244 parent
= delete_node (i
);
1246 set_interval_object (parent
, owner
);
1248 if (BUFFERP (owner
))
1249 set_buffer_intervals (XBUFFER (owner
), parent
);
1250 else if (STRINGP (owner
))
1251 set_string_intervals (owner
, parent
);
1258 parent
= INTERVAL_PARENT (i
);
1259 if (AM_LEFT_CHILD (i
))
1261 set_interval_left (parent
, delete_node (i
));
1263 set_interval_parent (parent
->left
, parent
);
1267 set_interval_right (parent
, delete_node (i
));
1269 set_interval_parent (parent
->right
, parent
);
1273 /* Find the interval in TREE corresponding to the relative position
1274 FROM and delete as much as possible of AMOUNT from that interval.
1275 Return the amount actually deleted, and if the interval was
1276 zeroed-out, delete that interval node from the tree.
1278 Note that FROM is actually origin zero, aka relative to the
1279 leftmost edge of tree. This is appropriate since we call ourselves
1280 recursively on subtrees.
1282 Do this by recursing down TREE to the interval in question, and
1283 deleting the appropriate amount of text. */
1286 interval_deletion_adjustment (register INTERVAL tree
, register ptrdiff_t from
,
1287 register ptrdiff_t amount
)
1289 register ptrdiff_t relative_position
= from
;
1295 if (relative_position
< LEFT_TOTAL_LENGTH (tree
))
1297 ptrdiff_t subtract
= interval_deletion_adjustment (tree
->left
,
1300 tree
->total_length
-= subtract
;
1301 eassert (TOTAL_LENGTH (tree
) >= 0);
1305 else if (relative_position
>= (TOTAL_LENGTH (tree
)
1306 - RIGHT_TOTAL_LENGTH (tree
)))
1310 relative_position
-= (tree
->total_length
1311 - RIGHT_TOTAL_LENGTH (tree
));
1312 subtract
= interval_deletion_adjustment (tree
->right
,
1315 tree
->total_length
-= subtract
;
1316 eassert (TOTAL_LENGTH (tree
) >= 0);
1319 /* Here -- this node. */
1322 /* How much can we delete from this interval? */
1323 ptrdiff_t my_amount
= ((tree
->total_length
1324 - RIGHT_TOTAL_LENGTH (tree
))
1325 - relative_position
);
1327 if (amount
> my_amount
)
1330 tree
->total_length
-= amount
;
1331 eassert (TOTAL_LENGTH (tree
) >= 0);
1332 if (LENGTH (tree
) == 0)
1333 delete_interval (tree
);
1338 /* Never reach here. */
1341 /* Effect the adjustments necessary to the interval tree of BUFFER to
1342 correspond to the deletion of LENGTH characters from that buffer
1343 text. The deletion is effected at position START (which is a
1344 buffer position, i.e. origin 1). */
1347 adjust_intervals_for_deletion (struct buffer
*buffer
,
1348 ptrdiff_t start
, ptrdiff_t length
)
1350 ptrdiff_t left_to_delete
= length
;
1351 INTERVAL tree
= buffer_intervals (buffer
);
1355 GET_INTERVAL_OBJECT (parent
, tree
);
1356 offset
= (BUFFERP (parent
) ? BUF_BEG (XBUFFER (parent
)) : 0);
1361 eassert (start
<= offset
+ TOTAL_LENGTH (tree
)
1362 && start
+ length
<= offset
+ TOTAL_LENGTH (tree
));
1364 if (length
== TOTAL_LENGTH (tree
))
1366 set_buffer_intervals (buffer
, NULL
);
1370 if (ONLY_INTERVAL_P (tree
))
1372 tree
->total_length
-= length
;
1373 eassert (TOTAL_LENGTH (tree
) >= 0);
1377 if (start
> offset
+ TOTAL_LENGTH (tree
))
1378 start
= offset
+ TOTAL_LENGTH (tree
);
1379 while (left_to_delete
> 0)
1381 left_to_delete
-= interval_deletion_adjustment (tree
, start
- offset
,
1383 tree
= buffer_intervals (buffer
);
1384 if (left_to_delete
== tree
->total_length
)
1386 set_buffer_intervals (buffer
, NULL
);
1392 /* Make the adjustments necessary to the interval tree of BUFFER to
1393 represent an addition or deletion of LENGTH characters starting
1394 at position START. Addition or deletion is indicated by the sign
1398 offset_intervals (struct buffer
*buffer
, ptrdiff_t start
, ptrdiff_t length
)
1400 if (!buffer_intervals (buffer
) || length
== 0)
1404 adjust_intervals_for_insertion (buffer_intervals (buffer
),
1407 adjust_intervals_for_deletion (buffer
, start
, -length
);
1410 /* Merge interval I with its lexicographic successor. The resulting
1411 interval is returned, and has the properties of the original
1412 successor. The properties of I are lost. I is removed from the
1416 The caller must verify that this is not the last (rightmost)
1420 merge_interval_right (register INTERVAL i
)
1422 register ptrdiff_t absorb
= LENGTH (i
);
1423 register INTERVAL successor
;
1425 /* Find the succeeding interval. */
1426 if (! NULL_RIGHT_CHILD (i
)) /* It's below us. Add absorb
1429 successor
= i
->right
;
1430 while (! NULL_LEFT_CHILD (successor
))
1432 successor
->total_length
+= absorb
;
1433 eassert (TOTAL_LENGTH (successor
) >= 0);
1434 successor
= successor
->left
;
1437 successor
->total_length
+= absorb
;
1438 eassert (TOTAL_LENGTH (successor
) >= 0);
1439 delete_interval (i
);
1443 /* Zero out this interval. */
1444 i
->total_length
-= absorb
;
1445 eassert (TOTAL_LENGTH (i
) >= 0);
1448 while (! NULL_PARENT (successor
)) /* It's above us. Subtract as
1451 if (AM_LEFT_CHILD (successor
))
1453 successor
= INTERVAL_PARENT (successor
);
1454 delete_interval (i
);
1458 successor
= INTERVAL_PARENT (successor
);
1459 successor
->total_length
-= absorb
;
1460 eassert (TOTAL_LENGTH (successor
) >= 0);
1463 /* This must be the rightmost or last interval and cannot
1464 be merged right. The caller should have known. */
1468 /* Merge interval I with its lexicographic predecessor. The resulting
1469 interval is returned, and has the properties of the original predecessor.
1470 The properties of I are lost. Interval node I is removed from the tree.
1473 The caller must verify that this is not the first (leftmost) interval. */
1476 merge_interval_left (register INTERVAL i
)
1478 register ptrdiff_t absorb
= LENGTH (i
);
1479 register INTERVAL predecessor
;
1481 /* Find the preceding interval. */
1482 if (! NULL_LEFT_CHILD (i
)) /* It's below us. Go down,
1483 adding ABSORB as we go. */
1485 predecessor
= i
->left
;
1486 while (! NULL_RIGHT_CHILD (predecessor
))
1488 predecessor
->total_length
+= absorb
;
1489 eassert (TOTAL_LENGTH (predecessor
) >= 0);
1490 predecessor
= predecessor
->right
;
1493 predecessor
->total_length
+= absorb
;
1494 eassert (TOTAL_LENGTH (predecessor
) >= 0);
1495 delete_interval (i
);
1499 /* Zero out this interval. */
1500 i
->total_length
-= absorb
;
1501 eassert (TOTAL_LENGTH (i
) >= 0);
1504 while (! NULL_PARENT (predecessor
)) /* It's above us. Go up,
1505 subtracting ABSORB. */
1507 if (AM_RIGHT_CHILD (predecessor
))
1509 predecessor
= INTERVAL_PARENT (predecessor
);
1510 delete_interval (i
);
1514 predecessor
= INTERVAL_PARENT (predecessor
);
1515 predecessor
->total_length
-= absorb
;
1516 eassert (TOTAL_LENGTH (predecessor
) >= 0);
1519 /* This must be the leftmost or first interval and cannot
1520 be merged left. The caller should have known. */
1524 /* Create a copy of SOURCE but with the default value of UP. */
1527 reproduce_interval (INTERVAL source
)
1529 register INTERVAL target
= make_interval ();
1531 target
->total_length
= source
->total_length
;
1532 target
->position
= source
->position
;
1534 copy_properties (source
, target
);
1536 if (! NULL_LEFT_CHILD (source
))
1537 set_interval_left (target
, reproduce_tree (source
->left
, target
));
1538 if (! NULL_RIGHT_CHILD (source
))
1539 set_interval_right (target
, reproduce_tree (source
->right
, target
));
1544 /* Make an exact copy of interval tree SOURCE which descends from
1545 PARENT. This is done by recursing through SOURCE, copying
1546 the current interval and its properties, and then adjusting
1547 the pointers of the copy. */
1550 reproduce_tree (INTERVAL source
, INTERVAL parent
)
1552 INTERVAL target
= reproduce_interval (source
);
1553 set_interval_parent (target
, parent
);
1558 reproduce_tree_obj (INTERVAL source
, Lisp_Object parent
)
1560 INTERVAL target
= reproduce_interval (source
);
1561 set_interval_object (target
, parent
);
1565 /* Insert the intervals of SOURCE into BUFFER at POSITION.
1566 LENGTH is the length of the text in SOURCE.
1568 The `position' field of the SOURCE intervals is assumed to be
1569 consistent with its parent; therefore, SOURCE must be an
1570 interval tree made with copy_interval or must be the whole
1571 tree of a buffer or a string.
1573 This is used in insdel.c when inserting Lisp_Strings into the
1574 buffer. The text corresponding to SOURCE is already in the buffer
1575 when this is called. The intervals of new tree are a copy of those
1576 belonging to the string being inserted; intervals are never
1579 If the inserted text had no intervals associated, and we don't
1580 want to inherit the surrounding text's properties, this function
1581 simply returns -- offset_intervals should handle placing the
1582 text in the correct interval, depending on the sticky bits.
1584 If the inserted text had properties (intervals), then there are two
1585 cases -- either insertion happened in the middle of some interval,
1586 or between two intervals.
1588 If the text goes into the middle of an interval, then new intervals
1589 are created in the middle, and new text has the union of its properties
1590 and those of the text into which it was inserted.
1592 If the text goes between two intervals, then if neither interval
1593 had its appropriate sticky property set (front_sticky, rear_sticky),
1594 the new text has only its properties. If one of the sticky properties
1595 is set, then the new text "sticks" to that region and its properties
1596 depend on merging as above. If both the preceding and succeeding
1597 intervals to the new text are "sticky", then the new text retains
1598 only its properties, as if neither sticky property were set. Perhaps
1599 we should consider merging all three sets of properties onto the new
1603 graft_intervals_into_buffer (INTERVAL source
, ptrdiff_t position
,
1604 ptrdiff_t length
, struct buffer
*buffer
,
1607 INTERVAL tree
= buffer_intervals (buffer
);
1608 INTERVAL under
, over
, this;
1609 ptrdiff_t over_used
;
1611 /* If the new text has no properties, then with inheritance it
1612 becomes part of whatever interval it was inserted into.
1613 To prevent inheritance, we must clear out the properties
1614 of the newly inserted text. */
1618 if (!inherit
&& tree
&& length
> 0)
1620 XSETBUFFER (buf
, buffer
);
1621 set_text_properties_1 (make_number (position
),
1622 make_number (position
+ length
),
1624 find_interval (tree
, position
));
1626 /* Shouldn't be necessary. --Stef */
1627 buffer_balance_intervals (buffer
);
1631 eassert (length
== TOTAL_LENGTH (source
));
1633 if ((BUF_Z (buffer
) - BUF_BEG (buffer
)) == length
)
1635 /* The inserted text constitutes the whole buffer, so
1636 simply copy over the interval structure. */
1639 XSETBUFFER (buf
, buffer
);
1640 set_buffer_intervals (buffer
, reproduce_tree_obj (source
, buf
));
1641 buffer_intervals (buffer
)->position
= BUF_BEG (buffer
);
1642 eassert (buffer_intervals (buffer
)->up_obj
== 1);
1647 /* Create an interval tree in which to place a copy
1648 of the intervals of the inserted string. */
1651 XSETBUFFER (buf
, buffer
);
1652 tree
= create_root_interval (buf
);
1654 /* Paranoia -- the text has already been added, so
1655 this buffer should be of non-zero length. */
1656 eassert (TOTAL_LENGTH (tree
) > 0);
1658 this = under
= find_interval (tree
, position
);
1660 over
= find_interval (source
, interval_start_pos (source
));
1662 /* Here for insertion in the middle of an interval.
1663 Split off an equivalent interval to the right,
1664 then don't bother with it any more. */
1666 if (position
> under
->position
)
1668 INTERVAL end_unchanged
1669 = split_interval_left (this, position
- under
->position
);
1670 copy_properties (under
, end_unchanged
);
1671 under
->position
= position
;
1675 /* This call may have some effect because previous_interval may
1676 update `position' fields of intervals. Thus, don't ignore it
1677 for the moment. Someone please tell me the truth (K.Handa). */
1678 INTERVAL prev
= previous_interval (under
);
1681 /* But, this code surely has no effect. And, anyway,
1682 END_NONSTICKY_P is unreliable now. */
1683 if (prev
&& !END_NONSTICKY_P (prev
))
1688 /* Insertion is now at beginning of UNDER. */
1690 /* The inserted text "sticks" to the interval `under',
1691 which means it gets those properties.
1692 The properties of under are the result of
1693 adjust_intervals_for_insertion, so stickiness has
1694 already been taken care of. */
1696 /* OVER is the interval we are copying from next.
1697 OVER_USED says how many characters' worth of OVER
1698 have already been copied into target intervals.
1699 UNDER is the next interval in the target. */
1703 /* If UNDER is longer than OVER, split it. */
1704 if (LENGTH (over
) - over_used
< LENGTH (under
))
1706 this = split_interval_left (under
, LENGTH (over
) - over_used
);
1707 copy_properties (under
, this);
1712 /* THIS is now the interval to copy or merge into.
1713 OVER covers all of it. */
1715 merge_properties (over
, this);
1717 copy_properties (over
, this);
1719 /* If THIS and OVER end at the same place,
1720 advance OVER to a new source interval. */
1721 if (LENGTH (this) == LENGTH (over
) - over_used
)
1723 over
= next_interval (over
);
1727 /* Otherwise just record that more of OVER has been used. */
1728 over_used
+= LENGTH (this);
1730 /* Always advance to a new target interval. */
1731 under
= next_interval (this);
1734 buffer_balance_intervals (buffer
);
1737 /* Get the value of property PROP from PLIST,
1738 which is the plist of an interval.
1739 We check for direct properties, for categories with property PROP,
1740 and for PROP appearing on the default-text-properties list. */
1743 textget (Lisp_Object plist
, register Lisp_Object prop
)
1745 return lookup_char_property (plist
, prop
, 1);
1749 lookup_char_property (Lisp_Object plist
, Lisp_Object prop
, bool textprop
)
1751 Lisp_Object tail
, fallback
= Qnil
;
1753 for (tail
= plist
; CONSP (tail
); tail
= Fcdr (XCDR (tail
)))
1755 register Lisp_Object tem
;
1758 return Fcar (XCDR (tail
));
1759 if (EQ (tem
, Qcategory
))
1761 tem
= Fcar (XCDR (tail
));
1763 fallback
= Fget (tem
, prop
);
1767 if (! NILP (fallback
))
1769 /* Check for alternative properties */
1770 tail
= Fassq (prop
, Vchar_property_alias_alist
);
1774 for (; NILP (fallback
) && CONSP (tail
); tail
= XCDR (tail
))
1775 fallback
= Fplist_get (plist
, XCAR (tail
));
1778 if (textprop
&& NILP (fallback
) && CONSP (Vdefault_text_properties
))
1779 fallback
= Fplist_get (Vdefault_text_properties
, prop
);
1784 /* Set point in BUFFER "temporarily" to CHARPOS, which corresponds to
1785 byte position BYTEPOS. */
1788 temp_set_point_both (struct buffer
*buffer
,
1789 ptrdiff_t charpos
, ptrdiff_t bytepos
)
1791 /* In a single-byte buffer, the two positions must be equal. */
1792 eassert (BUF_ZV (buffer
) != BUF_ZV_BYTE (buffer
) || charpos
== bytepos
);
1794 eassert (charpos
<= bytepos
);
1795 eassert (charpos
<= BUF_ZV (buffer
) || BUF_BEGV (buffer
) <= charpos
);
1797 SET_BUF_PT_BOTH (buffer
, charpos
, bytepos
);
1800 /* Set point "temporarily", without checking any text properties. */
1803 temp_set_point (struct buffer
*buffer
, ptrdiff_t charpos
)
1805 temp_set_point_both (buffer
, charpos
,
1806 buf_charpos_to_bytepos (buffer
, charpos
));
1809 /* Set point in BUFFER to CHARPOS. If the target position is
1810 before an intangible character, move to an ok place. */
1813 set_point (ptrdiff_t charpos
)
1815 set_point_both (charpos
, buf_charpos_to_bytepos (current_buffer
, charpos
));
1818 /* Set PT from MARKER's clipped position. */
1821 set_point_from_marker (Lisp_Object marker
)
1823 if (XMARKER (marker
)->buffer
!= current_buffer
)
1824 signal_error ("Marker points into wrong buffer", marker
);
1826 (clip_to_bounds (BEGV
, marker_position (marker
), ZV
),
1827 clip_to_bounds (BEGV_BYTE
, marker_byte_position (marker
), ZV_BYTE
));
1830 /* If there's an invisible character at position POS + TEST_OFFS in the
1831 current buffer, and the invisible property has a `stickiness' such that
1832 inserting a character at position POS would inherit the property it,
1833 return POS + ADJ, otherwise return POS. If TEST_INTANG, intangibility
1834 is required as well as invisibility.
1836 TEST_OFFS should be either 0 or -1, and ADJ should be either 1 or -1.
1838 Note that `stickiness' is determined by overlay marker insertion types,
1839 if the invisible property comes from an overlay. */
1842 adjust_for_invis_intang (ptrdiff_t pos
, ptrdiff_t test_offs
, ptrdiff_t adj
,
1845 Lisp_Object invis_propval
, invis_overlay
;
1846 Lisp_Object test_pos
;
1848 if ((adj
< 0 && pos
+ adj
< BEGV
) || (adj
> 0 && pos
+ adj
> ZV
))
1849 /* POS + ADJ would be beyond the buffer bounds, so do no adjustment. */
1852 test_pos
= make_number (pos
+ test_offs
);
1855 = get_char_property_and_overlay (test_pos
, Qinvisible
, Qnil
,
1859 || ! NILP (Fget_char_property (test_pos
, Qintangible
, Qnil
)))
1860 && TEXT_PROP_MEANS_INVISIBLE (invis_propval
)
1861 /* This next test is true if the invisible property has a stickiness
1862 such that an insertion at POS would inherit it. */
1863 && (NILP (invis_overlay
)
1864 /* Invisible property is from a text-property. */
1865 ? (text_property_stickiness (Qinvisible
, make_number (pos
), Qnil
)
1866 == (test_offs
== 0 ? 1 : -1))
1867 /* Invisible property is from an overlay. */
1869 ? XMARKER (OVERLAY_START (invis_overlay
))->insertion_type
== 0
1870 : XMARKER (OVERLAY_END (invis_overlay
))->insertion_type
== 1)))
1876 /* Set point in BUFFER to CHARPOS, which corresponds to byte
1877 position BYTEPOS. If the target position is
1878 before an intangible character, move to an ok place. */
1881 set_point_both (ptrdiff_t charpos
, ptrdiff_t bytepos
)
1883 register INTERVAL to
, from
, toprev
, fromprev
;
1884 ptrdiff_t buffer_point
;
1885 ptrdiff_t old_position
= PT
;
1886 /* This ensures that we move forward past intangible text when the
1887 initial position is the same as the destination, in the rare
1888 instances where this is important, e.g. in line-move-finish
1890 bool backwards
= charpos
< old_position
;
1892 ptrdiff_t original_position
;
1894 bset_point_before_scroll (current_buffer
, Qnil
);
1899 /* In a single-byte buffer, the two positions must be equal. */
1900 eassert (ZV
!= ZV_BYTE
|| charpos
== bytepos
);
1902 /* Check this now, before checking if the buffer has any intervals.
1903 That way, we can catch conditions which break this sanity check
1904 whether or not there are intervals in the buffer. */
1905 eassert (charpos
<= ZV
&& charpos
>= BEGV
);
1907 have_overlays
= buffer_has_overlays ();
1909 /* If we have no text properties and overlays,
1910 then we can do it quickly. */
1911 if (!buffer_intervals (current_buffer
) && ! have_overlays
)
1913 temp_set_point_both (current_buffer
, charpos
, bytepos
);
1917 /* Set TO to the interval containing the char after CHARPOS,
1918 and TOPREV to the interval containing the char before CHARPOS.
1919 Either one may be null. They may be equal. */
1920 to
= find_interval (buffer_intervals (current_buffer
), charpos
);
1921 if (charpos
== BEGV
)
1923 else if (to
&& to
->position
== charpos
)
1924 toprev
= previous_interval (to
);
1928 buffer_point
= (PT
== ZV
? ZV
- 1 : PT
);
1930 /* Set FROM to the interval containing the char after PT,
1931 and FROMPREV to the interval containing the char before PT.
1932 Either one may be null. They may be equal. */
1933 /* We could cache this and save time. */
1934 from
= find_interval (buffer_intervals (current_buffer
), buffer_point
);
1935 if (buffer_point
== BEGV
)
1937 else if (from
&& from
->position
== PT
)
1938 fromprev
= previous_interval (from
);
1939 else if (buffer_point
!= PT
)
1940 fromprev
= from
, from
= 0;
1944 /* Moving within an interval. */
1945 if (to
== from
&& toprev
== fromprev
&& INTERVAL_VISIBLE_P (to
)
1948 temp_set_point_both (current_buffer
, charpos
, bytepos
);
1952 original_position
= charpos
;
1954 /* If the new position is between two intangible characters
1955 with the same intangible property value,
1956 move forward or backward until a change in that property. */
1957 if (NILP (Vinhibit_point_motion_hooks
)
1960 /* Intangibility never stops us from positioning at the beginning
1961 or end of the buffer, so don't bother checking in that case. */
1962 && charpos
!= BEGV
&& charpos
!= ZV
)
1965 Lisp_Object intangible_propval
;
1969 /* If the preceding character is both intangible and invisible,
1970 and the invisible property is `rear-sticky', perturb it so
1971 that the search starts one character earlier -- this ensures
1972 that point can never move to the end of an invisible/
1973 intangible/rear-sticky region. */
1974 charpos
= adjust_for_invis_intang (charpos
, -1, -1, 1);
1976 XSETINT (pos
, charpos
);
1978 /* If following char is intangible,
1979 skip back over all chars with matching intangible property. */
1981 intangible_propval
= Fget_char_property (pos
, Qintangible
, Qnil
);
1983 if (! NILP (intangible_propval
))
1985 while (XINT (pos
) > BEGV
1986 && EQ (Fget_char_property (make_number (XINT (pos
) - 1),
1988 intangible_propval
))
1989 pos
= Fprevious_char_property_change (pos
, Qnil
);
1991 /* Set CHARPOS from POS, and if the final intangible character
1992 that we skipped over is also invisible, and the invisible
1993 property is `front-sticky', perturb it to be one character
1994 earlier -- this ensures that point can never move to the
1995 beginning of an invisible/intangible/front-sticky region. */
1996 charpos
= adjust_for_invis_intang (XINT (pos
), 0, -1, 0);
2001 /* If the following character is both intangible and invisible,
2002 and the invisible property is `front-sticky', perturb it so
2003 that the search starts one character later -- this ensures
2004 that point can never move to the beginning of an
2005 invisible/intangible/front-sticky region. */
2006 charpos
= adjust_for_invis_intang (charpos
, 0, 1, 1);
2008 XSETINT (pos
, charpos
);
2010 /* If preceding char is intangible,
2011 skip forward over all chars with matching intangible property. */
2013 intangible_propval
= Fget_char_property (make_number (charpos
- 1),
2016 if (! NILP (intangible_propval
))
2018 while (XINT (pos
) < ZV
2019 && EQ (Fget_char_property (pos
, Qintangible
, Qnil
),
2020 intangible_propval
))
2021 pos
= Fnext_char_property_change (pos
, Qnil
);
2023 /* Set CHARPOS from POS, and if the final intangible character
2024 that we skipped over is also invisible, and the invisible
2025 property is `rear-sticky', perturb it to be one character
2026 later -- this ensures that point can never move to the
2027 end of an invisible/intangible/rear-sticky region. */
2028 charpos
= adjust_for_invis_intang (XINT (pos
), -1, 1, 0);
2032 bytepos
= buf_charpos_to_bytepos (current_buffer
, charpos
);
2035 if (charpos
!= original_position
)
2037 /* Set TO to the interval containing the char after CHARPOS,
2038 and TOPREV to the interval containing the char before CHARPOS.
2039 Either one may be null. They may be equal. */
2040 to
= find_interval (buffer_intervals (current_buffer
), charpos
);
2041 if (charpos
== BEGV
)
2043 else if (to
&& to
->position
== charpos
)
2044 toprev
= previous_interval (to
);
2049 /* Here TO is the interval after the stopping point
2050 and TOPREV is the interval before the stopping point.
2051 One or the other may be null. */
2053 temp_set_point_both (current_buffer
, charpos
, bytepos
);
2055 /* We run point-left and point-entered hooks here, if the
2056 two intervals are not equivalent. These hooks take
2057 (old_point, new_point) as arguments. */
2058 if (NILP (Vinhibit_point_motion_hooks
)
2059 && (! intervals_equal (from
, to
)
2060 || ! intervals_equal (fromprev
, toprev
)))
2062 Lisp_Object leave_after
, leave_before
, enter_after
, enter_before
;
2065 leave_before
= textget (fromprev
->plist
, Qpoint_left
);
2067 leave_before
= Qnil
;
2070 leave_after
= textget (from
->plist
, Qpoint_left
);
2075 enter_before
= textget (toprev
->plist
, Qpoint_entered
);
2077 enter_before
= Qnil
;
2080 enter_after
= textget (to
->plist
, Qpoint_entered
);
2084 if (! EQ (leave_before
, enter_before
) && !NILP (leave_before
))
2085 call2 (leave_before
, make_number (old_position
),
2086 make_number (charpos
));
2087 if (! EQ (leave_after
, enter_after
) && !NILP (leave_after
))
2088 call2 (leave_after
, make_number (old_position
),
2089 make_number (charpos
));
2091 if (! EQ (enter_before
, leave_before
) && !NILP (enter_before
))
2092 call2 (enter_before
, make_number (old_position
),
2093 make_number (charpos
));
2094 if (! EQ (enter_after
, leave_after
) && !NILP (enter_after
))
2095 call2 (enter_after
, make_number (old_position
),
2096 make_number (charpos
));
2100 /* Move point to POSITION, unless POSITION is inside an intangible
2101 segment that reaches all the way to point. */
2104 move_if_not_intangible (ptrdiff_t position
)
2107 Lisp_Object intangible_propval
;
2109 XSETINT (pos
, position
);
2111 if (! NILP (Vinhibit_point_motion_hooks
))
2112 /* If intangible is inhibited, always move point to POSITION. */
2114 else if (PT
< position
&& XINT (pos
) < ZV
)
2116 /* We want to move forward, so check the text before POSITION. */
2118 intangible_propval
= Fget_char_property (pos
,
2121 /* If following char is intangible,
2122 skip back over all chars with matching intangible property. */
2123 if (! NILP (intangible_propval
))
2124 while (XINT (pos
) > BEGV
2125 && EQ (Fget_char_property (make_number (XINT (pos
) - 1),
2127 intangible_propval
))
2128 pos
= Fprevious_char_property_change (pos
, Qnil
);
2130 else if (XINT (pos
) > BEGV
)
2132 /* We want to move backward, so check the text after POSITION. */
2134 intangible_propval
= Fget_char_property (make_number (XINT (pos
) - 1),
2137 /* If following char is intangible,
2138 skip forward over all chars with matching intangible property. */
2139 if (! NILP (intangible_propval
))
2140 while (XINT (pos
) < ZV
2141 && EQ (Fget_char_property (pos
, Qintangible
, Qnil
),
2142 intangible_propval
))
2143 pos
= Fnext_char_property_change (pos
, Qnil
);
2146 else if (position
< BEGV
)
2148 else if (position
> ZV
)
2151 /* If the whole stretch between PT and POSITION isn't intangible,
2152 try moving to POSITION (which means we actually move farther
2153 if POSITION is inside of intangible text). */
2155 if (XINT (pos
) != PT
)
2159 /* If text at position POS has property PROP, set *VAL to the property
2160 value, *START and *END to the beginning and end of a region that
2161 has the same property, and return true. Otherwise return false.
2163 OBJECT is the string or buffer to look for the property in;
2164 nil means the current buffer. */
2167 get_property_and_range (ptrdiff_t pos
, Lisp_Object prop
, Lisp_Object
*val
,
2168 ptrdiff_t *start
, ptrdiff_t *end
, Lisp_Object object
)
2170 INTERVAL i
, prev
, next
;
2173 i
= find_interval (buffer_intervals (current_buffer
), pos
);
2174 else if (BUFFERP (object
))
2175 i
= find_interval (buffer_intervals (XBUFFER (object
)), pos
);
2176 else if (STRINGP (object
))
2177 i
= find_interval (string_intervals (object
), pos
);
2181 if (!i
|| (i
->position
+ LENGTH (i
) <= pos
))
2183 *val
= textget (i
->plist
, prop
);
2187 next
= i
; /* remember it in advance */
2188 prev
= previous_interval (i
);
2190 && EQ (*val
, textget (prev
->plist
, prop
)))
2191 i
= prev
, prev
= previous_interval (prev
);
2192 *start
= i
->position
;
2194 next
= next_interval (i
);
2195 while (next
&& EQ (*val
, textget (next
->plist
, prop
)))
2196 i
= next
, next
= next_interval (next
);
2197 *end
= i
->position
+ LENGTH (i
);
2202 /* Return the proper local keymap TYPE for position POSITION in
2203 BUFFER; TYPE should be one of `keymap' or `local-map'. Use the map
2204 specified by the PROP property, if any. Otherwise, if TYPE is
2205 `local-map' use BUFFER's local map. */
2208 get_local_map (ptrdiff_t position
, struct buffer
*buffer
, Lisp_Object type
)
2210 Lisp_Object prop
, lispy_position
, lispy_buffer
;
2211 ptrdiff_t old_begv
, old_zv
, old_begv_byte
, old_zv_byte
;
2213 position
= clip_to_bounds (BUF_BEGV (buffer
), position
, BUF_ZV (buffer
));
2215 /* Ignore narrowing, so that a local map continues to be valid even if
2216 the visible region contains no characters and hence no properties. */
2217 old_begv
= BUF_BEGV (buffer
);
2218 old_zv
= BUF_ZV (buffer
);
2219 old_begv_byte
= BUF_BEGV_BYTE (buffer
);
2220 old_zv_byte
= BUF_ZV_BYTE (buffer
);
2222 SET_BUF_BEGV_BOTH (buffer
, BUF_BEG (buffer
), BUF_BEG_BYTE (buffer
));
2223 SET_BUF_ZV_BOTH (buffer
, BUF_Z (buffer
), BUF_Z_BYTE (buffer
));
2225 XSETFASTINT (lispy_position
, position
);
2226 XSETBUFFER (lispy_buffer
, buffer
);
2227 /* First check if the CHAR has any property. This is because when
2228 we click with the mouse, the mouse pointer is really pointing
2229 to the CHAR after POS. */
2230 prop
= Fget_char_property (lispy_position
, type
, lispy_buffer
);
2231 /* If not, look at the POS's properties. This is necessary because when
2232 editing a field with a `local-map' property, we want insertion at the end
2233 to obey the `local-map' property. */
2235 prop
= get_pos_property (lispy_position
, type
, lispy_buffer
);
2237 SET_BUF_BEGV_BOTH (buffer
, old_begv
, old_begv_byte
);
2238 SET_BUF_ZV_BOTH (buffer
, old_zv
, old_zv_byte
);
2240 /* Use the local map only if it is valid. */
2241 prop
= get_keymap (prop
, 0, 0);
2245 if (EQ (type
, Qkeymap
))
2248 return BVAR (buffer
, keymap
);
2251 /* Produce an interval tree reflecting the intervals in
2252 TREE from START to START + LENGTH.
2253 The new interval tree has no parent and has a starting-position of 0. */
2256 copy_intervals (INTERVAL tree
, ptrdiff_t start
, ptrdiff_t length
)
2258 register INTERVAL i
, new, t
;
2259 register ptrdiff_t got
, prevlen
;
2261 if (!tree
|| length
<= 0)
2264 i
= find_interval (tree
, start
);
2265 eassert (i
&& LENGTH (i
) > 0);
2267 /* If there is only one interval and it's the default, return nil. */
2268 if ((start
- i
->position
+ 1 + length
) < LENGTH (i
)
2269 && DEFAULT_INTERVAL_P (i
))
2272 new = make_interval ();
2274 got
= (LENGTH (i
) - (start
- i
->position
));
2275 new->total_length
= length
;
2276 eassert (TOTAL_LENGTH (new) >= 0);
2277 copy_properties (i
, new);
2281 while (got
< length
)
2283 i
= next_interval (i
);
2284 t
= split_interval_right (t
, prevlen
);
2285 copy_properties (i
, t
);
2286 prevlen
= LENGTH (i
);
2290 return balance_an_interval (new);
2293 /* Give STRING the properties of BUFFER from POSITION to LENGTH. */
2296 copy_intervals_to_string (Lisp_Object string
, struct buffer
*buffer
,
2297 ptrdiff_t position
, ptrdiff_t length
)
2299 INTERVAL interval_copy
= copy_intervals (buffer_intervals (buffer
),
2304 set_interval_object (interval_copy
, string
);
2305 set_string_intervals (string
, interval_copy
);
2308 /* Return true if strings S1 and S2 have identical properties.
2309 Assume they have identical characters. */
2312 compare_string_intervals (Lisp_Object s1
, Lisp_Object s2
)
2316 ptrdiff_t end
= SCHARS (s1
);
2318 i1
= find_interval (string_intervals (s1
), 0);
2319 i2
= find_interval (string_intervals (s2
), 0);
2323 /* Determine how far we can go before we reach the end of I1 or I2. */
2324 ptrdiff_t len1
= (i1
!= 0 ? INTERVAL_LAST_POS (i1
) : end
) - pos
;
2325 ptrdiff_t len2
= (i2
!= 0 ? INTERVAL_LAST_POS (i2
) : end
) - pos
;
2326 ptrdiff_t distance
= min (len1
, len2
);
2328 /* If we ever find a mismatch between the strings,
2330 if (! intervals_equal (i1
, i2
))
2333 /* Advance POS till the end of the shorter interval,
2334 and advance one or both interval pointers for the new position. */
2336 if (len1
== distance
)
2337 i1
= next_interval (i1
);
2338 if (len2
== distance
)
2339 i2
= next_interval (i2
);
2344 /* Recursively adjust interval I in the current buffer
2345 for setting enable_multibyte_characters to MULTI_FLAG.
2346 The range of interval I is START ... END in characters,
2347 START_BYTE ... END_BYTE in bytes. */
2350 set_intervals_multibyte_1 (INTERVAL i
, bool multi_flag
,
2351 ptrdiff_t start
, ptrdiff_t start_byte
,
2352 ptrdiff_t end
, ptrdiff_t end_byte
)
2354 /* Fix the length of this interval. */
2356 i
->total_length
= end
- start
;
2358 i
->total_length
= end_byte
- start_byte
;
2359 eassert (TOTAL_LENGTH (i
) >= 0);
2361 if (TOTAL_LENGTH (i
) == 0)
2363 delete_interval (i
);
2367 /* Recursively fix the length of the subintervals. */
2370 ptrdiff_t left_end
, left_end_byte
;
2375 left_end_byte
= start_byte
+ LEFT_TOTAL_LENGTH (i
);
2376 left_end
= BYTE_TO_CHAR (left_end_byte
);
2378 temp
= CHAR_TO_BYTE (left_end
);
2380 /* If LEFT_END_BYTE is in the middle of a character,
2381 adjust it and LEFT_END to a char boundary. */
2382 if (left_end_byte
> temp
)
2384 left_end_byte
= temp
;
2386 if (left_end_byte
< temp
)
2389 left_end_byte
= CHAR_TO_BYTE (left_end
);
2394 left_end
= start
+ LEFT_TOTAL_LENGTH (i
);
2395 left_end_byte
= CHAR_TO_BYTE (left_end
);
2398 set_intervals_multibyte_1 (i
->left
, multi_flag
, start
, start_byte
,
2399 left_end
, left_end_byte
);
2403 ptrdiff_t right_start_byte
, right_start
;
2409 right_start_byte
= end_byte
- RIGHT_TOTAL_LENGTH (i
);
2410 right_start
= BYTE_TO_CHAR (right_start_byte
);
2412 /* If RIGHT_START_BYTE is in the middle of a character,
2413 adjust it and RIGHT_START to a char boundary. */
2414 temp
= CHAR_TO_BYTE (right_start
);
2416 if (right_start_byte
< temp
)
2418 right_start_byte
= temp
;
2420 if (right_start_byte
> temp
)
2423 right_start_byte
= CHAR_TO_BYTE (right_start
);
2428 right_start
= end
- RIGHT_TOTAL_LENGTH (i
);
2429 right_start_byte
= CHAR_TO_BYTE (right_start
);
2432 set_intervals_multibyte_1 (i
->right
, multi_flag
,
2433 right_start
, right_start_byte
,
2437 /* Rounding to char boundaries can theoretically ake this interval
2438 spurious. If so, delete one child, and copy its property list
2439 to this interval. */
2440 if (LEFT_TOTAL_LENGTH (i
) + RIGHT_TOTAL_LENGTH (i
) >= TOTAL_LENGTH (i
))
2444 set_interval_plist (i
, i
->left
->plist
);
2445 (i
)->left
->total_length
= 0;
2446 delete_interval ((i
)->left
);
2450 set_interval_plist (i
, i
->right
->plist
);
2451 (i
)->right
->total_length
= 0;
2452 delete_interval ((i
)->right
);
2457 /* Update the intervals of the current buffer
2458 to fit the contents as multibyte (if MULTI_FLAG)
2459 or to fit them as non-multibyte (if not MULTI_FLAG). */
2462 set_intervals_multibyte (bool multi_flag
)
2464 INTERVAL i
= buffer_intervals (current_buffer
);
2467 set_intervals_multibyte_1 (i
, multi_flag
, BEG
, BEG_BYTE
, Z
, Z_BYTE
);