From 0324dda2cc779bcd6d3eb425216e29b37a9a95ed Mon Sep 17 00:00:00 2001 From: cflip Date: Wed, 11 Jan 2023 11:21:41 -0700 Subject: Remove all terminal-related code --- Makefile | 2 +- editor.c | 116 ++++++++++--------------------------------------------------- error.c | 15 ++++++++ error.h | 7 ++++ file.c | 4 +-- input.c | 55 ----------------------------- terminal.c | 84 -------------------------------------------- terminal.h | 10 ------ 8 files changed, 44 insertions(+), 249 deletions(-) create mode 100644 error.c create mode 100644 error.h delete mode 100644 terminal.c delete mode 100644 terminal.h diff --git a/Makefile b/Makefile index 3265a64..d5c6c3c 100644 --- a/Makefile +++ b/Makefile @@ -5,12 +5,12 @@ OUT=editor OBJS=main.o \ buffer.o \ editor.o \ + error.o \ file.o \ font.o \ input.o \ row.o \ syntax.o \ - terminal.o \ window.o .PHONY: all clean diff --git a/editor.c b/editor.c index 43a469a..d08a961 100644 --- a/editor.c +++ b/editor.c @@ -10,7 +10,6 @@ #include "buffer.h" #include "input.h" #include "syntax.h" -#include "terminal.h" #include "row.h" void init_editor(struct editor_state* editor) @@ -42,66 +41,14 @@ void editor_set_status_message(struct editor_state* editor, const char* format, editor->status_message_time = time(NULL); } -void editor_refresh_screen(struct editor_state* editor) -{ - editor_scroll(editor); - - struct append_buffer buffer = ABUF_INIT; - - ab_append(&buffer, "\x1b[?25l", 6); - ab_append(&buffer, "\x1b[H", 3); - - editor_draw_rows(editor, &buffer); - editor_draw_status_bar(editor, &buffer); - editor_draw_message_bar(editor, &buffer); - - char buf[32]; - snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (editor->cursor_y - editor->row_offset) + 1, (editor->cursor_display_x - editor->col_offset) + 1); - ab_append(&buffer, buf, strlen(buf)); - - ab_append(&buffer, "\x1b[?25h", 6); - - write(STDOUT_FILENO, buffer.buffer, buffer.length); - ab_free(&buffer); -} - char* editor_prompt(struct editor_state* editor, char* prompt, void (*callback)(struct editor_state*, char*, int)) { - size_t buffer_size = 128; - char* buffer = malloc(buffer_size); - - size_t buffer_length = 0; - buffer[0] = '\0'; - - while (1) { - editor_set_status_message(editor, prompt, buffer); - // TODO: This should refresh the active screen - //editor_refresh_screen(); - - int c = editor_read_key(); - if (c == DELETE_KEY || c == BACKSPACE) { - if (buffer_length != 0) buffer[--buffer_length] = '\0'; - } else if (c == '\x1b') { - editor_set_status_message(editor, ""); - if (callback) callback(editor, buffer, c); - free(buffer); - return NULL; - } else if (c == '\r') { - if (buffer_length != 0) { - editor_set_status_message(editor, ""); - if (callback) callback(editor, buffer, c); - return buffer; - } - } else if (!iscntrl(c) && c < 128) { - if (buffer_length == buffer_size - 1) { - buffer_size *= 2; - buffer = realloc(buffer, buffer_size); - } - buffer[buffer_length++] = c; - buffer[buffer_length] = '\0'; - } - if (callback) callback(editor, buffer, c); - } + /* TODO: The previous implementation of this function relied on reading + * input from the terminal, but now that we get input through window events + * it's no longer possible to sit in an infinite loop waiting for keys here. + */ + printf("TODO: editor_prompt unimplemented\n"); + return NULL; } void editor_insert_char(struct editor_state* editor, int c) @@ -282,47 +229,26 @@ void editor_draw_rows(struct editor_state* editor, struct append_buffer* buffer) int j; for (j = 0; j < length; j++) { - if (iscntrl(c[j])) { - char symbol = (c[j] <= 26) ? '@' + c[j] : '?'; - ab_append(buffer, "\x1b[7m", 4); - ab_append(buffer, &symbol, 1); - ab_append(buffer, "\x1b[m", 3); - - if (current_colour != -1) { - char buf[16]; - int col_length = snprintf(buf, sizeof(buf), "\x1b[%dm", current_colour); - ab_append(buffer, buf, col_length); - } - } else if (highlight[j] == HIGHLIGHT_NORMAL) { - if (current_colour != -1) { - ab_append(buffer, "\x1b[39m", 5); - current_colour = -1; - } - ab_append(buffer, &c[j], 1); - } else { - int colour = editor_syntax_to_colour(highlight[j]); - if (colour != current_colour) { - current_colour = colour; - char colour_buffer[16]; - int colour_buffer_length = snprintf(colour_buffer, sizeof(colour_buffer), "\x1b[%dm", colour); - - ab_append(buffer, colour_buffer, colour_buffer_length); - } - ab_append(buffer, &c[j], 1); + /* + * TODO: Move colour handling stuff to the window. + int colour = editor_syntax_to_colour(highlight[j]); + if (colour != current_colour) { + current_colour = colour; + char colour_buffer[16]; + int colour_buffer_length = snprintf(colour_buffer, sizeof(colour_buffer), "\x1b[%dm", colour); + + ab_append(buffer, colour_buffer, colour_buffer_length); } + */ + ab_append(buffer, &c[j], 1); } - ab_append(buffer, "\x1b[39m", 5); } - - ab_append(buffer, "\x1b[K", 3); - ab_append(buffer, "\r\n", 2); + ab_append(buffer, "\n", 1); } } void editor_draw_status_bar(struct editor_state* editor, struct append_buffer* buffer) { - ab_append(buffer, "\x1b[7m", 4); - char status[80], right_status[80]; int length = snprintf(status, sizeof(status), "%.20s - %d lines %s", editor->filename ? editor->filename : "[New File]", editor->row_count, editor->dirty ? "(modified)" : ""); int right_length = snprintf(right_status, sizeof(right_status), "%s | %d/%d", editor->syntax ? editor->syntax->filetype : "plaintext", editor->cursor_y + 1, editor->row_count); @@ -341,15 +267,11 @@ void editor_draw_status_bar(struct editor_state* editor, struct append_buffer* b length++; } } - - ab_append(buffer, "\x1b[m", 3); - ab_append(buffer, "\r\n", 2); + ab_append(buffer, "\n", 1); } void editor_draw_message_bar(struct editor_state* editor, struct append_buffer* buffer) { - ab_append(buffer, "\x1b[K", 3); - int message_length = strlen(editor->status_message); if (message_length > editor->screen_cols) diff --git a/error.c b/error.c new file mode 100644 index 0000000..4944ff4 --- /dev/null +++ b/error.c @@ -0,0 +1,15 @@ +#include "error.h" + +#include +#include + +void fatal_error(const char *msg) +{ + perror(msg); + exit(-1); +} + +void warning(const char *msg) +{ + fprintf(stderr, "WARNING: %s\n", msg); +} diff --git a/error.h b/error.h new file mode 100644 index 0000000..f30fd24 --- /dev/null +++ b/error.h @@ -0,0 +1,7 @@ +#ifndef _ERROR_H +#define ERROR_H + +void fatal_error(const char *msg); +void warning(const char *msg); + +#endif diff --git a/file.c b/file.c index 78505df..f432b1d 100644 --- a/file.c +++ b/file.c @@ -7,9 +7,9 @@ #include #include +#include "error.h" #include "row.h" #include "syntax.h" -#include "terminal.h" char* editor_rows_to_string(struct editor_state* editor, int* buffer_length) { @@ -43,7 +43,7 @@ void editor_open(struct editor_state* editor, char* filename) FILE* fp = fopen(filename, "r"); if (!fp) - die("fopen"); + fatal_error("fopen"); char* line = NULL; size_t line_capacity = 0; diff --git a/input.c b/input.c index fac8aba..28f61f4 100644 --- a/input.c +++ b/input.c @@ -5,63 +5,8 @@ #include #include "file.h" -#include "terminal.h" #include "row.h" -int editor_read_key() -{ - int read_count; - char c; - - while ((read_count = read(STDIN_FILENO, &c, 1)) != 1) { - if (read_count == -1 && errno != EAGAIN) - die("read"); - } - - if (c == '\x1b') { - char seq[3]; - - 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 HOME_KEY; - case '3': return DELETE_KEY; - case '4': return END_KEY; - case '5': return PAGE_UP; - case '6': return PAGE_DOWN; - case '7': return HOME_KEY; - case '8': return END_KEY; - } - } - } 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 HOME_KEY; - case 'F': return END_KEY; - } - } - } else if (seq[0] == 'O') { - switch (seq[1]) { - case 'H': return HOME_KEY; - case 'F': return END_KEY; - } - } - return '\x1b'; - } else { - return c; - } -} - void editor_move_cursor(struct editor_state* editor, int key) { struct editor_row* row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y]; diff --git a/terminal.c b/terminal.c deleted file mode 100644 index c24f46d..0000000 --- a/terminal.c +++ /dev/null @@ -1,84 +0,0 @@ -#include "terminal.h" - -#include -#include -#include -#include -#include - -static struct termios original_termios; - -void die(const char* message) -{ - /* Clear the screen */ - write(STDOUT_FILENO, "\x1b[2J", 4); - write(STDOUT_FILENO, "\x1b[H", 3); - - perror(message); - exit(1); -} - -void disable_raw_mode() -{ - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios) == -1) - die("tcsetattr"); -} - -/* Disables any input and output processing from the terminal */ -void enable_raw_mode() -{ - if (tcgetattr(STDIN_FILENO, &original_termios) == -1) - die("tcgetattr"); - - atexit(disable_raw_mode); - - struct termios raw = original_termios; - raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); - raw.c_oflag &= ~(OPOST); - raw.c_cflag |= (CS8); - raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); - - raw.c_cc[VMIN] = 0; - raw.c_cc[VTIME] = 1; - - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) - die("tcsetattr"); -} - -int get_cursor_position(int* rows, int* cols) -{ - char buffer[32]; - unsigned int i = 0; - - if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) - return -1; - - while (i < sizeof(buffer) - 1) { - if (read(STDIN_FILENO, &buffer[i], 1) != 1) break; - if (buffer[i] == 'R') break; - i++; - } - buffer[i] = '\0'; - - printf("\r\n&buffer[1]: '%s'\r\n", &buffer[1]); - - if (buffer[0] != '\x1b' || buffer[1] != '[') return -1; - if (sscanf(&buffer[2], "%d;%d", rows, cols) != 2) return -1; - - return 0; -} - -int get_window_size(int* rows, int* cols) -{ - struct winsize size; - - if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == -1 || size.ws_col == 0) { - if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) - return -1; - return get_cursor_position(rows, cols); - } else { - *cols = size.ws_col; - *rows = size.ws_row; - return 0; - } -} diff --git a/terminal.h b/terminal.h deleted file mode 100644 index 1df7ea7..0000000 --- a/terminal.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef _TERMINAL_H -#define _TERMINAL_H - -void die(const char* message); -void disable_raw_mode(); -void enable_raw_mode(); -int get_cursor_position(int* rows, int* cols); -int get_window_size(int* rows, int* cols); - -#endif -- cgit v1.2.3