editorhooks.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """ 'editor' hooks for common editors that work well with ipython
  2. They should honor the line number argument, at least.
  3. Contributions are *very* welcome.
  4. """
  5. import os
  6. import shlex
  7. import subprocess
  8. import sys
  9. from IPython import get_ipython
  10. from IPython.core.error import TryNext
  11. from IPython.utils import py3compat
  12. def install_editor(template, wait=False):
  13. """Installs the editor that is called by IPython for the %edit magic.
  14. This overrides the default editor, which is generally set by your EDITOR
  15. environment variable or is notepad (windows) or vi (linux). By supplying a
  16. template string `run_template`, you can control how the editor is invoked
  17. by IPython -- (e.g. the format in which it accepts command line options)
  18. Parameters
  19. ----------
  20. template : basestring
  21. run_template acts as a template for how your editor is invoked by
  22. the shell. It should contain '{filename}', which will be replaced on
  23. invocation with the file name, and '{line}', $line by line number
  24. (or 0) to invoke the file with.
  25. wait : bool
  26. If `wait` is true, wait until the user presses enter before returning,
  27. to facilitate non-blocking editors that exit immediately after
  28. the call.
  29. """
  30. # not all editors support $line, so we'll leave out this check
  31. # for substitution in ['$file', '$line']:
  32. # if not substitution in run_template:
  33. # raise ValueError(('run_template should contain %s'
  34. # ' for string substitution. You supplied "%s"' % (substitution,
  35. # run_template)))
  36. def call_editor(self, filename, line=0):
  37. if line is None:
  38. line = 0
  39. cmd = template.format(filename=shlex.quote(filename), line=line)
  40. print(">", cmd)
  41. # shlex.quote doesn't work right on Windows, but it does after splitting
  42. if sys.platform.startswith('win'):
  43. cmd = shlex.split(cmd)
  44. proc = subprocess.Popen(cmd, shell=True)
  45. if proc.wait() != 0:
  46. raise TryNext()
  47. if wait:
  48. py3compat.input("Press Enter when done editing:")
  49. get_ipython().set_hook('editor', call_editor)
  50. get_ipython().editor = template
  51. # in these, exe is always the path/name of the executable. Useful
  52. # if you don't have the editor directory in your path
  53. def komodo(exe=u'komodo'):
  54. """ Activestate Komodo [Edit] """
  55. install_editor(exe + u' -l {line} {filename}', wait=True)
  56. def scite(exe=u"scite"):
  57. """ SciTE or Sc1 """
  58. install_editor(exe + u' {filename} -goto:{line}')
  59. def notepadplusplus(exe=u'notepad++'):
  60. """ Notepad++ http://notepad-plus.sourceforge.net """
  61. install_editor(exe + u' -n{line} {filename}')
  62. def jed(exe=u'jed'):
  63. """ JED, the lightweight emacsish editor """
  64. install_editor(exe + u' +{line} {filename}')
  65. def idle(exe=u'idle'):
  66. """ Idle, the editor bundled with python
  67. Parameters
  68. ----------
  69. exe : str, None
  70. If none, should be pretty smart about finding the executable.
  71. """
  72. if exe is None:
  73. import idlelib
  74. p = os.path.dirname(idlelib.__filename__)
  75. # i'm not sure if this actually works. Is this idle.py script
  76. # guaranteed to be executable?
  77. exe = os.path.join(p, 'idle.py')
  78. install_editor(exe + u' {filename}')
  79. def mate(exe=u'mate'):
  80. """ TextMate, the missing editor"""
  81. # wait=True is not required since we're using the -w flag to mate
  82. install_editor(exe + u' -w -l {line} {filename}')
  83. # ##########################################
  84. # these are untested, report any problems
  85. # ##########################################
  86. def emacs(exe=u'emacs'):
  87. install_editor(exe + u' +{line} {filename}')
  88. def gnuclient(exe=u'gnuclient'):
  89. install_editor(exe + u' -nw +{line} {filename}')
  90. def crimson_editor(exe=u'cedt.exe'):
  91. install_editor(exe + u' /L:{line} {filename}')
  92. def kate(exe=u'kate'):
  93. install_editor(exe + u' -u -l {line} {filename}')