On 2007-05-12, Allan Wind <[EMAIL PROTECTED]> wrote:
> I would like to auto-wrap emails and set formatoptions to tcqlaqw with:
> 
> autocmd FileType mail set formatoptions+=aw 
> 
> Other than missing the mentioned "j" in vim 7.0 to make join remove 
> comment leaders this works great for the body of the message.  Trouble 
> is that I have mutt configured to pass in headers, and obviously do not 
> want header lines wrapped.
> 
> Any ideas on how to configure vim to not wrap the header but do wrap the 
> body of a mail automatically?  mail syntax already knows what a header 
> looks like including quoted headers, so perhaps there is a way to use 
> that?

I've played with this a little, with mixed success.  My solution 
works well enough most of the time that I haven't taken the time to 
make it better, but it doesn't work well enough that I would post it 
as a plugin.

I use this autocommand,

    au CursorHold * if &ft == 'mail' | call <SID>flowed_context() | endif

and a function, s:flowed_context(), that attempts to determine which 
part of the message the cursor is in by executing

    let syn_name = synIDattr(synID(line("."), col("."), 1), "name")

    if syn_name =~ "^mailHeader" || syn_name == "mailEmail" || syn_name == 
"mailSubject"
        let l:msg_part = "header"
        let in_body = 0
    elseif syn_name == "mailSignature"
        let l:msg_part = "signature"
        let in_body = 0
    else
        let l:msg_part = "body"
        let in_body = 1
    endif

and then sets 'formatoptions' with or without 'aw' depending on the 
value of 'in_body'.

One of the problems with it is that sometimes, as I'm editing the 
header, the text is no longer identified by the syntax rules as part 
of the header, so 'aw' gets added to 'formatoptions' and the header 
text gets messed up.

If I were to work on this more, I think I would search the buffer to 
find the boundaries between parts instead of using the syntax 
highlighting and use the new winsaveview() and winrestview() 
functions to more completely save and restore the window and cursor 
states at the start and end of the flowed_context() function.  Those 
functions weren't available when I first wrote the function, which 
is why I used syntax highlighting to determine where the cursor was 
in order to avoid moving the cursor during the autocommand.

HTH,
Gary

-- 
Gary Johnson                 | Agilent Technologies
[EMAIL PROTECTED]     | Mobile Broadband Division
                             | Spokane, Washington, USA

Reply via email to