diff --git a/Makefile b/Makefile index 9821fff..dd66880 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,13 @@ PREFIX = /usr APP_ROOT = $(DESTDIR)$(PREFIX)/share/ibus-bogo IBUS_ROOT = $(DESTDIR)$(PREFIX)/share/ibus +PYTHON = $(shell which python3) all: run: - python3 ibus_engine/main.py + ${PYTHON} ibus_engine/main.py install: # TODO: More fine-grain copying @@ -15,9 +16,9 @@ install: mkdir --parent $(IBUS_ROOT)/component/ cp ibus_engine/data/bogo.xml $(IBUS_ROOT)/component/ sed -i \ - -e "s|@EXEC_PATH@|python3 $(APP_ROOT)/ibus_engine/main.py|g" \ + -e "s|@EXEC_PATH@|${PYTHON} $(APP_ROOT)/ibus_engine/main.py|g" \ -e "s|@ICON_PATH@|${APP_ROOT}/ibus_engine/data/ibus-bogo-dev.svg|g" \ - -e "s|@SETUP_PATH@|python3 ${APP_ROOT}/gui/controller.py|g" \ + -e "s|@SETUP_PATH@|${PYTHON} ${APP_ROOT}/gui/controller.py|g" \ $(IBUS_ROOT)/component/bogo.xml diff --git a/ibus_engine/base_config.py b/ibus_engine/base_config.py index bcda482..8325f67 100644 --- a/ibus_engine/base_config.py +++ b/ibus_engine/base_config.py @@ -45,7 +45,8 @@ "lxterminal", "konsole", "geany", - "skype" + "skype", + "__nan", ], "typo-correction-level": 2, "typo-correction-threshold": 1 diff --git a/ibus_engine/ibus_engine.py b/ibus_engine/ibus_engine.py index 670cc63..f0cb5f0 100644 --- a/ibus_engine/ibus_engine.py +++ b/ibus_engine/ibus_engine.py @@ -26,25 +26,21 @@ import os import subprocess import logging -import enchant #from mouse_detector import MouseDetector from ui import UiDelegate from preedit_backend import PreeditBackend from surrounding_text_backend import SurroundingTextBackend -from auto_corrector import AutoCorrector logger = logging.getLogger(__name__) ENGINE_PATH = os.path.dirname(__file__) -DICT_PATH = ENGINE_PATH + '/data' -PWL_PATH = os.path.expanduser('~/.config/ibus-bogo/spelling-blacklist.txt') class Engine(IBus.Engine): __gtype_name__ = 'EngineBoGo' - def __init__(self, config, abbr_expander): + def __init__(self, config, abbr_expander, auto_corrector): super().__init__() self.caps = 0 @@ -53,22 +49,6 @@ def __init__(self, config, abbr_expander): self.config = config self.ui_delegate = UiDelegate(engine=self) - custom_broker = enchant.Broker() - custom_broker.set_param( - 'enchant.myspell.dictionary.path', - DICT_PATH) - - spellchecker = enchant.DictWithPWL( - 'vi_VN_telex', - pwl=PWL_PATH, - broker=custom_broker) - - # FIXME: Catch enchant.errors.DictNotFoundError exception here. - english_spellchecker = enchant.Dict('en_US') - - auto_corrector = AutoCorrector( - config, spellchecker, english_spellchecker) - self.preedit_backend = PreeditBackend( engine=self, config=config, @@ -168,8 +148,11 @@ def find_focused_executable(self): "awk '/_NET_WM_PID\(CARDINAL\)/{print $NF}'", shell=True).decode().strip() - self.focused_exe = os.path.realpath( - "/proc/{0}/exe".format(focused_pid)) + if focused_pid: + self.focused_exe = os.path.realpath( + "/proc/{0}/exe".format(focused_pid)) + else: + self.focused_exe = '__nan' logger.debug("%s focused", self.focused_exe) diff --git a/ibus_engine/main.py b/ibus_engine/main.py index 5301f83..967bcf7 100644 --- a/ibus_engine/main.py +++ b/ibus_engine/main.py @@ -27,6 +27,7 @@ import locale import logging import argparse +import enchant ENGINE_PATH = os.path.dirname(__file__) + "/" sys.path.append(os.path.abspath(ENGINE_PATH + "..")) @@ -35,9 +36,12 @@ from ibus_engine import Engine from config import Config from abbr import AbbreviationExpander +from auto_corrector import AutoCorrector current_path = os.path.dirname(os.path.abspath(__file__)) +DICT_PATH = ENGINE_PATH + '/data' +PWL_PATH = os.path.expanduser('~/.config/ibus-bogo/spelling-blacklist.txt') class IMApp: @@ -90,6 +94,21 @@ def __init__(self, exec_by_ibus): self.bus.register_component(self.component) self.bus.set_global_engine_async( "bogo", -1, None, None, None) + custom_broker = enchant.Broker() + custom_broker.set_param( + 'enchant.myspell.dictionary.path', + DICT_PATH) + + spellchecker = enchant.DictWithPWL( + 'vi_VN_telex', + pwl=PWL_PATH, + broker=custom_broker) + + # FIXME: Catch enchant.errors.DictNotFoundError exception here. + english_spellchecker = enchant.Dict('en_US') + + self.auto_corrector = AutoCorrector( + self.config, spellchecker, english_spellchecker) def create_engine(self, factory, engine_name): if engine_name == "bogo": @@ -116,7 +135,7 @@ def create_engine(self, factory, engine_name): sys.stderr = stderr f.close() - Engine.__init__(engine, self.config, self.abbr_expander) + Engine.__init__(engine, self.config, self.abbr_expander, self.auto_corrector) self.engine_count += 1 return engine