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