Most of the Vim code is indented in the following way:
#v+
{
if (condition1
&& condition2)
action();
doit(arg1,
arg2,
arg3);
}
#v-
However, this has to be adjusted manually, because as close as Vim can
get automatically is with the setting cino=(0 (which can sometimes get
difficult to spot where the actual action for if is):
#v+
{
if (condition1
&& condition2)
action();
doit(arg1,
arg2,
arg3);
}
#v-
The attached patch makes Vim indent code as in the first example if you
specify cino=(0,k2s
This kind of indenting is applied to continuation lines for if(), for()
and while().
--
Cheers,
Lech
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
diff --git a/runtime/doc/indent.txt b/runtime/doc/indent.txt
index 9a4aaa7..5cd0183 100644
--- a/runtime/doc/indent.txt
+++ b/runtime/doc/indent.txt
@@ -460,6 +460,19 @@ The examples below assume a 'shiftwidth' of 4.
a_short_line(argument, a_short_line(argument,
argument); argument);
<
+ *cino-k*
+ kN When in unclosed parentheses which follows "if", "for" or
+ "while" and N is non-zero, indent the line N characters relative
+ to the outer context (i.e. the line where "if", "for" or
+ "while" is). (default: 0).
+
+ cino=(0 cino=(0,ks >
+ if (condition1 if (condition1
+ && condition2) && condition2)
+ action(); action();
+ function(argument1 function(argument1
+ && argument2); && argument2);
+<
*cino-m*
mN When N is non-zero, line up a line starting with a closing
parentheses with the first character of the line with the
diff --git a/src/misc1.c b/src/misc1.c
index 95b4547..d45d3a3 100644
--- a/src/misc1.c
+++ b/src/misc1.c
@@ -5724,6 +5724,52 @@ cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */
}
/*
+ * Check whether in "p" there is an "if", "for" or "while" before offset.
+ * 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(line, offset, poffset)
+ char_u *line;
+ size_t offset;
+ int *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;
@@ -6346,6 +6392,12 @@ get_c_indent()
*/
int ind_cpp_namespace = 0;
+ /*
+ * handle continuation lines containing conditions of if(), for() and
+ * while()
+ */
+ int ind_if_for_while = 0;
+
pos_T cur_curpos;
int amount;
int scope_amount;
@@ -6390,6 +6442,7 @@ get_c_indent()
int cont_amount = 0; /* amount for continuation line */
int original_line_islabel;
int added_to_amount = 0;
+ int is_if_for_while = 0;
for (options = curbuf->b_p_cino; *options; )
{
@@ -6462,6 +6515,7 @@ get_c_indent()
case 'l': ind_keep_case_label = n; break;
case '#': ind_hash_comment = n; break;
case 'N': ind_cpp_namespace = n; break;
+ case 'k': ind_if_for_while = n; break;
}
if (*options == ',')
++options;
@@ -6806,41 +6860,50 @@ get_c_indent()
{
cur_amount = MAXCOL;
l = ml_get(our_paren_pos.lnum);
- if (ind_unclosed_wrapped
- && cin_ends_in(l, (char_u *)"(", NULL))
+
+ if (ind_if_for_while)
+ is_if_for_while =
+ cin_is_if_for_while_before_offset(l, our_paren_pos.col,
+ &our_paren_pos.col);
+
+ if (!is_if_for_while)
{
- /* look for opening unmatched paren, indent one level
- * for each additional level */
- n = 1;
- for (col = 0; col < our_paren_pos.col; ++col)
+ if (ind_unclosed_wrapped
+ && cin_ends_in(l, (char_u *)"(", NULL))
{
- switch (l[col])
+ /* look for opening unmatched paren, indent one level
+ * for each additional level */
+ n = 1;
+ for (col = 0; col < our_paren_pos.col; ++col)
{
- case '(':
- case '{': ++n;
- break;
-
- case ')':
- case '}': if (n > 1)
- --n;
- break;
+ switch (l[col])
+ {
+ case '(':
+ case '{': ++n;
+ break;
+
+ case ')':
+ case '}': if (n > 1)
+ --n;
+ break;
+ }
}
- }
- our_paren_pos.col = 0;
- amount += n * ind_unclosed_wrapped;
- }
- else if (ind_unclosed_whiteok)
- our_paren_pos.col++;
- else
- {
- col = our_paren_pos.col + 1;
- while (vim_iswhite(l[col]))
- col++;
- if (l[col] != NUL) /* In case of trailing space */
- our_paren_pos.col = col;
- else
+ our_paren_pos.col = 0;
+ amount += n * ind_unclosed_wrapped;
+ }
+ else if (ind_unclosed_whiteok)
our_paren_pos.col++;
+ else
+ {
+ col = our_paren_pos.col + 1;
+ while (vim_iswhite(l[col]))
+ col++;
+ if (l[col] != NUL) /* In case of trailing space */
+ our_paren_pos.col = col;
+ else
+ our_paren_pos.col++;
+ }
}
}
@@ -6853,6 +6916,8 @@ get_c_indent()
getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
if (cur_amount > (int)col)
cur_amount = col;
+ if (is_if_for_while)
+ cur_amount += ind_if_for_while;
}
}
@@ -6860,8 +6925,9 @@ get_c_indent()
{
/* Line up with the start of the matching paren line. */
}
- else if (ind_unclosed == 0 || (!ind_unclosed_noignore
- && *look == '(' && ignore_paren_col == 0))
+ else if (is_if_for_while || ind_unclosed == 0
+ || (!ind_unclosed_noignore
+ && *look == '(' && ignore_paren_col == 0))
{
if (cur_amount != MAXCOL)
amount = cur_amount;
diff --git a/src/testdir/test3.in b/src/testdir/test3.in
index f987478..af267c1 100644
--- a/src/testdir/test3.in
+++ b/src/testdir/test3.in
@@ -1535,6 +1535,34 @@ baz();
}
STARTTEST
+:set cino&
+:set cino+=(0,k2s
+2kdd=][
+ENDTEST
+
+void func(int arg1, int arg2)
+{
+for (int i = 0;
+i < 10;
+++i)
+printf("for(): i == %d\n",
+i);
+
+if (arg1 > 0
+&& arg2 < 0)
+printf("arg1 > 0 (%d) && arg2 < 0 (%d)\n",
+arg1,
+arg2);
+
+while (arg1 > 0
+|| arg2 > 0)
+printf("%d : %d\n",
+arg1 = arg1 / 2,
+arg2 = arg2 / 2);
+
+}
+
+STARTTEST
:set cino=N-s
/^NAMESPACESTART
=/^NAMESPACEEND
diff --git a/src/testdir/test3.ok b/src/testdir/test3.ok
index c95b188..66f40e6 100644
--- a/src/testdir/test3.ok
+++ b/src/testdir/test3.ok
@@ -1372,6 +1372,29 @@ void func(void)
}
+void func(int arg1, int arg2)
+{
+ for (int i = 0;
+ i < 10;
+ ++i)
+ printf("for(): i == %d\n",
+ i);
+
+ if (arg1 > 0
+ && arg2 < 0)
+ printf("arg1 > 0 (%d) && arg2 < 0 (%d)\n",
+ arg1,
+ arg2);
+
+ while (arg1 > 0
+ || arg2 > 0)
+ printf("%d : %d\n",
+ arg1 = arg1 / 2,
+ arg2 = arg2 / 2);
+
+}
+
+
NAMESPACESTART
/* valid namespaces with normal indent */
namespace