Miscellaneous fixes to the commenting.kak script

This commit properly produces backslash characters within double quote
strings instead of hoping the shell will not recognize the escape
sequence that they form with the following character. Use the proper
POSIX function declaration form.

The uncommenting logic now also ignores trailing newline characters,
which shortens the amount of operations needed to uncomment a selection.
This commit is contained in:
Frank LENORMAND 2016-10-14 22:15:35 +03:00
parent 5db1d1beef
commit 68d3eda270

View File

@ -50,7 +50,7 @@ hook global BufSetOption filetype=ruby %{
def comment-selection -docstring "Comment/uncomment the current selection" %{ def comment-selection -docstring "Comment/uncomment the current selection" %{
%sh{ %sh{
function exec_proof { exec_proof() {
## Replace the '<' sign that is interpreted differently in `exec` ## Replace the '<' sign that is interpreted differently in `exec`
printf %s\\n "$@" | sed 's,<,<lt>,g' printf %s\\n "$@" | sed 's,<,<lt>,g'
} }
@ -63,51 +63,52 @@ def comment-selection -docstring "Comment/uncomment the current selection" %{
exit exit
fi fi
printf %s\\n "try %{ printf %s\\n "eval -draft %{ try %{
## The selection is empty ## The selection is empty
exec -draft %{<a-K>\A[\h\v\n]*\z<ret>} exec <a-K>\\A[\\h\\v\\n]*\\z<ret>
try %{ try %{
## The selection has already been commented ## The selection has already been commented
exec -draft %{<a-K>\A\Q${opening}\E.*\Q${closing}\E\z<ret>} exec %{<a-K>\\A\\Q${opening}\\E.*\\Q${closing}\\E\\n*\\z<ret>}
## Comment the selection ## Comment the selection
exec %{a${closing}<esc>i${opening}<esc>${#opening}H} exec -draft %{a${closing}<esc>i${opening}}
} catch %{ } catch %{
## Uncomment the commented selection ## Uncomment the commented selection
exec -draft %{s(\A\Q${opening}\E)|(\Q${closing}\E\z)<ret>d} exec -draft %{s(\\A\\Q${opening}\\E)|(\\Q${closing}\\E\\n*\\z)<ret>d}
} }
}" } }"
} }
} }
def comment-line -docstring "Comment/uncomment the current line" %{ def comment-line -docstring "Comment/uncomment the current line" %{
%sh{ %sh{
readonly opening="${kak_opt_comment_line_chars}" readonly opening="${kak_opt_comment_line_chars}"
readonly opening_escaped="\Q${opening}\E" readonly opening_escaped="\\Q${opening}\\E"
if [ -z "${opening}" ]; then if [ -z "${opening}" ]; then
echo "echo -debug 'The \`comment_line_chars\` variable is empty, could not comment the line'" echo "echo -debug 'The \`comment_line_chars\` variable is empty, could not comment the line'"
exit exit
fi fi
printf %s\\n " printf %s\\n "eval -draft %{
## Select the content of the line, without indentation ## Select the content of the line, without indentation
exec %{I<esc><a-l>} exec I<esc><a-l>
try %{
## There's no text on the line
exec -draft %{<a-K>\A[\h\v\n]*\z<ret>}
try %{ try %{
## The line has already been commented ## There's no text on the line
exec -draft %{<a-K>\A${opening_escaped}<ret>} exec <a-K>\\A[\\h\\v\\n]*\\z<ret>
## Comment the line try %{
exec %{i${opening}<esc>${#opening}H} ## The line has already been commented
} catch %{ exec %{<a-K>\\A${opening_escaped}<ret>}
## Uncomment the line
exec -draft %{s\A${opening_escaped}\h*<ret>d} ## Comment the line
exec -draft %{i${opening}}
} catch %{
## Uncomment the line
exec -draft %{s\\A${opening_escaped}\\h*<ret>d}
}
} }
}" }"
} }