Add editor highlight support for diff.
This commit is contained in:
parent
09084cf12d
commit
9554bab714
|
@ -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
|
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
|
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->rshift = false;
|
||||||
editor->dirty = false;
|
editor->dirty = false;
|
||||||
editor->modal_error = false;
|
editor->modal_error = false;
|
||||||
editor->highlight_source = false;
|
editor->highlight_source = LANGUAGE_NONE;
|
||||||
|
|
||||||
editor->lines_used = 1;
|
editor->lines_used = 1;
|
||||||
editor->lines_length = 1;
|
editor->lines_length = 1;
|
||||||
|
@ -100,7 +100,7 @@ void editor_reset_contents(struct editor* editor)
|
||||||
editor->lines[0].data = NULL;
|
editor->lines[0].data = NULL;
|
||||||
editor->lines[0].used = 0;
|
editor->lines[0].used = 0;
|
||||||
editor->lines[0].length = 0;
|
editor->lines[0].length = 0;
|
||||||
editor->highlight_source = false;
|
editor->highlight_source = LANGUAGE_NONE;
|
||||||
editor_cursor_set(editor, 0, 0);
|
editor_cursor_set(editor, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,7 +171,7 @@ bool editor_load_file(struct editor* editor, const char* path)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
editor->current_file_name = strdup(path);
|
editor->current_file_name = strdup(path);
|
||||||
editor->highlight_source = should_highlight_path(path);
|
editor->highlight_source = language_of_path(path);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -225,7 +225,7 @@ bool editor_save_file(struct editor* editor, const char* path)
|
||||||
|
|
||||||
editor->current_file_name = strdup(path);
|
editor->current_file_name = strdup(path);
|
||||||
editor->dirty = false;
|
editor->dirty = false;
|
||||||
editor->highlight_source = should_highlight_path(path);
|
editor->highlight_source = language_of_path(path);
|
||||||
|
|
||||||
return fclose(fp) != EOF;
|
return fclose(fp) != EOF;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,8 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "highlight.h++"
|
||||||
|
|
||||||
struct line
|
struct line
|
||||||
{
|
{
|
||||||
wchar_t* data;
|
wchar_t* data;
|
||||||
|
@ -82,7 +84,7 @@ struct editor
|
||||||
bool rshift;
|
bool rshift;
|
||||||
bool dirty;
|
bool dirty;
|
||||||
bool modal_error;
|
bool modal_error;
|
||||||
bool highlight_source;
|
enum language highlight_source;
|
||||||
bool line_numbering;
|
bool line_numbering;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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
|
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
|
under the terms of the GNU General Public License as published by the Free
|
||||||
|
@ -33,13 +33,13 @@
|
||||||
#include "editor.h++"
|
#include "editor.h++"
|
||||||
#include "highlight.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);
|
size_t path_length = strlen(path);
|
||||||
if ( 2 <= path_length &&
|
if ( 2 <= path_length &&
|
||||||
(!strcmp(path+path_length-2, ".c") ||
|
(!strcmp(path+path_length-2, ".c") ||
|
||||||
!strcmp(path+path_length-2, ".h")) )
|
!strcmp(path+path_length-2, ".h")) )
|
||||||
return true;
|
return LANGUAGE_C_CXX;
|
||||||
if ( 4 <= path_length &&
|
if ( 4 <= path_length &&
|
||||||
(!strcmp(path+path_length-4, ".c++") ||
|
(!strcmp(path+path_length-4, ".c++") ||
|
||||||
!strcmp(path+path_length-4, ".h++") ||
|
!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, ".hxx") ||
|
||||||
!strcmp(path+path_length-4, ".cpp") ||
|
!strcmp(path+path_length-4, ".cpp") ||
|
||||||
!strcmp(path+path_length-4, ".hpp")) )
|
!strcmp(path+path_length-4, ".hpp")) )
|
||||||
return true;
|
return LANGUAGE_C_CXX;
|
||||||
return false;
|
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 binary = false;
|
||||||
bool hex = false;
|
bool hex = false;
|
||||||
|
@ -131,10 +134,13 @@ size_t recognize_constant(const wchar_t* string, size_t string_length)
|
||||||
return result;
|
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)
|
void editor_colorize(struct editor* editor)
|
||||||
{
|
{
|
||||||
if ( editor->color_lines_length != editor->lines_used ||
|
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++ )
|
for ( size_t i = 0; i < editor->color_lines_used; i++ )
|
||||||
delete[] editor->color_lines[i].data;
|
delete[] editor->color_lines[i].data;
|
||||||
|
@ -144,7 +150,7 @@ void editor_colorize(struct editor* editor)
|
||||||
editor->color_lines = NULL;
|
editor->color_lines = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !editor->highlight_source )
|
if ( editor->highlight_source == LANGUAGE_NONE )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ( !editor->color_lines )
|
if ( !editor->color_lines )
|
||||||
|
@ -177,6 +183,16 @@ void editor_colorize(struct editor* editor)
|
||||||
editor->color_lines[i].length = editor->lines[i].used;
|
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
|
enum
|
||||||
{
|
{
|
||||||
STATE_INIT,
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
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
|
under the terms of the GNU General Public License as published by the Free
|
||||||
|
@ -25,7 +25,14 @@
|
||||||
|
|
||||||
struct editor;
|
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);
|
void editor_colorize(struct editor* editor);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -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
|
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
|
under the terms of the GNU General Public License as published by the Free
|
||||||
|
@ -34,6 +34,7 @@
|
||||||
#include "command.h++"
|
#include "command.h++"
|
||||||
#include "cursor.h++"
|
#include "cursor.h++"
|
||||||
#include "editor.h++"
|
#include "editor.h++"
|
||||||
|
#include "highlight.h++"
|
||||||
#include "modal.h++"
|
#include "modal.h++"
|
||||||
#include "multibyte.h++"
|
#include "multibyte.h++"
|
||||||
|
|
||||||
|
@ -208,12 +209,17 @@ void editor_modal_language(struct editor* editor, const char* language)
|
||||||
{
|
{
|
||||||
if ( !language[0] || !strcmp(language, "none") )
|
if ( !language[0] || !strcmp(language, "none") )
|
||||||
{
|
{
|
||||||
editor->highlight_source = false;
|
editor->highlight_source = LANGUAGE_NONE;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( !strcmp(language, "c") || !strcmp(language, "c++") )
|
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;
|
return;
|
||||||
}
|
}
|
||||||
editor->modal_error = true;
|
editor->modal_error = true;
|
||||||
|
|
Loading…
Reference in New Issue