Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_OBJECTS})

add_compile_definitions(_XOPEN_SOURCE=700)

# --- symlink zum data Ordner erstellen
execute_process(
COMMAND ${CMAKE_COMMAND} -E create_symlink
${CMAKE_SOURCE_DIR}/data
${CMAKE_BINARY_DIR}/data
)

# CTest-Unterstützung aktivieren
enable_testing()
Expand Down
Binary file modified assets/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 29 additions & 22 deletions data/syntax/ini.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,46 @@ name = INI
file_extensions = ini

[block:root]
allowed_blocks = comment, section, keyvalue
child_blocks = comment, section, comment, assignment
Comment thread
defname marked this conversation as resolved.
color = 15

[block:comment]
# comments start with ";" or "#"
start = "^[;#]"
start = "(;|#)"
end = "$"
color = 242 # gray
allowed_blocks =
child_blocks =

[block:section]
# section: [section_name]
start = "^[ \t]*\\[.*\\]"
start = "\\[.*\\]"
end = "$"
color = 33 # cyan
allowed_blocks =
child_blocks = comment

[block:keyvalue]
# key = Value
start = "^[ \t]*[^;#= \t][^=]*="
end = "$"
color = 226 # yellow
allowed_blocks = string
[block:assignment]
start = [ \t]*[.\-_:a-zA-Z0-9]+[ \t]*=[ \t]*
end = $
color=82
child_blocks= number, string, bare_string, comment

[block:number]
start = [0-9]+
color = 43

[block:string]
# string-values
start = "\""
end = "\""
color = 82 # green
allowed_blocks = escape_sequence

[block:escape_sequence]
# escape inside strings
start = "\\\\"
end = "."
color = 208 # orange
allowed_blocks =
end = "\"|$"
color = 160
child_blocks = escape_chars

[block:bare_string]
start = "."
end = $
color = 67
ends_on = comment

[block:escape_chars]
start = "\\\\[\\\\\\nt'\"]"
color = 217

40 changes: 40 additions & 0 deletions data/syntax/md.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[meta]
name = Markdown
file_extensions = md

[block:root]
child_blocks = title1, title2, title3, codeblock, code, emph, img
color = 15

[block:title1]
# comments start with ";" or "#"
start = "^# (.*)$"
color = 33
child_blocks =

[block:title2]
start = "^## (.*)$"
color = 33

[block:title3]
start = "^### (.*)$"
color = 33

[block:code]
start = `[^`]*` # make pattern greedy by explicitly exclude ` from permitted characters
color = 64

Comment thread
defname marked this conversation as resolved.
[block:emph]
start = \*\*
end = \*\*
color = 45

[block:img]
start = !\[.*\]\([^\)]+\)
color = 92

[block:codeblock]
start = "^```"
end = "^```"
color = 64
child_blocks =
15 changes: 15 additions & 0 deletions src/common/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ typedef struct _Config {

const char *exe_path;

const char *syntax;

int indent_size;
bool use_spaces_for_indent;
char filename[PATH_MAX];
Expand All @@ -45,6 +47,7 @@ void Config_Init(const char *argv0) {
config.table = NULL;
config.editor = NULL;
config.colors = NULL;
config.syntax = NULL;
config.dirty = false;
}

Expand All @@ -55,6 +58,7 @@ void Config_Deinit() {
config.table = NULL;
config.editor = NULL;
config.colors = NULL;
config.syntax = NULL;
}


Expand Down Expand Up @@ -103,6 +107,17 @@ const char *Config_GetFilename() {
return config.filename;
}


void Config_SetSyntax(const char *type) {
config.syntax = type;
config.dirty = true;
}

const char *Config_GetSyntax() {
return config.syntax;
}


Table *Config_GetModuleConfig(const char *section) {
if (!config.table || TypedTable_GetType(config.table, section) != VALUE_TYPE_TABLE) {
return NULL;
Expand Down
4 changes: 4 additions & 0 deletions src/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const char *Config_GetExePath();
void Config_SetFilename(const char *filename);
const char* Config_GetFilename();

void Config_SetSyntax(const char *type);
const char *Config_GetSyntax();


Table *Config_GetModuleConfig(const char *section);
int Config_GetNumber(Table *table, const char *key, int fallback);
const char *Config_GetStr(Table *table, const char *key, const char *fallback);
Expand Down
2 changes: 1 addition & 1 deletion src/common/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ void String_AppendView(String *str, const StringView *view) {
String_Deinit(&tmp);
return;
}
if (new_byte_size > str->bytes_capacity) {
if (new_byte_size + 1 > str->bytes_capacity) {
resize_bytes_capacity(str, new_byte_size + 1);
}
// use memove to handle overlapping memory areas
Expand Down
7 changes: 6 additions & 1 deletion src/common/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ static void increase_capacity(Table *table) {
return;
}
// allocate memory for the increased table
Table *new_table = Table_Create();
Table *new_table = Table_CreateCustom(
table->hash_func,
table->key_cmp_func,
table->key_copy_func,
table->key_free_func
);
new_table->capacity = table->capacity == 0 ? TABLE_INITIAL_CAPACITY : table->capacity * TABLE_GROWTH_FACTOR;
new_table->slots = malloc(new_table->capacity * sizeof(TableSlot));
if (!new_table->slots) {
Expand Down
57 changes: 55 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
#include "common/logging.h"
#include "common/iniparser.h"

#include "syntax/loader.h"

#include "widgets/primitives/frame.h"
#include "widgets/primitives/menu.h"

//const char *TESTFILE = "/home/cypher/projekte/clieditor/README.md";


TextBuffer tb;
SyntaxHighlighting *highlighting;


static void print_help(const char *program_name) {
fprintf(stderr, "Usage:\n %s <filename>\n", program_name);
Expand All @@ -56,6 +58,29 @@ static void parse_arguments(int argc, char *argv[]) {
// No file provided, could set a default or leave it empty.
Config_SetFilename(NULL);
}

int opt;
while ((opt = getopt(argc, argv, "hs:")) != -1) {
switch (opt) {
case 'h':
print_help(argv[0]);
return;
case 's':
Config_SetSyntax(optarg);
break;
case '?':
fprintf(stderr, "Unknown option: -%c\n", optopt);
exit(1);
}
}

// Verbleibende Argumente:
for (int i = optind; i < argc; i++) {
Config_SetFilename(argv[i]);
return;
}

print_help(argv[0]);
}

static void load_environment() {
Expand Down Expand Up @@ -100,6 +125,7 @@ static void finish() { // called automatically (set with atexit())
Timer_Deinit();
App_Deinit();
Config_Deinit();
SyntaxHighlighting_Destroy(highlighting);
TextBuffer_Deinit(&tb);
Input_Deinit();
Screen_Deinit();
Expand Down Expand Up @@ -132,6 +158,32 @@ int main(int argc, char *argv[]) {

TextBuffer_Init(&tb);

SyntaxHighlightingLoaderError error;
highlighting = SyntaxHighlighting_LoadFromFile(Config_GetSyntax(), &error);
if (!highlighting) {
switch (error.code) {
case SYNTAX_LOADER_FILE_NOT_FOUND:
logFatal("Syntax file not found.");
break;
case SYNTAX_LOADER_FILE_READ_ERROR:
logFatal("Could not read syntax file.");
break;
case SYNTAX_LOADER_PARSE_ERROR:
logFatal("Could not parse syntax file.\n%s", error.parsing_error.message);
break;
case SYNTAX_LOADER_DEFINITION_ERROR:
logFatal("Syntax definition error.\n%s", error.def_error.message);
break;
default:
logFatal("Could not load Syntaxdefinition (Code: %d)", error.code);
break;
}
SyntaxHighlightingLoaderError_Deinit(&error);
highlighting = NULL;
exit(1);
}


const char * fn = Config_GetFilename();
bool failure_on_file_load = false; // the failure message can only be shown after initializing the widget system
if (strcmp(fn, "") != 0) {
Expand Down Expand Up @@ -162,6 +214,7 @@ int main(int argc, char *argv[]) {

EditorView *editor = EditorView_Create(AS_WIDGET(&app), &tb);
Widget_Focus(AS_WIDGET(editor));
editor->editor->sh_binding.sh = highlighting;
(void)editor;
Comment thread
defname marked this conversation as resolved.
BottomBar *bottombar = BottomBar_Create(AS_WIDGET(&app));
(void)bottombar;
Expand Down
13 changes: 9 additions & 4 deletions src/syntax/definition.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,15 @@ static SyntaxDefinitionError build_blocks(SyntaxDefinition *def, const Table *ta
return NO_ERROR;
}

static SyntaxDefinitionError map_block_names_to_blocks(SyntaxBlockDef *current_block, StringView *block_names, size_t count, SyntaxBlockDef **out, Table *blocks) {
static SyntaxDefinitionError map_block_names_to_blocks(SyntaxBlockDef *current_block, StringView *block_names, size_t count, SyntaxBlockDef **out, size_t *out_count, Table *blocks) {
*out_count = 0;
for (size_t i=0; i<count; i++) {
String name = String_FromView(block_names[i]);
String_Trim(&name);
if (String_Length(&name) == 0) {
String_Deinit(&name);
continue;
}
table_block_mapping *mapping = Table_Get(blocks, name.bytes);

if (!mapping) {
Expand All @@ -235,7 +240,7 @@ static SyntaxDefinitionError map_block_names_to_blocks(SyntaxBlockDef *current_b
return error;
}
String_Deinit(&name);
out[i] = mapping->block;
out[(*out_count)++] = mapping->block;
}
return NO_ERROR;
}
Expand Down Expand Up @@ -266,14 +271,14 @@ static SyntaxDefinitionError block_name_list_str_to_blocks(SyntaxBlockDef *curre
logFatal("Cannot allocate memory for children of SyntaxBlockDef.");
}

SyntaxDefinitionError error = map_block_names_to_blocks(current_block, children, count, *out, blocks);
SyntaxDefinitionError error = map_block_names_to_blocks(current_block, children, count, *out, out_count, blocks);
if (error.code != SYNTAXDEFINITION_NO_ERROR) {
*out_count = 0;
String_Deinit(&s);
free(children);
return error;
}

*out_count = count;
String_Deinit(&s);
free(children);

Expand Down
2 changes: 1 addition & 1 deletion src/syntax/definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void SyntaxDefinitionError_Deinit(SyntaxDefinitionError *error);
* @brief Helper struct to cache regex match results. This information are used by the Highlight module.
*/
typedef struct _MatchCache {
ssize_t offset; // total offset from where match was calculated
ssize_t offset; // total **byte offset** from where match was calculated
regmatch_t match; // last match
bool done; // if true the last match was already found
} MatchCache;
Expand Down
Loading