Debian Codegraph @

IPython/lib/editorhooks.py

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