Show More
@@ -1,115 +1,121 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.error import TryNext |
|
8 | from IPython.core.error import TryNext | |
9 | from string import Template |
|
9 | from string import Template | |
10 | import os |
|
10 | import os | |
11 |
|
11 | |||
12 |
|
12 | |||
13 | def install_editor(run_template, wait=False): |
|
13 | def install_editor(run_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 | |
17 | environment variable or is notepad (windows) or vi (linux). By supplying a |
|
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 |
|
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) |
|
19 | by IPython -- (e.g. the format in which it accepts command line options) | |
20 |
|
20 | |||
21 | Parameters |
|
21 | Parameters | |
22 | ---------- |
|
22 | ---------- | |
23 | run_template : basestring |
|
23 | run_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, | |
30 | to facilitate non-blocking editors that exit immediately after |
|
30 | to facilitate non-blocking editors that exit immediately after | |
31 | the call. |
|
31 | the call. | |
32 | """ |
|
32 | """ | |
33 |
|
33 | |||
34 | for substitution in ['$file', '$line']: |
|
34 | # not all editors support $line, so we'll leave out this check | |
35 |
|
|
35 | # for substitution in ['$file', '$line']: | |
36 | raise ValueError(('run_template should contain %s' |
|
36 | # if not substitution in run_template: | |
37 | ' for string substitution. You supplied "%s"' % (substitution, |
|
37 | # raise ValueError(('run_template should contain %s' | |
38 | run_template))) |
|
38 | # ' for string substitution. You supplied "%s"' % (substitution, | |
|
39 | # run_template))) | |||
39 |
|
40 | |||
40 | template = Template(run_template) |
|
41 | template = Template(run_template) | |
41 |
|
42 | |||
42 | def call_editor(self, file, line=0): |
|
43 | def call_editor(self, file, line=0): | |
43 | if line is None: |
|
44 | if line is None: | |
44 | line = 0 |
|
45 | line = 0 | |
45 | cmd = template.substitute(file=file, line=line) |
|
46 | cmd = template.substitute(file=file, line=line) | |
46 | print ">", cmd |
|
47 | print ">", cmd | |
47 | if os.system(cmd) != 0: |
|
48 | if os.system(cmd) != 0: | |
48 | raise TryNext() |
|
49 | raise TryNext() | |
49 | if wait: |
|
50 | if wait: | |
50 | raw_input("Press Enter when done editing:") |
|
51 | raw_input("Press Enter when done editing:") | |
51 |
|
52 | |||
52 | get_ipython().set_hook('editor', call_editor) |
|
53 | get_ipython().set_hook('editor', call_editor) | |
53 |
|
54 | |||
54 |
|
55 | |||
55 | # in these, exe is always the path/name of the executable. Useful |
|
56 | # in these, exe is always the path/name of the executable. Useful | |
56 | # if you don't have the editor directory in your path |
|
57 | # if you don't have the editor directory in your path | |
57 |
|
58 | |||
58 |
|
59 | |||
59 | def komodo(exe='komodo'): |
|
60 | def komodo(exe='komodo'): | |
60 | """ Activestate Komodo [Edit] """ |
|
61 | """ Activestate Komodo [Edit] """ | |
61 | install_editor(exe + ' -l $line "$file"', wait=True) |
|
62 | install_editor(exe + ' -l $line "$file"', wait=True) | |
62 |
|
63 | |||
63 |
|
64 | |||
64 | def scite(exe="scite"): |
|
65 | def scite(exe="scite"): | |
65 | """ SciTE or Sc1 """ |
|
66 | """ SciTE or Sc1 """ | |
66 | install_editor(exe + ' "$file" -goto:$line') |
|
67 | install_editor(exe + ' "$file" -goto:$line') | |
67 |
|
68 | |||
68 |
|
69 | |||
69 | def notepadplusplus(exe='notepad++'): |
|
70 | def notepadplusplus(exe='notepad++'): | |
70 | """ Notepad++ http://notepad-plus.sourceforge.net """ |
|
71 | """ Notepad++ http://notepad-plus.sourceforge.net """ | |
71 | install_editor(exe + ' -n$line "$file"') |
|
72 | install_editor(exe + ' -n$line "$file"') | |
72 |
|
73 | |||
73 |
|
74 | |||
74 | def jed(exe='jed'): |
|
75 | def jed(exe='jed'): | |
75 | """ JED, the lightweight emacsish editor """ |
|
76 | """ JED, the lightweight emacsish editor """ | |
76 | install_editor(exe + ' +$line "$file"') |
|
77 | install_editor(exe + ' +$line "$file"') | |
77 |
|
78 | |||
78 |
|
79 | |||
79 |
def idle(exe= |
|
80 | def idle(exe='idle'): | |
80 | """ Idle, the editor bundled with python |
|
81 | """ Idle, the editor bundled with python | |
81 |
|
82 | |||
82 | Should be pretty smart about finding the executable. |
|
83 | Parameters | |
|
84 | ---------- | |||
|
85 | exe : str, None | |||
|
86 | If none, should be pretty smart about finding the executable. | |||
83 | """ |
|
87 | """ | |
84 | if exe is None: |
|
88 | if exe is None: | |
85 | import idlelib |
|
89 | import idlelib | |
86 | p = os.path.dirname(idlelib.__file__) |
|
90 | p = os.path.dirname(idlelib.__file__) | |
|
91 | # i'm not sure if this actually works. Is this idle.py script guarenteed | |||
|
92 | # to be executable? | |||
87 | exe = os.path.join(p, 'idle.py') |
|
93 | exe = os.path.join(p, 'idle.py') | |
88 | install_editor(exe + ' "$file"') |
|
94 | install_editor(exe + ' "$file"') | |
89 |
|
95 | |||
90 |
|
96 | |||
91 | def mate(exe='mate'): |
|
97 | def mate(exe='mate'): | |
92 | """ TextMate, the missing editor""" |
|
98 | """ TextMate, the missing editor""" | |
93 | # wait=True is not required since we're using the -w flag to mate |
|
99 | # wait=True is not required since we're using the -w flag to mate | |
94 | install_editor(exe + ' -w -l $line "$file"') |
|
100 | install_editor(exe + ' -w -l $line "$file"') | |
95 |
|
101 | |||
96 |
|
102 | |||
97 | # ########################################## |
|
103 | # ########################################## | |
98 | # these are untested, report any problems |
|
104 | # these are untested, report any problems | |
99 | # ########################################## |
|
105 | # ########################################## | |
100 |
|
106 | |||
101 |
|
107 | |||
102 | def emacs(exe='emacs'): |
|
108 | def emacs(exe='emacs'): | |
103 | install_editor(exe + ' +$line "$file"') |
|
109 | install_editor(exe + ' +$line "$file"') | |
104 |
|
110 | |||
105 |
|
111 | |||
106 | def gnuclient(exe='gnuclient'): |
|
112 | def gnuclient(exe='gnuclient'): | |
107 | install_editor(exe + ' -nw +$line "$file"') |
|
113 | install_editor(exe + ' -nw +$line "$file"') | |
108 |
|
114 | |||
109 |
|
115 | |||
110 | def crimson_editor(exe='cedt.exe'): |
|
116 | def crimson_editor(exe='cedt.exe'): | |
111 | install_editor(exe + ' /L:$line "$file"') |
|
117 | install_editor(exe + ' /L:$line "$file"') | |
112 |
|
118 | |||
113 |
|
119 | |||
114 | def kate(exe='kate'): |
|
120 | def kate(exe='kate'): | |
115 | install_editor(exe + ' -u -l $line "$file"') |
|
121 | install_editor(exe + ' -u -l $line "$file"') |
General Comments 0
You need to be logged in to leave comments.
Login now