shithub: riscv

Download patch

ref: 709e78b9f9a76658cc6c704176ebf8f5821ee9d2
parent: 98d518cc8c1ff7d1440ebffb9f546d3e0e149d60
author: aiju <devnull@localhost>
date: Thu Jul 31 07:36:24 EDT 2014

spred: scrolling and other bugfixes

--- a/sys/src/cmd/spred/cmd.c
+++ b/sys/src/cmd/spred/cmd.c
@@ -19,6 +19,7 @@
 	if(p == nil){
 		cmdprint("?%r\n");
 		p = newpal(argv[1]);
+		palsize(p, 8, 0);
 	}
 	if(newwinsel(PAL, mc, p) == nil){
 		if(p->ref == 0)
@@ -40,7 +41,7 @@
 		n = strtol(argv[1], &p, 0);
 		if(*p != 0 || n < 0)
 			goto err;
-		palsize((Pal *) actf->f, n);
+		palsize((Pal *) actf->f, n, 1);
 		return;
 	case SPR:
 		n = strtol(argv[1], &p, 0);
@@ -49,7 +50,7 @@
 		m = strtol(++p, &p, 0);
 		if(*p != 0 || m < 0)
 			goto err;
-		sprsize((Spr *) actf->f, n, m);
+		sprsize((Spr *) actf->f, n, m, 1);
 		return;
 	}
 err:
@@ -105,11 +106,14 @@
 	
 	s = newspr(argv[1]);
 	bp = Bopen(argv[1], OREAD);
-	if(bp == nil)
+	if(bp == nil){
 		cmdprint("?%r\n");
-	else{
-		if(readspr(s, bp) < 0)
+		sprsize(s, 8, 8, 0);
+	}else{
+		if(readspr(s, bp) < 0){
 			cmdprint("?%r\n");
+			sprsize(s, 8, 8, 0);
+		}
 		Bterm(bp);
 	}
 	w = newwinsel(SPR, mc, s);
--- a/sys/src/cmd/spred/cmdw.c
+++ b/sys/src/cmd/spred/cmdw.c
@@ -4,6 +4,7 @@
 #include <thread.h>
 #include <draw.h>
 #include <mouse.h>
+#include <keyboard.h>
 #include <frame.h>
 #include "dat.h"
 #include "fns.h"
@@ -57,15 +58,18 @@
 		for(r = w->toprune; r < w->nrunes && l != 0; r++)
 			if(w->runes[r] == '\n')
 				l--;
-		frdelete(&w->fr, 0, r - w->toprune);
 		w->toprune = r;
 	}else{
 		for(r = w->toprune; r > 0; r--)
-			if(w->runes[r] == '\n' && --l == 0)
+			if(w->runes[r] == '\n' && ++l == 0){
+				r++;
 				break;
-		frinsert(&w->fr, w->runes + r, w->runes + w->toprune, 0);
+			}
 		w->toprune = r;
+	
 	}
+	frdelete(&w->fr, 0, w->fr.nchars);
+		frinsert(&w->fr, w->runes + w->toprune, w->runes + w->nrunes, 0);
 	scrollbar(w);
 }
 
@@ -88,7 +92,7 @@
 	return 0;
 }
 
-void
+int
 cmdinsert(Win *w, Rune *r, int nr, int rp)
 {
 	Rune *s;
@@ -116,6 +120,7 @@
 				cmdscroll(w, 1);
 		}
 	}
+	return nr;
 }
 
 static void
@@ -148,24 +153,27 @@
 	case 0x1b:
 		break;
 	case '\b':
-		if(w->fr.p0 > 0)
+		if(w->fr.p0 > 0 && w->toprune + w->fr.p0 != w->opoint)
 			cmddel(w, w->toprune + w->fr.p0 - 1, w->toprune + w->fr.p0);
 		break;
 	case '\n':
 		cmdinsert(w, &r, 1, w->fr.p0 + w->toprune);
 		if(w->toprune + w->fr.p0 == w->nrunes){
-			q = w->runes + w->toprune + w->fr.p0 - 1;
+			q = w->runes + w->opoint;
 			p = buf;
-			while(*--q != 0xa && q > w->runes)
-				;
-			if(*q == 0xa)
-				q++;
-			while(q < w->runes + w->nrunes && p < buf + nelem(buf) + 1 && *q != 0xa)
+			while(q < w->runes + w->nrunes && p < buf + nelem(buf) + 1)
 				p += runetochar(p, q++);
 			*p = 0;
+			w->opoint = w->nrunes;
 			docmd(buf);
 		}
 		break;
+	case Kview:
+		cmdscroll(w, 3);
+		break;
+	case Kup:
+		cmdscroll(w, -3);
+		break;
 	default:
 		cmdinsert(w, &r, 1, w->fr.p0 + w->toprune);
 	}
@@ -181,7 +189,7 @@
 	r = runevsmprint(fmt, va);
 	va_end(va);
 	if(r != nil)
-		cmdinsert(cmdw, r, -1, -1);
+		cmdw->opoint += cmdinsert(cmdw, r, -1, cmdw->opoint);
 }
 
 Wintab cmdtab = {
--- a/sys/src/cmd/spred/dat.h
+++ b/sys/src/cmd/spred/dat.h
@@ -50,7 +50,7 @@
 	
 	Frame fr;
 	Rune *runes;
-	int nrunes, arunes;
+	int nrunes, arunes, opoint;
 	int toprune;
 	
 	int zoom;
--- a/sys/src/cmd/spred/fil.c
+++ b/sys/src/cmd/spred/fil.c
@@ -155,3 +155,10 @@
 	for(w = f->wins.wnext; w != &f->wins; w = w->wnext)
 		w->tab->draw(w);
 }
+
+void
+change(File *f)
+{
+	f->change = 1;
+	quitok = 0;
+}
--- a/sys/src/cmd/spred/fns.h
+++ b/sys/src/cmd/spred/fns.h
@@ -1,3 +1,4 @@
+void	change(File *);
 void	cmdprint(char *, ...);
 void	docmd(char *);
 void*	emalloc(ulong);
@@ -15,7 +16,7 @@
 Win*	newwinsel(int, Mousectl *, File *);
 void	paldraw(Win *);
 void	palset(Pal *, int, u32int);
-void	palsize(Pal *, int);
+void	palsize(Pal *, int, int);
 void	putfil(File *);
 void	putident(Ident);
 void	putpal(Pal *);
@@ -25,7 +26,7 @@
 int	readspr(Spr *, Biobuf *);
 void	resize(void);
 void	setfocus(Win *);
-void	sprsize(Spr *, int, int);
+void	sprsize(Spr *, int, int, int);
 int	tline(Biobuf *, char **, char **, int);
 void	winclick(Mousectl *);
 void	winclose(Win *);
--- a/sys/src/cmd/spred/pal.c
+++ b/sys/src/cmd/spred/pal.c
@@ -177,7 +177,7 @@
 }
 
 void
-palsize(Pal *p, int sz)
+palsize(Pal *p, int sz, int ch)
 {
 	int i;
 
@@ -185,14 +185,14 @@
 		return;
 	p->cols = realloc(p->cols, sz * sizeof(*p->cols));
 	p->ims = realloc(p->ims, sz * sizeof(*p->ims));
-	if(sz > p->ncol){
-		memset(p->cols + p->ncol, 0, sz);
-		for(i = p->ncol; i < sz; i++)
+	if(sz > p->ncol)
+		for(i = p->ncol; i < sz; i++){
+			p->cols[i] = 0;
 			p->ims[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0);
-	}
+		}
 	p->ncol = sz;
-	p->change = 1;
-	quitok = 0;
+	if(ch)
+		change(p);
 	palredraw(p);
 }
 
@@ -226,8 +226,7 @@
 	p->cols[s] = c;
 	freeimage(p->ims[s]);
 	p->ims[s] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, c << 8 | 0xff);
-	p->change = 1;
-	quitok = 0;
+	change(p);
 	palredraw(p);
 }
 
--- a/sys/src/cmd/spred/spr.c
+++ b/sys/src/cmd/spred/spr.c
@@ -258,8 +258,7 @@
 			continue;
 		if(s->data[q.y * s->w + q.x] != p->sel){
 			s->data[q.y * s->w + q.x] = p->sel;
-			s->change = 1;
-			quitok = 0;
+			change(s);
 			sprdraw(w);
 		}
 	}while(readmouse(mc) >= 0 && (mc->buttons & 1) != 0);
@@ -266,7 +265,7 @@
 }
 
 void
-sprsize(Spr *s, int n, int m)
+sprsize(Spr *s, int n, int m, int ch)
 {
 	u32int *v;
 	int i, j, w, h;
@@ -282,8 +281,8 @@
 			s->data[j * n + i] = v[j * w + i];
 	s->w = n;
 	s->h = m;
-	s->change = 1;
-	quitok = 0;
+	if(ch)
+		change(s);
 	filredraw(s);
 }
 
@@ -322,8 +321,8 @@
 			s->pal = (Pal *) wp->f;
 			free(s->palfile);
 			s->palfile = palfile(s->name, s->pal->name);
-			s->change = 1;
-			quitok = 0;
+			cmdprint("palette set to %q\n", s->palfile);
+			change(s);
 			filredraw(s);
 		}
 		break;