428
+ − 1 /* Buffer insertion/deletion and gap motion for XEmacs.
+ − 2 Copyright (C) 1985, 1986, 1991, 1992, 1993, 1994, 1995
+ − 3 Free Software Foundation, Inc.
+ − 4 Copyright (C) 1995 Sun Microsystems, Inc.
793
+ − 5 Copyright (C) 2001, 2002 Ben Wing.
428
+ − 6
+ − 7 This file is part of XEmacs.
+ − 8
+ − 9 XEmacs is free software; you can redistribute it and/or modify it
+ − 10 under the terms of the GNU General Public License as published by the
+ − 11 Free Software Foundation; either version 2, or (at your option) any
+ − 12 later version.
+ − 13
+ − 14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
+ − 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ − 16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ − 17 for more details.
+ − 18
+ − 19 You should have received a copy of the GNU General Public License
+ − 20 along with XEmacs; see the file COPYING. If not, write to
+ − 21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ − 22 Boston, MA 02111-1307, USA. */
+ − 23
+ − 24 /* Synched up with: Mule 2.0, FSF 19.30. Diverges significantly. */
+ − 25
+ − 26 /* This file has been Mule-ized. */
+ − 27
853
+ − 28 /* Original file from FSF, 1991.
+ − 29 Some changes for extents, c. 1991 by unknown Lucid author.
+ − 30 Completely rewritten December 1994, for Mule implementation by Ben Wing;
+ − 31 all buffer modification code ripped out of other files and consolidated
+ − 32 here.
+ − 33 Indirect buffers written c. 1997? by Hrvoje Niksic.
+ − 34 */
428
+ − 35
+ − 36 #include <config.h>
+ − 37 #include "lisp.h"
+ − 38
+ − 39 #include "buffer.h"
+ − 40 #include "device.h"
+ − 41 #include "frame.h"
+ − 42 #include "extents.h"
+ − 43 #include "insdel.h"
+ − 44 #include "lstream.h"
+ − 45 #include "redisplay.h"
+ − 46 #include "line-number.h"
+ − 47
+ − 48 /* Various macros modelled along the lines of those in buffer.h.
+ − 49 Purposefully omitted from buffer.h because files other than this
+ − 50 one should not be using them. */
+ − 51
+ − 52 /* Address of beginning of buffer. This is an lvalue because
+ − 53 BUFFER_ALLOC needs it to be. */
+ − 54 #define BUF_BEG_ADDR(buf) ((buf)->text->beg)
+ − 55
+ − 56 /* Set the address of beginning of buffer. */
+ − 57 #define SET_BUF_BEG_ADDR(buf, addr) do { (buf)->text->beg = (addr); } while (0)
+ − 58
+ − 59 /* Gap size. */
+ − 60 #define BUF_GAP_SIZE(buf) ((buf)->text->gap_size + 0)
+ − 61 #define BUF_END_GAP_SIZE(buf) ((buf)->text->end_gap_size + 0)
+ − 62 /* Set gap size. */
+ − 63 #define SET_BUF_GAP_SIZE(buf, value) \
+ − 64 do { (buf)->text->gap_size = (value); } while (0)
+ − 65 #define SET_BUF_END_GAP_SIZE(buf, value) \
+ − 66 do { (buf)->text->end_gap_size = (value); } while (0)
+ − 67
+ − 68 /* Gap location. */
826
+ − 69 #define BYTE_BUF_GPT(buf) ((buf)->text->gpt + 0)
+ − 70 #define BUF_GPT_ADDR(buf) (BUF_BEG_ADDR (buf) + BYTE_BUF_GPT (buf) - 1)
428
+ − 71
+ − 72 /* Set gap location. */
826
+ − 73 #define SET_BYTE_BUF_GPT(buf, value) do { (buf)->text->gpt = (value); } while (0)
428
+ − 74
+ − 75 /* Set end of buffer. */
+ − 76 #define SET_BOTH_BUF_Z(buf, val, bival) \
+ − 77 do \
+ − 78 { \
+ − 79 (buf)->text->z = (bival); \
+ − 80 (buf)->text->bufz = (val); \
+ − 81 } while (0)
+ − 82
+ − 83 /* Under Mule, we maintain two sentinels in the buffer: one at the
+ − 84 beginning of the gap, and one at the end of the buffer. This
+ − 85 allows us to move forward, examining bytes looking for the
+ − 86 end of a character, and not worry about running off the end.
+ − 87 We do not need corresponding sentinels when moving backwards
+ − 88 because we do not have to look past the beginning of a character
+ − 89 to find the beginning of the character.
+ − 90
+ − 91 Every time we change the beginning of the gap, we have to
+ − 92 call SET_GAP_SENTINEL().
+ − 93
+ − 94 Every time we change the total size (characters plus gap)
+ − 95 of the buffer, we have to call SET_END_SENTINEL().
+ − 96 */
+ − 97
+ − 98
+ − 99 #ifdef MULE
+ − 100 # define GAP_CAN_HOLD_SIZE_P(buf, len) (BUF_GAP_SIZE (buf) >= (len) + 1)
+ − 101 # define SET_GAP_SENTINEL(buf) (*BUF_GPT_ADDR (buf) = 0)
+ − 102 # define BUF_END_SENTINEL_SIZE 1
+ − 103 # define SET_END_SENTINEL(buf) \
826
+ − 104 (*(BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + BYTE_BUF_Z (buf) - 1) = 0)
428
+ − 105 #else
+ − 106 # define GAP_CAN_HOLD_SIZE_P(buf, len) (BUF_GAP_SIZE (buf) >= (len))
+ − 107 # define SET_GAP_SENTINEL(buf)
+ − 108 # define BUF_END_SENTINEL_SIZE 0
+ − 109 # define SET_END_SENTINEL(buf)
+ − 110 #endif
+ − 111
+ − 112
+ − 113 /************************************************************************/
+ − 114 /* point and marker adjustment */
+ − 115 /************************************************************************/
+ − 116
+ − 117 /* just_set_point() is the only place `PT' is an lvalue in all of emacs.
+ − 118 This function is called from set_buffer_point(), which is the function
+ − 119 that the SET_PT and BUF_SET_PT macros expand into, and from the
+ − 120 routines below that insert and delete text. (This is in cases where
+ − 121 the point marker logically doesn't move but PT (being a byte index)
+ − 122 needs to get adjusted.) */
+ − 123
+ − 124 /* Set point to a specified value. This is used only when the value
+ − 125 of point changes due to an insert or delete; it does not represent
+ − 126 a conceptual change in point as a marker. In particular, point is
+ − 127 not crossing any interval boundaries, so there's no need to use the
+ − 128 usual SET_PT macro. In fact it would be incorrect to do so, because
+ − 129 either the old or the new value of point is out of synch with the
+ − 130 current set of intervals. */
+ − 131
+ − 132 /* This gets called more than enough to make the function call
+ − 133 overhead a significant factor so we've turned it into a macro. */
665
+ − 134 #define JUST_SET_POINT(buf, charbpos, ind) \
428
+ − 135 do \
+ − 136 { \
665
+ − 137 buf->bufpt = (charbpos); \
428
+ − 138 buf->pt = (ind); \
+ − 139 } while (0)
+ − 140
+ − 141 /* Set a buffer's point. */
+ − 142
+ − 143 void
665
+ − 144 set_buffer_point (struct buffer *buf, Charbpos charbpos, Bytebpos bytpos)
428
+ − 145 {
826
+ − 146 assert (bytpos >= BYTE_BUF_BEGV (buf) && bytpos <= BYTE_BUF_ZV (buf));
+ − 147 if (bytpos == BYTE_BUF_PT (buf))
428
+ − 148 return;
665
+ − 149 JUST_SET_POINT (buf, charbpos, bytpos);
428
+ − 150 MARK_POINT_CHANGED;
+ − 151 assert (MARKERP (buf->point_marker));
665
+ − 152 XMARKER (buf->point_marker)->membpos =
+ − 153 bytebpos_to_membpos (buf, bytpos);
428
+ − 154
+ − 155 /* FSF makes sure that PT is not being set within invisible text.
+ − 156 However, this is the wrong place for that check. The check
+ − 157 should happen only at the next redisplay. */
+ − 158
+ − 159 /* Some old coder said:
+ − 160
+ − 161 "If there were to be hooks which were run when point entered/left an
+ − 162 extent, this would be the place to put them.
+ − 163
+ − 164 However, it's probably the case that such hooks should be implemented
+ − 165 using a post-command-hook instead, to avoid running the hooks as a
+ − 166 result of intermediate motion inside of save-excursions, for example."
+ − 167
+ − 168 I definitely agree with this. PT gets moved all over the place
+ − 169 and it would be a Bad Thing for any hooks to get called, both for
+ − 170 the reason above and because many callers are not prepared for
+ − 171 a GC within this function. --ben
+ − 172 */
+ − 173 }
+ − 174
+ − 175 /* Do the correct marker-like adjustment on MPOS (see below). FROM, TO,
+ − 176 and AMOUNT are as in adjust_markers(). If MPOS doesn't need to be
+ − 177 adjusted, nothing will happen. */
665
+ − 178 Membpos
+ − 179 do_marker_adjustment (Membpos mpos, Membpos from,
+ − 180 Membpos to, Bytecount amount)
428
+ − 181 {
+ − 182 if (amount > 0)
+ − 183 {
+ − 184 if (mpos > to && mpos < to + amount)
+ − 185 mpos = to + amount;
+ − 186 }
+ − 187 else
+ − 188 {
+ − 189 if (mpos > from + amount && mpos <= from)
+ − 190 mpos = from + amount;
+ − 191 }
+ − 192 if (mpos > from && mpos <= to)
+ − 193 mpos += amount;
+ − 194 return mpos;
+ − 195 }
+ − 196
+ − 197 /* Do the following:
+ − 198
+ − 199 (1) Add `amount' to the position of every marker in the current buffer
+ − 200 whose current position is between `from' (exclusive) and `to' (inclusive).
+ − 201
+ − 202 (2) Also, any markers past the outside of that interval, in the direction
+ − 203 of adjustment, are first moved back to the near end of the interval
+ − 204 and then adjusted by `amount'.
+ − 205
+ − 206 This function is called in two different cases: when a region of
+ − 207 characters adjacent to the gap is moved, causing the gap to shift
+ − 208 to the other side of the region (in this case, `from' and `to'
+ − 209 point to the old position of the region and there should be no
+ − 210 markers affected by (2) because they would be inside the gap),
+ − 211 or when a region of characters adjacent to the gap is wiped out,
+ − 212 causing the gap to increase to include the region (in this case,
+ − 213 `from' and `to' are the same, both pointing to the boundary
+ − 214 between the gap and the deleted region, and there are no markers
+ − 215 affected by (1)).
+ − 216
+ − 217 The reason for the use of exclusive and inclusive is that markers at
+ − 218 the gap always sit at the beginning, not at the end.
+ − 219 */
+ − 220
+ − 221 static void
665
+ − 222 adjust_markers (struct buffer *buf, Membpos from, Membpos to,
428
+ − 223 Bytecount amount)
+ − 224 {
440
+ − 225 Lisp_Marker *m;
428
+ − 226
+ − 227 for (m = BUF_MARKERS (buf); m; m = marker_next (m))
665
+ − 228 m->membpos = do_marker_adjustment (m->membpos, from, to, amount);
428
+ − 229 }
+ − 230
+ − 231 /* Adjust markers whose insertion-type is t
+ − 232 for an insertion of AMOUNT characters at POS. */
+ − 233
+ − 234 static void
665
+ − 235 adjust_markers_for_insert (struct buffer *buf, Membpos ind, Bytecount amount)
428
+ − 236 {
440
+ − 237 Lisp_Marker *m;
428
+ − 238
+ − 239 for (m = BUF_MARKERS (buf); m; m = marker_next (m))
+ − 240 {
665
+ − 241 if (m->insertion_type && m->membpos == ind)
+ − 242 m->membpos += amount;
428
+ − 243 }
+ − 244 }
+ − 245
+ − 246
+ − 247 /************************************************************************/
+ − 248 /* Routines for dealing with the gap */
+ − 249 /************************************************************************/
+ − 250
+ − 251 /* maximum amount of memory moved in a single chunk. Increasing this
+ − 252 value improves gap-motion efficiency but decreases QUIT responsiveness
+ − 253 time. Was 32000 but today's processors are faster and files are
+ − 254 bigger. --ben */
+ − 255 #define GAP_MOVE_CHUNK 300000
+ − 256
+ − 257 /* Move the gap to POS, which is less than the current GPT. */
+ − 258
+ − 259 static void
665
+ − 260 gap_left (struct buffer *buf, Bytebpos pos)
428
+ − 261 {
867
+ − 262 Ibyte *to, *from;
428
+ − 263 Bytecount i;
665
+ − 264 Bytebpos new_s1;
428
+ − 265 struct buffer *mbuf;
+ − 266 Lisp_Object bufcons;
+ − 267
+ − 268 from = BUF_GPT_ADDR (buf);
+ − 269 to = from + BUF_GAP_SIZE (buf);
826
+ − 270 new_s1 = BYTE_BUF_GPT (buf);
428
+ − 271
+ − 272 /* Now copy the characters. To move the gap down,
+ − 273 copy characters up. */
+ − 274
+ − 275 while (1)
+ − 276 {
+ − 277 /* I gets number of characters left to copy. */
+ − 278 i = new_s1 - pos;
+ − 279 if (i == 0)
+ − 280 break;
+ − 281 /* If a quit is requested, stop copying now.
+ − 282 Change POS to be where we have actually moved the gap to. */
+ − 283 if (QUITP)
+ − 284 {
+ − 285 pos = new_s1;
+ − 286 break;
+ − 287 }
+ − 288 /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */
+ − 289 if (i > GAP_MOVE_CHUNK)
+ − 290 i = GAP_MOVE_CHUNK;
440
+ − 291
+ − 292 if (i >= 128)
428
+ − 293 {
+ − 294 new_s1 -= i;
440
+ − 295 from -= i;
+ − 296 to -= i;
428
+ − 297 memmove (to, from, i);
+ − 298 }
+ − 299 else
+ − 300 {
+ − 301 new_s1 -= i;
+ − 302 while (--i >= 0)
+ − 303 *--to = *--from;
+ − 304 }
+ − 305 }
+ − 306
+ − 307 /* Adjust markers, and buffer data structure, to put the gap at POS.
+ − 308 POS is where the loop above stopped, which may be what was specified
+ − 309 or may be where a quit was detected. */
+ − 310 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 311 {
826
+ − 312 adjust_markers (mbuf, pos, BYTE_BUF_GPT (mbuf), BUF_GAP_SIZE (mbuf));
428
+ − 313 }
+ − 314 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 315 {
826
+ − 316 adjust_extents (wrap_buffer (mbuf), pos, BYTE_BUF_GPT (mbuf),
428
+ − 317 BUF_GAP_SIZE (mbuf));
+ − 318 }
826
+ − 319 SET_BYTE_BUF_GPT (buf, pos);
428
+ − 320 SET_GAP_SENTINEL (buf);
+ − 321 #ifdef ERROR_CHECK_EXTENTS
+ − 322 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 323 {
771
+ − 324 sledgehammer_extent_check (wrap_buffer (mbuf));
428
+ − 325 }
+ − 326 #endif
+ − 327 QUIT;
+ − 328 }
+ − 329
+ − 330 static void
665
+ − 331 gap_right (struct buffer *buf, Bytebpos pos)
428
+ − 332 {
867
+ − 333 Ibyte *to, *from;
428
+ − 334 Bytecount i;
665
+ − 335 Bytebpos new_s1;
428
+ − 336 struct buffer *mbuf;
+ − 337 Lisp_Object bufcons;
+ − 338
+ − 339 to = BUF_GPT_ADDR (buf);
+ − 340 from = to + BUF_GAP_SIZE (buf);
826
+ − 341 new_s1 = BYTE_BUF_GPT (buf);
428
+ − 342
+ − 343 /* Now copy the characters. To move the gap up,
+ − 344 copy characters down. */
+ − 345
+ − 346 while (1)
+ − 347 {
+ − 348 /* I gets number of characters left to copy. */
+ − 349 i = pos - new_s1;
+ − 350 if (i == 0)
+ − 351 break;
+ − 352 /* If a quit is requested, stop copying now.
+ − 353 Change POS to be where we have actually moved the gap to. */
+ − 354 if (QUITP)
+ − 355 {
+ − 356 pos = new_s1;
+ − 357 break;
+ − 358 }
+ − 359 /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */
+ − 360 if (i > GAP_MOVE_CHUNK)
+ − 361 i = GAP_MOVE_CHUNK;
440
+ − 362
+ − 363 if (i >= 128)
428
+ − 364 {
+ − 365 new_s1 += i;
+ − 366 memmove (to, from, i);
440
+ − 367 from += i;
+ − 368 to += i;
428
+ − 369 }
+ − 370 else
+ − 371 {
+ − 372 new_s1 += i;
+ − 373 while (--i >= 0)
+ − 374 *to++ = *from++;
+ − 375 }
+ − 376 }
+ − 377
+ − 378 {
+ − 379 int gsize = BUF_GAP_SIZE (buf);
+ − 380 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 381 {
826
+ − 382 adjust_markers (mbuf, BYTE_BUF_GPT (mbuf) + gsize, pos + gsize, - gsize);
428
+ − 383 }
+ − 384 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 385 {
826
+ − 386 adjust_extents (wrap_buffer (mbuf), BYTE_BUF_GPT (mbuf) + gsize,
428
+ − 387 pos + gsize, - gsize);
+ − 388 }
826
+ − 389 SET_BYTE_BUF_GPT (buf, pos);
428
+ − 390 SET_GAP_SENTINEL (buf);
+ − 391 #ifdef ERROR_CHECK_EXTENTS
+ − 392 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 393 {
771
+ − 394 sledgehammer_extent_check (wrap_buffer (mbuf));
428
+ − 395 }
+ − 396 #endif
+ − 397 }
826
+ − 398 if (pos == BYTE_BUF_Z (buf))
428
+ − 399 {
+ − 400 /* merge gap with end gap */
+ − 401
+ − 402 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + BUF_END_GAP_SIZE (buf));
+ − 403 SET_BUF_END_GAP_SIZE (buf, 0);
+ − 404 SET_END_SENTINEL (buf);
+ − 405 }
+ − 406
+ − 407 QUIT;
+ − 408 }
+ − 409
+ − 410 /* Move gap to position `pos'.
+ − 411 Note that this can quit! */
+ − 412
+ − 413 static void
665
+ − 414 move_gap (struct buffer *buf, Bytebpos pos)
428
+ − 415 {
+ − 416 if (! BUF_BEG_ADDR (buf))
+ − 417 abort ();
826
+ − 418 if (pos < BYTE_BUF_GPT (buf))
428
+ − 419 gap_left (buf, pos);
826
+ − 420 else if (pos > BYTE_BUF_GPT (buf))
428
+ − 421 gap_right (buf, pos);
+ − 422 }
+ − 423
+ − 424 /* Merge the end gap into the gap */
+ − 425
+ − 426 static void
+ − 427 merge_gap_with_end_gap (struct buffer *buf)
+ − 428 {
+ − 429 Lisp_Object tem;
665
+ − 430 Bytebpos real_gap_loc;
428
+ − 431 Bytecount old_gap_size;
+ − 432 Bytecount increment;
+ − 433
+ − 434 increment = BUF_END_GAP_SIZE (buf);
+ − 435 SET_BUF_END_GAP_SIZE (buf, 0);
+ − 436
+ − 437 if (increment > 0)
+ − 438 {
+ − 439 /* Prevent quitting in move_gap. */
+ − 440 tem = Vinhibit_quit;
+ − 441 Vinhibit_quit = Qt;
+ − 442
826
+ − 443 real_gap_loc = BYTE_BUF_GPT (buf);
428
+ − 444 old_gap_size = BUF_GAP_SIZE (buf);
+ − 445
+ − 446 /* Pretend the end gap is the gap */
826
+ − 447 SET_BYTE_BUF_GPT (buf, BYTE_BUF_Z (buf) + BUF_GAP_SIZE (buf));
428
+ − 448 SET_BUF_GAP_SIZE (buf, increment);
+ − 449
+ − 450 /* Move the new gap down to be consecutive with the end of the old one.
+ − 451 This adjusts the markers properly too. */
+ − 452 gap_left (buf, real_gap_loc + old_gap_size);
+ − 453
+ − 454 /* Now combine the two into one large gap. */
+ − 455 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + old_gap_size);
826
+ − 456 SET_BYTE_BUF_GPT (buf, real_gap_loc);
428
+ − 457 SET_GAP_SENTINEL (buf);
+ − 458
+ − 459 /* We changed the total size of the buffer (including gap),
+ − 460 so we need to fix up the end sentinel. */
+ − 461 SET_END_SENTINEL (buf);
+ − 462
+ − 463 Vinhibit_quit = tem;
+ − 464 }
+ − 465 }
+ − 466
+ − 467 /* Make the gap INCREMENT bytes longer. */
+ − 468
+ − 469 static void
+ − 470 make_gap (struct buffer *buf, Bytecount increment)
+ − 471 {
867
+ − 472 Ibyte *result;
428
+ − 473 Lisp_Object tem;
665
+ − 474 Bytebpos real_gap_loc;
428
+ − 475 Bytecount old_gap_size;
+ − 476
+ − 477 /* If we have to get more space, get enough to last a while. We use
+ − 478 a geometric progression that saves on realloc space. */
826
+ − 479 increment += 2000 + ((BYTE_BUF_Z (buf) - BYTE_BUF_BEG (buf)) / 8);
+ − 480 /* Make sure the gap is always aligned properly in case we're using a
+ − 481 16-bit or 32-bit fixed-width format. (Other sizes should already be
+ − 482 aligned in such a case.) */
+ − 483 increment = MAX_ALIGN_SIZE (increment);
428
+ − 484
+ − 485 if (increment > BUF_END_GAP_SIZE (buf))
+ − 486 {
+ − 487 /* Don't allow a buffer size that won't fit in an int
+ − 488 even if it will fit in a Lisp integer.
+ − 489 That won't work because so many places use `int'. */
+ − 490
+ − 491 if (BUF_Z (buf) - BUF_BEG (buf) + BUF_GAP_SIZE (buf) + increment
+ − 492 > EMACS_INT_MAX)
563
+ − 493 out_of_memory ("Maximum buffer size exceeded", Qunbound);
428
+ − 494
+ − 495 result = BUFFER_REALLOC (buf->text->beg,
826
+ − 496 BYTE_BUF_Z (buf) - BYTE_BUF_BEG (buf) +
428
+ − 497 BUF_GAP_SIZE (buf) + increment +
+ − 498 BUF_END_SENTINEL_SIZE);
+ − 499 if (result == 0)
+ − 500 memory_full ();
+ − 501
+ − 502 SET_BUF_BEG_ADDR (buf, result);
+ − 503 }
+ − 504 else
+ − 505 increment = BUF_END_GAP_SIZE (buf);
+ − 506
+ − 507 /* Prevent quitting in move_gap. */
+ − 508 tem = Vinhibit_quit;
+ − 509 Vinhibit_quit = Qt;
+ − 510
826
+ − 511 real_gap_loc = BYTE_BUF_GPT (buf);
428
+ − 512 old_gap_size = BUF_GAP_SIZE (buf);
+ − 513
+ − 514 /* Call the newly allocated space a gap at the end of the whole space. */
826
+ − 515 SET_BYTE_BUF_GPT (buf, BYTE_BUF_Z (buf) + BUF_GAP_SIZE (buf));
428
+ − 516 SET_BUF_GAP_SIZE (buf, increment);
+ − 517
+ − 518 SET_BUF_END_GAP_SIZE (buf, 0);
+ − 519
+ − 520 /* Move the new gap down to be consecutive with the end of the old one.
+ − 521 This adjusts the markers properly too. */
+ − 522 gap_left (buf, real_gap_loc + old_gap_size);
+ − 523
+ − 524 /* Now combine the two into one large gap. */
+ − 525 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + old_gap_size);
826
+ − 526 SET_BYTE_BUF_GPT (buf, real_gap_loc);
428
+ − 527 SET_GAP_SENTINEL (buf);
+ − 528
+ − 529 /* We changed the total size of the buffer (including gap),
+ − 530 so we need to fix up the end sentinel. */
+ − 531 SET_END_SENTINEL (buf);
+ − 532
+ − 533 Vinhibit_quit = tem;
+ − 534 }
+ − 535
+ − 536
+ − 537 /************************************************************************/
+ − 538 /* Before/after-change processing */
+ − 539 /************************************************************************/
+ − 540
+ − 541 /* Those magic changes ... */
+ − 542
+ − 543 static void
665
+ − 544 buffer_signal_changed_region (struct buffer *buf, Charbpos start,
+ − 545 Charbpos end)
428
+ − 546 {
+ − 547 /* The changed region is recorded as the number of unchanged
+ − 548 characters from the beginning and from the end of the
+ − 549 buffer. This obviates much of the need of shifting the
+ − 550 region around to compensate for insertions and deletions.
+ − 551 */
+ − 552 if (buf->changes->begin_unchanged < 0 ||
+ − 553 buf->changes->begin_unchanged > start - BUF_BEG (buf))
+ − 554 buf->changes->begin_unchanged = start - BUF_BEG (buf);
+ − 555 if (buf->changes->end_unchanged < 0 ||
+ − 556 buf->changes->end_unchanged > BUF_Z (buf) - end)
+ − 557 buf->changes->end_unchanged = BUF_Z (buf) - end;
+ − 558 }
+ − 559
+ − 560 void
665
+ − 561 buffer_extent_signal_changed_region (struct buffer *buf, Charbpos start,
+ − 562 Charbpos end)
428
+ − 563 {
+ − 564 if (buf->changes->begin_extent_unchanged < 0 ||
+ − 565 buf->changes->begin_extent_unchanged > start - BUF_BEG (buf))
+ − 566 buf->changes->begin_extent_unchanged = start - BUF_BEG (buf);
+ − 567 if (buf->changes->end_extent_unchanged < 0 ||
+ − 568 buf->changes->end_extent_unchanged > BUF_Z (buf) - end)
+ − 569 buf->changes->end_extent_unchanged = BUF_Z (buf) - end;
+ − 570 }
+ − 571
+ − 572 void
+ − 573 buffer_reset_changes (struct buffer *buf)
+ − 574 {
+ − 575 buf->changes->begin_unchanged = -1;
+ − 576 buf->changes->end_unchanged = -1;
+ − 577 buf->changes->begin_extent_unchanged = -1;
+ − 578 buf->changes->end_extent_unchanged = -1;
+ − 579 buf->changes->newline_was_deleted = 0;
+ − 580 }
+ − 581
+ − 582 static void
665
+ − 583 signal_after_change (struct buffer *buf, Charbpos start, Charbpos orig_end,
+ − 584 Charbpos new_end);
428
+ − 585
+ − 586
+ − 587 /* Call the after-change-functions according to the changes made so far
+ − 588 and treat all further changes as single until the outermost
+ − 589 multiple change exits. This is called when the outermost multiple
+ − 590 change exits and when someone is trying to make a change that violates
+ − 591 the constraints specified in begin_multiple_change(), typically
+ − 592 when nested multiple-change sessions occur. (There are smarter ways of
+ − 593 dealing with nested multiple changes, but these rarely occur so there's
+ − 594 probably no point in it.) */
+ − 595
+ − 596 /* #### This needs to keep track of what actually changed and only
+ − 597 call the after-change functions on that region. */
+ − 598
+ − 599 static void
+ − 600 cancel_multiple_change (struct buffer *buf)
+ − 601 {
+ − 602 /* This function can GC */
+ − 603 /* Call the after-change-functions except when they've already been
+ − 604 called or when there were no changes made to the buffer at all. */
+ − 605 if (buf->text->changes->mc_begin != 0 &&
+ − 606 buf->text->changes->mc_begin_signaled)
+ − 607 {
665
+ − 608 Charbpos real_mc_begin = buf->text->changes->mc_begin;
428
+ − 609 buf->text->changes->mc_begin = 0;
+ − 610
+ − 611 signal_after_change (buf, real_mc_begin, buf->text->changes->mc_orig_end,
+ − 612 buf->text->changes->mc_new_end);
+ − 613 }
+ − 614 else
+ − 615 {
+ − 616 buf->text->changes->mc_begin = 0;
+ − 617 }
+ − 618 }
+ − 619
+ − 620 /* this is an unwind_protect, to ensure that the after-change-functions
+ − 621 get called even in a non-local exit. */
+ − 622
+ − 623 static Lisp_Object
+ − 624 multiple_change_finish_up (Lisp_Object buffer)
+ − 625 {
+ − 626 struct buffer *buf = XBUFFER (buffer);
+ − 627
+ − 628 /* #### I don't know whether or not it should even be possible to
+ − 629 get here with a dead buffer (though given how it is called I can
+ − 630 see how it might be). In any case, there isn't time before 19.14
+ − 631 to find out. */
+ − 632 if (!BUFFER_LIVE_P (buf))
+ − 633 return Qnil;
+ − 634
+ − 635 /* This function can GC */
+ − 636 buf->text->changes->in_multiple_change = 0; /* do this first so that
+ − 637 errors in the after-change
+ − 638 functions don't mess things
+ − 639 up. */
+ − 640 cancel_multiple_change (buf);
+ − 641 return Qnil;
+ − 642 }
+ − 643
+ − 644 /* Call this function when you're about to make a number of buffer changes
+ − 645 that should be considered a single change. (e.g. `replace-match' calls
+ − 646 this.) You need to specify the START and END of the region that is
+ − 647 going to be changed so that the before-change-functions are called
+ − 648 with the correct arguments. The after-change region is calculated
+ − 649 automatically, however, and if changes somehow or other happen outside
+ − 650 of the specified region, that will also be handled correctly.
+ − 651
+ − 652 begin_multiple_change() returns a number (actually a specpdl depth)
438
+ − 653 that you must pass to end_multiple_change() when you are done.
+ − 654
+ − 655 FSF Emacs 20 implements a similar feature, accessible from Lisp
+ − 656 through a `combine-after-change-calls' special form, which is
+ − 657 essentially equivalent to this function. We should consider
+ − 658 whether we want to introduce a similar Lisp form. */
428
+ − 659
+ − 660 int
665
+ − 661 begin_multiple_change (struct buffer *buf, Charbpos start, Charbpos end)
428
+ − 662 {
+ − 663 /* This function can GC */
+ − 664 int count = -1;
+ − 665 if (buf->text->changes->in_multiple_change)
+ − 666 {
+ − 667 if (buf->text->changes->mc_begin != 0 &&
+ − 668 (start < buf->text->changes->mc_begin ||
+ − 669 end > buf->text->changes->mc_new_end))
+ − 670 cancel_multiple_change (buf);
+ − 671 }
+ − 672 else
+ − 673 {
+ − 674 Lisp_Object buffer;
+ − 675
+ − 676 buf->text->changes->mc_begin = start;
+ − 677 buf->text->changes->mc_orig_end = buf->text->changes->mc_new_end = end;
+ − 678 buf->text->changes->mc_begin_signaled = 0;
+ − 679 count = specpdl_depth ();
793
+ − 680 buffer = wrap_buffer (buf);
428
+ − 681 record_unwind_protect (multiple_change_finish_up, buffer);
+ − 682 }
+ − 683 buf->text->changes->in_multiple_change++;
+ − 684 /* We don't call before-change-functions until signal_before_change()
+ − 685 is called, in case there is a read-only or other error. */
+ − 686 return count;
+ − 687 }
+ − 688
+ − 689 void
+ − 690 end_multiple_change (struct buffer *buf, int count)
+ − 691 {
+ − 692 assert (buf->text->changes->in_multiple_change > 0);
+ − 693 buf->text->changes->in_multiple_change--;
+ − 694 if (!buf->text->changes->in_multiple_change)
771
+ − 695 unbind_to (count);
428
+ − 696 }
+ − 697
+ − 698 static int inside_change_hook;
+ − 699
+ − 700 static Lisp_Object
+ − 701 change_function_restore (Lisp_Object buffer)
+ − 702 {
+ − 703 /* We should first reset the variable and then change the buffer,
+ − 704 because Fset_buffer() can throw. */
+ − 705 inside_change_hook = 0;
438
+ − 706 if (XBUFFER (buffer) != current_buffer)
+ − 707 Fset_buffer (buffer);
428
+ − 708 return Qnil;
+ − 709 }
+ − 710
+ − 711 static int in_first_change;
+ − 712
+ − 713 static Lisp_Object
+ − 714 first_change_hook_restore (Lisp_Object buffer)
+ − 715 {
+ − 716 in_first_change = 0;
+ − 717 Fset_buffer (buffer);
+ − 718 return Qnil;
+ − 719 }
+ − 720
+ − 721 /* Signal an initial modification to the buffer. */
+ − 722
+ − 723 static void
+ − 724 signal_first_change (struct buffer *buf)
+ − 725 {
+ − 726 /* This function can GC */
793
+ − 727 Lisp_Object buffer = wrap_buffer (current_buffer);
+ − 728
428
+ − 729
+ − 730 if (!in_first_change)
+ − 731 {
+ − 732 if (!NILP (symbol_value_in_buffer (Qfirst_change_hook, buffer)))
+ − 733 {
+ − 734 int speccount = specpdl_depth ();
+ − 735 record_unwind_protect (first_change_hook_restore, buffer);
+ − 736 set_buffer_internal (buf);
+ − 737 in_first_change = 1;
853
+ − 738 run_hook_trapping_problems
+ − 739 (0, Qfirst_change_hook,
+ − 740 INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION);
771
+ − 741 unbind_to (speccount);
428
+ − 742 }
+ − 743 }
+ − 744 }
+ − 745
+ − 746 /* Signal a change to the buffer immediately before it happens.
+ − 747 START and END are the bounds of the text to be changed. */
+ − 748
+ − 749 static void
665
+ − 750 signal_before_change (struct buffer *buf, Charbpos start, Charbpos end)
428
+ − 751 {
+ − 752 /* This function can GC */
+ − 753 struct buffer *mbuf;
+ − 754 Lisp_Object bufcons;
+ − 755
+ − 756 if (!inside_change_hook)
+ − 757 {
+ − 758 Lisp_Object buffer;
438
+ − 759 int speccount;
428
+ − 760
+ − 761 /* Are we in a multiple-change session? */
+ − 762 if (buf->text->changes->in_multiple_change &&
+ − 763 buf->text->changes->mc_begin != 0)
+ − 764 {
+ − 765 /* If we're violating the constraints of the session,
+ − 766 call the after-change-functions as necessary for the
+ − 767 changes already made and treat further changes as
+ − 768 single. */
+ − 769 if (start < buf->text->changes->mc_begin ||
+ − 770 end > buf->text->changes->mc_new_end)
+ − 771 cancel_multiple_change (buf);
+ − 772 /* Do nothing if this is not the first change in the session. */
+ − 773 else if (buf->text->changes->mc_begin_signaled)
+ − 774 return;
+ − 775 else
+ − 776 {
+ − 777 /* First time through; call the before-change-functions
+ − 778 specifying the entire region to be changed. (Note that
+ − 779 we didn't call before-change-functions in
+ − 780 begin_multiple_change() because the buffer might be
+ − 781 read-only, etc.) */
+ − 782 start = buf->text->changes->mc_begin;
+ − 783 end = buf->text->changes->mc_new_end;
+ − 784 }
+ − 785 }
+ − 786
+ − 787 /* If buffer is unmodified, run a special hook for that case. */
+ − 788 if (BUF_SAVE_MODIFF (buf) >= BUF_MODIFF (buf))
+ − 789 {
+ − 790 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 791 {
+ − 792 signal_first_change (mbuf);
+ − 793 }
+ − 794 }
+ − 795
+ − 796 /* Now in any case run the before-change-functions if any. */
438
+ − 797 speccount = specpdl_depth ();
+ − 798 record_unwind_protect (change_function_restore, Fcurrent_buffer ());
+ − 799 inside_change_hook = 1;
428
+ − 800
+ − 801 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 802 {
793
+ − 803 buffer = wrap_buffer (mbuf);
428
+ − 804 if (!NILP (symbol_value_in_buffer (Qbefore_change_functions, buffer))
+ − 805 /* Obsolete, for compatibility */
+ − 806 || !NILP (symbol_value_in_buffer (Qbefore_change_function, buffer)))
+ − 807 {
+ − 808 set_buffer_internal (buf);
853
+ − 809 va_run_hook_with_args_trapping_problems
+ − 810 (0, Qbefore_change_functions, 2,
+ − 811 make_int (start), make_int (end),
+ − 812 INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION);
428
+ − 813 /* Obsolete, for compatibility */
853
+ − 814 va_run_hook_with_args_trapping_problems
+ − 815 (0, Qbefore_change_function, 2,
+ − 816 make_int (start), make_int (end),
+ − 817 INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION);
428
+ − 818 }
+ − 819 }
+ − 820
438
+ − 821 /* Make sure endpoints remain valid. before-change-functions
+ − 822 might have modified the buffer. */
+ − 823 if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
+ − 824 if (start > BUF_ZV (buf)) start = BUF_ZV (buf);
+ − 825 if (end < BUF_BEGV (buf)) end = BUF_BEGV (buf);
+ − 826 if (end > BUF_ZV (buf)) end = BUF_ZV (buf);
+ − 827
428
+ − 828 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 829 {
826
+ − 830 report_extent_modification (wrap_buffer (mbuf), start, end, 0);
428
+ − 831 }
771
+ − 832 unbind_to (speccount);
428
+ − 833
+ − 834 /* Only now do we indicate that the before-change-functions have
+ − 835 been called, in case some function throws out. */
+ − 836 buf->text->changes->mc_begin_signaled = 1;
+ − 837 }
+ − 838 }
+ − 839
+ − 840 /* Signal a change immediately after it happens.
665
+ − 841 START is the charbpos of the start of the changed text.
+ − 842 ORIG_END is the charbpos of the end of the before-changed text.
+ − 843 NEW_END is the charbpos of the end of the after-changed text.
428
+ − 844 */
+ − 845
+ − 846 static void
665
+ − 847 signal_after_change (struct buffer *buf, Charbpos start, Charbpos orig_end,
+ − 848 Charbpos new_end)
428
+ − 849 {
+ − 850 /* This function can GC */
+ − 851 struct buffer *mbuf;
+ − 852 Lisp_Object bufcons;
+ − 853
+ − 854 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 855 {
+ − 856 /* always do this. */
+ − 857 buffer_signal_changed_region (mbuf, start, new_end);
+ − 858 }
826
+ − 859 #ifdef USE_C_FONT_LOCK
428
+ − 860 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 861 {
+ − 862 /* #### This seems inefficient. Wouldn't it be better to just
+ − 863 keep one cache per base buffer? */
+ − 864 font_lock_maybe_update_syntactic_caches (mbuf, start, orig_end, new_end);
+ − 865 }
826
+ − 866 #endif /* USE_C_FONT_LOCK */
428
+ − 867
+ − 868 if (!inside_change_hook)
+ − 869 {
+ − 870 Lisp_Object buffer;
438
+ − 871 int speccount;
428
+ − 872
+ − 873 if (buf->text->changes->in_multiple_change &&
+ − 874 buf->text->changes->mc_begin != 0)
+ − 875 {
+ − 876 assert (start >= buf->text->changes->mc_begin &&
+ − 877 start <= buf->text->changes->mc_new_end);
+ − 878 assert (orig_end >= buf->text->changes->mc_begin &&
+ − 879 orig_end <= buf->text->changes->mc_new_end);
+ − 880 buf->text->changes->mc_new_end += new_end - orig_end;
+ − 881 return; /* after-change-functions signalled when all changes done */
+ − 882 }
+ − 883
438
+ − 884 speccount = specpdl_depth ();
+ − 885 record_unwind_protect (change_function_restore, Fcurrent_buffer ());
+ − 886 inside_change_hook = 1;
428
+ − 887 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 888 {
793
+ − 889 buffer = wrap_buffer (mbuf);
428
+ − 890
+ − 891 if (!NILP (symbol_value_in_buffer (Qafter_change_functions, buffer))
+ − 892 /* Obsolete, for compatibility */
+ − 893 || !NILP (symbol_value_in_buffer (Qafter_change_function, buffer)))
+ − 894 {
+ − 895 set_buffer_internal (buf);
+ − 896 /* The actual after-change functions take slightly
+ − 897 different arguments than what we were passed. */
853
+ − 898 va_run_hook_with_args_trapping_problems
+ − 899 (0, Qafter_change_functions, 3,
+ − 900 make_int (start), make_int (new_end),
+ − 901 make_int (orig_end - start),
+ − 902 INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION);
428
+ − 903 /* Obsolete, for compatibility */
853
+ − 904 va_run_hook_with_args_trapping_problems
+ − 905 (0, Qafter_change_function, 3,
+ − 906 make_int (start), make_int (new_end),
+ − 907 make_int (orig_end - start),
+ − 908 INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION);
428
+ − 909 }
+ − 910 }
+ − 911
438
+ − 912 /* Make sure endpoints remain valid. after-change-functions
+ − 913 might have modified the buffer. */
+ − 914 if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
+ − 915 if (start > BUF_ZV (buf)) start = BUF_ZV (buf);
+ − 916 if (new_end < BUF_BEGV (buf)) new_end = BUF_BEGV (buf);
+ − 917 if (new_end > BUF_ZV (buf)) new_end = BUF_ZV (buf);
+ − 918 if (orig_end < BUF_BEGV (buf)) orig_end = BUF_BEGV (buf);
+ − 919 if (orig_end > BUF_ZV (buf)) orig_end = BUF_ZV (buf);
+ − 920
428
+ − 921 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 922 {
793
+ − 923 buffer = wrap_buffer (mbuf);
438
+ − 924 report_extent_modification (buffer, start, new_end, 1);
428
+ − 925 }
771
+ − 926 unbind_to (speccount); /* sets inside_change_hook back to 0 */
428
+ − 927 }
+ − 928 }
+ − 929
+ − 930 /* Call this if you're about to change the region of BUFFER from START
+ − 931 to END. This checks the read-only properties of the region, calls
+ − 932 the necessary modification hooks, and warns the next redisplay that
+ − 933 it should pay attention to that area. */
+ − 934
+ − 935 static void
665
+ − 936 prepare_to_modify_buffer (struct buffer *buf, Charbpos start, Charbpos end,
428
+ − 937 int lockit)
+ − 938 {
+ − 939 /* This function can GC */
+ − 940 /* dmoore - This function can also kill the buffer buf, the current
+ − 941 buffer, and do anything it pleases. So if you call it, be
+ − 942 careful. */
+ − 943 struct buffer *mbuf;
+ − 944 Lisp_Object buffer, bufcons;
+ − 945 struct gcpro gcpro1;
+ − 946
+ − 947 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 948 {
853
+ − 949 check_allowed_operation (OPERATION_MODIFY_BUFFER_TEXT,
+ − 950 wrap_buffer (mbuf), Qnil);
428
+ − 951 barf_if_buffer_read_only (mbuf, start, end);
+ − 952 }
+ − 953
+ − 954 /* if this is the first modification, see about locking the buffer's
+ − 955 file */
793
+ − 956 buffer = wrap_buffer (buf);
428
+ − 957 GCPRO1 (buffer);
+ − 958 if (!NILP (buf->filename) && lockit &&
+ − 959 BUF_SAVE_MODIFF (buf) >= BUF_MODIFF (buf))
+ − 960 {
758
+ − 961 #ifdef CLASH_DETECTION
+ − 962 if (!NILP (buf->file_truename))
+ − 963 /* Make binding buffer-file-name to nil effective. */
+ − 964 lock_file (buf->file_truename);
+ − 965 #else
428
+ − 966 /* At least warn if this file has changed on disk since it was visited.*/
+ − 967 if (NILP (Fverify_visited_file_modtime (buffer))
+ − 968 && !NILP (Ffile_exists_p (buf->filename)))
+ − 969 call1_in_buffer (buf, intern ("ask-user-about-supersession-threat"),
+ − 970 buf->filename);
+ − 971 #endif /* not CLASH_DETECTION */
+ − 972 }
+ − 973 UNGCPRO;
+ − 974
+ − 975 /* #### dmoore - is this reasonable in case of buf being killed above? */
+ − 976 if (!BUFFER_LIVE_P (buf))
+ − 977 return;
+ − 978
+ − 979 signal_before_change (buf, start, end);
+ − 980
+ − 981 #ifdef REGION_CACHE_NEEDS_WORK
+ − 982 if (buf->newline_cache)
+ − 983 invalidate_region_cache (buf,
+ − 984 buf->newline_cache,
+ − 985 start - BUF_BEG (buf), BUF_Z (buf) - end);
+ − 986 if (buf->width_run_cache)
+ − 987 invalidate_region_cache (buf,
+ − 988 buf->width_run_cache,
+ − 989 start - BUF_BEG (buf), BUF_Z (buf) - end);
+ − 990 #endif
+ − 991
+ − 992 #if 0 /* FSFmacs */
+ − 993 Vdeactivate_mark = Qt;
+ − 994 #endif
+ − 995
+ − 996 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 997 {
+ − 998 mbuf->point_before_scroll = Qnil;
+ − 999 }
+ − 1000 }
+ − 1001
+ − 1002
+ − 1003 /************************************************************************/
+ − 1004 /* Insertion of strings */
+ − 1005 /************************************************************************/
+ − 1006
+ − 1007 void
867
+ − 1008 fixup_internal_substring (const Ibyte *nonreloc, Lisp_Object reloc,
428
+ − 1009 Bytecount offset, Bytecount *len)
+ − 1010 {
+ − 1011 assert ((nonreloc && NILP (reloc)) || (!nonreloc && STRINGP (reloc)));
+ − 1012
+ − 1013 if (*len < 0)
+ − 1014 {
+ − 1015 if (nonreloc)
442
+ − 1016 *len = strlen ((const char *) nonreloc) - offset;
428
+ − 1017 else
+ − 1018 *len = XSTRING_LENGTH (reloc) - offset;
+ − 1019 }
800
+ − 1020 #ifdef ERROR_CHECK_TEXT
428
+ − 1021 assert (*len >= 0);
+ − 1022 if (STRINGP (reloc))
+ − 1023 {
+ − 1024 assert (offset >= 0 && offset <= XSTRING_LENGTH (reloc));
+ − 1025 assert (offset + *len <= XSTRING_LENGTH (reloc));
+ − 1026 }
+ − 1027 #endif
+ − 1028 }
+ − 1029
665
+ − 1030 /* Insert a string into BUF at Charbpos POS. The string data comes
428
+ − 1031 from one of two sources: constant, non-relocatable data (specified
+ − 1032 in NONRELOC), or a Lisp string object (specified in RELOC), which
+ − 1033 is relocatable and may have extent data that needs to be copied
+ − 1034 into the buffer. OFFSET and LENGTH specify the substring of the
+ − 1035 data that is actually to be inserted. As a special case, if POS
+ − 1036 is -1, insert the string at point and move point to the end of the
+ − 1037 string.
+ − 1038
+ − 1039 Normally, markers at the insertion point end up before the
+ − 1040 inserted string. If INSDEL_BEFORE_MARKERS is set in flags, however,
+ − 1041 they end up after the string.
+ − 1042
+ − 1043 INSDEL_NO_LOCKING is kludgy and is used when insert-file-contents is
+ − 1044 visiting a new file; it inhibits the locking checks normally done
+ − 1045 before modifying a buffer. Similar checks were already done
+ − 1046 in the higher-level Lisp functions calling insert-file-contents. */
+ − 1047
+ − 1048 Charcount
665
+ − 1049 buffer_insert_string_1 (struct buffer *buf, Charbpos pos,
867
+ − 1050 const Ibyte *nonreloc, Lisp_Object reloc,
428
+ − 1051 Bytecount offset, Bytecount length,
+ − 1052 int flags)
+ − 1053 {
+ − 1054 /* This function can GC */
+ − 1055 struct gcpro gcpro1;
826
+ − 1056 Bytebpos bytepos;
+ − 1057 Bytecount length_in_buffer;
428
+ − 1058 Charcount cclen;
+ − 1059 int move_point = 0;
+ − 1060 struct buffer *mbuf;
+ − 1061 Lisp_Object bufcons;
+ − 1062
+ − 1063 /* Defensive steps just in case a buffer gets deleted and a calling
+ − 1064 function doesn't notice it. */
+ − 1065 if (!BUFFER_LIVE_P (buf))
+ − 1066 return 0;
+ − 1067
+ − 1068 fixup_internal_substring (nonreloc, reloc, offset, &length);
+ − 1069
+ − 1070 if (pos == -1)
+ − 1071 {
+ − 1072 pos = BUF_PT (buf);
+ − 1073 move_point = 1;
+ − 1074 }
+ − 1075
+ − 1076 #ifdef I18N3
+ − 1077 /* #### See the comment in print_internal(). If this buffer is marked
+ − 1078 as translatable, then Fgettext() should be called on obj if it
+ − 1079 is a string. */
+ − 1080 #endif
+ − 1081
+ − 1082 /* Make sure that point-max won't exceed the size of an emacs int. */
+ − 1083 if ((length + BUF_Z (buf)) > EMACS_INT_MAX)
563
+ − 1084 out_of_memory ("Maximum buffer size exceeded", Qunbound);
428
+ − 1085
+ − 1086 /* theoretically not necessary -- caller should GCPRO.
+ − 1087 #### buffer_insert_from_buffer_1() doesn't! */
+ − 1088 GCPRO1 (reloc);
+ − 1089
+ − 1090 prepare_to_modify_buffer (buf, pos, pos, !(flags & INSDEL_NO_LOCKING));
+ − 1091
+ − 1092 /* Defensive steps in case the before-change-functions fuck around */
+ − 1093 if (!BUFFER_LIVE_P (buf))
+ − 1094 {
+ − 1095 UNGCPRO;
+ − 1096 /* Bad bad pre-change function. */
+ − 1097 return 0;
+ − 1098 }
+ − 1099
+ − 1100 /* Make args be valid again. prepare_to_modify_buffer() might have
+ − 1101 modified the buffer. */
+ − 1102 if (pos < BUF_BEGV (buf))
+ − 1103 pos = BUF_BEGV (buf);
+ − 1104 if (pos > BUF_ZV (buf))
+ − 1105 pos = BUF_ZV (buf);
+ − 1106
826
+ − 1107 bytepos = charbpos_to_bytebpos (buf, pos);
771
+ − 1108
428
+ − 1109 /* string may have been relocated up to this point */
+ − 1110 if (STRINGP (reloc))
771
+ − 1111 {
793
+ − 1112 cclen = string_offset_byte_to_char_len (reloc, offset, length);
771
+ − 1113 nonreloc = XSTRING_DATA (reloc);
+ − 1114 }
+ − 1115 else
+ − 1116 cclen = bytecount_to_charcount (nonreloc + offset, length);
826
+ − 1117 /* &&#### Here we check if the text can't fit into the format of the buffer,
+ − 1118 and if so convert it to another format (either default or 32-bit-fixed,
+ − 1119 according to some flag; if no flag, use default). */
+ − 1120
+ − 1121 length_in_buffer = copy_text_between_formats (nonreloc + offset, length,
+ − 1122 FORMAT_DEFAULT,
+ − 1123 STRINGP (reloc) ? reloc : Qnil,
+ − 1124 NULL, 0,
+ − 1125 BUF_FORMAT (buf),
+ − 1126 wrap_buffer (buf),
+ − 1127 NULL);
428
+ − 1128
826
+ − 1129 if (bytepos != BYTE_BUF_GPT (buf))
428
+ − 1130 /* #### if debug-on-quit is invoked and the user changes the
+ − 1131 buffer, bad things can happen. This is a rampant problem
+ − 1132 in Emacs. */
826
+ − 1133 move_gap (buf, bytepos); /* may QUIT */
+ − 1134 if (! GAP_CAN_HOLD_SIZE_P (buf, length_in_buffer))
428
+ − 1135 {
826
+ − 1136 if (BUF_END_GAP_SIZE (buf) >= length_in_buffer)
428
+ − 1137 merge_gap_with_end_gap (buf);
+ − 1138 else
826
+ − 1139 make_gap (buf, length_in_buffer - BUF_GAP_SIZE (buf));
428
+ − 1140 }
+ − 1141
826
+ − 1142 /* At this point, no more QUITting or processing of Lisp code. Buffer is
+ − 1143 in a consistent state. Following code puts buffer in an inconsistent
+ − 1144 state and can be considered a "critical section". */
+ − 1145
428
+ − 1146 insert_invalidate_line_number_cache (buf, pos, nonreloc + offset, length);
+ − 1147
+ − 1148 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1149 {
+ − 1150 record_insert (mbuf, pos, cclen);
+ − 1151 }
+ − 1152
+ − 1153 BUF_MODIFF (buf)++;
+ − 1154 MARK_BUFFERS_CHANGED;
+ − 1155
826
+ − 1156 /* string may have been relocated up to this point #### if string is
+ − 1157 modified during quit processing, bad things can happen. */
428
+ − 1158 if (STRINGP (reloc))
+ − 1159 nonreloc = XSTRING_DATA (reloc);
+ − 1160
853
+ − 1161 memcpy (BUF_GPT_ADDR (buf), nonreloc + offset, length);
+ − 1162
826
+ − 1163 copy_text_between_formats (nonreloc + offset, length, FORMAT_DEFAULT,
+ − 1164 STRINGP (reloc) ? reloc : Qnil,
+ − 1165 BUF_GPT_ADDR (buf), length_in_buffer,
+ − 1166 BUF_FORMAT (buf), wrap_buffer (buf), NULL);
+ − 1167
+ − 1168 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) - length_in_buffer);
+ − 1169 SET_BYTE_BUF_GPT (buf, BYTE_BUF_GPT (buf) + length_in_buffer);
428
+ − 1170 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1171 {
826
+ − 1172 SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) + cclen,
+ − 1173 BYTE_BUF_ZV (mbuf) + length_in_buffer);
428
+ − 1174 }
826
+ − 1175 SET_BOTH_BUF_Z (buf, BUF_Z (buf) + cclen, BYTE_BUF_Z (buf) + length_in_buffer);
428
+ − 1176 SET_GAP_SENTINEL (buf);
771
+ − 1177
+ − 1178
428
+ − 1179 #ifdef MULE
826
+ − 1180 buffer_mule_signal_inserted_region (buf, pos, length_in_buffer, cclen);
+ − 1181 /* Update our count of ASCII, 8-bit and 16-bit chars and the
+ − 1182 entirely-one-byte flag */
+ − 1183 {
867
+ − 1184 const Ibyte *ptr = nonreloc + offset;
+ − 1185 const Ibyte *ptrend = ptr + length;
826
+ − 1186
+ − 1187 while (ptr < ptrend)
+ − 1188 {
867
+ − 1189 Ichar ch = itext_ichar (ptr);
+ − 1190 if (ichar_ascii_p (ch))
826
+ − 1191 buf->text->num_ascii_chars++;
867
+ − 1192 if (ichar_8_bit_fixed_p (ch, wrap_buffer (buf)))
826
+ − 1193 buf->text->num_8_bit_fixed_chars++;
867
+ − 1194 if (ichar_16_bit_fixed_p (ch, wrap_buffer (buf)))
826
+ − 1195 buf->text->num_16_bit_fixed_chars++;
867
+ − 1196 INC_IBYTEPTR (ptr);
826
+ − 1197 }
+ − 1198
+ − 1199 buf->text->entirely_one_byte_p =
+ − 1200 (BUF_FORMAT (buf) == FORMAT_8_BIT_FIXED ||
+ − 1201 (BUF_FORMAT (buf) == FORMAT_DEFAULT && BUF_Z (buf) == BYTE_BUF_Z (buf)));
+ − 1202 }
428
+ − 1203 #endif
+ − 1204
+ − 1205 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1206 {
826
+ − 1207 process_extents_for_insertion (wrap_buffer (mbuf), bytepos,
+ − 1208 length_in_buffer);
428
+ − 1209 }
+ − 1210
+ − 1211 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1212 {
826
+ − 1213 /* We know the gap is at BYTEPOS so the cast is OK. */
+ − 1214 adjust_markers_for_insert (mbuf, (Membpos) bytepos, length_in_buffer);
428
+ − 1215 }
+ − 1216
+ − 1217 /* Point logically doesn't move, but may need to be adjusted because
+ − 1218 it's a byte index. point-marker doesn't change because it's a
+ − 1219 memory index. */
+ − 1220 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1221 {
826
+ − 1222 if (BYTE_BUF_PT (mbuf) > bytepos)
428
+ − 1223 JUST_SET_POINT (mbuf, BUF_PT (mbuf) + cclen,
826
+ − 1224 BYTE_BUF_PT (mbuf) + length_in_buffer);
428
+ − 1225 }
+ − 1226
+ − 1227 /* Well, point might move. */
+ − 1228 if (move_point)
826
+ − 1229 BYTE_BUF_SET_PT (buf, bytepos + length_in_buffer);
428
+ − 1230
+ − 1231 if (STRINGP (reloc))
+ − 1232 {
+ − 1233 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1234 {
826
+ − 1235 splice_in_string_extents (reloc, mbuf, bytepos, length, offset);
428
+ − 1236 }
+ − 1237 }
+ − 1238
+ − 1239 if (flags & INSDEL_BEFORE_MARKERS)
+ − 1240 {
+ − 1241 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1242 {
826
+ − 1243 /* bytepos - 1 is correct because the FROM argument is exclusive.
665
+ − 1244 I formerly used DEC_BYTEBPOS() but that caused problems at the
428
+ − 1245 beginning of the buffer. */
826
+ − 1246 adjust_markers (mbuf, bytepos - 1, bytepos, length_in_buffer);
428
+ − 1247 }
+ − 1248 }
+ − 1249
826
+ − 1250 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1251 {
+ − 1252 signal_syntax_table_extent_adjust (mbuf);
+ − 1253 }
+ − 1254
428
+ − 1255 signal_after_change (buf, pos, pos, pos + cclen);
+ − 1256
+ − 1257 UNGCPRO;
+ − 1258
+ − 1259 return cclen;
+ − 1260 }
+ − 1261
+ − 1262
+ − 1263 /* The following functions are interfaces onto the above function,
+ − 1264 for inserting particular sorts of data. In all the functions,
+ − 1265 BUF and POS specify the buffer and location where the insertion is
+ − 1266 to take place. (If POS is -1, text is inserted at point and point
+ − 1267 moves forward past the text.) FLAGS is as above. */
+ − 1268
+ − 1269 Charcount
665
+ − 1270 buffer_insert_raw_string_1 (struct buffer *buf, Charbpos pos,
867
+ − 1271 const Ibyte *nonreloc, Bytecount length,
428
+ − 1272 int flags)
+ − 1273 {
+ − 1274 /* This function can GC */
+ − 1275 return buffer_insert_string_1 (buf, pos, nonreloc, Qnil, 0, length,
+ − 1276 flags);
+ − 1277 }
+ − 1278
+ − 1279 Charcount
665
+ − 1280 buffer_insert_lisp_string_1 (struct buffer *buf, Charbpos pos, Lisp_Object str,
428
+ − 1281 int flags)
+ − 1282 {
+ − 1283 /* This function can GC */
+ − 1284 return buffer_insert_string_1 (buf, pos, 0, str, 0,
+ − 1285 XSTRING_LENGTH (str),
+ − 1286 flags);
+ − 1287 }
+ − 1288
+ − 1289 /* Insert the null-terminated string S (in external format). */
+ − 1290
+ − 1291 Charcount
665
+ − 1292 buffer_insert_c_string_1 (struct buffer *buf, Charbpos pos, const char *s,
428
+ − 1293 int flags)
+ − 1294 {
+ − 1295 /* This function can GC */
442
+ − 1296 const char *translated = GETTEXT (s);
867
+ − 1297 return buffer_insert_string_1 (buf, pos, (const Ibyte *) translated, Qnil,
428
+ − 1298 0, strlen (translated), flags);
+ − 1299 }
+ − 1300
+ − 1301 Charcount
867
+ − 1302 buffer_insert_emacs_char_1 (struct buffer *buf, Charbpos pos, Ichar ch,
428
+ − 1303 int flags)
+ − 1304 {
+ − 1305 /* This function can GC */
867
+ − 1306 Ibyte str[MAX_ICHAR_LEN];
+ − 1307 Bytecount len = set_itext_ichar (str, ch);
428
+ − 1308 return buffer_insert_string_1 (buf, pos, str, Qnil, 0, len, flags);
+ − 1309 }
+ − 1310
+ − 1311 Charcount
665
+ − 1312 buffer_insert_c_char_1 (struct buffer *buf, Charbpos pos, char c,
428
+ − 1313 int flags)
+ − 1314 {
+ − 1315 /* This function can GC */
867
+ − 1316 return buffer_insert_emacs_char_1 (buf, pos, (Ichar) (unsigned char) c,
428
+ − 1317 flags);
+ − 1318 }
+ − 1319
+ − 1320 Charcount
665
+ − 1321 buffer_insert_from_buffer_1 (struct buffer *buf, Charbpos pos,
+ − 1322 struct buffer *buf2, Charbpos pos2,
428
+ − 1323 Charcount length, int flags)
+ − 1324 {
+ − 1325 /* This function can GC */
+ − 1326 Lisp_Object str = make_string_from_buffer (buf2, pos2, length);
+ − 1327 return buffer_insert_string_1 (buf, pos, 0, str, 0,
+ − 1328 XSTRING_LENGTH (str), flags);
+ − 1329 }
+ − 1330
+ − 1331
+ − 1332 /************************************************************************/
+ − 1333 /* Deletion of ranges */
+ − 1334 /************************************************************************/
+ − 1335
+ − 1336 /* Delete characters in buffer from FROM up to (but not including) TO. */
+ − 1337
+ − 1338 void
665
+ − 1339 buffer_delete_range (struct buffer *buf, Charbpos from, Charbpos to, int flags)
428
+ − 1340 {
+ − 1341 /* This function can GC */
+ − 1342 Charcount numdel;
826
+ − 1343 Bytebpos byte_from, byte_to;
+ − 1344 Bytecount byte_numdel;
428
+ − 1345 EMACS_INT shortage;
+ − 1346 struct buffer *mbuf;
+ − 1347 Lisp_Object bufcons;
826
+ − 1348 int do_move_gap = 0;
428
+ − 1349
+ − 1350 /* Defensive steps just in case a buffer gets deleted and a calling
+ − 1351 function doesn't notice it. */
+ − 1352 if (!BUFFER_LIVE_P (buf))
+ − 1353 return;
+ − 1354
+ − 1355 /* Make args be valid */
+ − 1356 if (from < BUF_BEGV (buf))
+ − 1357 from = BUF_BEGV (buf);
+ − 1358 if (to > BUF_ZV (buf))
+ − 1359 to = BUF_ZV (buf);
+ − 1360 if ((numdel = to - from) <= 0)
+ − 1361 return;
+ − 1362
+ − 1363 prepare_to_modify_buffer (buf, from, to, !(flags & INSDEL_NO_LOCKING));
+ − 1364
+ − 1365 /* Defensive steps in case the before-change-functions fuck around */
+ − 1366 if (!BUFFER_LIVE_P (buf))
+ − 1367 /* Bad bad pre-change function. */
+ − 1368 return;
+ − 1369
+ − 1370 /* Make args be valid again. prepare_to_modify_buffer() might have
+ − 1371 modified the buffer. */
+ − 1372 if (from < BUF_BEGV (buf))
+ − 1373 from = BUF_BEGV (buf);
+ − 1374 if (to > BUF_ZV (buf))
+ − 1375 to = BUF_ZV (buf);
+ − 1376 if ((numdel = to - from) <= 0)
+ − 1377 return;
+ − 1378
826
+ − 1379 byte_from = charbpos_to_bytebpos (buf, from);
+ − 1380 byte_to = charbpos_to_bytebpos (buf, to);
+ − 1381 byte_numdel = byte_to - byte_from;
+ − 1382
+ − 1383 if (to == BUF_Z (buf) &&
+ − 1384 byte_from > BYTE_BUF_GPT (buf))
+ − 1385 /* avoid moving the gap just to delete from the bottom. */
+ − 1386 do_move_gap = 0;
+ − 1387 else
+ − 1388 {
+ − 1389 /* Make sure the gap is somewhere in or next to what we are deleting. */
+ − 1390 /* NOTE: Can QUIT! */
+ − 1391 if (byte_to < BYTE_BUF_GPT (buf))
+ − 1392 gap_left (buf, byte_to);
+ − 1393 if (byte_from > BYTE_BUF_GPT (buf))
+ − 1394 gap_right (buf, byte_from);
+ − 1395 do_move_gap = 1;
+ − 1396 }
+ − 1397
+ − 1398 /* At this point, no more QUITting or processing of Lisp code. Buffer is
+ − 1399 in a consistent state. Following code puts buffer in an inconsistent
+ − 1400 state and can be considered a "critical section". */
+ − 1401
+ − 1402 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1403 {
+ − 1404 record_delete (mbuf, from, numdel);
+ − 1405 }
+ − 1406 BUF_MODIFF (buf)++;
+ − 1407 MARK_BUFFERS_CHANGED;
+ − 1408
+ − 1409 /* We used to do the following before the gap move. But that might QUIT,
+ − 1410 and (as a result of this) the gap code always leaves the buffer in
+ − 1411 a consistent state. Therefore, it's totally safe to do these operations
+ − 1412 now, and just as well not before, as we're making state changes
+ − 1413 related to the deletion. */
+ − 1414
428
+ − 1415 /* Redisplay needs to know if a newline was in the deleted region.
+ − 1416 If we've already marked the changed region as having a deleted
+ − 1417 newline there is no use in performing the check. */
+ − 1418 if (!buf->changes->newline_was_deleted)
+ − 1419 {
+ − 1420 scan_buffer (buf, '\n', from, to, 1, &shortage, 1);
+ − 1421 if (!shortage)
+ − 1422 {
+ − 1423 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1424 {
+ − 1425 mbuf->changes->newline_was_deleted = 1;
+ − 1426 }
+ − 1427 }
+ − 1428 }
+ − 1429
+ − 1430 delete_invalidate_line_number_cache (buf, from, to);
+ − 1431
826
+ − 1432 #ifdef MULE
+ − 1433 /* Update our count of ASCII, 8-bit and 16-bit chars and the
+ − 1434 entirely-one-byte flag */
+ − 1435 {
+ − 1436 Bytebpos i;
428
+ − 1437
826
+ − 1438 for (i = byte_from; i < byte_to; i = next_bytebpos (buf, i))
+ − 1439 {
867
+ − 1440 Ichar ch = BYTE_BUF_FETCH_CHAR (buf, i);
+ − 1441 if (ichar_ascii_p (ch))
826
+ − 1442 buf->text->num_ascii_chars--;
867
+ − 1443 if (ichar_8_bit_fixed_p (ch, wrap_buffer (buf)))
826
+ − 1444 buf->text->num_8_bit_fixed_chars--;
867
+ − 1445 if (ichar_16_bit_fixed_p (ch, wrap_buffer (buf)))
826
+ − 1446 buf->text->num_16_bit_fixed_chars--;
+ − 1447 }
+ − 1448 }
+ − 1449 #endif /* MULE */
428
+ − 1450
826
+ − 1451 /* #### Point used to be modified here, but this causes problems
+ − 1452 with MULE, as point is used to calculate bytebpos's, and if the
+ − 1453 offset in byte_numdel causes point to move to a non first-byte
+ − 1454 location, causing some other function to throw an assertion
+ − 1455 in ASSERT_VALID_BYTEBPOS. I've moved the code to right after
+ − 1456 the other movements and adjustments, but before the gap is
+ − 1457 moved. -- jh 970813 */
428
+ − 1458
826
+ − 1459 /* Detach any extents that are completely within the range [FROM, TO],
+ − 1460 if the extents are detachable.
+ − 1461
+ − 1462 This must come AFTER record_delete(), so that the appropriate extents
+ − 1463 will be present to be recorded, and BEFORE the gap size is increased,
+ − 1464 as otherwise we will be confused about where the extents end. */
+ − 1465 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1466 {
+ − 1467 process_extents_for_deletion (wrap_buffer (mbuf), byte_from, byte_to, 0);
428
+ − 1468 }
+ − 1469
826
+ − 1470 /* Relocate all markers pointing into the new, larger gap to
+ − 1471 point at the end of the text before the gap. */
+ − 1472 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1473 {
+ − 1474 adjust_markers (mbuf,
+ − 1475 (byte_to + BUF_GAP_SIZE (mbuf)),
+ − 1476 (byte_to + BUF_GAP_SIZE (mbuf)),
+ − 1477 (- byte_numdel -
+ − 1478 (do_move_gap ? BUF_GAP_SIZE (mbuf) : 0)));
+ − 1479 }
+ − 1480
+ − 1481 /* Relocate any extent endpoints just like markers. */
+ − 1482 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1483 {
+ − 1484 adjust_extents_for_deletion (wrap_buffer (mbuf), byte_from, byte_to,
+ − 1485 BUF_GAP_SIZE (mbuf),
+ − 1486 byte_numdel,
+ − 1487 do_move_gap ? BUF_GAP_SIZE (mbuf) : 0);
+ − 1488 }
+ − 1489
+ − 1490 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1491 {
+ − 1492 /* Relocate point as if it were a marker. */
+ − 1493 if (byte_from < BYTE_BUF_PT (mbuf))
+ − 1494 {
+ − 1495 if (BYTE_BUF_PT (mbuf) < byte_to)
+ − 1496 JUST_SET_POINT (mbuf, from, byte_from);
+ − 1497 else
+ − 1498 JUST_SET_POINT (mbuf, BUF_PT (mbuf) - numdel,
+ − 1499 BYTE_BUF_PT (mbuf) - byte_numdel);
+ − 1500 }
+ − 1501 }
+ − 1502
+ − 1503 if (do_move_gap)
+ − 1504 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + byte_numdel);
+ − 1505 else
+ − 1506 SET_BUF_END_GAP_SIZE (buf, BUF_END_GAP_SIZE (buf) + byte_numdel);
+ − 1507 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1508 {
+ − 1509 SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) - numdel,
+ − 1510 BYTE_BUF_ZV (mbuf) - byte_numdel);
+ − 1511 }
+ − 1512 SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BYTE_BUF_Z (buf) - byte_numdel);
+ − 1513 if (do_move_gap)
+ − 1514 SET_BYTE_BUF_GPT (buf, byte_from);
+ − 1515 SET_GAP_SENTINEL (buf);
+ − 1516
428
+ − 1517 #ifdef MULE
826
+ − 1518 buffer_mule_signal_deleted_region (buf, from, to, byte_from, byte_to);
+ − 1519 buf->text->entirely_one_byte_p =
+ − 1520 (BUF_FORMAT (buf) == FORMAT_8_BIT_FIXED ||
+ − 1521 (BUF_FORMAT (buf) == FORMAT_DEFAULT && BUF_Z (buf) == BYTE_BUF_Z (buf)));
428
+ − 1522 #endif
+ − 1523
+ − 1524 #ifdef ERROR_CHECK_EXTENTS
+ − 1525 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1526 {
771
+ − 1527 sledgehammer_extent_check (wrap_buffer (mbuf));
428
+ − 1528 }
+ − 1529 #endif
+ − 1530
826
+ − 1531 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1532 {
+ − 1533 signal_syntax_table_extent_adjust (mbuf);
+ − 1534 }
+ − 1535
+ − 1536 /* &&#### Here we consider converting the buffer from default to
+ − 1537 8-bit-fixed if is entirely 8-bit-fixed chars and has been that way for
+ − 1538 a long time, e.g. 20 minutes. And if the buffer just switched to all
+ − 1539 8-bit-fixed chars, start the timer. */
428
+ − 1540 signal_after_change (buf, from, to, from);
+ − 1541 }
+ − 1542
+ − 1543
+ − 1544 /************************************************************************/
+ − 1545 /* Replacement of characters */
+ − 1546 /************************************************************************/
+ − 1547
+ − 1548 /* Replace the character at POS in buffer B with CH. */
+ − 1549
+ − 1550 void
867
+ − 1551 buffer_replace_char (struct buffer *buf, Charbpos pos, Ichar ch,
428
+ − 1552 int not_real_change, int force_lock_check)
+ − 1553 {
+ − 1554 /* This function can GC */
867
+ − 1555 Ibyte newstr[MAX_ICHAR_LEN];
826
+ − 1556 Bytecount newlen;
867
+ − 1557 Ichar oldch;
428
+ − 1558
+ − 1559 /* Defensive steps just in case a buffer gets deleted and a calling
+ − 1560 function doesn't notice it. */
+ − 1561 if (!BUFFER_LIVE_P (buf))
+ − 1562 return;
+ − 1563
867
+ − 1564 newlen = set_itext_ichar_fmt (newstr, ch, BUF_FORMAT (buf),
826
+ − 1565 wrap_buffer (buf));
+ − 1566 oldch = BUF_FETCH_CHAR (buf, pos);
867
+ − 1567 if (ichar_fits_in_format (ch, BUF_FORMAT (buf), wrap_buffer (buf)) &&
+ − 1568 newlen == ichar_len_fmt (oldch, BUF_FORMAT (buf)))
428
+ − 1569 {
+ − 1570 struct buffer *mbuf;
+ − 1571 Lisp_Object bufcons;
+ − 1572
+ − 1573 /* then we can just replace the text. */
+ − 1574 prepare_to_modify_buffer (buf, pos, pos + 1,
+ − 1575 !not_real_change || force_lock_check);
+ − 1576 /* Defensive steps in case the before-change-functions fuck around */
+ − 1577 if (!BUFFER_LIVE_P (buf))
+ − 1578 /* Bad bad pre-change function. */
+ − 1579 return;
+ − 1580
+ − 1581 /* Make args be valid again. prepare_to_modify_buffer() might have
+ − 1582 modified the buffer. */
+ − 1583 if (pos < BUF_BEGV (buf))
+ − 1584 pos = BUF_BEGV (buf);
+ − 1585 if (pos >= BUF_ZV (buf))
+ − 1586 pos = BUF_ZV (buf) - 1;
+ − 1587 if (pos < BUF_BEGV (buf))
+ − 1588 /* no more characters in buffer! */
+ − 1589 return;
+ − 1590
+ − 1591 if (BUF_FETCH_CHAR (buf, pos) == '\n')
+ − 1592 {
+ − 1593 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1594 {
+ − 1595 mbuf->changes->newline_was_deleted = 1;
+ − 1596 }
+ − 1597 }
+ − 1598 MARK_BUFFERS_CHANGED;
+ − 1599 if (!not_real_change)
+ − 1600 {
+ − 1601 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
+ − 1602 {
+ − 1603 record_change (mbuf, pos, 1);
+ − 1604 }
+ − 1605 BUF_MODIFF (buf)++;
+ − 1606 }
826
+ − 1607
+ − 1608 #ifdef MULE
867
+ − 1609 if (ichar_ascii_p (oldch))
826
+ − 1610 buf->text->num_ascii_chars--;
867
+ − 1611 if (ichar_8_bit_fixed_p (oldch, wrap_buffer (buf)))
826
+ − 1612 buf->text->num_8_bit_fixed_chars--;
867
+ − 1613 if (ichar_16_bit_fixed_p (oldch, wrap_buffer (buf)))
826
+ − 1614 buf->text->num_16_bit_fixed_chars--;
867
+ − 1615 if (ichar_ascii_p (ch))
826
+ − 1616 buf->text->num_ascii_chars++;
867
+ − 1617 if (ichar_8_bit_fixed_p (ch, wrap_buffer (buf)))
826
+ − 1618 buf->text->num_8_bit_fixed_chars++;
867
+ − 1619 if (ichar_16_bit_fixed_p (ch, wrap_buffer (buf)))
826
+ − 1620 buf->text->num_16_bit_fixed_chars++;
+ − 1621 #endif /* MULE */
+ − 1622
428
+ − 1623 memcpy (BUF_BYTE_ADDRESS (buf, pos), newstr, newlen);
+ − 1624
+ − 1625 signal_after_change (buf, pos, pos + 1, pos + 1);
+ − 1626
+ − 1627 /* We do not have to adjust the Mule data; we just replaced a
+ − 1628 character with another of the same number of bytes. */
+ − 1629 }
+ − 1630 else
+ − 1631 {
+ − 1632 /*
+ − 1633 * Must implement as deletion followed by insertion.
+ − 1634 *
+ − 1635 * Make a note to move point forward later in the one situation
+ − 1636 * where it is needed, a delete/insert one position behind
+ − 1637 * point. Point will drift backward by one position and stay
+ − 1638 * there otherwise.
+ − 1639 */
+ − 1640 int movepoint = (pos == BUF_PT (buf) - 1);
+ − 1641
+ − 1642 buffer_delete_range (buf, pos, pos + 1, 0);
+ − 1643 /* Defensive steps in case the before-change-functions fuck around */
+ − 1644 if (!BUFFER_LIVE_P (buf))
+ − 1645 /* Bad bad pre-change function. */
+ − 1646 return;
+ − 1647
+ − 1648 /* Make args be valid again. prepare_to_modify_buffer() might have
+ − 1649 modified the buffer. */
+ − 1650 if (pos < BUF_BEGV (buf))
+ − 1651 pos = BUF_BEGV (buf);
+ − 1652 if (pos >= BUF_ZV (buf))
+ − 1653 pos = BUF_ZV (buf) - 1;
+ − 1654 if (pos < BUF_BEGV (buf))
+ − 1655 /* no more characters in buffer! */
+ − 1656 return;
+ − 1657 /*
+ − 1658 * -1 as the pos argument means to move point forward with the
+ − 1659 * insertion, which we must do if the deletion moved point
+ − 1660 * backward so that it now equals the insertion point.
+ − 1661 */
+ − 1662 buffer_insert_string_1 (buf, (movepoint ? -1 : pos),
+ − 1663 newstr, Qnil, 0, newlen, 0);
+ − 1664 }
+ − 1665 }
+ − 1666
+ − 1667
+ − 1668 /************************************************************************/
+ − 1669 /* Other functions */
+ − 1670 /************************************************************************/
+ − 1671
+ − 1672 /* Make a string from a buffer. This needs to take into account the gap,
+ − 1673 and add any necessary extents from the buffer. */
+ − 1674
+ − 1675 static Lisp_Object
665
+ − 1676 make_string_from_buffer_1 (struct buffer *buf, Charbpos pos, Charcount length,
428
+ − 1677 int no_extents)
+ − 1678 {
+ − 1679 /* This function can GC */
826
+ − 1680 Bytebpos bytepos = charbpos_to_bytebpos (buf, pos);
+ − 1681 Bytecount bytelen = charbpos_to_bytebpos (buf, pos + length) - bytepos;
+ − 1682 Bytecount needed = copy_buffer_text_out (buf, bytepos, bytelen, NULL, 0,
+ − 1683 FORMAT_DEFAULT, Qnil, NULL);
+ − 1684 Lisp_Object val = make_uninit_string (needed);
428
+ − 1685
+ − 1686 struct gcpro gcpro1;
+ − 1687 GCPRO1 (val);
+ − 1688
+ − 1689 if (!no_extents)
826
+ − 1690 add_string_extents (val, buf, bytepos, bytelen);
+ − 1691 copy_buffer_text_out (buf, bytepos, bytelen, XSTRING_DATA (val), needed,
+ − 1692 FORMAT_DEFAULT, Qnil, NULL);
771
+ − 1693 init_string_ascii_begin (val);
+ − 1694 sledgehammer_check_ascii_begin (val);
+ − 1695
428
+ − 1696 UNGCPRO;
+ − 1697 return val;
+ − 1698 }
+ − 1699
+ − 1700 Lisp_Object
665
+ − 1701 make_string_from_buffer (struct buffer *buf, Charbpos pos, Charcount length)
428
+ − 1702 {
+ − 1703 return make_string_from_buffer_1 (buf, pos, length, 0);
+ − 1704 }
+ − 1705
+ − 1706 Lisp_Object
665
+ − 1707 make_string_from_buffer_no_extents (struct buffer *buf, Charbpos pos,
428
+ − 1708 Charcount length)
+ − 1709 {
+ − 1710 return make_string_from_buffer_1 (buf, pos, length, 1);
+ − 1711 }
+ − 1712
+ − 1713 void
665
+ − 1714 barf_if_buffer_read_only (struct buffer *buf, Charbpos from, Charbpos to)
428
+ − 1715 {
+ − 1716 Lisp_Object buffer;
+ − 1717 Lisp_Object iro;
+ − 1718
793
+ − 1719 buffer = wrap_buffer (buf);
428
+ − 1720 back:
+ − 1721 iro = (buf == current_buffer ? Vinhibit_read_only :
+ − 1722 symbol_value_in_buffer (Qinhibit_read_only, buffer));
+ − 1723 if (!LISTP (iro))
+ − 1724 return;
+ − 1725 if (NILP (iro) && !NILP (buf->read_only))
+ − 1726 {
+ − 1727 Fsignal (Qbuffer_read_only, (list1 (buffer)));
+ − 1728 goto back;
+ − 1729 }
+ − 1730 if (from > 0)
+ − 1731 {
+ − 1732 if (to < 0)
+ − 1733 to = from;
+ − 1734 verify_extent_modification (buffer,
665
+ − 1735 charbpos_to_bytebpos (buf, from),
+ − 1736 charbpos_to_bytebpos (buf, to),
428
+ − 1737 iro);
+ − 1738 }
+ − 1739 }
+ − 1740
+ − 1741
+ − 1742 /************************************************************************/
+ − 1743 /* initialization */
+ − 1744 /************************************************************************/
+ − 1745
+ − 1746 void
+ − 1747 reinit_vars_of_insdel (void)
+ − 1748 {
+ − 1749 inside_change_hook = 0;
+ − 1750 in_first_change = 0;
+ − 1751 }
+ − 1752
+ − 1753 void
+ − 1754 vars_of_insdel (void)
+ − 1755 {
+ − 1756 reinit_vars_of_insdel ();
+ − 1757 }
+ − 1758
+ − 1759 void
+ − 1760 init_buffer_text (struct buffer *b)
+ − 1761 {
+ − 1762 if (!b->base_buffer)
+ − 1763 {
+ − 1764 SET_BUF_GAP_SIZE (b, 20);
+ − 1765 BUFFER_ALLOC (b->text->beg, BUF_GAP_SIZE (b) + BUF_END_SENTINEL_SIZE);
+ − 1766 if (! BUF_BEG_ADDR (b))
+ − 1767 memory_full ();
+ − 1768
+ − 1769 SET_BUF_END_GAP_SIZE (b, 0);
826
+ − 1770 SET_BYTE_BUF_GPT (b, 1);
428
+ − 1771 SET_BOTH_BUF_Z (b, 1, 1);
+ − 1772 SET_GAP_SENTINEL (b);
+ − 1773 SET_END_SENTINEL (b);
+ − 1774 #ifdef MULE
+ − 1775 {
+ − 1776 int i;
+ − 1777
+ − 1778 b->text->mule_bufmin = b->text->mule_bufmax = 1;
+ − 1779 b->text->mule_bytmin = b->text->mule_bytmax = 1;
826
+ − 1780 b->text->entirely_one_byte_p = 1;
428
+ − 1781
+ − 1782 for (i = 0; i < 16; i++)
+ − 1783 {
665
+ − 1784 b->text->mule_charbpos_cache[i] = 1;
+ − 1785 b->text->mule_bytebpos_cache[i] = 1;
428
+ − 1786 }
+ − 1787 }
826
+ − 1788 /* &&#### Set to FORMAT_8_BIT_FIXED when that code is working */
+ − 1789 BUF_FORMAT (b) = FORMAT_DEFAULT;
428
+ − 1790 #endif /* MULE */
+ − 1791 b->text->line_number_cache = Qnil;
+ − 1792
+ − 1793 BUF_MODIFF (b) = 1;
+ − 1794 BUF_SAVE_MODIFF (b) = 1;
+ − 1795
+ − 1796 JUST_SET_POINT (b, 1, 1);
+ − 1797 SET_BOTH_BUF_BEGV (b, 1, 1);
+ − 1798 SET_BOTH_BUF_ZV (b, 1, 1);
+ − 1799
+ − 1800 b->text->changes = xnew_and_zero (struct buffer_text_change_data);
+ − 1801 }
+ − 1802 else
+ − 1803 {
826
+ − 1804 JUST_SET_POINT (b, BUF_PT (b->base_buffer), BYTE_BUF_PT (b->base_buffer));
428
+ − 1805 SET_BOTH_BUF_BEGV (b, BUF_BEGV (b->base_buffer),
826
+ − 1806 BYTE_BUF_BEGV (b->base_buffer));
428
+ − 1807 SET_BOTH_BUF_ZV (b, BUF_ZV (b->base_buffer),
826
+ − 1808 BYTE_BUF_ZV (b->base_buffer));
428
+ − 1809 }
+ − 1810
+ − 1811 b->changes = xnew_and_zero (struct each_buffer_change_data);
+ − 1812 BUF_FACECHANGE (b) = 1;
+ − 1813
+ − 1814 #ifdef REGION_CACHE_NEEDS_WORK
+ − 1815 b->newline_cache = 0;
+ − 1816 b->width_run_cache = 0;
+ − 1817 b->width_table = Qnil;
+ − 1818 #endif
+ − 1819 }
+ − 1820
+ − 1821 void
+ − 1822 uninit_buffer_text (struct buffer *b)
+ − 1823 {
+ − 1824 if (!b->base_buffer)
+ − 1825 {
+ − 1826 BUFFER_FREE (b->text->beg);
+ − 1827 xfree (b->text->changes);
+ − 1828 }
+ − 1829 xfree (b->changes);
+ − 1830
+ − 1831 #ifdef REGION_CACHE_NEEDS_WORK
+ − 1832 if (b->newline_cache)
+ − 1833 {
+ − 1834 free_region_cache (b->newline_cache);
+ − 1835 b->newline_cache = 0;
+ − 1836 }
+ − 1837 if (b->width_run_cache)
+ − 1838 {
+ − 1839 free_region_cache (b->width_run_cache);
+ − 1840 b->width_run_cache = 0;
+ − 1841 }
+ − 1842 b->width_table = Qnil;
+ − 1843 #endif
+ − 1844 }