Tuesday, January 31, 2012

A Versatile fmt command

hi, More adventures with my Blind friend. Sun's version of fmt, let all lines
starting with a dot (.) alone, in deferrence to he unix markup language,
Troff. The Redhat version of fmt, does not do that.

I love Vim's versatile internal fmt command. However, it defines a paragraph
as a text block bounded either by blank lines, or one of the 2 character
(troff) dot commands, set with the paragraph variable (e.g., .IP .LP etc.).
My friend uses tons of dot commands and, so needs a fmt command that treats
virtually every ``dot'' command as a paragraph boundary. That could approach
100 commands or so to set paragraphs to.

So - I wrote a little script that ignore's dot commands (see below), and
formats a single ``paragraph'' bounded only by blank lines. Here it is:

keepmarks
kA
/^ *$\|\%$/
kB
'A,'B/^[^\.]/s/^\([^\.]\)/>\1/
'A,'B!fmt -p '>'
.,/^ *$\|\%$//^>/s/^>//
'A

It is mapped to, v which was simply map v !}fmt before. I learned one must
unmap v first, before a new map will work.

Im now interested in expanding the script in two ways: Make it format an
arbitary block of text, and make it format those quotes within emails like
this:

> Small line
> a much larger and larger and larger line

In so - a command that treats all dot commands are paragraph boundaries, that
formats a single paragraph by default, and that formats quoted text (even
interspersed with dot commands) within emails. Here is a try that does not
work:

if search("^>", 'cn') > 0
%!fmt -p '>'
elseif line("'>") > 0
'<,'>/^[^\.]/s/^\([^\.]\)/>\1/
'<,'>'!fmt -p '>'
'<,'>/^>/s/^>//
endif
keepmarks
kA
/^ *$\|\%$/
kB
'A,'B/^[^\.]/s/^\([^\.]\)/>\1/
'A,'B!fmt -p '>'
.,/^ *$\|\%$//^>/s/^>//
'A
endif


The idea was to use the marks like '> set by visual mode, after hitting <ESC>,
for arbitrary blocks. These marks to persis, but a script does not seem to
be aware of them. Also, given both a range and pattern for substitute commands
is to avoid ``pattern not found'' messages when lines do not have what is
being searched for.


Possible solutions: Somehow fool vim's paragraphs variable into accepting
every line that begins with a dot. Figure a script test for a mark like mA,
set on an initial line. Then I got to the end of a block and run the script.
trouble is, A (or whatever) may be already defined in a buffer, so how to test
that it was just set now? Or someway for a script to recognize the marks set
by the highlight mode.

Any ideas?


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: