comparison src/insdel.c @ 412:697ef44129c6 r21-2-14

Import from CVS: tag r21-2-14
author cvs
date Mon, 13 Aug 2007 11:20:41 +0200
parents de805c49cfc1
children 11054d720c21
comparison
equal deleted inserted replaced
411:12e008d41344 412:697ef44129c6
198 Working with the various representations: 198 Working with the various representations:
199 ========================================= */ 199 ========================================= */
200 200
201 #include <config.h> 201 #include <config.h>
202 #include "lisp.h" 202 #include "lisp.h"
203 #include <limits.h>
203 204
204 #include "buffer.h" 205 #include "buffer.h"
205 #include "device.h" 206 #include "device.h"
206 #include "frame.h" 207 #include "frame.h"
207 #include "extents.h" 208 #include "extents.h"
301 302
302 /* Given a pointer to a text string and a length in bytes, return 303 /* Given a pointer to a text string and a length in bytes, return
303 the equivalent length in characters. */ 304 the equivalent length in characters. */
304 305
305 Charcount 306 Charcount
306 bytecount_to_charcount (const Bufbyte *ptr, Bytecount len) 307 bytecount_to_charcount (CONST Bufbyte *ptr, Bytecount len)
307 { 308 {
308 Charcount count = 0; 309 Charcount count = 0;
309 const Bufbyte *end = ptr + len; 310 CONST Bufbyte *end = ptr + len;
310 311
311 #if SIZEOF_LONG == 8 312 #if (LONGBITS == 32 || LONGBITS == 64)
312 # define STRIDE_TYPE long 313
313 # define HIGH_BIT_MASK 0x8080808080808080UL 314 # if (LONGBITS == 32)
314 #elif SIZEOF_LONG_LONG == 8 && !(defined (i386) || defined (__i386__)) 315 # define LONG_BYTES 4
315 # define STRIDE_TYPE long long 316 # define ALIGN_MASK 0xFFFFFFFCU
316 # define HIGH_BIT_MASK 0x8080808080808080ULL 317 # define HIGH_BIT_MASK 0x80808080U
317 #elif SIZEOF_LONG == 4 318 # else
318 # define STRIDE_TYPE long 319 # define LONG_BYTES 8
319 # define HIGH_BIT_MASK 0x80808080UL 320 # define ALIGN_MASK 0xFFFFFFFFFFFFFFF8UL
320 #else 321 /* I had a dream, I was being overrun with early Intel processors ... */
321 # error Add support for 128-bit systems here 322 # define HIGH_BIT_MASK 0x8080808080808080UL
322 #endif 323 # endif
323 324
324 #define ALIGN_BITS ((EMACS_UINT) (ALIGNOF (STRIDE_TYPE) - 1)) 325 /* When we have a large number of bytes to scan, we can be trickier
325 #define ALIGN_MASK (~ ALIGN_BITS) 326 and significantly faster by scanning them in chunks of the CPU word
326 #define ALIGNED(ptr) ((((EMACS_UINT) ptr) & ALIGN_BITS) == 0) 327 size (assuming that they're all ASCII -- we cut out as soon as
327 #define STRIDE sizeof (STRIDE_TYPE) 328 we find something non-ASCII). */
328 329 if (len >= 12)
330 {
331 /* Determine the section in the middle of the string that's
332 amenable to this treatment. Everything has to be aligned
333 on CPU word boundaries. */
334 CONST Bufbyte *aligned_ptr =
335 (CONST Bufbyte *) (((unsigned long) (ptr + LONG_BYTES - 1)) &
336 ALIGN_MASK);
337 CONST Bufbyte *aligned_end =
338 (CONST Bufbyte *) (((unsigned long) end) & ALIGN_MASK);
339
340 /* Handle unaligned stuff at the beginning. */
341 while (ptr < aligned_ptr)
342 {
343 if (!BYTE_ASCII_P (*ptr))
344 goto bail;
345 count++, ptr++;
346 }
347 /* Now do it. */
348 while (ptr < aligned_end)
349 {
350
351 if ((* (unsigned long *) ptr) & HIGH_BIT_MASK)
352 goto bail;
353 ptr += LONG_BYTES;
354 count += LONG_BYTES;
355 }
356 }
357
358 #endif /* LONGBITS == 32 || LONGBITS == 64 */
359
360 bail:
329 while (ptr < end) 361 while (ptr < end)
330 { 362 {
331 if (BYTE_ASCII_P (*ptr)) 363 count++;
332 { 364 INC_CHARPTR (ptr);
333 /* optimize for long stretches of ASCII */ 365 }
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 366 #ifdef ERROR_CHECK_BUFPOS
365 /* Bomb out if the specified substring ends in the middle 367 /* Bomb out if the specified substring ends in the middle
366 of a character. Note that we might have already gotten 368 of a character. Note that we might have already gotten
367 a core dump above from an invalid reference, but at least 369 a core dump above from an invalid reference, but at least
368 we will get no farther than here. */ 370 we will get no farther than here. */
374 376
375 /* Given a pointer to a text string and a length in characters, return 377 /* Given a pointer to a text string and a length in characters, return
376 the equivalent length in bytes. */ 378 the equivalent length in bytes. */
377 379
378 Bytecount 380 Bytecount
379 charcount_to_bytecount (const Bufbyte *ptr, Charcount len) 381 charcount_to_bytecount (CONST Bufbyte *ptr, Charcount len)
380 { 382 {
381 const Bufbyte *newptr = ptr; 383 CONST Bufbyte *newptr = ptr;
382 384
383 while (len > 0) 385 while (len > 0)
384 { 386 {
385 INC_CHARPTR (newptr); 387 INC_CHARPTR (newptr);
386 len--; 388 len--;
1601 1603
1602 static void 1604 static void
1603 adjust_markers (struct buffer *buf, Memind from, Memind to, 1605 adjust_markers (struct buffer *buf, Memind from, Memind to,
1604 Bytecount amount) 1606 Bytecount amount)
1605 { 1607 {
1606 Lisp_Marker *m; 1608 struct Lisp_Marker *m;
1607 1609
1608 for (m = BUF_MARKERS (buf); m; m = marker_next (m)) 1610 for (m = BUF_MARKERS (buf); m; m = marker_next (m))
1609 m->memind = do_marker_adjustment (m->memind, from, to, amount); 1611 m->memind = do_marker_adjustment (m->memind, from, to, amount);
1610 } 1612 }
1611 1613
1613 for an insertion of AMOUNT characters at POS. */ 1615 for an insertion of AMOUNT characters at POS. */
1614 1616
1615 static void 1617 static void
1616 adjust_markers_for_insert (struct buffer *buf, Memind ind, Bytecount amount) 1618 adjust_markers_for_insert (struct buffer *buf, Memind ind, Bytecount amount)
1617 { 1619 {
1618 Lisp_Marker *m; 1620 struct Lisp_Marker *m;
1619 1621
1620 for (m = BUF_MARKERS (buf); m; m = marker_next (m)) 1622 for (m = BUF_MARKERS (buf); m; m = marker_next (m))
1621 { 1623 {
1622 if (m->insertion_type && m->memind == ind) 1624 if (m->insertion_type && m->memind == ind)
1623 m->memind += amount; 1625 m->memind += amount;
1626 1628
1627 1629
1628 /************************************************************************/ 1630 /************************************************************************/
1629 /* Routines for dealing with the gap */ 1631 /* Routines for dealing with the gap */
1630 /************************************************************************/ 1632 /************************************************************************/
1633
1634 /* XEmacs requires an ANSI C compiler, and it damn well better have a
1635 working memmove() */
1636 #define GAP_USE_BCOPY
1637 #ifdef BCOPY_UPWARD_SAFE
1638 # undef BCOPY_UPWARD_SAFE
1639 #endif
1640 #ifdef BCOPY_DOWNWARD_SAFE
1641 # undef BCOPY_DOWNWARD_SAFE
1642 #endif
1643 #define BCOPY_UPWARD_SAFE 1
1644 #define BCOPY_DOWNWARD_SAFE 1
1631 1645
1632 /* maximum amount of memory moved in a single chunk. Increasing this 1646 /* maximum amount of memory moved in a single chunk. Increasing this
1633 value improves gap-motion efficiency but decreases QUIT responsiveness 1647 value improves gap-motion efficiency but decreases QUIT responsiveness
1634 time. Was 32000 but today's processors are faster and files are 1648 time. Was 32000 but today's processors are faster and files are
1635 bigger. --ben */ 1649 bigger. --ben */
1667 break; 1681 break;
1668 } 1682 }
1669 /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */ 1683 /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */
1670 if (i > GAP_MOVE_CHUNK) 1684 if (i > GAP_MOVE_CHUNK)
1671 i = GAP_MOVE_CHUNK; 1685 i = GAP_MOVE_CHUNK;
1672 1686 #ifdef GAP_USE_BCOPY
1673 if (i >= 128) 1687 if (i >= 128
1674 { 1688 /* bcopy is safe if the two areas of memory do not overlap
1689 or on systems where bcopy is always safe for moving upward. */
1690 && (BCOPY_UPWARD_SAFE
1691 || to - from >= 128))
1692 {
1693 /* If overlap is not safe, avoid it by not moving too many
1694 characters at once. */
1695 if (!BCOPY_UPWARD_SAFE && i > to - from)
1696 i = to - from;
1675 new_s1 -= i; 1697 new_s1 -= i;
1676 from -= i; 1698 from -= i, to -= i;
1677 to -= i;
1678 memmove (to, from, i); 1699 memmove (to, from, i);
1679 } 1700 }
1680 else 1701 else
1702 #endif
1681 { 1703 {
1682 new_s1 -= i; 1704 new_s1 -= i;
1683 while (--i >= 0) 1705 while (--i >= 0)
1684 *--to = *--from; 1706 *--to = *--from;
1685 } 1707 }
1738 break; 1760 break;
1739 } 1761 }
1740 /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */ 1762 /* Move at most GAP_MOVE_CHUNK chars before checking again for a quit. */
1741 if (i > GAP_MOVE_CHUNK) 1763 if (i > GAP_MOVE_CHUNK)
1742 i = GAP_MOVE_CHUNK; 1764 i = GAP_MOVE_CHUNK;
1743 1765 #ifdef GAP_USE_BCOPY
1744 if (i >= 128) 1766 if (i >= 128
1745 { 1767 /* bcopy is safe if the two areas of memory do not overlap
1768 or on systems where bcopy is always safe for moving downward. */
1769 && (BCOPY_DOWNWARD_SAFE
1770 || from - to >= 128))
1771 {
1772 /* If overlap is not safe, avoid it by not moving too many
1773 characters at once. */
1774 if (!BCOPY_DOWNWARD_SAFE && i > from - to)
1775 i = from - to;
1746 new_s1 += i; 1776 new_s1 += i;
1747 memmove (to, from, i); 1777 memmove (to, from, i);
1748 from += i; 1778 from += i, to += i;
1749 to += i;
1750 } 1779 }
1751 else 1780 else
1781 #endif
1752 { 1782 {
1753 new_s1 += i; 1783 new_s1 += i;
1754 while (--i >= 0) 1784 while (--i >= 0)
1755 *to++ = *from++; 1785 *to++ = *from++;
1756 } 1786 }
2025 with the correct arguments. The after-change region is calculated 2055 with the correct arguments. The after-change region is calculated
2026 automatically, however, and if changes somehow or other happen outside 2056 automatically, however, and if changes somehow or other happen outside
2027 of the specified region, that will also be handled correctly. 2057 of the specified region, that will also be handled correctly.
2028 2058
2029 begin_multiple_change() returns a number (actually a specpdl depth) 2059 begin_multiple_change() returns a number (actually a specpdl depth)
2030 that you must pass to end_multiple_change() when you are done. 2060 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 2061
2037 int 2062 int
2038 begin_multiple_change (struct buffer *buf, Bufpos start, Bufpos end) 2063 begin_multiple_change (struct buffer *buf, Bufpos start, Bufpos end)
2039 { 2064 {
2040 /* This function can GC */ 2065 /* This function can GC */
2078 change_function_restore (Lisp_Object buffer) 2103 change_function_restore (Lisp_Object buffer)
2079 { 2104 {
2080 /* We should first reset the variable and then change the buffer, 2105 /* We should first reset the variable and then change the buffer,
2081 because Fset_buffer() can throw. */ 2106 because Fset_buffer() can throw. */
2082 inside_change_hook = 0; 2107 inside_change_hook = 0;
2083 if (XBUFFER (buffer) != current_buffer) 2108 Fset_buffer (buffer);
2084 Fset_buffer (buffer);
2085 return Qnil; 2109 return Qnil;
2086 } 2110 }
2087 2111
2088 static int in_first_change; 2112 static int in_first_change;
2089 2113
2129 Lisp_Object bufcons; 2153 Lisp_Object bufcons;
2130 2154
2131 if (!inside_change_hook) 2155 if (!inside_change_hook)
2132 { 2156 {
2133 Lisp_Object buffer; 2157 Lisp_Object buffer;
2134 int speccount;
2135 2158
2136 /* Are we in a multiple-change session? */ 2159 /* Are we in a multiple-change session? */
2137 if (buf->text->changes->in_multiple_change && 2160 if (buf->text->changes->in_multiple_change &&
2138 buf->text->changes->mc_begin != 0) 2161 buf->text->changes->mc_begin != 0)
2139 { 2162 {
2167 signal_first_change (mbuf); 2190 signal_first_change (mbuf);
2168 } 2191 }
2169 } 2192 }
2170 2193
2171 /* Now in any case run the before-change-functions if any. */ 2194 /* 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 2195
2176 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons) 2196 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2177 { 2197 {
2178 XSETBUFFER (buffer, mbuf); 2198 XSETBUFFER (buffer, mbuf);
2179 if (!NILP (symbol_value_in_buffer (Qbefore_change_functions, buffer)) 2199 if (!NILP (symbol_value_in_buffer (Qbefore_change_functions, buffer))
2180 /* Obsolete, for compatibility */ 2200 /* Obsolete, for compatibility */
2181 || !NILP (symbol_value_in_buffer (Qbefore_change_function, buffer))) 2201 || !NILP (symbol_value_in_buffer (Qbefore_change_function, buffer)))
2182 { 2202 {
2203 int speccount = specpdl_depth ();
2204 record_unwind_protect (change_function_restore, Fcurrent_buffer ());
2183 set_buffer_internal (buf); 2205 set_buffer_internal (buf);
2206 inside_change_hook = 1;
2184 va_run_hook_with_args (Qbefore_change_functions, 2, 2207 va_run_hook_with_args (Qbefore_change_functions, 2,
2185 make_int (start), make_int (end)); 2208 make_int (start), make_int (end));
2186 /* Obsolete, for compatibility */ 2209 /* Obsolete, for compatibility */
2187 va_run_hook_with_args (Qbefore_change_function, 2, 2210 va_run_hook_with_args (Qbefore_change_function, 2,
2188 make_int (start), make_int (end)); 2211 make_int (start), make_int (end));
2212 unbind_to (speccount, Qnil);
2189 } 2213 }
2190 } 2214 }
2191 2215
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) 2216 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2200 { 2217 {
2201 XSETBUFFER (buffer, mbuf); 2218 XSETBUFFER (buffer, mbuf);
2202 report_extent_modification (buffer, start, end, 0); 2219 report_extent_modification (buffer, start, end,
2203 } 2220 &inside_change_hook, 0);
2204 unbind_to (speccount, Qnil); 2221 }
2205 2222
2206 /* Only now do we indicate that the before-change-functions have 2223 /* Only now do we indicate that the before-change-functions have
2207 been called, in case some function throws out. */ 2224 been called, in case some function throws out. */
2208 buf->text->changes->mc_begin_signaled = 1; 2225 buf->text->changes->mc_begin_signaled = 1;
2209 } 2226 }
2236 } 2253 }
2237 2254
2238 if (!inside_change_hook) 2255 if (!inside_change_hook)
2239 { 2256 {
2240 Lisp_Object buffer; 2257 Lisp_Object buffer;
2241 int speccount;
2242 2258
2243 if (buf->text->changes->in_multiple_change && 2259 if (buf->text->changes->in_multiple_change &&
2244 buf->text->changes->mc_begin != 0) 2260 buf->text->changes->mc_begin != 0)
2245 { 2261 {
2246 assert (start >= buf->text->changes->mc_begin && 2262 assert (start >= buf->text->changes->mc_begin &&
2249 orig_end <= buf->text->changes->mc_new_end); 2265 orig_end <= buf->text->changes->mc_new_end);
2250 buf->text->changes->mc_new_end += new_end - orig_end; 2266 buf->text->changes->mc_new_end += new_end - orig_end;
2251 return; /* after-change-functions signalled when all changes done */ 2267 return; /* after-change-functions signalled when all changes done */
2252 } 2268 }
2253 2269
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) 2270 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2258 { 2271 {
2259 XSETBUFFER (buffer, mbuf); 2272 XSETBUFFER (buffer, mbuf);
2260 2273
2261 if (!NILP (symbol_value_in_buffer (Qafter_change_functions, buffer)) 2274 if (!NILP (symbol_value_in_buffer (Qafter_change_functions, buffer))
2262 /* Obsolete, for compatibility */ 2275 /* Obsolete, for compatibility */
2263 || !NILP (symbol_value_in_buffer (Qafter_change_function, buffer))) 2276 || !NILP (symbol_value_in_buffer (Qafter_change_function, buffer)))
2264 { 2277 {
2278 int speccount = specpdl_depth ();
2279 record_unwind_protect (change_function_restore, Fcurrent_buffer ());
2265 set_buffer_internal (buf); 2280 set_buffer_internal (buf);
2281 inside_change_hook = 1;
2266 /* The actual after-change functions take slightly 2282 /* The actual after-change functions take slightly
2267 different arguments than what we were passed. */ 2283 different arguments than what we were passed. */
2268 va_run_hook_with_args (Qafter_change_functions, 3, 2284 va_run_hook_with_args (Qafter_change_functions, 3,
2269 make_int (start), make_int (new_end), 2285 make_int (start), make_int (new_end),
2270 make_int (orig_end - start)); 2286 make_int (orig_end - start));
2271 /* Obsolete, for compatibility */ 2287 /* Obsolete, for compatibility */
2272 va_run_hook_with_args (Qafter_change_function, 3, 2288 va_run_hook_with_args (Qafter_change_function, 3,
2273 make_int (start), make_int (new_end), 2289 make_int (start), make_int (new_end),
2274 make_int (orig_end - start)); 2290 make_int (orig_end - start));
2291 unbind_to (speccount, Qnil);
2275 } 2292 }
2276 } 2293 }
2277 2294
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) 2295 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2288 { 2296 {
2289 XSETBUFFER (buffer, mbuf); 2297 XSETBUFFER (buffer, mbuf);
2290 report_extent_modification (buffer, start, new_end, 1); 2298 report_extent_modification (buffer, start, new_end,
2291 } 2299 &inside_change_hook, 1);
2292 unbind_to (speccount, Qnil); /* sets inside_change_hook back to 0 */ 2300 }
2293 } 2301 }
2294 } 2302 }
2295 2303
2296 /* Call this if you're about to change the region of BUFFER from START 2304 /* 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 2305 to END. This checks the read-only properties of the region, calls
2367 /************************************************************************/ 2375 /************************************************************************/
2368 /* Insertion of strings */ 2376 /* Insertion of strings */
2369 /************************************************************************/ 2377 /************************************************************************/
2370 2378
2371 void 2379 void
2372 fixup_internal_substring (const Bufbyte *nonreloc, Lisp_Object reloc, 2380 fixup_internal_substring (CONST Bufbyte *nonreloc, Lisp_Object reloc,
2373 Bytecount offset, Bytecount *len) 2381 Bytecount offset, Bytecount *len)
2374 { 2382 {
2375 assert ((nonreloc && NILP (reloc)) || (!nonreloc && STRINGP (reloc))); 2383 assert ((nonreloc && NILP (reloc)) || (!nonreloc && STRINGP (reloc)));
2376 2384
2377 if (*len < 0) 2385 if (*len < 0)
2378 { 2386 {
2379 if (nonreloc) 2387 if (nonreloc)
2380 *len = strlen ((const char *) nonreloc) - offset; 2388 *len = strlen ((CONST char *) nonreloc) - offset;
2381 else 2389 else
2382 *len = XSTRING_LENGTH (reloc) - offset; 2390 *len = XSTRING_LENGTH (reloc) - offset;
2383 } 2391 }
2384 #ifdef ERROR_CHECK_BUFPOS 2392 #ifdef ERROR_CHECK_BUFPOS
2385 assert (*len >= 0); 2393 assert (*len >= 0);
2409 before modifying a buffer. Similar checks were already done 2417 before modifying a buffer. Similar checks were already done
2410 in the higher-level Lisp functions calling insert-file-contents. */ 2418 in the higher-level Lisp functions calling insert-file-contents. */
2411 2419
2412 Charcount 2420 Charcount
2413 buffer_insert_string_1 (struct buffer *buf, Bufpos pos, 2421 buffer_insert_string_1 (struct buffer *buf, Bufpos pos,
2414 const Bufbyte *nonreloc, Lisp_Object reloc, 2422 CONST Bufbyte *nonreloc, Lisp_Object reloc,
2415 Bytecount offset, Bytecount length, 2423 Bytecount offset, Bytecount length,
2416 int flags) 2424 int flags)
2417 { 2425 {
2418 /* This function can GC */ 2426 /* This function can GC */
2419 struct gcpro gcpro1; 2427 struct gcpro gcpro1;
2574 to take place. (If POS is -1, text is inserted at point and point 2582 to take place. (If POS is -1, text is inserted at point and point
2575 moves forward past the text.) FLAGS is as above. */ 2583 moves forward past the text.) FLAGS is as above. */
2576 2584
2577 Charcount 2585 Charcount
2578 buffer_insert_raw_string_1 (struct buffer *buf, Bufpos pos, 2586 buffer_insert_raw_string_1 (struct buffer *buf, Bufpos pos,
2579 const Bufbyte *nonreloc, Bytecount length, 2587 CONST Bufbyte *nonreloc, Bytecount length,
2580 int flags) 2588 int flags)
2581 { 2589 {
2582 /* This function can GC */ 2590 /* This function can GC */
2583 return buffer_insert_string_1 (buf, pos, nonreloc, Qnil, 0, length, 2591 return buffer_insert_string_1 (buf, pos, nonreloc, Qnil, 0, length,
2584 flags); 2592 flags);
2598 } 2606 }
2599 2607
2600 /* Insert the null-terminated string S (in external format). */ 2608 /* Insert the null-terminated string S (in external format). */
2601 2609
2602 Charcount 2610 Charcount
2603 buffer_insert_c_string_1 (struct buffer *buf, Bufpos pos, const char *s, 2611 buffer_insert_c_string_1 (struct buffer *buf, Bufpos pos, CONST char *s,
2604 int flags) 2612 int flags)
2605 { 2613 {
2606 /* This function can GC */ 2614 /* This function can GC */
2607 const char *translated = GETTEXT (s); 2615 CONST char *translated = GETTEXT (s);
2608 return buffer_insert_string_1 (buf, pos, (const Bufbyte *) translated, Qnil, 2616 return buffer_insert_string_1 (buf, pos, (CONST Bufbyte *) translated, Qnil,
2609 0, strlen (translated), flags); 2617 0, strlen (translated), flags);
2610 } 2618 }
2611 2619
2612 Charcount 2620 Charcount
2613 buffer_insert_emacs_char_1 (struct buffer *buf, Bufpos pos, Emchar ch, 2621 buffer_insert_emacs_char_1 (struct buffer *buf, Bufpos pos, Emchar ch,
3069 iro); 3077 iro);
3070 } 3078 }
3071 } 3079 }
3072 3080
3073 void 3081 void
3074 find_charsets_in_bufbyte_string (unsigned char *charsets, const Bufbyte *str, 3082 find_charsets_in_bufbyte_string (unsigned char *charsets, CONST Bufbyte *str,
3075 Bytecount len) 3083 Bytecount len)
3076 { 3084 {
3077 #ifndef MULE 3085 #ifndef MULE
3078 /* Telescope this. */ 3086 /* Telescope this. */
3079 charsets[0] = 1; 3087 charsets[0] = 1;
3080 #else 3088 #else
3081 const Bufbyte *strend = str + len; 3089 CONST Bufbyte *strend = str + len;
3082 memset (charsets, 0, NUM_LEADING_BYTES); 3090 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
3091 while (str < strend) 3092 while (str < strend)
3092 { 3093 {
3093 charsets[CHAR_LEADING_BYTE (charptr_emchar (str)) - 128] = 1; 3094 charsets[CHAR_LEADING_BYTE (charptr_emchar (str)) - 128] = 1;
3094 INC_CHARPTR (str); 3095 INC_CHARPTR (str);
3095 } 3096 }
3096 #endif 3097 #endif
3097 } 3098 }
3098 3099
3099 void 3100 void
3100 find_charsets_in_emchar_string (unsigned char *charsets, const Emchar *str, 3101 find_charsets_in_emchar_string (unsigned char *charsets, CONST Emchar *str,
3101 Charcount len) 3102 Charcount len)
3102 { 3103 {
3103 #ifndef MULE 3104 #ifndef MULE
3104 /* Telescope this. */ 3105 /* Telescope this. */
3105 charsets[0] = 1; 3106 charsets[0] = 1;
3106 #else 3107 #else
3107 int i; 3108 int i;
3108 3109
3109 memset (charsets, 0, NUM_LEADING_BYTES); 3110 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++) 3111 for (i = 0; i < len; i++)
3119 { 3112 {
3120 charsets[CHAR_LEADING_BYTE (str[i]) - 128] = 1; 3113 charsets[CHAR_LEADING_BYTE (str[i]) - 128] = 1;
3121 } 3114 }
3122 #endif 3115 #endif
3123 } 3116 }
3124 3117
3125 int 3118 int
3126 bufbyte_string_displayed_columns (const Bufbyte *str, Bytecount len) 3119 bufbyte_string_displayed_columns (CONST Bufbyte *str, Bytecount len)
3127 { 3120 {
3128 int cols = 0; 3121 int cols = 0;
3129 const Bufbyte *end = str + len; 3122 CONST Bufbyte *end = str + len;
3130 3123
3131 while (str < end) 3124 while (str < end)
3132 { 3125 {
3133 #ifdef MULE 3126 #ifdef MULE
3134 Emchar ch = charptr_emchar (str); 3127 Emchar ch = charptr_emchar (str);
3141 3134
3142 return cols; 3135 return cols;
3143 } 3136 }
3144 3137
3145 int 3138 int
3146 emchar_string_displayed_columns (const Emchar *str, Charcount len) 3139 emchar_string_displayed_columns (CONST Emchar *str, Charcount len)
3147 { 3140 {
3148 #ifdef MULE 3141 #ifdef MULE
3149 int cols = 0; 3142 int cols = 0;
3150 int i; 3143 int i;
3151 3144
3159 } 3152 }
3160 3153
3161 /* NOTE: Does not reset the Dynarr. */ 3154 /* NOTE: Does not reset the Dynarr. */
3162 3155
3163 void 3156 void
3164 convert_bufbyte_string_into_emchar_dynarr (const Bufbyte *str, Bytecount len, 3157 convert_bufbyte_string_into_emchar_dynarr (CONST Bufbyte *str, Bytecount len,
3165 Emchar_dynarr *dyn) 3158 Emchar_dynarr *dyn)
3166 { 3159 {
3167 const Bufbyte *strend = str + len; 3160 CONST Bufbyte *strend = str + len;
3168 3161
3169 while (str < strend) 3162 while (str < strend)
3170 { 3163 {
3171 Emchar ch = charptr_emchar (str); 3164 Emchar ch = charptr_emchar (str);
3172 Dynarr_add (dyn, ch); 3165 Dynarr_add (dyn, ch);
3173 INC_CHARPTR (str); 3166 INC_CHARPTR (str);
3174 } 3167 }
3175 } 3168 }
3176 3169
3177 Charcount 3170 Charcount
3178 convert_bufbyte_string_into_emchar_string (const Bufbyte *str, Bytecount len, 3171 convert_bufbyte_string_into_emchar_string (CONST Bufbyte *str, Bytecount len,
3179 Emchar *arr) 3172 Emchar *arr)
3180 { 3173 {
3181 const Bufbyte *strend = str + len; 3174 CONST Bufbyte *strend = str + len;
3182 Charcount newlen = 0; 3175 Charcount newlen = 0;
3183 while (str < strend) 3176 while (str < strend)
3184 { 3177 {
3185 Emchar ch = charptr_emchar (str); 3178 Emchar ch = charptr_emchar (str);
3186 arr[newlen++] = ch; 3179 arr[newlen++] = ch;
3239 /************************************************************************/ 3232 /************************************************************************/
3240 /* initialization */ 3233 /* initialization */
3241 /************************************************************************/ 3234 /************************************************************************/
3242 3235
3243 void 3236 void
3244 reinit_vars_of_insdel (void) 3237 vars_of_insdel (void)
3245 { 3238 {
3246 int i; 3239 int i;
3247 3240
3248 inside_change_hook = 0; 3241 inside_change_hook = 0;
3249 in_first_change = 0; 3242 in_first_change = 0;
3250 3243
3251 for (i = 0; i <= MAX_BYTIND_GAP_SIZE_3; i++) 3244 for (i = 0; i <= MAX_BYTIND_GAP_SIZE_3; i++)
3252 three_to_one_table[i] = i / 3; 3245 three_to_one_table[i] = i / 3;
3253 }
3254
3255 void
3256 vars_of_insdel (void)
3257 {
3258 reinit_vars_of_insdel ();
3259 } 3246 }
3260 3247
3261 void 3248 void
3262 init_buffer_text (struct buffer *b) 3249 init_buffer_text (struct buffer *b)
3263 { 3250 {