]> code.delx.au - gnu-emacs/blob - src/intervals.c
CHECK_IMPURE and PURE_P speedup
[gnu-emacs] / src / intervals.c
1 /* Code for doing intervals.
2 Copyright (C) 1993-1995, 1997-1998, 2001-2015 Free Software
3 Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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/>. */
19
20
21 /* NOTES:
22
23 Have to ensure that we can't put symbol nil on a plist, or some
24 functions may work incorrectly.
25
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.
28
29 Need to call *_left_hook when buffer is killed.
30
31 Scan for zero-length, or 0-length to see notes about handling
32 zero length interval-markers.
33
34 There are comments around about freeing intervals. It might be
35 faster to explicitly free them (put them on the free list) than
36 to GC them.
37
38 */
39
40
41 #include <config.h>
42
43 #include <intprops.h>
44 #include "lisp.h"
45 #include "intervals.h"
46 #include "character.h"
47 #include "buffer.h"
48 #include "puresize.h"
49 #include "keyboard.h"
50 #include "keymap.h"
51
52 /* Test for membership, allowing for t (actually any non-cons) to mean the
53 universal set. */
54
55 #define TMEM(sym, set) (CONSP (set) ? ! NILP (Fmemq (sym, set)) : ! NILP (set))
56
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);
60 \f
61 /* Utility functions for intervals. */
62
63 /* Use these functions to set pointer slots of struct interval. */
64
65 static void
66 set_interval_left (INTERVAL i, INTERVAL left)
67 {
68 i->left = left;
69 }
70
71 static void
72 set_interval_right (INTERVAL i, INTERVAL right)
73 {
74 i->right = right;
75 }
76
77 /* Make the parent of D be whatever the parent of S is, regardless
78 of the type. This is used when balancing an interval tree. */
79
80 static void
81 copy_interval_parent (INTERVAL d, INTERVAL s)
82 {
83 d->up = s->up;
84 d->up_obj = s->up_obj;
85 }
86
87 /* Create the root interval of some object, a buffer or string. */
88
89 INTERVAL
90 create_root_interval (Lisp_Object parent)
91 {
92 INTERVAL new;
93
94 new = make_interval ();
95
96 if (! STRINGP (parent))
97 {
98 new->total_length = (BUF_Z (XBUFFER (parent))
99 - BUF_BEG (XBUFFER (parent)));
100 eassert (TOTAL_LENGTH (new) >= 0);
101 set_buffer_intervals (XBUFFER (parent), new);
102 new->position = BEG;
103 }
104 else
105 {
106 CHECK_IMPURE (parent, XSTRING (parent));
107 new->total_length = SCHARS (parent);
108 eassert (TOTAL_LENGTH (new) >= 0);
109 set_string_intervals (parent, new);
110 new->position = 0;
111 }
112 eassert (LENGTH (new) > 0);
113
114 set_interval_object (new, parent);
115
116 return new;
117 }
118
119 /* Make the interval TARGET have exactly the properties of SOURCE. */
120
121 void
122 copy_properties (register INTERVAL source, register INTERVAL target)
123 {
124 if (DEFAULT_INTERVAL_P (source) && DEFAULT_INTERVAL_P (target))
125 return;
126
127 COPY_INTERVAL_CACHE (source, target);
128 set_interval_plist (target, Fcopy_sequence (source->plist));
129 }
130
131 /* Merge the properties of interval SOURCE into the properties
132 of interval TARGET. That is to say, each property in SOURCE
133 is added to TARGET if TARGET has no such property as yet. */
134
135 static void
136 merge_properties (register INTERVAL source, register INTERVAL target)
137 {
138 register Lisp_Object o, sym, val;
139
140 if (DEFAULT_INTERVAL_P (source) && DEFAULT_INTERVAL_P (target))
141 return;
142
143 MERGE_INTERVAL_CACHE (source, target);
144
145 o = source->plist;
146 while (CONSP (o))
147 {
148 sym = XCAR (o);
149 o = XCDR (o);
150 CHECK_CONS (o);
151
152 val = target->plist;
153 while (CONSP (val) && !EQ (XCAR (val), sym))
154 {
155 val = XCDR (val);
156 if (!CONSP (val))
157 break;
158 val = XCDR (val);
159 }
160
161 if (NILP (val))
162 {
163 val = XCAR (o);
164 set_interval_plist (target, Fcons (sym, Fcons (val, target->plist)));
165 }
166 o = XCDR (o);
167 }
168 }
169
170 /* Return true if the two intervals have the same properties. */
171
172 bool
173 intervals_equal (INTERVAL i0, INTERVAL i1)
174 {
175 Lisp_Object i0_cdr, i0_sym;
176 Lisp_Object i1_cdr, i1_val;
177
178 if (DEFAULT_INTERVAL_P (i0) && DEFAULT_INTERVAL_P (i1))
179 return true;
180
181 if (DEFAULT_INTERVAL_P (i0) || DEFAULT_INTERVAL_P (i1))
182 return false;
183
184 i0_cdr = i0->plist;
185 i1_cdr = i1->plist;
186 while (CONSP (i0_cdr) && CONSP (i1_cdr))
187 {
188 i0_sym = XCAR (i0_cdr);
189 i0_cdr = XCDR (i0_cdr);
190 if (!CONSP (i0_cdr))
191 return false;
192 i1_val = i1->plist;
193 while (CONSP (i1_val) && !EQ (XCAR (i1_val), i0_sym))
194 {
195 i1_val = XCDR (i1_val);
196 if (!CONSP (i1_val))
197 return false;
198 i1_val = XCDR (i1_val);
199 }
200
201 /* i0 has something i1 doesn't. */
202 if (EQ (i1_val, Qnil))
203 return false;
204
205 /* i0 and i1 both have sym, but it has different values in each. */
206 if (!CONSP (i1_val)
207 || (i1_val = XCDR (i1_val), !CONSP (i1_val))
208 || !EQ (XCAR (i1_val), XCAR (i0_cdr)))
209 return false;
210
211 i0_cdr = XCDR (i0_cdr);
212
213 i1_cdr = XCDR (i1_cdr);
214 if (!CONSP (i1_cdr))
215 return false;
216 i1_cdr = XCDR (i1_cdr);
217 }
218
219 /* Lengths of the two plists were equal. */
220 return (NILP (i0_cdr) && NILP (i1_cdr));
221 }
222 \f
223
224 /* Traverse an interval tree TREE, performing FUNCTION on each node.
225 No guarantee is made about the order of traversal.
226 Pass FUNCTION two args: an interval, and ARG. */
227
228 void
229 traverse_intervals_noorder (INTERVAL tree, void (*function) (INTERVAL, Lisp_Object), Lisp_Object arg)
230 {
231 /* Minimize stack usage. */
232 while (tree)
233 {
234 (*function) (tree, arg);
235 if (!tree->right)
236 tree = tree->left;
237 else
238 {
239 traverse_intervals_noorder (tree->left, function, arg);
240 tree = tree->right;
241 }
242 }
243 }
244
245 /* Traverse an interval tree TREE, performing FUNCTION on each node.
246 Pass FUNCTION two args: an interval, and ARG. */
247
248 void
249 traverse_intervals (INTERVAL tree, ptrdiff_t position,
250 void (*function) (INTERVAL, Lisp_Object), Lisp_Object arg)
251 {
252 while (tree)
253 {
254 traverse_intervals (tree->left, position, function, arg);
255 position += LEFT_TOTAL_LENGTH (tree);
256 tree->position = position;
257 (*function) (tree, arg);
258 position += LENGTH (tree); tree = tree->right;
259 }
260 }
261 \f
262 #if 0
263
264 static int icount;
265 static int idepth;
266 static int zero_length;
267
268 /* These functions are temporary, for debugging purposes only. */
269
270 INTERVAL search_interval, found_interval;
271
272 void
273 check_for_interval (INTERVAL i)
274 {
275 if (i == search_interval)
276 {
277 found_interval = i;
278 icount++;
279 }
280 }
281
282 INTERVAL
283 search_for_interval (INTERVAL i, INTERVAL tree)
284 {
285 icount = 0;
286 search_interval = i;
287 found_interval = NULL;
288 traverse_intervals_noorder (tree, &check_for_interval, Qnil);
289 return found_interval;
290 }
291
292 static void
293 inc_interval_count (INTERVAL i)
294 {
295 icount++;
296 if (LENGTH (i) == 0)
297 zero_length++;
298 if (depth > idepth)
299 idepth = depth;
300 }
301
302 int
303 count_intervals (INTERVAL i)
304 {
305 icount = 0;
306 idepth = 0;
307 zero_length = 0;
308 traverse_intervals_noorder (i, &inc_interval_count, Qnil);
309
310 return icount;
311 }
312
313 static INTERVAL
314 root_interval (INTERVAL interval)
315 {
316 register INTERVAL i = interval;
317
318 while (! ROOT_INTERVAL_P (i))
319 i = INTERVAL_PARENT (i);
320
321 return i;
322 }
323 #endif
324 \f
325 /* Assuming that a left child exists, perform the following operation:
326
327 A B
328 / \ / \
329 B => A
330 / \ / \
331 c c
332 */
333
334 static INTERVAL
335 rotate_right (INTERVAL A)
336 {
337 INTERVAL B = A->left;
338 INTERVAL c = B->right;
339 ptrdiff_t old_total = A->total_length;
340
341 eassert (old_total > 0);
342 eassert (LENGTH (A) > 0);
343 eassert (LENGTH (B) > 0);
344
345 /* Deal with any Parent of A; make it point to B. */
346 if (! ROOT_INTERVAL_P (A))
347 {
348 if (AM_LEFT_CHILD (A))
349 set_interval_left (INTERVAL_PARENT (A), B);
350 else
351 set_interval_right (INTERVAL_PARENT (A), B);
352 }
353 copy_interval_parent (B, A);
354
355 /* Make B the parent of A. */
356 set_interval_right (B, A);
357 set_interval_parent (A, B);
358
359 /* Make A point to c. */
360 set_interval_left (A, c);
361 if (c)
362 set_interval_parent (c, A);
363
364 /* A's total length is decreased by the length of B and its left child. */
365 A->total_length -= B->total_length - TOTAL_LENGTH (c);
366 eassert (TOTAL_LENGTH (A) > 0);
367 eassert (LENGTH (A) > 0);
368
369 /* B must have the same total length of A. */
370 B->total_length = old_total;
371 eassert (LENGTH (B) > 0);
372
373 return B;
374 }
375
376 /* Assuming that a right child exists, perform the following operation:
377
378 A B
379 / \ / \
380 B => A
381 / \ / \
382 c c
383 */
384
385 static INTERVAL
386 rotate_left (INTERVAL A)
387 {
388 INTERVAL B = A->right;
389 INTERVAL c = B->left;
390 ptrdiff_t old_total = A->total_length;
391
392 eassert (old_total > 0);
393 eassert (LENGTH (A) > 0);
394 eassert (LENGTH (B) > 0);
395
396 /* Deal with any parent of A; make it point to B. */
397 if (! ROOT_INTERVAL_P (A))
398 {
399 if (AM_LEFT_CHILD (A))
400 set_interval_left (INTERVAL_PARENT (A), B);
401 else
402 set_interval_right (INTERVAL_PARENT (A), B);
403 }
404 copy_interval_parent (B, A);
405
406 /* Make B the parent of A. */
407 set_interval_left (B, A);
408 set_interval_parent (A, B);
409
410 /* Make A point to c. */
411 set_interval_right (A, c);
412 if (c)
413 set_interval_parent (c, A);
414
415 /* A's total length is decreased by the length of B and its right child. */
416 A->total_length -= B->total_length - TOTAL_LENGTH (c);
417 eassert (TOTAL_LENGTH (A) > 0);
418 eassert (LENGTH (A) > 0);
419
420 /* B must have the same total length of A. */
421 B->total_length = old_total;
422 eassert (LENGTH (B) > 0);
423
424 return B;
425 }
426 \f
427 /* Balance an interval tree with the assumption that the subtrees
428 themselves are already balanced. */
429
430 static INTERVAL
431 balance_an_interval (INTERVAL i)
432 {
433 register ptrdiff_t old_diff, new_diff;
434
435 eassert (LENGTH (i) > 0);
436 eassert (TOTAL_LENGTH (i) >= LENGTH (i));
437
438 while (1)
439 {
440 old_diff = LEFT_TOTAL_LENGTH (i) - RIGHT_TOTAL_LENGTH (i);
441 if (old_diff > 0)
442 {
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)
447 break;
448 i = rotate_right (i);
449 balance_an_interval (i->right);
450 }
451 else if (old_diff < 0)
452 {
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)
457 break;
458 i = rotate_left (i);
459 balance_an_interval (i->left);
460 }
461 else
462 break;
463 }
464 return i;
465 }
466
467 /* Balance INTERVAL, potentially stuffing it back into its parent
468 Lisp Object. */
469
470 static INTERVAL
471 balance_possible_root_interval (INTERVAL interval)
472 {
473 Lisp_Object parent;
474 bool have_parent = false;
475
476 if (INTERVAL_HAS_OBJECT (interval))
477 {
478 have_parent = true;
479 GET_INTERVAL_OBJECT (parent, interval);
480 }
481 else if (!INTERVAL_HAS_PARENT (interval))
482 return interval;
483
484 interval = balance_an_interval (interval);
485
486 if (have_parent)
487 {
488 if (BUFFERP (parent))
489 set_buffer_intervals (XBUFFER (parent), interval);
490 else if (STRINGP (parent))
491 set_string_intervals (parent, interval);
492 }
493
494 return interval;
495 }
496
497 /* Balance the interval tree TREE. Balancing is by weight
498 (the amount of text). */
499
500 static INTERVAL
501 balance_intervals_internal (register INTERVAL tree)
502 {
503 /* Balance within each side. */
504 if (tree->left)
505 balance_intervals_internal (tree->left);
506 if (tree->right)
507 balance_intervals_internal (tree->right);
508 return balance_an_interval (tree);
509 }
510
511 /* Advertised interface to balance intervals. */
512
513 INTERVAL
514 balance_intervals (INTERVAL tree)
515 {
516 return tree ? balance_intervals_internal (tree) : NULL;
517 }
518
519 /* Rebalance text properties of B. */
520
521 static void
522 buffer_balance_intervals (struct buffer *b)
523 {
524 INTERVAL i;
525
526 eassert (b != NULL);
527 i = buffer_intervals (b);
528 if (i)
529 set_buffer_intervals (b, balance_an_interval (i));
530 }
531
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.
536
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
540 result.
541
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. */
544
545 INTERVAL
546 split_interval_right (INTERVAL interval, ptrdiff_t offset)
547 {
548 INTERVAL new = make_interval ();
549 ptrdiff_t position = interval->position;
550 ptrdiff_t new_length = LENGTH (interval) - offset;
551
552 new->position = position + offset;
553 set_interval_parent (new, interval);
554
555 if (NULL_RIGHT_CHILD (interval))
556 {
557 set_interval_right (interval, new);
558 new->total_length = new_length;
559 eassert (LENGTH (new) > 0);
560 }
561 else
562 {
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 balance_an_interval (new);
569 }
570
571 balance_possible_root_interval (interval);
572
573 return new;
574 }
575
576 /* Split INTERVAL into two pieces, starting the second piece at
577 character position OFFSET (counting from 0), relative to INTERVAL.
578 INTERVAL becomes the right-hand piece, and the left-hand piece
579 (first, lexicographically) is returned.
580
581 The size and position fields of the two intervals are set based upon
582 those of the original interval. The property list of the new interval
583 is reset, thus it is up to the caller to do the right thing with the
584 result.
585
586 Note that this does not change the position of INTERVAL; if it is a root,
587 it is still a root after this operation. */
588
589 INTERVAL
590 split_interval_left (INTERVAL interval, ptrdiff_t offset)
591 {
592 INTERVAL new = make_interval ();
593 ptrdiff_t new_length = offset;
594
595 new->position = interval->position;
596 interval->position = interval->position + offset;
597 set_interval_parent (new, interval);
598
599 if (NULL_LEFT_CHILD (interval))
600 {
601 set_interval_left (interval, new);
602 new->total_length = new_length;
603 eassert (LENGTH (new) > 0);
604 }
605 else
606 {
607 /* Insert the new node between INTERVAL and its left child. */
608 set_interval_left (new, interval->left);
609 set_interval_parent (new->left, new);
610 set_interval_left (interval, new);
611 new->total_length = new_length + new->left->total_length;
612 balance_an_interval (new);
613 }
614
615 balance_possible_root_interval (interval);
616
617 return new;
618 }
619 \f
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.
624
625 Don't use this function on an interval which is the child
626 of another interval! */
627
628 static int
629 interval_start_pos (INTERVAL source)
630 {
631 Lisp_Object parent;
632
633 if (!source)
634 return 0;
635
636 if (! INTERVAL_HAS_OBJECT (source))
637 return 0;
638 GET_INTERVAL_OBJECT (parent, source);
639 if (BUFFERP (parent))
640 return BUF_BEG (XBUFFER (parent));
641 return 0;
642 }
643
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.
649
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. */
653
654 INTERVAL
655 find_interval (register INTERVAL tree, register ptrdiff_t position)
656 {
657 /* The distance from the left edge of the subtree at TREE
658 to POSITION. */
659 register ptrdiff_t relative_position;
660
661 if (!tree)
662 return NULL;
663
664 relative_position = position;
665 if (INTERVAL_HAS_OBJECT (tree))
666 {
667 Lisp_Object parent;
668 GET_INTERVAL_OBJECT (parent, tree);
669 if (BUFFERP (parent))
670 relative_position -= BUF_BEG (XBUFFER (parent));
671 }
672
673 eassert (relative_position <= TOTAL_LENGTH (tree));
674
675 tree = balance_possible_root_interval (tree);
676
677 while (1)
678 {
679 eassert (tree);
680 if (relative_position < LEFT_TOTAL_LENGTH (tree))
681 {
682 tree = tree->left;
683 }
684 else if (! NULL_RIGHT_CHILD (tree)
685 && relative_position >= (TOTAL_LENGTH (tree)
686 - RIGHT_TOTAL_LENGTH (tree)))
687 {
688 relative_position -= (TOTAL_LENGTH (tree)
689 - RIGHT_TOTAL_LENGTH (tree));
690 tree = tree->right;
691 }
692 else
693 {
694 tree->position
695 = (position - relative_position /* left edge of *tree. */
696 + LEFT_TOTAL_LENGTH (tree)); /* left edge of this interval. */
697
698 return tree;
699 }
700 }
701 }
702 \f
703 /* Find the succeeding interval (lexicographically) to INTERVAL.
704 Sets the `position' field based on that of INTERVAL (see
705 find_interval). */
706
707 INTERVAL
708 next_interval (register INTERVAL interval)
709 {
710 register INTERVAL i = interval;
711 register ptrdiff_t next_position;
712
713 if (!i)
714 return NULL;
715 next_position = interval->position + LENGTH (interval);
716
717 if (! NULL_RIGHT_CHILD (i))
718 {
719 i = i->right;
720 while (! NULL_LEFT_CHILD (i))
721 i = i->left;
722
723 i->position = next_position;
724 return i;
725 }
726
727 while (! NULL_PARENT (i))
728 {
729 if (AM_LEFT_CHILD (i))
730 {
731 i = INTERVAL_PARENT (i);
732 i->position = next_position;
733 return i;
734 }
735
736 i = INTERVAL_PARENT (i);
737 }
738
739 return NULL;
740 }
741
742 /* Find the preceding interval (lexicographically) to INTERVAL.
743 Sets the `position' field based on that of INTERVAL (see
744 find_interval). */
745
746 INTERVAL
747 previous_interval (register INTERVAL interval)
748 {
749 register INTERVAL i;
750
751 if (!interval)
752 return NULL;
753
754 if (! NULL_LEFT_CHILD (interval))
755 {
756 i = interval->left;
757 while (! NULL_RIGHT_CHILD (i))
758 i = i->right;
759
760 i->position = interval->position - LENGTH (i);
761 return i;
762 }
763
764 i = interval;
765 while (! NULL_PARENT (i))
766 {
767 if (AM_RIGHT_CHILD (i))
768 {
769 i = INTERVAL_PARENT (i);
770
771 i->position = interval->position - LENGTH (i);
772 return i;
773 }
774 i = INTERVAL_PARENT (i);
775 }
776
777 return NULL;
778 }
779
780 /* Find the interval containing POS given some non-NULL INTERVAL
781 in the same tree. Note that we need to update interval->position
782 if we go down the tree.
783 To speed up the process, we assume that the ->position of
784 I and all its parents is already uptodate. */
785 INTERVAL
786 update_interval (register INTERVAL i, ptrdiff_t pos)
787 {
788 if (!i)
789 return NULL;
790
791 while (1)
792 {
793 if (pos < i->position)
794 {
795 /* Move left. */
796 if (pos >= i->position - TOTAL_LENGTH (i->left))
797 {
798 i->left->position = i->position - TOTAL_LENGTH (i->left)
799 + LEFT_TOTAL_LENGTH (i->left);
800 i = i->left; /* Move to the left child. */
801 }
802 else if (NULL_PARENT (i))
803 error ("Point before start of properties");
804 else
805 i = INTERVAL_PARENT (i);
806 continue;
807 }
808 else if (pos >= INTERVAL_LAST_POS (i))
809 {
810 /* Move right. */
811 if (pos < INTERVAL_LAST_POS (i) + TOTAL_LENGTH (i->right))
812 {
813 i->right->position = INTERVAL_LAST_POS (i)
814 + LEFT_TOTAL_LENGTH (i->right);
815 i = i->right; /* Move to the right child. */
816 }
817 else if (NULL_PARENT (i))
818 error ("Point %"pD"d after end of properties", pos);
819 else
820 i = INTERVAL_PARENT (i);
821 continue;
822 }
823 else
824 return i;
825 }
826 }
827
828 /* Effect an adjustment corresponding to the addition of LENGTH characters
829 of text. Do this by finding the interval containing POSITION in the
830 interval tree TREE, and then adjusting all of its ancestors by adding
831 LENGTH to them.
832
833 If POSITION is the first character of an interval, meaning that point
834 is actually between the two intervals, make the new text belong to
835 the interval which is "sticky".
836
837 If both intervals are "sticky", then make them belong to the left-most
838 interval. Another possibility would be to create a new interval for
839 this text, and make it have the merged properties of both ends. */
840
841 static INTERVAL
842 adjust_intervals_for_insertion (INTERVAL tree,
843 ptrdiff_t position, ptrdiff_t length)
844 {
845 INTERVAL i;
846 INTERVAL temp;
847 bool eobp = 0;
848 Lisp_Object parent;
849 ptrdiff_t offset;
850
851 eassert (TOTAL_LENGTH (tree) > 0);
852
853 GET_INTERVAL_OBJECT (parent, tree);
854 offset = (BUFFERP (parent) ? BUF_BEG (XBUFFER (parent)) : 0);
855
856 /* If inserting at point-max of a buffer, that position will be out
857 of range. Remember that buffer positions are 1-based. */
858 if (position >= TOTAL_LENGTH (tree) + offset)
859 {
860 position = TOTAL_LENGTH (tree) + offset;
861 eobp = 1;
862 }
863
864 i = find_interval (tree, position);
865
866 /* If in middle of an interval which is not sticky either way,
867 we must not just give its properties to the insertion.
868 So split this interval at the insertion point.
869
870 Originally, the if condition here was this:
871 (! (position == i->position || eobp)
872 && END_NONSTICKY_P (i)
873 && FRONT_NONSTICKY_P (i))
874 But, these macros are now unreliable because of introduction of
875 Vtext_property_default_nonsticky. So, we always check properties
876 one by one if POSITION is in middle of an interval. */
877 if (! (position == i->position || eobp))
878 {
879 Lisp_Object tail;
880 Lisp_Object front, rear;
881
882 tail = i->plist;
883
884 /* Properties font-sticky and rear-nonsticky override
885 Vtext_property_default_nonsticky. So, if they are t, we can
886 skip one by one checking of properties. */
887 rear = textget (i->plist, Qrear_nonsticky);
888 if (! CONSP (rear) && ! NILP (rear))
889 {
890 /* All properties are nonsticky. We split the interval. */
891 goto check_done;
892 }
893 front = textget (i->plist, Qfront_sticky);
894 if (! CONSP (front) && ! NILP (front))
895 {
896 /* All properties are sticky. We don't split the interval. */
897 tail = Qnil;
898 goto check_done;
899 }
900
901 /* Does any actual property pose an actual problem? We break
902 the loop if we find a nonsticky property. */
903 for (; CONSP (tail); tail = Fcdr (XCDR (tail)))
904 {
905 Lisp_Object prop, tmp;
906 prop = XCAR (tail);
907
908 /* Is this particular property front-sticky? */
909 if (CONSP (front) && ! NILP (Fmemq (prop, front)))
910 continue;
911
912 /* Is this particular property rear-nonsticky? */
913 if (CONSP (rear) && ! NILP (Fmemq (prop, rear)))
914 break;
915
916 /* Is this particular property recorded as sticky or
917 nonsticky in Vtext_property_default_nonsticky? */
918 tmp = Fassq (prop, Vtext_property_default_nonsticky);
919 if (CONSP (tmp))
920 {
921 if (NILP (tmp))
922 continue;
923 break;
924 }
925
926 /* By default, a text property is rear-sticky, thus we
927 continue the loop. */
928 }
929
930 check_done:
931 /* If any property is a real problem, split the interval. */
932 if (! NILP (tail))
933 {
934 temp = split_interval_right (i, position - i->position);
935 copy_properties (i, temp);
936 i = temp;
937 }
938 }
939
940 /* If we are positioned between intervals, check the stickiness of
941 both of them. We have to do this too, if we are at BEG or Z. */
942 if (position == i->position || eobp)
943 {
944 register INTERVAL prev;
945
946 if (position == BEG)
947 prev = 0;
948 else if (eobp)
949 {
950 prev = i;
951 i = 0;
952 }
953 else
954 prev = previous_interval (i);
955
956 /* Even if we are positioned between intervals, we default
957 to the left one if it exists. We extend it now and split
958 off a part later, if stickiness demands it. */
959 for (temp = prev ? prev : i; temp; temp = INTERVAL_PARENT_OR_NULL (temp))
960 {
961 temp->total_length += length;
962 temp = balance_possible_root_interval (temp);
963 }
964
965 /* If at least one interval has sticky properties,
966 we check the stickiness property by property.
967
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. */
975 if (1)
976 {
977 Lisp_Object pleft, pright;
978 struct interval newi;
979
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));
984
985 if (! prev) /* i.e. position == BEG */
986 {
987 if (! intervals_equal (i, &newi))
988 {
989 i = split_interval_left (i, length);
990 set_interval_plist (i, newi.plist);
991 }
992 }
993 else if (! intervals_equal (prev, &newi))
994 {
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);
999 }
1000
1001 /* We will need to update the cache here later. */
1002 }
1003 else if (! prev && ! NILP (i->plist))
1004 {
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);
1008 }
1009 }
1010
1011 /* Otherwise just extend the interval. */
1012 else
1013 {
1014 for (temp = i; temp; temp = INTERVAL_PARENT_OR_NULL (temp))
1015 {
1016 temp->total_length += length;
1017 temp = balance_possible_root_interval (temp);
1018 }
1019 }
1020
1021 return tree;
1022 }
1023
1024 /* Any property might be front-sticky on the left, rear-sticky on the left,
1025 front-sticky on the right, or rear-sticky on the right; the 16 combinations
1026 can be arranged in a matrix with rows denoting the left conditions and
1027 columns denoting the right conditions:
1028 _ __ _
1029 _ FR FR FR FR
1030 FR__ 0 1 2 3
1031 _FR 4 5 6 7
1032 FR 8 9 A B
1033 FR C D E F
1034
1035 left-props = '(front-sticky (p8 p9 pa pb pc pd pe pf)
1036 rear-nonsticky (p4 p5 p6 p7 p8 p9 pa pb)
1037 p0 L p1 L p2 L p3 L p4 L p5 L p6 L p7 L
1038 p8 L p9 L pa L pb L pc L pd L pe L pf L)
1039 right-props = '(front-sticky (p2 p3 p6 p7 pa pb pe pf)
1040 rear-nonsticky (p1 p2 p5 p6 p9 pa pd pe)
1041 p0 R p1 R p2 R p3 R p4 R p5 R p6 R p7 R
1042 p8 R p9 R pa R pb R pc R pd R pe R pf R)
1043
1044 We inherit from whoever has a sticky side facing us. If both sides
1045 do (cases 2, 3, E, and F), then we inherit from whichever side has a
1046 non-nil value for the current property. If both sides do, then we take
1047 from the left.
1048
1049 When we inherit a property, we get its stickiness as well as its value.
1050 So, when we merge the above two lists, we expect to get this:
1051
1052 result = '(front-sticky (p6 p7 pa pb pc pd pe pf)
1053 rear-nonsticky (p6 pa)
1054 p0 L p1 L p2 L p3 L p6 R p7 R
1055 pa R pb R pc L pd L pe L pf L)
1056
1057 The optimizable special cases are:
1058 left rear-nonsticky = nil, right front-sticky = nil (inherit left)
1059 left rear-nonsticky = t, right front-sticky = t (inherit right)
1060 left rear-nonsticky = t, right front-sticky = nil (inherit none)
1061 */
1062
1063 static Lisp_Object
1064 merge_properties_sticky (Lisp_Object pleft, Lisp_Object pright)
1065 {
1066 Lisp_Object props, front, rear;
1067 Lisp_Object lfront, lrear, rfront, rrear;
1068 Lisp_Object tail1, tail2, sym, lval, rval, cat;
1069 bool use_left, use_right, lpresent;
1070
1071 props = Qnil;
1072 front = Qnil;
1073 rear = Qnil;
1074 lfront = textget (pleft, Qfront_sticky);
1075 lrear = textget (pleft, Qrear_nonsticky);
1076 rfront = textget (pright, Qfront_sticky);
1077 rrear = textget (pright, Qrear_nonsticky);
1078
1079 /* Go through each element of PRIGHT. */
1080 for (tail1 = pright; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
1081 {
1082 Lisp_Object tmp;
1083
1084 sym = XCAR (tail1);
1085
1086 /* Sticky properties get special treatment. */
1087 if (EQ (sym, Qrear_nonsticky) || EQ (sym, Qfront_sticky))
1088 continue;
1089
1090 rval = Fcar (XCDR (tail1));
1091 for (tail2 = pleft; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
1092 if (EQ (sym, XCAR (tail2)))
1093 break;
1094
1095 /* Indicate whether the property is explicitly defined on the left.
1096 (We know it is defined explicitly on the right
1097 because otherwise we don't get here.) */
1098 lpresent = ! NILP (tail2);
1099 lval = (NILP (tail2) ? Qnil : Fcar (Fcdr (tail2)));
1100
1101 /* Even if lrear or rfront say nothing about the stickiness of
1102 SYM, Vtext_property_default_nonsticky may give default
1103 stickiness to SYM. */
1104 tmp = Fassq (sym, Vtext_property_default_nonsticky);
1105 use_left = (lpresent
1106 && ! (TMEM (sym, lrear)
1107 || (CONSP (tmp) && ! NILP (XCDR (tmp)))));
1108 use_right = (TMEM (sym, rfront)
1109 || (CONSP (tmp) && NILP (XCDR (tmp))));
1110 if (use_left && use_right)
1111 {
1112 if (NILP (lval))
1113 use_left = 0;
1114 else if (NILP (rval))
1115 use_right = 0;
1116 }
1117 if (use_left)
1118 {
1119 /* We build props as (value sym ...) rather than (sym value ...)
1120 because we plan to nreverse it when we're done. */
1121 props = Fcons (lval, Fcons (sym, props));
1122 if (TMEM (sym, lfront))
1123 front = Fcons (sym, front);
1124 if (TMEM (sym, lrear))
1125 rear = Fcons (sym, rear);
1126 }
1127 else if (use_right)
1128 {
1129 props = Fcons (rval, Fcons (sym, props));
1130 if (TMEM (sym, rfront))
1131 front = Fcons (sym, front);
1132 if (TMEM (sym, rrear))
1133 rear = Fcons (sym, rear);
1134 }
1135 }
1136
1137 /* Now go through each element of PLEFT. */
1138 for (tail2 = pleft; CONSP (tail2); tail2 = Fcdr (XCDR (tail2)))
1139 {
1140 Lisp_Object tmp;
1141
1142 sym = XCAR (tail2);
1143
1144 /* Sticky properties get special treatment. */
1145 if (EQ (sym, Qrear_nonsticky) || EQ (sym, Qfront_sticky))
1146 continue;
1147
1148 /* If sym is in PRIGHT, we've already considered it. */
1149 for (tail1 = pright; CONSP (tail1); tail1 = Fcdr (XCDR (tail1)))
1150 if (EQ (sym, XCAR (tail1)))
1151 break;
1152 if (! NILP (tail1))
1153 continue;
1154
1155 lval = Fcar (XCDR (tail2));
1156
1157 /* Even if lrear or rfront say nothing about the stickiness of
1158 SYM, Vtext_property_default_nonsticky may give default
1159 stickiness to SYM. */
1160 tmp = Fassq (sym, Vtext_property_default_nonsticky);
1161
1162 /* Since rval is known to be nil in this loop, the test simplifies. */
1163 if (! (TMEM (sym, lrear) || (CONSP (tmp) && ! NILP (XCDR (tmp)))))
1164 {
1165 props = Fcons (lval, Fcons (sym, props));
1166 if (TMEM (sym, lfront))
1167 front = Fcons (sym, front);
1168 }
1169 else if (TMEM (sym, rfront) || (CONSP (tmp) && NILP (XCDR (tmp))))
1170 {
1171 /* The value is nil, but we still inherit the stickiness
1172 from the right. */
1173 front = Fcons (sym, front);
1174 if (TMEM (sym, rrear))
1175 rear = Fcons (sym, rear);
1176 }
1177 }
1178 props = Fnreverse (props);
1179 if (! NILP (rear))
1180 props = Fcons (Qrear_nonsticky, Fcons (Fnreverse (rear), props));
1181
1182 cat = textget (props, Qcategory);
1183 if (! NILP (front)
1184 &&
1185 /* If we have inherited a front-stick category property that is t,
1186 we don't need to set up a detailed one. */
1187 ! (! NILP (cat) && SYMBOLP (cat)
1188 && EQ (Fget (cat, Qfront_sticky), Qt)))
1189 props = Fcons (Qfront_sticky, Fcons (Fnreverse (front), props));
1190 return props;
1191 }
1192
1193 \f
1194 /* Delete a node I from its interval tree by merging its subtrees
1195 into one subtree which is then returned. Caller is responsible for
1196 storing the resulting subtree into its parent. */
1197
1198 static INTERVAL
1199 delete_node (register INTERVAL i)
1200 {
1201 register INTERVAL migrate, this;
1202 register ptrdiff_t migrate_amt;
1203
1204 if (!i->left)
1205 return i->right;
1206 if (!i->right)
1207 return i->left;
1208
1209 migrate = i->left;
1210 migrate_amt = i->left->total_length;
1211 this = i->right;
1212 this->total_length += migrate_amt;
1213 while (this->left)
1214 {
1215 this = this->left;
1216 this->total_length += migrate_amt;
1217 }
1218 set_interval_left (this, migrate);
1219 set_interval_parent (migrate, this);
1220 eassert (LENGTH (this) > 0);
1221 eassert (LENGTH (i->right) > 0);
1222
1223 return i->right;
1224 }
1225
1226 /* Delete interval I from its tree by calling `delete_node'
1227 and properly connecting the resultant subtree.
1228
1229 I is presumed to be empty; that is, no adjustments are made
1230 for the length of I. */
1231
1232 static void
1233 delete_interval (register INTERVAL i)
1234 {
1235 register INTERVAL parent;
1236 ptrdiff_t amt = LENGTH (i);
1237
1238 eassert (amt == 0); /* Only used on zero-length intervals now. */
1239
1240 if (ROOT_INTERVAL_P (i))
1241 {
1242 Lisp_Object owner;
1243 GET_INTERVAL_OBJECT (owner, i);
1244 parent = delete_node (i);
1245 if (parent)
1246 set_interval_object (parent, owner);
1247
1248 if (BUFFERP (owner))
1249 set_buffer_intervals (XBUFFER (owner), parent);
1250 else if (STRINGP (owner))
1251 set_string_intervals (owner, parent);
1252 else
1253 emacs_abort ();
1254
1255 return;
1256 }
1257
1258 parent = INTERVAL_PARENT (i);
1259 if (AM_LEFT_CHILD (i))
1260 {
1261 set_interval_left (parent, delete_node (i));
1262 if (parent->left)
1263 set_interval_parent (parent->left, parent);
1264 }
1265 else
1266 {
1267 set_interval_right (parent, delete_node (i));
1268 if (parent->right)
1269 set_interval_parent (parent->right, parent);
1270 }
1271 }
1272 \f
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.
1277
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.
1281
1282 Do this by recursing down TREE to the interval in question, and
1283 deleting the appropriate amount of text. */
1284
1285 static ptrdiff_t
1286 interval_deletion_adjustment (register INTERVAL tree, register ptrdiff_t from,
1287 register ptrdiff_t amount)
1288 {
1289 register ptrdiff_t relative_position = from;
1290
1291 if (!tree)
1292 return 0;
1293
1294 /* Left branch. */
1295 if (relative_position < LEFT_TOTAL_LENGTH (tree))
1296 {
1297 ptrdiff_t subtract = interval_deletion_adjustment (tree->left,
1298 relative_position,
1299 amount);
1300 tree->total_length -= subtract;
1301 eassert (LENGTH (tree) > 0);
1302 return subtract;
1303 }
1304 /* Right branch. */
1305 else if (relative_position >= (TOTAL_LENGTH (tree)
1306 - RIGHT_TOTAL_LENGTH (tree)))
1307 {
1308 ptrdiff_t subtract;
1309
1310 relative_position -= (tree->total_length
1311 - RIGHT_TOTAL_LENGTH (tree));
1312 subtract = interval_deletion_adjustment (tree->right,
1313 relative_position,
1314 amount);
1315 tree->total_length -= subtract;
1316 eassert (LENGTH (tree) > 0);
1317 return subtract;
1318 }
1319 /* Here -- this node. */
1320 else
1321 {
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);
1326
1327 if (amount > my_amount)
1328 amount = my_amount;
1329
1330 tree->total_length -= amount;
1331 eassert (LENGTH (tree) >= 0);
1332 if (LENGTH (tree) == 0)
1333 delete_interval (tree);
1334
1335 return amount;
1336 }
1337
1338 /* Never reach here. */
1339 }
1340
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). */
1345
1346 static void
1347 adjust_intervals_for_deletion (struct buffer *buffer,
1348 ptrdiff_t start, ptrdiff_t length)
1349 {
1350 ptrdiff_t left_to_delete = length;
1351 INTERVAL tree = buffer_intervals (buffer);
1352 Lisp_Object parent;
1353 ptrdiff_t offset;
1354
1355 GET_INTERVAL_OBJECT (parent, tree);
1356 offset = (BUFFERP (parent) ? BUF_BEG (XBUFFER (parent)) : 0);
1357
1358 if (!tree)
1359 return;
1360
1361 eassert (start <= offset + TOTAL_LENGTH (tree)
1362 && start + length <= offset + TOTAL_LENGTH (tree));
1363
1364 if (length == TOTAL_LENGTH (tree))
1365 {
1366 set_buffer_intervals (buffer, NULL);
1367 return;
1368 }
1369
1370 if (ONLY_INTERVAL_P (tree))
1371 {
1372 tree->total_length -= length;
1373 eassert (LENGTH (tree) > 0);
1374 return;
1375 }
1376
1377 if (start > offset + TOTAL_LENGTH (tree))
1378 start = offset + TOTAL_LENGTH (tree);
1379 while (left_to_delete > 0)
1380 {
1381 left_to_delete -= interval_deletion_adjustment (tree, start - offset,
1382 left_to_delete);
1383 tree = buffer_intervals (buffer);
1384 if (left_to_delete == tree->total_length)
1385 {
1386 set_buffer_intervals (buffer, NULL);
1387 return;
1388 }
1389 }
1390 }
1391 \f
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
1395 of LENGTH. */
1396
1397 void
1398 offset_intervals (struct buffer *buffer, ptrdiff_t start, ptrdiff_t length)
1399 {
1400 if (!buffer_intervals (buffer) || length == 0)
1401 return;
1402
1403 if (length > 0)
1404 adjust_intervals_for_insertion (buffer_intervals (buffer),
1405 start, length);
1406 else
1407 adjust_intervals_for_deletion (buffer, start, -length);
1408 }
1409 \f
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
1413 interval tree.
1414
1415 IMPORTANT:
1416 The caller must verify that this is not the last (rightmost)
1417 interval. */
1418
1419 static INTERVAL
1420 merge_interval_right (register INTERVAL i)
1421 {
1422 register ptrdiff_t absorb = LENGTH (i);
1423 register INTERVAL successor;
1424
1425 /* Find the succeeding interval. */
1426 if (! NULL_RIGHT_CHILD (i)) /* It's below us. Add absorb
1427 as we descend. */
1428 {
1429 successor = i->right;
1430 while (! NULL_LEFT_CHILD (successor))
1431 {
1432 successor->total_length += absorb;
1433 eassert (LENGTH (successor) > 0);
1434 successor = successor->left;
1435 }
1436
1437 successor->total_length += absorb;
1438 eassert (LENGTH (successor) > 0);
1439 delete_interval (i);
1440 return successor;
1441 }
1442
1443 /* Zero out this interval. */
1444 i->total_length -= absorb;
1445 eassert (TOTAL_LENGTH (i) >= 0);
1446
1447 successor = i;
1448 while (! NULL_PARENT (successor)) /* It's above us. Subtract as
1449 we ascend. */
1450 {
1451 if (AM_LEFT_CHILD (successor))
1452 {
1453 successor = INTERVAL_PARENT (successor);
1454 delete_interval (i);
1455 return successor;
1456 }
1457
1458 successor = INTERVAL_PARENT (successor);
1459 successor->total_length -= absorb;
1460 eassert (LENGTH (successor) > 0);
1461 }
1462
1463 /* This must be the rightmost or last interval and cannot
1464 be merged right. The caller should have known. */
1465 emacs_abort ();
1466 }
1467 \f
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.
1471
1472 IMPORTANT:
1473 The caller must verify that this is not the first (leftmost) interval. */
1474
1475 INTERVAL
1476 merge_interval_left (register INTERVAL i)
1477 {
1478 register ptrdiff_t absorb = LENGTH (i);
1479 register INTERVAL predecessor;
1480
1481 /* Find the preceding interval. */
1482 if (! NULL_LEFT_CHILD (i)) /* It's below us. Go down,
1483 adding ABSORB as we go. */
1484 {
1485 predecessor = i->left;
1486 while (! NULL_RIGHT_CHILD (predecessor))
1487 {
1488 predecessor->total_length += absorb;
1489 eassert (LENGTH (predecessor) > 0);
1490 predecessor = predecessor->right;
1491 }
1492
1493 predecessor->total_length += absorb;
1494 eassert (LENGTH (predecessor) > 0);
1495 delete_interval (i);
1496 return predecessor;
1497 }
1498
1499 /* Zero out this interval. */
1500 i->total_length -= absorb;
1501 eassert (TOTAL_LENGTH (i) >= 0);
1502
1503 predecessor = i;
1504 while (! NULL_PARENT (predecessor)) /* It's above us. Go up,
1505 subtracting ABSORB. */
1506 {
1507 if (AM_RIGHT_CHILD (predecessor))
1508 {
1509 predecessor = INTERVAL_PARENT (predecessor);
1510 delete_interval (i);
1511 return predecessor;
1512 }
1513
1514 predecessor = INTERVAL_PARENT (predecessor);
1515 predecessor->total_length -= absorb;
1516 eassert (LENGTH (predecessor) > 0);
1517 }
1518
1519 /* This must be the leftmost or first interval and cannot
1520 be merged left. The caller should have known. */
1521 emacs_abort ();
1522 }
1523 \f
1524 /* Create a copy of SOURCE but with the default value of UP. */
1525
1526 static INTERVAL
1527 reproduce_interval (INTERVAL source)
1528 {
1529 register INTERVAL target = make_interval ();
1530
1531 eassert (LENGTH (source) > 0);
1532
1533 target->total_length = source->total_length;
1534 target->position = source->position;
1535
1536 copy_properties (source, target);
1537
1538 if (! NULL_LEFT_CHILD (source))
1539 set_interval_left (target, reproduce_tree (source->left, target));
1540 if (! NULL_RIGHT_CHILD (source))
1541 set_interval_right (target, reproduce_tree (source->right, target));
1542
1543 eassert (LENGTH (target) > 0);
1544 return target;
1545 }
1546
1547 /* Make an exact copy of interval tree SOURCE which descends from
1548 PARENT. This is done by recursing through SOURCE, copying
1549 the current interval and its properties, and then adjusting
1550 the pointers of the copy. */
1551
1552 static INTERVAL
1553 reproduce_tree (INTERVAL source, INTERVAL parent)
1554 {
1555 INTERVAL target = reproduce_interval (source);
1556 set_interval_parent (target, parent);
1557 return target;
1558 }
1559
1560 static INTERVAL
1561 reproduce_tree_obj (INTERVAL source, Lisp_Object parent)
1562 {
1563 INTERVAL target = reproduce_interval (source);
1564 set_interval_object (target, parent);
1565 return target;
1566 }
1567 \f
1568 /* Insert the intervals of SOURCE into BUFFER at POSITION.
1569 LENGTH is the length of the text in SOURCE.
1570
1571 The `position' field of the SOURCE intervals is assumed to be
1572 consistent with its parent; therefore, SOURCE must be an
1573 interval tree made with copy_interval or must be the whole
1574 tree of a buffer or a string.
1575
1576 This is used in insdel.c when inserting Lisp_Strings into the
1577 buffer. The text corresponding to SOURCE is already in the buffer
1578 when this is called. The intervals of new tree are a copy of those
1579 belonging to the string being inserted; intervals are never
1580 shared.
1581
1582 If the inserted text had no intervals associated, and we don't
1583 want to inherit the surrounding text's properties, this function
1584 simply returns -- offset_intervals should handle placing the
1585 text in the correct interval, depending on the sticky bits.
1586
1587 If the inserted text had properties (intervals), then there are two
1588 cases -- either insertion happened in the middle of some interval,
1589 or between two intervals.
1590
1591 If the text goes into the middle of an interval, then new intervals
1592 are created in the middle, and new text has the union of its properties
1593 and those of the text into which it was inserted.
1594
1595 If the text goes between two intervals, then if neither interval
1596 had its appropriate sticky property set (front_sticky, rear_sticky),
1597 the new text has only its properties. If one of the sticky properties
1598 is set, then the new text "sticks" to that region and its properties
1599 depend on merging as above. If both the preceding and succeeding
1600 intervals to the new text are "sticky", then the new text retains
1601 only its properties, as if neither sticky property were set. Perhaps
1602 we should consider merging all three sets of properties onto the new
1603 text... */
1604
1605 void
1606 graft_intervals_into_buffer (INTERVAL source, ptrdiff_t position,
1607 ptrdiff_t length, struct buffer *buffer,
1608 bool inherit)
1609 {
1610 INTERVAL tree = buffer_intervals (buffer);
1611 INTERVAL under, over, this;
1612 ptrdiff_t over_used;
1613
1614 /* If the new text has no properties, then with inheritance it
1615 becomes part of whatever interval it was inserted into.
1616 To prevent inheritance, we must clear out the properties
1617 of the newly inserted text. */
1618 if (!source)
1619 {
1620 Lisp_Object buf;
1621 if (!inherit && tree && length > 0)
1622 {
1623 XSETBUFFER (buf, buffer);
1624 set_text_properties_1 (make_number (position),
1625 make_number (position + length),
1626 Qnil, buf,
1627 find_interval (tree, position));
1628 }
1629 /* Shouldn't be necessary. --Stef */
1630 buffer_balance_intervals (buffer);
1631 return;
1632 }
1633
1634 eassert (length == TOTAL_LENGTH (source));
1635
1636 if ((BUF_Z (buffer) - BUF_BEG (buffer)) == length)
1637 {
1638 /* The inserted text constitutes the whole buffer, so
1639 simply copy over the interval structure. */
1640 Lisp_Object buf;
1641
1642 XSETBUFFER (buf, buffer);
1643 set_buffer_intervals (buffer, reproduce_tree_obj (source, buf));
1644 buffer_intervals (buffer)->position = BUF_BEG (buffer);
1645 eassert (buffer_intervals (buffer)->up_obj == 1);
1646 return;
1647 }
1648 else if (!tree)
1649 {
1650 /* Create an interval tree in which to place a copy
1651 of the intervals of the inserted string. */
1652 Lisp_Object buf;
1653
1654 XSETBUFFER (buf, buffer);
1655 tree = create_root_interval (buf);
1656 }
1657 /* Paranoia -- the text has already been added, so
1658 this buffer should be of non-zero length. */
1659 eassert (TOTAL_LENGTH (tree) > 0);
1660
1661 this = under = find_interval (tree, position);
1662 eassert (under);
1663 over = find_interval (source, interval_start_pos (source));
1664
1665 /* Here for insertion in the middle of an interval.
1666 Split off an equivalent interval to the right,
1667 then don't bother with it any more. */
1668
1669 if (position > under->position)
1670 {
1671 INTERVAL end_unchanged
1672 = split_interval_left (this, position - under->position);
1673 copy_properties (under, end_unchanged);
1674 under->position = position;
1675 }
1676 else
1677 {
1678 /* This call may have some effect because previous_interval may
1679 update `position' fields of intervals. Thus, don't ignore it
1680 for the moment. Someone please tell me the truth (K.Handa). */
1681 INTERVAL prev = previous_interval (under);
1682 (void) prev;
1683 #if 0
1684 /* But, this code surely has no effect. And, anyway,
1685 END_NONSTICKY_P is unreliable now. */
1686 if (prev && !END_NONSTICKY_P (prev))
1687 prev = 0;
1688 #endif /* 0 */
1689 }
1690
1691 /* Insertion is now at beginning of UNDER. */
1692
1693 /* The inserted text "sticks" to the interval `under',
1694 which means it gets those properties.
1695 The properties of under are the result of
1696 adjust_intervals_for_insertion, so stickiness has
1697 already been taken care of. */
1698
1699 /* OVER is the interval we are copying from next.
1700 OVER_USED says how many characters' worth of OVER
1701 have already been copied into target intervals.
1702 UNDER is the next interval in the target. */
1703 over_used = 0;
1704 while (over)
1705 {
1706 /* If UNDER is longer than OVER, split it. */
1707 if (LENGTH (over) - over_used < LENGTH (under))
1708 {
1709 this = split_interval_left (under, LENGTH (over) - over_used);
1710 copy_properties (under, this);
1711 }
1712 else
1713 this = under;
1714
1715 /* THIS is now the interval to copy or merge into.
1716 OVER covers all of it. */
1717 if (inherit)
1718 merge_properties (over, this);
1719 else
1720 copy_properties (over, this);
1721
1722 /* If THIS and OVER end at the same place,
1723 advance OVER to a new source interval. */
1724 if (LENGTH (this) == LENGTH (over) - over_used)
1725 {
1726 over = next_interval (over);
1727 over_used = 0;
1728 }
1729 else
1730 /* Otherwise just record that more of OVER has been used. */
1731 over_used += LENGTH (this);
1732
1733 /* Always advance to a new target interval. */
1734 under = next_interval (this);
1735 }
1736
1737 buffer_balance_intervals (buffer);
1738 }
1739
1740 /* Get the value of property PROP from PLIST,
1741 which is the plist of an interval.
1742 We check for direct properties, for categories with property PROP,
1743 and for PROP appearing on the default-text-properties list. */
1744
1745 Lisp_Object
1746 textget (Lisp_Object plist, register Lisp_Object prop)
1747 {
1748 return lookup_char_property (plist, prop, 1);
1749 }
1750
1751 Lisp_Object
1752 lookup_char_property (Lisp_Object plist, Lisp_Object prop, bool textprop)
1753 {
1754 Lisp_Object tail, fallback = Qnil;
1755
1756 for (tail = plist; CONSP (tail); tail = Fcdr (XCDR (tail)))
1757 {
1758 register Lisp_Object tem;
1759 tem = XCAR (tail);
1760 if (EQ (prop, tem))
1761 return Fcar (XCDR (tail));
1762 if (EQ (tem, Qcategory))
1763 {
1764 tem = Fcar (XCDR (tail));
1765 if (SYMBOLP (tem))
1766 fallback = Fget (tem, prop);
1767 }
1768 }
1769
1770 if (! NILP (fallback))
1771 return fallback;
1772 /* Check for alternative properties. */
1773 tail = Fassq (prop, Vchar_property_alias_alist);
1774 if (! NILP (tail))
1775 {
1776 tail = XCDR (tail);
1777 for (; NILP (fallback) && CONSP (tail); tail = XCDR (tail))
1778 fallback = Fplist_get (plist, XCAR (tail));
1779 }
1780
1781 if (textprop && NILP (fallback) && CONSP (Vdefault_text_properties))
1782 fallback = Fplist_get (Vdefault_text_properties, prop);
1783 return fallback;
1784 }
1785
1786 \f
1787 /* Set point in BUFFER "temporarily" to CHARPOS, which corresponds to
1788 byte position BYTEPOS. */
1789
1790 void
1791 temp_set_point_both (struct buffer *buffer,
1792 ptrdiff_t charpos, ptrdiff_t bytepos)
1793 {
1794 /* In a single-byte buffer, the two positions must be equal. */
1795 eassert (BUF_ZV (buffer) != BUF_ZV_BYTE (buffer) || charpos == bytepos);
1796
1797 eassert (charpos <= bytepos);
1798 eassert (charpos <= BUF_ZV (buffer) || BUF_BEGV (buffer) <= charpos);
1799
1800 SET_BUF_PT_BOTH (buffer, charpos, bytepos);
1801 }
1802
1803 /* Set point "temporarily", without checking any text properties. */
1804
1805 void
1806 temp_set_point (struct buffer *buffer, ptrdiff_t charpos)
1807 {
1808 temp_set_point_both (buffer, charpos,
1809 buf_charpos_to_bytepos (buffer, charpos));
1810 }
1811
1812 /* Set point in BUFFER to CHARPOS. If the target position is
1813 before an intangible character, move to an ok place. */
1814
1815 void
1816 set_point (ptrdiff_t charpos)
1817 {
1818 set_point_both (charpos, buf_charpos_to_bytepos (current_buffer, charpos));
1819 }
1820
1821 /* Set PT from MARKER's clipped position. */
1822
1823 void
1824 set_point_from_marker (Lisp_Object marker)
1825 {
1826 if (XMARKER (marker)->buffer != current_buffer)
1827 signal_error ("Marker points into wrong buffer", marker);
1828 set_point_both
1829 (clip_to_bounds (BEGV, marker_position (marker), ZV),
1830 clip_to_bounds (BEGV_BYTE, marker_byte_position (marker), ZV_BYTE));
1831 }
1832
1833 /* If there's an invisible character at position POS + TEST_OFFS in the
1834 current buffer, and the invisible property has a `stickiness' such that
1835 inserting a character at position POS would inherit the property it,
1836 return POS + ADJ, otherwise return POS. If TEST_INTANG, intangibility
1837 is required as well as invisibility.
1838
1839 TEST_OFFS should be either 0 or -1, and ADJ should be either 1 or -1.
1840
1841 Note that `stickiness' is determined by overlay marker insertion types,
1842 if the invisible property comes from an overlay. */
1843
1844 static ptrdiff_t
1845 adjust_for_invis_intang (ptrdiff_t pos, ptrdiff_t test_offs, ptrdiff_t adj,
1846 bool test_intang)
1847 {
1848 Lisp_Object invis_propval, invis_overlay;
1849 Lisp_Object test_pos;
1850
1851 if ((adj < 0 && pos + adj < BEGV) || (adj > 0 && pos + adj > ZV))
1852 /* POS + ADJ would be beyond the buffer bounds, so do no adjustment. */
1853 return pos;
1854
1855 test_pos = make_number (pos + test_offs);
1856
1857 invis_propval
1858 = get_char_property_and_overlay (test_pos, Qinvisible, Qnil,
1859 &invis_overlay);
1860
1861 if ((!test_intang
1862 || ! NILP (Fget_char_property (test_pos, Qintangible, Qnil)))
1863 && TEXT_PROP_MEANS_INVISIBLE (invis_propval)
1864 /* This next test is true if the invisible property has a stickiness
1865 such that an insertion at POS would inherit it. */
1866 && (NILP (invis_overlay)
1867 /* Invisible property is from a text-property. */
1868 ? (text_property_stickiness (Qinvisible, make_number (pos), Qnil)
1869 == (test_offs == 0 ? 1 : -1))
1870 /* Invisible property is from an overlay. */
1871 : (test_offs == 0
1872 ? XMARKER (OVERLAY_START (invis_overlay))->insertion_type == 0
1873 : XMARKER (OVERLAY_END (invis_overlay))->insertion_type == 1)))
1874 pos += adj;
1875
1876 return pos;
1877 }
1878
1879 /* Set point in BUFFER to CHARPOS, which corresponds to byte
1880 position BYTEPOS. If the target position is
1881 before an intangible character, move to an ok place. */
1882
1883 void
1884 set_point_both (ptrdiff_t charpos, ptrdiff_t bytepos)
1885 {
1886 register INTERVAL to, from, toprev, fromprev;
1887 ptrdiff_t buffer_point;
1888 ptrdiff_t old_position = PT;
1889 /* This ensures that we move forward past intangible text when the
1890 initial position is the same as the destination, in the rare
1891 instances where this is important, e.g. in line-move-finish
1892 (simple.el). */
1893 bool backwards = charpos < old_position;
1894 bool have_overlays;
1895 ptrdiff_t original_position;
1896
1897 bset_point_before_scroll (current_buffer, Qnil);
1898
1899 if (charpos == PT)
1900 return;
1901
1902 /* In a single-byte buffer, the two positions must be equal. */
1903 eassert (ZV != ZV_BYTE || charpos == bytepos);
1904
1905 /* Check this now, before checking if the buffer has any intervals.
1906 That way, we can catch conditions which break this sanity check
1907 whether or not there are intervals in the buffer. */
1908 eassert (charpos <= ZV && charpos >= BEGV);
1909
1910 have_overlays = buffer_has_overlays ();
1911
1912 /* If we have no text properties and overlays,
1913 then we can do it quickly. */
1914 if (!buffer_intervals (current_buffer) && ! have_overlays)
1915 {
1916 temp_set_point_both (current_buffer, charpos, bytepos);
1917 return;
1918 }
1919
1920 /* Set TO to the interval containing the char after CHARPOS,
1921 and TOPREV to the interval containing the char before CHARPOS.
1922 Either one may be null. They may be equal. */
1923 to = find_interval (buffer_intervals (current_buffer), charpos);
1924 if (charpos == BEGV)
1925 toprev = 0;
1926 else if (to && to->position == charpos)
1927 toprev = previous_interval (to);
1928 else
1929 toprev = to;
1930
1931 buffer_point = (PT == ZV ? ZV - 1 : PT);
1932
1933 /* Set FROM to the interval containing the char after PT,
1934 and FROMPREV to the interval containing the char before PT.
1935 Either one may be null. They may be equal. */
1936 /* We could cache this and save time. */
1937 from = find_interval (buffer_intervals (current_buffer), buffer_point);
1938 if (buffer_point == BEGV)
1939 fromprev = 0;
1940 else if (from && from->position == PT)
1941 fromprev = previous_interval (from);
1942 else if (buffer_point != PT)
1943 fromprev = from, from = 0;
1944 else
1945 fromprev = from;
1946
1947 /* Moving within an interval. */
1948 if (to == from && toprev == fromprev && INTERVAL_VISIBLE_P (to)
1949 && ! have_overlays)
1950 {
1951 temp_set_point_both (current_buffer, charpos, bytepos);
1952 return;
1953 }
1954
1955 original_position = charpos;
1956
1957 /* If the new position is between two intangible characters
1958 with the same intangible property value,
1959 move forward or backward until a change in that property. */
1960 if (NILP (Vinhibit_point_motion_hooks)
1961 && ((to && toprev)
1962 || have_overlays)
1963 /* Intangibility never stops us from positioning at the beginning
1964 or end of the buffer, so don't bother checking in that case. */
1965 && charpos != BEGV && charpos != ZV)
1966 {
1967 Lisp_Object pos;
1968 Lisp_Object intangible_propval;
1969
1970 if (backwards)
1971 {
1972 /* If the preceding character is both intangible and invisible,
1973 and the invisible property is `rear-sticky', perturb it so
1974 that the search starts one character earlier -- this ensures
1975 that point can never move to the end of an invisible/
1976 intangible/rear-sticky region. */
1977 charpos = adjust_for_invis_intang (charpos, -1, -1, 1);
1978
1979 XSETINT (pos, charpos);
1980
1981 /* If following char is intangible,
1982 skip back over all chars with matching intangible property. */
1983
1984 intangible_propval = Fget_char_property (pos, Qintangible, Qnil);
1985
1986 if (! NILP (intangible_propval))
1987 {
1988 while (XINT (pos) > BEGV
1989 && EQ (Fget_char_property (make_number (XINT (pos) - 1),
1990 Qintangible, Qnil),
1991 intangible_propval))
1992 pos = Fprevious_char_property_change (pos, Qnil);
1993
1994 /* Set CHARPOS from POS, and if the final intangible character
1995 that we skipped over is also invisible, and the invisible
1996 property is `front-sticky', perturb it to be one character
1997 earlier -- this ensures that point can never move to the
1998 beginning of an invisible/intangible/front-sticky region. */
1999 charpos = adjust_for_invis_intang (XINT (pos), 0, -1, 0);
2000 }
2001 }
2002 else
2003 {
2004 /* If the following character is both intangible and invisible,
2005 and the invisible property is `front-sticky', perturb it so
2006 that the search starts one character later -- this ensures
2007 that point can never move to the beginning of an
2008 invisible/intangible/front-sticky region. */
2009 charpos = adjust_for_invis_intang (charpos, 0, 1, 1);
2010
2011 XSETINT (pos, charpos);
2012
2013 /* If preceding char is intangible,
2014 skip forward over all chars with matching intangible property. */
2015
2016 intangible_propval = Fget_char_property (make_number (charpos - 1),
2017 Qintangible, Qnil);
2018
2019 if (! NILP (intangible_propval))
2020 {
2021 while (XINT (pos) < ZV
2022 && EQ (Fget_char_property (pos, Qintangible, Qnil),
2023 intangible_propval))
2024 pos = Fnext_char_property_change (pos, Qnil);
2025
2026 /* Set CHARPOS from POS, and if the final intangible character
2027 that we skipped over is also invisible, and the invisible
2028 property is `rear-sticky', perturb it to be one character
2029 later -- this ensures that point can never move to the
2030 end of an invisible/intangible/rear-sticky region. */
2031 charpos = adjust_for_invis_intang (XINT (pos), -1, 1, 0);
2032 }
2033 }
2034
2035 bytepos = buf_charpos_to_bytepos (current_buffer, charpos);
2036 }
2037
2038 if (charpos != original_position)
2039 {
2040 /* Set TO to the interval containing the char after CHARPOS,
2041 and TOPREV to the interval containing the char before CHARPOS.
2042 Either one may be null. They may be equal. */
2043 to = find_interval (buffer_intervals (current_buffer), charpos);
2044 if (charpos == BEGV)
2045 toprev = 0;
2046 else if (to && to->position == charpos)
2047 toprev = previous_interval (to);
2048 else
2049 toprev = to;
2050 }
2051
2052 /* Here TO is the interval after the stopping point
2053 and TOPREV is the interval before the stopping point.
2054 One or the other may be null. */
2055
2056 temp_set_point_both (current_buffer, charpos, bytepos);
2057
2058 /* We run point-left and point-entered hooks here, if the
2059 two intervals are not equivalent. These hooks take
2060 (old_point, new_point) as arguments. */
2061 if (NILP (Vinhibit_point_motion_hooks)
2062 && (! intervals_equal (from, to)
2063 || ! intervals_equal (fromprev, toprev)))
2064 {
2065 Lisp_Object leave_after, leave_before, enter_after, enter_before;
2066
2067 if (fromprev)
2068 leave_before = textget (fromprev->plist, Qpoint_left);
2069 else
2070 leave_before = Qnil;
2071
2072 if (from)
2073 leave_after = textget (from->plist, Qpoint_left);
2074 else
2075 leave_after = Qnil;
2076
2077 if (toprev)
2078 enter_before = textget (toprev->plist, Qpoint_entered);
2079 else
2080 enter_before = Qnil;
2081
2082 if (to)
2083 enter_after = textget (to->plist, Qpoint_entered);
2084 else
2085 enter_after = Qnil;
2086
2087 if (! EQ (leave_before, enter_before) && !NILP (leave_before))
2088 call2 (leave_before, make_number (old_position),
2089 make_number (charpos));
2090 if (! EQ (leave_after, enter_after) && !NILP (leave_after))
2091 call2 (leave_after, make_number (old_position),
2092 make_number (charpos));
2093
2094 if (! EQ (enter_before, leave_before) && !NILP (enter_before))
2095 call2 (enter_before, make_number (old_position),
2096 make_number (charpos));
2097 if (! EQ (enter_after, leave_after) && !NILP (enter_after))
2098 call2 (enter_after, make_number (old_position),
2099 make_number (charpos));
2100 }
2101 }
2102 \f
2103 /* Move point to POSITION, unless POSITION is inside an intangible
2104 segment that reaches all the way to point. */
2105
2106 void
2107 move_if_not_intangible (ptrdiff_t position)
2108 {
2109 Lisp_Object pos;
2110 Lisp_Object intangible_propval;
2111
2112 XSETINT (pos, position);
2113
2114 if (! NILP (Vinhibit_point_motion_hooks))
2115 /* If intangible is inhibited, always move point to POSITION. */
2116 ;
2117 else if (PT < position && XINT (pos) < ZV)
2118 {
2119 /* We want to move forward, so check the text before POSITION. */
2120
2121 intangible_propval = Fget_char_property (pos,
2122 Qintangible, Qnil);
2123
2124 /* If following char is intangible,
2125 skip back over all chars with matching intangible property. */
2126 if (! NILP (intangible_propval))
2127 while (XINT (pos) > BEGV
2128 && EQ (Fget_char_property (make_number (XINT (pos) - 1),
2129 Qintangible, Qnil),
2130 intangible_propval))
2131 pos = Fprevious_char_property_change (pos, Qnil);
2132 }
2133 else if (XINT (pos) > BEGV)
2134 {
2135 /* We want to move backward, so check the text after POSITION. */
2136
2137 intangible_propval = Fget_char_property (make_number (XINT (pos) - 1),
2138 Qintangible, Qnil);
2139
2140 /* If following char is intangible,
2141 skip forward over all chars with matching intangible property. */
2142 if (! NILP (intangible_propval))
2143 while (XINT (pos) < ZV
2144 && EQ (Fget_char_property (pos, Qintangible, Qnil),
2145 intangible_propval))
2146 pos = Fnext_char_property_change (pos, Qnil);
2147
2148 }
2149 else if (position < BEGV)
2150 position = BEGV;
2151 else if (position > ZV)
2152 position = ZV;
2153
2154 /* If the whole stretch between PT and POSITION isn't intangible,
2155 try moving to POSITION (which means we actually move farther
2156 if POSITION is inside of intangible text). */
2157
2158 if (XINT (pos) != PT)
2159 SET_PT (position);
2160 }
2161 \f
2162 /* If text at position POS has property PROP, set *VAL to the property
2163 value, *START and *END to the beginning and end of a region that
2164 has the same property, and return true. Otherwise return false.
2165
2166 OBJECT is the string or buffer to look for the property in;
2167 nil means the current buffer. */
2168
2169 bool
2170 get_property_and_range (ptrdiff_t pos, Lisp_Object prop, Lisp_Object *val,
2171 ptrdiff_t *start, ptrdiff_t *end, Lisp_Object object)
2172 {
2173 INTERVAL i, prev, next;
2174
2175 if (NILP (object))
2176 i = find_interval (buffer_intervals (current_buffer), pos);
2177 else if (BUFFERP (object))
2178 i = find_interval (buffer_intervals (XBUFFER (object)), pos);
2179 else if (STRINGP (object))
2180 i = find_interval (string_intervals (object), pos);
2181 else
2182 emacs_abort ();
2183
2184 if (!i || (i->position + LENGTH (i) <= pos))
2185 return 0;
2186 *val = textget (i->plist, prop);
2187 if (NILP (*val))
2188 return 0;
2189
2190 next = i; /* remember it in advance */
2191 prev = previous_interval (i);
2192 while (prev
2193 && EQ (*val, textget (prev->plist, prop)))
2194 i = prev, prev = previous_interval (prev);
2195 *start = i->position;
2196
2197 next = next_interval (i);
2198 while (next && EQ (*val, textget (next->plist, prop)))
2199 i = next, next = next_interval (next);
2200 *end = i->position + LENGTH (i);
2201
2202 return 1;
2203 }
2204 \f
2205 /* Return the proper local keymap TYPE for position POSITION in
2206 BUFFER; TYPE should be one of `keymap' or `local-map'. Use the map
2207 specified by the PROP property, if any. Otherwise, if TYPE is
2208 `local-map' use BUFFER's local map. */
2209
2210 Lisp_Object
2211 get_local_map (ptrdiff_t position, struct buffer *buffer, Lisp_Object type)
2212 {
2213 Lisp_Object prop, lispy_position, lispy_buffer;
2214 ptrdiff_t old_begv, old_zv, old_begv_byte, old_zv_byte;
2215
2216 position = clip_to_bounds (BUF_BEGV (buffer), position, BUF_ZV (buffer));
2217
2218 /* Ignore narrowing, so that a local map continues to be valid even if
2219 the visible region contains no characters and hence no properties. */
2220 old_begv = BUF_BEGV (buffer);
2221 old_zv = BUF_ZV (buffer);
2222 old_begv_byte = BUF_BEGV_BYTE (buffer);
2223 old_zv_byte = BUF_ZV_BYTE (buffer);
2224
2225 SET_BUF_BEGV_BOTH (buffer, BUF_BEG (buffer), BUF_BEG_BYTE (buffer));
2226 SET_BUF_ZV_BOTH (buffer, BUF_Z (buffer), BUF_Z_BYTE (buffer));
2227
2228 XSETFASTINT (lispy_position, position);
2229 XSETBUFFER (lispy_buffer, buffer);
2230 /* First check if the CHAR has any property. This is because when
2231 we click with the mouse, the mouse pointer is really pointing
2232 to the CHAR after POS. */
2233 prop = Fget_char_property (lispy_position, type, lispy_buffer);
2234 /* If not, look at the POS's properties. This is necessary because when
2235 editing a field with a `local-map' property, we want insertion at the end
2236 to obey the `local-map' property. */
2237 if (NILP (prop))
2238 prop = Fget_pos_property (lispy_position, type, lispy_buffer);
2239
2240 SET_BUF_BEGV_BOTH (buffer, old_begv, old_begv_byte);
2241 SET_BUF_ZV_BOTH (buffer, old_zv, old_zv_byte);
2242
2243 /* Use the local map only if it is valid. */
2244 prop = get_keymap (prop, 0, 0);
2245 if (CONSP (prop))
2246 return prop;
2247
2248 if (EQ (type, Qkeymap))
2249 return Qnil;
2250 else
2251 return BVAR (buffer, keymap);
2252 }
2253 \f
2254 /* Produce an interval tree reflecting the intervals in
2255 TREE from START to START + LENGTH.
2256 The new interval tree has no parent and has a starting-position of 0. */
2257
2258 INTERVAL
2259 copy_intervals (INTERVAL tree, ptrdiff_t start, ptrdiff_t length)
2260 {
2261 register INTERVAL i, new, t;
2262 register ptrdiff_t got, prevlen;
2263
2264 if (!tree || length <= 0)
2265 return NULL;
2266
2267 i = find_interval (tree, start);
2268 eassert (i && LENGTH (i) > 0);
2269
2270 /* If there is only one interval and it's the default, return nil. */
2271 if ((start - i->position + 1 + length) < LENGTH (i)
2272 && DEFAULT_INTERVAL_P (i))
2273 return NULL;
2274
2275 new = make_interval ();
2276 new->position = 0;
2277 got = (LENGTH (i) - (start - i->position));
2278 new->total_length = length;
2279 eassert (TOTAL_LENGTH (new) >= 0);
2280 copy_properties (i, new);
2281
2282 t = new;
2283 prevlen = got;
2284 while (got < length)
2285 {
2286 i = next_interval (i);
2287 t = split_interval_right (t, prevlen);
2288 copy_properties (i, t);
2289 prevlen = LENGTH (i);
2290 got += prevlen;
2291 }
2292
2293 return balance_an_interval (new);
2294 }
2295
2296 /* Give STRING the properties of BUFFER from POSITION to LENGTH. */
2297
2298 void
2299 copy_intervals_to_string (Lisp_Object string, struct buffer *buffer,
2300 ptrdiff_t position, ptrdiff_t length)
2301 {
2302 INTERVAL interval_copy = copy_intervals (buffer_intervals (buffer),
2303 position, length);
2304 if (!interval_copy)
2305 return;
2306
2307 set_interval_object (interval_copy, string);
2308 set_string_intervals (string, interval_copy);
2309 }
2310 \f
2311 /* Return true if strings S1 and S2 have identical properties.
2312 Assume they have identical characters. */
2313
2314 bool
2315 compare_string_intervals (Lisp_Object s1, Lisp_Object s2)
2316 {
2317 INTERVAL i1, i2;
2318 ptrdiff_t pos = 0;
2319 ptrdiff_t end = SCHARS (s1);
2320
2321 i1 = find_interval (string_intervals (s1), 0);
2322 i2 = find_interval (string_intervals (s2), 0);
2323
2324 while (pos < end)
2325 {
2326 /* Determine how far we can go before we reach the end of I1 or I2. */
2327 ptrdiff_t len1 = (i1 != 0 ? INTERVAL_LAST_POS (i1) : end) - pos;
2328 ptrdiff_t len2 = (i2 != 0 ? INTERVAL_LAST_POS (i2) : end) - pos;
2329 ptrdiff_t distance = min (len1, len2);
2330
2331 /* If we ever find a mismatch between the strings,
2332 they differ. */
2333 if (! intervals_equal (i1, i2))
2334 return 0;
2335
2336 /* Advance POS till the end of the shorter interval,
2337 and advance one or both interval pointers for the new position. */
2338 pos += distance;
2339 if (len1 == distance)
2340 i1 = next_interval (i1);
2341 if (len2 == distance)
2342 i2 = next_interval (i2);
2343 }
2344 return 1;
2345 }
2346 \f
2347 /* Recursively adjust interval I in the current buffer
2348 for setting enable_multibyte_characters to MULTI_FLAG.
2349 The range of interval I is START ... END in characters,
2350 START_BYTE ... END_BYTE in bytes. */
2351
2352 static void
2353 set_intervals_multibyte_1 (INTERVAL i, bool multi_flag,
2354 ptrdiff_t start, ptrdiff_t start_byte,
2355 ptrdiff_t end, ptrdiff_t end_byte)
2356 {
2357 /* Fix the length of this interval. */
2358 if (multi_flag)
2359 i->total_length = end - start;
2360 else
2361 i->total_length = end_byte - start_byte;
2362 eassert (TOTAL_LENGTH (i) >= 0);
2363
2364 if (TOTAL_LENGTH (i) == 0)
2365 {
2366 delete_interval (i);
2367 return;
2368 }
2369
2370 /* Recursively fix the length of the subintervals. */
2371 if (i->left)
2372 {
2373 ptrdiff_t left_end, left_end_byte;
2374
2375 if (multi_flag)
2376 {
2377 ptrdiff_t temp;
2378 left_end_byte = start_byte + LEFT_TOTAL_LENGTH (i);
2379 left_end = BYTE_TO_CHAR (left_end_byte);
2380
2381 temp = CHAR_TO_BYTE (left_end);
2382
2383 /* If LEFT_END_BYTE is in the middle of a character,
2384 adjust it and LEFT_END to a char boundary. */
2385 if (left_end_byte > temp)
2386 {
2387 left_end_byte = temp;
2388 }
2389 if (left_end_byte < temp)
2390 {
2391 left_end--;
2392 left_end_byte = CHAR_TO_BYTE (left_end);
2393 }
2394 }
2395 else
2396 {
2397 left_end = start + LEFT_TOTAL_LENGTH (i);
2398 left_end_byte = CHAR_TO_BYTE (left_end);
2399 }
2400
2401 set_intervals_multibyte_1 (i->left, multi_flag, start, start_byte,
2402 left_end, left_end_byte);
2403 }
2404 if (i->right)
2405 {
2406 ptrdiff_t right_start_byte, right_start;
2407
2408 if (multi_flag)
2409 {
2410 ptrdiff_t temp;
2411
2412 right_start_byte = end_byte - RIGHT_TOTAL_LENGTH (i);
2413 right_start = BYTE_TO_CHAR (right_start_byte);
2414
2415 /* If RIGHT_START_BYTE is in the middle of a character,
2416 adjust it and RIGHT_START to a char boundary. */
2417 temp = CHAR_TO_BYTE (right_start);
2418
2419 if (right_start_byte < temp)
2420 {
2421 right_start_byte = temp;
2422 }
2423 if (right_start_byte > temp)
2424 {
2425 right_start++;
2426 right_start_byte = CHAR_TO_BYTE (right_start);
2427 }
2428 }
2429 else
2430 {
2431 right_start = end - RIGHT_TOTAL_LENGTH (i);
2432 right_start_byte = CHAR_TO_BYTE (right_start);
2433 }
2434
2435 set_intervals_multibyte_1 (i->right, multi_flag,
2436 right_start, right_start_byte,
2437 end, end_byte);
2438 }
2439
2440 /* Rounding to char boundaries can theoretically make this interval
2441 spurious. If so, delete one child, and copy its property list
2442 to this interval. */
2443 if (LEFT_TOTAL_LENGTH (i) + RIGHT_TOTAL_LENGTH (i) >= TOTAL_LENGTH (i))
2444 {
2445 if ((i)->left)
2446 {
2447 set_interval_plist (i, i->left->plist);
2448 (i)->left->total_length = 0;
2449 delete_interval ((i)->left);
2450 }
2451 else
2452 {
2453 set_interval_plist (i, i->right->plist);
2454 (i)->right->total_length = 0;
2455 delete_interval ((i)->right);
2456 }
2457 }
2458 }
2459
2460 /* Update the intervals of the current buffer
2461 to fit the contents as multibyte (if MULTI_FLAG)
2462 or to fit them as non-multibyte (if not MULTI_FLAG). */
2463
2464 void
2465 set_intervals_multibyte (bool multi_flag)
2466 {
2467 INTERVAL i = buffer_intervals (current_buffer);
2468
2469 if (i)
2470 set_intervals_multibyte_1 (i, multi_flag, BEG, BEG_BYTE, Z, Z_BYTE);
2471 }