diff options
-rw-r--r-- | Filelist | 2 | ||||
-rw-r--r-- | src/Make_cyg_ming.mak | 1 | ||||
-rw-r--r-- | src/Make_morph.mak | 1 | ||||
-rw-r--r-- | src/Make_mvc.mak | 4 | ||||
-rw-r--r-- | src/Make_vms.mms | 6 | ||||
-rw-r--r-- | src/Makefile | 10 | ||||
-rw-r--r-- | src/README.md | 3 | ||||
-rw-r--r-- | src/change.c | 145 | ||||
-rw-r--r-- | src/cindent.c | 4133 | ||||
-rw-r--r-- | src/edit.c | 341 | ||||
-rw-r--r-- | src/evalfunc.c | 64 | ||||
-rw-r--r-- | src/ex_cmds.c | 215 | ||||
-rw-r--r-- | src/globals.h | 2 | ||||
-rw-r--r-- | src/indent.c | 5408 | ||||
-rw-r--r-- | src/misc1.c | 556 | ||||
-rw-r--r-- | src/ops.c | 107 | ||||
-rw-r--r-- | src/proto.h | 1 | ||||
-rw-r--r-- | src/proto/cindent.pro | 10 | ||||
-rw-r--r-- | src/proto/edit.pro | 2 | ||||
-rw-r--r-- | src/proto/ex_cmds.pro | 1 | ||||
-rw-r--r-- | src/proto/indent.pro | 33 | ||||
-rw-r--r-- | src/proto/misc1.pro | 9 | ||||
-rw-r--r-- | src/proto/ops.pro | 1 | ||||
-rw-r--r-- | src/userfunc.c | 2 | ||||
-rw-r--r-- | src/version.c | 2 |
25 files changed, 5517 insertions, 5542 deletions
@@ -25,6 +25,7 @@ SRC_ALL = \ src/change.c \ src/channel.c \ src/charset.c \ + src/cindent.c \ src/cmdexpand.c \ src/cmdhist.c \ src/crypt.c \ @@ -191,6 +192,7 @@ SRC_ALL = \ src/proto/change.pro \ src/proto/channel.pro \ src/proto/charset.pro \ + src/proto/cindent.pro \ src/proto/cmdexpand.pro \ src/proto/cmdhist.pro \ src/proto/crypt.pro \ diff --git a/src/Make_cyg_ming.mak b/src/Make_cyg_ming.mak index 503761c54d..f04d11b188 100644 --- a/src/Make_cyg_ming.mak +++ b/src/Make_cyg_ming.mak @@ -712,6 +712,7 @@ OBJ = \ $(OUTDIR)/bufwrite.o \ $(OUTDIR)/change.o \ $(OUTDIR)/charset.o \ + $(OUTDIR)/cindent.o \ $(OUTDIR)/cmdexpand.o \ $(OUTDIR)/cmdhist.o \ $(OUTDIR)/crypt.o \ diff --git a/src/Make_morph.mak b/src/Make_morph.mak index 247a5788fe..bb5805c4d2 100644 --- a/src/Make_morph.mak +++ b/src/Make_morph.mak @@ -32,6 +32,7 @@ SRC = arabic.c \ bufwrite.c \ change.c \ charset.c \ + cindent.c \ cmdexpand.c \ cmdhist.c \ crypt.c \ diff --git a/src/Make_mvc.mak b/src/Make_mvc.mak index e853ae9b17..4fb045aff7 100644 --- a/src/Make_mvc.mak +++ b/src/Make_mvc.mak @@ -719,6 +719,7 @@ OBJ = \ $(OUTDIR)\bufwrite.obj \ $(OUTDIR)\change.obj \ $(OUTDIR)\charset.obj \ + $(OUTDIR)\cindent.obj \ $(OUTDIR)\cmdexpand.obj \ $(OUTDIR)\cmdhist.obj \ $(OUTDIR)\crypt.obj \ @@ -1464,6 +1465,8 @@ $(OUTDIR)/change.obj: $(OUTDIR) change.c $(INCL) $(OUTDIR)/charset.obj: $(OUTDIR) charset.c $(INCL) +$(OUTDIR)/cindent.obj: $(OUTDIR) cindent.c $(INCL) + $(OUTDIR)/cmdexpand.obj: $(OUTDIR) cmdexpand.c $(INCL) $(OUTDIR)/cmdhist.obj: $(OUTDIR) cmdhist.c $(INCL) @@ -1794,6 +1797,7 @@ proto.h: \ proto/bufwrite.pro \ proto/change.pro \ proto/charset.pro \ + proto/cindent.pro \ proto/cmdexpand.pro \ proto/cmdhist.pro \ proto/crypt.pro \ diff --git a/src/Make_vms.mms b/src/Make_vms.mms index 6046b5a235..3decf69116 100644 --- a/src/Make_vms.mms +++ b/src/Make_vms.mms @@ -318,6 +318,7 @@ SRC = \ bufwrite.c \ change.c \ charset.c \ + cindent.c \ cmdexpand.c \ cmdhist.c \ crypt.c \ @@ -420,6 +421,7 @@ OBJ = \ bufwrite.obj \ change.obj \ charset.obj \ + cindent.obj \ cmdexpand.obj \ cmdhist.obj \ crypt.obj \ @@ -700,6 +702,10 @@ charset.obj : charset.c vim.h [.auto]config.h feature.h os_unix.h \ ascii.h keymap.h term.h macros.h structs.h regexp.h \ gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \ globals.h +cindent.obj : cindent.c vim.h [.auto]config.h feature.h os_unix.h \ + ascii.h keymap.h term.h macros.h structs.h regexp.h \ + gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \ + globals.h cmdexpand.obj : cmdexpand.c vim.h [.auto]config.h feature.h os_unix.h \ ascii.h keymap.h term.h macros.h structs.h regexp.h \ gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \ diff --git a/src/Makefile b/src/Makefile index cae0e5ff9e..f169548556 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1585,6 +1585,7 @@ BASIC_SRC = \ buffer.c \ change.c \ charset.c \ + cindent.c \ cmdexpand.c \ cmdhist.c \ crypt.c \ @@ -1725,6 +1726,7 @@ OBJ_COMMON = \ objects/change.o \ objects/blob.o \ objects/blowfish.o \ + objects/cindent.o \ objects/cmdexpand.o \ objects/cmdhist.o \ objects/crypt.o \ @@ -1878,6 +1880,7 @@ PRO_AUTO = \ buffer.pro \ change.pro \ charset.pro \ + cindent.pro \ cmdexpand.pro \ cmdhist.pro \ crypt.pro \ @@ -3081,6 +3084,9 @@ objects/change.o: change.c objects/charset.o: charset.c $(CCC) -o $@ charset.c +objects/cindent.o: cindent.c + $(CCC) -o $@ cindent.c + objects/cmdexpand.o: cmdexpand.c $(CCC) -o $@ cmdexpand.c @@ -3621,6 +3627,10 @@ objects/charset.o: charset.c vim.h protodef.h auto/config.h feature.h os_unix.h auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \ proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \ proto.h globals.h +objects/cindent.o: cindent.c vim.h protodef.h auto/config.h feature.h os_unix.h \ + auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \ + proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \ + proto.h globals.h objects/cmdexpand.o: cmdexpand.c vim.h protodef.h auto/config.h feature.h os_unix.h \ auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \ proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \ diff --git a/src/README.md b/src/README.md index 8d20140851..5d17308425 100644 --- a/src/README.md +++ b/src/README.md @@ -29,6 +29,7 @@ blob.c | blob data type buffer.c | manipulating buffers (loaded files) bufwrite.c | writing a buffer to file change.c | handling changes to text +cindent.c | C and Lisp indentation cmdexpand.c | command-line completion cmdhist.c | command-line history debugger.c | vim script debugger @@ -46,7 +47,7 @@ findfile.c | search for files in 'path' fold.c | folding getchar.c | getting characters and key mapping highlight.c | syntax highlighting -indent.c | C and Lisp indentation +indent.c | text indentation insexpand.c | Insert mode completion mark.c | marks map.c | mapping and abbreviations diff --git a/src/change.c b/src/change.c index 0eb00c2836..caf9db0d71 100644 --- a/src/change.c +++ b/src/change.c @@ -1251,151 +1251,6 @@ del_bytes( } /* - * Copy the indent from ptr to the current line (and fill to size) - * Leaves the cursor on the first non-blank in the line. - * Returns TRUE if the line was changed. - */ - static int -copy_indent(int size, char_u *src) -{ - char_u *p = NULL; - char_u *line = NULL; - char_u *s; - int todo; - int ind_len; - int line_len = 0; - int tab_pad; - int ind_done; - int round; -#ifdef FEAT_VARTABS - int ind_col; -#endif - - // Round 1: compute the number of characters needed for the indent - // Round 2: copy the characters. - for (round = 1; round <= 2; ++round) - { - todo = size; - ind_len = 0; - ind_done = 0; -#ifdef FEAT_VARTABS - ind_col = 0; -#endif - s = src; - - // Count/copy the usable portion of the source line - while (todo > 0 && VIM_ISWHITE(*s)) - { - if (*s == TAB) - { -#ifdef FEAT_VARTABS - tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, - curbuf->b_p_vts_array); -#else - tab_pad = (int)curbuf->b_p_ts - - (ind_done % (int)curbuf->b_p_ts); -#endif - // Stop if this tab will overshoot the target - if (todo < tab_pad) - break; - todo -= tab_pad; - ind_done += tab_pad; -#ifdef FEAT_VARTABS - ind_col += tab_pad; -#endif - } - else - { - --todo; - ++ind_done; -#ifdef FEAT_VARTABS - ++ind_col; -#endif - } - ++ind_len; - if (p != NULL) - *p++ = *s; - ++s; - } - - // Fill to next tabstop with a tab, if possible -#ifdef FEAT_VARTABS - tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, - curbuf->b_p_vts_array); -#else - tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); -#endif - if (todo >= tab_pad && !curbuf->b_p_et) - { - todo -= tab_pad; - ++ind_len; -#ifdef FEAT_VARTABS - ind_col += tab_pad; -#endif - if (p != NULL) - *p++ = TAB; - } - - // Add tabs required for indent - if (!curbuf->b_p_et) - { -#ifdef FEAT_VARTABS - for (;;) - { - tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, - curbuf->b_p_vts_array); - if (todo < tab_pad) - break; - todo -= tab_pad; - ++ind_len; - ind_col += tab_pad; - if (p != NULL) - *p++ = TAB; - } -#else - while (todo >= (int)curbuf->b_p_ts) - { - todo -= (int)curbuf->b_p_ts; - ++ind_len; - if (p != NULL) - *p++ = TAB; - } -#endif - } - - // Count/add spaces required for indent - while (todo > 0) - { - --todo; - ++ind_len; - if (p != NULL) - *p++ = ' '; - } - - if (p == NULL) - { - // Allocate memory for the result: the copied indent, new indent - // and the rest of the line. - line_len = (int)STRLEN(ml_get_curline()) + 1; - line = alloc(ind_len + line_len); - if (line == NULL) - return FALSE; - p = line; - } - } - - // Append the original line - mch_memmove(p, ml_get_curline(), (size_t)line_len); - - // Replace the line - ml_replace(curwin->w_cursor.lnum, line, FALSE); - - // Put the cursor after the indent. - curwin->w_cursor.col = ind_len; - return TRUE; -} - -/* * open_line: Add a new line below or above the current line. * * For VREPLACE mode, we only add a new line when we get to the end of the diff --git a/src/cindent.c b/src/cindent.c new file mode 100644 index 0000000000..dd416720a9 --- /dev/null +++ b/src/cindent.c @@ -0,0 +1,4133 @@ +/* vi:set ts=8 sts=4 sw=4 noet: + * + * VIM - Vi IMproved by Bram Moolenaar + * + * Do ":help uganda" in Vim to read copying and usage conditions. + * Do ":help credits" in Vim to see a list of people who contributed. + * See README.txt for an overview of the Vim source code. + */ + +/* + * cindent.c: C indentation related functions + * + * Many of C-indenting functions originally come from Eric Fischer. + * + * Below "XXX" means that this function may unlock the current line. + */ + +#include "vim.h" + +// values for the "lookfor" state +#define LOOKFOR_INITIAL 0 +#define LOOKFOR_IF 1 +#define LOOKFOR_DO 2 +#define LOOKFOR_CASE 3 +#define LOOKFOR_ANY 4 +#define LOOKFOR_TERM 5 +#define LOOKFOR_UNTERM 6 +#define LOOKFOR_SCOPEDECL 7 +#define LOOKFOR_NOBREAK 8 +#define LOOKFOR_CPP_BASECLASS 9 +#define LOOKFOR_ENUM_OR_INIT 10 +#define LOOKFOR_JS_KEY 11 +#define LOOKFOR_COMMA 12 + +#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) +/* + * Return TRUE if the string "line" starts with a word from 'cinwords'. + */ + int +cin_is_cinword(char_u *line) +{ + char_u *cinw; + char_u *cinw_buf; + int cinw_len; + int retval = FALSE; + int len; + + cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1; + cinw_buf = alloc(cinw_len); + if (cinw_buf != NULL) + { + line = skipwhite(line); + for (cinw = curbuf->b_p_cinw; *cinw; ) + { + len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); + if (STRNCMP(line, cinw_buf, len) == 0 + && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) + { + retval = TRUE; + break; + } + } + vim_free(cinw_buf); + } + return retval; +} +#endif + +#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) + +/* + * Skip to the end of a "string" and a 'c' character. + * If there is no string or character, return argument unmodified. + */ + static char_u * +skip_string(char_u *p) +{ + int i; + + // We loop, because strings may be concatenated: "date""time". + for ( ; ; ++p) + { + if (p[0] == '\'') // 'c' or '\n' or '\000' + { + if (!p[1]) // ' at end of line + break; + i = 2; + if (p[1] == '\\') // '\n' or '\000' + { + ++i; + while (vim_isdigit(p[i - 1])) // '\000' + ++i; + } + if (p[i] == '\'') // check for trailing ' + { + p += i; + continue; + } + } + else if (p[0] == '"') // start of string + { + for (++p; p[0]; ++p) + { + if (p[0] == '\\' && p[1] != NUL) + ++p; + else if (p[0] == '"') // end of string + break; + } + if (p[0] == '"') + continue; // continue for another string + } + else if (p[0] == 'R' && p[1] == '"') + { + // Raw string: R"[delim](...)[delim]" + char_u *delim = p + 2; + char_u *paren = vim_strchr(delim, '('); + + if (paren != NULL) + { + size_t delim_len = paren - delim; + + for (p += 3; *p; ++p) + if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 + && p[delim_len + 1] == '"') + { + p += delim_len + 1; + break; + } + if (p[0] == '"') + continue; // continue for another string + } + } + break; // no string found + } + if (!*p) + --p; // backup from NUL + return p; +} + +/* + * 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); +} + + pos_T * +find_start_comment(int ind_maxcomment) // XXX +{ + pos_T *pos; + char_u *line; + char_u *p; + int cur_maxcomment = ind_maxcomment; + + for (;;) + { + pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); + if (pos == NULL) + 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. + line = ml_get(pos->lnum); + for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) + p = skip_string(p); + if ((colnr_T)(p - line) <= pos->col) + break; + cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; + if (cur_maxcomment <= 0) + { + pos = NULL; + break; + } + } + return 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. + */ + static pos_T * +find_start_rawstring(int ind_maxcomment) // XXX +{ + pos_T *pos; + char_u *line; + char_u *p; + int cur_maxcomment = ind_maxcomment; + + for (;;) + { + pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment); + if (pos == NULL) + break; + + // Check if the raw string start we found is inside a string. + // If it is then restrict the search to below this line and try again. + line = ml_get(pos->lnum); + for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) + p = skip_string(p); + if ((colnr_T)(p - line) <= pos->col) + break; + cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; + if (cur_maxcomment <= 0) + { + pos = NULL; + break; + } + } + return pos; +} + +/* + * Find the start of a comment or raw string, not knowing if we are in a + * comment or raw string right now. + * Search starts at w_cursor.lnum and goes backwards. + * If is_raw is given and returns start of raw_string, sets it to true. + * Return NULL when not inside a comment or raw string. + * "CORS" -> Comment Or Raw String + */ + static pos_T * +ind_find_start_CORS(linenr_T *is_raw) // XXX +{ + static pos_T comment_pos_copy; + pos_T *comment_pos; + pos_T *rs_pos; + + comment_pos = find_start_comment(curbuf->b_ind_maxcomment); + if (comment_pos != NULL) + { + // Need to make a copy of the static pos in findmatchlimit(), + // calling find_start_rawstring() may change it. + comment_pos_copy = *comment_pos; + comment_pos = &comment_pos_copy; + } + rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment); + + // If comment_pos is before rs_pos the raw string is inside the comment. + // If rs_pos is before comment_pos the comment is inside the raw string. + if (comment_pos == NULL || (rs_pos != NULL + && LT_POS(*rs_pos, *comment_pos))) + { + if (is_raw != NULL && rs_pos != NULL) + *is_raw = rs_pos->lnum; + return rs_pos; + } + return comment_pos; +} +#endif // FEAT_CINDENT || FEAT_SYN_HL + +#if defined(FEAT_CINDENT) || defined(PROTO) + +/* + * Return TRUE if C-indenting is on. + */ + int +cindent_on(void) +{ + return (!p_paste && (curbuf->b_p_cin +# ifdef FEAT_EVAL + || *curbuf->b_p_inde != NUL +# endif + )); +} + +// Find result cache for cpp_baseclass +typedef struct { + int found; + lpos_T lpos; +} cpp_baseclass_cache_T; + +/* + * Skip over white space and C comments within the line. + * Also skip over Perl/shell comments if desired. + */ + static char_u * +cin_skipcomment(char_u *s) +{ + while (*s) + { + char_u *prev_s = s; + + s = skipwhite(s); + + // 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); + break; + } + if (*s != '/') + break; + ++s; + if (*s == '/') // slash-slash comment continues till eol + { + s += STRLEN(s); + break; + } + if (*s != '*') + break; + for (++s; *s; ++s) // skip slash-star comment + if (s[0] == '*' && s[1] == '/') + { + s += 2; + break; + } + } + return s; +} + +/* + * Return TRUE if there is no code at *s. White space and comments are + * not considered code. + */ + static int +cin_nocode(char_u *s) +{ + return *cin_skipcomment(s) == NUL; +} + +/* + * Recognize the start of a C or C++ comment. + */ + static int +cin_iscomment(char_u *p) +{ + return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); +} + +/* + * Recognize the start of a "//" comment. + */ + static int +cin_islinecomment(char_u *p) +{ + return (p[0] == '/' && p[1] == '/'); +} + +/* + * 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; + + pos = curwin->w_cursor; + while (--pos.lnum > 0) + { + line = ml_get(pos.lnum); + p = skipwhite(line); + if (cin_islinecomment(p)) + { + pos.col = (int)(p - line); + return &pos; + } + if (*p != NUL) + break; + } + return NULL; +} + +/* + * Return TRUE if "text" starts with "key:". + */ + static int +cin_has_js_key(char_u *text) +{ + char_u *s = skipwhite(text); + int quote = -1; + + if (*s == '\'' || *s == '"') + { + // can be 'key': or "key": + quote = *s; + ++s; + } + if (!vim_isIDc(*s)) // need at least one ID character + return FALSE; + + while (vim_isIDc(*s)) + ++s; + if (*s == quote) + ++s; + + s = cin_skipcomment(s); + + // "::" is not a label, it's C++ + return (*s == ':' && s[1] != ':'); +} + +/* + * Check if string matches "label:"; move to character after ':' if true. + * "*s" must point to the start of the label, if there is one. + */ + static int +cin_islabel_skip(char_u **s) +{ + if (!vim_isIDc(**s)) // need at least one ID character + return FALSE; + + while (vim_isIDc(**s)) + (*s)++; + + *s = cin_skipcomment(*s); + + // "::" is not a label, it's C++ + return (**s == ':' && *++*s != ':'); +} + +/* + * Recognize a "public/private/protected" scope declaration label. + */ + static int +cin_isscopedecl(char_u *s) +{ + int i; + + s = cin_skipcomment(s); + if (STRNCMP(s, "public", 6) == 0) + i = 6; + else if (STRNCMP(s, "protected", 9) == 0) + i = 9; + else if (STRNCMP(s, "private", 7) == 0) + i = 7; + else + return FALSE; + return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); +} + +/* + * Recognize a preprocessor statement: Any line that starts with '#'. + */ + static int +cin_ispreproc(char_u *s) +{ + if (*skipwhite(s) == '#') + return TRUE; + return FALSE; +} + +/* + * 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(char_u **pp, linenr_T *lnump, int *amount) +{ + char_u *line = *pp; + linenr_T lnum = *lnump; + int retval = FALSE; + int candidate_amount = *amount; + + if (*line != NUL && line[STRLEN(line) - 1] == '\\') + candidate_amount = get_indent_lnum(lnum); + + for (;;) + { + if (cin_ispreproc(line)) + { + retval = TRUE; + *lnump = lnum; + break; + } + if (lnum == 1) + break; + line = ml_get(--lnum); + if (*line == NUL || line[STRLEN(line) - 1] != '\\') + break; + } + + if (lnum != *lnump) + *pp = ml_get(*lnump); + if (retval) + *amount = candidate_amount; + return retval; +} + + static int +cin_iselse( + char_u *p) +{ + if (*p == '}') // accept "} else" + p = cin_skipcomment(p + 1); + return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); +} + +/* + * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or + * '}'. + * Don't consider "} else" a terminated line. + * If a line begins with an "else", only consider it terminated if no unmatched + * opening braces follow (handle "else { foo();" correctly). + * Return the character terminating the line (ending char's have precedence if + * both apply in order to determine initializations). + */ + static int +cin_isterminated( + char_u *s, + int incl_open, // include '{' at the end as terminator + int incl_comma) // recognize a trailing comma +{ + char_u found_start = 0; + unsigned n_open = 0; + int is_else = FALSE; + + s = cin_skipcomment(s); + + if (*s == '{' || (*s == '}' && !cin_iselse(s))) + found_start = *s; + + if (!found_start) + is_else = cin_iselse(s); + + while (*s) + { + // skip over comments, "" strings and 'c'haracters + s = skip_string(cin_skipcomment(s)); + if (*s == '}' && n_open > 0) + --n_open; + if ((!is_else || n_open == 0) + && (*s == ';' || *s == '}' || (incl_comma && *s == ',')) + && cin_nocode(s + 1)) + return *s; + else if (*s == '{') + { + if (incl_open && cin_nocode(s + 1)) + return *s; + else + ++n_open; + } + + if (*s) + s++; + } + return found_start; +} + +/* + * Return TRUE when "s" starts with "word" and then a non-ID character. + */ + static int +cin_starts_with(char_u *s, char *word) +{ + int l = (int)STRLEN(word); + + return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l])); +} + +/* + * Recognize a "default" switch label. + */ + static int +cin_isdefault(char_u *s) +{ + return (STRNCMP(s, "default", 7) == 0 + && *(s = cin_skipcomment(s + 7)) == ':' + && s[1] != ':'); +} + +/* + * Recognize a switch label: "case .*:" or "default:". + */ + static int +cin_iscase( + char_u *s, + int strict) // Allow relaxed check of case statement for JS +{ + s = cin_skipcomment(s); + if (cin_starts_with(s, "case")) + { + for (s += 4; *s; ++s) + { + s = cin_skipcomment(s); + if (*s == ':') + { + if (s[1] == ':') // skip over "::" for C++ + ++s; + else + return TRUE; + } + if (*s == '\'' && s[1] && s[2] == '\'') + s += 2; // skip over ':' + else if (*s == '/' && (s[1] == '*' || s[1] == '/')) + return FALSE; // stop at comment + else if (*s == '"') + { + // JS etc. + if (strict) + return FALSE; // stop at string + else + return TRUE; + } + } + return FALSE; + } + + if (cin_isdefault(s)) + return TRUE; + return FALSE; +} + +/* + * Recognize a label: "label:". + * Note: curwin->w_cursor must be where we are looking for the label. + */ + static int +cin_islabel(void) // XXX +{ + char_u *s; + + s = cin_skipcomment(ml_get_curline()); + + // Exclude "default" from labels, since it should be indented + // like a switch label. Same for C++ scope declarations. + if (cin_isdefault(s)) + return FALSE; + if (cin_isscopedecl(s)) + return FALSE; + + if (cin_islabel_skip(&s)) + { + // Only accept a label if the previous line is terminated or is a case + // label. + pos_T cursor_save; + pos_T *trypos; + char_u *line; + + cursor_save = curwin->w_cursor; + while (curwin->w_cursor.lnum > 1) + { + --curwin->w_cursor.lnum; + + // 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; + + line = ml_get_curline(); + if (cin_ispreproc(line)) // ignore #defines, #if, etc. + continue; + if (*(line = cin_skipcomment(line)) == NUL) + continue; + + curwin->w_cursor = cursor_save; + if (cin_isterminated(line, TRUE, FALSE) + || cin_isscopedecl(line) + || cin_iscase(line, TRUE) + || (cin_islabel_skip(&line) && cin_nocode(line))) + return TRUE; + return FALSE; + } + curwin->w_cursor = cursor_save; + return TRUE; // label at start of file??? + } + return FALSE; +} + +/* + * 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(char_u *s, char_u *find, char_u *ignore) +{ + char_u *p = s; + char_u *r; + int len = (int)STRLEN(find); + + while (*p != NUL) + { + p = cin_skipcomment(p); + if (STRNCMP(p, find, 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; + } + if (*p != NUL) + ++p; + } + return FALSE; +} + +/* + * Recognize structure initialization and enumerations: + * "[typedef] [static|public|protected|private] enum" + * "[typedef] [static|public|protected|private] = {" + */ + static int +cin_isinit(void) +{ + char_u *s; + static char *skip[] = {"static", "public", "protected", "private"}; + + s = cin_skipcomment(ml_get_curline()); + + if (cin_starts_with(s, "typedef")) + s = cin_skipcomment(s + 7); + + for (;;) + { + int i, l; + + for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i) + { + l = (int)strlen(skip[i]); + if (cin_starts_with(s, skip[i])) + { + s = cin_skipcomment(s + l); + l = 0; + break; + } + } + if (l != 0) + break; + } + + if (cin_starts_with(s, "enum")) + return TRUE; + + if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) + return TRUE; + + return FALSE; +} + +// Maximum number of lines to search back for a "namespace" line. +#define FIND_NAMESPACE |