From 00d8716c79567a4d1b8d35686067e736ed81fcd2 Mon Sep 17 00:00:00 2001 From: Olivier Perret Date: Mon, 16 Oct 2017 16:07:43 +0200 Subject: [PATCH 1/3] commenting: move rust to the C-style group --- rc/core/comment.kak | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rc/core/comment.kak b/rc/core/comment.kak index acb96f89..994de24a 100644 --- a/rc/core/comment.kak +++ b/rc/core/comment.kak @@ -11,7 +11,7 @@ hook global BufSetOption filetype=asciidoc %{ set-option buffer comment_block '///:///' } -hook global BufSetOption filetype=(c|cpp|go|java|javascript|objc|php|sass|scala|scss|swift) %{ +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 '/*:*/' } @@ -76,8 +76,8 @@ hook global BufSetOption filetype=perl %{ set-option buffer comment_block '#[:]' } -hook global BufSetOption filetype=(pug|rust) %{ - set-option buffer comment_line '//' +hook global BufSetOption filetype=pug %{ + set buffer comment_line '//' } hook global BufSetOption filetype=python %{ From 16f42fe3a6806c467e2c0001c0740b70264e1e55 Mon Sep 17 00:00:00 2001 From: Olivier Perret Date: Mon, 16 Oct 2017 16:07:52 +0200 Subject: [PATCH 2/3] 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 conversion directly * show error messages directly in the buffer rather than in *debug* Fixes #1600 and #875 --- rc/core/comment.kak | 140 ++++++++++++++++++++++---------------------- 1 file changed, 71 insertions(+), 69 deletions(-) diff --git a/rc/core/comment.kak b/rc/core/comment.kak index 994de24a..2b101db8 100644 --- a/rc/core/comment.kak +++ b/rc/core/comment.kak @@ -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 '!--' + 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,<,,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 + } + evaluate-commands -draft %{ + # Keep non-empty selections + execute-keys \A\s*\z - printf %s\\n "evaluate-commands -draft %{ try %{ - ## The selection is empty - execute-keys \\A[\\h\\v\\n]*\\z + try %{ + # Assert that the selection has not been commented + execute-keys "\A\Q%opt{comment_block_begin}\E.*\Q%opt{comment_block_end}\E\n*\z" - try %{ - ## The selection has already been commented - execute-keys %{\\A\\Q${opening}\\E.*\\Q${closing}\\E\\n*\\z} - - ## Comment the selection - execute-keys -draft %{a${closing}i${opening}} - } catch %{ - ## Uncomment the commented selection - execute-keys -draft %{s(\\A\\Q${opening}\\E)|(\\Q${closing}\\E\\n*\\z)d} - } - } }" + # Comment the selection + execute-keys -draft "a%opt{comment_block_end}i%opt{comment_block_begin}" + } catch %{ + # Uncomment the commented selection + execute-keys -draft "s(\A\Q%opt{comment_block_begin}\E)|(\Q%opt{comment_block_end}\E\n*\z)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 + } + evaluate-commands -draft %{ + # Select the content of the lines, without indentation + execute-keys gi - printf %s\\n "evaluate-commands -draft %{ - ## Select the content of the lines, without indentation - execute-keys I + try %{ + # Keep non-empty lines + execute-keys \A\s*\z try %{ - ## There’s no text on the line - execute-keys \\A[\\h\\v\\n]*\\z + # Assert that the line has not been commented + execute-keys "\A\Q%opt{comment_line}\E" - try %{ - ## The line has already been commented - execute-keys %{\\A${opening_escaped}} - - ## Comment the line - execute-keys -draft %{i${opening}} - } catch %{ - ## Uncomment the line - execute-keys -draft %{s\\A${opening_escaped}\\h*d} - } + # Comment the line + execute-keys -draft "i%opt{comment_line}" + } catch %{ + # Uncomment the line + execute-keys -draft "s\A\Q%opt{comment_line}\E\h*d" } - }" + } } } From ee117b949912fd028a0efbf08b6fac1b63378c78 Mon Sep 17 00:00:00 2001 From: Olivier Perret Date: Thu, 23 Nov 2017 10:30:10 +0100 Subject: [PATCH 3/3] set registers directly instead of expanding options in exec --- rc/core/comment.kak | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/rc/core/comment.kak b/rc/core/comment.kak index 2b101db8..aeee9080 100644 --- a/rc/core/comment.kak +++ b/rc/core/comment.kak @@ -59,7 +59,7 @@ hook global BufSetOption filetype=haml %{ hook global BufSetOption filetype=html %{ set-option buffer comment_line '' - set-option buffer comment_block_begin '!--' + set-option buffer comment_block_begin '' } @@ -121,14 +121,18 @@ define-command comment-block -docstring '(un)comment selections using block comm execute-keys \A\s*\z try %{ - # Assert that the selection has not been commented - execute-keys "\A\Q%opt{comment_block_begin}\E.*\Q%opt{comment_block_end}\E\n*\z" - - # Comment the selection - execute-keys -draft "a%opt{comment_block_end}i%opt{comment_block_begin}" + # Assert that the selection has been commented + set-register / "\A\Q%opt{comment_block_begin}\E.*\Q%opt{comment_block_end}\E\n*\z" + execute-keys "s" + # Uncomment it + set-register / "\A\Q%opt{comment_block_begin}\E|\Q%opt{comment_block_end}\E\n*\z" + execute-keys sd } catch %{ - # Uncomment the commented selection - execute-keys -draft "s(\A\Q%opt{comment_block_begin}\E)|(\Q%opt{comment_block_end}\E\n*\z)d" + # Comment the selection + set-register '"' "%opt{comment_block_begin}" + execute-keys P + set-register '"' "%opt{comment_block_end}" + execute-keys p } } } @@ -148,14 +152,13 @@ define-command comment-line -docstring '(un)comment selected lines using line co execute-keys \A\s*\z try %{ - # Assert that the line has not been commented - execute-keys "\A\Q%opt{comment_line}\E" - - # Comment the line - execute-keys -draft "i%opt{comment_line}" + # Select the comment characters and remove them + set-register / "\A\Q%opt{comment_line}\E\h*" + execute-keys sd } catch %{ - # Uncomment the line - execute-keys -draft "s\A\Q%opt{comment_line}\E\h*d" + # Comment the line + set-register '"' "%opt{comment_line}" + execute-keys P } } }