]> 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:

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