Add editor highlight support for diff.

This commit is contained in:
Jonas 'Sortie' Termansen 2015-07-09 23:00:16 +02:00
parent 09084cf12d
commit 9554bab714
5 changed files with 71 additions and 19 deletions

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014, 2015.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
@ -74,7 +74,7 @@ void initialize_editor(struct editor* editor)
editor->rshift = false;
editor->dirty = false;
editor->modal_error = false;
editor->highlight_source = false;
editor->highlight_source = LANGUAGE_NONE;
editor->lines_used = 1;
editor->lines_length = 1;
@ -100,7 +100,7 @@ void editor_reset_contents(struct editor* editor)
editor->lines[0].data = NULL;
editor->lines[0].used = 0;
editor->lines[0].length = 0;
editor->highlight_source = false;
editor->highlight_source = LANGUAGE_NONE;
editor_cursor_set(editor, 0, 0);
}
@ -171,7 +171,7 @@ bool editor_load_file(struct editor* editor, const char* path)
return false;
editor->current_file_name = strdup(path);
editor->highlight_source = should_highlight_path(path);
editor->highlight_source = language_of_path(path);
return true;
}
@ -225,7 +225,7 @@ bool editor_save_file(struct editor* editor, const char* path)
editor->current_file_name = strdup(path);
editor->dirty = false;
editor->highlight_source = should_highlight_path(path);
editor->highlight_source = language_of_path(path);
return fclose(fp) != EOF;
}

View File

@ -27,6 +27,8 @@
#include <stdint.h>
#include <stdio.h>
#include "highlight.h++"
struct line
{
wchar_t* data;
@ -82,7 +84,7 @@ struct editor
bool rshift;
bool dirty;
bool modal_error;
bool highlight_source;
enum language highlight_source;
bool line_numbering;
};

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014, 2015.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
@ -33,13 +33,13 @@
#include "editor.h++"
#include "highlight.h++"
bool should_highlight_path(const char* path)
enum language language_of_path(const char* path)
{
size_t path_length = strlen(path);
if ( 2 <= path_length &&
(!strcmp(path+path_length-2, ".c") ||
!strcmp(path+path_length-2, ".h")) )
return true;
return LANGUAGE_C_CXX;
if ( 4 <= path_length &&
(!strcmp(path+path_length-4, ".c++") ||
!strcmp(path+path_length-4, ".h++") ||
@ -47,11 +47,14 @@ bool should_highlight_path(const char* path)
!strcmp(path+path_length-4, ".hxx") ||
!strcmp(path+path_length-4, ".cpp") ||
!strcmp(path+path_length-4, ".hpp")) )
return true;
return false;
return LANGUAGE_C_CXX;
if ( (5 <= path_length && !strcmp(path+path_length-5, ".diff")) ||
(6 <= path_length && !strcmp(path+path_length-6, ".patch")) )
return LANGUAGE_DIFF;
return LANGUAGE_NONE;
}
size_t recognize_constant(const wchar_t* string, size_t string_length)
static size_t recognize_constant(const wchar_t* string, size_t string_length)
{
bool binary = false;
bool hex = false;
@ -131,10 +134,13 @@ size_t recognize_constant(const wchar_t* string, size_t string_length)
return result;
}
static void editor_colorize_c_cxx(struct editor* editor);
static void editor_colorize_diff(struct editor* editor);
void editor_colorize(struct editor* editor)
{
if ( editor->color_lines_length != editor->lines_used ||
!editor->highlight_source )
editor->highlight_source == LANGUAGE_NONE )
{
for ( size_t i = 0; i < editor->color_lines_used; i++ )
delete[] editor->color_lines[i].data;
@ -144,7 +150,7 @@ void editor_colorize(struct editor* editor)
editor->color_lines = NULL;
}
if ( !editor->highlight_source )
if ( editor->highlight_source == LANGUAGE_NONE )
return;
if ( !editor->color_lines )
@ -177,6 +183,16 @@ void editor_colorize(struct editor* editor)
editor->color_lines[i].length = editor->lines[i].used;
}
switch ( editor->highlight_source )
{
case LANGUAGE_NONE: break;
case LANGUAGE_C_CXX: editor_colorize_c_cxx(editor); break;
case LANGUAGE_DIFF: editor_colorize_diff(editor); break;
}
}
static void editor_colorize_c_cxx(struct editor* editor)
{
enum
{
STATE_INIT,
@ -515,3 +531,24 @@ void editor_colorize(struct editor* editor)
}
}
}
static void editor_colorize_diff(struct editor* editor)
{
for ( size_t y = 0; y < editor->lines_used; y++ )
{
struct line* line = &editor->lines[y];
uint8_t color = 7;
if ( line->used && line->data[0] == L'-' )
color = 1;
else if ( line->used && line->data[0] == L'+' )
color = 2;
else if ( line->used && line->data[0] == L'@' )
color = 6;
else if ( line->used && !iswblank(line->data[0]) )
color = 4 + 8;
for ( size_t x = 0; x < line->used; x++ )
{
editor->color_lines[y].data[x] = color;
}
}
}

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014, 2015.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
@ -25,7 +25,14 @@
struct editor;
bool should_highlight_path(const char* path);
enum language
{
LANGUAGE_NONE,
LANGUAGE_C_CXX,
LANGUAGE_DIFF,
};
enum language language_of_path(const char* path);
void editor_colorize(struct editor* editor);
#endif

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014, 2015.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
@ -34,6 +34,7 @@
#include "command.h++"
#include "cursor.h++"
#include "editor.h++"
#include "highlight.h++"
#include "modal.h++"
#include "multibyte.h++"
@ -208,12 +209,17 @@ void editor_modal_language(struct editor* editor, const char* language)
{
if ( !language[0] || !strcmp(language, "none") )
{
editor->highlight_source = false;
editor->highlight_source = LANGUAGE_NONE;
return;
}
if ( !strcmp(language, "c") || !strcmp(language, "c++") )
{
editor->highlight_source = true;
editor->highlight_source = LANGUAGE_C_CXX;
return;
}
if ( !strcmp(language, "diff") || !strcmp(language, "patch") )
{
editor->highlight_source = LANGUAGE_DIFF;
return;
}
editor->modal_error = true;