#define _POSIX_C_SOURCE 200112L #include #include #include #include #include #include #include "gramscii.h" #include "config.h" /** extern declarations **/ extern lineset_t screen; /* what is visualised */ extern lineset_t cutbuf; /* cut/paste buffer */ extern lineset_t *undo; /* undo list */ extern pos_t marks[26]; /* position marks */ extern char mark_map[26]; /* marks map */ extern int undo_sz;/* allocated size of undo list*/ extern int undo_cur;/* undo position */ extern int undo_lst;/* last valid undo position */ extern int WIDTH, HEIGHT; extern int mode;/* mode */ extern int dir;/* line direction */ extern int x; extern int y; extern int step;/* current step */ extern int mult;/* current multiplier */ extern char corner; /* number of available markers for each type */ extern int hlines_sz; extern int vlines_sz; extern int corners_sz; extern int stmarks_sz; extern int endmarks_sz; /**/ /* line and arrow markers */ extern int cur_hl, cur_vl, cur_corn, cur_start, cur_end; extern char line_h; extern char line_v; extern char mark_st; extern char mark_end; /**/ extern char modified; /* set to 1 if screen modified since last save */ extern char fname[256]; extern char script; /* set to 1 in script-mode */ extern struct termios t2, t3; /*** screen management functions ***/ /*** _isblank ***/ int _isblank(int c){ return c==32 || c==9 ? 1 : 0; } /*** Status bar ***/ char* mode_str(){ switch(mode){ case MOVE: return "mov"; case TEXT: return "txt"; case BOX: return "box"; case ARROW: return "arr"; case DEL: return "del"; case VIS: return "vis"; case PAR: return "par"; case REM: return "rem"; case TRP: return "trp"; default: return "ERR"; } return "ERR"; } char get_mark(char dir){ switch(dir){ case DIR_U: return '^'; case DIR_D: return 'v'; case DIR_L: return '<'; case DIR_R: return '>'; } return '>'; } void status_bar(){ if (script) return; printf("\033[%d;1f\033[7m", HEIGHT+1); printf("%*s", WIDTH-1, ""); printf("\033[%d;1f\033[7m", HEIGHT+1); printf(" x:%3d y:%3d -- MODE:%4s HL:%c VL:%c CN:%c SP:%c EP:%c %10s", x, y, mode_str(), line_h, line_v, corner, mark_st, mark_end, ""); if (!modified) printf(" [%s]", fname ); else printf(" *%s*", fname ); #ifdef DEBUG printf(" '%d' ", screen.l[y].s[x]); #endif printf("\033[0m"); fflush(stdout); } char get_key(FILE *fc, char *msg){ if (script) return 0; printf("\033[%d;1f\033[7m", HEIGHT+1); printf("%*s", WIDTH, ""); printf("\033[%d;1f\033[7m", HEIGHT+1); printf("%s", msg); fflush(stdout); printf("\033[0m"); fflush(stdout); return fgetc(fc); } void get_string(FILE *fc, char *msg, char *s, int sz){ if (!script){ printf("\033[%d;1f\033[7m", HEIGHT+1); printf("%*s", WIDTH, ""); printf("\033[%d;1f\033[7m", HEIGHT+1); /* We must activate echo now */ t3 = t2; t3.c_lflag |= (ECHO | ICANON); tcsetattr(0, TCSANOW, &t3); printf("%s", msg); printf("\033[0m"); } fgets(s, sz, fc); s[strlen(s)-1] = '\0'; if (!script){ tcsetattr(0, TCSANOW, &t2); fflush(stdout); } } int is_yes(char c){ return c=='y' ? 1 : c == 'Y'? 1 : 0; } /*** Screen management ***/ void show_cursor(){ if (script) return; printf("\033[%d;%df", y+1, x+1); fflush(stdout); } void set_xy(int _x, int _y, char c){ 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.l[_y].s[_x] = c; if (_x == screen.l[_y].lst) screen.l[_y].s[_x+1] = '\0'; } void set_cur(char c){ set_xy(x, y, c); } void draw_xy(int x, int y, char c){ /* FIXME: check if x and y are valid!!!! */ if (script) return; printf("\033[%d;%df",y+1,x+1); putchar(c); fflush(stdout); } void update_current(){ if (script) return; printf("\033[%d;%df",y+1,x+1); putchar(screen.l[y].s[x]); fflush(stdout); } void erase_blank_lines(int y1, int y2){ int j; if (y1 > y2){ y1 ^= y2; y2 ^= y1; y1 ^= y2; } for (; y1 <= y2; y1++){ j = screen.l[y1].lst; while (j>=0 && _isblank(screen.l[y1].s[j])) j--; if (j<0){ screen.l[y1].lst = -1; screen.l[y1].s[0] = '\0'; } } } void erase_line(int i){ screen.l[i].lst = -1; screen.l[i].s[0] = '\0'; } void erase_box(int x1, int y1, char c){ int x_incr, y_incr, i; x_incr = x1 < x? +1: -1; y_incr = y1 < y? +1: -1; do{ i = y1; do{ set_xy(x1, i, c); } while(i != y && (1 | (i += y_incr))); } while(x1 != x && (1 | (x1 += x_incr))); } void erase_screen(){ int i; for(i=0;i=WIDTH) *x = WIDTH-1; if (*y<0) *y=0; else if (*y>=HEIGHT) *y = HEIGHT -1; } void reset_styles(){ cur_corn = 0; corner = corners[0]; cur_hl = cur_vl = 0; cur_start = cur_end = 0; line_h = hlines[cur_hl]; line_v = vlines[cur_vl]; mark_st = st_marks[cur_start]; mark_end = end_marks[cur_end]; } void redraw(){ int i; if (script) return; printf("\033[2J\033[1;1H"); for (i=0;i *x2) *x2 = j; } } void crop_to_rect(int x1, int y1, int x2, int y2){ int i; for (i=0; i<= y2-y1; i ++){ ensure_line_length(&(screen.l[i]), screen.l[i+y1].lst+1); sprintf(screen.l[i].s, "%s", screen.l[i+y1].s + x1); screen.l[i].lst = x2 - x1; screen.l[i].s[screen.l[i].lst + 1] = '\0'; } while (i< HEIGHT){ screen.l[i].lst = -1; screen.l[i].s[0]= '\0'; i ++; } } void crop_to_nonblank(){ int x1, x2, y1, y2; find_nonblank_rect(&x1, &y1, &x2, &y2); #ifdef DEBUG fprintf(stderr, "crop rectangle: (%d, %d)-(%d, %d)\n", x1, y1, x2, y2); #endif copy_lines_to_ring(0, y2, PRV_STATE); crop_to_rect(x1, y1, x2, y2); copy_lines_to_ring(0, y2, NEW_STATE); modified=1; redraw(); } /** position marks **/ void mark_pos(FILE *fc){ char c; c = tolower(fgetc(fc)); if (isalpha(c)){ marks[c - 'a'].x = x; marks[c - 'a'].y = y; mark_map[c - 'a'] = 1; #ifdef DEBUG fprintf(stderr, "marking pos (%d, %d) as '%c'\n", x, y, c); #endif } }