From da12067099d8bd46460ec558fc59e686ff6a42cb Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Tue, 21 Dec 2021 21:15:08 +1000 Subject: [PATCH] Made it work! --- line.c | 21 +++++++-- line.h | 1 + page.c | 38 ++++------------ page.h | 4 +- text.c | 141 ++++++++++++++++++++++++++++----------------------------- 5 files changed, 98 insertions(+), 107 deletions(-) diff --git a/line.c b/line.c index a1acc7a..be70311 100644 --- a/line.c +++ b/line.c @@ -5,6 +5,7 @@ void init_line(LINE *s) s->size = LINE_SIZE; s->line = (char *)malloc(LINE_SIZE * sizeof(char)); s->line[0] = '\0'; + s->lines_in_screen = 1; } // init_line @@ -19,16 +20,28 @@ void insert_char(LINE *s, char c, int index) s->line[i + 1] = s->line[i]; s->line[index] = c; + + s->lines_in_screen = strlen(s->line) / 75; + if (strlen(s->line) % 75) s->lines_in_screen++; + if (s->lines_in_screen == 0) { + s->lines_in_screen = 1; + } } // insert void remove_char(LINE *s, int index) { - int i; - int len = strlen(s->line); - for(i = index; i < len; i++) - s->line[i] = s->line[i + 1]; + int i; + int len = strlen(s->line); + for(i = index; i < len; i++) + s->line[i] = s->line[i + 1]; + + s->lines_in_screen = strlen(s->line) / 75; + if (strlen(s->line) % 75) s->lines_in_screen++; + if (s->lines_in_screen == 0) { + s->lines_in_screen = 1; + } } // remove_char diff --git a/line.h b/line.h index 8188dce..adeaf64 100644 --- a/line.h +++ b/line.h @@ -12,6 +12,7 @@ typedef struct { char *line; int size; // size of array, not string + int lines_in_screen; } LINE; void init_line(LINE *s); diff --git a/page.c b/page.c index b454a09..3e3b49c 100644 --- a/page.c +++ b/page.c @@ -80,48 +80,30 @@ void expand_page(PAGE *p) } // expand_page // NOTE: This moves the cursor to the end of the displayed text -void print_page(WINDOW *win, WINDOW *lnos, const PAGE *p, int start, int end) +void print_page(WINDOW *win, WINDOW *lnos, const PAGE *p, int start) { int i, line; - for(i = start, line = 0; i < p->numlines && i < end; i++) + for(i = start, line = 0; i < p->numlines && line < EDITOR_SIZE; i++) { - - - int linesinline = strlen(p->text[i].line) / 75; - if (strlen(p->text[i].line) % 75) linesinline++; - - if (linesinline > 0) { - for (int j=0;j < linesinline; j++) { - wmove(lnos, line, 0); - wclrtoeol(lnos); - if (j == 0) { - wprintw(lnos, "%5d", i + 1); - } - wmove(win, line++, 0); - wclrtoeol(win); - wprintw(win, "%.*s", strlen(&p->text[i].line[j * 75]) < 75 ? strlen(&p->text[i].line[j * 75]) : 75, &p->text[i].line[j * 75]); - } - } else { + for (int j=0;j < p->text[i].lines_in_screen; j++) { wmove(lnos, line, 0); wclrtoeol(lnos); - wprintw(lnos, "%5d", i + 1); + if (j == 0) { + wprintw(lnos, "%5d", i + 1); + } wmove(win, line++, 0); + wprintw(win, "%.*s", strlen(&p->text[i].line[j * 75]) < 75 ? strlen(&p->text[i].line[j * 75]) : 75, &p->text[i].line[j * 75]); wclrtoeol(win); } - } - for (int j=line; j < 23; j++) { + for (int j=line; j < EDITOR_SIZE; j++) { wmove(lnos, j, 0); wclrtoeol(lnos); + wmove(win, j, 0); + wclrtoeol(win); } - if(start < end) - { - wmove(win, line, 0); - wclrtoeol(win); // if we deleted a line this may be necessary - wmove(win, line-1, 1); - } wrefresh(lnos); //refresh(); } // print_page diff --git a/page.h b/page.h index 4c38925..421a697 100644 --- a/page.h +++ b/page.h @@ -6,7 +6,7 @@ #include "line.h" #define PAGE_SIZE 500 /* Default number of lines */ -#define WIN_SIZE (LINES - 2) /* Size of window, making room for bottom prompt */ +#define EDITOR_SIZE (LINES - 2) /* Size of window, making room for bottom prompt */ #define NAME_LIMIT 256 /* Max size of a unix filename + 1 */ typedef struct @@ -24,6 +24,6 @@ void dest_page(PAGE *p); void insert_line(PAGE *p, int index); void remove_line(PAGE *p, int index); void expand_page(PAGE *p); -void print_page(WINDOW *win, WINDOW *lnos, const PAGE *p, int start, int end); +void print_page(WINDOW *win, WINDOW *lnos, const PAGE *p, int start); #endif diff --git a/text.c b/text.c index f6b9ca0..d817bd9 100644 --- a/text.c +++ b/text.c @@ -15,8 +15,6 @@ int tab_offset = 0; extern void convert_xpm(char **xpm, unsigned char **buffer); extern unsigned char *QUINN_Icon; -#define EDITOR_SIZE (WIN_SIZE - 2) - WINDOW *menu; WINDOW *editor; WINDOW *linenos; @@ -106,8 +104,8 @@ void print_loc(int x, int y) { int oldx, oldy; getyx(stdscr, oldy, oldx); - wmove(menu, 0, COLS - 20); - wprintw(menu, "X: %d Y: %d ", x, y + y_offset + 1); + wmove(menu, 0, COLS - 30); + wprintw(menu, "X: %d Y: %d Offset: %d", x + 1, y + 1, y_offset); wclrtoeol(menu); move(oldy, oldx); } @@ -157,18 +155,16 @@ int main(int argc, char *argv[]) wbkgd(linenos, COLOR_PAIR(colornum(COLOR_BLUE, COLOR_WHITE))); int beg = 0; - int end = EDITOR_SIZE; int i; update_status(&page, ""); - print_page(editor, linenos, &page, beg, end); + print_page(editor, linenos, &page, beg); wmove(editor, winy, winx); while(1) { - beg = 0 + y_offset; - end = EDITOR_SIZE + y_offset; + beg = y_offset; int ch = wgetch(editor); update_status(&page, ""); // default text switch(ch) @@ -176,7 +172,7 @@ int main(int argc, char *argv[]) case KEY_F(4): if(prompt_yesno("Are you sure you want to quit?")) goto endnc; - print_page(editor, linenos, &page, beg, end); + print_page(editor, linenos, &page, beg); break; case KEY_F(5): save_file(&page); @@ -185,7 +181,7 @@ int main(int argc, char *argv[]) case KEY_F(6): prompt_string("Save As:", page.filename, NAME_LIMIT); save_file(&page); - print_page(editor, linenos, &page, beg, end); + print_page(editor, linenos, &page, beg); update_status(&page, "Saved!"); break; case KEY_UP: @@ -216,7 +212,7 @@ int main(int argc, char *argv[]) remove_char(&page.text[page.y], page.x - 1); // delete move_left(&page); // char behind cursor } - print_page(editor, linenos, &page, beg, end); + print_page(editor, linenos, &page, beg); wmove(editor, winy, winx); page.saved = 0; break; @@ -224,21 +220,21 @@ int main(int argc, char *argv[]) for(i = 0; i < TAB_WIDTH; i++) { insert_char(&page.text[page.y], ' ', page.x); - print_page(editor, linenos, &page, beg, end); + print_page(editor, linenos, &page, beg); move_right(&page); } page.saved = 0; break; case '\n': // newline insert_line(&page, page.y + 1); - print_page(editor, linenos, &page, beg, end); + print_page(editor, linenos, &page, beg); move_down(&page); break; default: // all other chars if( isprint(ch) ) { insert_char(&page.text[page.y], ch, page.x); - print_page(editor, linenos, &page, beg, end); + print_page(editor, linenos, &page, beg); move_right(&page); page.saved = 0; } @@ -293,13 +289,7 @@ void move_left(PAGE *p) } for (int j = y_offset; j < p->y; j++) { - if (strlen(p->text[j].line) > 75) { - int linesinline = strlen(p->text[j].line) / 75; - if (strlen(p->text[j].line) % 75) linesinline++; - winy += linesinline; - } else { - winy++; - } + winy += p->text[j].lines_in_screen; } wmove(editor, winy, winx); @@ -326,9 +316,7 @@ void move_right(PAGE *p) for (int j = y_offset; j < p->y; j++) { if (strlen(p->text[j].line) > 75) { - int linesinline = strlen(p->text[j].line) / 75; - if (strlen(p->text[j].line) % 75) linesinline++; - winy += linesinline; + winy += p->text[j].lines_in_screen; } else { winy++; } @@ -340,63 +328,70 @@ void move_right(PAGE *p) void move_up(PAGE *p) { - int linesinline = 1; + if (p->y == y_offset && y_offset > 0) { + p->y--; + y_offset--; - if (p->y > 0) { - if (strlen(p->text[p->y - 1].line) / 75 >= 1) { - linesinline = strlen(p->text[p->y - 1].line) / 75; - if (strlen(p->text[p->y - 1].line) % 75) linesinline++; - } - - if( p->y - y_offset >= linesinline) - { - p->y--; - winy -= linesinline; - } - else if (y_offset > 0) - { - p->y--; - --(y_offset); - print_page(editor, linenos, p, 0 + y_offset, EDITOR_SIZE + y_offset); - } else { - p->y--; - winy -= linesinline; - } if( p->x > strlen(p->text[p->y].line) ) { // cursor adjusts p->x = strlen(p->text[p->y].line); // to smaller lines - winx = p->x; + int oldy = p->x / 75; + p->x = strlen(p->text[p->y].line); + winx = p->x % 75; + winy = winy - (oldy - p->x / 75); + } + + print_page(editor, linenos, p, y_offset); + + wmove(editor, winy, winx); + } else if (p->y > 0) { + p->y--; + winy -= p->text[p->y].lines_in_screen; + if( p->x > strlen(p->text[p->y].line) ) { // cursor adjusts + int oldy = p->x / 75; + p->x = strlen(p->text[p->y].line); + winx = p->x % 75; + winy = winy - (oldy - p->x / 75); + } + wmove(editor, winy, winx); + } +} + +void move_down(PAGE *p) +{ + if (p->y < p->numlines - 1) { + winy += p->text[p->y].lines_in_screen; + p->y++; + int new_y_offset = y_offset; + + while (1) { + int count = 0; + for (int i = new_y_offset; i <= p->y; i++) { + count += p->text[i].lines_in_screen; + } + if (count > EDITOR_SIZE) { + new_y_offset++; + } else { + break; + } + } + if (new_y_offset != y_offset) { + winy = 0; + for (int i = new_y_offset;i < p->y; i++) { + winy += p->text[i].lines_in_screen; + } + y_offset = new_y_offset; + print_page(editor, linenos, p, y_offset); + } + if( p->x > strlen(p->text[p->y].line)) { + int oldy = p->x / 75; + p->x = strlen(p->text[p->y].line); + winx = p->x % 75; + winy = winy - (oldy - p->x / 75); } wmove(editor, winy, winx); } } -void move_down(PAGE *p) -{ - int linesinline = 1; - - if (strlen(p->text[p->y].line) / 75 >= 1) { - linesinline = strlen(p->text[p->y].line) / 75; - if (strlen(p->text[p->y].line) % 75) linesinline++; - } - if( p->y - y_offset < EDITOR_SIZE - 1 && p->y -y_offset < p->numlines - 1 ) - { - p->y++; - winy += linesinline; - } - else if ( p->y < p->numlines - 1 ) - { - p->y++; - ++(y_offset); - print_page(editor, linenos, p, 0 + y_offset, EDITOR_SIZE + y_offset); - } - - if( p->x > strlen(p->text[p->y].line)) { - p->x = strlen(p->text[p->y].line); - winx = p->x; - } - wmove(editor, winy, winx); - -} /* movement */ int count_lines(FILE *fp)