~ubuntu-branches/ubuntu/trusty/fcitx/trusty-proposed

« back to all changes in this revision

Viewing changes to src/im/pinyin/py.c

  • Committer: Package Import Robot
  • Author(s): Aron Xu
  • Date: 2013-02-10 17:03:56 UTC
  • mfrom: (1.3.18) (33.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20130210170356-2yuv6xy3ed378kn0
Tags: 1:4.2.7-1
* New upstream release.
* New binary packages:
  - fcitx-libs-gclient: D-Bus client library for Glib
  - fcitx-libs-qt: D-Bus client library for Qt
  - fcitx-module-quickphrase-editor: Quick Phrase editor module

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include "fcitx/ime.h"
35
35
#include "fcitx/keys.h"
36
36
#include "fcitx/ui.h"
 
37
#include "fcitx/frontend.h"
 
38
#include "fcitx/module.h"
 
39
#include "fcitx/context.h"
 
40
#include "fcitx/profile.h"
 
41
#include "fcitx/configfile.h"
 
42
#include "fcitx-config/xdg.h"
 
43
#include "fcitx-utils/utf8.h"
 
44
#include "fcitx-utils/log.h"
37
45
#include "py.h"
38
46
#include "PYFA.h"
39
47
#include "pyParser.h"
40
48
#include "sp.h"
41
 
#include "fcitx-utils/utf8.h"
42
 
#include "fcitx/profile.h"
43
 
#include "fcitx/configfile.h"
44
 
#include "fcitx-utils/log.h"
45
 
#include "fcitx-config/xdg.h"
46
49
#include "pyconfig.h"
47
 
#include "fcitx/instance.h"
48
 
#include "fcitx/frontend.h"
49
 
#include "fcitx/module.h"
50
 
#include "fcitx/candidate.h"
51
 
#include "fcitx/context.h"
52
50
#include "spdata.h"
 
51
#include "module/quickphrase/fcitx-quickphrase.h"
53
52
 
54
53
#define PY_INDEX_MAGIC_NUMBER 0xf7462e34
55
54
#define PINYIN_TEMP_FILE "pinyin_XXXXXX"
56
55
 
57
 
FCITX_EXPORT_API
58
 
FcitxIMClass ime = {
59
 
    PYCreate,
60
 
    NULL
61
 
};
62
 
 
63
 
FCITX_EXPORT_API
64
 
int ABI_VERSION = FCITX_ABI_VERSION;
65
 
 
66
 
const UT_icd pycand_icd = {sizeof(PYCandWord*) , NULL, NULL, NULL };
67
 
 
68
 
typedef struct _PYCandWordSortContext {
 
56
typedef struct {
69
57
    PY_CAND_WORD_TYPE type;
70
58
    ADJUSTORDER order;
71
59
    FcitxPinyinState* pystate;
72
60
} PYCandWordSortContext;
73
61
 
74
 
static void LoadPYPhraseDict(FcitxPinyinState* pystate, FILE* fp, boolean isSystem, boolean stripDup);
75
 
static void * LoadPYBaseDictWrapper(void* arg, FcitxModuleFunctionArg args);
76
 
static void * PYGetPYByHZWrapper(void* arg, FcitxModuleFunctionArg args);
77
 
static void * DoPYInputWrapper(void* arg, FcitxModuleFunctionArg args);
78
 
static void * PYGetCandWordsWrapper(void* arg, FcitxModuleFunctionArg args);
79
 
static void * PYGetFindStringWrapper(void* arg, FcitxModuleFunctionArg args);
80
 
static void * PYResetWrapper(void* arg, FcitxModuleFunctionArg args);
 
62
static void LoadPYPhraseDict(FcitxPinyinState* pystate, FILE* fp,
 
63
                             boolean isSystem, boolean stripDup);
81
64
static void ReloadConfigPY(void* arg);
82
65
static void PinyinMigration();
83
66
static int PYCandWordCmp(const void* b, const void* a, void* arg);
84
 
static void* PYSP2QP(void* arg, FcitxModuleFunctionArg args);
85
 
static boolean PYGetPYMapByHZ(FcitxPinyinState*pystate, char *strHZ, char* mapHint, char *strMap);
86
 
static void PYAddUserPhraseFromCString(void* arg, FcitxModuleFunctionArg args);
 
67
static boolean PYGetPYMapByHZ(FcitxPinyinState*pystate, char *strHZ,
 
68
                              char* mapHint, char *strMap);
 
69
 
 
70
FCITX_DEFINE_PLUGIN(fcitx_pinyin, ime2, FcitxIMClass2) = {
 
71
    PYCreate,
 
72
    PYDestroy,
 
73
    ReloadConfigPY,
 
74
    NULL,
 
75
    NULL,
 
76
    NULL,
 
77
    NULL,
 
78
    NULL
 
79
};
 
80
 
 
81
DECLARE_ADDFUNCTIONS(Pinyin)
87
82
 
88
83
void *PYCreate(FcitxInstance* instance)
89
84
{
90
 
    FcitxPinyinState *pystate = fcitx_utils_malloc0(sizeof(FcitxPinyinState));
91
 
    FcitxAddon* pyaddon = FcitxAddonsGetAddonByName(FcitxInstanceGetAddons(instance), FCITX_PINYIN_NAME);
 
85
    FcitxPinyinState *pystate = fcitx_utils_new(FcitxPinyinState);
92
86
    InitMHPY(&pystate->pyconfig.MHPY_C, MHPY_C_TEMPLATE);
93
87
    InitMHPY(&pystate->pyconfig.MHPY_S, MHPY_S_TEMPLATE);
94
88
    InitPYTable(&pystate->pyconfig);
97
91
        free(pystate->pyconfig.MHPY_C);
98
92
        free(pystate->pyconfig.MHPY_S);
99
93
        free(pystate->pyconfig.PYTable);
 
94
        FreePYSplitData(&pystate->pyconfig);
100
95
        free(pystate);
101
96
        return NULL;
102
97
    }
106
101
    pystate->pool = fcitx_memory_pool_create();
107
102
 
108
103
    FcitxInstanceRegisterIM(instance,
109
 
                      pystate,
110
 
                      "pinyin",
111
 
                      _("Pinyin"),
112
 
                      "pinyin",
113
 
                      PYInit,
114
 
                      ResetPYStatus,
115
 
                      DoPYInput,
116
 
                      PYGetCandWords,
117
 
                      NULL,
118
 
                      SavePY,
119
 
                      ReloadConfigPY,
120
 
                      NULL,
121
 
                      5,
122
 
                      "zh_CN"
123
 
                     );
 
104
                            pystate,
 
105
                            "pinyin",
 
106
                            _("Pinyin"),
 
107
                            "pinyin",
 
108
                            PYInit,
 
109
                            ResetPYStatus,
 
110
                            DoPYInput,
 
111
                            PYGetCandWords,
 
112
                            NULL,
 
113
                            SavePY,
 
114
                            NULL,
 
115
                            NULL,
 
116
                            5,
 
117
                            "zh_CN");
124
118
    FcitxInstanceRegisterIM(instance,
125
 
                      pystate,
126
 
                      "shuangpin",
127
 
                      _("Shuangpin"),
128
 
                      "shuangpin",
129
 
                      SPInit,
130
 
                      ResetPYStatus,
131
 
                      DoPYInput,
132
 
                      PYGetCandWords,
133
 
                      NULL,
134
 
                      SavePY,
135
 
                      ReloadConfigPY,
136
 
                      NULL,
137
 
                      5,
138
 
                      "zh_CN"
139
 
                     );
 
119
                            pystate,
 
120
                            "shuangpin",
 
121
                            _("Shuangpin"),
 
122
                            "shuangpin",
 
123
                            SPInit,
 
124
                            ResetPYStatus,
 
125
                            DoPYInput,
 
126
                            PYGetCandWords,
 
127
                            NULL,
 
128
                            SavePY,
 
129
                            NULL,
 
130
                            NULL,
 
131
                            5,
 
132
                            "zh_CN");
140
133
    pystate->owner = instance;
141
134
 
142
 
    /* ensure the order! */
143
 
    AddFunction(pyaddon, LoadPYBaseDictWrapper); // 0
144
 
    AddFunction(pyaddon, PYGetPYByHZWrapper); // 1
145
 
    AddFunction(pyaddon, DoPYInputWrapper); // 2
146
 
    AddFunction(pyaddon, PYGetCandWordsWrapper); // 3
147
 
    AddFunction(pyaddon, PYGetFindStringWrapper); // 4
148
 
    AddFunction(pyaddon, PYResetWrapper); // 5
149
 
    AddFunction(pyaddon, PYSP2QP); // 6
150
 
    AddFunction(pyaddon, PYAddUserPhraseFromCString); // 6
 
135
    FcitxPinyinAddFunctions(instance);
151
136
    return pystate;
152
137
}
153
138
 
 
139
void PYDestroy(void* arg)
 
140
{
 
141
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
 
142
    free(pystate->pyconfig.MHPY_C);
 
143
    free(pystate->pyconfig.MHPY_S);
 
144
    free(pystate->pyconfig.PYTable);
 
145
    FreePYSplitData(&pystate->pyconfig);
 
146
    FcitxConfigFree(&pystate->pyconfig.gconfig);
 
147
    fcitx_memory_pool_destroy(pystate->pool);
 
148
 
 
149
    int i, j, k;
 
150
    PYFA *PYFAList = pystate->PYFAList;
 
151
    for (i = 0; i < pystate->iPYFACount; i++) {
 
152
        for (j = 0; j < PYFAList[i].iBase; j++) {
 
153
            PyPhrase* phrase = USER_PHRASE_NEXT(PYFAList[i].pyBase[j].userPhrase);
 
154
            for (k = 0; k < PYFAList[i].pyBase[j].iUserPhrase; k++) {
 
155
                PyPhrase* cur = phrase;
 
156
                fcitx_utils_free(cur->strPhrase);
 
157
                fcitx_utils_free(cur->strMap);
 
158
                phrase = USER_PHRASE_NEXT(phrase);
 
159
                free(cur);
 
160
            }
 
161
 
 
162
            free(PYFAList[i].pyBase[j].userPhrase);
 
163
            fcitx_utils_free(PYFAList[i].pyBase[j].phrase);
 
164
        }
 
165
        free(PYFAList[i].pyBase);
 
166
    }
 
167
    free(PYFAList);
 
168
 
 
169
    while(pystate->pyFreq) {
 
170
        PyFreq* pCurFreq = pystate->pyFreq;
 
171
        pystate->pyFreq = pCurFreq->next;
 
172
        while (pCurFreq->HZList) {
 
173
            HZ* pHZ = pCurFreq->HZList;
 
174
            pCurFreq->HZList = pHZ->next;
 
175
            free(pHZ);
 
176
        }
 
177
        free(pCurFreq);
 
178
    }
 
179
 
 
180
    free(pystate);
 
181
}
 
182
 
154
183
boolean PYInit(void *arg)
155
184
{
156
185
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
180
209
boolean LoadPYBaseDict(FcitxPinyinState *pystate)
181
210
{
182
211
    FILE *fp;
183
 
    int i, j, iLen;
 
212
    int i, j;
 
213
    int32_t iLen;
184
214
 
185
215
    fp = FcitxXDGGetFileWithPrefix("pinyin", PY_BASE_FILE, "r", NULL);
186
216
    if (!fp)
187
217
        return false;
188
218
 
189
 
    fread(&pystate->iPYFACount, sizeof(int), 1, fp);
190
 
    pystate->PYFAList = (PYFA *) fcitx_utils_malloc0(sizeof(PYFA) * pystate->iPYFACount);
191
 
    PYFA* PYFAList = pystate->PYFAList;
 
219
    fcitx_utils_read_int32(fp, &pystate->iPYFACount);
 
220
    pystate->PYFAList = (PYFA*)fcitx_utils_malloc0(sizeof(PYFA) * pystate->iPYFACount);
 
221
    PYFA *PYFAList = pystate->PYFAList;
192
222
    for (i = 0; i < pystate->iPYFACount; i++) {
193
223
        fread(PYFAList[i].strMap, sizeof(char) * 2, 1, fp);
194
224
        PYFAList[i].strMap[2] = '\0';
195
225
 
196
 
        fread(&(PYFAList[i].iBase), sizeof(int), 1, fp);
197
 
        PYFAList[i].pyBase = (PyBase *) fcitx_utils_malloc0(sizeof(PyBase) * PYFAList[i].iBase);
 
226
        fcitx_utils_read_int32(fp, &PYFAList[i].iBase);
 
227
        PYFAList[i].pyBase = (PyBase*)fcitx_utils_malloc0(sizeof(PyBase) * PYFAList[i].iBase);
198
228
        for (j = 0; j < PYFAList[i].iBase; j++) {
199
229
            int8_t len;
200
230
            fread(&len, sizeof(char), 1, fp);
201
231
            fread(PYFAList[i].pyBase[j].strHZ, sizeof(char) * len, 1, fp);
202
232
            PYFAList[i].pyBase[j].strHZ[len] = '\0';
203
 
            fread(&iLen, sizeof(int), 1, fp);
 
233
            fcitx_utils_read_int32(fp, &iLen);
204
234
            PYFAList[i].pyBase[j].iIndex = iLen;
205
235
            PYFAList[i].pyBase[j].iHit = 0;
206
236
            if (iLen > pystate->iCounter)
207
237
                pystate->iCounter = iLen;
208
238
            PYFAList[i].pyBase[j].iPhrase = 0;
209
239
            PYFAList[i].pyBase[j].iUserPhrase = 0;
210
 
            PYFAList[i].pyBase[j].userPhrase = (PyUsrPhrase *) fcitx_utils_malloc0(sizeof(PyUsrPhrase));
 
240
            PYFAList[i].pyBase[j].userPhrase = fcitx_utils_new(PyUsrPhrase);
211
241
            PYFAList[i].pyBase[j].userPhrase->next = PYFAList[i].pyBase[j].userPhrase;
212
242
        }
213
243
    }
217
247
 
218
248
    pystate->iOrigCounter = pystate->iCounter;
219
249
 
220
 
    pystate->pyFreq = (PyFreq *) fcitx_utils_malloc0(sizeof(PyFreq));
221
 
    pystate->pyFreq->next = NULL;
 
250
    pystate->pyFreq = fcitx_utils_new(PyFreq);
 
251
    // pystate->pyFreq->next = NULL;
222
252
 
223
253
    return true;
224
254
}
225
255
 
226
256
void LoadPYPhraseDict(FcitxPinyinState* pystate, FILE *fp, boolean isSystem, boolean stripDup)
227
257
{
228
 
    int i, j , k, count, iLen;
 
258
    int j, k;
 
259
    int32_t i, count, iLen;
229
260
    char strBase[UTF8_MAX_LENGTH + 1];
230
261
    PyPhrase *phrase = NULL, *temp;
231
262
    PYFA* PYFAList = pystate->PYFAList;
232
263
    while (!feof(fp)) {
233
264
        int8_t clen;
234
 
        if (!fread(&i, sizeof(int), 1, fp))
 
265
        if (!fcitx_utils_read_int32(fp, &i))
235
266
            break;
236
267
        if (!fread(&clen, sizeof(int8_t), 1, fp))
237
268
            break;
240
271
        if (!fread(strBase, sizeof(char) * clen, 1, fp))
241
272
            break;
242
273
        strBase[clen] = '\0';
243
 
        if (!fread(&count, sizeof(int), 1, fp))
 
274
        if (!fcitx_utils_read_int32(fp, &count))
244
275
            break;
245
276
 
246
277
        j = GetBaseIndex(pystate, i, strBase);
248
279
            break;
249
280
 
250
281
        if (isSystem) {
251
 
            phrase = (PyPhrase *) fcitx_utils_malloc0(sizeof(PyPhrase) * count);
 
282
            phrase = (PyPhrase*)fcitx_utils_malloc0(sizeof(PyPhrase) * count);
252
283
            temp = phrase;
253
284
        } else {
254
285
            PYFAList[i].pyBase[j].iUserPhrase = count;
257
288
 
258
289
        for (k = 0; k < count; k++) {
259
290
            if (!isSystem)
260
 
                phrase = (PyPhrase *) fcitx_utils_malloc0(sizeof(PyUsrPhrase));
261
 
 
262
 
            fread(&iLen, sizeof(int), 1, fp);
263
 
 
264
 
            if (isSystem)
265
 
                phrase->strMap = (char* ) fcitx_memory_pool_alloc(pystate->pool, sizeof(char) * (iLen + 1));
266
 
            else
267
 
                phrase->strMap = (char *) fcitx_utils_malloc0(sizeof(char) * (iLen + 1));
 
291
                phrase = (PyPhrase*)fcitx_utils_malloc0(sizeof(PyUsrPhrase));
 
292
 
 
293
            fcitx_utils_read_int32(fp, &iLen);
 
294
 
 
295
            if (isSystem) {
 
296
                phrase->strMap = (char*)fcitx_memory_pool_alloc(pystate->pool, sizeof(char) * (iLen + 1));
 
297
            } else {
 
298
                phrase->strMap = (char*)fcitx_utils_malloc0(sizeof(char) * (iLen + 1));
 
299
            }
268
300
            fread(phrase->strMap, sizeof(char) * iLen, 1, fp);
269
301
            phrase->strMap[iLen] = '\0';
270
302
 
271
 
            fread(&iLen, sizeof(int), 1, fp);
 
303
            fcitx_utils_read_int32(fp, &iLen);
272
304
 
273
 
            if (isSystem)
274
 
                phrase->strPhrase = (char* ) fcitx_memory_pool_alloc(pystate->pool, sizeof(char) * (iLen + 1));
275
 
            else
276
 
                phrase->strPhrase = (char *) fcitx_utils_malloc0(sizeof(char) * (iLen + 1));
 
305
            if (isSystem) {
 
306
                phrase->strPhrase = (char*)fcitx_memory_pool_alloc(pystate->pool, sizeof(char) * (iLen + 1));
 
307
            } else {
 
308
                phrase->strPhrase = (char*)fcitx_utils_malloc0(sizeof(char) * (iLen + 1));
 
309
            }
277
310
            fread(phrase->strPhrase, sizeof(char) * iLen, 1, fp);
278
311
            phrase->strPhrase[iLen] = '\0';
279
312
 
280
 
            fread(&iLen, sizeof(unsigned int), 1, fp);
 
313
            fcitx_utils_read_int32(fp, &iLen);
281
314
            phrase->iIndex = iLen;
282
315
            if (iLen > pystate->iCounter)
283
316
                pystate->iCounter = iLen;
285
318
                phrase->iHit = 0;
286
319
                phrase ++;
287
320
            } else {
288
 
                fread(&iLen, sizeof(int), 1, fp);
 
321
                fcitx_utils_read_int32(fp, &iLen);
289
322
                phrase->iHit = iLen;
290
323
 
291
 
                ((PyUsrPhrase*) phrase)->next = ((PyUsrPhrase*) temp)->next;
292
 
                ((PyUsrPhrase*) temp)->next = (PyUsrPhrase*) phrase;
 
324
                ((PyUsrPhrase*)phrase)->next = ((PyUsrPhrase*) temp)->next;
 
325
                ((PyUsrPhrase*)temp)->next = (PyUsrPhrase*) phrase;
293
326
 
294
327
                temp = phrase;
295
328
            }
347
380
{
348
381
    //下面开始读系统词组
349
382
    FILE *fp;
350
 
    int i, j, k, iLen;
351
 
    uint iIndex;
 
383
    int32_t i, j, k, iLen;
 
384
    uint32_t iIndex;
352
385
    PyFreq *pyFreqTemp, *pPyFreq;
353
386
    HZ *HZTemp, *pHZ;
354
387
    PYFA* PYFAList = pystate->PYFAList;
396
429
    fp = FcitxXDGGetFileUserWithPrefix("pinyin", PY_INDEX_FILE, "r", NULL);
397
430
    if (fp) {
398
431
        uint32_t magic = 0;
399
 
        fread(&magic, sizeof(uint32_t), 1, fp);
 
432
        fcitx_utils_read_uint32(fp, &magic);
400
433
        if (magic == PY_INDEX_MAGIC_NUMBER) {
401
 
            fread(&iLen, sizeof(uint), 1, fp);
 
434
            fcitx_utils_read_int32(fp, &iLen);
402
435
            if (iLen > pystate->iCounter)
403
436
                pystate->iCounter = iLen;
404
437
            while (!feof(fp)) {
405
 
                fread(&i, sizeof(int), 1, fp);
406
 
                fread(&j, sizeof(int), 1, fp);
407
 
                fread(&k, sizeof(int), 1, fp);
408
 
                fread(&iIndex, sizeof(uint), 1, fp);
409
 
                fread(&iLen, sizeof(uint), 1, fp);
 
438
                fcitx_utils_read_int32(fp, &i);
 
439
                fcitx_utils_read_int32(fp, &j);
 
440
                fcitx_utils_read_int32(fp, &k);
 
441
                fcitx_utils_read_uint32(fp, &iIndex);
 
442
                fcitx_utils_read_int32(fp, &iLen);
410
443
 
411
444
                if (i < pystate->iPYFACount) {
412
445
                    if (j < PYFAList[i].iBase) {
433
466
    if (fp) {
434
467
        pPyFreq = pystate->pyFreq;
435
468
 
436
 
        fread(&pystate->iPYFreqCount, sizeof(uint), 1, fp);
 
469
        fcitx_utils_read_uint32(fp, &pystate->iPYFreqCount);
437
470
 
438
471
        for (i = 0; i < pystate->iPYFreqCount; i++) {
439
 
            pyFreqTemp = (PyFreq *) fcitx_utils_malloc0(sizeof(PyFreq));
440
 
            pyFreqTemp->next = NULL;
441
 
            pyFreqTemp->bIsSym = false;
 
472
            pyFreqTemp = fcitx_utils_new(PyFreq);
 
473
            // pyFreqTemp->next = NULL;
442
474
 
443
475
            fread(pyFreqTemp->strPY, sizeof(char) * 11, 1, fp);
444
 
            fread(&j, sizeof(int), 1, fp);
445
 
            pyFreqTemp->iCount = j;
 
476
            fcitx_utils_read_uint32(fp, &pyFreqTemp->iCount);
446
477
 
447
 
            pyFreqTemp->HZList = (HZ *) fcitx_utils_malloc0(sizeof(HZ));
448
 
            pyFreqTemp->HZList->next = NULL;
 
478
            pyFreqTemp->HZList = fcitx_utils_new(HZ);
 
479
            // pyFreqTemp->HZList->next = NULL;
449
480
            pHZ = pyFreqTemp->HZList;
450
481
 
451
482
            for (k = 0; k < pyFreqTemp->iCount; k++) {
452
483
                int8_t slen;
453
 
                HZTemp = (HZ *) fcitx_utils_malloc0(sizeof(HZ));
 
484
                HZTemp = fcitx_utils_new(HZ);
454
485
                fread(&slen, sizeof(int8_t), 1, fp);
455
486
                fread(HZTemp->strHZ, sizeof(char) * slen, 1, fp);
456
487
                HZTemp->strHZ[slen] = '\0';
457
 
                fread(&j, sizeof(int), 1, fp);
458
 
                HZTemp->iPYFA = j;
459
 
                fread(&j, sizeof(int), 1, fp);
460
 
                HZTemp->iHit = j;
461
 
                fread(&j, sizeof(int), 1, fp);
462
 
                HZTemp->iIndex = j;
 
488
                fcitx_utils_read_int32(fp, &HZTemp->iPYFA);
 
489
                fcitx_utils_read_uint32(fp, &HZTemp->iHit);
 
490
                fcitx_utils_read_uint32(fp, &HZTemp->iIndex);
463
491
                pHZ->next = HZTemp;
464
492
                pHZ = HZTemp;
465
493
            }
470
498
 
471
499
        fclose(fp);
472
500
    }
473
 
    //下面读取特殊符号表
474
 
    fp = FcitxXDGGetFileWithPrefix("pinyin", PY_SYMBOL_FILE, "r", NULL);
475
 
    if (fp) {
476
 
        char* buf = NULL, *buf1 = NULL;
477
 
        size_t len;
478
 
 
479
 
        while (getline(&buf, &len, fp) != -1) {
480
 
            if (buf1)
481
 
                free(buf1);
482
 
            buf1 = fcitx_utils_trim(buf);
483
 
            char *p = buf1;
484
 
 
485
 
            while (*p && !isspace(*p))
486
 
                p ++;
487
 
            if (*p == '\0' || *p == '#')
488
 
                continue;
489
 
 
490
 
            while (isspace(*p)) {
491
 
                *p = '\0';
492
 
                p ++;
493
 
            }
494
 
 
495
 
            if (strlen(buf1) >= MAX_PY_PHRASE_LENGTH * MAX_PY_LENGTH)
496
 
                continue;
497
 
 
498
 
            if (strlen(p) >= MAX_PY_PHRASE_LENGTH * UTF8_MAX_LENGTH)
499
 
                continue;
500
 
 
501
 
            //首先看看str1是否已经在列表中
502
 
            pyFreqTemp = pystate->pyFreq->next;
503
 
            pPyFreq = pystate->pyFreq;
504
 
            while (pyFreqTemp) {
505
 
                if (!strcmp(pyFreqTemp->strPY, buf1))
506
 
                    break;
507
 
                pPyFreq = pPyFreq->next;
508
 
                pyFreqTemp = pyFreqTemp->next;
509
 
            }
510
 
 
511
 
            if (!pyFreqTemp) {
512
 
                pyFreqTemp = (PyFreq *) fcitx_utils_malloc0(sizeof(PyFreq));
513
 
                strcpy(pyFreqTemp->strPY, buf1);
514
 
                pyFreqTemp->next = NULL;
515
 
                pyFreqTemp->iCount = 0;
516
 
                pyFreqTemp->bIsSym = true;
517
 
                pyFreqTemp->HZList = (HZ *) fcitx_utils_malloc0(sizeof(HZ));
518
 
                pyFreqTemp->HZList->next = NULL;
519
 
                pPyFreq->next = pyFreqTemp;
520
 
                pystate->iPYFreqCount++;
521
 
            } else {
522
 
                if (!pyFreqTemp->bIsSym)        //不能与常用字的编码相同
523
 
                    continue;
524
 
            }
525
 
 
526
 
            HZTemp = (HZ *) fcitx_utils_malloc0(sizeof(HZ));
527
 
            strcpy(HZTemp->strHZ, p);
528
 
            HZTemp->next = NULL;
529
 
            pyFreqTemp->iCount++;
530
 
 
531
 
            pHZ = pyFreqTemp->HZList;
532
 
            while (pHZ->next)
533
 
                pHZ = pHZ->next;
534
 
 
535
 
            pHZ->next = HZTemp;
536
 
        }
537
 
 
538
 
        if (buf)
539
 
            free(buf);
540
 
        if (buf1)
541
 
            free(buf1);
542
 
 
543
 
        fclose(fp);
544
 
    }
545
 
 
546
501
    return true;
547
502
}
548
503
 
561
516
    pystate->findMap.iMode = PARSE_SINGLEHZ;     //只要不是PARSE_ERROR就可以
562
517
}
563
518
 
564
 
int GetBaseIndex(FcitxPinyinState* pystate, int iPYFA, char *strBase)
 
519
int GetBaseIndex(FcitxPinyinState* pystate, int32_t iPYFA, char *strBase)
565
520
{
566
521
    int i;
567
522
 
582
537
    int i = 0;
583
538
    int val;
584
539
    INPUT_RETURN_VALUE retVal;
585
 
    char strTemp[MAX_USER_INPUT + 1];
586
540
 
587
541
    if (sym == 0 && state == 0)
588
542
        sym = -1;
597
551
    /* is not in py special state */
598
552
    if (!pystate->bIsPYAddFreq && !pystate->bIsPYDelFreq && !pystate->bIsPYDelUserPhr) {
599
553
        if ((FcitxHotkeyIsHotKeyLAZ(sym, state)
600
 
                || FcitxHotkeyIsHotKey(sym, state, FCITX_SEPARATOR)
601
 
                || (pystate->bSP && FcitxInputStateGetRawInputBufferSize(input) > 0 && pystate->bSP_UseSemicolon && FcitxHotkeyIsHotKey(sym, state, FCITX_SEMICOLON)))) {
 
554
             || FcitxHotkeyIsHotKey(sym, state, FCITX_SEPARATOR)
 
555
             || (pystate->bSP && FcitxInputStateGetRawInputBufferSize(input) > 0 && pystate->bSP_UseSemicolon && FcitxHotkeyIsHotKey(sym, state, FCITX_SEMICOLON)))) {
602
556
            FcitxInputStateSetIsInRemind(input, false);
603
557
            FcitxInputStateSetShowCursor(input, true);
604
558
 
612
566
 
613
567
            val = i = strlen(pystate->strFindString);
614
568
 
 
569
            if (!pystate->bSP &&
 
570
                pystate->pyconfig.bUseVForQuickPhrase && i == 0 &&
 
571
                FcitxHotkeyIsKey(sym, state, FcitxKey_v, FcitxKeyState_None)) {
 
572
                int key = sym;
 
573
                boolean useDup = false;
 
574
                boolean append = true;
 
575
                if (FcitxQuickPhraseLaunch(pystate->owner, &key,
 
576
                                           &useDup, &append))
 
577
                    return IRV_DISPLAY_MESSAGE;
 
578
            }
 
579
 
615
580
            /* do the insert */
616
581
            for (; i > pystate->iPYInsertPoint; i--)
617
582
                pystate->strFindString[i] = pystate->strFindString[i - 1];
645
610
 
646
611
                retVal = IRV_DISPLAY_CANDWORDS;
647
612
            } else if (pystate->iPYInsertPoint) {
 
613
                char *move_src = (pystate->strFindString
 
614
                                  + pystate->iPYInsertPoint);
648
615
                /* we cannot delete it if cursor is at the first */
649
616
                val = ((pystate->iPYInsertPoint > 1)
650
 
                       && (pystate->strFindString[pystate->iPYInsertPoint - 2] == PY_SEPARATOR)) ? 2 : 1;
651
 
                int len = strlen(pystate->strFindString + pystate->iPYInsertPoint), i = 0;
652
 
                /* 这里使用<=而不是<是因为还有'\0'需要拷贝 */
653
 
                for (i = 0; i <= len; i++)
654
 
                    pystate->strFindString[i + pystate->iPYInsertPoint - val] = pystate->strFindString[i + pystate->iPYInsertPoint];
655
 
                ParsePY(&pystate->pyconfig, pystate->strFindString, &pystate->findMap, PY_PARSE_INPUT_USER, pystate->bSP);
 
617
                       && (move_src[-2] == PY_SEPARATOR)) ? 2 : 1;
 
618
                memmove(move_src - val, move_src, strlen(move_src) + 1);
 
619
                ParsePY(&pystate->pyconfig, pystate->strFindString,
 
620
                        &pystate->findMap, PY_PARSE_INPUT_USER, pystate->bSP);
656
621
                pystate->iPYInsertPoint -= val;
657
622
 
658
 
                if (!strlen(pystate->strFindString)) {
659
 
                    if (!pystate->iPYSelected)
660
 
                        return IRV_CLEAN;
661
 
 
662
 
                    val = strlen(pystate->strFindString);
663
 
                    strcpy(strTemp, pystate->pySelected[pystate->iPYSelected - 1].strPY);
664
 
                    strcat(strTemp, pystate->strFindString);
665
 
                    strcpy(pystate->strFindString, strTemp);
666
 
                    pystate->iPYInsertPoint = strlen(pystate->strFindString) - val;
667
 
                    pystate->iPYSelected--;
668
 
                    ParsePY(&pystate->pyconfig, pystate->strFindString, &pystate->findMap, PY_PARSE_INPUT_USER, pystate->bSP);
669
 
                }
670
 
 
 
623
                if (!strlen(pystate->strFindString))
 
624
                    return IRV_CLEAN;
671
625
                retVal = IRV_DISPLAY_CANDWORDS;
672
626
            } else {
673
 
                if (strlen(pystate->strFindString) == 0)
 
627
                if (!strlen(pystate->strFindString))
674
628
                    return IRV_TO_PROCESS;
675
629
                else
676
630
                    return IRV_DO_NOTHING;
679
633
            if (FcitxInputStateGetRawInputBufferSize(input)) {
680
634
                if (pystate->iPYInsertPoint == strlen(pystate->strFindString))
681
635
                    return IRV_DO_NOTHING;
682
 
                val = (pystate->strFindString[pystate->iPYInsertPoint + 1] == PY_SEPARATOR) ? 2 : 1;
683
 
                int len = strlen(pystate->strFindString + pystate->iPYInsertPoint + val), i = 0;
684
 
                /* 这里使用<=而不是<是因为还有'\0'需要拷贝 */
685
 
                for (i = 0; i <= len; i++)
686
 
                    pystate->strFindString[i + pystate->iPYInsertPoint] = pystate->strFindString[i + pystate->iPYInsertPoint + val];
 
636
                char *move_dst = (pystate->strFindString
 
637
                                  + pystate->iPYInsertPoint);
 
638
                val = (move_dst[1] == PY_SEPARATOR) ? 2 : 1;
 
639
                memmove(move_dst, move_dst + val, strlen(move_dst + val) + 1);
687
640
 
688
 
                ParsePY(&pystate->pyconfig, pystate->strFindString, &pystate->findMap, PY_PARSE_INPUT_USER, pystate->bSP);
 
641
                ParsePY(&pystate->pyconfig, pystate->strFindString,
 
642
                        &pystate->findMap, PY_PARSE_INPUT_USER, pystate->bSP);
689
643
                if (!strlen(pystate->strFindString))
690
644
                    return IRV_CLEAN;
691
645
                retVal = IRV_DISPLAY_CANDWORDS;
730
684
                retVal = IRV_DISPLAY_CANDWORDS;
731
685
            }
732
686
        } else if (FcitxHotkeyIsHotKey(sym, state, FCITX_SPACE)) {
733
 
            if (pystate->findMap.iMode == PARSE_ERROR)
734
 
                return IRV_DO_NOTHING;
735
 
 
736
 
            if (FcitxCandidateWordPageCount(FcitxInputStateGetCandidateList(input)) == 0) {
 
687
            FcitxCandidateWordList *candList;
 
688
            candList = FcitxInputStateGetCandidateList(input);
 
689
            if (FcitxCandidateWordPageCount(candList) == 0) {
737
690
                if (FcitxInputStateGetRawInputBufferSize(input) == 0)
738
691
                    return IRV_TO_PROCESS;
739
692
                else
740
693
                    return IRV_DO_NOTHING;
741
694
            }
742
695
 
743
 
            retVal = FcitxCandidateWordChooseByIndex(FcitxInputStateGetCandidateList(input), 0);
 
696
            retVal = FcitxCandidateWordChooseByIndex(candList, 0);
744
697
        } else if (FcitxHotkeyIsHotKey(sym, state, pystate->pyconfig.hkPYDelUserPhr)) {
745
698
            if (!pystate->bIsPYDelUserPhr) {
 
699
                int i;
 
700
                FcitxCandidateWordList *candList;
746
701
                FcitxCandidateWord* candWord = NULL;
747
 
                for (candWord = FcitxCandidateWordGetCurrentWindow(FcitxInputStateGetCandidateList(input));
748
 
                        candWord != NULL;
749
 
                        candWord = FcitxCandidateWordGetCurrentWindowNext(FcitxInputStateGetCandidateList(input), candWord)) {
 
702
                candList = FcitxInputStateGetCandidateList(input);
 
703
                for (i = 0;
 
704
                     (candWord = FcitxCandidateWordGetByIndex(candList, i));
 
705
                     i++) {
750
706
                    if (candWord->owner == pystate) {
751
707
                        PYCandWord* pycandWord = candWord->priv;
752
708
                        if (pycandWord->iWhich == PY_CAND_USERPHRASE)
761
717
                FcitxInputStateSetIsDoInputOnly(input, true);
762
718
 
763
719
                FcitxInstanceCleanInputWindowUp(pystate->owner);
764
 
                FcitxMessagesAddMessageAtLast(FcitxInputStateGetAuxUp(input), MSG_TIPS, _("Press index to delete user phrase (ESC for cancel)"));
 
720
                FcitxMessagesAddMessageStringsAtLast(
 
721
                    FcitxInputStateGetAuxUp(input), MSG_TIPS,
 
722
                    _("Press index to delete user phrase (ESC for cancel)"));
765
723
                FcitxInputStateSetShowCursor(input, false);
766
724
 
767
725
                return IRV_DISPLAY_MESSAGE;
772
730
                FcitxInputStateSetIsDoInputOnly(input, true);
773
731
 
774
732
                FcitxInstanceCleanInputWindowUp(pystate->owner);
775
 
                FcitxMessagesAddMessageAtLast(FcitxInputStateGetAuxUp(input), MSG_TIPS, _("Press number to make word in frequent list"), pystate->strFindString);
 
733
                FcitxMessagesAddMessageStringsAtLast(
 
734
                    FcitxInputStateGetAuxUp(input), MSG_TIPS,
 
735
                    _("Press number to make word in frequent list"));
776
736
                FcitxInputStateSetShowCursor(input, false);
777
737
 
778
738
                return IRV_DISPLAY_MESSAGE;
780
740
        } else if (FcitxHotkeyIsHotKey(sym, state, pystate->pyconfig.hkPYDelFreq)) {
781
741
            if (!pystate->bIsPYDelFreq) {
782
742
                val = 0;
783
 
                int index = 0;
 
743
                int i;
784
744
                FcitxCandidateWord* candWord = NULL;
785
 
                for (candWord = FcitxCandidateWordGetCurrentWindow(FcitxInputStateGetCandidateList(input));
786
 
                        candWord != NULL;
787
 
                        candWord = FcitxCandidateWordGetCurrentWindowNext(FcitxInputStateGetCandidateList(input), candWord)) {
788
 
                    index ++ ;
 
745
                FcitxCandidateWordList *candList;
 
746
                candList = FcitxInputStateGetCandidateList(input);
 
747
                for (i = 0;
 
748
                     (candWord = FcitxCandidateWordGetByIndex(candList, i));
 
749
                     i++) {
789
750
                    if (candWord->owner == pystate) {
790
751
                        PYCandWord* pycandWord = candWord->priv;
791
752
                        if (pycandWord->iWhich == PY_CAND_FREQ) {
792
 
                            val = index;
 
753
                            val = i + 1;
793
754
                        }
794
755
                    }
795
756
                }
798
759
                    return IRV_DO_NOTHING;
799
760
 
800
761
                FcitxInstanceCleanInputWindowUp(pystate->owner);
801
 
                if (val == 1)
 
762
                if (val == 1) {
802
763
                    FcitxMessagesAddMessageAtLast(FcitxInputStateGetAuxUp(input), MSG_TIPS, _("Press 1 to delete %s in frequent list (ESC for cancel)"), pystate->strFindString);
803
 
                else
 
764
                } else {
804
765
                    FcitxMessagesAddMessageAtLast(FcitxInputStateGetAuxUp(input), MSG_TIPS, _("Press 1-%d to delete %s in frequent list (ESC for cancel)"), val, pystate->strFindString);
 
766
                }
805
767
 
806
768
                pystate->bIsPYDelFreq = true;
807
769
                FcitxInputStateSetIsDoInputOnly(input, true);
814
776
    }
815
777
 
816
778
    if (retVal == IRV_TO_PROCESS) {
817
 
        if (FcitxHotkeyIsHotKeyDigit(sym, state)) {
818
 
            int iKey = sym - FcitxKey_0;
819
 
            if (iKey == 0)
820
 
                iKey = 10;
821
 
 
822
 
            FcitxCandidateWord* candWord = FcitxCandidateWordGetByIndex(FcitxInputStateGetCandidateList(input), iKey - 1);
 
779
        FcitxCandidateWordList *candList;
 
780
        int iKey;
 
781
        candList = FcitxInputStateGetCandidateList(input);
 
782
        iKey = FcitxCandidateWordCheckChooseKey(candList, sym, state);
 
783
        if (iKey >= 0) {
 
784
            FcitxCandidateWord *candWord;
 
785
            candWord = FcitxCandidateWordGetByIndex(candList, iKey);
823
786
            if (!FcitxInputStateGetIsInRemind(input)) {
824
787
                if (!FcitxInputStateGetRawInputBufferSize(input))
825
788
                    return IRV_TO_PROCESS;
851
814
            ParsePY(&pystate->pyconfig, pystate->strFindString, &pystate->findMap, PY_PARSE_INPUT_USER, pystate->bSP);
852
815
            pystate->iPYInsertPoint = 0;
853
816
            retVal = IRV_DISPLAY_CANDWORDS;
854
 
        } else if (FcitxHotkeyIsHotKey(sym, state, FCITX_ESCAPE))
 
817
        } else if (FcitxHotkeyIsHotKey(sym, state, FCITX_ESCAPE)) {
855
818
            return IRV_TO_PROCESS;
856
 
        else {
857
 
            if (pystate->bIsPYAddFreq || pystate->bIsPYDelFreq || pystate->bIsPYDelUserPhr)
858
 
                return IRV_DO_NOTHING;
859
 
 
860
 
            //下面实现以词定字
861
 
            if (FcitxCandidateWordPageCount(FcitxInputStateGetCandidateList(input)) != 0) {
862
 
                if (state == FcitxKeyState_None && (sym == pystate->pyconfig.cPYYCDZ[0] || sym == pystate->pyconfig.cPYYCDZ[1])) {
863
 
                    FcitxCandidateWord* candWord = FcitxCandidateWordGetByIndex(FcitxInputStateGetCandidateList(input), pystate->iYCDZ);
864
 
                    if (candWord->owner == pystate) {
865
 
                        PYCandWord* pycandWord = candWord->priv;
866
 
                        if (pycandWord->iWhich == PY_CAND_USERPHRASE || pycandWord->iWhich == PY_CAND_SYSPHRASE) {
867
 
                            char *pBase, *pPhrase;
868
 
 
869
 
                            pBase = pystate->PYFAList[pycandWord->cand.phrase.iPYFA].pyBase[pycandWord->cand.phrase.iBase].strHZ;
870
 
                            pPhrase = pycandWord->cand.phrase.phrase->strPhrase;
871
 
 
872
 
                            if (sym == pystate->pyconfig.cPYYCDZ[0])
873
 
                                strcpy(FcitxInputStateGetOutputString(input), pBase);
874
 
                            else {
875
 
                                int8_t clen;
876
 
                                clen = fcitx_utf8_char_len(pPhrase);
877
 
                                strncpy(FcitxInputStateGetOutputString(input), pPhrase, clen);
878
 
                                FcitxInputStateGetOutputString(input)[clen] = '\0';
879
 
                            }
880
 
                            FcitxMessagesSetMessageCount(FcitxInputStateGetAuxDown(input), 0);
881
 
                            return IRV_COMMIT_STRING;
882
 
                        }
883
 
                    }
884
 
                } else if (!FcitxInputStateGetIsInRemind(input)) {
885
 
                    val = -1;
886
 
                    switch (sym) {
887
 
                    case FcitxKey_parenright:
888
 
                        val++;
889
 
                    case FcitxKey_parenleft:
890
 
                        val++;
891
 
                    case FcitxKey_asterisk:
892
 
                        val++;
893
 
                    case FcitxKey_ampersand:
894
 
                        val++;
895
 
                    case FcitxKey_asciicircum:
896
 
                        val++;
897
 
                    case FcitxKey_percent:
898
 
                        val++;
899
 
                    case FcitxKey_dollar:
900
 
                        val++;
901
 
                    case FcitxKey_numbersign:
902
 
                        val++;
903
 
                    case FcitxKey_at:
904
 
                        val++;
905
 
                    case FcitxKey_exclam:
906
 
                        val++;
907
 
                    default:
908
 
                        break;
909
 
                    }
910
 
 
911
 
                    if (val != -1 && FcitxCandidateWordGetByIndex(FcitxInputStateGetCandidateList(input), val)) {
912
 
                        pystate->iYCDZ = val;
913
 
                        return IRV_DISPLAY_CANDWORDS;
914
 
                    }
915
 
                }
916
 
            }
 
819
        } else if (pystate->bIsPYAddFreq || pystate->bIsPYDelFreq ||
 
820
                   pystate->bIsPYDelUserPhr) {
 
821
            return IRV_DO_NOTHING;
917
822
        }
918
823
    }
919
824
 
1002
907
    FcitxGlobalConfig* config = FcitxInstanceGetGlobalConfig(pystate->owner);
1003
908
    FcitxMessages* msgPreedit = FcitxInputStateGetPreedit(input);
1004
909
    FcitxMessages* msgClientPreedit = FcitxInputStateGetClientPreedit(input);
1005
 
    struct _FcitxCandidateWordList* candList = FcitxInputStateGetCandidateList(input);
 
910
    FcitxCandidateWordList* candList = FcitxInputStateGetCandidateList(input);
 
911
 
 
912
    FcitxCandidateWordSetPageSize(candList, config->iMaxCandWord);
 
913
    FcitxCandidateWordSetChoose(candList, DIGIT_STR_CHOOSE);
1006
914
 
1007
915
    /* update preedit string */
1008
916
    int i;
1009
917
    FcitxMessagesSetMessageCount(msgPreedit, 0);
1010
918
    FcitxMessagesSetMessageCount(msgClientPreedit, 0);
1011
919
    if (pystate->iPYSelected) {
1012
 
        FcitxMessagesAddMessageAtLast(msgPreedit, MSG_OTHER, "");
1013
 
        FcitxMessagesAddMessageAtLast(msgClientPreedit, MSG_OTHER, "");
 
920
        FcitxMessagesAddMessageStringsAtLast(msgPreedit, MSG_OTHER, "");
 
921
        FcitxMessagesAddMessageStringsAtLast(msgClientPreedit, MSG_OTHER, "");
1014
922
        for (i = 0; i < pystate->iPYSelected; i++) {
1015
923
            FcitxMessagesMessageConcat(msgPreedit, FcitxMessagesGetMessageCount(msgPreedit) - 1, pystate->pySelected[i].strHZ);
1016
924
            FcitxMessagesMessageConcat(msgClientPreedit, FcitxMessagesGetMessageCount(msgClientPreedit) - 1, pystate->pySelected[i].strHZ);
1018
926
    }
1019
927
 
1020
928
    for (i = 0; i < pystate->findMap.iHZCount; i++) {
1021
 
        FcitxMessagesAddMessageAtLast(msgPreedit, MSG_CODE, "%s", pystate->findMap.strPYParsed[i]);
 
929
        FcitxMessagesAddMessageStringsAtLast(msgPreedit, MSG_CODE,
 
930
                                             pystate->findMap.strPYParsed[i]);
1022
931
        if (i < pystate->findMap.iHZCount - 1)
1023
932
            FcitxMessagesMessageConcat(msgPreedit, FcitxMessagesGetMessageCount(msgPreedit) - 1, " ");
1024
933
    }
1025
934
 
1026
935
    if (pystate->findMap.iMode == PARSE_ERROR) {
1027
936
        for (i = 0; i < pystate->findMap.iHZCount; i++) {
1028
 
            FcitxMessagesAddMessageAtLast(msgClientPreedit, MSG_CODE, "%s", pystate->findMap.strPYParsed[i]);
1029
 
            if (i < pystate->findMap.iHZCount - 1)
1030
 
                FcitxMessagesMessageConcat(msgClientPreedit, FcitxMessagesGetMessageCount(msgClientPreedit) - 1, " ");
 
937
            FcitxMessagesAddMessageStringsAtLast(
 
938
                msgClientPreedit, MSG_CODE, pystate->findMap.strPYParsed[i]);
1031
939
        }
 
940
        char* errorAuto = FcitxUIMessagesToCString(msgClientPreedit);
1032
941
        FcitxInstanceCleanInputWindowDown(pystate->owner);
1033
 
        return IRV_DISPLAY_MESSAGE;
 
942
 
 
943
        FcitxCandidateWord candWord;
 
944
        candWord.owner = pystate;
 
945
        candWord.callback = PYGetCandWord;
 
946
        candWord.priv = NULL;
 
947
        candWord.strWord = strdup(errorAuto);
 
948
        candWord.strExtra = NULL;
 
949
        candWord.wordType = MSG_OTHER;
 
950
        FcitxCandidateWordAppend(candList, &candWord);
 
951
        return IRV_DISPLAY_CANDWORDS;
1034
952
    }
1035
953
 
1036
954
    if (FcitxInputStateGetIsInRemind(input))
1037
955
        return PYGetRemindCandWords(pystate);
1038
956
 
1039
 
    FcitxCandidateWordSetPageSize(candList, config->iMaxCandWord);
1040
 
    FcitxCandidateWordSetChoose(candList, DIGIT_STR_CHOOSE);
1041
 
 
1042
 
    pystate->iYCDZ = 0;
1043
 
 
1044
957
    //判断是不是要输入常用字或符号
1045
958
    PyFreq* pCurFreq = pystate->pyFreq->next;
1046
959
    for (iVal = 0; iVal < pystate->iPYFreqCount; iVal++) {
1049
962
        pCurFreq = pCurFreq->next;
1050
963
    }
1051
964
 
1052
 
    /* if it is symbol mode, all word will be take care */
1053
 
    if (!(pCurFreq && pCurFreq->bIsSym)) {
1054
 
 
1055
 
        if (pystate->pyconfig.bPYCreateAuto)
1056
 
            PYCreateAuto(pystate);
1057
 
 
1058
 
        if (pystate->strPYAuto[0]) {
1059
 
            FcitxCandidateWord candWord;
1060
 
            PYCandWord* pycandword = fcitx_utils_malloc0(sizeof(PYCandWord));
1061
 
            pycandword->iWhich = PY_CAND_AUTO;
1062
 
            candWord.owner = pystate;
1063
 
            candWord.callback = PYGetCandWord;
1064
 
            candWord.priv = pycandword;
1065
 
            candWord.strWord = strdup(pystate->strPYAuto);
1066
 
            candWord.strExtra = NULL;
1067
 
            candWord.wordType = MSG_OTHER;
1068
 
            FcitxCandidateWordAppend(candList, &candWord);
1069
 
        }
1070
 
 
1071
 
        PYGetPhraseCandWords(pystate);
1072
 
        if (pCurFreq)
1073
 
            PYGetFreqCandWords(pystate, pCurFreq);
1074
 
        PYGetBaseCandWords(pystate, pCurFreq);
1075
 
    } else {
1076
 
        PYGetSymCandWords(pystate, pCurFreq);
 
965
    if (pystate->pyconfig.bPYCreateAuto)
 
966
        PYCreateAuto(pystate);
 
967
 
 
968
    if (pystate->strPYAuto[0]) {
 
969
        FcitxCandidateWord candWord;
 
970
        PYCandWord* pycandword = fcitx_utils_new(PYCandWord);
 
971
        pycandword->iWhich = PY_CAND_AUTO;
 
972
        candWord.owner = pystate;
 
973
        candWord.callback = PYGetCandWord;
 
974
        candWord.priv = pycandword;
 
975
        candWord.strWord = strdup(pystate->strPYAuto);
 
976
        candWord.strExtra = NULL;
 
977
        candWord.wordType = MSG_OTHER;
 
978
        FcitxCandidateWordAppend(candList, &candWord);
1077
979
    }
1078
980
 
 
981
    PYGetPhraseCandWords(pystate);
 
982
    if (pCurFreq)
 
983
        PYGetFreqCandWords(pystate, pCurFreq);
 
984
    PYGetBaseCandWords(pystate, pCurFreq);
 
985
 
1079
986
    if (FcitxCandidateWordPageCount(candList) != 0) {
1080
 
        FcitxCandidateWord* candWord = FcitxCandidateWordGetCurrentWindow(candList);
1081
 
        FcitxMessagesAddMessageAtLast(msgClientPreedit, MSG_INPUT, "%s", candWord->strWord);
 
987
        FcitxCandidateWord *candWord = FcitxCandidateWordGetCurrentWindow(candList);
 
988
        FcitxMessagesAddMessageStringsAtLast(msgClientPreedit, MSG_INPUT,
 
989
                                             candWord->strWord);
1082
990
    }
1083
991
 
1084
992
    return IRV_DISPLAY_CANDWORDS;
1247
1155
INPUT_RETURN_VALUE PYGetCandWord(void* arg, FcitxCandidateWord* candWord)
1248
1156
{
1249
1157
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
 
1158
    FcitxInputState* input = FcitxInstanceGetInputState(pystate->owner);
 
1159
 
 
1160
    if (candWord->priv == NULL) {
 
1161
        strcpy(FcitxInputStateGetOutputString(input), candWord->strWord);
 
1162
        return IRV_COMMIT_STRING;
 
1163
    }
 
1164
 
1250
1165
    char *pBase = NULL, *pPhrase = NULL;
1251
1166
    char *pBaseMap = NULL, *pPhraseMap = NULL;
1252
1167
    uint *pIndex = NULL;
1254
1169
    int i;
1255
1170
    char strHZString[MAX_WORDS_USER_INPUT * UTF8_MAX_LENGTH + 1];
1256
1171
    int iLen;
1257
 
    FcitxInputState* input = FcitxInstanceGetInputState(pystate->owner);
1258
1172
    PYFA* PYFAList = pystate->PYFAList;
1259
1173
    FcitxInstance* instance = pystate->owner;
1260
1174
    PYCandWord* pycandWord = candWord->priv;
1273
1187
        PYFAList[pycandWord->cand.base.iPYFA].pyBase[pycandWord->cand.base.iBase].iHit++;
1274
1188
        pystate->iOrderCount++;
1275
1189
        break;
 
1190
    case PY_CAND_USERPHRASE:   //是用户词组
 
1191
        pystate->iNewPYPhraseCount++; // fall through
1276
1192
    case PY_CAND_SYSPHRASE:    //是系统词组
1277
 
    case PY_CAND_USERPHRASE:   //是用户词组
1278
1193
        pBase = PYFAList[pycandWord->cand.phrase.iPYFA].pyBase[pycandWord->cand.phrase.iBase].strHZ;
1279
1194
        pBaseMap = PYFAList[pycandWord->cand.phrase.iPYFA].strMap;
1280
1195
        pPhrase = pycandWord->cand.phrase.phrase->strPhrase;
1290
1205
        pIndex = &(pycandWord->cand.freq.hz->iIndex);
1291
1206
        pystate->iNewFreqCount++;
1292
1207
        break;
1293
 
    case PY_CAND_SYMBOL:       //是特殊符号
1294
 
        pBase = pycandWord->cand.freq.hz->strHZ;
1295
 
        bAddNewPhrase = false;
1296
 
        break;
1297
1208
    case PY_CAND_REMIND: {
1298
1209
        strcpy(pystate->strPYRemindSource, pycandWord->cand.remind.phrase->strPhrase + pycandWord->cand.remind.iLength);
1299
1210
        strcpy(pystate->strPYRemindMap, pycandWord->cand.remind.phrase->strMap + pycandWord->cand.remind.iLength);
1321
1232
    if (pPhrase)
1322
1233
        strcat(strHZString, pPhrase);
1323
1234
    iLen = fcitx_utf8_strlen(strHZString);
1324
 
    if (iLen == pystate->findMap.iHZCount || pycandWord->iWhich == PY_CAND_SYMBOL) {
 
1235
    if (iLen == pystate->findMap.iHZCount) {
1325
1236
        pystate->strPYAuto[0] = '\0';
1326
1237
        for (iLen = 0; iLen < pystate->iPYSelected; iLen++)
1327
1238
            strcat(pystate->strPYAuto, pystate->pySelected[iLen].strHZ);
1385
1296
        return;
1386
1297
 
1387
1298
    UT_array candtemp;
1388
 
    utarray_init(&candtemp, &pycand_icd);
 
1299
    utarray_init(&candtemp, fcitx_ptr_icd);
1389
1300
 
1390
1301
    str[0] = pystate->findMap.strMap[0][0];
1391
1302
    str[1] = pystate->findMap.strMap[0][1];
1401
1312
                        candPos.iPhrase < PYFAList[candPos.iPYFA].pyBase[candPos.iBase].iUserPhrase; candPos.iPhrase++) {
1402
1313
                    val = CmpMap(pyconfig, phrase->strMap, strMap, &iMatchedLength, pystate->bSP);
1403
1314
                    if (!val || (val && (strlen(phrase->strMap) == iMatchedLength))) {
1404
 
                        PYCandWord* pycandWord = fcitx_utils_malloc0(sizeof(PYCandWord));
 
1315
                        PYCandWord *pycandWord = fcitx_utils_new(PYCandWord);
1405
1316
                        PYAddPhraseCandWord(pystate, candPos, phrase, false, pycandWord);
1406
1317
                        utarray_push_back(&candtemp, &pycandWord);
1407
1318
                    }
1424
1335
                              pystate->bSP);
1425
1336
                    if (!val ||
1426
1337
                            (val && (strlen(PYFAList[candPos.iPYFA].pyBase[candPos.iBase].phrase[candPos.iPhrase].strMap) == iMatchedLength))) {
1427
 
                        PYCandWord* pycandWord = fcitx_utils_malloc0(sizeof(PYCandWord));
 
1338
                        PYCandWord* pycandWord = fcitx_utils_new(PYCandWord);
1428
1339
                        PYAddPhraseCandWord(pystate, candPos, &(PYFAList[candPos.iPYFA].pyBase[candPos.iBase].phrase[candPos.iPhrase]), true, pycandWord);
1429
1340
                        utarray_push_back(&candtemp, &pycandWord);
1430
1341
                    }
1456
1367
            candWord.wordType = MSG_OTHER;
1457
1368
        const char* pBase = PYFAList[(*pcand)->cand.phrase.iPYFA].pyBase[(*pcand)->cand.phrase.iBase].strHZ;
1458
1369
        const char* pPhrase = (*pcand)->cand.phrase.phrase->strPhrase;
1459
 
        asprintf(&candWord.strWord, "%s%s", pBase, pPhrase);
1460
 
        FcitxCandidateWordAppend(FcitxInputStateGetCandidateList(input), &candWord);
 
1370
        fcitx_utils_alloc_cat_str(candWord.strWord, pBase, pPhrase);
 
1371
        FcitxCandidateWordAppend(FcitxInputStateGetCandidateList(input),
 
1372
                                 &candWord);
1461
1373
    }
1462
1374
 
1463
1375
    utarray_done(&candtemp);
1487
1399
    return true;
1488
1400
}
1489
1401
 
1490
 
//********************************************
1491
 
void PYGetSymCandWords(FcitxPinyinState* pystate, PyFreq* pCurFreq)
1492
 
{
1493
 
    int i;
1494
 
    HZ *hz;
1495
 
    FcitxInputState* input = FcitxInstanceGetInputState(pystate->owner);
1496
 
 
1497
 
    if (pCurFreq && pCurFreq->bIsSym) {
1498
 
        hz = pCurFreq->HZList->next;
1499
 
        for (i = 0; i < pCurFreq->iCount; i++) {
1500
 
            PYCandWord* pycandWord = fcitx_utils_malloc0(sizeof(PYCandWord));
1501
 
            PYAddSymCandWord(hz, pycandWord);
1502
 
            FcitxCandidateWord candWord;
1503
 
            candWord.callback = PYGetCandWord;
1504
 
            candWord.owner = pystate;
1505
 
            candWord.priv = pycandWord;
1506
 
            candWord.strExtra = NULL;
1507
 
            candWord.strWord = strdup(hz->strHZ);
1508
 
            candWord.wordType = MSG_OTHER;
1509
 
            FcitxCandidateWordAppend(FcitxInputStateGetCandidateList(input), &candWord);
1510
 
            hz = hz->next;
1511
 
        }
1512
 
    }
1513
 
}
1514
 
 
1515
 
/*
1516
 
 * 将一个符号加入到候选列表的合适位置中
1517
 
 * 符号不需进行频率调整
1518
 
 */
1519
 
void PYAddSymCandWord(HZ * hz, PYCandWord* pycandWord)
1520
 
{
1521
 
    pycandWord->iWhich = PY_CAND_SYMBOL;
1522
 
    pycandWord->cand.sym.hz = hz;
1523
 
}
1524
 
 
1525
1402
//*****************************************************
1526
1403
 
1527
1404
void PYGetBaseCandWords(FcitxPinyinState* pystate, PyFreq* pCurFreq)
1534
1411
    FcitxPinyinConfig* pyconfig = &pystate->pyconfig;
1535
1412
    FcitxInputState* input = FcitxInstanceGetInputState(pystate->owner);
1536
1413
    UT_array candtemp;
1537
 
    utarray_init(&candtemp, &pycand_icd);
 
1414
    utarray_init(&candtemp, fcitx_ptr_icd);
1538
1415
 
1539
1416
    str[0] = pystate->findMap.strMap[0][0];
1540
1417
    str[1] = pystate->findMap.strMap[0][1];
1543
1420
        if (!Cmp2Map(pyconfig, PYFAList[candPos.iPYFA].strMap, str, pystate->bSP)) {
1544
1421
            for (candPos.iBase = 0; candPos.iBase < PYFAList[candPos.iPYFA].iBase; candPos.iBase++) {
1545
1422
                if (!PYIsInFreq(pCurFreq, PYFAList[candPos.iPYFA].pyBase[candPos.iBase].strHZ)) {
1546
 
                    PYCandWord* pycandWord = fcitx_utils_malloc0(sizeof(PYCandWord));
 
1423
                    PYCandWord *pycandWord = fcitx_utils_new(PYCandWord);
1547
1424
                    PYAddBaseCandWord(candPos, pycandWord);
1548
1425
                    utarray_push_back(&candtemp, &pycandWord);
1549
1426
                }
1592
1469
    HZ *hz;
1593
1470
    UT_array candtemp;
1594
1471
    FcitxInputState* input = FcitxInstanceGetInputState(pystate->owner);
1595
 
    utarray_init(&candtemp, &pycand_icd);
 
1472
    utarray_init(&candtemp, fcitx_ptr_icd);
1596
1473
 
1597
 
    if (pCurFreq && !pCurFreq->bIsSym) {
 
1474
    if (pCurFreq) {
1598
1475
        hz = pCurFreq->HZList->next;
1599
1476
        for (i = 0; i < pCurFreq->iCount; i++) {
1600
 
            PYCandWord* pycandWord = fcitx_utils_malloc0(sizeof(PYCandWord));
 
1477
            PYCandWord *pycandWord = fcitx_utils_new(PYCandWord);
1601
1478
            PYAddFreqCandWord(pCurFreq, hz, pCurFreq->strPY, pycandWord);
1602
1479
            utarray_push_back(&candtemp, &pycandWord);
1603
1480
            hz = hz->next;
1644
1521
 * 将一个词组保存到用户词组库中
1645
1522
 * 返回true表示是新词组
1646
1523
 */
1647
 
boolean PYAddUserPhrase(FcitxPinyinState* pystate, char *phrase, char *map, boolean incHit)
 
1524
boolean PYAddUserPhrase(FcitxPinyinState* pystate, const char *phrase, const char *map, boolean incHit)
1648
1525
{
1649
1526
    PyUsrPhrase *userPhrase, *newPhrase, *temp;
1650
1527
    char str[UTF8_MAX_LENGTH + 1];
1691
1568
            return false;
1692
1569
        }
1693
1570
    //下面将词组添加到列表中
1694
 
    newPhrase = (PyUsrPhrase *) fcitx_utils_malloc0(sizeof(PyUsrPhrase));
1695
 
    newPhrase->phrase.strMap = (char *) fcitx_utils_malloc0(sizeof(char) * strlen(map + 2) + 1);
1696
 
    newPhrase->phrase.strPhrase = (char *) fcitx_utils_malloc0(sizeof(char) * strlen(phrase + clen) + 1);
 
1571
    newPhrase = fcitx_utils_new(PyUsrPhrase);
 
1572
    newPhrase->phrase.strMap = (char*)fcitx_utils_malloc0(sizeof(char) * strlen(map + 2) + 1);
 
1573
    newPhrase->phrase.strPhrase = (char*)fcitx_utils_malloc0(sizeof(char) * strlen(phrase + clen) + 1);
1697
1574
    strcpy(newPhrase->phrase.strMap, map + 2);
1698
1575
    strcpy(newPhrase->phrase.strPhrase, phrase + clen);
1699
1576
    newPhrase->phrase.iIndex = ++pystate->iCounter;
1718
1595
    return true;
1719
1596
}
1720
1597
 
1721
 
void PYDelUserPhrase(FcitxPinyinState* pystate, int iPYFA, int iBase, PyUsrPhrase * phrase)
 
1598
void PYDelUserPhrase(FcitxPinyinState* pystate, int32_t iPYFA, int iBase, PyUsrPhrase * phrase)
1722
1599
{
1723
1600
    PyUsrPhrase *temp;
1724
1601
    PYFA* PYFAList = pystate->PYFAList;
1759
1636
 */
1760
1637
void SavePYUserPhrase(FcitxPinyinState* pystate)
1761
1638
{
1762
 
    int i, j, k;
1763
 
    int iTemp;
 
1639
    int j, k;
 
1640
    int32_t i, iTemp;
1764
1641
    char *tempfile, *pstr;
1765
1642
    FILE *fp;
1766
1643
    PyPhrase *phrase;
1786
1663
            iTemp = PYFAList[i].pyBase[j].iUserPhrase;
1787
1664
            if (iTemp) {
1788
1665
                char clen;
1789
 
                fwrite(&i, sizeof(int), 1, fp);
 
1666
                fcitx_utils_write_int32(fp, i);
1790
1667
                clen = strlen(PYFAList[i].pyBase[j].strHZ);
1791
1668
                fwrite(&clen, sizeof(char), 1, fp);
1792
1669
                fwrite(PYFAList[i].pyBase[j].strHZ, sizeof(char) * clen, 1, fp);
1793
 
                fwrite(&iTemp, sizeof(int), 1, fp);
 
1670
                fcitx_utils_write_int32(fp, iTemp);
1794
1671
                phrase = USER_PHRASE_NEXT(PYFAList[i].pyBase[j].userPhrase);
1795
1672
                for (k = 0; k < PYFAList[i].pyBase[j].iUserPhrase; k++) {
1796
1673
                    iTemp = strlen(phrase->strMap);
1797
 
                    fwrite(&iTemp, sizeof(int), 1, fp);
 
1674
                    fcitx_utils_write_int32(fp, iTemp);
1798
1675
                    fwrite(phrase->strMap, sizeof(char) * iTemp, 1, fp);
1799
1676
 
1800
1677
                    iTemp = strlen(phrase->strPhrase);
1801
 
                    fwrite(&iTemp, sizeof(int), 1, fp);
 
1678
                    fcitx_utils_write_int32(fp, iTemp);
1802
1679
                    fwrite(phrase->strPhrase, sizeof(char) * iTemp, 1, fp);
1803
1680
 
1804
 
                    iTemp = phrase->iIndex;
1805
 
                    fwrite(&iTemp, sizeof(int), 1, fp);
1806
 
                    iTemp = phrase->iHit;
1807
 
                    fwrite(&iTemp, sizeof(int), 1, fp);
 
1681
                    fcitx_utils_write_uint32(fp, phrase->iIndex);
 
1682
                    fcitx_utils_write_uint32(fp, phrase->iHit);
1808
1683
                    phrase = USER_PHRASE_NEXT(phrase);
1809
1684
                }
1810
1685
            }
1823
1698
 
1824
1699
void SavePYFreq(FcitxPinyinState *pystate)
1825
1700
{
1826
 
    int i, j, k;
 
1701
    int32_t i;
 
1702
    int k;
1827
1703
    char *pstr;
1828
1704
    char *tempfile;
1829
1705
    FILE *fp;
1848
1724
    i = 0;
1849
1725
    pPyFreq = pystate->pyFreq->next;
1850
1726
    while (pPyFreq) {
1851
 
        if (!pPyFreq->bIsSym)
1852
 
            i++;
 
1727
        i++;
1853
1728
        pPyFreq = pPyFreq->next;
1854
1729
    }
1855
 
    fwrite(&i, sizeof(uint), 1, fp);
 
1730
    fcitx_utils_write_int32(fp, i);
1856
1731
    pPyFreq = pystate->pyFreq->next;
1857
1732
    while (pPyFreq) {
1858
 
        if (!pPyFreq->bIsSym) {
1859
 
            fwrite(pPyFreq->strPY, sizeof(char) * 11, 1, fp);
1860
 
            j = pPyFreq->iCount;
1861
 
            fwrite(&j, sizeof(int), 1, fp);
1862
 
            hz = pPyFreq->HZList->next;
1863
 
            for (k = 0; k < pPyFreq->iCount; k++) {
1864
 
 
1865
 
                char slen = strlen(hz->strHZ);
1866
 
                fwrite(&slen, sizeof(char), 1, fp);
1867
 
                fwrite(hz->strHZ, sizeof(char) * slen, 1, fp);
1868
 
 
1869
 
                j = hz->iPYFA;
1870
 
                fwrite(&j, sizeof(int), 1, fp);
1871
 
 
1872
 
                j = hz->iHit;
1873
 
                fwrite(&j, sizeof(int), 1, fp);
1874
 
 
1875
 
                j = hz->iIndex;
1876
 
                fwrite(&j, sizeof(int), 1, fp);
1877
 
 
1878
 
                hz = hz->next;
1879
 
            }
 
1733
        fwrite(pPyFreq->strPY, sizeof(char) * 11, 1, fp);
 
1734
        fcitx_utils_write_int32(fp, pPyFreq->iCount);
 
1735
        hz = pPyFreq->HZList->next;
 
1736
        for (k = 0; k < pPyFreq->iCount; k++) {
 
1737
            char slen = strlen(hz->strHZ);
 
1738
            fwrite(&slen, sizeof(char), 1, fp);
 
1739
            fwrite(hz->strHZ, sizeof(char) * slen, 1, fp);
 
1740
            fcitx_utils_write_int32(fp, hz->iPYFA);
 
1741
            fcitx_utils_write_uint32(fp, hz->iHit);
 
1742
            fcitx_utils_write_int32(fp, hz->iIndex);
 
1743
 
 
1744
            hz = hz->next;
1880
1745
        }
1881
1746
        pPyFreq = pPyFreq->next;
1882
1747
    }
1897
1762
 */
1898
1763
void SavePYIndex(FcitxPinyinState *pystate)
1899
1764
{
1900
 
    int i, j, k, l;
 
1765
    int32_t i, j, k;
1901
1766
    char *pstr;
1902
1767
    char *tempfile;
1903
1768
    FILE *fp;
1918
1783
        return;
1919
1784
    }
1920
1785
 
1921
 
    uint32_t magic = PY_INDEX_MAGIC_NUMBER;
1922
 
    fwrite(&magic, sizeof(uint32_t), 1, fp);
 
1786
    fcitx_utils_write_uint32(fp, PY_INDEX_MAGIC_NUMBER);
1923
1787
 
1924
 
    //保存计数器
1925
 
    fwrite(&pystate->iCounter, sizeof(uint), 1, fp);
 
1788
    //Save Counter
 
1789
    fcitx_utils_write_uint32(fp, pystate->iCounter);
1926
1790
    //先保存索引不为0的单字
1927
1791
    k = -1;
1928
1792
    for (i = 0; i < pystate->iPYFACount; i++) {
1929
1793
        for (j = 0; j < PYFAList[i].iBase; j++) {
1930
1794
            if (PYFAList[i].pyBase[j].iIndex > pystate->iOrigCounter) {
1931
 
                fwrite(&i, sizeof(int), 1, fp);
1932
 
                fwrite(&j, sizeof(int), 1, fp);
1933
 
                fwrite(&k, sizeof(int), 1, fp);
1934
 
                l = PYFAList[i].pyBase[j].iIndex;
1935
 
                fwrite(&l, sizeof(uint), 1, fp);
1936
 
                l = PYFAList[i].pyBase[j].iHit;
1937
 
                fwrite(&l, sizeof(uint), 1, fp);
 
1795
                fcitx_utils_write_int32(fp, i);
 
1796
                fcitx_utils_write_int32(fp, j);
 
1797
                fcitx_utils_write_int32(fp, k);
 
1798
                fcitx_utils_write_uint32(fp, PYFAList[i].pyBase[j].iIndex);
 
1799
                fcitx_utils_write_uint32(fp, PYFAList[i].pyBase[j].iHit);
1938
1800
            }
1939
1801
        }
1940
1802
    }
1943
1805
    for (i = 0; i < pystate->iPYFACount; i++) {
1944
1806
        for (j = 0; j < PYFAList[i].iBase; j++) {
1945
1807
            for (k = 0; k < PYFAList[i].pyBase[j].iPhrase; k++) {
1946
 
                if (PYFAList[i].pyBase[j].phrase[k].iIndex > pystate->iOrigCounter) {
1947
 
                    fwrite(&i, sizeof(int), 1, fp);
1948
 
                    fwrite(&j, sizeof(int), 1, fp);
1949
 
                    fwrite(&k, sizeof(int), 1, fp);
1950
 
                    l = PYFAList[i].pyBase[j].phrase[k].iIndex;
1951
 
                    fwrite(&l, sizeof(uint), 1, fp);
1952
 
                    l = PYFAList[i].pyBase[j].phrase[k].iHit;
1953
 
                    fwrite(&l, sizeof(uint), 1, fp);
 
1808
                if (PYFAList[i].pyBase[j].phrase[k].iIndex
 
1809
                    > pystate->iOrigCounter) {
 
1810
                    fcitx_utils_write_int32(fp, i);
 
1811
                    fcitx_utils_write_int32(fp, j);
 
1812
                    fcitx_utils_write_int32(fp, k);
 
1813
                    fcitx_utils_write_uint32(
 
1814
                        fp, PYFAList[i].pyBase[j].phrase[k].iIndex);
 
1815
                    fcitx_utils_write_uint32(
 
1816
                        fp, PYFAList[i].pyBase[j].phrase[k].iHit);
1954
1817
                }
1955
1818
            }
1956
1819
        }
2010
1873
        return;
2011
1874
    //需要添加该字,此时该字必然是系统单字
2012
1875
    if (!pCurFreq) {
2013
 
        freq = (PyFreq *) fcitx_utils_malloc0(sizeof(PyFreq));
2014
 
        freq->HZList = (HZ *) fcitx_utils_malloc0(sizeof(HZ));
 
1876
        freq = fcitx_utils_new(PyFreq);
 
1877
        freq->HZList = fcitx_utils_new(HZ);
2015
1878
        freq->HZList->next = NULL;
2016
1879
        strcpy(freq->strPY, pystate->strFindString);
2017
1880
        freq->next = NULL;
2018
1881
        freq->iCount = 0;
2019
 
        freq->bIsSym = false;
2020
1882
        pCurFreq = pystate->pyFreq;
2021
1883
        for (i = 0; i < pystate->iPYFreqCount; i++)
2022
1884
            pCurFreq = pCurFreq->next;
2025
1887
        pCurFreq = freq;
2026
1888
    }
2027
1889
 
2028
 
    HZTemp = (HZ *) fcitx_utils_malloc0(sizeof(HZ));
 
1890
    HZTemp = fcitx_utils_new(HZ);
2029
1891
    strcpy(HZTemp->strHZ, PYFAList[pycandWord->cand.base.iPYFA].pyBase[pycandWord->cand.base.iBase].strHZ);
2030
1892
    HZTemp->iPYFA = pycandWord->cand.base.iPYFA;
2031
1893
    HZTemp->iHit = 0;
2075
1937
    HZ *hz;
2076
1938
    int i;
2077
1939
 
2078
 
    if (!pCurFreq || pCurFreq->bIsSym)
 
1940
    if (!pCurFreq)
2079
1941
        return false;
2080
1942
    hz = pCurFreq->HZList->next;
2081
1943
    for (i = 0; i < pCurFreq->iCount; i++) {
2121
1983
        return IRV_TO_PROCESS;
2122
1984
 
2123
1985
    UT_array candtemp;
2124
 
    utarray_init(&candtemp, &pycand_icd);
 
1986
    utarray_init(&candtemp, fcitx_ptr_icd);
2125
1987
 
2126
1988
    for (i = 0; i < pyBaseForRemind->iPhrase; i++) {
2127
1989
 
2130
1992
 
2131
1993
        if (fcitx_utf8_strlen(pystate->strPYRemindSource) == 1) {
2132
1994
            if (fcitx_utf8_strlen(pyBaseForRemind->phrase[i].strPhrase) == 1) {
2133
 
                PYCandWord* pycandWord = fcitx_utils_malloc0(sizeof(PYCandWord));
 
1995
                PYCandWord *pycandWord = fcitx_utils_new(PYCandWord);
2134
1996
                PYAddRemindCandWord(pystate, &pyBaseForRemind->phrase[i], pycandWord);
2135
1997
                utarray_push_back(&candtemp, &pycandWord);
2136
1998
            }
2139
2001
                    (pystate->strPYRemindSource + fcitx_utf8_char_len(pystate->strPYRemindSource),
2140
2002
                     pyBaseForRemind->phrase[i].strPhrase, strlen(pystate->strPYRemindSource + fcitx_utf8_char_len(pystate->strPYRemindSource)))
2141
2003
               ) {
2142
 
                PYCandWord* pycandWord = fcitx_utils_malloc0(sizeof(PYCandWord));
 
2004
                PYCandWord *pycandWord = fcitx_utils_new(PYCandWord);
2143
2005
                PYAddRemindCandWord(pystate, &pyBaseForRemind->phrase[i], pycandWord);
2144
2006
                utarray_push_back(&candtemp, &pycandWord);
2145
2007
            }
2153
2015
 
2154
2016
        if (fcitx_utf8_strlen(pystate->strPYRemindSource) == 1) {
2155
2017
            if (fcitx_utf8_strlen(phrase->strPhrase) == 1) {
2156
 
                PYCandWord* pycandWord = fcitx_utils_malloc0(sizeof(PYCandWord));
 
2018
                PYCandWord *pycandWord = fcitx_utils_new(PYCandWord);
2157
2019
                PYAddRemindCandWord(pystate, phrase, pycandWord);
2158
2020
                utarray_push_back(&candtemp, &pycandWord);
2159
2021
            }
2161
2023
            if (!strncmp
2162
2024
                    (pystate->strPYRemindSource + fcitx_utf8_char_len(pystate->strPYRemindSource),
2163
2025
                     phrase->strPhrase, strlen(pystate->strPYRemindSource + fcitx_utf8_char_len(pystate->strPYRemindSource)))) {
2164
 
                PYCandWord* pycandWord = fcitx_utils_malloc0(sizeof(PYCandWord));
 
2026
                PYCandWord *pycandWord = fcitx_utils_new(PYCandWord);
2165
2027
                PYAddRemindCandWord(pystate, phrase, pycandWord);
2166
2028
                utarray_push_back(&candtemp, &pycandWord);
2167
2029
            }
2170
2032
        phrase = USER_PHRASE_NEXT(phrase);
2171
2033
    }
2172
2034
 
2173
 
    FcitxMessagesSetMessageCount(FcitxInputStateGetAuxUp(input), 0);
2174
 
    FcitxMessagesAddMessageAtLast(FcitxInputStateGetAuxUp(input), MSG_TIPS, _("Remind: "));
2175
 
    FcitxMessagesAddMessageAtLast(FcitxInputStateGetAuxUp(input), MSG_INPUT, "%s", pystate->strPYRemindSource);
 
2035
    FcitxMessages *msg_up = FcitxInputStateGetAuxUp(input);
 
2036
    FcitxMessagesSetMessageCount(msg_up, 0);
 
2037
    FcitxMessagesAddMessageStringsAtLast(msg_up, MSG_TIPS, _("Remind: "));
 
2038
    FcitxMessagesAddMessageStringsAtLast(msg_up, MSG_INPUT,
 
2039
                                         pystate->strPYRemindSource);
2176
2040
 
2177
2041
    PYCandWord** pcand = NULL;
2178
2042
    for (pcand = (PYCandWord**) utarray_front(&candtemp);
2204
2068
    pyRemindCandWords->iLength = strlen(pystate->strPYRemindSource) - fcitx_utf8_char_len(pystate->strPYRemindSource);
2205
2069
}
2206
2070
 
2207
 
void PYGetPYByHZ(FcitxPinyinState*pystate, char *strHZ, char *strPY)
 
2071
void PYGetPYByHZ(FcitxPinyinState*pystate, const char *strHZ, char *strPY)
2208
2072
{
2209
2073
    int i, j;
2210
2074
    char str_PY[MAX_PY_LENGTH + 1];
2235
2099
        SavePYFreq(pystate);
2236
2100
}
2237
2101
 
2238
 
void* LoadPYBaseDictWrapper(void * arg, FcitxModuleFunctionArg args)
2239
 
{
2240
 
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
2241
 
    if (!pystate->bPYBaseDictLoaded)
2242
 
        LoadPYBaseDict(pystate);
2243
 
    return NULL;
2244
 
}
2245
 
 
2246
 
void* PYGetPYByHZWrapper(void * arg, FcitxModuleFunctionArg args)
2247
 
{
2248
 
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
2249
 
    char *a = args.args[0];
2250
 
    char *b = args.args[1];
2251
 
    PYGetPYByHZ(pystate, a, b);
2252
 
    return NULL;
2253
 
 
2254
 
}
2255
 
void* DoPYInputWrapper(void * arg, FcitxModuleFunctionArg args)
2256
 
{
2257
 
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
2258
 
    FcitxKeySym *a = args.args[0];
2259
 
    unsigned int *b = args.args[1];
2260
 
    DoPYInput(pystate, *a, *b);
2261
 
    return NULL;
2262
 
 
2263
 
}
2264
 
void* PYGetCandWordsWrapper(void * arg, FcitxModuleFunctionArg args)
2265
 
{
2266
 
    PYGetCandWords(arg);
2267
 
    return NULL;
2268
 
}
2269
 
 
2270
 
void* PYGetFindStringWrapper(void * arg, FcitxModuleFunctionArg args)
2271
 
{
2272
 
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
2273
 
    return pystate->strFindString;
2274
 
 
2275
 
}
2276
 
void* PYResetWrapper(void * arg, FcitxModuleFunctionArg args)
2277
 
{
2278
 
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
2279
 
 
2280
 
    pystate->bSP = false;
2281
 
    pystate->strPYAuto[0] = '\0';
2282
 
    ResetPYStatus(pystate);
2283
 
 
2284
 
    return NULL;
2285
 
}
2286
 
 
2287
2102
void ReloadConfigPY(void* arg)
2288
2103
{
2289
2104
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
2408
2223
    return 0;
2409
2224
}
2410
2225
 
2411
 
 
2412
 
void* PYSP2QP(void* arg, FcitxModuleFunctionArg args)
 
2226
static char*
 
2227
PYSP2QP(FcitxPinyinState *pystate, const char* strSP)
2413
2228
{
2414
 
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
2415
 
    char* strSP = args.args[0];
2416
 
    char strQP[MAX_PY_LENGTH + 1];
2417
 
    strQP[0] = 0;
2418
 
 
 
2229
    char strQP[MAX_PY_LENGTH + 1] = "";
2419
2230
    SP2QP(&pystate->pyconfig, strSP, strQP);
2420
 
 
2421
2231
    return strdup(strQP);
2422
2232
}
2423
2233
 
2424
 
boolean PYGetPYMapByHZ(FcitxPinyinState* pystate, char* strHZ, char* mapHint, char* strMap)
 
2234
static boolean
 
2235
PYGetPYMapByHZ(FcitxPinyinState* pystate, char* strHZ,
 
2236
               char* mapHint, char* strMap)
2425
2237
{
2426
2238
    int i, j;
2427
2239
    PYFA* PYFAList = pystate->PYFAList;
2440
2252
    return false;
2441
2253
}
2442
2254
 
2443
 
void PYAddUserPhraseFromCString(void* arg, FcitxModuleFunctionArg args)
 
2255
static void
 
2256
PYAddUserPhraseFromCString(FcitxPinyinState *pystate, const char *strHZ)
2444
2257
{
2445
 
    FcitxPinyinState *pystate = (FcitxPinyinState*)arg;
2446
 
    char* strHZ = args.args[0], *sp, *pivot;
 
2258
    const char *pivot;
 
2259
    char *sp;
2447
2260
    char singleHZ[UTF8_MAX_LENGTH + 1];
2448
2261
    char strMap[3];
2449
2262
    if (!fcitx_utf8_check_string(strHZ))
2454
2267
    size_t hzCountLocal = 0;
2455
2268
 
2456
2269
    if (pystate->iPYSelected) {
2457
 
        int i = 0;
2458
 
        for (i = 0 ; i < pystate->iPYSelected; i ++)
 
2270
        int i;
 
2271
        for (i = 0 ; i < pystate->iPYSelected; i ++) {
2459
2272
            hzCountLocal += strlen(pystate->pySelected[i].strMap) / 2;
 
2273
        }
2460
2274
    }
2461
2275
    hzCountLocal += pystate->findMap.iHZCount;
2462
2276
 
2463
2277
    /* in order not to get a wrong one, use strict check */
2464
2278
    if (hzCountLocal != hzCount || hzCount > MAX_PY_PHRASE_LENGTH)
2465
2279
        return;
2466
 
    char* totalMap = fcitx_utils_malloc0(sizeof(char) * (1 + 2 * hzCount));
 
2280
    char *totalMap = fcitx_utils_malloc0(1 + 2 * hzCount);
2467
2281
 
2468
2282
    if (pystate->iPYSelected) {
2469
 
        int i = 0;
 
2283
        int i;
2470
2284
        for (i = 0 ; i < pystate->iPYSelected; i ++)
2471
2285
            strcat(totalMap, pystate->pySelected[i].strMap);
2472
2286
        strHZ = fcitx_utf8_get_nth_char(strHZ, strlen(totalMap) / 2);
2474
2288
 
2475
2289
    int i = 0;
2476
2290
    while (*strHZ) {
2477
 
        int chr;
 
2291
        uint32_t chr;
2478
2292
 
2479
2293
        sp = fcitx_utf8_get_char(strHZ, &chr);
2480
2294
        size_t len = sp - strHZ;
2481
2295
        strncpy(singleHZ, strHZ, len);
2482
2296
        singleHZ[len] = '\0';
2483
2297
 
2484
 
        if (!PYGetPYMapByHZ(pystate, singleHZ, pystate->findMap.strMap[i], strMap)) {
 
2298
        if (!PYGetPYMapByHZ(pystate, singleHZ, pystate->findMap.strMap[i],
 
2299
                            strMap)) {
2485
2300
            free(totalMap);
2486
2301
            return;
2487
2302
        }
2488
2303
 
2489
2304
        strncat(totalMap, strMap, 2);
2490
 
 
2491
2305
        strHZ = sp;
2492
2306
        i ++;
2493
2307
    }
2494
2308
 
2495
2309
    PYAddUserPhrase(pystate, pivot, totalMap, true);
2496
2310
    free(totalMap);
2497
 
 
2498
 
    return;
2499
2311
}
2500
2312
 
2501
 
// kate: indent-mode cstyle; space-indent on; indent-width 0;
 
2313
#include "fcitx-pinyin-addfunctions.h"