editorhooks.py
129 lines
| 3.9 KiB
| text/x-python
|
PythonLexer
|
r851 | """ 'editor' hooks for common editors that work well with ipython | ||
They should honor the line number argument, at least. | ||||
Contributions are *very* welcome. | ||||
""" | ||||
|
r13348 | from __future__ import print_function | ||
|
r851 | |||
import os | ||||
|
r9660 | import pipes | ||
|
r18836 | import shlex | ||
|
r9660 | import subprocess | ||
|
r18869 | import sys | ||
|
r10580 | |||
from IPython import get_ipython | ||||
|
r9660 | from IPython.core.error import TryNext | ||
|
r13355 | from IPython.utils import py3compat | ||
|
r851 | |||
|
r8819 | |||
|
r18869 | def install_editor(template, wait=False): | ||
|
r8819 | """Installs the editor that is called by IPython for the %edit magic. | ||
This overrides the default editor, which is generally set by your EDITOR | ||||
environment variable or is notepad (windows) or vi (linux). By supplying a | ||||
template string `run_template`, you can control how the editor is invoked | ||||
by IPython -- (e.g. the format in which it accepts command line options) | ||||
Parameters | ||||
---------- | ||||
|
r8880 | template : basestring | ||
|
r8819 | run_template acts as a template for how your editor is invoked by | ||
|
r8891 | the shell. It should contain '{filename}', which will be replaced on | ||
|
r8880 | invokation with the file name, and '{line}', $line by line number | ||
|
r8819 | (or 0) to invoke the file with. | ||
wait : bool | ||||
If `wait` is true, wait until the user presses enter before returning, | ||||
to facilitate non-blocking editors that exit immediately after | ||||
the call. | ||||
|
r851 | """ | ||
|
r8819 | |||
|
r8820 | # not all editors support $line, so we'll leave out this check | ||
# for substitution in ['$file', '$line']: | ||||
# if not substitution in run_template: | ||||
# raise ValueError(('run_template should contain %s' | ||||
# ' for string substitution. You supplied "%s"' % (substitution, | ||||
# run_template))) | ||||
|
r8819 | |||
|
r8891 | def call_editor(self, filename, line=0): | ||
|
r851 | if line is None: | ||
line = 0 | ||||
|
r9660 | cmd = template.format(filename=pipes.quote(filename), line=line) | ||
|
r13348 | print(">", cmd) | ||
|
r18836 | # pipes.quote doesn't work right on Windows, but it does after splitting | ||
if sys.platform.startswith('win'): | ||||
cmd = shlex.split(cmd) | ||||
|
r8891 | proc = subprocess.Popen(cmd, shell=True) | ||
|
r23301 | if proc.wait() != 0: | ||
|
r2205 | raise TryNext() | ||
|
r851 | if wait: | ||
|
r13355 | py3compat.input("Press Enter when done editing:") | ||
|
r851 | |||
|
r8819 | get_ipython().set_hook('editor', call_editor) | ||
|
r8891 | get_ipython().editor = template | ||
|
r851 | |||
# in these, exe is always the path/name of the executable. Useful | ||||
# if you don't have the editor directory in your path | ||||
|
r8891 | def komodo(exe=u'komodo'): | ||
|
r851 | """ Activestate Komodo [Edit] """ | ||
|
r9660 | install_editor(exe + u' -l {line} {filename}', wait=True) | ||
|
r8819 | |||
|
r851 | |||
|
r8891 | def scite(exe=u"scite"): | ||
|
r851 | """ SciTE or Sc1 """ | ||
|
r9660 | install_editor(exe + u' {filename} -goto:{line}') | ||
|
r851 | |||
|
r8819 | |||
|
r8891 | def notepadplusplus(exe=u'notepad++'): | ||
|
r851 | """ Notepad++ http://notepad-plus.sourceforge.net """ | ||
|
r9660 | install_editor(exe + u' -n{line} {filename}') | ||
|
r8819 | |||
|
r8891 | def jed(exe=u'jed'): | ||
|
r851 | """ JED, the lightweight emacsish editor """ | ||
|
r9660 | install_editor(exe + u' +{line} {filename}') | ||
|
r851 | |||
|
r8819 | |||
|
r8891 | def idle(exe=u'idle'): | ||
|
r851 | """ Idle, the editor bundled with python | ||
|
r8819 | |||
|
r8820 | Parameters | ||
---------- | ||||
exe : str, None | ||||
If none, should be pretty smart about finding the executable. | ||||
|
r851 | """ | ||
if exe is None: | ||||
import idlelib | ||||
|
r8891 | p = os.path.dirname(idlelib.__filename__) | ||
|
r9660 | # i'm not sure if this actually works. Is this idle.py script | ||
# guarenteed to be executable? | ||||
|
r8819 | exe = os.path.join(p, 'idle.py') | ||
|
r9660 | install_editor(exe + u' {filename}') | ||
|
r1782 | |||
|
r8819 | |||
|
r8891 | def mate(exe=u'mate'): | ||
|
r1782 | """ TextMate, the missing editor""" | ||
|
r8819 | # wait=True is not required since we're using the -w flag to mate | ||
|
r9660 | install_editor(exe + u' -w -l {line} {filename}') | ||
|
r851 | |||
|
r8819 | |||
# ########################################## | ||||
|
r851 | # these are untested, report any problems | ||
|
r8819 | # ########################################## | ||
|
r851 | |||
|
r8891 | def emacs(exe=u'emacs'): | ||
|
r9660 | install_editor(exe + u' +{line} {filename}') | ||
|
r851 | |||
|
r8819 | |||
|
r8891 | def gnuclient(exe=u'gnuclient'): | ||
|
r9660 | install_editor(exe + u' -nw +{line} {filename}') | ||
|
r851 | |||
|
r8819 | |||
|
r8891 | def crimson_editor(exe=u'cedt.exe'): | ||
|
r9660 | install_editor(exe + u' /L:{line} {filename}') | ||
|
r8819 | |||
|
r8891 | def kate(exe=u'kate'): | ||
|
r9660 | install_editor(exe + u' -u -l {line} {filename}') | ||