This commit is contained in:
Tokuhiro Matsuno
2020-09-03 07:52:31 +09:00
parent b0d0993a00
commit e66521d7eb
4 changed files with 88 additions and 25 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/config.py
__pycache__
/comb.xml

View File

@@ -10,6 +10,10 @@ PYTHON ?= /usr/bin/python3
all: comb.xml config.py
check:
python -m py_compile ibus.py
python -m py_compile comb.py
comb.xml: comb.xml.in
sed -e "s:@PYTHON@:$(PYTHON):g;" \
-e "s:@DATADIR@:$(DATADIR):g" $< > $@
@@ -17,16 +21,18 @@ comb.xml: comb.xml.in
config.py: config.py.in
sed -e "s:@SYSCONFDIR@:$(SYSCONFDIR):g" $< > $@
install: all
install: all check
install -m 0755 -d $(DESTDIR)$(DATADIR)/ibus-comb $(DESTDIR)$(SYSCONFDIR)/xdg/comb $(DESTDIR)$(DATADIR)/ibus/component
install -m 0644 comb.svg $(DESTDIR)$(DATADIR)/ibus-comb
install -m 0644 ibus.py $(DESTDIR)$(DATADIR)/ibus-comb
install -m 0644 comb.py $(DESTDIR)$(DATADIR)/ibus-comb
install -m 0644 comb.xml $(DESTDIR)$(DATADIR)/ibus/component
uninstall:
rm -f $(DESTDIR)$(DATADIR)/ibus-comb/comb.svg
rm -f $(DESTDIR)$(DATADIR)/ibus-comb/config.py
rm -f $(DESTDIR)$(DATADIR)/ibus-comb/ibus.py
rm -f $(DESTDIR)$(DATADIR)/ibus-comb/comb.py
rmdir $(DESTDIR)$(DATADIR)/ibus-comb
rmdir $(DESTDIR)$(SYSCONFDIR)/xdg/comb
rm -f $(DESTDIR)$(DATADIR)/ibus/component/comb.xml
@@ -34,3 +40,6 @@ uninstall:
clean:
rm -f comb.xml
rm -f config.py
.PHONY: all check install uninstall

50
comb.py Normal file
View File

@@ -0,0 +1,50 @@
import romkan
import re
import sys
def parse_skkdict(path, encoding='euc-jp'):
result = {}
with open(path, 'r', encoding=encoding) as fp:
for line in fp:
if line.startswith(';;'):
continue
m = line.strip().split(' ', 1)
yomi, kanjis = m
kanjis = kanjis.lstrip('/').rstrip('/').split('/')
kanjis = [re.sub(';.*', '', k) for k in kanjis]
result[yomi] = set(kanjis)
return result
class Comb:
def __init__(self, logger):
self.logger = logger
try:
self.l_jisyo = parse_skkdict('/usr/share/skk/SKK-JISYO.L')
self.logger.info("LOADed JISYO")
except:
self.logger.debug("cannot LOAD JISYO %s" % sys.exc_info()[0])
if not self.l_jisyo:
self.l_jisyo = {}
def convert(self, src):
hiragana = romkan.to_hiragana(src).replace('.', '').replace(',', '')
katakana = romkan.to_kana(src).replace('.', '').replace(',', '')
retval = [
# KANA / KANJI KOUHO
(hiragana, hiragana),
(katakana, katakana),
]
if hiragana in self.l_jisyo:
got = self.l_jisyo[hiragana]
self.logger.debug("GOT: %s" % str(got))
for e in got:
retval.append([e, e])
retval.append([src, src])
return retval

51
ibus.py
View File

@@ -31,20 +31,16 @@ import sys
import getopt
import locale
from comb import Comb
import romkan
import logging
__base_dir__ = os.path.dirname(__file__)
logfp = open('/tmp/ibus-comb.log', 'w')
debug_on = True
def debug(msg):
if debug_on:
logfp.write(msg + "\n")
logfp.flush()
debug("bootstrap")
logging.debug(msg)
# gee thank you IBus :-)
num_keys = []
@@ -70,6 +66,7 @@ class CombIBusEngine(IBus.Engine):
self.preedit_string = ''
self.lookup_table = IBus.LookupTable.new(10, 0, True, True)
self.prop_list = IBus.PropList()
self.comb = Comb(logging.getLogger('Comb'))
debug("Create Comb engine OK")
@@ -141,14 +138,19 @@ class CombIBusEngine(IBus.Engine):
self.cursor_down()
return True
if keyval == IBus.space and len(self.preedit_string) == 0:
# Insert space if that's all you typed (so you can more easily
# type a bunch of emoji separated by spaces)
return False
if keyval == IBus.space:
if len(self.preedit_string) == 0:
# もし、まだなにもはいっていなければ、ただの空白をそのままいれる。
return False
else:
debug("cursor down")
self.cursor_down()
return True
# Allow typing all ASCII letters and punctuation, except digits
if ord(' ') <= keyval < ord('0') or \
if ord('!') <= keyval < ord('0') or \
ord('9') < keyval <= ord('~'):
debug("HM????")
if state & (IBus.ModifierType.CONTROL_MASK | IBus.ModifierType.MOD1_MASK) == 0:
self.preedit_string += chr(keyval)
self.invalidate()
@@ -205,17 +207,15 @@ class CombIBusEngine(IBus.Engine):
self.candidates = []
if preedit_len > 0:
comb_results = [
# KANA / KANJI KOUHO
(romkan.to_hiragana(self.preedit_string),
romkan.to_hiragana(self.preedit_string)),
]
debug("HAHAHA %s" % self.preedit_string)
# self.uniemoji.find_characters(self.preedit_string)
for char_sequence, display_str in comb_results:
candidate = IBus.Text.new_from_string(display_str)
self.candidates.append(char_sequence)
self.lookup_table.append_candidate(candidate)
try:
comb_results = self.comb.convert(self.preedit_string)
debug("HAHAHA %s, %s" % (str(self.preedit_string), str(comb_results)))
for char_sequence, display_str in comb_results:
candidate = IBus.Text.new_from_string(display_str)
self.candidates.append(char_sequence)
self.lookup_table.append_candidate(candidate)
except:
debug("cannot get kanji candidates %s" % (sys.exc_info()[0]))
# にほんご ですね.
text = IBus.Text.new_from_string(self.candidates[0] if len(self.candidates)>0 else self.preedit_string)
@@ -275,6 +275,9 @@ class IMApp:
self.bus.connect("disconnected", self.bus_disconnected_cb)
self.factory = IBus.Factory.new(self.bus.get_connection())
self.factory.add_engine("comb", GObject.type_from_name("CombIBusEngine"))
logging.basicConfig(level=logging.DEBUG, filename='/tmp/ibus-comb.log', filemode='w')
if exec_by_ibus:
self.bus.request_name("org.freedesktop.IBus.Comb", 0)
else: