##// END OF EJS Templates
rename the file to lib/editorhooks.py and chance string template to {} formating
Robert McGibbon -
Show More
@@ -6,11 +6,11 b' Contributions are *very* welcome.'
6 """
6 """
7
7
8 from IPython.core.error import TryNext
8 from IPython.core.error import TryNext
9 from string import Template
9 from IPython.frontend.qt.console import ipython_widget
10 import os
10 import os
11
11
12
12
13 def install_editor(run_template, wait=False):
13 def install_editor(template, wait=False):
14 """Installs the editor that is called by IPython for the %edit magic.
14 """Installs the editor that is called by IPython for the %edit magic.
15
15
16 This overrides the default editor, which is generally set by your EDITOR
16 This overrides the default editor, which is generally set by your EDITOR
@@ -20,10 +20,10 b' def install_editor(run_template, wait=False):'
20
20
21 Parameters
21 Parameters
22 ----------
22 ----------
23 run_template : basestring
23 template : basestring
24 run_template acts as a template for how your editor is invoked by
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
25 the shell. It should contain '{file}', which will be replaced on
26 invokation with the file name, and '$line$', $line by line number
26 invokation with the file name, and '{line}', $line by line number
27 (or 0) to invoke the file with.
27 (or 0) to invoke the file with.
28 wait : bool
28 wait : bool
29 If `wait` is true, wait until the user presses enter before returning,
29 If `wait` is true, wait until the user presses enter before returning,
@@ -38,12 +38,11 b' def install_editor(run_template, wait=False):'
38 # ' for string substitution. You supplied "%s"' % (substitution,
38 # ' for string substitution. You supplied "%s"' % (substitution,
39 # run_template)))
39 # run_template)))
40
40
41 template = Template(run_template)
42
41
43 def call_editor(self, file, line=0):
42 def call_editor(self, file, line=0):
44 if line is None:
43 if line is None:
45 line = 0
44 line = 0
46 cmd = template.substitute(file=file, line=line)
45 cmd = template.format(file=file, line=line)
47 print ">", cmd
46 print ">", cmd
48 if os.system(cmd) != 0:
47 if os.system(cmd) != 0:
49 raise TryNext()
48 raise TryNext()
@@ -51,7 +50,7 b' def install_editor(run_template, wait=False):'
51 raw_input("Press Enter when done editing:")
50 raw_input("Press Enter when done editing:")
52
51
53 get_ipython().set_hook('editor', call_editor)
52 get_ipython().set_hook('editor', call_editor)
54
53 ipython_widget.IPythonWidget.editor = template
55
54
56 # in these, exe is always the path/name of the executable. Useful
55 # in these, exe is always the path/name of the executable. Useful
57 # if you don't have the editor directory in your path
56 # if you don't have the editor directory in your path
@@ -59,22 +58,22 b' def install_editor(run_template, wait=False):'
59
58
60 def komodo(exe='komodo'):
59 def komodo(exe='komodo'):
61 """ Activestate Komodo [Edit] """
60 """ Activestate Komodo [Edit] """
62 install_editor(exe + ' -l $line "$file"', wait=True)
61 install_editor(exe + ' -l {line} "{file}"', wait=True)
63
62
64
63
65 def scite(exe="scite"):
64 def scite(exe="scite"):
66 """ SciTE or Sc1 """
65 """ SciTE or Sc1 """
67 install_editor(exe + ' "$file" -goto:$line')
66 install_editor(exe + ' "{file}" -goto:{line}')
68
67
69
68
70 def notepadplusplus(exe='notepad++'):
69 def notepadplusplus(exe='notepad++'):
71 """ Notepad++ http://notepad-plus.sourceforge.net """
70 """ Notepad++ http://notepad-plus.sourceforge.net """
72 install_editor(exe + ' -n$line "$file"')
71 install_editor(exe + ' -n{line} "{file}"')
73
72
74
73
75 def jed(exe='jed'):
74 def jed(exe='jed'):
76 """ JED, the lightweight emacsish editor """
75 """ JED, the lightweight emacsish editor """
77 install_editor(exe + ' +$line "$file"')
76 install_editor(exe + ' +{line} "{file}"')
78
77
79
78
80 def idle(exe='idle'):
79 def idle(exe='idle'):
@@ -91,13 +90,13 b" def idle(exe='idle'):"
91 # i'm not sure if this actually works. Is this idle.py script guarenteed
90 # i'm not sure if this actually works. Is this idle.py script guarenteed
92 # to be executable?
91 # to be executable?
93 exe = os.path.join(p, 'idle.py')
92 exe = os.path.join(p, 'idle.py')
94 install_editor(exe + ' "$file"')
93 install_editor(exe + ' "{file}"')
95
94
96
95
97 def mate(exe='mate'):
96 def mate(exe='mate'):
98 """ TextMate, the missing editor"""
97 """ TextMate, the missing editor"""
99 # wait=True is not required since we're using the -w flag to mate
98 # wait=True is not required since we're using the -w flag to mate
100 install_editor(exe + ' -w -l $line "$file"')
99 install_editor(exe + ' -w -l {line} "{file}"')
101
100
102
101
103 # ##########################################
102 # ##########################################
@@ -106,16 +105,16 b" def mate(exe='mate'):"
106
105
107
106
108 def emacs(exe='emacs'):
107 def emacs(exe='emacs'):
109 install_editor(exe + ' +$line "$file"')
108 install_editor(exe + ' +{line} "{file}"')
110
109
111
110
112 def gnuclient(exe='gnuclient'):
111 def gnuclient(exe='gnuclient'):
113 install_editor(exe + ' -nw +$line "$file"')
112 install_editor(exe + ' -nw +{line} "{file}"')
114
113
115
114
116 def crimson_editor(exe='cedt.exe'):
115 def crimson_editor(exe='cedt.exe'):
117 install_editor(exe + ' /L:$line "$file"')
116 install_editor(exe + ' /L:{line} "{file}"')
118
117
119
118
120 def kate(exe='kate'):
119 def kate(exe='kate'):
121 install_editor(exe + ' -u -l $line "$file"')
120 install_editor(exe + ' -u -l {line} "{file}"')
General Comments 0
You need to be logged in to leave comments. Login now