forked from DataKinds/mefa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.c
More file actions
57 lines (44 loc) · 1.51 KB
/
Copy pathmodule.c
File metadata and controls
57 lines (44 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "emacs-module.h"
#include "lua-emacs.h"
#include "emacs-interaction.h"
#include <stdlib.h>
#include <stdarg.h>
#include <luajit-2.1/lua.h>
#include <luajit-2.1/lauxlib.h>
#include <luajit-2.1/lualib.h>
#define FUCK_GPL int plugin_is_GPL_compatible
FUCK_GPL;
lua_State *L;
static emacs_value Flua(emacs_env *env, ptrdiff_t nargs, emacs_value args[], void *data) {
char *luabuffer;
intmax_t luabuffer_size;
env->copy_string_contents(env, args[0], NULL, &luabuffer_size);
luabuffer = malloc(luabuffer_size);
env->copy_string_contents(env, args[0], luabuffer, &luabuffer_size);
luaL_loadstring(L, luabuffer);
lua_pcall(L, 0, 1, 0);
size_t luaout_size;
const char *luaout = lua_tolstring(L, -1, &luaout_size);
emacs_value out = env->make_string(env, luaout, luaout_size);
free(luabuffer);
return out;
}
int emacs_module_init(struct emacs_runtime *ert) {
emacs_env *env = ert->get_environment(ert);
L = lua_emacs_init(env);
// bind the function `lua` into elisp
emacs_value emacsfunc = env->make_function(
env,
1,1,
Flua,
"Run a lua function and return the result as a string.",
NULL
);
emacs_value func_sym = env->intern(env, "lua");
// provide the lua function
slow_arbitrary_funcall(env, "defalias", 2, func_sym, emacsfunc);
// provide the feature `module`
emacs_value module_sym = env->intern(env, "luajit");
slow_arbitrary_funcall(env, "provide", 1, module_sym);
return 0;
}