1
0
Fork 0
trinitrix/config/c/plugin.c

121 lines
3.5 KiB
C
Raw Normal View History

/*
* Copyright (C) 2023 - 2024:
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
* 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 <https://www.gnu.org/licenses/>.
*/
#include "../../dist/interface.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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", "<ESC>", 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", "<TAB>", 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", "<C-c>", print_warning);
// workaround to avoid c de-allocating our nice strings
while (1) {
};
return EXIT_SUCCESS;
}