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