C comment block indentation woes

125 views
Skip to first unread message

Oliver Wraight

unread,
Aug 14, 2015, 6:13:59 PM8/14/15
to vim_use
I've been merrily using vim for the past fifteen years and loving the cindent/cinoptions functionality, but alas I have finally met my match when trying to auto indent some c block comments according to a new company's coding standard.

The behaviour I'm after is ludicrously simple:

/*
Comment block indented
by shiftwidth
*/

The behaviour I'm getting is:

/*
Comment block indented
by shiftwidth
*/

Try as I might, I can not get that closing */ to be aligned with the opening /*. I don't care about when typing (the default 'comments' rules are fine), but when I use the = command, the */ stubbornly lines with the rest of the comment block.

Given that there isn't an option for aligning the closing */ indicated in the cino section of the manual, I'm guessing that there probably isn't a way of doing it. I am an eternal optimist though, so I'm hoping some bright spark will be able to help out. Essentially, the option I'm looking for is the comment equivalent of cino-}

For reference, the relevant settings I have are:

:verbose set ft? cin? cino? com? si? ai? inde?

filetype=c
Last set from /usr/share/vim/vim74/filetype.vim
cindent
cinoptions={sn-s>2s^-scs
comments=sO:* -,mO:* ,exO:*/,s1:/*,mb:*,ex:*/,://
Last set from /usr/share/vim/vim74/ftplugin/c.vim
nosmartindent
noautoindent
indentexpr=

Thanks for any help you can offer, even a "Can't be done" would help.

Erik Christiansen

unread,
Aug 15, 2015, 6:16:54 AM8/15/15
to vim_use
On 14.08.15 04:02, Oliver Wraight wrote:
> The behaviour I'm after is ludicrously simple:
>
> /*
> Comment block indented
> by shiftwidth
> */
>
> The behaviour I'm getting is:
>
> /*
> Comment block indented
> by shiftwidth
> */
>
...

> comments=sO:* -,mO:* ,exO:*/,s1:/*,mb:*,ex:*/,://
^^^^^^^^^^^^^^^^
> Last set from /usr/share/vim/vim74/ftplugin/c.vim

that's what needs to be tweaked.

Unfortunately, performing a :set with that here gives:

E518: Unknown option: -,mO:*

So I'll just try tweaking the three-part comment portion of it (see
":h format-comments") to see what can be done with that in a few
minutes. Changing the s,m,e parts of my local setting to:

:set comments=s:/*,mb1:.,ex-1:*/,://,b:#,:%,:XCOMM,n:>,fb:-,fb:o
^^^^^^^^^^^^^^^^

produces:

/*
. Auto-generated middle line start character.
*/

Why the dot? If I replace '.' with a space, Vim barfs:

E525: Zero length string: comments=s1:/*,mb:

which seems to be due to >>>

Three-piece comments must have a middle string because otherwise Vim
can't recognize the middle lines.
<<<

/*
_ Is this any better?
_ (Use "mb1:_")
*/

> Thanks for any help you can offer, even a "Can't be done" would help.

The 'f' flag should ideally (wishfully?) be able to eliminate the
character auto-insertion for comment middle lines:

f Only the first line has the comment string. Do not repeat comment
on the next line, but preserve indentation.

But that is perhaps only for bullet lists, and it can't (at present) be
inveigled in a three-part comment format specification. Nevertheless,
this may be enough to get them off your back. The indentation is
compliant, and the underscore (or more conventional '*'?) might be able
to be sold, at least until a complete solution is winkled out of the
woodwork.

Best of luck with the format police.

Erik
(Who had the luxury, over three decades, of being or having the C local
format police report to him. :-))

--
C hasn't changed much since the 1970s. And let's face it it's ugly.
Can't we do better? C++? (Sorry, never mind.) - Rob Pike

LCD 47

unread,
Aug 17, 2015, 12:26:25 AM8/17/15
to vim_use
On 15 August 2015, Erik Christiansen <dva...@internode.on.net> wrote:
> On 14.08.15 04:02, Oliver Wraight wrote:
[...]
> > comments=sO:* -,mO:* ,exO:*/,s1:/*,mb:*,ex:*/,://
> ^^^^^^^^^^^^^^^^
> > Last set from /usr/share/vim/vim74/ftplugin/c.vim
>
> that's what needs to be tweaked.
>
> Unfortunately, performing a :set with that here gives:
>
> E518: Unknown option: -,mO:*
[...]

On a side note: you can usually avoid dealing with ":set" for
options that take string values by using "let &option = ..." instead of
"set option=...". E.g. for the line above:

let &comments = 'sO:* -,mO:* ,exO:*/,s1:/*,mb:*,ex:*/,://'

To set local options you'd use "let &l:option = ..." instead of
"setlocal option=...", and to explicitly set a global options you can do
"let &g:option = ..." instead of "setglobal option=...".

Of course, "&option" can also be used to read the value of an option
if needed.

/lcd

Erik Christiansen

unread,
Aug 17, 2015, 4:30:31 AM8/17/15
to vim_use
On 17.08.15 07:26, LCD 47 wrote:
> On a side note: you can usually avoid dealing with ":set" for
> options that take string values by using "let &option = ..." instead of
> "set option=...". E.g. for the line above:
>
> let &comments = 'sO:* -,mO:* ,exO:*/,s1:/*,mb:*,ex:*/,://'

Thanks for that, lcd. Trying my 90% fix of the indenting issue with that
&comments as a base, we can have:

/*
| As Vim requires a non-space in order to recognise
| comment middle lines, then
| it boils down to an aesthetic choice.
*/

/*
; Vim-style middle line
; prefix, perhaps?
*/

The latter with:

let &comments = 'sO:* -,mO:* ,exO:*/,s:/*,mb1:;,ex-1:*/,://'

The "let" syntax does allow entering "mb1: ,", and the comment middle
lines then have no visible prefix, but the auto-conversion of the last
prefix to "*/" on a single '/' keystroke, to finish the comment, then
fails.

Trying for a NBSP, to see if that's sufficiently distinctive, without
being visible, with:

let &comments = 'sO:* -,mO:* ,exO:*/,s:/*,mb1:%xA0,ex-1:*/,://'

Delivers the literal "%xA0" as middle line prefix. Is there a way to put
an NBSP into &comments, I wonder?

My alibi for the formatting police would be:

"The comments conform to the coding standard. The leading ';' are
comment content, and therefore not precluded by policy."

Erik
Reply all
Reply to author
Forward
0 new messages