~torresjrjr/public-inbox

ed: fix buffermodified logic v2 SUPERSEDED

Curtis Arthaud: 1
 fix buffermodified logic

 4 files changed, 11 insertions(+), 12 deletions(-)
Btw, this patch does apply succesfully.
Next
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/50259/mbox | git am -3
Learn more about email & git

[PATCH ed v2] fix buffermodified logic Export this patch

Signed-off-by: Curtis Arthaud <uku82@gmx.fr>
---
This should apply.
I must have made a mess with rebase,
now I've redone the patch on a fresh clone.

 buffer.ha  | 15 ++++-----------
 command.ha |  2 +-
 file.ha    |  4 ++++
 main.ha    |  2 ++
 4 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/buffer.ha b/buffer.ha
index 196afa6..a32216f 100644
--- a/buffer.ha
@@ -11,7 +11,6 @@ type Buffer = struct{
	hist: []ChangeSeq,
	cursor: size,
	modified: bool,
	written: bool,
	redolastchange: bool,
};

@@ -34,8 +33,7 @@ fn buf_insert(buf: *Buffer, n: size, ls: *Line...) void = {

	insert(buf.lines[n], ls...);

	if (buf.written)
		buf.modified = true;
	buf.modified = true;

	hist_append(buf, (n, len(ls)): Addition);

@@ -47,8 +45,7 @@ fn buf_deleteall(buf: *Buffer) void = {
		append(buf.trash, buf.lines[1..]...);
		delete(buf.lines[1..]);
	};
	if (buf.written)
		buf.modified = true;
	buf.modified = true;
};

fn buf_delete(buf: *Buffer, a: size, b: size) void = {
@@ -57,8 +54,7 @@ fn buf_delete(buf: *Buffer, a: size, b: size) void = {
	append(buf.trash, buf.lines[a..b+1]...);
	delete(buf.lines[a..b+1]);

	if (buf.written)
		buf.modified = true;
	buf.modified = true;

	hist_append(buf, (a, (b + 1 - a)): Deletion);

@@ -91,8 +87,7 @@ fn buf_read(

	hist_append(buf, (n + 1, m): Addition);

	if (buf.written)
		buf.modified = true;
	buf.modified = true;

	return (sz, m);
};
@@ -102,8 +97,6 @@ fn buf_write(buf: *Buffer, dest: io::handle, a: size, b: size) (size | io::error
	for (let n = a; n <= b; n += 1)
		sz += fmt::fprintln(dest, buf.lines[n].text)?;

	buf.written = true; // TODO: move into cmd_write() ?

	return sz;
};

diff --git a/command.ha b/command.ha
index 544b027..9ba1207 100644
--- a/command.ha
+++ b/command.ha
@@ -667,7 +667,7 @@ fn cmd_write(s: *Session, cmd: *Command) (void | Error) = {
	if (!s.suppressmode)
		fmt::println(sz)!;

	if (a == 1 && b == len(s.buf.lines))
	if (a == 1 && b == len(s.buf.lines)-1)
		// the entire buffer has been written.
		s.buf.modified = false;

diff --git a/file.ha b/file.ha
index e8bab6d..143f6af 100644
--- a/file.ha
+++ b/file.ha
@@ -20,6 +20,10 @@ fn edit(s: *Session, cmd: *Command, forced: bool) (void | Error) = {

	buf_deleteall(s.buf);
	const (sz, _) = buf_read(s.buf, rd, 0)?;
	if (s.newsession) {
		s.buf.modified = false;
		s.newsession = false;
	};

	hist_discardseq(s.buf);

diff --git a/main.ha b/main.ha
index be2094e..2cdc669 100644
--- a/main.ha
+++ b/main.ha
@@ -17,6 +17,7 @@ type Session = struct{
	undomode: UndoMode,

	warned: bool,
	newsession: bool,
	lasterror: (void | Error),
	lastregex: (void | str),
	lastshcmd: (void | str),
@@ -48,6 +49,7 @@ export fn main() void = {
		input = &input,
		buf = &buffer,
		prompt = "*",
		newsession = true,
		lasterror = void,
		lastregex = void,
		lastshcmd = void,
--
2.44.0
I have checked the implementation in https://github.com/slewsys/ed.
They never consider the buffer to be modified after an 'e' command.
If we do the same, we can get rid of the proposed s.newsession field,
and just reset s.buf.modified=false unconditionally inside the edit function.

Thoughts?