Skip to content

Instantly share code, notes, and snippets.

@lf94
Created May 14, 2015 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lf94/31748c017ab8053ae127 to your computer and use it in GitHub Desktop.
Save lf94/31748c017ab8053ae127 to your computer and use it in GitHub Desktop.
GNU Emac's buffer data management
void
insert_1_both (const char *string,
ptrdiff_t nchars, ptrdiff_t nbytes,
bool inherit, bool prepare, bool before_markers)
{
if (nchars == 0)
return;
if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
nchars = nbytes;
if (prepare)
/* Do this before moving and increasing the gap,
because the before-change hooks might move the gap
or make it smaller. */
prepare_to_modify_buffer (PT, PT, NULL);
if (PT != GPT)
move_gap_both (PT, PT_BYTE);
if (GAP_SIZE < nbytes)
make_gap (nbytes - GAP_SIZE);
#ifdef BYTE_COMBINING_DEBUG
if (count_combining_before (string, nbytes, PT, PT_BYTE)
|| count_combining_after (string, nbytes, PT, PT_BYTE))
emacs_abort ();
#endif
/* Record deletion of the surrounding text that combines with
the insertion. This, together with recording the insertion,
will add up to the right stuff in the undo list. */
record_insert (PT, nchars);
MODIFF++;
CHARS_MODIFF = MODIFF;
memcpy (GPT_ADDR, string, nbytes);
GAP_SIZE -= nbytes;
GPT += nchars;
ZV += nchars;
Z += nchars;
GPT_BYTE += nbytes;
ZV_BYTE += nbytes;
Z_BYTE += nbytes;
if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
eassert (GPT <= GPT_BYTE);
/* The insert may have been in the unchanged region, so check again. */
if (Z - GPT < END_UNCHANGED)
END_UNCHANGED = Z - GPT;
adjust_overlays_for_insert (PT, nchars);
adjust_markers_for_insert (PT, PT_BYTE,
PT + nchars, PT_BYTE + nbytes,
before_markers);
offset_intervals (current_buffer, PT, nchars);
if (!inherit && buffer_intervals (current_buffer))
set_text_properties (make_number (PT), make_number (PT + nchars),
Qnil, Qnil, Qnil);
adjust_point (nchars, nbytes);
check_markers ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment