2015-10-25 15:17:36 +03:00
|
|
|
## Characters that will be used to surround a selection with
|
|
|
|
decl str-list comment_selection_chars "/*:*/"
|
|
|
|
|
|
|
|
## Characters that will be inserted at the beginning of a line to comment
|
|
|
|
decl str comment_line_chars "//"
|
|
|
|
|
|
|
|
def comment-selection -docstring "Comment/uncomment the current selection" %{
|
|
|
|
%sh{
|
2015-12-02 13:05:48 +03:00
|
|
|
function exec_proof {
|
2015-12-03 08:52:57 +03:00
|
|
|
## Replace the '<' sign that is interpreted differently in `exec`
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "$@" | sed 's,<,<lt>,g'
|
2015-10-25 15:17:36 +03:00
|
|
|
}
|
|
|
|
|
2015-12-02 13:05:48 +03:00
|
|
|
readonly opening=$(exec_proof "${kak_opt_comment_selection_chars%%:*}")
|
|
|
|
readonly closing=$(exec_proof "${kak_opt_comment_selection_chars##*:}")
|
2015-10-25 15:17:36 +03:00
|
|
|
|
2016-03-15 14:51:19 +03:00
|
|
|
if [ -z "${opening}" ] || [ -z "${closing}" ]; then
|
2016-04-23 08:47:01 +03:00
|
|
|
echo "echo -debug 'The \`comment_selection_chars\` variable is empty, could not comment the selection'"
|
2015-10-25 15:17:36 +03:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "try %{
|
2015-10-25 15:17:36 +03:00
|
|
|
## The selection is empty
|
|
|
|
exec -draft %{<a-K>\A[\h\v\n]*\z<ret>}
|
|
|
|
|
|
|
|
try %{
|
|
|
|
## The selection has already been commented
|
2015-12-03 08:52:57 +03:00
|
|
|
exec -draft %{<a-K>\A\Q${opening}\E.*\Q${closing}\E\z<ret>}
|
2015-10-25 15:17:36 +03:00
|
|
|
|
|
|
|
## Comment the selection
|
2015-12-02 12:55:25 +03:00
|
|
|
exec %{a${closing}<esc>i${opening}<esc>${#opening}H}
|
2015-10-25 15:17:36 +03:00
|
|
|
} catch %{
|
|
|
|
## Uncomment the commented selection
|
2015-12-03 08:52:57 +03:00
|
|
|
exec -draft %{s(\A\Q${opening}\E)|(\Q${closing}\E\z)<ret>d}
|
2015-10-25 15:17:36 +03:00
|
|
|
}
|
|
|
|
}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def comment-line -docstring "Comment/uncomment the current line" %{
|
|
|
|
%sh{
|
|
|
|
readonly opening="${kak_opt_comment_line_chars}"
|
2015-12-03 16:38:37 +03:00
|
|
|
readonly opening_escaped="\Q${opening}\E"
|
2015-10-25 15:17:36 +03:00
|
|
|
|
|
|
|
if [ -z "${opening}" ]; then
|
2016-04-23 08:47:01 +03:00
|
|
|
echo "echo -debug 'The \`comment_line_chars\` variable is empty, could not comment the line'"
|
2015-10-25 15:17:36 +03:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "
|
2015-10-25 15:17:36 +03:00
|
|
|
## Select the content of the line, without indentation
|
|
|
|
exec %{I<esc><a-l>}
|
|
|
|
|
|
|
|
try %{
|
|
|
|
## There's no text on the line
|
|
|
|
exec -draft %{<a-K>\A[\h\v\n]*\z<ret>}
|
|
|
|
|
|
|
|
try %{
|
|
|
|
## The line has already been commented
|
2016-03-25 22:40:17 +03:00
|
|
|
exec -draft %{<a-K>\A${opening_escaped}<ret>}
|
2015-10-25 15:17:36 +03:00
|
|
|
|
|
|
|
## Comment the line
|
2015-12-02 12:55:25 +03:00
|
|
|
exec %{i${opening}<esc>${#opening}H}
|
2015-10-25 15:17:36 +03:00
|
|
|
} catch %{
|
|
|
|
## Uncomment the line
|
2016-03-25 22:40:17 +03:00
|
|
|
exec -draft %{s\A${opening_escaped}\h*<ret>d}
|
2015-10-25 15:17:36 +03:00
|
|
|
}
|
|
|
|
}"
|
|
|
|
}
|
|
|
|
}
|