summaryrefslogtreecommitdiffstats
path: root/mail.vim
blob: d6cb33c665ddda5d292c289781f4b847a714547f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"" ============================================================================
""   ~/.vim/mail
""   Cedric Duval
"" ============================================================================

" Setup to write mails with vim
" To use with Mutt, just put this line your ~/.vimrc :
"   autocmd BufRead /tmp/mutt*      :source ~/.vim/mail



"" ----------------------------------------------------------------------------
""   Misc
"" ----------------------------------------------------------------------------

set pastetoggle=<f12>   " toggle mouse paste mode with F12
set ft=mail             " needless to say, vim had already guessed that alone :)
"set spell


"" ----------------------------------------------------------------------------
""   Automatic line wrap
"" ----------------------------------------------------------------------------

set textwidth=72	" max line length
set formatoptions=tcql
set comments+=n:\|	" '|' is a quote char.
set comments+=n:%	" '%' as well.

" * <F1> to re-format the current paragraph correctly
" * <F2> to format a line which is too long, and go to the next line
" * <F3> to merge the previous line with the current one, with a correct
"        formatting (sometimes useful associated with <F2>)
"
" These keys might be used both in command mode and edit mode.
"
" <F1> might be smarter to use with the Mail_Del_Empty_Quoted() function
" defined below

nmap	<F1>	gqap
nmap	<F2>	gqqj
nmap	<F3>	kgqj
map!	<F1>	<ESC>gqapi
map!	<F2>	<ESC>gqqji
map!	<F3>	<ESC>kgqji



"" ----------------------------------------------------------------------------
""   Suppressing quoted signature(s) if any when replying
"" ----------------------------------------------------------------------------

" Thanks to Luc Hermitte for the original function
" (http://hermitte.free.fr/vim/ressources/vimfiles/ftplugin/mail/Mail_Sig_set_vim.html)
" Thanks to Loc Minier and Martin Treusch von Buttlar who pointed out an
" issue with the user's own sig.

function! Mail_Erase_Sig_old()
  let i = line('$')
  let j = i
  " search for the signature pattern (takes into account signature delimiters
  " from broken mailers that forget the space after the two dashes)
  while ((i > 0) && (getline(i) !~ '^> *-- \=$'))
    if (getline(i) =~ '^-- $')
      " this is my own sig. please don't delete it!
      let j = i - 1
    endif
    let i = i - 1
  endwhile

  " if found, then
  if (i != 0)
    " search for the last non empty (non sig) line
    while ((i > 0) && (getline(i - 1) =~ '^\(>\s*\)*$'))
      let i = i - 1
    endwhile
    " and delete those lines plus the signature
    exe ':'.i.','.j.'d'
  endif
endfunction

" this new version handles cases where there are several signatures
" (sometimes added by mailing list software)
function! Mail_Erase_Sig()
  " search for the signature pattern (takes into account signature delimiters
  " from broken mailers that forget the space after the two dashes)
  let i = 0
  while ((i <= line('$')) && (getline(i) !~ '^> *-- \=$'))
    let i = i + 1
  endwhile

  " if found, then
  if (i != line('$') + 1)
    " first, look for our own signature, to avoid deleting it
    let j = i
    while (j < line('$') && (getline(j + 1) !~ '^-- $'))
      let j = j + 1
    endwhile

    " second, search for the last non empty (non sig) line
    while ((i > 0) && (getline(i - 1) =~ '^\(>\s*\)*$'))
      let i = i - 1
    endwhile

    " third, delete those lines plus the signature
    exe ':'.i.','.j.'d'
  endif
endfunction


"" ----------------------------------------------------------------------------
""   Replacing empty quoted lines (e.g. "> $") with empty lines
""   (convenient to automatically reformat one paragraph)
"" ----------------------------------------------------------------------------

function! Mail_Del_Empty_Quoted()
  exe "normal :%s/^>[[:space:]\%\|\#>]\\+$//e\<CR>"
endfunction



"" ----------------------------------------------------------------------------
""   Moving the cursor at the begining of the mail
"" ----------------------------------------------------------------------------

function! Mail_Begining()
  exe "normal gg"
  if getline (line ('.')) =~ '^From: '
    " if we use edit_headers in Mutt, then go after the headers
    exe "normal /^$\<CR>"
  endif
endfunction



"" ----------------------------------------------------------------------------
""
""   Initializations
""
"" ----------------------------------------------------------------------------

call Mail_Erase_Sig()
call Mail_Del_Empty_Quoted()
call Mail_Begining()

"http://permalink.gmane.org/gmane.editors.vim.devel/20890
setlocal foldmethod=expr foldlevel=1 foldminlines=2
setlocal foldexpr=strlen(substitute(substitute(getline(v:lnum),'\\s','','g'),'[^>].*','',''))

fun! LBDBCompleteFn(findstart, base)
    if a:findstart
        " locate the start of the word
        let line = getline('.')
        let start = col('.') - 1
        while start > 0 && line[start - 1] =~ '[^:,]'
            let start -= 1
        endwhile
        while start < col('.') && line[start] =~ '[:, ]'
            let start += 1
        endwhile
        return start
    else
        let res = []
        let query = substitute(a:base, '"', '', 'g')
        let query = substitute(query, '\s*<.*>\s*', '', 'g')
        for m in LbdbQuery(query)
            call add(res, printf('"%s" <%s>', escape(m[0], '"'), m[1]))
        endfor
        return res
    endif
endfun

set completefunc=LBDBCompleteFn
ino <C-n> <C-X><C-U>
ino <C-p> <C-X><C-U>


" To be done elsewhere
" http://dollyfish.net.nz/blog/2008-04-01/mutt-and-vim-custom-autocompletion
":silent! lbdbq | awk '{ print  }' > /tmp/lbdict
"setlocal dictionary+=/tmp/lbdict
"silent! %s/\(^\([a-zA-z-]\+:\|--\)\)\@<!\s\+$//
"silent! %s/^\(>\+\) >/\1>/g
"silent! %s/^\(>\+\) >/\1>/g
" map <F3> and <F4> to go to next inlined reply
" " register save/restore
" "autocmd FileType mail map <buffer> <F3> :/^>//^[^>]/<CR>:nohlsearch<CR>zzzv
" autocmd FileType mail map <buffer> <F3>
" :/^>//^\(>\)\@!\ze\s*\S/<CR>:nohlsearch<CR>zzzv
" autocmd FileType mail map <buffer> <F4>
" :/^>\s*>//^>\(\s*>\)\@!\ze\s*\S/<CR>:nohlsearch<CR>zzzv
" " (doc: that's just ":/pattern1//pattern2/")
"