]> git.madduck.net Git - etc/vim.git/blob - .vim/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:

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