aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile7
-rw-r--r--event-basic.c23
-rw-r--r--main.c21
-rw-r--r--view.c103
-rw-r--r--vis-core.h4
-rw-r--r--vis-lua.c208
-rw-r--r--vis-lua.h21
-rw-r--r--vis.c112
-rw-r--r--vis.h25
9 files changed, 236 insertions, 288 deletions
diff --git a/Makefile b/Makefile
index 7e77c7f..a2f1df3 100644
--- a/Makefile
+++ b/Makefile
@@ -4,20 +4,20 @@ REGEX_SRC ?= text-regex.c
SRC = array.c \
buffer.c \
+ event-basic.c \
libutf.c \
main.c \
map.c \
sam.c \
- text.c \
text-common.c \
text-io.c \
text-iterator.c \
text-motions.c \
text-objects.c \
text-util.c \
+ text.c \
ui-terminal.c \
view.c \
- vis.c \
vis-lua.c \
vis-marks.c \
vis-modes.c \
@@ -25,8 +25,9 @@ SRC = array.c \
vis-operators.c \
vis-prompt.c \
vis-registers.c \
- vis-text-objects.c \
vis-subprocess.c \
+ vis-text-objects.c \
+ vis.c \
$(REGEX_SRC)
OBJ = $(SRC:%.c=obj/%.o)
diff --git a/event-basic.c b/event-basic.c
new file mode 100644
index 0000000..6d298b5
--- /dev/null
+++ b/event-basic.c
@@ -0,0 +1,23 @@
+#include <stdarg.h>
+
+#include "vis-core.h"
+
+#if !CONFIG_LUA
+bool vis_event_emit(Vis *vis, enum VisEvents id, ...) {
+ if (!vis->initialized) {
+ vis->initialized = true;
+ vis->ui->init(vis->ui, vis);
+ }
+
+ va_list ap;
+ va_start(ap, id);
+
+ if (id == VIS_EVENT_WIN_STATUS) {
+ Win *win = va_arg(ap, Win*);
+ window_status_update(vis, win);
+ }
+
+ va_end(ap);
+ return true;
+}
+#endif
diff --git a/main.c b/main.c
index 8aa31c6..41726df 100644
--- a/main.c
+++ b/main.c
@@ -2213,26 +2213,7 @@ static void signal_handler(int signum, siginfo_t *siginfo, void *context) {
}
int main(int argc, char *argv[]) {
-
- VisEvent event = {
- .init = vis_lua_init,
- .start = vis_lua_start,
- .quit = vis_lua_quit,
- .mode_insert_input = vis_lua_mode_insert_input,
- .mode_replace_input = vis_lua_mode_replace_input,
- .file_open = vis_lua_file_open,
- .file_save_pre = vis_lua_file_save_pre,
- .file_save_post = vis_lua_file_save_post,
- .file_close = vis_lua_file_close,
- .win_open = vis_lua_win_open,
- .win_close = vis_lua_win_close,
- .win_highlight = vis_lua_win_highlight,
- .win_status = vis_lua_win_status,
- .term_csi = vis_lua_term_csi,
- .ui_draw = vis_lua_ui_draw,
- };
-
- vis = vis_new(ui_term_new(), &event);
+ vis = vis_new(ui_term_new());
if (!vis)
return EXIT_FAILURE;
diff --git a/view.c b/view.c
index df7906b..f27760b 100644
--- a/view.c
+++ b/view.c
@@ -1,9 +1,12 @@
-#include <string.h>
-#include <stdlib.h>
-#include <wchar.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
+#include "vis-core.h"
#include "view.h"
#include "text.h"
#include "text-motions.h"
@@ -119,6 +122,100 @@ static void selection_free(Selection*);
/* set/move current cursor position to a given (line, column) pair */
static size_t cursor_set(Selection*, Line *line, int col);
+void window_status_update(Vis *vis, Win *win) {
+ char left_parts[4][255] = { "", "", "", "" };
+ char right_parts[4][32] = { "", "", "", "" };
+ char left[sizeof(left_parts)+LENGTH(left_parts)*8];
+ char right[sizeof(right_parts)+LENGTH(right_parts)*8];
+ char status[sizeof(left)+sizeof(right)+1];
+ size_t left_count = 0;
+ size_t right_count = 0;
+
+ View *view = win->view;
+ File *file = win->file;
+ Text *txt = file->text;
+ int width = vis_window_width_get(win);
+ enum UiOption options = view_options_get(view);
+ bool focused = vis->win == win;
+ const char *filename = file_name_get(file);
+ const char *mode = vis->mode->status;
+
+ if (focused && mode)
+ strcpy(left_parts[left_count++], mode);
+
+ snprintf(left_parts[left_count++], sizeof(left_parts[0]), "%s%s%s",
+ filename ? filename : "[No Name]",
+ text_modified(txt) ? " [+]" : "",
+ vis_macro_recording(vis) ? " @": "");
+
+ int count = vis_count_get(vis);
+ const char *keys = buffer_content0(&vis->input_queue);
+ if (keys && keys[0])
+ snprintf(right_parts[right_count++], sizeof(right_parts[0]), "%s", keys);
+ else if (count != VIS_COUNT_UNKNOWN)
+ snprintf(right_parts[right_count++], sizeof(right_parts[0]), "%d", count);
+
+ int sel_count = view_selections_count(view);
+ if (sel_count > 1) {
+ Selection *s = view_selections_primary_get(view);
+ int sel_number = view_selections_number(s) + 1;
+ snprintf(right_parts[right_count++], sizeof(right_parts[0]),
+ "%d/%d", sel_number, sel_count);
+ }
+
+ size_t size = text_size(txt);
+ size_t pos = view_cursor_get(view);
+ size_t percent = 0;
+ if (size > 0) {
+ double tmp = ((double)pos/(double)size)*100;
+ percent = (size_t)(tmp+1);
+ }
+ snprintf(right_parts[right_count++], sizeof(right_parts[0]),
+ "%zu%%", percent);
+
+ if (!(options & UI_OPTION_LARGE_FILE)) {
+ Selection *sel = view_selections_primary_get(win->view);
+ size_t line = view_cursors_line(sel);
+ size_t col = view_cursors_col(sel);
+ if (col > UI_LARGE_FILE_LINE_SIZE) {
+ options |= UI_OPTION_LARGE_FILE;
+ view_options_set(win->view, options);
+ }
+ snprintf(right_parts[right_count++], sizeof(right_parts[0]),
+ "%zu, %zu", line, col);
+ }
+
+ int left_len = snprintf(left, sizeof(left), " %s%s%s%s%s%s%s",
+ left_parts[0],
+ left_parts[1][0] ? " » " : "",
+ left_parts[1],
+ left_parts[2][0] ? " » " : "",
+ left_parts[2],
+ left_parts[3][0] ? " » " : "",
+ left_parts[3]);
+
+ int right_len = snprintf(right, sizeof(right), "%s%s%s%s%s%s%s ",
+ right_parts[0],
+ right_parts[1][0] ? " « " : "",
+ right_parts[1],
+ right_parts[2][0] ? " « " : "",
+ right_parts[2],
+ right_parts[3][0] ? " « " : "",
+ right_parts[3]);
+
+ if (left_len < 0 || right_len < 0)
+ return;
+ int left_width = text_string_width(left, left_len);
+ int right_width = text_string_width(right, right_len);
+
+ int spaces = width - left_width - right_width;
+ if (spaces < 1)
+ spaces = 1;
+
+ snprintf(status, sizeof(status), "%s%*s%s", left, spaces, " ", right);
+ vis_window_status(win, status);
+}
+
void view_tabwidth_set(View *view, int tabwidth) {
if (tabwidth < 1 || tabwidth > 8)
return;
diff --git a/vis-core.h b/vis-core.h
index 4f81e4c..9746fc9 100644
--- a/vis-core.h
+++ b/vis-core.h
@@ -215,7 +215,6 @@ struct Vis {
Array actions_user; /* dynamically allocated editor actions */
lua_State *lua; /* lua context used for syntax highlighting */
enum TextLoadMethod load_method; /* how existing files should be loaded */
- VisEvent *event;
Array operators;
Array motions;
Array textobjects;
@@ -268,8 +267,9 @@ Mode *mode_get(Vis*, enum VisMode);
void mode_set(Vis *vis, Mode *new_mode);
Macro *macro_get(Vis *vis, enum VisRegister);
-void window_selection_save(Win *win);
Win *window_new_file(Vis*, File*, enum UiOption);
+void window_selection_save(Win *win);
+void window_status_update(Vis *vis, Win *win);
char *absolute_path(const char *path);
diff --git a/vis-lua.c b/vis-lua.c
index ca36d2d..2246403 100644
--- a/vis-lua.c
+++ b/vis-lua.c
@@ -54,119 +54,13 @@
#define debug(...) do { } while (0)
#endif
-static void window_status_update(Vis *vis, Win *win) {
- char left_parts[4][255] = { "", "", "", "" };
- char right_parts[4][32] = { "", "", "", "" };
- char left[sizeof(left_parts)+LENGTH(left_parts)*8];
- char right[sizeof(right_parts)+LENGTH(right_parts)*8];
- char status[sizeof(left)+sizeof(right)+1];
- size_t left_count = 0;
- size_t right_count = 0;
-
- View *view = win->view;
- File *file = win->file;
- Text *txt = file->text;
- int width = vis_window_width_get(win);
- enum UiOption options = view_options_get(view);
- bool focused = vis->win == win;
- const char *filename = file_name_get(file);
- const char *mode = vis->mode->status;
-
- if (focused && mode)
- strcpy(left_parts[left_count++], mode);
-
- snprintf(left_parts[left_count++], sizeof(left_parts[0]), "%s%s%s",
- filename ? filename : "[No Name]",
- text_modified(txt) ? " [+]" : "",
- vis_macro_recording(vis) ? " @": "");
-
- int count = vis_count_get(vis);
- const char *keys = buffer_content0(&vis->input_queue);
- if (keys && keys[0])
- snprintf(right_parts[right_count++], sizeof(right_parts[0]), "%s", keys);
- else if (count != VIS_COUNT_UNKNOWN)
- snprintf(right_parts[right_count++], sizeof(right_parts[0]), "%d", count);
-
- int sel_count = view_selections_count(view);
- if (sel_count > 1) {
- Selection *s = view_selections_primary_get(view);
- int sel_number = view_selections_number(s) + 1;
- snprintf(right_parts[right_count++], sizeof(right_parts[0]),
- "%d/%d", sel_number, sel_count);
- }
-
- size_t size = text_size(txt);
- size_t pos = view_cursor_get(view);
- size_t percent = 0;
- if (size > 0) {
- double tmp = ((double)pos/(double)size)*100;
- percent = (size_t)(tmp+1);
- }
- snprintf(right_parts[right_count++], sizeof(right_parts[0]),
- "%zu%%", percent);
-
- if (!(options & UI_OPTION_LARGE_FILE)) {
- Selection *sel = view_selections_primary_get(win->view);
- size_t line = view_cursors_line(sel);
- size_t col = view_cursors_col(sel);
- if (col > UI_LARGE_FILE_LINE_SIZE) {
- options |= UI_OPTION_LARGE_FILE;
- view_options_set(win->view, options);
- }
- snprintf(right_parts[right_count++], sizeof(right_parts[0]),
- "%zu, %zu", line, col);
- }
-
- int left_len = snprintf(left, sizeof(left), " %s%s%s%s%s%s%s",
- left_parts[0],
- left_parts[1][0] ? " » " : "",
- left_parts[1],
- left_parts[2][0] ? " » " : "",
- left_parts[2],
- left_parts[3][0] ? " » " : "",
- left_parts[3]);
-
- int right_len = snprintf(right, sizeof(right), "%s%s%s%s%s%s%s ",
- right_parts[0],
- right_parts[1][0] ? " « " : "",
- right_parts[1],
- right_parts[2][0] ? " « " : "",
- right_parts[2],
- right_parts[3][0] ? " « " : "",
- right_parts[3]);
-
- if (left_len < 0 || right_len < 0)
- return;
- int left_width = text_string_width(left, left_len);
- int right_width = text_string_width(right, right_len);
-
- int spaces = width - left_width - right_width;
- if (spaces < 1)
- spaces = 1;
-
- snprintf(status, sizeof(status), "%s%*s%s", left, spaces, " ", right);
- vis_window_status(win, status);
-}
#if !CONFIG_LUA
bool vis_lua_path_add(Vis *vis, const char *path) { return true; }
bool vis_lua_paths_get(Vis *vis, char **lpath, char **cpath) { return false; }
-void vis_lua_init(Vis *vis) { }
-void vis_lua_start(Vis *vis) { }
-void vis_lua_quit(Vis *vis) { }
-void vis_lua_file_open(Vis *vis, File *file) { }
-bool vis_lua_file_save_pre(Vis *vis, File *file, const char *path) { return true; }
-void vis_lua_file_save_post(Vis *vis, File *file, const char *path) { }
-void vis_lua_file_close(Vis *vis, File *file) { }
-void vis_lua_win_open(Vis *vis, Win *win) { }
-void vis_lua_win_close(Vis *vis, Win *win) { }
-void vis_lua_win_highlight(Vis *vis, Win *win) { }
-void vis_lua_win_status(Vis *vis, Win *win) { window_status_update(vis, win); }
-void vis_lua_term_csi(Vis *vis, const long *csi) { }
void vis_lua_process_response(Vis *vis, const char *name,
char *buffer, size_t len, ResponseType rtype) { }
-void vis_lua_ui_draw(Vis *vis) { }
#else
@@ -3213,7 +3107,7 @@ static void *alloc_lua(void *ud, void *ptr, size_t osize, size_t nsize) {
* Can be used to set *global* configuration options.
* @function init
*/
-void vis_lua_init(Vis *vis) {
+static void vis_lua_init(Vis *vis) {
lua_State *L = lua_newstate(alloc_lua, vis);
if (!L)
return;
@@ -3432,7 +3326,7 @@ void vis_lua_init(Vis *vis) {
* We are about to process interactive keyboard input.
* @function start
*/
-void vis_lua_start(Vis *vis) {
+static void vis_lua_start(Vis *vis) {
vis_lua_event_call(vis, "start");
}
@@ -3440,7 +3334,7 @@ void vis_lua_start(Vis *vis) {
* Editor is about to terminate.
* @function quit
*/
-void vis_lua_quit(Vis *vis) {
+static void vis_lua_quit(Vis *vis) {
if (!vis->lua)
return;
vis_lua_event_call(vis, "quit");
@@ -3471,12 +3365,12 @@ static bool vis_lua_input(Vis *vis, const char *key, size_t len) {
return ret;
}
-void vis_lua_mode_insert_input(Vis *vis, const char *key, size_t len) {
+void vis_event_mode_insert_input(Vis *vis, const char *key, size_t len) {
if (!vis_lua_input(vis, key, len))
vis_insert_key(vis, key, len);
}
-void vis_lua_mode_replace_input(Vis *vis, const char *key, size_t len) {
+void vis_event_mode_replace_input(Vis *vis, const char *key, size_t len) {
if (!vis_lua_input(vis, key, len))
vis_replace_key(vis, key, len);
}
@@ -3486,7 +3380,7 @@ void vis_lua_mode_replace_input(Vis *vis, const char *key, size_t len) {
* @function file_open
* @tparam File file the file to be opened
*/
-void vis_lua_file_open(Vis *vis, File *file) {
+static void vis_lua_file_open(Vis *vis, File *file) {
debug("event: file-open: %s %p %p\n", file->name ? file->name : "unnamed", (void*)file, (void*)file->text);
lua_State *L = vis->lua;
if (!L)
@@ -3507,7 +3401,7 @@ void vis_lua_file_open(Vis *vis, File *file) {
* @tparam string path the absolute path to which the file will be written, `nil` if standard output
* @treturn bool whether the write operation should be proceeded
*/
-bool vis_lua_file_save_pre(Vis *vis, File *file, const char *path) {
+static bool vis_lua_file_save_pre(Vis *vis, File *file, const char *path) {
lua_State *L = vis->lua;
if (!L)
return true;
@@ -3530,7 +3424,7 @@ bool vis_lua_file_save_pre(Vis *vis, File *file, const char *path) {
* @tparam File file the file which was written
* @tparam string path the absolute path to which it was written, `nil` if standard output
*/
-void vis_lua_file_save_post(Vis *vis, File *file, const char *path) {
+static void vis_lua_file_save_post(Vis *vis, File *file, const char *path) {
lua_State *L = vis->lua;
if (!L)
return;
@@ -3549,7 +3443,7 @@ void vis_lua_file_save_post(Vis *vis, File *file, const char *path) {
* @function file_close
* @tparam File file the file being closed
*/
-void vis_lua_file_close(Vis *vis, File *file) {
+static void vis_lua_file_close(Vis *vis, File *file) {
debug("event: file-close: %s %p %p\n", file->name ? file->name : "unnamed", (void*)file, (void*)file->text);
lua_State *L = vis->lua;
if (!L)
@@ -3571,7 +3465,7 @@ void vis_lua_file_close(Vis *vis, File *file) {
* @function win_open
* @tparam Window win the window being opened
*/
-void vis_lua_win_open(Vis *vis, Win *win) {
+static void vis_lua_win_open(Vis *vis, Win *win) {
debug("event: win-open: %s %p %p\n", win->file->name ? win->file->name : "unnamed", (void*)win, (void*)win->view);
lua_State *L = vis->lua;
if (!L)
@@ -3590,7 +3484,7 @@ void vis_lua_win_open(Vis *vis, Win *win) {
* @function win_close
* @tparam Window win the window being closed
*/
-void vis_lua_win_close(Vis *vis, Win *win) {
+static void vis_lua_win_close(Vis *vis, Win *win) {
debug("event: win-close: %s %p %p\n", win->file->name ? win->file->name : "unnamed", (void*)win, (void*)win->view);
lua_State *L = vis->lua;
if (!L)
@@ -3612,7 +3506,7 @@ void vis_lua_win_close(Vis *vis, Win *win) {
* @tparam Window win the window being redrawn
* @see style
*/
-void vis_lua_win_highlight(Vis *vis, Win *win) {
+static void vis_lua_win_highlight(Vis *vis, Win *win) {
lua_State *L = vis->lua;
if (!L)
return;
@@ -3630,7 +3524,7 @@ void vis_lua_win_highlight(Vis *vis, Win *win) {
* @tparam Window win the affected window
* @see status
*/
-void vis_lua_win_status(Vis *vis, Win *win) {
+static void vis_lua_win_status(Vis *vis, Win *win) {
lua_State *L = vis->lua;
if (!L || win->file->internal) {
window_status_update(vis, win);
@@ -3651,7 +3545,7 @@ void vis_lua_win_status(Vis *vis, Win *win) {
* @function term_csi
* @param List of CSI parameters
*/
-void vis_lua_term_csi(Vis *vis, const long *csi) {
+static void vis_lua_term_csi(Vis *vis, const long *csi) {
lua_State *L = vis->lua;
if (!L)
return;
@@ -3711,8 +3605,80 @@ void vis_lua_process_response(Vis *vis, const char *name,
* Use sparingly and check for `nil` values!
* @function ui_draw
*/
-void vis_lua_ui_draw(Vis *vis) {
+static void vis_lua_ui_draw(Vis *vis) {
vis_lua_event_call(vis, "ui_draw");
}
+bool vis_event_emit(Vis *vis, enum VisEvents id, ...) {
+ if (!vis->initialized) {
+ vis->initialized = true;
+ vis->ui->init(vis->ui, vis);
+ vis_lua_init(vis);
+ }
+
+ va_list ap;
+ va_start(ap, id);
+ bool ret = true;
+
+ switch (id) {
+ case VIS_EVENT_INIT:
+ break;
+ case VIS_EVENT_START:
+ vis_lua_start(vis);
+ break;
+ case VIS_EVENT_FILE_OPEN:
+ case VIS_EVENT_FILE_SAVE_PRE:
+ case VIS_EVENT_FILE_SAVE_POST:
+ case VIS_EVENT_FILE_CLOSE:
+ {
+ File *file = va_arg(ap, File*);
+ if (file->internal)
+ break;
+ if (id == VIS_EVENT_FILE_OPEN) {
+ vis_lua_file_open(vis, file);
+ } else if (id == VIS_EVENT_FILE_SAVE_PRE) {
+ const char *path = va_arg(ap, const char*);
+ ret = vis_lua_file_save_pre(vis, file, path);
+ } else if (id == VIS_EVENT_FILE_SAVE_POST) {
+ const char *path = va_arg(ap, const char*);
+ vis_lua_file_save_post(vis, file, path);
+ } else if (id == VIS_EVENT_FILE_CLOSE) {
+ vis_lua_file_close(vis, file);
+ }
+ break;
+ }
+ case VIS_EVENT_WIN_OPEN:
+ case VIS_EVENT_WIN_CLOSE:
+ case VIS_EVENT_WIN_HIGHLIGHT:
+ case VIS_EVENT_WIN_STATUS:
+ {
+ Win *win = va_arg(ap, Win*);
+ if (win->file->internal && id != VIS_EVENT_WIN_STATUS)
+ break;
+ if (id == VIS_EVENT_WIN_OPEN) {
+ vis_lua_win_open(vis, win);
+ } else if (id == VIS_EVENT_WIN_CLOSE) {
+ vis_lua_win_close(vis, win);
+ } else if (id == VIS_EVENT_WIN_HIGHLIGHT) {
+ vis_lua_win_highlight(vis, win);
+ } else if (id == VIS_EVENT_WIN_STATUS) {
+ vis_lua_win_status(vis, win);
+ }
+ break;
+ }
+ case VIS_EVENT_QUIT:
+ vis_lua_quit(vis);
+ break;
+ case VIS_EVENT_TERM_CSI:
+ vis_lua_term_csi(vis, va_arg(ap, const long *));
+ break;
+ case VIS_EVENT_UI_DRAW:
+ vis_lua_ui_draw(vis);
+ break;
+ }
+
+ va_end(ap);
+ return ret;
+}
+
#endif
diff --git a/vis-lua.h b/vis-lua.h
index b4f3f51..76e7029 100644
--- a/vis-lua.h
+++ b/vis-lua.h
@@ -22,26 +22,13 @@ bool vis_lua_path_add(Vis*, const char *path);
bool vis_lua_paths_get(Vis*, char **lpath, char **cpath);
/* various event handlers, triggered by the vis core */
-void vis_lua_init(Vis*);
-void vis_lua_start(Vis*);
-void vis_lua_quit(Vis*);
#if !CONFIG_LUA
-#define vis_lua_mode_insert_input vis_insert_key
-#define vis_lua_mode_replace_input vis_replace_key
+#define vis_event_mode_insert_input vis_insert_key
+#define vis_event_mode_replace_input vis_replace_key
#else
-void vis_lua_mode_insert_input(Vis*, const char *key, size_t len);
-void vis_lua_mode_replace_input(Vis*, const char *key, size_t len);
+void vis_event_mode_insert_input(Vis*, const char *key, size_t len);
+void vis_event_mode_replace_input(Vis*, const char *key, size_t len);
#endif
-void vis_lua_file_open(Vis*, File*);
-bool vis_lua_file_save_pre(Vis*, File*, const char *path);
-void vis_lua_file_save_post(Vis*, File*, const char *path);
-void vis_lua_file_close(Vis*, File*);
-void vis_lua_win_open(Vis*, Win*);
-void vis_lua_win_close(Vis*, Win*);
-void vis_lua_win_highlight(Vis*, Win*);
-void vis_lua_win_status(Vis*, Win*);
-void vis_lua_term_csi(Vis*, const long *);
void vis_lua_process_response(Vis *, const char *, char *, size_t, ResponseType);
-void vis_lua_ui_draw(Vis*);
#endif
diff --git a/vis.c b/vis.c
index cabad67..616b463 100644
--- a/vis.c
+++ b/vis.c
@@ -35,86 +35,6 @@ static void macro_replay(Vis *vis, const Macro *macro);
static void macro_replay_internal(Vis *vis, const Macro *macro);
static void vis_keys_push(Vis *vis, const char *input, size_t pos, bool record);
-bool vis_event_emit(Vis *vis, enum VisEvents id, ...) {
- if (!vis->event)
- return true;
-
- if (!vis->initialized) {
- vis->initialized = true;
- vis->ui->init(vis->ui, vis);
- if (vis->event->init)
- vis->event->init(vis);
- }
-
- va_list ap;
- va_start(ap, id);
- bool ret = true;
-
- switch (id) {
- case VIS_EVENT_INIT:
- break;
- case VIS_EVENT_START:
- if (vis->event->start)
- vis->event->start(vis);
- break;
- case VIS_EVENT_FILE_OPEN:
- case VIS_EVENT_FILE_SAVE_PRE:
- case VIS_EVENT_FILE_SAVE_POST:
- case VIS_EVENT_FILE_CLOSE:
- {
- File *file = va_arg(ap, File*);
- if (file->internal)
- break;
- if (id == VIS_EVENT_FILE_OPEN && vis->event->file_open) {
- vis->event->file_open(vis, file);
- } else if (id == VIS_EVENT_FILE_SAVE_PRE && vis->event->file_save_pre) {
- const char *path = va_arg(ap, const char*);
- ret = vis->event->file_save_pre(vis, file, path);
- } else if (id == VIS_EVENT_FILE_SAVE_POST && vis->event->file_save_post) {
- const char *path = va_arg(ap, const char*);
- vis->event->file_save_post(vis, file, path);
- } else if (id == VIS_EVENT_FILE_CLOSE && vis->event->file_close) {
- vis->event->file_close(vis, file);
- }
- break;
- }
- case VIS_EVENT_WIN_OPEN:
- case VIS_EVENT_WIN_CLOSE:
- case VIS_EVENT_WIN_HIGHLIGHT:
- case VIS_EVENT_WIN_STATUS:
- {
- Win *win = va_arg(ap, Win*);
- if (win->file->internal && id != VIS_EVENT_WIN_STATUS)
- break;
- if (vis->event->win_open && id == VIS_EVENT_WIN_OPEN) {
- vis->event->win_open(vis, win);
- } else if (vis->event->win_close && id == VIS_EVENT_WIN_CLOSE) {
- vis->event->win_close(vis, win);
- } else if (vis->event->win_highlight && id == VIS_EVENT_WIN_HIGHLIGHT) {
- vis->event->win_highlight(vis, win);
- } else if (vis->event->win_status && id == VIS_EVENT_WIN_STATUS) {
- vis->event->win_status(vis, win);
- }
- break;
- }
- case VIS_EVENT_QUIT:
- if (vis->event->quit)
- vis->event->quit(vis);
- break;
- case VIS_EVENT_TERM_CSI:
- if (vis->event->term_csi)
- vis->event->term_csi(vis, va_arg(ap, const long *));
- break;
- case VIS_EVENT_UI_DRAW:
- if (vis->event->ui_draw)
- vis->event->ui_draw(vis);
- break;
- }
-
- va_end(ap);
- return ret;
-}
-
/** window / file handling */
static void file_free(Vis *vis, File *file) {
@@ -182,7 +102,7 @@ err:
return path_normalized[0] ? strdup(path_normalized) : NULL;
}
-static File *file_new(Vis *vis, const char *name) {
+static File *file_new(Vis *vis, const char *name, bool internal) {
char *name_absolute = NULL;
bool cmp_names = 0;
struct stat new;
@@ -225,7 +145,9 @@ static File *file_new(Vis *vis, const char *name) {
if (!(file = file_new_text(vis, text)))
goto err;
file->name = name_absolute;
- vis_event_emit(vis, VIS_EVENT_FILE_OPEN, file);
+ file->internal = internal;
+ if (!internal)
+ vis_event_emit(vis, VIS_EVENT_FILE_OPEN, file);
return file;
err:
free(name_absolute);
@@ -235,11 +157,9 @@ err:
}
static File *file_new_internal(Vis *vis, const char *filename) {
- File *file = file_new(vis, filename);
- if (file) {
+ File *file = file_new(vis, filename, true);
+ if (file)
file->refcount = 1;
- file->internal = true;
- }
return file;
}
@@ -495,7 +415,7 @@ bool vis_window_reload(Win *win) {
return false; /* can't reload unsaved file */
/* temporarily unset file name, otherwise file_new returns the same File */
win->file->name = NULL;
- File *file = file_new(win->vis, name);
+ File *file = file_new(win->vis, name, false);
win->file->name = name;
if (!file)
return false;
@@ -507,7 +427,7 @@ bool vis_window_reload(Win *win) {
}
bool vis_window_change_file(Win *win, const char* filename) {
- File *file = file_new(win->vis, filename);
+ File *file = file_new(win->vis, filename, false);
if (!file)
return false;
file->refcount++;
@@ -596,7 +516,7 @@ void vis_doupdates(Vis *vis, bool doupdate) {
}
bool vis_window_new(Vis *vis, const char *filename) {
- File *file = file_new(vis, filename);
+ File *file = file_new(vis, filename, false);
if (!file)
return false;
vis_doupdates(vis, false);
@@ -676,7 +596,7 @@ void vis_window_close(Win *win) {
vis_draw(vis);
}
-Vis *vis_new(Ui *ui, VisEvent *event) {
+Vis *vis_new(Ui *ui) {
if (!ui)
return NULL;
Vis *vis = calloc(1, sizeof(Vis));
@@ -719,13 +639,8 @@ Vis *vis_new(Ui *ui, VisEvent *event) {
if (!(vis->shell = strdup(shell)))
goto err;
vis->mode_prev = vis->mode = &vis_modes[VIS_MODE_NORMAL];
- vis->event = event;
- if (event) {
- if (event->mode_insert_input)
- vis_modes[VIS_MODE_INSERT].input = event->mode_insert_input;
- if (event->mode_replace_input)
- vis_modes[VIS_MODE_REPLACE].input = event->mode_replace_input;
- }
+ vis_modes[VIS_MODE_INSERT].input = vis_event_mode_insert_input;
+ vis_modes[VIS_MODE_REPLACE].input = vis_event_mode_replace_input;
return vis;
err:
vis_free(vis);
@@ -735,10 +650,9 @@ err:
void vis_free(Vis *vis) {
if (!vis)
return;
- vis_event_emit(vis, VIS_EVENT_QUIT);
- vis->event = NULL;
while (vis->windows)
vis_window_close(vis->windows);
+ vis_event_emit(vis, VIS_EVENT_QUIT);
file_free(vis, vis->command_file);
file_free(vis, vis->search_file);
file_free(vis, vis->error_file);
diff --git a/vis.h b/vis.h
index fcc1e05..181083d 100644
--- a/vis.h
+++ b/vis.h
@@ -40,27 +40,6 @@ typedef struct Win Win;
/* maximum bytes needed for string representation of a (pseudo) key */
#define VIS_KEY_LENGTH_MAX 64
-/**
- * Editor event handlers.
- */
-typedef struct {
- void (*init)(Vis*);
- void (*start)(Vis*);
- void (*quit)(Vis*);
- void (*mode_insert_input)(Vis*, const char *key, size_t len);
- void (*mode_replace_input)(Vis*, const char *key, size_t len);
- void (*file_open)(Vis*, File*);
- bool (*file_save_pre)(Vis*, File*, const char *path);
- void (*file_save_post)(Vis*, File*, const char *path);
- void (*file_close)(Vis*, File*);
- void (*win_open)(Vis*, Win*);
- void (*win_close)(Vis*, Win*);
- void (*win_highlight)(Vis*, Win*);
- void (*win_status)(Vis*, Win*);
- void (*term_csi)(Vis*, const long *);
- void (*ui_draw)(Vis*);
-} VisEvent;
-
/** Union used to pass arguments to key action functions. */
typedef union {
bool b;
@@ -111,8 +90,8 @@ typedef struct {
* @defgroup vis_lifecycle
* @{
*/
-/** Create a new editor instance using the given user interface and event handlers. */
-Vis *vis_new(Ui*, VisEvent*);
+/** Create a new editor instance using the given user interface. */
+Vis *vis_new(Ui*);
/** Free all resources associated with this editor instance, terminates UI. */
void vis_free(Vis*);
/**