1/* 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 41/* The buffers size used to be MAX_PATH (256 bytes), but that's not always 42 * enough */ 43#define BUFSIZE 1100 44 45// 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. 48// Returns the path in name[BUFSIZE]. It's empty when it fails. 49// 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 { 61 hlen = BUFSIZE; 62 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) 72 strcpy(name, searchpath((char *)"gvim.exe")); 73 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) 79 strcpy(name, searchpath((char *)"gvim.bat")); 80 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 88 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 99// 100// Get the Vim runtime directory into buf[BUFSIZE]. 101// 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 117 for (idx = (int)strlen(buf) - 1; idx >= 0; idx--) 118 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 { 166 {(char *)"gettext", (FARPROC*)&dyn_libintl_gettext}, 167 {(char *)"textdomain", (FARPROC*)&dyn_libintl_textdomain}, 168 {(char *)"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain}, 169 {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 * 219null_libintl_bindtextdomain(const char * /* domainname */, const char * /* dirname */) 220{ 221 return NULL; 222} 223 224 static char * 225null_libintl_textdomain(const char* /* domainname */) 226{ 227 return NULL; 228} 229 230// 231// Setup for translating strings. 232// 233 static void 234dyn_gettext_load(void) 235{ 236 char szBuff[BUFSIZE]; 237 char szLang[BUFSIZE]; 238 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 { 249 len = BUFSIZE; 250 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, 267 (LPTSTR)szBuff, BUFSIZE); 268 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 { 293 len = (DWORD)strlen(szBuff); 294 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 322DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /* lpReserved */) 323{ 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 452STDMETHODIMP CShellExtClassFactory::LockServer(BOOL /* fLock */) 453{ 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 534STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST /* pIDFolder */, 535 LPDATAOBJECT pDataObj, 536 HKEY /* hRegKey */) 537{ 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, 576 UINT /* idCmdLast */, 577 UINT /* uFlags */) 578{ 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; 589 // Retrieve all the vim instances 590 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++, 613 _("Diff with Vim")); 614 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 { 633 char title[BUFSIZE]; 634 char temp[BUFSIZE]; 635 636 // Obtain window title, continue if can not 637 if (GetWindowText(m_hWnd[i], title, BUFSIZE - 1) == 0) 638 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 648 strncpy(temp, _("Edit with existing Vim - "), BUFSIZE - 1); 649 temp[BUFSIZE - 1] = '\0'; 650 strncat(temp, title, BUFSIZE - 1 - strlen(temp)); 651 temp[BUFSIZE - 1] = '\0'; 652 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 734STDMETHODIMP CShellExt::PushToWindow(HWND /* hParent */, 735 LPCSTR /* pszWorkingDir */, 736 LPCSTR /* pszCmd */, 737 LPCSTR /* pszParam */, 738 int /* iShowCmd */, 739 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 756STDMETHODIMP CShellExt::GetCommandString(UINT_PTR /* idCmd */, 757 UINT uFlags, 758 UINT FAR * /* reserved */, 759 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{ 770 char temp[BUFSIZE]; 771 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{ 827 static char widename[2 * BUFSIZE]; 828 static char location[2 * BUFSIZE + 2]; 829 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, 836 (LPWSTR)widename, BUFSIZE); 837 if (FindExecutableW((LPCWSTR)widename, (LPCWSTR)"", 838 (LPWSTR)location) > (HINSTANCE)32) 839 { 840 WideCharToMultiByte(CP_ACP, 0, (LPWSTR)location, -1, 841 (LPSTR)widename, 2 * BUFSIZE, NULL, NULL); 842 return widename; 843 } 844 } 845 else 846 { 847 if (FindExecutableA((LPCTSTR)name, (LPCTSTR)"", 848 (LPTSTR)location) > (HINSTANCE)32) 849 return location; 850 } 851 return (char *)""; 852} 853# endif 854#endif 855 856STDMETHODIMP CShellExt::InvokeGvim(HWND hParent, 857 LPCSTR /* pszWorkingDir */, 858 LPCSTR /* pszCmd */, 859 LPCSTR /* pszParam */, 860 int /* iShowCmd */) 861{ 862 wchar_t m_szFileUserClickedOn[BUFSIZE]; 863 wchar_t cmdStrW[BUFSIZE]; 864 UINT i; 865 866 for (i = 0; i < cbFiles; i++) 867 { 868 DragQueryFileW((HDROP)medium.hGlobal, 869 i, 870 m_szFileUserClickedOn, 871 sizeof(m_szFileUserClickedOn)); 872 873 getGvimNameW(cmdStrW); 874 wcscat(cmdStrW, L" \""); 875 876 if ((wcslen(cmdStrW) + wcslen(m_szFileUserClickedOn) + 2) < BUFSIZE) 877 { 878 wcscat(cmdStrW, m_szFileUserClickedOn); 879 wcscat(cmdStrW, L"\""); 880 881 STARTUPINFOW si; 882 PROCESS_INFORMATION pi; 883 884 ZeroMemory(&si, sizeof(si)); 885 si.cb = sizeof(si); 886 887 // Start the child process. 888 if (!CreateProcessW(NULL, // No module name (use command line). 889 cmdStrW, // Command line. 890 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, 927 LPCSTR /* pszWorkingDir */, 928 LPCSTR /* pszCmd */, 929 LPCSTR /* pszParam */, 930 int /* iShowCmd */, 931 int useDiff) 932{ 933 wchar_t m_szFileUserClickedOn[BUFSIZE]; 934 wchar_t *cmdStrW; 935 size_t cmdlen; 936 size_t len; 937 UINT i; 938 939 cmdlen = BUFSIZE; 940 cmdStrW = (wchar_t *) malloc(cmdlen * sizeof(wchar_t)); 941 getGvimNameW(cmdStrW); 942 943 if (useDiff) 944 wcscat(cmdStrW, L" -d"); 945 for (i = 0; i < cbFiles; i++) 946 { 947 DragQueryFileW((HDROP)medium.hGlobal, 948 i, 949 m_szFileUserClickedOn, 950 sizeof(m_szFileUserClickedOn)); 951 952 len = wcslen(cmdStrW) + wcslen(m_szFileUserClickedOn) + 4; 953 if (len > cmdlen) 954 { 955 cmdlen = len + BUFSIZE; 956 cmdStrW = (wchar_t *)realloc(cmdStrW, cmdlen * sizeof(wchar_t)); 957 } 958 wcscat(cmdStrW, L" \""); 959 wcscat(cmdStrW, m_szFileUserClickedOn); 960 wcscat(cmdStrW, L"\""); 961 } 962 963 STARTUPINFOW si; 964 PROCESS_INFORMATION pi; 965 966 ZeroMemory(&si, sizeof(si)); 967 si.cb = sizeof(si); 968 969 // Start the child process. 970 if (!CreateProcessW(NULL, // No module name (use command line). 971 cmdStrW, // Command line. 972 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 994 free(cmdStrW); 995 996 return NOERROR; 997} 998