1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-02 00:46:42 +03:00
kakoune/rc/core/comment.kak

158 lines
4.2 KiB
Plaintext
Raw Normal View History

2017-01-15 16:58:07 +03:00
## Line comments
2017-01-15 17:12:57 +03:00
decl str comment_line '#'
2017-01-15 16:58:07 +03:00
## Block comments
2017-01-15 17:07:17 +03:00
decl str-list comment_block
2017-01-15 16:58:07 +03:00
## Default comments for all languages
hook global BufSetOption filetype=asciidoc %{
2017-01-15 16:58:07 +03:00
set buffer comment_block '///:///'
}
hook global BufSetOption filetype=(c|cpp|go|java|javascript|objc|php|sass|scala|scss|swift) %{
2017-01-15 16:58:07 +03:00
set buffer comment_line '//'
set buffer comment_block '/*:*/'
}
hook global BufSetOption filetype=(cabal|haskell|moon) %{
2017-01-15 16:58:07 +03:00
set buffer comment_line '--'
}
hook global BufSetOption filetype=clojure %{
2017-01-15 16:58:07 +03:00
set buffer comment_line '#_ '
set buffer comment_block '(comment :)'
}
hook global BufSetOption filetype=coffee %{
2017-01-15 16:58:07 +03:00
set buffer comment_block '###:###'
}
hook global BufSetOption filetype=css %{
2017-01-15 16:58:07 +03:00
set buffer comment_line ''
set buffer comment_block '/*:*/'
}
hook global BufSetOption filetype=d %{
2017-01-15 16:58:07 +03:00
set buffer comment_line '//'
set buffer comment_block '/+:+/'
}
hook global BufSetOption filetype=(gas|ini) %{
2017-01-15 16:58:07 +03:00
set buffer comment_line ';'
}
hook global BufSetOption filetype=haml %{
2017-01-15 16:58:07 +03:00
set buffer comment_line '-#'
}
hook global BufSetOption filetype=html %{
2017-01-15 16:58:07 +03:00
set buffer comment_line ''
set buffer comment_block '<!--:-->'
}
hook global BufSetOption filetype=latex %{
2017-01-15 16:58:07 +03:00
set buffer comment_line '%'
}
hook global BufSetOption filetype=lisp %{
2017-01-15 16:58:07 +03:00
set buffer comment_line ';'
set buffer comment_block '#|:|#'
}
hook global BufSetOption filetype=lua %{
2017-01-15 16:58:07 +03:00
set buffer comment_line '--'
set buffer comment_block '--[[:]]'
}
hook global BufSetOption filetype=markdown %{
2017-01-15 16:58:07 +03:00
set buffer comment_line ''
set buffer comment_block '[//]: # (:)'
}
hook global BufSetOption filetype=perl %{
2017-01-15 16:58:07 +03:00
set buffer comment_block '#[:]'
}
hook global BufSetOption filetype=(pug|rust) %{
2017-01-15 16:58:07 +03:00
set buffer comment_line '//'
}
hook global BufSetOption filetype=python %{
2017-01-15 16:58:07 +03:00
set buffer comment_block '\'\'\':\'\'\''
}
hook global BufSetOption filetype=ragel %{
2017-01-15 16:58:07 +03:00
set buffer comment_line '%%'
set buffer comment_block '%%{:}%%'
}
hook global BufSetOption filetype=ruby %{
2017-01-15 16:58:07 +03:00
set buffer comment_block '^begin=:^=end'
}
2017-01-15 17:12:57 +03:00
def comment-block -docstring '(un)comment selected lines using block comments' %{
%sh{
exec_proof() {
2015-12-03 08:52:57 +03:00
## Replace the '<' sign that is interpreted differently in `exec`
printf %s\\n "$@" | sed 's,<,<lt>,g'
}
2017-01-15 16:58:07 +03:00
readonly opening=$(exec_proof "${kak_opt_comment_block%:*}")
readonly closing=$(exec_proof "${kak_opt_comment_block##*:}")
if [ -z "${opening}" ] || [ -z "${closing}" ]; then
2017-01-15 16:58:07 +03:00
echo "echo -debug 'The \`comment_block\` variable is empty, could not comment the selection'"
exit
fi
printf %s\\n "eval -draft %{ try %{
## The selection is empty
exec <a-K>\\A[\\h\\v\\n]*\\z<ret>
try %{
## The selection has already been commented
exec %{<a-K>\\A\\Q${opening}\\E.*\\Q${closing}\\E\\n*\\z<ret>}
## Comment the selection
exec -draft %{a${closing}<esc>i${opening}}
} catch %{
## Uncomment the commented selection
exec -draft %{s(\\A\\Q${opening}\\E)|(\\Q${closing}\\E\\n*\\z)<ret>d}
}
} }"
}
}
2017-01-15 17:12:57 +03:00
def comment-line -docstring '(un)comment selected lines using line comments' %{
%sh{
2017-01-15 16:58:07 +03:00
readonly opening="${kak_opt_comment_line}"
readonly opening_escaped="\\Q${opening}\\E"
if [ -z "${opening}" ]; then
2017-01-15 16:58:07 +03:00
echo "echo -debug 'The \`comment_line\` variable is empty, could not comment the line'"
exit
fi
printf %s\\n "eval -draft %{
## Select the content of the lines, without indentation
exec <a-s>I<esc><a-l>
try %{
2017-01-15 17:05:01 +03:00
## Theres 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}
}
}
}"
}
}