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"
41 /************************************************************************/
43 /* definition of Lisp buffer object */
45 /************************************************************************/
47 /* Note: we keep both Bytind and Bufpos versions of some of the
48 important buffer positions because they are accessed so much.
49 If we didn't do this, we would constantly be invalidating the
50 bufpos<->bytind cache under Mule.
52 Note that under non-Mule, both versions will always be the
53 same so we don't really need to keep track of them. But it
54 simplifies the logic to go ahead and do so all the time and
55 the memory loss is insignificant. */
57 /* Formerly, it didn't much matter what went inside the struct buffer_text
58 and what went outside it. Now it does, with the advent of "indirect
59 buffers" that share text with another buffer. An indirect buffer
60 shares the same *text* as another buffer, but has its own buffer-local
61 variables, its own accessible region, and its own markers and extents.
62 (Due to the nature of markers, it doesn't actually matter much whether
63 we stick them inside or out of the struct buffer_text -- the user won't
64 notice any difference -- but we go ahead and put them outside for
65 consistency and overall saneness of algorithm.)
67 FSFmacs gets away with not maintaining any "children" pointers from
68 a buffer to the indirect buffers that refer to it by putting the
69 markers inside of the struct buffer_text, using markers to keep track
70 of BEGV and ZV in indirect buffers, and relying on the fact that
71 all intervals (text properties and overlays) use markers for their
72 start and end points. We don't do this for extents (markers are
73 inefficient anyway and take up space), so we have to maintain
74 children pointers. This is not terribly hard, though, and the
75 code to maintain this is just like the code already present in
76 extent-parent and extent-children.
81 Bufbyte *beg; /* Actual address of buffer contents. */
82 Bytind gpt; /* Index of gap in buffer. */
83 Bytind z; /* Index of end of buffer. */
84 Bufpos bufz; /* Equivalent as a Bufpos. */
85 int gap_size; /* Size of buffer's gap */
86 int end_gap_size; /* Size of buffer's end gap */
87 long modiff; /* This counts buffer-modification events
88 for this buffer. It is incremented for
89 each such event, and never otherwise
91 long save_modiff; /* Previous value of modiff, as of last
92 time buffer visited or saved a file. */
95 /* We keep track of a "known" region for very fast access.
96 This information is text-only so it goes here. */
97 Bufpos mule_bufmin, mule_bufmax;
98 Bytind mule_bytmin, mule_bytmax;
102 int mule_shifter, mule_three_p;
105 /* And we also cache 16 positions for fairly fast access near those
107 Bufpos mule_bufpos_cache[16];
108 Bytind mule_bytind_cache[16];
111 /* Similar to the above, we keep track of positions for which line
112 number has last been calculated. See line-number.c. */
113 Lisp_Object line_number_cache;
115 /* Change data that goes with the text. */
116 struct buffer_text_change_data *changes;
122 struct lcrecord_header header;
124 /* This structure holds the coordinates of the buffer contents
125 in ordinary buffers. In indirect buffers, this is not used. */
126 struct buffer_text own_text;
128 /* This points to the `struct buffer_text' that is used for this buffer.
129 In an ordinary buffer, this is the own_text field above.
130 In an indirect buffer, this is the own_text field of another buffer. */
131 struct buffer_text *text;
133 Bytind pt; /* Position of point in buffer. */
134 Bufpos bufpt; /* Equivalent as a Bufpos. */
135 Bytind begv; /* Index of beginning of accessible range. */
136 Bufpos bufbegv; /* Equivalent as a Bufpos. */
137 Bytind zv; /* Index of end of accessible range. */
138 Bufpos bufzv; /* Equivalent as a Bufpos. */
140 int face_change; /* This is set when a change in how the text should
141 be displayed (e.g., font, color) is made. */
143 /* Whether buffer specific face is specified. */
144 int buffer_local_face_property;
146 /* change data indicating what portion of the text has changed
147 since the last time this was reset. Used by redisplay.
148 Logically we should keep this with the text structure, but
149 redisplay resets it for each buffer individually and we don't
150 want interference between an indirect buffer and its base
152 struct each_buffer_change_data *changes;
154 #ifdef REGION_CACHE_NEEDS_WORK
155 /* If the long line scan cache is enabled (i.e. the buffer-local
156 variable cache-long-line-scans is non-nil), newline_cache
157 points to the newline cache, and width_run_cache points to the
160 The newline cache records which stretches of the buffer are
161 known *not* to contain newlines, so that they can be skipped
162 quickly when we search for newlines.
164 The width run cache records which stretches of the buffer are
165 known to contain characters whose widths are all the same. If
166 the width run cache maps a character to a value > 0, that value
167 is the character's width; if it maps a character to zero, we
168 don't know what its width is. This allows compute_motion to
169 process such regions very quickly, using algebra instead of
170 inspecting each character. See also width_table, below. */
171 struct region_cache *newline_cache;
172 struct region_cache *width_run_cache;
173 #endif /* REGION_CACHE_NEEDS_WORK */
175 /* The markers that refer to this buffer. This is actually a single
176 marker -- successive elements in its marker `chain' are the other
177 markers referring to this buffer */
178 Lisp_Marker *markers;
180 /* The buffer's extent info. This is its own type, an extent-info
181 object (done this way for ease in marking / finalizing). */
182 Lisp_Object extent_info;
184 /* ----------------------------------------------------------------- */
185 /* All the stuff above this line is the responsibility of insdel.c,
186 with some help from marker.c and extents.c.
187 All the stuff below this line is the responsibility of buffer.c. */
189 /* In an indirect buffer, this points to the base buffer.
190 In an ordinary buffer, it is 0.
191 We DO mark through this slot. */
192 struct buffer *base_buffer;
194 /* List of indirect buffers whose base is this buffer.
195 If we are an indirect buffer, this will be nil.
196 Do NOT mark through this. */
197 Lisp_Object indirect_children;
199 /* Flags saying which DEFVAR_PER_BUFFER variables
200 are local to this buffer. */
203 /* Set to the modtime of the visited file when read or written.
204 -1 means visited file was nonexistent.
205 0 means visited file modtime unknown; in no case complain
206 about any mismatch on next save attempt. */
209 /* the value of text->modiff at the last auto-save. */
210 long auto_save_modified;
212 /* The time at which we detected a failure to auto-save,
213 Or -1 if we didn't have a failure. */
214 int auto_save_failure_time;
216 /* Position in buffer at which display started
217 the last time this buffer was displayed. */
218 int last_window_start;
220 /* Everything from here down must be a Lisp_Object */
222 #define MARKED_SLOT(x) Lisp_Object x
223 #include "bufslots.h"
227 DECLARE_LRECORD (buffer, struct buffer);
228 #define XBUFFER(x) XRECORD (x, buffer, struct buffer)
229 #define XSETBUFFER(x, p) XSETRECORD (x, p, buffer)
230 #define BUFFERP(x) RECORDP (x, buffer)
231 #define CHECK_BUFFER(x) CHECK_RECORD (x, buffer)
232 #define CONCHECK_BUFFER(x) CONCHECK_RECORD (x, buffer)
234 #define BUFFER_LIVE_P(b) (!NILP ((b)->name))
236 #define CHECK_LIVE_BUFFER(x) do { \
238 if (!BUFFER_LIVE_P (XBUFFER (x))) \
239 dead_wrong_type_argument (Qbuffer_live_p, (x)); \
242 #define CONCHECK_LIVE_BUFFER(x) do { \
243 CONCHECK_BUFFER (x); \
244 if (!BUFFER_LIVE_P (XBUFFER (x))) \
245 x = wrong_type_argument (Qbuffer_live_p, (x)); \
249 #define BUFFER_BASE_BUFFER(b) ((b)->base_buffer ? (b)->base_buffer : (b))
251 /* Map over buffers sharing the same text as MPS_BUF. MPS_BUFVAR is a
252 variable that gets the buffer values (beginning with the base
253 buffer, then the children), and MPS_BUFCONS should be a temporary
254 Lisp_Object variable. */
255 #define MAP_INDIRECT_BUFFERS(mps_buf, mps_bufvar, mps_bufcons) \
256 for (mps_bufcons = Qunbound, \
257 mps_bufvar = BUFFER_BASE_BUFFER (mps_buf); \
258 UNBOUNDP (mps_bufcons) ? \
259 (mps_bufcons = mps_bufvar->indirect_children, \
261 : (!NILP (mps_bufcons) \
262 && (mps_bufvar = XBUFFER (XCAR (mps_bufcons)), 1) \
263 && (mps_bufcons = XCDR (mps_bufcons), 1)); \
268 /************************************************************************/
270 /* working with raw internal-format data */
272 /************************************************************************/
274 /* NOTE: In all the following macros, we follow these rules concerning
275 multiple evaluation of the arguments:
277 1) Anything that's an lvalue can be evaluated more than once.
278 2) Anything that's a Lisp Object can be evaluated more than once.
279 This should probably be changed, but this follows the way
280 that all the macros in lisp.h do things.
281 3) 'struct buffer *' arguments can be evaluated more than once.
282 4) Nothing else can be evaluated more than once. Use inline
283 functions, if necessary, to prevent multiple evaluation.
284 5) An exception to (4) is that there are some macros below that
285 may evaluate their arguments more than once. They are all
286 denoted with the word "unsafe" in their name and are generally
287 meant to be called only by other macros that have already
288 stored the calling values in temporary variables.
292 /*----------------------------------------------------------------------*/
293 /* Accessor macros for important positions in a buffer */
294 /*----------------------------------------------------------------------*/
296 /* We put them here because some stuff below wants them before the
297 place where we would normally put them. */
299 /* None of these are lvalues. Use the settor macros below to change
302 /* Beginning of buffer. */
303 #define BI_BUF_BEG(buf) ((Bytind) 1)
304 #define BUF_BEG(buf) ((Bufpos) 1)
306 /* Beginning of accessible range of buffer. */
307 #define BI_BUF_BEGV(buf) ((buf)->begv + 0)
308 #define BUF_BEGV(buf) ((buf)->bufbegv + 0)
310 /* End of accessible range of buffer. */
311 #define BI_BUF_ZV(buf) ((buf)->zv + 0)
312 #define BUF_ZV(buf) ((buf)->bufzv + 0)
315 #define BI_BUF_Z(buf) ((buf)->text->z + 0)
316 #define BUF_Z(buf) ((buf)->text->bufz + 0)
319 #define BI_BUF_PT(buf) ((buf)->pt + 0)
320 #define BUF_PT(buf) ((buf)->bufpt + 0)
322 /*----------------------------------------------------------------------*/
323 /* Converting between positions and addresses */
324 /*----------------------------------------------------------------------*/
326 /* Convert the address of a byte in the buffer into a position. */
327 INLINE_HEADER Bytind BI_BUF_PTR_BYTE_POS (struct buffer *buf, Bufbyte *ptr);
329 BI_BUF_PTR_BYTE_POS (struct buffer *buf, Bufbyte *ptr)
331 return (ptr - buf->text->beg + 1
332 - ((ptr - buf->text->beg + 1) > buf->text->gpt
333 ? buf->text->gap_size : 0));
336 #define BUF_PTR_BYTE_POS(buf, ptr) \
337 bytind_to_bufpos (buf, BI_BUF_PTR_BYTE_POS (buf, ptr))
339 /* Address of byte at position POS in buffer. */
340 INLINE_HEADER Bufbyte * BI_BUF_BYTE_ADDRESS (struct buffer *buf, Bytind pos);
341 INLINE_HEADER Bufbyte *
342 BI_BUF_BYTE_ADDRESS (struct buffer *buf, Bytind pos)
344 return (buf->text->beg +
345 ((pos >= buf->text->gpt ? (pos + buf->text->gap_size) : pos)
349 #define BUF_BYTE_ADDRESS(buf, pos) \
350 BI_BUF_BYTE_ADDRESS (buf, bufpos_to_bytind (buf, pos))
352 /* Address of byte before position POS in buffer. */
353 INLINE_HEADER Bufbyte * BI_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytind pos);
354 INLINE_HEADER Bufbyte *
355 BI_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytind pos)
357 return (buf->text->beg +
358 ((pos > buf->text->gpt ? (pos + buf->text->gap_size) : pos)
362 #define BUF_BYTE_ADDRESS_BEFORE(buf, pos) \
363 BI_BUF_BYTE_ADDRESS_BEFORE (buf, bufpos_to_bytind (buf, pos))
365 /*----------------------------------------------------------------------*/
366 /* Converting between byte indices and memory indices */
367 /*----------------------------------------------------------------------*/
369 INLINE_HEADER int valid_memind_p (struct buffer *buf, Memind x);
371 valid_memind_p (struct buffer *buf, Memind x)
373 return ((x >= 1 && x <= (Memind) buf->text->gpt) ||
374 (x > (Memind) (buf->text->gpt + buf->text->gap_size) &&
375 x <= (Memind) (buf->text->z + buf->text->gap_size)));
378 INLINE_HEADER Memind bytind_to_memind (struct buffer *buf, Bytind x);
380 bytind_to_memind (struct buffer *buf, Bytind x)
382 return (Memind) ((x > buf->text->gpt) ? (x + buf->text->gap_size) : x);
386 INLINE_HEADER Bytind memind_to_bytind (struct buffer *buf, Memind x);
388 memind_to_bytind (struct buffer *buf, Memind x)
390 #ifdef ERROR_CHECK_BUFPOS
391 assert (valid_memind_p (buf, x));
393 return (Bytind) ((x > (Memind) buf->text->gpt) ?
394 x - buf->text->gap_size :
398 #define memind_to_bufpos(buf, x) \
399 bytind_to_bufpos (buf, memind_to_bytind (buf, x))
400 #define bufpos_to_memind(buf, x) \
401 bytind_to_memind (buf, bufpos_to_bytind (buf, x))
403 /* These macros generalize many standard buffer-position functions to
404 either a buffer or a string. */
406 /* Converting between Meminds and Bytinds, for a buffer-or-string.
407 For strings, this is a no-op. For buffers, this resolves
408 to the standard memind<->bytind converters. */
410 #define buffer_or_string_bytind_to_memind(obj, ind) \
411 (BUFFERP (obj) ? bytind_to_memind (XBUFFER (obj), ind) : (Memind) ind)
413 #define buffer_or_string_memind_to_bytind(obj, ind) \
414 (BUFFERP (obj) ? memind_to_bytind (XBUFFER (obj), ind) : (Bytind) ind)
416 /* Converting between Bufpos's and Bytinds, for a buffer-or-string.
417 For strings, this maps to the bytecount<->charcount converters. */
419 #define buffer_or_string_bufpos_to_bytind(obj, pos) \
420 (BUFFERP (obj) ? bufpos_to_bytind (XBUFFER (obj), pos) : \
421 (Bytind) charcount_to_bytecount (XSTRING_DATA (obj), pos))
423 #define buffer_or_string_bytind_to_bufpos(obj, ind) \
424 (BUFFERP (obj) ? bytind_to_bufpos (XBUFFER (obj), ind) : \
425 (Bufpos) bytecount_to_charcount (XSTRING_DATA (obj), ind))
427 /* Similar for Bufpos's and Meminds. */
429 #define buffer_or_string_bufpos_to_memind(obj, pos) \
430 (BUFFERP (obj) ? bufpos_to_memind (XBUFFER (obj), pos) : \
431 (Memind) charcount_to_bytecount (XSTRING_DATA (obj), pos))
433 #define buffer_or_string_memind_to_bufpos(obj, ind) \
434 (BUFFERP (obj) ? memind_to_bufpos (XBUFFER (obj), ind) : \
435 (Bufpos) bytecount_to_charcount (XSTRING_DATA (obj), ind))
437 /************************************************************************/
439 /* working with buffer-level data */
441 /************************************************************************/
445 (A) Working with byte indices:
446 ------------------------------
448 VALID_BYTIND_P(buf, bi):
449 Given a byte index, does it point to the beginning of a character?
451 ASSERT_VALID_BYTIND_UNSAFE(buf, bi):
452 If error-checking is enabled, assert that the given byte index
453 is within range and points to the beginning of a character
454 or to the end of the buffer. Otherwise, do nothing.
456 ASSERT_VALID_BYTIND_BACKWARD_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 beginning of the buffer. (i.e. movement
460 backwards is OK.) Otherwise, do nothing.
462 ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, bi):
463 If error-checking is enabled, assert that the given byte index
464 is within range and satisfies ASSERT_VALID_BYTIND() and also
465 does not refer to the end of the buffer. (i.e. movement
466 forwards is OK.) Otherwise, do nothing.
468 VALIDATE_BYTIND_BACKWARD(buf, bi):
469 Make sure that the given byte index is pointing to the beginning
470 of a character. If not, back up until this is the case. Note
471 that there are not too many places where it is legitimate to do
472 this sort of thing. It's an error if you're passed an "invalid"
475 VALIDATE_BYTIND_FORWARD(buf, bi):
476 Make sure that the given byte index is pointing to the beginning
477 of a character. If not, move forward until this is the case.
478 Note that there are not too many places where it is legitimate
479 to do this sort of thing. It's an error if you're passed an
480 "invalid" byte index.
483 Given a byte index (assumed to point at the beginning of a
484 character), modify that value so it points to the beginning
485 of the next character.
488 Given a byte index (assumed to point at the beginning of a
489 character), modify that value so it points to the beginning
490 of the previous character. Unlike for DEC_CHARPTR(), we can
491 do all the assert()s because there are sentinels at the
492 beginning of the gap and the end of the buffer.
495 A constant representing an invalid Bytind. Valid Bytinds
496 can never have this value.
499 (B) Converting between Bufpos's and Bytinds:
500 --------------------------------------------
502 bufpos_to_bytind(buf, bu):
503 Given a Bufpos, return the equivalent Bytind.
505 bytind_to_bufpos(buf, bi):
506 Given a Bytind, return the equivalent Bufpos.
508 make_bufpos(buf, bi):
509 Given a Bytind, return the equivalent Bufpos as a Lisp Object.
513 /*----------------------------------------------------------------------*/
514 /* working with byte indices */
515 /*----------------------------------------------------------------------*/
518 # define VALID_BYTIND_P(buf, x) \
519 BUFBYTE_FIRST_BYTE_P (*BI_BUF_BYTE_ADDRESS (buf, x))
521 # define VALID_BYTIND_P(buf, x) 1
524 #ifdef ERROR_CHECK_BUFPOS
526 # define ASSERT_VALID_BYTIND_UNSAFE(buf, x) do { \
527 assert (BUFFER_LIVE_P (buf)); \
528 assert ((x) >= BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf)); \
529 assert (VALID_BYTIND_P (buf, x)); \
531 # define ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, x) do { \
532 assert (BUFFER_LIVE_P (buf)); \
533 assert ((x) > BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf)); \
534 assert (VALID_BYTIND_P (buf, x)); \
536 # define ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, x) do { \
537 assert (BUFFER_LIVE_P (buf)); \
538 assert ((x) >= BI_BUF_BEG (buf) && x < BI_BUF_Z (buf)); \
539 assert (VALID_BYTIND_P (buf, x)); \
542 #else /* not ERROR_CHECK_BUFPOS */
543 # define ASSERT_VALID_BYTIND_UNSAFE(buf, x)
544 # define ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, x)
545 # define ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, x)
547 #endif /* not ERROR_CHECK_BUFPOS */
549 /* Note that, although the Mule version will work fine for non-Mule
550 as well (it should reduce down to nothing), we provide a separate
551 version to avoid compilation warnings and possible non-optimal
552 results with stupid compilers. */
555 # define VALIDATE_BYTIND_BACKWARD(buf, x) do { \
556 Bufbyte *VBB_ptr = BI_BUF_BYTE_ADDRESS (buf, x); \
557 while (!BUFBYTE_FIRST_BYTE_P (*VBB_ptr)) \
561 # define VALIDATE_BYTIND_BACKWARD(buf, x)
564 /* Note that, although the Mule version will work fine for non-Mule
565 as well (it should reduce down to nothing), we provide a separate
566 version to avoid compilation warnings and possible non-optimal
567 results with stupid compilers. */
570 # define VALIDATE_BYTIND_FORWARD(buf, x) do { \
571 Bufbyte *VBF_ptr = BI_BUF_BYTE_ADDRESS (buf, x); \
572 while (!BUFBYTE_FIRST_BYTE_P (*VBF_ptr)) \
576 # define VALIDATE_BYTIND_FORWARD(buf, x)
579 /* Note that in the simplest case (no MULE, no ERROR_CHECK_BUFPOS),
580 this crap reduces down to simply (x)++. */
582 #define INC_BYTIND(buf, x) do \
584 ASSERT_VALID_BYTIND_FORWARD_UNSAFE (buf, x); \
585 /* Note that we do the increment first to \
586 make sure that the pointer in \
587 VALIDATE_BYTIND_FORWARD() ends up on \
588 the correct side of the gap */ \
590 VALIDATE_BYTIND_FORWARD (buf, x); \
593 /* Note that in the simplest case (no MULE, no ERROR_CHECK_BUFPOS),
594 this crap reduces down to simply (x)--. */
596 #define DEC_BYTIND(buf, x) do \
598 ASSERT_VALID_BYTIND_BACKWARD_UNSAFE (buf, x); \
599 /* Note that we do the decrement first to \
600 make sure that the pointer in \
601 VALIDATE_BYTIND_BACKWARD() ends up on \
602 the correct side of the gap */ \
604 VALIDATE_BYTIND_BACKWARD (buf, x); \
607 INLINE_HEADER Bytind prev_bytind (struct buffer *buf, Bytind x);
609 prev_bytind (struct buffer *buf, Bytind x)
615 INLINE_HEADER Bytind next_bytind (struct buffer *buf, Bytind x);
617 next_bytind (struct buffer *buf, Bytind x)
623 #define BYTIND_INVALID ((Bytind) -1)
625 /*----------------------------------------------------------------------*/
626 /* Converting between buffer positions and byte indices */
627 /*----------------------------------------------------------------------*/
631 Bytind bufpos_to_bytind_func (struct buffer *buf, Bufpos x);
632 Bufpos bytind_to_bufpos_func (struct buffer *buf, Bytind x);
634 /* The basic algorithm we use is to keep track of a known region of
635 characters in each buffer, all of which are of the same width. We
636 keep track of the boundaries of the region in both Bufpos and
637 Bytind coordinates and also keep track of the char width, which
638 is 1 - 4 bytes. If the position we're translating is not in
639 the known region, then we invoke a function to update the known
640 region to surround the position in question. This assumes
641 locality of reference, which is usually the case.
643 Note that the function to update the known region can be simple
644 or complicated depending on how much information we cache.
645 For the moment, we don't cache any information, and just move
646 linearly forward or back from the known region, with a few
647 shortcuts to catch all-ASCII buffers. (Note that this will
648 thrash with bad locality of reference.) A smarter method would
649 be to keep some sort of pseudo-extent layer over the buffer;
650 maybe keep track of the bufpos/bytind correspondence at the
651 beginning of each line, which would allow us to do a binary
652 search over the pseudo-extents to narrow things down to the
653 correct line, at which point you could use a linear movement
654 method. This would also mesh well with efficiently
655 implementing a line-numbering scheme.
657 Note also that we have to multiply or divide by the char width
658 in order to convert the positions. We do some tricks to avoid
659 ever actually having to do a multiply or divide, because that
660 is typically an expensive operation (esp. divide). Multiplying
661 or dividing by 1, 2, or 4 can be implemented simply as a
662 shift left or shift right, and we keep track of a shifter value
663 (0, 1, or 2) indicating how much to shift. Multiplying by 3
664 can be implemented by doubling and then adding the original
665 value. Dividing by 3, alas, cannot be implemented in any
666 simple shift/subtract method, as far as I know; so we just
667 do a table lookup. For simplicity, we use a table of size
668 128K, which indexes the "divide-by-3" values for the first
669 64K non-negative numbers. (Note that we can increase the
670 size up to 384K, i.e. indexing the first 192K non-negative
671 numbers, while still using shorts in the array.) This also
672 means that the size of the known region can be at most
673 64K for width-three characters.
677 extern short three_to_one_table[];
680 INLINE_HEADER int real_bufpos_to_bytind (struct buffer *buf, Bufpos x);
682 real_bufpos_to_bytind (struct buffer *buf, Bufpos x)
684 if (x >= buf->text->mule_bufmin && x <= buf->text->mule_bufmax)
685 return (buf->text->mule_bytmin +
687 (x - buf->text->mule_bufmin) * buf->text->mule_size
689 ((x - buf->text->mule_bufmin) << buf->text->mule_shifter) +
690 (buf->text->mule_three_p ? (x - buf->text->mule_bufmin) : 0)
694 return bufpos_to_bytind_func (buf, x);
697 INLINE_HEADER int real_bytind_to_bufpos (struct buffer *buf, Bytind x);
699 real_bytind_to_bufpos (struct buffer *buf, Bytind x)
701 if (x >= buf->text->mule_bytmin && x <= buf->text->mule_bytmax)
702 return (buf->text->mule_bufmin +
704 (buf->text->mule_size == 0 ? 0 :
705 (x - buf->text->mule_bytmin) / buf->text->mule_size)
707 ((buf->text->mule_three_p
708 ? three_to_one_table[x - buf->text->mule_bytmin]
709 : (x - buf->text->mule_bytmin) >> buf->text->mule_shifter))
713 return bytind_to_bufpos_func (buf, x);
718 # define real_bufpos_to_bytind(buf, x) ((Bytind) x)
719 # define real_bytind_to_bufpos(buf, x) ((Bufpos) x)
721 #endif /* not MULE */
723 #ifdef ERROR_CHECK_BUFPOS
725 Bytind bufpos_to_bytind (struct buffer *buf, Bufpos x);
726 Bufpos bytind_to_bufpos (struct buffer *buf, Bytind x);
728 #else /* not ERROR_CHECK_BUFPOS */
730 #define bufpos_to_bytind real_bufpos_to_bytind
731 #define bytind_to_bufpos real_bytind_to_bufpos
733 #endif /* not ERROR_CHECK_BUFPOS */
735 #define make_bufpos(buf, ind) make_int (bytind_to_bufpos (buf, ind))
737 /*----------------------------------------------------------------------*/
738 /* Converting between buffer bytes and Emacs characters */
739 /*----------------------------------------------------------------------*/
741 /* The character at position POS in buffer. */
742 #define BI_BUF_FETCH_CHAR(buf, pos) \
743 charptr_emchar (BI_BUF_BYTE_ADDRESS (buf, pos))
744 #define BUF_FETCH_CHAR(buf, pos) \
745 BI_BUF_FETCH_CHAR (buf, bufpos_to_bytind (buf, pos))
747 /* The character at position POS in buffer, as a string. This is
748 equivalent to set_charptr_emchar (str, BUF_FETCH_CHAR (buf, pos))
749 but is faster for Mule. */
751 # define BI_BUF_CHARPTR_COPY_CHAR(buf, pos, str) \
752 charptr_copy_char (BI_BUF_BYTE_ADDRESS (buf, pos), str)
753 #define BUF_CHARPTR_COPY_CHAR(buf, pos, str) \
754 BI_BUF_CHARPTR_COPY_CHAR (buf, bufpos_to_bytind (buf, pos), str)
757 /************************************************************************/
759 /* Converting between internal and external format */
761 /************************************************************************/
763 All client code should use only the two macros
765 TO_EXTERNAL_FORMAT (source_type, source, sink_type, sink, coding_system)
766 TO_INTERNAL_FORMAT (source_type, source, sink_type, sink, coding_system)
770 TO_EXTERNAL_FORMAT (DATA, (ptr, len),
774 The source or sink can be specified in one of these ways:
776 DATA, (ptr, len), // input data is a fixed buffer of size len
777 ALLOCA, (ptr, len), // output data is in a alloca()ed buffer of size len
778 MALLOC, (ptr, len), // output data is in a malloc()ed buffer of size len
779 C_STRING_ALLOCA, ptr, // equivalent to ALLOCA (ptr, len_ignored) on output
780 C_STRING_MALLOC, ptr, // equivalent to MALLOC (ptr, len_ignored) on output
781 C_STRING, ptr, // equivalent to DATA, (ptr, strlen (ptr) + 1) on input
782 LISP_STRING, string, // input or output is a Lisp_Object of type string
783 LISP_BUFFER, buffer, // output is written to (point) in lisp buffer
784 LISP_LSTREAM, lstream, // input or output is a Lisp_Object of type lstream
785 LISP_OPAQUE, object, // input or output is a Lisp_Object of type opaque
787 When specifying the sink, use lvalues, since the macro will assign to them,
788 except when the sink is an lstream or a lisp buffer.
790 The macros accept the kinds of sources and sinks appropriate for
791 internal and external data representation. See the type_checking_assert
792 macros below for the actual allowed types.
794 Since some sources and sinks use one argument (a Lisp_Object) to
795 specify them, while others take a (pointer, length) pair, we use
796 some C preprocessor trickery to allow pair arguments to be specified
797 by parenthesizing them, as in the examples above.
799 Anything prefixed by dfc_ (`data format conversion') is private.
800 They are only used to implement these macros.
802 Using C_STRING* is appropriate for using with external APIs that take
803 null-terminated strings. For internal data, we should try to be
804 '\0'-clean - i.e. allow arbitrary data to contain embedded '\0'.
806 Sometime in the future we might allow output to C_STRING_ALLOCA or
807 C_STRING_MALLOC _only_ with TO_EXTERNAL_FORMAT(), not
808 TO_INTERNAL_FORMAT(). */
810 #define TO_EXTERNAL_FORMAT(source_type, source, sink_type, sink, coding_system) \
812 dfc_conversion_type dfc_simplified_source_type; \
813 dfc_conversion_type dfc_simplified_sink_type; \
814 dfc_conversion_data dfc_source; \
815 dfc_conversion_data dfc_sink; \
817 type_checking_assert \
818 ((DFC_TYPE_##source_type == DFC_TYPE_DATA || \
819 DFC_TYPE_##source_type == DFC_TYPE_C_STRING || \
820 DFC_TYPE_##source_type == DFC_TYPE_LISP_STRING || \
821 DFC_TYPE_##source_type == DFC_TYPE_LISP_OPAQUE || \
822 DFC_TYPE_##source_type == DFC_TYPE_LISP_LSTREAM) \
824 (DFC_TYPE_##sink_type == DFC_TYPE_ALLOCA || \
825 DFC_TYPE_##sink_type == DFC_TYPE_MALLOC || \
826 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_ALLOCA || \
827 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_MALLOC || \
828 DFC_TYPE_##sink_type == DFC_TYPE_LISP_LSTREAM || \
829 DFC_TYPE_##sink_type == DFC_TYPE_LISP_OPAQUE)); \
831 DFC_SOURCE_##source_type##_TO_ARGS (source); \
832 DFC_SINK_##sink_type##_TO_ARGS (sink); \
834 DFC_CONVERT_TO_EXTERNAL_FORMAT (dfc_simplified_source_type, &dfc_source, \
836 dfc_simplified_sink_type, &dfc_sink); \
838 DFC_##sink_type##_USE_CONVERTED_DATA (sink); \
841 #define TO_INTERNAL_FORMAT(source_type, source, sink_type, sink, coding_system) \
843 dfc_conversion_type dfc_simplified_source_type; \
844 dfc_conversion_type dfc_simplified_sink_type; \
845 dfc_conversion_data dfc_source; \
846 dfc_conversion_data dfc_sink; \
848 type_checking_assert \
849 ((DFC_TYPE_##source_type == DFC_TYPE_DATA || \
850 DFC_TYPE_##source_type == DFC_TYPE_C_STRING || \
851 DFC_TYPE_##source_type == DFC_TYPE_LISP_OPAQUE || \
852 DFC_TYPE_##source_type == DFC_TYPE_LISP_LSTREAM) \
854 (DFC_TYPE_##sink_type == DFC_TYPE_ALLOCA || \
855 DFC_TYPE_##sink_type == DFC_TYPE_MALLOC || \
856 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_ALLOCA || \
857 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_MALLOC || \
858 DFC_TYPE_##sink_type == DFC_TYPE_LISP_STRING || \
859 DFC_TYPE_##sink_type == DFC_TYPE_LISP_LSTREAM || \
860 DFC_TYPE_##sink_type == DFC_TYPE_LISP_BUFFER)); \
862 DFC_SOURCE_##source_type##_TO_ARGS (source); \
863 DFC_SINK_##sink_type##_TO_ARGS (sink); \
865 DFC_CONVERT_TO_INTERNAL_FORMAT (dfc_simplified_source_type, &dfc_source, \
867 dfc_simplified_sink_type, &dfc_sink); \
869 DFC_##sink_type##_USE_CONVERTED_DATA (sink); \
873 #define DFC_CONVERT_TO_EXTERNAL_FORMAT dfc_convert_to_external_format
874 #define DFC_CONVERT_TO_INTERNAL_FORMAT dfc_convert_to_internal_format
876 /* ignore coding_system argument */
877 #define DFC_CONVERT_TO_EXTERNAL_FORMAT(a, b, coding_system, c, d) \
878 dfc_convert_to_external_format (a, b, c, d)
879 #define DFC_CONVERT_TO_INTERNAL_FORMAT(a, b, coding_system, c, d) \
880 dfc_convert_to_internal_format (a, b, c, d)
885 struct { const void *ptr; size_t len; } data;
886 Lisp_Object lisp_object;
887 } dfc_conversion_data;
889 enum dfc_conversion_type
895 DFC_TYPE_C_STRING_ALLOCA,
896 DFC_TYPE_C_STRING_MALLOC,
897 DFC_TYPE_LISP_STRING,
898 DFC_TYPE_LISP_LSTREAM,
899 DFC_TYPE_LISP_OPAQUE,
902 typedef enum dfc_conversion_type dfc_conversion_type;
904 /* WARNING: These use a static buffer. This can lead to disaster if
905 these functions are not used *very* carefully. Another reason to only use
906 TO_EXTERNAL_FORMAT() and TO_INTERNAL_FORMAT(). */
908 dfc_convert_to_external_format (dfc_conversion_type source_type,
909 dfc_conversion_data *source,
911 Lisp_Object coding_system,
913 dfc_conversion_type sink_type,
914 dfc_conversion_data *sink);
916 dfc_convert_to_internal_format (dfc_conversion_type source_type,
917 dfc_conversion_data *source,
919 Lisp_Object coding_system,
921 dfc_conversion_type sink_type,
922 dfc_conversion_data *sink);
924 #define DFC_CPP_CAR(x,y) (x)
925 #define DFC_CPP_CDR(x,y) (y)
927 /* Convert `source' to args for dfc_convert_to_*_format() */
928 #define DFC_SOURCE_DATA_TO_ARGS(val) do { \
929 dfc_source.data.ptr = DFC_CPP_CAR val; \
930 dfc_source.data.len = DFC_CPP_CDR val; \
931 dfc_simplified_source_type = DFC_TYPE_DATA; \
933 #define DFC_SOURCE_C_STRING_TO_ARGS(val) do { \
934 dfc_source.data.len = \
935 strlen ((char *) (dfc_source.data.ptr = (val))); \
936 dfc_simplified_source_type = DFC_TYPE_DATA; \
938 #define DFC_SOURCE_LISP_STRING_TO_ARGS(val) do { \
939 Lisp_Object dfc_slsta = (val); \
940 type_checking_assert (STRINGP (dfc_slsta)); \
941 dfc_source.lisp_object = dfc_slsta; \
942 dfc_simplified_source_type = DFC_TYPE_LISP_STRING; \
944 #define DFC_SOURCE_LISP_LSTREAM_TO_ARGS(val) do { \
945 Lisp_Object dfc_sllta = (val); \
946 type_checking_assert (LSTREAMP (dfc_sllta)); \
947 dfc_source.lisp_object = dfc_sllta; \
948 dfc_simplified_source_type = DFC_TYPE_LISP_LSTREAM; \
950 #define DFC_SOURCE_LISP_OPAQUE_TO_ARGS(val) do { \
951 Lisp_Opaque *dfc_slota = XOPAQUE (val); \
952 dfc_source.data.ptr = OPAQUE_DATA (dfc_slota); \
953 dfc_source.data.len = OPAQUE_SIZE (dfc_slota); \
954 dfc_simplified_source_type = DFC_TYPE_DATA; \
957 /* Convert `sink' to args for dfc_convert_to_*_format() */
958 #define DFC_SINK_ALLOCA_TO_ARGS(val) \
959 dfc_simplified_sink_type = DFC_TYPE_DATA
960 #define DFC_SINK_C_STRING_ALLOCA_TO_ARGS(val) \
961 dfc_simplified_sink_type = DFC_TYPE_DATA
962 #define DFC_SINK_MALLOC_TO_ARGS(val) \
963 dfc_simplified_sink_type = DFC_TYPE_DATA
964 #define DFC_SINK_C_STRING_MALLOC_TO_ARGS(val) \
965 dfc_simplified_sink_type = DFC_TYPE_DATA
966 #define DFC_SINK_LISP_STRING_TO_ARGS(val) \
967 dfc_simplified_sink_type = DFC_TYPE_DATA
968 #define DFC_SINK_LISP_OPAQUE_TO_ARGS(val) \
969 dfc_simplified_sink_type = DFC_TYPE_DATA
970 #define DFC_SINK_LISP_LSTREAM_TO_ARGS(val) do { \
971 Lisp_Object dfc_sllta = (val); \
972 type_checking_assert (LSTREAMP (dfc_sllta)); \
973 dfc_sink.lisp_object = dfc_sllta; \
974 dfc_simplified_sink_type = DFC_TYPE_LISP_LSTREAM; \
976 #define DFC_SINK_LISP_BUFFER_TO_ARGS(val) do { \
977 struct buffer *dfc_slbta = XBUFFER (val); \
978 dfc_sink.lisp_object = \
979 make_lisp_buffer_output_stream \
980 (dfc_slbta, BUF_PT (dfc_slbta), 0); \
981 dfc_simplified_sink_type = DFC_TYPE_LISP_LSTREAM; \
984 /* Assign to the `sink' lvalue(s) using the converted data. */
985 typedef union { char c; void *p; } *dfc_aliasing_voidpp;
986 #define DFC_ALLOCA_USE_CONVERTED_DATA(sink) do { \
987 void * dfc_sink_ret = alloca (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_MALLOC_USE_CONVERTED_DATA(sink) do { \
993 void * dfc_sink_ret = xmalloc (dfc_sink.data.len + 1); \
994 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1); \
995 ((dfc_aliasing_voidpp) &(DFC_CPP_CAR sink))->p = dfc_sink_ret; \
996 (DFC_CPP_CDR sink) = dfc_sink.data.len; \
998 #define DFC_C_STRING_ALLOCA_USE_CONVERTED_DATA(sink) do { \
999 void * dfc_sink_ret = alloca (dfc_sink.data.len + 1); \
1000 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1); \
1001 (sink) = (char *) dfc_sink_ret; \
1003 #define DFC_C_STRING_MALLOC_USE_CONVERTED_DATA(sink) do { \
1004 void * dfc_sink_ret = xmalloc (dfc_sink.data.len + 1); \
1005 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1); \
1006 (sink) = (char *) dfc_sink_ret; \
1008 #define DFC_LISP_STRING_USE_CONVERTED_DATA(sink) \
1009 sink = make_string ((Bufbyte *) dfc_sink.data.ptr, dfc_sink.data.len)
1010 #define DFC_LISP_OPAQUE_USE_CONVERTED_DATA(sink) \
1011 sink = make_opaque (dfc_sink.data.ptr, dfc_sink.data.len)
1012 #define DFC_LISP_LSTREAM_USE_CONVERTED_DATA(sink) /* data already used */
1013 #define DFC_LISP_BUFFER_USE_CONVERTED_DATA(sink) \
1014 Lstream_delete (XLSTREAM (dfc_sink.lisp_object))
1016 /* Someday we might want to distinguish between Qnative and Qfile_name
1017 by using coding-system aliases, but for now it suffices to have
1018 these be identical. Qnative can be used as the coding_system
1019 argument to TO_EXTERNAL_FORMAT() and TO_INTERNAL_FORMAT(). */
1020 #define Qnative Qfile_name
1022 #if defined (WIN32_NATIVE) || defined (CYGWIN)
1023 /* #### kludge!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1024 Remove this as soon as my Mule code is integrated. */
1025 #define Qmswindows_tstr Qnative
1028 /* More stand-ins */
1029 #define Qcommand_argument_encoding Qnative
1030 #define Qenvironment_variable_encoding Qnative
1032 /* Convenience macros for extremely common invocations */
1033 #define C_STRING_TO_EXTERNAL(in, out, coding_system) \
1034 TO_EXTERNAL_FORMAT (C_STRING, in, C_STRING_ALLOCA, out, coding_system)
1035 #define C_STRING_TO_EXTERNAL_MALLOC(in, out, coding_system) \
1036 TO_EXTERNAL_FORMAT (C_STRING, in, C_STRING_MALLOC, out, coding_system)
1037 #define EXTERNAL_TO_C_STRING(in, out, coding_system) \
1038 TO_INTERNAL_FORMAT (C_STRING, in, C_STRING_ALLOCA, out, coding_system)
1039 #define EXTERNAL_TO_C_STRING_MALLOC(in, out, coding_system) \
1040 TO_INTERNAL_FORMAT (C_STRING, in, C_STRING_MALLOC, out, coding_system)
1041 #define LISP_STRING_TO_EXTERNAL(in, out, coding_system) \
1042 TO_EXTERNAL_FORMAT (LISP_STRING, in, C_STRING_ALLOCA, out, coding_system)
1043 #define LISP_STRING_TO_EXTERNAL_MALLOC(in, out, coding_system) \
1044 TO_EXTERNAL_FORMAT (LISP_STRING, in, C_STRING_MALLOC, out, coding_system)
1047 /************************************************************************/
1049 /* higher-level buffer-position functions */
1051 /************************************************************************/
1053 /*----------------------------------------------------------------------*/
1054 /* Settor macros for important positions in a buffer */
1055 /*----------------------------------------------------------------------*/
1057 /* Set beginning of accessible range of buffer. */
1058 #define SET_BOTH_BUF_BEGV(buf, val, bival) \
1061 (buf)->begv = (bival); \
1062 (buf)->bufbegv = (val); \
1065 /* Set end of accessible range of buffer. */
1066 #define SET_BOTH_BUF_ZV(buf, val, bival) \
1069 (buf)->zv = (bival); \
1070 (buf)->bufzv = (val); \
1074 /* Since BEGV and ZV are almost never set, it's reasonable to enforce
1075 the restriction that the Bufpos and Bytind values must both be
1076 specified. However, point is set in lots and lots of places. So
1077 we provide the ability to specify both (for efficiency) or just
1079 #define BOTH_BUF_SET_PT(buf, val, bival) set_buffer_point (buf, val, bival)
1080 #define BI_BUF_SET_PT(buf, bival) \
1081 BOTH_BUF_SET_PT (buf, bytind_to_bufpos (buf, bival), bival)
1082 #define BUF_SET_PT(buf, value) \
1083 BOTH_BUF_SET_PT (buf, value, bufpos_to_bytind (buf, value))
1087 /* These macros exist in FSFmacs because SET_PT() in FSFmacs incorrectly
1088 does too much stuff, such as moving out of invisible extents. */
1089 #define TEMP_SET_PT(position) (temp_set_point ((position), current_buffer))
1090 #define SET_BUF_PT(buf, value) ((buf)->pt = (value))
1091 #endif /* FSFmacs */
1093 /*----------------------------------------------------------------------*/
1094 /* Miscellaneous buffer values */
1095 /*----------------------------------------------------------------------*/
1097 /* Number of characters in buffer */
1098 #define BUF_SIZE(buf) (BUF_Z (buf) - BUF_BEG (buf))
1100 /* Is this buffer narrowed? */
1101 #define BUF_NARROWED(buf) \
1102 ((BI_BUF_BEGV (buf) != BI_BUF_BEG (buf)) || \
1103 (BI_BUF_ZV (buf) != BI_BUF_Z (buf)))
1105 /* Modification count. */
1106 #define BUF_MODIFF(buf) ((buf)->text->modiff)
1108 /* Saved modification count. */
1109 #define BUF_SAVE_MODIFF(buf) ((buf)->text->save_modiff)
1112 #define BUF_FACECHANGE(buf) ((buf)->face_change)
1114 #define POINT_MARKER_P(marker) \
1115 (XMARKER (marker)->buffer != 0 && \
1116 EQ (marker, XMARKER (marker)->buffer->point_marker))
1118 #define BUF_MARKERS(buf) ((buf)->markers)
1122 The new definitions of CEILING_OF() and FLOOR_OF() differ semantically
1123 from the old ones (in FSF Emacs and XEmacs 19.11 and before).
1124 Conversion is as follows:
1126 OLD_BI_CEILING_OF(n) = NEW_BI_CEILING_OF(n) - 1
1127 OLD_BI_FLOOR_OF(n) = NEW_BI_FLOOR_OF(n + 1)
1129 The definitions were changed because the new definitions are more
1130 consistent with the way everything else works in Emacs.
1133 /* Properties of CEILING_OF and FLOOR_OF (also apply to BI_ variants):
1135 1) FLOOR_OF (CEILING_OF (n)) = n
1136 CEILING_OF (FLOOR_OF (n)) = n
1138 2) CEILING_OF (n) = n if and only if n = ZV
1139 FLOOR_OF (n) = n if and only if n = BEGV
1141 3) CEILING_OF (CEILING_OF (n)) = ZV
1142 FLOOR_OF (FLOOR_OF (n)) = BEGV
1144 4) The bytes in the regions
1146 [BYTE_ADDRESS (n), BYTE_ADDRESS_BEFORE (CEILING_OF (n))]
1150 [BYTE_ADDRESS (FLOOR_OF (n)), BYTE_ADDRESS_BEFORE (n)]
1156 /* Return the maximum index in the buffer it is safe to scan forwards
1157 past N to. This is used to prevent buffer scans from running into
1158 the gap (e.g. search.c). All characters between N and CEILING_OF(N)
1159 are located contiguous in memory. Note that the character *at*
1160 CEILING_OF(N) is not contiguous in memory. */
1161 #define BI_BUF_CEILING_OF(b, n) \
1162 ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_ZV (b) ? \
1163 (b)->text->gpt : BI_BUF_ZV (b))
1164 #define BUF_CEILING_OF(b, n) \
1165 bytind_to_bufpos (b, BI_BUF_CEILING_OF (b, bufpos_to_bytind (b, n)))
1167 /* Return the minimum index in the buffer it is safe to scan backwards
1168 past N to. All characters between FLOOR_OF(N) and N are located
1169 contiguous in memory. Note that the character *at* N may not be
1170 contiguous in memory. */
1171 #define BI_BUF_FLOOR_OF(b, n) \
1172 (BI_BUF_BEGV (b) < (b)->text->gpt && (b)->text->gpt < (n) ? \
1173 (b)->text->gpt : BI_BUF_BEGV (b))
1174 #define BUF_FLOOR_OF(b, n) \
1175 bytind_to_bufpos (b, BI_BUF_FLOOR_OF (b, bufpos_to_bytind (b, n)))
1177 #define BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \
1178 ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_Z (b) ? \
1179 (b)->text->gpt : BI_BUF_Z (b))
1180 #define BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \
1182 (b, BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
1184 #define BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \
1185 (BI_BUF_BEG (b) < (b)->text->gpt && (b)->text->gpt < (n) ? \
1186 (b)->text->gpt : BI_BUF_BEG (b))
1187 #define BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \
1189 (b, BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
1192 extern struct buffer *current_buffer;
1194 /* This is the initial (startup) directory, as used for the *scratch* buffer.
1195 We're making this a global to make others aware of the startup directory.
1196 `initial_directory' is stored in external format.
1198 extern char initial_directory[];
1199 extern void init_initial_directory (void); /* initialize initial_directory */
1201 EXFUN (Fbuffer_disable_undo, 1);
1202 EXFUN (Fbuffer_modified_p, 1);
1203 EXFUN (Fbuffer_name, 1);
1204 EXFUN (Fcurrent_buffer, 0);
1205 EXFUN (Ferase_buffer, 1);
1206 EXFUN (Fget_buffer, 1);
1207 EXFUN (Fget_buffer_create, 1);
1208 EXFUN (Fget_file_buffer, 1);
1209 EXFUN (Fkill_buffer, 1);
1210 EXFUN (Fother_buffer, 3);
1211 EXFUN (Frecord_buffer, 1);
1212 EXFUN (Fset_buffer, 1);
1213 EXFUN (Fset_buffer_modified_p, 2);
1215 extern Lisp_Object QSscratch, Qafter_change_function, Qafter_change_functions;
1216 extern Lisp_Object Qbefore_change_function, Qbefore_change_functions;
1217 extern Lisp_Object Qbuffer_or_string_p, Qdefault_directory, Qfirst_change_hook;
1218 extern Lisp_Object Qpermanent_local, Vafter_change_function;
1219 extern Lisp_Object Vafter_change_functions, Vbefore_change_function;
1220 extern Lisp_Object Vbefore_change_functions, Vbuffer_alist, Vbuffer_defaults;
1221 extern Lisp_Object Vinhibit_read_only, Vtransient_mark_mode;
1223 /* This structure marks which slots in a buffer have corresponding
1224 default values in Vbuffer_defaults.
1225 Each such slot has a nonzero value in this structure.
1226 The value has only one nonzero bit.
1228 When a buffer has its own local value for a slot,
1229 the bit for that slot (found in the same slot in this structure)
1230 is turned on in the buffer's local_var_flags slot.
1232 If a slot in this structure is zero, then even though there may
1233 be a DEFVAR_BUFFER_LOCAL for the slot, there is no default value for it;
1234 and the corresponding slot in Vbuffer_defaults is not used. */
1236 extern struct buffer buffer_local_flags;
1239 /* Allocation of buffer data. */
1243 char *r_alloc (unsigned char **, size_t);
1244 char *r_re_alloc (unsigned char **, size_t);
1245 void r_alloc_free (unsigned char **);
1247 #define BUFFER_ALLOC(data, size) \
1248 ((Bufbyte *) r_alloc ((unsigned char **) &data, (size) * sizeof(Bufbyte)))
1249 #define BUFFER_REALLOC(data, size) \
1250 ((Bufbyte *) r_re_alloc ((unsigned char **) &data, (size) * sizeof(Bufbyte)))
1251 #define BUFFER_FREE(data) r_alloc_free ((unsigned char **) &(data))
1252 #define R_ALLOC_DECLARE(var,data) r_alloc_declare (&(var), data)
1254 #else /* !REL_ALLOC */
1256 #define BUFFER_ALLOC(data,size)\
1257 (data = xnew_array (Bufbyte, size))
1258 #define BUFFER_REALLOC(data,size)\
1259 ((Bufbyte *) xrealloc (data, (size) * sizeof(Bufbyte)))
1260 /* Avoid excess parentheses, or syntax errors may rear their heads. */
1261 #define BUFFER_FREE(data) xfree (data)
1262 #define R_ALLOC_DECLARE(var,data)
1264 #endif /* !REL_ALLOC */
1266 extern Lisp_Object Vbuffer_alist;
1267 void set_buffer_internal (struct buffer *b);
1268 struct buffer *decode_buffer (Lisp_Object buffer, int allow_string);
1270 /* from editfns.c */
1271 void widen_buffer (struct buffer *b, int no_clip);
1272 int beginning_of_line_p (struct buffer *b, Bufpos pt);
1275 void set_buffer_point (struct buffer *buf, Bufpos pos, Bytind bipos);
1276 void find_charsets_in_bufbyte_string (Charset_ID *charsets,
1279 void find_charsets_in_charc_string (Charset_ID *charsets,
1282 int bufbyte_string_displayed_columns (const Bufbyte *str, Bytecount len);
1283 int charc_string_displayed_columns (const Charc *str, Charcount len);
1284 void convert_bufbyte_string_into_charc_dynarr (const Bufbyte *str,
1287 Charcount convert_bufbyte_string_into_emchar_string (const Bufbyte *str,
1290 void convert_charc_string_into_bufbyte_dynarr (Charc *arr, int nels,
1291 Bufbyte_dynarr *dyn);
1292 Bufbyte *convert_charc_string_into_malloced_string (Charc *arr, int nels,
1293 Bytecount *len_out);
1295 void init_buffer_markers (struct buffer *b);
1296 void uninit_buffer_markers (struct buffer *b);
1298 /* flags for get_buffer_pos_char(), get_buffer_range_char(), etc. */
1299 /* At most one of GB_COERCE_RANGE and GB_NO_ERROR_IF_BAD should be
1300 specified. At most one of GB_NEGATIVE_FROM_END and GB_NO_ERROR_IF_BAD
1301 should be specified. */
1303 #define GB_ALLOW_PAST_ACCESSIBLE (1 << 0)
1304 #define GB_ALLOW_NIL (1 << 1)
1305 #define GB_CHECK_ORDER (1 << 2)
1306 #define GB_COERCE_RANGE (1 << 3)
1307 #define GB_NO_ERROR_IF_BAD (1 << 4)
1308 #define GB_NEGATIVE_FROM_END (1 << 5)
1309 #define GB_HISTORICAL_STRING_BEHAVIOR (GB_NEGATIVE_FROM_END | GB_ALLOW_NIL)
1311 Bufpos get_buffer_pos_char (struct buffer *b, Lisp_Object pos,
1312 unsigned int flags);
1313 Bytind get_buffer_pos_byte (struct buffer *b, Lisp_Object pos,
1314 unsigned int flags);
1315 void get_buffer_range_char (struct buffer *b, Lisp_Object from, Lisp_Object to,
1316 Bufpos *from_out, Bufpos *to_out,
1317 unsigned int flags);
1318 void get_buffer_range_byte (struct buffer *b, Lisp_Object from, Lisp_Object to,
1319 Bytind *from_out, Bytind *to_out,
1320 unsigned int flags);
1321 Charcount get_string_pos_char (Lisp_Object string, Lisp_Object pos,
1322 unsigned int flags);
1323 Bytecount get_string_pos_byte (Lisp_Object string, Lisp_Object pos,
1324 unsigned int flags);
1325 void get_string_range_char (Lisp_Object string, Lisp_Object from,
1326 Lisp_Object to, Charcount *from_out,
1327 Charcount *to_out, unsigned int flags);
1328 void get_string_range_byte (Lisp_Object string, Lisp_Object from,
1329 Lisp_Object to, Bytecount *from_out,
1330 Bytecount *to_out, unsigned int flags);
1331 Bufpos get_buffer_or_string_pos_char (Lisp_Object object, Lisp_Object pos,
1332 unsigned int flags);
1333 Bytind get_buffer_or_string_pos_byte (Lisp_Object object, Lisp_Object pos,
1334 unsigned int flags);
1335 void get_buffer_or_string_range_char (Lisp_Object object, Lisp_Object from,
1336 Lisp_Object to, Bufpos *from_out,
1337 Bufpos *to_out, unsigned int flags);
1338 void get_buffer_or_string_range_byte (Lisp_Object object, Lisp_Object from,
1339 Lisp_Object to, Bytind *from_out,
1340 Bytind *to_out, unsigned int flags);
1341 Bufpos buffer_or_string_accessible_begin_char (Lisp_Object object);
1342 Bufpos buffer_or_string_accessible_end_char (Lisp_Object object);
1343 Bytind buffer_or_string_accessible_begin_byte (Lisp_Object object);
1344 Bytind buffer_or_string_accessible_end_byte (Lisp_Object object);
1345 Bufpos buffer_or_string_absolute_begin_char (Lisp_Object object);
1346 Bufpos buffer_or_string_absolute_end_char (Lisp_Object object);
1347 Bytind buffer_or_string_absolute_begin_byte (Lisp_Object object);
1348 Bytind buffer_or_string_absolute_end_byte (Lisp_Object object);
1349 void record_buffer (Lisp_Object buf);
1350 Lisp_Object get_buffer (Lisp_Object name,
1351 int error_if_deleted_or_does_not_exist);
1352 int map_over_sharing_buffers (struct buffer *buf,
1353 int (*mapfun) (struct buffer *buf,
1358 /************************************************************************/
1359 /* Case conversion */
1360 /************************************************************************/
1362 /* A "trt" table is a mapping from characters to other characters,
1363 typically used to convert between uppercase and lowercase. For
1364 compatibility reasons, trt tables are currently in the form of
1365 a Lisp string of 256 characters, specifying the conversion for each
1366 of the first 256 Emacs characters (i.e. the 256 Latin-1 characters).
1367 This should be generalized at some point to support conversions for
1368 all of the allowable Mule characters.
1371 /* The _1 macros are named as such because they assume that you have
1372 already guaranteed that the character values are all in the range
1373 0 - 255. Bad lossage will happen otherwise. */
1375 #define MAKE_TRT_TABLE() Fmake_char_table (Qgeneric)
1376 INLINE_HEADER Emchar TRT_TABLE_CHAR_1 (Lisp_Object table, Emchar c);
1377 INLINE_HEADER Emchar
1378 TRT_TABLE_CHAR_1 (Lisp_Object table, Emchar ch)
1380 Lisp_Object TRT_char;
1381 TRT_char = get_char_table (ch, XCHAR_TABLE (table));
1382 if (NILP (TRT_char))
1385 return XCHAR (TRT_char);
1387 #define SET_TRT_TABLE_CHAR_1(table, ch1, ch2) \
1388 Fput_char_table (make_char (ch1), make_char (ch2), table);
1390 INLINE_HEADER Emchar TRT_TABLE_OF (Lisp_Object trt, Emchar c);
1391 INLINE_HEADER Emchar
1392 TRT_TABLE_OF (Lisp_Object trt, Emchar c)
1394 return TRT_TABLE_CHAR_1 (trt, c);
1397 /* Macros used below. */
1398 #define DOWNCASE_TABLE_OF(buf, c) \
1399 TRT_TABLE_OF (XCASE_TABLE_DOWNCASE (buf->case_table), c)
1400 #define UPCASE_TABLE_OF(buf, c) \
1401 TRT_TABLE_OF (XCASE_TABLE_UPCASE (buf->case_table), c)
1403 /* 1 if CH is upper case. */
1405 INLINE_HEADER int UPPERCASEP (struct buffer *buf, Emchar ch);
1407 UPPERCASEP (struct buffer *buf, Emchar ch)
1409 return DOWNCASE_TABLE_OF (buf, ch) != ch;
1412 /* 1 if CH is lower case. */
1414 INLINE_HEADER int LOWERCASEP (struct buffer *buf, Emchar ch);
1416 LOWERCASEP (struct buffer *buf, Emchar ch)
1418 return (UPCASE_TABLE_OF (buf, ch) != ch &&
1419 DOWNCASE_TABLE_OF (buf, ch) == ch);
1422 /* 1 if CH is neither upper nor lower case. */
1424 INLINE_HEADER int NOCASEP (struct buffer *buf, Emchar ch);
1426 NOCASEP (struct buffer *buf, Emchar ch)
1428 return UPCASE_TABLE_OF (buf, ch) == ch;
1431 /* Upcase a character, or make no change if that cannot be done. */
1433 INLINE_HEADER Emchar UPCASE (struct buffer *buf, Emchar ch);
1434 INLINE_HEADER Emchar
1435 UPCASE (struct buffer *buf, Emchar ch)
1437 return (DOWNCASE_TABLE_OF (buf, ch) == ch) ? UPCASE_TABLE_OF (buf, ch) : ch;
1440 /* Upcase a character known to be not upper case. Unused. */
1442 #define UPCASE1(buf, ch) UPCASE_TABLE_OF (buf, ch)
1444 /* Downcase a character, or make no change if that cannot be done. */
1446 #define DOWNCASE(buf, ch) DOWNCASE_TABLE_OF (buf, ch)
1448 /************************************************************************/
1449 /* Lisp string representation convenience functions */
1450 /************************************************************************/
1451 /* Because the representation of internally formatted data is subject to change,
1452 It's bad style to do something like strcmp (XSTRING_DATA (s), "foo")
1453 Instead, use the portable: bufbyte_strcmp (XSTRING_DATA (s), "foo")
1454 or bufbyte_memcmp (XSTRING_DATA (s), "foo", 3) */
1456 /* Like strcmp, except first arg points at internally formatted data,
1457 while the second points at a string of only ASCII chars. */
1459 bufbyte_strcmp (const Bufbyte *bp, const char *ascii_string);
1461 bufbyte_strcmp (const Bufbyte *bp, const char *ascii_string)
1467 type_checking_assert (BYTE_ASCII_P (*ascii_string));
1468 if ((diff = charptr_emchar (bp) - *(Bufbyte *) ascii_string) != 0)
1470 if (*ascii_string == '\0')
1476 return strcmp ((char *)bp, ascii_string);
1481 /* Like memcmp, except first arg points at internally formatted data,
1482 while the second points at a string of only ASCII chars. */
1484 bufbyte_memcmp (const Bufbyte *bp, const char *ascii_string, size_t len);
1486 bufbyte_memcmp (const Bufbyte *bp, const char *ascii_string, size_t len)
1491 int diff = charptr_emchar (bp) - *(Bufbyte *) ascii_string;
1492 type_checking_assert (BYTE_ASCII_P (*ascii_string));
1500 return memcmp (bp, ascii_string, len);
1504 #endif /* INCLUDED_buffer_h_ */