From eebc645dee0d15871d6cc46f156d424cd916b191 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Tue, 30 Jul 2019 12:15:54 +0100 Subject: yank buffer and initial copy/cut/paste support --- screen.c | 117 +++++++++++++++++++++------------------------------------------ 1 file changed, 38 insertions(+), 79 deletions(-) (limited to 'screen.c') diff --git a/screen.c b/screen.c index 965d440..3cbfe12 100644 --- a/screen.c +++ b/screen.c @@ -1,9 +1,9 @@ #include #include #include -#include #include #include +#include #include "gramscii.h" #include "config.h" @@ -62,7 +62,7 @@ void status_bar(){ else printf(" *%s*", fname ); #ifdef DEBUG - printf(" '%d' ", screen[y].s[x]); + printf(" '%d' ", screen.l[y].s[x]); #endif printf("\033[0m"); fflush(stdout); @@ -109,52 +109,6 @@ int is_yes(char c){ /*** Screen management ***/ -void ensure_line_length(int i, int len){ - char *tmp; - - if (screen[i].sz < len + 1){ - tmp = realloc(screen[i].s, (len+1) * 2 * sizeof(char)); - if (!tmp){ - fprintf(stderr, "Unable to allocate string\n"); - exit(1); - } - screen[i].s = tmp; - screen[i].sz = (len + 1) * 2; - } -} - - -void alloc_line(int i){ - char *tmp; - - screen[i].sz = WIDTH+1; - tmp = malloc((screen[i].sz) * sizeof(char)); - if (tmp == NULL){ - fprintf(stderr, "unable to allocate line %d\n", i+1); - exit(1); - } - screen[i].s = tmp; - memset(screen[i].s, BG, screen[i].sz); - screen[i].lst = -1; - screen[i].s[0]='\0'; -} - -void ensure_num_lines(int n){ - line_t *tmp; - - if (n > num_lines){ - tmp = realloc(screen, (n + LONG_STEP) * sizeof(line_t)); - if (tmp == NULL){ - fprintf(stderr, "Unable to allocate memory for more lines"); - exit(1); - } - else while ( num_lines < n + LONG_STEP){ - alloc_line(num_lines); - num_lines ++; - } - } -} - void show_cursor(){ if (silent) @@ -165,15 +119,15 @@ void show_cursor(){ void set_xy(int _x, int _y, char c){ - ensure_num_lines(_y + 1); - ensure_line_length(_y, _x + 1); - while (screen[_y].lst<_x){ - screen[_y].lst ++; - screen[_y].s[screen[_y].lst] = BG; + ensure_num_lines(&screen, _y + 1); + ensure_line_length(&(screen.l[_y]), _x + 1); + while (screen.l[_y].lst<_x){ + screen.l[_y].lst ++; + screen.l[_y].s[screen.l[_y].lst] = BG; } - screen[_y].s[_x] = c; - if (_x == screen[_y].lst) - screen[_y].s[_x+1] = '\0'; + screen.l[_y].s[_x] = c; + if (_x == screen.l[_y].lst) + screen.l[_y].s[_x+1] = '\0'; } void set_cur(char c){ @@ -193,7 +147,7 @@ void update_current(){ if (silent) return; printf("\033[%d'%df",y+1,x+1); - putchar(screen[y].s[x]); + putchar(screen.l[y].s[x]); fflush(stdout); } @@ -206,20 +160,20 @@ void erase_blank_lines(int y1, int y2){ } for (; y1 <= y2; y1++){ - j = screen[y1].lst; - while (j>=0 && isblank(screen[y1].s[j])) + j = screen.l[y1].lst; + while (j>=0 && isblank(screen.l[y1].s[j])) j--; if (j<0){ - screen[y1].lst = -1; - screen[y1].s[0] = '\0'; + screen.l[y1].lst = -1; + screen.l[y1].s[0] = '\0'; } } } void erase_line(int i){ - screen[i].lst = -1; - screen[i].s[0] = '\0'; + screen.l[i].lst = -1; + screen.l[i].s[0] = '\0'; } void erase_box(int x1, int y1, char c){ @@ -268,7 +222,7 @@ void redraw(){ return; printf("\033[2J\033[1;1H"); for (i=0;i *x2) *x2 = j; @@ -483,13 +441,13 @@ void crop_to_rect(int x1, int y1, int x2, int y2){ int i; for (i=0; i<= y2-y1; i ++){ - ensure_line_length(i, screen[i+y1].lst); - sprintf(screen[i].s, "%s", screen[i+y1].s + x1); - screen[i].lst = screen[i+y1].lst - x1; + ensure_line_length(&(screen.l[i]), screen.l[i+y1].lst); + sprintf(screen.l[i].s, "%s", screen.l[i+y1].s + x1); + screen.l[i].lst = screen.l[i+y1].lst - x1; } while (i<=y2){ - screen[i].lst = -1; - screen[i].s[0]= '\0'; + screen.l[i].lst = -1; + screen.l[i].s[0]= '\0'; i ++; } } @@ -505,3 +463,4 @@ void crop_to_nonblank(){ redraw(); } + -- cgit v1.2.3