Skip to content

Commit

Permalink
Make tabwidth 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 ae450f7 commit 450dc2d
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ static const OptionDef options[] = {
},
[OPTION_TABWIDTH] = {
{ "tabwidth", "tw" },
VIS_OPTION_TYPE_NUMBER,
VIS_OPTION_TYPE_NUMBER|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Number of spaces to display (and insert if `expandtab` is enabled) for a tab")
},
[OPTION_SHOW_SPACES] = {
Expand Down
6 changes: 6 additions & 0 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,16 @@ static void selection_free(Selection*);
static size_t cursor_set(Selection*, Line *line, int col);

void view_tabwidth_set(View *view, int tabwidth) {
if (tabwidth < 1 || tabwidth > 8)
return;
view->tabwidth = tabwidth;
view_draw(view);
}

int view_tabwidth_get(View *view) {
return view->tabwidth;
}

/* reset internal view data structures (cell matrix, line offsets etc.) */
static void view_clear(View *view) {
memset(view->lines, 0, view->lines_size);
Expand Down
2 changes: 2 additions & 0 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ 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);
/** Get how many spaces are used to display a tab `\t` character. */
int view_tabwidth_get(View*);
/** Define a display style. */
bool view_style_define(View*, enum UiStyle, const char *style);
/** Apply a style to a text range. */
Expand Down
10 changes: 1 addition & 9 deletions vis-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,6 @@ static void windows_arrange(Vis *vis, enum UiLayout layout) {
vis->ui->arrange(vis->ui, layout);
}

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) {
Expand Down Expand Up @@ -269,7 +261,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:
vis_tabwidth_set(vis, arg.i);
view_tabwidth_set(vis->win->view, arg.i);
break;
case OPTION_SHOW_SPACES:
case OPTION_SHOW_TABS:
Expand Down
1 change: 0 additions & 1 deletion vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,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` */
int tabwidth; /* how many spaces should be used to display a tab */
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 */
Expand Down
12 changes: 6 additions & 6 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -1553,8 +1553,6 @@ static int vis_options_assign(Vis *vis, lua_State *L, const char *key, int next)
if (!lua_isstring(L, next))
return newindex_common(L);
vis_shell_set(vis, lua_tostring(L, next));
} else if (strcmp(key, "tabwidth") == 0 || strcmp(key, "tw") == 0) {
vis_tabwidth_set(vis, luaL_checkint(L, next));
}
return 0;
}
Expand Down Expand Up @@ -1663,7 +1661,6 @@ static const struct luaL_Reg vis_lua[] = {
* @tfield[opt=false] boolean ignorecase {ic}
* @tfield[opt="auto"] string loadmethod `"auto"`, `"read"`, or `"mmap"`.
* @tfield[opt="/bin/sh"] string shell
* @tfield[opt=8] int tabwidth {tw}
*/

static int vis_options_index(lua_State *L) {
Expand Down Expand Up @@ -1704,9 +1701,6 @@ static int vis_options_index(lua_State *L) {
} else if (strcmp(key, "shell") == 0) {
lua_pushstring(L, vis->shell);
return 1;
} else if (strcmp(key, "tabwidth") == 0 || strcmp(key, "tw") == 0) {
lua_pushinteger(L, vis->tabwidth);
return 1;
}
}
return index_common(L);
Expand Down Expand Up @@ -1971,6 +1965,8 @@ static int window_options_assign(Win *win, lua_State *L, const char *key, int ne
view_options_set(win->view, flags);
} else if (strcmp(key, "wrapcolumn") == 0 || strcmp(key, "wc") == 0) {
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));
}
return 0;
}
Expand Down Expand Up @@ -2180,6 +2176,7 @@ static const struct luaL_Reg window_funcs[] = {
* @tfield[opt=false] boolean showspaces
* @tfield[opt=false] boolean showtabs
* @tfield[opt=0] int wrapcolumn {wc}
* @tfield[opt=8] int tabwidth {tw}
*/

static int window_options_index(lua_State *L) {
Expand Down Expand Up @@ -2218,6 +2215,9 @@ static int window_options_index(lua_State *L) {
} 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;
}
}
return index_common(L);
Expand Down
4 changes: 2 additions & 2 deletions vis-operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,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(vis->tabwidth, LENGTH(spaces) - 1)] = '\0';
spaces[MIN(view_tabwidth_get(vis->win->view), LENGTH(spaces) - 1)] = '\0';
const char *tab = vis->expandtab ? spaces : "\t";
size_t tablen = strlen(tab);
size_t pos = text_line_begin(txt, c->range.end), prev_pos;
Expand All @@ -127,7 +127,7 @@ static size_t op_shift_right(Vis *vis, Text *txt, OperatorContext *c) {

static size_t op_shift_left(Vis *vis, Text *txt, OperatorContext *c) {
size_t pos = text_line_begin(txt, c->range.end), prev_pos;
size_t tabwidth = vis->tabwidth, tablen;
size_t tabwidth = view_tabwidth_get(vis->win->view), tablen;
size_t newpos = c->pos;

/* if range ends at the begin of a line, skip line break */
Expand Down
4 changes: 1 addition & 3 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@ Win *window_new_file(Vis *vis, File *file, enum UiOption options) {
mark_init(&win->saved_selections);
file->refcount++;
view_options_set(win->view, view_options_get(win->view));
view_tabwidth_set(win->view, vis->tabwidth);

if (vis->windows)
vis->windows->prev = win;
Expand Down Expand Up @@ -695,7 +694,6 @@ Vis *vis_new(Ui *ui, VisEvent *event) {
return NULL;
vis->exit_status = -1;
vis->ui = ui;
vis->tabwidth = 8;
vis->expandtab = false;
vis->change_colors = true;
for (size_t i = 0; i < LENGTH(vis->registers); i++)
Expand Down Expand Up @@ -1651,7 +1649,7 @@ void vis_insert_tab(Vis *vis) {
return;
}
char spaces[9];
int tabwidth = MIN(vis->tabwidth, LENGTH(spaces) - 1);
int tabwidth = MIN(view_tabwidth_get(vis->win->view), LENGTH(spaces) - 1);
for (Selection *s = view_selections(win->view); s; s = view_selections_next(s)) {
size_t pos = view_cursors_pos(s);
int width = text_line_width_get(win->file->text, pos);
Expand Down

0 comments on commit 450dc2d

Please sign in to comment.