/* processPrintableChar.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_processPrintableChar(word key, byte cursorCol, byte cursorRow) { if (key < 256) { // Reject if no room in edit buffer. // if (Ebitor_instance.bufferFilledSize + Ebitor_instance.insertBuffer.size + 1 > Ebitor_instance.bufferEnd - Ebitor_instance.bufferBegin) return; size_t eolOffset = Ebitor_findEOL(Ebitor_instance.cursorOffset); // eolOffset either designates a \r, or the end of the filled edit buffer. // If the character is about to be printed on the last row, check if scrolling up is needed. // Count the number of times a printable character will be printed in the last window column // plus the number of carriage returns that will be printed, when printing the rest of the // affected line. // If a scroll is done, the cursor is moved one row up. // if (cursorRow == Ebitor_instance.termHeight - 1) { BOOL needScroll = FALSE; byte col = cursorCol; ++col; // cursor position after printing 'key' if (col == Ebitor_instance.termWidth) // if past last column needScroll = TRUE; const char *p = Ebitor_instance.bufferBegin + Ebitor_instance.cursorOffset; const char *end = Ebitor_instance.bufferBegin + eolOffset; //locate(0, 8); printf("[%u,%u]", Ebitor_instance.cursorOffset, eolOffset); for ( ; p < end; ++p) { if (*p == '\r') { col = 0; needScroll = TRUE; } else // *p is printable { ++col; if (col == Ebitor_instance.termWidth) needScroll = TRUE; } } if (needScroll) { Ebitor_instance.scrollTerminalUp(1); --cursorRow; assert(cursorRow >= 0); assert(cursorRow < Ebitor_instance.termHeight); Ebitor_instance.locate(cursorCol, cursorRow); } } // Print the character just typed. // char c = (char) key; Ebitor_instance.printPrefix(&c, 1); // Reprint the rest of the line, if any, while preserving the new cursor position. // size_t lenRest = eolOffset - Ebitor_instance.cursorOffset; if (lenRest > 0) { Ebitor_instance.getCursorPos(&cursorCol, &cursorRow); assert(cursorRow < Ebitor_instance.termHeight); if (Ebitor_instance.printPrefix(Ebitor_instance.bufferBegin + Ebitor_instance.cursorOffset, lenRest)) { // We did not reach the end of the window. byte afterPrintCol, afterPrintRow; Ebitor_instance.getCursorPos(&afterPrintCol, &afterPrintRow); if (afterPrintCol == 0) // if printing ends at column 0 { Ebitor_instance.print("\r"); // show an empty row if (eolOffset < Ebitor_instance.bufferFilledSize // if something after current EOL && afterPrintRow + 1 < Ebitor_instance.termHeight) // and there is room in the terminal window Ebitor_fillTerminalToBottom(eolOffset + 1, afterPrintRow + 1); } } Ebitor_instance.locate(cursorCol, cursorRow); } // Ebitor_instance.cursorOffset is not incremented here b/c 'c' is only // added to the insert buffer. When the latter will be moved // to the edit buffer, Ebitor_instance.cursorOffset will be incremented. // Ebitor_insertChar(c); } }