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