323fb0951045efe2a3b83cd1919571b89bc7bbaf
[chise/xemacs-chise.git] / src / buffer.h
1 /* Header file for the buffer manipulation primitives.
2    Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995
3    Free Software Foundation, Inc.
4    Copyright (C) 1995 Sun Microsystems, Inc.
5
6 This file is part of XEmacs.
7
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* Synched up with: FSF 19.30. */
24
25 /* Authorship:
26
27    FSF: long ago.
28    JWZ: separated out bufslots.h, early in Lemacs.
29    Ben Wing: almost completely rewritten for Mule, 19.12.
30  */
31
32 #ifndef INCLUDED_buffer_h_
33 #define INCLUDED_buffer_h_
34
35 #include "character.h"
36 #include "multibyte.h"
37
38 /************************************************************************/
39 /*                                                                      */
40 /*                    definition of Lisp buffer object                  */
41 /*                                                                      */
42 /************************************************************************/
43
44 /* Note: we keep both Bytind and Bufpos versions of some of the
45    important buffer positions because they are accessed so much.
46    If we didn't do this, we would constantly be invalidating the
47    bufpos<->bytind cache under Mule.
48
49    Note that under non-Mule, both versions will always be the
50    same so we don't really need to keep track of them.  But it
51    simplifies the logic to go ahead and do so all the time and
52    the memory loss is insignificant. */
53
54 /* Formerly, it didn't much matter what went inside the struct buffer_text
55    and what went outside it.  Now it does, with the advent of "indirect
56    buffers" that share text with another buffer.  An indirect buffer
57    shares the same *text* as another buffer, but has its own buffer-local
58    variables, its own accessible region, and its own markers and extents.
59    (Due to the nature of markers, it doesn't actually matter much whether
60    we stick them inside or out of the struct buffer_text -- the user won't
61    notice any difference -- but we go ahead and put them outside for
62    consistency and overall saneness of algorithm.)
63
64    FSFmacs gets away with not maintaining any "children" pointers from
65    a buffer to the indirect buffers that refer to it by putting the
66    markers inside of the struct buffer_text, using markers to keep track
67    of BEGV and ZV in indirect buffers, and relying on the fact that
68    all intervals (text properties and overlays) use markers for their
69    start and end points.  We don't do this for extents (markers are
70    inefficient anyway and take up space), so we have to maintain
71    children pointers.  This is not terribly hard, though, and the
72    code to maintain this is just like the code already present in
73    extent-parent and extent-children.
74    */
75
76 struct buffer_text
77 {
78   Bufbyte *beg;         /* Actual address of buffer contents. */
79   Bytind gpt;           /* Index of gap in buffer. */
80   Bytind z;             /* Index of end of buffer. */
81   Bufpos bufz;          /* Equivalent as a Bufpos. */
82   int gap_size;         /* Size of buffer's gap */
83   int end_gap_size;     /* Size of buffer's end gap */
84   long modiff;          /* This counts buffer-modification events
85                            for this buffer.  It is incremented for
86                            each such event, and never otherwise
87                            changed.  */
88   long save_modiff;     /* Previous value of modiff, as of last
89                            time buffer visited or saved a file.  */
90
91 #ifdef MULE
92   /* We keep track of a "known" region for very fast access.
93      This information is text-only so it goes here. */
94   Bufpos mule_bufmin, mule_bufmax;
95   Bytind mule_bytmin, mule_bytmax;
96 #ifdef UTF2000
97   int mule_size;
98 #else
99   int mule_shifter, mule_three_p;
100 #endif
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   /* change data indicating what portion of the text has changed
141      since the last time this was reset.  Used by redisplay.
142      Logically we should keep this with the text structure, but
143      redisplay resets it for each buffer individually and we don't
144      want interference between an indirect buffer and its base
145      buffer. */
146   struct each_buffer_change_data *changes;
147
148 #ifdef REGION_CACHE_NEEDS_WORK
149   /* If the long line scan cache is enabled (i.e. the buffer-local
150      variable cache-long-line-scans is non-nil), newline_cache
151      points to the newline cache, and width_run_cache points to the
152      width run cache.
153
154      The newline cache records which stretches of the buffer are
155      known *not* to contain newlines, so that they can be skipped
156      quickly when we search for newlines.
157
158      The width run cache records which stretches of the buffer are
159      known to contain characters whose widths are all the same.  If
160      the width run cache maps a character to a value > 0, that value
161      is the character's width; if it maps a character to zero, we
162      don't know what its width is.  This allows compute_motion to
163      process such regions very quickly, using algebra instead of
164      inspecting each character.  See also width_table, below.  */
165   struct region_cache *newline_cache;
166   struct region_cache *width_run_cache;
167 #endif /* REGION_CACHE_NEEDS_WORK */
168
169   /* The markers that refer to this buffer.  This is actually a single
170      marker -- successive elements in its marker `chain' are the other
171      markers referring to this buffer */
172   Lisp_Marker *markers;
173
174   /* The buffer's extent info.  This is its own type, an extent-info
175      object (done this way for ease in marking / finalizing). */
176   Lisp_Object extent_info;
177
178   /* ----------------------------------------------------------------- */
179   /* All the stuff above this line is the responsibility of insdel.c,
180      with some help from marker.c and extents.c.
181      All the stuff below this line is the responsibility of buffer.c. */
182
183   /* In an indirect buffer, this points to the base buffer.
184      In an ordinary buffer, it is 0.
185      We DO mark through this slot. */
186   struct buffer *base_buffer;
187
188   /* List of indirect buffers whose base is this buffer.
189      If we are an indirect buffer, this will be nil.
190      Do NOT mark through this. */
191   Lisp_Object indirect_children;
192
193   /* Flags saying which DEFVAR_PER_BUFFER variables
194      are local to this buffer.  */
195   int local_var_flags;
196
197   /* Set to the modtime of the visited file when read or written.
198      -1 means visited file was nonexistent.
199      0  means visited file modtime unknown; in no case complain
200      about any mismatch on next save attempt.  */
201   int modtime;
202
203   /* the value of text->modiff at the last auto-save.  */
204   long auto_save_modified;
205
206   /* The time at which we detected a failure to auto-save,
207      Or -1 if we didn't have a failure.  */
208   int auto_save_failure_time;
209
210   /* Position in buffer at which display started
211      the last time this buffer was displayed.  */
212   int last_window_start;
213
214   /* Everything from here down must be a Lisp_Object */
215
216 #define MARKED_SLOT(x) Lisp_Object x
217 #include "bufslots.h"
218 #undef MARKED_SLOT
219 };
220
221 DECLARE_LRECORD (buffer, struct buffer);
222 #define XBUFFER(x) XRECORD (x, buffer, struct buffer)
223 #define XSETBUFFER(x, p) XSETRECORD (x, p, buffer)
224 #define BUFFERP(x) RECORDP (x, buffer)
225 #define CHECK_BUFFER(x) CHECK_RECORD (x, buffer)
226 #define CONCHECK_BUFFER(x) CONCHECK_RECORD (x, buffer)
227
228 #define BUFFER_LIVE_P(b) (!NILP ((b)->name))
229
230 #define CHECK_LIVE_BUFFER(x) do {                       \
231   CHECK_BUFFER (x);                                     \
232   if (!BUFFER_LIVE_P (XBUFFER (x)))                     \
233     dead_wrong_type_argument (Qbuffer_live_p, (x));     \
234 } while (0)
235
236 #define CONCHECK_LIVE_BUFFER(x) do {                    \
237   CONCHECK_BUFFER (x);                                  \
238   if (!BUFFER_LIVE_P (XBUFFER (x)))                     \
239     x = wrong_type_argument (Qbuffer_live_p, (x));      \
240 } while (0)
241
242 \f
243 #define BUFFER_BASE_BUFFER(b) ((b)->base_buffer ? (b)->base_buffer : (b))
244
245 /* Map over buffers sharing the same text as MPS_BUF.  MPS_BUFVAR is a
246    variable that gets the buffer values (beginning with the base
247    buffer, then the children), and MPS_BUFCONS should be a temporary
248    Lisp_Object variable.  */
249 #define MAP_INDIRECT_BUFFERS(mps_buf, mps_bufvar, mps_bufcons)                  \
250 for (mps_bufcons = Qunbound,                                                    \
251      mps_bufvar = BUFFER_BASE_BUFFER (mps_buf);                                 \
252      UNBOUNDP (mps_bufcons) ?                                                   \
253         (mps_bufcons = mps_bufvar->indirect_children,                           \
254         1)                                                                      \
255        : (!NILP (mps_bufcons)                                                   \
256           && (mps_bufvar = XBUFFER (XCAR (mps_bufcons)), 1)                     \
257           && (mps_bufcons = XCDR (mps_bufcons), 1));                            \
258      )
259
260 \f
261
262 /************************************************************************/
263 /*                                                                      */
264 /*                 working with raw internal-format data                */
265 /*                                                                      */
266 /************************************************************************/
267
268 /* NOTE: In all the following macros, we follow these rules concerning
269    multiple evaluation of the arguments:
270
271    1) Anything that's an lvalue can be evaluated more than once.
272    2) Anything that's a Lisp Object can be evaluated more than once.
273       This should probably be changed, but this follows the way
274       that all the macros in lisp.h do things.
275    3) 'struct buffer *' arguments can be evaluated more than once.
276    4) Nothing else can be evaluated more than once.  Use inline
277       functions, if necessary, to prevent multiple evaluation.
278    5) An exception to (4) is that there are some macros below that
279       may evaluate their arguments more than once.  They are all
280       denoted with the word "unsafe" in their name and are generally
281       meant to be called only by other macros that have already
282       stored the calling values in temporary variables.
283 */
284
285 \f
286 /*----------------------------------------------------------------------*/
287 /*          Accessor macros for important positions in a buffer         */
288 /*----------------------------------------------------------------------*/
289
290 /* We put them here because some stuff below wants them before the
291    place where we would normally put them. */
292
293 /* None of these are lvalues.  Use the settor macros below to change
294    the positions. */
295
296 /* Beginning of buffer.  */
297 #define BI_BUF_BEG(buf) ((Bytind) 1)
298 #define BUF_BEG(buf) ((Bufpos) 1)
299
300 /* Beginning of accessible range of buffer.  */
301 #define BI_BUF_BEGV(buf) ((buf)->begv + 0)
302 #define BUF_BEGV(buf) ((buf)->bufbegv + 0)
303
304 /* End of accessible range of buffer.  */
305 #define BI_BUF_ZV(buf) ((buf)->zv + 0)
306 #define BUF_ZV(buf) ((buf)->bufzv + 0)
307
308 /* End of buffer.  */
309 #define BI_BUF_Z(buf) ((buf)->text->z + 0)
310 #define BUF_Z(buf) ((buf)->text->bufz + 0)
311
312 /* Point. */
313 #define BI_BUF_PT(buf) ((buf)->pt + 0)
314 #define BUF_PT(buf) ((buf)->bufpt + 0)
315
316 /*----------------------------------------------------------------------*/
317 /*              Converting between positions and addresses              */
318 /*----------------------------------------------------------------------*/
319
320 /* Convert the address of a byte in the buffer into a position.  */
321 INLINE_HEADER Bytind BI_BUF_PTR_BYTE_POS (struct buffer *buf, Bufbyte *ptr);
322 INLINE_HEADER Bytind
323 BI_BUF_PTR_BYTE_POS (struct buffer *buf, Bufbyte *ptr)
324 {
325   return (ptr - buf->text->beg + 1
326           - ((ptr - buf->text->beg + 1) > buf->text->gpt
327              ? buf->text->gap_size : 0));
328 }
329
330 #define BUF_PTR_BYTE_POS(buf, ptr) \
331   bytind_to_bufpos (buf, BI_BUF_PTR_BYTE_POS (buf, ptr))
332
333 /* Address of byte at position POS in buffer. */
334 INLINE_HEADER Bufbyte * BI_BUF_BYTE_ADDRESS (struct buffer *buf, Bytind pos);
335 INLINE_HEADER Bufbyte *
336 BI_BUF_BYTE_ADDRESS (struct buffer *buf, Bytind pos)
337 {
338   return (buf->text->beg +
339           ((pos >= buf->text->gpt ? (pos + buf->text->gap_size) : pos)
340            - 1));
341 }
342
343 #define BUF_BYTE_ADDRESS(buf, pos) \
344   BI_BUF_BYTE_ADDRESS (buf, bufpos_to_bytind (buf, pos))
345
346 /* Address of byte before position POS in buffer. */
347 INLINE_HEADER Bufbyte * BI_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytind pos);
348 INLINE_HEADER Bufbyte *
349 BI_BUF_BYTE_ADDRESS_BEFORE (struct buffer *buf, Bytind pos)
350 {
351   return (buf->text->beg +
352           ((pos > buf->text->gpt ? (pos + buf->text->gap_size) : pos)
353            - 2));
354 }
355
356 #define BUF_BYTE_ADDRESS_BEFORE(buf, pos) \
357   BI_BUF_BYTE_ADDRESS_BEFORE (buf, bufpos_to_bytind (buf, pos))
358
359 /*----------------------------------------------------------------------*/
360 /*          Converting between byte indices and memory indices          */
361 /*----------------------------------------------------------------------*/
362
363 INLINE_HEADER int valid_memind_p (struct buffer *buf, Memind x);
364 INLINE_HEADER int
365 valid_memind_p (struct buffer *buf, Memind x)
366 {
367   return ((x >= 1 && x <= (Memind) buf->text->gpt) ||
368           (x  > (Memind) (buf->text->gpt + buf->text->gap_size) &&
369            x <= (Memind) (buf->text->z   + buf->text->gap_size)));
370 }
371
372 INLINE_HEADER Memind bytind_to_memind (struct buffer *buf, Bytind x);
373 INLINE_HEADER Memind
374 bytind_to_memind (struct buffer *buf, Bytind x)
375 {
376   return (Memind) ((x > buf->text->gpt) ? (x + buf->text->gap_size) : x);
377 }
378
379
380 INLINE_HEADER Bytind memind_to_bytind (struct buffer *buf, Memind x);
381 INLINE_HEADER Bytind
382 memind_to_bytind (struct buffer *buf, Memind x)
383 {
384 #ifdef ERROR_CHECK_BUFPOS
385   assert (valid_memind_p (buf, x));
386 #endif
387   return (Bytind) ((x > (Memind) buf->text->gpt) ?
388                    x - buf->text->gap_size :
389                    x);
390 }
391
392 #define memind_to_bufpos(buf, x) \
393   bytind_to_bufpos (buf, memind_to_bytind (buf, x))
394 #define bufpos_to_memind(buf, x) \
395   bytind_to_memind (buf, bufpos_to_bytind (buf, x))
396
397 /* These macros generalize many standard buffer-position functions to
398    either a buffer or a string. */
399
400 /* Converting between Meminds and Bytinds, for a buffer-or-string.
401    For strings, this is a no-op.  For buffers, this resolves
402    to the standard memind<->bytind converters. */
403
404 #define buffer_or_string_bytind_to_memind(obj, ind) \
405   (BUFFERP (obj) ? bytind_to_memind (XBUFFER (obj), ind) : (Memind) ind)
406
407 #define buffer_or_string_memind_to_bytind(obj, ind) \
408   (BUFFERP (obj) ? memind_to_bytind (XBUFFER (obj), ind) : (Bytind) ind)
409
410 /* Converting between Bufpos's and Bytinds, for a buffer-or-string.
411    For strings, this maps to the bytecount<->charcount converters. */
412
413 #define buffer_or_string_bufpos_to_bytind(obj, pos)             \
414   (BUFFERP (obj) ? bufpos_to_bytind (XBUFFER (obj), pos) :      \
415    (Bytind) charcount_to_bytecount (XSTRING_DATA (obj), pos))
416
417 #define buffer_or_string_bytind_to_bufpos(obj, ind)             \
418   (BUFFERP (obj) ? bytind_to_bufpos (XBUFFER (obj), ind) :      \
419    (Bufpos) bytecount_to_charcount (XSTRING_DATA (obj), ind))
420
421 /* Similar for Bufpos's and Meminds. */
422
423 #define buffer_or_string_bufpos_to_memind(obj, pos)             \
424   (BUFFERP (obj) ? bufpos_to_memind (XBUFFER (obj), pos) :      \
425    (Memind) charcount_to_bytecount (XSTRING_DATA (obj), pos))
426
427 #define buffer_or_string_memind_to_bufpos(obj, ind)             \
428   (BUFFERP (obj) ? memind_to_bufpos (XBUFFER (obj), ind) :      \
429    (Bufpos) bytecount_to_charcount (XSTRING_DATA (obj), ind))
430
431 /************************************************************************/
432 /*                                                                      */
433 /*                    working with buffer-level data                    */
434 /*                                                                      */
435 /************************************************************************/
436
437 /*
438
439    (A) Working with byte indices:
440    ------------------------------
441
442    VALID_BYTIND_P(buf, bi):
443         Given a byte index, does it point to the beginning of a character?
444
445    ASSERT_VALID_BYTIND_UNSAFE(buf, bi):
446         If error-checking is enabled, assert that the given byte index
447         is within range and points to the beginning of a character
448         or to the end of the buffer.  Otherwise, do nothing.
449
450    ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, bi):
451         If error-checking is enabled, assert that the given byte index
452         is within range and satisfies ASSERT_VALID_BYTIND() and also
453         does not refer to the beginning of the buffer. (i.e. movement
454         backwards is OK.) Otherwise, do nothing.
455
456    ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, bi):
457         If error-checking is enabled, assert that the given byte index
458         is within range and satisfies ASSERT_VALID_BYTIND() and also
459         does not refer to the end of the buffer. (i.e. movement
460         forwards is OK.) Otherwise, do nothing.
461
462    VALIDATE_BYTIND_BACKWARD(buf, bi):
463         Make sure that the given byte index is pointing to the beginning
464         of a character.  If not, back up until this is the case.  Note
465         that there are not too many places where it is legitimate to do
466         this sort of thing.  It's an error if you're passed an "invalid"
467         byte index.
468
469    VALIDATE_BYTIND_FORWARD(buf, bi):
470         Make sure that the given byte index is pointing to the beginning
471         of a character.  If not, move forward until this is the case.
472         Note that there are not too many places where it is legitimate
473         to do this sort of thing.  It's an error if you're passed an
474         "invalid" byte index.
475
476    INC_BYTIND(buf, bi):
477         Given a byte index (assumed to point at the beginning of a
478         character), modify that value so it points to the beginning
479         of the next character.
480
481    DEC_BYTIND(buf, bi):
482         Given a byte index (assumed to point at the beginning of a
483         character), modify that value so it points to the beginning
484         of the previous character.  Unlike for DEC_CHARPTR(), we can
485         do all the assert()s because there are sentinels at the
486         beginning of the gap and the end of the buffer.
487
488    BYTIND_INVALID:
489         A constant representing an invalid Bytind.  Valid Bytinds
490         can never have this value.
491
492
493    (B) Converting between Bufpos's and Bytinds:
494    --------------------------------------------
495
496     bufpos_to_bytind(buf, bu):
497         Given a Bufpos, return the equivalent Bytind.
498
499     bytind_to_bufpos(buf, bi):
500         Given a Bytind, return the equivalent Bufpos.
501
502     make_bufpos(buf, bi):
503         Given a Bytind, return the equivalent Bufpos as a Lisp Object.
504  */
505
506
507 /*----------------------------------------------------------------------*/
508 /*                       working with byte indices                      */
509 /*----------------------------------------------------------------------*/
510
511 #ifdef MULE
512 # define VALID_BYTIND_P(buf, x) \
513   BUFBYTE_FIRST_BYTE_P (*BI_BUF_BYTE_ADDRESS (buf, x))
514 #else
515 # define VALID_BYTIND_P(buf, x) 1
516 #endif
517
518 #ifdef ERROR_CHECK_BUFPOS
519
520 # define ASSERT_VALID_BYTIND_UNSAFE(buf, x) do {                \
521   assert (BUFFER_LIVE_P (buf));                                 \
522   assert ((x) >= BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf));      \
523   assert (VALID_BYTIND_P (buf, x));                             \
524 } while (0)
525 # define ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, x) do {       \
526   assert (BUFFER_LIVE_P (buf));                                 \
527   assert ((x) > BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf));       \
528   assert (VALID_BYTIND_P (buf, x));                             \
529 } while (0)
530 # define ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, x) do {        \
531   assert (BUFFER_LIVE_P (buf));                                 \
532   assert ((x) >= BI_BUF_BEG (buf) && x < BI_BUF_Z (buf));       \
533   assert (VALID_BYTIND_P (buf, x));                             \
534 } while (0)
535
536 #else /* not ERROR_CHECK_BUFPOS */
537 # define ASSERT_VALID_BYTIND_UNSAFE(buf, x)
538 # define ASSERT_VALID_BYTIND_BACKWARD_UNSAFE(buf, x)
539 # define ASSERT_VALID_BYTIND_FORWARD_UNSAFE(buf, x)
540
541 #endif /* not ERROR_CHECK_BUFPOS */
542
543 /* Note that, although the Mule version will work fine for non-Mule
544    as well (it should reduce down to nothing), we provide a separate
545    version to avoid compilation warnings and possible non-optimal
546    results with stupid compilers. */
547
548 #ifdef MULE
549 # define VALIDATE_BYTIND_BACKWARD(buf, x) do {          \
550   Bufbyte *VBB_ptr = BI_BUF_BYTE_ADDRESS (buf, x);      \
551   while (!BUFBYTE_FIRST_BYTE_P (*VBB_ptr))              \
552     VBB_ptr--, (x)--;                                   \
553 } while (0)
554 #else
555 # define VALIDATE_BYTIND_BACKWARD(buf, x)
556 #endif
557
558 /* Note that, although the Mule version will work fine for non-Mule
559    as well (it should reduce down to nothing), we provide a separate
560    version to avoid compilation warnings and possible non-optimal
561    results with stupid compilers. */
562
563 #ifdef MULE
564 # define VALIDATE_BYTIND_FORWARD(buf, x) do {           \
565   Bufbyte *VBF_ptr = BI_BUF_BYTE_ADDRESS (buf, x);      \
566   while (!BUFBYTE_FIRST_BYTE_P (*VBF_ptr))              \
567     VBF_ptr++, (x)++;                                   \
568 } while (0)
569 #else
570 # define VALIDATE_BYTIND_FORWARD(buf, x)
571 #endif
572
573 /* Note that in the simplest case (no MULE, no ERROR_CHECK_BUFPOS),
574    this crap reduces down to simply (x)++. */
575
576 #define INC_BYTIND(buf, x) do                           \
577 {                                                       \
578   ASSERT_VALID_BYTIND_FORWARD_UNSAFE (buf, x);          \
579   /* Note that we do the increment first to             \
580      make sure that the pointer in                      \
581      VALIDATE_BYTIND_FORWARD() ends up on               \
582      the correct side of the gap */                     \
583   (x)++;                                                \
584   VALIDATE_BYTIND_FORWARD (buf, x);                     \
585 } while (0)
586
587 /* Note that in the simplest case (no MULE, no ERROR_CHECK_BUFPOS),
588    this crap reduces down to simply (x)--. */
589
590 #define DEC_BYTIND(buf, x) do                           \
591 {                                                       \
592   ASSERT_VALID_BYTIND_BACKWARD_UNSAFE (buf, x);         \
593   /* Note that we do the decrement first to             \
594      make sure that the pointer in                      \
595      VALIDATE_BYTIND_BACKWARD() ends up on              \
596      the correct side of the gap */                     \
597   (x)--;                                                \
598   VALIDATE_BYTIND_BACKWARD (buf, x);                    \
599 } while (0)
600
601 INLINE_HEADER Bytind prev_bytind (struct buffer *buf, Bytind x);
602 INLINE_HEADER Bytind
603 prev_bytind (struct buffer *buf, Bytind x)
604 {
605   DEC_BYTIND (buf, x);
606   return x;
607 }
608
609 INLINE_HEADER Bytind next_bytind (struct buffer *buf, Bytind x);
610 INLINE_HEADER Bytind
611 next_bytind (struct buffer *buf, Bytind x)
612 {
613   INC_BYTIND (buf, x);
614   return x;
615 }
616
617 #define BYTIND_INVALID ((Bytind) -1)
618
619 /*----------------------------------------------------------------------*/
620 /*         Converting between buffer positions and byte indices         */
621 /*----------------------------------------------------------------------*/
622
623 #ifdef MULE
624
625 Bytind bufpos_to_bytind_func (struct buffer *buf, Bufpos x);
626 Bufpos bytind_to_bufpos_func (struct buffer *buf, Bytind x);
627
628 /* The basic algorithm we use is to keep track of a known region of
629    characters in each buffer, all of which are of the same width.  We
630    keep track of the boundaries of the region in both Bufpos and
631    Bytind coordinates and also keep track of the char width, which
632    is 1 - 4 bytes.  If the position we're translating is not in
633    the known region, then we invoke a function to update the known
634    region to surround the position in question.  This assumes
635    locality of reference, which is usually the case.
636
637    Note that the function to update the known region can be simple
638    or complicated depending on how much information we cache.
639    For the moment, we don't cache any information, and just move
640    linearly forward or back from the known region, with a few
641    shortcuts to catch all-ASCII buffers. (Note that this will
642    thrash with bad locality of reference.) A smarter method would
643    be to keep some sort of pseudo-extent layer over the buffer;
644    maybe keep track of the bufpos/bytind correspondence at the
645    beginning of each line, which would allow us to do a binary
646    search over the pseudo-extents to narrow things down to the
647    correct line, at which point you could use a linear movement
648    method.  This would also mesh well with efficiently
649    implementing a line-numbering scheme.
650
651    Note also that we have to multiply or divide by the char width
652    in order to convert the positions.  We do some tricks to avoid
653    ever actually having to do a multiply or divide, because that
654    is typically an expensive operation (esp. divide).  Multiplying
655    or dividing by 1, 2, or 4 can be implemented simply as a
656    shift left or shift right, and we keep track of a shifter value
657    (0, 1, or 2) indicating how much to shift.  Multiplying by 3
658    can be implemented by doubling and then adding the original
659    value.  Dividing by 3, alas, cannot be implemented in any
660    simple shift/subtract method, as far as I know; so we just
661    do a table lookup.  For simplicity, we use a table of size
662    128K, which indexes the "divide-by-3" values for the first
663    64K non-negative numbers. (Note that we can increase the
664    size up to 384K, i.e. indexing the first 192K non-negative
665    numbers, while still using shorts in the array.) This also
666    means that the size of the known region can be at most
667    64K for width-three characters.
668    */
669
670 #ifndef UTF2000
671 extern short three_to_one_table[];
672 #endif
673
674 INLINE_HEADER int real_bufpos_to_bytind (struct buffer *buf, Bufpos x);
675 INLINE_HEADER int
676 real_bufpos_to_bytind (struct buffer *buf, Bufpos x)
677 {
678   if (x >= buf->text->mule_bufmin && x <= buf->text->mule_bufmax)
679     return (buf->text->mule_bytmin +
680 #ifdef UTF2000
681             (x - buf->text->mule_bufmin) * buf->text->mule_size
682 #else
683             ((x - buf->text->mule_bufmin) << buf->text->mule_shifter) +
684             (buf->text->mule_three_p ? (x - buf->text->mule_bufmin) : 0)
685 #endif
686             );
687   else
688     return bufpos_to_bytind_func (buf, x);
689 }
690
691 INLINE_HEADER int real_bytind_to_bufpos (struct buffer *buf, Bytind x);
692 INLINE_HEADER int
693 real_bytind_to_bufpos (struct buffer *buf, Bytind x)
694 {
695   if (x >= buf->text->mule_bytmin && x <= buf->text->mule_bytmax)
696     return (buf->text->mule_bufmin +
697 #ifdef UTF2000
698             (buf->text->mule_size == 0 ? 0 :
699              (x - buf->text->mule_bytmin) / buf->text->mule_size)
700 #else
701             ((buf->text->mule_three_p
702               ? three_to_one_table[x - buf->text->mule_bytmin]
703               : (x - buf->text->mule_bytmin) >> buf->text->mule_shifter))
704 #endif
705             );
706   else
707     return bytind_to_bufpos_func (buf, x);
708 }
709
710 #else /* not MULE */
711
712 # define real_bufpos_to_bytind(buf, x)  ((Bytind) x)
713 # define real_bytind_to_bufpos(buf, x)  ((Bufpos) x)
714
715 #endif /* not MULE */
716
717 #ifdef ERROR_CHECK_BUFPOS
718
719 Bytind bufpos_to_bytind (struct buffer *buf, Bufpos x);
720 Bufpos bytind_to_bufpos (struct buffer *buf, Bytind x);
721
722 #else /* not ERROR_CHECK_BUFPOS */
723
724 #define bufpos_to_bytind real_bufpos_to_bytind
725 #define bytind_to_bufpos real_bytind_to_bufpos
726
727 #endif /* not ERROR_CHECK_BUFPOS */
728
729 #define make_bufpos(buf, ind) make_int (bytind_to_bufpos (buf, ind))
730
731 /*----------------------------------------------------------------------*/
732 /*         Converting between buffer bytes and Emacs characters         */
733 /*----------------------------------------------------------------------*/
734
735 /* The character at position POS in buffer. */
736 #define BI_BUF_FETCH_CHAR(buf, pos) \
737   charptr_emchar (BI_BUF_BYTE_ADDRESS (buf, pos))
738 #define BUF_FETCH_CHAR(buf, pos) \
739   BI_BUF_FETCH_CHAR (buf, bufpos_to_bytind (buf, pos))
740
741 /* The character at position POS in buffer, as a string.  This is
742    equivalent to set_charptr_emchar (str, BUF_FETCH_CHAR (buf, pos))
743    but is faster for Mule. */
744
745 # define BI_BUF_CHARPTR_COPY_CHAR(buf, pos, str) \
746   charptr_copy_char (BI_BUF_BYTE_ADDRESS (buf, pos), str)
747 #define BUF_CHARPTR_COPY_CHAR(buf, pos, str) \
748   BI_BUF_CHARPTR_COPY_CHAR (buf, bufpos_to_bytind (buf, pos), str)
749
750 \f
751 /************************************************************************/
752 /*                                                                      */
753 /*         Converting between internal and external format              */
754 /*                                                                      */
755 /************************************************************************/
756 /*
757   All client code should use only the two macros
758
759   TO_EXTERNAL_FORMAT (source_type, source, sink_type, sink, coding_system)
760   TO_INTERNAL_FORMAT (source_type, source, sink_type, sink, coding_system)
761
762   Typical use is
763
764   TO_EXTERNAL_FORMAT (DATA, (ptr, len),
765                       LISP_BUFFER, buffer,
766                       Qfile_name);
767
768   The source or sink can be specified in one of these ways:
769
770   DATA,   (ptr, len),    // input data is a fixed buffer of size len
771   ALLOCA, (ptr, len),    // output data is in a alloca()ed buffer of size len
772   MALLOC, (ptr, len),    // output data is in a malloc()ed buffer of size len
773   C_STRING_ALLOCA, ptr,  // equivalent to ALLOCA (ptr, len_ignored) on output.
774   C_STRING_MALLOC, ptr,  // equivalent to MALLOC (ptr, len_ignored) on output.
775   C_STRING,     ptr,     // equivalent to DATA, (ptr, strlen (ptr) + 1) on input
776   LISP_STRING,  string,  // input or output is a Lisp_Object of type string
777   LISP_BUFFER,  buffer,  // output is written to (point) in lisp buffer
778   LISP_LSTREAM, lstream, // input or output is a Lisp_Object of type lstream
779   LISP_OPAQUE,  object,  // input or output is a Lisp_Object of type opaque
780
781   When specifying the sink, use lvalues, since the macro will assign to them,
782   except when the sink is an lstream or a lisp buffer.
783
784   The macros accept the kinds of sources and sinks appropriate for
785   internal and external data representation.  See the type_checking_assert
786   macros below for the actual allowed types.
787
788   Since some sources and sinks use one argument (a Lisp_Object) to
789   specify them, while others take a (pointer, length) pair, we use
790   some C preprocessor trickery to allow pair arguments to be specified
791   by parenthesizing them, as in the examples above.
792
793   Anything prefixed by dfc_ (`data format conversion') is private.
794   They are only used to implement these macros.
795
796   Using C_STRING* is appropriate for using with external APIs that take
797   null-terminated strings.  For internal data, we should try to be
798   '\0'-clean - i.e. allow arbitrary data to contain embedded '\0'.
799
800   Sometime in the future we might allow output to C_STRING_ALLOCA or
801   C_STRING_MALLOC _only_ with TO_EXTERNAL_FORMAT(), not
802   TO_INTERNAL_FORMAT().  */
803
804 #define TO_EXTERNAL_FORMAT(source_type, source, sink_type, sink, coding_system) \
805 do {                                                                            \
806   dfc_conversion_type dfc_simplified_source_type;                               \
807   dfc_conversion_type dfc_simplified_sink_type;                                 \
808   dfc_conversion_data dfc_source;                                               \
809   dfc_conversion_data dfc_sink;                                                 \
810                                                                                 \
811   type_checking_assert                                                          \
812     ((DFC_TYPE_##source_type == DFC_TYPE_DATA ||                                \
813       DFC_TYPE_##source_type == DFC_TYPE_C_STRING ||                            \
814       DFC_TYPE_##source_type == DFC_TYPE_LISP_STRING ||                         \
815       DFC_TYPE_##source_type == DFC_TYPE_LISP_OPAQUE ||                         \
816       DFC_TYPE_##source_type == DFC_TYPE_LISP_LSTREAM)                          \
817     &&                                                                          \
818      (DFC_TYPE_##sink_type == DFC_TYPE_ALLOCA ||                                \
819       DFC_TYPE_##sink_type == DFC_TYPE_MALLOC ||                                \
820       DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_ALLOCA ||                       \
821       DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_MALLOC ||                       \
822       DFC_TYPE_##sink_type == DFC_TYPE_LISP_LSTREAM ||                          \
823       DFC_TYPE_##sink_type == DFC_TYPE_LISP_OPAQUE));                           \
824                                                                                 \
825   DFC_SOURCE_##source_type##_TO_ARGS (source);                                  \
826   DFC_SINK_##sink_type##_TO_ARGS     (sink);                                    \
827                                                                                 \
828   DFC_CONVERT_TO_EXTERNAL_FORMAT (dfc_simplified_source_type, &dfc_source,      \
829                                   coding_system,                                \
830                                   dfc_simplified_sink_type,   &dfc_sink);       \
831                                                                                 \
832   DFC_##sink_type##_USE_CONVERTED_DATA (sink);                                  \
833 } while (0)
834
835 #define TO_INTERNAL_FORMAT(source_type, source, sink_type, sink, coding_system) \
836 do {                                                                            \
837   dfc_conversion_type dfc_simplified_source_type;                               \
838   dfc_conversion_type dfc_simplified_sink_type;                                 \
839   dfc_conversion_data dfc_source;                                               \
840   dfc_conversion_data dfc_sink;                                                 \
841                                                                                 \
842   type_checking_assert                                                          \
843     ((DFC_TYPE_##source_type == DFC_TYPE_DATA ||                                \
844       DFC_TYPE_##source_type == DFC_TYPE_C_STRING ||                            \
845       DFC_TYPE_##source_type == DFC_TYPE_LISP_OPAQUE ||                         \
846       DFC_TYPE_##source_type == DFC_TYPE_LISP_LSTREAM)                          \
847      &&                                                                         \
848      (DFC_TYPE_##sink_type == DFC_TYPE_ALLOCA ||                                \
849       DFC_TYPE_##sink_type == DFC_TYPE_MALLOC ||                                \
850       DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_ALLOCA ||                       \
851       DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_MALLOC ||                       \
852       DFC_TYPE_##sink_type == DFC_TYPE_LISP_STRING ||                           \
853       DFC_TYPE_##sink_type == DFC_TYPE_LISP_LSTREAM ||                          \
854       DFC_TYPE_##sink_type == DFC_TYPE_LISP_BUFFER));                           \
855                                                                                 \
856   DFC_SOURCE_##source_type##_TO_ARGS (source);                                  \
857   DFC_SINK_##sink_type##_TO_ARGS     (sink);                                    \
858                                                                                 \
859   DFC_CONVERT_TO_INTERNAL_FORMAT (dfc_simplified_source_type, &dfc_source,      \
860                                   coding_system,                                \
861                                   dfc_simplified_sink_type,   &dfc_sink);       \
862                                                                                 \
863   DFC_##sink_type##_USE_CONVERTED_DATA (sink);                                  \
864 } while (0)
865
866 #ifdef FILE_CODING
867 #define DFC_CONVERT_TO_EXTERNAL_FORMAT dfc_convert_to_external_format
868 #define DFC_CONVERT_TO_INTERNAL_FORMAT dfc_convert_to_internal_format
869 #else
870 /* ignore coding_system argument */
871 #define DFC_CONVERT_TO_EXTERNAL_FORMAT(a, b, coding_system, c, d) \
872  dfc_convert_to_external_format (a, b, c, d)
873 #define DFC_CONVERT_TO_INTERNAL_FORMAT(a, b, coding_system, c, d) \
874  dfc_convert_to_internal_format (a, b, c, d)
875 #endif
876
877 typedef union
878 {
879   struct { const void *ptr; size_t len; } data;
880   Lisp_Object lisp_object;
881 } dfc_conversion_data;
882
883 enum dfc_conversion_type
884 {
885   DFC_TYPE_DATA,
886   DFC_TYPE_ALLOCA,
887   DFC_TYPE_MALLOC,
888   DFC_TYPE_C_STRING,
889   DFC_TYPE_C_STRING_ALLOCA,
890   DFC_TYPE_C_STRING_MALLOC,
891   DFC_TYPE_LISP_STRING,
892   DFC_TYPE_LISP_LSTREAM,
893   DFC_TYPE_LISP_OPAQUE,
894   DFC_TYPE_LISP_BUFFER
895 };
896 typedef enum dfc_conversion_type dfc_conversion_type;
897
898 /* WARNING: These use a static buffer.  This can lead to disaster if
899    these functions are not used *very* carefully.  Another reason to only use
900    TO_EXTERNAL_FORMAT() and TO_INTERNAL_FORMAT(). */
901 void
902 dfc_convert_to_external_format (dfc_conversion_type source_type,
903                                 dfc_conversion_data *source,
904 #ifdef FILE_CODING
905                                 Lisp_Object coding_system,
906 #endif
907                                 dfc_conversion_type sink_type,
908                                 dfc_conversion_data *sink);
909 void
910 dfc_convert_to_internal_format (dfc_conversion_type source_type,
911                                 dfc_conversion_data *source,
912 #ifdef FILE_CODING
913                                 Lisp_Object coding_system,
914 #endif
915                                 dfc_conversion_type sink_type,
916                                 dfc_conversion_data *sink);
917 /* CPP Trickery */
918 #define DFC_CPP_CAR(x,y) (x)
919 #define DFC_CPP_CDR(x,y) (y)
920
921 /* Convert `source' to args for dfc_convert_to_*_format() */
922 #define DFC_SOURCE_DATA_TO_ARGS(val) do {               \
923   dfc_source.data.ptr = DFC_CPP_CAR val;                \
924   dfc_source.data.len = DFC_CPP_CDR val;                \
925   dfc_simplified_source_type = DFC_TYPE_DATA;           \
926 } while (0)
927 #define DFC_SOURCE_C_STRING_TO_ARGS(val) do {           \
928   dfc_source.data.len =                                 \
929     strlen ((char *) (dfc_source.data.ptr = (val)));    \
930   dfc_simplified_source_type = DFC_TYPE_DATA;           \
931 } while (0)
932 #define DFC_SOURCE_LISP_STRING_TO_ARGS(val) do {        \
933   Lisp_Object dfc_slsta = (val);                        \
934   type_checking_assert (STRINGP (dfc_slsta));           \
935   dfc_source.lisp_object = dfc_slsta;                   \
936   dfc_simplified_source_type = DFC_TYPE_LISP_STRING;    \
937 } while (0)
938 #define DFC_SOURCE_LISP_LSTREAM_TO_ARGS(val) do {       \
939   Lisp_Object dfc_sllta = (val);                        \
940   type_checking_assert (LSTREAMP (dfc_sllta));          \
941   dfc_source.lisp_object = dfc_sllta;                   \
942   dfc_simplified_source_type = DFC_TYPE_LISP_LSTREAM;   \
943 } while (0)
944 #define DFC_SOURCE_LISP_OPAQUE_TO_ARGS(val) do {        \
945   Lisp_Opaque *dfc_slota = XOPAQUE (val);               \
946   dfc_source.data.ptr = OPAQUE_DATA (dfc_slota);        \
947   dfc_source.data.len = OPAQUE_SIZE (dfc_slota);        \
948   dfc_simplified_source_type = DFC_TYPE_DATA;           \
949 } while (0)
950
951 /* Convert `sink' to args for dfc_convert_to_*_format() */
952 #define DFC_SINK_ALLOCA_TO_ARGS(val)            \
953   dfc_simplified_sink_type = DFC_TYPE_DATA
954 #define DFC_SINK_C_STRING_ALLOCA_TO_ARGS(val)   \
955   dfc_simplified_sink_type = DFC_TYPE_DATA
956 #define DFC_SINK_MALLOC_TO_ARGS(val)            \
957   dfc_simplified_sink_type = DFC_TYPE_DATA
958 #define DFC_SINK_C_STRING_MALLOC_TO_ARGS(val)   \
959   dfc_simplified_sink_type = DFC_TYPE_DATA
960 #define DFC_SINK_LISP_STRING_TO_ARGS(val)       \
961   dfc_simplified_sink_type = DFC_TYPE_DATA
962 #define DFC_SINK_LISP_OPAQUE_TO_ARGS(val)       \
963   dfc_simplified_sink_type = DFC_TYPE_DATA
964 #define DFC_SINK_LISP_LSTREAM_TO_ARGS(val) do {         \
965   Lisp_Object dfc_sllta = (val);                        \
966   type_checking_assert (LSTREAMP (dfc_sllta));          \
967   dfc_sink.lisp_object = dfc_sllta;                     \
968   dfc_simplified_sink_type = DFC_TYPE_LISP_LSTREAM;     \
969 } while (0)
970 #define DFC_SINK_LISP_BUFFER_TO_ARGS(val) do {          \
971   struct buffer *dfc_slbta = XBUFFER (val);             \
972   dfc_sink.lisp_object =                                \
973     make_lisp_buffer_output_stream                      \
974     (dfc_slbta, BUF_PT (dfc_slbta), 0);                 \
975   dfc_simplified_sink_type = DFC_TYPE_LISP_LSTREAM;     \
976 } while (0)
977
978 /* Assign to the `sink' lvalue(s) using the converted data. */
979 typedef union { char c; void *p; } *dfc_aliasing_voidpp;
980 #define DFC_ALLOCA_USE_CONVERTED_DATA(sink) do {                        \
981   void * dfc_sink_ret = alloca (dfc_sink.data.len + 1);                 \
982   memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1);      \
983   ((dfc_aliasing_voidpp) &(DFC_CPP_CAR sink))->p = dfc_sink_ret;        \
984   (DFC_CPP_CDR sink) = dfc_sink.data.len;                               \
985 } while (0)
986 #define DFC_MALLOC_USE_CONVERTED_DATA(sink) do {                        \
987   void * dfc_sink_ret = xmalloc (dfc_sink.data.len + 1);                \
988   memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1);      \
989   ((dfc_aliasing_voidpp) &(DFC_CPP_CAR sink))->p = dfc_sink_ret;        \
990   (DFC_CPP_CDR sink) = dfc_sink.data.len;                               \
991 } while (0)
992 #define DFC_C_STRING_ALLOCA_USE_CONVERTED_DATA(sink) do {               \
993   void * dfc_sink_ret = alloca (dfc_sink.data.len + 1);                 \
994   memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1);      \
995   (sink) = (char *) dfc_sink_ret;                                       \
996 } while (0)
997 #define DFC_C_STRING_MALLOC_USE_CONVERTED_DATA(sink) do {               \
998   void * dfc_sink_ret = xmalloc (dfc_sink.data.len + 1);                \
999   memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 1);      \
1000   (sink) = (char *) dfc_sink_ret;                                       \
1001 } while (0)
1002 #define DFC_LISP_STRING_USE_CONVERTED_DATA(sink) \
1003   sink = make_string ((Bufbyte *) dfc_sink.data.ptr, dfc_sink.data.len)
1004 #define DFC_LISP_OPAQUE_USE_CONVERTED_DATA(sink) \
1005   sink = make_opaque (dfc_sink.data.ptr, dfc_sink.data.len)
1006 #define DFC_LISP_LSTREAM_USE_CONVERTED_DATA(sink) /* data already used */
1007 #define DFC_LISP_BUFFER_USE_CONVERTED_DATA(sink) \
1008   Lstream_delete (XLSTREAM (dfc_sink.lisp_object))
1009
1010 /* Someday we might want to distinguish between Qnative and Qfile_name
1011    by using coding-system aliases, but for now it suffices to have
1012    these be identical.  Qnative can be used as the coding_system
1013    argument to TO_EXTERNAL_FORMAT() and TO_INTERNAL_FORMAT(). */
1014 #define Qnative Qfile_name
1015
1016 #if defined (WIN32_NATIVE) || defined (CYGWIN)
1017 /* #### kludge!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1018    Remove this as soon as my Mule code is integrated. */
1019 #define Qmswindows_tstr Qnative
1020 #endif
1021
1022 /* More stand-ins */
1023 #define Qcommand_argument_encoding Qnative
1024 #define Qenvironment_variable_encoding Qnative
1025
1026 /* Convenience macros for extremely common invocations */
1027 #define C_STRING_TO_EXTERNAL(in, out, coding_system) \
1028   TO_EXTERNAL_FORMAT (C_STRING, in, C_STRING_ALLOCA, out, coding_system)
1029 #define C_STRING_TO_EXTERNAL_MALLOC(in, out, coding_system) \
1030   TO_EXTERNAL_FORMAT (C_STRING, in, C_STRING_MALLOC, out, coding_system)
1031 #define EXTERNAL_TO_C_STRING(in, out, coding_system) \
1032   TO_INTERNAL_FORMAT (C_STRING, in, C_STRING_ALLOCA, out, coding_system)
1033 #define EXTERNAL_TO_C_STRING_MALLOC(in, out, coding_system) \
1034   TO_INTERNAL_FORMAT (C_STRING, in, C_STRING_MALLOC, out, coding_system)
1035 #define LISP_STRING_TO_EXTERNAL(in, out, coding_system) \
1036   TO_EXTERNAL_FORMAT (LISP_STRING, in, C_STRING_ALLOCA, out, coding_system)
1037 #define LISP_STRING_TO_EXTERNAL_MALLOC(in, out, coding_system) \
1038   TO_EXTERNAL_FORMAT (LISP_STRING, in, C_STRING_MALLOC, out, coding_system)
1039
1040 \f
1041 /************************************************************************/
1042 /*                                                                      */
1043 /*                  higher-level buffer-position functions              */
1044 /*                                                                      */
1045 /************************************************************************/
1046
1047 /*----------------------------------------------------------------------*/
1048 /*           Settor macros for important positions in a buffer          */
1049 /*----------------------------------------------------------------------*/
1050
1051 /* Set beginning of accessible range of buffer.  */
1052 #define SET_BOTH_BUF_BEGV(buf, val, bival)      \
1053 do                                              \
1054 {                                               \
1055   (buf)->begv = (bival);                        \
1056   (buf)->bufbegv = (val);                       \
1057 } while (0)
1058
1059 /* Set end of accessible range of buffer.  */
1060 #define SET_BOTH_BUF_ZV(buf, val, bival)        \
1061 do                                              \
1062 {                                               \
1063   (buf)->zv = (bival);                          \
1064   (buf)->bufzv = (val);                         \
1065 } while (0)
1066
1067 /* Set point. */
1068 /* Since BEGV and ZV are almost never set, it's reasonable to enforce
1069    the restriction that the Bufpos and Bytind values must both be
1070    specified.  However, point is set in lots and lots of places.  So
1071    we provide the ability to specify both (for efficiency) or just
1072    one. */
1073 #define BOTH_BUF_SET_PT(buf, val, bival) set_buffer_point (buf, val, bival)
1074 #define BI_BUF_SET_PT(buf, bival) \
1075   BOTH_BUF_SET_PT (buf, bytind_to_bufpos (buf, bival), bival)
1076 #define BUF_SET_PT(buf, value) \
1077   BOTH_BUF_SET_PT (buf, value, bufpos_to_bytind (buf, value))
1078
1079
1080 #if 0 /* FSFmacs */
1081 /* These macros exist in FSFmacs because SET_PT() in FSFmacs incorrectly
1082    does too much stuff, such as moving out of invisible extents. */
1083 #define TEMP_SET_PT(position) (temp_set_point ((position), current_buffer))
1084 #define SET_BUF_PT(buf, value) ((buf)->pt = (value))
1085 #endif /* FSFmacs */
1086
1087 /*----------------------------------------------------------------------*/
1088 /*                      Miscellaneous buffer values                     */
1089 /*----------------------------------------------------------------------*/
1090
1091 /* Number of characters in buffer */
1092 #define BUF_SIZE(buf) (BUF_Z (buf) - BUF_BEG (buf))
1093
1094 /* Is this buffer narrowed? */
1095 #define BUF_NARROWED(buf) \
1096    ((BI_BUF_BEGV (buf) != BI_BUF_BEG (buf)) || \
1097     (BI_BUF_ZV   (buf) != BI_BUF_Z   (buf)))
1098
1099 /* Modification count.  */
1100 #define BUF_MODIFF(buf) ((buf)->text->modiff)
1101
1102 /* Saved modification count.  */
1103 #define BUF_SAVE_MODIFF(buf) ((buf)->text->save_modiff)
1104
1105 /* Face changed.  */
1106 #define BUF_FACECHANGE(buf) ((buf)->face_change)
1107
1108 #define POINT_MARKER_P(marker) \
1109    (XMARKER (marker)->buffer != 0 && \
1110     EQ (marker, XMARKER (marker)->buffer->point_marker))
1111
1112 #define BUF_MARKERS(buf) ((buf)->markers)
1113
1114 /* WARNING:
1115
1116    The new definitions of CEILING_OF() and FLOOR_OF() differ semantically
1117    from the old ones (in FSF Emacs and XEmacs 19.11 and before).
1118    Conversion is as follows:
1119
1120    OLD_BI_CEILING_OF(n) = NEW_BI_CEILING_OF(n) - 1
1121    OLD_BI_FLOOR_OF(n) = NEW_BI_FLOOR_OF(n + 1)
1122
1123    The definitions were changed because the new definitions are more
1124    consistent with the way everything else works in Emacs.
1125  */
1126
1127 /* Properties of CEILING_OF and FLOOR_OF (also apply to BI_ variants):
1128
1129    1) FLOOR_OF (CEILING_OF (n)) = n
1130       CEILING_OF (FLOOR_OF (n)) = n
1131
1132    2) CEILING_OF (n) = n if and only if n = ZV
1133       FLOOR_OF (n) = n if and only if n = BEGV
1134
1135    3) CEILING_OF (CEILING_OF (n)) = ZV
1136       FLOOR_OF (FLOOR_OF (n)) = BEGV
1137
1138    4) The bytes in the regions
1139
1140       [BYTE_ADDRESS (n), BYTE_ADDRESS_BEFORE (CEILING_OF (n))]
1141
1142       and
1143
1144       [BYTE_ADDRESS (FLOOR_OF (n)), BYTE_ADDRESS_BEFORE (n)]
1145
1146       are contiguous.
1147    */
1148
1149
1150 /*  Return the maximum index in the buffer it is safe to scan forwards
1151     past N to.  This is used to prevent buffer scans from running into
1152     the gap (e.g. search.c).  All characters between N and CEILING_OF(N)
1153     are located contiguous in memory.  Note that the character *at*
1154     CEILING_OF(N) is not contiguous in memory. */
1155 #define BI_BUF_CEILING_OF(b, n)                                         \
1156   ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_ZV (b) ?             \
1157    (b)->text->gpt : BI_BUF_ZV (b))
1158 #define BUF_CEILING_OF(b, n)                                            \
1159   bytind_to_bufpos (b, BI_BUF_CEILING_OF (b, bufpos_to_bytind (b, n)))
1160
1161 /*  Return the minimum index in the buffer it is safe to scan backwards
1162     past N to.  All characters between FLOOR_OF(N) and N are located
1163     contiguous in memory.  Note that the character *at* N may not be
1164     contiguous in memory. */
1165 #define BI_BUF_FLOOR_OF(b, n)                                           \
1166         (BI_BUF_BEGV (b) < (b)->text->gpt && (b)->text->gpt < (n) ?     \
1167          (b)->text->gpt : BI_BUF_BEGV (b))
1168 #define BUF_FLOOR_OF(b, n)                                              \
1169   bytind_to_bufpos (b, BI_BUF_FLOOR_OF (b, bufpos_to_bytind (b, n)))
1170
1171 #define BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n)                       \
1172   ((n) < (b)->text->gpt && (b)->text->gpt < BI_BUF_Z (b) ?              \
1173    (b)->text->gpt : BI_BUF_Z (b))
1174 #define BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n)                          \
1175   bytind_to_bufpos                                                      \
1176    (b, BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
1177
1178 #define BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n)                         \
1179         (BI_BUF_BEG (b) < (b)->text->gpt && (b)->text->gpt < (n) ?      \
1180          (b)->text->gpt : BI_BUF_BEG (b))
1181 #define BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n)                            \
1182   bytind_to_bufpos                                                      \
1183    (b, BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
1184
1185
1186 extern struct buffer *current_buffer;
1187
1188 /* This is the initial (startup) directory, as used for the *scratch* buffer.
1189    We're making this a global to make others aware of the startup directory.
1190    `initial_directory' is stored in external format.
1191  */
1192 extern char initial_directory[];
1193 extern void init_initial_directory (void);   /* initialize initial_directory */
1194
1195 EXFUN (Fbuffer_disable_undo, 1);
1196 EXFUN (Fbuffer_modified_p, 1);
1197 EXFUN (Fbuffer_name, 1);
1198 EXFUN (Fcurrent_buffer, 0);
1199 EXFUN (Ferase_buffer, 1);
1200 EXFUN (Fget_buffer, 1);
1201 EXFUN (Fget_buffer_create, 1);
1202 EXFUN (Fget_file_buffer, 1);
1203 EXFUN (Fkill_buffer, 1);
1204 EXFUN (Fother_buffer, 3);
1205 EXFUN (Frecord_buffer, 1);
1206 EXFUN (Fset_buffer, 1);
1207 EXFUN (Fset_buffer_modified_p, 2);
1208
1209 extern Lisp_Object QSscratch, Qafter_change_function, Qafter_change_functions;
1210 extern Lisp_Object Qbefore_change_function, Qbefore_change_functions;
1211 extern Lisp_Object Qbuffer_or_string_p, Qdefault_directory, Qfirst_change_hook;
1212 extern Lisp_Object Qpermanent_local, Vafter_change_function;
1213 extern Lisp_Object Vafter_change_functions, Vbefore_change_function;
1214 extern Lisp_Object Vbefore_change_functions, Vbuffer_alist, Vbuffer_defaults;
1215 extern Lisp_Object Vinhibit_read_only, Vtransient_mark_mode;
1216
1217 /* This structure marks which slots in a buffer have corresponding
1218    default values in Vbuffer_defaults.
1219    Each such slot has a nonzero value in this structure.
1220    The value has only one nonzero bit.
1221
1222    When a buffer has its own local value for a slot,
1223    the bit for that slot (found in the same slot in this structure)
1224    is turned on in the buffer's local_var_flags slot.
1225
1226    If a slot in this structure is zero, then even though there may
1227    be a DEFVAR_BUFFER_LOCAL for the slot, there is no default value for it;
1228    and the corresponding slot in Vbuffer_defaults is not used.  */
1229
1230 extern struct buffer buffer_local_flags;
1231
1232
1233 /* Allocation of buffer data. */
1234
1235 #ifdef REL_ALLOC
1236
1237 char *r_alloc (unsigned char **, size_t);
1238 char *r_re_alloc (unsigned char **, size_t);
1239 void r_alloc_free (unsigned char **);
1240
1241 #define BUFFER_ALLOC(data, size) \
1242   ((Bufbyte *) r_alloc ((unsigned char **) &data, (size) * sizeof(Bufbyte)))
1243 #define BUFFER_REALLOC(data, size) \
1244   ((Bufbyte *) r_re_alloc ((unsigned char **) &data, (size) * sizeof(Bufbyte)))
1245 #define BUFFER_FREE(data) r_alloc_free ((unsigned char **) &(data))
1246 #define R_ALLOC_DECLARE(var,data) r_alloc_declare (&(var), data)
1247
1248 #else /* !REL_ALLOC */
1249
1250 #define BUFFER_ALLOC(data,size)\
1251         (data = xnew_array (Bufbyte, size))
1252 #define BUFFER_REALLOC(data,size)\
1253         ((Bufbyte *) xrealloc (data, (size) * sizeof(Bufbyte)))
1254 /* Avoid excess parentheses, or syntax errors may rear their heads. */
1255 #define BUFFER_FREE(data) xfree (data)
1256 #define R_ALLOC_DECLARE(var,data)
1257
1258 #endif /* !REL_ALLOC */
1259
1260 extern Lisp_Object Vbuffer_alist;
1261 void set_buffer_internal (struct buffer *b);
1262 struct buffer *decode_buffer (Lisp_Object buffer, int allow_string);
1263
1264 /* from editfns.c */
1265 void widen_buffer (struct buffer *b, int no_clip);
1266 int beginning_of_line_p (struct buffer *b, Bufpos pt);
1267
1268 /* from insdel.c */
1269 void set_buffer_point (struct buffer *buf, Bufpos pos, Bytind bipos);
1270 void find_charsets_in_bufbyte_string (Charset_ID *charsets,
1271                                       const Bufbyte *str,
1272                                       Bytecount len);
1273 void find_charsets_in_charc_string (Charset_ID *charsets,
1274                                     const Charc *str,
1275                                     Charcount len);
1276 int bufbyte_string_displayed_columns (const Bufbyte *str, Bytecount len);
1277 int charc_string_displayed_columns (const Charc *str, Charcount len);
1278 void convert_bufbyte_string_into_charc_dynarr (const Bufbyte *str,
1279                                                Bytecount len,
1280                                                Charc_dynarr *dyn);
1281 Charcount convert_bufbyte_string_into_emchar_string (const Bufbyte *str,
1282                                                      Bytecount len,
1283                                                      Emchar *arr);
1284 void convert_charc_string_into_bufbyte_dynarr (Charc *arr, int nels,
1285                                                Bufbyte_dynarr *dyn);
1286 Bufbyte *convert_charc_string_into_malloced_string (Charc *arr, int nels,
1287                                                     Bytecount *len_out);
1288 /* from marker.c */
1289 void init_buffer_markers (struct buffer *b);
1290 void uninit_buffer_markers (struct buffer *b);
1291
1292 /* flags for get_buffer_pos_char(), get_buffer_range_char(), etc. */
1293 /* At most one of GB_COERCE_RANGE and GB_NO_ERROR_IF_BAD should be
1294    specified.  At most one of GB_NEGATIVE_FROM_END and GB_NO_ERROR_IF_BAD
1295    should be specified. */
1296
1297 #define GB_ALLOW_PAST_ACCESSIBLE        (1 << 0)
1298 #define GB_ALLOW_NIL                    (1 << 1)
1299 #define GB_CHECK_ORDER                  (1 << 2)
1300 #define GB_COERCE_RANGE                 (1 << 3)
1301 #define GB_NO_ERROR_IF_BAD              (1 << 4)
1302 #define GB_NEGATIVE_FROM_END            (1 << 5)
1303 #define GB_HISTORICAL_STRING_BEHAVIOR   (GB_NEGATIVE_FROM_END | GB_ALLOW_NIL)
1304
1305 Bufpos get_buffer_pos_char (struct buffer *b, Lisp_Object pos,
1306                             unsigned int flags);
1307 Bytind get_buffer_pos_byte (struct buffer *b, Lisp_Object pos,
1308                             unsigned int flags);
1309 void get_buffer_range_char (struct buffer *b, Lisp_Object from, Lisp_Object to,
1310                             Bufpos *from_out, Bufpos *to_out,
1311                             unsigned int flags);
1312 void get_buffer_range_byte (struct buffer *b, Lisp_Object from, Lisp_Object to,
1313                             Bytind *from_out, Bytind *to_out,
1314                             unsigned int flags);
1315 Charcount get_string_pos_char (Lisp_Object string, Lisp_Object pos,
1316                                unsigned int flags);
1317 Bytecount get_string_pos_byte (Lisp_Object string, Lisp_Object pos,
1318                                unsigned int flags);
1319 void get_string_range_char (Lisp_Object string, Lisp_Object from,
1320                             Lisp_Object to, Charcount *from_out,
1321                             Charcount *to_out, unsigned int flags);
1322 void get_string_range_byte (Lisp_Object string, Lisp_Object from,
1323                             Lisp_Object to, Bytecount *from_out,
1324                             Bytecount *to_out, unsigned int flags);
1325 Bufpos get_buffer_or_string_pos_char (Lisp_Object object, Lisp_Object pos,
1326                                       unsigned int flags);
1327 Bytind get_buffer_or_string_pos_byte (Lisp_Object object, Lisp_Object pos,
1328                                       unsigned int flags);
1329 void get_buffer_or_string_range_char (Lisp_Object object, Lisp_Object from,
1330                                       Lisp_Object to, Bufpos *from_out,
1331                                       Bufpos *to_out, unsigned int flags);
1332 void get_buffer_or_string_range_byte (Lisp_Object object, Lisp_Object from,
1333                                       Lisp_Object to, Bytind *from_out,
1334                                       Bytind *to_out, unsigned int flags);
1335 Bufpos buffer_or_string_accessible_begin_char (Lisp_Object object);
1336 Bufpos buffer_or_string_accessible_end_char (Lisp_Object object);
1337 Bytind buffer_or_string_accessible_begin_byte (Lisp_Object object);
1338 Bytind buffer_or_string_accessible_end_byte (Lisp_Object object);
1339 Bufpos buffer_or_string_absolute_begin_char (Lisp_Object object);
1340 Bufpos buffer_or_string_absolute_end_char (Lisp_Object object);
1341 Bytind buffer_or_string_absolute_begin_byte (Lisp_Object object);
1342 Bytind buffer_or_string_absolute_end_byte (Lisp_Object object);
1343 void record_buffer (Lisp_Object buf);
1344 Lisp_Object get_buffer (Lisp_Object name,
1345                         int error_if_deleted_or_does_not_exist);
1346 int map_over_sharing_buffers (struct buffer *buf,
1347                               int (*mapfun) (struct buffer *buf,
1348                                              void *closure),
1349                               void *closure);
1350
1351 \f
1352 /************************************************************************/
1353 /*                         Case conversion                              */
1354 /************************************************************************/
1355
1356 /* A "trt" table is a mapping from characters to other characters,
1357    typically used to convert between uppercase and lowercase.  For
1358    compatibility reasons, trt tables are currently in the form of
1359    a Lisp string of 256 characters, specifying the conversion for each
1360    of the first 256 Emacs characters (i.e. the 256 Latin-1 characters).
1361    This should be generalized at some point to support conversions for
1362    all of the allowable Mule characters.
1363    */
1364
1365 /* The _1 macros are named as such because they assume that you have
1366    already guaranteed that the character values are all in the range
1367    0 - 255.  Bad lossage will happen otherwise. */
1368
1369 # define MAKE_TRT_TABLE() Fmake_string (make_int (256), make_char (0))
1370 # define TRT_TABLE_AS_STRING(table) XSTRING_DATA (table)
1371 # define TRT_TABLE_CHAR_1(table, ch) \
1372   string_char (XSTRING (table), (Charcount) ch)
1373 # define SET_TRT_TABLE_CHAR_1(table, ch1, ch2) \
1374   set_string_char (XSTRING (table), (Charcount) ch1, ch2)
1375
1376 #ifdef MULE
1377 # define MAKE_MIRROR_TRT_TABLE() make_opaque (OPAQUE_CLEAR, 256)
1378 # define MIRROR_TRT_TABLE_AS_STRING(table) ((Bufbyte *) XOPAQUE_DATA (table))
1379 # define MIRROR_TRT_TABLE_CHAR_1(table, ch) \
1380   ((Emchar) (MIRROR_TRT_TABLE_AS_STRING (table)[ch]))
1381 # define SET_MIRROR_TRT_TABLE_CHAR_1(table, ch1, ch2) \
1382   (MIRROR_TRT_TABLE_AS_STRING (table)[ch1] = (Bufbyte) (ch2))
1383 #endif
1384
1385 # define IN_TRT_TABLE_DOMAIN(c) (((EMACS_UINT) (c)) <= 255)
1386
1387 #ifdef MULE
1388 #define MIRROR_DOWNCASE_TABLE_AS_STRING(buf) \
1389   MIRROR_TRT_TABLE_AS_STRING (buf->mirror_downcase_table)
1390 #define MIRROR_UPCASE_TABLE_AS_STRING(buf) \
1391   MIRROR_TRT_TABLE_AS_STRING (buf->mirror_upcase_table)
1392 #define MIRROR_CANON_TABLE_AS_STRING(buf) \
1393   MIRROR_TRT_TABLE_AS_STRING (buf->mirror_case_canon_table)
1394 #define MIRROR_EQV_TABLE_AS_STRING(buf) \
1395   MIRROR_TRT_TABLE_AS_STRING (buf->mirror_case_eqv_table)
1396 #else
1397 #define MIRROR_DOWNCASE_TABLE_AS_STRING(buf) \
1398   TRT_TABLE_AS_STRING (buf->downcase_table)
1399 #define MIRROR_UPCASE_TABLE_AS_STRING(buf) \
1400   TRT_TABLE_AS_STRING (buf->upcase_table)
1401 #define MIRROR_CANON_TABLE_AS_STRING(buf) \
1402   TRT_TABLE_AS_STRING (buf->case_canon_table)
1403 #define MIRROR_EQV_TABLE_AS_STRING(buf) \
1404   TRT_TABLE_AS_STRING (buf->case_eqv_table)
1405 #endif
1406
1407 INLINE_HEADER Emchar TRT_TABLE_OF (Lisp_Object trt, Emchar c);
1408 INLINE_HEADER Emchar
1409 TRT_TABLE_OF (Lisp_Object trt, Emchar c)
1410 {
1411   return IN_TRT_TABLE_DOMAIN (c) ? TRT_TABLE_CHAR_1 (trt, c) : c;
1412 }
1413
1414 /* Macros used below. */
1415 #define DOWNCASE_TABLE_OF(buf, c) TRT_TABLE_OF (buf->downcase_table, c)
1416 #define UPCASE_TABLE_OF(buf, c) TRT_TABLE_OF (buf->upcase_table, c)
1417
1418 /* 1 if CH is upper case.  */
1419
1420 INLINE_HEADER int UPPERCASEP (struct buffer *buf, Emchar ch);
1421 INLINE_HEADER int
1422 UPPERCASEP (struct buffer *buf, Emchar ch)
1423 {
1424   return DOWNCASE_TABLE_OF (buf, ch) != ch;
1425 }
1426
1427 /* 1 if CH is lower case.  */
1428
1429 INLINE_HEADER int LOWERCASEP (struct buffer *buf, Emchar ch);
1430 INLINE_HEADER int
1431 LOWERCASEP (struct buffer *buf, Emchar ch)
1432 {
1433   return (UPCASE_TABLE_OF   (buf, ch) != ch &&
1434           DOWNCASE_TABLE_OF (buf, ch) == ch);
1435 }
1436
1437 /* 1 if CH is neither upper nor lower case.  */
1438
1439 INLINE_HEADER int NOCASEP (struct buffer *buf, Emchar ch);
1440 INLINE_HEADER int
1441 NOCASEP (struct buffer *buf, Emchar ch)
1442 {
1443   return UPCASE_TABLE_OF (buf, ch) == ch;
1444 }
1445
1446 /* Upcase a character, or make no change if that cannot be done.  */
1447
1448 INLINE_HEADER Emchar UPCASE (struct buffer *buf, Emchar ch);
1449 INLINE_HEADER Emchar
1450 UPCASE (struct buffer *buf, Emchar ch)
1451 {
1452   return (DOWNCASE_TABLE_OF (buf, ch) == ch) ? UPCASE_TABLE_OF (buf, ch) : ch;
1453 }
1454
1455 /* Upcase a character known to be not upper case.  Unused. */
1456
1457 #define UPCASE1(buf, ch) UPCASE_TABLE_OF (buf, ch)
1458
1459 /* Downcase a character, or make no change if that cannot be done. */
1460
1461 #define DOWNCASE(buf, ch) DOWNCASE_TABLE_OF (buf, ch)
1462
1463 #endif /* INCLUDED_buffer_h_ */