From 4b47162ccede0b6d9cbb9473ad870220a24fbf54 Mon Sep 17 00:00:00 2001 From: Bram Moolenaar Date: Thu, 31 Jan 2019 13:48:09 +0100 Subject: patch 8.1.0857: indent functionality is not separated Problem: Ignore functionality is not separated. Solution: Move indent functionality into a new file. (Yegappan Lakshmanan, closes #3886) --- src/Make_bc5.mak | 1 + src/Make_cyg_ming.mak | 1 + src/Make_dice.mak | 4 + src/Make_ivc.mak | 5 + src/Make_manx.mak | 6 + src/Make_morph.mak | 1 + src/Make_mvc.mak | 4 + src/Make_sas.mak | 5 + src/Make_vms.mms | 5 +- src/Makefile | 10 + src/edit.c | 331 +--- src/indent.c | 4683 +++++++++++++++++++++++++++++++++++++++++++++++++ src/misc1.c | 4346 --------------------------------------------- src/proto.h | 1 + src/proto/edit.pro | 3 - src/proto/indent.pro | 16 + src/proto/misc1.pro | 9 - src/version.c | 2 + 18 files changed, 4744 insertions(+), 4689 deletions(-) create mode 100644 src/indent.c create mode 100644 src/proto/indent.pro (limited to 'src') diff --git a/src/Make_bc5.mak b/src/Make_bc5.mak index d0d31d4eb9..b2977e7007 100644 --- a/src/Make_bc5.mak +++ b/src/Make_bc5.mak @@ -548,6 +548,7 @@ vimobj = \ $(OBJDIR)\getchar.obj \ $(OBJDIR)\hardcopy.obj \ $(OBJDIR)\hashtab.obj \ + $(OBJDIR)\indent.obj \ $(OBJDIR)\json.obj \ $(OBJDIR)\list.obj \ $(OBJDIR)\main.obj \ diff --git a/src/Make_cyg_ming.mak b/src/Make_cyg_ming.mak index 8d8666ffbe..5795b50752 100644 --- a/src/Make_cyg_ming.mak +++ b/src/Make_cyg_ming.mak @@ -720,6 +720,7 @@ OBJ = \ $(OUTDIR)/getchar.o \ $(OUTDIR)/hardcopy.o \ $(OUTDIR)/hashtab.o \ + $(OUTDIR)/indent.o \ $(OUTDIR)/json.o \ $(OUTDIR)/list.o \ $(OUTDIR)/main.o \ diff --git a/src/Make_dice.mak b/src/Make_dice.mak index 83614ca911..57aed477b9 100644 --- a/src/Make_dice.mak +++ b/src/Make_dice.mak @@ -50,6 +50,7 @@ SRC = \ getchar.c \ hardcopy.c \ hashtab.c \ + indent.c \ json.c \ list.c \ main.c \ @@ -108,6 +109,7 @@ OBJ = o/arabic.o \ o/getchar.o \ o/hardcopy.o \ o/hashtab.o \ + o/indent.o \ o/json.o \ o/list.o \ o/main.o \ @@ -209,6 +211,8 @@ o/hardcopy.o: hardcopy.c $(SYMS) o/hashtab.o: hashtab.c $(SYMS) +o/indent.o: indent.c $(SYMS) + o/json.o: json.c $(SYMS) o/list.o: list.c $(SYMS) diff --git a/src/Make_ivc.mak b/src/Make_ivc.mak index 0459984dab..96d6a47b54 100644 --- a/src/Make_ivc.mak +++ b/src/Make_ivc.mak @@ -234,6 +234,7 @@ LINK32_OBJS= \ "$(INTDIR)/getchar.obj" \ "$(INTDIR)/hardcopy.obj" \ "$(INTDIR)/hashtab.obj" \ + "$(INTDIR)/indent.obj" \ "$(INTDIR)/json.obj" \ "$(INTDIR)/list.obj" \ "$(INTDIR)/main.obj" \ @@ -434,6 +435,10 @@ SOURCE=.\hardcopy.c SOURCE=.\hashtab.c # End Source File # Begin Source File +# +SOURCE=.\indent.c +# End Source File +# Begin Source File SOURCE=.\gui.c diff --git a/src/Make_manx.mak b/src/Make_manx.mak index b71b923ded..a44ad6558c 100644 --- a/src/Make_manx.mak +++ b/src/Make_manx.mak @@ -60,6 +60,7 @@ SRC = arabic.c \ getchar.c \ hardcopy.c \ hashtab.c \ + indent.c \ json.c \ list.c \ main.c \ @@ -120,6 +121,7 @@ OBJ = obj/arabic.o \ obj/getchar.o \ obj/hardcopy.o \ obj/hashtab.o \ + obj/indent.o \ obj/json.o \ obj/list.o \ obj/main.o \ @@ -178,6 +180,7 @@ PRO = proto/arabic.pro \ proto/getchar.pro \ proto/hardcopy.pro \ proto/hashtab.pro \ + proto/indent.pro \ proto/json.pro \ proto/list.pro \ proto/main.pro \ @@ -329,6 +332,9 @@ obj/hardcopy.o: hardcopy.c obj/hashtab.o: hashtab.c $(CCSYM) $@ hashtab.c +obj/indent.o: indent.c + $(CCSYM) $@ indent.c + obj/json.o: json.c $(CCSYM) $@ json.c diff --git a/src/Make_morph.mak b/src/Make_morph.mak index ae490416f4..6bcae1a0b1 100644 --- a/src/Make_morph.mak +++ b/src/Make_morph.mak @@ -48,6 +48,7 @@ SRC = arabic.c \ getchar.c \ hardcopy.c \ hashtab.c \ + indent.c \ json.c \ list.c \ main.c \ diff --git a/src/Make_mvc.mak b/src/Make_mvc.mak index 3f8c8a0384..52deb549c3 100644 --- a/src/Make_mvc.mak +++ b/src/Make_mvc.mak @@ -725,6 +725,7 @@ OBJ = \ $(OUTDIR)\getchar.obj \ $(OUTDIR)\hardcopy.obj \ $(OUTDIR)\hashtab.obj \ + $(OUTDIR)\indent.obj \ $(OUTDIR)\json.obj \ $(OUTDIR)\list.obj \ $(OUTDIR)\main.obj \ @@ -1414,6 +1415,8 @@ $(OUTDIR)/hardcopy.obj: $(OUTDIR) hardcopy.c $(INCL) $(OUTDIR)/hashtab.obj: $(OUTDIR) hashtab.c $(INCL) +$(OUTDIR)/indent.obj: $(OUTDIR) indent.c $(INCL) + $(OUTDIR)/gui.obj: $(OUTDIR) gui.c $(INCL) $(GUI_INCL) $(OUTDIR)/gui_beval.obj: $(OUTDIR) gui_beval.c $(INCL) $(GUI_INCL) @@ -1645,6 +1648,7 @@ proto.h: \ proto/getchar.pro \ proto/hardcopy.pro \ proto/hashtab.pro \ + proto/indent.pro \ proto/json.pro \ proto/list.pro \ proto/main.pro \ diff --git a/src/Make_sas.mak b/src/Make_sas.mak index c621360eab..e7faf56771 100644 --- a/src/Make_sas.mak +++ b/src/Make_sas.mak @@ -113,6 +113,7 @@ SRC = \ getchar.c \ hardcopy.c \ hashtab.c \ + indent.c \ json.c \ list.c \ main.c \ @@ -172,6 +173,7 @@ OBJ = \ getchar.o \ hardcopy.o \ hashtab.o \ + indent.o \ json.o \ list.o \ main.o \ @@ -231,6 +233,7 @@ PRO = \ proto/getchar.pro \ proto/hardcopy.pro \ proto/hashtab.pro \ + proto/indent.pro \ proto/json.pro \ proto/list.pro \ proto/main.pro \ @@ -368,6 +371,8 @@ hardcopy.o: hardcopy.c proto/hardcopy.pro: hardcopy.c hashtab.o: hashtab.c proto/hashtab.pro: hashtab.c +indent.o: indent.c +proto/indent.pro: indent.c json.o: json.c proto/json.pro: json.c list.o: list.c diff --git a/src/Make_vms.mms b/src/Make_vms.mms index 6a3508980f..1a21d15fb9 100644 --- a/src/Make_vms.mms +++ b/src/Make_vms.mms @@ -314,7 +314,7 @@ ALL_LIBS = $(LIBS) $(GUI_LIB_DIR) $(GUI_LIB) \ SRC = arabic.c autocmd.c beval.c blob.c blowfish.c buffer.c charset.c crypt.c crypt_zip.c dict.c diff.c digraph.c edit.c eval.c \ evalfunc.c ex_cmds.c ex_cmds2.c ex_docmd.c ex_eval.c ex_getln.c if_cscope.c if_xcmdsrv.c farsi.c fileio.c fold.c \ - getchar.c hardcopy.c hashtab.c json.c list.c main.c mark.c menu.c mbyte.c memfile.c memline.c message.c misc1.c \ + getchar.c hardcopy.c hashtab.c indent.c json.c list.c main.c mark.c menu.c mbyte.c memfile.c memline.c message.c misc1.c \ misc2.c move.c normal.c ops.c option.c popupmnu.c quickfix.c regexp.c search.c sha256.c sign.c \ spell.c spellfile.c syntax.c tag.c term.c termlib.c textprop.c ui.c undo.c userfunc.c version.c screen.c \ window.c os_unix.c os_vms.c pathdef.c \ @@ -323,7 +323,7 @@ SRC = arabic.c autocmd.c beval.c blob.c blowfish.c buffer.c charset.c crypt.c cr OBJ = arabic.obj autocmd.obj beval.obj blob.obj blowfish.obj buffer.obj charset.obj crypt.obj crypt_zip.obj dict.obj diff.obj digraph.obj \ edit.obj eval.obj evalfunc.obj ex_cmds.obj ex_cmds2.obj ex_docmd.obj ex_eval.obj ex_getln.obj if_cscope.obj \ - if_xcmdsrv.obj farsi.obj fileio.obj fold.obj getchar.obj hardcopy.obj hashtab.obj json.obj list.obj main.obj mark.obj \ + if_xcmdsrv.obj farsi.obj fileio.obj fold.obj getchar.obj hardcopy.obj hashtab.obj indent.obj json.obj list.obj main.obj mark.obj \ menu.obj memfile.obj memline.obj message.obj misc1.obj misc2.obj \ move.obj mbyte.obj normal.obj ops.obj option.obj popupmnu.obj quickfix.obj \ regexp.obj search.obj sha256.obj sign.obj spell.obj spellfile.obj syntax.obj tag.obj term.obj termlib.obj textprop.obj \ @@ -596,6 +596,7 @@ if_mzsch.obj : if_mzsch.c vim.h [.auto]config.h feature.h os_unix.h \ ascii.h keymap.h term.h macros.h option.h structs.h \ regexp.h gui.h beval.h [.proto]gui_beval.pro ex_cmds.h proto.h \ globals.h farsi.h arabic.h if_mzsch.h +indent.obj : indent.c vim.h [.auto]config.h feature.h os_unix.h json.obj : json.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 farsi.h \ diff --git a/src/Makefile b/src/Makefile index 3f54184564..12229a4476 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1599,6 +1599,7 @@ BASIC_SRC = \ hashtab.c \ if_cscope.c \ if_xcmdsrv.c \ + indent.c \ json.c \ list.c \ main.c \ @@ -1712,6 +1713,7 @@ OBJ_COMMON = \ $(HANGULIN_OBJ) \ objects/if_cscope.o \ objects/if_xcmdsrv.o \ + objects/indent.o \ objects/list.o \ objects/mark.o \ objects/memline.o \ @@ -1842,6 +1844,7 @@ PRO_AUTO = \ if_python3.pro \ if_ruby.pro \ if_xcmdsrv.pro \ + indent.pro \ json.pro \ list.pro \ main.pro \ @@ -3093,6 +3096,9 @@ objects/if_ruby.o: if_ruby.c objects/if_tcl.o: if_tcl.c $(CCC_NF) $(TCL_CFLAGS) $(ALL_CFLAGS) -o $@ if_tcl.c +objects/indent.o: indent.c + $(CCC) -o $@ indent.c + objects/json.o: json.c $(CCC) -o $@ json.c @@ -3490,6 +3496,10 @@ objects/if_xcmdsrv.o: if_xcmdsrv.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 farsi.h arabic.h version.h +objects/indent.o: indent.c vim.h protodef.h auto/config.h feature.h os_unix.h \ + os_mac.h ascii.h keymap.h term.h macros.h option.h beval.h structs.h \ + regexp.h gui.h alloc.h ex_cmds.h spell.h proto.h globals.h farsi.h \ + arabic.h objects/json.o: json.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/edit.c b/src/edit.c index de5adcb197..b226636aaa 100644 --- a/src/edit.c +++ b/src/edit.c @@ -216,9 +216,6 @@ static void mb_replace_pop_ins(int cc); static void replace_flush(void); static void replace_do_bs(int limit_col); static int del_char_after_col(int limit_col); -#ifdef FEAT_CINDENT -static int cindent_on(void); -#endif static void ins_reg(void); static void ins_ctrl_g(void); static void ins_ctrl_hat(void); @@ -380,6 +377,7 @@ edit( ins_compl_clear(); /* clear stuff for CTRL-X mode */ #endif +ch_log(NULL, "ENTERING Insert mode"); /* * Trigger InsertEnter autocommands. Do not do this for "r" or "grx". */ @@ -1050,6 +1048,7 @@ doESCkey: if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C) ins_apply_autocmds(EVENT_INSERTLEAVE); did_cursorhold = FALSE; +ch_log(NULL, "LEAVING Insert mode"); return (c == Ctrl_O); } continue; @@ -7923,332 +7922,6 @@ replace_do_bs(int limit_col) (void)del_char_after_col(limit_col); } -#ifdef FEAT_CINDENT -/* - * Return TRUE if C-indenting is on. - */ - static int -cindent_on(void) -{ - return (!p_paste && (curbuf->b_p_cin -# ifdef FEAT_EVAL - || *curbuf->b_p_inde != NUL -# endif - )); -} -#endif - -#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) -/* - * Re-indent the current line, based on the current contents of it and the - * surrounding lines. Fixing the cursor position seems really easy -- I'm very - * confused what all the part that handles Control-T is doing that I'm not. - * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. - */ - - void -fixthisline(int (*get_the_indent)(void)) -{ - int amount = get_the_indent(); - - if (amount >= 0) - { - change_indent(INDENT_SET, amount, FALSE, 0, TRUE); - if (linewhite(curwin->w_cursor.lnum)) - did_ai = TRUE; /* delete the indent if the line stays empty */ - } -} - - void -fix_indent(void) -{ - if (p_paste) - return; -# ifdef FEAT_LISP - if (curbuf->b_p_lisp && curbuf->b_p_ai) - fixthisline(get_lisp_indent); -# endif -# if defined(FEAT_LISP) && defined(FEAT_CINDENT) - else -# endif -# ifdef FEAT_CINDENT - if (cindent_on()) - do_c_expr_indent(); -# endif -} - -#endif - -#ifdef FEAT_CINDENT -/* - * return TRUE if 'cinkeys' contains the key "keytyped", - * when == '*': Only if key is preceded with '*' (indent before insert) - * when == '!': Only if key is preceded with '!' (don't insert) - * when == ' ': Only if key is not preceded with '*'(indent afterwards) - * - * "keytyped" can have a few special values: - * KEY_OPEN_FORW - * KEY_OPEN_BACK - * KEY_COMPLETE just finished completion. - * - * If line_is_empty is TRUE accept keys with '0' before them. - */ - int -in_cinkeys( - int keytyped, - int when, - int line_is_empty) -{ - char_u *look; - int try_match; - int try_match_word; - char_u *p; - char_u *line; - int icase; - int i; - - if (keytyped == NUL) - /* Can happen with CTRL-Y and CTRL-E on a short line. */ - return FALSE; - -#ifdef FEAT_EVAL - if (*curbuf->b_p_inde != NUL) - look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */ - else -#endif - look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */ - while (*look) - { - /* - * Find out if we want to try a match with this key, depending on - * 'when' and a '*' or '!' before the key. - */ - switch (when) - { - case '*': try_match = (*look == '*'); break; - case '!': try_match = (*look == '!'); break; - default: try_match = (*look != '*'); break; - } - if (*look == '*' || *look == '!') - ++look; - - /* - * If there is a '0', only accept a match if the line is empty. - * But may still match when typing last char of a word. - */ - if (*look == '0') - { - try_match_word = try_match; - if (!line_is_empty) - try_match = FALSE; - ++look; - } - else - try_match_word = FALSE; - - /* - * does it look like a control character? - */ - if (*look == '^' -#ifdef EBCDIC - && (Ctrl_chr(look[1]) != 0) -#else - && look[1] >= '?' && look[1] <= '_' -#endif - ) - { - if (try_match && keytyped == Ctrl_chr(look[1])) - return TRUE; - look += 2; - } - /* - * 'o' means "o" command, open forward. - * 'O' means "O" command, open backward. - */ - else if (*look == 'o') - { - if (try_match && keytyped == KEY_OPEN_FORW) - return TRUE; - ++look; - } - else if (*look == 'O') - { - if (try_match && keytyped == KEY_OPEN_BACK) - return TRUE; - ++look; - } - - /* - * 'e' means to check for "else" at start of line and just before the - * cursor. - */ - else if (*look == 'e') - { - if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) - { - p = ml_get_curline(); - if (skipwhite(p) == p + curwin->w_cursor.col - 4 && - STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) - return TRUE; - } - ++look; - } - - /* - * ':' only causes an indent if it is at the end of a label or case - * statement, or when it was before typing the ':' (to fix - * class::method for C++). - */ - else if (*look == ':') - { - if (try_match && keytyped == ':') - { - p = ml_get_curline(); - if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel()) - return TRUE; - /* Need to get the line again after cin_islabel(). */ - p = ml_get_curline(); - if (curwin->w_cursor.col > 2 - && p[curwin->w_cursor.col - 1] == ':' - && p[curwin->w_cursor.col - 2] == ':') - { - p[curwin->w_cursor.col - 1] = ' '; - i = (cin_iscase(p, FALSE) || cin_isscopedecl(p) - || cin_islabel()); - p = ml_get_curline(); - p[curwin->w_cursor.col - 1] = ':'; - if (i) - return TRUE; - } - } - ++look; - } - - - /* - * Is it a key in <>, maybe? - */ - else if (*look == '<') - { - if (try_match) - { - /* - * make up some named keys , , , <0>, <>>, <<>, <*>, - * <:> and so that people can re-indent on o, O, e, 0, <, - * >, *, : and ! keys if they really really want to. - */ - if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL - && keytyped == look[1]) - return TRUE; - - if (keytyped == get_special_key_code(look + 1)) - return TRUE; - } - while (*look && *look != '>') - look++; - while (*look == '>') - look++; - } - - /* - * Is it a word: "=word"? - */ - else if (*look == '=' && look[1] != ',' && look[1] != NUL) - { - ++look; - if (*look == '~') - { - icase = TRUE; - ++look; - } - else - icase = FALSE; - p = vim_strchr(look, ','); - if (p == NULL) - p = look + STRLEN(look); - if ((try_match || try_match_word) - && curwin->w_cursor.col >= (colnr_T)(p - look)) - { - int match = FALSE; - -#ifdef FEAT_INS_EXPAND - if (keytyped == KEY_COMPLETE) - { - char_u *s; - - /* Just completed a word, check if it starts with "look". - * search back for the start of a word. */ - line = ml_get_curline(); - if (has_mbyte) - { - char_u *n; - - for (s = line + curwin->w_cursor.col; s > line; s = n) - { - n = mb_prevptr(line, s); - if (!vim_iswordp(n)) - break; - } - } - else - for (s = line + curwin->w_cursor.col; s > line; --s) - if (!vim_iswordc(s[-1])) - break; - if (s + (p - look) <= line + curwin->w_cursor.col - && (icase - ? MB_STRNICMP(s, look, p - look) - : STRNCMP(s, look, p - look)) == 0) - match = TRUE; - } - else -#endif - /* TODO: multi-byte */ - if (keytyped == (int)p[-1] || (icase && keytyped < 256 - && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) - { - line = ml_get_cursor(); - if ((curwin->w_cursor.col == (colnr_T)(p - look) - || !vim_iswordc(line[-(p - look) - 1])) - && (icase - ? MB_STRNICMP(line - (p - look), look, p - look) - : STRNCMP(line - (p - look), look, p - look)) - == 0) - match = TRUE; - } - if (match && try_match_word && !try_match) - { - /* "0=word": Check if there are only blanks before the - * word. */ - if (getwhitecols_curline() != - (int)(curwin->w_cursor.col - (p - look))) - match = FALSE; - } - if (match) - return TRUE; - } - look = p; - } - - /* - * ok, it's a boring generic character. - */ - else - { - if (try_match && *look == keytyped) - return TRUE; - if (*look != NUL) - ++look; - } - - /* - * Skip over ", ". - */ - look = skip_to_option_part(look); - } - return FALSE; -} -#endif /* FEAT_CINDENT */ - #if defined(FEAT_RIGHTLEFT) || defined(PROTO) /* * Map Hebrew keyboard when in hkmap mode. diff --git a/src/indent.c b/src/indent.c new file mode 100644 index 0000000000..af401ee7ad --- /dev/null +++ b/src/indent.c @@ -0,0 +1,4683 @@ +/* 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. + */ + +/* + * indent.c: Indentation related functions + */ + +#include "vim.h" + +#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((unsigned)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) + +static char_u *skip_string(char_u *p); +static pos_T *find_start_rawstring(int ind_maxcomment); + +/* + * 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 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; +} + +/* + * 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; +} + +/* + * 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; +} +#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; + +/* + * Functions for C-indenting. + * Most of this originally comes from Eric Fischer. + */ +/* + * Below "XXX" means that this function may unlock the current line. + */ + +static int cin_isdefault(char_u *); +static int cin_ispreproc(char_u *); +static int cin_iscomment(char_u *); +static int cin_islinecomment(char_u *); +static int cin_isterminated(char_u *, int, int); +static int cin_iselse(char_u *); +static int cin_ends_in(char_u *, char_u *, char_u *); +static int cin_starts_with(char_u *s, char *word); +static pos_T *find_match_paren(int); +static pos_T *find_match_char(int c, int ind_maxparen); +static int find_last_paren(char_u *l, int start, int end); +static int find_match(int lookfor, linenr_T ourscope); + +/* + * 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; +} + +/* + * 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 label: "label:". + * Note: curwin->w_cursor must be where we are looking for the label. + */ + 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; +} + +/* + * 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; +} + +/* + * Recognize a switch label: "case .*:" or "default:". + */ + 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 "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 "public/private/protected" scope declaration label. + */ + 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] != ':'); +} + +/* Maximum number of lines to search back for a "namespace" line. */ +#define FIND_NAMESPACE_LIM 20 + +/* + * Recognize a "namespace" scope declaration. + */ + static int +cin_is_cpp_namespace(char_u *s) +{ + char_u *p; + int has_name = FALSE; + int has_name_start = FALSE; + + s = cin_skipcomment(s); + if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) + { + p = cin_skipcomment(skipwhite(s + 9)); + while (*p != NUL) + { + if (VIM_ISWHITE(*p)) + { + has_name = TRUE; /* found end of a name */ + p = cin_skipcomment(skipwhite(p)); + } + else if (*p == '{') + { + break; + } + else if (vim_iswordc(*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])) + { + if (!has_name_start || has_name) + return FALSE; + /* C++ 17 nested namespace */ + p += 3; + } + else + { + return FALSE; + } + } + return TRUE; + } + return FALSE; +} + +/* + * Recognize a `extern "C"` or `extern "C++"` linkage specifications. + */ + static int +cin_is_cpp_extern_c(char_u *s) +{ + char_u *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(skipwhite(s + 6)); + while (*p != NUL) + { + if (VIM_ISWHITE(*p)) + { + p = cin_skipcomment(skipwhite(p)); + } + else if (*p == '{') + { + break; + } + else if (p[0] == '"' && p[1] == 'C' && p[2] == '"') + { + if (has_string_literal) + return FALSE; + has_string_literal = TRUE; + p += 3; + } + else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+' + && p[4] == '"') + { + if (has_string_literal) + return FALSE; + has_string_literal = TRUE; + p += 5; + } + else + { + return FALSE; + } + } + return has_string_literal ? TRUE : FALSE; + } + 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 char_u * +after_label(char_u *l) +{ + for ( ; *l; ++l) + { + if (*l == ':') + { + if (l[1] == ':') /* skip over "::" for C++ */ + ++l; + else if (!cin_iscase(l + 1, FALSE)) + break; + } + else if (*l == '\'' && l[1] && l[2] == '\'') + l += 2; /* skip over 'x' */ + } + if (*l == NUL) + return NULL; + l = cin_skipcomment(l + 1); + if (*l == NUL) + return NULL; + return l; +} + +/* + * 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 */ +{ + char_u *l; + pos_T fp; + colnr_T col; + char_u *p; + + l = ml_get(lnum); + p = after_label(l); + if (p == NULL) + return 0; + + fp.col = (colnr_T)(p - l); + fp.lnum = lnum; + getvcol(curwin, &fp, &col, NULL, NULL); + 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, char_u **pp) +{ + char_u *l; + int amount; + pos_T cursor_save; + + cursor_save = curwin->w_cursor; + curwin->w_cursor.lnum = lnum; + l = ml_get_curline(); + /* XXX */ + if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel()) + { + amount = get_indent_nolabel(lnum); + l = after_label(ml_get_curline()); + if (l == NULL) /* just in case */ + l = ml_get_curline(); + } + else + { + amount = get_indent(); + l = ml_get_curline(); + } + *pp = l; + + curwin->w_cursor = cursor_save; + 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. + */ + static int +cin_first_id_amount(void) +{ + char_u *line, *p, *s; + int len; + pos_T fp; + colnr_T col; + + line = ml_get_curline(); + p = skipwhite(line); + len = (int)(skiptowhite(p) - p); + 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 = 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 && VIM_ISWHITE(s[3])) + || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4])) + || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5])) + || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4]))) + p = s; + } + for (len = 0; vim_isIDc(p[len]); ++len) + ; + if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p)) + return 0; + + 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"; + */ + static int +cin_get_equal_amount(linenr_T lnum) +{ + char_u *line; + char_u *s; + colnr_T col; + pos_T fp; + + if (lnum > 1) + { + line = ml_get(lnum - 1); + if (*line != NUL && line[STRLEN(line) - 1] == '\\') + return -1; + } + + line = s = ml_get(lnum); + while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) + { + if (cin_iscomment(s)) /* ignore comments */ + s = cin_skipcomment(s); + else + ++s; + } + if (*s != '=') + return 0; + + s = skipwhite(s + 1); + if (cin_nocode(s)) + return 0; + + if (*s == '"') /* nice alignment for continued strings */ + ++s; + + fp.lnum = lnum; + fp.col = (colnr_T)(s - line); + getvcol(curwin, &fp, &col, NULL, NULL); + return (int)col; +} + +/* + * 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; +} + +/* + * 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] == '/'); +} + +/* + * 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; +} + +/* + * Recognize the basic picture of a function declaration -- it needs to + * have an open paren somewhere and a close paren at the end of the line and + * no semicolons anywhere. + * When a line ends in a comma we continue looking in the next line. + * "sp" points to a string with the line. When looking at other lines it must + * be restored to the line. When it's NULL fetch lines here. + * "first_lnum" is where we start looking. + * "min_lnum" is the line before which we will not be looking. + */ + static int +cin_isfuncdecl( + char_u **sp, + linenr_T first_lnum, + linenr_T min_lnum) +{ + char_u *s; + linenr_T lnum = first_lnum; + linenr_T save_lnum = curwin->w_cursor.lnum; + int retval = FALSE; + pos_T *trypos; + int just_started = TRUE; + + if (sp == NULL) + s = ml_get(lnum); + else + s = *sp; + + curwin->w_cursor.lnum = lnum; + if (find_last_paren(s, '(', ')') + && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) + { + lnum = trypos->lnum; + if (lnum < min_lnum) + { + curwin->w_cursor.lnum = save_lnum; + return FALSE; + } + + s = ml_get(lnum); + } + curwin->w_cursor.lnum = save_lnum; + + /* Ignore line starting with #. */ + if (cin_ispreproc(s)) + return FALSE; + + while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') + { + if (cin_iscomment(s)) /* ignore comments */ + s = cin_skipcomment(s); + else if (*s == ':') + { + if (*(s + 1) == ':') + s += 2; + else + /* To avoid a mistake in the following situation: + * A::A(int a, int b) + * : a(0) // <--not a function decl + * , b(0) + * {... + */ + return FALSE; + } + else + ++s; + } + if (*s != '(') + return FALSE; /* ';', ' or " before any () or no '(' */ + + while (*s && *s != ';' && *s != '\'' && *s != '"') + { + if (*s == ')' && cin_nocode(s + 1)) + { + /* ')' at the end: may have found a match + * Check for he previous line not to end in a backslash: + * #if defined(x) && \ + * defined(y) + */ + lnum = first_lnum - 1; + s = ml_get(lnum); + if (*s == NUL || s[STRLEN(s) - 1] != '\\') + retval = TRUE; + goto done; + } + 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) */ + for (;;) + { + if (lnum >= curbuf->b_ml.ml_line_count) + break; + s = ml_get(++lnum); + if (!cin_ispreproc(s)) + break; + } + if (lnum >= curbuf->b_ml.ml_line_count) + break; + /* Require a comma at end of the line or a comma or ')' at the + * start of next line. */ + s = skipwhite(s); + if (!just_started && (!comma && *s != ',' && *s != ')')) + break; + just_started = FALSE; + } + else if (cin_iscomment(s)) /* ignore comments */ + s = cin_skipcomment(s); + else + { + ++s; + just_started = FALSE; + } + } + +done: + if (lnum != first_lnum && sp != NULL) + *sp = ml_get(first_lnum); + + return retval; +} + + static int +cin_isif(char_u *p) +{ + return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); +} + + 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])); +} + + static int +cin_isdo(char_u *p) +{ + return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(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 (char_u *p, linenr_T lnum) /* XXX */ +{ + pos_T cursor_save; + pos_T *trypos; + int retval = FALSE; + + p = cin_skipcomment(p); + if (*p == '}') /* accept "} while (cond);" */ + p = cin_skipcomment(p + 1); + if (cin_starts_with(p, "while")) + { + cursor_save = curwin->w_cursor; + curwin->w_cursor.lnum = lnum; + curwin->w_cursor.col = 0; + p = ml_get_curline(); + while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ + { + ++p; + ++curwin->w_cursor.col; + } + if ((trypos = findmatchlimit(NULL, 0, 0, + curbuf->b_ind_maxparen)) != NULL + && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') + retval = TRUE; + curwin->w_cursor = cursor_save; + } + 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(char_u *line, int *poffset) +{ + int offset = *poffset; + + if (offset-- < 2) + return 0; + while (offset > 2 && VIM_ISWHITE(line[offset])) + --offset; + + offset -= 1; + if (!STRNCMP(line + offset, "if", 2)) + goto probablyFound; + + if (offset >= 1) + { + offset -= 1; + if (!STRNCMP(line + offset, "for", 3)) + goto probablyFound; + + if (offset >= 2) + { + offset -= 2; + if (!STRNCMP(line + offset, "while", 5)) + goto probablyFound; + } + } + return 0; + +probablyFound: + if (!offset || !vim_isIDc(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". + */ + static int +cin_iswhileofdo_end(int terminated) +{ + char_u *line; + char_u *p; + char_u *s; + pos_T *trypos; + int i; + + if (terminated != ';') /* there must be a ';' at the end */ + return FALSE; + + p = line = ml_get_curline(); + while (*p != NUL) + { + p = cin_skipcomment(p); + if (*p == ')') + { + 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 */ + i = (int)(p - line); + curwin->w_cursor.col = i; + trypos = find_match_paren(curbuf->b_ind_maxparen); + if (trypos != NULL) + { + s = cin_skipcomment(ml_get(trypos->lnum)); + if (*s == '}') /* accept "} while (cond);" */ + s = cin_skipcomment(s + 1); + if (cin_starts_with(s, "while")) + { + curwin->w_cursor.lnum = trypos->lnum; + return TRUE; + } + } + + /* Searching may have made "line" invalid, get it again. */ + line = ml_get_curline(); + p = line + i; + } + } + if (*p != NUL) + ++p; + } + return FALSE; +} + + static int +cin_isbreak(char_u *p) +{ + return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(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". + */ + static int +cin_is_cpp_baseclass( + cpp_baseclass_cache_T *cached) /* input and output */ +{ + lpos_T *pos = &cached->lpos; /* find position */ + char_u *s; + int class_or_struct, lookfor_ctor_init, cpp_base_class; + linenr_T lnum = curwin->w_cursor.lnum; + char_u *line = ml_get_curline(); + + if (pos->lnum <= lnum) + return cached->found; /* Use the cached result */ + + pos->col = 0; + + s = skipwhite(line); + if (*s == '#') /* skip #define FOO x ? (x) : x */ + return FALSE; + s = cin_skipcomment(s); + if (*s == NUL) + 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) + * {} + */ + while (lnum > 1) + { + line = ml_get(lnum - 1); + s = skipwhite(line); + if (*s == '#' || *s == NUL) + break; + while (*s != NUL) + { + s = cin_skipcomment(s); + if (*s == '{' || *s == '}' + || (*s == ';' && cin_nocode(s + 1))) + break; + if (*s != NUL) + ++s; + } + if (*s != NUL) + break; + --lnum; + } + + pos->lnum = lnum; + line = ml_get(lnum); + s = line; + for (;;) + { + if (*s == NUL) + { + if (lnum == curwin->w_cursor.lnum) + break; + /* Continue in the cursor line. */ + line = ml_get(++lnum); + s = line; + } + if (s == line) + { + /* don't recognize "case (foo):" as a baseclass */ + if (cin_iscase(s, FALSE)) + break; + s = cin_skipcomment(line); + if (*s == NUL) + continue; + } + + if (s[0] == '"' || (s[0] == 'R' && s[1] == '"')) + 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; + 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 */ + cpp_base_class = TRUE; + lookfor_ctor_init = class_or_struct = FALSE; + pos->col = 0; + s = cin_skipcomment(s + 1); + } + 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; + + if (*s == 'c') + s = cin_skipcomment(s + 5); + else + s = cin_skipcomment(s + 6); + } + else + { + if (s[0] == '{' || s[0] == '}' || s[0] == ';') + { + 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; + } + else if (s[0] == '?') + { + /* Avoid seeing '() :' after '?' as constructor init. */ + return FALSE; + } + else if (!vim_isIDc(s[0])) + { + /* if it is not an identifier, we are wrong */ + class_or_struct = FALSE; + lookfor_ctor_init = FALSE; + } + else if (pos->col == 0) + { + /* it can't be a constructor-initialization any more */ + lookfor_ctor_init = FALSE; + + /* the first statement starts here: lineup with this one... */ + if (cpp_base_class) + pos->col = (colnr_T)(s - line); + } + + /* When the line ends in a comma don't align with it. */ + if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1)) + pos->col = 0; + + s = cin_skipcomment(s + 1); + } + } + + cached->found = cpp_base_class; + if (cpp_base_class) + pos->lnum = lnum; + return cpp_base_class; +} + + static int +get_baseclass_amount(int col) +{ + int amount; + colnr_T vcol; + pos_T *trypos; + + if (col == 0) + { + amount = get_indent(); + if (find_last_paren(ml_get_curline(), '(', ')') + && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) + amount = get_indent_lnum(trypos->lnum); /* XXX */ + if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL)) + amount += curbuf->b_ind_cpp_baseclass; + } + else + { + curwin->w_cursor.col = col; + getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); + amount = (int)vcol; + } + if (amount < curbuf->b_ind_cpp_baseclass) + amount = curbuf->b_ind_cpp_baseclass; + 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(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; +} + +/* + * 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])); +} + +/* + * Skip strings, chars and comments until at or past "trypos". + * Return the column found. + */ + static int +cin_skip2pos(pos_T *trypos) +{ + char_u *line; + char_u *p; + char_u *new_p; + + p = line = ml_get(trypos->lnum); + while (*p && (colnr_T)(p - line) < trypos->col) + { + if (cin_iscomment(p)) + p = cin_skipcomment(p); + else + { + new_p = skip_string(p); + if (new_p == p) + ++p; + else + p = new_p; + } + } + 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. */ +/* foo() */ +/* { */ +/* } */ + + static pos_T * +find_start_brace(void) /* XXX */ +{ + pos_T cursor_save; + pos_T *trypos; + pos_T *pos; + static pos_T pos_copy; + + cursor_save = curwin->w_cursor; + while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) + { + pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ + trypos = &pos_copy; + curwin->w_cursor = *trypos; + pos = NULL; + /* ignore the { if it's in a // or / * * / comment */ + if ((colnr_T)cin_skip2pos(trypos) == trypos->col + && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */ + break; + if (pos != NULL) + curwin->w_cursor.lnum = pos->lnum; + } + curwin->w_cursor = cursor_save; + return trypos; +} + +/* + * Find the matching '(', ignoring it if it is in a comment. + * Return NULL if no match found. + */ + static pos_T * +find_match_paren(int ind_maxparen) /* XXX */ +{ + return find_match_char('(', ind_maxparen); +} + + static pos_T * +find_match_char(int c, int ind_maxparen) /* XXX */ +{ + pos_T cursor_save; + pos_T *trypos; + static pos_T pos_copy; + int ind_maxp_wk; + + cursor_save = curwin->w_cursor; + ind_maxp_wk = ind_maxparen; +retry: + if ((trypos = findmatchlimit(NULL, 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 - (int)(cursor_save.lnum - trypos->lnum); + if (ind_maxp_wk > 0) + { + curwin->w_cursor = *trypos; + curwin->w_cursor.col = 0; /* XXX */ + goto retry; + } + trypos = NULL; + } + else + { + pos_T *trypos_wk; + + pos_copy = *trypos; /* copy trypos, findmatch will change it */ + trypos = &pos_copy; + curwin->w_cursor = *trypos; + if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */ + { + ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum + - trypos_wk->lnum); + if (ind_maxp_wk > 0) + { + curwin->w_cursor = *trypos_wk; + goto retry; + } + trypos = NULL; + } + } + } + curwin->w_cursor = cursor_save; + return trypos; +} + +/* + * Find the matching '(', ignoring it if it is in a comment or before an + * unmatched {. + * Return NULL if no match found. + */ + static pos_T * +find_match_paren_after_brace (int ind_maxparen) /* XXX */ +{ + pos_T *trypos = find_match_paren(ind_maxparen); + + if (trypos != NULL) + { + pos_T *tryposBrace = find_start_brace(); + + /* If both an unmatched '(' and '{' is found. Ignore the '(' + * position if the '{' is further down. */ + if (tryposBrace != NULL + && (trypos->lnum != tryposBrace->lnum + ? trypos->lnum < tryposBrace->lnum + : trypos->col < tryposBrace->col)) + trypos = NULL; + } + 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. + */ + static int +corr_ind_maxparen(pos_T *startpos) +{ + long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; + + if (n > 0 && n < curbuf->b_ind_maxparen / 2) + return curbuf->b_ind_maxparen - (int)n; + 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(char_u *l, int start, int end) +{ + int i; + int retval = FALSE; + int open_count = 0; + + curwin->w_cursor.col = 0; /* default is start of line */ + + for (i = 0; l[i] != NUL; i++) + { + i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ + i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ + if (l[i] == start) + ++open_count; + else if (l[i] == end) + { + if (open_count > 0) + --open_count; + else + { + curwin->w_cursor.col = i; + retval = TRUE; + } + } + } + return retval; +} + +/* + * 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_u *p; + char_u *l; + char_u *digits; + int n; + int divider; + int fraction = 0; + int sw = (int)get_sw_value(buf); + + /* + * 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. */ + buf->b_ind_open_imag = 0; + + /* 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. */ + 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. */ + 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. */ + 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. */ + buf->b_ind_jump_label = -1; + + /* Spaces from the switch() indent a "case xx" label should be located. */ + buf->b_ind_case = sw; + + /* Spaces from the "case xx:" code after a switch() should be located. */ + buf->b_ind_case_code = sw; + + /* 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. */ + buf->b_ind_scopedecl = sw; + + /* Spaces from the scope declaration label code should be located. */ + buf->b_ind_scopedecl_code = sw; + + /* Amount K&R-style parameters should be indented. */ + buf->b_ind_param = sw; + + /* 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. */ + buf->b_ind_cpp_baseclass = sw; + + /* 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. */ + buf->b_ind_unclosed2 = sw; + + /* Suppress ignoring spaces from the indent of a line starting with an + * unclosed parentheses. */ + 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). */ + buf->b_ind_unclosed_wrapped = 0; + + /* Suppress ignoring white space when lining up with the character after + * an unclosed parentheses. */ + buf->b_ind_unclosed_whiteok = 0; + + /* Indent a closing parentheses under the line start of the matching + * opening parentheses. */ + buf->b_ind_matching_paren = 0; + + /* Indent a closing parentheses under the previous line. */ + buf->b_ind_paren_prev = 0; + + /* Extra indent for comments. */ + buf->b_ind_comment = 0; + + /* 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. */ + buf->b_ind_in_comment2 = 0; + + /* Max lines to search for an open paren. */ + buf->b_ind_maxparen = 20; + + /* Max lines to search for an open comment. */ + buf->b_ind_maxcomment = 70; + + /* Handle braces for java code. */ + buf->b_ind_java = 0; + + /* Not to confuse JS object properties with labels. */ + buf->b_ind_js = 0; + + /* Handle blocked cases correctly. */ + buf->b_ind_keep_case_label = 0; + + /* Handle C++ namespace. */ + buf->b_ind_cpp_namespace = 0; + + /* Handle continuation lines containing conditions of if(), for() and + * while(). */ + buf->b_ind_if_for_while = 0; + + /* indentation for # comments */ + buf->b_ind_hash_comment = 0; + + /* Handle C++ extern "C" or "C++" */ + buf->b_ind_cpp_extern_c = 0; + + for (p = buf->b_p_cino; *p; ) + { + l = p++; + if (*p == '-') + ++p; + digits = p; /* remember where the digits start */ + n = getdigits(&p); + divider = 0; + if (*p == '.') /* ".5s" means a fraction */ + { + fraction = atol((char *)++p); + while (VIM_ISDIGIT(*p)) + { + ++p; + if (divider) + divider *= 10; + else + divider = 10; + } + } + if (*p == 's') /* "2s" means two times 'shiftwidth' */ + { + if (p == digits) + n = sw; /* just "s" is one 'shiftwidth' */ + else + { + n *= sw; + if (divider) + n += (sw * fraction + divider / 2) / divider; + } + ++p; + } + if (l[1] == '-') + n = -n; + + /* 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; break; + case 'e': buf->b_ind_open_imag = n; break; + case 'n': buf->b_ind_no_brace = n; break; + case 'f': buf->b_ind_first_open = n; break; + case '{': buf->b_ind_open_extra = n; break; + case '}': buf->b_ind_close_extra = n; break; + case '^': buf->b_ind_open_left_imag = n; break; + case 'L': buf->b_ind_jump_label = n; break; + case ':': buf->b_ind_case = n; break; + case '=': buf->b_ind_case_code = n; break; + case 'b': buf->b_ind_case_break = n; break; + case 'p': buf->b_ind_param = n; break; + case 't': buf->b_ind_func_type = n; break; + case '/': buf->b_ind_comment = n; break; + case 'c': buf->b_ind_in_comment = n; break; + case 'C': buf->b_ind_in_comment2 = n; break; + case 'i': buf->b_ind_cpp_baseclass = n; break; + case '+': buf->b_ind_continuation = n; break; + case '(': buf->b_ind_unclosed = n; break; + ca