diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | editor.c | 137 | ||||
-rw-r--r-- | editor.h | 9 | ||||
-rw-r--r-- | file.c | 18 | ||||
-rw-r--r-- | input.c | 9 | ||||
-rw-r--r-- | line.c | 150 | ||||
-rw-r--r-- | line.h | 33 | ||||
-rw-r--r-- | row.c | 149 | ||||
-rw-r--r-- | row.h | 30 | ||||
-rw-r--r-- | syntax.c | 58 | ||||
-rw-r--r-- | syntax.h | 4 | ||||
-rw-r--r-- | window.c | 2 |
12 files changed, 307 insertions, 294 deletions
@@ -9,7 +9,7 @@ OBJS=main.o \ file.o \ font.o \ input.o \ - row.o \ + line.o \ syntax.o \ window.o @@ -7,20 +7,19 @@ #include <string.h> #include <unistd.h> -#include "buffer.h" #include "input.h" #include "syntax.h" -#include "row.h" +#include "window.h" void init_editor(struct editor_state* editor) { editor->cursor_x = 0; editor->cursor_y = 0; editor->cursor_display_x = 0; - editor->row_offset = 0; + editor->line_offset = 0; editor->col_offset = 0; - editor->row_count = 0; - editor->rows = NULL; + editor->num_lines = 0; + editor->lines = NULL; editor->dirty = 0; editor->filename = NULL; editor->status_message[0] = '\0'; @@ -58,16 +57,16 @@ void editor_move_left(struct editor_state *editor) editor->cursor_x--; } else if (editor->cursor_y > 0) { editor->cursor_y--; - editor->cursor_x = editor->rows[editor->cursor_y].size; + editor->cursor_x = editor->lines[editor->cursor_y].size; } } void editor_move_right(struct editor_state *editor) { - struct editor_row* row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y]; - if (row && editor->cursor_x < row->size) { + line_t *line = (editor->cursor_y >= editor->num_lines) ? NULL : &editor->lines[editor->cursor_y]; + if (line && editor->cursor_x < line->size) { editor->cursor_x++; - } else if (row && editor->cursor_x == row->size) { + } else if (line && editor->cursor_x == line->size) { editor->cursor_y++; editor->cursor_x = 0; } @@ -78,43 +77,49 @@ void editor_move_up(struct editor_state *editor) if (editor->cursor_y != 0) editor->cursor_y--; - struct editor_row *row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y]; - int row_length = row ? row->size : 0; - if (editor->cursor_x > row_length) - editor->cursor_x = row_length; + line_t *line = (editor->cursor_y >= editor->num_lines) ? NULL : &editor->lines[editor->cursor_y]; + int line_length = line ? line->size : 0; + if (editor->cursor_x > line_length) + editor->cursor_x = line_length; } void editor_move_down(struct editor_state *editor) { - if (editor->cursor_y != editor->row_count - 1) + if (editor->cursor_y != editor->num_lines - 1) editor->cursor_y++; - struct editor_row *row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y]; - int row_length = row ? row->size : 0; - if (editor->cursor_x > row_length) - editor->cursor_x = row_length; + line_t *line = (editor->cursor_y >= editor->num_lines) ? NULL : &editor->lines[editor->cursor_y]; + int line_length = line ? line->size : 0; + if (editor->cursor_x > line_length) + editor->cursor_x = line_length; +} + +void editor_move_end(struct editor_state *editor) +{ + if (editor->cursor_y < editor->num_lines) + editor->cursor_x = editor->lines[editor->cursor_y].size; } void editor_insert_char(struct editor_state* editor, int c) { - if (editor->cursor_y == editor->row_count) - insert_row(editor, editor->row_count, "", 0); + if (editor->cursor_y == editor->num_lines) + editor_insert_line(editor, editor->num_lines, "", 0); - row_insert_char(editor, &editor->rows[editor->cursor_y], editor->cursor_x, c); + line_insert_char(editor, &editor->lines[editor->cursor_y], editor->cursor_x, c); editor->cursor_x++; } void editor_insert_newline(struct editor_state* editor) { if (editor->cursor_x == 0) { - insert_row(editor, editor->cursor_y, "", 0); + editor_insert_line(editor, editor->cursor_y, "", 0); } else { - struct editor_row* row = &editor->rows[editor->cursor_y]; - insert_row(editor, editor->cursor_y + 1, &row->chars[editor->cursor_x], row->size - editor->cursor_x); - row = &editor->rows[editor->cursor_y]; - row->size = editor->cursor_x; - row->chars[row->size] = '\0'; - update_row(editor, row); + line_t *line = &editor->lines[editor->cursor_y]; + editor_insert_line(editor, editor->cursor_y + 1, &line->chars[editor->cursor_x], line->size - editor->cursor_x); + line = &editor->lines[editor->cursor_y]; + line->size = editor->cursor_x; + line->chars[line->size] = '\0'; + editor_update_line(editor, line); } editor->cursor_y++; editor->cursor_x = 0; @@ -122,33 +127,33 @@ void editor_insert_newline(struct editor_state* editor) void editor_delete_char(struct editor_state* editor) { - if (editor->cursor_y == editor->row_count) + if (editor->cursor_y == editor->num_lines) return; if (editor->cursor_x == 0 && editor->cursor_y == 0) return; - struct editor_row* row = &editor->rows[editor->cursor_y]; + line_t *line = &editor->lines[editor->cursor_y]; if (editor->cursor_x > 0) { - row_delete_char(editor, row, editor->cursor_x - 1); + line_delete_char(editor, line, editor->cursor_x - 1); editor->cursor_x--; } else { - editor->cursor_x = editor->rows[editor->cursor_y - 1].size; - row_append_string(editor, &editor->rows[editor->cursor_y - 1], row->chars, row->size); - delete_row(editor, editor->cursor_y); + editor->cursor_x = editor->lines[editor->cursor_y - 1].size; + line_append_string(editor, &editor->lines[editor->cursor_y - 1], line->chars, line->size); + editor_delete_line(editor, editor->cursor_y); editor->cursor_y--; } } void editor_add_line_above(struct editor_state* editor) { - insert_row(editor, editor->cursor_y, "", 0); + editor_insert_line(editor, editor->cursor_y, "", 0); editor->cursor_x = 0; } void editor_add_line_below(struct editor_state* editor) { - insert_row(editor, editor->cursor_y + 1, "", 0); + editor_insert_line(editor, editor->cursor_y + 1, "", 0); editor->cursor_y++; editor->cursor_x = 0; } @@ -162,7 +167,7 @@ static void editor_find_callback(struct editor_state* editor, char* query, int k static char* saved_highlight; if (saved_highlight) { - memset(editor->rows[saved_highlight_line].highlight, (size_t)saved_highlight, editor->rows[saved_highlight_line].render_size); + memset(editor->lines[saved_highlight_line].highlight, (size_t)saved_highlight, editor->lines[saved_highlight_line].render_size); free(saved_highlight); saved_highlight = NULL; } @@ -187,27 +192,27 @@ static void editor_find_callback(struct editor_state* editor, char* query, int k int current = last_match; int i; - for (i = 0; i < editor->row_count; i++) { + for (i = 0; i < editor->num_lines; i++) { current += direction; if (current == -1) { - current = editor->row_count - 1; - } else if (current == editor->row_count) { + current = editor->num_lines - 1; + } else if (current == editor->num_lines) { current = 0; } - struct editor_row* row = &editor->rows[current]; - char* match = strstr(row->render, query); + line_t *line = &editor->lines[current]; + char* match = strstr(line->render, query); if (match) { last_match = current; editor->cursor_y = current; - editor->cursor_x = row_display_x_to_x(row, match - row->render); - editor->row_offset = editor->row_count; + editor->cursor_x = row_display_x_to_x(line, match - line->render); + editor->line_offset = editor->num_lines; saved_highlight_line = current; - saved_highlight = malloc(row->render_size); - memcpy(saved_highlight, row->highlight, row->render_size); - memset(&row->highlight[match - row->render], HIGHLIGHT_MATCH, strlen(query)); + saved_highlight = malloc(line->render_size); + memcpy(saved_highlight, line->highlight, line->render_size); + memset(&line->highlight[match - line->render], HIGHLIGHT_MATCH, strlen(query)); break; } } @@ -218,7 +223,7 @@ void editor_find(struct editor_state* editor) int saved_cursor_x = editor->cursor_x; int saved_cursor_y = editor->cursor_y; int saved_col_offset = editor->col_offset; - int saved_row_offset = editor->row_offset; + int saved_line_offset = editor->line_offset; char* query = editor_prompt(editor, "Search: %s (Use Esc/Arrows/Enter)", editor_find_callback); if (query) { @@ -227,21 +232,21 @@ void editor_find(struct editor_state* editor) editor->cursor_x = saved_cursor_x; editor->cursor_y = saved_cursor_y; editor->col_offset = saved_col_offset; - editor->row_offset = saved_row_offset; + editor->line_offset = saved_line_offset; } } void editor_scroll(struct editor_state* editor) { editor->cursor_display_x = 0; - if (editor->cursor_y < editor->row_count) - editor->cursor_display_x = row_x_to_display_x(&editor->rows[editor->cursor_y], editor->cursor_x); + if (editor->cursor_y < editor->num_lines) + editor->cursor_display_x = row_x_to_display_x(&editor->lines[editor->cursor_y], editor->cursor_x); - if (editor->cursor_y < editor->row_offset) - editor->row_offset = editor->cursor_y; + if (editor->cursor_y < editor->line_offset) + editor->line_offset = editor->cursor_y; - if (editor->cursor_y >= editor->row_offset + editor->screen_rows) - editor->row_offset = editor->cursor_y - editor->screen_rows + 1; + if (editor->cursor_y >= editor->line_offset + editor->screen_rows) + editor->line_offset = editor->cursor_y - editor->screen_rows + 1; if (editor->cursor_display_x < editor->col_offset) editor->col_offset = editor->cursor_display_x; @@ -254,9 +259,9 @@ void editor_draw_rows(struct editor_state* editor, struct append_buffer* buffer) { int y; for (y = 0; y < editor->screen_rows; y++) { - int file_row = y + editor->row_offset; - if (file_row >= editor->row_count) { - if (editor->row_count == 0 && y == editor->screen_rows / 3) { + int file_line = y + editor->line_offset; + if (file_line >= editor->num_lines) { + if (editor->num_lines == 0 && y == editor->screen_rows / 3) { char welcome[80]; int welcome_length = snprintf(welcome, sizeof(welcome), "Welcome to cflip text editor"); @@ -277,13 +282,13 @@ void editor_draw_rows(struct editor_state* editor, struct append_buffer* buffer) ab_append(buffer, "~", 1); } } else { - int length = editor->rows[file_row].render_size - editor->col_offset; + int length = editor->lines[file_line].render_size - editor->col_offset; if (length < 0) length = 0; if (length > editor->screen_cols) length = editor->screen_cols; - char* c = &editor->rows[file_row].render[editor->col_offset]; - unsigned char* highlight = &editor->rows[file_row].highlight[editor->col_offset]; + char* c = &editor->lines[file_line].render[editor->col_offset]; + unsigned char* highlight = &editor->lines[file_line].highlight[editor->col_offset]; int current_colour = -1; int j; @@ -309,8 +314,8 @@ void editor_draw_rows(struct editor_state* editor, struct append_buffer* buffer) void editor_draw_status_bar(struct editor_state* editor, struct append_buffer* buffer) { 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); + int length = snprintf(status, sizeof(status), "%.20s - %d lines %s", editor->filename ? editor->filename : "[New File]", editor->num_lines, 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->num_lines); if (length > editor->screen_cols) length = editor->screen_cols; @@ -348,7 +353,7 @@ void editor_draw_message_bar(struct editor_state* editor, struct append_buffer* void editor_destroy(struct editor_state *editor) { free(editor->filename); - for (int i = 0; i < editor->row_count; i++) - free_row(&editor->rows[i]); - free(editor->rows); + for (int i = 0; i < editor->num_lines; i++) + free_line(&editor->lines[i]); + free(editor->lines); } @@ -2,7 +2,9 @@ #define _EDITOR_H #include <time.h> + #include "buffer.h" +#include "line.h" enum editor_mode { EDITOR_MODE_NORMAL, @@ -13,12 +15,12 @@ enum editor_mode { struct editor_state { int cursor_x, cursor_y; int cursor_display_x; - int row_offset; + int line_offset; int col_offset; int screen_rows; int screen_cols; - int row_count; - struct editor_row* rows; + int num_lines; + line_t *lines; int dirty; char* filename; char status_message[80]; @@ -42,6 +44,7 @@ void editor_move_left(struct editor_state *); void editor_move_right(struct editor_state *); void editor_move_up(struct editor_state *); void editor_move_down(struct editor_state *); +void editor_move_end(struct editor_state *); void editor_insert_char(struct editor_state* editor, int c); void editor_insert_newline(struct editor_state* editor); @@ -8,25 +8,25 @@ #include <unistd.h> #include "error.h" -#include "row.h" +#include "line.h" #include "syntax.h" -char* editor_rows_to_string(struct editor_state* editor, int* buffer_length) +static char *lines_to_string(struct editor_state *editor, int *buffer_length) { int total_length = 0; int j; - for (j = 0; j < editor->row_count; j++) - total_length += editor->rows[j].size + 1; + for (j = 0; j < editor->num_lines; j++) + total_length += editor->lines[j].size + 1; *buffer_length = total_length; char* buffer = malloc(total_length); char* p = buffer; - for (j = 0; j < editor->row_count; j++) { - memcpy(p, editor->rows[j].chars, editor->rows[j].size); - p += editor->rows[j].size; + for (j = 0; j < editor->num_lines; j++) { + memcpy(p, editor->lines[j].chars, editor->lines[j].size); + p += editor->lines[j].size; *p = '\n'; p++; } @@ -60,7 +60,7 @@ void editor_open(struct editor_state* editor, char* filename) while (line_length > 0 && (line[line_length - 1] == '\n' || line[line_length - 1] == '\r')) line_length--; - insert_row(editor, editor->row_count, line, line_length); + editor_insert_line(editor, editor->num_lines, line, line_length); } free(line); @@ -81,7 +81,7 @@ void editor_save(struct editor_state* editor) } int length; - char* buffer = editor_rows_to_string(editor, &length); + char *buffer = lines_to_string(editor, &length); int fd = open(editor->filename, O_RDWR | O_CREAT, 0644); if (fd != -1) { @@ -1,7 +1,8 @@ #include "input.h" #include "editor.h" -#include "row.h" +#include "file.h" +#include "line.h" void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) { @@ -45,8 +46,8 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) editor->cursor_x = 0; break; case SDLK_4: - if (keysym->mod & KMOD_SHIFT && editor->cursor_y < editor->row_count) - editor->cursor_x = editor->rows[editor->cursor_y].size; + if (keysym->mod & KMOD_SHIFT) + editor_move_end(editor); break; case SDLK_SLASH: editor_find(editor); @@ -65,7 +66,7 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) break; case SDLK_a: if (keysym->mod & KMOD_SHIFT) - editor->cursor_x = editor->rows[editor->cursor_y].size; + editor_move_end(editor); else editor_move_right(editor); editor->mode = EDITOR_MODE_INSERT; @@ -0,0 +1,150 @@ +#include "line.h" + +#include <ctype.h> +#include <stdlib.h> +#include <string.h> + +#include "syntax.h" + +int row_x_to_display_x(line_t *line, int x) +{ + int display_x = 0; + int j; + + for (j = 0; j < x; j++) { + if (line->chars[j] == '\t') + display_x += (TAB_WIDTH - 1) - (display_x % TAB_WIDTH); + display_x++; + } + + return display_x; +} + +int row_display_x_to_x(line_t *line, int display_x) +{ + int current_display_x = 0; + int result_x; + + for (result_x = 0; result_x < line->size; result_x++) { + if (line->chars[result_x] == '\t') + result_x += (TAB_WIDTH - 1) - (current_display_x % TAB_WIDTH); + result_x++; + + if (current_display_x > display_x) + return result_x; + } + return result_x; +} + +void editor_update_line(struct editor_state *editor, line_t *line) +{ + int tabs = 0; + int j; + for (j = 0; j < line->size; j++) + if (line->chars[j] == '\t') tabs++; + + free(line->render); + line->render = malloc(line->size + tabs * (TAB_WIDTH - 1) + 1); + + int index = 0; + for (j = 0; j < line->size; j++) { + if (line->chars[j] == '\t') { + line->render[index++] = ' '; + while (index % TAB_WIDTH != 0) line->render[index++] = ' '; + } else { + line->render[index++] = line->chars[j]; + } + } + + line->render[index] = '\0'; + line->render_size = index; + + editor_update_syntax(editor, line); +} + +void editor_insert_line(struct editor_state *editor, int at, char* string, size_t length) +{ + if (at < 0 || at > editor->num_lines) + return; + + editor->lines = realloc(editor->lines, sizeof(line_t) * (editor->num_lines + 1)); + memmove(&editor->lines[at + 1], &editor->lines[at], sizeof(line_t) * (editor->num_lines - at)); + + for (int j = at + 1; j <= editor->num_lines; j++) + editor->lines[at].index++; + + editor->lines[at].index = at; + + editor->lines[at].size = length; + editor->lines[at].chars = malloc(length + 1); + memcpy(editor->lines[at].chars, string, length); + editor->lines[at].chars[length] = '\0'; + + editor->lines[at].render_size = 0; + editor->lines[at].render = NULL; + editor->lines[at].highlight = NULL; + editor->lines[at].highlight_open_comment = 0; + editor_update_line(editor, &editor->lines[at]); + + editor->num_lines++; + editor->dirty = 1; +} + +void free_line(line_t *line) +{ + free(line->render); + free(line->chars); + free(line->highlight); +} + +void editor_delete_line(struct editor_state *editor, int at) +{ + if (at < 0 || at >= editor->num_lines) + return; + + free_line(&editor->lines[at]); + memmove(&editor->lines[at], &editor->lines[at + 1], sizeof(line_t) * (editor->num_lines - at - 1)); + + for (int j = at; j < editor->num_lines; j++) + editor->lines[j].index--; + + editor->num_lines--; + editor->dirty = 1; +} + +void line_insert_char(struct editor_state *editor, line_t *line, int at, int c) +{ + if (at < 0 || at > line->size) + at = line->size; + + line->chars = realloc(line->chars, line->size + 2); + memmove(&line->chars[at + 1], &line->chars[at], line->size - at + 1); + line->size++; + line->chars[at] = c; + + editor_update_line(editor, line); + + editor->dirty = 1; +} + +void line_append_string(struct editor_state *editor, line_t *line, char* string, size_t length) +{ + line->chars = realloc(line->chars, line->size + length + 1); + memcpy(&line->chars[line->size], string, length); + line->size += length; + line->chars[line->size] = '\0'; + + editor_update_line(editor, line); + editor->dirty = 1; +} + +void line_delete_char(struct editor_state *editor, line_t *line, int at) +{ + if (at < 0 || at >= line->size) + return; + + memmove(&line->chars[at], &line->chars[at + 1], line->size - at); + line->size--; + editor_update_line(editor, line); + editor->dirty = 1; +} @@ -0,0 +1,33 @@ +#ifndef _LINE_H +#define _LINE_H + +#include <stddef.h> + +#define TAB_WIDTH 4 + +typedef struct { + int index; + int size; + char* chars; + int render_size; + char* render; + unsigned char* highlight; + int highlight_open_comment; +} line_t; + +struct editor_state; + +int row_x_to_display_x(line_t*, int x); +int row_display_x_to_x(line_t*, int display_x); + +void editor_update_line(struct editor_state*, line_t*); +void editor_insert_line(struct editor_state*, int at, char *string, size_t length); +void editor_delete_line(struct editor_state*, int at); + +void line_insert_char(struct editor_state*, line_t*, int at, int c); +void line_append_string(struct editor_state*, line_t*, char* string, size_t length); +void line_delete_char(struct editor_state*, line_t*, int at); + +void free_line(line_t*); + +#endif @@ -1,149 +0,0 @@ -#include "row.h" - -#include <stdlib.h> -#include <string.h> - -#include "syntax.h" - -int row_x_to_display_x(struct editor_row* row, int x) -{ - int display_x = 0; - int j; - - for (j = 0; j < x; j++) { - if (row->chars[j] == '\t') - display_x += (TAB_WIDTH - 1) - (display_x % TAB_WIDTH); - display_x++; - } - - return display_x; -} - -int row_display_x_to_x(struct editor_row* row, int display_x) -{ - int current_display_x = 0; - int result_x; - - for (result_x = 0; result_x < row->size; result_x++) { - if (row->chars[result_x] == '\t') - result_x += (TAB_WIDTH - 1) - (current_display_x % TAB_WIDTH); - result_x++; - - if (current_display_x > display_x) - return result_x; - } - return result_x; -} - -void update_row(struct editor_state* editor, struct editor_row* row) -{ - int tabs = 0; - int j; - for (j = 0; j < row->size; j++) - if (row->chars[j] == '\t') tabs++; - - free(row->render); - row->render = malloc(row->size + tabs * (TAB_WIDTH - 1) + 1); - - int index = 0; - for (j = 0; j < row->size; j++) { - if (row->chars[j] == '\t') { - row->render[index++] = ' '; - while (index % TAB_WIDTH != 0) row->render[index++] = ' '; - } else { - row->render[index++] = row->chars[j]; - } - } - - row->render[index] = '\0'; - row->render_size = index; - - editor_update_syntax(editor, row); -} - -void insert_row(struct editor_state* editor, int at, char* string, size_t length) -{ - if (at < 0 || at > editor->row_count) - return; - - editor->rows = realloc(editor->rows, sizeof(struct editor_row) * (editor->row_count + 1)); - memmove(&editor->rows[at + 1], &editor->rows[at], sizeof(struct editor_row) * (editor->row_count - at)); - - for (int j = at + 1; j <= editor->row_count; j++) - editor->rows[at].index++; - - editor->rows[at].index = at; - - editor->rows[at].size = length; - editor->rows[at].chars = malloc(length + 1); - memcpy(editor->rows[at].chars, string, length); - editor->rows[at].chars[length] = '\0'; - - editor->rows[at].render_size = 0; - editor->rows[at].render = NULL; - editor->rows[at].highlight = NULL; - editor->rows[at].highlight_open_comment = 0; - update_row(editor, &editor->rows[at]); - - editor->row_count++; - editor->dirty = 1; -} - -void free_row(struct editor_row* row) -{ - free(row->render); - free(row->chars); - free(row->highlight); -} - -void delete_row(struct editor_state* editor, int at) -{ - if (at < 0 || at >= editor->row_count) - return; - - free_row(&editor->rows[at]); - memmove(&editor->rows[at], &editor->rows[at + 1], sizeof(struct editor_row) * (editor->row_count - at - 1)); - - for (int j = at; j < editor->row_count; j++) - editor->rows[j].index--; - - editor->row_count--; - editor->dirty = 1; -} - -void row_insert_char(struct editor_state* editor, struct editor_row* row, int at, int c) -{ - if (at < 0 || at > row->size) - at = row->size; - - row->chars = realloc(row->chars, row->size + 2); - memmove(&row->chars[at + 1], &row->chars[at], row->size - at + 1); - row->size++; - row->chars[at] = c; - - update_row(editor, row); - - editor->dirty = 1; -} - -void row_append_string(struct editor_state* editor, struct editor_row* row, char* string, size_t length) -{ - row->chars = realloc(row->chars, row->size + length + 1); - memcpy(&row->chars[row->size], string, length); - row->size += length; - row->chars[row->size] = '\0'; - - update_row(editor, row); - editor->dirty = 1; -} - -void row_delete_char(struct editor_state* editor, struct editor_row* row, int at) -{ - if (at < 0 || at >= row->size) - return; - - memmove(&row->chars[at], &row->chars[at + 1], row->size - at); - row->size--; - update_row(editor, row); - editor->dirty = 1; -} @@ -1,30 +0,0 @@ -#ifndef _ROW_H -#define _ROW_H - -#include <ctype.h> - -#include "editor.h" - -#define TAB_WIDTH 4 - -struct editor_row { - int index; - int size; - char* chars; - int render_size; - char* render; - unsigned char* highlight; - int highlight_open_comment; -}; - -int row_x_to_display_x(struct editor_row* row, int x); -int row_display_x_to_x(struct editor_row* row, int display_x); -void update_row(struct editor_state* editor, struct editor_row* row); -void insert_row(struct editor_state* editor, int at, char* string, size_t length); -void free_row(struct editor_row* row); -void delete_row(struct editor_state* editor, int at); -void row_insert_char(struct editor_state* editor, struct editor_row* row, int at, int c); -void row_append_string(struct editor_state* editor, struct editor_row* row, char* string, size_t length); -void row_delete_char(struct editor_state* editor, struct editor_row* row, int at); - -#endif @@ -1,6 +1,8 @@ #include "syntax.h" +#include <ctype.h> #include <string.h> + #include "editor.h" char* c_highlight_extensions[] = { ".c", ".h", ".cpp", ".cc", NULL }; @@ -29,10 +31,10 @@ int is_separator(int c) return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL; } -void editor_update_syntax(struct editor_state* editor, struct editor_row* row) +void editor_update_syntax(struct editor_state *editor, line_t *line) { - row->highlight = realloc(row->highlight, row->render_size); - memset(row->highlight, HIGHLIGHT_NORMAL, row->render_size); + line->highlight = realloc(line->highlight, line->render_size); + memset(line->highlight, HIGHLIGHT_NORMAL, line->render_size); if (editor->syntax == NULL) return; @@ -50,25 +52,25 @@ void editor_update_syntax(struct editor_state* editor, struct editor_row* row) int previous_separator = 1; int in_string = 0; - int in_comment = (row->index > 0 && editor->rows[row->index - 1].highlight_open_comment); + int in_comment = (line->index > 0 && editor->lines[line->index - 1].highlight_open_comment); int i = 0; - while (i < row->render_size) { - char c = row->render[i]; - unsigned char previous_highlight = (i > 0) ? row->highlight[i - 1] : HIGHLIGHT_NORMAL; + while (i < line->render_size) { + char c = line->render[i]; + unsigned char previous_highlight = (i > 0) ? line->highlight[i - 1] : HIGHLIGHT_NORMAL; if (single_line_comment_start_length && !in_string && !in_comment) { - if (!strncmp(&row->render[i], single_line_comment_start, single_line_comment_start_length)) { - memset(&row->highlight[i], HIGHLIGHT_COMMENT, row->render_size - i); + if (!strncmp(&line->render[i], single_line_comment_start, single_line_comment_start_length)) { + memset(&line->highlight[i], HIGHLIGHT_COMMENT, line->render_size - i); break; } } if (multi_line_comment_start_length && multi_line_comment_end_length && !in_string) { if (in_comment) { - row->highlight[i] = HIGHLIGHT_MULTILINE_COMMENT; - if (!strncmp(&row->render[i], multi_line_comment_end, multi_line_comment_end_length)) { - memset(&row->highlight[i], HIGHLIGHT_MULTILINE_COMMENT, multi_line_comment_end_length); + line->highlight[i] = HIGHLIGHT_MULTILINE_COMMENT; + if (!strncmp(&line->render[i], multi_line_comment_end, multi_line_comment_end_length)) { + memset(&line->highlight[i], HIGHLIGHT_MULTILINE_COMMENT, multi_line_comment_end_length); i += multi_line_comment_end_length; in_comment = 0; @@ -78,8 +80,8 @@ void editor_update_syntax(struct editor_state* editor, struct editor_row* row) i++; continue; } - } else if (!strncmp(&row->render[i], multi_line_comment_start, multi_line_comment_start_length)) { - memset(&row->highlight[i], HIGHLIGHT_MULTILINE_COMMENT, multi_line_comment_start_length); + } else if (!strncmp(&line->render[i], multi_line_comment_start, multi_line_comment_start_length)) { + memset(&line->highlight[i], HIGHLIGHT_MULTILINE_COMMENT, multi_line_comment_start_length); i += multi_line_comment_start_length; in_comment = 1; continue; @@ -88,10 +90,10 @@ void editor_update_syntax(struct editor_state* editor, struct editor_row* row) if (editor->syntax->flags & HIGHLIGHT_FLAG_STRINGS) { if (in_string) { - row->highlight[i] = HIGHLIGHT_STRING; + line->highlight[i] = HIGHLIGHT_STRING; - if (c == '\\' && i + 1 < row->render_size) { - row->highlight[i + 1] = HIGHLIGHT_STRING; + if (c == '\\' && i + 1 < line->render_size) { + line->highlight[i + 1] = HIGHLIGHT_STRING; i += 2; continue; } @@ -105,7 +107,7 @@ void editor_update_syntax(struct editor_state* editor, struct editor_row* row) } else { if (c == '"' || c == '\'') { in_string = c; - row->highlight[i] = HIGHLIGHT_STRING; + line->highlight[i] = HIGHLIGHT_STRING; i++; continue; } @@ -114,7 +116,7 @@ void editor_update_syntax(struct editor_state* editor, struct editor_row* row) if (editor->syntax->flags & HIGHLIGHT_FLAG_NUMBERS) { if ((isdigit(c) && (previous_separator || previous_highlight == HIGHLIGHT_NUMBER)) || (c == '.' && previous_highlight == HIGHLIGHT_NUMBER)) { - row->highlight[i] = HIGHLIGHT_NUMBER; + line->highlight[i] = HIGHLIGHT_NUMBER; i++; previous_separator = 0; continue; @@ -130,8 +132,8 @@ void editor_update_syntax(struct editor_state* editor, struct editor_row* row) if (is_secondary) keyword_length--; - if (!strncmp(&row->render[i], keywords[j], keyword_length) && is_separator(row->render[i + keyword_length])) { - memset(&row->highlight[i], is_secondary ? HIGHLIGHT_KEYWORD2 : HIGHLIGHT_KEYWORD1, keyword_length); + if (!strncmp(&line->render[i], keywords[j], keyword_length) && is_separator(line->render[i + keyword_length])) { + memset(&line->highlight[i], is_secondary ? HIGHLIGHT_KEYWORD2 : HIGHLIGHT_KEYWORD1, keyword_length); i += keyword_length; break; } @@ -146,10 +148,10 @@ void editor_update_syntax(struct editor_state* editor, struct editor_row* row) i++; } - int changed = (row->highlight_open_comment != in_comment); - row->highlight_open_comment = in_comment; - if (changed && row->index + 1 < editor->row_count) - editor_update_syntax(editor, &editor->rows[row->index + 1]); + int changed = (line->highlight_open_comment != in_comment); + line->highlight_open_comment = in_comment; + if (changed && line->index + 1 < editor->num_lines) + editor_update_syntax(editor, &editor->lines[line->index + 1]); } int editor_syntax_to_colour(int highlight) @@ -184,10 +186,8 @@ void editor_select_syntax_highlight(struct editor_state* editor) if ((is_extension && extension && !strcmp(extension, syntax->filetype_match[i])) || (!is_extension && strstr(editor->filename, syntax->filetype_match[i]))) { editor->syntax = syntax; - int file_row; - for (file_row = 0; file_row < editor->row_count; file_row++) { - editor_update_syntax(editor, &editor->rows[file_row]); - } + for (int line = 0; line < editor->num_lines; line++) + editor_update_syntax(editor, &editor->lines[line]); return; } @@ -4,7 +4,7 @@ #include <stdlib.h> #include "editor.h" -#include "row.h" +#include "line.h" #define HIGHLIGHT_FLAG_NUMBERS (1 << 0) #define HIGHLIGHT_FLAG_STRINGS (1 << 1) @@ -32,7 +32,7 @@ enum editor_highlight { #define HIGHLIGHT_DATABASE_ENTRY_COUNT (sizeof(highlight_database) / sizeof(highlight_database[0])) -void editor_update_syntax(struct editor_state* editor, struct editor_row* row); +void editor_update_syntax(struct editor_state* editor, line_t*); int editor_syntax_to_colour(int highlight); void editor_select_syntax_highlight(struct editor_state* editor); @@ -115,7 +115,7 @@ void window_redraw(struct editor_state *editor) SDL_Rect cursor_rect; cursor_rect.x = editor->cursor_display_x * font.width; - cursor_rect.y = (editor->cursor_y - editor->row_offset) * font.height; + cursor_rect.y = (editor->cursor_y - editor->line_offset) * font.height; cursor_rect.w = font.width; cursor_rect.h = font.height; |