-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathibus-setup-mozc.in
191 lines (150 loc) · 5.69 KB
/
ibus-setup-mozc.in
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from os import path, popen, system
import gtk
import pango
from gtk import glade
LOCALE_DIR = '@LOCALE_DIR@'
GLADE_FILE = path.join('@IBUS_MOZC_DIR@', 'setup.glade')
MOZC_CONFIG = path.join('@BIN_DIR@', 'mozc-config')
enums = {
'preedit_method': ['ROMAN', 'KANA'],
'punctuation_method': ['KUTEN_TOUTEN', 'COMMA_PERIOD',
'KUTEN_PERIOD', 'COMMA_TOUTEN'],
'symbol_method': ['CORNER_BRACKET_MIDDLE_DOT',
'SQUARE_BRACKET_SLCORNER_BRASH',
'CORNER_BRACKET_SLASH',
'SQUARE_BRACKET_MIDDLE_DOT'],
'space_character_form': ['FUNDAMENTAL_INPUT_MODE',
'FUNDAMENTAL_FULL_WIDTH',
'FUNDAMENTAL_HALF_WIDTH'],
'selection_shortcut': ['NO_SHORTCUT',
'SHORTCUT_123456789',
'SHORTCUT_ASDFGHJKL],'],
'session_keymap': ['CUSTOM', 'ATOK', 'MSIME', 'KOTOERI'],
'history_learning_level': ['DEFAULT_HISTORY', 'READ_ONLY', 'NO_HISTORY']
}
COMMANDS = []
def get(name):
return popen('%s -g %s' % (MOZC_CONFIG, name)).readline().split(': ')[1].strip()
def set(name, value):
print 'mozc-config -s', name, value
COMMANDS.append([name, value])
def init_widget(xml):
for k in enums.keys():
try:
xml.get_widget(k).set_active(enums[k].index(get(k)))
except:
pass
for k in ['use_single_kanji_conversion', 'use_symbol_conversion',
'use_date_conversion', 'use_number_conversion',
'use_auto_ime_turn_off',
'use_history_suggest','use_dictionary_suggest']:
xml.get_widget(k).set_active(True if get(k) == 'true' else False)
for k in ['suggestions_size']:
xml.get_widget(k).set_value(int(get(k)))
def on_changed_tree_view(combo, path, new_iter, model, id):
model[path][id] = combo.get_property("model").get(new_iter, 0)[0]
set_character_form_rules(model[path])
conv = {
'KATAKANA': 'カタカナ',
'ALPHABET': 'アルファベット',
'NUMBER': '数字',
'\\"\\\'': '"\'',
'#%&@$^_|`~\\\\': '#%&@$^_|`~\\',
'HALF_WIDTH': '半角',
'FULL_WIDTH': '全角',
'LAST_FORM': '学習',
'NO_CONVERSION': '無変換',
}
rev = {
'"\'': '"\'\\',
'#%&@$^_|`~\\': '#%&@$^_|`~\\',
}
for k, v in conv.items():
rev.setdefault(v, k)
def set_character_form_rules(rule):
rule = " ".join(["'"+rev.get(s, s)+"'" for s in rule])
print 'mozc-config -s', "character_form_rules", rule
COMMANDS.append(["character_form_rules", rule])
def get_character_form_rules():
rules = []
n = 0
for s in popen('%s -g %s' % (MOZC_CONFIG, "character_form_rules")):
if s.startswith('character_form_rules'):
rules.append([])
continue
elif s.startswith('}'):
n += 1
else:
s = s.split(': ')[1].strip()
if s[0] == s[-1] == '"':
s = s[1:-1]
rules[n].append(conv.get(s, s))
return rules
def init_tree_view(xml):
tv = xml.get_widget("character_form_rules")
cell_text = gtk.CellRendererText()
c = gtk.TreeViewColumn("文字グループ", cell_text, text=0)
c.set_alignment(0.5)
c.set_property("expand", True)
tv.append_column(c)
char_form = gtk.ListStore(str)
for item in ["HALF_WIDTH", "FULL_WIDTH", "LAST_FORM", "NO_CONVERSION"]:
char_form.append([conv.get(item, item)])
cell_combo1 = gtk.CellRendererCombo()
cell_combo1.set_property("xalign", 0.5)
cell_combo1.set_property("model", char_form)
cell_combo1.set_property("editable", True)
cell_combo1.set_property("text-column", 0)
c = gtk.TreeViewColumn("変換前文字列", cell_combo1, text=1)
c.set_alignment(0.5)
c.set_property("expand", True)
tv.append_column(c)
cell_combo2 = gtk.CellRendererCombo()
cell_combo2.set_property("xalign", 0.5)
cell_combo2.set_property("model", char_form)
cell_combo2.set_property("editable", True)
cell_combo2.set_property("text-column", 0)
c = gtk.TreeViewColumn("変換中文字列", cell_combo2, text=2)
c.set_alignment(0.5)
c.set_property("expand", True)
tv.append_column(c)
rules = gtk.ListStore(str, str, str)
tv.set_model(rules)
for rule in get_character_form_rules():
rules.append(rule)
cell_combo1.connect("changed", on_changed_tree_view, rules, 1)
cell_combo2.connect("changed", on_changed_tree_view, rules, 2)
class MozcSetup(object):
def __init__(self):
glade.bindtextdomain("ibus-mozc", LOCALE_DIR)
glade.textdomain("ibus-mozc")
glade_file = GLADE_FILE
self.xml = glade.XML(glade_file)
init_tree_view(self.xml)
init_widget(self.xml)
self.xml.signal_autoconnect(self)
def on_ok_button_clicked(self, widget):
print 'OK button clicked'
for k, v in COMMANDS:
print '%s -s %s %s' % (MOZC_CONFIG, k, v)
system('%s -s %s %s' % (MOZC_CONFIG, k, v))
system('%s -r' % MOZC_CONFIG)
gtk.main_quit()
def on_cancel_button_clicked(self, widget):
print 'Cancel button clicked'
gtk.main_quit()
def on_combobox_changed(self, widget):
set(widget.name, enums[widget.name][widget.get_active()])
def on_checkbutton_toggled(self, widget):
set(widget.name, 'true' if widget.get_active() else 'false')
def on_spinbutton_value_changed(self, widget):
set(widget.name, str(widget.get_value_as_int()))
def on_main_delete(self, widget, event):
self.on_cancel_button_clicked(widget)
return True
def run(self):
gtk.main()
if __name__ == "__main__":
MozcSetup().run()