ierton/xkb-switch — pull request #2 by lyokha (2012-12-14 09:54:49+00:00)
Title: XKb Switch API for using in vim libcall() 

Hi.

This change will enable vim to load xkb-switch functionality via its libcall() interface: it adds an optional target (ON by default) that builds and installs shared library libxkbswitch.so compatible with libcall() API: there are two functions linked as extern "C" that return char* and have single argument of type const char*, returned data must be static (defined globally in an anonymous namespace).

Typical usage (in .vimrc):

```viml
let g:XkbSwitchLib = "/usr/local/lib/libxkbswitch.so"

fun! xkb_switch(mode)
let cur_layout = libcall(g:XkbSwitchLib, 'Xkb_Switch_getXkbLayout', '')
if a:mode == 0
if cur_layout != 'us'
if !exists('b:xkb_mappings_loaded')
call xkb_mappings_load()
endif
call libcall(g:XkbSwitchLib, 'Xkb_Switch_setXkbLayout', 'us')
endif
let b:xkb_layout = cur_layout
elseif a:mode == 1
if exists('b:xkb_layout')
if b:xkb_layout != cur_layout
call libcall(g:XkbSwitchLib, 'Xkb_Switch_setXkbLayout',
\ b:xkb_layout)
endif
endif
endif
endfun

if executable(g:XkbSwitchLib)
autocmd InsertEnter * call xkb_switch(1)
autocmd InsertLeave * call xkb_switch(0)
endif
```

Using call system('xkb_switch') may produce garbage on a terminal in some cases (there is a Russian article about this here: http://lin-techdet.blogspot.ru/2012/04/vim.html), whereas libcall() fixes this issue and xkb-switch works like a charm!

Cheers, Alexey.