insdel.c 65.6 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Buffer insertion/deletion and gap motion for GNU Emacs.
Richard M. Stallman's avatar
Richard M. Stallman committed
2
   Copyright (C) 1985, 86, 93, 94, 95, 97, 1998 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
3 4 5 6 7

This file is part of GNU Emacs.

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

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
along with GNU Emacs; see the file COPYING.  If not, write to
18 19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
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"
Karl Heuer's avatar
Karl Heuer committed
26
#include "charset.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"
Jim Blandy's avatar
Jim Blandy committed
29

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

34 35
#define min(x, y) ((x) < (y) ? (x) : (y))

36
static void insert_from_string_1 P_ ((Lisp_Object, int, int, int, int, int, int));
37
static void insert_from_buffer_1 ();
38 39 40
static void gap_left P_ ((int, int, int));
static void gap_right P_ ((int, int));
static void adjust_markers_gap_motion P_ ((int, int, int));
41
static void adjust_markers_for_insert P_ ((int, int, int, int, int, int, int));
42
static void adjust_markers_for_delete P_ ((int, int, int, int));
43
static void adjust_markers_for_record_delete P_ ((int, int, int, int));
44
static void adjust_point P_ ((int, int));
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
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;
66

67
/* Move gap to position CHARPOS.
Jim Blandy's avatar
Jim Blandy committed
68 69
   Note that this can quit!  */

70
void
71 72
move_gap (charpos)
     int charpos;
Jim Blandy's avatar
Jim Blandy committed
73
{
74
  move_gap_both (charpos, charpos_to_bytepos (charpos));
Jim Blandy's avatar
Jim Blandy committed
75 76
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
   Note that this can quit!  */

void
move_gap_both (charpos, bytepos)
     int charpos, bytepos;
{
  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
93 94
   If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged.  */

Karl Heuer's avatar
Karl Heuer committed
95
static void
96 97
gap_left (charpos, bytepos, newgap)
     register int charpos, bytepos;
Jim Blandy's avatar
Jim Blandy committed
98 99 100 101 102 103 104 105
     int newgap;
{
  register unsigned char *to, *from;
  register int i;
  int new_s1;

  if (!newgap)
    {
106 107
      if (unchanged_modified == MODIFF
	  && overlay_unchanged_modified == OVERLAY_MODIFF)
Jim Blandy's avatar
Jim Blandy committed
108
	{
109 110
	  beg_unchanged = charpos - BEG;
	  end_unchanged = Z - charpos;
Jim Blandy's avatar
Jim Blandy committed
111 112 113 114 115
	}
      else
	{
	  if (Z - GPT < end_unchanged)
	    end_unchanged = Z - GPT;
116 117
	  if (charpos < beg_unchanged)
	    beg_unchanged = charpos - BEG;
Jim Blandy's avatar
Jim Blandy committed
118 119 120
	}
    }

121
  i = GPT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
122 123
  to = GAP_END_ADDR;
  from = GPT_ADDR;
124
  new_s1 = GPT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
125 126 127 128 129 130 131

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

  while (1)
    {
      /* I gets number of characters left to copy.  */
132
      i = new_s1 - bytepos;
Jim Blandy's avatar
Jim Blandy committed
133 134 135
      if (i == 0)
	break;
      /* If a quit is requested, stop copying now.
136
	 Change BYTEPOS to be where we have actually moved the gap to.  */
Jim Blandy's avatar
Jim Blandy committed
137 138
      if (QUITP)
	{
139 140
	  bytepos = new_s1;
	  charpos = BYTE_TO_CHAR (bytepos);
Jim Blandy's avatar
Jim Blandy committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	  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;
	}
    }

170 171
  /* 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
172
     or may be where a quit was detected.  */
173 174 175 176 177
  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
178
  if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
Jim Blandy's avatar
Jim Blandy committed
179 180 181
  QUIT;
}

182 183 184 185
/* Move the gap to a position greater than than the current GPT.
   BYTEPOS describes the new position as a byte position,
   and CHARPOS is the corresponding char position.  */

Karl Heuer's avatar
Karl Heuer committed
186
static void
187 188
gap_right (charpos, bytepos)
     register int charpos, bytepos;
Jim Blandy's avatar
Jim Blandy committed
189 190 191 192 193
{
  register unsigned char *to, *from;
  register int i;
  int new_s1;

194 195
  if (unchanged_modified == MODIFF
      && overlay_unchanged_modified == OVERLAY_MODIFF)
Jim Blandy's avatar
Jim Blandy committed
196
    {
197 198
      beg_unchanged = charpos - BEG;
      end_unchanged = Z - charpos;
Jim Blandy's avatar
Jim Blandy committed
199 200 201
    }
  else
    {
202 203
      if (Z - charpos - 1 < end_unchanged)
	end_unchanged = Z - charpos;
Jim Blandy's avatar
Jim Blandy committed
204 205 206 207
      if (GPT - BEG < beg_unchanged)
	beg_unchanged = GPT - BEG;
    }

208
  i = GPT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
209 210
  from = GAP_END_ADDR;
  to = GPT_ADDR;
211
  new_s1 = GPT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
212 213 214 215 216 217 218

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

  while (1)
    {
      /* I gets number of characters left to copy.  */
219
      i = bytepos - new_s1;
Jim Blandy's avatar
Jim Blandy committed
220 221 222
      if (i == 0)
	break;
      /* If a quit is requested, stop copying now.
223
	 Change BYTEPOS to be where we have actually moved the gap to.  */
Jim Blandy's avatar
Jim Blandy committed
224 225
      if (QUITP)
	{
226 227
	  bytepos = new_s1;
	  charpos = BYTE_TO_CHAR (bytepos);
Jim Blandy's avatar
Jim Blandy committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	  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++;
	}
    }

257 258 259 260 261 262
  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
263
  if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
Jim Blandy's avatar
Jim Blandy committed
264 265
  QUIT;
}
266

267 268
/* Add AMOUNT to the byte position of every marker in the current buffer
   whose current byte position is between FROM (exclusive) and TO (inclusive).
269

Jim Blandy's avatar
Jim Blandy committed
270 271
   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
272 273 274 275
   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
276 277 278 279 280 281
   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
282

Karl Heuer's avatar
Karl Heuer committed
283
static void
284
adjust_markers_gap_motion (from, to, amount)
Jim Blandy's avatar
Jim Blandy committed
285 286
     register int from, to, amount;
{
287 288 289
  /* 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
290 291 292 293
  Lisp_Object marker;
  register struct Lisp_Marker *m;
  register int mpos;

294
  marker = BUF_MARKERS (current_buffer);
Jim Blandy's avatar
Jim Blandy committed
295

Jim Blandy's avatar
Jim Blandy committed
296
  while (!NILP (marker))
Jim Blandy's avatar
Jim Blandy committed
297 298
    {
      m = XMARKER (marker);
299
      mpos = m->bytepos;
Jim Blandy's avatar
Jim Blandy committed
300 301 302
      if (amount > 0)
	{
	  if (mpos > to && mpos < to + amount)
303 304 305 306 307
	    {
	      if (adjust_markers_test)
		abort ();
	      mpos = to + amount;
	    }
Jim Blandy's avatar
Jim Blandy committed
308 309 310
	}
      else
	{
311 312 313
	  /* 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
314
	  if (mpos > from + amount && mpos <= from)
315
	    {
316 317 318
	      if (adjust_markers_test)
		abort ();
	      mpos = from + amount;
319
	    }
Jim Blandy's avatar
Jim Blandy committed
320 321 322 323 324 325
	}
      if (mpos > from && mpos <= to)
	mpos += amount;
      m->bufpos = mpos;
      marker = m->chain;
    }
326
#endif
Jim Blandy's avatar
Jim Blandy committed
327
}
328

329 330 331 332 333 334
/* 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.  */
335 336

static void
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
adjust_markers_for_delete (from, from_byte, to, to_byte)
     register int from, from_byte, to, to_byte;
{
  Lisp_Object marker;
  register struct Lisp_Marker *m;
  register int charpos;

  marker = BUF_MARKERS (current_buffer);

  while (!NILP (marker))
    {
      m = XMARKER (marker);
      charpos = m->charpos;

      if (charpos > Z)
	abort ();

      /* If the marker is after the deletion,
355
	 relocate by number of chars / bytes deleted.  */
356
      if (charpos > to)
357 358 359 360
	{
	  m->charpos -= to - from;
	  m->bytepos -= to_byte - from_byte;
	}
361

362
      /* Here's the case where a marker is inside text being deleted.  */
363 364 365 366
      else if (charpos > from)
	{
	  record_marker_adjustment (marker, from - charpos);
	  m->charpos = from;
367
	  m->bytepos = from_byte;
368 369 370 371 372
	}

      marker = m->chain;
    }
}
373

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/* Adjust all markers for calling record_delete for combining bytes.
   whose range in bytes is FROM_BYTE to TO_BYTE.
   The range in charpos is FROM to TO.  */

static void
adjust_markers_for_record_delete (from, from_byte, to, to_byte)
     register int from, from_byte, to, to_byte;
{
  Lisp_Object marker;
  register struct Lisp_Marker *m;
  register int charpos;

  marker = BUF_MARKERS (current_buffer);

  while (!NILP (marker))
    {
      m = XMARKER (marker);
      charpos = m->charpos;

      /* If the marker is after the deletion,
	 relocate by number of chars / bytes deleted.  */
      if (charpos > to)
	;
      /* Here's the case where a marker is inside text being deleted.  */
      else if (charpos > from)
	record_marker_adjustment (marker, from - charpos);

      marker = m->chain;
    }
}

405 406 407
/* 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).
408

409 410
   COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
   that combine into one character with the text before the insertion.
411
   COMBINED_AFTER_BYTES is the number of bytes after the insertion
412
   that combine into one character with the last inserted bytes.
413 414 415 416 417 418

   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
419 420 421 422 423
adjust_markers_for_insert (from, from_byte, to, to_byte,
			   combined_before_bytes, combined_after_bytes,
			   before_markers)
     register int from, from_byte, to, to_byte;
     int combined_before_bytes, combined_after_bytes, before_markers;
424 425
{
  Lisp_Object marker;
Karl Heuer's avatar
Karl Heuer committed
426
  int adjusted = 0;
427 428
  int nchars = to - from;
  int nbytes = to_byte - from_byte;
429 430 431 432 433 434

  marker = BUF_MARKERS (current_buffer);

  while (!NILP (marker))
    {
      register struct Lisp_Marker *m = XMARKER (marker);
435 436 437 438 439 440 441 442 443 444 445

      /* In a single-byte buffer, a marker's two positions must be equal.
	 (If this insertion is going to combine characters, Z will
	 become different from Z_BYTE, but they might be the same now.
	 If so, the two OLD positions of the marker should be equal.)  */
      if (Z == Z_BYTE)
	{
	  if (m->charpos != m->bytepos)
	    abort ();
	}

446
      if (m->bytepos == from_byte)
Karl Heuer's avatar
Karl Heuer committed
447
	{
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	  if (m->insertion_type || before_markers)
	    {
	      m->bytepos += nbytes + combined_after_bytes;
	      m->charpos += nchars + !!combined_after_bytes;
	      /* Point the marker before the combined character,
		 so that undoing the insertion puts it back where it was.  */
	      if (combined_after_bytes)
		DEC_BOTH (m->charpos, m->bytepos);
	      if (m->insertion_type)
		adjusted = 1;
	    }
	  else if (combined_before_bytes)
	    {
	      /* This marker doesn't "need relocation",
		 but don't leave it pointing in the middle of a character.
		 Point the marker after the combined character,
		 so that undoing the insertion puts it back where it was.  */
465 466 467 468

	      /* Here we depend on the fact that the gap is after
		 all of the combining bytes that we are going to skip over.  */
	      DEC_BOTH (m->charpos, m->bytepos);
469 470
	      INC_BOTH (m->charpos, m->bytepos);
	    }
Karl Heuer's avatar
Karl Heuer committed
471
	}
472 473 474 475 476 477 478 479 480 481 482 483 484
      /* If a marker was pointing into the combining bytes
	 after the insertion, don't leave it there
	 in the middle of a character.  */
      else if (combined_after_bytes && m->bytepos >= from_byte
	       && m->bytepos < from_byte + combined_after_bytes)
	{
	  /* Put it after the combining bytes.  */
	  m->bytepos = to_byte + combined_after_bytes;
	  m->charpos = to + 1;
	  /* Now move it back before the combined character,
	     so that undoing the insertion will put it where it was.  */
	  DEC_BOTH (m->charpos, m->bytepos);
	}
485 486 487 488 489
      else if (m->bytepos > from_byte)
	{
	  m->bytepos += nbytes;
	  m->charpos += nchars;
	}
490

491 492
      marker = m->chain;
    }
493 494 495

  /* Adjusting only markers whose insertion-type is t may result in
     disordered overlays in the slot `overlays_before'.  */
Karl Heuer's avatar
Karl Heuer committed
496
  if (adjusted)
497
    fix_overlays_before (current_buffer, from, to);
498 499
}

500 501 502 503 504 505 506 507 508 509
/* 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
510
static void
511 512
adjust_point (nchars, nbytes)
     int nchars, nbytes;
Karl Heuer's avatar
Karl Heuer committed
513
{
514 515 516 517 518 519 520
  BUF_PT (current_buffer) += nchars;
  BUF_PT_BYTE (current_buffer) += nbytes;

  /* In a single-byte buffer, the two positions must be equal.  */
  if (ZV == ZV_BYTE
      && PT != PT_BYTE)
    abort ();
Karl Heuer's avatar
Karl Heuer committed
521
}
Jim Blandy's avatar
Jim Blandy committed
522

523
/* Make the gap NBYTES_ADDED bytes longer.  */
Jim Blandy's avatar
Jim Blandy committed
524

525
void
526 527
make_gap (nbytes_added)
     int nbytes_added;
Jim Blandy's avatar
Jim Blandy committed
528 529 530 531
{
  unsigned char *result;
  Lisp_Object tem;
  int real_gap_loc;
532
  int real_gap_loc_byte;
Jim Blandy's avatar
Jim Blandy committed
533 534 535
  int old_gap_size;

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

538 539 540 541
  /* Don't allow a buffer size that won't fit in an int
     even if it will fit in a Lisp integer.
     That won't work because so many places use `int'.  */
     
542
  if (Z_BYTE - BEG_BYTE + GAP_SIZE + nbytes_added
543
      >= ((unsigned) 1 << (min (BITS_PER_INT, VALBITS) - 1)))
544
    error ("Buffer exceeds maximum size");
545

546
  BLOCK_INPUT;
Karl Heuer's avatar
Karl Heuer committed
547
  /* We allocate extra 1-byte `\0' at the tail for anchoring a search.  */
548 549
  result = BUFFER_REALLOC (BEG_ADDR, (Z_BYTE - BEG_BYTE
				      + GAP_SIZE + nbytes_added + 1));
550

Jim Blandy's avatar
Jim Blandy committed
551
  if (result == 0)
552 553 554 555 556 557
    {
      UNBLOCK_INPUT;
      memory_full ();
    }

  /* We can't unblock until the new address is properly stored.  */
Jim Blandy's avatar
Jim Blandy committed
558
  BEG_ADDR = result;
559
  UNBLOCK_INPUT;
Jim Blandy's avatar
Jim Blandy committed
560 561 562 563 564 565

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

  real_gap_loc = GPT;
566
  real_gap_loc_byte = GPT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
567 568 569 570
  old_gap_size = GAP_SIZE;

  /* Call the newly allocated space a gap at the end of the whole space.  */
  GPT = Z + GAP_SIZE;
571
  GPT_BYTE = Z_BYTE + GAP_SIZE;
572
  GAP_SIZE = nbytes_added;
Jim Blandy's avatar
Jim Blandy committed
573 574 575

  /* Move the new gap down to be consecutive with the end of the old one.
     This adjusts the markers properly too.  */
576
  gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
Jim Blandy's avatar
Jim Blandy committed
577 578 579 580

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

Karl Heuer's avatar
Karl Heuer committed
583 584 585
  /* Put an anchor.  */
  *(Z_ADDR) = 0;

Jim Blandy's avatar
Jim Blandy committed
586 587 588
  Vinhibit_quit = tem;
}

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
/* 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.  */

int
copy_text (from_addr, to_addr, nbytes,
	   from_multibyte, to_multibyte)
     unsigned char *from_addr;
     unsigned char *to_addr;
     int nbytes;
     int from_multibyte, to_multibyte;
{
  if (from_multibyte == to_multibyte)
    {
      bcopy (from_addr, to_addr, nbytes);
      return nbytes;
    }
  else if (from_multibyte)
    {
      int nchars = 0;
      int bytes_left = nbytes;

      /* Convert multibyte to single byte.  */
      while (bytes_left > 0)
	{
	  int thislen, c;
	  c = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
619
	  *to_addr++ = SINGLE_BYTE_CHAR_P (c) ? c : (c & 0177) + 0200;
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
	  from_addr += thislen;
	  bytes_left--;
	  nchars++;
	}
      return nchars;
    }
  else
    {
      unsigned char *initial_to_addr = to_addr;

      /* Convert single-byte to multibyte.  */
      while (nbytes > 0)
	{
	  int c = *from_addr++;
	  unsigned char workbuf[4], *str;
	  int len;

637
	  if (c >= 0240 && c < 0400)
638
	    {
639
	      c = unibyte_char_to_multibyte (c);
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
	      len = CHAR_STRING (c, workbuf, str);
	      bcopy (str, to_addr, len);
	      to_addr += len;
	      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.  */

int
count_size_as_multibyte (ptr, nbytes)
     unsigned char *ptr;
     int nbytes;
{
  int i;
  int outgoing_nbytes = 0;

  for (i = 0; i < nbytes; i++)
    {
      unsigned int c = *ptr++;
668 669 670 671

      if (c < 0240)
	outgoing_nbytes++;
      else
672
	{
673 674
	  c = unibyte_char_to_multibyte (c);
	  outgoing_nbytes += XINT (Fchar_bytes (make_number (c)));
675 676 677 678 679 680
	}
    }

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

686 687
   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
688

689
void
690
insert (string, nbytes)
Jim Blandy's avatar
Jim Blandy committed
691
     register unsigned char *string;
692
     register nbytes;
Jim Blandy's avatar
Jim Blandy committed
693
{
694
  if (nbytes > 0)
695
    {
696 697 698
      int opoint = PT;
      insert_1 (string, nbytes, 0, 1, 0);
      signal_after_change (opoint, 0, PT - opoint);
699 700 701
    }
}

702 703
/* Likewise, but inherit text properties from neighboring characters.  */

704
void
705
insert_and_inherit (string, nbytes)
706
     register unsigned char *string;
707
     register nbytes;
708
{
709
  if (nbytes > 0)
710
    {
711 712 713
      int opoint = PT;
      insert_1 (string, nbytes, 1, 1, 0);
      signal_after_change (opoint, 0, PT - opoint);
714 715
    }
}
Jim Blandy's avatar
Jim Blandy committed
716

717
/* Insert the character C before point.  Do not inherit text properties.  */
718

719
void
720 721 722 723
insert_char (c)
     int c;
{
  unsigned char workbuf[4], *str;
724 725 726 727 728 729 730 731 732 733
  int len;

  if (! NILP (current_buffer->enable_multibyte_characters))
    len = CHAR_STRING (c, workbuf, str);
  else
    {
      len = 1;
      workbuf[0] = c;
      str = workbuf;
    }
734 735 736 737

  insert (str, len);
}

738
/* Insert the null-terminated string S before point.  */
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765

void
insert_string (s)
     char *s;
{
  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
insert_before_markers (string, nbytes)
     unsigned char *string;
     register int nbytes;
{
  if (nbytes > 0)
    {
      int opoint = PT;

      insert_1 (string, nbytes, 0, 1, 1);
      signal_after_change (opoint, 0, PT - opoint);
    }
}

766 767
/* Likewise, but inherit text properties from neighboring characters.  */

768 769 770 771 772 773 774 775 776 777 778 779 780
void
insert_before_markers_and_inherit (string, nbytes)
     unsigned char *string;
     register int nbytes;
{
  if (nbytes > 0)
    {
      int opoint = PT;

      insert_1 (string, nbytes, 1, 1, 1);
      signal_after_change (opoint, 0, PT - opoint);
    }
}
781

782 783 784 785
/* Subroutine used by the insert functions above.  */

void
insert_1 (string, nbytes, inherit, prepare, before_markers)
786
     register unsigned char *string;
787 788
     register int nbytes;
     int inherit, prepare, before_markers;
789
{
790 791 792
  insert_1_both (string, chars_in_text (string, nbytes), nbytes,
		 inherit, prepare, before_markers);
}
793

794 795
/* See if the bytes before POS/POS_BYTE combine with bytes
   at the start of STRING to form a single character.
796
   If so, return the number of bytes at the start of STRING
797
   which combine in this way.  Otherwise, return 0.  */
Jim Blandy's avatar
Jim Blandy committed
798

799 800 801 802 803 804 805 806
int
count_combining_before (string, length, pos, pos_byte)
     unsigned char *string;
     int length;
     int pos, pos_byte;
{
  int opos = pos, opos_byte = pos_byte;
  int c;
807
  unsigned char *p = string;
Jim Blandy's avatar
Jim Blandy committed
808

809 810 811 812 813 814 815 816 817 818 819 820 821
  if (NILP (current_buffer->enable_multibyte_characters))
    return 0;
  if (length == 0 || CHAR_HEAD_P (*string))
    return 0;
  if (pos == BEGV)
    return 0;
  c = FETCH_BYTE (pos_byte - 1);
  if (ASCII_BYTE_P (c))
    return 0;
  DEC_BOTH (pos, pos_byte);
  c = FETCH_BYTE (pos_byte);
  if (! BASE_LEADING_CODE_P (c))
    return 0;
822 823 824 825 826 827 828

  /* We have a combination situation.
     Count the bytes at STRING that will combine.  */
  while (!CHAR_HEAD_P (*p) && p < string + length)
    p++;

  return p - string;
829
}
Jim Blandy's avatar
Jim Blandy committed
830

831 832 833 834
/* 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.  */
835

836 837 838 839 840 841 842 843 844
int
count_combining_after (string, length, pos, pos_byte)
     unsigned char *string;
     int length;
     int pos, pos_byte;
{
  int opos = pos, opos_byte = pos_byte;
  int i;
  int c;
845

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
  if (NILP (current_buffer->enable_multibyte_characters))
    return 0;
  if (length == 0 || ASCII_BYTE_P (string[length - 1]))
    return 0;
  i = length - 1;
  while (i > 0 && ! CHAR_HEAD_P (string[i]))
    {
      i--;
    }
  if (! BASE_LEADING_CODE_P (string[i]))
    return 0;

  if (pos == ZV)
    return 0;
  c = FETCH_BYTE (pos_byte);
  if (CHAR_HEAD_P (c))
    return 0;
  while (pos_byte < ZV_BYTE)
    {
      c = FETCH_BYTE (pos_byte);
      if (CHAR_HEAD_P (c))
	break;
      pos_byte++;
    }
870

871
  return pos_byte - opos_byte;
872 873
}

Kenichi Handa's avatar
Kenichi Handa committed
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
/* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
   following the position POS/POS_BYTE to the character preceding POS.
   If TARGET is after POS+NBYTES, we only have to adjust the character
   position TARGET, else, if TARGET is after POS, we have to adjust
   both the character position TARGET and the byte position
   TARGET_BYTE, else we don't have to do any adjustment.  */

#define ADJUST_CHAR_POS(target, target_byte)	\
  do {						\
    if (target > pos + nbytes)			\
      target -= nbytes;				\
    else if (target >= pos)			\
      {						\
	target = pos;				\
	target_byte = pos_byte + nbytes;	\
      }						\
  } while (0)

892 893 894 895 896 897 898 899 900 901 902 903 904 905
/* Combine NBYTES stray trailing-codes, which were formerly separate
   characters, with the preceding character.  These bytes
   are located after position POS / POS_BYTE, and the preceding character
   is located just before that position.  */

static void
combine_bytes (pos, pos_byte, nbytes)
     int pos, pos_byte, nbytes;
{
  /* Adjust all markers.  */
  adjust_markers_for_delete (pos, pos_byte, pos + nbytes, pos_byte);

  adjust_overlays_for_delete (pos, nbytes);

Kenichi Handa's avatar
Kenichi Handa committed
906 907 908 909
  ADJUST_CHAR_POS (BUF_PT (current_buffer), BUF_PT_BYTE (current_buffer));
  ADJUST_CHAR_POS (GPT, GPT_BYTE);
  ADJUST_CHAR_POS (Z, Z_BYTE);
  ADJUST_CHAR_POS (ZV, ZV_BYTE);
910 911 912 913 914 915

  if (BUF_INTERVALS (current_buffer) != 0)
    /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES.  */
    offset_intervals (current_buffer, pos, - nbytes);
}

916 917 918 919 920 921 922 923 924 925
/* 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
insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
     register unsigned char *string;
     register int nchars, nbytes;
     int inherit, prepare, before_markers;
{
926
  register Lisp_Object temp, deletion;
927
  int combined_before_bytes, combined_after_bytes;
928

929 930 931
  if (NILP (current_buffer->enable_multibyte_characters))
    nchars = nbytes;

932 933 934 935 936
  if (PT != GPT)
    move_gap_both (PT, PT_BYTE);
  if (GAP_SIZE < nbytes)
    make_gap (nbytes - GAP_SIZE);

937
  if (prepare)
938 939 940 941 942 943
    prepare_to_modify_buffer (PT, PT, NULL);

  combined_before_bytes
    = count_combining_before (string, nbytes, PT, PT_BYTE);
  combined_after_bytes
    = count_combining_after (string, nbytes, PT, PT_BYTE);
944 945 946 947 948 949 950 951 952

  /* 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.

     But there is no need to actually delete the combining bytes
     from the buffer and reinsert them.  */

  if (combined_after_bytes)
953
    {
954 955 956 957
      deletion = make_buffer_string_both (PT, PT_BYTE,
					  PT + combined_after_bytes,
					  PT_BYTE + combined_after_bytes, 1);

958 959 960
      adjust_markers_for_record_delete (PT, PT_BYTE,
					PT + combined_after_bytes,
					PT_BYTE + combined_after_bytes);
961
      record_delete (PT, deletion);
962
    }
963 964

  if (combined_before_bytes)
965
    {
966 967
      deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
					  PT, PT_BYTE, 1);
968 969
      adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
					PT, PT_BYTE);
970
      record_delete (PT - 1, deletion);
971
    }
972

973 974
  record_insert (PT - !!combined_before_bytes,
		 nchars - combined_before_bytes + !!combined_before_bytes);
975 976 977 978 979
  MODIFF++;

  bcopy (string, GPT_ADDR, nbytes);

  GAP_SIZE -= nbytes;
980 981
  /* When we have combining at the end of the insertion,
     this is the character position before the combined character.  */
982 983 984
  GPT += nchars;
  ZV += nchars;
  Z += nchars;
985 986 987 988
  GPT_BYTE += nbytes;
  ZV_BYTE += nbytes;
  Z_BYTE += nbytes;
  if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
989 990 991 992 993 994 995 996 997

  if (combined_after_bytes)
    move_gap_both (GPT + combined_after_bytes,
		   GPT_BYTE + combined_after_bytes);

  if (GPT_BYTE < GPT)
    abort ();

  adjust_overlays_for_insert (PT, nchars);
998
  adjust_markers_for_insert (PT, PT_BYTE,
999
			     PT + nchars, PT_BYTE + nbytes,
1000
			     combined_before_bytes, combined_after_bytes,
1001
			     before_markers);
1002 1003

#ifdef USE_TEXT_PROPERTIES
1004 1005 1006 1007
  if (BUF_INTERVALS (current_buffer) != 0)
    /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES.  */
    offset_intervals (current_buffer, PT, nchars);

1008
  if (!inherit && BUF_INTERVALS (current_buffer) != 0)
1009
    Fset_text_properties (make_number (PT), make_number (PT + nchars),
1010 1011 1012
			  Qnil, Qnil);
#endif

1013 1014
  {
    int pos = PT, pos_byte = PT_BYTE;
1015

1016 1017
    adjust_point (nchars + combined_after_bytes,
		  nbytes + combined_after_bytes);
1018

1019 1020 1021 1022 1023 1024
    if (combined_after_bytes)
      combine_bytes (pos + nchars, pos_byte + nbytes, combined_after_bytes);

    if (combined_before_bytes)
      combine_bytes (pos, pos_byte, combined_before_bytes);
  }
Jim Blandy's avatar
Jim Blandy committed
1025
}
1026

1027
/* Insert the part of the text of STRING, a Lisp object assumed to be
1028 1029 1030
   of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
   starting at position POS / POS_BYTE.  If the text of STRING has properties,
   copy them into the buffer.
1031 1032

   It does not work to use `insert' for this, because a GC could happen
Jim Blandy's avatar
Jim Blandy committed
1033 1034
   before we bcopy the stuff into the buffer, and relocate the string
   without insert noticing.  */
1035

1036
void
1037
insert_from_string (string, pos, pos_byte, length, length_byte, inherit)
Jim Blandy's avatar
Jim Blandy committed
1038
     Lisp_Object string;
1039
     register int pos, pos_byte, length, length_byte;
1040
     int inherit;
1041 1042 1043
{
  if (length > 0)
    {
1044
      int opoint = PT;
1045 1046
      insert_from_string_1 (string, pos, pos_byte, length, length_byte,
			    inherit, 0);
1047
      signal_after_change (opoint, 0, PT - opoint);
1048 1049 1050
    }
}

1051 1052
/* Like `insert_from_string' except that all markers pointing
   at the place where the insertion happens are adjusted to point after it.  */
1053 1054

void
1055 1056
insert_from_string_before_markers (string, pos, pos_byte,
				   length, length_byte, inherit)
1057
     Lisp_Object string;
1058
     register int pos, pos_byte, length, length_byte;
1059
     int inherit;
1060 1061 1062 1063
{
  if (length > 0)
    {
      int opoint = PT;
1064 1065
      insert_from_string_1 (string, pos, pos_byte, length, length_byte,
			    inherit, 1);
1066 1067 1068 1069 1070 1071 1072
      signal_after_change (opoint, 0, PT - opoint);
    }
}

/* Subroutine of the insertion functions above.  */

static void
1073 1074
insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
		      inherit, before_markers)
1075
     Lisp_Object string;
1076
     register int pos, pos_byte, nchars, nbytes;
1077
     int inherit, before_markers;
Jim Blandy's avatar
Jim Blandy committed
1078 1079 1080
{
  register Lisp_Object temp;
  struct gcpro gcpro1;
1081
  int outgoing_nbytes = nbytes;
1082 1083
  int combined_before_bytes, combined_after_bytes;
  int adjusted_nchars;
1084
  INTERVAL intervals;
1085
  Lisp_Object deletion;
1086 1087 1088 1089 1090 1091

  /* Make OUTGOING_NBYTES describe the text
     as it will be inserted in this buffer.  */

  if (NILP (current_buffer->enable_multibyte_characters))
    outgoing_nbytes = nchars;
1092
  else if (! STRING_MULTIBYTE (string))
1093 1094 1095
    outgoing_nbytes
      = count_size_as_multibyte (&XSTRING (string)->data[pos_byte],
				 nbytes);
Jim Blandy's avatar
Jim Blandy committed
1096 1097

  /* Make sure point-max won't overflow after this insertion.  */
1098 1099
  XSETINT (temp, outgoing_nbytes + Z);
  if (outgoing_nbytes + Z != XINT (temp))
1100
    error ("Maximum buffer size exceeded");