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