aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-04-03 09:43:45 +0200
committerMarc André Tanner <mat@brain-dump.org>2016-04-03 13:17:32 +0200
commit6f3703737a97c3704d19f43156a8a9a86b495214 (patch)
tree0f04a933226ea1f4edac3f29bdd226a6dd0ba177 /main.c
parent5b018a537d52d90c630deb9fa03d81c71e316ec7 (diff)
downloadvis-6f3703737a97c3704d19f43156a8a9a86b495214.tar.gz
vis-6f3703737a97c3704d19f43156a8a9a86b495214.tar.xz
vis: in visual mode let \ trim selections
Diffstat (limited to 'main.c')
-rw-r--r--main.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/main.c b/main.c
index 6e8e245..b6cf1cb 100644
--- a/main.c
+++ b/main.c
@@ -2,6 +2,7 @@
#include <limits.h>
#include <string.h>
#include <wchar.h>
+#include <ctype.h>
#include <errno.h>
#include "ui-curses.h"
@@ -59,6 +60,8 @@ static const char *cursors_select_next(Vis*, const char *keys, const Arg *arg);
static const char *cursors_select_skip(Vis*, const char *keys, const Arg *arg);
/* rotate selection content count times left (arg->i < 0) or right (arg->i > 0) */
static const char *selections_rotate(Vis*, const char *keys, const Arg *arg);
+/* remove leading and trailing white spaces from selections */
+static const char *selections_trim(Vis*, const char *keys, const Arg *arg);
/* adjust current used count according to keys */
static const char *count(Vis*, const char *keys, const Arg *arg);
/* move to the count-th line or if not given either to the first (arg->i < 0)
@@ -256,6 +259,7 @@ enum {
VIS_ACTION_CURSORS_NEXT,
VIS_ACTION_SELECTIONS_ROTATE_LEFT,
VIS_ACTION_SELECTIONS_ROTATE_RIGHT,
+ VIS_ACTION_SELECTIONS_TRIM,
VIS_ACTION_TEXT_OBJECT_WORD_OUTER,
VIS_ACTION_TEXT_OBJECT_WORD_INNER,
VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER,
@@ -976,6 +980,11 @@ static const KeyAction vis_action[] = {
"Rotate selections right",
selections_rotate, { .i = +1 }
},
+ [VIS_ACTION_SELECTIONS_TRIM] = {
+ "selections-trim",
+ "Remove leading and trailing white space from selections",
+ selections_trim
+ },
[VIS_ACTION_TEXT_OBJECT_WORD_OUTER] = {
"text-object-word-outer",
"A word leading and trailing whitespace included",
@@ -1484,6 +1493,28 @@ static const char *selections_rotate(Vis *vis, const char *keys, const Arg *arg)
return keys;
}
+static const char *selections_trim(Vis *vis, const char *keys, const Arg *arg) {
+ Text *txt = vis_text(vis);
+ View *view = vis_view(vis);
+ for (Cursor *c = view_cursors(view), *next; c; c = next) {
+ next = view_cursors_next(c);
+ Filerange sel = view_cursors_selection_get(c);
+ if (!text_range_valid(&sel))
+ continue;
+ for (char b; sel.start < sel.end && text_byte_get(txt, sel.end-1, &b)
+ && isspace((unsigned char)b); sel.end--);
+ for (char b; sel.start <= sel.end && text_byte_get(txt, sel.start, &b)
+ && isspace((unsigned char)b); sel.start++);
+ if (sel.start < sel.end) {
+ view_cursors_selection_set(c, &sel);
+ view_cursors_selection_sync(c);
+ } else if (!view_cursors_dispose(c)) {
+ vis_mode_switch(vis, VIS_MODE_NORMAL);
+ }
+ }
+ return keys;
+}
+
static const char *replace(Vis *vis, const char *keys, const Arg *arg) {
if (!keys[0])
return NULL;