Diferencias
Muestra las diferencias entre dos versiones de la página.
| Próxima revisión | Revisión previa | ||
| kilo.c [2024/11/26 17:13] – creado peron | kilo.c [2026/04/21 16:07] (actual) – editor externo 127.0.0.1 | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| - | <code c> | + | <file c kilo.c> |
| /* Kilo -- Un editor muy simple en menos de 1000 líneaas de código | /* Kilo -- Un editor muy simple en menos de 1000 líneaas de código | ||
| * (como las contó " | * (como las contó " | ||
| Línea 79: | Línea 79: | ||
| }; | }; | ||
| - | /* This structure represents a single line of the file we are editing. */ | + | /* Esta estructura representa una unica línea del fichero que editamos. */ |
| typedef struct erow { | typedef struct erow { | ||
| - | int idx; /* Row index in the file, zero-based. */ | + | int idx; /* Indice de fila en el fichero, basado en cero. */ |
| - | int size; / | + | int size; / |
| - | int rsize; | + | int rsize; |
| - | char *chars; | + | char *chars; |
| - | char *render; | + | char *render; |
| - | unsigned char *hl; /* Syntax highlight type for each character in render.*/ | + | unsigned char *hl; /* Tipo de resaltado de sintaxis para cada caracter en render.*/ |
| - | int hl_oc; | + | int hl_oc; |
| - | check. */ | + | |
| } erow; | } erow; | ||
| Línea 96: | Línea 95: | ||
| struct editorConfig { | struct editorConfig { | ||
| - | int cx, | + | int cx, |
| - | int rowoff; | + | int rowoff; |
| - | int coloff; | + | int coloff; |
| - | int screenrows; /* Number of rows that we can show */ | + | int screenrows; /* Nro. de filas que podemos mostrar |
| - | int screencols; /* Number of cols that we can show */ | + | int screencols; /* Nro. de columnas que podemos mostrar |
| - | int numrows; | + | int numrows; |
| - | int rawmode; | + | int rawmode; |
| - | erow *row; /* Rows */ | + | erow *row; /* Filas */ |
| - | int dirty; | + | int dirty; |
| - | char *filename; /* Currently open filename | + | char *filename; /* Nombre de archivo actualmente abierto |
| char statusmsg[80]; | char statusmsg[80]; | ||
| time_t statusmsg_time; | time_t statusmsg_time; | ||
| - | struct editorSyntax *syntax; | + | struct editorSyntax *syntax; |
| }; | }; | ||
| Línea 127: | Línea 126: | ||
| ESC = 27, /* Escape */ | ESC = 27, /* Escape */ | ||
| BACKSPACE = 127, /* Backspace */ | BACKSPACE = 127, /* Backspace */ | ||
| - | /* The following are just soft codes, | + | /* Los siguientes sólo son soft codes, |
| - | | + | |
| ARROW_LEFT = 1000, | ARROW_LEFT = 1000, | ||
| ARROW_RIGHT, | ARROW_RIGHT, | ||
| Línea 142: | Línea 140: | ||
| void editorSetStatusMessage(const char *fmt, ...); | void editorSetStatusMessage(const char *fmt, ...); | ||
| - | /* =========================== Syntax highlights DB ========================= | + | /* ====================== |
| * | * | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | * nombre de fichero dado: si comienza un patrón coincidente con un punto, |
| - | | + | |
| + | | ||
| + | | ||
| * | * | ||
| - | | + | |
| - | | + | |
| - | | + | |
| * | * | ||
| - | | + | |
| - | | + | |
| - | | + | |
| * | * | ||
| - | | + | |
| - | | + | |
| + | | ||
| * | * | ||
| - | | + | |
| - | + | ||
| - | /* C / C++ */ | + | |
| char *C_HL_extensions[] = {" | char *C_HL_extensions[] = {" | ||
| char *C_HL_keywords[] = { | char *C_HL_keywords[] = { | ||
| - | /* C Keywords */ | + | /* Keywords |
| " | " | ||
| " | " | ||
| " | " | ||
| - | /* C++ Keywords | + | /* Keywords para C++ */ |
| " | " | ||
| " | " | ||
| Línea 180: | Línea 179: | ||
| " | " | ||
| - | /* C types */ | + | /* tipos para C */ |
| " | " | ||
| " | " | ||
| }; | }; | ||
| - | /* Here we define an array of syntax highlights by extensions, keywords, | + | /* Aqui defino un array de resaltadores de sintaxis para extensiones, |
| - | | + | |
| struct editorSyntax HLDB[] = { | struct editorSyntax HLDB[] = { | ||
| { | { | ||
| Línea 199: | Línea 198: | ||
| #define HLDB_ENTRIES (sizeof(HLDB)/ | #define HLDB_ENTRIES (sizeof(HLDB)/ | ||
| - | /* ======================= Low level terminal handling | + | /* ================== |
| - | static struct termios orig_termios; | + | static struct termios orig_termios; |
| void disableRawMode(int fd) { | void disableRawMode(int fd) { | ||
| - | /* Don't even check the return value as it's too late. */ | + | /* Ni siquiera revises este valor de retorno, ya que es muy tarde. */ |
| if (E.rawmode) { | if (E.rawmode) { | ||
| tcsetattr(fd, | tcsetattr(fd, | ||
| Línea 211: | Línea 210: | ||
| } | } | ||
| - | /* Called at exit to avoid remaining in raw mode. */ | + | /* Llamado al salir para evitar permanecer en modo raw. */ |
| void editorAtExit(void) { | void editorAtExit(void) { | ||
| disableRawMode(STDIN_FILENO); | disableRawMode(STDIN_FILENO); | ||
| } | } | ||
| - | /* Raw mode: 1960 magic shit. */ | + | /* Modo Raw: magia de los 60s. */ |
| int enableRawMode(int fd) { | int enableRawMode(int fd) { | ||
| struct termios raw; | struct termios raw; | ||
| - | if (E.rawmode) return 0; /* Already enabled. | + | if (E.rawmode) return 0; /* Ya activado |
| if (!isatty(STDIN_FILENO)) goto fatal; | if (!isatty(STDIN_FILENO)) goto fatal; | ||
| atexit(editorAtExit); | atexit(editorAtExit); | ||
| if (tcgetattr(fd,& | if (tcgetattr(fd,& | ||
| - | raw = orig_termios; | + | raw = orig_termios; |
| - | /* input modes: no break, no CR to NL, no parity check, | + | /* modos de entrada: sin break, no CR a NL, sin parity check, |
| - | | + | |
| raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); | raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); | ||
| - | /* output modes - disable | + | /* modos de salida |
| raw.c_oflag &= ~(OPOST); | raw.c_oflag &= ~(OPOST); | ||
| - | /* control | + | /* modos de control - activa |
| raw.c_cflag |= (CS8); | raw.c_cflag |= (CS8); | ||
| - | /* local modes - choing off, canonical off, no extended functions, | + | /* local modes - choing off, canonical off, sin funciones extendidas, |
| - | | + | |
| raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); | raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); | ||
| - | /* control | + | /* chars de control |
| - | raw.c_cc[VMIN] = 0; /* Return each byte, or zero for timeout. */ | + | raw.c_cc[VMIN] = 0; /* Devuelve cada byte, o zero para el timeout. */ |
| - | raw.c_cc[VTIME] = 1; /* 100 ms timeout | + | raw.c_cc[VTIME] = 1; /* timeout de 100 ms (la unidad es décimas de seg). */ |
| - | /* put terminal in raw mode after flushing | + | /* pongo temrinal en modo raw antes de descartar |
| if (tcsetattr(fd, | if (tcsetattr(fd, | ||
| E.rawmode = 1; | E.rawmode = 1; | ||
| Línea 250: | Línea 249: | ||
| } | } | ||
| - | /* Read a key from the terminal | + | /* Leo una tecla desde terminal |
| - | * escape | + | |
| int editorReadKey(int fd) { | int editorReadKey(int fd) { | ||
| int nread; | int nread; | ||
| Línea 260: | Línea 259: | ||
| while(1) { | while(1) { | ||
| switch(c) { | switch(c) { | ||
| - | case ESC: /* escape | + | case ESC: /* secuencia de escape */ |
| - | /* If this is just an ESC, we' | + | /* Si es un solo |
| if (read(fd, | if (read(fd, | ||
| if (read(fd, | if (read(fd, | ||
| - | /* ESC [ sequences. */ | + | /* ESC [ secuencias. */ |
| if (seq[0] == ' | if (seq[0] == ' | ||
| if (seq[1] >= ' | if (seq[1] >= ' | ||
| - | /* Extended escape, read additional | + | /* Escape extendido, lee byte adicional. */ |
| if (read(fd, | if (read(fd, | ||
| if (seq[2] == ' | if (seq[2] == ' | ||
| Línea 303: | Línea 302: | ||
| } | } | ||
| - | /* Use the ESC [6n escape sequence to query the horizontal | + | /* Usa la secuencia de escape |
| - | | + | |
| - | * cursor | + | |
| int getCursorPosition(int ifd, int ofd, int *rows, int *cols) { | int getCursorPosition(int ifd, int ofd, int *rows, int *cols) { | ||
| char buf[32]; | char buf[32]; | ||
| unsigned int i = 0; | unsigned int i = 0; | ||
| - | /* Report | + | /* Reporta posición del cursor */ |
| if (write(ofd, " | if (write(ofd, " | ||
| - | /* Read the response: ESC [ rows ; cols R */ | + | /* Read the response: ESC [ filas ; columnas |
| while (i < sizeof(buf)-1) { | while (i < sizeof(buf)-1) { | ||
| if (read(ifd, | if (read(ifd, | ||
| Línea 321: | Línea 320: | ||
| buf[i] = ' | buf[i] = ' | ||
| - | /* Parse it. */ | + | /* Lo parsea. */ |
| if (buf[0] != ESC || buf[1] != ' | if (buf[0] != ESC || buf[1] != ' | ||
| if (sscanf(buf+2," | if (sscanf(buf+2," | ||
| Línea 327: | Línea 326: | ||
| } | } | ||
| - | /* Try to get the number of columns in the current | + | /* Intenta obtener la cantidad de columnas de la terminal |
| - | * call fails the function will try to query the terminal | + | * llamada |
| - | | + | |
| int getWindowSize(int ifd, int ofd, int *rows, int *cols) { | int getWindowSize(int ifd, int ofd, int *rows, int *cols) { | ||
| struct winsize ws; | struct winsize ws; | ||
| Línea 337: | Línea 336: | ||
| int orig_row, orig_col, retval; | int orig_row, orig_col, retval; | ||
| - | /* Get the initial position so we can restore it later. */ | + | /* Obtiene posición inicial para poder volver a ella luego. */ |
| retval = getCursorPosition(ifd, | retval = getCursorPosition(ifd, | ||
| if (retval == -1) goto failed; | if (retval == -1) goto failed; | ||
| - | /* Go to right/bottom margin and get position. */ | + | /* Va al máergen derecho/inferior y obtiene posición. */ |
| if (write(ofd," | if (write(ofd," | ||
| retval = getCursorPosition(ifd, | retval = getCursorPosition(ifd, | ||
| if (retval == -1) goto failed; | if (retval == -1) goto failed; | ||
| - | /* Restore position. */ | + | /* Restaura posición. */ |
| char seq[32]; | char seq[32]; | ||
| snprintf(seq, | snprintf(seq, | ||
| if (write(ofd, | if (write(ofd, | ||
| - | /* Can't recover... */ | + | /* No puedo recupera... */ |
| } | } | ||
| return 0; | return 0; | ||
| Línea 363: | Línea 362: | ||
| } | } | ||
| - | /* ====================== Syntax highlight | + | /* ================ |
| int is_separator(int c) { | int is_separator(int c) { | ||
| Línea 411: | Línea 410: | ||
| while(*p) { | while(*p) { | ||
| - | /* Handle | + | /* Resuelve comentarios |
| if (prev_sep && *p == scs[0] && *(p+1) == scs[1]) { | if (prev_sep && *p == scs[0] && *(p+1) == scs[1]) { | ||
| - | /* From here to end is a comment | + | /* De aquí al final es un comentario |
| memset(row-> | memset(row-> | ||
| return; | return; | ||
| Línea 552: | Línea 551: | ||
| } | } | ||
| - | /* ======================= | + | /* ======================= |
| - | /* Update the rendered version and the syntax highlight of a row. */ | + | /* Actualizar la versión renderizada y el resaltado de sintaxis de una linea. */ |
| void editorUpdateRow(erow *row) { | void editorUpdateRow(erow *row) { | ||
| unsigned int tabs = 0, nonprint = 0; | unsigned int tabs = 0, nonprint = 0; | ||
| int j, idx; | int j, idx; | ||
| - | / | + | / |
| - | | + | * pantalla respetando |
| + | * '?' | ||
| free(row-> | free(row-> | ||
| for (j = 0; j < row-> | for (j = 0; j < row-> | ||
| Línea 568: | Línea 568: | ||
| (unsigned long long) row-> | (unsigned long long) row-> | ||
| if (allocsize > UINT32_MAX) { | if (allocsize > UINT32_MAX) { | ||
| - | printf(" | + | printf(" |
| exit(1); | exit(1); | ||
| } | } | ||
| Línea 585: | Línea 585: | ||
| row-> | row-> | ||
| - | /* Update the syntax highlighting attributes of the row. */ | + | /* Actualiza atributos de resaltado de sintaxis de la fila. */ |
| editorUpdateSyntax(row); | editorUpdateSyntax(row); | ||
| } | } | ||
| Línea 692: | Línea 692: | ||
| } | } | ||
| - | /* Delete the character at offset ' | + | /* Borrar el caracter en offset ' |
| void editorRowDelChar(erow *row, int at) { | void editorRowDelChar(erow *row, int at) { | ||
| if (row-> | if (row-> | ||
| Línea 736: | Línea 736: | ||
| return; | return; | ||
| } | } | ||
| - | /* If the cursor | + | /* Si el cursor |
| - | | + | |
| if (filecol >= row-> | if (filecol >= row-> | ||
| if (filecol == 0) { | if (filecol == 0) { | ||
| editorInsertRow(ficherofilas,"", | editorInsertRow(ficherofilas,"", | ||
| } else { | } else { | ||
| - | /* We are in the middle of a line. Split it between two rows. */ | + | /* Estamos en el medio de una línea. Dividirla en dos lineas. */ |
| editorInsertRow(ficherofilas+1, | editorInsertRow(ficherofilas+1, | ||
| row = & | row = & | ||
| Línea 759: | Línea 759: | ||
| } | } | ||
| - | /* Delete the char at the current | + | /* Borra el char en la posición del prompt |
| void editorDelChar() { | void editorDelChar() { | ||
| int ficherofilas = E.rowoff+E.cy; | int ficherofilas = E.rowoff+E.cy; | ||
| Línea 794: | Línea 794: | ||
| } | } | ||
| - | /* Load the specified program in the editor | + | /* Carga el programa especificado en la memoria del editor |
| - | | + | |
| int editorOpen(char *filename) { | int editorOpen(char *filename) { | ||
| FILE *fp; | FILE *fp; | ||
| Línea 879: | Línea 879: | ||
| } | } | ||
| - | �/* Esta función escribe la pantalla completa usando caracterede escape VT100 | + | /* Esta función escribe la pantalla completa usando caracterede escape VT100 |
| * comenzando del estado lógicodel editor en el estado global ' | * comenzando del estado lógicodel editor en el estado global ' | ||
| void editorRefreshScreen(void) { | void editorRefreshScreen(void) { | ||
| Línea 896: | Línea 896: | ||
| char welcome[80]; | char welcome[80]; | ||
| int welcomelen = snprintf(welcome, | int welcomelen = snprintf(welcome, | ||
| - | " | + | "Editor |
| int padding = (E.screencols-welcomelen)/ | int padding = (E.screencols-welcomelen)/ | ||
| if (padding) { | if (padding) { | ||
| Línea 1081: | Línea 1081: | ||
| find_next = 0; | find_next = 0; | ||
| - | /* Highlight | + | /* Resaltar |
| FIND_RESTORE_HL; | FIND_RESTORE_HL; | ||
| Línea 1097: | Línea 1097: | ||
| E.rowoff = current; | E.rowoff = current; | ||
| E.coloff = 0; | E.coloff = 0; | ||
| - | /* Scroll horizontally as needed. */ | + | /* Desplazar horizontalmente según se necesite. */ |
| if (E.cx > E.screencols) { | if (E.cx > E.screencols) { | ||
| int diff = E.cx - E.screencols; | int diff = E.cx - E.screencols; | ||
| Línea 1170: | Línea 1170: | ||
| break; | break; | ||
| } | } | ||
| - | /* Fix cx if the current line has not enough | + | /* Corrige |
| ficherofilas = E.rowoff+E.cy; | ficherofilas = E.rowoff+E.cy; | ||
| filecol = E.coloff+E.cx; | filecol = E.coloff+E.cx; | ||
| Línea 1308: | Línea 1308: | ||
| return 0; | return 0; | ||
| } | } | ||
| - | </code> | + | </file> |
