fcitx5 使用自定义 lua 扩展输入当前日期和时间

fcitx5 使用自定义 lua 扩展输入当前日期和时间

fcitx5 输入法在输入特殊”字符“时,可以通过 extension 转换成预定义的内容。

fcitx5 内置了一个 pinyin.lua 脚本。这个脚本我没有仔细读,大致是实现了:

  • 输入 “日期”,候选列表 提供 [2023-07-21],[2023年7月21日],[二〇二三年七月二十一日]
  • 输入 “时间”,候选列表 提供 [15:52],[15时52分],[十五时五十三分]

现在我使用起来,希望能够提供“时分秒”的时间精度,所以从我们将 /usr/share/fcitx5/lua/imeapi/extensions/pinyin.lua 复制到 /home/suse/.local/share/fcitx5/lua/imeapi/extensions/pinyin.lua,然后修改用户目录下的脚本。下面是脚本变更的部分:

脚本变更

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
┌──(suse@localhost)-[~/.local/share/fcitx5/lua/imeapi/extensions]
└─$ git --no-pager diff
diff --git a/pinyin.lua b/pinyin.lua
index f68484c..8af59b8 100644
--- a/pinyin.lua
+++ b/pinyin.lua
@@ -21,7 +21,7 @@ local _CHINESE_DIGITS = {
[10] = "十",
}
local _DATE_PATTERN = "^(%d+)-(%d+)-(%d+)$"
-local _TIME_PATTERN = "^(%d+):(%d+)$"
+local _TIME_PATTERN = "^(%d+):(%d+):(%d+)$"
local _MONTH_TABLE_LEAF = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }

local function get_chinese_math_num(num)
@@ -93,15 +93,18 @@ local function get_chinese_date(y, m, d, full)
end
end

-local function get_chinese_time(h, m, full)
+local function get_chinese_time(h, m, s, full)
if full then
local ret = get_chinese_math_num(h) .. "时"
- if m > 0 then
+ if m >= 0 then
ret = ret .. get_chinese_math_num(m) .. "分"
+ if s >= 0 then
+ ret = ret .. get_chinese_math_num(s) .. "秒"
+ end
end
return ret
else
- return h .. "时" .. m .. "分"
+ return h .. "时" .. m .. "分" .. s .. "秒"
end
end

@@ -109,8 +112,8 @@ local function normalize_date(y, m, d)
return string.format("%d-%02d-%02d", y, m, d)
end

-local function normalize_time(h, m)
- return string.format("%02d:%02d", h, m)
+local function normalize_time(h, m, s)
+ return string.format("%02d:%02d:%02d", h, m, s)
end

function pinyin_get_time(input)
@@ -120,18 +123,20 @@ function pinyin_get_time(input)

local now = input
if #input == 0 then
- now = os.date("%H:%M")
+ -- now = os.date("%H:%M")
+ now = os.date("%H:%M:%S")
end
local hour, minute
- now:gsub(_TIME_PATTERN, function(h, m)
+ now:gsub(_TIME_PATTERN, function(h, m, s)
hour = tonumber(h)
minute = tonumber(m)
+ seconds = tonumber(s)
end)
_verify_time(hour, minute)
return {
- normalize_time(hour, minute),
- get_chinese_time(hour, minute, false),
- get_chinese_time(hour, minute, true),
+ normalize_time(hour, minute, seconds),
+ get_chinese_time(hour, minute, seconds, false),
+ get_chinese_time(hour, minute, seconds, true),
}
end

完整脚本

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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
--
-- SPDX-FileCopyrightText: 2010 Peng Wu <[email protected]>
-- SPDX-FileCopyrightText: 2020 Weng Xuetian <[email protected]>
--
-- SPDX-License-Identifier: GPL-2.0-or-later
--
---- encoding: UTF-8
local fcitx = require("fcitx")

local _CHINESE_DIGITS = {
[0] = "〇",
[1] = "一",
[2] = "二",
[3] = "三",
[4] = "四",
[5] = "五",
[6] = "六",
[7] = "七",
[8] = "八",
[9] = "九",
[10] = "十",
}
local _DATE_PATTERN = "^(%d+)-(%d+)-(%d+)$"
local _TIME_PATTERN = "^(%d+):(%d+):(%d+)$"
local _MONTH_TABLE_LEAF = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }

local function get_chinese_math_num(num)
local ret
if num < 10 then
ret = _CHINESE_DIGITS[num]
elseif num < 20 then
ret = _CHINESE_DIGITS[10]
if num > 10 then
ret = ret .. _CHINESE_DIGITS[num % 10]
end
elseif num < 100 then
local mod = num % 10
ret = _CHINESE_DIGITS[(num - mod) / 10] .. _CHINESE_DIGITS[10]
if mod > 0 then
ret = ret .. _CHINESE_DIGITS[mod]
end
else
error("Invalid number")
end
return ret
end

local function get_chinese_non_math_num(num)
local ret = ""
for ch in tostring(num):gmatch(".") do
if ch >= "0" and ch <= "9" then
ch = _CHINESE_DIGITS[tonumber(ch)]
end
ret = ret .. ch
end
return ret
end

local function _verify_time(hour, minute)
if hour < 0 or hour > 23 or minute < 0 or minute > 59 then
error("Invalid time")
end
end

local function _verify_date(month, day)
if month < 1 or month > 12 or day < 1 or day > _MONTH_TABLE_LEAF[month] then
error("Invalid date")
end
end

local function _verify_date_with_year(year, month, day)
_verify_date(month, day)
if year < 1 or year > 9999 then
error("Invalid year")
end
if month == 2 and day == 29 then
if year % 400 ~= 0 and year % 100 == 0 then
error("Invalid lunar day")
end
if year % 4 ~= 0 then
error("Invalid lunar day")
end
end
end

local function get_chinese_date(y, m, d, full)
if full then
return get_chinese_non_math_num(y) .. "年" ..
get_chinese_math_num(m) .. "月" ..
get_chinese_math_num(d) .. "日"
else
return y .. "年" .. m .. "月" .. d .. "日"
end
end

local function get_chinese_time(h, m, s, full)
if full then
local ret = get_chinese_math_num(h) .. "时"
if m >= 0 then
ret = ret .. get_chinese_math_num(m) .. "分"
if s >= 0 then
ret = ret .. get_chinese_math_num(s) .. "秒"
end
end
return ret
else
return h .. "时" .. m .. "分" .. s .. "秒"
end
end

local function normalize_date(y, m, d)
return string.format("%d-%02d-%02d", y, m, d)
end

local function normalize_time(h, m, s)
return string.format("%02d:%02d:%02d", h, m, s)
end

function pinyin_get_time(input)
if fcitx.currentInputMethod() ~= "pinyin" and fcitx.currentInputMethod() ~= "shuangpin" then
return nil
end

local now = input
if #input == 0 then
-- now = os.date("%H:%M")
now = os.date("%H:%M:%S")
end
local hour, minute
now:gsub(_TIME_PATTERN, function(h, m, s)
hour = tonumber(h)
minute = tonumber(m)
seconds = tonumber(s)
end)
_verify_time(hour, minute)
return {
normalize_time(hour, minute, seconds),
get_chinese_time(hour, minute, seconds, false),
get_chinese_time(hour, minute, seconds, true),
}
end

function pinyin_get_date(input)
if fcitx.currentInputMethod() ~= "pinyin" and fcitx.currentInputMethod() ~= "shuangpin" then
return nil
end

local now = input
if #input == 0 then
now = os.date("%Y-%m-%d")
end
local year, month, day
now:gsub(_DATE_PATTERN, function(y, m, d)
year = tonumber(y)
month = tonumber(m)
day = tonumber(d)
end)
_verify_date_with_year(year, month, day)
return {
normalize_date(year, month, day),
get_chinese_date(year, month, day, false),
get_chinese_date(year, month, day, true),
}
end

function pinyin_get_current_time()
return pinyin_get_time("")
end

function pinyin_get_today()
return pinyin_get_date("")
end

local _MATH_SYMBOL = {
"+",
"-",
"<",
"=",
">",
"±",
"×",
"÷",
"∈",
"∏",
"∑",
"∕",
"√",
"∝",
"∞",
"∟",
"∠",
"∣",
"∥",
"∧",
"∨",
"∩",
"∪",
"∫",
"∮",
"∴",
"∵",
"∶",
"∷",
"∽",
"≈",
"≌",
"≒",
"≠",
"≡",
"≤",
"≥",
"≦",
"≧",
"≮",
"≯",
"⊕",
"⊙",
"⊥",
"⊿",
}

local _ROMAN_NUMBER = {
"ⅰ",
"ⅱ",
"ⅲ",
"ⅳ",
"ⅴ",
"ⅵ",
"ⅶ",
"ⅷ",
"ⅸ",
"ⅹ",
"ⅰ",
"ⅱ",
"ⅲ",
"ⅳ",
"ⅴ",
"ⅵ",
"ⅶ",
"ⅷ",
"ⅸ",
"ⅹ",
}

local _UPPER_GREEK_LETTER = {
"Α",
"Β",
"Γ",
"Δ",
"Ε",
"Ζ",
"Η",
"Θ",
"Ι",
"Κ",
"Λ",
"Μ",
"Ν",
"Ξ",
"Ο",
"Π",
"Ρ",
"Σ",
"Τ",
"Υ",
"Φ",
"Χ",
"Ψ",
"Ω",
}

local _LOWER_GREEK_LETTER = {
"α",
"β",
"γ",
"δ",
"ε",
"ζ",
"η",
"θ",
"ι",
"κ",
"λ",
"μ",
"ν",
"ξ",
"ο",
"π",
"ρ",
"σ",
"τ",
"υ",
"φ",
"χ",
"ψ",
"ω",
}

local _UPPER_RUSSIAN_LETTER = {
"А",
"Б",
"В",
"Г",
"Д",
"Е",
"Ж",
"З",
"И",
"Й",
"К",
"Л",
"М",
"Н",
"О",
"П",
"Р",
"С",
"Т",
"У",
"Ф",
"Х",
"Ц",
"Ч",
"Ш",
"Щ",
"Ъ",
"Ы",
"Ь",
"Э",
"Ю",
"Я",
"Ё",
}

local _LOWER_RUSSIAN_LETTER = {
"а",
"б",
"в",
"г",
"д",
"е",
"ж",
"з",
"и",
"й",
"к",
"л",
"м",
"н",
"о",
"п",
"р",
"с",
"т",
"у",
"ф",
"х",
"ц",
"ч",
"ш",
"щ",
"ъ",
"ы",
"ь",
"э",
"ю",
"я",
"ё",
}

local _NUMBER_SYMBOL = {
"①",
"②",
"③",
"④",
"⑤",
"⑥",
"⑦",
"⑧",
"⑨",
"⑩",
"⑴",
"⑵",
"⑶",
"⑷",
"⑸",
"⑹",
"⑺",
"⑻",
"⑼",
"⑽",
"⑾",
"⑿",
"⒀",
"⒁",
"⒂",
"⒃",
"⒄",
"⒅",
"⒆",
"⒇",
"⒈",
"⒉",
"⒊",
"⒋",
"⒌",
"⒍",
"⒎",
"⒏",
"⒐",
"⒑",
"⒒",
"⒓",
"⒔",
"⒕",
"⒖",
"⒗",
"⒘",
"⒙",
"⒚",
"⒛",
"㈠",
"㈡",
"㈢",
"㈣",
"㈤",
"㈥",
"㈦",
"㈧",
"㈨",
"㈩",
}

local _CURRENCY_SYMBOL = {
"$",
"¢",
"£",
"¥",
"¤",
}

local _ARROW_SYMBOL = {
"←",
"↑",
"→",
"↓",
"↖",
"↗",
"↘",
"↙",
}

function pinyin_get_symbol(input)
if fcitx.currentInputMethod() ~= "pinyin" and fcitx.currentInputMethod() ~= "shuangpin" then
return nil
end

if input == "" then
return {
{suggest="sx", help="数学符号"},
{suggest="lmsz", help="罗马数字"},
{suggest="dxxl", help="大写希腊"},
{suggest="xxxl", help="小写希腊"},
{suggest="dxew", help="大写俄文"},
{suggest="xxew", help="小写俄文"},
{suggest="sz", help="数字符号"},
{suggest="hb", help="货币"},
{suggest="jt", help="箭头"},
}
elseif input == "sx" then
return _MATH_SYMBOL
elseif input == "lmsz" then
return _ROMAN_NUMBER
elseif input == "dxxl" then
return _UPPER_GREEK_LETTER
elseif input == "xxxl" then
return _LOWER_GREEK_LETTER
elseif input == "dxew" then
return _UPPER_RUSSIAN_LETTER
elseif input == "xxew" then
return _LOWER_RUSSIAN_LETTER
elseif input == "sz" then
return _NUMBER_SYMBOL
elseif input == "hb" then
return _CURRENCY_SYMBOL
elseif input == "jt" then
return _ARROW_SYMBOL
end
return nil
end

------------
ime.register_command("fh", "pinyin_get_symbol", "输入符号", "digit", "")

ime.register_command("sj", "pinyin_get_time", "输入时间", "alpha", "输入可选时间,例如12:34")
ime.register_command("rq", "pinyin_get_date", "输入日期", "alpha", "输入可选日期,例如2013-01-01")
ime.register_trigger("pinyin_get_current_time", "显示时间", {}, {'时间'})
ime.register_trigger("pinyin_get_today", "显示日期", {}, {'日期'})


fcitx5 使用自定义 lua 扩展输入当前日期和时间
https://tedding.dev/2023/07/21/189774b2a10.html
作者
TED.DING
发布于
2023年7月21日
许可协议