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