From 88f323d0300fa923419a168b4622ba956bc5a778 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 4 May 2024 20:04:00 +0200 Subject: [PATCH] feat(config/c): Translate the example lua config to c --- config/c/plugin.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 config/c/plugin.c diff --git a/config/c/plugin.c b/config/c/plugin.c new file mode 100644 index 0000000..1280b7c --- /dev/null +++ b/config/c/plugin.c @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2023 - 2024: + * The Trinitrix Project + * SPDX-License-Identifier: LGPL-3.0-or-later + * + * This file is part of the Trixy crate for Trinitrix. + * + * Trixy is free software: you can redistribute it and/or modify + * it under the terms of the Lesser GNU General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * and the Lesser GNU General Public License along with this program. + * If not, see . + */ + +#include "../../dist/interface.h" +#include +#include +#include +#include + +#define println(args...) \ + fprintf(log_file, "\33[32;1m(plugin):\33[0m \33[34;1m"); \ + fprintf(log_file, args); \ + fprintf(log_file, "\n\33[0m"); \ + fflush(log_file); + +#define eprintln(args...) \ + fprintf(log_file, "\33[32;1m(plugin):\33[0m\33[31;1m "); \ + fprintf(log_file, args); \ + fprintf(log_file, "\n\33[0m"); \ + fflush(log_file); + +int is_first_log_file_open = 1; +FILE *get_log_file() { + FILE *log_file; + + if (is_first_log_file_open) { + is_first_log_file_open = 0; + log_file = fopen("plugin.txt", "w"); + } else { + log_file = fopen("plugin.txt", "wa"); + } + + if (log_file == NULL) { + printf("Error opening file!\n"); + exit(1); + } + + return log_file; +} + +void handle_error() { + FILE *log_file = get_log_file(); + eprintln("GOT an error"); + + int error_length = last_error_length(); + char *error = malloc(error_length); + last_error_message(error, error_length); + eprintln("Encountered error: %s", error); + free(error); +} + +void set_normal_mode() { + if (!trinitrix.api.ui.set_mode(Normal)) + handle_error(); +} +void set_command_mode() { + if (!trinitrix.api.ui.set_mode(Command)) + handle_error(); +} +void set_insert_mode() { + if (!trinitrix.api.ui.set_mode(Insert)) + handle_error(); +} +void print_hi() { + if (!trinitrix.api.raw.raise_error("hi!")) + handle_error(); +} +void print_warning() { + if (!trinitrix.api.raw.raise_error( + "To exit trinitrix use 'trinitrix.api.exit()' instead!")) + handle_error(); +} + +int plugin_main() { + FILE *log_file = get_log_file(); + + println("Hi, setting first keymap!"); + if (!trinitrix.api.keymaps.add("ci", "", set_normal_mode)) + handle_error(); + println("Done setting that keymap"); + + if (!trinitrix.api.keymaps.add("n", ":", set_command_mode)) + handle_error(); + + trinitrix.api.keymaps.add("n", "i", set_insert_mode); + trinitrix.api.keymaps.add("n", "", trinitrix.api.ui.cycle_planes); + + // a simple test to prove that key chords work + trinitrix.api.keymaps.add("ni", "jj", print_hi); + + trinitrix.api.keymaps.add("n", "q", trinitrix.api.exit); + + // Help people + trinitrix.api.keymaps.add("n", "", print_warning); + + // workaround to avoid c de-allocating our nice strings + while (1) { + }; + + return EXIT_SUCCESS; +}