aboutsummaryrefslogtreecommitdiff
path: root/vis.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2014-09-19 11:27:55 +0200
committerMarc André Tanner <mat@brain-dump.org>2014-09-19 11:27:55 +0200
commitf212b9d4952c87052e23b5161195917410b7c911 (patch)
tree238e6fdefac447230b556779870dde4baa466247 /vis.c
parentdef913f428365d048ce30d71117421acbf379a75 (diff)
downloadvis-f212b9d4952c87052e23b5161195917410b7c911.tar.gz
vis-f212b9d4952c87052e23b5161195917410b7c911.tar.xz
Implement expand tab functionality, make tabwidth configurable
If expandtab is enabled then inserted tabs are replaced by tabwidth amount of spaces. Both settings apply to all windows files and can be changed via: :set tabwidth n # where 1 <= n <= 8 :set expandtab (1|yes|true)|(0|no|false)
Diffstat (limited to 'vis.c')
-rw-r--r--vis.c42
1 files changed, 39 insertions, 3 deletions
diff --git a/vis.c b/vis.c
index dfe02fa..9ca39ab 100644
--- a/vis.c
+++ b/vis.c
@@ -410,6 +410,8 @@ static void call(const Arg *arg);
static void quit(const Arg *arg);
/** commands to enter at the ':'-prompt */
+/* set various runtime options */
+static bool cmd_set(const char *argv[]);
/* goto line indicated by argv[0] */
static bool cmd_gotoline(const char *argv[]);
/* for each argument create a new window and open the corresponding file */
@@ -473,7 +475,13 @@ static void op_put(OperatorContext *c) {
}
static const char *expand_tab(void) {
- return "\t";
+ static char spaces[9];
+ int tabwidth = editor_tabwidth_get(vis);
+ tabwidth = MIN(tabwidth, LENGTH(spaces) - 1);
+ for (int i = 0; i < tabwidth; i++)
+ spaces[i] = ' ';
+ spaces[tabwidth] = '\0';
+ return vis->expandtab ? spaces : "\t";
}
static void op_shift_right(OperatorContext *c) {
@@ -497,7 +505,7 @@ static void op_shift_right(OperatorContext *c) {
static void op_shift_left(OperatorContext *c) {
Text *txt = vis->win->text;
size_t pos = text_line_begin(txt, c->range.end), prev_pos;
- size_t tabwidth = 8; // TODO: make configurable
+ size_t tabwidth = editor_tabwidth_get(vis);
/* if range ends at the begin of a line, skip line break */
if (pos == c->range.end)
@@ -836,7 +844,7 @@ static void insert(const Arg *arg) {
}
static void insert_tab(const Arg *arg) {
- insert(&(const Arg){ .s = "\t" });
+ return insert(&(const Arg){ .s = expand_tab() });
}
static void insert_newline(const Arg *arg) {
@@ -985,6 +993,34 @@ static void switchmode_to(Mode *new_mode) {
/** ':'-command implementations */
+static bool cmd_set(const char *argv[]) {
+ if (!argv[2]) {
+ editor_info_show(vis, "Expecting: set option value");
+ return false;
+ }
+
+ if (!strcmp("tabwidth", argv[1])) {
+ editor_tabwidth_set(vis, strtoul(argv[2], NULL, 10));
+ } else if (!strcmp("expandtab", argv[1])) {
+ switch (argv[2][0]) {
+ case '1': case 't': /* true */ case 'y': /* yes */
+ vis->expandtab = true;
+ break;
+ case '0': case 'f': /* false */ case 'n': /* no */
+ vis->expandtab = false;
+ break;
+ default:
+ editor_info_show(vis, "Expecting: set expandtab [0|1]");
+ return false;
+ }
+ } else {
+ editor_info_show(vis, "Unknown option: `%s'", argv[1]);
+ return false;
+ }
+
+ return true;
+}
+
static bool cmd_gotoline(const char *argv[]) {
action.count = strtoul(argv[0], NULL, 10);
movement(&(const Arg){ .i = action.count <= 1 ? MOVE_FILE_BEGIN : MOVE_LINE });