shithub: pplay

Download patch

ref: b538a7f7e8552f9e190bf1746361cbe6d575977a
parent: 3cc271e1e7ba83030e354802edd077156dbfcd0c
author: qwx <qwx@sciops.net>
date: Mon Nov 21 22:45:28 EST 2022

full monty pcm editing, sans undo

- fix insert/pipefrom
- just count samples all over again when there are changes to avoid more bugs
- setrange: constrain dot.pos

--- a/cmd.c
+++ b/cmd.c
@@ -65,6 +65,16 @@
 }
 
 static void
+recalcsize(void)
+{
+	Chunk *c;
+
+	totalsz = 0;
+	for(c=norris.right; c!=&norris; c=c->right)
+		totalsz += c->bufsz;
+}
+
+static void
 linkchunk(Chunk *left, Chunk *c)
 {
 	c->left->right = left->right;
@@ -117,6 +127,8 @@
 {
 	dot.from.pos = from;
 	dot.to.pos = to;
+	if(dot.pos < from || dot.pos >= to)
+		dot.pos = from;
 }
 
 int
@@ -290,26 +302,6 @@
 }
 
 static int
-replace(char *, Chunk *c)
-{
-	Chunk *l, *dotc;
-
-	if(c == nil && (c = clonechunk()) == nil){
-		werrstr("replace: no buffer");
-		return -1;
-	}
-	dotc = p2c(dot.pos, nil);
-	l = dotc->left;
-	totalsz -= dotc->bufsz;
-	unlinkchunk(dotc);
-	freechunk(dotc);
-	linkchunk(l, c);
-	setrange(dot.from.pos, dot.from.pos + c->bufsz);
-	totalsz += c->bufsz;
-	return 1;
-}
-
-static int
 insert(char *, Chunk *c)
 {
 	usize p;
@@ -322,20 +314,12 @@
 	left = p2c(dot.pos, &p);
 	splitright(left, p);
 	linkchunk(left, c);
+	recalcsize();
 	setrange(dot.pos, dot.pos + c->bufsz);
-	totalsz += c->bufsz;
 	return 1;
 }
 
 static int
-paste(char *s, Chunk *c)
-{
-	if(dot.from.pos == 0 && dot.to.pos == totalsz)
-		return insert(s, c);
-	return replace(s, c);
-}
-
-static int
 copy(char *)
 {
 	Chunk *c;
@@ -351,12 +335,40 @@
 	Chunk *c;
 
 	c = splitdot();
-	totalsz -= c->bufsz;
+	recalcsize();
 	holdchunk(c, 1);
 	return 1;
 }
 
 static int
+replace(char *, Chunk *c)
+{
+	Chunk *left, *right;
+
+	if(c == nil && (c = clonechunk()) == nil){
+		werrstr("replace: no buffer");
+		return -1;
+	}
+	right = splitdot();
+	left = right->left;
+	unlinkchunk(right);
+	freechunk(right);
+	right = left->right;
+	linkchunk(left, c);
+	recalcsize();
+	setrange(dot.from.pos, right != &norris ? c2p(right) : totalsz);
+	return 1;
+}
+
+static int
+paste(char *s, Chunk *c)
+{
+	if(dot.from.pos == 0 && dot.to.pos == totalsz)
+		return insert(s, c);
+	return replace(s, c);
+}
+
+static int
 crop(char *)
 {
 	usize Δ;
@@ -373,7 +385,6 @@
 	}
 	dot.from.pos -= Δ;
 	dot.to.pos -= Δ;
-	totalsz -= Δ;
 	if(dot.from.pos > 0){
 		Δ = c->bufsz - dot.from.pos;
 		memmove(c->buf, c->buf + dot.from.pos, Δ);
@@ -380,7 +391,6 @@
 		erealloc(c->buf, Δ, c->bufsz);
 		c->bufsz = Δ;
 		dot.to.pos -= dot.from.pos;
-		totalsz -= dot.from.pos;
 		dot.from.pos = 0;
 	}
 	for(Δ=0; c!=&norris; Δ+=c->bufsz, c=c->right)
@@ -387,16 +397,15 @@
 		if(Δ + c->bufsz >= dot.to.pos)
 			break;
 	if(dot.to.pos > 0){
-		totalsz -= c->bufsz - dot.to.pos;
 		erealloc(c->buf, dot.to.pos, c->bufsz);
 		c->bufsz = dot.to.pos;
 	}
 	for(c=c->right; c!=&norris; c=d){
 		d = c->right;
-		totalsz -= c->bufsz;
 		unlinkchunk(c);
 		freechunk(c);
 	}
+	recalcsize();
 	dot.pos = 0;
 	dot.to.pos = totalsz;
 	return 1;
@@ -439,7 +448,6 @@
 	}
 	c->buf = erealloc(c->buf, off, c->bufsz);
 	c->bufsz = off;
-	totalsz += m;
 	return rc;
 }
 
@@ -511,7 +519,7 @@
 		paste(nil, c);
 	}
 	close(epfd[0]);
-	return 0;
+	return 1;
 }
 
 static int
@@ -596,6 +604,7 @@
 	if((c = readintochunks(fd)) == nil)
 		sysfatal("loadin: %r");
 	linkchunk(&norris, c);
+	recalcsize();
 	setrange(0, totalsz);
 	return 0;
 }