Indenting lists in comments

11 views
Skip to first unread message

Benjamin Fritz

unread,
Apr 9, 2008, 10:45:02 AM4/9/08
to vim_use
With formatoptions including the 'n' flag, Vim will autoformat lists
to keep the list item indented beside the list item. For example,

1: this is list
item 1
2: this is list
item 2

With the formatlistpat string, you can add arbitrary list patterns.

In this tip, I attempt to use this to indent javadoc intelligently:

http://vim.wikia.com/wiki/Format_javadoc

But, apparently the 'n' flag is not playing nicely with the comment leaders.

Can anyone fix this problem? I actually want it to fix a similar
problem of my own in C code:

/*
* NAME: func_1()
*
* DESCRIPTION: big long description
* on several lines that
* I need to indent just
* like numbered lists!
*
* NOTES: setting up a formatlistpat
* like in the javadoc tip
* above does dumb things
* like this!
*/

If you can fix the problem, please feel free to update the tip as well!

Benjamin Fritz

unread,
Apr 9, 2008, 12:11:02 PM4/9/08
to vim_use

Here's what I do to attempt to fix my problem, BTW:

In $HOME/vimfiles/ftplugin/c.vim:
if v:version >= 701 " feature introduced in version 7
" indent the info in function headers closer to the way we want
" This will treat NOTES: and similar as lists for indenting
setlocal flp+=\\\|^\\s*-\\?\\*\\s*\\C[A-Z\ ]\\+:\\s\\+
endif

In my vimrc, within an augroup:
autocmd Filetype * set fo+=n

(If you're confused about the -\\? in the pattern, it's because I
sometimes use comments like this:

/*
-* NOTES: blah blah blah
-*
-* etc.
*/

Benjamin Fritz

unread,
Apr 14, 2008, 2:11:37 PM4/14/08
to vim_use

Still no replies on this. Does anyone know how to have autoformat do
the following:

/*
* NOTES: look how my text
* aligns to the left
* after a header
* like 'NOTES'
*/

I was thinking formatlistpat could be used, but I tried it and it also
indents the comment leaders.

Any autoformat gurus out there? Is this even possible right now, or
would it require a code change?

Gary Johnson

unread,
Apr 14, 2008, 4:14:54 PM4/14/08
to vim...@googlegroups.com
On 2008-04-14, Benjamin Fritz <fritzo...@gmail.com> wrote:

> Still no replies on this. Does anyone know how to have autoformat do
> the following:
>
> /*
> * NOTES: look how my text
> * aligns to the left
> * after a header
> * like 'NOTES'
> */
>
> I was thinking formatlistpat could be used, but I tried it and it also
> indents the comment leaders.
>
> Any autoformat gurus out there? Is this even possible right now, or
> would it require a code change?

I got this to work my editing my 'comments' option:

1. Insert the 'n' code in front of every part so that each part
will accept a nested part.

2. Add this part:

nfb:NOTES:

For a C file, I set my 'comments' option to look like this:

nsO:* -,nmO:* ,nexO:*/,ns1:/*,nmb:*,nex:*/,://,nfb:NOTES:

Then I set 'tw' to 30 and started typing your example:

/* NOTES: look how my text


* aligns to the left

* after a header like
* 'NOTES'
*/

I never hit Enter except after the last word--the text just flowed
as you see it.

Is that what you were looking for?

HTH,
Gary

A.Politz

unread,
Apr 14, 2008, 5:09:41 PM4/14/08
to vim...@googlegroups.com
Benjamin Fritz wrote:

>Still no replies on this. Does anyone know how to have autoformat do
>the following:
>
>/*
> * NOTES: look how my text
> * aligns to the left
> * after a header
> * like 'NOTES'
> */
>
>I was thinking formatlistpat could be used, but I tried it and it also
>indents the comment leaders.
>
>Any autoformat gurus out there? Is this even possible right now, or
>would it require a code change?
>
>
>
>

As it looks, the builtin format doesn't work in this case.
But you can still use a 'formatexpr'.

-ap


--
:wq

Benjamin Fritz

unread,
Apr 14, 2008, 5:02:55 PM4/14/08
to vim...@googlegroups.com

I think this is exactly what I'm looking for, but I'm getting really
weird results.

I have formatoptions set to tcqro and textwidth set to 80, and get the
following with various configurations of the comments option:


/* comments = nsO: -,nmO:* ,nexO:*/,ns1:/*,nmb:*,nex:*/,://,nfb:NOTES:
*
* NOTES: mary had a little lamb whose fleece was white as snow and everywhere
* NOTES: that mary went that lamb was sure to go
*
*/

/* comments = nsO: -,nmO:* ,nexO:*/,ns1:/*,nmb:*,nex:*/,://,nfbO:NOTES:
*
* NOTES: mary had a little lamb whose fleece was white as snow and everywhere
* NOTES: that mary went
*
*/

/* comments = sO: -,mO:* ,exO:*/,s1:/*,mb:*,ex:*/,://,nfb:NOTES:
*
* NOTES: mary had a little lamb whose fleece was white as snow and everywhere
* that mary went
*
*/

/* comments = nsO: -,nmO:* ,nexO:*/,ns1:/*,nmb:*,nex:*/,://,fb:NOTES:
*
* NOTES: mary had a little lamb whose fleece was white as snow and everywhere
* that mary went
*
*/

From reading :help format-comments, I think something is a little
wonky. The 'f' flag in nfb:NOTES: should make it so NOTES: does not
get prepended again. Any ideas?

Behavior seems the same regardless of cindent, and regardless of
whether I remove the :// from the comments string.

I'm using Vim 7.1.293 on MS Windows, 'big' with GUI.

Benjamin Fritz

unread,
Apr 14, 2008, 5:05:45 PM4/14/08
to vim...@googlegroups.com

I'd really rather not, but if it's my only option...

What exactly is the format expression supposed to do? I can't seem to
piece it together from :help 'formatexpr' or :help 'formatprg'

Does it return something or actually do the formatting? How do I get
it to perform the default behavior in all but this one special case?

Gary Johnson

unread,
Apr 14, 2008, 5:33:59 PM4/14/08
to vim...@googlegroups.com

I'd agree with that.

I think I understand the last two results, but the first two
certainly don't agree with my experiments.

> The 'f' flag in nfb:NOTES: should make it so NOTES: does not
> get prepended again. Any ideas?

Right. No.

I'm running vim 7.1.236 on Linux. I didn't disable any plugins, so
let me do that now.

$ vim -N -u NONE -i NONE bar.c
:set comments=ns1:/*,nmb:*,nex:*/,n://,nb:#,n:%,n:XCOMM,n:>,nfb:-,nfb:NOTES:
:set tw=80
:set cindent
:set fo=tcqro

Now I get the same results as you did. Hmm.

I'll look into this some more, but in the mean time I'll send this
off so you'll know what I've found so far.

Regards,
Gary

Gary Johnson

unread,
Apr 14, 2008, 6:43:24 PM4/14/08
to vim...@googlegroups.com

I've got to drop this for now, but I'll share what I've found so
far. In the "... -u NONE ..." vim instance, I set 'comments' to be
the same as in the other, "full" vim instance. Then in the "full"
vim instance, I deleted all the autocommands and all the mappings,
then changed all the settings until ":set" in both instances showed
the same settings. I even saved the settings via ":mkvimrc!
<filename>" in both instances and verified that the two files were
identical. Still, the formatting works in the original "full" vim
instance and fails in the "... -u NONE ..." vim instance. I'm
confused.

The only clue to what might be happening is that at some point in
this progression of settings, both vim instances showed cindent set,
yet only one of the files saved with ":mkvimrc" showed cindent set.
I suspect a bug in the setting of cindent, but I don't have time at
the moment to verify that.

Regards,
Gary

Yukihiro Nakadaira

unread,
Apr 15, 2008, 9:00:53 AM4/15/08
to vim...@googlegroups.com
Try autofmt plugin.
http://www.vim.org/scripts/script.php?script_id=1939
This plugin emulate Vim's behavior and have a few improvement like you want.

--
Yukihiro Nakadaira - yukihiro....@gmail.com

Benjamin Fritz

unread,
Apr 15, 2008, 10:03:28 AM4/15/08
to vim...@googlegroups.com
On 4/15/08, Yukihiro Nakadaira <yukihiro....@gmail.com> wrote:
>
> Try autofmt plugin.
> http://www.vim.org/scripts/script.php?script_id=1939
> This plugin emulate Vim's behavior and have a few improvement like you want.
>
>

I might give that a try, but I'd really like to know why NOTES: gets
inserted on subsequent lines by the autoformat. According to :help
format-comments, with the 'f' flag,

"Only the first line has the comment string. Do not repeat comment on
the next line, but preserve indentation (e.g. a bullet-list)"

The fact that subsequent lines DO get the comment string indicates
either a bug or some screwy option to me.

Reply all
Reply to author
Forward
0 new messages