| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
local fcitx = require("fcitx") |
| 9 |
|
| 10 |
local _CHINESE_DIGITS = { |
| 11 |
[0] = "〇", |
| 12 |
[1] = "一", |
| 13 |
[2] = "二", |
| 14 |
[3] = "三", |
| 15 |
[4] = "四", |
| 16 |
[5] = "五", |
| 17 |
[6] = "六", |
| 18 |
[7] = "七", |
| 19 |
[8] = "八", |
| 20 |
[9] = "九", |
| 21 |
[10] = "十", |
| 22 |
} |
| 23 |
local _DATE_PATTERN = "^(%d+)-(%d+)-(%d+)$" |
| 24 |
local _TIME_PATTERN = "^(%d+):(%d+)$" |
| 25 |
local _MONTH_TABLE_LEAF = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } |
| 26 |
|
| 27 |
local function get_chinese_math_num(num) |
| 28 |
local ret |
| 29 |
if num < 10 then |
| 30 |
ret = _CHINESE_DIGITS[num] |
| 31 |
elseif num < 20 then |
| 32 |
ret = _CHINESE_DIGITS[10] |
| 33 |
if num > 10 then |
| 34 |
ret = ret .. _CHINESE_DIGITS[num % 10] |
| 35 |
end |
| 36 |
elseif num < 100 then |
| 37 |
local mod = num % 10 |
| 38 |
ret = _CHINESE_DIGITS[(num - mod) / 10] .. _CHINESE_DIGITS[10] |
| 39 |
if mod > 0 then |
| 40 |
ret = ret .. _CHINESE_DIGITS[mod] |
| 41 |
end |
| 42 |
else |
| 43 |
error("Invalid number") |
| 44 |
end |
| 45 |
return ret |
| 46 |
end |
| 47 |
|
| 48 |
local function get_chinese_non_math_num(num) |
| 49 |
local ret = "" |
| 50 |
for ch in tostring(num):gmatch(".") do |
| 51 |
local c = ch |
| 52 |
if c >= "0" and c <= "9" then |
| 53 |
c = _CHINESE_DIGITS[tonumber(c)] |
| 54 |
end |
| 55 |
ret = ret .. c |
| 56 |
end |
| 57 |
return ret |
| 58 |
end |
| 59 |
|
| 60 |
local function _verify_time(hour, minute) |
| 61 |
if hour < 0 or hour > 23 or minute < 0 or minute > 59 then |
| 62 |
error("Invalid time") |
| 63 |
end |
| 64 |
end |
| 65 |
|
| 66 |
local function _verify_date(month, day) |
| 67 |
if month < 1 or month > 12 or day < 1 or day > _MONTH_TABLE_LEAF[month] then |
| 68 |
error("Invalid date") |
| 69 |
end |
| 70 |
end |
| 71 |
|
| 72 |
local function _verify_date_with_year(year, month, day) |
| 73 |
_verify_date(month, day) |
| 74 |
if year < 1 or year > 9999 then |
| 75 |
error("Invalid year") |
| 76 |
end |
| 77 |
if month == 2 and day == 29 then |
| 78 |
if year % 400 ~= 0 and year % 100 == 0 then |
| 79 |
error("Invalid lunar day") |
| 80 |
end |
| 81 |
if year % 4 ~= 0 then |
| 82 |
error("Invalid lunar day") |
| 83 |
end |
| 84 |
end |
| 85 |
end |
| 86 |
|
| 87 |
local function get_chinese_date(y, m, d, full) |
| 88 |
if full then |
| 89 |
return get_chinese_non_math_num(y) .. "年" .. |
| 90 |
get_chinese_math_num(m) .. "月" .. |
| 91 |
get_chinese_math_num(d) .. "日" |
| 92 |
else |
| 93 |
return y .. "年" .. m .. "月" .. d .. "日" |
| 94 |
end |
| 95 |
end |
| 96 |
|
| 97 |
local function get_chinese_time(h, m, full) |
| 98 |
if full then |
| 99 |
local ret = get_chinese_math_num(h) .. "时" |
| 100 |
if m > 0 then |
| 101 |
ret = ret .. get_chinese_math_num(m) .. "分" |
| 102 |
end |
| 103 |
return ret |
| 104 |
else |
| 105 |
return h .. "时" .. m .. "分" |
| 106 |
end |
| 107 |
end |
| 108 |
|
| 109 |
local function normalize_date(y, m, d) |
| 110 |
return string.format("%d-%02d-%02d", y, m, d) |
| 111 |
end |
| 112 |
|
| 113 |
local function normalize_time(h, m) |
| 114 |
return string.format("%02d:%02d", h, m) |
| 115 |
end |
| 116 |
|
| 117 |
function pinyin_get_time(input) |
| 118 |
if fcitx.currentInputMethod() ~= "pinyin" and fcitx.currentInputMethod() ~= "shuangpin" then |
| 119 |
return nil |
| 120 |
end |
| 121 |
|
| 122 |
local now = input |
| 123 |
if #input == 0 then |
| 124 |
now = os.date("%H:%M") |
| 125 |
end |
| 126 |
local hour, minute |
| 127 |
now:gsub(_TIME_PATTERN, function(h, m) |
| 128 |
hour = tonumber(h) |
| 129 |
minute = tonumber(m) |
| 130 |
end) |
| 131 |
_verify_time(hour, minute) |
| 132 |
return { |
| 133 |
normalize_time(hour, minute), |
| 134 |
get_chinese_time(hour, minute, false), |
| 135 |
get_chinese_time(hour, minute, true), |
| 136 |
} |
| 137 |
end |
| 138 |
|
| 139 |
function pinyin_get_date(input) |
| 140 |
if fcitx.currentInputMethod() ~= "pinyin" and fcitx.currentInputMethod() ~= "shuangpin" then |
| 141 |
return nil |
| 142 |
end |
| 143 |
|
| 144 |
local now = input |
| 145 |
if #input == 0 then |
| 146 |
now = os.date("%Y-%m-%d") |
| 147 |
end |
| 148 |
local year, month, day |
| 149 |
now:gsub(_DATE_PATTERN, function(y, m, d) |
| 150 |
year = tonumber(y) |
| 151 |
month = tonumber(m) |
| 152 |
day = tonumber(d) |
| 153 |
end) |
| 154 |
_verify_date_with_year(year, month, day) |
| 155 |
return { |
| 156 |
normalize_date(year, month, day), |
| 157 |
get_chinese_date(year, month, day, false), |
| 158 |
get_chinese_date(year, month, day, true), |
| 159 |
} |
| 160 |
end |
| 161 |
|
| 162 |
function pinyin_get_current_time() |
| 163 |
return pinyin_get_time("") |
| 164 |
end |
| 165 |
|
| 166 |
function pinyin_get_today() |
| 167 |
return pinyin_get_date("") |
| 168 |
end |
| 169 |
|
| 170 |
local _MATH_SYMBOL = { |
| 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 |
local _ROMAN_NUMBER = { |
| 219 |
"ⅰ", |
| 220 |
"ⅱ", |
| 221 |
"ⅲ", |
| 222 |
"ⅳ", |
| 223 |
"ⅴ", |
| 224 |
"ⅵ", |
| 225 |
"ⅶ", |
| 226 |
"ⅷ", |
| 227 |
"ⅸ", |
| 228 |
"ⅹ", |
| 229 |
"ⅰ", |
| 230 |
"ⅱ", |
| 231 |
"ⅲ", |
| 232 |
"ⅳ", |
| 233 |
"ⅴ", |
| 234 |
"ⅵ", |
| 235 |
"ⅶ", |
| 236 |
"ⅷ", |
| 237 |
"ⅸ", |
| 238 |
"ⅹ", |
| 239 |
} |
| 240 |
|
| 241 |
local _UPPER_GREEK_LETTER = { |
| 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 |
local _LOWER_GREEK_LETTER = { |
| 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 |
local _UPPER_RUSSIAN_LETTER = { |
| 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 |
local _LOWER_RUSSIAN_LETTER = { |
| 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 |
local _NUMBER_SYMBOL = { |
| 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 |
local _CURRENCY_SYMBOL = { |
| 431 |
"$", |
| 432 |
"¢", |
| 433 |
"£", |
| 434 |
"¥", |
| 435 |
"¤", |
| 436 |
} |
| 437 |
|
| 438 |
local _ARROW_SYMBOL = { |
| 439 |
"←", |
| 440 |
"↑", |
| 441 |
"→", |
| 442 |
"↓", |
| 443 |
"↖", |
| 444 |
"↗", |
| 445 |
"↘", |
| 446 |
"↙", |
| 447 |
} |
| 448 |
|
| 449 |
function pinyin_get_symbol(input) |
| 450 |
if fcitx.currentInputMethod() ~= "pinyin" and fcitx.currentInputMethod() ~= "shuangpin" then |
| 451 |
return nil |
| 452 |
end |
| 453 |
|
| 454 |
if input == "" then |
| 455 |
return { |
| 456 |
{suggest="sx", help="数学符号"}, |
| 457 |
{suggest="lmsz", help="罗马数字"}, |
| 458 |
{suggest="dxxl", help="大写希腊"}, |
| 459 |
{suggest="xxxl", help="小写希腊"}, |
| 460 |
{suggest="dxew", help="大写俄文"}, |
| 461 |
{suggest="xxew", help="小写俄文"}, |
| 462 |
{suggest="sz", help="数字符号"}, |
| 463 |
{suggest="hb", help="货币"}, |
| 464 |
{suggest="jt", help="箭头"}, |
| 465 |
} |
| 466 |
elseif input == "sx" then |
| 467 |
return _MATH_SYMBOL |
| 468 |
elseif input == "lmsz" then |
| 469 |
return _ROMAN_NUMBER |
| 470 |
elseif input == "dxxl" then |
| 471 |
return _UPPER_GREEK_LETTER |
| 472 |
elseif input == "xxxl" then |
| 473 |
return _LOWER_GREEK_LETTER |
| 474 |
elseif input == "dxew" then |
| 475 |
return _UPPER_RUSSIAN_LETTER |
| 476 |
elseif input == "xxew" then |
| 477 |
return _LOWER_RUSSIAN_LETTER |
| 478 |
elseif input == "sz" then |
| 479 |
return _NUMBER_SYMBOL |
| 480 |
elseif input == "hb" then |
| 481 |
return _CURRENCY_SYMBOL |
| 482 |
elseif input == "jt" then |
| 483 |
return _ARROW_SYMBOL |
| 484 |
end |
| 485 |
return nil |
| 486 |
end |
| 487 |
|
| 488 |
|
| 489 |
ime.register_command("fh", "pinyin_get_symbol", "输入符号", "digit", "") |
| 490 |
|
| 491 |
ime.register_command("sj", "pinyin_get_time", "输入时间", "alpha", "输入可选时间,例如12:34") |
| 492 |
ime.register_command("rq", "pinyin_get_date", "输入日期", "alpha", "输入可选日期,例如2013-01-01") |
| 493 |
ime.register_trigger("pinyin_get_current_time", "显示时间", {}, {'时间'}) |
| 494 |
ime.register_trigger("pinyin_get_today", "显示日期", {}, {'日期'}) |