Skip to content

Commit 72f7f90

Browse files
committed
Add match-bracket widget that takes a position and/or parameter to store result, and does no weird vi stuff
I use this in my zle-line-pre-redraw hook, local -a hackcol=(red cyan) local mpos cpos off for off in 0 1; do (( cpos = CURSOR - off )) if (( cpos >= 0 )) && zle .match-bracket $cpos mpos; then region_highlight+=("$((cpos)) $((cpos+1)) bold,bg=${hackcol[2]},fg=black" "$((mpos)) $((mpos+1)) bold,bg=${hackcol[1]},fg=black") break fi done
1 parent 8ab150a commit 72f7f90

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

Src/Zle/iwidgets.list

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"list-choices", listchoices, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_LASTCOL | ZLE_ISCOMP
8484
"list-expand", listexpand, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_LASTCOL
8585
"magic-space", magicspace, ZLE_KEEPSUFFIX | ZLE_MENUCMP
86+
"match-bracket", matchbracket, ZLE_KEEPSUFFIX | ZLE_MENUCMP
8687
"menu-complete", menucomplete, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_ISCOMP
8788
"menu-expand-or-complete", menuexpandorcomplete, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_ISCOMP
8889
"neg-argument", negargument, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_LASTCOL | ZLE_NOTCOMMAND

Src/Zle/zle_move.c

+74
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,80 @@ vigotocolumn(UNUSED(char **args))
589589
return 0;
590590
}
591591

592+
/**/
593+
int
594+
matchbracket(char **args)
595+
{
596+
int ocs = zlecs, dir, ct;
597+
unsigned char oth, me;
598+
599+
if (*args) {
600+
char *end = NULL;
601+
zlecs = zstrtol(*args, &end, 10);
602+
if (end && end != *args && *end == '\0')
603+
args++;
604+
else
605+
zlecs = ocs;
606+
}
607+
608+
if (zlecs == zlell || zleline[zlecs] == '\n') {
609+
zlecs = ocs;
610+
return 1;
611+
}
612+
switch (me = zleline[zlecs]) {
613+
case '{':
614+
dir = 1;
615+
oth = '}';
616+
break;
617+
case /*{*/ '}':
618+
dir = -1;
619+
oth = '{'; /*}*/
620+
break;
621+
case '(':
622+
dir = 1;
623+
oth = ')';
624+
break;
625+
case ')':
626+
dir = -1;
627+
oth = '(';
628+
break;
629+
case '[':
630+
dir = 1;
631+
oth = ']';
632+
break;
633+
case ']':
634+
dir = -1;
635+
oth = '[';
636+
break;
637+
default:
638+
zlecs = ocs;
639+
return 1;
640+
}
641+
ct = 1;
642+
while (zlecs >= 0 && zlecs < zlell && ct) {
643+
if (dir < 0)
644+
DECCS();
645+
else
646+
INCCS();
647+
if (zleline[zlecs] == oth)
648+
ct--;
649+
else if (zleline[zlecs] == me)
650+
ct++;
651+
}
652+
if (zlecs < 0 || zlecs >= zlell) {
653+
zlecs = ocs;
654+
return 1;
655+
}
656+
657+
if (*args) {
658+
char digs[100];
659+
sprintf(digs, "%d", zlecs);
660+
zlecs = ocs;
661+
setsparam(*args, ztrdup(digs));
662+
}
663+
return 0;
664+
}
665+
592666
/**/
593667
int
594668
vimatchbracket(UNUSED(char **args))

0 commit comments

Comments
 (0)