ref: 69d4e9e10a45517ad32d399bfbf61390d5a43208
dir: /samterm/rasp.c/
#include <u.h> #include <libc.h> #include <draw.h> #include <thread.h> #include <mouse.h> #include <keyboard.h> #include <frame.h> #include "flayer.h" #include "samterm.h" void rinit(Rasp *r) { r->nrunes = 0; r->sect = 0; } void rclear(Rasp *r) { Section *s, *t; for(s = r->sect; s; s = t){ t = s->next; free(s->text); free(s); } r->sect = nil; } /* * Insert a new section t before s */ Section* rsinsert(Rasp *r, Section *s) { Section *t, *u; t = alloc(sizeof(Section)); if(r->sect == s) r->sect = t; else for(u = r->sect; u; u = u->next) if(u->next == s){ u->next = t; break; } t->next = s; return t; } void rsdelete(Rasp *r, Section *s) { Section *t; if(!s) panic("rsdelete"); if(r->sect == s) r->sect = s->next; else for(t = r->sect; t; t = t->next) if(t->next == s){ t->next = s->next; break; } free(s->text); free(s); } void splitsect(Rasp *r, Section *s, long n) { if(!s) panic("splitsect"); rsinsert(r, s->next); if(!s->text) s->next->text = nil; else{ s->next->text = alloc(RUNESIZE*(TBLOCKSIZE+1)); runestrcpy(s->next->text, s->text+n); s->text[n] = 0; } s->next->nrunes = s->nrunes - n; s->nrunes = n; } /* * Find the sect containing q and put q on a sect boundary */ Section* findsect(Rasp *r, Section *s, long p, long q) { if(!s && p != q) panic("findsect"); while(s && p + s->nrunes <= q){ p += s->nrunes; s = s->next; } if(p != q){ splitsect(r, s, q-p); s = s->next; } return s; } void rresize(Rasp *r, long a, long old, long new) { Section *s, *t, *ns; s = findsect(r, r->sect, 0L, a); t = findsect(r, s, a, a+old); for(; s!=t; s=ns){ ns = s->next; rsdelete(r, s); } /* now insert the new piece before t */ if(new > 0){ ns = rsinsert(r, t); ns->nrunes=new; ns->text=0; } r->nrunes += new-old; } void rdata(Rasp *r, long p0, long p1, Rune *cp) { Section *s, *t, *ns; s = findsect(r, r->sect, 0L, p0); t = findsect(r, s, p0, p1); while(s != t){ ns = s->next; if(s->text) panic("rdata"); rsdelete(r, s); s = ns; } p1 -= p0; s = rsinsert(r, t); s->text = alloc(RUNESIZE*(TBLOCKSIZE+1)); memmove(s->text, cp, RUNESIZE*p1); s->text[p1] = 0; s->nrunes = p1; } void rclean(Rasp *r) { Section *s; for(s=r->sect; s; s=s->next) while(s->next && (s->text!=0)==(s->next->text!=0)){ if(s->text){ if(s->nrunes+s->next->nrunes>TBLOCKSIZE) break; runestrcpy(s->text+s->nrunes, s->next->text); } s->nrunes += s->next->nrunes; rsdelete(r, s->next); } } Rune* rload(Rasp *r, ulong p0, ulong p1, ulong *nrp) { Section *s; long p; int n, nb; nb = 0; Strgrow(&scratch, &nscralloc, p1-p0+1); scratch[0] = 0; for(p=0,s=r->sect; s && p+s->nrunes<=p0; s=s->next) p += s->nrunes; while(p<p1 && s){ /* * Subtle and important. If we are preparing to handle an 'rdata' * call, it's because we have an 'rresize' hole here, so the * screen doesn't have data for that space anyway (it got cut * first). So pretend it isn't there. */ if(s->text){ n = s->nrunes-(p0-p); if(n>p1-p0) /* all in this section */ n = p1-p0; memmove(scratch+nb, s->text+(p0-p), n*RUNESIZE); nb += n; scratch[nb] = 0; } p += s->nrunes; p0 = p; s = s->next; } if(nrp) *nrp = nb; return scratch; } int rmissing(Rasp *r, ulong p0, ulong p1) { Section *s; long p; int n, nm; nm = p = 0; for(s = r->sect; s && p+s->nrunes <= p0; s = s->next) p += s->nrunes; while(p < p1 && s){ if(!s->text){ n = s->nrunes - (p0-p); if(n > p1-p0) /* all in this section */ n = p1-p0; nm += n; } p += s->nrunes; p0 = p; s = s->next; } return nm; } int rcontig(Rasp *r, ulong p0, ulong p1, int text) { Section *s; long p, n; int np; np = p = 0; for(s = r->sect; s && p+s->nrunes <= p0; s = s->next) p += s->nrunes; while(p < p1 && s && (text? s->text!=0: s->text==0)){ n = s->nrunes-(p0-p); if(n > p1-p0) /* all in this section */ n = p1-p0; np += n; p += s->nrunes; p0 = p; s = s->next; } return np; } void Strgrow(Rune **s, long *n, int want) /* can always toss the old data when called */ { if(*n >= want) return; free(*s); *s = alloc(RUNESIZE*want); *n = want; }