Skip to content

Commit

Permalink
Bundle syntax files
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlin96069 committed Feb 24, 2024
1 parent d521f28 commit ef96d5d
Show file tree
Hide file tree
Showing 23 changed files with 168 additions and 71 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
release/
debug/
*.txt

resources/bundler
resources/bundler.exe
resources/bundle.h
30 changes: 17 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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$(EXE_EXT)
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
Expand All @@ -57,18 +62,24 @@ DBGCFLAGS = -Og -g3 -D_DEBUG
# Default target
all: prep release

$(BUNDLER) : $(BUNDLER_SRC)
$(CC) -s -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)
Expand All @@ -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:
Expand All @@ -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/
16 changes: 2 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions docs/configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions resources/README.md
Original file line number Diff line number Diff line change
@@ -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.
61 changes: 61 additions & 0 deletions resources/bundler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>

#define ARGS_SHIFT() \
{ \
argc--; \
argv++; \
} \
while (0)

int main(int argc, char* argv[]) {
if (argc < 3) {
fprintf(stderr, "Usage: %s <output file> 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;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
97 changes: 59 additions & 38 deletions src/highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>

#include "../resources/bundle.h"
#include "config.h"
#include "os.h"

Expand Down Expand Up @@ -204,8 +205,12 @@ void editorSelectSyntaxHighlight(EditorFile* file) {
static JsonArena hldb_arena;

static void editorLoadNinoConfigHLDB(void);
static void editorLoadBundledHLDB(void);

void editorInitHLDB(void) {
editorLoadNinoConfigHLDB();
editorLoadBundledHLDB();

char path[EDITOR_PATH_MAX];
snprintf(path, sizeof(path), PATH_CAT("%s", CONF_DIR, "syntax"),
getenv(ENV_HOME));
Expand Down Expand Up @@ -234,8 +239,6 @@ void editorInitHLDB(void) {
}
} while (dirNext(&iter));
dirClose(&iter);

editorLoadNinoConfigHLDB();
}

// Built-in syntax highlighting for nino config
Expand Down Expand Up @@ -266,44 +269,20 @@ 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;
}

// Get data
#define CHECK(boolean) \
do { \
if (!(boolean)) \
goto END; \
#define CHECK(boolean) \
do { \
if (!(boolean)) \
return false; \
} while (0)

EditorSyntax* syntax = calloc_s(1, sizeof(EditorSyntax));
JsonObject* object = value->object;

JsonValue* name = json_object_find(object, "name");
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ef96d5d

Please sign in to comment.