Skip to content

Commit

Permalink
Add "[{" and "]}" motions to jump to a block's start/end
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrippling committed Apr 12, 2016
1 parent 3939baa commit 255da07
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions config.def.h
Expand Up @@ -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) },
Expand Down
12 changes: 12 additions & 0 deletions main.c
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions text-motions.c
Expand Up @@ -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))
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions text-motions.h
Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions vis-motions.c
Expand Up @@ -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 },
Expand Down
2 changes: 2 additions & 0 deletions vis.h
Expand Up @@ -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,
Expand Down

0 comments on commit 255da07

Please sign in to comment.