Commit f902a008 authored by Richard M. Stallman's avatar Richard M. Stallman

(scan_lists): Properly skip the comment-fence character

that ends a comment, when moving forward.
(Fbackward_prefix_chars): Return immediately if point is at BEGV.

(prev_char_comend_first): New function.
(back_comment): Use that.  Carefully update syntax table position
for each character that is fetched.
(Fforward_comment): Likewise.
(scan_lists): Likewise.
(prev_char_comstart_first): New function.
parent eb148b90
...@@ -130,7 +130,7 @@ update_syntax_table (charpos, count, init, object) ...@@ -130,7 +130,7 @@ update_syntax_table (charpos, count, init, object)
invalidate = 0; invalidate = 0;
if (NULL_INTERVAL_P (i)) if (NULL_INTERVAL_P (i))
return; return;
/* interval_of () updates only ->position of the return value, /* interval_of updates only ->position of the return value, so
update the parents manually to speed up update_interval. */ update the parents manually to speed up update_interval. */
while (!NULL_PARENT (i)) while (!NULL_PARENT (i))
{ {
...@@ -158,7 +158,7 @@ update_syntax_table (charpos, count, init, object) ...@@ -158,7 +158,7 @@ update_syntax_table (charpos, count, init, object)
else if (charpos < i->position) /* Move left. */ else if (charpos < i->position) /* Move left. */
{ {
if (count > 0) if (count > 0)
error ("Error in syntax_table logic for intervals <-."); error ("Error in syntax_table logic for intervals <-");
/* Update the interval. */ /* Update the interval. */
i = update_interval (i, charpos); i = update_interval (i, charpos);
if (oldi->position != INTERVAL_LAST_POS (i)) if (oldi->position != INTERVAL_LAST_POS (i))
...@@ -172,7 +172,7 @@ update_syntax_table (charpos, count, init, object) ...@@ -172,7 +172,7 @@ update_syntax_table (charpos, count, init, object)
else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */ else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
{ {
if (count < 0) if (count < 0)
error ("Error in syntax_table logic for intervals ->."); error ("Error in syntax_table logic for intervals ->");
/* Update the interval. */ /* Update the interval. */
i = update_interval (i, charpos); i = update_interval (i, charpos);
if (i->position != INTERVAL_LAST_POS (oldi)) if (i->position != INTERVAL_LAST_POS (oldi))
...@@ -394,6 +394,38 @@ find_defun_start (pos, pos_byte) ...@@ -394,6 +394,38 @@ find_defun_start (pos, pos_byte)
return find_start_value; return find_start_value;
} }
/* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
static int
prev_char_comend_first (pos, pos_byte)
int pos, pos_byte;
{
int c, val;
DEC_BOTH (pos, pos_byte);
UPDATE_SYNTAX_TABLE_BACKWARD (pos);
c = FETCH_CHAR (pos_byte);
val = SYNTAX_COMEND_FIRST (c);
UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
return val;
}
/* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
static int
prev_char_comstart_first (pos, pos_byte)
int pos, pos_byte;
{
int c, val;
DEC_BOTH (pos, pos_byte);
UPDATE_SYNTAX_TABLE_BACKWARD (pos);
c = FETCH_CHAR (pos_byte);
val = SYNTAX_COMSTART_FIRST (c);
UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
return val;
}
/* Checks whether charpos FROM is at the end of a comment. /* Checks whether charpos FROM is at the end of a comment.
FROM_BYTE is the bytepos corresponding to FROM. FROM_BYTE is the bytepos corresponding to FROM.
Do not move back before STOP. Do not move back before STOP.
...@@ -444,7 +476,7 @@ back_comment (from, from_byte, stop, comstyle, charpos_ptr, bytepos_ptr) ...@@ -444,7 +476,7 @@ back_comment (from, from_byte, stop, comstyle, charpos_ptr, bytepos_ptr)
that determines quote parity to the comment-end. */ that determines quote parity to the comment-end. */
while (from != stop) while (from != stop)
{ {
int temp_byte; int temp_byte, prev_comend_second;
/* Move back and examine a character. */ /* Move back and examine a character. */
DEC_BOTH (from, from_byte); DEC_BOTH (from, from_byte);
...@@ -456,23 +488,25 @@ back_comment (from, from_byte, stop, comstyle, charpos_ptr, bytepos_ptr) ...@@ -456,23 +488,25 @@ back_comment (from, from_byte, stop, comstyle, charpos_ptr, bytepos_ptr)
/* If this char is the second of a 2-char comment end sequence, /* If this char is the second of a 2-char comment end sequence,
back up and give the pair the appropriate syntax. */ back up and give the pair the appropriate syntax. */
if (from > stop && SYNTAX_COMEND_SECOND (c) if (from > stop && SYNTAX_COMEND_SECOND (c)
&& (temp_byte = dec_bytepos (from_byte), && prev_char_comend_first (from, from_byte))
SYNTAX_COMEND_FIRST (FETCH_CHAR (temp_byte))))
{ {
code = Sendcomment; code = Sendcomment;
DEC_BOTH (from, from_byte); DEC_BOTH (from, from_byte);
/* This is apparently the best we can do: */
UPDATE_SYNTAX_TABLE_BACKWARD (from); UPDATE_SYNTAX_TABLE_BACKWARD (from);
c = FETCH_CHAR (from_byte); c = FETCH_CHAR (from_byte);
} }
/* If this char starts a 2-char comment start sequence, /* If this char starts a 2-char comment start sequence,
treat it like a 1-char comment starter. */ treat it like a 1-char comment starter. */
if (from < scanstart && SYNTAX_COMSTART_FIRST (c) if (from < scanstart && SYNTAX_COMSTART_FIRST (c))
&& (temp_byte = inc_bytepos (from_byte), {
(SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte)) temp_byte = inc_bytepos (from_byte);
&& comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte))))) UPDATE_SYNTAX_TABLE_FORWARD (from + 1);
code = Scomment; if (SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte))
&& comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte)))
code = Scomment;
UPDATE_SYNTAX_TABLE_BACKWARD (from);
}
/* Ignore escaped characters, except comment-enders. */ /* Ignore escaped characters, except comment-enders. */
if (code != Sendcomment && char_quoted (from, from_byte)) if (code != Sendcomment && char_quoted (from, from_byte))
...@@ -1590,6 +1624,8 @@ between them, return t; otherwise return nil.") ...@@ -1590,6 +1624,8 @@ between them, return t; otherwise return nil.")
{ {
do do
{ {
int comstart_first;
if (from == stop) if (from == stop)
{ {
SET_PT_BOTH (from, from_byte); SET_PT_BOTH (from, from_byte);
...@@ -1599,9 +1635,11 @@ between them, return t; otherwise return nil.") ...@@ -1599,9 +1635,11 @@ between them, return t; otherwise return nil.")
UPDATE_SYNTAX_TABLE_FORWARD (from); UPDATE_SYNTAX_TABLE_FORWARD (from);
c = FETCH_CHAR (from_byte); c = FETCH_CHAR (from_byte);
code = SYNTAX (c); code = SYNTAX (c);
comstart_first = SYNTAX_COMSTART_FIRST (c);
INC_BOTH (from, from_byte); INC_BOTH (from, from_byte);
UPDATE_SYNTAX_TABLE_FORWARD (from);
comstyle = 0; comstyle = 0;
if (from < stop && SYNTAX_COMSTART_FIRST (c) if (from < stop && comstart_first
&& (c1 = FETCH_CHAR (from_byte), && (c1 = FETCH_CHAR (from_byte),
SYNTAX_COMSTART_SECOND (c1))) SYNTAX_COMSTART_SECOND (c1)))
{ {
...@@ -1649,9 +1687,10 @@ between them, return t; otherwise return nil.") ...@@ -1649,9 +1687,10 @@ between them, return t; otherwise return nil.")
section. */ section. */
break; break;
if (from < stop && SYNTAX_COMEND_FIRST (c) if (from < stop && SYNTAX_COMEND_FIRST (c)
&& SYNTAX_COMMENT_STYLE (c) == comstyle
&& (c1 = FETCH_CHAR (from_byte), && (c1 = FETCH_CHAR (from_byte),
SYNTAX_COMEND_SECOND (c1)) UPDATE_SYNTAX_TABLE_FORWARD (from),
&& SYNTAX_COMMENT_STYLE (c) == comstyle) SYNTAX_COMEND_SECOND (c1)))
/* we have encountered a comment end of the same style /* we have encountered a comment end of the same style
as the comment sequence which began this comment as the comment sequence which began this comment
section */ section */
...@@ -1668,7 +1707,8 @@ between them, return t; otherwise return nil.") ...@@ -1668,7 +1707,8 @@ between them, return t; otherwise return nil.")
{ {
while (1) while (1)
{ {
int quoted; int quoted, comstart_second;
if (from <= stop) if (from <= stop)
{ {
SET_PT_BOTH (BEGV, BEGV_BYTE); SET_PT_BOTH (BEGV, BEGV_BYTE);
...@@ -1677,34 +1717,34 @@ between them, return t; otherwise return nil.") ...@@ -1677,34 +1717,34 @@ between them, return t; otherwise return nil.")
} }
DEC_BOTH (from, from_byte); DEC_BOTH (from, from_byte);
/* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
quoted = char_quoted (from, from_byte); quoted = char_quoted (from, from_byte);
if (quoted) if (quoted)
{ {
DEC_BOTH (from, from_byte); DEC_BOTH (from, from_byte);
goto leave; goto leave;
} }
UPDATE_SYNTAX_TABLE_BACKWARD (from);
c = FETCH_CHAR (from_byte); c = FETCH_CHAR (from_byte);
code = SYNTAX (c); code = SYNTAX (c);
comstyle = 0; comstyle = 0;
if (code == Sendcomment) if (code == Sendcomment)
comstyle = SYNTAX_COMMENT_STYLE (c); comstyle = SYNTAX_COMMENT_STYLE (c);
temp_pos = dec_bytepos (from_byte); comstart_second = SYNTAX_COMSTART_SECOND (c);
if (from > stop && SYNTAX_COMEND_SECOND (c) if (from > stop && SYNTAX_COMEND_SECOND (c)
&& (c1 = FETCH_CHAR (temp_pos), && prev_char_comend_first (from, from_byte)
SYNTAX_COMEND_FIRST (c1))
&& !char_quoted (from - 1, temp_pos)) && !char_quoted (from - 1, temp_pos))
{ {
/* We must record the comment style encountered so that /* We must record the comment style encountered so that
later, we can match only the proper comment begin later, we can match only the proper comment begin
sequence of the same style. */ sequence of the same style. */
code = Sendcomment;
comstyle = SYNTAX_COMMENT_STYLE (c1);
DEC_BOTH (from, from_byte); DEC_BOTH (from, from_byte);
code = Sendcomment;
/* Calling char_quoted, above, set up global syntax position
at the new value of FROM. */
comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
} }
if (from > stop && SYNTAX_COMSTART_SECOND (c) if (from > stop && comstart_second
&& (c1 = FETCH_CHAR (temp_pos), && prev_char_comstart_first (from, from_byte)
SYNTAX_COMSTART_FIRST (c1))
&& !char_quoted (from - 1, temp_pos)) && !char_quoted (from - 1, temp_pos))
{ {
/* We must record the comment style encountered so that /* We must record the comment style encountered so that
...@@ -1798,14 +1838,17 @@ scan_lists (from, count, depth, sexpflag) ...@@ -1798,14 +1838,17 @@ scan_lists (from, count, depth, sexpflag)
{ {
while (from < stop) while (from < stop)
{ {
int comstart_first, prefix;
UPDATE_SYNTAX_TABLE_FORWARD (from); UPDATE_SYNTAX_TABLE_FORWARD (from);
c = FETCH_CHAR (from_byte); c = FETCH_CHAR (from_byte);
code = SYNTAX (c); code = SYNTAX (c);
comstart_first = SYNTAX_COMSTART_FIRST (c);
prefix = SYNTAX_PREFIX (c);
if (depth == min_depth) if (depth == min_depth)
last_good = from; last_good = from;
INC_BOTH (from, from_byte); INC_BOTH (from, from_byte);
UPDATE_SYNTAX_TABLE_FORWARD (from); UPDATE_SYNTAX_TABLE_FORWARD (from);
if (from < stop && SYNTAX_COMSTART_FIRST (c) if (from < stop && comstart_first
&& SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte)) && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte))
&& parse_sexp_ignore_comments) && parse_sexp_ignore_comments)
{ {
...@@ -1817,10 +1860,10 @@ scan_lists (from, count, depth, sexpflag) ...@@ -1817,10 +1860,10 @@ scan_lists (from, count, depth, sexpflag)
code = Scomment; code = Scomment;
comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)); comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
INC_BOTH (from, from_byte); INC_BOTH (from, from_byte);
UPDATE_SYNTAX_TABLE_FORWARD (from);
} }
UPDATE_SYNTAX_TABLE_FORWARD (from); if (prefix)
if (SYNTAX_PREFIX (c))
continue; continue;
switch (SWITCH_ENUM_CAST (code)) switch (SWITCH_ENUM_CAST (code))
...@@ -1871,6 +1914,7 @@ scan_lists (from, count, depth, sexpflag) ...@@ -1871,6 +1914,7 @@ scan_lists (from, count, depth, sexpflag)
} }
UPDATE_SYNTAX_TABLE_FORWARD (from); UPDATE_SYNTAX_TABLE_FORWARD (from);
c = FETCH_CHAR (from_byte); c = FETCH_CHAR (from_byte);
INC_BOTH (from, from_byte);
if (code == Scomment if (code == Scomment
? (SYNTAX (c) == Sendcomment ? (SYNTAX (c) == Sendcomment
&& SYNTAX_COMMENT_STYLE (c) == comstyle) && SYNTAX_COMMENT_STYLE (c) == comstyle)
...@@ -1879,10 +1923,10 @@ scan_lists (from, count, depth, sexpflag) ...@@ -1879,10 +1923,10 @@ scan_lists (from, count, depth, sexpflag)
as the comment sequence which began this comment as the comment sequence which began this comment
section */ section */
break; break;
INC_BOTH (from, from_byte);
if (from < stop && SYNTAX_COMEND_FIRST (c) if (from < stop && SYNTAX_COMEND_FIRST (c)
&& SYNTAX_COMEND_SECOND (FETCH_CHAR (from_byte))
&& SYNTAX_COMMENT_STYLE (c) == comstyle && SYNTAX_COMMENT_STYLE (c) == comstyle
&& (UPDATE_SYNTAX_TABLE_FORWARD (from),
SYNTAX_COMEND_SECOND (FETCH_CHAR (from_byte)))
&& code == Scomment) && code == Scomment)
/* we have encountered a comment end of the same style /* we have encountered a comment end of the same style
as the comment sequence which began this comment as the comment sequence which began this comment
...@@ -1975,25 +2019,22 @@ scan_lists (from, count, depth, sexpflag) ...@@ -1975,25 +2019,22 @@ scan_lists (from, count, depth, sexpflag)
comstyle = 0; comstyle = 0;
if (code == Sendcomment) if (code == Sendcomment)
comstyle = SYNTAX_COMMENT_STYLE (c); comstyle = SYNTAX_COMMENT_STYLE (c);
temp_pos = from_byte;
if (! NILP (current_buffer->enable_multibyte_characters))
DEC_POS (temp_pos);
else
temp_pos--;
if (from > stop && SYNTAX_COMEND_SECOND (c) if (from > stop && SYNTAX_COMEND_SECOND (c)
&& (c1 = FETCH_CHAR (temp_pos), SYNTAX_COMEND_FIRST (c1)) && prev_char_comstart_first (from, from_byte)
&& parse_sexp_ignore_comments) && parse_sexp_ignore_comments)
{ {
/* we must record the comment style encountered so that /* We must record the comment style encountered so that
later, we can match only the proper comment begin later, we can match only the proper comment begin
sequence of the same style */ sequence of the same style. */
code = Sendcomment;
comstyle = SYNTAX_COMMENT_STYLE (c1);
DEC_BOTH (from, from_byte); DEC_BOTH (from, from_byte);
UPDATE_SYNTAX_TABLE_BACKWARD (from);
code = Sendcomment;
comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
} }
/* Quoting turns anything except a comment-ender /* Quoting turns anything except a comment-ender
into a word character. */ into a word character. Note that this if cannot be true
if we decremented FROM in the if-statement above. */
if (code != Sendcomment && char_quoted (from, from_byte)) if (code != Sendcomment && char_quoted (from, from_byte))
code = Sword; code = Sword;
else if (SYNTAX_PREFIX (c)) else if (SYNTAX_PREFIX (c))
...@@ -2192,11 +2233,15 @@ This includes chars with \"quote\" or \"prefix\" syntax (' or p).") ...@@ -2192,11 +2233,15 @@ This includes chars with \"quote\" or \"prefix\" syntax (' or p).")
int pos_byte = PT_BYTE; int pos_byte = PT_BYTE;
int c; int c;
if (pos > beg) if (pos <= beg)
{ {
SETUP_SYNTAX_TABLE (pos, -1); SET_PT_BOTH (opoint, opoint_byte);
return Qnil;
} }
SETUP_SYNTAX_TABLE (pos, -1);
DEC_BOTH (pos, pos_byte); DEC_BOTH (pos, pos_byte);
while (!char_quoted (pos, pos_byte) while (!char_quoted (pos, pos_byte)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment