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