Version 0.1: Initial upload
[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 "---------------------------------------------------------------------------
35 " Function:     AppendSignature()
36 " Purpose:      Append a signature block at the end of message
37 "---------------------------------------------------------------------------
38 func! AppendSignature(sigfile)
39         " Add the signature marker at the end of the file
40         exe '$put =\"-- \"'
41
42         " Append the signature block file at the end of the file
43         exe '$r ' . fnameescape(a:sigfile)
44 endfunc
45
46
47 "---------------------------------------------------------------------------
48 " Function:     AddSignature()
49 " Purpose:      Add a signature block if there isn't one already
50 "---------------------------------------------------------------------------
51 func! AddSignature(sigfile)
52         " Save current cursor position in mark 'z'
53         normal mz
54
55         " Append a signature block only if there isn't one already
56         try
57                 exe '0/^-- $/'
58         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
59                 " put an extra newline
60                 exe '$put =\"\n\"'
61                 call AppendSignature(a:sigfile)
62         endtry
63
64         " restore cursor position from mark 'z' if the mark is still valid
65         silent! normal `z
66 endfunc
67
68
69 "---------------------------------------------------------------------------
70 " Function:     ReplaceAllSignatures()
71 " Purpose:      Replace all signature blocks in the message
72 "---------------------------------------------------------------------------
73 func! ReplaceAllSignatures(sigfile)
74         " Save current cursor position in mark 'z'
75         normal mz
76
77         try
78                 " delete from the FIRST signature marker '^-- $' down to
79                 " the end of the file
80                 exe '0/^-- $/,$d'
81         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
82         endtry
83
84         call AppendSignature(a:sigfile)
85         
86         " restore cursor position from mark 'z' if the mark is still valid
87         silent! normal `z
88 endfunc
89
90
91 "---------------------------------------------------------------------------
92 " Function:     ReplaceLastSignature()
93 " Purpose:      Replace only the last signature block in the message
94 "---------------------------------------------------------------------------
95 func! ReplaceLastSignature(sigfile)
96         " Save current cursor position in mark 'z'
97         normal mz
98
99         try
100                 " delete from the LAST signature marker '^-- $' down to
101                 " the end of the file
102                 exe '$?^-- $?,$d'
103         catch /^Vim\%((\a\+)\)\=:E486/  " catch error E486 (search command failed)
104         endtry
105
106         call AppendSignature(a:sigfile)
107         
108         " restore cursor position from mark 'z' if the mark is still valid
109         silent! normal `z
110 endfunc