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