home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / syntax.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  84KB  |  2,834 lines

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