aboutsummaryrefslogtreecommitdiff
path: root/vis.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2015-12-20 11:04:20 +0100
committerMarc André Tanner <mat@brain-dump.org>2015-12-26 21:29:15 +0100
commit13a95942b6e33033e4bbbe27cc8cafc98c6924a2 (patch)
tree04acfeef0b83b4d6e08212948313242b57779f73 /vis.c
parent39e2b6f1b7a750af55ceeadab977354919d0ccf5 (diff)
downloadvis-13a95942b6e33033e4bbbe27cc8cafc98c6924a2.tar.gz
vis-13a95942b6e33033e4bbbe27cc8cafc98c6924a2.tar.xz
vis: refactor Lua integration
Lua support can now be disabled at compile time using: $ make CONFIG_LUA=0 This commit also adds an initial Lua API and provides a few default hooks. We now also require Lua >= 5.2 due to the uservalue constructs. In principle the same functionality could be implemented using function environments from Lua 5.1.
Diffstat (limited to 'vis.c')
-rw-r--r--vis.c118
1 files changed, 15 insertions, 103 deletions
diff --git a/vis.c b/vis.c
index 3825500..f23c42e 100644
--- a/vis.c
+++ b/vis.c
@@ -34,9 +34,6 @@
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <pwd.h>
-#include <lauxlib.h>
-#include <lualib.h>
-#include <lua.h>
#include <termkey.h>
#include "vis.h"
@@ -67,7 +64,8 @@ static void file_free(Vis *vis, File *file) {
return;
if (--file->refcount > 0)
return;
-
+ if (vis->event && vis->event->file_close)
+ vis->event->file_close(vis, file);
text_free(file->text);
free((char*)file->name);
@@ -120,30 +118,17 @@ static File *file_new(Vis *vis, const char *filename) {
if (filename)
file->name = strdup(filename);
+ if (vis->event && vis->event->file_open)
+ vis->event->file_open(vis, file);
return file;
}
void vis_window_name(Win *win, const char *filename) {
- lua_State *L = win->vis->lua;
File *file = win->file;
if (filename != file->name) {
free((char*)file->name);
file->name = filename ? strdup(filename) : NULL;
}
-
- if (filename && L) {
- lua_getglobal(L, "vis");
- lua_getfield(L, -1, "lexers");
- lua_getfield(L, -1, "lexer_name");
- lua_pushstring(L, filename);
- lua_pcall(L, 1, 1, 0);
- if (lua_isstring(L, -1)) {
- const char *lexer_name = lua_tostring(L, -1);
- if (lexer_name)
- view_syntax_set(win->view, lexer_name);
- }
- lua_pop(L, 2); /* return value: lexer name, lexers variable */
- }
}
static void windows_invalidate(Vis *vis, size_t start, size_t end) {
@@ -189,6 +174,8 @@ static Win *window_new_file(Vis *vis, File *file) {
vis->windows = win;
vis->win = win;
vis->ui->window_focus(win->ui);
+ if (vis->event && vis->event->win_open)
+ vis->event->win_open(vis, win);
return win;
}
@@ -278,6 +265,8 @@ bool vis_window_new(Vis *vis, const char *filename) {
void vis_window_close(Win *win) {
Vis *vis = win->vis;
+ if (vis->event && vis->event->win_close)
+ vis->event->win_close(vis, win);
file_free(vis, win->file);
if (win->prev)
win->prev->next = win->next;
@@ -295,70 +284,14 @@ void vis_window_close(Win *win) {
vis_draw(vis);
}
-Vis *vis_new(Ui *ui) {
+Vis *vis_new(Ui *ui, VisEvent *event) {
if (!ui)
return NULL;
Vis *vis = calloc(1, sizeof(Vis));
if (!vis)
return NULL;
- lua_State *L = luaL_newstate();
- if (!(vis->lua = L))
- goto err;
- luaL_openlibs(L);
-
- /* try to get users home directory */
- const char *home = getenv("HOME");
- if (!home || !*home) {
- struct passwd *pw = getpwuid(getuid());
- if (pw)
- home = pw->pw_dir;
- }
-
- /* extends lua's package.path with:
- * - $VIS_PATH/lexers
- * - $HOME/.vis/lexers
- * - /usr/local/share/vis/lexers
- * - /usr/share/vis/lexers
- * - package.path (standard lua search path)
- */
- int paths = 3;
- lua_getglobal(L, "package");
-
- const char *vis_path = getenv("VIS_PATH");
- if (vis_path) {
- lua_pushstring(L, vis_path);
- lua_pushstring(L, "/lexers/?.lua;");
- lua_concat(L, 2);
- paths++;
- }
-
- if (home && *home) {
- lua_pushstring(L, home);
- lua_pushstring(L, "/.vis/lexers/?.lua;");
- lua_concat(L, 2);
- paths++;
- }
-
- lua_pushstring(L, "/usr/local/share/vis/lexers/?.lua;");
- lua_pushstring(L, "/usr/share/vis/lexers/?.lua;");
- lua_getfield(L, -paths, "path");
- lua_concat(L, paths);
- lua_setfield(L, -2, "path");
- lua_pop(L, 1); /* package */
-
- /* try to load the lexer module */
- lua_getglobal(L, "require");
- lua_pushstring(L, "lexer");
- if (lua_pcall(L, 1, 1, 0)) {
- lua_close(L);
- vis->lua = L = NULL;
- } else {
- lua_newtable(L); /* vis */
- lua_pushvalue(L, -2); /* require return value */
- lua_setfield(L, -2, "lexers");
- lua_setglobal(L, "vis");
- }
-
+ if (event && event->vis_start)
+ event->vis_start(vis);
vis->ui = ui;
vis->ui->init(vis->ui, vis);
vis->tabwidth = 8;
@@ -381,6 +314,7 @@ Vis *vis_new(Ui *ui) {
if (!(vis->search_pattern = text_regex_new()))
goto err;
vis->mode_prev = vis->mode = &vis_modes[VIS_MODE_NORMAL];
+ vis->event = event;
return vis;
err:
vis_free(vis);
@@ -390,8 +324,9 @@ err:
void vis_free(Vis *vis) {
if (!vis)
return;
- if (vis->lua)
- lua_close(vis->lua);
+ if (vis->event && vis->event->vis_quit)
+ vis->event->vis_quit(vis);
+ vis->event = NULL;
while (vis->windows)
vis_window_close(vis->windows);
file_free(vis, vis->prompt->file);
@@ -1352,26 +1287,3 @@ const char *vis_file_name(File *file) {
return file->name;
}
-bool vis_theme_load(Vis *vis, const char *name) {
- lua_State *L = vis->lua;
- if (!L)
- return false;
- /* package.loaded['themes/'..name] = nil
- * require 'themes/'..name */
- lua_pushstring(L, "themes/");
- lua_pushstring(L, name);
- lua_concat(L, 2);
- lua_getglobal(L, "package");
- lua_getfield(L, -1, "loaded");
- lua_pushvalue(L, -3);
- lua_pushnil(L);
- lua_settable(L, -3);
- lua_pop(L, 2);
- lua_getglobal(L, "require");
- lua_pushvalue(L, -2);
- if (lua_pcall(L, 1, 0, 0))
- return false;
- for (Win *win = vis->windows; win; win = win->next)
- view_syntax_set(win->view, view_syntax_get(win->view));
- return true;
-}