summaryrefslogtreecommitdiffstats
path: root/src/PYFallbackEditor.cc
blob: 6ac5449ce6186876c93d9e43362bc33d56d1aeb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* vim:set et ts=4 sts=4:
 *
 * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus
 *
 * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@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 "PYFallbackEditor.h"
#include "PYHalfFullConverter.h"
#include "PYPinyinProperties.h"

namespace PY {

inline gboolean
FallbackEditor::processPunctForSimplifiedChinese (guint keyval, guint keycode, guint modifiers)
{
    switch (keyval) {
    case '`':
        commit ("·"); return TRUE;
    case '~':
        commit ("~"); return TRUE;
    case '!':
        commit ("!"); return TRUE;
    // case '@':
    // case '#':
    case '$':
        commit ("¥"); return TRUE;
    // case '%':
    case '^':
        commit ("……"); return TRUE;
    // case '&':
    // case '*':
    case '(':
        commit ("("); return TRUE;
    case ')':
        commit (")"); return TRUE;
    // case '-':
    case '_':
        commit ("——"); return TRUE;
    // case '=':
    // case '+':
    case '[':
        commit ("【"); return TRUE;
    case ']':
        commit ("】"); return TRUE;
    case '{':
        commit ("『"); return TRUE;
    case '}':
        commit ("』"); return TRUE;
    case '\\':
        commit ("、"); return TRUE;
    // case '|':
    case ';':
        commit (";"); return TRUE;
    case ':':
        commit (":"); return TRUE;
    case '\'':
        commit (m_quote ? "‘" : "’");
        m_quote = !m_quote;
        return TRUE;
    case '"':
        commit (m_double_quote ? "“" : "”");
        m_double_quote = !m_double_quote;
        return TRUE;
    case ',':
        commit (","); return TRUE;
    case '.':
        if (m_prev_committed_char >= '0' && m_prev_committed_char <= '9')
            return FALSE;
        else
            commit ("。");
        return TRUE;
    case '<':
        commit ("《"); return TRUE;
    case '>':
        commit ("》"); return TRUE;
    // case '/':
    case '?':
        commit ("?"); return TRUE;
    }
    return FALSE;
}

inline gboolean
FallbackEditor::processPunctForTraditionalChinese (guint keyval, guint keycode, guint modifiers)
{
    switch (keyval) {
    case '~':
        commit ("~"); return TRUE;
    case '!':
        commit ("!"); return TRUE;
    // case '@':
    // case '#':
    case '$':
        commit ("¥"); return TRUE;
    // case '%':
    case '^':
        commit ("……"); return TRUE;
    // case '&':
    // case '*':
    case '(':
        commit ("("); return TRUE;
    case ')':
        commit (")"); return TRUE;
    // case '-':
    case '_':
        commit ("——"); return TRUE;
    // case '=':
    // case '+':
    case '[':
        commit ("「"); return TRUE;
    case ']':
        commit ("」"); return TRUE;
    case '{':
        commit ("『"); return TRUE;
    case '}':
        commit ("』"); return TRUE;
    case '\\':
        commit ("、"); return TRUE;
    // case '|':
    case ';':
        commit (";"); return TRUE;
    case ':':
        commit (":"); return TRUE;
    case '\'':
        commit (m_quote ? "‘" : "’");
        m_quote = !m_quote;
        return TRUE;
    case '"':
        commit (m_double_quote ? "“" : "”");
        m_double_quote = !m_double_quote;
        return TRUE;
    case ',':
        commit (","); return TRUE;
    case '.':
        if (m_prev_committed_char >= '0' && m_prev_committed_char <= '9')
            return FALSE;
        else
            commit ("。");
        return TRUE;
    case '<':
        commit ("《"); return TRUE;
    case '>':
        commit ("》"); return TRUE;
    case '?':
        commit ("?"); return TRUE;
    }
    return FALSE;
}

inline gboolean
FallbackEditor::processPunct (guint keyval, guint keycode, guint modifiers)
{
    guint cmshm_modifiers = cmshm_filter (modifiers);

    /* check ctrl, alt, hyper, supper masks */
    if (cmshm_modifiers != 0)
        return FALSE;

    /* English mode */
    if (G_UNLIKELY (!m_props.modeChinese ())) {
        if (G_UNLIKELY (m_props.modeFull ()))
            commit (HalfFullConverter::toFull (keyval));
        else
            return FALSE;
        return TRUE;
    }
    else {
        /* Chinese mode */
        if (m_props.modeFullPunct ()) {
            if (m_props.modeSimp ()) {
                if (processPunctForSimplifiedChinese (keyval, keycode, modifiers))
                    return TRUE;
            }
            else {
                if (processPunctForTraditionalChinese (keyval, keycode, modifiers))
                    return TRUE;
            }
        }
        if (m_props.modeFull ())
            commit (HalfFullConverter::toFull (keyval));
        else
            return FALSE;
    }
    return TRUE;
}

gboolean
FallbackEditor::processKeyEvent (guint keyval, guint keycode, guint modifiers)
{
    gboolean retval = FALSE;

    modifiers &= (IBUS_CONTROL_MASK |
                  IBUS_MOD1_MASK |
                  IBUS_SUPER_MASK |
                  IBUS_HYPER_MASK |
                  IBUS_META_MASK);

    switch (keyval) {
        /* numbers */
        case IBUS_KP_0 ... IBUS_KP_9:
            keyval = keyval - IBUS_KP_0 + IBUS_0;
        case IBUS_0 ... IBUS_9:
        /* letters */
        case IBUS_a ... IBUS_z:
        case IBUS_A ... IBUS_Z:
            if (modifiers == 0) {
                if (!m_props.modeFull ())
                    return FALSE;

                commit (HalfFullConverter::toFull (keyval));
                retval = TRUE;
            }
            break;
        /* punct */
        case IBUS_exclam ... IBUS_slash:
        case IBUS_colon ... IBUS_at:
        case IBUS_bracketleft ... IBUS_quoteleft:
        case IBUS_braceleft ... IBUS_asciitilde:
            retval = processPunct (keyval, keycode, modifiers);
            break;
        case IBUS_KP_Equal:
            retval = processPunct ('=', keycode, modifiers);
            break;
        case IBUS_KP_Multiply:
            retval = processPunct ('*', keycode, modifiers);
            break;
        case IBUS_KP_Add:
            retval = processPunct ('+', keycode, modifiers);
            break;
        #if 0
        case IBUS_KP_Separator:
            retval = processPunct (IBUS_separator, keycode, modifiers);
            break;
        #endif
        case IBUS_KP_Subtract:
            retval = processPunct ('-', keycode, modifiers);
            break;
        case IBUS_KP_Decimal:
            retval = processPunct ('.', keycode, modifiers);
            break;
        case IBUS_KP_Divide:
            retval = processPunct ('/', keycode, modifiers);
            break;
        /* space */
        case IBUS_KP_Space:
            keyval = IBUS_space;
        case IBUS_space:
            if (modifiers == 0) {
                if (!m_props.modeFull ())
                    return FALSE;

                commit (" ");
                retval = TRUE;
            }
            break;
        /* others */
        default:
            break;
    }
    return retval;
}

void
FallbackEditor::reset (void) {
    m_quote = TRUE;
    m_double_quote = TRUE;
    m_prev_committed_char = 0;
}

};