engine: use stable candidate pages

This commit is contained in:
2026-08-14 19:27:22 +09:00
parent 2e6d7d8e48
commit 4cac6f1a15
7 changed files with 306 additions and 51 deletions

View File

@@ -40,6 +40,64 @@ clearkouho(void)
im.sel = -1;
}
static void
selectfirst(void)
{
im.sel = im.nkouho > 0 ? 0 : -1;
}
static int
pagefirst(void)
{
if(im.sel < 0)
return 0;
return im.sel / Maxdisp * Maxdisp;
}
static int
movedelta(u32int ks)
{
static struct {
u32int key;
int delta;
} move[] = {
{Kup, -1}, {Kdown, 1},
{Kpgup, -Maxdisp}, {Kpgdown, Maxdisp},
};
int i;
for(i = 0; i < nelem(move); i++)
if(move[i].key == ks)
return move[i].delta;
return 0;
}
static void
movekouho(int delta)
{
if(im.sel < 0)
im.sel = 0;
else
im.sel += delta;
if(im.sel < 0)
im.sel = 0;
if(im.sel >= im.nkouho)
im.sel = im.nkouho - 1;
}
static int
numberedkouho(u32int ks, u32int mod)
{
int n;
if(mod != 0 || ks < '1' || ks > '9')
return -1;
n = pagefirst() + ks - '1';
if(n >= im.nkouho)
return -1;
return n;
}
static int
isjp(Im *p)
{
@@ -92,6 +150,7 @@ setkouho(Dictres *res)
im.kouho[i] = res->kouho[i];
im.nkouho++;
}
selectfirst();
}
static void
@@ -112,7 +171,7 @@ show(void)
!mapget(im.l->map, &pre, &dc.pre))
dc.pre = pre;
}
first = im.sel >= Maxdisp ? im.sel - Maxdisp + 1 : 0;
first = pagefirst();
n = im.nkouho - first;
if(n > Maxdisp) n = Maxdisp;
dc.nkouho = n;
@@ -484,6 +543,7 @@ emojiquery(void)
addkouho(&res.kouho[i]);
}
search.text = rawhit || !localhit ? raw : local;
selectfirst();
show();
}
@@ -566,25 +626,22 @@ picksearch(int n, Str *com)
static int
searchkey(u32int ks, u32int mod, Str *com)
{
int n, off;
int n;
Rune c;
if(ismodkey(ks))
return 0;
if(ks == Kdown || ks == Kup){
n = movedelta(ks);
if(n != 0){
if(im.nkouho == 0)
return 1;
if(ks == Kdown && im.sel < im.nkouho - 1)
im.sel++;
if(ks == Kup && im.sel > 0)
im.sel--;
movekouho(n);
show();
return 1;
}
n = ks - '1';
off = im.sel >= Maxdisp ? im.sel - Maxdisp + 1 : 0;
if(n >= 0 && n < Maxdisp && off + n < im.nkouho && mod == 0){
picksearch(off + n, com);
n = numberedkouho(ks, mod);
if(n >= 0){
picksearch(n, com);
return 1;
}
if(ks == Ktab){
@@ -655,29 +712,24 @@ searchkey(u32int ks, u32int mod, Str *com)
static int
keystroke(u32int ks, u32int mod, Str *com)
{
int n, off;
int n;
Rune c;
if(ismodkey(ks))
return 0;
if(search.lang)
return searchkey(ks, mod, com);
if(ks == Kdown || ks == Kup){
n = movedelta(ks);
if(n != 0){
if(im.nkouho == 0)
return 0;
if(ks == Kdown && im.sel < im.nkouho - 1)
im.sel++;
if(ks == Kup && im.sel >= 0)
im.sel--;
movekouho(n);
show();
return 1;
}
n = ks - '1';
off = 0;
if(im.sel >= Maxdisp)
off = im.sel - Maxdisp + 1;
if(n >= 0 && n < Maxdisp && off + n < im.nkouho && mod == 0){
sappend(com, &im.kouho[off + n]);
n = numberedkouho(ks, mod);
if(n >= 0){
sappend(com, &im.kouho[n]);
reset();
return 1;
}
@@ -769,6 +821,7 @@ init(void)
{
memset(&im, 0, sizeof(im));
im.l = getlang(LangEN);
im.sel = -1;
memset(&search, 0, sizeof search);
memset(&caret, 0, sizeof caret);
activeowner = nil;