Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 1.93 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. def getvar(varname, default):
  21.   if int(vim.eval('exists("' + varname + '")')):
  22.     return vim.eval(varname)
  23.   elif default:
  24.     return default
  25.   else:
  26.     raise Exception('The setting ' + varname + ' is required')
  27.  
  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. def publish(site=None, *args):
  34.   import urllib, imp, vim
  35.  
  36.   if not site:
  37.     site = getglobalsetting('default_site', 'pastebin')
  38.   getsetting.sitename = site
  39.  
  40.   fn = vim.eval('findfile("publishers/' + site + '.py", &rtp)')
  41.   if not fn:
  42.     raise Exception('no publisher exists for ' + site)
  43.  
  44.   with open(fn, 'r') as f:
  45.     mod = imp.load_module('vim_publisher_' + site, f, fn, ('.py', 'U', 1))
  46.     mod.getglobalsetting = getglobalsetting
  47.     mod.getsetting = getsetting
  48.  
  49.   data = {
  50.     'code': '\n'.join(vim.current.range),
  51.     'lang': vim.eval('&filetype'),
  52.   }
  53.  
  54.   if hasattr(mod, 'publish'):
  55.     response = mod.publish(data, *args)
  56.  
  57.   elif hasattr(mod, 'process'):
  58.     data = mod.process(data, *args)
  59.     response = urllib.urlopen(mod.url, urllib.urlencode(data))
  60.  
  61.   else:
  62.     raise Exception('The publisher for ' + site + ' seems to be broken')
  63.  
  64.   print(dir(response))
  65.   print('Published to ' + response.read())
  66. EOF
  67. endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement