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.
6 This file is part of XEmacs.
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Synched up with: FSF 19.30. */
28 JWZ: separated out bufslots.h, early in Lemacs.
29 Ben Wing: almost completely rewritten for Mule, 19.12.
32 #ifndef INCLUDED_buffer_h_
33 #define INCLUDED_buffer_h_
35 #include "character.h"
36 #include "multibyte.h"
38 /************************************************************************/
40 /* definition of Lisp buffer object */
42 /************************************************************************/
44 /* Note: we keep both Bytind and Bufpos versions of some of the
45 important buffer positions because they are accessed so much.
46 If we didn't do this, we would constantly be invalidating the
47 bufpos<->bytind cache under Mule.
49 Note that under non-Mule, both versions will always be the
50 same so we don't really need to keep track of them. But it
51 simplifies the logic to go ahead and do so all the time and
52 the memory loss is insignificant. */
54 /* Formerly, it didn't much matter what went inside the struct buffer_text
55 and what went outside it. Now it does, with the advent of "indirect
56 buffers" that share text with another buffer. An indirect buffer
57 shares the same *text* as another buffer, but has its own buffer-local
58 variables, its own accessible region, and its own markers and extents.
59 (Due to the nature of markers, it doesn't actually matter much whether
60 we stick them inside or out of the struct buffer_text -- the user won't
61 notice any difference -- but we go ahead and put them outside for
62 consistency and overall saneness of algorithm.)
64 FSFmacs gets away with not maintaining any "children" pointers from
65 a buffer to the indirect buffers that refer to it by putting the
66 markers inside of the struct buffer_text, using markers to keep track
67 of BEGV and ZV in indirect buffers, and relying on the fact that
68 all intervals (text properties and overlays) use markers for their
69 start and end points. We don't do this for extents (markers are
70 inefficient anyway and take up space), so we have to maintain
71 children pointers. This is not terribly hard, though, and the
72 code to maintain this is just like the code already present in
73 extent-parent and extent-children.
78 Bufbyte *beg; /* Actual address of buffer contents. */
79 Bytind gpt; /* Index of gap in buffer. */
80 Bytind z; /* Index of end of buffer. */
81 Bufpos bufz; /* Equivalent as a Bufpos. */
82 int gap_size; /* Size of buffer's gap */
83 int end_gap_size; /* Size of buffer's end gap */
84 long modiff; /* This counts buffer-modification events
85 for this buffer. It is incremented for
86 each such event, and never otherwise
88 long save_modiff; /* Previous value of modiff, as of last
89 time buffer visited or saved a file. */
92 /* We keep track of a "known" region for very fast access.
93 This information is text-only so it goes here. */
94 Bufpos mule_bufmin, mule_bufmax;
95 Bytind mule_bytmin, mule_bytmax;
99 int mule_shifter, mule_three_p;
102 /* And we also cache 16 positions for fairly fast access near those
104 Bufpos mule_bufpos_cache[16];
105 Bytind mule_bytind_cache[16];
108 /* Similar to the above, we keep track of positions for which line
109 number has last been calculated. See line-number.c. */
110 Lisp_Object line_number_cache;
112 /* Change data that goes with the text. */
113 struct buffer_text_change_data *changes;
119 struct lcrecord_header header;
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;
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;
130 Bytind pt; /* Position of point in buffer. */
131 Bufpos bufpt; /* Equivalent as a Bufpos. */
132 Bytind begv; /* Index of beginning of accessible range. */
133 Bufpos bufbegv; /* Equivalent as a Bufpos. */
134 Bytind zv; /* Index of end of accessible range. */
135 Bufpos bufzv; /* Equivalent as a Bufpos. */
137 int face_change; /* This is set when a change in how the text should
138 be displayed (e.g., font, color) is made. */
140 /* change data indicating what portion of the text has changed
141 since the last time this was reset. Used by redisplay.
142 Logically we should keep this with the text structure, but
143 redisplay resets it for each buffer individually and we don't
144 want interference between an indirect buffer and its base
146 struct each_buffer_change_data *changes;
148 #ifdef REGION_CACHE_NEEDS_WORK
149 /* If the long line scan cache is enabled (i.e. the buffer-local
150 variable cache-long-line-scans is non-nil), newline_cache
151 points to the newline cache, and width_run_cache points to the
154 The newline cache records which stretches of the buffer are
155 known *not* to contain newlines, so that they can be skipped
156 quickly when we search for newlines.
158 The width run cache records which stretches of the buffer are
159 known to contain characters whose widths are all the same. If
160 the width run cache maps a character to a value > 0, that value
161 is the character's width; if it maps a character to zero, we
162 don't know what its width is. This allows compute_motion to
163 process such regions very quickly, using algebra instead of
164 inspecting each character. See also width_table, below. */
165 struct region_cache *newline_cache;
166 struct region_cache *width_run_cache;
167 #endif /* REGION_CACHE_NEEDS_WORK */
169 /* The markers that refer to this buffer. This is actually a single
170 marker -- successive elements in its marker `chain' are the other
171 markers referring to this buffer */
172 Lisp_Marker *markers;
174 /* The buffer's extent info. This is its own type, an extent-info
175 object (done this way for ease in marking / finalizing). */
176 Lisp_Object extent_info;
178 /* ----------------------------------------------------------------- */
179 /* All the stuff above this line is the responsibility of insdel.c,
180 with some help from marker.c and extents.c.
181 All the stuff below this line is the responsibility of buffer.c. */
183 /* In an indirect buffer, this points to the base buffer.
184 In an ordinary buffer, it is 0.
185 We DO mark through this slot. */
186 struct buffer *base_buffer;
188 /* List of indirect buffers whose base is this buffer.
189 If we are an indirect buffer, this will be nil.
190 Do NOT mark through this. */
191 Lisp_Object indirect_children;
193 /* Flags saying which DEFVAR_PER_BUFFER variables
194 are local to this buffer. */
197 /* Set to the modtime of the visited file when read or written.
198 -1 means visited file was nonexistent.
199 0 means visited file modtime unknown; in no case complain
200 about any mismatch on next save attempt. */
203 /* the value of text->modiff at the last auto-save. */
204 long auto_save_modified;
206 /* The time at which we detected a failure to auto-save,
207 Or -1 if we didn't have a failure. */
208 int auto_save_failure_time;
210 /* Position in buffer at which display started
211 the last time this buffer was displayed. */
212 int last_window_start;
214 /* Everything from here down must be a Lisp_Object */
216 #define MARKED_SLOT(x) Lisp_Object x
217 #include "bufslots.h"
221 DECLARE_LRECORD (buffer, struct buffer);
222 #define XBUFFER(x) XRECORD (x, buffer, struct buffer)
223 #define XSETBUFFER(x, p) XSETRECORD (x, p, buffer)
224 #define BUFFERP(x) RECORDP (x, buffer)
225 #define CHECK_BUFFER(x) CHECK_RECORD (x, buffer)
226 #define CONCHECK_BUFFER(x) CONCHECK_RECORD (x, buffer)
228 #define BUFFER_LIVE_P(b) (!NILP ((b)->name))
230 #define CHECK_LIVE_BUFFER(x) do { \
232 if (!BUFFER_LIVE_P (XBUFFER (x))) \
233 dead_wrong_type_argument (Qbuffer_live_p, (x)); \
236 #define CONCHECK_LIVE_BUFFER(x) do { \
237 CONCHECK_BUFFER (x); \
238 if (!BUFFER_LIVE_P (XBUFFER (x))) \
239 x = wrong_type_argument (Qbuffer_live_p, (x)); \
243 #define BUFFER_BASE_BUFFER(b) ((b)->base_buffer ? (b)->base_buffer : (b))
245 /* Map over buffers sharing the same text as MPS_BUF. MPS_BUFVAR is a
246 variable that gets the buffer values (beginning with the base
247 buffer, then the children), and MPS_BUFCONS should be a temporary
248 Lisp_Object variable. */
249 #define MAP_INDIRECT_BUFFERS(mps_buf, mps_bufvar, mps_bufcons) \
250 for (mps_bufcons = Qunbound, \
251 mps_bufvar = BUFFER_BASE_BUFFER (mps_buf); \
252 UNBOUNDP (mps_bufcons) ? \
253 (mps_bufcons = mps_bufvar->indirect_children, \
255 : (!NILP (mps_bufcons) \
256 && (mps_bufvar = XBUFFER (XCAR (mps_bufcons)), 1) \
257 && (mps_bufcons = XCDR (mps_bufcons), 1)); \
262 /************************************************************************/
264 /* working with raw internal-format data */
266 /************************************************************************/
268 /* NOTE: In all the following macros, we follow these rules concerning
269 multiple evaluation of the arguments:
271 1) Anything that's an lvalue can be evaluated more than once.
272 2) Anything that's a Lisp Object can be evaluated more than once.
273 This should probably be changed, but this follows the way
274 that all the macros in lisp.h do things.
275 3) 'struct buffer *' arguments can be evaluated more than once.
276 4) Nothing else can be evaluated more than once. Use inline
277 functions, if necessary, to prevent multiple evaluation.
278 5) An exception to (4) is that there are some macros below that
279 may evaluate their arguments more than once. They are all
280 denoted with the word "unsafe" in their name and are generally
281 meant to be called only by other macros that have already
282 stored the calling values in temporary variables.
286 /*----------------------------------------------------------------------*/
287 /* Accessor macros for important positions in a buffer */
288 /*----------------------------------------------------------------------*/
290 /* We put them here because some stuff below wants them before the
291 place where we would normally put them. */
293 /* None of these are lvalues. Use the settor macros below to change
296 /* Beginning of buffer. */
297 #define BI_BUF_BEG(buf) ((Bytind) 1)
298 #define BUF_BEG(buf) ((Bufpos) 1)
300 /* Beginning of accessible range of buffer. */
301 #define BI_BUF_BEGV(buf) ((buf)->begv + 0)
302 #define BUF_BEGV(buf) ((buf)->bufbegv + 0)
304 /* End of accessible range of buffer. */
305 #define BI_BUF_ZV(buf) ((buf)->zv + 0)
306 #define BUF_ZV(buf) ((buf)->bufzv + 0)
309 #define BI_BUF_Z(buf) ((buf)->text->z + 0)
310 #define BUF_Z(buf) ((buf)->text->bufz + 0)
313 #define BI_BUF_PT(buf) ((buf)->pt + 0)
314 #define BUF_PT(buf) ((buf)->bufpt + 0)
316 /*----------------------------------------------------------------------*/
317 /* Converting between positions and addresses */
318 /*----------------------------------------------------------------------*/
320 /* Convert the address of a byte in the buffer into a position. */
321 INLINE_HEADER Bytind BI_BUF_PTR_BYTE_POS (struct buffer *buf, Bufbyte *ptr);
323 BI_BUF_PTR_BYTE_POS (struct buffer *buf, Bufbyte *ptr)
325 return (ptr - buf->text->beg + 1
326 - ((ptr - buf->text->beg + 1) > buf->text->gpt
327 ? buf->text->gap_size : 0));
330 #define BUF_PTR_BYTE_POS(buf, ptr) \
331 bytind_to_bufpos (buf, BI_BUF_PTR_BYTE_POS (buf, ptr))
333 /* Address of byte at position POS in buffer. */
334 INLINE_HEADER Bufbyte * BI_BUF_BYTE_ADDRESS (struct buffer *buf, Bytind pos);
335 INLINE_HEADER Bufbyte *
336 BI_BUF_BYTE_ADDRESS (struct buffer *buf, Bytind pos)
338 return (buf->text->beg +
339 ((pos >= buf->text->gpt ? (pos + buf->text->gap_size) : pos)
343 #define BUF_BYTE_ADDRESS(buf, pos) \
344 BI_BUF_BYTE_ADDRESS (buf, bufpos_to_bytind (buf, pos))
346 /* Address of byte before position POS in buffer. */
347 INLINE_HEADER Bufbyte * BI_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytind pos);
348 INLINE_HEADER Bufbyte *
349 BI_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytind pos)
351 return (buf->text->beg +
352 ((pos > buf->text->gpt ? (pos + buf->text->gap_size) : pos)
356 #define BUF_BYTE_ADDRESS_BEFORE(buf, pos) \
357 BI_BUF_BYTE_ADDRESS_BEFORE (buf, bufpos_to_bytind (buf, pos))
359 /*----------------------------------------------------------------------*/
360 /* Converting between byte indices and memory indices */
361 /*----------------------------------------------------------------------*/
363 INLINE_HEADER int valid_memind_p (struct buffer *buf, Memind x);
365 valid_memind_p (struct buffer *buf, Memind x)
367 return ((x >= 1 && x <= (Memind) buf->text->gpt) ||
368 (x > (Memind) (buf->text->gpt + buf->text->gap_size) &&
369 x <= (Memind) (buf->text->z + buf->text->gap_size)));
372 INLINE_HEADER Memind bytind_to_memind (struct buffer *buf, Bytind x);
374 bytind_to_memind (struct buffer *buf, Bytind x)
376 return (Memind) ((x > buf->text->gpt) ? (x + buf->text->gap_size) : x);
380 INLINE_HEADER Bytind memind_to_bytind (struct buffer *buf, Memind x);
382 memind_to_bytind (struct buffer *buf, Memind x)
384 #ifdef ERROR_CHECK_BUFPOS
385 assert (valid_memind_p (buf, x));
387 return (Bytind) ((x > (Memind) buf->text->gpt) ?
388 x - buf->text->gap_size :
392 #define memind_to_bufpos(buf, x) \
393 bytind_to_bufpos (buf, memind_to_bytind (buf, x))
394 #define bufpos_to_memind(buf, x) \
395 bytind_to_memind (buf, bufpos_to_bytind (buf, x))
397 /* These macros generalize many standard buffer-position functions to
398 either a buffer or a string. */
400 /* Converting between Meminds and Bytinds, for a buffer-or-string.
401 For strings, this is a no-op. For buffers, this resolves
402 to the standard memind<->bytind converters. */
404 #define buffer_or_string_bytind_to_memind(obj, ind) \
405 (BUFFERP (obj) ? bytind_to_memind (XBUFFER (obj), ind) : (Memind) ind)
407 #define buffer_or_string_memind_to_bytind(obj, ind) \
408 (BUFFERP (obj) ? memind_to_bytind (XBUFFER (obj), ind) : (Bytind) ind)
410 /* Converting between Bufpos's and Bytinds, for a buffer-or-string.
411 For strings, this maps to the bytecount<->charcount converters. */
413 #define buffer_or_string_bufpos_to_bytind(obj, pos) \
414 (BUFFERP (obj) ? bufpos_to_bytind (XBUFFER (obj), pos) : \
415 (Bytind) charcount_to_bytecount (XSTRING_DATA (obj), pos))
417 #define buffer_or_string_bytind_to_bufpos(obj, ind) \
418 (BUFFERP (obj) ? bytind_to_bufpos (XBUFFER (obj), ind) : \
419 (Bufpos) bytecount_to_charcount (XSTRING_DATA (obj), ind))
421 /* Similar for Bufpos's and Meminds. */
423 #define buffer_or_string_bufpos_to_memind(obj, pos) \
424 (BUFFERP (obj) ? bufpos_to_memind (XBUFFER (obj), pos) : \
425 (Memind) charcount_to_bytecount (XSTRING_DATA (obj), pos))
427 #define buffer_or_string_memind_to_bufpos(obj, ind) \
428 (BUFFERP (obj) ? memind_to_bufpos (XBUFFER (obj), ind) : \
429 (Bufpos) bytecount_to_charcount (XSTRING_DATA (obj), ind))
431 /************************************************************************/
433 /* working with buffer-level data */
435 /************************************************************************/
439 (A) Working with byte indices:
440 ------------------------------
442 VALID_BYTIND_P(buf, bi):
443 Given a byte index, does it point to the beginning of a character?
445 ASSERT_VALID_BYTIND_UNSAFE(buf, bi):
446 If error-checking is enabled, assert that the given byte index
447 is within range and points to the beginning of a character
448 or to the end of the buffer. Otherwise, do nothing.
450 ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, bi):
451 If error-checking is enabled, assert that the given byte index
452 is within range and satisfies ASSERT_VALID_BYTIND() and also
453 does not refer to the beginning of the buffer. (i.e. movement
454 backwards is OK.) Otherwise, do nothing.
456 ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, bi):
457 If error-checking is enabled, assert that the given byte index
458 is within range and satisfies ASSERT_VALID_BYTIND() and also
459 does not refer to the end of the buffer. (i.e. movement
460 forwards is OK.) Otherwise, do nothing.
462 VALIDATE_BYTIND_BACKWARD(buf, bi):
463 Make sure that the given byte index is pointing to the beginning
464 of a character. If not, back up until this is the case. Note
465 that there are not too many places where it is legitimate to do
466 this sort of thing. It's an error if you're passed an "invalid"
469 VALIDATE_BYTIND_FORWARD(buf, bi):
470 Make sure that the given byte index is pointing to the beginning
471 of a character. If not, move forward until this is the case.
472 Note that there are not too many places where it is legitimate
473 to do this sort of thing. It's an error if you're passed an
474 "invalid" byte index.
477 Given a byte index (assumed to point at the beginning of a
478 character), modify that value so it points to the beginning
479 of the next character.
482 Given a byte index (assumed to point at the beginning of a
483 character), modify that value so it points to the beginning
484 of the previous character. Unlike for DEC_CHARPTR(), we can
485 do all the assert()s because there are sentinels at the
486 beginning of the gap and the end of the buffer.
489 A constant representing an invalid Bytind. Valid Bytinds
490 can never have this value.
493 (B) Converting between Bufpos's and Bytinds:
494 --------------------------------------------
496 bufpos_to_bytind(buf, bu):
497 Given a Bufpos, return the equivalent Bytind.
499 bytind_to_bufpos(buf, bi):
500 Given a Bytind, return the equivalent Bufpos.
502 make_bufpos(buf, bi):
503 Given a Bytind, return the equivalent Bufpos as a Lisp Object.
507 /*----------------------------------------------------------------------*/
508 /* working with byte indices */
509 /*----------------------------------------------------------------------*/
512 # define VALID_BYTIND_P(buf, x) \
513 BUFBYTE_FIRST_BYTE_P (*BI_BUF_BYTE_ADDRESS (buf, x))
515 # define VALID_BYTIND_P(buf, x) 1
518 #ifdef ERROR_CHECK_BUFPOS
520 # define ASSERT_VALID_BYTIND_UNSAFE(buf, x) do { \
521 assert (BUFFER_LIVE_P (buf)); \
522 assert ((x) >= BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf)); \
523 assert (VALID_BYTIND_P (buf, x)); \
525 # define ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, x) do { \
526 assert (BUFFER_LIVE_P (buf)); \
527 assert ((x) > BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf)); \
528 assert (VALID_BYTIND_P (buf, x)); \
530 # define ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, x) do { \
531 assert (BUFFER_LIVE_P (buf)); \
532 assert ((x) >= BI_BUF_BEG (buf) && x < BI_BUF_Z (buf)); \
533 assert (VALID_BYTIND_P (buf, x)); \
536 #else /* not ERROR_CHECK_BUFPOS */
537 # define ASSERT_VALID_BYTIND_UNSAFE(buf, x)
538 # define ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, x)
539 # define ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, x)
541 #endif /* not ERROR_CHECK_BUFPOS */
543 /* Note that, although the Mule version will work fine for non-Mule
544 as well (it should reduce down to nothing), we provide a separate
545 version to avoid compilation warnings and possible non-optimal
546 results with stupid compilers. */
549 # define VALIDATE_BYTIND_BACKWARD(buf, x) do { \
550 Bufbyte *VBB_ptr = BI_BUF_BYTE_ADDRESS (buf, x); \
551 while (!BUFBYTE_FIRST_BYTE_P (*VBB_ptr)) \
555 # define VALIDATE_BYTIND_BACKWARD(buf, x)
558 /* Note that, although the Mule version will work fine for non-Mule
559 as well (it should reduce down to nothing), we provide a separate
560 version to avoid compilation warnings and possible non-optimal
561 results with stupid compilers. */
564 # define VALIDATE_BYTIND_FORWARD(buf, x) do { \
565 Bufbyte *VBF_ptr = BI_BUF_BYTE_ADDRESS (buf, x); \
566 while (!BUFBYTE_FIRST_BYTE_P (*VBF_ptr)) \
570 # define VALIDATE_BYTIND_FORWARD(buf, x)
573 /* Note that in the simplest case (no MULE, no ERROR_CHECK_BUFPOS),
574 this crap reduces down to simply (x)++. */
576 #define INC_BYTIND(buf, x) do \
578 ASSERT_VALID_BYTIND_FORWARD_UNSAFE (buf, x); \
579 /* Note that we do the increment first to \
580 make sure that the pointer in \
581 VALIDATE_BYTIND_FORWARD() ends up on \
582 the correct side of the gap */ \
584 VALIDATE_BYTIND_FORWARD (buf, x); \
587 /* Note that in the simplest case (no MULE, no ERROR_CHECK_BUFPOS),
588 this crap reduces down to simply (x)--. */
590 #define DEC_BYTIND(buf, x) do \
592 ASSERT_VALID_BYTIND_BACKWARD_UNSAFE (buf, x); \
593 /* Note that we do the decrement first to \
594 make sure that the pointer in \
595 VALIDATE_BYTIND_BACKWARD() ends up on \
596 the correct side of the gap */ \
598 VALIDATE_BYTIND_BACKWARD (buf, x); \
601 INLINE_HEADER Bytind prev_bytind (struct buffer *buf, Bytind x);
603 prev_bytind (struct buffer *buf, Bytind x)
609 INLINE_HEADER Bytind next_bytind (struct buffer *buf, Bytind x);
611 next_bytind (struct buffer *buf, Bytind x)
617 #define BYTIND_INVALID ((Bytind) -1)
619 /*----------------------------------------------------------------------*/
620 /* Converting between buffer positions and byte indices */
621 /*----------------------------------------------------------------------*/
625 Bytind bufpos_to_bytind_func (struct buffer *buf, Bufpos x);
626 Bufpos bytind_to_bufpos_func (struct buffer *buf, Bytind x);
628 /* The basic algorithm we use is to keep track of a known region of
629 characters in each buffer, all of which are of the same width. We
630 keep track of the boundaries of the region in both Bufpos and
631 Bytind coordinates and also keep track of the char width, which
632 is 1 - 4 bytes. If the position we're translating is not in
633 the known region, then we invoke a function to update the known
634 region to surround the position in question. This assumes
635 locality of reference, which is usually the case.
637 Note that the function to update the known region can be simple
638 or complicated depending on how much information we cache.
639 For the moment, we don't cache any information, and just move
640 linearly forward or back from the known region, with a few
641 shortcuts to catch all-ASCII buffers. (Note that this will
642 thrash with bad locality of reference.) A smarter method would
643 be to keep some sort of pseudo-extent layer over the buffer;
644 maybe keep track of the bufpos/bytind correspondence at the
645 beginning of each line, which would allow us to do a binary
646 search over the pseudo-extents to narrow things down to the
647 correct line, at which point you could use a linear movement
648 method. This would also mesh well with efficiently
649 implementing a line-numbering scheme.
651 Note also that we have to multiply or divide by the char width
652 in order to convert the positions. We do some tricks to avoid
653 ever actually having to do a multiply or divide, because that
654 is typically an expensive operation (esp. divide). Multiplying
655 or dividing by 1, 2, or 4 can be implemented simply as a
656 shift left or shift right, and we keep track of a shifter value
657 (0, 1, or 2) indicating how much to shift. Multiplying by 3
658 can be implemented by doubling and then adding the original
659 value. Dividing by 3, alas, cannot be implemented in any
660 simple shift/subtract method, as far as I know; so we just
661 do a table lookup. For simplicity, we use a table of size
662 128K, which indexes the "divide-by-3" values for the first
663 64K non-negative numbers. (Note that we can increase the
664 size up to 384K, i.e. indexing the first 192K non-negative
665 numbers, while still using shorts in the array.) This also
666 means that the size of the known region can be at most
667 64K for width-three characters.
671 extern short three_to_one_table[];
674 INLINE_HEADER int real_bufpos_to_bytind (struct buffer *buf, Bufpos x);
676 real_bufpos_to_bytind (struct buffer *buf, Bufpos x)
678 if (x >= buf->text->mule_bufmin && x <= buf->text->mule_bufmax)
679 return (buf->text->mule_bytmin +
681 (x - buf->text->mule_bufmin) * buf->text->mule_size
683 ((x - buf->text->mule_bufmin) << buf->text->mule_shifter) +
684 (buf->text->mule_three_p ? (x - buf->text->mule_bufmin) : 0)
688 return bufpos_to_bytind_func (buf, x);
691 INLINE_HEADER int real_bytind_to_bufpos (struct buffer *buf, Bytind x);
693 real_bytind_to_bufpos (struct buffer *buf, Bytind x)
695 if (x >= buf->text->mule_bytmin && x <= buf->text->mule_bytmax)
696 return (buf->text->mule_bufmin +
698 (buf->text->mule_size == 0 ? 0 :
699 (x - buf->text->mule_bytmin) / buf->text->mule_size)
701 ((buf->text->mule_three_p
702 ? three_to_one_table[x - buf->text->mule_bytmin]
703 : (x - buf->text->mule_bytmin) >> buf->text->mule_shifter))
707 return bytind_to_bufpos_func (buf, x);
712 # define real_bufpos_to_bytind(buf, x) ((Bytind) x)
713 # define real_bytind_to_bufpos(buf, x) ((Bufpos) x)
715 #endif /* not MULE */
717 #ifdef ERROR_CHECK_BUFPOS
719 Bytind bufpos_to_bytind (struct buffer *buf, Bufpos x);
720 Bufpos bytind_to_bufpos (struct buffer *buf, Bytind x);
722 #else /* not ERROR_CHECK_BUFPOS */
724 #define bufpos_to_bytind real_bufpos_to_bytind
725 #define bytind_to_bufpos real_bytind_to_bufpos
727 #endif /* not ERROR_CHECK_BUFPOS */
729 #define make_bufpos(buf, ind) make_int (bytind_to_bufpos (buf, ind))
731 /*----------------------------------------------------------------------*/
732 /* Converting between buffer bytes and Emacs characters */
733 /*----------------------------------------------------------------------*/
735 /* The character at position POS in buffer. */
736 #define BI_BUF_FETCH_CHAR(buf, pos) \
737 charptr_emchar (BI_BUF_BYTE_ADDRESS (buf, pos))
738 #define BUF_FETCH_CHAR(buf, pos) \
739 BI_BUF_FETCH_CHAR (buf, bufpos_to_bytind (buf, pos))
741 /* The character at position POS in buffer, as a string. This is
742 equivalent to set_charptr_emchar (str, BUF_FETCH_CHAR (buf, pos))
743 but is faster for Mule. */
745 # define BI_BUF_CHARPTR_COPY_CHAR(buf, pos, str) \
746 charptr_copy_char (BI_BUF_BYTE_ADDRESS (buf, pos), str)
747 #define BUF_CHARPTR_COPY_CHAR(buf, pos, str) \
748 BI_BUF_CHARPTR_COPY_CHAR (buf, bufpos_to_bytind (buf, pos), str)
751 /************************************************************************/
753 /* Converting between internal and external format */
755 /************************************************************************/
757 All client code should use only the two macros
759 TO_EXTERNAL_FORMAT (source_type, source, sink_type, sink, coding_system)
760 TO_INTERNAL_FORMAT (source_type, source, sink_type, sink, coding_system)
764 TO_EXTERNAL_FORMAT (DATA, (ptr, len),
768 The source or sink can be specified in one of these ways:
770 DATA, (ptr, len), // input data is a fixed buffer of size len
771 ALLOCA, (ptr, len), // output data is in a alloca()ed buffer of size len
772 MALLOC, (ptr, len), // output data is in a malloc()ed buffer of size len
773 C_STRING_ALLOCA, ptr, // equivalent to ALLOCA (ptr, len_ignored) on output.
774 C_STRING_MALLOC, ptr, // equivalent to MALLOC (ptr, len_ignored) on output.
775 C_STRING, ptr, // equivalent to DATA, (ptr, strlen (ptr) + 1) on input
776 LISP_STRING, string, // input or output is a Lisp_Object of type string
777 LISP_BUFFER, buffer, // output is written to (point) in lisp buffer
778 LISP_LSTREAM, lstream, // input or output is a Lisp_Object of type lstream
779 LISP_OPAQUE, object, // input or output is a Lisp_Object of type opaque
781 When specifying the sink, use lvalues, since the macro will assign to them,
782 except when the sink is an lstream or a lisp buffer.
784 The macros accept the kinds of sources and sinks appropriate for
785 internal and external data representation. See the type_checking_assert
786 macros below for the actual allowed types.
788 Since some sources and sinks use one argument (a Lisp_Object) to
789 specify them, while others take a (pointer, length) pair, we use
790 some C preprocessor trickery to allow pair arguments to be specified
791 by parenthesizing them, as in the examples above.
793 Anything prefixed by dfc_ (`data format conversion') is private.
794 They are only used to implement these macros.
796 Using C_STRING* is appropriate for using with external APIs that take
797 null-terminated strings. For internal data, we should try to be
798 '\0'-clean - i.e. allow arbitrary data to contain embedded '\0'.
800 Sometime in the future we might allow output to C_STRING_ALLOCA or
801 C_STRING_MALLOC _only_ with TO_EXTERNAL_FORMAT(), not
802 TO_INTERNAL_FORMAT(). */
804 #define TO_EXTERNAL_FORMAT(source_type, source, sink_type, sink, coding_system) \
806 dfc_conversion_type dfc_simplified_source_type; \
807 dfc_conversion_type dfc_simplified_sink_type; \
808 dfc_conversion_data dfc_source; \
809 dfc_conversion_data dfc_sink; \
811 type_checking_assert \
812 ((DFC_TYPE_##source_type == DFC_TYPE_DATA || \
813 DFC_TYPE_##source_type == DFC_TYPE_C_STRING || \
814 DFC_TYPE_##source_type == DFC_TYPE_LISP_STRING || \
815 DFC_TYPE_##source_type == DFC_TYPE_LISP_OPAQUE || \
816 DFC_TYPE_##source_type == DFC_TYPE_LISP_LSTREAM) \
818 (DFC_TYPE_##sink_type == DFC_TYPE_ALLOCA || \
819 DFC_TYPE_##sink_type == DFC_TYPE_MALLOC || \
820 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_ALLOCA || \
821 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_MALLOC || \
822 DFC_TYPE_##sink_type == DFC_TYPE_LISP_LSTREAM || \
823 DFC_TYPE_##sink_type == DFC_TYPE_LISP_OPAQUE)); \
825 DFC_SOURCE_##source_type##_TO_ARGS (source); \
826 DFC_SINK_##sink_type##_TO_ARGS (sink); \
828 DFC_CONVERT_TO_EXTERNAL_FORMAT (dfc_simplified_source_type, &dfc_source, \
830 dfc_simplified_sink_type, &dfc_sink); \
832 DFC_##sink_type##_USE_CONVERTED_DATA (sink); \
835 #define TO_INTERNAL_FORMAT(source_type, source, sink_type, sink, coding_system) \
837 dfc_conversion_type dfc_simplified_source_type; \
838 dfc_conversion_type dfc_simplified_sink_type; \
839 dfc_conversion_data dfc_source; \
840 dfc_conversion_data dfc_sink; \
842 type_checking_assert \
843 ((DFC_TYPE_##source_type == DFC_TYPE_DATA || \
844 DFC_TYPE_##source_type == DFC_TYPE_C_STRING || \
845 DFC_TYPE_##source_type == DFC_TYPE_LISP_OPAQUE || \
846 DFC_TYPE_##source_type == DFC_TYPE_LISP_LSTREAM) \
848 (DFC_TYPE_##sink_type == DFC_TYPE_ALLOCA || \
849 DFC_TYPE_##sink_type == DFC_TYPE_MALLOC || \
850 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_ALLOCA || \
851 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_MALLOC || \
852 DFC_TYPE_##sink_type == DFC_TYPE_LISP_STRING || \
853 DFC_TYPE_##sink_type == DFC_TYPE_LISP_LSTREAM || \
854 DFC_TYPE_##sink_type == DFC_TYPE_LISP_BUFFER)); \
856 DFC_SOURCE_##source_type##_TO_ARGS (source); \
857 DFC_SINK_##sink_type##_TO_ARGS (sink); \
859 DFC_CONVERT_TO_INTERNAL_FORMAT (dfc_simplified_source_type, &dfc_source, \
861 dfc_simplified_sink_type, &dfc_sink); \
863 DFC_##sink_type##_USE_CONVERTED_DATA (sink); \
867 #define DFC_CONVERT_TO_EXTERNAL_FORMAT dfc_convert_to_external_format
868 #define DFC_CONVERT_TO_INTERNAL_FORMAT dfc_convert_to_internal_format
870 /* ignore coding_system argument */
871 #define DFC_CONVERT_TO_EXTERNAL_FORMAT(a, b, coding_system, c, d) \
872 dfc_convert_to_external_format (a, b, c, d)
873 #define DFC_CONVERT_TO_INTERNAL_FORMAT(a, b, coding_system, c, d) \
874 dfc_convert_to_internal_format (a, b, c, d)
879 struct { const void *ptr; size_t len; } data;
880 Lisp_Object lisp_object;
881 } dfc_conversion_data;
883 enum dfc_conversion_type
889 DFC_TYPE_C_STRING_ALLOCA,
890 DFC_TYPE_C_STRING_MALLOC,
891 DFC_TYPE_LISP_STRING,
892 DFC_TYPE_LISP_LSTREAM,
893 DFC_TYPE_LISP_OPAQUE,
896 typedef enum dfc_conversion_type dfc_conversion_type;
898 /* WARNING: These use a static buffer. This can lead to disaster if
899 these functions are not used *very* carefully. Another reason to only use
900 TO_EXTERNAL_FORMATf() and TO_INTERNAL_FORMAT(). */
902 dfc_convert_to_external_format (dfc_conversion_type source_type,
903 dfc_conversion_data *source,
905 Lisp_Object coding_system,
907 dfc_conversion_type sink_type,
908 dfc_conversion_data *sink);
910 dfc_convert_to_internal_format (dfc_conversion_type source_type,
911 dfc_conversion_data *source,
913 Lisp_Object coding_system,
915 dfc_conversion_type sink_type,
916 dfc_conversion_data *sink);
918 #define DFC_CPP_CAR(x,y) (x)
919 #define DFC_CPP_CDR(x,y) (y)
921 /* Convert `source' to args for dfc_convert_to_*_format() */
922 #define DFC_SOURCE_DATA_TO_ARGS(val) do { \
923 dfc_source.data.ptr = DFC_CPP_CAR val; \
924 dfc_source.data.len = DFC_CPP_CDR val; \
925 dfc_simplified_source_type = DFC_TYPE_DATA; \
927 #define DFC_SOURCE_C_STRING_TO_ARGS(val) do { \
928 dfc_source.data.len = \
929 strlen ((char *) (dfc_source.data.ptr = (val))); \
930 dfc_simplified_source_type = DFC_TYPE_DATA; \
932 #define DFC_SOURCE_LISP_STRING_TO_ARGS(val) do { \
933 Lisp_Object dfc_slsta = (val); \
934 type_checking_assert (STRINGP (dfc_slsta)); \
935 dfc_source.lisp_object = dfc_slsta; \
936 dfc_simplified_source_type = DFC_TYPE_LISP_STRING; \
938 #define DFC_SOURCE_LISP_LSTREAM_TO_ARGS(val) do { \
939 Lisp_Object dfc_sllta = (val); \
940 type_checking_assert (LSTREAMP (dfc_sllta)); \
941 dfc_source.lisp_object = dfc_sllta; \
942 dfc_simplified_source_type = DFC_TYPE_LISP_LSTREAM; \
944 #define DFC_SOURCE_LISP_OPAQUE_TO_ARGS(val) do { \
945 Lisp_Opaque *dfc_slota = XOPAQUE (val); \
946 dfc_source.data.ptr = OPAQUE_DATA (dfc_slota); \
947 dfc_source.data.len = OPAQUE_SIZE (dfc_slota); \
948 dfc_simplified_source_type = DFC_TYPE_DATA; \
951 /* Convert `sink' to args for dfc_convert_to_*_format() */
952 #define DFC_SINK_ALLOCA_TO_ARGS(val) \
953 dfc_simplified_sink_type = DFC_TYPE_DATA
954 #define DFC_SINK_C_STRING_ALLOCA_TO_ARGS(val) \
955 dfc_simplified_sink_type = DFC_TYPE_DATA
956 #define DFC_SINK_MALLOC_TO_ARGS(val) \
957 dfc_simplified_sink_type = DFC_TYPE_DATA
958 #define DFC_SINK_C_STRING_MALLOC_TO_ARGS(val) \
959 dfc_simplified_sink_type = DFC_TYPE_DATA
960 #define DFC_SINK_LISP_STRING_TO_ARGS(val) \
961 dfc_simplified_sink_type = DFC_TYPE_DATA
962 #define DFC_SINK_LISP_OPAQUE_TO_ARGS(val) \
963 dfc_simplified_sink_type = DFC_TYPE_DATA
964 #define DFC_SINK_LISP_LSTREAM_TO_ARGS(val) do { \
965 Lisp_Object dfc_sllta = (val); \
966 type_checking_assert (LSTREAMP (dfc_sllta)); \
967 dfc_sink.lisp_object = dfc_sllta; \
968 dfc_simplified_sink_type = DFC_TYPE_LISP_LSTREAM; \
970 #define DFC_SINK_LISP_BUFFER_TO_ARGS(val) do { \
971 struct buffer *dfc_slbta = XBUFFER (val); \
972 dfc_sink.lisp_object = \
973 make_lisp_buffer_output_stream \
974 (dfc_slbta, BUF_PT (dfc_slbta), 0); \
975 dfc_simplified_sink_type = DFC_TYPE_LISP_LSTREAM; \
978 /* Assign to the `sink' lvalue(s) using the converted data. */
979 typedef union { char c; void *p; } *dfc_aliasing_voidpp;
980 #define DFC_ALLOCA_USE_CONVERTED_DATA(sink) do { \
981 void * dfc_sink_ret = alloca (dfc_sink.data.len + 1); \
982 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1); \
983 ((dfc_aliasing_voidpp) &(DFC_CPP_CAR sink))->p = dfc_sink_ret; \
984 (DFC_CPP_CDR sink) = dfc_sink.data.len; \
986 #define DFC_MALLOC_USE_CONVERTED_DATA(sink) do { \
987 void * dfc_sink_ret = xmalloc (dfc_sink.data.len + 1); \
988 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1); \
989 ((dfc_aliasing_voidpp) &(DFC_CPP_CAR sink))->p = dfc_sink_ret; \
990 (DFC_CPP_CDR sink) = dfc_sink.data.len; \
992 #define DFC_C_STRING_ALLOCA_USE_CONVERTED_DATA(sink) do { \
993 void * dfc_sink_ret = alloca (dfc_sink.data.len + 1); \
994 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1); \
995 (sink) = (char *) dfc_sink_ret; \
997 #define DFC_C_STRING_MALLOC_USE_CONVERTED_DATA(sink) do { \
998 void * dfc_sink_ret = xmalloc (dfc_sink.data.len + 1); \
999 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1); \
1000 (sink) = (char *) dfc_sink_ret; \
1002 #define DFC_LISP_STRING_USE_CONVERTED_DATA(sink) \
1003 sink = make_string ((Bufbyte *) dfc_sink.data.ptr, dfc_sink.data.len)
1004 #define DFC_LISP_OPAQUE_USE_CONVERTED_DATA(sink) \
1005 sink = make_opaque (dfc_sink.data.ptr, dfc_sink.data.len)
1006 #define DFC_LISP_LSTREAM_USE_CONVERTED_DATA(sink) /* data already used */
1007 #define DFC_LISP_BUFFER_USE_CONVERTED_DATA(sink) \
1008 Lstream_delete (XLSTREAM (dfc_sink.lisp_object))
1010 /* Someday we might want to distinguish between Qnative and Qfile_name
1011 by using coding-system aliases, but for now it suffices to have
1012 these be identical. Qnative can be used as the coding_system
1013 argument to TO_EXTERNAL_FORMAT() and TO_INTERNAL_FORMAT(). */
1014 #define Qnative Qfile_name
1016 #define GET_C_CHARPTR_EXT_DATA_ALLOCA(ptr, fmt, ptr_out) do \
1018 Extcount gcceda_ignored_len; \
1019 const Bufbyte *gcceda_ptr_in = (const Bufbyte *) (ptr); \
1020 Extbyte *gcceda_ptr_out; \
1022 GET_CHARPTR_EXT_DATA_ALLOCA (gcceda_ptr_in, \
1023 strlen ((char *) gcceda_ptr_in), \
1026 gcceda_ignored_len); \
1027 (ptr_out) = (char *) gcceda_ptr_out; \
1030 #define GET_C_CHARPTR_EXT_BINARY_DATA_ALLOCA(ptr, ptr_out) \
1031 GET_C_CHARPTR_EXT_DATA_ALLOCA (ptr, FORMAT_BINARY, ptr_out)
1032 #define GET_CHARPTR_EXT_BINARY_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1033 GET_CHARPTR_EXT_DATA_ALLOCA (ptr, len, FORMAT_BINARY, ptr_out, len_out)
1035 #define GET_C_CHARPTR_EXT_FILENAME_DATA_ALLOCA(ptr, ptr_out) \
1036 GET_C_CHARPTR_EXT_DATA_ALLOCA (ptr, FORMAT_FILENAME, ptr_out)
1037 #define GET_CHARPTR_EXT_FILENAME_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1038 GET_CHARPTR_EXT_DATA_ALLOCA (ptr, len, FORMAT_FILENAME, ptr_out, len_out)
1040 #define GET_C_CHARPTR_EXT_CTEXT_DATA_ALLOCA(ptr, ptr_out) \
1041 GET_C_CHARPTR_EXT_DATA_ALLOCA (ptr, FORMAT_CTEXT, ptr_out)
1042 #define GET_CHARPTR_EXT_CTEXT_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1043 GET_CHARPTR_EXT_DATA_ALLOCA (ptr, len, FORMAT_CTEXT, ptr_out, len_out)
1045 /* Maybe convert external charptr's data into internal format and store
1046 the result in alloca()'ed space.
1048 You may wonder why this is written in this fashion and not as a
1049 function call. With a little trickery it could certainly be
1050 written this way, but it won't work because of those DAMN GCC WANKERS
1051 who couldn't be bothered to handle alloca() properly on the x86
1052 architecture. (If you put a call to alloca() in the argument to
1053 a function call, the stack space gets allocated right in the
1054 middle of the arguments to the function call and you are unbelievably
1059 #define GET_CHARPTR_INT_DATA_ALLOCA(ptr, len, fmt, ptr_out, len_out) do \
1061 Extcount gcida_len_in = (Extcount) (len); \
1062 Bytecount gcida_len_out; \
1063 const Extbyte *gcida_ptr_in = (ptr); \
1064 Bufbyte *gcida_ptr_out = \
1065 convert_from_external_format (gcida_ptr_in, gcida_len_in, \
1066 &gcida_len_out, fmt); \
1067 /* If the new string is identical to the old (will be the case most \
1068 of the time), just return the same string back. This saves \
1069 on alloca()ing, which can be useful on C alloca() machines and \
1070 on stack-space-challenged environments. */ \
1072 if (gcida_len_in == gcida_len_out && \
1073 !memcmp (gcida_ptr_in, gcida_ptr_out, gcida_len_out)) \
1075 (ptr_out) = (Bufbyte *) gcida_ptr_in; \
1079 (ptr_out) = (Extbyte *) alloca (1 + gcida_len_out); \
1080 memcpy ((void *) ptr_out, gcida_ptr_out, 1 + gcida_len_out); \
1082 (len_out) = gcida_len_out; \
1087 #define GET_CHARPTR_INT_DATA_ALLOCA(ptr, len, fmt, ptr_out, len_out) do \
1089 (ptr_out) = (Bufbyte *) (ptr); \
1090 (len_out) = (Bytecount) (len); \
1095 #define GET_C_CHARPTR_INT_DATA_ALLOCA(ptr, fmt, ptr_out) do \
1097 Bytecount gccida_ignored_len; \
1098 const Extbyte *gccida_ptr_in = (const Extbyte *) (ptr); \
1099 Bufbyte *gccida_ptr_out; \
1101 GET_CHARPTR_INT_DATA_ALLOCA (gccida_ptr_in, \
1102 strlen ((char *) gccida_ptr_in), \
1105 gccida_ignored_len); \
1106 (ptr_out) = gccida_ptr_out; \
1109 #define GET_C_CHARPTR_INT_BINARY_DATA_ALLOCA(ptr, ptr_out) \
1110 GET_C_CHARPTR_INT_DATA_ALLOCA (ptr, FORMAT_BINARY, ptr_out)
1111 #define GET_CHARPTR_INT_BINARY_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1112 GET_CHARPTR_INT_DATA_ALLOCA (ptr, len, FORMAT_BINARY, ptr_out, len_out)
1114 #define GET_C_CHARPTR_INT_FILENAME_DATA_ALLOCA(ptr, ptr_out) \
1115 GET_C_CHARPTR_INT_DATA_ALLOCA (ptr, FORMAT_FILENAME, ptr_out)
1116 #define GET_CHARPTR_INT_FILENAME_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1117 GET_CHARPTR_INT_DATA_ALLOCA (ptr, len, FORMAT_FILENAME, ptr_out, len_out)
1119 #define GET_C_CHARPTR_INT_CTEXT_DATA_ALLOCA(ptr, ptr_out) \
1120 GET_C_CHARPTR_INT_DATA_ALLOCA (ptr, FORMAT_CTEXT, ptr_out)
1121 #define GET_CHARPTR_INT_CTEXT_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1122 GET_CHARPTR_INT_DATA_ALLOCA (ptr, len, FORMAT_CTEXT, ptr_out, len_out)
1125 /* Maybe convert Lisp string's data into ext-format and store the result in
1128 You may wonder why this is written in this fashion and not as a
1129 function call. With a little trickery it could certainly be
1130 written this way, but it won't work because of those DAMN GCC WANKERS
1131 who couldn't be bothered to handle alloca() properly on the x86
1132 architecture. (If you put a call to alloca() in the argument to
1133 a function call, the stack space gets allocated right in the
1134 middle of the arguments to the function call and you are unbelievably
1137 #define GET_STRING_EXT_DATA_ALLOCA(s, fmt, ptr_out, len_out) do \
1139 Extcount gseda_len_out; \
1140 struct Lisp_String *gseda_s = XSTRING (s); \
1141 Extbyte * gseda_ptr_out = \
1142 convert_to_external_format (string_data (gseda_s), \
1143 string_length (gseda_s), \
1144 &gseda_len_out, fmt); \
1145 (ptr_out) = (Extbyte *) alloca (1 + gseda_len_out); \
1146 memcpy ((void *) ptr_out, gseda_ptr_out, 1 + gseda_len_out); \
1147 (len_out) = gseda_len_out; \
1151 #define GET_C_STRING_EXT_DATA_ALLOCA(s, fmt, ptr_out) do \
1153 Extcount gcseda_ignored_len; \
1154 Extbyte *gcseda_ptr_out; \
1156 GET_STRING_EXT_DATA_ALLOCA (s, fmt, gcseda_ptr_out, \
1157 gcseda_ignored_len); \
1158 (ptr_out) = (char *) gcseda_ptr_out; \
1161 #define GET_STRING_BINARY_DATA_ALLOCA(s, ptr_out, len_out) \
1162 GET_STRING_EXT_DATA_ALLOCA (s, FORMAT_BINARY, ptr_out, len_out)
1163 #define GET_C_STRING_BINARY_DATA_ALLOCA(s, ptr_out) \
1164 GET_C_STRING_EXT_DATA_ALLOCA (s, FORMAT_BINARY, ptr_out)
1166 #define GET_STRING_FILENAME_DATA_ALLOCA(s, ptr_out, len_out) \
1167 GET_STRING_EXT_DATA_ALLOCA (s, FORMAT_FILENAME, ptr_out, len_out)
1168 #define GET_C_STRING_FILENAME_DATA_ALLOCA(s, ptr_out) \
1169 GET_C_STRING_EXT_DATA_ALLOCA (s, FORMAT_FILENAME, ptr_out)
1171 #define GET_STRING_OS_DATA_ALLOCA(s, ptr_out, len_out) \
1172 GET_STRING_EXT_DATA_ALLOCA (s, FORMAT_OS, ptr_out, len_out)
1173 #define GET_C_STRING_OS_DATA_ALLOCA(s, ptr_out) \
1174 GET_C_STRING_EXT_DATA_ALLOCA (s, FORMAT_OS, ptr_out)
1176 #define GET_STRING_CTEXT_DATA_ALLOCA(s, ptr_out, len_out) \
1177 GET_STRING_EXT_DATA_ALLOCA (s, FORMAT_CTEXT, ptr_out, len_out)
1178 #define GET_C_STRING_CTEXT_DATA_ALLOCA(s, ptr_out) \
1179 GET_C_STRING_EXT_DATA_ALLOCA (s, FORMAT_CTEXT, ptr_out)
1182 /************************************************************************/
1184 /* higher-level buffer-position functions */
1186 /************************************************************************/
1188 /*----------------------------------------------------------------------*/
1189 /* Settor macros for important positions in a buffer */
1190 /*----------------------------------------------------------------------*/
1192 /* Set beginning of accessible range of buffer. */
1193 #define SET_BOTH_BUF_BEGV(buf, val, bival) \
1196 (buf)->begv = (bival); \
1197 (buf)->bufbegv = (val); \
1200 /* Set end of accessible range of buffer. */
1201 #define SET_BOTH_BUF_ZV(buf, val, bival) \
1204 (buf)->zv = (bival); \
1205 (buf)->bufzv = (val); \
1209 /* Since BEGV and ZV are almost never set, it's reasonable to enforce
1210 the restriction that the Bufpos and Bytind values must both be
1211 specified. However, point is set in lots and lots of places. So
1212 we provide the ability to specify both (for efficiency) or just
1214 #define BOTH_BUF_SET_PT(buf, val, bival) set_buffer_point (buf, val, bival)
1215 #define BI_BUF_SET_PT(buf, bival) \
1216 BOTH_BUF_SET_PT (buf, bytind_to_bufpos (buf, bival), bival)
1217 #define BUF_SET_PT(buf, value) \
1218 BOTH_BUF_SET_PT (buf, value, bufpos_to_bytind (buf, value))
1222 /* These macros exist in FSFmacs because SET_PT() in FSFmacs incorrectly
1223 does too much stuff, such as moving out of invisible extents. */
1224 #define TEMP_SET_PT(position) (temp_set_point ((position), current_buffer))
1225 #define SET_BUF_PT(buf, value) ((buf)->pt = (value))
1226 #endif /* FSFmacs */
1228 /*----------------------------------------------------------------------*/
1229 /* Miscellaneous buffer values */
1230 /*----------------------------------------------------------------------*/
1232 /* Number of characters in buffer */
1233 #define BUF_SIZE(buf) (BUF_Z (buf) - BUF_BEG (buf))
1235 /* Is this buffer narrowed? */
1236 #define BUF_NARROWED(buf) \
1237 ((BI_BUF_BEGV (buf) != BI_BUF_BEG (buf)) || \
1238 (BI_BUF_ZV (buf) != BI_BUF_Z (buf)))
1240 /* Modification count. */
1241 #define BUF_MODIFF(buf) ((buf)->text->modiff)
1243 /* Saved modification count. */
1244 #define BUF_SAVE_MODIFF(buf) ((buf)->text->save_modiff)
1247 #define BUF_FACECHANGE(buf) ((buf)->face_change)
1249 #define POINT_MARKER_P(marker) \
1250 (XMARKER (marker)->buffer != 0 && \
1251 EQ (marker, XMARKER (marker)->buffer->point_marker))
1253 #define BUF_MARKERS(buf) ((buf)->markers)
1257 The new definitions of CEILING_OF() and FLOOR_OF() differ semantically
1258 from the old ones (in FSF Emacs and XEmacs 19.11 and before).
1259 Conversion is as follows:
1261 OLD_BI_CEILING_OF(n) = NEW_BI_CEILING_OF(n) - 1
1262 OLD_BI_FLOOR_OF(n) = NEW_BI_FLOOR_OF(n + 1)
1264 The definitions were changed because the new definitions are more
1265 consistent with the way everything else works in Emacs.
1268 /* Properties of CEILING_OF and FLOOR_OF (also apply to BI_ variants):
1270 1) FLOOR_OF (CEILING_OF (n)) = n
1271 CEILING_OF (FLOOR_OF (n)) = n
1273 2) CEILING_OF (n) = n if and only if n = ZV
1274 FLOOR_OF (n) = n if and only if n = BEGV
1276 3) CEILING_OF (CEILING_OF (n)) = ZV
1277 FLOOR_OF (FLOOR_OF (n)) = BEGV
1279 4) The bytes in the regions
1281 [BYTE_ADDRESS (n), BYTE_ADDRESS_BEFORE (CEILING_OF (n))]
1285 [BYTE_ADDRESS (FLOOR_OF (n)), BYTE_ADDRESS_BEFORE (n)]
1291 /* Return the maximum index in the buffer it is safe to scan forwards
1292 past N to. This is used to prevent buffer scans from running into
1293 the gap (e.g. search.c). All characters between N and CEILING_OF(N)
1294 are located contiguous in memory. Note that the character *at*
1295 CEILING_OF(N) is not contiguous in memory. */
1296 #define BI_BUF_CEILING_OF(b, n) \
1297 ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_ZV (b) ? \
1298 (b)->text->gpt : BI_BUF_ZV (b))
1299 #define BUF_CEILING_OF(b, n) \
1300 bytind_to_bufpos (b, BI_BUF_CEILING_OF (b, bufpos_to_bytind (b, n)))
1302 /* Return the minimum index in the buffer it is safe to scan backwards
1303 past N to. All characters between FLOOR_OF(N) and N are located
1304 contiguous in memory. Note that the character *at* N may not be
1305 contiguous in memory. */
1306 #define BI_BUF_FLOOR_OF(b, n) \
1307 (BI_BUF_BEGV (b) < (b)->text->gpt && (b)->text->gpt < (n) ? \
1308 (b)->text->gpt : BI_BUF_BEGV (b))
1309 #define BUF_FLOOR_OF(b, n) \
1310 bytind_to_bufpos (b, BI_BUF_FLOOR_OF (b, bufpos_to_bytind (b, n)))
1312 #define BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \
1313 ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_Z (b) ? \
1314 (b)->text->gpt : BI_BUF_Z (b))
1315 #define BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \
1317 (b, BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
1319 #define BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \
1320 (BI_BUF_BEG (b) < (b)->text->gpt && (b)->text->gpt < (n) ? \
1321 (b)->text->gpt : BI_BUF_BEG (b))
1322 #define BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \
1324 (b, BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
1327 extern struct buffer *current_buffer;
1329 /* This is the initial (startup) directory, as used for the *scratch* buffer.
1330 We're making this a global to make others aware of the startup directory.
1331 `initial_directory' is stored in external format.
1333 extern char initial_directory[];
1334 extern void init_initial_directory (void); /* initialize initial_directory */
1336 EXFUN (Fbuffer_disable_undo, 1);
1337 EXFUN (Fbuffer_modified_p, 1);
1338 EXFUN (Fbuffer_name, 1);
1339 EXFUN (Fcurrent_buffer, 0);
1340 EXFUN (Ferase_buffer, 1);
1341 EXFUN (Fget_buffer, 1);
1342 EXFUN (Fget_buffer_create, 1);
1343 EXFUN (Fget_file_buffer, 1);
1344 EXFUN (Fkill_buffer, 1);
1345 EXFUN (Fother_buffer, 3);
1346 EXFUN (Frecord_buffer, 1);
1347 EXFUN (Fset_buffer, 1);
1348 EXFUN (Fset_buffer_modified_p, 2);
1350 extern Lisp_Object QSscratch, Qafter_change_function, Qafter_change_functions;
1351 extern Lisp_Object Qbefore_change_function, Qbefore_change_functions;
1352 extern Lisp_Object Qbuffer_or_string_p, Qdefault_directory, Qfirst_change_hook;
1353 extern Lisp_Object Qpermanent_local, Vafter_change_function;
1354 extern Lisp_Object Vafter_change_functions, Vbefore_change_function;
1355 extern Lisp_Object Vbefore_change_functions, Vbuffer_alist, Vbuffer_defaults;
1356 extern Lisp_Object Vinhibit_read_only, Vtransient_mark_mode;
1358 /* This structure marks which slots in a buffer have corresponding
1359 default values in Vbuffer_defaults.
1360 Each such slot has a nonzero value in this structure.
1361 The value has only one nonzero bit.
1363 When a buffer has its own local value for a slot,
1364 the bit for that slot (found in the same slot in this structure)
1365 is turned on in the buffer's local_var_flags slot.
1367 If a slot in this structure is zero, then even though there may
1368 be a DEFVAR_BUFFER_LOCAL for the slot, there is no default value for it;
1369 and the corresponding slot in Vbuffer_defaults is not used. */
1371 extern struct buffer buffer_local_flags;
1374 /* Allocation of buffer data. */
1378 char *r_alloc (unsigned char **, size_t);
1379 char *r_re_alloc (unsigned char **, size_t);
1380 void r_alloc_free (unsigned char **);
1382 #define BUFFER_ALLOC(data, size) \
1383 ((Bufbyte *) r_alloc ((unsigned char **) &data, (size) * sizeof(Bufbyte)))
1384 #define BUFFER_REALLOC(data, size) \
1385 ((Bufbyte *) r_re_alloc ((unsigned char **) &data, (size) * sizeof(Bufbyte)))
1386 #define BUFFER_FREE(data) r_alloc_free ((unsigned char **) &(data))
1387 #define R_ALLOC_DECLARE(var,data) r_alloc_declare (&(var), data)
1389 #else /* !REL_ALLOC */
1391 #define BUFFER_ALLOC(data,size)\
1392 (data = xnew_array (Bufbyte, size))
1393 #define BUFFER_REALLOC(data,size)\
1394 ((Bufbyte *) xrealloc (data, (size) * sizeof(Bufbyte)))
1395 /* Avoid excess parentheses, or syntax errors may rear their heads. */
1396 #define BUFFER_FREE(data) xfree (data)
1397 #define R_ALLOC_DECLARE(var,data)
1399 #endif /* !REL_ALLOC */
1401 extern Lisp_Object Vbuffer_alist;
1402 void set_buffer_internal (struct buffer *b);
1403 struct buffer *decode_buffer (Lisp_Object buffer, int allow_string);
1405 /* from editfns.c */
1406 void widen_buffer (struct buffer *b, int no_clip);
1407 int beginning_of_line_p (struct buffer *b, Bufpos pt);
1410 void set_buffer_point (struct buffer *buf, Bufpos pos, Bytind bipos);
1411 void find_charsets_in_bufbyte_string (Charset_ID *charsets,
1414 void find_charsets_in_emchar_string (Charset_ID *charsets,
1417 int bufbyte_string_displayed_columns (const Bufbyte *str, Bytecount len);
1418 int emchar_string_displayed_columns (const Emchar *str, Charcount len);
1419 void convert_bufbyte_string_into_emchar_dynarr (const Bufbyte *str,
1421 Emchar_dynarr *dyn);
1422 Charcount convert_bufbyte_string_into_emchar_string (const Bufbyte *str,
1425 void convert_emchar_string_into_bufbyte_dynarr (Emchar *arr, int nels,
1426 Bufbyte_dynarr *dyn);
1427 Bufbyte *convert_emchar_string_into_malloced_string (Emchar *arr, int nels,
1428 Bytecount *len_out);
1430 void init_buffer_markers (struct buffer *b);
1431 void uninit_buffer_markers (struct buffer *b);
1433 /* flags for get_buffer_pos_char(), get_buffer_range_char(), etc. */
1434 /* At most one of GB_COERCE_RANGE and GB_NO_ERROR_IF_BAD should be
1435 specified. At most one of GB_NEGATIVE_FROM_END and GB_NO_ERROR_IF_BAD
1436 should be specified. */
1438 #define GB_ALLOW_PAST_ACCESSIBLE (1 << 0)
1439 #define GB_ALLOW_NIL (1 << 1)
1440 #define GB_CHECK_ORDER (1 << 2)
1441 #define GB_COERCE_RANGE (1 << 3)
1442 #define GB_NO_ERROR_IF_BAD (1 << 4)
1443 #define GB_NEGATIVE_FROM_END (1 << 5)
1444 #define GB_HISTORICAL_STRING_BEHAVIOR (GB_NEGATIVE_FROM_END | GB_ALLOW_NIL)
1446 Bufpos get_buffer_pos_char (struct buffer *b, Lisp_Object pos,
1447 unsigned int flags);
1448 Bytind get_buffer_pos_byte (struct buffer *b, Lisp_Object pos,
1449 unsigned int flags);
1450 void get_buffer_range_char (struct buffer *b, Lisp_Object from, Lisp_Object to,
1451 Bufpos *from_out, Bufpos *to_out,
1452 unsigned int flags);
1453 void get_buffer_range_byte (struct buffer *b, Lisp_Object from, Lisp_Object to,
1454 Bytind *from_out, Bytind *to_out,
1455 unsigned int flags);
1456 Charcount get_string_pos_char (Lisp_Object string, Lisp_Object pos,
1457 unsigned int flags);
1458 Bytecount get_string_pos_byte (Lisp_Object string, Lisp_Object pos,
1459 unsigned int flags);
1460 void get_string_range_char (Lisp_Object string, Lisp_Object from,
1461 Lisp_Object to, Charcount *from_out,
1462 Charcount *to_out, unsigned int flags);
1463 void get_string_range_byte (Lisp_Object string, Lisp_Object from,
1464 Lisp_Object to, Bytecount *from_out,
1465 Bytecount *to_out, unsigned int flags);
1466 Bufpos get_buffer_or_string_pos_char (Lisp_Object object, Lisp_Object pos,
1467 unsigned int flags);
1468 Bytind get_buffer_or_string_pos_byte (Lisp_Object object, Lisp_Object pos,
1469 unsigned int flags);
1470 void get_buffer_or_string_range_char (Lisp_Object object, Lisp_Object from,
1471 Lisp_Object to, Bufpos *from_out,
1472 Bufpos *to_out, unsigned int flags);
1473 void get_buffer_or_string_range_byte (Lisp_Object object, Lisp_Object from,
1474 Lisp_Object to, Bytind *from_out,
1475 Bytind *to_out, unsigned int flags);
1476 Bufpos buffer_or_string_accessible_begin_char (Lisp_Object object);
1477 Bufpos buffer_or_string_accessible_end_char (Lisp_Object object);
1478 Bytind buffer_or_string_accessible_begin_byte (Lisp_Object object);
1479 Bytind buffer_or_string_accessible_end_byte (Lisp_Object object);
1480 Bufpos buffer_or_string_absolute_begin_char (Lisp_Object object);
1481 Bufpos buffer_or_string_absolute_end_char (Lisp_Object object);
1482 Bytind buffer_or_string_absolute_begin_byte (Lisp_Object object);
1483 Bytind buffer_or_string_absolute_end_byte (Lisp_Object object);
1484 void record_buffer (Lisp_Object buf);
1485 Lisp_Object get_buffer (Lisp_Object name,
1486 int error_if_deleted_or_does_not_exist);
1487 int map_over_sharing_buffers (struct buffer *buf,
1488 int (*mapfun) (struct buffer *buf,
1493 /************************************************************************/
1494 /* Case conversion */
1495 /************************************************************************/
1497 /* A "trt" table is a mapping from characters to other characters,
1498 typically used to convert between uppercase and lowercase. For
1499 compatibility reasons, trt tables are currently in the form of
1500 a Lisp string of 256 characters, specifying the conversion for each
1501 of the first 256 Emacs characters (i.e. the 256 Latin-1 characters).
1502 This should be generalized at some point to support conversions for
1503 all of the allowable Mule characters.
1506 /* The _1 macros are named as such because they assume that you have
1507 already guaranteed that the character values are all in the range
1508 0 - 255. Bad lossage will happen otherwise. */
1510 # define MAKE_TRT_TABLE() Fmake_string (make_int (256), make_char (0))
1511 # define TRT_TABLE_AS_STRING(table) XSTRING_DATA (table)
1512 # define TRT_TABLE_CHAR_1(table, ch) \
1513 string_char (XSTRING (table), (Charcount) ch)
1514 # define SET_TRT_TABLE_CHAR_1(table, ch1, ch2) \
1515 set_string_char (XSTRING (table), (Charcount) ch1, ch2)
1518 # define MAKE_MIRROR_TRT_TABLE() make_opaque (OPAQUE_CLEAR, 256)
1519 # define MIRROR_TRT_TABLE_AS_STRING(table) ((Bufbyte *) XOPAQUE_DATA (table))
1520 # define MIRROR_TRT_TABLE_CHAR_1(table, ch) \
1521 ((Emchar) (MIRROR_TRT_TABLE_AS_STRING (table)[ch]))
1522 # define SET_MIRROR_TRT_TABLE_CHAR_1(table, ch1, ch2) \
1523 (MIRROR_TRT_TABLE_AS_STRING (table)[ch1] = (Bufbyte) (ch2))
1526 # define IN_TRT_TABLE_DOMAIN(c) (((EMACS_UINT) (c)) <= 255)
1529 #define MIRROR_DOWNCASE_TABLE_AS_STRING(buf) \
1530 MIRROR_TRT_TABLE_AS_STRING (buf->mirror_downcase_table)
1531 #define MIRROR_UPCASE_TABLE_AS_STRING(buf) \
1532 MIRROR_TRT_TABLE_AS_STRING (buf->mirror_upcase_table)
1533 #define MIRROR_CANON_TABLE_AS_STRING(buf) \
1534 MIRROR_TRT_TABLE_AS_STRING (buf->mirror_case_canon_table)
1535 #define MIRROR_EQV_TABLE_AS_STRING(buf) \
1536 MIRROR_TRT_TABLE_AS_STRING (buf->mirror_case_eqv_table)
1538 #define MIRROR_DOWNCASE_TABLE_AS_STRING(buf) \
1539 TRT_TABLE_AS_STRING (buf->downcase_table)
1540 #define MIRROR_UPCASE_TABLE_AS_STRING(buf) \
1541 TRT_TABLE_AS_STRING (buf->upcase_table)
1542 #define MIRROR_CANON_TABLE_AS_STRING(buf) \
1543 TRT_TABLE_AS_STRING (buf->case_canon_table)
1544 #define MIRROR_EQV_TABLE_AS_STRING(buf) \
1545 TRT_TABLE_AS_STRING (buf->case_eqv_table)
1548 INLINE_HEADER Emchar TRT_TABLE_OF (Lisp_Object trt, Emchar c);
1549 INLINE_HEADER Emchar
1550 TRT_TABLE_OF (Lisp_Object trt, Emchar c)
1552 return IN_TRT_TABLE_DOMAIN (c) ? TRT_TABLE_CHAR_1 (trt, c) : c;
1555 /* Macros used below. */
1556 #define DOWNCASE_TABLE_OF(buf, c) TRT_TABLE_OF (buf->downcase_table, c)
1557 #define UPCASE_TABLE_OF(buf, c) TRT_TABLE_OF (buf->upcase_table, c)
1559 /* 1 if CH is upper case. */
1561 INLINE_HEADER int UPPERCASEP (struct buffer *buf, Emchar ch);
1563 UPPERCASEP (struct buffer *buf, Emchar ch)
1565 return DOWNCASE_TABLE_OF (buf, ch) != ch;
1568 /* 1 if CH is lower case. */
1570 INLINE_HEADER int LOWERCASEP (struct buffer *buf, Emchar ch);
1572 LOWERCASEP (struct buffer *buf, Emchar ch)
1574 return (UPCASE_TABLE_OF (buf, ch) != ch &&
1575 DOWNCASE_TABLE_OF (buf, ch) == ch);
1578 /* 1 if CH is neither upper nor lower case. */
1580 INLINE_HEADER int NOCASEP (struct buffer *buf, Emchar ch);
1582 NOCASEP (struct buffer *buf, Emchar ch)
1584 return UPCASE_TABLE_OF (buf, ch) == ch;
1587 /* Upcase a character, or make no change if that cannot be done. */
1589 INLINE_HEADER Emchar UPCASE (struct buffer *buf, Emchar ch);
1590 INLINE_HEADER Emchar
1591 UPCASE (struct buffer *buf, Emchar ch)
1593 return (DOWNCASE_TABLE_OF (buf, ch) == ch) ? UPCASE_TABLE_OF (buf, ch) : ch;
1596 /* Upcase a character known to be not upper case. Unused. */
1598 #define UPCASE1(buf, ch) UPCASE_TABLE_OF (buf, ch)
1600 /* Downcase a character, or make no change if that cannot be done. */
1602 #define DOWNCASE(buf, ch) DOWNCASE_TABLE_OF (buf, ch)
1604 #endif /* INCLUDED_buffer_h_ */