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