Fix some uses of int instead of EMACS_INT.
[bpt/emacs.git] / src / insdel.c
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 #include <config.h>
23 #include <setjmp.h>
24 #include "lisp.h"
25 #include "intervals.h"
26 #include "buffer.h"
27 #include "character.h"
28 #include "window.h"
29 #include "blockinput.h"
30 #include "region-cache.h"
31
32 #ifndef NULL
33 #define NULL 0
34 #endif
35
36 static void insert_from_string_1 (Lisp_Object string,
37 EMACS_INT pos, EMACS_INT pos_byte,
38 EMACS_INT nchars, EMACS_INT nbytes,
39 int inherit, int before_markers);
40 static void insert_from_buffer_1 (struct buffer *buf,
41 EMACS_INT from, EMACS_INT nchars,
42 int inherit);
43 static void gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap);
44 static void gap_right (EMACS_INT charpos, EMACS_INT bytepos);
45 static void adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to,
46 EMACS_INT amount);
47 static void adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
48 EMACS_INT to, EMACS_INT to_byte,
49 int before_markers);
50 static void adjust_markers_for_replace (EMACS_INT, EMACS_INT, EMACS_INT,
51 EMACS_INT, EMACS_INT, EMACS_INT);
52 static void adjust_point (EMACS_INT nchars, EMACS_INT nbytes);
53
54 Lisp_Object Fcombine_after_change_execute (void);
55
56 /* Non-nil means don't call the after-change-functions right away,
57 just record an element in combine_after_change_list. */
58 Lisp_Object Vcombine_after_change_calls;
59
60 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
61 describing changes which happened while combine_after_change_calls
62 was nonzero. We use this to decide how to call them
63 once the deferral ends.
64
65 In each element.
66 BEG-UNCHANGED is the number of chars before the changed range.
67 END-UNCHANGED is the number of chars after the changed range,
68 and CHANGE-AMOUNT is the number of characters inserted by the change
69 (negative for a deletion). */
70 Lisp_Object combine_after_change_list;
71
72 /* Buffer which combine_after_change_list is about. */
73 Lisp_Object combine_after_change_buffer;
74
75 Lisp_Object Qinhibit_modification_hooks;
76
77 extern Lisp_Object Vselect_active_regions, Vsaved_region_selection, Qonly;
78
79 \f
80 /* Check all markers in the current buffer, looking for something invalid. */
81
82 static int check_markers_debug_flag;
83
84 #define CHECK_MARKERS() \
85 if (check_markers_debug_flag) \
86 check_markers (); \
87 else
88
89 void
90 check_markers (void)
91 {
92 register struct Lisp_Marker *tail;
93 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
94
95 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
96 {
97 if (tail->buffer->text != current_buffer->text)
98 abort ();
99 if (tail->charpos > Z)
100 abort ();
101 if (tail->bytepos > Z_BYTE)
102 abort ();
103 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
104 abort ();
105 }
106 }
107 \f
108 /* Move gap to position CHARPOS.
109 Note that this can quit! */
110
111 void
112 move_gap (EMACS_INT charpos)
113 {
114 move_gap_both (charpos, charpos_to_bytepos (charpos));
115 }
116
117 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
118 Note that this can quit! */
119
120 void
121 move_gap_both (EMACS_INT charpos, EMACS_INT bytepos)
122 {
123 if (bytepos < GPT_BYTE)
124 gap_left (charpos, bytepos, 0);
125 else if (bytepos > GPT_BYTE)
126 gap_right (charpos, bytepos);
127 }
128
129 /* Move the gap to a position less than the current GPT.
130 BYTEPOS describes the new position as a byte position,
131 and CHARPOS is the corresponding char position.
132 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
133
134 static void
135 gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap)
136 {
137 register unsigned char *to, *from;
138 register EMACS_INT i;
139 EMACS_INT new_s1;
140
141 if (!newgap)
142 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
143
144 i = GPT_BYTE;
145 to = GAP_END_ADDR;
146 from = GPT_ADDR;
147 new_s1 = GPT_BYTE;
148
149 /* Now copy the characters. To move the gap down,
150 copy characters up. */
151
152 while (1)
153 {
154 /* I gets number of characters left to copy. */
155 i = new_s1 - bytepos;
156 if (i == 0)
157 break;
158 /* If a quit is requested, stop copying now.
159 Change BYTEPOS to be where we have actually moved the gap to. */
160 if (QUITP)
161 {
162 bytepos = new_s1;
163 charpos = BYTE_TO_CHAR (bytepos);
164 break;
165 }
166 /* Move at most 32000 chars before checking again for a quit. */
167 if (i > 32000)
168 i = 32000;
169 new_s1 -= i;
170 from -= i, to -= i;
171 memmove (to, from, i);
172 }
173
174 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
175 BYTEPOS is where the loop above stopped, which may be what was specified
176 or may be where a quit was detected. */
177 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
178 GPT_BYTE = bytepos;
179 GPT = charpos;
180 if (bytepos < charpos)
181 abort ();
182 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
183 QUIT;
184 }
185
186 /* Move the gap to a position greater than the current GPT.
187 BYTEPOS describes the new position as a byte position,
188 and CHARPOS is the corresponding char position. */
189
190 static void
191 gap_right (EMACS_INT charpos, EMACS_INT bytepos)
192 {
193 register unsigned char *to, *from;
194 register EMACS_INT i;
195 EMACS_INT new_s1;
196
197 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
198
199 i = GPT_BYTE;
200 from = GAP_END_ADDR;
201 to = GPT_ADDR;
202 new_s1 = GPT_BYTE;
203
204 /* Now copy the characters. To move the gap up,
205 copy characters down. */
206
207 while (1)
208 {
209 /* I gets number of characters left to copy. */
210 i = bytepos - new_s1;
211 if (i == 0)
212 break;
213 /* If a quit is requested, stop copying now.
214 Change BYTEPOS to be where we have actually moved the gap to. */
215 if (QUITP)
216 {
217 bytepos = new_s1;
218 charpos = BYTE_TO_CHAR (bytepos);
219 break;
220 }
221 /* Move at most 32000 chars before checking again for a quit. */
222 if (i > 32000)
223 i = 32000;
224 new_s1 += i;
225 memmove (to, from, i);
226 from += i, to += i;
227 }
228
229 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
230 - GAP_SIZE);
231 GPT = charpos;
232 GPT_BYTE = bytepos;
233 if (bytepos < charpos)
234 abort ();
235 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
236 QUIT;
237 }
238 \f
239 /* Add AMOUNT to the byte position of every marker in the current buffer
240 whose current byte position is between FROM (exclusive) and TO (inclusive).
241
242 Also, any markers past the outside of that interval, in the direction
243 of adjustment, are first moved back to the near end of the interval
244 and then adjusted by AMOUNT.
245
246 When the latter adjustment is done, if AMOUNT is negative,
247 we record the adjustment for undo. (This case happens only for
248 deletion.)
249
250 The markers' character positions are not altered,
251 because gap motion does not affect character positions. */
252
253 int adjust_markers_test;
254
255 static void
256 adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to, EMACS_INT amount)
257 {
258 /* Now that a marker has a bytepos, not counting the gap,
259 nothing needs to be done here. */
260 #if 0
261 Lisp_Object marker;
262 register struct Lisp_Marker *m;
263 register EMACS_INT mpos;
264
265 marker = BUF_MARKERS (current_buffer);
266
267 while (!NILP (marker))
268 {
269 m = XMARKER (marker);
270 mpos = m->bytepos;
271 if (amount > 0)
272 {
273 if (mpos > to && mpos < to + amount)
274 {
275 if (adjust_markers_test)
276 abort ();
277 mpos = to + amount;
278 }
279 }
280 else
281 {
282 /* Here's the case where a marker is inside text being deleted.
283 AMOUNT can be negative for gap motion, too,
284 but then this range contains no markers. */
285 if (mpos > from + amount && mpos <= from)
286 {
287 if (adjust_markers_test)
288 abort ();
289 mpos = from + amount;
290 }
291 }
292 if (mpos > from && mpos <= to)
293 mpos += amount;
294 m->bufpos = mpos;
295 marker = m->chain;
296 }
297 #endif
298 }
299 \f
300 /* Adjust all markers for a deletion
301 whose range in bytes is FROM_BYTE to TO_BYTE.
302 The range in charpos is FROM to TO.
303
304 This function assumes that the gap is adjacent to
305 or inside of the range being deleted. */
306
307 void
308 adjust_markers_for_delete (EMACS_INT from, EMACS_INT from_byte,
309 EMACS_INT to, EMACS_INT to_byte)
310 {
311 Lisp_Object marker;
312 register struct Lisp_Marker *m;
313 register EMACS_INT charpos;
314
315 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
316 {
317 charpos = m->charpos;
318
319 if (charpos > Z)
320 abort ();
321
322 /* If the marker is after the deletion,
323 relocate by number of chars / bytes deleted. */
324 if (charpos > to)
325 {
326 m->charpos -= to - from;
327 m->bytepos -= to_byte - from_byte;
328 }
329 /* Here's the case where a marker is inside text being deleted. */
330 else if (charpos > from)
331 {
332 if (! m->insertion_type)
333 { /* Normal markers will end up at the beginning of the
334 re-inserted text after undoing a deletion, and must be
335 adjusted to move them to the correct place. */
336 XSETMISC (marker, m);
337 record_marker_adjustment (marker, from - charpos);
338 }
339 else if (charpos < to)
340 { /* Before-insertion markers will automatically move forward
341 upon re-inserting the deleted text, so we have to arrange
342 for them to move backward to the correct position. */
343 XSETMISC (marker, m);
344 record_marker_adjustment (marker, to - charpos);
345 }
346 m->charpos = from;
347 m->bytepos = from_byte;
348 }
349 /* Here's the case where a before-insertion marker is immediately
350 before the deleted region. */
351 else if (charpos == from && m->insertion_type)
352 {
353 /* Undoing the change uses normal insertion, which will
354 incorrectly make MARKER move forward, so we arrange for it
355 to then move backward to the correct place at the beginning
356 of the deleted region. */
357 XSETMISC (marker, m);
358 record_marker_adjustment (marker, to - from);
359 }
360 }
361 }
362
363 \f
364 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
365 to TO / TO_BYTE. We have to relocate the charpos of every marker
366 that points after the insertion (but not their bytepos).
367
368 When a marker points at the insertion point,
369 we advance it if either its insertion-type is t
370 or BEFORE_MARKERS is true. */
371
372 static void
373 adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
374 EMACS_INT to, EMACS_INT to_byte, int before_markers)
375 {
376 struct Lisp_Marker *m;
377 int adjusted = 0;
378 EMACS_INT nchars = to - from;
379 EMACS_INT nbytes = to_byte - from_byte;
380
381 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
382 {
383 eassert (m->bytepos >= m->charpos
384 && m->bytepos - m->charpos <= Z_BYTE - Z);
385
386 if (m->bytepos == from_byte)
387 {
388 if (m->insertion_type || before_markers)
389 {
390 m->bytepos = to_byte;
391 m->charpos = to;
392 if (m->insertion_type)
393 adjusted = 1;
394 }
395 }
396 else if (m->bytepos > from_byte)
397 {
398 m->bytepos += nbytes;
399 m->charpos += nchars;
400 }
401 }
402
403 /* Adjusting only markers whose insertion-type is t may result in
404 - disordered start and end in overlays, and
405 - disordered overlays in the slot `overlays_before' of current_buffer. */
406 if (adjusted)
407 {
408 fix_start_end_in_overlays(from, to);
409 fix_overlays_before (current_buffer, from, to);
410 }
411 }
412
413 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
414
415 This is used only when the value of point changes due to an insert
416 or delete; it does not represent a conceptual change in point as a
417 marker. In particular, point is not crossing any interval
418 boundaries, so there's no need to use the usual SET_PT macro. In
419 fact it would be incorrect to do so, because either the old or the
420 new value of point is out of sync with the current set of
421 intervals. */
422
423 static void
424 adjust_point (EMACS_INT nchars, EMACS_INT nbytes)
425 {
426 BUF_PT (current_buffer) += nchars;
427 BUF_PT_BYTE (current_buffer) += nbytes;
428
429 /* In a single-byte buffer, the two positions must be equal. */
430 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
431 }
432 \f
433 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
434 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
435 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
436 an insertion. */
437
438 static void
439 adjust_markers_for_replace (EMACS_INT from, EMACS_INT from_byte,
440 EMACS_INT old_chars, EMACS_INT old_bytes,
441 EMACS_INT new_chars, EMACS_INT new_bytes)
442 {
443 register struct Lisp_Marker *m;
444 EMACS_INT prev_to_byte = from_byte + old_bytes;
445 EMACS_INT diff_chars = new_chars - old_chars;
446 EMACS_INT diff_bytes = new_bytes - old_bytes;
447
448 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
449 {
450 if (m->bytepos >= prev_to_byte)
451 {
452 m->charpos += diff_chars;
453 m->bytepos += diff_bytes;
454 }
455 else if (m->bytepos > from_byte)
456 {
457 m->charpos = from;
458 m->bytepos = from_byte;
459 }
460 }
461
462 CHECK_MARKERS ();
463 }
464
465 \f
466 /* Make the gap NBYTES_ADDED bytes longer. */
467
468 void
469 make_gap_larger (EMACS_INT nbytes_added)
470 {
471 Lisp_Object tem;
472 EMACS_INT real_gap_loc;
473 EMACS_INT real_gap_loc_byte;
474 EMACS_INT old_gap_size;
475
476 /* If we have to get more space, get enough to last a while. */
477 nbytes_added += 2000;
478
479 { EMACS_INT total_size = Z_BYTE - BEG_BYTE + GAP_SIZE + nbytes_added;
480 if (total_size < 0
481 /* Don't allow a buffer size that won't fit in a Lisp integer. */
482 || total_size != XINT (make_number (total_size))
483 /* Don't allow a buffer size that won't fit in an int
484 even if it will fit in a Lisp integer.
485 That won't work because so many places still use `int'. */
486 || total_size != (EMACS_INT) (int) total_size)
487 error ("Buffer exceeds maximum size");
488 }
489
490 enlarge_buffer_text (current_buffer, nbytes_added);
491
492 /* Prevent quitting in move_gap. */
493 tem = Vinhibit_quit;
494 Vinhibit_quit = Qt;
495
496 real_gap_loc = GPT;
497 real_gap_loc_byte = GPT_BYTE;
498 old_gap_size = GAP_SIZE;
499
500 /* Call the newly allocated space a gap at the end of the whole space. */
501 GPT = Z + GAP_SIZE;
502 GPT_BYTE = Z_BYTE + GAP_SIZE;
503 GAP_SIZE = nbytes_added;
504
505 /* Move the new gap down to be consecutive with the end of the old one.
506 This adjusts the markers properly too. */
507 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
508
509 /* Now combine the two into one large gap. */
510 GAP_SIZE += old_gap_size;
511 GPT = real_gap_loc;
512 GPT_BYTE = real_gap_loc_byte;
513
514 /* Put an anchor. */
515 *(Z_ADDR) = 0;
516
517 Vinhibit_quit = tem;
518 }
519
520
521 /* Make the gap NBYTES_REMOVED bytes shorter. */
522
523 void
524 make_gap_smaller (EMACS_INT nbytes_removed)
525 {
526 Lisp_Object tem;
527 EMACS_INT real_gap_loc;
528 EMACS_INT real_gap_loc_byte;
529 EMACS_INT real_Z;
530 EMACS_INT real_Z_byte;
531 EMACS_INT real_beg_unchanged;
532 EMACS_INT new_gap_size;
533
534 /* Make sure the gap is at least 20 bytes. */
535 if (GAP_SIZE - nbytes_removed < 20)
536 nbytes_removed = GAP_SIZE - 20;
537
538 /* Prevent quitting in move_gap. */
539 tem = Vinhibit_quit;
540 Vinhibit_quit = Qt;
541
542 real_gap_loc = GPT;
543 real_gap_loc_byte = GPT_BYTE;
544 new_gap_size = GAP_SIZE - nbytes_removed;
545 real_Z = Z;
546 real_Z_byte = Z_BYTE;
547 real_beg_unchanged = BEG_UNCHANGED;
548
549 /* Pretend that the last unwanted part of the gap is the entire gap,
550 and that the first desired part of the gap is part of the buffer
551 text. */
552 memset (GPT_ADDR, 0, new_gap_size);
553 GPT += new_gap_size;
554 GPT_BYTE += new_gap_size;
555 Z += new_gap_size;
556 Z_BYTE += new_gap_size;
557 GAP_SIZE = nbytes_removed;
558
559 /* Move the unwanted pretend gap to the end of the buffer. This
560 adjusts the markers properly too. */
561 gap_right (Z, Z_BYTE);
562
563 enlarge_buffer_text (current_buffer, -nbytes_removed);
564
565 /* Now restore the desired gap. */
566 GAP_SIZE = new_gap_size;
567 GPT = real_gap_loc;
568 GPT_BYTE = real_gap_loc_byte;
569 Z = real_Z;
570 Z_BYTE = real_Z_byte;
571 BEG_UNCHANGED = real_beg_unchanged;
572
573 /* Put an anchor. */
574 *(Z_ADDR) = 0;
575
576 Vinhibit_quit = tem;
577 }
578
579 void
580 make_gap (EMACS_INT nbytes_added)
581 {
582 if (nbytes_added >= 0)
583 make_gap_larger (nbytes_added);
584 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
585 else
586 make_gap_smaller (-nbytes_added);
587 #endif
588 }
589 \f
590 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
591 FROM_MULTIBYTE says whether the incoming text is multibyte.
592 TO_MULTIBYTE says whether to store the text as multibyte.
593 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
594
595 Return the number of bytes stored at TO_ADDR. */
596
597 EMACS_INT
598 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
599 EMACS_INT nbytes, int from_multibyte, int to_multibyte)
600 {
601 if (from_multibyte == to_multibyte)
602 {
603 memcpy (to_addr, from_addr, nbytes);
604 return nbytes;
605 }
606 else if (from_multibyte)
607 {
608 EMACS_INT nchars = 0;
609 EMACS_INT bytes_left = nbytes;
610 Lisp_Object tbl = Qnil;
611
612 while (bytes_left > 0)
613 {
614 int thislen, c;
615 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
616 if (! ASCII_CHAR_P (c))
617 c &= 0xFF;
618 *to_addr++ = c;
619 from_addr += thislen;
620 bytes_left -= thislen;
621 nchars++;
622 }
623 return nchars;
624 }
625 else
626 {
627 unsigned char *initial_to_addr = to_addr;
628
629 /* Convert single-byte to multibyte. */
630 while (nbytes > 0)
631 {
632 int c = *from_addr++;
633
634 if (!ASCII_CHAR_P (c))
635 {
636 c = BYTE8_TO_CHAR (c);
637 to_addr += CHAR_STRING (c, to_addr);
638 nbytes--;
639 }
640 else
641 /* Special case for speed. */
642 *to_addr++ = c, nbytes--;
643 }
644 return to_addr - initial_to_addr;
645 }
646 }
647
648 /* Return the number of bytes it would take
649 to convert some single-byte text to multibyte.
650 The single-byte text consists of NBYTES bytes at PTR. */
651
652 EMACS_INT
653 count_size_as_multibyte (const unsigned char *ptr, EMACS_INT nbytes)
654 {
655 EMACS_INT i;
656 EMACS_INT outgoing_nbytes = 0;
657
658 for (i = 0; i < nbytes; i++)
659 {
660 unsigned int c = *ptr++;
661
662 if (ASCII_CHAR_P (c))
663 outgoing_nbytes++;
664 else
665 {
666 c = BYTE8_TO_CHAR (c);
667 outgoing_nbytes += CHAR_BYTES (c);
668 }
669 }
670
671 return outgoing_nbytes;
672 }
673 \f
674 /* Insert a string of specified length before point.
675 This function judges multibyteness based on
676 enable_multibyte_characters in the current buffer;
677 it never converts between single-byte and multibyte.
678
679 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
680 prepare_to_modify_buffer could relocate the text. */
681
682 void
683 insert (const unsigned char *string, EMACS_INT nbytes)
684 {
685 if (nbytes > 0)
686 {
687 EMACS_INT len = chars_in_text (string, nbytes), opoint;
688 insert_1_both (string, len, nbytes, 0, 1, 0);
689 opoint = PT - len;
690 signal_after_change (opoint, 0, len);
691 update_compositions (opoint, PT, CHECK_BORDER);
692 }
693 }
694
695 /* Likewise, but inherit text properties from neighboring characters. */
696
697 void
698 insert_and_inherit (const unsigned char *string, EMACS_INT nbytes)
699 {
700 if (nbytes > 0)
701 {
702 EMACS_INT len = chars_in_text (string, nbytes), opoint;
703 insert_1_both (string, len, nbytes, 1, 1, 0);
704 opoint = PT - len;
705 signal_after_change (opoint, 0, len);
706 update_compositions (opoint, PT, CHECK_BORDER);
707 }
708 }
709
710 /* Insert the character C before point. Do not inherit text properties. */
711
712 void
713 insert_char (int c)
714 {
715 unsigned char str[MAX_MULTIBYTE_LENGTH];
716 int len;
717
718 if (! NILP (current_buffer->enable_multibyte_characters))
719 len = CHAR_STRING (c, str);
720 else
721 {
722 len = 1;
723 str[0] = c;
724 }
725
726 insert (str, len);
727 }
728
729 /* Insert the null-terminated string S before point. */
730
731 void
732 insert_string (const char *s)
733 {
734 insert (s, strlen (s));
735 }
736
737 /* Like `insert' except that all markers pointing at the place where
738 the insertion happens are adjusted to point after it.
739 Don't use this function to insert part of a Lisp string,
740 since gc could happen and relocate it. */
741
742 void
743 insert_before_markers (const unsigned char *string, EMACS_INT nbytes)
744 {
745 if (nbytes > 0)
746 {
747 EMACS_INT len = chars_in_text (string, nbytes), opoint;
748 insert_1_both (string, len, nbytes, 0, 1, 1);
749 opoint = PT - len;
750 signal_after_change (opoint, 0, len);
751 update_compositions (opoint, PT, CHECK_BORDER);
752 }
753 }
754
755 /* Likewise, but inherit text properties from neighboring characters. */
756
757 void
758 insert_before_markers_and_inherit (const unsigned char *string,
759 EMACS_INT nbytes)
760 {
761 if (nbytes > 0)
762 {
763 EMACS_INT len = chars_in_text (string, nbytes), opoint;
764 insert_1_both (string, len, nbytes, 1, 1, 1);
765 opoint = PT - len;
766 signal_after_change (opoint, 0, len);
767 update_compositions (opoint, PT, CHECK_BORDER);
768 }
769 }
770
771 /* Subroutine used by the insert functions above. */
772
773 void
774 insert_1 (const unsigned char *string, EMACS_INT nbytes,
775 int inherit, int prepare, int before_markers)
776 {
777 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
778 inherit, prepare, before_markers);
779 }
780
781 \f
782 #ifdef BYTE_COMBINING_DEBUG
783
784 /* See if the bytes before POS/POS_BYTE combine with bytes
785 at the start of STRING to form a single character.
786 If so, return the number of bytes at the start of STRING
787 which combine in this way. Otherwise, return 0. */
788
789 int
790 count_combining_before (const unsigned char *string, EMACS_INT length,
791 EMACS_INT pos, EMACS_INT pos_byte)
792 {
793 int len, combining_bytes;
794 const unsigned char *p;
795
796 if (NILP (current_buffer->enable_multibyte_characters))
797 return 0;
798
799 /* At first, we can exclude the following cases:
800 (1) STRING[0] can't be a following byte of multibyte sequence.
801 (2) POS is the start of the current buffer.
802 (3) A character before POS is not a multibyte character. */
803 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
804 return 0;
805 if (pos_byte == BEG_BYTE) /* case (2) */
806 return 0;
807 len = 1;
808 p = BYTE_POS_ADDR (pos_byte - 1);
809 while (! CHAR_HEAD_P (*p)) p--, len++;
810 if (! LEADING_CODE_P (*p)) /* case (3) */
811 return 0;
812
813 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
814 if (combining_bytes <= 0)
815 /* The character preceding POS is, complete and no room for
816 combining bytes (combining_bytes == 0), or an independent 8-bit
817 character (combining_bytes < 0). */
818 return 0;
819
820 /* We have a combination situation. Count the bytes at STRING that
821 may combine. */
822 p = string + 1;
823 while (!CHAR_HEAD_P (*p) && p < string + length)
824 p++;
825
826 return (combining_bytes < p - string ? combining_bytes : p - string);
827 }
828
829 /* See if the bytes after POS/POS_BYTE combine with bytes
830 at the end of STRING to form a single character.
831 If so, return the number of bytes after POS/POS_BYTE
832 which combine in this way. Otherwise, return 0. */
833
834 int
835 count_combining_after (const unsigned char *string,
836 EMACS_INT length, EMACS_INT pos, EMACS_INT pos_byte)
837 {
838 EMACS_INT opos_byte = pos_byte;
839 EMACS_INT i;
840 EMACS_INT bytes;
841 unsigned char *bufp;
842
843 if (NILP (current_buffer->enable_multibyte_characters))
844 return 0;
845
846 /* At first, we can exclude the following cases:
847 (1) The last byte of STRING is an ASCII.
848 (2) POS is the last of the current buffer.
849 (3) A character at POS can't be a following byte of multibyte
850 character. */
851 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
852 return 0;
853 if (pos_byte == Z_BYTE) /* case (2) */
854 return 0;
855 bufp = BYTE_POS_ADDR (pos_byte);
856 if (CHAR_HEAD_P (*bufp)) /* case (3) */
857 return 0;
858
859 i = length - 1;
860 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
861 {
862 i--;
863 }
864 if (i < 0)
865 {
866 /* All characters in STRING are not character head. We must
867 check also preceding bytes at POS. We are sure that the gap
868 is at POS. */
869 unsigned char *p = BEG_ADDR;
870 i = pos_byte - 2;
871 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
872 i--;
873 if (i < 0 || !LEADING_CODE_P (p[i]))
874 return 0;
875
876 bytes = BYTES_BY_CHAR_HEAD (p[i]);
877 return (bytes <= pos_byte - 1 - i + length
878 ? 0
879 : bytes - (pos_byte - 1 - i + length));
880 }
881 if (!LEADING_CODE_P (string[i]))
882 return 0;
883
884 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
885 bufp++, pos_byte++;
886 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
887
888 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
889 }
890
891 #endif
892
893 \f
894 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
895 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
896 are the same as in insert_1. */
897
898 void
899 insert_1_both (const unsigned char *string,
900 EMACS_INT nchars, EMACS_INT nbytes,
901 int inherit, int prepare, int before_markers)
902 {
903 if (nchars == 0)
904 return;
905
906 if (NILP (current_buffer->enable_multibyte_characters))
907 nchars = nbytes;
908
909 if (prepare)
910 /* Do this before moving and increasing the gap,
911 because the before-change hooks might move the gap
912 or make it smaller. */
913 prepare_to_modify_buffer (PT, PT, NULL);
914
915 if (PT != GPT)
916 move_gap_both (PT, PT_BYTE);
917 if (GAP_SIZE < nbytes)
918 make_gap (nbytes - GAP_SIZE);
919
920 #ifdef BYTE_COMBINING_DEBUG
921 if (count_combining_before (string, nbytes, PT, PT_BYTE)
922 || count_combining_after (string, nbytes, PT, PT_BYTE))
923 abort ();
924 #endif
925
926 /* Record deletion of the surrounding text that combines with
927 the insertion. This, together with recording the insertion,
928 will add up to the right stuff in the undo list. */
929 record_insert (PT, nchars);
930 MODIFF++;
931 CHARS_MODIFF = MODIFF;
932
933 memcpy (GPT_ADDR, string, nbytes);
934
935 GAP_SIZE -= nbytes;
936 GPT += nchars;
937 ZV += nchars;
938 Z += nchars;
939 GPT_BYTE += nbytes;
940 ZV_BYTE += nbytes;
941 Z_BYTE += nbytes;
942 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
943
944 if (GPT_BYTE < GPT)
945 abort ();
946
947 /* The insert may have been in the unchanged region, so check again. */
948 if (Z - GPT < END_UNCHANGED)
949 END_UNCHANGED = Z - GPT;
950
951 adjust_overlays_for_insert (PT, nchars);
952 adjust_markers_for_insert (PT, PT_BYTE,
953 PT + nchars, PT_BYTE + nbytes,
954 before_markers);
955
956 if (BUF_INTERVALS (current_buffer) != 0)
957 offset_intervals (current_buffer, PT, nchars);
958
959 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
960 set_text_properties (make_number (PT), make_number (PT + nchars),
961 Qnil, Qnil, Qnil);
962
963 adjust_point (nchars, nbytes);
964
965 CHECK_MARKERS ();
966 }
967 \f
968 /* Insert the part of the text of STRING, a Lisp object assumed to be
969 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
970 starting at position POS / POS_BYTE. If the text of STRING has properties,
971 copy them into the buffer.
972
973 It does not work to use `insert' for this, because a GC could happen
974 before we copy the stuff into the buffer, and relocate the string
975 without insert noticing. */
976
977 void
978 insert_from_string (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
979 EMACS_INT length, EMACS_INT length_byte, int inherit)
980 {
981 EMACS_INT opoint = PT;
982
983 if (SCHARS (string) == 0)
984 return;
985
986 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
987 inherit, 0);
988 signal_after_change (opoint, 0, PT - opoint);
989 update_compositions (opoint, PT, CHECK_BORDER);
990 }
991
992 /* Like `insert_from_string' except that all markers pointing
993 at the place where the insertion happens are adjusted to point after it. */
994
995 void
996 insert_from_string_before_markers (Lisp_Object string,
997 EMACS_INT pos, EMACS_INT pos_byte,
998 EMACS_INT length, EMACS_INT length_byte,
999 int inherit)
1000 {
1001 EMACS_INT opoint = PT;
1002
1003 if (SCHARS (string) == 0)
1004 return;
1005
1006 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1007 inherit, 1);
1008 signal_after_change (opoint, 0, PT - opoint);
1009 update_compositions (opoint, PT, CHECK_BORDER);
1010 }
1011
1012 /* Subroutine of the insertion functions above. */
1013
1014 static void
1015 insert_from_string_1 (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
1016 EMACS_INT nchars, EMACS_INT nbytes,
1017 int inherit, int before_markers)
1018 {
1019 struct gcpro gcpro1;
1020 EMACS_INT outgoing_nbytes = nbytes;
1021 INTERVAL intervals;
1022
1023 /* Make OUTGOING_NBYTES describe the text
1024 as it will be inserted in this buffer. */
1025
1026 if (NILP (current_buffer->enable_multibyte_characters))
1027 outgoing_nbytes = nchars;
1028 else if (! STRING_MULTIBYTE (string))
1029 outgoing_nbytes
1030 = count_size_as_multibyte (SDATA (string) + pos_byte,
1031 nbytes);
1032
1033 GCPRO1 (string);
1034 /* Do this before moving and increasing the gap,
1035 because the before-change hooks might move the gap
1036 or make it smaller. */
1037 prepare_to_modify_buffer (PT, PT, NULL);
1038
1039 if (PT != GPT)
1040 move_gap_both (PT, PT_BYTE);
1041 if (GAP_SIZE < outgoing_nbytes)
1042 make_gap (outgoing_nbytes - GAP_SIZE);
1043 UNGCPRO;
1044
1045 /* Copy the string text into the buffer, perhaps converting
1046 between single-byte and multibyte. */
1047 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
1048 STRING_MULTIBYTE (string),
1049 ! NILP (current_buffer->enable_multibyte_characters));
1050
1051 #ifdef BYTE_COMBINING_DEBUG
1052 /* We have copied text into the gap, but we have not altered
1053 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1054 to these functions and get the same results as we would
1055 have got earlier on. Meanwhile, PT_ADDR does point to
1056 the text that has been stored by copy_text. */
1057 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1058 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1059 abort ();
1060 #endif
1061
1062 record_insert (PT, nchars);
1063 MODIFF++;
1064 CHARS_MODIFF = MODIFF;
1065
1066 GAP_SIZE -= outgoing_nbytes;
1067 GPT += nchars;
1068 ZV += nchars;
1069 Z += nchars;
1070 GPT_BYTE += outgoing_nbytes;
1071 ZV_BYTE += outgoing_nbytes;
1072 Z_BYTE += outgoing_nbytes;
1073 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1074
1075 if (GPT_BYTE < GPT)
1076 abort ();
1077
1078 /* The insert may have been in the unchanged region, so check again. */
1079 if (Z - GPT < END_UNCHANGED)
1080 END_UNCHANGED = Z - GPT;
1081
1082 adjust_overlays_for_insert (PT, nchars);
1083 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1084 PT_BYTE + outgoing_nbytes,
1085 before_markers);
1086
1087 offset_intervals (current_buffer, PT, nchars);
1088
1089 intervals = STRING_INTERVALS (string);
1090 /* Get the intervals for the part of the string we are inserting. */
1091 if (nbytes < SBYTES (string))
1092 intervals = copy_intervals (intervals, pos, nchars);
1093
1094 /* Insert those intervals. */
1095 graft_intervals_into_buffer (intervals, PT, nchars,
1096 current_buffer, inherit);
1097
1098 adjust_point (nchars, outgoing_nbytes);
1099
1100 CHECK_MARKERS ();
1101 }
1102 \f
1103 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1104 starting at GPT_ADDR. */
1105
1106 void
1107 insert_from_gap (EMACS_INT nchars, EMACS_INT nbytes)
1108 {
1109 if (NILP (current_buffer->enable_multibyte_characters))
1110 nchars = nbytes;
1111
1112 record_insert (GPT, nchars);
1113 MODIFF++;
1114
1115 GAP_SIZE -= nbytes;
1116 GPT += nchars;
1117 ZV += nchars;
1118 Z += nchars;
1119 GPT_BYTE += nbytes;
1120 ZV_BYTE += nbytes;
1121 Z_BYTE += nbytes;
1122 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1123
1124 if (GPT_BYTE < GPT)
1125 abort ();
1126
1127 adjust_overlays_for_insert (GPT - nchars, nchars);
1128 adjust_markers_for_insert (GPT - nchars, GPT_BYTE - nbytes,
1129 GPT, GPT_BYTE, 0);
1130
1131 if (BUF_INTERVALS (current_buffer) != 0)
1132 {
1133 offset_intervals (current_buffer, GPT - nchars, nchars);
1134 graft_intervals_into_buffer (NULL_INTERVAL, GPT - nchars, nchars,
1135 current_buffer, 0);
1136 }
1137
1138 if (GPT - nchars < PT)
1139 adjust_point (nchars, nbytes);
1140
1141 CHECK_MARKERS ();
1142 }
1143 \f
1144 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1145 current buffer. If the text in BUF has properties, they are absorbed
1146 into the current buffer.
1147
1148 It does not work to use `insert' for this, because a malloc could happen
1149 and relocate BUF's text before the copy happens. */
1150
1151 void
1152 insert_from_buffer (struct buffer *buf,
1153 EMACS_INT charpos, EMACS_INT nchars, int inherit)
1154 {
1155 EMACS_INT opoint = PT;
1156
1157 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1158 signal_after_change (opoint, 0, PT - opoint);
1159 update_compositions (opoint, PT, CHECK_BORDER);
1160 }
1161
1162 static void
1163 insert_from_buffer_1 (struct buffer *buf,
1164 EMACS_INT from, EMACS_INT nchars, int inherit)
1165 {
1166 register Lisp_Object temp;
1167 EMACS_INT chunk, chunk_expanded;
1168 EMACS_INT from_byte = buf_charpos_to_bytepos (buf, from);
1169 EMACS_INT to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1170 EMACS_INT incoming_nbytes = to_byte - from_byte;
1171 EMACS_INT outgoing_nbytes = incoming_nbytes;
1172 INTERVAL intervals;
1173
1174 /* Make OUTGOING_NBYTES describe the text
1175 as it will be inserted in this buffer. */
1176
1177 if (NILP (current_buffer->enable_multibyte_characters))
1178 outgoing_nbytes = nchars;
1179 else if (NILP (buf->enable_multibyte_characters))
1180 {
1181 EMACS_INT outgoing_before_gap = 0;
1182 EMACS_INT outgoing_after_gap = 0;
1183
1184 if (from < BUF_GPT (buf))
1185 {
1186 chunk = BUF_GPT_BYTE (buf) - from_byte;
1187 if (chunk > incoming_nbytes)
1188 chunk = incoming_nbytes;
1189 outgoing_before_gap
1190 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1191 chunk);
1192 }
1193 else
1194 chunk = 0;
1195
1196 if (chunk < incoming_nbytes)
1197 outgoing_after_gap
1198 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1199 from_byte + chunk),
1200 incoming_nbytes - chunk);
1201
1202 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1203 }
1204
1205 /* Make sure point-max won't overflow after this insertion. */
1206 XSETINT (temp, outgoing_nbytes + Z);
1207 if (outgoing_nbytes + Z != XINT (temp))
1208 error ("Maximum buffer size exceeded");
1209
1210 /* Do this before moving and increasing the gap,
1211 because the before-change hooks might move the gap
1212 or make it smaller. */
1213 prepare_to_modify_buffer (PT, PT, NULL);
1214
1215 if (PT != GPT)
1216 move_gap_both (PT, PT_BYTE);
1217 if (GAP_SIZE < outgoing_nbytes)
1218 make_gap (outgoing_nbytes - GAP_SIZE);
1219
1220 if (from < BUF_GPT (buf))
1221 {
1222 chunk = BUF_GPT_BYTE (buf) - from_byte;
1223 if (chunk > incoming_nbytes)
1224 chunk = incoming_nbytes;
1225 /* Record number of output bytes, so we know where
1226 to put the output from the second copy_text. */
1227 chunk_expanded
1228 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1229 GPT_ADDR, chunk,
1230 ! NILP (buf->enable_multibyte_characters),
1231 ! NILP (current_buffer->enable_multibyte_characters));
1232 }
1233 else
1234 chunk_expanded = chunk = 0;
1235
1236 if (chunk < incoming_nbytes)
1237 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1238 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1239 ! NILP (buf->enable_multibyte_characters),
1240 ! NILP (current_buffer->enable_multibyte_characters));
1241
1242 #ifdef BYTE_COMBINING_DEBUG
1243 /* We have copied text into the gap, but we have not altered
1244 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1245 to these functions and get the same results as we would
1246 have got earlier on. Meanwhile, GPT_ADDR does point to
1247 the text that has been stored by copy_text. */
1248 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1249 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1250 abort ();
1251 #endif
1252
1253 record_insert (PT, nchars);
1254 MODIFF++;
1255 CHARS_MODIFF = MODIFF;
1256
1257 GAP_SIZE -= outgoing_nbytes;
1258 GPT += nchars;
1259 ZV += nchars;
1260 Z += nchars;
1261 GPT_BYTE += outgoing_nbytes;
1262 ZV_BYTE += outgoing_nbytes;
1263 Z_BYTE += outgoing_nbytes;
1264 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1265
1266 if (GPT_BYTE < GPT)
1267 abort ();
1268
1269 /* The insert may have been in the unchanged region, so check again. */
1270 if (Z - GPT < END_UNCHANGED)
1271 END_UNCHANGED = Z - GPT;
1272
1273 adjust_overlays_for_insert (PT, nchars);
1274 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1275 PT_BYTE + outgoing_nbytes,
1276 0);
1277
1278 if (BUF_INTERVALS (current_buffer) != 0)
1279 offset_intervals (current_buffer, PT, nchars);
1280
1281 /* Get the intervals for the part of the string we are inserting. */
1282 intervals = BUF_INTERVALS (buf);
1283 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1284 {
1285 if (buf == current_buffer && PT <= from)
1286 from += nchars;
1287 intervals = copy_intervals (intervals, from, nchars);
1288 }
1289
1290 /* Insert those intervals. */
1291 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1292
1293 adjust_point (nchars, outgoing_nbytes);
1294 }
1295 \f
1296 /* Record undo information and adjust markers and position keepers for
1297 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1298 chars (LEN_BYTE bytes) which resides in the gap just after
1299 GPT_ADDR.
1300
1301 PREV_TEXT nil means the new text was just inserted. */
1302
1303 void
1304 adjust_after_replace (EMACS_INT from, EMACS_INT from_byte,
1305 Lisp_Object prev_text, EMACS_INT len, EMACS_INT len_byte)
1306 {
1307 EMACS_INT nchars_del = 0, nbytes_del = 0;
1308
1309 #ifdef BYTE_COMBINING_DEBUG
1310 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1311 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1312 abort ();
1313 #endif
1314
1315 if (STRINGP (prev_text))
1316 {
1317 nchars_del = SCHARS (prev_text);
1318 nbytes_del = SBYTES (prev_text);
1319 }
1320
1321 /* Update various buffer positions for the new text. */
1322 GAP_SIZE -= len_byte;
1323 ZV += len; Z+= len;
1324 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1325 GPT += len; GPT_BYTE += len_byte;
1326 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1327
1328 if (nchars_del > 0)
1329 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1330 len, len_byte);
1331 else
1332 adjust_markers_for_insert (from, from_byte,
1333 from + len, from_byte + len_byte, 0);
1334
1335 if (! EQ (current_buffer->undo_list, Qt))
1336 {
1337 if (nchars_del > 0)
1338 record_delete (from, prev_text);
1339 record_insert (from, len);
1340 }
1341
1342 if (len > nchars_del)
1343 adjust_overlays_for_insert (from, len - nchars_del);
1344 else if (len < nchars_del)
1345 adjust_overlays_for_delete (from, nchars_del - len);
1346 if (BUF_INTERVALS (current_buffer) != 0)
1347 {
1348 offset_intervals (current_buffer, from, len - nchars_del);
1349 }
1350
1351 if (from < PT)
1352 adjust_point (len - nchars_del, len_byte - nbytes_del);
1353
1354 /* As byte combining will decrease Z, we must check this again. */
1355 if (Z - GPT < END_UNCHANGED)
1356 END_UNCHANGED = Z - GPT;
1357
1358 CHECK_MARKERS ();
1359
1360 if (len == 0)
1361 evaporate_overlays (from);
1362 MODIFF++;
1363 CHARS_MODIFF = MODIFF;
1364 }
1365
1366 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1367 This is for use when undo is not enabled in the current buffer. */
1368
1369 void
1370 adjust_after_replace_noundo (EMACS_INT from, EMACS_INT from_byte,
1371 EMACS_INT nchars_del, EMACS_INT nbytes_del,
1372 EMACS_INT len, EMACS_INT len_byte)
1373 {
1374 #ifdef BYTE_COMBINING_DEBUG
1375 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1376 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1377 abort ();
1378 #endif
1379
1380 /* Update various buffer positions for the new text. */
1381 GAP_SIZE -= len_byte;
1382 ZV += len; Z+= len;
1383 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1384 GPT += len; GPT_BYTE += len_byte;
1385 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1386
1387 if (nchars_del > 0)
1388 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1389 len, len_byte);
1390 else
1391 adjust_markers_for_insert (from, from_byte,
1392 from + len, from_byte + len_byte, 0);
1393
1394 if (len > nchars_del)
1395 adjust_overlays_for_insert (from, len - nchars_del);
1396 else if (len < nchars_del)
1397 adjust_overlays_for_delete (from, nchars_del - len);
1398 if (BUF_INTERVALS (current_buffer) != 0)
1399 {
1400 offset_intervals (current_buffer, from, len - nchars_del);
1401 }
1402
1403 if (from < PT)
1404 adjust_point (len - nchars_del, len_byte - nbytes_del);
1405
1406 /* As byte combining will decrease Z, we must check this again. */
1407 if (Z - GPT < END_UNCHANGED)
1408 END_UNCHANGED = Z - GPT;
1409
1410 CHECK_MARKERS ();
1411
1412 if (len == 0)
1413 evaporate_overlays (from);
1414 MODIFF++;
1415 CHARS_MODIFF = MODIFF;
1416 }
1417
1418 /* Record undo information, adjust markers and position keepers for an
1419 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1420 text already exists in the current buffer but character length (TO
1421 - FROM) may be incorrect, the correct length is NEWLEN. */
1422
1423 void
1424 adjust_after_insert (EMACS_INT from, EMACS_INT from_byte,
1425 EMACS_INT to, EMACS_INT to_byte, EMACS_INT newlen)
1426 {
1427 EMACS_INT len = to - from, len_byte = to_byte - from_byte;
1428
1429 if (GPT != to)
1430 move_gap_both (to, to_byte);
1431 GAP_SIZE += len_byte;
1432 GPT -= len; GPT_BYTE -= len_byte;
1433 ZV -= len; ZV_BYTE -= len_byte;
1434 Z -= len; Z_BYTE -= len_byte;
1435 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1436 }
1437 \f
1438 /* Replace the text from character positions FROM to TO with NEW,
1439 If PREPARE is nonzero, call prepare_to_modify_buffer.
1440 If INHERIT, the newly inserted text should inherit text properties
1441 from the surrounding non-deleted text. */
1442
1443 /* Note that this does not yet handle markers quite right.
1444 Also it needs to record a single undo-entry that does a replacement
1445 rather than a separate delete and insert.
1446 That way, undo will also handle markers properly.
1447
1448 But if MARKERS is 0, don't relocate markers. */
1449
1450 void
1451 replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
1452 int prepare, int inherit, int markers)
1453 {
1454 EMACS_INT inschars = SCHARS (new);
1455 EMACS_INT insbytes = SBYTES (new);
1456 EMACS_INT from_byte, to_byte;
1457 EMACS_INT nbytes_del, nchars_del;
1458 register Lisp_Object temp;
1459 struct gcpro gcpro1;
1460 INTERVAL intervals;
1461 EMACS_INT outgoing_insbytes = insbytes;
1462 Lisp_Object deletion;
1463
1464 CHECK_MARKERS ();
1465
1466 GCPRO1 (new);
1467 deletion = Qnil;
1468
1469 if (prepare)
1470 {
1471 EMACS_INT range_length = to - from;
1472 prepare_to_modify_buffer (from, to, &from);
1473 to = from + range_length;
1474 }
1475
1476 UNGCPRO;
1477
1478 /* Make args be valid */
1479 if (from < BEGV)
1480 from = BEGV;
1481 if (to > ZV)
1482 to = ZV;
1483
1484 from_byte = CHAR_TO_BYTE (from);
1485 to_byte = CHAR_TO_BYTE (to);
1486
1487 nchars_del = to - from;
1488 nbytes_del = to_byte - from_byte;
1489
1490 if (nbytes_del <= 0 && insbytes == 0)
1491 return;
1492
1493 /* Make OUTGOING_INSBYTES describe the text
1494 as it will be inserted in this buffer. */
1495
1496 if (NILP (current_buffer->enable_multibyte_characters))
1497 outgoing_insbytes = inschars;
1498 else if (! STRING_MULTIBYTE (new))
1499 outgoing_insbytes
1500 = count_size_as_multibyte (SDATA (new), insbytes);
1501
1502 /* Make sure point-max won't overflow after this insertion. */
1503 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1504 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1505 error ("Maximum buffer size exceeded");
1506
1507 GCPRO1 (new);
1508
1509 /* Make sure the gap is somewhere in or next to what we are deleting. */
1510 if (from > GPT)
1511 gap_right (from, from_byte);
1512 if (to < GPT)
1513 gap_left (to, to_byte, 0);
1514
1515 /* Even if we don't record for undo, we must keep the original text
1516 because we may have to recover it because of inappropriate byte
1517 combining. */
1518 if (! EQ (current_buffer->undo_list, Qt))
1519 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1520
1521 GAP_SIZE += nbytes_del;
1522 ZV -= nchars_del;
1523 Z -= nchars_del;
1524 ZV_BYTE -= nbytes_del;
1525 Z_BYTE -= nbytes_del;
1526 GPT = from;
1527 GPT_BYTE = from_byte;
1528 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1529
1530 if (GPT_BYTE < GPT)
1531 abort ();
1532
1533 if (GPT - BEG < BEG_UNCHANGED)
1534 BEG_UNCHANGED = GPT - BEG;
1535 if (Z - GPT < END_UNCHANGED)
1536 END_UNCHANGED = Z - GPT;
1537
1538 if (GAP_SIZE < insbytes)
1539 make_gap (insbytes - GAP_SIZE);
1540
1541 /* Copy the string text into the buffer, perhaps converting
1542 between single-byte and multibyte. */
1543 copy_text (SDATA (new), GPT_ADDR, insbytes,
1544 STRING_MULTIBYTE (new),
1545 ! NILP (current_buffer->enable_multibyte_characters));
1546
1547 #ifdef BYTE_COMBINING_DEBUG
1548 /* We have copied text into the gap, but we have not marked
1549 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1550 here, for both the previous text and the following text.
1551 Meanwhile, GPT_ADDR does point to
1552 the text that has been stored by copy_text. */
1553 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1554 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1555 abort ();
1556 #endif
1557
1558 if (! EQ (current_buffer->undo_list, Qt))
1559 {
1560 /* Record the insertion first, so that when we undo,
1561 the deletion will be undone first. Thus, undo
1562 will insert before deleting, and thus will keep
1563 the markers before and after this text separate. */
1564 record_insert (from + SCHARS (deletion), inschars);
1565 record_delete (from, deletion);
1566 }
1567
1568 GAP_SIZE -= outgoing_insbytes;
1569 GPT += inschars;
1570 ZV += inschars;
1571 Z += inschars;
1572 GPT_BYTE += outgoing_insbytes;
1573 ZV_BYTE += outgoing_insbytes;
1574 Z_BYTE += outgoing_insbytes;
1575 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1576
1577 if (GPT_BYTE < GPT)
1578 abort ();
1579
1580 /* Adjust the overlay center as needed. This must be done after
1581 adjusting the markers that bound the overlays. */
1582 adjust_overlays_for_delete (from, nchars_del);
1583 adjust_overlays_for_insert (from, inschars);
1584
1585 /* Adjust markers for the deletion and the insertion. */
1586 if (markers)
1587 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1588 inschars, outgoing_insbytes);
1589
1590 offset_intervals (current_buffer, from, inschars - nchars_del);
1591
1592 /* Get the intervals for the part of the string we are inserting--
1593 not including the combined-before bytes. */
1594 intervals = STRING_INTERVALS (new);
1595 /* Insert those intervals. */
1596 graft_intervals_into_buffer (intervals, from, inschars,
1597 current_buffer, inherit);
1598
1599 /* Relocate point as if it were a marker. */
1600 if (from < PT)
1601 adjust_point ((from + inschars - (PT < to ? PT : to)),
1602 (from_byte + outgoing_insbytes
1603 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1604
1605 if (outgoing_insbytes == 0)
1606 evaporate_overlays (from);
1607
1608 CHECK_MARKERS ();
1609
1610 MODIFF++;
1611 CHARS_MODIFF = MODIFF;
1612 UNGCPRO;
1613
1614 signal_after_change (from, nchars_del, GPT - from);
1615 update_compositions (from, GPT, CHECK_BORDER);
1616 }
1617 \f
1618 /* Replace the text from character positions FROM to TO with
1619 the text in INS of length INSCHARS.
1620 Keep the text properties that applied to the old characters
1621 (extending them to all the new chars if there are more new chars).
1622
1623 Note that this does not yet handle markers quite right.
1624
1625 If MARKERS is nonzero, relocate markers.
1626
1627 Unlike most functions at this level, never call
1628 prepare_to_modify_buffer and never call signal_after_change. */
1629
1630 void
1631 replace_range_2 (EMACS_INT from, EMACS_INT from_byte,
1632 EMACS_INT to, EMACS_INT to_byte,
1633 const char *ins, EMACS_INT inschars, EMACS_INT insbytes,
1634 int markers)
1635 {
1636 EMACS_INT nbytes_del, nchars_del;
1637 Lisp_Object temp;
1638
1639 CHECK_MARKERS ();
1640
1641 nchars_del = to - from;
1642 nbytes_del = to_byte - from_byte;
1643
1644 if (nbytes_del <= 0 && insbytes == 0)
1645 return;
1646
1647 /* Make sure point-max won't overflow after this insertion. */
1648 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1649 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1650 error ("Maximum buffer size exceeded");
1651
1652 /* Make sure the gap is somewhere in or next to what we are deleting. */
1653 if (from > GPT)
1654 gap_right (from, from_byte);
1655 if (to < GPT)
1656 gap_left (to, to_byte, 0);
1657
1658 GAP_SIZE += nbytes_del;
1659 ZV -= nchars_del;
1660 Z -= nchars_del;
1661 ZV_BYTE -= nbytes_del;
1662 Z_BYTE -= nbytes_del;
1663 GPT = from;
1664 GPT_BYTE = from_byte;
1665 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1666
1667 if (GPT_BYTE < GPT)
1668 abort ();
1669
1670 if (GPT - BEG < BEG_UNCHANGED)
1671 BEG_UNCHANGED = GPT - BEG;
1672 if (Z - GPT < END_UNCHANGED)
1673 END_UNCHANGED = Z - GPT;
1674
1675 if (GAP_SIZE < insbytes)
1676 make_gap (insbytes - GAP_SIZE);
1677
1678 /* Copy the replacement text into the buffer. */
1679 memcpy (GPT_ADDR, ins, insbytes);
1680
1681 #ifdef BYTE_COMBINING_DEBUG
1682 /* We have copied text into the gap, but we have not marked
1683 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1684 here, for both the previous text and the following text.
1685 Meanwhile, GPT_ADDR does point to
1686 the text that has been stored by copy_text. */
1687 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1688 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1689 abort ();
1690 #endif
1691
1692 GAP_SIZE -= insbytes;
1693 GPT += inschars;
1694 ZV += inschars;
1695 Z += inschars;
1696 GPT_BYTE += insbytes;
1697 ZV_BYTE += insbytes;
1698 Z_BYTE += insbytes;
1699 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1700
1701 if (GPT_BYTE < GPT)
1702 abort ();
1703
1704 /* Adjust the overlay center as needed. This must be done after
1705 adjusting the markers that bound the overlays. */
1706 if (nchars_del != inschars)
1707 {
1708 adjust_overlays_for_insert (from, inschars);
1709 adjust_overlays_for_delete (from + inschars, nchars_del);
1710 }
1711
1712 /* Adjust markers for the deletion and the insertion. */
1713 if (markers
1714 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1715 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1716 inschars, insbytes);
1717
1718 offset_intervals (current_buffer, from, inschars - nchars_del);
1719
1720 /* Relocate point as if it were a marker. */
1721 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1722 {
1723 if (PT < to)
1724 /* PT was within the deleted text. Move it to FROM. */
1725 adjust_point (from - PT, from_byte - PT_BYTE);
1726 else
1727 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1728 }
1729
1730 if (insbytes == 0)
1731 evaporate_overlays (from);
1732
1733 CHECK_MARKERS ();
1734
1735 MODIFF++;
1736 CHARS_MODIFF = MODIFF;
1737 }
1738 \f
1739 /* Delete characters in current buffer
1740 from FROM up to (but not including) TO.
1741 If TO comes before FROM, we delete nothing. */
1742
1743 void
1744 del_range (EMACS_INT from, EMACS_INT to)
1745 {
1746 del_range_1 (from, to, 1, 0);
1747 }
1748
1749 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1750 RET_STRING says to return the deleted text. */
1751
1752 Lisp_Object
1753 del_range_1 (EMACS_INT from, EMACS_INT to, int prepare, int ret_string)
1754 {
1755 EMACS_INT from_byte, to_byte;
1756 Lisp_Object deletion;
1757 struct gcpro gcpro1;
1758
1759 /* Make args be valid */
1760 if (from < BEGV)
1761 from = BEGV;
1762 if (to > ZV)
1763 to = ZV;
1764
1765 if (to <= from)
1766 return Qnil;
1767
1768 if (prepare)
1769 {
1770 EMACS_INT range_length = to - from;
1771 prepare_to_modify_buffer (from, to, &from);
1772 to = min (ZV, from + range_length);
1773 }
1774
1775 from_byte = CHAR_TO_BYTE (from);
1776 to_byte = CHAR_TO_BYTE (to);
1777
1778 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1779 GCPRO1(deletion);
1780 signal_after_change (from, to - from, 0);
1781 update_compositions (from, from, CHECK_HEAD);
1782 UNGCPRO;
1783 return deletion;
1784 }
1785
1786 /* Like del_range_1 but args are byte positions, not char positions. */
1787
1788 void
1789 del_range_byte (EMACS_INT from_byte, EMACS_INT to_byte, int prepare)
1790 {
1791 EMACS_INT from, to;
1792
1793 /* Make args be valid */
1794 if (from_byte < BEGV_BYTE)
1795 from_byte = BEGV_BYTE;
1796 if (to_byte > ZV_BYTE)
1797 to_byte = ZV_BYTE;
1798
1799 if (to_byte <= from_byte)
1800 return;
1801
1802 from = BYTE_TO_CHAR (from_byte);
1803 to = BYTE_TO_CHAR (to_byte);
1804
1805 if (prepare)
1806 {
1807 EMACS_INT old_from = from, old_to = Z - to;
1808 EMACS_INT range_length = to - from;
1809 prepare_to_modify_buffer (from, to, &from);
1810 to = from + range_length;
1811
1812 if (old_from != from)
1813 from_byte = CHAR_TO_BYTE (from);
1814 if (to > ZV)
1815 {
1816 to = ZV;
1817 to_byte = ZV_BYTE;
1818 }
1819 else if (old_to == Z - to)
1820 to_byte = CHAR_TO_BYTE (to);
1821 }
1822
1823 del_range_2 (from, from_byte, to, to_byte, 0);
1824 signal_after_change (from, to - from, 0);
1825 update_compositions (from, from, CHECK_HEAD);
1826 }
1827
1828 /* Like del_range_1, but positions are specified both as charpos
1829 and bytepos. */
1830
1831 void
1832 del_range_both (EMACS_INT from, EMACS_INT from_byte,
1833 EMACS_INT to, EMACS_INT to_byte, int prepare)
1834 {
1835 /* Make args be valid */
1836 if (from_byte < BEGV_BYTE)
1837 from_byte = BEGV_BYTE;
1838 if (to_byte > ZV_BYTE)
1839 to_byte = ZV_BYTE;
1840
1841 if (to_byte <= from_byte)
1842 return;
1843
1844 if (from < BEGV)
1845 from = BEGV;
1846 if (to > ZV)
1847 to = ZV;
1848
1849 if (prepare)
1850 {
1851 EMACS_INT old_from = from, old_to = Z - to;
1852 EMACS_INT range_length = to - from;
1853 prepare_to_modify_buffer (from, to, &from);
1854 to = from + range_length;
1855
1856 if (old_from != from)
1857 from_byte = CHAR_TO_BYTE (from);
1858 if (to > ZV)
1859 {
1860 to = ZV;
1861 to_byte = ZV_BYTE;
1862 }
1863 else if (old_to == Z - to)
1864 to_byte = CHAR_TO_BYTE (to);
1865 }
1866
1867 del_range_2 (from, from_byte, to, to_byte, 0);
1868 signal_after_change (from, to - from, 0);
1869 update_compositions (from, from, CHECK_HEAD);
1870 }
1871
1872 /* Delete a range of text, specified both as character positions
1873 and byte positions. FROM and TO are character positions,
1874 while FROM_BYTE and TO_BYTE are byte positions.
1875 If RET_STRING is true, the deleted area is returned as a string. */
1876
1877 Lisp_Object
1878 del_range_2 (EMACS_INT from, EMACS_INT from_byte,
1879 EMACS_INT to, EMACS_INT to_byte, int ret_string)
1880 {
1881 register EMACS_INT nbytes_del, nchars_del;
1882 Lisp_Object deletion;
1883
1884 CHECK_MARKERS ();
1885
1886 nchars_del = to - from;
1887 nbytes_del = to_byte - from_byte;
1888
1889 /* Make sure the gap is somewhere in or next to what we are deleting. */
1890 if (from > GPT)
1891 gap_right (from, from_byte);
1892 if (to < GPT)
1893 gap_left (to, to_byte, 0);
1894
1895 #ifdef BYTE_COMBINING_DEBUG
1896 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1897 Z_BYTE - to_byte, from, from_byte))
1898 abort ();
1899 #endif
1900
1901 if (ret_string || ! EQ (current_buffer->undo_list, Qt))
1902 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1903 else
1904 deletion = Qnil;
1905
1906 /* Relocate all markers pointing into the new, larger gap
1907 to point at the end of the text before the gap.
1908 Do this before recording the deletion,
1909 so that undo handles this after reinserting the text. */
1910 adjust_markers_for_delete (from, from_byte, to, to_byte);
1911
1912 if (! EQ (current_buffer->undo_list, Qt))
1913 record_delete (from, deletion);
1914 MODIFF++;
1915 CHARS_MODIFF = MODIFF;
1916
1917 /* Relocate point as if it were a marker. */
1918 if (from < PT)
1919 adjust_point (from - (PT < to ? PT : to),
1920 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1921
1922 offset_intervals (current_buffer, from, - nchars_del);
1923
1924 /* Adjust the overlay center as needed. This must be done after
1925 adjusting the markers that bound the overlays. */
1926 adjust_overlays_for_delete (from, nchars_del);
1927
1928 GAP_SIZE += nbytes_del;
1929 ZV_BYTE -= nbytes_del;
1930 Z_BYTE -= nbytes_del;
1931 ZV -= nchars_del;
1932 Z -= nchars_del;
1933 GPT = from;
1934 GPT_BYTE = from_byte;
1935 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1936 /* Put an anchor, unless called from decode_coding_object which
1937 needs to access the previous gap contents. */
1938 *(GPT_ADDR) = 0;
1939
1940 if (GPT_BYTE < GPT)
1941 abort ();
1942
1943 if (GPT - BEG < BEG_UNCHANGED)
1944 BEG_UNCHANGED = GPT - BEG;
1945 if (Z - GPT < END_UNCHANGED)
1946 END_UNCHANGED = Z - GPT;
1947
1948 CHECK_MARKERS ();
1949
1950 evaporate_overlays (from);
1951
1952 return deletion;
1953 }
1954 \f
1955 /* Call this if you're about to change the region of BUFFER from
1956 character positions START to END. This checks the read-only
1957 properties of the region, calls the necessary modification hooks,
1958 and warns the next redisplay that it should pay attention to that
1959 area.
1960
1961 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
1962 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1963
1964 void
1965 modify_region (struct buffer *buffer, EMACS_INT start, EMACS_INT end,
1966 int preserve_chars_modiff)
1967 {
1968 struct buffer *old_buffer = current_buffer;
1969
1970 if (buffer != old_buffer)
1971 set_buffer_internal (buffer);
1972
1973 prepare_to_modify_buffer (start, end, NULL);
1974
1975 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
1976
1977 if (MODIFF <= SAVE_MODIFF)
1978 record_first_change ();
1979 MODIFF++;
1980 if (! preserve_chars_modiff)
1981 CHARS_MODIFF = MODIFF;
1982
1983 buffer->point_before_scroll = Qnil;
1984
1985 if (buffer != old_buffer)
1986 set_buffer_internal (old_buffer);
1987 }
1988 \f
1989 /* Check that it is okay to modify the buffer between START and END,
1990 which are char positions.
1991
1992 Run the before-change-function, if any. If intervals are in use,
1993 verify that the text to be modified is not read-only, and call
1994 any modification properties the text may have.
1995
1996 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1997 by holding its value temporarily in a marker. */
1998
1999 void
2000 prepare_to_modify_buffer (EMACS_INT start, EMACS_INT end,
2001 EMACS_INT *preserve_ptr)
2002 {
2003 struct buffer *base_buffer;
2004
2005 if (!NILP (current_buffer->read_only))
2006 Fbarf_if_buffer_read_only ();
2007
2008 /* Let redisplay consider other windows than selected_window
2009 if modifying another buffer. */
2010 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2011 ++windows_or_buffers_changed;
2012
2013 if (BUF_INTERVALS (current_buffer) != 0)
2014 {
2015 if (preserve_ptr)
2016 {
2017 Lisp_Object preserve_marker;
2018 struct gcpro gcpro1;
2019 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
2020 GCPRO1 (preserve_marker);
2021 verify_interval_modification (current_buffer, start, end);
2022 *preserve_ptr = marker_position (preserve_marker);
2023 unchain_marker (XMARKER (preserve_marker));
2024 UNGCPRO;
2025 }
2026 else
2027 verify_interval_modification (current_buffer, start, end);
2028 }
2029
2030 /* For indirect buffers, use the base buffer to check clashes. */
2031 if (current_buffer->base_buffer != 0)
2032 base_buffer = current_buffer->base_buffer;
2033 else
2034 base_buffer = current_buffer;
2035
2036 #ifdef CLASH_DETECTION
2037 if (!NILP (base_buffer->file_truename)
2038 /* Make binding buffer-file-name to nil effective. */
2039 && !NILP (base_buffer->filename)
2040 && SAVE_MODIFF >= MODIFF)
2041 lock_file (base_buffer->file_truename);
2042 #else
2043 /* At least warn if this file has changed on disk since it was visited. */
2044 if (!NILP (base_buffer->filename)
2045 && SAVE_MODIFF >= MODIFF
2046 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2047 && !NILP (Ffile_exists_p (base_buffer->filename)))
2048 call1 (intern ("ask-user-about-supersession-threat"),
2049 base_buffer->filename);
2050 #endif /* not CLASH_DETECTION */
2051
2052 /* If `select-active-regions' is non-nil, save the region text. */
2053 if (!NILP (current_buffer->mark_active)
2054 && NILP (Vsaved_region_selection)
2055 && (EQ (Vselect_active_regions, Qonly)
2056 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
2057 : (!NILP (Vselect_active_regions)
2058 && !NILP (Vtransient_mark_mode))))
2059 {
2060 EMACS_INT b = XINT (Fmarker_position (current_buffer->mark));
2061 EMACS_INT e = PT;
2062 if (b < e)
2063 Vsaved_region_selection = make_buffer_string (b, e, 0);
2064 else if (b > e)
2065 Vsaved_region_selection = make_buffer_string (e, b, 0);
2066 }
2067
2068 signal_before_change (start, end, preserve_ptr);
2069
2070 if (current_buffer->newline_cache)
2071 invalidate_region_cache (current_buffer,
2072 current_buffer->newline_cache,
2073 start - BEG, Z - end);
2074 if (current_buffer->width_run_cache)
2075 invalidate_region_cache (current_buffer,
2076 current_buffer->width_run_cache,
2077 start - BEG, Z - end);
2078
2079 Vdeactivate_mark = Qt;
2080 }
2081 \f
2082 /* These macros work with an argument named `preserve_ptr'
2083 and a local variable named `preserve_marker'. */
2084
2085 #define PRESERVE_VALUE \
2086 if (preserve_ptr && NILP (preserve_marker)) \
2087 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2088
2089 #define RESTORE_VALUE \
2090 if (! NILP (preserve_marker)) \
2091 { \
2092 *preserve_ptr = marker_position (preserve_marker); \
2093 unchain_marker (XMARKER (preserve_marker)); \
2094 }
2095
2096 #define PRESERVE_START_END \
2097 if (NILP (start_marker)) \
2098 start_marker = Fcopy_marker (start, Qnil); \
2099 if (NILP (end_marker)) \
2100 end_marker = Fcopy_marker (end, Qnil);
2101
2102 #define FETCH_START \
2103 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2104
2105 #define FETCH_END \
2106 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2107
2108 /* Set a variable to nil if an error occurred.
2109 Don't change the variable if there was no error.
2110 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2111 VARIABLE is the variable to maybe set to nil.
2112 NO-ERROR-FLAG is nil if there was an error,
2113 anything else meaning no error (so this function does nothing). */
2114 Lisp_Object
2115 reset_var_on_error (Lisp_Object val)
2116 {
2117 if (NILP (XCDR (val)))
2118 Fset (XCAR (val), Qnil);
2119 return Qnil;
2120 }
2121
2122 /* Signal a change to the buffer immediately before it happens.
2123 START_INT and END_INT are the bounds of the text to be changed.
2124
2125 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2126 by holding its value temporarily in a marker. */
2127
2128 void
2129 signal_before_change (EMACS_INT start_int, EMACS_INT end_int,
2130 EMACS_INT *preserve_ptr)
2131 {
2132 Lisp_Object start, end;
2133 Lisp_Object start_marker, end_marker;
2134 Lisp_Object preserve_marker;
2135 struct gcpro gcpro1, gcpro2, gcpro3;
2136 int count = SPECPDL_INDEX ();
2137
2138 if (inhibit_modification_hooks)
2139 return;
2140
2141 start = make_number (start_int);
2142 end = make_number (end_int);
2143 preserve_marker = Qnil;
2144 start_marker = Qnil;
2145 end_marker = Qnil;
2146 GCPRO3 (preserve_marker, start_marker, end_marker);
2147
2148 specbind (Qinhibit_modification_hooks, Qt);
2149
2150 /* If buffer is unmodified, run a special hook for that case. */
2151 if (SAVE_MODIFF >= MODIFF
2152 && !NILP (Vfirst_change_hook)
2153 && !NILP (Vrun_hooks))
2154 {
2155 PRESERVE_VALUE;
2156 PRESERVE_START_END;
2157 call1 (Vrun_hooks, Qfirst_change_hook);
2158 }
2159
2160 /* Now run the before-change-functions if any. */
2161 if (!NILP (Vbefore_change_functions))
2162 {
2163 Lisp_Object args[3];
2164 Lisp_Object rvoe_arg = Fcons (Qbefore_change_functions, Qnil);
2165
2166 PRESERVE_VALUE;
2167 PRESERVE_START_END;
2168
2169 /* Mark before-change-functions to be reset to nil in case of error. */
2170 record_unwind_protect (reset_var_on_error, rvoe_arg);
2171
2172 /* Actually run the hook functions. */
2173 args[0] = Qbefore_change_functions;
2174 args[1] = FETCH_START;
2175 args[2] = FETCH_END;
2176 Frun_hook_with_args (3, args);
2177
2178 /* There was no error: unarm the reset_on_error. */
2179 XSETCDR (rvoe_arg, Qt);
2180 }
2181
2182 if (current_buffer->overlays_before || current_buffer->overlays_after)
2183 {
2184 PRESERVE_VALUE;
2185 report_overlay_modification (FETCH_START, FETCH_END, 0,
2186 FETCH_START, FETCH_END, Qnil);
2187 }
2188
2189 if (! NILP (start_marker))
2190 free_marker (start_marker);
2191 if (! NILP (end_marker))
2192 free_marker (end_marker);
2193 RESTORE_VALUE;
2194 UNGCPRO;
2195
2196 unbind_to (count, Qnil);
2197 }
2198
2199 /* Signal a change immediately after it happens.
2200 CHARPOS is the character position of the start of the changed text.
2201 LENDEL is the number of characters of the text before the change.
2202 (Not the whole buffer; just the part that was changed.)
2203 LENINS is the number of characters in that part of the text
2204 after the change. */
2205
2206 void
2207 signal_after_change (EMACS_INT charpos, EMACS_INT lendel, EMACS_INT lenins)
2208 {
2209 int count = SPECPDL_INDEX ();
2210 if (inhibit_modification_hooks)
2211 return;
2212
2213 /* If we are deferring calls to the after-change functions
2214 and there are no before-change functions,
2215 just record the args that we were going to use. */
2216 if (! NILP (Vcombine_after_change_calls)
2217 && NILP (Vbefore_change_functions)
2218 && !current_buffer->overlays_before
2219 && !current_buffer->overlays_after)
2220 {
2221 Lisp_Object elt;
2222
2223 if (!NILP (combine_after_change_list)
2224 && current_buffer != XBUFFER (combine_after_change_buffer))
2225 Fcombine_after_change_execute ();
2226
2227 elt = Fcons (make_number (charpos - BEG),
2228 Fcons (make_number (Z - (charpos - lendel + lenins)),
2229 Fcons (make_number (lenins - lendel), Qnil)));
2230 combine_after_change_list
2231 = Fcons (elt, combine_after_change_list);
2232 combine_after_change_buffer = Fcurrent_buffer ();
2233
2234 return;
2235 }
2236
2237 if (!NILP (combine_after_change_list))
2238 Fcombine_after_change_execute ();
2239
2240 specbind (Qinhibit_modification_hooks, Qt);
2241
2242 if (!NILP (Vafter_change_functions))
2243 {
2244 Lisp_Object args[4];
2245 Lisp_Object rvoe_arg = Fcons (Qafter_change_functions, Qnil);
2246
2247 /* Mark after-change-functions to be reset to nil in case of error. */
2248 record_unwind_protect (reset_var_on_error, rvoe_arg);
2249
2250 /* Actually run the hook functions. */
2251 args[0] = Qafter_change_functions;
2252 XSETFASTINT (args[1], charpos);
2253 XSETFASTINT (args[2], charpos + lenins);
2254 XSETFASTINT (args[3], lendel);
2255 Frun_hook_with_args (4, args);
2256
2257 /* There was no error: unarm the reset_on_error. */
2258 XSETCDR (rvoe_arg, Qt);
2259 }
2260
2261 if (current_buffer->overlays_before || current_buffer->overlays_after)
2262 report_overlay_modification (make_number (charpos),
2263 make_number (charpos + lenins),
2264 1,
2265 make_number (charpos),
2266 make_number (charpos + lenins),
2267 make_number (lendel));
2268
2269 /* After an insertion, call the text properties
2270 insert-behind-hooks or insert-in-front-hooks. */
2271 if (lendel == 0)
2272 report_interval_modification (make_number (charpos),
2273 make_number (charpos + lenins));
2274
2275 unbind_to (count, Qnil);
2276 }
2277
2278 Lisp_Object
2279 Fcombine_after_change_execute_1 (Lisp_Object val)
2280 {
2281 Vcombine_after_change_calls = val;
2282 return val;
2283 }
2284
2285 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2286 Scombine_after_change_execute, 0, 0, 0,
2287 doc: /* This function is for use internally in `combine-after-change-calls'. */)
2288 (void)
2289 {
2290 int count = SPECPDL_INDEX ();
2291 EMACS_INT beg, end, change;
2292 EMACS_INT begpos, endpos;
2293 Lisp_Object tail;
2294
2295 if (NILP (combine_after_change_list))
2296 return Qnil;
2297
2298 /* It is rare for combine_after_change_buffer to be invalid, but
2299 possible. It can happen when combine-after-change-calls is
2300 non-nil, and insertion calls a file handler (e.g. through
2301 lock_file) which scribbles into a temp file -- cyd */
2302 if (!BUFFERP (combine_after_change_buffer)
2303 || NILP (XBUFFER (combine_after_change_buffer)->name))
2304 {
2305 combine_after_change_list = Qnil;
2306 return Qnil;
2307 }
2308
2309 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2310
2311 Fset_buffer (combine_after_change_buffer);
2312
2313 /* # chars unchanged at beginning of buffer. */
2314 beg = Z - BEG;
2315 /* # chars unchanged at end of buffer. */
2316 end = beg;
2317 /* Total amount of insertion (negative for deletion). */
2318 change = 0;
2319
2320 /* Scan the various individual changes,
2321 accumulating the range info in BEG, END and CHANGE. */
2322 for (tail = combine_after_change_list; CONSP (tail);
2323 tail = XCDR (tail))
2324 {
2325 Lisp_Object elt;
2326 EMACS_INT thisbeg, thisend, thischange;
2327
2328 /* Extract the info from the next element. */
2329 elt = XCAR (tail);
2330 if (! CONSP (elt))
2331 continue;
2332 thisbeg = XINT (XCAR (elt));
2333
2334 elt = XCDR (elt);
2335 if (! CONSP (elt))
2336 continue;
2337 thisend = XINT (XCAR (elt));
2338
2339 elt = XCDR (elt);
2340 if (! CONSP (elt))
2341 continue;
2342 thischange = XINT (XCAR (elt));
2343
2344 /* Merge this range into the accumulated range. */
2345 change += thischange;
2346 if (thisbeg < beg)
2347 beg = thisbeg;
2348 if (thisend < end)
2349 end = thisend;
2350 }
2351
2352 /* Get the current start and end positions of the range
2353 that was changed. */
2354 begpos = BEG + beg;
2355 endpos = Z - end;
2356
2357 /* We are about to handle these, so discard them. */
2358 combine_after_change_list = Qnil;
2359
2360 /* Now run the after-change functions for real.
2361 Turn off the flag that defers them. */
2362 record_unwind_protect (Fcombine_after_change_execute_1,
2363 Vcombine_after_change_calls);
2364 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2365 update_compositions (begpos, endpos, CHECK_ALL);
2366
2367 return unbind_to (count, Qnil);
2368 }
2369 \f
2370 void
2371 syms_of_insdel (void)
2372 {
2373 staticpro (&combine_after_change_list);
2374 staticpro (&combine_after_change_buffer);
2375 combine_after_change_list = Qnil;
2376 combine_after_change_buffer = Qnil;
2377
2378 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
2379 doc: /* Non-nil means enable debugging checks for invalid marker positions. */);
2380 check_markers_debug_flag = 0;
2381 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2382 doc: /* Used internally by the `combine-after-change-calls' macro. */);
2383 Vcombine_after_change_calls = Qnil;
2384
2385 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks,
2386 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2387 This affects `before-change-functions' and `after-change-functions',
2388 as well as hooks attached to text properties and overlays. */);
2389 inhibit_modification_hooks = 0;
2390 Qinhibit_modification_hooks = intern_c_string ("inhibit-modification-hooks");
2391 staticpro (&Qinhibit_modification_hooks);
2392
2393 defsubr (&Scombine_after_change_execute);
2394 }
2395
2396 /* arch-tag: 9b34b886-47d7-465e-a234-299af411b23d
2397 (do not change this comment) */