PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/syntax.h

https://bitbucket.org/zielmicha/emacs
C Header | 302 lines | 185 code | 39 blank | 78 comment | 16 complexity | 5cb410fb5a020e82af57347698a25278 MD5 | raw file
  1. /* Declarations having to do with GNU Emacs syntax tables.
  2. Copyright (C) 1985, 1993-1994, 1997-1998, 2001-2012
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Emacs.
  5. GNU Emacs is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. GNU Emacs is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
  15. extern void update_syntax_table (ptrdiff_t, EMACS_INT, int, Lisp_Object);
  16. /* The standard syntax table is stored where it will automatically
  17. be used in all new buffers. */
  18. #define Vstandard_syntax_table BVAR (&buffer_defaults, syntax_table)
  19. /* A syntax table is a chartable whose elements are cons cells
  20. (CODE+FLAGS . MATCHING-CHAR). MATCHING-CHAR can be nil if the char
  21. is not a kind of parenthesis.
  22. The low 8 bits of CODE+FLAGS is a code, as follows: */
  23. enum syntaxcode
  24. {
  25. Swhitespace, /* for a whitespace character */
  26. Spunct, /* for random punctuation characters */
  27. Sword, /* for a word constituent */
  28. Ssymbol, /* symbol constituent but not word constituent */
  29. Sopen, /* for a beginning delimiter */
  30. Sclose, /* for an ending delimiter */
  31. Squote, /* for a prefix character like Lisp ' */
  32. Sstring, /* for a string-grouping character like Lisp " */
  33. Smath, /* for delimiters like $ in Tex. */
  34. Sescape, /* for a character that begins a C-style escape */
  35. Scharquote, /* for a character that quotes the following character */
  36. Scomment, /* for a comment-starting character */
  37. Sendcomment, /* for a comment-ending character */
  38. Sinherit, /* use the standard syntax table for this character */
  39. Scomment_fence, /* Starts/ends comment which is delimited on the
  40. other side by any char with the same syntaxcode. */
  41. Sstring_fence, /* Starts/ends string which is delimited on the
  42. other side by any char with the same syntaxcode. */
  43. Smax /* Upper bound on codes that are meaningful */
  44. };
  45. /* Set the syntax entry VAL for char C in table TABLE. */
  46. #define SET_RAW_SYNTAX_ENTRY(table, c, val) \
  47. CHAR_TABLE_SET ((table), c, (val))
  48. /* Set the syntax entry VAL for char-range RANGE in table TABLE.
  49. RANGE is a cons (FROM . TO) specifying the range of characters. */
  50. #define SET_RAW_SYNTAX_ENTRY_RANGE(table, range, val) \
  51. Fset_char_table_range ((table), (range), (val))
  52. /* SYNTAX_ENTRY fetches the information from the entry for character C
  53. in syntax table TABLE, or from globally kept data (gl_state).
  54. Does inheritance. */
  55. /* CURRENT_SYNTAX_TABLE gives the syntax table valid for current
  56. position, it is either the buffer's syntax table, or syntax table
  57. found in text properties. */
  58. #ifdef SYNTAX_ENTRY_VIA_PROPERTY
  59. # define SYNTAX_ENTRY(c) \
  60. (gl_state.use_global ? gl_state.global_code : SYNTAX_ENTRY_INT (c))
  61. # define CURRENT_SYNTAX_TABLE gl_state.current_syntax_table
  62. #else
  63. # define SYNTAX_ENTRY SYNTAX_ENTRY_INT
  64. # define CURRENT_SYNTAX_TABLE BVAR (current_buffer, syntax_table)
  65. #endif
  66. #define SYNTAX_ENTRY_INT(c) CHAR_TABLE_REF (CURRENT_SYNTAX_TABLE, (c))
  67. /* Extract the information from the entry for character C
  68. in the current syntax table. */
  69. #ifdef __GNUC__
  70. #define SYNTAX(c) \
  71. ({ Lisp_Object _syntax_temp; \
  72. _syntax_temp = SYNTAX_ENTRY (c); \
  73. (CONSP (_syntax_temp) \
  74. ? (enum syntaxcode) (XINT (XCAR (_syntax_temp)) & 0xff) \
  75. : Swhitespace); })
  76. #define SYNTAX_WITH_FLAGS(c) \
  77. ({ Lisp_Object _syntax_temp; \
  78. _syntax_temp = SYNTAX_ENTRY (c); \
  79. (CONSP (_syntax_temp) \
  80. ? XINT (XCAR (_syntax_temp)) \
  81. : (int) Swhitespace); })
  82. #define SYNTAX_MATCH(c) \
  83. ({ Lisp_Object _syntax_temp; \
  84. _syntax_temp = SYNTAX_ENTRY (c); \
  85. (CONSP (_syntax_temp) \
  86. ? XCDR (_syntax_temp) \
  87. : Qnil); })
  88. #else
  89. extern Lisp_Object syntax_temp;
  90. #define SYNTAX(c) \
  91. (syntax_temp = SYNTAX_ENTRY ((c)), \
  92. (CONSP (syntax_temp) \
  93. ? (enum syntaxcode) (XINT (XCAR (syntax_temp)) & 0xff) \
  94. : Swhitespace))
  95. #define SYNTAX_WITH_FLAGS(c) \
  96. (syntax_temp = SYNTAX_ENTRY ((c)), \
  97. (CONSP (syntax_temp) \
  98. ? XINT (XCAR (syntax_temp)) \
  99. : (int) Swhitespace))
  100. #define SYNTAX_MATCH(c) \
  101. (syntax_temp = SYNTAX_ENTRY ((c)), \
  102. (CONSP (syntax_temp) \
  103. ? XCDR (syntax_temp) \
  104. : Qnil))
  105. #endif
  106. /* Whether the syntax of the character C has the prefix flag set. */
  107. extern int syntax_prefix_flag_p (int c);
  108. /* This array, indexed by a character, contains the syntax code which that
  109. character signifies (as a char). For example,
  110. (enum syntaxcode) syntax_spec_code['w'] is Sword. */
  111. extern unsigned char syntax_spec_code[0400];
  112. /* Indexed by syntax code, give the letter that describes it. */
  113. extern char syntax_code_spec[16];
  114. /* Convert the byte offset BYTEPOS into a character position,
  115. for the object recorded in gl_state with SETUP_SYNTAX_TABLE_FOR_OBJECT.
  116. The value is meant for use in the UPDATE_SYNTAX_TABLE... macros.
  117. These macros do nothing when parse_sexp_lookup_properties is 0,
  118. so we return 0 in that case, for speed. */
  119. #define SYNTAX_TABLE_BYTE_TO_CHAR(bytepos) \
  120. (! parse_sexp_lookup_properties \
  121. ? 0 \
  122. : STRINGP (gl_state.object) \
  123. ? string_byte_to_char (gl_state.object, (bytepos)) \
  124. : BUFFERP (gl_state.object) \
  125. ? buf_bytepos_to_charpos (XBUFFER (gl_state.object), \
  126. (bytepos) + BUF_BEGV_BYTE (XBUFFER (gl_state.object)) - 1) - BUF_BEGV (XBUFFER (gl_state.object)) + 1 \
  127. : NILP (gl_state.object) \
  128. ? BYTE_TO_CHAR ((bytepos) + BEGV_BYTE - 1) - BEGV + 1 \
  129. : (bytepos))
  130. /* Make syntax table state (gl_state) good for CHARPOS, assuming it is
  131. currently good for a position before CHARPOS. */
  132. #define UPDATE_SYNTAX_TABLE_FORWARD(charpos) \
  133. (parse_sexp_lookup_properties \
  134. && (charpos) >= gl_state.e_property \
  135. ? (update_syntax_table ((charpos) + gl_state.offset, 1, 0, \
  136. gl_state.object), \
  137. 1) \
  138. : 0)
  139. /* Make syntax table state (gl_state) good for CHARPOS, assuming it is
  140. currently good for a position after CHARPOS. */
  141. #define UPDATE_SYNTAX_TABLE_BACKWARD(charpos) \
  142. (parse_sexp_lookup_properties \
  143. && (charpos) < gl_state.b_property \
  144. ? (update_syntax_table ((charpos) + gl_state.offset, -1, 0, \
  145. gl_state.object), \
  146. 1) \
  147. : 0)
  148. /* Make syntax table good for CHARPOS. */
  149. #define UPDATE_SYNTAX_TABLE(charpos) \
  150. (parse_sexp_lookup_properties \
  151. && (charpos) < gl_state.b_property \
  152. ? (update_syntax_table ((charpos) + gl_state.offset, -1, 0, \
  153. gl_state.object), \
  154. 1) \
  155. : (parse_sexp_lookup_properties \
  156. && (charpos) >= gl_state.e_property \
  157. ? (update_syntax_table ((charpos) + gl_state.offset, 1, 0,\
  158. gl_state.object), \
  159. 1) \
  160. : 0))
  161. /* This macro sets up the buffer-global syntax table. */
  162. #define SETUP_BUFFER_SYNTAX_TABLE() \
  163. do \
  164. { \
  165. gl_state.use_global = 0; \
  166. gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);\
  167. } while (0)
  168. /* This macro should be called with FROM at the start of forward
  169. search, or after the last position of the backward search. It
  170. makes sure that the first char is picked up with correct table, so
  171. one does not need to call UPDATE_SYNTAX_TABLE immediately after the
  172. call.
  173. Sign of COUNT gives the direction of the search.
  174. */
  175. #define SETUP_SYNTAX_TABLE(FROM, COUNT) \
  176. do \
  177. { \
  178. SETUP_BUFFER_SYNTAX_TABLE (); \
  179. gl_state.b_property = BEGV; \
  180. gl_state.e_property = ZV + 1; \
  181. gl_state.object = Qnil; \
  182. gl_state.offset = 0; \
  183. if (parse_sexp_lookup_properties) \
  184. if ((COUNT) > 0 || (FROM) > BEGV) \
  185. update_syntax_table ((COUNT) > 0 ? (FROM) : (FROM) - 1, (COUNT),\
  186. 1, Qnil); \
  187. } \
  188. while (0)
  189. /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
  190. If it is t (which is only used in fast_c_string_match_ignore_case),
  191. ignore properties altogether.
  192. This is meant for regex.c to use. For buffers, regex.c passes arguments
  193. to the UPDATE_SYNTAX_TABLE macros which are relative to BEGV.
  194. So if it is a buffer, we set the offset field to BEGV. */
  195. #define SETUP_SYNTAX_TABLE_FOR_OBJECT(OBJECT, FROM, COUNT) \
  196. do \
  197. { \
  198. SETUP_BUFFER_SYNTAX_TABLE (); \
  199. gl_state.object = (OBJECT); \
  200. if (BUFFERP (gl_state.object)) \
  201. { \
  202. struct buffer *buf = XBUFFER (gl_state.object); \
  203. gl_state.b_property = 1; \
  204. gl_state.e_property = BUF_ZV (buf) - BUF_BEGV (buf) + 1; \
  205. gl_state.offset = BUF_BEGV (buf) - 1; \
  206. } \
  207. else if (NILP (gl_state.object)) \
  208. { \
  209. gl_state.b_property = 1; \
  210. gl_state.e_property = ZV - BEGV + 1; \
  211. gl_state.offset = BEGV - 1; \
  212. } \
  213. else if (EQ (gl_state.object, Qt)) \
  214. { \
  215. gl_state.b_property = 0; \
  216. gl_state.e_property = PTRDIFF_MAX; \
  217. gl_state.offset = 0; \
  218. } \
  219. else \
  220. { \
  221. gl_state.b_property = 0; \
  222. gl_state.e_property = 1 + SCHARS (gl_state.object); \
  223. gl_state.offset = 0; \
  224. } \
  225. if (parse_sexp_lookup_properties) \
  226. update_syntax_table (((FROM) + gl_state.offset \
  227. + (COUNT > 0 ? 0 : -1)), \
  228. COUNT, 1, gl_state.object); \
  229. } \
  230. while (0)
  231. struct gl_state_s
  232. {
  233. Lisp_Object object; /* The object we are scanning. */
  234. ptrdiff_t start; /* Where to stop. */
  235. ptrdiff_t stop; /* Where to stop. */
  236. int use_global; /* Whether to use global_code
  237. or c_s_t. */
  238. Lisp_Object global_code; /* Syntax code of current char. */
  239. Lisp_Object current_syntax_table; /* Syntax table for current pos. */
  240. Lisp_Object old_prop; /* Syntax-table prop at prev pos. */
  241. ptrdiff_t b_property; /* First index where c_s_t is valid. */
  242. ptrdiff_t e_property; /* First index where c_s_t is
  243. not valid. */
  244. INTERVAL forward_i; /* Where to start lookup on forward */
  245. INTERVAL backward_i; /* or backward movement. The
  246. data in c_s_t is valid
  247. between these intervals,
  248. and possibly at the
  249. intervals too, depending
  250. on: */
  251. /* Offset for positions specified to UPDATE_SYNTAX_TABLE. */
  252. ptrdiff_t offset;
  253. };
  254. extern struct gl_state_s gl_state;
  255. extern ptrdiff_t scan_words (ptrdiff_t, EMACS_INT);