diff options
Diffstat (limited to 'src/nvim/indent_c.c')
-rw-r--r-- | src/nvim/indent_c.c | 1667 |
1 files changed, 735 insertions, 932 deletions
diff --git a/src/nvim/indent_c.c b/src/nvim/indent_c.c index 34a3de4f78..1c771073b2 100644 --- a/src/nvim/indent_c.c +++ b/src/nvim/indent_c.c @@ -1,19 +1,26 @@ // This is an open source non-commercial project. Dear PVS-Studio, please check // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com -#include <assert.h> #include <inttypes.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> #include "nvim/ascii.h" +#include "nvim/buffer_defs.h" #include "nvim/charset.h" #include "nvim/cursor.h" #include "nvim/edit.h" +#include "nvim/globals.h" #include "nvim/indent.h" #include "nvim/indent_c.h" +#include "nvim/macros.h" #include "nvim/mark.h" #include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/option.h" +#include "nvim/pos.h" #include "nvim/search.h" #include "nvim/strings.h" #include "nvim/vim.h" @@ -27,11 +34,9 @@ typedef struct { #ifdef INCLUDE_GENERATED_DECLARATIONS # include "indent_c.c.generated.h" #endif -/* - * Find the start of a comment, not knowing if we are in a comment right now. - * Search starts at w_cursor.lnum and goes backwards. - * Return NULL when not inside a comment. - */ +// Find the start of a comment, not knowing if we are in a comment right now. +// Search starts at w_cursor.lnum and goes backwards. +// Return NULL when not inside a comment. static pos_T *ind_find_start_comment(void) // XXX { return find_start_comment(curbuf->b_ind_maxcomment); @@ -48,10 +53,8 @@ pos_T *find_start_comment(int ind_maxcomment) // XXX break; } - /* - * Check if the comment start we found is inside a string. - * If it is then restrict the search to below this line and try again. - */ + // Check if the comment start we found is inside a string. + // If it is then restrict the search to below this line and try again. if (!is_pos_in_string(ml_get(pos->lnum), pos->col)) { break; } @@ -97,11 +100,9 @@ static pos_T *ind_find_start_CORS(linenr_T *is_raw) return comment_pos; } -/* - * Find the start of a raw string, not knowing if we are in one right now. - * Search starts at w_cursor.lnum and goes backwards. - * Return NULL when not inside a raw string. - */ +// Find the start of a raw string, not knowing if we are in one right now. +// Search starts at w_cursor.lnum and goes backwards. +// Return NULL when not inside a raw string. static pos_T *find_start_rawstring(int ind_maxcomment) // XXX { pos_T *pos; @@ -127,17 +128,13 @@ static pos_T *find_start_rawstring(int ind_maxcomment) // XXX return pos; } -/* - * Skip to the end of a "string" and a 'c' character. - * If there is no string or character, return argument unmodified. - */ -static const char_u *skip_string(const char_u *p) +// Skip to the end of a "string" and a 'c' character. +// If there is no string or character, return argument unmodified. +static const char *skip_string(const char *p) { int i; - /* - * We loop, because strings may be concatenated: "date""time". - */ + // We loop, because strings may be concatenated: "date""time". for (;; p++) { if (p[0] == '\'') { // 'c' or '\n' or '\000' if (p[1] == NUL) { // ' at end of line @@ -167,14 +164,14 @@ static const char_u *skip_string(const char_u *p) } } else if (p[0] == 'R' && p[1] == '"') { // Raw string: R"[delim](...)[delim]" - const char *delim = (char *)p + 2; - const char *paren = vim_strchr((char *)delim, '('); + const char *delim = p + 2; + const char *paren = vim_strchr(delim, '('); if (paren != NULL) { const ptrdiff_t delim_len = paren - delim; for (p += 3; *p; p++) { - if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 + if (p[0] == ')' && strncmp(p + 1, delim, (size_t)delim_len) == 0 && p[delim_len + 1] == '"') { p += delim_len + 1; break; @@ -194,9 +191,9 @@ static const char_u *skip_string(const char_u *p) } /// @returns true if "line[col]" is inside a C string. -int is_pos_in_string(const char_u *line, colnr_T col) +int is_pos_in_string(const char *line, colnr_T col) { - const char_u *p; + const char *p; for (p = line; *p && (colnr_T)(p - line) < col; p++) { p = skip_string(p); @@ -204,29 +201,24 @@ int is_pos_in_string(const char_u *line, colnr_T col) return !((colnr_T)(p - line) <= col); } -/* - * Functions for C-indenting. - * Most of this originally comes from Eric Fischer. - */ -/* - * Below "XXX" means that this function may unlock the current line. - */ - -/* - * Return true if the string "line" starts with a word from 'cinwords'. - */ -bool cin_is_cinword(const char_u *line) +// Functions for C-indenting. +// Most of this originally comes from Eric Fischer. + +// Below "XXX" means that this function may unlock the current line. + +/// @return true if the string "line" starts with a word from 'cinwords'. +bool cin_is_cinword(const char *line) { bool retval = false; - size_t cinw_len = STRLEN(curbuf->b_p_cinw) + 1; - char_u *cinw_buf = xmalloc(cinw_len); - line = (char_u *)skipwhite((char *)line); + size_t cinw_len = strlen(curbuf->b_p_cinw) + 1; + char *cinw_buf = xmalloc(cinw_len); + line = skipwhite(line); - for (char *cinw = (char *)curbuf->b_p_cinw; *cinw;) { - size_t len = copy_option_part(&cinw, (char *)cinw_buf, cinw_len, ","); - if (STRNCMP(line, cinw_buf, len) == 0 - && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) { + for (char *cinw = curbuf->b_p_cinw; *cinw;) { + size_t len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); + if (strncmp(line, cinw_buf, len) == 0 + && (!vim_iswordc((uint8_t)line[len]) || !vim_iswordc((uint8_t)line[len - 1]))) { retval = true; break; } @@ -237,21 +229,19 @@ bool cin_is_cinword(const char_u *line) return retval; } -/* - * Skip over white space and C comments within the line. - * Also skip over Perl/shell comments if desired. - */ -static const char_u *cin_skipcomment(const char_u *s) +// Skip over white space and C comments within the line. +// Also skip over Perl/shell comments if desired. +static const char *cin_skipcomment(const char *s) { while (*s) { - const char_u *prev_s = s; + const char *prev_s = s; - s = (char_u *)skipwhite((char *)s); + s = skipwhite(s); - /* Perl/shell # comment comment continues until eol. Require a space - * before # to avoid recognizing $#array. */ + // Perl/shell # comment comment continues until eol. Require a space + // before # to avoid recognizing $#array. if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#') { - s += STRLEN(s); + s += strlen(s); break; } if (*s != '/') { @@ -259,7 +249,7 @@ static const char_u *cin_skipcomment(const char_u *s) } s++; if (*s == '/') { // slash-slash comment continues till eol - s += STRLEN(s); + s += strlen(s); break; } if (*s != '*') { @@ -275,28 +265,24 @@ static const char_u *cin_skipcomment(const char_u *s) return s; } -/* - * Return TRUE if there is no code at *s. White space and comments are - * not considered code. - */ -static int cin_nocode(const char_u *s) +/// Return true if there is no code at *s. White space and comments are +/// not considered code. +static int cin_nocode(const char *s) { return *cin_skipcomment(s) == NUL; } -/* - * Check previous lines for a "//" line comment, skipping over blank lines. - */ +// Check previous lines for a "//" line comment, skipping over blank lines. static pos_T *find_line_comment(void) // XXX { static pos_T pos; - char_u *line; - char_u *p; + char *line; + char *p; pos = curwin->w_cursor; while (--pos.lnum > 0) { line = ml_get(pos.lnum); - p = (char_u *)skipwhite((char *)line); + p = skipwhite(line); if (cin_islinecomment(p)) { pos.col = (int)(p - line); return &pos; @@ -309,21 +295,21 @@ static pos_T *find_line_comment(void) // XXX } /// Checks if `text` starts with "key:". -static bool cin_has_js_key(const char_u *text) +static bool cin_has_js_key(const char *text) { - const char_u *s = (char_u *)skipwhite((char *)text); + const char *s = skipwhite(text); - char_u quote = 0; + char quote = 0; if (*s == '\'' || *s == '"') { // can be 'key': or "key": quote = *s; s++; } - if (!vim_isIDc(*s)) { // need at least one ID character + if (!vim_isIDc((uint8_t)(*s))) { // need at least one ID character return false; } - while (vim_isIDc(*s)) { + while (vim_isIDc((uint8_t)(*s))) { s++; } if (*s && *s == quote) { @@ -338,14 +324,14 @@ static bool cin_has_js_key(const char_u *text) /// Checks if string matches "label:"; move to character after ':' if true. /// "*s" must point to the start of the label, if there is one. -static bool cin_islabel_skip(const char_u **s) +static bool cin_islabel_skip(const char **s) FUNC_ATTR_NONNULL_ALL { - if (!vim_isIDc(**s)) { // need at least one ID character + if (!vim_isIDc((uint8_t)(**s))) { // need at least one ID character return false; } - while (vim_isIDc(**s)) { + while (vim_isIDc((uint8_t)(**s))) { (*s)++; } @@ -359,7 +345,7 @@ static bool cin_islabel_skip(const char_u **s) // Note: curwin->w_cursor must be where we are looking for the label. bool cin_islabel(void) // XXX { - const char_u *s = cin_skipcomment(get_cursor_line_ptr()); + const char *s = cin_skipcomment(get_cursor_line_ptr()); // Exclude "default" from labels, since it should be indented // like a switch label. Same for C++ scope declarations. @@ -369,26 +355,23 @@ bool cin_islabel(void) // XXX if (cin_isscopedecl(s)) { return false; } + if (!cin_islabel_skip(&s)) { return false; } - /* - * Only accept a label if the previous line is terminated or is a case - * label. - */ + // Only accept a label if the previous line is terminated or is a case + // label. pos_T cursor_save; pos_T *trypos; - const char_u *line; + const char *line; cursor_save = curwin->w_cursor; while (curwin->w_cursor.lnum > 1) { - --curwin->w_cursor.lnum; + curwin->w_cursor.lnum--; - /* - * If we're in a comment or raw string now, skip to the start of - * it. - */ + // If we're in a comment or raw string now, skip to the start of + // it. curwin->w_cursor.col = 0; if ((trypos = ind_find_start_CORS(NULL)) != NULL) { // XXX curwin->w_cursor = *trypos; @@ -403,7 +386,7 @@ bool cin_islabel(void) // XXX } curwin->w_cursor = cursor_save; - if (cin_isterminated(line, TRUE, FALSE) + if (cin_isterminated(line, true, false) || cin_isscopedecl(line) || cin_iscase(line, true) || (cin_islabel_skip(&line) && cin_nocode(line))) { @@ -415,14 +398,12 @@ bool cin_islabel(void) // XXX return true; // label at start of file??? } -/* - * Recognize structure initialization and enumerations: - * "[typedef] [static|public|protected|private] enum" - * "[typedef] [static|public|protected|private] = {" - */ +// Recognize structure initialization and enumerations: +// "[typedef] [static|public|protected|private] enum" +// "[typedef] [static|public|protected|private] = {" static int cin_isinit(void) { - const char_u *s; + const char *s; static char *skip[] = { "static", "public", "protected", "private" }; s = cin_skipcomment(get_cursor_line_ptr()); @@ -434,7 +415,7 @@ static int cin_isinit(void) for (;;) { int i, l; - for (i = 0; i < (int)ARRAY_SIZE(skip); ++i) { + for (i = 0; i < (int)ARRAY_SIZE(skip); i++) { l = (int)strlen(skip[i]); if (cin_starts_with(s, skip[i])) { s = cin_skipcomment(s + l); @@ -451,21 +432,21 @@ static int cin_isinit(void) return true; } - if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) { + if (cin_ends_in(s, "=", "{")) { return true; } - return FALSE; + return false; } /// Recognize a switch label: "case .*:" or "default:". /// /// @param strict Allow relaxed check of case statement for JS -bool cin_iscase(const char_u *s, bool strict) +bool cin_iscase(const char *s, bool strict) { s = cin_skipcomment(s); if (cin_starts_with(s, "case")) { - for (s += 4; *s; ++s) { + for (s += 4; *s; s++) { s = cin_skipcomment(s); if (*s == NUL) { break; @@ -485,9 +466,8 @@ bool cin_iscase(const char_u *s, bool strict) // JS etc. if (strict) { return false; // stop at string - } else { - return true; } + return true; } } return false; @@ -499,30 +479,28 @@ bool cin_iscase(const char_u *s, bool strict) return false; } -/* - * Recognize a "default" switch label. - */ -static int cin_isdefault(const char_u *s) +// Recognize a "default" switch label. +static int cin_isdefault(const char *s) { - return STRNCMP(s, "default", 7) == 0 + return strncmp(s, "default", 7) == 0 && *(s = cin_skipcomment(s + 7)) == ':' && s[1] != ':'; } /// Recognize a scope declaration label set in 'cinscopedecls'. -bool cin_isscopedecl(const char_u *p) +bool cin_isscopedecl(const char *p) { - const char_u *s = cin_skipcomment(p); + const char *s = cin_skipcomment(p); - const size_t cinsd_len = STRLEN(curbuf->b_p_cinsd) + 1; - char_u *cinsd_buf = xmalloc(cinsd_len); + const size_t cinsd_len = strlen(curbuf->b_p_cinsd) + 1; + char *cinsd_buf = xmalloc(cinsd_len); bool found = false; - for (char *cinsd = (char *)curbuf->b_p_cinsd; *cinsd;) { - const size_t len = copy_option_part(&cinsd, (char *)cinsd_buf, cinsd_len, ","); - if (STRNCMP(s, cinsd_buf, len) == 0) { - const char_u *skip = cin_skipcomment(s + len); + for (char *cinsd = curbuf->b_p_cinsd; *cinsd;) { + const size_t len = copy_option_part(&cinsd, cinsd_buf, cinsd_len, ","); + if (strncmp(s, cinsd_buf, len) == 0) { + const char *skip = cin_skipcomment(s + len); if (*skip == ':' && skip[1] != ':') { found = true; break; @@ -539,33 +517,33 @@ bool cin_isscopedecl(const char_u *p) #define FIND_NAMESPACE_LIM 20 // Recognize a "namespace" scope declaration. -static bool cin_is_cpp_namespace(const char_u *s) +static bool cin_is_cpp_namespace(const char *s) { - const char_u *p; + const char *p; bool has_name = false; bool has_name_start = false; s = cin_skipcomment(s); - if (STRNCMP(s, "inline", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) { - s = cin_skipcomment((char_u *)skipwhite((char *)s + 6)); + if (strncmp(s, "inline", 6) == 0 && (s[6] == NUL || !vim_iswordc((uint8_t)s[6]))) { + s = cin_skipcomment(skipwhite(s + 6)); } - if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) { - p = cin_skipcomment((char_u *)skipwhite((char *)s + 9)); + if (strncmp(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc((uint8_t)s[9]))) { + p = cin_skipcomment(skipwhite(s + 9)); while (*p != NUL) { if (ascii_iswhite(*p)) { has_name = true; // found end of a name - p = cin_skipcomment((char_u *)skipwhite((char *)p)); + p = cin_skipcomment(skipwhite(p)); } else if (*p == '{') { break; - } else if (vim_iswordc(*p)) { + } else if (vim_iswordc((uint8_t)(*p))) { has_name_start = true; if (has_name) { return false; // word character after skipping past name } p++; - } else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2])) { + } else if (p[0] == ':' && p[1] == ':' && vim_iswordc((uint8_t)p[2])) { if (!has_name_start || has_name) { return false; } @@ -580,15 +558,13 @@ static bool cin_is_cpp_namespace(const char_u *s) return false; } -/* - * Return a pointer to the first non-empty non-comment character after a ':'. - * Return NULL if not found. - * case 234: a = b; - * ^ - */ -static const char_u *after_label(const char_u *l) +// Return a pointer to the first non-empty non-comment character after a ':'. +// Return NULL if not found. +// case 234: a = b; +// ^ +static const char *after_label(const char *l) { - for (; *l; ++l) { + for (; *l; l++) { if (*l == ':') { if (l[1] == ':') { // skip over "::" for C++ l++; @@ -609,16 +585,14 @@ static const char_u *after_label(const char_u *l) return l; } -/* - * Get indent of line "lnum", skipping a label. - * Return 0 if there is nothing after the label. - */ +// Get indent of line "lnum", skipping a label. +// Return 0 if there is nothing after the label. static int get_indent_nolabel(linenr_T lnum) // XXX { - const char_u *l; + const char *l; pos_T fp; colnr_T col; - const char_u *p; + const char *p; l = ml_get(lnum); p = after_label(l); @@ -632,15 +606,13 @@ static int get_indent_nolabel(linenr_T lnum) // XXX return (int)col; } -/* - * Find indent for line "lnum", ignoring any case or jump label. - * Also return a pointer to the text (after the label) in "pp". - * label: if (asdf && asdfasdf) - * ^ - */ -static int skip_label(linenr_T lnum, const char_u **pp) +// Find indent for line "lnum", ignoring any case or jump label. +// Also return a pointer to the text (after the label) in "pp". +// label: if (asdf && asdfasdf) +// ^ +static int skip_label(linenr_T lnum, const char **pp) { - const char_u *l; + const char *l; int amount; pos_T cursor_save; @@ -664,78 +636,75 @@ static int skip_label(linenr_T lnum, const char_u **pp) return amount; } -/* - * Return the indent of the first variable name after a type in a declaration. - * int a, indent of "a" - * static struct foo b, indent of "b" - * enum bla c, indent of "c" - * Returns zero when it doesn't look like a declaration. - */ +// Return the indent of the first variable name after a type in a declaration. +// int a, indent of "a" +// static struct foo b, indent of "b" +// enum bla c, indent of "c" +// Returns zero when it doesn't look like a declaration. static int cin_first_id_amount(void) { - char_u *line, *p, *s; + char *line, *p, *s; int len; pos_T fp; colnr_T col; line = get_cursor_line_ptr(); - p = (char_u *)skipwhite((char *)line); + p = skipwhite(line); len = (int)(skiptowhite(p) - p); - if (len == 6 && STRNCMP(p, "static", 6) == 0) { - p = (char_u *)skipwhite((char *)p + 6); + if (len == 6 && strncmp(p, "static", 6) == 0) { + p = skipwhite(p + 6); len = (int)(skiptowhite(p) - p); } - if (len == 6 && STRNCMP(p, "struct", 6) == 0) { - p = (char_u *)skipwhite((char *)p + 6); - } else if (len == 4 && STRNCMP(p, "enum", 4) == 0) { - p = (char_u *)skipwhite((char *)p + 4); - } else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) - || (len == 6 && STRNCMP(p, "signed", 6) == 0)) { - s = (char_u *)skipwhite((char *)p + len); - if ((STRNCMP(s, "int", 3) == 0 && ascii_iswhite(s[3])) - || (STRNCMP(s, "long", 4) == 0 && ascii_iswhite(s[4])) - || (STRNCMP(s, "short", 5) == 0 && ascii_iswhite(s[5])) - || (STRNCMP(s, "char", 4) == 0 && ascii_iswhite(s[4]))) { + if (len == 6 && strncmp(p, "struct", 6) == 0) { + p = skipwhite(p + 6); + } else if (len == 4 && strncmp(p, "enum", 4) == 0) { + p = skipwhite(p + 4); + } else if ((len == 8 && strncmp(p, "unsigned", 8) == 0) + || (len == 6 && strncmp(p, "signed", 6) == 0)) { + s = skipwhite(p + len); + if ((strncmp(s, "int", 3) == 0 && ascii_iswhite(s[3])) + || (strncmp(s, "long", 4) == 0 && ascii_iswhite(s[4])) + || (strncmp(s, "short", 5) == 0 && ascii_iswhite(s[5])) + || (strncmp(s, "char", 4) == 0 && ascii_iswhite(s[4]))) { p = s; } } - for (len = 0; vim_isIDc(p[len]); len++) {} + for (len = 0; vim_isIDc((uint8_t)p[len]); len++) {} if (len == 0 || !ascii_iswhite(p[len]) || cin_nocode(p)) { return 0; } - p = (char_u *)skipwhite((char *)p + len); + p = skipwhite(p + len); fp.lnum = curwin->w_cursor.lnum; fp.col = (colnr_T)(p - line); getvcol(curwin, &fp, &col, NULL, NULL); return (int)col; } -/* - * Return the indent of the first non-blank after an equal sign. - * char *foo = "here"; - * Return zero if no (useful) equal sign found. - * Return -1 if the line above "lnum" ends in a backslash. - * foo = "asdf\ - * asdf\ - * here"; - */ +// Return the indent of the first non-blank after an equal sign. +// char *foo = "here"; +// Return zero if no (useful) equal sign found. +// Return -1 if the line above "lnum" ends in a backslash. +// foo = "asdf{backslash} +// asdf{backslash} +// here"; static int cin_get_equal_amount(linenr_T lnum) { - const char_u *line; - const char_u *s; + const char *line; + const char *s; colnr_T col; pos_T fp; if (lnum > 1) { line = ml_get(lnum - 1); - if (*line != NUL && line[STRLEN(line) - 1] == '\\') { + if (*line != NUL && line[strlen(line) - 1] == '\\') { return -1; } } - line = s = ml_get(lnum); - while (*s != NUL && vim_strchr("=;{}\"'", *s) == NULL) { + s = ml_get(lnum); + line = s; + while (*s != NUL && vim_strchr("=;{}\"'", (uint8_t)(*s)) == NULL) { if (cin_iscomment(s)) { // ignore comments s = cin_skipcomment(s); } else { @@ -746,7 +715,7 @@ static int cin_get_equal_amount(linenr_T lnum) return 0; } - s = (char_u *)skipwhite((char *)s + 1); + s = skipwhite(s + 1); if (cin_nocode(s)) { return 0; } @@ -761,35 +730,33 @@ static int cin_get_equal_amount(linenr_T lnum) return (int)col; } -/* - * Recognize a preprocessor statement: Any line that starts with '#'. - */ -static int cin_ispreproc(const char_u *s) +// Recognize a preprocessor statement: Any line that starts with '#'. +static int cin_ispreproc(const char *s) { - if (*skipwhite((char *)s) == '#') { + if (*skipwhite(s) == '#') { return true; } - return FALSE; + return false; } -/// Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a +/// Return true if line "*pp" at "*lnump" is a preprocessor statement or a /// continuation line of a preprocessor statement. Decrease "*lnump" to the /// start and return the line in "*pp". /// Put the amount of indent in "*amount". -static int cin_ispreproc_cont(const char_u **pp, linenr_T *lnump, int *amount) +static int cin_ispreproc_cont(const char **pp, linenr_T *lnump, int *amount) { - const char_u *line = *pp; + const char *line = *pp; linenr_T lnum = *lnump; int retval = false; int candidate_amount = *amount; - if (*line != NUL && line[STRLEN(line) - 1] == '\\') { + if (*line != NUL && line[strlen(line) - 1] == '\\') { candidate_amount = get_indent_lnum(lnum); } for (;;) { if (cin_ispreproc(line)) { - retval = TRUE; + retval = true; *lnump = lnum; break; } @@ -797,7 +764,7 @@ static int cin_ispreproc_cont(const char_u **pp, linenr_T *lnump, int *amount) break; } line = ml_get(--lnum); - if (*line == NUL || line[STRLEN(line) - 1] != '\\') { + if (*line == NUL || line[strlen(line) - 1] != '\\') { break; } } @@ -811,18 +778,14 @@ static int cin_ispreproc_cont(const char_u **pp, linenr_T *lnump, int *amount) return retval; } -/* - * Recognize the start of a C or C++ comment. - */ -static int cin_iscomment(const char_u *p) +// Recognize the start of a C or C++ comment. +static int cin_iscomment(const char *p) { return p[0] == '/' && (p[1] == '*' || p[1] == '/'); } -/* - * Recognize the start of a "//" comment. - */ -static int cin_islinecomment(const char_u *p) +// Recognize the start of a "//" comment. +static int cin_islinecomment(const char *p) { return p[0] == '/' && p[1] == '/'; } @@ -838,11 +801,11 @@ static int cin_islinecomment(const char_u *p) /// /// @return the character terminating the line (ending char's have precedence if /// both apply in order to determine initializations). -static char_u cin_isterminated(const char_u *s, int incl_open, int incl_comma) +static char cin_isterminated(const char *s, int incl_open, int incl_comma) { - char_u found_start = 0; + char found_start = 0; unsigned n_open = 0; - int is_else = FALSE; + int is_else = false; s = cin_skipcomment(s); @@ -889,9 +852,9 @@ static char_u cin_isterminated(const char_u *s, int incl_open, int incl_comma) /// lines here. /// @param[in] first_lnum Where to start looking. /// @param[in] min_lnum The line before which we will not be looking. -static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_lnum) +static int cin_isfuncdecl(const char **sp, linenr_T first_lnum, linenr_T min_lnum) { - const char_u *s; + const char *s; linenr_T lnum = first_lnum; linenr_T save_lnum = curwin->w_cursor.lnum; int retval = false; @@ -952,7 +915,7 @@ static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_l // defined(y) lnum = first_lnum - 1; s = ml_get(lnum); - if (*s == NUL || s[STRLEN(s) - 1] != '\\') { + if (*s == NUL || s[strlen(s) - 1] != '\\') { retval = true; } goto done; @@ -960,10 +923,10 @@ static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_l if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) { int comma = (*s == ','); - /* ',' at the end: continue looking in the next line. - * At the end: check for ',' in the next line, for this style: - * func(arg1 - * , arg2) */ + // ',' at the end: continue looking in the next line. + // At the end: check for ',' in the next line, for this style: + // func(arg1 + // , arg2) for (;;) { if (lnum >= curbuf->b_ml.ml_line_count) { break; @@ -978,7 +941,7 @@ static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_l } // Require a comma at end of the line or a comma or ')' at the // start of next line. - s = (char_u *)skipwhite((char *)s); + s = skipwhite(s); if (!just_started && (!comma && *s != ',' && *s != ')')) { break; } @@ -999,30 +962,28 @@ done: return retval; } -static int cin_isif(const char_u *p) +static int cin_isif(const char *p) { - return STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]); + return strncmp(p, "if", 2) == 0 && !vim_isIDc((uint8_t)p[2]); } -static int cin_iselse(const char_u *p) +static int cin_iselse(const char *p) { if (*p == '}') { // accept "} else" p = cin_skipcomment(p + 1); } - return STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]); + return strncmp(p, "else", 4) == 0 && !vim_isIDc((uint8_t)p[4]); } -static int cin_isdo(const char_u *p) +static int cin_isdo(const char *p) { - return STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]); + return strncmp(p, "do", 2) == 0 && !vim_isIDc((uint8_t)p[2]); } -/* - * Check if this is a "while" that should have a matching "do". - * We only accept a "while (condition) ;", with only white space between the - * ')' and ';'. The condition may be spread over several lines. - */ -static int cin_iswhileofdo(const char_u *p, linenr_T lnum) // XXX +// Check if this is a "while" that should have a matching "do". +// We only accept a "while (condition) ;", with only white space between the +// ')' and ';'. The condition may be spread over several lines. +static int cin_iswhileofdo(const char *p, linenr_T lnum) // XXX { pos_T cursor_save; pos_T *trypos; @@ -1050,13 +1011,11 @@ static int cin_iswhileofdo(const char_u *p, linenr_T lnum) // XXX return retval; } -/* - * Check whether in "p" there is an "if", "for" or "while" before "*poffset". - * Return 0 if there is none. - * Otherwise return !0 and update "*poffset" to point to the place where the - * string was found. - */ -static int cin_is_if_for_while_before_offset(const char_u *line, int *poffset) +// Check whether in "p" there is an "if", "for" or "while" before "*poffset". +// Return 0 if there is none. +// Otherwise return !0 and update "*poffset" to point to the place where the +// string was found. +static int cin_is_if_for_while_before_offset(const char *line, int *poffset) { int offset = *poffset; @@ -1068,19 +1027,19 @@ static int cin_is_if_for_while_before_offset(const char_u *line, int *poffset) } offset -= 1; - if (!STRNCMP(line + offset, "if", 2)) { + if (!strncmp(line + offset, "if", 2)) { goto probablyFound; } if (offset >= 1) { offset -= 1; - if (!STRNCMP(line + offset, "for", 3)) { + if (!strncmp(line + offset, "for", 3)) { goto probablyFound; } if (offset >= 2) { offset -= 2; - if (!STRNCMP(line + offset, "while", 5)) { + if (!strncmp(line + offset, "while", 5)) { goto probablyFound; } } @@ -1088,26 +1047,24 @@ static int cin_is_if_for_while_before_offset(const char_u *line, int *poffset) return 0; probablyFound: - if (!offset || !vim_isIDc(line[offset - 1])) { + if (!offset || !vim_isIDc((uint8_t)line[offset - 1])) { *poffset = offset; return 1; } return 0; } -/* - * Return TRUE if we are at the end of a do-while. - * do - * nothing; - * while (foo - * && bar); <-- here - * Adjust the cursor to the line with "while". - */ +/// Return true if we are at the end of a do-while. +/// do +/// nothing; +/// while (foo +/// && bar); <-- here +/// Adjust the cursor to the line with "while". static int cin_iswhileofdo_end(int terminated) { - const char_u *line; - const char_u *p; - const char_u *s; + const char *line; + const char *p; + const char *s; pos_T *trypos; int i; @@ -1119,10 +1076,10 @@ static int cin_iswhileofdo_end(int terminated) while (*p != NUL) { p = cin_skipcomment(p); if (*p == ')') { - s = (char_u *)skipwhite((char *)p + 1); + s = skipwhite(p + 1); if (*s == ';' && cin_nocode(s + 1)) { - /* Found ");" at end of the line, now check there is "while" - * before the matching '('. XXX */ + // Found ");" at end of the line, now check there is "while" + // before the matching '('. XXX i = (int)(p - line); curwin->w_cursor.col = i; trypos = find_match_paren(curbuf->b_ind_maxparen); @@ -1133,7 +1090,7 @@ static int cin_iswhileofdo_end(int terminated) } if (cin_starts_with(s, "while")) { curwin->w_cursor.lnum = trypos->lnum; - return TRUE; + return true; } } @@ -1146,34 +1103,32 @@ static int cin_iswhileofdo_end(int terminated) p++; } } - return FALSE; + return false; } -static int cin_isbreak(const char_u *p) +static int cin_isbreak(const char *p) { - return STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]); + return strncmp(p, "break", 5) == 0 && !vim_isIDc((uint8_t)p[5]); } -/* - * Find the position of a C++ base-class declaration or - * constructor-initialization. eg: - * - * class MyClass : - * baseClass <-- here - * class MyClass : public baseClass, - * anotherBaseClass <-- here (should probably lineup ??) - * MyClass::MyClass(...) : - * baseClass(...) <-- here (constructor-initialization) - * - * This is a lot of guessing. Watch out for "cond ? func() : foo". - */ +// Find the position of a C++ base-class declaration or +// constructor-initialization. eg: +// +// class MyClass : +// baseClass <-- here +// class MyClass : public baseClass, +// anotherBaseClass <-- here (should probably lineup ??) +// MyClass::MyClass(...) : +// baseClass(...) <-- here (constructor-initialization) +// +// This is a lot of guessing. Watch out for "cond ? func() : foo". static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) { lpos_T *pos = &cached->lpos; // find position - const char_u *s; + const char *s; int class_or_struct, lookfor_ctor_init, cpp_base_class; linenr_T lnum = curwin->w_cursor.lnum; - const char_u *line = get_cursor_line_ptr(); + const char *line = get_cursor_line_ptr(); if (pos->lnum <= lnum) { return cached->found; // Use the cached result @@ -1181,7 +1136,7 @@ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) pos->col = 0; - s = (char_u *)skipwhite((char *)line); + s = skipwhite(line); if (*s == '#') { // skip #define FOO x ? (x) : x return false; } @@ -1190,24 +1145,23 @@ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) return false; } - cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; - - /* Search for a line starting with '#', empty, ending in ';' or containing - * '{' or '}' and start below it. This handles the following situations: - * a = cond ? - * func() : - * asdf; - * func::foo() - * : something - * {} - * Foo::Foo (int one, int two) - * : something(4), - * somethingelse(3) - * {} - */ + cpp_base_class = lookfor_ctor_init = class_or_struct = false; + + // Search for a line starting with '#', empty, ending in ';' or containing + // '{' or '}' and start below it. This handles the following situations: + // a = cond ? + // func() : + // asdf; + // func::foo() + // : something + // {} + // Foo::Foo (int one, int two) + // : something(4), + // somethingelse(3) + // {} while (lnum > 1) { line = ml_get(lnum - 1); - s = (char_u *)skipwhite((char *)line); + s = skipwhite(line); if (*s == '#' || *s == NUL) { break; } @@ -1254,13 +1208,13 @@ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) s = skip_string(s) + 1; } else if (s[0] == ':') { if (s[1] == ':') { - /* skip double colon. It can't be a constructor - * initialization any more */ - lookfor_ctor_init = FALSE; + // skip double colon. It can't be a constructor + // initialization any more + lookfor_ctor_init = false; s = cin_skipcomment(s + 2); } else if (lookfor_ctor_init || class_or_struct) { - /* we have something found, that looks like the start of - * cpp-base-class-declaration or constructor-initialization */ + // we have something found, that looks like the start of + // cpp-base-class-declaration or constructor-initialization cpp_base_class = true; lookfor_ctor_init = class_or_struct = false; pos->col = 0; @@ -1268,10 +1222,10 @@ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) } else { s = cin_skipcomment(s + 1); } - } else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) - || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) { - class_or_struct = TRUE; - lookfor_ctor_init = FALSE; + } else if ((strncmp(s, "class", 5) == 0 && !vim_isIDc((uint8_t)s[5])) + || (strncmp(s, "struct", 6) == 0 && !vim_isIDc((uint8_t)s[6]))) { + class_or_struct = true; + lookfor_ctor_init = false; if (*s == 'c') { s = cin_skipcomment(s + 5); @@ -1280,16 +1234,16 @@ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) } } else { if (s[0] == '{' || s[0] == '}' || s[0] == ';') { - cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; + cpp_base_class = lookfor_ctor_init = class_or_struct = false; } else if (s[0] == ')') { - /* Constructor-initialization is assumed if we come across - * something like "):" */ - class_or_struct = FALSE; - lookfor_ctor_init = TRUE; + // Constructor-initialization is assumed if we come across + // something like "):" + class_or_struct = false; + lookfor_ctor_init = true; } else if (s[0] == '?') { // Avoid seeing '() :' after '?' as constructor init. return false; - } else if (!vim_isIDc(s[0])) { + } else if (!vim_isIDc((uint8_t)s[0])) { // if it is not an identifier, we are wrong class_or_struct = false; lookfor_ctor_init = false; @@ -1331,7 +1285,7 @@ static int get_baseclass_amount(int col) && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) { amount = get_indent_lnum(trypos->lnum); // XXX } - if (!cin_ends_in(get_cursor_line_ptr(), (char_u *)",", NULL)) { + if (!cin_ends_in(get_cursor_line_ptr(), ",", NULL)) { amount += curbuf->b_ind_cpp_baseclass; } } else { @@ -1345,23 +1299,21 @@ static int get_baseclass_amount(int col) return amount; } -/* - * Return TRUE if string "s" ends with the string "find", possibly followed by - * white space and comments. Skip strings and comments. - * Ignore "ignore" after "find" if it's not NULL. - */ -static int cin_ends_in(const char_u *s, const char_u *find, const char_u *ignore) +/// Return true if string "s" ends with the string "find", possibly followed by +/// white space and comments. Skip strings and comments. +/// Ignore "ignore" after "find" if it's not NULL. +static int cin_ends_in(const char *s, const char *find, const char *ignore) { - const char_u *p = s; - const char_u *r; - int len = (int)STRLEN(find); + const char *p = s; + const char *r; + int len = (int)strlen(find); while (*p != NUL) { p = cin_skipcomment(p); - if (STRNCMP(p, find, len) == 0) { - r = (char_u *)skipwhite((char *)p + len); - if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) { - r = (char_u *)skipwhite((char *)r + STRLEN(ignore)); + if (strncmp(p, find, (size_t)len) == 0) { + r = skipwhite(p + len); + if (ignore != NULL && strncmp(r, ignore, strlen(ignore)) == 0) { + r = skipwhite(r + strlen(ignore)); } if (cin_nocode(r)) { return true; @@ -1371,31 +1323,29 @@ static int cin_ends_in(const char_u *s, const char_u *find, const char_u *ignore p++; } } - return FALSE; + return false; } -/* - * Return TRUE when "s" starts with "word" and then a non-ID character. - */ -static int cin_starts_with(const char_u *s, const char *word) +/// Return true when "s" starts with "word" and then a non-ID character. +static int cin_starts_with(const char *s, const char *word) { - int l = (int)STRLEN(word); + size_t l = strlen(word); - return STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]); + return strncmp(s, word, l) == 0 && !vim_isIDc((uint8_t)s[l]); } /// Recognize a `extern "C"` or `extern "C++"` linkage specifications. -static int cin_is_cpp_extern_c(const char_u *s) +static int cin_is_cpp_extern_c(const char *s) { - const char_u *p; + const char *p; int has_string_literal = false; s = cin_skipcomment(s); - if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) { - p = cin_skipcomment((char_u *)skipwhite((char *)s + 6)); + if (strncmp(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc((uint8_t)s[6]))) { + p = cin_skipcomment(skipwhite(s + 6)); while (*p != NUL) { if (ascii_iswhite(*p)) { - p = cin_skipcomment((char_u *)skipwhite((char *)p)); + p = cin_skipcomment(skipwhite(p)); } else if (*p == '{') { break; } else if (p[0] == '"' && p[1] == 'C' && p[2] == '"') { @@ -1420,17 +1370,16 @@ static int cin_is_cpp_extern_c(const char_u *s) return false; } -/* - * Skip strings, chars and comments until at or past "trypos". - * Return the column found. - */ +// Skip strings, chars and comments until at or past "trypos". +// Return the column found. static int cin_skip2pos(pos_T *trypos) { - const char_u *line; - const char_u *p; - const char_u *new_p; + const char *line; + const char *p; + const char *new_p; - p = line = ml_get(trypos->lnum); + line = ml_get(trypos->lnum); + p = line; while (*p && (colnr_T)(p - line) < trypos->col) { if (cin_iscomment(p)) { p = cin_skipcomment(p); @@ -1446,11 +1395,10 @@ static int cin_skip2pos(pos_T *trypos) return (int)(p - line); } -/* - * Find the '{' at the start of the block we are in. - * Return NULL if no match found. - * Ignore a '{' that is in a comment, makes indenting the next three lines - * work. */ +// Find the '{' at the start of the block we are in. +// Return NULL if no match found. +// Ignore a '{' that is in a comment, makes indenting the next three lines +// work. // foo() // { // } @@ -1488,7 +1436,7 @@ static pos_T *find_match_paren(int ind_maxparen) return find_match_char('(', ind_maxparen); } -static pos_T *find_match_char(char_u c, int ind_maxparen) +static pos_T *find_match_char(char c, int ind_maxparen) { pos_T cursor_save; pos_T *trypos; @@ -1498,7 +1446,7 @@ static pos_T *find_match_char(char_u c, int ind_maxparen) cursor_save = curwin->w_cursor; ind_maxp_wk = ind_maxparen; retry: - if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL) { + if ((trypos = findmatchlimit(NULL, (uint8_t)c, 0, ind_maxp_wk)) != NULL) { // check if the ( is in a // comment if ((colnr_T)cin_skip2pos(trypos) > trypos->col) { ind_maxp_wk = ind_maxparen - (cursor_save.lnum - trypos->lnum); @@ -1550,12 +1498,10 @@ static pos_T *find_match_paren_after_brace(int ind_maxparen) return trypos; } -/* - * Return ind_maxparen corrected for the difference in line number between the - * cursor position and "startpos". This makes sure that searching for a - * matching paren above the cursor line doesn't find a match because of - * looking a few lines further. - */ +// Return ind_maxparen corrected for the difference in line number between the +// cursor position and "startpos". This makes sure that searching for a +// matching paren above the cursor line doesn't find a match because of +// looking a few lines further. static int corr_ind_maxparen(pos_T *startpos) { long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; @@ -1566,14 +1512,12 @@ static int corr_ind_maxparen(pos_T *startpos) return curbuf->b_ind_maxparen; } -/* - * Set w_cursor.col to the column number of the last unmatched ')' or '{' in - * line "l". "l" must point to the start of the line. - */ -static int find_last_paren(const char_u *l, int start, int end) +// Set w_cursor.col to the column number of the last unmatched ')' or '{' in +// line "l". "l" must point to the start of the line. +static int find_last_paren(const char *l, char start, char end) { int i; - int retval = FALSE; + int retval = false; int open_count = 0; curwin->w_cursor.col = 0; // default is start of line @@ -1588,17 +1532,15 @@ static int find_last_paren(const char_u *l, int start, int end) open_count--; } else { curwin->w_cursor.col = i; - retval = TRUE; + retval = true; } } } return retval; } -/* - * Parse 'cinoptions' and set the values in "curbuf". - * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes. - */ +// Parse 'cinoptions' and set the values in "curbuf". +// Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes. void parse_cino(buf_T *buf) { char *p; @@ -1607,39 +1549,37 @@ void parse_cino(buf_T *buf) int fraction = 0; int sw = get_sw_value(buf); - /* - * Set the default values. - */ - /* Spaces from a block's opening brace the prevailing indent for that - * block should be. */ + // Set the default values. + // Spaces from a block's opening brace the prevailing indent for that + // block should be. buf->b_ind_level = sw; - /* Spaces from the edge of the line an open brace that's at the end of a - * line is imagined to be. */ + // Spaces from the edge of the line an open brace that's at the end of a + // line is imagined to be. buf->b_ind_open_imag = 0; - /* Spaces from the prevailing indent for a line that is not preceded by - * an opening brace. */ + // Spaces from the prevailing indent for a line that is not preceded by + // an opening brace. buf->b_ind_no_brace = 0; // Column where the first { of a function should be located }. buf->b_ind_first_open = 0; - /* Spaces from the prevailing indent a leftmost open brace should be - * located. */ + // Spaces from the prevailing indent a leftmost open brace should be + // located. buf->b_ind_open_extra = 0; - /* Spaces from the matching open brace (real location for one at the left - * edge; imaginary location from one that ends a line) the matching close - * brace should be located. */ + // Spaces from the matching open brace (real location for one at the left + // edge; imaginary location from one that ends a line) the matching close + // brace should be located. buf->b_ind_close_extra = 0; - /* Spaces from the edge of the line an open brace sitting in the leftmost - * column is imagined to be. */ + // Spaces from the edge of the line an open brace sitting in the leftmost + // column is imagined to be. buf->b_ind_open_left_imag = 0; - /* Spaces jump labels should be shifted to the left if N is non-negative, - * otherwise the jump label will be put to column 1. */ + // Spaces jump labels should be shifted to the left if N is non-negative, + // otherwise the jump label will be put to column 1. buf->b_ind_jump_label = -1; // Spaces from the switch() indent a "case xx" label should be located. @@ -1651,8 +1591,8 @@ void parse_cino(buf_T *buf) // Lineup break at end of case in switch() with case label. buf->b_ind_case_break = 0; - /* Spaces from the class declaration indent a scope declaration label - * should be located. */ + // Spaces from the class declaration indent a scope declaration label + // should be located. buf->b_ind_scopedecl = sw; // Spaces from the scope declaration label code should be located. @@ -1664,32 +1604,32 @@ void parse_cino(buf_T *buf) // Amount a function type spec should be indented. buf->b_ind_func_type = sw; - /* Amount a cpp base class declaration or constructor initialization - * should be indented. */ + // Amount a cpp base class declaration or constructor initialization + // should be indented. buf->b_ind_cpp_baseclass = sw; - /* additional spaces beyond the prevailing indent a continuation line - * should be located. */ + // additional spaces beyond the prevailing indent a continuation line + // should be located. buf->b_ind_continuation = sw; // Spaces from the indent of the line with an unclosed parentheses. buf->b_ind_unclosed = sw * 2; - /* Spaces from the indent of the line with an unclosed parentheses, which - * itself is also unclosed. */ + // Spaces from the indent of the line with an unclosed parentheses, which + // itself is also unclosed. buf->b_ind_unclosed2 = sw; // Suppress ignoring spaces from the indent of a line starting with an // unclosed parenthesis. buf->b_ind_unclosed_noignore = 0; - /* If the opening paren is the last nonwhite character on the line, and - * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer - * context (for very long lines). */ + // If the opening paren is the last nonwhite character on the line, and + // b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer + // context (for very long lines). buf->b_ind_unclosed_wrapped = 0; - /* Suppress ignoring white space when lining up with the character after - * an unclosed parentheses. */ + // Suppress ignoring white space when lining up with the character after + // an unclosed parentheses. buf->b_ind_unclosed_whiteok = 0; // Indent a closing parenthesis under the line start of the matching @@ -1705,8 +1645,8 @@ void parse_cino(buf_T *buf) // Spaces from the comment opener when there is nothing after it. buf->b_ind_in_comment = 3; - /* Boolean: if non-zero, use b_ind_in_comment even if there is something - * after the comment opener. */ + // Boolean: if non-zero, use b_ind_in_comment even if there is something + // after the comment opener. buf->b_ind_in_comment2 = 0; // Max lines to search for an open paren. @@ -1727,8 +1667,8 @@ void parse_cino(buf_T *buf) // Handle C++ namespace. buf->b_ind_cpp_namespace = 0; - /* Handle continuation lines containing conditions of if(), for() and - * while(). */ + // Handle continuation lines containing conditions of if(), for() and + // while(). buf->b_ind_if_for_while = 0; // indentation for # comments @@ -1740,7 +1680,7 @@ void parse_cino(buf_T *buf) // Handle C #pragma directives buf->b_ind_pragma = 0; - for (p = (char *)buf->b_p_cino; *p;) { + for (p = buf->b_p_cino; *p;) { l = p++; if (*p == '-') { p++; @@ -1774,8 +1714,8 @@ void parse_cino(buf_T *buf) n = -n; } - /* When adding an entry here, also update the default 'cinoptions' in - * doc/indent.txt, and add explanation for it! */ + // When adding an entry here, also update the default 'cinoptions' in + // doc/indent.txt, and add explanation for it! switch (*l) { case '>': buf->b_ind_level = n; @@ -1895,10 +1835,8 @@ void parse_cino(buf_T *buf) } } -/* - * Return the desired indent for C code. - * Return -1 if the indent should be left alone (inside a raw string). - */ +// Return the desired indent for C code. +// Return -1 if the indent should be left alone (inside a raw string). int get_c_indent(void) { pos_T cur_curpos; @@ -1906,22 +1844,22 @@ int get_c_indent(void) int scope_amount; int cur_amount = MAXCOL; colnr_T col; - char_u *theline; - char_u *linecopy; + char *theline; + char *linecopy; pos_T *trypos; pos_T *comment_pos; pos_T *tryposBrace = NULL; pos_T tryposCopy; pos_T our_paren_pos; - char_u *start; + char *start; int start_brace; #define BRACE_IN_COL0 1 // '{' is in column 0 #define BRACE_AT_START 2 // '{' is at start of line #define BRACE_AT_END 3 // '{' is at end of line linenr_T ourscope; - const char_u *l; - const char_u *look; - char_u terminated; + const char *l; + const char *look; + char terminated; int lookfor; #define LOOKFOR_INITIAL 0 #define LOOKFOR_IF 1 @@ -1959,25 +1897,23 @@ int get_c_indent(void) return 0; } - /* Get a copy of the current contents of the line. - * This is required, because only the most recent line obtained with - * ml_get is valid! */ - linecopy = vim_strsave(ml_get(cur_curpos.lnum)); + // Get a copy of the current contents of the line. + // This is required, because only the most recent line obtained with + // ml_get is valid! + linecopy = xstrdup(ml_get(cur_curpos.lnum)); - /* - * In insert mode and the cursor is on a ')' truncate the line at the - * cursor position. We don't want to line up with the matching '(' when - * inserting new stuff. - * For unknown reasons the cursor might be past the end of the line, thus - * check for that. - */ + // In insert mode and the cursor is on a ')' truncate the line at the + // cursor position. We don't want to line up with the matching '(' when + // inserting new stuff. + // For unknown reasons the cursor might be past the end of the line, thus + // check for that. if ((State & MODE_INSERT) - && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) + && curwin->w_cursor.col < (colnr_T)strlen(linecopy) && linecopy[curwin->w_cursor.col] == ')') { linecopy[curwin->w_cursor.col] = NUL; } - theline = (char_u *)skipwhite((char *)linecopy); + theline = skipwhite(linecopy); // move the cursor to the start of the line @@ -1985,10 +1921,8 @@ int get_c_indent(void) original_line_islabel = cin_islabel(); // XXX - /* - * If we are inside a raw string don't change the indent. - * Ignore a raw string inside a comment. - */ + // If we are inside a raw string don't change the indent. + // Ignore a raw string inside a comment. comment_pos = ind_find_start_comment(); if (comment_pos != NULL) { // findmatchlimit() static pos is overwritten, make a copy @@ -2004,8 +1938,8 @@ int get_c_indent(void) // #defines and so on go at the left when included in 'cinkeys', // excluding pragmas when customized in 'cinoptions' if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', true))) { - const char_u *const directive = (char_u *)skipwhite((char *)theline + 1); - if (curbuf->b_ind_pragma == 0 || STRNCMP(directive, "pragma", 6) != 0) { + const char *const directive = skipwhite(theline + 1); + if (curbuf->b_ind_pragma == 0 || strncmp(directive, "pragma", 6) != 0) { amount = curbuf->b_ind_hash_comment; goto theend; } @@ -2018,10 +1952,8 @@ int get_c_indent(void) amount = 0; goto theend; } - /* - * If we're inside a "//" comment and there is a "//" comment in a - * previous line, lineup with that one. - */ + // If we're inside a "//" comment and there is a "//" comment in a + // previous line, lineup with that one. if (cin_islinecomment(theline)) { pos_T linecomment_pos; @@ -2042,10 +1974,8 @@ int get_c_indent(void) goto theend; } } - /* - * If we're inside a comment and not looking at the start of the - * comment, try using the 'comments' option. - */ + // If we're inside a comment and not looking at the start of the + // comment, try using the 'comments' option. if (!cin_iscomment(theline) && comment_pos != NULL) { // XXX int lead_start_len = 2; int lead_middle_len = 1; @@ -2055,7 +1985,7 @@ int get_c_indent(void) char *p; int start_align = 0; int start_off = 0; - int done = FALSE; + int done = false; // find how indented the line beginning the comment is getvcol(curwin, comment_pos, &col, NULL, NULL); @@ -2063,7 +1993,7 @@ int get_c_indent(void) *lead_start = NUL; *lead_middle = NUL; - p = (char *)curbuf->b_p_com; + p = curbuf->b_p_com; while (*p != NUL) { int align = 0; int off = 0; @@ -2087,31 +2017,31 @@ int get_c_indent(void) (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); if (what == COM_START) { STRCPY(lead_start, lead_end); - lead_start_len = (int)STRLEN(lead_start); + lead_start_len = (int)strlen(lead_start); start_off = off; start_align = align; } else if (what == COM_MIDDLE) { STRCPY(lead_middle, lead_end); - lead_middle_len = (int)STRLEN(lead_middle); + lead_middle_len = (int)strlen(lead_middle); } else if (what == COM_END) { - /* If our line starts with the middle comment string, line it - * up with the comment opener per the 'comments' option. */ - if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 - && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) { - done = TRUE; + // If our line starts with the middle comment string, line it + // up with the comment opener per the 'comments' option. + if (strncmp(theline, lead_middle, (size_t)lead_middle_len) == 0 + && strncmp(theline, lead_end, strlen(lead_end)) != 0) { + done = true; if (curwin->w_cursor.lnum > 1) { - /* If the start comment string matches in the previous - * line, use the indent of that line plus offset. If - * the middle comment string matches in the previous - * line, use the indent of that line. XXX */ - look = (char_u *)skipwhite((char *)ml_get(curwin->w_cursor.lnum - 1)); - if (STRNCMP(look, lead_start, lead_start_len) == 0) { + // If the start comment string matches in the previous + // line, use the indent of that line plus offset. If + // the middle comment string matches in the previous + // line, use the indent of that line. XXX + look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); + if (strncmp(look, lead_start, (size_t)lead_start_len) == 0) { amount = get_indent_lnum(curwin->w_cursor.lnum - 1); - } else if (STRNCMP(look, lead_middle, lead_middle_len) == 0) { + } else if (strncmp(look, lead_middle, (size_t)lead_middle_len) == 0) { amount = get_indent_lnum(curwin->w_cursor.lnum - 1); break; - } else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col, - lead_start, lead_start_len) != 0) { + } else if (strncmp(ml_get(comment_pos->lnum) + comment_pos->col, + lead_start, (size_t)lead_start_len) != 0) { // If the start comment string doesn't match with the // start of the comment, skip this entry. XXX continue; @@ -2125,10 +2055,10 @@ int get_c_indent(void) break; } - /* If our line starts with the end comment string, line it up - * with the middle comment */ - if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 - && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) { + // If our line starts with the end comment string, line it up + // with the middle comment + if (strncmp(theline, lead_middle, (size_t)lead_middle_len) != 0 + && strncmp(theline, lead_end, strlen(lead_end)) == 0) { amount = get_indent_lnum(curwin->w_cursor.lnum - 1); // XXX if (off != 0) { @@ -2142,10 +2072,9 @@ int get_c_indent(void) } } - /* If our line starts with an asterisk, line up with the - * asterisk in the comment opener; otherwise, line up - * with the first character of the comment text. - */ + // If our line starts with an asterisk, line up with the + // asterisk in the comment opener; otherwise, line up + // with the first character of the comment text. if (done) { // skip } else if (theline[0] == '*') { @@ -2169,7 +2098,7 @@ int get_c_indent(void) start = ml_get(comment_pos->lnum); look = start + comment_pos->col + 2; // skip / and * if (*look != NUL) { // if something after it - comment_pos->col = (colnr_T)((char_u *)skipwhite((char *)look) - start); + comment_pos->col = (colnr_T)(skipwhite(look) - start); } } getvcol(curwin, comment_pos, &col, NULL, NULL); @@ -2182,7 +2111,7 @@ int get_c_indent(void) goto theend; } // Are we looking at a ']' that has a match? - if (*skipwhite((char *)theline) == ']' + if (*skipwhite(theline) == ']' && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) { // align with the line containing the '['. amount = get_indent_lnum(trypos->lnum); @@ -2195,8 +2124,8 @@ int get_c_indent(void) || (tryposBrace = find_start_brace()) != NULL || trypos != NULL) { if (trypos != NULL && tryposBrace != NULL) { - /* Both an unmatched '(' and '{' is found. Use the one which is - * closer to the current cursor position, set the other to NULL. */ + // Both an unmatched '(' and '{' is found. Use the one which is + // closer to the current cursor position, set the other to NULL. if (trypos->lnum != tryposBrace->lnum ? trypos->lnum < tryposBrace->lnum : trypos->col < tryposBrace->col) { @@ -2208,17 +2137,15 @@ int get_c_indent(void) if (trypos != NULL) { our_paren_pos = *trypos; - /* - * If the matching paren is more than one line away, use the indent of - * a previous non-empty line that matches the same paren. - */ + // If the matching paren is more than one line away, use the indent of + // a previous non-empty line that matches the same paren. if (theline[0] == ')' && curbuf->b_ind_paren_prev) { // Line up with the start of the matching paren line. amount = get_indent_lnum(curwin->w_cursor.lnum - 1); // XXX } else { amount = -1; for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; lnum--) { - l = (char_u *)skipwhite((char *)ml_get(lnum)); + l = skipwhite(ml_get(lnum)); if (cin_nocode(l)) { // skip comment lines continue; } @@ -2251,22 +2178,20 @@ int get_c_indent(void) } } - /* - * Line up with line where the matching paren is. XXX - * If the line starts with a '(' or the indent for unclosed - * parentheses is zero, line up with the unclosed parentheses. - */ + // Line up with line where the matching paren is. XXX + // If the line starts with a '(' or the indent for unclosed + // parentheses is zero, line up with the unclosed parentheses. if (amount == -1) { int ignore_paren_col = 0; int is_if_for_while = 0; if (curbuf->b_ind_if_for_while) { - /* Look for the outermost opening parenthesis on this line - * and check whether it belongs to an "if", "for" or "while". */ + // Look for the outermost opening parenthesis on this line + // and check whether it belongs to an "if", "for" or "while". pos_T cursor_save = curwin->w_cursor; pos_T outermost; - char_u *line; + char *line; trypos = &our_paren_pos; do { @@ -2286,14 +2211,14 @@ int get_c_indent(void) } amount = skip_label(our_paren_pos.lnum, &look); - look = (char_u *)skipwhite((char *)look); + look = skipwhite(look); if (*look == '(') { linenr_T save_lnum = curwin->w_cursor.lnum; - char_u *line; + char *line; int look_col; - /* Ignore a '(' in front of the line that has a match before - * our matching '('. */ + // Ignore a '(' in front of the line that has a match before + // our matching '('. curwin->w_cursor.lnum = our_paren_pos.lnum; line = get_cursor_line_ptr(); look_col = (int)(look - line); @@ -2313,24 +2238,22 @@ int get_c_indent(void) && is_if_for_while == 0) || (!curbuf->b_ind_unclosed_noignore && *look == '(' && ignore_paren_col == 0)) { - /* - * If we're looking at a close paren, line up right there; - * otherwise, line up with the next (non-white) character. - * When b_ind_unclosed_wrapped is set and the matching paren is - * the last nonwhite character of the line, use either the - * indent of the current line or the indentation of the next - * outer paren and add b_ind_unclosed_wrapped (for very long - * lines). - */ + // If we're looking at a close paren, line up right there; + // otherwise, line up with the next (non-white) character. + // When b_ind_unclosed_wrapped is set and the matching paren is + // the last nonwhite character of the line, use either the + // indent of the current line or the indentation of the next + // outer paren and add b_ind_unclosed_wrapped (for very long + // lines). if (theline[0] != ')') { cur_amount = MAXCOL; l = ml_get(our_paren_pos.lnum); if (curbuf->b_ind_unclosed_wrapped - && cin_ends_in(l, (char_u *)"(", NULL)) { - /* look for opening unmatched paren, indent one level - * for each additional level */ + && cin_ends_in(l, "(", NULL)) { + // look for opening unmatched paren, indent one level + // for each additional level n = 1; - for (col = 0; col < our_paren_pos.col; ++col) { + for (col = 0; col < our_paren_pos.col; col++) { switch (l[col]) { case '(': case '{': @@ -2363,10 +2286,8 @@ int get_c_indent(void) } } - /* - * Find how indented the paren is, or the character after it - * if we did the above "if". - */ + // Find how indented the paren is, or the character after it + // if we did the above "if". if (our_paren_pos.col > 0) { getvcol(curwin, &our_paren_pos, &col, NULL, NULL); if (cur_amount > (int)col) { @@ -2384,11 +2305,11 @@ int get_c_indent(void) amount = cur_amount; } } else { - /* Add b_ind_unclosed2 for each '(' before our matching one, - * but ignore (void) before the line (ignore_paren_col). */ + // Add b_ind_unclosed2 for each '(' before our matching one, + // but ignore (void) before the line (ignore_paren_col). col = our_paren_pos.col; while ((int)our_paren_pos.col > ignore_paren_col) { - --our_paren_pos.col; + our_paren_pos.col--; switch (*ml_get_pos(&our_paren_pos)) { case '(': amount += curbuf->b_ind_unclosed2; @@ -2401,8 +2322,8 @@ int get_c_indent(void) } } - /* Use b_ind_unclosed once, when the first '(' is not inside - * braces */ + // Use b_ind_unclosed once, when the first '(' is not inside + // braces if (col == MAXCOL) { amount += curbuf->b_ind_unclosed; } else { @@ -2418,14 +2339,12 @@ int get_c_indent(void) } } } - /* - * For a line starting with ')' use the minimum of the two - * positions, to avoid giving it more indent than the previous - * lines: - * func_long_name( if (x - * arg && yy - * ) ^ not here ) ^ not here - */ + // For a line starting with ')' use the minimum of the two + // positions, to avoid giving it more indent than the previous + // lines: + // func_long_name( if (x + // arg && yy + // ) ^ not here ) ^ not here if (cur_amount < amount) { amount = cur_amount; } @@ -2447,13 +2366,11 @@ int get_c_indent(void) ourscope = trypos->lnum; start = ml_get(ourscope); - /* - * Now figure out how indented the line is in general. - * If the brace was at the start of the line, we use that; - * otherwise, check out the indentation of the line as - * a whole and then add the "imaginary indent" to that. - */ - look = (char_u *)skipwhite((char *)start); + // Now figure out how indented the line is in general. + // If the brace was at the start of the line, we use that; + // otherwise, check out the indentation of the line as + // a whole and then add the "imaginary indent" to that. + look = skipwhite(start); if (*look == '{') { getvcol(curwin, trypos, &col, NULL, NULL); amount = col; @@ -2480,7 +2397,7 @@ int get_c_indent(void) // ldfd) { // } if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label) - && cin_iscase((char_u *)skipwhite((char *)get_cursor_line_ptr()), false)) { + && cin_iscase(skipwhite(get_cursor_line_ptr()), false)) { amount = get_indent(); } else if (curbuf->b_ind_js) { amount = get_indent_lnum(lnum); @@ -2498,18 +2415,14 @@ int get_c_indent(void) // we want to be. Otherwise, add the amount of room // that an indent is supposed to be. if (theline[0] == '}') { - /* - * they may want closing braces to line up with something - * other than the open brace. indulge them, if so. - */ + // they may want closing braces to line up with something + // other than the open brace. indulge them, if so. amount += curbuf->b_ind_close_extra; } else { - /* - * If we're looking at an "else", try to find an "if" - * to match it with. - * If we're looking at a "while", try to find a "do" - * to match it with. - */ + // If we're looking at an "else", try to find an "if" + // to match it with. + // If we're looking at a "while", try to find a "do" + // to match it with. lookfor = LOOKFOR_INITIAL; if (cin_iselse(theline)) { lookfor = LOOKFOR_IF; @@ -2524,18 +2437,14 @@ int get_c_indent(void) } } - /* - * We get here if we are not on an "while-of-do" or "else" (or - * failed to find a matching "if"). - * Search backwards for something to line up with. - * First set amount for when we don't find anything. - */ + // We get here if we are not on an "while-of-do" or "else" (or + // failed to find a matching "if"). + // Search backwards for something to line up with. + // First set amount for when we don't find anything. - /* - * if the '{' is _really_ at the left margin, use the imaginary - * location of a left-margin brace. Otherwise, correct the - * location for b_ind_open_extra. - */ + // if the '{' is _really_ at the left margin, use the imaginary + // location of a left-margin brace. Otherwise, correct the + // location for b_ind_open_extra. if (start_brace == BRACE_IN_COL0) { // '{' is in column 0 amount = curbuf->b_ind_open_left_imag; @@ -2547,7 +2456,7 @@ int get_c_indent(void) if (start_brace == BRACE_AT_END) { // '{' is at end of line amount += curbuf->b_ind_open_imag; - l = (char_u *)skipwhite((char *)get_cursor_line_ptr()); + l = skipwhite(get_cursor_line_ptr()); if (cin_is_cpp_namespace(l)) { amount += curbuf->b_ind_cpp_namespace; } else if (cin_is_cpp_extern_c(l)) { @@ -2562,7 +2471,7 @@ int get_c_indent(void) } } - lookfor_break = FALSE; + lookfor_break = false; if (cin_iscase(theline, false)) { // it's a switch() label lookfor = LOOKFOR_CASE; // find a previous switch() label @@ -2594,10 +2503,8 @@ int get_c_indent(void) curwin->w_cursor.lnum--; curwin->w_cursor.col = 0; - /* - * If we went all the way back to the start of our scope, line - * up with it. - */ + // If we went all the way back to the start of our scope, line + // up with it. if (curwin->w_cursor.lnum <= ourscope) { // We reached end of scope: // If looking for a enum or structure initialization @@ -2611,9 +2518,9 @@ int get_c_indent(void) if (curwin->w_cursor.lnum == 0 || curwin->w_cursor.lnum < ourscope - curbuf->b_ind_maxparen) { - /* nothing found (abuse curbuf->b_ind_maxparen as - * limit) assume terminated line (i.e. a variable - * initialization) */ + // nothing found (abuse curbuf->b_ind_maxparen as + // limit) assume terminated line (i.e. a variable + // initialization) if (cont_amount > 0) { amount = cont_amount; } else if (!curbuf->b_ind_js) { @@ -2624,10 +2531,8 @@ int get_c_indent(void) l = get_cursor_line_ptr(); - /* - * If we're in a comment or raw string now, skip to - * the start of it. - */ + // If we're in a comment or raw string now, skip to + // the start of it. trypos = ind_find_start_CORS(NULL); if (trypos != NULL) { curwin->w_cursor.lnum = trypos->lnum + 1; @@ -2646,29 +2551,25 @@ int get_c_indent(void) continue; } - terminated = cin_isterminated(l, FALSE, TRUE); + terminated = cin_isterminated(l, false, true); - /* - * If we are at top level and the line looks like a - * function declaration, we are done - * (it's a variable declaration). - */ + // If we are at top level and the line looks like a + // function declaration, we are done + // (it's a variable declaration). if (start_brace != BRACE_IN_COL0 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) { - /* if the line is terminated with another ',' - * it is a continued variable initialization. - * don't add extra indent. - * TODO: does not work, if a function - * declaration is split over multiple lines: - * cin_isfuncdecl returns FALSE then. - */ + // if the line is terminated with another ',' + // it is a continued variable initialization. + // don't add extra indent. + // TODO(vim): does not work, if a function + // declaration is split over multiple lines: + // cin_isfuncdecl returns false then. if (terminated == ',') { break; } - /* if it is an enum declaration or an assignment, - * we are done. - */ + // if it is an enum declaration or an assignment, + // we are done. if (terminated != ';' && cin_isinit()) { break; } @@ -2700,11 +2601,10 @@ int get_c_indent(void) } } - /* it's a variable declaration, add indentation - * like in - * int a, - * b; - */ + // it's a variable declaration, add indentation + // like in + // int a, + // b; if (cont_amount > 0) { amount = cont_amount; } else { @@ -2728,10 +2628,8 @@ int get_c_indent(void) } if (lookfor_cpp_namespace) { - /* - * Looking for C++ namespace, need to look further - * back. - */ + // Looking for C++ namespace, need to look further + // back. if (curwin->w_cursor.lnum == ourscope) { continue; } @@ -2744,8 +2642,8 @@ int get_c_indent(void) l = get_cursor_line_ptr(); - /* If we're in a comment or raw string now, skip - * to the start of it. */ + // If we're in a comment or raw string now, skip + // to the start of it. trypos = ind_find_start_CORS(NULL); if (trypos != NULL) { curwin->w_cursor.lnum = trypos->lnum + 1; @@ -2787,20 +2685,18 @@ int get_c_indent(void) l = get_cursor_line_ptr(); - /* - * If this is a switch() label, may line up relative to that. - * If this is a C++ scope declaration, do the same. - */ + // If this is a switch() label, may line up relative to that. + // If this is a C++ scope declaration, do the same. bool iscase = cin_iscase(l, false); if (iscase || cin_isscopedecl(l)) { - /* we are only looking for cpp base class - * declaration/initialization any longer */ + // we are only looking for cpp base class + // declaration/initialization any longer if (lookfor == LOOKFOR_CPP_BASECLASS) { break; } - /* When looking for a "do" we are not interested in - * labels. */ + // When looking for a "do" we are not interested in + // labels. if (whilelevel > 0) { continue; } @@ -2873,14 +2769,12 @@ int get_c_indent(void) break; } - /* - * Try to get the indent of a statement before the switch - * label. If nothing is found, line up relative to the - * switch label. - * break; <- may line up with this line - * case xx: - * -> y = 1; - */ + // Try to get the indent of a statement before the switch + // label. If nothing is found, line up relative to the + // switch label. + // break; <- may line up with this line + // case xx: + // -> y = 1; scope_amount = get_indent() + (iscase // XXX ? curbuf->b_ind_case_code : curbuf->b_ind_scopedecl_code); @@ -2889,10 +2783,8 @@ int get_c_indent(void) continue; } - /* - * Looking for a switch() label or C++ scope declaration, - * ignore other lines, skip {}-blocks. - */ + // Looking for a switch() label or C++ scope declaration, + // ignore other lines, skip {}-blocks. if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) { if (find_last_paren(l, '{', '}') && (trypos = find_start_brace()) != NULL) { @@ -2902,9 +2794,7 @@ int get_c_indent(void) continue; } - /* - * Ignore jump labels with nothing after them. - */ + // Ignore jump labels with nothing after them. if (!curbuf->b_ind_js && cin_islabel()) { l = after_label(get_cursor_line_ptr()); if (l == NULL || cin_nocode(l)) { @@ -2912,12 +2802,10 @@ int get_c_indent(void) } } - /* - * Ignore #defines, #if, etc. - * Ignore comment and empty lines. - * (need to get the line again, cin_islabel() may have - * unlocked it) - */ + // Ignore #defines, #if, etc. + // Ignore comment and empty lines. + // (need to get the line again, cin_islabel() may have + // unlocked it) l = get_cursor_line_ptr(); if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount) || cin_nocode(l)) { @@ -2950,9 +2838,8 @@ int get_c_indent(void) } break; } else if (lookfor == LOOKFOR_CPP_BASECLASS) { - /* only look, whether there is a cpp base class - * declaration or initialization before the opening brace. - */ + // only look, whether there is a cpp base class + // declaration or initialization before the opening brace. if (cin_isterminated(l, true, false)) { break; } else { @@ -2960,18 +2847,16 @@ int get_c_indent(void) } } - /* - * What happens next depends on the line being terminated. - * If terminated with a ',' only consider it terminating if - * there is another unterminated statement behind, eg: - * 123, - * sizeof - * here - * Otherwise check whether it is an enumeration or structure - * initialisation (not indented) or a variable declaration - * (indented). - */ - terminated = cin_isterminated(l, FALSE, TRUE); + // What happens next depends on the line being terminated. + // If terminated with a ',' only consider it terminating if + // there is another unterminated statement behind, eg: + // 123, + // sizeof + // here + // Otherwise check whether it is an enumeration or structure + // initialisation (not indented) or a variable declaration + // (indented). + terminated = cin_isterminated(l, false, true); if (js_cur_has_key) { js_cur_has_key = false; // only check the first line @@ -2999,21 +2884,20 @@ int get_c_indent(void) // Line below current line is the one that starts a // (possibly broken) line ending in a comma. break; - } else { - amount = get_indent(); - if (curwin->w_cursor.lnum - 1 == ourscope) { - // line above is start of the scope, thus current - // line is the one that stars a (possibly broken) - // line ending in a comma. - break; - } + } + amount = get_indent(); + if (curwin->w_cursor.lnum - 1 == ourscope) { + // line above is start of the scope, thus current + // line is the one that stars a (possibly broken) + // line ending in a comma. + break; } } if (terminated == 0 || (lookfor != LOOKFOR_UNTERM && terminated == ',')) { if (lookfor != LOOKFOR_ENUM_OR_INIT - && (*skipwhite((char *)l) == '[' || l[STRLEN(l) - 1] == '[')) { + && (*skipwhite(l) == '[' || l[strlen(l) - 1] == '[')) { amount += ind_continuation; } // If we're in the middle of a paren thing, Go back to the line @@ -3040,12 +2924,10 @@ int get_c_indent(void) } if (trypos != NULL) { - /* - * Check if we are on a case label now. This is - * handled above. - * case xx: if ( asdf && - * asdf) - */ + // Check if we are on a case label now. This is + // handled above. + // case xx: if ( asdf && + // asdf) curwin->w_cursor = *trypos; l = get_cursor_line_ptr(); if (cin_iscase(l, false) || cin_isscopedecl(l)) { @@ -3055,17 +2937,15 @@ int get_c_indent(void) } } - /* - * Skip over continuation lines to find the one to get the - * indent from - * char *usethis = "bla\ - * bla", - * here; - */ + // Skip over continuation lines to find the one to get the + // indent from + // char *usethis = "bla{backslash} + // bla", + // here; if (terminated == ',') { while (curwin->w_cursor.lnum > 1) { l = ml_get(curwin->w_cursor.lnum - 1); - if (*l == NUL || l[STRLEN(l) - 1] != '\\') { + if (*l == NUL || l[strlen(l) - 1] != '\\') { break; } curwin->w_cursor.lnum--; @@ -3073,50 +2953,42 @@ int get_c_indent(void) } } - /* - * Get indent and pointer to text for current line, - * ignoring any jump label. XXX - */ + // Get indent and pointer to text for current line, + // ignoring any jump label. XXX if (curbuf->b_ind_js) { cur_amount = get_indent(); } else { cur_amount = skip_label(curwin->w_cursor.lnum, &l); } - /* - * If this is just above the line we are indenting, and it - * starts with a '{', line it up with this line. - * while (not) - * -> { - * } - */ + // If this is just above the line we are indenting, and it + // starts with a '{', line it up with this line. + // while (not) + // -> { + // } if (terminated != ',' && lookfor != LOOKFOR_TERM && theline[0] == '{') { amount = cur_amount; - /* - * Only add b_ind_open_extra when the current line - * doesn't start with a '{', which must have a match - * in the same line (scope is the same). Probably: - * { 1, 2 }, - * -> { 3, 4 } - */ - if (*skipwhite((char *)l) != '{') { + // Only add b_ind_open_extra when the current line + // doesn't start with a '{', which must have a match + // in the same line (scope is the same). Probably: + // { 1, 2 }, + // -> { 3, 4 } + if (*skipwhite(l) != '{') { amount += curbuf->b_ind_open_extra; } if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js) { - /* have to look back, whether it is a cpp base - * class declaration or initialization */ + // have to look back, whether it is a cpp base + // class declaration or initialization lookfor = LOOKFOR_CPP_BASECLASS; continue; } break; } - /* - * Check if we are after an "if", "while", etc. - * Also allow " } else". - */ - if (cin_is_cinword(l) || cin_iselse((char_u *)skipwhite((char *)l))) { + // Check if we are after an "if", "while", etc. + // Also allow " } else". + if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) { // Found an unterminated line after an if (), line up // with the last one. // if (cond) @@ -3132,19 +3004,17 @@ int get_c_indent(void) break; } - /* - * If this is just above the line we are indenting, we - * are finished. - * while (not) - * -> here; - * Otherwise this indent can be used when the line - * before this is terminated. - * yyy; - * if (stat) - * while (not) - * xxx; - * -> here; - */ + // If this is just above the line we are indenting, we + // are finished. + // while (not) + // -> here; + // Otherwise this indent can be used when the line + // before this is terminated. + // yyy; + // if (stat) + // while (not) + // xxx; + // -> here; amount = cur_amount; if (theline[0] == '{') { amount += curbuf->b_ind_open_extra; @@ -3155,14 +3025,12 @@ int get_c_indent(void) break; } - /* - * Special trick: when expecting the while () after a - * do, line up with the while() - * do - * x = 1; - * -> here - */ - l = (char_u *)skipwhite((char *)get_cursor_line_ptr()); + // Special trick: when expecting the while () after a + // do, line up with the while() + // do + // x = 1; + // -> here + l = skipwhite(get_cursor_line_ptr()); if (cin_isdo(l)) { if (whilelevel == 0) { break; @@ -3170,16 +3038,14 @@ int get_c_indent(void) whilelevel--; } - /* - * When searching for a terminated line, don't use the - * one between the "if" and the matching "else". - * Need to use the scope of this "else". XXX - * If whilelevel != 0 continue looking for a "do {". - */ + // When searching for a terminated line, don't use the + // one between the "if" and the matching "else". + // Need to use the scope of this "else". XXX + // If whilelevel != 0 continue looking for a "do {". if (cin_iselse(l) && whilelevel == 0) { - /* If we're looking at "} else", let's make sure we - * find the opening brace of the enclosing scope, - * not the one from "if () {". */ + // If we're looking at "} else", let's make sure we + // find the opening brace of the enclosing scope, + // not the one from "if () {". if (*l == '}') { curwin->w_cursor.col = (colnr_T)(l - get_cursor_line_ptr()) + 1; @@ -3191,21 +3057,17 @@ int get_c_indent(void) break; } } - } - /* - * If we're below an unterminated line that is not an - * "if" or something, we may line up with this line or - * add something for a continuation line, depending on - * the line before this one. - */ - else { - /* - * Found two unterminated lines on a row, line up with - * the last one. - * c = 99 + - * 100 + - * -> here; - */ + } else { + // If we're below an unterminated line that is not an + // "if" or something, we may line up with this line or + // add something for a continuation line, depending on + // the line before this one. + + // Found two unterminated lines on a row, line up with + // the last one. + // c = 99 + + // 100 + + // -> here; if (lookfor == LOOKFOR_UNTERM) { // When line ends in a comma add extra indent if (terminated == ',') { @@ -3215,11 +3077,11 @@ int get_c_indent(void) } if (lookfor == LOOKFOR_ENUM_OR_INIT) { - /* Found two lines ending in ',', lineup with the - * lowest one, but check for cpp base class - * declaration/initialization, if it is an - * opening brace or we are looking just for - * enumerations/initializations. */ + // Found two lines ending in ',', lineup with the + // lowest one, but check for cpp base class + // declaration/initialization, if it is an + // opening brace or we are looking just for + // enumerations/initializations. if (terminated == ',') { if (curbuf->b_ind_cpp_baseclass == 0) { break; @@ -3242,9 +3104,9 @@ int get_c_indent(void) l = get_cursor_line_ptr(); amount = cur_amount; - n = (int)STRLEN(l); + n = (int)strlen(l); if (terminated == ',' - && (*skipwhite((char *)l) == ']' + && (*skipwhite(l) == ']' || (n >= 2 && l[n - 2] == ']'))) { break; } @@ -3271,7 +3133,7 @@ int get_c_indent(void) // 4 * // 5, // 6, - if (cin_iscomment((char_u *)skipwhite((char *)l))) { + if (cin_iscomment(skipwhite(l))) { break; } lookfor = LOOKFOR_COMMA; @@ -3291,7 +3153,7 @@ int get_c_indent(void) } else { if (lookfor == LOOKFOR_INITIAL && *l != NUL - && l[STRLEN(l) - 1] == '\\') { + && l[strlen(l) - 1] == '\\') { // XXX cont_amount = cin_get_equal_amount(curwin->w_cursor.lnum); } @@ -3304,19 +3166,14 @@ int get_c_indent(void) } } } - } - /* - * Check if we are after a while (cond); - * If so: Ignore until the matching "do". - */ - else if (cin_iswhileofdo_end(terminated)) { // XXX - /* - * Found an unterminated line after a while ();, line up - * with the last one. - * while (cond); - * 100 + <- line up with this one - * -> here; - */ + // Check if we are after a while (cond); + // If so: Ignore until the matching "do". + } else if (cin_iswhileofdo_end((uint8_t)terminated)) { // XXX + // Found an unterminated line after a while ();, line up + // with the last one. + // while (cond); + // 100 + <- line up with this one + // -> here; if (lookfor == LOOKFOR_UNTERM || lookfor == LOOKFOR_ENUM_OR_INIT) { if (cont_amount > 0) { @@ -3335,28 +3192,22 @@ int get_c_indent(void) } } whilelevel++; - } - /* - * We are after a "normal" statement. - * If we had another statement we can stop now and use the - * indent of that other statement. - * Otherwise the indent of the current statement may be used, - * search backwards for the next "normal" statement. - */ - else { - /* - * Skip single break line, if before a switch label. It - * may be lined up with the case label. - */ + } else { + // We are after a "normal" statement. + // If we had another statement we can stop now and use the + // indent of that other statement. + // Otherwise the indent of the current statement may be used, + // search backwards for the next "normal" statement. + + // Skip single break line, if before a switch label. It + // may be lined up with the case label. if (lookfor == LOOKFOR_NOBREAK - && cin_isbreak((char_u *)skipwhite((char *)get_cursor_line_ptr()))) { + && cin_isbreak(skipwhite(get_cursor_line_ptr()))) { lookfor = LOOKFOR_ANY; continue; } - /* - * Handle "do {" line. - */ + // Handle "do {" line. if (whilelevel > 0) { l = cin_skipcomment(get_cursor_line_ptr()); if (cin_isdo(l)) { @@ -3366,17 +3217,15 @@ int get_c_indent(void) } } - /* - * Found a terminated line above an unterminated line. Add - * the amount for a continuation line. - * x = 1; - * y = foo + - * -> here; - * or - * int x = 1; - * int foo, - * -> here; - */ + // Found a terminated line above an unterminated line. Add + // the amount for a continuation line. + // x = 1; + // y = foo + + // -> here; + // or + // int x = 1; + // int foo, + // -> here; if (lookfor == LOOKFOR_UNTERM || lookfor == LOOKFOR_ENUM_OR_INIT) { if (cont_amount > 0) { @@ -3387,34 +3236,28 @@ int get_c_indent(void) break; } - /* - * Found a terminated line above a terminated line or "if" - * etc. line. Use the amount of the line below us. - * x = 1; x = 1; - * if (asdf) y = 2; - * while (asdf) ->here; - * here; - * ->foo; - */ + // Found a terminated line above a terminated line or "if" + // etc. line. Use the amount of the line below us. + // x = 1; x = 1; + // if (asdf) y = 2; + // while (asdf) ->here; + // here; + // ->foo; if (lookfor == LOOKFOR_TERM) { if (!lookfor_break && whilelevel == 0) { break; } - } - /* - * First line above the one we're indenting is terminated. - * To know what needs to be done look further backward for - * a terminated line. - */ - else { - /* - * position the cursor over the rightmost paren, so - * that matching it will take us back to the start of - * the line. Helps for: - * func(asdr, - * asdfasdf); - * here; - */ + } else { + // First line above the one we're indenting is terminated. + // To know what needs to be done look further backward for + // a terminated line. + + // position the cursor over the rightmost paren, so + // that matching it will take us back to the start of + // the line. Helps for: + // func(asdr, + // asdfasdf); + // here; term_again: l = get_cursor_line_ptr(); if (find_last_paren(l, '(', ')') @@ -3432,41 +3275,36 @@ term_again: } } - /* When aligning with the case statement, don't align - * with a statement after it. - * case 1: { <-- don't use this { position - * stat; - * } - * case 2: - * stat; - * } - */ + // When aligning with the case statement, don't align + // with a statement after it. + // case 1: { <-- don't use this { position + // stat; + // } + // case 2: + // stat; + // } iscase = curbuf->b_ind_keep_case_label && cin_iscase(l, false); - /* - * Get indent and pointer to text for current line, - * ignoring any jump label. - */ + // Get indent and pointer to text for current line, + // ignoring any jump label. amount = skip_label(curwin->w_cursor.lnum, &l); if (theline[0] == '{') { amount += curbuf->b_ind_open_extra; } // See remark above: "Only add b_ind_open_extra.." - l = (char_u *)skipwhite((char *)l); + l = skipwhite(l); if (*l == '{') { amount -= curbuf->b_ind_open_extra; } lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; - /* - * When a terminated line starts with "else" skip to - * the matching "if": - * else 3; - * indent this; - * Need to use the scope of this "else". XXX - * If whilelevel != 0 continue looking for a "do {". - */ + // When a terminated line starts with "else" skip to + // the matching "if": + // else 3; + // indent this; + // Need to use the scope of this "else". XXX + // If whilelevel != 0 continue looking for a "do {". if (lookfor == LOOKFOR_TERM && *l != '}' && cin_iselse(l) @@ -3479,10 +3317,8 @@ term_again: continue; } - /* - * If we're at the end of a block, skip to the start of - * that block. - */ + // If we're at the end of a block, skip to the start of + // that block. l = get_cursor_line_ptr(); if (find_last_paren(l, '{', '}') // XXX && (trypos = find_start_brace()) != NULL) { @@ -3529,19 +3365,17 @@ term_again: amount = curbuf->b_ind_first_open; goto theend; } - /* - * If the NEXT line is a function declaration, the current - * line needs to be indented as a function type spec. - * Don't do this if the current line looks like a comment or if the - * current line is terminated, ie. ends in ';', or if the current line - * contains { or }: "void f() {\n if (1)" - */ + // If the NEXT line is a function declaration, the current + // line needs to be indented as a function type spec. + // Don't do this if the current line looks like a comment or if the + // current line is terminated, ie. ends in ';', or if the current line + // contains { or }: "void f() {\n if (1)" if (cur_curpos.lnum < curbuf->b_ml.ml_line_count && !cin_nocode(theline) - && vim_strchr((char *)theline, '{') == NULL - && vim_strchr((char *)theline, '}') == NULL - && !cin_ends_in(theline, (char_u *)":", NULL) - && !cin_ends_in(theline, (char_u *)",", NULL) + && vim_strchr(theline, '{') == NULL + && vim_strchr(theline, '}') == NULL + && !cin_ends_in(theline, ":", NULL) + && !cin_ends_in(theline, ",", NULL) && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, cur_curpos.lnum + 1) && !cin_isterminated(theline, false, true)) { amount = curbuf->b_ind_func_type; @@ -3590,35 +3424,32 @@ term_again: continue; } - /* - * If the previous line ends in ',', use one level of - * indentation: - * int foo, - * bar; - * do this before checking for '}' in case of eg. - * enum foobar - * { - * ... - * } foo, - * bar; - */ - if (cin_ends_in(l, (char_u *)",", NULL) - || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) { + // If the previous line ends in ',', use one level of + // indentation: + // int foo, + // bar; + // do this before checking for '}' in case of eg. + // enum foobar + // { + // ... + // } foo, + // bar; + if (cin_ends_in(l, ",", NULL) + || (*l != NUL && (n = (uint8_t)l[strlen(l) - 1]) == '\\')) { // take us back to opening paren if (find_last_paren(l, '(', ')') && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) { curwin->w_cursor = *trypos; } - /* For a line ending in ',' that is a continuation line go - * back to the first line with a backslash: - * char *foo = "bla\ - * bla", - * here; - */ + // For a line ending in ',' that is a continuation line go + // back to the first line with a backslash: + // char *foo = "bla{backslash} + // bla", + // here; while (n == 0 && curwin->w_cursor.lnum > 1) { l = ml_get(curwin->w_cursor.lnum - 1); - if (*l == NUL || l[STRLEN(l) - 1] != '\\') { + if (*l == NUL || l[strlen(l) - 1] != '\\') { break; } curwin->w_cursor.lnum--; @@ -3636,20 +3467,16 @@ term_again: break; } - /* - * If the line looks like a function declaration, and we're - * not in a comment, put it the left margin. - */ + // If the line looks like a function declaration, and we're + // not in a comment, put it the left margin. if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) { // XXX break; } l = get_cursor_line_ptr(); - /* - * Finding the closing '}' of a previous function. Put - * current line at the left margin. For when 'cino' has "fs". - */ - if (*skipwhite((char *)l) == '}') { + // Finding the closing '}' of a previous function. Put + // current line at the left margin. For when 'cino' has "fs". + if (*skipwhite(l) == '}') { break; } @@ -3658,7 +3485,7 @@ term_again: // comments) align at column 0. For example: // char *string_array[] = { "foo", // / * x * / "b};ar" }; / * foobar * / - if (cin_ends_in(l, (char_u *)"};", NULL)) { + if (cin_ends_in(l, "};", NULL)) { break; } @@ -3666,17 +3493,15 @@ term_again: // array constant: // something = [ // 234, <- extra indent - if (cin_ends_in(l, (char_u *)"[", NULL)) { + if (cin_ends_in(l, "[", NULL)) { amount = get_indent() + ind_continuation; break; } - /* - * Find a line only has a semicolon that belongs to a previous - * line ending in '}', e.g. before an #endif. Don't increase - * indent then. - */ - if (*(look = (char_u *)skipwhite((char *)l)) == ';' && cin_nocode(look + 1)) { + // Find a line only has a semicolon that belongs to a previous + // line ending in '}', e.g. before an #endif. Don't increase + // indent then. + if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) { pos_T curpos_save = curwin->w_cursor; while (curwin->w_cursor.lnum > 1) { @@ -3687,46 +3512,40 @@ term_again: } } if (curwin->w_cursor.lnum > 0 - && cin_ends_in(look, (char_u *)"}", NULL)) { + && cin_ends_in(look, "}", NULL)) { break; } curwin->w_cursor = curpos_save; } - /* - * If the PREVIOUS line is a function declaration, the current - * line (and the ones that follow) needs to be indented as - * parameters. - */ + // If the PREVIOUS line is a function declaration, the current + // line (and the ones that follow) needs to be indented as + // parameters. if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) { amount = curbuf->b_ind_param; break; } - /* - * If the previous line ends in ';' and the line before the - * previous line ends in ',' or '\', ident to column zero: - * int foo, - * bar; - * indent_to_0 here; - */ - if (cin_ends_in(l, (char_u *)";", NULL)) { + // If the previous line ends in ';' and the line before the + // previous line ends in ',' or '\', ident to column zero: + // int foo, + // bar; + // indent_to_0 here; + if (cin_ends_in(l, ";", NULL)) { l = ml_get(curwin->w_cursor.lnum - 1); - if (cin_ends_in(l, (char_u *)",", NULL) - || (*l != NUL && l[STRLEN(l) - 1] == '\\')) { + if (cin_ends_in(l, ",", NULL) + || (*l != NUL && l[strlen(l) - 1] == '\\')) { break; } l = get_cursor_line_ptr(); } - /* - * Doesn't look like anything interesting -- so just - * use the indent of this line. - * - * Position the cursor over the rightmost paren, so that - * matching it will take us back to the start of the line. - */ + // Doesn't look like anything interesting -- so just + // use the indent of this line. + // + // Position the cursor over the rightmost paren, so that + // matching it will take us back to the start of the line. (void)find_last_paren(l, '(', ')'); if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) { @@ -3748,7 +3567,7 @@ term_again: // here"; if (cur_curpos.lnum > 1) { l = ml_get(cur_curpos.lnum - 1); - if (*l != NUL && l[STRLEN(l) - 1] == '\\') { + if (*l != NUL && l[strlen(l) - 1] == '\\') { cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); if (cur_amount > 0) { amount = cur_amount; @@ -3774,9 +3593,9 @@ laterend: static int find_match(int lookfor, linenr_T ourscope) { - const char_u *look; + const char *look; pos_T *theirscope; - const char_u *mightbeif; + const char *mightbeif; int elselevel; int whilelevel; @@ -3802,38 +3621,30 @@ static int find_match(int lookfor, linenr_T ourscope) continue; } - /* - * if we've gone outside the braces entirely, - * we must be out of scope... - */ + // if we've gone outside the braces entirely, + // we must be out of scope... theirscope = find_start_brace(); // XXX if (theirscope == NULL) { break; } - /* - * and if the brace enclosing this is further - * back than the one enclosing the else, we're - * out of luck too. - */ + // and if the brace enclosing this is further + // back than the one enclosing the else, we're + // out of luck too. if (theirscope->lnum < ourscope) { break; } - /* - * and if they're enclosed in a *deeper* brace, - * then we can ignore it because it's in a - * different scope... - */ + // and if they're enclosed in a *deeper* brace, + // then we can ignore it because it's in a + // different scope... if (theirscope->lnum > ourscope) { continue; } - /* - * if it was an "else" (that's not an "else if") - * then we need to go back to another if, so - * increment elselevel - */ + // if it was an "else" (that's not an "else if") + // then we need to go back to another if, so + // increment elselevel look = cin_skipcomment(get_cursor_line_ptr()); if (cin_iselse(look)) { mightbeif = cin_skipcomment(look + 4); @@ -3843,10 +3654,8 @@ static int find_match(int lookfor, linenr_T ourscope) continue; } - /* - * if it was a "while" then we need to go back to - * another "do", so increment whilelevel. XXX - */ + // if it was a "while" then we need to go back to + // another "do", so increment whilelevel. XXX if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) { whilelevel++; continue; @@ -3855,11 +3664,9 @@ static int find_match(int lookfor, linenr_T ourscope) // If it's an "if" decrement elselevel look = cin_skipcomment(get_cursor_line_ptr()); if (cin_isif(look)) { - elselevel--; - /* - * When looking for an "if" ignore "while"s that - * get in the way. - */ + elselevel--; // NOLINT(readability/braces) + // When looking for an "if" ignore "while"s that + // get in the way. if (elselevel == 0 && lookfor == LOOKFOR_IF) { whilelevel = 0; } @@ -3870,11 +3677,9 @@ static int find_match(int lookfor, linenr_T ourscope) whilelevel--; } - /* - * if we've used up all the elses, then - * this must be the if that we want! - * match the indent level of that if. - */ + // if we've used up all the elses, then + // this must be the if that we want! + // match the indent level of that if. if (elselevel <= 0 && whilelevel <= 0) { return OK; } @@ -3882,9 +3687,7 @@ static int find_match(int lookfor, linenr_T ourscope) return FAIL; } -/* - * Do C or expression indenting on the current line. - */ +// Do C or expression indenting on the current line. void do_c_expr_indent(void) { if (*curbuf->b_p_inde != NUL) { |