Show More
@@ -1,219 +1,242 b'' | |||||
1 | import IPython.ipapi |
|
1 | import IPython.ipapi | |
2 | ip = IPython.ipapi.get() |
|
2 | ip = IPython.ipapi.get() | |
3 |
|
3 | |||
4 | import win32api |
|
4 | import win32api | |
5 | import win32ui |
|
5 | import win32ui | |
6 | import win32console |
|
6 | import win32console | |
7 | import dde |
|
7 | import dde | |
8 | import os |
|
8 | import os | |
9 | import scitedirector |
|
9 | import scitedirector | |
10 |
|
10 | |||
11 | # test to write. |
|
11 | # test to write. | |
12 |
|
12 | |||
13 |
def set |
|
13 | def set_hook(synchronize_with_editor): | |
|
14 | """Set the synchronize with editor hook with a callable object. | |||
|
15 | ||||
|
16 | The callable object will be called with the following arguments when | |||
|
17 | IPython wants to synchronize with you favorite editor: | |||
|
18 | ||||
|
19 | - ip: a running IPython instance. | |||
|
20 | ||||
|
21 | - filename: the path of the file the editor is supposed to display. | |||
|
22 | ||||
|
23 | - lineno : the line number of the line the editor is supposed to | |||
|
24 | highlight. | |||
|
25 | ||||
|
26 | - columnno : the column number of the character the editor is supposed | |||
|
27 | to highlight. | |||
|
28 | """ | |||
14 | ip.set_hook("synchronize_with_editor", synchronize_with_editor) |
|
29 | ip.set_hook("synchronize_with_editor", synchronize_with_editor) | |
15 |
|
30 | |||
16 |
|
31 | |||
17 |
def find |
|
32 | def find_filename(filename): | |
|
33 | """Return the filename to synchronize with based on """ | |||
18 | filename = os.path.splitext(filename) |
|
34 | filename = os.path.splitext(filename) | |
19 | if filename[1] == ".pyc": |
|
35 | if filename[1] == ".pyc": | |
20 | filename = (filename[0], ".py") |
|
36 | filename = (filename[0], ".py") | |
21 | filename = "".join(filename) |
|
37 | filename = "".join(filename) | |
22 |
|
38 | |||
23 | if not os.path.isabs(filename): |
|
39 | if not os.path.isabs(filename): | |
24 | filename = os.path.join(os.getcwd(), filename) |
|
40 | filename = os.path.join(os.getcwd(), filename) | |
25 |
|
41 | |||
26 | if os.path.isfile(filename): |
|
42 | if os.path.isfile(filename): | |
27 | return filename |
|
43 | return filename | |
28 |
|
44 | |||
29 | return "" |
|
45 | return "" | |
30 |
|
46 | |||
31 |
|
47 | |||
32 |
def run |
|
48 | def run_command(path, command, arguments, asynchronous = True): | |
|
49 | """Run a shell command and return the exit code of the command""" | |||
|
50 | # This is a thin wrapper around os.system that: | |||
|
51 | # - Let you run command asynchronously. | |||
|
52 | # - Accept spaces in command path. | |||
|
53 | # - Dont throw exception if the command don't exist. | |||
33 | line = '' |
|
54 | line = '' | |
34 | if asynchronous: |
|
55 | if asynchronous: | |
35 | line += 'start ' |
|
56 | line += 'start ' | |
36 |
|
57 | |||
37 | try: |
|
58 | try: | |
38 | line += win32api.GetShortPathName(os.path.join(path, command) + ".exe") + " " |
|
59 | line += win32api.GetShortPathName(os.path.join(path, command) + ".exe") + " " | |
39 | except: |
|
60 | except: | |
40 | print 'could not find: "%s"' % (os.path.join(path, command) + ".exe") |
|
61 | print 'could not find: "%s"' % (os.path.join(path, command) + ".exe") | |
41 | return -1 |
|
62 | return -1 | |
42 |
|
63 | |||
43 | line += arguments |
|
64 | line += arguments | |
44 | r = os.system(line) |
|
65 | r = os.system(line) | |
45 | return r |
|
66 | return r | |
46 |
|
67 | |||
47 |
|
68 | |||
48 | def sleep(milliseconds): |
|
69 | def sleep(milliseconds): | |
|
70 | """Wait some milliseconds.""" | |||
|
71 | # This is used to make sure the editor did its job before we reset the focus on the console. | |||
49 | win32api.Sleep(milliseconds) |
|
72 | win32api.Sleep(milliseconds) | |
50 |
|
73 | |||
51 |
|
74 | |||
52 |
def restore |
|
75 | def restore_console_focus(): | |
|
76 | """Restore the focus to the IPython console.""" | |||
53 | h = win32console.GetConsoleWindow() |
|
77 | h = win32console.GetConsoleWindow() | |
54 | console_window = win32ui.CreateWindowFromHandle(h) |
|
78 | console_window = win32ui.CreateWindowFromHandle(h) | |
55 | console_window.SetForegroundWindow() |
|
79 | console_window.SetForegroundWindow() | |
56 |
|
80 | |||
57 |
|
81 | |||
58 |
|
||||
59 | # This is the most simple example of hook: |
|
82 | # This is the most simple example of hook: | |
60 | class GVimHook: |
|
83 | class GVimHook: | |
61 | def __init__(self, path, wakeup_duration): |
|
84 | def __init__(self, path, wakeup_duration): | |
62 | self.path = path |
|
85 | self.path = path | |
63 | self.wakeup_duration = wakeup_duration |
|
86 | self.wakeup_duration = wakeup_duration | |
64 |
|
87 | |||
65 | def __call__(self, ip, filename, lineno, columnno): |
|
88 | def __call__(self, ip, filename, lineno, columnno): | |
66 |
filename = find |
|
89 | filename = find_filename(filename) | |
67 |
|
90 | |||
68 | if not filename: |
|
91 | if not filename: | |
69 | return |
|
92 | return | |
70 |
|
93 | |||
71 |
run |
|
94 | run_command(self.path, 'gvim', '--remote-silent +%d "%s"' % (lineno, filename)) | |
72 |
|
95 | |||
73 | sleep(self.wakeup_duration) |
|
96 | sleep(self.wakeup_duration) | |
74 |
|
97 | |||
75 |
restore |
|
98 | restore_console_focus() | |
76 |
|
99 | |||
77 |
|
100 | |||
78 | def gvim(path = r"C:\Program Files\vim\vim71", wakeup_duration = 100): |
|
101 | def gvim(path = r"C:\Program Files\vim\vim71", wakeup_duration = 100): | |
79 | synchronize_with_editor = GVimHook(path, wakeup_duration) |
|
102 | synchronize_with_editor = GVimHook(path, wakeup_duration) | |
80 |
set |
|
103 | set_hook(synchronize_with_editor) | |
81 |
|
104 | |||
82 |
|
105 | |||
83 | class EmacsHook: |
|
106 | class EmacsHook: | |
84 | def __init__(self, path, wakeup_duration, start_duration): |
|
107 | def __init__(self, path, wakeup_duration, start_duration): | |
85 | self.path = path |
|
108 | self.path = path | |
86 | self.wakeup_duration = wakeup_duration |
|
109 | self.wakeup_duration = wakeup_duration | |
87 | self.start_duration = start_duration |
|
110 | self.start_duration = start_duration | |
88 |
|
111 | |||
89 | def __call__(self, ip, filename, lineno, columnno): |
|
112 | def __call__(self, ip, filename, lineno, columnno): | |
90 |
filename = find |
|
113 | filename = find_filename(filename) | |
91 |
|
114 | |||
92 | if not filename: |
|
115 | if not filename: | |
93 | return |
|
116 | return | |
94 |
|
117 | |||
95 |
r = run |
|
118 | r = run_command(self.path, "emacsclient", '-n +%d:%d "%s" 2>nul' % (lineno, columnno, filename), False) | |
96 | if r != 0: |
|
119 | if r != 0: | |
97 |
run |
|
120 | run_command(self.path, 'runemacs', '--quick -f server-start +%d:%d "%s"' % (lineno, columnno, filename)) | |
98 | sleep(self.start_duration) |
|
121 | sleep(self.start_duration) | |
99 | else: |
|
122 | else: | |
100 | sleep(self.wakeup_duration) |
|
123 | sleep(self.wakeup_duration) | |
101 |
|
124 | |||
102 |
restore |
|
125 | restore_console_focus() | |
103 |
|
126 | |||
104 |
|
127 | |||
105 | def emacs(path = r"C:\Program Files\emacs\bin", wakeup_duration = 100, start_duration = 2000): |
|
128 | def emacs(path = r"C:\Program Files\emacs\bin", wakeup_duration = 100, start_duration = 2000): | |
106 | synchronize_with_editor = EmacsHook(path, wakeup_duration, start_duration) |
|
129 | synchronize_with_editor = EmacsHook(path, wakeup_duration, start_duration) | |
107 |
set |
|
130 | set_hook(synchronize_with_editor) | |
108 |
|
131 | |||
109 |
|
132 | |||
110 | class SciteHook: |
|
133 | class SciteHook: | |
111 | def __init__(self, path, wakeup_duration, start_duration): |
|
134 | def __init__(self, path, wakeup_duration, start_duration): | |
112 | self.path = path |
|
135 | self.path = path | |
113 | self.wakeup_duration = wakeup_duration |
|
136 | self.wakeup_duration = wakeup_duration | |
114 | self.start_duration = start_duration |
|
137 | self.start_duration = start_duration | |
115 |
|
138 | |||
116 | def __call__(self, ip, filename, lineno, columnno): |
|
139 | def __call__(self, ip, filename, lineno, columnno): | |
117 |
filename = find |
|
140 | filename = find_filename(filename) | |
118 |
|
141 | |||
119 | if not filename: |
|
142 | if not filename: | |
120 | return |
|
143 | return | |
121 |
|
144 | |||
122 | scites = scitedirector.findWindows() |
|
145 | scites = scitedirector.findWindows() | |
123 | if not scites: |
|
146 | if not scites: | |
124 |
run |
|
147 | run_command(self.path, "scite", '"-open:%s" -goto:%d' % (filename.replace("\\", "/"), lineno)) | |
125 |
|
148 | |||
126 | sleep(self.start_duration) |
|
149 | sleep(self.start_duration) | |
127 |
restore |
|
150 | restore_console_focus() | |
128 | else: |
|
151 | else: | |
129 | scite = scites[0] |
|
152 | scite = scites[0] | |
130 | scitedirector.sendCommand(scite, 'open:%s' % filename.replace("\\", "/")) |
|
153 | scitedirector.sendCommand(scite, 'open:%s' % filename.replace("\\", "/")) | |
131 | scitedirector.sendCommand(scite, "goto:%d" % lineno) |
|
154 | scitedirector.sendCommand(scite, "goto:%d" % lineno) | |
132 |
|
155 | |||
133 |
|
156 | |||
134 | def scite(path = r"C:\Program Files\SciTE Source Code Editor", wakeup_duration = 100, start_duration = 500): |
|
157 | def scite(path = r"C:\Program Files\SciTE Source Code Editor", wakeup_duration = 100, start_duration = 500): | |
135 | synchronize_with_editor = SciteHook(path, wakeup_duration, start_duration) |
|
158 | synchronize_with_editor = SciteHook(path, wakeup_duration, start_duration) | |
136 |
set |
|
159 | set_hook(synchronize_with_editor) | |
137 |
|
160 | |||
138 |
|
161 | |||
139 | class NodePadPlusPlusHook: |
|
162 | class NodePadPlusPlusHook: | |
140 | def __init__(self, path, wakeup_duration): |
|
163 | def __init__(self, path, wakeup_duration): | |
141 | self.path = path |
|
164 | self.path = path | |
142 | self.wakeup_duration = wakeup_duration |
|
165 | self.wakeup_duration = wakeup_duration | |
143 |
|
166 | |||
144 | def __call__(self, ip, filename, lineno, columnno): |
|
167 | def __call__(self, ip, filename, lineno, columnno): | |
145 |
filename = find |
|
168 | filename = find_filename(filename) | |
146 |
|
169 | |||
147 | if not filename: |
|
170 | if not filename: | |
148 | return |
|
171 | return | |
149 |
|
172 | |||
150 |
run |
|
173 | run_command(self.path, "notepad++", '"%s" -n%d' % (filename, lineno)) | |
151 |
|
174 | |||
152 | sleep(self.wakeup_duration) |
|
175 | sleep(self.wakeup_duration) | |
153 |
|
176 | |||
154 |
restore |
|
177 | restore_console_focus() | |
155 |
|
178 | |||
156 |
|
179 | |||
157 | def notepadplusplus(path = r"C:\Program Files\Notepad++", wakeup_duration = 100): |
|
180 | def notepadplusplus(path = r"C:\Program Files\Notepad++", wakeup_duration = 100): | |
158 | synchronize_with_editor = NodePadPlusPlusHook(path, wakeup_duration) |
|
181 | synchronize_with_editor = NodePadPlusPlusHook(path, wakeup_duration) | |
159 |
set |
|
182 | set_hook(synchronize_with_editor) | |
160 |
|
183 | |||
161 |
|
184 | |||
162 | class PsPadHook: |
|
185 | class PsPadHook: | |
163 | def __init__(self, path, wakeup_duration): |
|
186 | def __init__(self, path, wakeup_duration): | |
164 | self.path = path |
|
187 | self.path = path | |
165 | self.wakeup_duration = wakeup_duration |
|
188 | self.wakeup_duration = wakeup_duration | |
166 |
|
189 | |||
167 | def __call__(self, ip, filename, lineno, columnno): |
|
190 | def __call__(self, ip, filename, lineno, columnno): | |
168 |
filename = find |
|
191 | filename = find_filename(filename) | |
169 |
|
192 | |||
170 | if not filename: |
|
193 | if not filename: | |
171 | return |
|
194 | return | |
172 |
|
195 | |||
173 |
run |
|
196 | run_command(self.path, "pspad", '"%s" -%d' % (filename, lineno)) | |
174 |
|
197 | |||
175 | sleep(self.wakeup_duration) |
|
198 | sleep(self.wakeup_duration) | |
176 |
|
199 | |||
177 |
restore |
|
200 | restore_console_focus() | |
178 |
|
201 | |||
179 |
|
202 | |||
180 | def pspad(path = r"C:\Program Files\PSPad editor", wakeup_duration = 100): |
|
203 | def pspad(path = r"C:\Program Files\PSPad editor", wakeup_duration = 100): | |
181 | synchronize_with_editor = PsPadHook(path, wakeup_duration) |
|
204 | synchronize_with_editor = PsPadHook(path, wakeup_duration) | |
182 |
set |
|
205 | set_hook(synchronize_with_editor) | |
183 |
|
206 | |||
184 |
|
207 | |||
185 | # This is an example of DDE hook: |
|
208 | # This is an example of DDE hook: | |
186 | class UltraEditHook: |
|
209 | class UltraEditHook: | |
187 | def __init__(self, path, wakeup_duration, start_duration): |
|
210 | def __init__(self, path, wakeup_duration, start_duration): | |
188 | self.path = path |
|
211 | self.path = path | |
189 | self.wakeup_duration = wakeup_duration |
|
212 | self.wakeup_duration = wakeup_duration | |
190 | self.start_duration = start_duration |
|
213 | self.start_duration = start_duration | |
191 |
|
214 | |||
192 | def __call__(self, ip, filename, lineno, columnno): |
|
215 | def __call__(self, ip, filename, lineno, columnno): | |
193 |
filename = find |
|
216 | filename = find_filename(filename) | |
194 |
|
217 | |||
195 | if not filename: |
|
218 | if not filename: | |
196 | return |
|
219 | return | |
197 |
|
220 | |||
198 | server = dde.CreateServer() |
|
221 | server = dde.CreateServer() | |
199 | server.Create("myddeserver") |
|
222 | server.Create("myddeserver") | |
200 | conversation = dde.CreateConversation(server) |
|
223 | conversation = dde.CreateConversation(server) | |
201 | try: |
|
224 | try: | |
202 | conversation.ConnectTo("uedit32", "System") |
|
225 | conversation.ConnectTo("uedit32", "System") | |
203 | conversation.Exec(r'[open("%s/%d"])' % (filename, lineno)) |
|
226 | conversation.Exec(r'[open("%s/%d"])' % (filename, lineno)) | |
204 |
|
227 | |||
205 | sleep(self.wakeup_duration) |
|
228 | sleep(self.wakeup_duration) | |
206 | except: |
|
229 | except: | |
207 |
run |
|
230 | run_command(self.path, 'uedit32', '"%s/%d"' % (filename, lineno)) | |
208 |
|
231 | |||
209 | sleep(self.start_duration) |
|
232 | sleep(self.start_duration) | |
210 |
|
233 | |||
211 | server.Shutdown() |
|
234 | server.Shutdown() | |
212 |
|
235 | |||
213 |
restore |
|
236 | restore_console_focus() | |
214 |
|
237 | |||
215 |
|
238 | |||
216 | def ultraedit(path = r"C:\Program Files\IDM Computer Solutions\UltraEdit-32", wakeup_duration = 10, start_duration = 2000): |
|
239 | def ultraedit(path = r"C:\Program Files\IDM Computer Solutions\UltraEdit-32", wakeup_duration = 10, start_duration = 2000): | |
217 | synchronize_with_editor = UltraEditHook(path, wakeup_duration, start_duration) |
|
240 | synchronize_with_editor = UltraEditHook(path, wakeup_duration, start_duration) | |
218 |
set |
|
241 | set_hook(synchronize_with_editor) | |
219 | No newline at end of file |
|
242 |
General Comments 0
You need to be logged in to leave comments.
Login now