Original author: Pierre Sarrazin. Source: http://sarrazip.com/dev/ebitor-0.1.1.tar.gz Archive SHA-256: 6911a9d2ee80925622f1d82db1b77710a1f896865e63d29a04dc16489b150e83
940 lines
30 KiB
C
940 lines
30 KiB
C
/* Ebitor.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/>.
|
|
*/
|
|
|
|
/*
|
|
This file uses the terms "terminal" and "window" interchangeably.
|
|
*/
|
|
|
|
#include "Ebitor_private.h"
|
|
|
|
|
|
enum
|
|
{
|
|
MODE_COMMAND,
|
|
MODE_INSERTION,
|
|
};
|
|
|
|
|
|
Ebitor Ebitor_instance;
|
|
|
|
|
|
void
|
|
Ebitor_init(char *bufferBegin, size_t bufferSize,
|
|
byte termWidth, byte termHeight,
|
|
ebitor_clear clear,
|
|
ebitor_locateFunc locate,
|
|
ebitor_getCursorPosFunc getCursorPos,
|
|
ebitor_printFunc print,
|
|
ebitor_printPrefixFunc printPrefix,
|
|
ebitor_clrtoeolFunc clrtoeol,
|
|
ebitor_scrollTerminalUpFunc scrollTerminalUp,
|
|
ebitor_scrollTerminalDownFunc scrollTerminalDown,
|
|
ebitor_waitKeyFunc waitKey)
|
|
{
|
|
Ebitor *self = &Ebitor_instance;
|
|
self->termWidth = termWidth;
|
|
self->termHeight = termHeight;
|
|
self->bufferBegin = bufferBegin;
|
|
self->bufferEnd = bufferBegin + bufferSize;
|
|
self->bufferFilledSize = 0;
|
|
self->cursorOffset = 0;
|
|
self->savedCursorCol = 0;
|
|
self->savedCursorRow = 0;
|
|
self->mode = MODE_COMMAND;
|
|
self->insertBuffer.size = 0;
|
|
|
|
self->clear = clear;
|
|
self->locate = locate;
|
|
self->getCursorPos = getCursorPos;
|
|
self->print = print;
|
|
self->printPrefix = printPrefix;
|
|
self->clrtoeol = clrtoeol;
|
|
self->scrollTerminalUp = scrollTerminalUp;
|
|
self->scrollTerminalDown = scrollTerminalDown;
|
|
self->waitKey = waitKey;
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_shutdown(void)
|
|
{
|
|
}
|
|
|
|
|
|
Ebitor *
|
|
Ebitor_getInstance(void)
|
|
{
|
|
return &Ebitor_instance;
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_setContentLength(size_t newLength)
|
|
{
|
|
size_t max = Ebitor_instance.bufferEnd - Ebitor_instance.bufferBegin;
|
|
Ebitor_instance.bufferFilledSize = min(newLength, max);
|
|
|
|
Ebitor_setLogicalCursorAtBeginning();
|
|
|
|
Ebitor_instance.mode = MODE_COMMAND;
|
|
Ebitor_instance.insertBuffer.size = 0;
|
|
}
|
|
|
|
|
|
size_t
|
|
Ebitor_getContentLength(void)
|
|
{
|
|
return Ebitor_instance.bufferFilledSize;
|
|
}
|
|
|
|
|
|
// Move the contents of insert buffer to the cursor position in the edit buffer.
|
|
// Insert buffer ends up empty.
|
|
// Cursor offset updated to point to after the copied characters.
|
|
//
|
|
void
|
|
Ebitor_flushInsertBuffer(void)
|
|
{
|
|
Ebitor *self = &Ebitor_instance;
|
|
if (self->insertBuffer.size == 0)
|
|
return;
|
|
|
|
// Check if there is room in the edit buffer for the contents of the insert buffer.
|
|
//
|
|
size_t availRoom = self->bufferEnd - self->bufferBegin - self->bufferFilledSize;
|
|
if (self->insertBuffer.size > availRoom)
|
|
self->insertBuffer.size = availRoom;
|
|
|
|
// Create a hole in the edit buffer where to copy the insert buffer.
|
|
//
|
|
memmove(self->bufferBegin + self->cursorOffset + self->insertBuffer.size,
|
|
self->bufferBegin + self->cursorOffset,
|
|
self->bufferFilledSize - self->cursorOffset);
|
|
|
|
memcpy(self->bufferBegin + self->cursorOffset, self->insertBuffer.buffer, self->insertBuffer.size);
|
|
|
|
self->bufferFilledSize += self->insertBuffer.size;
|
|
self->cursorOffset += self->insertBuffer.size;
|
|
self->insertBuffer.size = 0;
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_startCommandMode(void)
|
|
{
|
|
Ebitor *self = &Ebitor_instance;
|
|
if (self->mode != MODE_COMMAND)
|
|
{
|
|
Ebitor_flushInsertBuffer();
|
|
self->mode = MODE_COMMAND;
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_insertChar(char c)
|
|
{
|
|
Ebitor *self = &Ebitor_instance;
|
|
if (self->mode == MODE_COMMAND)
|
|
{
|
|
// Go to insertion mode.
|
|
self->mode = MODE_INSERTION;
|
|
self->insertBuffer.buffer[0] = c;
|
|
self->insertBuffer.size = 1;
|
|
}
|
|
else // already in insertion mode
|
|
{
|
|
self->insertBuffer.buffer[self->insertBuffer.size++] = c;
|
|
|
|
if (self->insertBuffer.size == INSERT_BUFFER_SIZE) // if full
|
|
{
|
|
Ebitor_flushInsertBuffer();
|
|
assert(self->insertBuffer.size == 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// If no \r is found, returns the end of the filled part of the edit buffer.
|
|
//
|
|
size_t
|
|
Ebitor_findEOL(size_t startOffset)
|
|
{
|
|
const Ebitor *self = &Ebitor_instance;
|
|
char *p = (char *) memchr(self->bufferBegin + startOffset, '\r', self->bufferFilledSize - startOffset);
|
|
return p ? p - self->bufferBegin : self->bufferFilledSize;
|
|
}
|
|
|
|
|
|
size_t
|
|
Ebitor_findSOL(size_t startOffset)
|
|
{
|
|
const Ebitor *self = &Ebitor_instance;
|
|
char *p = self->bufferBegin + startOffset;
|
|
while (p > self->bufferBegin) // while possible to go back
|
|
{
|
|
--p;
|
|
if (*p == '\r')
|
|
return p + 1 - self->bufferBegin;
|
|
}
|
|
return p - self->bufferBegin;
|
|
}
|
|
|
|
|
|
size_t
|
|
Ebitor_findPreviousSOL(size_t solOffset)
|
|
{
|
|
const Ebitor *self = &Ebitor_instance;
|
|
if (solOffset == 0)
|
|
return solOffset;
|
|
assert(self->bufferBegin[solOffset - 1] == '\r');
|
|
return Ebitor_findSOL(solOffset - 1);
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_fillTerminalToBottom(size_t firstLogicalCharOffsetToPrint, byte firstRowToFill)
|
|
{
|
|
const Ebitor *self = &Ebitor_instance;
|
|
//printf("(%u,%u;%u)", firstLogicalCharOffsetToPrint, firstRowToFill, self->bufferFilledSize);
|
|
assert(firstLogicalCharOffsetToPrint <= self->bufferFilledSize);
|
|
if (firstRowToFill >= self->termHeight)
|
|
return; // failsafe -- nothing to do
|
|
|
|
self->locate(0, firstRowToFill);
|
|
|
|
if (firstLogicalCharOffsetToPrint == self->bufferFilledSize) // if nothing to print
|
|
{
|
|
self->clrtoeol(); // erase a line that may have been merged up
|
|
return;
|
|
}
|
|
|
|
// Reprint the rest of the window, while preserving the cursor position.
|
|
// The portion to print starts at self->cursorOffset and ends either at
|
|
// the end of the filled edit buffer, or at the bottom of the window.
|
|
//
|
|
const char *begin = self->bufferBegin + firstLogicalCharOffsetToPrint;
|
|
const char *end = self->bufferBegin + self->bufferFilledSize;
|
|
byte c = 0, r = firstRowToFill;
|
|
const char *p = begin;
|
|
for ( ; r < self->termHeight && p < end; ++p)
|
|
{
|
|
if (*p == '\r')
|
|
{
|
|
c = 0;
|
|
++r;
|
|
}
|
|
else // printable char
|
|
{
|
|
++c;
|
|
if (c == self->termWidth)
|
|
{
|
|
c = 0;
|
|
++r;
|
|
}
|
|
}
|
|
}
|
|
// p points to the first char NOT to be printed.
|
|
|
|
self->printPrefix(begin, p - begin);
|
|
|
|
if (r < self->termHeight)
|
|
self->clrtoeol();
|
|
if (r < self->termHeight - 1)
|
|
{
|
|
self->locate(0, r + 1);
|
|
self->clrtoeol();
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_fillWindowFromHomeToRow(size_t firstLogicalCharOffsetToPrint, byte numRowsToFill)
|
|
{
|
|
Ebitor *self = &Ebitor_instance;
|
|
if (numRowsToFill == 0)
|
|
return;
|
|
self->locate(0, 0);
|
|
size_t offset = firstLogicalCharOffsetToPrint;
|
|
for (;;)
|
|
{
|
|
size_t eolOffset = Ebitor_findEOL(offset);
|
|
while (eolOffset - offset >= self->termWidth) // if current row to be filled completely
|
|
{
|
|
self->printPrefix(self->bufferBegin + offset, self->termWidth);
|
|
if (--numRowsToFill == 0)
|
|
return;
|
|
offset += self->termWidth;
|
|
}
|
|
|
|
if (offset != eolOffset)
|
|
self->printPrefix(self->bufferBegin + offset, eolOffset - offset);
|
|
self->print("\r");
|
|
if (--numRowsToFill == 0)
|
|
return;
|
|
if (eolOffset == self->bufferFilledSize) // if EOF instead of \r
|
|
break; // go blank remaining rows to fill
|
|
|
|
offset = eolOffset + 1; // pass \r
|
|
}
|
|
|
|
for ( ; numRowsToFill--; )
|
|
self->print("\r");
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_setLogicalCursorAtBeginning(void)
|
|
{
|
|
Ebitor_instance.cursorOffset = 0;
|
|
Ebitor_instance.savedCursorCol = 0;
|
|
Ebitor_instance.savedCursorRow = 0;
|
|
}
|
|
|
|
|
|
size_t
|
|
Ebitor_getLogicalCursorOffset(void)
|
|
{
|
|
return Ebitor_instance.cursorOffset;
|
|
}
|
|
|
|
|
|
static void
|
|
Ebitor_refresh(byte cursorCol, byte cursorRow)
|
|
{
|
|
Ebitor_instance.clear();
|
|
|
|
// Reprint the window starting at the logical offset that corresponds to
|
|
// the top-left corner of the terminal.
|
|
//
|
|
size_t homeOffset = Ebitor_getLogicalOffsetAtHome(cursorCol, cursorRow);
|
|
Ebitor_fillTerminalToBottom(homeOffset, 0);
|
|
}
|
|
|
|
|
|
static void
|
|
Ebitor_processLeftArrow(byte cursorCol, byte cursorRow)
|
|
{
|
|
assert(Ebitor_instance.mode == MODE_COMMAND);
|
|
|
|
if (Ebitor_instance.cursorOffset == 0) // if already at start of edit buffer
|
|
return; // nothing to do
|
|
|
|
if (cursorCol > 0) // if not at leftmost window column
|
|
{
|
|
Ebitor_instance.locate(cursorCol - 1, cursorRow);
|
|
}
|
|
else
|
|
{
|
|
// Cursor at leftmost window column.
|
|
// If at start of logical line, go to end of previous logical line.
|
|
// Otherwise, scroll up if needed, then go to rightmost window column
|
|
// on previous window row.
|
|
//
|
|
size_t solOffset = Ebitor_findSOL(Ebitor_instance.cursorOffset);
|
|
if (solOffset == Ebitor_instance.cursorOffset) // if at start of logical line
|
|
{
|
|
size_t prevSOLOffset = Ebitor_findPreviousSOL(Ebitor_instance.cursorOffset);
|
|
assert(prevSOLOffset != Ebitor_instance.cursorOffset); // previous line found, because cursorOffset not 0
|
|
size_t prevLineLen = solOffset - prevSOLOffset - 1; // minus one to exclude \r
|
|
byte lastRowLen = (byte) (prevLineLen % Ebitor_instance.termWidth); // e.g., if 37-char logical line on 32-column terminal, then 5
|
|
if (cursorRow == 0)
|
|
{
|
|
// At top row of window. There is a line before the current one, b/c cursorOffset > 0.
|
|
// Scroll the window down one row.
|
|
// Print the last 'lastRowLen' chars of that line. Leave the cursor after these.
|
|
size_t offsetToPrint = Ebitor_instance.cursorOffset - 1 - lastRowLen;
|
|
Ebitor_instance.scrollTerminalDown(1);
|
|
Ebitor_instance.printPrefix(Ebitor_instance.bufferBegin + offsetToPrint, lastRowLen);
|
|
}
|
|
else
|
|
--cursorRow;
|
|
Ebitor_instance.locate(lastRowLen, cursorRow);
|
|
}
|
|
else // not at start of logical line
|
|
{
|
|
// Current logical line starts before cursorOffset.
|
|
if (cursorRow == 0)
|
|
{
|
|
// Cursor is home. Print full window row of chars from current logical line.
|
|
Ebitor_instance.scrollTerminalDown(1);
|
|
Ebitor_instance.printPrefix(Ebitor_instance.bufferBegin + (Ebitor_instance.cursorOffset - Ebitor_instance.termWidth), Ebitor_instance.termWidth);
|
|
}
|
|
else
|
|
--cursorRow;
|
|
Ebitor_instance.locate(Ebitor_instance.termWidth - 1, cursorRow);
|
|
}
|
|
}
|
|
--Ebitor_instance.cursorOffset;
|
|
}
|
|
|
|
|
|
static void
|
|
Ebitor_processRightArrow(byte cursorCol, byte cursorRow)
|
|
{
|
|
assert(Ebitor_instance.mode == MODE_COMMAND);
|
|
|
|
if (Ebitor_instance.cursorOffset == Ebitor_instance.bufferFilledSize) // if already at EOF
|
|
return; // nothing to do
|
|
|
|
size_t eolOffset = Ebitor_findEOL(Ebitor_instance.cursorOffset);
|
|
if (Ebitor_instance.cursorOffset == eolOffset // if at end of logical line
|
|
|| cursorCol + 1 == Ebitor_instance.termWidth) // or if at rightmost window column
|
|
{
|
|
if (cursorRow == Ebitor_instance.termHeight - 1) // if starting on last window row
|
|
{
|
|
Ebitor_instance.scrollTerminalUp(1);
|
|
Ebitor_instance.locate(0, cursorRow);
|
|
|
|
// If the move still leaves the logical cursor in the current logical line,
|
|
// then print then the next row of the current logical line.
|
|
// Otherwise, print the first row of the next logical line.
|
|
//
|
|
if (Ebitor_instance.cursorOffset < eolOffset)
|
|
{
|
|
size_t nextCharOffset = Ebitor_instance.cursorOffset + 1;
|
|
size_t lineLenFromNextChar = eolOffset - nextCharOffset;
|
|
byte toPrint = (byte) min(lineLenFromNextChar, (size_t) Ebitor_instance.termWidth);
|
|
Ebitor_instance.printPrefix(Ebitor_instance.bufferBegin + nextCharOffset, toPrint);
|
|
}
|
|
else
|
|
{
|
|
size_t nextLineOffset = eolOffset + 1; // after the \r
|
|
size_t nextLineLen = Ebitor_findEOL(nextLineOffset) - nextLineOffset;
|
|
byte charsOnFirstRow = (byte) min(nextLineLen, (size_t) Ebitor_instance.termWidth);
|
|
Ebitor_instance.printPrefix(Ebitor_instance.bufferBegin + nextLineOffset, charsOnFirstRow);
|
|
}
|
|
}
|
|
else
|
|
++cursorRow;
|
|
|
|
cursorCol = 0;
|
|
}
|
|
else
|
|
++cursorCol; // move cursor right
|
|
|
|
assert(cursorRow < Ebitor_instance.termHeight);
|
|
Ebitor_instance.locate(cursorCol, cursorRow);
|
|
|
|
++Ebitor_instance.cursorOffset;
|
|
assert(Ebitor_instance.cursorOffset <= Ebitor_instance.bufferFilledSize);
|
|
}
|
|
|
|
|
|
// May change *cursorCol and *cursorRow.
|
|
// Does not change any fields of Ebitor_instance.
|
|
//
|
|
static void
|
|
Ebitor_scrollDownAndPrintRow(byte *cursorRow, size_t newOffsetToPrintFrom, size_t numCharsToPrint, BOOL affectTerminal)
|
|
{
|
|
if (*cursorRow == 0) // if cursor is on top row of terminal
|
|
{
|
|
Ebitor_instance.scrollTerminalDown(1); // scroll terminal down to free top row
|
|
|
|
if (affectTerminal)
|
|
{
|
|
// Print the previous logical line on the top terminal row,
|
|
// or a portion of that line if it is longer than a terminal row.
|
|
//
|
|
Ebitor_instance.locate(0, *cursorRow);
|
|
Ebitor_instance.printPrefix(Ebitor_instance.bufferBegin + newOffsetToPrintFrom, numCharsToPrint);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
assert(*cursorRow > 0);
|
|
--*cursorRow;
|
|
}
|
|
}
|
|
|
|
|
|
static void
|
|
Ebitor_processUpArrow(byte cursorCol, byte cursorRow, BOOL affectTerminal)
|
|
{
|
|
assert(Ebitor_instance.mode == MODE_COMMAND);
|
|
|
|
// Do nothing if already on 1st logical line.
|
|
//
|
|
size_t solOffset = Ebitor_findSOL(Ebitor_instance.cursorOffset);
|
|
|
|
// If the current logical line starts before the cursor row, go up in that logical line.
|
|
//
|
|
size_t newCursorOffset;
|
|
if (Ebitor_instance.cursorOffset - solOffset >= Ebitor_instance.termWidth)
|
|
{
|
|
// Current logical line starts before cursor row.
|
|
// Place cursor earlier in current logical line.
|
|
//
|
|
newCursorOffset = Ebitor_instance.cursorOffset - Ebitor_instance.termWidth;
|
|
|
|
Ebitor_scrollDownAndPrintRow(&cursorRow, newCursorOffset, Ebitor_instance.termWidth, affectTerminal);
|
|
}
|
|
else
|
|
{
|
|
// Cursor is on first window row of current logical line.
|
|
// Place cursor on last window row of preceding logical line, if any.
|
|
//
|
|
if (solOffset == 0)
|
|
return;
|
|
|
|
size_t prevSOLOffset = Ebitor_findPreviousSOL(solOffset);
|
|
assert(prevSOLOffset < solOffset);
|
|
size_t prevLineLen = solOffset - prevSOLOffset - 1; // exclude \r
|
|
byte charsOnLastRow = (byte) (prevLineLen % Ebitor_instance.termWidth); // after last char of previous line
|
|
size_t offsetOfLastRowOfPrevLine = solOffset - 1 - charsOnLastRow;
|
|
|
|
Ebitor_scrollDownAndPrintRow(&cursorRow, offsetOfLastRowOfPrevLine, charsOnLastRow, affectTerminal);
|
|
|
|
if (charsOnLastRow < cursorCol) // avoid putting cursor in empty space
|
|
cursorCol = charsOnLastRow;
|
|
|
|
newCursorOffset = offsetOfLastRowOfPrevLine + cursorCol;
|
|
}
|
|
Ebitor_instance.cursorOffset = newCursorOffset; // move the logical cursor
|
|
Ebitor_instance.locate(cursorCol, cursorRow);
|
|
}
|
|
|
|
|
|
// May change *cursorCol and *cursorRow.
|
|
// Does not change any fields of Ebitor_instance.
|
|
//
|
|
static void
|
|
Ebitor_scrollUpAndPrintRow(byte *cursorCol, byte *cursorRow, size_t newOffsetToPrintFrom, byte numCharsToPrint, BOOL affectTerminal)
|
|
{
|
|
if (*cursorRow == Ebitor_instance.termHeight - 1) // scroll if cursor on last window row
|
|
{
|
|
if (affectTerminal)
|
|
{
|
|
Ebitor_instance.scrollTerminalUp(1);
|
|
Ebitor_instance.locate(0, *cursorRow);
|
|
Ebitor_instance.printPrefix(Ebitor_instance.bufferBegin + newOffsetToPrintFrom, numCharsToPrint);
|
|
}
|
|
}
|
|
else
|
|
++*cursorRow;
|
|
assert(*cursorRow < Ebitor_instance.termHeight);
|
|
|
|
if (*cursorCol > numCharsToPrint) // avoid putting cursor in empty space
|
|
*cursorCol = numCharsToPrint;
|
|
}
|
|
|
|
|
|
static void
|
|
Ebitor_processDownArrow(byte cursorCol, byte cursorRow, BOOL affectTerminal)
|
|
{
|
|
assert(Ebitor_instance.mode == MODE_COMMAND);
|
|
|
|
size_t eolOffset = Ebitor_findEOL(Ebitor_instance.cursorOffset);
|
|
|
|
// If the current logical line has characters beyond the current cursor row,
|
|
// move down this logical line.
|
|
//
|
|
size_t newCursorOffset;
|
|
if (cursorCol + (eolOffset - Ebitor_instance.cursorOffset) >= Ebitor_instance.termWidth)
|
|
{
|
|
const size_t newOffsetToPrintFrom = Ebitor_instance.cursorOffset - cursorCol + Ebitor_instance.termWidth;
|
|
const byte charsOnNewRow = (byte) min(eolOffset - newOffsetToPrintFrom, (size_t) Ebitor_instance.termWidth);
|
|
|
|
Ebitor_scrollUpAndPrintRow(&cursorCol, &cursorRow, newOffsetToPrintFrom, charsOnNewRow, affectTerminal);
|
|
|
|
newCursorOffset = min(Ebitor_instance.cursorOffset + Ebitor_instance.termWidth, eolOffset);
|
|
}
|
|
else if (eolOffset < Ebitor_instance.bufferFilledSize) // logical line ends on current window row, so if it has a \r:
|
|
{
|
|
assert(Ebitor_instance.bufferBegin[eolOffset] == '\r');
|
|
const size_t newOffsetToPrintFrom = eolOffset + 1; // after the \r
|
|
assert(newOffsetToPrintFrom <= Ebitor_instance.bufferFilledSize);
|
|
const size_t nextLineLen = Ebitor_findEOL(newOffsetToPrintFrom) - newOffsetToPrintFrom;
|
|
const byte charsOnNewRow = (byte) min(nextLineLen, (size_t) Ebitor_instance.termWidth);
|
|
|
|
Ebitor_scrollUpAndPrintRow(&cursorCol, &cursorRow, newOffsetToPrintFrom, charsOnNewRow, affectTerminal);
|
|
|
|
newCursorOffset = newOffsetToPrintFrom + cursorCol; // move the logical cursor
|
|
}
|
|
else
|
|
return;
|
|
|
|
Ebitor_instance.cursorOffset = newCursorOffset;
|
|
if (affectTerminal)
|
|
Ebitor_instance.locate(cursorCol, cursorRow);
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_processUpOrDownArrowsAndRefresh(BOOL isUpArrow, size_t numRepetitions)
|
|
{
|
|
Ebitor_locateCursorAtSavedPosition();
|
|
|
|
byte cursorCol, cursorRow;
|
|
Ebitor_instance.getCursorPos(&cursorCol, &cursorRow);
|
|
|
|
Ebitor_startCommandMode();
|
|
|
|
// Point to the right arrow processing function, depending on isUpArrow.
|
|
void (*processArrow)(byte cursorCol, byte cursorRow, BOOL affectTerminal) = isUpArrow ? Ebitor_processUpArrow : Ebitor_processDownArrow;
|
|
|
|
for ( ; numRepetitions--; )
|
|
(*processArrow)(cursorCol, cursorRow, FALSE); // do not affect terminal
|
|
|
|
Ebitor_scrollToShowLogicalOffset(Ebitor_instance.cursorOffset, 0);
|
|
Ebitor_saveCursorPosition();
|
|
|
|
Ebitor_refresh(Ebitor_instance.savedCursorCol, Ebitor_instance.savedCursorRow);
|
|
}
|
|
|
|
|
|
// key: Value returned by a function of type ebitor_waitKeyFunc.
|
|
//
|
|
void
|
|
Ebitor_processKey(word key)
|
|
{
|
|
byte cursorCol, cursorRow;
|
|
Ebitor_instance.getCursorPos(&cursorCol, &cursorRow);
|
|
|
|
switch (key)
|
|
{
|
|
case '\b': // erase previous char
|
|
Ebitor_processBackspace(cursorCol, cursorRow);
|
|
break;
|
|
case '\r':
|
|
{
|
|
Ebitor_instance.clrtoeol();
|
|
|
|
if (cursorRow < Ebitor_instance.termHeight - 1) // if not on last window row
|
|
++cursorRow;
|
|
else // on last window row
|
|
Ebitor_instance.scrollTerminalUp(1);
|
|
|
|
Ebitor_fillTerminalToBottom(Ebitor_instance.cursorOffset, cursorRow);
|
|
Ebitor_instance.locate(0, cursorRow);
|
|
|
|
Ebitor_insertChar('\r');
|
|
}
|
|
break;
|
|
case ebitor_KEY_ARROW_LEFT:
|
|
// Go to command mode if not already in it.
|
|
// This flushes the insert buffer, if any.
|
|
//
|
|
Ebitor_startCommandMode();
|
|
|
|
Ebitor_processLeftArrow(cursorCol, cursorRow);
|
|
break;
|
|
case ebitor_KEY_ARROW_RIGHT:
|
|
Ebitor_startCommandMode();
|
|
Ebitor_processRightArrow(cursorCol, cursorRow);
|
|
break;
|
|
case ebitor_KEY_ARROW_UP:
|
|
Ebitor_startCommandMode();
|
|
Ebitor_processUpArrow(cursorCol, cursorRow, TRUE);
|
|
break;
|
|
case ebitor_KEY_ARROW_DOWN:
|
|
Ebitor_startCommandMode();
|
|
Ebitor_processDownArrow(cursorCol, cursorRow, TRUE);
|
|
break;
|
|
default:
|
|
Ebitor_processPrintableChar(key, cursorCol, cursorRow);
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_locateCursorAtSavedPosition(void)
|
|
{
|
|
Ebitor_instance.locate(Ebitor_instance.savedCursorCol, Ebitor_instance.savedCursorRow);
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_saveCursorPosition(void)
|
|
{
|
|
Ebitor_instance.getCursorPos(&Ebitor_instance.savedCursorCol, &Ebitor_instance.savedCursorRow);
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_processEditing(BOOL startWithRefresh)
|
|
{
|
|
if (startWithRefresh)
|
|
Ebitor_refresh(Ebitor_instance.savedCursorCol, Ebitor_instance.savedCursorRow);
|
|
|
|
Ebitor_locateCursorAtSavedPosition();
|
|
|
|
for (;;)
|
|
{
|
|
assert(Ebitor_instance.cursorOffset <= Ebitor_instance.bufferFilledSize);
|
|
|
|
word key = Ebitor_instance.waitKey();
|
|
if (key == ebitor_KEY_ESCAPE)
|
|
break;
|
|
|
|
Ebitor_processKey(key);
|
|
}
|
|
|
|
Ebitor_startCommandMode(); // put inserted chars in edit buffer
|
|
|
|
// Remember the cursor position for the next time this method is called.
|
|
//
|
|
Ebitor_saveCursorPosition();
|
|
}
|
|
|
|
|
|
// Determines which logical offset is currently at position (0, 0) on the terminal,
|
|
// assuming that the current logical cursor is at terminal position (cursorCol, cursorRow).
|
|
//
|
|
size_t
|
|
Ebitor_getLogicalOffsetAtHome(byte cursorCol, byte cursorRow)
|
|
{
|
|
Ebitor *self = &Ebitor_instance;
|
|
Ebitor_startCommandMode(); // put insert buffer into edit buffer, so that all the text is contiguous
|
|
|
|
assert(self->cursorOffset >= cursorCol);
|
|
size_t offset = self->cursorOffset - cursorCol; // initialize at start of terminal row
|
|
//printf("[gLOAH(%u,%u):co=%u,o=%u]", cursorCol, cursorRow, self->cursorOffset, offset);
|
|
for (;;) // bring 'offset' back to the logical offset that is at (0, 0) on the terminal
|
|
{
|
|
//printf("(cr=%u,o=%u)", cursorRow, offset);
|
|
if (cursorRow == 0)
|
|
return offset;
|
|
if (offset == 0)
|
|
return 0;
|
|
//printf("(CHAR %u)", self->bufferBegin[offset - 1]);
|
|
if (self->bufferBegin[offset - 1] != '\r')
|
|
{
|
|
offset -= self->termWidth; // go back to start of terminal row
|
|
--cursorRow;
|
|
}
|
|
else // if offset is at the start of a logical line
|
|
{
|
|
//printf("(FOR)");
|
|
for (;;)
|
|
{
|
|
size_t startOfLogicalLine = Ebitor_findSOL(offset - 1); // start of previous line
|
|
size_t logicalLineLen = offset - 1 - startOfLogicalLine;
|
|
//printf("(soll=%u,lll=%u)", startOfLogicalLine, logicalLineLen);
|
|
offset -= 1 + logicalLineLen % self->termWidth; // go back to \r, then back to start of row
|
|
--cursorRow;
|
|
for (;;)
|
|
{
|
|
if (cursorRow == 0)
|
|
return offset;
|
|
if (offset == startOfLogicalLine)
|
|
break; // reached start of logical line
|
|
|
|
// In the current logical line, terminal location (0, cursorRow) is now preceded by
|
|
// a number of logical chars divisible by termWidth.
|
|
assert((offset - startOfLogicalLine) % self->termWidth == 0);
|
|
offset -= self->termWidth;
|
|
--cursorRow;
|
|
}
|
|
//printf("[cr=%u,o=%u]", cursorRow, offset);
|
|
assert(cursorRow > 0);
|
|
if (offset == 0)
|
|
return 0;
|
|
//printf("[%u]", self->bufferBegin[offset - 1]);
|
|
assert(self->bufferBegin[offset - 1] == '\r');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_getLogicalOffsetBelowWindow(size_t homeOffset, size_t *resultOffset, BOOL *isInWindow)
|
|
{
|
|
Ebitor *self = &Ebitor_instance;
|
|
byte termRow = 0;
|
|
size_t offset = homeOffset;
|
|
byte numEditorLines = self->termHeight;
|
|
for (;;)
|
|
{
|
|
const size_t eolOffset = Ebitor_findEOL(offset);
|
|
while (eolOffset - offset >= self->termWidth)
|
|
{
|
|
offset += self->termWidth;
|
|
++termRow;
|
|
if (termRow >= numEditorLines) // if off window
|
|
{
|
|
*resultOffset = offset;
|
|
*isInWindow = FALSE;
|
|
return;
|
|
}
|
|
}
|
|
if (eolOffset == self->bufferFilledSize)
|
|
{
|
|
*resultOffset = eolOffset;
|
|
*isInWindow = TRUE;
|
|
return;
|
|
}
|
|
assert(self->bufferBegin[eolOffset] == '\r');
|
|
offset = eolOffset + 1; // pass '\r', which goes to next row
|
|
++termRow;
|
|
if (termRow >= numEditorLines) // if off window
|
|
{
|
|
*resultOffset = offset;
|
|
*isInWindow = FALSE;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
char
|
|
Ebitor_isRegionVisible(size_t regionOffset, size_t regionLength, byte cursorCol, byte cursorRow,
|
|
size_t *homeOffset, size_t *firstOffWindowOffset)
|
|
{
|
|
Ebitor *self = &Ebitor_instance;
|
|
//printf("[iRV(%u,%u,%u,%u): BFS=%u]",
|
|
// regionOffset, regionLength, cursorCol, cursorRow, self->bufferFilledSize);
|
|
assert(regionOffset + regionLength <= self->bufferFilledSize);
|
|
assert(self->insertBuffer.size == 0);
|
|
|
|
*homeOffset = Ebitor_getLogicalOffsetAtHome(cursorCol, cursorRow);
|
|
if (regionOffset < *homeOffset) // if region is entirely or partly before the text that starts at home
|
|
return -1; // region is (entirely or partly) before displayed text
|
|
|
|
// Get the offset of the first character that is below the bottom of the terminal.
|
|
//
|
|
BOOL isInWindow;
|
|
Ebitor_getLogicalOffsetBelowWindow(*homeOffset, firstOffWindowOffset, &isInWindow);
|
|
|
|
if (!isInWindow && regionOffset + regionLength > *firstOffWindowOffset)
|
|
return +1; // region is (entirely or partly) after displayed text
|
|
|
|
return 0; // region is entirely in the displayed text
|
|
}
|
|
|
|
|
|
size_t
|
|
Ebitor_countRowsToScrollDown(size_t logicalOffsetToShow, size_t homeOffset,
|
|
size_t *logicalOffsetAtStartOfTermRow)
|
|
{
|
|
// Count the number of terminal rows from logicalOffsetToShow to homeOffset.
|
|
// This is how many rows we need to scroll the terminal down.
|
|
//
|
|
assert(logicalOffsetToShow < homeOffset);
|
|
|
|
Ebitor *self = &Ebitor_instance;
|
|
size_t solOffset = Ebitor_findSOL(logicalOffsetToShow);
|
|
|
|
// Bring logicalOffsetToShow back to the preceding logical position that is displayed
|
|
// at the start of a window row.
|
|
// We have: logicalOffsetToShow == solOffset + R * termWidth + C, where R >= 0 and C < termWidth.
|
|
// We replace logicalOffsetToShow with solOffset + R * termWidth.
|
|
// Example: A line has 67 chars and the terminal width is 32
|
|
// We have logicalOffsetToShow - solOffset == 67.
|
|
// 67 % 32 == 3, so we bring logicalOffsetToShow to 64.
|
|
//
|
|
*logicalOffsetAtStartOfTermRow = logicalOffsetToShow - (logicalOffsetToShow - solOffset) % self->termWidth;
|
|
|
|
size_t numTermRows = 0;
|
|
size_t offset = *logicalOffsetAtStartOfTermRow;
|
|
for (;;)
|
|
{
|
|
const size_t eolOffset = Ebitor_findEOL(offset);
|
|
while (eolOffset - offset >= self->termWidth)
|
|
{
|
|
offset += self->termWidth;
|
|
++numTermRows;
|
|
if (offset == homeOffset)
|
|
return numTermRows;
|
|
}
|
|
offset = eolOffset + 1; // pass \r
|
|
++numTermRows;
|
|
if (offset == homeOffset)
|
|
return numTermRows;
|
|
}
|
|
}
|
|
|
|
|
|
size_t
|
|
Ebitor_countRowsToScrollUp(size_t logicalOffsetToShow, size_t firstOffWindowOffset)
|
|
{
|
|
Ebitor *self = &Ebitor_instance;
|
|
assert(firstOffWindowOffset <= logicalOffsetToShow);
|
|
assert((firstOffWindowOffset - Ebitor_findSOL(firstOffWindowOffset)) % self->termWidth == 0);
|
|
|
|
size_t numTermRows = 1;
|
|
|
|
size_t offset = firstOffWindowOffset;
|
|
for (;;)
|
|
{
|
|
assert((offset - Ebitor_findSOL(offset)) % self->termWidth == 0);
|
|
|
|
if (offset == logicalOffsetToShow) // if target reached
|
|
return numTermRows;
|
|
|
|
const size_t eolOffset = Ebitor_findEOL(offset);
|
|
|
|
while (eolOffset - offset >= self->termWidth) // as long as term row at 'offset' is filled
|
|
{
|
|
if (logicalOffsetToShow - offset < self->termWidth)
|
|
return numTermRows; // logicalOffsetToShow is on current row
|
|
++numTermRows; // logicalOffsetToShow is on next row or later
|
|
offset += self->termWidth;
|
|
}
|
|
|
|
// Term row at 'offset' is not filled completely, so the current logical line ends on this row.
|
|
//
|
|
if (logicalOffsetToShow <= eolOffset) // if target logical position is on current row
|
|
return numTermRows;
|
|
offset = eolOffset;
|
|
if (offset == self->bufferFilledSize) // if EOF
|
|
return numTermRows; // can't scroll more than this
|
|
|
|
++offset; // pass the \r; now at position that would be at start of window row
|
|
++numTermRows;
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
Ebitor_locateAndSavePos(byte col, byte row)
|
|
{
|
|
Ebitor_instance.locate(col, row);
|
|
Ebitor_instance.savedCursorCol = col;
|
|
Ebitor_instance.savedCursorRow = row;
|
|
}
|
|
|
|
|
|
// startOfRowOffset: Logical offset of a character that is in column 0.
|
|
// Returns the logical offset of another character down the text that is also in column 0.
|
|
// If there are not enough rows, stops at the last row.
|
|
//
|
|
size_t
|
|
Ebitor_goDownRows(size_t startOfRowOffset, size_t numRows)
|
|
{
|
|
size_t offset = startOfRowOffset;
|
|
for (;;)
|
|
{
|
|
size_t eolOffset = Ebitor_findEOL(offset);
|
|
while (eolOffset - offset >= Ebitor_instance.termWidth) // while terminal row filled
|
|
{
|
|
offset += Ebitor_instance.termWidth;
|
|
if (--numRows == 0)
|
|
return offset;
|
|
}
|
|
// EOL is on current row.
|
|
if (eolOffset == Ebitor_instance.bufferFilledSize) // if EOL is EOF
|
|
return offset;
|
|
offset = eolOffset + 1; // pass \r
|
|
// 'offset' is now at start of next logical line, which is displayed at column 0.
|
|
if (--numRows == 0)
|
|
return offset;
|
|
}
|
|
}
|