Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 2.70 KB | None | 0 0
  1. " Version:      1.0
  2. " Last Change:  Sat, 14 May 2011 14:38:50 +0200
  3. " Maintainer:   Michon van Dooren <michon1992+vim+publish  gmail  com>
  4. " Description:  Vim plugin that allows you to paste/publish pieces of code to various sites.
  5. " Requires:     +python
  6. " Commands:
  7. " :Publish <site> <arguments>
  8. "     Site can be any valid publisher.
  9. "     Arguments are site-specific.
  10.  
  11.  
  12. if has('python')
  13.   command! -nargs=* -range=% Publish execute ':<line1>,<line2>python publish(<f-args>)'
  14. else
  15.   command! -nargs=* -range=% Publish echo 'Only avaliable with +python support.'
  16. endif
  17.  
  18. if has('python')
  19. python << EOF
  20. # Functions to get settings.
  21. def getvar(varname, default):
  22.   if int(vim.eval('exists("' + varname + '")')):
  23.     return vim.eval(varname)
  24.   elif default != None:
  25.     return default
  26.   else:
  27.     raise Exception('The setting ' + varname + ' is required')
  28. def getglobalsetting(settingname, default = None):
  29.   return getvar('g:Publish_' + settingname, default)
  30. def getsetting(settingname, default = None):
  31.   return getvar('g:Publish_' + getsetting.sitename + '_' + settingname, default)
  32.  
  33. # Call a function, with an optional pre and post hook.
  34. def call(mod, fnname, data, mid = None, pre = None, post = None):
  35.   pre =  getattr(mod, fnname + '_pre', pre)
  36.   mid =  getattr(mod, fnname, mid or makefn(getattr(mod, fnname + '_url')))
  37.   post = getattr(mod, fnname + '_post', post)
  38.  
  39.   if data and pre:
  40.     data = pre(*data)
  41.   elif pre:
  42.     data = pre()
  43.     if not data:
  44.       return
  45.   elif data:
  46.     data = data[0]
  47.  
  48.   data = mid(data)
  49.  
  50.   if post:
  51.     return post(data)
  52.   else:
  53.     return data
  54.  
  55. # Make a simple callback function for basic publishing/login.
  56. def makefn(url):
  57.   def _(data):
  58.     import urllib
  59.     return urllib.urlopen(url, urllib.urlencode(data)).read()
  60.   return _
  61.  
  62. # The actual publishing part.
  63. def publish(site=None, *args):
  64.   import imp, vim
  65.  
  66.   if not site:
  67.     site = getglobalsetting('default_site', 'pastebin')
  68.   getsetting.sitename = site
  69.  
  70.   fn = vim.eval('findfile("publishers/' + site + '.py", &rtp)')
  71.   if not fn:
  72.     raise Exception('no publisher exists for ' + site)
  73.  
  74.   with open(fn, 'r') as f:
  75.     mod = imp.load_module('vim_publisher_' + site, f, fn, ('.py', 'U', 1))
  76.     mod.getglobalsetting = getglobalsetting
  77.     mod.getsetting = getsetting
  78.  
  79.   # Login, if needed/possible
  80.   if not getsetting('logindata', 0) and (hasattr(mod, 'login_url') or hasattr(mod, 'login')):
  81.     data = call(mod, 'login', None)
  82.     if data:
  83.       vim.eval('let g:Publish_' + site + '_logindata = "' + data + '"')
  84.  
  85.   # Build a default data object.
  86.   data = {
  87.     'code': '\n'.join(vim.current.range),
  88.     'lang': vim.eval('&filetype'),
  89.   }
  90.  
  91.   # Publish.
  92.   args = (data,) + args
  93.   response = call(mod, 'publish', args)
  94.  
  95.   print('Published to ' + response)
  96. EOF
  97. endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement