Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rlue committed Aug 3, 2017
0 parents commit 409c23f
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/doc/tags
77 changes: 77 additions & 0 deletions README.md
@@ -0,0 +1,77 @@
⌨️ Barbaric
==========

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

**vim + non-Latin scripts = 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.

_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.

### No, I mean why ‘Barbaric’?

From [Wikipedia](https://en.wikipedia.org/w/index.php?title=Barbarian&oldid=792816841) (emphasis mine):

> The Greeks used the term _barbarian_ for all non-Greek-speaking peoples,
> including the Egyptians, Persians, Medes and Phoenicians, emphasizing their
> otherness, because the language they spoke sounded to Greeks like gibberish
> represented by the sounds “bar bar bar”....
>
> The Romans adapted the term **to refer to anything non-Roman**.
Vim doesn’t play nicely with non-Latin scripts; _i.e.,_ input languages of non-Roman origin. _Ipso facto,_ this is a plugin for barbarians.

### Supported Platforms

macOS only. For other platforms, try [vim-xkbswitch](https://github.com/lyokha/vim-xkbswitch), or consider built-in support for X11 and MS-Windows IMEs (`:help mbyte-XIM` / `:help mbyte-IME`).

Installation
------------

There are lots of vim plugin managers out there. I like [vim-plug](https://github.com/junegunn/vim-plug).

### Dependencies

* **[xkbswitch-macosx](https://github.com/myshov/xkbswitch-macosx)**

To install, copy the binary to one of the folders on your `PATH`:

```
$ git clone https://github.com/myshov/xkbswitch-macosx
$ cp xkbswitch-macosx/bin/xkbswitch /usr/local/bin
$ rm -rf xkbswitch-macosx
```

(It’s not ideal — I wish there were a brew formula. If you do too, let [@myshov](https://github.com/myshov) know [here](https://github.com/myshov/xkbswitch-macosx/issues/4).)

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.

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`)
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.
let g:barbaric_timeout = -1
```

License
-------

The MIT License (MIT)

Copyright © 2017 Ryan Lue
69 changes: 69 additions & 0 deletions autoload/barbaric.vim
@@ -0,0 +1,69 @@
" 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 -------------------------------------------------------------
function! barbaric#switch(next_mode)
if a:next_mode == 'normal'
call s:record_current_im()
call s:switch_to_im(g:barbaric_default)
call s:set_timeout()
elseif a:next_mode == 'insert'
call s:check_timeout()
if !exists(s:current_im()) | return | endif
call s:switch_to_im(eval(s:current_im()))
endif
endfunction

" HELPER FUNCTIONS -------------------------------------------------------------
function! s:current_im()
return s:current_scope() . ':barbaric_current'
endfunction

function! s:record_current_im()
execute "let " . s:current_im() . " = system('xkbswitch -g')"
if eval(s:current_im()) == g:barbaric_default
execute 'silent! unlet ' . s:current_im()
endif
endfunction

function! s:switch_to_im(target)
call system('xkbswitch -s ' . a:target)
endfunction

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

let s:timeout = { 'scope': s:scope_marker(), 'begin': localtime() }
endfunction

function! s:check_timeout()
if g:barbaric_timeout < 0 | return | endif
if !exists('s:timeout') || (s:scope_marker() != get(s:timeout, 'scope'))
return
endif

if (localtime() - get(s:timeout, 'begin')) > g:barbaric_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
65 changes: 65 additions & 0 deletions doc/barbaric.txt
@@ -0,0 +1,65 @@
*barbaric.txt* Automatic input method switching for Mac OS

==============================================================================
INTRODUCTION *barbaric*

Barbaric facilitates the use of non-Latin input methods in vim (since Normal
mode commands are nearly all mapped to Latin characters). It automatically
detects and switches between input methods as appropriate, and requires no
user interaction.

This plugin depends on a third-party binary which is only available for macOS.
For other platforms, consider built-in support for X11 (|mbyte-XIM|) and
Windows (|mbyte-IME|) IMEs.

Technical Reference ~

* Dependencies......................................|barbaric-dependencies|
* Options................................................|barbaric-options|


==============================================================================
DEPENDENCIES *barbaric-dependencies*

This plugin depends on xkbswitch-macosx.
To install it, copy the binary to one of the folders on your PATH:

$ git clone https://github.com/myshov/xkbswitch-macosx
$ cp xkbswitch-macosx/bin/xkbswitch /usr/local/bin
$ rm -rf xkbswitch-macosx

==============================================================================
OPTIONS *barbaric-options*

*g:barbaric_default*
Default input method number (default system('xkbswitch -g'))
Specifies the input method to activate when exiting Insert mode (as
reported by xkbswitch).
Be sure to set this if you think you might ever have a non-Latin input
method active when launching vim.

*g:barbaric_scope*
Persistence scope string (default "buffer")
Sets the scope across which remembered input methods will persist.
For example, when "buffer" is used, remembered input methods are local
to a each buffer.
Options are "global", "tab", "window", or "buffer".

*g:barbaric_timeout*
Persistence timeout number (default -1)
Forget remembered input methods if this many seconds have elapsed
since the last time you entered Insert mode.
Useful if you only need input method persistence for short bursts of
active work.
Disabled by default.

==============================================================================
ABOUT *barbaric-about*

This project is hosted on GitHub.
https://github.com/rlue/vim-barbaric

*barbaric-license*
Copyright © Ryan Lue. Distributed under the MIT License.

vim:tw=78:ts=8:et:ft=help:norl:
19 changes: 19 additions & 0 deletions plugin/barbaric.vim
@@ -0,0 +1,19 @@
call system('type xkbswitch') | if !v:shell_error
if !exists('g:barbaric_default')
let g:barbaric_default = system('xkbswitch -g')
endif

if !exists('g:barbaric_scope')
let g:barbaric_scope = 'buffer'
endif

if !exists('g:barbaric_timeout')
let g:barbaric_timeout = -1
endif

augroup barbaric
autocmd!
autocmd InsertEnter * call barbaric#switch('insert')
autocmd InsertLeave * call barbaric#switch('normal')
augroup END
endif

0 comments on commit 409c23f

Please sign in to comment.