blob: 55d5a76338271301532f654a0d9e36e3ada47d9a [file] [log] [blame]
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved gvimext by Tianmiao Hu
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * gvimext is a DLL which is used for the "Edit with Vim" context menu
11 * extension. It implements a MS defined interface with the Shell.
12 *
13 * If you have any questions or any suggestions concerning gvimext, please
14 * contact Tianmiao Hu: tianmiao@acm.org.
15 */
16
17#include "gvimext.h"
18
19#ifdef __BORLANDC__
20# include <dir.h>
21# ifndef _strnicmp
22# define _strnicmp(a, b, c) strnicmp((a), (b), (c))
23# endif
24#else
25static char *searchpath(char *name);
26#endif
27
28// Always get an error while putting the following stuff to the
29// gvimext.h file as class protected variables, give up and
30// declare them as global stuff
31FORMATETC fmte = {CF_HDROP,
32 (DVTARGETDEVICE FAR *)NULL,
33 DVASPECT_CONTENT,
34 -1,
35 TYMED_HGLOBAL
36 };
37STGMEDIUM medium;
38HRESULT hres = 0;
39UINT cbFiles = 0;
40
Bram Moolenaare759a7a2005-07-12 22:50:18 +000041/* The buffers size used to be MAX_PATH (256 bytes), but that's not always
42 * enough */
43#define BUFSIZE 1100
44
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000045//
46// Get the name of the Gvim executable to use, with the path.
47// When "runtime" is non-zero, we were called to find the runtime directory.
Bram Moolenaare759a7a2005-07-12 22:50:18 +000048// Returns the path in name[BUFSIZE]. It's empty when it fails.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000049//
50 static void
51getGvimName(char *name, int runtime)
52{
53 HKEY keyhandle;
54 DWORD hlen;
55
56 // Get the location of gvim from the registry.
57 name[0] = 0;
58 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", 0,
59 KEY_READ, &keyhandle) == ERROR_SUCCESS)
60 {
Bram Moolenaare759a7a2005-07-12 22:50:18 +000061 hlen = BUFSIZE;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000062 if (RegQueryValueEx(keyhandle, "path", 0, NULL, (BYTE *)name, &hlen)
63 != ERROR_SUCCESS)
64 name[0] = 0;
65 else
66 name[hlen] = 0;
67 RegCloseKey(keyhandle);
68 }
69
70 // Registry didn't work, use the search path.
71 if (name[0] == 0)
Bram Moolenaar7baa45d2007-08-18 15:00:42 +000072 strcpy(name, searchpath((char *)"gvim.exe"));
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000073
74 if (!runtime)
75 {
76 // Only when looking for the executable, not the runtime dir, we can
77 // search for the batch file or a name without a path.
78 if (name[0] == 0)
Bram Moolenaar7baa45d2007-08-18 15:00:42 +000079 strcpy(name, searchpath((char *)"gvim.bat"));
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000080 if (name[0] == 0)
81 strcpy(name, "gvim"); // finds gvim.bat or gvim.exe
82
83 // avoid that Vim tries to expand wildcards in the file names
84 strcat(name, " --literal");
85 }
86}
87
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +020088 static void
89getGvimNameW(wchar_t *nameW)
90{
91 char *name;
92
93 name = (char *)malloc(BUFSIZE);
94 getGvimName(name, 0);
95 mbstowcs(nameW, name, BUFSIZE);
96 free(name);
97}
98
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000099//
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000100// Get the Vim runtime directory into buf[BUFSIZE].
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000101// The result is empty when it failed.
102// When it works, the path ends in a slash or backslash.
103//
104 static void
105getRuntimeDir(char *buf)
106{
107 int idx;
108
109 getGvimName(buf, 1);
110 if (buf[0] != 0)
111 {
112 // When no path found, use the search path to expand it.
113 if (strchr(buf, '/') == NULL && strchr(buf, '\\') == NULL)
114 strcpy(buf, searchpath(buf));
115
116 // remove "gvim.exe" from the end
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000117 for (idx = (int)strlen(buf) - 1; idx >= 0; idx--)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000118 if (buf[idx] == '\\' || buf[idx] == '/')
119 {
120 buf[idx + 1] = 0;
121 break;
122 }
123 }
124}
125
126//
127// GETTEXT: translated messages and menu entries
128//
129#ifndef FEAT_GETTEXT
130# define _(x) x
131#else
132# define _(x) (*dyn_libintl_gettext)(x)
133# define VIMPACKAGE "vim"
134# ifndef GETTEXT_DLL
135# define GETTEXT_DLL "libintl.dll"
136# endif
137
138// Dummy functions
139static char *null_libintl_gettext(const char *);
140static char *null_libintl_textdomain(const char *);
141static char *null_libintl_bindtextdomain(const char *, const char *);
142static int dyn_libintl_init(char *dir);
143static void dyn_libintl_end(void);
144
145static HINSTANCE hLibintlDLL = 0;
146static char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
147static char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
148static char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
149 = null_libintl_bindtextdomain;
150
151//
152// Attempt to load libintl.dll. If it doesn't work, use dummy functions.
153// "dir" is the directory where the libintl.dll might be.
154// Return 1 for success, 0 for failure.
155//
156 static int
157dyn_libintl_init(char *dir)
158{
159 int i;
160 static struct
161 {
162 char *name;
163 FARPROC *ptr;
164 } libintl_entry[] =
165 {
Bram Moolenaar7baa45d2007-08-18 15:00:42 +0000166 {(char *)"gettext", (FARPROC*)&dyn_libintl_gettext},
167 {(char *)"textdomain", (FARPROC*)&dyn_libintl_textdomain},
168 {(char *)"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000169 {NULL, NULL}
170 };
171
172 // No need to initialize twice.
173 if (hLibintlDLL)
174 return 1;
175
176 // Load gettext library, first try the Vim runtime directory, then search
177 // the path.
178 strcat(dir, GETTEXT_DLL);
179 hLibintlDLL = LoadLibrary(dir);
180 if (!hLibintlDLL)
181 {
182 hLibintlDLL = LoadLibrary(GETTEXT_DLL);
183 if (!hLibintlDLL)
184 return 0;
185 }
186
187 // Get the addresses of the functions we need.
188 for (i = 0; libintl_entry[i].name != NULL
189 && libintl_entry[i].ptr != NULL; ++i)
190 {
191 if ((*libintl_entry[i].ptr = GetProcAddress(hLibintlDLL,
192 libintl_entry[i].name)) == NULL)
193 {
194 dyn_libintl_end();
195 return 0;
196 }
197 }
198 return 1;
199}
200
201 static void
202dyn_libintl_end(void)
203{
204 if (hLibintlDLL)
205 FreeLibrary(hLibintlDLL);
206 hLibintlDLL = NULL;
207 dyn_libintl_gettext = null_libintl_gettext;
208 dyn_libintl_textdomain = null_libintl_textdomain;
209 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
210}
211
212 static char *
213null_libintl_gettext(const char *msgid)
214{
215 return (char *)msgid;
216}
217
218 static char *
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000219null_libintl_bindtextdomain(const char * /* domainname */, const char * /* dirname */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000220{
221 return NULL;
222}
223
224 static char *
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000225null_libintl_textdomain(const char* /* domainname */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000226{
227 return NULL;
228}
229
230//
231// Setup for translating strings.
232//
233 static void
234dyn_gettext_load(void)
235{
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000236 char szBuff[BUFSIZE];
237 char szLang[BUFSIZE];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000238 DWORD len;
239 HKEY keyhandle;
240 int gotlang = 0;
241
242 strcpy(szLang, "LANG=");
243
244 // First try getting the language from the registry, this can be
245 // used to overrule the system language.
246 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", 0,
247 KEY_READ, &keyhandle) == ERROR_SUCCESS)
248 {
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000249 len = BUFSIZE;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000250 if (RegQueryValueEx(keyhandle, "lang", 0, NULL, (BYTE*)szBuff, &len)
251 == ERROR_SUCCESS)
252 {
253 szBuff[len] = 0;
254 strcat(szLang, szBuff);
255 gotlang = 1;
256 }
257 RegCloseKey(keyhandle);
258 }
259
260 if (!gotlang && getenv("LANG") == NULL)
261 {
262 // Get the language from the system.
263 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
264 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
265 // only the first two.
266 len = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000267 (LPTSTR)szBuff, BUFSIZE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000268 if (len >= 2 && _strnicmp(szBuff, "en", 2) != 0)
269 {
270 // There are a few exceptions (probably more)
271 if (_strnicmp(szBuff, "cht", 3) == 0
272 || _strnicmp(szBuff, "zht", 3) == 0)
273 strcpy(szBuff, "zh_TW");
274 else if (_strnicmp(szBuff, "chs", 3) == 0
275 || _strnicmp(szBuff, "zhc", 3) == 0)
276 strcpy(szBuff, "zh_CN");
277 else if (_strnicmp(szBuff, "jp", 2) == 0)
278 strcpy(szBuff, "ja");
279 else
280 szBuff[2] = 0; // truncate to two-letter code
281 strcat(szLang, szBuff);
282 gotlang = 1;
283 }
284 }
285 if (gotlang)
286 putenv(szLang);
287
288 // Try to locate the runtime files. The path is used to find libintl.dll
289 // and the vim.mo files.
290 getRuntimeDir(szBuff);
291 if (szBuff[0] != 0)
292 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000293 len = (DWORD)strlen(szBuff);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000294 if (dyn_libintl_init(szBuff))
295 {
296 strcpy(szBuff + len, "lang");
297
298 (*dyn_libintl_bindtextdomain)(VIMPACKAGE, szBuff);
299 (*dyn_libintl_textdomain)(VIMPACKAGE);
300 }
301 }
302}
303
304 static void
305dyn_gettext_free(void)
306{
307 dyn_libintl_end();
308}
309#endif // FEAT_GETTEXT
310
311//
312// Global variables
313//
314UINT g_cRefThisDll = 0; // Reference count of this DLL.
315HINSTANCE g_hmodThisDll = NULL; // Handle to this DLL itself.
316
317
318//---------------------------------------------------------------------------
319// DllMain
320//---------------------------------------------------------------------------
321extern "C" int APIENTRY
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000322DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /* lpReserved */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000323{
324 switch (dwReason)
325 {
326 case DLL_PROCESS_ATTACH:
327 // Extension DLL one-time initialization
328 g_hmodThisDll = hInstance;
329 break;
330
331 case DLL_PROCESS_DETACH:
332 break;
333 }
334
335 return 1; // ok
336}
337
338 static void
339inc_cRefThisDLL()
340{
341#ifdef FEAT_GETTEXT
342 if (g_cRefThisDll == 0)
343 dyn_gettext_load();
344#endif
345 InterlockedIncrement((LPLONG)&g_cRefThisDll);
346}
347
348 static void
349dec_cRefThisDLL()
350{
351#ifdef FEAT_GETTEXT
352 if (InterlockedDecrement((LPLONG)&g_cRefThisDll) == 0)
353 dyn_gettext_free();
354#else
355 InterlockedDecrement((LPLONG)&g_cRefThisDll);
356#endif
357}
358
359//---------------------------------------------------------------------------
360// DllCanUnloadNow
361//---------------------------------------------------------------------------
362
363STDAPI DllCanUnloadNow(void)
364{
365 return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
366}
367
368STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
369{
370 *ppvOut = NULL;
371
372 if (IsEqualIID(rclsid, CLSID_ShellExtension))
373 {
374 CShellExtClassFactory *pcf = new CShellExtClassFactory;
375
376 return pcf->QueryInterface(riid, ppvOut);
377 }
378
379 return CLASS_E_CLASSNOTAVAILABLE;
380}
381
382CShellExtClassFactory::CShellExtClassFactory()
383{
384 m_cRef = 0L;
385
386 inc_cRefThisDLL();
387}
388
389CShellExtClassFactory::~CShellExtClassFactory()
390{
391 dec_cRefThisDLL();
392}
393
394STDMETHODIMP CShellExtClassFactory::QueryInterface(REFIID riid,
395 LPVOID FAR *ppv)
396{
397 *ppv = NULL;
398
399 // Any interface on this object is the object pointer
400
401 if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
402 {
403 *ppv = (LPCLASSFACTORY)this;
404
405 AddRef();
406
407 return NOERROR;
408 }
409
410 return E_NOINTERFACE;
411}
412
413STDMETHODIMP_(ULONG) CShellExtClassFactory::AddRef()
414{
415 return InterlockedIncrement((LPLONG)&m_cRef);
416}
417
418STDMETHODIMP_(ULONG) CShellExtClassFactory::Release()
419{
420 if (InterlockedDecrement((LPLONG)&m_cRef))
421 return m_cRef;
422
423 delete this;
424
425 return 0L;
426}
427
428STDMETHODIMP CShellExtClassFactory::CreateInstance(LPUNKNOWN pUnkOuter,
429 REFIID riid,
430 LPVOID *ppvObj)
431{
432 *ppvObj = NULL;
433
434 // Shell extensions typically don't support aggregation (inheritance)
435
436 if (pUnkOuter)
437 return CLASS_E_NOAGGREGATION;
438
439 // Create the main shell extension object. The shell will then call
440 // QueryInterface with IID_IShellExtInit--this is how shell extensions are
441 // initialized.
442
443 LPCSHELLEXT pShellExt = new CShellExt(); //Create the CShellExt object
444
445 if (NULL == pShellExt)
446 return E_OUTOFMEMORY;
447
448 return pShellExt->QueryInterface(riid, ppvObj);
449}
450
451
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000452STDMETHODIMP CShellExtClassFactory::LockServer(BOOL /* fLock */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000453{
454 return NOERROR;
455}
456
457// *********************** CShellExt *************************
458CShellExt::CShellExt()
459{
460 m_cRef = 0L;
461 m_pDataObj = NULL;
462
463 inc_cRefThisDLL();
464}
465
466CShellExt::~CShellExt()
467{
468 if (m_pDataObj)
469 m_pDataObj->Release();
470
471 dec_cRefThisDLL();
472}
473
474STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
475{
476 *ppv = NULL;
477
478 if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
479 {
480 *ppv = (LPSHELLEXTINIT)this;
481 }
482 else if (IsEqualIID(riid, IID_IContextMenu))
483 {
484 *ppv = (LPCONTEXTMENU)this;
485 }
486
487 if (*ppv)
488 {
489 AddRef();
490
491 return NOERROR;
492 }
493
494 return E_NOINTERFACE;
495}
496
497STDMETHODIMP_(ULONG) CShellExt::AddRef()
498{
499 return InterlockedIncrement((LPLONG)&m_cRef);
500}
501
502STDMETHODIMP_(ULONG) CShellExt::Release()
503{
504
505 if (InterlockedDecrement((LPLONG)&m_cRef))
506 return m_cRef;
507
508 delete this;
509
510 return 0L;
511}
512
513
514//
515// FUNCTION: CShellExt::Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY)
516//
517// PURPOSE: Called by the shell when initializing a context menu or property
518// sheet extension.
519//
520// PARAMETERS:
521// pIDFolder - Specifies the parent folder
522// pDataObj - Spefifies the set of items selected in that folder.
523// hRegKey - Specifies the type of the focused item in the selection.
524//
525// RETURN VALUE:
526//
527// NOERROR in all cases.
528//
529// COMMENTS: Note that at the time this function is called, we don't know
530// (or care) what type of shell extension is being initialized.
531// It could be a context menu or a property sheet.
532//
533
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000534STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST /* pIDFolder */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000535 LPDATAOBJECT pDataObj,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000536 HKEY /* hRegKey */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000537{
538 // Initialize can be called more than once
539 if (m_pDataObj)
540 m_pDataObj->Release();
541
542 // duplicate the object pointer and registry handle
543
544 if (pDataObj)
545 {
546 m_pDataObj = pDataObj;
547 pDataObj->AddRef();
548 }
549
550 return NOERROR;
551}
552
553
554//
555// FUNCTION: CShellExt::QueryContextMenu(HMENU, UINT, UINT, UINT, UINT)
556//
557// PURPOSE: Called by the shell just before the context menu is displayed.
558// This is where you add your specific menu items.
559//
560// PARAMETERS:
561// hMenu - Handle to the context menu
562// indexMenu - Index of where to begin inserting menu items
563// idCmdFirst - Lowest value for new menu ID's
564// idCmtLast - Highest value for new menu ID's
565// uFlags - Specifies the context of the menu event
566//
567// RETURN VALUE:
568//
569//
570// COMMENTS:
571//
572
573STDMETHODIMP CShellExt::QueryContextMenu(HMENU hMenu,
574 UINT indexMenu,
575 UINT idCmdFirst,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000576 UINT /* idCmdLast */,
577 UINT /* uFlags */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000578{
579 UINT idCmd = idCmdFirst;
580
581 hres = m_pDataObj->GetData(&fmte, &medium);
582 if (medium.hGlobal)
583 cbFiles = DragQueryFile((HDROP)medium.hGlobal, (UINT)-1, 0, 0);
584
585 // InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
586
587 // Initialize m_cntOfHWnd to 0
588 m_cntOfHWnd = 0;
Bram Moolenaar49325942007-05-10 19:19:59 +0000589 // Retrieve all the vim instances
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000590 EnumWindows(EnumWindowsProc, (LPARAM)this);
591
592 if (cbFiles > 1)
593 {
594 InsertMenu(hMenu,
595 indexMenu++,
596 MF_STRING|MF_BYPOSITION,
597 idCmd++,
598 _("Edit with &multiple Vims"));
599
600 InsertMenu(hMenu,
601 indexMenu++,
602 MF_STRING|MF_BYPOSITION,
603 idCmd++,
604 _("Edit with single &Vim"));
605
606 if (cbFiles <= 4)
607 {
608 // Can edit up to 4 files in diff mode
609 InsertMenu(hMenu,
610 indexMenu++,
611 MF_STRING|MF_BYPOSITION,
612 idCmd++,
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000613 _("Diff with Vim"));
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000614 m_edit_existing_off = 3;
615 }
616 else
617 m_edit_existing_off = 2;
618
619 }
620 else
621 {
622 InsertMenu(hMenu,
623 indexMenu++,
624 MF_STRING|MF_BYPOSITION,
625 idCmd++,
626 _("Edit with &Vim"));
627 m_edit_existing_off = 1;
628 }
629
630 // Now display all the vim instances
631 for (int i = 0; i < m_cntOfHWnd; i++)
632 {
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000633 char title[BUFSIZE];
634 char temp[BUFSIZE];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000635
636 // Obtain window title, continue if can not
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000637 if (GetWindowText(m_hWnd[i], title, BUFSIZE - 1) == 0)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000638 continue;
639 // Truncate the title before the path, keep the file name
640 char *pos = strchr(title, '(');
641 if (pos != NULL)
642 {
643 if (pos > title && pos[-1] == ' ')
644 --pos;
645 *pos = 0;
646 }
647 // Now concatenate
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000648 strncpy(temp, _("Edit with existing Vim - "), BUFSIZE - 1);
Bram Moolenaardc7e00e2009-09-11 11:26:56 +0000649 temp[BUFSIZE - 1] = '\0';
650 strncat(temp, title, BUFSIZE - 1 - strlen(temp));
651 temp[BUFSIZE - 1] = '\0';
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000652 InsertMenu(hMenu,
653 indexMenu++,
654 MF_STRING|MF_BYPOSITION,
655 idCmd++,
656 temp);
657 }
658 // InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
659
660 // Must return number of menu items we added.
661 return ResultFromShort(idCmd-idCmdFirst);
662}
663
664//
665// FUNCTION: CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO)
666//
667// PURPOSE: Called by the shell after the user has selected on of the
668// menu items that was added in QueryContextMenu().
669//
670// PARAMETERS:
671// lpcmi - Pointer to an CMINVOKECOMMANDINFO structure
672//
673// RETURN VALUE:
674//
675//
676// COMMENTS:
677//
678
679STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
680{
681 HRESULT hr = E_INVALIDARG;
682
683 // If HIWORD(lpcmi->lpVerb) then we have been called programmatically
684 // and lpVerb is a command that should be invoked. Otherwise, the shell
685 // has called us, and LOWORD(lpcmi->lpVerb) is the menu ID the user has
686 // selected. Actually, it's (menu ID - idCmdFirst) from QueryContextMenu().
687 if (!HIWORD(lpcmi->lpVerb))
688 {
689 UINT idCmd = LOWORD(lpcmi->lpVerb);
690
691 if (idCmd >= m_edit_existing_off)
692 {
693 // Existing with vim instance
694 hr = PushToWindow(lpcmi->hwnd,
695 lpcmi->lpDirectory,
696 lpcmi->lpVerb,
697 lpcmi->lpParameters,
698 lpcmi->nShow,
699 idCmd - m_edit_existing_off);
700 }
701 else
702 {
703 switch (idCmd)
704 {
705 case 0:
706 hr = InvokeGvim(lpcmi->hwnd,
707 lpcmi->lpDirectory,
708 lpcmi->lpVerb,
709 lpcmi->lpParameters,
710 lpcmi->nShow);
711 break;
712 case 1:
713 hr = InvokeSingleGvim(lpcmi->hwnd,
714 lpcmi->lpDirectory,
715 lpcmi->lpVerb,
716 lpcmi->lpParameters,
717 lpcmi->nShow,
718 0);
719 break;
720 case 2:
721 hr = InvokeSingleGvim(lpcmi->hwnd,
722 lpcmi->lpDirectory,
723 lpcmi->lpVerb,
724 lpcmi->lpParameters,
725 lpcmi->nShow,
726 1);
727 break;
728 }
729 }
730 }
731 return hr;
732}
733
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000734STDMETHODIMP CShellExt::PushToWindow(HWND /* hParent */,
735 LPCSTR /* pszWorkingDir */,
736 LPCSTR /* pszCmd */,
737 LPCSTR /* pszParam */,
738 int /* iShowCmd */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000739 int idHWnd)
740{
741 HWND hWnd = m_hWnd[idHWnd];
742
743 // Show and bring vim instance to foreground
744 if (IsIconic(hWnd) != 0)
745 ShowWindow(hWnd, SW_RESTORE);
746 else
747 ShowWindow(hWnd, SW_SHOW);
748 SetForegroundWindow(hWnd);
749
750 // Post the selected files to the vim instance
751 PostMessage(hWnd, WM_DROPFILES, (WPARAM)medium.hGlobal, 0);
752
753 return NOERROR;
754}
755
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000756STDMETHODIMP CShellExt::GetCommandString(UINT_PTR /* idCmd */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000757 UINT uFlags,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000758 UINT FAR * /* reserved */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000759 LPSTR pszName,
760 UINT cchMax)
761{
762 if (uFlags == GCS_HELPTEXT && cchMax > 35)
763 lstrcpy(pszName, _("Edits the selected file(s) with Vim"));
764
765 return NOERROR;
766}
767
768BOOL CALLBACK CShellExt::EnumWindowsProc(HWND hWnd, LPARAM lParam)
769{
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000770 char temp[BUFSIZE];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000771
772 // First do a bunch of check
773 // No invisible window
774 if (!IsWindowVisible(hWnd)) return TRUE;
775 // No child window ???
776 // if (GetParent(hWnd)) return TRUE;
777 // Class name should be Vim, if failed to get class name, return
778 if (GetClassName(hWnd, temp, sizeof(temp)) == 0)
779 return TRUE;
780 // Compare class name to that of vim, if not, return
781 if (_strnicmp(temp, "vim", sizeof("vim")) != 0)
782 return TRUE;
783 // First check if the number of vim instance exceeds MAX_HWND
784 CShellExt *cs = (CShellExt*) lParam;
785 if (cs->m_cntOfHWnd >= MAX_HWND) return TRUE;
786 // Now we get the vim window, put it into some kind of array
787 cs->m_hWnd[cs->m_cntOfHWnd] = hWnd;
788 cs->m_cntOfHWnd ++;
789
790 return TRUE; // continue enumeration (otherwise this would be false)
791}
792
793#ifdef WIN32
794// This symbol is not defined in older versions of the SDK or Visual C++.
795
796#ifndef VER_PLATFORM_WIN32_WINDOWS
797# define VER_PLATFORM_WIN32_WINDOWS 1
798#endif
799
800static DWORD g_PlatformId;
801
802//
803// Set g_PlatformId to VER_PLATFORM_WIN32_NT (NT) or
804// VER_PLATFORM_WIN32_WINDOWS (Win95).
805//
806 static void
807PlatformId(void)
808{
809 static int done = FALSE;
810
811 if (!done)
812 {
813 OSVERSIONINFO ovi;
814
815 ovi.dwOSVersionInfoSize = sizeof(ovi);
816 GetVersionEx(&ovi);
817
818 g_PlatformId = ovi.dwPlatformId;
819 done = TRUE;
820 }
821}
822
823# ifndef __BORLANDC__
824 static char *
825searchpath(char *name)
826{
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000827 static char widename[2 * BUFSIZE];
828 static char location[2 * BUFSIZE + 2];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000829
830 // There appears to be a bug in FindExecutableA() on Windows NT.
831 // Use FindExecutableW() instead...
832 PlatformId();
833 if (g_PlatformId == VER_PLATFORM_WIN32_NT)
834 {
835 MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)name, -1,
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000836 (LPWSTR)widename, BUFSIZE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000837 if (FindExecutableW((LPCWSTR)widename, (LPCWSTR)"",
838 (LPWSTR)location) > (HINSTANCE)32)
839 {
840 WideCharToMultiByte(CP_ACP, 0, (LPWSTR)location, -1,
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000841 (LPSTR)widename, 2 * BUFSIZE, NULL, NULL);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000842 return widename;
843 }
844 }
845 else
846 {
847 if (FindExecutableA((LPCTSTR)name, (LPCTSTR)"",
848 (LPTSTR)location) > (HINSTANCE)32)
849 return location;
850 }
Bram Moolenaar7baa45d2007-08-18 15:00:42 +0000851 return (char *)"";
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000852}
853# endif
854#endif
855
856STDMETHODIMP CShellExt::InvokeGvim(HWND hParent,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000857 LPCSTR /* pszWorkingDir */,
858 LPCSTR /* pszCmd */,
859 LPCSTR /* pszParam */,
860 int /* iShowCmd */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000861{
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200862 wchar_t m_szFileUserClickedOn[BUFSIZE];
863 wchar_t cmdStrW[BUFSIZE];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000864 UINT i;
865
866 for (i = 0; i < cbFiles; i++)
867 {
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200868 DragQueryFileW((HDROP)medium.hGlobal,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000869 i,
870 m_szFileUserClickedOn,
871 sizeof(m_szFileUserClickedOn));
872
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200873 getGvimNameW(cmdStrW);
874 wcscat(cmdStrW, L" \"");
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000875
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200876 if ((wcslen(cmdStrW) + wcslen(m_szFileUserClickedOn) + 2) < BUFSIZE)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000877 {
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200878 wcscat(cmdStrW, m_szFileUserClickedOn);
879 wcscat(cmdStrW, L"\"");
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000880
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200881 STARTUPINFOW si;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000882 PROCESS_INFORMATION pi;
883
884 ZeroMemory(&si, sizeof(si));
885 si.cb = sizeof(si);
886
887 // Start the child process.
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200888 if (!CreateProcessW(NULL, // No module name (use command line).
889 cmdStrW, // Command line.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000890 NULL, // Process handle not inheritable.
891 NULL, // Thread handle not inheritable.
892 FALSE, // Set handle inheritance to FALSE.
893 0, // No creation flags.
894 NULL, // Use parent's environment block.
895 NULL, // Use parent's starting directory.
896 &si, // Pointer to STARTUPINFO structure.
897 &pi) // Pointer to PROCESS_INFORMATION structure.
898 )
899 {
900 MessageBox(
901 hParent,
902 _("Error creating process: Check if gvim is in your path!"),
903 _("gvimext.dll error"),
904 MB_OK);
905 }
906 else
907 {
908 CloseHandle( pi.hProcess );
909 CloseHandle( pi.hThread );
910 }
911 }
912 else
913 {
914 MessageBox(
915 hParent,
916 _("Path length too long!"),
917 _("gvimext.dll error"),
918 MB_OK);
919 }
920 }
921
922 return NOERROR;
923}
924
925
926STDMETHODIMP CShellExt::InvokeSingleGvim(HWND hParent,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000927 LPCSTR /* pszWorkingDir */,
928 LPCSTR /* pszCmd */,
929 LPCSTR /* pszParam */,
930 int /* iShowCmd */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000931 int useDiff)
932{
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200933 wchar_t m_szFileUserClickedOn[BUFSIZE];
934 wchar_t *cmdStrW;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000935 size_t cmdlen;
936 size_t len;
937 UINT i;
938
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000939 cmdlen = BUFSIZE;
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200940 cmdStrW = (wchar_t *) malloc(cmdlen * sizeof(wchar_t));
941 getGvimNameW(cmdStrW);
942
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000943 if (useDiff)
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200944 wcscat(cmdStrW, L" -d");
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000945 for (i = 0; i < cbFiles; i++)
946 {
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200947 DragQueryFileW((HDROP)medium.hGlobal,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000948 i,
949 m_szFileUserClickedOn,
950 sizeof(m_szFileUserClickedOn));
951
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200952 len = wcslen(cmdStrW) + wcslen(m_szFileUserClickedOn) + 4;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000953 if (len > cmdlen)
954 {
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000955 cmdlen = len + BUFSIZE;
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200956 cmdStrW = (wchar_t *)realloc(cmdStrW, cmdlen * sizeof(wchar_t));
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000957 }
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200958 wcscat(cmdStrW, L" \"");
959 wcscat(cmdStrW, m_szFileUserClickedOn);
960 wcscat(cmdStrW, L"\"");
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000961 }
962
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200963 STARTUPINFOW si;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000964 PROCESS_INFORMATION pi;
965
966 ZeroMemory(&si, sizeof(si));
967 si.cb = sizeof(si);
968
969 // Start the child process.
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200970 if (!CreateProcessW(NULL, // No module name (use command line).
971 cmdStrW, // Command line.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000972 NULL, // Process handle not inheritable.
973 NULL, // Thread handle not inheritable.
974 FALSE, // Set handle inheritance to FALSE.
975 0, // No creation flags.
976 NULL, // Use parent's environment block.
977 NULL, // Use parent's starting directory.
978 &si, // Pointer to STARTUPINFO structure.
979 &pi) // Pointer to PROCESS_INFORMATION structure.
980 )
981 {
982 MessageBox(
983 hParent,
984 _("Error creating process: Check if gvim is in your path!"),
985 _("gvimext.dll error"),
986 MB_OK);
987 }
988 else
989 {
990 CloseHandle(pi.hProcess);
991 CloseHandle(pi.hThread);
992 }
993
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200994 free(cmdStrW);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000995
996 return NOERROR;
997}