From: a bellenir Date: Mon, 4 Jan 2016 19:48:20 +0000 (-0500) Subject: break cursor movement out into functions X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=e3d972be5fbeb1ff4a405758506bac7681cf4b00;p=projs%2Ftide.git break cursor movement out into functions --- diff --git a/source/main.c b/source/main.c index 11a8ba2..a43bc13 100644 --- a/source/main.c +++ b/source/main.c @@ -109,59 +109,79 @@ static void load(char* fname) fclose(file); } +static void cursorLeft() +{ + Curr.x--; + if (Curr.x < 0) { + Curr.x = 0; + Loc.offset--; + if (Loc.offset < 0) + Loc.offset = 0; + ScreenDirty = true; + } +} + +static void cursorDown() +{ + Curr.y++; + if (Curr.y >= Max.y) { + Curr.y = Max.y-1; + if (Curr_File.start->next) { + Curr_File.start = Curr_File.start->next; + ScreenDirty = true; + } + } + if (Loc.line->next) + Loc.line = Loc.line->next; +} + +static void cursorUp() +{ + Curr.y--; + if (Curr.y < 0) { + Curr.y = 0; + if (Curr_File.start->prev) { + Curr_File.start = Curr_File.start->prev; + ScreenDirty = true; + } + } + if (Loc.line->prev) + Loc.line = Loc.line->prev; +} + +static void cursorRight() +{ + Curr.x++; + if (Curr.x >= Max.x) { + Curr.x = Max.x-1; + Loc.offset++; + if (Loc.offset >= Loc.line->length-1) + Loc.offset = Loc.line->length-2; + ScreenDirty = true; + } +} + static void input(int ch) { switch (ch) { case KEY_LEFT: case 'h': - Curr.x--; - if (Curr.x < 0) { - Curr.x = 0; - Loc.offset--; - if (Loc.offset < 0) - Loc.offset = 0; - ScreenDirty = true; - } + cursorLeft(); break; case KEY_DOWN: case 'j': - Curr.y++; - if (Curr.y >= Max.y) { - Curr.y = Max.y-1; - if (Curr_File.start->next) { - Curr_File.start = Curr_File.start->next; - ScreenDirty = true; - } - } - if (Loc.line->next) - Loc.line = Loc.line->next; + cursorDown(); break; case KEY_UP: case 'k': - Curr.y--; - if (Curr.y < 0) { - Curr.y = 0; - if (Curr_File.start->prev) { - Curr_File.start = Curr_File.start->prev; - ScreenDirty = true; - } - } - if (Loc.line->prev) - Loc.line = Loc.line->prev; + cursorUp(); break; case KEY_RIGHT: case 'l': - Curr.x++; - if (Curr.x >= Max.x) { - Curr.x = Max.x-1; - Loc.offset++; - if (Loc.offset >= Loc.line->length-1) - Loc.offset = Loc.line->length-2; - ScreenDirty = true; - } + cursorRight(); break; } /* Cap the column selection at the end of text on the current line */