import -ko -b 1.1.3 XEmacs XEmacs-21_2 r21-2-35
[chise/xemacs-chise.git.1] / src / insdel.c
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.
5
6 This file is part of XEmacs.
7
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
11 later version.
12
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
16 for more details.
17
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.  */
22
23 /* Synched up with: Mule 2.0, FSF 19.30.  Diverges significantly. */
24
25 /* This file has been Mule-ized. */
26
27 /* Overhauled by Ben Wing, December 1994, for Mule implementation. */
28
29 /*
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.
33
34    As a "buffer position" (typedef Bufpos):
35
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.
42
43    As a "byte index" (typedef Bytind):
44
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
51       position.
52
53    As a "memory index" (typedef Memind):
54
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
64
65         buffer_start_address + memory_index(position) - 1
66
67       except in the case of characters at the gap position.
68
69    Other typedefs:
70    ===============
71
72       Emchar:
73       -------
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
80         expects a string.
81
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
86
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.
90
91         This means that Emchar values are upwardly compatible with
92         the standard 8-bit representation of ASCII/ISO-8859-1.
93
94       Bufbyte:
95       --------
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
101         format are
102
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.
108
109         This leads to a number of desirable properties:
110
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
116              searching routines.
117
118       array of char:
119       --------------
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.
133
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.
146
147         NOTE: filenames need to be in external format so that
148         ISO-8859-1 characters come out correctly.
149
150       Charcount:
151       ----------
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
157         a Charcount.
158
159       Bytecount:
160       ----------
161         Similar to a Charcount but represents a count of bytes.
162         The difference between two Bytind's is a Bytecount.
163
164
165    Usage of the various representations:
166    =====================================
167
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.
173
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.)
179
180    Byte indices are used in other code that needs to be fast,
181    such as the searching, redisplay, and extent-manipulation code.
182
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).
190
191    Similarly, Charcounts are used in all code that deals with strings
192    except for code that needs to be fast, which used Bytecounts.
193
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.
197
198    Working with the various representations:
199    ========================================= */
200
201 #include <config.h>
202 #include "lisp.h"
203
204 #include "buffer.h"
205 #include "device.h"
206 #include "frame.h"
207 #include "extents.h"
208 #include "insdel.h"
209 #include "lstream.h"
210 #include "redisplay.h"
211 #include "line-number.h"
212
213 /* We write things this way because it's very important the
214    MAX_BYTIND_GAP_SIZE_3 is a multiple of 3. (As it happens,
215    65535 is a multiple of 3, but this may not always be the
216    case.) */
217
218 #define MAX_BUFPOS_GAP_SIZE_3 (65535/3)
219 #define MAX_BYTIND_GAP_SIZE_3 (3 * MAX_BUFPOS_GAP_SIZE_3)
220
221 short three_to_one_table[1 + MAX_BYTIND_GAP_SIZE_3];
222
223 /* Various macros modelled along the lines of those in buffer.h.
224    Purposefully omitted from buffer.h because files other than this
225    one should not be using them. */
226
227 /* Address of beginning of buffer.  This is an lvalue because
228    BUFFER_ALLOC needs it to be. */
229 #define BUF_BEG_ADDR(buf) ((buf)->text->beg)
230
231 /* Set the address of beginning of buffer. */
232 #define SET_BUF_BEG_ADDR(buf, addr) do { (buf)->text->beg = (addr); } while (0)
233
234 /* Gap size.  */
235 #define BUF_GAP_SIZE(buf) ((buf)->text->gap_size + 0)
236 #define BUF_END_GAP_SIZE(buf) ((buf)->text->end_gap_size + 0)
237 /* Set gap size.  */
238 #define SET_BUF_GAP_SIZE(buf, value) \
239   do { (buf)->text->gap_size = (value); } while (0)
240 #define SET_BUF_END_GAP_SIZE(buf, value) \
241   do { (buf)->text->end_gap_size = (value); } while (0)
242
243 /* Gap location.  */
244 #define BI_BUF_GPT(buf) ((buf)->text->gpt + 0)
245 #define BUF_GPT_ADDR(buf) (BUF_BEG_ADDR (buf) + BI_BUF_GPT (buf) - 1)
246
247 /* Set gap location.  */
248 #define SET_BI_BUF_GPT(buf, value) do { (buf)->text->gpt = (value); } while (0)
249
250 /* Set end of buffer.  */
251 #define SET_BOTH_BUF_Z(buf, val, bival)         \
252 do                                              \
253 {                                               \
254   (buf)->text->z = (bival);                     \
255   (buf)->text->bufz = (val);                    \
256 } while (0)
257
258 /* Under Mule, we maintain two sentinels in the buffer: one at the
259    beginning of the gap, and one at the end of the buffer.  This
260    allows us to move forward, examining bytes looking for the
261    end of a character, and not worry about running off the end.
262    We do not need corresponding sentinels when moving backwards
263    because we do not have to look past the beginning of a character
264    to find the beginning of the character.
265
266    Every time we change the beginning of the gap, we have to
267    call SET_GAP_SENTINEL().
268
269    Every time we change the total size (characters plus gap)
270    of the buffer, we have to call SET_END_SENTINEL().
271  */
272
273
274 #ifdef MULE
275 # define GAP_CAN_HOLD_SIZE_P(buf, len) (BUF_GAP_SIZE (buf) >= (len) + 1)
276 # define SET_GAP_SENTINEL(buf) (*BUF_GPT_ADDR (buf) = 0)
277 # define BUF_END_SENTINEL_SIZE 1
278 # define SET_END_SENTINEL(buf) \
279   (*(BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + BI_BUF_Z (buf) - 1) = 0)
280 #else
281 # define GAP_CAN_HOLD_SIZE_P(buf, len) (BUF_GAP_SIZE (buf) >= (len))
282 # define SET_GAP_SENTINEL(buf)
283 # define BUF_END_SENTINEL_SIZE 0
284 # define SET_END_SENTINEL(buf)
285 #endif
286
287 \f
288 /************************************************************************/
289 /*                    Charcount/Bytecount conversion                    */
290 /************************************************************************/
291
292 /* Optimization.  Do it.  Live it.  Love it.  */
293
294 #ifdef MULE
295
296 /* We include the basic functions here that require no specific
297    knowledge of how data is Mule-encoded into a buffer other
298    than the basic (00 - 7F), (80 - 9F), (A0 - FF) scheme.
299    Anything that requires more specific knowledge goes into
300    mule-charset.c. */
301
302 /* Given a pointer to a text string and a length in bytes, return
303    the equivalent length in characters. */
304
305 Charcount
306 bytecount_to_charcount (const Bufbyte *ptr, Bytecount len)
307 {
308   Charcount count = 0;
309   const Bufbyte *end = ptr + len;
310
311 #if SIZEOF_LONG == 8
312 # define STRIDE_TYPE long
313 # define HIGH_BIT_MASK 0x8080808080808080UL
314 #elif SIZEOF_LONG_LONG == 8 && !(defined (i386) || defined (__i386__))
315 # define STRIDE_TYPE long long
316 # define HIGH_BIT_MASK 0x8080808080808080ULL
317 #elif SIZEOF_LONG == 4
318 # define STRIDE_TYPE long
319 # define HIGH_BIT_MASK 0x80808080UL
320 #else
321 # error Add support for 128-bit systems here
322 #endif
323
324 #define ALIGN_BITS ((EMACS_UINT) (ALIGNOF (STRIDE_TYPE) - 1))
325 #define ALIGN_MASK (~ ALIGN_BITS)
326 #define ALIGNED(ptr) ((((EMACS_UINT) ptr) & ALIGN_BITS) == 0)
327 #define STRIDE sizeof (STRIDE_TYPE)
328
329   while (ptr < end)
330     {
331       if (BYTE_ASCII_P (*ptr))
332         {
333           /* optimize for long stretches of ASCII */
334           if (! ALIGNED (ptr))
335             ptr++, count++;
336           else
337             {
338               const unsigned STRIDE_TYPE *ascii_end =
339                 (const unsigned STRIDE_TYPE *) ptr;
340               /* This loop screams, because we can typically
341                  detect ASCII characters 8 at a time. */
342               while ((const Bufbyte *) ascii_end + STRIDE <= end
343                      && !(*ascii_end & HIGH_BIT_MASK))
344                 ascii_end++;
345               if ((Bufbyte *) ascii_end == ptr)
346                 ptr++, count++;
347               else
348                 {
349                   count += (Bufbyte *) ascii_end - ptr;
350                   ptr = (Bufbyte *) ascii_end;
351                 }
352             }
353         }
354       else
355         {
356           /* optimize for successive characters from the same charset */
357           Bufbyte leading_byte = *ptr;
358           size_t bytes = REP_BYTES_BY_FIRST_BYTE (leading_byte);
359           while ((ptr < end) && (*ptr == leading_byte))
360             ptr += bytes, count++;
361         }
362     }
363
364 #ifdef ERROR_CHECK_BUFPOS
365   /* Bomb out if the specified substring ends in the middle
366      of a character.  Note that we might have already gotten
367      a core dump above from an invalid reference, but at least
368      we will get no farther than here. */
369   assert (ptr == end);
370 #endif
371
372   return count;
373 }
374
375 /* Given a pointer to a text string and a length in characters, return
376    the equivalent length in bytes. */
377
378 Bytecount
379 charcount_to_bytecount (const Bufbyte *ptr, Charcount len)
380 {
381   const Bufbyte *newptr = ptr;
382
383   while (len > 0)
384     {
385       INC_CHARPTR (newptr);
386       len--;
387     }
388   return newptr - ptr;
389 }
390
391 /* The next two functions are the actual meat behind the
392    bufpos-to-bytind and bytind-to-bufpos conversions.  Currently
393    the method they use is fairly unsophisticated; see buffer.h.
394
395    Note that bufpos_to_bytind_func() is probably the most-called
396    function in all of XEmacs.  Therefore, it must be FAST FAST FAST.
397    This is the reason why so much of the code is duplicated.
398
399    Similar considerations apply to bytind_to_bufpos_func(), although
400    less so because the function is not called so often.
401
402    #### At some point this should use a more sophisticated method;
403    see buffer.h. */
404
405 static int not_very_random_number;
406
407 Bytind
408 bufpos_to_bytind_func (struct buffer *buf, Bufpos x)
409 {
410   Bufpos bufmin;
411   Bufpos bufmax;
412   Bytind bytmin;
413   Bytind bytmax;
414   int size;
415   int forward_p;
416   Bytind retval;
417   int diff_so_far;
418   int add_to_cache = 0;
419
420   /* Check for some cached positions, for speed. */
421   if (x == BUF_PT (buf))
422     return BI_BUF_PT (buf);
423   if (x == BUF_ZV (buf))
424     return BI_BUF_ZV (buf);
425   if (x == BUF_BEGV (buf))
426     return BI_BUF_BEGV (buf);
427
428   bufmin = buf->text->mule_bufmin;
429   bufmax = buf->text->mule_bufmax;
430   bytmin = buf->text->mule_bytmin;
431   bytmax = buf->text->mule_bytmax;
432   size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p;
433
434   /* The basic idea here is that we shift the "known region" up or down
435      until it overlaps the specified position.  We do this by moving
436      the upper bound of the known region up one character at a time,
437      and moving the lower bound of the known region up as necessary
438      when the size of the character just seen changes.
439
440      We optimize this, however, by first shifting the known region to
441      one of the cached points if it's close by. (We don't check BEG or
442      Z, even though they're cached; most of the time these will be the
443      same as BEGV and ZV, and when they're not, they're not likely
444      to be used.) */
445
446   if (x > bufmax)
447     {
448       Bufpos diffmax = x - bufmax;
449       Bufpos diffpt = x - BUF_PT (buf);
450       Bufpos diffzv = BUF_ZV (buf) - x;
451       /* #### This value could stand some more exploration. */
452       Charcount heuristic_hack = (bufmax - bufmin) >> 2;
453
454       /* Check if the position is closer to PT or ZV than to the
455          end of the known region. */
456
457       if (diffpt < 0)
458         diffpt = -diffpt;
459       if (diffzv < 0)
460         diffzv = -diffzv;
461
462       /* But also implement a heuristic that favors the known region
463          over PT or ZV.  The reason for this is that switching to
464          PT or ZV will wipe out the knowledge in the known region,
465          which might be annoying if the known region is large and
466          PT or ZV is not that much closer than the end of the known
467          region. */
468
469       diffzv += heuristic_hack;
470       diffpt += heuristic_hack;
471       if (diffpt < diffmax && diffpt <= diffzv)
472         {
473           bufmax = bufmin = BUF_PT (buf);
474           bytmax = bytmin = BI_BUF_PT (buf);
475           /* We set the size to 1 even though it doesn't really
476              matter because the new known region contains no
477              characters.  We do this because this is the most
478              likely size of the characters around the new known
479              region, and we avoid potential yuckiness that is
480              done when size == 3. */
481           size = 1;
482         }
483       if (diffzv < diffmax)
484         {
485           bufmax = bufmin = BUF_ZV (buf);
486           bytmax = bytmin = BI_BUF_ZV (buf);
487           size = 1;
488         }
489     }
490 #ifdef ERROR_CHECK_BUFPOS
491   else if (x >= bufmin)
492     abort ();
493 #endif
494   else
495     {
496       Bufpos diffmin = bufmin - x;
497       Bufpos diffpt = BUF_PT (buf) - x;
498       Bufpos diffbegv = x - BUF_BEGV (buf);
499       /* #### This value could stand some more exploration. */
500       Charcount heuristic_hack = (bufmax - bufmin) >> 2;
501
502       if (diffpt < 0)
503         diffpt = -diffpt;
504       if (diffbegv < 0)
505         diffbegv = -diffbegv;
506
507       /* But also implement a heuristic that favors the known region --
508          see above. */
509
510       diffbegv += heuristic_hack;
511       diffpt += heuristic_hack;
512
513       if (diffpt < diffmin && diffpt <= diffbegv)
514         {
515           bufmax = bufmin = BUF_PT (buf);
516           bytmax = bytmin = BI_BUF_PT (buf);
517           /* We set the size to 1 even though it doesn't really
518              matter because the new known region contains no
519              characters.  We do this because this is the most
520              likely size of the characters around the new known
521              region, and we avoid potential yuckiness that is
522              done when size == 3. */
523           size = 1;
524         }
525       if (diffbegv < diffmin)
526         {
527           bufmax = bufmin = BUF_BEGV (buf);
528           bytmax = bytmin = BI_BUF_BEGV (buf);
529           size = 1;
530         }
531     }
532
533   diff_so_far = x > bufmax ? x - bufmax : bufmin - x;
534   if (diff_so_far > 50)
535     {
536       /* If we have to move more than a certain amount, then look
537          into our cache. */
538       int minval = INT_MAX;
539       int found = 0;
540       int i;
541
542       add_to_cache = 1;
543       /* I considered keeping the positions ordered.  This would speed
544          up this loop, but updating the cache would take longer, so
545          it doesn't seem like it would really matter. */
546       for (i = 0; i < 16; i++)
547         {
548           int diff = buf->text->mule_bufpos_cache[i] - x;
549
550           if (diff < 0)
551             diff = -diff;
552           if (diff < minval)
553             {
554               minval = diff;
555               found = i;
556             }
557         }
558
559       if (minval < diff_so_far)
560         {
561           bufmax = bufmin = buf->text->mule_bufpos_cache[found];
562           bytmax = bytmin = buf->text->mule_bytind_cache[found];
563           size = 1;
564         }
565     }
566
567   /* It's conceivable that the caching above could lead to X being
568      the same as one of the range edges. */
569   if (x >= bufmax)
570     {
571       Bytind newmax;
572       Bytecount newsize;
573
574       forward_p = 1;
575       while (x > bufmax)
576         {
577           newmax = bytmax;
578
579           INC_BYTIND (buf, newmax);
580           newsize = newmax - bytmax;
581           if (newsize != size)
582             {
583               bufmin = bufmax;
584               bytmin = bytmax;
585               size = newsize;
586             }
587           bytmax = newmax;
588           bufmax++;
589         }
590       retval = bytmax;
591
592       /* #### Should go past the found location to reduce the number
593          of times that this function is called */
594     }
595   else /* x < bufmin */
596     {
597       Bytind newmin;
598       Bytecount newsize;
599
600       forward_p = 0;
601       while (x < bufmin)
602         {
603           newmin = bytmin;
604
605           DEC_BYTIND (buf, newmin);
606           newsize = bytmin - newmin;
607           if (newsize != size)
608             {
609               bufmax = bufmin;
610               bytmax = bytmin;
611               size = newsize;
612             }
613           bytmin = newmin;
614           bufmin--;
615         }
616       retval = bytmin;
617
618       /* #### Should go past the found location to reduce the number
619          of times that this function is called
620          */
621     }
622
623   /* If size is three, than we have to max sure that the range we
624      discovered isn't too large, because we use a fixed-length
625      table to divide by 3. */
626
627   if (size == 3)
628     {
629       int gap = bytmax - bytmin;
630       buf->text->mule_three_p = 1;
631       buf->text->mule_shifter = 1;
632
633       if (gap > MAX_BYTIND_GAP_SIZE_3)
634         {
635           if (forward_p)
636             {
637               bytmin = bytmax - MAX_BYTIND_GAP_SIZE_3;
638               bufmin = bufmax - MAX_BUFPOS_GAP_SIZE_3;
639             }
640           else
641             {
642               bytmax = bytmin + MAX_BYTIND_GAP_SIZE_3;
643               bufmax = bufmin + MAX_BUFPOS_GAP_SIZE_3;
644             }
645         }
646     }
647   else
648     {
649       buf->text->mule_three_p = 0;
650       if (size == 4)
651         buf->text->mule_shifter = 2;
652       else
653         buf->text->mule_shifter = size - 1;
654     }
655
656   buf->text->mule_bufmin = bufmin;
657   buf->text->mule_bufmax = bufmax;
658   buf->text->mule_bytmin = bytmin;
659   buf->text->mule_bytmax = bytmax;
660
661   if (add_to_cache)
662     {
663       int replace_loc;
664
665       /* We throw away a "random" cached value and replace it with
666          the new value.  It doesn't actually have to be very random
667          at all, just evenly distributed.
668
669          #### It would be better to use a least-recently-used algorithm
670          or something that tries to space things out, but I'm not sure
671          it's worth it to go to the trouble of maintaining that. */
672       not_very_random_number += 621;
673       replace_loc = not_very_random_number & 15;
674       buf->text->mule_bufpos_cache[replace_loc] = x;
675       buf->text->mule_bytind_cache[replace_loc] = retval;
676     }
677
678   return retval;
679 }
680
681 /* The logic in this function is almost identical to the logic in
682    the previous function. */
683
684 Bufpos
685 bytind_to_bufpos_func (struct buffer *buf, Bytind x)
686 {
687   Bufpos bufmin;
688   Bufpos bufmax;
689   Bytind bytmin;
690   Bytind bytmax;
691   int size;
692   int forward_p;
693   Bufpos retval;
694   int diff_so_far;
695   int add_to_cache = 0;
696
697   /* Check for some cached positions, for speed. */
698   if (x == BI_BUF_PT (buf))
699     return BUF_PT (buf);
700   if (x == BI_BUF_ZV (buf))
701     return BUF_ZV (buf);
702   if (x == BI_BUF_BEGV (buf))
703     return BUF_BEGV (buf);
704
705   bufmin = buf->text->mule_bufmin;
706   bufmax = buf->text->mule_bufmax;
707   bytmin = buf->text->mule_bytmin;
708   bytmax = buf->text->mule_bytmax;
709   size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p;
710
711   /* The basic idea here is that we shift the "known region" up or down
712      until it overlaps the specified position.  We do this by moving
713      the upper bound of the known region up one character at a time,
714      and moving the lower bound of the known region up as necessary
715      when the size of the character just seen changes.
716
717      We optimize this, however, by first shifting the known region to
718      one of the cached points if it's close by. (We don't check BI_BEG or
719      BI_Z, even though they're cached; most of the time these will be the
720      same as BI_BEGV and BI_ZV, and when they're not, they're not likely
721      to be used.) */
722
723   if (x > bytmax)
724     {
725       Bytind diffmax = x - bytmax;
726       Bytind diffpt = x - BI_BUF_PT (buf);
727       Bytind diffzv = BI_BUF_ZV (buf) - x;
728       /* #### This value could stand some more exploration. */
729       Bytecount heuristic_hack = (bytmax - bytmin) >> 2;
730
731       /* Check if the position is closer to PT or ZV than to the
732          end of the known region. */
733
734       if (diffpt < 0)
735         diffpt = -diffpt;
736       if (diffzv < 0)
737         diffzv = -diffzv;
738
739       /* But also implement a heuristic that favors the known region
740          over BI_PT or BI_ZV.  The reason for this is that switching to
741          BI_PT or BI_ZV will wipe out the knowledge in the known region,
742          which might be annoying if the known region is large and
743          BI_PT or BI_ZV is not that much closer than the end of the known
744          region. */
745
746       diffzv += heuristic_hack;
747       diffpt += heuristic_hack;
748       if (diffpt < diffmax && diffpt <= diffzv)
749         {
750           bufmax = bufmin = BUF_PT (buf);
751           bytmax = bytmin = BI_BUF_PT (buf);
752           /* We set the size to 1 even though it doesn't really
753              matter because the new known region contains no
754              characters.  We do this because this is the most
755              likely size of the characters around the new known
756              region, and we avoid potential yuckiness that is
757              done when size == 3. */
758           size = 1;
759         }
760       if (diffzv < diffmax)
761         {
762           bufmax = bufmin = BUF_ZV (buf);
763           bytmax = bytmin = BI_BUF_ZV (buf);
764           size = 1;
765         }
766     }
767 #ifdef ERROR_CHECK_BUFPOS
768   else if (x >= bytmin)
769     abort ();
770 #endif
771   else
772     {
773       Bytind diffmin = bytmin - x;
774       Bytind diffpt = BI_BUF_PT (buf) - x;
775       Bytind diffbegv = x - BI_BUF_BEGV (buf);
776       /* #### This value could stand some more exploration. */
777       Bytecount heuristic_hack = (bytmax - bytmin) >> 2;
778
779       if (diffpt < 0)
780         diffpt = -diffpt;
781       if (diffbegv < 0)
782         diffbegv = -diffbegv;
783
784       /* But also implement a heuristic that favors the known region --
785          see above. */
786
787       diffbegv += heuristic_hack;
788       diffpt += heuristic_hack;
789
790       if (diffpt < diffmin && diffpt <= diffbegv)
791         {
792           bufmax = bufmin = BUF_PT (buf);
793           bytmax = bytmin = BI_BUF_PT (buf);
794           /* We set the size to 1 even though it doesn't really
795              matter because the new known region contains no
796              characters.  We do this because this is the most
797              likely size of the characters around the new known
798              region, and we avoid potential yuckiness that is
799              done when size == 3. */
800           size = 1;
801         }
802       if (diffbegv < diffmin)
803         {
804           bufmax = bufmin = BUF_BEGV (buf);
805           bytmax = bytmin = BI_BUF_BEGV (buf);
806           size = 1;
807         }
808     }
809
810   diff_so_far = x > bytmax ? x - bytmax : bytmin - x;
811   if (diff_so_far > 50)
812     {
813       /* If we have to move more than a certain amount, then look
814          into our cache. */
815       int minval = INT_MAX;
816       int found = 0;
817       int i;
818
819       add_to_cache = 1;
820       /* I considered keeping the positions ordered.  This would speed
821          up this loop, but updating the cache would take longer, so
822          it doesn't seem like it would really matter. */
823       for (i = 0; i < 16; i++)
824         {
825           int diff = buf->text->mule_bytind_cache[i] - x;
826
827           if (diff < 0)
828             diff = -diff;
829           if (diff < minval)
830             {
831               minval = diff;
832               found = i;
833             }
834         }
835
836       if (minval < diff_so_far)
837         {
838           bufmax = bufmin = buf->text->mule_bufpos_cache[found];
839           bytmax = bytmin = buf->text->mule_bytind_cache[found];
840           size = 1;
841         }
842     }
843
844   /* It's conceivable that the caching above could lead to X being
845      the same as one of the range edges. */
846   if (x >= bytmax)
847     {
848       Bytind newmax;
849       Bytecount newsize;
850
851       forward_p = 1;
852       while (x > bytmax)
853         {
854           newmax = bytmax;
855
856           INC_BYTIND (buf, newmax);
857           newsize = newmax - bytmax;
858           if (newsize != size)
859             {
860               bufmin = bufmax;
861               bytmin = bytmax;
862               size = newsize;
863             }
864           bytmax = newmax;
865           bufmax++;
866         }
867       retval = bufmax;
868
869       /* #### Should go past the found location to reduce the number
870          of times that this function is called */
871     }
872   else /* x <= bytmin */
873     {
874       Bytind newmin;
875       Bytecount newsize;
876
877       forward_p = 0;
878       while (x < bytmin)
879         {
880           newmin = bytmin;
881
882           DEC_BYTIND (buf, newmin);
883           newsize = bytmin - newmin;
884           if (newsize != size)
885             {
886               bufmax = bufmin;
887               bytmax = bytmin;
888               size = newsize;
889             }
890           bytmin = newmin;
891           bufmin--;
892         }
893       retval = bufmin;
894
895       /* #### Should go past the found location to reduce the number
896          of times that this function is called
897          */
898     }
899
900   /* If size is three, than we have to max sure that the range we
901      discovered isn't too large, because we use a fixed-length
902      table to divide by 3. */
903
904   if (size == 3)
905     {
906       int gap = bytmax - bytmin;
907       buf->text->mule_three_p = 1;
908       buf->text->mule_shifter = 1;
909
910       if (gap > MAX_BYTIND_GAP_SIZE_3)
911         {
912           if (forward_p)
913             {
914               bytmin = bytmax - MAX_BYTIND_GAP_SIZE_3;
915               bufmin = bufmax - MAX_BUFPOS_GAP_SIZE_3;
916             }
917           else
918             {
919               bytmax = bytmin + MAX_BYTIND_GAP_SIZE_3;
920               bufmax = bufmin + MAX_BUFPOS_GAP_SIZE_3;
921             }
922         }
923     }
924   else
925     {
926       buf->text->mule_three_p = 0;
927       if (size == 4)
928         buf->text->mule_shifter = 2;
929       else
930         buf->text->mule_shifter = size - 1;
931     }
932
933   buf->text->mule_bufmin = bufmin;
934   buf->text->mule_bufmax = bufmax;
935   buf->text->mule_bytmin = bytmin;
936   buf->text->mule_bytmax = bytmax;
937
938   if (add_to_cache)
939     {
940       int replace_loc;
941
942       /* We throw away a "random" cached value and replace it with
943          the new value.  It doesn't actually have to be very random
944          at all, just evenly distributed.
945
946          #### It would be better to use a least-recently-used algorithm
947          or something that tries to space things out, but I'm not sure
948          it's worth it to go to the trouble of maintaining that. */
949       not_very_random_number += 621;
950       replace_loc = not_very_random_number & 15;
951       buf->text->mule_bufpos_cache[replace_loc] = retval;
952       buf->text->mule_bytind_cache[replace_loc] = x;
953     }
954
955   return retval;
956 }
957
958 /* Text of length BYTELENGTH and CHARLENGTH (in different units)
959    was inserted at bufpos START. */
960
961 static void
962 buffer_mule_signal_inserted_region (struct buffer *buf, Bufpos start,
963                                     Bytecount bytelength,
964                                     Charcount charlength)
965 {
966   int size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p;
967   int i;
968
969   /* Adjust the cache of known positions. */
970   for (i = 0; i < 16; i++)
971     {
972
973       if (buf->text->mule_bufpos_cache[i] > start)
974         {
975           buf->text->mule_bufpos_cache[i] += charlength;
976           buf->text->mule_bytind_cache[i] += bytelength;
977         }
978     }
979
980   if (start >= buf->text->mule_bufmax)
981     return;
982
983   /* The insertion is either before the known region, in which case
984      it shoves it forward; or within the known region, in which case
985      it shoves the end forward. (But it may make the known region
986      inconsistent, so we may have to shorten it.) */
987
988   if (start <= buf->text->mule_bufmin)
989     {
990       buf->text->mule_bufmin += charlength;
991       buf->text->mule_bufmax += charlength;
992       buf->text->mule_bytmin += bytelength;
993       buf->text->mule_bytmax += bytelength;
994     }
995   else
996     {
997       Bufpos end = start + charlength;
998       /* the insertion point divides the known region in two.
999          Keep the longer half, at least, and expand into the
1000          inserted chunk as much as possible. */
1001
1002       if (start - buf->text->mule_bufmin > buf->text->mule_bufmax - start)
1003         {
1004           Bytind bytestart = (buf->text->mule_bytmin
1005                               + size * (start - buf->text->mule_bufmin));
1006           Bytind bytenew;
1007
1008           while (start < end)
1009             {
1010               bytenew = bytestart;
1011               INC_BYTIND (buf, bytenew);
1012               if (bytenew - bytestart != size)
1013                 break;
1014               start++;
1015               bytestart = bytenew;
1016             }
1017           if (start != end)
1018             {
1019               buf->text->mule_bufmax = start;
1020               buf->text->mule_bytmax = bytestart;
1021             }
1022           else
1023             {
1024               buf->text->mule_bufmax += charlength;
1025               buf->text->mule_bytmax += bytelength;
1026             }
1027         }
1028       else
1029         {
1030           Bytind byteend = (buf->text->mule_bytmin
1031                             + size * (start - buf->text->mule_bufmin)
1032                             + bytelength);
1033           Bytind bytenew;
1034
1035           buf->text->mule_bufmax += charlength;
1036           buf->text->mule_bytmax += bytelength;
1037
1038           while (end > start)
1039             {
1040               bytenew = byteend;
1041               DEC_BYTIND (buf, bytenew);
1042               if (byteend - bytenew != size)
1043                 break;
1044               end--;
1045               byteend = bytenew;
1046             }
1047           if (start != end)
1048             {
1049               buf->text->mule_bufmin = end;
1050               buf->text->mule_bytmin = byteend;
1051             }
1052         }
1053     }
1054 }
1055
1056 /* Text from START to END (equivalent in Bytinds: from BI_START to
1057    BI_END) was deleted. */
1058
1059 static void
1060 buffer_mule_signal_deleted_region (struct buffer *buf, Bufpos start,
1061                                    Bufpos end, Bytind bi_start,
1062                                    Bytind bi_end)
1063 {
1064   int i;
1065
1066   /* Adjust the cache of known positions. */
1067   for (i = 0; i < 16; i++)
1068     {
1069       /* After the end; gets shoved backward */
1070       if (buf->text->mule_bufpos_cache[i] > end)
1071         {
1072           buf->text->mule_bufpos_cache[i] -= end - start;
1073           buf->text->mule_bytind_cache[i] -= bi_end - bi_start;
1074         }
1075       /* In the range; moves to start of range */
1076       else if (buf->text->mule_bufpos_cache[i] > start)
1077         {
1078           buf->text->mule_bufpos_cache[i] = start;
1079           buf->text->mule_bytind_cache[i] = bi_start;
1080         }
1081     }
1082
1083   /* We don't care about any text after the end of the known region. */
1084
1085   end = min (end, buf->text->mule_bufmax);
1086   bi_end = min (bi_end, buf->text->mule_bytmax);
1087   if (start >= end)
1088     return;
1089
1090   /* The end of the known region offsets by the total amount of deletion,
1091      since it's all before it. */
1092
1093   buf->text->mule_bufmax -= end - start;
1094   buf->text->mule_bytmax -= bi_end - bi_start;
1095
1096   /* Now we don't care about any text after the start of the known region. */
1097
1098   end = min (end, buf->text->mule_bufmin);
1099   bi_end = min (bi_end, buf->text->mule_bytmin);
1100   if (start >= end)
1101     return;
1102
1103   buf->text->mule_bufmin -= end - start;
1104   buf->text->mule_bytmin -= bi_end - bi_start;
1105 }
1106
1107 #endif /* MULE */
1108
1109 #ifdef ERROR_CHECK_BUFPOS
1110
1111 Bytind
1112 bufpos_to_bytind (struct buffer *buf, Bufpos x)
1113 {
1114   Bytind retval = real_bufpos_to_bytind (buf, x);
1115   ASSERT_VALID_BYTIND_UNSAFE (buf, retval);
1116   return retval;
1117 }
1118
1119 Bufpos
1120 bytind_to_bufpos (struct buffer *buf, Bytind x)
1121 {
1122   ASSERT_VALID_BYTIND_UNSAFE (buf, x);
1123   return real_bytind_to_bufpos (buf, x);
1124 }
1125
1126 #endif /* ERROR_CHECK_BUFPOS */
1127
1128 \f
1129 /************************************************************************/
1130 /*                verifying buffer and string positions                 */
1131 /************************************************************************/
1132
1133 /* Functions below are tagged with either _byte or _char indicating
1134    whether they return byte or character positions.  For a buffer,
1135    a character position is a "Bufpos" and a byte position is a "Bytind".
1136    For strings, these are sometimes typed using "Charcount" and
1137    "Bytecount". */
1138
1139 /* Flags for the functions below are:
1140
1141    GB_ALLOW_PAST_ACCESSIBLE
1142
1143      Allow positions to range over the entire buffer (BUF_BEG to BUF_Z),
1144      rather than just the accessible portion (BUF_BEGV to BUF_ZV).
1145      For strings, this flag has no effect.
1146
1147    GB_COERCE_RANGE
1148
1149      If the position is outside the allowable range, return the lower
1150      or upper bound of the range, whichever is closer to the specified
1151      position.
1152
1153    GB_NO_ERROR_IF_BAD
1154
1155      If the position is outside the allowable range, return -1.
1156
1157    GB_NEGATIVE_FROM_END
1158
1159      If a value is negative, treat it as an offset from the end.
1160      Only applies to strings.
1161
1162    The following additional flags apply only to the functions
1163    that return ranges:
1164
1165    GB_ALLOW_NIL
1166
1167      Either or both positions can be nil.  If FROM is nil,
1168      FROM_OUT will contain the lower bound of the allowed range.
1169      If TO is nil, TO_OUT will contain the upper bound of the
1170      allowed range.
1171
1172    GB_CHECK_ORDER
1173
1174      FROM must contain the lower bound and TO the upper bound
1175      of the range.  If the positions are reversed, an error is
1176      signalled.
1177
1178    The following is a combination flag:
1179
1180    GB_HISTORICAL_STRING_BEHAVIOR
1181
1182      Equivalent to (GB_NEGATIVE_FROM_END | GB_ALLOW_NIL).
1183  */
1184
1185 /* Return a buffer position stored in a Lisp_Object.  Full
1186    error-checking is done on the position.  Flags can be specified to
1187    control the behavior of out-of-range values.  The default behavior
1188    is to require that the position is within the accessible part of
1189    the buffer (BEGV and ZV), and to signal an error if the position is
1190    out of range.
1191
1192 */
1193
1194 Bufpos
1195 get_buffer_pos_char (struct buffer *b, Lisp_Object pos, unsigned int flags)
1196 {
1197   Bufpos ind;
1198   Bufpos min_allowed, max_allowed;
1199
1200   CHECK_INT_COERCE_MARKER (pos);
1201   ind = XINT (pos);
1202   min_allowed = flags & GB_ALLOW_PAST_ACCESSIBLE ? BUF_BEG (b) : BUF_BEGV (b);
1203   max_allowed = flags & GB_ALLOW_PAST_ACCESSIBLE ? BUF_Z   (b) : BUF_ZV   (b);
1204
1205   if (ind < min_allowed || ind > max_allowed)
1206     {
1207       if (flags & GB_COERCE_RANGE)
1208         ind = ind < min_allowed ? min_allowed : max_allowed;
1209       else if (flags & GB_NO_ERROR_IF_BAD)
1210         ind = -1;
1211       else
1212         {
1213           Lisp_Object buffer;
1214           XSETBUFFER (buffer, b);
1215           args_out_of_range (buffer, pos);
1216         }
1217     }
1218
1219   return ind;
1220 }
1221
1222 Bytind
1223 get_buffer_pos_byte (struct buffer *b, Lisp_Object pos, unsigned int flags)
1224 {
1225   Bufpos bpos = get_buffer_pos_char (b, pos, flags);
1226   if (bpos < 0) /* could happen with GB_NO_ERROR_IF_BAD */
1227     return -1;
1228   return bufpos_to_bytind (b, bpos);
1229 }
1230
1231 /* Return a pair of buffer positions representing a range of text,
1232    taken from a pair of Lisp_Objects.  Full error-checking is
1233    done on the positions.  Flags can be specified to control the
1234    behavior of out-of-range values.  The default behavior is to
1235    allow the range bounds to be specified in either order
1236    (however, FROM_OUT will always be the lower bound of the range
1237    and TO_OUT the upper bound),to require that the positions
1238    are within the accessible part of the buffer (BEGV and ZV),
1239    and to signal an error if the positions are out of range.
1240 */
1241
1242 void
1243 get_buffer_range_char (struct buffer *b, Lisp_Object from, Lisp_Object to,
1244                        Bufpos *from_out, Bufpos *to_out, unsigned int flags)
1245 {
1246   Bufpos min_allowed, max_allowed;
1247
1248   min_allowed = (flags & GB_ALLOW_PAST_ACCESSIBLE) ?
1249     BUF_BEG (b) : BUF_BEGV (b);
1250   max_allowed = (flags & GB_ALLOW_PAST_ACCESSIBLE) ?
1251     BUF_Z (b) : BUF_ZV (b);
1252
1253   if (NILP (from) && (flags & GB_ALLOW_NIL))
1254     *from_out = min_allowed;
1255   else
1256     *from_out = get_buffer_pos_char (b, from, flags | GB_NO_ERROR_IF_BAD);
1257
1258   if (NILP (to) && (flags & GB_ALLOW_NIL))
1259     *to_out = max_allowed;
1260   else
1261     *to_out = get_buffer_pos_char (b, to, flags | GB_NO_ERROR_IF_BAD);
1262
1263   if ((*from_out < 0 || *to_out < 0) && !(flags & GB_NO_ERROR_IF_BAD))
1264     {
1265       Lisp_Object buffer;
1266       XSETBUFFER (buffer, b);
1267       args_out_of_range_3 (buffer, from, to);
1268     }
1269
1270   if (*from_out >= 0 && *to_out >= 0 && *from_out > *to_out)
1271     {
1272       if (flags & GB_CHECK_ORDER)
1273         signal_simple_error_2 ("start greater than end", from, to);
1274       else
1275         {
1276           Bufpos temp = *from_out;
1277           *from_out = *to_out;
1278           *to_out = temp;
1279         }
1280     }
1281 }
1282
1283 void
1284 get_buffer_range_byte (struct buffer *b, Lisp_Object from, Lisp_Object to,
1285                        Bytind *from_out, Bytind *to_out, unsigned int flags)
1286 {
1287   Bufpos s, e;
1288
1289   get_buffer_range_char (b, from, to, &s, &e, flags);
1290   if (s >= 0)
1291     *from_out = bufpos_to_bytind (b, s);
1292   else /* could happen with GB_NO_ERROR_IF_BAD */
1293     *from_out = -1;
1294   if (e >= 0)
1295     *to_out = bufpos_to_bytind (b, e);
1296   else
1297     *to_out = -1;
1298 }
1299
1300 static Charcount
1301 get_string_pos_char_1 (Lisp_Object string, Lisp_Object pos, unsigned int flags,
1302                        Charcount known_length)
1303 {
1304   Charcount ccpos;
1305   Charcount min_allowed = 0;
1306   Charcount max_allowed = known_length;
1307
1308   /* Computation of KNOWN_LENGTH is potentially expensive so we pass
1309      it in. */
1310   CHECK_INT (pos);
1311   ccpos = XINT (pos);
1312   if (ccpos < 0 && flags & GB_NEGATIVE_FROM_END)
1313     ccpos += max_allowed;
1314
1315   if (ccpos < min_allowed || ccpos > max_allowed)
1316     {
1317       if (flags & GB_COERCE_RANGE)
1318         ccpos = ccpos < min_allowed ? min_allowed : max_allowed;
1319       else if (flags & GB_NO_ERROR_IF_BAD)
1320         ccpos = -1;
1321       else
1322         args_out_of_range (string, pos);
1323     }
1324
1325   return ccpos;
1326 }
1327
1328 Charcount
1329 get_string_pos_char (Lisp_Object string, Lisp_Object pos, unsigned int flags)
1330 {
1331   return get_string_pos_char_1 (string, pos, flags,
1332                                 XSTRING_CHAR_LENGTH (string));
1333 }
1334
1335 Bytecount
1336 get_string_pos_byte (Lisp_Object string, Lisp_Object pos, unsigned int flags)
1337 {
1338   Charcount ccpos = get_string_pos_char (string, pos, flags);
1339   if (ccpos < 0) /* could happen with GB_NO_ERROR_IF_BAD */
1340     return -1;
1341   return charcount_to_bytecount (XSTRING_DATA (string), ccpos);
1342 }
1343
1344 void
1345 get_string_range_char (Lisp_Object string, Lisp_Object from, Lisp_Object to,
1346                        Charcount *from_out, Charcount *to_out,
1347                        unsigned int flags)
1348 {
1349   Charcount min_allowed = 0;
1350   Charcount max_allowed = XSTRING_CHAR_LENGTH (string);
1351
1352   if (NILP (from) && (flags & GB_ALLOW_NIL))
1353     *from_out = min_allowed;
1354   else
1355     *from_out = get_string_pos_char_1 (string, from,
1356                                        flags | GB_NO_ERROR_IF_BAD,
1357                                        max_allowed);
1358
1359   if (NILP (to) && (flags & GB_ALLOW_NIL))
1360     *to_out = max_allowed;
1361   else
1362     *to_out = get_string_pos_char_1 (string, to,
1363                                      flags | GB_NO_ERROR_IF_BAD,
1364                                      max_allowed);
1365
1366   if ((*from_out < 0 || *to_out < 0) && !(flags & GB_NO_ERROR_IF_BAD))
1367     args_out_of_range_3 (string, from, to);
1368
1369   if (*from_out >= 0 && *to_out >= 0 && *from_out > *to_out)
1370     {
1371       if (flags & GB_CHECK_ORDER)
1372         signal_simple_error_2 ("start greater than end", from, to);
1373       else
1374         {
1375           Bufpos temp = *from_out;
1376           *from_out = *to_out;
1377           *to_out = temp;
1378         }
1379     }
1380 }
1381
1382 void
1383 get_string_range_byte (Lisp_Object string, Lisp_Object from, Lisp_Object to,
1384                        Bytecount *from_out, Bytecount *to_out,
1385                        unsigned int flags)
1386 {
1387   Charcount s, e;
1388
1389   get_string_range_char (string, from, to, &s, &e, flags);
1390   if (s >= 0)
1391     *from_out = charcount_to_bytecount (XSTRING_DATA (string), s);
1392   else /* could happen with GB_NO_ERROR_IF_BAD */
1393     *from_out = -1;
1394   if (e >= 0)
1395     *to_out = charcount_to_bytecount (XSTRING_DATA (string), e);
1396   else
1397     *to_out = -1;
1398
1399 }
1400
1401 Bufpos
1402 get_buffer_or_string_pos_char (Lisp_Object object, Lisp_Object pos,
1403                                unsigned int flags)
1404 {
1405   return STRINGP (object) ?
1406     get_string_pos_char (object, pos, flags) :
1407     get_buffer_pos_char (XBUFFER (object), pos, flags);
1408 }
1409
1410 Bytind
1411 get_buffer_or_string_pos_byte (Lisp_Object object, Lisp_Object pos,
1412                                unsigned int flags)
1413 {
1414   return STRINGP (object) ?
1415     get_string_pos_byte (object, pos, flags) :
1416     get_buffer_pos_byte (XBUFFER (object), pos, flags);
1417 }
1418
1419 void
1420 get_buffer_or_string_range_char (Lisp_Object object, Lisp_Object from,
1421                                  Lisp_Object to, Bufpos *from_out,
1422                                  Bufpos *to_out, unsigned int flags)
1423 {
1424   if (STRINGP (object))
1425     get_string_range_char (object, from, to, from_out, to_out, flags);
1426   else
1427     get_buffer_range_char (XBUFFER (object), from, to, from_out, to_out, flags);
1428 }
1429
1430 void
1431 get_buffer_or_string_range_byte (Lisp_Object object, Lisp_Object from,
1432                                  Lisp_Object to, Bytind *from_out,
1433                                  Bytind *to_out, unsigned int flags)
1434 {
1435   if (STRINGP (object))
1436     get_string_range_byte (object, from, to, from_out, to_out, flags);
1437   else
1438     get_buffer_range_byte (XBUFFER (object), from, to, from_out, to_out, flags);
1439 }
1440
1441 Bufpos
1442 buffer_or_string_accessible_begin_char (Lisp_Object object)
1443 {
1444   return STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object));
1445 }
1446
1447 Bufpos
1448 buffer_or_string_accessible_end_char (Lisp_Object object)
1449 {
1450   return STRINGP (object) ?
1451     XSTRING_CHAR_LENGTH (object) : BUF_ZV (XBUFFER (object));
1452 }
1453
1454 Bytind
1455 buffer_or_string_accessible_begin_byte (Lisp_Object object)
1456 {
1457   return STRINGP (object) ? 0 : BI_BUF_BEGV (XBUFFER (object));
1458 }
1459
1460 Bytind
1461 buffer_or_string_accessible_end_byte (Lisp_Object object)
1462 {
1463   return STRINGP (object) ?
1464     XSTRING_LENGTH (object) : BI_BUF_ZV (XBUFFER (object));
1465 }
1466
1467 Bufpos
1468 buffer_or_string_absolute_begin_char (Lisp_Object object)
1469 {
1470   return STRINGP (object) ? 0 : BUF_BEG (XBUFFER (object));
1471 }
1472
1473 Bufpos
1474 buffer_or_string_absolute_end_char (Lisp_Object object)
1475 {
1476   return STRINGP (object) ?
1477     XSTRING_CHAR_LENGTH (object) : BUF_Z (XBUFFER (object));
1478 }
1479
1480 Bytind
1481 buffer_or_string_absolute_begin_byte (Lisp_Object object)
1482 {
1483   return STRINGP (object) ? 0 : BI_BUF_BEG (XBUFFER (object));
1484 }
1485
1486 Bytind
1487 buffer_or_string_absolute_end_byte (Lisp_Object object)
1488 {
1489   return STRINGP (object) ?
1490     XSTRING_LENGTH (object) : BI_BUF_Z (XBUFFER (object));
1491 }
1492
1493 \f
1494 /************************************************************************/
1495 /*                     point and marker adjustment                      */
1496 /************************************************************************/
1497
1498 /* just_set_point() is the only place `PT' is an lvalue in all of emacs.
1499    This function is called from set_buffer_point(), which is the function
1500    that the SET_PT and BUF_SET_PT macros expand into, and from the
1501    routines below that insert and delete text. (This is in cases where
1502    the point marker logically doesn't move but PT (being a byte index)
1503    needs to get adjusted.) */
1504
1505 /* Set point to a specified value.  This is used only when the value
1506    of point changes due to an insert or delete; it does not represent
1507    a conceptual change in point as a marker.  In particular, point is
1508    not crossing any interval boundaries, so there's no need to use the
1509    usual SET_PT macro.  In fact it would be incorrect to do so, because
1510    either the old or the new value of point is out of synch with the
1511    current set of intervals.  */
1512
1513 /* This gets called more than enough to make the function call
1514    overhead a significant factor so we've turned it into a macro. */
1515 #define JUST_SET_POINT(buf, bufpos, ind)        \
1516 do                                              \
1517 {                                               \
1518   buf->bufpt = (bufpos);                        \
1519   buf->pt = (ind);                              \
1520 } while (0)
1521
1522 /* Set a buffer's point. */
1523
1524 void
1525 set_buffer_point (struct buffer *buf, Bufpos bufpos, Bytind bytpos)
1526 {
1527   assert (bytpos >= BI_BUF_BEGV (buf) && bytpos <= BI_BUF_ZV (buf));
1528   if (bytpos == BI_BUF_PT (buf))
1529     return;
1530   JUST_SET_POINT (buf, bufpos, bytpos);
1531   MARK_POINT_CHANGED;
1532   assert (MARKERP (buf->point_marker));
1533   XMARKER (buf->point_marker)->memind =
1534     bytind_to_memind (buf, bytpos);
1535
1536   /* FSF makes sure that PT is not being set within invisible text.
1537      However, this is the wrong place for that check.  The check
1538      should happen only at the next redisplay. */
1539
1540   /* Some old coder said:
1541
1542      "If there were to be hooks which were run when point entered/left an
1543      extent, this would be the place to put them.
1544
1545      However, it's probably the case that such hooks should be implemented
1546      using a post-command-hook instead, to avoid running the hooks as a
1547      result of intermediate motion inside of save-excursions, for example."
1548
1549      I definitely agree with this.  PT gets moved all over the place
1550      and it would be a Bad Thing for any hooks to get called, both for
1551      the reason above and because many callers are not prepared for
1552      a GC within this function. --ben
1553    */
1554 }
1555
1556 /* Do the correct marker-like adjustment on MPOS (see below).  FROM, TO,
1557    and AMOUNT are as in adjust_markers().  If MPOS doesn't need to be
1558    adjusted, nothing will happen. */
1559 Memind
1560 do_marker_adjustment (Memind mpos, Memind from,
1561                       Memind to, Bytecount amount)
1562 {
1563   if (amount > 0)
1564     {
1565       if (mpos > to && mpos < to + amount)
1566         mpos = to + amount;
1567     }
1568   else
1569     {
1570       if (mpos > from + amount && mpos <= from)
1571         mpos = from + amount;
1572     }
1573   if (mpos > from && mpos <= to)
1574     mpos += amount;
1575   return mpos;
1576 }
1577
1578 /* Do the following:
1579
1580    (1) Add `amount' to the position of every marker in the current buffer
1581    whose current position is between `from' (exclusive) and `to' (inclusive).
1582
1583    (2) Also, any markers past the outside of that interval, in the direction
1584    of adjustment, are first moved back to the near end of the interval
1585    and then adjusted by `amount'.
1586
1587    This function is called in two different cases: when a region of
1588    characters adjacent to the gap is moved, causing the gap to shift
1589    to the other side of the region (in this case, `from' and `to'
1590    point to the old position of the region and there should be no
1591    markers affected by (2) because they would be inside the gap),
1592    or when a region of characters adjacent to the gap is wiped out,
1593    causing the gap to increase to include the region (in this case,
1594    `from' and `to' are the same, both pointing to the boundary
1595    between the gap and the deleted region, and there are no markers
1596    affected by (1)).
1597
1598    The reason for the use of exclusive and inclusive is that markers at
1599    the gap always sit at the beginning, not at the end.
1600 */
1601
1602 static void
1603 adjust_markers (struct buffer *buf, Memind from, Memind to,
1604                 Bytecount amount)
1605 {
1606   Lisp_Marker *m;
1607
1608   for (m = BUF_MARKERS (buf); m; m = marker_next (m))
1609     m->memind = do_marker_adjustment (m->memind, from, to, amount);
1610 }
1611
1612 /* Adjust markers whose insertion-type is t
1613    for an insertion of AMOUNT characters at POS.  */
1614
1615 static void
1616 adjust_markers_for_insert (struct buffer *buf, Memind ind, Bytecount amount)
1617 {
1618   Lisp_Marker *m;
1619
1620   for (m = BUF_MARKERS (buf); m; m = marker_next (m))
1621     {
1622       if (m->insertion_type && m->memind == ind)
1623         m->memind += amount;
1624     }
1625 }
1626
1627 \f
1628 /************************************************************************/
1629 /*                  Routines for dealing with the gap                   */
1630 /************************************************************************/
1631
1632 /* maximum amount of memory moved in a single chunk.  Increasing this
1633    value improves gap-motion efficiency but decreases QUIT responsiveness
1634    time.  Was 32000 but today's processors are faster and files are
1635    bigger.  --ben */
1636 #define GAP_MOVE_CHUNK 300000
1637
1638 /* Move the gap to POS, which is less than the current GPT. */
1639
1640 static void
1641 gap_left (struct buffer *buf, Bytind pos)
1642 {
1643   Bufbyte *to, *from;
1644   Bytecount i;
1645   Bytind new_s1;
1646   struct buffer *mbuf;
1647   Lisp_Object bufcons;
1648
1649   from = BUF_GPT_ADDR (buf);
1650   to = from + BUF_GAP_SIZE (buf);
1651   new_s1 = BI_BUF_GPT (buf);
1652
1653   /* Now copy the characters.  To move the gap down,
1654      copy characters up.  */
1655
1656   while (1)
1657     {
1658       /* I gets number of characters left to copy.  */
1659       i = new_s1 - pos;
1660       if (i == 0)
1661         break;
1662       /* If a quit is requested, stop copying now.
1663          Change POS to be where we have actually moved the gap to.  */
1664       if (QUITP)
1665         {
1666           pos = new_s1;
1667           break;
1668         }
1669       /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */
1670       if (i > GAP_MOVE_CHUNK)
1671         i = GAP_MOVE_CHUNK;
1672
1673       if (i >= 128)
1674         {
1675           new_s1 -= i;
1676           from   -= i;
1677           to     -= i;
1678           memmove (to, from, i);
1679         }
1680       else
1681         {
1682           new_s1 -= i;
1683           while (--i >= 0)
1684             *--to = *--from;
1685         }
1686     }
1687
1688   /* Adjust markers, and buffer data structure, to put the gap at POS.
1689      POS is where the loop above stopped, which may be what was specified
1690      or may be where a quit was detected.  */
1691   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1692     {
1693       adjust_markers (mbuf, pos, BI_BUF_GPT (mbuf), BUF_GAP_SIZE (mbuf));
1694     }
1695   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1696     {
1697       adjust_extents (make_buffer (mbuf), pos, BI_BUF_GPT (mbuf),
1698                       BUF_GAP_SIZE (mbuf));
1699     }
1700   SET_BI_BUF_GPT (buf, pos);
1701   SET_GAP_SENTINEL (buf);
1702 #ifdef ERROR_CHECK_EXTENTS
1703   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1704     {
1705       sledgehammer_extent_check (make_buffer (mbuf));
1706     }
1707 #endif
1708   QUIT;
1709 }
1710
1711 static void
1712 gap_right (struct buffer *buf, Bytind pos)
1713 {
1714   Bufbyte *to, *from;
1715   Bytecount i;
1716   Bytind new_s1;
1717   struct buffer *mbuf;
1718   Lisp_Object bufcons;
1719
1720   to = BUF_GPT_ADDR (buf);
1721   from = to + BUF_GAP_SIZE (buf);
1722   new_s1 = BI_BUF_GPT (buf);
1723
1724   /* Now copy the characters.  To move the gap up,
1725      copy characters down.  */
1726
1727   while (1)
1728     {
1729       /* I gets number of characters left to copy.  */
1730       i = pos - new_s1;
1731       if (i == 0)
1732         break;
1733       /* If a quit is requested, stop copying now.
1734          Change POS to be where we have actually moved the gap to.  */
1735       if (QUITP)
1736         {
1737           pos = new_s1;
1738           break;
1739         }
1740       /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */
1741       if (i > GAP_MOVE_CHUNK)
1742         i = GAP_MOVE_CHUNK;
1743
1744       if (i >= 128)
1745         {
1746           new_s1 += i;
1747           memmove (to, from, i);
1748           from += i;
1749           to   += i;
1750         }
1751       else
1752         {
1753           new_s1 += i;
1754           while (--i >= 0)
1755             *to++ = *from++;
1756         }
1757     }
1758
1759   {
1760     int gsize = BUF_GAP_SIZE (buf);
1761     MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1762       {
1763         adjust_markers (mbuf, BI_BUF_GPT (mbuf) + gsize, pos + gsize, - gsize);
1764       }
1765     MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1766       {
1767         adjust_extents (make_buffer (mbuf), BI_BUF_GPT (mbuf) + gsize,
1768                         pos + gsize, - gsize);
1769       }
1770     SET_BI_BUF_GPT (buf, pos);
1771     SET_GAP_SENTINEL (buf);
1772 #ifdef ERROR_CHECK_EXTENTS
1773     MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
1774       {
1775         sledgehammer_extent_check (make_buffer (mbuf));
1776       }
1777 #endif
1778   }
1779   if (pos == BI_BUF_Z (buf))
1780     {
1781       /* merge gap with end gap */
1782
1783       SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + BUF_END_GAP_SIZE (buf));
1784       SET_BUF_END_GAP_SIZE (buf, 0);
1785       SET_END_SENTINEL (buf);
1786     }
1787
1788   QUIT;
1789 }
1790
1791 /* Move gap to position `pos'.
1792    Note that this can quit!  */
1793
1794 static void
1795 move_gap (struct buffer *buf, Bytind pos)
1796 {
1797   if (! BUF_BEG_ADDR (buf))
1798     abort ();
1799   if (pos < BI_BUF_GPT (buf))
1800     gap_left (buf, pos);
1801   else if (pos > BI_BUF_GPT (buf))
1802     gap_right (buf, pos);
1803 }
1804
1805 /* Merge the end gap into the gap */
1806
1807 static void
1808 merge_gap_with_end_gap (struct buffer *buf)
1809 {
1810   Lisp_Object tem;
1811   Bytind real_gap_loc;
1812   Bytecount old_gap_size;
1813   Bytecount increment;
1814
1815   increment = BUF_END_GAP_SIZE (buf);
1816   SET_BUF_END_GAP_SIZE (buf, 0);
1817
1818   if (increment > 0)
1819     {
1820       /* Prevent quitting in move_gap.  */
1821       tem = Vinhibit_quit;
1822       Vinhibit_quit = Qt;
1823
1824       real_gap_loc = BI_BUF_GPT (buf);
1825       old_gap_size = BUF_GAP_SIZE (buf);
1826
1827       /* Pretend the end gap is the gap */
1828       SET_BI_BUF_GPT (buf, BI_BUF_Z (buf) + BUF_GAP_SIZE (buf));
1829       SET_BUF_GAP_SIZE (buf, increment);
1830
1831       /* Move the new gap down to be consecutive with the end of the old one.
1832          This adjusts the markers properly too.  */
1833       gap_left (buf, real_gap_loc + old_gap_size);
1834
1835       /* Now combine the two into one large gap.  */
1836       SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + old_gap_size);
1837       SET_BI_BUF_GPT (buf, real_gap_loc);
1838       SET_GAP_SENTINEL (buf);
1839
1840       /* We changed the total size of the buffer (including gap),
1841          so we need to fix up the end sentinel. */
1842       SET_END_SENTINEL (buf);
1843
1844       Vinhibit_quit = tem;
1845     }
1846 }
1847
1848 /* Make the gap INCREMENT bytes longer.  */
1849
1850 static void
1851 make_gap (struct buffer *buf, Bytecount increment)
1852 {
1853   Bufbyte *result;
1854   Lisp_Object tem;
1855   Bytind real_gap_loc;
1856   Bytecount old_gap_size;
1857
1858   /* If we have to get more space, get enough to last a while.  We use
1859      a geometric progression that saves on realloc space. */
1860   increment += 2000 + ((BI_BUF_Z (buf) - BI_BUF_BEG (buf)) / 8);
1861
1862   if (increment > BUF_END_GAP_SIZE (buf))
1863     {
1864       /* Don't allow a buffer size that won't fit in an int
1865          even if it will fit in a Lisp integer.
1866          That won't work because so many places use `int'.  */
1867
1868       if (BUF_Z (buf) - BUF_BEG (buf) + BUF_GAP_SIZE (buf) + increment
1869           > EMACS_INT_MAX)
1870         error ("Maximum buffer size exceeded");
1871
1872       result = BUFFER_REALLOC (buf->text->beg,
1873                                BI_BUF_Z (buf) - BI_BUF_BEG (buf) +
1874                                BUF_GAP_SIZE (buf) + increment +
1875                                BUF_END_SENTINEL_SIZE);
1876       if (result == 0)
1877         memory_full ();
1878
1879       SET_BUF_BEG_ADDR (buf, result);
1880     }
1881   else
1882     increment = BUF_END_GAP_SIZE (buf);
1883
1884   /* Prevent quitting in move_gap.  */
1885   tem = Vinhibit_quit;
1886   Vinhibit_quit = Qt;
1887
1888   real_gap_loc = BI_BUF_GPT (buf);
1889   old_gap_size = BUF_GAP_SIZE (buf);
1890
1891   /* Call the newly allocated space a gap at the end of the whole space.  */
1892   SET_BI_BUF_GPT (buf, BI_BUF_Z (buf) + BUF_GAP_SIZE (buf));
1893   SET_BUF_GAP_SIZE (buf, increment);
1894
1895   SET_BUF_END_GAP_SIZE (buf, 0);
1896
1897   /* Move the new gap down to be consecutive with the end of the old one.
1898      This adjusts the markers properly too.  */
1899   gap_left (buf, real_gap_loc + old_gap_size);
1900
1901   /* Now combine the two into one large gap.  */
1902   SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + old_gap_size);
1903   SET_BI_BUF_GPT (buf, real_gap_loc);
1904   SET_GAP_SENTINEL (buf);
1905
1906   /* We changed the total size of the buffer (including gap),
1907      so we need to fix up the end sentinel. */
1908   SET_END_SENTINEL (buf);
1909
1910   Vinhibit_quit = tem;
1911 }
1912
1913 \f
1914 /************************************************************************/
1915 /*                     Before/after-change processing                   */
1916 /************************************************************************/
1917
1918 /* Those magic changes ... */
1919
1920 static void
1921 buffer_signal_changed_region (struct buffer *buf, Bufpos start,
1922                               Bufpos end)
1923 {
1924   /* The changed region is recorded as the number of unchanged
1925      characters from the beginning and from the end of the
1926      buffer.  This obviates much of the need of shifting the
1927      region around to compensate for insertions and deletions.
1928      */
1929   if (buf->changes->begin_unchanged < 0 ||
1930       buf->changes->begin_unchanged > start - BUF_BEG (buf))
1931     buf->changes->begin_unchanged = start - BUF_BEG (buf);
1932   if (buf->changes->end_unchanged < 0 ||
1933       buf->changes->end_unchanged > BUF_Z (buf) - end)
1934     buf->changes->end_unchanged = BUF_Z (buf) - end;
1935 }
1936
1937 void
1938 buffer_extent_signal_changed_region (struct buffer *buf, Bufpos start,
1939                                      Bufpos end)
1940 {
1941   if (buf->changes->begin_extent_unchanged < 0 ||
1942       buf->changes->begin_extent_unchanged > start - BUF_BEG (buf))
1943     buf->changes->begin_extent_unchanged = start - BUF_BEG (buf);
1944   if (buf->changes->end_extent_unchanged < 0 ||
1945       buf->changes->end_extent_unchanged > BUF_Z (buf) - end)
1946     buf->changes->end_extent_unchanged = BUF_Z (buf) - end;
1947 }
1948
1949 void
1950 buffer_reset_changes (struct buffer *buf)
1951 {
1952   buf->changes->begin_unchanged = -1;
1953   buf->changes->end_unchanged = -1;
1954   buf->changes->begin_extent_unchanged = -1;
1955   buf->changes->end_extent_unchanged = -1;
1956   buf->changes->newline_was_deleted = 0;
1957 }
1958
1959 static void
1960 signal_after_change (struct buffer *buf, Bufpos start, Bufpos orig_end,
1961                      Bufpos new_end);
1962
1963
1964 /* Call the after-change-functions according to the changes made so far
1965    and treat all further changes as single until the outermost
1966    multiple change exits.  This is called when the outermost multiple
1967    change exits and when someone is trying to make a change that violates
1968    the constraints specified in begin_multiple_change(), typically
1969    when nested multiple-change sessions occur. (There are smarter ways of
1970    dealing with nested multiple changes, but these rarely occur so there's
1971    probably no point in it.) */
1972
1973 /* #### This needs to keep track of what actually changed and only
1974    call the after-change functions on that region. */
1975
1976 static void
1977 cancel_multiple_change (struct buffer *buf)
1978 {
1979   /* This function can GC */
1980   /* Call the after-change-functions except when they've already been
1981      called or when there were no changes made to the buffer at all. */
1982   if (buf->text->changes->mc_begin != 0 &&
1983       buf->text->changes->mc_begin_signaled)
1984     {
1985       Bufpos real_mc_begin = buf->text->changes->mc_begin;
1986       buf->text->changes->mc_begin = 0;
1987
1988       signal_after_change (buf, real_mc_begin, buf->text->changes->mc_orig_end,
1989                            buf->text->changes->mc_new_end);
1990     }
1991   else
1992     {
1993       buf->text->changes->mc_begin = 0;
1994     }
1995 }
1996
1997 /* this is an unwind_protect, to ensure that the after-change-functions
1998    get called even in a non-local exit. */
1999
2000 static Lisp_Object
2001 multiple_change_finish_up (Lisp_Object buffer)
2002 {
2003   struct buffer *buf = XBUFFER (buffer);
2004
2005   /* #### I don't know whether or not it should even be possible to
2006      get here with a dead buffer (though given how it is called I can
2007      see how it might be).  In any case, there isn't time before 19.14
2008      to find out. */
2009   if (!BUFFER_LIVE_P (buf))
2010     return Qnil;
2011
2012   /* This function can GC */
2013   buf->text->changes->in_multiple_change = 0; /* do this first so that
2014                                                  errors in the after-change
2015                                                  functions don't mess things
2016                                                  up. */
2017   cancel_multiple_change (buf);
2018   return Qnil;
2019 }
2020
2021 /* Call this function when you're about to make a number of buffer changes
2022    that should be considered a single change. (e.g. `replace-match' calls
2023    this.) You need to specify the START and END of the region that is
2024    going to be changed so that the before-change-functions are called
2025    with the correct arguments.  The after-change region is calculated
2026    automatically, however, and if changes somehow or other happen outside
2027    of the specified region, that will also be handled correctly.
2028
2029    begin_multiple_change() returns a number (actually a specpdl depth)
2030    that you must pass to end_multiple_change() when you are done.
2031
2032    FSF Emacs 20 implements a similar feature, accessible from Lisp
2033    through a `combine-after-change-calls' special form, which is
2034    essentially equivalent to this function.  We should consider
2035    whether we want to introduce a similar Lisp form.  */
2036
2037 int
2038 begin_multiple_change (struct buffer *buf, Bufpos start, Bufpos end)
2039 {
2040   /* This function can GC */
2041   int count = -1;
2042   if (buf->text->changes->in_multiple_change)
2043     {
2044       if (buf->text->changes->mc_begin != 0 &&
2045           (start < buf->text->changes->mc_begin ||
2046            end > buf->text->changes->mc_new_end))
2047         cancel_multiple_change (buf);
2048     }
2049   else
2050     {
2051       Lisp_Object buffer;
2052
2053       buf->text->changes->mc_begin = start;
2054       buf->text->changes->mc_orig_end = buf->text->changes->mc_new_end = end;
2055       buf->text->changes->mc_begin_signaled = 0;
2056       count = specpdl_depth ();
2057       XSETBUFFER (buffer, buf);
2058       record_unwind_protect (multiple_change_finish_up, buffer);
2059     }
2060   buf->text->changes->in_multiple_change++;
2061   /* We don't call before-change-functions until signal_before_change()
2062      is called, in case there is a read-only or other error. */
2063   return count;
2064 }
2065
2066 void
2067 end_multiple_change (struct buffer *buf, int count)
2068 {
2069   assert (buf->text->changes->in_multiple_change > 0);
2070   buf->text->changes->in_multiple_change--;
2071   if (!buf->text->changes->in_multiple_change)
2072     unbind_to (count, Qnil);
2073 }
2074
2075 static int inside_change_hook;
2076
2077 static Lisp_Object
2078 change_function_restore (Lisp_Object buffer)
2079 {
2080   /* We should first reset the variable and then change the buffer,
2081      because Fset_buffer() can throw.  */
2082   inside_change_hook = 0;
2083   if (XBUFFER (buffer) != current_buffer)
2084     Fset_buffer (buffer);
2085   return Qnil;
2086 }
2087
2088 static int in_first_change;
2089
2090 static Lisp_Object
2091 first_change_hook_restore (Lisp_Object buffer)
2092 {
2093   in_first_change = 0;
2094   Fset_buffer (buffer);
2095   return Qnil;
2096 }
2097
2098 /* Signal an initial modification to the buffer.  */
2099
2100 static void
2101 signal_first_change (struct buffer *buf)
2102 {
2103   /* This function can GC */
2104   Lisp_Object buffer;
2105   XSETBUFFER (buffer, current_buffer);
2106
2107   if (!in_first_change)
2108     {
2109       if (!NILP (symbol_value_in_buffer (Qfirst_change_hook, buffer)))
2110         {
2111           int speccount = specpdl_depth ();
2112           record_unwind_protect (first_change_hook_restore, buffer);
2113           set_buffer_internal (buf);
2114           in_first_change = 1;
2115           run_hook (Qfirst_change_hook);
2116           unbind_to (speccount, Qnil);
2117         }
2118     }
2119 }
2120
2121 /* Signal a change to the buffer immediately before it happens.
2122    START and END are the bounds of the text to be changed. */
2123
2124 static void
2125 signal_before_change (struct buffer *buf, Bufpos start, Bufpos end)
2126 {
2127   /* This function can GC */
2128   struct buffer *mbuf;
2129   Lisp_Object bufcons;
2130
2131   if (!inside_change_hook)
2132     {
2133       Lisp_Object buffer;
2134       int speccount;
2135
2136       /* Are we in a multiple-change session? */
2137       if (buf->text->changes->in_multiple_change &&
2138           buf->text->changes->mc_begin != 0)
2139         {
2140           /* If we're violating the constraints of the session,
2141              call the after-change-functions as necessary for the
2142              changes already made and treat further changes as
2143              single. */
2144           if (start < buf->text->changes->mc_begin ||
2145               end > buf->text->changes->mc_new_end)
2146             cancel_multiple_change (buf);
2147           /* Do nothing if this is not the first change in the session. */
2148           else if (buf->text->changes->mc_begin_signaled)
2149             return;
2150           else
2151             {
2152               /* First time through; call the before-change-functions
2153                  specifying the entire region to be changed. (Note that
2154                  we didn't call before-change-functions in
2155                  begin_multiple_change() because the buffer might be
2156                  read-only, etc.) */
2157               start = buf->text->changes->mc_begin;
2158               end = buf->text->changes->mc_new_end;
2159             }
2160         }
2161
2162       /* If buffer is unmodified, run a special hook for that case.  */
2163       if (BUF_SAVE_MODIFF (buf) >= BUF_MODIFF (buf))
2164         {
2165           MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2166             {
2167               signal_first_change (mbuf);
2168             }
2169         }
2170
2171       /* Now in any case run the before-change-functions if any.  */
2172       speccount = specpdl_depth ();
2173       record_unwind_protect (change_function_restore, Fcurrent_buffer ());
2174       inside_change_hook = 1;
2175
2176       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2177         {
2178           XSETBUFFER (buffer, mbuf);
2179           if (!NILP (symbol_value_in_buffer (Qbefore_change_functions, buffer))
2180               /* Obsolete, for compatibility */
2181               || !NILP (symbol_value_in_buffer (Qbefore_change_function, buffer)))
2182             {
2183               set_buffer_internal (buf);
2184               va_run_hook_with_args (Qbefore_change_functions, 2,
2185                                      make_int (start), make_int (end));
2186               /* Obsolete, for compatibility */
2187               va_run_hook_with_args (Qbefore_change_function, 2,
2188                                      make_int (start), make_int (end));
2189             }
2190         }
2191
2192       /* Make sure endpoints remain valid.  before-change-functions
2193          might have modified the buffer. */
2194       if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
2195       if (start > BUF_ZV (buf))   start = BUF_ZV (buf);
2196       if (end < BUF_BEGV (buf)) end = BUF_BEGV (buf);
2197       if (end > BUF_ZV (buf))   end = BUF_ZV (buf);
2198
2199       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2200         {
2201           XSETBUFFER (buffer, mbuf);
2202           report_extent_modification (buffer, start, end, 0);
2203         }
2204       unbind_to (speccount, Qnil);
2205
2206       /* Only now do we indicate that the before-change-functions have
2207          been called, in case some function throws out. */
2208       buf->text->changes->mc_begin_signaled = 1;
2209     }
2210 }
2211
2212 /* Signal a change immediately after it happens.
2213    START is the bufpos of the start of the changed text.
2214    ORIG_END is the bufpos of the end of the before-changed text.
2215    NEW_END is the bufpos of the end of the after-changed text.
2216  */
2217
2218 static void
2219 signal_after_change (struct buffer *buf, Bufpos start, Bufpos orig_end,
2220                      Bufpos new_end)
2221 {
2222   /* This function can GC */
2223   struct buffer *mbuf;
2224   Lisp_Object bufcons;
2225
2226   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2227     {
2228       /* always do this. */
2229       buffer_signal_changed_region (mbuf, start, new_end);
2230     }
2231   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2232     {
2233       /* #### This seems inefficient.  Wouldn't it be better to just
2234          keep one cache per base buffer?  */
2235       font_lock_maybe_update_syntactic_caches (mbuf, start, orig_end, new_end);
2236     }
2237
2238   if (!inside_change_hook)
2239     {
2240       Lisp_Object buffer;
2241       int speccount;
2242
2243       if (buf->text->changes->in_multiple_change &&
2244           buf->text->changes->mc_begin != 0)
2245         {
2246           assert (start >= buf->text->changes->mc_begin &&
2247                   start <= buf->text->changes->mc_new_end);
2248           assert (orig_end >= buf->text->changes->mc_begin &&
2249                   orig_end <= buf->text->changes->mc_new_end);
2250           buf->text->changes->mc_new_end += new_end - orig_end;
2251           return; /* after-change-functions signalled when all changes done */
2252         }
2253
2254       speccount = specpdl_depth ();
2255       record_unwind_protect (change_function_restore, Fcurrent_buffer ());
2256       inside_change_hook = 1;
2257       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2258         {
2259           XSETBUFFER (buffer, mbuf);
2260
2261           if (!NILP (symbol_value_in_buffer (Qafter_change_functions, buffer))
2262               /* Obsolete, for compatibility */
2263               || !NILP (symbol_value_in_buffer (Qafter_change_function, buffer)))
2264             {
2265               set_buffer_internal (buf);
2266               /* The actual after-change functions take slightly
2267                  different arguments than what we were passed. */
2268               va_run_hook_with_args (Qafter_change_functions, 3,
2269                                      make_int (start), make_int (new_end),
2270                                      make_int (orig_end - start));
2271               /* Obsolete, for compatibility */
2272               va_run_hook_with_args (Qafter_change_function, 3,
2273                                      make_int (start), make_int (new_end),
2274                                      make_int (orig_end - start));
2275             }
2276         }
2277
2278       /* Make sure endpoints remain valid.  after-change-functions
2279          might have modified the buffer. */
2280       if (start < BUF_BEGV (buf)) start = BUF_BEGV (buf);
2281       if (start > BUF_ZV (buf))   start = BUF_ZV (buf);
2282       if (new_end < BUF_BEGV (buf)) new_end = BUF_BEGV (buf);
2283       if (new_end > BUF_ZV (buf))   new_end = BUF_ZV (buf);
2284       if (orig_end < BUF_BEGV (buf)) orig_end = BUF_BEGV (buf);
2285       if (orig_end > BUF_ZV (buf))   orig_end = BUF_ZV (buf);
2286
2287       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2288         {
2289           XSETBUFFER (buffer, mbuf);
2290           report_extent_modification (buffer, start, new_end, 1);
2291         }
2292       unbind_to (speccount, Qnil); /* sets inside_change_hook back to 0 */
2293     }
2294 }
2295
2296 /* Call this if you're about to change the region of BUFFER from START
2297    to END.  This checks the read-only properties of the region, calls
2298    the necessary modification hooks, and warns the next redisplay that
2299    it should pay attention to that area.  */
2300
2301 static void
2302 prepare_to_modify_buffer (struct buffer *buf, Bufpos start, Bufpos end,
2303                           int lockit)
2304 {
2305   /* This function can GC */
2306   /* dmoore - This function can also kill the buffer buf, the current
2307      buffer, and do anything it pleases.  So if you call it, be
2308      careful. */
2309   struct buffer *mbuf;
2310   Lisp_Object buffer, bufcons;
2311   struct gcpro gcpro1;
2312
2313   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2314     {
2315       barf_if_buffer_read_only (mbuf, start, end);
2316     }
2317
2318   /* if this is the first modification, see about locking the buffer's
2319      file */
2320   XSETBUFFER (buffer, buf);
2321   GCPRO1 (buffer);
2322   if (!NILP (buf->filename) && lockit &&
2323       BUF_SAVE_MODIFF (buf) >= BUF_MODIFF (buf))
2324     {
2325 #ifdef CLASH_DETECTION
2326       if (!NILP (buf->file_truename))
2327         /* Make binding buffer-file-name to nil effective.  */
2328         lock_file (buf->file_truename);
2329 #else
2330       /* At least warn if this file has changed on disk since it was visited.*/
2331       if (NILP (Fverify_visited_file_modtime (buffer))
2332           && !NILP (Ffile_exists_p (buf->filename)))
2333         call1_in_buffer (buf, intern ("ask-user-about-supersession-threat"),
2334                          buf->filename);
2335 #endif /* not CLASH_DETECTION */
2336     }
2337   UNGCPRO;
2338
2339   /* #### dmoore - is this reasonable in case of buf being killed above? */
2340   if (!BUFFER_LIVE_P (buf))
2341     return;
2342
2343   signal_before_change (buf, start, end);
2344
2345 #ifdef REGION_CACHE_NEEDS_WORK
2346   if (buf->newline_cache)
2347     invalidate_region_cache (buf,
2348                              buf->newline_cache,
2349                              start - BUF_BEG (buf), BUF_Z (buf) - end);
2350   if (buf->width_run_cache)
2351     invalidate_region_cache (buf,
2352                              buf->width_run_cache,
2353                              start - BUF_BEG (buf), BUF_Z (buf) - end);
2354 #endif
2355
2356 #if 0 /* FSFmacs */
2357   Vdeactivate_mark = Qt;
2358 #endif
2359
2360   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2361     {
2362       mbuf->point_before_scroll = Qnil;
2363     }
2364 }
2365
2366 \f
2367 /************************************************************************/
2368 /*                        Insertion of strings                          */
2369 /************************************************************************/
2370
2371 void
2372 fixup_internal_substring (const Bufbyte *nonreloc, Lisp_Object reloc,
2373                           Bytecount offset, Bytecount *len)
2374 {
2375   assert ((nonreloc && NILP (reloc)) || (!nonreloc && STRINGP (reloc)));
2376
2377   if (*len < 0)
2378     {
2379       if (nonreloc)
2380         *len = strlen ((const char *) nonreloc) - offset;
2381       else
2382         *len = XSTRING_LENGTH (reloc) - offset;
2383     }
2384 #ifdef ERROR_CHECK_BUFPOS
2385   assert (*len >= 0);
2386   if (STRINGP (reloc))
2387     {
2388       assert (offset >= 0 && offset <= XSTRING_LENGTH (reloc));
2389       assert (offset + *len <= XSTRING_LENGTH (reloc));
2390     }
2391 #endif
2392 }
2393
2394 /* Insert a string into BUF at Bufpos POS.  The string data comes
2395    from one of two sources: constant, non-relocatable data (specified
2396    in NONRELOC), or a Lisp string object (specified in RELOC), which
2397    is relocatable and may have extent data that needs to be copied
2398    into the buffer.  OFFSET and LENGTH specify the substring of the
2399    data that is actually to be inserted.  As a special case, if POS
2400    is -1, insert the string at point and move point to the end of the
2401    string.
2402
2403    Normally, markers at the insertion point end up before the
2404    inserted string.  If INSDEL_BEFORE_MARKERS is set in flags, however,
2405    they end up after the string.
2406
2407    INSDEL_NO_LOCKING is kludgy and is used when insert-file-contents is
2408    visiting a new file; it inhibits the locking checks normally done
2409    before modifying a buffer.  Similar checks were already done
2410    in the higher-level Lisp functions calling insert-file-contents. */
2411
2412 Charcount
2413 buffer_insert_string_1 (struct buffer *buf, Bufpos pos,
2414                         const Bufbyte *nonreloc, Lisp_Object reloc,
2415                         Bytecount offset, Bytecount length,
2416                         int flags)
2417 {
2418   /* This function can GC */
2419   struct gcpro gcpro1;
2420   Bytind ind;
2421   Charcount cclen;
2422   int move_point = 0;
2423   struct buffer *mbuf;
2424   Lisp_Object bufcons;
2425
2426   /* Defensive steps just in case a buffer gets deleted and a calling
2427      function doesn't notice it. */
2428   if (!BUFFER_LIVE_P (buf))
2429     return 0;
2430
2431   fixup_internal_substring (nonreloc, reloc, offset, &length);
2432
2433   if (pos == -1)
2434     {
2435       pos = BUF_PT (buf);
2436       move_point = 1;
2437     }
2438
2439 #ifdef I18N3
2440   /* #### See the comment in print_internal().  If this buffer is marked
2441      as translatable, then Fgettext() should be called on obj if it
2442      is a string. */
2443 #endif
2444
2445   /* Make sure that point-max won't exceed the size of an emacs int. */
2446   if ((length + BUF_Z (buf)) > EMACS_INT_MAX)
2447     error ("Maximum buffer size exceeded");
2448
2449   /* theoretically not necessary -- caller should GCPRO.
2450      #### buffer_insert_from_buffer_1() doesn't!  */
2451   GCPRO1 (reloc);
2452
2453   prepare_to_modify_buffer (buf, pos, pos, !(flags & INSDEL_NO_LOCKING));
2454
2455   /* Defensive steps in case the before-change-functions fuck around */
2456   if (!BUFFER_LIVE_P (buf))
2457     {
2458       UNGCPRO;
2459       /* Bad bad pre-change function. */
2460       return 0;
2461     }
2462
2463   /* Make args be valid again.  prepare_to_modify_buffer() might have
2464      modified the buffer. */
2465   if (pos < BUF_BEGV (buf))
2466     pos = BUF_BEGV (buf);
2467   if (pos > BUF_ZV (buf))
2468     pos = BUF_ZV (buf);
2469
2470   /* string may have been relocated up to this point */
2471   if (STRINGP (reloc))
2472     nonreloc = XSTRING_DATA (reloc);
2473
2474   ind = bufpos_to_bytind (buf, pos);
2475   cclen = bytecount_to_charcount (nonreloc + offset, length);
2476
2477   if (ind != BI_BUF_GPT (buf))
2478     /* #### if debug-on-quit is invoked and the user changes the
2479        buffer, bad things can happen.  This is a rampant problem
2480        in Emacs. */
2481     move_gap (buf, ind); /* may QUIT */
2482   if (! GAP_CAN_HOLD_SIZE_P (buf, length))
2483     {
2484       if (BUF_END_GAP_SIZE (buf) >= length)
2485         merge_gap_with_end_gap (buf);
2486       else
2487         make_gap (buf, length - BUF_GAP_SIZE (buf));
2488     }
2489
2490   insert_invalidate_line_number_cache (buf, pos, nonreloc + offset, length);
2491
2492   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2493     {
2494       record_insert (mbuf, pos, cclen);
2495     }
2496
2497   BUF_MODIFF (buf)++;
2498   MARK_BUFFERS_CHANGED;
2499
2500   /* string may have been relocated up to this point */
2501   if (STRINGP (reloc))
2502     nonreloc = XSTRING_DATA (reloc);
2503
2504   memcpy (BUF_GPT_ADDR (buf), nonreloc + offset, length);
2505
2506   SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) - length);
2507   SET_BI_BUF_GPT (buf, BI_BUF_GPT (buf) + length);
2508   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2509     {
2510       SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) + cclen, BI_BUF_ZV (mbuf) + length);
2511     }
2512   SET_BOTH_BUF_Z (buf, BUF_Z (buf) + cclen, BI_BUF_Z (buf) + length);
2513   SET_GAP_SENTINEL (buf);
2514
2515 #ifdef MULE
2516   buffer_mule_signal_inserted_region (buf, pos, length, cclen);
2517 #endif
2518
2519   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2520     {
2521       process_extents_for_insertion (make_buffer (mbuf), ind, length);
2522     }
2523
2524   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2525     {
2526       /* We know the gap is at IND so the cast is OK. */
2527       adjust_markers_for_insert (mbuf, (Memind) ind, length);
2528     }
2529
2530   /* Point logically doesn't move, but may need to be adjusted because
2531      it's a byte index.  point-marker doesn't change because it's a
2532      memory index. */
2533   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2534     {
2535       if (BI_BUF_PT (mbuf) > ind)
2536         JUST_SET_POINT (mbuf, BUF_PT (mbuf) + cclen,
2537                         BI_BUF_PT (mbuf) + length);
2538     }
2539
2540   /* Well, point might move. */
2541   if (move_point)
2542     BI_BUF_SET_PT (buf, ind + length);
2543
2544   if (STRINGP (reloc))
2545     {
2546       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2547         {
2548           splice_in_string_extents (reloc, mbuf, ind, length, offset);
2549         }
2550     }
2551
2552   if (flags & INSDEL_BEFORE_MARKERS)
2553     {
2554       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2555         {
2556           /* ind - 1 is correct because the FROM argument is exclusive.
2557              I formerly used DEC_BYTIND() but that caused problems at the
2558              beginning of the buffer. */
2559           adjust_markers (mbuf, ind - 1, ind, length);
2560         }
2561     }
2562
2563   signal_after_change (buf, pos, pos, pos + cclen);
2564
2565   UNGCPRO;
2566
2567   return cclen;
2568 }
2569
2570
2571 /* The following functions are interfaces onto the above function,
2572    for inserting particular sorts of data.  In all the functions,
2573    BUF and POS specify the buffer and location where the insertion is
2574    to take place. (If POS is -1, text is inserted at point and point
2575    moves forward past the text.) FLAGS is as above. */
2576
2577 Charcount
2578 buffer_insert_raw_string_1 (struct buffer *buf, Bufpos pos,
2579                             const Bufbyte *nonreloc, Bytecount length,
2580                             int flags)
2581 {
2582   /* This function can GC */
2583   return buffer_insert_string_1 (buf, pos, nonreloc, Qnil, 0, length,
2584                                  flags);
2585 }
2586
2587 Charcount
2588 buffer_insert_lisp_string_1 (struct buffer *buf, Bufpos pos, Lisp_Object str,
2589                              int flags)
2590 {
2591   /* This function can GC */
2592 #ifdef ERROR_CHECK_TYPECHECK
2593   assert (STRINGP (str));
2594 #endif
2595   return buffer_insert_string_1 (buf, pos, 0, str, 0,
2596                                  XSTRING_LENGTH (str),
2597                                  flags);
2598 }
2599
2600 /* Insert the null-terminated string S (in external format). */
2601
2602 Charcount
2603 buffer_insert_c_string_1 (struct buffer *buf, Bufpos pos, const char *s,
2604                           int flags)
2605 {
2606   /* This function can GC */
2607   const char *translated = GETTEXT (s);
2608   return buffer_insert_string_1 (buf, pos, (const Bufbyte *) translated, Qnil,
2609                                  0, strlen (translated), flags);
2610 }
2611
2612 Charcount
2613 buffer_insert_emacs_char_1 (struct buffer *buf, Bufpos pos, Emchar ch,
2614                             int flags)
2615 {
2616   /* This function can GC */
2617   Bufbyte str[MAX_EMCHAR_LEN];
2618   Bytecount len = set_charptr_emchar (str, ch);
2619   return buffer_insert_string_1 (buf, pos, str, Qnil, 0, len, flags);
2620 }
2621
2622 Charcount
2623 buffer_insert_c_char_1 (struct buffer *buf, Bufpos pos, char c,
2624                         int flags)
2625 {
2626   /* This function can GC */
2627   return buffer_insert_emacs_char_1 (buf, pos, (Emchar) (unsigned char) c,
2628                                      flags);
2629 }
2630
2631 Charcount
2632 buffer_insert_from_buffer_1 (struct buffer *buf, Bufpos pos,
2633                              struct buffer *buf2, Bufpos pos2,
2634                              Charcount length, int flags)
2635 {
2636   /* This function can GC */
2637   Lisp_Object str = make_string_from_buffer (buf2, pos2, length);
2638   return buffer_insert_string_1 (buf, pos, 0, str, 0,
2639                                  XSTRING_LENGTH (str), flags);
2640 }
2641
2642 \f
2643 /************************************************************************/
2644 /*                        Deletion of ranges                            */
2645 /************************************************************************/
2646
2647 /* Delete characters in buffer from FROM up to (but not including) TO.  */
2648
2649 void
2650 buffer_delete_range (struct buffer *buf, Bufpos from, Bufpos to, int flags)
2651 {
2652   /* This function can GC */
2653   Charcount numdel;
2654   Bytind bi_from, bi_to;
2655   Bytecount bc_numdel;
2656   EMACS_INT shortage;
2657   struct buffer *mbuf;
2658   Lisp_Object bufcons;
2659
2660   /* Defensive steps just in case a buffer gets deleted and a calling
2661      function doesn't notice it. */
2662   if (!BUFFER_LIVE_P (buf))
2663     return;
2664
2665   /* Make args be valid */
2666   if (from < BUF_BEGV (buf))
2667     from = BUF_BEGV (buf);
2668   if (to > BUF_ZV (buf))
2669     to = BUF_ZV (buf);
2670   if ((numdel = to - from) <= 0)
2671     return;
2672
2673   prepare_to_modify_buffer (buf, from, to, !(flags & INSDEL_NO_LOCKING));
2674
2675   /* Defensive steps in case the before-change-functions fuck around */
2676   if (!BUFFER_LIVE_P (buf))
2677     /* Bad bad pre-change function. */
2678     return;
2679
2680   /* Make args be valid again.  prepare_to_modify_buffer() might have
2681      modified the buffer. */
2682   if (from < BUF_BEGV (buf))
2683     from = BUF_BEGV (buf);
2684   if (to > BUF_ZV (buf))
2685     to = BUF_ZV (buf);
2686   if ((numdel = to - from) <= 0)
2687     return;
2688
2689   /* Redisplay needs to know if a newline was in the deleted region.
2690      If we've already marked the changed region as having a deleted
2691      newline there is no use in performing the check. */
2692   if (!buf->changes->newline_was_deleted)
2693     {
2694       scan_buffer (buf, '\n', from, to, 1, &shortage, 1);
2695       if (!shortage)
2696         {
2697           MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2698             {
2699               mbuf->changes->newline_was_deleted = 1;
2700             }
2701         }
2702     }
2703
2704   bi_from = bufpos_to_bytind (buf, from);
2705   bi_to = bufpos_to_bytind (buf, to);
2706   bc_numdel = bi_to - bi_from;
2707
2708   delete_invalidate_line_number_cache (buf, from, to);
2709
2710   if (to == BUF_Z (buf) &&
2711       bi_from > BI_BUF_GPT (buf))
2712     {
2713       /* avoid moving the gap just to delete from the bottom. */
2714
2715       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2716         {
2717           record_delete (mbuf, from, numdel);
2718         }
2719       BUF_MODIFF (buf)++;
2720       MARK_BUFFERS_CHANGED;
2721
2722       /* #### Point used to be modified here, but this causes problems
2723          with MULE, as point is used to calculate bytinds, and if the
2724          offset in bc_numdel causes point to move to a non first-byte
2725          location, causing some other function to throw an assertion
2726          in ASSERT_VALID_BYTIND. I've moved the code to right after
2727          the other movements and adjustments, but before the gap is
2728          moved.  -- jh 970813 */
2729
2730       /* Detach any extents that are completely within the range [FROM, TO],
2731          if the extents are detachable.
2732
2733          This must come AFTER record_delete(), so that the appropriate
2734          extents will be present to be recorded, and BEFORE the gap
2735          size is increased, as otherwise we will be confused about
2736          where the extents end. */
2737       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2738         {
2739           process_extents_for_deletion (make_buffer (mbuf), bi_from, bi_to, 0);
2740         }
2741
2742       /* Relocate all markers pointing into the new, larger gap to
2743          point at the end of the text before the gap.  */
2744       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2745         {
2746           adjust_markers (mbuf,
2747                           (bi_to + BUF_GAP_SIZE (mbuf)),
2748                           (bi_to + BUF_GAP_SIZE (mbuf)),
2749                           (- bc_numdel));
2750         }
2751
2752       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2753         {
2754           /* Relocate any extent endpoints just like markers. */
2755           adjust_extents_for_deletion (make_buffer (mbuf), bi_from, bi_to,
2756                                        BUF_GAP_SIZE (mbuf), bc_numdel, 0);
2757         }
2758
2759       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2760         {
2761           /* Relocate point as if it were a marker.  */
2762           if (bi_from < BI_BUF_PT (mbuf))
2763             {
2764               if (BI_BUF_PT (mbuf) < bi_to)
2765                 JUST_SET_POINT (mbuf, from, bi_from);
2766               else
2767                 JUST_SET_POINT (mbuf, BUF_PT (mbuf) - numdel,
2768                                 BI_BUF_PT (mbuf) - bc_numdel);
2769             }
2770         }
2771
2772       SET_BUF_END_GAP_SIZE (buf, BUF_END_GAP_SIZE (buf) + bc_numdel);
2773
2774       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2775         {
2776           SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) - numdel,
2777                            BI_BUF_ZV (mbuf) - bc_numdel);
2778         }
2779       SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BI_BUF_Z (buf) - bc_numdel);
2780       SET_GAP_SENTINEL (buf);
2781     }
2782   else
2783     {
2784       /* Make sure the gap is somewhere in or next to what we are deleting.  */
2785       if (bi_to < BI_BUF_GPT (buf))
2786         gap_left (buf, bi_to);
2787       if (bi_from > BI_BUF_GPT (buf))
2788         gap_right (buf, bi_from);
2789
2790       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2791         {
2792           record_delete (mbuf, from, numdel);
2793         }
2794       BUF_MODIFF (buf)++;
2795       MARK_BUFFERS_CHANGED;
2796
2797       /* #### Point used to be modified here, but this causes problems
2798          with MULE, as point is used to calculate bytinds, and if the
2799          offset in bc_numdel causes point to move to a non first-byte
2800          location, causing some other function to throw an assertion
2801          in ASSERT_VALID_BYTIND. I've moved the code to right after
2802          the other movements and adjustments, but before the gap is
2803          moved.  -- jh 970813 */
2804
2805       /* Detach any extents that are completely within the range [FROM, TO],
2806          if the extents are detachable.
2807
2808          This must come AFTER record_delete(), so that the appropriate extents
2809          will be present to be recorded, and BEFORE the gap size is increased,
2810          as otherwise we will be confused about where the extents end. */
2811       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2812         {
2813           process_extents_for_deletion (make_buffer (mbuf), bi_from, bi_to, 0);
2814         }
2815
2816       /* Relocate all markers pointing into the new, larger gap to
2817          point at the end of the text before the gap.  */
2818       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2819         {
2820           adjust_markers (mbuf,
2821                           (bi_to + BUF_GAP_SIZE (mbuf)),
2822                           (bi_to + BUF_GAP_SIZE (mbuf)),
2823                           (- bc_numdel - BUF_GAP_SIZE (mbuf)));
2824         }
2825
2826       /* Relocate any extent endpoints just like markers. */
2827       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2828         {
2829           adjust_extents_for_deletion (make_buffer (mbuf), bi_from, bi_to,
2830                                        BUF_GAP_SIZE (mbuf),
2831                                        bc_numdel, BUF_GAP_SIZE (mbuf));
2832         }
2833
2834       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2835         {
2836           /* Relocate point as if it were a marker.  */
2837           if (bi_from < BI_BUF_PT (mbuf))
2838             {
2839               if (BI_BUF_PT (mbuf) < bi_to)
2840                 JUST_SET_POINT (mbuf, from, bi_from);
2841               else
2842                 JUST_SET_POINT (mbuf, BUF_PT (mbuf) - numdel,
2843                                 BI_BUF_PT (mbuf) - bc_numdel);
2844             }
2845         }
2846
2847       SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + bc_numdel);
2848       MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2849         {
2850           SET_BOTH_BUF_ZV (mbuf, BUF_ZV (mbuf) - numdel,
2851                            BI_BUF_ZV (mbuf) - bc_numdel);
2852         }
2853       SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BI_BUF_Z (buf) - bc_numdel);
2854       SET_BI_BUF_GPT (buf, bi_from);
2855       SET_GAP_SENTINEL (buf);
2856     }
2857
2858 #ifdef MULE
2859   buffer_mule_signal_deleted_region (buf, from, to, bi_from, bi_to);
2860 #endif
2861
2862 #ifdef ERROR_CHECK_EXTENTS
2863   MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2864     {
2865       sledgehammer_extent_check (make_buffer (mbuf));
2866     }
2867 #endif
2868
2869   signal_after_change (buf, from, to, from);
2870 }
2871
2872 \f
2873 /************************************************************************/
2874 /*                    Replacement of characters                         */
2875 /************************************************************************/
2876
2877 /* Replace the character at POS in buffer B with CH. */
2878
2879 void
2880 buffer_replace_char (struct buffer *buf, Bufpos pos, Emchar ch,
2881                      int not_real_change, int force_lock_check)
2882 {
2883   /* This function can GC */
2884   Bufbyte curstr[MAX_EMCHAR_LEN];
2885   Bufbyte newstr[MAX_EMCHAR_LEN];
2886   Bytecount curlen, newlen;
2887
2888   /* Defensive steps just in case a buffer gets deleted and a calling
2889      function doesn't notice it. */
2890   if (!BUFFER_LIVE_P (buf))
2891     return;
2892
2893   curlen = BUF_CHARPTR_COPY_CHAR (buf, pos, curstr);
2894   newlen = set_charptr_emchar (newstr, ch);
2895
2896   if (curlen == newlen)
2897     {
2898       struct buffer *mbuf;
2899       Lisp_Object bufcons;
2900
2901       /* then we can just replace the text. */
2902       prepare_to_modify_buffer (buf, pos, pos + 1,
2903                                 !not_real_change || force_lock_check);
2904       /* Defensive steps in case the before-change-functions fuck around */
2905       if (!BUFFER_LIVE_P (buf))
2906         /* Bad bad pre-change function. */
2907         return;
2908
2909       /* Make args be valid again.  prepare_to_modify_buffer() might have
2910          modified the buffer. */
2911       if (pos < BUF_BEGV (buf))
2912         pos = BUF_BEGV (buf);
2913       if (pos >= BUF_ZV (buf))
2914         pos = BUF_ZV (buf) - 1;
2915       if (pos < BUF_BEGV (buf))
2916         /* no more characters in buffer! */
2917         return;
2918
2919       if (BUF_FETCH_CHAR (buf, pos) == '\n')
2920         {
2921           MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2922             {
2923               mbuf->changes->newline_was_deleted = 1;
2924             }
2925         }
2926       MARK_BUFFERS_CHANGED;
2927       if (!not_real_change)
2928         {
2929           MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2930             {
2931               record_change (mbuf, pos, 1);
2932             }
2933           BUF_MODIFF (buf)++;
2934         }
2935       memcpy (BUF_BYTE_ADDRESS (buf, pos), newstr, newlen);
2936
2937       signal_after_change (buf, pos, pos + 1, pos + 1);
2938
2939       /* We do not have to adjust the Mule data; we just replaced a
2940          character with another of the same number of bytes. */
2941     }
2942   else
2943     {
2944       /*
2945        * Must implement as deletion followed by insertion.
2946        *
2947        * Make a note to move point forward later in the one situation
2948        * where it is needed, a delete/insert one position behind
2949        * point.  Point will drift backward by one position and stay
2950        * there otherwise.
2951        */
2952       int movepoint = (pos == BUF_PT (buf) - 1);
2953
2954       buffer_delete_range (buf, pos, pos + 1, 0);
2955       /* Defensive steps in case the before-change-functions fuck around */
2956       if (!BUFFER_LIVE_P (buf))
2957         /* Bad bad pre-change function. */
2958         return;
2959
2960       /* Make args be valid again.  prepare_to_modify_buffer() might have
2961          modified the buffer. */
2962       if (pos < BUF_BEGV (buf))
2963         pos = BUF_BEGV (buf);
2964       if (pos >= BUF_ZV (buf))
2965         pos = BUF_ZV (buf) - 1;
2966       if (pos < BUF_BEGV (buf))
2967         /* no more characters in buffer! */
2968         return;
2969       /*
2970        * -1 as the pos argument means to move point forward with the
2971        * insertion, which we must do if the deletion moved point
2972        * backward so that it now equals the insertion point.
2973        */
2974       buffer_insert_string_1 (buf, (movepoint ? -1 : pos),
2975                               newstr, Qnil, 0, newlen, 0);
2976     }
2977 }
2978
2979 \f
2980 /************************************************************************/
2981 /*                            Other functions                           */
2982 /************************************************************************/
2983
2984 /* Make a string from a buffer.  This needs to take into account the gap,
2985    and add any necessary extents from the buffer. */
2986
2987 static Lisp_Object
2988 make_string_from_buffer_1 (struct buffer *buf, Bufpos pos, Charcount length,
2989                            int no_extents)
2990 {
2991   /* This function can GC */
2992   Bytind    bi_ind = bufpos_to_bytind (buf, pos);
2993   Bytecount bi_len = bufpos_to_bytind (buf, pos + length) - bi_ind;
2994   Lisp_Object  val = make_uninit_string (bi_len);
2995
2996   struct gcpro gcpro1;
2997   GCPRO1 (val);
2998
2999   if (!no_extents)
3000     add_string_extents (val, buf, bi_ind, bi_len);
3001
3002   {
3003     Bytecount len1 = BI_BUF_GPT (buf) - bi_ind;
3004     Bufbyte *start1 = BI_BUF_BYTE_ADDRESS (buf, bi_ind);
3005     Bufbyte *dest = XSTRING_DATA (val);
3006
3007     if (len1 < 0)
3008       {
3009         /* Completely after gap */
3010         memcpy (dest, start1, bi_len);
3011       }
3012     else if (bi_len <= len1)
3013       {
3014         /* Completely before gap */
3015         memcpy (dest, start1, bi_len);
3016       }
3017     else
3018       {
3019         /* Spans gap */
3020         Bytind pos2 = bi_ind + len1;
3021         Bufbyte *start2 = BI_BUF_BYTE_ADDRESS (buf, pos2);
3022
3023         memcpy (dest, start1, len1);
3024         memcpy (dest + len1, start2, bi_len - len1);
3025       }
3026   }
3027
3028   UNGCPRO;
3029   return val;
3030 }
3031
3032 Lisp_Object
3033 make_string_from_buffer (struct buffer *buf, Bufpos pos, Charcount length)
3034 {
3035   return make_string_from_buffer_1 (buf, pos, length, 0);
3036 }
3037
3038 Lisp_Object
3039 make_string_from_buffer_no_extents (struct buffer *buf, Bufpos pos,
3040                                     Charcount length)
3041 {
3042   return make_string_from_buffer_1 (buf, pos, length, 1);
3043 }
3044
3045 void
3046 barf_if_buffer_read_only (struct buffer *buf, Bufpos from, Bufpos to)
3047 {
3048   Lisp_Object buffer;
3049   Lisp_Object iro;
3050
3051   XSETBUFFER (buffer, buf);
3052  back:
3053   iro = (buf == current_buffer ? Vinhibit_read_only :
3054          symbol_value_in_buffer (Qinhibit_read_only, buffer));
3055   if (!LISTP (iro))
3056     return;
3057   if (NILP (iro) && !NILP (buf->read_only))
3058     {
3059       Fsignal (Qbuffer_read_only, (list1 (buffer)));
3060       goto back;
3061     }
3062   if (from > 0)
3063     {
3064       if (to < 0)
3065         to = from;
3066       verify_extent_modification (buffer,
3067                                   bufpos_to_bytind (buf, from),
3068                                   bufpos_to_bytind (buf, to),
3069                                   iro);
3070     }
3071 }
3072
3073 void
3074 find_charsets_in_bufbyte_string (unsigned char *charsets, const Bufbyte *str,
3075                                  Bytecount len)
3076 {
3077 #ifndef MULE
3078   /* Telescope this. */
3079   charsets[0] = 1;
3080 #else
3081   const Bufbyte *strend = str + len;
3082   memset (charsets, 0, NUM_LEADING_BYTES);
3083
3084   /* #### SJT doesn't like this. */
3085   if (len == 0)
3086     {
3087       charsets[XCHARSET_LEADING_BYTE (Vcharset_ascii) - 128] = 1;
3088       return;
3089     }
3090
3091   while (str < strend)
3092     {
3093       charsets[CHAR_LEADING_BYTE (charptr_emchar (str)) - 128] = 1;
3094       INC_CHARPTR (str);
3095     }
3096 #endif
3097 }
3098
3099 void
3100 find_charsets_in_emchar_string (unsigned char *charsets, const Emchar *str,
3101                                 Charcount len)
3102 {
3103 #ifndef MULE
3104   /* Telescope this. */
3105   charsets[0] = 1;
3106 #else
3107   int i;
3108
3109   memset (charsets, 0, NUM_LEADING_BYTES);
3110
3111   /* #### SJT doesn't like this. */
3112   if (len == 0)
3113     {
3114       charsets[XCHARSET_LEADING_BYTE (Vcharset_ascii) - 128] = 1;
3115       return;
3116     }
3117
3118   for (i = 0; i < len; i++)
3119     {
3120       charsets[CHAR_LEADING_BYTE (str[i]) - 128] = 1;
3121     }
3122 #endif
3123 }
3124
3125 int
3126 bufbyte_string_displayed_columns (const Bufbyte *str, Bytecount len)
3127 {
3128   int cols = 0;
3129   const Bufbyte *end = str + len;
3130
3131   while (str < end)
3132     {
3133 #ifdef MULE
3134       Emchar ch = charptr_emchar (str);
3135       cols += XCHARSET_COLUMNS (CHAR_CHARSET (ch));
3136 #else
3137       cols++;
3138 #endif
3139       INC_CHARPTR (str);
3140     }
3141
3142   return cols;
3143 }
3144
3145 int
3146 emchar_string_displayed_columns (const Emchar *str, Charcount len)
3147 {
3148 #ifdef MULE
3149   int cols = 0;
3150   int i;
3151
3152   for (i = 0; i < len; i++)
3153     cols += XCHARSET_COLUMNS (CHAR_CHARSET (str[i]));
3154
3155   return cols;
3156 #else  /* not MULE */
3157   return len;
3158 #endif
3159 }
3160
3161 /* NOTE: Does not reset the Dynarr. */
3162
3163 void
3164 convert_bufbyte_string_into_emchar_dynarr (const Bufbyte *str, Bytecount len,
3165                                            Emchar_dynarr *dyn)
3166 {
3167   const Bufbyte *strend = str + len;
3168
3169   while (str < strend)
3170     {
3171       Emchar ch = charptr_emchar (str);
3172       Dynarr_add (dyn, ch);
3173       INC_CHARPTR (str);
3174     }
3175 }
3176
3177 Charcount
3178 convert_bufbyte_string_into_emchar_string (const Bufbyte *str, Bytecount len,
3179                                            Emchar *arr)
3180 {
3181   const Bufbyte *strend = str + len;
3182   Charcount newlen = 0;
3183   while (str < strend)
3184     {
3185       Emchar ch = charptr_emchar (str);
3186       arr[newlen++] = ch;
3187       INC_CHARPTR (str);
3188     }
3189   return newlen;
3190 }
3191
3192 /* Convert an array of Emchars into the equivalent string representation.
3193    Store into the given Bufbyte dynarr.  Does not reset the dynarr.
3194    Does not add a terminating zero. */
3195
3196 void
3197 convert_emchar_string_into_bufbyte_dynarr (Emchar *arr, int nels,
3198                                           Bufbyte_dynarr *dyn)
3199 {
3200   Bufbyte str[MAX_EMCHAR_LEN];
3201   int i;
3202
3203   for (i = 0; i < nels; i++)
3204     {
3205       Bytecount len = set_charptr_emchar (str, arr[i]);
3206       Dynarr_add_many (dyn, str, len);
3207     }
3208 }
3209
3210 /* Convert an array of Emchars into the equivalent string representation.
3211    Malloc the space needed for this and return it.  If LEN_OUT is not a
3212    NULL pointer, store into LEN_OUT the number of Bufbytes in the
3213    malloc()ed string.  Note that the actual number of Bufbytes allocated
3214    is one more than this: the returned string is zero-terminated. */
3215
3216 Bufbyte *
3217 convert_emchar_string_into_malloced_string (Emchar *arr, int nels,
3218                                            Bytecount *len_out)
3219 {
3220   /* Damn zero-termination. */
3221   Bufbyte *str = (Bufbyte *) alloca (nels * MAX_EMCHAR_LEN + 1);
3222   Bufbyte *strorig = str;
3223   Bytecount len;
3224
3225   int i;
3226
3227   for (i = 0; i < nels; i++)
3228     str += set_charptr_emchar (str, arr[i]);
3229   *str = '\0';
3230   len = str - strorig;
3231   str = (Bufbyte *) xmalloc (1 + len);
3232   memcpy (str, strorig, 1 + len);
3233   if (len_out)
3234     *len_out = len;
3235   return str;
3236 }
3237
3238 \f
3239 /************************************************************************/
3240 /*                            initialization                            */
3241 /************************************************************************/
3242
3243 void
3244 reinit_vars_of_insdel (void)
3245 {
3246   int i;
3247
3248   inside_change_hook = 0;
3249   in_first_change = 0;
3250
3251   for (i = 0; i <= MAX_BYTIND_GAP_SIZE_3; i++)
3252     three_to_one_table[i] = i / 3;
3253 }
3254
3255 void
3256 vars_of_insdel (void)
3257 {
3258   reinit_vars_of_insdel ();
3259 }
3260
3261 void
3262 init_buffer_text (struct buffer *b)
3263 {
3264   if (!b->base_buffer)
3265     {
3266       SET_BUF_GAP_SIZE (b, 20);
3267       BUFFER_ALLOC (b->text->beg, BUF_GAP_SIZE (b) + BUF_END_SENTINEL_SIZE);
3268       if (! BUF_BEG_ADDR (b))
3269         memory_full ();
3270
3271       SET_BUF_END_GAP_SIZE (b, 0);
3272       SET_BI_BUF_GPT (b, 1);
3273       SET_BOTH_BUF_Z (b, 1, 1);
3274       SET_GAP_SENTINEL (b);
3275       SET_END_SENTINEL (b);
3276 #ifdef MULE
3277       {
3278         int i;
3279
3280         b->text->mule_bufmin = b->text->mule_bufmax = 1;
3281         b->text->mule_bytmin = b->text->mule_bytmax = 1;
3282         b->text->mule_shifter = 0;
3283         b->text->mule_three_p = 0;
3284
3285         for (i = 0; i < 16; i++)
3286           {
3287             b->text->mule_bufpos_cache[i] = 1;
3288             b->text->mule_bytind_cache[i] = 1;
3289           }
3290       }
3291 #endif /* MULE */
3292       b->text->line_number_cache = Qnil;
3293
3294       BUF_MODIFF (b) = 1;
3295       BUF_SAVE_MODIFF (b) = 1;
3296
3297       JUST_SET_POINT (b, 1, 1);
3298       SET_BOTH_BUF_BEGV (b, 1, 1);
3299       SET_BOTH_BUF_ZV (b, 1, 1);
3300
3301       b->text->changes = xnew_and_zero (struct buffer_text_change_data);
3302     }
3303   else
3304     {
3305       JUST_SET_POINT (b, BUF_PT (b->base_buffer), BI_BUF_PT (b->base_buffer));
3306       SET_BOTH_BUF_BEGV (b, BUF_BEGV (b->base_buffer),
3307                          BI_BUF_BEGV (b->base_buffer));
3308       SET_BOTH_BUF_ZV (b, BUF_ZV (b->base_buffer),
3309                          BI_BUF_ZV (b->base_buffer));
3310     }
3311
3312   b->changes = xnew_and_zero (struct each_buffer_change_data);
3313   BUF_FACECHANGE (b) = 1;
3314
3315 #ifdef REGION_CACHE_NEEDS_WORK
3316   b->newline_cache = 0;
3317   b->width_run_cache = 0;
3318   b->width_table = Qnil;
3319 #endif
3320 }
3321
3322 void
3323 uninit_buffer_text (struct buffer *b)
3324 {
3325   if (!b->base_buffer)
3326     {
3327       BUFFER_FREE (b->text->beg);
3328       xfree (b->text->changes);
3329     }
3330   xfree (b->changes);
3331
3332 #ifdef REGION_CACHE_NEEDS_WORK
3333   if (b->newline_cache)
3334     {
3335       free_region_cache (b->newline_cache);
3336       b->newline_cache = 0;
3337     }
3338   if (b->width_run_cache)
3339     {
3340       free_region_cache (b->width_run_cache);
3341       b->width_run_cache = 0;
3342     }
3343   b->width_table = Qnil;
3344 #endif
3345 }