summaryrefslogtreecommitdiff
path: root/src/buffer_utils.cc
diff options
context:
space:
mode:
authorJohannes Altmanninger <aclopte@gmail.com>2024-01-28 18:23:08 +0100
committerMaxime Coste <mawww@kakoune.org>2024-01-30 08:24:27 +1100
commit582c3c56b218b9be9745efc69c2eb282bbe99b67 (patch)
treec4c82fc177193886074529cfd5d307f18a3bac6c /src/buffer_utils.cc
parentfdbad4d9b2183e03d3cc9a9d71b1055a49350331 (diff)
Do not add trailing newline to non-scrolling fifo buffers
Internally, all lines have a trailing "\n". Buffers created empty (like fifo buffers) start with a single line. When reading data into fifo buffers, we insert *before* the last line's trailing newline ("last newline"). This enables autoscrolling (enabled with "edit -scroll") as long as the cursor is on the last newline. When autoscrolling is disabled, we have a special case to insert *after* the last newline. This means that a cursor on that newline won't be moved. Then we transplant the newline character from the beginning to the end of the buffer. This special case happens only on the very first fifo read; on subsequent reads, the cursor at position 1.1 will not be moved anway because insertions happen below 1.1. Since we always insert (effectively) before the last newline, fifo buffers have a trailing empty line. For autoscrolling buffers this seems correct; it gives users an obvious way to toggle autoscrolling. For non-scrolling buffers the newline is redundant. Remove it. This requires keeping track of whether the last newline comes from the fifo, or was added by us. The shortest fix I could find is to always append to the buffer if not scrolling, and then delete the added newline character if applicable. m_buffer.insert(m_scroll ? pos : m_buffer.next(pos), StringView(data, data+count)); if (not m_scroll and not m_had_trailing_newline) m_buffer.erase(pos, m_buffer.next(pos)); maybe that's the best fix overall; but erasing at the end seems better than erasing in the middle, so do that whenever possible. Reported in https://lists.sr.ht/~mawww/kakoune/%3CZbTK7qit9nzvrMkx@gmail.com%3E
Diffstat (limited to 'src/buffer_utils.cc')
-rw-r--r--src/buffer_utils.cc22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc
index 6de07c71..21ec7092 100644
--- a/src/buffer_utils.cc
+++ b/src/buffer_utils.cc
@@ -205,7 +205,7 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
m_buffer.flags() &= ~(Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
}
- void read_fifo() const
+ void read_fifo()
{
kak_assert(m_buffer.flags() & Buffer::Flags::Fifo);
@@ -234,20 +234,21 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
}
auto pos = m_buffer.back_coord();
- const bool prevent_scrolling = pos == BufferCoord{0,0} and not m_scroll;
- if (prevent_scrolling)
+ const bool is_first = pos == BufferCoord{0,0};
+ if (not m_scroll and (is_first or m_had_trailing_newline))
pos = m_buffer.next(pos);
- m_buffer.insert(pos, StringView(data, data+count));
+ pos = m_buffer.insert(pos, StringView(data, data+count)).end;
- if (prevent_scrolling)
+ bool have_trailing_newline = (data[count-1] == '\n');
+ if (not m_scroll)
{
- m_buffer.erase({0,0}, m_buffer.next({0,0}));
- // in the other case, the buffer will have automatically
- // inserted a \n to guarantee its invariant.
- if (data[count-1] == '\n')
- m_buffer.insert(m_buffer.end_coord(), "\n");
+ if (is_first)
+ m_buffer.erase({0,0}, m_buffer.next({0,0}));
+ else if (not m_had_trailing_newline and have_trailing_newline)
+ m_buffer.erase(pos, m_buffer.next(pos));
}
+ m_had_trailing_newline = have_trailing_newline;
}
while (++loop < max_loop and fd_readable(fifo));
}
@@ -263,6 +264,7 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
Buffer& m_buffer;
bool m_scroll;
+ bool m_had_trailing_newline;
};
buffer->values()[fifo_watcher_id] = Value(Meta::Type<FifoWatcher>{}, fd, *buffer, scroll);