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