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