Made it work!

This commit is contained in:
Andrew Pamment 2021-12-21 21:15:08 +10:00
parent 23cfeba63c
commit da12067099
5 changed files with 98 additions and 107 deletions

21
line.c
View File

@ -5,6 +5,7 @@ void init_line(LINE *s)
s->size = LINE_SIZE; s->size = LINE_SIZE;
s->line = (char *)malloc(LINE_SIZE * sizeof(char)); s->line = (char *)malloc(LINE_SIZE * sizeof(char));
s->line[0] = '\0'; s->line[0] = '\0';
s->lines_in_screen = 1;
} // init_line } // init_line
@ -19,16 +20,28 @@ void insert_char(LINE *s, char c, int index)
s->line[i + 1] = s->line[i]; s->line[i + 1] = s->line[i];
s->line[index] = c; 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 } // insert
void remove_char(LINE *s, int index) void remove_char(LINE *s, int index)
{ {
int i; int i;
int len = strlen(s->line); int len = strlen(s->line);
for(i = index; i < len; i++) for(i = index; i < len; i++)
s->line[i] = s->line[i + 1]; 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 } // remove_char

1
line.h
View File

@ -12,6 +12,7 @@ typedef struct
{ {
char *line; char *line;
int size; // size of array, not string int size; // size of array, not string
int lines_in_screen;
} LINE; } LINE;
void init_line(LINE *s); void init_line(LINE *s);

38
page.c
View File

@ -80,48 +80,30 @@ void expand_page(PAGE *p)
} // expand_page } // expand_page
// NOTE: This moves the cursor to the end of the displayed text // 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; 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++)
{ {
for (int j=0;j < p->text[i].lines_in_screen; j++) {
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 {
wmove(lnos, line, 0); wmove(lnos, line, 0);
wclrtoeol(lnos); wclrtoeol(lnos);
wprintw(lnos, "%5d", i + 1); if (j == 0) {
wprintw(lnos, "%5d", i + 1);
}
wmove(win, line++, 0); 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); wclrtoeol(win);
} }
} }
for (int j=line; j < 23; j++) { for (int j=line; j < EDITOR_SIZE; j++) {
wmove(lnos, j, 0); wmove(lnos, j, 0);
wclrtoeol(lnos); 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); wrefresh(lnos);
//refresh(); //refresh();
} // print_page } // print_page

4
page.h
View File

@ -6,7 +6,7 @@
#include "line.h" #include "line.h"
#define PAGE_SIZE 500 /* Default number of lines */ #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 */ #define NAME_LIMIT 256 /* Max size of a unix filename + 1 */
typedef struct typedef struct
@ -24,6 +24,6 @@ void dest_page(PAGE *p);
void insert_line(PAGE *p, int index); void insert_line(PAGE *p, int index);
void remove_line(PAGE *p, int index); void remove_line(PAGE *p, int index);
void expand_page(PAGE *p); 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 #endif

141
text.c
View File

@ -15,8 +15,6 @@ int tab_offset = 0;
extern void convert_xpm(char **xpm, unsigned char **buffer); extern void convert_xpm(char **xpm, unsigned char **buffer);
extern unsigned char *QUINN_Icon; extern unsigned char *QUINN_Icon;
#define EDITOR_SIZE (WIN_SIZE - 2)
WINDOW *menu; WINDOW *menu;
WINDOW *editor; WINDOW *editor;
WINDOW *linenos; WINDOW *linenos;
@ -106,8 +104,8 @@ void print_loc(int x, int y)
{ {
int oldx, oldy; int oldx, oldy;
getyx(stdscr, oldy, oldx); getyx(stdscr, oldy, oldx);
wmove(menu, 0, COLS - 20); wmove(menu, 0, COLS - 30);
wprintw(menu, "X: %d Y: %d ", x, y + y_offset + 1); wprintw(menu, "X: %d Y: %d Offset: %d", x + 1, y + 1, y_offset);
wclrtoeol(menu); wclrtoeol(menu);
move(oldy, oldx); move(oldy, oldx);
} }
@ -157,18 +155,16 @@ int main(int argc, char *argv[])
wbkgd(linenos, COLOR_PAIR(colornum(COLOR_BLUE, COLOR_WHITE))); wbkgd(linenos, COLOR_PAIR(colornum(COLOR_BLUE, COLOR_WHITE)));
int beg = 0; int beg = 0;
int end = EDITOR_SIZE;
int i; int i;
update_status(&page, ""); update_status(&page, "");
print_page(editor, linenos, &page, beg, end); print_page(editor, linenos, &page, beg);
wmove(editor, winy, winx); wmove(editor, winy, winx);
while(1) while(1)
{ {
beg = 0 + y_offset; beg = y_offset;
end = EDITOR_SIZE + y_offset;
int ch = wgetch(editor); int ch = wgetch(editor);
update_status(&page, ""); // default text update_status(&page, ""); // default text
switch(ch) switch(ch)
@ -176,7 +172,7 @@ int main(int argc, char *argv[])
case KEY_F(4): case KEY_F(4):
if(prompt_yesno("Are you sure you want to quit?")) if(prompt_yesno("Are you sure you want to quit?"))
goto endnc; goto endnc;
print_page(editor, linenos, &page, beg, end); print_page(editor, linenos, &page, beg);
break; break;
case KEY_F(5): case KEY_F(5):
save_file(&page); save_file(&page);
@ -185,7 +181,7 @@ int main(int argc, char *argv[])
case KEY_F(6): case KEY_F(6):
prompt_string("Save As:", page.filename, NAME_LIMIT); prompt_string("Save As:", page.filename, NAME_LIMIT);
save_file(&page); save_file(&page);
print_page(editor, linenos, &page, beg, end); print_page(editor, linenos, &page, beg);
update_status(&page, "Saved!"); update_status(&page, "Saved!");
break; break;
case KEY_UP: case KEY_UP:
@ -216,7 +212,7 @@ int main(int argc, char *argv[])
remove_char(&page.text[page.y], page.x - 1); // delete remove_char(&page.text[page.y], page.x - 1); // delete
move_left(&page); // char behind cursor move_left(&page); // char behind cursor
} }
print_page(editor, linenos, &page, beg, end); print_page(editor, linenos, &page, beg);
wmove(editor, winy, winx); wmove(editor, winy, winx);
page.saved = 0; page.saved = 0;
break; break;
@ -224,21 +220,21 @@ int main(int argc, char *argv[])
for(i = 0; i < TAB_WIDTH; i++) for(i = 0; i < TAB_WIDTH; i++)
{ {
insert_char(&page.text[page.y], ' ', page.x); insert_char(&page.text[page.y], ' ', page.x);
print_page(editor, linenos, &page, beg, end); print_page(editor, linenos, &page, beg);
move_right(&page); move_right(&page);
} }
page.saved = 0; page.saved = 0;
break; break;
case '\n': // newline case '\n': // newline
insert_line(&page, page.y + 1); insert_line(&page, page.y + 1);
print_page(editor, linenos, &page, beg, end); print_page(editor, linenos, &page, beg);
move_down(&page); move_down(&page);
break; break;
default: // all other chars default: // all other chars
if( isprint(ch) ) if( isprint(ch) )
{ {
insert_char(&page.text[page.y], ch, page.x); 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); move_right(&page);
page.saved = 0; page.saved = 0;
} }
@ -293,13 +289,7 @@ void move_left(PAGE *p)
} }
for (int j = y_offset; j < p->y; j++) { for (int j = y_offset; j < p->y; j++) {
if (strlen(p->text[j].line) > 75) { winy += p->text[j].lines_in_screen;
int linesinline = strlen(p->text[j].line) / 75;
if (strlen(p->text[j].line) % 75) linesinline++;
winy += linesinline;
} else {
winy++;
}
} }
wmove(editor, winy, winx); wmove(editor, winy, winx);
@ -326,9 +316,7 @@ void move_right(PAGE *p)
for (int j = y_offset; j < p->y; j++) { for (int j = y_offset; j < p->y; j++) {
if (strlen(p->text[j].line) > 75) { if (strlen(p->text[j].line) > 75) {
int linesinline = strlen(p->text[j].line) / 75; winy += p->text[j].lines_in_screen;
if (strlen(p->text[j].line) % 75) linesinline++;
winy += linesinline;
} else { } else {
winy++; winy++;
} }
@ -340,63 +328,70 @@ void move_right(PAGE *p)
void move_up(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 if( p->x > strlen(p->text[p->y].line) ) { // cursor adjusts
p->x = strlen(p->text[p->y].line); // to smaller lines 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); 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 */ /* movement */
int count_lines(FILE *fp) int count_lines(FILE *fp)