rime for python, attached to prompt-toolkit keybindings for some prompt-toolkit applications such as ptpython.
This project is consist of two parts:
- A python binding of librime
- A librime frontend on ptpython
# Ubuntu
sudo apt-get -y install librime-dev librime1 pkg-config
sudo apt-mark auto librime-dev pkg-config
# ArchLinux
sudo pacman -S --noconfirm librime pkg-config
# Android Termux
apt-get -y install librime pkg-config
# Nix
# use nix-shell to create a virtual environment then build
# homebrew
brew install librime pkg-config
# Windows msys2
pacboy -S --noconfirm pkg-config librime gcc
from pyrime.key import Key
from pyrime.session import Session
from pyrime.ui import UI
session = Session()
key = Key.new("n")
ui = UI()
if not session.process_key(key.code, key.mask):
raise Exception
context = session.get_context()
if context is None:
raise Exception
content, _ = ui.draw(context)
print("\n".join(content))
n|
[β δ½ ]β‘ ι£ β’ ε’ β£ θ½ β€ εΉ΄ β₯ ζ¨ β¦ ε
β§ ζΏ β¨ εͺ βͺ εΌ |>
A simplest example can be found by:
pip install pyrime[cli]
python -m pyrime
~/.config/ptpython/config.py
:
from ptpython.repl import PythonRepl
from prompt_toolkit.filters.app import (
emacs_insert_mode,
vi_insert_mode,
vi_navigation_mode,
)
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
from pyrime.ptpython import RIME
def configure(repl: PythonRepl) -> None:
rime = RIME(repl)
@repl.add_key_binding(
"c-^",
filter=(emacs_insert_mode | vi_insert_mode) & rime.filter(),
)
def _(event: KeyPressEvent) -> None:
rime.toggle()
If you have defined some key bindings which will disturb rime, try:
@repl.add_key_binding("c-h", filter=emacs_insert_mode & rime.filter())
def _(event: KeyPressEvent) -> None:
rime.toggle()
If you want to exit rime in vi_navigation_mode
, try:
@repl.add_key_binding("escape", filter=emacs_insert_mode)
def _(event: KeyPressEvent) -> None:
""".
:param event:
:type event: KeyPressEvent
:rtype: None
"""
event.app.editing_mode = EditingMode.VI
event.app.vi_state.input_mode = InputMode.NAVIGATION
rime.disable()
# and a, I, A, ...
@repl.add_key_binding("i", filter=vi_navigation_mode)
def _(event: KeyPressEvent) -> None:
""".
:param event:
:type event: KeyPressEvent
:rtype: None
"""
event.app.editing_mode = EditingMode.EMACS
event.app.vi_state.input_mode = InputMode.INSERT
rime.enable()
It will remember rime status and enable it when reenter vi_insert_mode
or
emacs_insert_mode
.
Some utility functions are defined in this project. Refer my ptpython config to know more.
- A collection of rime frontends
- A collection of rime bindings
- A collection of rime translators and filters