Skip to content

Commit

Permalink
Improve documentation comments, configuration variables, and autocmds
Browse files Browse the repository at this point in the history
  • Loading branch information
rlue committed Nov 15, 2017
1 parent 409c23f commit 106db56
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
⌨️ Barbaric
⌨️ Barbaric
==========

![](https://raw.githubusercontent.com/rlue/i/master/vim-barbaric/demo.gif)

**vim + non-Latin scripts = pain.** _Barbaric_ is the cure.
**vim + non-Latin input = pain.** _Barbaric_ is the cure.

### Why?

Normal mode mappings are all 8-bit ASCII (_i.e.,_ Latin) characters. That means that when you want to work on a file in Russian (or Greek or Chinese), you’ve got to switch back to English (or Spanish or German) every time you leave Insert mode.
Normal mode mappings (_e.g.,_ `h` / `j` / `k` / `l`) are all 8-bit ASCII characters. That means that when you want to work on a file in Russian (or Greek or Chinese), you’ve got to switch back to English (or Spanish or German) every time you leave Insert mode.

_Barbaric_ detects which input method is active when you first launch vim, then again any time you leave Input mode, and switches between them for you automatically.

Expand Down Expand Up @@ -52,20 +52,24 @@ Usage

_Barbaric_ should work out of the box provided that whenever you launch vim, the active input method is the same one you want to use in Normal mode. If that’s not the case, be sure to set the first option in the [configuration section](#configuration) below.

#### Known bugs

On certain input methods (notably Chinese and Korean), switching windows away from vim and back can cause xkbswitch to malfunction. The problem is described [here](https://github.com/myshov/xkbswitch-macosx/issues/5).

Configuration
-------------

To change the default behavior of _Barbaric_, modify the lines below and add them to your `.vimrc`.

```viml
" The input method for NORMAL mode (as defined by `xkbswitch -g`)
" The input method for Normal mode (as defined by `xkbswitch -g`)
let g:barbaric_default = 0
" The scope where alternate input methods persist (buffer, window, tab, global)
let g:barbaric_scope = 'buffer'
" Forget alternate input method after n seconds in Normal mode (disabled by default)
" Useful if you only need input method persistence for short bursts of active work.
" Useful if you only need IM persistence for short bursts of active work.
let g:barbaric_timeout = -1
```

Expand Down
43 changes: 23 additions & 20 deletions autoload/barbaric.vim
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
" GUARD CLAUSES ----------------------------------------------------------------
" GUARD CLAUSES ================================================================
" Prevent double-sourcing
execute exists('g:loaded_barbaric') ? 'finish' : 'let g:loaded_barbaric = 1'

" Check dependencies
call system('type xkbswitch') | if v:shell_error | finish | endif

" PUBLIC FUNCTIONS -------------------------------------------------------------
" PUBLIC FUNCTIONS =============================================================
function! barbaric#switch(next_mode)
if a:next_mode == 'normal'
call s:record_current_im()
Expand All @@ -18,7 +18,26 @@ function! barbaric#switch(next_mode)
endif
endfunction

" HELPER FUNCTIONS -------------------------------------------------------------
" HELPER FUNCTIONS =============================================================
" Scope ------------------------------------------------------------------------
function! s:scope_marker()
let l:scope = s:current_scope()
if l:scope == 'g'
return
elseif l:scope == 't'
return tabpagenr()
elseif l:scope == 'w'
return win_getid()
elseif l:scope == 'b'
return bufnr('%')
endif
endfunction

function! s:current_scope()
return strcharpart(g:barbaric_scope, 0, 1)
endfunction

" Input method -----------------------------------------------------------------
function! s:current_im()
return s:current_scope() . ':barbaric_current'
endfunction
Expand All @@ -34,6 +53,7 @@ function! s:switch_to_im(target)
call system('xkbswitch -s ' . a:target)
endfunction

" Timeout ----------------------------------------------------------------------
function! s:set_timeout()
if g:barbaric_timeout < 0 | return | endif

Expand All @@ -50,20 +70,3 @@ function! s:check_timeout()
execute 'silent! unlet ' . s:current_im()
endif
endfunction

function! s:scope_marker()
let l:scope = s:current_scope()
if l:scope == 'g'
return
elseif l:scope == 't'
return tabpagenr()
elseif l:scope == 'w'
return win_getid()
elseif l:scope == 'b'
return bufnr('%')
endif
endfunction

function! s:current_scope()
return strcharpart(g:barbaric_scope, 0, 1)
endfunction
21 changes: 11 additions & 10 deletions plugin/barbaric.vim
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
call system('type xkbswitch') | if !v:shell_error
if !exists('g:barbaric_default')
let g:barbaric_default = system('xkbswitch -g')
endif
if executable('xkbswitch')
" The input method for Normal mode (as defined by `xkbswitch -g`)
let g:barbaric_default = system('xkbswitch -g')

if !exists('g:barbaric_scope')
let g:barbaric_scope = 'buffer'
endif
" The scope where alternate input methods persist (buffer, window, tab, global)
let g:barbaric_scope = 'buffer'

if !exists('g:barbaric_timeout')
let g:barbaric_timeout = -1
endif
" Forget alternate input method after n seconds in Normal mode (disabled by default)
" Useful if you only need IM persistence for short bursts of active work.
let g:barbaric_timeout = -1

augroup barbaric
autocmd!
autocmd InsertEnter * call barbaric#switch('insert')
autocmd InsertLeave * call barbaric#switch('normal')
autocmd FocusGained * call barbaric#switch('focus')
autocmd FocusLost * call barbaric#switch('unfocus')
autocmd VimLeave * call barbaric#switch('unfocus')
augroup END
endif

0 comments on commit 106db56

Please sign in to comment.