async.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. if __name__ == "__main__":
  3. import sys
  4. from distutils.spawn import find_executable
  5. import re
  6. import getopt
  7. from subprocess import Popen
  8. opts = dict(getopt.getopt(sys.argv[1:3], "", ["servername=", "open", "noopen"])[0])
  9. servername = opts["--servername"]
  10. should_open = '1' if "--open" in opts else '0'
  11. # run the command
  12. with open("pandoc.out", 'w') as tmp:
  13. com = Popen(sys.argv[3:], stdout=tmp, stderr=tmp)
  14. com.wait()
  15. returncode = str(com.returncode)
  16. # once it's done, we call back the server that called us
  17. # to notify pandoc's execution
  18. func_call = "pandoc#command#PandocAsyncCallback("+should_open+","+returncode+")"
  19. if find_executable('gvim') not in ('', None):
  20. command = [find_executable('gvim')]
  21. elif find_executable('mvim') not in ('', None):
  22. command = [find_executable('mvim')]
  23. elif find_executable('vim') not in ('', None):
  24. command = [find_executable('vim')]
  25. else:
  26. cmd = re.match('[gm]?vim', servername.lower())
  27. if cmd:
  28. command = cmd.group()
  29. else:
  30. sys.exit()
  31. command.extend(["--servername", servername])
  32. # windows requires the callback name to be sent instead of being eval'ed,
  33. # for some reason. note this is more fragile.
  34. if sys.platform.startswith("win"):
  35. command.extend(["--remote-send", "<ESC>:call " + func_call + "<CR>"])
  36. else:
  37. command.extend(["--remote-expr", func_call])
  38. Popen(command)