Mercurial > vim
comparison src/change.c @ 28942:6cdf55afaae9 v8.2.4993
patch 8.2.4993: smart/C/lisp indenting is optional
Commit: https://github.com/vim/vim/commit/8e145b82464a21ee4fdf7948f04e2a1d505f8bfa
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat May 21 20:17:31 2022 +0100
patch 8.2.4993: smart/C/lisp indenting is optional
Problem: smart/C/lisp indenting is optional, which makes the code more
complex, while it only reduces the executable size a bit.
Solution: Graduate FEAT_CINDENT, FEAT_SMARTINDENT and FEAT_LISP.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 21 May 2022 21:30:04 +0200 |
parents | b57caac54649 |
children | 864fa5276e78 |
comparison
equal
deleted
inserted
replaced
28941:f17de8647585 | 28942:6cdf55afaae9 |
---|---|
1395 char_u *leader = NULL; // copy of comment leader | 1395 char_u *leader = NULL; // copy of comment leader |
1396 char_u *allocated = NULL; // allocated memory | 1396 char_u *allocated = NULL; // allocated memory |
1397 char_u *p; | 1397 char_u *p; |
1398 int saved_char = NUL; // init for GCC | 1398 int saved_char = NUL; // init for GCC |
1399 pos_T *pos; | 1399 pos_T *pos; |
1400 #ifdef FEAT_CINDENT | |
1401 int do_cindent; | 1400 int do_cindent; |
1402 #endif | |
1403 #ifdef FEAT_SMARTINDENT | |
1404 int do_si = may_do_si(); | 1401 int do_si = may_do_si(); |
1405 int no_si = FALSE; // reset did_si afterwards | 1402 int no_si = FALSE; // reset did_si afterwards |
1406 int first_char = NUL; // init for GCC | 1403 int first_char = NUL; // init for GCC |
1407 #endif | |
1408 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) | |
1409 int vreplace_mode; | 1404 int vreplace_mode; |
1410 #endif | |
1411 int did_append; // appended a new line | 1405 int did_append; // appended a new line |
1412 int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting | 1406 int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting |
1413 | 1407 |
1414 // make a copy of the current line so we can mess with it | 1408 // make a copy of the current line so we can mess with it |
1415 saved_line = vim_strsave(ml_get_curline()); | 1409 saved_line = vim_strsave(ml_get_curline()); |
1451 } | 1445 } |
1452 | 1446 |
1453 if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0) | 1447 if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0) |
1454 { | 1448 { |
1455 p_extra = saved_line + curwin->w_cursor.col; | 1449 p_extra = saved_line + curwin->w_cursor.col; |
1456 #ifdef FEAT_SMARTINDENT | |
1457 if (do_si) // need first char after new line break | 1450 if (do_si) // need first char after new line break |
1458 { | 1451 { |
1459 p = skipwhite(p_extra); | 1452 p = skipwhite(p_extra); |
1460 first_char = *p; | 1453 first_char = *p; |
1461 } | 1454 } |
1462 #endif | |
1463 extra_len = (int)STRLEN(p_extra); | 1455 extra_len = (int)STRLEN(p_extra); |
1464 saved_char = *p_extra; | 1456 saved_char = *p_extra; |
1465 *p_extra = NUL; | 1457 *p_extra = NUL; |
1466 } | 1458 } |
1467 | 1459 |
1468 u_clearline(); // cannot do "U" command when adding lines | 1460 u_clearline(); // cannot do "U" command when adding lines |
1469 #ifdef FEAT_SMARTINDENT | |
1470 did_si = FALSE; | 1461 did_si = FALSE; |
1471 #endif | |
1472 ai_col = 0; | 1462 ai_col = 0; |
1473 | 1463 |
1474 // If we just did an auto-indent, then we didn't type anything on | 1464 // If we just did an auto-indent, then we didn't type anything on |
1475 // the prior line, and it should be truncated. Do this even if 'ai' is not | 1465 // the prior line, and it should be truncated. Do this even if 'ai' is not |
1476 // set because automatically inserting a comment leader also sets did_ai. | 1466 // set because automatically inserting a comment leader also sets did_ai. |
1477 if (dir == FORWARD && did_ai) | 1467 if (dir == FORWARD && did_ai) |
1478 trunc_line = TRUE; | 1468 trunc_line = TRUE; |
1479 | 1469 |
1480 // If 'autoindent' and/or 'smartindent' is set, try to figure out what | 1470 // If 'autoindent' and/or 'smartindent' is set, try to figure out what |
1481 // indent to use for the new line. | 1471 // indent to use for the new line. |
1482 if (curbuf->b_p_ai | 1472 if (curbuf->b_p_ai || do_si) |
1483 #ifdef FEAT_SMARTINDENT | |
1484 || do_si | |
1485 #endif | |
1486 ) | |
1487 { | 1473 { |
1488 // count white space on current line | 1474 // count white space on current line |
1489 #ifdef FEAT_VARTABS | 1475 #ifdef FEAT_VARTABS |
1490 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts, | 1476 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts, |
1491 curbuf->b_p_vts_array, FALSE); | 1477 curbuf->b_p_vts_array, FALSE); |
1493 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE); | 1479 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE); |
1494 #endif | 1480 #endif |
1495 if (newindent == 0 && !(flags & OPENLINE_COM_LIST)) | 1481 if (newindent == 0 && !(flags & OPENLINE_COM_LIST)) |
1496 newindent = second_line_indent; // for ^^D command in insert mode | 1482 newindent = second_line_indent; // for ^^D command in insert mode |
1497 | 1483 |
1498 #ifdef FEAT_SMARTINDENT | |
1499 // Do smart indenting. | 1484 // Do smart indenting. |
1500 // In insert/replace mode (only when dir == FORWARD) | 1485 // In insert/replace mode (only when dir == FORWARD) |
1501 // we may move some text to the next line. If it starts with '{' | 1486 // we may move some text to the next line. If it starts with '{' |
1502 // don't add an indent. Fixes inserting a NL before '{' in line | 1487 // don't add an indent. Fixes inserting a NL before '{' in line |
1503 // "if (condition) {" | 1488 // "if (condition) {" |
1634 } | 1619 } |
1635 curwin->w_cursor = old_cursor; | 1620 curwin->w_cursor = old_cursor; |
1636 } | 1621 } |
1637 if (do_si) | 1622 if (do_si) |
1638 can_si = TRUE; | 1623 can_si = TRUE; |
1639 #endif // FEAT_SMARTINDENT | |
1640 | 1624 |
1641 did_ai = TRUE; | 1625 did_ai = TRUE; |
1642 } | 1626 } |
1643 | 1627 |
1644 #ifdef FEAT_CINDENT | |
1645 // May do indenting after opening a new line. | 1628 // May do indenting after opening a new line. |
1646 do_cindent = !p_paste && (curbuf->b_p_cin | 1629 do_cindent = !p_paste && (curbuf->b_p_cin |
1647 # ifdef FEAT_EVAL | 1630 #ifdef FEAT_EVAL |
1648 || *curbuf->b_p_inde != NUL | 1631 || *curbuf->b_p_inde != NUL |
1649 # endif | 1632 #endif |
1650 ) | 1633 ) |
1651 && in_cinkeys(dir == FORWARD | 1634 && in_cinkeys(dir == FORWARD |
1652 ? KEY_OPEN_FORW | 1635 ? KEY_OPEN_FORW |
1653 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)); | 1636 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)); |
1654 #endif | |
1655 | 1637 |
1656 // Find out if the current line starts with a comment leader. | 1638 // Find out if the current line starts with a comment leader. |
1657 // This may then be inserted in front of the new line. | 1639 // This may then be inserted in front of the new line. |
1658 end_comment_pending = NUL; | 1640 end_comment_pending = NUL; |
1659 if (flags & OPENLINE_DO_COM) | 1641 if (flags & OPENLINE_DO_COM) |
1660 { | 1642 { |
1661 lead_len = get_leader_len(saved_line, &lead_flags, | 1643 lead_len = get_leader_len(saved_line, &lead_flags, |
1662 dir == BACKWARD, TRUE); | 1644 dir == BACKWARD, TRUE); |
1663 #ifdef FEAT_CINDENT | |
1664 if (lead_len == 0 && curbuf->b_p_cin && do_cindent && dir == FORWARD | 1645 if (lead_len == 0 && curbuf->b_p_cin && do_cindent && dir == FORWARD |
1665 && !has_format_option(FO_NO_OPEN_COMS)) | 1646 && !has_format_option(FO_NO_OPEN_COMS)) |
1666 { | 1647 { |
1667 // Check for a line comment after code. | 1648 // Check for a line comment after code. |
1668 comment_start = check_linecomment(saved_line); | 1649 comment_start = check_linecomment(saved_line); |
1676 if (did_do_comment != NULL) | 1657 if (did_do_comment != NULL) |
1677 *did_do_comment = TRUE; | 1658 *did_do_comment = TRUE; |
1678 } | 1659 } |
1679 } | 1660 } |
1680 } | 1661 } |
1681 #endif | |
1682 } | 1662 } |
1683 else | 1663 else |
1684 lead_len = 0; | 1664 lead_len = 0; |
1685 if (lead_len > 0) | 1665 if (lead_len > 0) |
1686 { | 1666 { |
1982 } | 1962 } |
1983 *p = NUL; | 1963 *p = NUL; |
1984 } | 1964 } |
1985 | 1965 |
1986 // Recompute the indent, it may have changed. | 1966 // Recompute the indent, it may have changed. |
1987 if (curbuf->b_p_ai | 1967 if (curbuf->b_p_ai || do_si) |
1988 #ifdef FEAT_SMARTINDENT | |
1989 || do_si | |
1990 #endif | |
1991 ) | |
1992 #ifdef FEAT_VARTABS | 1968 #ifdef FEAT_VARTABS |
1993 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts, | 1969 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts, |
1994 curbuf->b_p_vts_array, FALSE); | 1970 curbuf->b_p_vts_array, FALSE); |
1995 #else | 1971 #else |
1996 newindent = get_indent_str(leader, | 1972 newindent = get_indent_str(leader, |
2033 | 2009 |
2034 newcol = lead_len; | 2010 newcol = lead_len; |
2035 | 2011 |
2036 // if a new indent will be set below, remove the indent that | 2012 // if a new indent will be set below, remove the indent that |
2037 // is in the comment leader | 2013 // is in the comment leader |
2038 if (newindent | 2014 if (newindent || did_si) |
2039 #ifdef FEAT_SMARTINDENT | |
2040 || did_si | |
2041 #endif | |
2042 ) | |
2043 { | 2015 { |
2044 while (lead_len && VIM_ISWHITE(*leader)) | 2016 while (lead_len && VIM_ISWHITE(*leader)) |
2045 { | 2017 { |
2046 --lead_len; | 2018 --lead_len; |
2047 --newcol; | 2019 --newcol; |
2048 ++leader; | 2020 ++leader; |
2049 } | 2021 } |
2050 } | 2022 } |
2051 | 2023 |
2052 } | 2024 } |
2053 #ifdef FEAT_SMARTINDENT | |
2054 did_si = can_si = FALSE; | 2025 did_si = can_si = FALSE; |
2055 #endif | |
2056 } | 2026 } |
2057 else if (comment_end != NULL) | 2027 else if (comment_end != NULL) |
2058 { | 2028 { |
2059 // We have finished a comment, so we don't use the leader. | 2029 // We have finished a comment, so we don't use the leader. |
2060 // If this was a C-comment and 'ai' or 'si' is set do a normal | 2030 // If this was a C-comment and 'ai' or 'si' is set do a normal |
2061 // indent to align with the line containing the start of the | 2031 // indent to align with the line containing the start of the |
2062 // comment. | 2032 // comment. |
2063 if (comment_end[0] == '*' && comment_end[1] == '/' && | 2033 if (comment_end[0] == '*' && comment_end[1] == '/' && |
2064 (curbuf->b_p_ai | 2034 (curbuf->b_p_ai || do_si)) |
2065 #ifdef FEAT_SMARTINDENT | |
2066 || do_si | |
2067 #endif | |
2068 )) | |
2069 { | 2035 { |
2070 old_cursor = curwin->w_cursor; | 2036 old_cursor = curwin->w_cursor; |
2071 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); | 2037 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); |
2072 if ((pos = findmatch(NULL, NUL)) != NULL) | 2038 if ((pos = findmatch(NULL, NUL)) != NULL) |
2073 { | 2039 { |
2180 changed_bytes(curwin->w_cursor.lnum, 0); | 2146 changed_bytes(curwin->w_cursor.lnum, 0); |
2181 curwin->w_cursor.lnum--; | 2147 curwin->w_cursor.lnum--; |
2182 did_append = FALSE; | 2148 did_append = FALSE; |
2183 } | 2149 } |
2184 | 2150 |
2185 if (newindent | 2151 if (newindent || did_si) |
2186 #ifdef FEAT_SMARTINDENT | |
2187 || did_si | |
2188 #endif | |
2189 ) | |
2190 { | 2152 { |
2191 ++curwin->w_cursor.lnum; | 2153 ++curwin->w_cursor.lnum; |
2192 #ifdef FEAT_SMARTINDENT | |
2193 if (did_si) | 2154 if (did_si) |
2194 { | 2155 { |
2195 int sw = (int)get_sw_value(curbuf); | 2156 int sw = (int)get_sw_value(curbuf); |
2196 | 2157 |
2197 if (p_sr) | 2158 if (p_sr) |
2198 newindent -= newindent % sw; | 2159 newindent -= newindent % sw; |
2199 newindent += sw; | 2160 newindent += sw; |
2200 } | 2161 } |
2201 #endif | |
2202 // Copy the indent | 2162 // Copy the indent |
2203 if (curbuf->b_p_ci) | 2163 if (curbuf->b_p_ci) |
2204 { | 2164 { |
2205 (void)copy_indent(newindent, saved_line); | 2165 (void)copy_indent(newindent, saved_line); |
2206 | 2166 |
2219 // must be a NUL on the replace stack, for when it is deleted with BS | 2179 // must be a NUL on the replace stack, for when it is deleted with BS |
2220 if (REPLACE_NORMAL(State)) | 2180 if (REPLACE_NORMAL(State)) |
2221 for (n = 0; n < (int)curwin->w_cursor.col; ++n) | 2181 for (n = 0; n < (int)curwin->w_cursor.col; ++n) |
2222 replace_push(NUL); | 2182 replace_push(NUL); |
2223 newcol += curwin->w_cursor.col; | 2183 newcol += curwin->w_cursor.col; |
2224 #ifdef FEAT_SMARTINDENT | |
2225 if (no_si) | 2184 if (no_si) |
2226 did_si = FALSE; | 2185 did_si = FALSE; |
2227 #endif | |
2228 } | 2186 } |
2229 | 2187 |
2230 // In MODE_REPLACE state, for each character in the extra leader, there | 2188 // In MODE_REPLACE state, for each character in the extra leader, there |
2231 // must be a NUL on the replace stack, for when it is deleted with BS. | 2189 // must be a NUL on the replace stack, for when it is deleted with BS. |
2232 if (REPLACE_NORMAL(State)) | 2190 if (REPLACE_NORMAL(State)) |
2276 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); | 2234 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); |
2277 | 2235 |
2278 curwin->w_cursor.col = newcol; | 2236 curwin->w_cursor.col = newcol; |
2279 curwin->w_cursor.coladd = 0; | 2237 curwin->w_cursor.coladd = 0; |
2280 | 2238 |
2281 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) | |
2282 // In MODE_VREPLACE state, we are handling the replace stack ourselves, so | 2239 // In MODE_VREPLACE state, we are handling the replace stack ourselves, so |
2283 // stop fixthisline() from doing it (via change_indent()) by telling it | 2240 // stop fixthisline() from doing it (via change_indent()) by telling it |
2284 // we're in normal MODE_INSERT state. | 2241 // we're in normal MODE_INSERT state. |
2285 if (State & VREPLACE_FLAG) | 2242 if (State & VREPLACE_FLAG) |
2286 { | 2243 { |
2287 vreplace_mode = State; // So we know to put things right later | 2244 vreplace_mode = State; // So we know to put things right later |
2288 State = MODE_INSERT; | 2245 State = MODE_INSERT; |
2289 } | 2246 } |
2290 else | 2247 else |
2291 vreplace_mode = 0; | 2248 vreplace_mode = 0; |
2292 #endif | 2249 |
2293 #ifdef FEAT_LISP | |
2294 // May do lisp indenting. | 2250 // May do lisp indenting. |
2295 if (!p_paste | 2251 if (!p_paste |
2296 && leader == NULL | 2252 && leader == NULL |
2297 && curbuf->b_p_lisp | 2253 && curbuf->b_p_lisp |
2298 && curbuf->b_p_ai) | 2254 && curbuf->b_p_ai) |
2299 { | 2255 { |
2300 fixthisline(get_lisp_indent); | 2256 fixthisline(get_lisp_indent); |
2301 ai_col = (colnr_T)getwhitecols_curline(); | 2257 ai_col = (colnr_T)getwhitecols_curline(); |
2302 } | 2258 } |
2303 #endif | 2259 |
2304 #ifdef FEAT_CINDENT | |
2305 // May do indenting after opening a new line. | 2260 // May do indenting after opening a new line. |
2306 if (do_cindent) | 2261 if (do_cindent) |
2307 { | 2262 { |
2308 do_c_expr_indent(); | 2263 do_c_expr_indent(); |
2309 ai_col = (colnr_T)getwhitecols_curline(); | 2264 ai_col = (colnr_T)getwhitecols_curline(); |
2310 } | 2265 } |
2311 #endif | 2266 |
2312 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) | |
2313 if (vreplace_mode != 0) | 2267 if (vreplace_mode != 0) |
2314 State = vreplace_mode; | 2268 State = vreplace_mode; |
2315 #endif | |
2316 | 2269 |
2317 // Finally, MODE_VREPLACE gets the stuff on the new line, then puts back | 2270 // Finally, MODE_VREPLACE gets the stuff on the new line, then puts back |
2318 // the original line, and inserts the new stuff char by char, pushing old | 2271 // the original line, and inserts the new stuff char by char, pushing old |
2319 // stuff onto the replace stack (via ins_char()). | 2272 // stuff onto the replace stack (via ins_char()). |
2320 if (State & VREPLACE_FLAG) | 2273 if (State & VREPLACE_FLAG) |