From 68d3eda2705308099cd95dc7003442628a531c8f Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Fri, 14 Oct 2016 22:15:35 +0300 Subject: [PATCH] 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. --- rc/core/commenting.kak | 45 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/rc/core/commenting.kak b/rc/core/commenting.kak index c2b7054a..8b749047 100644 --- a/rc/core/commenting.kak +++ b/rc/core/commenting.kak @@ -50,7 +50,7 @@ hook global BufSetOption filetype=ruby %{ def comment-selection -docstring "Comment/uncomment the current selection" %{ %sh{ - function exec_proof { + exec_proof() { ## Replace the '<' sign that is interpreted differently in `exec` printf %s\\n "$@" | sed 's,<,,g' } @@ -63,51 +63,52 @@ def comment-selection -docstring "Comment/uncomment the current selection" %{ exit fi - printf %s\\n "try %{ + printf %s\\n "eval -draft %{ try %{ ## The selection is empty - exec -draft %{\A[\h\v\n]*\z} + exec \\A[\\h\\v\\n]*\\z try %{ ## The selection has already been commented - exec -draft %{\A\Q${opening}\E.*\Q${closing}\E\z} + exec %{\\A\\Q${opening}\\E.*\\Q${closing}\\E\\n*\\z} ## Comment the selection - exec %{a${closing}i${opening}${#opening}H} + exec -draft %{a${closing}i${opening}} } catch %{ ## Uncomment the commented selection - exec -draft %{s(\A\Q${opening}\E)|(\Q${closing}\E\z)d} + exec -draft %{s(\\A\\Q${opening}\\E)|(\\Q${closing}\\E\\n*\\z)d} } - }" + } }" } } def comment-line -docstring "Comment/uncomment the current line" %{ %sh{ readonly opening="${kak_opt_comment_line_chars}" - readonly opening_escaped="\Q${opening}\E" + readonly opening_escaped="\\Q${opening}\\E" if [ -z "${opening}" ]; then echo "echo -debug 'The \`comment_line_chars\` variable is empty, could not comment the line'" exit fi - printf %s\\n " - ## Select the content of the line, without indentation - exec %{I} - - try %{ - ## There's no text on the line - exec -draft %{\A[\h\v\n]*\z} + printf %s\\n "eval -draft %{ + ## Select the content of the line, without indentation + exec I try %{ - ## The line has already been commented - exec -draft %{\A${opening_escaped}} + ## There's no text on the line + exec \\A[\\h\\v\\n]*\\z - ## Comment the line - exec %{i${opening}${#opening}H} - } catch %{ - ## Uncomment the line - exec -draft %{s\A${opening_escaped}\h*d} + try %{ + ## The line has already been commented + exec %{\\A${opening_escaped}} + + ## Comment the line + exec -draft %{i${opening}} + } catch %{ + ## Uncomment the line + exec -draft %{s\\A${opening_escaped}\\h*d} + } } }" }