| 1 | /* GNU Emacs routines to deal with syntax tables; also word and list parsing. |
| 2 | Copyright (C) 1985, 87, 93, 94, 95, 97, 1998 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Emacs. |
| 5 | |
| 6 | GNU Emacs is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GNU Emacs is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Emacs; see the file COPYING. If not, write to |
| 18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | |
| 22 | #include <config.h> |
| 23 | #include <ctype.h> |
| 24 | #include "lisp.h" |
| 25 | #include "commands.h" |
| 26 | #include "buffer.h" |
| 27 | #include "charset.h" |
| 28 | #include <assert.h> |
| 29 | |
| 30 | /* Make syntax table lookup grant data in gl_state. */ |
| 31 | #define SYNTAX_ENTRY_VIA_PROPERTY |
| 32 | |
| 33 | #include "syntax.h" |
| 34 | #include "intervals.h" |
| 35 | |
| 36 | /* We use these constants in place for comment-style and |
| 37 | string-ender-char to distinguish comments/strings started by |
| 38 | comment_fence and string_fence codes. */ |
| 39 | |
| 40 | #define ST_COMMENT_STYLE (256 + 1) |
| 41 | #define ST_STRING_STYLE (256 + 2) |
| 42 | #include "category.h" |
| 43 | |
| 44 | Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error; |
| 45 | |
| 46 | int words_include_escapes; |
| 47 | int parse_sexp_lookup_properties; |
| 48 | |
| 49 | /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h, |
| 50 | if not compiled with GCC. No need to mark it, since it is used |
| 51 | only very temporarily. */ |
| 52 | Lisp_Object syntax_temp; |
| 53 | |
| 54 | /* This is the internal form of the parse state used in parse-partial-sexp. */ |
| 55 | |
| 56 | struct lisp_parse_state |
| 57 | { |
| 58 | int depth; /* Depth at end of parsing. */ |
| 59 | int instring; /* -1 if not within string, else desired terminator. */ |
| 60 | int incomment; /* -1 if in unnestable comment else comment nesting */ |
| 61 | int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */ |
| 62 | int quoted; /* Nonzero if just after an escape char at end of parsing */ |
| 63 | int thislevelstart; /* Char number of most recent start-of-expression at current level */ |
| 64 | int prevlevelstart; /* Char number of start of containing expression */ |
| 65 | int location; /* Char number at which parsing stopped. */ |
| 66 | int mindepth; /* Minimum depth seen while scanning. */ |
| 67 | int comstr_start; /* Position just after last comment/string starter. */ |
| 68 | Lisp_Object levelstarts; /* Char numbers of starts-of-expression |
| 69 | of levels (starting from outermost). */ |
| 70 | }; |
| 71 | \f |
| 72 | /* These variables are a cache for finding the start of a defun. |
| 73 | find_start_pos is the place for which the defun start was found. |
| 74 | find_start_value is the defun start position found for it. |
| 75 | find_start_value_byte is the corresponding byte position. |
| 76 | find_start_buffer is the buffer it was found in. |
| 77 | find_start_begv is the BEGV value when it was found. |
| 78 | find_start_modiff is the value of MODIFF when it was found. */ |
| 79 | |
| 80 | static int find_start_pos; |
| 81 | static int find_start_value; |
| 82 | static int find_start_value_byte; |
| 83 | static struct buffer *find_start_buffer; |
| 84 | static int find_start_begv; |
| 85 | static int find_start_modiff; |
| 86 | |
| 87 | |
| 88 | static int find_defun_start P_ ((int, int)); |
| 89 | static int back_comment P_ ((int, int, int, int, int, int *, int *)); |
| 90 | static int char_quoted P_ ((int, int)); |
| 91 | static Lisp_Object skip_chars P_ ((int, int, Lisp_Object, Lisp_Object)); |
| 92 | static Lisp_Object scan_lists P_ ((int, int, int, int)); |
| 93 | static void scan_sexps_forward P_ ((struct lisp_parse_state *, |
| 94 | int, int, int, int, |
| 95 | int, Lisp_Object, int)); |
| 96 | \f |
| 97 | |
| 98 | struct gl_state_s gl_state; /* Global state of syntax parser. */ |
| 99 | |
| 100 | INTERVAL interval_of (); |
| 101 | #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals |
| 102 | to scan to property-change. */ |
| 103 | |
| 104 | /* Update gl_state to an appropriate interval which contains CHARPOS. The |
| 105 | sign of COUNT give the relative position of CHARPOS wrt the previously |
| 106 | valid interval. If INIT, only [be]_property fields of gl_state are |
| 107 | valid at start, the rest is filled basing on OBJECT. |
| 108 | |
| 109 | `gl_state.*_i' are the intervals, and CHARPOS is further in the search |
| 110 | direction than the intervals - or in an interval. We update the |
| 111 | current syntax-table basing on the property of this interval, and |
| 112 | update the interval to start further than CHARPOS - or be |
| 113 | NULL_INTERVAL. We also update lim_property to be the next value of |
| 114 | charpos to call this subroutine again - or be before/after the |
| 115 | start/end of OBJECT. */ |
| 116 | |
| 117 | void |
| 118 | update_syntax_table (charpos, count, init, object) |
| 119 | int charpos, count, init; |
| 120 | Lisp_Object object; |
| 121 | { |
| 122 | Lisp_Object tmp_table; |
| 123 | int cnt = 0, invalidate = 1; |
| 124 | INTERVAL i, oldi; |
| 125 | |
| 126 | if (init) |
| 127 | { |
| 128 | gl_state.start = gl_state.b_property; |
| 129 | gl_state.stop = gl_state.e_property; |
| 130 | gl_state.forward_i = interval_of (charpos, object); |
| 131 | i = gl_state.backward_i = gl_state.forward_i; |
| 132 | gl_state.left_ok = gl_state.right_ok = 1; |
| 133 | invalidate = 0; |
| 134 | if (NULL_INTERVAL_P (i)) |
| 135 | return; |
| 136 | /* interval_of updates only ->position of the return value, so |
| 137 | update the parents manually to speed up update_interval. */ |
| 138 | while (!NULL_PARENT (i)) |
| 139 | { |
| 140 | if (AM_RIGHT_CHILD (i)) |
| 141 | i->parent->position = i->position |
| 142 | - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */ |
| 143 | - TOTAL_LENGTH (i->parent) |
| 144 | + LEFT_TOTAL_LENGTH (i->parent); |
| 145 | else |
| 146 | i->parent->position = i->position - LEFT_TOTAL_LENGTH (i) |
| 147 | + TOTAL_LENGTH (i); |
| 148 | i = i->parent; |
| 149 | } |
| 150 | i = gl_state.forward_i; |
| 151 | gl_state.b_property = i->position - 1 - gl_state.offset; |
| 152 | gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset; |
| 153 | goto update; |
| 154 | } |
| 155 | oldi = i = count > 0 ? gl_state.forward_i : gl_state.backward_i; |
| 156 | |
| 157 | /* We are guarantied to be called with CHARPOS either in i, |
| 158 | or further off. */ |
| 159 | if (NULL_INTERVAL_P (i)) |
| 160 | error ("Error in syntax_table logic for to-the-end intervals"); |
| 161 | else if (charpos < i->position) /* Move left. */ |
| 162 | { |
| 163 | if (count > 0) |
| 164 | error ("Error in syntax_table logic for intervals <-"); |
| 165 | /* Update the interval. */ |
| 166 | i = update_interval (i, charpos); |
| 167 | if (oldi->position != INTERVAL_LAST_POS (i)) |
| 168 | { |
| 169 | invalidate = 0; |
| 170 | gl_state.right_ok = 1; /* Invalidate the other end. */ |
| 171 | gl_state.forward_i = i; |
| 172 | gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset; |
| 173 | } |
| 174 | } |
| 175 | else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */ |
| 176 | { |
| 177 | if (count < 0) |
| 178 | error ("Error in syntax_table logic for intervals ->"); |
| 179 | /* Update the interval. */ |
| 180 | i = update_interval (i, charpos); |
| 181 | if (i->position != INTERVAL_LAST_POS (oldi)) |
| 182 | { |
| 183 | invalidate = 0; |
| 184 | gl_state.left_ok = 1; /* Invalidate the other end. */ |
| 185 | gl_state.backward_i = i; |
| 186 | gl_state.b_property = i->position - 1 - gl_state.offset; |
| 187 | } |
| 188 | } |
| 189 | else if (count > 0 ? gl_state.right_ok : gl_state.left_ok) |
| 190 | { |
| 191 | /* We do not need to recalculate tmp_table. */ |
| 192 | tmp_table = gl_state.old_prop; |
| 193 | } |
| 194 | |
| 195 | update: |
| 196 | tmp_table = textget (i->plist, Qsyntax_table); |
| 197 | |
| 198 | if (invalidate) |
| 199 | invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */ |
| 200 | |
| 201 | if (invalidate) /* Did not get to adjacent interval. */ |
| 202 | { /* with the same table => */ |
| 203 | /* invalidate the old range. */ |
| 204 | if (count > 0) |
| 205 | { |
| 206 | gl_state.backward_i = i; |
| 207 | gl_state.left_ok = 1; /* Invalidate the other end. */ |
| 208 | gl_state.b_property = i->position - 1 - gl_state.offset; |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | gl_state.forward_i = i; |
| 213 | gl_state.right_ok = 1; /* Invalidate the other end. */ |
| 214 | gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | gl_state.current_syntax_table = tmp_table; |
| 219 | gl_state.old_prop = tmp_table; |
| 220 | if (EQ (Fsyntax_table_p (tmp_table), Qt)) |
| 221 | { |
| 222 | gl_state.use_global = 0; |
| 223 | } |
| 224 | else if (CONSP (tmp_table)) |
| 225 | { |
| 226 | gl_state.use_global = 1; |
| 227 | gl_state.global_code = tmp_table; |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | gl_state.use_global = 0; |
| 232 | gl_state.current_syntax_table = current_buffer->syntax_table; |
| 233 | } |
| 234 | |
| 235 | while (!NULL_INTERVAL_P (i)) |
| 236 | { |
| 237 | if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table))) |
| 238 | { |
| 239 | if (count > 0) |
| 240 | gl_state.right_ok = 0; |
| 241 | else |
| 242 | gl_state.left_ok = 0; |
| 243 | break; |
| 244 | } |
| 245 | else if (cnt == INTERVALS_AT_ONCE) |
| 246 | { |
| 247 | if (count > 0) |
| 248 | gl_state.right_ok = 1; |
| 249 | else |
| 250 | gl_state.left_ok = 1; |
| 251 | break; |
| 252 | } |
| 253 | cnt++; |
| 254 | i = count > 0 ? next_interval (i) : previous_interval (i); |
| 255 | } |
| 256 | if (NULL_INTERVAL_P (i)) |
| 257 | { /* This property goes to the end. */ |
| 258 | if (count > 0) |
| 259 | gl_state.e_property = gl_state.stop; |
| 260 | else |
| 261 | gl_state.b_property = gl_state.start; |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | if (count > 0) |
| 266 | { |
| 267 | gl_state.e_property = i->position - gl_state.offset; |
| 268 | gl_state.forward_i = i; |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | gl_state.b_property = i->position + LENGTH (i) - 1 - gl_state.offset; |
| 273 | gl_state.backward_i = i; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | \f |
| 278 | /* Returns TRUE if char at CHARPOS is quoted. |
| 279 | Global syntax-table data should be set up already to be good at CHARPOS |
| 280 | or after. On return global syntax data is good for lookup at CHARPOS. */ |
| 281 | |
| 282 | static int |
| 283 | char_quoted (charpos, bytepos) |
| 284 | register int charpos, bytepos; |
| 285 | { |
| 286 | register enum syntaxcode code; |
| 287 | register int beg = BEGV; |
| 288 | register int quoted = 0; |
| 289 | int orig = charpos; |
| 290 | |
| 291 | DEC_BOTH (charpos, bytepos); |
| 292 | |
| 293 | while (bytepos >= beg) |
| 294 | { |
| 295 | UPDATE_SYNTAX_TABLE_BACKWARD (charpos); |
| 296 | code = SYNTAX (FETCH_CHAR (bytepos)); |
| 297 | if (! (code == Scharquote || code == Sescape)) |
| 298 | break; |
| 299 | |
| 300 | DEC_BOTH (charpos, bytepos); |
| 301 | quoted = !quoted; |
| 302 | } |
| 303 | |
| 304 | UPDATE_SYNTAX_TABLE (orig); |
| 305 | return quoted; |
| 306 | } |
| 307 | |
| 308 | /* Return the bytepos one character after BYTEPOS. |
| 309 | We assume that BYTEPOS is not at the end of the buffer. */ |
| 310 | |
| 311 | INLINE int |
| 312 | inc_bytepos (bytepos) |
| 313 | int bytepos; |
| 314 | { |
| 315 | if (NILP (current_buffer->enable_multibyte_characters)) |
| 316 | return bytepos + 1; |
| 317 | |
| 318 | INC_POS (bytepos); |
| 319 | return bytepos; |
| 320 | } |
| 321 | |
| 322 | /* Return the bytepos one character before BYTEPOS. |
| 323 | We assume that BYTEPOS is not at the start of the buffer. */ |
| 324 | |
| 325 | INLINE int |
| 326 | dec_bytepos (bytepos) |
| 327 | int bytepos; |
| 328 | { |
| 329 | if (NILP (current_buffer->enable_multibyte_characters)) |
| 330 | return bytepos - 1; |
| 331 | |
| 332 | DEC_POS (bytepos); |
| 333 | return bytepos; |
| 334 | } |
| 335 | \f |
| 336 | /* Find a defun-start that is the last one before POS (or nearly the last). |
| 337 | We record what we find, so that another call in the same area |
| 338 | can return the same value right away. |
| 339 | |
| 340 | There is no promise at which position the global syntax data is |
| 341 | valid on return from the subroutine, so the caller should explicitly |
| 342 | update the global data. */ |
| 343 | |
| 344 | static int |
| 345 | find_defun_start (pos, pos_byte) |
| 346 | int pos, pos_byte; |
| 347 | { |
| 348 | int opoint = PT, opoint_byte = PT_BYTE; |
| 349 | |
| 350 | /* Use previous finding, if it's valid and applies to this inquiry. */ |
| 351 | if (current_buffer == find_start_buffer |
| 352 | /* Reuse the defun-start even if POS is a little farther on. |
| 353 | POS might be in the next defun, but that's ok. |
| 354 | Our value may not be the best possible, but will still be usable. */ |
| 355 | && pos <= find_start_pos + 1000 |
| 356 | && pos >= find_start_value |
| 357 | && BEGV == find_start_begv |
| 358 | && MODIFF == find_start_modiff) |
| 359 | return find_start_value; |
| 360 | |
| 361 | /* Back up to start of line. */ |
| 362 | scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1); |
| 363 | |
| 364 | /* We optimize syntax-table lookup for rare updates. Thus we accept |
| 365 | only those `^\s(' which are good in global _and_ text-property |
| 366 | syntax-tables. */ |
| 367 | gl_state.current_syntax_table = current_buffer->syntax_table; |
| 368 | gl_state.use_global = 0; |
| 369 | while (PT > BEGV) |
| 370 | { |
| 371 | /* Open-paren at start of line means we found our defun-start. */ |
| 372 | if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen) |
| 373 | { |
| 374 | SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */ |
| 375 | if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen) |
| 376 | break; |
| 377 | /* Now fallback to the default value. */ |
| 378 | gl_state.current_syntax_table = current_buffer->syntax_table; |
| 379 | gl_state.use_global = 0; |
| 380 | } |
| 381 | /* Move to beg of previous line. */ |
| 382 | scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1); |
| 383 | } |
| 384 | |
| 385 | /* Record what we found, for the next try. */ |
| 386 | find_start_value = PT; |
| 387 | find_start_value_byte = PT_BYTE; |
| 388 | find_start_buffer = current_buffer; |
| 389 | find_start_modiff = MODIFF; |
| 390 | find_start_begv = BEGV; |
| 391 | find_start_pos = pos; |
| 392 | |
| 393 | TEMP_SET_PT_BOTH (opoint, opoint_byte); |
| 394 | |
| 395 | return find_start_value; |
| 396 | } |
| 397 | \f |
| 398 | /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */ |
| 399 | |
| 400 | static int |
| 401 | prev_char_comend_first (pos, pos_byte) |
| 402 | int pos, pos_byte; |
| 403 | { |
| 404 | int c, val; |
| 405 | |
| 406 | DEC_BOTH (pos, pos_byte); |
| 407 | UPDATE_SYNTAX_TABLE_BACKWARD (pos); |
| 408 | c = FETCH_CHAR (pos_byte); |
| 409 | val = SYNTAX_COMEND_FIRST (c); |
| 410 | UPDATE_SYNTAX_TABLE_FORWARD (pos + 1); |
| 411 | return val; |
| 412 | } |
| 413 | |
| 414 | /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */ |
| 415 | |
| 416 | static int |
| 417 | prev_char_comstart_first (pos, pos_byte) |
| 418 | int pos, pos_byte; |
| 419 | { |
| 420 | int c, val; |
| 421 | |
| 422 | DEC_BOTH (pos, pos_byte); |
| 423 | UPDATE_SYNTAX_TABLE_BACKWARD (pos); |
| 424 | c = FETCH_CHAR (pos_byte); |
| 425 | val = SYNTAX_COMSTART_FIRST (c); |
| 426 | UPDATE_SYNTAX_TABLE_FORWARD (pos + 1); |
| 427 | return val; |
| 428 | } |
| 429 | |
| 430 | /* Checks whether charpos FROM is at the end of a comment. |
| 431 | FROM_BYTE is the bytepos corresponding to FROM. |
| 432 | Do not move back before STOP. |
| 433 | |
| 434 | Return a positive value if we find a comment ending at FROM/FROM_BYTE; |
| 435 | return -1 otherwise. |
| 436 | |
| 437 | If successful, store the charpos of the comment's beginning |
| 438 | into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR. |
| 439 | |
| 440 | Global syntax data remains valid for backward search starting at |
| 441 | the returned value (or at FROM, if the search was not successful). */ |
| 442 | |
| 443 | static int |
| 444 | back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr) |
| 445 | int from, from_byte, stop; |
| 446 | int comnested, comstyle; |
| 447 | int *charpos_ptr, *bytepos_ptr; |
| 448 | { |
| 449 | /* Look back, counting the parity of string-quotes, |
| 450 | and recording the comment-starters seen. |
| 451 | When we reach a safe place, assume that's not in a string; |
| 452 | then step the main scan to the earliest comment-starter seen |
| 453 | an even number of string quotes away from the safe place. |
| 454 | |
| 455 | OFROM[I] is position of the earliest comment-starter seen |
| 456 | which is I+2X quotes from the comment-end. |
| 457 | PARITY is current parity of quotes from the comment end. */ |
| 458 | int parity = 0; |
| 459 | int my_stringend = 0; |
| 460 | int string_lossage = 0; |
| 461 | int comment_end = from; |
| 462 | int comment_end_byte = from_byte; |
| 463 | int comstart_pos = 0; |
| 464 | int comstart_byte; |
| 465 | /* Value that PARITY had, when we reached the position |
| 466 | in COMSTART_POS. */ |
| 467 | int comstart_parity = 0; |
| 468 | int scanstart = from - 1; |
| 469 | /* Place where the containing defun starts, |
| 470 | or 0 if we didn't come across it yet. */ |
| 471 | int defun_start = 0; |
| 472 | int defun_start_byte = 0; |
| 473 | register enum syntaxcode code; |
| 474 | int nesting = 1; /* current comment nesting */ |
| 475 | int c; |
| 476 | |
| 477 | /* At beginning of range to scan, we're outside of strings; |
| 478 | that determines quote parity to the comment-end. */ |
| 479 | while (from != stop) |
| 480 | { |
| 481 | int temp_byte; |
| 482 | |
| 483 | /* Move back and examine a character. */ |
| 484 | DEC_BOTH (from, from_byte); |
| 485 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
| 486 | |
| 487 | c = FETCH_CHAR (from_byte); |
| 488 | code = SYNTAX (c); |
| 489 | |
| 490 | /* If this char is the second of a 2-char comment end sequence, |
| 491 | back up and give the pair the appropriate syntax. */ |
| 492 | if (from > stop && SYNTAX_COMEND_SECOND (c) |
| 493 | && prev_char_comend_first (from, from_byte)) |
| 494 | { |
| 495 | code = Sendcomment; |
| 496 | DEC_BOTH (from, from_byte); |
| 497 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
| 498 | c = FETCH_CHAR (from_byte); |
| 499 | } |
| 500 | |
| 501 | /* If this char starts a 2-char comment start sequence, |
| 502 | treat it like a 1-char comment starter. */ |
| 503 | if (from < scanstart && SYNTAX_COMSTART_FIRST (c)) |
| 504 | { |
| 505 | temp_byte = inc_bytepos (from_byte); |
| 506 | UPDATE_SYNTAX_TABLE_FORWARD (from + 1); |
| 507 | if (SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte)) |
| 508 | && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte))) |
| 509 | code = Scomment; |
| 510 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
| 511 | } |
| 512 | |
| 513 | /* Ignore escaped characters, except comment-enders. */ |
| 514 | if (code != Sendcomment && char_quoted (from, from_byte)) |
| 515 | continue; |
| 516 | |
| 517 | /* Track parity of quotes. */ |
| 518 | if (code == Sstring) |
| 519 | { |
| 520 | parity ^= 1; |
| 521 | if (my_stringend == 0) |
| 522 | my_stringend = c; |
| 523 | /* If we have two kinds of string delimiters. |
| 524 | There's no way to grok this scanning backwards. */ |
| 525 | else if (my_stringend != c) |
| 526 | string_lossage = 1; |
| 527 | } |
| 528 | |
| 529 | if (code == Sstring_fence || code == Scomment_fence) |
| 530 | { |
| 531 | parity ^= 1; |
| 532 | if (my_stringend == 0) |
| 533 | my_stringend |
| 534 | = code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE; |
| 535 | /* If we have two kinds of string delimiters. |
| 536 | There's no way to grok this scanning backwards. */ |
| 537 | else if (my_stringend != (code == Sstring_fence |
| 538 | ? ST_STRING_STYLE : ST_COMMENT_STYLE)) |
| 539 | string_lossage = 1; |
| 540 | } |
| 541 | |
| 542 | if (code == Scomment) |
| 543 | /* FIXME: we should also check that the comstyle is correct |
| 544 | if the Scomment is a single-char. */ |
| 545 | { |
| 546 | if (comnested && --nesting <= 0 && parity == 0 && !string_lossage) |
| 547 | /* nested comments have to be balanced, so we don't need to |
| 548 | keep looking for earlier ones. We use here the same (slightly |
| 549 | incorrect) reasoning as below: since it is followed by uniform |
| 550 | paired string quotes, this comment-start has to be outside of |
| 551 | strings, else the comment-end itself would be inside a string. */ |
| 552 | goto done; |
| 553 | |
| 554 | /* Record comment-starters according to that |
| 555 | quote-parity to the comment-end. */ |
| 556 | comstart_parity = parity; |
| 557 | comstart_pos = from; |
| 558 | comstart_byte = from_byte; |
| 559 | } |
| 560 | |
| 561 | /* If we find another earlier comment-ender, |
| 562 | any comment-starts earlier than that don't count |
| 563 | (because they go with the earlier comment-ender). */ |
| 564 | if (code == Sendcomment |
| 565 | && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)) == comstyle) |
| 566 | { |
| 567 | if (comnested) |
| 568 | nesting++; |
| 569 | else |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | /* Assume a defun-start point is outside of strings. */ |
| 574 | if (code == Sopen |
| 575 | && (from == stop |
| 576 | || (temp_byte = dec_bytepos (from_byte), |
| 577 | FETCH_CHAR (temp_byte) == '\n'))) |
| 578 | { |
| 579 | defun_start = from; |
| 580 | defun_start_byte = from_byte; |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | if (comstart_pos == 0) |
| 586 | { |
| 587 | from = comment_end; |
| 588 | from_byte = comment_end_byte; |
| 589 | UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1); |
| 590 | } |
| 591 | /* If the earliest comment starter |
| 592 | is followed by uniform paired string quotes or none, |
| 593 | we know it can't be inside a string |
| 594 | since if it were then the comment ender would be inside one. |
| 595 | So it does start a comment. Skip back to it. */ |
| 596 | else if (!comnested && comstart_parity == 0 && !string_lossage) |
| 597 | { |
| 598 | from = comstart_pos; |
| 599 | from_byte = comstart_byte; |
| 600 | /* Globals are correct now. */ |
| 601 | } |
| 602 | else |
| 603 | { |
| 604 | /* We had two kinds of string delimiters mixed up |
| 605 | together. Decode this going forwards. |
| 606 | Scan fwd from the previous comment ender |
| 607 | to the one in question; this records where we |
| 608 | last passed a comment starter. */ |
| 609 | struct lisp_parse_state state; |
| 610 | /* If we did not already find the defun start, find it now. */ |
| 611 | if (defun_start == 0) |
| 612 | { |
| 613 | defun_start = find_defun_start (comment_end, comment_end_byte); |
| 614 | defun_start_byte = find_start_value_byte; |
| 615 | } |
| 616 | scan_sexps_forward (&state, |
| 617 | defun_start, defun_start_byte, |
| 618 | comment_end - 1, -10000, 0, Qnil, 0); |
| 619 | if (state.incomment) |
| 620 | { |
| 621 | /* scan_sexps_forward changed the direction of search in |
| 622 | global variables, so we need to update it completely. */ |
| 623 | |
| 624 | from = state.comstr_start; |
| 625 | } |
| 626 | else |
| 627 | { |
| 628 | from = comment_end; |
| 629 | } |
| 630 | from_byte = CHAR_TO_BYTE (from); |
| 631 | UPDATE_SYNTAX_TABLE_FORWARD (from - 1); |
| 632 | } |
| 633 | |
| 634 | done: |
| 635 | *charpos_ptr = from; |
| 636 | *bytepos_ptr = from_byte; |
| 637 | |
| 638 | return from; |
| 639 | } |
| 640 | \f |
| 641 | DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0, |
| 642 | "Return t if OBJECT is a syntax table.\n\ |
| 643 | Currently, any char-table counts as a syntax table.") |
| 644 | (object) |
| 645 | Lisp_Object object; |
| 646 | { |
| 647 | if (CHAR_TABLE_P (object) |
| 648 | && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table)) |
| 649 | return Qt; |
| 650 | return Qnil; |
| 651 | } |
| 652 | |
| 653 | static void |
| 654 | check_syntax_table (obj) |
| 655 | Lisp_Object obj; |
| 656 | { |
| 657 | if (!(CHAR_TABLE_P (obj) |
| 658 | && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table))) |
| 659 | wrong_type_argument (Qsyntax_table_p, obj); |
| 660 | } |
| 661 | |
| 662 | DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0, |
| 663 | "Return the current syntax table.\n\ |
| 664 | This is the one specified by the current buffer.") |
| 665 | () |
| 666 | { |
| 667 | return current_buffer->syntax_table; |
| 668 | } |
| 669 | |
| 670 | DEFUN ("standard-syntax-table", Fstandard_syntax_table, |
| 671 | Sstandard_syntax_table, 0, 0, 0, |
| 672 | "Return the standard syntax table.\n\ |
| 673 | This is the one used for new buffers.") |
| 674 | () |
| 675 | { |
| 676 | return Vstandard_syntax_table; |
| 677 | } |
| 678 | |
| 679 | DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0, |
| 680 | "Construct a new syntax table and return it.\n\ |
| 681 | It is a copy of the TABLE, which defaults to the standard syntax table.") |
| 682 | (table) |
| 683 | Lisp_Object table; |
| 684 | { |
| 685 | Lisp_Object copy; |
| 686 | |
| 687 | if (!NILP (table)) |
| 688 | check_syntax_table (table); |
| 689 | else |
| 690 | table = Vstandard_syntax_table; |
| 691 | |
| 692 | copy = Fcopy_sequence (table); |
| 693 | |
| 694 | /* Only the standard syntax table should have a default element. |
| 695 | Other syntax tables should inherit from parents instead. */ |
| 696 | XCHAR_TABLE (copy)->defalt = Qnil; |
| 697 | |
| 698 | /* Copied syntax tables should all have parents. |
| 699 | If we copied one with no parent, such as the standard syntax table, |
| 700 | use the standard syntax table as the copy's parent. */ |
| 701 | if (NILP (XCHAR_TABLE (copy)->parent)) |
| 702 | Fset_char_table_parent (copy, Vstandard_syntax_table); |
| 703 | return copy; |
| 704 | } |
| 705 | |
| 706 | DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0, |
| 707 | "Select a new syntax table for the current buffer.\n\ |
| 708 | One argument, a syntax table.") |
| 709 | (table) |
| 710 | Lisp_Object table; |
| 711 | { |
| 712 | check_syntax_table (table); |
| 713 | current_buffer->syntax_table = table; |
| 714 | /* Indicate that this buffer now has a specified syntax table. */ |
| 715 | current_buffer->local_var_flags |
| 716 | |= XFASTINT (buffer_local_flags.syntax_table); |
| 717 | return table; |
| 718 | } |
| 719 | \f |
| 720 | /* Convert a letter which signifies a syntax code |
| 721 | into the code it signifies. |
| 722 | This is used by modify-syntax-entry, and other things. */ |
| 723 | |
| 724 | unsigned char syntax_spec_code[0400] = |
| 725 | { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, |
| 726 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, |
| 727 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, |
| 728 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, |
| 729 | (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377, |
| 730 | (char) Smath, 0377, 0377, (char) Squote, |
| 731 | (char) Sopen, (char) Sclose, 0377, 0377, |
| 732 | 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote, |
| 733 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, |
| 734 | 0377, 0377, 0377, 0377, |
| 735 | (char) Scomment, 0377, (char) Sendcomment, 0377, |
| 736 | (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */ |
| 737 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, |
| 738 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword, |
| 739 | 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol, |
| 740 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */ |
| 741 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, |
| 742 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword, |
| 743 | 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377 |
| 744 | }; |
| 745 | |
| 746 | /* Indexed by syntax code, give the letter that describes it. */ |
| 747 | |
| 748 | char syntax_code_spec[16] = |
| 749 | { |
| 750 | ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@', |
| 751 | '!', '|' |
| 752 | }; |
| 753 | |
| 754 | /* Indexed by syntax code, give the object (cons of syntax code and |
| 755 | nil) to be stored in syntax table. Since these objects can be |
| 756 | shared among syntax tables, we generate them in advance. By |
| 757 | sharing objects, the function `describe-syntax' can give a more |
| 758 | compact listing. */ |
| 759 | static Lisp_Object Vsyntax_code_object; |
| 760 | |
| 761 | \f |
| 762 | /* Look up the value for CHARACTER in syntax table TABLE's parent |
| 763 | and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil |
| 764 | for CHARACTER. It's actually used only when not compiled with GCC. */ |
| 765 | |
| 766 | Lisp_Object |
| 767 | syntax_parent_lookup (table, character) |
| 768 | Lisp_Object table; |
| 769 | int character; |
| 770 | { |
| 771 | Lisp_Object value; |
| 772 | |
| 773 | while (1) |
| 774 | { |
| 775 | table = XCHAR_TABLE (table)->parent; |
| 776 | if (NILP (table)) |
| 777 | return Qnil; |
| 778 | |
| 779 | value = XCHAR_TABLE (table)->contents[character]; |
| 780 | if (!NILP (value)) |
| 781 | return value; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0, |
| 786 | "Return the syntax code of CHARACTER, described by a character.\n\ |
| 787 | For example, if CHARACTER is a word constituent,\n\ |
| 788 | the character `w' is returned.\n\ |
| 789 | The characters that correspond to various syntax codes\n\ |
| 790 | are listed in the documentation of `modify-syntax-entry'.") |
| 791 | (character) |
| 792 | Lisp_Object character; |
| 793 | { |
| 794 | int char_int; |
| 795 | gl_state.current_syntax_table = current_buffer->syntax_table; |
| 796 | |
| 797 | gl_state.use_global = 0; |
| 798 | CHECK_NUMBER (character, 0); |
| 799 | char_int = XINT (character); |
| 800 | return make_number (syntax_code_spec[(int) SYNTAX (char_int)]); |
| 801 | } |
| 802 | |
| 803 | DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0, |
| 804 | "Return the matching parenthesis of CHARACTER, or nil if none.") |
| 805 | (character) |
| 806 | Lisp_Object character; |
| 807 | { |
| 808 | int char_int, code; |
| 809 | gl_state.current_syntax_table = current_buffer->syntax_table; |
| 810 | gl_state.use_global = 0; |
| 811 | CHECK_NUMBER (character, 0); |
| 812 | char_int = XINT (character); |
| 813 | code = SYNTAX (char_int); |
| 814 | if (code == Sopen || code == Sclose) |
| 815 | return SYNTAX_MATCH (char_int); |
| 816 | return Qnil; |
| 817 | } |
| 818 | |
| 819 | /* This comment supplies the doc string for modify-syntax-entry, |
| 820 | for make-docfile to see. We cannot put this in the real DEFUN |
| 821 | due to limits in the Unix cpp. |
| 822 | |
| 823 | DEFUN ("modify-syntax-entry", foo, bar, 2, 3, 0, |
| 824 | "Set syntax for character CHAR according to string S.\n\ |
| 825 | The syntax is changed only for table TABLE, which defaults to\n\ |
| 826 | the current buffer's syntax table.\n\ |
| 827 | The first character of S should be one of the following:\n\ |
| 828 | Space or - whitespace syntax. w word constituent.\n\ |
| 829 | _ symbol constituent. . punctuation.\n\ |
| 830 | ( open-parenthesis. ) close-parenthesis.\n\ |
| 831 | \" string quote. \\ escape.\n\ |
| 832 | $ paired delimiter. ' expression quote or prefix operator.\n\ |
| 833 | < comment starter. > comment ender.\n\ |
| 834 | / character-quote. @ inherit from `standard-syntax-table'.\n\ |
| 835 | \n\ |
| 836 | Only single-character comment start and end sequences are represented thus.\n\ |
| 837 | Two-character sequences are represented as described below.\n\ |
| 838 | The second character of S is the matching parenthesis,\n\ |
| 839 | used only if the first character is `(' or `)'.\n\ |
| 840 | Any additional characters are flags.\n\ |
| 841 | Defined flags are the characters 1, 2, 3, 4, b, p, and n.\n\ |
| 842 | 1 means CHAR is the start of a two-char comment start sequence.\n\ |
| 843 | 2 means CHAR is the second character of such a sequence.\n\ |
| 844 | 3 means CHAR is the start of a two-char comment end sequence.\n\ |
| 845 | 4 means CHAR is the second character of such a sequence.\n\ |
| 846 | \n\ |
| 847 | There can be up to two orthogonal comment sequences. This is to support\n\ |
| 848 | language modes such as C++. By default, all comment sequences are of style\n\ |
| 849 | a, but you can set the comment sequence style to b (on the second character\n\ |
| 850 | of a comment-start, or the first character of a comment-end sequence) using\n\ |
| 851 | this flag:\n\ |
| 852 | b means CHAR is part of comment sequence b.\n\ |
| 853 | n means CHAR is part of a nestable comment sequence.\n\ |
| 854 | \n\ |
| 855 | p means CHAR is a prefix character for `backward-prefix-chars';\n\ |
| 856 | such characters are treated as whitespace when they occur\n\ |
| 857 | between expressions.") |
| 858 | (char, s, table) |
| 859 | */ |
| 860 | |
| 861 | DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3, |
| 862 | /* I really don't know why this is interactive |
| 863 | help-form should at least be made useful whilst reading the second arg |
| 864 | */ |
| 865 | "cSet syntax for character: \nsSet syntax for %s to: ", |
| 866 | 0 /* See immediately above */) |
| 867 | (c, newentry, syntax_table) |
| 868 | Lisp_Object c, newentry, syntax_table; |
| 869 | { |
| 870 | register unsigned char *p; |
| 871 | register enum syntaxcode code; |
| 872 | int val; |
| 873 | Lisp_Object match; |
| 874 | |
| 875 | CHECK_NUMBER (c, 0); |
| 876 | CHECK_STRING (newentry, 1); |
| 877 | |
| 878 | if (NILP (syntax_table)) |
| 879 | syntax_table = current_buffer->syntax_table; |
| 880 | else |
| 881 | check_syntax_table (syntax_table); |
| 882 | |
| 883 | p = XSTRING (newentry)->data; |
| 884 | code = (enum syntaxcode) syntax_spec_code[*p++]; |
| 885 | if (((int) code & 0377) == 0377) |
| 886 | error ("invalid syntax description letter: %c", p[-1]); |
| 887 | |
| 888 | if (code == Sinherit) |
| 889 | { |
| 890 | SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), Qnil); |
| 891 | return Qnil; |
| 892 | } |
| 893 | |
| 894 | if (*p) |
| 895 | { |
| 896 | int len; |
| 897 | int character = (STRING_CHAR_AND_LENGTH |
| 898 | (p, STRING_BYTES (XSTRING (newentry)) - 1, len)); |
| 899 | XSETINT (match, character); |
| 900 | if (XFASTINT (match) == ' ') |
| 901 | match = Qnil; |
| 902 | p += len; |
| 903 | } |
| 904 | else |
| 905 | match = Qnil; |
| 906 | |
| 907 | val = (int) code; |
| 908 | while (*p) |
| 909 | switch (*p++) |
| 910 | { |
| 911 | case '1': |
| 912 | val |= 1 << 16; |
| 913 | break; |
| 914 | |
| 915 | case '2': |
| 916 | val |= 1 << 17; |
| 917 | break; |
| 918 | |
| 919 | case '3': |
| 920 | val |= 1 << 18; |
| 921 | break; |
| 922 | |
| 923 | case '4': |
| 924 | val |= 1 << 19; |
| 925 | break; |
| 926 | |
| 927 | case 'p': |
| 928 | val |= 1 << 20; |
| 929 | break; |
| 930 | |
| 931 | case 'b': |
| 932 | val |= 1 << 21; |
| 933 | break; |
| 934 | |
| 935 | case 'n': |
| 936 | val |= 1 << 22; |
| 937 | break; |
| 938 | } |
| 939 | |
| 940 | if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match)) |
| 941 | newentry = XVECTOR (Vsyntax_code_object)->contents[val]; |
| 942 | else |
| 943 | /* Since we can't use a shared object, let's make a new one. */ |
| 944 | newentry = Fcons (make_number (val), match); |
| 945 | |
| 946 | SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry); |
| 947 | |
| 948 | return Qnil; |
| 949 | } |
| 950 | \f |
| 951 | /* Dump syntax table to buffer in human-readable format */ |
| 952 | |
| 953 | static void |
| 954 | describe_syntax (value) |
| 955 | Lisp_Object value; |
| 956 | { |
| 957 | register enum syntaxcode code; |
| 958 | char desc, start1, start2, end1, end2, prefix, comstyle; |
| 959 | char str[2]; |
| 960 | Lisp_Object first, match_lisp; |
| 961 | |
| 962 | Findent_to (make_number (16), make_number (1)); |
| 963 | |
| 964 | if (NILP (value)) |
| 965 | { |
| 966 | insert_string ("default\n"); |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | if (CHAR_TABLE_P (value)) |
| 971 | { |
| 972 | insert_string ("deeper char-table ...\n"); |
| 973 | return; |
| 974 | } |
| 975 | |
| 976 | if (!CONSP (value)) |
| 977 | { |
| 978 | insert_string ("invalid\n"); |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | first = XCAR (value); |
| 983 | match_lisp = XCDR (value); |
| 984 | |
| 985 | if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp))) |
| 986 | { |
| 987 | insert_string ("invalid\n"); |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | code = (enum syntaxcode) (XINT (first) & 0377); |
| 992 | start1 = (XINT (first) >> 16) & 1; |
| 993 | start2 = (XINT (first) >> 17) & 1; |
| 994 | end1 = (XINT (first) >> 18) & 1; |
| 995 | end2 = (XINT (first) >> 19) & 1; |
| 996 | prefix = (XINT (first) >> 20) & 1; |
| 997 | comstyle = (XINT (first) >> 21) & 1; |
| 998 | |
| 999 | if ((int) code < 0 || (int) code >= (int) Smax) |
| 1000 | { |
| 1001 | insert_string ("invalid"); |
| 1002 | return; |
| 1003 | } |
| 1004 | desc = syntax_code_spec[(int) code]; |
| 1005 | |
| 1006 | str[0] = desc, str[1] = 0; |
| 1007 | insert (str, 1); |
| 1008 | |
| 1009 | if (NILP (match_lisp)) |
| 1010 | insert (" ", 1); |
| 1011 | else |
| 1012 | insert_char (XINT (match_lisp)); |
| 1013 | |
| 1014 | if (start1) |
| 1015 | insert ("1", 1); |
| 1016 | if (start2) |
| 1017 | insert ("2", 1); |
| 1018 | |
| 1019 | if (end1) |
| 1020 | insert ("3", 1); |
| 1021 | if (end2) |
| 1022 | insert ("4", 1); |
| 1023 | |
| 1024 | if (prefix) |
| 1025 | insert ("p", 1); |
| 1026 | if (comstyle) |
| 1027 | insert ("b", 1); |
| 1028 | |
| 1029 | insert_string ("\twhich means: "); |
| 1030 | |
| 1031 | switch (SWITCH_ENUM_CAST (code)) |
| 1032 | { |
| 1033 | case Swhitespace: |
| 1034 | insert_string ("whitespace"); break; |
| 1035 | case Spunct: |
| 1036 | insert_string ("punctuation"); break; |
| 1037 | case Sword: |
| 1038 | insert_string ("word"); break; |
| 1039 | case Ssymbol: |
| 1040 | insert_string ("symbol"); break; |
| 1041 | case Sopen: |
| 1042 | insert_string ("open"); break; |
| 1043 | case Sclose: |
| 1044 | insert_string ("close"); break; |
| 1045 | case Squote: |
| 1046 | insert_string ("quote"); break; |
| 1047 | case Sstring: |
| 1048 | insert_string ("string"); break; |
| 1049 | case Smath: |
| 1050 | insert_string ("math"); break; |
| 1051 | case Sescape: |
| 1052 | insert_string ("escape"); break; |
| 1053 | case Scharquote: |
| 1054 | insert_string ("charquote"); break; |
| 1055 | case Scomment: |
| 1056 | insert_string ("comment"); break; |
| 1057 | case Sendcomment: |
| 1058 | insert_string ("endcomment"); break; |
| 1059 | default: |
| 1060 | insert_string ("invalid"); |
| 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | if (!NILP (match_lisp)) |
| 1065 | { |
| 1066 | insert_string (", matches "); |
| 1067 | insert_char (XINT (match_lisp)); |
| 1068 | } |
| 1069 | |
| 1070 | if (start1) |
| 1071 | insert_string (",\n\t is the first character of a comment-start sequence"); |
| 1072 | if (start2) |
| 1073 | insert_string (",\n\t is the second character of a comment-start sequence"); |
| 1074 | |
| 1075 | if (end1) |
| 1076 | insert_string (",\n\t is the first character of a comment-end sequence"); |
| 1077 | if (end2) |
| 1078 | insert_string (",\n\t is the second character of a comment-end sequence"); |
| 1079 | if (comstyle) |
| 1080 | insert_string (" (comment style b)"); |
| 1081 | |
| 1082 | if (prefix) |
| 1083 | insert_string (",\n\t is a prefix character for `backward-prefix-chars'"); |
| 1084 | |
| 1085 | insert_string ("\n"); |
| 1086 | } |
| 1087 | |
| 1088 | static Lisp_Object |
| 1089 | describe_syntax_1 (vector) |
| 1090 | Lisp_Object vector; |
| 1091 | { |
| 1092 | struct buffer *old = current_buffer; |
| 1093 | set_buffer_internal (XBUFFER (Vstandard_output)); |
| 1094 | describe_vector (vector, Qnil, describe_syntax, 0, Qnil, Qnil, (int *) 0, 0); |
| 1095 | while (! NILP (XCHAR_TABLE (vector)->parent)) |
| 1096 | { |
| 1097 | vector = XCHAR_TABLE (vector)->parent; |
| 1098 | insert_string ("\nThe parent syntax table is:"); |
| 1099 | describe_vector (vector, Qnil, describe_syntax, 0, Qnil, Qnil, |
| 1100 | (int *) 0, 0); |
| 1101 | } |
| 1102 | |
| 1103 | call0 (intern ("help-mode")); |
| 1104 | set_buffer_internal (old); |
| 1105 | return Qnil; |
| 1106 | } |
| 1107 | |
| 1108 | DEFUN ("describe-syntax", Fdescribe_syntax, Sdescribe_syntax, 0, 0, "", |
| 1109 | "Describe the syntax specifications in the syntax table.\n\ |
| 1110 | The descriptions are inserted in a buffer, which is then displayed.") |
| 1111 | () |
| 1112 | { |
| 1113 | internal_with_output_to_temp_buffer |
| 1114 | ("*Help*", describe_syntax_1, current_buffer->syntax_table); |
| 1115 | |
| 1116 | return Qnil; |
| 1117 | } |
| 1118 | \f |
| 1119 | int parse_sexp_ignore_comments; |
| 1120 | |
| 1121 | /* Return the position across COUNT words from FROM. |
| 1122 | If that many words cannot be found before the end of the buffer, return 0. |
| 1123 | COUNT negative means scan backward and stop at word beginning. */ |
| 1124 | |
| 1125 | int |
| 1126 | scan_words (from, count) |
| 1127 | register int from, count; |
| 1128 | { |
| 1129 | register int beg = BEGV; |
| 1130 | register int end = ZV; |
| 1131 | register int from_byte = CHAR_TO_BYTE (from); |
| 1132 | register enum syntaxcode code; |
| 1133 | int ch0, ch1; |
| 1134 | |
| 1135 | immediate_quit = 1; |
| 1136 | QUIT; |
| 1137 | |
| 1138 | SETUP_SYNTAX_TABLE (from, count); |
| 1139 | |
| 1140 | while (count > 0) |
| 1141 | { |
| 1142 | while (1) |
| 1143 | { |
| 1144 | if (from == end) |
| 1145 | { |
| 1146 | immediate_quit = 0; |
| 1147 | return 0; |
| 1148 | } |
| 1149 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 1150 | ch0 = FETCH_CHAR (from_byte); |
| 1151 | code = SYNTAX (ch0); |
| 1152 | INC_BOTH (from, from_byte); |
| 1153 | if (words_include_escapes |
| 1154 | && (code == Sescape || code == Scharquote)) |
| 1155 | break; |
| 1156 | if (code == Sword) |
| 1157 | break; |
| 1158 | } |
| 1159 | /* Now CH0 is a character which begins a word and FROM is the |
| 1160 | position of the next character. */ |
| 1161 | while (1) |
| 1162 | { |
| 1163 | if (from == end) break; |
| 1164 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 1165 | ch1 = FETCH_CHAR (from_byte); |
| 1166 | code = SYNTAX (ch1); |
| 1167 | if (!(words_include_escapes |
| 1168 | && (code == Sescape || code == Scharquote))) |
| 1169 | if (code != Sword || WORD_BOUNDARY_P (ch0, ch1)) |
| 1170 | break; |
| 1171 | INC_BOTH (from, from_byte); |
| 1172 | ch0 = ch1; |
| 1173 | } |
| 1174 | count--; |
| 1175 | } |
| 1176 | while (count < 0) |
| 1177 | { |
| 1178 | while (1) |
| 1179 | { |
| 1180 | if (from == beg) |
| 1181 | { |
| 1182 | immediate_quit = 0; |
| 1183 | return 0; |
| 1184 | } |
| 1185 | DEC_BOTH (from, from_byte); |
| 1186 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
| 1187 | ch1 = FETCH_CHAR (from_byte); |
| 1188 | code = SYNTAX (ch1); |
| 1189 | if (words_include_escapes |
| 1190 | && (code == Sescape || code == Scharquote)) |
| 1191 | break; |
| 1192 | if (code == Sword) |
| 1193 | break; |
| 1194 | } |
| 1195 | /* Now CH1 is a character which ends a word and FROM is the |
| 1196 | position of it. */ |
| 1197 | while (1) |
| 1198 | { |
| 1199 | int temp_byte; |
| 1200 | |
| 1201 | if (from == beg) |
| 1202 | break; |
| 1203 | temp_byte = dec_bytepos (from_byte); |
| 1204 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
| 1205 | ch0 = FETCH_CHAR (temp_byte); |
| 1206 | code = SYNTAX (ch0); |
| 1207 | if (!(words_include_escapes |
| 1208 | && (code == Sescape || code == Scharquote))) |
| 1209 | if (code != Sword || WORD_BOUNDARY_P (ch0, ch1)) |
| 1210 | break; |
| 1211 | DEC_BOTH (from, from_byte); |
| 1212 | ch1 = ch0; |
| 1213 | } |
| 1214 | count++; |
| 1215 | } |
| 1216 | |
| 1217 | immediate_quit = 0; |
| 1218 | |
| 1219 | return from; |
| 1220 | } |
| 1221 | |
| 1222 | DEFUN ("forward-word", Fforward_word, Sforward_word, 1, 1, "p", |
| 1223 | "Move point forward ARG words (backward if ARG is negative).\n\ |
| 1224 | Normally returns t.\n\ |
| 1225 | If an edge of the buffer is reached, point is left there\n\ |
| 1226 | and nil is returned.") |
| 1227 | (count) |
| 1228 | Lisp_Object count; |
| 1229 | { |
| 1230 | int val, prompt_end; |
| 1231 | CHECK_NUMBER (count, 0); |
| 1232 | |
| 1233 | if (!(val = scan_words (PT, XINT (count)))) |
| 1234 | { |
| 1235 | SET_PT (XINT (count) > 0 ? ZV : BEGV); |
| 1236 | return Qnil; |
| 1237 | } |
| 1238 | |
| 1239 | /* If in a mini-buffer and moving backwards, stop at the end of the |
| 1240 | prompt. This prevents accidentially moving into the read-only |
| 1241 | prompt. */ |
| 1242 | if (INTEGERP (current_buffer->prompt_end_charpos) |
| 1243 | && (prompt_end = XINT (current_buffer->prompt_end_charpos), |
| 1244 | ((PT > prompt_end && val < prompt_end) |
| 1245 | || (PT < prompt_end && val > prompt_end)))) |
| 1246 | val = prompt_end; |
| 1247 | |
| 1248 | SET_PT (val); |
| 1249 | return Qt; |
| 1250 | } |
| 1251 | \f |
| 1252 | Lisp_Object skip_chars (); |
| 1253 | |
| 1254 | DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0, |
| 1255 | "Move point forward, stopping before a char not in STRING, or at pos LIM.\n\ |
| 1256 | STRING is like the inside of a `[...]' in a regular expression\n\ |
| 1257 | except that `]' is never special and `\\' quotes `^', `-' or `\\'\n\ |
| 1258 | (but not as the end of a range; quoting is never needed there).\n\ |
| 1259 | Thus, with arg \"a-zA-Z\", this skips letters stopping before first nonletter.\n\ |
| 1260 | With arg \"^a-zA-Z\", skips nonletters stopping before first letter.\n\ |
| 1261 | Returns the distance traveled, either zero or positive.") |
| 1262 | (string, lim) |
| 1263 | Lisp_Object string, lim; |
| 1264 | { |
| 1265 | return skip_chars (1, 0, string, lim); |
| 1266 | } |
| 1267 | |
| 1268 | DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0, |
| 1269 | "Move point backward, stopping after a char not in STRING, or at pos LIM.\n\ |
| 1270 | See `skip-chars-forward' for details.\n\ |
| 1271 | Returns the distance traveled, either zero or negative.") |
| 1272 | (string, lim) |
| 1273 | Lisp_Object string, lim; |
| 1274 | { |
| 1275 | return skip_chars (0, 0, string, lim); |
| 1276 | } |
| 1277 | |
| 1278 | DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0, |
| 1279 | "Move point forward across chars in specified syntax classes.\n\ |
| 1280 | SYNTAX is a string of syntax code characters.\n\ |
| 1281 | Stop before a char whose syntax is not in SYNTAX, or at position LIM.\n\ |
| 1282 | If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\ |
| 1283 | This function returns the distance traveled, either zero or positive.") |
| 1284 | (syntax, lim) |
| 1285 | Lisp_Object syntax, lim; |
| 1286 | { |
| 1287 | return skip_chars (1, 1, syntax, lim); |
| 1288 | } |
| 1289 | |
| 1290 | DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0, |
| 1291 | "Move point backward across chars in specified syntax classes.\n\ |
| 1292 | SYNTAX is a string of syntax code characters.\n\ |
| 1293 | Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.\n\ |
| 1294 | If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\ |
| 1295 | This function returns the distance traveled, either zero or negative.") |
| 1296 | (syntax, lim) |
| 1297 | Lisp_Object syntax, lim; |
| 1298 | { |
| 1299 | return skip_chars (0, 1, syntax, lim); |
| 1300 | } |
| 1301 | |
| 1302 | static Lisp_Object |
| 1303 | skip_chars (forwardp, syntaxp, string, lim) |
| 1304 | int forwardp, syntaxp; |
| 1305 | Lisp_Object string, lim; |
| 1306 | { |
| 1307 | register unsigned int c; |
| 1308 | register int ch; |
| 1309 | unsigned char fastmap[0400]; |
| 1310 | /* If SYNTAXP is 0, STRING may contain multi-byte form of characters |
| 1311 | of which codes don't fit in FASTMAP. In that case, we set the |
| 1312 | first byte of multibyte form (i.e. base leading-code) in FASTMAP |
| 1313 | and set the actual ranges of characters in CHAR_RANGES. In the |
| 1314 | form "X-Y" of STRING, both X and Y must belong to the same |
| 1315 | character set because a range striding across character sets is |
| 1316 | meaningless. */ |
| 1317 | int *char_ranges; |
| 1318 | int n_char_ranges = 0; |
| 1319 | int negate = 0; |
| 1320 | register int i, i_byte; |
| 1321 | int multibyte = !NILP (current_buffer->enable_multibyte_characters); |
| 1322 | int string_multibyte; |
| 1323 | int size_byte; |
| 1324 | |
| 1325 | CHECK_STRING (string, 0); |
| 1326 | char_ranges = (int *) alloca (XSTRING (string)->size * (sizeof (int)) * 2); |
| 1327 | string_multibyte = STRING_MULTIBYTE (string); |
| 1328 | size_byte = STRING_BYTES (XSTRING (string)); |
| 1329 | |
| 1330 | if (NILP (lim)) |
| 1331 | XSETINT (lim, forwardp ? ZV : BEGV); |
| 1332 | else |
| 1333 | CHECK_NUMBER_COERCE_MARKER (lim, 0); |
| 1334 | |
| 1335 | /* In any case, don't allow scan outside bounds of buffer. */ |
| 1336 | if (XINT (lim) > ZV) |
| 1337 | XSETFASTINT (lim, ZV); |
| 1338 | if (XINT (lim) < BEGV) |
| 1339 | XSETFASTINT (lim, BEGV); |
| 1340 | |
| 1341 | bzero (fastmap, sizeof fastmap); |
| 1342 | |
| 1343 | i = 0, i_byte = 0; |
| 1344 | |
| 1345 | if (i_byte < size_byte |
| 1346 | && XSTRING (string)->data[0] == '^') |
| 1347 | { |
| 1348 | negate = 1; i++, i_byte++; |
| 1349 | } |
| 1350 | |
| 1351 | /* Find the characters specified and set their elements of fastmap. |
| 1352 | If syntaxp, each character counts as itself. |
| 1353 | Otherwise, handle backslashes and ranges specially. */ |
| 1354 | |
| 1355 | while (i_byte < size_byte) |
| 1356 | { |
| 1357 | int c_leading_code; |
| 1358 | |
| 1359 | if (string_multibyte) |
| 1360 | { |
| 1361 | c_leading_code = XSTRING (string)->data[i_byte]; |
| 1362 | FETCH_STRING_CHAR_ADVANCE (c, string, i, i_byte); |
| 1363 | } |
| 1364 | else |
| 1365 | c = c_leading_code = XSTRING (string)->data[i_byte++]; |
| 1366 | |
| 1367 | /* Convert multibyteness between what the string has |
| 1368 | and what the buffer has. */ |
| 1369 | if (multibyte) |
| 1370 | c = unibyte_char_to_multibyte (c); |
| 1371 | else |
| 1372 | c &= 0377; |
| 1373 | |
| 1374 | if (syntaxp) |
| 1375 | fastmap[syntax_spec_code[c & 0377]] = 1; |
| 1376 | else |
| 1377 | { |
| 1378 | if (c == '\\') |
| 1379 | { |
| 1380 | if (i_byte == size_byte) |
| 1381 | break; |
| 1382 | |
| 1383 | if (string_multibyte) |
| 1384 | { |
| 1385 | c_leading_code = XSTRING (string)->data[i_byte]; |
| 1386 | FETCH_STRING_CHAR_ADVANCE (c, string, i, i_byte); |
| 1387 | } |
| 1388 | else |
| 1389 | c = c_leading_code = XSTRING (string)->data[i_byte++]; |
| 1390 | } |
| 1391 | if (i_byte < size_byte |
| 1392 | && XSTRING (string)->data[i_byte] == '-') |
| 1393 | { |
| 1394 | unsigned int c2, c2_leading_code; |
| 1395 | |
| 1396 | /* Skip over the dash. */ |
| 1397 | i++, i_byte++; |
| 1398 | |
| 1399 | if (i_byte == size_byte) |
| 1400 | break; |
| 1401 | |
| 1402 | /* Get the end of the range. */ |
| 1403 | if (string_multibyte) |
| 1404 | { |
| 1405 | c2_leading_code = XSTRING (string)->data[i_byte]; |
| 1406 | FETCH_STRING_CHAR_ADVANCE (c2, string, i, i_byte); |
| 1407 | } |
| 1408 | else |
| 1409 | c2 = XSTRING (string)->data[i_byte++]; |
| 1410 | |
| 1411 | if (SINGLE_BYTE_CHAR_P (c)) |
| 1412 | { |
| 1413 | if (! SINGLE_BYTE_CHAR_P (c2)) |
| 1414 | error ("Invalid charcter range: %s", |
| 1415 | XSTRING (string)->data); |
| 1416 | while (c <= c2) |
| 1417 | { |
| 1418 | fastmap[c] = 1; |
| 1419 | c++; |
| 1420 | } |
| 1421 | } |
| 1422 | else |
| 1423 | { |
| 1424 | if (c_leading_code != c2_leading_code) |
| 1425 | error ("Invalid charcter range: %s", |
| 1426 | XSTRING (string)->data); |
| 1427 | fastmap[c_leading_code] = 1; |
| 1428 | if (c <= c2) |
| 1429 | { |
| 1430 | char_ranges[n_char_ranges++] = c; |
| 1431 | char_ranges[n_char_ranges++] = c2; |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | else |
| 1436 | { |
| 1437 | fastmap[c_leading_code] = 1; |
| 1438 | if (!SINGLE_BYTE_CHAR_P (c)) |
| 1439 | { |
| 1440 | char_ranges[n_char_ranges++] = c; |
| 1441 | char_ranges[n_char_ranges++] = c; |
| 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | /* If ^ was the first character, complement the fastmap. In |
| 1448 | addition, as all multibyte characters have possibility of |
| 1449 | matching, set all entries for base leading codes, which is |
| 1450 | harmless even if SYNTAXP is 1. */ |
| 1451 | |
| 1452 | if (negate) |
| 1453 | for (i = 0; i < sizeof fastmap; i++) |
| 1454 | { |
| 1455 | if (!multibyte || !BASE_LEADING_CODE_P (i)) |
| 1456 | fastmap[i] ^= 1; |
| 1457 | else |
| 1458 | fastmap[i] = 1; |
| 1459 | } |
| 1460 | |
| 1461 | { |
| 1462 | int start_point = PT; |
| 1463 | int pos = PT; |
| 1464 | int pos_byte = PT_BYTE; |
| 1465 | |
| 1466 | immediate_quit = 1; |
| 1467 | if (syntaxp) |
| 1468 | { |
| 1469 | SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1); |
| 1470 | if (forwardp) |
| 1471 | { |
| 1472 | if (multibyte) |
| 1473 | { |
| 1474 | if (pos < XINT (lim)) |
| 1475 | while (fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))]) |
| 1476 | { |
| 1477 | /* Since we already checked for multibyteness, |
| 1478 | avoid using INC_BOTH which checks again. */ |
| 1479 | INC_POS (pos_byte); |
| 1480 | pos++; |
| 1481 | if (pos >= XINT (lim)) |
| 1482 | break; |
| 1483 | UPDATE_SYNTAX_TABLE_FORWARD (pos); |
| 1484 | } |
| 1485 | } |
| 1486 | else |
| 1487 | { |
| 1488 | while (pos < XINT (lim) |
| 1489 | && fastmap[(int) SYNTAX (FETCH_BYTE (pos))]) |
| 1490 | { |
| 1491 | pos++; |
| 1492 | UPDATE_SYNTAX_TABLE_FORWARD (pos); |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | else |
| 1497 | { |
| 1498 | if (multibyte) |
| 1499 | { |
| 1500 | while (pos > XINT (lim)) |
| 1501 | { |
| 1502 | int savepos = pos_byte; |
| 1503 | /* Since we already checked for multibyteness, |
| 1504 | avoid using DEC_BOTH which checks again. */ |
| 1505 | pos--; |
| 1506 | DEC_POS (pos_byte); |
| 1507 | UPDATE_SYNTAX_TABLE_BACKWARD (pos); |
| 1508 | if (!fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))]) |
| 1509 | { |
| 1510 | pos++; |
| 1511 | pos_byte = savepos; |
| 1512 | break; |
| 1513 | } |
| 1514 | } |
| 1515 | } |
| 1516 | else |
| 1517 | { |
| 1518 | if (pos > XINT (lim)) |
| 1519 | while (fastmap[(int) SYNTAX (FETCH_BYTE (pos - 1))]) |
| 1520 | { |
| 1521 | pos--; |
| 1522 | if (pos <= XINT (lim)) |
| 1523 | break; |
| 1524 | UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1); |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | else |
| 1530 | { |
| 1531 | if (forwardp) |
| 1532 | { |
| 1533 | if (multibyte) |
| 1534 | while (pos < XINT (lim) && fastmap[(c = FETCH_BYTE (pos_byte))]) |
| 1535 | { |
| 1536 | if (!BASE_LEADING_CODE_P (c)) |
| 1537 | INC_BOTH (pos, pos_byte); |
| 1538 | else if (n_char_ranges) |
| 1539 | { |
| 1540 | /* We much check CHAR_RANGES for a multibyte |
| 1541 | character. */ |
| 1542 | ch = FETCH_MULTIBYTE_CHAR (pos_byte); |
| 1543 | for (i = 0; i < n_char_ranges; i += 2) |
| 1544 | if ((ch >= char_ranges[i] && ch <= char_ranges[i + 1])) |
| 1545 | break; |
| 1546 | if (!(negate ^ (i < n_char_ranges))) |
| 1547 | break; |
| 1548 | |
| 1549 | INC_BOTH (pos, pos_byte); |
| 1550 | } |
| 1551 | else |
| 1552 | { |
| 1553 | if (!negate) break; |
| 1554 | INC_BOTH (pos, pos_byte); |
| 1555 | } |
| 1556 | } |
| 1557 | else |
| 1558 | while (pos < XINT (lim) && fastmap[FETCH_BYTE (pos)]) |
| 1559 | pos++; |
| 1560 | } |
| 1561 | else |
| 1562 | { |
| 1563 | if (multibyte) |
| 1564 | while (pos > XINT (lim)) |
| 1565 | { |
| 1566 | int savepos = pos_byte; |
| 1567 | DEC_BOTH (pos, pos_byte); |
| 1568 | if (fastmap[(c = FETCH_BYTE (pos_byte))]) |
| 1569 | { |
| 1570 | if (!BASE_LEADING_CODE_P (c)) |
| 1571 | ; |
| 1572 | else if (n_char_ranges) |
| 1573 | { |
| 1574 | /* We much check CHAR_RANGES for a multibyte |
| 1575 | character. */ |
| 1576 | ch = FETCH_MULTIBYTE_CHAR (pos_byte); |
| 1577 | for (i = 0; i < n_char_ranges; i += 2) |
| 1578 | if (ch >= char_ranges[i] && ch <= char_ranges[i + 1]) |
| 1579 | break; |
| 1580 | if (!(negate ^ (i < n_char_ranges))) |
| 1581 | { |
| 1582 | pos++; |
| 1583 | pos_byte = savepos; |
| 1584 | break; |
| 1585 | } |
| 1586 | } |
| 1587 | else |
| 1588 | if (!negate) |
| 1589 | { |
| 1590 | pos++; |
| 1591 | pos_byte = savepos; |
| 1592 | break; |
| 1593 | } |
| 1594 | } |
| 1595 | else |
| 1596 | { |
| 1597 | pos++; |
| 1598 | pos_byte = savepos; |
| 1599 | break; |
| 1600 | } |
| 1601 | } |
| 1602 | else |
| 1603 | while (pos > XINT (lim) && fastmap[FETCH_BYTE (pos - 1)]) |
| 1604 | pos--; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | #if 0 /* Not needed now that a position in mid-character |
| 1609 | cannot be specified in Lisp. */ |
| 1610 | if (multibyte |
| 1611 | /* INC_POS or DEC_POS might have moved POS over LIM. */ |
| 1612 | && (forwardp ? (pos > XINT (lim)) : (pos < XINT (lim)))) |
| 1613 | pos = XINT (lim); |
| 1614 | #endif |
| 1615 | |
| 1616 | if (! multibyte) |
| 1617 | pos_byte = pos; |
| 1618 | |
| 1619 | SET_PT_BOTH (pos, pos_byte); |
| 1620 | immediate_quit = 0; |
| 1621 | |
| 1622 | return make_number (PT - start_point); |
| 1623 | } |
| 1624 | } |
| 1625 | \f |
| 1626 | /* Jump over a comment, assuming we are at the beginning of one. |
| 1627 | FROM is the current position. |
| 1628 | FROM_BYTE is the bytepos corresponding to FROM. |
| 1629 | Do not move past STOP (a charpos). |
| 1630 | The comment over which we have to jump is of style STYLE |
| 1631 | (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE). |
| 1632 | NESTING should be positive to indicate the nesting at the beginning |
| 1633 | for nested comments and should be zero or negative else. |
| 1634 | ST_COMMENT_STYLE cannot be nested. |
| 1635 | PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character |
| 1636 | (or 0 If the search cannot start in the middle of a two-character). |
| 1637 | |
| 1638 | If successful, return 1 and store the charpos of the comment's end |
| 1639 | into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR. |
| 1640 | Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the |
| 1641 | corresponding bytepos into *BYTEPOS_PTR and the current nesting |
| 1642 | (as defined for state.incomment) in *INCOMMENT_PTR. |
| 1643 | |
| 1644 | The comment end is the last character of the comment rather than the |
| 1645 | character just after the comment. |
| 1646 | |
| 1647 | Global syntax data is assumed to initially be valid for FROM and |
| 1648 | remains valid for forward search starting at the returned position. */ |
| 1649 | |
| 1650 | static int |
| 1651 | forw_comment (from, from_byte, stop, nesting, style, prev_syntax, |
| 1652 | charpos_ptr, bytepos_ptr, incomment_ptr) |
| 1653 | int from, from_byte, stop; |
| 1654 | int nesting, style, prev_syntax; |
| 1655 | int *charpos_ptr, *bytepos_ptr, *incomment_ptr; |
| 1656 | { |
| 1657 | register int c, c1; |
| 1658 | register enum syntaxcode code; |
| 1659 | register int syntax; |
| 1660 | |
| 1661 | if (nesting <= 0) nesting = -1; |
| 1662 | |
| 1663 | /* Enter the loop in the middle so that we find |
| 1664 | a 2-char comment ender if we start in the middle of it. */ |
| 1665 | syntax = prev_syntax; |
| 1666 | if (syntax != 0) goto forw_incomment; |
| 1667 | |
| 1668 | while (1) |
| 1669 | { |
| 1670 | if (from == stop) |
| 1671 | { |
| 1672 | *incomment_ptr = nesting; |
| 1673 | *charpos_ptr = from; |
| 1674 | *bytepos_ptr = from_byte; |
| 1675 | return 0; |
| 1676 | } |
| 1677 | c = FETCH_CHAR (from_byte); |
| 1678 | syntax = SYNTAX_WITH_FLAGS (c); |
| 1679 | code = syntax & 0xff; |
| 1680 | if (code == Sendcomment |
| 1681 | && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style |
| 1682 | && --nesting <= 0) |
| 1683 | /* we have encountered a comment end of the same style |
| 1684 | as the comment sequence which began this comment |
| 1685 | section */ |
| 1686 | break; |
| 1687 | if (code == Scomment_fence |
| 1688 | && style == ST_COMMENT_STYLE) |
| 1689 | /* we have encountered a comment end of the same style |
| 1690 | as the comment sequence which began this comment |
| 1691 | section. */ |
| 1692 | break; |
| 1693 | if (nesting > 0 |
| 1694 | && code == Scomment |
| 1695 | && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style) |
| 1696 | /* we have encountered a nested comment of the same style |
| 1697 | as the comment sequence which began this comment section */ |
| 1698 | nesting++; |
| 1699 | INC_BOTH (from, from_byte); |
| 1700 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 1701 | |
| 1702 | forw_incomment: |
| 1703 | if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax) |
| 1704 | && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style |
| 1705 | && (c1 = FETCH_CHAR (from_byte), |
| 1706 | SYNTAX_COMEND_SECOND (c1))) |
| 1707 | { |
| 1708 | if (--nesting <= 0) |
| 1709 | /* we have encountered a comment end of the same style |
| 1710 | as the comment sequence which began this comment |
| 1711 | section */ |
| 1712 | break; |
| 1713 | else |
| 1714 | { |
| 1715 | INC_BOTH (from, from_byte); |
| 1716 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 1717 | } |
| 1718 | } |
| 1719 | if (nesting > 0 |
| 1720 | && from < stop |
| 1721 | && SYNTAX_FLAGS_COMSTART_FIRST (syntax) |
| 1722 | && (c1 = FETCH_CHAR (from_byte), |
| 1723 | SYNTAX_COMMENT_STYLE (c1) == style |
| 1724 | && SYNTAX_COMSTART_SECOND (c1))) |
| 1725 | /* we have encountered a nested comment of the same style |
| 1726 | as the comment sequence which began this comment |
| 1727 | section */ |
| 1728 | { |
| 1729 | INC_BOTH (from, from_byte); |
| 1730 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 1731 | nesting++; |
| 1732 | } |
| 1733 | } |
| 1734 | *charpos_ptr = from; |
| 1735 | *bytepos_ptr = from_byte; |
| 1736 | return 1; |
| 1737 | } |
| 1738 | |
| 1739 | DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0, |
| 1740 | "Move forward across up to N comments. If N is negative, move backward.\n\ |
| 1741 | Stop scanning if we find something other than a comment or whitespace.\n\ |
| 1742 | Set point to where scanning stops.\n\ |
| 1743 | If N comments are found as expected, with nothing except whitespace\n\ |
| 1744 | between them, return t; otherwise return nil.") |
| 1745 | (count) |
| 1746 | Lisp_Object count; |
| 1747 | { |
| 1748 | register int from; |
| 1749 | int from_byte; |
| 1750 | register int stop; |
| 1751 | register int c, c1; |
| 1752 | register enum syntaxcode code; |
| 1753 | int comstyle = 0; /* style of comment encountered */ |
| 1754 | int comnested = 0; /* whether the comment is nestable or not */ |
| 1755 | int found; |
| 1756 | int count1; |
| 1757 | int out_charpos, out_bytepos; |
| 1758 | int dummy; |
| 1759 | |
| 1760 | CHECK_NUMBER (count, 0); |
| 1761 | count1 = XINT (count); |
| 1762 | stop = count1 > 0 ? ZV : BEGV; |
| 1763 | |
| 1764 | immediate_quit = 1; |
| 1765 | QUIT; |
| 1766 | |
| 1767 | from = PT; |
| 1768 | from_byte = PT_BYTE; |
| 1769 | |
| 1770 | SETUP_SYNTAX_TABLE (from, count1); |
| 1771 | while (count1 > 0) |
| 1772 | { |
| 1773 | do |
| 1774 | { |
| 1775 | int comstart_first; |
| 1776 | |
| 1777 | if (from == stop) |
| 1778 | { |
| 1779 | SET_PT_BOTH (from, from_byte); |
| 1780 | immediate_quit = 0; |
| 1781 | return Qnil; |
| 1782 | } |
| 1783 | c = FETCH_CHAR (from_byte); |
| 1784 | code = SYNTAX (c); |
| 1785 | comstart_first = SYNTAX_COMSTART_FIRST (c); |
| 1786 | comnested = SYNTAX_COMMENT_NESTED (c); |
| 1787 | INC_BOTH (from, from_byte); |
| 1788 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 1789 | comstyle = 0; |
| 1790 | if (from < stop && comstart_first |
| 1791 | && (c1 = FETCH_CHAR (from_byte), |
| 1792 | SYNTAX_COMSTART_SECOND (c1))) |
| 1793 | { |
| 1794 | /* We have encountered a comment start sequence and we |
| 1795 | are ignoring all text inside comments. We must record |
| 1796 | the comment style this sequence begins so that later, |
| 1797 | only a comment end of the same style actually ends |
| 1798 | the comment section. */ |
| 1799 | code = Scomment; |
| 1800 | comstyle = SYNTAX_COMMENT_STYLE (c1); |
| 1801 | comnested = comnested || SYNTAX_COMMENT_NESTED (c1); |
| 1802 | INC_BOTH (from, from_byte); |
| 1803 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 1804 | } |
| 1805 | } |
| 1806 | while (code == Swhitespace || code == Sendcomment); |
| 1807 | |
| 1808 | if (code == Scomment_fence) |
| 1809 | comstyle = ST_COMMENT_STYLE; |
| 1810 | else if (code != Scomment) |
| 1811 | { |
| 1812 | immediate_quit = 0; |
| 1813 | DEC_BOTH (from, from_byte); |
| 1814 | SET_PT_BOTH (from, from_byte); |
| 1815 | return Qnil; |
| 1816 | } |
| 1817 | /* We're at the start of a comment. */ |
| 1818 | found = forw_comment (from, from_byte, stop, comnested, comstyle, 0, |
| 1819 | &out_charpos, &out_bytepos, &dummy); |
| 1820 | from = out_charpos; from_byte = out_bytepos; |
| 1821 | if (!found) |
| 1822 | { |
| 1823 | immediate_quit = 0; |
| 1824 | SET_PT_BOTH (from, from_byte); |
| 1825 | return Qnil; |
| 1826 | } |
| 1827 | INC_BOTH (from, from_byte); |
| 1828 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 1829 | /* We have skipped one comment. */ |
| 1830 | count1--; |
| 1831 | } |
| 1832 | |
| 1833 | while (count1 < 0) |
| 1834 | { |
| 1835 | while (1) |
| 1836 | { |
| 1837 | int quoted, comstart_second; |
| 1838 | |
| 1839 | if (from <= stop) |
| 1840 | { |
| 1841 | SET_PT_BOTH (BEGV, BEGV_BYTE); |
| 1842 | immediate_quit = 0; |
| 1843 | return Qnil; |
| 1844 | } |
| 1845 | |
| 1846 | DEC_BOTH (from, from_byte); |
| 1847 | /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */ |
| 1848 | quoted = char_quoted (from, from_byte); |
| 1849 | if (quoted) |
| 1850 | { |
| 1851 | DEC_BOTH (from, from_byte); |
| 1852 | goto leave; |
| 1853 | } |
| 1854 | c = FETCH_CHAR (from_byte); |
| 1855 | code = SYNTAX (c); |
| 1856 | comstyle = 0; |
| 1857 | comnested = SYNTAX_COMMENT_NESTED (c); |
| 1858 | if (code == Sendcomment) |
| 1859 | comstyle = SYNTAX_COMMENT_STYLE (c); |
| 1860 | comstart_second = SYNTAX_COMSTART_SECOND (c); |
| 1861 | if (from > stop && SYNTAX_COMEND_SECOND (c) |
| 1862 | && prev_char_comend_first (from, from_byte) |
| 1863 | && !char_quoted (from - 1, dec_bytepos (from_byte))) |
| 1864 | { |
| 1865 | /* We must record the comment style encountered so that |
| 1866 | later, we can match only the proper comment begin |
| 1867 | sequence of the same style. */ |
| 1868 | DEC_BOTH (from, from_byte); |
| 1869 | code = Sendcomment; |
| 1870 | /* Calling char_quoted, above, set up global syntax position |
| 1871 | at the new value of FROM. */ |
| 1872 | c1 = FETCH_CHAR (from_byte); |
| 1873 | comstyle = SYNTAX_COMMENT_STYLE (c1); |
| 1874 | comnested = comnested || SYNTAX_COMMENT_NESTED (c1); |
| 1875 | } |
| 1876 | if (from > stop && comstart_second |
| 1877 | && prev_char_comstart_first (from, from_byte) |
| 1878 | && !char_quoted (from - 1, dec_bytepos (from_byte))) |
| 1879 | { |
| 1880 | code = Scomment; |
| 1881 | DEC_BOTH (from, from_byte); |
| 1882 | } |
| 1883 | |
| 1884 | if (code == Scomment_fence) |
| 1885 | { |
| 1886 | /* Skip until first preceding unquoted comment_fence. */ |
| 1887 | int found = 0, ini = from, ini_byte = from_byte; |
| 1888 | |
| 1889 | while (1) |
| 1890 | { |
| 1891 | DEC_BOTH (from, from_byte); |
| 1892 | if (from == stop) |
| 1893 | break; |
| 1894 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
| 1895 | c = FETCH_CHAR (from_byte); |
| 1896 | if (SYNTAX (c) == Scomment_fence |
| 1897 | && !char_quoted (from, from_byte)) |
| 1898 | { |
| 1899 | found = 1; |
| 1900 | break; |
| 1901 | } |
| 1902 | } |
| 1903 | if (found == 0) |
| 1904 | { |
| 1905 | from = ini; /* Set point to ini + 1. */ |
| 1906 | from_byte = ini_byte; |
| 1907 | goto leave; |
| 1908 | } |
| 1909 | } |
| 1910 | else if (code == Sendcomment) |
| 1911 | { |
| 1912 | found = back_comment (from, from_byte, stop, comnested, comstyle, |
| 1913 | &out_charpos, &out_bytepos); |
| 1914 | if (found != -1) |
| 1915 | from = out_charpos, from_byte = out_bytepos; |
| 1916 | /* We have skipped one comment. */ |
| 1917 | break; |
| 1918 | } |
| 1919 | else if (code != Swhitespace && code != Scomment) |
| 1920 | { |
| 1921 | leave: |
| 1922 | immediate_quit = 0; |
| 1923 | INC_BOTH (from, from_byte); |
| 1924 | SET_PT_BOTH (from, from_byte); |
| 1925 | return Qnil; |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | count1++; |
| 1930 | } |
| 1931 | |
| 1932 | SET_PT_BOTH (from, from_byte); |
| 1933 | immediate_quit = 0; |
| 1934 | return Qt; |
| 1935 | } |
| 1936 | \f |
| 1937 | static Lisp_Object |
| 1938 | scan_lists (from, count, depth, sexpflag) |
| 1939 | register int from; |
| 1940 | int count, depth, sexpflag; |
| 1941 | { |
| 1942 | Lisp_Object val; |
| 1943 | register int stop = count > 0 ? ZV : BEGV; |
| 1944 | register int c, c1; |
| 1945 | int stringterm; |
| 1946 | int quoted; |
| 1947 | int mathexit = 0; |
| 1948 | register enum syntaxcode code, temp_code; |
| 1949 | int min_depth = depth; /* Err out if depth gets less than this. */ |
| 1950 | int comstyle = 0; /* style of comment encountered */ |
| 1951 | int comnested = 0; /* whether the comment is nestable or not */ |
| 1952 | int temp_pos; |
| 1953 | int last_good = from; |
| 1954 | int found; |
| 1955 | int from_byte; |
| 1956 | int out_bytepos, out_charpos; |
| 1957 | int temp, dummy; |
| 1958 | |
| 1959 | if (depth > 0) min_depth = 0; |
| 1960 | |
| 1961 | if (from > ZV) from = ZV; |
| 1962 | if (from < BEGV) from = BEGV; |
| 1963 | |
| 1964 | from_byte = CHAR_TO_BYTE (from); |
| 1965 | |
| 1966 | immediate_quit = 1; |
| 1967 | QUIT; |
| 1968 | |
| 1969 | SETUP_SYNTAX_TABLE (from, count); |
| 1970 | while (count > 0) |
| 1971 | { |
| 1972 | while (from < stop) |
| 1973 | { |
| 1974 | int comstart_first, prefix; |
| 1975 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 1976 | c = FETCH_CHAR (from_byte); |
| 1977 | code = SYNTAX (c); |
| 1978 | comstart_first = SYNTAX_COMSTART_FIRST (c); |
| 1979 | comnested = SYNTAX_COMMENT_NESTED (c); |
| 1980 | prefix = SYNTAX_PREFIX (c); |
| 1981 | if (depth == min_depth) |
| 1982 | last_good = from; |
| 1983 | INC_BOTH (from, from_byte); |
| 1984 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 1985 | if (from < stop && comstart_first |
| 1986 | && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte)) |
| 1987 | && parse_sexp_ignore_comments) |
| 1988 | { |
| 1989 | /* we have encountered a comment start sequence and we |
| 1990 | are ignoring all text inside comments. We must record |
| 1991 | the comment style this sequence begins so that later, |
| 1992 | only a comment end of the same style actually ends |
| 1993 | the comment section */ |
| 1994 | code = Scomment; |
| 1995 | c1 = FETCH_CHAR (from_byte); |
| 1996 | comstyle = SYNTAX_COMMENT_STYLE (c1); |
| 1997 | comnested = comnested || SYNTAX_COMMENT_NESTED (c1); |
| 1998 | INC_BOTH (from, from_byte); |
| 1999 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 2000 | } |
| 2001 | |
| 2002 | if (prefix) |
| 2003 | continue; |
| 2004 | |
| 2005 | switch (SWITCH_ENUM_CAST (code)) |
| 2006 | { |
| 2007 | case Sescape: |
| 2008 | case Scharquote: |
| 2009 | if (from == stop) goto lose; |
| 2010 | INC_BOTH (from, from_byte); |
| 2011 | /* treat following character as a word constituent */ |
| 2012 | case Sword: |
| 2013 | case Ssymbol: |
| 2014 | if (depth || !sexpflag) break; |
| 2015 | /* This word counts as a sexp; return at end of it. */ |
| 2016 | while (from < stop) |
| 2017 | { |
| 2018 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 2019 | |
| 2020 | /* Some compilers can't handle this inside the switch. */ |
| 2021 | temp = SYNTAX (FETCH_CHAR (from_byte)); |
| 2022 | switch (temp) |
| 2023 | { |
| 2024 | case Scharquote: |
| 2025 | case Sescape: |
| 2026 | INC_BOTH (from, from_byte); |
| 2027 | if (from == stop) goto lose; |
| 2028 | break; |
| 2029 | case Sword: |
| 2030 | case Ssymbol: |
| 2031 | case Squote: |
| 2032 | break; |
| 2033 | default: |
| 2034 | goto done; |
| 2035 | } |
| 2036 | INC_BOTH (from, from_byte); |
| 2037 | } |
| 2038 | goto done; |
| 2039 | |
| 2040 | case Scomment_fence: |
| 2041 | comstyle = ST_COMMENT_STYLE; |
| 2042 | /* FALLTHROUGH */ |
| 2043 | case Scomment: |
| 2044 | if (!parse_sexp_ignore_comments) break; |
| 2045 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 2046 | found = forw_comment (from, from_byte, stop, |
| 2047 | comnested, comstyle, 0, |
| 2048 | &out_charpos, &out_bytepos, &dummy); |
| 2049 | from = out_charpos, from_byte = out_bytepos; |
| 2050 | if (!found) |
| 2051 | { |
| 2052 | if (depth == 0) |
| 2053 | goto done; |
| 2054 | goto lose; |
| 2055 | } |
| 2056 | INC_BOTH (from, from_byte); |
| 2057 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 2058 | break; |
| 2059 | |
| 2060 | case Smath: |
| 2061 | if (!sexpflag) |
| 2062 | break; |
| 2063 | if (from != stop && c == FETCH_CHAR (from_byte)) |
| 2064 | { |
| 2065 | INC_BOTH (from, from_byte); |
| 2066 | } |
| 2067 | if (mathexit) |
| 2068 | { |
| 2069 | mathexit = 0; |
| 2070 | goto close1; |
| 2071 | } |
| 2072 | mathexit = 1; |
| 2073 | |
| 2074 | case Sopen: |
| 2075 | if (!++depth) goto done; |
| 2076 | break; |
| 2077 | |
| 2078 | case Sclose: |
| 2079 | close1: |
| 2080 | if (!--depth) goto done; |
| 2081 | if (depth < min_depth) |
| 2082 | Fsignal (Qscan_error, |
| 2083 | Fcons (build_string ("Containing expression ends prematurely"), |
| 2084 | Fcons (make_number (last_good), |
| 2085 | Fcons (make_number (from), Qnil)))); |
| 2086 | break; |
| 2087 | |
| 2088 | case Sstring: |
| 2089 | case Sstring_fence: |
| 2090 | temp_pos = dec_bytepos (from_byte); |
| 2091 | stringterm = FETCH_CHAR (temp_pos); |
| 2092 | while (1) |
| 2093 | { |
| 2094 | if (from >= stop) goto lose; |
| 2095 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 2096 | if (code == Sstring |
| 2097 | ? (FETCH_CHAR (from_byte) == stringterm) |
| 2098 | : SYNTAX (FETCH_CHAR (from_byte)) == Sstring_fence) |
| 2099 | break; |
| 2100 | |
| 2101 | /* Some compilers can't handle this inside the switch. */ |
| 2102 | temp = SYNTAX (FETCH_CHAR (from_byte)); |
| 2103 | switch (temp) |
| 2104 | { |
| 2105 | case Scharquote: |
| 2106 | case Sescape: |
| 2107 | INC_BOTH (from, from_byte); |
| 2108 | } |
| 2109 | INC_BOTH (from, from_byte); |
| 2110 | } |
| 2111 | INC_BOTH (from, from_byte); |
| 2112 | if (!depth && sexpflag) goto done; |
| 2113 | break; |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | /* Reached end of buffer. Error if within object, return nil if between */ |
| 2118 | if (depth) goto lose; |
| 2119 | |
| 2120 | immediate_quit = 0; |
| 2121 | return Qnil; |
| 2122 | |
| 2123 | /* End of object reached */ |
| 2124 | done: |
| 2125 | count--; |
| 2126 | } |
| 2127 | |
| 2128 | while (count < 0) |
| 2129 | { |
| 2130 | while (from > stop) |
| 2131 | { |
| 2132 | DEC_BOTH (from, from_byte); |
| 2133 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
| 2134 | c = FETCH_CHAR (from_byte); |
| 2135 | code = SYNTAX (c); |
| 2136 | if (depth == min_depth) |
| 2137 | last_good = from; |
| 2138 | comstyle = 0; |
| 2139 | comnested = SYNTAX_COMMENT_NESTED (c); |
| 2140 | if (code == Sendcomment) |
| 2141 | comstyle = SYNTAX_COMMENT_STYLE (c); |
| 2142 | if (from > stop && SYNTAX_COMEND_SECOND (c) |
| 2143 | && prev_char_comend_first (from, from_byte) |
| 2144 | && parse_sexp_ignore_comments) |
| 2145 | { |
| 2146 | /* We must record the comment style encountered so that |
| 2147 | later, we can match only the proper comment begin |
| 2148 | sequence of the same style. */ |
| 2149 | DEC_BOTH (from, from_byte); |
| 2150 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
| 2151 | code = Sendcomment; |
| 2152 | c1 = FETCH_CHAR (from_byte); |
| 2153 | comstyle = SYNTAX_COMMENT_STYLE (c1); |
| 2154 | comnested = comnested || SYNTAX_COMMENT_NESTED (c1); |
| 2155 | } |
| 2156 | |
| 2157 | /* Quoting turns anything except a comment-ender |
| 2158 | into a word character. Note that this if cannot be true |
| 2159 | if we decremented FROM in the if-statement above. */ |
| 2160 | if (code != Sendcomment && char_quoted (from, from_byte)) |
| 2161 | code = Sword; |
| 2162 | else if (SYNTAX_PREFIX (c)) |
| 2163 | continue; |
| 2164 | |
| 2165 | switch (SWITCH_ENUM_CAST (code)) |
| 2166 | { |
| 2167 | case Sword: |
| 2168 | case Ssymbol: |
| 2169 | case Sescape: |
| 2170 | case Scharquote: |
| 2171 | if (depth || !sexpflag) break; |
| 2172 | /* This word counts as a sexp; count object finished |
| 2173 | after passing it. */ |
| 2174 | while (from > stop) |
| 2175 | { |
| 2176 | temp_pos = from_byte; |
| 2177 | if (! NILP (current_buffer->enable_multibyte_characters)) |
| 2178 | DEC_POS (temp_pos); |
| 2179 | else |
| 2180 | temp_pos--; |
| 2181 | UPDATE_SYNTAX_TABLE_BACKWARD (from - 1); |
| 2182 | c1 = FETCH_CHAR (temp_pos); |
| 2183 | temp_code = SYNTAX (c1); |
| 2184 | /* Don't allow comment-end to be quoted. */ |
| 2185 | if (temp_code == Sendcomment) |
| 2186 | goto done2; |
| 2187 | quoted = char_quoted (from - 1, temp_pos); |
| 2188 | if (quoted) |
| 2189 | { |
| 2190 | DEC_BOTH (from, from_byte); |
| 2191 | temp_pos = dec_bytepos (temp_pos); |
| 2192 | UPDATE_SYNTAX_TABLE_BACKWARD (from - 1); |
| 2193 | } |
| 2194 | c1 = FETCH_CHAR (temp_pos); |
| 2195 | temp_code = SYNTAX (c1); |
| 2196 | if (! (quoted || temp_code == Sword |
| 2197 | || temp_code == Ssymbol |
| 2198 | || temp_code == Squote)) |
| 2199 | goto done2; |
| 2200 | DEC_BOTH (from, from_byte); |
| 2201 | } |
| 2202 | goto done2; |
| 2203 | |
| 2204 | case Smath: |
| 2205 | if (!sexpflag) |
| 2206 | break; |
| 2207 | temp_pos = dec_bytepos (from_byte); |
| 2208 | UPDATE_SYNTAX_TABLE_BACKWARD (from - 1); |
| 2209 | if (from != stop && c == FETCH_CHAR (temp_pos)) |
| 2210 | DEC_BOTH (from, from_byte); |
| 2211 | if (mathexit) |
| 2212 | { |
| 2213 | mathexit = 0; |
| 2214 | goto open2; |
| 2215 | } |
| 2216 | mathexit = 1; |
| 2217 | |
| 2218 | case Sclose: |
| 2219 | if (!++depth) goto done2; |
| 2220 | break; |
| 2221 | |
| 2222 | case Sopen: |
| 2223 | open2: |
| 2224 | if (!--depth) goto done2; |
| 2225 | if (depth < min_depth) |
| 2226 | Fsignal (Qscan_error, |
| 2227 | Fcons (build_string ("Containing expression ends prematurely"), |
| 2228 | Fcons (make_number (last_good), |
| 2229 | Fcons (make_number (from), Qnil)))); |
| 2230 | break; |
| 2231 | |
| 2232 | case Sendcomment: |
| 2233 | if (!parse_sexp_ignore_comments) |
| 2234 | break; |
| 2235 | found = back_comment (from, from_byte, stop, comnested, comstyle, |
| 2236 | &out_charpos, &out_bytepos); |
| 2237 | if (found != -1) |
| 2238 | from = out_charpos, from_byte = out_bytepos; |
| 2239 | break; |
| 2240 | |
| 2241 | case Scomment_fence: |
| 2242 | case Sstring_fence: |
| 2243 | while (1) |
| 2244 | { |
| 2245 | DEC_BOTH (from, from_byte); |
| 2246 | if (from == stop) goto lose; |
| 2247 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
| 2248 | if (!char_quoted (from, from_byte) |
| 2249 | && SYNTAX (FETCH_CHAR (from_byte)) == code) |
| 2250 | break; |
| 2251 | } |
| 2252 | if (code == Sstring_fence && !depth && sexpflag) goto done2; |
| 2253 | break; |
| 2254 | |
| 2255 | case Sstring: |
| 2256 | stringterm = FETCH_CHAR (from_byte); |
| 2257 | while (1) |
| 2258 | { |
| 2259 | if (from == stop) goto lose; |
| 2260 | temp_pos = from_byte; |
| 2261 | if (! NILP (current_buffer->enable_multibyte_characters)) |
| 2262 | DEC_POS (temp_pos); |
| 2263 | else |
| 2264 | temp_pos--; |
| 2265 | UPDATE_SYNTAX_TABLE_BACKWARD (from - 1); |
| 2266 | if (!char_quoted (from - 1, temp_pos) |
| 2267 | && stringterm == FETCH_CHAR (temp_pos)) |
| 2268 | break; |
| 2269 | DEC_BOTH (from, from_byte); |
| 2270 | } |
| 2271 | DEC_BOTH (from, from_byte); |
| 2272 | if (!depth && sexpflag) goto done2; |
| 2273 | break; |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | /* Reached start of buffer. Error if within object, return nil if between */ |
| 2278 | if (depth) goto lose; |
| 2279 | |
| 2280 | immediate_quit = 0; |
| 2281 | return Qnil; |
| 2282 | |
| 2283 | done2: |
| 2284 | count++; |
| 2285 | } |
| 2286 | |
| 2287 | |
| 2288 | immediate_quit = 0; |
| 2289 | XSETFASTINT (val, from); |
| 2290 | return val; |
| 2291 | |
| 2292 | lose: |
| 2293 | Fsignal (Qscan_error, |
| 2294 | Fcons (build_string ("Unbalanced parentheses"), |
| 2295 | Fcons (make_number (last_good), |
| 2296 | Fcons (make_number (from), Qnil)))); |
| 2297 | |
| 2298 | /* NOTREACHED */ |
| 2299 | } |
| 2300 | |
| 2301 | DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0, |
| 2302 | "Scan from character number FROM by COUNT lists.\n\ |
| 2303 | Returns the character number of the position thus found.\n\ |
| 2304 | \n\ |
| 2305 | If DEPTH is nonzero, paren depth begins counting from that value,\n\ |
| 2306 | only places where the depth in parentheses becomes zero\n\ |
| 2307 | are candidates for stopping; COUNT such places are counted.\n\ |
| 2308 | Thus, a positive value for DEPTH means go out levels.\n\ |
| 2309 | \n\ |
| 2310 | Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\ |
| 2311 | \n\ |
| 2312 | If the beginning or end of (the accessible part of) the buffer is reached\n\ |
| 2313 | and the depth is wrong, an error is signaled.\n\ |
| 2314 | If the depth is right but the count is not used up, nil is returned.") |
| 2315 | (from, count, depth) |
| 2316 | Lisp_Object from, count, depth; |
| 2317 | { |
| 2318 | CHECK_NUMBER (from, 0); |
| 2319 | CHECK_NUMBER (count, 1); |
| 2320 | CHECK_NUMBER (depth, 2); |
| 2321 | |
| 2322 | return scan_lists (XINT (from), XINT (count), XINT (depth), 0); |
| 2323 | } |
| 2324 | |
| 2325 | DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0, |
| 2326 | "Scan from character number FROM by COUNT balanced expressions.\n\ |
| 2327 | If COUNT is negative, scan backwards.\n\ |
| 2328 | Returns the character number of the position thus found.\n\ |
| 2329 | \n\ |
| 2330 | Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\ |
| 2331 | \n\ |
| 2332 | If the beginning or end of (the accessible part of) the buffer is reached\n\ |
| 2333 | in the middle of a parenthetical grouping, an error is signaled.\n\ |
| 2334 | If the beginning or end is reached between groupings\n\ |
| 2335 | but before count is used up, nil is returned.") |
| 2336 | (from, count) |
| 2337 | Lisp_Object from, count; |
| 2338 | { |
| 2339 | CHECK_NUMBER (from, 0); |
| 2340 | CHECK_NUMBER (count, 1); |
| 2341 | |
| 2342 | return scan_lists (XINT (from), XINT (count), 0, 1); |
| 2343 | } |
| 2344 | |
| 2345 | DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars, |
| 2346 | 0, 0, 0, |
| 2347 | "Move point backward over any number of chars with prefix syntax.\n\ |
| 2348 | This includes chars with \"quote\" or \"prefix\" syntax (' or p).") |
| 2349 | () |
| 2350 | { |
| 2351 | int beg = BEGV; |
| 2352 | int opoint = PT; |
| 2353 | int opoint_byte = PT_BYTE; |
| 2354 | int pos = PT; |
| 2355 | int pos_byte = PT_BYTE; |
| 2356 | int c; |
| 2357 | |
| 2358 | if (pos <= beg) |
| 2359 | { |
| 2360 | SET_PT_BOTH (opoint, opoint_byte); |
| 2361 | |
| 2362 | return Qnil; |
| 2363 | } |
| 2364 | |
| 2365 | SETUP_SYNTAX_TABLE (pos, -1); |
| 2366 | |
| 2367 | DEC_BOTH (pos, pos_byte); |
| 2368 | |
| 2369 | while (!char_quoted (pos, pos_byte) |
| 2370 | /* Previous statement updates syntax table. */ |
| 2371 | && ((c = FETCH_CHAR (pos_byte), SYNTAX (c) == Squote) |
| 2372 | || SYNTAX_PREFIX (c))) |
| 2373 | { |
| 2374 | opoint = pos; |
| 2375 | opoint_byte = pos_byte; |
| 2376 | |
| 2377 | if (pos + 1 > beg) |
| 2378 | DEC_BOTH (pos, pos_byte); |
| 2379 | } |
| 2380 | |
| 2381 | SET_PT_BOTH (opoint, opoint_byte); |
| 2382 | |
| 2383 | return Qnil; |
| 2384 | } |
| 2385 | \f |
| 2386 | /* Parse forward from FROM / FROM_BYTE to END, |
| 2387 | assuming that FROM has state OLDSTATE (nil means FROM is start of function), |
| 2388 | and return a description of the state of the parse at END. |
| 2389 | If STOPBEFORE is nonzero, stop at the start of an atom. |
| 2390 | If COMMENTSTOP is 1, stop at the start of a comment. |
| 2391 | If COMMENTSTOP is -1, stop at the start or end of a comment, |
| 2392 | after the beginning of a string, or after the end of a string. */ |
| 2393 | |
| 2394 | static void |
| 2395 | scan_sexps_forward (stateptr, from, from_byte, end, targetdepth, |
| 2396 | stopbefore, oldstate, commentstop) |
| 2397 | struct lisp_parse_state *stateptr; |
| 2398 | register int from; |
| 2399 | int end, targetdepth, stopbefore; |
| 2400 | Lisp_Object oldstate; |
| 2401 | int commentstop; |
| 2402 | { |
| 2403 | struct lisp_parse_state state; |
| 2404 | |
| 2405 | register enum syntaxcode code; |
| 2406 | int c1; |
| 2407 | int comnested; |
| 2408 | struct level { int last, prev; }; |
| 2409 | struct level levelstart[100]; |
| 2410 | register struct level *curlevel = levelstart; |
| 2411 | struct level *endlevel = levelstart + 100; |
| 2412 | register int depth; /* Paren depth of current scanning location. |
| 2413 | level - levelstart equals this except |
| 2414 | when the depth becomes negative. */ |
| 2415 | int mindepth; /* Lowest DEPTH value seen. */ |
| 2416 | int start_quoted = 0; /* Nonzero means starting after a char quote */ |
| 2417 | Lisp_Object tem; |
| 2418 | int prev_from; /* Keep one character before FROM. */ |
| 2419 | int prev_from_byte; |
| 2420 | int prev_from_syntax; |
| 2421 | int boundary_stop = commentstop == -1; |
| 2422 | int nofence; |
| 2423 | int found; |
| 2424 | int out_bytepos, out_charpos; |
| 2425 | int temp; |
| 2426 | |
| 2427 | prev_from = from; |
| 2428 | prev_from_byte = from_byte; |
| 2429 | if (from != BEGV) |
| 2430 | DEC_BOTH (prev_from, prev_from_byte); |
| 2431 | |
| 2432 | /* Use this macro instead of `from++'. */ |
| 2433 | #define INC_FROM \ |
| 2434 | do { prev_from = from; \ |
| 2435 | prev_from_byte = from_byte; \ |
| 2436 | prev_from_syntax \ |
| 2437 | = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); \ |
| 2438 | INC_BOTH (from, from_byte); \ |
| 2439 | UPDATE_SYNTAX_TABLE_FORWARD (from); \ |
| 2440 | } while (0) |
| 2441 | |
| 2442 | immediate_quit = 1; |
| 2443 | QUIT; |
| 2444 | |
| 2445 | if (NILP (oldstate)) |
| 2446 | { |
| 2447 | depth = 0; |
| 2448 | state.instring = -1; |
| 2449 | state.incomment = 0; |
| 2450 | state.comstyle = 0; /* comment style a by default. */ |
| 2451 | state.comstr_start = -1; /* no comment/string seen. */ |
| 2452 | } |
| 2453 | else |
| 2454 | { |
| 2455 | tem = Fcar (oldstate); |
| 2456 | if (!NILP (tem)) |
| 2457 | depth = XINT (tem); |
| 2458 | else |
| 2459 | depth = 0; |
| 2460 | |
| 2461 | oldstate = Fcdr (oldstate); |
| 2462 | oldstate = Fcdr (oldstate); |
| 2463 | oldstate = Fcdr (oldstate); |
| 2464 | tem = Fcar (oldstate); |
| 2465 | /* Check whether we are inside string_fence-style string: */ |
| 2466 | state.instring = ( !NILP (tem) |
| 2467 | ? ( INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE) |
| 2468 | : -1); |
| 2469 | |
| 2470 | oldstate = Fcdr (oldstate); |
| 2471 | tem = Fcar (oldstate); |
| 2472 | state.incomment = ( !NILP (tem) |
| 2473 | ? ( INTEGERP (tem) ? XINT (tem) : -1) |
| 2474 | : 0); |
| 2475 | |
| 2476 | oldstate = Fcdr (oldstate); |
| 2477 | tem = Fcar (oldstate); |
| 2478 | start_quoted = !NILP (tem); |
| 2479 | |
| 2480 | /* if the eighth element of the list is nil, we are in comment |
| 2481 | style a. If it is non-nil, we are in comment style b */ |
| 2482 | oldstate = Fcdr (oldstate); |
| 2483 | oldstate = Fcdr (oldstate); |
| 2484 | tem = Fcar (oldstate); |
| 2485 | state.comstyle = NILP (tem) ? 0 : ( EQ (tem, Qsyntax_table) |
| 2486 | ? ST_COMMENT_STYLE : 1 ); |
| 2487 | |
| 2488 | oldstate = Fcdr (oldstate); |
| 2489 | tem = Fcar (oldstate); |
| 2490 | state.comstr_start = NILP (tem) ? -1 : XINT (tem) ; |
| 2491 | oldstate = Fcdr (oldstate); |
| 2492 | tem = Fcar (oldstate); |
| 2493 | while (!NILP (tem)) /* >= second enclosing sexps. */ |
| 2494 | { |
| 2495 | /* curlevel++->last ran into compiler bug on Apollo */ |
| 2496 | curlevel->last = XINT (Fcar (tem)); |
| 2497 | if (++curlevel == endlevel) |
| 2498 | error ("Nesting too deep for parser"); |
| 2499 | curlevel->prev = -1; |
| 2500 | curlevel->last = -1; |
| 2501 | tem = Fcdr (tem); |
| 2502 | } |
| 2503 | } |
| 2504 | state.quoted = 0; |
| 2505 | mindepth = depth; |
| 2506 | |
| 2507 | curlevel->prev = -1; |
| 2508 | curlevel->last = -1; |
| 2509 | |
| 2510 | SETUP_SYNTAX_TABLE (prev_from, 1); |
| 2511 | prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); |
| 2512 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 2513 | |
| 2514 | /* Enter the loop at a place appropriate for initial state. */ |
| 2515 | |
| 2516 | if (state.incomment) |
| 2517 | goto startincomment; |
| 2518 | if (state.instring >= 0) |
| 2519 | { |
| 2520 | nofence = state.instring != ST_STRING_STYLE; |
| 2521 | if (start_quoted) |
| 2522 | goto startquotedinstring; |
| 2523 | goto startinstring; |
| 2524 | } |
| 2525 | else if (start_quoted) |
| 2526 | goto startquoted; |
| 2527 | |
| 2528 | #if 0 /* This seems to be redundant with the identical code above. */ |
| 2529 | SETUP_SYNTAX_TABLE (prev_from, 1); |
| 2530 | prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); |
| 2531 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
| 2532 | #endif |
| 2533 | |
| 2534 | while (from < end) |
| 2535 | { |
| 2536 | INC_FROM; |
| 2537 | code = prev_from_syntax & 0xff; |
| 2538 | |
| 2539 | if (code == Scomment) |
| 2540 | { |
| 2541 | state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ? |
| 2542 | 1 : -1); |
| 2543 | state.comstr_start = prev_from; |
| 2544 | } |
| 2545 | else if (code == Scomment_fence) |
| 2546 | { |
| 2547 | /* Record the comment style we have entered so that only |
| 2548 | the comment-end sequence of the same style actually |
| 2549 | terminates the comment section. */ |
| 2550 | state.comstyle = ST_COMMENT_STYLE; |
| 2551 | state.incomment = -1; |
| 2552 | state.comstr_start = prev_from; |
| 2553 | code = Scomment; |
| 2554 | } |
| 2555 | else if (from < end) |
| 2556 | if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)) |
| 2557 | if (c1 = FETCH_CHAR (from_byte), |
| 2558 | SYNTAX_COMSTART_SECOND (c1)) |
| 2559 | /* Duplicate code to avoid a complex if-expression |
| 2560 | which causes trouble for the SGI compiler. */ |
| 2561 | { |
| 2562 | /* Record the comment style we have entered so that only |
| 2563 | the comment-end sequence of the same style actually |
| 2564 | terminates the comment section. */ |
| 2565 | state.comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)); |
| 2566 | comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax); |
| 2567 | comnested = comnested || SYNTAX_COMMENT_NESTED (c1); |
| 2568 | state.incomment = comnested ? 1 : -1; |
| 2569 | state.comstr_start = prev_from; |
| 2570 | INC_FROM; |
| 2571 | code = Scomment; |
| 2572 | } |
| 2573 | |
| 2574 | if (SYNTAX_FLAGS_PREFIX (prev_from_syntax)) |
| 2575 | continue; |
| 2576 | switch (SWITCH_ENUM_CAST (code)) |
| 2577 | { |
| 2578 | case Sescape: |
| 2579 | case Scharquote: |
| 2580 | if (stopbefore) goto stop; /* this arg means stop at sexp start */ |
| 2581 | curlevel->last = prev_from; |
| 2582 | startquoted: |
| 2583 | if (from == end) goto endquoted; |
| 2584 | INC_FROM; |
| 2585 | goto symstarted; |
| 2586 | /* treat following character as a word constituent */ |
| 2587 | case Sword: |
| 2588 | case Ssymbol: |
| 2589 | if (stopbefore) goto stop; /* this arg means stop at sexp start */ |
| 2590 | curlevel->last = prev_from; |
| 2591 | symstarted: |
| 2592 | while (from < end) |
| 2593 | { |
| 2594 | /* Some compilers can't handle this inside the switch. */ |
| 2595 | temp = SYNTAX (FETCH_CHAR (from_byte)); |
| 2596 | switch (temp) |
| 2597 | { |
| 2598 | case Scharquote: |
| 2599 | case Sescape: |
| 2600 | INC_FROM; |
| 2601 | if (from == end) goto endquoted; |
| 2602 | break; |
| 2603 | case Sword: |
| 2604 | case Ssymbol: |
| 2605 | case Squote: |
| 2606 | break; |
| 2607 | default: |
| 2608 | goto symdone; |
| 2609 | } |
| 2610 | INC_FROM; |
| 2611 | } |
| 2612 | symdone: |
| 2613 | curlevel->prev = curlevel->last; |
| 2614 | break; |
| 2615 | |
| 2616 | startincomment: |
| 2617 | if (commentstop == 1) |
| 2618 | goto done; |
| 2619 | goto commentloop; |
| 2620 | |
| 2621 | case Scomment: |
| 2622 | assert (state.incomment != 0); /* state.incomment = -1; */ |
| 2623 | if (commentstop || boundary_stop) goto done; |
| 2624 | commentloop: |
| 2625 | /* The (from == BEGV) test is to enter the loop in the middle so |
| 2626 | that we find a 2-char comment ender even if we start in the |
| 2627 | middle of it. */ |
| 2628 | found = forw_comment (from, from_byte, end, |
| 2629 | state.incomment, state.comstyle, |
| 2630 | (from == BEGV) ? 0 : prev_from_syntax, |
| 2631 | &out_charpos, &out_bytepos, &state.incomment); |
| 2632 | from = out_charpos; from_byte = out_bytepos; |
| 2633 | /* Beware! prev_from and friends are invalid now. |
| 2634 | Luckily, the `done' doesn't use them and the INC_FROM |
| 2635 | sets them to a sane value without looking at them. */ |
| 2636 | if (!found) goto done; |
| 2637 | INC_FROM; |
| 2638 | state.incomment = 0; |
| 2639 | state.comstyle = 0; /* reset the comment style */ |
| 2640 | if (boundary_stop) goto done; |
| 2641 | break; |
| 2642 | |
| 2643 | case Sopen: |
| 2644 | if (stopbefore) goto stop; /* this arg means stop at sexp start */ |
| 2645 | depth++; |
| 2646 | /* curlevel++->last ran into compiler bug on Apollo */ |
| 2647 | curlevel->last = prev_from; |
| 2648 | if (++curlevel == endlevel) |
| 2649 | error ("Nesting too deep for parser"); |
| 2650 | curlevel->prev = -1; |
| 2651 | curlevel->last = -1; |
| 2652 | if (targetdepth == depth) goto done; |
| 2653 | break; |
| 2654 | |
| 2655 | case Sclose: |
| 2656 | depth--; |
| 2657 | if (depth < mindepth) |
| 2658 | mindepth = depth; |
| 2659 | if (curlevel != levelstart) |
| 2660 | curlevel--; |
| 2661 | curlevel->prev = curlevel->last; |
| 2662 | if (targetdepth == depth) goto done; |
| 2663 | break; |
| 2664 | |
| 2665 | case Sstring: |
| 2666 | case Sstring_fence: |
| 2667 | state.comstr_start = from - 1; |
| 2668 | if (stopbefore) goto stop; /* this arg means stop at sexp start */ |
| 2669 | curlevel->last = prev_from; |
| 2670 | state.instring = (code == Sstring |
| 2671 | ? (FETCH_CHAR (prev_from_byte)) |
| 2672 | : ST_STRING_STYLE); |
| 2673 | if (boundary_stop) goto done; |
| 2674 | startinstring: |
| 2675 | { |
| 2676 | nofence = state.instring != ST_STRING_STYLE; |
| 2677 | |
| 2678 | while (1) |
| 2679 | { |
| 2680 | int c; |
| 2681 | |
| 2682 | if (from >= end) goto done; |
| 2683 | c = FETCH_CHAR (from_byte); |
| 2684 | /* Some compilers can't handle this inside the switch. */ |
| 2685 | temp = SYNTAX (c); |
| 2686 | |
| 2687 | /* Check TEMP here so that if the char has |
| 2688 | a syntax-table property which says it is NOT |
| 2689 | a string character, it does not end the string. */ |
| 2690 | if (nofence && c == state.instring && temp == Sstring) |
| 2691 | break; |
| 2692 | |
| 2693 | switch (temp) |
| 2694 | { |
| 2695 | case Sstring_fence: |
| 2696 | if (!nofence) goto string_end; |
| 2697 | break; |
| 2698 | case Scharquote: |
| 2699 | case Sescape: |
| 2700 | INC_FROM; |
| 2701 | startquotedinstring: |
| 2702 | if (from >= end) goto endquoted; |
| 2703 | } |
| 2704 | INC_FROM; |
| 2705 | } |
| 2706 | } |
| 2707 | string_end: |
| 2708 | state.instring = -1; |
| 2709 | curlevel->prev = curlevel->last; |
| 2710 | INC_FROM; |
| 2711 | if (boundary_stop) goto done; |
| 2712 | break; |
| 2713 | |
| 2714 | case Smath: |
| 2715 | break; |
| 2716 | } |
| 2717 | } |
| 2718 | goto done; |
| 2719 | |
| 2720 | stop: /* Here if stopping before start of sexp. */ |
| 2721 | from = prev_from; /* We have just fetched the char that starts it; */ |
| 2722 | goto done; /* but return the position before it. */ |
| 2723 | |
| 2724 | endquoted: |
| 2725 | state.quoted = 1; |
| 2726 | done: |
| 2727 | state.depth = depth; |
| 2728 | state.mindepth = mindepth; |
| 2729 | state.thislevelstart = curlevel->prev; |
| 2730 | state.prevlevelstart |
| 2731 | = (curlevel == levelstart) ? -1 : (curlevel - 1)->last; |
| 2732 | state.location = from; |
| 2733 | state.levelstarts = Qnil; |
| 2734 | while (--curlevel >= levelstart) |
| 2735 | state.levelstarts = Fcons (make_number (curlevel->last), |
| 2736 | state.levelstarts); |
| 2737 | immediate_quit = 0; |
| 2738 | |
| 2739 | *stateptr = state; |
| 2740 | } |
| 2741 | |
| 2742 | /* This comment supplies the doc string for parse-partial-sexp, |
| 2743 | for make-docfile to see. We cannot put this in the real DEFUN |
| 2744 | due to limits in the Unix cpp. |
| 2745 | |
| 2746 | DEFUN ("parse-partial-sexp", Ffoo, Sfoo, 2, 6, 0, |
| 2747 | "Parse Lisp syntax starting at FROM until TO; return status of parse at TO.\n\ |
| 2748 | Parsing stops at TO or when certain criteria are met;\n\ |
| 2749 | point is set to where parsing stops.\n\ |
| 2750 | If fifth arg STATE is omitted or nil,\n\ |
| 2751 | parsing assumes that FROM is the beginning of a function.\n\ |
| 2752 | Value is a list of ten elements describing final state of parsing:\n\ |
| 2753 | 0. depth in parens.\n\ |
| 2754 | 1. character address of start of innermost containing list; nil if none.\n\ |
| 2755 | 2. character address of start of last complete sexp terminated.\n\ |
| 2756 | 3. non-nil if inside a string.\n\ |
| 2757 | (it is the character that will terminate the string,\n\ |
| 2758 | or t if the string should be terminated by a generic string delimiter.)\n\ |
| 2759 | 4. nil if outside a comment, t if inside a non-nestable comment, \n\ |
| 2760 | else an integer (the current comment nesting).\n\ |
| 2761 | 5. t if following a quote character.\n\ |
| 2762 | 6. the minimum paren-depth encountered during this scan.\n\ |
| 2763 | 7. t if in a comment of style b; `syntax-table' if the comment\n\ |
| 2764 | should be terminated by a generic comment delimiter.\n\ |
| 2765 | 8. character address of start of comment or string; nil if not in one.\n\ |
| 2766 | 9. Intermediate data for continuation of parsing (subject to change).\n\ |
| 2767 | If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\ |
| 2768 | in parentheses becomes equal to TARGETDEPTH.\n\ |
| 2769 | Fourth arg STOPBEFORE non-nil means stop when come to\n\ |
| 2770 | any character that starts a sexp.\n\ |
| 2771 | Fifth arg STATE is a nine-element list like what this function returns.\n\ |
| 2772 | It is used to initialize the state of the parse. Elements number 1, 2, 6\n\ |
| 2773 | and 8 are ignored; you can leave off element 8 (the last) entirely.\n\ |
| 2774 | Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.\n\ |
| 2775 | If it is `syntax-table', stop after the start of a comment or a string,\n\ |
| 2776 | or after end of a comment or a string.") |
| 2777 | (from, to, targetdepth, stopbefore, state, commentstop) |
| 2778 | */ |
| 2779 | |
| 2780 | DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0, |
| 2781 | 0 /* See immediately above */) |
| 2782 | (from, to, targetdepth, stopbefore, oldstate, commentstop) |
| 2783 | Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop; |
| 2784 | { |
| 2785 | struct lisp_parse_state state; |
| 2786 | int target; |
| 2787 | |
| 2788 | if (!NILP (targetdepth)) |
| 2789 | { |
| 2790 | CHECK_NUMBER (targetdepth, 3); |
| 2791 | target = XINT (targetdepth); |
| 2792 | } |
| 2793 | else |
| 2794 | target = -100000; /* We won't reach this depth */ |
| 2795 | |
| 2796 | validate_region (&from, &to); |
| 2797 | scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)), |
| 2798 | XINT (to), |
| 2799 | target, !NILP (stopbefore), oldstate, |
| 2800 | (NILP (commentstop) |
| 2801 | ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1))); |
| 2802 | |
| 2803 | SET_PT (state.location); |
| 2804 | |
| 2805 | return Fcons (make_number (state.depth), |
| 2806 | Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart), |
| 2807 | Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart), |
| 2808 | Fcons (state.instring >= 0 |
| 2809 | ? (state.instring == ST_STRING_STYLE |
| 2810 | ? Qt : make_number (state.instring)) : Qnil, |
| 2811 | Fcons (state.incomment < 0 ? Qt : |
| 2812 | (state.incomment == 0 ? Qnil : |
| 2813 | make_number (state.incomment)), |
| 2814 | Fcons (state.quoted ? Qt : Qnil, |
| 2815 | Fcons (make_number (state.mindepth), |
| 2816 | Fcons ((state.comstyle |
| 2817 | ? (state.comstyle == ST_COMMENT_STYLE |
| 2818 | ? Qsyntax_table : Qt) : |
| 2819 | Qnil), |
| 2820 | Fcons (((state.incomment |
| 2821 | || (state.instring >= 0)) |
| 2822 | ? make_number (state.comstr_start) |
| 2823 | : Qnil), |
| 2824 | Fcons (state.levelstarts, Qnil)))))))))); |
| 2825 | } |
| 2826 | \f |
| 2827 | void |
| 2828 | init_syntax_once () |
| 2829 | { |
| 2830 | register int i, c; |
| 2831 | Lisp_Object temp; |
| 2832 | |
| 2833 | /* This has to be done here, before we call Fmake_char_table. */ |
| 2834 | Qsyntax_table = intern ("syntax-table"); |
| 2835 | staticpro (&Qsyntax_table); |
| 2836 | |
| 2837 | /* Intern this now in case it isn't already done. |
| 2838 | Setting this variable twice is harmless. |
| 2839 | But don't staticpro it here--that is done in alloc.c. */ |
| 2840 | Qchar_table_extra_slots = intern ("char-table-extra-slots"); |
| 2841 | |
| 2842 | /* Create objects which can be shared among syntax tables. */ |
| 2843 | Vsyntax_code_object = Fmake_vector (make_number (13), Qnil); |
| 2844 | for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++) |
| 2845 | XVECTOR (Vsyntax_code_object)->contents[i] |
| 2846 | = Fcons (make_number (i), Qnil); |
| 2847 | |
| 2848 | /* Now we are ready to set up this property, so we can |
| 2849 | create syntax tables. */ |
| 2850 | Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0)); |
| 2851 | |
| 2852 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace]; |
| 2853 | |
| 2854 | Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp); |
| 2855 | |
| 2856 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword]; |
| 2857 | for (i = 'a'; i <= 'z'; i++) |
| 2858 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp); |
| 2859 | for (i = 'A'; i <= 'Z'; i++) |
| 2860 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp); |
| 2861 | for (i = '0'; i <= '9'; i++) |
| 2862 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp); |
| 2863 | |
| 2864 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp); |
| 2865 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp); |
| 2866 | |
| 2867 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(', |
| 2868 | Fcons (make_number (Sopen), make_number (')'))); |
| 2869 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')', |
| 2870 | Fcons (make_number (Sclose), make_number ('('))); |
| 2871 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[', |
| 2872 | Fcons (make_number (Sopen), make_number (']'))); |
| 2873 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']', |
| 2874 | Fcons (make_number (Sclose), make_number ('['))); |
| 2875 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{', |
| 2876 | Fcons (make_number (Sopen), make_number ('}'))); |
| 2877 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}', |
| 2878 | Fcons (make_number (Sclose), make_number ('{'))); |
| 2879 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"', |
| 2880 | Fcons (make_number ((int) Sstring), Qnil)); |
| 2881 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\', |
| 2882 | Fcons (make_number ((int) Sescape), Qnil)); |
| 2883 | |
| 2884 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol]; |
| 2885 | for (i = 0; i < 10; i++) |
| 2886 | { |
| 2887 | c = "_-+*/&|<>="[i]; |
| 2888 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp); |
| 2889 | } |
| 2890 | |
| 2891 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct]; |
| 2892 | for (i = 0; i < 12; i++) |
| 2893 | { |
| 2894 | c = ".,;:?!#@~^'`"[i]; |
| 2895 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp); |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | void |
| 2900 | syms_of_syntax () |
| 2901 | { |
| 2902 | Qsyntax_table_p = intern ("syntax-table-p"); |
| 2903 | staticpro (&Qsyntax_table_p); |
| 2904 | |
| 2905 | staticpro (&Vsyntax_code_object); |
| 2906 | |
| 2907 | Qscan_error = intern ("scan-error"); |
| 2908 | staticpro (&Qscan_error); |
| 2909 | Fput (Qscan_error, Qerror_conditions, |
| 2910 | Fcons (Qscan_error, Fcons (Qerror, Qnil))); |
| 2911 | Fput (Qscan_error, Qerror_message, |
| 2912 | build_string ("Scan error")); |
| 2913 | |
| 2914 | DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments, |
| 2915 | "Non-nil means `forward-sexp', etc., should treat comments as whitespace."); |
| 2916 | |
| 2917 | DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties, |
| 2918 | "Non-nil means `forward-sexp', etc., grant `syntax-table' property.\n\ |
| 2919 | The value of this property should be either a syntax table, or a cons\n\ |
| 2920 | of the form (SYNTAXCODE . MATCHCHAR), SYNTAXCODE being the numeric\n\ |
| 2921 | syntax code, MATCHCHAR being nil or the character to match (which is\n\ |
| 2922 | relevant only for open/close type."); |
| 2923 | |
| 2924 | words_include_escapes = 0; |
| 2925 | DEFVAR_BOOL ("words-include-escapes", &words_include_escapes, |
| 2926 | "Non-nil means `forward-word', etc., should treat escape chars part of words."); |
| 2927 | |
| 2928 | defsubr (&Ssyntax_table_p); |
| 2929 | defsubr (&Ssyntax_table); |
| 2930 | defsubr (&Sstandard_syntax_table); |
| 2931 | defsubr (&Scopy_syntax_table); |
| 2932 | defsubr (&Sset_syntax_table); |
| 2933 | defsubr (&Schar_syntax); |
| 2934 | defsubr (&Smatching_paren); |
| 2935 | defsubr (&Smodify_syntax_entry); |
| 2936 | defsubr (&Sdescribe_syntax); |
| 2937 | |
| 2938 | defsubr (&Sforward_word); |
| 2939 | |
| 2940 | defsubr (&Sskip_chars_forward); |
| 2941 | defsubr (&Sskip_chars_backward); |
| 2942 | defsubr (&Sskip_syntax_forward); |
| 2943 | defsubr (&Sskip_syntax_backward); |
| 2944 | |
| 2945 | defsubr (&Sforward_comment); |
| 2946 | defsubr (&Sscan_lists); |
| 2947 | defsubr (&Sscan_sexps); |
| 2948 | defsubr (&Sbackward_prefix_chars); |
| 2949 | defsubr (&Sparse_partial_sexp); |
| 2950 | } |