aboutsummaryrefslogtreecommitdiff
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
parent5b018a537d52d90c630deb9fa03d81c71e316ec7 (diff)
downloadvis-6f3703737a97c3704d19f43156a8a9a86b495214.tar.gz
vis-6f3703737a97c3704d19f43156a8a9a86b495214.tar.xz
vis: in visual mode let \ trim selections
-rw-r--r--README.md1
-rw-r--r--config.def.h1
-rw-r--r--main.c31
3 files changed, 33 insertions, 0 deletions
diff --git a/README.md b/README.md
index c8b0ddb..86f01c9 100644
--- a/README.md
+++ b/README.md
@@ -223,6 +223,7 @@ Operators can be forced to work line wise by specifying `V`.
Ctrl-D make the count next cursor primary
+ rotates selections rightwards count times
- rotates selections leftwards count times
+ \ trim selections, remove leading and trailing white space
Esc clear all selections, switch to normal mode
In insert/replace mode
diff --git a/config.def.h b/config.def.h
index 67e4c73..3a09da2 100644
--- a/config.def.h
+++ b/config.def.h
@@ -284,6 +284,7 @@ static const KeyBinding bindings_visual[] = {
{ "<S-Tab>", ACTION(CURSORS_ALIGN_INDENT_RIGHT) },
{ "-", ACTION(SELECTIONS_ROTATE_LEFT) },
{ "+", ACTION(SELECTIONS_ROTATE_RIGHT) },
+ { "\\", ACTION(SELECTIONS_TRIM) },
{ 0 /* empty last element, array terminator */ },
};
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;