Skip to content

Commit

Permalink
vis: deindent blank autoindented lines when leaving insert mode
Browse files Browse the repository at this point in the history
Does not work for the current implementation of `O` because the
"lookbehind" i.e. second to last processed key is `<Up>` and not
`<Enter>`.

Fix martanne#383
  • Loading branch information
martanne committed Jan 28, 2017
1 parent 043a183 commit 007ff80
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1975,14 +1975,14 @@ static const char *openline(Vis *vis, const char *keys, const Arg *arg) {
vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
if (arg->i > 0) {
vis_motion(vis, VIS_MOVE_LINE_END);
vis_keys_feed(vis, "<insert-newline>");
vis_keys_feed(vis, "<Enter>");
} else {
if (vis_get_autoindent(vis)) {
vis_motion(vis, VIS_MOVE_LINE_START);
} else {
vis_motion(vis, VIS_MOVE_LINE_BEGIN);
}
vis_keys_feed(vis, "<insert-newline><Up>");
vis_keys_feed(vis, "<Enter><Up>");
}
return keys;
}
Expand Down
16 changes: 16 additions & 0 deletions vis-modes.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <string.h>
#include <strings.h>
#include "vis-core.h"
#include "text-motions.h"
#include "util.h"

KeyAction *vis_action_new(Vis *vis, const char *name, const char *help, KeyActionFunction *func, Arg arg) {
Expand Down Expand Up @@ -138,6 +139,21 @@ bool vis_window_mode_map(Win *win, enum VisMode id, bool force, const char *key,
static void vis_mode_normal_enter(Vis *vis, Mode *old) {
if (old != mode_get(vis, VIS_MODE_INSERT) && old != mode_get(vis, VIS_MODE_REPLACE))
return;
if (vis->autoindent && strcmp(vis->key_prev, "<Enter>") == 0) {
Text *txt = vis->win->file->text;
for (Cursor *c = view_cursors(vis->win->view); c; c = view_cursors_next(c)) {
size_t pos = view_cursors_pos(c);
size_t start = text_line_start(txt, pos);
if (start == pos) {
size_t begin = text_line_begin(txt, pos);
size_t len = start - begin;
if (len) {
text_delete(txt, begin, len);
view_cursors_to(c, pos-len);
}
}
}
}
macro_operator_stop(vis);
if (!vis->win->parent && vis->action_prev.op == &vis_operators[VIS_OP_MODESWITCH] &&
vis->action_prev.count > 1) {
Expand Down

0 comments on commit 007ff80

Please sign in to comment.