Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 711 lines (594 sloc) 17.594 kB
e5c6ef7 updated for version 7.0001
vimboss authored
1 #include "stdafx.h"
2 #include <comdef.h> // For _bstr_t
3 #include "VisVim.h"
4 #include "Commands.h"
5 #include "OleAut.h"
6
7 #ifdef _DEBUG
8 #define new DEBUG_NEW
9 #undef THIS_FILE
10 static char THIS_FILE[] = __FILE__;
11
12 #endif
13
14
15 // Change directory before opening file?
16 #define CD_SOURCE 0 // Cd to source path
17 #define CD_SOURCE_PARENT 1 // Cd to parent directory of source path
18 #define CD_NONE 2 // No cd
19
20
21 static BOOL g_bEnableVim = TRUE; // Vim enabled
22 static BOOL g_bDevStudioEditor = FALSE; // Open file in Dev Studio editor simultaneously
396732e updated for version 7.2-150
vimboss authored
23 static BOOL g_bNewTabs = FALSE;
e5c6ef7 updated for version 7.0001
vimboss authored
24 static int g_ChangeDir = CD_NONE; // CD after file open?
25
396732e updated for version 7.2-150
vimboss authored
26 static void VimSetEnableState(BOOL bEnableState);
27 static BOOL VimOpenFile(BSTR& FileName, long LineNr);
28 static DISPID VimGetDispatchId(COleAutomationControl& VimOle, char* Method);
29 static void VimErrDiag(COleAutomationControl& VimOle);
30 static void VimChangeDir(COleAutomationControl& VimOle, DISPID DispatchId, BSTR& FileName);
31 static void DebugMsg(char* Msg, char* Arg = NULL);
e5c6ef7 updated for version 7.0001
vimboss authored
32
33
34 /////////////////////////////////////////////////////////////////////////////
35 // CCommands
36
396732e updated for version 7.2-150
vimboss authored
37 CCommands::CCommands()
e5c6ef7 updated for version 7.0001
vimboss authored
38 {
39 // m_pApplication == NULL; M$ Code generation bug!!!
40 m_pApplication = NULL;
41 m_pApplicationEventsObj = NULL;
42 m_pDebuggerEventsObj = NULL;
43 }
44
396732e updated for version 7.2-150
vimboss authored
45 CCommands::~CCommands()
e5c6ef7 updated for version 7.0001
vimboss authored
46 {
396732e updated for version 7.2-150
vimboss authored
47 ASSERT(m_pApplication != NULL);
e5c6ef7 updated for version 7.0001
vimboss authored
48 if (m_pApplication)
49 {
396732e updated for version 7.2-150
vimboss authored
50 m_pApplication->Release();
e5c6ef7 updated for version 7.0001
vimboss authored
51 m_pApplication = NULL;
52 }
53 }
54
396732e updated for version 7.2-150
vimboss authored
55 void CCommands::SetApplicationObject(IApplication * pApplication)
e5c6ef7 updated for version 7.0001
vimboss authored
56 {
57 // This function assumes pApplication has already been AddRef'd
ec01c0f updated for version 7.1b
vimboss authored
58 // for us, which CDSAddIn did in it's QueryInterface call
e5c6ef7 updated for version 7.0001
vimboss authored
59 // just before it called us.
60 m_pApplication = pApplication;
61 if (! m_pApplication)
62 return;
63
64 // Create Application event handlers
396732e updated for version 7.2-150
vimboss authored
65 XApplicationEventsObj::CreateInstance(&m_pApplicationEventsObj);
e5c6ef7 updated for version 7.0001
vimboss authored
66 if (! m_pApplicationEventsObj)
67 {
396732e updated for version 7.2-150
vimboss authored
68 ReportInternalError("XApplicationEventsObj::CreateInstance");
e5c6ef7 updated for version 7.0001
vimboss authored
69 return;
70 }
396732e updated for version 7.2-150
vimboss authored
71 m_pApplicationEventsObj->AddRef();
72 m_pApplicationEventsObj->Connect(m_pApplication);
e5c6ef7 updated for version 7.0001
vimboss authored
73 m_pApplicationEventsObj->m_pCommands = this;
74
75 #ifdef NEVER
76 // Create Debugger event handler
77 CComPtr < IDispatch > pDebugger;
396732e updated for version 7.2-150
vimboss authored
78 if (SUCCEEDED(m_pApplication->get_Debugger(&pDebugger))
e5c6ef7 updated for version 7.0001
vimboss authored
79 && pDebugger != NULL)
80 {
396732e updated for version 7.2-150
vimboss authored
81 XDebuggerEventsObj::CreateInstance(&m_pDebuggerEventsObj);
82 m_pDebuggerEventsObj->AddRef();
83 m_pDebuggerEventsObj->Connect(pDebugger);
e5c6ef7 updated for version 7.0001
vimboss authored
84 m_pDebuggerEventsObj->m_pCommands = this;
85 }
86 #endif
87
88 // Get settings from registry HKEY_CURRENT_USER\Software\Vim\VisVim
396732e updated for version 7.2-150
vimboss authored
89 HKEY hAppKey = GetAppKey("Vim");
e5c6ef7 updated for version 7.0001
vimboss authored
90 if (hAppKey)
91 {
396732e updated for version 7.2-150
vimboss authored
92 HKEY hSectionKey = GetSectionKey(hAppKey, "VisVim");
e5c6ef7 updated for version 7.0001
vimboss authored
93 if (hSectionKey)
94 {
396732e updated for version 7.2-150
vimboss authored
95 g_bEnableVim = GetRegistryInt(hSectionKey, "EnableVim",
e5c6ef7 updated for version 7.0001
vimboss authored
96 g_bEnableVim);
396732e updated for version 7.2-150
vimboss authored
97 g_bDevStudioEditor = GetRegistryInt(hSectionKey,
98 "DevStudioEditor", g_bDevStudioEditor);
99 g_bNewTabs = GetRegistryInt(hSectionKey, "NewTabs",
100 g_bNewTabs);
101 g_ChangeDir = GetRegistryInt(hSectionKey, "ChangeDir",
e5c6ef7 updated for version 7.0001
vimboss authored
102 g_ChangeDir);
396732e updated for version 7.2-150
vimboss authored
103 RegCloseKey(hSectionKey);
e5c6ef7 updated for version 7.0001
vimboss authored
104 }
396732e updated for version 7.2-150
vimboss authored
105 RegCloseKey(hAppKey);
e5c6ef7 updated for version 7.0001
vimboss authored
106 }
107 }
108
396732e updated for version 7.2-150
vimboss authored
109 void CCommands::UnadviseFromEvents()
e5c6ef7 updated for version 7.0001
vimboss authored
110 {
396732e updated for version 7.2-150
vimboss authored
111 ASSERT(m_pApplicationEventsObj != NULL);
e5c6ef7 updated for version 7.0001
vimboss authored
112 if (m_pApplicationEventsObj)
113 {
396732e updated for version 7.2-150
vimboss authored
114 m_pApplicationEventsObj->Disconnect(m_pApplication);
115 m_pApplicationEventsObj->Release();
e5c6ef7 updated for version 7.0001
vimboss authored
116 m_pApplicationEventsObj = NULL;
117 }
118
119 #ifdef NEVER
120 if (m_pDebuggerEventsObj)
121 {
122 // Since we were able to connect to the Debugger events, we
123 // should be able to access the Debugger object again to
124 // unadvise from its events (thus the VERIFY_OK below--see
125 // stdafx.h).
126 CComPtr < IDispatch > pDebugger;
396732e updated for version 7.2-150
vimboss authored
127 VERIFY_OK(m_pApplication->get_Debugger(&pDebugger));
128 ASSERT(pDebugger != NULL);
129 m_pDebuggerEventsObj->Disconnect(pDebugger);
130 m_pDebuggerEventsObj->Release();
e5c6ef7 updated for version 7.0001
vimboss authored
131 m_pDebuggerEventsObj = NULL;
132 }
133 #endif
134 }
135
136
137 /////////////////////////////////////////////////////////////////////////////
138 // Event handlers
139
140 // Application events
141
396732e updated for version 7.2-150
vimboss authored
142 HRESULT CCommands::XApplicationEvents::BeforeBuildStart()
e5c6ef7 updated for version 7.0001
vimboss authored
143 {
396732e updated for version 7.2-150
vimboss authored
144 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
145 return S_OK;
146 }
147
396732e updated for version 7.2-150
vimboss authored
148 HRESULT CCommands::XApplicationEvents::BuildFinish(long nNumErrors, long nNumWarnings)
e5c6ef7 updated for version 7.0001
vimboss authored
149 {
396732e updated for version 7.2-150
vimboss authored
150 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
151 return S_OK;
152 }
153
396732e updated for version 7.2-150
vimboss authored
154 HRESULT CCommands::XApplicationEvents::BeforeApplicationShutDown()
e5c6ef7 updated for version 7.0001
vimboss authored
155 {
396732e updated for version 7.2-150
vimboss authored
156 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
157 return S_OK;
158 }
159
160 // The open document event handle is the place where the real interface work
161 // is done.
162 // Vim gets called from here.
163 //
396732e updated for version 7.2-150
vimboss authored
164 HRESULT CCommands::XApplicationEvents::DocumentOpen(IDispatch * theDocument)
e5c6ef7 updated for version 7.0001
vimboss authored
165 {
396732e updated for version 7.2-150
vimboss authored
166 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
167
168 if (! g_bEnableVim)
169 // Vim not enabled or empty command line entered
170 return S_OK;
171
172 // First get the current file name and line number
173
174 // Get the document object
396732e updated for version 7.2-150
vimboss authored
175 CComQIPtr < ITextDocument, &IID_ITextDocument > pDoc(theDocument);
e5c6ef7 updated for version 7.0001
vimboss authored
176 if (! pDoc)
177 return S_OK;
178
179 BSTR FileName;
180 long LineNr = -1;
181
182 // Get the document name
396732e updated for version 7.2-150
vimboss authored
183 if (FAILED(pDoc->get_FullName(&FileName)))
e5c6ef7 updated for version 7.0001
vimboss authored
184 return S_OK;
185
186 LPDISPATCH pDispSel;
187
188 // Get a selection object dispatch pointer
396732e updated for version 7.2-150
vimboss authored
189 if (SUCCEEDED(pDoc->get_Selection(&pDispSel)))
e5c6ef7 updated for version 7.0001
vimboss authored
190 {
191 // Get the selection object
396732e updated for version 7.2-150
vimboss authored
192 CComQIPtr < ITextSelection, &IID_ITextSelection > pSel(pDispSel);
e5c6ef7 updated for version 7.0001
vimboss authored
193
194 if (pSel)
195 // Get the selection line number
396732e updated for version 7.2-150
vimboss authored
196 pSel->get_CurrentLine(&LineNr);
e5c6ef7 updated for version 7.0001
vimboss authored
197
396732e updated for version 7.2-150
vimboss authored
198 pDispSel->Release();
e5c6ef7 updated for version 7.0001
vimboss authored
199 }
200
201 // Open the file in Vim and position to the current line
396732e updated for version 7.2-150
vimboss authored
202 if (VimOpenFile(FileName, LineNr))
e5c6ef7 updated for version 7.0001
vimboss authored
203 {
204 if (! g_bDevStudioEditor)
205 {
206 // Close the document in developer studio
207 CComVariant vSaveChanges = dsSaveChangesPrompt;
208 DsSaveStatus Saved;
209
396732e updated for version 7.2-150
vimboss authored
210 pDoc->Close(vSaveChanges, &Saved);
e5c6ef7 updated for version 7.0001
vimboss authored
211 }
212 }
213
214 // We're done here
396732e updated for version 7.2-150
vimboss authored
215 SysFreeString(FileName);
e5c6ef7 updated for version 7.0001
vimboss authored
216 return S_OK;
217 }
218
396732e updated for version 7.2-150
vimboss authored
219 HRESULT CCommands::XApplicationEvents::BeforeDocumentClose(IDispatch * theDocument)
e5c6ef7 updated for version 7.0001
vimboss authored
220 {
396732e updated for version 7.2-150
vimboss authored
221 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
222 return S_OK;
223 }
224
396732e updated for version 7.2-150
vimboss authored
225 HRESULT CCommands::XApplicationEvents::DocumentSave(IDispatch * theDocument)
e5c6ef7 updated for version 7.0001
vimboss authored
226 {
396732e updated for version 7.2-150
vimboss authored
227 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
228 return S_OK;
229 }
230
396732e updated for version 7.2-150
vimboss authored
231 HRESULT CCommands::XApplicationEvents::NewDocument(IDispatch * theDocument)
e5c6ef7 updated for version 7.0001
vimboss authored
232 {
396732e updated for version 7.2-150
vimboss authored
233 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
234
235 if (! g_bEnableVim)
236 // Vim not enabled or empty command line entered
237 return S_OK;
238
239 // First get the current file name and line number
240
396732e updated for version 7.2-150
vimboss authored
241 CComQIPtr < ITextDocument, &IID_ITextDocument > pDoc(theDocument);
e5c6ef7 updated for version 7.0001
vimboss authored
242 if (! pDoc)
243 return S_OK;
244
245 BSTR FileName;
246 HRESULT hr;
247
396732e updated for version 7.2-150
vimboss authored
248 hr = pDoc->get_FullName(&FileName);
249 if (FAILED(hr))
e5c6ef7 updated for version 7.0001
vimboss authored
250 return S_OK;
251
252 // Open the file in Vim and position to the current line
396732e updated for version 7.2-150
vimboss authored
253 if (VimOpenFile(FileName, 0))
e5c6ef7 updated for version 7.0001
vimboss authored
254 {
255 if (! g_bDevStudioEditor)
256 {
257 // Close the document in developer studio
258 CComVariant vSaveChanges = dsSaveChangesPrompt;
259 DsSaveStatus Saved;
260
396732e updated for version 7.2-150
vimboss authored
261 pDoc->Close(vSaveChanges, &Saved);
e5c6ef7 updated for version 7.0001
vimboss authored
262 }
263 }
264
396732e updated for version 7.2-150
vimboss authored
265 SysFreeString(FileName);
e5c6ef7 updated for version 7.0001
vimboss authored
266 return S_OK;
267 }
268
396732e updated for version 7.2-150
vimboss authored
269 HRESULT CCommands::XApplicationEvents::WindowActivate(IDispatch * theWindow)
e5c6ef7 updated for version 7.0001
vimboss authored
270 {
396732e updated for version 7.2-150
vimboss authored
271 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
272 return S_OK;
273 }
274
396732e updated for version 7.2-150
vimboss authored
275 HRESULT CCommands::XApplicationEvents::WindowDeactivate(IDispatch * theWindow)
e5c6ef7 updated for version 7.0001
vimboss authored
276 {
396732e updated for version 7.2-150
vimboss authored
277 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
278 return S_OK;
279 }
280
396732e updated for version 7.2-150
vimboss authored
281 HRESULT CCommands::XApplicationEvents::WorkspaceOpen()
e5c6ef7 updated for version 7.0001
vimboss authored
282 {
396732e updated for version 7.2-150
vimboss authored
283 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
284 return S_OK;
285 }
286
396732e updated for version 7.2-150
vimboss authored
287 HRESULT CCommands::XApplicationEvents::WorkspaceClose()
e5c6ef7 updated for version 7.0001
vimboss authored
288 {
396732e updated for version 7.2-150
vimboss authored
289 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
290 return S_OK;
291 }
292
396732e updated for version 7.2-150
vimboss authored
293 HRESULT CCommands::XApplicationEvents::NewWorkspace()
e5c6ef7 updated for version 7.0001
vimboss authored
294 {
396732e updated for version 7.2-150
vimboss authored
295 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
296 return S_OK;
297 }
298
299 // Debugger event
300
396732e updated for version 7.2-150
vimboss authored
301 HRESULT CCommands::XDebuggerEvents::BreakpointHit(IDispatch * pBreakpoint)
e5c6ef7 updated for version 7.0001
vimboss authored
302 {
396732e updated for version 7.2-150
vimboss authored
303 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
304 return S_OK;
305 }
306
307
308 /////////////////////////////////////////////////////////////////////////////
309 // VisVim dialog
310
311 class CMainDialog : public CDialog
312 {
313 public:
396732e updated for version 7.2-150
vimboss authored
314 CMainDialog(CWnd * pParent = NULL); // Standard constructor
e5c6ef7 updated for version 7.0001
vimboss authored
315
316 //{{AFX_DATA(CMainDialog)
317 enum { IDD = IDD_ADDINMAIN };
318 int m_ChangeDir;
319 BOOL m_bDevStudioEditor;
396732e updated for version 7.2-150
vimboss authored
320 BOOL m_bNewTabs;
e5c6ef7 updated for version 7.0001
vimboss authored
321 //}}AFX_DATA
322
323 //{{AFX_VIRTUAL(CMainDialog)
324 protected:
396732e updated for version 7.2-150
vimboss authored
325 virtual void DoDataExchange(CDataExchange * pDX); // DDX/DDV support
e5c6ef7 updated for version 7.0001
vimboss authored
326 //}}AFX_VIRTUAL
327
328 protected:
329 //{{AFX_MSG(CMainDialog)
330 afx_msg void OnEnable();
331 afx_msg void OnDisable();
332 //}}AFX_MSG
396732e updated for version 7.2-150
vimboss authored
333 DECLARE_MESSAGE_MAP()
e5c6ef7 updated for version 7.0001
vimboss authored
334 };
335
396732e updated for version 7.2-150
vimboss authored
336 CMainDialog::CMainDialog(CWnd * pParent /* =NULL */ )
337 : CDialog(CMainDialog::IDD, pParent)
e5c6ef7 updated for version 7.0001
vimboss authored
338 {
339 //{{AFX_DATA_INIT(CMainDialog)
340 m_ChangeDir = -1;
341 m_bDevStudioEditor = FALSE;
396732e updated for version 7.2-150
vimboss authored
342 m_bNewTabs = FALSE;
e5c6ef7 updated for version 7.0001
vimboss authored
343 //}}AFX_DATA_INIT
344 }
345
396732e updated for version 7.2-150
vimboss authored
346 void CMainDialog::DoDataExchange(CDataExchange * pDX)
e5c6ef7 updated for version 7.0001
vimboss authored
347 {
396732e updated for version 7.2-150
vimboss authored
348 CDialog::DoDataExchange(pDX);
e5c6ef7 updated for version 7.0001
vimboss authored
349 //{{AFX_DATA_MAP(CMainDialog)
350 DDX_Radio(pDX, IDC_CD_SOURCE_PATH, m_ChangeDir);
396732e updated for version 7.2-150
vimboss authored
351 DDX_Check(pDX, IDC_DEVSTUDIO_EDITOR, m_bDevStudioEditor);
352 DDX_Check(pDX, IDC_NEW_TABS, m_bNewTabs);
e5c6ef7 updated for version 7.0001
vimboss authored
353 //}}AFX_DATA_MAP
354 }
355
396732e updated for version 7.2-150
vimboss authored
356 BEGIN_MESSAGE_MAP(CMainDialog, CDialog)
e5c6ef7 updated for version 7.0001
vimboss authored
357 //{{AFX_MSG_MAP(CMainDialog)
358 //}}AFX_MSG_MAP
396732e updated for version 7.2-150
vimboss authored
359 END_MESSAGE_MAP()
e5c6ef7 updated for version 7.0001
vimboss authored
360
361
362 /////////////////////////////////////////////////////////////////////////////
363 // CCommands methods
364
396732e updated for version 7.2-150
vimboss authored
365 STDMETHODIMP CCommands::VisVimDialog()
e5c6ef7 updated for version 7.0001
vimboss authored
366 {
396732e updated for version 7.2-150
vimboss authored
367 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
368
369 // Use m_pApplication to access the Developer Studio Application
370 // object,
371 // and VERIFY_OK to see error strings in DEBUG builds of your add-in
372 // (see stdafx.h)
373
396732e updated for version 7.2-150
vimboss authored
374 VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
e5c6ef7 updated for version 7.0001
vimboss authored
375
376 CMainDialog Dlg;
377
378 Dlg.m_bDevStudioEditor = g_bDevStudioEditor;
396732e updated for version 7.2-150
vimboss authored
379 Dlg.m_bNewTabs = g_bNewTabs;
e5c6ef7 updated for version 7.0001
vimboss authored
380 Dlg.m_ChangeDir = g_ChangeDir;
396732e updated for version 7.2-150
vimboss authored
381 if (Dlg.DoModal() == IDOK)
e5c6ef7 updated for version 7.0001
vimboss authored
382 {
383 g_bDevStudioEditor = Dlg.m_bDevStudioEditor;
396732e updated for version 7.2-150
vimboss authored
384 g_bNewTabs = Dlg.m_bNewTabs;
e5c6ef7 updated for version 7.0001
vimboss authored
385 g_ChangeDir = Dlg.m_ChangeDir;
386
387 // Save settings to registry HKEY_CURRENT_USER\Software\Vim\VisVim
396732e updated for version 7.2-150
vimboss authored
388 HKEY hAppKey = GetAppKey("Vim");
e5c6ef7 updated for version 7.0001
vimboss authored
389 if (hAppKey)
390 {
396732e updated for version 7.2-150
vimboss authored
391 HKEY hSectionKey = GetSectionKey(hAppKey, "VisVim");
e5c6ef7 updated for version 7.0001
vimboss authored
392 if (hSectionKey)
393 {
396732e updated for version 7.2-150
vimboss authored
394 WriteRegistryInt(hSectionKey, "DevStudioEditor",
e5c6ef7 updated for version 7.0001
vimboss authored
395 g_bDevStudioEditor);
396732e updated for version 7.2-150
vimboss authored
396 WriteRegistryInt(hSectionKey, "NewTabs",
397 g_bNewTabs);
398 WriteRegistryInt(hSectionKey, "ChangeDir", g_ChangeDir);
399 RegCloseKey(hSectionKey);
e5c6ef7 updated for version 7.0001
vimboss authored
400 }
396732e updated for version 7.2-150
vimboss authored
401 RegCloseKey(hAppKey);
e5c6ef7 updated for version 7.0001
vimboss authored
402 }
403 }
404
396732e updated for version 7.2-150
vimboss authored
405 VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
e5c6ef7 updated for version 7.0001
vimboss authored
406 return S_OK;
407 }
408
396732e updated for version 7.2-150
vimboss authored
409 STDMETHODIMP CCommands::VisVimEnable()
e5c6ef7 updated for version 7.0001
vimboss authored
410 {
396732e updated for version 7.2-150
vimboss authored
411 AFX_MANAGE_STATE(AfxGetStaticModuleState());
412 VimSetEnableState(true);
e5c6ef7 updated for version 7.0001
vimboss authored
413 return S_OK;
414 }
415
396732e updated for version 7.2-150
vimboss authored
416 STDMETHODIMP CCommands::VisVimDisable()
e5c6ef7 updated for version 7.0001
vimboss authored
417 {
396732e updated for version 7.2-150
vimboss authored
418 AFX_MANAGE_STATE(AfxGetStaticModuleState());
419 VimSetEnableState(false);
e5c6ef7 updated for version 7.0001
vimboss authored
420 return S_OK;
421 }
422
396732e updated for version 7.2-150
vimboss authored
423 STDMETHODIMP CCommands::VisVimToggle()
e5c6ef7 updated for version 7.0001
vimboss authored
424 {
396732e updated for version 7.2-150
vimboss authored
425 AFX_MANAGE_STATE(AfxGetStaticModuleState());
426 VimSetEnableState(! g_bEnableVim);
e5c6ef7 updated for version 7.0001
vimboss authored
427 return S_OK;
428 }
429
396732e updated for version 7.2-150
vimboss authored
430 STDMETHODIMP CCommands::VisVimLoad()
e5c6ef7 updated for version 7.0001
vimboss authored
431 {
396732e updated for version 7.2-150
vimboss authored
432 AFX_MANAGE_STATE(AfxGetStaticModuleState());
e5c6ef7 updated for version 7.0001
vimboss authored
433
434 // Use m_pApplication to access the Developer Studio Application object,
435 // and VERIFY_OK to see error strings in DEBUG builds of your add-in
436 // (see stdafx.h)
437
438 CComBSTR bStr;
439 // Define dispatch pointers for document and selection objects
440 CComPtr < IDispatch > pDispDoc, pDispSel;
441
442 // Get a document object dispatch pointer
396732e updated for version 7.2-150
vimboss authored
443 VERIFY_OK(m_pApplication->get_ActiveDocument(&pDispDoc));
e5c6ef7 updated for version 7.0001
vimboss authored
444 if (! pDispDoc)
445 return S_OK;
446
447 BSTR FileName;
448 long LineNr = -1;
449
450 // Get the document object
396732e updated for version 7.2-150
vimboss authored
451 CComQIPtr < ITextDocument, &IID_ITextDocument > pDoc(pDispDoc);
e5c6ef7 updated for version 7.0001
vimboss authored
452
453 if (! pDoc)
454 return S_OK;
455
456 // Get the document name
396732e updated for version 7.2-150
vimboss authored
457 if (FAILED(pDoc->get_FullName(&FileName)))
e5c6ef7 updated for version 7.0001
vimboss authored
458 return S_OK;
459
460 // Get a selection object dispatch pointer
396732e updated for version 7.2-150
vimboss authored
461 if (SUCCEEDED(pDoc->get_Selection(&pDispSel)))
e5c6ef7 updated for version 7.0001
vimboss authored
462 {
463 // Get the selection object
396732e updated for version 7.2-150
vimboss authored
464 CComQIPtr < ITextSelection, &IID_ITextSelection > pSel(pDispSel);
e5c6ef7 updated for version 7.0001
vimboss authored
465
466 if (pSel)
467 // Get the selection line number
396732e updated for version 7.2-150
vimboss authored
468 pSel->get_CurrentLine(&LineNr);
e5c6ef7 updated for version 7.0001
vimboss authored
469 }
470
471 // Open the file in Vim
396732e updated for version 7.2-150
vimboss authored
472 VimOpenFile(FileName, LineNr);
e5c6ef7 updated for version 7.0001
vimboss authored
473
396732e updated for version 7.2-150
vimboss authored
474 SysFreeString(FileName);
e5c6ef7 updated for version 7.0001
vimboss authored
475 return S_OK;
476 }
477
478
479 //
480 // Here we do the actual processing and communication with Vim
481 //
482
483 // Set the enable state and save to registry
484 //
396732e updated for version 7.2-150
vimboss authored
485 static void VimSetEnableState(BOOL bEnableState)
e5c6ef7 updated for version 7.0001
vimboss authored
486 {
487 g_bEnableVim = bEnableState;
396732e updated for version 7.2-150
vimboss authored
488 HKEY hAppKey = GetAppKey("Vim");
e5c6ef7 updated for version 7.0001
vimboss authored
489 if (hAppKey)
490 {
396732e updated for version 7.2-150
vimboss authored
491 HKEY hSectionKey = GetSectionKey(hAppKey, "VisVim");
e5c6ef7 updated for version 7.0001
vimboss authored
492 if (hSectionKey)
396732e updated for version 7.2-150
vimboss authored
493 WriteRegistryInt(hSectionKey, "EnableVim", g_bEnableVim);
494 RegCloseKey(hAppKey);
e5c6ef7 updated for version 7.0001
vimboss authored
495 }
496 }
497
498 // Open the file 'FileName' in Vim and goto line 'LineNr'
499 // 'FileName' is expected to contain an absolute DOS path including the drive
500 // letter.
501 // 'LineNr' must contain a valid line number or 0, e. g. for a new file
502 //
396732e updated for version 7.2-150
vimboss authored
503 static BOOL VimOpenFile(BSTR& FileName, long LineNr)
e5c6ef7 updated for version 7.0001
vimboss authored
504 {
505
506 // OLE automation object for com. with Vim
ec01c0f updated for version 7.1b
vimboss authored
507 // When the object goes out of scope, it's destructor destroys the OLE
508 // connection;
509 // This is important to avoid blocking the object
e5c6ef7 updated for version 7.0001
vimboss authored
510 // (in this memory corruption would be likely when terminating Vim
511 // while still running DevStudio).
512 // So keep this object local!
513 COleAutomationControl VimOle;
514
515 // :cd D:/Src2/VisVim/
516 //
517 // Get a dispatch id for the SendKeys method of Vim;
518 // enables connection to Vim if necessary
519 DISPID DispatchId;
396732e updated for version 7.2-150
vimboss authored
520 DispatchId = VimGetDispatchId(VimOle, "SendKeys");
e5c6ef7 updated for version 7.0001
vimboss authored
521 if (! DispatchId)
522 // OLE error, can't obtain dispatch id
523 goto OleError;
524
525 OLECHAR Buf[MAX_OLE_STR];
526 char FileNameTmp[MAX_OLE_STR];
527 char VimCmd[MAX_OLE_STR];
528 char *s, *p;
529
530 // Prepend CTRL-\ CTRL-N to exit insert mode
531 VimCmd[0] = 0x1c;
532 VimCmd[1] = 0x0e;
533 VimCmd[2] = 0;
534
535 #ifdef SINGLE_WINDOW
536 // Update the current file in Vim if it has been modified.
537 // Disabled, because it could write the file when you don't want to.
396732e updated for version 7.2-150
vimboss authored
538 sprintf(VimCmd + 2, ":up\n");
e5c6ef7 updated for version 7.0001
vimboss authored
539 #endif
396732e updated for version 7.2-150
vimboss authored
540 if (! VimOle.Method(DispatchId, "s", TO_OLE_STR_BUF(VimCmd, Buf)))
e5c6ef7 updated for version 7.0001
vimboss authored
541 goto OleError;
542
543 // Change Vim working directory to where the file is if desired
544 if (g_ChangeDir != CD_NONE)
396732e updated for version 7.2-150
vimboss authored
545 VimChangeDir(VimOle, DispatchId, FileName);
e5c6ef7 updated for version 7.0001
vimboss authored
546
547 // Make Vim open the file.
548 // In the filename convert all \ to /, put a \ before a space.
396732e updated for version 7.2-150
vimboss authored
549 if (g_bNewTabs)
550 {
551 sprintf(VimCmd, ":tab drop ");
4cc118b updated for version 7.3.278
Bram Moolenaar authored
552 s = VimCmd + 10;
396732e updated for version 7.2-150
vimboss authored
553 }
554 else
555 {
556 sprintf(VimCmd, ":drop ");
557 s = VimCmd + 6;
558 }
e5c6ef7 updated for version 7.0001
vimboss authored
559 sprintf(FileNameTmp, "%S", (char *)FileName);
4cc118b updated for version 7.3.278
Bram Moolenaar authored
560 for (p = FileNameTmp; *p != '\0' && s < VimCmd + MAX_OLE_STR - 4; ++p)
e5c6ef7 updated for version 7.0001
vimboss authored
561 if (*p == '\\')
562 *s++ = '/';
563 else
564 {
565 if (*p == ' ')
566 *s++ = '\\';
567 *s++ = *p;
568 }
569 *s++ = '\n';
570 *s = '\0';
571
396732e updated for version 7.2-150
vimboss authored
572 if (! VimOle.Method(DispatchId, "s", TO_OLE_STR_BUF(VimCmd, Buf)))
e5c6ef7 updated for version 7.0001
vimboss authored
573 goto OleError;
574
575 if (LineNr > 0)
576 {
577 // Goto line
396732e updated for version 7.2-150
vimboss authored
578 sprintf(VimCmd, ":%d\n", LineNr);
579 if (! VimOle.Method(DispatchId, "s", TO_OLE_STR_BUF(VimCmd, Buf)))
e5c6ef7 updated for version 7.0001
vimboss authored
580 goto OleError;
581 }
582
583 // Make Vim come to the foreground
396732e updated for version 7.2-150
vimboss authored
584 if (! VimOle.Method("SetForeground"))
585 VimOle.ErrDiag();
e5c6ef7 updated for version 7.0001
vimboss authored
586
587 // We're done
588 return true;
589
590 OleError:
591 // There was an OLE error
592 // Check if it's the "unknown class string" error
396732e updated for version 7.2-150
vimboss authored
593 VimErrDiag(VimOle);
e5c6ef7 updated for version 7.0001
vimboss authored
594 return false;
595 }
596
597 // Return the dispatch id for the Vim method 'Method'
598 // Create the Vim OLE object if necessary
599 // Returns a valid dispatch id or null on error
600 //
396732e updated for version 7.2-150
vimboss authored
601 static DISPID VimGetDispatchId(COleAutomationControl& VimOle, char* Method)
e5c6ef7 updated for version 7.0001
vimboss authored
602 {
603 // Initialize Vim OLE connection if not already done
396732e updated for version 7.2-150
vimboss authored
604 if (! VimOle.IsCreated())
e5c6ef7 updated for version 7.0001
vimboss authored
605 {
396732e updated for version 7.2-150
vimboss authored
606 if (! VimOle.CreateObject("Vim.Application"))
e5c6ef7 updated for version 7.0001
vimboss authored
607 return NULL;
608 }
609
610 // Get the dispatch id for the SendKeys method.
611 // By doing this, we are checking if Vim is still there...
396732e updated for version 7.2-150
vimboss authored
612 DISPID DispatchId = VimOle.GetDispatchId("SendKeys");
e5c6ef7 updated for version 7.0001
vimboss authored
613 if (! DispatchId)
614 {
615 // We can't get a dispatch id.
616 // This means that probably Vim has been terminated.
617 // Don't issue an error message here, instead
618 // destroy the OLE object and try to connect once more
619 //
620 // In fact, this should never happen, because the OLE aut. object
621 // should not be kept long enough to allow the user to terminate Vim
622 // to avoid memory corruption (why the heck is there no system garbage
623 // collection for those damned OLE memory chunks???).
396732e updated for version 7.2-150
vimboss authored
624 VimOle.DeleteObject();
625 if (! VimOle.CreateObject("Vim.Application"))
e5c6ef7 updated for version 7.0001
vimboss authored
626 // If this create fails, it's time for an error msg
627 return NULL;
628
396732e updated for version 7.2-150
vimboss authored
629 if (! (DispatchId = VimOle.GetDispatchId("SendKeys")))
e5c6ef7 updated for version 7.0001
vimboss authored
630 // There is something wrong...
631 return NULL;
632 }
633
634 return DispatchId;
635 }
636
637 // Output an error message for an OLE error
638 // Check on the classstring error, which probably means Vim wasn't registered.
639 //
396732e updated for version 7.2-150
vimboss authored
640 static void VimErrDiag(COleAutomationControl& VimOle)
e5c6ef7 updated for version 7.0001
vimboss authored
641 {
396732e updated for version 7.2-150
vimboss authored
642 SCODE sc = GetScode(VimOle.GetResult());
e5c6ef7 updated for version 7.0001
vimboss authored
643 if (sc == CO_E_CLASSSTRING)
644 {
645 char Buf[256];
396732e updated for version 7.2-150
vimboss authored
646 sprintf(Buf, "There is no registered OLE automation server named "
e5c6ef7 updated for version 7.0001
vimboss authored
647 "\"Vim.Application\".\n"
648 "Use the OLE-enabled version of Vim with VisVim and "
649 "make sure to register Vim by running \"vim -register\".");
396732e updated for version 7.2-150
vimboss authored
650 MessageBox(NULL, Buf, "OLE Error", MB_OK);
e5c6ef7 updated for version 7.0001
vimboss authored
651 }
652 else
396732e updated for version 7.2-150
vimboss authored
653 VimOle.ErrDiag();
e5c6ef7 updated for version 7.0001
vimboss authored
654 }
655
656 // Change directory to the directory the file 'FileName' is in or it's parent
657 // directory according to the setting of the global 'g_ChangeDir':
658 // 'FileName' is expected to contain an absolute DOS path including the drive
659 // letter.
660 // CD_NONE
661 // CD_SOURCE_PATH
662 // CD_SOURCE_PARENT
663 //
396732e updated for version 7.2-150
vimboss authored
664 static void VimChangeDir(COleAutomationControl& VimOle, DISPID DispatchId, BSTR& FileName)
e5c6ef7 updated for version 7.0001
vimboss authored
665 {
666 // Do a :cd first
667
668 // Get the path name of the file ("dir/")
669 CString StrFileName = FileName;
670 char Drive[_MAX_DRIVE];
671 char Dir[_MAX_DIR];
672 char DirUnix[_MAX_DIR * 2];
673 char *s, *t;
674
396732e updated for version 7.2-150
vimboss authored
675 _splitpath(StrFileName, Drive, Dir, NULL, NULL);
e5c6ef7 updated for version 7.0001
vimboss authored
676
677 // Convert to Unix path name format, escape spaces.
678 t = DirUnix;
679 for (s = Dir; *s; ++s)
680 if (*s == '\\')
681 *t++ = '/';
682 else
683 {
684 if (*s == ' ')
685 *t++ = '\\';
686 *t++ = *s;
687 }
688 *t = '\0';
689
690
691 // Construct the cd command; append /.. if cd to parent
692 // directory and not in root directory
693 OLECHAR Buf[MAX_OLE_STR];
694 char VimCmd[MAX_OLE_STR];
695
396732e updated for version 7.2-150
vimboss authored
696 sprintf(VimCmd, ":cd %s%s%s\n", Drive, DirUnix,
e5c6ef7 updated for version 7.0001
vimboss authored
697 g_ChangeDir == CD_SOURCE_PARENT && DirUnix[1] ? ".." : "");
396732e updated for version 7.2-150
vimboss authored
698 VimOle.Method(DispatchId, "s", TO_OLE_STR_BUF(VimCmd, Buf));
e5c6ef7 updated for version 7.0001
vimboss authored
699 }
700
701 #ifdef _DEBUG
702 // Print out a debug message
703 //
396732e updated for version 7.2-150
vimboss authored
704 static void DebugMsg(char* Msg, char* Arg)
e5c6ef7 updated for version 7.0001
vimboss authored
705 {
706 char Buf[400];
396732e updated for version 7.2-150
vimboss authored
707 sprintf(Buf, Msg, Arg);
708 AfxMessageBox(Buf);
e5c6ef7 updated for version 7.0001
vimboss authored
709 }
710 #endif
Something went wrong with that request. Please try again.