From 8a9e6cd945e50ea861257a87e543c76632ee1bca Mon Sep 17 00:00:00 2001 From: evanlin96069 <72735402+evanlin96069@users.noreply.github.com> Date: Fri, 23 Feb 2024 22:42:45 -0500 Subject: [PATCH] Bundle syntax files --- .gitignore | 3 + Makefile | 30 ++++--- README.md | 16 +--- docs/configs.md | 6 +- docs/syntax.md | 7 +- resources/README.md | 18 +++++ resources/bundler.c | 61 +++++++++++++++ {syntax => resources/syntax}/c.json | 0 {syntax => resources/syntax}/cpp.json | 0 {syntax => resources/syntax}/java.json | 0 {syntax => resources/syntax}/json.json | 0 {syntax => resources/syntax}/kuroko.json | 0 {syntax => resources/syntax}/make.json | 0 {syntax => resources/syntax}/python.json | 0 {syntax => resources/syntax}/rust.json | 0 {syntax => resources/syntax}/srctas.json | 0 {syntax => resources/syntax}/zig.json | 0 {themes => resources/themes}/dark.nino | 0 {themes => resources/themes}/light.nino | 0 {themes => resources/themes}/monokai.nino | 0 {themes => resources/themes}/ms-dos.nino | 0 {themes => resources/themes}/solarized.nino | 0 src/highlight.c | 87 +++++++++++++-------- 23 files changed, 162 insertions(+), 66 deletions(-) create mode 100644 resources/README.md create mode 100644 resources/bundler.c rename {syntax => resources/syntax}/c.json (100%) rename {syntax => resources/syntax}/cpp.json (100%) rename {syntax => resources/syntax}/java.json (100%) rename {syntax => resources/syntax}/json.json (100%) rename {syntax => resources/syntax}/kuroko.json (100%) rename {syntax => resources/syntax}/make.json (100%) rename {syntax => resources/syntax}/python.json (100%) rename {syntax => resources/syntax}/rust.json (100%) rename {syntax => resources/syntax}/srctas.json (100%) rename {syntax => resources/syntax}/zig.json (100%) rename {themes => resources/themes}/dark.nino (100%) rename {themes => resources/themes}/light.nino (100%) rename {themes => resources/themes}/monokai.nino (100%) rename {themes => resources/themes}/ms-dos.nino (100%) rename {themes => resources/themes}/solarized.nino (100%) diff --git a/.gitignore b/.gitignore index b9cdf26..6e0a22c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ release/ debug/ *.txt + +resources/bundler +resources/bundle.h diff --git a/Makefile b/Makefile index 21ebd9c..9a80616 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all prep release debug clean format install install-data +.PHONY: all prep release debug clean format install # Platform detection ifeq ($(OS),Windows_NT) @@ -31,14 +31,19 @@ OBJS = $(patsubst $(SRCDIR)/%.c, %.o, $(SOURCES)) DEPS = $(patsubst $(SRCDIR)/%.c, %.d, $(SOURCES)) EXE = nino$(EXE_EXT) +# Resources +RESOURCE_DIR = resources +SYNTAX_FILES= $(wildcard $(RESOURCE_DIR)/syntax/*.json) +BUNDLER_SRC = $(RESOURCE_DIR)/bundler.c +BUNDLER = $(RESOURCE_DIR)/bundler +BUNDLE = $(RESOURCE_DIR)/bundle.h + # Install settings prefix ?= /usr/local exec_prefix ?= $(prefix) bindir ?= $(exec_prefix)/bin -datadir = $(HOME)/.config/nino INSTALL = install -INSTALL_DATA = $(INSTALL) -m 644 # Release build settings RELDIR = release @@ -57,18 +62,24 @@ DBGCFLAGS = -Og -g3 -D_DEBUG # Default target all: prep release +$(BUNDLER) : $(BUNDLER_SRC) + $(CC) -o $@ $< + +$(BUNDLE) : $(BUNDLER) + $(BUNDLER) $(BUNDLE) $(SYNTAX_FILES) + # Release build release: $(RELEXE) $(RELEXE): $(RELOBJS) $(CC) $(CFLAGS) $(RELCFLAGS) -s -o $(RELEXE) $^ -$(RELDIR)/%.o: $(SRCDIR)/%.c +$(RELDIR)/%.o: $(SRCDIR)/%.c $(BUNDLE) $(CC) -c -MMD $(CFLAGS) $(RELCFLAGS) -o $@ $< # Debug build debug: $(DBGEXE) $(DBGEXE): $(DBGOBJS) $(CC) $(CFLAGS) $(DBGCFLAGS) -o $(DBGEXE) $^ -$(DBGDIR)/%.o: $(SRCDIR)/%.c +$(DBGDIR)/%.o: $(SRCDIR)/%.c $(BUNDLE) $(CC) -c -MMD $(CFLAGS) $(DBGCFLAGS) -o $@ $< -include $(RELDEPS) $(DBGDEPS) @@ -80,7 +91,7 @@ prep: # Clean target clean: - $(call rm, $(RELEXE) $(RELDEPS) $(RELOBJS) $(DBGEXE) $(DBGDEPS) $(DBGOBJS)) + $(call rm, $(RELEXE) $(RELDEPS) $(RELOBJS) $(DBGEXE) $(DBGDEPS) $(DBGOBJS) $(BUNDLER) $(BUNDLE)) # Format all files format: @@ -89,10 +100,3 @@ format: # Install target install: $(INSTALL) $(RELEXE) $(DESTDIR)$(bindir)/$(EXE) - -install-data: - $(call mkdir, $(datadir)) - $(call mkdir, $(datadir)/syntax) - $(INSTALL_DATA) syntax/*.json $(DESTDIR)$(datadir)/syntax/ - $(call mkdir, $(datadir)/themes) - $(INSTALL_DATA) themes/*.nino $(DESTDIR)$(datadir)/themes/ diff --git a/README.md b/README.md index 89a5d10..05eb767 100644 --- a/README.md +++ b/README.md @@ -23,24 +23,12 @@ I'm not used to [Vim](https://www.vim.org/) and I don't like [nano](https://nano - Search with smart case sensitivity - File explorer -## Installation -### Linux -nino requires a sufficiently Unix-like C library and a C11 compiler. - -#### Build nino: +## Build ``` git clone https://github.com/evanlin96069/nino.git cd nino -make && sudo make install -``` - -#### Install Themes and Syntax Highlighting +make ``` -make install-data -``` - -### Windows -Use Mingw-w64. Run `make` and get `nino.exe` in the `release` folder ## Documentation - [Configurations](docs/configs.md) diff --git a/docs/configs.md b/docs/configs.md index 7c76f46..9c5688a 100644 --- a/docs/configs.md +++ b/docs/configs.md @@ -4,10 +4,8 @@ Configurations are set by running commands. Use `Ctrl+P` to open the command pro To run commands on start, create `ninorc` in the configuration directory. ## Configuration Directory: -- Linux - - `~/.config/nino` -- Windows - - `~/.nino` +- Linux: `~/.config/nino` +- Windows: `~/.nino` ## Execute a Config File `exec` command executes a config file. diff --git a/docs/syntax.md b/docs/syntax.md index c95ed7b..7641cb8 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -3,8 +3,11 @@ nino supports simple keywords syntax highlighting. Enable syntax highlighting with command `syntax 1`. -## Install Syntax Highlighting Data -Copy the `syntax` folder to the [configuration directory](configs.md). +## Add Syntax Highlighting Data +If you would like to make your own syntax files, +you can put them in: +- Linux: `~/.config/nino/syntax` +- Windows: `~/.nino/syntax` ## Syntax Highlighting Data Syntax highlighting data are stored in JSON files. diff --git a/resources/README.md b/resources/README.md new file mode 100644 index 0000000..524f1d7 --- /dev/null +++ b/resources/README.md @@ -0,0 +1,18 @@ +# Resource files for nino + +## syntax +Files in this folder will be bundled in the binary for portability. +If you would like to make your own syntax files, +you can put them in: +- Linux: `~/.config/nino/syntax` +- Windows: `~/.nino/syntax` + +## themes +These are some example themes. +You can copy them to the configuration directory like normal configuration files. + +Configuration Directory: +- Linux: `~/.config/nino` +- Windows: `~/.nino` + +To apply the theme, run it with the `exec` command. diff --git a/resources/bundler.c b/resources/bundler.c new file mode 100644 index 0000000..96bde5f --- /dev/null +++ b/resources/bundler.c @@ -0,0 +1,61 @@ +#include + +#define ARGS_SHIFT() \ + { \ + argc--; \ + argv++; \ + } \ + while (0) + +int main(int argc, char* argv[]) { + if (argc < 3) { + fprintf(stderr, "Usage: %s files...\n", argv[0]); + return 1; + } + + ARGS_SHIFT(); + + FILE* out = fopen(argv[0], "w"); + if (!out) { + fprintf(stderr, "Failed to open %s to write.\n", argv[1]); + return 1; + } + + ARGS_SHIFT(); + + fprintf(out, "#ifndef BUNDLE_H\n"); + fprintf(out, "#define BUNDLE_H\n\n"); + + for (int i = 0; i < argc; i++) { + FILE* fp = fopen(argv[i], "r"); + if (!fp) { + fprintf(stderr, "Failed to open %s to read.\n", argv[i]); + return 1; + } + + fprintf(out, "const char bundle%d[] = {", i); + + int index = 0; + + int byte; + while ((byte = fgetc(fp)) != EOF) { + if (index % 10 == 0) { + fprintf(out, "\n "); + } + fprintf(out, "0x%02X, ", byte); + index++; + } + + fprintf(out, "\n};\n\n"); + } + + fprintf(out, "const char* bundle[] = {\n"); + for (int i = 0; i < argc; i++) { + fprintf(out, " bundle%d,\n", i); + } + fprintf(out, "};\n\n"); + + fprintf(out, "#endif\n"); + + return 0; +} diff --git a/syntax/c.json b/resources/syntax/c.json similarity index 100% rename from syntax/c.json rename to resources/syntax/c.json diff --git a/syntax/cpp.json b/resources/syntax/cpp.json similarity index 100% rename from syntax/cpp.json rename to resources/syntax/cpp.json diff --git a/syntax/java.json b/resources/syntax/java.json similarity index 100% rename from syntax/java.json rename to resources/syntax/java.json diff --git a/syntax/json.json b/resources/syntax/json.json similarity index 100% rename from syntax/json.json rename to resources/syntax/json.json diff --git a/syntax/kuroko.json b/resources/syntax/kuroko.json similarity index 100% rename from syntax/kuroko.json rename to resources/syntax/kuroko.json diff --git a/syntax/make.json b/resources/syntax/make.json similarity index 100% rename from syntax/make.json rename to resources/syntax/make.json diff --git a/syntax/python.json b/resources/syntax/python.json similarity index 100% rename from syntax/python.json rename to resources/syntax/python.json diff --git a/syntax/rust.json b/resources/syntax/rust.json similarity index 100% rename from syntax/rust.json rename to resources/syntax/rust.json diff --git a/syntax/srctas.json b/resources/syntax/srctas.json similarity index 100% rename from syntax/srctas.json rename to resources/syntax/srctas.json diff --git a/syntax/zig.json b/resources/syntax/zig.json similarity index 100% rename from syntax/zig.json rename to resources/syntax/zig.json diff --git a/themes/dark.nino b/resources/themes/dark.nino similarity index 100% rename from themes/dark.nino rename to resources/themes/dark.nino diff --git a/themes/light.nino b/resources/themes/light.nino similarity index 100% rename from themes/light.nino rename to resources/themes/light.nino diff --git a/themes/monokai.nino b/resources/themes/monokai.nino similarity index 100% rename from themes/monokai.nino rename to resources/themes/monokai.nino diff --git a/themes/ms-dos.nino b/resources/themes/ms-dos.nino similarity index 100% rename from themes/ms-dos.nino rename to resources/themes/ms-dos.nino diff --git a/themes/solarized.nino b/resources/themes/solarized.nino similarity index 100% rename from themes/solarized.nino rename to resources/themes/solarized.nino diff --git a/src/highlight.c b/src/highlight.c index eb8e9ce..ac898ac 100644 --- a/src/highlight.c +++ b/src/highlight.c @@ -5,6 +5,7 @@ #include #include +#include "../resources/bundle.h" #include "config.h" #include "os.h" @@ -204,6 +205,7 @@ void editorSelectSyntaxHighlight(EditorFile* file) { static JsonArena hldb_arena; static void editorLoadNinoConfigHLDB(void); +static void editorLoadBundledHLDB(void); void editorInitHLDB(void) { char path[EDITOR_PATH_MAX]; @@ -236,6 +238,7 @@ void editorInitHLDB(void) { dirClose(&iter); editorLoadNinoConfigHLDB(); + editorLoadBundledHLDB(); } // Built-in syntax highlighting for nino config @@ -266,32 +269,9 @@ static void editorLoadNinoConfigHLDB(void) { gEditor.HLDB = syntax; } -bool editorLoadHLDB(const char* json_file) { - FILE* fp; - size_t size; - char* buffer; - - // Load file - fp = openFile(json_file, "rb"); - if (!fp) - return false; - - fseek(fp, 0, SEEK_END); - size = ftell(fp); - fseek(fp, 0, SEEK_SET); - - buffer = calloc_s(1, size + 1); - - if (fread(buffer, size, 1, fp) != 1) { - fclose(fp); - free(buffer); - return false; - } - fclose(fp); - +static bool editorLoadJsonHLDB(const char* json, EditorSyntax* syntax) { // Parse json - JsonValue* value = json_parse(buffer, &hldb_arena); - free(buffer); + JsonValue* value = json_parse(json, &hldb_arena); if (value->type != JSON_OBJECT) { return false; } @@ -300,10 +280,9 @@ bool editorLoadHLDB(const char* json_file) { #define CHECK(boolean) \ do { \ if (!(boolean)) \ - goto END; \ + return false; \ } while (0) - EditorSyntax* syntax = calloc_s(1, sizeof(EditorSyntax)); JsonObject* object = value->object; JsonValue* name = json_object_find(object, "name"); @@ -359,14 +338,56 @@ bool editorLoadHLDB(const char* json_file) { // TODO: Add flags option in json file syntax->flags = HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS; - // Add to HLDB - syntax->next = gEditor.HLDB; - gEditor.HLDB = syntax; + return true; +} + +static void editorLoadBundledHLDB(void) { + for (size_t i = 0; i < sizeof(bundle) / sizeof(bundle[0]); i++) { + EditorSyntax* syntax = calloc_s(1, sizeof(EditorSyntax)); + if (editorLoadJsonHLDB(bundle[i], syntax)) { + // Add to HLDB + syntax->next = gEditor.HLDB; + gEditor.HLDB = syntax; + } else { + free(syntax); + } + } +} + +bool editorLoadHLDB(const char* path) { + FILE* fp; + size_t size; + char* buffer; + + // Load file + fp = openFile(path, "rb"); + if (!fp) + return false; + + fseek(fp, 0, SEEK_END); + size = ftell(fp); + fseek(fp, 0, SEEK_SET); + buffer = calloc_s(1, size + 1); + + if (fread(buffer, size, 1, fp) != 1) { + fclose(fp); + free(buffer); + return false; + } + fclose(fp); + + EditorSyntax* syntax = calloc_s(1, sizeof(EditorSyntax)); + if (editorLoadJsonHLDB(buffer, syntax)) { + // Add to HLDB + syntax->next = gEditor.HLDB; + gEditor.HLDB = syntax; + } else { + free(syntax); + } + + free(buffer); return true; -END: - free(syntax); - return false; } void editorFreeHLDB(void) {