Thanks to visit codestin.com
Credit goes to github.com

Skip to content

rimeinn/rime.nvim

Repository files navigation

rime.nvim

readthedocs pre-commit.ci status github/workflow

github/downloads github/downloads/latest github/issues github/issues-closed github/issues-pr github/issues-pr-closed github/discussions github/milestones github/forks github/stars github/watchers github/contributors github/commit-activity github/last-commit github/release-date

github/license github/languages github/languages/top github/directory-file-count github/code-size github/repo-size github/v

luarocks

Rime for neovim.

screencast

This project is consist of two parts:

  • A lua binding of librime
  • A librime frontend on neovim

Dependence

# Ubuntu
sudo apt-get -y install librime1
# ArchLinux
sudo pacman -S librime
# Android Termux
apt-get -y install librime
# Nix
# use nix-shell to create a virtual environment then build
# homebrew
brew install librime
# Windows msys2
pacboy -S --noconfirm librime

Install

rocks.nvim

We provide a luarocks server to avoid your troublesome of building binary lua module.

vim.g.rocks_nvim = {
  -- ...
  luarocks_config = {
    -- ...
    rocks_servers = {
      "https://rimeinn.github.io/rime.nvim",
      -- other servers
      "https://lumen-oss.github.io/rocks-binaries",
      "https://luarocks.org",
    }
  }
}

Command style

:Rocks install rime.nvim

Declare style

~/.config/nvim/rocks.toml:

[plugins]
"rime.nvim" = "scm"

Then

:Rocks sync

or:

$ luarocks --lua-version 5.1 --local --tree ~/.local/share/nvim/rocks install rime.nvim
# ~/.local/share/nvim/rocks is the default rocks tree path
# you can change it according to your vim.g.rocks_nvim.rocks_path

Usage

Binding

local UI = require "ime.ui".UI

local Key = require "rime.key".Key
local Session = require "rime.session".Session

local session = Session()
local key = Key:from_vim"n"
local ui = UI()
if not session:process_key(key.code, key.mask) then
    return
end
local context = session:get_context()
if context == nil then
    return
end
local content, _ = ui:draw(context)
print(table.concat(content, "\n"))
n|
[① 你]② 那 ③ 呢 ④ 能 ⑤ 年 ⑥ 您 ⑦ 内 ⑧ 拿 ⑨ 哪 ⓪ 弄 |>

A simplest example can be found by:

rime

Frontend

Set keymap:

local Rime = require('rime.nvim.rime').Rime
local rime = Rime()
rime:create_autocmds()
vim.keymap.set('i', '<C-^>', rime:toggle_cb())
vim.keymap.set('i', '<C-@>', rime:enable_cb())
vim.keymap.set('i', '<C-_>', rime:disable_cb())

Once it is enabled, any printable key will be passed to rime in any case while any non-printable key will be passed to rime only if rime window is opened. If you want to pass a key to rime in any case, try:

vim.keymap.set('i', '<C-\\>', rime:callback('<C-\\>'))

It is useful for some key such as the key for switching input schema.

Lazy load is possible:

local ui = require('ime.ui')

local rime = require('rime.nvim')
rime.rime = { ui = ui.UI { indices = ui.styles.square } }
vim.keymap.set('i', '<C-^>', rime.toggle)
vim.keymap.set('i', '<C-@>', rime.enable)
vim.keymap.set('i', '<C-_>', rime.disable)
vim.keymap.set('i', '<C-\\>', rime.callback('<C-\\>'))

square

Only when you press <C-^>, Rime():create_autocmds() will be call, which will save time.

Once you switch to ascii mode of rime, you cannot switch back unless you have defined any hotkey to pass the key for switching ascii mode of rime to rime. Because only printable key can be passed to rime when rime window is closed.

Integration

Other frontends of librime

This plugin will search ibus/fcitx/trime's config path by order and load it. You can customize it by:

local Traits = require 'rime.traits'.Traits
local Session = require "rime.session".Session
local Rime = require 'rime.nvim.rime'.Rime

local rime = Rime {
    session = Session {
        traits = Traits {
            user_data_dir = vim.fn.expand "~/.config/ibus/rime"
        }
    }
}
rime:create_autocmds()
vim.keymap.set('i', '<C-^>', rime:toggle_cb())

Vim Cursor

set guicursor=n-v-c-sm:block-Cursor/lCursor,i-ci-ve:ver25-CursorIM/lCursorIM,r-cr-o:hor20-CursorIM/lCursorIM
local Cursor = require('ime.nvim.hooks.cursor').Cursor

local cursor = Cursor {
  schemas = {
    [".default"] = { bg = 'white' },
    double_pinyin_mspy = { bg = 'red' },
    japanese = { bg = 'yellow' }
  }
}
local rime = Rime {
  hook = cursor
}

ASCII

MSPY

japanese

In insert/replace/select/... mode, it will display current input schema name.

You can customize it. Such as:

Only display input schema name in insert mode:

local Airline = require('ime.nvim.hooks.airline').Airline

local airline = Airline()

function airline.get_new_mode(mode, old, name)
  if mode == 'i' then
    return name
  end
  return old
end

local rime = Rime {
  hook = airline
}

See airline's g:airline_mode_map to know i, R, s, ...

Disable all hooks:

local ChainedHook = require('ime.nvim.hooks.chainedhook').ChainedHook

local hook = ChainedHook { }
-- by default
-- local hook = ChainedHook { cursor, airline }
local rime = Rime {
  hook = hook
}

Like cmp-rime:

require('cmp').setup {
  -- ...
  sources = {
    -- ...
    { name = 'rime' }
  }
}
local cfg = require('lualine').get_config()
table.insert(
  cfg.sections.lualine_y,
  'require("rime.nvim").get_schema_name()'
)
require('lualine').setup(cfg)
  • ime.nvim uses :set iminsert=1/0 and :set imsearch=1/0 to save external IME's enabled flags.
  • rime.nvim uses :let/unlet b:iminsert to save internal IME's enabled flags.

So they will not conflict.

ime.nvim

Both rime.nvim and fcitx5-ui.nvim uses :let/unlet b:iminsert to save internal IME's enabled flags. They will conflict.

call smartinput#map_to_trigger('i', '', '', '')
call smartinput#define_rule({
      \ 'at': '\%#',
      \ 'char': '',
      \ 'input': '()<Left>',
      \ })

We use | to represent cursor. Every time you input a character:

  1. rime.nvim will process it firstly, such as |( -> |(
  2. vim-smartinput will process it then, such as |( -> (|)

However, if a menu exists, the situation is different. E.g.,

call smartinput#map_to_trigger('i', '', '', '')
call smartinput#define_rule({
      \ 'at': '\%#',
      \ 'char': '',
      \ 'input': '【】<Left>',
      \ })

When you input [:

「|
[① 「 〔全角〕]② 【 ③ 〔 ④ [ 〔全角〕

Then you press 2 to select the second candidate , it will be:

  1. rime.nvim will process it firstly, such as |2 -> |【
  2. vim-smartinput will do nothing, because |2 will not trigger the rule of |【.

You will not get 【|】!

Especially, when you mix Chinese punctuation and ASCII punctuation:

call smartinput#map_to_trigger('i', '<Space>', '<Space>', '<Space>')
call smartinput#define_rule({
      \ 'at': '(\%#)',
      \ 'char': '<Space>',
      \ 'input': '<Space><Space><Left>',
      \ })

If you press <Space> in (|), you will get ( | ). However, if you press <Space> to select the first candidate 你好, you will get (你好| ) due to | -> |你好! The best solution is using Chinese punctuation to get (你好|).

Nix

For Nix user, run /the/path/of/luarocks/rocks-5.1/rime.nvim/VERSION/scripts/update.sh when dynamic link libraries are broken after nix-collect-garbage -d.

Related Projects

Translators and Filters

Packages

No packages published

Languages