(enum gdb_lisp_params): Put in #if 0, since it doesn't
[emacs.git] / src / insdel.c
blob05c86616c1dadccaf5453821ccad413e30394686
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 86,93,94,95,97,98, 1999 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 #include "lisp.h"
24 #include "intervals.h"
25 #include "buffer.h"
26 #include "charset.h"
27 #include "window.h"
28 #include "blockinput.h"
29 #include "region-cache.h"
31 #ifndef NULL
32 #define NULL 0
33 #endif
35 #define min(x, y) ((x) < (y) ? (x) : (y))
36 #define max(x, y) ((x) > (y) ? (x) : (y))
38 static void insert_from_string_1 P_ ((Lisp_Object, int, int, int, int, int, int));
39 static void insert_from_buffer_1 ();
40 static void gap_left P_ ((int, int, int));
41 static void gap_right P_ ((int, int));
42 static void adjust_markers_gap_motion P_ ((int, int, int));
43 static void adjust_markers_for_insert P_ ((int, int, int, int, int, int, int));
44 static void adjust_markers_for_delete P_ ((int, int, int, int));
45 static void adjust_markers_for_record_delete P_ ((int, int, int, int));
46 static void adjust_point P_ ((int, int));
48 Lisp_Object Fcombine_after_change_execute ();
50 /* Non-nil means don't call the after-change-functions right away,
51 just record an element in Vcombine_after_change_calls_list. */
52 Lisp_Object Vcombine_after_change_calls;
54 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
55 describing changes which happened while combine_after_change_calls
56 was nonzero. We use this to decide how to call them
57 once the deferral ends.
59 In each element.
60 BEG-UNCHANGED is the number of chars before the changed range.
61 END-UNCHANGED is the number of chars after the changed range,
62 and CHANGE-AMOUNT is the number of characters inserted by the change
63 (negative for a deletion). */
64 Lisp_Object combine_after_change_list;
66 /* Buffer which combine_after_change_list is about. */
67 Lisp_Object combine_after_change_buffer;
69 /* Check all markers in the current buffer, looking for something invalid. */
71 static int check_markers_debug_flag;
73 #define CHECK_MARKERS() \
74 if (check_markers_debug_flag) \
75 check_markers (); \
76 else
78 void
79 check_markers ()
81 register Lisp_Object tail;
82 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
84 tail = BUF_MARKERS (current_buffer);
86 while (XSYMBOL (tail) != XSYMBOL (Qnil))
88 if (XMARKER (tail)->buffer->text != current_buffer->text)
89 abort ();
90 if (XMARKER (tail)->charpos > Z)
91 abort ();
92 if (XMARKER (tail)->bytepos > Z_BYTE)
93 abort ();
94 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (XMARKER (tail)->bytepos)))
95 abort ();
97 tail = XMARKER (tail)->chain;
101 /* Move gap to position CHARPOS.
102 Note that this can quit! */
104 void
105 move_gap (charpos)
106 int charpos;
108 move_gap_both (charpos, charpos_to_bytepos (charpos));
111 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
112 Note that this can quit! */
114 void
115 move_gap_both (charpos, bytepos)
116 int charpos, bytepos;
118 if (bytepos < GPT_BYTE)
119 gap_left (charpos, bytepos, 0);
120 else if (bytepos > GPT_BYTE)
121 gap_right (charpos, bytepos);
124 /* Move the gap to a position less than the current GPT.
125 BYTEPOS describes the new position as a byte position,
126 and CHARPOS is the corresponding char position.
127 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
129 static void
130 gap_left (charpos, bytepos, newgap)
131 register int charpos, bytepos;
132 int newgap;
134 register unsigned char *to, *from;
135 register int i;
136 int new_s1;
138 if (!newgap)
139 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
141 i = GPT_BYTE;
142 to = GAP_END_ADDR;
143 from = GPT_ADDR;
144 new_s1 = GPT_BYTE;
146 /* Now copy the characters. To move the gap down,
147 copy characters up. */
149 while (1)
151 /* I gets number of characters left to copy. */
152 i = new_s1 - bytepos;
153 if (i == 0)
154 break;
155 /* If a quit is requested, stop copying now.
156 Change BYTEPOS to be where we have actually moved the gap to. */
157 if (QUITP)
159 bytepos = new_s1;
160 charpos = BYTE_TO_CHAR (bytepos);
161 break;
163 /* Move at most 32000 chars before checking again for a quit. */
164 if (i > 32000)
165 i = 32000;
166 #ifdef GAP_USE_BCOPY
167 if (i >= 128
168 /* bcopy is safe if the two areas of memory do not overlap
169 or on systems where bcopy is always safe for moving upward. */
170 && (BCOPY_UPWARD_SAFE
171 || to - from >= 128))
173 /* If overlap is not safe, avoid it by not moving too many
174 characters at once. */
175 if (!BCOPY_UPWARD_SAFE && i > to - from)
176 i = to - from;
177 new_s1 -= i;
178 from -= i, to -= i;
179 bcopy (from, to, i);
181 else
182 #endif
184 new_s1 -= i;
185 while (--i >= 0)
186 *--to = *--from;
190 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
191 BYTEPOS is where the loop above stopped, which may be what was specified
192 or may be where a quit was detected. */
193 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
194 GPT_BYTE = bytepos;
195 GPT = charpos;
196 if (bytepos < charpos)
197 abort ();
198 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
199 QUIT;
202 /* Move the gap to a position greater than than the current GPT.
203 BYTEPOS describes the new position as a byte position,
204 and CHARPOS is the corresponding char position. */
206 static void
207 gap_right (charpos, bytepos)
208 register int charpos, bytepos;
210 register unsigned char *to, *from;
211 register int i;
212 int new_s1;
214 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
216 i = GPT_BYTE;
217 from = GAP_END_ADDR;
218 to = GPT_ADDR;
219 new_s1 = GPT_BYTE;
221 /* Now copy the characters. To move the gap up,
222 copy characters down. */
224 while (1)
226 /* I gets number of characters left to copy. */
227 i = bytepos - new_s1;
228 if (i == 0)
229 break;
230 /* If a quit is requested, stop copying now.
231 Change BYTEPOS to be where we have actually moved the gap to. */
232 if (QUITP)
234 bytepos = new_s1;
235 charpos = BYTE_TO_CHAR (bytepos);
236 break;
238 /* Move at most 32000 chars before checking again for a quit. */
239 if (i > 32000)
240 i = 32000;
241 #ifdef GAP_USE_BCOPY
242 if (i >= 128
243 /* bcopy is safe if the two areas of memory do not overlap
244 or on systems where bcopy is always safe for moving downward. */
245 && (BCOPY_DOWNWARD_SAFE
246 || from - to >= 128))
248 /* If overlap is not safe, avoid it by not moving too many
249 characters at once. */
250 if (!BCOPY_DOWNWARD_SAFE && i > from - to)
251 i = from - to;
252 new_s1 += i;
253 bcopy (from, to, i);
254 from += i, to += i;
256 else
257 #endif
259 new_s1 += i;
260 while (--i >= 0)
261 *to++ = *from++;
265 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
266 - GAP_SIZE);
267 GPT = charpos;
268 GPT_BYTE = bytepos;
269 if (bytepos < charpos)
270 abort ();
271 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
272 QUIT;
275 /* Add AMOUNT to the byte position of every marker in the current buffer
276 whose current byte position is between FROM (exclusive) and TO (inclusive).
278 Also, any markers past the outside of that interval, in the direction
279 of adjustment, are first moved back to the near end of the interval
280 and then adjusted by AMOUNT.
282 When the latter adjustment is done, if AMOUNT is negative,
283 we record the adjustment for undo. (This case happens only for
284 deletion.)
286 The markers' character positions are not altered,
287 because gap motion does not affect character positions. */
289 int adjust_markers_test;
291 static void
292 adjust_markers_gap_motion (from, to, amount)
293 register int from, to, amount;
295 /* Now that a marker has a bytepos, not counting the gap,
296 nothing needs to be done here. */
297 #if 0
298 Lisp_Object marker;
299 register struct Lisp_Marker *m;
300 register int mpos;
302 marker = BUF_MARKERS (current_buffer);
304 while (!NILP (marker))
306 m = XMARKER (marker);
307 mpos = m->bytepos;
308 if (amount > 0)
310 if (mpos > to && mpos < to + amount)
312 if (adjust_markers_test)
313 abort ();
314 mpos = to + amount;
317 else
319 /* Here's the case where a marker is inside text being deleted.
320 AMOUNT can be negative for gap motion, too,
321 but then this range contains no markers. */
322 if (mpos > from + amount && mpos <= from)
324 if (adjust_markers_test)
325 abort ();
326 mpos = from + amount;
329 if (mpos > from && mpos <= to)
330 mpos += amount;
331 m->bufpos = mpos;
332 marker = m->chain;
334 #endif
337 /* Adjust all markers for a deletion
338 whose range in bytes is FROM_BYTE to TO_BYTE.
339 The range in charpos is FROM to TO.
341 This function assumes that the gap is adjacent to
342 or inside of the range being deleted. */
344 static void
345 adjust_markers_for_delete (from, from_byte, to, to_byte)
346 register int from, from_byte, to, to_byte;
348 Lisp_Object marker;
349 register struct Lisp_Marker *m;
350 register int charpos;
352 marker = BUF_MARKERS (current_buffer);
354 while (!NILP (marker))
356 m = XMARKER (marker);
357 charpos = m->charpos;
359 if (charpos > Z)
360 abort ();
362 /* If the marker is after the deletion,
363 relocate by number of chars / bytes deleted. */
364 if (charpos > to)
366 m->charpos -= to - from;
367 m->bytepos -= to_byte - from_byte;
370 /* Here's the case where a marker is inside text being deleted. */
371 else if (charpos > from)
373 record_marker_adjustment (marker, from - charpos);
374 m->charpos = from;
375 m->bytepos = from_byte;
378 marker = m->chain;
383 /* Adjust all markers for calling record_delete for combining bytes.
384 whose range in bytes is FROM_BYTE to TO_BYTE.
385 The range in charpos is FROM to TO. */
387 static void
388 adjust_markers_for_record_delete (from, from_byte, to, to_byte)
389 register int from, from_byte, to, to_byte;
391 Lisp_Object marker;
392 register struct Lisp_Marker *m;
393 register int charpos;
395 marker = BUF_MARKERS (current_buffer);
397 while (!NILP (marker))
399 m = XMARKER (marker);
400 charpos = m->charpos;
402 /* If the marker is after the deletion,
403 relocate by number of chars / bytes deleted. */
404 if (charpos > to)
406 /* Here's the case where a marker is inside text being deleted. */
407 else if (charpos > from)
408 record_marker_adjustment (marker, from - charpos);
410 marker = m->chain;
414 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
415 to TO / TO_BYTE. We have to relocate the charpos of every marker
416 that points after the insertion (but not their bytepos).
418 COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
419 that combine into one character with the text before the insertion.
420 COMBINED_AFTER_BYTES is the number of bytes after the insertion
421 that combine into one character with the last inserted bytes.
423 When a marker points at the insertion point,
424 we advance it if either its insertion-type is t
425 or BEFORE_MARKERS is true. */
427 static void
428 adjust_markers_for_insert (from, from_byte, to, to_byte,
429 combined_before_bytes, combined_after_bytes,
430 before_markers)
431 register int from, from_byte, to, to_byte;
432 int combined_before_bytes, combined_after_bytes, before_markers;
434 Lisp_Object marker;
435 int adjusted = 0;
436 int nchars = to - from;
437 int nbytes = to_byte - from_byte;
439 marker = BUF_MARKERS (current_buffer);
441 while (!NILP (marker))
443 register struct Lisp_Marker *m = XMARKER (marker);
445 /* In a single-byte buffer, a marker's two positions must be equal.
446 (If this insertion is going to combine characters, Z will
447 become different from Z_BYTE, but they might be the same now.
448 If so, the two OLD positions of the marker should be equal.) */
449 if (Z == Z_BYTE)
451 if (m->charpos != m->bytepos)
452 abort ();
455 if (m->bytepos == from_byte)
457 if (m->insertion_type || before_markers)
459 m->bytepos = to_byte + combined_after_bytes;
460 m->charpos = to - combined_before_bytes;
461 /* Point the marker before the combined character,
462 so that undoing the insertion puts it back where it was. */
463 if (combined_after_bytes)
464 DEC_BOTH (m->charpos, m->bytepos);
465 if (m->insertion_type)
466 adjusted = 1;
468 else if (combined_before_bytes)
470 /* This marker doesn't "need relocation",
471 but don't leave it pointing in the middle of a character.
472 Point the marker after the combined character,
473 so that undoing the insertion puts it back where it was. */
474 m->bytepos += combined_before_bytes;
475 if (combined_before_bytes == nbytes)
476 /* All new bytes plus combined_after_bytes (if any)
477 are combined. */
478 m->bytepos += combined_after_bytes;
481 /* If a marker was pointing into the combining bytes
482 after the insertion, don't leave it there
483 in the middle of a character. */
484 else if (combined_after_bytes && m->bytepos >= from_byte
485 && m->bytepos < from_byte + combined_after_bytes)
487 /* Put it after the combining bytes. */
488 m->bytepos = to_byte + combined_after_bytes;
489 m->charpos = to - combined_before_bytes;
490 /* Now move it back before the combined character,
491 so that undoing the insertion will put it where it was. */
492 DEC_BOTH (m->charpos, m->bytepos);
494 else if (m->bytepos > from_byte)
496 m->bytepos += nbytes;
497 m->charpos += nchars - combined_after_bytes - combined_before_bytes;
500 marker = m->chain;
503 /* Adjusting only markers whose insertion-type is t may result in
504 disordered overlays in the slot `overlays_before'. */
505 if (adjusted)
506 fix_overlays_before (current_buffer, from, to);
509 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
511 This is used only when the value of point changes due to an insert
512 or delete; it does not represent a conceptual change in point as a
513 marker. In particular, point is not crossing any interval
514 boundaries, so there's no need to use the usual SET_PT macro. In
515 fact it would be incorrect to do so, because either the old or the
516 new value of point is out of sync with the current set of
517 intervals. */
519 static void
520 adjust_point (nchars, nbytes)
521 int nchars, nbytes;
523 BUF_PT (current_buffer) += nchars;
524 BUF_PT_BYTE (current_buffer) += nbytes;
526 /* In a single-byte buffer, the two positions must be equal. */
527 if (ZV == ZV_BYTE
528 && PT != PT_BYTE)
529 abort ();
532 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
533 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
534 (NEW_BYTES).
536 See the comment of adjust_markers_for_insert for the args
537 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES. */
539 static void
540 adjust_markers_for_replace (from, from_byte, old_chars, old_bytes,
541 new_chars, new_bytes,
542 combined_before_bytes, combined_after_bytes)
543 int from, from_byte, old_chars, old_bytes, new_chars, new_bytes;
544 int combined_before_bytes, combined_after_bytes;
546 Lisp_Object marker = BUF_MARKERS (current_buffer);
547 int prev_to_byte = from_byte + old_bytes;
548 int diff_chars
549 = (new_chars - combined_before_bytes) - (old_chars + combined_after_bytes);
550 int diff_bytes = new_bytes - old_bytes;
552 while (!NILP (marker))
554 register struct Lisp_Marker *m = XMARKER (marker);
556 if (m->bytepos >= prev_to_byte
557 && (old_bytes != 0
558 /* If this is an insertion (replacing 0 chars),
559 reject the case of a marker that is at the
560 insertion point and should stay before the insertion. */
561 || m->bytepos > from_byte || m->insertion_type))
563 if (m->bytepos < prev_to_byte + combined_after_bytes)
565 /* Put it after the combining bytes. */
566 m->bytepos = from_byte + new_bytes + combined_after_bytes;
567 m->charpos = from + new_chars - combined_before_bytes;
569 else
571 m->charpos += diff_chars;
572 m->bytepos += diff_bytes;
575 else if (m->bytepos >= from_byte)
577 m->charpos = from;
578 m->bytepos = from_byte + combined_before_bytes;
579 /* If all new bytes are combined in addition to that there
580 are after combining bytes, we must set byte position of
581 the marker after the after combining bytes. */
582 if (combined_before_bytes == new_bytes)
583 m->bytepos += combined_after_bytes;
586 marker = m->chain;
589 CHECK_MARKERS ();
593 /* Make the gap NBYTES_ADDED bytes longer. */
595 void
596 make_gap (nbytes_added)
597 int nbytes_added;
599 unsigned char *result;
600 Lisp_Object tem;
601 int real_gap_loc;
602 int real_gap_loc_byte;
603 int old_gap_size;
605 /* If we have to get more space, get enough to last a while. */
606 nbytes_added += 2000;
608 /* Don't allow a buffer size that won't fit in an int
609 even if it will fit in a Lisp integer.
610 That won't work because so many places use `int'. */
612 if (Z_BYTE - BEG_BYTE + GAP_SIZE + nbytes_added
613 >= ((unsigned) 1 << (min (BITS_PER_INT, VALBITS) - 1)))
614 error ("Buffer exceeds maximum size");
616 BLOCK_INPUT;
617 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
618 result = BUFFER_REALLOC (BEG_ADDR, (Z_BYTE - BEG_BYTE
619 + GAP_SIZE + nbytes_added + 1));
621 if (result == 0)
623 UNBLOCK_INPUT;
624 memory_full ();
627 /* We can't unblock until the new address is properly stored. */
628 BEG_ADDR = result;
629 UNBLOCK_INPUT;
631 /* Prevent quitting in move_gap. */
632 tem = Vinhibit_quit;
633 Vinhibit_quit = Qt;
635 real_gap_loc = GPT;
636 real_gap_loc_byte = GPT_BYTE;
637 old_gap_size = GAP_SIZE;
639 /* Call the newly allocated space a gap at the end of the whole space. */
640 GPT = Z + GAP_SIZE;
641 GPT_BYTE = Z_BYTE + GAP_SIZE;
642 GAP_SIZE = nbytes_added;
644 /* Move the new gap down to be consecutive with the end of the old one.
645 This adjusts the markers properly too. */
646 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
648 /* Now combine the two into one large gap. */
649 GAP_SIZE += old_gap_size;
650 GPT = real_gap_loc;
651 GPT_BYTE = real_gap_loc_byte;
653 /* Put an anchor. */
654 *(Z_ADDR) = 0;
656 Vinhibit_quit = tem;
659 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
660 FROM_MULTIBYTE says whether the incoming text is multibyte.
661 TO_MULTIBYTE says whether to store the text as multibyte.
662 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
664 Return the number of bytes stored at TO_ADDR. */
667 copy_text (from_addr, to_addr, nbytes,
668 from_multibyte, to_multibyte)
669 unsigned char *from_addr;
670 unsigned char *to_addr;
671 int nbytes;
672 int from_multibyte, to_multibyte;
674 if (from_multibyte == to_multibyte)
676 bcopy (from_addr, to_addr, nbytes);
677 return nbytes;
679 else if (from_multibyte)
681 int nchars = 0;
682 int bytes_left = nbytes;
683 Lisp_Object tbl = Qnil;
685 /* We set the variable tbl to the reverse table of
686 Vnonascii_translation_table in advance. */
687 if (CHAR_TABLE_P (Vnonascii_translation_table))
689 tbl = Fchar_table_extra_slot (Vnonascii_translation_table,
690 make_number (0));
691 if (!CHAR_TABLE_P (tbl))
692 tbl = Qnil;
695 /* Convert multibyte to single byte. */
696 while (bytes_left > 0)
698 int thislen, c, c_save;
699 c = c_save = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
700 if (!SINGLE_BYTE_CHAR_P (c))
701 c = multibyte_char_to_unibyte (c, tbl);
702 *to_addr++ = c;
703 from_addr += thislen;
704 bytes_left -= thislen;
705 nchars++;
707 return nchars;
709 else
711 unsigned char *initial_to_addr = to_addr;
713 /* Convert single-byte to multibyte. */
714 while (nbytes > 0)
716 int c = *from_addr++;
717 unsigned char workbuf[4], *str;
718 int len;
720 if (c < 0400
721 && (c >= 0240
722 || (c >= 0200 && !NILP (Vnonascii_translation_table))))
724 c = unibyte_char_to_multibyte (c);
725 len = CHAR_STRING (c, workbuf, str);
726 bcopy (str, to_addr, len);
727 to_addr += len;
728 nbytes--;
730 else
731 /* Special case for speed. */
732 *to_addr++ = c, nbytes--;
734 return to_addr - initial_to_addr;
738 /* Return the number of bytes it would take
739 to convert some single-byte text to multibyte.
740 The single-byte text consists of NBYTES bytes at PTR. */
743 count_size_as_multibyte (ptr, nbytes)
744 unsigned char *ptr;
745 int nbytes;
747 int i;
748 int outgoing_nbytes = 0;
750 for (i = 0; i < nbytes; i++)
752 unsigned int c = *ptr++;
754 if (c < 0200 || (c < 0240 && NILP (Vnonascii_translation_table)))
755 outgoing_nbytes++;
756 else
758 c = unibyte_char_to_multibyte (c);
759 outgoing_nbytes += CHAR_BYTES (c);
763 return outgoing_nbytes;
766 /* Insert a string of specified length before point.
767 This function judges multibyteness based on
768 enable_multibyte_characters in the current buffer;
769 it never converts between single-byte and multibyte.
771 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
772 prepare_to_modify_buffer could relocate the text. */
774 void
775 insert (string, nbytes)
776 register unsigned char *string;
777 register int nbytes;
779 if (nbytes > 0)
781 int opoint = PT;
782 insert_1 (string, nbytes, 0, 1, 0);
783 signal_after_change (opoint, 0, PT - opoint);
787 /* Likewise, but inherit text properties from neighboring characters. */
789 void
790 insert_and_inherit (string, nbytes)
791 register unsigned char *string;
792 register int nbytes;
794 if (nbytes > 0)
796 int opoint = PT;
797 insert_1 (string, nbytes, 1, 1, 0);
798 signal_after_change (opoint, 0, PT - opoint);
802 /* Insert the character C before point. Do not inherit text properties. */
804 void
805 insert_char (c)
806 int c;
808 unsigned char workbuf[4], *str;
809 int len;
811 if (! NILP (current_buffer->enable_multibyte_characters))
812 len = CHAR_STRING (c, workbuf, str);
813 else
815 len = 1;
816 workbuf[0] = c;
817 str = workbuf;
820 insert (str, len);
823 /* Insert the null-terminated string S before point. */
825 void
826 insert_string (s)
827 char *s;
829 insert (s, strlen (s));
832 /* Like `insert' except that all markers pointing at the place where
833 the insertion happens are adjusted to point after it.
834 Don't use this function to insert part of a Lisp string,
835 since gc could happen and relocate it. */
837 void
838 insert_before_markers (string, nbytes)
839 unsigned char *string;
840 register int nbytes;
842 if (nbytes > 0)
844 int opoint = PT;
846 insert_1 (string, nbytes, 0, 1, 1);
847 signal_after_change (opoint, 0, PT - opoint);
851 /* Likewise, but inherit text properties from neighboring characters. */
853 void
854 insert_before_markers_and_inherit (string, nbytes)
855 unsigned char *string;
856 register int nbytes;
858 if (nbytes > 0)
860 int opoint = PT;
862 insert_1 (string, nbytes, 1, 1, 1);
863 signal_after_change (opoint, 0, PT - opoint);
867 /* Subroutine used by the insert functions above. */
869 void
870 insert_1 (string, nbytes, inherit, prepare, before_markers)
871 register unsigned char *string;
872 register int nbytes;
873 int inherit, prepare, before_markers;
875 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
876 inherit, prepare, before_markers);
879 /* See if the byte sequence at STR1 of length LEN1 combine with the
880 byte sequence at STR2 of length LEN2 to form a single composite
881 character. If so, return the number of bytes at the start of STR2
882 which combine in this way. Otherwise, return 0. If STR3 is not
883 NULL, it is a byte sequence of length LEN3 to be appended to STR1
884 before checking the combining. */
886 count_combining_composition (str1, len1, str2, len2, str3, len3)
887 unsigned char *str1, *str2, *str3;
888 int len1, len2, len3;
890 int len = len1 + len2 + len3;
891 unsigned char *buf = (unsigned char *) alloca (len + 1);
892 int bytes;
894 bcopy (str1, buf, len1);
895 if (str3)
897 bcopy (str3, buf + len1, len3);
898 len1 += len3;
900 bcopy (str2, buf + len1 , len2);
901 buf[len] = 0;
902 PARSE_MULTIBYTE_SEQ (buf, len, bytes);
903 return (bytes <= len1 ? 0 : bytes - len1);
906 /* See if the bytes before POS/POS_BYTE combine with bytes
907 at the start of STRING to form a single character.
908 If so, return the number of bytes at the start of STRING
909 which combine in this way. Otherwise, return 0. */
912 count_combining_before (string, length, pos, pos_byte)
913 unsigned char *string;
914 int length;
915 int pos, pos_byte;
917 int len, combining_bytes;
918 unsigned char *p;
920 if (NILP (current_buffer->enable_multibyte_characters))
921 return 0;
923 /* At first, we can exclude the following cases:
924 (1) STRING[0] can't be a following byte of multibyte sequence.
925 (2) POS is the start of the current buffer.
926 (3) A character before POS is not a multibyte character. */
927 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
928 return 0;
929 if (pos_byte == BEG_BYTE) /* case (2) */
930 return 0;
931 len = 1;
932 p = BYTE_POS_ADDR (pos_byte - 1);
933 while (! CHAR_HEAD_P (*p)) p--, len++;
934 if (! BASE_LEADING_CODE_P (*p)) /* case (3) */
935 return 0;
937 /* A sequence of a composite character requires a special handling. */
938 if (*p == LEADING_CODE_COMPOSITION)
939 return count_combining_composition (p, len, string, length, NULL, 0);
941 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
942 if (combining_bytes <= 0)
943 /* The character preceding POS is, complete and no room for
944 combining bytes (combining_bytes == 0), or an independent 8-bit
945 character (combining_bytes < 0). */
946 return 0;
948 /* We have a combination situation. Count the bytes at STRING that
949 may combine. */
950 p = string + 1;
951 while (!CHAR_HEAD_P (*p) && p < string + length)
952 p++;
954 return (combining_bytes < p - string ? combining_bytes : p - string);
957 /* See if the bytes after POS/POS_BYTE combine with bytes
958 at the end of STRING to form a single character.
959 If so, return the number of bytes after POS/POS_BYTE
960 which combine in this way. Otherwise, return 0. */
963 count_combining_after (string, length, pos, pos_byte)
964 unsigned char *string;
965 int length;
966 int pos, pos_byte;
968 int opos_byte = pos_byte;
969 int i;
970 int bytes;
971 unsigned char *bufp;
973 if (NILP (current_buffer->enable_multibyte_characters))
974 return 0;
976 /* At first, we can exclude the following cases:
977 (1) The last byte of STRING is an ASCII.
978 (2) POS is the last of the current buffer.
979 (3) A character at POS can't be a following byte of multibyte
980 character. */
981 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
982 return 0;
983 if (pos_byte == Z_BYTE) /* case (2) */
984 return 0;
985 bufp = BYTE_POS_ADDR (pos_byte);
986 if (CHAR_HEAD_P (*bufp)) /* case (3) */
987 return 0;
989 i = length - 1;
990 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
992 i--;
994 if (i < 0)
996 /* All characters in STRING are not character head. We must
997 check also preceding bytes at POS. We are sure that the gap
998 is at POS. */
999 unsigned char *p = BEG_ADDR;
1000 i = pos_byte - 2;
1001 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
1002 i--;
1003 if (i < 0 || !BASE_LEADING_CODE_P (p[i]))
1004 return 0;
1005 /* A sequence of a composite character requires a special handling. */
1006 if (p[i] == LEADING_CODE_COMPOSITION)
1007 return count_combining_composition (p + i, pos_byte - 1 - i,
1008 bufp, Z_BYTE - pos_byte,
1009 string, length);
1010 bytes = BYTES_BY_CHAR_HEAD (p[i]);
1011 return (bytes <= pos_byte - 1 - i + length
1013 : bytes - (pos_byte - 1 - i + length));
1015 if (!BASE_LEADING_CODE_P (string[i]))
1016 return 0;
1017 /* A sequence of a composite character requires a special handling. */
1018 if (string[i] == LEADING_CODE_COMPOSITION)
1019 return count_combining_composition (string + i, length - i,
1020 bufp, Z_BYTE - pos_byte, NULL, 0);
1022 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
1023 bufp++, pos_byte++;
1024 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
1026 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
1029 /* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
1030 following the position POS/POS_BYTE to the character preceding POS.
1031 If TARGET is after POS+NBYTES, we only have to adjust the character
1032 position TARGET, else, if TARGET is after POS, we have to adjust
1033 both the character position TARGET and the byte position
1034 TARGET_BYTE, else we don't have to do any adjustment. */
1036 #define ADJUST_CHAR_POS(target, target_byte) \
1037 do { \
1038 if (target > pos + nbytes) \
1039 target -= nbytes; \
1040 else if (target >= pos) \
1042 target = pos; \
1043 target_byte = pos_byte + nbytes; \
1045 } while (0)
1047 /* Combine NBYTES stray trailing-codes, which were formerly separate
1048 characters, with the preceding character. These bytes
1049 are located after position POS / POS_BYTE, and the preceding character
1050 is located just before that position.
1052 This function does not adjust markers for byte combining. That
1053 should be done in advance by the functions
1054 adjust_markers_for_insert or adjust_markers_for_replace. */
1056 static void
1057 combine_bytes (pos, pos_byte, nbytes)
1058 int pos, pos_byte, nbytes;
1060 adjust_overlays_for_delete (pos, nbytes);
1062 ADJUST_CHAR_POS (BUF_PT (current_buffer), BUF_PT_BYTE (current_buffer));
1063 ADJUST_CHAR_POS (GPT, GPT_BYTE);
1064 ADJUST_CHAR_POS (Z, Z_BYTE);
1065 ADJUST_CHAR_POS (ZV, ZV_BYTE);
1067 if (BUF_INTERVALS (current_buffer) != 0)
1068 offset_intervals (current_buffer, pos, - nbytes);
1071 void
1072 byte_combining_error ()
1074 error ("Byte combining across boundary of accessible buffer text inhibitted");
1077 /* If we are going to combine bytes at POS which is at a narrowed
1078 region boundary, signal an error. */
1079 #define CHECK_BYTE_COMBINING_FOR_INSERT(pos) \
1080 do { \
1081 if ((combined_before_bytes && pos == BEGV) \
1082 || (combined_after_bytes && pos == ZV)) \
1083 byte_combining_error (); \
1084 } while (0)
1087 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1088 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
1089 are the same as in insert_1. */
1091 void
1092 insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
1093 register unsigned char *string;
1094 register int nchars, nbytes;
1095 int inherit, prepare, before_markers;
1097 int combined_before_bytes, combined_after_bytes;
1099 if (NILP (current_buffer->enable_multibyte_characters))
1100 nchars = nbytes;
1102 if (prepare)
1103 /* Do this before moving and increasing the gap,
1104 because the before-change hooks might move the gap
1105 or make it smaller. */
1106 prepare_to_modify_buffer (PT, PT, NULL);
1108 if (PT != GPT)
1109 move_gap_both (PT, PT_BYTE);
1110 if (GAP_SIZE < nbytes)
1111 make_gap (nbytes - GAP_SIZE);
1113 combined_before_bytes
1114 = count_combining_before (string, nbytes, PT, PT_BYTE);
1115 combined_after_bytes
1116 = count_combining_after (string, nbytes, PT, PT_BYTE);
1117 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1119 /* Record deletion of the surrounding text that combines with
1120 the insertion. This, together with recording the insertion,
1121 will add up to the right stuff in the undo list.
1123 But there is no need to actually delete the combining bytes
1124 from the buffer and reinsert them. */
1126 if (combined_after_bytes)
1128 Lisp_Object deletion;
1129 deletion = Qnil;
1131 if (! EQ (current_buffer->undo_list, Qt))
1132 deletion = make_buffer_string_both (PT, PT_BYTE,
1133 PT + combined_after_bytes,
1134 PT_BYTE + combined_after_bytes, 1);
1136 adjust_markers_for_record_delete (PT, PT_BYTE,
1137 PT + combined_after_bytes,
1138 PT_BYTE + combined_after_bytes);
1139 if (! EQ (current_buffer->undo_list, Qt))
1140 record_delete (PT, deletion);
1143 if (combined_before_bytes)
1145 Lisp_Object deletion;
1146 deletion = Qnil;
1148 if (! EQ (current_buffer->undo_list, Qt))
1149 deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
1150 PT, PT_BYTE, 1);
1151 adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
1152 PT, PT_BYTE);
1153 if (! EQ (current_buffer->undo_list, Qt))
1154 record_delete (PT - 1, deletion);
1157 record_insert (PT - !!combined_before_bytes,
1158 nchars - combined_before_bytes + !!combined_before_bytes);
1159 MODIFF++;
1161 bcopy (string, GPT_ADDR, nbytes);
1163 GAP_SIZE -= nbytes;
1164 /* When we have combining at the end of the insertion,
1165 this is the character position before the combined character. */
1166 GPT += nchars;
1167 ZV += nchars;
1168 Z += nchars;
1169 GPT_BYTE += nbytes;
1170 ZV_BYTE += nbytes;
1171 Z_BYTE += nbytes;
1172 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1174 if (combined_after_bytes)
1175 move_gap_both (GPT + combined_after_bytes,
1176 GPT_BYTE + combined_after_bytes);
1178 if (GPT_BYTE < GPT)
1179 abort ();
1181 adjust_overlays_for_insert (PT, nchars);
1182 adjust_markers_for_insert (PT, PT_BYTE,
1183 PT + nchars, PT_BYTE + nbytes,
1184 combined_before_bytes, combined_after_bytes,
1185 before_markers);
1187 if (BUF_INTERVALS (current_buffer) != 0)
1188 offset_intervals (current_buffer, PT, nchars);
1190 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
1191 Fset_text_properties (make_number (PT), make_number (PT + nchars),
1192 Qnil, Qnil);
1195 int pos = PT, pos_byte = PT_BYTE;
1197 adjust_point (nchars + combined_after_bytes,
1198 nbytes + combined_after_bytes);
1200 if (combined_after_bytes)
1201 combine_bytes (pos + nchars, pos_byte + nbytes, combined_after_bytes);
1203 if (combined_before_bytes)
1204 combine_bytes (pos, pos_byte, combined_before_bytes);
1207 CHECK_MARKERS ();
1210 /* Insert the part of the text of STRING, a Lisp object assumed to be
1211 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1212 starting at position POS / POS_BYTE. If the text of STRING has properties,
1213 copy them into the buffer.
1215 It does not work to use `insert' for this, because a GC could happen
1216 before we bcopy the stuff into the buffer, and relocate the string
1217 without insert noticing. */
1219 void
1220 insert_from_string (string, pos, pos_byte, length, length_byte, inherit)
1221 Lisp_Object string;
1222 register int pos, pos_byte, length, length_byte;
1223 int inherit;
1225 int opoint = PT;
1226 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1227 inherit, 0);
1228 signal_after_change (opoint, 0, PT - opoint);
1231 /* Like `insert_from_string' except that all markers pointing
1232 at the place where the insertion happens are adjusted to point after it. */
1234 void
1235 insert_from_string_before_markers (string, pos, pos_byte,
1236 length, length_byte, inherit)
1237 Lisp_Object string;
1238 register int pos, pos_byte, length, length_byte;
1239 int inherit;
1241 int opoint = PT;
1242 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1243 inherit, 1);
1244 signal_after_change (opoint, 0, PT - opoint);
1247 /* Subroutine of the insertion functions above. */
1249 static void
1250 insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
1251 inherit, before_markers)
1252 Lisp_Object string;
1253 register int pos, pos_byte, nchars, nbytes;
1254 int inherit, before_markers;
1256 struct gcpro gcpro1;
1257 int outgoing_nbytes = nbytes;
1258 int combined_before_bytes, combined_after_bytes;
1259 INTERVAL intervals;
1261 /* Make OUTGOING_NBYTES describe the text
1262 as it will be inserted in this buffer. */
1264 if (NILP (current_buffer->enable_multibyte_characters))
1265 outgoing_nbytes = nchars;
1266 else if (! STRING_MULTIBYTE (string))
1267 outgoing_nbytes
1268 = count_size_as_multibyte (&XSTRING (string)->data[pos_byte],
1269 nbytes);
1271 GCPRO1 (string);
1272 /* Do this before moving and increasing the gap,
1273 because the before-change hooks might move the gap
1274 or make it smaller. */
1275 prepare_to_modify_buffer (PT, PT, NULL);
1277 if (PT != GPT)
1278 move_gap_both (PT, PT_BYTE);
1279 if (GAP_SIZE < outgoing_nbytes)
1280 make_gap (outgoing_nbytes - GAP_SIZE);
1281 UNGCPRO;
1283 /* Copy the string text into the buffer, perhaps converting
1284 between single-byte and multibyte. */
1285 copy_text (XSTRING (string)->data + pos_byte, GPT_ADDR, nbytes,
1286 STRING_MULTIBYTE (string),
1287 ! NILP (current_buffer->enable_multibyte_characters));
1289 /* We have copied text into the gap, but we have not altered
1290 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1291 to these functions and get the same results as we would
1292 have got earlier on. Meanwhile, PT_ADDR does point to
1293 the text that has been stored by copy_text. */
1295 combined_before_bytes
1296 = count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1297 combined_after_bytes
1298 = count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1300 unsigned char save = *(GPT_ADDR);
1301 *(GPT_ADDR) = 0;
1302 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1303 *(GPT_ADDR) = save;
1306 /* Record deletion of the surrounding text that combines with
1307 the insertion. This, together with recording the insertion,
1308 will add up to the right stuff in the undo list.
1310 But there is no need to actually delete the combining bytes
1311 from the buffer and reinsert them. */
1313 if (combined_after_bytes)
1315 Lisp_Object deletion;
1316 deletion = Qnil;
1318 if (! EQ (current_buffer->undo_list, Qt))
1319 deletion = make_buffer_string_both (PT, PT_BYTE,
1320 PT + combined_after_bytes,
1321 PT_BYTE + combined_after_bytes, 1);
1323 adjust_markers_for_record_delete (PT, PT_BYTE,
1324 PT + combined_after_bytes,
1325 PT_BYTE + combined_after_bytes);
1326 if (! EQ (current_buffer->undo_list, Qt))
1327 record_delete (PT, deletion);
1330 if (combined_before_bytes)
1332 Lisp_Object deletion;
1333 deletion = Qnil;
1335 if (! EQ (current_buffer->undo_list, Qt))
1336 deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
1337 PT, PT_BYTE, 1);
1338 adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
1339 PT, PT_BYTE);
1340 if (! EQ (current_buffer->undo_list, Qt))
1341 record_delete (PT - 1, deletion);
1344 record_insert (PT - !!combined_before_bytes,
1345 nchars - combined_before_bytes + !!combined_before_bytes);
1346 MODIFF++;
1348 GAP_SIZE -= outgoing_nbytes;
1349 GPT += nchars;
1350 ZV += nchars;
1351 Z += nchars;
1352 GPT_BYTE += outgoing_nbytes;
1353 ZV_BYTE += outgoing_nbytes;
1354 Z_BYTE += outgoing_nbytes;
1355 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1357 if (combined_after_bytes)
1358 move_gap_both (GPT + combined_after_bytes,
1359 GPT_BYTE + combined_after_bytes);
1361 if (GPT_BYTE < GPT)
1362 abort ();
1364 adjust_overlays_for_insert (PT, nchars);
1365 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1366 PT_BYTE + outgoing_nbytes,
1367 combined_before_bytes, combined_after_bytes,
1368 before_markers);
1370 offset_intervals (current_buffer, PT, nchars);
1372 intervals = XSTRING (string)->intervals;
1373 /* Get the intervals for the part of the string we are inserting--
1374 not including the combined-before bytes. */
1375 if (nbytes < STRING_BYTES (XSTRING (string)))
1376 intervals = copy_intervals (intervals, pos, nchars);
1378 /* Insert those intervals. */
1379 graft_intervals_into_buffer (intervals, PT, nchars,
1380 current_buffer, inherit);
1383 int pos = PT, pos_byte = PT_BYTE;
1385 adjust_point (nchars + combined_after_bytes,
1386 outgoing_nbytes + combined_after_bytes);
1388 if (combined_after_bytes)
1389 combine_bytes (pos + nchars, pos_byte + outgoing_nbytes,
1390 combined_after_bytes);
1392 if (combined_before_bytes)
1393 combine_bytes (pos, pos_byte, combined_before_bytes);
1397 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1398 current buffer. If the text in BUF has properties, they are absorbed
1399 into the current buffer.
1401 It does not work to use `insert' for this, because a malloc could happen
1402 and relocate BUF's text before the bcopy happens. */
1404 void
1405 insert_from_buffer (buf, charpos, nchars, inherit)
1406 struct buffer *buf;
1407 int charpos, nchars;
1408 int inherit;
1410 int opoint = PT;
1412 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1413 signal_after_change (opoint, 0, PT - opoint);
1416 static void
1417 insert_from_buffer_1 (buf, from, nchars, inherit)
1418 struct buffer *buf;
1419 int from, nchars;
1420 int inherit;
1422 register Lisp_Object temp;
1423 int chunk, chunk_expanded;
1424 int from_byte = buf_charpos_to_bytepos (buf, from);
1425 int to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1426 int incoming_nbytes = to_byte - from_byte;
1427 int outgoing_nbytes = incoming_nbytes;
1428 int combined_before_bytes, combined_after_bytes;
1429 INTERVAL intervals;
1431 /* Make OUTGOING_NBYTES describe the text
1432 as it will be inserted in this buffer. */
1434 if (NILP (current_buffer->enable_multibyte_characters))
1435 outgoing_nbytes = nchars;
1436 else if (NILP (buf->enable_multibyte_characters))
1438 int outgoing_before_gap = 0;
1439 int outgoing_after_gap = 0;
1441 if (from < BUF_GPT (buf))
1443 chunk = BUF_GPT_BYTE (buf) - from_byte;
1444 if (chunk > incoming_nbytes)
1445 chunk = incoming_nbytes;
1446 outgoing_before_gap
1447 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1448 chunk);
1450 else
1451 chunk = 0;
1453 if (chunk < incoming_nbytes)
1454 outgoing_after_gap
1455 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1456 from_byte + chunk),
1457 incoming_nbytes - chunk);
1459 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1462 /* Make sure point-max won't overflow after this insertion. */
1463 XSETINT (temp, outgoing_nbytes + Z);
1464 if (outgoing_nbytes + Z != XINT (temp))
1465 error ("Maximum buffer size exceeded");
1467 /* Do this before moving and increasing the gap,
1468 because the before-change hooks might move the gap
1469 or make it smaller. */
1470 prepare_to_modify_buffer (PT, PT, NULL);
1472 if (PT != GPT)
1473 move_gap_both (PT, PT_BYTE);
1474 if (GAP_SIZE < outgoing_nbytes)
1475 make_gap (outgoing_nbytes - GAP_SIZE);
1477 if (from < BUF_GPT (buf))
1479 chunk = BUF_GPT_BYTE (buf) - from_byte;
1480 if (chunk > incoming_nbytes)
1481 chunk = incoming_nbytes;
1482 /* Record number of output bytes, so we know where
1483 to put the output from the second copy_text. */
1484 chunk_expanded
1485 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1486 GPT_ADDR, chunk,
1487 ! NILP (buf->enable_multibyte_characters),
1488 ! NILP (current_buffer->enable_multibyte_characters));
1490 else
1491 chunk_expanded = chunk = 0;
1493 if (chunk < incoming_nbytes)
1494 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1495 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1496 ! NILP (buf->enable_multibyte_characters),
1497 ! NILP (current_buffer->enable_multibyte_characters));
1499 /* We have copied text into the gap, but we have not altered
1500 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1501 to these functions and get the same results as we would
1502 have got earlier on. Meanwhile, GPT_ADDR does point to
1503 the text that has been stored by copy_text. */
1504 combined_before_bytes
1505 = count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1506 combined_after_bytes
1507 = count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE);
1509 unsigned char save = *(GPT_ADDR);
1510 *(GPT_ADDR) = 0;
1511 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1512 *(GPT_ADDR) = save;
1515 /* Record deletion of the surrounding text that combines with
1516 the insertion. This, together with recording the insertion,
1517 will add up to the right stuff in the undo list.
1519 But there is no need to actually delete the combining bytes
1520 from the buffer and reinsert them. */
1522 if (combined_after_bytes)
1524 Lisp_Object deletion;
1525 deletion = Qnil;
1527 if (! EQ (current_buffer->undo_list, Qt))
1528 deletion = make_buffer_string_both (PT, PT_BYTE,
1529 PT + combined_after_bytes,
1530 PT_BYTE + combined_after_bytes, 1);
1532 adjust_markers_for_record_delete (PT, PT_BYTE,
1533 PT + combined_after_bytes,
1534 PT_BYTE + combined_after_bytes);
1535 if (! EQ (current_buffer->undo_list, Qt))
1536 record_delete (PT, deletion);
1539 if (combined_before_bytes)
1541 Lisp_Object deletion;
1542 deletion = Qnil;
1544 if (! EQ (current_buffer->undo_list, Qt))
1545 deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
1546 PT, PT_BYTE, 1);
1547 adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
1548 PT, PT_BYTE);
1549 if (! EQ (current_buffer->undo_list, Qt))
1550 record_delete (PT - 1, deletion);
1553 record_insert (PT - !!combined_before_bytes,
1554 nchars - combined_before_bytes + !!combined_before_bytes);
1555 MODIFF++;
1557 GAP_SIZE -= outgoing_nbytes;
1558 GPT += nchars;
1559 ZV += nchars;
1560 Z += nchars;
1561 GPT_BYTE += outgoing_nbytes;
1562 ZV_BYTE += outgoing_nbytes;
1563 Z_BYTE += outgoing_nbytes;
1564 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1566 if (combined_after_bytes)
1567 move_gap_both (GPT + combined_after_bytes,
1568 GPT_BYTE + combined_after_bytes);
1570 if (GPT_BYTE < GPT)
1571 abort ();
1573 adjust_overlays_for_insert (PT, nchars);
1574 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1575 PT_BYTE + outgoing_nbytes,
1576 combined_before_bytes, combined_after_bytes, 0);
1578 if (BUF_INTERVALS (current_buffer) != 0)
1579 offset_intervals (current_buffer, PT, nchars);
1581 /* Get the intervals for the part of the string we are inserting--
1582 not including the combined-before bytes. */
1583 intervals = BUF_INTERVALS (buf);
1584 if (outgoing_nbytes < BUF_Z_BYTE (buf) - BUF_BEG_BYTE (buf))
1585 intervals = copy_intervals (intervals, from, nchars);
1587 /* Insert those intervals. */
1588 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1591 int pos = PT, pos_byte = PT_BYTE;
1593 adjust_point (nchars + combined_after_bytes,
1594 outgoing_nbytes + combined_after_bytes);
1596 if (combined_after_bytes)
1597 combine_bytes (pos + nchars, pos_byte + outgoing_nbytes,
1598 combined_after_bytes);
1600 if (combined_before_bytes)
1601 combine_bytes (pos, pos_byte, combined_before_bytes);
1605 /* This function should be called after moving gap to FROM and before
1606 altering text between FROM and TO. This adjusts various position
1607 keepers and markers as if the text is deleted. Don't forget to
1608 call adjust_after_replace after you actually alter the text. */
1610 void
1611 adjust_before_replace (from, from_byte, to, to_byte)
1612 int from, from_byte, to, to_byte;
1614 Lisp_Object deletion;
1616 if (! EQ (current_buffer->undo_list, Qt))
1617 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1619 CHECK_MARKERS ();
1621 adjust_markers_for_delete (from, from_byte, to, to_byte);
1623 if (! EQ (current_buffer->undo_list, Qt))
1624 record_delete (from, deletion);
1626 adjust_overlays_for_delete (from, to - from);
1629 /* Record undo information and adjust markers and position keepers for
1630 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1631 chars (LEN_BYTE bytes) which resides in the gap just after
1632 GPT_ADDR.
1634 PREV_TEXT nil means the new text was just inserted. */
1636 void
1637 adjust_after_replace (from, from_byte, prev_text, len, len_byte)
1638 int from, from_byte, len, len_byte;
1639 Lisp_Object prev_text;
1641 int combined_before_bytes
1642 = count_combining_before (GPT_ADDR, len_byte, from, from_byte);
1643 int combined_after_bytes
1644 = count_combining_after (GPT_ADDR, len_byte, from, from_byte);
1645 /* This flag tells if we combine some bytes with a character before
1646 FROM. This happens even if combined_before_bytes is zero. */
1647 int combine_before = (combined_before_bytes
1648 || (len == 0 && combined_after_bytes));
1650 int nchars_del = 0, nbytes_del = 0;
1652 if (STRINGP (prev_text))
1654 nchars_del = XSTRING (prev_text)->size;
1655 nbytes_del = STRING_BYTES (XSTRING (prev_text));
1658 if ((combine_before && from == BEGV)
1659 || (combined_after_bytes && from == ZV))
1661 /* We can't combine bytes nor signal an error here. So, let's
1662 pretend that the new text is just a single space. */
1663 len = len_byte = 1;
1664 combined_before_bytes = combined_after_bytes = 0;
1665 *(GPT_ADDR) = ' ';
1668 if (combined_after_bytes)
1670 Lisp_Object deletion;
1671 deletion = Qnil;
1673 if (! EQ (current_buffer->undo_list, Qt))
1674 deletion = make_buffer_string_both (from, from_byte,
1675 from + combined_after_bytes,
1676 from_byte + combined_after_bytes,
1679 adjust_markers_for_record_delete (from, from_byte,
1680 from + combined_after_bytes,
1681 from_byte + combined_after_bytes);
1683 if (! EQ (current_buffer->undo_list, Qt))
1684 record_delete (from + nchars_del, deletion);
1687 if (combined_before_bytes
1688 || (len_byte == 0 && combined_after_bytes > 0))
1690 Lisp_Object deletion;
1691 deletion = Qnil;
1693 if (! EQ (current_buffer->undo_list, Qt))
1694 deletion = make_buffer_string_both (from - 1, CHAR_TO_BYTE (from - 1),
1695 from, from_byte, 1);
1696 adjust_markers_for_record_delete (from - 1, CHAR_TO_BYTE (from - 1),
1697 from, from_byte);
1698 if (! EQ (current_buffer->undo_list, Qt))
1699 record_delete (from - 1, deletion);
1702 /* Update various buffer positions for the new text. */
1703 GAP_SIZE -= len_byte;
1704 ZV += len; Z+= len;
1705 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1706 GPT += len; GPT_BYTE += len_byte;
1707 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1709 /* The gap should be at character boundary. */
1710 if (combined_after_bytes)
1711 move_gap_both (GPT + combined_after_bytes,
1712 GPT_BYTE + combined_after_bytes);
1714 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1715 len, len_byte,
1716 combined_before_bytes, combined_after_bytes);
1717 if (! EQ (current_buffer->undo_list, Qt))
1719 if (nchars_del > 0)
1720 record_delete (from - combine_before, prev_text);
1721 if (combine_before)
1722 record_insert (from - 1, len - combined_before_bytes + 1);
1723 else
1724 record_insert (from, len);
1727 if (len > nchars_del)
1728 adjust_overlays_for_insert (from, len - nchars_del);
1729 else if (len < nchars_del)
1730 adjust_overlays_for_delete (from, nchars_del - len);
1731 if (BUF_INTERVALS (current_buffer) != 0)
1733 offset_intervals (current_buffer, from, len - nchars_del);
1737 if (from < PT)
1738 adjust_point (len - nchars_del, len_byte - nbytes_del);
1740 if (combined_after_bytes)
1742 if (combined_before_bytes == len_byte)
1743 /* This is the case that all new bytes are combined. */
1744 combined_before_bytes += combined_after_bytes;
1745 else
1746 combine_bytes (from + len, from_byte + len_byte,
1747 combined_after_bytes);
1749 if (combined_before_bytes)
1750 combine_bytes (from, from_byte, combined_before_bytes);
1753 /* As byte combining will decrease Z, we must check this again. */
1754 if (Z - GPT < END_UNCHANGED)
1755 END_UNCHANGED = Z - GPT;
1757 CHECK_MARKERS ();
1759 if (len == 0)
1760 evaporate_overlays (from);
1761 MODIFF++;
1764 /* Record undo information, adjust markers and position keepers for an
1765 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1766 text already exists in the current buffer but character length (TO
1767 - FROM) may be incorrect, the correct length is NEWLEN. */
1769 void
1770 adjust_after_insert (from, from_byte, to, to_byte, newlen)
1771 int from, from_byte, to, to_byte, newlen;
1773 int len = to - from, len_byte = to_byte - from_byte;
1775 if (GPT != to)
1776 move_gap_both (to, to_byte);
1777 GAP_SIZE += len_byte;
1778 GPT -= len; GPT_BYTE -= len_byte;
1779 ZV -= len; ZV_BYTE -= len_byte;
1780 Z -= len; Z_BYTE -= len_byte;
1781 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1784 /* Replace the text from character positions FROM to TO with NEW,
1785 If PREPARE is nonzero, call prepare_to_modify_buffer.
1786 If INHERIT, the newly inserted text should inherit text properties
1787 from the surrounding non-deleted text. */
1789 /* Note that this does not yet handle markers quite right.
1790 Also it needs to record a single undo-entry that does a replacement
1791 rather than a separate delete and insert.
1792 That way, undo will also handle markers properly.
1794 But if MARKERS is 0, don't relocate markers. */
1796 void
1797 replace_range (from, to, new, prepare, inherit, markers)
1798 Lisp_Object new;
1799 int from, to, prepare, inherit, markers;
1801 int inschars = XSTRING (new)->size;
1802 int insbytes = STRING_BYTES (XSTRING (new));
1803 int from_byte, to_byte;
1804 int nbytes_del, nchars_del;
1805 register Lisp_Object temp;
1806 struct gcpro gcpro1;
1807 int combined_before_bytes, combined_after_bytes;
1808 INTERVAL intervals;
1809 int outgoing_insbytes = insbytes;
1810 Lisp_Object deletion;
1812 CHECK_MARKERS ();
1814 GCPRO1 (new);
1816 if (prepare)
1818 int range_length = to - from;
1819 prepare_to_modify_buffer (from, to, &from);
1820 to = from + range_length;
1823 UNGCPRO;
1825 /* Make args be valid */
1826 if (from < BEGV)
1827 from = BEGV;
1828 if (to > ZV)
1829 to = ZV;
1831 from_byte = CHAR_TO_BYTE (from);
1832 to_byte = CHAR_TO_BYTE (to);
1834 nchars_del = to - from;
1835 nbytes_del = to_byte - from_byte;
1837 if (nbytes_del <= 0 && insbytes == 0)
1838 return;
1840 /* Make OUTGOING_INSBYTES describe the text
1841 as it will be inserted in this buffer. */
1843 if (NILP (current_buffer->enable_multibyte_characters))
1844 outgoing_insbytes = inschars;
1845 else if (! STRING_MULTIBYTE (new))
1846 outgoing_insbytes
1847 = count_size_as_multibyte (XSTRING (new)->data, insbytes);
1849 /* Make sure point-max won't overflow after this insertion. */
1850 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1851 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1852 error ("Maximum buffer size exceeded");
1854 GCPRO1 (new);
1856 /* Make sure the gap is somewhere in or next to what we are deleting. */
1857 if (from > GPT)
1858 gap_right (from, from_byte);
1859 if (to < GPT)
1860 gap_left (to, to_byte, 0);
1862 /* Even if we don't record for undo, we must keep the original text
1863 because we may have to recover it because of inappropriate byte
1864 combining. */
1865 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1867 if (markers)
1868 /* Relocate all markers pointing into the new, larger gap
1869 to point at the end of the text before the gap.
1870 Do this before recording the deletion,
1871 so that undo handles this after reinserting the text. */
1872 adjust_markers_for_delete (from, from_byte, to, to_byte);
1874 GAP_SIZE += nbytes_del;
1875 ZV -= nchars_del;
1876 Z -= nchars_del;
1877 ZV_BYTE -= nbytes_del;
1878 Z_BYTE -= nbytes_del;
1879 GPT = from;
1880 GPT_BYTE = from_byte;
1881 *(GPT_ADDR) = 0; /* Put an anchor. */
1883 if (GPT_BYTE < GPT)
1884 abort ();
1886 if (GPT - BEG < BEG_UNCHANGED)
1887 BEG_UNCHANGED = GPT - BEG;
1888 if (Z - GPT < END_UNCHANGED)
1889 END_UNCHANGED = Z - GPT;
1891 if (GAP_SIZE < insbytes)
1892 make_gap (insbytes - GAP_SIZE);
1894 /* Copy the string text into the buffer, perhaps converting
1895 between single-byte and multibyte. */
1896 copy_text (XSTRING (new)->data, GPT_ADDR, insbytes,
1897 STRING_MULTIBYTE (new),
1898 ! NILP (current_buffer->enable_multibyte_characters));
1900 /* We have copied text into the gap, but we have not marked
1901 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1902 here, for both the previous text and the following text.
1903 Meanwhile, GPT_ADDR does point to
1904 the text that has been stored by copy_text. */
1906 combined_before_bytes
1907 = count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte);
1908 combined_after_bytes
1909 = count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte);
1911 if ((combined_before_bytes && from == BEGV)
1912 || (combined_after_bytes && from == ZV))
1914 /* Bytes are being combined across the region boundary. We
1915 should avoid it. We recover the original contents before
1916 signaling an error. */
1917 bcopy (XSTRING (deletion)->data, GPT_ADDR, nbytes_del);
1918 GAP_SIZE -= nbytes_del;
1919 ZV += nchars_del;
1920 Z += nchars_del;
1921 ZV_BYTE += nbytes_del;
1922 Z_BYTE += nbytes_del;
1923 GPT = from + nchars_del;
1924 GPT_BYTE = from_byte + nbytes_del;
1925 *(GPT_ADDR) = 0; /* Put an anchor. */
1926 if (markers)
1927 adjust_markers_for_insert (from, from_byte, to, to_byte, 0, 0, 0);
1928 UNGCPRO;
1929 byte_combining_error ();
1930 GCPRO1 (new);
1933 /* Record deletion of the surrounding text that combines with
1934 the insertion. This, together with recording the insertion,
1935 will add up to the right stuff in the undo list.
1937 But there is no need to actually delete the combining bytes
1938 from the buffer and reinsert them. */
1940 if (combined_after_bytes)
1942 Lisp_Object deletion;
1943 deletion = Qnil;
1945 if (! EQ (current_buffer->undo_list, Qt))
1946 deletion = make_buffer_string_both (from, from_byte,
1947 from + combined_after_bytes,
1948 from_byte + combined_after_bytes,
1951 adjust_markers_for_record_delete (from, from_byte,
1952 from + combined_after_bytes,
1953 from_byte + combined_after_bytes);
1954 if (! EQ (current_buffer->undo_list, Qt))
1955 record_delete (from + nchars_del, deletion);
1958 if (combined_before_bytes)
1960 Lisp_Object deletion;
1961 deletion = Qnil;
1963 if (! EQ (current_buffer->undo_list, Qt))
1964 deletion = make_buffer_string_both (from - 1, CHAR_TO_BYTE (from - 1),
1965 from, from_byte, 1);
1966 adjust_markers_for_record_delete (from - 1, CHAR_TO_BYTE (from - 1),
1967 from, from_byte);
1968 if (! EQ (current_buffer->undo_list, Qt))
1969 record_delete (from - 1, deletion);
1972 if (! EQ (current_buffer->undo_list, Qt))
1974 record_delete (from - !!combined_before_bytes, deletion);
1975 record_insert (from - !!combined_before_bytes,
1976 (inschars - combined_before_bytes
1977 + !!combined_before_bytes));
1980 GAP_SIZE -= outgoing_insbytes;
1981 GPT += inschars;
1982 ZV += inschars;
1983 Z += inschars;
1984 GPT_BYTE += outgoing_insbytes;
1985 ZV_BYTE += outgoing_insbytes;
1986 Z_BYTE += outgoing_insbytes;
1987 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1989 if (combined_after_bytes)
1990 move_gap_both (GPT + combined_after_bytes,
1991 GPT_BYTE + combined_after_bytes);
1993 if (GPT_BYTE < GPT)
1994 abort ();
1996 /* Adjust the overlay center as needed. This must be done after
1997 adjusting the markers that bound the overlays. */
1998 adjust_overlays_for_delete (from, nchars_del);
1999 adjust_overlays_for_insert (from, inschars);
2000 if (markers)
2001 adjust_markers_for_insert (from, from_byte,
2002 from + inschars, from_byte + outgoing_insbytes,
2003 combined_before_bytes, combined_after_bytes, 0);
2005 offset_intervals (current_buffer, from, inschars - nchars_del);
2007 /* Get the intervals for the part of the string we are inserting--
2008 not including the combined-before bytes. */
2009 intervals = XSTRING (new)->intervals;
2010 /* Insert those intervals. */
2011 graft_intervals_into_buffer (intervals, from, inschars,
2012 current_buffer, inherit);
2014 /* Relocate point as if it were a marker. */
2015 if (from < PT)
2016 adjust_point ((from + inschars - (PT < to ? PT : to)),
2017 (from_byte + outgoing_insbytes
2018 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
2020 if (combined_after_bytes)
2022 if (combined_before_bytes == outgoing_insbytes)
2023 /* This is the case that all new bytes are combined. */
2024 combined_before_bytes += combined_after_bytes;
2025 else
2026 combine_bytes (from + inschars, from_byte + outgoing_insbytes,
2027 combined_after_bytes);
2029 if (combined_before_bytes)
2030 combine_bytes (from, from_byte, combined_before_bytes);
2032 /* As byte combining will decrease Z, we must check this again. */
2033 if (Z - GPT < END_UNCHANGED)
2034 END_UNCHANGED = Z - GPT;
2036 if (outgoing_insbytes == 0)
2037 evaporate_overlays (from);
2039 CHECK_MARKERS ();
2041 MODIFF++;
2042 UNGCPRO;
2044 signal_after_change (from, nchars_del, GPT - from);
2047 /* Delete characters in current buffer
2048 from FROM up to (but not including) TO.
2049 If TO comes before FROM, we delete nothing. */
2051 void
2052 del_range (from, to)
2053 register int from, to;
2055 del_range_1 (from, to, 1);
2058 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
2060 void
2061 del_range_1 (from, to, prepare)
2062 int from, to, prepare;
2064 int from_byte, to_byte;
2066 /* Make args be valid */
2067 if (from < BEGV)
2068 from = BEGV;
2069 if (to > ZV)
2070 to = ZV;
2072 if (to <= from)
2073 return;
2075 if (prepare)
2077 int range_length = to - from;
2078 prepare_to_modify_buffer (from, to, &from);
2079 to = from + range_length;
2082 from_byte = CHAR_TO_BYTE (from);
2083 to_byte = CHAR_TO_BYTE (to);
2085 del_range_2 (from, from_byte, to, to_byte);
2086 signal_after_change (from, to - from, 0);
2089 /* Like del_range_1 but args are byte positions, not char positions. */
2091 void
2092 del_range_byte (from_byte, to_byte, prepare)
2093 int from_byte, to_byte, prepare;
2095 int from, to;
2097 /* Make args be valid */
2098 if (from_byte < BEGV_BYTE)
2099 from_byte = BEGV_BYTE;
2100 if (to_byte > ZV_BYTE)
2101 to_byte = ZV_BYTE;
2103 if (to_byte <= from_byte)
2104 return;
2106 from = BYTE_TO_CHAR (from_byte);
2107 to = BYTE_TO_CHAR (to_byte);
2109 if (prepare)
2111 int old_from = from, old_to = Z - to;
2112 int range_length = to - from;
2113 prepare_to_modify_buffer (from, to, &from);
2114 to = from + range_length;
2116 if (old_from != from)
2117 from_byte = CHAR_TO_BYTE (from);
2118 if (old_to == Z - to)
2119 to_byte = CHAR_TO_BYTE (to);
2122 del_range_2 (from, from_byte, to, to_byte);
2123 signal_after_change (from, to - from, 0);
2126 /* Like del_range_1, but positions are specified both as charpos
2127 and bytepos. */
2129 void
2130 del_range_both (from, from_byte, to, to_byte, prepare)
2131 int from, from_byte, to, to_byte, prepare;
2133 /* Make args be valid */
2134 if (from_byte < BEGV_BYTE)
2135 from_byte = BEGV_BYTE;
2136 if (to_byte > ZV_BYTE)
2137 to_byte = ZV_BYTE;
2139 if (to_byte <= from_byte)
2140 return;
2142 if (from < BEGV)
2143 from = BEGV;
2144 if (to > ZV)
2145 to = ZV;
2147 if (prepare)
2149 int old_from = from, old_to = Z - to;
2150 int range_length = to - from;
2151 prepare_to_modify_buffer (from, to, &from);
2152 to = from + range_length;
2154 if (old_from != from)
2155 from_byte = CHAR_TO_BYTE (from);
2156 if (old_to == Z - to)
2157 to_byte = CHAR_TO_BYTE (to);
2160 del_range_2 (from, from_byte, to, to_byte);
2161 signal_after_change (from, to - from, 0);
2164 /* Delete a range of text, specified both as character positions
2165 and byte positions. FROM and TO are character positions,
2166 while FROM_BYTE and TO_BYTE are byte positions. */
2168 void
2169 del_range_2 (from, from_byte, to, to_byte)
2170 int from, from_byte, to, to_byte;
2172 register int nbytes_del, nchars_del;
2173 int combined_after_bytes;
2174 Lisp_Object deletion;
2175 int from_byte_1;
2177 CHECK_MARKERS ();
2179 nchars_del = to - from;
2180 nbytes_del = to_byte - from_byte;
2182 /* Make sure the gap is somewhere in or next to what we are deleting. */
2183 if (from > GPT)
2184 gap_right (from, from_byte);
2185 if (to < GPT)
2186 gap_left (to, to_byte, 0);
2188 combined_after_bytes
2189 = count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
2190 Z_BYTE - to_byte, from, from_byte);
2191 if (combined_after_bytes)
2193 if (from == BEGV || to == ZV)
2194 byte_combining_error ();
2195 from_byte_1 = from_byte;
2196 DEC_POS (from_byte_1);
2198 else
2199 from_byte_1 = from_byte;
2201 if (! EQ (current_buffer->undo_list, Qt))
2202 deletion
2203 = make_buffer_string_both (from - !!combined_after_bytes,
2204 from_byte_1,
2205 to + combined_after_bytes,
2206 to_byte + combined_after_bytes, 1);
2207 if (combined_after_bytes)
2208 /* COMBINED_AFTER_BYTES nonzero means that the above code moved
2209 the gap. We must move the gap again to a proper place. */
2210 move_gap_both (from, from_byte);
2212 /* Relocate all markers pointing into the new, larger gap
2213 to point at the end of the text before the gap.
2214 Do this before recording the deletion,
2215 so that undo handles this after reinserting the text. */
2216 adjust_markers_for_delete (from, from_byte, to, to_byte);
2217 if (combined_after_bytes)
2219 /* Adjust markers for the phony deletion
2220 that we are about to call record_undo for. */
2222 /* Here we delete the markers that formerly
2223 pointed at TO ... TO + COMBINED_AFTER_BYTES.
2224 But because of the call to adjust_markers_for_delete, above,
2225 they now point at FROM ... FROM + COMBINED_AFTER_BYTES. */
2226 adjust_markers_for_record_delete (from, from_byte,
2227 from + combined_after_bytes,
2228 from_byte + combined_after_bytes);
2230 adjust_markers_for_record_delete (from - 1, from_byte_1,
2231 from, from_byte);
2233 if (! EQ (current_buffer->undo_list, Qt))
2234 record_delete (from - !!combined_after_bytes, deletion);
2235 MODIFF++;
2237 /* Relocate point as if it were a marker. */
2238 if (from < PT)
2239 adjust_point (from - (PT < to ? PT : to),
2240 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
2242 offset_intervals (current_buffer, from, - nchars_del);
2244 /* Adjust the overlay center as needed. This must be done after
2245 adjusting the markers that bound the overlays. */
2246 adjust_overlays_for_delete (from, nchars_del);
2248 GAP_SIZE += nbytes_del;
2249 ZV_BYTE -= nbytes_del;
2250 Z_BYTE -= nbytes_del;
2251 ZV -= nchars_del;
2252 Z -= nchars_del;
2253 GPT = from;
2254 GPT_BYTE = from_byte;
2256 if (combined_after_bytes)
2257 move_gap_both (GPT + combined_after_bytes,
2258 GPT_BYTE + combined_after_bytes);
2260 *(GPT_ADDR) = 0; /* Put an anchor. */
2262 if (GPT_BYTE < GPT)
2263 abort ();
2265 if (GPT - BEG < BEG_UNCHANGED)
2266 BEG_UNCHANGED = GPT - BEG;
2267 if (Z - GPT < END_UNCHANGED)
2268 END_UNCHANGED = Z - GPT;
2270 if (combined_after_bytes)
2272 /* Adjust markers for byte combining. As we have already
2273 adjuted markers without concerning byte combining, here we
2274 must concern only byte combining. */
2275 adjust_markers_for_replace (from, from_byte, 0, 0, 0, 0,
2276 0, combined_after_bytes);
2277 combine_bytes (from, from_byte, combined_after_bytes);
2279 record_insert (GPT - 1, 1);
2281 if (Z - GPT < END_UNCHANGED)
2282 END_UNCHANGED = Z - GPT;
2285 CHECK_MARKERS ();
2287 evaporate_overlays (from);
2290 /* Call this if you're about to change the region of BUFFER from
2291 character positions START to END. This checks the read-only
2292 properties of the region, calls the necessary modification hooks,
2293 and warns the next redisplay that it should pay attention to that
2294 area. */
2296 void
2297 modify_region (buffer, start, end)
2298 struct buffer *buffer;
2299 int start, end;
2301 struct buffer *old_buffer = current_buffer;
2303 if (buffer != old_buffer)
2304 set_buffer_internal (buffer);
2306 prepare_to_modify_buffer (start, end, NULL);
2308 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
2310 if (MODIFF <= SAVE_MODIFF)
2311 record_first_change ();
2312 MODIFF++;
2314 buffer->point_before_scroll = Qnil;
2316 if (buffer != old_buffer)
2317 set_buffer_internal (old_buffer);
2320 /* Check that it is okay to modify the buffer between START and END,
2321 which are char positions.
2323 Run the before-change-function, if any. If intervals are in use,
2324 verify that the text to be modified is not read-only, and call
2325 any modification properties the text may have.
2327 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2328 by holding its value temporarily in a marker. */
2330 void
2331 prepare_to_modify_buffer (start, end, preserve_ptr)
2332 int start, end;
2333 int *preserve_ptr;
2335 if (!NILP (current_buffer->read_only))
2336 Fbarf_if_buffer_read_only ();
2338 /* Let redisplay consider other windows than selected_window
2339 if modifying another buffer. */
2340 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2341 ++windows_or_buffers_changed;
2343 if (BUF_INTERVALS (current_buffer) != 0)
2345 if (preserve_ptr)
2347 Lisp_Object preserve_marker;
2348 struct gcpro gcpro1;
2349 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
2350 GCPRO1 (preserve_marker);
2351 verify_interval_modification (current_buffer, start, end);
2352 *preserve_ptr = marker_position (preserve_marker);
2353 unchain_marker (preserve_marker);
2354 UNGCPRO;
2356 else
2357 verify_interval_modification (current_buffer, start, end);
2360 #ifdef CLASH_DETECTION
2361 if (!NILP (current_buffer->file_truename)
2362 /* Make binding buffer-file-name to nil effective. */
2363 && !NILP (current_buffer->filename)
2364 && SAVE_MODIFF >= MODIFF)
2365 lock_file (current_buffer->file_truename);
2366 #else
2367 /* At least warn if this file has changed on disk since it was visited. */
2368 if (!NILP (current_buffer->filename)
2369 && SAVE_MODIFF >= MODIFF
2370 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2371 && !NILP (Ffile_exists_p (current_buffer->filename)))
2372 call1 (intern ("ask-user-about-supersession-threat"),
2373 current_buffer->filename);
2374 #endif /* not CLASH_DETECTION */
2376 signal_before_change (start, end, preserve_ptr);
2378 if (current_buffer->newline_cache)
2379 invalidate_region_cache (current_buffer,
2380 current_buffer->newline_cache,
2381 start - BEG, Z - end);
2382 if (current_buffer->width_run_cache)
2383 invalidate_region_cache (current_buffer,
2384 current_buffer->width_run_cache,
2385 start - BEG, Z - end);
2387 Vdeactivate_mark = Qt;
2390 /* These macros work with an argument named `preserve_ptr'
2391 and a local variable named `preserve_marker'. */
2393 #define PRESERVE_VALUE \
2394 if (preserve_ptr && NILP (preserve_marker)) \
2395 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2397 #define RESTORE_VALUE \
2398 if (! NILP (preserve_marker)) \
2400 *preserve_ptr = marker_position (preserve_marker); \
2401 unchain_marker (preserve_marker); \
2404 #define PRESERVE_START_END \
2405 if (NILP (start_marker)) \
2406 start_marker = Fcopy_marker (start, Qnil); \
2407 if (NILP (end_marker)) \
2408 end_marker = Fcopy_marker (end, Qnil);
2410 #define FETCH_START \
2411 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2413 #define FETCH_END \
2414 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2416 /* Signal a change to the buffer immediately before it happens.
2417 START_INT and END_INT are the bounds of the text to be changed.
2419 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2420 by holding its value temporarily in a marker. */
2422 void
2423 signal_before_change (start_int, end_int, preserve_ptr)
2424 int start_int, end_int;
2425 int *preserve_ptr;
2427 Lisp_Object start, end;
2428 Lisp_Object start_marker, end_marker;
2429 Lisp_Object preserve_marker;
2430 struct gcpro gcpro1, gcpro2, gcpro3;
2432 if (inhibit_modification_hooks)
2433 return;
2435 start = make_number (start_int);
2436 end = make_number (end_int);
2437 preserve_marker = Qnil;
2438 start_marker = Qnil;
2439 end_marker = Qnil;
2440 GCPRO3 (preserve_marker, start_marker, end_marker);
2442 /* If buffer is unmodified, run a special hook for that case. */
2443 if (SAVE_MODIFF >= MODIFF
2444 && !NILP (Vfirst_change_hook)
2445 && !NILP (Vrun_hooks))
2447 PRESERVE_VALUE;
2448 PRESERVE_START_END;
2449 call1 (Vrun_hooks, Qfirst_change_hook);
2452 /* Run the before-change-function if any.
2453 We don't bother "binding" this variable to nil
2454 because it is obsolete anyway and new code should not use it. */
2455 if (!NILP (Vbefore_change_function))
2457 PRESERVE_VALUE;
2458 PRESERVE_START_END;
2459 call2 (Vbefore_change_function, FETCH_START, FETCH_END);
2462 /* Now run the before-change-functions if any. */
2463 if (!NILP (Vbefore_change_functions))
2465 Lisp_Object args[3];
2466 Lisp_Object before_change_functions;
2467 Lisp_Object after_change_functions;
2468 struct gcpro gcpro1, gcpro2;
2470 PRESERVE_VALUE;
2471 PRESERVE_START_END;
2473 /* "Bind" before-change-functions and after-change-functions
2474 to nil--but in a way that errors don't know about.
2475 That way, if there's an error in them, they will stay nil. */
2476 before_change_functions = Vbefore_change_functions;
2477 after_change_functions = Vafter_change_functions;
2478 Vbefore_change_functions = Qnil;
2479 Vafter_change_functions = Qnil;
2480 GCPRO2 (before_change_functions, after_change_functions);
2482 /* Actually run the hook functions. */
2483 args[0] = Qbefore_change_functions;
2484 args[1] = FETCH_START;
2485 args[2] = FETCH_END;
2486 run_hook_list_with_args (before_change_functions, 3, args);
2488 /* "Unbind" the variables we "bound" to nil. */
2489 Vbefore_change_functions = before_change_functions;
2490 Vafter_change_functions = after_change_functions;
2491 UNGCPRO;
2494 if (!NILP (current_buffer->overlays_before)
2495 || !NILP (current_buffer->overlays_after))
2497 PRESERVE_VALUE;
2498 report_overlay_modification (FETCH_START, FETCH_END, 0,
2499 FETCH_START, FETCH_END, Qnil);
2502 if (! NILP (start_marker))
2503 free_marker (start_marker);
2504 if (! NILP (end_marker))
2505 free_marker (end_marker);
2506 RESTORE_VALUE;
2507 UNGCPRO;
2510 /* Signal a change immediately after it happens.
2511 CHARPOS is the character position of the start of the changed text.
2512 LENDEL is the number of characters of the text before the change.
2513 (Not the whole buffer; just the part that was changed.)
2514 LENINS is the number of characters in that part of the text
2515 after the change. */
2517 void
2518 signal_after_change (charpos, lendel, lenins)
2519 int charpos, lendel, lenins;
2521 if (inhibit_modification_hooks)
2522 return;
2524 /* If we are deferring calls to the after-change functions
2525 and there are no before-change functions,
2526 just record the args that we were going to use. */
2527 if (! NILP (Vcombine_after_change_calls)
2528 && NILP (Vbefore_change_function) && NILP (Vbefore_change_functions)
2529 && NILP (current_buffer->overlays_before)
2530 && NILP (current_buffer->overlays_after))
2532 Lisp_Object elt;
2534 if (!NILP (combine_after_change_list)
2535 && current_buffer != XBUFFER (combine_after_change_buffer))
2536 Fcombine_after_change_execute ();
2538 elt = Fcons (make_number (charpos - BEG),
2539 Fcons (make_number (Z - (charpos - lendel + lenins)),
2540 Fcons (make_number (lenins - lendel), Qnil)));
2541 combine_after_change_list
2542 = Fcons (elt, combine_after_change_list);
2543 combine_after_change_buffer = Fcurrent_buffer ();
2545 return;
2548 if (!NILP (combine_after_change_list))
2549 Fcombine_after_change_execute ();
2551 /* Run the after-change-function if any.
2552 We don't bother "binding" this variable to nil
2553 because it is obsolete anyway and new code should not use it. */
2554 if (!NILP (Vafter_change_function))
2555 call3 (Vafter_change_function,
2556 make_number (charpos), make_number (charpos + lenins),
2557 make_number (lendel));
2559 if (!NILP (Vafter_change_functions))
2561 Lisp_Object args[4];
2562 Lisp_Object before_change_functions;
2563 Lisp_Object after_change_functions;
2564 struct gcpro gcpro1, gcpro2;
2566 /* "Bind" before-change-functions and after-change-functions
2567 to nil--but in a way that errors don't know about.
2568 That way, if there's an error in them, they will stay nil. */
2569 before_change_functions = Vbefore_change_functions;
2570 after_change_functions = Vafter_change_functions;
2571 Vbefore_change_functions = Qnil;
2572 Vafter_change_functions = Qnil;
2573 GCPRO2 (before_change_functions, after_change_functions);
2575 /* Actually run the hook functions. */
2576 args[0] = Qafter_change_functions;
2577 XSETFASTINT (args[1], charpos);
2578 XSETFASTINT (args[2], charpos + lenins);
2579 XSETFASTINT (args[3], lendel);
2580 run_hook_list_with_args (after_change_functions,
2581 4, args);
2583 /* "Unbind" the variables we "bound" to nil. */
2584 Vbefore_change_functions = before_change_functions;
2585 Vafter_change_functions = after_change_functions;
2586 UNGCPRO;
2589 if (!NILP (current_buffer->overlays_before)
2590 || !NILP (current_buffer->overlays_after))
2591 report_overlay_modification (make_number (charpos),
2592 make_number (charpos + lenins),
2594 make_number (charpos),
2595 make_number (charpos + lenins),
2596 make_number (lendel));
2598 /* After an insertion, call the text properties
2599 insert-behind-hooks or insert-in-front-hooks. */
2600 if (lendel == 0)
2601 report_interval_modification (make_number (charpos),
2602 make_number (charpos + lenins));
2605 Lisp_Object
2606 Fcombine_after_change_execute_1 (val)
2607 Lisp_Object val;
2609 Vcombine_after_change_calls = val;
2610 return val;
2613 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2614 Scombine_after_change_execute, 0, 0, 0,
2615 "This function is for use internally in `combine-after-change-calls'.")
2618 int count = specpdl_ptr - specpdl;
2619 int beg, end, change;
2620 int begpos, endpos;
2621 Lisp_Object tail;
2623 if (NILP (combine_after_change_list))
2624 return Qnil;
2626 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2628 Fset_buffer (combine_after_change_buffer);
2630 /* # chars unchanged at beginning of buffer. */
2631 beg = Z - BEG;
2632 /* # chars unchanged at end of buffer. */
2633 end = beg;
2634 /* Total amount of insertion (negative for deletion). */
2635 change = 0;
2637 /* Scan the various individual changes,
2638 accumulating the range info in BEG, END and CHANGE. */
2639 for (tail = combine_after_change_list; CONSP (tail);
2640 tail = XCDR (tail))
2642 Lisp_Object elt;
2643 int thisbeg, thisend, thischange;
2645 /* Extract the info from the next element. */
2646 elt = XCAR (tail);
2647 if (! CONSP (elt))
2648 continue;
2649 thisbeg = XINT (XCAR (elt));
2651 elt = XCDR (elt);
2652 if (! CONSP (elt))
2653 continue;
2654 thisend = XINT (XCAR (elt));
2656 elt = XCDR (elt);
2657 if (! CONSP (elt))
2658 continue;
2659 thischange = XINT (XCAR (elt));
2661 /* Merge this range into the accumulated range. */
2662 change += thischange;
2663 if (thisbeg < beg)
2664 beg = thisbeg;
2665 if (thisend < end)
2666 end = thisend;
2669 /* Get the current start and end positions of the range
2670 that was changed. */
2671 begpos = BEG + beg;
2672 endpos = Z - end;
2674 /* We are about to handle these, so discard them. */
2675 combine_after_change_list = Qnil;
2677 /* Now run the after-change functions for real.
2678 Turn off the flag that defers them. */
2679 record_unwind_protect (Fcombine_after_change_execute_1,
2680 Vcombine_after_change_calls);
2681 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2683 return unbind_to (count, Qnil);
2686 void
2687 syms_of_insdel ()
2689 staticpro (&combine_after_change_list);
2690 combine_after_change_list = Qnil;
2691 combine_after_change_buffer = Qnil;
2693 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
2694 "Non-nil means enable debugging checks for invalid marker positions.");
2695 check_markers_debug_flag = 0;
2696 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2697 "Used internally by the `combine-after-change-calls' macro.");
2698 Vcombine_after_change_calls = Qnil;
2700 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks,
2701 "Non-nil means don't run any of the hooks that respond to buffer changes.\n\
2702 This affects `before-change-functions' and `after-change-functions',\n\
2703 as well as hooks attached to text properties and overlays.");
2704 inhibit_modification_hooks = 0;
2706 defsubr (&Scombine_after_change_execute);