https://github.com/rimeinn/pyrime
ㄓ rime for python 🐍️
https://github.com/rimeinn/pyrime
prompt-toolkit ptpython python rime
Last synced: 28 days ago
JSON representation
ㄓ rime for python 🐍️
- Host: GitHub
- URL: https://github.com/rimeinn/pyrime
- Owner: rimeinn
- License: gpl-3.0
- Created: 2024-07-31T16:01:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-02-23T19:35:19.000Z (about 1 month ago)
- Last Synced: 2026-02-24T02:04:17.020Z (about 1 month ago)
- Topics: prompt-toolkit, ptpython, python, rime
- Language: Python
- Homepage: https://pyrime.readthedocs.io/
- Size: 356 KB
- Stars: 8
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pyrime
[](https://pyrime.readthedocs.io)
[](https://results.pre-commit.ci/latest/github/rimeinn/pyrime/main)
[](https://github.com/rimeinn/pyrime/actions)
[](https://codecov.io/gh/rimeinn/pyrime)
[](https://github.com/rimeinn/pyrime/releases)
[](https://github.com/rimeinn/pyrime/releases/latest)
[](https://github.com/rimeinn/pyrime/issues)
[](https://github.com/rimeinn/pyrime/issues?q=is%3Aissue+is%3Aclosed)
[](https://github.com/rimeinn/pyrime/pulls)
[](https://github.com/rimeinn/pyrime/pulls?q=is%3Apr+is%3Aclosed)
[](https://github.com/rimeinn/pyrime/discussions)
[](https://github.com/rimeinn/pyrime/milestones)
[](https://github.com/rimeinn/pyrime/network/members)
[](https://github.com/rimeinn/pyrime/stargazers)
[](https://github.com/rimeinn/pyrime/watchers)
[](https://github.com/rimeinn/pyrime/graphs/contributors)
[](https://github.com/rimeinn/pyrime/graphs/commit-activity)
[](https://github.com/rimeinn/pyrime/commits)
[](https://github.com/rimeinn/pyrime/releases/latest)
[](https://github.com/rimeinn/pyrime/blob/main/LICENSE)
[](https://github.com/rimeinn/pyrime)
[](https://github.com/rimeinn/pyrime)
[](https://github.com/rimeinn/pyrime)
[](https://github.com/rimeinn/pyrime)
[](https://github.com/rimeinn/pyrime)
[](https://github.com/rimeinn/pyrime)
[](https://pypi.org/project/pyrime/#description)
[](https://pypi.org/project/pyrime/#history)
[](https://pypi.org/project/pyrime/#files)
[](https://pypi.org/project/pyrime/#files)
[](https://pypi.org/project/pyrime/#files)
[](https://pypi.org/project/pyrime/#files)
[](https://aur.archlinux.org/packages/python-pyrime)
[](https://aur.archlinux.org/packages/python-pyrime)
[](https://aur.archlinux.org/packages/python-pyrime)
[](https://aur.archlinux.org/packages/python-pyrime)
[](https://aur.archlinux.org/packages/python-pyrime)

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
- A librime frontend on neovim (TODO)
## Dependence
- [librime](https://github.com/rime/librime)
```sh
# 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
```
## Usage
### Binding
```python
from pyrime.ime.ui.vertical import UI
from pyrime.key import Key
from pyrime.session import Session
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))
```
```text
n|
[① 你]② 那 ③ 呢 ④ 能 ⑤ 年 ⑥ 您 ⑦ 内 ⑧ 拿 ⑨ 哪 ⓪ 弄 |>
```
A simplest example can be found by:
```sh
pip install pyrime[cli]
python -m pyrime
```
By default, pyrime search ibus/fcitx/trime's config paths. You can see where it
found:
```python
from pyrime.api import Traits
print(Traits.user_data_dir)
```
### Frontend
#### Enable
`~/.config/ptpython/config.py`:
```python
from ptpython.repl import PythonRepl
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
from pyrime.ptpython.rime import Rime
def configure(repl: PythonRepl) -> None:
rime = Rime(repl)
@repl.add_key_binding("c-^", filter=rime.insert_mode)
def _(event: KeyPressEvent) -> None:
rime.is_enabled = not rime.is_enabled
```
If you have a special rime config path, you can:
```python
import os
from ptpython.repl import PythonRepl
from pyrime.session import Session
from pyrime.api import Traits
def configure(repl: PythonRepl) -> None:
rime = Rime(
repl,
Session(Traits(user_config_dir=os.path.expanduser("~/.config/rime"))),
)
```
#### Key Bindings
If you have defined some key bindings in `*_insert_mode`, they can disturb rime.
try:
```python
# from prompt_toolkit.filters.app import emacs_insert_mode, vi_insert_mode
# @repl.add_key_binding("c-h", filter=emacs_insert_mode | vi_insert_mode)
@repl.add_key_binding("c-h", filter=rime.insert_mode)
def _(event: KeyPressEvent) -> None:
pass
```
If you want to exit rime in `vi_navigation_mode`, refer the following code of
`pyrime.ptpython.bindings.viemacs`'s `load_viemacs_bindings()`:
```python
from prompt_toolkit.filters.app import vi_navigation_mode
@repl.add_key_binding("escape", filter=rime.insert_mode)
def _(event: KeyPressEvent) -> None:
"""Switch insert mode to normal mode.
:param event:
:type event: KeyPressEvent
:rtype: None
"""
# store rime status
rime.iminsert = rime.is_enabled
# disable rime
rime.is_enabled = False
event.app.editing_mode = EditingMode.VI
event.app.vi_state.input_mode = InputMode.NAVIGATION
# and a, I, A, ...
@repl.add_key_binding("i", filter=vi_navigation_mode)
def _(event: KeyPressEvent) -> None:
"""Switch normal mode to insert mode.
:param event:
:type event: KeyPressEvent
:rtype: None
"""
event.app.editing_mode = EditingMode.EMACS
event.app.vi_state.input_mode = InputMode.INSERT
# recovery rime status
rime.is_enabled = rime.iminsert
```
It will remember rime status and enable it when reenter `vi_insert_mode` or
`emacs_insert_mode`.
Some predefined key bindings are
[provided](https://github.com/rimeinn/pyrime/tree/main/src/pyrime/ptpython/bindings).
You can enable what you want:
```python
from prompt_toolkit.key_binding.key_bindings import merge_key_bindings
from pyrime.ptpython.bindings.autopair import load_autopair_bindings
from pyrime.ptpython.bindings.rime import load_rime_bindings
# by default, last registry is:
# from pyrime.ptpython.bindings import load_key_bindings
# rime.app.key_bindings.registries[-1] = load_key_bindings(rime)
rime.app.key_bindings.registries[-1] = merge_key_bindings([
load_rime_bindings(rime),
load_autopair_bindings(rime),
])
```
## Related Projects
- [A collection](https://github.com/rime/librime#frontends) of rime frontends
- [A collection](https://github.com/rimeinn/ime.nvim/#librime) of rime frontends
for neovim
- [A collection](https://github.com/rimeinn/rime.nvim#translators-and-filters)
of rime translators and filters