Last active
August 29, 2015 14:27
-
-
Save ynkdir/17e5c0bcbbc5041b5409 to your computer and use it in GitHub Desktop.
vim ctrl-shift-b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cl /LD ctrlshiftkey.c user32.lib | |
#include <windows.h> | |
#ifdef _MSC_VER | |
# define EXPORT __declspec(dllexport) | |
# else | |
# define EXPORT | |
#endif | |
#define VIM_CLASSNAME "VIM_MESSAGES" | |
#define COPYDATA_KEYS 0 | |
#define COPYDATA_EXPR 10 | |
HWND hVimWnd = NULL; | |
HWND hVimServer = NULL; | |
WNDPROC origWindowProc = NULL; | |
LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); | |
static BOOL CALLBACK enumWindowsGetServer(HWND hwnd, LPARAM lParam); | |
EXPORT char *ctrlshiftkey_load(char *path) { | |
if (LoadLibrary(path) == NULL) | |
return "error"; | |
return "ok"; | |
} | |
EXPORT char *ctrlshiftkey_servername(char *name) { | |
EnumWindows(enumWindowsGetServer, (LPARAM)name); | |
if (hVimServer == NULL) | |
return "error"; | |
return "ok"; | |
} | |
EXPORT char *ctrlshiftkey_init(int hwnd) { | |
hVimWnd = (HWND)hwnd; | |
origWindowProc = (WNDPROC)GetWindowLongPtr(hVimWnd, GWLP_WNDPROC); | |
if (origWindowProc == NULL) | |
return "error"; | |
if (SetWindowLongPtr(hVimWnd, GWLP_WNDPROC, (LONG_PTR)MyWindowProc) == 0) | |
return "error"; | |
return "ok"; | |
} | |
LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { | |
if (uMsg == WM_CHAR) { | |
UINT ch = (UINT)wParam; | |
COPYDATASTRUCT data; | |
if (0x00 <= ch && ch < 0x20 && (GetKeyState(VK_SHIFT) & 0x8000)) { | |
char buf[] = "feedkeys(\"\\<S-X>\")"; | |
buf[14] = ch; | |
data.dwData = COPYDATA_EXPR; | |
data.cbData = strlen(buf) + 1; | |
data.lpData = buf; | |
SendMessage(hVimServer, WM_COPYDATA, (WPARAM)hwnd /* dummy */, (LPARAM)(&data)); | |
return 0; | |
} | |
} | |
return CallWindowProc(origWindowProc, hwnd, uMsg, wParam, lParam); | |
} | |
static BOOL CALLBACK enumWindowsGetServer(HWND hwnd, LPARAM lParam) { | |
int len; | |
char buf[100]; | |
char *name = (char *)lParam; | |
len = GetClassName(hwnd, buf, sizeof(buf)); | |
if (len == 0 || lstrcmpi(buf, VIM_CLASSNAME) != 0) | |
return TRUE; | |
len = GetWindowText(hwnd, buf, sizeof(buf)); | |
if (len == 0 || lstrcmpi(buf, name) != 0) | |
return TRUE; | |
hVimServer = hwnd; | |
return FALSE; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if !has('gui_running') | |
finish | |
endif | |
let s:dll = expand('<sfile>:p:h') . '\ctrlshiftkey.dll' | |
let s:dll = iconv(s:dll, &encoding, 'default') | |
function! s:init() | |
if libcall(s:dll, 'ctrlshiftkey_load', s:dll) != 'ok' | |
echoerr 'ctrlshiftkey_load: error' | |
return | |
endif | |
if libcall(s:dll, 'ctrlshiftkey_servername', v:servername) != 'ok' | |
echoerr 'ctrlshiftkey_servername: error' | |
return | |
endif | |
if libcall(s:dll, 'ctrlshiftkey_init', v:windowid) != 'ok' | |
echoerr 'ctrlshiftkey_init: error' | |
return | |
endif | |
endfunction | |
if has('vim_starting') | |
augroup CtrlShiftKey | |
au! | |
autocmd GuiEnter * call s:init() | |
augroup END | |
else | |
call s:init() | |
endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment