/* Return true if the text immediately after POS in BUF is known, for the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest position after POS where the knownness changes. */ int region_cache_forward (struct buffer *buf, struct region_cache *c, EMACS_INT pos, EMACS_INT *next) { revalidate_region_cache (buf, c); { EMACS_INT i = find_cache_boundary (c, pos); int i_value = BOUNDARY_VALUE (c, i); EMACS_INT j; /* Beyond the end of the buffer is unknown, by definition. */ if (pos >= BUF_Z (buf)) { if (next) *next = BUF_Z (buf); i_value = 0; } else if (next) { /* Scan forward from i to find the next differing position. */ for (j = i + 1; j < c->cache_len; j++) if (BOUNDARY_VALUE (c, j) != i_value) break; if (j < c->cache_len) *next = BOUNDARY_POS (c, j); else *next = BUF_Z (buf); } return i_value; } }
/* Indicate that a section of BUF has changed, to invalidate CACHE. HEAD is the number of chars unchanged at the beginning of the buffer. TAIL is the number of chars unchanged at the end of the buffer. NOTE: this is *not* the same as the ending position of modified region. (This way of specifying regions makes more sense than absolute buffer positions in the presence of insertions and deletions; the args to pass are the same before and after such an operation.) */ void invalidate_region_cache (struct buffer *buf, struct region_cache *c, EMACS_INT head, EMACS_INT tail) { /* Let chead = c->beg_unchanged, and ctail = c->end_unchanged. If z-tail < beg+chead by a large amount, or z-ctail < beg+head by a large amount, then cutting back chead and ctail to head and tail would lose a lot of information that we could preserve by revalidating the cache before processing this invalidation. Losing that information may be more costly than revalidating the cache now. So go ahead and call revalidate_region_cache if it seems that it might be worthwhile. */ if (((BUF_BEG (buf) + c->beg_unchanged) - (BUF_Z (buf) - tail) > PRESERVE_THRESHOLD) || ((BUF_BEG (buf) + head) - (BUF_Z (buf) - c->end_unchanged) > PRESERVE_THRESHOLD)) revalidate_region_cache (buf, c); if (head < c->beg_unchanged) c->beg_unchanged = head; if (tail < c->end_unchanged) c->end_unchanged = tail; /* We now know nothing about the region between the unchanged head and the unchanged tail (call it the "modified region"), not even its length. If the modified region has shrunk in size (deletions do this), then the cache may now contain boundaries originally located in text that doesn't exist any more. If the modified region has increased in size (insertions do this), then there may now be boundaries in the modified region whose positions are wrong. Even calling BOUNDARY_POS on boundaries still in the unchanged head or tail may well give incorrect answers now, since c->buffer_beg and c->buffer_end may well be wrong now. (Well, okay, c->buffer_beg never changes, so boundaries in the unchanged head will still be okay. But it's the principle of the thing.) So things are generally a mess. But we don't clean up this mess here; that would be expensive, and this function gets called every time any buffer modification occurs. Rather, we can clean up everything in one swell foop, accounting for all the modifications at once, by calling revalidate_region_cache before we try to consult the cache the next time. */ }
static void revalidate_region_cache (struct buffer *buf, struct region_cache *c) { /* The boundaries now in the cache are expressed relative to the buffer_beg and buffer_end values stored in the cache. Now, buffer_beg and buffer_end may not be the same as BUF_BEG (buf) and BUF_Z (buf), so we have two different "bases" to deal with --- the cache's, and the buffer's. */ /* If the entire buffer is still valid, don't waste time. Yes, this should be a >, not a >=; think about what beg_unchanged and end_unchanged get set to when the only change has been an insertion. */ if (c->buffer_beg + c->beg_unchanged > c->buffer_end - c->end_unchanged) return; /* If all the text we knew about as of the last cache revalidation is still there, then all of the information in the cache is still valid. Because c->buffer_beg and c->buffer_end are out-of-date, the modified region appears from the cache's point of view to be a null region located someplace in the buffer. Now, invalidating that empty string will have no actual affect on the cache; instead, we need to update the cache's basis first (which will give the modified region the same size in the cache as it has in the buffer), and then invalidate the modified region. */ if (c->buffer_beg + c->beg_unchanged == c->buffer_end - c->end_unchanged) { /* Move the gap so that all the boundaries in the unchanged head are expressed beg-relative, and all the boundaries in the unchanged tail are expressed end-relative. That done, we can plug in the new buffer beg and end, and all the positions will be accurate. The boundary which has jurisdiction over the modified region should be left before the gap. */ move_cache_gap (c, (find_cache_boundary (c, (c->buffer_beg + c->beg_unchanged)) + 1), 0); c->buffer_beg = BUF_BEG (buf); c->buffer_end = BUF_Z (buf); /* Now that the cache's basis has been changed, the modified region actually takes up some space in the cache, so we can invalidate it. */ set_cache_region (c, c->buffer_beg + c->beg_unchanged, c->buffer_end - c->end_unchanged, 0); } /* Otherwise, there is a non-empty region in the cache which corresponds to the modified region of the buffer. */ else { EMACS_INT modified_ix; /* These positions are correct, relative to both the cache basis and the buffer basis. */ set_cache_region (c, c->buffer_beg + c->beg_unchanged, c->buffer_end - c->end_unchanged, 0); /* Now the cache contains only boundaries that are in the unchanged head and tail; we've disposed of any boundaries whose positions we can't be sure of given the information we've saved. If we put the cache gap between the unchanged head and the unchanged tail, we can adjust all the boundary positions at once, simply by setting buffer_beg and buffer_end. The boundary which has jurisdiction over the modified region should be left before the gap. */ modified_ix = find_cache_boundary (c, (c->buffer_beg + c->beg_unchanged)) + 1; move_cache_gap (c, modified_ix, 0); c->buffer_beg = BUF_BEG (buf); c->buffer_end = BUF_Z (buf); /* Now, we may have shrunk the buffer when we changed the basis, and brought the boundaries we created for the start and end of the modified region together, giving them the same position. If that's the case, we should collapse them into one boundary. Or we may even delete them both, if the values before and after them are the same. */ if (modified_ix < c->cache_len && (BOUNDARY_POS (c, modified_ix - 1) == BOUNDARY_POS (c, modified_ix))) { int value_after = BOUNDARY_VALUE (c, modified_ix); /* Should we remove both of the boundaries? Yes, if the latter boundary is now establishing the same value that the former boundary's predecessor does. */ if (modified_ix - 1 > 0 && value_after == BOUNDARY_VALUE (c, modified_ix - 2)) delete_cache_boundaries (c, modified_ix - 1, modified_ix + 1); else { /* We do need a boundary here; collapse the two boundaries into one. */ SET_BOUNDARY_VALUE (c, modified_ix - 1, value_after); delete_cache_boundaries (c, modified_ix, modified_ix + 1); } } } /* Now the entire cache is valid. */ c->beg_unchanged = c->end_unchanged = c->buffer_end - c->buffer_beg; }
EMACS_INT buf_bytepos_to_charpos (struct buffer *b, EMACS_INT bytepos) { struct Lisp_Marker *tail; EMACS_INT best_above, best_above_byte; EMACS_INT best_below, best_below_byte; if (bytepos < BUF_BEG_BYTE (b) || bytepos > BUF_Z_BYTE (b)) abort (); best_above = BUF_Z (b); best_above_byte = BUF_Z_BYTE (b); /* If this buffer has as many characters as bytes, each character must be one byte. This takes care of the case where enable-multibyte-characters is nil. */ if (best_above == best_above_byte) return bytepos; best_below = BEG; best_below_byte = BEG_BYTE; CONSIDER (BUF_PT_BYTE (b), BUF_PT (b)); CONSIDER (BUF_GPT_BYTE (b), BUF_GPT (b)); CONSIDER (BUF_BEGV_BYTE (b), BUF_BEGV (b)); CONSIDER (BUF_ZV_BYTE (b), BUF_ZV (b)); if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff) CONSIDER (cached_bytepos, cached_charpos); for (tail = BUF_MARKERS (b); tail; tail = tail->next) { CONSIDER (tail->bytepos, tail->charpos); /* If we are down to a range of 50 chars, don't bother checking any other markers; scan the intervening chars directly now. */ if (best_above - best_below < 50) break; } /* We get here if we did not exactly hit one of the known places. We have one known above and one known below. Scan, counting characters, from whichever one is closer. */ if (bytepos - best_below_byte < best_above_byte - bytepos) { int record = bytepos - best_below_byte > 5000; while (best_below_byte < bytepos) { best_below++; BUF_INC_POS (b, best_below_byte); } /* If this position is quite far from the nearest known position, cache the correspondence by creating a marker here. It will last until the next GC. But don't do it if BUF_MARKERS is nil; that is a signal from Fset_buffer_multibyte. */ if (record && BUF_MARKERS (b)) { Lisp_Object marker, buffer; marker = Fmake_marker (); XSETBUFFER (buffer, b); set_marker_both (marker, buffer, best_below, best_below_byte); } if (byte_debug_flag) byte_char_debug_check (b, best_below, bytepos); cached_buffer = b; cached_modiff = BUF_MODIFF (b); cached_charpos = best_below; cached_bytepos = best_below_byte; return best_below; } else { int record = best_above_byte - bytepos > 5000; while (best_above_byte > bytepos) { best_above--; BUF_DEC_POS (b, best_above_byte); } /* If this position is quite far from the nearest known position, cache the correspondence by creating a marker here. It will last until the next GC. But don't do it if BUF_MARKERS is nil; that is a signal from Fset_buffer_multibyte. */ if (record && BUF_MARKERS (b)) { Lisp_Object marker, buffer; marker = Fmake_marker (); XSETBUFFER (buffer, b); set_marker_both (marker, buffer, best_above, best_above_byte); } if (byte_debug_flag) byte_char_debug_check (b, best_above, bytepos); cached_buffer = b; cached_modiff = BUF_MODIFF (b); cached_charpos = best_above; cached_bytepos = best_above_byte; return best_above; } }
ptrdiff_t buf_charpos_to_bytepos (struct buffer *b, ptrdiff_t charpos) { struct Lisp_Marker *tail; ptrdiff_t best_above, best_above_byte; ptrdiff_t best_below, best_below_byte; eassert (BUF_BEG (b) <= charpos && charpos <= BUF_Z (b)); best_above = BUF_Z (b); best_above_byte = BUF_Z_BYTE (b); /* If this buffer has as many characters as bytes, each character must be one byte. This takes care of the case where enable-multibyte-characters is nil. */ if (best_above == best_above_byte) return charpos; best_below = BEG; best_below_byte = BEG_BYTE; /* We find in best_above and best_above_byte the closest known point above CHARPOS, and in best_below and best_below_byte the closest known point below CHARPOS, If at any point we can tell that the space between those two best approximations is all single-byte, we interpolate the result immediately. */ CONSIDER (BUF_PT (b), BUF_PT_BYTE (b)); CONSIDER (BUF_GPT (b), BUF_GPT_BYTE (b)); CONSIDER (BUF_BEGV (b), BUF_BEGV_BYTE (b)); CONSIDER (BUF_ZV (b), BUF_ZV_BYTE (b)); if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff) CONSIDER (cached_charpos, cached_bytepos); for (tail = BUF_MARKERS (b); tail; tail = tail->next) { CONSIDER (tail->charpos, tail->bytepos); /* If we are down to a range of 50 chars, don't bother checking any other markers; scan the intervening chars directly now. */ if (best_above - best_below < 50) break; } /* We get here if we did not exactly hit one of the known places. We have one known above and one known below. Scan, counting characters, from whichever one is closer. */ if (charpos - best_below < best_above - charpos) { bool record = charpos - best_below > 5000; while (best_below != charpos) { best_below++; BUF_INC_POS (b, best_below_byte); } /* If this position is quite far from the nearest known position, cache the correspondence by creating a marker here. It will last until the next GC. */ if (record) build_marker (b, best_below, best_below_byte); byte_char_debug_check (b, best_below, best_below_byte); cached_buffer = b; cached_modiff = BUF_MODIFF (b); cached_charpos = best_below; cached_bytepos = best_below_byte; return best_below_byte; } else { bool record = best_above - charpos > 5000; while (best_above != charpos) { best_above--; BUF_DEC_POS (b, best_above_byte); } /* If this position is quite far from the nearest known position, cache the correspondence by creating a marker here. It will last until the next GC. */ if (record) build_marker (b, best_above, best_above_byte); byte_char_debug_check (b, best_above, best_above_byte); cached_buffer = b; cached_modiff = BUF_MODIFF (b); cached_charpos = best_above; cached_bytepos = best_above_byte; return best_above_byte; } }