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