/*
* 1. Mary had a
* little lamb
*/
This seems a simple task, but both methods I've tried have failed.
Method 1: using :set fo+=n formatlistpat=^\\s*\\*\\s*\\d\\+\\.\\s*
Idea: get Vim to recognize the list as a "numbered list" and format it
appropriately.
Typing exactly the same text gives:
/*
* 1. Mary had a
* ltle lamb
*
*/it
Note _two_ problems with this:
1. The comment leader is indented along with the text
2. The cursor, for whatever reason, is placed in the middle of the
word "little" when the automatic linebreak/indent happens.
This behavior puzzles me, but not yet ready to give up, I try method
2. (be sure to relaunch vim or set fo and flp to their defaults).
Method 2: using set comments=ns1:/*,nmb:*,nex:*/,nfb:1.
Idea: use 1. as a nested first-line-only comment. Obviously you
wouldn't actually do this for a numeric list, but bear with me...I'm
actually doing this for several specific key words.
Result:
/*
* 1. Mary had a
* 1. little lamb
*/
Note the problem: the "first line only" comment is included on the
second line as well. This bizzare behavior is made even worse if you
try typing the text on the first line instead, yielding:
/* 1. Mary had a
* little lamb
*/
Note that this is exactly the result we want, but it only works on the
first line of the outer comment. This inconsistency is puzzling, and
it also makes this unworkable as a general solution, where the list
can occur anywhere within the comment.
If anyone understands what is causing the problem with either method,
please enlighten me. If anyone knows a workaround or an alternate
method to accomplish this, that's even better.
I'm using Vim 7.1.297 Big for MS Windows, obtained from the "Cream"
download page on sourceforge.
I considered going this route, but is there a way to have this
formatting function _only_ act when it matches the specific case I
want, and have the default behavior be applied in all other cases? I
don't want to re-write Vim's built-in autoformat capability!
Something weird is going on. Try this:
:set formatexpr=0 tw=10
Type: Mary had a little lamb
I get "Mary had a little lamb" all on one line, as expected.
Now :set formatexpr=1 and repeat.
I get:
Mary had a
little
lamb
Again, this is as expected.
But now, go to the first line (where "Mary had a little lamb" is all
on one line) and use gqq. Nothing happens!
:help gq says it uses 'formatexpr'
:help 'formatexpr' says it is "evaluated to format a range of lines
for the gq operator" but that it is "also evaluated when 'textwidth'
is set and adding text beyond that limit." It seems to be evaluated
_only_ in the second case.
What's going on?
I don't get the same results: with formatexpr=0 and with formatexpr=1 I
get no formatting. With formatexpr= (the empty string) the line is
reformatted. This contradicts the help sentence "When the function
returns non-zero Vim will fall back to using the internal format
mechanism.".
Best regards,
Tony.
--
"There are three possibilities: Pioneer's solar panel has turned away
from the sun; there's a large meteor blocking transmission; or someone
loaded Star Trek 3.2 into our video processor."
For the example I gave, my formatoptions is set to "tcq"
I'll think about that some more later. Probably will post it to vim_dev.
In the meantime...I'm having a lot of trouble making this function.
First problem:
I'm doing something like:
let subst_command = s/will_not_match/replace_pattern/
silent! execute subst_command
But even though the pattern does not match, v:errmsg is not set. Is
there a reason for this?
Also, I'm having a LOT of trouble trying to restore the cursor to
where it needs to be while inserting. I did find an example using
col() and cursor()...I'll have to give that a shot, but does anyone
know of a good, reliable method to restore cursor position within a
formatexpr function?
Finally, Brett:
Have you had any success with calling gw within the formatexpr as
discussed here:
http://tech.groups.yahoo.com/group/vim/message/87942?threaded=1&var=1&p=2
or have you discovered a better way to do this? I have a very similar
situation: I only want to apply special formatting to the lines
following the list headings, the rest of the lines can be formatted
normally.