-
Notifications
You must be signed in to change notification settings - Fork 4
/
surround.kak
239 lines (223 loc) · 6.66 KB
/
surround.kak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
define-command -hidden -params 2 _surround %{ execute-keys "i%arg{1}<esc>a%arg{2}<esc>" }
define-command surround %{
_surrounder-info 'surround'
on-key %{ eval %sh{
if [ $kak_key = t ] ; then
echo surround-with-tag
else
#use $val{key}. if use $kak_key, it break quote case
echo '_impl-surround _surround %val{key}'
fi
}}
}
define-command delete-surround %!
_surrounding-object-info 'delete surround'
on-key %@ eval %sh^
case $kak_key in
b|'('|')'|B|'{'|'}'|r|'['|']'|a|'<lt>'|'<gt>'|'"'|Q|"'"|q|'`'|g)
#use $val{key}. if use $kak_key, it break quote case
echo '_select-surrounding-pair %val{key}'
echo 'execute-keys d' ;;
t) echo delete-surrounding-tag ;;
#to close information window, use execute-keys
*) echo 'execute-keys :nop<ret>' ;;
esac
^@
!
define-command change-surround %!
_surrounding-object-info 'change surround'
on-key %@ eval %sh^
case $kak_key in
b|'('|')'|B|'{'|'}'|r|'['|']'|a|'<lt>'|'<gt>'|'"'|Q|"'"|q|'`'|g)
#use $val{key}. if use $kak_key, it break quote case
echo '_change-surrounding-pair %val{key}' ;;
t) echo change-surrounding-tag ;;
#to close information window, use execute-keys
*) echo 'execute-keys :nop<ret>' ;;
esac
^@
!
define-command -hidden -params 1 _select-surrounding-pair %{ execute-keys "<a-a>%arg{1}<a-S>" }
define-command select-surround %!
_surrounding-object-info 'select surround'
on-key %@ eval %sh^
case $kak_key in
b|'('|')'|B|'{'|'}'|r|'['|']'|a|'<lt>'|'<gt>'|'"'|Q|"'"|q|'`'|g)
#use $val{key}. if use $kak_key, it break quote case
echo '_select-surrounding-pair %val{key}' ;;
t) echo select-surrounding-tag ;;
#to close information window, use execute-keys
*) echo 'execute-keys :nop<ret>' ;;
esac
^@
!
define-command -hidden -params 2 _impl-surround %! eval %sh@
command=$1
case $2 in
b|'('|')') open='('; close=')' ;;
r|'['|']') open='['; close=']' ;;
B|'{'|'}') open='{'; close='}' ;;
a|'<lt>'|'<gt>') open='<lt>'; close='>' ;;
#also catch ' " to escape quotes
q|\') open="<'>"; close="<'>" ;;
Q|\") open='<">'; close='<">' ;;
g) open='`'; close='`' ;;
*) open=$2; close=$2 ;;
esac
echo "$command $open $close"
@!
define-command -hidden -params 1 _surrounding-object-info %{
info -title %arg{1} 'select surrounding object
b,(,): parentheses block
B,{,}: braces block
r,[,]: bracket block
a,<,>: angle block
",Q: double quote string
'',q: single quote string
`,g: grave quote string
t: markup tag
'
}
define-command -hidden -params 1 _surrounder-info %{
info -title %arg{1} 'enter char to select surrounder
b,(,): parentheses block
B,{,}: braces block
r,[,]: bracket block
a,<,>: angle block
",Q: double quote string
'',q: single quote string
`,g: grave quote string
t: markup tag
others: pressed character
'
}
#after implement change surround pair for tag,
# remove this command, and use _surrounder-info
define-command -hidden _change-surround-info %{
info -title 'change surround' 'enter char to select surrounder
b,(,): parentheses block
B,{,}: braces block
r,[,]: bracket block
a,<,>: angle block
",Q: double quote string
'',q: single quote string
`,g: grave quote string
others: pressed character
'
}
define-command -hidden _change-surrounding-pair -params 1 %{ eval %sh{
#restore selection within on-key to use itersel
selections="$kak_selections_desc"
#while discard this selection within proceeding process,
#use this command to show what is going to be selected
echo '_select-surrounding-pair %arg{1}'
# echo "_surrounder-info 'change surround'"
echo _change-surround-info
echo "on-key %{
select $selections
evaluate-commands -itersel %{
_select-surrounding-pair %arg{1}
_impl-surround _change-surround %val{key}
}
}"
}}
define-command -hidden -params 2 _change-surround %{ execute-keys "r%arg{1}<space>r%arg{2}" }
#use evaluate-commands to collapse undo history
define-command surround-with-tag %{ evaluate-commands %{
#first append, to put cursor inside inserting tag pair
execute-keys 'a<lt>/><esc>i<lt>><esc>'
execute-keys '<a-a>c<lt>>,<lt>/><ret>'
execute-keys '<a-S><a-a>>s><ret>)'
_activate-hooks-tag-attribute-handler
execute-keys -with-hooks i
}}
define-command delete-surrounding-tag %{
evaluate-commands -itersel _select-surrounding-tag-including-space
execute-keys d
}
define-command change-surrounding-tag %{
evaluate-commands -itersel _select-boundary-of-surrounding-tag
execute-keys '<a-i>c<lt>/?,><ret>)'
_activate-hooks-tag-attribute-handler
execute-keys -with-hooks c
}
define-command select-surrounding-tag %{
evaluate-commands -itersel _select-boundary-of-surrounding-tag
execute-keys '<a-a>c<lt>/?,><ret>'
}
define-command -hidden _activate-hooks-tag-attribute-handler %{
hook -group surround-tag-attribute-handler window RawKey '<space>' %{
execute-keys '<backspace>'
_keep-odds
execute-keys '<space>'
remove-hooks window surround-tag-attribute-handler
}
hook -group surround-tag-attribute-handler window ModeChange insert:normal %{
remove-hooks window surround-tag-attribute-handler
}
}
#for multiple selection
#odd selections is open tag
#even selections is close tag
define-command -hidden _keep-odds %{ eval %sh{
accum_selections=
is_odd=0
for selection in $kak_selections_desc ; do
if [ $is_odd -eq 0 ] ; then
is_odd=1
accum_selections="$accum_selections $selection"
else
is_odd=0
fi
done
#accum_selections has space on head
echo "select$accum_selections"
}}
#use evaluate-commands to restore mark
define-command -hidden _select-surrounding-tag-including-space %{ evaluate-commands %{
_select-boundary-of-surrounding-tag
execute-keys -save-regs '' 'Z<space><a-a>c\\s*<lt>/,><ret><a-Z>a'
execute-keys -save-regs '' 'z(<space><a-a>c<lt>,>\\h*\\n?<ret>'
execute-keys -save-regs '' '<a-z>a'
}}
define-command -hidden _select-boundary-of-surrounding-tag %{
execute-keys \;
#handle inside open tag
try %{
#<a-a>> produce side effect inside close tag
#that make tag_list include the close tag
execute-keys -draft '<a-a>c<lt>/,><ret>'
} catch %{
try %{
execute-keys '<a-a>>'
}
}
execute-keys 'Ge<a-;>'
eval %sh{
tag_list=`echo "$kak_selection" | grep -P -o '(?<=<)[^>]+(?=>)' | cut -d ' ' -f 1`
open=
open_stack=
result=
for tag in $tag_list ; do
if [ `echo $tag | cut -c 1` != / ] ; then
case $tag in
#self-closing tags
area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr) continue ;;
*)
open=$tag
open_stack=$open\\n$open_stack ;;
esac
else
if [ $tag = /$open ] ; then
open_stack=${open_stack#*\\n}
open=`echo $open_stack | head -n 1`
else
result=${tag#/}
break
fi
fi
done
echo "execute-keys '<a-a>c<lt>$result\s?[^>]*>,<lt>/$result><ret>'"
}
execute-keys '<a-S>'
}