1 "---------------------------------------------------------------------------
2 " Vim global plugin for adding and manipulating signature blocks in e-mails
3 " Maintainer: Antonio Ospite <ospite@studenti.unina.it>
5 " Last Change: 2009-11-24
6 " License: This script is free software; you can redistribute it and/or
7 " modify it under the terms of the GNU General Public License.
10 " Drop this file into your $HOME/.vim/plugin directory.
13 " map <Leader>s :call AppendSignature('~/.signature')<CR>
14 " map <Leader>r :call ReplaceLastSignature('~/.signature')<CR>
15 " map <Leader>R :call ReplaceAllSignatures('~/.signature')<CR>
17 " " Append a signature block to all e-mails
18 " autocmd FileType mail if AddSignature('~/.signature') | w | endif
20 " " Replace the git version with a signature in cover letters generated with git-format-patch
21 " autocmd BufRead 0000-cover-letter.patch if ReplaceLastSignature('~/.signature') | w | endif
22 " autocmd BufRead 0000-cover-letter.patch autocmd! BufRead 0000-cover-letter.patch
25 " http://en.wikipedia.org/wiki/Signature_block
26 " http://tools.ietf.org/html/rfc1855
28 "---------------------------------------------------------------------------
30 if exists("g:loaded_signaturePlugin") | finish | endif
31 let g:loaded_signaturePlugin = 1
34 " Function: SigFileReadable()
35 " Purpose: Check if the signature file is readable
36 "---------------------------------------------------------------------------
37 func! SigFileReadable(sigfile)
38 let filename = expand(a:sigfile)
39 if !filereadable(filename)
40 echoerr "E484: Can't open file " . filename
47 "---------------------------------------------------------------------------
48 " Function: AppendSignature()
49 " Purpose: Append a signature block at the end of message
50 "---------------------------------------------------------------------------
51 func! AppendSignature(sigfile)
52 " Add the signature marker at the end of the file
55 " Append the signature block file at the end of the file
56 exe '$r ' . fnameescape(a:sigfile)
60 "---------------------------------------------------------------------------
61 " Function: AddSignature()
62 " Purpose: Add a signature block if there isn't one already
63 "---------------------------------------------------------------------------
64 func! AddSignature(sigfile)
65 if !SigFileReadable(a:sigfile)
69 " Save current cursor position in mark 'z'
72 " Append a signature block only if there isn't one already
75 catch /^Vim\%((\a\+)\)\=:E486/ " catch error E486 (search command failed)
76 " put an extra newline
78 call AppendSignature(a:sigfile)
81 " restore cursor position from mark 'z' if the mark is still valid
88 "---------------------------------------------------------------------------
89 " Function: ReplaceAllSignatures()
90 " Purpose: Replace all signature blocks in the message
91 "---------------------------------------------------------------------------
92 func! ReplaceAllSignatures(sigfile)
93 if !SigFileReadable(a:sigfile)
97 " Save current cursor position in mark 'z'
101 " delete from the FIRST signature marker '^-- $' down to
102 " the end of the file
104 catch /^Vim\%((\a\+)\)\=:E486/ " catch error E486 (search command failed)
107 call AppendSignature(a:sigfile)
109 " restore cursor position from mark 'z' if the mark is still valid
116 "---------------------------------------------------------------------------
117 " Function: ReplaceLastSignature()
118 " Purpose: Replace only the last signature block in the message
119 "---------------------------------------------------------------------------
120 func! ReplaceLastSignature(sigfile)
121 if !SigFileReadable(a:sigfile)
125 " Save current cursor position in mark 'z'
129 " delete from the LAST signature marker '^-- $' down to
130 " the end of the file
132 catch /^Vim\%((\a\+)\)\=:E486/ " catch error E486 (search command failed)
135 call AppendSignature(a:sigfile)
137 " restore cursor position from mark 'z' if the mark is still valid