+/Femto
toy text editor
git clone https://git.julia.blue/+/Femto
973c5b9
master
1#include "../inc/display.h" 2struct editorSyntax HL_SETTINGS [NUM_SYNTAX ]; 3 4int ENABLE_LINE_NUMS = 1 ; 5int LINE_NUM_WIDTH = 0 ; 6int MESSAGE_TIMER = 3 ; 7 8char * ckw []= { 9"auto" ,"break" ,"case" ,"char" ,"const" ,"continue" , 10"default" ,"do" ,"double" ,"else" ,"enum" ,"extern" , 11"float" ,"for" ,"goto" ,"if" ,"int" ,"long" , 12"register" ,"return" ,"short" ,"signed" ,"sizeof" ,"static" , 13"struct" ,"switch" ,"typedef" ,"union" ,"unsigned" ,"void" , 14"volatile" ,"while" ,"getchar|" ,"putchar|" ,"printf|" ,"scanf|" , 15"strcat|" ,"strcmp|" ,"strcpy|" ,"isdigit|" ,"isalpha|" ,"isalnum|" , 16"islower|" ,"isupper|" ,"acos|" ,"asin|" ,"atan|" ,"cos|" , 17"exp|" ,"fabs|" ,"sqrt|" ,"time|" ,"difftime|" ,"clock|" , 18"malloc|" ,"realloc|" ,"rand|" ,"srand|" ,NULL }; 19 20char * pykw []= { 21"and" ,"assert" ,"break" ,"class" ,"continue" ,"def" , 22"del" ,"elif" ,"else" ,"except" ,"exec" ,"finally" , 23"for" ,"from" ,"global" ,"if" ,"import" ,"in" , 24"is" ,"lambda" ,"not" ,"or" ,"pass" ,"print" , 25"raise" ,"return" ,"try" ,"while" ,"abs|" ,"all|" , 26"any|" ,"bin|" ,"bool|" ,"char|" ,"cmp|" ,"dict|" , 27"dir|" ,"divmod|" ,"enumerate|" ,"eval|" ,"file|" ,"filter|" , 28"float|" ,"format|" ,"hash|" ,"help|" ,"hex|" ,"id|" , 29"input|" ,"int|" ,"iter|" ,"len|" ,"list|" ,"long|" , 30"map|" ,"max|" ,"min|" ,"next|" ,"oct|" ,"open|" , 31"ord|" ,"pow|" ,"print|" ,"range|" ,"raw_input|" ,"reduce|" , 32"repr|" ,"reversed|" ,"set|" ,"slice|" ,"sorted|" ,"str|" , 33"sum|" ,"super|" ,"tuple|" ,"type|" ,"unicode|" ,"xrange|" , 34"zip|" ,"__import__|" ,NULL }; 35 36void initDisplay () { 37struct editorSyntax * s = & HL_SETTINGS [0 ]; 38s -> filetype = "C" ; 39s -> filematch = "c h cpp" ; 40s -> slCommentStart = "//" ; 41s -> mlCommentStart = "/*" ; 42s -> mlCommentEnd = "*/" ; 43s -> keywords = ckw ; 44s -> flags = SETTING_NUMBER |SETTING_STRING ; 45s = & HL_SETTINGS [1 ]; 46s -> filetype = "Python" ; 47s -> filematch = "py" ; 48s -> slCommentStart = "#" ; 49s -> mlCommentStart = NULL ; 50s -> mlCommentEnd = NULL ; 51s -> keywords = pykw ; 52s -> flags = SETTING_NUMBER |SETTING_STRING ; 53} 54 55int is_separator (int c ) { 56return isspace (c )|| c == '\0' || strchr (",.()+-/*=~%<>[];" ,c )!= NULL ; 57} 58 59void renderRow (struct erow * row ) { 60row -> hl = realloc (row -> hl ,row -> size ); 61memset (row -> hl ,HL_NORMAL ,row -> size ); 62if (E .syntax == NULL ) {return ; } 63 64int sep = 1 ; 65char instr = 0 ; 66int incom = row -> index > 0 && E .row [row -> index - 1 ].unclosed ; 67char * scs = E .syntax -> slCommentStart ; 68char * mcs = E .syntax -> mlCommentStart ; 69char * mce = E .syntax -> mlCommentEnd ; 70int scslen = scs ?strlen (scs ) :0 ; 71int mcslen = mcs ?strlen (mcs ) :0 ; 72int mcelen = mce ?strlen (mcs ) :0 ; 73char ** kw = E .syntax -> keywords ; 74int i = 0 ; 75while (i < row -> size ) { 76char c = row -> text [i ]; 77char prev = i == 0 ?HL_NORMAL :row -> hl [i - 1 ]; 78 79if (scslen && !instr && !incom ) { 80if (!strncmp (& row -> text [i ],scs ,scslen )) { 81memset (& row -> hl [i ],HL_COMMENT ,row -> size - i ); 82break ; 83 } 84 } 85 86if (mcslen && mcelen && !instr ) { 87if (incom ) { 88row -> hl [i ]= HL_MLCOMMENT ; 89if (!strncmp (mce ,& row -> text [i ],mcelen )) { 90memset (& row -> hl [i ],HL_MLCOMMENT ,mcelen ); 91i += mcelen ; 92incom = 0 ; 93sep = 1 ; 94continue ; 95 }else { 96++ i ; 97continue ; 98 } 99 }else if (!strncmp (mcs ,& row -> text [i ],mcslen )) { 100memset (& row -> hl [i ],HL_MLCOMMENT ,mcslen ); 101i += mcslen ; 102incom = 1 ; 103continue ; 104 } 105 } 106 107if (E .syntax -> flags & SETTING_STRING ) { 108if (instr ) { 109if (c == '\\' ) { 110row -> hl [i ]= HL_ESCAPE ; 111row -> hl [i + 1 ]= HL_ESCAPE ; 112i += 2 ; 113continue ; 114 } 115if (c == instr ) { 116instr = 0 ; 117sep = 1 ; 118 } 119row -> hl [i ]= HL_STRING ; 120++ i ; 121continue ; 122 }else { 123if (c == '"' || c == '\'' ) { 124instr = c ; 125row -> hl [i ]= HL_STRING ; 126++ i ; 127continue ; 128 } 129 } 130 } 131 132if (E .syntax -> flags & SETTING_NUMBER ) { 133if ((isdigit (c )&& (sep || prev == HL_NUMBER ))|| 134 (prev == HL_NUMBER && c == '.' )) { 135row -> hl [i ]= HL_NUMBER ; 136sep = 0 ; 137++ i ; 138continue ; 139 } 140 } 141 142if (sep ) { 143int j ; 144for (j = 0 ;kw [j ];++ j ) { 145int kwlen = strlen (kw [j ]); 146int kw2 = kw [j ][kwlen - 1 ]== '|' ; 147kwlen -= kw2 ; 148if (!strncmp (& row -> text [i ],kw [j ],kwlen )&& 149is_separator (row -> text [i + kwlen ])) { 150memset (& row -> hl [i ],kw2 ?HL_KEYWORD_2 :HL_KEYWORD_1 , 151kwlen ); 152i += kwlen ; 153break ; 154 } 155 } 156if (kw [j ]!= NULL ) {continue ; } 157 } 158 159sep = is_separator (c ); 160++ i ; 161 } 162 163int changed = incom != row -> unclosed ; 164row -> unclosed = incom ; 165if (changed && row -> index + 1 < E .numrows ) { 166renderRow (& E .row [row -> index + 1 ]); 167 } 168} 169 170int getColor (int hl ) { 171switch (hl ) { 172case HL_NUMBER :return 31 ; 173case HL_MATCH :return 34 ; 174case HL_STRING :return 35 ; 175case HL_ESCAPE :return 34 ; 176case HL_COMMENT :return 36 ; 177case HL_MLCOMMENT :return 36 ; 178case HL_KEYWORD_1 :return 32 ; 179case HL_KEYWORD_2 :return 33 ; 180default :return 37 ; 181 } 182} 183 184void scroll () { 185if (E .cy < E .scrollRow ) {E .scrollRow = E .cy ; } 186if (E .cy >=E .scrollRow + E .height ) {E .scrollRow = (E .cy - E .height )+ 1 ; } 187if (E .cx < E .scrollCol ) {E .scrollCol = E .cx ; } 188if (E .cx >=E .scrollCol + E .width - LINE_NUM_WIDTH ) { 189E .scrollCol = (E .cx - (E .width - LINE_NUM_WIDTH ))+ 1 ; 190 } 191} 192 193void drawWelcome (abuf * ab ) { 194char welcome []= "Welcome to Femto!" ; 195int welcomelen = sizeof (welcome ); 196if (welcomelen > E .width ) {welcomelen = E .width ; } 197int padding = (E .width - welcomelen ) /2 ; 198if (padding ) { 199abappend (ab ,"~" ,1 ); 200-- padding ; 201while (padding ) { 202abappend (ab ," " ,1 ); 203-- padding ; 204 } 205 } 206abappend (ab ,welcome ,welcomelen ); 207} 208 209void drawStatus (abuf * ab ) { 210abappend (ab ,"\x1b[7m" ,4 ); 211char status [80 ],rstatus [80 ]; 212char * temp ; 213if (E .filename ) { 214temp = & E .filename [strlen (E .filename )- 20 ]; 215if (strlen (E .filename )< 20 ) {temp = E .filename ; } 216 } 217snprintf (status ,sizeof (status ),"%s - %d lines %s" , 218E .filename ?temp :"[No Name]" ,E .numrows , 219E .dirty ?"(modified)" :"" ); 220int len = strlen (status ); 221int rlen = 222snprintf (rstatus ,sizeof (rstatus ),"Lang-%s | %d:%d" , 223E .syntax ?E .syntax -> filetype :"???" ,E .cy + 1 ,E .cx + 1 ); 224if (len > E .width ) {len = E .width ; } 225abappend (ab ,status ,len ); 226while (len < E .width ) { 227if (E .width - len == rlen ) { 228abappend (ab ,rstatus ,rlen ); 229break ; 230 } 231abappend (ab ," " ,1 ); 232++ len ; 233 } 234abappend (ab ,"\x1b[m" ,3 ); 235abappend (ab ,"\r\n" ,2 ); 236} 237 238void drawMessageBar (abuf * ab ) { 239abappend (ab ,"\x1b[K" ,3 ); 240int msglen = strlen (E .statusmsg ); 241if (msglen > E .width ) {msglen = E .width ; } 242if (msglen && time (NULL )- E .messageTime < MESSAGE_TIMER ) { 243abappend (ab ,E .statusmsg ,msglen ); 244 } 245} 246 247void drawRows (abuf * ab ) { 248for (int i = 0 ;i < E .height ;++ i ) { 249int currentRow = E .scrollRow + i ; 250if (currentRow >=E .numrows ) { 251if (E .numrows == 0 && i == E .height /3 ) { 252drawWelcome (ab ); 253 }else { 254abappend (ab ,"~" ,1 ); 255 } 256 }else { 257char lineNum [16 ]; 258if (ENABLE_LINE_NUMS && LINE_NUM_WIDTH ) { 259snprintf (lineNum ,sizeof (lineNum ),"%08d" ,E .scrollRow + i + 1 ); 260char buf [32 ]; 261snprintf (buf ,sizeof (buf ),"\x1b[7m%s\x1b[m" , 262& lineNum [8 - LINE_NUM_WIDTH ]); 263abappend (ab ,buf ,strlen (buf )); 264 } 265int len = E .row [currentRow ].size - E .scrollCol ; 266if (len < 0 ) {len = 0 ; } 267if (len > E .width - LINE_NUM_WIDTH ) { 268len = E .width - LINE_NUM_WIDTH ; 269 } 270int color = -1 ; 271char * c = & E .row [currentRow ].text [E .scrollCol ]; 272char * hl = & E .row [currentRow ].hl [E .scrollCol ]; 273for (int j = 0 ;j < len ;++ j ) { 274if (iscntrl (c [j ])) { 275char sym = (c [j ] <=26 ) ?'@' + c [j ] :'?' ; 276abappend (ab ,"\x1b[7m" ,4 ); 277abappend (ab ,& sym ,1 ); 278abappend (ab ,"\x1b[m" ,3 ); 279if (hl [j ]!= -1 ) { 280char buf [16 ]; 281int clen = snprintf (buf ,sizeof (buf ),"\x1b[%dm" , 282getColor (hl [j ])); 283abappend (ab ,buf ,clen ); 284 } 285continue ; 286 } 287if (hl [j ]== HL_NORMAL ) { 288color = -1 ; 289abappend (ab ,"\x1b[39m" ,5 ); 290 }else if (hl [j ]!= color ) { 291color = hl [j ]; 292char buf [16 ]; 293int clen = 294snprintf (buf ,sizeof (buf ),"\x1b[%dm" ,getColor (hl [j ])); 295abappend (ab ,buf ,clen ); 296 } 297abappend (ab ,& c [j ],1 ); 298 } 299abappend (ab ,"\x1b[39m" ,5 ); 300 } 301abappend (ab ,"\x1b[K\r\n" ,5 ); 302 } 303} 304 305void refresh () { 306scroll (); 307abuf ab = ABUF_INIT ; 308abappend (& ab ,"\x1b[?25l" ,6 ); 309abappend (& ab ,"\x1b[H" ,3 ); 310drawRows (& ab ); 311drawStatus (& ab ); 312drawMessageBar (& ab ); 313char buf [16 ]; 314snprintf (buf ,sizeof (buf ),"\x1b[%d;%dH" ,E .cy - E .scrollRow + 1 , 315E .cx - E .scrollCol + 1 + (ENABLE_LINE_NUMS ?LINE_NUM_WIDTH :0 )); 316abappend (& ab ,buf ,strlen (buf )); 317abappend (& ab ,"\x1b[?25h" ,6 ); 318write (STDOUT_FILENO ,ab .b ,ab .len ); 319abfree (& ab ); 320}