blob: f98423c15f87ba0a7fe345c904a4f33630e10905 [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
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000019static char *searchpath(char *name);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000020
21// Always get an error while putting the following stuff to the
22// gvimext.h file as class protected variables, give up and
23// declare them as global stuff
24FORMATETC fmte = {CF_HDROP,
25 (DVTARGETDEVICE FAR *)NULL,
26 DVASPECT_CONTENT,
27 -1,
28 TYMED_HGLOBAL
29 };
30STGMEDIUM medium;
31HRESULT hres = 0;
32UINT cbFiles = 0;
33
Bram Moolenaar6199d432017-10-14 19:05:44 +020034/* The buffers size used to be MAX_PATH (260 bytes), but that's not always
Bram Moolenaare759a7a2005-07-12 22:50:18 +000035 * enough */
36#define BUFSIZE 1100
37
msoyka-of-wharton83cd0152021-07-29 19:18:33 +020038// The "Edit with Vim" shell extension provides these choices when
39// a new instance of Gvim is selected:
40// - use tabpages
41// - enable diff mode
42// - none of the above
43#define EDIT_WITH_VIM_USE_TABPAGES (2)
44#define EDIT_WITH_VIM_IN_DIFF_MODE (1)
45#define EDIT_WITH_VIM_NO_OPTIONS (0)
46
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000047//
48// Get the name of the Gvim executable to use, with the path.
49// When "runtime" is non-zero, we were called to find the runtime directory.
Bram Moolenaare759a7a2005-07-12 22:50:18 +000050// Returns the path in name[BUFSIZE]. It's empty when it fails.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000051//
52 static void
53getGvimName(char *name, int runtime)
54{
55 HKEY keyhandle;
56 DWORD hlen;
57
David Wagner84d96112024-06-05 20:01:19 +020058 // Get the location of gvim from the registry. Try
59 // HKEY_CURRENT_USER first, then fall back to HKEY_LOCAL_MACHINE if
60 // not found.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000061 name[0] = 0;
David Wagner84d96112024-06-05 20:01:19 +020062 if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Vim\\Gvim", 0,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000063 KEY_READ, &keyhandle) == ERROR_SUCCESS)
64 {
Bram Moolenaare759a7a2005-07-12 22:50:18 +000065 hlen = BUFSIZE;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000066 if (RegQueryValueEx(keyhandle, "path", 0, NULL, (BYTE *)name, &hlen)
67 != ERROR_SUCCESS)
68 name[0] = 0;
69 else
70 name[hlen] = 0;
71 RegCloseKey(keyhandle);
72 }
73
David Wagner84d96112024-06-05 20:01:19 +020074 if ((name[0] == 0) &&
75 (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", 0,
76 KEY_READ, &keyhandle) == ERROR_SUCCESS))
77 {
78 hlen = BUFSIZE;
79 if (RegQueryValueEx(keyhandle, "path", 0, NULL, (BYTE *)name, &hlen)
80 != ERROR_SUCCESS)
81 name[0] = 0;
82 else
83 name[hlen] = 0;
84 RegCloseKey(keyhandle);
85 }
86
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000087 // Registry didn't work, use the search path.
88 if (name[0] == 0)
Bram Moolenaar7baa45d2007-08-18 15:00:42 +000089 strcpy(name, searchpath((char *)"gvim.exe"));
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000090
91 if (!runtime)
92 {
93 // Only when looking for the executable, not the runtime dir, we can
94 // search for the batch file or a name without a path.
95 if (name[0] == 0)
Bram Moolenaar7baa45d2007-08-18 15:00:42 +000096 strcpy(name, searchpath((char *)"gvim.bat"));
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000097 if (name[0] == 0)
98 strcpy(name, "gvim"); // finds gvim.bat or gvim.exe
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000099 }
100}
101
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200102 static void
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200103getGvimInvocation(char *name, int runtime)
104{
105 getGvimName(name, runtime);
106 // avoid that Vim tries to expand wildcards in the file names
107 strcat(name, " --literal");
108}
109
110 static void
111getGvimInvocationW(wchar_t *nameW)
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200112{
113 char *name;
114
115 name = (char *)malloc(BUFSIZE);
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200116 getGvimInvocation(name, 0);
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200117 mbstowcs(nameW, name, BUFSIZE);
118 free(name);
119}
120
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000121//
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000122// Get the Vim runtime directory into buf[BUFSIZE].
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000123// The result is empty when it failed.
124// When it works, the path ends in a slash or backslash.
125//
126 static void
127getRuntimeDir(char *buf)
128{
129 int idx;
130
131 getGvimName(buf, 1);
132 if (buf[0] != 0)
133 {
134 // When no path found, use the search path to expand it.
135 if (strchr(buf, '/') == NULL && strchr(buf, '\\') == NULL)
136 strcpy(buf, searchpath(buf));
137
138 // remove "gvim.exe" from the end
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000139 for (idx = (int)strlen(buf) - 1; idx >= 0; idx--)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000140 if (buf[idx] == '\\' || buf[idx] == '/')
141 {
142 buf[idx + 1] = 0;
143 break;
144 }
145 }
146}
147
K.Takata12715722023-05-25 16:43:27 +0100148 WCHAR *
149utf8_to_utf16(const char *s)
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200150{
K.Takata12715722023-05-25 16:43:27 +0100151 int size = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
152 WCHAR *buf = (WCHAR *)malloc(size * sizeof(WCHAR));
153 MultiByteToWideChar(CP_UTF8, 0, s, -1, buf, size);
154 return buf;
155}
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200156
K.Takata12715722023-05-25 16:43:27 +0100157 HBITMAP
158IconToBitmap(HICON hIcon, HBRUSH hBackground, int width, int height)
159{
160 HDC hDC = GetDC(NULL);
161 HDC hMemDC = CreateCompatibleDC(hDC);
162 HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, width, height);
163 HBITMAP hResultBmp = NULL;
164 HGDIOBJ hOrgBMP = SelectObject(hMemDC, hMemBmp);
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200165
K.Takata12715722023-05-25 16:43:27 +0100166 DrawIconEx(hMemDC, 0, 0, hIcon, width, height, 0, hBackground, DI_NORMAL);
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200167
K.Takata12715722023-05-25 16:43:27 +0100168 hResultBmp = hMemBmp;
169 hMemBmp = NULL;
170
171 SelectObject(hMemDC, hOrgBMP);
172 DeleteDC(hMemDC);
173 ReleaseDC(NULL, hDC);
174 DestroyIcon(hIcon);
175 return hResultBmp;
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200176}
177
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000178//
179// GETTEXT: translated messages and menu entries
180//
181#ifndef FEAT_GETTEXT
K.Takata12715722023-05-25 16:43:27 +0100182# define _(x) x
183# define W_impl(x) _wcsdup(L##x)
184# define W(x) W_impl(x)
185# define set_gettext_codeset() NULL
186# define restore_gettext_codeset(x)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000187#else
K.Takata12715722023-05-25 16:43:27 +0100188# define _(x) (*dyn_libintl_gettext)(x)
189# define W(x) utf8_to_utf16(x)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000190# define VIMPACKAGE "vim"
191# ifndef GETTEXT_DLL
192# define GETTEXT_DLL "libintl.dll"
Bram Moolenaar271273c2016-02-21 20:30:22 +0100193# define GETTEXT_DLL_ALT "libintl-8.dll"
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000194# endif
195
196// Dummy functions
197static char *null_libintl_gettext(const char *);
198static char *null_libintl_textdomain(const char *);
199static char *null_libintl_bindtextdomain(const char *, const char *);
K.Takata12715722023-05-25 16:43:27 +0100200static char *null_libintl_bind_textdomain_codeset(const char *, const char *);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000201static int dyn_libintl_init(char *dir);
202static void dyn_libintl_end(void);
203
204static HINSTANCE hLibintlDLL = 0;
205static char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext;
206static char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain;
207static char *(*dyn_libintl_bindtextdomain)(const char *, const char *)
208 = null_libintl_bindtextdomain;
K.Takata12715722023-05-25 16:43:27 +0100209static char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *)
210 = null_libintl_bind_textdomain_codeset;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000211
212//
213// Attempt to load libintl.dll. If it doesn't work, use dummy functions.
214// "dir" is the directory where the libintl.dll might be.
215// Return 1 for success, 0 for failure.
216//
217 static int
218dyn_libintl_init(char *dir)
219{
220 int i;
221 static struct
222 {
223 char *name;
224 FARPROC *ptr;
225 } libintl_entry[] =
226 {
Bram Moolenaar7baa45d2007-08-18 15:00:42 +0000227 {(char *)"gettext", (FARPROC*)&dyn_libintl_gettext},
228 {(char *)"textdomain", (FARPROC*)&dyn_libintl_textdomain},
229 {(char *)"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain},
K.Takata12715722023-05-25 16:43:27 +0100230 {(char *)"bind_textdomain_codeset", (FARPROC*)&dyn_libintl_bind_textdomain_codeset},
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000231 {NULL, NULL}
232 };
Bram Moolenaar271273c2016-02-21 20:30:22 +0100233 DWORD len, len2;
234 LPWSTR buf = NULL;
235 LPWSTR buf2 = NULL;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000236
237 // No need to initialize twice.
238 if (hLibintlDLL)
239 return 1;
240
Bram Moolenaar6199d432017-10-14 19:05:44 +0200241 // Load gettext library from $VIMRUNTIME\GvimExt{64,32} directory.
Bram Moolenaar271273c2016-02-21 20:30:22 +0100242 // Add the directory to $PATH temporarily.
243 len = GetEnvironmentVariableW(L"PATH", NULL, 0);
244 len2 = MAX_PATH + 1 + len;
245 buf = (LPWSTR)malloc(len * sizeof(WCHAR));
246 buf2 = (LPWSTR)malloc(len2 * sizeof(WCHAR));
247 if (buf != NULL && buf2 != NULL)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000248 {
Bram Moolenaar271273c2016-02-21 20:30:22 +0100249 GetEnvironmentVariableW(L"PATH", buf, len);
Bram Moolenaar38231922020-11-18 15:30:09 +0100250# ifdef _WIN64
Bram Moolenaar6199d432017-10-14 19:05:44 +0200251 _snwprintf(buf2, len2, L"%S\\GvimExt64;%s", dir, buf);
Bram Moolenaar38231922020-11-18 15:30:09 +0100252# else
Bram Moolenaar6199d432017-10-14 19:05:44 +0200253 _snwprintf(buf2, len2, L"%S\\GvimExt32;%s", dir, buf);
Bram Moolenaar38231922020-11-18 15:30:09 +0100254# endif
Bram Moolenaar271273c2016-02-21 20:30:22 +0100255 SetEnvironmentVariableW(L"PATH", buf2);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000256 hLibintlDLL = LoadLibrary(GETTEXT_DLL);
Bram Moolenaar38231922020-11-18 15:30:09 +0100257# ifdef GETTEXT_DLL_ALT
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000258 if (!hLibintlDLL)
Bram Moolenaar271273c2016-02-21 20:30:22 +0100259 hLibintlDLL = LoadLibrary(GETTEXT_DLL_ALT);
Bram Moolenaar38231922020-11-18 15:30:09 +0100260# endif
Bram Moolenaar271273c2016-02-21 20:30:22 +0100261 SetEnvironmentVariableW(L"PATH", buf);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000262 }
Bram Moolenaar271273c2016-02-21 20:30:22 +0100263 free(buf);
264 free(buf2);
265 if (!hLibintlDLL)
266 return 0;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000267
268 // Get the addresses of the functions we need.
269 for (i = 0; libintl_entry[i].name != NULL
270 && libintl_entry[i].ptr != NULL; ++i)
271 {
272 if ((*libintl_entry[i].ptr = GetProcAddress(hLibintlDLL,
273 libintl_entry[i].name)) == NULL)
274 {
275 dyn_libintl_end();
276 return 0;
277 }
278 }
279 return 1;
280}
281
282 static void
283dyn_libintl_end(void)
284{
285 if (hLibintlDLL)
286 FreeLibrary(hLibintlDLL);
287 hLibintlDLL = NULL;
288 dyn_libintl_gettext = null_libintl_gettext;
289 dyn_libintl_textdomain = null_libintl_textdomain;
290 dyn_libintl_bindtextdomain = null_libintl_bindtextdomain;
K.Takata12715722023-05-25 16:43:27 +0100291 dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000292}
293
294 static char *
295null_libintl_gettext(const char *msgid)
296{
297 return (char *)msgid;
298}
299
300 static char *
K.Takata12715722023-05-25 16:43:27 +0100301null_libintl_textdomain(const char * /* domainname */)
302{
303 return NULL;
304}
305
306 static char *
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000307null_libintl_bindtextdomain(const char * /* domainname */, const char * /* dirname */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000308{
309 return NULL;
310}
311
312 static char *
K.Takata12715722023-05-25 16:43:27 +0100313null_libintl_bind_textdomain_codeset(const char * /* domainname */, const char * /* codeset */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000314{
315 return NULL;
316}
317
318//
319// Setup for translating strings.
320//
321 static void
322dyn_gettext_load(void)
323{
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000324 char szBuff[BUFSIZE];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000325 DWORD len;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000326
327 // Try to locate the runtime files. The path is used to find libintl.dll
328 // and the vim.mo files.
329 getRuntimeDir(szBuff);
330 if (szBuff[0] != 0)
331 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000332 len = (DWORD)strlen(szBuff);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000333 if (dyn_libintl_init(szBuff))
334 {
335 strcpy(szBuff + len, "lang");
336
337 (*dyn_libintl_bindtextdomain)(VIMPACKAGE, szBuff);
338 (*dyn_libintl_textdomain)(VIMPACKAGE);
339 }
340 }
341}
342
343 static void
344dyn_gettext_free(void)
345{
346 dyn_libintl_end();
347}
K.Takata12715722023-05-25 16:43:27 +0100348
349//
350// Use UTF-8 for gettext. Returns previous codeset.
351//
352 static char *
353set_gettext_codeset(void)
354{
355 char *prev = dyn_libintl_bind_textdomain_codeset(VIMPACKAGE, NULL);
356 prev = _strdup((prev != NULL) ? prev : "char");
357 dyn_libintl_bind_textdomain_codeset(VIMPACKAGE, "utf-8");
358
359 return prev;
360}
361
362//
363// Restore previous codeset for gettext.
364//
365 static void
366restore_gettext_codeset(char *prev)
367{
368 dyn_libintl_bind_textdomain_codeset(VIMPACKAGE, prev);
369 free(prev);
370}
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000371#endif // FEAT_GETTEXT
372
373//
374// Global variables
375//
376UINT g_cRefThisDll = 0; // Reference count of this DLL.
377HINSTANCE g_hmodThisDll = NULL; // Handle to this DLL itself.
378
379
380//---------------------------------------------------------------------------
381// DllMain
382//---------------------------------------------------------------------------
383extern "C" int APIENTRY
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000384DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /* lpReserved */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000385{
386 switch (dwReason)
387 {
388 case DLL_PROCESS_ATTACH:
389 // Extension DLL one-time initialization
390 g_hmodThisDll = hInstance;
391 break;
392
393 case DLL_PROCESS_DETACH:
394 break;
395 }
396
397 return 1; // ok
398}
399
400 static void
401inc_cRefThisDLL()
402{
403#ifdef FEAT_GETTEXT
Bram Moolenaar38231922020-11-18 15:30:09 +0100404 if (g_cRefThisDll == 0)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000405 dyn_gettext_load();
406#endif
407 InterlockedIncrement((LPLONG)&g_cRefThisDll);
408}
409
410 static void
411dec_cRefThisDLL()
412{
413#ifdef FEAT_GETTEXT
Bram Moolenaar38231922020-11-18 15:30:09 +0100414 if (InterlockedDecrement((LPLONG)&g_cRefThisDll) == 0)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000415 dyn_gettext_free();
416#else
417 InterlockedDecrement((LPLONG)&g_cRefThisDll);
418#endif
419}
420
421//---------------------------------------------------------------------------
422// DllCanUnloadNow
423//---------------------------------------------------------------------------
424
425STDAPI DllCanUnloadNow(void)
426{
427 return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
428}
429
430STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
431{
432 *ppvOut = NULL;
433
434 if (IsEqualIID(rclsid, CLSID_ShellExtension))
435 {
436 CShellExtClassFactory *pcf = new CShellExtClassFactory;
437
438 return pcf->QueryInterface(riid, ppvOut);
439 }
440
441 return CLASS_E_CLASSNOTAVAILABLE;
442}
443
444CShellExtClassFactory::CShellExtClassFactory()
445{
446 m_cRef = 0L;
447
448 inc_cRefThisDLL();
449}
450
451CShellExtClassFactory::~CShellExtClassFactory()
452{
453 dec_cRefThisDLL();
454}
455
456STDMETHODIMP CShellExtClassFactory::QueryInterface(REFIID riid,
457 LPVOID FAR *ppv)
458{
459 *ppv = NULL;
460
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200461 // any interface on this object is the object pointer
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000462
463 if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
464 {
465 *ppv = (LPCLASSFACTORY)this;
466
467 AddRef();
468
469 return NOERROR;
470 }
471
472 return E_NOINTERFACE;
473}
474
475STDMETHODIMP_(ULONG) CShellExtClassFactory::AddRef()
476{
477 return InterlockedIncrement((LPLONG)&m_cRef);
478}
479
480STDMETHODIMP_(ULONG) CShellExtClassFactory::Release()
481{
482 if (InterlockedDecrement((LPLONG)&m_cRef))
483 return m_cRef;
484
485 delete this;
486
487 return 0L;
488}
489
490STDMETHODIMP CShellExtClassFactory::CreateInstance(LPUNKNOWN pUnkOuter,
491 REFIID riid,
492 LPVOID *ppvObj)
493{
494 *ppvObj = NULL;
495
496 // Shell extensions typically don't support aggregation (inheritance)
497
498 if (pUnkOuter)
499 return CLASS_E_NOAGGREGATION;
500
501 // Create the main shell extension object. The shell will then call
502 // QueryInterface with IID_IShellExtInit--this is how shell extensions are
503 // initialized.
504
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200505 LPCSHELLEXT pShellExt = new CShellExt(); // create the CShellExt object
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000506
507 if (NULL == pShellExt)
508 return E_OUTOFMEMORY;
509
510 return pShellExt->QueryInterface(riid, ppvObj);
511}
512
513
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000514STDMETHODIMP CShellExtClassFactory::LockServer(BOOL /* fLock */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000515{
516 return NOERROR;
517}
518
519// *********************** CShellExt *************************
520CShellExt::CShellExt()
521{
522 m_cRef = 0L;
523 m_pDataObj = NULL;
524
525 inc_cRefThisDLL();
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200526
527 LoadMenuIcon();
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000528}
529
530CShellExt::~CShellExt()
531{
532 if (m_pDataObj)
533 m_pDataObj->Release();
534
535 dec_cRefThisDLL();
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200536
537 if (m_hVimIconBitmap)
538 DeleteObject(m_hVimIconBitmap);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000539}
540
541STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
542{
543 *ppv = NULL;
544
545 if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
546 {
547 *ppv = (LPSHELLEXTINIT)this;
548 }
549 else if (IsEqualIID(riid, IID_IContextMenu))
550 {
551 *ppv = (LPCONTEXTMENU)this;
552 }
553
554 if (*ppv)
555 {
556 AddRef();
557
558 return NOERROR;
559 }
560
561 return E_NOINTERFACE;
562}
563
564STDMETHODIMP_(ULONG) CShellExt::AddRef()
565{
566 return InterlockedIncrement((LPLONG)&m_cRef);
567}
568
569STDMETHODIMP_(ULONG) CShellExt::Release()
570{
571
572 if (InterlockedDecrement((LPLONG)&m_cRef))
573 return m_cRef;
574
575 delete this;
576
577 return 0L;
578}
579
580
581//
582// FUNCTION: CShellExt::Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY)
583//
584// PURPOSE: Called by the shell when initializing a context menu or property
585// sheet extension.
586//
587// PARAMETERS:
588// pIDFolder - Specifies the parent folder
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200589// pDataObj - Specifies the set of items selected in that folder.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000590// hRegKey - Specifies the type of the focused item in the selection.
591//
592// RETURN VALUE:
593//
594// NOERROR in all cases.
595//
596// COMMENTS: Note that at the time this function is called, we don't know
597// (or care) what type of shell extension is being initialized.
598// It could be a context menu or a property sheet.
599//
600
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000601STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST /* pIDFolder */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000602 LPDATAOBJECT pDataObj,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000603 HKEY /* hRegKey */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000604{
605 // Initialize can be called more than once
606 if (m_pDataObj)
607 m_pDataObj->Release();
608
609 // duplicate the object pointer and registry handle
610
611 if (pDataObj)
612 {
613 m_pDataObj = pDataObj;
614 pDataObj->AddRef();
615 }
616
617 return NOERROR;
618}
619
620
621//
622// FUNCTION: CShellExt::QueryContextMenu(HMENU, UINT, UINT, UINT, UINT)
623//
624// PURPOSE: Called by the shell just before the context menu is displayed.
625// This is where you add your specific menu items.
626//
627// PARAMETERS:
628// hMenu - Handle to the context menu
629// indexMenu - Index of where to begin inserting menu items
630// idCmdFirst - Lowest value for new menu ID's
631// idCmtLast - Highest value for new menu ID's
632// uFlags - Specifies the context of the menu event
633//
634// RETURN VALUE:
635//
636//
637// COMMENTS:
638//
639
640STDMETHODIMP CShellExt::QueryContextMenu(HMENU hMenu,
641 UINT indexMenu,
642 UINT idCmdFirst,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000643 UINT /* idCmdLast */,
644 UINT /* uFlags */)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000645{
646 UINT idCmd = idCmdFirst;
647
648 hres = m_pDataObj->GetData(&fmte, &medium);
649 if (medium.hGlobal)
K.Takata12715722023-05-25 16:43:27 +0100650 cbFiles = DragQueryFileW((HDROP)medium.hGlobal, (UINT)-1, 0, 0);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000651
652 // InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
653
654 // Initialize m_cntOfHWnd to 0
655 m_cntOfHWnd = 0;
Bram Moolenaarce35c882011-07-20 17:27:25 +0200656
657 HKEY keyhandle;
658 bool showExisting = true;
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200659 bool showIcons = true;
Bram Moolenaarce35c882011-07-20 17:27:25 +0200660
661 // Check whether "Edit with existing Vim" entries are disabled.
662 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Vim\\Gvim", 0,
663 KEY_READ, &keyhandle) == ERROR_SUCCESS)
664 {
665 if (RegQueryValueEx(keyhandle, "DisableEditWithExisting", 0, NULL,
666 NULL, NULL) == ERROR_SUCCESS)
667 showExisting = false;
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200668 if (RegQueryValueEx(keyhandle, "DisableContextMenuIcons", 0, NULL,
669 NULL, NULL) == ERROR_SUCCESS)
670 showIcons = false;
Bram Moolenaarce35c882011-07-20 17:27:25 +0200671 RegCloseKey(keyhandle);
672 }
673
K.Takata12715722023-05-25 16:43:27 +0100674 // Use UTF-8 for gettext.
675 char *prev = set_gettext_codeset();
676
Bram Moolenaarce35c882011-07-20 17:27:25 +0200677 // Retrieve all the vim instances, unless disabled.
678 if (showExisting)
679 EnumWindows(EnumWindowsProc, (LPARAM)this);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000680
K.Takata12715722023-05-25 16:43:27 +0100681 MENUITEMINFOW mii = { sizeof(MENUITEMINFOW) };
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200682 mii.fMask = MIIM_STRING | MIIM_ID;
683 if (showIcons)
684 {
685 mii.fMask |= MIIM_BITMAP;
686 mii.hbmpItem = m_hVimIconBitmap;
687 }
688
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000689 if (cbFiles > 1)
690 {
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200691 mii.wID = idCmd++;
K.Takata12715722023-05-25 16:43:27 +0100692 mii.dwTypeData = W(_("Edit with Vim using &tabpages"));
693 mii.cch = wcslen(mii.dwTypeData);
694 InsertMenuItemW(hMenu, indexMenu++, TRUE, &mii);
695 free(mii.dwTypeData);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000696
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200697 mii.wID = idCmd++;
K.Takata12715722023-05-25 16:43:27 +0100698 mii.dwTypeData = W(_("Edit with single &Vim"));
699 mii.cch = wcslen(mii.dwTypeData);
700 InsertMenuItemW(hMenu, indexMenu++, TRUE, &mii);
701 free(mii.dwTypeData);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000702
703 if (cbFiles <= 4)
704 {
705 // Can edit up to 4 files in diff mode
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200706 mii.wID = idCmd++;
K.Takata12715722023-05-25 16:43:27 +0100707 mii.dwTypeData = W(_("Diff with Vim"));
708 mii.cch = wcslen(mii.dwTypeData);
709 InsertMenuItemW(hMenu, indexMenu++, TRUE, &mii);
710 free(mii.dwTypeData);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000711 m_edit_existing_off = 3;
712 }
713 else
714 m_edit_existing_off = 2;
715
716 }
717 else
718 {
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200719 mii.wID = idCmd++;
K.Takata12715722023-05-25 16:43:27 +0100720 mii.dwTypeData = W(_("Edit with &Vim"));
721 mii.cch = wcslen(mii.dwTypeData);
722 InsertMenuItemW(hMenu, indexMenu++, TRUE, &mii);
723 free(mii.dwTypeData);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000724 m_edit_existing_off = 1;
725 }
726
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200727 HMENU hSubMenu = NULL;
728 if (m_cntOfHWnd > 1)
729 {
730 hSubMenu = CreatePopupMenu();
731 mii.fMask |= MIIM_SUBMENU;
732 mii.wID = idCmd;
K.Takata12715722023-05-25 16:43:27 +0100733 mii.dwTypeData = W(_("Edit with existing Vim"));
734 mii.cch = wcslen(mii.dwTypeData);
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200735 mii.hSubMenu = hSubMenu;
K.Takata12715722023-05-25 16:43:27 +0100736 InsertMenuItemW(hMenu, indexMenu++, TRUE, &mii);
737 free(mii.dwTypeData);
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200738 mii.fMask = mii.fMask & ~MIIM_SUBMENU;
739 mii.hSubMenu = NULL;
740 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000741 // Now display all the vim instances
742 for (int i = 0; i < m_cntOfHWnd; i++)
743 {
K.Takata12715722023-05-25 16:43:27 +0100744 WCHAR title[BUFSIZE];
745 WCHAR temp[BUFSIZE];
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200746 int index;
747 HMENU hmenu;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000748
749 // Obtain window title, continue if can not
K.Takata12715722023-05-25 16:43:27 +0100750 if (GetWindowTextW(m_hWnd[i], title, BUFSIZE - 1) == 0)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000751 continue;
752 // Truncate the title before the path, keep the file name
K.Takata12715722023-05-25 16:43:27 +0100753 WCHAR *pos = wcschr(title, L'(');
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000754 if (pos != NULL)
755 {
K.Takata12715722023-05-25 16:43:27 +0100756 if (pos > title && pos[-1] == L' ')
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000757 --pos;
758 *pos = 0;
759 }
760 // Now concatenate
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200761 if (m_cntOfHWnd > 1)
K.Takata12715722023-05-25 16:43:27 +0100762 temp[0] = L'\0';
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200763 else
764 {
K.Takata12715722023-05-25 16:43:27 +0100765 WCHAR *s = W(_("Edit with existing Vim - "));
766 wcsncpy(temp, s, BUFSIZE - 1);
767 temp[BUFSIZE - 1] = L'\0';
768 free(s);
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200769 }
K.Takata12715722023-05-25 16:43:27 +0100770 wcsncat(temp, title, BUFSIZE - 1 - wcslen(temp));
771 temp[BUFSIZE - 1] = L'\0';
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200772
773 mii.wID = idCmd++;
774 mii.dwTypeData = temp;
K.Takata12715722023-05-25 16:43:27 +0100775 mii.cch = wcslen(mii.dwTypeData);
Bram Moolenaarbf9679a2018-10-25 11:25:53 +0200776 if (m_cntOfHWnd > 1)
777 {
778 hmenu = hSubMenu;
779 index = i;
780 }
781 else
782 {
783 hmenu = hMenu;
784 index = indexMenu++;
785 }
K.Takata12715722023-05-25 16:43:27 +0100786 InsertMenuItemW(hmenu, index, TRUE, &mii);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000787 }
788 // InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
789
K.Takata12715722023-05-25 16:43:27 +0100790 // Restore previous codeset.
791 restore_gettext_codeset(prev);
792
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000793 // Must return number of menu items we added.
794 return ResultFromShort(idCmd-idCmdFirst);
795}
796
797//
798// FUNCTION: CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO)
799//
800// PURPOSE: Called by the shell after the user has selected on of the
801// menu items that was added in QueryContextMenu().
802//
803// PARAMETERS:
804// lpcmi - Pointer to an CMINVOKECOMMANDINFO structure
805//
806// RETURN VALUE:
807//
808//
809// COMMENTS:
810//
811
812STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
813{
814 HRESULT hr = E_INVALIDARG;
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200815 int gvimExtraOptions;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000816
817 // If HIWORD(lpcmi->lpVerb) then we have been called programmatically
818 // and lpVerb is a command that should be invoked. Otherwise, the shell
819 // has called us, and LOWORD(lpcmi->lpVerb) is the menu ID the user has
820 // selected. Actually, it's (menu ID - idCmdFirst) from QueryContextMenu().
821 if (!HIWORD(lpcmi->lpVerb))
822 {
823 UINT idCmd = LOWORD(lpcmi->lpVerb);
824
825 if (idCmd >= m_edit_existing_off)
826 {
827 // Existing with vim instance
828 hr = PushToWindow(lpcmi->hwnd,
829 lpcmi->lpDirectory,
830 lpcmi->lpVerb,
831 lpcmi->lpParameters,
832 lpcmi->nShow,
833 idCmd - m_edit_existing_off);
834 }
835 else
836 {
837 switch (idCmd)
838 {
839 case 0:
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200840 gvimExtraOptions = EDIT_WITH_VIM_USE_TABPAGES;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000841 break;
842 case 1:
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200843 gvimExtraOptions = EDIT_WITH_VIM_NO_OPTIONS;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000844 break;
845 case 2:
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200846 gvimExtraOptions = EDIT_WITH_VIM_IN_DIFF_MODE;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000847 break;
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200848 default:
849 // If execution reaches this point we likely have an
850 // inconsistency between the code that setup the menus
851 // and this code that determines what the user
852 // selected. This should be detected and fixed during
853 // development.
854 return E_FAIL;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000855 }
Nir Lichtman1aeccdb2021-12-22 15:21:15 +0000856
857 LPCMINVOKECOMMANDINFOEX lpcmiex = (LPCMINVOKECOMMANDINFOEX)lpcmi;
858 LPCWSTR currentDirectory = lpcmi->cbSize == sizeof(CMINVOKECOMMANDINFOEX) ? lpcmiex->lpDirectoryW : NULL;
859
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200860 hr = InvokeSingleGvim(lpcmi->hwnd,
Nir Lichtman1aeccdb2021-12-22 15:21:15 +0000861 currentDirectory,
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200862 lpcmi->lpVerb,
863 lpcmi->lpParameters,
864 lpcmi->nShow,
865 gvimExtraOptions);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000866 }
867 }
868 return hr;
869}
870
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000871STDMETHODIMP CShellExt::PushToWindow(HWND /* hParent */,
872 LPCSTR /* pszWorkingDir */,
873 LPCSTR /* pszCmd */,
874 LPCSTR /* pszParam */,
875 int /* iShowCmd */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000876 int idHWnd)
877{
878 HWND hWnd = m_hWnd[idHWnd];
879
880 // Show and bring vim instance to foreground
881 if (IsIconic(hWnd) != 0)
882 ShowWindow(hWnd, SW_RESTORE);
883 else
884 ShowWindow(hWnd, SW_SHOW);
885 SetForegroundWindow(hWnd);
886
887 // Post the selected files to the vim instance
888 PostMessage(hWnd, WM_DROPFILES, (WPARAM)medium.hGlobal, 0);
889
890 return NOERROR;
891}
892
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000893STDMETHODIMP CShellExt::GetCommandString(UINT_PTR /* idCmd */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000894 UINT uFlags,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000895 UINT FAR * /* reserved */,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000896 LPSTR pszName,
897 UINT cchMax)
898{
K.Takata12715722023-05-25 16:43:27 +0100899 // Use UTF-8 for gettext.
900 char *prev = set_gettext_codeset();
901
902 WCHAR *s = W(_("Edits the selected file(s) with Vim"));
903 if (uFlags == GCS_HELPTEXTW && cchMax > wcslen(s))
904 wcscpy((WCHAR *)pszName, s);
905 free(s);
906
907 // Restore previous codeset.
908 restore_gettext_codeset(prev);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000909
910 return NOERROR;
911}
912
913BOOL CALLBACK CShellExt::EnumWindowsProc(HWND hWnd, LPARAM lParam)
914{
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000915 char temp[BUFSIZE];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000916
917 // First do a bunch of check
918 // No invisible window
K.Takata12715722023-05-25 16:43:27 +0100919 if (!IsWindowVisible(hWnd))
920 return TRUE;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000921 // No child window ???
922 // if (GetParent(hWnd)) return TRUE;
923 // Class name should be Vim, if failed to get class name, return
924 if (GetClassName(hWnd, temp, sizeof(temp)) == 0)
925 return TRUE;
926 // Compare class name to that of vim, if not, return
927 if (_strnicmp(temp, "vim", sizeof("vim")) != 0)
928 return TRUE;
929 // First check if the number of vim instance exceeds MAX_HWND
930 CShellExt *cs = (CShellExt*) lParam;
K.Takata12715722023-05-25 16:43:27 +0100931 if (cs->m_cntOfHWnd >= MAX_HWND)
932 return FALSE; // stop enumeration
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000933 // Now we get the vim window, put it into some kind of array
934 cs->m_hWnd[cs->m_cntOfHWnd] = hWnd;
935 cs->m_cntOfHWnd ++;
936
937 return TRUE; // continue enumeration (otherwise this would be false)
938}
939
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200940BOOL CShellExt::LoadMenuIcon()
941{
K.Takata12715722023-05-25 16:43:27 +0100942 char vimExeFile[BUFSIZE];
943 getGvimName(vimExeFile, 1);
944 if (vimExeFile[0] == '\0')
945 return FALSE;
946 HICON hVimIcon;
947 if (ExtractIconEx(vimExeFile, 0, NULL, &hVimIcon, 1) == 0)
948 return FALSE;
949 m_hVimIconBitmap = IconToBitmap(hVimIcon,
950 GetSysColorBrush(COLOR_MENU),
951 GetSystemMetrics(SM_CXSMICON),
952 GetSystemMetrics(SM_CYSMICON));
953 return TRUE;
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200954}
955
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000956 static char *
957searchpath(char *name)
958{
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000959 static char widename[2 * BUFSIZE];
960 static char location[2 * BUFSIZE + 2];
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000961
962 // There appears to be a bug in FindExecutableA() on Windows NT.
963 // Use FindExecutableW() instead...
Bram Moolenaar6199d432017-10-14 19:05:44 +0200964 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)name, -1,
965 (LPWSTR)widename, BUFSIZE);
966 if (FindExecutableW((LPCWSTR)widename, (LPCWSTR)"",
967 (LPWSTR)location) > (HINSTANCE)32)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000968 {
Bram Moolenaar6199d432017-10-14 19:05:44 +0200969 WideCharToMultiByte(CP_ACP, 0, (LPWSTR)location, -1,
970 (LPSTR)widename, 2 * BUFSIZE, NULL, NULL);
971 return widename;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000972 }
Bram Moolenaar7baa45d2007-08-18 15:00:42 +0000973 return (char *)"";
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000974}
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000975
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000976
977STDMETHODIMP CShellExt::InvokeSingleGvim(HWND hParent,
Nir Lichtman1aeccdb2021-12-22 15:21:15 +0000978 LPCWSTR workingDir,
Bram Moolenaare6a91fd2008-07-24 18:51:11 +0000979 LPCSTR /* pszCmd */,
980 LPCSTR /* pszParam */,
981 int /* iShowCmd */,
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200982 int gvimExtraOptions)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000983{
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200984 wchar_t m_szFileUserClickedOn[BUFSIZE];
985 wchar_t *cmdStrW;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000986 size_t cmdlen;
987 size_t len;
988 UINT i;
989
Bram Moolenaare759a7a2005-07-12 22:50:18 +0000990 cmdlen = BUFSIZE;
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200991 cmdStrW = (wchar_t *) malloc(cmdlen * sizeof(wchar_t));
Bram Moolenaar06d4c4c2018-12-14 19:20:02 +0100992 if (cmdStrW == NULL)
Bram Moolenaar142a9752018-12-14 19:54:39 +0100993 return E_FAIL;
Bram Moolenaar7bc25ae2015-05-04 18:27:36 +0200994 getGvimInvocationW(cmdStrW);
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200995
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200996 if (gvimExtraOptions == EDIT_WITH_VIM_IN_DIFF_MODE)
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +0200997 wcscat(cmdStrW, L" -d");
msoyka-of-wharton83cd0152021-07-29 19:18:33 +0200998 else if (gvimExtraOptions == EDIT_WITH_VIM_USE_TABPAGES)
999 wcscat(cmdStrW, L" -p");
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001000 for (i = 0; i < cbFiles; i++)
1001 {
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +02001002 DragQueryFileW((HDROP)medium.hGlobal,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001003 i,
1004 m_szFileUserClickedOn,
1005 sizeof(m_szFileUserClickedOn));
1006
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +02001007 len = wcslen(cmdStrW) + wcslen(m_szFileUserClickedOn) + 4;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001008 if (len > cmdlen)
1009 {
Bram Moolenaare759a7a2005-07-12 22:50:18 +00001010 cmdlen = len + BUFSIZE;
Bram Moolenaar06d4c4c2018-12-14 19:20:02 +01001011 wchar_t *cmdStrW_new = (wchar_t *)realloc(cmdStrW, cmdlen * sizeof(wchar_t));
1012 if (cmdStrW_new == NULL)
Bram Moolenaar142a9752018-12-14 19:54:39 +01001013 {
1014 free(cmdStrW);
1015 return E_FAIL;
1016 }
Bram Moolenaar06d4c4c2018-12-14 19:20:02 +01001017 cmdStrW = cmdStrW_new;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001018 }
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +02001019 wcscat(cmdStrW, L" \"");
1020 wcscat(cmdStrW, m_szFileUserClickedOn);
1021 wcscat(cmdStrW, L"\"");
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001022 }
1023
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +02001024 STARTUPINFOW si;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001025 PROCESS_INFORMATION pi;
1026
1027 ZeroMemory(&si, sizeof(si));
1028 si.cb = sizeof(si);
1029
1030 // Start the child process.
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +02001031 if (!CreateProcessW(NULL, // No module name (use command line).
1032 cmdStrW, // Command line.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001033 NULL, // Process handle not inheritable.
1034 NULL, // Thread handle not inheritable.
1035 FALSE, // Set handle inheritance to FALSE.
Bram Moolenaar38231922020-11-18 15:30:09 +01001036 0, // No creation flags.
1037 NULL, // Use parent's environment block.
K.Takata12715722023-05-25 16:43:27 +01001038 workingDir, // Use parent's starting directory.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001039 &si, // Pointer to STARTUPINFO structure.
1040 &pi) // Pointer to PROCESS_INFORMATION structure.
1041 )
1042 {
K.Takata12715722023-05-25 16:43:27 +01001043 // Use UTF-8 for gettext.
1044 char *prev = set_gettext_codeset();
1045
1046 WCHAR *msg = W(_("Error creating process: Check if gvim is in your path!"));
1047 WCHAR *title = W(_("gvimext.dll error"));
1048
1049 MessageBoxW(hParent, msg, title, MB_OK);
1050
1051 free(msg);
1052 free(title);
1053
1054 // Restore previous codeset.
1055 restore_gettext_codeset(prev);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001056 }
1057 else
1058 {
1059 CloseHandle(pi.hProcess);
1060 CloseHandle(pi.hThread);
1061 }
Bram Moolenaar7e6d3bd2010-07-10 19:22:44 +02001062 free(cmdStrW);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001063
1064 return NOERROR;
1065}