Add line numbering to editor.

This commit is contained in:
Jonas 'Sortie' Termansen 2014-08-01 12:12:54 +02:00
parent 915ce1367b
commit e97032761e
5 changed files with 48 additions and 1 deletions

View File

@ -320,6 +320,7 @@ simple to use. It currently supports these keyboard commands:
* `Ctrl-V` - Paste
* `Ctrl-X` - Cut
* `ESC language <c or c++>` - enable syntax highlighting
* `ESC line-numbering <on or off>` - enable line numbering
* `ESC margin <column>` - add right margin at column
* `ESC popen <command>` - open command output
* `ESC tabsize <desired-tab-size>` - change tab size

View File

@ -26,6 +26,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#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;

View File

@ -83,6 +83,7 @@ struct editor
bool dirty;
bool modal_error;
bool highlight_source;
bool line_numbering;
};
__attribute__((unused))

View File

@ -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;
}

View File

@ -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);