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

148 lines
3.9 KiB
C
Raw Normal View History

/*
* Copyright (C) 2024 - 2024:
* The Trinitrix Project <bpeetz@b-peetz.de, antifallobst@systemausfall.org>
* SPDX-License-Identifier: MIT
*
* This file is part of Trinitrix.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the Software), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#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;
}