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