diff options
| author | Matthias Braun <matze@braunis.de> | 2015-01-04 20:35:21 -0800 |
|---|---|---|
| committer | Matthias Braun <matze@braunis.de> | 2015-01-04 20:49:06 -0800 |
| commit | 1dba77b9704734cae7d1d683ae0f5d88acb3ed9a (patch) | |
| tree | aa757e9dd02419b3500e8831820612478b2b4ba3 | |
| parent | c8d9e1d3d9f947f511e2ab395fae2d2710454c3e (diff) | |
| download | vis-1dba77b9704734cae7d1d683ae0f5d88acb3ed9a.tar.gz vis-1dba77b9704734cae7d1d683ae0f5d88acb3ed9a.tar.xz | |
implement rudimentary autoindent
| -rw-r--r-- | README | 5 | ||||
| -rw-r--r-- | editor.h | 1 | ||||
| -rw-r--r-- | vis.c | 32 |
3 files changed, 34 insertions, 4 deletions
@@ -448,6 +448,11 @@ and their current support in vis. whether typed in tabs should be expanded to tabwidth spaces + autoindent (yes|no) + + replicate spaces and tabs at the beginning of the line when + starting a new line. + number (yes|no) whether line numbers are printed alongside the file content @@ -108,6 +108,7 @@ struct Editor { void (*statusbar)(EditorWin*); /* configurable user hook to draw statusbar */ 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 */ }; Editor *editor_new(int width, int height); @@ -1113,9 +1113,28 @@ static void insert_tab(const Arg *arg) { return insert(&(const Arg){ .s = expand_tab() }); } +static void copy_indent_from_previous_line(Win *win, Text *text) { + size_t pos = window_cursor_get(win); + size_t prev_line = text_line_prev(text, pos); + if (pos == prev_line) + return; + size_t begin = text_line_begin(text, prev_line); + size_t start = text_line_start(text, begin); + size_t len = start-begin; + char *buf = malloc(len+1); + if (!buf) + return; + len = text_bytes_get(text, begin, len, buf); + editor_insert_key(vis, buf, len); + free(buf); +} + static void insert_newline(const Arg *arg) { insert(&(const Arg){ .s = text_newlines_crnl(vis->win->text) ? "\r\n" : "\n" }); + + if (vis->autoindent) + copy_indent_from_previous_line(vis->win->win, vis->win->text); } static void put(const Arg *arg) { @@ -1319,6 +1338,7 @@ static bool cmd_set(Filerange *range, const char *argv[]) { } OptionDef; enum { + OPTION_AUTOINDENT, OPTION_EXPANDTAB, OPTION_TABWIDTH, OPTION_SYNTAX, @@ -1326,10 +1346,11 @@ static bool cmd_set(Filerange *range, const char *argv[]) { }; static OptionDef options[] = { - [OPTION_EXPANDTAB] = { "^(expandtab|et)$", OPTION_TYPE_BOOL }, - [OPTION_TABWIDTH] = { "^(tabwidth|tw)$", OPTION_TYPE_NUMBER }, - [OPTION_SYNTAX] = { "^syntax$", OPTION_TYPE_STRING }, - [OPTION_NUMBER] = { "^(numbers?|nu)$", OPTION_TYPE_BOOL }, + [OPTION_AUTOINDENT] = { "^(autoindent|ai)$", OPTION_TYPE_BOOL }, + [OPTION_EXPANDTAB] = { "^(expandtab|et)$", OPTION_TYPE_BOOL }, + [OPTION_TABWIDTH] = { "^(tabwidth|tw)$", OPTION_TYPE_NUMBER }, + [OPTION_SYNTAX] = { "^syntax$", OPTION_TYPE_STRING }, + [OPTION_NUMBER] = { "^(numbers?|nu)$", OPTION_TYPE_BOOL }, }; static bool init = false; @@ -1396,6 +1417,9 @@ static bool cmd_set(Filerange *range, const char *argv[]) { case OPTION_EXPANDTAB: vis->expandtab = arg.b; break; + case OPTION_AUTOINDENT: + vis->autoindent = arg.b; + break; case OPTION_TABWIDTH: editor_tabwidth_set(vis, arg.i); break; |
