Release version 0.2
[vim/signature_block.vim.git] / plugin / signature_block.vim
1 "---------------------------------------------------------------------------
2 " Vim global plugin for adding and manipulating signature blocks in e-mails
3 " Maintainer:  Antonio Ospite <ao2@ao2.it>
4 " Version:     0.2
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.
8 "
9 " History:
10 "   1.0: Initial upload
11 "   2.0:
12 "     - Check that the signature file is readable before removing the old
13 "       signature and append the new one.
14 "     - Minor fixes.
15 "
16 " Install Details:
17 " Drop this file into your $HOME/.vim/plugin directory.
18 "
19 " Examples:
20 " map <Leader>s :call AppendSignature('~/.signature')<CR>
21 " map <Leader>r :call ReplaceLastSignature('~/.signature')<CR>
22 " map <Leader>R :call ReplaceAllSignatures('~/.signature')<CR>
23 "
24 " " Append a signature block to all e-mails
25 " autocmd FileType mail if AddSignature('~/.signature') | w | endif
26 "
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
30 "
31 " References:
32 " http://en.wikipedia.org/wiki/Signature_block
33 " http://tools.ietf.org/html/rfc1855
34 "
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
39 "
40 "---------------------------------------------------------------------------
41
42 if exists("g:loaded_signaturePlugin") | finish | endif
43 let g:loaded_signaturePlugin = 1
44
45
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
53                 return v:false
54         endif
55
56         return v:true
57 endfunc
58
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
65         exe '$put =\"-- \"'
66
67         " Append the signature block file at the end of the file
68         exe '$r ' . fnameescape(a:sigfile)
69 endfunc
70
71
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)
78                 return v:false
79         endif
80
81         " Save current cursor position in mark 'z'
82         normal mz
83
84         " Append a signature block only if there isn't one already
85         try
86                 exe '0/^-- $/'
87         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
88                 " put an extra newline
89                 exe '$put =\"\n\"'
90                 call AppendSignature(a:sigfile)
91         endtry
92
93         " restore cursor position from mark 'z' if the mark is still valid
94         silent! normal `z
95
96         return v:true
97 endfunc
98
99
100 "---------------------------------------------------------------------------
101 " Function:     ReplaceAllSignatures()
102 " Purpose:      Replace all signature blocks in the message
103 "---------------------------------------------------------------------------
104 func! ReplaceAllSignatures(sigfile)
105         if !SigFileReadable(a:sigfile)
106                 return v:false
107         endif
108
109         " Save current cursor position in mark 'z'
110         normal mz
111
112         try
113                 " delete from the FIRST signature marker '^-- $' down to
114                 " the end of the file
115                 exe '0/^-- $/,$d'
116         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
117         endtry
118
119         call AppendSignature(a:sigfile)
120
121         " restore cursor position from mark 'z' if the mark is still valid
122         silent! normal `z
123
124         return v:true
125 endfunc
126
127
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)
134                 return v:false
135         endif
136
137         " Save current cursor position in mark 'z'
138         normal mz
139
140         try
141                 " delete from the LAST signature marker '^-- $' down to
142                 " the end of the file
143                 exe '$?^-- $?,$d'
144         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
145         endtry
146
147         call AppendSignature(a:sigfile)
148
149         " restore cursor position from mark 'z' if the mark is still valid
150         silent! normal `z
151
152         return v:true
153 endfunc