Mohammed Ashour
Mohammed Ashour

Reputation: 75

curses moving cursor to non blank position

I'm writing a simple text editor in C, on cygwin using curses, now I have a screen full of line of text, some line are partial lines, now when I move the cursor up or down one line I want it to move to non blank position if the previous/next line is partial line, how to do it? any help will be greatly appreciated. thanks.

Upvotes: 0

Views: 1507

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54475

curses (and ncurses and PDCurses) all support the winch function, which allows an application to read the character stored at the current cursor position. Likewise, all versions of curses can and do represent some characters as multiple cells. So storing line-lengths and attempting to use those as column numbers on the screen can produce unsatisfactory results.

As an example, you could do something like this (for simplicity, all in-line and just for the standard screen stdscr — real programs aren't really like this):

int y, x, xc;
bool partial = TRUE;
getyx(stdscr, y, x);
if (y > 0) {
    y--;
    for (xc = x; xc < COLS; ++xc) {
        move(y, xc);
        if ((inch() & A_CHARTEXT) != ' ') {
            partial = FALSE;
            break; /* found a nonblank cell at or beyond current x */
        }
    }
    if (partial) {
        for (xc = x; xc >= 0; --xc) {
            move(y, xc);
            if ((inch() & A_CHARTEXT) != ' ') {
                break; /* found the last nonblank cell on the line */
            }
        }
    }
}

Upvotes: 1

Related Questions