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