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 #ifdef HAVE_MS_WINDOWS
1017 /* #### kludge!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1018 Remove this as soon as my Mule code is integrated. */
1019 #define Qmswindows_tstr Qnative
1022 /************************************************************************/
1024 /* higher-level buffer-position functions */
1026 /************************************************************************/
1028 /*----------------------------------------------------------------------*/
1029 /* Settor macros for important positions in a buffer */
1030 /*----------------------------------------------------------------------*/
1032 /* Set beginning of accessible range of buffer. */
1033 #define SET_BOTH_BUF_BEGV(buf, val, bival) \
1036 (buf)->begv = (bival); \
1037 (buf)->bufbegv = (val); \
1040 /* Set end of accessible range of buffer. */
1041 #define SET_BOTH_BUF_ZV(buf, val, bival) \
1044 (buf)->zv = (bival); \
1045 (buf)->bufzv = (val); \
1049 /* Since BEGV and ZV are almost never set, it's reasonable to enforce
1050 the restriction that the Bufpos and Bytind values must both be
1051 specified. However, point is set in lots and lots of places. So
1052 we provide the ability to specify both (for efficiency) or just
1054 #define BOTH_BUF_SET_PT(buf, val, bival) set_buffer_point (buf, val, bival)
1055 #define BI_BUF_SET_PT(buf, bival) \
1056 BOTH_BUF_SET_PT (buf, bytind_to_bufpos (buf, bival), bival)
1057 #define BUF_SET_PT(buf, value) \
1058 BOTH_BUF_SET_PT (buf, value, bufpos_to_bytind (buf, value))
1062 /* These macros exist in FSFmacs because SET_PT() in FSFmacs incorrectly
1063 does too much stuff, such as moving out of invisible extents. */
1064 #define TEMP_SET_PT(position) (temp_set_point ((position), current_buffer))
1065 #define SET_BUF_PT(buf, value) ((buf)->pt = (value))
1066 #endif /* FSFmacs */
1068 /*----------------------------------------------------------------------*/
1069 /* Miscellaneous buffer values */
1070 /*----------------------------------------------------------------------*/
1072 /* Number of characters in buffer */
1073 #define BUF_SIZE(buf) (BUF_Z (buf) - BUF_BEG (buf))
1075 /* Is this buffer narrowed? */
1076 #define BUF_NARROWED(buf) \
1077 ((BI_BUF_BEGV (buf) != BI_BUF_BEG (buf)) || \
1078 (BI_BUF_ZV (buf) != BI_BUF_Z (buf)))
1080 /* Modification count. */
1081 #define BUF_MODIFF(buf) ((buf)->text->modiff)
1083 /* Saved modification count. */
1084 #define BUF_SAVE_MODIFF(buf) ((buf)->text->save_modiff)
1087 #define BUF_FACECHANGE(buf) ((buf)->face_change)
1089 #define POINT_MARKER_P(marker) \
1090 (XMARKER (marker)->buffer != 0 && \
1091 EQ (marker, XMARKER (marker)->buffer->point_marker))
1093 #define BUF_MARKERS(buf) ((buf)->markers)
1097 The new definitions of CEILING_OF() and FLOOR_OF() differ semantically
1098 from the old ones (in FSF Emacs and XEmacs 19.11 and before).
1099 Conversion is as follows:
1101 OLD_BI_CEILING_OF(n) = NEW_BI_CEILING_OF(n) - 1
1102 OLD_BI_FLOOR_OF(n) = NEW_BI_FLOOR_OF(n + 1)
1104 The definitions were changed because the new definitions are more
1105 consistent with the way everything else works in Emacs.
1108 /* Properties of CEILING_OF and FLOOR_OF (also apply to BI_ variants):
1110 1) FLOOR_OF (CEILING_OF (n)) = n
1111 CEILING_OF (FLOOR_OF (n)) = n
1113 2) CEILING_OF (n) = n if and only if n = ZV
1114 FLOOR_OF (n) = n if and only if n = BEGV
1116 3) CEILING_OF (CEILING_OF (n)) = ZV
1117 FLOOR_OF (FLOOR_OF (n)) = BEGV
1119 4) The bytes in the regions
1121 [BYTE_ADDRESS (n), BYTE_ADDRESS_BEFORE (CEILING_OF (n))]
1125 [BYTE_ADDRESS (FLOOR_OF (n)), BYTE_ADDRESS_BEFORE (n)]
1131 /* Return the maximum index in the buffer it is safe to scan forwards
1132 past N to. This is used to prevent buffer scans from running into
1133 the gap (e.g. search.c). All characters between N and CEILING_OF(N)
1134 are located contiguous in memory. Note that the character *at*
1135 CEILING_OF(N) is not contiguous in memory. */
1136 #define BI_BUF_CEILING_OF(b, n) \
1137 ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_ZV (b) ? \
1138 (b)->text->gpt : BI_BUF_ZV (b))
1139 #define BUF_CEILING_OF(b, n) \
1140 bytind_to_bufpos (b, BI_BUF_CEILING_OF (b, bufpos_to_bytind (b, n)))
1142 /* Return the minimum index in the buffer it is safe to scan backwards
1143 past N to. All characters between FLOOR_OF(N) and N are located
1144 contiguous in memory. Note that the character *at* N may not be
1145 contiguous in memory. */
1146 #define BI_BUF_FLOOR_OF(b, n) \
1147 (BI_BUF_BEGV (b) < (b)->text->gpt && (b)->text->gpt < (n) ? \
1148 (b)->text->gpt : BI_BUF_BEGV (b))
1149 #define BUF_FLOOR_OF(b, n) \
1150 bytind_to_bufpos (b, BI_BUF_FLOOR_OF (b, bufpos_to_bytind (b, n)))
1152 #define BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \
1153 ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_Z (b) ? \
1154 (b)->text->gpt : BI_BUF_Z (b))
1155 #define BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n) \
1157 (b, BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
1159 #define BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \
1160 (BI_BUF_BEG (b) < (b)->text->gpt && (b)->text->gpt < (n) ? \
1161 (b)->text->gpt : BI_BUF_BEG (b))
1162 #define BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n) \
1164 (b, BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
1167 extern struct buffer *current_buffer;
1169 /* This is the initial (startup) directory, as used for the *scratch* buffer.
1170 We're making this a global to make others aware of the startup directory.
1171 `initial_directory' is stored in external format.
1173 extern char initial_directory[];
1174 extern void init_initial_directory (void); /* initialize initial_directory */
1176 EXFUN (Fbuffer_disable_undo, 1);
1177 EXFUN (Fbuffer_modified_p, 1);
1178 EXFUN (Fbuffer_name, 1);
1179 EXFUN (Fcurrent_buffer, 0);
1180 EXFUN (Ferase_buffer, 1);
1181 EXFUN (Fget_buffer, 1);
1182 EXFUN (Fget_buffer_create, 1);
1183 EXFUN (Fget_file_buffer, 1);
1184 EXFUN (Fkill_buffer, 1);
1185 EXFUN (Fother_buffer, 3);
1186 EXFUN (Frecord_buffer, 1);
1187 EXFUN (Fset_buffer, 1);
1188 EXFUN (Fset_buffer_modified_p, 2);
1190 extern Lisp_Object QSscratch, Qafter_change_function, Qafter_change_functions;
1191 extern Lisp_Object Qbefore_change_function, Qbefore_change_functions;
1192 extern Lisp_Object Qbuffer_or_string_p, Qdefault_directory, Qfirst_change_hook;
1193 extern Lisp_Object Qpermanent_local, Vafter_change_function;
1194 extern Lisp_Object Vafter_change_functions, Vbefore_change_function;
1195 extern Lisp_Object Vbefore_change_functions, Vbuffer_alist, Vbuffer_defaults;
1196 extern Lisp_Object Vinhibit_read_only, Vtransient_mark_mode;
1198 /* This structure marks which slots in a buffer have corresponding
1199 default values in Vbuffer_defaults.
1200 Each such slot has a nonzero value in this structure.
1201 The value has only one nonzero bit.
1203 When a buffer has its own local value for a slot,
1204 the bit for that slot (found in the same slot in this structure)
1205 is turned on in the buffer's local_var_flags slot.
1207 If a slot in this structure is zero, then even though there may
1208 be a DEFVAR_BUFFER_LOCAL for the slot, there is no default value for it;
1209 and the corresponding slot in Vbuffer_defaults is not used. */
1211 extern struct buffer buffer_local_flags;
1214 /* Allocation of buffer data. */
1218 char *r_alloc (unsigned char **, size_t);
1219 char *r_re_alloc (unsigned char **, size_t);
1220 void r_alloc_free (unsigned char **);
1222 #define BUFFER_ALLOC(data, size) \
1223 ((Bufbyte *) r_alloc ((unsigned char **) &data, (size) * sizeof(Bufbyte)))
1224 #define BUFFER_REALLOC(data, size) \
1225 ((Bufbyte *) r_re_alloc ((unsigned char **) &data, (size) * sizeof(Bufbyte)))
1226 #define BUFFER_FREE(data) r_alloc_free ((unsigned char **) &(data))
1227 #define R_ALLOC_DECLARE(var,data) r_alloc_declare (&(var), data)
1229 #else /* !REL_ALLOC */
1231 #define BUFFER_ALLOC(data,size)\
1232 (data = xnew_array (Bufbyte, size))
1233 #define BUFFER_REALLOC(data,size)\
1234 ((Bufbyte *) xrealloc (data, (size) * sizeof(Bufbyte)))
1235 /* Avoid excess parentheses, or syntax errors may rear their heads. */
1236 #define BUFFER_FREE(data) xfree (data)
1237 #define R_ALLOC_DECLARE(var,data)
1239 #endif /* !REL_ALLOC */
1241 extern Lisp_Object Vbuffer_alist;
1242 void set_buffer_internal (struct buffer *b);
1243 struct buffer *decode_buffer (Lisp_Object buffer, int allow_string);
1245 /* from editfns.c */
1246 void widen_buffer (struct buffer *b, int no_clip);
1247 int beginning_of_line_p (struct buffer *b, Bufpos pt);
1250 void set_buffer_point (struct buffer *buf, Bufpos pos, Bytind bipos);
1251 void find_charsets_in_bufbyte_string (Charset_ID *charsets,
1254 void find_charsets_in_charc_string (Charset_ID *charsets,
1257 int bufbyte_string_displayed_columns (const Bufbyte *str, Bytecount len);
1258 int charc_string_displayed_columns (const Charc *str, Charcount len);
1259 void convert_bufbyte_string_into_charc_dynarr (const Bufbyte *str,
1262 Charcount convert_bufbyte_string_into_emchar_string (const Bufbyte *str,
1265 void convert_charc_string_into_bufbyte_dynarr (Charc *arr, int nels,
1266 Bufbyte_dynarr *dyn);
1267 Bufbyte *convert_charc_string_into_malloced_string (Charc *arr, int nels,
1268 Bytecount *len_out);
1270 void init_buffer_markers (struct buffer *b);
1271 void uninit_buffer_markers (struct buffer *b);
1273 /* flags for get_buffer_pos_char(), get_buffer_range_char(), etc. */
1274 /* At most one of GB_COERCE_RANGE and GB_NO_ERROR_IF_BAD should be
1275 specified. At most one of GB_NEGATIVE_FROM_END and GB_NO_ERROR_IF_BAD
1276 should be specified. */
1278 #define GB_ALLOW_PAST_ACCESSIBLE (1 << 0)
1279 #define GB_ALLOW_NIL (1 << 1)
1280 #define GB_CHECK_ORDER (1 << 2)
1281 #define GB_COERCE_RANGE (1 << 3)
1282 #define GB_NO_ERROR_IF_BAD (1 << 4)
1283 #define GB_NEGATIVE_FROM_END (1 << 5)
1284 #define GB_HISTORICAL_STRING_BEHAVIOR (GB_NEGATIVE_FROM_END | GB_ALLOW_NIL)
1286 Bufpos get_buffer_pos_char (struct buffer *b, Lisp_Object pos,
1287 unsigned int flags);
1288 Bytind get_buffer_pos_byte (struct buffer *b, Lisp_Object pos,
1289 unsigned int flags);
1290 void get_buffer_range_char (struct buffer *b, Lisp_Object from, Lisp_Object to,
1291 Bufpos *from_out, Bufpos *to_out,
1292 unsigned int flags);
1293 void get_buffer_range_byte (struct buffer *b, Lisp_Object from, Lisp_Object to,
1294 Bytind *from_out, Bytind *to_out,
1295 unsigned int flags);
1296 Charcount get_string_pos_char (Lisp_Object string, Lisp_Object pos,
1297 unsigned int flags);
1298 Bytecount get_string_pos_byte (Lisp_Object string, Lisp_Object pos,
1299 unsigned int flags);
1300 void get_string_range_char (Lisp_Object string, Lisp_Object from,
1301 Lisp_Object to, Charcount *from_out,
1302 Charcount *to_out, unsigned int flags);
1303 void get_string_range_byte (Lisp_Object string, Lisp_Object from,
1304 Lisp_Object to, Bytecount *from_out,
1305 Bytecount *to_out, unsigned int flags);
1306 Bufpos get_buffer_or_string_pos_char (Lisp_Object object, Lisp_Object pos,
1307 unsigned int flags);
1308 Bytind get_buffer_or_string_pos_byte (Lisp_Object object, Lisp_Object pos,
1309 unsigned int flags);
1310 void get_buffer_or_string_range_char (Lisp_Object object, Lisp_Object from,
1311 Lisp_Object to, Bufpos *from_out,
1312 Bufpos *to_out, unsigned int flags);
1313 void get_buffer_or_string_range_byte (Lisp_Object object, Lisp_Object from,
1314 Lisp_Object to, Bytind *from_out,
1315 Bytind *to_out, unsigned int flags);
1316 Bufpos buffer_or_string_accessible_begin_char (Lisp_Object object);
1317 Bufpos buffer_or_string_accessible_end_char (Lisp_Object object);
1318 Bytind buffer_or_string_accessible_begin_byte (Lisp_Object object);
1319 Bytind buffer_or_string_accessible_end_byte (Lisp_Object object);
1320 Bufpos buffer_or_string_absolute_begin_char (Lisp_Object object);
1321 Bufpos buffer_or_string_absolute_end_char (Lisp_Object object);
1322 Bytind buffer_or_string_absolute_begin_byte (Lisp_Object object);
1323 Bytind buffer_or_string_absolute_end_byte (Lisp_Object object);
1324 void record_buffer (Lisp_Object buf);
1325 Lisp_Object get_buffer (Lisp_Object name,
1326 int error_if_deleted_or_does_not_exist);
1327 int map_over_sharing_buffers (struct buffer *buf,
1328 int (*mapfun) (struct buffer *buf,
1333 /************************************************************************/
1334 /* Case conversion */
1335 /************************************************************************/
1337 /* A "trt" table is a mapping from characters to other characters,
1338 typically used to convert between uppercase and lowercase. For
1339 compatibility reasons, trt tables are currently in the form of
1340 a Lisp string of 256 characters, specifying the conversion for each
1341 of the first 256 Emacs characters (i.e. the 256 Latin-1 characters).
1342 This should be generalized at some point to support conversions for
1343 all of the allowable Mule characters.
1346 /* The _1 macros are named as such because they assume that you have
1347 already guaranteed that the character values are all in the range
1348 0 - 255. Bad lossage will happen otherwise. */
1350 # define MAKE_TRT_TABLE() Fmake_string (make_int (256), make_char (0))
1351 # define TRT_TABLE_AS_STRING(table) XSTRING_DATA (table)
1352 # define TRT_TABLE_CHAR_1(table, ch) \
1353 string_char (XSTRING (table), (Charcount) ch)
1354 # define SET_TRT_TABLE_CHAR_1(table, ch1, ch2) \
1355 set_string_char (XSTRING (table), (Charcount) ch1, ch2)
1358 # define MAKE_MIRROR_TRT_TABLE() make_opaque (OPAQUE_CLEAR, 256)
1359 # define MIRROR_TRT_TABLE_AS_STRING(table) ((Bufbyte *) XOPAQUE_DATA (table))
1360 # define MIRROR_TRT_TABLE_CHAR_1(table, ch) \
1361 ((Emchar) (MIRROR_TRT_TABLE_AS_STRING (table)[ch]))
1362 # define SET_MIRROR_TRT_TABLE_CHAR_1(table, ch1, ch2) \
1363 (MIRROR_TRT_TABLE_AS_STRING (table)[ch1] = (Bufbyte) (ch2))
1366 # define IN_TRT_TABLE_DOMAIN(c) (((EMACS_UINT) (c)) <= 255)
1369 #define MIRROR_DOWNCASE_TABLE_AS_STRING(buf) \
1370 MIRROR_TRT_TABLE_AS_STRING (buf->mirror_downcase_table)
1371 #define MIRROR_UPCASE_TABLE_AS_STRING(buf) \
1372 MIRROR_TRT_TABLE_AS_STRING (buf->mirror_upcase_table)
1373 #define MIRROR_CANON_TABLE_AS_STRING(buf) \
1374 MIRROR_TRT_TABLE_AS_STRING (buf->mirror_case_canon_table)
1375 #define MIRROR_EQV_TABLE_AS_STRING(buf) \
1376 MIRROR_TRT_TABLE_AS_STRING (buf->mirror_case_eqv_table)
1378 #define MIRROR_DOWNCASE_TABLE_AS_STRING(buf) \
1379 TRT_TABLE_AS_STRING (buf->downcase_table)
1380 #define MIRROR_UPCASE_TABLE_AS_STRING(buf) \
1381 TRT_TABLE_AS_STRING (buf->upcase_table)
1382 #define MIRROR_CANON_TABLE_AS_STRING(buf) \
1383 TRT_TABLE_AS_STRING (buf->case_canon_table)
1384 #define MIRROR_EQV_TABLE_AS_STRING(buf) \
1385 TRT_TABLE_AS_STRING (buf->case_eqv_table)
1388 INLINE_HEADER Emchar TRT_TABLE_OF (Lisp_Object trt, Emchar c);
1389 INLINE_HEADER Emchar
1390 TRT_TABLE_OF (Lisp_Object trt, Emchar c)
1392 return IN_TRT_TABLE_DOMAIN (c) ? TRT_TABLE_CHAR_1 (trt, c) : c;
1395 /* Macros used below. */
1396 #define DOWNCASE_TABLE_OF(buf, c) TRT_TABLE_OF (buf->downcase_table, c)
1397 #define UPCASE_TABLE_OF(buf, c) TRT_TABLE_OF (buf->upcase_table, c)
1399 /* 1 if CH is upper case. */
1401 INLINE_HEADER int UPPERCASEP (struct buffer *buf, Emchar ch);
1403 UPPERCASEP (struct buffer *buf, Emchar ch)
1405 return DOWNCASE_TABLE_OF (buf, ch) != ch;
1408 /* 1 if CH is lower case. */
1410 INLINE_HEADER int LOWERCASEP (struct buffer *buf, Emchar ch);
1412 LOWERCASEP (struct buffer *buf, Emchar ch)
1414 return (UPCASE_TABLE_OF (buf, ch) != ch &&
1415 DOWNCASE_TABLE_OF (buf, ch) == ch);
1418 /* 1 if CH is neither upper nor lower case. */
1420 INLINE_HEADER int NOCASEP (struct buffer *buf, Emchar ch);
1422 NOCASEP (struct buffer *buf, Emchar ch)
1424 return UPCASE_TABLE_OF (buf, ch) == ch;
1427 /* Upcase a character, or make no change if that cannot be done. */
1429 INLINE_HEADER Emchar UPCASE (struct buffer *buf, Emchar ch);
1430 INLINE_HEADER Emchar
1431 UPCASE (struct buffer *buf, Emchar ch)
1433 return (DOWNCASE_TABLE_OF (buf, ch) == ch) ? UPCASE_TABLE_OF (buf, ch) : ch;
1436 /* Upcase a character known to be not upper case. Unused. */
1438 #define UPCASE1(buf, ch) UPCASE_TABLE_OF (buf, ch)
1440 /* Downcase a character, or make no change if that cannot be done. */
1442 #define DOWNCASE(buf, ch) DOWNCASE_TABLE_OF (buf, ch)
1444 #endif /* INCLUDED_buffer_h_ */