~torresjrjr/public-inbox

ed: history: restore cursor v1 APPLIED

Curtis Arthaud: 1
 history: restore cursor

 1 files changed, 7 insertions(+), 1 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~torresjrjr/public-inbox/patches/50330/mbox | git am -3
Learn more about email & git

[PATCH ed] history: restore cursor Export this patch

Signed-off-by: Curtis Arthaud <uku82@gmx.fr>
---
 history.ha | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/history.ha b/history.ha
index 5cd3f1c..ea109a2 100644
--- a/history.ha
+++ b/history.ha
@@ -65,11 +65,14 @@ fn hist_undo(buf: *Buffer) (void | NoHistory) = {
			let lines = buf.trash[(lentrash - m)..];
			insert(buf.lines[n], lines...);
			delete(buf.trash[(lentrash - m)..]);
			buf.cursor = (n + m) - 1;
		case let addn: Addition =>
			let (n, m) = addn;
			let lines = buf.lines[n..(n + m)];
			insert(buf.trash[0], lines...);
			delete(buf.lines[n..(n + m)]);
			let prev = addr_prevline(buf, n);
			buf.cursor = prev;
		};
};

@@ -84,16 +87,19 @@ fn hist_redo(buf: *Buffer) (void | NoHistory) = {
		match (seq[i]) {
		case let addn: Addition =>
			let (n, m) = addn;
			let lentrash = len(buf.trash);
			//let lentrash = len(buf.trash);
			//let lines = buf.trash[(lentrash - m)..];
			let lines = buf.trash[..m];
			insert(buf.lines[n], lines...);
			//delete(buf.trash[(lentrash - m)..]);
			delete(buf.trash[..m]);
			buf.cursor = (n + m) - 1;
		case let deln: Deletion =>
			let (n, m) = deln;
			let lines = buf.lines[n..(n + m)];
			append(buf.trash, lines...);
			delete(buf.lines[n..(n + m)]);
			let prev = addr_prevline(buf, n);
			buf.cursor = prev;
		};
};
--
2.44.0
So I assume you came across something like this:

	$ ./ed file
	H
	P
	*,n
	1	hello world
	*a
	foo
	bar
	baz
	.
	*u
	*n
	?
	Invalid address
	*

Interest bug!

This is actually a lack of an implementation of the undo command.
ed(1p):
-%<-
That means a proper implementation would have to add a third "previous
cursor position" slot to the Addition and Deletion tuples, record those
throughout buffer.ha, and apply them in the undo & redo functions. That
would actually "restore the cursor" as your patch title says.

Seems here you've just had the cursor moved to a sensible place.

On Wed Mar 20, 2024 at 12:09 PM GMT, Curtis Arthaud wrote: