fix: preserve complete Japanese readings

This commit is contained in:
2026-08-12 15:28:16 +09:00
parent 5ae0945015
commit ec7e6eb992
7 changed files with 319 additions and 32 deletions

164
strans.c
View File

@@ -5,6 +5,9 @@ static Im im;
static int popup = 0;
static int visible = 0;
static void dictqmap(Im*);
static void dictqjp(Im*);
static void backjp(Im*);
static int maplookup(Trie*, Str*, Str*);
typedef struct Search Search;
struct Search
@@ -23,8 +26,8 @@ backmap(Im *im)
Lang langs[] = {
{LangEN, "english", nil, transmap, backmap, dictqmap, nil, nil},
{LangJP, "hira", "kanji", transmap, backmap, dictqmap, nil, nil},
{LangJPK, "kata", "kanji", transmap, backmap, dictqmap, nil, nil},
{LangJP, "hira", "kanji", transmap, backjp, dictqjp, nil, nil},
{LangJPK, "kata", "kanji", transmap, backjp, dictqjp, nil, nil},
{LangKO, "hangul", nil, transko, backko, dictqmap, nil, nil},
{LangEMOJI, nil, "emoji", nil, nil, nil, nil, nil},
{LangVI, "telex", nil, transvi, backvi, dictqmap, nil, nil},
@@ -38,6 +41,48 @@ clearkouho(void)
im.sel = -1;
}
static int
isjp(Im *p)
{
return p->l->lang == LangJP || p->l->lang == LangJPK;
}
/*
* Japanese keeps completed kana in pre and an ambiguous romaji syllable in
* raw. A mapped raw suffix is part of the reading even while it remains
* pending (notably n, which may still grow into nya).
*/
static int
jpreading(Im *p, Str *s)
{
Str mapped;
*s = p->pre;
if(p->raw.n == 0)
return 1;
if(mapget(p->l->map, &p->raw, &mapped)){
sappend(s, &mapped);
return 1;
}
sappend(s, &p->raw);
return 0;
}
static int
haspre(Im *p)
{
return p->pre.n != 0 || (isjp(p) && p->raw.n != 0);
}
static void
impre(Str *s)
{
if(isjp(&im))
jpreading(&im, s);
else
*s = im.pre;
}
static void
setkouho(Dictres *res)
{
@@ -55,14 +100,18 @@ show(void)
{
static Drawcmd old;
Drawcmd dc;
Str *pre;
Str pre;
int i, first, n;
sclear(&dc.pre);
pre = search.on ? &search.text : &im.pre;
if(search.on)
pre = search.text;
else
impre(&pre);
if(popup){
if(search.on || im.l->map == nil || !mapget(im.l->map, pre, &dc.pre))
dc.pre = *pre;
if(search.on || isjp(&im) || im.l->map == nil ||
!mapget(im.l->map, &pre, &dc.pre))
dc.pre = pre;
}
first = im.sel >= Maxdisp ? im.sel - Maxdisp + 1 : 0;
n = im.nkouho - first;
@@ -96,7 +145,10 @@ dictsend(Im *im, Str *key)
req.key = *key;
req.lang = im->l->lang;
req.pre = im->pre;
if(isjp(im))
jpreading(im, &req.pre);
else
req.pre = im->pre;
channbsend(dictreqc, &req);
}
@@ -112,6 +164,18 @@ dictqmap(Im *im)
dictsend(im, &dict);
}
static void
dictqjp(Im *im)
{
Str reading;
clearkouho();
show();
if(im->l->dict == nil || !jpreading(im, &reading) || reading.n == 0)
return;
dictsend(im, &reading);
}
static int
setlang(int c)
{
@@ -129,7 +193,10 @@ commit(Str *com)
{
Str val;
if(im.l->map != nil && mapget(im.l->map, &im.pre, &val))
if(isjp(&im)){
jpreading(&im, &val);
sappend(com, &val);
}else if(im.l->map != nil && mapget(im.l->map, &im.pre, &val))
sappend(com, &val);
else
sappend(com, &im.pre);
@@ -137,6 +204,51 @@ commit(Str *com)
sclear(&im.raw);
}
static void
backjp(Im *im)
{
if(im->raw.n != 0)
spopr(&im->raw);
else
spopr(&im->pre);
}
static int
dotransjp(Rune c, Str *com)
{
Str key, mapped;
if(c == '\'' && im.raw.n == 1 && im.raw.r[0] == 'n'){
if(mapget(im.l->map, &im.raw, &mapped))
sappend(&im.pre, &mapped);
sclear(&im.raw);
dictqjp(&im);
return 1;
}
key = im.raw;
if(key.n < Maxrunes)
sputr(&key, c);
if(maplookup(im.l->map, &key, &mapped)){
im.raw = key;
dictqjp(&im);
return 1;
}
if(im.raw.n != 0 && mapget(im.l->map, &im.raw, &mapped)){
sappend(&im.pre, &mapped);
sclear(&im.raw);
sclear(&key);
sputr(&key, c);
if(maplookup(im.l->map, &key, &mapped)){
im.raw = key;
dictqjp(&im);
return 1;
}
}
commit(com);
clearkouho();
return 0;
}
static int
dotrans(Rune c, Str *com)
{
@@ -144,6 +256,8 @@ dotrans(Rune c, Str *com)
Dictreq req;
Str mapped;
if(isjp(&im))
return dotransjp(c, com);
e = im.l->trans(&im, c);
if(im.l->lang == LangVI){
if(e.s.n > 0 || !e.eat)
@@ -347,7 +461,7 @@ endsearch(void)
static void
startsearch(Str *com)
{
if(im.pre.n > 0)
if(haspre(&im))
commit(com);
sclear(&im.pre);
sclear(&im.raw);
@@ -495,7 +609,7 @@ keystroke(u32int ks, u32int mod, Str *com)
reset();
return 1;
}
if(im.pre.n > 0){
if(haspre(&im)){
commit(com);
reset();
return 1;
@@ -503,10 +617,10 @@ keystroke(u32int ks, u32int mod, Str *com)
return 0;
}
if(ks == Kback){
if(im.pre.n == 0)
if(!haspre(&im))
return 0;
im.l->back(&im);
if(im.pre.n == 0){
if(!haspre(&im)){
reset();
return 1;
}
@@ -514,7 +628,7 @@ keystroke(u32int ks, u32int mod, Str *com)
return 1;
}
if(ks == Kesc){
if(im.pre.n == 0)
if(!haspre(&im))
return 0;
reset();
return 1;
@@ -535,9 +649,14 @@ keystroke(u32int ks, u32int mod, Str *com)
show();
return 1;
}
reset();
if(setlang(c))
if(getlang(c) != nil){
if(isjp(&im) && haspre(&im))
commit(com);
reset();
setlang(c);
return 1;
}
reset();
return 0;
}
if(ks == '0' && im.nkouho > 0){
@@ -546,7 +665,7 @@ keystroke(u32int ks, u32int mod, Str *com)
return 1;
}
if(ks > 0x7f || ks == ' '){
if(im.pre.n == 0)
if(!haspre(&im))
return 0;
commit(com);
sputr(com, ks);
@@ -575,16 +694,23 @@ imhandlekey(Keyreq *kr)
sclear(&res.commit);
sclear(&res.preedit);
res.eaten = keystroke(kr->ks, kr->mod, &res.commit);
if(kr->want)
res.preedit = search.on ? search.text : im.pre;
if(kr->want){
if(search.on)
res.preedit = search.text;
else
impre(&res.preedit);
}
chansend(kr->reply, &res);
}
static void
dictresult(Dictres *res)
{
Str pre;
impre(&pre);
if(search.on || res->lang != im.l->lang ||
scmp(&res->key, &im.pre) != 0)
scmp(&res->key, &pre) != 0)
return;
setkouho(res);
show();