diff options
author | Peng Wu <alexepico@gmail.com> | 2018-06-13 15:42:24 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2018-06-13 15:42:24 +0800 |
commit | 78dbd77ee0bf15dcab4f72b1cd4733d54ad55867 (patch) | |
tree | 28907661e17596901edfae560df5d7dbaded9b11 | |
parent | 4b9d8723f13108be4e2da91d72db9f32b9177e4f (diff) | |
download | ibus-libpinyin-78dbd77ee0bf15dcab4f72b1cd4733d54ad55867.tar.gz ibus-libpinyin-78dbd77ee0bf15dcab4f72b1cd4733d54ad55867.tar.xz ibus-libpinyin-78dbd77ee0bf15dcab4f72b1cd4733d54ad55867.zip |
write class LuaTriggerCandidates
-rw-r--r-- | src/PYPEnhancedCandidates.h | 3 | ||||
-rw-r--r-- | src/PYPLuaTriggerCandidates.cc | 103 | ||||
-rw-r--r-- | src/PYPLuaTriggerCandidates.h | 13 | ||||
-rw-r--r-- | src/PYPPhoneticEditor.h | 3 |
4 files changed, 116 insertions, 6 deletions
diff --git a/src/PYPEnhancedCandidates.h b/src/PYPEnhancedCandidates.h index 8c2a33f..7cf6709 100644 --- a/src/PYPEnhancedCandidates.h +++ b/src/PYPEnhancedCandidates.h @@ -31,8 +31,7 @@ namespace PY { class PhoneticEditor; enum CandidateType { - CANDIDATE_USER_RAW_INPUT = 1, - CANDIDATE_NBEST_MATCH, + CANDIDATE_NBEST_MATCH = 1, /* not included with user candidate */ CANDIDATE_NORMAL, /* both normal candidate and user candidate */ diff --git a/src/PYPLuaTriggerCandidates.cc b/src/PYPLuaTriggerCandidates.cc new file mode 100644 index 0000000..550a28e --- /dev/null +++ b/src/PYPLuaTriggerCandidates.cc @@ -0,0 +1,103 @@ +/* vim:set et ts=4 sts=4: + * + * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus + * + * Copyright (c) 2018 Peng Wu <alexepico@gmail.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "PYPLuaTriggerCandidates.h" +#include <assert.h> +#include "PYString.h" +#include "PYConfig.h" +#include "PYPPhoneticEditor.h" + +using namespace PY; + +LuaTriggerCandidates::LuaTriggerCandidates (PhoneticEditor *editor) +{ + m_editor = editor; + + m_lua_plugin = ibus_engine_plugin_new (); + + loadLuaScript ( ".." G_DIR_SEPARATOR_S "lua" G_DIR_SEPARATOR_S "base.lua")|| + loadLuaScript (PKGDATADIR G_DIR_SEPARATOR_S "base.lua"); + + gchar * path = g_build_filename (g_get_user_config_dir (), + "ibus", "libpinyin", "user.lua", NULL); + loadLuaScript(path); + g_free(path); +} + +int +LuaTriggerCandidates::loadLuaScript (std::string filename) +{ + return !ibus_engine_plugin_load_lua_script + (m_lua_plugin, filename.c_str ()); +} + +gboolean +LuaTriggerCandidates::processCandidates (std::vector<EnhancedCandidate> & candidates) +{ + EnhancedCandidate enhanced; + enhanced.m_candidate_type = CANDIDATE_LUA_TRIGGER; + enhanced.m_candidate_id = 0; + + std::vector<EnhancedCandidate>::iterator pos; + for (pos = candidates.begin (); pos != candidates.end (); ++pos) { + if (CANDIDATE_NBEST_MATCH != pos->m_candidate_type) + break; + } + + const char * lua_function_name = NULL; + const char * text = m_editor->m_text; + + if (ibus_engine_plugin_match_input + (m_lua_plugin, text, &lua_function_name)) { + ibus_engine_plugin_call(m_lua_plugin, lua_function_name, text); + enhanced.m_display_string = + ibus_engine_plugin_get_first_result (m_lua_plugin); + + candidates.insert (pos, enhanced); + return TRUE; + } else { + int num = std::min + (m_editor->m_config.pageSize (), (guint)candidates.size ()); + for (int i = 0; i < num; ++i) { + text = candidates[i].m_display_string.c_str (); + if (ibus_engine_plugin_match_candidate + (m_lua_plugin, text, &lua_function_name)) { + ibus_engine_plugin_call(m_lua_plugin, lua_function_name, text); + enhanced.m_display_string = + ibus_engine_plugin_get_first_result (m_lua_plugin); + + candidates.insert (pos, enhanced); + return TRUE; + } + } + } + + return FALSE; +} + +SelectCandidateAction +LuaTriggerCandidates::selectCandidate (EnhancedCandidate & enhanced) +{ + assert (CANDIDATE_LUA_TRIGGER == enhanced.m_candidate_type); + assert (0 == enhanced.m_candidate_id); + + return SELECT_CANDIDATE_COMMIT; +} diff --git a/src/PYPLuaTriggerCandidates.h b/src/PYPLuaTriggerCandidates.h index 7c9d42e..3dd67c2 100644 --- a/src/PYPLuaTriggerCandidates.h +++ b/src/PYPLuaTriggerCandidates.h @@ -22,15 +22,18 @@ #ifndef __PY_LIB_PINYIN_LUA_TRIGGER_CANDIDATES_H_ #define __PY_LIB_PINYIN_LUA_TRIGGER_CANDIDATES_H_ +extern "C" { +#include "lua-plugin.h" +} + +#include "PYPointer.h" #include "PYPEnhancedCandidates.h" namespace PY { class LuaTriggerCandidates : public EnhancedCandidates { public: - LuaTriggerCandidates (PhoneticEditor *editor) { - m_editor = editor; - } + LuaTriggerCandidates (PhoneticEditor *editor); public: gboolean processCandidates (std::vector<EnhancedCandidate> & candidates); @@ -38,7 +41,9 @@ public: SelectCandidateAction selectCandidate (EnhancedCandidate & enhanced); protected: - EnhancedCandidate m_candidate; + int loadLuaScript (std::string filename); + + Pointer<IBusEnginePlugin> m_lua_plugin; }; }; diff --git a/src/PYPPhoneticEditor.h b/src/PYPPhoneticEditor.h index 96421fd..a2a0352 100644 --- a/src/PYPPhoneticEditor.h +++ b/src/PYPPhoneticEditor.h @@ -28,16 +28,19 @@ #include "PYPEnhancedCandidates.h" #include "PYPLibPinyinCandidates.h" #include "PYPTradCandidates.h" +#include "PYPLuaTriggerCandidates.h" namespace PY { class LibPinyinCandidates; class TraditionalCandidates; +class LuaTriggerCandidates; class PhoneticEditor : public Editor { friend class LibPinyinCandidates; friend class TraditionalCandidates; + friend class LuaTriggerCandidates; public: PhoneticEditor (PinyinProperties & props, Config & config); virtual ~PhoneticEditor (); |