2015-10-25 13:17:36 +01:00
|
|
|
## Characters that will be inserted at the beginning of a line to comment
|
2016-10-15 10:04:43 +02:00
|
|
|
decl str comment_line_chars "#"
|
|
|
|
|
|
|
|
## Characters that will be used to surround a selection with
|
|
|
|
decl str-list comment_selection_chars ""
|
2015-10-25 13:17:36 +01:00
|
|
|
|
2016-10-14 21:12:01 +02:00
|
|
|
## Default characters for all languages
|
2016-10-15 10:04:43 +02:00
|
|
|
hook global BufSetOption filetype=asciidoc %{
|
|
|
|
set buffer comment_selection_chars '///:///'
|
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=(c|cpp|go|java|javascript|objc|sass|scala|scss|swift) %{
|
|
|
|
set buffer comment_line_chars '//'
|
|
|
|
set buffer comment_selection_chars '/*:*/'
|
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=(cabal|haskell|moon) %{
|
|
|
|
set buffer comment_line_chars '--'
|
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=clojure %{
|
|
|
|
set buffer comment_line_chars '#_ '
|
|
|
|
set buffer comment_selection_chars '(comment :)'
|
|
|
|
}
|
|
|
|
|
2016-10-14 21:12:01 +02:00
|
|
|
hook global BufSetOption filetype=coffee %{
|
|
|
|
set buffer comment_selection_chars '###:###'
|
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=css %{
|
|
|
|
set buffer comment_line_chars ''
|
2016-10-15 10:04:43 +02:00
|
|
|
set buffer comment_selection_chars '/*:*/'
|
2016-10-14 21:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=d %{
|
2016-10-15 10:04:43 +02:00
|
|
|
set buffer comment_line_chars '//'
|
2016-10-14 21:12:01 +02:00
|
|
|
set buffer comment_selection_chars '/+:+/'
|
|
|
|
}
|
|
|
|
|
2016-10-15 10:04:43 +02:00
|
|
|
hook global BufSetOption filetype=(gas|ini) %{
|
|
|
|
set buffer comment_line_chars ';'
|
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=haml %{
|
|
|
|
set buffer comment_line_chars '-#'
|
|
|
|
}
|
|
|
|
|
2016-10-14 21:12:01 +02:00
|
|
|
hook global BufSetOption filetype=html %{
|
|
|
|
set buffer comment_line_chars ''
|
|
|
|
set buffer comment_selection_chars '<!--:-->'
|
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=latex %{
|
|
|
|
set buffer comment_line_chars '%'
|
2016-10-15 10:04:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=lisp %{
|
|
|
|
set buffer comment_line_chars ';'
|
|
|
|
set buffer comment_selection_chars '#|:|#'
|
2016-10-14 21:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=lua %{
|
|
|
|
set buffer comment_line_chars '--'
|
|
|
|
set buffer comment_selection_chars '--[[:]]'
|
|
|
|
}
|
|
|
|
|
2016-10-15 10:04:43 +02:00
|
|
|
hook global BufSetOption filetype=markdown %{
|
|
|
|
set buffer comment_line_chars ''
|
|
|
|
set buffer comment_selection_chars '[//]: # (:)'
|
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=perl %{
|
|
|
|
set buffer comment_selection_chars '#[:]'
|
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=(pug|rust) %{
|
|
|
|
set buffer comment_line_chars '//'
|
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=python %{
|
|
|
|
set buffer comment_selection_chars '""":"""'
|
2016-10-14 21:12:01 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 10:04:43 +02:00
|
|
|
hook global BufSetOption filetype=ragel %{
|
|
|
|
set buffer comment_line_chars '%%'
|
|
|
|
set buffer comment_selection_chars '%%{:}%%'
|
2016-10-14 21:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hook global BufSetOption filetype=ruby %{
|
|
|
|
set buffer comment_selection_chars '^begin=:^=end'
|
|
|
|
}
|
|
|
|
|
2015-10-25 13:17:36 +01:00
|
|
|
def comment-selection -docstring "Comment/uncomment the current selection" %{
|
|
|
|
%sh{
|
2016-10-14 21:15:35 +02:00
|
|
|
exec_proof() {
|
2015-12-03 06:52:57 +01:00
|
|
|
## Replace the '<' sign that is interpreted differently in `exec`
|
2016-04-23 07:47:01 +02:00
|
|
|
printf %s\\n "$@" | sed 's,<,<lt>,g'
|
2015-10-25 13:17:36 +01:00
|
|
|
}
|
|
|
|
|
2016-10-15 10:04:43 +02:00
|
|
|
readonly opening=$(exec_proof "${kak_opt_comment_selection_chars%:*}")
|
2015-12-02 11:05:48 +01:00
|
|
|
readonly closing=$(exec_proof "${kak_opt_comment_selection_chars##*:}")
|
2015-10-25 13:17:36 +01:00
|
|
|
|
2016-03-15 12:51:19 +01:00
|
|
|
if [ -z "${opening}" ] || [ -z "${closing}" ]; then
|
2016-04-23 07:47:01 +02:00
|
|
|
echo "echo -debug 'The \`comment_selection_chars\` variable is empty, could not comment the selection'"
|
2015-10-25 13:17:36 +01:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2016-10-14 21:15:35 +02:00
|
|
|
printf %s\\n "eval -draft %{ try %{
|
2015-10-25 13:17:36 +01:00
|
|
|
## The selection is empty
|
2016-10-14 21:15:35 +02:00
|
|
|
exec <a-K>\\A[\\h\\v\\n]*\\z<ret>
|
2015-10-25 13:17:36 +01:00
|
|
|
|
|
|
|
try %{
|
|
|
|
## The selection has already been commented
|
2016-10-14 21:15:35 +02:00
|
|
|
exec %{<a-K>\\A\\Q${opening}\\E.*\\Q${closing}\\E\\n*\\z<ret>}
|
2015-10-25 13:17:36 +01:00
|
|
|
|
|
|
|
## Comment the selection
|
2016-10-14 21:15:35 +02:00
|
|
|
exec -draft %{a${closing}<esc>i${opening}}
|
2015-10-25 13:17:36 +01:00
|
|
|
} catch %{
|
|
|
|
## Uncomment the commented selection
|
2016-10-14 21:15:35 +02:00
|
|
|
exec -draft %{s(\\A\\Q${opening}\\E)|(\\Q${closing}\\E\\n*\\z)<ret>d}
|
2015-10-25 13:17:36 +01:00
|
|
|
}
|
2016-10-14 21:15:35 +02:00
|
|
|
} }"
|
2015-10-25 13:17:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def comment-line -docstring "Comment/uncomment the current line" %{
|
|
|
|
%sh{
|
|
|
|
readonly opening="${kak_opt_comment_line_chars}"
|
2016-10-14 21:15:35 +02:00
|
|
|
readonly opening_escaped="\\Q${opening}\\E"
|
2015-10-25 13:17:36 +01:00
|
|
|
|
|
|
|
if [ -z "${opening}" ]; then
|
2016-04-23 07:47:01 +02:00
|
|
|
echo "echo -debug 'The \`comment_line_chars\` variable is empty, could not comment the line'"
|
2015-10-25 13:17:36 +01:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2016-10-14 21:15:35 +02:00
|
|
|
printf %s\\n "eval -draft %{
|
|
|
|
## Select the content of the line, without indentation
|
|
|
|
exec I<esc><a-l>
|
2015-10-25 13:17:36 +01:00
|
|
|
|
|
|
|
try %{
|
2016-10-14 21:15:35 +02:00
|
|
|
## There's no text on the line
|
|
|
|
exec <a-K>\\A[\\h\\v\\n]*\\z<ret>
|
|
|
|
|
|
|
|
try %{
|
|
|
|
## The line has already been commented
|
|
|
|
exec %{<a-K>\\A${opening_escaped}<ret>}
|
|
|
|
|
|
|
|
## Comment the line
|
|
|
|
exec -draft %{i${opening}}
|
|
|
|
} catch %{
|
|
|
|
## Uncomment the line
|
|
|
|
exec -draft %{s\\A${opening_escaped}\\h*<ret>d}
|
|
|
|
}
|
2015-10-25 13:17:36 +01:00
|
|
|
}
|
|
|
|
}"
|
|
|
|
}
|
|
|
|
}
|