Original author: Pierre Sarrazin. Source: http://sarrazip.com/dev/ebitor-0.1.1.tar.gz Archive SHA-256: 6911a9d2ee80925622f1d82db1b77710a1f896865e63d29a04dc16489b150e83
110 lines
4.2 KiB
C
110 lines
4.2 KiB
C
/* processBackspace.c
|
|
|
|
Copyright (C) 2021-2025 Pierre Sarrazin <http://sarrazip.com/>
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "Ebitor_private.h"
|
|
|
|
|
|
void
|
|
Ebitor_processBackspace(byte cursorCol, byte cursorRow)
|
|
{
|
|
Ebitor_startCommandMode();
|
|
|
|
// This check must be done after Ebitor_startCommandMode(), which can update cursorOffset
|
|
if (Ebitor_instance.cursorOffset == 0) // if at start of edit buffer
|
|
return; // nothing to do
|
|
|
|
size_t solOffset = Ebitor_findSOL(Ebitor_instance.cursorOffset);
|
|
size_t eolOffset = Ebitor_findEOL(Ebitor_instance.cursorOffset);
|
|
|
|
// E.g.: ...ABCDEF..............FOOBAR
|
|
// ^ ^ ^ ^bufferFilledSize
|
|
// | | `eolOffset
|
|
// | `cursorOffset
|
|
// `solOffset
|
|
|
|
// Print rest of line at preceding position.
|
|
//
|
|
BOOL erasingPrevCR = (cursorCol == 0);
|
|
if (!erasingPrevCR)
|
|
{
|
|
--cursorCol;
|
|
}
|
|
else // at column 0:
|
|
{
|
|
if (Ebitor_instance.cursorOffset == solOffset) // if at start of logical line
|
|
{
|
|
// About to erase previous line's terminator.
|
|
assert(Ebitor_instance.bufferBegin[solOffset - 1] == '\r'); // b/c cursorOffset > 0, there must be a line before
|
|
|
|
size_t prevSOLOffset = Ebitor_findPreviousSOL(solOffset);
|
|
assert(prevSOLOffset < solOffset);
|
|
size_t prevLineLen = solOffset - prevSOLOffset - 1; // exclude \r
|
|
cursorCol = (byte) (prevLineLen % Ebitor_instance.termWidth); // after last char of previous line
|
|
if (cursorRow == 0)
|
|
{
|
|
Ebitor_instance.scrollTerminalDown(1);
|
|
Ebitor_instance.printPrefix(Ebitor_instance.bufferBegin + (solOffset - 1 - prevLineLen), prevLineLen);
|
|
}
|
|
else
|
|
--cursorRow;
|
|
}
|
|
else // inside a logical line:
|
|
{
|
|
cursorCol = Ebitor_instance.termWidth - 1;
|
|
if (cursorRow == 0)
|
|
{
|
|
Ebitor_instance.scrollTerminalDown(1);
|
|
Ebitor_instance.printPrefix(Ebitor_instance.bufferBegin + (Ebitor_instance.cursorOffset - Ebitor_instance.termWidth), Ebitor_instance.termWidth);
|
|
}
|
|
else
|
|
--cursorRow;
|
|
}
|
|
}
|
|
assert(cursorRow < Ebitor_instance.termHeight);
|
|
Ebitor_instance.locate(cursorCol, cursorRow);
|
|
char *cursorPtr = Ebitor_instance.bufferBegin + Ebitor_instance.cursorOffset;
|
|
assert(Ebitor_instance.cursorOffset <= eolOffset);
|
|
size_t numCharsToPrint = eolOffset - Ebitor_instance.cursorOffset;
|
|
if (Ebitor_instance.printPrefix(cursorPtr, numCharsToPrint))
|
|
{
|
|
// We have not reached the end of the window.
|
|
|
|
Ebitor_instance.clrtoeol(); // erase last char of logical line
|
|
|
|
if (erasingPrevCR)
|
|
{
|
|
// Print the lines that follow the cursor row.
|
|
// This will make a new line visible at the bottom, if the text is long enough.
|
|
// TODO: This might be optimized by only writing the bottom row.
|
|
//
|
|
size_t firstLogicalCharOffsetToPrint = (eolOffset == Ebitor_instance.bufferFilledSize ? eolOffset : eolOffset + 1);
|
|
|
|
byte dummy, r;
|
|
Ebitor_instance.getCursorPos(&dummy, &r);
|
|
|
|
Ebitor_fillTerminalToBottom(firstLogicalCharOffsetToPrint, r + 1);
|
|
}
|
|
}
|
|
|
|
Ebitor_instance.locate(cursorCol, cursorRow);
|
|
|
|
memmove(cursorPtr - 1, cursorPtr, Ebitor_instance.bufferFilledSize - Ebitor_instance.cursorOffset);
|
|
--Ebitor_instance.cursorOffset;
|
|
--Ebitor_instance.bufferFilledSize;
|
|
}
|