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