aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Pilling <robpilling@gmail.com>2016-04-12 22:56:29 +0100
committerRob Pilling <robpilling@gmail.com>2016-04-12 22:56:29 +0100
commit255da074c15de15d45eb1582c819d68ee480800d (patch)
tree339ae9529e55a2a71b5c66d79fda08ff3c533c25
parent3939baaac101cea08c711638dfd537f4a039d0ae (diff)
downloadvis-255da074c15de15d45eb1582c819d68ee480800d.tar.gz
vis-255da074c15de15d45eb1582c819d68ee480800d.tar.xz
Add "[{" and "]}" motions to jump to a block's start/end
-rw-r--r--README.md2
-rw-r--r--config.def.h2
-rw-r--r--main.c12
-rw-r--r--text-motions.c26
-rw-r--r--text-motions.h2
-rw-r--r--vis-motions.c2
-rw-r--r--vis.h2
7 files changed, 48 insertions, 0 deletions
diff --git a/README.md b/README.md
index 9d23797..25ade25 100644
--- a/README.md
+++ b/README.md
@@ -137,6 +137,8 @@ Operators can be forced to work line wise by specifying `V`.
N (repeat last search backwards)
n (repeat last search forward)
[] (previous end of C-like function)
+ [{ (previous start of block)
+ ]} (next start of block)
{ (previous paragraph)
( (previous sentence)
[[ (previous start of C-like function)
diff --git a/config.def.h b/config.def.h
index 3441536..ba27fec 100644
--- a/config.def.h
+++ b/config.def.h
@@ -30,6 +30,8 @@ static const KeyBinding bindings_motions[] = {
{ "[]", ACTION(CURSOR_FUNCTION_END_PREV) },
{ "][", ACTION(CURSOR_FUNCTION_START_NEXT) },
{ "[[", ACTION(CURSOR_FUNCTION_START_PREV) },
+ { "[{", ACTION(CURSOR_BLOCK_START) },
+ { "]}", ACTION(CURSOR_BLOCK_END) },
{ "$", ACTION(CURSOR_LINE_LASTCHAR) },
{ "^", ACTION(CURSOR_LINE_START) },
{ "}", ACTION(CURSOR_PARAGRAPH_NEXT) },
diff --git a/main.c b/main.c
index fe3fcf4..c3437c7 100644
--- a/main.c
+++ b/main.c
@@ -159,6 +159,8 @@ enum {
VIS_ACTION_CURSOR_FUNCTION_END_PREV,
VIS_ACTION_CURSOR_FUNCTION_START_NEXT,
VIS_ACTION_CURSOR_FUNCTION_END_NEXT,
+ VIS_ACTION_CURSOR_BLOCK_START,
+ VIS_ACTION_CURSOR_BLOCK_END,
VIS_ACTION_CURSOR_COLUMN,
VIS_ACTION_CURSOR_LINE_FIRST,
VIS_ACTION_CURSOR_LINE_LAST,
@@ -465,6 +467,16 @@ static const KeyAction vis_action[] = {
"Move cursor forwards to end of function",
movement, { .i = VIS_MOVE_FUNCTION_END_NEXT }
},
+ [VIS_ACTION_CURSOR_BLOCK_START] = {
+ "cursor-block-start",
+ "Move cursor to the opening curly brace in a block",
+ movement, { .i = VIS_MOVE_BLOCK_START }
+ },
+ [VIS_ACTION_CURSOR_BLOCK_END] = {
+ "cursor-block-end",
+ "Move cursor to the closing curly brace in a block",
+ movement, { .i = VIS_MOVE_BLOCK_END }
+ },
[VIS_ACTION_CURSOR_COLUMN] = {
"cursor-column",
"Move cursor to given column of current line",
diff --git a/text-motions.c b/text-motions.c
index 0ad8e81..e948fae 100644
--- a/text-motions.c
+++ b/text-motions.c
@@ -6,6 +6,7 @@
#include "text-motions.h"
#include "text-util.h"
#include "util.h"
+#include "text-objects.h"
#define space(c) (isspace((unsigned char)c))
#define boundary(c) (isboundary((unsigned char)c))
@@ -586,6 +587,31 @@ size_t text_function_end_prev(Text *txt, size_t pos) {
return text_function_end_direction(txt, pos, -1);
}
+static size_t text_paren_start_end(Text *txt, size_t pos, int direction, Filerange (*rangefn)(Text *, size_t)) {
+ /* don't select (as a text obj) a bracket we're currently on */
+ size_t offbracketpos = pos + direction;
+
+ Filerange r = rangefn(txt, offbracketpos);
+ if (!text_range_valid(&r))
+ return pos;
+
+ /* we want the outer text object */
+ r.start += direction;
+ r.end -= direction;
+ if (!text_range_valid(&r))
+ return pos;
+
+ return direction < 0 ? r.start : r.end;
+}
+
+size_t text_block_start(Text *txt, size_t pos) {
+ return text_paren_start_end(txt, pos, -1, text_object_curly_bracket);
+}
+
+size_t text_block_end(Text *txt, size_t pos) {
+ return text_paren_start_end(txt, pos, +1, text_object_curly_bracket);
+}
+
size_t text_bracket_match(Text *txt, size_t pos) {
return text_bracket_match_symbol(txt, pos, NULL);
}
diff --git a/text-motions.h b/text-motions.h
index e601073..6066168 100644
--- a/text-motions.h
+++ b/text-motions.h
@@ -112,6 +112,8 @@ size_t text_function_end_prev(Text*, size_t pos);
size_t text_section_next(Text*, size_t pos);
size_t text_section_prev(Text*, size_t pos);
*/
+size_t text_block_start(Text*, size_t pos);
+size_t text_block_end(Text*, size_t pos);
/* search coresponding '(', ')', '{', '}', '[', ']', '>', '<', '"', ''' */
size_t text_bracket_match(Text*, size_t pos);
/* same as above but explicitly specify symbols to match */
diff --git a/vis-motions.c b/vis-motions.c
index b42f153..b89f94c 100644
--- a/vis-motions.c
+++ b/vis-motions.c
@@ -366,6 +366,8 @@ const Movement vis_motions[] = {
[VIS_MOVE_FUNCTION_START_NEXT] = { .txt = text_function_start_next, .type = LINEWISE|JUMP },
[VIS_MOVE_FUNCTION_END_PREV] = { .txt = text_function_end_prev, .type = LINEWISE|JUMP },
[VIS_MOVE_FUNCTION_END_NEXT] = { .txt = text_function_end_next, .type = LINEWISE|JUMP },
+ [VIS_MOVE_BLOCK_START] = { .txt = text_block_start, .type = JUMP },
+ [VIS_MOVE_BLOCK_END] = { .txt = text_block_end, .type = JUMP },
[VIS_MOVE_BRACKET_MATCH] = { .txt = bracket_match, .type = INCLUSIVE|JUMP },
[VIS_MOVE_FILE_BEGIN] = { .txt = text_begin, .type = LINEWISE|JUMP },
[VIS_MOVE_FILE_END] = { .txt = text_end, .type = LINEWISE|JUMP },
diff --git a/vis.h b/vis.h
index 5e7f338..830aafc 100644
--- a/vis.h
+++ b/vis.h
@@ -219,6 +219,8 @@ enum VisMotion {
VIS_MOVE_FUNCTION_START_NEXT,
VIS_MOVE_FUNCTION_END_PREV,
VIS_MOVE_FUNCTION_END_NEXT,
+ VIS_MOVE_BLOCK_START,
+ VIS_MOVE_BLOCK_END,
VIS_MOVE_BRACKET_MATCH,
VIS_MOVE_LEFT_TO,
VIS_MOVE_RIGHT_TO,