(eshell-parse-argument-hook): Put `number' property on entire argument
[emacs.git] / src / insdel.c
blobf94f4f0067bcaaedae7aa554b5e931551875cd21
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
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
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.
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.
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/>. */
22 #include <config.h>
23 #include "lisp.h"
24 #include "intervals.h"
25 #include "buffer.h"
26 #include "character.h"
27 #include "window.h"
28 #include "blockinput.h"
29 #include "region-cache.h"
31 #ifndef NULL
32 #define NULL 0
33 #endif
35 static void insert_from_string_1 (Lisp_Object string,
36 EMACS_INT pos, EMACS_INT pos_byte,
37 EMACS_INT nchars, EMACS_INT nbytes,
38 int inherit, int before_markers);
39 static void insert_from_buffer_1 (struct buffer *buf,
40 EMACS_INT from, EMACS_INT nchars,
41 int inherit);
42 static void gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap);
43 static void gap_right (EMACS_INT charpos, EMACS_INT bytepos);
44 static void adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to,
45 EMACS_INT amount);
46 static void adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
47 EMACS_INT to, EMACS_INT to_byte,
48 int before_markers);
49 static void adjust_markers_for_replace (EMACS_INT, EMACS_INT, EMACS_INT,
50 EMACS_INT, EMACS_INT, EMACS_INT);
51 static void adjust_point (EMACS_INT nchars, EMACS_INT nbytes);
53 Lisp_Object Fcombine_after_change_execute ();
55 /* Non-nil means don't call the after-change-functions right away,
56 just record an element in Vcombine_after_change_calls_list. */
57 Lisp_Object Vcombine_after_change_calls;
59 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
60 describing changes which happened while combine_after_change_calls
61 was nonzero. We use this to decide how to call them
62 once the deferral ends.
64 In each element.
65 BEG-UNCHANGED is the number of chars before the changed range.
66 END-UNCHANGED is the number of chars after the changed range,
67 and CHANGE-AMOUNT is the number of characters inserted by the change
68 (negative for a deletion). */
69 Lisp_Object combine_after_change_list;
71 /* Buffer which combine_after_change_list is about. */
72 Lisp_Object combine_after_change_buffer;
74 Lisp_Object Qinhibit_modification_hooks;
77 /* Check all markers in the current buffer, looking for something invalid. */
79 static int check_markers_debug_flag;
81 #define CHECK_MARKERS() \
82 if (check_markers_debug_flag) \
83 check_markers (); \
84 else
86 void
87 check_markers ()
89 register struct Lisp_Marker *tail;
90 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
92 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
94 if (tail->buffer->text != current_buffer->text)
95 abort ();
96 if (tail->charpos > Z)
97 abort ();
98 if (tail->bytepos > Z_BYTE)
99 abort ();
100 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
101 abort ();
105 /* Move gap to position CHARPOS.
106 Note that this can quit! */
108 void
109 move_gap (EMACS_INT charpos)
111 move_gap_both (charpos, charpos_to_bytepos (charpos));
114 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
115 Note that this can quit! */
117 void
118 move_gap_both (EMACS_INT charpos, EMACS_INT bytepos)
120 if (bytepos < GPT_BYTE)
121 gap_left (charpos, bytepos, 0);
122 else if (bytepos > GPT_BYTE)
123 gap_right (charpos, bytepos);
126 /* Move the gap to a position less than the current GPT.
127 BYTEPOS describes the new position as a byte position,
128 and CHARPOS is the corresponding char position.
129 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
131 static void
132 gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap)
134 register unsigned char *to, *from;
135 register EMACS_INT i;
136 EMACS_INT new_s1;
138 if (!newgap)
139 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
141 i = GPT_BYTE;
142 to = GAP_END_ADDR;
143 from = GPT_ADDR;
144 new_s1 = GPT_BYTE;
146 /* Now copy the characters. To move the gap down,
147 copy characters up. */
149 while (1)
151 /* I gets number of characters left to copy. */
152 i = new_s1 - bytepos;
153 if (i == 0)
154 break;
155 /* If a quit is requested, stop copying now.
156 Change BYTEPOS to be where we have actually moved the gap to. */
157 if (QUITP)
159 bytepos = new_s1;
160 charpos = BYTE_TO_CHAR (bytepos);
161 break;
163 /* Move at most 32000 chars before checking again for a quit. */
164 if (i > 32000)
165 i = 32000;
166 #ifdef GAP_USE_BCOPY
167 if (i >= 128
168 /* bcopy is safe if the two areas of memory do not overlap
169 or on systems where bcopy is always safe for moving upward. */
170 && (BCOPY_UPWARD_SAFE
171 || to - from >= 128))
173 /* If overlap is not safe, avoid it by not moving too many
174 characters at once. */
175 if (!BCOPY_UPWARD_SAFE && i > to - from)
176 i = to - from;
177 new_s1 -= i;
178 from -= i, to -= i;
179 bcopy (from, to, i);
181 else
182 #endif
184 new_s1 -= i;
185 while (--i >= 0)
186 *--to = *--from;
190 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
191 BYTEPOS is where the loop above stopped, which may be what was specified
192 or may be where a quit was detected. */
193 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
194 GPT_BYTE = bytepos;
195 GPT = charpos;
196 if (bytepos < charpos)
197 abort ();
198 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
199 QUIT;
202 /* Move the gap to a position greater than than the current GPT.
203 BYTEPOS describes the new position as a byte position,
204 and CHARPOS is the corresponding char position. */
206 static void
207 gap_right (EMACS_INT charpos, EMACS_INT bytepos)
209 register unsigned char *to, *from;
210 register EMACS_INT i;
211 EMACS_INT new_s1;
213 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
215 i = GPT_BYTE;
216 from = GAP_END_ADDR;
217 to = GPT_ADDR;
218 new_s1 = GPT_BYTE;
220 /* Now copy the characters. To move the gap up,
221 copy characters down. */
223 while (1)
225 /* I gets number of characters left to copy. */
226 i = bytepos - new_s1;
227 if (i == 0)
228 break;
229 /* If a quit is requested, stop copying now.
230 Change BYTEPOS to be where we have actually moved the gap to. */
231 if (QUITP)
233 bytepos = new_s1;
234 charpos = BYTE_TO_CHAR (bytepos);
235 break;
237 /* Move at most 32000 chars before checking again for a quit. */
238 if (i > 32000)
239 i = 32000;
240 #ifdef GAP_USE_BCOPY
241 if (i >= 128
242 /* bcopy is safe if the two areas of memory do not overlap
243 or on systems where bcopy is always safe for moving downward. */
244 && (BCOPY_DOWNWARD_SAFE
245 || from - to >= 128))
247 /* If overlap is not safe, avoid it by not moving too many
248 characters at once. */
249 if (!BCOPY_DOWNWARD_SAFE && i > from - to)
250 i = from - to;
251 new_s1 += i;
252 bcopy (from, to, i);
253 from += i, to += i;
255 else
256 #endif
258 new_s1 += i;
259 while (--i >= 0)
260 *to++ = *from++;
264 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
265 - GAP_SIZE);
266 GPT = charpos;
267 GPT_BYTE = bytepos;
268 if (bytepos < charpos)
269 abort ();
270 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
271 QUIT;
274 /* Add AMOUNT to the byte position of every marker in the current buffer
275 whose current byte position is between FROM (exclusive) and TO (inclusive).
277 Also, any markers past the outside of that interval, in the direction
278 of adjustment, are first moved back to the near end of the interval
279 and then adjusted by AMOUNT.
281 When the latter adjustment is done, if AMOUNT is negative,
282 we record the adjustment for undo. (This case happens only for
283 deletion.)
285 The markers' character positions are not altered,
286 because gap motion does not affect character positions. */
288 int adjust_markers_test;
290 static void
291 adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to, EMACS_INT amount)
293 /* Now that a marker has a bytepos, not counting the gap,
294 nothing needs to be done here. */
295 #if 0
296 Lisp_Object marker;
297 register struct Lisp_Marker *m;
298 register EMACS_INT mpos;
300 marker = BUF_MARKERS (current_buffer);
302 while (!NILP (marker))
304 m = XMARKER (marker);
305 mpos = m->bytepos;
306 if (amount > 0)
308 if (mpos > to && mpos < to + amount)
310 if (adjust_markers_test)
311 abort ();
312 mpos = to + amount;
315 else
317 /* Here's the case where a marker is inside text being deleted.
318 AMOUNT can be negative for gap motion, too,
319 but then this range contains no markers. */
320 if (mpos > from + amount && mpos <= from)
322 if (adjust_markers_test)
323 abort ();
324 mpos = from + amount;
327 if (mpos > from && mpos <= to)
328 mpos += amount;
329 m->bufpos = mpos;
330 marker = m->chain;
332 #endif
335 /* Adjust all markers for a deletion
336 whose range in bytes is FROM_BYTE to TO_BYTE.
337 The range in charpos is FROM to TO.
339 This function assumes that the gap is adjacent to
340 or inside of the range being deleted. */
342 void
343 adjust_markers_for_delete (EMACS_INT from, EMACS_INT from_byte,
344 EMACS_INT to, EMACS_INT to_byte)
346 Lisp_Object marker;
347 register struct Lisp_Marker *m;
348 register EMACS_INT charpos;
350 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
352 charpos = m->charpos;
354 if (charpos > Z)
355 abort ();
357 /* If the marker is after the deletion,
358 relocate by number of chars / bytes deleted. */
359 if (charpos > to)
361 m->charpos -= to - from;
362 m->bytepos -= to_byte - from_byte;
364 /* Here's the case where a marker is inside text being deleted. */
365 else if (charpos > from)
367 if (! m->insertion_type)
368 { /* Normal markers will end up at the beginning of the
369 re-inserted text after undoing a deletion, and must be
370 adjusted to move them to the correct place. */
371 XSETMISC (marker, m);
372 record_marker_adjustment (marker, from - charpos);
374 else if (charpos < to)
375 { /* Before-insertion markers will automatically move forward
376 upon re-inserting the deleted text, so we have to arrange
377 for them to move backward to the correct position. */
378 XSETMISC (marker, m);
379 record_marker_adjustment (marker, charpos - to);
381 m->charpos = from;
382 m->bytepos = from_byte;
384 /* Here's the case where a before-insertion marker is immediately
385 before the deleted region. */
386 else if (charpos == from && m->insertion_type)
388 /* Undoing the change uses normal insertion, which will
389 incorrectly make MARKER move forward, so we arrange for it
390 to then move backward to the correct place at the beginning
391 of the deleted region. */
392 XSETMISC (marker, m);
393 record_marker_adjustment (marker, to - from);
399 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
400 to TO / TO_BYTE. We have to relocate the charpos of every marker
401 that points after the insertion (but not their bytepos).
403 When a marker points at the insertion point,
404 we advance it if either its insertion-type is t
405 or BEFORE_MARKERS is true. */
407 static void
408 adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
409 EMACS_INT to, EMACS_INT to_byte, int before_markers)
411 struct Lisp_Marker *m;
412 int adjusted = 0;
413 EMACS_INT nchars = to - from;
414 EMACS_INT nbytes = to_byte - from_byte;
416 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
418 eassert (m->bytepos >= m->charpos
419 && m->bytepos - m->charpos <= Z_BYTE - Z);
421 if (m->bytepos == from_byte)
423 if (m->insertion_type || before_markers)
425 m->bytepos = to_byte;
426 m->charpos = to;
427 if (m->insertion_type)
428 adjusted = 1;
431 else if (m->bytepos > from_byte)
433 m->bytepos += nbytes;
434 m->charpos += nchars;
438 /* Adjusting only markers whose insertion-type is t may result in
439 - disordered start and end in overlays, and
440 - disordered overlays in the slot `overlays_before' of current_buffer. */
441 if (adjusted)
443 fix_start_end_in_overlays(from, to);
444 fix_overlays_before (current_buffer, from, to);
448 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
450 This is used only when the value of point changes due to an insert
451 or delete; it does not represent a conceptual change in point as a
452 marker. In particular, point is not crossing any interval
453 boundaries, so there's no need to use the usual SET_PT macro. In
454 fact it would be incorrect to do so, because either the old or the
455 new value of point is out of sync with the current set of
456 intervals. */
458 static void
459 adjust_point (EMACS_INT nchars, EMACS_INT nbytes)
461 BUF_PT (current_buffer) += nchars;
462 BUF_PT_BYTE (current_buffer) += nbytes;
464 /* In a single-byte buffer, the two positions must be equal. */
465 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
468 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
469 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
470 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
471 an insertion. */
473 static void
474 adjust_markers_for_replace (EMACS_INT from, EMACS_INT from_byte,
475 EMACS_INT old_chars, EMACS_INT old_bytes,
476 EMACS_INT new_chars, EMACS_INT new_bytes)
478 register struct Lisp_Marker *m;
479 EMACS_INT prev_to_byte = from_byte + old_bytes;
480 EMACS_INT diff_chars = new_chars - old_chars;
481 EMACS_INT diff_bytes = new_bytes - old_bytes;
483 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
485 if (m->bytepos >= prev_to_byte)
487 m->charpos += diff_chars;
488 m->bytepos += diff_bytes;
490 else if (m->bytepos > from_byte)
492 m->charpos = from;
493 m->bytepos = from_byte;
497 CHECK_MARKERS ();
501 /* Make the gap NBYTES_ADDED bytes longer. */
503 void
504 make_gap_larger (EMACS_INT nbytes_added)
506 Lisp_Object tem;
507 EMACS_INT real_gap_loc;
508 EMACS_INT real_gap_loc_byte;
509 EMACS_INT old_gap_size;
511 /* If we have to get more space, get enough to last a while. */
512 nbytes_added += 2000;
514 /* Don't allow a buffer size that won't fit in an int
515 even if it will fit in a Lisp integer.
516 That won't work because so many places use `int'.
518 Make sure we don't introduce overflows in the calculation. */
520 if (Z_BYTE - BEG_BYTE + GAP_SIZE
521 >= (((EMACS_INT) 1 << (min (VALBITS, BITS_PER_INT) - 1)) - 1
522 - nbytes_added))
523 error ("Buffer exceeds maximum size");
525 enlarge_buffer_text (current_buffer, nbytes_added);
527 /* Prevent quitting in move_gap. */
528 tem = Vinhibit_quit;
529 Vinhibit_quit = Qt;
531 real_gap_loc = GPT;
532 real_gap_loc_byte = GPT_BYTE;
533 old_gap_size = GAP_SIZE;
535 /* Call the newly allocated space a gap at the end of the whole space. */
536 GPT = Z + GAP_SIZE;
537 GPT_BYTE = Z_BYTE + GAP_SIZE;
538 GAP_SIZE = nbytes_added;
540 /* Move the new gap down to be consecutive with the end of the old one.
541 This adjusts the markers properly too. */
542 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
544 /* Now combine the two into one large gap. */
545 GAP_SIZE += old_gap_size;
546 GPT = real_gap_loc;
547 GPT_BYTE = real_gap_loc_byte;
549 /* Put an anchor. */
550 *(Z_ADDR) = 0;
552 Vinhibit_quit = tem;
556 /* Make the gap NBYTES_REMOVED bytes shorter. */
558 void
559 make_gap_smaller (EMACS_INT nbytes_removed)
561 Lisp_Object tem;
562 EMACS_INT real_gap_loc;
563 EMACS_INT real_gap_loc_byte;
564 EMACS_INT real_Z;
565 EMACS_INT real_Z_byte;
566 EMACS_INT real_beg_unchanged;
567 EMACS_INT new_gap_size;
569 /* Make sure the gap is at least 20 bytes. */
570 if (GAP_SIZE - nbytes_removed < 20)
571 nbytes_removed = GAP_SIZE - 20;
573 /* Prevent quitting in move_gap. */
574 tem = Vinhibit_quit;
575 Vinhibit_quit = Qt;
577 real_gap_loc = GPT;
578 real_gap_loc_byte = GPT_BYTE;
579 new_gap_size = GAP_SIZE - nbytes_removed;
580 real_Z = Z;
581 real_Z_byte = Z_BYTE;
582 real_beg_unchanged = BEG_UNCHANGED;
584 /* Pretend that the last unwanted part of the gap is the entire gap,
585 and that the first desired part of the gap is part of the buffer
586 text. */
587 bzero (GPT_ADDR, new_gap_size);
588 GPT += new_gap_size;
589 GPT_BYTE += new_gap_size;
590 Z += new_gap_size;
591 Z_BYTE += new_gap_size;
592 GAP_SIZE = nbytes_removed;
594 /* Move the unwanted pretend gap to the end of the buffer. This
595 adjusts the markers properly too. */
596 gap_right (Z, Z_BYTE);
598 enlarge_buffer_text (current_buffer, -nbytes_removed);
600 /* Now restore the desired gap. */
601 GAP_SIZE = new_gap_size;
602 GPT = real_gap_loc;
603 GPT_BYTE = real_gap_loc_byte;
604 Z = real_Z;
605 Z_BYTE = real_Z_byte;
606 BEG_UNCHANGED = real_beg_unchanged;
608 /* Put an anchor. */
609 *(Z_ADDR) = 0;
611 Vinhibit_quit = tem;
614 void
615 make_gap (EMACS_INT nbytes_added)
617 if (nbytes_added >= 0)
618 make_gap_larger (nbytes_added);
619 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
620 else
621 make_gap_smaller (-nbytes_added);
622 #endif
625 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
626 FROM_MULTIBYTE says whether the incoming text is multibyte.
627 TO_MULTIBYTE says whether to store the text as multibyte.
628 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
630 Return the number of bytes stored at TO_ADDR. */
632 EMACS_INT
633 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
634 EMACS_INT nbytes, int from_multibyte, int to_multibyte)
636 if (from_multibyte == to_multibyte)
638 bcopy (from_addr, to_addr, nbytes);
639 return nbytes;
641 else if (from_multibyte)
643 EMACS_INT nchars = 0;
644 EMACS_INT bytes_left = nbytes;
645 Lisp_Object tbl = Qnil;
647 while (bytes_left > 0)
649 int thislen, c;
650 c = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
651 if (! ASCII_CHAR_P (c))
652 c &= 0xFF;
653 *to_addr++ = c;
654 from_addr += thislen;
655 bytes_left -= thislen;
656 nchars++;
658 return nchars;
660 else
662 unsigned char *initial_to_addr = to_addr;
664 /* Convert single-byte to multibyte. */
665 while (nbytes > 0)
667 int c = *from_addr++;
669 if (c >= 0200)
671 c = unibyte_char_to_multibyte (c);
672 to_addr += CHAR_STRING (c, to_addr);
673 nbytes--;
675 else
676 /* Special case for speed. */
677 *to_addr++ = c, nbytes--;
679 return to_addr - initial_to_addr;
683 /* Return the number of bytes it would take
684 to convert some single-byte text to multibyte.
685 The single-byte text consists of NBYTES bytes at PTR. */
687 EMACS_INT
688 count_size_as_multibyte (const unsigned char *ptr, EMACS_INT nbytes)
690 EMACS_INT i;
691 EMACS_INT outgoing_nbytes = 0;
693 for (i = 0; i < nbytes; i++)
695 unsigned int c = *ptr++;
697 if (c < 0200)
698 outgoing_nbytes++;
699 else
701 c = unibyte_char_to_multibyte (c);
702 outgoing_nbytes += CHAR_BYTES (c);
706 return outgoing_nbytes;
709 /* Insert a string of specified length before point.
710 This function judges multibyteness based on
711 enable_multibyte_characters in the current buffer;
712 it never converts between single-byte and multibyte.
714 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
715 prepare_to_modify_buffer could relocate the text. */
717 void
718 insert (const unsigned char *string, EMACS_INT nbytes)
720 if (nbytes > 0)
722 EMACS_INT len = chars_in_text (string, nbytes), opoint;
723 insert_1_both (string, len, nbytes, 0, 1, 0);
724 opoint = PT - len;
725 signal_after_change (opoint, 0, len);
726 update_compositions (opoint, PT, CHECK_BORDER);
730 /* Likewise, but inherit text properties from neighboring characters. */
732 void
733 insert_and_inherit (const unsigned char *string, EMACS_INT nbytes)
735 if (nbytes > 0)
737 EMACS_INT len = chars_in_text (string, nbytes), opoint;
738 insert_1_both (string, len, nbytes, 1, 1, 0);
739 opoint = PT - len;
740 signal_after_change (opoint, 0, len);
741 update_compositions (opoint, PT, CHECK_BORDER);
745 /* Insert the character C before point. Do not inherit text properties. */
747 void
748 insert_char (int c)
750 unsigned char str[MAX_MULTIBYTE_LENGTH];
751 int len;
753 if (! NILP (current_buffer->enable_multibyte_characters))
754 len = CHAR_STRING (c, str);
755 else
757 len = 1;
758 str[0] = c;
761 insert (str, len);
764 /* Insert the null-terminated string S before point. */
766 void
767 insert_string (const char *s)
769 insert (s, strlen (s));
772 /* Like `insert' except that all markers pointing at the place where
773 the insertion happens are adjusted to point after it.
774 Don't use this function to insert part of a Lisp string,
775 since gc could happen and relocate it. */
777 void
778 insert_before_markers (const unsigned char *string, EMACS_INT nbytes)
780 if (nbytes > 0)
782 EMACS_INT len = chars_in_text (string, nbytes), opoint;
783 insert_1_both (string, len, nbytes, 0, 1, 1);
784 opoint = PT - len;
785 signal_after_change (opoint, 0, len);
786 update_compositions (opoint, PT, CHECK_BORDER);
790 /* Likewise, but inherit text properties from neighboring characters. */
792 void
793 insert_before_markers_and_inherit (const unsigned char *string,
794 EMACS_INT nbytes)
796 if (nbytes > 0)
798 EMACS_INT len = chars_in_text (string, nbytes), opoint;
799 insert_1_both (string, len, nbytes, 1, 1, 1);
800 opoint = PT - len;
801 signal_after_change (opoint, 0, len);
802 update_compositions (opoint, PT, CHECK_BORDER);
806 /* Subroutine used by the insert functions above. */
808 void
809 insert_1 (const unsigned char *string, EMACS_INT nbytes,
810 int inherit, int prepare, int before_markers)
812 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
813 inherit, prepare, before_markers);
817 #ifdef BYTE_COMBINING_DEBUG
819 /* See if the bytes before POS/POS_BYTE combine with bytes
820 at the start of STRING to form a single character.
821 If so, return the number of bytes at the start of STRING
822 which combine in this way. Otherwise, return 0. */
825 count_combining_before (const unsigned char *string, EMACS_INT length,
826 EMACS_INT pos, EMACS_INT pos_byte)
828 int len, combining_bytes;
829 const unsigned char *p;
831 if (NILP (current_buffer->enable_multibyte_characters))
832 return 0;
834 /* At first, we can exclude the following cases:
835 (1) STRING[0] can't be a following byte of multibyte sequence.
836 (2) POS is the start of the current buffer.
837 (3) A character before POS is not a multibyte character. */
838 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
839 return 0;
840 if (pos_byte == BEG_BYTE) /* case (2) */
841 return 0;
842 len = 1;
843 p = BYTE_POS_ADDR (pos_byte - 1);
844 while (! CHAR_HEAD_P (*p)) p--, len++;
845 if (! BASE_LEADING_CODE_P (*p)) /* case (3) */
846 return 0;
848 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
849 if (combining_bytes <= 0)
850 /* The character preceding POS is, complete and no room for
851 combining bytes (combining_bytes == 0), or an independent 8-bit
852 character (combining_bytes < 0). */
853 return 0;
855 /* We have a combination situation. Count the bytes at STRING that
856 may combine. */
857 p = string + 1;
858 while (!CHAR_HEAD_P (*p) && p < string + length)
859 p++;
861 return (combining_bytes < p - string ? combining_bytes : p - string);
864 /* See if the bytes after POS/POS_BYTE combine with bytes
865 at the end of STRING to form a single character.
866 If so, return the number of bytes after POS/POS_BYTE
867 which combine in this way. Otherwise, return 0. */
870 count_combining_after (const unsigned char *string,
871 EMACS_INT length, EMACS_INT pos, EMACS_INT pos_byte)
873 EMACS_INT opos_byte = pos_byte;
874 EMACS_INT i;
875 EMACS_INT bytes;
876 unsigned char *bufp;
878 if (NILP (current_buffer->enable_multibyte_characters))
879 return 0;
881 /* At first, we can exclude the following cases:
882 (1) The last byte of STRING is an ASCII.
883 (2) POS is the last of the current buffer.
884 (3) A character at POS can't be a following byte of multibyte
885 character. */
886 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
887 return 0;
888 if (pos_byte == Z_BYTE) /* case (2) */
889 return 0;
890 bufp = BYTE_POS_ADDR (pos_byte);
891 if (CHAR_HEAD_P (*bufp)) /* case (3) */
892 return 0;
894 i = length - 1;
895 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
897 i--;
899 if (i < 0)
901 /* All characters in STRING are not character head. We must
902 check also preceding bytes at POS. We are sure that the gap
903 is at POS. */
904 unsigned char *p = BEG_ADDR;
905 i = pos_byte - 2;
906 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
907 i--;
908 if (i < 0 || !BASE_LEADING_CODE_P (p[i]))
909 return 0;
911 bytes = BYTES_BY_CHAR_HEAD (p[i]);
912 return (bytes <= pos_byte - 1 - i + length
914 : bytes - (pos_byte - 1 - i + length));
916 if (!BASE_LEADING_CODE_P (string[i]))
917 return 0;
919 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
920 bufp++, pos_byte++;
921 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
923 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
926 #endif
929 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
930 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
931 are the same as in insert_1. */
933 void
934 insert_1_both (const unsigned char *string,
935 EMACS_INT nchars, EMACS_INT nbytes,
936 int inherit, int prepare, int before_markers)
938 if (nchars == 0)
939 return;
941 if (NILP (current_buffer->enable_multibyte_characters))
942 nchars = nbytes;
944 if (prepare)
945 /* Do this before moving and increasing the gap,
946 because the before-change hooks might move the gap
947 or make it smaller. */
948 prepare_to_modify_buffer (PT, PT, NULL);
950 if (PT != GPT)
951 move_gap_both (PT, PT_BYTE);
952 if (GAP_SIZE < nbytes)
953 make_gap (nbytes - GAP_SIZE);
955 #ifdef BYTE_COMBINING_DEBUG
956 if (count_combining_before (string, nbytes, PT, PT_BYTE)
957 || count_combining_after (string, nbytes, PT, PT_BYTE))
958 abort ();
959 #endif
961 /* Record deletion of the surrounding text that combines with
962 the insertion. This, together with recording the insertion,
963 will add up to the right stuff in the undo list. */
964 record_insert (PT, nchars);
965 MODIFF++;
966 CHARS_MODIFF = MODIFF;
968 bcopy (string, GPT_ADDR, nbytes);
970 GAP_SIZE -= nbytes;
971 GPT += nchars;
972 ZV += nchars;
973 Z += nchars;
974 GPT_BYTE += nbytes;
975 ZV_BYTE += nbytes;
976 Z_BYTE += nbytes;
977 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
979 if (GPT_BYTE < GPT)
980 abort ();
982 /* The insert may have been in the unchanged region, so check again. */
983 if (Z - GPT < END_UNCHANGED)
984 END_UNCHANGED = Z - GPT;
986 adjust_overlays_for_insert (PT, nchars);
987 adjust_markers_for_insert (PT, PT_BYTE,
988 PT + nchars, PT_BYTE + nbytes,
989 before_markers);
991 if (BUF_INTERVALS (current_buffer) != 0)
992 offset_intervals (current_buffer, PT, nchars);
994 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
995 set_text_properties (make_number (PT), make_number (PT + nchars),
996 Qnil, Qnil, Qnil);
998 adjust_point (nchars, nbytes);
1000 CHECK_MARKERS ();
1003 /* Insert the part of the text of STRING, a Lisp object assumed to be
1004 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1005 starting at position POS / POS_BYTE. If the text of STRING has properties,
1006 copy them into the buffer.
1008 It does not work to use `insert' for this, because a GC could happen
1009 before we bcopy the stuff into the buffer, and relocate the string
1010 without insert noticing. */
1012 void
1013 insert_from_string (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
1014 EMACS_INT length, EMACS_INT length_byte, int inherit)
1016 EMACS_INT opoint = PT;
1018 if (SCHARS (string) == 0)
1019 return;
1021 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1022 inherit, 0);
1023 signal_after_change (opoint, 0, PT - opoint);
1024 update_compositions (opoint, PT, CHECK_BORDER);
1027 /* Like `insert_from_string' except that all markers pointing
1028 at the place where the insertion happens are adjusted to point after it. */
1030 void
1031 insert_from_string_before_markers (Lisp_Object string,
1032 EMACS_INT pos, EMACS_INT pos_byte,
1033 EMACS_INT length, EMACS_INT length_byte,
1034 int inherit)
1036 EMACS_INT opoint = PT;
1038 if (SCHARS (string) == 0)
1039 return;
1041 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1042 inherit, 1);
1043 signal_after_change (opoint, 0, PT - opoint);
1044 update_compositions (opoint, PT, CHECK_BORDER);
1047 /* Subroutine of the insertion functions above. */
1049 static void
1050 insert_from_string_1 (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
1051 EMACS_INT nchars, EMACS_INT nbytes,
1052 int inherit, int before_markers)
1054 struct gcpro gcpro1;
1055 EMACS_INT outgoing_nbytes = nbytes;
1056 INTERVAL intervals;
1058 /* Make OUTGOING_NBYTES describe the text
1059 as it will be inserted in this buffer. */
1061 if (NILP (current_buffer->enable_multibyte_characters))
1062 outgoing_nbytes = nchars;
1063 else if (! STRING_MULTIBYTE (string))
1064 outgoing_nbytes
1065 = count_size_as_multibyte (SDATA (string) + pos_byte,
1066 nbytes);
1068 GCPRO1 (string);
1069 /* Do this before moving and increasing the gap,
1070 because the before-change hooks might move the gap
1071 or make it smaller. */
1072 prepare_to_modify_buffer (PT, PT, NULL);
1074 if (PT != GPT)
1075 move_gap_both (PT, PT_BYTE);
1076 if (GAP_SIZE < outgoing_nbytes)
1077 make_gap (outgoing_nbytes - GAP_SIZE);
1078 UNGCPRO;
1080 /* Copy the string text into the buffer, perhaps converting
1081 between single-byte and multibyte. */
1082 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
1083 STRING_MULTIBYTE (string),
1084 ! NILP (current_buffer->enable_multibyte_characters));
1086 #ifdef BYTE_COMBINING_DEBUG
1087 /* We have copied text into the gap, but we have not altered
1088 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1089 to these functions and get the same results as we would
1090 have got earlier on. Meanwhile, PT_ADDR does point to
1091 the text that has been stored by copy_text. */
1092 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1093 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1094 abort ();
1095 #endif
1097 record_insert (PT, nchars);
1098 MODIFF++;
1099 CHARS_MODIFF = MODIFF;
1101 GAP_SIZE -= outgoing_nbytes;
1102 GPT += nchars;
1103 ZV += nchars;
1104 Z += nchars;
1105 GPT_BYTE += outgoing_nbytes;
1106 ZV_BYTE += outgoing_nbytes;
1107 Z_BYTE += outgoing_nbytes;
1108 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1110 if (GPT_BYTE < GPT)
1111 abort ();
1113 /* The insert may have been in the unchanged region, so check again. */
1114 if (Z - GPT < END_UNCHANGED)
1115 END_UNCHANGED = Z - GPT;
1117 adjust_overlays_for_insert (PT, nchars);
1118 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1119 PT_BYTE + outgoing_nbytes,
1120 before_markers);
1122 offset_intervals (current_buffer, PT, nchars);
1124 intervals = STRING_INTERVALS (string);
1125 /* Get the intervals for the part of the string we are inserting. */
1126 if (nbytes < SBYTES (string))
1127 intervals = copy_intervals (intervals, pos, nchars);
1129 /* Insert those intervals. */
1130 graft_intervals_into_buffer (intervals, PT, nchars,
1131 current_buffer, inherit);
1133 adjust_point (nchars, outgoing_nbytes);
1135 CHECK_MARKERS ();
1138 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1139 starting at GPT_ADDR. */
1141 void
1142 insert_from_gap (EMACS_INT nchars, EMACS_INT nbytes)
1144 if (NILP (current_buffer->enable_multibyte_characters))
1145 nchars = nbytes;
1147 record_insert (GPT, nchars);
1148 MODIFF++;
1150 GAP_SIZE -= nbytes;
1151 GPT += nchars;
1152 ZV += nchars;
1153 Z += nchars;
1154 GPT_BYTE += nbytes;
1155 ZV_BYTE += nbytes;
1156 Z_BYTE += nbytes;
1157 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1159 if (GPT_BYTE < GPT)
1160 abort ();
1162 adjust_overlays_for_insert (GPT - nchars, nchars);
1163 adjust_markers_for_insert (GPT - nchars, GPT_BYTE - nbytes,
1164 GPT, GPT_BYTE, 0);
1166 if (BUF_INTERVALS (current_buffer) != 0)
1168 offset_intervals (current_buffer, GPT - nchars, nchars);
1169 graft_intervals_into_buffer (NULL_INTERVAL, GPT - nchars, nchars,
1170 current_buffer, 0);
1173 if (GPT - nchars < PT)
1174 adjust_point (nchars, nbytes);
1176 CHECK_MARKERS ();
1179 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1180 current buffer. If the text in BUF has properties, they are absorbed
1181 into the current buffer.
1183 It does not work to use `insert' for this, because a malloc could happen
1184 and relocate BUF's text before the bcopy happens. */
1186 void
1187 insert_from_buffer (struct buffer *buf,
1188 EMACS_INT charpos, EMACS_INT nchars, int inherit)
1190 EMACS_INT opoint = PT;
1192 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1193 signal_after_change (opoint, 0, PT - opoint);
1194 update_compositions (opoint, PT, CHECK_BORDER);
1197 static void
1198 insert_from_buffer_1 (struct buffer *buf,
1199 EMACS_INT from, EMACS_INT nchars, int inherit)
1201 register Lisp_Object temp;
1202 EMACS_INT chunk, chunk_expanded;
1203 EMACS_INT from_byte = buf_charpos_to_bytepos (buf, from);
1204 EMACS_INT to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1205 EMACS_INT incoming_nbytes = to_byte - from_byte;
1206 EMACS_INT outgoing_nbytes = incoming_nbytes;
1207 INTERVAL intervals;
1209 /* Make OUTGOING_NBYTES describe the text
1210 as it will be inserted in this buffer. */
1212 if (NILP (current_buffer->enable_multibyte_characters))
1213 outgoing_nbytes = nchars;
1214 else if (NILP (buf->enable_multibyte_characters))
1216 EMACS_INT outgoing_before_gap = 0;
1217 EMACS_INT outgoing_after_gap = 0;
1219 if (from < BUF_GPT (buf))
1221 chunk = BUF_GPT_BYTE (buf) - from_byte;
1222 if (chunk > incoming_nbytes)
1223 chunk = incoming_nbytes;
1224 outgoing_before_gap
1225 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1226 chunk);
1228 else
1229 chunk = 0;
1231 if (chunk < incoming_nbytes)
1232 outgoing_after_gap
1233 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1234 from_byte + chunk),
1235 incoming_nbytes - chunk);
1237 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1240 /* Make sure point-max won't overflow after this insertion. */
1241 XSETINT (temp, outgoing_nbytes + Z);
1242 if (outgoing_nbytes + Z != XINT (temp))
1243 error ("Maximum buffer size exceeded");
1245 /* Do this before moving and increasing the gap,
1246 because the before-change hooks might move the gap
1247 or make it smaller. */
1248 prepare_to_modify_buffer (PT, PT, NULL);
1250 if (PT != GPT)
1251 move_gap_both (PT, PT_BYTE);
1252 if (GAP_SIZE < outgoing_nbytes)
1253 make_gap (outgoing_nbytes - GAP_SIZE);
1255 if (from < BUF_GPT (buf))
1257 chunk = BUF_GPT_BYTE (buf) - from_byte;
1258 if (chunk > incoming_nbytes)
1259 chunk = incoming_nbytes;
1260 /* Record number of output bytes, so we know where
1261 to put the output from the second copy_text. */
1262 chunk_expanded
1263 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1264 GPT_ADDR, chunk,
1265 ! NILP (buf->enable_multibyte_characters),
1266 ! NILP (current_buffer->enable_multibyte_characters));
1268 else
1269 chunk_expanded = chunk = 0;
1271 if (chunk < incoming_nbytes)
1272 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1273 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1274 ! NILP (buf->enable_multibyte_characters),
1275 ! NILP (current_buffer->enable_multibyte_characters));
1277 #ifdef BYTE_COMBINING_DEBUG
1278 /* We have copied text into the gap, but we have not altered
1279 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1280 to these functions and get the same results as we would
1281 have got earlier on. Meanwhile, GPT_ADDR does point to
1282 the text that has been stored by copy_text. */
1283 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1284 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1285 abort ();
1286 #endif
1288 record_insert (PT, nchars);
1289 MODIFF++;
1290 CHARS_MODIFF = MODIFF;
1292 GAP_SIZE -= outgoing_nbytes;
1293 GPT += nchars;
1294 ZV += nchars;
1295 Z += nchars;
1296 GPT_BYTE += outgoing_nbytes;
1297 ZV_BYTE += outgoing_nbytes;
1298 Z_BYTE += outgoing_nbytes;
1299 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1301 if (GPT_BYTE < GPT)
1302 abort ();
1304 /* The insert may have been in the unchanged region, so check again. */
1305 if (Z - GPT < END_UNCHANGED)
1306 END_UNCHANGED = Z - GPT;
1308 adjust_overlays_for_insert (PT, nchars);
1309 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1310 PT_BYTE + outgoing_nbytes,
1313 if (BUF_INTERVALS (current_buffer) != 0)
1314 offset_intervals (current_buffer, PT, nchars);
1316 /* Get the intervals for the part of the string we are inserting. */
1317 intervals = BUF_INTERVALS (buf);
1318 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1320 if (buf == current_buffer && PT <= from)
1321 from += nchars;
1322 intervals = copy_intervals (intervals, from, nchars);
1325 /* Insert those intervals. */
1326 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1328 adjust_point (nchars, outgoing_nbytes);
1331 /* Record undo information and adjust markers and position keepers for
1332 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1333 chars (LEN_BYTE bytes) which resides in the gap just after
1334 GPT_ADDR.
1336 PREV_TEXT nil means the new text was just inserted. */
1338 void
1339 adjust_after_replace (EMACS_INT from, EMACS_INT from_byte,
1340 Lisp_Object prev_text, EMACS_INT len, EMACS_INT len_byte)
1342 EMACS_INT nchars_del = 0, nbytes_del = 0;
1344 #ifdef BYTE_COMBINING_DEBUG
1345 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1346 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1347 abort ();
1348 #endif
1350 if (STRINGP (prev_text))
1352 nchars_del = SCHARS (prev_text);
1353 nbytes_del = SBYTES (prev_text);
1356 /* Update various buffer positions for the new text. */
1357 GAP_SIZE -= len_byte;
1358 ZV += len; Z+= len;
1359 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1360 GPT += len; GPT_BYTE += len_byte;
1361 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1363 if (nchars_del > 0)
1364 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1365 len, len_byte);
1366 else
1367 adjust_markers_for_insert (from, from_byte,
1368 from + len, from_byte + len_byte, 0);
1370 if (! EQ (current_buffer->undo_list, Qt))
1372 if (nchars_del > 0)
1373 record_delete (from, prev_text);
1374 record_insert (from, len);
1377 if (len > nchars_del)
1378 adjust_overlays_for_insert (from, len - nchars_del);
1379 else if (len < nchars_del)
1380 adjust_overlays_for_delete (from, nchars_del - len);
1381 if (BUF_INTERVALS (current_buffer) != 0)
1383 offset_intervals (current_buffer, from, len - nchars_del);
1386 if (from < PT)
1387 adjust_point (len - nchars_del, len_byte - nbytes_del);
1389 /* As byte combining will decrease Z, we must check this again. */
1390 if (Z - GPT < END_UNCHANGED)
1391 END_UNCHANGED = Z - GPT;
1393 CHECK_MARKERS ();
1395 if (len == 0)
1396 evaporate_overlays (from);
1397 MODIFF++;
1398 CHARS_MODIFF = MODIFF;
1401 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1402 This is for use when undo is not enabled in the current buffer. */
1404 void
1405 adjust_after_replace_noundo (EMACS_INT from, EMACS_INT from_byte,
1406 EMACS_INT nchars_del, EMACS_INT nbytes_del,
1407 EMACS_INT len, EMACS_INT len_byte)
1409 #ifdef BYTE_COMBINING_DEBUG
1410 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1411 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1412 abort ();
1413 #endif
1415 /* Update various buffer positions for the new text. */
1416 GAP_SIZE -= len_byte;
1417 ZV += len; Z+= len;
1418 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1419 GPT += len; GPT_BYTE += len_byte;
1420 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1422 if (nchars_del > 0)
1423 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1424 len, len_byte);
1425 else
1426 adjust_markers_for_insert (from, from_byte,
1427 from + len, from_byte + len_byte, 0);
1429 if (len > nchars_del)
1430 adjust_overlays_for_insert (from, len - nchars_del);
1431 else if (len < nchars_del)
1432 adjust_overlays_for_delete (from, nchars_del - len);
1433 if (BUF_INTERVALS (current_buffer) != 0)
1435 offset_intervals (current_buffer, from, len - nchars_del);
1438 if (from < PT)
1439 adjust_point (len - nchars_del, len_byte - nbytes_del);
1441 /* As byte combining will decrease Z, we must check this again. */
1442 if (Z - GPT < END_UNCHANGED)
1443 END_UNCHANGED = Z - GPT;
1445 CHECK_MARKERS ();
1447 if (len == 0)
1448 evaporate_overlays (from);
1449 MODIFF++;
1450 CHARS_MODIFF = MODIFF;
1453 /* Record undo information, adjust markers and position keepers for an
1454 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1455 text already exists in the current buffer but character length (TO
1456 - FROM) may be incorrect, the correct length is NEWLEN. */
1458 void
1459 adjust_after_insert (EMACS_INT from, EMACS_INT from_byte,
1460 EMACS_INT to, EMACS_INT to_byte, EMACS_INT newlen)
1462 EMACS_INT len = to - from, len_byte = to_byte - from_byte;
1464 if (GPT != to)
1465 move_gap_both (to, to_byte);
1466 GAP_SIZE += len_byte;
1467 GPT -= len; GPT_BYTE -= len_byte;
1468 ZV -= len; ZV_BYTE -= len_byte;
1469 Z -= len; Z_BYTE -= len_byte;
1470 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1473 /* Replace the text from character positions FROM to TO with NEW,
1474 If PREPARE is nonzero, call prepare_to_modify_buffer.
1475 If INHERIT, the newly inserted text should inherit text properties
1476 from the surrounding non-deleted text. */
1478 /* Note that this does not yet handle markers quite right.
1479 Also it needs to record a single undo-entry that does a replacement
1480 rather than a separate delete and insert.
1481 That way, undo will also handle markers properly.
1483 But if MARKERS is 0, don't relocate markers. */
1485 void
1486 replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
1487 int prepare, int inherit, int markers)
1489 EMACS_INT inschars = SCHARS (new);
1490 EMACS_INT insbytes = SBYTES (new);
1491 EMACS_INT from_byte, to_byte;
1492 EMACS_INT nbytes_del, nchars_del;
1493 register Lisp_Object temp;
1494 struct gcpro gcpro1;
1495 INTERVAL intervals;
1496 EMACS_INT outgoing_insbytes = insbytes;
1497 Lisp_Object deletion;
1499 CHECK_MARKERS ();
1501 GCPRO1 (new);
1502 deletion = Qnil;
1504 if (prepare)
1506 EMACS_INT range_length = to - from;
1507 prepare_to_modify_buffer (from, to, &from);
1508 to = from + range_length;
1511 UNGCPRO;
1513 /* Make args be valid */
1514 if (from < BEGV)
1515 from = BEGV;
1516 if (to > ZV)
1517 to = ZV;
1519 from_byte = CHAR_TO_BYTE (from);
1520 to_byte = CHAR_TO_BYTE (to);
1522 nchars_del = to - from;
1523 nbytes_del = to_byte - from_byte;
1525 if (nbytes_del <= 0 && insbytes == 0)
1526 return;
1528 /* Make OUTGOING_INSBYTES describe the text
1529 as it will be inserted in this buffer. */
1531 if (NILP (current_buffer->enable_multibyte_characters))
1532 outgoing_insbytes = inschars;
1533 else if (! STRING_MULTIBYTE (new))
1534 outgoing_insbytes
1535 = count_size_as_multibyte (SDATA (new), insbytes);
1537 /* Make sure point-max won't overflow after this insertion. */
1538 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1539 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1540 error ("Maximum buffer size exceeded");
1542 GCPRO1 (new);
1544 /* Make sure the gap is somewhere in or next to what we are deleting. */
1545 if (from > GPT)
1546 gap_right (from, from_byte);
1547 if (to < GPT)
1548 gap_left (to, to_byte, 0);
1550 /* Even if we don't record for undo, we must keep the original text
1551 because we may have to recover it because of inappropriate byte
1552 combining. */
1553 if (! EQ (current_buffer->undo_list, Qt))
1554 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1556 GAP_SIZE += nbytes_del;
1557 ZV -= nchars_del;
1558 Z -= nchars_del;
1559 ZV_BYTE -= nbytes_del;
1560 Z_BYTE -= nbytes_del;
1561 GPT = from;
1562 GPT_BYTE = from_byte;
1563 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1565 if (GPT_BYTE < GPT)
1566 abort ();
1568 if (GPT - BEG < BEG_UNCHANGED)
1569 BEG_UNCHANGED = GPT - BEG;
1570 if (Z - GPT < END_UNCHANGED)
1571 END_UNCHANGED = Z - GPT;
1573 if (GAP_SIZE < insbytes)
1574 make_gap (insbytes - GAP_SIZE);
1576 /* Copy the string text into the buffer, perhaps converting
1577 between single-byte and multibyte. */
1578 copy_text (SDATA (new), GPT_ADDR, insbytes,
1579 STRING_MULTIBYTE (new),
1580 ! NILP (current_buffer->enable_multibyte_characters));
1582 #ifdef BYTE_COMBINING_DEBUG
1583 /* We have copied text into the gap, but we have not marked
1584 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1585 here, for both the previous text and the following text.
1586 Meanwhile, GPT_ADDR does point to
1587 the text that has been stored by copy_text. */
1588 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1589 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1590 abort ();
1591 #endif
1593 if (! EQ (current_buffer->undo_list, Qt))
1595 /* Record the insertion first, so that when we undo,
1596 the deletion will be undone first. Thus, undo
1597 will insert before deleting, and thus will keep
1598 the markers before and after this text separate. */
1599 record_insert (from + SCHARS (deletion), inschars);
1600 record_delete (from, deletion);
1603 GAP_SIZE -= outgoing_insbytes;
1604 GPT += inschars;
1605 ZV += inschars;
1606 Z += inschars;
1607 GPT_BYTE += outgoing_insbytes;
1608 ZV_BYTE += outgoing_insbytes;
1609 Z_BYTE += outgoing_insbytes;
1610 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1612 if (GPT_BYTE < GPT)
1613 abort ();
1615 /* Adjust the overlay center as needed. This must be done after
1616 adjusting the markers that bound the overlays. */
1617 adjust_overlays_for_delete (from, nchars_del);
1618 adjust_overlays_for_insert (from, inschars);
1620 /* Adjust markers for the deletion and the insertion. */
1621 if (markers)
1622 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1623 inschars, outgoing_insbytes);
1625 offset_intervals (current_buffer, from, inschars - nchars_del);
1627 /* Get the intervals for the part of the string we are inserting--
1628 not including the combined-before bytes. */
1629 intervals = STRING_INTERVALS (new);
1630 /* Insert those intervals. */
1631 graft_intervals_into_buffer (intervals, from, inschars,
1632 current_buffer, inherit);
1634 /* Relocate point as if it were a marker. */
1635 if (from < PT)
1636 adjust_point ((from + inschars - (PT < to ? PT : to)),
1637 (from_byte + outgoing_insbytes
1638 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1640 if (outgoing_insbytes == 0)
1641 evaporate_overlays (from);
1643 CHECK_MARKERS ();
1645 MODIFF++;
1646 CHARS_MODIFF = MODIFF;
1647 UNGCPRO;
1649 signal_after_change (from, nchars_del, GPT - from);
1650 update_compositions (from, GPT, CHECK_BORDER);
1653 /* Replace the text from character positions FROM to TO with
1654 the text in INS of length INSCHARS.
1655 Keep the text properties that applied to the old characters
1656 (extending them to all the new chars if there are more new chars).
1658 Note that this does not yet handle markers quite right.
1660 If MARKERS is nonzero, relocate markers.
1662 Unlike most functions at this level, never call
1663 prepare_to_modify_buffer and never call signal_after_change. */
1665 void
1666 replace_range_2 (EMACS_INT from, EMACS_INT from_byte,
1667 EMACS_INT to, EMACS_INT to_byte,
1668 char *ins, EMACS_INT inschars, EMACS_INT insbytes,
1669 int markers)
1671 EMACS_INT nbytes_del, nchars_del;
1672 Lisp_Object temp;
1674 CHECK_MARKERS ();
1676 nchars_del = to - from;
1677 nbytes_del = to_byte - from_byte;
1679 if (nbytes_del <= 0 && insbytes == 0)
1680 return;
1682 /* Make sure point-max won't overflow after this insertion. */
1683 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1684 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1685 error ("Maximum buffer size exceeded");
1687 /* Make sure the gap is somewhere in or next to what we are deleting. */
1688 if (from > GPT)
1689 gap_right (from, from_byte);
1690 if (to < GPT)
1691 gap_left (to, to_byte, 0);
1693 GAP_SIZE += nbytes_del;
1694 ZV -= nchars_del;
1695 Z -= nchars_del;
1696 ZV_BYTE -= nbytes_del;
1697 Z_BYTE -= nbytes_del;
1698 GPT = from;
1699 GPT_BYTE = from_byte;
1700 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1702 if (GPT_BYTE < GPT)
1703 abort ();
1705 if (GPT - BEG < BEG_UNCHANGED)
1706 BEG_UNCHANGED = GPT - BEG;
1707 if (Z - GPT < END_UNCHANGED)
1708 END_UNCHANGED = Z - GPT;
1710 if (GAP_SIZE < insbytes)
1711 make_gap (insbytes - GAP_SIZE);
1713 /* Copy the replacement text into the buffer. */
1714 bcopy (ins, GPT_ADDR, insbytes);
1716 #ifdef BYTE_COMBINING_DEBUG
1717 /* We have copied text into the gap, but we have not marked
1718 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1719 here, for both the previous text and the following text.
1720 Meanwhile, GPT_ADDR does point to
1721 the text that has been stored by copy_text. */
1722 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1723 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1724 abort ();
1725 #endif
1727 GAP_SIZE -= insbytes;
1728 GPT += inschars;
1729 ZV += inschars;
1730 Z += inschars;
1731 GPT_BYTE += insbytes;
1732 ZV_BYTE += insbytes;
1733 Z_BYTE += insbytes;
1734 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1736 if (GPT_BYTE < GPT)
1737 abort ();
1739 /* Adjust the overlay center as needed. This must be done after
1740 adjusting the markers that bound the overlays. */
1741 if (nchars_del != inschars)
1743 adjust_overlays_for_insert (from, inschars);
1744 adjust_overlays_for_delete (from + inschars, nchars_del);
1747 /* Adjust markers for the deletion and the insertion. */
1748 if (markers
1749 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1750 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1751 inschars, insbytes);
1753 offset_intervals (current_buffer, from, inschars - nchars_del);
1755 /* Relocate point as if it were a marker. */
1756 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1758 if (PT < to)
1759 /* PT was within the deleted text. Move it to FROM. */
1760 adjust_point (from - PT, from_byte - PT_BYTE);
1761 else
1762 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1765 if (insbytes == 0)
1766 evaporate_overlays (from);
1768 CHECK_MARKERS ();
1770 MODIFF++;
1771 CHARS_MODIFF = MODIFF;
1774 /* Delete characters in current buffer
1775 from FROM up to (but not including) TO.
1776 If TO comes before FROM, we delete nothing. */
1778 void
1779 del_range (EMACS_INT from, EMACS_INT to)
1781 del_range_1 (from, to, 1, 0);
1784 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1785 RET_STRING says to return the deleted text. */
1787 Lisp_Object
1788 del_range_1 (EMACS_INT from, EMACS_INT to, int prepare, int ret_string)
1790 EMACS_INT from_byte, to_byte;
1791 Lisp_Object deletion;
1792 struct gcpro gcpro1;
1794 /* Make args be valid */
1795 if (from < BEGV)
1796 from = BEGV;
1797 if (to > ZV)
1798 to = ZV;
1800 if (to <= from)
1801 return Qnil;
1803 if (prepare)
1805 EMACS_INT range_length = to - from;
1806 prepare_to_modify_buffer (from, to, &from);
1807 to = min (ZV, from + range_length);
1810 from_byte = CHAR_TO_BYTE (from);
1811 to_byte = CHAR_TO_BYTE (to);
1813 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1814 GCPRO1(deletion);
1815 signal_after_change (from, to - from, 0);
1816 update_compositions (from, from, CHECK_HEAD);
1817 UNGCPRO;
1818 return deletion;
1821 /* Like del_range_1 but args are byte positions, not char positions. */
1823 void
1824 del_range_byte (EMACS_INT from_byte, EMACS_INT to_byte, int prepare)
1826 EMACS_INT from, to;
1828 /* Make args be valid */
1829 if (from_byte < BEGV_BYTE)
1830 from_byte = BEGV_BYTE;
1831 if (to_byte > ZV_BYTE)
1832 to_byte = ZV_BYTE;
1834 if (to_byte <= from_byte)
1835 return;
1837 from = BYTE_TO_CHAR (from_byte);
1838 to = BYTE_TO_CHAR (to_byte);
1840 if (prepare)
1842 EMACS_INT old_from = from, old_to = Z - to;
1843 EMACS_INT range_length = to - from;
1844 prepare_to_modify_buffer (from, to, &from);
1845 to = from + range_length;
1847 if (old_from != from)
1848 from_byte = CHAR_TO_BYTE (from);
1849 if (to > ZV)
1851 to = ZV;
1852 to_byte = ZV_BYTE;
1854 else if (old_to == Z - to)
1855 to_byte = CHAR_TO_BYTE (to);
1858 del_range_2 (from, from_byte, to, to_byte, 0);
1859 signal_after_change (from, to - from, 0);
1860 update_compositions (from, from, CHECK_HEAD);
1863 /* Like del_range_1, but positions are specified both as charpos
1864 and bytepos. */
1866 void
1867 del_range_both (EMACS_INT from, EMACS_INT from_byte,
1868 EMACS_INT to, EMACS_INT to_byte, int prepare)
1870 /* Make args be valid */
1871 if (from_byte < BEGV_BYTE)
1872 from_byte = BEGV_BYTE;
1873 if (to_byte > ZV_BYTE)
1874 to_byte = ZV_BYTE;
1876 if (to_byte <= from_byte)
1877 return;
1879 if (from < BEGV)
1880 from = BEGV;
1881 if (to > ZV)
1882 to = ZV;
1884 if (prepare)
1886 EMACS_INT old_from = from, old_to = Z - to;
1887 EMACS_INT range_length = to - from;
1888 prepare_to_modify_buffer (from, to, &from);
1889 to = from + range_length;
1891 if (old_from != from)
1892 from_byte = CHAR_TO_BYTE (from);
1893 if (to > ZV)
1895 to = ZV;
1896 to_byte = ZV_BYTE;
1898 else if (old_to == Z - to)
1899 to_byte = CHAR_TO_BYTE (to);
1902 del_range_2 (from, from_byte, to, to_byte, 0);
1903 signal_after_change (from, to - from, 0);
1904 update_compositions (from, from, CHECK_HEAD);
1907 /* Delete a range of text, specified both as character positions
1908 and byte positions. FROM and TO are character positions,
1909 while FROM_BYTE and TO_BYTE are byte positions.
1910 If RET_STRING is true, the deleted area is returned as a string. */
1912 Lisp_Object
1913 del_range_2 (EMACS_INT from, EMACS_INT from_byte,
1914 EMACS_INT to, EMACS_INT to_byte, int ret_string)
1916 register EMACS_INT nbytes_del, nchars_del;
1917 Lisp_Object deletion;
1919 CHECK_MARKERS ();
1921 nchars_del = to - from;
1922 nbytes_del = to_byte - from_byte;
1924 /* Make sure the gap is somewhere in or next to what we are deleting. */
1925 if (from > GPT)
1926 gap_right (from, from_byte);
1927 if (to < GPT)
1928 gap_left (to, to_byte, 0);
1930 #ifdef BYTE_COMBINING_DEBUG
1931 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1932 Z_BYTE - to_byte, from, from_byte))
1933 abort ();
1934 #endif
1936 if (ret_string || ! EQ (current_buffer->undo_list, Qt))
1937 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1938 else
1939 deletion = Qnil;
1941 /* Relocate all markers pointing into the new, larger gap
1942 to point at the end of the text before the gap.
1943 Do this before recording the deletion,
1944 so that undo handles this after reinserting the text. */
1945 adjust_markers_for_delete (from, from_byte, to, to_byte);
1947 if (! EQ (current_buffer->undo_list, Qt))
1948 record_delete (from, deletion);
1949 MODIFF++;
1950 CHARS_MODIFF = MODIFF;
1952 /* Relocate point as if it were a marker. */
1953 if (from < PT)
1954 adjust_point (from - (PT < to ? PT : to),
1955 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1957 offset_intervals (current_buffer, from, - nchars_del);
1959 /* Adjust the overlay center as needed. This must be done after
1960 adjusting the markers that bound the overlays. */
1961 adjust_overlays_for_delete (from, nchars_del);
1963 GAP_SIZE += nbytes_del;
1964 ZV_BYTE -= nbytes_del;
1965 Z_BYTE -= nbytes_del;
1966 ZV -= nchars_del;
1967 Z -= nchars_del;
1968 GPT = from;
1969 GPT_BYTE = from_byte;
1970 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1971 /* Put an anchor, unless called from decode_coding_object which
1972 needs to access the previous gap contents. */
1973 *(GPT_ADDR) = 0;
1975 if (GPT_BYTE < GPT)
1976 abort ();
1978 if (GPT - BEG < BEG_UNCHANGED)
1979 BEG_UNCHANGED = GPT - BEG;
1980 if (Z - GPT < END_UNCHANGED)
1981 END_UNCHANGED = Z - GPT;
1983 CHECK_MARKERS ();
1985 evaporate_overlays (from);
1987 return deletion;
1990 /* Call this if you're about to change the region of BUFFER from
1991 character positions START to END. This checks the read-only
1992 properties of the region, calls the necessary modification hooks,
1993 and warns the next redisplay that it should pay attention to that
1994 area.
1996 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
1997 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1999 void
2000 modify_region (struct buffer *buffer, EMACS_INT start, EMACS_INT end,
2001 int preserve_chars_modiff)
2003 struct buffer *old_buffer = current_buffer;
2005 if (buffer != old_buffer)
2006 set_buffer_internal (buffer);
2008 prepare_to_modify_buffer (start, end, NULL);
2010 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
2012 if (MODIFF <= SAVE_MODIFF)
2013 record_first_change ();
2014 MODIFF++;
2015 if (! preserve_chars_modiff)
2016 CHARS_MODIFF = MODIFF;
2018 buffer->point_before_scroll = Qnil;
2020 if (buffer != old_buffer)
2021 set_buffer_internal (old_buffer);
2024 /* Check that it is okay to modify the buffer between START and END,
2025 which are char positions.
2027 Run the before-change-function, if any. If intervals are in use,
2028 verify that the text to be modified is not read-only, and call
2029 any modification properties the text may have.
2031 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2032 by holding its value temporarily in a marker. */
2034 void
2035 prepare_to_modify_buffer (EMACS_INT start, EMACS_INT end,
2036 EMACS_INT *preserve_ptr)
2038 struct buffer *base_buffer;
2040 if (!NILP (current_buffer->read_only))
2041 Fbarf_if_buffer_read_only ();
2043 /* Let redisplay consider other windows than selected_window
2044 if modifying another buffer. */
2045 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2046 ++windows_or_buffers_changed;
2048 if (BUF_INTERVALS (current_buffer) != 0)
2050 if (preserve_ptr)
2052 Lisp_Object preserve_marker;
2053 struct gcpro gcpro1;
2054 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
2055 GCPRO1 (preserve_marker);
2056 verify_interval_modification (current_buffer, start, end);
2057 *preserve_ptr = marker_position (preserve_marker);
2058 unchain_marker (XMARKER (preserve_marker));
2059 UNGCPRO;
2061 else
2062 verify_interval_modification (current_buffer, start, end);
2065 /* For indirect buffers, use the base buffer to check clashes. */
2066 if (current_buffer->base_buffer != 0)
2067 base_buffer = current_buffer->base_buffer;
2068 else
2069 base_buffer = current_buffer;
2071 #ifdef CLASH_DETECTION
2072 if (!NILP (base_buffer->file_truename)
2073 /* Make binding buffer-file-name to nil effective. */
2074 && !NILP (base_buffer->filename)
2075 && SAVE_MODIFF >= MODIFF)
2076 lock_file (base_buffer->file_truename);
2077 #else
2078 /* At least warn if this file has changed on disk since it was visited. */
2079 if (!NILP (base_buffer->filename)
2080 && SAVE_MODIFF >= MODIFF
2081 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2082 && !NILP (Ffile_exists_p (base_buffer->filename)))
2083 call1 (intern ("ask-user-about-supersession-threat"),
2084 base_buffer->filename);
2085 #endif /* not CLASH_DETECTION */
2087 signal_before_change (start, end, preserve_ptr);
2089 if (current_buffer->newline_cache)
2090 invalidate_region_cache (current_buffer,
2091 current_buffer->newline_cache,
2092 start - BEG, Z - end);
2093 if (current_buffer->width_run_cache)
2094 invalidate_region_cache (current_buffer,
2095 current_buffer->width_run_cache,
2096 start - BEG, Z - end);
2098 Vdeactivate_mark = Qt;
2101 /* These macros work with an argument named `preserve_ptr'
2102 and a local variable named `preserve_marker'. */
2104 #define PRESERVE_VALUE \
2105 if (preserve_ptr && NILP (preserve_marker)) \
2106 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2108 #define RESTORE_VALUE \
2109 if (! NILP (preserve_marker)) \
2111 *preserve_ptr = marker_position (preserve_marker); \
2112 unchain_marker (XMARKER (preserve_marker)); \
2115 #define PRESERVE_START_END \
2116 if (NILP (start_marker)) \
2117 start_marker = Fcopy_marker (start, Qnil); \
2118 if (NILP (end_marker)) \
2119 end_marker = Fcopy_marker (end, Qnil);
2121 #define FETCH_START \
2122 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2124 #define FETCH_END \
2125 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2127 /* Set a variable to nil if an error occurred.
2128 Don't change the variable if there was no error.
2129 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2130 VARIABLE is the variable to maybe set to nil.
2131 NO-ERROR-FLAG is nil if there was an error,
2132 anything else meaning no error (so this function does nothing). */
2133 Lisp_Object
2134 reset_var_on_error (val)
2135 Lisp_Object val;
2137 if (NILP (XCDR (val)))
2138 Fset (XCAR (val), Qnil);
2139 return Qnil;
2142 /* Signal a change to the buffer immediately before it happens.
2143 START_INT and END_INT are the bounds of the text to be changed.
2145 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2146 by holding its value temporarily in a marker. */
2148 void
2149 signal_before_change (EMACS_INT start_int, EMACS_INT end_int,
2150 EMACS_INT *preserve_ptr)
2152 Lisp_Object start, end;
2153 Lisp_Object start_marker, end_marker;
2154 Lisp_Object preserve_marker;
2155 struct gcpro gcpro1, gcpro2, gcpro3;
2156 int count = SPECPDL_INDEX ();
2158 if (inhibit_modification_hooks)
2159 return;
2161 start = make_number (start_int);
2162 end = make_number (end_int);
2163 preserve_marker = Qnil;
2164 start_marker = Qnil;
2165 end_marker = Qnil;
2166 GCPRO3 (preserve_marker, start_marker, end_marker);
2168 specbind (Qinhibit_modification_hooks, Qt);
2170 /* If buffer is unmodified, run a special hook for that case. */
2171 if (SAVE_MODIFF >= MODIFF
2172 && !NILP (Vfirst_change_hook)
2173 && !NILP (Vrun_hooks))
2175 PRESERVE_VALUE;
2176 PRESERVE_START_END;
2177 call1 (Vrun_hooks, Qfirst_change_hook);
2180 /* Now run the before-change-functions if any. */
2181 if (!NILP (Vbefore_change_functions))
2183 Lisp_Object args[3];
2184 Lisp_Object rvoe_arg = Fcons (Qbefore_change_functions, Qnil);
2186 PRESERVE_VALUE;
2187 PRESERVE_START_END;
2189 /* Mark before-change-functions to be reset to nil in case of error. */
2190 record_unwind_protect (reset_var_on_error, rvoe_arg);
2192 /* Actually run the hook functions. */
2193 args[0] = Qbefore_change_functions;
2194 args[1] = FETCH_START;
2195 args[2] = FETCH_END;
2196 Frun_hook_with_args (3, args);
2198 /* There was no error: unarm the reset_on_error. */
2199 XSETCDR (rvoe_arg, Qt);
2202 if (current_buffer->overlays_before || current_buffer->overlays_after)
2204 PRESERVE_VALUE;
2205 report_overlay_modification (FETCH_START, FETCH_END, 0,
2206 FETCH_START, FETCH_END, Qnil);
2209 if (! NILP (start_marker))
2210 free_marker (start_marker);
2211 if (! NILP (end_marker))
2212 free_marker (end_marker);
2213 RESTORE_VALUE;
2214 UNGCPRO;
2216 unbind_to (count, Qnil);
2219 /* Signal a change immediately after it happens.
2220 CHARPOS is the character position of the start of the changed text.
2221 LENDEL is the number of characters of the text before the change.
2222 (Not the whole buffer; just the part that was changed.)
2223 LENINS is the number of characters in that part of the text
2224 after the change. */
2226 void
2227 signal_after_change (EMACS_INT charpos, EMACS_INT lendel, EMACS_INT lenins)
2229 int count = SPECPDL_INDEX ();
2230 if (inhibit_modification_hooks)
2231 return;
2233 /* If we are deferring calls to the after-change functions
2234 and there are no before-change functions,
2235 just record the args that we were going to use. */
2236 if (! NILP (Vcombine_after_change_calls)
2237 && NILP (Vbefore_change_functions)
2238 && !current_buffer->overlays_before
2239 && !current_buffer->overlays_after)
2241 Lisp_Object elt;
2243 if (!NILP (combine_after_change_list)
2244 && current_buffer != XBUFFER (combine_after_change_buffer))
2245 Fcombine_after_change_execute ();
2247 elt = Fcons (make_number (charpos - BEG),
2248 Fcons (make_number (Z - (charpos - lendel + lenins)),
2249 Fcons (make_number (lenins - lendel), Qnil)));
2250 combine_after_change_list
2251 = Fcons (elt, combine_after_change_list);
2252 combine_after_change_buffer = Fcurrent_buffer ();
2254 return;
2257 if (!NILP (combine_after_change_list))
2258 Fcombine_after_change_execute ();
2260 specbind (Qinhibit_modification_hooks, Qt);
2262 if (!NILP (Vafter_change_functions))
2264 Lisp_Object args[4];
2265 Lisp_Object rvoe_arg = Fcons (Qafter_change_functions, Qnil);
2267 /* Mark after-change-functions to be reset to nil in case of error. */
2268 record_unwind_protect (reset_var_on_error, rvoe_arg);
2270 /* Actually run the hook functions. */
2271 args[0] = Qafter_change_functions;
2272 XSETFASTINT (args[1], charpos);
2273 XSETFASTINT (args[2], charpos + lenins);
2274 XSETFASTINT (args[3], lendel);
2275 Frun_hook_with_args (4, args);
2277 /* There was no error: unarm the reset_on_error. */
2278 XSETCDR (rvoe_arg, Qt);
2281 if (current_buffer->overlays_before || current_buffer->overlays_after)
2282 report_overlay_modification (make_number (charpos),
2283 make_number (charpos + lenins),
2285 make_number (charpos),
2286 make_number (charpos + lenins),
2287 make_number (lendel));
2289 /* After an insertion, call the text properties
2290 insert-behind-hooks or insert-in-front-hooks. */
2291 if (lendel == 0)
2292 report_interval_modification (make_number (charpos),
2293 make_number (charpos + lenins));
2295 unbind_to (count, Qnil);
2298 Lisp_Object
2299 Fcombine_after_change_execute_1 (val)
2300 Lisp_Object val;
2302 Vcombine_after_change_calls = val;
2303 return val;
2306 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2307 Scombine_after_change_execute, 0, 0, 0,
2308 doc: /* This function is for use internally in `combine-after-change-calls'. */)
2311 int count = SPECPDL_INDEX ();
2312 EMACS_INT beg, end, change;
2313 EMACS_INT begpos, endpos;
2314 Lisp_Object tail;
2316 if (NILP (combine_after_change_list))
2317 return Qnil;
2319 /* It is rare for combine_after_change_buffer to be invalid, but
2320 possible. It can happen when combine-after-change-calls is
2321 non-nil, and insertion calls a file handler (e.g. through
2322 lock_file) which scribbles into a temp file -- cyd */
2323 if (!BUFFERP (combine_after_change_buffer)
2324 || NILP (XBUFFER (combine_after_change_buffer)->name))
2326 combine_after_change_list = Qnil;
2327 return Qnil;
2330 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2332 Fset_buffer (combine_after_change_buffer);
2334 /* # chars unchanged at beginning of buffer. */
2335 beg = Z - BEG;
2336 /* # chars unchanged at end of buffer. */
2337 end = beg;
2338 /* Total amount of insertion (negative for deletion). */
2339 change = 0;
2341 /* Scan the various individual changes,
2342 accumulating the range info in BEG, END and CHANGE. */
2343 for (tail = combine_after_change_list; CONSP (tail);
2344 tail = XCDR (tail))
2346 Lisp_Object elt;
2347 EMACS_INT thisbeg, thisend, thischange;
2349 /* Extract the info from the next element. */
2350 elt = XCAR (tail);
2351 if (! CONSP (elt))
2352 continue;
2353 thisbeg = XINT (XCAR (elt));
2355 elt = XCDR (elt);
2356 if (! CONSP (elt))
2357 continue;
2358 thisend = XINT (XCAR (elt));
2360 elt = XCDR (elt);
2361 if (! CONSP (elt))
2362 continue;
2363 thischange = XINT (XCAR (elt));
2365 /* Merge this range into the accumulated range. */
2366 change += thischange;
2367 if (thisbeg < beg)
2368 beg = thisbeg;
2369 if (thisend < end)
2370 end = thisend;
2373 /* Get the current start and end positions of the range
2374 that was changed. */
2375 begpos = BEG + beg;
2376 endpos = Z - end;
2378 /* We are about to handle these, so discard them. */
2379 combine_after_change_list = Qnil;
2381 /* Now run the after-change functions for real.
2382 Turn off the flag that defers them. */
2383 record_unwind_protect (Fcombine_after_change_execute_1,
2384 Vcombine_after_change_calls);
2385 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2386 update_compositions (begpos, endpos, CHECK_ALL);
2388 return unbind_to (count, Qnil);
2391 void
2392 syms_of_insdel ()
2394 staticpro (&combine_after_change_list);
2395 staticpro (&combine_after_change_buffer);
2396 combine_after_change_list = Qnil;
2397 combine_after_change_buffer = Qnil;
2399 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
2400 doc: /* Non-nil means enable debugging checks for invalid marker positions. */);
2401 check_markers_debug_flag = 0;
2402 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2403 doc: /* Used internally by the `combine-after-change-calls' macro. */);
2404 Vcombine_after_change_calls = Qnil;
2406 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks,
2407 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2408 This affects `before-change-functions' and `after-change-functions',
2409 as well as hooks attached to text properties and overlays. */);
2410 inhibit_modification_hooks = 0;
2411 Qinhibit_modification_hooks = intern ("inhibit-modification-hooks");
2412 staticpro (&Qinhibit_modification_hooks);
2414 defsubr (&Scombine_after_change_execute);
2417 /* arch-tag: 9b34b886-47d7-465e-a234-299af411b23d
2418 (do not change this comment) */