triple click selection in sam

three clicks selects a whitespace-delimited line.
This commit is contained in:
Ori Bernstein 2020-04-25 08:57:26 -07:00
parent 614b18484c
commit f616a0c1bd
7 changed files with 54 additions and 18 deletions

View file

@ -54,7 +54,7 @@ char *hname[] = {
[Hsnarflen] "Hsnarflen",
[Hack] "Hack",
[Hexit] "Hexit",
[Hplumb] "Hplumb",
[Hplumb] "Hplumb",
};
char *tname[] = {
@ -76,11 +76,12 @@ char *tname[] = {
[Tsearch] "Tsearch",
[Tsend] "Tsend",
[Tdclick] "Tdclick",
[Ttclick] "Ttclick",
[Tstartsnarf] "Tstartsnarf",
[Tsetsnarf] "Tsetsnarf",
[Tack] "Tack",
[Texit] "Texit",
[Tplumb] "Tplumb",
[Tplumb] "Tplumb",
};
void
@ -458,9 +459,10 @@ inmesg(Tmesg type)
break;
case Tdclick:
case Ttclick:
f = whichfile(inshort());
p1 = inlong();
doubleclick(f, p1);
stretchsel(f, p1, type == Ttclick);
f->tdot.p1 = f->tdot.p2 = p1;
telldot(f);
outTs(Hunlockfile, f->tag);

View file

@ -1,5 +1,6 @@
/* VERSION 1 introduces plumbing
2 increases SNARFSIZE from 4096 to 32000
3 adds a triple click
*/
#define VERSION 2
@ -34,6 +35,7 @@ typedef enum Tmesg
Tack, /* acknowledge Hack */
Texit, /* exit */
Tplumb, /* send plumb message */
Ttclick, /* triple click */
TMAX,
}Tmesg;
/*

View file

@ -61,7 +61,7 @@ lookorigin(File *f, Posn p0, Posn ls)
}
int
alnum(int c)
isalnum(int c)
{
/*
* Hard to get absolutely right. Use what we know about ASCII
@ -77,6 +77,19 @@ alnum(int c)
return 1;
}
int
isspace(Rune c)
{
return c == 0 || c == ' ' || c == '\t' ||
c == '\n' || c == '\r' || c == '\v';
}
int
inmode(Rune r, int mode)
{
return (mode == 0) ? isalnum(r) : r && !isspace(r);
}
int
clickmatch(File *f, int cl, int cr, int dir, Posn *p)
{
@ -119,8 +132,15 @@ strrune(Rune *s, Rune c)
return 0;
}
/*
* Stretches a selection out over current text,
* selecting matching range if possible.
* If there's no matching range, mode 0 selects
* a single alphanumeric region. Mode 1 selects
* a non-whitespace region.
*/
void
doubleclick(File *f, Posn p1)
stretchsel(File *f, Posn p1, int mode)
{
int c, i;
Rune *r, *l;
@ -163,11 +183,11 @@ doubleclick(File *f, Posn p1)
}
/* try filling out word to right */
p = p1;
while(p < f->nc && alnum(filereadc(f, p++)))
while(p < f->nc && inmode(filereadc(f, p++), mode))
f->dot.r.p2++;
/* try filling out word to left */
p = p1;
while(--p >= 0 && alnum(filereadc(f, p)))
while(--p >= 0 && inmode(filereadc(f, p), mode))
f->dot.r.p1--;
}

View file

@ -241,7 +241,7 @@ File *current(File*);
void delete(File*);
void delfile(File*);
void dellist(List*, int);
void doubleclick(File*, Posn);
void stretchsel(File*, Posn, int);
void dprint(char*, ...);
void edit(File*, int);
void *emalloc(ulong);

View file

@ -252,15 +252,23 @@ fldelete(Flayer *l, long p0, long p1)
int
flselect(Flayer *l)
{
static int clickcount;
static Point clickpt = {-10, -10};
int dt, dx, dy;
if(l->visible!=All)
flupfront(l);
if(l->f.p0==l->f.p1)
if(mousep->msec-l->click<Clicktime && l->f.p0+l->origin==l->p0 &&
l->f.p0==frcharofpt(&l->f, mousep->xy)){
l->click = 0;
return 1;
}
dt = l->click = mousep->msec;
dx = abs(mousep->xy.x - clickpt.x);
dy = abs(mousep->xy.y - clickpt.y);
l->click = mousep->msec;
clickpt = mousep->xy;
if(dx < 3 && dy < 3 && dt < Clicktime && clickcount < 3)
return ++clickcount;
clickcount = 0;
frselect(&l->f, mousectl);
l->p0 = l->f.p0+l->origin, l->p1 = l->f.p1+l->origin;
return 0;

View file

@ -5,7 +5,7 @@ typedef enum Vis{
}Vis;
enum{
Clicktime=1000, /* one second */
Clicktime=500, /* milliseconds */
};
typedef struct Flayer Flayer;

View file

@ -31,7 +31,7 @@ int spacesindent;
void
threadmain(int argc, char *argv[])
{
int i, got, scr, chord;
int i, got, nclick, scr, chord;
Text *t;
Rectangle r;
Flayer *nwhich;
@ -105,8 +105,12 @@ threadmain(int argc, char *argv[])
current(nwhich);
else{
t=(Text *)which->user1;
if(flselect(which)){
outTsl(Tdclick, t->tag, which->p0);
nclick = flselect(which);
if(nclick > 0){
if(nclick > 1)
outTsl(Ttclick, t->tag, which->p0);
else
outTsl(Tdclick, t->tag, which->p0);
t->lock++;
}else if(t!=&cmd)
outcmd();