Merge changes made in Gnus trunk.
[emacs.git] / src / region-cache.c
blob9037b5df4b4190c2d33c3fdd1cac4d359ee299ea
1 /* Caching facts about regions of the buffer, for optimization.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1995, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <setjmp.h>
25 #include "lisp.h"
26 #include "buffer.h"
27 #include "region-cache.h"
30 /* Data structures. */
32 /* The region cache.
34 We want something that maps character positions in a buffer onto
35 values. The representation should deal well with long runs of
36 characters with the same value.
38 The tricky part: the representation should be very cheap to
39 maintain in the presence of many insertions and deletions. If the
40 overhead of maintaining the cache is too high, the speedups it
41 offers will be worthless.
44 We represent the region cache as a sorted array of struct
45 boundary's, each of which contains a buffer position and a value;
46 the value applies to all the characters after the buffer position,
47 until the position of the next boundary, or the end of the buffer.
49 The cache always has a boundary whose position is BUF_BEG, so
50 there's always a value associated with every character in the
51 buffer. Since the cache is sorted, this is always the first
52 element of the cache.
54 To facilitate the insertion and deletion of boundaries in the
55 cache, the cache has a gap, just like Emacs's text buffers do.
57 To help boundary positions float along with insertions and
58 deletions, all boundary positions before the cache gap are stored
59 relative to BUF_BEG (buf) (thus they're >= 0), and all boundary
60 positions after the gap are stored relative to BUF_Z (buf) (thus
61 they're <= 0). Look at BOUNDARY_POS to see this in action. See
62 revalidate_region_cache to see how this helps. */
64 struct boundary {
65 EMACS_INT pos;
66 int value;
69 struct region_cache {
70 /* A sorted array of locations where the known-ness of the buffer
71 changes. */
72 struct boundary *boundaries;
74 /* boundaries[gap_start ... gap_start + gap_len - 1] is the gap. */
75 EMACS_INT gap_start, gap_len;
77 /* The number of elements allocated to boundaries, not including the
78 gap. */
79 EMACS_INT cache_len;
81 /* The areas that haven't changed since the last time we cleaned out
82 invalid entries from the cache. These overlap when the buffer is
83 entirely unchanged. */
84 EMACS_INT beg_unchanged, end_unchanged;
86 /* The first and last positions in the buffer. Because boundaries
87 store their positions relative to the start (BEG) and end (Z) of
88 the buffer, knowing these positions allows us to accurately
89 interpret positions without having to pass the buffer structure
90 or its endpoints around all the time.
92 Yes, buffer_beg is always 1. It's there for symmetry with
93 buffer_end and the BEG and BUF_BEG macros. */
94 EMACS_INT buffer_beg, buffer_end;
97 /* Return the position of boundary i in cache c. */
98 #define BOUNDARY_POS(c, i) \
99 ((i) < (c)->gap_start \
100 ? (c)->buffer_beg + (c)->boundaries[(i)].pos \
101 : (c)->buffer_end + (c)->boundaries[(c)->gap_len + (i)].pos)
103 /* Return the value for text after boundary i in cache c. */
104 #define BOUNDARY_VALUE(c, i) \
105 ((i) < (c)->gap_start \
106 ? (c)->boundaries[(i)].value \
107 : (c)->boundaries[(c)->gap_len + (i)].value)
109 /* Set the value for text after boundary i in cache c to v. */
110 #define SET_BOUNDARY_VALUE(c, i, v) \
111 ((i) < (c)->gap_start \
112 ? ((c)->boundaries[(i)].value = (v))\
113 : ((c)->boundaries[(c)->gap_len + (i)].value = (v)))
116 /* How many elements to add to the gap when we resize the buffer. */
117 #define NEW_CACHE_GAP (40)
119 /* See invalidate_region_cache; if an invalidation would throw away
120 information about this many characters, call
121 revalidate_region_cache before doing the new invalidation, to
122 preserve that information, instead of throwing it away. */
123 #define PRESERVE_THRESHOLD (500)
125 static void revalidate_region_cache (struct buffer *buf, struct region_cache *c);
128 /* Interface: Allocating, initializing, and disposing of region caches. */
130 struct region_cache *
131 new_region_cache (void)
133 struct region_cache *c
134 = (struct region_cache *) xmalloc (sizeof (struct region_cache));
136 c->gap_start = 0;
137 c->gap_len = NEW_CACHE_GAP;
138 c->cache_len = 0;
139 c->boundaries =
140 (struct boundary *) xmalloc ((c->gap_len + c->cache_len)
141 * sizeof (*c->boundaries));
143 c->beg_unchanged = 0;
144 c->end_unchanged = 0;
145 c->buffer_beg = BEG;
146 c->buffer_end = BEG;
148 /* Insert the boundary for the buffer start. */
149 c->cache_len++;
150 c->gap_len--;
151 c->gap_start++;
152 c->boundaries[0].pos = 0; /* from buffer_beg */
153 c->boundaries[0].value = 0;
155 return c;
158 void
159 free_region_cache (struct region_cache *c)
161 xfree (c->boundaries);
162 xfree (c);
166 /* Finding positions in the cache. */
168 /* Return the index of the last boundary in cache C at or before POS.
169 In other words, return the boundary that specifies the value for
170 the region POS..(POS + 1).
172 This operation should be logarithmic in the number of cache
173 entries. It would be nice if it took advantage of locality of
174 reference, too, by searching entries near the last entry found. */
175 static EMACS_INT
176 find_cache_boundary (struct region_cache *c, EMACS_INT pos)
178 EMACS_INT low = 0, high = c->cache_len;
180 while (low + 1 < high)
182 /* mid is always a valid index, because low < high and ">> 1"
183 rounds down. */
184 EMACS_INT mid = (low + high) >> 1;
185 EMACS_INT boundary = BOUNDARY_POS (c, mid);
187 if (pos < boundary)
188 high = mid;
189 else
190 low = mid;
193 /* Some testing. */
194 if (BOUNDARY_POS (c, low) > pos
195 || (low + 1 < c->cache_len
196 && BOUNDARY_POS (c, low + 1) <= pos))
197 abort ();
199 return low;
204 /* Moving the cache gap around, inserting, and deleting. */
207 /* Move the gap of cache C to index POS, and make sure it has space
208 for at least MIN_SIZE boundaries. */
209 static void
210 move_cache_gap (struct region_cache *c, EMACS_INT pos, EMACS_INT min_size)
212 /* Copy these out of the cache and into registers. */
213 EMACS_INT gap_start = c->gap_start;
214 EMACS_INT gap_len = c->gap_len;
215 EMACS_INT buffer_beg = c->buffer_beg;
216 EMACS_INT buffer_end = c->buffer_end;
218 if (pos < 0
219 || pos > c->cache_len)
220 abort ();
222 /* We mustn't ever try to put the gap before the dummy start
223 boundary. That must always be start-relative. */
224 if (pos == 0)
225 abort ();
227 /* Need we move the gap right? */
228 while (gap_start < pos)
230 /* Copy one boundary from after to before the gap, and
231 convert its position to start-relative. */
232 c->boundaries[gap_start].pos
233 = (buffer_end
234 + c->boundaries[gap_start + gap_len].pos
235 - buffer_beg);
236 c->boundaries[gap_start].value
237 = c->boundaries[gap_start + gap_len].value;
238 gap_start++;
241 /* To enlarge the gap, we need to re-allocate the boundary array, and
242 then shift the area after the gap to the new end. Since the cost
243 is proportional to the amount of stuff after the gap, we do the
244 enlargement here, after a right shift but before a left shift,
245 when the portion after the gap is smallest. */
246 if (gap_len < min_size)
248 EMACS_INT i;
250 /* Always make at least NEW_CACHE_GAP elements, as long as we're
251 expanding anyway. */
252 if (min_size < NEW_CACHE_GAP)
253 min_size = NEW_CACHE_GAP;
255 c->boundaries =
256 (struct boundary *) xrealloc (c->boundaries,
257 ((min_size + c->cache_len)
258 * sizeof (*c->boundaries)));
260 /* Some systems don't provide a version of the copy routine that
261 can be trusted to shift memory upward into an overlapping
262 region. memmove isn't widely available. */
263 min_size -= gap_len;
264 for (i = c->cache_len - 1; i >= gap_start; i--)
266 c->boundaries[i + min_size].pos = c->boundaries[i + gap_len].pos;
267 c->boundaries[i + min_size].value = c->boundaries[i + gap_len].value;
270 gap_len = min_size;
273 /* Need we move the gap left? */
274 while (pos < gap_start)
276 gap_start--;
278 /* Copy one region from before to after the gap, and
279 convert its position to end-relative. */
280 c->boundaries[gap_start + gap_len].pos
281 = c->boundaries[gap_start].pos + buffer_beg - buffer_end;
282 c->boundaries[gap_start + gap_len].value
283 = c->boundaries[gap_start].value;
286 /* Assign these back into the cache. */
287 c->gap_start = gap_start;
288 c->gap_len = gap_len;
292 /* Insert a new boundary in cache C; it will have cache index INDEX,
293 and have the specified POS and VALUE. */
294 static void
295 insert_cache_boundary (struct region_cache *c, EMACS_INT index, EMACS_INT pos,
296 int value)
298 /* index must be a valid cache index. */
299 if (index < 0 || index > c->cache_len)
300 abort ();
302 /* We must never want to insert something before the dummy first
303 boundary. */
304 if (index == 0)
305 abort ();
307 /* We must only be inserting things in order. */
308 if (! (BOUNDARY_POS (c, index-1) < pos
309 && (index == c->cache_len
310 || pos < BOUNDARY_POS (c, index))))
311 abort ();
313 /* The value must be different from the ones around it. However, we
314 temporarily create boundaries that establish the same value as
315 the subsequent boundary, so we're not going to flag that case. */
316 if (BOUNDARY_VALUE (c, index-1) == value)
317 abort ();
319 move_cache_gap (c, index, 1);
321 c->boundaries[index].pos = pos - c->buffer_beg;
322 c->boundaries[index].value = value;
323 c->gap_start++;
324 c->gap_len--;
325 c->cache_len++;
329 /* Delete the i'th entry from cache C if START <= i < END. */
331 static void
332 delete_cache_boundaries (struct region_cache *c,
333 EMACS_INT start, EMACS_INT end)
335 EMACS_INT len = end - start;
337 /* Gotta be in range. */
338 if (start < 0
339 || end > c->cache_len)
340 abort ();
342 /* Gotta be in order. */
343 if (start > end)
344 abort ();
346 /* Can't delete the dummy entry. */
347 if (start == 0
348 && end >= 1)
349 abort ();
351 /* Minimize gap motion. If we're deleting nothing, do nothing. */
352 if (len == 0)
354 /* If the gap is before the region to delete, delete from the start
355 forward. */
356 else if (c->gap_start <= start)
358 move_cache_gap (c, start, 0);
359 c->gap_len += len;
361 /* If the gap is after the region to delete, delete from the end
362 backward. */
363 else if (end <= c->gap_start)
365 move_cache_gap (c, end, 0);
366 c->gap_start -= len;
367 c->gap_len += len;
369 /* If the gap is in the region to delete, just expand it. */
370 else
372 c->gap_start = start;
373 c->gap_len += len;
376 c->cache_len -= len;
381 /* Set the value for a region. */
383 /* Set the value in cache C for the region START..END to VALUE. */
384 static void
385 set_cache_region (struct region_cache *c,
386 EMACS_INT start, EMACS_INT end, int value)
388 if (start > end)
389 abort ();
390 if (start < c->buffer_beg
391 || end > c->buffer_end)
392 abort ();
394 /* Eliminate this case; then we can assume that start and end-1 are
395 both the locations of real characters in the buffer. */
396 if (start == end)
397 return;
400 /* We need to make sure that there are no boundaries in the area
401 between start to end; the whole area will have the same value,
402 so those boundaries will not be necessary.
404 Let start_ix be the cache index of the boundary governing the
405 first character of start..end, and let end_ix be the cache
406 index of the earliest boundary after the last character in
407 start..end. (This tortured terminology is intended to answer
408 all the "< or <=?" sort of questions.) */
409 EMACS_INT start_ix = find_cache_boundary (c, start);
410 EMACS_INT end_ix = find_cache_boundary (c, end - 1) + 1;
412 /* We must remember the value established by the last boundary
413 before end; if that boundary's domain stretches beyond end,
414 we'll need to create a new boundary at end, and that boundary
415 must have that remembered value. */
416 int value_at_end = BOUNDARY_VALUE (c, end_ix - 1);
418 /* Delete all boundaries strictly within start..end; this means
419 those whose indices are between start_ix (exclusive) and end_ix
420 (exclusive). */
421 delete_cache_boundaries (c, start_ix + 1, end_ix);
423 /* Make sure we have the right value established going in to
424 start..end from the left, and no unnecessary boundaries. */
425 if (BOUNDARY_POS (c, start_ix) == start)
427 /* Is this boundary necessary? If no, remove it; if yes, set
428 its value. */
429 if (start_ix > 0
430 && BOUNDARY_VALUE (c, start_ix - 1) == value)
432 delete_cache_boundaries (c, start_ix, start_ix + 1);
433 start_ix--;
435 else
436 SET_BOUNDARY_VALUE (c, start_ix, value);
438 else
440 /* Do we need to add a new boundary here? */
441 if (BOUNDARY_VALUE (c, start_ix) != value)
443 insert_cache_boundary (c, start_ix + 1, start, value);
444 start_ix++;
448 /* This is equivalent to letting end_ix float (like a buffer
449 marker does) with the insertions and deletions we may have
450 done. */
451 end_ix = start_ix + 1;
453 /* Make sure we have the correct value established as we leave
454 start..end to the right. */
455 if (end == c->buffer_end)
456 /* There is no text after start..end; nothing to do. */
458 else if (end_ix >= c->cache_len
459 || end < BOUNDARY_POS (c, end_ix))
461 /* There is no boundary at end, but we may need one. */
462 if (value_at_end != value)
463 insert_cache_boundary (c, end_ix, end, value_at_end);
465 else
467 /* There is a boundary at end; should it be there? */
468 if (value == BOUNDARY_VALUE (c, end_ix))
469 delete_cache_boundaries (c, end_ix, end_ix + 1);
476 /* Interface: Invalidating the cache. Private: Re-validating the cache. */
478 /* Indicate that a section of BUF has changed, to invalidate CACHE.
479 HEAD is the number of chars unchanged at the beginning of the buffer.
480 TAIL is the number of chars unchanged at the end of the buffer.
481 NOTE: this is *not* the same as the ending position of modified
482 region.
483 (This way of specifying regions makes more sense than absolute
484 buffer positions in the presence of insertions and deletions; the
485 args to pass are the same before and after such an operation.) */
486 void
487 invalidate_region_cache (struct buffer *buf, struct region_cache *c,
488 EMACS_INT head, EMACS_INT tail)
490 /* Let chead = c->beg_unchanged, and
491 ctail = c->end_unchanged.
492 If z-tail < beg+chead by a large amount, or
493 z-ctail < beg+head by a large amount,
495 then cutting back chead and ctail to head and tail would lose a
496 lot of information that we could preserve by revalidating the
497 cache before processing this invalidation. Losing that
498 information may be more costly than revalidating the cache now.
499 So go ahead and call revalidate_region_cache if it seems that it
500 might be worthwhile. */
501 if (((BUF_BEG (buf) + c->beg_unchanged) - (BUF_Z (buf) - tail)
502 > PRESERVE_THRESHOLD)
503 || ((BUF_BEG (buf) + head) - (BUF_Z (buf) - c->end_unchanged)
504 > PRESERVE_THRESHOLD))
505 revalidate_region_cache (buf, c);
508 if (head < c->beg_unchanged)
509 c->beg_unchanged = head;
510 if (tail < c->end_unchanged)
511 c->end_unchanged = tail;
513 /* We now know nothing about the region between the unchanged head
514 and the unchanged tail (call it the "modified region"), not even
515 its length.
517 If the modified region has shrunk in size (deletions do this),
518 then the cache may now contain boundaries originally located in
519 text that doesn't exist any more.
521 If the modified region has increased in size (insertions do
522 this), then there may now be boundaries in the modified region
523 whose positions are wrong.
525 Even calling BOUNDARY_POS on boundaries still in the unchanged
526 head or tail may well give incorrect answers now, since
527 c->buffer_beg and c->buffer_end may well be wrong now. (Well,
528 okay, c->buffer_beg never changes, so boundaries in the unchanged
529 head will still be okay. But it's the principle of the thing.)
531 So things are generally a mess.
533 But we don't clean up this mess here; that would be expensive,
534 and this function gets called every time any buffer modification
535 occurs. Rather, we can clean up everything in one swell foop,
536 accounting for all the modifications at once, by calling
537 revalidate_region_cache before we try to consult the cache the
538 next time. */
542 /* Clean out any cache entries applying to the modified region, and
543 make the positions of the remaining entries accurate again.
545 After calling this function, the mess described in the comment in
546 invalidate_region_cache is cleaned up.
548 This function operates by simply throwing away everything it knows
549 about the modified region. It doesn't care exactly which
550 insertions and deletions took place; it just tosses it all.
552 For example, if you insert a single character at the beginning of
553 the buffer, and a single character at the end of the buffer (for
554 example), without calling this function in between the two
555 insertions, then the entire cache will be freed of useful
556 information. On the other hand, if you do manage to call this
557 function in between the two insertions, then the modified regions
558 will be small in both cases, no information will be tossed, and the
559 cache will know that it doesn't have knowledge of the first and
560 last characters any more.
562 Calling this function may be expensive; it does binary searches in
563 the cache, and causes cache gap motion. */
565 static void
566 revalidate_region_cache (struct buffer *buf, struct region_cache *c)
568 /* The boundaries now in the cache are expressed relative to the
569 buffer_beg and buffer_end values stored in the cache. Now,
570 buffer_beg and buffer_end may not be the same as BUF_BEG (buf)
571 and BUF_Z (buf), so we have two different "bases" to deal with
572 --- the cache's, and the buffer's. */
574 /* If the entire buffer is still valid, don't waste time. Yes, this
575 should be a >, not a >=; think about what beg_unchanged and
576 end_unchanged get set to when the only change has been an
577 insertion. */
578 if (c->buffer_beg + c->beg_unchanged
579 > c->buffer_end - c->end_unchanged)
580 return;
582 /* If all the text we knew about as of the last cache revalidation
583 is still there, then all of the information in the cache is still
584 valid. Because c->buffer_beg and c->buffer_end are out-of-date,
585 the modified region appears from the cache's point of view to be
586 a null region located someplace in the buffer.
588 Now, invalidating that empty string will have no actual affect on
589 the cache; instead, we need to update the cache's basis first
590 (which will give the modified region the same size in the cache
591 as it has in the buffer), and then invalidate the modified
592 region. */
593 if (c->buffer_beg + c->beg_unchanged
594 == c->buffer_end - c->end_unchanged)
596 /* Move the gap so that all the boundaries in the unchanged head
597 are expressed beg-relative, and all the boundaries in the
598 unchanged tail are expressed end-relative. That done, we can
599 plug in the new buffer beg and end, and all the positions
600 will be accurate.
602 The boundary which has jurisdiction over the modified region
603 should be left before the gap. */
604 move_cache_gap (c,
605 (find_cache_boundary (c, (c->buffer_beg
606 + c->beg_unchanged))
607 + 1),
610 c->buffer_beg = BUF_BEG (buf);
611 c->buffer_end = BUF_Z (buf);
613 /* Now that the cache's basis has been changed, the modified
614 region actually takes up some space in the cache, so we can
615 invalidate it. */
616 set_cache_region (c,
617 c->buffer_beg + c->beg_unchanged,
618 c->buffer_end - c->end_unchanged,
622 /* Otherwise, there is a non-empty region in the cache which
623 corresponds to the modified region of the buffer. */
624 else
626 EMACS_INT modified_ix;
628 /* These positions are correct, relative to both the cache basis
629 and the buffer basis. */
630 set_cache_region (c,
631 c->buffer_beg + c->beg_unchanged,
632 c->buffer_end - c->end_unchanged,
635 /* Now the cache contains only boundaries that are in the
636 unchanged head and tail; we've disposed of any boundaries
637 whose positions we can't be sure of given the information
638 we've saved.
640 If we put the cache gap between the unchanged head and the
641 unchanged tail, we can adjust all the boundary positions at
642 once, simply by setting buffer_beg and buffer_end.
644 The boundary which has jurisdiction over the modified region
645 should be left before the gap. */
646 modified_ix =
647 find_cache_boundary (c, (c->buffer_beg + c->beg_unchanged)) + 1;
648 move_cache_gap (c, modified_ix, 0);
650 c->buffer_beg = BUF_BEG (buf);
651 c->buffer_end = BUF_Z (buf);
653 /* Now, we may have shrunk the buffer when we changed the basis,
654 and brought the boundaries we created for the start and end
655 of the modified region together, giving them the same
656 position. If that's the case, we should collapse them into
657 one boundary. Or we may even delete them both, if the values
658 before and after them are the same. */
659 if (modified_ix < c->cache_len
660 && (BOUNDARY_POS (c, modified_ix - 1)
661 == BOUNDARY_POS (c, modified_ix)))
663 int value_after = BOUNDARY_VALUE (c, modified_ix);
665 /* Should we remove both of the boundaries? Yes, if the
666 latter boundary is now establishing the same value that
667 the former boundary's predecessor does. */
668 if (modified_ix - 1 > 0
669 && value_after == BOUNDARY_VALUE (c, modified_ix - 2))
670 delete_cache_boundaries (c, modified_ix - 1, modified_ix + 1);
671 else
673 /* We do need a boundary here; collapse the two
674 boundaries into one. */
675 SET_BOUNDARY_VALUE (c, modified_ix - 1, value_after);
676 delete_cache_boundaries (c, modified_ix, modified_ix + 1);
681 /* Now the entire cache is valid. */
682 c->beg_unchanged
683 = c->end_unchanged
684 = c->buffer_end - c->buffer_beg;
688 /* Interface: Adding information to the cache. */
690 /* Assert that the region of BUF between START and END (absolute
691 buffer positions) is "known," for the purposes of CACHE (e.g. "has
692 no newlines", in the case of the line cache). */
693 void
694 know_region_cache (struct buffer *buf, struct region_cache *c,
695 EMACS_INT start, EMACS_INT end)
697 revalidate_region_cache (buf, c);
699 set_cache_region (c, start, end, 1);
703 /* Interface: using the cache. */
705 /* Return true if the text immediately after POS in BUF is known, for
706 the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest
707 position after POS where the knownness changes. */
709 region_cache_forward (struct buffer *buf, struct region_cache *c,
710 EMACS_INT pos, EMACS_INT *next)
712 revalidate_region_cache (buf, c);
715 EMACS_INT i = find_cache_boundary (c, pos);
716 int i_value = BOUNDARY_VALUE (c, i);
717 EMACS_INT j;
719 /* Beyond the end of the buffer is unknown, by definition. */
720 if (pos >= BUF_Z (buf))
722 if (next) *next = BUF_Z (buf);
723 i_value = 0;
725 else if (next)
727 /* Scan forward from i to find the next differing position. */
728 for (j = i + 1; j < c->cache_len; j++)
729 if (BOUNDARY_VALUE (c, j) != i_value)
730 break;
732 if (j < c->cache_len)
733 *next = BOUNDARY_POS (c, j);
734 else
735 *next = BUF_Z (buf);
738 return i_value;
742 /* Return true if the text immediately before POS in BUF is known, for
743 the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest
744 position before POS where the knownness changes. */
745 int region_cache_backward (struct buffer *buf, struct region_cache *c,
746 EMACS_INT pos, EMACS_INT *next)
748 revalidate_region_cache (buf, c);
750 /* Before the beginning of the buffer is unknown, by
751 definition. */
752 if (pos <= BUF_BEG (buf))
754 if (next) *next = BUF_BEG (buf);
755 return 0;
759 EMACS_INT i = find_cache_boundary (c, pos - 1);
760 int i_value = BOUNDARY_VALUE (c, i);
761 EMACS_INT j;
763 if (next)
765 /* Scan backward from i to find the next differing position. */
766 for (j = i - 1; j >= 0; j--)
767 if (BOUNDARY_VALUE (c, j) != i_value)
768 break;
770 if (j >= 0)
771 *next = BOUNDARY_POS (c, j + 1);
772 else
773 *next = BUF_BEG (buf);
776 return i_value;
781 /* Debugging: pretty-print a cache to the standard error output. */
783 void
784 pp_cache (struct region_cache *c)
786 int i;
787 EMACS_INT beg_u = c->buffer_beg + c->beg_unchanged;
788 EMACS_INT end_u = c->buffer_end - c->end_unchanged;
790 fprintf (stderr,
791 "basis: %ld..%ld modified: %ld..%ld\n",
792 (long)c->buffer_beg, (long)c->buffer_end,
793 (long)beg_u, (long)end_u);
795 for (i = 0; i < c->cache_len; i++)
797 EMACS_INT pos = BOUNDARY_POS (c, i);
799 putc (((pos < beg_u) ? 'v'
800 : (pos == beg_u) ? '-'
801 : ' '),
802 stderr);
803 putc (((pos > end_u) ? '^'
804 : (pos == end_u) ? '-'
805 : ' '),
806 stderr);
807 fprintf (stderr, "%ld : %d\n", (long)pos, BOUNDARY_VALUE (c, i));
811 /* arch-tag: 98c29f3f-2ca2-4e3a-92f0-f2249200a17d
812 (do not change this comment) */