Hello,
Emilia Smólska <[email protected]> wrote:
> I am running OpenBSD 8.0 -stable, it seems from CVS history that the issue has
> been there since c-mode existed though.
>
> To reproduce the issue, try entering a snippet like the following with c-mode
> enabled:
>
> int
> main(void)
> {
> for (;;)
> printf("hello:world\n");
> }
>
> mg incorrectly interprets the colon in the string literal to be a label, and
> forces the line's indentation to be reduced by one. A workaround is bracing
> the statement, or not using c-mode.
it's not due to mg interpreting it as a label, but rather mg
(re)computing the indentation for the previous line, and then using it
to figure out what the current line indentation should be.
mg doesn't know that for can accept just one statement without braces,
so it's assuming that printf(...) needs to be aligned just like the
previous line.
the colon is "just" one of those character that triggers mg automatic
indentation in c-mode.
diff below is a first, ugly, stab at this. works for me, but it's not
the best diff i wrote today. or this week.
diff /home/op/w/mg
path + /home/op/w/mg
commit - 94abcc23ae38e54186093bb9819ce0c6cf00a083
blob - 26bba76dd5a610f78220d3d63d600e6c2f6cd078
file + cmode.c
--- cmode.c
+++ cmode.c
@@ -14,6 +14,7 @@
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
+#include <string.h>
#include "def.h"
#include "funmap.h"
@@ -85,6 +86,12 @@ static struct KEYMAPE (3) cmodemap = {
}
};
+/* grammar */
+
+static const char *keywords[] = { /* statements that acceps no brace */
+ "do", "else", "for", "if", "while",
+};
+
/* Function, Mode hooks */
void
@@ -237,6 +244,8 @@ getindent(const struct line *lp, int *curi)
int astp = FALSE; /* Asterisk? */
int cpos = -1; /* comment position */
int cppp = FALSE; /* Preprocessor command? */
+ int keywordp = FALSE; /* looking at a keyword? */
+ int i;
*curi = 0;
@@ -250,6 +259,17 @@ getindent(const struct line *lp, int *curi)
nicol++;
}
+ /* Does it starts with a keyword that accepts no braces? */
+ for (i = 0; i < sizeof(keywords)/sizeof(keywords[0]); ++i) {
+ size_t t = strlen(keywords[i]);
+ if (t > llength(lp) - lo)
+ t = llength(lp) - lo;
+ if (!strncmp(lp->l_text+lo, keywords[i], t)) {
+ keywordp = TRUE;
+ break;
+ }
+ }
+
/* If last line was blank, choose 0 */
if (lo == llength(lp))
nicol = 0;
@@ -324,6 +344,12 @@ getindent(const struct line *lp, int *curi)
if (colonp) {
*curi += cc_colon_indent;
newind -= cc_colon_indent;
+ } else if (keywordp && obrace == 0) {
+ /*
+ * otherwise, if the line starts with a keyword but
+ * no brace, indent anyway
+ */
+ newind += cc_basic_indent;
}
*curi -= (cbrace) * cc_basic_indent;