diff options
author | KatolaZ <katolaz@freaknet.org> | 2019-07-30 12:15:54 +0100 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2019-07-30 12:15:54 +0100 |
commit | eebc645dee0d15871d6cc46f156d424cd916b191 (patch) | |
tree | 82bb51d04d05a3cf1b4937e2cefb70cc282e9993 /screen.c | |
parent | a99759398841d86928c7ad4d8248f907765cbeb2 (diff) |
yank buffer and initial copy/cut/paste support
Diffstat (limited to 'screen.c')
-rw-r--r-- | screen.c | 117 |
1 files changed, 38 insertions, 79 deletions
@@ -1,9 +1,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <ctype.h> #include <termios.h> #include <sys/ioctl.h> +#include <ctype.h> #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<HEIGHT;i++){ - fprintf(stdout,"%s\n",screen[i].s); + fprintf(stdout,"%s\n",screen.l[i].s); } status_bar(); show_cursor(); @@ -435,14 +389,15 @@ void init_screen(){ WIDTH=80; HEIGHT=24; } - screen = malloc(HEIGHT * sizeof(line_t)); - num_lines = HEIGHT; - if (screen == NULL){ + screen.l = malloc(HEIGHT * sizeof(line_t)); + screen.sz = HEIGHT; + screen.num = HEIGHT; + if (screen.l == NULL){ perror("allocating screen"); exit(1); } for (i=0; i<HEIGHT; i++){ - alloc_line(i); + alloc_line(&(screen.l[i])); } hlines_sz= sizeof(hlines) -1; vlines_sz= sizeof(vlines) -1; @@ -450,6 +405,9 @@ void init_screen(){ stmarks_sz = sizeof(st_marks) - 1; endmarks_sz = sizeof(st_marks) - 1; reset_styles(); + cutbuf.sz = 0; + cutbuf.l = NULL; + cutbuf.num = 0; } void find_nonblank_rect(int *x1, int *y1, int *x2, int *y2){ @@ -457,22 +415,22 @@ void find_nonblank_rect(int *x1, int *y1, int *x2, int *y2){ int i, j; int first; *x1= WIDTH; /** FIXME: replace with num_cols **/ - *y1 = num_lines; + *y1 = screen.num; *x2 = *y2 = 0; - for (i=0; i<num_lines; i++){ - if (screen[i].lst < 0) + for (i=0; i<screen.num; i++){ + if (screen.l[i].lst < 0) continue; *y2 = i; if (i < *y1) *y1 = i; j = 0; - while((j <= screen[i].lst) && isblank(first=screen[i].s[j])) + while((j <= screen.l[i].lst) && isblank(first=screen.l[i].s[j])) j++; if (j < *x1) *x1 = j; - j = screen[i].lst; - while(isblank(screen[i].s[j])) + j = screen.l[i].lst; + while(isblank(screen.l[i].s[j])) j--; if (j > *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(); } + |