1 "---------------------------------------------------------------------------
2 " Vim global plugin for adding and manipulating signature blocks in e-mails
3 " Maintainer: Antonio Ospite <ao2@ao2.it>
5 " Last Change: 2016-06-08
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.
12 " - Check that the signature file is readable before removing the old
13 " signature and append the new one.
17 " Drop this file into your $HOME/.vim/plugin directory.
20 " map <Leader>s :call AppendSignature('~/.signature')<CR>
21 " map <Leader>r :call ReplaceLastSignature('~/.signature')<CR>
22 " map <Leader>R :call ReplaceAllSignatures('~/.signature')<CR>
24 " " Append a signature block to all e-mails
25 " autocmd FileType mail if AddSignature('~/.signature') | w | endif
27 " " Replace the git version with a signature in cover letters generated with git-format-patch
28 " autocmd BufRead 0000-cover-letter.patch if ReplaceLastSignature('~/.signature') | w | endif
29 " autocmd BufRead 0000-cover-letter.patch autocmd! BufRead 0000-cover-letter.patch
32 " http://en.wikipedia.org/wiki/Signature_block
33 " http://tools.ietf.org/html/rfc1855
35 " The latest version of this script is available at these locations:
36 " https://git.ao2.it/vim/signature_block.vim.git
37 " http://www.vim.org/scripts/script.php?script_id=2872
38 " https://github.com/vim-scripts/signature_block.vim
40 "---------------------------------------------------------------------------
42 if exists("g:loaded_signaturePlugin") | finish | endif
43 let g:loaded_signaturePlugin = 1
46 " Function: SigFileReadable()
47 " Purpose: Check if the signature file is readable
48 "---------------------------------------------------------------------------
49 func! SigFileReadable(sigfile)
50 let filename = expand(a:sigfile)
51 if !filereadable(filename)
52 echoerr "E484: Can't open file " . filename
59 "---------------------------------------------------------------------------
60 " Function: AppendSignature()
61 " Purpose: Append a signature block at the end of message
62 "---------------------------------------------------------------------------
63 func! AppendSignature(sigfile)
64 " Add the signature marker at the end of the file
67 " Append the signature block file at the end of the file
68 exe '$r ' . fnameescape(a:sigfile)
72 "---------------------------------------------------------------------------
73 " Function: AddSignature()
74 " Purpose: Add a signature block if there isn't one already
75 "---------------------------------------------------------------------------
76 func! AddSignature(sigfile)
77 if !SigFileReadable(a:sigfile)
81 " Save current cursor position in mark 'z'
84 " Append a signature block only if there isn't one already
87 catch /^Vim\%((\a\+)\)\=:E486/ " catch error E486 (search command failed)
88 " put an extra newline
90 call AppendSignature(a:sigfile)
93 " restore cursor position from mark 'z' if the mark is still valid
100 "---------------------------------------------------------------------------
101 " Function: ReplaceAllSignatures()
102 " Purpose: Replace all signature blocks in the message
103 "---------------------------------------------------------------------------
104 func! ReplaceAllSignatures(sigfile)
105 if !SigFileReadable(a:sigfile)
109 " Save current cursor position in mark 'z'
113 " delete from the FIRST signature marker '^-- $' down to
114 " the end of the file
116 catch /^Vim\%((\a\+)\)\=:E486/ " catch error E486 (search command failed)
119 call AppendSignature(a:sigfile)
121 " restore cursor position from mark 'z' if the mark is still valid
128 "---------------------------------------------------------------------------
129 " Function: ReplaceLastSignature()
130 " Purpose: Replace only the last signature block in the message
131 "---------------------------------------------------------------------------
132 func! ReplaceLastSignature(sigfile)
133 if !SigFileReadable(a:sigfile)
137 " Save current cursor position in mark 'z'
141 " delete from the LAST signature marker '^-- $' down to
142 " the end of the file
144 catch /^Vim\%((\a\+)\)\=:E486/ " catch error E486 (search command failed)
147 call AppendSignature(a:sigfile)
149 " restore cursor position from mark 'z' if the mark is still valid