/* scrollToShowLogicalOffset.c
Copyright (C) 2021-2025 Pierre Sarrazin
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 .
*/
#include "Ebitor_private.h"
void
Ebitor_scrollToShowLogicalOffset(size_t logicalOffsetToShow, size_t lengthToShow)
{
Ebitor *self = &Ebitor_instance;
byte cursorCol = self->savedCursorCol, cursorRow = self->savedCursorRow;
size_t homeOffset, firstOffWindowOffset;
char code = Ebitor_isRegionVisible(logicalOffsetToShow, lengthToShow, cursorCol, cursorRow,
&homeOffset, &firstOffWindowOffset);
//printf("(%u,%u,%u->CODE=%d,HO=%u,F=%u)",
// logicalOffsetToShow, cursorCol, cursorRow, code, homeOffset, firstOffWindowOffset);
if (code < 0) // if region to show is before displayed text
{
size_t logicalOffsetAtStartOfTermRow;
size_t numTermRows = Ebitor_countRowsToScrollDown(logicalOffsetToShow, homeOffset, &logicalOffsetAtStartOfTermRow);
assert(numTermRows > 0);
if (numTermRows < self->termHeight) // if NOT scrolling all current rows off
{
assert(logicalOffsetAtStartOfTermRow <= logicalOffsetToShow);
assert(logicalOffsetToShow - logicalOffsetAtStartOfTermRow < self->termWidth);
self->scrollTerminalDown((byte) numTermRows);
}
else // redraw whole window
numTermRows = self->termHeight;
Ebitor_fillWindowFromHomeToRow(logicalOffsetAtStartOfTermRow, (byte) numTermRows);
byte cursorCol = (byte) (logicalOffsetToShow - logicalOffsetAtStartOfTermRow);
Ebitor_locateAndSavePos(cursorCol, 0);
}
else if (code > 0) // region to show is after displayed text
{
size_t numTermRows = Ebitor_countRowsToScrollUp(logicalOffsetToShow, firstOffWindowOffset);
assert(numTermRows > 0);
if (numTermRows < self->termHeight) // if NOT scrolling all current rows off
{
self->scrollTerminalUp((byte) numTermRows);
Ebitor_fillTerminalToBottom(firstOffWindowOffset, self->termHeight - (byte) numTermRows);
}
else // redraw whole window
{
size_t newHomeOffset = Ebitor_goDownRows(homeOffset, numTermRows);
assert(newHomeOffset <= self->bufferFilledSize);
Ebitor_fillWindowFromHomeToRow(newHomeOffset, self->termHeight);
}
size_t solOffset = Ebitor_findSOL(logicalOffsetToShow);
byte cursorCol = (byte) ((logicalOffsetToShow - solOffset) % self->termWidth);
Ebitor_locateAndSavePos(cursorCol, self->termHeight - 1);
}
else // code == 0: region is complete in displayed text
{
// Locate the cursor at logicalOffsetToShow.
byte cursorCol = 0, cursorRow = 0; // position of homeOffset
size_t offset = homeOffset;
for (;;)
{
// Here, offset is in column 0 of the terminal.
//
size_t eolOffset = Ebitor_findEOL(offset);
if (logicalOffsetToShow <= eolOffset) // if target position is on current logical line
{
cursorCol = (byte) ((logicalOffsetToShow - offset) % self->termWidth);
cursorRow += (byte) ((logicalOffsetToShow - offset) / self->termWidth);
// No overflow possible b/c we know logicalOffsetToShow is on window
assert(cursorRow < self->termHeight);
Ebitor_locateAndSavePos(cursorCol, cursorRow);
break;
}
assert(eolOffset < self->bufferFilledSize); // b/c logicalOffsetToShow is after eolOffset
size_t numRowsInLine = (eolOffset - offset) / self->termWidth + 1;
assert(cursorRow + numRowsInLine < self->termHeight);
// No overflow possible b/c we know logicalOffsetToShow is on window
offset = eolOffset + 1; // go to char after \r; this char is at column 0
cursorRow += (byte) numRowsInLine;
}
}
self->cursorOffset = logicalOffsetToShow;
}