Search code examples
cvimfolding

`fold-create-marker` with default `{{{` marker not working properly when function definition contains `*`


I have this setting in vim

foldmarker={{{,}}}
commentstring=/*%s*/
foldmethod=marker

I want to create new marker using zf, which does work for

void foo(void bar) {
}

when I selected that and press zf, I get

void foo(void bar) {/*{{{*/
}/*}}}*/

so far so good. However when function definition contains pointer, like in

void foo(void * bar) {
}

I do get

void foo(void * bar) {{{{
}/*}}}*/

which is wrong. How can I configure vim to work properly even in second case?


Solution

  • That strange behavior is caused by the default value of the :help 'comments' option, in particular the mb:* part.

    :help format-comments explains

      To avoid recognizing "*ptr" as a comment, the middle string includes the 'b' flag.
    

    Unfortunately, the way you write the pointer (which whitespace after the *) defeats this heuristic, and Vim detects the * as a middle piece of a three-piece comment (Vim doesn't verify that there's actually a start piece before it), assumes the fold start already is part of a comment, and therefore skips adding the 'commentstring' there.

    To fix this, either adapt your coding style (write void *bar), or modify the 'comments' option, e.g. by removing (or refining if you use it) the middle piece part:

    setlocal comments-=mb:*
    

    If you need to keep middle pieces and correct manual folds, you'd have to write a small wrapper mapping around zf that temporarily changes the option.