Skip to content

Commit

Permalink
lua: allow changing the displayed file of a window
Browse files Browse the repository at this point in the history
Change the file displayed in a window by writing the new file name
to the window's file member.
  • Loading branch information
fischerling committed May 7, 2024
1 parent 0b65657 commit 68223ca
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,9 @@ static int window_newindex(lua_State *L) {
}
lua_pop(L, 1);
return ret;
} else if (strcmp(key, "file") == 0 && lua_isstring(L, 3)) {
const char* filename = lua_tostring(L, 3);
vis_window_change_file(win, filename);
}
}

Expand Down
11 changes: 11 additions & 0 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,17 @@ bool vis_window_reload(Win *win) {
return true;
}

bool vis_window_change_file(Win *win, const char* filename) {
File *file = file_new(win->vis, filename);
if (!file)
return false;
file->refcount++;
if (win->file)
file_free(win->vis, win->file);
win->file = file;
view_reload(win->view, file->text);
}

bool vis_window_split(Win *original) {
vis_doupdates(original->vis, false);
Win *win = window_new_file(original->vis, original->file, UI_OPTION_STATUSBAR);
Expand Down
2 changes: 2 additions & 0 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ bool vis_window_new(Vis*, const char *filename);
bool vis_window_new_fd(Vis*, int fd);
/** Reload the file currently displayed in the window from disk. */
bool vis_window_reload(Win*);
/** Change the file currently displayed in the window. */
bool vis_window_change_file(Win*, const char *filename);
/** Check whether closing the window would loose unsaved changes. */
bool vis_window_closable(Win*);
/** Close window, redraw user interface. */
Expand Down

0 comments on commit 68223ca

Please sign in to comment.