##// END OF EJS Templates
Fixed quarentine/ipy_editor.py so that it (0) uses string formatting instead of some old old 'itpl' module (1) doesn't use the deprecated ipyapi, (2) fully pep8 compliant, (3) better docstring
Robert McGibbon -
Show More
@@ -5,87 +5,111 b' They should honor the line number argument, at least.'
5 5 Contributions are *very* welcome.
6 6 """
7 7
8 from IPython.core import ipapi
9 8 from IPython.core.error import TryNext
10 ip = ipapi.get()
11
12 # We no longer bundle Itpl. If you update this module, you should use advanced
13 # string formatting instead.
14 from IPython.external.Itpl import itplns
9 from string import Template
15 10 import os
16 11
17 def install_editor(run_template, wait = False):
18 """ Gets a template in format "myeditor bah bah $file bah bah $line"
19
20 $file will be replaced by file name, $line by line number (or 0).
21 Installs the editor that is called by IPython, instead of the default
22 notepad or vi.
23
24 If wait is true, wait until the user presses enter before returning,
25 to facilitate non-blocking editors that exit immediately after
26 the call.
12
13 def install_editor(run_template, wait=False):
14 """Installs the editor that is called by IPython for the %edit magic.
15
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
21 Parameters
22 ----------
23 run_template : basestring
24 run_template acts as a template for how your editor is invoked by
25 the shell. It should contain '$file', which will be replaced on
26 invokation with the file name, and '$line$', $line by line number
27 (or 0) to invoke the file with.
28 wait : bool
29 If `wait` is true, wait until the user presses enter before returning,
30 to facilitate non-blocking editors that exit immediately after
31 the call.
27 32 """
28
33
34 for substitution in ['$file', '$line']:
35 if not substitution in run_template:
36 raise ValueError(('run_template should contain %s'
37 ' for string substitution. You supplied "%s"' % (substitution,
38 run_template)))
39
40 template = Template(run_template)
41
29 42 def call_editor(self, file, line=0):
30 43 if line is None:
31 44 line = 0
32 cmd = itplns(run_template, locals())
33 print ">",cmd
45 cmd = template.substitute(file=file, line=line)
46 print ">", cmd
34 47 if os.system(cmd) != 0:
35 48 raise TryNext()
36 49 if wait:
37 50 raw_input("Press Enter when done editing:")
38 51
39 ip.set_hook('editor',call_editor)
52 get_ipython().set_hook('editor', call_editor)
40 53
41 54
42 55 # in these, exe is always the path/name of the executable. Useful
43 56 # if you don't have the editor directory in your path
44 57
45 def komodo(exe = 'komodo'):
58
59 def komodo(exe='komodo'):
46 60 """ Activestate Komodo [Edit] """
47 install_editor(exe + ' -l $line "$file"', wait = True)
61 install_editor(exe + ' -l $line "$file"', wait=True)
62
48 63
49 def scite(exe = "scite"):
64 def scite(exe="scite"):
50 65 """ SciTE or Sc1 """
51 66 install_editor(exe + ' "$file" -goto:$line')
52 67
53 def notepadplusplus(exe = 'notepad++'):
68
69 def notepadplusplus(exe='notepad++'):
54 70 """ Notepad++ http://notepad-plus.sourceforge.net """
55 71 install_editor(exe + ' -n$line "$file"')
56
57 def jed(exe = 'jed'):
72
73
74 def jed(exe='jed'):
58 75 """ JED, the lightweight emacsish editor """
59 76 install_editor(exe + ' +$line "$file"')
60 77
61 def idle(exe = None):
78
79 def idle(exe=None):
62 80 """ Idle, the editor bundled with python
63
81
64 82 Should be pretty smart about finding the executable.
65 83 """
66 84 if exe is None:
67 85 import idlelib
68 86 p = os.path.dirname(idlelib.__file__)
69 exe = p + '/idle.py'
87 exe = os.path.join(p, 'idle.py')
70 88 install_editor(exe + ' "$file"')
71 89
72 def mate(exe = 'mate'):
90
91 def mate(exe='mate'):
73 92 """ TextMate, the missing editor"""
93 # wait=True is not required since we're using the -w flag to mate
74 94 install_editor(exe + ' -w -l $line "$file"')
75 95
96
97 # ##########################################
76 98 # these are untested, report any problems
99 # ##########################################
100
77 101
78 def emacs(exe = 'emacs'):
102 def emacs(exe='emacs'):
79 103 install_editor(exe + ' +$line "$file"')
80 104
81 def gnuclient(exe= 'gnuclient'):
105
106 def gnuclient(exe='gnuclient'):
82 107 install_editor(exe + ' -nw +$line "$file"')
83 108
84 def crimson_editor(exe = 'cedt.exe'):
109
110 def crimson_editor(exe='cedt.exe'):
85 111 install_editor(exe + ' /L:$line "$file"')
86
87 def kate(exe = 'kate'):
112
113
114 def kate(exe='kate'):
88 115 install_editor(exe + ' -u -l $line "$file"')
89
90
91
General Comments 0
You need to be logged in to leave comments. Login now