Show More
@@ -1,218 +1,219 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 |
|
12 | |||
12 | def setHook(synchronize_with_editor): |
|
13 | def setHook(synchronize_with_editor): | |
13 | ip.set_hook("synchronize_with_editor", synchronize_with_editor) |
|
14 | ip.set_hook("synchronize_with_editor", synchronize_with_editor) | |
14 |
|
15 | |||
15 |
|
16 | |||
16 | def findFilename(filename): |
|
17 | def findFilename(filename): | |
17 | filename = os.path.splitext(filename) |
|
18 | filename = os.path.splitext(filename) | |
18 | if filename[1] == ".pyc": |
|
19 | if filename[1] == ".pyc": | |
19 | filename = (filename[0], ".py") |
|
20 | filename = (filename[0], ".py") | |
20 | filename = "".join(filename) |
|
21 | filename = "".join(filename) | |
21 |
|
22 | |||
22 | if not os.path.isabs(filename): |
|
23 | if not os.path.isabs(filename): | |
23 | filename = os.path.join(os.getcwd(), filename) |
|
24 | filename = os.path.join(os.getcwd(), filename) | |
24 |
|
25 | |||
25 | if os.path.isfile(filename): |
|
26 | if os.path.isfile(filename): | |
26 | return filename |
|
27 | return filename | |
27 |
|
28 | |||
28 | return "" |
|
29 | return "" | |
29 |
|
30 | |||
30 |
|
31 | |||
31 | def runCommand(path, command, arguments, asynchronous = True): |
|
32 | def runCommand(path, command, arguments, asynchronous = True): | |
32 | line = '' |
|
33 | line = '' | |
33 | if asynchronous: |
|
34 | if asynchronous: | |
34 | line += 'start ' |
|
35 | line += 'start ' | |
35 |
|
36 | |||
36 | try: |
|
37 | try: | |
37 | line += win32api.GetShortPathName(os.path.join(path, command) + ".exe") + " " |
|
38 | line += win32api.GetShortPathName(os.path.join(path, command) + ".exe") + " " | |
38 | except: |
|
39 | except: | |
39 | print 'could not find: "%s"' % (os.path.join(path, command) + ".exe") |
|
40 | print 'could not find: "%s"' % (os.path.join(path, command) + ".exe") | |
40 | return -1 |
|
41 | return -1 | |
41 |
|
42 | |||
42 | line += arguments |
|
43 | line += arguments | |
43 | r = os.system(line) |
|
44 | r = os.system(line) | |
44 | return r |
|
45 | return r | |
45 |
|
46 | |||
46 |
|
47 | |||
47 | def sleep(milliseconds): |
|
48 | def sleep(milliseconds): | |
48 | win32api.Sleep(milliseconds) |
|
49 | win32api.Sleep(milliseconds) | |
49 |
|
50 | |||
50 |
|
51 | |||
51 | def restoreConsoleFocus(): |
|
52 | def restoreConsoleFocus(): | |
52 | h = win32console.GetConsoleWindow() |
|
53 | h = win32console.GetConsoleWindow() | |
53 | console_window = win32ui.CreateWindowFromHandle(h) |
|
54 | console_window = win32ui.CreateWindowFromHandle(h) | |
54 | console_window.SetForegroundWindow() |
|
55 | console_window.SetForegroundWindow() | |
55 |
|
56 | |||
56 |
|
57 | |||
57 |
|
58 | |||
58 | # This is the most simple example of hook: |
|
59 | # This is the most simple example of hook: | |
59 | class GVimHook: |
|
60 | class GVimHook: | |
60 | def __init__(self, path, wakeup_duration): |
|
61 | def __init__(self, path, wakeup_duration): | |
61 | self.path = path |
|
62 | self.path = path | |
62 | self.wakeup_duration = wakeup_duration |
|
63 | self.wakeup_duration = wakeup_duration | |
63 |
|
64 | |||
64 | def __call__(self, ip, filename, lineno, columnno): |
|
65 | def __call__(self, ip, filename, lineno, columnno): | |
65 | filename = findFilename(filename) |
|
66 | filename = findFilename(filename) | |
66 |
|
67 | |||
67 | if not filename: |
|
68 | if not filename: | |
68 | return |
|
69 | return | |
69 |
|
70 | |||
70 | runCommand(self.path, 'gvim', '--remote-silent +%d "%s"' % (lineno, filename)) |
|
71 | runCommand(self.path, 'gvim', '--remote-silent +%d "%s"' % (lineno, filename)) | |
71 |
|
72 | |||
72 | sleep(self.wakeup_duration) |
|
73 | sleep(self.wakeup_duration) | |
73 |
|
74 | |||
74 | restoreConsoleFocus() |
|
75 | restoreConsoleFocus() | |
75 |
|
76 | |||
76 |
|
77 | |||
77 | def gvim(path = r"C:\Program Files\vim\vim71", wakeup_duration = 100): |
|
78 | def gvim(path = r"C:\Program Files\vim\vim71", wakeup_duration = 100): | |
78 | synchronize_with_editor = GVimHook(path, wakeup_duration) |
|
79 | synchronize_with_editor = GVimHook(path, wakeup_duration) | |
79 | setHook(synchronize_with_editor) |
|
80 | setHook(synchronize_with_editor) | |
80 |
|
81 | |||
81 |
|
82 | |||
82 | class EmacsHook: |
|
83 | class EmacsHook: | |
83 | def __init__(self, path, wakeup_duration, start_duration): |
|
84 | def __init__(self, path, wakeup_duration, start_duration): | |
84 | self.path = path |
|
85 | self.path = path | |
85 | self.wakeup_duration = wakeup_duration |
|
86 | self.wakeup_duration = wakeup_duration | |
86 | self.start_duration = start_duration |
|
87 | self.start_duration = start_duration | |
87 |
|
88 | |||
88 | def __call__(self, ip, filename, lineno, columnno): |
|
89 | def __call__(self, ip, filename, lineno, columnno): | |
89 | filename = findFilename(filename) |
|
90 | filename = findFilename(filename) | |
90 |
|
91 | |||
91 | if not filename: |
|
92 | if not filename: | |
92 | return |
|
93 | return | |
93 |
|
94 | |||
94 | r = runCommand(self.path, "emacsclient", '-n +%d:%d "%s" 2>nul' % (lineno, columnno, filename), False) |
|
95 | r = runCommand(self.path, "emacsclient", '-n +%d:%d "%s" 2>nul' % (lineno, columnno, filename), False) | |
95 | if r != 0: |
|
96 | if r != 0: | |
96 | runCommand(self.path, 'runemacs', '--quick -f server-start +%d:%d "%s"' % (lineno, columnno, filename)) |
|
97 | runCommand(self.path, 'runemacs', '--quick -f server-start +%d:%d "%s"' % (lineno, columnno, filename)) | |
97 | sleep(self.start_duration) |
|
98 | sleep(self.start_duration) | |
98 | else: |
|
99 | else: | |
99 | sleep(self.wakeup_duration) |
|
100 | sleep(self.wakeup_duration) | |
100 |
|
101 | |||
101 | restoreConsoleFocus() |
|
102 | restoreConsoleFocus() | |
102 |
|
103 | |||
103 |
|
104 | |||
104 | def emacs(path = r"C:\Program Files\emacs\bin", wakeup_duration = 100, start_duration = 2000): |
|
105 | def emacs(path = r"C:\Program Files\emacs\bin", wakeup_duration = 100, start_duration = 2000): | |
105 | synchronize_with_editor = EmacsHook(path, wakeup_duration, start_duration) |
|
106 | synchronize_with_editor = EmacsHook(path, wakeup_duration, start_duration) | |
106 | setHook(synchronize_with_editor) |
|
107 | setHook(synchronize_with_editor) | |
107 |
|
108 | |||
108 |
|
109 | |||
109 | class SciteHook: |
|
110 | class SciteHook: | |
110 | def __init__(self, path, wakeup_duration, start_duration): |
|
111 | def __init__(self, path, wakeup_duration, start_duration): | |
111 | self.path = path |
|
112 | self.path = path | |
112 | self.wakeup_duration = wakeup_duration |
|
113 | self.wakeup_duration = wakeup_duration | |
113 | self.start_duration = start_duration |
|
114 | self.start_duration = start_duration | |
114 |
|
115 | |||
115 | def __call__(self, ip, filename, lineno, columnno): |
|
116 | def __call__(self, ip, filename, lineno, columnno): | |
116 | filename = findFilename(filename) |
|
117 | filename = findFilename(filename) | |
117 |
|
118 | |||
118 | if not filename: |
|
119 | if not filename: | |
119 | return |
|
120 | return | |
120 |
|
121 | |||
121 | scites = scitedirector.findWindows() |
|
122 | scites = scitedirector.findWindows() | |
122 | if not scites: |
|
123 | if not scites: | |
123 | runCommand(self.path, "scite", '"-open:%s" -goto:%d' % (filename.replace("\\", "/"), lineno)) |
|
124 | runCommand(self.path, "scite", '"-open:%s" -goto:%d' % (filename.replace("\\", "/"), lineno)) | |
124 |
|
125 | |||
125 | sleep(self.start_duration) |
|
126 | sleep(self.start_duration) | |
126 | restoreConsoleFocus() |
|
127 | restoreConsoleFocus() | |
127 | else: |
|
128 | else: | |
128 | scite = scites[0] |
|
129 | scite = scites[0] | |
129 | scitedirector.sendCommand(scite, 'open:%s' % filename.replace("\\", "/")) |
|
130 | scitedirector.sendCommand(scite, 'open:%s' % filename.replace("\\", "/")) | |
130 | scitedirector.sendCommand(scite, "goto:%d" % lineno) |
|
131 | scitedirector.sendCommand(scite, "goto:%d" % lineno) | |
131 |
|
132 | |||
132 |
|
133 | |||
133 | def scite(path = r"C:\Program Files\SciTE Source Code Editor", wakeup_duration = 100, start_duration = 500): |
|
134 | def scite(path = r"C:\Program Files\SciTE Source Code Editor", wakeup_duration = 100, start_duration = 500): | |
134 | synchronize_with_editor = SciteHook(path, wakeup_duration, start_duration) |
|
135 | synchronize_with_editor = SciteHook(path, wakeup_duration, start_duration) | |
135 | setHook(synchronize_with_editor) |
|
136 | setHook(synchronize_with_editor) | |
136 |
|
137 | |||
137 |
|
138 | |||
138 | class NodePadPlusPlusHook: |
|
139 | class NodePadPlusPlusHook: | |
139 | def __init__(self, path, wakeup_duration): |
|
140 | def __init__(self, path, wakeup_duration): | |
140 | self.path = path |
|
141 | self.path = path | |
141 | self.wakeup_duration = wakeup_duration |
|
142 | self.wakeup_duration = wakeup_duration | |
142 |
|
143 | |||
143 | def __call__(self, ip, filename, lineno, columnno): |
|
144 | def __call__(self, ip, filename, lineno, columnno): | |
144 | filename = findFilename(filename) |
|
145 | filename = findFilename(filename) | |
145 |
|
146 | |||
146 | if not filename: |
|
147 | if not filename: | |
147 | return |
|
148 | return | |
148 |
|
149 | |||
149 | runCommand(self.path, "notepad++", '"%s" -n%d' % (filename, lineno)) |
|
150 | runCommand(self.path, "notepad++", '"%s" -n%d' % (filename, lineno)) | |
150 |
|
151 | |||
151 | sleep(self.wakeup_duration) |
|
152 | sleep(self.wakeup_duration) | |
152 |
|
153 | |||
153 | restoreConsoleFocus() |
|
154 | restoreConsoleFocus() | |
154 |
|
155 | |||
155 |
|
156 | |||
156 | def notepadplusplus(path = r"C:\Program Files\Notepad++", wakeup_duration = 100): |
|
157 | def notepadplusplus(path = r"C:\Program Files\Notepad++", wakeup_duration = 100): | |
157 | synchronize_with_editor = NodePadPlusPlusHook(path, wakeup_duration) |
|
158 | synchronize_with_editor = NodePadPlusPlusHook(path, wakeup_duration) | |
158 | setHook(synchronize_with_editor) |
|
159 | setHook(synchronize_with_editor) | |
159 |
|
160 | |||
160 |
|
161 | |||
161 | class PsPadHook: |
|
162 | class PsPadHook: | |
162 | def __init__(self, path, wakeup_duration): |
|
163 | def __init__(self, path, wakeup_duration): | |
163 | self.path = path |
|
164 | self.path = path | |
164 | self.wakeup_duration = wakeup_duration |
|
165 | self.wakeup_duration = wakeup_duration | |
165 |
|
166 | |||
166 | def __call__(self, ip, filename, lineno, columnno): |
|
167 | def __call__(self, ip, filename, lineno, columnno): | |
167 | filename = findFilename(filename) |
|
168 | filename = findFilename(filename) | |
168 |
|
169 | |||
169 | if not filename: |
|
170 | if not filename: | |
170 | return |
|
171 | return | |
171 |
|
172 | |||
172 | runCommand(self.path, "pspad", '"%s" -%d' % (filename, lineno)) |
|
173 | runCommand(self.path, "pspad", '"%s" -%d' % (filename, lineno)) | |
173 |
|
174 | |||
174 | sleep(self.wakeup_duration) |
|
175 | sleep(self.wakeup_duration) | |
175 |
|
176 | |||
176 | restoreConsoleFocus() |
|
177 | restoreConsoleFocus() | |
177 |
|
178 | |||
178 |
|
179 | |||
179 | def pspad(path = r"C:\Program Files\PSPad editor", wakeup_duration = 100): |
|
180 | def pspad(path = r"C:\Program Files\PSPad editor", wakeup_duration = 100): | |
180 | synchronize_with_editor = PsPadHook(path, wakeup_duration) |
|
181 | synchronize_with_editor = PsPadHook(path, wakeup_duration) | |
181 | setHook(synchronize_with_editor) |
|
182 | setHook(synchronize_with_editor) | |
182 |
|
183 | |||
183 |
|
184 | |||
184 | # This is an example of DDE hook: |
|
185 | # This is an example of DDE hook: | |
185 | class UltraEditHook: |
|
186 | class UltraEditHook: | |
186 | def __init__(self, path, wakeup_duration, start_duration): |
|
187 | def __init__(self, path, wakeup_duration, start_duration): | |
187 | self.path = path |
|
188 | self.path = path | |
188 | self.wakeup_duration = wakeup_duration |
|
189 | self.wakeup_duration = wakeup_duration | |
189 | self.start_duration = start_duration |
|
190 | self.start_duration = start_duration | |
190 |
|
191 | |||
191 | def __call__(self, ip, filename, lineno, columnno): |
|
192 | def __call__(self, ip, filename, lineno, columnno): | |
192 | filename = findFilename(filename) |
|
193 | filename = findFilename(filename) | |
193 |
|
194 | |||
194 | if not filename: |
|
195 | if not filename: | |
195 | return |
|
196 | return | |
196 |
|
197 | |||
197 | server = dde.CreateServer() |
|
198 | server = dde.CreateServer() | |
198 | server.Create("myddeserver") |
|
199 | server.Create("myddeserver") | |
199 | conversation = dde.CreateConversation(server) |
|
200 | conversation = dde.CreateConversation(server) | |
200 | try: |
|
201 | try: | |
201 | conversation.ConnectTo("uedit32", "System") |
|
202 | conversation.ConnectTo("uedit32", "System") | |
202 | conversation.Exec(r'[open("%s/%d"])' % (filename, lineno)) |
|
203 | conversation.Exec(r'[open("%s/%d"])' % (filename, lineno)) | |
203 |
|
204 | |||
204 | sleep(self.wakeup_duration) |
|
205 | sleep(self.wakeup_duration) | |
205 | except: |
|
206 | except: | |
206 | runCommand(self.path, 'uedit32', '"%s/%d"' % (filename, lineno)) |
|
207 | runCommand(self.path, 'uedit32', '"%s/%d"' % (filename, lineno)) | |
207 |
|
208 | |||
208 | sleep(self.start_duration) |
|
209 | sleep(self.start_duration) | |
209 |
|
210 | |||
210 | server.Shutdown() |
|
211 | server.Shutdown() | |
211 |
|
212 | |||
212 | restoreConsoleFocus() |
|
213 | restoreConsoleFocus() | |
213 |
|
214 | |||
214 |
|
215 | |||
215 | def ultraedit(path = r"C:\Program Files\IDM Computer Solutions\UltraEdit-32", wakeup_duration = 10, start_duration = 2000): |
|
216 | def ultraedit(path = r"C:\Program Files\IDM Computer Solutions\UltraEdit-32", wakeup_duration = 10, start_duration = 2000): | |
216 | synchronize_with_editor = UltraEditHook(path, wakeup_duration, start_duration) |
|
217 | synchronize_with_editor = UltraEditHook(path, wakeup_duration, start_duration) | |
217 | setHook(synchronize_with_editor) |
|
218 | setHook(synchronize_with_editor) | |
218 | No newline at end of file |
|
219 |
General Comments 0
You need to be logged in to leave comments.
Login now