editorhooks.py 3.9 KB

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