## 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{ function exec_proof { ## Replace the '<' sign that is interpreted differently in `exec` printf %s "$@" | sed 's,<,,g' } readonly opening=$(exec_proof "${kak_opt_comment_selection_chars%%:*}") readonly closing=$(exec_proof "${kak_opt_comment_selection_chars##*:}") if [ -z "${opening}" ] || [ -z "${closing}" ]; then printf %s "The \`comment_selection_chars\` variable is empty, couldn't comment the selection" >&2 exit fi printf %s "try %{ ## The selection is empty exec -draft %{\A[\h\v\n]*\z} try %{ ## The selection has already been commented 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\Q${opening}\E)|(\Q${closing}\E\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" if [ -z "${opening}" ]; then printf %s "The \`comment_line_chars\` variable is empty, couldn't comment the line" >&2 exit fi printf %s " ## 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} try %{ ## The line has already been commented exec -draft %{\A${opening_escaped}} ## Comment the line exec %{i${opening}${#opening}H} } catch %{ ## Uncomment the line exec -draft %{s\A${opening_escaped}\h*d} } }" } }