~torresjrjr/public-inbox

ed: fix buffermodified logic v1 SUPERSEDED

Curtis Arthaud: 1
 fix buffermodified logic

 4 files changed, 18 insertions(+), 12 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/50253/mbox | git am -3
Learn more about email & git

[PATCH ed] fix buffermodified logic Export this patch

Signed-off-by: Curtis Arthaud <uku82@gmx.fr>
---
This seems to fix the quit bugs
(there was a second one,
quitting on a dirty buffer wasn't prevented if the buffer wasn't
associated to a filename).

I left the debugs in to make the review easier.

I had to introduce a new s.newssion field.
Not sure if there's a better solution.
It's needed because otherwise the buffer is marked as modified as soon
as we open a new file.
That's good for subsequent edit commands, but not for the first one.

I removed the buf.written logic because IMHO it's incorrect:
we always want to mark the buffer as modified on insert/delete
operations, it doesn't matter if it had been written *before*.
 buffer.ha  | 19 ++++++++-----------
 command.ha |  4 +++-
 file.ha    |  5 +++++
 main.ha    |  2 ++
 4 files changed, 18 insertions(+), 12 deletions(-)

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

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

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

	if (buf.written)
		buf.modified = true;
	buf.modified = true;
	debug("buf_insert: set buf.modified=true");

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

@@ -47,8 +46,8 @@ 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;
	debug("buf_deleteall: set buf.modified=true");
};

fn buf_delete(buf: *Buffer, a: size, b: size) void = {
@@ -57,8 +56,8 @@ 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;
	debug("buf_delete: set buf.modified=true");

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

@@ -91,8 +90,8 @@ fn buf_read(

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

	if (buf.written)
		buf.modified = true;
	buf.modified = true;
	debug("buf_read: set buf.modified=true");

	return (sz, m);
};
@@ -102,8 +101,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 6b00a0e..fa3eb94 100644
--- a/command.ha
+++ b/command.ha
@@ -725,9 +725,11 @@ 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;
		debug("buf_write: set buf.modified=false");
	};

	if (cmd.suffix == 'q') {
		if (s.buf.modified && !s.warned) {
diff --git a/file.ha b/file.ha
index 7c971a3..ad1b6af 100644
--- a/file.ha
+++ b/file.ha
@@ -22,6 +22,11 @@ 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;
		debug("edit: set s.buf.modified=false on s.newsession");
		s.newsession = false;
	};

	hist_discardseq(s.buf);

diff --git a/main.ha b/main.ha
index a7bf2bb..72ebfb3 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