comparison src/insdel.c @ 665:fdefd0186b75

[xemacs-hg @ 2001-09-20 06:28:42 by ben] The great integral types renaming. The purpose of this is to rationalize the names used for various integral types, so that they match their intended uses and follow consist conventions, and eliminate types that were not semantically different from each other. The conventions are: -- All integral types that measure quantities of anything are signed. Some people disagree vociferously with this, but their arguments are mostly theoretical, and are vastly outweighed by the practical headaches of mixing signed and unsigned values, and more importantly by the far increased likelihood of inadvertent bugs: Because of the broken "viral" nature of unsigned quantities in C (operations involving mixed signed/unsigned are done unsigned, when exactly the opposite is nearly always wanted), even a single error in declaring a quantity unsigned that should be signed, or even the even more subtle error of comparing signed and unsigned values and forgetting the necessary cast, can be catastrophic, as comparisons will yield wrong results. -Wsign-compare is turned on specifically to catch this, but this tends to result in a great number of warnings when mixing signed and unsigned, and the casts are annoying. More has been written on this elsewhere. -- All such quantity types just mentioned boil down to EMACS_INT, which is 32 bits on 32-bit machines and 64 bits on 64-bit machines. This is guaranteed to be the same size as Lisp objects of type `int', and (as far as I can tell) of size_t (unsigned!) and ssize_t. The only type below that is not an EMACS_INT is Hashcode, which is an unsigned value of the same size as EMACS_INT. -- Type names should be relatively short (no more than 10 characters or so), with the first letter capitalized and no underscores if they can at all be avoided. -- "count" == a zero-based measurement of some quantity. Includes sizes, offsets, and indexes. -- "bpos" == a one-based measurement of a position in a buffer. "Charbpos" and "Bytebpos" count text in the buffer, rather than bytes in memory; thus Bytebpos does not directly correspond to the memory representation. Use "Membpos" for this. -- "Char" refers to internal-format characters, not to the C type "char", which is really a byte. -- For the actual name changes, see the script below. I ran the following script to do the conversion. (NOTE: This script is idempotent. You can safely run it multiple times and it will not screw up previous results -- in fact, it will do nothing if nothing has changed. Thus, it can be run repeatedly as necessary to handle patches coming in from old workspaces, or old branches.) There are two tags, just before and just after the change: `pre-integral-type-rename' and `post-integral-type-rename'. When merging code from the main trunk into a branch, the best thing to do is first merge up to `pre-integral-type-rename', then apply the script and associated changes, then merge from `post-integral-type-change' to the present. (Alternatively, just do the merging in one operation; but you may then have a lot of conflicts needing to be resolved by hand.) Script `fixtypes.sh' follows: ----------------------------------- cut ------------------------------------ files="*.[ch] s/*.h m/*.h config.h.in ../configure.in Makefile.in.in ../lib-src/*.[ch] ../lwlib/*.[ch]" gr Memory_Count Bytecount $files gr Lstream_Data_Count Bytecount $files gr Element_Count Elemcount $files gr Hash_Code Hashcode $files gr extcount bytecount $files gr bufpos charbpos $files gr bytind bytebpos $files gr memind membpos $files gr bufbyte intbyte $files gr Extcount Bytecount $files gr Bufpos Charbpos $files gr Bytind Bytebpos $files gr Memind Membpos $files gr Bufbyte Intbyte $files gr EXTCOUNT BYTECOUNT $files gr BUFPOS CHARBPOS $files gr BYTIND BYTEBPOS $files gr MEMIND MEMBPOS $files gr BUFBYTE INTBYTE $files gr MEMORY_COUNT BYTECOUNT $files gr LSTREAM_DATA_COUNT BYTECOUNT $files gr ELEMENT_COUNT ELEMCOUNT $files gr HASH_CODE HASHCODE $files ----------------------------------- cut ------------------------------------ `fixtypes.sh' is a Bourne-shell script; it uses 'gr': ----------------------------------- cut ------------------------------------ #!/bin/sh # Usage is like this: # gr FROM TO FILES ... # globally replace FROM with TO in FILES. FROM and TO are regular expressions. # backup files are stored in the `backup' directory. from="$1" to="$2" shift 2 echo ${1+"$@"} | xargs global-replace "s/$from/$to/g" ----------------------------------- cut ------------------------------------ `gr' in turn uses a Perl script to do its real work, `global-replace', which follows: ----------------------------------- cut ------------------------------------ : #-*- Perl -*- ### global-modify --- modify the contents of a file by a Perl expression ## Copyright (C) 1999 Martin Buchholz. ## Copyright (C) 2001 Ben Wing. ## Authors: Martin Buchholz <martin@xemacs.org>, Ben Wing <ben@xemacs.org> ## Maintainer: Ben Wing <ben@xemacs.org> ## Current Version: 1.0, May 5, 2001 # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with XEmacs; see the file COPYING. If not, write to the Free # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA. eval 'exec perl -w -S $0 ${1+"$@"}' if 0; use strict; use FileHandle; use Carp; use Getopt::Long; use File::Basename; (my $myName = $0) =~ s@.*/@@; my $usage=" Usage: $myName [--help] [--backup-dir=DIR] [--line-mode] [--hunk-mode] PERLEXPR FILE ... Globally modify a file, either line by line or in one big hunk. Typical usage is like this: [with GNU print, GNU xargs: guaranteed to handle spaces, quotes, etc. in file names] find . -name '*.[ch]' -print0 | xargs -0 $0 's/\bCONST\b/const/g'\n [with non-GNU print, xargs] find . -name '*.[ch]' -print | xargs $0 's/\bCONST\b/const/g'\n The file is read in, either line by line (with --line-mode specified) or in one big hunk (with --hunk-mode specified; it's the default), and the Perl expression is then evalled with \$_ set to the line or hunk of text, including the terminating newline if there is one. It should destructively modify the value there, storing the changed result in \$_. Files in which any modifications are made are backed up to the directory specified using --backup-dir, or to `backup' by default. To disable this, use --backup-dir= with no argument. Hunk mode is the default because it is MUCH MUCH faster than line-by-line. Use line-by-line only when it matters, e.g. you want to do a replacement only once per line (the default without the `g' argument). Conversely, when using hunk mode, *ALWAYS* use `g'; otherwise, you will only make one replacement in the entire file! "; my %options = (); $Getopt::Long::ignorecase = 0; &GetOptions ( \%options, 'help', 'backup-dir=s', 'line-mode', 'hunk-mode', ); die $usage if $options{"help"} or @ARGV <= 1; my $code = shift; die $usage if grep (-d || ! -w, @ARGV); sub SafeOpen { open ((my $fh = new FileHandle), $_[0]); confess "Can't open $_[0]: $!" if ! defined $fh; return $fh; } sub SafeClose { close $_[0] or confess "Can't close $_[0]: $!"; } sub FileContents { my $fh = SafeOpen ("< $_[0]"); my $olddollarslash = $/; local $/ = undef; my $contents = <$fh>; $/ = $olddollarslash; return $contents; } sub WriteStringToFile { my $fh = SafeOpen ("> $_[0]"); binmode $fh; print $fh $_[1] or confess "$_[0]: $!\n"; SafeClose $fh; } foreach my $file (@ARGV) { my $changed_p = 0; my $new_contents = ""; if ($options{"line-mode"}) { my $fh = SafeOpen $file; while (<$fh>) { my $save_line = $_; eval $code; $changed_p = 1 if $save_line ne $_; $new_contents .= $_; } } else { my $orig_contents = $_ = FileContents $file; eval $code; if ($_ ne $orig_contents) { $changed_p = 1; $new_contents = $_; } } if ($changed_p) { my $backdir = $options{"backup-dir"}; $backdir = "backup" if !defined ($backdir); if ($backdir) { my ($name, $path, $suffix) = fileparse ($file, ""); my $backfulldir = $path . $backdir; my $backfile = "$backfulldir/$name"; mkdir $backfulldir, 0755 unless -d $backfulldir; print "modifying $file (original saved in $backfile)\n"; rename $file, $backfile; } WriteStringToFile ($file, $new_contents); } } ----------------------------------- cut ------------------------------------ In addition to those programs, I needed to fix up a few other things, particularly relating to the duplicate definitions of types, now that some types merged with others. Specifically: 1. in lisp.h, removed duplicate declarations of Bytecount. The changed code should now look like this: (In each code snippet below, the first and last lines are the same as the original, as are all lines outside of those lines. That allows you to locate the section to be replaced, and replace the stuff in that section, verifying that there isn't anything new added that would need to be kept.) --------------------------------- snip ------------------------------------- /* Counts of bytes or chars */ typedef EMACS_INT Bytecount; typedef EMACS_INT Charcount; /* Counts of elements */ typedef EMACS_INT Elemcount; /* Hash codes */ typedef unsigned long Hashcode; /* ------------------------ dynamic arrays ------------------- */ --------------------------------- snip ------------------------------------- 2. in lstream.h, removed duplicate declaration of Bytecount. Rewrote the comment about this type. The changed code should now look like this: --------------------------------- snip ------------------------------------- #endif /* The have been some arguments over the what the type should be that specifies a count of bytes in a data block to be written out or read in, using Lstream_read(), Lstream_write(), and related functions. Originally it was long, which worked fine; Martin "corrected" these to size_t and ssize_t on the grounds that this is theoretically cleaner and is in keeping with the C standards. Unfortunately, this practice is horribly error-prone due to design flaws in the way that mixed signed/unsigned arithmetic happens. In fact, by doing this change, Martin introduced a subtle but fatal error that caused the operation of sending large mail messages to the SMTP server under Windows to fail. By putting all values back to be signed, avoiding any signed/unsigned mixing, the bug immediately went away. The type then in use was Lstream_Data_Count, so that it be reverted cleanly if a vote came to that. Now it is Bytecount. Some earlier comments about why the type must be signed: This MUST BE SIGNED, since it also is used in functions that return the number of bytes actually read to or written from in an operation, and these functions can return -1 to signal error. Note that the standard Unix read() and write() functions define the count going in as a size_t, which is UNSIGNED, and the count going out as an ssize_t, which is SIGNED. This is a horrible design flaw. Not only is it highly likely to lead to logic errors when a -1 gets interpreted as a large positive number, but operations are bound to fail in all sorts of horrible ways when a number in the upper-half of the size_t range is passed in -- this number is unrepresentable as an ssize_t, so code that checks to see how many bytes are actually written (which is mandatory if you are dealing with certain types of devices) will get completely screwed up. --ben */ typedef enum lstream_buffering --------------------------------- snip ------------------------------------- 3. in dumper.c, there are four places, all inside of switch() statements, where XD_BYTECOUNT appears twice as a case tag. In each case, the two case blocks contain identical code, and you should *REMOVE THE SECOND* and leave the first.
author ben
date Thu, 20 Sep 2001 06:31:11 +0000
parents b39c14581166
children 760db937b9ee
comparison
equal deleted inserted replaced
664:6e99cc8c6ca5 665:fdefd0186b75
29 /* 29 /*
30 There are three possible ways to specify positions in a buffer. All 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 31 of these are one-based: the beginning of the buffer is position or
32 index 1, and 0 is not a valid position. 32 index 1, and 0 is not a valid position.
33 33
34 As a "buffer position" (typedef Bufpos): 34 As a "buffer position" (typedef Charbpos):
35 35
36 This is an index specifying an offset in characters from the 36 This is an index specifying an offset in characters from the
37 beginning of the buffer. Note that buffer positions are 37 beginning of the buffer. Note that buffer positions are
38 logically *between* characters, not on a character. The 38 logically *between* characters, not on a character. The
39 difference between two buffer positions specifies the number of 39 difference between two buffer positions specifies the number of
40 characters between those positions. Buffer positions are the 40 characters between those positions. Buffer positions are the
41 only kind of position externally visible to the user. 41 only kind of position externally visible to the user.
42 42
43 As a "byte index" (typedef Bytind): 43 As a "byte index" (typedef Bytebpos):
44 44
45 This is an index over the bytes used to represent the characters 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 46 in the buffer. If there is no Mule support, this is identical
47 to a buffer position, because each character is represented 47 to a buffer position, because each character is represented
48 using one byte. However, with Mule support, many characters 48 using one byte. However, with Mule support, many characters
49 require two or more bytes for their representation, and so a 49 require two or more bytes for their representation, and so a
50 byte index may be greater than the corresponding buffer 50 byte index may be greater than the corresponding buffer
51 position. 51 position.
52 52
53 As a "memory index" (typedef Memind): 53 As a "memory index" (typedef Membpos):
54 54
55 This is the byte index adjusted for the gap. For positions 55 This is the byte index adjusted for the gap. For positions
56 before the gap, this is identical to the byte index. For 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 57 positions after the gap, this is the byte index plus the gap
58 size. There are two possible memory indices for the gap 58 size. There are two possible memory indices for the gap
89 -- values 0x100 and up represent all other characters. 89 -- values 0x100 and up represent all other characters.
90 90
91 This means that Emchar values are upwardly compatible with 91 This means that Emchar values are upwardly compatible with
92 the standard 8-bit representation of ASCII/ISO-8859-1. 92 the standard 8-bit representation of ASCII/ISO-8859-1.
93 93
94 Bufbyte: 94 Intbyte:
95 -------- 95 --------
96 The data in a buffer or string is logically made up of Bufbyte 96 The data in a buffer or string is logically made up of Intbyte
97 objects, where a Bufbyte takes up the same amount of space as a 97 objects, where a Intbyte takes up the same amount of space as a
98 char. (It is declared differently, though, to catch invalid 98 char. (It is declared differently, though, to catch invalid
99 usages.) Strings stored using Bufbytes are said to be in 99 usages.) Strings stored using Intbytes are said to be in
100 "internal format". The important characteristics of internal 100 "internal format". The important characteristics of internal
101 format are 101 format are
102 102
103 -- ASCII characters are represented as a single Bufbyte, 103 -- ASCII characters are represented as a single Intbyte,
104 in the range 0 - 0x7f. 104 in the range 0 - 0x7f.
105 -- All other characters are represented as a Bufbyte in 105 -- All other characters are represented as a Intbyte in
106 the range 0x80 - 0x9f followed by one or more Bufbytes 106 the range 0x80 - 0x9f followed by one or more Intbytes
107 in the range 0xa0 to 0xff. 107 in the range 0xa0 to 0xff.
108 108
109 This leads to a number of desirable properties: 109 This leads to a number of desirable properties:
110 110
111 -- Given the position of the beginning of a character, 111 -- Given the position of the beginning of a character,
150 Charcount: 150 Charcount:
151 ---------- 151 ----------
152 This typedef represents a count of characters, such as 152 This typedef represents a count of characters, such as
153 a character offset into a string or the number of 153 a character offset into a string or the number of
154 characters between two positions in a buffer. The 154 characters between two positions in a buffer. The
155 difference between two Bufpos's is a Charcount, and 155 difference between two Charbpos's is a Charcount, and
156 character positions in a string are represented using 156 character positions in a string are represented using
157 a Charcount. 157 a Charcount.
158 158
159 Bytecount: 159 Bytecount:
160 ---------- 160 ----------
161 Similar to a Charcount but represents a count of bytes. 161 Similar to a Charcount but represents a count of bytes.
162 The difference between two Bytind's is a Bytecount. 162 The difference between two Bytebpos's is a Bytecount.
163 163
164 164
165 Usage of the various representations: 165 Usage of the various representations:
166 ===================================== 166 =====================================
167 167
209 #include "lstream.h" 209 #include "lstream.h"
210 #include "redisplay.h" 210 #include "redisplay.h"
211 #include "line-number.h" 211 #include "line-number.h"
212 212
213 /* We write things this way because it's very important the 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, 214 MAX_BYTEBPOS_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 215 65535 is a multiple of 3, but this may not always be the
216 case.) */ 216 case.) */
217 217
218 #define MAX_BUFPOS_GAP_SIZE_3 (65535/3) 218 #define MAX_CHARBPOS_GAP_SIZE_3 (65535/3)
219 #define MAX_BYTIND_GAP_SIZE_3 (3 * MAX_BUFPOS_GAP_SIZE_3) 219 #define MAX_BYTEBPOS_GAP_SIZE_3 (3 * MAX_CHARBPOS_GAP_SIZE_3)
220 220
221 short three_to_one_table[1 + MAX_BYTIND_GAP_SIZE_3]; 221 short three_to_one_table[1 + MAX_BYTEBPOS_GAP_SIZE_3];
222 222
223 /* Various macros modelled along the lines of those in buffer.h. 223 /* Various macros modelled along the lines of those in buffer.h.
224 Purposefully omitted from buffer.h because files other than this 224 Purposefully omitted from buffer.h because files other than this
225 one should not be using them. */ 225 one should not be using them. */
226 226
301 301
302 /* Given a pointer to a text string and a length in bytes, return 302 /* Given a pointer to a text string and a length in bytes, return
303 the equivalent length in characters. */ 303 the equivalent length in characters. */
304 304
305 Charcount 305 Charcount
306 bytecount_to_charcount (const Bufbyte *ptr, Bytecount len) 306 bytecount_to_charcount (const Intbyte *ptr, Bytecount len)
307 { 307 {
308 Charcount count = 0; 308 Charcount count = 0;
309 const Bufbyte *end = ptr + len; 309 const Intbyte *end = ptr + len;
310 310
311 #if SIZEOF_LONG == 8 311 #if SIZEOF_LONG == 8
312 # define STRIDE_TYPE long 312 # define STRIDE_TYPE long
313 # define HIGH_BIT_MASK 0x8080808080808080UL 313 # define HIGH_BIT_MASK 0x8080808080808080UL
314 #elif SIZEOF_LONG_LONG == 8 && !(defined (i386) || defined (__i386__)) 314 #elif SIZEOF_LONG_LONG == 8 && !(defined (i386) || defined (__i386__))
337 { 337 {
338 const unsigned STRIDE_TYPE *ascii_end = 338 const unsigned STRIDE_TYPE *ascii_end =
339 (const unsigned STRIDE_TYPE *) ptr; 339 (const unsigned STRIDE_TYPE *) ptr;
340 /* This loop screams, because we can typically 340 /* This loop screams, because we can typically
341 detect ASCII characters 8 at a time. */ 341 detect ASCII characters 8 at a time. */
342 while ((const Bufbyte *) ascii_end + STRIDE <= end 342 while ((const Intbyte *) ascii_end + STRIDE <= end
343 && !(*ascii_end & HIGH_BIT_MASK)) 343 && !(*ascii_end & HIGH_BIT_MASK))
344 ascii_end++; 344 ascii_end++;
345 if ((Bufbyte *) ascii_end == ptr) 345 if ((Intbyte *) ascii_end == ptr)
346 ptr++, count++; 346 ptr++, count++;
347 else 347 else
348 { 348 {
349 count += (Bufbyte *) ascii_end - ptr; 349 count += (Intbyte *) ascii_end - ptr;
350 ptr = (Bufbyte *) ascii_end; 350 ptr = (Intbyte *) ascii_end;
351 } 351 }
352 } 352 }
353 } 353 }
354 else 354 else
355 { 355 {
356 /* optimize for successive characters from the same charset */ 356 /* optimize for successive characters from the same charset */
357 Bufbyte leading_byte = *ptr; 357 Intbyte leading_byte = *ptr;
358 Memory_Count bytes = REP_BYTES_BY_FIRST_BYTE (leading_byte); 358 Bytecount bytes = REP_BYTES_BY_FIRST_BYTE (leading_byte);
359 while ((ptr < end) && (*ptr == leading_byte)) 359 while ((ptr < end) && (*ptr == leading_byte))
360 ptr += bytes, count++; 360 ptr += bytes, count++;
361 } 361 }
362 } 362 }
363 363
364 #ifdef ERROR_CHECK_BUFPOS 364 #ifdef ERROR_CHECK_CHARBPOS
365 /* Bomb out if the specified substring ends in the middle 365 /* Bomb out if the specified substring ends in the middle
366 of a character. Note that we might have already gotten 366 of a character. Note that we might have already gotten
367 a core dump above from an invalid reference, but at least 367 a core dump above from an invalid reference, but at least
368 we will get no farther than here. */ 368 we will get no farther than here. */
369 assert (ptr == end); 369 assert (ptr == end);
374 374
375 /* Given a pointer to a text string and a length in characters, return 375 /* Given a pointer to a text string and a length in characters, return
376 the equivalent length in bytes. */ 376 the equivalent length in bytes. */
377 377
378 Bytecount 378 Bytecount
379 charcount_to_bytecount (const Bufbyte *ptr, Charcount len) 379 charcount_to_bytecount (const Intbyte *ptr, Charcount len)
380 { 380 {
381 const Bufbyte *newptr = ptr; 381 const Intbyte *newptr = ptr;
382 382
383 while (len > 0) 383 while (len > 0)
384 { 384 {
385 INC_CHARPTR (newptr); 385 INC_CHARPTR (newptr);
386 len--; 386 len--;
387 } 387 }
388 return newptr - ptr; 388 return newptr - ptr;
389 } 389 }
390 390
391 /* The next two functions are the actual meat behind the 391 /* The next two functions are the actual meat behind the
392 bufpos-to-bytind and bytind-to-bufpos conversions. Currently 392 charbpos-to-bytebpos and bytebpos-to-charbpos conversions. Currently
393 the method they use is fairly unsophisticated; see buffer.h. 393 the method they use is fairly unsophisticated; see buffer.h.
394 394
395 Note that bufpos_to_bytind_func() is probably the most-called 395 Note that charbpos_to_bytebpos_func() is probably the most-called
396 function in all of XEmacs. Therefore, it must be FAST FAST FAST. 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. 397 This is the reason why so much of the code is duplicated.
398 398
399 Similar considerations apply to bytind_to_bufpos_func(), although 399 Similar considerations apply to bytebpos_to_charbpos_func(), although
400 less so because the function is not called so often. 400 less so because the function is not called so often.
401 401
402 #### At some point this should use a more sophisticated method; 402 #### At some point this should use a more sophisticated method;
403 see buffer.h. */ 403 see buffer.h. */
404 404
405 static int not_very_random_number; 405 static int not_very_random_number;
406 406
407 Bytind 407 Bytebpos
408 bufpos_to_bytind_func (struct buffer *buf, Bufpos x) 408 charbpos_to_bytebpos_func (struct buffer *buf, Charbpos x)
409 { 409 {
410 Bufpos bufmin; 410 Charbpos bufmin;
411 Bufpos bufmax; 411 Charbpos bufmax;
412 Bytind bytmin; 412 Bytebpos bytmin;
413 Bytind bytmax; 413 Bytebpos bytmax;
414 int size; 414 int size;
415 int forward_p; 415 int forward_p;
416 Bytind retval; 416 Bytebpos retval;
417 int diff_so_far; 417 int diff_so_far;
418 int add_to_cache = 0; 418 int add_to_cache = 0;
419 419
420 /* Check for some cached positions, for speed. */ 420 /* Check for some cached positions, for speed. */
421 if (x == BUF_PT (buf)) 421 if (x == BUF_PT (buf))
443 same as BEGV and ZV, and when they're not, they're not likely 443 same as BEGV and ZV, and when they're not, they're not likely
444 to be used.) */ 444 to be used.) */
445 445
446 if (x > bufmax) 446 if (x > bufmax)
447 { 447 {
448 Bufpos diffmax = x - bufmax; 448 Charbpos diffmax = x - bufmax;
449 Bufpos diffpt = x - BUF_PT (buf); 449 Charbpos diffpt = x - BUF_PT (buf);
450 Bufpos diffzv = BUF_ZV (buf) - x; 450 Charbpos diffzv = BUF_ZV (buf) - x;
451 /* #### This value could stand some more exploration. */ 451 /* #### This value could stand some more exploration. */
452 Charcount heuristic_hack = (bufmax - bufmin) >> 2; 452 Charcount heuristic_hack = (bufmax - bufmin) >> 2;
453 453
454 /* Check if the position is closer to PT or ZV than to the 454 /* Check if the position is closer to PT or ZV than to the
455 end of the known region. */ 455 end of the known region. */
485 bufmax = bufmin = BUF_ZV (buf); 485 bufmax = bufmin = BUF_ZV (buf);
486 bytmax = bytmin = BI_BUF_ZV (buf); 486 bytmax = bytmin = BI_BUF_ZV (buf);
487 size = 1; 487 size = 1;
488 } 488 }
489 } 489 }
490 #ifdef ERROR_CHECK_BUFPOS 490 #ifdef ERROR_CHECK_CHARBPOS
491 else if (x >= bufmin) 491 else if (x >= bufmin)
492 abort (); 492 abort ();
493 #endif 493 #endif
494 else 494 else
495 { 495 {
496 Bufpos diffmin = bufmin - x; 496 Charbpos diffmin = bufmin - x;
497 Bufpos diffpt = BUF_PT (buf) - x; 497 Charbpos diffpt = BUF_PT (buf) - x;
498 Bufpos diffbegv = x - BUF_BEGV (buf); 498 Charbpos diffbegv = x - BUF_BEGV (buf);
499 /* #### This value could stand some more exploration. */ 499 /* #### This value could stand some more exploration. */
500 Charcount heuristic_hack = (bufmax - bufmin) >> 2; 500 Charcount heuristic_hack = (bufmax - bufmin) >> 2;
501 501
502 if (diffpt < 0) 502 if (diffpt < 0)
503 diffpt = -diffpt; 503 diffpt = -diffpt;
543 /* I considered keeping the positions ordered. This would speed 543 /* I considered keeping the positions ordered. This would speed
544 up this loop, but updating the cache would take longer, so 544 up this loop, but updating the cache would take longer, so
545 it doesn't seem like it would really matter. */ 545 it doesn't seem like it would really matter. */
546 for (i = 0; i < 16; i++) 546 for (i = 0; i < 16; i++)
547 { 547 {
548 int diff = buf->text->mule_bufpos_cache[i] - x; 548 int diff = buf->text->mule_charbpos_cache[i] - x;
549 549
550 if (diff < 0) 550 if (diff < 0)
551 diff = -diff; 551 diff = -diff;
552 if (diff < minval) 552 if (diff < minval)
553 { 553 {
556 } 556 }
557 } 557 }
558 558
559 if (minval < diff_so_far) 559 if (minval < diff_so_far)
560 { 560 {
561 bufmax = bufmin = buf->text->mule_bufpos_cache[found]; 561 bufmax = bufmin = buf->text->mule_charbpos_cache[found];
562 bytmax = bytmin = buf->text->mule_bytind_cache[found]; 562 bytmax = bytmin = buf->text->mule_bytebpos_cache[found];
563 size = 1; 563 size = 1;
564 } 564 }
565 } 565 }
566 566
567 /* It's conceivable that the caching above could lead to X being 567 /* It's conceivable that the caching above could lead to X being
568 the same as one of the range edges. */ 568 the same as one of the range edges. */
569 if (x >= bufmax) 569 if (x >= bufmax)
570 { 570 {
571 Bytind newmax; 571 Bytebpos newmax;
572 Bytecount newsize; 572 Bytecount newsize;
573 573
574 forward_p = 1; 574 forward_p = 1;
575 while (x > bufmax) 575 while (x > bufmax)
576 { 576 {
577 newmax = bytmax; 577 newmax = bytmax;
578 578
579 INC_BYTIND (buf, newmax); 579 INC_BYTEBPOS (buf, newmax);
580 newsize = newmax - bytmax; 580 newsize = newmax - bytmax;
581 if (newsize != size) 581 if (newsize != size)
582 { 582 {
583 bufmin = bufmax; 583 bufmin = bufmax;
584 bytmin = bytmax; 584 bytmin = bytmax;
592 /* #### Should go past the found location to reduce the number 592 /* #### Should go past the found location to reduce the number
593 of times that this function is called */ 593 of times that this function is called */
594 } 594 }
595 else /* x < bufmin */ 595 else /* x < bufmin */
596 { 596 {
597 Bytind newmin; 597 Bytebpos newmin;
598 Bytecount newsize; 598 Bytecount newsize;
599 599
600 forward_p = 0; 600 forward_p = 0;
601 while (x < bufmin) 601 while (x < bufmin)
602 { 602 {
603 newmin = bytmin; 603 newmin = bytmin;
604 604
605 DEC_BYTIND (buf, newmin); 605 DEC_BYTEBPOS (buf, newmin);
606 newsize = bytmin - newmin; 606 newsize = bytmin - newmin;
607 if (newsize != size) 607 if (newsize != size)
608 { 608 {
609 bufmax = bufmin; 609 bufmax = bufmin;
610 bytmax = bytmin; 610 bytmax = bytmin;
628 { 628 {
629 int gap = bytmax - bytmin; 629 int gap = bytmax - bytmin;
630 buf->text->mule_three_p = 1; 630 buf->text->mule_three_p = 1;
631 buf->text->mule_shifter = 1; 631 buf->text->mule_shifter = 1;
632 632
633 if (gap > MAX_BYTIND_GAP_SIZE_3) 633 if (gap > MAX_BYTEBPOS_GAP_SIZE_3)
634 { 634 {
635 if (forward_p) 635 if (forward_p)
636 { 636 {
637 bytmin = bytmax - MAX_BYTIND_GAP_SIZE_3; 637 bytmin = bytmax - MAX_BYTEBPOS_GAP_SIZE_3;
638 bufmin = bufmax - MAX_BUFPOS_GAP_SIZE_3; 638 bufmin = bufmax - MAX_CHARBPOS_GAP_SIZE_3;
639 } 639 }
640 else 640 else
641 { 641 {
642 bytmax = bytmin + MAX_BYTIND_GAP_SIZE_3; 642 bytmax = bytmin + MAX_BYTEBPOS_GAP_SIZE_3;
643 bufmax = bufmin + MAX_BUFPOS_GAP_SIZE_3; 643 bufmax = bufmin + MAX_CHARBPOS_GAP_SIZE_3;
644 } 644 }
645 } 645 }
646 } 646 }
647 else 647 else
648 { 648 {
669 #### It would be better to use a least-recently-used algorithm 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 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. */ 671 it's worth it to go to the trouble of maintaining that. */
672 not_very_random_number += 621; 672 not_very_random_number += 621;
673 replace_loc = not_very_random_number & 15; 673 replace_loc = not_very_random_number & 15;
674 buf->text->mule_bufpos_cache[replace_loc] = x; 674 buf->text->mule_charbpos_cache[replace_loc] = x;
675 buf->text->mule_bytind_cache[replace_loc] = retval; 675 buf->text->mule_bytebpos_cache[replace_loc] = retval;
676 } 676 }
677 677
678 return retval; 678 return retval;
679 } 679 }
680 680
681 /* The logic in this function is almost identical to the logic in 681 /* The logic in this function is almost identical to the logic in
682 the previous function. */ 682 the previous function. */
683 683
684 Bufpos 684 Charbpos
685 bytind_to_bufpos_func (struct buffer *buf, Bytind x) 685 bytebpos_to_charbpos_func (struct buffer *buf, Bytebpos x)
686 { 686 {
687 Bufpos bufmin; 687 Charbpos bufmin;
688 Bufpos bufmax; 688 Charbpos bufmax;
689 Bytind bytmin; 689 Bytebpos bytmin;
690 Bytind bytmax; 690 Bytebpos bytmax;
691 int size; 691 int size;
692 int forward_p; 692 int forward_p;
693 Bufpos retval; 693 Charbpos retval;
694 int diff_so_far; 694 int diff_so_far;
695 int add_to_cache = 0; 695 int add_to_cache = 0;
696 696
697 /* Check for some cached positions, for speed. */ 697 /* Check for some cached positions, for speed. */
698 if (x == BI_BUF_PT (buf)) 698 if (x == BI_BUF_PT (buf))
720 same as BI_BEGV and BI_ZV, and when they're not, they're not likely 720 same as BI_BEGV and BI_ZV, and when they're not, they're not likely
721 to be used.) */ 721 to be used.) */
722 722
723 if (x > bytmax) 723 if (x > bytmax)
724 { 724 {
725 Bytind diffmax = x - bytmax; 725 Bytebpos diffmax = x - bytmax;
726 Bytind diffpt = x - BI_BUF_PT (buf); 726 Bytebpos diffpt = x - BI_BUF_PT (buf);
727 Bytind diffzv = BI_BUF_ZV (buf) - x; 727 Bytebpos diffzv = BI_BUF_ZV (buf) - x;
728 /* #### This value could stand some more exploration. */ 728 /* #### This value could stand some more exploration. */
729 Bytecount heuristic_hack = (bytmax - bytmin) >> 2; 729 Bytecount heuristic_hack = (bytmax - bytmin) >> 2;
730 730
731 /* Check if the position is closer to PT or ZV than to the 731 /* Check if the position is closer to PT or ZV than to the
732 end of the known region. */ 732 end of the known region. */
762 bufmax = bufmin = BUF_ZV (buf); 762 bufmax = bufmin = BUF_ZV (buf);
763 bytmax = bytmin = BI_BUF_ZV (buf); 763 bytmax = bytmin = BI_BUF_ZV (buf);
764 size = 1; 764 size = 1;
765 } 765 }
766 } 766 }
767 #ifdef ERROR_CHECK_BUFPOS 767 #ifdef ERROR_CHECK_CHARBPOS
768 else if (x >= bytmin) 768 else if (x >= bytmin)
769 abort (); 769 abort ();
770 #endif 770 #endif
771 else 771 else
772 { 772 {
773 Bytind diffmin = bytmin - x; 773 Bytebpos diffmin = bytmin - x;
774 Bytind diffpt = BI_BUF_PT (buf) - x; 774 Bytebpos diffpt = BI_BUF_PT (buf) - x;
775 Bytind diffbegv = x - BI_BUF_BEGV (buf); 775 Bytebpos diffbegv = x - BI_BUF_BEGV (buf);
776 /* #### This value could stand some more exploration. */ 776 /* #### This value could stand some more exploration. */
777 Bytecount heuristic_hack = (bytmax - bytmin) >> 2; 777 Bytecount heuristic_hack = (bytmax - bytmin) >> 2;
778 778
779 if (diffpt < 0) 779 if (diffpt < 0)
780 diffpt = -diffpt; 780 diffpt = -diffpt;
820 /* I considered keeping the positions ordered. This would speed 820 /* I considered keeping the positions ordered. This would speed
821 up this loop, but updating the cache would take longer, so 821 up this loop, but updating the cache would take longer, so
822 it doesn't seem like it would really matter. */ 822 it doesn't seem like it would really matter. */
823 for (i = 0; i < 16; i++) 823 for (i = 0; i < 16; i++)
824 { 824 {
825 int diff = buf->text->mule_bytind_cache[i] - x; 825 int diff = buf->text->mule_bytebpos_cache[i] - x;
826 826
827 if (diff < 0) 827 if (diff < 0)
828 diff = -diff; 828 diff = -diff;
829 if (diff < minval) 829 if (diff < minval)
830 { 830 {
833 } 833 }
834 } 834 }
835 835
836 if (minval < diff_so_far) 836 if (minval < diff_so_far)
837 { 837 {
838 bufmax = bufmin = buf->text->mule_bufpos_cache[found]; 838 bufmax = bufmin = buf->text->mule_charbpos_cache[found];
839 bytmax = bytmin = buf->text->mule_bytind_cache[found]; 839 bytmax = bytmin = buf->text->mule_bytebpos_cache[found];
840 size = 1; 840 size = 1;
841 } 841 }
842 } 842 }
843 843
844 /* It's conceivable that the caching above could lead to X being 844 /* It's conceivable that the caching above could lead to X being
845 the same as one of the range edges. */ 845 the same as one of the range edges. */
846 if (x >= bytmax) 846 if (x >= bytmax)
847 { 847 {
848 Bytind newmax; 848 Bytebpos newmax;
849 Bytecount newsize; 849 Bytecount newsize;
850 850
851 forward_p = 1; 851 forward_p = 1;
852 while (x > bytmax) 852 while (x > bytmax)
853 { 853 {
854 newmax = bytmax; 854 newmax = bytmax;
855 855
856 INC_BYTIND (buf, newmax); 856 INC_BYTEBPOS (buf, newmax);
857 newsize = newmax - bytmax; 857 newsize = newmax - bytmax;
858 if (newsize != size) 858 if (newsize != size)
859 { 859 {
860 bufmin = bufmax; 860 bufmin = bufmax;
861 bytmin = bytmax; 861 bytmin = bytmax;
869 /* #### Should go past the found location to reduce the number 869 /* #### Should go past the found location to reduce the number
870 of times that this function is called */ 870 of times that this function is called */
871 } 871 }
872 else /* x <= bytmin */ 872 else /* x <= bytmin */
873 { 873 {
874 Bytind newmin; 874 Bytebpos newmin;
875 Bytecount newsize; 875 Bytecount newsize;
876 876
877 forward_p = 0; 877 forward_p = 0;
878 while (x < bytmin) 878 while (x < bytmin)
879 { 879 {
880 newmin = bytmin; 880 newmin = bytmin;
881 881
882 DEC_BYTIND (buf, newmin); 882 DEC_BYTEBPOS (buf, newmin);
883 newsize = bytmin - newmin; 883 newsize = bytmin - newmin;
884 if (newsize != size) 884 if (newsize != size)
885 { 885 {
886 bufmax = bufmin; 886 bufmax = bufmin;
887 bytmax = bytmin; 887 bytmax = bytmin;
905 { 905 {
906 int gap = bytmax - bytmin; 906 int gap = bytmax - bytmin;
907 buf->text->mule_three_p = 1; 907 buf->text->mule_three_p = 1;
908 buf->text->mule_shifter = 1; 908 buf->text->mule_shifter = 1;
909 909
910 if (gap > MAX_BYTIND_GAP_SIZE_3) 910 if (gap > MAX_BYTEBPOS_GAP_SIZE_3)
911 { 911 {
912 if (forward_p) 912 if (forward_p)
913 { 913 {
914 bytmin = bytmax - MAX_BYTIND_GAP_SIZE_3; 914 bytmin = bytmax - MAX_BYTEBPOS_GAP_SIZE_3;
915 bufmin = bufmax - MAX_BUFPOS_GAP_SIZE_3; 915 bufmin = bufmax - MAX_CHARBPOS_GAP_SIZE_3;
916 } 916 }
917 else 917 else
918 { 918 {
919 bytmax = bytmin + MAX_BYTIND_GAP_SIZE_3; 919 bytmax = bytmin + MAX_BYTEBPOS_GAP_SIZE_3;
920 bufmax = bufmin + MAX_BUFPOS_GAP_SIZE_3; 920 bufmax = bufmin + MAX_CHARBPOS_GAP_SIZE_3;
921 } 921 }
922 } 922 }
923 } 923 }
924 else 924 else
925 { 925 {
946 #### It would be better to use a least-recently-used algorithm 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 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. */ 948 it's worth it to go to the trouble of maintaining that. */
949 not_very_random_number += 621; 949 not_very_random_number += 621;
950 replace_loc = not_very_random_number & 15; 950 replace_loc = not_very_random_number & 15;
951 buf->text->mule_bufpos_cache[replace_loc] = retval; 951 buf->text->mule_charbpos_cache[replace_loc] = retval;
952 buf->text->mule_bytind_cache[replace_loc] = x; 952 buf->text->mule_bytebpos_cache[replace_loc] = x;
953 } 953 }
954 954
955 return retval; 955 return retval;
956 } 956 }
957 957
958 /* Text of length BYTELENGTH and CHARLENGTH (in different units) 958 /* Text of length BYTELENGTH and CHARLENGTH (in different units)
959 was inserted at bufpos START. */ 959 was inserted at charbpos START. */
960 960
961 static void 961 static void
962 buffer_mule_signal_inserted_region (struct buffer *buf, Bufpos start, 962 buffer_mule_signal_inserted_region (struct buffer *buf, Charbpos start,
963 Bytecount bytelength, 963 Bytecount bytelength,
964 Charcount charlength) 964 Charcount charlength)
965 { 965 {
966 int size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p; 966 int size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p;
967 int i; 967 int i;
968 968
969 /* Adjust the cache of known positions. */ 969 /* Adjust the cache of known positions. */
970 for (i = 0; i < 16; i++) 970 for (i = 0; i < 16; i++)
971 { 971 {
972 972
973 if (buf->text->mule_bufpos_cache[i] > start) 973 if (buf->text->mule_charbpos_cache[i] > start)
974 { 974 {
975 buf->text->mule_bufpos_cache[i] += charlength; 975 buf->text->mule_charbpos_cache[i] += charlength;
976 buf->text->mule_bytind_cache[i] += bytelength; 976 buf->text->mule_bytebpos_cache[i] += bytelength;
977 } 977 }
978 } 978 }
979 979
980 if (start >= buf->text->mule_bufmax) 980 if (start >= buf->text->mule_bufmax)
981 return; 981 return;
992 buf->text->mule_bytmin += bytelength; 992 buf->text->mule_bytmin += bytelength;
993 buf->text->mule_bytmax += bytelength; 993 buf->text->mule_bytmax += bytelength;
994 } 994 }
995 else 995 else
996 { 996 {
997 Bufpos end = start + charlength; 997 Charbpos end = start + charlength;
998 /* the insertion point divides the known region in two. 998 /* the insertion point divides the known region in two.
999 Keep the longer half, at least, and expand into the 999 Keep the longer half, at least, and expand into the
1000 inserted chunk as much as possible. */ 1000 inserted chunk as much as possible. */
1001 1001
1002 if (start - buf->text->mule_bufmin > buf->text->mule_bufmax - start) 1002 if (start - buf->text->mule_bufmin > buf->text->mule_bufmax - start)
1003 { 1003 {
1004 Bytind bytestart = (buf->text->mule_bytmin 1004 Bytebpos bytestart = (buf->text->mule_bytmin
1005 + size * (start - buf->text->mule_bufmin)); 1005 + size * (start - buf->text->mule_bufmin));
1006 Bytind bytenew; 1006 Bytebpos bytenew;
1007 1007
1008 while (start < end) 1008 while (start < end)
1009 { 1009 {
1010 bytenew = bytestart; 1010 bytenew = bytestart;
1011 INC_BYTIND (buf, bytenew); 1011 INC_BYTEBPOS (buf, bytenew);
1012 if (bytenew - bytestart != size) 1012 if (bytenew - bytestart != size)
1013 break; 1013 break;
1014 start++; 1014 start++;
1015 bytestart = bytenew; 1015 bytestart = bytenew;
1016 } 1016 }
1025 buf->text->mule_bytmax += bytelength; 1025 buf->text->mule_bytmax += bytelength;
1026 } 1026 }
1027 } 1027 }
1028 else 1028 else
1029 { 1029 {
1030 Bytind byteend = (buf->text->mule_bytmin 1030 Bytebpos byteend = (buf->text->mule_bytmin
1031 + size * (start - buf->text->mule_bufmin) 1031 + size * (start - buf->text->mule_bufmin)
1032 + bytelength); 1032 + bytelength);
1033 Bytind bytenew; 1033 Bytebpos bytenew;
1034 1034
1035 buf->text->mule_bufmax += charlength; 1035 buf->text->mule_bufmax += charlength;
1036 buf->text->mule_bytmax += bytelength; 1036 buf->text->mule_bytmax += bytelength;
1037 1037
1038 while (end > start) 1038 while (end > start)
1039 { 1039 {
1040 bytenew = byteend; 1040 bytenew = byteend;
1041 DEC_BYTIND (buf, bytenew); 1041 DEC_BYTEBPOS (buf, bytenew);
1042 if (byteend - bytenew != size) 1042 if (byteend - bytenew != size)
1043 break; 1043 break;
1044 end--; 1044 end--;
1045 byteend = bytenew; 1045 byteend = bytenew;
1046 } 1046 }
1051 } 1051 }
1052 } 1052 }
1053 } 1053 }
1054 } 1054 }
1055 1055
1056 /* Text from START to END (equivalent in Bytinds: from BI_START to 1056 /* Text from START to END (equivalent in Bytebposs: from BI_START to
1057 BI_END) was deleted. */ 1057 BI_END) was deleted. */
1058 1058
1059 static void 1059 static void
1060 buffer_mule_signal_deleted_region (struct buffer *buf, Bufpos start, 1060 buffer_mule_signal_deleted_region (struct buffer *buf, Charbpos start,
1061 Bufpos end, Bytind bi_start, 1061 Charbpos end, Bytebpos bi_start,
1062 Bytind bi_end) 1062 Bytebpos bi_end)
1063 { 1063 {
1064 int i; 1064 int i;
1065 1065
1066 /* Adjust the cache of known positions. */ 1066 /* Adjust the cache of known positions. */
1067 for (i = 0; i < 16; i++) 1067 for (i = 0; i < 16; i++)
1068 { 1068 {
1069 /* After the end; gets shoved backward */ 1069 /* After the end; gets shoved backward */
1070 if (buf->text->mule_bufpos_cache[i] > end) 1070 if (buf->text->mule_charbpos_cache[i] > end)
1071 { 1071 {
1072 buf->text->mule_bufpos_cache[i] -= end - start; 1072 buf->text->mule_charbpos_cache[i] -= end - start;
1073 buf->text->mule_bytind_cache[i] -= bi_end - bi_start; 1073 buf->text->mule_bytebpos_cache[i] -= bi_end - bi_start;
1074 } 1074 }
1075 /* In the range; moves to start of range */ 1075 /* In the range; moves to start of range */
1076 else if (buf->text->mule_bufpos_cache[i] > start) 1076 else if (buf->text->mule_charbpos_cache[i] > start)
1077 { 1077 {
1078 buf->text->mule_bufpos_cache[i] = start; 1078 buf->text->mule_charbpos_cache[i] = start;
1079 buf->text->mule_bytind_cache[i] = bi_start; 1079 buf->text->mule_bytebpos_cache[i] = bi_start;
1080 } 1080 }
1081 } 1081 }
1082 1082
1083 /* We don't care about any text after the end of the known region. */ 1083 /* We don't care about any text after the end of the known region. */
1084 1084
1104 buf->text->mule_bytmin -= bi_end - bi_start; 1104 buf->text->mule_bytmin -= bi_end - bi_start;
1105 } 1105 }
1106 1106
1107 #endif /* MULE */ 1107 #endif /* MULE */
1108 1108
1109 #ifdef ERROR_CHECK_BUFPOS 1109 #ifdef ERROR_CHECK_CHARBPOS
1110 1110
1111 Bytind 1111 Bytebpos
1112 bufpos_to_bytind (struct buffer *buf, Bufpos x) 1112 charbpos_to_bytebpos (struct buffer *buf, Charbpos x)
1113 { 1113 {
1114 Bytind retval = real_bufpos_to_bytind (buf, x); 1114 Bytebpos retval = real_charbpos_to_bytebpos (buf, x);
1115 ASSERT_VALID_BYTIND_UNSAFE (buf, retval); 1115 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, retval);
1116 return retval; 1116 return retval;
1117 } 1117 }
1118 1118
1119 Bufpos 1119 Charbpos
1120 bytind_to_bufpos (struct buffer *buf, Bytind x) 1120 bytebpos_to_charbpos (struct buffer *buf, Bytebpos x)
1121 { 1121 {
1122 ASSERT_VALID_BYTIND_UNSAFE (buf, x); 1122 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, x);
1123 return real_bytind_to_bufpos (buf, x); 1123 return real_bytebpos_to_charbpos (buf, x);
1124 } 1124 }
1125 1125
1126 #endif /* ERROR_CHECK_BUFPOS */ 1126 #endif /* ERROR_CHECK_CHARBPOS */
1127 1127
1128 1128
1129 /************************************************************************/ 1129 /************************************************************************/
1130 /* verifying buffer and string positions */ 1130 /* verifying buffer and string positions */
1131 /************************************************************************/ 1131 /************************************************************************/
1132 1132
1133 /* Functions below are tagged with either _byte or _char indicating 1133 /* Functions below are tagged with either _byte or _char indicating
1134 whether they return byte or character positions. For a buffer, 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". 1135 a character position is a "Charbpos" and a byte position is a "Bytebpos".
1136 For strings, these are sometimes typed using "Charcount" and 1136 For strings, these are sometimes typed using "Charcount" and
1137 "Bytecount". */ 1137 "Bytecount". */
1138 1138
1139 /* Flags for the functions below are: 1139 /* Flags for the functions below are:
1140 1140
1189 the buffer (BEGV and ZV), and to signal an error if the position is 1189 the buffer (BEGV and ZV), and to signal an error if the position is
1190 out of range. 1190 out of range.
1191 1191
1192 */ 1192 */
1193 1193
1194 Bufpos 1194 Charbpos
1195 get_buffer_pos_char (struct buffer *b, Lisp_Object pos, unsigned int flags) 1195 get_buffer_pos_char (struct buffer *b, Lisp_Object pos, unsigned int flags)
1196 { 1196 {
1197 /* Does not GC */ 1197 /* Does not GC */
1198 Bufpos ind; 1198 Charbpos ind;
1199 Bufpos min_allowed, max_allowed; 1199 Charbpos min_allowed, max_allowed;
1200 1200
1201 CHECK_INT_COERCE_MARKER (pos); 1201 CHECK_INT_COERCE_MARKER (pos);
1202 ind = XINT (pos); 1202 ind = XINT (pos);
1203 min_allowed = flags & GB_ALLOW_PAST_ACCESSIBLE ? BUF_BEG (b) : BUF_BEGV (b); 1203 min_allowed = flags & GB_ALLOW_PAST_ACCESSIBLE ? BUF_BEG (b) : BUF_BEGV (b);
1204 max_allowed = flags & GB_ALLOW_PAST_ACCESSIBLE ? BUF_Z (b) : BUF_ZV (b); 1204 max_allowed = flags & GB_ALLOW_PAST_ACCESSIBLE ? BUF_Z (b) : BUF_ZV (b);
1218 } 1218 }
1219 1219
1220 return ind; 1220 return ind;
1221 } 1221 }
1222 1222
1223 Bytind 1223 Bytebpos
1224 get_buffer_pos_byte (struct buffer *b, Lisp_Object pos, unsigned int flags) 1224 get_buffer_pos_byte (struct buffer *b, Lisp_Object pos, unsigned int flags)
1225 { 1225 {
1226 Bufpos bpos = get_buffer_pos_char (b, pos, flags); 1226 Charbpos bpos = get_buffer_pos_char (b, pos, flags);
1227 if (bpos < 0) /* could happen with GB_NO_ERROR_IF_BAD */ 1227 if (bpos < 0) /* could happen with GB_NO_ERROR_IF_BAD */
1228 return -1; 1228 return -1;
1229 return bufpos_to_bytind (b, bpos); 1229 return charbpos_to_bytebpos (b, bpos);
1230 } 1230 }
1231 1231
1232 /* Return a pair of buffer positions representing a range of text, 1232 /* Return a pair of buffer positions representing a range of text,
1233 taken from a pair of Lisp_Objects. Full error-checking is 1233 taken from a pair of Lisp_Objects. Full error-checking is
1234 done on the positions. Flags can be specified to control the 1234 done on the positions. Flags can be specified to control the
1240 and to signal an error if the positions are out of range. 1240 and to signal an error if the positions are out of range.
1241 */ 1241 */
1242 1242
1243 void 1243 void
1244 get_buffer_range_char (struct buffer *b, Lisp_Object from, Lisp_Object to, 1244 get_buffer_range_char (struct buffer *b, Lisp_Object from, Lisp_Object to,
1245 Bufpos *from_out, Bufpos *to_out, unsigned int flags) 1245 Charbpos *from_out, Charbpos *to_out, unsigned int flags)
1246 { 1246 {
1247 /* Does not GC */ 1247 /* Does not GC */
1248 Bufpos min_allowed, max_allowed; 1248 Charbpos min_allowed, max_allowed;
1249 1249
1250 min_allowed = (flags & GB_ALLOW_PAST_ACCESSIBLE) ? 1250 min_allowed = (flags & GB_ALLOW_PAST_ACCESSIBLE) ?
1251 BUF_BEG (b) : BUF_BEGV (b); 1251 BUF_BEG (b) : BUF_BEGV (b);
1252 max_allowed = (flags & GB_ALLOW_PAST_ACCESSIBLE) ? 1252 max_allowed = (flags & GB_ALLOW_PAST_ACCESSIBLE) ?
1253 BUF_Z (b) : BUF_ZV (b); 1253 BUF_Z (b) : BUF_ZV (b);
1273 { 1273 {
1274 if (flags & GB_CHECK_ORDER) 1274 if (flags & GB_CHECK_ORDER)
1275 invalid_argument_2 ("start greater than end", from, to); 1275 invalid_argument_2 ("start greater than end", from, to);
1276 else 1276 else
1277 { 1277 {
1278 Bufpos temp = *from_out; 1278 Charbpos temp = *from_out;
1279 *from_out = *to_out; 1279 *from_out = *to_out;
1280 *to_out = temp; 1280 *to_out = temp;
1281 } 1281 }
1282 } 1282 }
1283 } 1283 }
1284 1284
1285 void 1285 void
1286 get_buffer_range_byte (struct buffer *b, Lisp_Object from, Lisp_Object to, 1286 get_buffer_range_byte (struct buffer *b, Lisp_Object from, Lisp_Object to,
1287 Bytind *from_out, Bytind *to_out, unsigned int flags) 1287 Bytebpos *from_out, Bytebpos *to_out, unsigned int flags)
1288 { 1288 {
1289 Bufpos s, e; 1289 Charbpos s, e;
1290 1290
1291 get_buffer_range_char (b, from, to, &s, &e, flags); 1291 get_buffer_range_char (b, from, to, &s, &e, flags);
1292 if (s >= 0) 1292 if (s >= 0)
1293 *from_out = bufpos_to_bytind (b, s); 1293 *from_out = charbpos_to_bytebpos (b, s);
1294 else /* could happen with GB_NO_ERROR_IF_BAD */ 1294 else /* could happen with GB_NO_ERROR_IF_BAD */
1295 *from_out = -1; 1295 *from_out = -1;
1296 if (e >= 0) 1296 if (e >= 0)
1297 *to_out = bufpos_to_bytind (b, e); 1297 *to_out = charbpos_to_bytebpos (b, e);
1298 else 1298 else
1299 *to_out = -1; 1299 *to_out = -1;
1300 } 1300 }
1301 1301
1302 static Charcount 1302 static Charcount
1372 { 1372 {
1373 if (flags & GB_CHECK_ORDER) 1373 if (flags & GB_CHECK_ORDER)
1374 invalid_argument_2 ("start greater than end", from, to); 1374 invalid_argument_2 ("start greater than end", from, to);
1375 else 1375 else
1376 { 1376 {
1377 Bufpos temp = *from_out; 1377 Charbpos temp = *from_out;
1378 *from_out = *to_out; 1378 *from_out = *to_out;
1379 *to_out = temp; 1379 *to_out = temp;
1380 } 1380 }
1381 } 1381 }
1382 } 1382 }
1398 else 1398 else
1399 *to_out = -1; 1399 *to_out = -1;
1400 1400
1401 } 1401 }
1402 1402
1403 Bufpos 1403 Charbpos
1404 get_buffer_or_string_pos_char (Lisp_Object object, Lisp_Object pos, 1404 get_buffer_or_string_pos_char (Lisp_Object object, Lisp_Object pos,
1405 unsigned int flags) 1405 unsigned int flags)
1406 { 1406 {
1407 return STRINGP (object) ? 1407 return STRINGP (object) ?
1408 get_string_pos_char (object, pos, flags) : 1408 get_string_pos_char (object, pos, flags) :
1409 get_buffer_pos_char (XBUFFER (object), pos, flags); 1409 get_buffer_pos_char (XBUFFER (object), pos, flags);
1410 } 1410 }
1411 1411
1412 Bytind 1412 Bytebpos
1413 get_buffer_or_string_pos_byte (Lisp_Object object, Lisp_Object pos, 1413 get_buffer_or_string_pos_byte (Lisp_Object object, Lisp_Object pos,
1414 unsigned int flags) 1414 unsigned int flags)
1415 { 1415 {
1416 return STRINGP (object) ? 1416 return STRINGP (object) ?
1417 get_string_pos_byte (object, pos, flags) : 1417 get_string_pos_byte (object, pos, flags) :
1418 get_buffer_pos_byte (XBUFFER (object), pos, flags); 1418 get_buffer_pos_byte (XBUFFER (object), pos, flags);
1419 } 1419 }
1420 1420
1421 void 1421 void
1422 get_buffer_or_string_range_char (Lisp_Object object, Lisp_Object from, 1422 get_buffer_or_string_range_char (Lisp_Object object, Lisp_Object from,
1423 Lisp_Object to, Bufpos *from_out, 1423 Lisp_Object to, Charbpos *from_out,
1424 Bufpos *to_out, unsigned int flags) 1424 Charbpos *to_out, unsigned int flags)
1425 { 1425 {
1426 if (STRINGP (object)) 1426 if (STRINGP (object))
1427 get_string_range_char (object, from, to, from_out, to_out, flags); 1427 get_string_range_char (object, from, to, from_out, to_out, flags);
1428 else 1428 else
1429 get_buffer_range_char (XBUFFER (object), from, to, from_out, to_out, flags); 1429 get_buffer_range_char (XBUFFER (object), from, to, from_out, to_out, flags);
1430 } 1430 }
1431 1431
1432 void 1432 void
1433 get_buffer_or_string_range_byte (Lisp_Object object, Lisp_Object from, 1433 get_buffer_or_string_range_byte (Lisp_Object object, Lisp_Object from,
1434 Lisp_Object to, Bytind *from_out, 1434 Lisp_Object to, Bytebpos *from_out,
1435 Bytind *to_out, unsigned int flags) 1435 Bytebpos *to_out, unsigned int flags)
1436 { 1436 {
1437 if (STRINGP (object)) 1437 if (STRINGP (object))
1438 get_string_range_byte (object, from, to, from_out, to_out, flags); 1438 get_string_range_byte (object, from, to, from_out, to_out, flags);
1439 else 1439 else
1440 get_buffer_range_byte (XBUFFER (object), from, to, from_out, to_out, flags); 1440 get_buffer_range_byte (XBUFFER (object), from, to, from_out, to_out, flags);
1441 } 1441 }
1442 1442
1443 Bufpos 1443 Charbpos
1444 buffer_or_string_accessible_begin_char (Lisp_Object object) 1444 buffer_or_string_accessible_begin_char (Lisp_Object object)
1445 { 1445 {
1446 return STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object)); 1446 return STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object));
1447 } 1447 }
1448 1448
1449 Bufpos 1449 Charbpos
1450 buffer_or_string_accessible_end_char (Lisp_Object object) 1450 buffer_or_string_accessible_end_char (Lisp_Object object)
1451 { 1451 {
1452 return STRINGP (object) ? 1452 return STRINGP (object) ?
1453 XSTRING_CHAR_LENGTH (object) : BUF_ZV (XBUFFER (object)); 1453 XSTRING_CHAR_LENGTH (object) : BUF_ZV (XBUFFER (object));
1454 } 1454 }
1455 1455
1456 Bytind 1456 Bytebpos
1457 buffer_or_string_accessible_begin_byte (Lisp_Object object) 1457 buffer_or_string_accessible_begin_byte (Lisp_Object object)
1458 { 1458 {
1459 return STRINGP (object) ? 0 : BI_BUF_BEGV (XBUFFER (object)); 1459 return STRINGP (object) ? 0 : BI_BUF_BEGV (XBUFFER (object));
1460 } 1460 }
1461 1461
1462 Bytind 1462 Bytebpos
1463 buffer_or_string_accessible_end_byte (Lisp_Object object) 1463 buffer_or_string_accessible_end_byte (Lisp_Object object)
1464 { 1464 {
1465 return STRINGP (object) ? 1465 return STRINGP (object) ?
1466 XSTRING_LENGTH (object) : BI_BUF_ZV (XBUFFER (object)); 1466 XSTRING_LENGTH (object) : BI_BUF_ZV (XBUFFER (object));
1467 } 1467 }
1468 1468
1469 Bufpos 1469 Charbpos
1470 buffer_or_string_absolute_begin_char (Lisp_Object object) 1470 buffer_or_string_absolute_begin_char (Lisp_Object object)
1471 { 1471 {
1472 return STRINGP (object) ? 0 : BUF_BEG (XBUFFER (object)); 1472 return STRINGP (object) ? 0 : BUF_BEG (XBUFFER (object));
1473 } 1473 }
1474 1474
1475 Bufpos 1475 Charbpos
1476 buffer_or_string_absolute_end_char (Lisp_Object object) 1476 buffer_or_string_absolute_end_char (Lisp_Object object)
1477 { 1477 {
1478 return STRINGP (object) ? 1478 return STRINGP (object) ?
1479 XSTRING_CHAR_LENGTH (object) : BUF_Z (XBUFFER (object)); 1479 XSTRING_CHAR_LENGTH (object) : BUF_Z (XBUFFER (object));
1480 } 1480 }
1481 1481
1482 Bytind 1482 Bytebpos
1483 buffer_or_string_absolute_begin_byte (Lisp_Object object) 1483 buffer_or_string_absolute_begin_byte (Lisp_Object object)
1484 { 1484 {
1485 return STRINGP (object) ? 0 : BI_BUF_BEG (XBUFFER (object)); 1485 return STRINGP (object) ? 0 : BI_BUF_BEG (XBUFFER (object));
1486 } 1486 }
1487 1487
1488 Bytind 1488 Bytebpos
1489 buffer_or_string_absolute_end_byte (Lisp_Object object) 1489 buffer_or_string_absolute_end_byte (Lisp_Object object)
1490 { 1490 {
1491 return STRINGP (object) ? 1491 return STRINGP (object) ?
1492 XSTRING_LENGTH (object) : BI_BUF_Z (XBUFFER (object)); 1492 XSTRING_LENGTH (object) : BI_BUF_Z (XBUFFER (object));
1493 } 1493 }
1512 either the old or the new value of point is out of synch with the 1512 either the old or the new value of point is out of synch with the
1513 current set of intervals. */ 1513 current set of intervals. */
1514 1514
1515 /* This gets called more than enough to make the function call 1515 /* This gets called more than enough to make the function call
1516 overhead a significant factor so we've turned it into a macro. */ 1516 overhead a significant factor so we've turned it into a macro. */
1517 #define JUST_SET_POINT(buf, bufpos, ind) \ 1517 #define JUST_SET_POINT(buf, charbpos, ind) \
1518 do \ 1518 do \
1519 { \ 1519 { \
1520 buf->bufpt = (bufpos); \ 1520 buf->bufpt = (charbpos); \
1521 buf->pt = (ind); \ 1521 buf->pt = (ind); \
1522 } while (0) 1522 } while (0)
1523 1523
1524 /* Set a buffer's point. */ 1524 /* Set a buffer's point. */
1525 1525
1526 void 1526 void
1527 set_buffer_point (struct buffer *buf, Bufpos bufpos, Bytind bytpos) 1527 set_buffer_point (struct buffer *buf, Charbpos charbpos, Bytebpos bytpos)
1528 { 1528 {
1529 assert (bytpos >= BI_BUF_BEGV (buf) && bytpos <= BI_BUF_ZV (buf)); 1529 assert (bytpos >= BI_BUF_BEGV (buf) && bytpos <= BI_BUF_ZV (buf));
1530 if (bytpos == BI_BUF_PT (buf)) 1530 if (bytpos == BI_BUF_PT (buf))
1531 return; 1531 return;
1532 JUST_SET_POINT (buf, bufpos, bytpos); 1532 JUST_SET_POINT (buf, charbpos, bytpos);
1533 MARK_POINT_CHANGED; 1533 MARK_POINT_CHANGED;
1534 assert (MARKERP (buf->point_marker)); 1534 assert (MARKERP (buf->point_marker));
1535 XMARKER (buf->point_marker)->memind = 1535 XMARKER (buf->point_marker)->membpos =
1536 bytind_to_memind (buf, bytpos); 1536 bytebpos_to_membpos (buf, bytpos);
1537 1537
1538 /* FSF makes sure that PT is not being set within invisible text. 1538 /* FSF makes sure that PT is not being set within invisible text.
1539 However, this is the wrong place for that check. The check 1539 However, this is the wrong place for that check. The check
1540 should happen only at the next redisplay. */ 1540 should happen only at the next redisplay. */
1541 1541
1556 } 1556 }
1557 1557
1558 /* Do the correct marker-like adjustment on MPOS (see below). FROM, TO, 1558 /* Do the correct marker-like adjustment on MPOS (see below). FROM, TO,
1559 and AMOUNT are as in adjust_markers(). If MPOS doesn't need to be 1559 and AMOUNT are as in adjust_markers(). If MPOS doesn't need to be
1560 adjusted, nothing will happen. */ 1560 adjusted, nothing will happen. */
1561 Memind 1561 Membpos
1562 do_marker_adjustment (Memind mpos, Memind from, 1562 do_marker_adjustment (Membpos mpos, Membpos from,
1563 Memind to, Bytecount amount) 1563 Membpos to, Bytecount amount)
1564 { 1564 {
1565 if (amount > 0) 1565 if (amount > 0)
1566 { 1566 {
1567 if (mpos > to && mpos < to + amount) 1567 if (mpos > to && mpos < to + amount)
1568 mpos = to + amount; 1568 mpos = to + amount;
1600 The reason for the use of exclusive and inclusive is that markers at 1600 The reason for the use of exclusive and inclusive is that markers at
1601 the gap always sit at the beginning, not at the end. 1601 the gap always sit at the beginning, not at the end.
1602 */ 1602 */
1603 1603
1604 static void 1604 static void
1605 adjust_markers (struct buffer *buf, Memind from, Memind to, 1605 adjust_markers (struct buffer *buf, Membpos from, Membpos to,
1606 Bytecount amount) 1606 Bytecount amount)
1607 { 1607 {
1608 Lisp_Marker *m; 1608 Lisp_Marker *m;
1609 1609
1610 for (m = BUF_MARKERS (buf); m; m = marker_next (m)) 1610 for (m = BUF_MARKERS (buf); m; m = marker_next (m))
1611 m->memind = do_marker_adjustment (m->memind, from, to, amount); 1611 m->membpos = do_marker_adjustment (m->membpos, from, to, amount);
1612 } 1612 }
1613 1613
1614 /* Adjust markers whose insertion-type is t 1614 /* Adjust markers whose insertion-type is t
1615 for an insertion of AMOUNT characters at POS. */ 1615 for an insertion of AMOUNT characters at POS. */
1616 1616
1617 static void 1617 static void
1618 adjust_markers_for_insert (struct buffer *buf, Memind ind, Bytecount amount) 1618 adjust_markers_for_insert (struct buffer *buf, Membpos ind, Bytecount amount)
1619 { 1619 {
1620 Lisp_Marker *m; 1620 Lisp_Marker *m;
1621 1621
1622 for (m = BUF_MARKERS (buf); m; m = marker_next (m)) 1622 for (m = BUF_MARKERS (buf); m; m = marker_next (m))
1623 { 1623 {
1624 if (m->insertion_type && m->memind == ind) 1624 if (m->insertion_type && m->membpos == ind)
1625 m->memind += amount; 1625 m->membpos += amount;
1626 } 1626 }
1627 } 1627 }
1628 1628
1629 1629
1630 /************************************************************************/ 1630 /************************************************************************/
1638 #define GAP_MOVE_CHUNK 300000 1638 #define GAP_MOVE_CHUNK 300000
1639 1639
1640 /* Move the gap to POS, which is less than the current GPT. */ 1640 /* Move the gap to POS, which is less than the current GPT. */
1641 1641
1642 static void 1642 static void
1643 gap_left (struct buffer *buf, Bytind pos) 1643 gap_left (struct buffer *buf, Bytebpos pos)
1644 { 1644 {
1645 Bufbyte *to, *from; 1645 Intbyte *to, *from;
1646 Bytecount i; 1646 Bytecount i;
1647 Bytind new_s1; 1647 Bytebpos new_s1;
1648 struct buffer *mbuf; 1648 struct buffer *mbuf;
1649 Lisp_Object bufcons; 1649 Lisp_Object bufcons;
1650 1650
1651 from = BUF_GPT_ADDR (buf); 1651 from = BUF_GPT_ADDR (buf);
1652 to = from + BUF_GAP_SIZE (buf); 1652 to = from + BUF_GAP_SIZE (buf);
1709 #endif 1709 #endif
1710 QUIT; 1710 QUIT;
1711 } 1711 }
1712 1712
1713 static void 1713 static void
1714 gap_right (struct buffer *buf, Bytind pos) 1714 gap_right (struct buffer *buf, Bytebpos pos)
1715 { 1715 {
1716 Bufbyte *to, *from; 1716 Intbyte *to, *from;
1717 Bytecount i; 1717 Bytecount i;
1718 Bytind new_s1; 1718 Bytebpos new_s1;
1719 struct buffer *mbuf; 1719 struct buffer *mbuf;
1720 Lisp_Object bufcons; 1720 Lisp_Object bufcons;
1721 1721
1722 to = BUF_GPT_ADDR (buf); 1722 to = BUF_GPT_ADDR (buf);
1723 from = to + BUF_GAP_SIZE (buf); 1723 from = to + BUF_GAP_SIZE (buf);
1792 1792
1793 /* Move gap to position `pos'. 1793 /* Move gap to position `pos'.
1794 Note that this can quit! */ 1794 Note that this can quit! */
1795 1795
1796 static void 1796 static void
1797 move_gap (struct buffer *buf, Bytind pos) 1797 move_gap (struct buffer *buf, Bytebpos pos)
1798 { 1798 {
1799 if (! BUF_BEG_ADDR (buf)) 1799 if (! BUF_BEG_ADDR (buf))
1800 abort (); 1800 abort ();
1801 if (pos < BI_BUF_GPT (buf)) 1801 if (pos < BI_BUF_GPT (buf))
1802 gap_left (buf, pos); 1802 gap_left (buf, pos);
1808 1808
1809 static void 1809 static void
1810 merge_gap_with_end_gap (struct buffer *buf) 1810 merge_gap_with_end_gap (struct buffer *buf)
1811 { 1811 {
1812 Lisp_Object tem; 1812 Lisp_Object tem;
1813 Bytind real_gap_loc; 1813 Bytebpos real_gap_loc;
1814 Bytecount old_gap_size; 1814 Bytecount old_gap_size;
1815 Bytecount increment; 1815 Bytecount increment;
1816 1816
1817 increment = BUF_END_GAP_SIZE (buf); 1817 increment = BUF_END_GAP_SIZE (buf);
1818 SET_BUF_END_GAP_SIZE (buf, 0); 1818 SET_BUF_END_GAP_SIZE (buf, 0);
1850 /* Make the gap INCREMENT bytes longer. */ 1850 /* Make the gap INCREMENT bytes longer. */
1851 1851
1852 static void 1852 static void
1853 make_gap (struct buffer *buf, Bytecount increment) 1853 make_gap (struct buffer *buf, Bytecount increment)
1854 { 1854 {
1855 Bufbyte *result; 1855 Intbyte *result;
1856 Lisp_Object tem; 1856 Lisp_Object tem;
1857 Bytind real_gap_loc; 1857 Bytebpos real_gap_loc;
1858 Bytecount old_gap_size; 1858 Bytecount old_gap_size;
1859 1859
1860 /* If we have to get more space, get enough to last a while. We use 1860 /* If we have to get more space, get enough to last a while. We use
1861 a geometric progression that saves on realloc space. */ 1861 a geometric progression that saves on realloc space. */
1862 increment += 2000 + ((BI_BUF_Z (buf) - BI_BUF_BEG (buf)) / 8); 1862 increment += 2000 + ((BI_BUF_Z (buf) - BI_BUF_BEG (buf)) / 8);
1918 /************************************************************************/ 1918 /************************************************************************/
1919 1919
1920 /* Those magic changes ... */ 1920 /* Those magic changes ... */
1921 1921
1922 static void 1922 static void
1923 buffer_signal_changed_region (struct buffer *buf, Bufpos start, 1923 buffer_signal_changed_region (struct buffer *buf, Charbpos start,
1924 Bufpos end) 1924 Charbpos end)
1925 { 1925 {
1926 /* The changed region is recorded as the number of unchanged 1926 /* The changed region is recorded as the number of unchanged
1927 characters from the beginning and from the end of the 1927 characters from the beginning and from the end of the
1928 buffer. This obviates much of the need of shifting the 1928 buffer. This obviates much of the need of shifting the
1929 region around to compensate for insertions and deletions. 1929 region around to compensate for insertions and deletions.
1935 buf->changes->end_unchanged > BUF_Z (buf) - end) 1935 buf->changes->end_unchanged > BUF_Z (buf) - end)
1936 buf->changes->end_unchanged = BUF_Z (buf) - end; 1936 buf->changes->end_unchanged = BUF_Z (buf) - end;
1937 } 1937 }
1938 1938
1939 void 1939 void
1940 buffer_extent_signal_changed_region (struct buffer *buf, Bufpos start, 1940 buffer_extent_signal_changed_region (struct buffer *buf, Charbpos start,
1941 Bufpos end) 1941 Charbpos end)
1942 { 1942 {
1943 if (buf->changes->begin_extent_unchanged < 0 || 1943 if (buf->changes->begin_extent_unchanged < 0 ||
1944 buf->changes->begin_extent_unchanged > start - BUF_BEG (buf)) 1944 buf->changes->begin_extent_unchanged > start - BUF_BEG (buf))
1945 buf->changes->begin_extent_unchanged = start - BUF_BEG (buf); 1945 buf->changes->begin_extent_unchanged = start - BUF_BEG (buf);
1946 if (buf->changes->end_extent_unchanged < 0 || 1946 if (buf->changes->end_extent_unchanged < 0 ||
1957 buf->changes->end_extent_unchanged = -1; 1957 buf->changes->end_extent_unchanged = -1;
1958 buf->changes->newline_was_deleted = 0; 1958 buf->changes->newline_was_deleted = 0;
1959 } 1959 }
1960 1960
1961 static void 1961 static void
1962 signal_after_change (struct buffer *buf, Bufpos start, Bufpos orig_end, 1962 signal_after_change (struct buffer *buf, Charbpos start, Charbpos orig_end,
1963 Bufpos new_end); 1963 Charbpos new_end);
1964 1964
1965 1965
1966 /* Call the after-change-functions according to the changes made so far 1966 /* Call the after-change-functions according to the changes made so far
1967 and treat all further changes as single until the outermost 1967 and treat all further changes as single until the outermost
1968 multiple change exits. This is called when the outermost multiple 1968 multiple change exits. This is called when the outermost multiple
1982 /* Call the after-change-functions except when they've already been 1982 /* Call the after-change-functions except when they've already been
1983 called or when there were no changes made to the buffer at all. */ 1983 called or when there were no changes made to the buffer at all. */
1984 if (buf->text->changes->mc_begin != 0 && 1984 if (buf->text->changes->mc_begin != 0 &&
1985 buf->text->changes->mc_begin_signaled) 1985 buf->text->changes->mc_begin_signaled)
1986 { 1986 {
1987 Bufpos real_mc_begin = buf->text->changes->mc_begin; 1987 Charbpos real_mc_begin = buf->text->changes->mc_begin;
1988 buf->text->changes->mc_begin = 0; 1988 buf->text->changes->mc_begin = 0;
1989 1989
1990 signal_after_change (buf, real_mc_begin, buf->text->changes->mc_orig_end, 1990 signal_after_change (buf, real_mc_begin, buf->text->changes->mc_orig_end,
1991 buf->text->changes->mc_new_end); 1991 buf->text->changes->mc_new_end);
1992 } 1992 }
2035 through a `combine-after-change-calls' special form, which is 2035 through a `combine-after-change-calls' special form, which is
2036 essentially equivalent to this function. We should consider 2036 essentially equivalent to this function. We should consider
2037 whether we want to introduce a similar Lisp form. */ 2037 whether we want to introduce a similar Lisp form. */
2038 2038
2039 int 2039 int
2040 begin_multiple_change (struct buffer *buf, Bufpos start, Bufpos end) 2040 begin_multiple_change (struct buffer *buf, Charbpos start, Charbpos end)
2041 { 2041 {
2042 /* This function can GC */ 2042 /* This function can GC */
2043 int count = -1; 2043 int count = -1;
2044 if (buf->text->changes->in_multiple_change) 2044 if (buf->text->changes->in_multiple_change)
2045 { 2045 {
2122 2122
2123 /* Signal a change to the buffer immediately before it happens. 2123 /* Signal a change to the buffer immediately before it happens.
2124 START and END are the bounds of the text to be changed. */ 2124 START and END are the bounds of the text to be changed. */
2125 2125
2126 static void 2126 static void
2127 signal_before_change (struct buffer *buf, Bufpos start, Bufpos end) 2127 signal_before_change (struct buffer *buf, Charbpos start, Charbpos end)
2128 { 2128 {
2129 /* This function can GC */ 2129 /* This function can GC */
2130 struct buffer *mbuf; 2130 struct buffer *mbuf;
2131 Lisp_Object bufcons; 2131 Lisp_Object bufcons;
2132 2132
2210 buf->text->changes->mc_begin_signaled = 1; 2210 buf->text->changes->mc_begin_signaled = 1;
2211 } 2211 }
2212 } 2212 }
2213 2213
2214 /* Signal a change immediately after it happens. 2214 /* Signal a change immediately after it happens.
2215 START is the bufpos of the start of the changed text. 2215 START is the charbpos of the start of the changed text.
2216 ORIG_END is the bufpos of the end of the before-changed text. 2216 ORIG_END is the charbpos of the end of the before-changed text.
2217 NEW_END is the bufpos of the end of the after-changed text. 2217 NEW_END is the charbpos of the end of the after-changed text.
2218 */ 2218 */
2219 2219
2220 static void 2220 static void
2221 signal_after_change (struct buffer *buf, Bufpos start, Bufpos orig_end, 2221 signal_after_change (struct buffer *buf, Charbpos start, Charbpos orig_end,
2222 Bufpos new_end) 2222 Charbpos new_end)
2223 { 2223 {
2224 /* This function can GC */ 2224 /* This function can GC */
2225 struct buffer *mbuf; 2225 struct buffer *mbuf;
2226 Lisp_Object bufcons; 2226 Lisp_Object bufcons;
2227 2227
2299 to END. This checks the read-only properties of the region, calls 2299 to END. This checks the read-only properties of the region, calls
2300 the necessary modification hooks, and warns the next redisplay that 2300 the necessary modification hooks, and warns the next redisplay that
2301 it should pay attention to that area. */ 2301 it should pay attention to that area. */
2302 2302
2303 static void 2303 static void
2304 prepare_to_modify_buffer (struct buffer *buf, Bufpos start, Bufpos end, 2304 prepare_to_modify_buffer (struct buffer *buf, Charbpos start, Charbpos end,
2305 int lockit) 2305 int lockit)
2306 { 2306 {
2307 /* This function can GC */ 2307 /* This function can GC */
2308 /* dmoore - This function can also kill the buffer buf, the current 2308 /* dmoore - This function can also kill the buffer buf, the current
2309 buffer, and do anything it pleases. So if you call it, be 2309 buffer, and do anything it pleases. So if you call it, be
2368 /************************************************************************/ 2368 /************************************************************************/
2369 /* Insertion of strings */ 2369 /* Insertion of strings */
2370 /************************************************************************/ 2370 /************************************************************************/
2371 2371
2372 void 2372 void
2373 fixup_internal_substring (const Bufbyte *nonreloc, Lisp_Object reloc, 2373 fixup_internal_substring (const Intbyte *nonreloc, Lisp_Object reloc,
2374 Bytecount offset, Bytecount *len) 2374 Bytecount offset, Bytecount *len)
2375 { 2375 {
2376 assert ((nonreloc && NILP (reloc)) || (!nonreloc && STRINGP (reloc))); 2376 assert ((nonreloc && NILP (reloc)) || (!nonreloc && STRINGP (reloc)));
2377 2377
2378 if (*len < 0) 2378 if (*len < 0)
2380 if (nonreloc) 2380 if (nonreloc)
2381 *len = strlen ((const char *) nonreloc) - offset; 2381 *len = strlen ((const char *) nonreloc) - offset;
2382 else 2382 else
2383 *len = XSTRING_LENGTH (reloc) - offset; 2383 *len = XSTRING_LENGTH (reloc) - offset;
2384 } 2384 }
2385 #ifdef ERROR_CHECK_BUFPOS 2385 #ifdef ERROR_CHECK_CHARBPOS
2386 assert (*len >= 0); 2386 assert (*len >= 0);
2387 if (STRINGP (reloc)) 2387 if (STRINGP (reloc))
2388 { 2388 {
2389 assert (offset >= 0 && offset <= XSTRING_LENGTH (reloc)); 2389 assert (offset >= 0 && offset <= XSTRING_LENGTH (reloc));
2390 assert (offset + *len <= XSTRING_LENGTH (reloc)); 2390 assert (offset + *len <= XSTRING_LENGTH (reloc));
2391 } 2391 }
2392 #endif 2392 #endif
2393 } 2393 }
2394 2394
2395 /* Insert a string into BUF at Bufpos POS. The string data comes 2395 /* Insert a string into BUF at Charbpos POS. The string data comes
2396 from one of two sources: constant, non-relocatable data (specified 2396 from one of two sources: constant, non-relocatable data (specified
2397 in NONRELOC), or a Lisp string object (specified in RELOC), which 2397 in NONRELOC), or a Lisp string object (specified in RELOC), which
2398 is relocatable and may have extent data that needs to be copied 2398 is relocatable and may have extent data that needs to be copied
2399 into the buffer. OFFSET and LENGTH specify the substring of the 2399 into the buffer. OFFSET and LENGTH specify the substring of the
2400 data that is actually to be inserted. As a special case, if POS 2400 data that is actually to be inserted. As a special case, if POS
2409 visiting a new file; it inhibits the locking checks normally done 2409 visiting a new file; it inhibits the locking checks normally done
2410 before modifying a buffer. Similar checks were already done 2410 before modifying a buffer. Similar checks were already done
2411 in the higher-level Lisp functions calling insert-file-contents. */ 2411 in the higher-level Lisp functions calling insert-file-contents. */
2412 2412
2413 Charcount 2413 Charcount
2414 buffer_insert_string_1 (struct buffer *buf, Bufpos pos, 2414 buffer_insert_string_1 (struct buffer *buf, Charbpos pos,
2415 const Bufbyte *nonreloc, Lisp_Object reloc, 2415 const Intbyte *nonreloc, Lisp_Object reloc,
2416 Bytecount offset, Bytecount length, 2416 Bytecount offset, Bytecount length,
2417 int flags) 2417 int flags)
2418 { 2418 {
2419 /* This function can GC */ 2419 /* This function can GC */
2420 struct gcpro gcpro1; 2420 struct gcpro gcpro1;
2421 Bytind ind; 2421 Bytebpos ind;
2422 Charcount cclen; 2422 Charcount cclen;
2423 int move_point = 0; 2423 int move_point = 0;
2424 struct buffer *mbuf; 2424 struct buffer *mbuf;
2425 Lisp_Object bufcons; 2425 Lisp_Object bufcons;
2426 2426
2470 2470
2471 /* string may have been relocated up to this point */ 2471 /* string may have been relocated up to this point */
2472 if (STRINGP (reloc)) 2472 if (STRINGP (reloc))
2473 nonreloc = XSTRING_DATA (reloc); 2473 nonreloc = XSTRING_DATA (reloc);
2474 2474
2475 ind = bufpos_to_bytind (buf, pos); 2475 ind = charbpos_to_bytebpos (buf, pos);
2476 cclen = bytecount_to_charcount (nonreloc + offset, length); 2476 cclen = bytecount_to_charcount (nonreloc + offset, length);
2477 2477
2478 if (ind != BI_BUF_GPT (buf)) 2478 if (ind != BI_BUF_GPT (buf))
2479 /* #### if debug-on-quit is invoked and the user changes the 2479 /* #### if debug-on-quit is invoked and the user changes the
2480 buffer, bad things can happen. This is a rampant problem 2480 buffer, bad things can happen. This is a rampant problem
2523 } 2523 }
2524 2524
2525 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons) 2525 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2526 { 2526 {
2527 /* We know the gap is at IND so the cast is OK. */ 2527 /* We know the gap is at IND so the cast is OK. */
2528 adjust_markers_for_insert (mbuf, (Memind) ind, length); 2528 adjust_markers_for_insert (mbuf, (Membpos) ind, length);
2529 } 2529 }
2530 2530
2531 /* Point logically doesn't move, but may need to be adjusted because 2531 /* Point logically doesn't move, but may need to be adjusted because
2532 it's a byte index. point-marker doesn't change because it's a 2532 it's a byte index. point-marker doesn't change because it's a
2533 memory index. */ 2533 memory index. */
2553 if (flags & INSDEL_BEFORE_MARKERS) 2553 if (flags & INSDEL_BEFORE_MARKERS)
2554 { 2554 {
2555 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons) 2555 MAP_INDIRECT_BUFFERS (buf, mbuf, bufcons)
2556 { 2556 {
2557 /* ind - 1 is correct because the FROM argument is exclusive. 2557 /* ind - 1 is correct because the FROM argument is exclusive.
2558 I formerly used DEC_BYTIND() but that caused problems at the 2558 I formerly used DEC_BYTEBPOS() but that caused problems at the
2559 beginning of the buffer. */ 2559 beginning of the buffer. */
2560 adjust_markers (mbuf, ind - 1, ind, length); 2560 adjust_markers (mbuf, ind - 1, ind, length);
2561 } 2561 }
2562 } 2562 }
2563 2563
2574 BUF and POS specify the buffer and location where the insertion is 2574 BUF and POS specify the buffer and location where the insertion is
2575 to take place. (If POS is -1, text is inserted at point and point 2575 to take place. (If POS is -1, text is inserted at point and point
2576 moves forward past the text.) FLAGS is as above. */ 2576 moves forward past the text.) FLAGS is as above. */
2577 2577
2578 Charcount 2578 Charcount
2579 buffer_insert_raw_string_1 (struct buffer *buf, Bufpos pos, 2579 buffer_insert_raw_string_1 (struct buffer *buf, Charbpos pos,
2580 const Bufbyte *nonreloc, Bytecount length, 2580 const Intbyte *nonreloc, Bytecount length,
2581 int flags) 2581 int flags)
2582 { 2582 {
2583 /* This function can GC */ 2583 /* This function can GC */
2584 return buffer_insert_string_1 (buf, pos, nonreloc, Qnil, 0, length, 2584 return buffer_insert_string_1 (buf, pos, nonreloc, Qnil, 0, length,
2585 flags); 2585 flags);
2586 } 2586 }
2587 2587
2588 Charcount 2588 Charcount
2589 buffer_insert_lisp_string_1 (struct buffer *buf, Bufpos pos, Lisp_Object str, 2589 buffer_insert_lisp_string_1 (struct buffer *buf, Charbpos pos, Lisp_Object str,
2590 int flags) 2590 int flags)
2591 { 2591 {
2592 /* This function can GC */ 2592 /* This function can GC */
2593 #ifdef ERROR_CHECK_TYPECHECK 2593 #ifdef ERROR_CHECK_TYPECHECK
2594 assert (STRINGP (str)); 2594 assert (STRINGP (str));
2599 } 2599 }
2600 2600
2601 /* Insert the null-terminated string S (in external format). */ 2601 /* Insert the null-terminated string S (in external format). */
2602 2602
2603 Charcount 2603 Charcount
2604 buffer_insert_c_string_1 (struct buffer *buf, Bufpos pos, const char *s, 2604 buffer_insert_c_string_1 (struct buffer *buf, Charbpos pos, const char *s,
2605 int flags) 2605 int flags)
2606 { 2606 {
2607 /* This function can GC */ 2607 /* This function can GC */
2608 const char *translated = GETTEXT (s); 2608 const char *translated = GETTEXT (s);
2609 return buffer_insert_string_1 (buf, pos, (const Bufbyte *) translated, Qnil, 2609 return buffer_insert_string_1 (buf, pos, (const Intbyte *) translated, Qnil,
2610 0, strlen (translated), flags); 2610 0, strlen (translated), flags);
2611 } 2611 }
2612 2612
2613 Charcount 2613 Charcount
2614 buffer_insert_emacs_char_1 (struct buffer *buf, Bufpos pos, Emchar ch, 2614 buffer_insert_emacs_char_1 (struct buffer *buf, Charbpos pos, Emchar ch,
2615 int flags) 2615 int flags)
2616 { 2616 {
2617 /* This function can GC */ 2617 /* This function can GC */
2618 Bufbyte str[MAX_EMCHAR_LEN]; 2618 Intbyte str[MAX_EMCHAR_LEN];
2619 Bytecount len = set_charptr_emchar (str, ch); 2619 Bytecount len = set_charptr_emchar (str, ch);
2620 return buffer_insert_string_1 (buf, pos, str, Qnil, 0, len, flags); 2620 return buffer_insert_string_1 (buf, pos, str, Qnil, 0, len, flags);
2621 } 2621 }
2622 2622
2623 Charcount 2623 Charcount
2624 buffer_insert_c_char_1 (struct buffer *buf, Bufpos pos, char c, 2624 buffer_insert_c_char_1 (struct buffer *buf, Charbpos pos, char c,
2625 int flags) 2625 int flags)
2626 { 2626 {
2627 /* This function can GC */ 2627 /* This function can GC */
2628 return buffer_insert_emacs_char_1 (buf, pos, (Emchar) (unsigned char) c, 2628 return buffer_insert_emacs_char_1 (buf, pos, (Emchar) (unsigned char) c,
2629 flags); 2629 flags);
2630 } 2630 }
2631 2631
2632 Charcount 2632 Charcount
2633 buffer_insert_from_buffer_1 (struct buffer *buf, Bufpos pos, 2633 buffer_insert_from_buffer_1 (struct buffer *buf, Charbpos pos,
2634 struct buffer *buf2, Bufpos pos2, 2634 struct buffer *buf2, Charbpos pos2,
2635 Charcount length, int flags) 2635 Charcount length, int flags)
2636 { 2636 {
2637 /* This function can GC */ 2637 /* This function can GC */
2638 Lisp_Object str = make_string_from_buffer (buf2, pos2, length); 2638 Lisp_Object str = make_string_from_buffer (buf2, pos2, length);
2639 return buffer_insert_string_1 (buf, pos, 0, str, 0, 2639 return buffer_insert_string_1 (buf, pos, 0, str, 0,
2646 /************************************************************************/ 2646 /************************************************************************/
2647 2647
2648 /* Delete characters in buffer from FROM up to (but not including) TO. */ 2648 /* Delete characters in buffer from FROM up to (but not including) TO. */
2649 2649
2650 void 2650 void
2651 buffer_delete_range (struct buffer *buf, Bufpos from, Bufpos to, int flags) 2651 buffer_delete_range (struct buffer *buf, Charbpos from, Charbpos to, int flags)
2652 { 2652 {
2653 /* This function can GC */ 2653 /* This function can GC */
2654 Charcount numdel; 2654 Charcount numdel;
2655 Bytind bi_from, bi_to; 2655 Bytebpos bi_from, bi_to;
2656 Bytecount bc_numdel; 2656 Bytecount bc_numdel;
2657 EMACS_INT shortage; 2657 EMACS_INT shortage;
2658 struct buffer *mbuf; 2658 struct buffer *mbuf;
2659 Lisp_Object bufcons; 2659 Lisp_Object bufcons;
2660 2660
2700 mbuf->changes->newline_was_deleted = 1; 2700 mbuf->changes->newline_was_deleted = 1;
2701 } 2701 }
2702 } 2702 }
2703 } 2703 }
2704 2704
2705 bi_from = bufpos_to_bytind (buf, from); 2705 bi_from = charbpos_to_bytebpos (buf, from);
2706 bi_to = bufpos_to_bytind (buf, to); 2706 bi_to = charbpos_to_bytebpos (buf, to);
2707 bc_numdel = bi_to - bi_from; 2707 bc_numdel = bi_to - bi_from;
2708 2708
2709 delete_invalidate_line_number_cache (buf, from, to); 2709 delete_invalidate_line_number_cache (buf, from, to);
2710 2710
2711 if (to == BUF_Z (buf) && 2711 if (to == BUF_Z (buf) &&
2719 } 2719 }
2720 BUF_MODIFF (buf)++; 2720 BUF_MODIFF (buf)++;
2721 MARK_BUFFERS_CHANGED; 2721 MARK_BUFFERS_CHANGED;
2722 2722
2723 /* #### Point used to be modified here, but this causes problems 2723 /* #### Point used to be modified here, but this causes problems
2724 with MULE, as point is used to calculate bytinds, and if the 2724 with MULE, as point is used to calculate bytebposs, and if the
2725 offset in bc_numdel causes point to move to a non first-byte 2725 offset in bc_numdel causes point to move to a non first-byte
2726 location, causing some other function to throw an assertion 2726 location, causing some other function to throw an assertion
2727 in ASSERT_VALID_BYTIND. I've moved the code to right after 2727 in ASSERT_VALID_BYTEBPOS. I've moved the code to right after
2728 the other movements and adjustments, but before the gap is 2728 the other movements and adjustments, but before the gap is
2729 moved. -- jh 970813 */ 2729 moved. -- jh 970813 */
2730 2730
2731 /* Detach any extents that are completely within the range [FROM, TO], 2731 /* Detach any extents that are completely within the range [FROM, TO],
2732 if the extents are detachable. 2732 if the extents are detachable.
2794 } 2794 }
2795 BUF_MODIFF (buf)++; 2795 BUF_MODIFF (buf)++;
2796 MARK_BUFFERS_CHANGED; 2796 MARK_BUFFERS_CHANGED;
2797 2797
2798 /* #### Point used to be modified here, but this causes problems 2798 /* #### Point used to be modified here, but this causes problems
2799 with MULE, as point is used to calculate bytinds, and if the 2799 with MULE, as point is used to calculate bytebposs, and if the
2800 offset in bc_numdel causes point to move to a non first-byte 2800 offset in bc_numdel causes point to move to a non first-byte
2801 location, causing some other function to throw an assertion 2801 location, causing some other function to throw an assertion
2802 in ASSERT_VALID_BYTIND. I've moved the code to right after 2802 in ASSERT_VALID_BYTEBPOS. I've moved the code to right after
2803 the other movements and adjustments, but before the gap is 2803 the other movements and adjustments, but before the gap is
2804 moved. -- jh 970813 */ 2804 moved. -- jh 970813 */
2805 2805
2806 /* Detach any extents that are completely within the range [FROM, TO], 2806 /* Detach any extents that are completely within the range [FROM, TO],
2807 if the extents are detachable. 2807 if the extents are detachable.
2876 /************************************************************************/ 2876 /************************************************************************/
2877 2877
2878 /* Replace the character at POS in buffer B with CH. */ 2878 /* Replace the character at POS in buffer B with CH. */
2879 2879
2880 void 2880 void
2881 buffer_replace_char (struct buffer *buf, Bufpos pos, Emchar ch, 2881 buffer_replace_char (struct buffer *buf, Charbpos pos, Emchar ch,
2882 int not_real_change, int force_lock_check) 2882 int not_real_change, int force_lock_check)
2883 { 2883 {
2884 /* This function can GC */ 2884 /* This function can GC */
2885 Bufbyte curstr[MAX_EMCHAR_LEN]; 2885 Intbyte curstr[MAX_EMCHAR_LEN];
2886 Bufbyte newstr[MAX_EMCHAR_LEN]; 2886 Intbyte newstr[MAX_EMCHAR_LEN];
2887 Bytecount curlen, newlen; 2887 Bytecount curlen, newlen;
2888 2888
2889 /* Defensive steps just in case a buffer gets deleted and a calling 2889 /* Defensive steps just in case a buffer gets deleted and a calling
2890 function doesn't notice it. */ 2890 function doesn't notice it. */
2891 if (!BUFFER_LIVE_P (buf)) 2891 if (!BUFFER_LIVE_P (buf))
2984 2984
2985 /* Make a string from a buffer. This needs to take into account the gap, 2985 /* Make a string from a buffer. This needs to take into account the gap,
2986 and add any necessary extents from the buffer. */ 2986 and add any necessary extents from the buffer. */
2987 2987
2988 static Lisp_Object 2988 static Lisp_Object
2989 make_string_from_buffer_1 (struct buffer *buf, Bufpos pos, Charcount length, 2989 make_string_from_buffer_1 (struct buffer *buf, Charbpos pos, Charcount length,
2990 int no_extents) 2990 int no_extents)
2991 { 2991 {
2992 /* This function can GC */ 2992 /* This function can GC */
2993 Bytind bi_ind = bufpos_to_bytind (buf, pos); 2993 Bytebpos bi_ind = charbpos_to_bytebpos (buf, pos);
2994 Bytecount bi_len = bufpos_to_bytind (buf, pos + length) - bi_ind; 2994 Bytecount bi_len = charbpos_to_bytebpos (buf, pos + length) - bi_ind;
2995 Lisp_Object val = make_uninit_string (bi_len); 2995 Lisp_Object val = make_uninit_string (bi_len);
2996 2996
2997 struct gcpro gcpro1; 2997 struct gcpro gcpro1;
2998 GCPRO1 (val); 2998 GCPRO1 (val);
2999 2999
3000 if (!no_extents) 3000 if (!no_extents)
3001 add_string_extents (val, buf, bi_ind, bi_len); 3001 add_string_extents (val, buf, bi_ind, bi_len);
3002 3002
3003 { 3003 {
3004 Bytecount len1 = BI_BUF_GPT (buf) - bi_ind; 3004 Bytecount len1 = BI_BUF_GPT (buf) - bi_ind;
3005 Bufbyte *start1 = BI_BUF_BYTE_ADDRESS (buf, bi_ind); 3005 Intbyte *start1 = BI_BUF_BYTE_ADDRESS (buf, bi_ind);
3006 Bufbyte *dest = XSTRING_DATA (val); 3006 Intbyte *dest = XSTRING_DATA (val);
3007 3007
3008 if (len1 < 0) 3008 if (len1 < 0)
3009 { 3009 {
3010 /* Completely after gap */ 3010 /* Completely after gap */
3011 memcpy (dest, start1, bi_len); 3011 memcpy (dest, start1, bi_len);
3016 memcpy (dest, start1, bi_len); 3016 memcpy (dest, start1, bi_len);
3017 } 3017 }
3018 else 3018 else
3019 { 3019 {
3020 /* Spans gap */ 3020 /* Spans gap */
3021 Bytind pos2 = bi_ind + len1; 3021 Bytebpos pos2 = bi_ind + len1;
3022 Bufbyte *start2 = BI_BUF_BYTE_ADDRESS (buf, pos2); 3022 Intbyte *start2 = BI_BUF_BYTE_ADDRESS (buf, pos2);
3023 3023
3024 memcpy (dest, start1, len1); 3024 memcpy (dest, start1, len1);
3025 memcpy (dest + len1, start2, bi_len - len1); 3025 memcpy (dest + len1, start2, bi_len - len1);
3026 } 3026 }
3027 } 3027 }
3029 UNGCPRO; 3029 UNGCPRO;
3030 return val; 3030 return val;
3031 } 3031 }
3032 3032
3033 Lisp_Object 3033 Lisp_Object
3034 make_string_from_buffer (struct buffer *buf, Bufpos pos, Charcount length) 3034 make_string_from_buffer (struct buffer *buf, Charbpos pos, Charcount length)
3035 { 3035 {
3036 return make_string_from_buffer_1 (buf, pos, length, 0); 3036 return make_string_from_buffer_1 (buf, pos, length, 0);
3037 } 3037 }
3038 3038
3039 Lisp_Object 3039 Lisp_Object
3040 make_string_from_buffer_no_extents (struct buffer *buf, Bufpos pos, 3040 make_string_from_buffer_no_extents (struct buffer *buf, Charbpos pos,
3041 Charcount length) 3041 Charcount length)
3042 { 3042 {
3043 return make_string_from_buffer_1 (buf, pos, length, 1); 3043 return make_string_from_buffer_1 (buf, pos, length, 1);
3044 } 3044 }
3045 3045
3046 void 3046 void
3047 barf_if_buffer_read_only (struct buffer *buf, Bufpos from, Bufpos to) 3047 barf_if_buffer_read_only (struct buffer *buf, Charbpos from, Charbpos to)
3048 { 3048 {
3049 Lisp_Object buffer; 3049 Lisp_Object buffer;
3050 Lisp_Object iro; 3050 Lisp_Object iro;
3051 3051
3052 XSETBUFFER (buffer, buf); 3052 XSETBUFFER (buffer, buf);
3063 if (from > 0) 3063 if (from > 0)
3064 { 3064 {
3065 if (to < 0) 3065 if (to < 0)
3066 to = from; 3066 to = from;
3067 verify_extent_modification (buffer, 3067 verify_extent_modification (buffer,
3068 bufpos_to_bytind (buf, from), 3068 charbpos_to_bytebpos (buf, from),
3069 bufpos_to_bytind (buf, to), 3069 charbpos_to_bytebpos (buf, to),
3070 iro); 3070 iro);
3071 } 3071 }
3072 } 3072 }
3073 3073
3074 void 3074 void
3075 find_charsets_in_bufbyte_string (unsigned char *charsets, const Bufbyte *str, 3075 find_charsets_in_intbyte_string (unsigned char *charsets, const Intbyte *str,
3076 Bytecount len) 3076 Bytecount len)
3077 { 3077 {
3078 #ifndef MULE 3078 #ifndef MULE
3079 /* Telescope this. */ 3079 /* Telescope this. */
3080 charsets[0] = 1; 3080 charsets[0] = 1;
3081 #else 3081 #else
3082 const Bufbyte *strend = str + len; 3082 const Intbyte *strend = str + len;
3083 memset (charsets, 0, NUM_LEADING_BYTES); 3083 memset (charsets, 0, NUM_LEADING_BYTES);
3084 3084
3085 /* #### SJT doesn't like this. */ 3085 /* #### SJT doesn't like this. */
3086 if (len == 0) 3086 if (len == 0)
3087 { 3087 {
3122 } 3122 }
3123 #endif 3123 #endif
3124 } 3124 }
3125 3125
3126 int 3126 int
3127 bufbyte_string_displayed_columns (const Bufbyte *str, Bytecount len) 3127 intbyte_string_displayed_columns (const Intbyte *str, Bytecount len)
3128 { 3128 {
3129 int cols = 0; 3129 int cols = 0;
3130 const Bufbyte *end = str + len; 3130 const Intbyte *end = str + len;
3131 3131
3132 while (str < end) 3132 while (str < end)
3133 { 3133 {
3134 #ifdef MULE 3134 #ifdef MULE
3135 Emchar ch = charptr_emchar (str); 3135 Emchar ch = charptr_emchar (str);
3160 } 3160 }
3161 3161
3162 /* NOTE: Does not reset the Dynarr. */ 3162 /* NOTE: Does not reset the Dynarr. */
3163 3163
3164 void 3164 void
3165 convert_bufbyte_string_into_emchar_dynarr (const Bufbyte *str, Bytecount len, 3165 convert_intbyte_string_into_emchar_dynarr (const Intbyte *str, Bytecount len,
3166 Emchar_dynarr *dyn) 3166 Emchar_dynarr *dyn)
3167 { 3167 {
3168 const Bufbyte *strend = str + len; 3168 const Intbyte *strend = str + len;
3169 3169
3170 while (str < strend) 3170 while (str < strend)
3171 { 3171 {
3172 Emchar ch = charptr_emchar (str); 3172 Emchar ch = charptr_emchar (str);
3173 Dynarr_add (dyn, ch); 3173 Dynarr_add (dyn, ch);
3174 INC_CHARPTR (str); 3174 INC_CHARPTR (str);
3175 } 3175 }
3176 } 3176 }
3177 3177
3178 Charcount 3178 Charcount
3179 convert_bufbyte_string_into_emchar_string (const Bufbyte *str, Bytecount len, 3179 convert_intbyte_string_into_emchar_string (const Intbyte *str, Bytecount len,
3180 Emchar *arr) 3180 Emchar *arr)
3181 { 3181 {
3182 const Bufbyte *strend = str + len; 3182 const Intbyte *strend = str + len;
3183 Charcount newlen = 0; 3183 Charcount newlen = 0;
3184 while (str < strend) 3184 while (str < strend)
3185 { 3185 {
3186 Emchar ch = charptr_emchar (str); 3186 Emchar ch = charptr_emchar (str);
3187 arr[newlen++] = ch; 3187 arr[newlen++] = ch;
3189 } 3189 }
3190 return newlen; 3190 return newlen;
3191 } 3191 }
3192 3192
3193 /* Convert an array of Emchars into the equivalent string representation. 3193 /* Convert an array of Emchars into the equivalent string representation.
3194 Store into the given Bufbyte dynarr. Does not reset the dynarr. 3194 Store into the given Intbyte dynarr. Does not reset the dynarr.
3195 Does not add a terminating zero. */ 3195 Does not add a terminating zero. */
3196 3196
3197 void 3197 void
3198 convert_emchar_string_into_bufbyte_dynarr (Emchar *arr, int nels, 3198 convert_emchar_string_into_intbyte_dynarr (Emchar *arr, int nels,
3199 Bufbyte_dynarr *dyn) 3199 Intbyte_dynarr *dyn)
3200 { 3200 {
3201 Bufbyte str[MAX_EMCHAR_LEN]; 3201 Intbyte str[MAX_EMCHAR_LEN];
3202 int i; 3202 int i;
3203 3203
3204 for (i = 0; i < nels; i++) 3204 for (i = 0; i < nels; i++)
3205 { 3205 {
3206 Bytecount len = set_charptr_emchar (str, arr[i]); 3206 Bytecount len = set_charptr_emchar (str, arr[i]);
3208 } 3208 }
3209 } 3209 }
3210 3210
3211 /* Convert an array of Emchars into the equivalent string representation. 3211 /* Convert an array of Emchars into the equivalent string representation.
3212 Malloc the space needed for this and return it. If LEN_OUT is not a 3212 Malloc the space needed for this and return it. If LEN_OUT is not a
3213 NULL pointer, store into LEN_OUT the number of Bufbytes in the 3213 NULL pointer, store into LEN_OUT the number of Intbytes in the
3214 malloc()ed string. Note that the actual number of Bufbytes allocated 3214 malloc()ed string. Note that the actual number of Intbytes allocated
3215 is one more than this: the returned string is zero-terminated. */ 3215 is one more than this: the returned string is zero-terminated. */
3216 3216
3217 Bufbyte * 3217 Intbyte *
3218 convert_emchar_string_into_malloced_string (Emchar *arr, int nels, 3218 convert_emchar_string_into_malloced_string (Emchar *arr, int nels,
3219 Bytecount *len_out) 3219 Bytecount *len_out)
3220 { 3220 {
3221 /* Damn zero-termination. */ 3221 /* Damn zero-termination. */
3222 Bufbyte *str = (Bufbyte *) alloca (nels * MAX_EMCHAR_LEN + 1); 3222 Intbyte *str = (Intbyte *) alloca (nels * MAX_EMCHAR_LEN + 1);
3223 Bufbyte *strorig = str; 3223 Intbyte *strorig = str;
3224 Bytecount len; 3224 Bytecount len;
3225 3225
3226 int i; 3226 int i;
3227 3227
3228 for (i = 0; i < nels; i++) 3228 for (i = 0; i < nels; i++)
3229 str += set_charptr_emchar (str, arr[i]); 3229 str += set_charptr_emchar (str, arr[i]);
3230 *str = '\0'; 3230 *str = '\0';
3231 len = str - strorig; 3231 len = str - strorig;
3232 str = (Bufbyte *) xmalloc (1 + len); 3232 str = (Intbyte *) xmalloc (1 + len);
3233 memcpy (str, strorig, 1 + len); 3233 memcpy (str, strorig, 1 + len);
3234 if (len_out) 3234 if (len_out)
3235 *len_out = len; 3235 *len_out = len;
3236 return str; 3236 return str;
3237 } 3237 }
3247 int i; 3247 int i;
3248 3248
3249 inside_change_hook = 0; 3249 inside_change_hook = 0;
3250 in_first_change = 0; 3250 in_first_change = 0;
3251 3251
3252 for (i = 0; i <= MAX_BYTIND_GAP_SIZE_3; i++) 3252 for (i = 0; i <= MAX_BYTEBPOS_GAP_SIZE_3; i++)
3253 three_to_one_table[i] = i / 3; 3253 three_to_one_table[i] = i / 3;
3254 } 3254 }
3255 3255
3256 void 3256 void
3257 vars_of_insdel (void) 3257 vars_of_insdel (void)
3283 b->text->mule_shifter = 0; 3283 b->text->mule_shifter = 0;
3284 b->text->mule_three_p = 0; 3284 b->text->mule_three_p = 0;
3285 3285
3286 for (i = 0; i < 16; i++) 3286 for (i = 0; i < 16; i++)
3287 { 3287 {
3288 b->text->mule_bufpos_cache[i] = 1; 3288 b->text->mule_charbpos_cache[i] = 1;
3289 b->text->mule_bytind_cache[i] = 1; 3289 b->text->mule_bytebpos_cache[i] = 1;
3290 } 3290 }
3291 } 3291 }
3292 #endif /* MULE */ 3292 #endif /* MULE */
3293 b->text->line_number_cache = Qnil; 3293 b->text->line_number_cache = Qnil;
3294 3294