(gap_left): Use BUF_COMPUTE_UNCHANGED.
[bpt/emacs.git] / src / insdel.c
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 86, 93, 94, 95, 97, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <config.h>
23 #ifdef HAVE_STDLIB_H
24 #include <stdlib.h>
25 #endif
26 #include "lisp.h"
27 #include "intervals.h"
28 #include "buffer.h"
29 #include "charset.h"
30 #include "window.h"
31 #include "blockinput.h"
32 #include "region-cache.h"
33
34 #ifndef NULL
35 #define NULL 0
36 #endif
37
38 #define min(x, y) ((x) < (y) ? (x) : (y))
39 #define max(x, y) ((x) > (y) ? (x) : (y))
40
41 static void insert_from_string_1 P_ ((Lisp_Object, int, int, int, int, int, int));
42 static void insert_from_buffer_1 ();
43 static void gap_left P_ ((int, int, int));
44 static void gap_right P_ ((int, int));
45 static void adjust_markers_gap_motion P_ ((int, int, int));
46 static void adjust_markers_for_insert P_ ((int, int, int, int, int, int, int));
47 static void adjust_markers_for_delete P_ ((int, int, int, int));
48 static void adjust_markers_for_record_delete P_ ((int, int, int, int));
49 static void adjust_point P_ ((int, int));
50
51 Lisp_Object Fcombine_after_change_execute ();
52
53 /* Non-nil means don't call the after-change-functions right away,
54 just record an element in Vcombine_after_change_calls_list. */
55 Lisp_Object Vcombine_after_change_calls;
56
57 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
58 describing changes which happened while combine_after_change_calls
59 was nonzero. We use this to decide how to call them
60 once the deferral ends.
61
62 In each element.
63 BEG-UNCHANGED is the number of chars before the changed range.
64 END-UNCHANGED is the number of chars after the changed range,
65 and CHANGE-AMOUNT is the number of characters inserted by the change
66 (negative for a deletion). */
67 Lisp_Object combine_after_change_list;
68
69 /* Buffer which combine_after_change_list is about. */
70 Lisp_Object combine_after_change_buffer;
71 \f
72 /* Check all markers in the current buffer, looking for something invalid. */
73
74 static int check_markers_debug_flag;
75
76 #define CHECK_MARKERS() \
77 if (check_markers_debug_flag) \
78 check_markers (); \
79 else
80
81 void
82 check_markers ()
83 {
84 register Lisp_Object tail, prev, next;
85 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
86
87 tail = BUF_MARKERS (current_buffer);
88
89 while (XSYMBOL (tail) != XSYMBOL (Qnil))
90 {
91 if (XMARKER (tail)->buffer->text != current_buffer->text)
92 abort ();
93 if (XMARKER (tail)->charpos > Z)
94 abort ();
95 if (XMARKER (tail)->bytepos > Z_BYTE)
96 abort ();
97 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (XMARKER (tail)->bytepos)))
98 abort ();
99
100 tail = XMARKER (tail)->chain;
101 }
102 }
103 \f
104 /* Move gap to position CHARPOS.
105 Note that this can quit! */
106
107 void
108 move_gap (charpos)
109 int charpos;
110 {
111 move_gap_both (charpos, charpos_to_bytepos (charpos));
112 }
113
114 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
115 Note that this can quit! */
116
117 void
118 move_gap_both (charpos, bytepos)
119 int charpos, bytepos;
120 {
121 if (bytepos < GPT_BYTE)
122 gap_left (charpos, bytepos, 0);
123 else if (bytepos > GPT_BYTE)
124 gap_right (charpos, bytepos);
125 }
126
127 /* Move the gap to a position less than the current GPT.
128 BYTEPOS describes the new position as a byte position,
129 and CHARPOS is the corresponding char position.
130 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
131
132 static void
133 gap_left (charpos, bytepos, newgap)
134 register int charpos, bytepos;
135 int newgap;
136 {
137 register unsigned char *to, *from;
138 register int i;
139 int new_s1;
140
141 if (!newgap)
142 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
143
144 i = GPT_BYTE;
145 to = GAP_END_ADDR;
146 from = GPT_ADDR;
147 new_s1 = GPT_BYTE;
148
149 /* Now copy the characters. To move the gap down,
150 copy characters up. */
151
152 while (1)
153 {
154 /* I gets number of characters left to copy. */
155 i = new_s1 - bytepos;
156 if (i == 0)
157 break;
158 /* If a quit is requested, stop copying now.
159 Change BYTEPOS to be where we have actually moved the gap to. */
160 if (QUITP)
161 {
162 bytepos = new_s1;
163 charpos = BYTE_TO_CHAR (bytepos);
164 break;
165 }
166 /* Move at most 32000 chars before checking again for a quit. */
167 if (i > 32000)
168 i = 32000;
169 #ifdef GAP_USE_BCOPY
170 if (i >= 128
171 /* bcopy is safe if the two areas of memory do not overlap
172 or on systems where bcopy is always safe for moving upward. */
173 && (BCOPY_UPWARD_SAFE
174 || to - from >= 128))
175 {
176 /* If overlap is not safe, avoid it by not moving too many
177 characters at once. */
178 if (!BCOPY_UPWARD_SAFE && i > to - from)
179 i = to - from;
180 new_s1 -= i;
181 from -= i, to -= i;
182 bcopy (from, to, i);
183 }
184 else
185 #endif
186 {
187 new_s1 -= i;
188 while (--i >= 0)
189 *--to = *--from;
190 }
191 }
192
193 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
194 BYTEPOS is where the loop above stopped, which may be what was specified
195 or may be where a quit was detected. */
196 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
197 GPT_BYTE = bytepos;
198 GPT = charpos;
199 if (bytepos < charpos)
200 abort ();
201 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
202 QUIT;
203 }
204
205 /* Move the gap to a position greater than than the current GPT.
206 BYTEPOS describes the new position as a byte position,
207 and CHARPOS is the corresponding char position. */
208
209 static void
210 gap_right (charpos, bytepos)
211 register int charpos, bytepos;
212 {
213 register unsigned char *to, *from;
214 register int i;
215 int new_s1;
216
217 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
218
219 i = GPT_BYTE;
220 from = GAP_END_ADDR;
221 to = GPT_ADDR;
222 new_s1 = GPT_BYTE;
223
224 /* Now copy the characters. To move the gap up,
225 copy characters down. */
226
227 while (1)
228 {
229 /* I gets number of characters left to copy. */
230 i = bytepos - new_s1;
231 if (i == 0)
232 break;
233 /* If a quit is requested, stop copying now.
234 Change BYTEPOS to be where we have actually moved the gap to. */
235 if (QUITP)
236 {
237 bytepos = new_s1;
238 charpos = BYTE_TO_CHAR (bytepos);
239 break;
240 }
241 /* Move at most 32000 chars before checking again for a quit. */
242 if (i > 32000)
243 i = 32000;
244 #ifdef GAP_USE_BCOPY
245 if (i >= 128
246 /* bcopy is safe if the two areas of memory do not overlap
247 or on systems where bcopy is always safe for moving downward. */
248 && (BCOPY_DOWNWARD_SAFE
249 || from - to >= 128))
250 {
251 /* If overlap is not safe, avoid it by not moving too many
252 characters at once. */
253 if (!BCOPY_DOWNWARD_SAFE && i > from - to)
254 i = from - to;
255 new_s1 += i;
256 bcopy (from, to, i);
257 from += i, to += i;
258 }
259 else
260 #endif
261 {
262 new_s1 += i;
263 while (--i >= 0)
264 *to++ = *from++;
265 }
266 }
267
268 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
269 - GAP_SIZE);
270 GPT = charpos;
271 GPT_BYTE = bytepos;
272 if (bytepos < charpos)
273 abort ();
274 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
275 QUIT;
276 }
277 \f
278 /* Add AMOUNT to the byte position of every marker in the current buffer
279 whose current byte position is between FROM (exclusive) and TO (inclusive).
280
281 Also, any markers past the outside of that interval, in the direction
282 of adjustment, are first moved back to the near end of the interval
283 and then adjusted by AMOUNT.
284
285 When the latter adjustment is done, if AMOUNT is negative,
286 we record the adjustment for undo. (This case happens only for
287 deletion.)
288
289 The markers' character positions are not altered,
290 because gap motion does not affect character positions. */
291
292 int adjust_markers_test;
293
294 static void
295 adjust_markers_gap_motion (from, to, amount)
296 register int from, to, amount;
297 {
298 /* Now that a marker has a bytepos, not counting the gap,
299 nothing needs to be done here. */
300 #if 0
301 Lisp_Object marker;
302 register struct Lisp_Marker *m;
303 register int mpos;
304
305 marker = BUF_MARKERS (current_buffer);
306
307 while (!NILP (marker))
308 {
309 m = XMARKER (marker);
310 mpos = m->bytepos;
311 if (amount > 0)
312 {
313 if (mpos > to && mpos < to + amount)
314 {
315 if (adjust_markers_test)
316 abort ();
317 mpos = to + amount;
318 }
319 }
320 else
321 {
322 /* Here's the case where a marker is inside text being deleted.
323 AMOUNT can be negative for gap motion, too,
324 but then this range contains no markers. */
325 if (mpos > from + amount && mpos <= from)
326 {
327 if (adjust_markers_test)
328 abort ();
329 mpos = from + amount;
330 }
331 }
332 if (mpos > from && mpos <= to)
333 mpos += amount;
334 m->bufpos = mpos;
335 marker = m->chain;
336 }
337 #endif
338 }
339 \f
340 /* Adjust all markers for a deletion
341 whose range in bytes is FROM_BYTE to TO_BYTE.
342 The range in charpos is FROM to TO.
343
344 This function assumes that the gap is adjacent to
345 or inside of the range being deleted. */
346
347 static void
348 adjust_markers_for_delete (from, from_byte, to, to_byte)
349 register int from, from_byte, to, to_byte;
350 {
351 Lisp_Object marker;
352 register struct Lisp_Marker *m;
353 register int charpos;
354
355 marker = BUF_MARKERS (current_buffer);
356
357 while (!NILP (marker))
358 {
359 m = XMARKER (marker);
360 charpos = m->charpos;
361
362 if (charpos > Z)
363 abort ();
364
365 /* If the marker is after the deletion,
366 relocate by number of chars / bytes deleted. */
367 if (charpos > to)
368 {
369 m->charpos -= to - from;
370 m->bytepos -= to_byte - from_byte;
371 }
372
373 /* Here's the case where a marker is inside text being deleted. */
374 else if (charpos > from)
375 {
376 record_marker_adjustment (marker, from - charpos);
377 m->charpos = from;
378 m->bytepos = from_byte;
379 }
380
381 marker = m->chain;
382 }
383 }
384
385 \f
386 /* Adjust all markers for calling record_delete for combining bytes.
387 whose range in bytes is FROM_BYTE to TO_BYTE.
388 The range in charpos is FROM to TO. */
389
390 static void
391 adjust_markers_for_record_delete (from, from_byte, to, to_byte)
392 register int from, from_byte, to, to_byte;
393 {
394 Lisp_Object marker;
395 register struct Lisp_Marker *m;
396 register int charpos;
397
398 marker = BUF_MARKERS (current_buffer);
399
400 while (!NILP (marker))
401 {
402 m = XMARKER (marker);
403 charpos = m->charpos;
404
405 /* If the marker is after the deletion,
406 relocate by number of chars / bytes deleted. */
407 if (charpos > to)
408 ;
409 /* Here's the case where a marker is inside text being deleted. */
410 else if (charpos > from)
411 record_marker_adjustment (marker, from - charpos);
412
413 marker = m->chain;
414 }
415 }
416 \f
417 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
418 to TO / TO_BYTE. We have to relocate the charpos of every marker
419 that points after the insertion (but not their bytepos).
420
421 COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
422 that combine into one character with the text before the insertion.
423 COMBINED_AFTER_BYTES is the number of bytes after the insertion
424 that combine into one character with the last inserted bytes.
425
426 When a marker points at the insertion point,
427 we advance it if either its insertion-type is t
428 or BEFORE_MARKERS is true. */
429
430 static void
431 adjust_markers_for_insert (from, from_byte, to, to_byte,
432 combined_before_bytes, combined_after_bytes,
433 before_markers)
434 register int from, from_byte, to, to_byte;
435 int combined_before_bytes, combined_after_bytes, before_markers;
436 {
437 Lisp_Object marker;
438 int adjusted = 0;
439 int nchars = to - from;
440 int nbytes = to_byte - from_byte;
441
442 marker = BUF_MARKERS (current_buffer);
443
444 while (!NILP (marker))
445 {
446 register struct Lisp_Marker *m = XMARKER (marker);
447
448 /* In a single-byte buffer, a marker's two positions must be equal.
449 (If this insertion is going to combine characters, Z will
450 become different from Z_BYTE, but they might be the same now.
451 If so, the two OLD positions of the marker should be equal.) */
452 if (Z == Z_BYTE)
453 {
454 if (m->charpos != m->bytepos)
455 abort ();
456 }
457
458 if (m->bytepos == from_byte)
459 {
460 if (m->insertion_type || before_markers)
461 {
462 m->bytepos = to_byte + combined_after_bytes;
463 m->charpos = to - combined_before_bytes;
464 /* Point the marker before the combined character,
465 so that undoing the insertion puts it back where it was. */
466 if (combined_after_bytes)
467 DEC_BOTH (m->charpos, m->bytepos);
468 if (m->insertion_type)
469 adjusted = 1;
470 }
471 else if (combined_before_bytes)
472 {
473 /* This marker doesn't "need relocation",
474 but don't leave it pointing in the middle of a character.
475 Point the marker after the combined character,
476 so that undoing the insertion puts it back where it was. */
477 m->bytepos += combined_before_bytes;
478 if (combined_before_bytes == nbytes)
479 /* All new bytes plus combined_after_bytes (if any)
480 are combined. */
481 m->bytepos += combined_after_bytes;
482 }
483 }
484 /* If a marker was pointing into the combining bytes
485 after the insertion, don't leave it there
486 in the middle of a character. */
487 else if (combined_after_bytes && m->bytepos >= from_byte
488 && m->bytepos < from_byte + combined_after_bytes)
489 {
490 /* Put it after the combining bytes. */
491 m->bytepos = to_byte + combined_after_bytes;
492 m->charpos = to - combined_before_bytes;
493 /* Now move it back before the combined character,
494 so that undoing the insertion will put it where it was. */
495 DEC_BOTH (m->charpos, m->bytepos);
496 }
497 else if (m->bytepos > from_byte)
498 {
499 m->bytepos += nbytes;
500 m->charpos += nchars - combined_after_bytes - combined_before_bytes;
501 }
502
503 marker = m->chain;
504 }
505
506 /* Adjusting only markers whose insertion-type is t may result in
507 disordered overlays in the slot `overlays_before'. */
508 if (adjusted)
509 fix_overlays_before (current_buffer, from, to);
510 }
511
512 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
513
514 This is used only when the value of point changes due to an insert
515 or delete; it does not represent a conceptual change in point as a
516 marker. In particular, point is not crossing any interval
517 boundaries, so there's no need to use the usual SET_PT macro. In
518 fact it would be incorrect to do so, because either the old or the
519 new value of point is out of sync with the current set of
520 intervals. */
521
522 static void
523 adjust_point (nchars, nbytes)
524 int nchars, nbytes;
525 {
526 BUF_PT (current_buffer) += nchars;
527 BUF_PT_BYTE (current_buffer) += nbytes;
528
529 /* In a single-byte buffer, the two positions must be equal. */
530 if (ZV == ZV_BYTE
531 && PT != PT_BYTE)
532 abort ();
533 }
534 \f
535 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
536 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
537 (NEW_BYTES).
538
539 See the comment of adjust_markers_for_insert for the args
540 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES. */
541
542 static void
543 adjust_markers_for_replace (from, from_byte, old_chars, old_bytes,
544 new_chars, new_bytes,
545 combined_before_bytes, combined_after_bytes)
546 int from, from_byte, old_chars, old_bytes, new_chars, new_bytes;
547 int combined_before_bytes, combined_after_bytes;
548 {
549 Lisp_Object marker = BUF_MARKERS (current_buffer);
550 int prev_to_byte = from_byte + old_bytes;
551 int diff_chars
552 = (new_chars - combined_before_bytes) - (old_chars + combined_after_bytes);
553 int diff_bytes = new_bytes - old_bytes;
554
555 while (!NILP (marker))
556 {
557 register struct Lisp_Marker *m = XMARKER (marker);
558
559 if (m->bytepos >= prev_to_byte
560 && (old_bytes != 0
561 /* If this is an insertion (replacing 0 chars),
562 reject the case of a marker that is at the
563 insertion point and should stay before the insertion. */
564 || m->bytepos > from_byte || m->insertion_type))
565 {
566 if (m->bytepos < prev_to_byte + combined_after_bytes)
567 {
568 /* Put it after the combining bytes. */
569 m->bytepos = from_byte + new_bytes + combined_after_bytes;
570 m->charpos = from + new_chars - combined_before_bytes;
571 }
572 else
573 {
574 m->charpos += diff_chars;
575 m->bytepos += diff_bytes;
576 }
577 }
578 else if (m->bytepos >= from_byte)
579 {
580 m->charpos = from;
581 m->bytepos = from_byte + combined_before_bytes;
582 /* If all new bytes are combined in addition to that there
583 are after combining bytes, we must set byte position of
584 the marker after the after combining bytes. */
585 if (combined_before_bytes == new_bytes)
586 m->bytepos += combined_after_bytes;
587 }
588
589 marker = m->chain;
590 }
591
592 CHECK_MARKERS ();
593 }
594
595 \f
596 /* Make the gap NBYTES_ADDED bytes longer. */
597
598 void
599 make_gap (nbytes_added)
600 int nbytes_added;
601 {
602 unsigned char *result;
603 Lisp_Object tem;
604 int real_gap_loc;
605 int real_gap_loc_byte;
606 int old_gap_size;
607
608 /* If we have to get more space, get enough to last a while. */
609 nbytes_added += 2000;
610
611 /* Don't allow a buffer size that won't fit in an int
612 even if it will fit in a Lisp integer.
613 That won't work because so many places use `int'. */
614
615 if (Z_BYTE - BEG_BYTE + GAP_SIZE + nbytes_added
616 >= ((unsigned) 1 << (min (BITS_PER_INT, VALBITS) - 1)))
617 error ("Buffer exceeds maximum size");
618
619 BLOCK_INPUT;
620 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
621 result = BUFFER_REALLOC (BEG_ADDR, (Z_BYTE - BEG_BYTE
622 + GAP_SIZE + nbytes_added + 1));
623
624 if (result == 0)
625 {
626 UNBLOCK_INPUT;
627 memory_full ();
628 }
629
630 /* We can't unblock until the new address is properly stored. */
631 BEG_ADDR = result;
632 UNBLOCK_INPUT;
633
634 /* Prevent quitting in move_gap. */
635 tem = Vinhibit_quit;
636 Vinhibit_quit = Qt;
637
638 real_gap_loc = GPT;
639 real_gap_loc_byte = GPT_BYTE;
640 old_gap_size = GAP_SIZE;
641
642 /* Call the newly allocated space a gap at the end of the whole space. */
643 GPT = Z + GAP_SIZE;
644 GPT_BYTE = Z_BYTE + GAP_SIZE;
645 GAP_SIZE = nbytes_added;
646
647 /* Move the new gap down to be consecutive with the end of the old one.
648 This adjusts the markers properly too. */
649 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
650
651 /* Now combine the two into one large gap. */
652 GAP_SIZE += old_gap_size;
653 GPT = real_gap_loc;
654 GPT_BYTE = real_gap_loc_byte;
655
656 /* Put an anchor. */
657 *(Z_ADDR) = 0;
658
659 Vinhibit_quit = tem;
660 }
661 \f
662 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
663 FROM_MULTIBYTE says whether the incoming text is multibyte.
664 TO_MULTIBYTE says whether to store the text as multibyte.
665 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
666
667 Return the number of bytes stored at TO_ADDR. */
668
669 int
670 copy_text (from_addr, to_addr, nbytes,
671 from_multibyte, to_multibyte)
672 unsigned char *from_addr;
673 unsigned char *to_addr;
674 int nbytes;
675 int from_multibyte, to_multibyte;
676 {
677 if (from_multibyte == to_multibyte)
678 {
679 bcopy (from_addr, to_addr, nbytes);
680 return nbytes;
681 }
682 else if (from_multibyte)
683 {
684 int nchars = 0;
685 int bytes_left = nbytes;
686 Lisp_Object tbl = Qnil, temp;
687
688 /* We set the variable tbl to the reverse table of
689 Vnonascii_translation_table in advance. */
690 if (CHAR_TABLE_P (Vnonascii_translation_table))
691 {
692 tbl = Fchar_table_extra_slot (Vnonascii_translation_table,
693 make_number (0));
694 if (!CHAR_TABLE_P (tbl))
695 tbl = Qnil;
696 }
697
698 /* Convert multibyte to single byte. */
699 while (bytes_left > 0)
700 {
701 int thislen, c, c_save;
702 c = c_save = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
703 if (!SINGLE_BYTE_CHAR_P (c))
704 c = multibyte_char_to_unibyte (c, tbl);
705 *to_addr++ = c;
706 from_addr += thislen;
707 bytes_left -= thislen;
708 nchars++;
709 }
710 return nchars;
711 }
712 else
713 {
714 unsigned char *initial_to_addr = to_addr;
715
716 /* Convert single-byte to multibyte. */
717 while (nbytes > 0)
718 {
719 int c = *from_addr++;
720 unsigned char workbuf[4], *str;
721 int len;
722
723 if (c < 0400
724 && (c >= 0240
725 || (c >= 0200 && !NILP (Vnonascii_translation_table))))
726 {
727 c = unibyte_char_to_multibyte (c);
728 len = CHAR_STRING (c, workbuf, str);
729 bcopy (str, to_addr, len);
730 to_addr += len;
731 nbytes--;
732 }
733 else
734 /* Special case for speed. */
735 *to_addr++ = c, nbytes--;
736 }
737 return to_addr - initial_to_addr;
738 }
739 }
740
741 /* Return the number of bytes it would take
742 to convert some single-byte text to multibyte.
743 The single-byte text consists of NBYTES bytes at PTR. */
744
745 int
746 count_size_as_multibyte (ptr, nbytes)
747 unsigned char *ptr;
748 int nbytes;
749 {
750 int i;
751 int outgoing_nbytes = 0;
752
753 for (i = 0; i < nbytes; i++)
754 {
755 unsigned int c = *ptr++;
756
757 if (c < 0200 || (c < 0240 && NILP (Vnonascii_translation_table)))
758 outgoing_nbytes++;
759 else
760 {
761 c = unibyte_char_to_multibyte (c);
762 outgoing_nbytes += CHAR_BYTES (c);
763 }
764 }
765
766 return outgoing_nbytes;
767 }
768 \f
769 /* Insert a string of specified length before point.
770 This function judges multibyteness based on
771 enable_multibyte_characters in the current buffer;
772 it never converts between single-byte and multibyte.
773
774 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
775 prepare_to_modify_buffer could relocate the text. */
776
777 void
778 insert (string, nbytes)
779 register unsigned char *string;
780 register int nbytes;
781 {
782 if (nbytes > 0)
783 {
784 int opoint = PT;
785 insert_1 (string, nbytes, 0, 1, 0);
786 signal_after_change (opoint, 0, PT - opoint);
787 }
788 }
789
790 /* Likewise, but inherit text properties from neighboring characters. */
791
792 void
793 insert_and_inherit (string, nbytes)
794 register unsigned char *string;
795 register int nbytes;
796 {
797 if (nbytes > 0)
798 {
799 int opoint = PT;
800 insert_1 (string, nbytes, 1, 1, 0);
801 signal_after_change (opoint, 0, PT - opoint);
802 }
803 }
804
805 /* Insert the character C before point. Do not inherit text properties. */
806
807 void
808 insert_char (c)
809 int c;
810 {
811 unsigned char workbuf[4], *str;
812 int len;
813
814 if (! NILP (current_buffer->enable_multibyte_characters))
815 len = CHAR_STRING (c, workbuf, str);
816 else
817 {
818 len = 1;
819 workbuf[0] = c;
820 str = workbuf;
821 }
822
823 insert (str, len);
824 }
825
826 /* Insert the null-terminated string S before point. */
827
828 void
829 insert_string (s)
830 char *s;
831 {
832 insert (s, strlen (s));
833 }
834
835 /* Like `insert' except that all markers pointing at the place where
836 the insertion happens are adjusted to point after it.
837 Don't use this function to insert part of a Lisp string,
838 since gc could happen and relocate it. */
839
840 void
841 insert_before_markers (string, nbytes)
842 unsigned char *string;
843 register int nbytes;
844 {
845 if (nbytes > 0)
846 {
847 int opoint = PT;
848
849 insert_1 (string, nbytes, 0, 1, 1);
850 signal_after_change (opoint, 0, PT - opoint);
851 }
852 }
853
854 /* Likewise, but inherit text properties from neighboring characters. */
855
856 void
857 insert_before_markers_and_inherit (string, nbytes)
858 unsigned char *string;
859 register int nbytes;
860 {
861 if (nbytes > 0)
862 {
863 int opoint = PT;
864
865 insert_1 (string, nbytes, 1, 1, 1);
866 signal_after_change (opoint, 0, PT - opoint);
867 }
868 }
869
870 /* Subroutine used by the insert functions above. */
871
872 void
873 insert_1 (string, nbytes, inherit, prepare, before_markers)
874 register unsigned char *string;
875 register int nbytes;
876 int inherit, prepare, before_markers;
877 {
878 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
879 inherit, prepare, before_markers);
880 }
881 \f
882 /* See if the bytes before POS/POS_BYTE combine with bytes
883 at the start of STRING to form a single character.
884 If so, return the number of bytes at the start of STRING
885 which combine in this way. Otherwise, return 0. */
886
887 int
888 count_combining_before (string, length, pos, pos_byte)
889 unsigned char *string;
890 int length;
891 int pos, pos_byte;
892 {
893 int opos = pos, opos_byte = pos_byte;
894 int c;
895 unsigned char *p = string;
896
897 if (NILP (current_buffer->enable_multibyte_characters))
898 return 0;
899 if (length == 0 || CHAR_HEAD_P (*string))
900 return 0;
901 if (pos == BEG)
902 return 0;
903 c = FETCH_BYTE (pos_byte - 1);
904 if (ASCII_BYTE_P (c))
905 return 0;
906 DEC_BOTH (pos, pos_byte);
907 c = FETCH_BYTE (pos_byte);
908 if (! BASE_LEADING_CODE_P (c))
909 return 0;
910
911 /* We have a combination situation.
912 Count the bytes at STRING that will combine. */
913 while (!CHAR_HEAD_P (*p) && p < string + length)
914 p++;
915
916 return p - string;
917 }
918
919 /* See if the bytes after POS/POS_BYTE combine with bytes
920 at the end of STRING to form a single character.
921 If so, return the number of bytes after POS/POS_BYTE
922 which combine in this way. Otherwise, return 0. */
923
924 int
925 count_combining_after (string, length, pos, pos_byte)
926 unsigned char *string;
927 int length;
928 int pos, pos_byte;
929 {
930 int opos = pos, opos_byte = pos_byte;
931 int i;
932 int c;
933
934 if (NILP (current_buffer->enable_multibyte_characters))
935 return 0;
936 if (length > 0 && ASCII_BYTE_P (string[length - 1]))
937 return 0;
938 i = length - 1;
939 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
940 {
941 i--;
942 }
943 if (i < 0)
944 {
945 /* All characters in `string' are not character head.
946 We must check also preceding bytes at POS.
947 We are sure that the gap is at POS. */
948 string = BEG_ADDR;
949 i = pos_byte - 2;
950 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
951 i--;
952 if (i < 0 || !BASE_LEADING_CODE_P (string[i]))
953 return 0;
954 }
955 else if (!BASE_LEADING_CODE_P (string[i]))
956 return 0;
957
958 if (pos == Z)
959 return 0;
960 c = FETCH_BYTE (pos_byte);
961 if (CHAR_HEAD_P (c))
962 return 0;
963 while (pos_byte < Z_BYTE)
964 {
965 c = FETCH_BYTE (pos_byte);
966 if (CHAR_HEAD_P (c))
967 break;
968 pos_byte++;
969 }
970
971 return pos_byte - opos_byte;
972 }
973
974 /* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
975 following the position POS/POS_BYTE to the character preceding POS.
976 If TARGET is after POS+NBYTES, we only have to adjust the character
977 position TARGET, else, if TARGET is after POS, we have to adjust
978 both the character position TARGET and the byte position
979 TARGET_BYTE, else we don't have to do any adjustment. */
980
981 #define ADJUST_CHAR_POS(target, target_byte) \
982 do { \
983 if (target > pos + nbytes) \
984 target -= nbytes; \
985 else if (target >= pos) \
986 { \
987 target = pos; \
988 target_byte = pos_byte + nbytes; \
989 } \
990 } while (0)
991
992 /* Combine NBYTES stray trailing-codes, which were formerly separate
993 characters, with the preceding character. These bytes
994 are located after position POS / POS_BYTE, and the preceding character
995 is located just before that position.
996
997 This function does not adjust markers for byte combining. That
998 should be done in advance by the functions
999 adjust_markers_for_insert or adjust_markers_for_replace. */
1000
1001 static void
1002 combine_bytes (pos, pos_byte, nbytes)
1003 int pos, pos_byte, nbytes;
1004 {
1005 adjust_overlays_for_delete (pos, nbytes);
1006
1007 ADJUST_CHAR_POS (BUF_PT (current_buffer), BUF_PT_BYTE (current_buffer));
1008 ADJUST_CHAR_POS (GPT, GPT_BYTE);
1009 ADJUST_CHAR_POS (Z, Z_BYTE);
1010 ADJUST_CHAR_POS (ZV, ZV_BYTE);
1011
1012 if (BUF_INTERVALS (current_buffer) != 0)
1013 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1014 offset_intervals (current_buffer, pos, - nbytes);
1015 }
1016
1017 void
1018 byte_combining_error ()
1019 {
1020 error ("Byte combining across boundary of accessible buffer text inhibitted");
1021 }
1022
1023 /* If we are going to combine bytes at POS which is at a narrowed
1024 region boundary, signal an error. */
1025 #define CHECK_BYTE_COMBINING_FOR_INSERT(pos) \
1026 do { \
1027 if (combined_before_bytes && pos == BEGV \
1028 || combined_after_bytes && pos == ZV) \
1029 byte_combining_error (); \
1030 } while (0)
1031
1032 \f
1033 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1034 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
1035 are the same as in insert_1. */
1036
1037 void
1038 insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
1039 register unsigned char *string;
1040 register int nchars, nbytes;
1041 int inherit, prepare, before_markers;
1042 {
1043 register Lisp_Object temp;
1044 int combined_before_bytes, combined_after_bytes;
1045
1046 if (NILP (current_buffer->enable_multibyte_characters))
1047 nchars = nbytes;
1048
1049 if (prepare)
1050 /* Do this before moving and increasing the gap,
1051 because the before-change hooks might move the gap
1052 or make it smaller. */
1053 prepare_to_modify_buffer (PT, PT, NULL);
1054
1055 if (PT != GPT)
1056 move_gap_both (PT, PT_BYTE);
1057 if (GAP_SIZE < nbytes)
1058 make_gap (nbytes - GAP_SIZE);
1059
1060 combined_before_bytes
1061 = count_combining_before (string, nbytes, PT, PT_BYTE);
1062 combined_after_bytes
1063 = count_combining_after (string, nbytes, PT, PT_BYTE);
1064 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1065
1066 /* Record deletion of the surrounding text that combines with
1067 the insertion. This, together with recording the insertion,
1068 will add up to the right stuff in the undo list.
1069
1070 But there is no need to actually delete the combining bytes
1071 from the buffer and reinsert them. */
1072
1073 if (combined_after_bytes)
1074 {
1075 Lisp_Object deletion;
1076 deletion = Qnil;
1077
1078 if (! EQ (current_buffer->undo_list, Qt))
1079 deletion = make_buffer_string_both (PT, PT_BYTE,
1080 PT + combined_after_bytes,
1081 PT_BYTE + combined_after_bytes, 1);
1082
1083 adjust_markers_for_record_delete (PT, PT_BYTE,
1084 PT + combined_after_bytes,
1085 PT_BYTE + combined_after_bytes);
1086 if (! EQ (current_buffer->undo_list, Qt))
1087 record_delete (PT, deletion);
1088 }
1089
1090 if (combined_before_bytes)
1091 {
1092 Lisp_Object deletion;
1093 deletion = Qnil;
1094
1095 if (! EQ (current_buffer->undo_list, Qt))
1096 deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
1097 PT, PT_BYTE, 1);
1098 adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
1099 PT, PT_BYTE);
1100 if (! EQ (current_buffer->undo_list, Qt))
1101 record_delete (PT - 1, deletion);
1102 }
1103
1104 record_insert (PT - !!combined_before_bytes,
1105 nchars - combined_before_bytes + !!combined_before_bytes);
1106 MODIFF++;
1107
1108 bcopy (string, GPT_ADDR, nbytes);
1109
1110 GAP_SIZE -= nbytes;
1111 /* When we have combining at the end of the insertion,
1112 this is the character position before the combined character. */
1113 GPT += nchars;
1114 ZV += nchars;
1115 Z += nchars;
1116 GPT_BYTE += nbytes;
1117 ZV_BYTE += nbytes;
1118 Z_BYTE += nbytes;
1119 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1120
1121 if (combined_after_bytes)
1122 move_gap_both (GPT + combined_after_bytes,
1123 GPT_BYTE + combined_after_bytes);
1124
1125 if (GPT_BYTE < GPT)
1126 abort ();
1127
1128 adjust_overlays_for_insert (PT, nchars);
1129 adjust_markers_for_insert (PT, PT_BYTE,
1130 PT + nchars, PT_BYTE + nbytes,
1131 combined_before_bytes, combined_after_bytes,
1132 before_markers);
1133
1134 #ifdef USE_TEXT_PROPERTIES
1135 if (BUF_INTERVALS (current_buffer) != 0)
1136 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1137 offset_intervals (current_buffer, PT, nchars);
1138
1139 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
1140 Fset_text_properties (make_number (PT), make_number (PT + nchars),
1141 Qnil, Qnil);
1142 #endif
1143
1144 {
1145 int pos = PT, pos_byte = PT_BYTE;
1146
1147 adjust_point (nchars + combined_after_bytes,
1148 nbytes + combined_after_bytes);
1149
1150 if (combined_after_bytes)
1151 combine_bytes (pos + nchars, pos_byte + nbytes, combined_after_bytes);
1152
1153 if (combined_before_bytes)
1154 combine_bytes (pos, pos_byte, combined_before_bytes);
1155 }
1156
1157 CHECK_MARKERS ();
1158 }
1159 \f
1160 /* Insert the part of the text of STRING, a Lisp object assumed to be
1161 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1162 starting at position POS / POS_BYTE. If the text of STRING has properties,
1163 copy them into the buffer.
1164
1165 It does not work to use `insert' for this, because a GC could happen
1166 before we bcopy the stuff into the buffer, and relocate the string
1167 without insert noticing. */
1168
1169 void
1170 insert_from_string (string, pos, pos_byte, length, length_byte, inherit)
1171 Lisp_Object string;
1172 register int pos, pos_byte, length, length_byte;
1173 int inherit;
1174 {
1175 int opoint = PT;
1176 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1177 inherit, 0);
1178 signal_after_change (opoint, 0, PT - opoint);
1179 }
1180
1181 /* Like `insert_from_string' except that all markers pointing
1182 at the place where the insertion happens are adjusted to point after it. */
1183
1184 void
1185 insert_from_string_before_markers (string, pos, pos_byte,
1186 length, length_byte, inherit)
1187 Lisp_Object string;
1188 register int pos, pos_byte, length, length_byte;
1189 int inherit;
1190 {
1191 int opoint = PT;
1192 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1193 inherit, 1);
1194 signal_after_change (opoint, 0, PT - opoint);
1195 }
1196
1197 /* Subroutine of the insertion functions above. */
1198
1199 static void
1200 insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
1201 inherit, before_markers)
1202 Lisp_Object string;
1203 register int pos, pos_byte, nchars, nbytes;
1204 int inherit, before_markers;
1205 {
1206 register Lisp_Object temp;
1207 struct gcpro gcpro1;
1208 int outgoing_nbytes = nbytes;
1209 int combined_before_bytes, combined_after_bytes;
1210 int adjusted_nchars;
1211 INTERVAL intervals;
1212
1213 /* Make OUTGOING_NBYTES describe the text
1214 as it will be inserted in this buffer. */
1215
1216 if (NILP (current_buffer->enable_multibyte_characters))
1217 outgoing_nbytes = nchars;
1218 else if (! STRING_MULTIBYTE (string))
1219 outgoing_nbytes
1220 = count_size_as_multibyte (&XSTRING (string)->data[pos_byte],
1221 nbytes);
1222
1223 GCPRO1 (string);
1224 /* Do this before moving and increasing the gap,
1225 because the before-change hooks might move the gap
1226 or make it smaller. */
1227 prepare_to_modify_buffer (PT, PT, NULL);
1228
1229 if (PT != GPT)
1230 move_gap_both (PT, PT_BYTE);
1231 if (GAP_SIZE < outgoing_nbytes)
1232 make_gap (outgoing_nbytes - GAP_SIZE);
1233 UNGCPRO;
1234
1235 /* Copy the string text into the buffer, perhaps converting
1236 between single-byte and multibyte. */
1237 copy_text (XSTRING (string)->data + pos_byte, GPT_ADDR, nbytes,
1238 STRING_MULTIBYTE (string),
1239 ! NILP (current_buffer->enable_multibyte_characters));
1240
1241 /* We have copied text into the gap, but we have not altered
1242 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1243 to these functions and get the same results as we would
1244 have got earlier on. Meanwhile, PT_ADDR does point to
1245 the text that has been stored by copy_text. */
1246
1247 combined_before_bytes
1248 = count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1249 combined_after_bytes
1250 = count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1251 {
1252 unsigned char save = *(GPT_ADDR);
1253 *(GPT_ADDR) = 0;
1254 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1255 *(GPT_ADDR) = save;
1256 }
1257
1258 /* Record deletion of the surrounding text that combines with
1259 the insertion. This, together with recording the insertion,
1260 will add up to the right stuff in the undo list.
1261
1262 But there is no need to actually delete the combining bytes
1263 from the buffer and reinsert them. */
1264
1265 if (combined_after_bytes)
1266 {
1267 Lisp_Object deletion;
1268 deletion = Qnil;
1269
1270 if (! EQ (current_buffer->undo_list, Qt))
1271 deletion = make_buffer_string_both (PT, PT_BYTE,
1272 PT + combined_after_bytes,
1273 PT_BYTE + combined_after_bytes, 1);
1274
1275 adjust_markers_for_record_delete (PT, PT_BYTE,
1276 PT + combined_after_bytes,
1277 PT_BYTE + combined_after_bytes);
1278 if (! EQ (current_buffer->undo_list, Qt))
1279 record_delete (PT, deletion);
1280 }
1281
1282 if (combined_before_bytes)
1283 {
1284 Lisp_Object deletion;
1285 deletion = Qnil;
1286
1287 if (! EQ (current_buffer->undo_list, Qt))
1288 deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
1289 PT, PT_BYTE, 1);
1290 adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
1291 PT, PT_BYTE);
1292 if (! EQ (current_buffer->undo_list, Qt))
1293 record_delete (PT - 1, deletion);
1294 }
1295
1296 record_insert (PT - !!combined_before_bytes,
1297 nchars - combined_before_bytes + !!combined_before_bytes);
1298 MODIFF++;
1299
1300 GAP_SIZE -= outgoing_nbytes;
1301 GPT += nchars;
1302 ZV += nchars;
1303 Z += nchars;
1304 GPT_BYTE += outgoing_nbytes;
1305 ZV_BYTE += outgoing_nbytes;
1306 Z_BYTE += outgoing_nbytes;
1307 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1308
1309 if (combined_after_bytes)
1310 move_gap_both (GPT + combined_after_bytes,
1311 GPT_BYTE + combined_after_bytes);
1312
1313 if (GPT_BYTE < GPT)
1314 abort ();
1315
1316 adjust_overlays_for_insert (PT, nchars);
1317 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1318 PT_BYTE + outgoing_nbytes,
1319 combined_before_bytes, combined_after_bytes,
1320 before_markers);
1321
1322 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1323 offset_intervals (current_buffer, PT, nchars);
1324
1325 intervals = XSTRING (string)->intervals;
1326 /* Get the intervals for the part of the string we are inserting--
1327 not including the combined-before bytes. */
1328 if (nbytes < STRING_BYTES (XSTRING (string)))
1329 intervals = copy_intervals (intervals, pos, nchars);
1330
1331 /* Insert those intervals. */
1332 graft_intervals_into_buffer (intervals, PT, nchars,
1333 current_buffer, inherit);
1334
1335 {
1336 int pos = PT, pos_byte = PT_BYTE;
1337
1338 adjust_point (nchars + combined_after_bytes,
1339 outgoing_nbytes + combined_after_bytes);
1340
1341 if (combined_after_bytes)
1342 combine_bytes (pos + nchars, pos_byte + outgoing_nbytes,
1343 combined_after_bytes);
1344
1345 if (combined_before_bytes)
1346 combine_bytes (pos, pos_byte, combined_before_bytes);
1347 }
1348 }
1349 \f
1350 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1351 current buffer. If the text in BUF has properties, they are absorbed
1352 into the current buffer.
1353
1354 It does not work to use `insert' for this, because a malloc could happen
1355 and relocate BUF's text before the bcopy happens. */
1356
1357 void
1358 insert_from_buffer (buf, charpos, nchars, inherit)
1359 struct buffer *buf;
1360 int charpos, nchars;
1361 int inherit;
1362 {
1363 int opoint = PT;
1364
1365 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1366 signal_after_change (opoint, 0, PT - opoint);
1367 }
1368
1369 static void
1370 insert_from_buffer_1 (buf, from, nchars, inherit)
1371 struct buffer *buf;
1372 int from, nchars;
1373 int inherit;
1374 {
1375 register Lisp_Object temp;
1376 int chunk, chunk_expanded;
1377 int from_byte = buf_charpos_to_bytepos (buf, from);
1378 int to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1379 int incoming_nbytes = to_byte - from_byte;
1380 int outgoing_nbytes = incoming_nbytes;
1381 int combined_before_bytes, combined_after_bytes;
1382 int adjusted_nchars;
1383 INTERVAL intervals;
1384
1385 /* Make OUTGOING_NBYTES describe the text
1386 as it will be inserted in this buffer. */
1387
1388 if (NILP (current_buffer->enable_multibyte_characters))
1389 outgoing_nbytes = nchars;
1390 else if (NILP (buf->enable_multibyte_characters))
1391 {
1392 int outgoing_before_gap = 0;
1393 int outgoing_after_gap = 0;
1394
1395 if (from < BUF_GPT (buf))
1396 {
1397 chunk = BUF_GPT_BYTE (buf) - from_byte;
1398 if (chunk > incoming_nbytes)
1399 chunk = incoming_nbytes;
1400 outgoing_before_gap
1401 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1402 chunk);
1403 }
1404 else
1405 chunk = 0;
1406
1407 if (chunk < incoming_nbytes)
1408 outgoing_after_gap
1409 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1410 from_byte + chunk),
1411 incoming_nbytes - chunk);
1412
1413 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1414 }
1415
1416 /* Make sure point-max won't overflow after this insertion. */
1417 XSETINT (temp, outgoing_nbytes + Z);
1418 if (outgoing_nbytes + Z != XINT (temp))
1419 error ("Maximum buffer size exceeded");
1420
1421 /* Do this before moving and increasing the gap,
1422 because the before-change hooks might move the gap
1423 or make it smaller. */
1424 prepare_to_modify_buffer (PT, PT, NULL);
1425
1426 if (PT != GPT)
1427 move_gap_both (PT, PT_BYTE);
1428 if (GAP_SIZE < outgoing_nbytes)
1429 make_gap (outgoing_nbytes - GAP_SIZE);
1430
1431 if (from < BUF_GPT (buf))
1432 {
1433 chunk = BUF_GPT_BYTE (buf) - from_byte;
1434 if (chunk > incoming_nbytes)
1435 chunk = incoming_nbytes;
1436 /* Record number of output bytes, so we know where
1437 to put the output from the second copy_text. */
1438 chunk_expanded
1439 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1440 GPT_ADDR, chunk,
1441 ! NILP (buf->enable_multibyte_characters),
1442 ! NILP (current_buffer->enable_multibyte_characters));
1443 }
1444 else
1445 chunk_expanded = chunk = 0;
1446
1447 if (chunk < incoming_nbytes)
1448 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1449 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1450 ! NILP (buf->enable_multibyte_characters),
1451 ! NILP (current_buffer->enable_multibyte_characters));
1452
1453 /* We have copied text into the gap, but we have not altered
1454 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1455 to these functions and get the same results as we would
1456 have got earlier on. Meanwhile, GPT_ADDR does point to
1457 the text that has been stored by copy_text. */
1458 combined_before_bytes
1459 = count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1460 combined_after_bytes
1461 = count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1462 {
1463 unsigned char save = *(GPT_ADDR);
1464 *(GPT_ADDR) = 0;
1465 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1466 *(GPT_ADDR) = save;
1467 }
1468
1469 /* Record deletion of the surrounding text that combines with
1470 the insertion. This, together with recording the insertion,
1471 will add up to the right stuff in the undo list.
1472
1473 But there is no need to actually delete the combining bytes
1474 from the buffer and reinsert them. */
1475
1476 if (combined_after_bytes)
1477 {
1478 Lisp_Object deletion;
1479 deletion = Qnil;
1480
1481 if (! EQ (current_buffer->undo_list, Qt))
1482 deletion = make_buffer_string_both (PT, PT_BYTE,
1483 PT + combined_after_bytes,
1484 PT_BYTE + combined_after_bytes, 1);
1485
1486 adjust_markers_for_record_delete (PT, PT_BYTE,
1487 PT + combined_after_bytes,
1488 PT_BYTE + combined_after_bytes);
1489 if (! EQ (current_buffer->undo_list, Qt))
1490 record_delete (PT, deletion);
1491 }
1492
1493 if (combined_before_bytes)
1494 {
1495 Lisp_Object deletion;
1496 deletion = Qnil;
1497
1498 if (! EQ (current_buffer->undo_list, Qt))
1499 deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
1500 PT, PT_BYTE, 1);
1501 adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
1502 PT, PT_BYTE);
1503 if (! EQ (current_buffer->undo_list, Qt))
1504 record_delete (PT - 1, deletion);
1505 }
1506
1507 record_insert (PT - !!combined_before_bytes,
1508 nchars - combined_before_bytes + !!combined_before_bytes);
1509 MODIFF++;
1510
1511 GAP_SIZE -= outgoing_nbytes;
1512 GPT += nchars;
1513 ZV += nchars;
1514 Z += nchars;
1515 GPT_BYTE += outgoing_nbytes;
1516 ZV_BYTE += outgoing_nbytes;
1517 Z_BYTE += outgoing_nbytes;
1518 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1519
1520 if (combined_after_bytes)
1521 move_gap_both (GPT + combined_after_bytes,
1522 GPT_BYTE + combined_after_bytes);
1523
1524 if (GPT_BYTE < GPT)
1525 abort ();
1526
1527 adjust_overlays_for_insert (PT, nchars);
1528 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1529 PT_BYTE + outgoing_nbytes,
1530 combined_before_bytes, combined_after_bytes, 0);
1531
1532 #ifdef USE_TEXT_PROPERTIES
1533 if (BUF_INTERVALS (current_buffer) != 0)
1534 offset_intervals (current_buffer, PT, nchars);
1535 #endif
1536
1537 /* Get the intervals for the part of the string we are inserting--
1538 not including the combined-before bytes. */
1539 intervals = BUF_INTERVALS (buf);
1540 if (outgoing_nbytes < BUF_Z_BYTE (buf) - BUF_BEG_BYTE (buf))
1541 intervals = copy_intervals (intervals, from, nchars);
1542
1543 /* Insert those intervals. */
1544 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1545
1546 {
1547 int pos = PT, pos_byte = PT_BYTE;
1548
1549 adjust_point (nchars + combined_after_bytes,
1550 outgoing_nbytes + combined_after_bytes);
1551
1552 if (combined_after_bytes)
1553 combine_bytes (pos + nchars, pos_byte + outgoing_nbytes,
1554 combined_after_bytes);
1555
1556 if (combined_before_bytes)
1557 combine_bytes (pos, pos_byte, combined_before_bytes);
1558 }
1559 }
1560 \f
1561 /* This function should be called after moving gap to FROM and before
1562 altering text between FROM and TO. This adjusts various position
1563 keepers and markers as if the text is deleted. Don't forget to
1564 call adjust_after_replace after you actually alter the text. */
1565
1566 void
1567 adjust_before_replace (from, from_byte, to, to_byte)
1568 int from, from_byte, to, to_byte;
1569 {
1570 Lisp_Object deletion;
1571
1572 if (! EQ (current_buffer->undo_list, Qt))
1573 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1574
1575 CHECK_MARKERS ();
1576
1577 adjust_markers_for_delete (from, from_byte, to, to_byte);
1578
1579 if (! EQ (current_buffer->undo_list, Qt))
1580 record_delete (from, deletion);
1581
1582 adjust_overlays_for_delete (from, to - from);
1583 }
1584
1585 /* Record undo information and adjust markers and position keepers for
1586 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1587 chars (LEN_BYTE bytes) which resides in the gap just after
1588 GPT_ADDR.
1589
1590 PREV_TEXT nil means the new text was just inserted. */
1591
1592 void
1593 adjust_after_replace (from, from_byte, prev_text, len, len_byte)
1594 int from, from_byte, len, len_byte;
1595 Lisp_Object prev_text;
1596 {
1597 int combined_before_bytes
1598 = count_combining_before (GPT_ADDR, len_byte, from, from_byte);
1599 int combined_after_bytes
1600 = count_combining_after (GPT_ADDR, len_byte, from, from_byte);
1601 /* This flag tells if we combine some bytes with a character before
1602 FROM. This happens even if combined_before_bytes is zero. */
1603 int combine_before = (combined_before_bytes
1604 || (len == 0 && combined_after_bytes));
1605
1606 int nchars_del = 0, nbytes_del = 0;
1607
1608 if (STRINGP (prev_text))
1609 {
1610 nchars_del = XSTRING (prev_text)->size;
1611 nbytes_del = STRING_BYTES (XSTRING (prev_text));
1612 }
1613
1614 if (combine_before && from == BEGV
1615 || combined_after_bytes && from == ZV)
1616 {
1617 /* We can't combine bytes nor signal an error here. So, let's
1618 pretend that the new text is just a single space. */
1619 len = len_byte = 1;
1620 combined_before_bytes = combined_after_bytes = 0;
1621 *(GPT_ADDR) = ' ';
1622 }
1623
1624 if (combined_after_bytes)
1625 {
1626 Lisp_Object deletion;
1627 deletion = Qnil;
1628
1629 if (! EQ (current_buffer->undo_list, Qt))
1630 deletion = make_buffer_string_both (from, from_byte,
1631 from + combined_after_bytes,
1632 from_byte + combined_after_bytes,
1633 1);
1634
1635 adjust_markers_for_record_delete (from, from_byte,
1636 from + combined_after_bytes,
1637 from_byte + combined_after_bytes);
1638
1639 if (! EQ (current_buffer->undo_list, Qt))
1640 record_delete (from + nchars_del, deletion);
1641 }
1642
1643 if (combined_before_bytes
1644 || len_byte == 0 && combined_after_bytes > 0)
1645 {
1646 Lisp_Object deletion;
1647 deletion = Qnil;
1648
1649 if (! EQ (current_buffer->undo_list, Qt))
1650 deletion = make_buffer_string_both (from - 1, CHAR_TO_BYTE (from - 1),
1651 from, from_byte, 1);
1652 adjust_markers_for_record_delete (from - 1, CHAR_TO_BYTE (from - 1),
1653 from, from_byte);
1654 if (! EQ (current_buffer->undo_list, Qt))
1655 record_delete (from - 1, deletion);
1656 }
1657
1658 /* Update various buffer positions for the new text. */
1659 GAP_SIZE -= len_byte;
1660 ZV += len; Z+= len;
1661 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1662 GPT += len; GPT_BYTE += len_byte;
1663 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1664
1665 /* The gap should be at character boundary. */
1666 if (combined_after_bytes)
1667 move_gap_both (GPT + combined_after_bytes,
1668 GPT_BYTE + combined_after_bytes);
1669
1670 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1671 len, len_byte,
1672 combined_before_bytes, combined_after_bytes);
1673 if (! EQ (current_buffer->undo_list, Qt))
1674 {
1675 if (nchars_del > 0)
1676 record_delete (from - combine_before, prev_text);
1677 if (combine_before)
1678 record_insert (from - 1, len - combined_before_bytes + 1);
1679 else
1680 record_insert (from, len);
1681 }
1682
1683 if (len > nchars_del)
1684 adjust_overlays_for_insert (from, len - nchars_del);
1685 else if (len < nchars_del)
1686 adjust_overlays_for_delete (from, nchars_del - len);
1687 #ifdef USE_TEXT_PROPERTIES
1688 if (BUF_INTERVALS (current_buffer) != 0)
1689 {
1690 offset_intervals (current_buffer, from, len - nchars_del);
1691 }
1692 #endif
1693
1694 {
1695 int pos = PT, pos_byte = PT_BYTE;
1696
1697 if (from < PT)
1698 adjust_point (len - nchars_del, len_byte - nbytes_del);
1699
1700 if (combined_after_bytes)
1701 {
1702 if (combined_before_bytes == len_byte)
1703 /* This is the case that all new bytes are combined. */
1704 combined_before_bytes += combined_after_bytes;
1705 else
1706 combine_bytes (from + len, from_byte + len_byte,
1707 combined_after_bytes);
1708 }
1709 if (combined_before_bytes)
1710 combine_bytes (from, from_byte, combined_before_bytes);
1711 }
1712
1713 /* As byte combining will decrease Z, we must check this again. */
1714 if (Z - GPT < END_UNCHANGED)
1715 END_UNCHANGED = Z - GPT;
1716
1717 CHECK_MARKERS ();
1718
1719 if (len == 0)
1720 evaporate_overlays (from);
1721 MODIFF++;
1722 }
1723
1724 /* Record undo information, adjust markers and position keepers for an
1725 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1726 text already exists in the current buffer but character length (TO
1727 - FROM) may be incorrect, the correct length is NEWLEN. */
1728
1729 void
1730 adjust_after_insert (from, from_byte, to, to_byte, newlen)
1731 int from, from_byte, to, to_byte, newlen;
1732 {
1733 int len = to - from, len_byte = to_byte - from_byte;
1734
1735 if (GPT != to)
1736 move_gap_both (to, to_byte);
1737 GAP_SIZE += len_byte;
1738 GPT -= len; GPT_BYTE -= len_byte;
1739 ZV -= len; ZV_BYTE -= len_byte;
1740 Z -= len; Z_BYTE -= len_byte;
1741 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1742 }
1743
1744 /* Replace the text from character positions FROM to TO with NEW,
1745 If PREPARE is nonzero, call prepare_to_modify_buffer.
1746 If INHERIT, the newly inserted text should inherit text properties
1747 from the surrounding non-deleted text. */
1748
1749 /* Note that this does not yet handle markers quite right.
1750 Also it needs to record a single undo-entry that does a replacement
1751 rather than a separate delete and insert.
1752 That way, undo will also handle markers properly.
1753
1754 But if MARKERS is 0, don't relocate markers. */
1755
1756 void
1757 replace_range (from, to, new, prepare, inherit, markers)
1758 Lisp_Object new;
1759 int from, to, prepare, inherit, markers;
1760 {
1761 int inschars = XSTRING (new)->size;
1762 int insbytes = STRING_BYTES (XSTRING (new));
1763 int from_byte, to_byte;
1764 int nbytes_del, nchars_del;
1765 register Lisp_Object temp;
1766 struct gcpro gcpro1;
1767 int combined_before_bytes, combined_after_bytes;
1768 int adjusted_inschars;
1769 INTERVAL intervals;
1770 int outgoing_insbytes = insbytes;
1771 Lisp_Object deletion;
1772
1773 CHECK_MARKERS ();
1774
1775 GCPRO1 (new);
1776
1777 if (prepare)
1778 {
1779 int range_length = to - from;
1780 prepare_to_modify_buffer (from, to, &from);
1781 to = from + range_length;
1782 }
1783
1784 UNGCPRO;
1785
1786 /* Make args be valid */
1787 if (from < BEGV)
1788 from = BEGV;
1789 if (to > ZV)
1790 to = ZV;
1791
1792 from_byte = CHAR_TO_BYTE (from);
1793 to_byte = CHAR_TO_BYTE (to);
1794
1795 nchars_del = to - from;
1796 nbytes_del = to_byte - from_byte;
1797
1798 if (nbytes_del <= 0 && insbytes == 0)
1799 return;
1800
1801 /* Make OUTGOING_INSBYTES describe the text
1802 as it will be inserted in this buffer. */
1803
1804 if (NILP (current_buffer->enable_multibyte_characters))
1805 outgoing_insbytes = inschars;
1806 else if (! STRING_MULTIBYTE (new))
1807 outgoing_insbytes
1808 = count_size_as_multibyte (XSTRING (new)->data, insbytes);
1809
1810 /* Make sure point-max won't overflow after this insertion. */
1811 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1812 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1813 error ("Maximum buffer size exceeded");
1814
1815 GCPRO1 (new);
1816
1817 /* Make sure the gap is somewhere in or next to what we are deleting. */
1818 if (from > GPT)
1819 gap_right (from, from_byte);
1820 if (to < GPT)
1821 gap_left (to, to_byte, 0);
1822
1823 /* Even if we don't record for undo, we must keep the original text
1824 because we may have to recover it because of inappropriate byte
1825 combining. */
1826 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1827
1828 if (markers)
1829 /* Relocate all markers pointing into the new, larger gap
1830 to point at the end of the text before the gap.
1831 Do this before recording the deletion,
1832 so that undo handles this after reinserting the text. */
1833 adjust_markers_for_delete (from, from_byte, to, to_byte);
1834
1835 GAP_SIZE += nbytes_del;
1836 ZV -= nchars_del;
1837 Z -= nchars_del;
1838 ZV_BYTE -= nbytes_del;
1839 Z_BYTE -= nbytes_del;
1840 GPT = from;
1841 GPT_BYTE = from_byte;
1842 *(GPT_ADDR) = 0; /* Put an anchor. */
1843
1844 if (GPT_BYTE < GPT)
1845 abort ();
1846
1847 if (GPT - BEG < BEG_UNCHANGED)
1848 BEG_UNCHANGED = GPT - BEG;
1849 if (Z - GPT < END_UNCHANGED)
1850 END_UNCHANGED = Z - GPT;
1851
1852 if (GAP_SIZE < insbytes)
1853 make_gap (insbytes - GAP_SIZE);
1854
1855 /* Copy the string text into the buffer, perhaps converting
1856 between single-byte and multibyte. */
1857 copy_text (XSTRING (new)->data, GPT_ADDR, insbytes,
1858 STRING_MULTIBYTE (new),
1859 ! NILP (current_buffer->enable_multibyte_characters));
1860
1861 /* We have copied text into the gap, but we have not marked
1862 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1863 here, for both the previous text and the following text.
1864 Meanwhile, GPT_ADDR does point to
1865 the text that has been stored by copy_text. */
1866
1867 combined_before_bytes
1868 = count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte);
1869 combined_after_bytes
1870 = count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte);
1871
1872 if (combined_before_bytes && from == BEGV
1873 || combined_after_bytes && from == ZV)
1874 {
1875 /* Bytes are being combined across the region boundary. We
1876 should avoid it. We recover the original contents before
1877 signaling an error. */
1878 bcopy (XSTRING (deletion)->data, GPT_ADDR, nbytes_del);
1879 GAP_SIZE -= nbytes_del;
1880 ZV += nchars_del;
1881 Z += nchars_del;
1882 ZV_BYTE += nbytes_del;
1883 Z_BYTE += nbytes_del;
1884 GPT = from + nchars_del;
1885 GPT_BYTE = from_byte + nbytes_del;
1886 *(GPT_ADDR) = 0; /* Put an anchor. */
1887 if (markers)
1888 adjust_markers_for_insert (from, from_byte, to, to_byte, 0, 0, 0);
1889 UNGCPRO;
1890 byte_combining_error ();
1891 GCPRO1 (new);
1892 }
1893
1894 /* Record deletion of the surrounding text that combines with
1895 the insertion. This, together with recording the insertion,
1896 will add up to the right stuff in the undo list.
1897
1898 But there is no need to actually delete the combining bytes
1899 from the buffer and reinsert them. */
1900
1901 if (combined_after_bytes)
1902 {
1903 Lisp_Object deletion;
1904 deletion = Qnil;
1905
1906 if (! EQ (current_buffer->undo_list, Qt))
1907 deletion = make_buffer_string_both (from, from_byte,
1908 from + combined_after_bytes,
1909 from_byte + combined_after_bytes,
1910 1);
1911
1912 adjust_markers_for_record_delete (from, from_byte,
1913 from + combined_after_bytes,
1914 from_byte + combined_after_bytes);
1915 if (! EQ (current_buffer->undo_list, Qt))
1916 record_delete (from + nchars_del, deletion);
1917 }
1918
1919 if (combined_before_bytes)
1920 {
1921 Lisp_Object deletion;
1922 deletion = Qnil;
1923
1924 if (! EQ (current_buffer->undo_list, Qt))
1925 deletion = make_buffer_string_both (from - 1, CHAR_TO_BYTE (from - 1),
1926 from, from_byte, 1);
1927 adjust_markers_for_record_delete (from - 1, CHAR_TO_BYTE (from - 1),
1928 from, from_byte);
1929 if (! EQ (current_buffer->undo_list, Qt))
1930 record_delete (from - 1, deletion);
1931 }
1932
1933 if (! EQ (current_buffer->undo_list, Qt))
1934 {
1935 record_delete (from - !!combined_before_bytes, deletion);
1936 record_insert (from - !!combined_before_bytes,
1937 (inschars - combined_before_bytes
1938 + !!combined_before_bytes));
1939 }
1940
1941 GAP_SIZE -= outgoing_insbytes;
1942 GPT += inschars;
1943 ZV += inschars;
1944 Z += inschars;
1945 GPT_BYTE += outgoing_insbytes;
1946 ZV_BYTE += outgoing_insbytes;
1947 Z_BYTE += outgoing_insbytes;
1948 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1949
1950 if (combined_after_bytes)
1951 move_gap_both (GPT + combined_after_bytes,
1952 GPT_BYTE + combined_after_bytes);
1953
1954 if (GPT_BYTE < GPT)
1955 abort ();
1956
1957 /* Adjust the overlay center as needed. This must be done after
1958 adjusting the markers that bound the overlays. */
1959 adjust_overlays_for_delete (from, nchars_del);
1960 adjust_overlays_for_insert (from, inschars);
1961 if (markers)
1962 adjust_markers_for_insert (from, from_byte,
1963 from + inschars, from_byte + outgoing_insbytes,
1964 combined_before_bytes, combined_after_bytes, 0);
1965
1966 #ifdef USE_TEXT_PROPERTIES
1967 offset_intervals (current_buffer, from, inschars - nchars_del);
1968
1969 /* Get the intervals for the part of the string we are inserting--
1970 not including the combined-before bytes. */
1971 intervals = XSTRING (new)->intervals;
1972 /* Insert those intervals. */
1973 graft_intervals_into_buffer (intervals, from, inschars,
1974 current_buffer, inherit);
1975 #endif
1976
1977 /* Relocate point as if it were a marker. */
1978 if (from < PT)
1979 adjust_point ((from + inschars - (PT < to ? PT : to)),
1980 (from_byte + outgoing_insbytes
1981 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1982
1983 if (combined_after_bytes)
1984 {
1985 if (combined_before_bytes == outgoing_insbytes)
1986 /* This is the case that all new bytes are combined. */
1987 combined_before_bytes += combined_after_bytes;
1988 else
1989 combine_bytes (from + inschars, from_byte + outgoing_insbytes,
1990 combined_after_bytes);
1991 }
1992 if (combined_before_bytes)
1993 combine_bytes (from, from_byte, combined_before_bytes);
1994
1995 /* As byte combining will decrease Z, we must check this again. */
1996 if (Z - GPT < END_UNCHANGED)
1997 END_UNCHANGED = Z - GPT;
1998
1999 if (outgoing_insbytes == 0)
2000 evaporate_overlays (from);
2001
2002 CHECK_MARKERS ();
2003
2004 MODIFF++;
2005 UNGCPRO;
2006
2007 signal_after_change (from, nchars_del, GPT - from);
2008 }
2009 \f
2010 /* Delete characters in current buffer
2011 from FROM up to (but not including) TO.
2012 If TO comes before FROM, we delete nothing. */
2013
2014 void
2015 del_range (from, to)
2016 register int from, to;
2017 {
2018 del_range_1 (from, to, 1);
2019 }
2020
2021 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
2022
2023 void
2024 del_range_1 (from, to, prepare)
2025 int from, to, prepare;
2026 {
2027 int from_byte, to_byte;
2028
2029 if (INTEGERP (current_buffer->minibuffer_prompt_length))
2030 {
2031 /* Don't delete part of a mini-buffer prompt. */
2032 int len = XFASTINT (current_buffer->minibuffer_prompt_length);
2033 from = max (from, len);
2034 }
2035
2036 /* Make args be valid */
2037 if (from < BEGV)
2038 from = BEGV;
2039 if (to > ZV)
2040 to = ZV;
2041
2042 if (to <= from)
2043 return;
2044
2045 if (prepare)
2046 {
2047 int range_length = to - from;
2048 prepare_to_modify_buffer (from, to, &from);
2049 to = from + range_length;
2050 }
2051
2052 from_byte = CHAR_TO_BYTE (from);
2053 to_byte = CHAR_TO_BYTE (to);
2054
2055 del_range_2 (from, from_byte, to, to_byte);
2056 signal_after_change (from, to - from, 0);
2057 }
2058
2059 /* Like del_range_1 but args are byte positions, not char positions. */
2060
2061 void
2062 del_range_byte (from_byte, to_byte, prepare)
2063 int from_byte, to_byte, prepare;
2064 {
2065 int from, to;
2066
2067 /* Make args be valid */
2068 if (from_byte < BEGV_BYTE)
2069 from_byte = BEGV_BYTE;
2070 if (to_byte > ZV_BYTE)
2071 to_byte = ZV_BYTE;
2072
2073 if (to_byte <= from_byte)
2074 return;
2075
2076 from = BYTE_TO_CHAR (from_byte);
2077 to = BYTE_TO_CHAR (to_byte);
2078
2079 if (prepare)
2080 {
2081 int old_from = from, old_to = Z - to;
2082 int range_length = to - from;
2083 prepare_to_modify_buffer (from, to, &from);
2084 to = from + range_length;
2085
2086 if (old_from != from)
2087 from_byte = CHAR_TO_BYTE (from);
2088 if (old_to == Z - to)
2089 to_byte = CHAR_TO_BYTE (to);
2090 }
2091
2092 del_range_2 (from, from_byte, to, to_byte);
2093 signal_after_change (from, to - from, 0);
2094 }
2095
2096 /* Like del_range_1, but positions are specified both as charpos
2097 and bytepos. */
2098
2099 void
2100 del_range_both (from, from_byte, to, to_byte, prepare)
2101 int from, from_byte, to, to_byte, prepare;
2102 {
2103 /* Make args be valid */
2104 if (from_byte < BEGV_BYTE)
2105 from_byte = BEGV_BYTE;
2106 if (to_byte > ZV_BYTE)
2107 to_byte = ZV_BYTE;
2108
2109 if (to_byte <= from_byte)
2110 return;
2111
2112 if (from < BEGV)
2113 from = BEGV;
2114 if (to > ZV)
2115 to = ZV;
2116
2117 if (prepare)
2118 {
2119 int old_from = from, old_to = Z - to;
2120 int range_length = to - from;
2121 prepare_to_modify_buffer (from, to, &from);
2122 to = from + range_length;
2123
2124 if (old_from != from)
2125 from_byte = CHAR_TO_BYTE (from);
2126 if (old_to == Z - to)
2127 to_byte = CHAR_TO_BYTE (to);
2128 }
2129
2130 del_range_2 (from, from_byte, to, to_byte);
2131 signal_after_change (from, to - from, 0);
2132 }
2133
2134 /* Delete a range of text, specified both as character positions
2135 and byte positions. FROM and TO are character positions,
2136 while FROM_BYTE and TO_BYTE are byte positions. */
2137
2138 void
2139 del_range_2 (from, from_byte, to, to_byte)
2140 int from, from_byte, to, to_byte;
2141 {
2142 register int nbytes_del, nchars_del;
2143 int combined_after_bytes;
2144 Lisp_Object deletion;
2145 int from_byte_1;
2146
2147 CHECK_MARKERS ();
2148
2149 nchars_del = to - from;
2150 nbytes_del = to_byte - from_byte;
2151
2152 /* Make sure the gap is somewhere in or next to what we are deleting. */
2153 if (from > GPT)
2154 gap_right (from, from_byte);
2155 if (to < GPT)
2156 gap_left (to, to_byte, 0);
2157
2158 combined_after_bytes
2159 = count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
2160 Z_BYTE - to_byte, from, from_byte);
2161 if (combined_after_bytes)
2162 {
2163 if (from == BEGV || to == ZV)
2164 byte_combining_error ();
2165 from_byte_1 = from_byte;
2166 DEC_POS (from_byte_1);
2167 }
2168 else
2169 from_byte_1 = from_byte;
2170
2171 if (! EQ (current_buffer->undo_list, Qt))
2172 deletion
2173 = make_buffer_string_both (from - !!combined_after_bytes,
2174 from_byte_1,
2175 to + combined_after_bytes,
2176 to_byte + combined_after_bytes, 1);
2177 if (combined_after_bytes)
2178 /* COMBINED_AFTER_BYTES nonzero means that the above code moved
2179 the gap. We must move the gap again to a proper place. */
2180 move_gap_both (from, from_byte);
2181
2182 /* Relocate all markers pointing into the new, larger gap
2183 to point at the end of the text before the gap.
2184 Do this before recording the deletion,
2185 so that undo handles this after reinserting the text. */
2186 adjust_markers_for_delete (from, from_byte, to, to_byte);
2187 if (combined_after_bytes)
2188 {
2189 /* Adjust markers for the phony deletion
2190 that we are about to call record_undo for. */
2191
2192 /* Here we delete the markers that formerly
2193 pointed at TO ... TO + COMBINED_AFTER_BYTES.
2194 But because of the call to adjust_markers_for_delete, above,
2195 they now point at FROM ... FROM + COMBINED_AFTER_BYTES. */
2196 adjust_markers_for_record_delete (from, from_byte,
2197 from + combined_after_bytes,
2198 from_byte + combined_after_bytes);
2199
2200 adjust_markers_for_record_delete (from - 1, from_byte_1,
2201 from, from_byte);
2202 }
2203 if (! EQ (current_buffer->undo_list, Qt))
2204 record_delete (from - !!combined_after_bytes, deletion);
2205 MODIFF++;
2206
2207 /* Relocate point as if it were a marker. */
2208 if (from < PT)
2209 adjust_point (from - (PT < to ? PT : to),
2210 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
2211
2212 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2213 offset_intervals (current_buffer, from, - nchars_del);
2214
2215 /* Adjust the overlay center as needed. This must be done after
2216 adjusting the markers that bound the overlays. */
2217 adjust_overlays_for_delete (from, nchars_del);
2218
2219 GAP_SIZE += nbytes_del;
2220 ZV_BYTE -= nbytes_del;
2221 Z_BYTE -= nbytes_del;
2222 ZV -= nchars_del;
2223 Z -= nchars_del;
2224 GPT = from;
2225 GPT_BYTE = from_byte;
2226
2227 if (combined_after_bytes)
2228 move_gap_both (GPT + combined_after_bytes,
2229 GPT_BYTE + combined_after_bytes);
2230
2231 *(GPT_ADDR) = 0; /* Put an anchor. */
2232
2233 if (GPT_BYTE < GPT)
2234 abort ();
2235
2236 if (GPT - BEG < BEG_UNCHANGED)
2237 BEG_UNCHANGED = GPT - BEG;
2238 if (Z - GPT < END_UNCHANGED)
2239 END_UNCHANGED = Z - GPT;
2240
2241 if (combined_after_bytes)
2242 {
2243 /* Adjust markers for byte combining. As we have already
2244 adjuted markers without concerning byte combining, here we
2245 must concern only byte combining. */
2246 adjust_markers_for_replace (from, from_byte, 0, 0, 0, 0,
2247 0, combined_after_bytes);
2248 combine_bytes (from, from_byte, combined_after_bytes);
2249
2250 record_insert (GPT - 1, 1);
2251
2252 if (Z - GPT < END_UNCHANGED)
2253 END_UNCHANGED = Z - GPT;
2254 }
2255
2256 CHECK_MARKERS ();
2257
2258 evaporate_overlays (from);
2259 }
2260 \f
2261 /* Call this if you're about to change the region of BUFFER from
2262 character positions START to END. This checks the read-only
2263 properties of the region, calls the necessary modification hooks,
2264 and warns the next redisplay that it should pay attention to that
2265 area. */
2266
2267 void
2268 modify_region (buffer, start, end)
2269 struct buffer *buffer;
2270 int start, end;
2271 {
2272 struct buffer *old_buffer = current_buffer;
2273
2274 if (buffer != old_buffer)
2275 set_buffer_internal (buffer);
2276
2277 prepare_to_modify_buffer (start, end, NULL);
2278
2279 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
2280
2281 if (MODIFF <= SAVE_MODIFF)
2282 record_first_change ();
2283 MODIFF++;
2284
2285 buffer->point_before_scroll = Qnil;
2286
2287 if (buffer != old_buffer)
2288 set_buffer_internal (old_buffer);
2289 }
2290 \f
2291 /* Check that it is okay to modify the buffer between START and END,
2292 which are char positions.
2293
2294 Run the before-change-function, if any. If intervals are in use,
2295 verify that the text to be modified is not read-only, and call
2296 any modification properties the text may have.
2297
2298 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2299 by holding its value temporarily in a marker. */
2300
2301 void
2302 prepare_to_modify_buffer (start, end, preserve_ptr)
2303 int start, end;
2304 int *preserve_ptr;
2305 {
2306 if (!NILP (current_buffer->read_only))
2307 Fbarf_if_buffer_read_only ();
2308
2309 /* Let redisplay consider other windows than selected_window
2310 if modifying another buffer. */
2311 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2312 ++windows_or_buffers_changed;
2313
2314 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2315 if (BUF_INTERVALS (current_buffer) != 0)
2316 {
2317 if (preserve_ptr)
2318 {
2319 Lisp_Object preserve_marker;
2320 struct gcpro gcpro1;
2321 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
2322 GCPRO1 (preserve_marker);
2323 verify_interval_modification (current_buffer, start, end);
2324 *preserve_ptr = marker_position (preserve_marker);
2325 unchain_marker (preserve_marker);
2326 UNGCPRO;
2327 }
2328 else
2329 verify_interval_modification (current_buffer, start, end);
2330 }
2331
2332 #ifdef CLASH_DETECTION
2333 if (!NILP (current_buffer->file_truename)
2334 /* Make binding buffer-file-name to nil effective. */
2335 && !NILP (current_buffer->filename)
2336 && SAVE_MODIFF >= MODIFF)
2337 lock_file (current_buffer->file_truename);
2338 #else
2339 /* At least warn if this file has changed on disk since it was visited. */
2340 if (!NILP (current_buffer->filename)
2341 && SAVE_MODIFF >= MODIFF
2342 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2343 && !NILP (Ffile_exists_p (current_buffer->filename)))
2344 call1 (intern ("ask-user-about-supersession-threat"),
2345 current_buffer->filename);
2346 #endif /* not CLASH_DETECTION */
2347
2348 signal_before_change (start, end, preserve_ptr);
2349
2350 if (current_buffer->newline_cache)
2351 invalidate_region_cache (current_buffer,
2352 current_buffer->newline_cache,
2353 start - BEG, Z - end);
2354 if (current_buffer->width_run_cache)
2355 invalidate_region_cache (current_buffer,
2356 current_buffer->width_run_cache,
2357 start - BEG, Z - end);
2358
2359 Vdeactivate_mark = Qt;
2360 }
2361 \f
2362 /* These macros work with an argument named `preserve_ptr'
2363 and a local variable named `preserve_marker'. */
2364
2365 #define PRESERVE_VALUE \
2366 if (preserve_ptr && NILP (preserve_marker)) \
2367 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2368
2369 #define RESTORE_VALUE \
2370 if (! NILP (preserve_marker)) \
2371 { \
2372 *preserve_ptr = marker_position (preserve_marker); \
2373 unchain_marker (preserve_marker); \
2374 }
2375
2376 #define PRESERVE_START_END \
2377 if (NILP (start_marker)) \
2378 start_marker = Fcopy_marker (start, Qnil); \
2379 if (NILP (end_marker)) \
2380 end_marker = Fcopy_marker (end, Qnil);
2381
2382 #define FETCH_START \
2383 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2384
2385 #define FETCH_END \
2386 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2387
2388 /* Signal a change to the buffer immediately before it happens.
2389 START_INT and END_INT are the bounds of the text to be changed.
2390
2391 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2392 by holding its value temporarily in a marker. */
2393
2394 void
2395 signal_before_change (start_int, end_int, preserve_ptr)
2396 int start_int, end_int;
2397 int *preserve_ptr;
2398 {
2399 Lisp_Object start, end;
2400 Lisp_Object start_marker, end_marker;
2401 Lisp_Object preserve_marker;
2402 struct gcpro gcpro1, gcpro2, gcpro3;
2403
2404 if (inhibit_modification_hooks)
2405 return;
2406
2407 start = make_number (start_int);
2408 end = make_number (end_int);
2409 preserve_marker = Qnil;
2410 start_marker = Qnil;
2411 end_marker = Qnil;
2412 GCPRO3 (preserve_marker, start_marker, end_marker);
2413
2414 /* If buffer is unmodified, run a special hook for that case. */
2415 if (SAVE_MODIFF >= MODIFF
2416 && !NILP (Vfirst_change_hook)
2417 && !NILP (Vrun_hooks))
2418 {
2419 PRESERVE_VALUE;
2420 PRESERVE_START_END;
2421 call1 (Vrun_hooks, Qfirst_change_hook);
2422 }
2423
2424 /* Run the before-change-function if any.
2425 We don't bother "binding" this variable to nil
2426 because it is obsolete anyway and new code should not use it. */
2427 if (!NILP (Vbefore_change_function))
2428 {
2429 PRESERVE_VALUE;
2430 PRESERVE_START_END;
2431 call2 (Vbefore_change_function, FETCH_START, FETCH_END);
2432 }
2433
2434 /* Now run the before-change-functions if any. */
2435 if (!NILP (Vbefore_change_functions))
2436 {
2437 Lisp_Object args[3];
2438 Lisp_Object before_change_functions;
2439 Lisp_Object after_change_functions;
2440 struct gcpro gcpro1, gcpro2;
2441
2442 PRESERVE_VALUE;
2443 PRESERVE_START_END;
2444
2445 /* "Bind" before-change-functions and after-change-functions
2446 to nil--but in a way that errors don't know about.
2447 That way, if there's an error in them, they will stay nil. */
2448 before_change_functions = Vbefore_change_functions;
2449 after_change_functions = Vafter_change_functions;
2450 Vbefore_change_functions = Qnil;
2451 Vafter_change_functions = Qnil;
2452 GCPRO2 (before_change_functions, after_change_functions);
2453
2454 /* Actually run the hook functions. */
2455 args[0] = Qbefore_change_functions;
2456 args[1] = FETCH_START;
2457 args[2] = FETCH_END;
2458 run_hook_list_with_args (before_change_functions, 3, args);
2459
2460 /* "Unbind" the variables we "bound" to nil. */
2461 Vbefore_change_functions = before_change_functions;
2462 Vafter_change_functions = after_change_functions;
2463 UNGCPRO;
2464 }
2465
2466 if (!NILP (current_buffer->overlays_before)
2467 || !NILP (current_buffer->overlays_after))
2468 {
2469 PRESERVE_VALUE;
2470 report_overlay_modification (FETCH_START, FETCH_END, 0,
2471 FETCH_START, FETCH_END, Qnil);
2472 }
2473
2474 if (! NILP (start_marker))
2475 free_marker (start_marker);
2476 if (! NILP (end_marker))
2477 free_marker (end_marker);
2478 RESTORE_VALUE;
2479 UNGCPRO;
2480 }
2481
2482 /* Signal a change immediately after it happens.
2483 CHARPOS is the character position of the start of the changed text.
2484 LENDEL is the number of characters of the text before the change.
2485 (Not the whole buffer; just the part that was changed.)
2486 LENINS is the number of characters in that part of the text
2487 after the change. */
2488
2489 void
2490 signal_after_change (charpos, lendel, lenins)
2491 int charpos, lendel, lenins;
2492 {
2493 if (inhibit_modification_hooks)
2494 return;
2495
2496 /* If we are deferring calls to the after-change functions
2497 and there are no before-change functions,
2498 just record the args that we were going to use. */
2499 if (! NILP (Vcombine_after_change_calls)
2500 && NILP (Vbefore_change_function) && NILP (Vbefore_change_functions)
2501 && NILP (current_buffer->overlays_before)
2502 && NILP (current_buffer->overlays_after))
2503 {
2504 Lisp_Object elt;
2505
2506 if (!NILP (combine_after_change_list)
2507 && current_buffer != XBUFFER (combine_after_change_buffer))
2508 Fcombine_after_change_execute ();
2509
2510 elt = Fcons (make_number (charpos - BEG),
2511 Fcons (make_number (Z - (charpos - lendel + lenins)),
2512 Fcons (make_number (lenins - lendel), Qnil)));
2513 combine_after_change_list
2514 = Fcons (elt, combine_after_change_list);
2515 combine_after_change_buffer = Fcurrent_buffer ();
2516
2517 return;
2518 }
2519
2520 if (!NILP (combine_after_change_list))
2521 Fcombine_after_change_execute ();
2522
2523 /* Run the after-change-function if any.
2524 We don't bother "binding" this variable to nil
2525 because it is obsolete anyway and new code should not use it. */
2526 if (!NILP (Vafter_change_function))
2527 call3 (Vafter_change_function,
2528 make_number (charpos), make_number (charpos + lenins),
2529 make_number (lendel));
2530
2531 if (!NILP (Vafter_change_functions))
2532 {
2533 Lisp_Object args[4];
2534 Lisp_Object before_change_functions;
2535 Lisp_Object after_change_functions;
2536 struct gcpro gcpro1, gcpro2;
2537
2538 /* "Bind" before-change-functions and after-change-functions
2539 to nil--but in a way that errors don't know about.
2540 That way, if there's an error in them, they will stay nil. */
2541 before_change_functions = Vbefore_change_functions;
2542 after_change_functions = Vafter_change_functions;
2543 Vbefore_change_functions = Qnil;
2544 Vafter_change_functions = Qnil;
2545 GCPRO2 (before_change_functions, after_change_functions);
2546
2547 /* Actually run the hook functions. */
2548 args[0] = Qafter_change_functions;
2549 XSETFASTINT (args[1], charpos);
2550 XSETFASTINT (args[2], charpos + lenins);
2551 XSETFASTINT (args[3], lendel);
2552 run_hook_list_with_args (after_change_functions,
2553 4, args);
2554
2555 /* "Unbind" the variables we "bound" to nil. */
2556 Vbefore_change_functions = before_change_functions;
2557 Vafter_change_functions = after_change_functions;
2558 UNGCPRO;
2559 }
2560
2561 if (!NILP (current_buffer->overlays_before)
2562 || !NILP (current_buffer->overlays_after))
2563 report_overlay_modification (make_number (charpos),
2564 make_number (charpos + lenins),
2565 1,
2566 make_number (charpos),
2567 make_number (charpos + lenins),
2568 make_number (lendel));
2569
2570 /* After an insertion, call the text properties
2571 insert-behind-hooks or insert-in-front-hooks. */
2572 if (lendel == 0)
2573 report_interval_modification (make_number (charpos),
2574 make_number (charpos + lenins));
2575 }
2576
2577 Lisp_Object
2578 Fcombine_after_change_execute_1 (val)
2579 Lisp_Object val;
2580 {
2581 Vcombine_after_change_calls = val;
2582 return val;
2583 }
2584
2585 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2586 Scombine_after_change_execute, 0, 0, 0,
2587 "This function is for use internally in `combine-after-change-calls'.")
2588 ()
2589 {
2590 int count = specpdl_ptr - specpdl;
2591 int beg, end, change;
2592 int begpos, endpos;
2593 Lisp_Object tail;
2594
2595 if (NILP (combine_after_change_list))
2596 return Qnil;
2597
2598 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2599
2600 Fset_buffer (combine_after_change_buffer);
2601
2602 /* # chars unchanged at beginning of buffer. */
2603 beg = Z - BEG;
2604 /* # chars unchanged at end of buffer. */
2605 end = beg;
2606 /* Total amount of insertion (negative for deletion). */
2607 change = 0;
2608
2609 /* Scan the various individual changes,
2610 accumulating the range info in BEG, END and CHANGE. */
2611 for (tail = combine_after_change_list; CONSP (tail);
2612 tail = XCONS (tail)->cdr)
2613 {
2614 Lisp_Object elt;
2615 int thisbeg, thisend, thischange;
2616
2617 /* Extract the info from the next element. */
2618 elt = XCONS (tail)->car;
2619 if (! CONSP (elt))
2620 continue;
2621 thisbeg = XINT (XCONS (elt)->car);
2622
2623 elt = XCONS (elt)->cdr;
2624 if (! CONSP (elt))
2625 continue;
2626 thisend = XINT (XCONS (elt)->car);
2627
2628 elt = XCONS (elt)->cdr;
2629 if (! CONSP (elt))
2630 continue;
2631 thischange = XINT (XCONS (elt)->car);
2632
2633 /* Merge this range into the accumulated range. */
2634 change += thischange;
2635 if (thisbeg < beg)
2636 beg = thisbeg;
2637 if (thisend < end)
2638 end = thisend;
2639 }
2640
2641 /* Get the current start and end positions of the range
2642 that was changed. */
2643 begpos = BEG + beg;
2644 endpos = Z - end;
2645
2646 /* We are about to handle these, so discard them. */
2647 combine_after_change_list = Qnil;
2648
2649 /* Now run the after-change functions for real.
2650 Turn off the flag that defers them. */
2651 record_unwind_protect (Fcombine_after_change_execute_1,
2652 Vcombine_after_change_calls);
2653 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2654
2655 return unbind_to (count, Qnil);
2656 }
2657 \f
2658 void
2659 syms_of_insdel ()
2660 {
2661 staticpro (&combine_after_change_list);
2662 combine_after_change_list = Qnil;
2663 combine_after_change_buffer = Qnil;
2664
2665 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
2666 "Non-nil means enable debugging checks for invalid marker positions.");
2667 check_markers_debug_flag = 0;
2668 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2669 "Used internally by the `combine-after-change-calls' macro.");
2670 Vcombine_after_change_calls = Qnil;
2671
2672 defsubr (&Scombine_after_change_execute);
2673 }