* lisp/emacs-lisp/cl-macs.el (cl-transform-lambda): Don't add spurious
[bpt/emacs.git] / src / region-cache.c
CommitLineData
b9c5136f 1/* Caching facts about regions of the buffer, for optimization.
95df8112 2
acaf905b 3Copyright (C) 1985-1989, 1993, 1995, 2001-2012
95df8112 4 Free Software Foundation, Inc.
b9c5136f
KH
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
b9c5136f 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
b9c5136f
KH
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
b9c5136f
KH
20
21
22#include <config.h>
5890e9f7 23#include <stdio.h>
d7306fe6 24#include <setjmp.h>
5890e9f7 25
b9c5136f
KH
26#include "lisp.h"
27#include "buffer.h"
28#include "region-cache.h"
29
b9c5136f
KH
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
65struct boundary {
0065d054 66 ptrdiff_t pos;
b9c5136f
KH
67 int value;
68};
69
70struct 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. */
0065d054 76 ptrdiff_t gap_start, gap_len;
b9c5136f
KH
77
78 /* The number of elements allocated to boundaries, not including the
79 gap. */
0065d054 80 ptrdiff_t cache_len;
b9c5136f
KH
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. */
0065d054 85 ptrdiff_t beg_unchanged, end_unchanged;
b9c5136f
KH
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. */
0065d054 95 ptrdiff_t buffer_beg, buffer_end;
b9c5136f
KH
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
971de7fb 126static void revalidate_region_cache (struct buffer *buf, struct region_cache *c);
b9c5136f
KH
127
128\f
129/* Interface: Allocating, initializing, and disposing of region caches. */
130
131struct region_cache *
971de7fb 132new_region_cache (void)
b9c5136f 133{
177c0ea7 134 struct region_cache *c
b9c5136f
KH
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;
b0ead4a8
SM
146 c->buffer_beg = BEG;
147 c->buffer_end = BEG;
b9c5136f
KH
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
159void
971de7fb 160free_region_cache (struct region_cache *c)
b9c5136f
KH
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. */
0065d054
PE
176static ptrdiff_t
177find_cache_boundary (struct region_cache *c, ptrdiff_t pos)
b9c5136f 178{
0065d054 179 ptrdiff_t low = 0, high = c->cache_len;
b9c5136f
KH
180
181 while (low + 1 < high)
182 {
183 /* mid is always a valid index, because low < high and ">> 1"
184 rounds down. */
0065d054
PE
185 ptrdiff_t mid = (low >> 1) + (high >> 1) + (low & high & 1);
186 ptrdiff_t boundary = BOUNDARY_POS (c, mid);
b9c5136f
KH
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. */
210static void
0065d054 211move_cache_gap (struct region_cache *c, ptrdiff_t pos, ptrdiff_t min_size)
b9c5136f
KH
212{
213 /* Copy these out of the cache and into registers. */
0065d054
PE
214 ptrdiff_t gap_start = c->gap_start;
215 ptrdiff_t gap_len = c->gap_len;
216 ptrdiff_t buffer_beg = c->buffer_beg;
217 ptrdiff_t buffer_end = c->buffer_end;
b9c5136f
KH
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 {
0065d054 249 ptrdiff_t i;
b9c5136f
KH
250
251 c->boundaries =
0065d054
PE
252 xpalloc (c->boundaries, &c->cache_len, min_size, -1,
253 sizeof *c->boundaries);
b9c5136f
KH
254
255 /* Some systems don't provide a version of the copy routine that
256 can be trusted to shift memory upward into an overlapping
257 region. memmove isn't widely available. */
258 min_size -= gap_len;
259 for (i = c->cache_len - 1; i >= gap_start; i--)
260 {
261 c->boundaries[i + min_size].pos = c->boundaries[i + gap_len].pos;
262 c->boundaries[i + min_size].value = c->boundaries[i + gap_len].value;
263 }
264
265 gap_len = min_size;
266 }
267
268 /* Need we move the gap left? */
269 while (pos < gap_start)
270 {
271 gap_start--;
272
273 /* Copy one region from before to after the gap, and
274 convert its position to end-relative. */
275 c->boundaries[gap_start + gap_len].pos
276 = c->boundaries[gap_start].pos + buffer_beg - buffer_end;
277 c->boundaries[gap_start + gap_len].value
278 = c->boundaries[gap_start].value;
279 }
280
281 /* Assign these back into the cache. */
282 c->gap_start = gap_start;
283 c->gap_len = gap_len;
284}
285
286
c4fc4e30 287/* Insert a new boundary in cache C; it will have cache index I,
b9c5136f
KH
288 and have the specified POS and VALUE. */
289static void
0065d054 290insert_cache_boundary (struct region_cache *c, ptrdiff_t i, ptrdiff_t pos,
c098fdb8 291 int value)
b9c5136f 292{
c4fc4e30
PE
293 /* i must be a valid cache index. */
294 if (i < 0 || i > c->cache_len)
b9c5136f
KH
295 abort ();
296
297 /* We must never want to insert something before the dummy first
298 boundary. */
c4fc4e30 299 if (i == 0)
b9c5136f
KH
300 abort ();
301
302 /* We must only be inserting things in order. */
c4fc4e30
PE
303 if (! (BOUNDARY_POS (c, i - 1) < pos
304 && (i == c->cache_len
305 || pos < BOUNDARY_POS (c, i))))
b9c5136f
KH
306 abort ();
307
308 /* The value must be different from the ones around it. However, we
309 temporarily create boundaries that establish the same value as
310 the subsequent boundary, so we're not going to flag that case. */
c4fc4e30 311 if (BOUNDARY_VALUE (c, i - 1) == value)
b9c5136f
KH
312 abort ();
313
c4fc4e30 314 move_cache_gap (c, i, 1);
b9c5136f 315
c4fc4e30
PE
316 c->boundaries[i].pos = pos - c->buffer_beg;
317 c->boundaries[i].value = value;
b9c5136f
KH
318 c->gap_start++;
319 c->gap_len--;
320 c->cache_len++;
321}
322
323
324/* Delete the i'th entry from cache C if START <= i < END. */
325
326static void
c098fdb8 327delete_cache_boundaries (struct region_cache *c,
0065d054 328 ptrdiff_t start, ptrdiff_t end)
b9c5136f 329{
0065d054 330 ptrdiff_t len = end - start;
b9c5136f
KH
331
332 /* Gotta be in range. */
333 if (start < 0
334 || end > c->cache_len)
335 abort ();
336
337 /* Gotta be in order. */
338 if (start > end)
339 abort ();
340
341 /* Can't delete the dummy entry. */
342 if (start == 0
343 && end >= 1)
344 abort ();
345
346 /* Minimize gap motion. If we're deleting nothing, do nothing. */
347 if (len == 0)
348 ;
349 /* If the gap is before the region to delete, delete from the start
350 forward. */
351 else if (c->gap_start <= start)
352 {
353 move_cache_gap (c, start, 0);
354 c->gap_len += len;
355 }
356 /* If the gap is after the region to delete, delete from the end
357 backward. */
358 else if (end <= c->gap_start)
359 {
360 move_cache_gap (c, end, 0);
361 c->gap_start -= len;
362 c->gap_len += len;
363 }
364 /* If the gap is in the region to delete, just expand it. */
365 else
366 {
367 c->gap_start = start;
368 c->gap_len += len;
369 }
370
371 c->cache_len -= len;
372}
177c0ea7 373
b9c5136f
KH
374
375\f
376/* Set the value for a region. */
377
378/* Set the value in cache C for the region START..END to VALUE. */
379static void
c098fdb8 380set_cache_region (struct region_cache *c,
0065d054 381 ptrdiff_t start, ptrdiff_t end, int value)
b9c5136f
KH
382{
383 if (start > end)
384 abort ();
385 if (start < c->buffer_beg
386 || end > c->buffer_end)
387 abort ();
388
389 /* Eliminate this case; then we can assume that start and end-1 are
390 both the locations of real characters in the buffer. */
391 if (start == end)
392 return;
177c0ea7 393
b9c5136f
KH
394 {
395 /* We need to make sure that there are no boundaries in the area
396 between start to end; the whole area will have the same value,
397 so those boundaries will not be necessary.
177c0ea7 398
b9c5136f
KH
399 Let start_ix be the cache index of the boundary governing the
400 first character of start..end, and let end_ix be the cache
401 index of the earliest boundary after the last character in
402 start..end. (This tortured terminology is intended to answer
403 all the "< or <=?" sort of questions.) */
0065d054
PE
404 ptrdiff_t start_ix = find_cache_boundary (c, start);
405 ptrdiff_t end_ix = find_cache_boundary (c, end - 1) + 1;
b9c5136f
KH
406
407 /* We must remember the value established by the last boundary
408 before end; if that boundary's domain stretches beyond end,
409 we'll need to create a new boundary at end, and that boundary
410 must have that remembered value. */
411 int value_at_end = BOUNDARY_VALUE (c, end_ix - 1);
412
413 /* Delete all boundaries strictly within start..end; this means
414 those whose indices are between start_ix (exclusive) and end_ix
415 (exclusive). */
416 delete_cache_boundaries (c, start_ix + 1, end_ix);
417
418 /* Make sure we have the right value established going in to
419 start..end from the left, and no unnecessary boundaries. */
420 if (BOUNDARY_POS (c, start_ix) == start)
421 {
422 /* Is this boundary necessary? If no, remove it; if yes, set
423 its value. */
424 if (start_ix > 0
425 && BOUNDARY_VALUE (c, start_ix - 1) == value)
426 {
427 delete_cache_boundaries (c, start_ix, start_ix + 1);
428 start_ix--;
429 }
430 else
431 SET_BOUNDARY_VALUE (c, start_ix, value);
432 }
433 else
434 {
435 /* Do we need to add a new boundary here? */
436 if (BOUNDARY_VALUE (c, start_ix) != value)
437 {
438 insert_cache_boundary (c, start_ix + 1, start, value);
439 start_ix++;
440 }
441 }
177c0ea7 442
b9c5136f
KH
443 /* This is equivalent to letting end_ix float (like a buffer
444 marker does) with the insertions and deletions we may have
445 done. */
446 end_ix = start_ix + 1;
447
448 /* Make sure we have the correct value established as we leave
449 start..end to the right. */
450 if (end == c->buffer_end)
451 /* There is no text after start..end; nothing to do. */
452 ;
453 else if (end_ix >= c->cache_len
454 || end < BOUNDARY_POS (c, end_ix))
455 {
456 /* There is no boundary at end, but we may need one. */
457 if (value_at_end != value)
458 insert_cache_boundary (c, end_ix, end, value_at_end);
459 }
460 else
461 {
462 /* There is a boundary at end; should it be there? */
463 if (value == BOUNDARY_VALUE (c, end_ix))
464 delete_cache_boundaries (c, end_ix, end_ix + 1);
465 }
466 }
467}
468
469
470\f
471/* Interface: Invalidating the cache. Private: Re-validating the cache. */
472
473/* Indicate that a section of BUF has changed, to invalidate CACHE.
474 HEAD is the number of chars unchanged at the beginning of the buffer.
475 TAIL is the number of chars unchanged at the end of the buffer.
476 NOTE: this is *not* the same as the ending position of modified
477 region.
478 (This way of specifying regions makes more sense than absolute
479 buffer positions in the presence of insertions and deletions; the
480 args to pass are the same before and after such an operation.) */
481void
c098fdb8 482invalidate_region_cache (struct buffer *buf, struct region_cache *c,
0065d054 483 ptrdiff_t head, ptrdiff_t tail)
b9c5136f
KH
484{
485 /* Let chead = c->beg_unchanged, and
486 ctail = c->end_unchanged.
487 If z-tail < beg+chead by a large amount, or
488 z-ctail < beg+head by a large amount,
489
490 then cutting back chead and ctail to head and tail would lose a
491 lot of information that we could preserve by revalidating the
492 cache before processing this invalidation. Losing that
493 information may be more costly than revalidating the cache now.
494 So go ahead and call revalidate_region_cache if it seems that it
495 might be worthwhile. */
496 if (((BUF_BEG (buf) + c->beg_unchanged) - (BUF_Z (buf) - tail)
497 > PRESERVE_THRESHOLD)
498 || ((BUF_BEG (buf) + head) - (BUF_Z (buf) - c->end_unchanged)
499 > PRESERVE_THRESHOLD))
500 revalidate_region_cache (buf, c);
501
502
503 if (head < c->beg_unchanged)
504 c->beg_unchanged = head;
505 if (tail < c->end_unchanged)
506 c->end_unchanged = tail;
507
508 /* We now know nothing about the region between the unchanged head
509 and the unchanged tail (call it the "modified region"), not even
510 its length.
511
512 If the modified region has shrunk in size (deletions do this),
513 then the cache may now contain boundaries originally located in
514 text that doesn't exist any more.
515
516 If the modified region has increased in size (insertions do
517 this), then there may now be boundaries in the modified region
518 whose positions are wrong.
519
520 Even calling BOUNDARY_POS on boundaries still in the unchanged
521 head or tail may well give incorrect answers now, since
522 c->buffer_beg and c->buffer_end may well be wrong now. (Well,
523 okay, c->buffer_beg never changes, so boundaries in the unchanged
524 head will still be okay. But it's the principle of the thing.)
525
526 So things are generally a mess.
527
528 But we don't clean up this mess here; that would be expensive,
529 and this function gets called every time any buffer modification
530 occurs. Rather, we can clean up everything in one swell foop,
531 accounting for all the modifications at once, by calling
532 revalidate_region_cache before we try to consult the cache the
533 next time. */
534}
535
536
177c0ea7 537/* Clean out any cache entries applying to the modified region, and
b9c5136f
KH
538 make the positions of the remaining entries accurate again.
539
540 After calling this function, the mess described in the comment in
541 invalidate_region_cache is cleaned up.
542
543 This function operates by simply throwing away everything it knows
544 about the modified region. It doesn't care exactly which
545 insertions and deletions took place; it just tosses it all.
546
547 For example, if you insert a single character at the beginning of
548 the buffer, and a single character at the end of the buffer (for
549 example), without calling this function in between the two
550 insertions, then the entire cache will be freed of useful
551 information. On the other hand, if you do manage to call this
552 function in between the two insertions, then the modified regions
553 will be small in both cases, no information will be tossed, and the
554 cache will know that it doesn't have knowledge of the first and
555 last characters any more.
556
557 Calling this function may be expensive; it does binary searches in
558 the cache, and causes cache gap motion. */
559
560static void
971de7fb 561revalidate_region_cache (struct buffer *buf, struct region_cache *c)
b9c5136f
KH
562{
563 /* The boundaries now in the cache are expressed relative to the
564 buffer_beg and buffer_end values stored in the cache. Now,
565 buffer_beg and buffer_end may not be the same as BUF_BEG (buf)
566 and BUF_Z (buf), so we have two different "bases" to deal with
567 --- the cache's, and the buffer's. */
568
569 /* If the entire buffer is still valid, don't waste time. Yes, this
570 should be a >, not a >=; think about what beg_unchanged and
571 end_unchanged get set to when the only change has been an
572 insertion. */
573 if (c->buffer_beg + c->beg_unchanged
574 > c->buffer_end - c->end_unchanged)
575 return;
576
577 /* If all the text we knew about as of the last cache revalidation
578 is still there, then all of the information in the cache is still
579 valid. Because c->buffer_beg and c->buffer_end are out-of-date,
580 the modified region appears from the cache's point of view to be
581 a null region located someplace in the buffer.
582
583 Now, invalidating that empty string will have no actual affect on
584 the cache; instead, we need to update the cache's basis first
585 (which will give the modified region the same size in the cache
586 as it has in the buffer), and then invalidate the modified
587 region. */
177c0ea7 588 if (c->buffer_beg + c->beg_unchanged
b9c5136f
KH
589 == c->buffer_end - c->end_unchanged)
590 {
591 /* Move the gap so that all the boundaries in the unchanged head
592 are expressed beg-relative, and all the boundaries in the
593 unchanged tail are expressed end-relative. That done, we can
594 plug in the new buffer beg and end, and all the positions
595 will be accurate.
596
597 The boundary which has jurisdiction over the modified region
598 should be left before the gap. */
599 move_cache_gap (c,
600 (find_cache_boundary (c, (c->buffer_beg
601 + c->beg_unchanged))
602 + 1),
603 0);
604
605 c->buffer_beg = BUF_BEG (buf);
606 c->buffer_end = BUF_Z (buf);
607
608 /* Now that the cache's basis has been changed, the modified
609 region actually takes up some space in the cache, so we can
610 invalidate it. */
611 set_cache_region (c,
612 c->buffer_beg + c->beg_unchanged,
613 c->buffer_end - c->end_unchanged,
614 0);
615 }
616
617 /* Otherwise, there is a non-empty region in the cache which
618 corresponds to the modified region of the buffer. */
619 else
620 {
0065d054 621 ptrdiff_t modified_ix;
b9c5136f
KH
622
623 /* These positions are correct, relative to both the cache basis
624 and the buffer basis. */
625 set_cache_region (c,
626 c->buffer_beg + c->beg_unchanged,
627 c->buffer_end - c->end_unchanged,
628 0);
629
630 /* Now the cache contains only boundaries that are in the
631 unchanged head and tail; we've disposed of any boundaries
632 whose positions we can't be sure of given the information
633 we've saved.
634
635 If we put the cache gap between the unchanged head and the
636 unchanged tail, we can adjust all the boundary positions at
637 once, simply by setting buffer_beg and buffer_end.
638
639 The boundary which has jurisdiction over the modified region
640 should be left before the gap. */
641 modified_ix =
642 find_cache_boundary (c, (c->buffer_beg + c->beg_unchanged)) + 1;
643 move_cache_gap (c, modified_ix, 0);
644
645 c->buffer_beg = BUF_BEG (buf);
646 c->buffer_end = BUF_Z (buf);
647
648 /* Now, we may have shrunk the buffer when we changed the basis,
649 and brought the boundaries we created for the start and end
650 of the modified region together, giving them the same
651 position. If that's the case, we should collapse them into
652 one boundary. Or we may even delete them both, if the values
653 before and after them are the same. */
654 if (modified_ix < c->cache_len
655 && (BOUNDARY_POS (c, modified_ix - 1)
656 == BOUNDARY_POS (c, modified_ix)))
657 {
658 int value_after = BOUNDARY_VALUE (c, modified_ix);
659
660 /* Should we remove both of the boundaries? Yes, if the
661 latter boundary is now establishing the same value that
662 the former boundary's predecessor does. */
663 if (modified_ix - 1 > 0
664 && value_after == BOUNDARY_VALUE (c, modified_ix - 2))
665 delete_cache_boundaries (c, modified_ix - 1, modified_ix + 1);
666 else
667 {
668 /* We do need a boundary here; collapse the two
669 boundaries into one. */
670 SET_BOUNDARY_VALUE (c, modified_ix - 1, value_after);
671 delete_cache_boundaries (c, modified_ix, modified_ix + 1);
672 }
673 }
674 }
675
676 /* Now the entire cache is valid. */
677 c->beg_unchanged
678 = c->end_unchanged
679 = c->buffer_end - c->buffer_beg;
680}
681
682\f
683/* Interface: Adding information to the cache. */
684
685/* Assert that the region of BUF between START and END (absolute
686 buffer positions) is "known," for the purposes of CACHE (e.g. "has
687 no newlines", in the case of the line cache). */
688void
c098fdb8 689know_region_cache (struct buffer *buf, struct region_cache *c,
0065d054 690 ptrdiff_t start, ptrdiff_t end)
b9c5136f
KH
691{
692 revalidate_region_cache (buf, c);
693
694 set_cache_region (c, start, end, 1);
695}
696
697\f
698/* Interface: using the cache. */
699
700/* Return true if the text immediately after POS in BUF is known, for
177c0ea7 701 the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest
333f9019 702 position after POS where the knowledge changes. */
b9c5136f 703int
c098fdb8 704region_cache_forward (struct buffer *buf, struct region_cache *c,
0065d054 705 ptrdiff_t pos, ptrdiff_t *next)
b9c5136f
KH
706{
707 revalidate_region_cache (buf, c);
708
709 {
0065d054 710 ptrdiff_t i = find_cache_boundary (c, pos);
b9c5136f 711 int i_value = BOUNDARY_VALUE (c, i);
0065d054 712 ptrdiff_t j;
b9c5136f
KH
713
714 /* Beyond the end of the buffer is unknown, by definition. */
715 if (pos >= BUF_Z (buf))
716 {
717 if (next) *next = BUF_Z (buf);
718 i_value = 0;
719 }
720 else if (next)
721 {
722 /* Scan forward from i to find the next differing position. */
723 for (j = i + 1; j < c->cache_len; j++)
724 if (BOUNDARY_VALUE (c, j) != i_value)
725 break;
726
727 if (j < c->cache_len)
728 *next = BOUNDARY_POS (c, j);
729 else
730 *next = BUF_Z (buf);
731 }
732
733 return i_value;
734 }
735}
736
737/* Return true if the text immediately before POS in BUF is known, for
738 the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest
333f9019 739 position before POS where the knowledge changes. */
c098fdb8 740int region_cache_backward (struct buffer *buf, struct region_cache *c,
0065d054 741 ptrdiff_t pos, ptrdiff_t *next)
b9c5136f
KH
742{
743 revalidate_region_cache (buf, c);
744
745 /* Before the beginning of the buffer is unknown, by
746 definition. */
747 if (pos <= BUF_BEG (buf))
748 {
749 if (next) *next = BUF_BEG (buf);
750 return 0;
751 }
752
753 {
0065d054 754 ptrdiff_t i = find_cache_boundary (c, pos - 1);
b9c5136f 755 int i_value = BOUNDARY_VALUE (c, i);
0065d054 756 ptrdiff_t j;
b9c5136f
KH
757
758 if (next)
759 {
760 /* Scan backward from i to find the next differing position. */
761 for (j = i - 1; j >= 0; j--)
762 if (BOUNDARY_VALUE (c, j) != i_value)
763 break;
764
765 if (j >= 0)
766 *next = BOUNDARY_POS (c, j + 1);
767 else
768 *next = BUF_BEG (buf);
769 }
770
771 return i_value;
772 }
773}
774
775\f
776/* Debugging: pretty-print a cache to the standard error output. */
777
e3b27b31 778void pp_cache (struct region_cache *) EXTERNALLY_VISIBLE;
b9c5136f 779void
971de7fb 780pp_cache (struct region_cache *c)
b9c5136f 781{
0065d054
PE
782 ptrdiff_t i;
783 ptrdiff_t beg_u = c->buffer_beg + c->beg_unchanged;
784 ptrdiff_t end_u = c->buffer_end - c->end_unchanged;
b9c5136f
KH
785
786 fprintf (stderr,
0065d054 787 "basis: %"pD"d..%"pD"d modified: %"pD"d..%"pD"d\n",
c2982e87
PE
788 c->buffer_beg, c->buffer_end,
789 beg_u, end_u);
b9c5136f
KH
790
791 for (i = 0; i < c->cache_len; i++)
792 {
0065d054 793 ptrdiff_t pos = BOUNDARY_POS (c, i);
b9c5136f
KH
794
795 putc (((pos < beg_u) ? 'v'
796 : (pos == beg_u) ? '-'
797 : ' '),
798 stderr);
799 putc (((pos > end_u) ? '^'
800 : (pos == end_u) ? '-'
801 : ' '),
802 stderr);
0065d054 803 fprintf (stderr, "%"pD"d : %d\n", pos, BOUNDARY_VALUE (c, i));
b9c5136f
KH
804 }
805}