From e97032761ef93d1f5d0442f43a4226334a4d087d Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 1 Aug 2014 12:12:54 +0200 Subject: [PATCH] Add line numbering to editor. --- doc/user-guide | 1 + editor/display.c++ | 20 +++++++++++++++++++- editor/editor.h++ | 1 + editor/modal.c++ | 23 +++++++++++++++++++++++ editor/modal.h++ | 4 ++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/doc/user-guide b/doc/user-guide index e64e4718..22d9c2a2 100644 --- a/doc/user-guide +++ b/doc/user-guide @@ -320,6 +320,7 @@ simple to use. It currently supports these keyboard commands: * `Ctrl-V` - Paste * `Ctrl-X` - Cut * `ESC language ` - enable syntax highlighting +* `ESC line-numbering ` - enable line numbering * `ESC margin ` - add right margin at column * `ESC popen ` - open command output * `ESC tabsize ` - change tab size diff --git a/editor/display.c++ b/editor/display.c++ index ac69d85f..cafd7c95 100644 --- a/editor/display.c++ +++ b/editor/display.c++ @@ -26,6 +26,7 @@ #include #include +#include #include #include "display.h++" @@ -138,9 +139,15 @@ void render_editor(struct editor* editor, struct terminal_state* state) state->data[header_start_len+i].character = wcs_file_name[i]; free(wcs_file_name); + size_t line_number_width = 1; + for ( size_t tmp = editor->lines_used; 10 <= tmp; tmp /= 10 ) + line_number_width++; + if ( !editor->line_numbering ) + line_number_width = 0; + // Calculate the dimensions of the viewport. size_t viewport_top = 1; - size_t viewport_left = 0; + size_t viewport_left = line_number_width; editor->viewport_width = (size_t) state->width; if ( editor->viewport_width < viewport_left ) @@ -202,6 +209,17 @@ void render_editor(struct editor* editor, struct terminal_state* state) chars = NULL, chars_length = 0; else chars += page_x_offset, chars_length -= page_x_offset; + for ( size_t x = 0; x < line_number_width; x++ ) + raw_data_line[x] = make_terminal_datum(L' ', 0x70); + if ( editor->line_numbering && line_index < editor->lines_used ) + { + char line_number[sizeof(size_t) * 3]; + snprintf(line_number, sizeof(line_number), "%zu", line_index + 1); + size_t length = strlen(line_number); + size_t offset = line_number_width - length; + for ( size_t i = 0; i < length; i++ ) + raw_data_line[offset + i].character = btowc(line_number[i]); + } for ( size_t x = 0; x < editor->viewport_width; x++ ) { size_t column_index = page_x_offset + x; diff --git a/editor/editor.h++ b/editor/editor.h++ index 709657f5..8c9cd769 100644 --- a/editor/editor.h++ +++ b/editor/editor.h++ @@ -83,6 +83,7 @@ struct editor bool dirty; bool modal_error; bool highlight_source; + bool line_numbering; }; __attribute__((unused)) diff --git a/editor/modal.c++ b/editor/modal.c++ index afe9a94f..d7d788d4 100644 --- a/editor/modal.c++ +++ b/editor/modal.c++ @@ -37,6 +37,16 @@ #include "modal.h++" #include "multibyte.h++" +bool is_truth_string(const char* truth) +{ + return !strcmp(truth, "on") || !strcmp(truth, "off"); +} + +bool is_truth_true(const char* truth) +{ + return strcmp(truth, "off") != 0; +} + void editor_modal_left(struct editor* editor) { if ( editor->modal_cursor ) @@ -210,6 +220,17 @@ void editor_modal_language(struct editor* editor, const char* language) editor_type_edit(editor); } +void editor_modal_line_numbering(struct editor* editor, const char* truth) +{ + if ( !is_truth_string(truth) ) + { + editor->modal_error = true; + return; + } + editor->line_numbering = is_truth_true(truth); + editor_type_edit(editor); +} + bool is_modal_command(const char* cmd, const char* candidate, const char** rest) { size_t candidate_len = strlen(candidate); @@ -249,6 +270,8 @@ void editor_modal_command(struct editor* editor, const char* cmd) editor_modal_tabsize(editor, cmd); else if ( is_modal_command(cmd, "language", &cmd) ) editor_modal_language(editor, cmd); + else if ( is_modal_command(cmd, "line-numbering", &cmd) ) + editor_modal_line_numbering(editor, cmd); else editor->modal_error = true; } diff --git a/editor/modal.h++ b/editor/modal.h++ index ed8e1869..17214a75 100644 --- a/editor/modal.h++ +++ b/editor/modal.h++ @@ -25,6 +25,9 @@ struct editor; +bool is_truth_string(const char* truth); +bool is_truth_true(const char* truth); + void editor_modal_left(struct editor* editor); void editor_modal_right(struct editor* editor); void editor_modal_home(struct editor* editor); @@ -40,6 +43,7 @@ void editor_modal_margin(struct editor* editor, const char* marginstr); void editor_modal_popen(struct editor* editor, const char* cmd); void editor_modal_tabsize(struct editor* editor, const char* tabsizestr); void editor_modal_language(struct editor* editor, const char* language); +void editor_modal_line_numbering(struct editor* editor, const char* truth); bool is_modal_command(const char* cmd, const char* candidate, const char** rest); void editor_modal_command(struct editor* editor, const char* cmd);