formatprg and gq or gw

478 views
Skip to first unread message

Kevin Walsh

unread,
May 26, 2011, 11:21:08 AM5/26/11
to v...@vim.org
Hello,

First time poster here, but long time vim user. Can someone help me
with the formatprg variable?

I set the variable to some external program, like:
formatprg=par

There are three problems I'd like help or advice on resolving:

1. gq} leaves the cursor on the last formatted line. This is sort of
documented in the help, but it is also silly, and inconsistent with
the behavior of gq} using the internal formatter. It seems that
putting the cursor where the motion would have put it is much more
reasonable, and this is what the happens with formatprg unset. It also
allow "." to repeat the command for the next paragraph. Putting it on
the last line of the paragraph seems pointless. Is there any way to
work around this, so that the cursor is placed reasonably after
invoking the external command?

2. Is there any way to send the external command the vim textwidth
variable? I know I can do "formatprg=par\ -w80" or something, but I
have different files with different textwidths. I was hoping for
something like "formatprg=par\ -w$textwidth". Without this ability,
the external formatprg is much less useful: it is both more powerful
than the internal formatter, because it can invoke a program written
in C or python or whatever, but also less powerful in that it can't
even implement an equivalent to the standard internal formatter.

3. gq uses formatprg, but gw ignores it. This is documented. But is
there some reason why this is so? It doesn't make any sense from the
user perspective. Is a feature or just a documented bug?

Maybe formatexpr can help? Can I use formatexpr to invoke an external
program but without the issues in 1 and 2? And from reports on the
web, it seems that gw might even use formatexpr, though the docs say
otherwise. Does anyone have a simple example of a formatexpr I could
modify for this purpose?

Thanks,
Kevin Walsh

Jean-Rene David

unread,
May 26, 2011, 6:58:08 PM5/26/11
to v...@vim.org
* Kevin Walsh [2011.05.26 17:40]:
[...]

> 2. Is there any way to send the external command the vim textwidth
> variable?

You need to arrange for this:

exe 'set fp=par\ -w' . &tw

to be executed whenever 'textwidth' changes. Perhaps in a filetype
plugin, a mapping or an autocommand.

HTH,

--
JR

Kevin Walsh

unread,
May 27, 2011, 8:19:36 AM5/27/11
to vim...@googlegroups.com, v...@vim.org

That is helpful, thanks. But is it possible to arrange for it to
execute whenever 'textwidth' changes? I edit lots of files with mode
lines at the top, and 'textwidth' is one of the common things found in
all those files. It isn't possible for me to put 'fp=...' in all of
those mode lines, unfortunately.

-kevin

ZyX

unread,
May 27, 2011, 8:58:28 AM5/27/11
to vim...@googlegroups.com
Reply to message «formatprg and gq or gw»,
sent 19:21:08 27 May 2011, Friday
by Kevin Walsh:

> Maybe formatexpr can help? Can I use formatexpr to invoke an external
> program but without the issues in 1 and 2?

Try
function s:Eval(expr)
return eval(a:expr)
endfunction
let s:sid=matchstr(s:Eval('expand("<sfile>")'), '\d\+')
function s:ParFormat()
let savedwinview=winsaveview()
try
execute v:lnum.','.(v:lnum+v:count-1).'!par -w'.&textwidth
finally
call winrestview(savedwinview)
endtry
endfunction
let &formatexpr='<SNR>'.s:sid.'_ParFormat()'

Original message:

signature.asc
Reply all
Reply to author
Forward
0 new messages