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.
6 This file is part of XEmacs.
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Synched up with: Mule 2.0, FSF 19.30. Diverges significantly. */
25 /* This file has been Mule-ized. */
27 /* Overhauled by Ben Wing, December 1994, for Mule implementation. */
30 There are three possible ways to specify positions in a buffer. All
31 of these are one-based: the beginning of the buffer is position or
32 index 1, and 0 is not a valid position.
34 As a "buffer position" (typedef Bufpos):
36 This is an index specifying an offset in characters from the
37 beginning of the buffer. Note that buffer positions are
38 logically *between* characters, not on a character. The
39 difference between two buffer positions specifies the number of
40 characters between those positions. Buffer positions are the
41 only kind of position externally visible to the user.
43 As a "byte index" (typedef Bytind):
45 This is an index over the bytes used to represent the characters
46 in the buffer. If there is no Mule support, this is identical
47 to a buffer position, because each character is represented
48 using one byte. However, with Mule support, many characters
49 require two or more bytes for their representation, and so a
50 byte index may be greater than the corresponding buffer
53 As a "memory index" (typedef Memind):
55 This is the byte index adjusted for the gap. For positions
56 before the gap, this is identical to the byte index. For
57 positions after the gap, this is the byte index plus the gap
58 size. There are two possible memory indices for the gap
59 position; the memory index at the beginning of the gap should
60 always be used, except in code that deals with manipulating the
61 gap, where both indices may be seen. The address of the
62 character "at" (i.e. following) a particular position can be
63 obtained from the formula
65 buffer_start_address + memory_index(position) - 1
67 except in the case of characters at the gap position.
74 This typedef represents a single Emacs character, which can be
75 ASCII, ISO-8859, or some extended character, as would typically
76 be used for Kanji. Note that the representation of a character
77 as an Emchar is *not* the same as the representation of that
78 same character in a string; thus, you cannot do the standard
79 C trick of passing a pointer to a character to a function that
82 An Emchar takes up 19 bits of representation and (for code
83 compatibility and such) is compatible with an int. This
84 representation is visible on the Lisp level. The important
85 characteristics of the Emchar representation are
87 -- values 0x00 - 0x7f represent ASCII.
88 -- values 0x80 - 0xff represent the right half of ISO-8859-1.
89 -- values 0x100 and up represent all other characters.
91 This means that Emchar values are upwardly compatible with
92 the standard 8-bit representation of ASCII/ISO-8859-1.
96 The data in a buffer or string is logically made up of Bufbyte
97 objects, where a Bufbyte takes up the same amount of space as a
98 char. (It is declared differently, though, to catch invalid
99 usages.) Strings stored using Bufbytes are said to be in
100 "internal format". The important characteristics of internal
103 -- ASCII characters are represented as a single Bufbyte,
104 in the range 0 - 0x7f.
105 -- All other characters are represented as a Bufbyte in
106 the range 0x80 - 0x9f followed by one or more Bufbytes
107 in the range 0xa0 to 0xff.
109 This leads to a number of desirable properties:
111 -- Given the position of the beginning of a character,
112 you can find the beginning of the next or previous
113 character in constant time.
114 -- When searching for a substring or an ASCII character
115 within the string, you need merely use standard
120 Strings that go in or out of Emacs are in "external format",
121 typedef'ed as an array of char or a char *. There is more
122 than one external format (JIS, EUC, etc.) but they all
123 have similar properties. They are modal encodings,
124 which is to say that the meaning of particular bytes is
125 not fixed but depends on what "mode" the string is currently
126 in (e.g. bytes in the range 0 - 0x7f might be
127 interpreted as ASCII, or as Hiragana, or as 2-byte Kanji,
128 depending on the current mode). The mode starts out in
129 ASCII/ISO-8859-1 and is switched using escape sequences --
130 for example, in the JIS encoding, 'ESC $ B' switches to a
131 mode where pairs of bytes in the range 0 - 0x7f
132 are interpreted as Kanji characters.
134 External-formatted data is generally desirable for passing
135 data between programs because it is upwardly compatible
136 with standard ASCII/ISO-8859-1 strings and may require
137 less space than internal encodings such as the one
138 described above. In addition, some encodings (e.g. JIS)
139 keep all characters (except the ESC used to switch modes)
140 in the printing ASCII range 0x20 - 0x7e, which results in
141 a much higher probability that the data will avoid being
142 garbled in transmission. Externally-formatted data is
143 generally not very convenient to work with, however, and
144 for this reason is usually converted to internal format
145 before any work is done on the string.
147 NOTE: filenames need to be in external format so that
148 ISO-8859-1 characters come out correctly.
152 This typedef represents a count of characters, such as
153 a character offset into a string or the number of
154 characters between two positions in a buffer. The
155 difference between two Bufpos's is a Charcount, and
156 character positions in a string are represented using
161 Similar to a Charcount but represents a count of bytes.
162 The difference between two Bytind's is a Bytecount.
165 Usage of the various representations:
166 =====================================
168 Memory indices are used in low-level functions in insdel.c and for
169 extent endpoints and marker positions. The reason for this is that
170 this way, the extents and markers don't need to be updated for most
171 insertions, which merely shrink the gap and don't move any
172 characters around in memory.
174 (The beginning-of-gap memory index simplifies insertions w.r.t.
175 markers, because text usually gets inserted after markers. For
176 extents, it is merely for consistency, because text can get
177 inserted either before or after an extent's endpoint depending on
178 the open/closedness of the endpoint.)
180 Byte indices are used in other code that needs to be fast,
181 such as the searching, redisplay, and extent-manipulation code.
183 Buffer positions are used in all other code. This is because this
184 representation is easiest to work with (especially since Lisp
185 code always uses buffer positions), necessitates the fewest
186 changes to existing code, and is the safest (e.g. if the text gets
187 shifted underneath a buffer position, it will still point to a
188 character; if text is shifted under a byte index, it might point
189 to the middle of a character, which would be bad).
191 Similarly, Charcounts are used in all code that deals with strings
192 except for code that needs to be fast, which used Bytecounts.
194 Strings are always passed around internally using internal format.
195 Conversions between external format are performed at the time
196 that the data goes in or out of Emacs.
198 Working with the various representations:
199 ========================================= */
211 #include "redisplay.h"
212 #include "line-number.h"
214 /* We write things this way because it's very important the
215 MAX_BYTIND_GAP_SIZE_3 is a multiple of 3. (As it happens,
216 65535 is a multiple of 3, but this may not always be the
219 #define MAX_BUFPOS_GAP_SIZE_3 (65535/3)
220 #define MAX_BYTIND_GAP_SIZE_3 (3 * MAX_BUFPOS_GAP_SIZE_3)
222 short three_to_one_table[1 + MAX_BYTIND_GAP_SIZE_3];
224 /* Various macros modelled along the lines of those in buffer.h.
225 Purposefully omitted from buffer.h because files other than this
226 one should not be using them. */
228 /* Address of beginning of buffer. This is an lvalue because
229 BUFFER_ALLOC needs it to be. */
230 #define BUF_BEG_ADDR(buf) ((buf)->text->beg)
232 /* Set the address of beginning of buffer. */
233 #define SET_BUF_BEG_ADDR(buf, addr) do { (buf)->text->beg = (addr); } while (0)
236 #define BUF_GAP_SIZE(buf) ((buf)->text->gap_size + 0)
237 #define BUF_END_GAP_SIZE(buf) ((buf)->text->end_gap_size + 0)
239 #define SET_BUF_GAP_SIZE(buf, value) \
240 do { (buf)->text->gap_size = (value); } while (0)
241 #define SET_BUF_END_GAP_SIZE(buf, value) \
242 do { (buf)->text->end_gap_size = (value); } while (0)
245 #define BI_BUF_GPT(buf) ((buf)->text->gpt + 0)
246 #define BUF_GPT_ADDR(buf) (BUF_BEG_ADDR (buf) + BI_BUF_GPT (buf) - 1)
248 /* Set gap location. */
249 #define SET_BI_BUF_GPT(buf, value) do { (buf)->text->gpt = (value); } while (0)
251 /* Set end of buffer. */
252 #define SET_BOTH_BUF_Z(buf, val, bival) \
255 (buf)->text->z = (bival); \
256 (buf)->text->bufz = (val); \
259 /* Under Mule, we maintain two sentinels in the buffer: one at the
260 beginning of the gap, and one at the end of the buffer. This
261 allows us to move forward, examining bytes looking for the
262 end of a character, and not worry about running off the end.
263 We do not need corresponding sentinels when moving backwards
264 because we do not have to look past the beginning of a character
265 to find the beginning of the character.
267 Every time we change the beginning of the gap, we have to
268 call SET_GAP_SENTINEL().
270 Every time we change the total size (characters plus gap)
271 of the buffer, we have to call SET_END_SENTINEL().
276 # define GAP_CAN_HOLD_SIZE_P(buf, len) (BUF_GAP_SIZE (buf) >= (len) + 1)
277 # define SET_GAP_SENTINEL(buf) (*BUF_GPT_ADDR (buf) = 0)
278 # define BUF_END_SENTINEL_SIZE 1
279 # define SET_END_SENTINEL(buf) \
280 (*(BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + BI_BUF_Z (buf) - 1) = 0)
282 # define GAP_CAN_HOLD_SIZE_P(buf, len) (BUF_GAP_SIZE (buf) >= (len))
283 # define SET_GAP_SENTINEL(buf)
284 # define BUF_END_SENTINEL_SIZE 0
285 # define SET_END_SENTINEL(buf)
289 /************************************************************************/
290 /* Charcount/Bytecount conversion */
291 /************************************************************************/
293 /* Optimization. Do it. Live it. Love it. */
297 /* We include the basic functions here that require no specific
298 knowledge of how data is Mule-encoded into a buffer other
299 than the basic (00 - 7F), (80 - 9F), (A0 - FF) scheme.
300 Anything that requires more specific knowledge goes into
303 /* Given a pointer to a text string and a length in bytes, return
304 the equivalent length in characters. */
307 bytecount_to_charcount (const Bufbyte *ptr, Bytecount len)
310 const Bufbyte *end = ptr + len;
313 # define STRIDE_TYPE long
314 # define HIGH_BIT_MASK 0x8080808080808080UL
315 #elif SIZEOF_LONG_LONG == 8 && !(defined (i386) || defined (__i386__))
316 # define STRIDE_TYPE long long
317 # define HIGH_BIT_MASK 0x8080808080808080ULL
318 #elif SIZEOF_LONG == 4
319 # define STRIDE_TYPE long
320 # define HIGH_BIT_MASK 0x80808080UL
322 # error Add support for 128-bit systems here
325 #define ALIGN_BITS ((EMACS_UINT) (ALIGNOF (STRIDE_TYPE) - 1))
326 #define ALIGN_MASK (~ ALIGN_BITS)
327 #define ALIGNED(ptr) ((((EMACS_UINT) ptr) & ALIGN_BITS) == 0)
328 #define STRIDE sizeof (STRIDE_TYPE)
332 if (BYTE_ASCII_P (*ptr))
334 /* optimize for long stretches of ASCII */
339 const unsigned STRIDE_TYPE *ascii_end =
340 (const unsigned STRIDE_TYPE *) ptr;
341 /* This loop screams, because we can typically
342 detect ASCII characters 8 at a time. */
343 while ((const Bufbyte *) ascii_end + STRIDE <= end
344 && !(*ascii_end & HIGH_BIT_MASK))
346 if ((Bufbyte *) ascii_end == ptr)
350 count += (Bufbyte *) ascii_end - ptr;
351 ptr = (Bufbyte *) ascii_end;
357 /* optimize for successive characters from the same charset */
358 Bufbyte leading_byte = *ptr;
359 size_t bytes = REP_BYTES_BY_FIRST_BYTE (leading_byte);
360 while ((ptr < end) && (*ptr == leading_byte))
361 ptr += bytes, count++;
365 #ifdef ERROR_CHECK_BUFPOS
366 /* Bomb out if the specified substring ends in the middle
367 of a character. Note that we might have already gotten
368 a core dump above from an invalid reference, but at least
369 we will get no farther than here. */
376 /* Given a pointer to a text string and a length in characters, return
377 the equivalent length in bytes. */
380 charcount_to_bytecount (const Bufbyte *ptr, Charcount len)
382 const Bufbyte *newptr = ptr;
386 INC_CHARPTR (newptr);
392 /* The next two functions are the actual meat behind the
393 bufpos-to-bytind and bytind-to-bufpos conversions. Currently
394 the method they use is fairly unsophisticated; see buffer.h.
396 Note that bufpos_to_bytind_func() is probably the most-called
397 function in all of XEmacs. Therefore, it must be FAST FAST FAST.
398 This is the reason why so much of the code is duplicated.
400 Similar considerations apply to bytind_to_bufpos_func(), although
401 less so because the function is not called so often.
403 #### At some point this should use a more sophisticated method;
406 static int not_very_random_number;
409 bufpos_to_bytind_func (struct buffer *buf, Bufpos x)
419 int add_to_cache = 0;
421 /* Check for some cached positions, for speed. */
422 if (x == BUF_PT (buf))
423 return BI_BUF_PT (buf);
424 if (x == BUF_ZV (buf))
425 return BI_BUF_ZV (buf);
426 if (x == BUF_BEGV (buf))
427 return BI_BUF_BEGV (buf);
429 bufmin = buf->text->mule_bufmin;
430 bufmax = buf->text->mule_bufmax;
431 bytmin = buf->text->mule_bytmin;
432 bytmax = buf->text->mule_bytmax;
433 size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p;
435 /* The basic idea here is that we shift the "known region" up or down
436 until it overlaps the specified position. We do this by moving
437 the upper bound of the known region up one character at a time,
438 and moving the lower bound of the known region up as necessary
439 when the size of the character just seen changes.
441 We optimize this, however, by first shifting the known region to
442 one of the cached points if it's close by. (We don't check BEG or
443 Z, even though they're cached; most of the time these will be the
444 same as BEGV and ZV, and when they're not, they're not likely
449 Bufpos diffmax = x - bufmax;
450 Bufpos diffpt = x - BUF_PT (buf);
451 Bufpos diffzv = BUF_ZV (buf) - x;
452 /* #### This value could stand some more exploration. */
453 Charcount heuristic_hack = (bufmax - bufmin) >> 2;
455 /* Check if the position is closer to PT or ZV than to the
456 end of the known region. */
463 /* But also implement a heuristic that favors the known region
464 over PT or ZV. The reason for this is that switching to
465 PT or ZV will wipe out the knowledge in the known region,
466 which might be annoying if the known region is large and
467 PT or ZV is not that much closer than the end of the known
470 diffzv += heuristic_hack;
471 diffpt += heuristic_hack;
472 if (diffpt < diffmax && diffpt <= diffzv)
474 bufmax = bufmin = BUF_PT (buf);
475 bytmax = bytmin = BI_BUF_PT (buf);
476 /* We set the size to 1 even though it doesn't really
477 matter because the new known region contains no
478 characters. We do this because this is the most
479 likely size of the characters around the new known
480 region, and we avoid potential yuckiness that is
481 done when size == 3. */
484 if (diffzv < diffmax)
486 bufmax = bufmin = BUF_ZV (buf);
487 bytmax = bytmin = BI_BUF_ZV (buf);
491 #ifdef ERROR_CHECK_BUFPOS
492 else if (x >= bufmin)
497 Bufpos diffmin = bufmin - x;
498 Bufpos diffpt = BUF_PT (buf) - x;
499 Bufpos diffbegv = x - BUF_BEGV (buf);
500 /* #### This value could stand some more exploration. */
501 Charcount heuristic_hack = (bufmax - bufmin) >> 2;
506 diffbegv = -diffbegv;
508 /* But also implement a heuristic that favors the known region --
511 diffbegv += heuristic_hack;
512 diffpt += heuristic_hack;
514 if (diffpt < diffmin && diffpt <= diffbegv)
516 bufmax = bufmin = BUF_PT (buf);
517 bytmax = bytmin = BI_BUF_PT (buf);
518 /* We set the size to 1 even though it doesn't really
519 matter because the new known region contains no
520 characters. We do this because this is the most
521 likely size of the characters around the new known
522 region, and we avoid potential yuckiness that is
523 done when size == 3. */
526 if (diffbegv < diffmin)
528 bufmax = bufmin = BUF_BEGV (buf);
529 bytmax = bytmin = BI_BUF_BEGV (buf);
534 diff_so_far = x > bufmax ? x - bufmax : bufmin - x;
535 if (diff_so_far > 50)
537 /* If we have to move more than a certain amount, then look
539 int minval = INT_MAX;
544 /* I considered keeping the positions ordered. This would speed
545 up this loop, but updating the cache would take longer, so
546 it doesn't seem like it would really matter. */
547 for (i = 0; i < 16; i++)
549 int diff = buf->text->mule_bufpos_cache[i] - x;
560 if (minval < diff_so_far)
562 bufmax = bufmin = buf->text->mule_bufpos_cache[found];
563 bytmax = bytmin = buf->text->mule_bytind_cache[found];
568 /* It's conceivable that the caching above could lead to X being
569 the same as one of the range edges. */
580 INC_BYTIND (buf, newmax);
581 newsize = newmax - bytmax;
593 /* #### Should go past the found location to reduce the number
594 of times that this function is called */
596 else /* x < bufmin */
606 DEC_BYTIND (buf, newmin);
607 newsize = bytmin - newmin;
619 /* #### Should go past the found location to reduce the number
620 of times that this function is called
624 /* If size is three, than we have to max sure that the range we
625 discovered isn't too large, because we use a fixed-length
626 table to divide by 3. */
630 int gap = bytmax - bytmin;
631 buf->text->mule_three_p = 1;
632 buf->text->mule_shifter = 1;
634 if (gap > MAX_BYTIND_GAP_SIZE_3)
638 bytmin = bytmax - MAX_BYTIND_GAP_SIZE_3;
639 bufmin = bufmax - MAX_BUFPOS_GAP_SIZE_3;
643 bytmax = bytmin + MAX_BYTIND_GAP_SIZE_3;
644 bufmax = bufmin + MAX_BUFPOS_GAP_SIZE_3;
650 buf->text->mule_three_p = 0;
652 buf->text->mule_shifter = 2;
654 buf->text->mule_shifter = size - 1;
657 buf->text->mule_bufmin = bufmin;
658 buf->text->mule_bufmax = bufmax;
659 buf->text->mule_bytmin = bytmin;
660 buf->text->mule_bytmax = bytmax;
666 /* We throw away a "random" cached value and replace it with
667 the new value. It doesn't actually have to be very random
668 at all, just evenly distributed.
670 #### It would be better to use a least-recently-used algorithm
671 or something that tries to space things out, but I'm not sure
672 it's worth it to go to the trouble of maintaining that. */
673 not_very_random_number += 621;
674 replace_loc = not_very_random_number & 15;
675 buf->text->mule_bufpos_cache[replace_loc] = x;
676 buf->text->mule_bytind_cache[replace_loc] = retval;
682 /* The logic in this function is almost identical to the logic in
683 the previous function. */
686 bytind_to_bufpos_func (struct buffer *buf, Bytind x)
696 int add_to_cache = 0;
698 /* Check for some cached positions, for speed. */
699 if (x == BI_BUF_PT (buf))
701 if (x == BI_BUF_ZV (buf))
703 if (x == BI_BUF_BEGV (buf))
704 return BUF_BEGV (buf);
706 bufmin = buf->text->mule_bufmin;
707 bufmax = buf->text->mule_bufmax;
708 bytmin = buf->text->mule_bytmin;
709 bytmax = buf->text->mule_bytmax;
710 size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p;
712 /* The basic idea here is that we shift the "known region" up or down
713 until it overlaps the specified position. We do this by moving
714 the upper bound of the known region up one character at a time,
715 and moving the lower bound of the known region up as necessary
716 when the size of the character just seen changes.
718 We optimize this, however, by first shifting the known region to
719 one of the cached points if it's close by. (We don't check BI_BEG or
720 BI_Z, even though they're cached; most of the time these will be the
721 same as BI_BEGV and BI_ZV, and when they're not, they're not likely
726 Bytind diffmax = x - bytmax;
727 Bytind diffpt = x - BI_BUF_PT (buf);
728 Bytind diffzv = BI_BUF_ZV (buf) - x;
729 /* #### This value could stand some more exploration. */
730 Bytecount heuristic_hack = (bytmax - bytmin) >> 2;
732 /* Check if the position is closer to PT or ZV than to the
733 end of the known region. */
740 /* But also implement a heuristic that favors the known region
741 over BI_PT or BI_ZV. The reason for this is that switching to
742 BI_PT or BI_ZV will wipe out the knowledge in the known region,
743 which might be annoying if the known region is large and
744 BI_PT or BI_ZV is not that much closer than the end of the known
747 diffzv += heuristic_hack;
748 diffpt += heuristic_hack;
749 if (diffpt < diffmax && diffpt <= diffzv)
751 bufmax = bufmin = BUF_PT (buf);
752 bytmax = bytmin = BI_BUF_PT (buf);
753 /* We set the size to 1 even though it doesn't really
754 matter because the new known region contains no
755 characters. We do this because this is the most
756 likely size of the characters around the new known
757 region, and we avoid potential yuckiness that is
758 done when size == 3. */
761 if (diffzv < diffmax)
763 bufmax = bufmin = BUF_ZV (buf);
764 bytmax = bytmin = BI_BUF_ZV (buf);
768 #ifdef ERROR_CHECK_BUFPOS
769 else if (x >= bytmin)
774 Bytind diffmin = bytmin - x;
775 Bytind diffpt = BI_BUF_PT (buf) - x;
776 Bytind diffbegv = x - BI_BUF_BEGV (buf);
777 /* #### This value could stand some more exploration. */
778 Bytecount heuristic_hack = (bytmax - bytmin) >> 2;
783 diffbegv = -diffbegv;
785 /* But also implement a heuristic that favors the known region --
788 diffbegv += heuristic_hack;
789 diffpt += heuristic_hack;
791 if (diffpt < diffmin && diffpt <= diffbegv)
793 bufmax = bufmin = BUF_PT (buf);
794 bytmax = bytmin = BI_BUF_PT (buf);
795 /* We set the size to 1 even though it doesn't really
796 matter because the new known region contains no
797 characters. We do this because this is the most
798 likely size of the characters around the new known
799 region, and we avoid potential yuckiness that is
800 done when size == 3. */
803 if (diffbegv < diffmin)
805 bufmax = bufmin = BUF_BEGV (buf);
806 bytmax = bytmin = BI_BUF_BEGV (buf);
811 diff_so_far = x > bytmax ? x - bytmax : bytmin - x;
812 if (diff_so_far > 50)
814 /* If we have to move more than a certain amount, then look
816 int minval = INT_MAX;
821 /* I considered keeping the positions ordered. This would speed
822 up this loop, but updating the cache would take longer, so
823 it doesn't seem like it would really matter. */
824 for (i = 0; i < 16; i++)
826 int diff = buf->text->mule_bytind_cache[i] - x;
837 if (minval < diff_so_far)
839 bufmax = bufmin = buf->text->mule_bufpos_cache[found];
840 bytmax = bytmin = buf->text->mule_bytind_cache[found];
845 /* It's conceivable that the caching above could lead to X being
846 the same as one of the range edges. */
857 INC_BYTIND (buf, newmax);
858 newsize = newmax - bytmax;
870 /* #### Should go past the found location to reduce the number
871 of times that this function is called */
873 else /* x <= bytmin */
883 DEC_BYTIND (buf, newmin);
884 newsize = bytmin - newmin;
896 /* #### Should go past the found location to reduce the number
897 of times that this function is called
901 /* If size is three, than we have to max sure that the range we
902 discovered isn't too large, because we use a fixed-length
903 table to divide by 3. */
907 int gap = bytmax - bytmin;
908 buf->text->mule_three_p = 1;
909 buf->text->mule_shifter = 1;
911 if (gap > MAX_BYTIND_GAP_SIZE_3)
915 bytmin = bytmax - MAX_BYTIND_GAP_SIZE_3;
916 bufmin = bufmax - MAX_BUFPOS_GAP_SIZE_3;
920 bytmax = bytmin + MAX_BYTIND_GAP_SIZE_3;
921 bufmax = bufmin + MAX_BUFPOS_GAP_SIZE_3;
927 buf->text->mule_three_p = 0;
929 buf->text->mule_shifter = 2;
931 buf->text->mule_shifter = size - 1;
934 buf->text->mule_bufmin = bufmin;
935 buf->text->mule_bufmax = bufmax;
936 buf->text->mule_bytmin = bytmin;
937 buf->text->mule_bytmax = bytmax;
943 /* We throw away a "random" cached value and replace it with
944 the new value. It doesn't actually have to be very random
945 at all, just evenly distributed.
947 #### It would be better to use a least-recently-used algorithm
948 or something that tries to space things out, but I'm not sure
949 it's worth it to go to the trouble of maintaining that. */
950 not_very_random_number += 621;
951 replace_loc = not_very_random_number & 15;
952 buf->text->mule_bufpos_cache[replace_loc] = retval;
953 buf->text->mule_bytind_cache[replace_loc] = x;
959 /* Text of length BYTELENGTH and CHARLENGTH (in different units)
960 was inserted at bufpos START. */
963 buffer_mule_signal_inserted_region (struct buffer *buf, Bufpos start,
964 Bytecount bytelength,
965 Charcount charlength)
967 int size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p;
970 /* Adjust the cache of known positions. */
971 for (i = 0; i < 16; i++)
974 if (buf->text->mule_bufpos_cache[i] > start)
976 buf->text->mule_bufpos_cache[i] += charlength;
977 buf->text->mule_bytind_cache[i] += bytelength;
981 if (start >= buf->text->mule_bufmax)
984 /* The insertion is either before the known region, in which case
985 it shoves it forward; or within the known region, in which case
986 it shoves the end forward. (But it may make the known region
987 inconsistent, so we may have to shorten it.) */
989 if (start <= buf->text->mule_bufmin)
991 buf->text->mule_bufmin += charlength;
992 buf->text->mule_bufmax += charlength;
993 buf->text->mule_bytmin += bytelength;
994 buf->text->mule_bytmax += bytelength;
998 Bufpos end = start + charlength;
999 /* the insertion point divides the known region in two.
1000 Keep the longer half, at least, and expand into the
1001 inserted chunk as much as possible. */
1003 if (start - buf->text->mule_bufmin > buf->text->mule_bufmax - start)
1005 Bytind bytestart = (buf->text->mule_bytmin
1006 + size * (start - buf->text->mule_bufmin));
1011 bytenew = bytestart;
1012 INC_BYTIND (buf, bytenew);
1013 if (bytenew - bytestart != size)
1016 bytestart = bytenew;
1020 buf->text->mule_bufmax = start;
1021 buf->text->mule_bytmax = bytestart;
1025 buf->text->mule_bufmax += charlength;
1026 buf->text->mule_bytmax += bytelength;
1031 Bytind byteend = (buf->text->mule_bytmin
1032 + size * (start - buf->text->mule_bufmin)
1036 buf->text->mule_bufmax += charlength;
1037 buf->text->mule_bytmax += bytelength;
1042 DEC_BYTIND (buf, bytenew);
1043 if (byteend - bytenew != size)
1050 buf->text->mule_bufmin = end;
1051 buf->text->mule_bytmin = byteend;
1057 /* Text from START to END (equivalent in Bytinds: from BI_START to
1058 BI_END) was deleted. */
1061 buffer_mule_signal_deleted_region (struct buffer *buf, Bufpos start,
1062 Bufpos end, Bytind bi_start,
1067 /* Adjust the cache of known positions. */
1068 for (i = 0; i < 16; i++)
1070 /* After the end; gets shoved backward */
1071 if (buf->text->mule_bufpos_cache[i] > end)
1073 buf->text->mule_bufpos_cache[i] -= end - start;
1074 buf->text->mule_bytind_cache[i] -= bi_end - bi_start;
1076 /* In the range; moves to start of range */
1077 else if (buf->text->mule_bufpos_cache[i] > start)
1079 buf->text->mule_bufpos_cache[i] = start;
1080 buf->text->mule_bytind_cache[i] = bi_start;
1084 /* We don't care about any text after the end of the known region. */
1086 end = min (end, buf->text->mule_bufmax);
1087 bi_end = min (bi_end, buf->text->mule_bytmax);
1091 /* The end of the known region offsets by the total amount of deletion,
1092 since it's all before it. */
1094 buf->text->mule_bufmax -= end - start;
1095 buf->text->mule_bytmax -= bi_end - bi_start;
1097 /* Now we don't care about any text after the start of the known region. */
1099 end = min (end, buf->text->mule_bufmin);
1100 bi_end = min (bi_end, buf->text->mule_bytmin);
1104 buf->text->mule_bufmin -= end - start;
1105 buf->text->mule_bytmin -= bi_end - bi_start;
1110 #ifdef ERROR_CHECK_BUFPOS
1113 bufpos_to_bytind (struct buffer *buf, Bufpos x)
1115 Bytind retval = real_bufpos_to_bytind (buf, x);
1116 ASSERT_VALID_BYTIND_UNSAFE (buf, retval);
1121 bytind_to_bufpos (struct buffer *buf, Bytind x)
1123 ASSERT_VALID_BYTIND_UNSAFE (buf, x);
1124 return real_bytind_to_bufpos (buf, x);
1127 #endif /* ERROR_CHECK_BUFPOS */
1130 /************************************************************************/
1131 /* verifying buffer and string positions */
1132 /************************************************************************/
1134 /* Functions below are tagged with either _byte or _char indicating
1135 whether they return byte or character positions. For a buffer,
1136 a character position is a "Bufpos" and a byte position is a "Bytind".
1137 For strings, these are sometimes typed using "Charcount" and
1140 /* Flags for the functions below are:
1142 GB_ALLOW_PAST_ACCESSIBLE
1144 Allow positions to range over the entire buffer (BUF_BEG to BUF_Z),
1145 rather than just the accessible portion (BUF_BEGV to BUF_ZV).
1146 For strings, this flag has no effect.
1150 If the position is outside the allowable range, return the lower
1151 or upper bound of the range, whichever is closer to the specified
1156 If the position is outside the allowable range, return -1.
1158 GB_NEGATIVE_FROM_END
1160 If a value is negative, treat it as an offset from the end.
1161 Only applies to strings.
1163 The following additional flags apply only to the functions
1168 Either or both positions can be nil. If FROM is nil,
1169 FROM_OUT will contain the lower bound of the allowed range.
1170 If TO is nil, TO_OUT will contain the upper bound of the
1175 FROM must contain the lower bound and TO the upper bound
1176 of the range. If the positions are reversed, an error is
1179 The following is a combination flag:
1181 GB_HISTORICAL_STRING_BEHAVIOR
1183 Equivalent to (GB_NEGATIVE_FROM_END | GB_ALLOW_NIL).
1186 /* Return a buffer position stored in a Lisp_Object. Full
1187 error-checking is done on the position. Flags can be specified to
1188 control the behavior of out-of-range values. The default behavior
1189 is to require that the position is within the accessible part of
1190 the buffer (BEGV and ZV), and to signal an error if the position is
1196 get_buffer_pos_char (struct buffer *b, Lisp_Object pos, unsigned int flags)
1199 Bufpos min_allowed, max_allowed;
1201 CHECK_INT_COERCE_MARKER (pos);
1203 min_allowed = flags & GB_ALLOW_PAST_ACCESSIBLE ? BUF_BEG (b) : BUF_BEGV (b);
1204 max_allowed = flags & GB_ALLOW_PAST_ACCESSIBLE ? BUF_Z (b) : BUF_ZV (b);
1206 if (ind < min_allowed || ind > max_allowed)
1208 if (flags & GB_COERCE_RANGE)
1209 ind = ind < min_allowed ? min_allowed : max_allowed;
1210 else if (flags & GB_NO_ERROR_IF_BAD)
1215 XSETBUFFER (buffer, b);
1216 args_out_of_range (buffer, pos);
1224 get_buffer_pos_byte (struct buffer *b, Lisp_Object pos, unsigned int flags)
1226 Bufpos bpos = get_buffer_pos_char (b, pos, flags);
1227 if (bpos < 0) /* could happen with GB_NO_ERROR_IF_BAD */
1229 return bufpos_to_bytind (b, bpos);
1232 /* Return a pair of buffer positions representing a range of text,
1233 taken from a pair of Lisp_Objects. Full error-checking is
1234 done on the positions. Flags can be specified to control the
1235 behavior of out-of-range values. The default behavior is to
1236 allow the range bounds to be specified in either order
1237 (however, FROM_OUT will always be the lower bound of the range
1238 and TO_OUT the upper bound),to require that the positions
1239 are within the accessible part of the buffer (BEGV and ZV),
1240 and to signal an error if the positions are out of range.
1244 get_buffer_range_char (struct buffer *b, Lisp_Object from, Lisp_Object to,
1245 Bufpos *from_out, Bufpos *to_out, unsigned int flags)
1247 Bufpos min_allowed, max_allowed;
1249 min_allowed = (flags & GB_ALLOW_PAST_ACCESSIBLE) ?
1250 BUF_BEG (b) : BUF_BEGV (b);
1251 max_allowed = (flags & GB_ALLOW_PAST_ACCESSIBLE) ?
1252 BUF_Z (b) : BUF_ZV (b);
1254 if (NILP (from) && (flags & GB_ALLOW_NIL))
1255 *from_out = min_allowed;
1257 *from_out = get_buffer_pos_char (b, from, flags | GB_NO_ERROR_IF_BAD);
1259 if (NILP (to) && (flags & GB_ALLOW_NIL))
1260 *to_out = max_allowed;
1262 *to_out = get_buffer_pos_char (b, to, flags | GB_NO_ERROR_IF_BAD);
1264 if ((*from_out < 0 || *to_out < 0) && !(flags & GB_NO_ERROR_IF_BAD))
1267 XSETBUFFER (buffer, b);
1268 args_out_of_range_3 (buffer, from, to);
1271 if (*from_out >= 0 && *to_out >= 0 && *from_out > *to_out)
1273 if (flags & GB_CHECK_ORDER)
1274 signal_simple_error_2 ("start greater than end", from, to);
1277 Bufpos temp = *from_out;
1278 *from_out = *to_out;
1285 get_buffer_range_byte (struct buffer *b, Lisp_Object from, Lisp_Object to,
1286 Bytind *from_out, Bytind *to_out, unsigned int flags)
1290 get_buffer_range_char (b, from, to, &s, &e, flags);
1292 *from_out = bufpos_to_bytind (b, s);
1293 else /* could happen with GB_NO_ERROR_IF_BAD */
1296 *to_out = bufpos_to_bytind (b, e);
1302 get_string_pos_char_1 (Lisp_Object string, Lisp_Object pos, unsigned int flags,
1303 Charcount known_length)
1306 Charcount min_allowed = 0;
1307 Charcount max_allowed = known_length;
1309 /* Computation of KNOWN_LENGTH is potentially expensive so we pass
1313 if (ccpos < 0 && flags & GB_NEGATIVE_FROM_END)
1314 ccpos += max_allowed;
1316 if (ccpos < min_allowed || ccpos > max_allowed)
1318 if (flags & GB_COERCE_RANGE)
1319 ccpos = ccpos < min_allowed ? min_allowed : max_allowed;
1320 else if (flags & GB_NO_ERROR_IF_BAD)
1323 args_out_of_range (string, pos);
1330 get_string_pos_char (Lisp_Object string, Lisp_Object pos, unsigned int flags)
1332 return get_string_pos_char_1 (string, pos, flags,
1333 XSTRING_CHAR_LENGTH (string));
1337 get_string_pos_byte (Lisp_Object string, Lisp_Object pos, unsigned int flags)
1339 Charcount ccpos = get_string_pos_char (string, pos, flags);
1340 if (ccpos < 0) /* could happen with GB_NO_ERROR_IF_BAD */
1342 return charcount_to_bytecount (XSTRING_DATA (string), ccpos);
1346 get_string_range_char (Lisp_Object string, Lisp_Object from, Lisp_Object to,
1347 Charcount *from_out, Charcount *to_out,
1350 Charcount min_allowed = 0;
1351 Charcount max_allowed = XSTRING_CHAR_LENGTH (string);
1353 if (NILP (from) && (flags & GB_ALLOW_NIL))
1354 *from_out = min_allowed;
1356 *from_out = get_string_pos_char_1 (string, from,
1357 flags | GB_NO_ERROR_IF_BAD,
1360 if (NILP (to) && (flags & GB_ALLOW_NIL))
1361 *to_out = max_allowed;
1363 *to_out = get_string_pos_char_1 (string, to,
1364 flags | GB_NO_ERROR_IF_BAD,
1367 if ((*from_out < 0 || *to_out < 0) && !(flags & GB_NO_ERROR_IF_BAD))
1368 args_out_of_range_3 (string, from, to);
1370 if (*from_out >= 0 && *to_out >= 0 && *from_out > *to_out)
1372 if (flags & GB_CHECK_ORDER)
1373 signal_simple_error_2 ("start greater than end", from, to);
1376 Bufpos temp = *from_out;
1377 *from_out = *to_out;
1384 get_string_range_byte (Lisp_Object string, Lisp_Object from, Lisp_Object to,
1385 Bytecount *from_out, Bytecount *to_out,
1390 get_string_range_char (string, from, to, &s, &e, flags);
1392 *from_out = charcount_to_bytecount (XSTRING_DATA (string), s);
1393 else /* could happen with GB_NO_ERROR_IF_BAD */
1396 *to_out = charcount_to_bytecount (XSTRING_DATA (string), e);
1403 get_buffer_or_string_pos_char (Lisp_Object object, Lisp_Object pos,
1406 return STRINGP (object) ?
1407 get_string_pos_char (object, pos, flags) :
1408 get_buffer_pos_char (XBUFFER (object), pos, flags);
1412 get_buffer_or_string_pos_byte (Lisp_Object object, Lisp_Object pos,
1415 return STRINGP (object) ?
1416 get_string_pos_byte (object, pos, flags) :
1417 get_buffer_pos_byte (XBUFFER (object), pos, flags);
1421 get_buffer_or_string_range_char (Lisp_Object object, Lisp_Object from,
1422 Lisp_Object to, Bufpos *from_out,
1423 Bufpos *to_out, unsigned int flags)
1425 if (STRINGP (object))
1426 get_string_range_char (object, from, to, from_out, to_out, flags);
1428 get_buffer_range_char (XBUFFER (object), from, to, from_out, to_out, flags);
1432 get_buffer_or_string_range_byte (Lisp_Object object, Lisp_Object from,
1433 Lisp_Object to, Bytind *from_out,
1434 Bytind *to_out, unsigned int flags)
1436 if (STRINGP (object))
1437 get_string_range_byte (object, from, to, from_out, to_out, flags);
1439 get_buffer_range_byte (XBUFFER (object), from, to, from_out, to_out, flags);
1443 buffer_or_string_accessible_begin_char (Lisp_Object object)
1445 return STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object));
1449 buffer_or_string_accessible_end_char (Lisp_Object object)
1451 return STRINGP (object) ?
1452 XSTRING_CHAR_LENGTH (object) : BUF_ZV (XBUFFER (object));
1456 buffer_or_string_accessible_begin_byte (Lisp_Object object)
1458 return STRINGP (object) ? 0 : BI_BUF_BEGV (XBUFFER (object));
1462 buffer_or_string_accessible_end_byte (Lisp_Object object)
1464 return STRINGP (object) ?
1465 XSTRING_LENGTH (object) : BI_BUF_ZV (XBUFFER (object));
1469 buffer_or_string_absolute_begin_char (Lisp_Object object)
1471 return STRINGP (object) ? 0 : BUF_BEG (XBUFFER (object));
1475 buffer_or_string_absolute_end_char (Lisp_Object object)
1477 return STRINGP (object) ?
1478 XSTRING_CHAR_LENGTH (object) : BUF_Z (XBUFFER (object));
1482 buffer_or_string_absolute_begin_byte (Lisp_Object object)
1484 return STRINGP (object) ? 0 : BI_BUF_BEG (XBUFFER (object));
1488 buffer_or_string_absolute_end_byte (Lisp_Object object)
1490 return STRINGP (object) ?
1491 XSTRING_LENGTH (object) : BI_BUF_Z (XBUFFER (object));
1495 /************************************************************************/
1496 /* point and marker adjustment */
1497 /************************************************************************/
1499 /* just_set_point() is the only place `PT' is an lvalue in all of emacs.
1500 This function is called from set_buffer_point(), which is the function
1501 that the SET_PT and BUF_SET_PT macros expand into, and from the
1502 routines below that insert and delete text. (This is in cases where
1503 the point marker logically doesn't move but PT (being a byte index)
1504 needs to get adjusted.) */
1506 /* Set point to a specified value. This is used only when the value
1507 of point changes due to an insert or delete; it does not represent
1508 a conceptual change in point as a marker. In particular, point is
1509 not crossing any interval boundaries, so there's no need to use the
1510 usual SET_PT macro. In fact it would be incorrect to do so, because
1511 either the old or the new value of point is out of synch with the
1512 current set of intervals. */
1514 /* This gets called more than enough to make the function call
1515 overhead a significant factor so we've turned it into a macro. */
1516 #define JUST_SET_POINT(buf, bufpos, ind) \
1519 buf->bufpt = (bufpos); \
1523 /* Set a buffer's point. */
1526 set_buffer_point (struct buffer *buf, Bufpos bufpos, Bytind bytpos)
1528 assert (bytpos >= BI_BUF_BEGV (buf) && bytpos <= BI_BUF_ZV (buf));
1529 if (bytpos == BI_BUF_PT (buf))
1531 JUST_SET_POINT (buf, bufpos, bytpos);
1533 assert (MARKERP (buf->point_marker));
1534 XMARKER (buf->point_marker)->memind =
1535 bytind_to_memind (buf, bytpos);
1537 /* FSF makes sure that PT is not being set within invisible text.
1538 However, this is the wrong place for that check. The check
1539 should happen only at the next redisplay. */
1541 /* Some old coder said:
1543 "If there were to be hooks which were run when point entered/left an
1544 extent, this would be the place to put them.
1546 However, it's probably the case that such hooks should be implemented
1547 using a post-command-hook instead, to avoid running the hooks as a
1548 result of intermediate motion inside of save-excursions, for example."
1550 I definitely agree with this. PT gets moved all over the place
1551 and it would be a Bad Thing for any hooks to get called, both for
1552 the reason above and because many callers are not prepared for
1553 a GC within this function. --ben
1557 /* Do the correct marker-like adjustment on MPOS (see below). FROM, TO,
1558 and AMOUNT are as in adjust_markers(). If MPOS doesn't need to be
1559 adjusted, nothing will happen. */
1561 do_marker_adjustment (Memind mpos, Memind from,
1562 Memind to, Bytecount amount)
1566 if (mpos > to && mpos < to + amount)
1571 if (mpos > from + amount && mpos <= from)
1572 mpos = from + amount;
1574 if (mpos > from && mpos <= to)
1579 /* Do the following:
1581 (1) Add `amount' to the position of every marker in the current buffer
1582 whose current position is between `from' (exclusive) and `to' (inclusive).
1584 (2) Also, any markers past the outside of that interval, in the direction
1585 of adjustment, are first moved back to the near end of the interval
1586 and then adjusted by `amount'.
1588 This function is called in two different cases: when a region of
1589 characters adjacent to the gap is moved, causing the gap to shift
1590 to the other side of the region (in this case, `from' and `to'
1591 point to the old position of the region and there should be no
1592 markers affected by (2) because they would be inside the gap),
1593 or when a region of characters adjacent to the gap is wiped out,
1594 causing the gap to increase to include the region (in this case,
1595 `from' and `to' are the same, both pointing to the boundary
1596 between the gap and the deleted region, and there are no markers
1599 The reason for the use of exclusive and inclusive is that markers at
1600 the gap always sit at the beginning, not at the end.
1604 adjust_markers (struct buffer *buf, Memind from, Memind to,
1609 for (m = BUF_MARKERS (buf); m; m = marker_next (m))
1610 m->memind = do_marker_adjustment (m->memind, from, to, amount);
1613 /* Adjust markers whose insertion-type is t
1614 for an insertion of AMOUNT characters at POS. */
1617 adjust_markers_for_insert (struct buffer *buf, Memind ind, Bytecount amount)
1621 for (m = BUF_MARKERS (buf); m; m = marker_next (m))
1623 if (m->insertion_type && m->memind == ind)
1624 m->memind += amount;
1629 /************************************************************************/
1630 /* Routines for dealing with the gap */
1631 /************************************************************************/
1633 /* maximum amount of memory moved in a single chunk. Increasing this
1634 value improves gap-motion efficiency but decreases QUIT responsiveness
1635 time. Was 32000 but today's processors are faster and files are
1637 #define GAP_MOVE_CHUNK 300000
1639 /* Move the gap to POS, which is less than the current GPT. */
1642 gap_left (struct buffer *buf, Bytind pos)
1647 struct buffer *mbuf;
1648 Lisp_Object bufcons;
1650 from = BUF_GPT_ADDR (buf);
1651 to = from + BUF_GAP_SIZE (buf);
1652 new_s1 = BI_BUF_GPT (buf);
1654 /* Now copy the characters. To move the gap down,
1655 copy characters up. */
1659 /* I gets number of characters left to copy. */
1663 /* If a quit is requested, stop copying now.
1664 Change POS to be where we have actually moved the gap to. */
1670 /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */
1671 if (i > GAP_MOVE_CHUNK)
1679 memmove (to, from, i);
1689 /* Adjust markers, and buffer data structure, to put the gap at POS.
1690 POS is where the loop above stopped, which may be what was specified
1691 or may be where a quit was detected. */
1692 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1694 adjust_markers (mbuf, pos, BI_BUF_GPT (mbuf), BUF_GAP_SIZE (mbuf));
1696 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1698 adjust_extents (make_buffer (mbuf), pos, BI_BUF_GPT (mbuf),
1699 BUF_GAP_SIZE (mbuf));
1701 SET_BI_BUF_GPT (buf, pos);
1702 SET_GAP_SENTINEL (buf);
1703 #ifdef ERROR_CHECK_EXTENTS
1704 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1706 sledgehammer_extent_check (make_buffer (mbuf));
1713 gap_right (struct buffer *buf, Bytind pos)
1718 struct buffer *mbuf;
1719 Lisp_Object bufcons;
1721 to = BUF_GPT_ADDR (buf);
1722 from = to + BUF_GAP_SIZE (buf);
1723 new_s1 = BI_BUF_GPT (buf);
1725 /* Now copy the characters. To move the gap up,
1726 copy characters down. */
1730 /* I gets number of characters left to copy. */
1734 /* If a quit is requested, stop copying now.
1735 Change POS to be where we have actually moved the gap to. */
1741 /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */
1742 if (i > GAP_MOVE_CHUNK)
1748 memmove (to, from, i);
1761 int gsize = BUF_GAP_SIZE (buf);
1762 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1764 adjust_markers (mbuf, BI_BUF_GPT (mbuf) + gsize, pos + gsize, - gsize);
1766 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1768 adjust_extents (make_buffer (mbuf), BI_BUF_GPT (mbuf) + gsize,
1769 pos + gsize, - gsize);
1771 SET_BI_BUF_GPT (buf, pos);
1772 SET_GAP_SENTINEL (buf);
1773 #ifdef ERROR_CHECK_EXTENTS
1774 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1776 sledgehammer_extent_check (make_buffer (mbuf));
1780 if (pos == BI_BUF_Z (buf))
1782 /* merge gap with end gap */
1784 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + BUF_END_GAP_SIZE (buf));
1785 SET_BUF_END_GAP_SIZE (buf, 0);
1786 SET_END_SENTINEL (buf);
1792 /* Move gap to position `pos'.
1793 Note that this can quit! */
1796 move_gap (struct buffer *buf, Bytind pos)
1798 if (! BUF_BEG_ADDR (buf))
1800 if (pos < BI_BUF_GPT (buf))
1801 gap_left (buf, pos);
1802 else if (pos > BI_BUF_GPT (buf))
1803 gap_right (buf, pos);
1806 /* Merge the end gap into the gap */
1809 merge_gap_with_end_gap (struct buffer *buf)
1812 Bytind real_gap_loc;
1813 Bytecount old_gap_size;
1814 Bytecount increment;
1816 increment = BUF_END_GAP_SIZE (buf);
1817 SET_BUF_END_GAP_SIZE (buf, 0);
1821 /* Prevent quitting in move_gap. */
1822 tem = Vinhibit_quit;
1825 real_gap_loc = BI_BUF_GPT (buf);
1826 old_gap_size = BUF_GAP_SIZE (buf);
1828 /* Pretend the end gap is the gap */
1829 SET_BI_BUF_GPT (buf, BI_BUF_Z (buf) + BUF_GAP_SIZE (buf));
1830 SET_BUF_GAP_SIZE (buf, increment);
1832 /* Move the new gap down to be consecutive with the end of the old one.
1833 This adjusts the markers properly too. */
1834 gap_left (buf, real_gap_loc + old_gap_size);
1836 /* Now combine the two into one large gap. */
1837 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + old_gap_size);
1838 SET_BI_BUF_GPT (buf, real_gap_loc);
1839 SET_GAP_SENTINEL (buf);
1841 /* We changed the total size of the buffer (including gap),
1842 so we need to fix up the end sentinel. */
1843 SET_END_SENTINEL (buf);
1845 Vinhibit_quit = tem;
1849 /* Make the gap INCREMENT bytes longer. */
1852 make_gap (struct buffer *buf, Bytecount increment)
1856 Bytind real_gap_loc;
1857 Bytecount old_gap_size;
1859 /* If we have to get more space, get enough to last a while. We use
1860 a geometric progression that saves on realloc space. */
1861 increment += 2000 + ((BI_BUF_Z (buf) - BI_BUF_BEG (buf)) / 8);
1863 if (increment > BUF_END_GAP_SIZE (buf))
1865 /* Don't allow a buffer size that won't fit in an int
1866 even if it will fit in a Lisp integer.
1867 That won't work because so many places use `int'. */
1869 if (BUF_Z (buf) - BUF_BEG (buf) + BUF_GAP_SIZE (buf) + increment
1871 error ("Maximum buffer size exceeded");
1873 result = BUFFER_REALLOC (buf->text->beg,
1874 BI_BUF_Z (buf) - BI_BUF_BEG (buf) +
1875 BUF_GAP_SIZE (buf) + increment +
1876 BUF_END_SENTINEL_SIZE);
1880 SET_BUF_BEG_ADDR (buf, result);
1883 increment = BUF_END_GAP_SIZE (buf);
1885 /* Prevent quitting in move_gap. */
1886 tem = Vinhibit_quit;
1889 real_gap_loc = BI_BUF_GPT (buf);
1890 old_gap_size = BUF_GAP_SIZE (buf);
1892 /* Call the newly allocated space a gap at the end of the whole space. */
1893 SET_BI_BUF_GPT (buf, BI_BUF_Z (buf) + BUF_GAP_SIZE (buf));
1894 SET_BUF_GAP_SIZE (buf, increment);
1896 SET_BUF_END_GAP_SIZE (buf, 0);
1898 /* Move the new gap down to be consecutive with the end of the old one.
1899 This adjusts the markers properly too. */
1900 gap_left (buf, real_gap_loc + old_gap_size);
1902 /* Now combine the two into one large gap. */
1903 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + old_gap_size);
1904 SET_BI_BUF_GPT (buf, real_gap_loc);
1905 SET_GAP_SENTINEL (buf);
1907 /* We changed the total size of the buffer (including gap),
1908 so we need to fix up the end sentinel. */
1909 SET_END_SENTINEL (buf);
1911 Vinhibit_quit = tem;
1915 /************************************************************************/
1916 /* Before/after-change processing */
1917 /************************************************************************/
1919 /* Those magic changes ... */
1922 buffer_signal_changed_region (struct buffer *buf, Bufpos start,
1925 /* The changed region is recorded as the number of unchanged
1926 characters from the beginning and from the end of the
1927 buffer. This obviates much of the need of shifting the
1928 region around to compensate for insertions and deletions.
1930 if (buf->changes->begin_unchanged < 0 ||
1931 buf->changes->begin_unchanged > start - BUF_BEG (buf))
1932 buf->changes->begin_unchanged = start - BUF_BEG (buf);
1933 if (buf->changes->end_unchanged < 0 ||
1934 buf->changes->end_unchanged > BUF_Z (buf) - end)
1935 buf->changes->end_unchanged = BUF_Z (buf) - end;
1939 buffer_extent_signal_changed_region (struct buffer *buf, Bufpos start,
1942 if (buf->changes->begin_extent_unchanged < 0 ||
1943 buf->changes->begin_extent_unchanged > start - BUF_BEG (buf))
1944 buf->changes->begin_extent_unchanged = start - BUF_BEG (buf);
1945 if (buf->changes->end_extent_unchanged < 0 ||
1946 buf->changes->end_extent_unchanged > BUF_Z (buf) - end)
1947 buf->changes->end_extent_unchanged = BUF_Z (buf) - end;
1951 buffer_reset_changes (struct buffer *buf)
1953 buf->changes->begin_unchanged = -1;
1954 buf->changes->end_unchanged = -1;
1955 buf->changes->begin_extent_unchanged = -1;
1956 buf->changes->end_extent_unchanged = -1;
1957 buf->changes->newline_was_deleted = 0;
1961 signal_after_change (struct buffer *buf, Bufpos start, Bufpos orig_end,
1965 /* Call the after-change-functions according to the changes made so far
1966 and treat all further changes as single until the outermost
1967 multiple change exits. This is called when the outermost multiple
1968 change exits and when someone is trying to make a change that violates
1969 the constraints specified in begin_multiple_change(), typically
1970 when nested multiple-change sessions occur. (There are smarter ways of
1971 dealing with nested multiple changes, but these rarely occur so there's
1972 probably no point in it.) */
1974 /* #### This needs to keep track of what actually changed and only
1975 call the after-change functions on that region. */
1978 cancel_multiple_change (struct buffer *buf)
1980 /* This function can GC */
1981 /* Call the after-change-functions except when they've already been
1982 called or when there were no changes made to the buffer at all. */
1983 if (buf->text->changes->mc_begin != 0 &&
1984 buf->text->changes->mc_begin_signaled)
1986 Bufpos real_mc_begin = buf->text->changes->mc_begin;
1987 buf->text->changes->mc_begin = 0;
1989 signal_after_change (buf, real_mc_begin, buf->text->changes->mc_orig_end,
1990 buf->text->changes->mc_new_end);
1994 buf->text->changes->mc_begin = 0;
1998 /* this is an unwind_protect, to ensure that the after-change-functions
1999 get called even in a non-local exit. */
2002 multiple_change_finish_up (Lisp_Object buffer)
2004 struct buffer *buf = XBUFFER (buffer);
2006 /* #### I don't know whether or not it should even be possible to
2007 get here with a dead buffer (though given how it is called I can
2008 see how it might be). In any case, there isn't time before 19.14
2010 if (!BUFFER_LIVE_P (buf))
2013 /* This function can GC */
2014 buf->text->changes->in_multiple_change = 0; /* do this first so that
2015 errors in the after-change
2016 functions don't mess things
2018 cancel_multiple_change (buf);
2022 /* Call this function when you're about to make a number of buffer changes
2023 that should be considered a single change. (e.g. `replace-match' calls
2024 this.) You need to specify the START and END of the region that is
2025 going to be changed so that the before-change-functions are called
2026 with the correct arguments. The after-change region is calculated
2027 automatically, however, and if changes somehow or other happen outside
2028 of the specified region, that will also be handled correctly.
2030 begin_multiple_change() returns a number (actually a specpdl depth)
2031 that you must pass to end_multiple_change() when you are done.
2033 FSF Emacs 20 implements a similar feature, accessible from Lisp
2034 through a `combine-after-change-calls' special form, which is
2035 essentially equivalent to this function. We should consider
2036 whether we want to introduce a similar Lisp form. */
2039 begin_multiple_change (struct buffer *buf, Bufpos start, Bufpos end)
2041 /* This function can GC */
2043 if (buf->text->changes->in_multiple_change)
2045 if (buf->text->changes->mc_begin != 0 &&
2046 (start < buf->text->changes->mc_begin ||
2047 end > buf->text->changes->mc_new_end))
2048 cancel_multiple_change (buf);
2054 buf->text->changes->mc_begin = start;
2055 buf->text->changes->mc_orig_end = buf->text->changes->mc_new_end = end;
2056 buf->text->changes->mc_begin_signaled = 0;
2057 count = specpdl_depth ();
2058 XSETBUFFER (buffer, buf);
2059 record_unwind_protect (multiple_change_finish_up, buffer);
2061 buf->text->changes->in_multiple_change++;
2062 /* We don't call before-change-functions until signal_before_change()
2063 is called, in case there is a read-only or other error. */
2068 end_multiple_change (struct buffer *buf, int count)
2070 assert (buf->text->changes->in_multiple_change > 0);
2071 buf->text->changes->in_multiple_change--;
2072 if (!buf->text->changes->in_multiple_change)
2073 unbind_to (count, Qnil);
2076 static int inside_change_hook;
2079 change_function_restore (Lisp_Object buffer)
2081 /* We should first reset the variable and then change the buffer,
2082 because Fset_buffer() can throw. */
2083 inside_change_hook = 0;
2084 if (XBUFFER (buffer) != current_buffer)
2085 Fset_buffer (buffer);
2089 static int in_first_change;
2092 first_change_hook_restore (Lisp_Object buffer)
2094 in_first_change = 0;
2095 Fset_buffer (buffer);
2099 /* Signal an initial modification to the buffer. */
2102 signal_first_change (struct buffer *buf)
2104 /* This function can GC */
2106 XSETBUFFER (buffer, current_buffer);
2108 if (!in_first_change)
2110 if (!NILP (symbol_value_in_buffer (Qfirst_change_hook, buffer)))
2112 int speccount = specpdl_depth ();
2113 record_unwind_protect (first_change_hook_restore, buffer);
2114 set_buffer_internal (buf);
2115 in_first_change = 1;
2116 run_hook (Qfirst_change_hook);
2117 unbind_to (speccount, Qnil);
2122 /* Signal a change to the buffer immediately before it happens.
2123 START and END are the bounds of the text to be changed. */
2126 signal_before_change (struct buffer *buf, Bufpos start, Bufpos end)
2128 /* This function can GC */
2129 struct buffer *mbuf;
2130 Lisp_Object bufcons;
2132 if (!inside_change_hook)
2137 /* Are we in a multiple-change session? */
2138 if (buf->text->changes->in_multiple_change &&
2139 buf->text->changes->mc_begin != 0)
2141 /* If we're violating the constraints of the session,
2142 call the after-change-functions as necessary for the
2143 changes already made and treat further changes as
2145 if (start < buf->text->changes->mc_begin ||
2146 end > buf->text->changes->mc_new_end)
2147 cancel_multiple_change (buf);
2148 /* Do nothing if this is not the first change in the session. */
2149 else if (buf->text->changes->mc_begin_signaled)
2153 /* First time through; call the before-change-functions
2154 specifying the entire region to be changed. (Note that
2155 we didn't call before-change-functions in
2156 begin_multiple_change() because the buffer might be
2158 start = buf->text->changes->mc_begin;
2159 end = buf->text->changes->mc_new_end;
2163 /* If buffer is unmodified, run a special hook for that case. */
2164 if (BUF_SAVE_MODIFF (buf) >= BUF_MODIFF (buf))
2166 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2168 signal_first_change (mbuf);
2172 /* Now in any case run the before-change-functions if any. */
2173 speccount = specpdl_depth ();
2174 record_unwind_protect (change_function_restore, Fcurrent_buffer ());
2175 inside_change_hook = 1;
2177 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2179 XSETBUFFER (buffer, mbuf);
2180 if (!NILP (symbol_value_in_buffer (Qbefore_change_functions, buffer))
2181 /* Obsolete, for compatibility */
2182 || !NILP (symbol_value_in_buffer (Qbefore_change_function, buffer)))
2184 set_buffer_internal (buf);
2185 va_run_hook_with_args (Qbefore_change_functions, 2,
2186 make_int (start), make_int (end));
2187 /* Obsolete, for compatibility */
2188 va_run_hook_with_args (Qbefore_change_function, 2,
2189 make_int (start), make_int (end));
2193 /* Make sure endpoints remain valid. before-change-functions
2194 might have modified the buffer. */
2195 if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
2196 if (start > BUF_ZV (buf)) start = BUF_ZV (buf);
2197 if (end < BUF_BEGV (buf)) end = BUF_BEGV (buf);
2198 if (end > BUF_ZV (buf)) end = BUF_ZV (buf);
2200 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2202 XSETBUFFER (buffer, mbuf);
2203 report_extent_modification (buffer, start, end, 0);
2205 unbind_to (speccount, Qnil);
2207 /* Only now do we indicate that the before-change-functions have
2208 been called, in case some function throws out. */
2209 buf->text->changes->mc_begin_signaled = 1;
2213 /* Signal a change immediately after it happens.
2214 START is the bufpos of the start of the changed text.
2215 ORIG_END is the bufpos of the end of the before-changed text.
2216 NEW_END is the bufpos of the end of the after-changed text.
2220 signal_after_change (struct buffer *buf, Bufpos start, Bufpos orig_end,
2223 /* This function can GC */
2224 struct buffer *mbuf;
2225 Lisp_Object bufcons;
2227 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2229 /* always do this. */
2230 buffer_signal_changed_region (mbuf, start, new_end);
2232 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2234 /* #### This seems inefficient. Wouldn't it be better to just
2235 keep one cache per base buffer? */
2236 font_lock_maybe_update_syntactic_caches (mbuf, start, orig_end, new_end);
2239 if (!inside_change_hook)
2244 if (buf->text->changes->in_multiple_change &&
2245 buf->text->changes->mc_begin != 0)
2247 assert (start >= buf->text->changes->mc_begin &&
2248 start <= buf->text->changes->mc_new_end);
2249 assert (orig_end >= buf->text->changes->mc_begin &&
2250 orig_end <= buf->text->changes->mc_new_end);
2251 buf->text->changes->mc_new_end += new_end - orig_end;
2252 return; /* after-change-functions signalled when all changes done */
2255 speccount = specpdl_depth ();
2256 record_unwind_protect (change_function_restore, Fcurrent_buffer ());
2257 inside_change_hook = 1;
2258 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2260 XSETBUFFER (buffer, mbuf);
2262 if (!NILP (symbol_value_in_buffer (Qafter_change_functions, buffer))
2263 /* Obsolete, for compatibility */
2264 || !NILP (symbol_value_in_buffer (Qafter_change_function, buffer)))
2266 set_buffer_internal (buf);
2267 /* The actual after-change functions take slightly
2268 different arguments than what we were passed. */
2269 va_run_hook_with_args (Qafter_change_functions, 3,
2270 make_int (start), make_int (new_end),
2271 make_int (orig_end - start));
2272 /* Obsolete, for compatibility */
2273 va_run_hook_with_args (Qafter_change_function, 3,
2274 make_int (start), make_int (new_end),
2275 make_int (orig_end - start));
2279 /* Make sure endpoints remain valid. after-change-functions
2280 might have modified the buffer. */
2281 if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
2282 if (start > BUF_ZV (buf)) start = BUF_ZV (buf);
2283 if (new_end < BUF_BEGV (buf)) new_end = BUF_BEGV (buf);
2284 if (new_end > BUF_ZV (buf)) new_end = BUF_ZV (buf);
2285 if (orig_end < BUF_BEGV (buf)) orig_end = BUF_BEGV (buf);
2286 if (orig_end > BUF_ZV (buf)) orig_end = BUF_ZV (buf);
2288 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2290 XSETBUFFER (buffer, mbuf);
2291 report_extent_modification (buffer, start, new_end, 1);
2293 unbind_to (speccount, Qnil); /* sets inside_change_hook back to 0 */
2297 /* Call this if you're about to change the region of BUFFER from START
2298 to END. This checks the read-only properties of the region, calls
2299 the necessary modification hooks, and warns the next redisplay that
2300 it should pay attention to that area. */
2303 prepare_to_modify_buffer (struct buffer *buf, Bufpos start, Bufpos end,
2306 /* This function can GC */
2307 /* dmoore - This function can also kill the buffer buf, the current
2308 buffer, and do anything it pleases. So if you call it, be
2310 struct buffer *mbuf;
2311 Lisp_Object buffer, bufcons;
2312 struct gcpro gcpro1;
2314 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2316 barf_if_buffer_read_only (mbuf, start, end);
2319 /* if this is the first modification, see about locking the buffer's
2321 XSETBUFFER (buffer, buf);
2323 if (!NILP (buf->filename) && lockit &&
2324 BUF_SAVE_MODIFF (buf) >= BUF_MODIFF (buf))
2326 #ifdef CLASH_DETECTION
2327 if (!NILP (buf->file_truename))
2328 /* Make binding buffer-file-name to nil effective. */
2329 lock_file (buf->file_truename);
2331 /* At least warn if this file has changed on disk since it was visited.*/
2332 if (NILP (Fverify_visited_file_modtime (buffer))
2333 && !NILP (Ffile_exists_p (buf->filename)))
2334 call1_in_buffer (buf, intern ("ask-user-about-supersession-threat"),
2336 #endif /* not CLASH_DETECTION */
2340 /* #### dmoore - is this reasonable in case of buf being killed above? */
2341 if (!BUFFER_LIVE_P (buf))
2344 signal_before_change (buf, start, end);
2346 #ifdef REGION_CACHE_NEEDS_WORK
2347 if (buf->newline_cache)
2348 invalidate_region_cache (buf,
2350 start - BUF_BEG (buf), BUF_Z (buf) - end);
2351 if (buf->width_run_cache)
2352 invalidate_region_cache (buf,
2353 buf->width_run_cache,
2354 start - BUF_BEG (buf), BUF_Z (buf) - end);
2358 Vdeactivate_mark = Qt;
2361 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2363 mbuf->point_before_scroll = Qnil;
2368 /************************************************************************/
2369 /* Insertion of strings */
2370 /************************************************************************/
2373 fixup_internal_substring (const Bufbyte *nonreloc, Lisp_Object reloc,
2374 Bytecount offset, Bytecount *len)
2376 assert ((nonreloc && NILP (reloc)) || (!nonreloc && STRINGP (reloc)));
2381 *len = strlen ((const char *) nonreloc) - offset;
2383 *len = XSTRING_LENGTH (reloc) - offset;
2385 #ifdef ERROR_CHECK_BUFPOS
2387 if (STRINGP (reloc))
2389 assert (offset >= 0 && offset <= XSTRING_LENGTH (reloc));
2390 assert (offset + *len <= XSTRING_LENGTH (reloc));
2395 /* Insert a string into BUF at Bufpos POS. The string data comes
2396 from one of two sources: constant, non-relocatable data (specified
2397 in NONRELOC), or a Lisp string object (specified in RELOC), which
2398 is relocatable and may have extent data that needs to be copied
2399 into the buffer. OFFSET and LENGTH specify the substring of the
2400 data that is actually to be inserted. As a special case, if POS
2401 is -1, insert the string at point and move point to the end of the
2404 Normally, markers at the insertion point end up before the
2405 inserted string. If INSDEL_BEFORE_MARKERS is set in flags, however,
2406 they end up after the string.
2408 INSDEL_NO_LOCKING is kludgy and is used when insert-file-contents is
2409 visiting a new file; it inhibits the locking checks normally done
2410 before modifying a buffer. Similar checks were already done
2411 in the higher-level Lisp functions calling insert-file-contents. */
2414 buffer_insert_string_1 (struct buffer *buf, Bufpos pos,
2415 const Bufbyte *nonreloc, Lisp_Object reloc,
2416 Bytecount offset, Bytecount length,
2419 /* This function can GC */
2420 struct gcpro gcpro1;
2424 struct buffer *mbuf;
2425 Lisp_Object bufcons;
2427 /* Defensive steps just in case a buffer gets deleted and a calling
2428 function doesn't notice it. */
2429 if (!BUFFER_LIVE_P (buf))
2432 fixup_internal_substring (nonreloc, reloc, offset, &length);
2441 /* #### See the comment in print_internal(). If this buffer is marked
2442 as translatable, then Fgettext() should be called on obj if it
2446 /* Make sure that point-max won't exceed the size of an emacs int. */
2447 if ((length + BUF_Z (buf)) > EMACS_INT_MAX)
2448 error ("Maximum buffer size exceeded");
2450 /* theoretically not necessary -- caller should GCPRO.
2451 #### buffer_insert_from_buffer_1() doesn't! */
2454 prepare_to_modify_buffer (buf, pos, pos, !(flags & INSDEL_NO_LOCKING));
2456 /* Defensive steps in case the before-change-functions fuck around */
2457 if (!BUFFER_LIVE_P (buf))
2460 /* Bad bad pre-change function. */
2464 /* Make args be valid again. prepare_to_modify_buffer() might have
2465 modified the buffer. */
2466 if (pos < BUF_BEGV (buf))
2467 pos = BUF_BEGV (buf);
2468 if (pos > BUF_ZV (buf))
2471 /* string may have been relocated up to this point */
2472 if (STRINGP (reloc))
2473 nonreloc = XSTRING_DATA (reloc);
2475 ind = bufpos_to_bytind (buf, pos);
2476 cclen = bytecount_to_charcount (nonreloc + offset, length);
2478 if (ind != BI_BUF_GPT (buf))
2479 /* #### if debug-on-quit is invoked and the user changes the
2480 buffer, bad things can happen. This is a rampant problem
2482 move_gap (buf, ind); /* may QUIT */
2483 if (! GAP_CAN_HOLD_SIZE_P (buf, length))
2485 if (BUF_END_GAP_SIZE (buf) >= length)
2486 merge_gap_with_end_gap (buf);
2488 make_gap (buf, length - BUF_GAP_SIZE (buf));
2491 insert_invalidate_line_number_cache (buf, pos, nonreloc + offset, length);
2493 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2495 record_insert (mbuf, pos, cclen);
2499 MARK_BUFFERS_CHANGED;
2501 /* string may have been relocated up to this point */
2502 if (STRINGP (reloc))
2503 nonreloc = XSTRING_DATA (reloc);
2505 memcpy (BUF_GPT_ADDR (buf), nonreloc + offset, length);
2507 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) - length);
2508 SET_BI_BUF_GPT (buf, BI_BUF_GPT (buf) + length);
2509 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2511 SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) + cclen, BI_BUF_ZV (mbuf) + length);
2513 SET_BOTH_BUF_Z (buf, BUF_Z (buf) + cclen, BI_BUF_Z (buf) + length);
2514 SET_GAP_SENTINEL (buf);
2517 buffer_mule_signal_inserted_region (buf, pos, length, cclen);
2520 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2522 process_extents_for_insertion (make_buffer (mbuf), ind, length);
2525 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2527 /* We know the gap is at IND so the cast is OK. */
2528 adjust_markers_for_insert (mbuf, (Memind) ind, length);
2531 /* Point logically doesn't move, but may need to be adjusted because
2532 it's a byte index. point-marker doesn't change because it's a
2534 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2536 if (BI_BUF_PT (mbuf) > ind)
2537 JUST_SET_POINT (mbuf, BUF_PT (mbuf) + cclen,
2538 BI_BUF_PT (mbuf) + length);
2541 /* Well, point might move. */
2543 BI_BUF_SET_PT (buf, ind + length);
2545 if (STRINGP (reloc))
2547 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2549 splice_in_string_extents (reloc, mbuf, ind, length, offset);
2553 if (flags & INSDEL_BEFORE_MARKERS)
2555 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2557 /* ind - 1 is correct because the FROM argument is exclusive.
2558 I formerly used DEC_BYTIND() but that caused problems at the
2559 beginning of the buffer. */
2560 adjust_markers (mbuf, ind - 1, ind, length);
2564 signal_after_change (buf, pos, pos, pos + cclen);
2572 /* The following functions are interfaces onto the above function,
2573 for inserting particular sorts of data. In all the functions,
2574 BUF and POS specify the buffer and location where the insertion is
2575 to take place. (If POS is -1, text is inserted at point and point
2576 moves forward past the text.) FLAGS is as above. */
2579 buffer_insert_raw_string_1 (struct buffer *buf, Bufpos pos,
2580 const Bufbyte *nonreloc, Bytecount length,
2583 /* This function can GC */
2584 return buffer_insert_string_1 (buf, pos, nonreloc, Qnil, 0, length,
2589 buffer_insert_lisp_string_1 (struct buffer *buf, Bufpos pos, Lisp_Object str,
2592 /* This function can GC */
2593 #ifdef ERROR_CHECK_TYPECHECK
2594 assert (STRINGP (str));
2596 return buffer_insert_string_1 (buf, pos, 0, str, 0,
2597 XSTRING_LENGTH (str),
2601 /* Insert the null-terminated string S (in external format). */
2604 buffer_insert_c_string_1 (struct buffer *buf, Bufpos pos, const char *s,
2607 /* This function can GC */
2608 const char *translated = GETTEXT (s);
2609 return buffer_insert_string_1 (buf, pos, (const Bufbyte *) translated, Qnil,
2610 0, strlen (translated), flags);
2614 buffer_insert_emacs_char_1 (struct buffer *buf, Bufpos pos, Emchar ch,
2617 /* This function can GC */
2618 Bufbyte str[MAX_EMCHAR_LEN];
2619 Bytecount len = set_charptr_emchar (str, ch);
2620 return buffer_insert_string_1 (buf, pos, str, Qnil, 0, len, flags);
2624 buffer_insert_c_char_1 (struct buffer *buf, Bufpos pos, char c,
2627 /* This function can GC */
2628 return buffer_insert_emacs_char_1 (buf, pos, (Emchar) (unsigned char) c,
2633 buffer_insert_from_buffer_1 (struct buffer *buf, Bufpos pos,
2634 struct buffer *buf2, Bufpos pos2,
2635 Charcount length, int flags)
2637 /* This function can GC */
2638 Lisp_Object str = make_string_from_buffer (buf2, pos2, length);
2639 return buffer_insert_string_1 (buf, pos, 0, str, 0,
2640 XSTRING_LENGTH (str), flags);
2644 /************************************************************************/
2645 /* Deletion of ranges */
2646 /************************************************************************/
2648 /* Delete characters in buffer from FROM up to (but not including) TO. */
2651 buffer_delete_range (struct buffer *buf, Bufpos from, Bufpos to, int flags)
2653 /* This function can GC */
2655 Bytind bi_from, bi_to;
2656 Bytecount bc_numdel;
2658 struct buffer *mbuf;
2659 Lisp_Object bufcons;
2661 /* Defensive steps just in case a buffer gets deleted and a calling
2662 function doesn't notice it. */
2663 if (!BUFFER_LIVE_P (buf))
2666 /* Make args be valid */
2667 if (from < BUF_BEGV (buf))
2668 from = BUF_BEGV (buf);
2669 if (to > BUF_ZV (buf))
2671 if ((numdel = to - from) <= 0)
2674 prepare_to_modify_buffer (buf, from, to, !(flags & INSDEL_NO_LOCKING));
2676 /* Defensive steps in case the before-change-functions fuck around */
2677 if (!BUFFER_LIVE_P (buf))
2678 /* Bad bad pre-change function. */
2681 /* Make args be valid again. prepare_to_modify_buffer() might have
2682 modified the buffer. */
2683 if (from < BUF_BEGV (buf))
2684 from = BUF_BEGV (buf);
2685 if (to > BUF_ZV (buf))
2687 if ((numdel = to - from) <= 0)
2690 /* Redisplay needs to know if a newline was in the deleted region.
2691 If we've already marked the changed region as having a deleted
2692 newline there is no use in performing the check. */
2693 if (!buf->changes->newline_was_deleted)
2695 scan_buffer (buf, '\n', from, to, 1, &shortage, 1);
2698 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2700 mbuf->changes->newline_was_deleted = 1;
2705 bi_from = bufpos_to_bytind (buf, from);
2706 bi_to = bufpos_to_bytind (buf, to);
2707 bc_numdel = bi_to - bi_from;
2709 delete_invalidate_line_number_cache (buf, from, to);
2711 if (to == BUF_Z (buf) &&
2712 bi_from > BI_BUF_GPT (buf))
2714 /* avoid moving the gap just to delete from the bottom. */
2716 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2718 record_delete (mbuf, from, numdel);
2721 MARK_BUFFERS_CHANGED;
2723 /* #### Point used to be modified here, but this causes problems
2724 with MULE, as point is used to calculate bytinds, and if the
2725 offset in bc_numdel causes point to move to a non first-byte
2726 location, causing some other function to throw an assertion
2727 in ASSERT_VALID_BYTIND. I've moved the code to right after
2728 the other movements and adjustments, but before the gap is
2729 moved. -- jh 970813 */
2731 /* Detach any extents that are completely within the range [FROM, TO],
2732 if the extents are detachable.
2734 This must come AFTER record_delete(), so that the appropriate
2735 extents will be present to be recorded, and BEFORE the gap
2736 size is increased, as otherwise we will be confused about
2737 where the extents end. */
2738 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2740 process_extents_for_deletion (make_buffer (mbuf), bi_from, bi_to, 0);
2743 /* Relocate all markers pointing into the new, larger gap to
2744 point at the end of the text before the gap. */
2745 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2747 adjust_markers (mbuf,
2748 (bi_to + BUF_GAP_SIZE (mbuf)),
2749 (bi_to + BUF_GAP_SIZE (mbuf)),
2753 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2755 /* Relocate any extent endpoints just like markers. */
2756 adjust_extents_for_deletion (make_buffer (mbuf), bi_from, bi_to,
2757 BUF_GAP_SIZE (mbuf), bc_numdel, 0);
2760 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2762 /* Relocate point as if it were a marker. */
2763 if (bi_from < BI_BUF_PT (mbuf))
2765 if (BI_BUF_PT (mbuf) < bi_to)
2766 JUST_SET_POINT (mbuf, from, bi_from);
2768 JUST_SET_POINT (mbuf, BUF_PT (mbuf) - numdel,
2769 BI_BUF_PT (mbuf) - bc_numdel);
2773 SET_BUF_END_GAP_SIZE (buf, BUF_END_GAP_SIZE (buf) + bc_numdel);
2775 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2777 SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) - numdel,
2778 BI_BUF_ZV (mbuf) - bc_numdel);
2780 SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BI_BUF_Z (buf) - bc_numdel);
2781 SET_GAP_SENTINEL (buf);
2785 /* Make sure the gap is somewhere in or next to what we are deleting. */
2786 if (bi_to < BI_BUF_GPT (buf))
2787 gap_left (buf, bi_to);
2788 if (bi_from > BI_BUF_GPT (buf))
2789 gap_right (buf, bi_from);
2791 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2793 record_delete (mbuf, from, numdel);
2796 MARK_BUFFERS_CHANGED;
2798 /* #### Point used to be modified here, but this causes problems
2799 with MULE, as point is used to calculate bytinds, and if the
2800 offset in bc_numdel causes point to move to a non first-byte
2801 location, causing some other function to throw an assertion
2802 in ASSERT_VALID_BYTIND. I've moved the code to right after
2803 the other movements and adjustments, but before the gap is
2804 moved. -- jh 970813 */
2806 /* Detach any extents that are completely within the range [FROM, TO],
2807 if the extents are detachable.
2809 This must come AFTER record_delete(), so that the appropriate extents
2810 will be present to be recorded, and BEFORE the gap size is increased,
2811 as otherwise we will be confused about where the extents end. */
2812 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2814 process_extents_for_deletion (make_buffer (mbuf), bi_from, bi_to, 0);
2817 /* Relocate all markers pointing into the new, larger gap to
2818 point at the end of the text before the gap. */
2819 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2821 adjust_markers (mbuf,
2822 (bi_to + BUF_GAP_SIZE (mbuf)),
2823 (bi_to + BUF_GAP_SIZE (mbuf)),
2824 (- bc_numdel - BUF_GAP_SIZE (mbuf)));
2827 /* Relocate any extent endpoints just like markers. */
2828 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2830 adjust_extents_for_deletion (make_buffer (mbuf), bi_from, bi_to,
2831 BUF_GAP_SIZE (mbuf),
2832 bc_numdel, BUF_GAP_SIZE (mbuf));
2835 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2837 /* Relocate point as if it were a marker. */
2838 if (bi_from < BI_BUF_PT (mbuf))
2840 if (BI_BUF_PT (mbuf) < bi_to)
2841 JUST_SET_POINT (mbuf, from, bi_from);
2843 JUST_SET_POINT (mbuf, BUF_PT (mbuf) - numdel,
2844 BI_BUF_PT (mbuf) - bc_numdel);
2848 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + bc_numdel);
2849 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2851 SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) - numdel,
2852 BI_BUF_ZV (mbuf) - bc_numdel);
2854 SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BI_BUF_Z (buf) - bc_numdel);
2855 SET_BI_BUF_GPT (buf, bi_from);
2856 SET_GAP_SENTINEL (buf);
2860 buffer_mule_signal_deleted_region (buf, from, to, bi_from, bi_to);
2863 #ifdef ERROR_CHECK_EXTENTS
2864 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2866 sledgehammer_extent_check (make_buffer (mbuf));
2870 signal_after_change (buf, from, to, from);
2874 /************************************************************************/
2875 /* Replacement of characters */
2876 /************************************************************************/
2878 /* Replace the character at POS in buffer B with CH. */
2881 buffer_replace_char (struct buffer *buf, Bufpos pos, Emchar ch,
2882 int not_real_change, int force_lock_check)
2884 /* This function can GC */
2885 Bufbyte curstr[MAX_EMCHAR_LEN];
2886 Bufbyte newstr[MAX_EMCHAR_LEN];
2887 Bytecount curlen, newlen;
2889 /* Defensive steps just in case a buffer gets deleted and a calling
2890 function doesn't notice it. */
2891 if (!BUFFER_LIVE_P (buf))
2894 curlen = BUF_CHARPTR_COPY_CHAR (buf, pos, curstr);
2895 newlen = set_charptr_emchar (newstr, ch);
2897 if (curlen == newlen)
2899 struct buffer *mbuf;
2900 Lisp_Object bufcons;
2902 /* then we can just replace the text. */
2903 prepare_to_modify_buffer (buf, pos, pos + 1,
2904 !not_real_change || force_lock_check);
2905 /* Defensive steps in case the before-change-functions fuck around */
2906 if (!BUFFER_LIVE_P (buf))
2907 /* Bad bad pre-change function. */
2910 /* Make args be valid again. prepare_to_modify_buffer() might have
2911 modified the buffer. */
2912 if (pos < BUF_BEGV (buf))
2913 pos = BUF_BEGV (buf);
2914 if (pos >= BUF_ZV (buf))
2915 pos = BUF_ZV (buf) - 1;
2916 if (pos < BUF_BEGV (buf))
2917 /* no more characters in buffer! */
2920 if (BUF_FETCH_CHAR (buf, pos) == '\n')
2922 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2924 mbuf->changes->newline_was_deleted = 1;
2927 MARK_BUFFERS_CHANGED;
2928 if (!not_real_change)
2930 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2932 record_change (mbuf, pos, 1);
2936 memcpy (BUF_BYTE_ADDRESS (buf, pos), newstr, newlen);
2938 signal_after_change (buf, pos, pos + 1, pos + 1);
2940 /* We do not have to adjust the Mule data; we just replaced a
2941 character with another of the same number of bytes. */
2946 * Must implement as deletion followed by insertion.
2948 * Make a note to move point forward later in the one situation
2949 * where it is needed, a delete/insert one position behind
2950 * point. Point will drift backward by one position and stay
2953 int movepoint = (pos == BUF_PT (buf) - 1);
2955 buffer_delete_range (buf, pos, pos + 1, 0);
2956 /* Defensive steps in case the before-change-functions fuck around */
2957 if (!BUFFER_LIVE_P (buf))
2958 /* Bad bad pre-change function. */
2961 /* Make args be valid again. prepare_to_modify_buffer() might have
2962 modified the buffer. */
2963 if (pos < BUF_BEGV (buf))
2964 pos = BUF_BEGV (buf);
2965 if (pos >= BUF_ZV (buf))
2966 pos = BUF_ZV (buf) - 1;
2967 if (pos < BUF_BEGV (buf))
2968 /* no more characters in buffer! */
2971 * -1 as the pos argument means to move point forward with the
2972 * insertion, which we must do if the deletion moved point
2973 * backward so that it now equals the insertion point.
2975 buffer_insert_string_1 (buf, (movepoint ? -1 : pos),
2976 newstr, Qnil, 0, newlen, 0);
2981 /************************************************************************/
2982 /* Other functions */
2983 /************************************************************************/
2985 /* Make a string from a buffer. This needs to take into account the gap,
2986 and add any necessary extents from the buffer. */
2989 make_string_from_buffer_1 (struct buffer *buf, Bufpos pos, Charcount length,
2992 /* This function can GC */
2993 Bytind bi_ind = bufpos_to_bytind (buf, pos);
2994 Bytecount bi_len = bufpos_to_bytind (buf, pos + length) - bi_ind;
2995 Lisp_Object val = make_uninit_string (bi_len);
2997 struct gcpro gcpro1;
3001 add_string_extents (val, buf, bi_ind, bi_len);
3004 Bytecount len1 = BI_BUF_GPT (buf) - bi_ind;
3005 Bufbyte *start1 = BI_BUF_BYTE_ADDRESS (buf, bi_ind);
3006 Bufbyte *dest = XSTRING_DATA (val);
3010 /* Completely after gap */
3011 memcpy (dest, start1, bi_len);
3013 else if (bi_len <= len1)
3015 /* Completely before gap */
3016 memcpy (dest, start1, bi_len);
3021 Bytind pos2 = bi_ind + len1;
3022 Bufbyte *start2 = BI_BUF_BYTE_ADDRESS (buf, pos2);
3024 memcpy (dest, start1, len1);
3025 memcpy (dest + len1, start2, bi_len - len1);
3034 make_string_from_buffer (struct buffer *buf, Bufpos pos, Charcount length)
3036 return make_string_from_buffer_1 (buf, pos, length, 0);
3040 make_string_from_buffer_no_extents (struct buffer *buf, Bufpos pos,
3043 return make_string_from_buffer_1 (buf, pos, length, 1);
3047 barf_if_buffer_read_only (struct buffer *buf, Bufpos from, Bufpos to)
3052 XSETBUFFER (buffer, buf);
3054 iro = (buf == current_buffer ? Vinhibit_read_only :
3055 symbol_value_in_buffer (Qinhibit_read_only, buffer));
3058 if (NILP (iro) && !NILP (buf->read_only))
3060 Fsignal (Qbuffer_read_only, (list1 (buffer)));
3067 verify_extent_modification (buffer,
3068 bufpos_to_bytind (buf, from),
3069 bufpos_to_bytind (buf, to),
3075 find_charsets_in_bufbyte_string (unsigned char *charsets, const Bufbyte *str,
3079 /* Telescope this. */
3082 const Bufbyte *strend = str + len;
3083 memset (charsets, 0, NUM_LEADING_BYTES);
3085 while (str < strend)
3087 charsets[CHAR_LEADING_BYTE (charptr_emchar (str)) - 128] = 1;
3094 find_charsets_in_emchar_string (unsigned char *charsets, const Emchar *str,
3098 /* Telescope this. */
3103 memset (charsets, 0, NUM_LEADING_BYTES);
3104 for (i = 0; i < len; i++)
3106 charsets[CHAR_LEADING_BYTE (str[i]) - 128] = 1;
3112 bufbyte_string_displayed_columns (const Bufbyte *str, Bytecount len)
3115 const Bufbyte *end = str + len;
3120 Emchar ch = charptr_emchar (str);
3121 cols += XCHARSET_COLUMNS (CHAR_CHARSET (ch));
3132 emchar_string_displayed_columns (const Emchar *str, Charcount len)
3138 for (i = 0; i < len; i++)
3139 cols += XCHARSET_COLUMNS (CHAR_CHARSET (str[i]));
3142 #else /* not MULE */
3147 /* NOTE: Does not reset the Dynarr. */
3150 convert_bufbyte_string_into_emchar_dynarr (const Bufbyte *str, Bytecount len,
3153 const Bufbyte *strend = str + len;
3155 while (str < strend)
3157 Emchar ch = charptr_emchar (str);
3158 Dynarr_add (dyn, ch);
3164 convert_bufbyte_string_into_emchar_string (const Bufbyte *str, Bytecount len,
3167 const Bufbyte *strend = str + len;
3168 Charcount newlen = 0;
3169 while (str < strend)
3171 Emchar ch = charptr_emchar (str);
3178 /* Convert an array of Emchars into the equivalent string representation.
3179 Store into the given Bufbyte dynarr. Does not reset the dynarr.
3180 Does not add a terminating zero. */
3183 convert_emchar_string_into_bufbyte_dynarr (Emchar *arr, int nels,
3184 Bufbyte_dynarr *dyn)
3186 Bufbyte str[MAX_EMCHAR_LEN];
3189 for (i = 0; i < nels; i++)
3191 Bytecount len = set_charptr_emchar (str, arr[i]);
3192 Dynarr_add_many (dyn, str, len);
3196 /* Convert an array of Emchars into the equivalent string representation.
3197 Malloc the space needed for this and return it. If LEN_OUT is not a
3198 NULL pointer, store into LEN_OUT the number of Bufbytes in the
3199 malloc()ed string. Note that the actual number of Bufbytes allocated
3200 is one more than this: the returned string is zero-terminated. */
3203 convert_emchar_string_into_malloced_string (Emchar *arr, int nels,
3206 /* Damn zero-termination. */
3207 Bufbyte *str = (Bufbyte *) alloca (nels * MAX_EMCHAR_LEN + 1);
3208 Bufbyte *strorig = str;
3213 for (i = 0; i < nels; i++)
3214 str += set_charptr_emchar (str, arr[i]);
3216 len = str - strorig;
3217 str = (Bufbyte *) xmalloc (1 + len);
3218 memcpy (str, strorig, 1 + len);
3225 /************************************************************************/
3226 /* initialization */
3227 /************************************************************************/
3230 reinit_vars_of_insdel (void)
3234 inside_change_hook = 0;
3235 in_first_change = 0;
3237 for (i = 0; i <= MAX_BYTIND_GAP_SIZE_3; i++)
3238 three_to_one_table[i] = i / 3;
3242 vars_of_insdel (void)
3244 reinit_vars_of_insdel ();
3248 init_buffer_text (struct buffer *b)
3250 if (!b->base_buffer)
3252 SET_BUF_GAP_SIZE (b, 20);
3253 BUFFER_ALLOC (b->text->beg, BUF_GAP_SIZE (b) + BUF_END_SENTINEL_SIZE);
3254 if (! BUF_BEG_ADDR (b))
3257 SET_BUF_END_GAP_SIZE (b, 0);
3258 SET_BI_BUF_GPT (b, 1);
3259 SET_BOTH_BUF_Z (b, 1, 1);
3260 SET_GAP_SENTINEL (b);
3261 SET_END_SENTINEL (b);
3266 b->text->mule_bufmin = b->text->mule_bufmax = 1;
3267 b->text->mule_bytmin = b->text->mule_bytmax = 1;
3268 b->text->mule_shifter = 0;
3269 b->text->mule_three_p = 0;
3271 for (i = 0; i < 16; i++)
3273 b->text->mule_bufpos_cache[i] = 1;
3274 b->text->mule_bytind_cache[i] = 1;
3278 b->text->line_number_cache = Qnil;
3281 BUF_SAVE_MODIFF (b) = 1;
3283 JUST_SET_POINT (b, 1, 1);
3284 SET_BOTH_BUF_BEGV (b, 1, 1);
3285 SET_BOTH_BUF_ZV (b, 1, 1);
3287 b->text->changes = xnew_and_zero (struct buffer_text_change_data);
3291 JUST_SET_POINT (b, BUF_PT (b->base_buffer), BI_BUF_PT (b->base_buffer));
3292 SET_BOTH_BUF_BEGV (b, BUF_BEGV (b->base_buffer),
3293 BI_BUF_BEGV (b->base_buffer));
3294 SET_BOTH_BUF_ZV (b, BUF_ZV (b->base_buffer),
3295 BI_BUF_ZV (b->base_buffer));
3298 b->changes = xnew_and_zero (struct each_buffer_change_data);
3299 BUF_FACECHANGE (b) = 1;
3301 #ifdef REGION_CACHE_NEEDS_WORK
3302 b->newline_cache = 0;
3303 b->width_run_cache = 0;
3304 b->width_table = Qnil;
3309 uninit_buffer_text (struct buffer *b)
3311 if (!b->base_buffer)
3313 BUFFER_FREE (b->text->beg);
3314 xfree (b->text->changes);
3318 #ifdef REGION_CACHE_NEEDS_WORK
3319 if (b->newline_cache)
3321 free_region_cache (b->newline_cache);
3322 b->newline_cache = 0;
3324 if (b->width_run_cache)
3326 free_region_cache (b->width_run_cache);
3327 b->width_run_cache = 0;
3329 b->width_table = Qnil;