editorhooks.py
129 lines
| 3.9 KiB
| text/x-python
|
PythonLexer
vivainio
|
r851 | """ 'editor' hooks for common editors that work well with ipython | ||
They should honor the line number argument, at least. | ||||
Contributions are *very* welcome. | ||||
""" | ||||
Thomas Kluyver
|
r13348 | from __future__ import print_function | ||
vivainio
|
r851 | |||
import os | ||||
Robert McGibbon
|
r9660 | import pipes | ||
Min RK
|
r18836 | import shlex | ||
Robert McGibbon
|
r9660 | import subprocess | ||
Min RK
|
r18869 | import sys | ||
MinRK
|
r10580 | |||
from IPython import get_ipython | ||||
Robert McGibbon
|
r9660 | from IPython.core.error import TryNext | ||
Thomas Kluyver
|
r13355 | from IPython.utils import py3compat | ||
vivainio
|
r851 | |||
Robert McGibbon
|
r8819 | |||
Min RK
|
r18869 | def install_editor(template, wait=False): | ||
Robert McGibbon
|
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 | ||||
---------- | ||||
Robert McGibbon
|
r8880 | template : basestring | ||
Robert McGibbon
|
r8819 | run_template acts as a template for how your editor is invoked by | ||
Robert McGibbon
|
r8891 | the shell. It should contain '{filename}', which will be replaced on | ||
Robert McGibbon
|
r8880 | invokation with the file name, and '{line}', $line by line number | ||
Robert McGibbon
|
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. | ||||
vivainio
|
r851 | """ | ||
Robert McGibbon
|
r8819 | |||
Robert McGibbon
|
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))) | ||||
Robert McGibbon
|
r8819 | |||
Robert McGibbon
|
r8891 | def call_editor(self, filename, line=0): | ||
vivainio
|
r851 | if line is None: | ||
line = 0 | ||||
Robert McGibbon
|
r9660 | cmd = template.format(filename=pipes.quote(filename), line=line) | ||
Thomas Kluyver
|
r13348 | print(">", cmd) | ||
Min RK
|
r18836 | # pipes.quote doesn't work right on Windows, but it does after splitting | ||
if sys.platform.startswith('win'): | ||||
cmd = shlex.split(cmd) | ||||
Robert McGibbon
|
r8891 | proc = subprocess.Popen(cmd, shell=True) | ||
if wait and proc.wait() != 0: | ||||
Brian Granger
|
r2205 | raise TryNext() | ||
vivainio
|
r851 | if wait: | ||
Thomas Kluyver
|
r13355 | py3compat.input("Press Enter when done editing:") | ||
vivainio
|
r851 | |||
Robert McGibbon
|
r8819 | get_ipython().set_hook('editor', call_editor) | ||
Robert McGibbon
|
r8891 | get_ipython().editor = template | ||
vivainio
|
r851 | |||
# in these, exe is always the path/name of the executable. Useful | ||||
# if you don't have the editor directory in your path | ||||
Robert McGibbon
|
r8891 | def komodo(exe=u'komodo'): | ||
vivainio
|
r851 | """ Activestate Komodo [Edit] """ | ||
Robert McGibbon
|
r9660 | install_editor(exe + u' -l {line} {filename}', wait=True) | ||
Robert McGibbon
|
r8819 | |||
vivainio
|
r851 | |||
Robert McGibbon
|
r8891 | def scite(exe=u"scite"): | ||
vivainio
|
r851 | """ SciTE or Sc1 """ | ||
Robert McGibbon
|
r9660 | install_editor(exe + u' {filename} -goto:{line}') | ||
vivainio
|
r851 | |||
Robert McGibbon
|
r8819 | |||
Robert McGibbon
|
r8891 | def notepadplusplus(exe=u'notepad++'): | ||
vivainio
|
r851 | """ Notepad++ http://notepad-plus.sourceforge.net """ | ||
Robert McGibbon
|
r9660 | install_editor(exe + u' -n{line} {filename}') | ||
Robert McGibbon
|
r8819 | |||
Robert McGibbon
|
r8891 | def jed(exe=u'jed'): | ||
vivainio
|
r851 | """ JED, the lightweight emacsish editor """ | ||
Robert McGibbon
|
r9660 | install_editor(exe + u' +{line} {filename}') | ||
vivainio
|
r851 | |||
Robert McGibbon
|
r8819 | |||
Robert McGibbon
|
r8891 | def idle(exe=u'idle'): | ||
vivainio
|
r851 | """ Idle, the editor bundled with python | ||
Robert McGibbon
|
r8819 | |||
Robert McGibbon
|
r8820 | Parameters | ||
---------- | ||||
exe : str, None | ||||
If none, should be pretty smart about finding the executable. | ||||
vivainio
|
r851 | """ | ||
if exe is None: | ||||
import idlelib | ||||
Robert McGibbon
|
r8891 | p = os.path.dirname(idlelib.__filename__) | ||
Robert McGibbon
|
r9660 | # i'm not sure if this actually works. Is this idle.py script | ||
# guarenteed to be executable? | ||||
Robert McGibbon
|
r8819 | exe = os.path.join(p, 'idle.py') | ||
Robert McGibbon
|
r9660 | install_editor(exe + u' {filename}') | ||
Brian Granger
|
r1782 | |||
Robert McGibbon
|
r8819 | |||
Robert McGibbon
|
r8891 | def mate(exe=u'mate'): | ||
Brian Granger
|
r1782 | """ TextMate, the missing editor""" | ||
Robert McGibbon
|
r8819 | # wait=True is not required since we're using the -w flag to mate | ||
Robert McGibbon
|
r9660 | install_editor(exe + u' -w -l {line} {filename}') | ||
vivainio
|
r851 | |||
Robert McGibbon
|
r8819 | |||
# ########################################## | ||||
vivainio
|
r851 | # these are untested, report any problems | ||
Robert McGibbon
|
r8819 | # ########################################## | ||
vivainio
|
r851 | |||
Robert McGibbon
|
r8891 | def emacs(exe=u'emacs'): | ||
Robert McGibbon
|
r9660 | install_editor(exe + u' +{line} {filename}') | ||
vivainio
|
r851 | |||
Robert McGibbon
|
r8819 | |||
Robert McGibbon
|
r8891 | def gnuclient(exe=u'gnuclient'): | ||
Robert McGibbon
|
r9660 | install_editor(exe + u' -nw +{line} {filename}') | ||
vivainio
|
r851 | |||
Robert McGibbon
|
r8819 | |||
Robert McGibbon
|
r8891 | def crimson_editor(exe=u'cedt.exe'): | ||
Robert McGibbon
|
r9660 | install_editor(exe + u' /L:{line} {filename}') | ||
Robert McGibbon
|
r8819 | |||
Robert McGibbon
|
r8891 | def kate(exe=u'kate'): | ||
Robert McGibbon
|
r9660 | install_editor(exe + u' -u -l {line} {filename}') | ||