From c84f06300ab2e878babd196723440b6ae1bc7e08 Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Wed, 2 Dec 2015 12:55:25 +0300 Subject: [PATCH 1/3] Do not add an extra space before/after the commenting characters, to let the user choose exactly what will be inserted in the selection --- rc/commenting.kak | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rc/commenting.kak b/rc/commenting.kak index b8d3dbd7..7fa7caed 100644 --- a/rc/commenting.kak +++ b/rc/commenting.kak @@ -30,10 +30,10 @@ def comment-selection -docstring "Comment/uncomment the current selection" %{ exec -draft %{\A${opening_escaped}.*${closing_escaped}\z} ## Comment the selection - exec %{a ${closing}i${opening} $((${#opening} + 1))H} + exec %{a${closing}i${opening}${#opening}H} } catch %{ ## Uncomment the commented selection - exec -draft %{s(\A${opening_escaped} )|( ${closing_escaped}\z)d} + exec -draft %{s(\A${opening_escaped})|(${closing_escaped}\z)d} } }" } @@ -67,7 +67,7 @@ def comment-line -docstring "Comment/uncomment the current line" %{ exec -draft %{^${opening_escaped}} ## Comment the line - exec %{i${opening} $((${#opening} + 1))H} + exec %{i${opening}${#opening}H} } catch %{ ## Uncomment the line exec -draft %{s^${opening_escaped}\h*d} From c40dba8a208ccf68ac4e7cb43654b069966117ae Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Wed, 2 Dec 2015 13:05:48 +0300 Subject: [PATCH 2/3] Escape additional characters that could be interpreted by the regex/exec engine --- rc/commenting.kak | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/rc/commenting.kak b/rc/commenting.kak index 7fa7caed..076e4027 100644 --- a/rc/commenting.kak +++ b/rc/commenting.kak @@ -8,11 +8,15 @@ def comment-selection -docstring "Comment/uncomment the current selection" %{ %sh{ function escape_regex_chars { ## Escape characters that can be interpreted as modifiers/repetitors by the regex engine - sed -r 's,(\*|\+|\[|\]|\{\}|\||\(|\)|\?),\\\1,g' <<< "$@" + sed -r 's,(\*|\+|\[|\]|\{\}|\||\(|\)|\?|\^|\$),\\\1,g' <<< "$@" + } + function exec_proof { + ## Replace some special characters that are interpreted differently in `exec` + sed -r -e 's,<,,gt>,g' -e 's,,g' -e 's,\bgt>,,g' <<< "$@" } - readonly opening="${kak_opt_comment_selection_chars%%:*}" - readonly closing="${kak_opt_comment_selection_chars##*:}" + readonly opening=$(exec_proof "${kak_opt_comment_selection_chars%%:*}") + readonly closing=$(exec_proof "${kak_opt_comment_selection_chars##*:}") readonly opening_escaped=$(escape_regex_chars "${opening}") readonly closing_escaped=$(escape_regex_chars "${closing}") From 74112ef23cc6fb74cddc115d45f0048af49752d3 Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Thu, 3 Dec 2015 08:52:57 +0300 Subject: [PATCH 3/3] Simplify/remove the escaping routines --- rc/commenting.kak | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/rc/commenting.kak b/rc/commenting.kak index 076e4027..f11e5634 100644 --- a/rc/commenting.kak +++ b/rc/commenting.kak @@ -6,19 +6,13 @@ decl str comment_line_chars "//" def comment-selection -docstring "Comment/uncomment the current selection" %{ %sh{ - function escape_regex_chars { - ## Escape characters that can be interpreted as modifiers/repetitors by the regex engine - sed -r 's,(\*|\+|\[|\]|\{\}|\||\(|\)|\?|\^|\$),\\\1,g' <<< "$@" - } function exec_proof { - ## Replace some special characters that are interpreted differently in `exec` - sed -r -e 's,<,,gt>,g' -e 's,,g' -e 's,\bgt>,,g' <<< "$@" + ## Replace the '<' sign that is interpreted differently in `exec` + sed -r 's,<,,g' <<< "$@" } readonly opening=$(exec_proof "${kak_opt_comment_selection_chars%%:*}") readonly closing=$(exec_proof "${kak_opt_comment_selection_chars##*:}") - readonly opening_escaped=$(escape_regex_chars "${opening}") - readonly closing_escaped=$(escape_regex_chars "${closing}") if [ -z "${opening}" -o -z "${closing}" ]; then echo "The \`comment_selection_chars\` variable is empty, couldn't comment the selection" >&2 @@ -31,13 +25,13 @@ def comment-selection -docstring "Comment/uncomment the current selection" %{ try %{ ## The selection has already been commented - exec -draft %{\A${opening_escaped}.*${closing_escaped}\z} + exec -draft %{\A\Q${opening}\E.*\Q${closing}\E\z} ## Comment the selection exec %{a${closing}i${opening}${#opening}H} } catch %{ ## Uncomment the commented selection - exec -draft %{s(\A${opening_escaped})|(${closing_escaped}\z)d} + exec -draft %{s(\A\Q${opening}\E)|(\Q${closing}\E\z)d} } }" }