















































































Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!
















































































#define KILO_VERSION "0.0.1" #define _BSD_SOURCE #define _GNU_SOURCE #include <termios.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <sys/types.h> #include <sys/ioctl.h> #include <sys/time.h> #include <unistd.h> #include <stdarg.h> #include <fcntl.h> /* Syntax highlight types / #define HL_NORMAL 0 #define HL_NONPRINT 1 #define HL_COMMENT 2 / Single line comment. / #define HL_MLCOMMENT 3 / Multi-line comment. / #define HL_KEYWORD1 4 #define HL_KEYWORD2 5 #define HL_STRING 6 #define HL_NUMBER 7 #define HL_MATCH 8 / Search match. */
#define HL_HIGHLIGHT_STRINGS (1<<0) #define HL_HIGHLIGHT_NUMBERS (1<<1) struct editorSyntax { char **filematch; char *keywords; char singleline_comment_start[2]; char multiline_comment_start[3]; char multiline_comment_end[3]; int flags; }; / This structure represents a single line of the file we are editing. / typedef struct erow { int idx; / Row index in the file, zero-based. / int size; / Size of the row, excluding the null term. / int rsize; / Size of the rendered row. */ char chars; / Row content. / char render; / Row content "rendered" for screen (for TABs). / unsigned char hl; / Syntax highlight type for each character in render./ int hl_oc; / Row had open comment at end in last syntax highlight check. */ } erow; typedef struct hlcolor { int r,g,b; } hlcolor;
CTRL_U = 21, /* Ctrl-u / ESC = 27, / Escape / BACKSPACE = 127, / Backspace / / The following are just soft codes, not really reported by the
atexit(editorAtExit); if (tcgetattr(fd,&orig_termios) == -1) goto fatal; raw = orig_termios; /* modify the original mode / / input modes: no break, no CR to NL, no parity check, no strip char,
/* Report cursor location / if (write(ofd, "\x1b[6n", 4) != 4) return -1; / Read the response: ESC [ rows ; cols R / while (i < sizeof(buf)-1) { if (read(ifd,buf+i,1) != 1) break; if (buf[i] == 'R') break; i++; } buf[i] = '\0'; / Parse it. / if (buf[0] != ESC || buf[1] != '[') return -1; if (sscanf(buf+2,"%d;%d",rows,cols) != 2) return -1; return 0; } / Try to get the number of columns in the current terminal. If the ioctl()
retval = getCursorPosition(ifd,ofd,&orig_row,&orig_col); if (retval == -1) goto failed; /* Go to right/bottom margin and get position. / if (write(ofd,"\x1b[999C\x1b[999B",12) != 12) goto failed; retval = getCursorPosition(ifd,ofd,rows,cols); if (retval == -1) goto failed; / Restore position. / char seq[32]; snprintf(seq,32,"\x1b[%d;%dH",orig_row,orig_col); if (write(ofd,seq,strlen(seq)) == -1) { / Can't recover... */ } return 0; } else { *cols = ws.ws_col; rows = ws.ws_row; return 0; } failed: return -1; } / ====================== Syntax highlight color scheme ==================== / int is_separator(int c) { return c == '\0' || isspace(c) || strchr(",.()+-/=~%[];",c) != NULL;
i = 0; /* Current char offset / while(p && isspace(p)) { p++; i++; } prev_sep = 1; / Tell the parser if 'i' points to start of word. / in_string = 0; / Are we inside "" or ''? / in_comment = 0; / Are we inside multi-line comment? / / If the previous line has an open comment, this line starts
prev_sep = 1; continue; } else { prev_sep = 0; p++; i++; continue; } } else if (p == mcs[0] && (p+1) == mcs[1]) { row->hl[i] = HL_MLCOMMENT; row->hl[i+1] = HL_MLCOMMENT; p += 2; i += 2; in_comment = 1; prev_sep = 0; continue; } / Handle "" and '' / if (in_string) { row->hl[i] = HL_STRING; if (p == '\') { row->hl[i+1] = HL_STRING; p += 2; i += 2; prev_sep = 0; continue; } if (p == in_string) in_string = 0; p++; i++; continue; } else {
for (j = 0; keywords[j]; j++) { int klen = strlen(keywords[j]); int kw2 = keywords[j][klen-1] == '|'; if (kw2) klen--; if (!memcmp(p,keywords[j],klen) && is_separator((p+klen))) { / Keyword / memset(row->hl+i,kw2? HL_KEYWORD2 : HL_KEYWORD1,klen); p += klen; i += klen; break; } } if (keywords[j] != NULL) { prev_sep = 0; continue; / We had a keyword match / } } / Not special chars / prev_sep = is_separator(p); p++; i++; } /* Propagate syntax change to the next row if the open commen
int oc = editorRowHasOpenComment(row); if (row->hl_oc != oc && row->idx+1 < E.numrows) editorUpdateSyntax(&E.row[row->idx+1]); row->hl_oc = oc; } /* Maps syntax highlight token types to terminal colors. / int editorSyntaxToColor(int hl) { switch(hl) { case HL_COMMENT: case HL_MLCOMMENT: return 36; / cyan / case HL_KEYWORD1: return 33; / yellow / case HL_KEYWORD2: return 32; / green / case HL_STRING: return 35; / magenta / case HL_NUMBER: return 31; / red / case HL_MATCH: return 34; / blu / default: return 37; / white / } } / Select the syntax highlight scheme depending on the filename,
} else { row->render[idx++] = row->chars[j]; } } row->rsize = idx; row->render[idx] = '\0'; /* Update the syntax highlighting attributes of the row. / editorUpdateSyntax(row); } / Insert a row at the specified position, shifting the other rows on the bottom
E.numrows++; E.dirty++; } /* Free row's heap allocated stuff. */ void editorFreeRow(erow row) { free(row->render); free(row->chars); free(row->hl); } / Remove the row at the specified position, shifting the remainign on the