From: martin f. krafft Date: Wed, 19 Dec 2018 11:41:31 +0000 (+0100) Subject: editor config depending on mail area detection X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/cd2954216a912e56167a897c91e6cca0e4f92903?hp=54266c30afbde03d9ddcfbfbb4d22f4750655a55 editor config depending on mail area detection --- diff --git a/.vim/after/ftplugin/mail.vim b/.vim/after/ftplugin/mail.vim index 6b6d527..21e66d8 100644 --- a/.vim/after/ftplugin/mail.vim +++ b/.vim/after/ftplugin/mail.vim @@ -3,3 +3,81 @@ setlocal formatoptions-=o setlocal formatoptions-=r "setlocal spell + +" Dynamically set format options, depending on where you are in a +" mail, idea from Teemu Likonen: +" http://groups.google.com/group/vim_use/msg/f59e5c1adc6be2b3 + +let d_fo = &fo +let s:defaults = 'setlocal tw=68 ts=2 sts=2 sw=2 fo='.d_fo +execute s:defaults +let b:MailAreaDetect=1 + +"nnoremap ma1 :call MailAreaDetect_On() +" \ echo 'MailAreaDetect On' +"nnoremap ma0 :call MailAreaDetect_Off() +" \ echo 'MailAreaDetect Off' + +nnoremap :call MailAreaDetect_Switch(0) +inoremap :call MailAreaDetect_Switch(1) + +function! s:MailAreaDetect_Switch(vmode) + if b:MailAreaDetect + silent call MailAreaDetect_Off() + let b:MailAreaDetect=0 + echo 'MailAreaDetect Off' + if a:vmode + sleep 1 + endif + else + silent call MailAreaDetect_On() + let b:MailAreaDetect=1 + echo 'MailAreaDetect On' + if a:vmode + sleep 1 + endif + endif +endfu + + +function! s:MailAreaDetect_On() + silent autocmd! MailAreaDetect CursorMoved,CursorMoved + \ call AreaOptions() + let b:MailAreaDetect=1 +endfunction + +function! s:MailAreaDetect_Off() + silent autocmd! MailAreaDetect + execute s:defaults + let b:MailAreaDetect=0 +endfunction + +augroup MailAreaDetect + autocmd! + call MailAreaDetect_On() +augroup END + +function! s:AreaOptions() + execute s:defaults + if CheckArea('\v^From( |: ).*\n','\v^$') + "echo 'Header' + setlocal fo-=a fo-=w fo-=t sts=0 sw=2 noet + elseif getline('.') =~ '\m^\s*>' + "echo 'Quotation' + setlocal fo-=a fo-=w + elseif CheckArea('\m^--- .*\n^+++ ','\v(^$|\n^-- $)') + "echo 'Patch' + setlocal fo-=a fo-=w fo-=t sts=0 sw=2 noet + elseif CheckArea('^-- $','^$') + "echo 'Signature' + setlocal fo-=a fo-=w fo-=t sts=0 sw=2 noet + else + "echo 'My text' + setlocal fo+=aw et + endif +endfunction + +function! s:CheckArea(start, end) + return (search(a:start,'bcnW')-line('.')) > + \ (search(a:end,'bnW')-line('.')) +endfunction