Debian Codegraph @

im/table/ime.h

raw symbols SCIP
1 /*
2 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7 #ifndef _TABLE_TABLEDICTRESOLVER_H_
8 #define _TABLE_TABLEDICTRESOLVER_H_
9
10 #include <fcitx-config/configuration.h>
11 #include <fcitx-config/enum.h>
12 #include <fcitx-config/option.h>
13 #include <fcitx-config/rawconfig.h>
14 #include <fcitx-utils/i18n.h>
15 #include <fcitx-utils/key.h>
16 #include <fcitx-utils/keysym.h>
17 #include <fcitx-utils/log.h>
18 #include <fcitx-utils/macros.h>
19 #include <fcitx-utils/misc.h>
20 #include <fcitx/candidatelist.h>
21 #include <libime/core/languagemodel.h>
22 #include <libime/core/prediction.h>
23 #include <libime/core/userlanguagemodel.h>
24 #include <libime/table/tablebaseddictionary.h>
25 #include <memory>
26 #include <string>
27 #include <tuple>
28 #include <unordered_map>
29 #include <unordered_set>
30 #include <vector>
31
32 namespace fcitx {
33
34 enum class OrderPolicy { No, Freq, Fast };
35 FCITX_CONFIG_ENUM_NAME_WITH_I18N(OrderPolicy, N_("No"), N_("Freq"), N_("Fast"));
36 FCITX_CONFIG_ENUM_NAME_WITH_I18N(CandidateLayoutHint, N_("Not set"),
37 N_("Vertical"), N_("Horizontal"));
38
39 struct NoSaveAnnotation {
40 bool skipDescription() { return true; }
41 bool skipSave() { return true; }
42 void dumpDescription(RawConfig &config) const { FCITX_UNUSED(config); }
43 };
44
45 template <typename T>
46 using NoSaveOption = OptionWithAnnotation<T, NoSaveAnnotation>;
47
48 FCITX_CONFIGURATION(
49 TableConfig, NoSaveOption<std::string> file{this, "File", _("File")};
50 KeyListOption prevPage{
51 this,
52 "PrevPage",
53 _("Previous page"),
54 {Key(FcitxKey_Up)},
55 KeyListConstrain(KeyConstrainFlag::AllowModifierLess)};
56 KeyListOption nextPage{
57 this,
58 "NextPage",
59 _("Next page"),
60 {Key(FcitxKey_Down)},
61 KeyListConstrain(KeyConstrainFlag::AllowModifierLess)};
62 KeyListOption prevCandidate{
63 this,
64 "PrevCandidate",
65 _("Previous Candidate"),
66 {Key("Left")},
67 KeyListConstrain(KeyConstrainFlag::AllowModifierLess)};
68 KeyListOption nextCandidate{
69 this,
70 "NextCandidate",
71 _("Next Candidate"),
72 {Key("Right")},
73 KeyListConstrain(KeyConstrainFlag::AllowModifierLess)};
74 KeyListOption defaultCandidate{
75 this,
76 "DefaultCandidate",
77 _("Select Current Candidate"),
78 {Key(FcitxKey_space)},
79 KeyListConstrain({KeyConstrainFlag::AllowModifierLess})};
80 KeyListOption secondCandidate{
81 this,
82 "SecondCandidate",
83 _("Select Second Candidate"),
84 {},
85 KeyListConstrain({KeyConstrainFlag::AllowModifierLess,
86 KeyConstrainFlag::AllowModifierOnly})};
87 KeyListOption thirdCandidate{
88 this,
89 "ThirdCandidate",
90 _("Select Third Candidate"),
91 {},
92 KeyListConstrain({KeyConstrainFlag::AllowModifierLess,
93 KeyConstrainFlag::AllowModifierOnly})};
94 NoSaveOption<KeyList> selection{
95 this,
96 "Selection",
97 _("Selection"),
98 {Key(FcitxKey_1), Key(FcitxKey_2), Key(FcitxKey_3), Key(FcitxKey_4),
99 Key(FcitxKey_5), Key(FcitxKey_6), Key(FcitxKey_7), Key(FcitxKey_8),
100 Key(FcitxKey_9), Key(FcitxKey_0)}};
101 Option<int, IntConstrain, DefaultMarshaller<int>, ToolTipAnnotation>
102 pageSize{
103 this,
104 "PageSize",
105 _("Candidates Per Page"),
106 5,
107 IntConstrain(0, 10),
108 {},
109 {_("0 means no candidate will be displayed and a special compose "
110 "table mode will be enabled. This means you will "
111 "not be able to manually select candidate. This may be suitable "
112 "for table that only relies on auto selection. It will also "
113 "implicitly disable many standard key handling, Enter to "
114 "commit code, Tab to select candidate, cursor moving key (e.g. "
115 "Arrow, Page Up/Down) to trigger auto select and forward cursor "
116 "moving key to client, in order to simulate a more compose "
117 "table like experience.")}};
118 SubConfigOption punctuationMap{this, "TableGlobal",
119 _("Table Global Options"),
120 "fcitx://config/addon/table"};
121 // Fcitx 4 default behavior.
122 Option<bool> commitAfterSelect{this, "CommitAfterSelect",
123 _("Commit after auto select candidates"),
124 true};
125 Option<bool> commitWhenDeactivate{
126 this, "CommitWhenDeactivate",
127 _("Commit buffer when deactivating input method"), true};
128 OptionWithAnnotation<bool, ToolTipAnnotation> commitInvalidSegment{
129 this,
130 "CommitInvalidSegment",
131 _("Commit Invalid Segment"),
132 false,
133 {},
134 {},
135 ToolTipAnnotation(
136 _("Commit the segment that does not exist in the table. This "
137 "option is useful for some latin/compose based table."))};
138 Option<bool> useFullWidth{this, "UseFullWidth", _("Use full width"), true};
139 Option<bool> ignorePunc{this, "IgnorePunc",
140 _("Ignore built in punctuation"), false};
141 Option<bool> firstCandidateAsPreedit{this, "FirstCandidateAsPreedit",
142 _("First candidate as Preedit"),
143 false};
144 Option<bool> preeditCursorPositionAtBeginning{
145 this, "PreeditCursorPositionAtBeginning",
146 _("Fix embedded preedit cursor at the beginning of the preedit"),
147 !isAndroid() && !isApple() && !isEmscripten()};
148 Option<Key, KeyConstrain> quickphrase{
149 this,
150 "QuickPhraseKey",
151 _("Key to trigger quickphrase"),
152 Key{},
153 {KeyConstrainFlag::AllowModifierLess}};
154 OptionWithAnnotation<std::string, ToolTipAnnotation> quickphraseText{
155 this,
156 "QuickPhraseText",
157 _("Text to trigger quickphrase"),
158 "",
159 {},
160 {},
161 {_("When a new character is typed, if it matches any character in this "
162 "string, all the existing text will be put into quickphrase "
163 "buffer.")}};
164 NoSaveOption<std::string> icon{this, "Icon", _("Icon")};
165 Option<int> noSortInputLength{this, "NoSortInputLength",
166 _("Don't sort word shorter than")};
167 Option<bool> sortByCodeLength{this, "SortByCodeLength",
168 _("Sort candidate by code length"), true};
169 OptionWithAnnotation<OrderPolicy, OrderPolicyI18NAnnotation> orderPolicy{
170 this, "OrderPolicy", _("Order policy")};
171 OptionWithAnnotation<bool, ToolTipAnnotation> useSystemLanguageModel{
172 this,
173 "UseSystemLanguageModel",
174 _("Use system language model"),
175 true,
176 {},
177 {},
178 ToolTipAnnotation(_("When available, system language model will be "
179 "loaded for given language and provide ability to "
180 "give better context based order."))};
181 OptionWithAnnotation<bool, ToolTipAnnotation> useContextBasedOrder{
182 this,
183 "UseContextRelatedOrder",
184 _("Sort candidates based on current context"),
185 true,
186 {},
187 {},
188 ToolTipAnnotation(
189 _("Order the candidate based on the word you already typed. You "
190 "may want to disable this option if you want the candidate order "
191 "to be stable regardless of the history."))};
192 Option<Key, KeyConstrain> matchingKey{
193 this,
194 "MatchingKey",
195 _("Wildcard matching Key"),
196 Key(),
197 {KeyConstrainFlag::AllowModifierLess}};
198 Option<Key, KeyConstrain> pinyinKey{this,
199 "PinyinKey",
200 _("Prefix key to trigger Pinyin"),
201 Key(),
202 {KeyConstrainFlag::AllowModifierLess}};
203
204 Option<bool> autoSelect{this, "AutoSelect", _("Auto select candidate")};
205 OptionWithAnnotation<int, ToolTipAnnotation> autoSelectLength{
206 this,
207 "AutoSelectLength",
208 _("Length limit of selecting the only candidate"),
209 0,
210 {},
211 {},
212 ToolTipAnnotation(
213 _("When current input length is greater than or equal to the limit "
214 "and there is only one candidate available, automatically select "
215 "this candidate. -1 Means the maximum code length of the table. "
216 "0 means this behavior is disabled."))};
217 OptionWithAnnotation<std::string, ToolTipAnnotation> autoSelectRegex{
218 this,
219 "AutoSelectRegex",
220 _("Regular expression for selecting the only candidate"),
221 "",
222 {},
223 {},
224 ToolTipAnnotation(_("When current input matches the regular expression "
225 "and there is only one candidate available, "
226 "automatically select this candidate."))};
227 OptionWithAnnotation<int, ToolTipAnnotation> noMatchAutoSelectLength{
228 this,
229 "NoMatchAutoSelectLength",
230 _("Auto select when no match is found"),
231 0,
232 {},
233 {},
234 ToolTipAnnotation(
235 _("When typing a new character, if the current input length is "
236 "greater than or equal to the limit, and the existing segment "
237 "with the new character does not have any matched entries in the "
238 "table, select the current candidate and then type in the new "
239 "character. -1 means the maximum code length of the table. 0 "
240 "means this behavior is disabled."))};
241 OptionWithAnnotation<std::string, ToolTipAnnotation> noMatchAutoSelectRegex{
242 this,
243 "NoMatchAutoSelectRegex",
244 _("Regular expression for auto select when no match is found"),
245 "",
246 {},
247 {},
248 ToolTipAnnotation(
249 _("When typing a new character, if the current input matches the "
250 "regular expression and the existing segment "
251 "with the new character does not have any matched entries in the "
252 "table, select the current candidate and then type in the new "
253 "character."))};
254 NoSaveOption<KeyList> endKey{this, "EndKey", _("End key")};
255 OptionWithAnnotation<int, ToolTipAnnotation> autoPhraseLength{
256 this,
257 "AutoPhraseLength",
258 _("Auto phrase length"),
259 -1,
260 {},
261 {},
262 ToolTipAnnotation(
263 _("After committing every character, learn the new phrase based on "
264 "the input history. If certain word is seen for multiple times "
265 "or selected by user once, it will be added as user phrase. -1 "
266 "means the maximum code length of the table. 0 means disable "
267 "this feature. "))};
268 OptionWithAnnotation<bool, ToolTipAnnotation> autoPhraseWithPhrase{
269 this,
270 "AutoPhraseWithPhrase",
271 _("Learn auto phrase with phrase"),
272 false,
273 {},
274 {},
275 ToolTipAnnotation(_("Whether to learn new phrase from other phrases or "
276 "just single characters."))};
277 Option<std::string> markerForAutoPhrase{
278 this, "MarkerForAutoPhrase",
279 _("Marker for auto phrase in the candidates"), "*"};
280 OptionWithAnnotation<int, ToolTipAnnotation> saveAutoPhraseAfter{
281 this,
282 "SaveAutoPhraseAfter",
283 _("Save auto phrase after being typed for ... times"),
284 -1,
285 {},
286 {},
287 ToolTipAnnotation(
288 _("If the value less than 0, means this feature is disabled. If "
289 "auto phrase is enabled, selecting auto phrase candidate "
290 "explicitly will still add the word as a new user phrase."))};
291 Option<bool> exactMatch{this, "ExactMatch", _("Exact Match")};
292 Option<bool> learning{this, "Learning", _("Learning"), true};
293 Option<bool> hint{this, "Hint", _("Display Hint for word")};
294 Option<bool> displayCustomHint{this, "DisplayCustomHint",
295 _("Display custom hint")};
296 Option<std::string> hintSeparator{
297 this, "HintSeparator", _("Separator text between hint and candidate"),
298 "~ "};
299 Option<bool> spaceBeforeHint{
300 this, "SpaceBeforeHint",
301 _("Show space between candidate and hint text"), true};
302 OptionWithAnnotation<CandidateLayoutHint, CandidateLayoutHintI18NAnnotation>
303 candidateLayoutHint{this, "CandidateLayoutHint",
304 _("Candidate List orientation"),
305 CandidateLayoutHint::NotSet};
306 Option<bool> keypadAsInput{this, "KeypadAsInput",
307 _("Allow use keypad key as table input"), false};
308 NoSaveOption<std::vector<std::string>> autoRuleSet{this, "AutoRuleSet",
309 _("Auto rule set")};);
310
311 FCITX_CONFIGURATION(PartialIMInfo, HiddenOption<std::string> languageCode{
312 this, "LangCode", "Language Code"};);
313
314 FCITX_CONFIGURATION(TableConfigRoot,
315 Option<TableConfig> config{this, "Table", "Table"};
316 Option<PartialIMInfo, NoConstrain<PartialIMInfo>,
317 DefaultMarshaller<PartialIMInfo>, NoSaveAnnotation>
318 im{this, "InputMethod", "InputMethod"};);
319
320 struct TableData {
321 TableConfigRoot root;
322 std::unique_ptr<libime::TableBasedDictionary> dict;
323 std::unique_ptr<libime::UserLanguageModel> model;
324 };
325
326 class TableIME {
327 public:
328 TableIME(libime::LanguageModelResolver *lmResolver);
329
330 const TableConfig &config(const std::string &name);
331
332 std::tuple<libime::TableBasedDictionary *, libime::UserLanguageModel *,
333 const TableConfig *>
334 requestDict(const std::string &name);
335 void saveDict(const std::string &name);
336 void saveAll();
337 void updateConfig(const std::string &name, const RawConfig &config);
338
339 void releaseUnusedDict(const std::unordered_set<std::string> &names);
340 void reloadAllDict();
341
342 private:
343 libime::LanguageModelResolver *lm_;
344 std::unordered_map<std::string, TableData> tables_;
345 };
346
347 FCITX_DECLARE_LOG_CATEGORY(table_logcategory);
348
349 #define TABLE_DEBUG() FCITX_LOGC(::fcitx::table_logcategory, Debug)
350 #define TABLE_ERROR() FCITX_LOGC(::fcitx::table_logcategory, Error)
351 } // namespace fcitx
352
353 #endif // _TABLE_TABLEDICTRESOLVER_H_