(insert_1_both, insert_from_string_1, insert_from_buffer_1):
[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 #include "lisp.h"
24 #include "intervals.h"
25 #include "buffer.h"
26 #include "charset.h"
27 #include "window.h"
28 #include "blockinput.h"
29
30 #ifndef NULL
31 #define NULL 0
32 #endif
33
34 #define min(x, y) ((x) < (y) ? (x) : (y))
35
36 static void insert_from_string_1 P_ ((Lisp_Object, int, int, int, int, int, int));
37 static void insert_from_buffer_1 ();
38 static void gap_left P_ ((int, int, int));
39 static void gap_right P_ ((int, int));
40 static void adjust_markers_gap_motion P_ ((int, int, int));
41 static void adjust_markers_for_insert P_ ((int, int, int, int, int, int, int));
42 static void adjust_markers_for_delete P_ ((int, int, int, int));
43 static void adjust_point P_ ((int, int));
44
45 Lisp_Object Fcombine_after_change_execute ();
46
47 /* Non-nil means don't call the after-change-functions right away,
48 just record an element in Vcombine_after_change_calls_list. */
49 Lisp_Object Vcombine_after_change_calls;
50
51 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
52 describing changes which happened while combine_after_change_calls
53 was nonzero. We use this to decide how to call them
54 once the deferral ends.
55
56 In each element.
57 BEG-UNCHANGED is the number of chars before the changed range.
58 END-UNCHANGED is the number of chars after the changed range,
59 and CHANGE-AMOUNT is the number of characters inserted by the change
60 (negative for a deletion). */
61 Lisp_Object combine_after_change_list;
62
63 /* Buffer which combine_after_change_list is about. */
64 Lisp_Object combine_after_change_buffer;
65 \f
66 /* Move gap to position CHARPOS.
67 Note that this can quit! */
68
69 void
70 move_gap (charpos)
71 int charpos;
72 {
73 move_gap_both (charpos, charpos_to_bytepos (charpos));
74 }
75
76 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
77 Note that this can quit! */
78
79 void
80 move_gap_both (charpos, bytepos)
81 int charpos, bytepos;
82 {
83 if (bytepos < GPT_BYTE)
84 gap_left (charpos, bytepos, 0);
85 else if (bytepos > GPT_BYTE)
86 gap_right (charpos, bytepos);
87 }
88
89 /* Move the gap to a position less than the current GPT.
90 BYTEPOS describes the new position as a byte position,
91 and CHARPOS is the corresponding char position.
92 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
93
94 static void
95 gap_left (charpos, bytepos, newgap)
96 register int charpos, bytepos;
97 int newgap;
98 {
99 register unsigned char *to, *from;
100 register int i;
101 int new_s1;
102
103 if (!newgap)
104 {
105 if (unchanged_modified == MODIFF
106 && overlay_unchanged_modified == OVERLAY_MODIFF)
107 {
108 beg_unchanged = charpos - BEG;
109 end_unchanged = Z - charpos;
110 }
111 else
112 {
113 if (Z - GPT < end_unchanged)
114 end_unchanged = Z - GPT;
115 if (charpos < beg_unchanged)
116 beg_unchanged = charpos - BEG;
117 }
118 }
119
120 i = GPT_BYTE;
121 to = GAP_END_ADDR;
122 from = GPT_ADDR;
123 new_s1 = GPT_BYTE;
124
125 /* Now copy the characters. To move the gap down,
126 copy characters up. */
127
128 while (1)
129 {
130 /* I gets number of characters left to copy. */
131 i = new_s1 - bytepos;
132 if (i == 0)
133 break;
134 /* If a quit is requested, stop copying now.
135 Change BYTEPOS to be where we have actually moved the gap to. */
136 if (QUITP)
137 {
138 bytepos = new_s1;
139 charpos = BYTE_TO_CHAR (bytepos);
140 break;
141 }
142 /* Move at most 32000 chars before checking again for a quit. */
143 if (i > 32000)
144 i = 32000;
145 #ifdef GAP_USE_BCOPY
146 if (i >= 128
147 /* bcopy is safe if the two areas of memory do not overlap
148 or on systems where bcopy is always safe for moving upward. */
149 && (BCOPY_UPWARD_SAFE
150 || to - from >= 128))
151 {
152 /* If overlap is not safe, avoid it by not moving too many
153 characters at once. */
154 if (!BCOPY_UPWARD_SAFE && i > to - from)
155 i = to - from;
156 new_s1 -= i;
157 from -= i, to -= i;
158 bcopy (from, to, i);
159 }
160 else
161 #endif
162 {
163 new_s1 -= i;
164 while (--i >= 0)
165 *--to = *--from;
166 }
167 }
168
169 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
170 BYTEPOS is where the loop above stopped, which may be what was specified
171 or may be where a quit was detected. */
172 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
173 GPT_BYTE = bytepos;
174 GPT = charpos;
175 if (bytepos < charpos)
176 abort ();
177 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
178 QUIT;
179 }
180
181 /* Move the gap to a position greater than than the current GPT.
182 BYTEPOS describes the new position as a byte position,
183 and CHARPOS is the corresponding char position. */
184
185 static void
186 gap_right (charpos, bytepos)
187 register int charpos, bytepos;
188 {
189 register unsigned char *to, *from;
190 register int i;
191 int new_s1;
192
193 if (unchanged_modified == MODIFF
194 && overlay_unchanged_modified == OVERLAY_MODIFF)
195 {
196 beg_unchanged = charpos - BEG;
197 end_unchanged = Z - charpos;
198 }
199 else
200 {
201 if (Z - charpos - 1 < end_unchanged)
202 end_unchanged = Z - charpos;
203 if (GPT - BEG < beg_unchanged)
204 beg_unchanged = GPT - BEG;
205 }
206
207 i = GPT_BYTE;
208 from = GAP_END_ADDR;
209 to = GPT_ADDR;
210 new_s1 = GPT_BYTE;
211
212 /* Now copy the characters. To move the gap up,
213 copy characters down. */
214
215 while (1)
216 {
217 /* I gets number of characters left to copy. */
218 i = bytepos - new_s1;
219 if (i == 0)
220 break;
221 /* If a quit is requested, stop copying now.
222 Change BYTEPOS to be where we have actually moved the gap to. */
223 if (QUITP)
224 {
225 bytepos = new_s1;
226 charpos = BYTE_TO_CHAR (bytepos);
227 break;
228 }
229 /* Move at most 32000 chars before checking again for a quit. */
230 if (i > 32000)
231 i = 32000;
232 #ifdef GAP_USE_BCOPY
233 if (i >= 128
234 /* bcopy is safe if the two areas of memory do not overlap
235 or on systems where bcopy is always safe for moving downward. */
236 && (BCOPY_DOWNWARD_SAFE
237 || from - to >= 128))
238 {
239 /* If overlap is not safe, avoid it by not moving too many
240 characters at once. */
241 if (!BCOPY_DOWNWARD_SAFE && i > from - to)
242 i = from - to;
243 new_s1 += i;
244 bcopy (from, to, i);
245 from += i, to += i;
246 }
247 else
248 #endif
249 {
250 new_s1 += i;
251 while (--i >= 0)
252 *to++ = *from++;
253 }
254 }
255
256 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
257 - GAP_SIZE);
258 GPT = charpos;
259 GPT_BYTE = bytepos;
260 if (bytepos < charpos)
261 abort ();
262 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
263 QUIT;
264 }
265 \f
266 /* Add AMOUNT to the byte position of every marker in the current buffer
267 whose current byte position is between FROM (exclusive) and TO (inclusive).
268
269 Also, any markers past the outside of that interval, in the direction
270 of adjustment, are first moved back to the near end of the interval
271 and then adjusted by AMOUNT.
272
273 When the latter adjustment is done, if AMOUNT is negative,
274 we record the adjustment for undo. (This case happens only for
275 deletion.)
276
277 The markers' character positions are not altered,
278 because gap motion does not affect character positions. */
279
280 int adjust_markers_test;
281
282 static void
283 adjust_markers_gap_motion (from, to, amount)
284 register int from, to, amount;
285 {
286 /* Now that a marker has a bytepos, not counting the gap,
287 nothing needs to be done here. */
288 #if 0
289 Lisp_Object marker;
290 register struct Lisp_Marker *m;
291 register int mpos;
292
293 marker = BUF_MARKERS (current_buffer);
294
295 while (!NILP (marker))
296 {
297 m = XMARKER (marker);
298 mpos = m->bytepos;
299 if (amount > 0)
300 {
301 if (mpos > to && mpos < to + amount)
302 {
303 if (adjust_markers_test)
304 abort ();
305 mpos = to + amount;
306 }
307 }
308 else
309 {
310 /* Here's the case where a marker is inside text being deleted.
311 AMOUNT can be negative for gap motion, too,
312 but then this range contains no markers. */
313 if (mpos > from + amount && mpos <= from)
314 {
315 if (adjust_markers_test)
316 abort ();
317 mpos = from + amount;
318 }
319 }
320 if (mpos > from && mpos <= to)
321 mpos += amount;
322 m->bufpos = mpos;
323 marker = m->chain;
324 }
325 #endif
326 }
327 \f
328 /* Adjust all markers for a deletion
329 whose range in bytes is FROM_BYTE to TO_BYTE.
330 The range in charpos is FROM to TO.
331
332 This function assumes that the gap is adjacent to
333 or inside of the range being deleted. */
334
335 static void
336 adjust_markers_for_delete (from, from_byte, to, to_byte)
337 register int from, from_byte, to, to_byte;
338 {
339 Lisp_Object marker;
340 register struct Lisp_Marker *m;
341 register int charpos;
342 /* This is what GAP_SIZE will be when this deletion is finished. */
343 int coming_gap_size = GAP_SIZE + to_byte - from_byte;
344
345 marker = BUF_MARKERS (current_buffer);
346
347 while (!NILP (marker))
348 {
349 m = XMARKER (marker);
350 charpos = m->charpos;
351
352 if (charpos > Z)
353 abort ();
354
355 /* If the marker is after the deletion,
356 relocate by number of chars / bytes deleted. */
357 if (charpos > to)
358 {
359 m->charpos -= to - from;
360 m->bytepos -= to_byte - from_byte;
361 }
362
363 /* Here's the case where a marker is inside text being deleted. */
364 else if (charpos > from)
365 {
366 record_marker_adjustment (marker, from - charpos);
367 m->charpos = from;
368 m->bytepos = from_byte;
369 }
370
371 /* In a single-byte buffer, a marker's two positions must be equal. */
372 if (Z == Z_BYTE)
373 {
374 register int i = m->bytepos;
375
376 #if 0
377 /* We use FROM_BYTE here instead of GPT_BYTE
378 because FROM_BYTE is where the gap will be after the deletion. */
379 if (i > from_byte + coming_gap_size)
380 i -= coming_gap_size;
381 else if (i > from_byte)
382 i = from_byte;
383 #endif
384
385 if (m->charpos != i)
386 abort ();
387 }
388
389 marker = m->chain;
390 }
391 }
392 \f
393 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
394 to TO / TO_BYTE. We have to relocate the charpos of every marker
395 that points after the insertion (but not their bytepos).
396
397 COMBINED_BEFORE_BYTES is the number of bytes before the insertion
398 that combines into one character with the first inserted bytes.
399 COMBINED_AFTER_BYTES is the number of bytes after the insertion
400 that combines into one character with the last inserted bytes.
401
402 When a marker points at the insertion point,
403 we advance it if either its insertion-type is t
404 or BEFORE_MARKERS is true. */
405
406 static void
407 adjust_markers_for_insert (from, from_byte, to, to_byte,
408 combined_before_bytes, combined_after_bytes,
409 before_markers)
410 register int from, from_byte, to, to_byte;
411 int combined_before_bytes, combined_after_bytes, before_markers;
412 {
413 Lisp_Object marker;
414 int adjusted = 0;
415 int nchars = to - from;
416 int nbytes = to_byte - from_byte;
417
418 marker = BUF_MARKERS (current_buffer);
419
420 while (!NILP (marker))
421 {
422 register struct Lisp_Marker *m = XMARKER (marker);
423 if (m->bytepos == from_byte)
424 {
425 if (m->insertion_type || before_markers)
426 {
427 m->bytepos += nbytes + combined_after_bytes;
428 m->charpos += nchars + !!combined_after_bytes;
429 /* Point the marker before the combined character,
430 so that undoing the insertion puts it back where it was. */
431 if (combined_after_bytes)
432 DEC_BOTH (m->charpos, m->bytepos);
433 if (m->insertion_type)
434 adjusted = 1;
435 }
436 else if (combined_before_bytes)
437 {
438 /* This marker doesn't "need relocation",
439 but don't leave it pointing in the middle of a character.
440 Point the marker after the combined character,
441 so that undoing the insertion puts it back where it was. */
442 m->bytepos -= combined_before_bytes;
443 m->charpos -= 1;
444 INC_BOTH (m->charpos, m->bytepos);
445 }
446 }
447 else if (m->bytepos > from_byte)
448 {
449 m->bytepos += nbytes;
450 m->charpos += nchars;
451 }
452
453 /* In a single-byte buffer, a marker's two positions must be equal. */
454 if (Z == Z_BYTE)
455 {
456 if (m->charpos != m->bytepos)
457 abort ();
458 }
459
460 marker = m->chain;
461 }
462
463 /* Adjusting only markers whose insertion-type is t may result in
464 disordered overlays in the slot `overlays_before'. */
465 if (adjusted)
466 fix_overlays_before (current_buffer, from, to);
467 }
468
469 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
470
471 This is used only when the value of point changes due to an insert
472 or delete; it does not represent a conceptual change in point as a
473 marker. In particular, point is not crossing any interval
474 boundaries, so there's no need to use the usual SET_PT macro. In
475 fact it would be incorrect to do so, because either the old or the
476 new value of point is out of sync with the current set of
477 intervals. */
478
479 static void
480 adjust_point (nchars, nbytes)
481 int nchars, nbytes;
482 {
483 BUF_PT (current_buffer) += nchars;
484 BUF_PT_BYTE (current_buffer) += nbytes;
485
486 /* In a single-byte buffer, the two positions must be equal. */
487 if (ZV == ZV_BYTE
488 && PT != PT_BYTE)
489 abort ();
490 }
491 \f
492 /* Make the gap NBYTES_ADDED bytes longer. */
493
494 void
495 make_gap (nbytes_added)
496 int nbytes_added;
497 {
498 unsigned char *result;
499 Lisp_Object tem;
500 int real_gap_loc;
501 int real_gap_loc_byte;
502 int old_gap_size;
503
504 /* If we have to get more space, get enough to last a while. */
505 nbytes_added += 2000;
506
507 /* Don't allow a buffer size that won't fit in an int
508 even if it will fit in a Lisp integer.
509 That won't work because so many places use `int'. */
510
511 if (Z_BYTE - BEG_BYTE + GAP_SIZE + nbytes_added
512 >= ((unsigned) 1 << (min (BITS_PER_INT, VALBITS) - 1)))
513 error ("Buffer exceeds maximum size");
514
515 BLOCK_INPUT;
516 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
517 result = BUFFER_REALLOC (BEG_ADDR, (Z_BYTE - BEG_BYTE
518 + GAP_SIZE + nbytes_added + 1));
519
520 if (result == 0)
521 {
522 UNBLOCK_INPUT;
523 memory_full ();
524 }
525
526 /* We can't unblock until the new address is properly stored. */
527 BEG_ADDR = result;
528 UNBLOCK_INPUT;
529
530 /* Prevent quitting in move_gap. */
531 tem = Vinhibit_quit;
532 Vinhibit_quit = Qt;
533
534 real_gap_loc = GPT;
535 real_gap_loc_byte = GPT_BYTE;
536 old_gap_size = GAP_SIZE;
537
538 /* Call the newly allocated space a gap at the end of the whole space. */
539 GPT = Z + GAP_SIZE;
540 GPT_BYTE = Z_BYTE + GAP_SIZE;
541 GAP_SIZE = nbytes_added;
542
543 /* Move the new gap down to be consecutive with the end of the old one.
544 This adjusts the markers properly too. */
545 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
546
547 /* Now combine the two into one large gap. */
548 GAP_SIZE += old_gap_size;
549 GPT = real_gap_loc;
550 GPT_BYTE = real_gap_loc_byte;
551
552 /* Put an anchor. */
553 *(Z_ADDR) = 0;
554
555 Vinhibit_quit = tem;
556 }
557 \f
558 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
559 FROM_MULTIBYTE says whether the incoming text is multibyte.
560 TO_MULTIBYTE says whether to store the text as multibyte.
561 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
562
563 Return the number of bytes stored at TO_ADDR. */
564
565 int
566 copy_text (from_addr, to_addr, nbytes,
567 from_multibyte, to_multibyte)
568 unsigned char *from_addr;
569 unsigned char *to_addr;
570 int nbytes;
571 int from_multibyte, to_multibyte;
572 {
573 if (from_multibyte == to_multibyte)
574 {
575 bcopy (from_addr, to_addr, nbytes);
576 return nbytes;
577 }
578 else if (from_multibyte)
579 {
580 int nchars = 0;
581 int bytes_left = nbytes;
582
583 /* Convert multibyte to single byte. */
584 while (bytes_left > 0)
585 {
586 int thislen, c;
587 c = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
588 *to_addr++ = SINGLE_BYTE_CHAR_P (c) ? c : (c & 0177) + 0200;
589 from_addr += thislen;
590 bytes_left--;
591 nchars++;
592 }
593 return nchars;
594 }
595 else
596 {
597 unsigned char *initial_to_addr = to_addr;
598
599 /* Convert single-byte to multibyte. */
600 while (nbytes > 0)
601 {
602 int c = *from_addr++;
603 unsigned char workbuf[4], *str;
604 int len;
605
606 if (c >= 0240 && c < 0400)
607 {
608 c = unibyte_char_to_multibyte (c);
609 len = CHAR_STRING (c, workbuf, str);
610 bcopy (str, to_addr, len);
611 to_addr += len;
612 nbytes--;
613 }
614 else
615 /* Special case for speed. */
616 *to_addr++ = c, nbytes--;
617 }
618 return to_addr - initial_to_addr;
619 }
620 }
621
622 /* Return the number of bytes it would take
623 to convert some single-byte text to multibyte.
624 The single-byte text consists of NBYTES bytes at PTR. */
625
626 int
627 count_size_as_multibyte (ptr, nbytes)
628 unsigned char *ptr;
629 int nbytes;
630 {
631 int i;
632 int outgoing_nbytes = 0;
633
634 for (i = 0; i < nbytes; i++)
635 {
636 unsigned int c = *ptr++;
637
638 if (c < 0240)
639 outgoing_nbytes++;
640 else
641 {
642 c = unibyte_char_to_multibyte (c);
643 outgoing_nbytes += XINT (Fchar_bytes (make_number (c)));
644 }
645 }
646
647 return outgoing_nbytes;
648 }
649 \f
650 /* Insert a string of specified length before point.
651 This function judges multibyteness based on
652 enable_multibyte_characters in the current buffer;
653 it never converts between single-byte and multibyte.
654
655 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
656 prepare_to_modify_buffer could relocate the text. */
657
658 void
659 insert (string, nbytes)
660 register unsigned char *string;
661 register nbytes;
662 {
663 if (nbytes > 0)
664 {
665 int opoint = PT;
666 insert_1 (string, nbytes, 0, 1, 0);
667 signal_after_change (opoint, 0, PT - opoint);
668 }
669 }
670
671 /* Likewise, but inherit text properties from neighboring characters. */
672
673 void
674 insert_and_inherit (string, nbytes)
675 register unsigned char *string;
676 register nbytes;
677 {
678 if (nbytes > 0)
679 {
680 int opoint = PT;
681 insert_1 (string, nbytes, 1, 1, 0);
682 signal_after_change (opoint, 0, PT - opoint);
683 }
684 }
685
686 /* Insert the character C before point. Do not inherit text properties. */
687
688 void
689 insert_char (c)
690 int c;
691 {
692 unsigned char workbuf[4], *str;
693 int len;
694
695 if (! NILP (current_buffer->enable_multibyte_characters))
696 len = CHAR_STRING (c, workbuf, str);
697 else
698 {
699 len = 1;
700 workbuf[0] = c;
701 str = workbuf;
702 }
703
704 insert (str, len);
705 }
706
707 /* Insert the null-terminated string S before point. */
708
709 void
710 insert_string (s)
711 char *s;
712 {
713 insert (s, strlen (s));
714 }
715
716 /* Like `insert' except that all markers pointing at the place where
717 the insertion happens are adjusted to point after it.
718 Don't use this function to insert part of a Lisp string,
719 since gc could happen and relocate it. */
720
721 void
722 insert_before_markers (string, nbytes)
723 unsigned char *string;
724 register int nbytes;
725 {
726 if (nbytes > 0)
727 {
728 int opoint = PT;
729
730 insert_1 (string, nbytes, 0, 1, 1);
731 signal_after_change (opoint, 0, PT - opoint);
732 }
733 }
734
735 /* Likewise, but inherit text properties from neighboring characters. */
736
737 void
738 insert_before_markers_and_inherit (string, nbytes)
739 unsigned char *string;
740 register int nbytes;
741 {
742 if (nbytes > 0)
743 {
744 int opoint = PT;
745
746 insert_1 (string, nbytes, 1, 1, 1);
747 signal_after_change (opoint, 0, PT - opoint);
748 }
749 }
750 \f
751 /* Subroutine used by the insert functions above. */
752
753 void
754 insert_1 (string, nbytes, inherit, prepare, before_markers)
755 register unsigned char *string;
756 register int nbytes;
757 int inherit, prepare, before_markers;
758 {
759 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
760 inherit, prepare, before_markers);
761 }
762
763 /* See if the bytes before POS/POS_BYTE combine with bytes
764 at the start of STRING to form a single character.
765 If so, return the number of bytes before POS/POS_BYTE
766 which combine in this way. Otherwise, return 0. */
767
768 int
769 count_combining_before (string, length, pos, pos_byte)
770 unsigned char *string;
771 int length;
772 int pos, pos_byte;
773 {
774 int opos = pos, opos_byte = pos_byte;
775 int c;
776
777 if (NILP (current_buffer->enable_multibyte_characters))
778 return 0;
779 if (length == 0 || CHAR_HEAD_P (*string))
780 return 0;
781 if (pos == BEGV)
782 return 0;
783 c = FETCH_BYTE (pos_byte - 1);
784 if (ASCII_BYTE_P (c))
785 return 0;
786 DEC_BOTH (pos, pos_byte);
787 c = FETCH_BYTE (pos_byte);
788 if (! BASE_LEADING_CODE_P (c))
789 return 0;
790 return opos_byte - pos_byte;
791 }
792
793 /* See if the bytes after POS/POS_BYTE combine with bytes
794 at the end of STRING to form a single character.
795 If so, return the number of bytes after POS/POS_BYTE
796 which combine in this way. Otherwise, return 0. */
797
798 int
799 count_combining_after (string, length, pos, pos_byte)
800 unsigned char *string;
801 int length;
802 int pos, pos_byte;
803 {
804 int opos = pos, opos_byte = pos_byte;
805 int i;
806 int c;
807
808 if (NILP (current_buffer->enable_multibyte_characters))
809 return 0;
810 if (length == 0 || ASCII_BYTE_P (string[length - 1]))
811 return 0;
812 i = length - 1;
813 while (i > 0 && ! CHAR_HEAD_P (string[i]))
814 {
815 i--;
816 }
817 if (! BASE_LEADING_CODE_P (string[i]))
818 return 0;
819
820 if (pos == ZV)
821 return 0;
822 c = FETCH_BYTE (pos_byte);
823 if (CHAR_HEAD_P (c))
824 return 0;
825 while (pos_byte < ZV_BYTE)
826 {
827 c = FETCH_BYTE (pos_byte);
828 if (CHAR_HEAD_P (c))
829 break;
830 pos_byte++;
831 }
832
833 return pos_byte - opos_byte;
834 }
835
836 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
837 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
838 are the same as in insert_1. */
839
840 void
841 insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
842 register unsigned char *string;
843 register int nchars, nbytes;
844 int inherit, prepare, before_markers;
845 {
846 register Lisp_Object temp;
847 int combined_before_bytes, combined_after_bytes;
848 int adjusted_nchars;
849
850 if (NILP (current_buffer->enable_multibyte_characters))
851 nchars = nbytes;
852
853 if (PT != GPT)
854 move_gap_both (PT, PT_BYTE);
855 if (GAP_SIZE < nbytes)
856 make_gap (nbytes - GAP_SIZE);
857
858 combined_before_bytes = count_combining_before (string, nbytes, PT, PT_BYTE);
859 combined_after_bytes = count_combining_after (string, nbytes, PT, PT_BYTE);
860
861 /* This is the net amount that Z will increase from this insertion. */
862 /* The combined bytes before all count as one character, because
863 they start with a leading code, but the combined bytes after
864 count as separate characters, because they are all trailing codes. */
865
866 adjusted_nchars = nchars - !!combined_before_bytes - combined_after_bytes;
867
868 if (prepare)
869 prepare_to_modify_buffer (PT - !!combined_before_bytes,
870 PT + combined_after_bytes,
871 NULL);
872
873 /* Record deletion of the surrounding text that combines with
874 the insertion. This, together with recording the insertion,
875 will add up to the right stuff in the undo list.
876
877 But there is no need to actually delete the combining bytes
878 from the buffer and reinsert them. */
879
880 if (combined_after_bytes)
881 record_delete (PT, combined_after_bytes);
882
883 if (combined_before_bytes)
884 record_delete (PT - 1, 1);
885
886 record_insert (PT - !!combined_before_bytes, nchars);
887 MODIFF++;
888
889 bcopy (string, GPT_ADDR, nbytes);
890
891 #ifdef USE_TEXT_PROPERTIES
892 if (BUF_INTERVALS (current_buffer) != 0)
893 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
894 offset_intervals (current_buffer, PT, adjusted_nchars);
895 #endif
896
897 GAP_SIZE -= nbytes;
898 GPT += nchars - !! combined_before_bytes - !!combined_after_bytes;
899 ZV += adjusted_nchars;
900 Z += adjusted_nchars;
901 GPT_BYTE += nbytes;
902 ZV_BYTE += nbytes;
903 Z_BYTE += nbytes;
904 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
905 adjust_overlays_for_insert (PT, adjusted_nchars);
906 adjust_markers_for_insert (PT, PT_BYTE,
907 PT + adjusted_nchars, PT_BYTE + nbytes,
908 combined_before_bytes, combined_after_bytes,
909 before_markers);
910 adjust_point (adjusted_nchars + combined_after_bytes,
911 nbytes + combined_after_bytes);
912
913 if (combined_after_bytes)
914 move_gap_both (GPT + 1, GPT_BYTE + combined_after_bytes);
915
916 if (GPT_BYTE < GPT)
917 abort ();
918
919 #ifdef USE_TEXT_PROPERTIES
920 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
921 Fset_text_properties (make_number (PT - adjusted_nchars), make_number (PT),
922 Qnil, Qnil);
923 #endif
924 }
925 \f
926 /* Insert the part of the text of STRING, a Lisp object assumed to be
927 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
928 starting at position POS / POS_BYTE. If the text of STRING has properties,
929 copy them into the buffer.
930
931 It does not work to use `insert' for this, because a GC could happen
932 before we bcopy the stuff into the buffer, and relocate the string
933 without insert noticing. */
934
935 void
936 insert_from_string (string, pos, pos_byte, length, length_byte, inherit)
937 Lisp_Object string;
938 register int pos, pos_byte, length, length_byte;
939 int inherit;
940 {
941 if (length > 0)
942 {
943 int opoint = PT;
944 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
945 inherit, 0);
946 signal_after_change (opoint, 0, PT - opoint);
947 }
948 }
949
950 /* Like `insert_from_string' except that all markers pointing
951 at the place where the insertion happens are adjusted to point after it. */
952
953 void
954 insert_from_string_before_markers (string, pos, pos_byte,
955 length, length_byte, inherit)
956 Lisp_Object string;
957 register int pos, pos_byte, length, length_byte;
958 int inherit;
959 {
960 if (length > 0)
961 {
962 int opoint = PT;
963 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
964 inherit, 1);
965 signal_after_change (opoint, 0, PT - opoint);
966 }
967 }
968
969 /* Subroutine of the insertion functions above. */
970
971 static void
972 insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
973 inherit, before_markers)
974 Lisp_Object string;
975 register int pos, pos_byte, nchars, nbytes;
976 int inherit, before_markers;
977 {
978 register Lisp_Object temp;
979 struct gcpro gcpro1;
980 int outgoing_nbytes = nbytes;
981 int combined_before_bytes, combined_after_bytes;
982 int adjusted_nchars;
983
984 /* Make OUTGOING_NBYTES describe the text
985 as it will be inserted in this buffer. */
986
987 if (NILP (current_buffer->enable_multibyte_characters))
988 outgoing_nbytes = nchars;
989 else if (nchars == nbytes)
990 outgoing_nbytes
991 = count_size_as_multibyte (&XSTRING (string)->data[pos_byte],
992 nbytes);
993
994 /* Make sure point-max won't overflow after this insertion. */
995 XSETINT (temp, outgoing_nbytes + Z);
996 if (outgoing_nbytes + Z != XINT (temp))
997 error ("Maximum buffer size exceeded");
998
999 GCPRO1 (string);
1000 prepare_to_modify_buffer (PT, PT, NULL);
1001
1002 if (PT != GPT)
1003 move_gap_both (PT, PT_BYTE);
1004 if (GAP_SIZE < nbytes)
1005 make_gap (outgoing_nbytes - GAP_SIZE);
1006 UNGCPRO;
1007
1008 /* Copy the string text into the buffer, perhaps converting
1009 between single-byte and multibyte. */
1010 copy_text (XSTRING (string)->data + pos_byte, GPT_ADDR, nbytes,
1011 /* If these are equal, it is a single-byte string.
1012 Its chars are either ASCII, in which case copy_text
1013 won't change it, or single-byte non-ASCII chars,
1014 that need to be changed. */
1015 nchars != nbytes,
1016 ! NILP (current_buffer->enable_multibyte_characters));
1017
1018 /* We have copied text into the gap, but we have not altered
1019 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1020 to these functions and get the same results as we would
1021 have got earlier on. Meanwhile, PT_ADDR does point to
1022 the text that has been stored by copy_text. */
1023
1024 combined_before_bytes
1025 = count_combining_before (XSTRING (string)->data + pos_byte, nbytes,
1026 PT, PT_BYTE);
1027 combined_after_bytes
1028 = count_combining_after (XSTRING (string)->data + pos_byte, nbytes,
1029 PT, PT_BYTE);
1030
1031 /* This is the net amount that Z will increase from this insertion. */
1032 adjusted_nchars = nchars - !!combined_before_bytes - combined_after_bytes;
1033
1034 /* Record deletion of the surrounding text that combines with
1035 the insertion. This, together with recording the insertion,
1036 will add up to the right stuff in the undo list.
1037
1038 But there is no need to actually delete the combining bytes
1039 from the buffer and reinsert them. */
1040
1041 if (combined_after_bytes)
1042 record_delete (PT, combined_after_bytes);
1043
1044 if (combined_before_bytes)
1045 record_delete (PT - 1, 1);
1046
1047 record_insert (PT - !!combined_before_bytes, nchars);
1048 MODIFF++;
1049
1050 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1051 offset_intervals (current_buffer, PT, adjusted_nchars);
1052
1053 GAP_SIZE -= outgoing_nbytes;
1054 GPT += nchars - !!combined_before_bytes - !!combined_after_bytes;
1055 ZV += adjusted_nchars;
1056 Z += adjusted_nchars;
1057 GPT_BYTE += outgoing_nbytes;
1058 ZV_BYTE += outgoing_nbytes;
1059 Z_BYTE += outgoing_nbytes;
1060 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1061 adjust_overlays_for_insert (PT, adjusted_nchars);
1062 adjust_markers_for_insert (PT, PT_BYTE, PT + adjusted_nchars,
1063 PT_BYTE + outgoing_nbytes,
1064 combined_before_bytes, combined_after_bytes,
1065 before_markers);
1066
1067 if (combined_after_bytes)
1068 move_gap_both (GPT + 1, GPT_BYTE + combined_after_bytes);
1069
1070 if (GPT_BYTE < GPT)
1071 abort ();
1072
1073 graft_intervals_into_buffer (XSTRING (string)->intervals, PT, nchars,
1074 current_buffer, inherit);
1075 adjust_point (adjusted_nchars + combined_after_bytes,
1076 outgoing_nbytes + combined_after_bytes);
1077 }
1078 \f
1079 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1080 current buffer. If the text in BUF has properties, they are absorbed
1081 into the current buffer.
1082
1083 It does not work to use `insert' for this, because a malloc could happen
1084 and relocate BUF's text before the bcopy happens. */
1085
1086 void
1087 insert_from_buffer (buf, charpos, nchars, inherit)
1088 struct buffer *buf;
1089 int charpos, nchars;
1090 int inherit;
1091 {
1092 if (nchars > 0)
1093 {
1094 int opoint = PT;
1095
1096 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1097 signal_after_change (opoint, 0, PT - opoint);
1098 }
1099 }
1100
1101 static void
1102 insert_from_buffer_1 (buf, from, nchars, inherit)
1103 struct buffer *buf;
1104 int from, nchars;
1105 int inherit;
1106 {
1107 register Lisp_Object temp;
1108 int chunk;
1109 int from_byte = buf_charpos_to_bytepos (buf, from);
1110 int to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1111 int incoming_nbytes = to_byte - from_byte;
1112 int outgoing_nbytes = incoming_nbytes;
1113 int combined_before_bytes, combined_after_bytes;
1114 int adjusted_nchars;
1115
1116 /* Make OUTGOING_NBYTES describe the text
1117 as it will be inserted in this buffer. */
1118
1119 if (NILP (current_buffer->enable_multibyte_characters))
1120 outgoing_nbytes = nchars;
1121 else if (NILP (buf->enable_multibyte_characters))
1122 outgoing_nbytes
1123 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1124 incoming_nbytes);
1125
1126 /* Make sure point-max won't overflow after this insertion. */
1127 XSETINT (temp, outgoing_nbytes + Z);
1128 if (outgoing_nbytes + Z != XINT (temp))
1129 error ("Maximum buffer size exceeded");
1130
1131 prepare_to_modify_buffer (PT, PT, NULL);
1132
1133 if (PT != GPT)
1134 move_gap_both (PT, PT_BYTE);
1135 if (GAP_SIZE < outgoing_nbytes)
1136 make_gap (outgoing_nbytes - GAP_SIZE);
1137
1138 if (from < BUF_GPT (buf))
1139 {
1140 chunk = BUF_GPT_BYTE (buf) - from_byte;
1141 if (chunk > incoming_nbytes)
1142 chunk = incoming_nbytes;
1143 copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1144 GPT_ADDR, chunk,
1145 ! NILP (buf->enable_multibyte_characters),
1146 ! NILP (current_buffer->enable_multibyte_characters));
1147 }
1148 else
1149 chunk = 0;
1150 if (chunk < incoming_nbytes)
1151 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1152 GPT_ADDR + chunk, incoming_nbytes - chunk,
1153 ! NILP (buf->enable_multibyte_characters),
1154 ! NILP (current_buffer->enable_multibyte_characters));
1155
1156 /* We have copied text into the gap, but we have not altered
1157 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1158 to these functions and get the same results as we would
1159 have got earlier on. Meanwhile, PT_ADDR does point to
1160 the text that has been stored by copy_text. */
1161 combined_before_bytes
1162 = count_combining_before (PT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1163 combined_after_bytes
1164 = count_combining_after (PT_ADDR, outgoing_nbytes,
1165 PT, PT_BYTE);
1166
1167 /* This is the net amount that Z will increase from this insertion. */
1168 adjusted_nchars = nchars - !!combined_before_bytes - combined_after_bytes;
1169
1170 /* Record deletion of the surrounding text that combines with
1171 the insertion. This, together with recording the insertion,
1172 will add up to the right stuff in the undo list.
1173
1174 But there is no need to actually delete the combining bytes
1175 from the buffer and reinsert them. */
1176
1177 if (combined_after_bytes)
1178 record_delete (PT, combined_after_bytes);
1179
1180 if (combined_before_bytes)
1181 record_delete (PT - 1, 1);
1182
1183 record_insert (PT - !!combined_before_bytes, nchars);
1184 MODIFF++;
1185
1186 #ifdef USE_TEXT_PROPERTIES
1187 if (BUF_INTERVALS (current_buffer) != 0)
1188 offset_intervals (current_buffer, PT, adjusted_nchars);
1189 #endif
1190
1191 GAP_SIZE -= outgoing_nbytes;
1192 GPT += nchars - !!combined_before_bytes - !!combined_after_bytes;
1193 ZV += adjusted_nchars;
1194 Z += adjusted_nchars;
1195 GPT_BYTE += outgoing_nbytes;
1196 ZV_BYTE += outgoing_nbytes;
1197 Z_BYTE += outgoing_nbytes;
1198 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1199 adjust_overlays_for_insert (PT, adjusted_nchars);
1200 adjust_markers_for_insert (PT, PT_BYTE, PT + adjusted_nchars,
1201 PT_BYTE + outgoing_nbytes,
1202 combined_before_bytes, combined_after_bytes, 0);
1203 adjust_point (adjusted_nchars + combined_after_bytes,
1204 outgoing_nbytes + combined_after_bytes);
1205
1206 if (combined_after_bytes)
1207 move_gap_both (GPT + 1, GPT_BYTE + combined_after_bytes);
1208
1209 if (GPT_BYTE < GPT)
1210 abort ();
1211
1212 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1213 graft_intervals_into_buffer (copy_intervals (BUF_INTERVALS (buf),
1214 from, nchars),
1215 PT - adjusted_nchars, adjusted_nchars,
1216 current_buffer, inherit);
1217 }
1218 \f
1219 /* This function should be called after moving gap to FROM and before
1220 altering text between FROM and TO. This adjusts various position
1221 keepers and markers as if the text is deleted. Don't forget to
1222 call adjust_after_replace after you actually alter the text. */
1223
1224 void
1225 adjust_before_replace (from, from_byte, to, to_byte)
1226 int from, from_byte, to, to_byte;
1227 {
1228 adjust_markers_for_delete (from, from_byte, to, to_byte);
1229 record_delete (from, to - from);
1230 }
1231
1232 /* This function should be called after altering the text between FROM
1233 and TO to a new text of LEN chars (LEN_BYTE bytes).
1234 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES are the number
1235 of bytes before (resp. after) the change which combine with
1236 the beginning or end of the replacement text to form one character. */
1237
1238 void
1239 adjust_after_replace (from, from_byte, to, to_byte, len, len_byte,
1240 combined_before_bytes, combined_after_bytes)
1241 int from, from_byte, to, to_byte, len, len_byte;
1242 int combined_before_bytes, combined_after_bytes;
1243 {
1244 int adjusted_nchars = len - !!combined_before_bytes - combined_after_bytes;
1245 record_insert (from - !!combined_before_bytes, len);
1246 if (from < PT)
1247 adjust_point (len - (to - from) + combined_after_bytes,
1248 len_byte - (to_byte - from_byte) + combined_after_bytes);
1249 #ifdef USE_TEXT_PROPERTIES
1250 offset_intervals (current_buffer, PT, adjusted_nchars - (to - from));
1251 #endif
1252 adjust_overlays_for_delete (from, to - from);
1253 adjust_overlays_for_insert (from, adjusted_nchars);
1254 adjust_markers_for_insert (from, from_byte,
1255 from + adjusted_nchars, from_byte + len_byte,
1256 combined_before_bytes, combined_after_bytes, 0);
1257 if (len == 0)
1258 evaporate_overlays (from);
1259 MODIFF++;
1260 }
1261
1262 /* Replace the text from character positions FROM to TO with NEW,
1263 If PREPARE is nonzero, call prepare_to_modify_buffer.
1264 If INHERIT, the newly inserted text should inherit text properties
1265 from the surrounding non-deleted text. */
1266
1267 /* Note that this does not yet handle markers quite right.
1268 Also it needs to record a single undo-entry that does a replacement
1269 rather than a separate delete and insert.
1270 That way, undo will also handle markers properly. */
1271
1272 void
1273 replace_range (from, to, new, prepare, inherit)
1274 Lisp_Object new;
1275 int from, to, prepare, inherit;
1276 {
1277 int inschars = XSTRING (new)->size;
1278 int insbytes = XSTRING (new)->size_byte;
1279 int from_byte, to_byte;
1280 int nbytes_del, nchars_del;
1281 register Lisp_Object temp;
1282 struct gcpro gcpro1;
1283 int combined_before_bytes, combined_after_bytes;
1284 int adjusted_inschars;
1285
1286 GCPRO1 (new);
1287
1288 if (prepare)
1289 {
1290 int range_length = to - from;
1291 prepare_to_modify_buffer (from, to, &from);
1292 to = from + range_length;
1293 }
1294
1295 UNGCPRO;
1296
1297 /* Make args be valid */
1298 if (from < BEGV)
1299 from = BEGV;
1300 if (to > ZV)
1301 to = ZV;
1302
1303 from_byte = CHAR_TO_BYTE (from);
1304 to_byte = CHAR_TO_BYTE (to);
1305
1306 nchars_del = to - from;
1307 nbytes_del = to_byte - from_byte;
1308
1309 if (nbytes_del <= 0 && insbytes == 0)
1310 return;
1311
1312 /* Make sure point-max won't overflow after this insertion. */
1313 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1314 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1315 error ("Maximum buffer size exceeded");
1316
1317 GCPRO1 (new);
1318
1319 /* Make sure the gap is somewhere in or next to what we are deleting. */
1320 if (from > GPT)
1321 gap_right (from, from_byte);
1322 if (to < GPT)
1323 gap_left (to, to_byte, 0);
1324
1325 /* Relocate all markers pointing into the new, larger gap
1326 to point at the end of the text before the gap.
1327 Do this before recording the deletion,
1328 so that undo handles this after reinserting the text. */
1329 adjust_markers_for_delete (from, from_byte, to, to_byte);
1330
1331 record_delete (from, nchars_del);
1332
1333 GAP_SIZE += nbytes_del;
1334 ZV -= nchars_del;
1335 Z -= nchars_del;
1336 ZV_BYTE -= nbytes_del;
1337 Z_BYTE -= nbytes_del;
1338 GPT = from;
1339 GPT_BYTE = from_byte;
1340 *(GPT_ADDR) = 0; /* Put an anchor. */
1341
1342 if (GPT_BYTE < GPT)
1343 abort ();
1344
1345 if (GPT - BEG < beg_unchanged)
1346 beg_unchanged = GPT - BEG;
1347 if (Z - GPT < end_unchanged)
1348 end_unchanged = Z - GPT;
1349
1350 if (GAP_SIZE < insbytes)
1351 make_gap (insbytes - GAP_SIZE);
1352
1353 /* We have copied text into the gap, but we have not altered
1354 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1355 to these functions and get the same results as we would
1356 have got earlier on. Meanwhile, PT_ADDR does point to
1357 the text that has been stored by copy_text. */
1358
1359 combined_before_bytes
1360 = count_combining_before (XSTRING (new)->data, insbytes, PT, PT_BYTE);
1361 combined_after_bytes
1362 = count_combining_after (XSTRING (new)->data, insbytes, PT, PT_BYTE);
1363
1364 /* This is the net amount that Z will increase from this insertion. */
1365 adjusted_inschars
1366 = inschars - !!combined_before_bytes - combined_after_bytes;
1367
1368 /* Record deletion of the surrounding text that combines with
1369 the insertion. This, together with recording the insertion,
1370 will add up to the right stuff in the undo list.
1371
1372 But there is no need to actually delete the combining bytes
1373 from the buffer and reinsert them. */
1374
1375 if (combined_after_bytes)
1376 record_delete (PT, combined_after_bytes);
1377
1378 if (combined_before_bytes)
1379 record_delete (PT - 1, 1);
1380
1381 record_insert (from, inschars);
1382
1383 bcopy (XSTRING (new)->data, GPT_ADDR, insbytes);
1384
1385 /* Relocate point as if it were a marker. */
1386 if (from < PT)
1387 adjust_point ((from + adjusted_inschars - (PT < to ? PT : to)
1388 + combined_after_bytes),
1389 (from_byte + insbytes
1390 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)
1391 + combined_after_bytes));
1392
1393 #ifdef USE_TEXT_PROPERTIES
1394 offset_intervals (current_buffer, PT, adjusted_inschars - nchars_del);
1395 #endif
1396
1397 GAP_SIZE -= insbytes;
1398 GPT += inschars - !!combined_before_bytes - !!combined_after_bytes;
1399 ZV += adjusted_inschars;
1400 Z += adjusted_inschars;
1401 GPT_BYTE += insbytes;
1402 ZV_BYTE += insbytes;
1403 ZV_BYTE += insbytes;
1404 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1405
1406 if (GPT_BYTE < GPT)
1407 abort ();
1408
1409 /* Adjust the overlay center as needed. This must be done after
1410 adjusting the markers that bound the overlays. */
1411 adjust_overlays_for_delete (from, nchars_del);
1412 adjust_overlays_for_insert (from, adjusted_inschars);
1413 adjust_markers_for_insert (from, from_byte, from + adjusted_inschars,
1414 from_byte + insbytes,
1415 combined_before_bytes, combined_after_bytes, 0);
1416
1417 #ifdef USE_TEXT_PROPERTIES
1418 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1419 graft_intervals_into_buffer (XSTRING (new)->intervals, from,
1420 inschars, current_buffer, inherit);
1421 #endif
1422
1423 if (insbytes == 0)
1424 evaporate_overlays (from);
1425
1426 if (combined_after_bytes)
1427 move_gap_both (GPT + 1, GPT_BYTE + combined_after_bytes);
1428
1429 MODIFF++;
1430 UNGCPRO;
1431
1432 signal_after_change (from, nchars_del, adjusted_inschars);
1433 }
1434 \f
1435 /* Delete characters in current buffer
1436 from FROM up to (but not including) TO.
1437 If TO comes before FROM, we delete nothing. */
1438
1439 void
1440 del_range (from, to)
1441 register int from, to;
1442 {
1443 del_range_1 (from, to, 1);
1444 }
1445
1446 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
1447
1448 void
1449 del_range_1 (from, to, prepare)
1450 int from, to, prepare;
1451 {
1452 int from_byte, to_byte;
1453
1454 /* Make args be valid */
1455 if (from < BEGV)
1456 from = BEGV;
1457 if (to > ZV)
1458 to = ZV;
1459
1460 if (to <= from)
1461 return;
1462
1463 if (prepare)
1464 {
1465 int range_length = to - from;
1466 prepare_to_modify_buffer (from, to, &from);
1467 to = from + range_length;
1468 }
1469
1470 from_byte = CHAR_TO_BYTE (from);
1471 to_byte = CHAR_TO_BYTE (to);
1472
1473 del_range_2 (from, from_byte, to, to_byte);
1474 }
1475
1476 /* Like del_range_1 but args are byte positions, not char positions. */
1477
1478 void
1479 del_range_byte (from_byte, to_byte, prepare)
1480 int from_byte, to_byte, prepare;
1481 {
1482 int from, to;
1483
1484 /* Make args be valid */
1485 if (from_byte < BEGV_BYTE)
1486 from_byte = BEGV_BYTE;
1487 if (to_byte > ZV_BYTE)
1488 to_byte = ZV_BYTE;
1489
1490 if (to_byte <= from_byte)
1491 return;
1492
1493 from = BYTE_TO_CHAR (from_byte);
1494 to = BYTE_TO_CHAR (to_byte);
1495
1496 if (prepare)
1497 {
1498 int old_from = from, old_to = Z - to;
1499 int range_length = to - from;
1500 prepare_to_modify_buffer (from, to, &from);
1501 to = from + range_length;
1502
1503 if (old_from != from)
1504 from_byte = CHAR_TO_BYTE (from);
1505 if (old_to == Z - to)
1506 to_byte = CHAR_TO_BYTE (to);
1507 }
1508
1509 del_range_2 (from, from_byte, to, to_byte);
1510 }
1511
1512 /* Like del_range_1, but positions are specified both as charpos
1513 and bytepos. */
1514
1515 void
1516 del_range_both (from, from_byte, to, to_byte, prepare)
1517 int from, from_byte, to, to_byte, prepare;
1518 {
1519 /* Make args be valid */
1520 if (from_byte < BEGV_BYTE)
1521 from_byte = BEGV_BYTE;
1522 if (to_byte > ZV_BYTE)
1523 to_byte = ZV_BYTE;
1524
1525 if (to_byte <= from_byte)
1526 return;
1527
1528 if (from < BEGV)
1529 from = BEGV;
1530 if (to > ZV)
1531 to = ZV;
1532
1533 if (prepare)
1534 {
1535 int old_from = from, old_to = Z - to;
1536 int range_length = to - from;
1537 prepare_to_modify_buffer (from, to, &from);
1538 to = from + range_length;
1539
1540 if (old_from != from)
1541 from_byte = CHAR_TO_BYTE (from);
1542 if (old_to == Z - to)
1543 to_byte = CHAR_TO_BYTE (to);
1544 }
1545
1546 del_range_2 (from, from_byte, to, to_byte);
1547 }
1548
1549 /* Delete a range of text, specified both as character positions
1550 and byte positions. FROM and TO are character positions,
1551 while FROM_BYTE and TO_BYTE are byte positions. */
1552
1553 void
1554 del_range_2 (from, from_byte, to, to_byte)
1555 int from, from_byte, to, to_byte;
1556 {
1557 register int nbytes_del, nchars_del;
1558
1559 nchars_del = to - from;
1560 nbytes_del = to_byte - from_byte;
1561
1562 /* Make sure the gap is somewhere in or next to what we are deleting. */
1563 if (from > GPT)
1564 gap_right (from, from_byte);
1565 if (to < GPT)
1566 gap_left (to, to_byte, 0);
1567
1568 /* Relocate all markers pointing into the new, larger gap
1569 to point at the end of the text before the gap.
1570 Do this before recording the deletion,
1571 so that undo handles this after reinserting the text. */
1572 adjust_markers_for_delete (from, from_byte, to, to_byte);
1573
1574 record_delete (from, nchars_del);
1575 MODIFF++;
1576
1577 /* Relocate point as if it were a marker. */
1578 if (from < PT)
1579 adjust_point (from - (PT < to ? PT : to),
1580 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1581
1582 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1583 offset_intervals (current_buffer, from, - nchars_del);
1584
1585 /* Adjust the overlay center as needed. This must be done after
1586 adjusting the markers that bound the overlays. */
1587 adjust_overlays_for_delete (from_byte, nchars_del);
1588
1589 GAP_SIZE += nbytes_del;
1590 ZV_BYTE -= nbytes_del;
1591 Z_BYTE -= nbytes_del;
1592 ZV -= nchars_del;
1593 Z -= nchars_del;
1594 GPT = from;
1595 GPT_BYTE = from_byte;
1596 *(GPT_ADDR) = 0; /* Put an anchor. */
1597
1598 if (GPT_BYTE < GPT)
1599 abort ();
1600
1601 if (GPT - BEG < beg_unchanged)
1602 beg_unchanged = GPT - BEG;
1603 if (Z - GPT < end_unchanged)
1604 end_unchanged = Z - GPT;
1605
1606 evaporate_overlays (from);
1607 signal_after_change (from, nchars_del, 0);
1608 }
1609 \f
1610 /* Call this if you're about to change the region of BUFFER from
1611 character positions START to END. This checks the read-only
1612 properties of the region, calls the necessary modification hooks,
1613 and warns the next redisplay that it should pay attention to that
1614 area. */
1615
1616 void
1617 modify_region (buffer, start, end)
1618 struct buffer *buffer;
1619 int start, end;
1620 {
1621 struct buffer *old_buffer = current_buffer;
1622
1623 if (buffer != old_buffer)
1624 set_buffer_internal (buffer);
1625
1626 prepare_to_modify_buffer (start, end, NULL);
1627
1628 if (start - 1 < beg_unchanged
1629 || (unchanged_modified == MODIFF
1630 && overlay_unchanged_modified == OVERLAY_MODIFF))
1631 beg_unchanged = start - 1;
1632 if (Z - end < end_unchanged
1633 || (unchanged_modified == MODIFF
1634 && overlay_unchanged_modified == OVERLAY_MODIFF))
1635 end_unchanged = Z - end;
1636
1637 if (MODIFF <= SAVE_MODIFF)
1638 record_first_change ();
1639 MODIFF++;
1640
1641 buffer->point_before_scroll = Qnil;
1642
1643 if (buffer != old_buffer)
1644 set_buffer_internal (old_buffer);
1645 }
1646 \f
1647 /* Check that it is okay to modify the buffer between START and END,
1648 which are char positions.
1649
1650 Run the before-change-function, if any. If intervals are in use,
1651 verify that the text to be modified is not read-only, and call
1652 any modification properties the text may have.
1653
1654 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1655 by holding its value temporarily in a marker. */
1656
1657 void
1658 prepare_to_modify_buffer (start, end, preserve_ptr)
1659 int start, end;
1660 int *preserve_ptr;
1661 {
1662 if (!NILP (current_buffer->read_only))
1663 Fbarf_if_buffer_read_only ();
1664
1665 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1666 if (BUF_INTERVALS (current_buffer) != 0)
1667 {
1668 if (preserve_ptr)
1669 {
1670 Lisp_Object preserve_marker;
1671 struct gcpro gcpro1;
1672 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1673 GCPRO1 (preserve_marker);
1674 verify_interval_modification (current_buffer, start, end);
1675 *preserve_ptr = marker_position (preserve_marker);
1676 unchain_marker (preserve_marker);
1677 UNGCPRO;
1678 }
1679 else
1680 verify_interval_modification (current_buffer, start, end);
1681 }
1682
1683 #ifdef CLASH_DETECTION
1684 if (!NILP (current_buffer->file_truename)
1685 /* Make binding buffer-file-name to nil effective. */
1686 && !NILP (current_buffer->filename)
1687 && SAVE_MODIFF >= MODIFF)
1688 lock_file (current_buffer->file_truename);
1689 #else
1690 /* At least warn if this file has changed on disk since it was visited. */
1691 if (!NILP (current_buffer->filename)
1692 && SAVE_MODIFF >= MODIFF
1693 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1694 && !NILP (Ffile_exists_p (current_buffer->filename)))
1695 call1 (intern ("ask-user-about-supersession-threat"),
1696 current_buffer->filename);
1697 #endif /* not CLASH_DETECTION */
1698
1699 signal_before_change (start, end, preserve_ptr);
1700
1701 if (current_buffer->newline_cache)
1702 invalidate_region_cache (current_buffer,
1703 current_buffer->newline_cache,
1704 start - BEG, Z - end);
1705 if (current_buffer->width_run_cache)
1706 invalidate_region_cache (current_buffer,
1707 current_buffer->width_run_cache,
1708 start - BEG, Z - end);
1709
1710 Vdeactivate_mark = Qt;
1711 }
1712 \f
1713 /* These macros work with an argument named `preserve_ptr'
1714 and a local variable named `preserve_marker'. */
1715
1716 #define PRESERVE_VALUE \
1717 if (preserve_ptr && NILP (preserve_marker)) \
1718 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1719
1720 #define RESTORE_VALUE \
1721 if (! NILP (preserve_marker)) \
1722 { \
1723 *preserve_ptr = marker_position (preserve_marker); \
1724 unchain_marker (preserve_marker); \
1725 }
1726
1727 #define PRESERVE_START_END \
1728 if (NILP (start_marker)) \
1729 start_marker = Fcopy_marker (start, Qnil); \
1730 if (NILP (end_marker)) \
1731 end_marker = Fcopy_marker (end, Qnil);
1732
1733 #define FETCH_START \
1734 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1735
1736 #define FETCH_END \
1737 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1738
1739 /* Signal a change to the buffer immediately before it happens.
1740 START_INT and END_INT are the bounds of the text to be changed.
1741
1742 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1743 by holding its value temporarily in a marker. */
1744
1745 void
1746 signal_before_change (start_int, end_int, preserve_ptr)
1747 int start_int, end_int;
1748 int *preserve_ptr;
1749 {
1750 Lisp_Object start, end;
1751 Lisp_Object start_marker, end_marker;
1752 Lisp_Object preserve_marker;
1753 struct gcpro gcpro1, gcpro2, gcpro3;
1754
1755 start = make_number (start_int);
1756 end = make_number (end_int);
1757 preserve_marker = Qnil;
1758 start_marker = Qnil;
1759 end_marker = Qnil;
1760 GCPRO3 (preserve_marker, start_marker, end_marker);
1761
1762 /* If buffer is unmodified, run a special hook for that case. */
1763 if (SAVE_MODIFF >= MODIFF
1764 && !NILP (Vfirst_change_hook)
1765 && !NILP (Vrun_hooks))
1766 {
1767 PRESERVE_VALUE;
1768 PRESERVE_START_END;
1769 call1 (Vrun_hooks, Qfirst_change_hook);
1770 }
1771
1772 /* Run the before-change-function if any.
1773 We don't bother "binding" this variable to nil
1774 because it is obsolete anyway and new code should not use it. */
1775 if (!NILP (Vbefore_change_function))
1776 {
1777 PRESERVE_VALUE;
1778 PRESERVE_START_END;
1779 call2 (Vbefore_change_function, FETCH_START, FETCH_END);
1780 }
1781
1782 /* Now run the before-change-functions if any. */
1783 if (!NILP (Vbefore_change_functions))
1784 {
1785 Lisp_Object args[3];
1786 Lisp_Object before_change_functions;
1787 Lisp_Object after_change_functions;
1788 struct gcpro gcpro1, gcpro2;
1789
1790 PRESERVE_VALUE;
1791 PRESERVE_START_END;
1792
1793 /* "Bind" before-change-functions and after-change-functions
1794 to nil--but in a way that errors don't know about.
1795 That way, if there's an error in them, they will stay nil. */
1796 before_change_functions = Vbefore_change_functions;
1797 after_change_functions = Vafter_change_functions;
1798 Vbefore_change_functions = Qnil;
1799 Vafter_change_functions = Qnil;
1800 GCPRO2 (before_change_functions, after_change_functions);
1801
1802 /* Actually run the hook functions. */
1803 args[0] = Qbefore_change_functions;
1804 args[1] = FETCH_START;
1805 args[2] = FETCH_END;
1806 run_hook_list_with_args (before_change_functions, 3, args);
1807
1808 /* "Unbind" the variables we "bound" to nil. */
1809 Vbefore_change_functions = before_change_functions;
1810 Vafter_change_functions = after_change_functions;
1811 UNGCPRO;
1812 }
1813
1814 if (!NILP (current_buffer->overlays_before)
1815 || !NILP (current_buffer->overlays_after))
1816 {
1817 PRESERVE_VALUE;
1818 report_overlay_modification (FETCH_START, FETCH_END, 0,
1819 FETCH_START, FETCH_END, Qnil);
1820 }
1821
1822 if (! NILP (start_marker))
1823 free_marker (start_marker);
1824 if (! NILP (end_marker))
1825 free_marker (end_marker);
1826 RESTORE_VALUE;
1827 UNGCPRO;
1828 }
1829
1830 /* Signal a change immediately after it happens.
1831 CHARPOS is the character position of the start of the changed text.
1832 LENDEL is the number of characters of the text before the change.
1833 (Not the whole buffer; just the part that was changed.)
1834 LENINS is the number of characters in that part of the text
1835 after the change. */
1836
1837 void
1838 signal_after_change (charpos, lendel, lenins)
1839 int charpos, lendel, lenins;
1840 {
1841 /* If we are deferring calls to the after-change functions
1842 and there are no before-change functions,
1843 just record the args that we were going to use. */
1844 if (! NILP (Vcombine_after_change_calls)
1845 && NILP (Vbefore_change_function) && NILP (Vbefore_change_functions)
1846 && NILP (current_buffer->overlays_before)
1847 && NILP (current_buffer->overlays_after))
1848 {
1849 Lisp_Object elt;
1850
1851 if (!NILP (combine_after_change_list)
1852 && current_buffer != XBUFFER (combine_after_change_buffer))
1853 Fcombine_after_change_execute ();
1854
1855 elt = Fcons (make_number (charpos - BEG),
1856 Fcons (make_number (Z - (charpos - lendel + lenins)),
1857 Fcons (make_number (lenins - lendel), Qnil)));
1858 combine_after_change_list
1859 = Fcons (elt, combine_after_change_list);
1860 combine_after_change_buffer = Fcurrent_buffer ();
1861
1862 return;
1863 }
1864
1865 if (!NILP (combine_after_change_list))
1866 Fcombine_after_change_execute ();
1867
1868 /* Run the after-change-function if any.
1869 We don't bother "binding" this variable to nil
1870 because it is obsolete anyway and new code should not use it. */
1871 if (!NILP (Vafter_change_function))
1872 call3 (Vafter_change_function,
1873 make_number (charpos), make_number (charpos + lenins),
1874 make_number (lendel));
1875
1876 if (!NILP (Vafter_change_functions))
1877 {
1878 Lisp_Object args[4];
1879 Lisp_Object before_change_functions;
1880 Lisp_Object after_change_functions;
1881 struct gcpro gcpro1, gcpro2;
1882
1883 /* "Bind" before-change-functions and after-change-functions
1884 to nil--but in a way that errors don't know about.
1885 That way, if there's an error in them, they will stay nil. */
1886 before_change_functions = Vbefore_change_functions;
1887 after_change_functions = Vafter_change_functions;
1888 Vbefore_change_functions = Qnil;
1889 Vafter_change_functions = Qnil;
1890 GCPRO2 (before_change_functions, after_change_functions);
1891
1892 /* Actually run the hook functions. */
1893 args[0] = Qafter_change_functions;
1894 XSETFASTINT (args[1], charpos);
1895 XSETFASTINT (args[2], charpos + lenins);
1896 XSETFASTINT (args[3], lendel);
1897 run_hook_list_with_args (after_change_functions,
1898 4, args);
1899
1900 /* "Unbind" the variables we "bound" to nil. */
1901 Vbefore_change_functions = before_change_functions;
1902 Vafter_change_functions = after_change_functions;
1903 UNGCPRO;
1904 }
1905
1906 if (!NILP (current_buffer->overlays_before)
1907 || !NILP (current_buffer->overlays_after))
1908 report_overlay_modification (make_number (charpos),
1909 make_number (charpos + lenins),
1910 1,
1911 make_number (charpos),
1912 make_number (charpos + lenins),
1913 make_number (lendel));
1914
1915 /* After an insertion, call the text properties
1916 insert-behind-hooks or insert-in-front-hooks. */
1917 if (lendel == 0)
1918 report_interval_modification (charpos, charpos + lenins);
1919 }
1920
1921 Lisp_Object
1922 Fcombine_after_change_execute_1 (val)
1923 Lisp_Object val;
1924 {
1925 Vcombine_after_change_calls = val;
1926 return val;
1927 }
1928
1929 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
1930 Scombine_after_change_execute, 0, 0, 0,
1931 "This function is for use internally in `combine-after-change-calls'.")
1932 ()
1933 {
1934 register Lisp_Object val;
1935 int count = specpdl_ptr - specpdl;
1936 int beg, end, change;
1937 int begpos, endpos;
1938 Lisp_Object tail;
1939
1940 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
1941
1942 Fset_buffer (combine_after_change_buffer);
1943
1944 /* # chars unchanged at beginning of buffer. */
1945 beg = Z - BEG;
1946 /* # chars unchanged at end of buffer. */
1947 end = beg;
1948 /* Total amount of insertion (negative for deletion). */
1949 change = 0;
1950
1951 /* Scan the various individual changes,
1952 accumulating the range info in BEG, END and CHANGE. */
1953 for (tail = combine_after_change_list; CONSP (tail);
1954 tail = XCONS (tail)->cdr)
1955 {
1956 Lisp_Object elt;
1957 int thisbeg, thisend, thischange;
1958
1959 /* Extract the info from the next element. */
1960 elt = XCONS (tail)->car;
1961 if (! CONSP (elt))
1962 continue;
1963 thisbeg = XINT (XCONS (elt)->car);
1964
1965 elt = XCONS (elt)->cdr;
1966 if (! CONSP (elt))
1967 continue;
1968 thisend = XINT (XCONS (elt)->car);
1969
1970 elt = XCONS (elt)->cdr;
1971 if (! CONSP (elt))
1972 continue;
1973 thischange = XINT (XCONS (elt)->car);
1974
1975 /* Merge this range into the accumulated range. */
1976 change += thischange;
1977 if (thisbeg < beg)
1978 beg = thisbeg;
1979 if (thisend < end)
1980 end = thisend;
1981 }
1982
1983 /* Get the current start and end positions of the range
1984 that was changed. */
1985 begpos = BEG + beg;
1986 endpos = Z - end;
1987
1988 /* We are about to handle these, so discard them. */
1989 combine_after_change_list = Qnil;
1990
1991 /* Now run the after-change functions for real.
1992 Turn off the flag that defers them. */
1993 record_unwind_protect (Fcombine_after_change_execute_1,
1994 Vcombine_after_change_calls);
1995 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
1996
1997 return unbind_to (count, val);
1998 }
1999 \f
2000 syms_of_insdel ()
2001 {
2002 staticpro (&combine_after_change_list);
2003 combine_after_change_list = Qnil;
2004
2005 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2006 "Used internally by the `combine-after-change-calls' macro.");
2007 Vcombine_after_change_calls = Qnil;
2008
2009 defsubr (&Scombine_after_change_execute);
2010 }