signature_block.vim: check that the signature file is readable
[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 <ospite@studenti.unina.it>
4 " Version:     0.1
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.
8 "
9 " Install Details:
10 " Drop this file into your $HOME/.vim/plugin directory.
11 "
12 " Examples:
13 " map <Leader>s :call AppendSignature('~/.signature')<CR>
14 " map <Leader>r :call ReplaceLastSignature('~/.signature')<CR>
15 " map <Leader>R :call ReplaceAllSignatures('~/.signature')<CR>
16 "
17 " " Append a signature block to all e-mails
18 " autocmd FileType mail silent call AddSignature('~/.signature') | w
19 "
20 " " Append a signature block to cover letters generated with git-format-patch
21 " autocmd BufRead 0000-cover-letter.patch silent call AddSignature('~/.signature') | w
22 " autocmd BufRead 0000-cover-letter.patch autocmd! BufRead 0000-cover-letter.patch
23 "
24 " References:
25 " http://en.wikipedia.org/wiki/Signature_block
26 " http://tools.ietf.org/html/rfc1855
27 "
28 "---------------------------------------------------------------------------
29
30 if exists("g:loaded_signaturePlugin") | finish | endif
31 let g:loaded_signaturePlugin = 1
32
33
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
41                 return v:false
42         endif
43
44         return v:true
45 endfunc
46
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
53         exe '$put =\"-- \"'
54
55         " Append the signature block file at the end of the file
56         exe '$r ' . fnameescape(a:sigfile)
57 endfunc
58
59
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)
66                 return v:false
67         endif
68
69         " Save current cursor position in mark 'z'
70         normal mz
71
72         " Append a signature block only if there isn't one already
73         try
74                 exe '0/^-- $/'
75         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
76                 " put an extra newline
77                 exe '$put =\"\n\"'
78                 call AppendSignature(a:sigfile)
79         endtry
80
81         " restore cursor position from mark 'z' if the mark is still valid
82         silent! normal `z
83
84         return v:true
85 endfunc
86
87
88 "---------------------------------------------------------------------------
89 " Function:     ReplaceAllSignatures()
90 " Purpose:      Replace all signature blocks in the message
91 "---------------------------------------------------------------------------
92 func! ReplaceAllSignatures(sigfile)
93         if !SigFileReadable(a:sigfile)
94                 return v:false
95         endif
96
97         " Save current cursor position in mark 'z'
98         normal mz
99
100         try
101                 " delete from the FIRST signature marker '^-- $' down to
102                 " the end of the file
103                 exe '0/^-- $/,$d'
104         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
105         endtry
106
107         call AppendSignature(a:sigfile)
108         
109         " restore cursor position from mark 'z' if the mark is still valid
110         silent! normal `z
111
112         return v:true
113 endfunc
114
115
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)
122                 return v:false
123         endif
124
125         " Save current cursor position in mark 'z'
126         normal mz
127
128         try
129                 " delete from the LAST signature marker '^-- $' down to
130                 " the end of the file
131                 exe '$?^-- $?,$d'
132         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
133         endtry
134
135         call AppendSignature(a:sigfile)
136         
137         " restore cursor position from mark 'z' if the mark is still valid
138         silent! normal `z
139
140         return v:true
141 endfunc