Show More
@@ -1,91 +1,115 b'' | |||||
1 | """ 'editor' hooks for common editors that work well with ipython |
|
1 | """ 'editor' hooks for common editors that work well with ipython | |
2 |
|
2 | |||
3 | They should honor the line number argument, at least. |
|
3 | They should honor the line number argument, at least. | |
4 |
|
4 | |||
5 | Contributions are *very* welcome. |
|
5 | Contributions are *very* welcome. | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | from IPython.core import ipapi |
|
|||
9 | from IPython.core.error import TryNext |
|
8 | from IPython.core.error import TryNext | |
10 | ip = ipapi.get() |
|
9 | from string import Template | |
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 |
|
|||
15 | import os |
|
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 |
|
12 | |||
24 | If wait is true, wait until the user presses enter before returning, |
|
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, | |||
25 | to facilitate non-blocking editors that exit immediately after |
|
30 | to facilitate non-blocking editors that exit immediately after | |
26 | the call. |
|
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 | def call_editor(self, file, line=0): |
|
42 | def call_editor(self, file, line=0): | |
30 | if line is None: |
|
43 | if line is None: | |
31 | line = 0 |
|
44 | line = 0 | |
32 | cmd = itplns(run_template, locals()) |
|
45 | cmd = template.substitute(file=file, line=line) | |
33 | print ">",cmd |
|
46 | print ">", cmd | |
34 | if os.system(cmd) != 0: |
|
47 | if os.system(cmd) != 0: | |
35 | raise TryNext() |
|
48 | raise TryNext() | |
36 | if wait: |
|
49 | if wait: | |
37 | raw_input("Press Enter when done editing:") |
|
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 | # 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 | |
43 | # if you don't have the editor directory in your path |
|
56 | # if you don't have the editor directory in your path | |
44 |
|
57 | |||
|
58 | ||||
45 |
def komodo(exe |
|
59 | def komodo(exe='komodo'): | |
46 | """ Activestate Komodo [Edit] """ |
|
60 | """ Activestate Komodo [Edit] """ | |
47 |
install_editor(exe + ' -l $line "$file"', wait |
|
61 | install_editor(exe + ' -l $line "$file"', wait=True) | |
48 |
|
62 | |||
|
63 | ||||
49 |
def scite(exe |
|
64 | def scite(exe="scite"): | |
50 | """ SciTE or Sc1 """ |
|
65 | """ SciTE or Sc1 """ | |
51 | install_editor(exe + ' "$file" -goto:$line') |
|
66 | install_editor(exe + ' "$file" -goto:$line') | |
52 |
|
67 | |||
|
68 | ||||
53 |
def notepadplusplus(exe |
|
69 | def notepadplusplus(exe='notepad++'): | |
54 | """ Notepad++ http://notepad-plus.sourceforge.net """ |
|
70 | """ Notepad++ http://notepad-plus.sourceforge.net """ | |
55 | install_editor(exe + ' -n$line "$file"') |
|
71 | install_editor(exe + ' -n$line "$file"') | |
56 |
|
72 | |||
|
73 | ||||
57 |
def jed(exe |
|
74 | def jed(exe='jed'): | |
58 | """ JED, the lightweight emacsish editor """ |
|
75 | """ JED, the lightweight emacsish editor """ | |
59 | install_editor(exe + ' +$line "$file"') |
|
76 | install_editor(exe + ' +$line "$file"') | |
60 |
|
77 | |||
|
78 | ||||
61 |
def idle(exe |
|
79 | def idle(exe=None): | |
62 | """ Idle, the editor bundled with python |
|
80 | """ Idle, the editor bundled with python | |
63 |
|
81 | |||
64 | Should be pretty smart about finding the executable. |
|
82 | Should be pretty smart about finding the executable. | |
65 | """ |
|
83 | """ | |
66 | if exe is None: |
|
84 | if exe is None: | |
67 | import idlelib |
|
85 | import idlelib | |
68 | p = os.path.dirname(idlelib.__file__) |
|
86 | p = os.path.dirname(idlelib.__file__) | |
69 |
exe = p |
|
87 | exe = os.path.join(p, 'idle.py') | |
70 | install_editor(exe + ' "$file"') |
|
88 | install_editor(exe + ' "$file"') | |
71 |
|
89 | |||
|
90 | ||||
72 |
def mate(exe |
|
91 | def mate(exe='mate'): | |
73 | """ TextMate, the missing editor""" |
|
92 | """ TextMate, the missing editor""" | |
|
93 | # wait=True is not required since we're using the -w flag to mate | |||
74 | install_editor(exe + ' -w -l $line "$file"') |
|
94 | install_editor(exe + ' -w -l $line "$file"') | |
75 |
|
95 | |||
|
96 | ||||
|
97 | # ########################################## | |||
76 | # these are untested, report any problems |
|
98 | # these are untested, report any problems | |
|
99 | # ########################################## | |||
|
100 | ||||
77 |
|
101 | |||
78 |
def emacs(exe |
|
102 | def emacs(exe='emacs'): | |
79 | install_editor(exe + ' +$line "$file"') |
|
103 | install_editor(exe + ' +$line "$file"') | |
80 |
|
104 | |||
|
105 | ||||
81 |
def gnuclient(exe= |
|
106 | def gnuclient(exe='gnuclient'): | |
82 | install_editor(exe + ' -nw +$line "$file"') |
|
107 | install_editor(exe + ' -nw +$line "$file"') | |
83 |
|
108 | |||
|
109 | ||||
84 |
def crimson_editor(exe |
|
110 | def crimson_editor(exe='cedt.exe'): | |
85 | install_editor(exe + ' /L:$line "$file"') |
|
111 | install_editor(exe + ' /L:$line "$file"') | |
86 |
|
112 | |||
|
113 | ||||
87 |
def kate(exe |
|
114 | def kate(exe='kate'): | |
88 | install_editor(exe + ' -u -l $line "$file"') |
|
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