Include character unconditionally.
[chise/xemacs-chise.git-] / src / buffer.h
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.
5
6 This file is part of XEmacs.
7
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
11 later version.
12
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
16 for more details.
17
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.  */
22
23 /* Synched up with: FSF 19.30. */
24
25 /* Authorship:
26
27    FSF: long ago.
28    JWZ: separated out bufslots.h, early in Lemacs.
29    Ben Wing: almost completely rewritten for Mule, 19.12.
30  */
31
32 #ifndef _XEMACS_BUFFER_H_
33 #define _XEMACS_BUFFER_H_
34
35 #include "character.h"
36
37 #ifdef UTF2000
38 #include "mb-utf-8.h"
39 #endif
40
41 /************************************************************************/
42 /*                                                                      */
43 /*                    definition of Lisp buffer object                  */
44 /*                                                                      */
45 /************************************************************************/
46
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.
51
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. */
56
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.)
66
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.
77    */
78
79 struct buffer_text
80 {
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
90                            changed.  */
91   long save_modiff;     /* Previous value of modiff, as of last
92                            time buffer visited or saved a file.  */
93
94 #ifdef MULE
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;
99 #ifdef UTF2000
100   int mule_size;
101 #else
102   int mule_shifter, mule_three_p;
103 #endif
104
105   /* And we also cache 16 positions for fairly fast access near those
106      positions. */
107   Bufpos mule_bufpos_cache[16];
108   Bytind mule_bytind_cache[16];
109 #endif
110
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;
114
115   /* Change data that goes with the text. */
116   struct buffer_text_change_data *changes;
117
118 };
119
120 struct buffer
121 {
122   struct lcrecord_header header;
123
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;
127
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;
132
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. */
139
140   int face_change;      /* This is set when a change in how the text should
141                            be displayed (e.g., font, color) is made. */
142
143   /* change data indicating what portion of the text has changed
144      since the last time this was reset.  Used by redisplay.
145      Logically we should keep this with the text structure, but
146      redisplay resets it for each buffer individually and we don't
147      want interference between an indirect buffer and its base
148      buffer. */
149   struct each_buffer_change_data *changes;
150
151 #ifdef REGION_CACHE_NEEDS_WORK
152   /* If the long line scan cache is enabled (i.e. the buffer-local
153      variable cache-long-line-scans is non-nil), newline_cache
154      points to the newline cache, and width_run_cache points to the
155      width run cache.
156
157      The newline cache records which stretches of the buffer are
158      known *not* to contain newlines, so that they can be skipped
159      quickly when we search for newlines.
160
161      The width run cache records which stretches of the buffer are
162      known to contain characters whose widths are all the same.  If
163      the width run cache maps a character to a value > 0, that value
164      is the character's width; if it maps a character to zero, we
165      don't know what its width is.  This allows compute_motion to
166      process such regions very quickly, using algebra instead of
167      inspecting each character.  See also width_table, below.  */
168   struct region_cache *newline_cache;
169   struct region_cache *width_run_cache;
170 #endif /* REGION_CACHE_NEEDS_WORK */
171
172   /* The markers that refer to this buffer.  This is actually a single
173      marker -- successive elements in its marker `chain' are the other
174      markers referring to this buffer */
175   struct Lisp_Marker *markers;
176
177   /* The buffer's extent info.  This is its own type, an extent-info
178      object (done this way for ease in marking / finalizing). */
179   Lisp_Object extent_info;
180
181   /* ----------------------------------------------------------------- */
182   /* All the stuff above this line is the responsibility of insdel.c,
183      with some help from marker.c and extents.c.
184      All the stuff below this line is the responsibility of buffer.c. */
185
186   /* In an indirect buffer, this points to the base buffer.
187      In an ordinary buffer, it is 0.
188      We DO mark through this slot. */
189   struct buffer *base_buffer;
190
191   /* List of indirect buffers whose base is this buffer.
192      If we are an indirect buffer, this will be nil.
193      Do NOT mark through this. */
194   Lisp_Object indirect_children;
195
196   /* Flags saying which DEFVAR_PER_BUFFER variables
197      are local to this buffer.  */
198   int local_var_flags;
199
200   /* Set to the modtime of the visited file when read or written.
201      -1 means visited file was nonexistent.
202      0  means visited file modtime unknown; in no case complain
203      about any mismatch on next save attempt.  */
204   int modtime;
205
206   /* the value of text->modiff at the last auto-save.  */
207   int auto_save_modified;
208
209   /* The time at which we detected a failure to auto-save,
210      Or -1 if we didn't have a failure.  */
211   int auto_save_failure_time;
212
213   /* Position in buffer at which display started
214      the last time this buffer was displayed.  */
215   int last_window_start;
216
217   /* Everything from here down must be a Lisp_Object */
218
219 #define MARKED_SLOT(x) Lisp_Object x
220 #include "bufslots.h"
221 #undef MARKED_SLOT
222 };
223
224 DECLARE_LRECORD (buffer, struct buffer);
225 #define XBUFFER(x) XRECORD (x, buffer, struct buffer)
226 #define XSETBUFFER(x, p) XSETRECORD (x, p, buffer)
227 #define BUFFERP(x) RECORDP (x, buffer)
228 #define GC_BUFFERP(x) GC_RECORDP (x, buffer)
229 #define CHECK_BUFFER(x) CHECK_RECORD (x, buffer)
230 #define CONCHECK_BUFFER(x) CONCHECK_RECORD (x, buffer)
231
232 #define BUFFER_LIVE_P(b) (!NILP ((b)->name))
233
234 #define CHECK_LIVE_BUFFER(x) do {                       \
235   CHECK_BUFFER (x);                                     \
236   if (!BUFFER_LIVE_P (XBUFFER (x)))                     \
237     dead_wrong_type_argument (Qbuffer_live_p, (x));     \
238 } while (0)
239
240 #define CONCHECK_LIVE_BUFFER(x) do {                    \
241   CONCHECK_BUFFER (x);                                  \
242   if (!BUFFER_LIVE_P (XBUFFER (x)))                     \
243     x = wrong_type_argument (Qbuffer_live_p, (x));      \
244 } while (0)
245
246 \f
247 #define BUFFER_BASE_BUFFER(b) ((b)->base_buffer ? (b)->base_buffer : (b))
248
249 /* Map over buffers sharing the same text as MPS_BUF.  MPS_BUFVAR is a
250    variable that gets the buffer values (beginning with the base
251    buffer, then the children), and MPS_BUFCONS should be a temporary
252    Lisp_Object variable.  */
253 #define MAP_INDIRECT_BUFFERS(mps_buf, mps_bufvar, mps_bufcons)                  \
254 for (mps_bufcons = Qunbound,                                                    \
255      mps_bufvar = BUFFER_BASE_BUFFER (mps_buf);                                 \
256      UNBOUNDP (mps_bufcons) ?                                                   \
257         (mps_bufcons = mps_bufvar->indirect_children,                           \
258         1)                                                                      \
259        : (!NILP (mps_bufcons)                                                   \
260           && (mps_bufvar = XBUFFER (XCAR (mps_bufcons)), 1)                     \
261           && (mps_bufcons = XCDR (mps_bufcons), 1));                            \
262      )
263
264 \f
265
266 /************************************************************************/
267 /*                                                                      */
268 /*                 working with raw internal-format data                */
269 /*                                                                      */
270 /************************************************************************/
271
272 /* NOTE: In all the following macros, we follow these rules concerning
273    multiple evaluation of the arguments:
274
275    1) Anything that's an lvalue can be evaluated more than once.
276    2) Anything that's a Lisp Object can be evaluated more than once.
277       This should probably be changed, but this follows the way
278       that all the macros in lisp.h do things.
279    3) 'struct buffer *' arguments can be evaluated more than once.
280    4) Nothing else can be evaluated more than once.  Use inline
281       functions, if necessary, to prevent multiple evaluation.
282    5) An exception to (4) is that there are some macros below that
283       may evaluate their arguments more than once.  They are all
284       denoted with the word "unsafe" in their name and are generally
285       meant to be called only by other macros that have already
286       stored the calling values in temporary variables.
287
288
289    Use the following functions/macros on contiguous strings of data.
290    If the text you're operating on is known to come from a buffer, use
291    the buffer-level functions below -- they know about the gap and may
292    be more efficient.
293
294
295   (A) For working with charptr's (pointers to internally-formatted text):
296   -----------------------------------------------------------------------
297
298    VALID_CHARPTR_P (ptr):
299         Given a charptr, does it point to the beginning of a character?
300
301    ASSERT_VALID_CHARPTR (ptr):
302         If error-checking is enabled, assert that the given charptr
303         points to the beginning of a character.  Otherwise, do nothing.
304
305    INC_CHARPTR (ptr):
306         Given a charptr (assumed to point at the beginning of a character),
307         modify that pointer so it points to the beginning of the next
308         character.
309
310    DEC_CHARPTR (ptr):
311         Given a charptr (assumed to point at the beginning of a
312         character or at the very end of the text), modify that pointer
313         so it points to the beginning of the previous character.
314
315    VALIDATE_CHARPTR_BACKWARD (ptr):
316         Make sure that PTR is pointing to the beginning of a character.
317         If not, back up until this is the case.   Note that there are not
318         too many places where it is legitimate to do this sort of thing.
319         It's an error if you're passed an "invalid" char * pointer.
320         NOTE: PTR *must* be pointing to a valid part of the string (i.e.
321         not the very end, unless the string is zero-terminated or
322         something) in order for this function to not cause crashes.
323
324    VALIDATE_CHARPTR_FORWARD (ptr):
325         Make sure that PTR is pointing to the beginning of a character.
326         If not, move forward until this is the case.  Note that there
327         are not too many places where it is legitimate to do this sort
328         of thing.  It's an error if you're passed an "invalid" char *
329         pointer.
330
331
332    (B) For working with the length (in bytes and characters) of a
333        section of internally-formatted text:
334    --------------------------------------------------------------
335
336    bytecount_to_charcount (ptr, nbi):
337         Given a pointer to a text string and a length in bytes,
338         return the equivalent length in characters.
339
340    charcount_to_bytecount (ptr, nch):
341         Given a pointer to a text string and a length in characters,
342         return the equivalent length in bytes.
343
344    charptr_n_addr (ptr, n):
345         Return a pointer to the beginning of the character offset N
346         (in characters) from PTR.
347
348
349    (C) For retrieving or changing the character pointed to by a charptr:
350    ---------------------------------------------------------------------
351
352    charptr_emchar (ptr):
353         Retrieve the character pointed to by PTR as an Emchar.
354
355    charptr_emchar_n (ptr, n):
356         Retrieve the character at offset N (in characters) from PTR,
357         as an Emchar.
358
359    set_charptr_emchar (ptr, ch):
360         Store the character CH (an Emchar) as internally-formatted
361         text starting at PTR.  Return the number of bytes stored.
362
363    charptr_copy_char (ptr, ptr2):
364         Retrieve the character pointed to by PTR and store it as
365         internally-formatted text in PTR2.
366
367
368    (D) For working with Emchars:
369    -----------------------------
370
371    [Note that there are other functions/macros for working with Emchars
372     in mule-charset.h, for retrieving the charset of an Emchar
373     and such.  These are only valid when MULE is defined.]
374
375    valid_char_p (ch):
376         Return whether the given Emchar is valid.
377
378    CHARP (ch):
379         Return whether the given Lisp_Object is a character.
380
381    CHECK_CHAR_COERCE_INT (ch):
382         Signal an error if CH is not a valid character or integer Lisp_Object.
383         If CH is an integer Lisp_Object, convert it to a character Lisp_Object,
384         but merely by repackaging, without performing tests for char validity.
385
386    MAX_EMCHAR_LEN:
387         Maximum number of buffer bytes per Emacs character.
388
389 */
390
391
392 /* ---------------------------------------------------------------------- */
393 /* (A) For working with charptr's (pointers to internally-formatted text) */
394 /* ---------------------------------------------------------------------- */
395
396 #ifdef MULE
397 # define VALID_CHARPTR_P(ptr) BUFBYTE_FIRST_BYTE_P (* (unsigned char *) ptr)
398 #else
399 # define VALID_CHARPTR_P(ptr) 1
400 #endif
401
402 #ifdef ERROR_CHECK_BUFPOS
403 # define ASSERT_VALID_CHARPTR(ptr) assert (VALID_CHARPTR_P (ptr))
404 #else
405 # define ASSERT_VALID_CHARPTR(ptr)
406 #endif
407
408 /* Note that INC_CHARPTR() and DEC_CHARPTR() have to be written in
409    completely separate ways.  INC_CHARPTR() cannot use the DEC_CHARPTR()
410    trick of looking for a valid first byte because it might run off
411    the end of the string.  DEC_CHARPTR() can't use the INC_CHARPTR()
412    method because it doesn't have easy access to the first byte of
413    the character it's moving over. */
414
415 #define REAL_INC_CHARPTR(ptr) \
416   ((void) ((ptr) += REP_BYTES_BY_FIRST_BYTE (* (unsigned char *) (ptr))))
417
418 #define REAL_INC_CHARBYTIND(ptr,pos) \
419   (pos += REP_BYTES_BY_FIRST_BYTE (* (unsigned char *) (ptr)))
420
421 #define REAL_DEC_CHARPTR(ptr) do {      \
422   (ptr)--;                              \
423 } while (!VALID_CHARPTR_P (ptr))
424
425 #ifdef ERROR_CHECK_BUFPOS
426 #define INC_CHARPTR(ptr) do {           \
427   ASSERT_VALID_CHARPTR (ptr);           \
428   REAL_INC_CHARPTR (ptr);               \
429 } while (0)
430
431 #define INC_CHARBYTIND(ptr,pos) do {            \
432   ASSERT_VALID_CHARPTR (ptr);           \
433   REAL_INC_CHARBYTIND (ptr,pos);                \
434 } while (0)
435
436 #define DEC_CHARPTR(ptr) do {                   \
437   CONST Bufbyte *dc_ptr1 = (ptr);               \
438   CONST Bufbyte *dc_ptr2 = dc_ptr1;             \
439   REAL_DEC_CHARPTR (dc_ptr2);                   \
440   assert (dc_ptr1 - dc_ptr2 ==                  \
441           REP_BYTES_BY_FIRST_BYTE (*dc_ptr2));  \
442   (ptr) = dc_ptr2;                              \
443 } while (0)
444
445 #else /* ! ERROR_CHECK_BUFPOS */
446 #define INC_CHARBYTIND(ptr,pos) REAL_INC_CHARBYTIND (ptr,pos)
447 #define INC_CHARPTR(ptr) REAL_INC_CHARPTR (ptr)
448 #define DEC_CHARPTR(ptr) REAL_DEC_CHARPTR (ptr)
449 #endif /* ! ERROR_CHECK_BUFPOS */
450
451 #ifdef MULE
452
453 #define VALIDATE_CHARPTR_BACKWARD(ptr) do {     \
454   while (!VALID_CHARPTR_P (ptr)) ptr--;         \
455 } while (0)
456
457 /* This needs to be trickier to avoid the possibility of running off
458    the end of the string. */
459
460 #define VALIDATE_CHARPTR_FORWARD(ptr) do {      \
461   Bufbyte *vcf_ptr = (ptr);                     \
462   VALIDATE_CHARPTR_BACKWARD (vcf_ptr);          \
463   if (vcf_ptr != (ptr))                         \
464     {                                           \
465       (ptr) = vcf_ptr;                          \
466       INC_CHARPTR (ptr);                        \
467     }                                           \
468 } while (0)
469
470 #else /* not MULE */
471 #define VALIDATE_CHARPTR_BACKWARD(ptr)
472 #define VALIDATE_CHARPTR_FORWARD(ptr)
473 #endif /* not MULE */
474
475 /* -------------------------------------------------------------- */
476 /* (B) For working with the length (in bytes and characters) of a */
477 /*     section of internally-formatted text                       */
478 /* -------------------------------------------------------------- */
479
480 INLINE CONST Bufbyte *charptr_n_addr (CONST Bufbyte *ptr, Charcount offset);
481 INLINE CONST Bufbyte *
482 charptr_n_addr (CONST Bufbyte *ptr, Charcount offset)
483 {
484   return ptr + charcount_to_bytecount (ptr, offset);
485 }
486
487 /* -------------------------------------------------------------------- */
488 /* (C) For retrieving or changing the character pointed to by a charptr */
489 /* -------------------------------------------------------------------- */
490
491 #define simple_charptr_emchar(ptr)              ((Emchar) (ptr)[0])
492 #define simple_set_charptr_emchar(ptr, x)       ((ptr)[0] = (Bufbyte) (x), 1)
493 #define simple_charptr_copy_char(ptr, ptr2)     ((ptr2)[0] = *(ptr), 1)
494
495 #ifdef MULE
496
497 Emchar non_ascii_charptr_emchar (CONST Bufbyte *ptr);
498 Bytecount non_ascii_set_charptr_emchar (Bufbyte *ptr, Emchar c);
499 Bytecount non_ascii_charptr_copy_char (CONST Bufbyte *ptr, Bufbyte *ptr2);
500
501 INLINE Emchar charptr_emchar (CONST Bufbyte *ptr);
502 INLINE Emchar
503 charptr_emchar (CONST Bufbyte *ptr)
504 {
505   return BYTE_ASCII_P (*ptr) ?
506     simple_charptr_emchar (ptr) :
507     non_ascii_charptr_emchar (ptr);
508 }
509
510 INLINE Bytecount set_charptr_emchar (Bufbyte *ptr, Emchar x);
511 INLINE Bytecount
512 set_charptr_emchar (Bufbyte *ptr, Emchar x)
513 {
514   return !CHAR_MULTIBYTE_P (x) ?
515     simple_set_charptr_emchar (ptr, x) :
516     non_ascii_set_charptr_emchar (ptr, x);
517 }
518
519 INLINE Bytecount charptr_copy_char (CONST Bufbyte *ptr, Bufbyte *ptr2);
520 INLINE Bytecount
521 charptr_copy_char (CONST Bufbyte *ptr, Bufbyte *ptr2)
522 {
523   return BYTE_ASCII_P (*ptr) ?
524     simple_charptr_copy_char (ptr, ptr2) :
525     non_ascii_charptr_copy_char (ptr, ptr2);
526 }
527
528 #else /* not MULE */
529
530 # define charptr_emchar(ptr)            simple_charptr_emchar (ptr)
531 # define set_charptr_emchar(ptr, x)     simple_set_charptr_emchar (ptr, x)
532 # define charptr_copy_char(ptr, ptr2)   simple_charptr_copy_char (ptr, ptr2)
533
534 #endif /* not MULE */
535
536 #define charptr_emchar_n(ptr, offset) \
537   charptr_emchar (charptr_n_addr (ptr, offset))
538
539
540 /* ---------------------------- */
541 /* (D) For working with Emchars */
542 /* ---------------------------- */
543
544 #define CHAR_INTP(x) (INTP (x) && valid_char_p (XINT (x)))
545
546 #define CHAR_OR_CHAR_INTP(x) (CHARP (x) || CHAR_INTP (x))
547
548 #ifdef ERROR_CHECK_TYPECHECK
549
550 INLINE Emchar XCHAR_OR_CHAR_INT (Lisp_Object obj);
551 INLINE Emchar
552 XCHAR_OR_CHAR_INT (Lisp_Object obj)
553 {
554   assert (CHAR_OR_CHAR_INTP (obj));
555   return CHARP (obj) ? XCHAR (obj) : XINT (obj);
556 }
557
558 #else
559
560 #define XCHAR_OR_CHAR_INT(obj) (CHARP ((obj)) ? XCHAR ((obj)) : XINT ((obj)))
561
562 #endif
563
564 #define CHECK_CHAR_COERCE_INT(x) do {           \
565   if (CHARP (x))                                \
566      ;                                          \
567   else if (CHAR_INTP (x))                       \
568     x = make_char (XINT (x));                   \
569   else                                          \
570     x = wrong_type_argument (Qcharacterp, x);   \
571 } while (0)
572
573 #ifdef UTF2000
574 # define MAX_EMCHAR_LEN 6
575 #else
576 #ifdef MULE
577 # define MAX_EMCHAR_LEN 4
578 #else
579 # define MAX_EMCHAR_LEN 1
580 #endif
581 #endif
582
583 \f
584 /*----------------------------------------------------------------------*/
585 /*          Accessor macros for important positions in a buffer         */
586 /*----------------------------------------------------------------------*/
587
588 /* We put them here because some stuff below wants them before the
589    place where we would normally put them. */
590
591 /* None of these are lvalues.  Use the settor macros below to change
592    the positions. */
593
594 /* Beginning of buffer.  */
595 #define BI_BUF_BEG(buf) ((Bytind) 1)
596 #define BUF_BEG(buf) ((Bufpos) 1)
597
598 /* Beginning of accessible range of buffer.  */
599 #define BI_BUF_BEGV(buf) ((buf)->begv + 0)
600 #define BUF_BEGV(buf) ((buf)->bufbegv + 0)
601
602 /* End of accessible range of buffer.  */
603 #define BI_BUF_ZV(buf) ((buf)->zv + 0)
604 #define BUF_ZV(buf) ((buf)->bufzv + 0)
605
606 /* End of buffer.  */
607 #define BI_BUF_Z(buf) ((buf)->text->z + 0)
608 #define BUF_Z(buf) ((buf)->text->bufz + 0)
609
610 /* Point. */
611 #define BI_BUF_PT(buf) ((buf)->pt + 0)
612 #define BUF_PT(buf) ((buf)->bufpt + 0)
613
614 /*----------------------------------------------------------------------*/
615 /*              Converting between positions and addresses              */
616 /*----------------------------------------------------------------------*/
617
618 /* Convert the address of a byte in the buffer into a position.  */
619 INLINE Bytind BI_BUF_PTR_BYTE_POS (struct buffer *buf, Bufbyte *ptr);
620 INLINE Bytind
621 BI_BUF_PTR_BYTE_POS (struct buffer *buf, Bufbyte *ptr)
622 {
623   return ((ptr) - (buf)->text->beg + 1
624           - ((ptr - (buf)->text->beg + 1) > (buf)->text->gpt
625              ? (buf)->text->gap_size : 0));
626 }
627
628 #define BUF_PTR_BYTE_POS(buf, ptr) \
629   bytind_to_bufpos (buf, BI_BUF_PTR_BYTE_POS (buf, ptr))
630
631 /* Address of byte at position POS in buffer. */
632 INLINE Bufbyte * BI_BUF_BYTE_ADDRESS (struct buffer *buf, Bytind pos);
633 INLINE Bufbyte *
634 BI_BUF_BYTE_ADDRESS (struct buffer *buf, Bytind pos)
635 {
636   return ((buf)->text->beg +
637           ((pos >= (buf)->text->gpt ? (pos + (buf)->text->gap_size) : pos)
638            - 1));
639 }
640
641 #define BUF_BYTE_ADDRESS(buf, pos) \
642   BI_BUF_BYTE_ADDRESS (buf, bufpos_to_bytind (buf, pos))
643
644 /* Address of byte before position POS in buffer. */
645 INLINE Bufbyte * BI_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytind pos);
646 INLINE Bufbyte *
647 BI_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytind pos)
648 {
649   return ((buf)->text->beg +
650           ((pos > (buf)->text->gpt ? (pos + (buf)->text->gap_size) : pos)
651            - 2));
652 }
653
654 #define BUF_BYTE_ADDRESS_BEFORE(buf, pos) \
655   BI_BUF_BYTE_ADDRESS_BEFORE (buf, bufpos_to_bytind (buf, pos))
656
657 /*----------------------------------------------------------------------*/
658 /*          Converting between byte indices and memory indices          */
659 /*----------------------------------------------------------------------*/
660
661 INLINE int valid_memind_p (struct buffer *buf, Memind x);
662 INLINE int
663 valid_memind_p (struct buffer *buf, Memind x)
664 {
665   return ((x >= 1 && x <= (Memind) (buf)->text->gpt) ||
666           (x  > (Memind) ((buf)->text->gpt + (buf)->text->gap_size) &&
667            x <= (Memind) ((buf)->text->z   + (buf)->text->gap_size)));
668 }
669
670 INLINE Memind bytind_to_memind (struct buffer *buf, Bytind x);
671 INLINE Memind
672 bytind_to_memind (struct buffer *buf, Bytind x)
673 {
674   return (Memind) ((x > (buf)->text->gpt) ? (x + (buf)->text->gap_size) : x);
675 }
676
677
678 INLINE Bytind memind_to_bytind (struct buffer *buf, Memind x);
679 INLINE Bytind
680 memind_to_bytind (struct buffer *buf, Memind x)
681 {
682 #ifdef ERROR_CHECK_BUFPOS
683   assert (valid_memind_p (buf, x));
684 #endif
685   return (Bytind) ((x > (Memind) (buf)->text->gpt) ?
686                    x - (buf)->text->gap_size :
687                    x);
688 }
689
690 #define memind_to_bufpos(buf, x) \
691   bytind_to_bufpos (buf, memind_to_bytind (buf, x))
692 #define bufpos_to_memind(buf, x) \
693   bytind_to_memind (buf, bufpos_to_bytind (buf, x))
694
695 /* These macros generalize many standard buffer-position functions to
696    either a buffer or a string. */
697
698 /* Converting between Meminds and Bytinds, for a buffer-or-string.
699    For strings, this is a no-op.  For buffers, this resolves
700    to the standard memind<->bytind converters. */
701
702 #define buffer_or_string_bytind_to_memind(obj, ind) \
703   (BUFFERP (obj) ? bytind_to_memind (XBUFFER (obj), ind) : (Memind) ind)
704
705 #define buffer_or_string_memind_to_bytind(obj, ind) \
706   (BUFFERP (obj) ? memind_to_bytind (XBUFFER (obj), ind) : (Bytind) ind)
707
708 /* Converting between Bufpos's and Bytinds, for a buffer-or-string.
709    For strings, this maps to the bytecount<->charcount converters. */
710
711 #define buffer_or_string_bufpos_to_bytind(obj, pos)             \
712   (BUFFERP (obj) ? bufpos_to_bytind (XBUFFER (obj), pos) :      \
713    (Bytind) charcount_to_bytecount (XSTRING_DATA (obj), pos))
714
715 #define buffer_or_string_bytind_to_bufpos(obj, ind)             \
716   (BUFFERP (obj) ? bytind_to_bufpos (XBUFFER (obj), ind) :      \
717    (Bufpos) bytecount_to_charcount (XSTRING_DATA (obj), ind))
718
719 /* Similar for Bufpos's and Meminds. */
720
721 #define buffer_or_string_bufpos_to_memind(obj, pos)             \
722   (BUFFERP (obj) ? bufpos_to_memind (XBUFFER (obj), pos) :      \
723    (Memind) charcount_to_bytecount (XSTRING_DATA (obj), pos))
724
725 #define buffer_or_string_memind_to_bufpos(obj, ind)             \
726   (BUFFERP (obj) ? memind_to_bufpos (XBUFFER (obj), ind) :      \
727    (Bufpos) bytecount_to_charcount (XSTRING_DATA (obj), ind))
728
729 /************************************************************************/
730 /*                                                                      */
731 /*                    working with buffer-level data                    */
732 /*                                                                      */
733 /************************************************************************/
734
735 /*
736
737    (A) Working with byte indices:
738    ------------------------------
739
740    VALID_BYTIND_P(buf, bi):
741         Given a byte index, does it point to the beginning of a character?
742
743    ASSERT_VALID_BYTIND_UNSAFE(buf, bi):
744         If error-checking is enabled, assert that the given byte index
745         is within range and points to the beginning of a character
746         or to the end of the buffer.  Otherwise, do nothing.
747
748    ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, bi):
749         If error-checking is enabled, assert that the given byte index
750         is within range and satisfies ASSERT_VALID_BYTIND() and also
751         does not refer to the beginning of the buffer. (i.e. movement
752         backwards is OK.) Otherwise, do nothing.
753
754    ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, bi):
755         If error-checking is enabled, assert that the given byte index
756         is within range and satisfies ASSERT_VALID_BYTIND() and also
757         does not refer to the end of the buffer. (i.e. movement
758         forwards is OK.) Otherwise, do nothing.
759
760    VALIDATE_BYTIND_BACKWARD(buf, bi):
761         Make sure that the given byte index is pointing to the beginning
762         of a character.  If not, back up until this is the case.  Note
763         that there are not too many places where it is legitimate to do
764         this sort of thing.  It's an error if you're passed an "invalid"
765         byte index.
766
767    VALIDATE_BYTIND_FORWARD(buf, bi):
768         Make sure that the given byte index is pointing to the beginning
769         of a character.  If not, move forward until this is the case.
770         Note that there are not too many places where it is legitimate
771         to do this sort of thing.  It's an error if you're passed an
772         "invalid" byte index.
773
774    INC_BYTIND(buf, bi):
775         Given a byte index (assumed to point at the beginning of a
776         character), modify that value so it points to the beginning
777         of the next character.
778
779    DEC_BYTIND(buf, bi):
780         Given a byte index (assumed to point at the beginning of a
781         character), modify that value so it points to the beginning
782         of the previous character.  Unlike for DEC_CHARPTR(), we can
783         do all the assert()s because there are sentinels at the
784         beginning of the gap and the end of the buffer.
785
786    BYTIND_INVALID:
787         A constant representing an invalid Bytind.  Valid Bytinds
788         can never have this value.
789
790
791    (B) Converting between Bufpos's and Bytinds:
792    --------------------------------------------
793
794     bufpos_to_bytind(buf, bu):
795         Given a Bufpos, return the equivalent Bytind.
796
797     bytind_to_bufpos(buf, bi):
798         Given a Bytind, return the equivalent Bufpos.
799
800     make_bufpos(buf, bi):
801         Given a Bytind, return the equivalent Bufpos as a Lisp Object.
802  */
803
804
805 /*----------------------------------------------------------------------*/
806 /*                       working with byte indices                      */
807 /*----------------------------------------------------------------------*/
808
809 #ifdef MULE
810 # define VALID_BYTIND_P(buf, x) \
811   BUFBYTE_FIRST_BYTE_P (*BI_BUF_BYTE_ADDRESS (buf, x))
812 #else
813 # define VALID_BYTIND_P(buf, x) 1
814 #endif
815
816 #ifdef ERROR_CHECK_BUFPOS
817
818 # define ASSERT_VALID_BYTIND_UNSAFE(buf, x) do {                \
819   assert (BUFFER_LIVE_P (buf));                                 \
820   assert ((x) >= BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf));      \
821   assert (VALID_BYTIND_P (buf, x));                             \
822 } while (0)
823 # define ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, x) do {       \
824   assert (BUFFER_LIVE_P (buf));                                 \
825   assert ((x) > BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf));       \
826   assert (VALID_BYTIND_P (buf, x));                             \
827 } while (0)
828 # define ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, x) do {        \
829   assert (BUFFER_LIVE_P (buf));                                 \
830   assert ((x) >= BI_BUF_BEG (buf) && x < BI_BUF_Z (buf));       \
831   assert (VALID_BYTIND_P (buf, x));                             \
832 } while (0)
833
834 #else /* not ERROR_CHECK_BUFPOS */
835 # define ASSERT_VALID_BYTIND_UNSAFE(buf, x)
836 # define ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, x)
837 # define ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, x)
838
839 #endif /* not ERROR_CHECK_BUFPOS */
840
841 /* Note that, although the Mule version will work fine for non-Mule
842    as well (it should reduce down to nothing), we provide a separate
843    version to avoid compilation warnings and possible non-optimal
844    results with stupid compilers. */
845
846 #ifdef MULE
847 # define VALIDATE_BYTIND_BACKWARD(buf, x) do {          \
848   Bufbyte *VBB_ptr = BI_BUF_BYTE_ADDRESS (buf, x);      \
849   while (!BUFBYTE_FIRST_BYTE_P (*VBB_ptr))              \
850     VBB_ptr--, (x)--;                                   \
851 } while (0)
852 #else
853 # define VALIDATE_BYTIND_BACKWARD(buf, x)
854 #endif
855
856 /* Note that, although the Mule version will work fine for non-Mule
857    as well (it should reduce down to nothing), we provide a separate
858    version to avoid compilation warnings and possible non-optimal
859    results with stupid compilers. */
860
861 #ifdef MULE
862 # define VALIDATE_BYTIND_FORWARD(buf, x) do {           \
863   Bufbyte *VBF_ptr = BI_BUF_BYTE_ADDRESS (buf, x);      \
864   while (!BUFBYTE_FIRST_BYTE_P (*VBF_ptr))              \
865     VBF_ptr++, (x)++;                                   \
866 } while (0)
867 #else
868 # define VALIDATE_BYTIND_FORWARD(buf, x)
869 #endif
870
871 /* Note that in the simplest case (no MULE, no ERROR_CHECK_BUFPOS),
872    this crap reduces down to simply (x)++. */
873
874 #define INC_BYTIND(buf, x) do                           \
875 {                                                       \
876   ASSERT_VALID_BYTIND_FORWARD_UNSAFE (buf, x);          \
877   /* Note that we do the increment first to             \
878      make sure that the pointer in                      \
879      VALIDATE_BYTIND_FORWARD() ends up on               \
880      the correct side of the gap */                     \
881   (x)++;                                                \
882   VALIDATE_BYTIND_FORWARD (buf, x);                     \
883 } while (0)
884
885 /* Note that in the simplest case (no MULE, no ERROR_CHECK_BUFPOS),
886    this crap reduces down to simply (x)--. */
887
888 #define DEC_BYTIND(buf, x) do                           \
889 {                                                       \
890   ASSERT_VALID_BYTIND_BACKWARD_UNSAFE (buf, x);         \
891   /* Note that we do the decrement first to             \
892      make sure that the pointer in                      \
893      VALIDATE_BYTIND_BACKWARD() ends up on              \
894      the correct side of the gap */                     \
895   (x)--;                                                \
896   VALIDATE_BYTIND_BACKWARD (buf, x);                    \
897 } while (0)
898
899 INLINE Bytind prev_bytind (struct buffer *buf, Bytind x);
900 INLINE Bytind
901 prev_bytind (struct buffer *buf, Bytind x)
902 {
903   DEC_BYTIND (buf, x);
904   return x;
905 }
906
907 INLINE Bytind next_bytind (struct buffer *buf, Bytind x);
908 INLINE Bytind
909 next_bytind (struct buffer *buf, Bytind x)
910 {
911   INC_BYTIND (buf, x);
912   return x;
913 }
914
915 #define BYTIND_INVALID ((Bytind) -1)
916
917 /*----------------------------------------------------------------------*/
918 /*         Converting between buffer positions and byte indices         */
919 /*----------------------------------------------------------------------*/
920
921 #ifdef MULE
922
923 Bytind bufpos_to_bytind_func (struct buffer *buf, Bufpos x);
924 Bufpos bytind_to_bufpos_func (struct buffer *buf, Bytind x);
925
926 /* The basic algorithm we use is to keep track of a known region of
927    characters in each buffer, all of which are of the same width.  We
928    keep track of the boundaries of the region in both Bufpos and
929    Bytind coordinates and also keep track of the char width, which
930    is 1 - 4 bytes.  If the position we're translating is not in
931    the known region, then we invoke a function to update the known
932    region to surround the position in question.  This assumes
933    locality of reference, which is usually the case.
934
935    Note that the function to update the known region can be simple
936    or complicated depending on how much information we cache.
937    For the moment, we don't cache any information, and just move
938    linearly forward or back from the known region, with a few
939    shortcuts to catch all-ASCII buffers. (Note that this will
940    thrash with bad locality of reference.) A smarter method would
941    be to keep some sort of pseudo-extent layer over the buffer;
942    maybe keep track of the bufpos/bytind correspondence at the
943    beginning of each line, which would allow us to do a binary
944    search over the pseudo-extents to narrow things down to the
945    correct line, at which point you could use a linear movement
946    method.  This would also mesh well with efficiently
947    implementing a line-numbering scheme.
948
949    Note also that we have to multiply or divide by the char width
950    in order to convert the positions.  We do some tricks to avoid
951    ever actually having to do a multiply or divide, because that
952    is typically an expensive operation (esp. divide).  Multiplying
953    or dividing by 1, 2, or 4 can be implemented simply as a
954    shift left or shift right, and we keep track of a shifter value
955    (0, 1, or 2) indicating how much to shift.  Multiplying by 3
956    can be implemented by doubling and then adding the original
957    value.  Dividing by 3, alas, cannot be implemented in any
958    simple shift/subtract method, as far as I know; so we just
959    do a table lookup.  For simplicity, we use a table of size
960    128K, which indexes the "divide-by-3" values for the first
961    64K non-negative numbers. (Note that we can increase the
962    size up to 384K, i.e. indexing the first 192K non-negative
963    numbers, while still using shorts in the array.) This also
964    means that the size of the known region can be at most
965    64K for width-three characters.
966    */
967
968 #ifndef UTF2000
969 extern short three_to_one_table[];
970 #endif
971
972 INLINE int real_bufpos_to_bytind (struct buffer *buf, Bufpos x);
973 INLINE int
974 real_bufpos_to_bytind (struct buffer *buf, Bufpos x)
975 {
976   if (x >= buf->text->mule_bufmin && x <= buf->text->mule_bufmax)
977     return (buf->text->mule_bytmin +
978 #ifdef UTF2000
979             (x - buf->text->mule_bufmin) * buf->text->mule_size
980 #else
981             ((x - buf->text->mule_bufmin) << buf->text->mule_shifter) +
982             (buf->text->mule_three_p ? (x - buf->text->mule_bufmin) : 0)
983 #endif
984             );
985   else
986     return bufpos_to_bytind_func (buf, x);
987 }
988
989 INLINE int real_bytind_to_bufpos (struct buffer *buf, Bytind x);
990 INLINE int
991 real_bytind_to_bufpos (struct buffer *buf, Bytind x)
992 {
993   if (x >= buf->text->mule_bytmin && x <= buf->text->mule_bytmax)
994     return (buf->text->mule_bufmin +
995 #ifdef UTF2000
996             (buf->text->mule_size == 0 ? 0 :
997              (x - buf->text->mule_bytmin) / buf->text->mule_size)
998 #else
999             ((buf->text->mule_three_p
1000               ? three_to_one_table[x - buf->text->mule_bytmin]
1001               : (x - buf->text->mule_bytmin) >> buf->text->mule_shifter))
1002 #endif
1003             );
1004   else
1005     return bytind_to_bufpos_func (buf, x);
1006 }
1007
1008 #else /* not MULE */
1009
1010 # define real_bufpos_to_bytind(buf, x)  ((Bytind) x)
1011 # define real_bytind_to_bufpos(buf, x)  ((Bufpos) x)
1012
1013 #endif /* not MULE */
1014
1015 #ifdef ERROR_CHECK_BUFPOS
1016
1017 Bytind bufpos_to_bytind (struct buffer *buf, Bufpos x);
1018 Bufpos bytind_to_bufpos (struct buffer *buf, Bytind x);
1019
1020 #else /* not ERROR_CHECK_BUFPOS */
1021
1022 #define bufpos_to_bytind real_bufpos_to_bytind
1023 #define bytind_to_bufpos real_bytind_to_bufpos
1024
1025 #endif /* not ERROR_CHECK_BUFPOS */
1026
1027 #define make_bufpos(buf, ind) make_int (bytind_to_bufpos (buf, ind))
1028
1029 /*----------------------------------------------------------------------*/
1030 /*         Converting between buffer bytes and Emacs characters         */
1031 /*----------------------------------------------------------------------*/
1032
1033 /* The character at position POS in buffer. */
1034 #define BI_BUF_FETCH_CHAR(buf, pos) \
1035   charptr_emchar (BI_BUF_BYTE_ADDRESS (buf, pos))
1036 #define BUF_FETCH_CHAR(buf, pos) \
1037   BI_BUF_FETCH_CHAR (buf, bufpos_to_bytind (buf, pos))
1038
1039 /* The character at position POS in buffer, as a string.  This is
1040    equivalent to set_charptr_emchar (str, BUF_FETCH_CHAR (buf, pos))
1041    but is faster for Mule. */
1042
1043 # define BI_BUF_CHARPTR_COPY_CHAR(buf, pos, str) \
1044   charptr_copy_char (BI_BUF_BYTE_ADDRESS (buf, pos), str)
1045 #define BUF_CHARPTR_COPY_CHAR(buf, pos, str) \
1046   BI_BUF_CHARPTR_COPY_CHAR (buf, bufpos_to_bytind (buf, pos), str)
1047
1048
1049
1050 \f
1051 /************************************************************************/
1052 /*                                                                      */
1053 /*                  working with externally-formatted data              */
1054 /*                                                                      */
1055 /************************************************************************/
1056
1057 /* Sometimes strings need to be converted into one or another
1058    external format, for passing to a library function. (Note
1059    that we encapsulate and automatically convert the arguments
1060    of some functions, but not others.) At times this conversion
1061    also has to go the other way -- i.e. when we get external-
1062    format strings back from a library function.
1063 */
1064
1065 #ifdef FILE_CODING
1066
1067 /* WARNING: These use a static buffer.  This can lead to disaster if
1068    these functions are not used *very* carefully.  Under normal
1069    circumstances, do not call these functions; call the front ends
1070    below. */
1071
1072 Extbyte *convert_to_external_format (CONST Bufbyte *ptr,
1073                                      Bytecount len,
1074                                      Extcount *len_out,
1075                                      enum external_data_format fmt);
1076 Bufbyte *convert_from_external_format (CONST Extbyte *ptr,
1077                                        Extcount len,
1078                                        Bytecount *len_out,
1079                                        enum external_data_format fmt);
1080
1081 #else /* ! MULE */
1082
1083 #define convert_to_external_format(ptr, len, len_out, fmt) \
1084      (*(len_out) = (int) (len), (Extbyte *) (ptr))
1085 #define convert_from_external_format(ptr, len, len_out, fmt) \
1086      (*(len_out) = (Bytecount) (len), (Bufbyte *) (ptr))
1087
1088 #endif /* ! MULE */
1089
1090 /* In all of the following macros we use the following general principles:
1091
1092    -- Functions that work with charptr's accept two sorts of charptr's:
1093
1094       a) Pointers to memory with a length specified.  The pointer will be
1095          fundamentally of type `unsigned char *' (although labelled
1096          as `Bufbyte *' for internal-format data and `Extbyte *' for
1097          external-format data) and the length will be fundamentally of
1098          type `int' (although labelled as `Bytecount' for internal-format
1099          data and `Extcount' for external-format data).  The length is
1100          always a count in bytes.
1101       b) Zero-terminated pointers; no length specified.  The pointer
1102          is of type `char *', whether the data pointed to is internal-format
1103          or external-format.  These sorts of pointers are available for
1104          convenience in working with C library functions and literal
1105          strings.  In general you should use these sorts of pointers only
1106          to interface to library routines and not for general manipulation,
1107          as you are liable to lose embedded nulls and such.  This could
1108          be a big problem for routines that want Unicode-formatted data,
1109          which is likely to have lots of embedded nulls in it.
1110          (In the real world, though, external Unicode data will be UTF-8,
1111          which will not have embedded nulls and is ASCII-compatible - martin)
1112
1113    -- Functions that work with Lisp strings accept strings as Lisp Objects
1114       (as opposed to the `struct Lisp_String *' for some of the other
1115       string accessors).  This is for convenience in working with the
1116       functions, as otherwise you will almost always have to call
1117       XSTRING() on the object.
1118
1119    -- Functions that work with charptr's are not guaranteed to copy
1120       their data into alloca()ed space.  Functions that work with
1121       Lisp strings are, however.  The reason is that Lisp strings can
1122       be relocated any time a GC happens, and it could happen at some
1123       rather unexpected times.  The internal-external conversion is
1124       rarely done in time-critical functions, and so the slight
1125       extra time required for alloca() and copy is well-worth the
1126       safety of knowing your string data won't be relocated out from
1127       under you.
1128       */
1129
1130
1131 /* Maybe convert charptr's data into ext-format and store the result in
1132    alloca()'ed space.
1133
1134    You may wonder why this is written in this fashion and not as a
1135    function call.  With a little trickery it could certainly be
1136    written this way, but it won't work because of those DAMN GCC WANKERS
1137    who couldn't be bothered to handle alloca() properly on the x86
1138    architecture. (If you put a call to alloca() in the argument to
1139    a function call, the stack space gets allocated right in the
1140    middle of the arguments to the function call and you are unbelievably
1141    hosed.) */
1142
1143 #ifdef MULE
1144
1145 #define GET_CHARPTR_EXT_DATA_ALLOCA(ptr, len, fmt, ptr_out, len_out) do \
1146 {                                                                       \
1147   Bytecount gceda_len_in = (Bytecount) (len);                           \
1148   Extcount  gceda_len_out;                                              \
1149   CONST Bufbyte *gceda_ptr_in = (ptr);                                  \
1150   Extbyte *gceda_ptr_out =                                              \
1151     convert_to_external_format (gceda_ptr_in, gceda_len_in,             \
1152                                 &gceda_len_out, fmt);                   \
1153   /* If the new string is identical to the old (will be the case most   \
1154      of the time), just return the same string back.  This saves        \
1155      on alloca()ing, which can be useful on C alloca() machines and     \
1156      on stack-space-challenged environments. */                         \
1157                                                                         \
1158   if (gceda_len_in == gceda_len_out &&                                  \
1159       !memcmp (gceda_ptr_in, gceda_ptr_out, gceda_len_out))             \
1160     {                                                                   \
1161       (ptr_out) = (Extbyte *) gceda_ptr_in;                             \
1162     }                                                                   \
1163   else                                                                  \
1164     {                                                                   \
1165       (ptr_out) = (Extbyte *) alloca (1 + gceda_len_out);               \
1166       memcpy ((void *) ptr_out, gceda_ptr_out, 1 + gceda_len_out);      \
1167     }                                                                   \
1168   (len_out) = gceda_len_out;                                            \
1169 } while (0)
1170
1171 #else /* ! MULE */
1172
1173 #define GET_CHARPTR_EXT_DATA_ALLOCA(ptr, len, fmt, ptr_out, len_out) do \
1174 {                                       \
1175   (ptr_out) = (Extbyte *) (ptr);        \
1176   (len_out) = (Extcount) (len);         \
1177 } while (0)
1178
1179 #endif /* ! MULE */
1180
1181 #define GET_C_CHARPTR_EXT_DATA_ALLOCA(ptr, fmt, ptr_out) do     \
1182 {                                                               \
1183   Extcount gcceda_ignored_len;                                  \
1184   CONST Bufbyte *gcceda_ptr_in = (CONST Bufbyte *) (ptr);       \
1185   Extbyte *gcceda_ptr_out;                                      \
1186                                                                 \
1187   GET_CHARPTR_EXT_DATA_ALLOCA (gcceda_ptr_in,                   \
1188                                strlen ((char *) gcceda_ptr_in), \
1189                                fmt,                             \
1190                                gcceda_ptr_out,                  \
1191                                gcceda_ignored_len);             \
1192   (ptr_out) = (char *) gcceda_ptr_out;                          \
1193 } while (0)
1194
1195 #define GET_C_CHARPTR_EXT_BINARY_DATA_ALLOCA(ptr, ptr_out) \
1196   GET_C_CHARPTR_EXT_DATA_ALLOCA (ptr, FORMAT_BINARY, ptr_out)
1197 #define GET_CHARPTR_EXT_BINARY_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1198   GET_CHARPTR_EXT_DATA_ALLOCA (ptr, len, FORMAT_BINARY, ptr_out, len_out)
1199
1200 #define GET_C_CHARPTR_EXT_FILENAME_DATA_ALLOCA(ptr, ptr_out) \
1201   GET_C_CHARPTR_EXT_DATA_ALLOCA (ptr, FORMAT_FILENAME, ptr_out)
1202 #define GET_CHARPTR_EXT_FILENAME_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1203   GET_CHARPTR_EXT_DATA_ALLOCA (ptr, len, FORMAT_FILENAME, ptr_out, len_out)
1204
1205 #define GET_C_CHARPTR_EXT_CTEXT_DATA_ALLOCA(ptr, ptr_out) \
1206   GET_C_CHARPTR_EXT_DATA_ALLOCA (ptr, FORMAT_CTEXT, ptr_out)
1207 #define GET_CHARPTR_EXT_CTEXT_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1208   GET_CHARPTR_EXT_DATA_ALLOCA (ptr, len, FORMAT_CTEXT, ptr_out, len_out)
1209
1210 /* Maybe convert external charptr's data into internal format and store
1211    the result in alloca()'ed space.
1212
1213    You may wonder why this is written in this fashion and not as a
1214    function call.  With a little trickery it could certainly be
1215    written this way, but it won't work because of those DAMN GCC WANKERS
1216    who couldn't be bothered to handle alloca() properly on the x86
1217    architecture. (If you put a call to alloca() in the argument to
1218    a function call, the stack space gets allocated right in the
1219    middle of the arguments to the function call and you are unbelievably
1220    hosed.) */
1221
1222 #ifdef MULE
1223
1224 #define GET_CHARPTR_INT_DATA_ALLOCA(ptr, len, fmt, ptr_out, len_out) do \
1225 {                                                                       \
1226   Extcount gcida_len_in = (Extcount) (len);                             \
1227   Bytecount gcida_len_out;                                              \
1228   CONST Extbyte *gcida_ptr_in = (ptr);                                  \
1229   Bufbyte *gcida_ptr_out =                                              \
1230     convert_from_external_format (gcida_ptr_in, gcida_len_in,           \
1231                                   &gcida_len_out, fmt);                 \
1232   /* If the new string is identical to the old (will be the case most   \
1233      of the time), just return the same string back.  This saves        \
1234      on alloca()ing, which can be useful on C alloca() machines and     \
1235      on stack-space-challenged environments. */                         \
1236                                                                         \
1237   if (gcida_len_in == gcida_len_out &&                                  \
1238       !memcmp (gcida_ptr_in, gcida_ptr_out, gcida_len_out))             \
1239     {                                                                   \
1240       (ptr_out) = (Bufbyte *) gcida_ptr_in;                             \
1241     }                                                                   \
1242   else                                                                  \
1243     {                                                                   \
1244       (ptr_out) = (Extbyte *) alloca (1 + gcida_len_out);               \
1245       memcpy ((void *) ptr_out, gcida_ptr_out, 1 + gcida_len_out);      \
1246     }                                                                   \
1247   (len_out) = gcida_len_out;                                            \
1248 } while (0)
1249
1250 #else /* ! MULE */
1251
1252 #define GET_CHARPTR_INT_DATA_ALLOCA(ptr, len, fmt, ptr_out, len_out) do \
1253 {                                       \
1254   (ptr_out) = (Bufbyte *) (ptr);        \
1255   (len_out) = (Bytecount) (len);        \
1256 } while (0)
1257
1258 #endif /* ! MULE */
1259
1260 #define GET_C_CHARPTR_INT_DATA_ALLOCA(ptr, fmt, ptr_out) do     \
1261 {                                                               \
1262   Bytecount gccida_ignored_len;                                 \
1263   CONST Extbyte *gccida_ptr_in = (CONST Extbyte *) (ptr);       \
1264   Bufbyte *gccida_ptr_out;                                      \
1265                                                                 \
1266   GET_CHARPTR_INT_DATA_ALLOCA (gccida_ptr_in,                   \
1267                                strlen ((char *) gccida_ptr_in), \
1268                                fmt,                             \
1269                                gccida_ptr_out,                  \
1270                                gccida_ignored_len);             \
1271   (ptr_out) = gccida_ptr_out;                                   \
1272 } while (0)
1273
1274 #define GET_C_CHARPTR_INT_BINARY_DATA_ALLOCA(ptr, ptr_out)      \
1275   GET_C_CHARPTR_INT_DATA_ALLOCA (ptr, FORMAT_BINARY, ptr_out)
1276 #define GET_CHARPTR_INT_BINARY_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1277   GET_CHARPTR_INT_DATA_ALLOCA (ptr, len, FORMAT_BINARY, ptr_out, len_out)
1278
1279 #define GET_C_CHARPTR_INT_FILENAME_DATA_ALLOCA(ptr, ptr_out)    \
1280   GET_C_CHARPTR_INT_DATA_ALLOCA (ptr, FORMAT_FILENAME, ptr_out)
1281 #define GET_CHARPTR_INT_FILENAME_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1282   GET_CHARPTR_INT_DATA_ALLOCA (ptr, len, FORMAT_FILENAME, ptr_out, len_out)
1283
1284 #define GET_C_CHARPTR_INT_CTEXT_DATA_ALLOCA(ptr, ptr_out)       \
1285   GET_C_CHARPTR_INT_DATA_ALLOCA (ptr, FORMAT_CTEXT, ptr_out)
1286 #define GET_CHARPTR_INT_CTEXT_DATA_ALLOCA(ptr, len, ptr_out, len_out) \
1287   GET_CHARPTR_INT_DATA_ALLOCA (ptr, len, FORMAT_CTEXT, ptr_out, len_out)
1288
1289
1290 /* Maybe convert Lisp string's data into ext-format and store the result in
1291    alloca()'ed space.
1292
1293    You may wonder why this is written in this fashion and not as a
1294    function call.  With a little trickery it could certainly be
1295    written this way, but it won't work because of those DAMN GCC WANKERS
1296    who couldn't be bothered to handle alloca() properly on the x86
1297    architecture. (If you put a call to alloca() in the argument to
1298    a function call, the stack space gets allocated right in the
1299    middle of the arguments to the function call and you are unbelievably
1300    hosed.) */
1301
1302 #define GET_STRING_EXT_DATA_ALLOCA(s, fmt, ptr_out, len_out) do \
1303 {                                                               \
1304   Extcount gseda_len_out;                                       \
1305   struct Lisp_String *gseda_s = XSTRING (s);                    \
1306   Extbyte * gseda_ptr_out =                                     \
1307     convert_to_external_format (string_data (gseda_s),          \
1308                                 string_length (gseda_s),        \
1309                                 &gseda_len_out, fmt);           \
1310   (ptr_out) = (Extbyte *) alloca (1 + gseda_len_out);           \
1311   memcpy ((void *) ptr_out, gseda_ptr_out, 1 + gseda_len_out);  \
1312   (len_out) = gseda_len_out;                                    \
1313 } while (0)
1314
1315
1316 #define GET_C_STRING_EXT_DATA_ALLOCA(s, fmt, ptr_out) do        \
1317 {                                                               \
1318   Extcount gcseda_ignored_len;                                  \
1319   Extbyte *gcseda_ptr_out;                                      \
1320                                                                 \
1321   GET_STRING_EXT_DATA_ALLOCA (s, fmt, gcseda_ptr_out,           \
1322                               gcseda_ignored_len);              \
1323   (ptr_out) = (char *) gcseda_ptr_out;                          \
1324 } while (0)
1325
1326 #define GET_STRING_BINARY_DATA_ALLOCA(s, ptr_out, len_out)      \
1327   GET_STRING_EXT_DATA_ALLOCA (s, FORMAT_BINARY, ptr_out, len_out)
1328 #define GET_C_STRING_BINARY_DATA_ALLOCA(s, ptr_out)             \
1329   GET_C_STRING_EXT_DATA_ALLOCA (s, FORMAT_BINARY, ptr_out)
1330
1331 #define GET_STRING_FILENAME_DATA_ALLOCA(s, ptr_out, len_out)    \
1332   GET_STRING_EXT_DATA_ALLOCA (s, FORMAT_FILENAME, ptr_out, len_out)
1333 #define GET_C_STRING_FILENAME_DATA_ALLOCA(s, ptr_out)           \
1334   GET_C_STRING_EXT_DATA_ALLOCA (s, FORMAT_FILENAME, ptr_out)
1335
1336 #define GET_STRING_OS_DATA_ALLOCA(s, ptr_out, len_out)          \
1337   GET_STRING_EXT_DATA_ALLOCA (s, FORMAT_OS, ptr_out, len_out)
1338 #define GET_C_STRING_OS_DATA_ALLOCA(s, ptr_out)                 \
1339   GET_C_STRING_EXT_DATA_ALLOCA (s, FORMAT_OS, ptr_out)
1340
1341 #define GET_STRING_CTEXT_DATA_ALLOCA(s, ptr_out, len_out)       \
1342   GET_STRING_EXT_DATA_ALLOCA (s, FORMAT_CTEXT, ptr_out, len_out)
1343 #define GET_C_STRING_CTEXT_DATA_ALLOCA(s, ptr_out)              \
1344   GET_C_STRING_EXT_DATA_ALLOCA (s, FORMAT_CTEXT, ptr_out)
1345
1346
1347 \f
1348 /************************************************************************/
1349 /*                                                                      */
1350 /*                          fake charset functions                      */
1351 /*                                                                      */
1352 /************************************************************************/
1353
1354 /* used when MULE is not defined, so that Charset-type stuff can still
1355    be done */
1356
1357 #ifndef MULE
1358
1359 typedef int Charset_ID;
1360
1361 #define Vcharset_ascii Qnil
1362
1363 #define CHAR_CHARSET(ch) Vcharset_ascii
1364 #define CHAR_LEADING_BYTE(ch) LEADING_BYTE_ASCII
1365 #define LEADING_BYTE_ASCII 0x80
1366 #define NUM_LEADING_BYTES 1
1367 #define MIN_LEADING_BYTE 0x80
1368 #define CHARSETP(cs) 1
1369 #define CHARSET_BY_LEADING_BYTE(lb) Vcharset_ascii
1370 #define XCHARSET_LEADING_BYTE(cs) LEADING_BYTE_ASCII
1371 #define XCHARSET_GRAPHIC(cs) -1
1372 #define XCHARSET_COLUMNS(cs) 1
1373 #define XCHARSET_DIMENSION(cs) 1
1374 #define REP_BYTES_BY_FIRST_BYTE(fb) 1
1375 #define BREAKUP_CHAR(ch, charset, byte1, byte2) do {    \
1376   (charset) = Vcharset_ascii;                           \
1377   (byte1) = (ch);                                       \
1378   (byte2) = 0;                                          \
1379 } while (0)
1380 #define BYTE_ASCII_P(byte) 1
1381
1382 #endif /* ! MULE */
1383 \f
1384 /************************************************************************/
1385 /*                                                                      */
1386 /*                  higher-level buffer-position functions              */
1387 /*                                                                      */
1388 /************************************************************************/
1389
1390 /*----------------------------------------------------------------------*/
1391 /*           Settor macros for important positions in a buffer          */
1392 /*----------------------------------------------------------------------*/
1393
1394 /* Set beginning of accessible range of buffer.  */
1395 #define SET_BOTH_BUF_BEGV(buf, val, bival)      \
1396 do                                              \
1397 {                                               \
1398   (buf)->begv = (bival);                        \
1399   (buf)->bufbegv = (val);                       \
1400 } while (0)
1401
1402 /* Set end of accessible range of buffer.  */
1403 #define SET_BOTH_BUF_ZV(buf, val, bival)        \
1404 do                                              \
1405 {                                               \
1406   (buf)->zv = (bival);                          \
1407   (buf)->bufzv = (val);                         \
1408 } while (0)
1409
1410 /* Set point. */
1411 /* Since BEGV and ZV are almost never set, it's reasonable to enforce
1412    the restriction that the Bufpos and Bytind values must both be
1413    specified.  However, point is set in lots and lots of places.  So
1414    we provide the ability to specify both (for efficiency) or just
1415    one. */
1416 #define BOTH_BUF_SET_PT(buf, val, bival) set_buffer_point (buf, val, bival)
1417 #define BI_BUF_SET_PT(buf, bival) \
1418   BOTH_BUF_SET_PT (buf, bytind_to_bufpos (buf, bival), bival)
1419 #define BUF_SET_PT(buf, value) \
1420   BOTH_BUF_SET_PT (buf, value, bufpos_to_bytind (buf, value))
1421
1422
1423 #if 0 /* FSFmacs */
1424 /* These macros exist in FSFmacs because SET_PT() in FSFmacs incorrectly
1425    does too much stuff, such as moving out of invisible extents. */
1426 #define TEMP_SET_PT(position) (temp_set_point ((position), current_buffer))
1427 #define SET_BUF_PT(buf, value) ((buf)->pt = (value))
1428 #endif /* FSFmacs */
1429
1430 /*----------------------------------------------------------------------*/
1431 /*                      Miscellaneous buffer values                     */
1432 /*----------------------------------------------------------------------*/
1433
1434 /* Number of characters in buffer */
1435 #define BUF_SIZE(buf) (BUF_Z (buf) - BUF_BEG (buf))
1436
1437 /* Is this buffer narrowed? */
1438 #define BUF_NARROWED(buf) \
1439    ((BI_BUF_BEGV (buf) != BI_BUF_BEG (buf)) || \
1440     (BI_BUF_ZV   (buf) != BI_BUF_Z   (buf)))
1441
1442 /* Modification count.  */
1443 #define BUF_MODIFF(buf) ((buf)->text->modiff)
1444
1445 /* Saved modification count.  */
1446 #define BUF_SAVE_MODIFF(buf) ((buf)->text->save_modiff)
1447
1448 /* Face changed.  */
1449 #define BUF_FACECHANGE(buf) ((buf)->face_change)
1450
1451 #define POINT_MARKER_P(marker) \
1452    (XMARKER (marker)->buffer != 0 && \
1453     EQ ((marker), XMARKER (marker)->buffer->point_marker))
1454
1455 #define BUF_MARKERS(buf) ((buf)->markers)
1456
1457 /* WARNING:
1458
1459    The new definitions of CEILING_OF() and FLOOR_OF() differ semantically
1460    from the old ones (in FSF Emacs and XEmacs 19.11 and before).
1461    Conversion is as follows:
1462
1463    OLD_BI_CEILING_OF(n) = NEW_BI_CEILING_OF(n) - 1
1464    OLD_BI_FLOOR_OF(n) = NEW_BI_FLOOR_OF(n + 1)
1465
1466    The definitions were changed because the new definitions are more
1467    consistent with the way everything else works in Emacs.
1468  */
1469
1470 /* Properties of CEILING_OF and FLOOR_OF (also apply to BI_ variants):
1471
1472    1) FLOOR_OF (CEILING_OF (n)) = n
1473       CEILING_OF (FLOOR_OF (n)) = n
1474
1475    2) CEILING_OF (n) = n if and only if n = ZV
1476       FLOOR_OF (n) = n if and only if n = BEGV
1477
1478    3) CEILING_OF (CEILING_OF (n)) = ZV
1479       FLOOR_OF (FLOOR_OF (n)) = BEGV
1480
1481    4) The bytes in the regions
1482
1483       [BYTE_ADDRESS (n), BYTE_ADDRESS_BEFORE (CEILING_OF (n))]
1484
1485       and
1486
1487       [BYTE_ADDRESS (FLOOR_OF (n)), BYTE_ADDRESS_BEFORE (n)]
1488
1489       are contiguous.
1490    */
1491
1492
1493 /*  Return the maximum index in the buffer it is safe to scan forwards
1494     past N to.  This is used to prevent buffer scans from running into
1495     the gap (e.g. search.c).  All characters between N and CEILING_OF(N)
1496     are located contiguous in memory.  Note that the character *at*
1497     CEILING_OF(N) is not contiguous in memory. */
1498 #define BI_BUF_CEILING_OF(b, n)                                         \
1499   ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_ZV (b) ?             \
1500    (b)->text->gpt : BI_BUF_ZV (b))
1501 #define BUF_CEILING_OF(b, n)                                            \
1502   bytind_to_bufpos (b, BI_BUF_CEILING_OF (b, bufpos_to_bytind (b, n)))
1503
1504 /*  Return the minimum index in the buffer it is safe to scan backwards
1505     past N to.  All characters between FLOOR_OF(N) and N are located
1506     contiguous in memory.  Note that the character *at* N may not be
1507     contiguous in memory. */
1508 #define BI_BUF_FLOOR_OF(b, n)                                           \
1509         (BI_BUF_BEGV (b) < (b)->text->gpt && (b)->text->gpt < (n) ?     \
1510          (b)->text->gpt : BI_BUF_BEGV (b))
1511 #define BUF_FLOOR_OF(b, n)                                              \
1512   bytind_to_bufpos (b, BI_BUF_FLOOR_OF (b, bufpos_to_bytind (b, n)))
1513
1514 #define BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n)                       \
1515   ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_Z (b) ?              \
1516    (b)->text->gpt : BI_BUF_Z (b))
1517 #define BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n)                          \
1518   bytind_to_bufpos                                                      \
1519    (b, BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
1520
1521 #define BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n)                         \
1522         (BI_BUF_BEG (b) < (b)->text->gpt && (b)->text->gpt < (n) ?      \
1523          (b)->text->gpt : BI_BUF_BEG (b))
1524 #define BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n)                            \
1525   bytind_to_bufpos                                                      \
1526    (b, BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
1527
1528
1529 extern struct buffer *current_buffer;
1530
1531 /* This is the initial (startup) directory, as used for the *scratch* buffer.
1532    We're making this a global to make others aware of the startup directory.
1533    `initial_directory' is stored in external format.
1534  */
1535 extern char initial_directory[];
1536 extern void init_initial_directory (void);   /* initialize initial_directory */
1537
1538 EXFUN (Fbuffer_disable_undo, 1);
1539 EXFUN (Fbuffer_modified_p, 1);
1540 EXFUN (Fbuffer_name, 1);
1541 EXFUN (Fcurrent_buffer, 0);
1542 EXFUN (Ferase_buffer, 1);
1543 EXFUN (Fget_buffer, 1);
1544 EXFUN (Fget_buffer_create, 1);
1545 EXFUN (Fget_file_buffer, 1);
1546 EXFUN (Fkill_buffer, 1);
1547 EXFUN (Fother_buffer, 3);
1548 EXFUN (Frecord_buffer, 1);
1549 EXFUN (Fset_buffer, 1);
1550 EXFUN (Fset_buffer_modified_p, 2);
1551
1552 extern Lisp_Object QSscratch, Qafter_change_function, Qafter_change_functions;
1553 extern Lisp_Object Qbefore_change_function, Qbefore_change_functions;
1554 extern Lisp_Object Qbuffer_or_string_p, Qdefault_directory, Qfirst_change_hook;
1555 extern Lisp_Object Qpermanent_local, Vafter_change_function;
1556 extern Lisp_Object Vafter_change_functions, Vbefore_change_function;
1557 extern Lisp_Object Vbefore_change_functions, Vbuffer_alist, Vbuffer_defaults;
1558 extern Lisp_Object Vinhibit_read_only, Vtransient_mark_mode;
1559
1560 /* This structure marks which slots in a buffer have corresponding
1561    default values in Vbuffer_defaults.
1562    Each such slot has a nonzero value in this structure.
1563    The value has only one nonzero bit.
1564
1565    When a buffer has its own local value for a slot,
1566    the bit for that slot (found in the same slot in this structure)
1567    is turned on in the buffer's local_var_flags slot.
1568
1569    If a slot in this structure is zero, then even though there may
1570    be a DEFVAR_BUFFER_LOCAL for the slot, there is no default value for it;
1571    and the corresponding slot in Vbuffer_defaults is not used.  */
1572
1573 extern struct buffer buffer_local_flags;
1574
1575
1576 /* Allocation of buffer data. */
1577
1578 #ifdef REL_ALLOC
1579
1580 char *r_alloc (unsigned char **, unsigned long);
1581 char *r_re_alloc (unsigned char **, unsigned long);
1582 void r_alloc_free (unsigned char **);
1583
1584 #define BUFFER_ALLOC(data, size) \
1585   ((Bufbyte *) r_alloc ((unsigned char **) &data, (size) * sizeof(Bufbyte)))
1586 #define BUFFER_REALLOC(data, size) \
1587   ((Bufbyte *) r_re_alloc ((unsigned char **) &data, (size) * sizeof(Bufbyte)))
1588 #define BUFFER_FREE(data) r_alloc_free ((unsigned char **) &(data))
1589 #define R_ALLOC_DECLARE(var,data) r_alloc_declare (&(var), data)
1590
1591 #else /* !REL_ALLOC */
1592
1593 #define BUFFER_ALLOC(data,size)\
1594         (data = xnew_array (Bufbyte, size))
1595 #define BUFFER_REALLOC(data,size)\
1596         ((Bufbyte *) xrealloc (data, (size) * sizeof(Bufbyte)))
1597 /* Avoid excess parentheses, or syntax errors may rear their heads. */
1598 #define BUFFER_FREE(data) xfree (data)
1599 #define R_ALLOC_DECLARE(var,data)
1600
1601 #endif /* !REL_ALLOC */
1602
1603 extern Lisp_Object Vbuffer_alist;
1604 void set_buffer_internal (struct buffer *b);
1605 struct buffer *decode_buffer (Lisp_Object buffer, int allow_string);
1606
1607 /* from editfns.c */
1608 void widen_buffer (struct buffer *b, int no_clip);
1609 int beginning_of_line_p (struct buffer *b, Bufpos pt);
1610
1611 /* from insdel.c */
1612 void set_buffer_point (struct buffer *buf, Bufpos pos, Bytind bipos);
1613 void find_charsets_in_bufbyte_string (Charset_ID *charsets,
1614                                       CONST Bufbyte *str,
1615                                       Bytecount len);
1616 void find_charsets_in_emchar_string (Charset_ID *charsets,
1617                                      CONST Emchar *str,
1618                                      Charcount len);
1619 int bufbyte_string_displayed_columns (CONST Bufbyte *str, Bytecount len);
1620 int emchar_string_displayed_columns (CONST Emchar *str, Charcount len);
1621 void convert_bufbyte_string_into_emchar_dynarr (CONST Bufbyte *str,
1622                                                 Bytecount len,
1623                                                 Emchar_dynarr *dyn);
1624 Charcount convert_bufbyte_string_into_emchar_string (CONST Bufbyte *str,
1625                                                      Bytecount len,
1626                                                      Emchar *arr);
1627 void convert_emchar_string_into_bufbyte_dynarr (Emchar *arr, int nels,
1628                                                 Bufbyte_dynarr *dyn);
1629 Bufbyte *convert_emchar_string_into_malloced_string (Emchar *arr, int nels,
1630                                                     Bytecount *len_out);
1631 /* from marker.c */
1632 void init_buffer_markers (struct buffer *b);
1633 void uninit_buffer_markers (struct buffer *b);
1634
1635 /* flags for get_buffer_pos_char(), get_buffer_range_char(), etc. */
1636 /* At most one of GB_COERCE_RANGE and GB_NO_ERROR_IF_BAD should be
1637    specified.  At most one of GB_NEGATIVE_FROM_END and GB_NO_ERROR_IF_BAD
1638    should be specified. */
1639
1640 #define GB_ALLOW_PAST_ACCESSIBLE        (1 << 0)
1641 #define GB_ALLOW_NIL                    (1 << 1)
1642 #define GB_CHECK_ORDER                  (1 << 2)
1643 #define GB_COERCE_RANGE                 (1 << 3)
1644 #define GB_NO_ERROR_IF_BAD              (1 << 4)
1645 #define GB_NEGATIVE_FROM_END            (1 << 5)
1646 #define GB_HISTORICAL_STRING_BEHAVIOR   (GB_NEGATIVE_FROM_END | GB_ALLOW_NIL)
1647
1648 Bufpos get_buffer_pos_char (struct buffer *b, Lisp_Object pos,
1649                             unsigned int flags);
1650 Bytind get_buffer_pos_byte (struct buffer *b, Lisp_Object pos,
1651                             unsigned int flags);
1652 void get_buffer_range_char (struct buffer *b, Lisp_Object from, Lisp_Object to,
1653                             Bufpos *from_out, Bufpos *to_out,
1654                             unsigned int flags);
1655 void get_buffer_range_byte (struct buffer *b, Lisp_Object from, Lisp_Object to,
1656                             Bytind *from_out, Bytind *to_out,
1657                             unsigned int flags);
1658 Charcount get_string_pos_char (Lisp_Object string, Lisp_Object pos,
1659                                unsigned int flags);
1660 Bytecount get_string_pos_byte (Lisp_Object string, Lisp_Object pos,
1661                                unsigned int flags);
1662 void get_string_range_char (Lisp_Object string, Lisp_Object from,
1663                             Lisp_Object to, Charcount *from_out,
1664                             Charcount *to_out, unsigned int flags);
1665 void get_string_range_byte (Lisp_Object string, Lisp_Object from,
1666                             Lisp_Object to, Bytecount *from_out,
1667                             Bytecount *to_out, unsigned int flags);
1668 Bufpos get_buffer_or_string_pos_char (Lisp_Object object, Lisp_Object pos,
1669                                       unsigned int flags);
1670 Bytind get_buffer_or_string_pos_byte (Lisp_Object object, Lisp_Object pos,
1671                                       unsigned int flags);
1672 void get_buffer_or_string_range_char (Lisp_Object object, Lisp_Object from,
1673                                       Lisp_Object to, Bufpos *from_out,
1674                                       Bufpos *to_out, unsigned int flags);
1675 void get_buffer_or_string_range_byte (Lisp_Object object, Lisp_Object from,
1676                                       Lisp_Object to, Bytind *from_out,
1677                                       Bytind *to_out, unsigned int flags);
1678 Bufpos buffer_or_string_accessible_begin_char (Lisp_Object object);
1679 Bufpos buffer_or_string_accessible_end_char (Lisp_Object object);
1680 Bytind buffer_or_string_accessible_begin_byte (Lisp_Object object);
1681 Bytind buffer_or_string_accessible_end_byte (Lisp_Object object);
1682 Bufpos buffer_or_string_absolute_begin_char (Lisp_Object object);
1683 Bufpos buffer_or_string_absolute_end_char (Lisp_Object object);
1684 Bytind buffer_or_string_absolute_begin_byte (Lisp_Object object);
1685 Bytind buffer_or_string_absolute_end_byte (Lisp_Object object);
1686 void record_buffer (Lisp_Object buf);
1687 Lisp_Object get_buffer (Lisp_Object name,
1688                         int error_if_deleted_or_does_not_exist);
1689 int map_over_sharing_buffers (struct buffer *buf,
1690                               int (*mapfun) (struct buffer *buf,
1691                                              void *closure),
1692                               void *closure);
1693
1694 \f
1695 /************************************************************************/
1696 /*                         Case conversion                              */
1697 /************************************************************************/
1698
1699 /* A "trt" table is a mapping from characters to other characters,
1700    typically used to convert between uppercase and lowercase.  For
1701    compatibility reasons, trt tables are currently in the form of
1702    a Lisp string of 256 characters, specifying the conversion for each
1703    of the first 256 Emacs characters (i.e. the 256 Latin-1 characters).
1704    This should be generalized at some point to support conversions for
1705    all of the allowable Mule characters.
1706    */
1707
1708 /* The _1 macros are named as such because they assume that you have
1709    already guaranteed that the character values are all in the range
1710    0 - 255.  Bad lossage will happen otherwise. */
1711
1712 # define MAKE_TRT_TABLE() Fmake_string (make_int (256), make_char (0))
1713 # define TRT_TABLE_AS_STRING(table) XSTRING_DATA (table)
1714 # define TRT_TABLE_CHAR_1(table, ch) \
1715   string_char (XSTRING (table), (Charcount) ch)
1716 # define SET_TRT_TABLE_CHAR_1(table, ch1, ch2) \
1717   set_string_char (XSTRING (table), (Charcount) ch1, ch2)
1718
1719 #ifdef MULE
1720 # define MAKE_MIRROR_TRT_TABLE() make_opaque (256, 0)
1721 # define MIRROR_TRT_TABLE_AS_STRING(table) ((Bufbyte *) XOPAQUE_DATA (table))
1722 # define MIRROR_TRT_TABLE_CHAR_1(table, ch) \
1723   ((Emchar) (MIRROR_TRT_TABLE_AS_STRING (table)[ch]))
1724 # define SET_MIRROR_TRT_TABLE_CHAR_1(table, ch1, ch2) \
1725   (MIRROR_TRT_TABLE_AS_STRING (table)[ch1] = (Bufbyte) (ch2))
1726 #endif
1727
1728 # define IN_TRT_TABLE_DOMAIN(c) (((EMACS_UINT) (c)) <= 255)
1729
1730 #ifdef MULE
1731 #define MIRROR_DOWNCASE_TABLE_AS_STRING(buf) \
1732   MIRROR_TRT_TABLE_AS_STRING (buf->mirror_downcase_table)
1733 #define MIRROR_UPCASE_TABLE_AS_STRING(buf) \
1734   MIRROR_TRT_TABLE_AS_STRING (buf->mirror_upcase_table)
1735 #define MIRROR_CANON_TABLE_AS_STRING(buf) \
1736   MIRROR_TRT_TABLE_AS_STRING (buf->mirror_case_canon_table)
1737 #define MIRROR_EQV_TABLE_AS_STRING(buf) \
1738   MIRROR_TRT_TABLE_AS_STRING (buf->mirror_case_eqv_table)
1739 #else
1740 #define MIRROR_DOWNCASE_TABLE_AS_STRING(buf) \
1741   TRT_TABLE_AS_STRING (buf->downcase_table)
1742 #define MIRROR_UPCASE_TABLE_AS_STRING(buf) \
1743   TRT_TABLE_AS_STRING (buf->upcase_table)
1744 #define MIRROR_CANON_TABLE_AS_STRING(buf) \
1745   TRT_TABLE_AS_STRING (buf->case_canon_table)
1746 #define MIRROR_EQV_TABLE_AS_STRING(buf) \
1747   TRT_TABLE_AS_STRING (buf->case_eqv_table)
1748 #endif
1749
1750 INLINE Emchar TRT_TABLE_OF (Lisp_Object trt, Emchar c);
1751 INLINE Emchar
1752 TRT_TABLE_OF (Lisp_Object trt, Emchar c)
1753 {
1754   return IN_TRT_TABLE_DOMAIN (c) ? TRT_TABLE_CHAR_1 (trt, c) : c;
1755 }
1756
1757 /* Macros used below. */
1758 #define DOWNCASE_TABLE_OF(buf, c) TRT_TABLE_OF (buf->downcase_table, c)
1759 #define UPCASE_TABLE_OF(buf, c) TRT_TABLE_OF (buf->upcase_table, c)
1760
1761 /* 1 if CH is upper case.  */
1762
1763 INLINE int UPPERCASEP (struct buffer *buf, Emchar ch);
1764 INLINE int
1765 UPPERCASEP (struct buffer *buf, Emchar ch)
1766 {
1767   return DOWNCASE_TABLE_OF (buf, ch) != ch;
1768 }
1769
1770 /* 1 if CH is lower case.  */
1771
1772 INLINE int LOWERCASEP (struct buffer *buf, Emchar ch);
1773 INLINE int
1774 LOWERCASEP (struct buffer *buf, Emchar ch)
1775 {
1776   return (UPCASE_TABLE_OF   (buf, ch) != ch &&
1777           DOWNCASE_TABLE_OF (buf, ch) == ch);
1778 }
1779
1780 /* 1 if CH is neither upper nor lower case.  */
1781
1782 INLINE int NOCASEP (struct buffer *buf, Emchar ch);
1783 INLINE int
1784 NOCASEP (struct buffer *buf, Emchar ch)
1785 {
1786   return UPCASE_TABLE_OF (buf, ch) == ch;
1787 }
1788
1789 /* Upcase a character, or make no change if that cannot be done.  */
1790
1791 INLINE Emchar UPCASE (struct buffer *buf, Emchar ch);
1792 INLINE Emchar
1793 UPCASE (struct buffer *buf, Emchar ch)
1794 {
1795   return (DOWNCASE_TABLE_OF (buf, ch) == ch) ? UPCASE_TABLE_OF (buf, ch) : ch;
1796 }
1797
1798 /* Upcase a character known to be not upper case.  Unused. */
1799
1800 #define UPCASE1(buf, ch) UPCASE_TABLE_OF (buf, ch)
1801
1802 /* Downcase a character, or make no change if that cannot be done. */
1803
1804 #define DOWNCASE(buf, ch) DOWNCASE_TABLE_OF (buf, ch)
1805
1806 #endif /* _XEMACS_BUFFER_H_ */