diff --git a/README.md b/README.md index 595b121..25cdace 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ This is me coding along with Mr. [Tsoding](https://www.twitch.tv/tsoding)'s stream about creating a text editor from scratch using C. Left off at https://www.twitch.tv/videos/1685122716?t=02h25m02s on Jan 11, 2023 + +# Differences +I've decided to go with vim-like key bindings, and I've also modernized some of the code, for example when initializing the `term` instance. diff --git a/input.txt b/input.txt index 77c0d7b..e6a2f7b 100644 --- a/input.txt +++ b/input.txt @@ -1,4 +1,3 @@ Hello, Zed! Foo, Bar! Test, test - diff --git a/main.c b/main.c index a787151..463a80e 100644 --- a/main.c +++ b/main.c @@ -23,7 +23,7 @@ typedef struct { size_t cursor; } Editor; -void editor_recompute_lines(Editor *e) { +void editor_compute_lines(Editor *e) { size_t begin = 0; for (size_t i = 0; i < e->data_count; ++i) { if (e->data[i] == '\n') { @@ -40,11 +40,13 @@ void editor_recompute_lines(Editor *e) { size_t editor_current_line(const Editor *e) { assert(e->cursor <= e->data_count); + Line line; for (size_t i = 0; i < e->lines_count; ++i) { + line = e->lines[i]; if ( - e->lines[i].begin <= e->cursor + line.begin <= e->cursor && - e->cursor <= e->lines[i].end + e->cursor <= line.end ) return i; } return 0; @@ -62,8 +64,6 @@ void editor_rerender(const Editor *e) { ); } -static Editor editor = {0}; - int editor_start_interactive(Editor *e) { if (!isatty(0)) { fprintf(stderr, "Please run in the terminal!\n"); @@ -111,7 +111,7 @@ int editor_start_interactive(Editor *e) { } } break; case 'l': { - if (e->cursor < EDITOR_CAPACITY) { + if (e->cursor < e->data_count - 1) { e->cursor += 1; } } break; @@ -127,8 +127,9 @@ int editor_start_interactive(Editor *e) { } -int main(int argc, char **argv) -{ +static Editor editor; + +int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "Usage: zed \n"); fprintf(stderr, "ERROR: no input file was provided\n"); @@ -147,11 +148,13 @@ int main(int argc, char **argv) return 1; } + // load contents of file into `editor.data` + // assign length of file to `editor.data_count` editor.data_count = fread(editor.data, 1, EDITOR_CAPACITY, f); fclose(f); - - editor_recompute_lines(&editor); + editor_compute_lines(&editor); return editor_start_interactive(&editor); + return 0; }