summaryrefslogtreecommitdiff
path: root/.config/vim/plugin/lodgeit.vim
blob: 4053b0b4adda84702a60fcb33ca407631a318833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
" lodgeit.vim: Vim plugin for paste.pocoo.org
" Maintainer:   Armin Ronacher <armin.ronacher@active-4.com>
" Version:      0.1.5

" Usage:
"   :Lodgeit    create a paste from the current buffer of selection
"   :e <url>    download a paste. If you then use :Lodgeit you can
"               reply to that paste.
"
" If you want to paste on ctrl + p just add this to your vimrc:
" 	map ^P :Lodgeit<CR>
" (where ^P is entered using ctrl + v, ctrl + p in vim)

function! s:LodgeitInit()
python << EOF

import vim
import re
from xmlrpclib import ServerProxy
srv = ServerProxy('http://paste.pocoo.org/xmlrpc/', allow_none=True)

new_paste = srv.pastes.newPaste
get_paste = srv.pastes.getPaste

language_mapping = {
    'python':           'python',
    'php':              'html+php',
    'smarty':           'smarty',
    'tex':              'tex',
    'rst':              'rst',
    'cs':               'csharp',
    'haskell':          'haskell',
    'xml':              'xml',
    'html':             'html',
    'xhtml':            'html',
    'htmldjango':       'html+django',
    'django':           'html+django',
    'htmljinja':        'html+django',
    'jinja':            'html+django',
    'lua':              'lua',
    'scheme':           'scheme',
    'mako':             'html+mako',
    'c':                'c',
    'cpp':              'cpp',
    'javascript':       'js',
    'jsp':              'jsp',
    'ruby':             'ruby',
    'bash':             'bash',
    'bat':              'bat',
    'd':                'd',
    'genshi':           'html+genshi'
}

language_reverse_mapping = {}
for key, value in language_mapping.iteritems():
    language_reverse_mapping[value] = key

def paste_id_from_url(url):
    regex = re.compile(r'^http://paste.pocoo.org/show/(\d+)/?$')
    m = regex.match(url)
    if m is not None:
        return int(m.group(1))

def make_utf8(code):
    enc = vim.eval('&fenc') or vim.eval('&enc')
    return code.decode(enc, 'ignore').encode('utf-8')

EOF
endfunction


function! s:Lodgeit(line1,line2,count,...)
call s:LodgeitInit()
python << endpython

# download paste
if vim.eval('a:0') == '1':
    paste = paste_id = None
    arg = vim.eval('a:1')

    if arg.startswith('#'):
        try:
            paste_id = int(arg[1:])
        except:
            pass
    if paste_id is None:
        paste_id = paste_id_from_url(vim.eval('a:1'))
    if paste_id is not None:
        paste = get_paste(paste_id)

    if paste:
        vim.command('tabnew')
        vim.command('file Lodgeit\ Paste\ \#%d"' % paste_id)
        vim.current.buffer[:] = paste['code'].splitlines()
        vim.command('setlocal ft=' + language_reverse_mapping.
                    get(paste['language'], 'text'))
        vim.command('setlocal nomodified')
        vim.command('let b:lodgeit_paste_id=%d' % paste_id)
    else:
        print 'Paste not Found'

# new paste or reply
else:
    rng_start = int(vim.eval('a:line1')) - 1
    rng_end = int(vim.eval('a:line2'))
    if int(vim.eval('a:count')):
        code = '\n'.join(vim.current.buffer[rng_start:rng_end])
    else:
        code = '\n'.join(vim.current.buffer)
    code = make_utf8(code)

    parent = None
    update_buffer_info = False
    if vim.eval('exists("b:lodgeit_paste_id")') == '1':
        parent = int(vim.eval('b:lodgeit_paste_id'))
        update_buffer_info = True

    lng_code = language_mapping.get(vim.eval('&ft'), 'text')
    paste_id = new_paste(lng_code, code, parent)
    url = 'http://paste.pocoo.org/show/%d' % paste_id

    print 'Pasted #%d to %s' % (paste_id, url)
    vim.command(':call setreg(\'+\', %r)' % url)

    if update_buffer_info:
        vim.command('file Lodgeit\ Paste\ \#%d"' % paste_id)
        vim.command('setlocal nomodified')
        vim.command('let b:lodgeit_paste_id=%d' % paste_id)

endpython
endfunction


command! -range=0 -nargs=* Lodgeit :call s:Lodgeit(<line1>,<line2>,<count>,<f-args>)