richterm

"terminal emulator" with support for text fonts and images for plan9
git clone git://nsmpr.xyz/richterm.git
Log | Files | Refs | README

commit e9d452185710e8009db49c876c876312640455ec
parent 191b0d598e6c8cd4d503794475e6d89dc89c7ede
Author: glenda <glenda@9front.local>
Date:   Tue, 17 Aug 2021 13:40:23 +0000

switch from Data to Array

Diffstat:
MTODO | 2++
Marray.c | 14+++++++-------
Marray.h | 4++--
Mdevfs.c | 4++--
Mrichterm.c | 44++++++++++++++++++++++++++------------------
Mrichterm.h | 24+++++++++++-------------
6 files changed, 50 insertions(+), 42 deletions(-)

diff --git a/TODO b/TODO @@ -3,3 +3,5 @@ Only store pointer offset in objects, so that offset of next obj is treated as e Remove Views and draw Objects directly, maybe even letter by letter. Keep track of object's positional info, such as start point, end poit and next line offset. + +After transitioning from Data to Array struct there are some inappropriate uses of Array (manual memory management instead of defined funcs, raw variable instead of pointer). Go over code and fix this. diff --git a/array.c b/array.c @@ -36,17 +36,17 @@ arrayfree(Array *ap) } void * -arrayadd(Array *ap) +arraygrow(Array *ap, long n) { qlock(ap->l); - ap->count++; + ap->count += n; if (ap->count > ap->n) { ap->n += ap->n; - ap->mem = realloc(ap->mem, ap->size * ap->n); + ap->p = realloc(ap->p, ap->size * ap->n); } - memset(arrayget(ap, ap->count - 1), 0, ap->size); + memset(arrayget(ap, ap->count - n), 0, ap->size * n); qunlock(ap->l); - return (void *)(ap->mem + ap->size * (ap->count - 1)); + return (void *)(ap->p + ap->size * (ap->count - n)); } void @@ -54,7 +54,7 @@ arraydel(Array *ap, long n) { char *v; qlock(ap->l); - v = ap->mem + ap->size * n; + v = ap->p + ap->size * n; if (ap->free != nil) ap->free(v); memcpy(v, v + ap->size, (ap->count - n) * ap->size); ap->count--; @@ -64,5 +64,5 @@ arraydel(Array *ap, long n) void * arrayget(Array *ap, long n) { - return (void *)(ap->mem + ap->size * n); + return (void *)(ap->p + ap->size * n); } diff --git a/array.h b/array.h @@ -5,12 +5,12 @@ struct Array { usize size; usize n; long count; - char *mem; + char *p; void (*free)(void *); }; Array * arraycreate(usize, long, void (*free)(void *)); void arrayfree(Array *); -void * arrayadd(Array *); +void * arraygrow(Array *, long); void arraydel(Array *, long); void * arrayget(Array *, long); diff --git a/devfs.c b/devfs.c @@ -15,7 +15,7 @@ devfs_read(Req *r) { File *f; Devfsctl *dctl; - Data dv; + Array dv; f = r->fid->file; dctl = f->aux; if (f == cons) { @@ -59,7 +59,7 @@ initdevfs(void) dctl = mallocz(sizeof(Devfsctl), 1); dctl->wc = chancreate(sizeof(char *), 0); - dctl->rc = chancreate(sizeof(Data), 1024); + dctl->rc = chancreate(sizeof(Array), 1024); srv.tree = alloctree("richterm", "richterm", DMDIR|0555, nil); if (srv.tree == nil) return nil; cons = createfile(srv.tree->root, "cons", "richterm", 0666, dctl); diff --git a/richterm.c b/richterm.c @@ -68,7 +68,7 @@ threadmain(int argc, char **argv) int rv[2], mmode; Mouse mv; Rune kv; - Data dv; + Array dv; ARGBEGIN{ case 'D': @@ -78,7 +78,11 @@ threadmain(int argc, char **argv) usage(); } ARGEND - dv = (Data) {nil, 0}; + //dv = (Data) {.p=nil, .n=0}; + dv.p = nil; + dv.n = 0; + dv.count = 0; + mmode = MM_NONE; if (initdraw(0, 0, "richterm") < 0) @@ -98,7 +102,7 @@ threadmain(int argc, char **argv) display, Rect(0,0,1,1), screen->chan, 1, DBlue); fonts = arraycreate(sizeof(Font *), 2, nil); - fp = arrayadd(fonts); + fp = arraygrow(fonts, 1); *fp = font; rich.l = mallocz(sizeof(QLock), 1); @@ -107,6 +111,7 @@ threadmain(int argc, char **argv) rich.objects = arraycreate(sizeof(Object *), 8, nil); rich.views = arraycreate(sizeof(View), 8, nil); + rich.text = arraycreate(sizeof(char), 4096, nil); rich.page.scroll = ZP; @@ -289,14 +294,14 @@ mouse(Mousectl *mc, Mouse mv, int *mmode) } if (mv.buttons == 4) { Object *obj; - Data *dlink; + Array *dlink; v = getview(mv.xy); obj = nil; dlink = nil; if (v != nil) obj = v->obj; if (obj != nil) dlink = obj->dlink; if ((dlink != nil) && (dlink->n > 0)) { - Data dv; + Array dv; dv.n = dlink->n; dv.p = malloc(dlink->n); memcpy(dv.p, dlink->p, dv.n); @@ -473,7 +478,7 @@ viewadd(Array *views, Object *obj, (Dy(rprev) > obj->font->height) ? Dy(rprev) : obj->font->height))); - vp = arrayadd(views); + vp = arraygrow(views, 1); *vp = (View) { obj, @@ -584,7 +589,7 @@ getfont(Array *fonts, char *name) fprint(2, "%r\n"); newfont = font; } else { - fp = arrayadd(fonts); + fp = arraygrow(fonts, 1); *fp = newfont; } return newfont; @@ -604,7 +609,7 @@ scroll(Point p, Rich *r) } Faux * -fauxalloc(Object *obj, Data *data, int type) +fauxalloc(Object *obj, Array *data, int type) { Faux *aux; aux = mallocz(sizeof(Faux), 1); @@ -620,7 +625,7 @@ newobject(Rich *rich, char *text) Object *obj, **op; qlock(rich->l); - op = arrayadd(rich->objects); + op = arraygrow(rich->objects, 1); if (rich->objects->count > 1) { Object **o1; @@ -633,10 +638,10 @@ newobject(Rich *rich, char *text) *op = obj; - obj->dtext = mallocz(sizeof(Data), 1); - obj->dfont = mallocz(sizeof(Data), 1); - obj->dlink = mallocz(sizeof(Data), 1); - obj->dimage = mallocz(sizeof(Data), 1); + obj->dtext = mallocz(sizeof(Array), 1); + obj->dfont = mallocz(sizeof(Array), 1); + obj->dlink = mallocz(sizeof(Array), 1); + obj->dimage = mallocz(sizeof(Array), 1); if (text != nil) { obj->dtext->p = text; @@ -649,10 +654,13 @@ newobject(Rich *rich, char *text) obj->dlink->p = strdup(""); obj->dimage->p = strdup(""); - obj->id = smprint("%lld", rich->idcount); + obj->id = smprint("%ulld", rich->idcount); obj->font = font; + obj->text = rich->text; + obj->offset = rich->text->count; + rich->idcount++; qunlock(rich->l); @@ -796,15 +804,15 @@ objectfree(void *v) free(op); } -Data * +Array * getseltext(Rich *rich) { long n, nmin, nmax; View *vmin, *vmax; Object **o, **om, *omin, *omax; - Data *d; + Array *d; - d = malloc(sizeof(Data)); + d = malloc(sizeof(Array)); vmin = rich->sel.v[0]; vmax = rich->sel.v[1]; @@ -872,7 +880,7 @@ mpaste(Rich *rich) void msnarf(Rich *rich) { - Data *d; + Array *d; int snarf; long n; n = 0; diff --git a/richterm.h b/richterm.h @@ -1,12 +1,7 @@ void redraw(int); void drawscrollbar(void); -typedef struct Data Data; - -struct Data { - char *p; - long n; -}; +typedef Array Data; typedef struct Object Object; @@ -17,12 +12,15 @@ struct Object { File *flink; File *fimage; char *id; - Data *dtext; - Data *dfont; - Data *dlink; - Data *dimage; + Array *dtext; + Array *dfont; + Array *dlink; + Array *dimage; Font *font; Image *image; + + Array *text; + usize offset; }; extern Object *olast; @@ -63,6 +61,7 @@ struct Rich { QLock *l; Array *objects; Array *views; + Array *text; u64int idcount; Page page; struct { @@ -102,7 +101,7 @@ typedef struct Faux Faux; struct Faux { int type; Object *obj; - Data *data; + Array *data; }; enum { @@ -112,4 +111,4 @@ enum { FT_IMAGE }; -Faux * fauxalloc(Object *, Data *, int); -\ No newline at end of file +Faux * fauxalloc(Object *, Array *, int);