From 8d7ddfdf8b5cebac0679ddf72017d667c6063eb5 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 17 Dec 2014 13:58:05 +0000 Subject: [PATCH] Only remove trailing whitespaces on InsertEnd when last insert was auto indent Fixes #242 --- rc/c-family.kak | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/rc/c-family.kak b/rc/c-family.kak index a3106c8df..ff651b711 100644 --- a/rc/c-family.kak +++ b/rc/c-family.kak @@ -14,8 +14,10 @@ hook global BufSetOption mimetype=text/x-objc %{ set buffer filetype objc } +decl -hidden bool _c_family_last_change_was_indent false + def -hidden _c-family-indent-on-new-line %~ - eval -draft -itersel %_ + eval -draft -itersel %= # preserve previous line indent try %{ exec -draft \;K } # indent after lines ending with { or ( @@ -32,7 +34,8 @@ def -hidden _c-family-indent-on-new-line %~ 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 ] - _ + set buffer _c_family_last_change_was_indent true + = ~ def -hidden _c-family-indent-on-opening-curly-brace %[ @@ -86,8 +89,15 @@ addhl -group /objc/code regex "@(property|synthesize|interface|implementation|pr addhl -group /objc/code regex "\<(IBAction|IBOutlet)\>" 0:attribute hook global WinSetOption filetype=(cpp|objc) %[ + hook window BufInsert .* -group c-family-hooks %{ set buffer _c_family_last_change_was_indent false } + hook window BufErase .* -group c-family-hooks %{ set buffer _c_family_last_change_was_indent false } + # cleanup trailing whitespaces when exiting insert mode - hook window InsertEnd .* -group c-family-hooks %{ try %{ exec -draft s\h+$d } } + hook window InsertEnd .* -group c-family-hooks %{ %sh{ + if [ $kak_opt__c_family_last_change_was_indent = "true" ]; then + echo 'try %{ exec -draft s\h+$d }' + fi + } } 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