Skip to content

Commit

Permalink
Lua API: access and set all available options
Browse files Browse the repository at this point in the history
the first point of this commit is to allow all options to be read from
lua. this has a number of uses for plugin writers. they are grouped into
a couple of tables depending on what they control:
`vis.options`: table with global configuration
`win.options`: table with window specific configuration

the second point is to allow you to set all these options as if they
were simply lua variables. technically this is already possible by
using `vis:command("set ...")` but personally I think this interface
is cleaner. Note that this already possible for some things like the
current mode (eg. vis.mode = vis.modes.VISUAL). examples:
`vis.options.ai = true`
`vis.options.brk = " !?."`

there are a number of related issues and pull requests:
closes #803: Lua API: let plugins read the values of options
closes #812: Window layout property
supersedes/closes #717: Add ability to access tabwidth from Lua
supersedes/closes #1066: expose UI layout and allow it to be set from lua API
  • Loading branch information
rnpnr committed Jul 30, 2023
1 parent 5d7d62c commit e64523e
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 27 deletions.
5 changes: 5 additions & 0 deletions ui-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,11 @@ static bool ui_init(Ui *ui, Vis *vis) {
return false;
}

enum UiLayout ui_layout_get(Ui *ui) {
UiTerm *tui = (UiTerm *)ui;
return tui->layout;
}

Ui *ui_term_new(void) {
size_t styles_size = UI_STYLE_MAX * sizeof(CellStyle);
CellStyle *styles = calloc(1, styles_size);
Expand Down
1 change: 1 addition & 0 deletions ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ struct UiWin {
};

bool is_default_color(CellColor c);
enum UiLayout ui_layout_get(Ui *ui);

#endif
8 changes: 8 additions & 0 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,10 @@ void view_wrapcolumn_set(View *view, int col) {
view->wrapcolumn = col;
}

int view_wrapcolumn_get(View *view) {
return view->wrapcolumn;
}

bool view_breakat_set(View *view, const char *breakat) {
char *copy = strdup(breakat);
if (!copy)
Expand All @@ -919,6 +923,10 @@ bool view_breakat_set(View *view, const char *breakat) {
return true;
}

const char *view_breakat_get(View *view) {
return view->breakat;
}

size_t view_screenline_goto(View *view, int n) {
size_t pos = view->start;
for (Line *line = view->topline; --n > 0 && line != view->lastline; line = line->next)
Expand Down
2 changes: 2 additions & 0 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,9 @@ enum UiOption view_options_get(View*);
void view_colorcolumn_set(View*, int col);
int view_colorcolumn_get(View*);
void view_wrapcolumn_set(View*, int col);
int view_wrapcolumn_get(View*);
bool view_breakat_set(View*, const char *breakat);
const char *view_breakat_get(View*);

/** Set how many spaces are used to display a tab `\t` character. */
void view_tabwidth_set(View*, int tabwidth);
Expand Down
24 changes: 13 additions & 11 deletions vis-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,24 @@ static void windows_arrange(Vis *vis, enum UiLayout layout) {
vis->ui->arrange(vis->ui, layout);
}

static void tabwidth_set(Vis *vis, int tabwidth) {
void vis_tabwidth_set(Vis *vis, int tabwidth) {
if (tabwidth < 1 || tabwidth > 8)
return;
for (Win *win = vis->windows; win; win = win->next)
view_tabwidth_set(win->view, tabwidth);
vis->tabwidth = tabwidth;
}

void vis_shell_set(Vis *vis, const char *new_shell) {
char *shell = strdup(new_shell);
if (!shell) {
vis_info_show(vis, "Failed to change shell");
} else {
free(vis->shell);
vis->shell = shell;
}
}

/* parse human-readable boolean value in s. If successful, store the result in
* outval and return true. Else return false and leave outval alone. */
static bool parse_bool(const char *s, bool *outval) {
Expand Down Expand Up @@ -241,16 +251,8 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Select

switch (opt_index) {
case OPTION_SHELL:
{
char *shell = strdup(arg.s);
if (!shell) {
vis_info_show(vis, "Failed to change shell");
return false;
}
free(vis->shell);
vis->shell = shell;
vis_shell_set(vis, arg.s);
break;
}
case OPTION_ESCDELAY:
{
TermKey *termkey = vis->ui->termkey_get(vis->ui);
Expand All @@ -264,7 +266,7 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Select
vis->autoindent = toggle ? !vis->autoindent : arg.b;
break;
case OPTION_TABWIDTH:
tabwidth_set(vis, arg.i);
vis_tabwidth_set(vis, arg.i);
break;
case OPTION_SHOW_SPACES:
case OPTION_SHOW_TABS:
Expand Down
Loading

0 comments on commit e64523e

Please sign in to comment.