2c09fb74401ea9c36620df3660dce5d87d41fffd
[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 if AddSignature('~/.signature') | w | endif
19 "
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
23 "
24 " References:
25 " http://en.wikipedia.org/wiki/Signature_block
26 " http://tools.ietf.org/html/rfc1855
27 "
28 " The latest version of this script is available at these locations:
29 " https://git.ao2.it/vim/signature_block.vim.git
30 " http://www.vim.org/scripts/script.php?script_id=2872
31 " https://github.com/vim-scripts/signature_block.vim
32 "
33 "---------------------------------------------------------------------------
34
35 if exists("g:loaded_signaturePlugin") | finish | endif
36 let g:loaded_signaturePlugin = 1
37
38
39 " Function:     SigFileReadable()
40 " Purpose:      Check if the signature file is readable
41 "---------------------------------------------------------------------------
42 func! SigFileReadable(sigfile)
43         let filename = expand(a:sigfile)
44         if !filereadable(filename)
45                 echoerr "E484: Can't open file " . filename
46                 return v:false
47         endif
48
49         return v:true
50 endfunc
51
52 "---------------------------------------------------------------------------
53 " Function:     AppendSignature()
54 " Purpose:      Append a signature block at the end of message
55 "---------------------------------------------------------------------------
56 func! AppendSignature(sigfile)
57         " Add the signature marker at the end of the file
58         exe '$put =\"-- \"'
59
60         " Append the signature block file at the end of the file
61         exe '$r ' . fnameescape(a:sigfile)
62 endfunc
63
64
65 "---------------------------------------------------------------------------
66 " Function:     AddSignature()
67 " Purpose:      Add a signature block if there isn't one already
68 "---------------------------------------------------------------------------
69 func! AddSignature(sigfile)
70         if !SigFileReadable(a:sigfile)
71                 return v:false
72         endif
73
74         " Save current cursor position in mark 'z'
75         normal mz
76
77         " Append a signature block only if there isn't one already
78         try
79                 exe '0/^-- $/'
80         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
81                 " put an extra newline
82                 exe '$put =\"\n\"'
83                 call AppendSignature(a:sigfile)
84         endtry
85
86         " restore cursor position from mark 'z' if the mark is still valid
87         silent! normal `z
88
89         return v:true
90 endfunc
91
92
93 "---------------------------------------------------------------------------
94 " Function:     ReplaceAllSignatures()
95 " Purpose:      Replace all signature blocks in the message
96 "---------------------------------------------------------------------------
97 func! ReplaceAllSignatures(sigfile)
98         if !SigFileReadable(a:sigfile)
99                 return v:false
100         endif
101
102         " Save current cursor position in mark 'z'
103         normal mz
104
105         try
106                 " delete from the FIRST signature marker '^-- $' down to
107                 " the end of the file
108                 exe '0/^-- $/,$d'
109         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
110         endtry
111
112         call AppendSignature(a:sigfile)
113
114         " restore cursor position from mark 'z' if the mark is still valid
115         silent! normal `z
116
117         return v:true
118 endfunc
119
120
121 "---------------------------------------------------------------------------
122 " Function:     ReplaceLastSignature()
123 " Purpose:      Replace only the last signature block in the message
124 "---------------------------------------------------------------------------
125 func! ReplaceLastSignature(sigfile)
126         if !SigFileReadable(a:sigfile)
127                 return v:false
128         endif
129
130         " Save current cursor position in mark 'z'
131         normal mz
132
133         try
134                 " delete from the LAST signature marker '^-- $' down to
135                 " the end of the file
136                 exe '$?^-- $?,$d'
137         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
138         endtry
139
140         call AppendSignature(a:sigfile)
141
142         " restore cursor position from mark 'z' if the mark is still valid
143         silent! normal `z
144
145         return v:true
146 endfunc