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