refactor comment.kak
* move most of the kakoune implementation outside of the shell scope to avoid unnecessary escaping * let kakoune do the option expansions to avoid injection of special characters * split block commenting options into two and do the < to <lt> conversion directly * show error messages directly in the buffer rather than in *debug* Fixes #1600 and #875
This commit is contained in:
parent
00d8716c79
commit
16f42fe3a6
|
@ -1,43 +1,52 @@
|
|||
## Line comments
|
||||
# Line comments
|
||||
declare-option -docstring "characters inserted at the beginning of a commented line" \
|
||||
str comment_line '#'
|
||||
|
||||
## Block comments
|
||||
declare-option -docstring "colon separated tuple containing the characters inserted before/after a commented line" \
|
||||
str-list comment_block
|
||||
# Block comments
|
||||
declare-option -docstring "characters inserted before a commented block" \
|
||||
str comment_block_begin
|
||||
declare-option -docstring "characters inserted after a commented block" \
|
||||
str comment_block_end
|
||||
|
||||
## Default comments for all languages
|
||||
# Default comments for all languages
|
||||
hook global BufSetOption filetype=asciidoc %{
|
||||
set-option buffer comment_block '///:///'
|
||||
set-option buffer comment_block_begin '///'
|
||||
set-option buffer comment_block_end '///'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=(c|cpp|go|java|javascript|objc|php|rust|sass|scala|scss|swift) %{
|
||||
set-option buffer comment_line '//'
|
||||
set-option buffer comment_block '/*:*/'
|
||||
set-option buffer comment_block_begin '/*'
|
||||
set-option buffer comment_block_end '*/'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=(cabal|haskell|moon) %{
|
||||
set-option buffer comment_line '--'
|
||||
set-option buffer comment_block '{-:-}'
|
||||
set-option buffer comment_block_begin '{-'
|
||||
set-option buffer comment_block_end '-}'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=clojure %{
|
||||
set-option buffer comment_line '#_ '
|
||||
set-option buffer comment_block '(comment :)'
|
||||
set-option buffer comment_block_begin '(comment '
|
||||
set-option buffer comment_block_end ')'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=coffee %{
|
||||
set-option buffer comment_block '###:###'
|
||||
set-option buffer comment_block_begin '###'
|
||||
set-option buffer comment_block_end '###'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=css %{
|
||||
set-option buffer comment_line ''
|
||||
set-option buffer comment_block '/*:*/'
|
||||
set-option buffer comment_block_begin '/*'
|
||||
set-option buffer comment_block_end '*/'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=d %{
|
||||
set-option buffer comment_line '//'
|
||||
set-option buffer comment_block '/+:+/'
|
||||
set-option buffer comment_block_begin '/+'
|
||||
set-option buffer comment_block_end '+/'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=(gas|ini) %{
|
||||
|
@ -50,7 +59,8 @@ hook global BufSetOption filetype=haml %{
|
|||
|
||||
hook global BufSetOption filetype=html %{
|
||||
set-option buffer comment_line ''
|
||||
set-option buffer comment_block '<!--:-->'
|
||||
set-option buffer comment_block_begin '<lt>!--'
|
||||
set-option buffer comment_block_end '-->'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=latex %{
|
||||
|
@ -59,102 +69,94 @@ hook global BufSetOption filetype=latex %{
|
|||
|
||||
hook global BufSetOption filetype=lisp %{
|
||||
set-option buffer comment_line ';'
|
||||
set-option buffer comment_block '#|:|#'
|
||||
set-option buffer comment_block_begin '#|'
|
||||
set-option buffer comment_block_end '|#'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=lua %{
|
||||
set-option buffer comment_line '--'
|
||||
set-option buffer comment_block '--[[:]]'
|
||||
set-option buffer comment_block_begin '--[['
|
||||
set-option buffer comment_block_end ']]'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=markdown %{
|
||||
set-option buffer comment_line ''
|
||||
set-option buffer comment_block '[//]: # (:)'
|
||||
set-option buffer comment_block_begin '[//]'
|
||||
set-option buffer comment_block_end '# (:)'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=perl %{
|
||||
set-option buffer comment_block '#[:]'
|
||||
set-option buffer comment_block_begin '#['
|
||||
set-option buffer comment_block_end ']'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=pug %{
|
||||
set buffer comment_line '//'
|
||||
set-option buffer comment_line '//'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=python %{
|
||||
set-option buffer comment_block "''':'''"
|
||||
set-option buffer comment_block_begin "'''"
|
||||
set-option buffer comment_block_end "'''"
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=ragel %{
|
||||
set-option buffer comment_line '%%'
|
||||
set-option buffer comment_block '%%{:}%%'
|
||||
set-option buffer comment_block_begin '%%{'
|
||||
set-option buffer comment_block_end '}%%'
|
||||
}
|
||||
|
||||
hook global BufSetOption filetype=ruby %{
|
||||
set-option buffer comment_block '^begin=:^=end'
|
||||
set-option buffer comment_block_begin '^begin='
|
||||
set-option buffer comment_block_end '^=end'
|
||||
}
|
||||
|
||||
define-command comment-block -docstring '(un)comment selected lines using block comments' %{
|
||||
define-command comment-block -docstring '(un)comment selections using block comments' %{
|
||||
%sh{
|
||||
exec_proof() {
|
||||
## Replace the '<' sign that is interpreted differently in `execute-keys`
|
||||
printf %s\\n "$@" | sed 's,<,<lt>,g'
|
||||
}
|
||||
|
||||
readonly opening=$(exec_proof "${kak_opt_comment_block%:*}")
|
||||
readonly closing=$(exec_proof "${kak_opt_comment_block##*:}")
|
||||
|
||||
if [ -z "${opening}" ] || [ -z "${closing}" ]; then
|
||||
echo "echo -debug 'The \`comment_block\` variable is empty, could not comment the selection'"
|
||||
exit
|
||||
if [ -z "${kak_opt_comment_block_begin}" ] || [ -z "${kak_opt_comment_block_end}" ]; then
|
||||
echo "fail \"The 'comment_block' options are empty, could not comment the selection\""
|
||||
fi
|
||||
|
||||
printf %s\\n "evaluate-commands -draft %{ try %{
|
||||
## The selection is empty
|
||||
execute-keys <a-K>\\A[\\h\\v\\n]*\\z<ret>
|
||||
}
|
||||
evaluate-commands -draft %{
|
||||
# Keep non-empty selections
|
||||
execute-keys <a-K>\A\s*\z<ret>
|
||||
|
||||
try %{
|
||||
## The selection has already been commented
|
||||
execute-keys %{<a-K>\\A\\Q${opening}\\E.*\\Q${closing}\\E\\n*\\z<ret>}
|
||||
# Assert that the selection has not been commented
|
||||
execute-keys "<a-K>\A\Q%opt{comment_block_begin}\E.*\Q%opt{comment_block_end}\E\n*\z<ret>"
|
||||
|
||||
## Comment the selection
|
||||
execute-keys -draft %{a${closing}<esc>i${opening}}
|
||||
# Comment the selection
|
||||
execute-keys -draft "a%opt{comment_block_end}<esc>i%opt{comment_block_begin}"
|
||||
} catch %{
|
||||
## Uncomment the commented selection
|
||||
execute-keys -draft %{s(\\A\\Q${opening}\\E)|(\\Q${closing}\\E\\n*\\z)<ret>d}
|
||||
# Uncomment the commented selection
|
||||
execute-keys -draft "s(\A\Q%opt{comment_block_begin}\E)|(\Q%opt{comment_block_end}\E\n*\z)<ret>d"
|
||||
}
|
||||
} }"
|
||||
}
|
||||
}
|
||||
|
||||
define-command comment-line -docstring '(un)comment selected lines using line comments' %{
|
||||
%sh{
|
||||
readonly opening="${kak_opt_comment_line}"
|
||||
readonly opening_escaped="\\Q${opening}\\E"
|
||||
|
||||
if [ -z "${opening}" ]; then
|
||||
echo "echo -debug 'The \`comment_line\` variable is empty, could not comment the line'"
|
||||
exit
|
||||
if [ -z "${kak_opt_comment_line}" ]; then
|
||||
echo "fail \"The 'comment_line' option is empty, could not comment the line\""
|
||||
fi
|
||||
|
||||
printf %s\\n "evaluate-commands -draft %{
|
||||
## Select the content of the lines, without indentation
|
||||
execute-keys <a-s>I<esc><a-l>
|
||||
}
|
||||
evaluate-commands -draft %{
|
||||
# Select the content of the lines, without indentation
|
||||
execute-keys <a-s>gi<a-l>
|
||||
|
||||
try %{
|
||||
## There’s no text on the line
|
||||
execute-keys <a-K>\\A[\\h\\v\\n]*\\z<ret>
|
||||
# Keep non-empty lines
|
||||
execute-keys <a-K>\A\s*\z<ret>
|
||||
|
||||
try %{
|
||||
## The line has already been commented
|
||||
execute-keys %{<a-K>\\A${opening_escaped}<ret>}
|
||||
# Assert that the line has not been commented
|
||||
execute-keys "<a-K>\A\Q%opt{comment_line}\E<ret>"
|
||||
|
||||
## Comment the line
|
||||
execute-keys -draft %{i${opening}}
|
||||
# Comment the line
|
||||
execute-keys -draft "i%opt{comment_line}"
|
||||
} catch %{
|
||||
## Uncomment the line
|
||||
execute-keys -draft %{s\\A${opening_escaped}\\h*<ret>d}
|
||||
# Uncomment the line
|
||||
execute-keys -draft "s\A\Q%opt{comment_line}\E\h*<ret>d"
|
||||
}
|
||||
}
|
||||
}"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user