428
|
1 /* Header file for the buffer manipulation primitives.
|
|
2 Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995
|
|
3 Free Software Foundation, Inc.
|
|
4 Copyright (C) 1995 Sun Microsystems, Inc.
|
771
|
5 Copyright (C) 2001, 2002 Ben Wing.
|
428
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
9 XEmacs is free software; you can redistribute it and/or modify it
|
|
10 under the terms of the GNU General Public License as published by the
|
|
11 Free Software Foundation; either version 2, or (at your option) any
|
|
12 later version.
|
|
13
|
|
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
17 for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with XEmacs; see the file COPYING. If not, write to
|
|
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 Boston, MA 02111-1307, USA. */
|
|
23
|
|
24 /* Synched up with: FSF 19.30. */
|
|
25
|
|
26 /* Authorship:
|
|
27
|
|
28 FSF: long ago.
|
|
29 JWZ: separated out bufslots.h, early in Lemacs.
|
|
30 Ben Wing: almost completely rewritten for Mule, 19.12.
|
|
31 */
|
|
32
|
440
|
33 #ifndef INCLUDED_buffer_h_
|
|
34 #define INCLUDED_buffer_h_
|
428
|
35
|
446
|
36 #include "casetab.h"
|
|
37 #include "chartab.h"
|
|
38
|
428
|
39 /************************************************************************/
|
|
40 /* */
|
|
41 /* definition of Lisp buffer object */
|
|
42 /* */
|
|
43 /************************************************************************/
|
|
44
|
665
|
45 /* Note: we keep both Bytebpos and Charbpos versions of some of the
|
428
|
46 important buffer positions because they are accessed so much.
|
|
47 If we didn't do this, we would constantly be invalidating the
|
665
|
48 charbpos<->bytebpos cache under Mule.
|
428
|
49
|
|
50 Note that under non-Mule, both versions will always be the
|
|
51 same so we don't really need to keep track of them. But it
|
|
52 simplifies the logic to go ahead and do so all the time and
|
|
53 the memory loss is insignificant. */
|
|
54
|
|
55 /* Formerly, it didn't much matter what went inside the struct buffer_text
|
|
56 and what went outside it. Now it does, with the advent of "indirect
|
|
57 buffers" that share text with another buffer. An indirect buffer
|
|
58 shares the same *text* as another buffer, but has its own buffer-local
|
|
59 variables, its own accessible region, and its own markers and extents.
|
|
60 (Due to the nature of markers, it doesn't actually matter much whether
|
|
61 we stick them inside or out of the struct buffer_text -- the user won't
|
|
62 notice any difference -- but we go ahead and put them outside for
|
|
63 consistency and overall saneness of algorithm.)
|
|
64
|
|
65 FSFmacs gets away with not maintaining any "children" pointers from
|
|
66 a buffer to the indirect buffers that refer to it by putting the
|
|
67 markers inside of the struct buffer_text, using markers to keep track
|
|
68 of BEGV and ZV in indirect buffers, and relying on the fact that
|
|
69 all intervals (text properties and overlays) use markers for their
|
|
70 start and end points. We don't do this for extents (markers are
|
|
71 inefficient anyway and take up space), so we have to maintain
|
|
72 children pointers. This is not terribly hard, though, and the
|
|
73 code to maintain this is just like the code already present in
|
|
74 extent-parent and extent-children.
|
|
75 */
|
|
76
|
|
77 struct buffer_text
|
|
78 {
|
867
|
79 Ibyte *beg; /* Actual address of buffer contents. */
|
665
|
80 Bytebpos gpt; /* Index of gap in buffer. */
|
|
81 Bytebpos z; /* Index of end of buffer. */
|
|
82 Charbpos bufz; /* Equivalent as a Charbpos. */
|
|
83 Bytecount gap_size;/* Size of buffer's gap */
|
|
84 Bytecount end_gap_size;/* Size of buffer's end gap */
|
428
|
85 long modiff; /* This counts buffer-modification events
|
|
86 for this buffer. It is incremented for
|
|
87 each such event, and never otherwise
|
|
88 changed. */
|
|
89 long save_modiff; /* Previous value of modiff, as of last
|
|
90 time buffer visited or saved a file. */
|
|
91
|
|
92 #ifdef MULE
|
771
|
93 /* We keep track of a "known" region for very fast access. This
|
|
94 information is text-only so it goes here. We update this at each
|
|
95 change to the buffer, so if it's entirely ASCII, these will always
|
|
96 contain the minimum and maximum positions of the buffer. */
|
665
|
97 Charbpos mule_bufmin, mule_bufmax;
|
|
98 Bytebpos mule_bytmin, mule_bytmax;
|
428
|
99 int mule_shifter, mule_three_p;
|
|
100
|
|
101 /* And we also cache 16 positions for fairly fast access near those
|
|
102 positions. */
|
665
|
103 Charbpos mule_charbpos_cache[16];
|
|
104 Bytebpos mule_bytebpos_cache[16];
|
771
|
105
|
826
|
106 /* True if all chars fit into one byte;
|
|
107 == (format == FORMAT_8_BIT_FIXED ||
|
|
108 (format == FORMAT_DEFAULT && num_ascii_chars == bufz - 1))
|
|
109 kept around to speed up (slightly?) the byte-char conversion routines. */
|
|
110 int entirely_one_byte_p;
|
|
111 /* Number of ASCII chars in buffer (0 - 127) */
|
|
112 Charcount num_ascii_chars;
|
|
113 /* Number of chars in buffer that would fit in an 8-bit-fixed buffer. */
|
|
114 Charcount num_8_bit_fixed_chars;
|
|
115 /* Number of chars in buffer that would fit in an 16-bit-fixed buffer. */
|
|
116 Charcount num_16_bit_fixed_chars;
|
|
117
|
|
118 /* Currently we only handle 8 bit fixed and default */
|
|
119 Internal_Format format;
|
428
|
120 #endif
|
|
121
|
|
122 /* Similar to the above, we keep track of positions for which line
|
|
123 number has last been calculated. See line-number.c. */
|
|
124 Lisp_Object line_number_cache;
|
|
125
|
|
126 /* Change data that goes with the text. */
|
|
127 struct buffer_text_change_data *changes;
|
|
128 };
|
|
129
|
|
130 struct buffer
|
|
131 {
|
|
132 struct lcrecord_header header;
|
|
133
|
|
134 /* This structure holds the coordinates of the buffer contents
|
|
135 in ordinary buffers. In indirect buffers, this is not used. */
|
|
136 struct buffer_text own_text;
|
|
137
|
|
138 /* This points to the `struct buffer_text' that is used for this buffer.
|
|
139 In an ordinary buffer, this is the own_text field above.
|
|
140 In an indirect buffer, this is the own_text field of another buffer. */
|
|
141 struct buffer_text *text;
|
|
142
|
665
|
143 Bytebpos pt; /* Position of point in buffer. */
|
|
144 Charbpos bufpt; /* Equivalent as a Charbpos. */
|
|
145 Bytebpos begv; /* Index of beginning of accessible range. */
|
|
146 Charbpos bufbegv; /* Equivalent as a Charbpos. */
|
|
147 Bytebpos zv; /* Index of end of accessible range. */
|
|
148 Charbpos bufzv; /* Equivalent as a Charbpos. */
|
428
|
149
|
|
150 int face_change; /* This is set when a change in how the text should
|
|
151 be displayed (e.g., font, color) is made. */
|
|
152
|
448
|
153 /* Whether buffer specific face is specified. */
|
|
154 int buffer_local_face_property;
|
|
155
|
428
|
156 /* change data indicating what portion of the text has changed
|
|
157 since the last time this was reset. Used by redisplay.
|
|
158 Logically we should keep this with the text structure, but
|
|
159 redisplay resets it for each buffer individually and we don't
|
|
160 want interference between an indirect buffer and its base
|
|
161 buffer. */
|
|
162 struct each_buffer_change_data *changes;
|
|
163
|
|
164 #ifdef REGION_CACHE_NEEDS_WORK
|
|
165 /* If the long line scan cache is enabled (i.e. the buffer-local
|
|
166 variable cache-long-line-scans is non-nil), newline_cache
|
|
167 points to the newline cache, and width_run_cache points to the
|
|
168 width run cache.
|
|
169
|
|
170 The newline cache records which stretches of the buffer are
|
|
171 known *not* to contain newlines, so that they can be skipped
|
|
172 quickly when we search for newlines.
|
|
173
|
|
174 The width run cache records which stretches of the buffer are
|
|
175 known to contain characters whose widths are all the same. If
|
|
176 the width run cache maps a character to a value > 0, that value
|
|
177 is the character's width; if it maps a character to zero, we
|
|
178 don't know what its width is. This allows compute_motion to
|
|
179 process such regions very quickly, using algebra instead of
|
|
180 inspecting each character. See also width_table, below. */
|
|
181 struct region_cache *newline_cache;
|
|
182 struct region_cache *width_run_cache;
|
|
183 #endif /* REGION_CACHE_NEEDS_WORK */
|
|
184
|
|
185 /* The markers that refer to this buffer. This is actually a single
|
|
186 marker -- successive elements in its marker `chain' are the other
|
|
187 markers referring to this buffer */
|
440
|
188 Lisp_Marker *markers;
|
428
|
189
|
|
190 /* The buffer's extent info. This is its own type, an extent-info
|
|
191 object (done this way for ease in marking / finalizing). */
|
|
192 Lisp_Object extent_info;
|
|
193
|
826
|
194 /* The buffer's syntax cache. This caches a known region where the
|
|
195 `syntax-table' property is unchanged, for quick lookup in the routines
|
|
196 that scan a buffer looking for a particular syntax (regex routines,
|
|
197 parse-partial-sexp, etc.). */
|
|
198 struct syntax_cache *syntax_cache;
|
|
199
|
428
|
200 /* ----------------------------------------------------------------- */
|
|
201 /* All the stuff above this line is the responsibility of insdel.c,
|
826
|
202 with some help from marker.c, extents.c and syntax.c.
|
428
|
203 All the stuff below this line is the responsibility of buffer.c. */
|
|
204
|
|
205 /* In an indirect buffer, this points to the base buffer.
|
|
206 In an ordinary buffer, it is 0.
|
|
207 We DO mark through this slot. */
|
|
208 struct buffer *base_buffer;
|
|
209
|
|
210 /* List of indirect buffers whose base is this buffer.
|
|
211 If we are an indirect buffer, this will be nil.
|
|
212 Do NOT mark through this. */
|
|
213 Lisp_Object indirect_children;
|
|
214
|
|
215 /* Flags saying which DEFVAR_PER_BUFFER variables
|
|
216 are local to this buffer. */
|
|
217 int local_var_flags;
|
|
218
|
|
219 /* Set to the modtime of the visited file when read or written.
|
|
220 -1 means visited file was nonexistent.
|
|
221 0 means visited file modtime unknown; in no case complain
|
|
222 about any mismatch on next save attempt. */
|
|
223 int modtime;
|
|
224
|
|
225 /* the value of text->modiff at the last auto-save. */
|
442
|
226 long auto_save_modified;
|
428
|
227
|
|
228 /* The time at which we detected a failure to auto-save,
|
|
229 Or -1 if we didn't have a failure. */
|
|
230 int auto_save_failure_time;
|
|
231
|
|
232 /* Position in buffer at which display started
|
|
233 the last time this buffer was displayed. */
|
|
234 int last_window_start;
|
|
235
|
|
236 /* Everything from here down must be a Lisp_Object */
|
|
237
|
|
238 #define MARKED_SLOT(x) Lisp_Object x
|
|
239 #include "bufslots.h"
|
|
240 #undef MARKED_SLOT
|
|
241 };
|
|
242
|
|
243 DECLARE_LRECORD (buffer, struct buffer);
|
|
244 #define XBUFFER(x) XRECORD (x, buffer, struct buffer)
|
617
|
245 #define wrap_buffer(p) wrap_record (p, buffer)
|
428
|
246 #define BUFFERP(x) RECORDP (x, buffer)
|
|
247 #define CHECK_BUFFER(x) CHECK_RECORD (x, buffer)
|
|
248 #define CONCHECK_BUFFER(x) CONCHECK_RECORD (x, buffer)
|
|
249
|
|
250 #define BUFFER_LIVE_P(b) (!NILP ((b)->name))
|
|
251
|
|
252 #define CHECK_LIVE_BUFFER(x) do { \
|
|
253 CHECK_BUFFER (x); \
|
|
254 if (!BUFFER_LIVE_P (XBUFFER (x))) \
|
|
255 dead_wrong_type_argument (Qbuffer_live_p, (x)); \
|
|
256 } while (0)
|
|
257
|
|
258 #define CONCHECK_LIVE_BUFFER(x) do { \
|
|
259 CONCHECK_BUFFER (x); \
|
|
260 if (!BUFFER_LIVE_P (XBUFFER (x))) \
|
|
261 x = wrong_type_argument (Qbuffer_live_p, (x)); \
|
|
262 } while (0)
|
|
263
|
|
264
|
|
265 #define BUFFER_BASE_BUFFER(b) ((b)->base_buffer ? (b)->base_buffer : (b))
|
|
266
|
|
267 /* Map over buffers sharing the same text as MPS_BUF. MPS_BUFVAR is a
|
|
268 variable that gets the buffer values (beginning with the base
|
|
269 buffer, then the children), and MPS_BUFCONS should be a temporary
|
|
270 Lisp_Object variable. */
|
647
|
271 #define MAP_INDIRECT_BUFFERS(mps_buf, mps_bufvar, mps_bufcons) \
|
|
272 for (mps_bufcons = Qunbound, \
|
|
273 mps_bufvar = BUFFER_BASE_BUFFER (mps_buf); \
|
|
274 UNBOUNDP (mps_bufcons) ? \
|
|
275 (mps_bufcons = mps_bufvar->indirect_children, \
|
|
276 1) \
|
|
277 : (!NILP (mps_bufcons) \
|
|
278 && (mps_bufvar = XBUFFER (XCAR (mps_bufcons)), 1) \
|
|
279 && (mps_bufcons = XCDR (mps_bufcons), 1)); \
|
428
|
280 )
|
|
281
|
|
282
|
826
|
283 /* All macros below follow the three golden rules of macros (see text.h),
|
|
284 with the following exception:
|
|
285
|
|
286 -- 'struct buffer *' arguments can be evaluated more than once.
|
|
287 */
|
|
288
|
428
|
289 /*----------------------------------------------------------------------*/
|
|
290 /* Accessor macros for important positions in a buffer */
|
|
291 /*----------------------------------------------------------------------*/
|
|
292
|
|
293 /* We put them here because some stuff below wants them before the
|
|
294 place where we would normally put them. */
|
|
295
|
|
296 /* None of these are lvalues. Use the settor macros below to change
|
|
297 the positions. */
|
|
298
|
|
299 /* Beginning of buffer. */
|
826
|
300 #define BYTE_BUF_BEG(buf) ((Bytebpos) 1)
|
665
|
301 #define BUF_BEG(buf) ((Charbpos) 1)
|
428
|
302
|
|
303 /* Beginning of accessible range of buffer. */
|
826
|
304 #define BYTE_BUF_BEGV(buf) ((buf)->begv + 0)
|
428
|
305 #define BUF_BEGV(buf) ((buf)->bufbegv + 0)
|
|
306
|
|
307 /* End of accessible range of buffer. */
|
826
|
308 #define BYTE_BUF_ZV(buf) ((buf)->zv + 0)
|
428
|
309 #define BUF_ZV(buf) ((buf)->bufzv + 0)
|
|
310
|
|
311 /* End of buffer. */
|
826
|
312 #define BYTE_BUF_Z(buf) ((buf)->text->z + 0)
|
428
|
313 #define BUF_Z(buf) ((buf)->text->bufz + 0)
|
|
314
|
|
315 /* Point. */
|
826
|
316 #define BYTE_BUF_PT(buf) ((buf)->pt + 0)
|
428
|
317 #define BUF_PT(buf) ((buf)->bufpt + 0)
|
|
318
|
826
|
319 /* Internal format of buffer. */
|
|
320 #ifdef MULE
|
|
321 #define BUF_FORMAT(buf) ((buf)->text->format)
|
|
322 #else
|
|
323 #define BUF_FORMAT(buf) FORMAT_DEFAULT
|
|
324 #endif
|
|
325
|
428
|
326 /*----------------------------------------------------------------------*/
|
826
|
327 /* Validating byte positions */
|
428
|
328 /*----------------------------------------------------------------------*/
|
|
329
|
826
|
330 /* Address of byte at position POS in buffer, no error checking. */
|
|
331 DECLARE_INLINE_HEADER (
|
867
|
332 Ibyte *
|
826
|
333 BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (struct buffer *buf, Bytebpos pos)
|
|
334 )
|
428
|
335 {
|
|
336 return (buf->text->beg +
|
|
337 ((pos >= buf->text->gpt ? (pos + buf->text->gap_size) : pos)
|
|
338 - 1));
|
|
339 }
|
|
340
|
826
|
341 /* Given a byte position, does it point to the beginning of a character?
|
|
342 */
|
|
343 #ifdef MULE
|
|
344 DECLARE_INLINE_HEADER (
|
|
345 int
|
|
346 VALID_BYTEBPOS_P (struct buffer *buf, Bytebpos x)
|
|
347 )
|
428
|
348 {
|
826
|
349 switch (BUF_FORMAT (buf))
|
|
350 {
|
|
351 case FORMAT_DEFAULT:
|
867
|
352 return ibyte_first_byte_p (*BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, x));
|
826
|
353 case FORMAT_16_BIT_FIXED:
|
|
354 return ((x - 1) & 1) == 0;
|
|
355 case FORMAT_32_BIT_FIXED:
|
|
356 return ((x - 1) & 3) == 0;
|
|
357 default:
|
|
358 return 1;
|
|
359 }
|
428
|
360 }
|
|
361 #else
|
665
|
362 # define VALID_BYTEBPOS_P(buf, x) 1
|
428
|
363 #endif
|
|
364
|
826
|
365 /* If error-checking is enabled, assert that the given char position is
|
|
366 within range. Otherwise, do nothing.
|
|
367 */
|
|
368 # define ASSERT_VALID_CHARBPOS_UNSAFE(buf, x) do { \
|
|
369 text_checking_assert (BUFFER_LIVE_P (buf)); \
|
|
370 text_checking_assert ((x) >= BUF_BEG (buf) && x <= BUF_Z (buf)); \
|
428
|
371 } while (0)
|
826
|
372
|
|
373 /* If error-checking is enabled, assert that the given byte position is
|
|
374 within range and points to the beginning of a character or to the end of
|
|
375 the buffer. Otherwise, do nothing.
|
|
376 */
|
|
377 # define ASSERT_VALID_BYTEBPOS_UNSAFE(buf, x) do { \
|
|
378 text_checking_assert (BUFFER_LIVE_P (buf)); \
|
|
379 text_checking_assert ((x) >= BYTE_BUF_BEG (buf) && x <= BYTE_BUF_Z (buf)); \
|
|
380 text_checking_assert (VALID_BYTEBPOS_P (buf, x)); \
|
428
|
381 } while (0)
|
826
|
382
|
|
383 /* If error-checking is enabled, assert that the given byte position is
|
|
384 within range and satisfies ASSERT_VALID_BYTEBPOS() and also does not
|
|
385 refer to the beginning of the buffer. (i.e. movement backwards is OK.)
|
|
386 Otherwise, do nothing.
|
|
387 */
|
|
388 # define ASSERT_VALID_BYTEBPOS_BACKWARD_UNSAFE(buf, x) do { \
|
|
389 text_checking_assert (BUFFER_LIVE_P (buf)); \
|
|
390 text_checking_assert ((x) > BYTE_BUF_BEG (buf) && x <= BYTE_BUF_Z (buf)); \
|
|
391 text_checking_assert (VALID_BYTEBPOS_P (buf, x)); \
|
428
|
392 } while (0)
|
|
393
|
826
|
394 /* If error-checking is enabled, assert that the given byte position is
|
|
395 within range and satisfies ASSERT_VALID_BYTEBPOS() and also does not
|
|
396 refer to the end of the buffer. (i.e. movement forwards is OK.)
|
|
397 Otherwise, do nothing.
|
|
398 */
|
|
399 # define ASSERT_VALID_BYTEBPOS_FORWARD_UNSAFE(buf, x) do { \
|
|
400 text_checking_assert (BUFFER_LIVE_P (buf)); \
|
|
401 text_checking_assert ((x) >= BYTE_BUF_BEG (buf) && x < BYTE_BUF_Z (buf)); \
|
|
402 text_checking_assert (VALID_BYTEBPOS_P (buf, x)); \
|
|
403 } while (0)
|
428
|
404
|
|
405 #ifdef MULE
|
826
|
406 /* Make sure that the given byte position is pointing to the beginning of a
|
|
407 character. If not, back up until this is the case. Note that there are
|
|
408 not too many places where it is legitimate to do this sort of thing.
|
|
409 It's an error if you're passed an "invalid" byte position.
|
|
410 */
|
|
411 # define VALIDATE_BYTEBPOS_BACKWARD(buf, x) do { \
|
|
412 switch (BUF_FORMAT (buf)) \
|
|
413 { \
|
|
414 case FORMAT_DEFAULT: \
|
|
415 { \
|
867
|
416 Ibyte *VBB_ptr = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, x); \
|
|
417 while (!ibyte_first_byte_p (*VBB_ptr)) \
|
826
|
418 VBB_ptr--, (x)--; \
|
|
419 } \
|
|
420 break; \
|
|
421 case FORMAT_16_BIT_FIXED: \
|
|
422 if (((x - 1) & 1) != 0) \
|
|
423 x--; \
|
|
424 break; \
|
|
425 case FORMAT_32_BIT_FIXED: \
|
|
426 while (((x - 1) & 3) != 0) \
|
|
427 x--; \
|
|
428 break; \
|
|
429 default: \
|
|
430 break; \
|
|
431 } \
|
428
|
432 } while (0)
|
|
433 #else
|
665
|
434 # define VALIDATE_BYTEBPOS_BACKWARD(buf, x)
|
428
|
435 #endif
|
|
436
|
|
437 #ifdef MULE
|
826
|
438 /* Make sure that the given byte position is pointing to the beginning of a
|
|
439 character. If not, move forward until this is the case. Note that
|
|
440 there are not too many places where it is legitimate to do this sort of
|
|
441 thing. It's an error if you're passed an "invalid" byte position.
|
|
442 */
|
|
443 # define VALIDATE_BYTEBPOS_FORWARD(buf, x) do { \
|
|
444 switch (BUF_FORMAT (buf)) \
|
|
445 { \
|
|
446 case FORMAT_DEFAULT: \
|
|
447 { \
|
867
|
448 Ibyte *VBF_ptr = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, x); \
|
|
449 while (!ibyte_first_byte_p (*VBF_ptr)) \
|
826
|
450 VBF_ptr++, (x)++; \
|
|
451 } \
|
|
452 break; \
|
|
453 case FORMAT_16_BIT_FIXED: \
|
|
454 if (((x - 1) & 1) != 0) \
|
|
455 x++; \
|
|
456 break; \
|
|
457 case FORMAT_32_BIT_FIXED: \
|
|
458 while (((x - 1) & 3) != 0) \
|
|
459 x++; \
|
|
460 break; \
|
|
461 default: \
|
|
462 break; \
|
|
463 } \
|
428
|
464 } while (0)
|
|
465 #else
|
665
|
466 # define VALIDATE_BYTEBPOS_FORWARD(buf, x)
|
428
|
467 #endif
|
|
468
|
826
|
469 /*----------------------------------------------------------------------*/
|
|
470 /* Working with byte positions */
|
|
471 /*----------------------------------------------------------------------*/
|
|
472
|
|
473
|
|
474 /* Given a byte position (assumed to point at the beginning of a
|
|
475 character), modify that value so it points to the beginning of the next
|
|
476 character.
|
|
477
|
|
478 Note that in the simplest case (no MULE, no ERROR_CHECK_TEXT),
|
|
479 this crap reduces down to simply (x)++. */
|
428
|
480
|
665
|
481 #define INC_BYTEBPOS(buf, x) do \
|
428
|
482 { \
|
826
|
483 ASSERT_VALID_BYTEBPOS_FORWARD_UNSAFE (buf, x); \
|
428
|
484 /* Note that we do the increment first to \
|
|
485 make sure that the pointer in \
|
665
|
486 VALIDATE_BYTEBPOS_FORWARD() ends up on \
|
428
|
487 the correct side of the gap */ \
|
|
488 (x)++; \
|
665
|
489 VALIDATE_BYTEBPOS_FORWARD (buf, x); \
|
428
|
490 } while (0)
|
|
491
|
826
|
492 /* Given a byte position (assumed to point at the beginning of a
|
|
493 character), modify that value so it points to the beginning of the
|
867
|
494 previous character. Unlike for DEC_IBYTEPTR(), we can do all the
|
826
|
495 assert()s because there are sentinels at the beginning of the gap and
|
|
496 the end of the buffer.
|
|
497
|
|
498 Note that in the simplest case (no MULE, no ERROR_CHECK_TEXT), this
|
|
499 crap reduces down to simply (x)--. */
|
428
|
500
|
665
|
501 #define DEC_BYTEBPOS(buf, x) do \
|
428
|
502 { \
|
771
|
503 ASSERT_VALID_BYTEBPOS_BACKWARD_UNSAFE (buf, x); \
|
428
|
504 /* Note that we do the decrement first to \
|
|
505 make sure that the pointer in \
|
665
|
506 VALIDATE_BYTEBPOS_BACKWARD() ends up on \
|
428
|
507 the correct side of the gap */ \
|
|
508 (x)--; \
|
665
|
509 VALIDATE_BYTEBPOS_BACKWARD (buf, x); \
|
428
|
510 } while (0)
|
|
511
|
826
|
512 DECLARE_INLINE_HEADER (
|
|
513 Bytebpos
|
665
|
514 prev_bytebpos (struct buffer *buf, Bytebpos x)
|
826
|
515 )
|
428
|
516 {
|
665
|
517 DEC_BYTEBPOS (buf, x);
|
428
|
518 return x;
|
|
519 }
|
|
520
|
826
|
521 DECLARE_INLINE_HEADER (
|
|
522 Bytebpos
|
665
|
523 next_bytebpos (struct buffer *buf, Bytebpos x)
|
826
|
524 )
|
428
|
525 {
|
665
|
526 INC_BYTEBPOS (buf, x);
|
428
|
527 return x;
|
|
528 }
|
|
529
|
826
|
530 /* A constant representing an invalid Bytebpos. Valid Bytebposes
|
|
531 can never have this value. */
|
|
532
|
665
|
533 #define BYTEBPOS_INVALID ((Bytebpos) -1)
|
428
|
534
|
|
535 /*----------------------------------------------------------------------*/
|
826
|
536 /* Converting between byte and character positions */
|
428
|
537 /*----------------------------------------------------------------------*/
|
|
538
|
|
539 #ifdef MULE
|
|
540
|
|
541 /* The basic algorithm we use is to keep track of a known region of
|
771
|
542 characters in each buffer, all of which are of the same width. We keep
|
|
543 track of the boundaries of the region in both Charbpos and Bytebpos
|
|
544 coordinates and also keep track of the char width, which is 1 - 4 bytes.
|
|
545 If the position we're translating is not in the known region, then we
|
|
546 invoke a function to update the known region to surround the position in
|
|
547 question. This assumes locality of reference, which is usually the
|
|
548 case.
|
|
549
|
|
550 Note that the function to update the known region can be simple or
|
|
551 complicated depending on how much information we cache. In addition to
|
|
552 the known region, we always cache the correct conversions for point,
|
|
553 BEGV, and ZV, and in addition to this we cache 16 positions where the
|
|
554 conversion is known. We only look in the cache or update it when we
|
|
555 need to move the known region more than a certain amount (currently 50
|
|
556 chars), and then we throw away a "random" value and replace it with the
|
|
557 newly calculated value.
|
|
558
|
|
559 Finally, we maintain an extra flag that tracks whether the buffer is
|
|
560 entirely ASCII, to speed up the conversions even more. This flag is
|
|
561 actually of dubious value because in an entirely-ASCII buffer the known
|
|
562 region will always span the entire buffer (in fact, we update the flag
|
|
563 based on this fact), and so all we're saving is a few machine cycles.
|
428
|
564
|
771
|
565 A potentially smarter method than what we do with known regions and
|
|
566 cached positions would be to keep some sort of pseudo-extent layer over
|
|
567 the buffer; maybe keep track of the charbpos/bytebpos correspondence at the
|
|
568 beginning of each line, which would allow us to do a binary search over
|
|
569 the pseudo-extents to narrow things down to the correct line, at which
|
|
570 point you could use a linear movement method. This would also mesh well
|
|
571 with efficiently implementing a line-numbering scheme. However, you
|
|
572 have to weigh the amount of time spent updating the cache vs. the
|
|
573 savings that result from it. In reality, we modify the buffer far less
|
|
574 often than we access it, so a cache of this sort that provides
|
|
575 guaranteed LOG (N) performance (or perhaps N * LOG (N), if we set a
|
|
576 maximum on the cache size) would indeed be a win, particularly in very
|
|
577 large buffers. If we ever implement this, we should probably set a
|
|
578 reasonably high minimum below which we use the old method, because the
|
|
579 time spent updating the fancy cache would likely become dominant when
|
|
580 making buffer modifications in smaller buffers.
|
428
|
581
|
771
|
582 Note also that we have to multiply or divide by the char width in order
|
|
583 to convert the positions. We do some tricks to avoid ever actually
|
|
584 having to do a multiply or divide, because that is typically an
|
|
585 expensive operation (esp. divide). Multiplying or dividing by 1, 2, or
|
|
586 4 can be implemented simply as a shift left or shift right, and we keep
|
|
587 track of a shifter value (0, 1, or 2) indicating how much to shift.
|
|
588 Multiplying by 3 can be implemented by doubling and then adding the
|
|
589 original value. Dividing by 3, alas, cannot be implemented in any
|
|
590 simple shift/subtract method, as far as I know; so we just do a table
|
|
591 lookup. For simplicity, we use a table of size 128K, which indexes the
|
|
592 "divide-by-3" values for the first 64K non-negative numbers. (Note that
|
|
593 we can increase the size up to 384K, i.e. indexing the first 192K
|
|
594 non-negative numbers, while still using shorts in the array.) This also
|
|
595 means that the size of the known region can be at most 64K for
|
|
596 width-three characters.
|
|
597
|
|
598 !!#### We should investigate the algorithm in GNU Emacs. I think it
|
|
599 does something similar, but it may differ in some details, and it's
|
|
600 worth seeing if anything can be gleaned.
|
428
|
601 */
|
|
602
|
826
|
603 Bytebpos charbpos_to_bytebpos_func (struct buffer *buf, Charbpos x);
|
|
604 Charbpos bytebpos_to_charbpos_func (struct buffer *buf, Bytebpos x);
|
428
|
605 extern short three_to_one_table[];
|
|
606
|
826
|
607 #endif /* MULE */
|
|
608
|
|
609 /* Given a Charbpos, return the equivalent Bytebpos. */
|
|
610
|
|
611 DECLARE_INLINE_HEADER (
|
|
612 Bytebpos
|
|
613 charbpos_to_bytebpos (struct buffer *buf, Charbpos x)
|
|
614 )
|
428
|
615 {
|
826
|
616 Bytebpos retval;
|
|
617 ASSERT_VALID_CHARBPOS_UNSAFE (buf, x);
|
|
618 #ifdef MULE
|
|
619 if (buf->text->entirely_one_byte_p)
|
|
620 retval = (Bytebpos) x;
|
|
621 else if (BUF_FORMAT (buf) == FORMAT_16_BIT_FIXED)
|
|
622 retval = (Bytebpos) (x << 1);
|
|
623 else if (BUF_FORMAT (buf) == FORMAT_32_BIT_FIXED)
|
|
624 retval = (Bytebpos) (x << 2);
|
|
625 else if (x >= buf->text->mule_bufmin && x <= buf->text->mule_bufmax)
|
|
626 retval = (buf->text->mule_bytmin +
|
428
|
627 ((x - buf->text->mule_bufmin) << buf->text->mule_shifter) +
|
814
|
628 (buf->text->mule_three_p ? (x - buf->text->mule_bufmin) :
|
|
629 (Bytebpos) 0));
|
428
|
630 else
|
826
|
631 retval = charbpos_to_bytebpos_func (buf, x);
|
|
632 #else
|
|
633 retval = (Bytebpos) x;
|
|
634 #endif
|
|
635 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, retval);
|
|
636 return retval;
|
|
637 }
|
|
638
|
|
639 /* Given a Bytebpos, return the equivalent Charbpos. */
|
|
640
|
|
641 DECLARE_INLINE_HEADER (
|
|
642 Charbpos
|
|
643 bytebpos_to_charbpos (struct buffer *buf, Bytebpos x)
|
|
644 )
|
|
645 {
|
|
646 Charbpos retval;
|
|
647 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, x);
|
|
648 #ifdef MULE
|
|
649 if (buf->text->entirely_one_byte_p)
|
|
650 retval = (Charbpos) x;
|
|
651 else if (BUF_FORMAT (buf) == FORMAT_16_BIT_FIXED)
|
|
652 retval = (Charbpos) (x >> 1);
|
|
653 else if (BUF_FORMAT (buf) == FORMAT_32_BIT_FIXED)
|
|
654 retval = (Charbpos) (x >> 2);
|
|
655 else if (x >= buf->text->mule_bytmin && x <= buf->text->mule_bytmax)
|
|
656 retval = (buf->text->mule_bufmin +
|
|
657 ((buf->text->mule_three_p
|
|
658 ? three_to_one_table[x - buf->text->mule_bytmin]
|
|
659 : (x - buf->text->mule_bytmin) >> buf->text->mule_shifter)));
|
|
660 else
|
|
661 retval = bytebpos_to_charbpos_func (buf, x);
|
|
662 #else
|
|
663 retval = (Charbpos) x;
|
|
664 #endif
|
|
665 ASSERT_VALID_CHARBPOS_UNSAFE (buf, retval);
|
|
666 return retval;
|
|
667 }
|
|
668
|
|
669 /* Given a Bytebpos, return the equivalent Charbpos as a Lisp Object. */
|
|
670
|
|
671 #define make_charbpos(buf, ind) make_int (bytebpos_to_charbpos (buf, ind))
|
|
672
|
|
673 /*----------------------------------------------------------------------*/
|
|
674 /* Converting between byte and memory positions */
|
|
675 /*----------------------------------------------------------------------*/
|
|
676
|
|
677 DECLARE_INLINE_HEADER (
|
|
678 int
|
|
679 valid_membpos_p (struct buffer *buf, Membpos x)
|
|
680 )
|
|
681 {
|
|
682 return ((x >= 1 && x <= (Membpos) buf->text->gpt) ||
|
|
683 (x > (Membpos) (buf->text->gpt + buf->text->gap_size) &&
|
|
684 x <= (Membpos) (buf->text->z + buf->text->gap_size)));
|
428
|
685 }
|
|
686
|
826
|
687 DECLARE_INLINE_HEADER (
|
|
688 Membpos
|
|
689 bytebpos_to_membpos (struct buffer *buf, Bytebpos x)
|
|
690 )
|
|
691 {
|
|
692 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, x);
|
|
693 return (Membpos) ((x > buf->text->gpt) ? (x + buf->text->gap_size) : x);
|
|
694 }
|
|
695
|
|
696
|
|
697 DECLARE_INLINE_HEADER (
|
|
698 Bytebpos
|
|
699 membpos_to_bytebpos (struct buffer *buf, Membpos x)
|
|
700 )
|
|
701 {
|
|
702 Bytebpos retval;
|
|
703 text_checking_assert (valid_membpos_p (buf, x));
|
|
704 retval = (Bytebpos) ((x > (Membpos) buf->text->gpt) ?
|
|
705 x - buf->text->gap_size :
|
|
706 x);
|
|
707 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, retval);
|
|
708 return retval;
|
|
709 }
|
|
710
|
|
711 DECLARE_INLINE_HEADER (
|
|
712 Charbpos
|
|
713 membpos_to_charbpos (struct buffer *buf, Membpos x)
|
|
714 )
|
428
|
715 {
|
826
|
716 return bytebpos_to_charbpos (buf, membpos_to_bytebpos (buf, x));
|
|
717 }
|
|
718
|
|
719 DECLARE_INLINE_HEADER (
|
|
720 Membpos
|
|
721 charbpos_to_membpos (struct buffer *buf, Charbpos x)
|
|
722 )
|
|
723 {
|
|
724 return bytebpos_to_membpos (buf, charbpos_to_bytebpos (buf, x));
|
|
725 }
|
|
726
|
|
727 /*----------------------------------------------------------------------*/
|
|
728 /* Generalized buffer/string position convertors */
|
|
729 /*----------------------------------------------------------------------*/
|
|
730
|
|
731 /* These macros generalize many standard buffer-position functions to
|
|
732 either a buffer or a string. */
|
|
733
|
|
734 /* Converting between Memxpos's and Bytexpos's, for a buffer-or-string.
|
|
735 For strings, this is a no-op. For buffers, this resolves
|
|
736 to the standard membpos<->bytebpos converters. */
|
|
737
|
|
738 DECLARE_INLINE_HEADER (
|
|
739 Memxpos buffer_or_string_bytexpos_to_memxpos (Lisp_Object obj, Bytexpos pos)
|
|
740 )
|
|
741 {
|
|
742 return (BUFFERP (obj) ? bytebpos_to_membpos (XBUFFER (obj), pos) :
|
|
743 (Memxpos) pos);
|
428
|
744 }
|
|
745
|
826
|
746 DECLARE_INLINE_HEADER (
|
|
747 Bytexpos buffer_or_string_memxpos_to_bytexpos (Lisp_Object obj, Memxpos pos)
|
|
748 )
|
|
749 {
|
|
750 return (BUFFERP (obj) ? membpos_to_bytebpos (XBUFFER (obj), pos) :
|
|
751 (Bytexpos) pos);
|
|
752 }
|
|
753
|
|
754 /* Converting between Charxpos's and Bytexpos's, for a buffer-or-string.
|
|
755 For strings, this maps to the bytecount<->charcount converters. */
|
|
756
|
|
757 DECLARE_INLINE_HEADER (
|
|
758 Bytexpos buffer_or_string_charxpos_to_bytexpos (Lisp_Object obj, Charxpos pos)
|
|
759 )
|
|
760 {
|
|
761 return (BUFFERP (obj) ? charbpos_to_bytebpos (XBUFFER (obj), pos) :
|
|
762 (Bytexpos) string_index_char_to_byte (obj, pos));
|
|
763 }
|
|
764
|
|
765 DECLARE_INLINE_HEADER (
|
|
766 Charxpos buffer_or_string_bytexpos_to_charxpos (Lisp_Object obj, Bytexpos pos)
|
|
767 )
|
|
768 {
|
|
769 return (BUFFERP (obj) ? bytebpos_to_charbpos (XBUFFER (obj), pos) :
|
|
770 (Charxpos) string_index_byte_to_char (obj, pos));
|
|
771 }
|
428
|
772
|
826
|
773 /* Similar for Charxpos's and Memxpos's. */
|
|
774
|
|
775 DECLARE_INLINE_HEADER (
|
|
776 Memxpos buffer_or_string_charxpos_to_memxpos (Lisp_Object obj, Charxpos pos)
|
|
777 )
|
|
778 {
|
|
779 return (BUFFERP (obj) ? charbpos_to_membpos (XBUFFER (obj), pos) :
|
|
780 (Memxpos) string_index_char_to_byte (obj, pos));
|
|
781 }
|
428
|
782
|
826
|
783 DECLARE_INLINE_HEADER (
|
|
784 Charxpos buffer_or_string_memxpos_to_charxpos (Lisp_Object obj, Memxpos pos)
|
|
785 )
|
|
786 {
|
|
787 return (BUFFERP (obj) ? membpos_to_charbpos (XBUFFER (obj), pos) :
|
|
788 (Charxpos) string_index_byte_to_char (obj, pos));
|
|
789 }
|
428
|
790
|
826
|
791 DECLARE_INLINE_HEADER (
|
|
792 Internal_Format buffer_or_other_internal_format (Lisp_Object obj)
|
|
793 )
|
|
794 {
|
|
795 return BUFFERP (obj) ? BUF_FORMAT (XBUFFER (obj)) : FORMAT_DEFAULT;
|
|
796 }
|
|
797
|
|
798 /* Return the index to the character before the one at X,
|
|
799 in a buffer or string. */
|
428
|
800
|
826
|
801 DECLARE_INLINE_HEADER (
|
|
802 Bytebpos
|
|
803 prev_bytexpos (Lisp_Object obj, Bytebpos x)
|
|
804 )
|
|
805 {
|
|
806 return BUFFERP (obj) ? prev_bytebpos (XBUFFER (obj), x) :
|
|
807 prev_string_index (obj, x);
|
|
808 }
|
|
809
|
|
810 /* Return the index to the character after the one at X,
|
|
811 in a buffer or string. */
|
428
|
812
|
826
|
813 DECLARE_INLINE_HEADER (
|
|
814 Bytebpos
|
|
815 next_bytexpos (Lisp_Object obj, Bytebpos x)
|
|
816 )
|
|
817 {
|
|
818 return BUFFERP (obj) ? next_bytebpos (XBUFFER (obj), x) :
|
|
819 next_string_index (obj, x);
|
|
820 }
|
|
821
|
|
822 /*----------------------------------------------------------------------*/
|
|
823 /* Converting between positions and addresses */
|
|
824 /*----------------------------------------------------------------------*/
|
428
|
825
|
826
|
826 /* Convert the address of a byte in the buffer into a position. */
|
|
827 DECLARE_INLINE_HEADER (
|
|
828 Bytebpos
|
867
|
829 BYTE_BUF_PTR_BYTE_POS (struct buffer *buf, Ibyte *ptr)
|
826
|
830 )
|
|
831 {
|
|
832 Bytebpos retval = (ptr - buf->text->beg + 1
|
|
833 - ((ptr - buf->text->beg + 1) > buf->text->gpt
|
|
834 ? buf->text->gap_size : (Bytebpos) 0));
|
|
835 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, retval);
|
|
836 return retval;
|
|
837 }
|
|
838
|
|
839 #define BUF_PTR_BYTE_POS(buf, ptr) \
|
|
840 bytebpos_to_charbpos (buf, BYTE_BUF_PTR_BYTE_POS (buf, ptr))
|
428
|
841
|
826
|
842 /* Address of byte at position POS in buffer. */
|
|
843 DECLARE_INLINE_HEADER (
|
867
|
844 Ibyte *
|
826
|
845 BYTE_BUF_BYTE_ADDRESS (struct buffer *buf, Bytebpos pos)
|
|
846 )
|
|
847 {
|
|
848 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, pos);
|
|
849 return BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos);
|
|
850 }
|
|
851
|
|
852 #define BUF_BYTE_ADDRESS(buf, pos) \
|
|
853 BYTE_BUF_BYTE_ADDRESS (buf, charbpos_to_bytebpos (buf, pos))
|
428
|
854
|
826
|
855 /* Address of byte before position POS in buffer. */
|
|
856 DECLARE_INLINE_HEADER (
|
867
|
857 Ibyte *
|
826
|
858 BYTE_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytebpos pos)
|
|
859 )
|
|
860 {
|
|
861 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, pos);
|
|
862 return (buf->text->beg +
|
|
863 ((pos > buf->text->gpt ? (pos + buf->text->gap_size) : pos)
|
|
864 - 2));
|
|
865 }
|
|
866
|
|
867 #define BUF_BYTE_ADDRESS_BEFORE(buf, pos) \
|
|
868 BYTE_BUF_BYTE_ADDRESS_BEFORE (buf, charbpos_to_bytebpos (buf, pos))
|
428
|
869
|
|
870 /*----------------------------------------------------------------------*/
|
|
871 /* Converting between buffer bytes and Emacs characters */
|
|
872 /*----------------------------------------------------------------------*/
|
|
873
|
|
874 /* The character at position POS in buffer. */
|
826
|
875
|
|
876 #define BYTE_BUF_FETCH_CHAR(buf, pos) \
|
867
|
877 itext_ichar_fmt (BYTE_BUF_BYTE_ADDRESS (buf, pos), BUF_FORMAT (buf), \
|
826
|
878 wrap_buffer (buf))
|
428
|
879 #define BUF_FETCH_CHAR(buf, pos) \
|
826
|
880 BYTE_BUF_FETCH_CHAR (buf, charbpos_to_bytebpos (buf, pos))
|
|
881
|
|
882 /* The "raw value" of the character at position POS in buffer.
|
867
|
883 See ichar_to_raw(). */
|
826
|
884
|
|
885 #define BYTE_BUF_FETCH_CHAR_RAW(buf, pos) \
|
867
|
886 itext_ichar_raw_fmt (BYTE_BUF_BYTE_ADDRESS (buf, pos), BUF_FORMAT (buf))
|
826
|
887 #define BUF_FETCH_CHAR_RAW(buf, pos) \
|
|
888 BYTE_BUF_FETCH_CHAR_RAW (buf, charbpos_to_bytebpos (buf, pos))
|
428
|
889
|
|
890 /* The character at position POS in buffer, as a string. This is
|
867
|
891 equivalent to set_itext_ichar (str, BUF_FETCH_CHAR (buf, pos))
|
428
|
892 but is faster for Mule. */
|
|
893
|
867
|
894 # define BYTE_BUF_ITEXT_COPY_ICHAR(buf, pos, str) \
|
826
|
895 (BUF_FORMAT (buf) == FORMAT_DEFAULT ? \
|
867
|
896 itext_copy_ichar (BYTE_BUF_BYTE_ADDRESS (buf, pos), str) : \
|
|
897 set_itext_ichar (str, BYTE_BUF_FETCH_CHAR (buf, pos)))
|
|
898 #define BUF_ITEXT_COPY_ICHAR(buf, pos, str) \
|
|
899 BYTE_BUF_ITEXT_COPY_ICHAR (buf, charbpos_to_bytebpos (buf, pos), str)
|
428
|
900
|
|
901
|
|
902 /************************************************************************/
|
440
|
903 /* */
|
428
|
904 /* higher-level buffer-position functions */
|
|
905 /* */
|
|
906 /************************************************************************/
|
|
907
|
|
908 /*----------------------------------------------------------------------*/
|
|
909 /* Settor macros for important positions in a buffer */
|
|
910 /*----------------------------------------------------------------------*/
|
|
911
|
|
912 /* Set beginning of accessible range of buffer. */
|
826
|
913 #define SET_BOTH_BUF_BEGV(buf, val, bpval) \
|
428
|
914 do \
|
|
915 { \
|
826
|
916 (buf)->begv = (bpval); \
|
428
|
917 (buf)->bufbegv = (val); \
|
|
918 } while (0)
|
|
919
|
|
920 /* Set end of accessible range of buffer. */
|
826
|
921 #define SET_BOTH_BUF_ZV(buf, val, bpval) \
|
428
|
922 do \
|
|
923 { \
|
826
|
924 (buf)->zv = (bpval); \
|
428
|
925 (buf)->bufzv = (val); \
|
|
926 } while (0)
|
|
927
|
|
928 /* Set point. */
|
|
929 /* Since BEGV and ZV are almost never set, it's reasonable to enforce
|
665
|
930 the restriction that the Charbpos and Bytebpos values must both be
|
428
|
931 specified. However, point is set in lots and lots of places. So
|
|
932 we provide the ability to specify both (for efficiency) or just
|
|
933 one. */
|
826
|
934 #define BOTH_BUF_SET_PT(buf, val, bpval) set_buffer_point (buf, val, bpval)
|
|
935 #define BYTE_BUF_SET_PT(buf, bpval) \
|
|
936 do \
|
|
937 { \
|
|
938 Bytebpos __bpbsp_bpval = (bpval); \
|
|
939 BOTH_BUF_SET_PT (buf, bytebpos_to_charbpos (buf, __bpbsp_bpval), \
|
|
940 __bpbsp_bpval); \
|
|
941 } while (0)
|
|
942 #define BUF_SET_PT(buf, value) \
|
|
943 do \
|
|
944 { \
|
|
945 Bytebpos __bsp_val = (value); \
|
|
946 BOTH_BUF_SET_PT (buf, __bsp_val, charbpos_to_bytebpos (buf, __bsp_val)); \
|
|
947 } while (0)
|
428
|
948
|
|
949
|
|
950 #if 0 /* FSFmacs */
|
|
951 /* These macros exist in FSFmacs because SET_PT() in FSFmacs incorrectly
|
|
952 does too much stuff, such as moving out of invisible extents. */
|
|
953 #define TEMP_SET_PT(position) (temp_set_point ((position), current_buffer))
|
|
954 #define SET_BUF_PT(buf, value) ((buf)->pt = (value))
|
|
955 #endif /* FSFmacs */
|
|
956
|
|
957 /*----------------------------------------------------------------------*/
|
|
958 /* Miscellaneous buffer values */
|
|
959 /*----------------------------------------------------------------------*/
|
|
960
|
|
961 /* Number of characters in buffer */
|
|
962 #define BUF_SIZE(buf) (BUF_Z (buf) - BUF_BEG (buf))
|
|
963
|
|
964 /* Is this buffer narrowed? */
|
|
965 #define BUF_NARROWED(buf) \
|
826
|
966 ((BYTE_BUF_BEGV (buf) != BYTE_BUF_BEG (buf)) || \
|
|
967 (BYTE_BUF_ZV (buf) != BYTE_BUF_Z (buf)))
|
428
|
968
|
826
|
969 /* Modification count */
|
428
|
970 #define BUF_MODIFF(buf) ((buf)->text->modiff)
|
|
971
|
826
|
972 /* Saved modification count */
|
428
|
973 #define BUF_SAVE_MODIFF(buf) ((buf)->text->save_modiff)
|
|
974
|
|
975 /* Face changed. */
|
|
976 #define BUF_FACECHANGE(buf) ((buf)->face_change)
|
|
977
|
826
|
978 DECLARE_INLINE_HEADER (
|
|
979 int
|
|
980 POINT_MARKER_P (Lisp_Object marker)
|
|
981 )
|
|
982 {
|
|
983 return (XMARKER (marker)->buffer != 0 &&
|
|
984 EQ (marker, XMARKER (marker)->buffer->point_marker));
|
|
985 }
|
428
|
986
|
|
987 #define BUF_MARKERS(buf) ((buf)->markers)
|
|
988
|
826
|
989 #ifdef MULE
|
|
990
|
|
991 DECLARE_INLINE_HEADER (
|
|
992 Lisp_Object
|
|
993 BUFFER_CATEGORY_TABLE (struct buffer *buf)
|
|
994 )
|
|
995 {
|
|
996 return buf ? buf->category_table : Vstandard_category_table;
|
|
997 }
|
|
998
|
|
999 #endif /* MULE */
|
|
1000
|
|
1001 DECLARE_INLINE_HEADER (
|
|
1002 Lisp_Object
|
|
1003 BUFFER_SYNTAX_TABLE (struct buffer *buf)
|
|
1004 )
|
|
1005 {
|
|
1006 return buf ? buf->syntax_table : Vstandard_syntax_table;
|
|
1007 }
|
|
1008
|
|
1009 DECLARE_INLINE_HEADER (
|
|
1010 Lisp_Object
|
|
1011 BUFFER_MIRROR_SYNTAX_TABLE (struct buffer *buf)
|
|
1012 )
|
|
1013 {
|
|
1014 return buf ? buf->mirror_syntax_table :
|
|
1015 XCHAR_TABLE (Vstandard_syntax_table)->mirror_table;
|
|
1016 }
|
|
1017
|
428
|
1018 /* WARNING:
|
|
1019
|
|
1020 The new definitions of CEILING_OF() and FLOOR_OF() differ semantically
|
|
1021 from the old ones (in FSF Emacs and XEmacs 19.11 and before).
|
|
1022 Conversion is as follows:
|
|
1023
|
826
|
1024 OLD_BYTE_CEILING_OF(n) = NEW_BYTE_CEILING_OF(n) - 1
|
|
1025 OLD_BYTE_FLOOR_OF(n) = NEW_BYTE_FLOOR_OF(n + 1)
|
428
|
1026
|
|
1027 The definitions were changed because the new definitions are more
|
771
|
1028 consistent with the way everything else works in XEmacs.
|
428
|
1029 */
|
|
1030
|
826
|
1031 /* Properties of CEILING_OF and FLOOR_OF (also apply to BYTE_ variants):
|
428
|
1032
|
|
1033 1) FLOOR_OF (CEILING_OF (n)) = n
|
|
1034 CEILING_OF (FLOOR_OF (n)) = n
|
|
1035
|
|
1036 2) CEILING_OF (n) = n if and only if n = ZV
|
|
1037 FLOOR_OF (n) = n if and only if n = BEGV
|
|
1038
|
|
1039 3) CEILING_OF (CEILING_OF (n)) = ZV
|
|
1040 FLOOR_OF (FLOOR_OF (n)) = BEGV
|
|
1041
|
|
1042 4) The bytes in the regions
|
|
1043
|
|
1044 [BYTE_ADDRESS (n), BYTE_ADDRESS_BEFORE (CEILING_OF (n))]
|
|
1045
|
|
1046 and
|
|
1047
|
|
1048 [BYTE_ADDRESS (FLOOR_OF (n)), BYTE_ADDRESS_BEFORE (n)]
|
|
1049
|
|
1050 are contiguous.
|
771
|
1051
|
|
1052 A typical loop using CEILING_OF to process contiguous ranges of text
|
|
1053 between [from, to) looks like this:
|
|
1054
|
|
1055 {
|
|
1056 Bytebpos pos = from;
|
|
1057
|
|
1058 while (pos < to)
|
|
1059 {
|
826
|
1060 Bytebpos ceil = BYTE_BUF_CEILING_OF (buf, pos);
|
771
|
1061 ceil = min (to, ceil);
|
867
|
1062 process_ibyte_string (BYTE_BUF_BYTE_ADDRESS (buf, pos), ceil - pos);
|
771
|
1063 pos = ceil;
|
|
1064 }
|
|
1065 }
|
|
1066
|
|
1067 Currently there will be at most two iterations in the loop, but it is
|
|
1068 written in such a way that it will still work if the buffer
|
|
1069 representation is changed to have multiple gaps in it.
|
|
1070 */
|
428
|
1071
|
|
1072
|
826
|
1073 /* Return the maximum position in the buffer it is safe to scan forwards
|
428
|
1074 past N to. This is used to prevent buffer scans from running into
|
|
1075 the gap (e.g. search.c). All characters between N and CEILING_OF(N)
|
|
1076 are located contiguous in memory. Note that the character *at*
|
|
1077 CEILING_OF(N) is not contiguous in memory. */
|
826
|
1078 #define BYTE_BUF_CEILING_OF(b, n) \
|
|
1079 ((n) < (b)->text->gpt && (b)->text->gpt < BYTE_BUF_ZV (b) ? \
|
|
1080 (b)->text->gpt : BYTE_BUF_ZV (b))
|
428
|
1081 #define BUF_CEILING_OF(b, n) \
|
826
|
1082 bytebpos_to_charbpos (b, BYTE_BUF_CEILING_OF (b, charbpos_to_bytebpos (b, n)))
|
428
|
1083
|
826
|
1084 /* Return the minimum position in the buffer it is safe to scan backwards
|
428
|
1085 past N to. All characters between FLOOR_OF(N) and N are located
|
|
1086 contiguous in memory. Note that the character *at* N may not be
|
|
1087 contiguous in memory. */
|
826
|
1088 #define BYTE_BUF_FLOOR_OF(b, n) \
|
|
1089 (BYTE_BUF_BEGV (b) < (b)->text->gpt && (b)->text->gpt < (n) ? \
|
|
1090 (b)->text->gpt : BYTE_BUF_BEGV (b))
|
428
|
1091 #define BUF_FLOOR_OF(b, n) \
|
826
|
1092 bytebpos_to_charbpos (b, BYTE_BUF_FLOOR_OF (b, charbpos_to_bytebpos (b, n)))
|
428
|
1093
|
826
|
1094 #define BYTE_BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \
|
|
1095 ((n) < (b)->text->gpt && (b)->text->gpt < BYTE_BUF_Z (b) ? \
|
|
1096 (b)->text->gpt : BYTE_BUF_Z (b))
|
428
|
1097 #define BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \
|
665
|
1098 bytebpos_to_charbpos \
|
826
|
1099 (b, BYTE_BUF_CEILING_OF_IGNORE_ACCESSIBLE (b, charbpos_to_bytebpos (b, n)))
|
428
|
1100
|
826
|
1101 #define BYTE_BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \
|
|
1102 (BYTE_BUF_BEG (b) < (b)->text->gpt && (b)->text->gpt < (n) ? \
|
|
1103 (b)->text->gpt : BYTE_BUF_BEG (b))
|
428
|
1104 #define BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \
|
665
|
1105 bytebpos_to_charbpos \
|
826
|
1106 (b, BYTE_BUF_FLOOR_OF_IGNORE_ACCESSIBLE (b, charbpos_to_bytebpos (b, n)))
|
|
1107
|
|
1108 /* Iterate over contiguous chunks of text in buffer BUF, starting at POS,
|
|
1109 of length LEN. Evaluates POS and LEN only once, but BUF multiply. In
|
|
1110 each iteration, store the current chunk into RUNPTR/RUNLEN, which will
|
|
1111 be automatically declared (don't declare them yourself). This does not
|
|
1112 respect the limits of accessibility (BUF_BEGV/BUF_ZV); if you want these
|
|
1113 limits respected, you need to impose them yourself.
|
|
1114
|
|
1115 NOTE: This must be surrounded with braces! */
|
|
1116
|
|
1117 #define BUFFER_TEXT_LOOP(buf, pos, len, runptr, runlen) \
|
867
|
1118 Ibyte *runptr; \
|
826
|
1119 Bytecount runlen; \
|
|
1120 Bytebpos BTL_pos = (pos); \
|
|
1121 Bytebpos BTL_len = (len); \
|
|
1122 for (runptr = BYTE_BUF_BYTE_ADDRESS (buf, BTL_pos), \
|
|
1123 runlen = BYTE_BUF_CEILING_OF_IGNORE_ACCESSIBLE (buf, BTL_pos) - BTL_pos, \
|
|
1124 runlen = min (BTL_len, runlen); \
|
|
1125 BTL_len > 0; \
|
|
1126 BTL_pos += runlen, \
|
|
1127 BTL_len -= runlen, \
|
|
1128 runptr = BYTE_BUF_BYTE_ADDRESS (buf, BTL_pos), \
|
|
1129 runlen = BYTE_BUF_CEILING_OF_IGNORE_ACCESSIBLE (buf, BTL_pos) - BTL_pos, \
|
|
1130 runlen = min (BTL_len, runlen))
|
428
|
1131
|
|
1132 /* This structure marks which slots in a buffer have corresponding
|
|
1133 default values in Vbuffer_defaults.
|
|
1134 Each such slot has a nonzero value in this structure.
|
|
1135 The value has only one nonzero bit.
|
|
1136
|
|
1137 When a buffer has its own local value for a slot,
|
|
1138 the bit for that slot (found in the same slot in this structure)
|
|
1139 is turned on in the buffer's local_var_flags slot.
|
|
1140
|
|
1141 If a slot in this structure is zero, then even though there may
|
|
1142 be a DEFVAR_BUFFER_LOCAL for the slot, there is no default value for it;
|
|
1143 and the corresponding slot in Vbuffer_defaults is not used. */
|
|
1144
|
|
1145 extern struct buffer buffer_local_flags;
|
|
1146
|
|
1147
|
|
1148 /* Allocation of buffer data. */
|
|
1149
|
|
1150 #ifdef REL_ALLOC
|
|
1151
|
440
|
1152 char *r_alloc (unsigned char **, size_t);
|
|
1153 char *r_re_alloc (unsigned char **, size_t);
|
428
|
1154 void r_alloc_free (unsigned char **);
|
|
1155
|
|
1156 #define BUFFER_ALLOC(data, size) \
|
867
|
1157 ((Ibyte *) r_alloc ((unsigned char **) &data, (size) * sizeof(Ibyte)))
|
428
|
1158 #define BUFFER_REALLOC(data, size) \
|
867
|
1159 ((Ibyte *) r_re_alloc ((unsigned char **) &data, (size) * sizeof(Ibyte)))
|
428
|
1160 #define BUFFER_FREE(data) r_alloc_free ((unsigned char **) &(data))
|
|
1161 #define R_ALLOC_DECLARE(var,data) r_alloc_declare (&(var), data)
|
|
1162
|
|
1163 #else /* !REL_ALLOC */
|
|
1164
|
|
1165 #define BUFFER_ALLOC(data,size)\
|
867
|
1166 (data = xnew_array (Ibyte, size))
|
428
|
1167 #define BUFFER_REALLOC(data,size)\
|
867
|
1168 ((Ibyte *) xrealloc (data, (size) * sizeof(Ibyte)))
|
428
|
1169 /* Avoid excess parentheses, or syntax errors may rear their heads. */
|
|
1170 #define BUFFER_FREE(data) xfree (data)
|
|
1171 #define R_ALLOC_DECLARE(var,data)
|
|
1172
|
|
1173 #endif /* !REL_ALLOC */
|
|
1174
|
|
1175
|
|
1176 /************************************************************************/
|
|
1177 /* Case conversion */
|
|
1178 /************************************************************************/
|
|
1179
|
|
1180 /* A "trt" table is a mapping from characters to other characters,
|
826
|
1181 typically used to convert between uppercase and lowercase.
|
428
|
1182 */
|
|
1183
|
|
1184 /* The _1 macros are named as such because they assume that you have
|
|
1185 already guaranteed that the character values are all in the range
|
|
1186 0 - 255. Bad lossage will happen otherwise. */
|
|
1187
|
446
|
1188 #define MAKE_TRT_TABLE() Fmake_char_table (Qgeneric)
|
826
|
1189 DECLARE_INLINE_HEADER (
|
867
|
1190 Ichar
|
|
1191 TRT_TABLE_OF (Lisp_Object table, Ichar ch)
|
826
|
1192 )
|
446
|
1193 {
|
|
1194 Lisp_Object TRT_char;
|
826
|
1195 TRT_char = get_char_table (ch, table);
|
446
|
1196 if (NILP (TRT_char))
|
|
1197 return ch;
|
|
1198 else
|
|
1199 return XCHAR (TRT_char);
|
|
1200 }
|
826
|
1201 #define SET_TRT_TABLE_OF(table, ch1, ch2) \
|
|
1202 Fput_char_table (make_char (ch1), make_char (ch2), table)
|
428
|
1203
|
826
|
1204 DECLARE_INLINE_HEADER (
|
|
1205 Lisp_Object
|
771
|
1206 BUFFER_CASE_TABLE (struct buffer *buf)
|
826
|
1207 )
|
771
|
1208 {
|
|
1209 return buf ? buf->case_table : Vstandard_case_table;
|
|
1210 }
|
|
1211
|
428
|
1212 /* Macros used below. */
|
446
|
1213 #define DOWNCASE_TABLE_OF(buf, c) \
|
771
|
1214 TRT_TABLE_OF (XCASE_TABLE_DOWNCASE (BUFFER_CASE_TABLE (buf)), c)
|
446
|
1215 #define UPCASE_TABLE_OF(buf, c) \
|
771
|
1216 TRT_TABLE_OF (XCASE_TABLE_UPCASE (BUFFER_CASE_TABLE (buf)), c)
|
428
|
1217
|
|
1218 /* 1 if CH is upper case. */
|
|
1219
|
826
|
1220 DECLARE_INLINE_HEADER (
|
|
1221 int
|
867
|
1222 UPPERCASEP (struct buffer *buf, Ichar ch)
|
826
|
1223 )
|
428
|
1224 {
|
|
1225 return DOWNCASE_TABLE_OF (buf, ch) != ch;
|
|
1226 }
|
|
1227
|
|
1228 /* 1 if CH is lower case. */
|
|
1229
|
826
|
1230 DECLARE_INLINE_HEADER (
|
|
1231 int
|
867
|
1232 LOWERCASEP (struct buffer *buf, Ichar ch)
|
826
|
1233 )
|
428
|
1234 {
|
|
1235 return (UPCASE_TABLE_OF (buf, ch) != ch &&
|
|
1236 DOWNCASE_TABLE_OF (buf, ch) == ch);
|
|
1237 }
|
|
1238
|
|
1239 /* 1 if CH is neither upper nor lower case. */
|
|
1240
|
826
|
1241 DECLARE_INLINE_HEADER (
|
|
1242 int
|
867
|
1243 NOCASEP (struct buffer *buf, Ichar ch)
|
826
|
1244 )
|
428
|
1245 {
|
|
1246 return UPCASE_TABLE_OF (buf, ch) == ch;
|
|
1247 }
|
|
1248
|
|
1249 /* Upcase a character, or make no change if that cannot be done. */
|
|
1250
|
826
|
1251 DECLARE_INLINE_HEADER (
|
867
|
1252 Ichar
|
|
1253 UPCASE (struct buffer *buf, Ichar ch)
|
826
|
1254 )
|
428
|
1255 {
|
|
1256 return (DOWNCASE_TABLE_OF (buf, ch) == ch) ? UPCASE_TABLE_OF (buf, ch) : ch;
|
|
1257 }
|
|
1258
|
|
1259 /* Upcase a character known to be not upper case. Unused. */
|
|
1260
|
|
1261 #define UPCASE1(buf, ch) UPCASE_TABLE_OF (buf, ch)
|
|
1262
|
|
1263 /* Downcase a character, or make no change if that cannot be done. */
|
|
1264
|
|
1265 #define DOWNCASE(buf, ch) DOWNCASE_TABLE_OF (buf, ch)
|
|
1266
|
440
|
1267 #endif /* INCLUDED_buffer_h_ */
|