-
Notifications
You must be signed in to change notification settings - Fork 715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rc/auto-pairs #1295
Add rc/auto-pairs #1295
Conversation
@alexherbo2 , wow, it's like Christmas, thank you so much |
I don't think this belongs in the upstream repository, it's better off as an extra script. |
rc/base/auto-pairs.kak
Outdated
def -hidden auto-pairs-insert-backspace %{ | ||
try %{ | ||
%sh{ | ||
regex='\Q'$(echo "$kak_opt_auto_pairs" | sed s/:/'\\E|\\Q'/g';'s/,//g) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be wiser to use printf '%s\n'
with arbitrary strings. Same remark for the following calls to echo
.
rc/base/auto-pairs.kak
Outdated
%sh{ | ||
for pair in $(echo "$kak_opt_auto_pairs" | tr : '\n'); do | ||
opener=$(echo $pair | cut --delimiter , --fields 1) | ||
closer=$(echo $pair | cut --delimiter , --fields 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Long options are not defined by the standard, we better use -d
and -f
here. Same remark for the following calls.
I added some fixes that @casimir pointed out, notably his trick to preserve the selections |
I changed the whole implementation to use hooks instead of maps |
I added basic support for quotes |
Surely there should be some tests for this feature and PR? |
Long options for |
@casimir Does it work for you now? |
Following our discussion on IRC, 😺 @mawww added |
It works now but I get a weird selection when I insert a pair, one char to the right. |
@danr Done :) |
I love it, I love it very much)) Insert new indented line after Return
|
And I think |
@BjornMelgaard Done :) |
Not polished yet though |
Also waiting for implementation of idea that you told ) |
@BjornMelgaard I think the surround functionality could be superseded by the auto-pairs, using the selection set plus normal insertion |
@alexherbo2 yeah, I'm looking forward to it) |
@BjornMelgaard I added the surround functionality To use, craft your selections then execute |
@alexherbo2 finnaly, I will look at it right now ) |
@mawww I think the plug-in is ready to merge now. Here is a recap on how to use it. hook global WinCreate .* %{
auto-pairs-enable
} map global user s :auto-pairs-surround<ret> |
This script is nice! Thanks to everyone involved. May I suggest a few additions?
|
Also a minor observation. There are currently |
rc/base/auto-pairs.kak
Outdated
def -hidden auto-pairs-insert-new-line %{ try %{ | ||
%sh{ | ||
regex=$(printf '\Q%s\E' "$kak_opt_auto_pairs" | sed s/:/'\\E|\\Q'/g';'s/'<,>'/'<lt>,<gt>'/g';'s/,/'\\E\\n\\h*\\Q'/g) | ||
echo "exec -draft %(;KGl<a-k>$regex<ret>)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the $regex
variable contains backslashes, passing this string to echo
is risky, better use printf
.
@Delapouite I added the toggle command. Commands:
Options:
And for the name, I prefer real word command / option names (#806). |
Some tests were failing due to not being POSIX. I fixed them thanks to @lenormf. |
Threre seems to be an issue remaining on the Here's what I observed:
|
rc/base/auto-pairs.kak
Outdated
|
||
def -hidden -params 2 auto-pairs-insert-opener %{ try %{ | ||
%sh{ | ||
if [ $1 = $2 ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs quotes around expansions.
rc/base/auto-pairs.kak
Outdated
def -hidden auto-pairs-delete-new-line %{ try %{ | ||
%sh{ | ||
regex=$(printf '\Q%s\E' "$kak_opt_auto_pairs" | sed s/:/'\\E|\\Q'/g';'s/'<,>'/'<lt>,<gt>'/g';'s/,/'\\E\\n\\h*\\Q'/g) | ||
echo "exec -draft %(;JGi<a-k>$regex<ret>)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
printf
is better here.
rc/base/auto-pairs.kak
Outdated
def -hidden auto-pairs-insert-space %{ try %{ | ||
%sh{ | ||
regex=$(printf '\Q%s\E' "$kak_opt_auto_pairs" | sed s/:/'\\E|\\Q'/g';'s/'<,>'/'<lt>,<gt>'/g';'s/,/'\\E\\h\\Q'/g) | ||
echo "exec -draft %(;2H<a-k>$regex<ret>)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
printf
is better here.
rc/base/auto-pairs.kak
Outdated
def -hidden auto-pairs-delete-space %{ try %{ | ||
%sh{ | ||
regex=$(printf '\Q%s\E' "$kak_opt_auto_pairs" | sed s/:/'\\E|\\Q'/g';'s/'<,>'/'<lt>,<gt>'/g';'s/,/'\\E\\h\\Q'/g) | ||
echo "exec -draft %(;l2H<a-k>$regex<ret>)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
printf
is better here.
rc/base/auto-pairs.kak
Outdated
def auto-pairs-enable -docstring 'Enable automatic closing of pairs' %{ | ||
%sh{ | ||
for pair in $(echo "$kak_opt_auto_pairs" | tr : '\n'); do | ||
opener=$(echo $pair | cut -d , -f 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
printf
is better here, also quotes around the expansion.
rc/base/auto-pairs.kak
Outdated
for pair in $(echo "$kak_opt_auto_pairs" | tr : '\n'); do | ||
opener=$(echo $pair | cut -d , -f 1) | ||
closer=$(echo $pair | cut -d , -f 2) | ||
echo "hook window InsertChar \Q$opener -group auto-pairs-insert %(auto-pairs-insert-opener %-$opener- %-$closer-)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
printf is better here.
rc/base/auto-pairs.kak
Outdated
|
||
def auto-pairs-surround -docstring 'Enable automatic closing of pairs on selection boundaries for the whole insert session' %{ | ||
%sh{ | ||
if [ $kak_opt_auto_pairs_enabled = yes ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs quotes around expansions.
rc/base/auto-pairs.kak
Outdated
else | ||
echo set window auto_pairs_was_enabled no | ||
fi | ||
for pair in $(echo "$kak_opt_auto_pairs" | tr : '\n'); do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
printf
is better here.
rc/base/auto-pairs.kak
Outdated
echo set window auto_pairs_was_enabled no | ||
fi | ||
for pair in $(echo "$kak_opt_auto_pairs" | tr : '\n'); do | ||
opener=$(echo $pair | cut -d , -f 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
printf is better here, also quotes around the expansion.
rc/base/auto-pairs.kak
Outdated
fi | ||
for pair in $(echo "$kak_opt_auto_pairs" | tr : '\n'); do | ||
opener=$(echo $pair | cut -d , -f 1) | ||
closer=$(echo $pair | cut -d , -f 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
printf is better here, also quotes around the expansion.
@Delapouite I can reproduce, but I did not isolate the bug yet. @lenormf I added the POSIX changes you requested. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I found the issues:
- a
echo
was missign from a%sh{}
block - internally
kakoune
turnyes/no
booleans intotrue/false
booleans. So it has undesirable effects when comparingyes
with atrue
.
So I would recommend to never use yes/no
booleans, and always sticks with good ol' true/false
rc/base/auto-pairs.kak
Outdated
hook window InsertEnd .* -group auto-pairs-surround-insert-end %{ | ||
%sh{ | ||
if [ "$kak_opt_auto_pairs_was_enabled" = yes ]; then | ||
auto-pairs-enable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing echo
@@ -0,0 +1,131 @@ | |||
decl str-list auto_pairs %((,):{,}:[,]:<,>:",":',':`,`) | |||
decl bool auto_pairs_enabled no |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use false
for this 3 booleans
hook window InsertDelete \n -group auto-pairs-delete auto-pairs-delete-new-line | ||
hook window InsertChar \h -group auto-pairs-insert auto-pairs-insert-space | ||
hook window InsertDelete \h -group auto-pairs-delete auto-pairs-delete-space | ||
set window auto_pairs_enabled yes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use true
rc/base/auto-pairs.kak
Outdated
} | ||
|
||
def auto-pairs-toggle -docstring 'Toggle automatic closing of pairs' %{ %sh{ | ||
if [ "$kak_opt_auto_pairs_enabled" = yes ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use true
rc/base/auto-pairs.kak
Outdated
|
||
def auto-pairs-surround -docstring 'Enable automatic closing of pairs on selection boundaries for the whole insert session' %{ | ||
%sh{ | ||
if [ "$kak_opt_auto_pairs_enabled" = yes ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use true
rc/base/auto-pairs.kak
Outdated
} | ||
hook window InsertEnd .* -group auto-pairs-surround-insert-end %{ | ||
%sh{ | ||
if [ "$kak_opt_auto_pairs_was_enabled" = yes ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use true
def auto-pairs-disable -docstring 'Disable automatic closing of pairs' %{ | ||
remove-hooks window auto-pairs-insert | ||
remove-hooks window auto-pairs-delete | ||
set window auto_pairs_enabled no |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use false
remove-hooks window auto-pairs-surround-insert | ||
remove-hooks window auto-pairs-surround-delete | ||
remove-hooks window auto-pairs-surround-insert-end | ||
set window auto_pairs_surround_enabled no |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use false
set window auto_pairs_surround_enabled no | ||
} | ||
auto-pairs-disable | ||
set window auto_pairs_surround_enabled yes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use true
rc/base/auto-pairs.kak
Outdated
if [ "$kak_opt_auto_pairs_enabled" = yes ]; then | ||
echo set window auto_pairs_was_enabled yes | ||
else | ||
echo set window auto_pairs_was_enabled no |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use false
This is a bit drastic, all that needs to be done is compare the shell boolean variables to |
@Delapouite Thanks! |
Well, as in other languages like YAML or Coffeescript, which support similar booleans niceties, it turns out to be source of errors (plainly demonstrated here) for no real benefits in term of lisibility or human confort. |
Boolean niceties are not a problem in general, but you’re right in the case of Kakoune, because it targets POSIX shell which has no boolean type. |
@Delapouite Did you mind opening an issue on that? |
Reviving #561 eh? |
I still not fond of decentralizing a whole repository, like kakoune-extra. But cherry-picking an extension to have a life on his own. |
Usage
Status line integration
Commands
auto-pairs-enable
: enable automatic closing of pairsauto-pairs-disable
: disable automatic closing of pairsauto-pairs-toggle
: toggle automatic closing of pairsauto-pairs-surround
: enable automatic closing of pairs on selection boundaries for the whole insert sessionOptions
auto_pairs
str-list
: list of pairs (default:(,):{,}:[,]:<,>:",":',':<grave-quote>,<grave-quote>
)auto_pairs_enabled
bool
: information about the way auto-pairs is active (read-only)auto_pairs_surround_enabled
bool
: information about the way auto-pairs-surround is active (read-only)