Skip to content

Commit

Permalink
replace wren with lua (working)
Browse files Browse the repository at this point in the history
  • Loading branch information
adsr committed Mar 8, 2018
1 parent 001ef68 commit 76f1c7a
Show file tree
Hide file tree
Showing 12 changed files with 2,404 additions and 1,825 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
DESTDIR?=/usr/local/bin/

mle_cflags:=$(CFLAGS) -D_GNU_SOURCE -Wall -Wextra -Wno-missing-braces -Wno-unused-parameter -Wno-unused-result -g -O2 -I./mlbuf/ -I./termbox/src/ -I./uthash/src/ -I./lua
mle_cflags:=$(CFLAGS) -D_GNU_SOURCE -Wall -Wextra -Wno-missing-braces -Wno-unused-parameter -Wno-unused-result -Wno-unused-function -g -O0 -I./mlbuf/ -I./termbox/src/ -I./uthash/src/ -I./lua
mle_ldlibs:=$(LDLIBS) -lm -lpcre
mle_ldflags:=$(LDFLAGS)
mle_objects:=$(patsubst %.c,%.o,$(wildcard *.c))
mle_static:=
termbox_cflags:=$(CFLAGS) -D_XOPEN_SOURCE -Wall -Wextra -Wno-unused-result -std=gnu99 -g -O2
termbox_cflags:=$(CFLAGS) -D_XOPEN_SOURCE -Wall -Wextra -Wno-unused-result -std=gnu99 -g -O0
termbox_objects:=$(patsubst termbox/src/%.c,termbox/src/%.o,$(wildcard termbox/src/*.c))
lua_objects:=$(patsubst lua/%.c,lua/%.o,$(wildcard lua/*.c))
lua_cflags:=$(CFLAGS) -DLUA_USER_H='"ltests.h"' -DLUA_USE_POSIX -DLUA_USE_DLOPEN -DLUA_COMPAT_5_2 -Wall -Wextra -std=gnu99 -g -O2
lua_cflags:=$(CFLAGS) -DLUA_USE_POSIX -Wall -Wextra -std=gnu99 -g -O0

all: mle

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mle is a small, flexible, terminal-based text editor written in C.
* Full UTF-8 support
* Syntax highlighting
* Stackable key maps (modes)
* Extensible via [Lua](https://www.lua.org)
* Scriptable rc file
* Key macros
* Multiple splittable windows
Expand Down Expand Up @@ -93,6 +94,15 @@ executable `~/.mlerc` PHP script:
This overrides the normal grep command with `git grep` if `.git` exists in the
current working directory.

### Advanced usage: Scripting

mle is extensible via the [Lua](https://www.lua.org) programming language.
Scripts are loaded via the `-x` cli option. Commands registered by scripts can
be mapped to keys as normal via `-k`. See `uscript.lua` for a simple example.

There is also a `wren` branch with [Wren](http://wren.io) scripting support.
That work is on pause.

### Advanced usage: Headless mode

mle provides support for non-interactive editing which may be useful for using
Expand Down
22 changes: 11 additions & 11 deletions editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ int editor_menu(editor_t* editor, cmd_func_t callback, char* opt_buf_data, int o
}

// Open a bview
int editor_open_bview(editor_t* editor, bview_t* parent, int type, char* opt_path, int opt_path_len, int make_active, bint_t linenum, int skip_resize, buffer_t* opt_buffer, bview_t** optret_bview) {
int editor_open_bview(editor_t* editor, bview_t* opt_parent, int type, char* opt_path, int opt_path_len, int make_active, bint_t linenum, int skip_resize, buffer_t* opt_buffer, bview_t** optret_bview) {
bview_t* bview;
bview_rect_t* rect;
int found;
Expand All @@ -310,10 +310,10 @@ int editor_open_bview(editor_t* editor, bview_t* parent, int type, char* opt_pat
bview = bview_new(editor, opt_path, opt_path_len, opt_buffer);
bview->type = type;
CDL_APPEND2(editor->all_bviews, bview, all_prev, all_next);
if (!parent) {
if (!opt_parent) {
DL_APPEND2(editor->top_bviews, bview, top_prev, top_next);
} else {
parent->split_child = bview;
opt_parent->split_child = bview;
}
}
if (make_active) {
Expand Down Expand Up @@ -2027,7 +2027,7 @@ static int _editor_init_from_args(editor_t* editor, int argc, char** argv) {
int rv;
kmap_t* cur_kmap;
syntax_t* cur_syntax;
// uscript_t* uscript;
uscript_t* uscript;
int c;
rv = MLE_OK;

Expand Down Expand Up @@ -2159,13 +2159,13 @@ static int _editor_init_from_args(editor_t* editor, int argc, char** argv) {
break;
case 'x':
// TODO
//if (!(uscript = uscript_run(editor, optarg))) {
// MLE_LOG_ERR("Failed to run uscript: %s\n", optarg);
// editor->exit_code = EXIT_FAILURE;
// rv = MLE_ERR;
//} else {
// DL_APPEND(editor->uscripts, uscript);
//}
if (!(uscript = uscript_run(editor, optarg))) {
MLE_LOG_ERR("Failed to run uscript: %s\n", optarg);
editor->exit_code = EXIT_FAILURE;
rv = MLE_ERR;
} else {
DL_APPEND(editor->uscripts, uscript);
}
break;
case 'y':
editor->syntax_override = optarg;
Expand Down
15 changes: 7 additions & 8 deletions mle.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "uthash.h"
#include "mlbuf.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

// Typedefs
typedef struct editor_s editor_t; // A container for editor-wide globals
Expand Down Expand Up @@ -377,9 +379,7 @@ struct prompt_hnode_s {
// uscript_t
struct uscript_s {
editor_t* editor;
//WrenVM* vm;
//WrenHandle* class_mle;
//WrenHandle* func_list2map;
lua_State* L;
uhandle_t* uhandles;
uscript_t* prev;
uscript_t* next;
Expand All @@ -388,8 +388,7 @@ struct uscript_s {
// uhandle_t
struct uhandle_s {
uscript_t* uscript;
//WrenHandle* receiver;
//WrenHandle* method;
int callback_ref;
uhandle_t* next;
uhandle_t* prev;
};
Expand All @@ -400,7 +399,7 @@ int editor_run(editor_t* editor);
int editor_deinit(editor_t* editor);
int editor_prompt(editor_t* editor, char* prompt, editor_prompt_params_t* params, char** optret_answer);
int editor_menu(editor_t* editor, cmd_func_t fn_callback, char* opt_buf_data, int opt_buf_data_len, aproc_t* opt_aproc, bview_t** optret_menu);
int editor_open_bview(editor_t* editor, bview_t* parent, int type, char* opt_path, int opt_path_len, int make_active, bint_t linenum, int skip_resize, buffer_t* opt_buffer, bview_t** optret_bview);
int editor_open_bview(editor_t* editor, bview_t* opt_parent, int type, char* opt_path, int opt_path_len, int make_active, bint_t linenum, int skip_resize, buffer_t* opt_buffer, bview_t** optret_bview);
int editor_close_bview(editor_t* editor, bview_t* bview, int* optret_num_closed);
int editor_set_active(editor_t* editor, bview_t* bview);
int editor_bview_edit_count(editor_t* editor);
Expand Down Expand Up @@ -546,8 +545,8 @@ int aproc_destroy(aproc_t* aproc, int preempt);
int aproc_drain_all(aproc_t* aprocs, int* ttyfd);

// uscript functions
//uscript_t* uscript_run(editor_t* editor, char* path); TODO
//int uscript_destroy(uscript_t* uscript); TODO
uscript_t* uscript_run(editor_t* editor, char* path);
int uscript_destroy(uscript_t* uscript);

// util functions
int util_shell_exec(editor_t* editor, char* cmd, long timeout_s, char* input, size_t input_len, int setsid, char* opt_shell, char** optret_output, size_t* optret_output_len);
Expand Down
93 changes: 93 additions & 0 deletions tests/test_lua.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

on_exit() { [ -n "$lua_script" ] && rm -f $lua_script; }
trap on_exit EXIT
lua_script=$(mktemp 'mle.test_lua.XXXXXXXXXX')
extra_opts="-x $lua_script -K lua_kmap,,1 -k cmd_lua_test,F11, -k cmd_quit_without_saving,F12, -n lua_kmap"

# mle.mark_insert_before
macro='F11'
cat >$lua_script <<"EOD"
mle.editor_register_cmd("cmd_lua_test", function (ctx)
mle.mark_insert_before(ctx["mark"], "hello from lua\n", 15)
end)
EOD
declare -A expected
expected[simple_data]='^hello from lua$'
source 'test.sh'


# mle.editor_open_bview
macro='F11'
cat >$lua_script <<"EOD"
mle.editor_register_cmd("cmd_lua_test", function (ctx)
print "hi1"
mle.editor_open_bview(ctx["editor"], nil, 0, nil, 0, 1, 0, 0, nil)
print "hi2"
end)
EOD
declare -A expected
expected[open_data1 ]='^hi1$'
expected[open_data2 ]='^hi2$'
expected[open_bview_count]='^bview_count=2$'
source 'test.sh'

# mle.editor_prompt
macro='F11 t e s t enter . . . F11 C-c'
cat >$lua_script <<"EOD"
mle.editor_register_cmd("cmd_lua_test", function (ctx)
rv = mle.editor_prompt(ctx["editor"], "input?")
if rv then
str = "hello " .. rv .. " from lua"
else
str = "you hit ctrl-c"
end
mle.mark_insert_before(ctx["mark"], str, string.len(str))
end)
EOD
declare -A expected
expected[prompt_data]='^hello test from lua...you hit ctrl-c$'
source 'test.sh'

# mle.editor_register_observer
macro='F11'
cat >$lua_script <<"EOD"
mle.editor_register_cmd("cmd_lua_test", function (ctx)
print "ell"
end)
mle.editor_register_observer("cmd:cmd_lua_test:before", function (ctx)
print "h"
end)
mle.editor_register_observer("cmd:cmd_lua_test:after", function (ctx)
print "o"
end)
EOD
declare -A expected
expected[observer_data]='^hello$'
source 'test.sh'

# mle.editor_register_observer
macro='F11 h i enter M-e s e q space 1 space 5 | p a s t e space - s d , enter backspace backspace'
cat >$lua_script <<"EOD"
mark = nil
in_callback = false
mle.editor_register_cmd("cmd_lua_test", function (ctx)
mark = ctx["mark"]
bview = mle.editor_open_bview(ctx["editor"], nil, 0, nil, 0, 1, 0, 0, nil)
mle.editor_set_active(ctx["editor"], bview["optret_bview"])
end)
mle.editor_register_observer("buffer:baction", function (baction)
if in_callback or not mark then return end
in_callback = true
str = "buffer=" .. baction["buffer"] .. " byte_delta=" .. baction["byte_delta"] .. "\n"
mle.mark_insert_before(mark, str, string.len(str))
in_callback = false
end)
EOD
declare -A expected
expected[observer_data1 ]='^hi$'
expected[observer_data2 ]='^1,2,3,4,$'
expected[observer_output1]='^buffer=\S+ byte_delta=1$' # typing
expected[observer_output1]='^buffer=\S+ byte_delta=10$' # output from `seq 1 5 | paste -sd,`
expected[observer_output1]='^buffer=\S+ byte_delta=-1$' # backspacing
source 'test.sh'
93 changes: 0 additions & 93 deletions tests/test_lua.sh.todo

This file was deleted.

Loading

0 comments on commit 76f1c7a

Please sign in to comment.