From b1813ca864ed04f28b8ae0121c913fbe16aab8ee Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 18 Jul 2024 15:01:22 +0100 Subject: ed: fix line insertion before current line When text is inserted by insertLine() the lines following the insertion are moved down and the insertion point is made the new current line. To avoid too much scanning of the linked list of lines setCurNum() may use the position of the old current line to determine the location of the new current line. If the insertion point is before the old current line in the file the latter will have been moved down, so its line pointer needs to be adjusted. (GitHub issue #431) --- editors/ed.c | 4 ++++ testsuite/ed.tests | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100755 testsuite/ed.tests diff --git a/editors/ed.c b/editors/ed.c index 8ec23d07f..e25179ddc 100644 --- a/editors/ed.c +++ b/editors/ed.c @@ -345,6 +345,10 @@ static int insertLine(int num, const char *data, int len) lp->prev->next = newLp; lp->prev = newLp; +#if ENABLE_PLATFORM_MINGW32 + if (num <= curNum) + curLine = curLine->prev; +#endif lastNum++; dirty = TRUE; return setCurNum(num); diff --git a/testsuite/ed.tests b/testsuite/ed.tests new file mode 100755 index 000000000..475fdc244 --- /dev/null +++ b/testsuite/ed.tests @@ -0,0 +1,21 @@ +#!/bin/sh + +. ./testing.sh + +# testing "test name" "command" "expected result" "file input" "stdin" + +testing "ed insert text before current line" \ + "ed input" "8\n1\ntext\n2\n3\n4\n13\n" "1\n2\n3\n4\n" '2i +text +. +,p +w +q +' + +testing "ed read text before current line" \ + "ed input" "8\n8\n1\n2\n1\n2\n3\n4\n3\n4\n16\n" "1\n2\n3\n4\n" '2r input +,p +w +q +' -- cgit v1.2.3-55-g6feb