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