Skip to content

Commit 81a4490

Browse files
author
Rob Pilling
committed
Added visual inside (vi<bracket>)
1 parent 20fc986 commit 81a4490

File tree

6 files changed

+98
-3
lines changed

6 files changed

+98
-3
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Micro Vi
22

33
Similar commands to vi.
4+
5+
See uvi.1

gui/base.c

+69-3
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,66 @@ static void colon(const char *initial)
764764
free(in);
765765
}
766766

767+
void visual_inside(int include)
768+
{
769+
struct list *l;
770+
char *s;
771+
int i;
772+
int start_y;
773+
int bracket;
774+
775+
/* TODO: number */
776+
777+
bracket = gui_getch(GETCH_COOKED);
778+
l = buffer_getindex(buffers_current(), gui_y());
779+
s = l->data;
780+
781+
if(!isparen(bracket))
782+
/* TODO: iw, ip (paragraph), etc - swap to a motion? */
783+
return;
784+
785+
/* TODO: go forward if it's a close bracket, else go backward */
786+
787+
i = gui_x();
788+
start_y = gui_y();
789+
while(s[i] != bracket){
790+
if(--i < 0){
791+
l = l->prev;
792+
start_y--;
793+
if(!l)
794+
return;
795+
s = l->data;
796+
i = strlen(s) - 1;
797+
}
798+
}
799+
800+
/* s[i] is the bracket, do a motion_percent on it and visual() */
801+
{
802+
/* TODO: distinguish between vi{ and va{ */
803+
struct motion m;
804+
struct bufferpos bp;
805+
struct screeninfo si;
806+
int x[2], y[2];
807+
808+
memset(&m, 0, sizeof m);
809+
m.motion = MOTION_PAREN_MATCH;
810+
811+
si.top = gui_top();
812+
si.height = gui_max_y();
813+
814+
x[0] = x[1] = i;
815+
y[0] = y[1] = start_y;
816+
bp.x = x + 1;
817+
bp.y = y + 1;
818+
819+
if(motion_apply(&m, &bp, &si))
820+
return;
821+
822+
/* highlight from {x,y][1] to {x,y}[0] */
823+
visual_setpoints(x, y);
824+
}
825+
}
826+
767827
static int is_edit_char(int c)
768828
{
769829
switch(c){
@@ -994,9 +1054,15 @@ void gui_run()
9941054
flag = 1;
9951055
case 'i':
9961056
case_i:
997-
insert(flag, 0, 0);
998-
buffer_changed = 1;
999-
SET_DOT();
1057+
if(visual_get() == VISUAL_NONE){
1058+
insert(flag, 0, 0);
1059+
buffer_changed = 1;
1060+
SET_DOT();
1061+
}else{
1062+
/* ibracket */
1063+
visual_inside(flag);
1064+
view_changed = 1;
1065+
}
10001066
break;
10011067
case 'I':
10021068
SET_MOTION(MOTION_LINE_START);

gui/visual.c

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ static enum visual visual_state = VISUAL_NONE;
1010
static struct range visual_cursor;
1111
static struct range visual_anchor;
1212

13+
void visual_setpoints(int x[2], int y[2])
14+
{
15+
visual_cursor.start = y[0];
16+
visual_cursor.end = x[0];
17+
visual_anchor.start = y[1];
18+
visual_anchor.end = x[1];
19+
gui_move(visual_cursor.start, visual_cursor.end);
20+
}
21+
1322
void visual_update(struct range *v)
1423
{
1524
v->start = gui_y();

gui/visual.h

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ enum visual visual_get();
1313
void visual_swap();
1414
void visual_join();
1515

16+
void visual_setpoints(int x[2], int y[2]);
17+
1618
const struct range *visual_get_start();
1719
const struct range *visual_get_end();
1820
const struct range *visual_get_col_start();

util/str.c

+14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ int isfnamechar(int c)
2424
return !isspace(c) && !ispunct(c) && c != '"' && c != '\''; /* TODO: ignore trailing punctuation */
2525
}
2626

27+
int isparen(int c)
28+
{
29+
switch(c){
30+
case '{':
31+
case '(':
32+
case '[':
33+
case '}':
34+
case ')':
35+
case ']':
36+
return 1;
37+
}
38+
return 0;
39+
}
40+
2741
char *chars_at(const char *line, int x, int (*cmp)(int))
2842
{
2943
const char *wordstart, *wordend;

util/str.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ char **words_begin(struct list *, const char *);
1010

1111
int line_isspace(const char *s);
1212

13+
int isparen(int);
14+
1315
void str_escape(char *arg);
1416
char *chr_expand(char *arg, char c, const char *rep);
1517
char *str_expand(char *str, const char *grow_from, const char *grow_to);

0 commit comments

Comments
 (0)