Remove USE_TEXT_PROPERTIES.
[bpt/emacs.git] / src / insdel.c
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 86,93,94,95,97,98, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <config.h>
23 #include "lisp.h"
24 #include "intervals.h"
25 #include "buffer.h"
26 #include "charset.h"
27 #include "window.h"
28 #include "blockinput.h"
29 #include "region-cache.h"
30
31 #ifndef NULL
32 #define NULL 0
33 #endif
34
35 #define min(x, y) ((x) < (y) ? (x) : (y))
36 #define max(x, y) ((x) > (y) ? (x) : (y))
37
38 static void insert_from_string_1 P_ ((Lisp_Object, int, int, int, int, int, int));
39 static void insert_from_buffer_1 ();
40 static void gap_left P_ ((int, int, int));
41 static void gap_right P_ ((int, int));
42 static void adjust_markers_gap_motion P_ ((int, int, int));
43 static void adjust_markers_for_insert P_ ((int, int, int, int, int, int, int));
44 static void adjust_markers_for_delete P_ ((int, int, int, int));
45 static void adjust_markers_for_record_delete P_ ((int, int, int, int));
46 static void adjust_point P_ ((int, int));
47
48 Lisp_Object Fcombine_after_change_execute ();
49
50 /* Non-nil means don't call the after-change-functions right away,
51 just record an element in Vcombine_after_change_calls_list. */
52 Lisp_Object Vcombine_after_change_calls;
53
54 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
55 describing changes which happened while combine_after_change_calls
56 was nonzero. We use this to decide how to call them
57 once the deferral ends.
58
59 In each element.
60 BEG-UNCHANGED is the number of chars before the changed range.
61 END-UNCHANGED is the number of chars after the changed range,
62 and CHANGE-AMOUNT is the number of characters inserted by the change
63 (negative for a deletion). */
64 Lisp_Object combine_after_change_list;
65
66 /* Buffer which combine_after_change_list is about. */
67 Lisp_Object combine_after_change_buffer;
68 \f
69 /* Check all markers in the current buffer, looking for something invalid. */
70
71 static int check_markers_debug_flag;
72
73 #define CHECK_MARKERS() \
74 if (check_markers_debug_flag) \
75 check_markers (); \
76 else
77
78 void
79 check_markers ()
80 {
81 register Lisp_Object tail;
82 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
83
84 tail = BUF_MARKERS (current_buffer);
85
86 while (XSYMBOL (tail) != XSYMBOL (Qnil))
87 {
88 if (XMARKER (tail)->buffer->text != current_buffer->text)
89 abort ();
90 if (XMARKER (tail)->charpos > Z)
91 abort ();
92 if (XMARKER (tail)->bytepos > Z_BYTE)
93 abort ();
94 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (XMARKER (tail)->bytepos)))
95 abort ();
96
97 tail = XMARKER (tail)->chain;
98 }
99 }
100 \f
101 /* Move gap to position CHARPOS.
102 Note that this can quit! */
103
104 void
105 move_gap (charpos)
106 int charpos;
107 {
108 move_gap_both (charpos, charpos_to_bytepos (charpos));
109 }
110
111 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
112 Note that this can quit! */
113
114 void
115 move_gap_both (charpos, bytepos)
116 int charpos, bytepos;
117 {
118 if (bytepos < GPT_BYTE)
119 gap_left (charpos, bytepos, 0);
120 else if (bytepos > GPT_BYTE)
121 gap_right (charpos, bytepos);
122 }
123
124 /* Move the gap to a position less than the current GPT.
125 BYTEPOS describes the new position as a byte position,
126 and CHARPOS is the corresponding char position.
127 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
128
129 static void
130 gap_left (charpos, bytepos, newgap)
131 register int charpos, bytepos;
132 int newgap;
133 {
134 register unsigned char *to, *from;
135 register int i;
136 int new_s1;
137
138 if (!newgap)
139 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
140
141 i = GPT_BYTE;
142 to = GAP_END_ADDR;
143 from = GPT_ADDR;
144 new_s1 = GPT_BYTE;
145
146 /* Now copy the characters. To move the gap down,
147 copy characters up. */
148
149 while (1)
150 {
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)
158 {
159 bytepos = new_s1;
160 charpos = BYTE_TO_CHAR (bytepos);
161 break;
162 }
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))
172 {
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);
180 }
181 else
182 #endif
183 {
184 new_s1 -= i;
185 while (--i >= 0)
186 *--to = *--from;
187 }
188 }
189
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;
200 }
201
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. */
205
206 static void
207 gap_right (charpos, bytepos)
208 register int charpos, bytepos;
209 {
210 register unsigned char *to, *from;
211 register int i;
212 int new_s1;
213
214 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
215
216 i = GPT_BYTE;
217 from = GAP_END_ADDR;
218 to = GPT_ADDR;
219 new_s1 = GPT_BYTE;
220
221 /* Now copy the characters. To move the gap up,
222 copy characters down. */
223
224 while (1)
225 {
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)
233 {
234 bytepos = new_s1;
235 charpos = BYTE_TO_CHAR (bytepos);
236 break;
237 }
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))
247 {
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;
255 }
256 else
257 #endif
258 {
259 new_s1 += i;
260 while (--i >= 0)
261 *to++ = *from++;
262 }
263 }
264
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;
273 }
274 \f
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).
277
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.
281
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.)
285
286 The markers' character positions are not altered,
287 because gap motion does not affect character positions. */
288
289 int adjust_markers_test;
290
291 static void
292 adjust_markers_gap_motion (from, to, amount)
293 register int from, to, amount;
294 {
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;
301
302 marker = BUF_MARKERS (current_buffer);
303
304 while (!NILP (marker))
305 {
306 m = XMARKER (marker);
307 mpos = m->bytepos;
308 if (amount > 0)
309 {
310 if (mpos > to && mpos < to + amount)
311 {
312 if (adjust_markers_test)
313 abort ();
314 mpos = to + amount;
315 }
316 }
317 else
318 {
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)
323 {
324 if (adjust_markers_test)
325 abort ();
326 mpos = from + amount;
327 }
328 }
329 if (mpos > from && mpos <= to)
330 mpos += amount;
331 m->bufpos = mpos;
332 marker = m->chain;
333 }
334 #endif
335 }
336 \f
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.
340
341 This function assumes that the gap is adjacent to
342 or inside of the range being deleted. */
343
344 static void
345 adjust_markers_for_delete (from, from_byte, to, to_byte)
346 register int from, from_byte, to, to_byte;
347 {
348 Lisp_Object marker;
349 register struct Lisp_Marker *m;
350 register int charpos;
351
352 marker = BUF_MARKERS (current_buffer);
353
354 while (!NILP (marker))
355 {
356 m = XMARKER (marker);
357 charpos = m->charpos;
358
359 if (charpos > Z)
360 abort ();
361
362 /* If the marker is after the deletion,
363 relocate by number of chars / bytes deleted. */
364 if (charpos > to)
365 {
366 m->charpos -= to - from;
367 m->bytepos -= to_byte - from_byte;
368 }
369
370 /* Here's the case where a marker is inside text being deleted. */
371 else if (charpos > from)
372 {
373 record_marker_adjustment (marker, from - charpos);
374 m->charpos = from;
375 m->bytepos = from_byte;
376 }
377
378 marker = m->chain;
379 }
380 }
381
382 \f
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. */
386
387 static void
388 adjust_markers_for_record_delete (from, from_byte, to, to_byte)
389 register int from, from_byte, to, to_byte;
390 {
391 Lisp_Object marker;
392 register struct Lisp_Marker *m;
393 register int charpos;
394
395 marker = BUF_MARKERS (current_buffer);
396
397 while (!NILP (marker))
398 {
399 m = XMARKER (marker);
400 charpos = m->charpos;
401
402 /* If the marker is after the deletion,
403 relocate by number of chars / bytes deleted. */
404 if (charpos > to)
405 ;
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);
409
410 marker = m->chain;
411 }
412 }
413 \f
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).
417
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.
422
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. */
426
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;
433 {
434 Lisp_Object marker;
435 int adjusted = 0;
436 int nchars = to - from;
437 int nbytes = to_byte - from_byte;
438
439 marker = BUF_MARKERS (current_buffer);
440
441 while (!NILP (marker))
442 {
443 register struct Lisp_Marker *m = XMARKER (marker);
444
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)
450 {
451 if (m->charpos != m->bytepos)
452 abort ();
453 }
454
455 if (m->bytepos == from_byte)
456 {
457 if (m->insertion_type || before_markers)
458 {
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;
467 }
468 else if (combined_before_bytes)
469 {
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;
479 }
480 }
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)
486 {
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);
493 }
494 else if (m->bytepos > from_byte)
495 {
496 m->bytepos += nbytes;
497 m->charpos += nchars - combined_after_bytes - combined_before_bytes;
498 }
499
500 marker = m->chain;
501 }
502
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);
507 }
508
509 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
510
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. */
518
519 static void
520 adjust_point (nchars, nbytes)
521 int nchars, nbytes;
522 {
523 BUF_PT (current_buffer) += nchars;
524 BUF_PT_BYTE (current_buffer) += nbytes;
525
526 /* In a single-byte buffer, the two positions must be equal. */
527 if (ZV == ZV_BYTE
528 && PT != PT_BYTE)
529 abort ();
530 }
531 \f
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).
535
536 See the comment of adjust_markers_for_insert for the args
537 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES. */
538
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;
545 {
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;
551
552 while (!NILP (marker))
553 {
554 register struct Lisp_Marker *m = XMARKER (marker);
555
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))
562 {
563 if (m->bytepos < prev_to_byte + combined_after_bytes)
564 {
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;
568 }
569 else
570 {
571 m->charpos += diff_chars;
572 m->bytepos += diff_bytes;
573 }
574 }
575 else if (m->bytepos >= from_byte)
576 {
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;
584 }
585
586 marker = m->chain;
587 }
588
589 CHECK_MARKERS ();
590 }
591
592 \f
593 /* Make the gap NBYTES_ADDED bytes longer. */
594
595 void
596 make_gap (nbytes_added)
597 int nbytes_added;
598 {
599 unsigned char *result;
600 Lisp_Object tem;
601 int real_gap_loc;
602 int real_gap_loc_byte;
603 int old_gap_size;
604
605 /* If we have to get more space, get enough to last a while. */
606 nbytes_added += 2000;
607
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'. */
611
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");
615
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));
620
621 if (result == 0)
622 {
623 UNBLOCK_INPUT;
624 memory_full ();
625 }
626
627 /* We can't unblock until the new address is properly stored. */
628 BEG_ADDR = result;
629 UNBLOCK_INPUT;
630
631 /* Prevent quitting in move_gap. */
632 tem = Vinhibit_quit;
633 Vinhibit_quit = Qt;
634
635 real_gap_loc = GPT;
636 real_gap_loc_byte = GPT_BYTE;
637 old_gap_size = GAP_SIZE;
638
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;
643
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);
647
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;
652
653 /* Put an anchor. */
654 *(Z_ADDR) = 0;
655
656 Vinhibit_quit = tem;
657 }
658 \f
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.
663
664 Return the number of bytes stored at TO_ADDR. */
665
666 int
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;
673 {
674 if (from_multibyte == to_multibyte)
675 {
676 bcopy (from_addr, to_addr, nbytes);
677 return nbytes;
678 }
679 else if (from_multibyte)
680 {
681 int nchars = 0;
682 int bytes_left = nbytes;
683 Lisp_Object tbl = Qnil;
684
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))
688 {
689 tbl = Fchar_table_extra_slot (Vnonascii_translation_table,
690 make_number (0));
691 if (!CHAR_TABLE_P (tbl))
692 tbl = Qnil;
693 }
694
695 /* Convert multibyte to single byte. */
696 while (bytes_left > 0)
697 {
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++;
706 }
707 return nchars;
708 }
709 else
710 {
711 unsigned char *initial_to_addr = to_addr;
712
713 /* Convert single-byte to multibyte. */
714 while (nbytes > 0)
715 {
716 int c = *from_addr++;
717 unsigned char workbuf[4], *str;
718 int len;
719
720 if (c < 0400
721 && (c >= 0240
722 || (c >= 0200 && !NILP (Vnonascii_translation_table))))
723 {
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--;
729 }
730 else
731 /* Special case for speed. */
732 *to_addr++ = c, nbytes--;
733 }
734 return to_addr - initial_to_addr;
735 }
736 }
737
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. */
741
742 int
743 count_size_as_multibyte (ptr, nbytes)
744 unsigned char *ptr;
745 int nbytes;
746 {
747 int i;
748 int outgoing_nbytes = 0;
749
750 for (i = 0; i < nbytes; i++)
751 {
752 unsigned int c = *ptr++;
753
754 if (c < 0200 || (c < 0240 && NILP (Vnonascii_translation_table)))
755 outgoing_nbytes++;
756 else
757 {
758 c = unibyte_char_to_multibyte (c);
759 outgoing_nbytes += CHAR_BYTES (c);
760 }
761 }
762
763 return outgoing_nbytes;
764 }
765 \f
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.
770
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. */
773
774 void
775 insert (string, nbytes)
776 register unsigned char *string;
777 register int nbytes;
778 {
779 if (nbytes > 0)
780 {
781 int opoint = PT;
782 insert_1 (string, nbytes, 0, 1, 0);
783 signal_after_change (opoint, 0, PT - opoint);
784 }
785 }
786
787 /* Likewise, but inherit text properties from neighboring characters. */
788
789 void
790 insert_and_inherit (string, nbytes)
791 register unsigned char *string;
792 register int nbytes;
793 {
794 if (nbytes > 0)
795 {
796 int opoint = PT;
797 insert_1 (string, nbytes, 1, 1, 0);
798 signal_after_change (opoint, 0, PT - opoint);
799 }
800 }
801
802 /* Insert the character C before point. Do not inherit text properties. */
803
804 void
805 insert_char (c)
806 int c;
807 {
808 unsigned char workbuf[4], *str;
809 int len;
810
811 if (! NILP (current_buffer->enable_multibyte_characters))
812 len = CHAR_STRING (c, workbuf, str);
813 else
814 {
815 len = 1;
816 workbuf[0] = c;
817 str = workbuf;
818 }
819
820 insert (str, len);
821 }
822
823 /* Insert the null-terminated string S before point. */
824
825 void
826 insert_string (s)
827 char *s;
828 {
829 insert (s, strlen (s));
830 }
831
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. */
836
837 void
838 insert_before_markers (string, nbytes)
839 unsigned char *string;
840 register int nbytes;
841 {
842 if (nbytes > 0)
843 {
844 int opoint = PT;
845
846 insert_1 (string, nbytes, 0, 1, 1);
847 signal_after_change (opoint, 0, PT - opoint);
848 }
849 }
850
851 /* Likewise, but inherit text properties from neighboring characters. */
852
853 void
854 insert_before_markers_and_inherit (string, nbytes)
855 unsigned char *string;
856 register int nbytes;
857 {
858 if (nbytes > 0)
859 {
860 int opoint = PT;
861
862 insert_1 (string, nbytes, 1, 1, 1);
863 signal_after_change (opoint, 0, PT - opoint);
864 }
865 }
866
867 /* Subroutine used by the insert functions above. */
868
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;
874 {
875 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
876 inherit, prepare, before_markers);
877 }
878 \f
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. */
885 int
886 count_combining_composition (str1, len1, str2, len2, str3, len3)
887 unsigned char *str1, *str2, *str3;
888 int len1, len2, len3;
889 {
890 int len = len1 + len2 + len3;
891 unsigned char *buf = (unsigned char *) alloca (len + 1);
892 int bytes;
893
894 bcopy (str1, buf, len1);
895 if (str3)
896 {
897 bcopy (str3, buf + len1, len3);
898 len1 += len3;
899 }
900 bcopy (str2, buf + len1 , len2);
901 buf[len] = 0;
902 PARSE_MULTIBYTE_SEQ (buf, len, bytes);
903 return (bytes <= len1 ? 0 : bytes - len1);
904 }
905
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. */
910
911 int
912 count_combining_before (string, length, pos, pos_byte)
913 unsigned char *string;
914 int length;
915 int pos, pos_byte;
916 {
917 int len, combining_bytes;
918 unsigned char *p;
919
920 if (NILP (current_buffer->enable_multibyte_characters))
921 return 0;
922
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;
936
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);
940
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;
947
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++;
953
954 return (combining_bytes < p - string ? combining_bytes : p - string);
955 }
956
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. */
961
962 int
963 count_combining_after (string, length, pos, pos_byte)
964 unsigned char *string;
965 int length;
966 int pos, pos_byte;
967 {
968 int opos_byte = pos_byte;
969 int i;
970 int bytes;
971 unsigned char *bufp;
972
973 if (NILP (current_buffer->enable_multibyte_characters))
974 return 0;
975
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;
988
989 i = length - 1;
990 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
991 {
992 i--;
993 }
994 if (i < 0)
995 {
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
1012 ? 0
1013 : bytes - (pos_byte - 1 - i + length));
1014 }
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);
1021
1022 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
1023 bufp++, pos_byte++;
1024 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
1025
1026 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
1027 }
1028
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. */
1035
1036 #define ADJUST_CHAR_POS(target, target_byte) \
1037 do { \
1038 if (target > pos + nbytes) \
1039 target -= nbytes; \
1040 else if (target >= pos) \
1041 { \
1042 target = pos; \
1043 target_byte = pos_byte + nbytes; \
1044 } \
1045 } while (0)
1046
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.
1051
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. */
1055
1056 static void
1057 combine_bytes (pos, pos_byte, nbytes)
1058 int pos, pos_byte, nbytes;
1059 {
1060 adjust_overlays_for_delete (pos, nbytes);
1061
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);
1066
1067 if (BUF_INTERVALS (current_buffer) != 0)
1068 offset_intervals (current_buffer, pos, - nbytes);
1069 }
1070
1071 void
1072 byte_combining_error ()
1073 {
1074 error ("Byte combining across boundary of accessible buffer text inhibitted");
1075 }
1076
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)
1085
1086 \f
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. */
1090
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;
1096 {
1097 int combined_before_bytes, combined_after_bytes;
1098
1099 if (NILP (current_buffer->enable_multibyte_characters))
1100 nchars = nbytes;
1101
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);
1107
1108 if (PT != GPT)
1109 move_gap_both (PT, PT_BYTE);
1110 if (GAP_SIZE < nbytes)
1111 make_gap (nbytes - GAP_SIZE);
1112
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);
1118
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.
1122
1123 But there is no need to actually delete the combining bytes
1124 from the buffer and reinsert them. */
1125
1126 if (combined_after_bytes)
1127 {
1128 Lisp_Object deletion;
1129 deletion = Qnil;
1130
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);
1135
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);
1141 }
1142
1143 if (combined_before_bytes)
1144 {
1145 Lisp_Object deletion;
1146 deletion = Qnil;
1147
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);
1155 }
1156
1157 record_insert (PT - !!combined_before_bytes,
1158 nchars - combined_before_bytes + !!combined_before_bytes);
1159 MODIFF++;
1160
1161 bcopy (string, GPT_ADDR, nbytes);
1162
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. */
1173
1174 if (combined_after_bytes)
1175 move_gap_both (GPT + combined_after_bytes,
1176 GPT_BYTE + combined_after_bytes);
1177
1178 if (GPT_BYTE < GPT)
1179 abort ();
1180
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);
1186
1187 if (BUF_INTERVALS (current_buffer) != 0)
1188 offset_intervals (current_buffer, PT, nchars);
1189
1190 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
1191 Fset_text_properties (make_number (PT), make_number (PT + nchars),
1192 Qnil, Qnil);
1193
1194 {
1195 int pos = PT, pos_byte = PT_BYTE;
1196
1197 adjust_point (nchars + combined_after_bytes,
1198 nbytes + combined_after_bytes);
1199
1200 if (combined_after_bytes)
1201 combine_bytes (pos + nchars, pos_byte + nbytes, combined_after_bytes);
1202
1203 if (combined_before_bytes)
1204 combine_bytes (pos, pos_byte, combined_before_bytes);
1205 }
1206
1207 CHECK_MARKERS ();
1208 }
1209 \f
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.
1214
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. */
1218
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;
1224 {
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);
1229 }
1230
1231 /* Like `insert_from_string' except that all markers pointing
1232 at the place where the insertion happens are adjusted to point after it. */
1233
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;
1240 {
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);
1245 }
1246
1247 /* Subroutine of the insertion functions above. */
1248
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;
1255 {
1256 struct gcpro gcpro1;
1257 int outgoing_nbytes = nbytes;
1258 int combined_before_bytes, combined_after_bytes;
1259 INTERVAL intervals;
1260
1261 /* Make OUTGOING_NBYTES describe the text
1262 as it will be inserted in this buffer. */
1263
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);
1270
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);
1276
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;
1282
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));
1288
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. */
1294
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);
1299 {
1300 unsigned char save = *(GPT_ADDR);
1301 *(GPT_ADDR) = 0;
1302 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1303 *(GPT_ADDR) = save;
1304 }
1305
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.
1309
1310 But there is no need to actually delete the combining bytes
1311 from the buffer and reinsert them. */
1312
1313 if (combined_after_bytes)
1314 {
1315 Lisp_Object deletion;
1316 deletion = Qnil;
1317
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);
1322
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);
1328 }
1329
1330 if (combined_before_bytes)
1331 {
1332 Lisp_Object deletion;
1333 deletion = Qnil;
1334
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);
1342 }
1343
1344 record_insert (PT - !!combined_before_bytes,
1345 nchars - combined_before_bytes + !!combined_before_bytes);
1346 MODIFF++;
1347
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. */
1356
1357 if (combined_after_bytes)
1358 move_gap_both (GPT + combined_after_bytes,
1359 GPT_BYTE + combined_after_bytes);
1360
1361 if (GPT_BYTE < GPT)
1362 abort ();
1363
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);
1369
1370 offset_intervals (current_buffer, PT, nchars);
1371
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);
1377
1378 /* Insert those intervals. */
1379 graft_intervals_into_buffer (intervals, PT, nchars,
1380 current_buffer, inherit);
1381
1382 {
1383 int pos = PT, pos_byte = PT_BYTE;
1384
1385 adjust_point (nchars + combined_after_bytes,
1386 outgoing_nbytes + combined_after_bytes);
1387
1388 if (combined_after_bytes)
1389 combine_bytes (pos + nchars, pos_byte + outgoing_nbytes,
1390 combined_after_bytes);
1391
1392 if (combined_before_bytes)
1393 combine_bytes (pos, pos_byte, combined_before_bytes);
1394 }
1395 }
1396 \f
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.
1400
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. */
1403
1404 void
1405 insert_from_buffer (buf, charpos, nchars, inherit)
1406 struct buffer *buf;
1407 int charpos, nchars;
1408 int inherit;
1409 {
1410 int opoint = PT;
1411
1412 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1413 signal_after_change (opoint, 0, PT - opoint);
1414 }
1415
1416 static void
1417 insert_from_buffer_1 (buf, from, nchars, inherit)
1418 struct buffer *buf;
1419 int from, nchars;
1420 int inherit;
1421 {
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;
1430
1431 /* Make OUTGOING_NBYTES describe the text
1432 as it will be inserted in this buffer. */
1433
1434 if (NILP (current_buffer->enable_multibyte_characters))
1435 outgoing_nbytes = nchars;
1436 else if (NILP (buf->enable_multibyte_characters))
1437 {
1438 int outgoing_before_gap = 0;
1439 int outgoing_after_gap = 0;
1440
1441 if (from < BUF_GPT (buf))
1442 {
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);
1449 }
1450 else
1451 chunk = 0;
1452
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);
1458
1459 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1460 }
1461
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");
1466
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);
1471
1472 if (PT != GPT)
1473 move_gap_both (PT, PT_BYTE);
1474 if (GAP_SIZE < outgoing_nbytes)
1475 make_gap (outgoing_nbytes - GAP_SIZE);
1476
1477 if (from < BUF_GPT (buf))
1478 {
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));
1489 }
1490 else
1491 chunk_expanded = chunk = 0;
1492
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));
1498
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);
1508 {
1509 unsigned char save = *(GPT_ADDR);
1510 *(GPT_ADDR) = 0;
1511 CHECK_BYTE_COMBINING_FOR_INSERT (PT);
1512 *(GPT_ADDR) = save;
1513 }
1514
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.
1518
1519 But there is no need to actually delete the combining bytes
1520 from the buffer and reinsert them. */
1521
1522 if (combined_after_bytes)
1523 {
1524 Lisp_Object deletion;
1525 deletion = Qnil;
1526
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);
1531
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);
1537 }
1538
1539 if (combined_before_bytes)
1540 {
1541 Lisp_Object deletion;
1542 deletion = Qnil;
1543
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);
1551 }
1552
1553 record_insert (PT - !!combined_before_bytes,
1554 nchars - combined_before_bytes + !!combined_before_bytes);
1555 MODIFF++;
1556
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. */
1565
1566 if (combined_after_bytes)
1567 move_gap_both (GPT + combined_after_bytes,
1568 GPT_BYTE + combined_after_bytes);
1569
1570 if (GPT_BYTE < GPT)
1571 abort ();
1572
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);
1577
1578 if (BUF_INTERVALS (current_buffer) != 0)
1579 offset_intervals (current_buffer, PT, nchars);
1580
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);
1586
1587 /* Insert those intervals. */
1588 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1589
1590 {
1591 int pos = PT, pos_byte = PT_BYTE;
1592
1593 adjust_point (nchars + combined_after_bytes,
1594 outgoing_nbytes + combined_after_bytes);
1595
1596 if (combined_after_bytes)
1597 combine_bytes (pos + nchars, pos_byte + outgoing_nbytes,
1598 combined_after_bytes);
1599
1600 if (combined_before_bytes)
1601 combine_bytes (pos, pos_byte, combined_before_bytes);
1602 }
1603 }
1604 \f
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. */
1609
1610 void
1611 adjust_before_replace (from, from_byte, to, to_byte)
1612 int from, from_byte, to, to_byte;
1613 {
1614 Lisp_Object deletion;
1615
1616 if (! EQ (current_buffer->undo_list, Qt))
1617 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1618
1619 CHECK_MARKERS ();
1620
1621 adjust_markers_for_delete (from, from_byte, to, to_byte);
1622
1623 if (! EQ (current_buffer->undo_list, Qt))
1624 record_delete (from, deletion);
1625
1626 adjust_overlays_for_delete (from, to - from);
1627 }
1628
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.
1633
1634 PREV_TEXT nil means the new text was just inserted. */
1635
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;
1640 {
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));
1649
1650 int nchars_del = 0, nbytes_del = 0;
1651
1652 if (STRINGP (prev_text))
1653 {
1654 nchars_del = XSTRING (prev_text)->size;
1655 nbytes_del = STRING_BYTES (XSTRING (prev_text));
1656 }
1657
1658 if ((combine_before && from == BEGV)
1659 || (combined_after_bytes && from == ZV))
1660 {
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) = ' ';
1666 }
1667
1668 if (combined_after_bytes)
1669 {
1670 Lisp_Object deletion;
1671 deletion = Qnil;
1672
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,
1677 1);
1678
1679 adjust_markers_for_record_delete (from, from_byte,
1680 from + combined_after_bytes,
1681 from_byte + combined_after_bytes);
1682
1683 if (! EQ (current_buffer->undo_list, Qt))
1684 record_delete (from + nchars_del, deletion);
1685 }
1686
1687 if (combined_before_bytes
1688 || (len_byte == 0 && combined_after_bytes > 0))
1689 {
1690 Lisp_Object deletion;
1691 deletion = Qnil;
1692
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);
1700 }
1701
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. */
1708
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);
1713
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))
1718 {
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);
1725 }
1726
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)
1732 {
1733 offset_intervals (current_buffer, from, len - nchars_del);
1734 }
1735
1736 {
1737 if (from < PT)
1738 adjust_point (len - nchars_del, len_byte - nbytes_del);
1739
1740 if (combined_after_bytes)
1741 {
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);
1748 }
1749 if (combined_before_bytes)
1750 combine_bytes (from, from_byte, combined_before_bytes);
1751 }
1752
1753 /* As byte combining will decrease Z, we must check this again. */
1754 if (Z - GPT < END_UNCHANGED)
1755 END_UNCHANGED = Z - GPT;
1756
1757 CHECK_MARKERS ();
1758
1759 if (len == 0)
1760 evaporate_overlays (from);
1761 MODIFF++;
1762 }
1763
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. */
1768
1769 void
1770 adjust_after_insert (from, from_byte, to, to_byte, newlen)
1771 int from, from_byte, to, to_byte, newlen;
1772 {
1773 int len = to - from, len_byte = to_byte - from_byte;
1774
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);
1782 }
1783
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. */
1788
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.
1793
1794 But if MARKERS is 0, don't relocate markers. */
1795
1796 void
1797 replace_range (from, to, new, prepare, inherit, markers)
1798 Lisp_Object new;
1799 int from, to, prepare, inherit, markers;
1800 {
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;
1811
1812 CHECK_MARKERS ();
1813
1814 GCPRO1 (new);
1815
1816 if (prepare)
1817 {
1818 int range_length = to - from;
1819 prepare_to_modify_buffer (from, to, &from);
1820 to = from + range_length;
1821 }
1822
1823 UNGCPRO;
1824
1825 /* Make args be valid */
1826 if (from < BEGV)
1827 from = BEGV;
1828 if (to > ZV)
1829 to = ZV;
1830
1831 from_byte = CHAR_TO_BYTE (from);
1832 to_byte = CHAR_TO_BYTE (to);
1833
1834 nchars_del = to - from;
1835 nbytes_del = to_byte - from_byte;
1836
1837 if (nbytes_del <= 0 && insbytes == 0)
1838 return;
1839
1840 /* Make OUTGOING_INSBYTES describe the text
1841 as it will be inserted in this buffer. */
1842
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);
1848
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");
1853
1854 GCPRO1 (new);
1855
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);
1861
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);
1866
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);
1873
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. */
1882
1883 if (GPT_BYTE < GPT)
1884 abort ();
1885
1886 if (GPT - BEG < BEG_UNCHANGED)
1887 BEG_UNCHANGED = GPT - BEG;
1888 if (Z - GPT < END_UNCHANGED)
1889 END_UNCHANGED = Z - GPT;
1890
1891 if (GAP_SIZE < insbytes)
1892 make_gap (insbytes - GAP_SIZE);
1893
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));
1899
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. */
1905
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);
1910
1911 if ((combined_before_bytes && from == BEGV)
1912 || (combined_after_bytes && from == ZV))
1913 {
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);
1931 }
1932
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.
1936
1937 But there is no need to actually delete the combining bytes
1938 from the buffer and reinsert them. */
1939
1940 if (combined_after_bytes)
1941 {
1942 Lisp_Object deletion;
1943 deletion = Qnil;
1944
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,
1949 1);
1950
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);
1956 }
1957
1958 if (combined_before_bytes)
1959 {
1960 Lisp_Object deletion;
1961 deletion = Qnil;
1962
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);
1970 }
1971
1972 if (! EQ (current_buffer->undo_list, Qt))
1973 {
1974 record_delete (from - !!combined_before_bytes, deletion);
1975 record_insert (from - !!combined_before_bytes,
1976 (inschars - combined_before_bytes
1977 + !!combined_before_bytes));
1978 }
1979
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. */
1988
1989 if (combined_after_bytes)
1990 move_gap_both (GPT + combined_after_bytes,
1991 GPT_BYTE + combined_after_bytes);
1992
1993 if (GPT_BYTE < GPT)
1994 abort ();
1995
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);
2004
2005 offset_intervals (current_buffer, from, inschars - nchars_del);
2006
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);
2013
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)));
2019
2020 if (combined_after_bytes)
2021 {
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);
2028 }
2029 if (combined_before_bytes)
2030 combine_bytes (from, from_byte, combined_before_bytes);
2031
2032 /* As byte combining will decrease Z, we must check this again. */
2033 if (Z - GPT < END_UNCHANGED)
2034 END_UNCHANGED = Z - GPT;
2035
2036 if (outgoing_insbytes == 0)
2037 evaporate_overlays (from);
2038
2039 CHECK_MARKERS ();
2040
2041 MODIFF++;
2042 UNGCPRO;
2043
2044 signal_after_change (from, nchars_del, GPT - from);
2045 }
2046 \f
2047 /* Delete characters in current buffer
2048 from FROM up to (but not including) TO.
2049 If TO comes before FROM, we delete nothing. */
2050
2051 void
2052 del_range (from, to)
2053 register int from, to;
2054 {
2055 del_range_1 (from, to, 1);
2056 }
2057
2058 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
2059
2060 void
2061 del_range_1 (from, to, prepare)
2062 int from, to, prepare;
2063 {
2064 int from_byte, to_byte;
2065
2066 /* Make args be valid */
2067 if (from < BEGV)
2068 from = BEGV;
2069 if (to > ZV)
2070 to = ZV;
2071
2072 if (to <= from)
2073 return;
2074
2075 if (prepare)
2076 {
2077 int range_length = to - from;
2078 prepare_to_modify_buffer (from, to, &from);
2079 to = from + range_length;
2080 }
2081
2082 from_byte = CHAR_TO_BYTE (from);
2083 to_byte = CHAR_TO_BYTE (to);
2084
2085 del_range_2 (from, from_byte, to, to_byte);
2086 signal_after_change (from, to - from, 0);
2087 }
2088
2089 /* Like del_range_1 but args are byte positions, not char positions. */
2090
2091 void
2092 del_range_byte (from_byte, to_byte, prepare)
2093 int from_byte, to_byte, prepare;
2094 {
2095 int from, to;
2096
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;
2102
2103 if (to_byte <= from_byte)
2104 return;
2105
2106 from = BYTE_TO_CHAR (from_byte);
2107 to = BYTE_TO_CHAR (to_byte);
2108
2109 if (prepare)
2110 {
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;
2115
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);
2120 }
2121
2122 del_range_2 (from, from_byte, to, to_byte);
2123 signal_after_change (from, to - from, 0);
2124 }
2125
2126 /* Like del_range_1, but positions are specified both as charpos
2127 and bytepos. */
2128
2129 void
2130 del_range_both (from, from_byte, to, to_byte, prepare)
2131 int from, from_byte, to, to_byte, prepare;
2132 {
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;
2138
2139 if (to_byte <= from_byte)
2140 return;
2141
2142 if (from < BEGV)
2143 from = BEGV;
2144 if (to > ZV)
2145 to = ZV;
2146
2147 if (prepare)
2148 {
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;
2153
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);
2158 }
2159
2160 del_range_2 (from, from_byte, to, to_byte);
2161 signal_after_change (from, to - from, 0);
2162 }
2163
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. */
2167
2168 void
2169 del_range_2 (from, from_byte, to, to_byte)
2170 int from, from_byte, to, to_byte;
2171 {
2172 register int nbytes_del, nchars_del;
2173 int combined_after_bytes;
2174 Lisp_Object deletion;
2175 int from_byte_1;
2176
2177 CHECK_MARKERS ();
2178
2179 nchars_del = to - from;
2180 nbytes_del = to_byte - from_byte;
2181
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);
2187
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)
2192 {
2193 if (from == BEGV || to == ZV)
2194 byte_combining_error ();
2195 from_byte_1 = from_byte;
2196 DEC_POS (from_byte_1);
2197 }
2198 else
2199 from_byte_1 = from_byte;
2200
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);
2211
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)
2218 {
2219 /* Adjust markers for the phony deletion
2220 that we are about to call record_undo for. */
2221
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);
2229
2230 adjust_markers_for_record_delete (from - 1, from_byte_1,
2231 from, from_byte);
2232 }
2233 if (! EQ (current_buffer->undo_list, Qt))
2234 record_delete (from - !!combined_after_bytes, deletion);
2235 MODIFF++;
2236
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));
2241
2242 offset_intervals (current_buffer, from, - nchars_del);
2243
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);
2247
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;
2255
2256 if (combined_after_bytes)
2257 move_gap_both (GPT + combined_after_bytes,
2258 GPT_BYTE + combined_after_bytes);
2259
2260 *(GPT_ADDR) = 0; /* Put an anchor. */
2261
2262 if (GPT_BYTE < GPT)
2263 abort ();
2264
2265 if (GPT - BEG < BEG_UNCHANGED)
2266 BEG_UNCHANGED = GPT - BEG;
2267 if (Z - GPT < END_UNCHANGED)
2268 END_UNCHANGED = Z - GPT;
2269
2270 if (combined_after_bytes)
2271 {
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);
2278
2279 record_insert (GPT - 1, 1);
2280
2281 if (Z - GPT < END_UNCHANGED)
2282 END_UNCHANGED = Z - GPT;
2283 }
2284
2285 CHECK_MARKERS ();
2286
2287 evaporate_overlays (from);
2288 }
2289 \f
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. */
2295
2296 void
2297 modify_region (buffer, start, end)
2298 struct buffer *buffer;
2299 int start, end;
2300 {
2301 struct buffer *old_buffer = current_buffer;
2302
2303 if (buffer != old_buffer)
2304 set_buffer_internal (buffer);
2305
2306 prepare_to_modify_buffer (start, end, NULL);
2307
2308 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
2309
2310 if (MODIFF <= SAVE_MODIFF)
2311 record_first_change ();
2312 MODIFF++;
2313
2314 buffer->point_before_scroll = Qnil;
2315
2316 if (buffer != old_buffer)
2317 set_buffer_internal (old_buffer);
2318 }
2319 \f
2320 /* Check that it is okay to modify the buffer between START and END,
2321 which are char positions.
2322
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.
2326
2327 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2328 by holding its value temporarily in a marker. */
2329
2330 void
2331 prepare_to_modify_buffer (start, end, preserve_ptr)
2332 int start, end;
2333 int *preserve_ptr;
2334 {
2335 if (!NILP (current_buffer->read_only))
2336 Fbarf_if_buffer_read_only ();
2337
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;
2342
2343 if (BUF_INTERVALS (current_buffer) != 0)
2344 {
2345 if (preserve_ptr)
2346 {
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;
2355 }
2356 else
2357 verify_interval_modification (current_buffer, start, end);
2358 }
2359
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 */
2375
2376 signal_before_change (start, end, preserve_ptr);
2377
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);
2386
2387 Vdeactivate_mark = Qt;
2388 }
2389 \f
2390 /* These macros work with an argument named `preserve_ptr'
2391 and a local variable named `preserve_marker'. */
2392
2393 #define PRESERVE_VALUE \
2394 if (preserve_ptr && NILP (preserve_marker)) \
2395 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2396
2397 #define RESTORE_VALUE \
2398 if (! NILP (preserve_marker)) \
2399 { \
2400 *preserve_ptr = marker_position (preserve_marker); \
2401 unchain_marker (preserve_marker); \
2402 }
2403
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);
2409
2410 #define FETCH_START \
2411 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2412
2413 #define FETCH_END \
2414 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2415
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.
2418
2419 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2420 by holding its value temporarily in a marker. */
2421
2422 void
2423 signal_before_change (start_int, end_int, preserve_ptr)
2424 int start_int, end_int;
2425 int *preserve_ptr;
2426 {
2427 Lisp_Object start, end;
2428 Lisp_Object start_marker, end_marker;
2429 Lisp_Object preserve_marker;
2430 struct gcpro gcpro1, gcpro2, gcpro3;
2431
2432 if (inhibit_modification_hooks)
2433 return;
2434
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);
2441
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))
2446 {
2447 PRESERVE_VALUE;
2448 PRESERVE_START_END;
2449 call1 (Vrun_hooks, Qfirst_change_hook);
2450 }
2451
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))
2456 {
2457 PRESERVE_VALUE;
2458 PRESERVE_START_END;
2459 call2 (Vbefore_change_function, FETCH_START, FETCH_END);
2460 }
2461
2462 /* Now run the before-change-functions if any. */
2463 if (!NILP (Vbefore_change_functions))
2464 {
2465 Lisp_Object args[3];
2466 Lisp_Object before_change_functions;
2467 Lisp_Object after_change_functions;
2468 struct gcpro gcpro1, gcpro2;
2469
2470 PRESERVE_VALUE;
2471 PRESERVE_START_END;
2472
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);
2481
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);
2487
2488 /* "Unbind" the variables we "bound" to nil. */
2489 Vbefore_change_functions = before_change_functions;
2490 Vafter_change_functions = after_change_functions;
2491 UNGCPRO;
2492 }
2493
2494 if (!NILP (current_buffer->overlays_before)
2495 || !NILP (current_buffer->overlays_after))
2496 {
2497 PRESERVE_VALUE;
2498 report_overlay_modification (FETCH_START, FETCH_END, 0,
2499 FETCH_START, FETCH_END, Qnil);
2500 }
2501
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;
2508 }
2509
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. */
2516
2517 void
2518 signal_after_change (charpos, lendel, lenins)
2519 int charpos, lendel, lenins;
2520 {
2521 if (inhibit_modification_hooks)
2522 return;
2523
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))
2531 {
2532 Lisp_Object elt;
2533
2534 if (!NILP (combine_after_change_list)
2535 && current_buffer != XBUFFER (combine_after_change_buffer))
2536 Fcombine_after_change_execute ();
2537
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 ();
2544
2545 return;
2546 }
2547
2548 if (!NILP (combine_after_change_list))
2549 Fcombine_after_change_execute ();
2550
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));
2558
2559 if (!NILP (Vafter_change_functions))
2560 {
2561 Lisp_Object args[4];
2562 Lisp_Object before_change_functions;
2563 Lisp_Object after_change_functions;
2564 struct gcpro gcpro1, gcpro2;
2565
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);
2574
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);
2582
2583 /* "Unbind" the variables we "bound" to nil. */
2584 Vbefore_change_functions = before_change_functions;
2585 Vafter_change_functions = after_change_functions;
2586 UNGCPRO;
2587 }
2588
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),
2593 1,
2594 make_number (charpos),
2595 make_number (charpos + lenins),
2596 make_number (lendel));
2597
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));
2603 }
2604
2605 Lisp_Object
2606 Fcombine_after_change_execute_1 (val)
2607 Lisp_Object val;
2608 {
2609 Vcombine_after_change_calls = val;
2610 return val;
2611 }
2612
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'.")
2616 ()
2617 {
2618 int count = specpdl_ptr - specpdl;
2619 int beg, end, change;
2620 int begpos, endpos;
2621 Lisp_Object tail;
2622
2623 if (NILP (combine_after_change_list))
2624 return Qnil;
2625
2626 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2627
2628 Fset_buffer (combine_after_change_buffer);
2629
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;
2636
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))
2641 {
2642 Lisp_Object elt;
2643 int thisbeg, thisend, thischange;
2644
2645 /* Extract the info from the next element. */
2646 elt = XCAR (tail);
2647 if (! CONSP (elt))
2648 continue;
2649 thisbeg = XINT (XCAR (elt));
2650
2651 elt = XCDR (elt);
2652 if (! CONSP (elt))
2653 continue;
2654 thisend = XINT (XCAR (elt));
2655
2656 elt = XCDR (elt);
2657 if (! CONSP (elt))
2658 continue;
2659 thischange = XINT (XCAR (elt));
2660
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;
2667 }
2668
2669 /* Get the current start and end positions of the range
2670 that was changed. */
2671 begpos = BEG + beg;
2672 endpos = Z - end;
2673
2674 /* We are about to handle these, so discard them. */
2675 combine_after_change_list = Qnil;
2676
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);
2682
2683 return unbind_to (count, Qnil);
2684 }
2685 \f
2686 void
2687 syms_of_insdel ()
2688 {
2689 staticpro (&combine_after_change_list);
2690 combine_after_change_list = Qnil;
2691 combine_after_change_buffer = Qnil;
2692
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;
2699
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;
2705
2706 defsubr (&Scombine_after_change_execute);
2707 }