home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / buffer.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-07  |  29.4 KB  |  827 lines

  1. /* Header file for the buffer manipulation primitives.
  2.    Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.    Copyright (C) 1994, 1995 Amdahl Corporation.
  4.  
  5. This file is part of XEmacs.
  6.  
  7. XEmacs is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with XEmacs; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Synched up with: FSF 19.28. */
  22.  
  23. /* Substantially cleaned up and modified by Ben Wing. */
  24.  
  25. #ifndef _XEMACS_BUFFER_H_
  26. #define _XEMACS_BUFFER_H_
  27.  
  28. /*----------------------------------------------------------------------*/
  29. /*         Converting between buffer positions and byte indices         */
  30. /*----------------------------------------------------------------------*/
  31.  
  32. /********** Low-level functions **********/
  33.  
  34. /* Use these on contiguous strings of data.  If the text you're
  35.    operating on is known to come from a buffer, use the buffer-level
  36.    functions below -- they know about the gap and may be more
  37.    efficient. */
  38.  
  39. /* VALID_CHARPTR_P(): Given a pointer to text, does it point to
  40.    the beginning of a character? */
  41.  
  42. #ifdef MULE
  43. # define VALID_CHARPTR_P(ptr) MULE_FIRST_BYTE_P (*ptr)
  44. #else
  45. # define VALID_CHARPTR_P(ptr) 1
  46. #endif
  47.  
  48. /* ASSERT_VALID_CHARPTR(): If error-checking is enabled, verify
  49.    that the given pointer to text points to the beginning of a
  50.    character. */
  51.  
  52. #ifdef ERROR_CHECK_BUFPOS
  53. # define ASSERT_VALID_CHARPTR(ptr) assert (VALID_CHARPTR_P (ptr))
  54. #else
  55. # define ASSERT_VALID_CHARPTR(ptr)
  56. #endif
  57.  
  58. /* INC_CHARPTR(): Given a pointer to text (assumed to point at the
  59.    beginning of a character), modify that pointer so it points to
  60.    the beginning of the next character. */
  61.  
  62. #define INC_CHARPTR_1(ptr) do            \
  63. {                        \
  64.   ptr++;                    \
  65. } while (!VALID_CHARPTR_P (*ptr))
  66.  
  67. #define INC_CHARPTR(ptr) do            \
  68. {                        \
  69.   ASSERT_VALID_CHARPTR (ptr);            \
  70.   INC_CHARPTR_1 (ptr);                \
  71. } while (0)
  72.  
  73. /* DEC_CHARPTR(): Given a pointer to text (assumed to point at the
  74.    beginning of a character), modify that pointer so it points to
  75.    the beginning of the previous character.  We can't do the
  76.    assert() above because we might not be pointing to valid memory --
  77.    we might be just off the end. */
  78.  
  79. #define DEC_CHARPTR_1(ptr) do            \
  80. {                        \
  81.   ptr--;                    \
  82. } while (!VALID_CHARPTR_P (*ptr))
  83.  
  84. #define DEC_CHARPTR(ptr) do            \
  85. {                        \
  86.   ASSERT_VALID_CHARPTR (ptr);            \
  87.   DEC_CHARPTR_1 (ptr);                \
  88. } while (0)
  89.  
  90. /* VALIDATE_CHARPTR_BACKWARD(): Make sure that PTR is pointing to the
  91.    beginning of a character.  If not, back up until this is the case.
  92.    Note that there are not too many places where it is legitimate to
  93.    do this sort of thing.  It's an error if you're passed an "invalid"
  94.    char * pointer. */
  95.  
  96. # define VALIDATE_CHARPTR_BACKWARD(ptr) do    \
  97. {                        \
  98.   while (!VALID_CHARPTR_P (*ptr)) ptr--;    \
  99. } while (0)
  100.  
  101. /* VALIDATE_CHARPTR_FORWARD(): Make sure that PTR is pointing to the
  102.    beginning of a character.  If not, move forward until this is the
  103.    case.  Note that there are not too many places where it is
  104.    legitimate to do this sort of thing.  It's an error if you're
  105.    passed an "invalid" char * pointer. */
  106.  
  107. # define VALIDATE_CHARPTR_FORWARD(ptr) do    \
  108. {                        \
  109.   while (!VALID_CHARPTR_P (*ptr)) ptr++;    \
  110. } while (0)
  111.  
  112. #ifdef MULE
  113.  
  114. #include "mule.h"
  115.  
  116. /* bytecount_to_charcount(): Given a pointer to a text string and a
  117.    length in bytes, return the equivalent length in characters. */
  118.  
  119. extern Charcount bytecount_to_charcount (unsigned char *ptr, Bytecount len);
  120.  
  121. /* charcount_to_bytecount(): Given a pointer to a text string and a
  122.    length in characters, return the equivalent length in bytes. */
  123.  
  124. extern Bytecount charcount_to_bytecount (unsigned char *ptr, Charcount len);
  125.  
  126. /* valid_char_p(): Return whether the given Emchar is valid. */
  127.  
  128. extern int valid_char_p (Emchar ch);
  129.  
  130. #else /* not MULE */
  131.  
  132. # define bytecount_to_charcount(ptr, len) (len)
  133. # define charcount_to_bytecount(ptr, len) (len)
  134. # define valid_char_p(ch)        ((ch) >= 0 && (ch) < 0400)
  135.  
  136. #endif /* MULE */
  137.  
  138. MAC_DECLARE_EXTERN (CONST Bufbyte *, mactemp_charptr)
  139.  
  140. /* charptr_addr(): Given a pointer to Emacs characters and an offset in
  141.    characters, return a pointer to the beginning of the character
  142.    referenced by the offset. */
  143.  
  144. #define charptr_addr(ptr, offset)                    \
  145. MAC_BEGIN                                \
  146.   MAC_DECLARE (CONST Bufbyte *, mactemp_charptr, ptr)            \
  147.   mactemp_charptr + charcount_to_bytecount (mactemp_charptr, offset)    \
  148. MAC_END  
  149.  
  150. /* charptr_length(): Given a zero-terminated pointer to Emacs characters,
  151.    return the number of Emacs characters contained within. */
  152.  
  153. #define charptr_length(ptr)                              \
  154. MAC_BEGIN                                      \
  155.   MAC_DECLARE (CONST Bufbyte *, mactemp_charptr, ptr)                  \
  156.   bytecount_to_charcount (mactemp_charptr, strlen ((char *) mactemp_charptr)) \
  157. MAC_END
  158.  
  159. /* charptr_char(): Given a pointer to Emacs characters and an offset in
  160.    characters, return the Emacs character referenced by the offset. */
  161.  
  162. #define charptr_char(ptr, offset) \
  163.   charptr_to_emchar (charptr_addr (ptr, offset))
  164.  
  165. /********** Buffer-level functions **********/
  166.  
  167. /* VALID_BYTIND_P(): Given a byte index, does it point to
  168.    the beginning of a character? */
  169.  
  170. #ifdef MULE
  171. # define VALID_BYTIND_P(buf, x) \
  172.   MULE_FIRST_BYTE_P (*BI_BUF_BYTE_ADDRESS (buf, x))
  173. #else
  174. # define VALID_BYTIND_P(buf, x) 1
  175. #endif
  176.  
  177. /* ASSERT_VALID_BYTIND(): If error-checking is enabled, verify
  178.    that the given byte index is within range and points to the
  179.    beginning of a character. */
  180.  
  181. #ifdef ERROR_CHECK_BUFPOS
  182. # define ASSERT_VALID_BYTIND(buf, x) do                \
  183. {                                \
  184.   assert (BUFFER_LIVE_P (buf));                    \
  185.   assert ((x) >= BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf));    \
  186.   assert (VALID_BYTIND_P (buf, x));                \
  187. } while (0)
  188. # define ASSERT_VALID_BYTIND_FORWARD(buf, x) do            \
  189. {                                \
  190.   assert (BUFFER_LIVE_P (buf));                    \
  191.   assert ((x) >= BI_BUF_BEG (buf) && x < BI_BUF_Z (buf));    \
  192.   assert (VALID_BYTIND_P (buf, x));                \
  193. } while (0)
  194. # define ASSERT_VALID_BYTIND_BACKWARD(buf, x) do        \
  195. {                                \
  196.   assert (BUFFER_LIVE_P (buf));                    \
  197.   assert ((x) > BI_BUF_BEG (buf) && x <= BI_BUF_Z (buf));    \
  198.   assert (VALID_BYTIND_P (buf, x));                \
  199. } while (0)
  200. #else
  201. # define ASSERT_VALID_BYTIND(buf, x)
  202. # define ASSERT_VALID_BYTIND_FORWARD(buf, x)
  203. # define ASSERT_VALID_BYTIND_BACKWARD(buf, x)
  204. #endif
  205.  
  206. /* VALIDATE_BYTIND_BACKWARD(): Make sure that the given byte index is
  207.    pointing to the beginning of a character.  If not, back up until
  208.    this is the case.  Note that there are not too many places where it
  209.    is legitimate to do this sort of thing.  It's an error if you're
  210.    passed an "invalid" byte index. */
  211.  
  212. /* #### these should add some error-checking and encapsulate the
  213.    args against multiple evaluation. */
  214.  
  215. #define VALIDATE_BYTIND_BACKWARD(buf, x) \
  216.   VALIDATE_BYTIND_BACKWARD_1 (buf, x)
  217. #define VALIDATE_BYTIND_FORWARD(buf, x) \
  218.   VALIDATE_BYTIND_FORWARD_1 (buf, x)
  219.  
  220. /* Note that, although the Mule version will work fine for non-Mule
  221.    as well (it should reduce down to nothing), we provide a separate
  222.    version to avoid compilation warnings and possible non-optimal
  223.    results with stupid compilers. */
  224.  
  225. /* Note also that this may evaluate the lvalue (x) more than once. */
  226.  
  227. #ifdef MULE
  228. # define VALIDATE_BYTIND_BACKWARD_1(buf, x) do        \
  229. {                            \
  230.   Bufbyte *__ibptr = BI_BUF_BYTE_ADDRESS (buf, x);    \
  231.   while (!VALID_BYTIND_P (*__ibptr))            \
  232.     __ibptr--, (x)--;                    \
  233. } while (0)
  234. #else
  235. # define VALIDATE_BYTIND_BACKWARD_1(buf, x)
  236. #endif
  237.  
  238. /* VALIDATE_BYTIND_FORWARD(): Make sure that the given byte index is
  239.    pointing to the beginning of a character.  If not, move forward
  240.    until this is the case.  Note that there are not too many places
  241.    where it is legitimate to do this sort of thing.  It's an error if
  242.    you're passed an "invalid" byte index. */
  243.  
  244. /* Note that, although the Mule version will work fine for non-Mule
  245.    as well (it should reduce down to nothing), we provide a separate
  246.    version to avoid compilation warnings and possible non-optimal
  247.    results with stupid compilers. */
  248.  
  249. /* Note also that this may evaluate the lvalue (x) more than once. */
  250.  
  251. #ifdef MULE
  252. # define VALIDATE_BYTIND_FORWARD_1(buf, x) do        \
  253. {                            \
  254.   Bufbyte *__ibptr = BI_BUF_BYTE_ADDRESS (buf, x);    \
  255.   while (!VALID_BYTIND_P (*__ibptr))            \
  256.     __ibptr++, (x)++;                    \
  257. } while (0)
  258. #else
  259. # define VALIDATE_BYTIND_FORWARD_1(buf, x)
  260. #endif
  261.  
  262. /* INC_BYTIND(): Given a byte index (assumed to point at the beginning
  263.    of a character), modify that value so it points to the beginning
  264.    of the next character. */
  265.  
  266. /* Note that in the simplest case (no MULE, no ERROR_CHECK_BUFPOS),
  267.    this crap reduces down to simply (x)++. */
  268.  
  269. #define INC_BYTIND_1(buf, x) do                \
  270. {                            \
  271.   /* Note that we do the increment first to        \
  272.      make sure that the pointer in            \
  273.      VALIDATE_BYTIND_FORWARD_1() ends up on        \
  274.      the correct side of the gap */            \
  275.   (x)++;                        \
  276.   VALIDATE_BYTIND_FORWARD_1 (buf, x);            \
  277. } while (0)
  278.  
  279. #define INC_BYTIND(buf, x) do                \
  280. {                            \
  281.   struct buffer *__ibbuf = (buf);            \
  282.   Bytind *__ibx = &(x);                    \
  283.   ASSERT_VALID_BYTIND_FORWARD (__ibbuf, *__ibx);    \
  284.   INC_BYTIND_1 (__ibbuf, *__ibx);            \
  285. } while (0)
  286.  
  287. /* DEC_BYTIND(): Given a byte index (assumed to point at the
  288.    beginning of a character), modify that value so it points to
  289.    the beginning of the previous character.  Unlike for DEC_CHARPTR(),
  290.    we can do all the assert()s because there are sentinels at the
  291.    beginning of the gap and the end of the buffer. */
  292.  
  293. /* Note that in the simplest case (no MULE, no ERROR_CHECK_BUFPOS),
  294.    this crap reduces down to simply (x)--. */
  295.  
  296. #define DEC_BYTIND_1(buf, x) do                \
  297. {                            \
  298.   /* Note that we do the decrement first to        \
  299.      make sure that the pointer in            \
  300.      VALIDATE_BYTIND_FORWARD_1() ends up on        \
  301.      the correct side of the gap */            \
  302.   (x)--;                        \
  303.   VALIDATE_BYTIND_BACKWARD_1 (buf, x);            \
  304. } while (0)
  305.  
  306. #define DEC_BYTIND(buf, x) do                \
  307. {                            \
  308.   struct buffer *__ibbuf = (buf);            \
  309.   Bytind *__ibx = &(x);                    \
  310.   ASSERT_VALID_BYTIND_BACKWARD (__ibbuf, *__ibx);    \
  311.   DEC_BYTIND_1 (__ibbuf, *__ibx);            \
  312. } while (0)
  313.  
  314. #if defined (MULE) || defined (ERROR_CHECK_BUFPOS)
  315.  
  316. extern Bytind bufpos_to_bytind (struct buffer *buf, Bufpos x);
  317. extern Bufpos bytind_to_bufpos (struct buffer *buf, Bytind x);
  318. extern Charcount buf_bytecount_to_charcount (struct buffer *buf,
  319.                          Bytind x,
  320.                          Bytecount len);
  321. extern Bytecount buf_charcount_to_bytecount (struct buffer *buf,
  322.                          Bytind x,
  323.                          Charcount len);
  324.  
  325. #else
  326.  
  327. # define bufpos_to_bytind(buf, x)    ((Bytind) x)
  328. # define bytind_to_bufpos(buf, x)    ((Bufpos) x)
  329. # define buf_bytecount_to_charcount(buf, x, len) (len)
  330. # define buf_charcount_to_bytecount(buf, x, len) (len)
  331.  
  332. #endif /* not MULE and not ERROR_CHECK_BUFPOS */
  333.  
  334.  
  335. /* Maximum number of buffer bytes per Emacs character */
  336. #ifdef MULE
  337. # define MAX_EMCHAR_LEN 4
  338. #else
  339. # define MAX_EMCHAR_LEN 1
  340. #endif
  341.  
  342. /* flags for get_bufpos() and get_bufrange(). */
  343. /* at most one of GB_COERCE_RANGE and GB_NO_ERROR_IF_BAD should be
  344.    specified. */
  345.  
  346. #define GB_ALLOW_PAST_ACCESSIBLE    (1 << 0)
  347. #define GB_ALLOW_NIL            (1 << 1)
  348. #define GB_CHECK_ORDER            (1 << 2)
  349. #define GB_COERCE_RANGE            (1 << 3)
  350. #define GB_NO_ERROR_IF_BAD        (1 << 4)
  351.  
  352. #define BYTIND_INVALID ((Bytind) -1)
  353.  
  354. /* Some convenience macros for working with buffer positions
  355.    and byte indices. */
  356.  
  357. #define make_bufpos(buf, ind) make_number (bytind_to_bufpos (buf, ind))
  358.  
  359. #define c_charptr_from_external_static(ptr, bin) \
  360.   ((char *) charptr_from_external_static (ptr, -1, 0, bin))
  361. #define c_charptr_to_external_static(ptr, bin) \
  362.   charptr_to_external_static ((Bufbyte *) ptr, -1, 0, bin)
  363.  
  364. #define c_charptr_from_external(ptr) c_charptr_from_external_static (ptr, 0)
  365. #define c_charptr_from_external2(ptr) c_charptr_from_external_static (ptr, 1)
  366. #define c_charptr_from_external3(ptr) c_charptr_from_external_static (ptr, 2)
  367. #define c_charptr_from_external4(ptr) c_charptr_from_external_static (ptr, 3)
  368. #define c_charptr_from_external5(ptr) c_charptr_from_external_static (ptr, 4)
  369.  
  370. #define c_charptr_to_external(ptr) c_charptr_to_external_static (ptr, 0)
  371. #define c_charptr_to_external2(ptr) c_charptr_to_external_static (ptr, 1)
  372. #define c_charptr_to_external3(ptr) c_charptr_to_external_static (ptr, 2)
  373. #define c_charptr_to_external4(ptr) c_charptr_to_external_static (ptr, 3)
  374. #define c_charptr_to_external5(ptr) c_charptr_to_external_static (ptr, 4)
  375.  
  376. #define charptr_from_external(ptr, len, len_out) \
  377.   charptr_from_external_static (ptr, len, len_out, 0)
  378. #define charptr_from_external2(ptr, len, len_out) \
  379.   charptr_from_external_static (ptr, len, len_out, 1)
  380. #define charptr_from_external3(ptr, len, len_out) \
  381.   charptr_from_external_static (ptr, len, len_out, 2)
  382. #define charptr_from_external4(ptr, len, len_out) \
  383.   charptr_from_external_static (ptr, len, len_out, 3)
  384. #define charptr_from_external5(ptr, len, len_out) \
  385.   charptr_from_external_static (ptr, len, len_out, 4)
  386.  
  387. #define charptr_to_external(ptr, len, len_out) \
  388.   charptr_to_external_static (ptr, len, len_out, 0)
  389. #define charptr_to_external2(ptr, len, len_out) \
  390.   charptr_to_external_static (ptr, len, len_out, 1)
  391. #define charptr_to_external3(ptr, len, len_out) \
  392.   charptr_to_external_static (ptr, len, len_out, 2)
  393. #define charptr_to_external4(ptr, len, len_out) \
  394.   charptr_to_external_static (ptr, len, len_out, 3)
  395. #define charptr_to_external5(ptr, len, len_out) \
  396.   charptr_to_external_static (ptr, len, len_out, 4)
  397.  
  398. /*----------------------------------------------------------------------*/
  399. /*              Converting between positions and addresses              */
  400. /*----------------------------------------------------------------------*/
  401.  
  402. /* Convert the address of a byte in the buffer into a position.  */
  403. #define BI_BUF_PTR_BYTE_POS(buf, ptr)            \
  404. ((ptr) - (buf)->text.beg + 1                \
  405.  - ((ptr - (buf)->text.beg + 1) > (buf)->text.gpt    \
  406.     ? (buf)->text.gap_size : 0))
  407. #define BUF_PTR_BYTE_POS(buf, ptr) \
  408.   bytind_to_bufpos (buf, BI_BUF_PTR_BYTE_POS (buf, ptr))
  409.  
  410. /* Address of byte at position POS in buffer. */
  411. #define BI_BUF_BYTE_ADDRESS(buf, pos)            \
  412. ((buf)->text.beg + (((pos) >= (buf)->text.gpt ?        \
  413.  ((pos) + (buf)->text.gap_size) : (pos)) - 1))
  414. #define BUF_BYTE_ADDRESS(buf, pos) \
  415.   BI_BUF_BYTE_ADDRESS (buf, bufpos_to_bytind (buf, pos))
  416.  
  417. /* Address of byte before position POS in buffer. */
  418. #define BI_BUF_BYTE_ADDRESS_BEFORE(buf, pos)        \
  419. ((buf)->text.beg + (((pos) > (buf)->text.gpt ?        \
  420.  ((pos) + (buf)->text.gap_size) : (pos)) - 2))
  421. #define BUF_BYTE_ADDRESS_BEFORE(buf, pos) \
  422.   BI_BUF_BYTE_ADDRESS_BEFORE (buf, bufpos_to_bytind (buf, pos))
  423.  
  424. /*----------------------------------------------------------------------*/
  425. /*          Converting between byte indices and memory indices          */
  426. /*----------------------------------------------------------------------*/
  427.  
  428. #define valid_memind_p(buf, x)                        \
  429.   (((x) >= 1 && (x) <= (Memind) (buf)->text.gpt) ||            \
  430.    ((x) > (Memind) ((buf)->text.gpt + (buf)->text.gap_size) &&        \
  431.    (x) <= (Memind) ((buf)->text.z + (buf)->text.gap_size)))
  432. #define bytind_to_memind(buf, x)                    \
  433.   ((Memind) ((x) > (buf)->text.gpt ? ((x) + (buf)->text.gap_size) : (x)))
  434. #ifdef ERROR_CHECK_BUFPOS
  435. # define memind_to_bytind(buf, x)                    \
  436.   (assert (valid_memind_p (buf, x)),                    \
  437.    ((Bytind) ((x) > (Memind) (buf)->text.gpt ?                \
  438.           ((x) - (buf)->text.gap_size) : (x))))
  439. #else
  440. # define memind_to_bytind(buf, x)                    \
  441.   ((Bytind) ((x) > (Memind) (buf)->text.gpt ?                \
  442.          ((x) - (buf)->text.gap_size) : (x)))
  443. #endif
  444. #define memind_to_bufpos(buf, x)                    \
  445.   bytind_to_bufpos (buf, memind_to_bytind (buf, x))
  446. #define bufpos_to_memind(buf, x)                    \
  447.   bytind_to_memind (buf, bufpos_to_bytind (buf, x))
  448.  
  449.  
  450.  
  451. /*----------------------------------------------------------------------*/
  452. /*         Converting between buffer bytes and Emacs characters         */
  453. /*----------------------------------------------------------------------*/
  454.  
  455. # define simple_charptr_to_emchar(str)        ((Emchar) (str)[0])
  456. # define simple_emchar_to_charptr(x, str)    ((str)[0] = (Bufbyte) (x), 1)
  457.  
  458. #ifdef MULE
  459. extern Bytecount non_ascii_emchar_to_charptr (Emchar c, Bufbyte *str);
  460. extern Emchar non_ascii_charptr_to_emchar (Bufbyte *str);
  461. # define charptr_to_emchar(str)                    \
  462.   (BUFBYTE_ASCII_P (*(str)) ? simple_charptr_to_emchar (str) :    \
  463.    non_ascii_charptr_to_emchar (str))
  464. # define emchar_to_charptr(x, str)                \
  465.   (BUFBYTE_ASCII_P (x) ? simple_emchar_to_charptr (x, str) :    \
  466.    non_ascii_emchar_to_charptr (x, str))
  467. #else
  468. # define charptr_to_emchar(str)        simple_charptr_to_emchar (str)
  469. # define emchar_to_charptr(x, str)    simple_emchar_to_charptr (x, str)
  470. #endif /* MULE */
  471.  
  472. /* The character at position POS in buffer. */
  473. #define BI_BUF_FETCH_CHAR(buf, pos) \
  474.   charptr_to_emchar (BI_BUF_BYTE_ADDRESS (buf, pos))
  475. #define BUF_FETCH_CHAR(buf, pos) \
  476.   BI_BUF_FETCH_CHAR (buf, bufpos_to_bytind (buf, pos))
  477.  
  478. /* The character at position POS in buffer, as a string.  This is
  479.    equivalent to emchar_to_charptr (BUF_FETCH_CHAR (buf, pos), str)
  480.    but is faster for Mule. */
  481. #ifdef MULE
  482. # define BI_BUF_FETCH_CHAR_AS_STR(buf, pos, str) ---- no Mule support yet ----
  483. #else
  484. # define BI_BUF_FETCH_CHAR_AS_STR(buf, pos, str) \
  485.   ((str)[0] = (*BI_BUF_BYTE_ADDRESS (buf, pos)), 1)
  486. #endif /* MULE */
  487. #define BUF_FETCH_CHAR_AS_STR(buf, pos, str) \
  488.   BI_BUF_FETCH_CHAR_AS_STR (buf, bufpos_to_bytind (buf, pos), str)
  489.  
  490.  
  491. /*----------------------------------------------------------------------*/
  492. /*          Accessor macros for important positions in a buffer         */
  493. /*----------------------------------------------------------------------*/
  494.  
  495. /* None of these are lvalues.  Use the settor macros below to change
  496.    the positions. */
  497.  
  498. /* Beginning of buffer.  */ 
  499. #define BI_BUF_BEG(buf) ((Bytind) 1)
  500. #define BUF_BEG(buf) bytind_to_bufpos (buf, BI_BUF_BEG (buf))
  501.  
  502. /* Beginning of accessible range of buffer.  */ 
  503. #define BI_BUF_BEGV(buf) ((buf)->text.begv + 0)
  504. #define BUF_BEGV(buf) bytind_to_bufpos (buf, BI_BUF_BEGV (buf))
  505.  
  506. /* End of accessible range of buffer.  */ 
  507. #define BI_BUF_ZV(buf) ((buf)->text.zv + 0)
  508. #define BUF_ZV(buf) bytind_to_bufpos (buf, BI_BUF_ZV (buf))
  509.  
  510. /* End of buffer.  */ 
  511. #define BI_BUF_Z(buf) ((buf)->text.z + 0)
  512. #define BUF_Z(buf) bytind_to_bufpos (buf, BI_BUF_Z (buf))
  513.  
  514. /* Point. */
  515. #define BI_BUF_PT(buf) ((buf)->text.pt + 0)
  516. #define BUF_PT(buf) bytind_to_bufpos (buf, BI_BUF_PT (buf))
  517.  
  518. /*----------------------------------------------------------------------*/
  519. /*           Settor macros for important positions in a buffer          */
  520. /*----------------------------------------------------------------------*/
  521.  
  522. /* Set beginning of accessible range of buffer.  */ 
  523. #define SET_BI_BUF_BEGV(buf, value) \
  524.   do { (buf)->text.begv = (value); } while (0)
  525. #define SET_BUF_BEGV(buf, value) \
  526.   SET_BI_BUF_BEGV (buf, bytind_to_bufpos (buf, value))
  527.  
  528. /* Set end of accessible range of buffer.  */ 
  529. #define SET_BI_BUF_ZV(buf, value) do { (buf)->text.zv = (value); } while (0)
  530. #define SET_BUF_ZV(buf, value) \
  531.   SET_BI_BUF_ZV (buf, bytind_to_bufpos (buf, value))
  532.  
  533. /* Set point. */
  534. #define BI_BUF_SET_PT(buf, value) set_buffer_point (buf, value)
  535. #define BUF_SET_PT(buf, value) \
  536.   BI_BUF_SET_PT (buf, bufpos_to_bytind (buf, value))
  537.  
  538.  
  539. #if 0 /* FSFmacs */
  540. /* These macros exist in FSFmacs because SET_PT() in FSFmacs incorrectly
  541.    does too much stuff, such as moving out of invisible extents. */
  542. #define TEMP_SET_PT(position) (temp_set_point ((position), current_buffer))
  543. #define SET_BUF_PT(buf, value) ((buf)->text.pt = (value))
  544. #endif
  545.  
  546. /*----------------------------------------------------------------------*/
  547. /*                      Miscellaneous buffer values                     */
  548. /*----------------------------------------------------------------------*/
  549.  
  550. /* Number of characters in buffer */
  551. #define BUF_SIZE(buf) (BUF_Z (buf) - BUF_BEG (buf))
  552.  
  553. /* Is this buffer narrowed? */
  554. #define BUF_NARROWED(buf) ((BI_BUF_BEGV (buf) != BI_BUF_BEG (buf)) \
  555.                || (BI_BUF_ZV (buf) != BI_BUF_Z (buf)))
  556.  
  557. /* Modification count.  */
  558. #define BUF_MODIFF(buf) ((buf)->text.modiff)
  559.  
  560. /* Face changed.  */
  561. #define BUF_FACECHANGE(buf) ((buf)->text.face_change)
  562.  
  563. #define POINT_MARKER_P(marker) \
  564.    (XMARKER (marker)->buffer != 0 && \
  565.     EQ ((marker), XMARKER (marker)->buffer->point_marker))
  566.  
  567. /* WARNING:
  568.  
  569.    The new definitions of CEILING_OF() and FLOOR_OF() differ semantically
  570.    from the old ones (in FSF Emacs and XEmacs 19.11 and before).
  571.    Conversion is as follows:
  572.  
  573.    OLD_BI_CEILING_OF(n) = NEW_BI_CEILING_OF(n) - 1
  574.    OLD_BI_FLOOR_OF(n) = NEW_BI_FLOOR_OF(n + 1)
  575.  
  576.    The definitions were changed because the new definitions are more
  577.    consistent with the way everything else works in Emacs.
  578.  */
  579.  
  580. /* Properties of CEILING_OF and FLOOR_OF (also apply to BI_ variants):
  581.  
  582.    1) FLOOR_OF (CEILING_OF (n)) = n
  583.       CEILING_OF (FLOOR_OF (n)) = n
  584.  
  585.    2) CEILING_OF (n) = n if and only if n = ZV
  586.       FLOOR_OF (n) = n if and only if n = BEGV
  587.  
  588.    3) CEILING_OF (CEILING_OF (n)) = ZV
  589.       FLOOR_OF (FLOOR_OF (n)) = BEGV
  590.  
  591.    4) The bytes in the regions
  592.  
  593.       [BYTE_ADDRESS (n), BYTE_ADDRESS_BEFORE (CEILING_OF (n))]
  594.  
  595.       and
  596.  
  597.       [BYTE_ADDRESS (FLOOR_OF (n)), BYTE_ADDRESS_BEFORE (n)]
  598.  
  599.       are contiguous.
  600.    */
  601.  
  602.  
  603. /*  Return the maximum index in the buffer it is safe to scan forwards
  604.     past N to.  This is used to prevent buffer scans from running into
  605.     the gap (e.g. search.c).  All characters between N and CEILING_OF(N)
  606.     are located contiguous in memory.  Note that the character *at*
  607.     CEILING_OF(N) is not contiguous in memory. */
  608. #define BI_BUF_CEILING_OF(b, n)                        \
  609.   ((n) < (b)->text.gpt && (b)->text.gpt < BI_BUF_ZV (b) ?        \
  610.    (b)->text.gpt : BI_BUF_ZV (b))
  611. #define BUF_CEILING_OF(b, n)                        \
  612.   bytind_to_bufpos (b, BI_BUF_CEILING_OF (b, bufpos_to_bytind (b, n)))
  613.  
  614. /*  Return the minimum index in the buffer it is safe to scan backwards
  615.     past N to.  All characters between FLOOR_OF(N) and N are located
  616.     contiguous in memory.  Note that the character *at* N may not be
  617.     contiguous in memory. */
  618. #define BI_BUF_FLOOR_OF(b, n)                        \
  619.         (BI_BUF_BEGV (b) < (b)->text.gpt && (b)->text.gpt < (n) ?    \
  620.      (b)->text.gpt : BI_BUF_BEGV (b))
  621. #define BUF_FLOOR_OF(b, n)                        \
  622.   bytind_to_bufpos (b, BI_BUF_FLOOR_OF (b, bufpos_to_bytind (b, n)))
  623.  
  624. #define BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n)            \
  625.   ((n) < (b)->text.gpt && (b)->text.gpt < BI_BUF_Z (b) ?        \
  626.    (b)->text.gpt : BI_BUF_Z (b))
  627. #define BUF_CEILING_OF_IGNORE_ACCESSIBLE(b, n)                 \
  628.   bytind_to_bufpos                            \
  629.    (b, BI_BUF_CEILING_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
  630.  
  631. #define BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n)                \
  632.         (BI_BUF_BEG (b) < (b)->text.gpt && (b)->text.gpt < (n) ?    \
  633.      (b)->text.gpt : BI_BUF_BEG (b))
  634. #define BUF_FLOOR_OF_IGNORE_ACCESSIBLE(b, n)                 \
  635.   bytind_to_bufpos                            \
  636.    (b, BI_BUF_FLOOR_OF_IGNORE_ACCESSIBLE (b, bufpos_to_bytind (b, n)))
  637.  
  638.  
  639. struct buffer_text
  640.   {
  641.     Bufbyte *beg;        /* Actual address of buffer contents. */    
  642.     Bytind begv;        /* Index of beginning of accessible range. */
  643.     Bytind pt;            /* Position of point in buffer. */
  644.     Bytind gpt;            /* Index of gap in buffer. */
  645.     Bytind zv;            /* Index of end of accessible range. */
  646.     Bytind z;            /* Index of end of buffer. */
  647.     int gap_size;        /* Size of buffer's gap */
  648.     int modiff;            /* This counts buffer-modification events
  649.                    for this buffer.  It is incremented for
  650.                    each such event, and never otherwise
  651.                    changed.  */
  652.     int face_change;        /* This is set when a change in how the text
  653.                    should be displayed (e.g., font, color)
  654.                    is made. */
  655.   };
  656.  
  657. struct buffer
  658.   {
  659.     struct lcrecord_header header;
  660.  
  661.     /* This structure holds the coordinates of the buffer contents.  */
  662.     struct buffer_text text;
  663.  
  664.     /* Flags saying which DEFVAR_PER_BUFFER variables
  665.        are local to this buffer.  */
  666.     int local_var_flags;
  667.  
  668.     /* Value of text.modiff as of when visited file was read or written. */
  669.     int save_modified;
  670.  
  671.     /* Set to the modtime of the visited file when read or written.
  672.        -1 means visited file was nonexistent.
  673.        0 means visited file modtime unknown; in no case complain
  674.        about any mismatch on next save attempt.  */
  675.     int modtime;
  676.  
  677.     /* the value of text.modiff at the last auto-save. */
  678.     int auto_save_modified;
  679.  
  680.     /* The time at which we detected a failure to auto-save,
  681.        Or -1 if we didn't have a failure.  */
  682.     int auto_save_failure_time;
  683.  
  684.     /* Position in buffer at which display started
  685.        the last time this buffer was displayed */
  686.     int last_window_start;
  687.  
  688.     struct extent_list *extents;
  689.     struct stack_of_extents *soe;
  690.     struct buffer_change_data *changes;
  691. #ifdef MULE
  692.     struct buffer_mule_bufpos_data *mule_data;
  693. #endif
  694.  
  695.     /* These next two are exceptions -- both slots are be handled 
  696.        "specially" by gc_sweep, and their contents are not lisp-accessible 
  697.        as a local variable, but they are Lisp_Objects. */
  698.  
  699.     /* The markers that refer to this buffer.  This
  700.        is actually a single marker -- successive elements in its marker
  701.        `chain' are the other markers referring to this buffer */
  702.     struct Lisp_Marker *markers;
  703.  
  704.  
  705.     /* Everything from here down must be a Lisp_Object */
  706.  
  707. #define MARKED_SLOT(x) Lisp_Object x
  708. #include "bufslots.h"
  709. #undef MARKED_SLOT
  710. };
  711.  
  712. DECLARE_LRECORD (buffer, struct buffer);
  713. #define XBUFFER(x) XRECORD (x, buffer, struct buffer)
  714. #define XSETBUFFER(x, p) XSETRECORD (x, p, buffer)
  715. #define BUFFERP(x) RECORDP (x, buffer)
  716. #define CHECK_BUFFER(x, i) CHECK_RECORD (x, buffer)
  717.  
  718. #define BUFFER_LIVE_P(b) (!NILP ((b)->name))
  719. extern Lisp_Object Qbuffer_live_p;
  720. #define CHECK_LIVE_BUFFER(x, i)                     \
  721.   do { CHECK_BUFFER (x, i);                        \
  722.        if (!BUFFER_LIVE_P (XBUFFER (x)))                \
  723.      x = wrong_type_argument (Qbuffer_live_p, (x));            \
  724.      } while (0)
  725.  
  726. #define BUFFER_OR_STRING_P(x) (BUFFERP (x) || STRINGP (x))
  727.  
  728. extern Lisp_Object Qbuffer_or_string_p;
  729. #define CHECK_BUFFER_OR_STRING(x, i)                    \
  730.   do { if (!BUFFER_OR_STRING_P (x))                    \
  731.      x = wrong_type_argument (Qbuffer_or_string_p, (x));        \
  732.      } while (0)
  733.  
  734. #define CHECK_LIVE_BUFFER_OR_STRING(x, i)                \
  735.   do { CHECK_BUFFER_OR_STRING (x, i);                    \
  736.        if (BUFFERP (x))                            \
  737.      CHECK_LIVE_BUFFER (x, i);                    \
  738.      } while (0)
  739.  
  740. extern struct buffer *current_buffer;
  741.  
  742. /* This structure holds the default values of the buffer-local variables
  743.    defined with DefBufferLispVar, that have special slots in each buffer.
  744.    The default value occupies the same slot in this structure
  745.    as an individual buffer's value occupies in that buffer.
  746.    Setting the default value also goes through the alist of buffers
  747.    and stores into each buffer that does not say it has a local value.  */
  748.  
  749. extern Lisp_Object Vbuffer_defaults;
  750.  
  751. /* This structure marks which slots in a buffer have corresponding
  752.    default values in buffer_defaults.
  753.    Each such slot has a nonzero value in this structure.
  754.    The value has only one nonzero bit.
  755.  
  756.    When a buffer has its own local value for a slot,
  757.    the bit for that slot (found in the same slot in this structure)
  758.    is turned on in the buffer's local_var_flags slot.
  759.  
  760.    If a slot in this structure is zero, then even though there may
  761.    be a DefBufferLispVar for the slot, there is no default valuefeor it;
  762.    and the corresponding slot in buffer_defaults is not used.  */
  763.  
  764. extern struct buffer buffer_local_flags;
  765.  
  766.  
  767. /* Allocation of buffer data. */
  768.  
  769. #ifdef REL_ALLOC
  770.  
  771. extern char* r_alloc (char **, unsigned long);
  772. extern char* r_re_alloc (char **, unsigned long);
  773. extern void r_alloc_free (void **);
  774.  
  775. #define BUFFER_ALLOC(data,size) \
  776.   ((Bufbyte *) r_alloc ((char **) &data, (size) * sizeof(Bufbyte)))
  777. #define BUFFER_REALLOC(data,size) \
  778.   ((Bufbyte *) r_re_alloc ((char **) &data, (size) * sizeof(Bufbyte)))
  779. #define BUFFER_FREE(data) r_alloc_free ((void **) &(data))
  780. #define R_ALLOC_DECLARE(var,data) r_alloc_declare (&(var), data)
  781.  
  782. #else /* !REL_ALLOC */
  783.  
  784. #define BUFFER_ALLOC(data,size)\
  785.     (data = (Bufbyte *) xmalloc ((size) * sizeof(Bufbyte)))
  786. #define BUFFER_REALLOC(data,size)\
  787.     ((Bufbyte *) xrealloc (data, (size) * sizeof(Bufbyte)))
  788. /* Avoid excess parentheses, or syntax errors may rear their heads. */
  789. #define BUFFER_FREE(data) xfree (data)
  790. #define R_ALLOC_DECLARE(var,data)
  791.  
  792. #endif /* !REL_ALLOC */
  793.  
  794. /* A search buffer, with a fastmap allocated and ready to go.  */
  795. extern struct re_pattern_buffer searchbuf;
  796.  
  797.  
  798. extern Lisp_Object Vbuffer_alist;
  799. extern void set_buffer_internal (struct buffer *b);
  800. extern struct buffer *decode_buffer (Lisp_Object buffer, int allow_string);
  801. extern void get_bufrange (struct buffer *b, Lisp_Object from, Lisp_Object to,
  802.               Bufpos *start, Bufpos *end, unsigned int flags);
  803. extern void get_bufrange_bytind (struct buffer *b, Lisp_Object from,
  804.                  Lisp_Object to, Bytind *start, Bytind *end,
  805.                  unsigned int flags);
  806. extern Bufpos get_bufpos (struct buffer *b, Lisp_Object pos, int flags);
  807. extern Bytind get_bytind (struct buffer *b, Lisp_Object pos, int flags);
  808. extern void record_buffer (Lisp_Object buf);
  809. extern Lisp_Object get_buffer (Lisp_Object name,
  810.                                int error_if_deleted_or_does_not_exist);
  811.  
  812. /* from editfns.c */
  813. extern void widen_buffer (struct buffer *b, int no_clip);
  814.  
  815. /* from insdel.c */
  816. extern void set_buffer_point (struct buffer *buf, Bytind position);
  817. extern Bufbyte *charptr_from_external_static (CONST char *ptr, int len,
  818.                           Bytecount *len_out, int bin);
  819. extern char *charptr_to_external_static (CONST Bufbyte *ptr, Bytecount len,
  820.                      int *len_out, int bin);
  821. extern Bufbyte *charptr_from_external_malloc (CONST char *ptr, int len,
  822.                           Bytecount *len_out);
  823. extern char *charptr_to_external_malloc (CONST Bufbyte *ptr, Bytecount len,
  824.                      int *len_out);
  825.  
  826. #endif /* _XEMACS_BUFFER_H_ */
  827.