]> git.madduck.net Git - etc/vim.git/blob - .vim/after/ftplugin/mail.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

245ef06d34c6205d4d5e07aad73484bdc286963a
[etc/vim.git] / .vim / after / ftplugin / mail.vim
1 setlocal textwidth=68
2 setlocal formatoptions-=o
3 setlocal formatoptions-=r
4 setlocal formatoptions+=a
5 setlocal formatoptions+=w
6
7 "setlocal spell
8
9 " Dynamically set format options, depending on where you are in a
10 " mail, idea from Teemu Likonen:
11 " http://groups.google.com/group/vim_use/msg/f59e5c1adc6be2b3
12
13 let s:defaults = "setlocal fo=".&fo
14 " tw=".&tw." ts=".&ts." sts=".&sts." sw=".&sw." fo=".&fo
15 execute s:defaults
16 let b:MailAreaDetect=1
17
18 nnoremap <buffer><silent> <F9> :call <SID>MailAreaDetect_Switch(0)<CR>
19 inoremap <buffer><silent> <F9> <C-\><C-O>:call <SID>MailAreaDetect_Switch(1)<CR>
20
21 function! s:MailAreaDetect_Switch(vmode)
22     if b:MailAreaDetect
23         silent call <SID>MailAreaDetect_Off()
24         let b:MailAreaDetect=0
25         echo 'MailAreaDetect Off'
26         if a:vmode
27             sleep 1
28         endif
29     else
30         silent call <SID>MailAreaDetect_On()
31         let b:MailAreaDetect=1
32         echo 'MailAreaDetect On'
33         if a:vmode
34             sleep 1
35         endif
36     endif
37 endfu
38
39 function! s:MailAreaDetect_On()
40     silent autocmd! MailAreaDetect CursorMoved,CursorMoved
41         \ <buffer> call <SID>AreaOptions()
42     let b:MailAreaDetect=1
43 endfunction
44
45 function! s:MailAreaDetect_Off()
46     silent autocmd! MailAreaDetect
47     execute s:defaults
48     let b:MailAreaDetect=0
49 endfunction
50
51 augroup MailAreaDetect
52     autocmd!
53     call <SID>MailAreaDetect_On()
54 augroup END
55
56 function! s:AreaOptions()
57     execute s:defaults
58
59     let s:synCur = synIDattr(synID(line("."),1,0),"name")
60     let s:synNext = synIDattr(synID(line(".")+1,1,0),"name")
61
62     if s:synNext =~ '^mailQuote' && s:synCur == '' && getline('.') =~ '^.'
63       " elseif getline('.') =~ '^\s*[^>]' && getline(line('.')+1) =~ '\m^\s*>'
64       setlocal fo-=a
65       "echo 'Quotation leader (fo='.&fo.')'
66     elseif s:synCur =~ '^mailQuote'
67       " elseif getline('.') =~ '\m^\s*>'
68       setlocal fo-=a
69       "echo 'Quotation (fo='.&fo.')'
70     elseif <SID>CheckArea('\m^--- .*\n^+++ ','\v(^$|\n^-- $)')
71       setlocal fo-=a fo-=w fo-=t noet
72       "echo 'Patch (fo='.&fo.')'
73     elseif s:synCur == '' && s:synNext =~ '^mailSignature'
74       " elseif getline(line('.')+1) =~ '\m^-- $'
75       setlocal fo-=a fo-=w fo-=t
76       "echo 'Last line before signature (fo='.&fo.')'
77     elseif s:synCur =~ '^mailSignature'
78       " elseif <SID>CheckArea('^-- $','^$')
79       setlocal fo-=a fo-=w fo-=t
80       "echo 'Signature (fo='.&fo.')'
81     elseif s:synCur =~ '^mail'
82       " if <SID>CheckArea('\v^From( |: ).*\n','\v^$')
83       setlocal fo-=a fo-=w fo-=t
84       "echo 'Header (fo='.&fo.')'
85     else
86       "echo 'My text (fo='.&fo.')'
87     endif
88 endfunction
89
90 function! s:CheckArea(start, end)
91     return (search(a:start,'bcnW')-line('.')) >
92         \ (search(a:end,'bnW')-line('.'))
93 endfunction
94
95 function! FixQuotes()
96   let l:i = 0
97   let l:lineNxt = getline(l:i)
98   let l:synNxt = synIDattr(synID(l:i,1,0),"name")
99   while l:i < line('$')-1
100     let l:lineCur = l:lineNxt
101     let l:synCur = l:synNxt
102     let l:lineNxt = getline(l:i+1)
103     let l:synNxt = synIDattr(synID(l:i+1,1,0),"name")
104     if l:synCur =~ '^mailQuote' && l:lineCur =~ '\w.*\S$' &&
105           \ l:synNxt =~ '^mailQuote' && l:lineNxt =~ '\w'
106       call setline(l:i, l:lineCur . ' ')
107     endif
108     let l:i += 1
109   endwhile
110 endfunction
111
112 augroup fixquotes
113   au!
114   autocmd BufWinEnter $TMPDIR/mutt-* call FixQuotes()
115 augroup END
116
117 nmap <buffer> <C-P><F1> :w<CR>:%!mailplate --keep-unknown official<CR>
118 nmap <buffer> <C-P><F2> :w<CR>:%!mailplate --keep-unknown tahi<CR>
119 nmap <buffer> <C-P><F3> :w<CR>:%!mailplate --keep-unknown pmvm<CR>
120 nmap <buffer> <C-P><F4> :w<CR>:%!mailplate --keep-unknown pobox<CR>
121 nmap <buffer> <C-P><F5> :w<CR>:%!mailplate --keep-unknown debian<CR>
122 nmap <buffer> <C-P><F6> :w<CR>:%!mailplate --keep-unknown debconf<CR>
123 nmap <buffer> <C-P><F7> :w<CR>:%!mailplate --keep-unknown uniwh<CR>
124 nmap <buffer> <C-P><F8> :w<CR>:%!mailplate --keep-unknown mtfk<CR>
125 nmap <buffer> <C-P><F9> :w<CR>:%!mailplate --keep-unknown sudetia<CR>
126 nmap <buffer> <C-P><F12> :w<CR>:%!mailplate --keep-unknown private<CR>
127 nmap <buffer> <F1> :w<CR>:%!mailplate --auto --keep-unknown 2>/dev/null<CR>
128
129 " change subject line
130 " Before: Subject: old
131 " After : Subject: New (was: old)
132 " http://strcat.de/wiki/dotfiles#vim
133 " map ,sw 1G/^Subject: <CR>:s/Re:/was:/<CR>Wi (<C-O>$)<ESC>0Whi
134 map <Leader>ns 1G/^Subject: /<CR>:s,\(Subject: \)\(Re: \)*\(.*\)$,\1 (was: \3),<CR>:set nohls<CR>f li
135
136 " Delete 'was' in the Subject.
137 " Before: Subject: New (was: old)
138 " After : Subject: New
139 " http://strcat.de/wiki/dotfiles#vim
140 map <Leader>dw 1G/^Subject: /<CR>:s, *(was: .*)$<CR>:set nohls<CR>f l
141
142 " http://dollyfish.net.nz/blog/2008-04-01/mutt-and-vim-custom-autocompletion
143 fun! LBDBCompleteFn(findstart, base)
144   let line = getline('.')
145   if a:findstart
146     " locate the start of the word
147     let start = col('.') - 1
148     while start > 0 && line[start - 1] =~ '[^:,]'
149       let start -= 1
150     endwhile
151     while start < col('.') && line[start] =~ '[:, ]'
152       let start += 1
153     endwhile
154     return start
155   else
156     let res = []
157     let query = substitute(a:base, '"', '', 'g')
158     let query = substitute(query, '\s*<.*>\s*', '', 'g')
159     for m in LbdbQuery(query)
160       call complete_add(printf('"%s" <%s>', escape(m[0], '"'), m[1]))
161       if complete_check()
162         break
163       endif
164     endfor
165     return res
166   endif
167 endfun
168 set completefunc=LBDBCompleteFn
169
170 " And this magic by James Vega, which ensures that we only complete with lbdb
171 " on the recipient lines of the mail header.
172 inoremap <expr> <C-n> DelegateCompletion(1)
173 inoremap <expr> <C-p> DelegateCompletion(0)
174
175 fun! DelegateCompletion(next)
176   if getline('.') =~? '^\%(To\|B\=Cc\):'
177     return "\x18\x15"
178   elseif a:next
179     return "\x0e"
180   else
181     return "\x10"
182   endif
183 endfun