lisp changes
Build project / build (push) Successful in 1m26s

This commit is contained in:
2026-05-22 14:37:32 +02:00
parent edb5384f0e
commit 310498d582
6 changed files with 116 additions and 75 deletions
+110 -68
View File
@@ -7,8 +7,8 @@
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <unistd.h>
#include "include/utf8.h"
@@ -44,28 +44,52 @@ void enableRawMode() {
}
}
#include <ctype.h> /* isprint */
#include <ctype.h> /* isprint */
char *key_to_string(int key) {
static char key_str[32];
if (key == '\r') {
strcpy(key_str, "ENTER");
} else if (key == 0x09) {
strcpy(key_str, "TAB");
} else if (key >= 1 && key <= 26) {
snprintf(key_str, sizeof(key_str), "CTRL-%c", 'a' + key - 1);
} else {
switch (key) {
case ARROW_UP: strcpy(key_str, "ARROW-UP"); break;
case ARROW_DOWN: strcpy(key_str, "ARROW-DOWN"); break;
case ARROW_LEFT: strcpy(key_str, "ARROW-LEFT"); break;
case ARROW_RIGHT: strcpy(key_str, "ARROW-RIGHT"); break;
case PAGE_UP: strcpy(key_str, "PAGE-UP"); break;
case PAGE_DOWN: strcpy(key_str, "PAGE-DOWN"); break;
case DEL_KEY: strcpy(key_str, "DEL"); break;
case BACKSPACE: strcpy(key_str, "BACKSPACE"); break;
case BEG_LINE: strcpy(key_str, "HOME"); break;
case END_LINE: strcpy(key_str, "END"); break;
case '\x1b': strcpy(key_str, "ESCAPE"); break;
case ARROW_UP:
strcpy(key_str, "ARROW-UP");
break;
case ARROW_DOWN:
strcpy(key_str, "ARROW-DOWN");
break;
case ARROW_LEFT:
strcpy(key_str, "ARROW-LEFT");
break;
case ARROW_RIGHT:
strcpy(key_str, "ARROW-RIGHT");
break;
case PAGE_UP:
strcpy(key_str, "PAGE-UP");
break;
case PAGE_DOWN:
strcpy(key_str, "PAGE-DOWN");
break;
case DEL_KEY:
strcpy(key_str, "DEL");
break;
case BACKSPACE:
strcpy(key_str, "BACKSPACE");
break;
case BEG_LINE:
strcpy(key_str, "HOME");
break;
case END_LINE:
strcpy(key_str, "END");
break;
case '\x1b':
strcpy(key_str, "ESCAPE");
break;
default:
if (key > 127) {
/* UTF-8 code point — re-encode into the buffer */
@@ -84,58 +108,76 @@ char *key_to_string(int key) {
int editorReadKey() {
char c;
/* read first byte — may be start of UTF-8 or escape */
while (read(STDIN_FILENO, &c, 1) != 1);
appDebug("f : %X\r\n", c);
/* read first byte — may be start of UTF-8 or escape */
while (read(STDIN_FILENO, &c, 1) != 1)
;
appDebug("f : %X\r\n", c);
if (c == '\x1b') {
char seq[6];
/* try to read escape sequence */
if (read(STDIN_FILENO, &seq[0], 1) != 1) return '\x1b';
if (read(STDIN_FILENO, &seq[1], 1) != 1) return '\x1b';
if (seq[0] == '[') {
if (seq[1] >= '0' && seq[1] <= '9') {
if (read(STDIN_FILENO, &seq[2], 1) != 1) return '\x1b';
if (seq[2] == '~') {
switch (seq[1]) {
case '1': return BEG_LINE;
case '3': return DEL_KEY;
case '4': return END_LINE;
case '5': return PAGE_UP;
case '6': return PAGE_DOWN;
case '7': return BEG_LINE;
case '8': return END_LINE;
}
}
} else {
switch (seq[1]) {
case 'A': return ARROW_UP;
case 'B': return ARROW_DOWN;
case 'C': return ARROW_RIGHT;
case 'D': return ARROW_LEFT;
case 'H': return BEG_LINE;
case 'F': return END_LINE;
}
}
if (c == '\x1b') {
char seq[6];
/* try to read escape sequence */
if (read(STDIN_FILENO, &seq[0], 1) != 1)
return '\x1b';
if (read(STDIN_FILENO, &seq[1], 1) != 1)
return '\x1b';
if (seq[0] == '[') {
if (seq[1] >= '0' && seq[1] <= '9') {
if (read(STDIN_FILENO, &seq[2], 1) != 1)
return '\x1b';
if (seq[2] == '~') {
switch (seq[1]) {
case '1':
return BEG_LINE;
case '3':
return DEL_KEY;
case '4':
return END_LINE;
case '5':
return PAGE_UP;
case '6':
return PAGE_DOWN;
case '7':
return BEG_LINE;
case '8':
return END_LINE;
}
}
return '\x1b';
} else {
switch (seq[1]) {
case 'A':
return ARROW_UP;
case 'B':
return ARROW_DOWN;
case 'C':
return ARROW_RIGHT;
case 'D':
return ARROW_LEFT;
case 'H':
return BEG_LINE;
case 'F':
return END_LINE;
}
}
}
return '\x1b';
}
/* multi-byte UTF-8: read remaining bytes */
int seqlen = utf8Seqlen((unsigned char)c);
if (seqlen > 1) {
/* pack into a pseudo-codepoint just to pass bytes through;
we handle encoding/decoding at the row level */
char buf[4] = {c, 0, 0, 0};
for (int i = 1; i < seqlen; i++)
if (read(STDIN_FILENO, &buf[i], 1) != 1) break;
/* decode and return as uint32, but we need int — use high range */
const char *p = buf;
uint32_t cp = utf8Decode(&p);
return (int)cp; /* caller re-encodes when inserting */
}
/* multi-byte UTF-8: read remaining bytes */
int seqlen = utf8Seqlen((unsigned char)c);
if (seqlen > 1) {
/* pack into a pseudo-codepoint just to pass bytes through;
we handle encoding/decoding at the row level */
char buf[4] = {c, 0, 0, 0};
for (int i = 1; i < seqlen; i++)
if (read(STDIN_FILENO, &buf[i], 1) != 1)
break;
/* decode and return as uint32, but we need int — use high range */
const char *p = buf;
uint32_t cp = utf8Decode(&p);
return (int)cp; /* caller re-encodes when inserting */
}
return (unsigned char)c;
return (unsigned char)c;
}
int getCursorPosition(int *rows, int *cols) {
@@ -183,12 +225,12 @@ int getWindowSize(int *rows, int *cols) {
}
void appDebug(const char *fmt, ...) {
#ifdef APP_DEBUG
va_list ap;
char message[256];
va_start(ap, fmt);
vsnprintf(message, 256, fmt, ap);
va_end(ap);
fprintf(stderr, "%s\n", message);
#endif
#ifdef APP_DEBUG
va_list ap;
char message[256];
va_start(ap, fmt);
vsnprintf(message, 256, fmt, ap);
va_end(ap);
fprintf(stderr, "%s\n", message);
#endif
}