(Fmake_temp_name): forgot the \n\ in the docstring
[emacs.git] / src / insdel.c
blobb4f6ac5a5a2fc4e01a986a15568483c973b368e5
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 86, 93, 94, 95, 97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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. */
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"
34 #ifndef NULL
35 #define NULL 0
36 #endif
38 #define min(x, y) ((x) < (y) ? (x) : (y))
39 #define max(x, y) ((x) > (y) ? (x) : (y))
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));
51 Lisp_Object Fcombine_after_change_execute ();
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;
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.
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;
69 /* Buffer which combine_after_change_list is about. */
70 Lisp_Object combine_after_change_buffer;
72 /* Check all markers in the current buffer, looking for something invalid. */
74 static int check_markers_debug_flag;
76 #define CHECK_MARKERS() \
77 if (check_markers_debug_flag) \
78 check_markers (); \
79 else
81 void
82 check_markers ()
84 register Lisp_Object tail;
85 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
87 tail = BUF_MARKERS (current_buffer);
89 while (XSYMBOL (tail) != XSYMBOL (Qnil))
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 ();
100 tail = XMARKER (tail)->chain;
104 /* Move gap to position CHARPOS.
105 Note that this can quit! */
107 void
108 move_gap (charpos)
109 int charpos;
111 move_gap_both (charpos, charpos_to_bytepos (charpos));
114 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
115 Note that this can quit! */
117 void
118 move_gap_both (charpos, bytepos)
119 int charpos, bytepos;
121 if (bytepos < GPT_BYTE)
122 gap_left (charpos, bytepos, 0);
123 else if (bytepos > GPT_BYTE)
124 gap_right (charpos, bytepos);
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. */
132 static void
133 gap_left (charpos, bytepos, newgap)
134 register int charpos, bytepos;
135 int newgap;
137 register unsigned char *to, *from;
138 register int i;
139 int new_s1;
141 if (!newgap)
142 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
144 i = GPT_BYTE;
145 to = GAP_END_ADDR;
146 from = GPT_ADDR;
147 new_s1 = GPT_BYTE;
149 /* Now copy the characters. To move the gap down,
150 copy characters up. */
152 while (1)
154 /* I gets number of characters left to copy. */
155 i = new_s1 - bytepos;
156 if (i == 0)
157 break;
158 /* If a quit is requested, stop copying now.
159 Change BYTEPOS to be where we have actually moved the gap to. */
160 if (QUITP)
162 bytepos = new_s1;
163 charpos = BYTE_TO_CHAR (bytepos);
164 break;
166 /* Move at most 32000 chars before checking again for a quit. */
167 if (i > 32000)
168 i = 32000;
169 #ifdef GAP_USE_BCOPY
170 if (i >= 128
171 /* bcopy is safe if the two areas of memory do not overlap
172 or on systems where bcopy is always safe for moving upward. */
173 && (BCOPY_UPWARD_SAFE
174 || to - from >= 128))
176 /* If overlap is not safe, avoid it by not moving too many
177 characters at once. */
178 if (!BCOPY_UPWARD_SAFE && i > to - from)
179 i = to - from;
180 new_s1 -= i;
181 from -= i, to -= i;
182 bcopy (from, to, i);
184 else
185 #endif
187 new_s1 -= i;
188 while (--i >= 0)
189 *--to = *--from;
193 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
194 BYTEPOS is where the loop above stopped, which may be what was specified
195 or may be where a quit was detected. */
196 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
197 GPT_BYTE = bytepos;
198 GPT = charpos;
199 if (bytepos < charpos)
200 abort ();
201 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
202 QUIT;
205 /* Move the gap to a position greater than than the current GPT.
206 BYTEPOS describes the new position as a byte position,
207 and CHARPOS is the corresponding char position. */
209 static void
210 gap_right (charpos, bytepos)
211 register int charpos, bytepos;
213 register unsigned char *to, *from;
214 register int i;
215 int new_s1;
217 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
219 i = GPT_BYTE;
220 from = GAP_END_ADDR;
221 to = GPT_ADDR;
222 new_s1 = GPT_BYTE;
224 /* Now copy the characters. To move the gap up,
225 copy characters down. */
227 while (1)
229 /* I gets number of characters left to copy. */
230 i = bytepos - new_s1;
231 if (i == 0)
232 break;
233 /* If a quit is requested, stop copying now.
234 Change BYTEPOS to be where we have actually moved the gap to. */
235 if (QUITP)
237 bytepos = new_s1;
238 charpos = BYTE_TO_CHAR (bytepos);
239 break;
241 /* Move at most 32000 chars before checking again for a quit. */
242 if (i > 32000)
243 i = 32000;
244 #ifdef GAP_USE_BCOPY
245 if (i >= 128
246 /* bcopy is safe if the two areas of memory do not overlap
247 or on systems where bcopy is always safe for moving downward. */
248 && (BCOPY_DOWNWARD_SAFE
249 || from - to >= 128))
251 /* If overlap is not safe, avoid it by not moving too many
252 characters at once. */
253 if (!BCOPY_DOWNWARD_SAFE && i > from - to)
254 i = from - to;
255 new_s1 += i;
256 bcopy (from, to, i);
257 from += i, to += i;
259 else
260 #endif
262 new_s1 += i;
263 while (--i >= 0)
264 *to++ = *from++;
268 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
269 - GAP_SIZE);
270 GPT = charpos;
271 GPT_BYTE = bytepos;
272 if (bytepos < charpos)
273 abort ();
274 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
275 QUIT;
278 /* Add AMOUNT to the byte position of every marker in the current buffer
279 whose current byte position is between FROM (exclusive) and TO (inclusive).
281 Also, any markers past the outside of that interval, in the direction
282 of adjustment, are first moved back to the near end of the interval
283 and then adjusted by AMOUNT.
285 When the latter adjustment is done, if AMOUNT is negative,
286 we record the adjustment for undo. (This case happens only for
287 deletion.)
289 The markers' character positions are not altered,
290 because gap motion does not affect character positions. */
292 int adjust_markers_test;
294 static void
295 adjust_markers_gap_motion (from, to, amount)
296 register int from, to, amount;
298 /* Now that a marker has a bytepos, not counting the gap,
299 nothing needs to be done here. */
300 #if 0
301 Lisp_Object marker;
302 register struct Lisp_Marker *m;
303 register int mpos;
305 marker = BUF_MARKERS (current_buffer);
307 while (!NILP (marker))
309 m = XMARKER (marker);
310 mpos = m->bytepos;
311 if (amount > 0)
313 if (mpos > to && mpos < to + amount)
315 if (adjust_markers_test)
316 abort ();
317 mpos = to + amount;
320 else
322 /* Here's the case where a marker is inside text being deleted.
323 AMOUNT can be negative for gap motion, too,
324 but then this range contains no markers. */
325 if (mpos > from + amount && mpos <= from)
327 if (adjust_markers_test)
328 abort ();
329 mpos = from + amount;
332 if (mpos > from && mpos <= to)
333 mpos += amount;
334 m->bufpos = mpos;
335 marker = m->chain;
337 #endif
340 /* Adjust all markers for a deletion
341 whose range in bytes is FROM_BYTE to TO_BYTE.
342 The range in charpos is FROM to TO.
344 This function assumes that the gap is adjacent to
345 or inside of the range being deleted. */
347 static void
348 adjust_markers_for_delete (from, from_byte, to, to_byte)
349 register int from, from_byte, to, to_byte;
351 Lisp_Object marker;
352 register struct Lisp_Marker *m;
353 register int charpos;
355 marker = BUF_MARKERS (current_buffer);
357 while (!NILP (marker))
359 m = XMARKER (marker);
360 charpos = m->charpos;
362 if (charpos > Z)
363 abort ();
365 /* If the marker is after the deletion,
366 relocate by number of chars / bytes deleted. */
367 if (charpos > to)
369 m->charpos -= to - from;
370 m->bytepos -= to_byte - from_byte;
373 /* Here's the case where a marker is inside text being deleted. */
374 else if (charpos > from)
376 record_marker_adjustment (marker, from - charpos);
377 m->charpos = from;
378 m->bytepos = from_byte;
381 marker = m->chain;
386 /* Adjust all markers for calling record_delete for combining bytes.
387 whose range in bytes is FROM_BYTE to TO_BYTE.
388 The range in charpos is FROM to TO. */
390 static void
391 adjust_markers_for_record_delete (from, from_byte, to, to_byte)
392 register int from, from_byte, to, to_byte;
394 Lisp_Object marker;
395 register struct Lisp_Marker *m;
396 register int charpos;
398 marker = BUF_MARKERS (current_buffer);
400 while (!NILP (marker))
402 m = XMARKER (marker);
403 charpos = m->charpos;
405 /* If the marker is after the deletion,
406 relocate by number of chars / bytes deleted. */
407 if (charpos > to)
409 /* Here's the case where a marker is inside text being deleted. */
410 else if (charpos > from)
411 record_marker_adjustment (marker, from - charpos);
413 marker = m->chain;
417 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
418 to TO / TO_BYTE. We have to relocate the charpos of every marker
419 that points after the insertion (but not their bytepos).
421 COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
422 that combine into one character with the text before the insertion.
423 COMBINED_AFTER_BYTES is the number of bytes after the insertion
424 that combine into one character with the last inserted bytes.
426 When a marker points at the insertion point,
427 we advance it if either its insertion-type is t
428 or BEFORE_MARKERS is true. */
430 static void
431 adjust_markers_for_insert (from, from_byte, to, to_byte,
432 combined_before_bytes, combined_after_bytes,
433 before_markers)
434 register int from, from_byte, to, to_byte;
435 int combined_before_bytes, combined_after_bytes, before_markers;
437 Lisp_Object marker;
438 int adjusted = 0;
439 int nchars = to - from;
440 int nbytes = to_byte - from_byte;
442 marker = BUF_MARKERS (current_buffer);
444 while (!NILP (marker))
446 register struct Lisp_Marker *m = XMARKER (marker);
448 /* In a single-byte buffer, a marker's two positions must be equal.
449 (If this insertion is going to combine characters, Z will
450 become different from Z_BYTE, but they might be the same now.
451 If so, the two OLD positions of the marker should be equal.) */
452 if (Z == Z_BYTE)
454 if (m->charpos != m->bytepos)
455 abort ();
458 if (m->bytepos == from_byte)
460 if (m->insertion_type || before_markers)
462 m->bytepos = to_byte + combined_after_bytes;
463 m->charpos = to - combined_before_bytes;
464 /* Point the marker before the combined character,
465 so that undoing the insertion puts it back where it was. */
466 if (combined_after_bytes)
467 DEC_BOTH (m->charpos, m->bytepos);
468 if (m->insertion_type)
469 adjusted = 1;
471 else if (combined_before_bytes)
473 /* This marker doesn't "need relocation",
474 but don't leave it pointing in the middle of a character.
475 Point the marker after the combined character,
476 so that undoing the insertion puts it back where it was. */
477 m->bytepos += combined_before_bytes;
478 if (combined_before_bytes == nbytes)
479 /* All new bytes plus combined_after_bytes (if any)
480 are combined. */
481 m->bytepos += combined_after_bytes;
484 /* If a marker was pointing into the combining bytes
485 after the insertion, don't leave it there
486 in the middle of a character. */
487 else if (combined_after_bytes && m->bytepos >= from_byte
488 && m->bytepos < from_byte + combined_after_bytes)
490 /* Put it after the combining bytes. */
491 m->bytepos = to_byte + combined_after_bytes;
492 m->charpos = to - combined_before_bytes;
493 /* Now move it back before the combined character,
494 so that undoing the insertion will put it where it was. */
495 DEC_BOTH (m->charpos, m->bytepos);
497 else if (m->bytepos > from_byte)
499 m->bytepos += nbytes;
500 m->charpos += nchars - combined_after_bytes - combined_before_bytes;
503 marker = m->chain;
506 /* Adjusting only markers whose insertion-type is t may result in
507 disordered overlays in the slot `overlays_before'. */
508 if (adjusted)
509 fix_overlays_before (current_buffer, from, to);
512 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
514 This is used only when the value of point changes due to an insert
515 or delete; it does not represent a conceptual change in point as a
516 marker. In particular, point is not crossing any interval
517 boundaries, so there's no need to use the usual SET_PT macro. In
518 fact it would be incorrect to do so, because either the old or the
519 new value of point is out of sync with the current set of
520 intervals. */
522 static void
523 adjust_point (nchars, nbytes)
524 int nchars, nbytes;
526 BUF_PT (current_buffer) += nchars;
527 BUF_PT_BYTE (current_buffer) += nbytes;
529 /* In a single-byte buffer, the two positions must be equal. */
530 if (ZV == ZV_BYTE
531 && PT != PT_BYTE)
532 abort ();
535 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
536 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
537 (NEW_BYTES).
539 See the comment of adjust_markers_for_insert for the args
540 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES. */
542 static void
543 adjust_markers_for_replace (from, from_byte, old_chars, old_bytes,
544 new_chars, new_bytes,
545 combined_before_bytes, combined_after_bytes)
546 int from, from_byte, old_chars, old_bytes, new_chars, new_bytes;
547 int combined_before_bytes, combined_after_bytes;
549 Lisp_Object marker = BUF_MARKERS (current_buffer);
550 int prev_to_byte = from_byte + old_bytes;
551 int diff_chars
552 = (new_chars - combined_before_bytes) - (old_chars + combined_after_bytes);
553 int diff_bytes = new_bytes - old_bytes;
555 while (!NILP (marker))
557 register struct Lisp_Marker *m = XMARKER (marker);
559 if (m->bytepos >= prev_to_byte
560 && (old_bytes != 0
561 /* If this is an insertion (replacing 0 chars),
562 reject the case of a marker that is at the
563 insertion point and should stay before the insertion. */
564 || m->bytepos > from_byte || m->insertion_type))
566 if (m->bytepos < prev_to_byte + combined_after_bytes)
568 /* Put it after the combining bytes. */
569 m->bytepos = from_byte + new_bytes + combined_after_bytes;
570 m->charpos = from + new_chars - combined_before_bytes;
572 else
574 m->charpos += diff_chars;
575 m->bytepos += diff_bytes;
578 else if (m->bytepos >= from_byte)
580 m->charpos = from;
581 m->bytepos = from_byte + combined_before_bytes;
582 /* If all new bytes are combined in addition to that there
583 are after combining bytes, we must set byte position of
584 the marker after the after combining bytes. */
585 if (combined_before_bytes == new_bytes)
586 m->bytepos += combined_after_bytes;
589 marker = m->chain;
592 CHECK_MARKERS ();
596 /* Make the gap NBYTES_ADDED bytes longer. */
598 void
599 make_gap (nbytes_added)
600 int nbytes_added;
602 unsigned char *result;
603 Lisp_Object tem;
604 int real_gap_loc;
605 int real_gap_loc_byte;
606 int old_gap_size;
608 /* If we have to get more space, get enough to last a while. */
609 nbytes_added += 2000;
611 /* Don't allow a buffer size that won't fit in an int
612 even if it will fit in a Lisp integer.
613 That won't work because so many places use `int'. */
615 if (Z_BYTE - BEG_BYTE + GAP_SIZE + nbytes_added
616 >= ((unsigned) 1 << (min (BITS_PER_INT, VALBITS) - 1)))
617 error ("Buffer exceeds maximum size");
619 BLOCK_INPUT;
620 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
621 result = BUFFER_REALLOC (BEG_ADDR, (Z_BYTE - BEG_BYTE
622 + GAP_SIZE + nbytes_added + 1));
624 if (result == 0)
626 UNBLOCK_INPUT;
627 memory_full ();
630 /* We can't unblock until the new address is properly stored. */
631 BEG_ADDR = result;
632 UNBLOCK_INPUT;
634 /* Prevent quitting in move_gap. */
635 tem = Vinhibit_quit;
636 Vinhibit_quit = Qt;
638 real_gap_loc = GPT;
639 real_gap_loc_byte = GPT_BYTE;
640 old_gap_size = GAP_SIZE;
642 /* Call the newly allocated space a gap at the end of the whole space. */
643 GPT = Z + GAP_SIZE;
644 GPT_BYTE = Z_BYTE + GAP_SIZE;
645 GAP_SIZE = nbytes_added;
647 /* Move the new gap down to be consecutive with the end of the old one.
648 This adjusts the markers properly too. */
649 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
651 /* Now combine the two into one large gap. */
652 GAP_SIZE += old_gap_size;
653 GPT = real_gap_loc;
654 GPT_BYTE = real_gap_loc_byte;
656 /* Put an anchor. */
657 *(Z_ADDR) = 0;
659 Vinhibit_quit = tem;
662 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
663 FROM_MULTIBYTE says whether the incoming text is multibyte.
664 TO_MULTIBYTE says whether to store the text as multibyte.
665 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
667 Return the number of bytes stored at TO_ADDR. */
670 copy_text (from_addr, to_addr, nbytes,
671 from_multibyte, to_multibyte)
672 unsigned char *from_addr;
673 unsigned char *to_addr;
674 int nbytes;
675 int from_multibyte, to_multibyte;
677 if (from_multibyte == to_multibyte)
679 bcopy (from_addr, to_addr, nbytes);
680 return nbytes;
682 else if (from_multibyte)
684 int nchars = 0;
685 int bytes_left = nbytes;
686 Lisp_Object tbl = Qnil;
688 /* We set the variable tbl to the reverse table of
689 Vnonascii_translation_table in advance. */
690 if (CHAR_TABLE_P (Vnonascii_translation_table))
692 tbl = Fchar_table_extra_slot (Vnonascii_translation_table,
693 make_number (0));
694 if (!CHAR_TABLE_P (tbl))
695 tbl = Qnil;
698 /* Convert multibyte to single byte. */
699 while (bytes_left > 0)
701 int thislen, c, c_save;
702 c = c_save = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
703 if (!SINGLE_BYTE_CHAR_P (c))
704 c = multibyte_char_to_unibyte (c, tbl);
705 *to_addr++ = c;
706 from_addr += thislen;
707 bytes_left -= thislen;
708 nchars++;
710 return nchars;
712 else
714 unsigned char *initial_to_addr = to_addr;
716 /* Convert single-byte to multibyte. */
717 while (nbytes > 0)
719 int c = *from_addr++;
720 unsigned char workbuf[4], *str;
721 int len;
723 if (c < 0400
724 && (c >= 0240
725 || (c >= 0200 && !NILP (Vnonascii_translation_table))))
727 c = unibyte_char_to_multibyte (c);
728 len = CHAR_STRING (c, workbuf, str);
729 bcopy (str, to_addr, len);
730 to_addr += len;
731 nbytes--;
733 else
734 /* Special case for speed. */
735 *to_addr++ = c, nbytes--;
737 return to_addr - initial_to_addr;
741 /* Return the number of bytes it would take
742 to convert some single-byte text to multibyte.
743 The single-byte text consists of NBYTES bytes at PTR. */
746 count_size_as_multibyte (ptr, nbytes)
747 unsigned char *ptr;
748 int nbytes;
750 int i;
751 int outgoing_nbytes = 0;
753 for (i = 0; i < nbytes; i++)
755 unsigned int c = *ptr++;
757 if (c < 0200 || (c < 0240 && NILP (Vnonascii_translation_table)))
758 outgoing_nbytes++;
759 else
761 c = unibyte_char_to_multibyte (c);
762 outgoing_nbytes += CHAR_BYTES (c);
766 return outgoing_nbytes;
769 /* Insert a string of specified length before point.
770 This function judges multibyteness based on
771 enable_multibyte_characters in the current buffer;
772 it never converts between single-byte and multibyte.
774 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
775 prepare_to_modify_buffer could relocate the text. */
777 void
778 insert (string, nbytes)
779 register unsigned char *string;
780 register int nbytes;
782 if (nbytes > 0)
784 int opoint = PT;
785 insert_1 (string, nbytes, 0, 1, 0);
786 signal_after_change (opoint, 0, PT - opoint);
790 /* Likewise, but inherit text properties from neighboring characters. */
792 void
793 insert_and_inherit (string, nbytes)
794 register unsigned char *string;
795 register int nbytes;
797 if (nbytes > 0)
799 int opoint = PT;
800 insert_1 (string, nbytes, 1, 1, 0);
801 signal_after_change (opoint, 0, PT - opoint);
805 /* Insert the character C before point. Do not inherit text properties. */
807 void
808 insert_char (c)
809 int c;
811 unsigned char workbuf[4], *str;
812 int len;
814 if (! NILP (current_buffer->enable_multibyte_characters))
815 len = CHAR_STRING (c, workbuf, str);
816 else
818 len = 1;
819 workbuf[0] = c;
820 str = workbuf;
823 insert (str, len);
826 /* Insert the null-terminated string S before point. */
828 void
829 insert_string (s)
830 char *s;
832 insert (s, strlen (s));
835 /* Like `insert' except that all markers pointing at the place where
836 the insertion happens are adjusted to point after it.
837 Don't use this function to insert part of a Lisp string,
838 since gc could happen and relocate it. */
840 void
841 insert_before_markers (string, nbytes)
842 unsigned char *string;
843 register int nbytes;
845 if (nbytes > 0)
847 int opoint = PT;
849 insert_1 (string, nbytes, 0, 1, 1);
850 signal_after_change (opoint, 0, PT - opoint);
854 /* Likewise, but inherit text properties from neighboring characters. */
856 void
857 insert_before_markers_and_inherit (string, nbytes)
858 unsigned char *string;
859 register int nbytes;
861 if (nbytes > 0)
863 int opoint = PT;
865 insert_1 (string, nbytes, 1, 1, 1);
866 signal_after_change (opoint, 0, PT - opoint);
870 /* Subroutine used by the insert functions above. */
872 void
873 insert_1 (string, nbytes, inherit, prepare, before_markers)
874 register unsigned char *string;
875 register int nbytes;
876 int inherit, prepare, before_markers;
878 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
879 inherit, prepare, before_markers);
882 /* See if the byte sequence at STR1 of length LEN1 combine with the
883 byte sequence at STR2 of length LEN2 to form a single composite
884 character. If so, return the number of bytes at the start of STR2
885 which combine in this way. Otherwise, return 0. If STR3 is not
886 NULL, it is a byte sequence of length LEN3 to be appended to STR1
887 before checking the combining. */
889 count_combining_composition (str1, len1, str2, len2, str3, len3)
890 unsigned char *str1, *str2, *str3;
891 int len1, len2, len3;
893 int len = len1 + len2 + len3;
894 unsigned char *buf = (unsigned char *) alloca (len + 1);
895 int bytes;
897 bcopy (str1, buf, len1);
898 if (str3)
900 bcopy (str3, buf + len1, len3);
901 len1 += len3;
903 bcopy (str2, buf + len1 , len2);
904 buf[len] = 0;
905 PARSE_MULTIBYTE_SEQ (buf, len, bytes);
906 return (bytes <= len1 ? 0 : bytes - len1);
909 /* See if the bytes before POS/POS_BYTE combine with bytes
910 at the start of STRING to form a single character.
911 If so, return the number of bytes at the start of STRING
912 which combine in this way. Otherwise, return 0. */
915 count_combining_before (string, length, pos, pos_byte)
916 unsigned char *string;
917 int length;
918 int pos, pos_byte;
920 int len, combining_bytes;
921 unsigned char *p;
923 if (NILP (current_buffer->enable_multibyte_characters))
924 return 0;
926 /* At first, we can exclude the following cases:
927 (1) STRING[0] can't be a following byte of multibyte sequence.
928 (2) POS is the start of the current buffer.
929 (3) A character before POS is not a multibyte character. */
930 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
931 return 0;
932 if (pos_byte == BEG_BYTE) /* case (2) */
933 return 0;
934 len = 1;
935 p = BYTE_POS_ADDR (pos_byte - 1);
936 while (! CHAR_HEAD_P (*p)) p--, len++;
937 if (! BASE_LEADING_CODE_P (*p)) /* case (3) */
938 return 0;
940 /* A sequence of a composite character requires a special handling. */
941 if (*p == LEADING_CODE_COMPOSITION)
942 return count_combining_composition (p, len, string, length, NULL, 0);
944 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
945 if (combining_bytes <= 0)
946 /* The character preceding POS is, complete and no room for
947 combining bytes (combining_bytes == 0), or an independent 8-bit
948 character (combining_bytes < 0). */
949 return 0;
951 /* We have a combination situation. Count the bytes at STRING that
952 may combine. */
953 p = string + 1;
954 while (!CHAR_HEAD_P (*p) && p < string + length)
955 p++;
957 return (combining_bytes < p - string ? combining_bytes : p - string);
960 /* See if the bytes after POS/POS_BYTE combine with bytes
961 at the end of STRING to form a single character.
962 If so, return the number of bytes after POS/POS_BYTE
963 which combine in this way. Otherwise, return 0. */
966 count_combining_after (string, length, pos, pos_byte)
967 unsigned char *string;
968 int length;
969 int pos, pos_byte;
971 int opos_byte = pos_byte;
972 int i;
973 int bytes;
974 unsigned char *bufp;
976 if (NILP (current_buffer->enable_multibyte_characters))
977 return 0;
979 /* At first, we can exclude the following cases:
980 (1) The last byte of STRING is an ASCII.
981 (2) POS is the last of the current buffer.
982 (3) A character at POS can't be a following byte of multibyte
983 character. */
984 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
985 return 0;
986 if (pos_byte == Z_BYTE) /* case (2) */
987 return 0;
988 bufp = BYTE_POS_ADDR (pos_byte);
989 if (CHAR_HEAD_P (*bufp)) /* case (3) */
990 return 0;
992 i = length - 1;
993 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
995 i--;
997 if (i < 0)
999 /* All characters in STRING are not character head. We must
1000 check also preceding bytes at POS. We are sure that the gap
1001 is at POS. */
1002 unsigned char *p = BEG_ADDR;
1003 i = pos_byte - 2;
1004 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
1005 i--;
1006 if (i < 0 || !BASE_LEADING_CODE_P (p[i]))
1007 return 0;
1008 /* A sequence of a composite character requires a special handling. */
1009 if (p[i] == LEADING_CODE_COMPOSITION)
1010 return count_combining_composition (p + i, pos_byte - 1 - i,
1011 bufp, Z_BYTE - pos_byte,
1012 string, length);
1013 bytes = BYTES_BY_CHAR_HEAD (p[i]);
1014 return (bytes <= pos_byte - 1 - i + length
1016 : bytes - (pos_byte - 1 - i + length));
1018 if (!BASE_LEADING_CODE_P (string[i]))
1019 return 0;
1020 /* A sequence of a composite character requires a special handling. */
1021 if (string[i] == LEADING_CODE_COMPOSITION)
1022 return count_combining_composition (string + i, length - i,
1023 bufp, Z_BYTE - pos_byte, NULL, 0);
1025 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
1026 bufp++, pos_byte++;
1027 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
1029 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
1032 /* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
1033 following the position POS/POS_BYTE to the character preceding POS.
1034 If TARGET is after POS+NBYTES, we only have to adjust the character
1035 position TARGET, else, if TARGET is after POS, we have to adjust
1036 both the character position TARGET and the byte position
1037 TARGET_BYTE, else we don't have to do any adjustment. */
1039 #define ADJUST_CHAR_POS(target, target_byte) \
1040 do { \
1041 if (target > pos + nbytes) \
1042 target -= nbytes; \
1043 else if (target >= pos) \
1045 target = pos; \
1046 target_byte = pos_byte + nbytes; \
1048 } while (0)
1050 /* Combine NBYTES stray trailing-codes, which were formerly separate
1051 characters, with the preceding character. These bytes
1052 are located after position POS / POS_BYTE, and the preceding character
1053 is located just before that position.
1055 This function does not adjust markers for byte combining. That
1056 should be done in advance by the functions
1057 adjust_markers_for_insert or adjust_markers_for_replace. */
1059 static void
1060 combine_bytes (pos, pos_byte, nbytes)
1061 int pos, pos_byte, nbytes;
1063 adjust_overlays_for_delete (pos, nbytes);
1065 ADJUST_CHAR_POS (BUF_PT (current_buffer), BUF_PT_BYTE (current_buffer));
1066 ADJUST_CHAR_POS (GPT, GPT_BYTE);
1067 ADJUST_CHAR_POS (Z, Z_BYTE);
1068 ADJUST_CHAR_POS (ZV, ZV_BYTE);
1070 if (BUF_INTERVALS (current_buffer) != 0)
1071 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1072 offset_intervals (current_buffer, pos, - nbytes);
1075 void
1076 byte_combining_error ()
1078 error ("Byte combining across boundary of accessible buffer text inhibitted");
1081 /* If we are going to combine bytes at POS which is at a narrowed
1082 region boundary, signal an error. */
1083 #define CHECK_BYTE_COMBINING_FOR_INSERT(pos) \
1084 do { \
1085 if ((combined_before_bytes && pos == BEGV) \
1086 || (combined_after_bytes && pos == ZV)) \
1087 byte_combining_error (); \
1088 } while (0)
1091 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1092 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
1093 are the same as in insert_1. */
1095 void
1096 insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
1097 register unsigned char *string;
1098 register int nchars, nbytes;
1099 int inherit, prepare, before_markers;
1101 int combined_before_bytes, combined_after_bytes;
1103 if (NILP (current_buffer->enable_multibyte_characters))
1104 nchars = nbytes;
1106 if (prepare)
1107 /* Do this before moving and increasing the gap,
1108 because the before-change hooks might move the gap
1109 or make it smaller. */
1110 prepare_to_modify_buffer (PT, PT, NULL);
1112 if (PT != GPT)
1113 move_gap_both (PT, PT_BYTE);
1114 if (GAP_SIZE < nbytes)
1115 make_gap (nbytes - GAP_SIZE);
1117 combined_before_bytes
1118 = count_combining_before (string, nbytes, PT, PT_BYTE);
1119 combined_after_bytes
1120 = count_combining_after (string, nbytes, PT, PT_BYTE);
1121 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1123 /* Record deletion of the surrounding text that combines with
1124 the insertion. This, together with recording the insertion,
1125 will add up to the right stuff in the undo list.
1127 But there is no need to actually delete the combining bytes
1128 from the buffer and reinsert them. */
1130 if (combined_after_bytes)
1132 Lisp_Object deletion;
1133 deletion = Qnil;
1135 if (! EQ (current_buffer->undo_list, Qt))
1136 deletion = make_buffer_string_both (PT, PT_BYTE,
1137 PT + combined_after_bytes,
1138 PT_BYTE + combined_after_bytes, 1);
1140 adjust_markers_for_record_delete (PT, PT_BYTE,
1141 PT + combined_after_bytes,
1142 PT_BYTE + combined_after_bytes);
1143 if (! EQ (current_buffer->undo_list, Qt))
1144 record_delete (PT, deletion);
1147 if (combined_before_bytes)
1149 Lisp_Object deletion;
1150 deletion = Qnil;
1152 if (! EQ (current_buffer->undo_list, Qt))
1153 deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
1154 PT, PT_BYTE, 1);
1155 adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
1156 PT, PT_BYTE);
1157 if (! EQ (current_buffer->undo_list, Qt))
1158 record_delete (PT - 1, deletion);
1161 record_insert (PT - !!combined_before_bytes,
1162 nchars - combined_before_bytes + !!combined_before_bytes);
1163 MODIFF++;
1165 bcopy (string, GPT_ADDR, nbytes);
1167 GAP_SIZE -= nbytes;
1168 /* When we have combining at the end of the insertion,
1169 this is the character position before the combined character. */
1170 GPT += nchars;
1171 ZV += nchars;
1172 Z += nchars;
1173 GPT_BYTE += nbytes;
1174 ZV_BYTE += nbytes;
1175 Z_BYTE += nbytes;
1176 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1178 if (combined_after_bytes)
1179 move_gap_both (GPT + combined_after_bytes,
1180 GPT_BYTE + combined_after_bytes);
1182 if (GPT_BYTE < GPT)
1183 abort ();
1185 adjust_overlays_for_insert (PT, nchars);
1186 adjust_markers_for_insert (PT, PT_BYTE,
1187 PT + nchars, PT_BYTE + nbytes,
1188 combined_before_bytes, combined_after_bytes,
1189 before_markers);
1191 #ifdef USE_TEXT_PROPERTIES
1192 if (BUF_INTERVALS (current_buffer) != 0)
1193 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1194 offset_intervals (current_buffer, PT, nchars);
1196 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
1197 Fset_text_properties (make_number (PT), make_number (PT + nchars),
1198 Qnil, Qnil);
1199 #endif
1202 int pos = PT, pos_byte = PT_BYTE;
1204 adjust_point (nchars + combined_after_bytes,
1205 nbytes + combined_after_bytes);
1207 if (combined_after_bytes)
1208 combine_bytes (pos + nchars, pos_byte + nbytes, combined_after_bytes);
1210 if (combined_before_bytes)
1211 combine_bytes (pos, pos_byte, combined_before_bytes);
1214 CHECK_MARKERS ();
1217 /* Insert the part of the text of STRING, a Lisp object assumed to be
1218 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1219 starting at position POS / POS_BYTE. If the text of STRING has properties,
1220 copy them into the buffer.
1222 It does not work to use `insert' for this, because a GC could happen
1223 before we bcopy the stuff into the buffer, and relocate the string
1224 without insert noticing. */
1226 void
1227 insert_from_string (string, pos, pos_byte, length, length_byte, inherit)
1228 Lisp_Object string;
1229 register int pos, pos_byte, length, length_byte;
1230 int inherit;
1232 int opoint = PT;
1233 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1234 inherit, 0);
1235 signal_after_change (opoint, 0, PT - opoint);
1238 /* Like `insert_from_string' except that all markers pointing
1239 at the place where the insertion happens are adjusted to point after it. */
1241 void
1242 insert_from_string_before_markers (string, pos, pos_byte,
1243 length, length_byte, inherit)
1244 Lisp_Object string;
1245 register int pos, pos_byte, length, length_byte;
1246 int inherit;
1248 int opoint = PT;
1249 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1250 inherit, 1);
1251 signal_after_change (opoint, 0, PT - opoint);
1254 /* Subroutine of the insertion functions above. */
1256 static void
1257 insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
1258 inherit, before_markers)
1259 Lisp_Object string;
1260 register int pos, pos_byte, nchars, nbytes;
1261 int inherit, before_markers;
1263 struct gcpro gcpro1;
1264 int outgoing_nbytes = nbytes;
1265 int combined_before_bytes, combined_after_bytes;
1266 INTERVAL intervals;
1268 /* Make OUTGOING_NBYTES describe the text
1269 as it will be inserted in this buffer. */
1271 if (NILP (current_buffer->enable_multibyte_characters))
1272 outgoing_nbytes = nchars;
1273 else if (! STRING_MULTIBYTE (string))
1274 outgoing_nbytes
1275 = count_size_as_multibyte (&XSTRING (string)->data[pos_byte],
1276 nbytes);
1278 GCPRO1 (string);
1279 /* Do this before moving and increasing the gap,
1280 because the before-change hooks might move the gap
1281 or make it smaller. */
1282 prepare_to_modify_buffer (PT, PT, NULL);
1284 if (PT != GPT)
1285 move_gap_both (PT, PT_BYTE);
1286 if (GAP_SIZE < outgoing_nbytes)
1287 make_gap (outgoing_nbytes - GAP_SIZE);
1288 UNGCPRO;
1290 /* Copy the string text into the buffer, perhaps converting
1291 between single-byte and multibyte. */
1292 copy_text (XSTRING (string)->data + pos_byte, GPT_ADDR, nbytes,
1293 STRING_MULTIBYTE (string),
1294 ! NILP (current_buffer->enable_multibyte_characters));
1296 /* We have copied text into the gap, but we have not altered
1297 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1298 to these functions and get the same results as we would
1299 have got earlier on. Meanwhile, PT_ADDR does point to
1300 the text that has been stored by copy_text. */
1302 combined_before_bytes
1303 = count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1304 combined_after_bytes
1305 = count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1307 unsigned char save = *(GPT_ADDR);
1308 *(GPT_ADDR) = 0;
1309 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1310 *(GPT_ADDR) = save;
1313 /* Record deletion of the surrounding text that combines with
1314 the insertion. This, together with recording the insertion,
1315 will add up to the right stuff in the undo list.
1317 But there is no need to actually delete the combining bytes
1318 from the buffer and reinsert them. */
1320 if (combined_after_bytes)
1322 Lisp_Object deletion;
1323 deletion = Qnil;
1325 if (! EQ (current_buffer->undo_list, Qt))
1326 deletion = make_buffer_string_both (PT, PT_BYTE,
1327 PT + combined_after_bytes,
1328 PT_BYTE + combined_after_bytes, 1);
1330 adjust_markers_for_record_delete (PT, PT_BYTE,
1331 PT + combined_after_bytes,
1332 PT_BYTE + combined_after_bytes);
1333 if (! EQ (current_buffer->undo_list, Qt))
1334 record_delete (PT, deletion);
1337 if (combined_before_bytes)
1339 Lisp_Object deletion;
1340 deletion = Qnil;
1342 if (! EQ (current_buffer->undo_list, Qt))
1343 deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
1344 PT, PT_BYTE, 1);
1345 adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
1346 PT, PT_BYTE);
1347 if (! EQ (current_buffer->undo_list, Qt))
1348 record_delete (PT - 1, deletion);
1351 record_insert (PT - !!combined_before_bytes,
1352 nchars - combined_before_bytes + !!combined_before_bytes);
1353 MODIFF++;
1355 GAP_SIZE -= outgoing_nbytes;
1356 GPT += nchars;
1357 ZV += nchars;
1358 Z += nchars;
1359 GPT_BYTE += outgoing_nbytes;
1360 ZV_BYTE += outgoing_nbytes;
1361 Z_BYTE += outgoing_nbytes;
1362 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1364 if (combined_after_bytes)
1365 move_gap_both (GPT + combined_after_bytes,
1366 GPT_BYTE + combined_after_bytes);
1368 if (GPT_BYTE < GPT)
1369 abort ();
1371 adjust_overlays_for_insert (PT, nchars);
1372 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1373 PT_BYTE + outgoing_nbytes,
1374 combined_before_bytes, combined_after_bytes,
1375 before_markers);
1377 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1378 offset_intervals (current_buffer, PT, nchars);
1380 intervals = XSTRING (string)->intervals;
1381 /* Get the intervals for the part of the string we are inserting--
1382 not including the combined-before bytes. */
1383 if (nbytes < STRING_BYTES (XSTRING (string)))
1384 intervals = copy_intervals (intervals, pos, nchars);
1386 /* Insert those intervals. */
1387 graft_intervals_into_buffer (intervals, PT, nchars,
1388 current_buffer, inherit);
1391 int pos = PT, pos_byte = PT_BYTE;
1393 adjust_point (nchars + combined_after_bytes,
1394 outgoing_nbytes + combined_after_bytes);
1396 if (combined_after_bytes)
1397 combine_bytes (pos + nchars, pos_byte + outgoing_nbytes,
1398 combined_after_bytes);
1400 if (combined_before_bytes)
1401 combine_bytes (pos, pos_byte, combined_before_bytes);
1405 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1406 current buffer. If the text in BUF has properties, they are absorbed
1407 into the current buffer.
1409 It does not work to use `insert' for this, because a malloc could happen
1410 and relocate BUF's text before the bcopy happens. */
1412 void
1413 insert_from_buffer (buf, charpos, nchars, inherit)
1414 struct buffer *buf;
1415 int charpos, nchars;
1416 int inherit;
1418 int opoint = PT;
1420 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1421 signal_after_change (opoint, 0, PT - opoint);
1424 static void
1425 insert_from_buffer_1 (buf, from, nchars, inherit)
1426 struct buffer *buf;
1427 int from, nchars;
1428 int inherit;
1430 register Lisp_Object temp;
1431 int chunk, chunk_expanded;
1432 int from_byte = buf_charpos_to_bytepos (buf, from);
1433 int to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1434 int incoming_nbytes = to_byte - from_byte;
1435 int outgoing_nbytes = incoming_nbytes;
1436 int combined_before_bytes, combined_after_bytes;
1437 INTERVAL intervals;
1439 /* Make OUTGOING_NBYTES describe the text
1440 as it will be inserted in this buffer. */
1442 if (NILP (current_buffer->enable_multibyte_characters))
1443 outgoing_nbytes = nchars;
1444 else if (NILP (buf->enable_multibyte_characters))
1446 int outgoing_before_gap = 0;
1447 int outgoing_after_gap = 0;
1449 if (from < BUF_GPT (buf))
1451 chunk = BUF_GPT_BYTE (buf) - from_byte;
1452 if (chunk > incoming_nbytes)
1453 chunk = incoming_nbytes;
1454 outgoing_before_gap
1455 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1456 chunk);
1458 else
1459 chunk = 0;
1461 if (chunk < incoming_nbytes)
1462 outgoing_after_gap
1463 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1464 from_byte + chunk),
1465 incoming_nbytes - chunk);
1467 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1470 /* Make sure point-max won't overflow after this insertion. */
1471 XSETINT (temp, outgoing_nbytes + Z);
1472 if (outgoing_nbytes + Z != XINT (temp))
1473 error ("Maximum buffer size exceeded");
1475 /* Do this before moving and increasing the gap,
1476 because the before-change hooks might move the gap
1477 or make it smaller. */
1478 prepare_to_modify_buffer (PT, PT, NULL);
1480 if (PT != GPT)
1481 move_gap_both (PT, PT_BYTE);
1482 if (GAP_SIZE < outgoing_nbytes)
1483 make_gap (outgoing_nbytes - GAP_SIZE);
1485 if (from < BUF_GPT (buf))
1487 chunk = BUF_GPT_BYTE (buf) - from_byte;
1488 if (chunk > incoming_nbytes)
1489 chunk = incoming_nbytes;
1490 /* Record number of output bytes, so we know where
1491 to put the output from the second copy_text. */
1492 chunk_expanded
1493 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1494 GPT_ADDR, chunk,
1495 ! NILP (buf->enable_multibyte_characters),
1496 ! NILP (current_buffer->enable_multibyte_characters));
1498 else
1499 chunk_expanded = chunk = 0;
1501 if (chunk < incoming_nbytes)
1502 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1503 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1504 ! NILP (buf->enable_multibyte_characters),
1505 ! NILP (current_buffer->enable_multibyte_characters));
1507 /* We have copied text into the gap, but we have not altered
1508 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1509 to these functions and get the same results as we would
1510 have got earlier on. Meanwhile, GPT_ADDR does point to
1511 the text that has been stored by copy_text. */
1512 combined_before_bytes
1513 = count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1514 combined_after_bytes
1515 = count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1517 unsigned char save = *(GPT_ADDR);
1518 *(GPT_ADDR) = 0;
1519 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1520 *(GPT_ADDR) = save;
1523 /* Record deletion of the surrounding text that combines with
1524 the insertion. This, together with recording the insertion,
1525 will add up to the right stuff in the undo list.
1527 But there is no need to actually delete the combining bytes
1528 from the buffer and reinsert them. */
1530 if (combined_after_bytes)
1532 Lisp_Object deletion;
1533 deletion = Qnil;
1535 if (! EQ (current_buffer->undo_list, Qt))
1536 deletion = make_buffer_string_both (PT, PT_BYTE,
1537 PT + combined_after_bytes,
1538 PT_BYTE + combined_after_bytes, 1);
1540 adjust_markers_for_record_delete (PT, PT_BYTE,
1541 PT + combined_after_bytes,
1542 PT_BYTE + combined_after_bytes);
1543 if (! EQ (current_buffer->undo_list, Qt))
1544 record_delete (PT, deletion);
1547 if (combined_before_bytes)
1549 Lisp_Object deletion;
1550 deletion = Qnil;
1552 if (! EQ (current_buffer->undo_list, Qt))
1553 deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
1554 PT, PT_BYTE, 1);
1555 adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
1556 PT, PT_BYTE);
1557 if (! EQ (current_buffer->undo_list, Qt))
1558 record_delete (PT - 1, deletion);
1561 record_insert (PT - !!combined_before_bytes,
1562 nchars - combined_before_bytes + !!combined_before_bytes);
1563 MODIFF++;
1565 GAP_SIZE -= outgoing_nbytes;
1566 GPT += nchars;
1567 ZV += nchars;
1568 Z += nchars;
1569 GPT_BYTE += outgoing_nbytes;
1570 ZV_BYTE += outgoing_nbytes;
1571 Z_BYTE += outgoing_nbytes;
1572 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1574 if (combined_after_bytes)
1575 move_gap_both (GPT + combined_after_bytes,
1576 GPT_BYTE + combined_after_bytes);
1578 if (GPT_BYTE < GPT)
1579 abort ();
1581 adjust_overlays_for_insert (PT, nchars);
1582 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1583 PT_BYTE + outgoing_nbytes,
1584 combined_before_bytes, combined_after_bytes, 0);
1586 #ifdef USE_TEXT_PROPERTIES
1587 if (BUF_INTERVALS (current_buffer) != 0)
1588 offset_intervals (current_buffer, PT, nchars);
1589 #endif
1591 /* Get the intervals for the part of the string we are inserting--
1592 not including the combined-before bytes. */
1593 intervals = BUF_INTERVALS (buf);
1594 if (outgoing_nbytes < BUF_Z_BYTE (buf) - BUF_BEG_BYTE (buf))
1595 intervals = copy_intervals (intervals, from, nchars);
1597 /* Insert those intervals. */
1598 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1601 int pos = PT, pos_byte = PT_BYTE;
1603 adjust_point (nchars + combined_after_bytes,
1604 outgoing_nbytes + combined_after_bytes);
1606 if (combined_after_bytes)
1607 combine_bytes (pos + nchars, pos_byte + outgoing_nbytes,
1608 combined_after_bytes);
1610 if (combined_before_bytes)
1611 combine_bytes (pos, pos_byte, combined_before_bytes);
1615 /* This function should be called after moving gap to FROM and before
1616 altering text between FROM and TO. This adjusts various position
1617 keepers and markers as if the text is deleted. Don't forget to
1618 call adjust_after_replace after you actually alter the text. */
1620 void
1621 adjust_before_replace (from, from_byte, to, to_byte)
1622 int from, from_byte, to, to_byte;
1624 Lisp_Object deletion;
1626 if (! EQ (current_buffer->undo_list, Qt))
1627 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1629 CHECK_MARKERS ();
1631 adjust_markers_for_delete (from, from_byte, to, to_byte);
1633 if (! EQ (current_buffer->undo_list, Qt))
1634 record_delete (from, deletion);
1636 adjust_overlays_for_delete (from, to - from);
1639 /* Record undo information and adjust markers and position keepers for
1640 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1641 chars (LEN_BYTE bytes) which resides in the gap just after
1642 GPT_ADDR.
1644 PREV_TEXT nil means the new text was just inserted. */
1646 void
1647 adjust_after_replace (from, from_byte, prev_text, len, len_byte)
1648 int from, from_byte, len, len_byte;
1649 Lisp_Object prev_text;
1651 int combined_before_bytes
1652 = count_combining_before (GPT_ADDR, len_byte, from, from_byte);
1653 int combined_after_bytes
1654 = count_combining_after (GPT_ADDR, len_byte, from, from_byte);
1655 /* This flag tells if we combine some bytes with a character before
1656 FROM. This happens even if combined_before_bytes is zero. */
1657 int combine_before = (combined_before_bytes
1658 || (len == 0 && combined_after_bytes));
1660 int nchars_del = 0, nbytes_del = 0;
1662 if (STRINGP (prev_text))
1664 nchars_del = XSTRING (prev_text)->size;
1665 nbytes_del = STRING_BYTES (XSTRING (prev_text));
1668 if ((combine_before && from == BEGV)
1669 || (combined_after_bytes && from == ZV))
1671 /* We can't combine bytes nor signal an error here. So, let's
1672 pretend that the new text is just a single space. */
1673 len = len_byte = 1;
1674 combined_before_bytes = combined_after_bytes = 0;
1675 *(GPT_ADDR) = ' ';
1678 if (combined_after_bytes)
1680 Lisp_Object deletion;
1681 deletion = Qnil;
1683 if (! EQ (current_buffer->undo_list, Qt))
1684 deletion = make_buffer_string_both (from, from_byte,
1685 from + combined_after_bytes,
1686 from_byte + combined_after_bytes,
1689 adjust_markers_for_record_delete (from, from_byte,
1690 from + combined_after_bytes,
1691 from_byte + combined_after_bytes);
1693 if (! EQ (current_buffer->undo_list, Qt))
1694 record_delete (from + nchars_del, deletion);
1697 if (combined_before_bytes
1698 || (len_byte == 0 && combined_after_bytes > 0))
1700 Lisp_Object deletion;
1701 deletion = Qnil;
1703 if (! EQ (current_buffer->undo_list, Qt))
1704 deletion = make_buffer_string_both (from - 1, CHAR_TO_BYTE (from - 1),
1705 from, from_byte, 1);
1706 adjust_markers_for_record_delete (from - 1, CHAR_TO_BYTE (from - 1),
1707 from, from_byte);
1708 if (! EQ (current_buffer->undo_list, Qt))
1709 record_delete (from - 1, deletion);
1712 /* Update various buffer positions for the new text. */
1713 GAP_SIZE -= len_byte;
1714 ZV += len; Z+= len;
1715 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1716 GPT += len; GPT_BYTE += len_byte;
1717 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1719 /* The gap should be at character boundary. */
1720 if (combined_after_bytes)
1721 move_gap_both (GPT + combined_after_bytes,
1722 GPT_BYTE + combined_after_bytes);
1724 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1725 len, len_byte,
1726 combined_before_bytes, combined_after_bytes);
1727 if (! EQ (current_buffer->undo_list, Qt))
1729 if (nchars_del > 0)
1730 record_delete (from - combine_before, prev_text);
1731 if (combine_before)
1732 record_insert (from - 1, len - combined_before_bytes + 1);
1733 else
1734 record_insert (from, len);
1737 if (len > nchars_del)
1738 adjust_overlays_for_insert (from, len - nchars_del);
1739 else if (len < nchars_del)
1740 adjust_overlays_for_delete (from, nchars_del - len);
1741 #ifdef USE_TEXT_PROPERTIES
1742 if (BUF_INTERVALS (current_buffer) != 0)
1744 offset_intervals (current_buffer, from, len - nchars_del);
1746 #endif
1749 if (from < PT)
1750 adjust_point (len - nchars_del, len_byte - nbytes_del);
1752 if (combined_after_bytes)
1754 if (combined_before_bytes == len_byte)
1755 /* This is the case that all new bytes are combined. */
1756 combined_before_bytes += combined_after_bytes;
1757 else
1758 combine_bytes (from + len, from_byte + len_byte,
1759 combined_after_bytes);
1761 if (combined_before_bytes)
1762 combine_bytes (from, from_byte, combined_before_bytes);
1765 /* As byte combining will decrease Z, we must check this again. */
1766 if (Z - GPT < END_UNCHANGED)
1767 END_UNCHANGED = Z - GPT;
1769 CHECK_MARKERS ();
1771 if (len == 0)
1772 evaporate_overlays (from);
1773 MODIFF++;
1776 /* Record undo information, adjust markers and position keepers for an
1777 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1778 text already exists in the current buffer but character length (TO
1779 - FROM) may be incorrect, the correct length is NEWLEN. */
1781 void
1782 adjust_after_insert (from, from_byte, to, to_byte, newlen)
1783 int from, from_byte, to, to_byte, newlen;
1785 int len = to - from, len_byte = to_byte - from_byte;
1787 if (GPT != to)
1788 move_gap_both (to, to_byte);
1789 GAP_SIZE += len_byte;
1790 GPT -= len; GPT_BYTE -= len_byte;
1791 ZV -= len; ZV_BYTE -= len_byte;
1792 Z -= len; Z_BYTE -= len_byte;
1793 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1796 /* Replace the text from character positions FROM to TO with NEW,
1797 If PREPARE is nonzero, call prepare_to_modify_buffer.
1798 If INHERIT, the newly inserted text should inherit text properties
1799 from the surrounding non-deleted text. */
1801 /* Note that this does not yet handle markers quite right.
1802 Also it needs to record a single undo-entry that does a replacement
1803 rather than a separate delete and insert.
1804 That way, undo will also handle markers properly.
1806 But if MARKERS is 0, don't relocate markers. */
1808 void
1809 replace_range (from, to, new, prepare, inherit, markers)
1810 Lisp_Object new;
1811 int from, to, prepare, inherit, markers;
1813 int inschars = XSTRING (new)->size;
1814 int insbytes = STRING_BYTES (XSTRING (new));
1815 int from_byte, to_byte;
1816 int nbytes_del, nchars_del;
1817 register Lisp_Object temp;
1818 struct gcpro gcpro1;
1819 int combined_before_bytes, combined_after_bytes;
1820 INTERVAL intervals;
1821 int outgoing_insbytes = insbytes;
1822 Lisp_Object deletion;
1824 CHECK_MARKERS ();
1826 GCPRO1 (new);
1828 if (prepare)
1830 int range_length = to - from;
1831 prepare_to_modify_buffer (from, to, &from);
1832 to = from + range_length;
1835 UNGCPRO;
1837 /* Make args be valid */
1838 if (from < BEGV)
1839 from = BEGV;
1840 if (to > ZV)
1841 to = ZV;
1843 from_byte = CHAR_TO_BYTE (from);
1844 to_byte = CHAR_TO_BYTE (to);
1846 nchars_del = to - from;
1847 nbytes_del = to_byte - from_byte;
1849 if (nbytes_del <= 0 && insbytes == 0)
1850 return;
1852 /* Make OUTGOING_INSBYTES describe the text
1853 as it will be inserted in this buffer. */
1855 if (NILP (current_buffer->enable_multibyte_characters))
1856 outgoing_insbytes = inschars;
1857 else if (! STRING_MULTIBYTE (new))
1858 outgoing_insbytes
1859 = count_size_as_multibyte (XSTRING (new)->data, insbytes);
1861 /* Make sure point-max won't overflow after this insertion. */
1862 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1863 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1864 error ("Maximum buffer size exceeded");
1866 GCPRO1 (new);
1868 /* Make sure the gap is somewhere in or next to what we are deleting. */
1869 if (from > GPT)
1870 gap_right (from, from_byte);
1871 if (to < GPT)
1872 gap_left (to, to_byte, 0);
1874 /* Even if we don't record for undo, we must keep the original text
1875 because we may have to recover it because of inappropriate byte
1876 combining. */
1877 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1879 if (markers)
1880 /* Relocate all markers pointing into the new, larger gap
1881 to point at the end of the text before the gap.
1882 Do this before recording the deletion,
1883 so that undo handles this after reinserting the text. */
1884 adjust_markers_for_delete (from, from_byte, to, to_byte);
1886 GAP_SIZE += nbytes_del;
1887 ZV -= nchars_del;
1888 Z -= nchars_del;
1889 ZV_BYTE -= nbytes_del;
1890 Z_BYTE -= nbytes_del;
1891 GPT = from;
1892 GPT_BYTE = from_byte;
1893 *(GPT_ADDR) = 0; /* Put an anchor. */
1895 if (GPT_BYTE < GPT)
1896 abort ();
1898 if (GPT - BEG < BEG_UNCHANGED)
1899 BEG_UNCHANGED = GPT - BEG;
1900 if (Z - GPT < END_UNCHANGED)
1901 END_UNCHANGED = Z - GPT;
1903 if (GAP_SIZE < insbytes)
1904 make_gap (insbytes - GAP_SIZE);
1906 /* Copy the string text into the buffer, perhaps converting
1907 between single-byte and multibyte. */
1908 copy_text (XSTRING (new)->data, GPT_ADDR, insbytes,
1909 STRING_MULTIBYTE (new),
1910 ! NILP (current_buffer->enable_multibyte_characters));
1912 /* We have copied text into the gap, but we have not marked
1913 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1914 here, for both the previous text and the following text.
1915 Meanwhile, GPT_ADDR does point to
1916 the text that has been stored by copy_text. */
1918 combined_before_bytes
1919 = count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte);
1920 combined_after_bytes
1921 = count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte);
1923 if ((combined_before_bytes && from == BEGV)
1924 || (combined_after_bytes && from == ZV))
1926 /* Bytes are being combined across the region boundary. We
1927 should avoid it. We recover the original contents before
1928 signaling an error. */
1929 bcopy (XSTRING (deletion)->data, GPT_ADDR, nbytes_del);
1930 GAP_SIZE -= nbytes_del;
1931 ZV += nchars_del;
1932 Z += nchars_del;
1933 ZV_BYTE += nbytes_del;
1934 Z_BYTE += nbytes_del;
1935 GPT = from + nchars_del;
1936 GPT_BYTE = from_byte + nbytes_del;
1937 *(GPT_ADDR) = 0; /* Put an anchor. */
1938 if (markers)
1939 adjust_markers_for_insert (from, from_byte, to, to_byte, 0, 0, 0);
1940 UNGCPRO;
1941 byte_combining_error ();
1942 GCPRO1 (new);
1945 /* Record deletion of the surrounding text that combines with
1946 the insertion. This, together with recording the insertion,
1947 will add up to the right stuff in the undo list.
1949 But there is no need to actually delete the combining bytes
1950 from the buffer and reinsert them. */
1952 if (combined_after_bytes)
1954 Lisp_Object deletion;
1955 deletion = Qnil;
1957 if (! EQ (current_buffer->undo_list, Qt))
1958 deletion = make_buffer_string_both (from, from_byte,
1959 from + combined_after_bytes,
1960 from_byte + combined_after_bytes,
1963 adjust_markers_for_record_delete (from, from_byte,
1964 from + combined_after_bytes,
1965 from_byte + combined_after_bytes);
1966 if (! EQ (current_buffer->undo_list, Qt))
1967 record_delete (from + nchars_del, deletion);
1970 if (combined_before_bytes)
1972 Lisp_Object deletion;
1973 deletion = Qnil;
1975 if (! EQ (current_buffer->undo_list, Qt))
1976 deletion = make_buffer_string_both (from - 1, CHAR_TO_BYTE (from - 1),
1977 from, from_byte, 1);
1978 adjust_markers_for_record_delete (from - 1, CHAR_TO_BYTE (from - 1),
1979 from, from_byte);
1980 if (! EQ (current_buffer->undo_list, Qt))
1981 record_delete (from - 1, deletion);
1984 if (! EQ (current_buffer->undo_list, Qt))
1986 record_delete (from - !!combined_before_bytes, deletion);
1987 record_insert (from - !!combined_before_bytes,
1988 (inschars - combined_before_bytes
1989 + !!combined_before_bytes));
1992 GAP_SIZE -= outgoing_insbytes;
1993 GPT += inschars;
1994 ZV += inschars;
1995 Z += inschars;
1996 GPT_BYTE += outgoing_insbytes;
1997 ZV_BYTE += outgoing_insbytes;
1998 Z_BYTE += outgoing_insbytes;
1999 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
2001 if (combined_after_bytes)
2002 move_gap_both (GPT + combined_after_bytes,
2003 GPT_BYTE + combined_after_bytes);
2005 if (GPT_BYTE < GPT)
2006 abort ();
2008 /* Adjust the overlay center as needed. This must be done after
2009 adjusting the markers that bound the overlays. */
2010 adjust_overlays_for_delete (from, nchars_del);
2011 adjust_overlays_for_insert (from, inschars);
2012 if (markers)
2013 adjust_markers_for_insert (from, from_byte,
2014 from + inschars, from_byte + outgoing_insbytes,
2015 combined_before_bytes, combined_after_bytes, 0);
2017 #ifdef USE_TEXT_PROPERTIES
2018 offset_intervals (current_buffer, from, inschars - nchars_del);
2020 /* Get the intervals for the part of the string we are inserting--
2021 not including the combined-before bytes. */
2022 intervals = XSTRING (new)->intervals;
2023 /* Insert those intervals. */
2024 graft_intervals_into_buffer (intervals, from, inschars,
2025 current_buffer, inherit);
2026 #endif
2028 /* Relocate point as if it were a marker. */
2029 if (from < PT)
2030 adjust_point ((from + inschars - (PT < to ? PT : to)),
2031 (from_byte + outgoing_insbytes
2032 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
2034 if (combined_after_bytes)
2036 if (combined_before_bytes == outgoing_insbytes)
2037 /* This is the case that all new bytes are combined. */
2038 combined_before_bytes += combined_after_bytes;
2039 else
2040 combine_bytes (from + inschars, from_byte + outgoing_insbytes,
2041 combined_after_bytes);
2043 if (combined_before_bytes)
2044 combine_bytes (from, from_byte, combined_before_bytes);
2046 /* As byte combining will decrease Z, we must check this again. */
2047 if (Z - GPT < END_UNCHANGED)
2048 END_UNCHANGED = Z - GPT;
2050 if (outgoing_insbytes == 0)
2051 evaporate_overlays (from);
2053 CHECK_MARKERS ();
2055 MODIFF++;
2056 UNGCPRO;
2058 signal_after_change (from, nchars_del, GPT - from);
2061 /* Delete characters in current buffer
2062 from FROM up to (but not including) TO.
2063 If TO comes before FROM, we delete nothing. */
2065 void
2066 del_range (from, to)
2067 register int from, to;
2069 del_range_1 (from, to, 1);
2072 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
2074 void
2075 del_range_1 (from, to, prepare)
2076 int from, to, prepare;
2078 int from_byte, to_byte;
2080 /* Make args be valid */
2081 if (from < BEGV)
2082 from = BEGV;
2083 if (to > ZV)
2084 to = ZV;
2086 if (to <= from)
2087 return;
2089 if (prepare)
2091 int range_length = to - from;
2092 prepare_to_modify_buffer (from, to, &from);
2093 to = from + range_length;
2096 from_byte = CHAR_TO_BYTE (from);
2097 to_byte = CHAR_TO_BYTE (to);
2099 del_range_2 (from, from_byte, to, to_byte);
2100 signal_after_change (from, to - from, 0);
2103 /* Like del_range_1 but args are byte positions, not char positions. */
2105 void
2106 del_range_byte (from_byte, to_byte, prepare)
2107 int from_byte, to_byte, prepare;
2109 int from, to;
2111 /* Make args be valid */
2112 if (from_byte < BEGV_BYTE)
2113 from_byte = BEGV_BYTE;
2114 if (to_byte > ZV_BYTE)
2115 to_byte = ZV_BYTE;
2117 if (to_byte <= from_byte)
2118 return;
2120 from = BYTE_TO_CHAR (from_byte);
2121 to = BYTE_TO_CHAR (to_byte);
2123 if (prepare)
2125 int old_from = from, old_to = Z - to;
2126 int range_length = to - from;
2127 prepare_to_modify_buffer (from, to, &from);
2128 to = from + range_length;
2130 if (old_from != from)
2131 from_byte = CHAR_TO_BYTE (from);
2132 if (old_to == Z - to)
2133 to_byte = CHAR_TO_BYTE (to);
2136 del_range_2 (from, from_byte, to, to_byte);
2137 signal_after_change (from, to - from, 0);
2140 /* Like del_range_1, but positions are specified both as charpos
2141 and bytepos. */
2143 void
2144 del_range_both (from, from_byte, to, to_byte, prepare)
2145 int from, from_byte, to, to_byte, prepare;
2147 /* Make args be valid */
2148 if (from_byte < BEGV_BYTE)
2149 from_byte = BEGV_BYTE;
2150 if (to_byte > ZV_BYTE)
2151 to_byte = ZV_BYTE;
2153 if (to_byte <= from_byte)
2154 return;
2156 if (from < BEGV)
2157 from = BEGV;
2158 if (to > ZV)
2159 to = ZV;
2161 if (prepare)
2163 int old_from = from, old_to = Z - to;
2164 int range_length = to - from;
2165 prepare_to_modify_buffer (from, to, &from);
2166 to = from + range_length;
2168 if (old_from != from)
2169 from_byte = CHAR_TO_BYTE (from);
2170 if (old_to == Z - to)
2171 to_byte = CHAR_TO_BYTE (to);
2174 del_range_2 (from, from_byte, to, to_byte);
2175 signal_after_change (from, to - from, 0);
2178 /* Delete a range of text, specified both as character positions
2179 and byte positions. FROM and TO are character positions,
2180 while FROM_BYTE and TO_BYTE are byte positions. */
2182 void
2183 del_range_2 (from, from_byte, to, to_byte)
2184 int from, from_byte, to, to_byte;
2186 register int nbytes_del, nchars_del;
2187 int combined_after_bytes;
2188 Lisp_Object deletion;
2189 int from_byte_1;
2191 CHECK_MARKERS ();
2193 nchars_del = to - from;
2194 nbytes_del = to_byte - from_byte;
2196 /* Make sure the gap is somewhere in or next to what we are deleting. */
2197 if (from > GPT)
2198 gap_right (from, from_byte);
2199 if (to < GPT)
2200 gap_left (to, to_byte, 0);
2202 combined_after_bytes
2203 = count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
2204 Z_BYTE - to_byte, from, from_byte);
2205 if (combined_after_bytes)
2207 if (from == BEGV || to == ZV)
2208 byte_combining_error ();
2209 from_byte_1 = from_byte;
2210 DEC_POS (from_byte_1);
2212 else
2213 from_byte_1 = from_byte;
2215 if (! EQ (current_buffer->undo_list, Qt))
2216 deletion
2217 = make_buffer_string_both (from - !!combined_after_bytes,
2218 from_byte_1,
2219 to + combined_after_bytes,
2220 to_byte + combined_after_bytes, 1);
2221 if (combined_after_bytes)
2222 /* COMBINED_AFTER_BYTES nonzero means that the above code moved
2223 the gap. We must move the gap again to a proper place. */
2224 move_gap_both (from, from_byte);
2226 /* Relocate all markers pointing into the new, larger gap
2227 to point at the end of the text before the gap.
2228 Do this before recording the deletion,
2229 so that undo handles this after reinserting the text. */
2230 adjust_markers_for_delete (from, from_byte, to, to_byte);
2231 if (combined_after_bytes)
2233 /* Adjust markers for the phony deletion
2234 that we are about to call record_undo for. */
2236 /* Here we delete the markers that formerly
2237 pointed at TO ... TO + COMBINED_AFTER_BYTES.
2238 But because of the call to adjust_markers_for_delete, above,
2239 they now point at FROM ... FROM + COMBINED_AFTER_BYTES. */
2240 adjust_markers_for_record_delete (from, from_byte,
2241 from + combined_after_bytes,
2242 from_byte + combined_after_bytes);
2244 adjust_markers_for_record_delete (from - 1, from_byte_1,
2245 from, from_byte);
2247 if (! EQ (current_buffer->undo_list, Qt))
2248 record_delete (from - !!combined_after_bytes, deletion);
2249 MODIFF++;
2251 /* Relocate point as if it were a marker. */
2252 if (from < PT)
2253 adjust_point (from - (PT < to ? PT : to),
2254 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
2256 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2257 offset_intervals (current_buffer, from, - nchars_del);
2259 /* Adjust the overlay center as needed. This must be done after
2260 adjusting the markers that bound the overlays. */
2261 adjust_overlays_for_delete (from, nchars_del);
2263 GAP_SIZE += nbytes_del;
2264 ZV_BYTE -= nbytes_del;
2265 Z_BYTE -= nbytes_del;
2266 ZV -= nchars_del;
2267 Z -= nchars_del;
2268 GPT = from;
2269 GPT_BYTE = from_byte;
2271 if (combined_after_bytes)
2272 move_gap_both (GPT + combined_after_bytes,
2273 GPT_BYTE + combined_after_bytes);
2275 *(GPT_ADDR) = 0; /* Put an anchor. */
2277 if (GPT_BYTE < GPT)
2278 abort ();
2280 if (GPT - BEG < BEG_UNCHANGED)
2281 BEG_UNCHANGED = GPT - BEG;
2282 if (Z - GPT < END_UNCHANGED)
2283 END_UNCHANGED = Z - GPT;
2285 if (combined_after_bytes)
2287 /* Adjust markers for byte combining. As we have already
2288 adjuted markers without concerning byte combining, here we
2289 must concern only byte combining. */
2290 adjust_markers_for_replace (from, from_byte, 0, 0, 0, 0,
2291 0, combined_after_bytes);
2292 combine_bytes (from, from_byte, combined_after_bytes);
2294 record_insert (GPT - 1, 1);
2296 if (Z - GPT < END_UNCHANGED)
2297 END_UNCHANGED = Z - GPT;
2300 CHECK_MARKERS ();
2302 evaporate_overlays (from);
2305 /* Call this if you're about to change the region of BUFFER from
2306 character positions START to END. This checks the read-only
2307 properties of the region, calls the necessary modification hooks,
2308 and warns the next redisplay that it should pay attention to that
2309 area. */
2311 void
2312 modify_region (buffer, start, end)
2313 struct buffer *buffer;
2314 int start, end;
2316 struct buffer *old_buffer = current_buffer;
2318 if (buffer != old_buffer)
2319 set_buffer_internal (buffer);
2321 prepare_to_modify_buffer (start, end, NULL);
2323 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
2325 if (MODIFF <= SAVE_MODIFF)
2326 record_first_change ();
2327 MODIFF++;
2329 buffer->point_before_scroll = Qnil;
2331 if (buffer != old_buffer)
2332 set_buffer_internal (old_buffer);
2335 /* Check that it is okay to modify the buffer between START and END,
2336 which are char positions.
2338 Run the before-change-function, if any. If intervals are in use,
2339 verify that the text to be modified is not read-only, and call
2340 any modification properties the text may have.
2342 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2343 by holding its value temporarily in a marker. */
2345 void
2346 prepare_to_modify_buffer (start, end, preserve_ptr)
2347 int start, end;
2348 int *preserve_ptr;
2350 if (!NILP (current_buffer->read_only))
2351 Fbarf_if_buffer_read_only ();
2353 /* Let redisplay consider other windows than selected_window
2354 if modifying another buffer. */
2355 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2356 ++windows_or_buffers_changed;
2358 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2359 if (BUF_INTERVALS (current_buffer) != 0)
2361 if (preserve_ptr)
2363 Lisp_Object preserve_marker;
2364 struct gcpro gcpro1;
2365 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
2366 GCPRO1 (preserve_marker);
2367 verify_interval_modification (current_buffer, start, end);
2368 *preserve_ptr = marker_position (preserve_marker);
2369 unchain_marker (preserve_marker);
2370 UNGCPRO;
2372 else
2373 verify_interval_modification (current_buffer, start, end);
2376 #ifdef CLASH_DETECTION
2377 if (!NILP (current_buffer->file_truename)
2378 /* Make binding buffer-file-name to nil effective. */
2379 && !NILP (current_buffer->filename)
2380 && SAVE_MODIFF >= MODIFF)
2381 lock_file (current_buffer->file_truename);
2382 #else
2383 /* At least warn if this file has changed on disk since it was visited. */
2384 if (!NILP (current_buffer->filename)
2385 && SAVE_MODIFF >= MODIFF
2386 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2387 && !NILP (Ffile_exists_p (current_buffer->filename)))
2388 call1 (intern ("ask-user-about-supersession-threat"),
2389 current_buffer->filename);
2390 #endif /* not CLASH_DETECTION */
2392 signal_before_change (start, end, preserve_ptr);
2394 if (current_buffer->newline_cache)
2395 invalidate_region_cache (current_buffer,
2396 current_buffer->newline_cache,
2397 start - BEG, Z - end);
2398 if (current_buffer->width_run_cache)
2399 invalidate_region_cache (current_buffer,
2400 current_buffer->width_run_cache,
2401 start - BEG, Z - end);
2403 Vdeactivate_mark = Qt;
2406 /* These macros work with an argument named `preserve_ptr'
2407 and a local variable named `preserve_marker'. */
2409 #define PRESERVE_VALUE \
2410 if (preserve_ptr && NILP (preserve_marker)) \
2411 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2413 #define RESTORE_VALUE \
2414 if (! NILP (preserve_marker)) \
2416 *preserve_ptr = marker_position (preserve_marker); \
2417 unchain_marker (preserve_marker); \
2420 #define PRESERVE_START_END \
2421 if (NILP (start_marker)) \
2422 start_marker = Fcopy_marker (start, Qnil); \
2423 if (NILP (end_marker)) \
2424 end_marker = Fcopy_marker (end, Qnil);
2426 #define FETCH_START \
2427 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2429 #define FETCH_END \
2430 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2432 /* Signal a change to the buffer immediately before it happens.
2433 START_INT and END_INT are the bounds of the text to be changed.
2435 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2436 by holding its value temporarily in a marker. */
2438 void
2439 signal_before_change (start_int, end_int, preserve_ptr)
2440 int start_int, end_int;
2441 int *preserve_ptr;
2443 Lisp_Object start, end;
2444 Lisp_Object start_marker, end_marker;
2445 Lisp_Object preserve_marker;
2446 struct gcpro gcpro1, gcpro2, gcpro3;
2448 if (inhibit_modification_hooks)
2449 return;
2451 start = make_number (start_int);
2452 end = make_number (end_int);
2453 preserve_marker = Qnil;
2454 start_marker = Qnil;
2455 end_marker = Qnil;
2456 GCPRO3 (preserve_marker, start_marker, end_marker);
2458 /* If buffer is unmodified, run a special hook for that case. */
2459 if (SAVE_MODIFF >= MODIFF
2460 && !NILP (Vfirst_change_hook)
2461 && !NILP (Vrun_hooks))
2463 PRESERVE_VALUE;
2464 PRESERVE_START_END;
2465 call1 (Vrun_hooks, Qfirst_change_hook);
2468 /* Run the before-change-function if any.
2469 We don't bother "binding" this variable to nil
2470 because it is obsolete anyway and new code should not use it. */
2471 if (!NILP (Vbefore_change_function))
2473 PRESERVE_VALUE;
2474 PRESERVE_START_END;
2475 call2 (Vbefore_change_function, FETCH_START, FETCH_END);
2478 /* Now run the before-change-functions if any. */
2479 if (!NILP (Vbefore_change_functions))
2481 Lisp_Object args[3];
2482 Lisp_Object before_change_functions;
2483 Lisp_Object after_change_functions;
2484 struct gcpro gcpro1, gcpro2;
2486 PRESERVE_VALUE;
2487 PRESERVE_START_END;
2489 /* "Bind" before-change-functions and after-change-functions
2490 to nil--but in a way that errors don't know about.
2491 That way, if there's an error in them, they will stay nil. */
2492 before_change_functions = Vbefore_change_functions;
2493 after_change_functions = Vafter_change_functions;
2494 Vbefore_change_functions = Qnil;
2495 Vafter_change_functions = Qnil;
2496 GCPRO2 (before_change_functions, after_change_functions);
2498 /* Actually run the hook functions. */
2499 args[0] = Qbefore_change_functions;
2500 args[1] = FETCH_START;
2501 args[2] = FETCH_END;
2502 run_hook_list_with_args (before_change_functions, 3, args);
2504 /* "Unbind" the variables we "bound" to nil. */
2505 Vbefore_change_functions = before_change_functions;
2506 Vafter_change_functions = after_change_functions;
2507 UNGCPRO;
2510 if (!NILP (current_buffer->overlays_before)
2511 || !NILP (current_buffer->overlays_after))
2513 PRESERVE_VALUE;
2514 report_overlay_modification (FETCH_START, FETCH_END, 0,
2515 FETCH_START, FETCH_END, Qnil);
2518 if (! NILP (start_marker))
2519 free_marker (start_marker);
2520 if (! NILP (end_marker))
2521 free_marker (end_marker);
2522 RESTORE_VALUE;
2523 UNGCPRO;
2526 /* Signal a change immediately after it happens.
2527 CHARPOS is the character position of the start of the changed text.
2528 LENDEL is the number of characters of the text before the change.
2529 (Not the whole buffer; just the part that was changed.)
2530 LENINS is the number of characters in that part of the text
2531 after the change. */
2533 void
2534 signal_after_change (charpos, lendel, lenins)
2535 int charpos, lendel, lenins;
2537 if (inhibit_modification_hooks)
2538 return;
2540 /* If we are deferring calls to the after-change functions
2541 and there are no before-change functions,
2542 just record the args that we were going to use. */
2543 if (! NILP (Vcombine_after_change_calls)
2544 && NILP (Vbefore_change_function) && NILP (Vbefore_change_functions)
2545 && NILP (current_buffer->overlays_before)
2546 && NILP (current_buffer->overlays_after))
2548 Lisp_Object elt;
2550 if (!NILP (combine_after_change_list)
2551 && current_buffer != XBUFFER (combine_after_change_buffer))
2552 Fcombine_after_change_execute ();
2554 elt = Fcons (make_number (charpos - BEG),
2555 Fcons (make_number (Z - (charpos - lendel + lenins)),
2556 Fcons (make_number (lenins - lendel), Qnil)));
2557 combine_after_change_list
2558 = Fcons (elt, combine_after_change_list);
2559 combine_after_change_buffer = Fcurrent_buffer ();
2561 return;
2564 if (!NILP (combine_after_change_list))
2565 Fcombine_after_change_execute ();
2567 /* Run the after-change-function if any.
2568 We don't bother "binding" this variable to nil
2569 because it is obsolete anyway and new code should not use it. */
2570 if (!NILP (Vafter_change_function))
2571 call3 (Vafter_change_function,
2572 make_number (charpos), make_number (charpos + lenins),
2573 make_number (lendel));
2575 if (!NILP (Vafter_change_functions))
2577 Lisp_Object args[4];
2578 Lisp_Object before_change_functions;
2579 Lisp_Object after_change_functions;
2580 struct gcpro gcpro1, gcpro2;
2582 /* "Bind" before-change-functions and after-change-functions
2583 to nil--but in a way that errors don't know about.
2584 That way, if there's an error in them, they will stay nil. */
2585 before_change_functions = Vbefore_change_functions;
2586 after_change_functions = Vafter_change_functions;
2587 Vbefore_change_functions = Qnil;
2588 Vafter_change_functions = Qnil;
2589 GCPRO2 (before_change_functions, after_change_functions);
2591 /* Actually run the hook functions. */
2592 args[0] = Qafter_change_functions;
2593 XSETFASTINT (args[1], charpos);
2594 XSETFASTINT (args[2], charpos + lenins);
2595 XSETFASTINT (args[3], lendel);
2596 run_hook_list_with_args (after_change_functions,
2597 4, args);
2599 /* "Unbind" the variables we "bound" to nil. */
2600 Vbefore_change_functions = before_change_functions;
2601 Vafter_change_functions = after_change_functions;
2602 UNGCPRO;
2605 if (!NILP (current_buffer->overlays_before)
2606 || !NILP (current_buffer->overlays_after))
2607 report_overlay_modification (make_number (charpos),
2608 make_number (charpos + lenins),
2610 make_number (charpos),
2611 make_number (charpos + lenins),
2612 make_number (lendel));
2614 /* After an insertion, call the text properties
2615 insert-behind-hooks or insert-in-front-hooks. */
2616 if (lendel == 0)
2617 report_interval_modification (make_number (charpos),
2618 make_number (charpos + lenins));
2621 Lisp_Object
2622 Fcombine_after_change_execute_1 (val)
2623 Lisp_Object val;
2625 Vcombine_after_change_calls = val;
2626 return val;
2629 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2630 Scombine_after_change_execute, 0, 0, 0,
2631 "This function is for use internally in `combine-after-change-calls'.")
2634 int count = specpdl_ptr - specpdl;
2635 int beg, end, change;
2636 int begpos, endpos;
2637 Lisp_Object tail;
2639 if (NILP (combine_after_change_list))
2640 return Qnil;
2642 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2644 Fset_buffer (combine_after_change_buffer);
2646 /* # chars unchanged at beginning of buffer. */
2647 beg = Z - BEG;
2648 /* # chars unchanged at end of buffer. */
2649 end = beg;
2650 /* Total amount of insertion (negative for deletion). */
2651 change = 0;
2653 /* Scan the various individual changes,
2654 accumulating the range info in BEG, END and CHANGE. */
2655 for (tail = combine_after_change_list; CONSP (tail);
2656 tail = XCDR (tail))
2658 Lisp_Object elt;
2659 int thisbeg, thisend, thischange;
2661 /* Extract the info from the next element. */
2662 elt = XCAR (tail);
2663 if (! CONSP (elt))
2664 continue;
2665 thisbeg = XINT (XCAR (elt));
2667 elt = XCDR (elt);
2668 if (! CONSP (elt))
2669 continue;
2670 thisend = XINT (XCAR (elt));
2672 elt = XCDR (elt);
2673 if (! CONSP (elt))
2674 continue;
2675 thischange = XINT (XCAR (elt));
2677 /* Merge this range into the accumulated range. */
2678 change += thischange;
2679 if (thisbeg < beg)
2680 beg = thisbeg;
2681 if (thisend < end)
2682 end = thisend;
2685 /* Get the current start and end positions of the range
2686 that was changed. */
2687 begpos = BEG + beg;
2688 endpos = Z - end;
2690 /* We are about to handle these, so discard them. */
2691 combine_after_change_list = Qnil;
2693 /* Now run the after-change functions for real.
2694 Turn off the flag that defers them. */
2695 record_unwind_protect (Fcombine_after_change_execute_1,
2696 Vcombine_after_change_calls);
2697 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2699 return unbind_to (count, Qnil);
2702 void
2703 syms_of_insdel ()
2705 staticpro (&combine_after_change_list);
2706 combine_after_change_list = Qnil;
2707 combine_after_change_buffer = Qnil;
2709 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
2710 "Non-nil means enable debugging checks for invalid marker positions.");
2711 check_markers_debug_flag = 0;
2712 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2713 "Used internally by the `combine-after-change-calls' macro.");
2714 Vcombine_after_change_calls = Qnil;
2716 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks,
2717 "Non-nil means don't run any of the hooks that respond to buffer changes.\n\
2718 This affects `before-change-functions' and `after-change-functions',\n\
2719 as well as hooks attached to text properties and overlays.");
2720 inhibit_modification_hooks = 0;
2722 defsubr (&Scombine_after_change_execute);