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