📄 scrn.c
字号:
/* * Device independant TTY interface for JOE * Copyright * (C) 1992 Joseph H. Allen * * This file is part of JOE (Joe's Own Editor) */#include "types.h"int bg_text = 0; /* Background color for text */int skiptop = 0;int lines = 0;int columns = 0;int notite = 0;int usetabs = 0;int assume_color = 0;int assume_256color = 0;/* How to display characters (especially the control ones) *//* here are characters ... */unsigned char xlatc[256] = { 64, 65, 66, 67, 68, 69, 70, 71, /* 8 */ 72, 73, 74, 75, 76, 77, 78, 79, /* 16 */ 80, 81, 82, 83, 84, 85, 86, 87, /* 24 */ 88, 89, 90, 91, 92, 93, 94, 95, /* 32 */ 32, 33, 34, 35, 36, 37, 38, 39, /* 40 */ 40, 41, 42, 43, 44, 45, 46, 47, /* 48 */ 48, 49, 50, 51, 52, 53, 54, 55, /* 56 */ 56, 57, 58, 59, 60, 61, 62, 63, /* 64 */ 64, 65, 66, 67, 68, 69, 70, 71, /* 72 */ 72, 73, 74, 75, 76, 77, 78, 79, /* 80 */ 80, 81, 82, 83, 84, 85, 86, 87, /* 88 */ 88, 89, 90, 91, 92, 93, 94, 95, /* 96 */ 96, 97, 98, 99, 100, 101, 102, 103, /* 104 */ 104, 105, 106, 107, 108, 109, 110, 111, /* 112 */ 112, 113, 114, 115, 116, 117, 118, 119, /* 120 */ 120, 121, 122, 123, 124, 125, 126, 63, /* 128 */ 64, 65, 66, 67, 68, 69, 70, 71, /* 136 */ 72, 73, 74, 75, 76, 77, 78, 79, /* 144 */ 80, 81, 82, 83, 84, 85, 86, 87, /* 152 */ 88, 89, 90, 91, 92, 93, 94, 95, /* 160 */ 32, 33, 34, 35, 36, 37, 38, 39, /* 168 */ 40, 41, 42, 43, 44, 45, 46, 47, /* 176 */ 48, 49, 50, 51, 52, 53, 54, 55, /* 184 */ 56, 57, 58, 59, 60, 61, 62, 63, /* 192 */ 64, 65, 66, 67, 68, 69, 70, 71, /* 200 */ 72, 73, 74, 75, 76, 77, 78, 79, /* 208 */ 80, 81, 82, 83, 84, 85, 86, 87, /* 216 */ 88, 89, 90, 91, 92, 93, 94, 95, /* 224 */ 96, 97, 98, 99, 100, 101, 102, 103, /* 232 */ 104, 105, 106, 107, 108, 109, 110, 111, /* 240 */ 112, 113, 114, 115, 116, 117, 118, 119, /* 248 */ 120, 121, 122, 123, 124, 125, 126, 63 /* 256 */};/* ... and here their attributes */ unsigned xlata[256] = { UNDERLINE, UNDERLINE, UNDERLINE, UNDERLINE, /* 4 */ UNDERLINE, UNDERLINE, UNDERLINE, UNDERLINE, /* 8 */ UNDERLINE, UNDERLINE, UNDERLINE, UNDERLINE, /* 12 */ UNDERLINE, UNDERLINE, UNDERLINE, UNDERLINE, /* 16 */ UNDERLINE, UNDERLINE, UNDERLINE, UNDERLINE, /* 20 */ UNDERLINE, UNDERLINE, UNDERLINE, UNDERLINE, /* 24 */ UNDERLINE, UNDERLINE, UNDERLINE, UNDERLINE, /* 28 */ UNDERLINE, UNDERLINE, UNDERLINE, UNDERLINE, /* 32 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 48 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 64 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 96 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 112 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, UNDERLINE, /* 128 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 130 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 132 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 134 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 136 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 138 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 140 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 142 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 144 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 146 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 148 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 150 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 152 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 154 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 156 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 158 */ INVERSE + UNDERLINE, INVERSE + UNDERLINE, /* 160 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 164 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 168 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 172 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 176 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 180 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 184 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 188 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 192 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 196 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 200 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 204 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 208 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 212 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 216 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 220 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 224 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 228 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 232 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 236 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 240 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 244 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 248 */ INVERSE, INVERSE, INVERSE, INVERSE, /* 252 */ INVERSE, INVERSE, INVERSE, INVERSE + UNDERLINE /* 256 */};/* Set attributes */int set_attr(SCRN *t, int c){ int e; c &= ~255; /* Attributes which have gone off */ e = ((AT_MASK|FG_NOT_DEFAULT|BG_NOT_DEFAULT)&t->attrib & ~c); if (e) { /* If any attribute go off, switch them all off: fixes bug on PCs */ if (t->me) texec(t->cap, t->me, 1, 0, 0, 0, 0); else { if (t->ue) texec(t->cap, t->ue, 1, 0, 0, 0, 0); if (t->se) texec(t->cap, t->se, 1, 0, 0, 0, 0); } t->attrib = 0; } /* Attributes which have turned on */ e = (c & ~t->attrib); if (e & INVERSE) { if (t->mr) texec(t->cap, t->mr, 1, 0, 0, 0, 0); else if (t->so) texec(t->cap, t->so, 1, 0, 0, 0, 0); } if (e & UNDERLINE) if (t->us) texec(t->cap, t->us, 1, 0, 0, 0, 0); if (e & BLINK) if (t->mb) texec(t->cap, t->mb, 1, 0, 0, 0, 0); if (e & BOLD) if (t->md) texec(t->cap, t->md, 1, 0, 0, 0, 0); if (e & DIM) if (t->mh) texec(t->cap, t->mh, 1, 0, 0, 0, 0); if ((t->attrib & FG_MASK) != (c & FG_MASK)) { if (t->Sf) { int color = ((c & FG_VALUE) >> FG_SHIFT); if (t->assume_256 && color >= t->Co) { unsigned char bf[32]; joe_snprintf_1(bf,sizeof(bf),"\033[38;5;%dm",color); ttputs(bf); } else { if (t->Co & (t->Co - 1)) texec(t->cap, t->Sf, 1, color % t->Co, 0, 0, 0); else texec(t->cap, t->Sf, 1, color & (t->Co - 1), 0, 0, 0); } } } if ((t->attrib & BG_MASK) != (c & BG_MASK)) { if (t->Sb) { int color = ((c & BG_VALUE) >> BG_SHIFT); if (t->assume_256 && color >= t->Co) { unsigned char bf[32]; joe_snprintf_1(bf,sizeof(bf),"\033[48;5;%dm",color); ttputs(bf); } else { if (t->Co & (t->Co - 1)) texec(t->cap, t->Sb, 1, color % t->Co, 0, 0, 0); else texec(t->cap, t->Sb, 1, color & (t->Co - 1), 0, 0, 0); } } } t->attrib = c; return 0;}/* Output character with attributes */void outatr(struct charmap *map,SCRN *t,int *scrn,int *attrf,int xx,int yy,int c,int a){ if(map->type) if(locale_map->type) { /* UTF-8 char to UTF-8 terminal */ int wid; int uni_ctrl = 0; unsigned char buf[16]; /* Deal with control characters */ if (c<32) { c = c + '@'; a ^= UNDERLINE; } else if (c==127) { c = '?'; a ^= UNDERLINE; } else if (unictrl(c)) { a ^= UNDERLINE; uni_ctrl = 1; } if(*scrn==c && *attrf==a) return; wid = joe_wcwidth(1,c); *scrn = c; *attrf = a; if(t->ins) clrins(t); if(t->x != xx || t->y != yy) cpos(t, xx, yy); if(t->attrib != a) set_attr(t, a); if (uni_ctrl) { sprintf((char *)buf,"<%X>",c); ttputs(buf); } else { utf8_encode(buf,c); ttputs(buf); } t->x+=wid; while (wid>1) { *++scrn= -1; *++attrf= 0; --wid; } } else { /* UTF-8 char to non-UTF-8 terminal */ /* Don't convert control chars below 256 */ if ((c>=32 && c<=126) || c>=160) { if (unictrl(c)) a ^= UNDERLINE; c = from_uni(locale_map,c); if (c==-1) c = '?'; } /* Deal with control characters */ if (!joe_isprint(locale_map,c) && !(dspasis && c>=128)) { a ^= xlata[c]; c = xlatc[c]; } if(*scrn==c && *attrf==a) return; *scrn = c; *attrf = a; if(t->ins) clrins(t); if(t->x != xx || t->y != yy) cpos(t,xx,yy); if(t->attrib != a) set_attr(t,a); ttputc(c); t->x++; } else if (!locale_map->type) { /* Non UTF-8 char to non UTF-8 terminal */ /* Byte-byte Translate? */ /* Deal with control characters */ if (!joe_isprint(locale_map,c) && !(dspasis && c>=128)) { a ^= xlata[c]; c = xlatc[c]; } if (*scrn==c && *attrf==a) return; *scrn = c; *attrf = a; if(t->ins) clrins(t); if(t->x != xx || t->y != yy) cpos(t,xx,yy); if(t->attrib != a) set_attr(t,a); ttputc(c); t->x++; } else { /* Non UTF-8 char to UTF-8 terminal */ unsigned char buf[16]; int wid; /* Deal with control characters */ if (!(dspasis && c>=128) && !joe_isprint(map,c)) { a ^= xlata[c]; c = xlatc[c]; } c = to_uni(map,c); if (c == -1) c = '?'; utf8_encode(buf,c); if (*scrn == c && *attrf == a) return; wid = joe_wcwidth(0,c); *scrn = c; *attrf = a; if(t->ins) clrins(t); if(t->x != xx || t->y != yy) cpos(t, xx, yy); if(t->attrib != a) set_attr(t, a); ttputs(buf); t->x+=wid; while(wid>1) { *++scrn= -1; *++attrf= 0; --wid; } }}/* Set scrolling region */static void setregn(SCRN *t, int top, int bot){ if (!t->cs) { t->top = top; t->bot = bot; return; } if (t->top != top || t->bot != bot) { t->top = top; t->bot = bot; texec(t->cap, t->cs, 1, top, bot - 1, 0, 0); t->x = -1; t->y = -1; }}/* Enter insert mode */static void setins(SCRN *t, int x){ if (t->ins != 1 && t->im) { t->ins = 1; texec(t->cap, t->im, 1, x, 0, 0, 0); }}/* Exit insert mode */int clrins(SCRN *t){ if (t->ins != 0) { texec(t->cap, t->ei, 1, 0, 0, 0, 0); t->ins = 0; } return 0;}/* Erase from given screen coordinate to end of line */int eraeol(SCRN *t, int x, int y, int atr){ int *s, *ss, *a, *aa; int w = t->co - x - 1; /* Don't worry about last column */ if (w <= 0) return 0; s = t->scrn + y * t->co + x; a = t->attr + y * t->co + x; ss = s + w; aa = a + w; do { if (*--ss != ' ') { ++ss; break; } else if (*--aa != atr) { ++ss; ++aa; break; } } while (ss != s); if ((ss - s > 3 || s[w] != ' ' || a[w] != atr) && t->ce) { cpos(t, x, y); if(t->attrib != atr) set_attr(t, atr); texec(t->cap, t->ce, 1, 0, 0, 0, 0); msetI(s, ' ', w); msetI(a, atr, w); } else if (s != ss) { if (t->ins) clrins(t); if (t->x != x || t->y != y) cpos(t, x, y); if (t->attrib != atr) set_attr(t, atr); while (s != ss) { *s = ' '; *a = atr; ttputc(' '); ++t->x; ++s; ++a; } } return 0;}/* As above but useable in insert mode *//* The cursor position must already be correct */static void outatri(SCRN *t, int x, int y, int c, int a){/* if (c == -1) c = ' '; if (a != t->attrib) set_attr(t, a); if (t->haz && c == '~') c = '\\'; utf8_putc(c); t->x+=joe_wcwidth(1,c);*/ /* ++t->x; */}static void out(unsigned char *t, unsigned char c){ ttputc(c);}SCRN *nopen(CAP *cap){ SCRN *t = (SCRN *) joe_malloc(sizeof(SCRN)); int x, y; ttopen(); t->cap = cap; setcap(cap, baud, out, NULL); t->li = getnum(t->cap,USTR "li"); if (t->li < 1) t->li = 24; t->co = getnum(t->cap,USTR "co"); if (t->co < 2) t->co = 80; x = y = 0; ttgtsz(&x, &y); if (x > 7 && y > 3) { t->li = y; t->co = x; } t->haz = getflag(t->cap,USTR "hz"); t->os = getflag(t->cap,USTR "os"); t->eo = getflag(t->cap,USTR "eo"); if (getflag(t->cap,USTR "hc")) t->os = 1; if (t->os || getflag(t->cap,USTR "ul")) t->ul = 1; else t->ul = 0; t->xn = getflag(t->cap,USTR "xn"); t->am = getflag(t->cap,USTR "am"); if (notite) t->ti = 0; else t->ti = jgetstr(t->cap,USTR "ti"); t->cl = jgetstr(t->cap,USTR "cl"); t->cd = jgetstr(t->cap,USTR "cd"); if (notite) t->te = 0; else t->te = jgetstr(t->cap,USTR "te"); t->ut = getflag(t->cap,USTR "ut"); t->Sb = jgetstr(t->cap,USTR "AB"); if (!t->Sb) t->Sb = jgetstr(t->cap,USTR "Sb"); t->Sf = jgetstr(t->cap,USTR "AF"); if (!t->Sf) t->Sf = jgetstr(t->cap,USTR "Sf"); t->Co = getnum(t->cap,USTR "Co"); if (t->Co == -1) t->Co = 8; t->mb = NULL; t->md = NULL; t->mh = NULL; t->mr = NULL; t->avattr = 0; if (!(t->me = jgetstr(t->cap,USTR "me"))) goto oops; if ((t->mb = jgetstr(t->cap,USTR "mb"))) t->avattr |= BLINK; if ((t->md = jgetstr(t->cap,USTR "md"))) t->avattr |= BOLD; if ((t->mh = jgetstr(t->cap,USTR "mh"))) t->avattr |= DIM; if ((t->mr = jgetstr(t->cap,USTR "mr"))) t->avattr |= INVERSE; oops: if (assume_color || assume_256color) { /* Install 8 color support if it looks like an ansi terminal (it has bold which begins with ESC [) */#ifndef TERMINFO if (!t->Sf && t->md && t->md[0]=='\\' && t->md[1]=='E' && t->md[2]=='[') { t->ut = 1; t->Sf = USTR "\\E[3%dm"; t->Sb = USTR "\\E[4%dm"; t->Co = 8; }#else if (!t->Sf && t->md && t->md[0]=='\033' && t->md[1]=='[') { t->ut = 1; t->Sf = USTR "\033[3%p1%dm"; t->Sb = USTR "\033[4%p1%dm"; }#endif } t->assume_256 = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -