aboutsummaryrefslogtreecommitdiffstats
path: root/src/syntax.c
diff options
context:
space:
mode:
authorRichard M. Stallman1998-06-06 20:20:13 +0000
committerRichard M. Stallman1998-06-06 20:20:13 +0000
commitf902a008322cdabe1f43e7dbbf29cff545aa0895 (patch)
treec35222daf04953ed18318a477cd5e78050f27eda /src/syntax.c
parenteb148b90e292a4793c94aa268bbd1adb840cb0c4 (diff)
downloademacs-f902a008322cdabe1f43e7dbbf29cff545aa0895.tar.gz
emacs-f902a008322cdabe1f43e7dbbf29cff545aa0895.zip
(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.
Diffstat (limited to 'src/syntax.c')
-rw-r--r--src/syntax.c131
1 files changed, 88 insertions, 43 deletions
diff --git a/src/syntax.c b/src/syntax.c
index d043e115565..efa4bf3d271 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -130,7 +130,7 @@ update_syntax_table (charpos, count, init, object)
130 invalidate = 0; 130 invalidate = 0;
131 if (NULL_INTERVAL_P (i)) 131 if (NULL_INTERVAL_P (i))
132 return; 132 return;
133 /* interval_of () updates only ->position of the return value, 133 /* interval_of updates only ->position of the return value, so
134 update the parents manually to speed up update_interval. */ 134 update the parents manually to speed up update_interval. */
135 while (!NULL_PARENT (i)) 135 while (!NULL_PARENT (i))
136 { 136 {
@@ -158,7 +158,7 @@ update_syntax_table (charpos, count, init, object)
158 else if (charpos < i->position) /* Move left. */ 158 else if (charpos < i->position) /* Move left. */
159 { 159 {
160 if (count > 0) 160 if (count > 0)
161 error ("Error in syntax_table logic for intervals <-."); 161 error ("Error in syntax_table logic for intervals <-");
162 /* Update the interval. */ 162 /* Update the interval. */
163 i = update_interval (i, charpos); 163 i = update_interval (i, charpos);
164 if (oldi->position != INTERVAL_LAST_POS (i)) 164 if (oldi->position != INTERVAL_LAST_POS (i))
@@ -172,7 +172,7 @@ update_syntax_table (charpos, count, init, object)
172 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */ 172 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
173 { 173 {
174 if (count < 0) 174 if (count < 0)
175 error ("Error in syntax_table logic for intervals ->."); 175 error ("Error in syntax_table logic for intervals ->");
176 /* Update the interval. */ 176 /* Update the interval. */
177 i = update_interval (i, charpos); 177 i = update_interval (i, charpos);
178 if (i->position != INTERVAL_LAST_POS (oldi)) 178 if (i->position != INTERVAL_LAST_POS (oldi))
@@ -394,6 +394,38 @@ find_defun_start (pos, pos_byte)
394 return find_start_value; 394 return find_start_value;
395} 395}
396 396
397/* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
398
399static int
400prev_char_comend_first (pos, pos_byte)
401 int pos, pos_byte;
402{
403 int c, val;
404
405 DEC_BOTH (pos, pos_byte);
406 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
407 c = FETCH_CHAR (pos_byte);
408 val = SYNTAX_COMEND_FIRST (c);
409 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
410 return val;
411}
412
413/* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
414
415static int
416prev_char_comstart_first (pos, pos_byte)
417 int pos, pos_byte;
418{
419 int c, val;
420
421 DEC_BOTH (pos, pos_byte);
422 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
423 c = FETCH_CHAR (pos_byte);
424 val = SYNTAX_COMSTART_FIRST (c);
425 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
426 return val;
427}
428
397/* Checks whether charpos FROM is at the end of a comment. 429/* Checks whether charpos FROM is at the end of a comment.
398 FROM_BYTE is the bytepos corresponding to FROM. 430 FROM_BYTE is the bytepos corresponding to FROM.
399 Do not move back before STOP. 431 Do not move back before STOP.
@@ -444,7 +476,7 @@ back_comment (from, from_byte, stop, comstyle, charpos_ptr, bytepos_ptr)
444 that determines quote parity to the comment-end. */ 476 that determines quote parity to the comment-end. */
445 while (from != stop) 477 while (from != stop)
446 { 478 {
447 int temp_byte; 479 int temp_byte, prev_comend_second;
448 480
449 /* Move back and examine a character. */ 481 /* Move back and examine a character. */
450 DEC_BOTH (from, from_byte); 482 DEC_BOTH (from, from_byte);
@@ -456,23 +488,25 @@ back_comment (from, from_byte, stop, comstyle, charpos_ptr, bytepos_ptr)
456 /* If this char is the second of a 2-char comment end sequence, 488 /* If this char is the second of a 2-char comment end sequence,
457 back up and give the pair the appropriate syntax. */ 489 back up and give the pair the appropriate syntax. */
458 if (from > stop && SYNTAX_COMEND_SECOND (c) 490 if (from > stop && SYNTAX_COMEND_SECOND (c)
459 && (temp_byte = dec_bytepos (from_byte), 491 && prev_char_comend_first (from, from_byte))
460 SYNTAX_COMEND_FIRST (FETCH_CHAR (temp_byte))))
461 { 492 {
462 code = Sendcomment; 493 code = Sendcomment;
463 DEC_BOTH (from, from_byte); 494 DEC_BOTH (from, from_byte);
464 /* This is apparently the best we can do: */
465 UPDATE_SYNTAX_TABLE_BACKWARD (from); 495 UPDATE_SYNTAX_TABLE_BACKWARD (from);
466 c = FETCH_CHAR (from_byte); 496 c = FETCH_CHAR (from_byte);
467 } 497 }
468 498
469 /* If this char starts a 2-char comment start sequence, 499 /* If this char starts a 2-char comment start sequence,
470 treat it like a 1-char comment starter. */ 500 treat it like a 1-char comment starter. */
471 if (from < scanstart && SYNTAX_COMSTART_FIRST (c) 501 if (from < scanstart && SYNTAX_COMSTART_FIRST (c))
472 && (temp_byte = inc_bytepos (from_byte), 502 {
473 (SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte)) 503 temp_byte = inc_bytepos (from_byte);
474 && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte))))) 504 UPDATE_SYNTAX_TABLE_FORWARD (from + 1);
475 code = Scomment; 505 if (SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte))
506 && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte)))
507 code = Scomment;
508 UPDATE_SYNTAX_TABLE_BACKWARD (from);
509 }
476 510
477 /* Ignore escaped characters, except comment-enders. */ 511 /* Ignore escaped characters, except comment-enders. */
478 if (code != Sendcomment && char_quoted (from, from_byte)) 512 if (code != Sendcomment && char_quoted (from, from_byte))
@@ -1590,6 +1624,8 @@ between them, return t; otherwise return nil.")
1590 { 1624 {
1591 do 1625 do
1592 { 1626 {
1627 int comstart_first;
1628
1593 if (from == stop) 1629 if (from == stop)
1594 { 1630 {
1595 SET_PT_BOTH (from, from_byte); 1631 SET_PT_BOTH (from, from_byte);
@@ -1599,9 +1635,11 @@ between them, return t; otherwise return nil.")
1599 UPDATE_SYNTAX_TABLE_FORWARD (from); 1635 UPDATE_SYNTAX_TABLE_FORWARD (from);
1600 c = FETCH_CHAR (from_byte); 1636 c = FETCH_CHAR (from_byte);
1601 code = SYNTAX (c); 1637 code = SYNTAX (c);
1638 comstart_first = SYNTAX_COMSTART_FIRST (c);
1602 INC_BOTH (from, from_byte); 1639 INC_BOTH (from, from_byte);
1640 UPDATE_SYNTAX_TABLE_FORWARD (from);
1603 comstyle = 0; 1641 comstyle = 0;
1604 if (from < stop && SYNTAX_COMSTART_FIRST (c) 1642 if (from < stop && comstart_first
1605 && (c1 = FETCH_CHAR (from_byte), 1643 && (c1 = FETCH_CHAR (from_byte),
1606 SYNTAX_COMSTART_SECOND (c1))) 1644 SYNTAX_COMSTART_SECOND (c1)))
1607 { 1645 {
@@ -1649,9 +1687,10 @@ between them, return t; otherwise return nil.")
1649 section. */ 1687 section. */
1650 break; 1688 break;
1651 if (from < stop && SYNTAX_COMEND_FIRST (c) 1689 if (from < stop && SYNTAX_COMEND_FIRST (c)
1690 && SYNTAX_COMMENT_STYLE (c) == comstyle
1652 && (c1 = FETCH_CHAR (from_byte), 1691 && (c1 = FETCH_CHAR (from_byte),
1653 SYNTAX_COMEND_SECOND (c1)) 1692 UPDATE_SYNTAX_TABLE_FORWARD (from),
1654 && SYNTAX_COMMENT_STYLE (c) == comstyle) 1693 SYNTAX_COMEND_SECOND (c1)))
1655 /* we have encountered a comment end of the same style 1694 /* we have encountered a comment end of the same style
1656 as the comment sequence which began this comment 1695 as the comment sequence which began this comment
1657 section */ 1696 section */
@@ -1668,7 +1707,8 @@ between them, return t; otherwise return nil.")
1668 { 1707 {
1669 while (1) 1708 while (1)
1670 { 1709 {
1671 int quoted; 1710 int quoted, comstart_second;
1711
1672 if (from <= stop) 1712 if (from <= stop)
1673 { 1713 {
1674 SET_PT_BOTH (BEGV, BEGV_BYTE); 1714 SET_PT_BOTH (BEGV, BEGV_BYTE);
@@ -1677,34 +1717,34 @@ between them, return t; otherwise return nil.")
1677 } 1717 }
1678 1718
1679 DEC_BOTH (from, from_byte); 1719 DEC_BOTH (from, from_byte);
1720 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
1680 quoted = char_quoted (from, from_byte); 1721 quoted = char_quoted (from, from_byte);
1681 if (quoted) 1722 if (quoted)
1682 { 1723 {
1683 DEC_BOTH (from, from_byte); 1724 DEC_BOTH (from, from_byte);
1684 goto leave; 1725 goto leave;
1685 } 1726 }
1686 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1687 c = FETCH_CHAR (from_byte); 1727 c = FETCH_CHAR (from_byte);
1688 code = SYNTAX (c); 1728 code = SYNTAX (c);
1689 comstyle = 0; 1729 comstyle = 0;
1690 if (code == Sendcomment) 1730 if (code == Sendcomment)
1691 comstyle = SYNTAX_COMMENT_STYLE (c); 1731 comstyle = SYNTAX_COMMENT_STYLE (c);
1692 temp_pos = dec_bytepos (from_byte); 1732 comstart_second = SYNTAX_COMSTART_SECOND (c);
1693 if (from > stop && SYNTAX_COMEND_SECOND (c) 1733 if (from > stop && SYNTAX_COMEND_SECOND (c)
1694 && (c1 = FETCH_CHAR (temp_pos), 1734 && prev_char_comend_first (from, from_byte)
1695 SYNTAX_COMEND_FIRST (c1))
1696 && !char_quoted (from - 1, temp_pos)) 1735 && !char_quoted (from - 1, temp_pos))
1697 { 1736 {
1698 /* We must record the comment style encountered so that 1737 /* We must record the comment style encountered so that
1699 later, we can match only the proper comment begin 1738 later, we can match only the proper comment begin
1700 sequence of the same style. */ 1739 sequence of the same style. */
1701 code = Sendcomment;
1702 comstyle = SYNTAX_COMMENT_STYLE (c1);
1703 DEC_BOTH (from, from_byte); 1740 DEC_BOTH (from, from_byte);
1741 code = Sendcomment;
1742 /* Calling char_quoted, above, set up global syntax position
1743 at the new value of FROM. */
1744 comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
1704 } 1745 }
1705 if (from > stop && SYNTAX_COMSTART_SECOND (c) 1746 if (from > stop && comstart_second
1706 && (c1 = FETCH_CHAR (temp_pos), 1747 && prev_char_comstart_first (from, from_byte)
1707 SYNTAX_COMSTART_FIRST (c1))
1708 && !char_quoted (from - 1, temp_pos)) 1748 && !char_quoted (from - 1, temp_pos))
1709 { 1749 {
1710 /* We must record the comment style encountered so that 1750 /* We must record the comment style encountered so that
@@ -1798,14 +1838,17 @@ scan_lists (from, count, depth, sexpflag)
1798 { 1838 {
1799 while (from < stop) 1839 while (from < stop)
1800 { 1840 {
1841 int comstart_first, prefix;
1801 UPDATE_SYNTAX_TABLE_FORWARD (from); 1842 UPDATE_SYNTAX_TABLE_FORWARD (from);
1802 c = FETCH_CHAR (from_byte); 1843 c = FETCH_CHAR (from_byte);
1803 code = SYNTAX (c); 1844 code = SYNTAX (c);
1845 comstart_first = SYNTAX_COMSTART_FIRST (c);
1846 prefix = SYNTAX_PREFIX (c);
1804 if (depth == min_depth) 1847 if (depth == min_depth)
1805 last_good = from; 1848 last_good = from;
1806 INC_BOTH (from, from_byte); 1849 INC_BOTH (from, from_byte);
1807 UPDATE_SYNTAX_TABLE_FORWARD (from); 1850 UPDATE_SYNTAX_TABLE_FORWARD (from);
1808 if (from < stop && SYNTAX_COMSTART_FIRST (c) 1851 if (from < stop && comstart_first
1809 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte)) 1852 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte))
1810 && parse_sexp_ignore_comments) 1853 && parse_sexp_ignore_comments)
1811 { 1854 {
@@ -1817,10 +1860,10 @@ scan_lists (from, count, depth, sexpflag)
1817 code = Scomment; 1860 code = Scomment;
1818 comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)); 1861 comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
1819 INC_BOTH (from, from_byte); 1862 INC_BOTH (from, from_byte);
1863 UPDATE_SYNTAX_TABLE_FORWARD (from);
1820 } 1864 }
1821 1865
1822 UPDATE_SYNTAX_TABLE_FORWARD (from); 1866 if (prefix)
1823 if (SYNTAX_PREFIX (c))
1824 continue; 1867 continue;
1825 1868
1826 switch (SWITCH_ENUM_CAST (code)) 1869 switch (SWITCH_ENUM_CAST (code))
@@ -1871,6 +1914,7 @@ scan_lists (from, count, depth, sexpflag)
1871 } 1914 }
1872 UPDATE_SYNTAX_TABLE_FORWARD (from); 1915 UPDATE_SYNTAX_TABLE_FORWARD (from);
1873 c = FETCH_CHAR (from_byte); 1916 c = FETCH_CHAR (from_byte);
1917 INC_BOTH (from, from_byte);
1874 if (code == Scomment 1918 if (code == Scomment
1875 ? (SYNTAX (c) == Sendcomment 1919 ? (SYNTAX (c) == Sendcomment
1876 && SYNTAX_COMMENT_STYLE (c) == comstyle) 1920 && SYNTAX_COMMENT_STYLE (c) == comstyle)
@@ -1879,10 +1923,10 @@ scan_lists (from, count, depth, sexpflag)
1879 as the comment sequence which began this comment 1923 as the comment sequence which began this comment
1880 section */ 1924 section */
1881 break; 1925 break;
1882 INC_BOTH (from, from_byte);
1883 if (from < stop && SYNTAX_COMEND_FIRST (c) 1926 if (from < stop && SYNTAX_COMEND_FIRST (c)
1884 && SYNTAX_COMEND_SECOND (FETCH_CHAR (from_byte))
1885 && SYNTAX_COMMENT_STYLE (c) == comstyle 1927 && SYNTAX_COMMENT_STYLE (c) == comstyle
1928 && (UPDATE_SYNTAX_TABLE_FORWARD (from),
1929 SYNTAX_COMEND_SECOND (FETCH_CHAR (from_byte)))
1886 && code == Scomment) 1930 && code == Scomment)
1887 /* we have encountered a comment end of the same style 1931 /* we have encountered a comment end of the same style
1888 as the comment sequence which began this comment 1932 as the comment sequence which began this comment
@@ -1975,25 +2019,22 @@ scan_lists (from, count, depth, sexpflag)
1975 comstyle = 0; 2019 comstyle = 0;
1976 if (code == Sendcomment) 2020 if (code == Sendcomment)
1977 comstyle = SYNTAX_COMMENT_STYLE (c); 2021 comstyle = SYNTAX_COMMENT_STYLE (c);
1978 temp_pos = from_byte;
1979 if (! NILP (current_buffer->enable_multibyte_characters))
1980 DEC_POS (temp_pos);
1981 else
1982 temp_pos--;
1983 if (from > stop && SYNTAX_COMEND_SECOND (c) 2022 if (from > stop && SYNTAX_COMEND_SECOND (c)
1984 && (c1 = FETCH_CHAR (temp_pos), SYNTAX_COMEND_FIRST (c1)) 2023 && prev_char_comstart_first (from, from_byte)
1985 && parse_sexp_ignore_comments) 2024 && parse_sexp_ignore_comments)
1986 { 2025 {
1987 /* we must record the comment style encountered so that 2026 /* We must record the comment style encountered so that
1988 later, we can match only the proper comment begin 2027 later, we can match only the proper comment begin
1989 sequence of the same style */ 2028 sequence of the same style. */
1990 code = Sendcomment;
1991 comstyle = SYNTAX_COMMENT_STYLE (c1);
1992 DEC_BOTH (from, from_byte); 2029 DEC_BOTH (from, from_byte);
2030 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2031 code = Sendcomment;
2032 comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
1993 } 2033 }
1994 2034
1995 /* Quoting turns anything except a comment-ender 2035 /* Quoting turns anything except a comment-ender
1996 into a word character. */ 2036 into a word character. Note that this if cannot be true
2037 if we decremented FROM in the if-statement above. */
1997 if (code != Sendcomment && char_quoted (from, from_byte)) 2038 if (code != Sendcomment && char_quoted (from, from_byte))
1998 code = Sword; 2039 code = Sword;
1999 else if (SYNTAX_PREFIX (c)) 2040 else if (SYNTAX_PREFIX (c))
@@ -2192,11 +2233,15 @@ This includes chars with \"quote\" or \"prefix\" syntax (' or p).")
2192 int pos_byte = PT_BYTE; 2233 int pos_byte = PT_BYTE;
2193 int c; 2234 int c;
2194 2235
2195 if (pos > beg) 2236 if (pos <= beg)
2196 { 2237 {
2197 SETUP_SYNTAX_TABLE (pos, -1); 2238 SET_PT_BOTH (opoint, opoint_byte);
2239
2240 return Qnil;
2198 } 2241 }
2199 2242
2243 SETUP_SYNTAX_TABLE (pos, -1);
2244
2200 DEC_BOTH (pos, pos_byte); 2245 DEC_BOTH (pos, pos_byte);
2201 2246
2202 while (!char_quoted (pos, pos_byte) 2247 while (!char_quoted (pos, pos_byte)