commit 42658b8d989c76983cac71c0fa8efa49e6d97939 Author: Kyle Isom Date: Fri Feb 7 20:46:43 2020 -0800 Initial import, starting with kyle's editor. diff --git a/.hgignore b/.hgignore new file mode 100644 index 0000000..6f7beca --- /dev/null +++ b/.hgignore @@ -0,0 +1 @@ +.o$ diff --git a/ke/Makefile b/ke/Makefile new file mode 100644 index 0000000..c5cceb4 --- /dev/null +++ b/ke/Makefile @@ -0,0 +1,22 @@ +BIN := ke +OBJS := main.o + +LDFLAGS := -static +CFLAGS := -pedantic -Wall -Werror -Wextra -O2 -std=c99 + +.PHONY: all +all: $(BIN) run + +$(BIN): $(OBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) + +.PHONY: clean +clean: + rm -f $(BIN) $(OBJS) + +.PHONY: run +run: $(BIN) + ./$(BIN) + +%.o: %.c + $(CC) $(CFLAGS) -c $@ $< diff --git a/ke/keys.txt b/ke/keys.txt new file mode 100644 index 0000000..afee820 --- /dev/null +++ b/ke/keys.txt @@ -0,0 +1,37 @@ +An Exploration of the Keys +========================== + +ESC: 1b + +Function keys: 1b followed by + +F1 F2 F3 F4 F5 F6 F7 F8 +4f O 4f O 4f O 4f O 5b 5b 5b 5b +50 P 51 Q 52 R 54 S 31 1 31 1 31 1 31 1 + 35 5 37 7 38 8 39 9 + 7e ~ 7e ~ 7e ~ 7e ~ + +F9 F10 F11 F12 +5b [ 5b [ 5b [ 5b [ +32 2 32 2 32 2 32 2 +30 0 31 1 33 3 34 4 +7e ~ 7e ~ 7e ~ 7e ~ + +DEL 7f - but it should be a 4-byte escape sequence? +BKSP 7f + +Arrow keys: 1b followed by +UP DN LF RT +5b [ 5b [ 5b [ 5b [ +41 A 42 B 44 D 43 C + +Other keys: 1b followed by + +HOME END PGUP PGDN INS +5b [ 5b [ 5b [ 5b [ 5b [ +48 H 46 F 35 5 36 6 32 2 + 7e ~ 7e ~ 7e ~ + + +Notes: any time you see escape, you must process another key to figure +out what to do. diff --git a/ke/main.c b/ke/main.c new file mode 100644 index 0000000..0f80869 --- /dev/null +++ b/ke/main.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include + + +/* entry_term contains a snapshot of the termios settings at startup. */ +struct termios entry_term; + + +/* + * A text editor needs the terminal to be in raw mode; but the default + * is to be in canonical (cooked) mode, which is a buffered input mode. + */ + +static void +enable_termraw() +{ + struct termios raw; + + /* Read the current terminal parameters for standard input. */ + tcgetattr(STDIN_FILENO, &raw); + + /* + * Turn off the local ECHO mode, which we need to do in raw mode + * because what gets displayed is going to need extra control. + * + * NOTE(kyle): look into cfmakeraw, which will require + * snapshotting. + */ + raw.c_lflag &= ~(ECHO|ICANON); + + /* + * Now write the terminal parameters to the current terminal, + * after flushing any waiting input out. + */ + tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); +} + + +static void +disable_termraw() +{ + tcsetattr(STDIN_FILENO, TCSAFLUSH, &entry_term); +} + + +int +main() +{ + char c; + + /* prepare the terminal */ + tcgetattr(STDIN_FILENO, &entry_term); + atexit(disable_termraw); + enable_termraw(); + + while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') { + if (iscntrl(c)) { + printf("%02x\n", c); + } else { + printf("%02x ('%c')\n", c, c); + } + } + + return 0; +}