Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes introducing a couple of events to enable the 'vis-line-numbers' plugin #525

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lua/vis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ local events = {
INPUT = "Event::INPUT", -- see @{input}
QUIT = "Event::QUIT", -- see @{quit}
START = "Event::START", -- see @{start}
MODE_CHANGE = "Event::MODE_CHANGE", -- see @{mode_change}
WIN_CLOSE = "Event::WIN_CLOSE", -- see @{win_close}
WIN_ENTER = "Event::WIN_ENTER", -- see @{win_enter}
WIN_HIGHLIGHT = "Event::WIN_HIGHLIGHT", -- see @{win_highlight}
WIN_LEAVE = "Event::WIN_LEAVE", -- see @{win_leave}
WIN_OPEN = "Event::WIN_OPEN", -- see @{win_open}
WIN_STATUS = "Event::WIN_STATUS", -- see @{win_status}
}
Expand All @@ -157,10 +160,13 @@ events.file_save_post = function(...) events.emit(events.FILE_SAVE_POST, ...) en
events.file_save_pre = function(...) return events.emit(events.FILE_SAVE_PRE, ...) end
events.init = function(...) events.emit(events.INIT, ...) end
events.input = function(...) return events.emit(events.INPUT, ...) end
events.mode_change = function(...) events.emit(events.MODE_CHANGE, ...) end
events.quit = function(...) events.emit(events.QUIT, ...) end
events.start = function(...) events.emit(events.START, ...) end
events.win_close = function(...) events.emit(events.WIN_CLOSE, ...) end
events.win_enter = function(...) events.emit(events.WIN_ENTER, ...) end
events.win_highlight = function(...) events.emit(events.WIN_HIGHLIGHT, ...) end
events.win_leave = function(...) events.emit(events.WIN_LEAVE, ...) end
events.win_open = function(...) events.emit(events.WIN_OPEN, ...) end
events.win_status = function(...) events.emit(events.WIN_STATUS, ...) end

Expand Down
19 changes: 12 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2033,8 +2033,11 @@ int main(int argc, char *argv[]) {
.file_close = vis_lua_file_close,
.win_open = vis_lua_win_open,
.win_close = vis_lua_win_close,
.win_enter = vis_lua_win_enter,
.win_leave = vis_lua_win_leave,
.win_highlight = vis_lua_win_highlight,
.win_status = vis_lua_win_status,
.mode_change = vis_lua_mode_change,
};

vis = vis_new(ui_term_new(), &event);
Expand Down Expand Up @@ -2112,11 +2115,12 @@ int main(int argc, char *argv[]) {

char *cmd = NULL;
bool end_of_options = false, win_created = false;
Win *win = NULL;

for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-' && !end_of_options) {
if (strcmp(argv[i], "-") == 0) {
if (!vis_window_new_fd(vis, STDOUT_FILENO))
if (!(win = vis_window_new_fd(vis, STDOUT_FILENO)))
vis_die(vis, "Can not create empty buffer\n");
ssize_t len = 0;
char buf[PIPE_BUF];
Expand All @@ -2138,7 +2142,7 @@ int main(int argc, char *argv[]) {
} else if (argv[i][0] == '+' && !end_of_options) {
cmd = argv[i] + (argv[i][1] == '/' || argv[i][1] == '?');
continue;
} else if (!vis_window_new(vis, argv[i])) {
} else if (!(win = vis_window_new(vis, argv[i]))) {
vis_die(vis, "Can not load `%s': %s\n", argv[i], strerror(errno));
}
win_created = true;
Expand All @@ -2148,12 +2152,13 @@ int main(int argc, char *argv[]) {
}
}

if (!vis_window(vis) && !win_created) {
if (!vis_window_new(vis, NULL))
bool create_default_win = !vis_window(vis) && !win_created;
if (create_default_win)
if (!(win =vis_window_new(vis, NULL)))
vis_die(vis, "Can not create empty buffer\n");
if (cmd)
vis_prompt_cmd(vis, cmd);
}
vis_window_focus(win);
if (create_default_win && cmd)
vis_prompt_cmd(vis, cmd);

int status = vis_run(vis, argc, argv);
vis_free(vis);
Expand Down
9 changes: 6 additions & 3 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,9 @@ static enum SamError command_validate(Command *cmd) {
return validate(cmd, false, false);
}

enum SamError sam_cmd(Vis *vis, const char *s) {
/* The window argument can be null, in which case the currently active window is
* used. */
enum SamError sam_cmd(Vis *vis, Win *win, const char *s) {
enum SamError err = SAM_ERR_OK;
if (!s)
return err;
Expand All @@ -1097,7 +1099,7 @@ enum SamError sam_cmd(Vis *vis, const char *s) {
bool visual = vis->mode->visual;
size_t primary_pos = vis->win ? view_cursor_get(vis->win->view) : EPOS;
Filerange range = text_range_empty();
sam_execute(vis, vis->win, cmd, NULL, &range);
sam_execute(vis, win ? win : vis->win, cmd, NULL, &range);

for (File *file = vis->files; file; file = file->next) {
if (file->internal)
Expand Down Expand Up @@ -1162,7 +1164,8 @@ enum SamError sam_cmd(Vis *vis, const char *s) {
break;
}
}
vis_mode_switch(vis, completed ? VIS_MODE_NORMAL : VIS_MODE_VISUAL);
if (!completed)
vis_mode_switch(vis, VIS_MODE_VISUAL);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not to sure about this. It passes the tests, but I did not take time to investigate fully.
There is a potential issue here with recursive events being fired on mode switch

}
command_free(cmd);
return err;
Expand Down
2 changes: 1 addition & 1 deletion sam.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum SamError {
};

bool sam_init(Vis*);
enum SamError sam_cmd(Vis*, const char *cmd);
enum SamError sam_cmd(Vis*, Win*, const char *cmd);
const char *sam_error(enum SamError);

#endif
31 changes: 15 additions & 16 deletions vis-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,25 +390,30 @@ static const char *file_open_dialog(Vis *vis, const char *pattern) {
return name[0] ? name : NULL;
}

static bool openfiles(Vis *vis, const char **files) {
static bool openfiles(Vis *vis, const char **files, enum UiOption options) {
Win *win = NULL;
for (; *files; files++) {
const char *file = file_open_dialog(vis, *files);
if (!file)
return false;
errno = 0;
if (!vis_window_new(vis, file)) {
win = vis_window_new(vis, file);
if (!win) {
vis_info_show(vis, "Could not open `%s' %s", file,
errno ? strerror(errno) : "");
return false;
}
view_options_set(win->view, options);
}
if (win)
vis_window_focus(win);
return true;
}

static bool cmd_open(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
if (!argv[1])
return vis_window_new(vis, NULL);
return openfiles(vis, &argv[1]);
return vis_window_focus_new(vis, NULL);
return openfiles(vis, &argv[1], view_options_get(vis->win->view));
}

static void info_unsaved_changes(Vis *vis) {
Expand All @@ -434,7 +439,7 @@ static bool cmd_edit(Vis *vis, Win *win, Command *cmd, const char *argv[], Curso
}
return vis_window_reload(oldwin);
}
if (!openfiles(vis, &argv[1]))
if (!openfiles(vis, &argv[1], view_options_get(vis->win->view)))
return false;
if (vis->win != oldwin) {
Win *newwin = vis->win;
Expand Down Expand Up @@ -504,10 +509,7 @@ static bool cmd_split(Vis *vis, Win *win, Command *cmd, const char *argv[], Curs
windows_arrange(vis, UI_LAYOUT_HORIZONTAL);
if (!argv[1])
return vis_window_split(win);
bool ret = openfiles(vis, &argv[1]);
if (ret)
view_options_set(vis->win->view, options);
return ret;
return openfiles(vis, &argv[1], options);
}

static bool cmd_vsplit(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
Expand All @@ -517,20 +519,17 @@ static bool cmd_vsplit(Vis *vis, Win *win, Command *cmd, const char *argv[], Cur
windows_arrange(vis, UI_LAYOUT_VERTICAL);
if (!argv[1])
return vis_window_split(win);
bool ret = openfiles(vis, &argv[1]);
if (ret)
view_options_set(vis->win->view, options);
return ret;
return openfiles(vis, &argv[1], options);
}

static bool cmd_new(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
windows_arrange(vis, UI_LAYOUT_HORIZONTAL);
return vis_window_new(vis, NULL);
return vis_window_focus_new(vis, NULL);
}

static bool cmd_vnew(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
windows_arrange(vis, UI_LAYOUT_VERTICAL);
return vis_window_new(vis, NULL);
return vis_window_focus_new(vis, NULL);
}

static bool cmd_wq(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
Expand Down Expand Up @@ -717,7 +716,7 @@ static void print_symbolic_keys(Vis *vis, Text *txt) {
}

static bool cmd_help(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
if (!vis_window_new(vis, NULL))
if (!vis_window_focus_new(vis, NULL))
return false;

Text *txt = vis->win->file->text;
Expand Down
7 changes: 6 additions & 1 deletion vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,11 @@ enum VisEvents {
VIS_EVENT_FILE_CLOSE,
VIS_EVENT_WIN_OPEN,
VIS_EVENT_WIN_CLOSE,
VIS_EVENT_WIN_ENTER,
VIS_EVENT_WIN_LEAVE,
VIS_EVENT_WIN_HIGHLIGHT,
VIS_EVENT_WIN_STATUS,
VIS_EVENT_MODE_CHANGE,
};

bool vis_event_emit(Vis*, enum VisEvents, ...);
Expand Down Expand Up @@ -247,7 +250,9 @@ void vis_do(Vis *vis);
void action_reset(Action*);
size_t vis_text_insert_nl(Vis*, Text*, size_t pos);

void mode_set(Vis *vis, Mode *new_mode);
Mode *mode_get(Vis *vis, enum VisMode mode);
/* Returns true if the mode actually changed. */
bool mode_set(Vis *vis, Mode *new_mode);

void window_selection_save(Win *win);
Win *window_new_file(Vis*, File*, enum UiOption);
Expand Down
52 changes: 50 additions & 2 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ 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_enter(Vis *vis, Win *win) { }
void vis_lua_win_leave(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_mode_change(Vis *vis) { }

#else

Expand Down Expand Up @@ -742,14 +745,16 @@ static int register_names_iter(lua_State *L) {
* Execute a `:`-command.
* @function command
* @tparam string command the command to execute
* @tparam[opt] Window win the window to execute the command in
* @treturn bool whether the command succeeded
* @usage
* vis:command("set number")
*/
static int command(lua_State *L) {
Vis *vis = obj_ref_check(L, 1, "vis");
const char *cmd = luaL_checkstring(L, 2);
bool ret = vis_cmd(vis, cmd);
Win *win = lua_gettop(L) > 2 ? obj_ref_check(L, 3, VIS_LUA_TYPE_WINDOW) : NULL;
bool ret = vis_win_cmd(vis, win, cmd);
lua_pushboolean(L, ret);
return 1;
}
Expand Down Expand Up @@ -2806,7 +2811,7 @@ void vis_lua_win_open(Vis *vis, Win *win) {

/***
* Window close.
* An window is being closed.
* A window is being closed.
* @function win_close
* @tparam Window win the window being closed
*/
Expand All @@ -2825,6 +2830,38 @@ void vis_lua_win_close(Vis *vis, Win *win) {
lua_pop(L, 1);
}

/***
* Window enter.
* A window is gaining focus.
* @function win_enter
* @tparam Window win the window that gains focus
*/
void vis_lua_win_enter(Vis *vis, Win *win) {
lua_State *L = vis->lua;
vis_lua_event_get(L, "win_enter");
if (lua_isfunction(L, -1)) {
obj_ref_new(L, win, VIS_LUA_TYPE_WINDOW);
pcall(vis, L, 1, 0);
}
lua_pop(L, 1);
}

/***
* Window leave.
* A window is losing focus.
* @function win_leave
* @tparam Window win the window that loses focus
*/
void vis_lua_win_leave(Vis *vis, Win *win) {
lua_State *L = vis->lua;
vis_lua_event_get(L, "win_leave");
if (lua_isfunction(L, -1)) {
obj_ref_new(L, win, VIS_LUA_TYPE_WINDOW);
pcall(vis, L, 1, 0);
}
lua_pop(L, 1);
}

/**
* Window highlight.
* The window has been redrawn and the syntax highlighting needs to be performed.
Expand Down Expand Up @@ -2866,4 +2903,15 @@ void vis_lua_win_status(Vis *vis, Win *win) {
lua_pop(L, 1);
}

/**
* Window mode change.
* The vis mode has changed.
* @function mode_change
* @see Vis.mode
* @see Vis.modes
*/
void vis_lua_mode_change(Vis *vis) {
vis_lua_event_call(vis, "mode_change");
}

#endif
3 changes: 3 additions & 0 deletions vis-lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ 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_enter(Vis*, Win*);
void vis_lua_win_leave(Vis*, Win*);
void vis_lua_win_highlight(Vis*, Win*);
void vis_lua_win_status(Vis*, Win*);
void vis_lua_mode_change(Vis*);

#endif
10 changes: 6 additions & 4 deletions vis-modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,29 @@ void vis_binding_free(Vis *vis, KeyBinding *binding) {
}
}

static Mode *mode_get(Vis *vis, enum VisMode mode) {
Mode *mode_get(Vis *vis, enum VisMode mode) {
if (mode < LENGTH(vis_modes))
return &vis_modes[mode];
return NULL;
}

void mode_set(Vis *vis, Mode *new_mode) {
bool mode_set(Vis *vis, Mode *new_mode) {
if (vis->mode == new_mode)
return;
return false;
if (vis->mode->leave)
vis->mode->leave(vis, new_mode);
if (vis->mode != &vis_modes[VIS_MODE_OPERATOR_PENDING])
vis->mode_prev = vis->mode;
vis->mode = new_mode;
if (new_mode->enter)
new_mode->enter(vis, vis->mode_prev);
return true;
}

void vis_mode_switch(Vis *vis, enum VisMode mode) {
if (mode < LENGTH(vis_modes))
mode_set(vis, &vis_modes[mode]);
if (mode_set(vis, &vis_modes[mode]))
vis_event_emit(vis, VIS_EVENT_MODE_CHANGE);
}

enum VisMode vis_mode_from(Vis *vis, const char *name) {
Expand Down
1 change: 1 addition & 0 deletions vis-prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ void vis_prompt_show(Vis *vis, const char *title) {
Win *active = vis->win;
Win *prompt = window_new_file(vis, title[0] == ':' ? vis->command_file : vis->search_file,
UI_OPTION_ONELINE);
vis_window_focus(prompt);
if (!prompt)
return;
if (vis->mode->visual)
Expand Down