insdel.c 67.8 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Buffer insertion/deletion and gap motion for GNU Emacs.
2
   Copyright (C) 1985, 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
Glenn Morris's avatar
Glenn Morris committed
3
                 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
Glenn Morris's avatar
Glenn Morris committed
4
                 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
5 6 7

This file is part of GNU Emacs.

8
GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
9
it under the terms of the GNU General Public License as published by
10 11
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
12 13 14 15 16 17 18

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

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/>.  */
Jim Blandy's avatar
Jim Blandy committed
20 21


22
#include <config.h>
Jim Blandy's avatar
Jim Blandy committed
23
#include "lisp.h"
24
#include "intervals.h"
Jim Blandy's avatar
Jim Blandy committed
25
#include "buffer.h"
26
#include "character.h"
Jim Blandy's avatar
Jim Blandy committed
27
#include "window.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
28
#include "blockinput.h"
Andreas Schwab's avatar
Andreas Schwab committed
29
#include "region-cache.h"
Jim Blandy's avatar
Jim Blandy committed
30

31 32 33 34
#ifndef NULL
#define NULL 0
#endif

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
static void insert_from_string_1 (Lisp_Object string,
				  EMACS_INT pos, EMACS_INT pos_byte,
				  EMACS_INT nchars, EMACS_INT nbytes,
				  int inherit, int before_markers);
static void insert_from_buffer_1 (struct buffer *buf,
				  EMACS_INT from, EMACS_INT nchars,
				  int inherit);
static void gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap);
static void gap_right (EMACS_INT charpos, EMACS_INT bytepos);
static void adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to,
				       EMACS_INT amount);
static void adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
				       EMACS_INT to, EMACS_INT to_byte,
				       int before_markers);
static void adjust_markers_for_replace (EMACS_INT, EMACS_INT, EMACS_INT,
					EMACS_INT, EMACS_INT, EMACS_INT);
static void adjust_point (EMACS_INT nchars, EMACS_INT nbytes);
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
Lisp_Object Fcombine_after_change_execute ();

/* Non-nil means don't call the after-change-functions right away,
   just record an element in Vcombine_after_change_calls_list.  */
Lisp_Object Vcombine_after_change_calls;

/* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
   describing changes which happened while combine_after_change_calls
   was nonzero.  We use this to decide how to call them
   once the deferral ends.

   In each element.
   BEG-UNCHANGED is the number of chars before the changed range.
   END-UNCHANGED is the number of chars after the changed range,
   and CHANGE-AMOUNT is the number of characters inserted by the change
   (negative for a deletion).  */
Lisp_Object combine_after_change_list;

/* Buffer which combine_after_change_list is about.  */
Lisp_Object combine_after_change_buffer;
73 74 75

Lisp_Object Qinhibit_modification_hooks;

76

77 78 79 80 81 82 83 84 85 86 87 88
/* Check all markers in the current buffer, looking for something invalid.  */

static int check_markers_debug_flag;

#define CHECK_MARKERS()				\
  if (check_markers_debug_flag)			\
    check_markers ();				\
  else

void
check_markers ()
{
89
  register struct Lisp_Marker *tail;
90
  int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
91

92
  for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
93
    {
94
      if (tail->buffer->text != current_buffer->text)
95
	abort ();
96
      if (tail->charpos > Z)
97
	abort ();
98
      if (tail->bytepos > Z_BYTE)
99
	abort ();
100
      if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
101
	abort ();
102 103 104
    }
}

105
/* Move gap to position CHARPOS.
Jim Blandy's avatar
Jim Blandy committed
106 107
   Note that this can quit!  */

108
void
109
move_gap (EMACS_INT charpos)
Jim Blandy's avatar
Jim Blandy committed
110
{
111
  move_gap_both (charpos, charpos_to_bytepos (charpos));
Jim Blandy's avatar
Jim Blandy committed
112 113
}

114 115 116 117
/* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
   Note that this can quit!  */

void
118
move_gap_both (EMACS_INT charpos, EMACS_INT bytepos)
119 120 121 122 123 124 125 126 127 128
{
  if (bytepos < GPT_BYTE)
    gap_left (charpos, bytepos, 0);
  else if (bytepos > GPT_BYTE)
    gap_right (charpos, bytepos);
}

/* Move the gap to a position less than the current GPT.
   BYTEPOS describes the new position as a byte position,
   and CHARPOS is the corresponding char position.
Jim Blandy's avatar
Jim Blandy committed
129 130
   If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged.  */

Karl Heuer's avatar
Karl Heuer committed
131
static void
132
gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap)
Jim Blandy's avatar
Jim Blandy committed
133 134
{
  register unsigned char *to, *from;
135 136
  register EMACS_INT i;
  EMACS_INT new_s1;
Jim Blandy's avatar
Jim Blandy committed
137 138

  if (!newgap)
139
    BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
Jim Blandy's avatar
Jim Blandy committed
140

141
  i = GPT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
142 143
  to = GAP_END_ADDR;
  from = GPT_ADDR;
144
  new_s1 = GPT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
145 146 147 148 149 150 151

  /* Now copy the characters.  To move the gap down,
     copy characters up.  */

  while (1)
    {
      /* I gets number of characters left to copy.  */
152
      i = new_s1 - bytepos;
Jim Blandy's avatar
Jim Blandy committed
153 154 155
      if (i == 0)
	break;
      /* If a quit is requested, stop copying now.
156
	 Change BYTEPOS to be where we have actually moved the gap to.  */
Jim Blandy's avatar
Jim Blandy committed
157 158
      if (QUITP)
	{
159 160
	  bytepos = new_s1;
	  charpos = BYTE_TO_CHAR (bytepos);
Jim Blandy's avatar
Jim Blandy committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	  break;
	}
      /* Move at most 32000 chars before checking again for a quit.  */
      if (i > 32000)
	i = 32000;
#ifdef GAP_USE_BCOPY
      if (i >= 128
	  /* bcopy is safe if the two areas of memory do not overlap
	     or on systems where bcopy is always safe for moving upward.  */
	  && (BCOPY_UPWARD_SAFE
	      || to - from >= 128))
	{
	  /* If overlap is not safe, avoid it by not moving too many
	     characters at once.  */
	  if (!BCOPY_UPWARD_SAFE && i > to - from)
	    i = to - from;
	  new_s1 -= i;
	  from -= i, to -= i;
	  bcopy (from, to, i);
	}
      else
#endif
	{
	  new_s1 -= i;
	  while (--i >= 0)
	    *--to = *--from;
	}
    }

190 191
  /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
     BYTEPOS is where the loop above stopped, which may be what was specified
Jim Blandy's avatar
Jim Blandy committed
192
     or may be where a quit was detected.  */
193 194 195 196 197
  adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
  GPT_BYTE = bytepos;
  GPT = charpos;
  if (bytepos < charpos)
    abort ();
Karl Heuer's avatar
Karl Heuer committed
198
  if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
Jim Blandy's avatar
Jim Blandy committed
199 200 201
  QUIT;
}

Juanma Barranquero's avatar
Juanma Barranquero committed
202
/* Move the gap to a position greater than the current GPT.
203 204 205
   BYTEPOS describes the new position as a byte position,
   and CHARPOS is the corresponding char position.  */

Karl Heuer's avatar
Karl Heuer committed
206
static void
207
gap_right (EMACS_INT charpos, EMACS_INT bytepos)
Jim Blandy's avatar
Jim Blandy committed
208 209
{
  register unsigned char *to, *from;
210 211
  register EMACS_INT i;
  EMACS_INT new_s1;
Jim Blandy's avatar
Jim Blandy committed
212

213
  BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
Jim Blandy's avatar
Jim Blandy committed
214

215
  i = GPT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
216 217
  from = GAP_END_ADDR;
  to = GPT_ADDR;
218
  new_s1 = GPT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
219 220 221 222 223 224 225

  /* Now copy the characters.  To move the gap up,
     copy characters down.  */

  while (1)
    {
      /* I gets number of characters left to copy.  */
226
      i = bytepos - new_s1;
Jim Blandy's avatar
Jim Blandy committed
227 228 229
      if (i == 0)
	break;
      /* If a quit is requested, stop copying now.
230
	 Change BYTEPOS to be where we have actually moved the gap to.  */
Jim Blandy's avatar
Jim Blandy committed
231 232
      if (QUITP)
	{
233 234
	  bytepos = new_s1;
	  charpos = BYTE_TO_CHAR (bytepos);
Jim Blandy's avatar
Jim Blandy committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	  break;
	}
      /* Move at most 32000 chars before checking again for a quit.  */
      if (i > 32000)
	i = 32000;
#ifdef GAP_USE_BCOPY
      if (i >= 128
	  /* bcopy is safe if the two areas of memory do not overlap
	     or on systems where bcopy is always safe for moving downward.  */
	  && (BCOPY_DOWNWARD_SAFE
	      || from - to >= 128))
	{
	  /* If overlap is not safe, avoid it by not moving too many
	     characters at once.  */
	  if (!BCOPY_DOWNWARD_SAFE && i > from - to)
	    i = from - to;
	  new_s1 += i;
	  bcopy (from, to, i);
	  from += i, to += i;
	}
      else
#endif
	{
	  new_s1 += i;
	  while (--i >= 0)
	    *to++ = *from++;
	}
    }

264 265 266 267 268 269
  adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
			     - GAP_SIZE);
  GPT = charpos;
  GPT_BYTE = bytepos;
  if (bytepos < charpos)
    abort ();
Karl Heuer's avatar
Karl Heuer committed
270
  if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
Jim Blandy's avatar
Jim Blandy committed
271 272
  QUIT;
}
273

274 275
/* Add AMOUNT to the byte position of every marker in the current buffer
   whose current byte position is between FROM (exclusive) and TO (inclusive).
276

Jim Blandy's avatar
Jim Blandy committed
277 278
   Also, any markers past the outside of that interval, in the direction
   of adjustment, are first moved back to the near end of the interval
279 280 281 282
   and then adjusted by AMOUNT.

   When the latter adjustment is done, if AMOUNT is negative,
   we record the adjustment for undo.  (This case happens only for
283 284 285 286 287 288
   deletion.)

   The markers' character positions are not altered,
   because gap motion does not affect character positions.  */

int adjust_markers_test;
Jim Blandy's avatar
Jim Blandy committed
289

Karl Heuer's avatar
Karl Heuer committed
290
static void
291
adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to, EMACS_INT amount)
Jim Blandy's avatar
Jim Blandy committed
292
{
293 294 295
  /* Now that a marker has a bytepos, not counting the gap,
     nothing needs to be done here.  */
#if 0
Jim Blandy's avatar
Jim Blandy committed
296 297
  Lisp_Object marker;
  register struct Lisp_Marker *m;
298
  register EMACS_INT mpos;
Jim Blandy's avatar
Jim Blandy committed
299

300
  marker = BUF_MARKERS (current_buffer);
Jim Blandy's avatar
Jim Blandy committed
301

Jim Blandy's avatar
Jim Blandy committed
302
  while (!NILP (marker))
Jim Blandy's avatar
Jim Blandy committed
303 304
    {
      m = XMARKER (marker);
305
      mpos = m->bytepos;
Jim Blandy's avatar
Jim Blandy committed
306 307 308
      if (amount > 0)
	{
	  if (mpos > to && mpos < to + amount)
309 310 311 312 313
	    {
	      if (adjust_markers_test)
		abort ();
	      mpos = to + amount;
	    }
Jim Blandy's avatar
Jim Blandy committed
314 315 316
	}
      else
	{
317 318 319
	  /* Here's the case where a marker is inside text being deleted.
	     AMOUNT can be negative for gap motion, too,
	     but then this range contains no markers.  */
Jim Blandy's avatar
Jim Blandy committed
320
	  if (mpos > from + amount && mpos <= from)
321
	    {
322 323 324
	      if (adjust_markers_test)
		abort ();
	      mpos = from + amount;
325
	    }
Jim Blandy's avatar
Jim Blandy committed
326 327 328 329 330 331
	}
      if (mpos > from && mpos <= to)
	mpos += amount;
      m->bufpos = mpos;
      marker = m->chain;
    }
332
#endif
Jim Blandy's avatar
Jim Blandy committed
333
}
334

335 336 337 338 339 340
/* Adjust all markers for a deletion
   whose range in bytes is FROM_BYTE to TO_BYTE.
   The range in charpos is FROM to TO.

   This function assumes that the gap is adjacent to
   or inside of the range being deleted.  */
341

342
void
343 344
adjust_markers_for_delete (EMACS_INT from, EMACS_INT from_byte,
			   EMACS_INT to, EMACS_INT to_byte)
345 346 347
{
  Lisp_Object marker;
  register struct Lisp_Marker *m;
348
  register EMACS_INT charpos;
349

350
  for (m = BUF_MARKERS (current_buffer); m; m = m->next)
351 352 353 354 355 356 357
    {
      charpos = m->charpos;

      if (charpos > Z)
	abort ();

      /* If the marker is after the deletion,
358
	 relocate by number of chars / bytes deleted.  */
359
      if (charpos > to)
360 361 362 363 364
	{
	  m->charpos -= to - from;
	  m->bytepos -= to_byte - from_byte;
	}
      /* Here's the case where a marker is inside text being deleted.  */
365 366
      else if (charpos > from)
	{
367
	  if (! m->insertion_type)
368
	    { /* Normal markers will end up at the beginning of the
369
	       re-inserted text after undoing a deletion, and must be
370
	       adjusted to move them to the correct place.  */
371
	      XSETMISC (marker, m);
372
	    record_marker_adjustment (marker, from - charpos);
373
	    }
374
	  else if (charpos < to)
375
	    { /* Before-insertion markers will automatically move forward
376 377
	       upon re-inserting the deleted text, so we have to arrange
	       for them to move backward to the correct position.  */
378
	      XSETMISC (marker, m);
379
	    record_marker_adjustment (marker, charpos - to);
380
	    }
381
	  m->charpos = from;
382
	  m->bytepos = from_byte;
383
	}
384 385 386 387 388 389 390 391
      /* Here's the case where a before-insertion marker is immediately
	 before the deleted region.  */
      else if (charpos == from && m->insertion_type)
	{
	  /* Undoing the change uses normal insertion, which will
	     incorrectly make MARKER move forward, so we arrange for it
	     to then move backward to the correct place at the beginning
	     of the deleted region.  */
392
	  XSETMISC (marker, m);
393 394
	  record_marker_adjustment (marker, to - from);
	}
395 396
    }
}
397

398

399 400 401
/* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
   to TO / TO_BYTE.  We have to relocate the charpos of every marker
   that points after the insertion (but not their bytepos).
402 403 404 405 406 407

   When a marker points at the insertion point,
   we advance it if either its insertion-type is t
   or BEFORE_MARKERS is true.  */

static void
408 409
adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
			   EMACS_INT to, EMACS_INT to_byte, int before_markers)
410
{
411
  struct Lisp_Marker *m;
Karl Heuer's avatar
Karl Heuer committed
412
  int adjusted = 0;
413 414
  EMACS_INT nchars = to - from;
  EMACS_INT nbytes = to_byte - from_byte;
415

416
  for (m = BUF_MARKERS (current_buffer); m; m = m->next)
417
    {
418 419
      eassert (m->bytepos >= m->charpos
	       && m->bytepos - m->charpos <= Z_BYTE - Z);
420

421
      if (m->bytepos == from_byte)
Karl Heuer's avatar
Karl Heuer committed
422
	{
423 424
	  if (m->insertion_type || before_markers)
	    {
425 426
	      m->bytepos = to_byte;
	      m->charpos = to;
427 428 429
	      if (m->insertion_type)
		adjusted = 1;
	    }
430
	}
431 432 433
      else if (m->bytepos > from_byte)
	{
	  m->bytepos += nbytes;
434
	  m->charpos += nchars;
435
	}
436
    }
437 438

  /* Adjusting only markers whose insertion-type is t may result in
Kenichi Handa's avatar
Kenichi Handa committed
439 440
     - disordered start and end in overlays, and 
     - disordered overlays in the slot `overlays_before' of current_buffer.  */
Karl Heuer's avatar
Karl Heuer committed
441
  if (adjusted)
Kenichi Handa's avatar
Kenichi Handa committed
442 443 444 445
    {
      fix_start_end_in_overlays(from, to);
      fix_overlays_before (current_buffer, from, to);
    }
446 447
}

448 449 450 451 452 453 454 455 456 457
/* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.

   This is used only when the value of point changes due to an insert
   or delete; it does not represent a conceptual change in point as a
   marker.  In particular, point is not crossing any interval
   boundaries, so there's no need to use the usual SET_PT macro.  In
   fact it would be incorrect to do so, because either the old or the
   new value of point is out of sync with the current set of
   intervals.  */

Karl Heuer's avatar
Karl Heuer committed
458
static void
459
adjust_point (EMACS_INT nchars, EMACS_INT nbytes)
Karl Heuer's avatar
Karl Heuer committed
460
{
461 462 463 464
  BUF_PT (current_buffer) += nchars;
  BUF_PT_BYTE (current_buffer) += nbytes;

  /* In a single-byte buffer, the two positions must be equal.  */
465
  eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
Karl Heuer's avatar
Karl Heuer committed
466
}
467 468 469

/* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
   length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
470 471
   (NEW_BYTES).  It is assumed that OLD_CHARS > 0, i.e., this is not
   an insertion.  */
472 473

static void
474 475 476
adjust_markers_for_replace (EMACS_INT from, EMACS_INT from_byte,
			    EMACS_INT old_chars, EMACS_INT old_bytes,
			    EMACS_INT new_chars, EMACS_INT new_bytes)
477
{
478
  register struct Lisp_Marker *m;
479 480 481
  EMACS_INT prev_to_byte = from_byte + old_bytes;
  EMACS_INT diff_chars = new_chars - old_chars;
  EMACS_INT diff_bytes = new_bytes - old_bytes;
482

483
  for (m = BUF_MARKERS (current_buffer); m; m = m->next)
484
    {
485
      if (m->bytepos >= prev_to_byte)
486
	{
487 488
	  m->charpos += diff_chars;
	  m->bytepos += diff_bytes;
489
	}
490
      else if (m->bytepos > from_byte)
491 492
	{
	  m->charpos = from;
493
	  m->bytepos = from_byte;
494 495
	}
    }
496 497

  CHECK_MARKERS ();
498 499
}

Jim Blandy's avatar
Jim Blandy committed
500

501
/* Make the gap NBYTES_ADDED bytes longer.  */
Jim Blandy's avatar
Jim Blandy committed
502

503
void
504
make_gap_larger (EMACS_INT nbytes_added)
Jim Blandy's avatar
Jim Blandy committed
505 506
{
  Lisp_Object tem;
507 508 509
  EMACS_INT real_gap_loc;
  EMACS_INT real_gap_loc_byte;
  EMACS_INT old_gap_size;
Jim Blandy's avatar
Jim Blandy committed
510 511

  /* If we have to get more space, get enough to last a while.  */
512
  nbytes_added += 2000;
Jim Blandy's avatar
Jim Blandy committed
513

514 515
  /* Don't allow a buffer size that won't fit in an int
     even if it will fit in a Lisp integer.
516 517 518
     That won't work because so many places use `int'.

     Make sure we don't introduce overflows in the calculation.  */
519

520 521 522
  if (Z_BYTE - BEG_BYTE + GAP_SIZE
      >= (((EMACS_INT) 1 << (min (VALBITS, BITS_PER_INT) - 1)) - 1
	  - nbytes_added))
523
    error ("Buffer exceeds maximum size");
524

525
  enlarge_buffer_text (current_buffer, nbytes_added);
Jim Blandy's avatar
Jim Blandy committed
526 527 528 529 530 531

  /* Prevent quitting in move_gap.  */
  tem = Vinhibit_quit;
  Vinhibit_quit = Qt;

  real_gap_loc = GPT;
532
  real_gap_loc_byte = GPT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
533 534 535 536
  old_gap_size = GAP_SIZE;

  /* Call the newly allocated space a gap at the end of the whole space.  */
  GPT = Z + GAP_SIZE;
537
  GPT_BYTE = Z_BYTE + GAP_SIZE;
538
  GAP_SIZE = nbytes_added;
Jim Blandy's avatar
Jim Blandy committed
539 540 541

  /* Move the new gap down to be consecutive with the end of the old one.
     This adjusts the markers properly too.  */
542
  gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
Jim Blandy's avatar
Jim Blandy committed
543 544 545 546

  /* Now combine the two into one large gap.  */
  GAP_SIZE += old_gap_size;
  GPT = real_gap_loc;
547
  GPT_BYTE = real_gap_loc_byte;
Jim Blandy's avatar
Jim Blandy committed
548

Karl Heuer's avatar
Karl Heuer committed
549 550 551
  /* Put an anchor.  */
  *(Z_ADDR) = 0;

Jim Blandy's avatar
Jim Blandy committed
552 553
  Vinhibit_quit = tem;
}
554 555


Dave Love's avatar
Dave Love committed
556
/* Make the gap NBYTES_REMOVED bytes shorter.  */
557 558

void
559
make_gap_smaller (EMACS_INT nbytes_removed)
560 561
{
  Lisp_Object tem;
562 563 564 565 566 567
  EMACS_INT real_gap_loc;
  EMACS_INT real_gap_loc_byte;
  EMACS_INT real_Z;
  EMACS_INT real_Z_byte;
  EMACS_INT real_beg_unchanged;
  EMACS_INT new_gap_size;
568 569 570 571 572 573 574 575 576 577 578

  /* Make sure the gap is at least 20 bytes.  */
  if (GAP_SIZE - nbytes_removed < 20)
    nbytes_removed = GAP_SIZE - 20;

  /* Prevent quitting in move_gap.  */
  tem = Vinhibit_quit;
  Vinhibit_quit = Qt;

  real_gap_loc = GPT;
  real_gap_loc_byte = GPT_BYTE;
579
  new_gap_size = GAP_SIZE - nbytes_removed;
580 581
  real_Z = Z;
  real_Z_byte = Z_BYTE;
582
  real_beg_unchanged = BEG_UNCHANGED;
583 584 585 586

  /* Pretend that the last unwanted part of the gap is the entire gap,
     and that the first desired part of the gap is part of the buffer
     text.  */
587 588 589 590 591
  bzero (GPT_ADDR, new_gap_size);
  GPT += new_gap_size;
  GPT_BYTE += new_gap_size;
  Z += new_gap_size;
  Z_BYTE += new_gap_size;
592 593 594 595 596 597 598 599 600
  GAP_SIZE = nbytes_removed;

  /* Move the unwanted pretend gap to the end of the buffer.  This
     adjusts the markers properly too.  */
  gap_right (Z, Z_BYTE);

  enlarge_buffer_text (current_buffer, -nbytes_removed);

  /* Now restore the desired gap.  */
601
  GAP_SIZE = new_gap_size;
602 603 604 605
  GPT = real_gap_loc;
  GPT_BYTE = real_gap_loc_byte;
  Z = real_Z;
  Z_BYTE = real_Z_byte;
606
  BEG_UNCHANGED = real_beg_unchanged;
607 608 609 610 611 612 613 614

  /* Put an anchor.  */
  *(Z_ADDR) = 0;

  Vinhibit_quit = tem;
}

void
615
make_gap (EMACS_INT nbytes_added)
616 617 618
{
  if (nbytes_added >= 0)
    make_gap_larger (nbytes_added);
619
#if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
620 621 622 623
  else
    make_gap_smaller (-nbytes_added);
#endif
}
Jim Blandy's avatar
Jim Blandy committed
624

625 626 627 628 629 630 631
/* 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.
   If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.

   Return the number of bytes stored at TO_ADDR.  */

632 633 634
EMACS_INT
copy_text (const unsigned char *from_addr, unsigned char *to_addr,
	   EMACS_INT nbytes, int from_multibyte, int to_multibyte)
635 636 637 638 639 640 641 642
{
  if (from_multibyte == to_multibyte)
    {
      bcopy (from_addr, to_addr, nbytes);
      return nbytes;
    }
  else if (from_multibyte)
    {
643 644
      EMACS_INT nchars = 0;
      EMACS_INT bytes_left = nbytes;
645
      Lisp_Object tbl = Qnil;
646

647 648
      while (bytes_left > 0)
	{
649 650
	  int thislen, c;
	  c = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
651 652
	  if (! ASCII_CHAR_P (c))
	    c &= 0xFF;
653
	  *to_addr++ = c;
654
	  from_addr += thislen;
655
	  bytes_left -= thislen;
656 657 658 659 660 661 662 663 664 665 666 667 668
	  nchars++;
	}
      return nchars;
    }
  else
    {
      unsigned char *initial_to_addr = to_addr;

      /* Convert single-byte to multibyte.  */
      while (nbytes > 0)
	{
	  int c = *from_addr++;

669
	  if (c >= 0200)
670
	    {
671
	      c = unibyte_char_to_multibyte (c);
672
	      to_addr += CHAR_STRING (c, to_addr);
673 674 675 676 677 678 679 680 681 682 683 684 685 686
	      nbytes--;
	    }
	  else
	    /* Special case for speed.  */
	    *to_addr++ = c, nbytes--;
	}
      return to_addr - initial_to_addr;
    }
}

/* Return the number of bytes it would take
   to convert some single-byte text to multibyte.
   The single-byte text consists of NBYTES bytes at PTR.  */

687 688
EMACS_INT
count_size_as_multibyte (const unsigned char *ptr, EMACS_INT nbytes)
689
{
690 691
  EMACS_INT i;
  EMACS_INT outgoing_nbytes = 0;
692 693 694 695

  for (i = 0; i < nbytes; i++)
    {
      unsigned int c = *ptr++;
696

697
      if (c < 0200)
698 699
	outgoing_nbytes++;
      else
700
	{
701
	  c = unibyte_char_to_multibyte (c);
702
	  outgoing_nbytes += CHAR_BYTES (c);
703 704 705 706 707 708
	}
    }

  return outgoing_nbytes;
}

Jim Blandy's avatar
Jim Blandy committed
709
/* Insert a string of specified length before point.
710 711 712 713
   This function judges multibyteness based on
   enable_multibyte_characters in the current buffer;
   it never converts between single-byte and multibyte.

714 715
   DO NOT use this for the contents of a Lisp string or a Lisp buffer!
   prepare_to_modify_buffer could relocate the text.  */
Jim Blandy's avatar
Jim Blandy committed
716

717
void
718
insert (const unsigned char *string, EMACS_INT nbytes)
Jim Blandy's avatar
Jim Blandy committed
719
{
720
  if (nbytes > 0)
721
    {
722
      EMACS_INT len = chars_in_text (string, nbytes), opoint;
723 724 725
      insert_1_both (string, len, nbytes, 0, 1, 0);
      opoint = PT - len;
      signal_after_change (opoint, 0, len);
726
      update_compositions (opoint, PT, CHECK_BORDER);
727 728 729
    }
}

730 731
/* Likewise, but inherit text properties from neighboring characters.  */

732
void
733
insert_and_inherit (const unsigned char *string, EMACS_INT nbytes)
734
{
735
  if (nbytes > 0)
736
    {
737
      EMACS_INT len = chars_in_text (string, nbytes), opoint;
738 739 740
      insert_1_both (string, len, nbytes, 1, 1, 0);
      opoint = PT - len;
      signal_after_change (opoint, 0, len);
741
      update_compositions (opoint, PT, CHECK_BORDER);
742 743
    }
}
Jim Blandy's avatar
Jim Blandy committed
744

745
/* Insert the character C before point.  Do not inherit text properties.  */
746

747
void
748
insert_char (int c)
749
{
750
  unsigned char str[MAX_MULTIBYTE_LENGTH];
751 752 753
  int len;

  if (! NILP (current_buffer->enable_multibyte_characters))
754
    len = CHAR_STRING (c, str);
755 756 757
  else
    {
      len = 1;
758
      str[0] = c;
759
    }
760 761 762 763

  insert (str, len);
}

764
/* Insert the null-terminated string S before point.  */
765 766

void
767
insert_string (const char *s)
768 769 770 771 772 773 774 775 776 777
{
  insert (s, strlen (s));
}

/* Like `insert' except that all markers pointing at the place where
   the insertion happens are adjusted to point after it.
   Don't use this function to insert part of a Lisp string,
   since gc could happen and relocate it.  */

void
778
insert_before_markers (const unsigned char *string, EMACS_INT nbytes)
779 780 781
{
  if (nbytes > 0)
    {
782
      EMACS_INT len = chars_in_text (string, nbytes), opoint;
783 784 785
      insert_1_both (string, len, nbytes, 0, 1, 1);
      opoint = PT - len;
      signal_after_change (opoint, 0, len);
786
      update_compositions (opoint, PT, CHECK_BORDER);
787 788 789
    }
}

790 791
/* Likewise, but inherit text properties from neighboring characters.  */

792
void
793 794
insert_before_markers_and_inherit (const unsigned char *string,
				   EMACS_INT nbytes)
795 796 797
{
  if (nbytes > 0)
    {
798
      EMACS_INT len = chars_in_text (string, nbytes), opoint;
799 800 801
      insert_1_both (string, len, nbytes, 1, 1, 1);
      opoint = PT - len;
      signal_after_change (opoint, 0, len);
802
      update_compositions (opoint, PT, CHECK_BORDER);
803 804
    }
}
805

806 807 808
/* Subroutine used by the insert functions above.  */

void
809 810
insert_1 (const unsigned char *string, EMACS_INT nbytes,
	  int inherit, int prepare, int before_markers)
811
{
812 813 814
  insert_1_both (string, chars_in_text (string, nbytes), nbytes,
		 inherit, prepare, before_markers);
}
815

816

817 818
#ifdef BYTE_COMBINING_DEBUG

819 820
/* See if the bytes before POS/POS_BYTE combine with bytes
   at the start of STRING to form a single character.
821
   If so, return the number of bytes at the start of STRING
822
   which combine in this way.  Otherwise, return 0.  */
Jim Blandy's avatar
Jim Blandy committed
823

824
int
825 826
count_combining_before (const unsigned char *string, EMACS_INT length,
			EMACS_INT pos, EMACS_INT pos_byte)
827
{
828
  int len, combining_bytes;
829
  const unsigned char *p;
Jim Blandy's avatar
Jim Blandy committed
830

831 832
  if (NILP (current_buffer->enable_multibyte_characters))
    return 0;
833 834 835 836 837 838

  /* At first, we can exclude the following cases:
	(1) STRING[0] can't be a following byte of multibyte sequence.
	(2) POS is the start of the current buffer.
	(3) A character before POS is not a multibyte character.  */
  if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
839
    return 0;
840
  if (pos_byte == BEG_BYTE)	/* case (2) */
841
    return 0;
842 843 844 845
  len = 1;
  p = BYTE_POS_ADDR (pos_byte - 1);
  while (! CHAR_HEAD_P (*p)) p--, len++;
  if (! BASE_LEADING_CODE_P (*p)) /* case (3) */
846
    return 0;
847 848 849 850 851 852

  combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
  if (combining_bytes <= 0)
    /* The character preceding POS is, complete and no room for
       combining bytes (combining_bytes == 0), or an independent 8-bit
       character (combining_bytes < 0).  */
853
    return 0;
854

855 856 857
  /* We have a combination situation.  Count the bytes at STRING that
     may combine.  */
  p = string + 1;
858 859 860
  while (!CHAR_HEAD_P (*p) && p < string + length)
    p++;

861
  return (combining_bytes < p - string ? combining_bytes : p - string);
862
}
Jim Blandy's avatar
Jim Blandy committed
863

864 865 866 867
/* See if the bytes after POS/POS_BYTE combine with bytes
   at the end of STRING to form a single character.
   If so, return the number of bytes after POS/POS_BYTE
   which combine in this way.  Otherwise, return 0.  */
868

869
int
870 871
count_combining_after (const unsigned char *string,
		       EMACS_INT length, EMACS_INT pos, EMACS_INT pos_byte)
872
{
873 874 875
  EMACS_INT opos_byte = pos_byte;
  EMACS_INT i;
  EMACS_INT bytes;
876
  unsigned char *bufp;
877

878 879
  if (NILP (current_buffer->enable_multibyte_characters))
    return 0;
880 881 882 883 884 885 886 887 888

  /* At first, we can exclude the following cases:
	(1) The last byte of STRING is an ASCII.
	(2) POS is the last of the current buffer.
	(3) A character at POS can't be a following byte of multibyte
	    character.  */
  if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
    return 0;
  if (pos_byte == Z_BYTE)	/* case (2) */
889
    return 0;
890 891 892 893
  bufp = BYTE_POS_ADDR (pos_byte);
  if (CHAR_HEAD_P (*bufp))	/* case (3) */
    return 0;

894
  i = length - 1;
895
  while (i >= 0 && ! CHAR_HEAD_P (string[i]))
896 897 898
    {
      i--;
    }
899 900
  if (i < 0)
    {
901 902 903 904
      /* All characters in STRING are not character head.  We must
	 check also preceding bytes at POS.  We are sure that the gap
	 is at POS.  */
      unsigned char *p = BEG_ADDR;
905
      i = pos_byte - 2;
906
      while (i >= 0 && ! CHAR_HEAD_P (p[i]))
907
	i--;
908
      if (i < 0 || !BASE_LEADING_CODE_P (p[i]))
909
	return 0;
910

911 912 913 914
      bytes = BYTES_BY_CHAR_HEAD (p[i]);
      return (bytes <= pos_byte - 1 - i + length
	      ? 0
	      : bytes - (pos_byte - 1 - i + length));
915
    }
916
  if (!BASE_LEADING_CODE_P (string[i]))
917 918
    return 0;

919 920 921
  bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
  bufp++, pos_byte++;
  while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
922

923
  return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
924 925
}

926
#endif
927

928

929 930 931 932 933
/* 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.  */

void
934 935 936
insert_1_both (const unsigned char *string,
	       EMACS_INT nchars, EMACS_INT nbytes,
	       int inherit, int prepare, int before_markers)
937
{
938 939
  if (nchars == 0)
    return;
940

941 942 943
  if (NILP (current_buffer->enable_multibyte_characters))
    nchars = nbytes;

944 945 946 947 948 949
  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);

950 951 952 953 954
  if (PT != GPT)
    move_gap_both (PT, PT_BYTE);
  if (GAP_SIZE < nbytes)
    make_gap (nbytes - GAP_SIZE);

955 956 957 958 959
#ifdef BYTE_COMBINING_DEBUG
  if (count_combining_before (string, nbytes, PT, PT_BYTE)
      || count_combining_after (string, nbytes, PT, PT_BYTE))
    abort ();
#endif
960 961 962

  /* Record deletion of the surrounding text that combines with
     the insertion.  This, together with recording the insertion,
963 964
     will add up to the right stuff in the undo list.  */
  record_insert (PT, nchars);
965
  MODIFF++;
966
  CHARS_MODIFF = MODIFF;
967 968 969 970

  bcopy (string, GPT_ADDR, nbytes);

  GAP_SIZE -= nbytes;
971 972 973
  GPT += nchars;
  ZV += nchars;
  Z += nchars;
974 975 976 977
  GPT_BYTE += nbytes;
  ZV_BYTE += nbytes;
  Z_BYTE += nbytes;
  if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
978 979 980 981

  if (GPT_BYTE < GPT)
    abort