Skip to content

Commit

Permalink
Make expandtab option window-local
Browse files Browse the repository at this point in the history
  • Loading branch information
YerinAlexey authored and rnpnr committed Aug 27, 2023
1 parent 450dc2d commit de315f8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ static const OptionDef options[] = {
},
[OPTION_EXPANDTAB] = {
{ "expandtab", "et" },
VIS_OPTION_TYPE_BOOL,
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Replace entered <Tab> with `tabwidth` spaces")
},
[OPTION_TABWIDTH] = {
Expand Down
2 changes: 1 addition & 1 deletion vis-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Select
break;
}
case OPTION_EXPANDTAB:
vis->expandtab = toggle ? !vis->expandtab : arg.b;
vis->win->expandtab = toggle ? !vis->win->expandtab : arg.b;
break;
case OPTION_AUTOINDENT:
vis->autoindent = toggle ? !vis->autoindent : arg.b;
Expand Down
2 changes: 1 addition & 1 deletion vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ struct Win {
UiWin *ui; /* ui object handling visual appearance of this window */
File *file; /* file being displayed in this window */
View *view; /* currently displayed part of underlying text */
bool expandtab; /* whether typed tabs should be converted to spaces in this window*/
MarkList jumplist; /* LRU jump management */
Array saved_selections; /* register used to store selections */
Mode modes[VIS_MODE_INVALID]; /* overlay mods used for per window key bindings */
Expand All @@ -183,7 +184,6 @@ struct Vis {
char search_char[8]; /* last used character to search for via 'f', 'F', 't', 'T' */
int last_totill; /* last to/till movement used for ';' and ',' */
int search_direction; /* used for `n` and `N` */
bool expandtab; /* whether typed tabs should be converted to spaces */
bool autoindent; /* whether indentation should be copied from previous line on newline */
bool change_colors; /* whether to adjust 256 color palette for true colors */
char *shell; /* shell used to launch external commands */
Expand Down
20 changes: 10 additions & 10 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -1535,8 +1535,6 @@ static int vis_options_assign(Vis *vis, lua_State *L, const char *key, int next)
} else if (strcmp(key, "escdelay") == 0) {
TermKey *tk = vis->ui->termkey_get(vis->ui);
termkey_set_waittime(tk, luaL_checkint(L, next));
} else if (strcmp(key, "expandtab") == 0 || strcmp(key, "et") == 0) {
vis->expandtab = lua_toboolean(L, next);
} else if (strcmp(key, "ignorecase") == 0 || strcmp(key, "ic") == 0) {
vis->ignorecase = lua_toboolean(L, next);
} else if (strcmp(key, "loadmethod") == 0) {
Expand Down Expand Up @@ -1657,7 +1655,6 @@ static const struct luaL_Reg vis_lua[] = {
* @tfield[opt=false] boolean autoindent {ai}
* @tfield[opt=false] boolean changecolors
* @tfield[opt=50] int escdelay
* @tfield[opt=false] boolean expandtab {et}
* @tfield[opt=false] boolean ignorecase {ic}
* @tfield[opt="auto"] string loadmethod `"auto"`, `"read"`, or `"mmap"`.
* @tfield[opt="/bin/sh"] string shell
Expand All @@ -1679,9 +1676,6 @@ static int vis_options_index(lua_State *L) {
TermKey *tk = vis->ui->termkey_get(vis->ui);
lua_pushunsigned(L, termkey_get_waittime(tk));
return 1;
} else if (strcmp(key, "expandtab") == 0 || strcmp(key, "et") == 0) {
lua_pushboolean(L, vis->expandtab);
return 1;
} else if (strcmp(key, "ignorecase") == 0 || strcmp(key, "ic") == 0) {
lua_pushboolean(L, vis->ignorecase);
return 1;
Expand Down Expand Up @@ -1967,6 +1961,8 @@ static int window_options_assign(Win *win, lua_State *L, const char *key, int ne
view_wrapcolumn_set(win->view, luaL_checkint(L, next));
} else if (strcmp(key, "tabwidth") == 0 || strcmp(key, "tw") == 0) {
view_tabwidth_set(win->view, luaL_checkint(L, next));
} else if (strcmp(key, "expandtab") == 0 || strcmp(key, "et") == 0) {
win->expandtab = lua_toboolean(L, next);
}
return 0;
}
Expand Down Expand Up @@ -2169,14 +2165,15 @@ static const struct luaL_Reg window_funcs[] = {
* @tfield[opt=""] string breakat {brk}
* @tfield[opt=0] int colorcolumn {cc}
* @tfield[opt=false] boolean cursorline {cul}
* @tfield[opt=false] boolean expandtab {et}
* @tfield[opt=false] boolean numbers {nu}
* @tfield[opt=false] boolean relativenumbers {rnu}
* @tfield[opt=true] boolean showeof
* @tfield[opt=false] boolean shownewlines
* @tfield[opt=false] boolean showspaces
* @tfield[opt=false] boolean showtabs
* @tfield[opt=0] int wrapcolumn {wc}
* @tfield[opt=8] int tabwidth {tw}
* @tfield[opt=0] int wrapcolumn {wc}
*/

static int window_options_index(lua_State *L) {
Expand All @@ -2194,6 +2191,9 @@ static int window_options_index(lua_State *L) {
} else if (strcmp(key, "cursorline") == 0 || strcmp(key, "cul") == 0) {
lua_pushboolean(L, view_options_get(win->view) & UI_OPTION_CURSOR_LINE);
return 1;
} else if (strcmp(key, "expandtab") == 0 || strcmp(key, "et") == 0) {
lua_pushboolean(L, win->expandtab);
return 1;
} else if (strcmp(key, "numbers") == 0 || strcmp(key, "nu") == 0) {
lua_pushboolean(L, view_options_get(win->view) & UI_OPTION_LINE_NUMBERS_ABSOLUTE);
return 1;
Expand All @@ -2212,12 +2212,12 @@ static int window_options_index(lua_State *L) {
} else if (strcmp(key, "showtabs") == 0) {
lua_pushboolean(L, view_options_get(win->view) & UI_OPTION_SYMBOL_TAB);
return 1;
} else if (strcmp(key, "wrapcolumn") == 0 || strcmp(key, "wc") == 0) {
lua_pushunsigned(L, view_wrapcolumn_get(win->view));
return 1;
} else if (strcmp(key, "tabwidth") == 0 || strcmp(key, "tw") == 0) {
lua_pushinteger(L, view_tabwidth_get(win->view));
return 1;
} else if (strcmp(key, "wrapcolumn") == 0 || strcmp(key, "wc") == 0) {
lua_pushunsigned(L, view_wrapcolumn_get(win->view));
return 1;
}
}
return index_common(L);
Expand Down
2 changes: 1 addition & 1 deletion vis-operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static size_t op_put(Vis *vis, Text *txt, OperatorContext *c) {
static size_t op_shift_right(Vis *vis, Text *txt, OperatorContext *c) {
char spaces[9] = " ";
spaces[MIN(view_tabwidth_get(vis->win->view), LENGTH(spaces) - 1)] = '\0';
const char *tab = vis->expandtab ? spaces : "\t";
const char *tab = vis->win->expandtab ? spaces : "\t";
size_t tablen = strlen(tab);
size_t pos = text_line_begin(txt, c->range.end), prev_pos;
size_t newpos = c->pos;
Expand Down
4 changes: 2 additions & 2 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ Win *window_new_file(Vis *vis, File *file, enum UiOption options) {
win->vis = vis;
win->file = file;
win->view = view_new(file->text);
win->expandtab = false;
win->ui = vis->ui->window_new(vis->ui, win, options);
if (!win->view || !win->ui) {
window_free(win);
Expand Down Expand Up @@ -694,7 +695,6 @@ Vis *vis_new(Ui *ui, VisEvent *event) {
return NULL;
vis->exit_status = -1;
vis->ui = ui;
vis->expandtab = false;
vis->change_colors = true;
for (size_t i = 0; i < LENGTH(vis->registers); i++)
register_init(&vis->registers[i]);
Expand Down Expand Up @@ -1644,7 +1644,7 @@ void vis_insert_tab(Vis *vis) {
Win *win = vis->win;
if (!win)
return;
if (!vis->expandtab) {
if (!win->expandtab) {
vis_insert_key(vis, "\t", 1);
return;
}
Expand Down

0 comments on commit de315f8

Please sign in to comment.