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 {
|
665
|
79 Intbyte *beg; /* Actual address of buffer contents. */
|
|
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
|
|
106 int entirely_ascii_p;
|
428
|
107 #endif
|
|
108
|
|
109 /* Similar to the above, we keep track of positions for which line
|
|
110 number has last been calculated. See line-number.c. */
|
|
111 Lisp_Object line_number_cache;
|
|
112
|
|
113 /* Change data that goes with the text. */
|
|
114 struct buffer_text_change_data *changes;
|
|
115 };
|
|
116
|
|
117 struct buffer
|
|
118 {
|
|
119 struct lcrecord_header header;
|
|
120
|
|
121 /* This structure holds the coordinates of the buffer contents
|
|
122 in ordinary buffers. In indirect buffers, this is not used. */
|
|
123 struct buffer_text own_text;
|
|
124
|
|
125 /* This points to the `struct buffer_text' that is used for this buffer.
|
|
126 In an ordinary buffer, this is the own_text field above.
|
|
127 In an indirect buffer, this is the own_text field of another buffer. */
|
|
128 struct buffer_text *text;
|
|
129
|
665
|
130 Bytebpos pt; /* Position of point in buffer. */
|
|
131 Charbpos bufpt; /* Equivalent as a Charbpos. */
|
|
132 Bytebpos begv; /* Index of beginning of accessible range. */
|
|
133 Charbpos bufbegv; /* Equivalent as a Charbpos. */
|
|
134 Bytebpos zv; /* Index of end of accessible range. */
|
|
135 Charbpos bufzv; /* Equivalent as a Charbpos. */
|
428
|
136
|
|
137 int face_change; /* This is set when a change in how the text should
|
|
138 be displayed (e.g., font, color) is made. */
|
|
139
|
448
|
140 /* Whether buffer specific face is specified. */
|
|
141 int buffer_local_face_property;
|
|
142
|
428
|
143 /* change data indicating what portion of the text has changed
|
|
144 since the last time this was reset. Used by redisplay.
|
|
145 Logically we should keep this with the text structure, but
|
|
146 redisplay resets it for each buffer individually and we don't
|
|
147 want interference between an indirect buffer and its base
|
|
148 buffer. */
|
|
149 struct each_buffer_change_data *changes;
|
|
150
|
|
151 #ifdef REGION_CACHE_NEEDS_WORK
|
|
152 /* If the long line scan cache is enabled (i.e. the buffer-local
|
|
153 variable cache-long-line-scans is non-nil), newline_cache
|
|
154 points to the newline cache, and width_run_cache points to the
|
|
155 width run cache.
|
|
156
|
|
157 The newline cache records which stretches of the buffer are
|
|
158 known *not* to contain newlines, so that they can be skipped
|
|
159 quickly when we search for newlines.
|
|
160
|
|
161 The width run cache records which stretches of the buffer are
|
|
162 known to contain characters whose widths are all the same. If
|
|
163 the width run cache maps a character to a value > 0, that value
|
|
164 is the character's width; if it maps a character to zero, we
|
|
165 don't know what its width is. This allows compute_motion to
|
|
166 process such regions very quickly, using algebra instead of
|
|
167 inspecting each character. See also width_table, below. */
|
|
168 struct region_cache *newline_cache;
|
|
169 struct region_cache *width_run_cache;
|
|
170 #endif /* REGION_CACHE_NEEDS_WORK */
|
|
171
|
|
172 /* The markers that refer to this buffer. This is actually a single
|
|
173 marker -- successive elements in its marker `chain' are the other
|
|
174 markers referring to this buffer */
|
440
|
175 Lisp_Marker *markers;
|
428
|
176
|
|
177 /* The buffer's extent info. This is its own type, an extent-info
|
|
178 object (done this way for ease in marking / finalizing). */
|
|
179 Lisp_Object extent_info;
|
|
180
|
|
181 /* ----------------------------------------------------------------- */
|
|
182 /* All the stuff above this line is the responsibility of insdel.c,
|
|
183 with some help from marker.c and extents.c.
|
|
184 All the stuff below this line is the responsibility of buffer.c. */
|
|
185
|
|
186 /* In an indirect buffer, this points to the base buffer.
|
|
187 In an ordinary buffer, it is 0.
|
|
188 We DO mark through this slot. */
|
|
189 struct buffer *base_buffer;
|
|
190
|
|
191 /* List of indirect buffers whose base is this buffer.
|
|
192 If we are an indirect buffer, this will be nil.
|
|
193 Do NOT mark through this. */
|
|
194 Lisp_Object indirect_children;
|
|
195
|
|
196 /* Flags saying which DEFVAR_PER_BUFFER variables
|
|
197 are local to this buffer. */
|
|
198 int local_var_flags;
|
|
199
|
|
200 /* Set to the modtime of the visited file when read or written.
|
|
201 -1 means visited file was nonexistent.
|
|
202 0 means visited file modtime unknown; in no case complain
|
|
203 about any mismatch on next save attempt. */
|
|
204 int modtime;
|
|
205
|
|
206 /* the value of text->modiff at the last auto-save. */
|
442
|
207 long auto_save_modified;
|
428
|
208
|
|
209 /* The time at which we detected a failure to auto-save,
|
|
210 Or -1 if we didn't have a failure. */
|
|
211 int auto_save_failure_time;
|
|
212
|
|
213 /* Position in buffer at which display started
|
|
214 the last time this buffer was displayed. */
|
|
215 int last_window_start;
|
|
216
|
|
217 /* Everything from here down must be a Lisp_Object */
|
|
218
|
|
219 #define MARKED_SLOT(x) Lisp_Object x
|
|
220 #include "bufslots.h"
|
|
221 #undef MARKED_SLOT
|
|
222 };
|
|
223
|
|
224 DECLARE_LRECORD (buffer, struct buffer);
|
|
225 #define XBUFFER(x) XRECORD (x, buffer, struct buffer)
|
617
|
226 #define wrap_buffer(p) wrap_record (p, buffer)
|
428
|
227 #define BUFFERP(x) RECORDP (x, buffer)
|
|
228 #define CHECK_BUFFER(x) CHECK_RECORD (x, buffer)
|
|
229 #define CONCHECK_BUFFER(x) CONCHECK_RECORD (x, buffer)
|
|
230
|
|
231 #define BUFFER_LIVE_P(b) (!NILP ((b)->name))
|
|
232
|
|
233 #define CHECK_LIVE_BUFFER(x) do { \
|
|
234 CHECK_BUFFER (x); \
|
|
235 if (!BUFFER_LIVE_P (XBUFFER (x))) \
|
|
236 dead_wrong_type_argument (Qbuffer_live_p, (x)); \
|
|
237 } while (0)
|
|
238
|
|
239 #define CONCHECK_LIVE_BUFFER(x) do { \
|
|
240 CONCHECK_BUFFER (x); \
|
|
241 if (!BUFFER_LIVE_P (XBUFFER (x))) \
|
|
242 x = wrong_type_argument (Qbuffer_live_p, (x)); \
|
|
243 } while (0)
|
|
244
|
|
245
|
|
246 #define BUFFER_BASE_BUFFER(b) ((b)->base_buffer ? (b)->base_buffer : (b))
|
|
247
|
|
248 /* Map over buffers sharing the same text as MPS_BUF. MPS_BUFVAR is a
|
|
249 variable that gets the buffer values (beginning with the base
|
|
250 buffer, then the children), and MPS_BUFCONS should be a temporary
|
|
251 Lisp_Object variable. */
|
647
|
252 #define MAP_INDIRECT_BUFFERS(mps_buf, mps_bufvar, mps_bufcons) \
|
|
253 for (mps_bufcons = Qunbound, \
|
|
254 mps_bufvar = BUFFER_BASE_BUFFER (mps_buf); \
|
|
255 UNBOUNDP (mps_bufcons) ? \
|
|
256 (mps_bufcons = mps_bufvar->indirect_children, \
|
|
257 1) \
|
|
258 : (!NILP (mps_bufcons) \
|
|
259 && (mps_bufvar = XBUFFER (XCAR (mps_bufcons)), 1) \
|
|
260 && (mps_bufcons = XCDR (mps_bufcons), 1)); \
|
428
|
261 )
|
|
262
|
|
263
|
|
264 /*----------------------------------------------------------------------*/
|
|
265 /* Accessor macros for important positions in a buffer */
|
|
266 /*----------------------------------------------------------------------*/
|
|
267
|
|
268 /* We put them here because some stuff below wants them before the
|
|
269 place where we would normally put them. */
|
|
270
|
|
271 /* None of these are lvalues. Use the settor macros below to change
|
|
272 the positions. */
|
|
273
|
|
274 /* Beginning of buffer. */
|
665
|
275 #define BI_BUF_BEG(buf) ((Bytebpos) 1)
|
|
276 #define BUF_BEG(buf) ((Charbpos) 1)
|
428
|
277
|
|
278 /* Beginning of accessible range of buffer. */
|
|
279 #define BI_BUF_BEGV(buf) ((buf)->begv + 0)
|
|
280 #define BUF_BEGV(buf) ((buf)->bufbegv + 0)
|
|
281
|
|
282 /* End of accessible range of buffer. */
|
|
283 #define BI_BUF_ZV(buf) ((buf)->zv + 0)
|
|
284 #define BUF_ZV(buf) ((buf)->bufzv + 0)
|
|
285
|
|
286 /* End of buffer. */
|
|
287 #define BI_BUF_Z(buf) ((buf)->text->z + 0)
|
|
288 #define BUF_Z(buf) ((buf)->text->bufz + 0)
|
|
289
|
|
290 /* Point. */
|
|
291 #define BI_BUF_PT(buf) ((buf)->pt + 0)
|
|
292 #define BUF_PT(buf) ((buf)->bufpt + 0)
|
|
293
|
|
294 /*----------------------------------------------------------------------*/
|
|
295 /* Converting between positions and addresses */
|
|
296 /*----------------------------------------------------------------------*/
|
|
297
|
|
298 /* Convert the address of a byte in the buffer into a position. */
|
665
|
299 INLINE_HEADER Bytebpos BI_BUF_PTR_BYTE_POS (struct buffer *buf, Intbyte *ptr);
|
|
300 INLINE_HEADER Bytebpos
|
|
301 BI_BUF_PTR_BYTE_POS (struct buffer *buf, Intbyte *ptr)
|
428
|
302 {
|
|
303 return (ptr - buf->text->beg + 1
|
|
304 - ((ptr - buf->text->beg + 1) > buf->text->gpt
|
|
305 ? buf->text->gap_size : 0));
|
|
306 }
|
|
307
|
|
308 #define BUF_PTR_BYTE_POS(buf, ptr) \
|
665
|
309 bytebpos_to_charbpos (buf, BI_BUF_PTR_BYTE_POS (buf, ptr))
|
428
|
310
|
|
311 /* Address of byte at position POS in buffer. */
|
665
|
312 INLINE_HEADER Intbyte * BI_BUF_BYTE_ADDRESS (struct buffer *buf, Bytebpos pos);
|
|
313 INLINE_HEADER Intbyte *
|
|
314 BI_BUF_BYTE_ADDRESS (struct buffer *buf, Bytebpos pos)
|
428
|
315 {
|
|
316 return (buf->text->beg +
|
|
317 ((pos >= buf->text->gpt ? (pos + buf->text->gap_size) : pos)
|
|
318 - 1));
|
|
319 }
|
|
320
|
|
321 #define BUF_BYTE_ADDRESS(buf, pos) \
|
665
|
322 BI_BUF_BYTE_ADDRESS (buf, charbpos_to_bytebpos (buf, pos))
|
428
|
323
|
|
324 /* Address of byte before position POS in buffer. */
|
665
|
325 INLINE_HEADER Intbyte * BI_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytebpos pos);
|
|
326 INLINE_HEADER Intbyte *
|
|
327 BI_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytebpos pos)
|
428
|
328 {
|
|
329 return (buf->text->beg +
|
|
330 ((pos > buf->text->gpt ? (pos + buf->text->gap_size) : pos)
|
|
331 - 2));
|
|
332 }
|
|
333
|
|
334 #define BUF_BYTE_ADDRESS_BEFORE(buf, pos) \
|
665
|
335 BI_BUF_BYTE_ADDRESS_BEFORE (buf, charbpos_to_bytebpos (buf, pos))
|
428
|
336
|
|
337 /*----------------------------------------------------------------------*/
|
|
338 /* Converting between byte indices and memory indices */
|
|
339 /*----------------------------------------------------------------------*/
|
|
340
|
665
|
341 INLINE_HEADER int valid_membpos_p (struct buffer *buf, Membpos x);
|
442
|
342 INLINE_HEADER int
|
665
|
343 valid_membpos_p (struct buffer *buf, Membpos x)
|
428
|
344 {
|
665
|
345 return ((x >= 1 && x <= (Membpos) buf->text->gpt) ||
|
|
346 (x > (Membpos) (buf->text->gpt + buf->text->gap_size) &&
|
|
347 x <= (Membpos) (buf->text->z + buf->text->gap_size)));
|
428
|
348 }
|
|
349
|
665
|
350 INLINE_HEADER Membpos bytebpos_to_membpos (struct buffer *buf, Bytebpos x);
|
|
351 INLINE_HEADER Membpos
|
|
352 bytebpos_to_membpos (struct buffer *buf, Bytebpos x)
|
428
|
353 {
|
665
|
354 return (Membpos) ((x > buf->text->gpt) ? (x + buf->text->gap_size) : x);
|
428
|
355 }
|
|
356
|
|
357
|
665
|
358 INLINE_HEADER Bytebpos membpos_to_bytebpos (struct buffer *buf, Membpos x);
|
|
359 INLINE_HEADER Bytebpos
|
|
360 membpos_to_bytebpos (struct buffer *buf, Membpos x)
|
428
|
361 {
|
800
|
362 #ifdef ERROR_CHECK_TEXT
|
665
|
363 assert (valid_membpos_p (buf, x));
|
428
|
364 #endif
|
665
|
365 return (Bytebpos) ((x > (Membpos) buf->text->gpt) ?
|
428
|
366 x - buf->text->gap_size :
|
|
367 x);
|
|
368 }
|
|
369
|
665
|
370 #define membpos_to_charbpos(buf, x) \
|
|
371 bytebpos_to_charbpos (buf, membpos_to_bytebpos (buf, x))
|
|
372 #define charbpos_to_membpos(buf, x) \
|
|
373 bytebpos_to_membpos (buf, charbpos_to_bytebpos (buf, x))
|
428
|
374
|
|
375 /* These macros generalize many standard buffer-position functions to
|
|
376 either a buffer or a string. */
|
|
377
|
665
|
378 /* Converting between Membposs and Bytebposs, for a buffer-or-string.
|
428
|
379 For strings, this is a no-op. For buffers, this resolves
|
665
|
380 to the standard membpos<->bytebpos converters. */
|
428
|
381
|
665
|
382 #define buffer_or_string_bytebpos_to_membpos(obj, ind) \
|
|
383 (BUFFERP (obj) ? bytebpos_to_membpos (XBUFFER (obj), ind) : (Membpos) ind)
|
428
|
384
|
665
|
385 #define buffer_or_string_membpos_to_bytebpos(obj, ind) \
|
|
386 (BUFFERP (obj) ? membpos_to_bytebpos (XBUFFER (obj), ind) : (Bytebpos) ind)
|
428
|
387
|
665
|
388 /* Converting between Charbpos's and Bytebposs, for a buffer-or-string.
|
428
|
389 For strings, this maps to the bytecount<->charcount converters. */
|
|
390
|
665
|
391 #define buffer_or_string_charbpos_to_bytebpos(obj, pos) \
|
|
392 (BUFFERP (obj) ? charbpos_to_bytebpos (XBUFFER (obj), pos) : \
|
793
|
393 (Bytebpos) string_index_char_to_byte (obj, pos))
|
428
|
394
|
665
|
395 #define buffer_or_string_bytebpos_to_charbpos(obj, ind) \
|
|
396 (BUFFERP (obj) ? bytebpos_to_charbpos (XBUFFER (obj), ind) : \
|
793
|
397 (Charbpos) string_index_byte_to_char (obj, ind))
|
428
|
398
|
665
|
399 /* Similar for Charbpos's and Membposs. */
|
428
|
400
|
665
|
401 #define buffer_or_string_charbpos_to_membpos(obj, pos) \
|
|
402 (BUFFERP (obj) ? charbpos_to_membpos (XBUFFER (obj), pos) : \
|
793
|
403 (Membpos) string_index_char_to_byte (obj, pos))
|
428
|
404
|
665
|
405 #define buffer_or_string_membpos_to_charbpos(obj, ind) \
|
|
406 (BUFFERP (obj) ? membpos_to_charbpos (XBUFFER (obj), ind) : \
|
793
|
407 (Charbpos) string_index_byte_to_char (obj, ind))
|
428
|
408
|
|
409 /************************************************************************/
|
|
410 /* */
|
|
411 /* working with buffer-level data */
|
|
412 /* */
|
|
413 /************************************************************************/
|
|
414
|
|
415 /*
|
|
416
|
|
417 (A) Working with byte indices:
|
|
418 ------------------------------
|
|
419
|
665
|
420 VALID_BYTEBPOS_P(buf, bi):
|
428
|
421 Given a byte index, does it point to the beginning of a character?
|
|
422
|
665
|
423 ASSERT_VALID_BYTEBPOS_UNSAFE(buf, bi):
|
428
|
424 If error-checking is enabled, assert that the given byte index
|
|
425 is within range and points to the beginning of a character
|
|
426 or to the end of the buffer. Otherwise, do nothing.
|
|
427
|
665
|
428 ASSERT_VALID_BYTEBPOS_BACKWARD_UNSAFE(buf, bi):
|
428
|
429 If error-checking is enabled, assert that the given byte index
|
665
|
430 is within range and satisfies ASSERT_VALID_BYTEBPOS() and also
|
428
|
431 does not refer to the beginning of the buffer. (i.e. movement
|
|
432 backwards is OK.) Otherwise, do nothing.
|
|
433
|
665
|
434 ASSERT_VALID_BYTEBPOS_FORWARD_UNSAFE(buf, bi):
|
428
|
435 If error-checking is enabled, assert that the given byte index
|
665
|
436 is within range and satisfies ASSERT_VALID_BYTEBPOS() and also
|
428
|
437 does not refer to the end of the buffer. (i.e. movement
|
|
438 forwards is OK.) Otherwise, do nothing.
|
|
439
|
665
|
440 VALIDATE_BYTEBPOS_BACKWARD(buf, bi):
|
428
|
441 Make sure that the given byte index is pointing to the beginning
|
|
442 of a character. If not, back up until this is the case. Note
|
|
443 that there are not too many places where it is legitimate to do
|
|
444 this sort of thing. It's an error if you're passed an "invalid"
|
|
445 byte index.
|
|
446
|
665
|
447 VALIDATE_BYTEBPOS_FORWARD(buf, bi):
|
428
|
448 Make sure that the given byte index is pointing to the beginning
|
|
449 of a character. If not, move forward until this is the case.
|
|
450 Note that there are not too many places where it is legitimate
|
|
451 to do this sort of thing. It's an error if you're passed an
|
|
452 "invalid" byte index.
|
|
453
|
665
|
454 INC_BYTEBPOS(buf, bi):
|
428
|
455 Given a byte index (assumed to point at the beginning of a
|
|
456 character), modify that value so it points to the beginning
|
|
457 of the next character.
|
|
458
|
665
|
459 DEC_BYTEBPOS(buf, bi):
|
428
|
460 Given a byte index (assumed to point at the beginning of a
|
|
461 character), modify that value so it points to the beginning
|
|
462 of the previous character. Unlike for DEC_CHARPTR(), we can
|
|
463 do all the assert()s because there are sentinels at the
|
|
464 beginning of the gap and the end of the buffer.
|
|
465
|
665
|
466 BYTEBPOS_INVALID:
|
|
467 A constant representing an invalid Bytebpos. Valid Bytebposs
|
428
|
468 can never have this value.
|
|
469
|
|
470
|
665
|
471 (B) Converting between Charbpos's and Bytebposs:
|
428
|
472 --------------------------------------------
|
|
473
|
665
|
474 charbpos_to_bytebpos(buf, bu):
|
|
475 Given a Charbpos, return the equivalent Bytebpos.
|
428
|
476
|
665
|
477 bytebpos_to_charbpos(buf, bi):
|
|
478 Given a Bytebpos, return the equivalent Charbpos.
|
428
|
479
|
665
|
480 make_charbpos(buf, bi):
|
|
481 Given a Bytebpos, return the equivalent Charbpos as a Lisp Object.
|
428
|
482 */
|
|
483
|
|
484
|
|
485 /*----------------------------------------------------------------------*/
|
|
486 /* working with byte indices */
|
|
487 /*----------------------------------------------------------------------*/
|
|
488
|
|
489 #ifdef MULE
|
665
|
490 # define VALID_BYTEBPOS_P(buf, x) \
|
|
491 INTBYTE_FIRST_BYTE_P (*BI_BUF_BYTE_ADDRESS (buf, x))
|
428
|
492 #else
|
665
|
493 # define VALID_BYTEBPOS_P(buf, x) 1
|
428
|
494 #endif
|
|
495
|
800
|
496 #ifdef ERROR_CHECK_TEXT
|
428
|
497
|
665
|
498 # define ASSERT_VALID_BYTEBPOS_UNSAFE(buf, x) do { \
|
428
|
499 assert (BUFFER_LIVE_P (buf)); \
|
|
500 assert ((x) >= BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf)); \
|
665
|
501 assert (VALID_BYTEBPOS_P (buf, x)); \
|
428
|
502 } while (0)
|
665
|
503 # define ASSERT_VALID_BYTEBPOS_BACKWARD_UNSAFE(buf, x) do { \
|
428
|
504 assert (BUFFER_LIVE_P (buf)); \
|
|
505 assert ((x) > BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf)); \
|
665
|
506 assert (VALID_BYTEBPOS_P (buf, x)); \
|
428
|
507 } while (0)
|
665
|
508 # define ASSERT_VALID_BYTEBPOS_FORWARD_UNSAFE(buf, x) do { \
|
428
|
509 assert (BUFFER_LIVE_P (buf)); \
|
|
510 assert ((x) >= BI_BUF_BEG (buf) && x < BI_BUF_Z (buf)); \
|
665
|
511 assert (VALID_BYTEBPOS_P (buf, x)); \
|
428
|
512 } while (0)
|
|
513
|
800
|
514 #else /* not ERROR_CHECK_TEXT */
|
665
|
515 # define ASSERT_VALID_BYTEBPOS_UNSAFE(buf, x)
|
|
516 # define ASSERT_VALID_BYTEBPOS_BACKWARD_UNSAFE(buf, x)
|
|
517 # define ASSERT_VALID_BYTEBPOS_FORWARD_UNSAFE(buf, x)
|
428
|
518
|
800
|
519 #endif /* not ERROR_CHECK_TEXT */
|
428
|
520
|
|
521 /* Note that, although the Mule version will work fine for non-Mule
|
|
522 as well (it should reduce down to nothing), we provide a separate
|
|
523 version to avoid compilation warnings and possible non-optimal
|
|
524 results with stupid compilers. */
|
|
525
|
|
526 #ifdef MULE
|
665
|
527 # define VALIDATE_BYTEBPOS_BACKWARD(buf, x) do { \
|
|
528 Intbyte *VBB_ptr = BI_BUF_BYTE_ADDRESS (buf, x); \
|
|
529 while (!INTBYTE_FIRST_BYTE_P (*VBB_ptr)) \
|
428
|
530 VBB_ptr--, (x)--; \
|
|
531 } while (0)
|
|
532 #else
|
665
|
533 # define VALIDATE_BYTEBPOS_BACKWARD(buf, x)
|
428
|
534 #endif
|
|
535
|
|
536 /* Note that, although the Mule version will work fine for non-Mule
|
|
537 as well (it should reduce down to nothing), we provide a separate
|
|
538 version to avoid compilation warnings and possible non-optimal
|
|
539 results with stupid compilers. */
|
|
540
|
|
541 #ifdef MULE
|
665
|
542 # define VALIDATE_BYTEBPOS_FORWARD(buf, x) do { \
|
|
543 Intbyte *VBF_ptr = BI_BUF_BYTE_ADDRESS (buf, x); \
|
|
544 while (!INTBYTE_FIRST_BYTE_P (*VBF_ptr)) \
|
428
|
545 VBF_ptr++, (x)++; \
|
|
546 } while (0)
|
|
547 #else
|
665
|
548 # define VALIDATE_BYTEBPOS_FORWARD(buf, x)
|
428
|
549 #endif
|
|
550
|
800
|
551 /* Note that in the simplest case (no MULE, no ERROR_CHECK_TEXT),
|
428
|
552 this crap reduces down to simply (x)++. */
|
|
553
|
665
|
554 #define INC_BYTEBPOS(buf, x) do \
|
428
|
555 { \
|
665
|
556 ASSERT_VALID_BYTEBPOS_FORWARD_UNSAFE (buf, x); \
|
428
|
557 /* Note that we do the increment first to \
|
|
558 make sure that the pointer in \
|
665
|
559 VALIDATE_BYTEBPOS_FORWARD() ends up on \
|
428
|
560 the correct side of the gap */ \
|
|
561 (x)++; \
|
665
|
562 VALIDATE_BYTEBPOS_FORWARD (buf, x); \
|
428
|
563 } while (0)
|
|
564
|
800
|
565 /* Note that in the simplest case (no MULE, no ERROR_CHECK_TEXT),
|
428
|
566 this crap reduces down to simply (x)--. */
|
|
567
|
665
|
568 #define DEC_BYTEBPOS(buf, x) do \
|
428
|
569 { \
|
771
|
570 ASSERT_VALID_BYTEBPOS_BACKWARD_UNSAFE (buf, x); \
|
428
|
571 /* Note that we do the decrement first to \
|
|
572 make sure that the pointer in \
|
665
|
573 VALIDATE_BYTEBPOS_BACKWARD() ends up on \
|
428
|
574 the correct side of the gap */ \
|
|
575 (x)--; \
|
665
|
576 VALIDATE_BYTEBPOS_BACKWARD (buf, x); \
|
428
|
577 } while (0)
|
|
578
|
665
|
579 INLINE_HEADER Bytebpos prev_bytebpos (struct buffer *buf, Bytebpos x);
|
|
580 INLINE_HEADER Bytebpos
|
|
581 prev_bytebpos (struct buffer *buf, Bytebpos x)
|
428
|
582 {
|
665
|
583 DEC_BYTEBPOS (buf, x);
|
428
|
584 return x;
|
|
585 }
|
|
586
|
665
|
587 INLINE_HEADER Bytebpos next_bytebpos (struct buffer *buf, Bytebpos x);
|
|
588 INLINE_HEADER Bytebpos
|
|
589 next_bytebpos (struct buffer *buf, Bytebpos x)
|
428
|
590 {
|
665
|
591 INC_BYTEBPOS (buf, x);
|
428
|
592 return x;
|
|
593 }
|
|
594
|
665
|
595 #define BYTEBPOS_INVALID ((Bytebpos) -1)
|
428
|
596
|
|
597 /*----------------------------------------------------------------------*/
|
|
598 /* Converting between buffer positions and byte indices */
|
|
599 /*----------------------------------------------------------------------*/
|
|
600
|
|
601 #ifdef MULE
|
|
602
|
665
|
603 Bytebpos charbpos_to_bytebpos_func (struct buffer *buf, Charbpos x);
|
|
604 Charbpos bytebpos_to_charbpos_func (struct buffer *buf, Bytebpos x);
|
428
|
605
|
|
606 /* The basic algorithm we use is to keep track of a known region of
|
771
|
607 characters in each buffer, all of which are of the same width. We keep
|
|
608 track of the boundaries of the region in both Charbpos and Bytebpos
|
|
609 coordinates and also keep track of the char width, which is 1 - 4 bytes.
|
|
610 If the position we're translating is not in the known region, then we
|
|
611 invoke a function to update the known region to surround the position in
|
|
612 question. This assumes locality of reference, which is usually the
|
|
613 case.
|
|
614
|
|
615 Note that the function to update the known region can be simple or
|
|
616 complicated depending on how much information we cache. In addition to
|
|
617 the known region, we always cache the correct conversions for point,
|
|
618 BEGV, and ZV, and in addition to this we cache 16 positions where the
|
|
619 conversion is known. We only look in the cache or update it when we
|
|
620 need to move the known region more than a certain amount (currently 50
|
|
621 chars), and then we throw away a "random" value and replace it with the
|
|
622 newly calculated value.
|
|
623
|
|
624 Finally, we maintain an extra flag that tracks whether the buffer is
|
|
625 entirely ASCII, to speed up the conversions even more. This flag is
|
|
626 actually of dubious value because in an entirely-ASCII buffer the known
|
|
627 region will always span the entire buffer (in fact, we update the flag
|
|
628 based on this fact), and so all we're saving is a few machine cycles.
|
428
|
629
|
771
|
630 A potentially smarter method than what we do with known regions and
|
|
631 cached positions would be to keep some sort of pseudo-extent layer over
|
|
632 the buffer; maybe keep track of the charbpos/bytebpos correspondence at the
|
|
633 beginning of each line, which would allow us to do a binary search over
|
|
634 the pseudo-extents to narrow things down to the correct line, at which
|
|
635 point you could use a linear movement method. This would also mesh well
|
|
636 with efficiently implementing a line-numbering scheme. However, you
|
|
637 have to weigh the amount of time spent updating the cache vs. the
|
|
638 savings that result from it. In reality, we modify the buffer far less
|
|
639 often than we access it, so a cache of this sort that provides
|
|
640 guaranteed LOG (N) performance (or perhaps N * LOG (N), if we set a
|
|
641 maximum on the cache size) would indeed be a win, particularly in very
|
|
642 large buffers. If we ever implement this, we should probably set a
|
|
643 reasonably high minimum below which we use the old method, because the
|
|
644 time spent updating the fancy cache would likely become dominant when
|
|
645 making buffer modifications in smaller buffers.
|
428
|
646
|
771
|
647 Note also that we have to multiply or divide by the char width in order
|
|
648 to convert the positions. We do some tricks to avoid ever actually
|
|
649 having to do a multiply or divide, because that is typically an
|
|
650 expensive operation (esp. divide). Multiplying or dividing by 1, 2, or
|
|
651 4 can be implemented simply as a shift left or shift right, and we keep
|
|
652 track of a shifter value (0, 1, or 2) indicating how much to shift.
|
|
653 Multiplying by 3 can be implemented by doubling and then adding the
|
|
654 original value. Dividing by 3, alas, cannot be implemented in any
|
|
655 simple shift/subtract method, as far as I know; so we just do a table
|
|
656 lookup. For simplicity, we use a table of size 128K, which indexes the
|
|
657 "divide-by-3" values for the first 64K non-negative numbers. (Note that
|
|
658 we can increase the size up to 384K, i.e. indexing the first 192K
|
|
659 non-negative numbers, while still using shorts in the array.) This also
|
|
660 means that the size of the known region can be at most 64K for
|
|
661 width-three characters.
|
|
662
|
|
663 !!#### We should investigate the algorithm in GNU Emacs. I think it
|
|
664 does something similar, but it may differ in some details, and it's
|
|
665 worth seeing if anything can be gleaned.
|
428
|
666 */
|
|
667
|
|
668 extern short three_to_one_table[];
|
|
669
|
771
|
670 INLINE_HEADER Bytebpos real_charbpos_to_bytebpos (struct buffer *buf, Charbpos x);
|
|
671 INLINE_HEADER Bytebpos
|
665
|
672 real_charbpos_to_bytebpos (struct buffer *buf, Charbpos x)
|
428
|
673 {
|
771
|
674 if (buf->text->entirely_ascii_p)
|
|
675 return (Bytebpos) x;
|
428
|
676 if (x >= buf->text->mule_bufmin && x <= buf->text->mule_bufmax)
|
|
677 return (buf->text->mule_bytmin +
|
|
678 ((x - buf->text->mule_bufmin) << buf->text->mule_shifter) +
|
|
679 (buf->text->mule_three_p ? (x - buf->text->mule_bufmin) : 0));
|
|
680 else
|
665
|
681 return charbpos_to_bytebpos_func (buf, x);
|
428
|
682 }
|
|
683
|
771
|
684 INLINE_HEADER Charbpos real_bytebpos_to_charbpos (struct buffer *buf, Bytebpos x);
|
|
685 INLINE_HEADER Charbpos
|
665
|
686 real_bytebpos_to_charbpos (struct buffer *buf, Bytebpos x)
|
428
|
687 {
|
771
|
688 if (buf->text->entirely_ascii_p)
|
|
689 return (Charbpos) x;
|
428
|
690 if (x >= buf->text->mule_bytmin && x <= buf->text->mule_bytmax)
|
|
691 return (buf->text->mule_bufmin +
|
|
692 ((buf->text->mule_three_p
|
|
693 ? three_to_one_table[x - buf->text->mule_bytmin]
|
|
694 : (x - buf->text->mule_bytmin) >> buf->text->mule_shifter)));
|
|
695 else
|
665
|
696 return bytebpos_to_charbpos_func (buf, x);
|
428
|
697 }
|
|
698
|
|
699 #else /* not MULE */
|
|
700
|
665
|
701 # define real_charbpos_to_bytebpos(buf, x) ((Bytebpos) x)
|
|
702 # define real_bytebpos_to_charbpos(buf, x) ((Charbpos) x)
|
428
|
703
|
|
704 #endif /* not MULE */
|
|
705
|
800
|
706 #ifdef ERROR_CHECK_TEXT
|
428
|
707
|
665
|
708 Bytebpos charbpos_to_bytebpos (struct buffer *buf, Charbpos x);
|
|
709 Charbpos bytebpos_to_charbpos (struct buffer *buf, Bytebpos x);
|
428
|
710
|
800
|
711 #else /* not ERROR_CHECK_TEXT */
|
428
|
712
|
665
|
713 #define charbpos_to_bytebpos real_charbpos_to_bytebpos
|
|
714 #define bytebpos_to_charbpos real_bytebpos_to_charbpos
|
428
|
715
|
800
|
716 #endif /* not ERROR_CHECK_TEXT */
|
428
|
717
|
665
|
718 #define make_charbpos(buf, ind) make_int (bytebpos_to_charbpos (buf, ind))
|
428
|
719
|
|
720 /*----------------------------------------------------------------------*/
|
|
721 /* Converting between buffer bytes and Emacs characters */
|
|
722 /*----------------------------------------------------------------------*/
|
|
723
|
|
724 /* The character at position POS in buffer. */
|
|
725 #define BI_BUF_FETCH_CHAR(buf, pos) \
|
|
726 charptr_emchar (BI_BUF_BYTE_ADDRESS (buf, pos))
|
|
727 #define BUF_FETCH_CHAR(buf, pos) \
|
665
|
728 BI_BUF_FETCH_CHAR (buf, charbpos_to_bytebpos (buf, pos))
|
428
|
729
|
|
730 /* The character at position POS in buffer, as a string. This is
|
|
731 equivalent to set_charptr_emchar (str, BUF_FETCH_CHAR (buf, pos))
|
|
732 but is faster for Mule. */
|
|
733
|
|
734 # define BI_BUF_CHARPTR_COPY_CHAR(buf, pos, str) \
|
|
735 charptr_copy_char (BI_BUF_BYTE_ADDRESS (buf, pos), str)
|
|
736 #define BUF_CHARPTR_COPY_CHAR(buf, pos, str) \
|
665
|
737 BI_BUF_CHARPTR_COPY_CHAR (buf, charbpos_to_bytebpos (buf, pos), str)
|
428
|
738
|
|
739
|
|
740 /************************************************************************/
|
440
|
741 /* */
|
428
|
742 /* higher-level buffer-position functions */
|
|
743 /* */
|
|
744 /************************************************************************/
|
|
745
|
|
746 /*----------------------------------------------------------------------*/
|
|
747 /* Settor macros for important positions in a buffer */
|
|
748 /*----------------------------------------------------------------------*/
|
|
749
|
|
750 /* Set beginning of accessible range of buffer. */
|
|
751 #define SET_BOTH_BUF_BEGV(buf, val, bival) \
|
|
752 do \
|
|
753 { \
|
|
754 (buf)->begv = (bival); \
|
|
755 (buf)->bufbegv = (val); \
|
|
756 } while (0)
|
|
757
|
|
758 /* Set end of accessible range of buffer. */
|
|
759 #define SET_BOTH_BUF_ZV(buf, val, bival) \
|
|
760 do \
|
|
761 { \
|
|
762 (buf)->zv = (bival); \
|
|
763 (buf)->bufzv = (val); \
|
|
764 } while (0)
|
|
765
|
|
766 /* Set point. */
|
|
767 /* Since BEGV and ZV are almost never set, it's reasonable to enforce
|
665
|
768 the restriction that the Charbpos and Bytebpos values must both be
|
428
|
769 specified. However, point is set in lots and lots of places. So
|
|
770 we provide the ability to specify both (for efficiency) or just
|
|
771 one. */
|
|
772 #define BOTH_BUF_SET_PT(buf, val, bival) set_buffer_point (buf, val, bival)
|
|
773 #define BI_BUF_SET_PT(buf, bival) \
|
665
|
774 BOTH_BUF_SET_PT (buf, bytebpos_to_charbpos (buf, bival), bival)
|
428
|
775 #define BUF_SET_PT(buf, value) \
|
665
|
776 BOTH_BUF_SET_PT (buf, value, charbpos_to_bytebpos (buf, value))
|
428
|
777
|
|
778
|
|
779 #if 0 /* FSFmacs */
|
|
780 /* These macros exist in FSFmacs because SET_PT() in FSFmacs incorrectly
|
|
781 does too much stuff, such as moving out of invisible extents. */
|
|
782 #define TEMP_SET_PT(position) (temp_set_point ((position), current_buffer))
|
|
783 #define SET_BUF_PT(buf, value) ((buf)->pt = (value))
|
|
784 #endif /* FSFmacs */
|
|
785
|
|
786 /*----------------------------------------------------------------------*/
|
|
787 /* Miscellaneous buffer values */
|
|
788 /*----------------------------------------------------------------------*/
|
|
789
|
|
790 /* Number of characters in buffer */
|
|
791 #define BUF_SIZE(buf) (BUF_Z (buf) - BUF_BEG (buf))
|
|
792
|
|
793 /* Is this buffer narrowed? */
|
|
794 #define BUF_NARROWED(buf) \
|
|
795 ((BI_BUF_BEGV (buf) != BI_BUF_BEG (buf)) || \
|
|
796 (BI_BUF_ZV (buf) != BI_BUF_Z (buf)))
|
|
797
|
|
798 /* Modification count. */
|
|
799 #define BUF_MODIFF(buf) ((buf)->text->modiff)
|
|
800
|
|
801 /* Saved modification count. */
|
|
802 #define BUF_SAVE_MODIFF(buf) ((buf)->text->save_modiff)
|
|
803
|
|
804 /* Face changed. */
|
|
805 #define BUF_FACECHANGE(buf) ((buf)->face_change)
|
|
806
|
|
807 #define POINT_MARKER_P(marker) \
|
|
808 (XMARKER (marker)->buffer != 0 && \
|
434
|
809 EQ (marker, XMARKER (marker)->buffer->point_marker))
|
428
|
810
|
|
811 #define BUF_MARKERS(buf) ((buf)->markers)
|
|
812
|
|
813 /* WARNING:
|
|
814
|
|
815 The new definitions of CEILING_OF() and FLOOR_OF() differ semantically
|
|
816 from the old ones (in FSF Emacs and XEmacs 19.11 and before).
|
|
817 Conversion is as follows:
|
|
818
|
|
819 OLD_BI_CEILING_OF(n) = NEW_BI_CEILING_OF(n) - 1
|
|
820 OLD_BI_FLOOR_OF(n) = NEW_BI_FLOOR_OF(n + 1)
|
|
821
|
|
822 The definitions were changed because the new definitions are more
|
771
|
823 consistent with the way everything else works in XEmacs.
|
428
|
824 */
|
|
825
|
|
826 /* Properties of CEILING_OF and FLOOR_OF (also apply to BI_ variants):
|
|
827
|
|
828 1) FLOOR_OF (CEILING_OF (n)) = n
|
|
829 CEILING_OF (FLOOR_OF (n)) = n
|
|
830
|
|
831 2) CEILING_OF (n) = n if and only if n = ZV
|
|
832 FLOOR_OF (n) = n if and only if n = BEGV
|
|
833
|
|
834 3) CEILING_OF (CEILING_OF (n)) = ZV
|
|
835 FLOOR_OF (FLOOR_OF (n)) = BEGV
|
|
836
|
|
837 4) The bytes in the regions
|
|
838
|
|
839 [BYTE_ADDRESS (n), BYTE_ADDRESS_BEFORE (CEILING_OF (n))]
|
|
840
|
|
841 and
|
|
842
|
|
843 [BYTE_ADDRESS (FLOOR_OF (n)), BYTE_ADDRESS_BEFORE (n)]
|
|
844
|
|
845 are contiguous.
|
771
|
846
|
|
847 A typical loop using CEILING_OF to process contiguous ranges of text
|
|
848 between [from, to) looks like this:
|
|
849
|
|
850 {
|
|
851 Bytebpos pos = from;
|
|
852
|
|
853 while (pos < to)
|
|
854 {
|
|
855 Bytebpos ceil;
|
|
856
|
|
857 ceil = BI_BUF_CEILING_OF (buf, pos);
|
|
858 ceil = min (to, ceil);
|
|
859 process_intbyte_string (BI_BUF_BYTE_ADDRESS (buf, pos), ceil - pos);
|
|
860 pos = ceil;
|
|
861 }
|
|
862 }
|
|
863
|
|
864 Currently there will be at most two iterations in the loop, but it is
|
|
865 written in such a way that it will still work if the buffer
|
|
866 representation is changed to have multiple gaps in it.
|
|
867 */
|
428
|
868
|
|
869
|
|
870 /* Return the maximum index in the buffer it is safe to scan forwards
|
|
871 past N to. This is used to prevent buffer scans from running into
|
|
872 the gap (e.g. search.c). All characters between N and CEILING_OF(N)
|
|
873 are located contiguous in memory. Note that the character *at*
|
|
874 CEILING_OF(N) is not contiguous in memory. */
|
|
875 #define BI_BUF_CEILING_OF(b, n) \
|
|
876 ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_ZV (b) ? \
|
|
877 (b)->text->gpt : BI_BUF_ZV (b))
|
|
878 #define BUF_CEILING_OF(b, n) \
|
665
|
879 bytebpos_to_charbpos (b, BI_BUF_CEILING_OF (b, charbpos_to_bytebpos (b, n)))
|
428
|
880
|
|
881 /* Return the minimum index in the buffer it is safe to scan backwards
|
|
882 past N to. All characters between FLOOR_OF(N) and N are located
|
|
883 contiguous in memory. Note that the character *at* N may not be
|
|
884 contiguous in memory. */
|
|
885 #define BI_BUF_FLOOR_OF(b, n) \
|
|
886 (BI_BUF_BEGV (b) < (b)->text->gpt && (b)->text->gpt < (n) ? \
|
|
887 (b)->text->gpt : BI_BUF_BEGV (b))
|
|
888 #define BUF_FLOOR_OF(b, n) \
|
665
|
889 bytebpos_to_charbpos (b, BI_BUF_FLOOR_OF (b, charbpos_to_bytebpos (b, n)))
|
428
|
890
|
|
891 #define BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \
|
|
892 ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_Z (b) ? \
|
|
893 (b)->text->gpt : BI_BUF_Z (b))
|
|
894 #define BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \
|
665
|
895 bytebpos_to_charbpos \
|
|
896 (b, BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE (b, charbpos_to_bytebpos (b, n)))
|
428
|
897
|
|
898 #define BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \
|
|
899 (BI_BUF_BEG (b) < (b)->text->gpt && (b)->text->gpt < (n) ? \
|
|
900 (b)->text->gpt : BI_BUF_BEG (b))
|
|
901 #define BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \
|
665
|
902 bytebpos_to_charbpos \
|
|
903 (b, BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE (b, charbpos_to_bytebpos (b, n)))
|
428
|
904
|
|
905 /* This structure marks which slots in a buffer have corresponding
|
|
906 default values in Vbuffer_defaults.
|
|
907 Each such slot has a nonzero value in this structure.
|
|
908 The value has only one nonzero bit.
|
|
909
|
|
910 When a buffer has its own local value for a slot,
|
|
911 the bit for that slot (found in the same slot in this structure)
|
|
912 is turned on in the buffer's local_var_flags slot.
|
|
913
|
|
914 If a slot in this structure is zero, then even though there may
|
|
915 be a DEFVAR_BUFFER_LOCAL for the slot, there is no default value for it;
|
|
916 and the corresponding slot in Vbuffer_defaults is not used. */
|
|
917
|
|
918 extern struct buffer buffer_local_flags;
|
|
919
|
|
920
|
|
921 /* Allocation of buffer data. */
|
|
922
|
|
923 #ifdef REL_ALLOC
|
|
924
|
440
|
925 char *r_alloc (unsigned char **, size_t);
|
|
926 char *r_re_alloc (unsigned char **, size_t);
|
428
|
927 void r_alloc_free (unsigned char **);
|
|
928
|
|
929 #define BUFFER_ALLOC(data, size) \
|
665
|
930 ((Intbyte *) r_alloc ((unsigned char **) &data, (size) * sizeof(Intbyte)))
|
428
|
931 #define BUFFER_REALLOC(data, size) \
|
665
|
932 ((Intbyte *) r_re_alloc ((unsigned char **) &data, (size) * sizeof(Intbyte)))
|
428
|
933 #define BUFFER_FREE(data) r_alloc_free ((unsigned char **) &(data))
|
|
934 #define R_ALLOC_DECLARE(var,data) r_alloc_declare (&(var), data)
|
|
935
|
|
936 #else /* !REL_ALLOC */
|
|
937
|
|
938 #define BUFFER_ALLOC(data,size)\
|
665
|
939 (data = xnew_array (Intbyte, size))
|
428
|
940 #define BUFFER_REALLOC(data,size)\
|
665
|
941 ((Intbyte *) xrealloc (data, (size) * sizeof(Intbyte)))
|
428
|
942 /* Avoid excess parentheses, or syntax errors may rear their heads. */
|
|
943 #define BUFFER_FREE(data) xfree (data)
|
|
944 #define R_ALLOC_DECLARE(var,data)
|
|
945
|
|
946 #endif /* !REL_ALLOC */
|
|
947
|
|
948
|
|
949 /************************************************************************/
|
|
950 /* Case conversion */
|
|
951 /************************************************************************/
|
|
952
|
|
953 /* A "trt" table is a mapping from characters to other characters,
|
|
954 typically used to convert between uppercase and lowercase. For
|
|
955 compatibility reasons, trt tables are currently in the form of
|
|
956 a Lisp string of 256 characters, specifying the conversion for each
|
|
957 of the first 256 Emacs characters (i.e. the 256 Latin-1 characters).
|
|
958 This should be generalized at some point to support conversions for
|
|
959 all of the allowable Mule characters.
|
|
960 */
|
|
961
|
|
962 /* The _1 macros are named as such because they assume that you have
|
|
963 already guaranteed that the character values are all in the range
|
|
964 0 - 255. Bad lossage will happen otherwise. */
|
|
965
|
446
|
966 #define MAKE_TRT_TABLE() Fmake_char_table (Qgeneric)
|
|
967 INLINE_HEADER Emchar TRT_TABLE_CHAR_1 (Lisp_Object table, Emchar c);
|
|
968 INLINE_HEADER Emchar
|
|
969 TRT_TABLE_CHAR_1 (Lisp_Object table, Emchar ch)
|
|
970 {
|
|
971 Lisp_Object TRT_char;
|
|
972 TRT_char = get_char_table (ch, XCHAR_TABLE (table));
|
|
973 if (NILP (TRT_char))
|
|
974 return ch;
|
|
975 else
|
|
976 return XCHAR (TRT_char);
|
|
977 }
|
|
978 #define SET_TRT_TABLE_CHAR_1(table, ch1, ch2) \
|
|
979 Fput_char_table (make_char (ch1), make_char (ch2), table);
|
428
|
980
|
442
|
981 INLINE_HEADER Emchar TRT_TABLE_OF (Lisp_Object trt, Emchar c);
|
|
982 INLINE_HEADER Emchar
|
428
|
983 TRT_TABLE_OF (Lisp_Object trt, Emchar c)
|
|
984 {
|
446
|
985 return TRT_TABLE_CHAR_1 (trt, c);
|
428
|
986 }
|
|
987
|
771
|
988 INLINE_HEADER Lisp_Object BUFFER_CASE_TABLE (struct buffer *buf);
|
|
989 INLINE_HEADER Lisp_Object
|
|
990 BUFFER_CASE_TABLE (struct buffer *buf)
|
|
991 {
|
|
992 return buf ? buf->case_table : Vstandard_case_table;
|
|
993 }
|
|
994
|
428
|
995 /* Macros used below. */
|
446
|
996 #define DOWNCASE_TABLE_OF(buf, c) \
|
771
|
997 TRT_TABLE_OF (XCASE_TABLE_DOWNCASE (BUFFER_CASE_TABLE (buf)), c)
|
446
|
998 #define UPCASE_TABLE_OF(buf, c) \
|
771
|
999 TRT_TABLE_OF (XCASE_TABLE_UPCASE (BUFFER_CASE_TABLE (buf)), c)
|
428
|
1000
|
|
1001 /* 1 if CH is upper case. */
|
|
1002
|
442
|
1003 INLINE_HEADER int UPPERCASEP (struct buffer *buf, Emchar ch);
|
|
1004 INLINE_HEADER int
|
428
|
1005 UPPERCASEP (struct buffer *buf, Emchar ch)
|
|
1006 {
|
|
1007 return DOWNCASE_TABLE_OF (buf, ch) != ch;
|
|
1008 }
|
|
1009
|
|
1010 /* 1 if CH is lower case. */
|
|
1011
|
442
|
1012 INLINE_HEADER int LOWERCASEP (struct buffer *buf, Emchar ch);
|
|
1013 INLINE_HEADER int
|
428
|
1014 LOWERCASEP (struct buffer *buf, Emchar ch)
|
|
1015 {
|
|
1016 return (UPCASE_TABLE_OF (buf, ch) != ch &&
|
|
1017 DOWNCASE_TABLE_OF (buf, ch) == ch);
|
|
1018 }
|
|
1019
|
|
1020 /* 1 if CH is neither upper nor lower case. */
|
|
1021
|
442
|
1022 INLINE_HEADER int NOCASEP (struct buffer *buf, Emchar ch);
|
|
1023 INLINE_HEADER int
|
428
|
1024 NOCASEP (struct buffer *buf, Emchar ch)
|
|
1025 {
|
|
1026 return UPCASE_TABLE_OF (buf, ch) == ch;
|
|
1027 }
|
|
1028
|
|
1029 /* Upcase a character, or make no change if that cannot be done. */
|
|
1030
|
442
|
1031 INLINE_HEADER Emchar UPCASE (struct buffer *buf, Emchar ch);
|
|
1032 INLINE_HEADER Emchar
|
428
|
1033 UPCASE (struct buffer *buf, Emchar ch)
|
|
1034 {
|
|
1035 return (DOWNCASE_TABLE_OF (buf, ch) == ch) ? UPCASE_TABLE_OF (buf, ch) : ch;
|
|
1036 }
|
|
1037
|
|
1038 /* Upcase a character known to be not upper case. Unused. */
|
|
1039
|
|
1040 #define UPCASE1(buf, ch) UPCASE_TABLE_OF (buf, ch)
|
|
1041
|
|
1042 /* Downcase a character, or make no change if that cannot be done. */
|
|
1043
|
|
1044 #define DOWNCASE(buf, ch) DOWNCASE_TABLE_OF (buf, ch)
|
|
1045
|
440
|
1046 #endif /* INCLUDED_buffer_h_ */
|