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