changeset 20546:a475efff810e

(scan_sexps_forward): Additional arg FROM_BYTE. Calls changed. (find_defun_start): Additional arg POS_BYTE. Save final byte pos in find_start_value_byte. (find_start_value_byte): New variable. (Fforward_comment): Scan by bytes and chars. (Fbackward_prefix_chars): Scan by bytes and chars. (scan_words, scan_sexps_forward): Scan bytes as well as chars. (skip_chars, scan_lists): Scan by bytes and chars. Now static. (char_quoted): Take charpos and bytepos as args. (back_comment): Likewise. (update_syntax_table): Arg renamed. (inc_bytepos, dec_bytepos): New functions.
author Richard M. Stallman <rms@gnu.org>
date Thu, 01 Jan 1998 06:31:54 +0000
parents c20c92ff4055
children 07053199a368
files src/syntax.c
diffstat 1 files changed, 328 insertions(+), 497 deletions(-) [+]
line wrap: on
line diff
--- a/src/syntax.c	Thu Jan 01 02:52:20 1998 +0000
+++ b/src/syntax.c	Thu Jan 01 06:31:54 1998 +0000
@@ -42,9 +42,6 @@
 
 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
 
-static void scan_sexps_forward ();
-static int char_quoted ();
-
 int words_include_escapes;
 int parse_sexp_lookup_properties;
 
@@ -72,15 +69,27 @@
 /* These variables are a cache for finding the start of a defun.
    find_start_pos is the place for which the defun start was found.
    find_start_value is the defun start position found for it.
+   find_start_value_byte is the corresponding byte position.
    find_start_buffer is the buffer it was found in.
    find_start_begv is the BEGV value when it was found.
    find_start_modiff is the value of MODIFF when it was found.  */
 
 static int find_start_pos;
 static int find_start_value;
+static int find_start_value_byte;
 static struct buffer *find_start_buffer;
 static int find_start_begv;
 static int find_start_modiff;
+
+
+static int find_defun_start P_ ((int, int));
+static int back_comment P_ ((int, int, int, int, int *, int *));
+static int char_quoted P_ ((int, int));
+static Lisp_Object skip_chars P_ ((int, int, Lisp_Object, Lisp_Object));
+static Lisp_Object scan_lists P_ ((int, int, int, int));
+static void scan_sexps_forward P_ ((struct lisp_parse_state *,
+				    int, int, int, int,
+				    int, Lisp_Object, int));
 
 
 struct gl_state_s gl_state;		/* Global state of syntax parser.  */
@@ -89,23 +98,22 @@
 #define INTERVALS_AT_ONCE 10		/* 1 + max-number of intervals
 					   to scan to property-change.  */
 
-/* 
-   Update gl_state to an appropriate interval which contains POS.  The
-   sign of COUNT give the relative position of POS wrt the previously
+/* Update gl_state to an appropriate interval which contains CHARPOS.  The
+   sign of COUNT give the relative position of CHARPOS wrt the previously
    valid interval.  If INIT, only [be]_property fields of gl_state are
-   valid at start, the  rest is filled basing on OBJECT.
+   valid at start, the rest is filled basing on OBJECT.
 
-   `gl_state.*_i' are the intervals, and pos is further in the search
+   `gl_state.*_i' are the intervals, and CHARPOS is further in the search
    direction than the intervals - or in an interval.  We update the
    current syntax-table basing on the property of this interval, and
-   update the interval to start further than POS - or be
+   update the interval to start further than CHARPOS - or be
    NULL_INTERVAL.  We also update lim_property to be the next value of
-   pos to call this subroutine again - or be before/after the
+   charpos to call this subroutine again - or be before/after the
    start/end of OBJECT.  */
 
 void
-update_syntax_table (pos, count, init, object)
-     int pos, count, init;
+update_syntax_table (charpos, count, init, object)
+     int charpos, count, init;
      Lisp_Object object;
 {
   Lisp_Object tmp_table;
@@ -116,7 +124,7 @@
     {
       gl_state.start = gl_state.b_property;
       gl_state.stop = gl_state.e_property;
-      gl_state.forward_i = interval_of (pos, object);
+      gl_state.forward_i = interval_of (charpos, object);
       i = gl_state.backward_i = gl_state.forward_i;
       gl_state.left_ok = gl_state.right_ok = 1;
       invalidate = 0;
@@ -128,15 +136,16 @@
     }
   oldi = i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
 
-  /* We are guarantied to be called with pos either in i, of further off.  */
+  /* We are guarantied to be called with CHARPOS either in i,
+     or further off.  */
   if (NULL_INTERVAL_P (i))
     error ("Error in syntax_table logic for to-the-end intervals");
-  else if (pos < i->position)		/* Move left.  */
+  else if (charpos < i->position)		/* Move left.  */
     {
       if (count > 0)
 	error ("Error in syntax_table logic for intervals <-.");
       /* Update the interval.  */
-      i = update_interval (i, pos);
+      i = update_interval (i, charpos);
       if (oldi->position != INTERVAL_LAST_POS (i))
 	{
 	  invalidate = 0;
@@ -145,12 +154,12 @@
 	  gl_state.e_property = INTERVAL_LAST_POS (i);
 	}
     } 
-  else if (pos >= INTERVAL_LAST_POS (i)) /* Move right.  */
+  else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right.  */
     {
       if (count < 0)
 	error ("Error in syntax_table logic for intervals ->.");
       /* Update the interval.  */
-      i = update_interval (i, pos);
+      i = update_interval (i, charpos);
       if (i->position != INTERVAL_LAST_POS (oldi))
 	{
 	  invalidate = 0;
@@ -248,29 +257,56 @@
     }    
 }
 
-/* Returns TRUE if char at POS is quoted.
-   Global syntax-table data should be set up already to be good at pos
-   or after.  On return global syntax data is good for lookup at POS. */
+/* Returns TRUE if char at CHARPOS is quoted.
+   Global syntax-table data should be set up already to be good at CHARPOS
+   or after.  On return global syntax data is good for lookup at CHARPOS. */
 
 static int
-char_quoted (pos)
-     register int pos;
+char_quoted (charpos, bytepos)
+     register int charpos, bytepos;
 {
   register enum syntaxcode code;
   register int beg = BEGV;
   register int quoted = 0;
-  int temp_pos = pos;
+  int orig = charpos;
+
+  DEC_BOTH (charpos, bytepos);
+
+  while (bytepos >= beg)
+    {
+      UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
+      code = SYNTAX (FETCH_CHAR (bytepos));
+      if (! (code == Scharquote || code == Sescape))
+	break;
+
+      DEC_BOTH (charpos, bytepos);
+      quoted = !quoted;
+    }
+
+  UPDATE_SYNTAX_TABLE (orig);
+  return quoted;
+}
 
-  DEC_POS (temp_pos);
-  while (temp_pos >= beg
-	 && ( UPDATE_SYNTAX_TABLE_BACKWARD (temp_pos), 1)
-	 && ((code = SYNTAX (FETCH_CHAR (temp_pos))) == Scharquote
-	     || code == Sescape))
-    {
-      temp_pos--, quoted = !quoted;
-    }
-  UPDATE_SYNTAX_TABLE (pos);
-  return quoted;
+/* Return the bytepos one character after BYTEPOS.
+   We assume that BYTEPOS is not at the end of the buffer.  */
+
+INLINE int
+inc_bytepos (bytepos)
+     int bytepos;
+{
+  INC_POS (bytepos);
+  return bytepos;
+}
+
+/* Return the bytepos one character before BYTEPOS.
+   We assume that BYTEPOS is not at the start of the buffer.  */
+
+INLINE int
+dec_bytepos (bytepos)
+     int bytepos;
+{
+  DEC_POS (bytepos);
+  return bytepos;
 }
 
 /* Find a defun-start that is the last one before POS (or nearly the last).
@@ -282,11 +318,12 @@
    update the global data.  */
 
 static int
-find_defun_start (pos)
-     int pos;
+find_defun_start (pos, pos_byte)
+     int pos, pos_byte;
 {
   int tem;
   int shortage;
+  int opoint = PT, opoint_byte = PT_BYTE;
 
   /* Use previous finding, if it's valid and applies to this inquiry.  */
   if (current_buffer == find_start_buffer
@@ -300,50 +337,60 @@
     return find_start_value;
 
   /* Back up to start of line.  */
-  tem = scan_buffer ('\n', pos, BEGV, -1, &shortage, 1);
+  scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
 
   /* We optimize syntax-table lookup for rare updates.  Thus we accept
      only those `^\s(' which are good in global _and_ text-property
      syntax-tables.  */
   gl_state.current_syntax_table = current_buffer->syntax_table;
   gl_state.use_global = 0;
-  while (tem > BEGV)
+  while (PT > BEGV)
     {
       /* Open-paren at start of line means we found our defun-start.  */
-      if (SYNTAX (FETCH_CHAR (tem)) == Sopen)
+      if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
 	{
-	  SETUP_SYNTAX_TABLE (tem + 1, -1);	/* Try again... */
-	  if (SYNTAX (FETCH_CHAR (tem)) == Sopen)
+	  SETUP_SYNTAX_TABLE (PT + 1, -1);	/* Try again... */
+	  if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
 	    break;
 	  /* Now fallback to the default value.  */
 	  gl_state.current_syntax_table = current_buffer->syntax_table;
 	  gl_state.use_global = 0;
 	}
       /* Move to beg of previous line.  */
-      tem = scan_buffer ('\n', tem, BEGV, -2, &shortage, 1);
+      scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
     }
 
   /* Record what we found, for the next try.  */
-  find_start_value = tem;
+  find_start_value = PT;
+  find_start_value_byte = PT_BYTE;
   find_start_buffer = current_buffer;
   find_start_modiff = MODIFF;
   find_start_begv = BEGV;
   find_start_pos = pos;
 
+  TEMP_SET_PT_BOTH (opoint, opoint_byte);
+
   return find_start_value;
 }
 
-/* Checks whether FROM is at the end of a comment;
-   and if so, returns position of the start of the comment.
-   But does not move back before STOP.
-   Returns -1 if there is no comment ending at FROM.
+/* Checks whether charpos FROM is at the end of a comment.
+   FROM_BYTE is the bytepos corresponding to FROM.
+   Do not move back before STOP.
+
+   Return a positive value if we find a comment ending at FROM/FROM_BYTE;
+   return -1 otherwise.
+
+   If successful, store the charpos of the comment's beginning
+   into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
 
    Global syntax data remains valid for backward search starting at
    the returned value (or at FROM, if the search was not successful).  */
 
 static int
-back_comment (from, stop, comstyle)
-     int from, stop, comstyle;
+back_comment (from, from_byte, stop, comstyle, charpos_ptr, bytepos_ptr)
+     int from, from_byte, stop;
+     int comstyle;
+     int *charpos_ptr, *bytepos_ptr;
 {
   /* Look back, counting the parity of string-quotes,
      and recording the comment-starters seen.
@@ -358,8 +405,10 @@
   int my_stringend = 0;
   int string_lossage = 0;
   int comment_end = from;
+  int comment_end_byte = from_byte;
   int comstart_pos = 0;
   int comstart_parity = 0;
+  int comstart_byte;
   int scanstart = from - 1;
   register enum syntaxcode code;
   int c;
@@ -368,34 +417,38 @@
      that determines quote parity to the comment-end.  */
   while (from != stop)
     {
+      int temp_byte;
+
       /* Move back and examine a character.  */
-      DEC_POS (from);
+      DEC_BOTH (from, from_byte);
       UPDATE_SYNTAX_TABLE_BACKWARD (from);
 
-      c = FETCH_CHAR (from);
+      c = FETCH_CHAR (from_byte);
       code = SYNTAX (c);
 
       /* If this char is the second of a 2-char comment end sequence,
 	 back up and give the pair the appropriate syntax.  */
       if (from > stop && SYNTAX_COMEND_SECOND (c)
-	  && SYNTAX_COMEND_FIRST (FETCH_CHAR (from - 1)))
+	  && (temp_byte = dec_bytepos (from_byte),
+	      SYNTAX_COMEND_FIRST (FETCH_CHAR (temp_byte))))
 	{
 	  code = Sendcomment;
-	  DEC_POS (from);
+	  DEC_BOTH (from, from_byte);
 	  /* This is apparently the best we can do: */
 	  UPDATE_SYNTAX_TABLE_BACKWARD (from);
-	  c = FETCH_CHAR (from);
+	  c = FETCH_CHAR (from_byte);
 	}
 			
       /* If this char starts a 2-char comment start sequence,
 	 treat it like a 1-char comment starter.  */
       if (from < scanstart && SYNTAX_COMSTART_FIRST (c)
-	  && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from + 1))
-	  && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (from + 1)))
+	  && (temp_byte = inc_bytepos (from_byte),
+	      (SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte))
+	       && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte)))))
 	code = Scomment;
 
       /* Ignore escaped characters.  */
-      if (char_quoted (from))
+      if (char_quoted (from, from_byte))
 	continue;
 
       /* Track parity of quotes.  */
@@ -414,8 +467,8 @@
 	{
 	  parity ^= 1;
 	  if (my_stringend == 0)
-	    my_stringend = 
-		code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE;
+	    my_stringend
+	      = code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE;
 	  /* If we have two kinds of string delimiters.
 	     There's no way to grok this scanning backwards.  */
 	  else if (my_stringend != (code == Sstring_fence 
@@ -429,24 +482,28 @@
 	{
 	  comstart_parity = parity;
 	  comstart_pos = from;
+	  comstart_byte = from_byte;
 	}
 
       /* If we find another earlier comment-ender,
 	 any comment-starts earlier than that don't count
 	 (because they go with the earlier comment-ender).  */
       if (code == Sendcomment
-	  && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)) == comstyle)
+	  && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)) == comstyle)
 	break;
 
       /* Assume a defun-start point is outside of strings.  */
       if (code == Sopen
-	  && (from == stop || FETCH_CHAR (from - 1) == '\n'))
+	  && (from == stop
+	      || (temp_byte = dec_bytepos (from_byte),
+		  FETCH_CHAR (temp_byte) == '\n')))
 	break;
     }
 
   if (comstart_pos == 0)
     {
       from = comment_end;
+      from_byte = comment_end_byte;
       UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
     }
   /* If the earliest comment starter
@@ -457,6 +514,7 @@
   else if (comstart_parity == 0 && !string_lossage) 
     {
       from = comstart_pos;
+      from_byte = comstart_byte;
       /* Globals are correct now.  */
     }
   else
@@ -467,7 +525,9 @@
 	 to the one in question; this records where we
 	 last passed a comment starter.  */
       struct lisp_parse_state state;
-      scan_sexps_forward (&state, find_defun_start (comment_end),
+      scan_sexps_forward (&state,
+			  find_defun_start (comment_end, comment_end_byte),
+			  find_start_value_byte,
 			  comment_end - 1, -10000, 0, Qnil, 0);
       if (state.incomment)
 	{
@@ -480,9 +540,13 @@
 	{
 	  from = comment_end;	  
 	}
+      from_byte = CHAR_TO_BYTE (from);
       UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
     }
     
+  *charpos_ptr = from;
+  *bytepos_ptr = from_byte;
+
   return from;
 }
 
@@ -970,9 +1034,9 @@
 {
   register int beg = BEGV;
   register int end = ZV;
+  register int from_byte = CHAR_TO_BYTE (from);
   register enum syntaxcode code;
   int ch0, ch1;
-  int temp_pos;
 
   immediate_quit = 1;
   QUIT;
@@ -989,9 +1053,9 @@
 	      return 0;
 	    }
 	  UPDATE_SYNTAX_TABLE_FORWARD (from);
-	  ch0 = FETCH_CHAR (from);
+	  ch0 = FETCH_CHAR (from_byte);
 	  code = SYNTAX (ch0);
-	  INC_POS (from);
+	  INC_BOTH (from, from_byte);
 	  if (words_include_escapes
 	      && (code == Sescape || code == Scharquote))
 	    break;
@@ -1004,13 +1068,13 @@
 	{
 	  if (from == end) break;
 	  UPDATE_SYNTAX_TABLE_FORWARD (from);
-	  ch1 = FETCH_CHAR (from);
+	  ch1 = FETCH_CHAR (from_byte);
 	  code = SYNTAX (ch1);
 	  if (!(words_include_escapes
 		&& (code == Sescape || code == Scharquote)))
 	    if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
 	      break;
-	  INC_POS (from);
+	  INC_BOTH (from, from_byte);
 	  ch0 = ch1;
 	}
       count--;
@@ -1024,9 +1088,9 @@
 	      immediate_quit = 0;
 	      return 0;
 	    }
-	  DEC_POS (from);
+	  DEC_BOTH (from, from_byte);
 	  UPDATE_SYNTAX_TABLE_BACKWARD (from);
-	  ch1 = FETCH_CHAR (from);
+	  ch1 = FETCH_CHAR (from_byte);
 	  code = SYNTAX (ch1);
 	  if (words_include_escapes
 	      && (code == Sescape || code == Scharquote))
@@ -1038,17 +1102,19 @@
          position of it.  */
       while (1)
 	{
-	  if (from == beg) break;
-	  temp_pos = from;
-	  DEC_POS (temp_pos);
+	  int temp_byte;
+
+	  if (from == beg)
+	    break;
+	  temp_byte = dec_bytepos (from_byte);
 	  UPDATE_SYNTAX_TABLE_BACKWARD (from);
-	  ch0 = FETCH_CHAR (temp_pos);
+	  ch0 = FETCH_CHAR (temp_byte);
 	  code = SYNTAX (ch0);
 	  if (!(words_include_escapes
 		&& (code == Sescape || code == Scharquote)))
 	    if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
 	      break;
-	  from = temp_pos;
+	  DEC_BOTH (from, from_byte);
 	  ch1 = ch0;
 	}
       count++;
@@ -1128,7 +1194,7 @@
   return skip_chars (0, 1, syntax, lim);
 }
 
-Lisp_Object
+static Lisp_Object
 skip_chars (forwardp, syntaxp, string, lim)
      int forwardp, syntaxp;
      Lisp_Object string, lim;
@@ -1156,12 +1222,9 @@
   if (NILP (lim))
     XSETINT (lim, forwardp ? ZV : BEGV);
   else
-    CHECK_NUMBER_COERCE_MARKER (lim, 1);
+    CHECK_NUMBER_COERCE_MARKER (lim, 0);
 
   /* In any case, don't allow scan outside bounds of buffer.  */
-  /* jla turned this off, for no known reason.
-     bfox turned the ZV part on, and rms turned the
-     BEGV part back on.  */
   if (XINT (lim) > ZV)
     XSETFASTINT (lim, ZV);
   if (XINT (lim) < BEGV)
@@ -1253,6 +1316,7 @@
   {
     int start_point = PT;
     int pos = PT;
+    int pos_byte = PT_BYTE;
 
     immediate_quit = 1;
     if (syntaxp)
@@ -1263,9 +1327,9 @@
 	    if (multibyte)
 	      {
 		while (pos < XINT (lim)
-		       && fastmap[(int) SYNTAX (FETCH_CHAR (pos))])
+		       && fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))])
 		  {
-		    INC_POS (pos);
+		    INC_BOTH (pos, pos_byte);
 		    UPDATE_SYNTAX_TABLE_FORWARD (pos);
 		  }
 	      }
@@ -1285,12 +1349,13 @@
 	      {
 		while (pos > XINT (lim))
 		  {
-		    int savepos = pos;
-		    DEC_POS (pos);
+		    int savepos = pos_byte;
+		    DEC_BOTH (pos, pos_byte);
 		    UPDATE_SYNTAX_TABLE_BACKWARD (pos);
-		    if (!fastmap[(int) SYNTAX (FETCH_CHAR (pos))])
+		    if (!fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))])
 		      {
-			pos = savepos;
+			pos++;
+			pos_byte = savepos;
 			break;
 		      }
 		  }
@@ -1315,27 +1380,27 @@
 	if (forwardp)
 	  {
 	    if (multibyte)
-	      while (pos < XINT (lim) && fastmap[(c = FETCH_BYTE (pos))])
+	      while (pos < XINT (lim) && fastmap[(c = FETCH_BYTE (pos_byte))])
 		{
 		  if (!BASE_LEADING_CODE_P (c))
-		    pos++;
+		    INC_BOTH (pos, pos_byte);
 		  else if (n_char_ranges)
 		    {
 		      /* We much check CHAR_RANGES for a multibyte
 			 character.  */
-		      ch = FETCH_MULTIBYTE_CHAR (pos);
+		      ch = FETCH_MULTIBYTE_CHAR (pos_byte);
 		      for (i = 0; i < n_char_ranges; i += 2)
 			if ((ch >= char_ranges[i] && ch <= char_ranges[i + 1]))
 			  break;
 		      if (!(negate ^ (i < n_char_ranges)))
 			break;
 
-		      INC_POS (pos);
+		      INC_BOTH (pos, pos_byte);
 		    }
 		  else
 		    {
 		      if (!negate) break;
-		      INC_POS (pos);
+		      INC_BOTH (pos, pos_byte);
 		    }
 		}
 	    else
@@ -1347,9 +1412,9 @@
 	    if (multibyte)
 	      while (pos > XINT (lim))
 		{
-		  int savepos = pos;
-		  DEC_POS (pos);
-		  if (fastmap[(c = FETCH_BYTE (pos))])
+		  int savepos = pos_byte;
+		  DEC_BOTH (pos, pos_byte);
+		  if (fastmap[(c = FETCH_BYTE (pos_byte))])
 		    {
 		      if (!BASE_LEADING_CODE_P (c))
 			;
@@ -1357,26 +1422,29 @@
 			{
 			  /* We much check CHAR_RANGES for a multibyte
 			     character.  */
-			  ch = FETCH_MULTIBYTE_CHAR (pos);
+			  ch = FETCH_MULTIBYTE_CHAR (pos_byte);
 			  for (i = 0; i < n_char_ranges; i += 2)
 			    if (ch >= char_ranges[i] && ch <= char_ranges[i + 1])
 			      break;
 			  if (!(negate ^ (i < n_char_ranges)))
 			    {
-			      pos = savepos;
+			      pos++;
+			      pos_byte = savepos;
 			      break;
 			    }
 			}
 		      else
 			if (!negate)
 			  {
-			    pos = savepos;
+			    pos++;
+			    pos_byte = savepos;
 			    break;
 			  }
 		    }
 		  else
 		    {
-		      pos = savepos;
+		      pos++;
+		      pos_byte = savepos;
 		      break;
 		    }
 		}
@@ -1386,12 +1454,18 @@
 	  }
       }
 
+#if 0 /* Not needed now that a position in mid-character
+	 cannot be specified in Lisp.  */
     if (multibyte
 	/* INC_POS or DEC_POS might have moved POS over LIM.  */
 	&& (forwardp ? (pos > XINT (lim)) : (pos < XINT (lim))))
       pos = XINT (lim);
+#endif
 
-    SET_PT (pos);
+    if (! multibyte)
+      pos_byte = pos;
+
+    SET_PT_BOTH (pos, pos_byte);
     immediate_quit = 0;
 
     return make_number (PT - start_point);
@@ -1408,6 +1482,7 @@
      Lisp_Object count;
 {
   register int from;
+  int from_byte;
   register int stop;
   register int c, c1;
   register enum syntaxcode code;
@@ -1415,6 +1490,7 @@
   int found;
   int count1;
   int temp_pos;
+  int out_charpos, out_bytepos;
 
   CHECK_NUMBER (count, 0);
   count1 = XINT (count);
@@ -1424,6 +1500,7 @@
   QUIT;
 
   from = PT;
+  from_byte = PT_BYTE;
 
   SETUP_SYNTAX_TABLE (from, count1);
   while (count1 > 0)
@@ -1432,17 +1509,20 @@
 	{
 	  if (from == stop)
 	    {
-	      SET_PT (from);
+	      if (! NILP (current_buffer->enable_multibyte_characters))
+		SET_PT_BOTH (from, from_byte);
+	      else
+		SET_PT_BOTH (from_byte, from_byte);
 	      immediate_quit = 0;
 	      return Qnil;
 	    }
 	  UPDATE_SYNTAX_TABLE_FORWARD (from);
-	  c = FETCH_CHAR (from);
+	  c = FETCH_CHAR (from_byte);
 	  code = SYNTAX (c);
-	  INC_POS (from);
+	  INC_BOTH (from, from_byte);
 	  comstyle = 0;
 	  if (from < stop && SYNTAX_COMSTART_FIRST (c)
-	      && (c1 = FETCH_CHAR (from),
+	      && (c1 = FETCH_CHAR (from_byte),
 		  SYNTAX_COMSTART_SECOND (c1)))
 	    {
 	      /* We have encountered a comment start sequence and we 
@@ -1452,15 +1532,19 @@
 		 the comment section.  */
 	      code = Scomment;
 	      comstyle = SYNTAX_COMMENT_STYLE (c1);
-	      INC_POS (from);
+	      INC_BOTH (from, from_byte);
 	    }
 	}
       while (code == Swhitespace || code == Sendcomment);
+
       if (code != Scomment && code != Scomment_fence)
 	{
 	  immediate_quit = 0;
-	  DEC_POS (from);
-	  SET_PT (from);
+	  DEC_BOTH (from, from_byte);
+	  if (! NILP (current_buffer->enable_multibyte_characters))
+	    SET_PT_BOTH (from, from_byte);
+	  else
+	    SET_PT_BOTH (from_byte, from_byte);
 	  return Qnil;
 	}
       /* We're at the start of a comment.  */
@@ -1469,12 +1553,15 @@
 	  if (from == stop)
 	    {
 	      immediate_quit = 0;
-	      SET_PT (from);
+	      if (! NILP (current_buffer->enable_multibyte_characters))
+		SET_PT_BOTH (from, from_byte);
+	      else
+		SET_PT_BOTH (from_byte, from_byte);
 	      return Qnil;
 	    }
 	  UPDATE_SYNTAX_TABLE_FORWARD (from);
-	  c = FETCH_CHAR (from);
-	  INC_POS (from);
+	  c = FETCH_CHAR (from_byte);
+	  INC_BOTH (from, from_byte);
 	  if (SYNTAX (c) == Sendcomment
 	      && SYNTAX_COMMENT_STYLE (c) == comstyle)
 	    /* we have encountered a comment end of the same style
@@ -1488,13 +1575,16 @@
 	       section.  */
 	    break;
 	  if (from < stop && SYNTAX_COMEND_FIRST (c)
-	      && (c1 = FETCH_CHAR (from),
+	      && (c1 = FETCH_CHAR (from_byte),
 		  SYNTAX_COMEND_SECOND (c1))
 	      && SYNTAX_COMMENT_STYLE (c) == comstyle)
 	    /* we have encountered a comment end of the same style
 	       as the comment sequence which began this comment
 	       section */
-	    { INC_POS (from); break; }
+	    {
+	      INC_BOTH (from, from_byte);
+	      break;
+	    }
 	}
       /* We have skipped one comment.  */
       count1--;
@@ -1507,60 +1597,64 @@
 	  int quoted;
 	  if (from <= stop)
 	    {
-	      SET_PT (stop);
+	      SET_PT_BOTH (BEGV, BEGV_BYTE);
 	      immediate_quit = 0;
 	      return Qnil;
 	    }
 
-	  DEC_POS (from);
-	  quoted = char_quoted (from);
+	  DEC_BOTH (from, from_byte);
+	  quoted = char_quoted (from, from_byte);
 	  if (quoted)
 	    {
-	      DEC_POS (from);
-	      goto leave;		/* ????? XXXXX */
+	      DEC_BOTH (from, from_byte);
+	      goto leave;
 	    }
 	  UPDATE_SYNTAX_TABLE_BACKWARD (from);
-	  c = FETCH_CHAR (from);
+	  c = FETCH_CHAR (from_byte);
 	  code = SYNTAX (c);
 	  comstyle = 0;
 	  if (code == Sendcomment)
 	    comstyle = SYNTAX_COMMENT_STYLE (c);
-	  temp_pos = from;
+	  temp_pos = from_byte;
 	  DEC_POS (temp_pos);
 	  if (from > stop && SYNTAX_COMEND_SECOND (c)
 	      && (c1 = FETCH_CHAR (temp_pos),
 		  SYNTAX_COMEND_FIRST (c1))
-	      && !char_quoted (temp_pos))
+	      && !char_quoted (from - 1, temp_pos))
 	    {
 	      /* We must record the comment style encountered so that
 		 later, we can match only the proper comment begin
 		 sequence of the same style.  */
 	      code = Sendcomment;
 	      comstyle = SYNTAX_COMMENT_STYLE (c1);
-	      from = temp_pos;
+	      DEC_BOTH (from, from_byte);
 	    }
 	  if (from > stop && SYNTAX_COMSTART_SECOND (c)
 	      && (c1 = FETCH_CHAR (temp_pos),
 		  SYNTAX_COMSTART_FIRST (c1))
-	      && !char_quoted (temp_pos))
+	      && !char_quoted (from - 1, temp_pos))
 	    {
 	      /* We must record the comment style encountered so that
 		 later, we can match only the proper comment begin
 		 sequence of the same style.  */
 	      code = Scomment;
-	      from = temp_pos;
+	      DEC_BOTH (from, from_byte);
 	    }
 
 	  if (code == Scomment_fence)
 	    {
 	      /* Skip until first preceding unquoted comment_fence.  */
-	      int found = 0, ini = from;
+	      int found = 0, ini = from, ini_byte = from_byte;
 	      
-	      while (--from != stop)
+	      while (1)
 		{
+		  DEC_BOTH (from, from_byte);
+		  if (from == stop)
+		    break;
 		  UPDATE_SYNTAX_TABLE_BACKWARD (from);
-		  c = FETCH_CHAR (from);
-		  if (SYNTAX (c) == Scomment_fence && !char_quoted (from)) 
+		  c = FETCH_CHAR (from_byte);
+		  if (SYNTAX (c) == Scomment_fence
+		      && !char_quoted (from, from_byte)) 
 		    {
 		      found = 1; 
 		      break;
@@ -1569,160 +1663,16 @@
 	      if (found == 0)
 		{
 		  from = ini;		/* Set point to ini + 1.  */
+		  from_byte = ini_byte;
 		  goto leave;
 		}
 	    }
 	  else if (code == Sendcomment)
 	    {
-#if 0
-	      if (code != SYNTAX (c))
-		/* For a two-char comment ender, we can assume
-		   it does end a comment.  So scan back in a simple way.  */
-		{
-		  if (from != stop) DEC_POS (from);
-		  while (1)
-		    {
-		      if ((c = FETCH_CHAR (from),
-			   SYNTAX (c) == Scomment)
-			  && SYNTAX_COMMENT_STYLE (c) == comstyle)
-			break;
-		      if (from == stop)
-			{
-			  immediate_quit = 0;
-			  SET_PT (from);
-			  return Qnil;
-			}
-		      DEC_POS (from);
-		      if (SYNTAX_COMSTART_SECOND (c)
-			  && (c1 = FETCH_CHAR (from),
-			      SYNTAX_COMSTART_FIRST (c1))
-			  && SYNTAX_COMMENT_STYLE (c) == comstyle
-			  && !char_quoted (from))
-			break;
-		    }
-		  break;
-		}
-#endif /* 0 */
-	      found = back_comment (from, stop, comstyle);
-	      if (found != -1) from = found;
-#if 0
-	      /* Look back, counting the parity of string-quotes,
-		 and recording the comment-starters seen.
-		 When we reach a safe place, assume that's not in a string;
-		 then step the main scan to the earliest comment-starter seen
-		 an even number of string quotes away from the safe place.
-
-		 OFROM[I] is position of the earliest comment-starter seen
-		 which is I+2X quotes from the comment-end.
-		 PARITY is current parity of quotes from the comment end.  */
-	      {
-		int parity = 0;
-		char my_stringend = 0;
-		int string_lossage = 0;
-		int comment_end = from;
-		int comstart_pos = 0;
-		int comstart_parity = 0;
-		int scanstart = from;
-
-		DEC_POS (scanstart);
-		/* At beginning of range to scan, we're outside of strings;
-		   that determines quote parity to the comment-end.  */
-		while (from != stop)
-		  {
-		    /* Move back and examine a character.  */
-		    DEC_POS (from);
-
-		    UPDATE_SYNTAX_TABLE_BACKWARD (from);
-		    c = FETCH_CHAR (from);
-		    code = SYNTAX (c);
-
-		    /* If this char is the second of a 2-char comment sequence,
-		       back up and give the pair the appropriate syntax.  */
-		    temp_pos = from;
-		    DEC_POS (temp_pos);
-		    if (from > stop && SYNTAX_COMEND_SECOND (c)
-			&& (c1 = FETCH_CHAR (temp_pos),
-			    SYNTAX_COMEND_FIRST (c1)))
-		      {
-			code = Sendcomment;
-			from = temp_pos;
-		        c = c1;
-		      }
-			
-		    temp_pos = from;
-		    INC_POS (temp_pos);
-		    /* If this char starts a 2-char comment start sequence,
-		       treat it like a 1-char comment starter.  */
-		    if (from < scanstart && SYNTAX_COMSTART_FIRST (c)
-			&& (c1 = FETCH_CHAR (temp_pos),
-			    SYNTAX_COMSTART_SECOND (c1))
-			&& comstyle == SYNTAX_COMMENT_STYLE (c1))
-		      code = Scomment;
-
-		    /* Ignore escaped characters.  */
-		    if (char_quoted (from))
-		      continue;
-
-		    /* Track parity of quotes.  */
-		    if (code == Sstring)
-		      {
-			parity ^= 1;
-			if (my_stringend == 0)
-			  my_stringend = c;
-			/* If we have two kinds of string delimiters.
-			   There's no way to grok this scanning backwards.  */
-			else if (my_stringend != c)
-			  string_lossage = 1;
-		      }
-
-		    /* Record comment-starters according to that
-		       quote-parity to the comment-end.  */
-		    if (code == Scomment)
-		      {
-			comstart_parity = parity;
-			comstart_pos = from;
-		      }
-
-		    /* If we find another earlier comment-ender,
-		       any comment-starts earlier than that don't count
-		       (because they go with the earlier comment-ender).  */
-		    if (code == Sendcomment
-			&& SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)) == comstyle)
-		      break;
-
-		    /* Assume a defun-start point is outside of strings.  */
-		    if (code == Sopen
-			&& (from == stop || FETCH_BYTE (from - 1) == '\n'))
-		      break;
-		  }
-
-		if (comstart_pos == 0)
-		  from = comment_end;
-		/* If the earliest comment starter
-		   is followed by uniform paired string quotes or none,
-		   we know it can't be inside a string
-		   since if it were then the comment ender would be inside one.
-		   So it does start a comment.  Skip back to it.  */
-		else if (comstart_parity == 0 && !string_lossage)
-		  from = comstart_pos;
-		else
-		  {
-		    /* We had two kinds of string delimiters mixed up
-		       together.  Decode this going forwards.
-		       Scan fwd from the previous comment ender
-		       to the one in question; this records where we
-		       last passed a comment starter.  */
-		    struct lisp_parse_state state;
-		    scan_sexps_forward (&state, find_defun_start (comment_end),
-					comment_end - 1, -10000, 0, Qnil, 0);
-		    if (state.incomment)
-		      from = state.comstr_start;
-		    else
-		      /* We can't grok this as a comment; scan it normally.  */
-		      from = comment_end;
-		  }
-	      }
-#endif /* 0 */
+	      found = back_comment (from, from_byte, stop, comstyle,
+				    &out_charpos, &out_bytepos);
+	      if (found != -1)
+		from = out_charpos, from_byte = out_bytepos;
 	      /* We have skipped one comment.  */
 	      break;
 	    }
@@ -1730,8 +1680,11 @@
 	    {
 	    leave:
 	      immediate_quit = 0;
-	      INC_POS (from);
-	      SET_PT (from);
+	      INC_BOTH (from, from_byte);
+	      if (! NILP (current_buffer->enable_multibyte_characters))
+		SET_PT_BOTH (from, from_byte);
+	      else
+		SET_PT_BOTH (from_byte, from_byte);
 	      return Qnil;
 	    }
 	}
@@ -1739,12 +1692,15 @@
       count1++;
     }
 
-  SET_PT (from);
+  if (! NILP (current_buffer->enable_multibyte_characters))
+    SET_PT_BOTH (from, from_byte);
+  else
+    SET_PT_BOTH (from_byte, from_byte);
   immediate_quit = 0;
   return Qt;
 }
 
-Lisp_Object
+static Lisp_Object
 scan_lists (from, count, depth, sexpflag)
      register int from;
      int count, depth, sexpflag;
@@ -1761,6 +1717,8 @@
   int temp_pos;
   int last_good = from;
   int found;
+  int from_byte = CHAR_TO_BYTE (from);
+  int out_bytepos, out_charpos;
 
   if (depth > 0) min_depth = 0;
 
@@ -1773,14 +1731,14 @@
       while (from < stop)
 	{
 	  UPDATE_SYNTAX_TABLE_FORWARD (from);
-	  c = FETCH_CHAR (from);
+	  c = FETCH_CHAR (from_byte);
 	  code = SYNTAX (c);
 	  if (depth == min_depth)
 	    last_good = from;
-	  INC_POS (from);
+	  INC_BOTH (from, from_byte);
 	  UPDATE_SYNTAX_TABLE_FORWARD (from);
 	  if (from < stop && SYNTAX_COMSTART_FIRST (c)
-	      && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from))
+	      && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte))
 	      && parse_sexp_ignore_comments)
 	    {
 	      /* we have encountered a comment start sequence and we 
@@ -1789,8 +1747,8 @@
 		 only a comment end of the same style actually ends
 		 the comment section */
 	      code = Scomment;
-	      comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from));
-	      INC_POS (from);
+	      comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
+	      INC_BOTH (from, from_byte);
 	    }
 	  
 	  UPDATE_SYNTAX_TABLE_FORWARD (from);
@@ -1802,7 +1760,7 @@
 	    case Sescape:
 	    case Scharquote:
 	      if (from == stop) goto lose;
-	      INC_POS (from);
+	      INC_BOTH (from, from_byte);
 	      /* treat following character as a word constituent */
 	    case Sword:
 	    case Ssymbol:
@@ -1811,11 +1769,11 @@
 	      while (from < stop)
 		{
 		  UPDATE_SYNTAX_TABLE_FORWARD (from);
-		  switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from))))
+		  switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from_byte))))
 		    {
 		    case Scharquote:
 		    case Sescape:
-		      INC_POS (from);
+		      INC_BOTH (from, from_byte);
 		      if (from == stop) goto lose;
 		      break;
 		    case Sword:
@@ -1825,7 +1783,7 @@
 		    default:
 		      goto done;
 		    }
-		  INC_POS (from);
+		  INC_BOTH (from, from_byte);
 		}
 	      goto done;
 
@@ -1841,7 +1799,7 @@
 		      goto lose;
 		    }
 		  UPDATE_SYNTAX_TABLE_FORWARD (from);
-		  c = FETCH_CHAR (from);
+		  c = FETCH_CHAR (from_byte);
 		  if (code == Scomment 
 		      ? (SYNTAX (c) == Sendcomment
 			 && SYNTAX_COMMENT_STYLE (c) == comstyle)
@@ -1850,23 +1808,28 @@
 		       as the comment sequence which began this comment
 		       section */
 		    break;
-		  INC_POS (from);
+		  INC_BOTH (from, from_byte);
 		  if (from < stop && SYNTAX_COMEND_FIRST (c)
-		      && SYNTAX_COMEND_SECOND (FETCH_CHAR (from))
+		      && SYNTAX_COMEND_SECOND (FETCH_CHAR (from_byte))
 		      && SYNTAX_COMMENT_STYLE (c) == comstyle
 		      && code == Scomment)
 		    /* we have encountered a comment end of the same style
 		       as the comment sequence which began this comment
 		       section */
-		    { INC_POS (from); break; }
+		    {
+		      INC_BOTH (from, from_byte);
+		      break;
+		    }
 		}
 	      break;
 
 	    case Smath:
 	      if (!sexpflag)
 		break;
-	      if (from != stop && c == FETCH_CHAR (from))
-		INC_POS (from);
+	      if (from != stop && c == FETCH_CHAR (from_byte))
+		{
+		  INC_BOTH (from, from_byte);
+		}
 	      if (mathexit)
 		{
 		  mathexit = 0;
@@ -1890,7 +1853,7 @@
 
 	    case Sstring:
 	    case Sstring_fence:
-	      temp_pos = from;
+	      temp_pos = from_byte;
 	      DEC_POS (temp_pos);
 	      stringterm = FETCH_CHAR (temp_pos);
 	      while (1)
@@ -1898,18 +1861,18 @@
 		  if (from >= stop) goto lose;
 		  UPDATE_SYNTAX_TABLE_FORWARD (from);
 		  if (code == Sstring 
-		      ? (FETCH_CHAR (from) == stringterm)
-		      : SYNTAX (FETCH_CHAR (from)) == Sstring_fence) 
+		      ? (FETCH_CHAR (from_byte) == stringterm)
+		      : SYNTAX (FETCH_CHAR (from_byte)) == Sstring_fence) 
 		    break;
-		  switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from))))
+		  switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from_byte))))
 		    {
 		    case Scharquote:
 		    case Sescape:
-		      INC_POS (from);
+		      INC_BOTH (from, from_byte);
 		    }
-		  INC_POS (from);
+		  INC_BOTH (from, from_byte);
 		}
-	      INC_POS (from);
+	      INC_BOTH (from, from_byte);
 	      if (!depth && sexpflag) goto done;
 	      break;
 	    }
@@ -1930,25 +1893,25 @@
     {
       while (from > stop)
 	{
-	  DEC_POS (from);
+	  DEC_BOTH (from, from_byte);
 	  UPDATE_SYNTAX_TABLE_BACKWARD (from);
-	  if (quoted = char_quoted (from))
+	  if (quoted = char_quoted (from, from_byte))
 	    {
-	      DEC_POS (from);
+	      DEC_BOTH (from, from_byte);
 	      UPDATE_SYNTAX_TABLE_BACKWARD (from);
 	    }
-	  c = FETCH_CHAR (from);
+	  c = FETCH_CHAR (from_byte);
 	  code = SYNTAX (c);
 	  if (depth == min_depth)
 	    last_good = from;
 	  comstyle = 0;
 	  if (code == Sendcomment)
 	    comstyle = SYNTAX_COMMENT_STYLE (c);
-	  temp_pos = from;
+	  temp_pos = from_byte;
 	  DEC_POS (temp_pos);
 	  if (from > stop && SYNTAX_COMEND_SECOND (c)
 	      && (c1 = FETCH_CHAR (temp_pos), SYNTAX_COMEND_FIRST (c1))
-	      && !char_quoted (temp_pos)
+	      && !char_quoted (from - 1, temp_pos)
 	      && parse_sexp_ignore_comments)
 	    {
 	      /* we must record the comment style encountered so that
@@ -1956,7 +1919,7 @@
 		 sequence of the same style */
 	      code = Sendcomment;
 	      comstyle = SYNTAX_COMMENT_STYLE (c1);
-	      from = temp_pos;
+	      DEC_BOTH (from, from_byte);
 	    }
 	  
 	  if (SYNTAX_PREFIX (c))
@@ -1971,15 +1934,15 @@
 		 after passing it.  */
 	      while (from > stop)
 		{
-		  temp_pos = from;
+		  temp_pos = from_byte;
 		  DEC_POS (temp_pos);
-		  UPDATE_SYNTAX_TABLE_BACKWARD (temp_pos);
-		  quoted = char_quoted (temp_pos);
+		  UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
+		  quoted = char_quoted (from - 1, temp_pos);
 		  if (quoted)
 		    {
-		      from = temp_pos;
+		      DEC_BOTH (from, from_byte);
 		      DEC_POS (temp_pos);
-		      UPDATE_SYNTAX_TABLE_BACKWARD (temp_pos);
+		      UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
 		    }
 		  c1 = FETCH_CHAR (temp_pos);
 		  temp_code = SYNTAX (c1);
@@ -1987,18 +1950,18 @@
 			 || temp_code == Ssymbol
 			 || temp_code == Squote))
             	    goto done2;
-		  from = temp_pos;
+		  DEC_BOTH (from, from_byte);
 		}
 	      goto done2;
 
 	    case Smath:
 	      if (!sexpflag)
 		break;
-	      temp_pos = from;
+	      temp_pos = from_byte;
 	      DEC_POS (temp_pos);
-	      UPDATE_SYNTAX_TABLE_BACKWARD (temp_pos);
+	      UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
 	      if (from != stop && c == FETCH_CHAR (temp_pos))
-		from = temp_pos;
+		DEC_BOTH (from, from_byte);
 	      if (mathexit)
 		{
 		  mathexit = 0;
@@ -2023,183 +1986,40 @@
 	    case Sendcomment:
 	      if (!parse_sexp_ignore_comments)
 		break;
-#if 0
-	      if (code != SYNTAX (c))
-		/* For a two-char comment ender, we can assume
-		   it does end a comment.  So scan back in a simple way.  */
-		{
-		  if (from != stop) DEC_POS (from);
-		  while (1)
-		    {
-		      if (SYNTAX (c = FETCH_CHAR (from)) == Scomment
-			  && SYNTAX_COMMENT_STYLE (c) == comstyle)
-			break;
-		      if (from == stop)
-			{
-			  if (depth == 0)
-			    goto done2;
-			  goto lose;
-			}
-		      DEC_POS (from);
-		      if (SYNTAX_COMSTART_SECOND (c)
-			  && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from))
-			  && SYNTAX_COMMENT_STYLE (c) == comstyle
-			  && !char_quoted (from))
-			break;
-		    }
-		  break;
-		}
-#endif /* 0 */
-	      found = back_comment (from, stop, comstyle);
-	      if (found != -1) from = found;
-#if 0
-	      /* Look back, counting the parity of string-quotes,
-		 and recording the comment-starters seen.
-		 When we reach a safe place, assume that's not in a string;
-		 then step the main scan to the earliest comment-starter seen
-		 an even number of string quotes away from the safe place.
-
-		 OFROM[I] is position of the earliest comment-starter seen
-		 which is I+2X quotes from the comment-end.
-		 PARITY is current parity of quotes from the comment end.  */
-	      {
-		int parity = 0;
-		char my_stringend = 0;
-		int string_lossage = 0;
-		int comment_end = from;
-		int comstart_pos = 0;
-		int comstart_parity = 0;
-		int scanstart = from;
-
-		DEC_POS (scanstart);
-
-		/* At beginning of range to scan, we're outside of strings;
-		   that determines quote parity to the comment-end.  */
-		while (from != stop)
-		  {
-		    /* Move back and examine a character.  */
-		    DEC_POS (from);
-
-		    c = FETCH_CHAR (from);
-		    code = SYNTAX (c);
-
-		    /* If this char is the second of a 2-char comment sequence,
-		       back up and give the pair the appropriate syntax.  */
-		    temp_pos = from;
-		    DEC_POS (temp_pos);
-		    if (from > stop && SYNTAX_COMEND_SECOND (c)
-			&& (c1 = FETCH_CHAR (temp_pos),
-			    SYNTAX_COMEND_FIRST (c1)))
-		      {
-			code = Sendcomment;
-			from = temp_pos;
-		        c = c1;
-		      }
-			
-		    /* If this char starts a 2-char comment start sequence,
-		       treat it like a 1-char comment starter.  */
-		    temp_pos = from;
-		    INC_POS (temp_pos);
-		    if (from < scanstart && SYNTAX_COMSTART_FIRST (c)
-			&& (c1 = FETCH_CHAR (temp_pos),
-			    SYNTAX_COMSTART_SECOND (c1))
-			&& comstyle == SYNTAX_COMMENT_STYLE (c1))
-		      code = Scomment;
-
-		    /* Ignore escaped characters.  */
-		    if (char_quoted (from))
-		      continue;
-
-		    /* Track parity of quotes.  */
-		    if (code == Sstring)
-		      {
-			parity ^= 1;
-			if (my_stringend == 0)
-			  my_stringend = c;
-			/* If we have two kinds of string delimiters.
-			   There's no way to grok this scanning backwards.  */
-			else if (my_stringend != c)
-			  string_lossage = 1;
-		      }
-
-		    /* Record comment-starters according to that
-		       quote-parity to the comment-end.  */
-		    if (code == Scomment)
-		      {
-			comstart_parity = parity;
-			comstart_pos = from;
-		      }
-
-		    /* If we find another earlier comment-ender,
-		       any comment-starts earlier than that don't count
-		       (because they go with the earlier comment-ender).  */
-		    if (code == Sendcomment
-			&& SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)) == comstyle)
-		      break;
-
-		    /* Assume a defun-start point is outside of strings.  */
-		    if (code == Sopen
-			&& (from == stop || FETCH_BYTE (from - 1) == '\n'))
-		      break;
-		  }
-
-		if (comstart_pos == 0)
-		  from = comment_end;
-		/* If the earliest comment starter
-		   is followed by uniform paired string quotes or none,
-		   we know it can't be inside a string
-		   since if it were then the comment ender would be inside one.
-		   So it does start a comment.  Skip back to it.  */
-		else if (comstart_parity == 0 && !string_lossage)
-		  from = comstart_pos;
-		else
-		  {
-		    /* We had two kinds of string delimiters mixed up
-		       together.  Decode this going forwards.
-		       Scan fwd from the previous comment ender
-		       to the one in question; this records where we
-		       last passed a comment starter.  */
-		    struct lisp_parse_state state;
-		    scan_sexps_forward (&state, find_defun_start (comment_end),
-					comment_end - 1, -10000, 0, Qnil, 0);
-		    if (state.incomment)
-		      from = state.comstr_start;
-		    else
-		      /* We can't grok this as a comment; scan it normally.  */
-		      from = comment_end;
-		  }
-	      }
-#endif /* 0 */
+	      found = back_comment (from, from_byte, stop, comstyle,
+				    &out_charpos, &out_bytepos);
+	      if (found != -1)
+		from = out_charpos, from_byte = out_bytepos;
 	      break;
 
 	    case Scomment_fence:
 	    case Sstring_fence:
 	      while (1)
 		{
-		  DEC_POS (from);
+		  DEC_BOTH (from, from_byte);
 		  if (from == stop) goto lose;
 		  UPDATE_SYNTAX_TABLE_BACKWARD (from);
-		  if (!char_quoted (from) 
-		      && SYNTAX (FETCH_CHAR (from)) == code)
+		  if (!char_quoted (from, from_byte) 
+		      && SYNTAX (FETCH_CHAR (from_byte)) == code)
 		    break;
 		}
 	      if (code == Sstring_fence && !depth && sexpflag) goto done2;
 	      break;
 	      
 	    case Sstring:
-	      stringterm = FETCH_CHAR (from);
+	      stringterm = FETCH_CHAR (from_byte);
 	      while (1)
 		{
 		  if (from == stop) goto lose;
-		  temp_pos = from;
+		  temp_pos = from_byte;
 		  DEC_POS (temp_pos);
-		  UPDATE_SYNTAX_TABLE_BACKWARD (temp_pos);
-		  if (!char_quoted (temp_pos)
+		  UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
+		  if (!char_quoted (from - 1, temp_pos)
 		      && stringterm == FETCH_CHAR (temp_pos))
 		    break;
-		  from = temp_pos;
+		  DEC_BOTH (from, from_byte);
 		}
-	      DEC_POS (from);
+	      DEC_BOTH (from, from_byte);
 	      if (!depth && sexpflag) goto done2;
 	      break;
 	    }
@@ -2280,31 +2100,33 @@
   ()
 {
   int beg = BEGV;
+  int opoint = PT;
+  int opoint_byte = PT_BYTE;
   int pos = PT;
+  int pos_byte = PT_BYTE;
   int c;
-  int temp_pos = pos;
 
   if (pos > beg) 
     {
       SETUP_SYNTAX_TABLE (pos, -1);
     }
-  DEC_POS (temp_pos);
+
+  DEC_BOTH (pos, pos_byte);
 
-  while (pos > beg && !char_quoted (temp_pos)
+  while (pos + 1 > beg && !char_quoted (pos, pos_byte)
 	 /* Previous statement updates syntax table.  */
-	 && ((c = FETCH_CHAR (temp_pos), SYNTAX (c) == Squote)
+	 && ((c = FETCH_CHAR (pos_byte), SYNTAX (c) == Squote)
 	     || SYNTAX_PREFIX (c)))
     {
-      pos = temp_pos;
-      DEC_POS (temp_pos);
+      DEC_BOTH (pos, pos_byte);
     }
 
-  SET_PT (pos);
+  SET_PT_BOTH (opoint, opoint_byte);
 
   return Qnil;
 }
 
-/* Parse forward from FROM to END,
+/* Parse forward from FROM / FROM_BYTE to END,
    assuming that FROM has state OLDSTATE (nil means FROM is start of function),
    and return a description of the state of the parse at END.
    If STOPBEFORE is nonzero, stop at the start of an atom.
@@ -2313,7 +2135,7 @@
    after the beginning of a string, or after the end of a string.  */
 
 static void
-scan_sexps_forward (stateptr, from, end, targetdepth,
+scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
 		    stopbefore, oldstate, commentstop)
      struct lisp_parse_state *stateptr;
      register int from;
@@ -2336,14 +2158,22 @@
   int start_quoted = 0;		/* Nonzero means starting after a char quote */
   Lisp_Object tem;
   int prev_from;		/* Keep one character before FROM.  */
+  int prev_from_byte;
   int boundary_stop = commentstop == -1;
   int nofence;
 
   prev_from = from;
-  DEC_POS (prev_from);
+  prev_from_byte = from_byte;
+  if (from != BEGV)
+    DEC_BOTH (prev_from, prev_from_byte);
 
   /* Use this macro instead of `from++'.  */
-#define INC_FROM do { prev_from = from; INC_POS (from); } while (0)
+#define INC_FROM				\
+do { prev_from = from;				\
+     prev_from_byte = from_byte; 		\
+     from++;					\
+     INC_POS (from_byte);			\
+  } while (0)
 
   immediate_quit = 1;
   QUIT;
@@ -2415,7 +2245,7 @@
   while (from < end)
     {
       UPDATE_SYNTAX_TABLE_FORWARD (from);
-      code = SYNTAX (FETCH_CHAR (from));
+      code = SYNTAX (FETCH_CHAR (from_byte));
       INC_FROM;
 
       if (code == Scomment)
@@ -2427,14 +2257,14 @@
 	     terminates the comment section.  */
 	  state.comstyle = ( code == Scomment_fence 
 			     ? ST_COMMENT_STYLE 
-			     : SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)));
+			     : SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)));
 	  state.comstr_start = prev_from;
 	  if (code != Scomment_fence) INC_FROM;
 	  code = Scomment;
 	}
      else if (from < end)
-	if (SYNTAX_COMSTART_FIRST (FETCH_CHAR (prev_from)))
-	  if (SYNTAX_COMSTART_SECOND (FETCH_CHAR (from)))
+	if (SYNTAX_COMSTART_FIRST (FETCH_CHAR (prev_from_byte)))
+	  if (SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte)))
 	    /* Duplicate code to avoid a very complex if-expression
 	       which causes trouble for the SGI compiler.  */
 	    {
@@ -2443,13 +2273,13 @@
 		 terminates the comment section.  */
 	      state.comstyle = ( code == Scomment_fence 
 				 ? ST_COMMENT_STYLE 
-				 : SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)));
+				 : SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)));
 	      state.comstr_start = prev_from;
 	      if (code != Scomment_fence) INC_FROM;
 	      code = Scomment;
 	    }
 
-      if (SYNTAX_PREFIX (FETCH_CHAR (prev_from)))
+      if (SYNTAX_PREFIX (FETCH_CHAR (prev_from_byte)))
 	continue;
       switch (SWITCH_ENUM_CAST (code))
 	{
@@ -2470,7 +2300,7 @@
 	  while (from < end)
 	    {
 	      UPDATE_SYNTAX_TABLE_FORWARD (from);
-	      switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from))))
+	      switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from_byte))))
 		{
 		case Scharquote:
 		case Sescape:
@@ -2497,7 +2327,7 @@
 	    {
 	      /* Enter the loop in the middle so that we find
 		 a 2-char comment ender if we start in the middle of it.  */
-	      prev = FETCH_CHAR (prev_from);
+	      prev = FETCH_CHAR (prev_from_byte);
 	      goto startincomment_1;
 	    }
 	  /* At beginning of buffer, enter the loop the ordinary way.  */
@@ -2512,7 +2342,7 @@
 	    {
 	      if (from == end) goto done;
 	      UPDATE_SYNTAX_TABLE_FORWARD (from);
-	      prev = FETCH_CHAR (from);
+	      prev = FETCH_CHAR (from_byte);
 	      if (SYNTAX (prev) == Sendcomment
 		  && SYNTAX_COMMENT_STYLE (prev) == state.comstyle)
 		/* Only terminate the comment section if the endcomment
@@ -2525,12 +2355,12 @@
 	      INC_FROM;
 	    startincomment_1:
 	      if (from < end && SYNTAX_COMEND_FIRST (prev)
-		  && SYNTAX_COMEND_SECOND (FETCH_CHAR (from))
+		  && SYNTAX_COMEND_SECOND (FETCH_CHAR (from_byte))
 		  && SYNTAX_COMMENT_STYLE (prev) == state.comstyle)
 		/* Only terminate the comment section if the end-comment
 		   sequence of the same style as the start sequence has
 		   been encountered.  */
-		{ break; }
+		break;
 	    }
 	  INC_FROM; 
 	  state.incomment = 0;
@@ -2566,7 +2396,7 @@
 	  if (stopbefore) goto stop;  /* this arg means stop at sexp start */
 	  curlevel->last = prev_from;
 	  state.instring = (code == Sstring 
-			    ? (FETCH_CHAR (prev_from))
+			    ? (FETCH_CHAR (prev_from_byte))
 			    : ST_STRING_STYLE);
 	  if (boundary_stop) goto done;
 	startinstring:
@@ -2578,7 +2408,7 @@
 		int c;
 
 		if (from >= end) goto done;
-		c = FETCH_CHAR (from);
+		c = FETCH_CHAR (from_byte);
 		if (nofence && c == state.instring) break;
 		UPDATE_SYNTAX_TABLE_FORWARD (from);
 		switch (SWITCH_ENUM_CAST (SYNTAX (c)))
@@ -2679,7 +2509,8 @@
     target = -100000;		/* We won't reach this depth */
 
   validate_region (&from, &to);
-  scan_sexps_forward (&state, XINT (from), XINT (to),
+  scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
+		      XINT (to),
 		      target, !NILP (stopbefore), oldstate,
 		      (NILP (commentstop) 
 		       ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));