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