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

13
line.c
View File

@ -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,6 +20,12 @@ 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
@ -29,6 +36,12 @@ void remove_char(LINE *s, int index)
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

1
line.h
View File

@ -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);

30
page.c
View File

@ -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++) {
for (int j=0;j < p->text[i].lines_in_screen; 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);
wclrtoeol(lnos);
wprintw(lnos, "%5d", i + 1);
wmove(win, line++, 0);
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

4
page.h
View File

@ -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

123
text.c
View File

@ -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,62 +328,69 @@ 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)
{
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 )
{
if (p->y < p->numlines - 1) {
winy += p->text[p->y].lines_in_screen;
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);
}
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;
winx = p->x % 75;
winy = winy - (oldy - p->x / 75);
}
wmove(editor, winy, winx);
wmove(editor, winy, winx);
}
}
/* movement */