/* Buffer insertion/deletion and gap motion for GNU Emacs.
- Copyright (C) 1985-1986, 1993-1995, 1997-2012
- Free Software Foundation, Inc.
+ Copyright (C) 1985-1986, 1993-1995, 1997-2013 Free Software
+ Foundation, Inc.
This file is part of GNU Emacs.
#endif /* MARKER_DEBUG */
-/* Move gap to position CHARPOS.
- Note that this can quit! */
-
-void
-move_gap (ptrdiff_t charpos)
-{
- move_gap_both (charpos, charpos_to_bytepos (charpos));
-}
-
/* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
Note that this can quit! */
void
move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
{
+ eassert (charpos == BYTE_TO_CHAR (bytepos)
+ && bytepos == CHAR_TO_BYTE (charpos));
if (bytepos < GPT_BYTE)
gap_left (charpos, bytepos, 0);
else if (bytepos > GPT_BYTE)
ptrdiff_t real_gap_loc_byte;
ptrdiff_t old_gap_size;
ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
- enum { enough_for_a_while = 2000 };
if (BUF_BYTES_MAX - current_size < nbytes_added)
buffer_overflow ();
/* If we have to get more space, get enough to last a while;
but do not exceed the maximum buffer size. */
- nbytes_added = min (nbytes_added + enough_for_a_while,
+ nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
BUF_BYTES_MAX - current_size);
enlarge_buffer_text (current_buffer, nbytes_added);
GPT_BYTE = Z_BYTE + GAP_SIZE;
GAP_SIZE = nbytes_added;
- /* Move the new gap down to be consecutive with the end of the old one.
- This adjusts the markers properly too. */
+ /* Move the new gap down to be consecutive with the end of the old one. */
gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
/* Now combine the two into one large gap. */
ptrdiff_t real_beg_unchanged;
ptrdiff_t new_gap_size;
- /* Make sure the gap is at least 20 bytes. */
- if (GAP_SIZE - nbytes_removed < 20)
- nbytes_removed = GAP_SIZE - 20;
+ /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
+ if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
+ nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
/* Prevent quitting in move_gap. */
tem = Vinhibit_quit;
Z_BYTE += new_gap_size;
GAP_SIZE = nbytes_removed;
- /* Move the unwanted pretend gap to the end of the buffer. This
- adjusts the markers properly too. */
+ /* Move the unwanted pretend gap to the end of the buffer. */
gap_right (Z, Z_BYTE);
enlarge_buffer_text (current_buffer, -nbytes_removed);
make_gap_smaller (-nbytes_added);
#endif
}
-\f
+
+/* Add NBYTES to B's gap. It's enough to temporarily
+ fake current_buffer and avoid real switch to B. */
+
+void
+make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
+{
+ struct buffer *oldb = current_buffer;
+
+ current_buffer = b;
+ make_gap (nbytes);
+ current_buffer = oldb;
+}
+
/* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
FROM_MULTIBYTE says whether the incoming text is multibyte.
TO_MULTIBYTE says whether to store the text as multibyte.
}
}
-/* Subroutine used by the insert functions above. */
-
-void
-insert_1 (const char *string, ptrdiff_t nbytes,
- bool inherit, bool prepare, bool before_markers)
-{
- insert_1_both (string, chars_in_text ((unsigned char *) string, nbytes),
- nbytes, inherit, prepare, before_markers);
-}
-
-\f
#ifdef BYTE_COMBINING_DEBUG
/* See if the bytes before POS/POS_BYTE combine with bytes
\f
/* Insert a sequence of NCHARS chars which occupy NBYTES bytes
- starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
- are the same as in insert_1. */
+ starting at STRING. INHERIT non-zero means inherit the text
+ properties from neighboring characters; zero means inserted text
+ will have no text properties. PREPARE non-zero means call
+ prepare_to_modify_buffer, which checks that the region is not
+ read-only, and calls before-change-function and any modification
+ properties the text may have. BEFORE_MARKERS non-zero means adjust
+ all markers that point at the insertion place to point after it. */
void
insert_1_both (const char *string,
}
\f
/* Insert a sequence of NCHARS chars which occupy NBYTES bytes
- starting at GPT_ADDR. */
+ starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
+ GPT_ADDR (if not text_at_gap_tail). */
void
-insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes)
+insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
{
+ int ins_charpos = GPT;
+ int ins_bytepos = GPT_BYTE;
+
if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
nchars = nbytes;
MODIFF++;
GAP_SIZE -= nbytes;
- GPT += nchars;
+ if (! text_at_gap_tail)
+ {
+ GPT += nchars;
+ GPT_BYTE += nbytes;
+ }
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);
- adjust_overlays_for_insert (GPT - nchars, nchars);
- adjust_markers_for_insert (GPT - nchars, GPT_BYTE - nbytes,
- GPT, GPT_BYTE, 0);
+ adjust_overlays_for_insert (ins_charpos, nchars);
+ adjust_markers_for_insert (ins_charpos, ins_bytepos,
+ ins_charpos + nchars, ins_bytepos + nbytes, 0);
if (buffer_intervals (current_buffer))
{
- offset_intervals (current_buffer, GPT - nchars, nchars);
- graft_intervals_into_buffer (NULL, GPT - nchars, nchars,
+ offset_intervals (current_buffer, ins_charpos, nchars);
+ graft_intervals_into_buffer (NULL, ins_charpos, nchars,
current_buffer, 0);
}
- if (GPT - nchars < PT)
+ if (ins_charpos < PT)
adjust_point (nchars, nbytes);
check_markers ();
adjust_markers_for_insert (from, from_byte,
from + len, from_byte + len_byte, 0);
- if (! EQ (BVAR (current_buffer, undo_list), Qt))
- {
- if (nchars_del > 0)
- record_delete (from, prev_text);
- record_insert (from, len);
- }
+ if (nchars_del > 0)
+ record_delete (from, prev_text);
+ record_insert (from, len);
if (len > nchars_del)
adjust_overlays_for_insert (from, len - nchars_del);
emacs_abort ();
#endif
- if (! EQ (BVAR (current_buffer, undo_list), Qt))
+ /* Record the insertion first, so that when we undo,
+ the deletion will be undone first. Thus, undo
+ will insert before deleting, and thus will keep
+ the markers before and after this text separate. */
+ if (!NILP (deletion))
{
- /* Record the insertion first, so that when we undo,
- the deletion will be undone first. Thus, undo
- will insert before deleting, and thus will keep
- the markers before and after this text separate. */
record_insert (from + SCHARS (deletion), inschars);
record_delete (from, deletion);
}
so that undo handles this after reinserting the text. */
adjust_markers_for_delete (from, from_byte, to, to_byte);
- if (! EQ (BVAR (current_buffer, undo_list), Qt))
- record_delete (from, deletion);
+ record_delete (from, deletion);
MODIFF++;
CHARS_MODIFF = MODIFF;
return deletion;
}
-\f
-/* Call this if you're about to change the region of BUFFER from
- character positions START to END. This checks the read-only
+
+/* Call this if you're about to change the text of current buffer
+ from character positions START to END. This checks the read-only
properties of the region, calls the necessary modification hooks,
and warns the next redisplay that it should pay attention to that
- area.
-
- If PRESERVE_CHARS_MODIFF, do not update CHARS_MODIFF.
- Otherwise set CHARS_MODIFF to the new value of MODIFF. */
+ area. */
void
-modify_region (struct buffer *buffer, ptrdiff_t start, ptrdiff_t end,
- bool preserve_chars_modiff)
+modify_text (ptrdiff_t start, ptrdiff_t end)
{
- struct buffer *old_buffer = current_buffer;
-
- set_buffer_internal (buffer);
-
prepare_to_modify_buffer (start, end, NULL);
- BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
-
+ BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
if (MODIFF <= SAVE_MODIFF)
record_first_change ();
MODIFF++;
- if (! preserve_chars_modiff)
- CHARS_MODIFF = MODIFF;
-
- bset_point_before_scroll (buffer, Qnil);
+ CHARS_MODIFF = MODIFF;
- set_buffer_internal (old_buffer);
+ bset_point_before_scroll (current_buffer, Qnil);
}
-\f
+
/* Check that it is okay to modify the buffer between START and END,
which are char positions.
by holding its value temporarily in a marker. */
void
-prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
- ptrdiff_t *preserve_ptr)
+prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
+ ptrdiff_t *preserve_ptr)
{
struct buffer *base_buffer;
if (!NILP (BVAR (current_buffer, read_only)))
Fbarf_if_buffer_read_only ();
- /* Let redisplay consider other windows than selected_window
- if modifying another buffer. */
- if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
+ /* If we're modifying the buffer other than shown in a selected window,
+ let redisplay consider other windows if this buffer is visible. */
+ if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer
+ && buffer_window_count (current_buffer))
++windows_or_buffers_changed;
if (buffer_intervals (current_buffer))
: (!NILP (Vselect_active_regions)
&& !NILP (Vtransient_mark_mode))))
{
- ptrdiff_t b = XMARKER (BVAR (current_buffer, mark))->charpos;
+ ptrdiff_t b = marker_position (BVAR (current_buffer, mark));
ptrdiff_t e = PT;
if (b < e)
Vsaved_region_selection = make_buffer_string (b, e, 0);
}
signal_before_change (start, end, preserve_ptr);
+ Vdeactivate_mark = Qt;
+}
+
+/* Like above, but called when we know that the buffer text
+ will be modified and region caches should be invalidated. */
+
+void
+prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
+ ptrdiff_t *preserve_ptr)
+{
+ prepare_to_modify_buffer_1 (start, end, preserve_ptr);
if (current_buffer->newline_cache)
invalidate_region_cache (current_buffer,
invalidate_region_cache (current_buffer,
current_buffer->width_run_cache,
start - BEG, Z - end);
-
- Vdeactivate_mark = Qt;
+ if (current_buffer->bidi_paragraph_cache)
+ invalidate_region_cache (current_buffer,
+ current_buffer->bidi_paragraph_cache,
+ start - BEG, Z - end);
}
-\f
+
/* These macros work with an argument named `preserve_ptr'
and a local variable named `preserve_marker'. */
VARIABLE is the variable to maybe set to nil.
NO-ERROR-FLAG is nil if there was an error,
anything else meaning no error (so this function does nothing). */
-static Lisp_Object
-reset_var_on_error (Lisp_Object val)
+struct rvoe_arg
{
- if (NILP (XCDR (val)))
- Fset (XCAR (val), Qnil);
- return Qnil;
+ Lisp_Object *location;
+ bool errorp;
+};
+
+static void
+reset_var_on_error (void *ptr)
+{
+ struct rvoe_arg *p = ptr;
+ if (p->errorp)
+ *p->location = Qnil;
}
/* Signal a change to the buffer immediately before it happens.
Lisp_Object preserve_marker;
struct gcpro gcpro1, gcpro2, gcpro3;
ptrdiff_t count = SPECPDL_INDEX ();
+ struct rvoe_arg rvoe_arg;
if (inhibit_modification_hooks)
return;
if (!NILP (Vbefore_change_functions))
{
Lisp_Object args[3];
- Lisp_Object rvoe_arg = Fcons (Qbefore_change_functions, Qnil);
+ rvoe_arg.location = &Vbefore_change_functions;
+ rvoe_arg.errorp = 1;
PRESERVE_VALUE;
PRESERVE_START_END;
/* Mark before-change-functions to be reset to nil in case of error. */
- record_unwind_protect (reset_var_on_error, rvoe_arg);
+ record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
/* Actually run the hook functions. */
args[0] = Qbefore_change_functions;
Frun_hook_with_args (3, args);
/* There was no error: unarm the reset_on_error. */
- XSETCDR (rvoe_arg, Qt);
+ rvoe_arg.errorp = 0;
}
if (buffer_has_overlays ())
signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
{
ptrdiff_t count = SPECPDL_INDEX ();
+ struct rvoe_arg rvoe_arg;
+
if (inhibit_modification_hooks)
return;
&& current_buffer != XBUFFER (combine_after_change_buffer))
Fcombine_after_change_execute ();
- elt = Fcons (make_number (charpos - BEG),
- Fcons (make_number (Z - (charpos - lendel + lenins)),
- Fcons (make_number (lenins - lendel), Qnil)));
+ elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
+ lenins - lendel);
combine_after_change_list
= Fcons (elt, combine_after_change_list);
combine_after_change_buffer = Fcurrent_buffer ();
if (!NILP (Vafter_change_functions))
{
Lisp_Object args[4];
- Lisp_Object rvoe_arg = Fcons (Qafter_change_functions, Qnil);
+ rvoe_arg.location = &Vafter_change_functions;
+ rvoe_arg.errorp = 1;
/* Mark after-change-functions to be reset to nil in case of error. */
- record_unwind_protect (reset_var_on_error, rvoe_arg);
+ record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
/* Actually run the hook functions. */
args[0] = Qafter_change_functions;
Frun_hook_with_args (4, args);
/* There was no error: unarm the reset_on_error. */
- XSETCDR (rvoe_arg, Qt);
+ rvoe_arg.errorp = 0;
}
if (buffer_has_overlays ())
unbind_to (count, Qnil);
}
-static Lisp_Object
+static void
Fcombine_after_change_execute_1 (Lisp_Object val)
{
Vcombine_after_change_calls = val;
- return val;
}
DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
Scombine_after_change_execute, 0, 0, 0,
- doc: /* This function is for use internally in `combine-after-change-calls'. */)
+ doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
(void)
{
ptrdiff_t count = SPECPDL_INDEX ();
combine_after_change_buffer = Qnil;
DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
- doc: /* Used internally by the `combine-after-change-calls' macro. */);
+ doc: /* Used internally by the function `combine-after-change-calls' macro. */);
Vcombine_after_change_calls = Qnil;
DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,