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