1
|
|
|
"""Digraph module.""" |
2
|
|
|
import re |
3
|
|
|
from .key import Key |
4
|
|
|
from .util import ensure_bytes |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
DIGRAPH_PATTERN = re.compile(r'(\S{2})\s+(\S)+\s+\d+') |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class Digraph: |
11
|
|
|
__slots__ = ('registry',) |
12
|
|
|
|
13
|
|
|
def __init__(self): |
14
|
|
|
self.registry = None |
15
|
|
|
|
16
|
|
|
def find(self, nvim: object, char1: str, char2: str) -> str: |
17
|
|
|
if self.registry is None: |
18
|
|
|
digraph_output = nvim.call('execute', 'digraphs') |
19
|
|
|
self.registry = _parse_digraph_output(digraph_output) |
20
|
|
|
if char1 + char2 not in self.registry: |
21
|
|
|
return self.registry.get(char2 + char1, char2) |
22
|
|
|
return self.registry[char1 + char2] |
23
|
|
|
|
24
|
|
|
def retrieve(self, nvim: object) -> str: |
25
|
|
|
nvim.command('echon "?"') |
26
|
|
|
code1 = nvim.call('getchar') |
27
|
|
|
if isinstance(code1, str) and code1.startswith('\udc80'): |
28
|
|
|
return Key.represent(ensure_bytes(nvim, code1)) |
29
|
|
|
code2 = ensure_bytes(nvim, nvim.call('getchar')) |
30
|
|
|
if isinstance(code2, str) and code2.startswith('\udc80'): |
31
|
|
|
return Key.represent(ensure_bytes(nvim, code2)) |
32
|
|
|
char1 = nvim.call('nr2char', code1) |
33
|
|
|
char2 = nvim.call('nr2char', code2) |
34
|
|
|
return self.find(nvim, char1, char2) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def _parse_digraph_output(output: str) -> dict: |
38
|
|
|
output = output.replace('\n', '') |
39
|
|
|
output = output.replace('\r', '') |
40
|
|
|
return { |
41
|
|
|
m.group(1): m.group(2) |
42
|
|
|
for m in DIGRAPH_PATTERN.finditer(output) |
43
|
|
|
} |
44
|
|
|
|