citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

lodgeit.vim (3882B)


      1 " lodgeit.vim: Vim plugin for paste.pocoo.org
      2 " Maintainer:   Armin Ronacher <armin.ronacher@active-4.com>
      3 " Version:      0.2
      4 
      5 " Usage:
      6 "   :Lodgeit    create a paste from the current buffer of selection
      7 "   :e <url>    download a paste. If you then use :Lodgeit you can
      8 "               reply to that paste.
      9 "
     10 " If you want to paste on ctrl + p just add this to your vimrc:
     11 " 	map ^P :Lodgeit<CR>
     12 " (where ^P is entered using ctrl + v, ctrl + p in vim)
     13 
     14 function! s:LodgeitInit()
     15 python << EOF
     16 
     17 import vim
     18 import re
     19 from xmlrpclib import ServerProxy
     20 srv = ServerProxy('http://paste.pocoo.org/xmlrpc/', allow_none=True)
     21 
     22 new_paste = srv.pastes.newPaste
     23 get_paste = srv.pastes.getPaste
     24 
     25 language_mapping = {
     26     'python':           'python',
     27     'php':              'html+php',
     28     'smarty':           'smarty',
     29     'tex':              'tex',
     30     'rst':              'rst',
     31     'cs':               'csharp',
     32     'haskell':          'haskell',
     33     'xml':              'xml',
     34     'html':             'html',
     35     'xhtml':            'html',
     36     'htmldjango':       'html+django',
     37     'django':           'html+django',
     38     'htmljinja':        'html+django',
     39     'jinja':            'html+django',
     40     'lua':              'lua',
     41     'scheme':           'scheme',
     42     'mako':             'html+mako',
     43     'c':                'c',
     44     'cpp':              'cpp',
     45     'javascript':       'js',
     46     'jsp':              'jsp',
     47     'ruby':             'ruby',
     48     'bash':             'bash',
     49     'bat':              'bat',
     50     'd':                'd',
     51     'genshi':           'html+genshi'
     52 }
     53 
     54 language_reverse_mapping = {}
     55 for key, value in language_mapping.iteritems():
     56     language_reverse_mapping[value] = key
     57 
     58 def paste_id_from_url(url):
     59     regex = re.compile(r'^http://paste.pocoo.org/show/([^/]+)/?$')
     60     m = regex.match(url)
     61     if m is not None:
     62         return m.group(1)
     63 
     64 def make_utf8(code):
     65     enc = vim.eval('&fenc') or vim.eval('&enc')
     66     return code.decode(enc, 'ignore').encode('utf-8')
     67 
     68 EOF
     69 endfunction
     70 
     71 
     72 function! s:Lodgeit(line1,line2,count,...)
     73 call s:LodgeitInit()
     74 python << endpython
     75 
     76 # download paste
     77 if vim.eval('a:0') == '1':
     78     paste = paste_id = None
     79     arg = vim.eval('a:1')
     80 
     81     if arg.startswith('#'):
     82         paste_id = arg[1:].split()[0]
     83     if paste_id is None:
     84         paste_id = paste_id_from_url(vim.eval('a:1'))
     85     if paste_id is not None:
     86         paste = get_paste(paste_id)
     87 
     88     if paste:
     89         vim.command('tabnew')
     90         vim.command('file Lodgeit\ Paste\ \#%s' % paste_id)
     91         vim.current.buffer[:] = paste['code'].splitlines()
     92         vim.command('setlocal ft=' + language_reverse_mapping.
     93                     get(paste['language'], 'text'))
     94         vim.command('setlocal nomodified')
     95         vim.command('let b:lodgeit_paste_id="%s"' % paste_id)
     96     else:
     97         print 'Paste not Found'
     98 
     99 # new paste or reply
    100 else:
    101     rng_start = int(vim.eval('a:line1')) - 1
    102     rng_end = int(vim.eval('a:line2'))
    103     if int(vim.eval('a:count')):
    104         code = '\n'.join(vim.current.buffer[rng_start:rng_end])
    105     else:
    106         code = '\n'.join(vim.current.buffer)
    107     code = make_utf8(code)
    108 
    109     parent = None
    110     update_buffer_info = False
    111     if vim.eval('exists("b:lodgeit_paste_id")') == '1':
    112         parent = int(vim.eval('b:lodgeit_paste_id'))
    113         update_buffer_info = True
    114 
    115     lng_code = language_mapping.get(vim.eval('&ft'), 'text')
    116     paste_id = new_paste(lng_code, code, parent)
    117     url = 'http://paste.pocoo.org/show/%s' % paste_id
    118 
    119     print 'Pasted #%s to %s' % (paste_id, url)
    120     vim.command(':call setreg(\'+\', %r)' % url)
    121 
    122     if update_buffer_info:
    123         vim.command('file Lodgeit\ Paste\ \#%s' % paste_id)
    124         vim.command('setlocal nomodified')
    125         vim.command('let b:lodgeit_paste_id="%s"' % paste_id)
    126 
    127 endpython
    128 endfunction
    129 
    130 
    131 command! -range=0 -nargs=* Lodgeit :call s:Lodgeit(<line1>,<line2>,<count>,<f-args>)