-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Weird indentation of C labels when a statement block starts on the same line #3229
Comments
<offtopic> Then came programming theorists who had cut their milk teeth on C, ALGOL, and the like, and they told us that GOTO statements were evil, that they necessarily begat unreadable spaghetti code, and that "structured programming" was the way to go. So we painstakingly learned to make COBOL programs with no GOTO statements, or maybe only a very few of them located exclusively inside AT END and ON SIZE ERROR clauses. And in fact, now that the only Turing-complete language I'm still fluently writing is Vim script, it doesn't even have goto statements. But they seem to be doing a comeback in C, which I had always thought of as belonging to the family of "structured programming" languages, where goto statements were neither necessary, nor even useful. Strange how history turns back on itself, like the famed Ouroboros snake. |
I came up with a workaround, but it's rather ugly and inefficient and it fails when there are inline comments: " Do not force label at column 0
setlocal cinoptions+=L0
setlocal indentexpr=GetMyCIndent()
function! GetMyCIndent()
" Fix indentation of labeled blocks,
let m = matchstr(getline(v:lnum), '^[[:space:]]*[[:alpha:][:digit:]_]\+:')
if !empty(m)
" look for the indentation level of the containing block
let [lnum,col] = searchpairpos('{', '', '}', 'bnW')
if lnum > 0
" and increase it by one indentation level
return indent(prevnonblank(lnum)) + &shiftwidth
endif
endif
" Fix indentation of the first line in a labeled block.
let m = matchstr(getline(prevnonblank(v:lnum - 1)), '^[[:space:]]*[[:alpha:][:digit:]_]\+:')
if !empty(m)
return indent(prevnonblank(v:lnum - 1)) + &shiftwidth
endif
" Indent closing braces at the same level of the opening one
let m = matchstr(getline(v:lnum), '^[[:space:]]*}')
if !empty(m)
let [lnum,col] = searchpairpos('{', '', '}', 'bnW')
if lnum > 0
return indent(lnum)
endif
endif
" fall back to cindent for the other cases
return cindent(v:lnum)
endfunction |
Hi,
I am seeing a weird behavior (vim 8.1.89) when autoindenting C code where a goto label is followed by a block opened on the same line:
Also on http://vpaste.net/oR1VG
I know it's an unusual syntax but it's still valid C.
I found this issue when I was trying to reuse
cindent
to indent Device-Tree files which have a very C-like syntax.Any idea if there is some setting to remove the extra padding?
I read
cinoptions
andcinkeys
documentation but couldn't find anything to solve this,+=J1
seems to help for the first label but it messes up some other indentation.Is it a bug?
This may be somehow related to #37.
Thanks,
Antonio
The text was updated successfully, but these errors were encountered: