From 4204f6dae6cdbf7b9ecb59c9b30820787c159b2c Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Tue, 12 Jul 2016 17:23:55 +0300 Subject: [PATCH 1/2] Create an insert group, fix and improve the existing hooks --- rc/core/c-family.kak | 113 ++++++++++++++++++++++++++++++++----------- 1 file changed, 85 insertions(+), 28 deletions(-) diff --git a/rc/core/c-family.kak b/rc/core/c-family.kak index c3336bdf..c28f112b 100644 --- a/rc/core/c-family.kak +++ b/rc/core/c-family.kak @@ -35,26 +35,30 @@ hook global BufSetOption mimetype=text/x-objc %{ set buffer filetype objc } -def -hidden _c-family-indent-on-new-line %~ - eval -draft -itersel %= - # preserve previous line indent - try %{ exec -draft \;K } - # indent after lines ending with { or ( - try %[ exec -draft k [{(]\h*$ j ] - # cleanup trailing white space son previous line - try %{ exec -draft k s \h+$ d } - # align to opening paren of previous line - try %{ exec -draft [( \`\([^\n]+\n[^\n]*\n?\' s \`\(\h*.|.\' '' & } - # align to previous statement start when previous line closed a parenthesis - # try %{ exec -draft \)M\`\(.*\)[^\n()]*\n\h*\n?\'s\`|.\'1 } - # copy // comments prefix - try %{ exec -draft \;k s ^\h*\K/{2,} yP } - # indent after visibility specifier - try %[ exec -draft k ^\h*(public|private|protected):\h*$ j ] - # indent after if|else|while|for - try %[ exec -draft \;)MB \`(if|else|while|for)\h*\(.*\)\h*\n\h*\n?\' s \`|.\' 11 ] - = -~ +def -hidden _c-family-trim-autoindent %[ eval -draft -itersel %[ + ## remove the line if it's empty when leaving the insert mode + try %[ exec ^\h+$ d ] +] ] + +def -hidden _c-family-indent-on-newline %[ eval -draft -itersel %[ + exec \; + ## indent new lines with the same level as the previous one + exec -draft K + ## remove previous empty lines resulting from the automatic indent + try %[ exec -draft k H ^\h+$ d ] + ## indent after an opening brace + try %[ exec -draft K s\{\h*$ j ] + ## indent after a label + try %[ exec -draft K s[a-zA-Z0-9_-]+:\h*$ j ] + ## indent after a statement not followed by an opening brace + try %[ exec -draft k \b(if|else|for|while)\h*\(.+?\)\h*$ j ] + ## align to the opening parenthesis when inserting a newline + try %[ eval -draft %[ + exec {b L + try %[ exec -draft s^\h+ d ] + exec & + ] ] +] ] def -hidden _c-family-indent-on-opening-curly-brace %[ # align indent with opening paren when { is entered on a new line after the closing paren @@ -64,8 +68,60 @@ def -hidden _c-family-indent-on-opening-curly-brace %[ def -hidden _c-family-indent-on-closing-curly-brace %[ # align to opening curly brace when alone on a line try %[ exec -itersel -draft ^\h+\}$hms\`|.\'1 ] - # add ; after } if class or struct definition - try %[ exec -draft "hm;(class|struct|union)\`(class|struct|union)[^{}\n]+(\n)?\s*\{\'ma;" ] +] + +def -hidden _c-family-insert-on-closing-curly-brace %[ + # add a semicolon after a closing brace if part of a class, union or struct definition + try %[ exec -itersel -draft hmB^\h*(class|struct|union) a\; ] +] + +def -hidden _c-family-insert-on-newline %[ + exec \; + try %[ + eval -draft %[ + ## copy the commenting prefix + exec -save-regs '' k 1s^\h*(//+\h*) y + try %[ + ## if the previous comment isn't empty, create a new one + exec ^\h*//+\h*$ js^\h*p + ] catch %[ + ## if there is no text in the previous comment, remove it completely + exec d + ] + ] + ] + try %[ + eval -draft %[ + ## select the previous line + exec k + + try %{ + ## if the previous line isn't within a comment scope, break + exec ^(\h*/\*|\h+\*[^/]) + ## simple test to check that the previous comment has been left open + exec \*/\h*$ + + try %[ + ## if the next line is a comment line, add a star + exec -draft 2j^\h+\* + exec -draft js^\h*a* + ] catch %[ + try %[ + ## if the previous line is an empty comment line, close the comment scope + exec -draft ^\h+\*\h+$ 1s\*(\h*)c/ + ] catch %[ + ## if the previous line is a non-empty comment line, add a star + exec -draft js^\h*a* + ] + ] + + ## trim trailing whitespace on the previous line + try %[ exec -draft 1s(\h+)$d ] + ## align the new star with the previous one + exec J1s^[^*]*(\*)& + } + ] + ] ] # Regions definition are the same between c++ and objective-c @@ -180,12 +236,12 @@ hook global WinSetOption filetype=(c|cpp|objc) %[ rmhooks window c-family-indent } - # cleanup trailing whitespaces when exiting insert mode - hook window InsertEnd .* -group c-family-hooks %{ try %{ exec -draft s^\h+$d } } - - hook window InsertChar \n -group c-family-indent _c-family-indent-on-new-line - hook window InsertChar \{ -group c-family-indent _c-family-indent-on-opening-curly-brace - hook window InsertChar \} -group c-family-indent _c-family-indent-on-closing-curly-brace + hook -group c-family-indent window InsertEnd .* _c-family-trim-autoindent + hook -group c-family-indent window InsertChar \n _c-family-indent-on-newline + hook -group c-family-indent window InsertChar \{ _c-family-indent-on-opening-curly-brace + hook -group c-family-indent window InsertChar \} _c-family-indent-on-closing-curly-brace + hook -group c-family-insert window InsertChar \} _c-family-insert-on-closing-curly-brace + hook -group c-family-insert window InsertChar \n _c-family-insert-on-newline alias window alt c-family-alternative-file @@ -195,6 +251,7 @@ hook global WinSetOption filetype=(c|cpp|objc) %[ hook global WinSetOption filetype=(?!(c|cpp|objc)$).* %[ rmhooks window c-family-hooks rmhooks window c-family-indent + rmhooks window c-family-insert unalias window alt c-family-alternative-file ] From 78c6b7c03daaed018ecd1a39deb7b1c6b6a75de5 Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Fri, 15 Jul 2016 10:29:19 +0300 Subject: [PATCH 2/2] Don't completely remove empty lines when fixing indents --- rc/core/c-family.kak | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rc/core/c-family.kak b/rc/core/c-family.kak index c28f112b..5d3a384b 100644 --- a/rc/core/c-family.kak +++ b/rc/core/c-family.kak @@ -37,7 +37,7 @@ hook global BufSetOption mimetype=text/x-objc %{ def -hidden _c-family-trim-autoindent %[ eval -draft -itersel %[ ## remove the line if it's empty when leaving the insert mode - try %[ exec ^\h+$ d ] + try %[ exec 1s^(\h+)$ d ] ] ] def -hidden _c-family-indent-on-newline %[ eval -draft -itersel %[