diff --git a/rc/autowrap.kak b/rc/autowrap.kak index 3ac3b506..355ba7e4 100644 --- a/rc/autowrap.kak +++ b/rc/autowrap.kak @@ -1,8 +1,7 @@ ## Maximum amount of characters per line decl int autowrap_column 80 -## Automatically wrap the selection -def autowrap-selection -docstring "Wrap the selection" %{ +def -hidden _autowrap-break-line %{ try %{ ## : ensure that the cursor is located after the anchor, then reverse the ## selection (make sure the cursor is at the beginning of the selection) @@ -10,21 +9,34 @@ def autowrap-selection -docstring "Wrap the selection" %{ ## the selection is wrap-able (contains at least an horizontal space, any ## non-whitespace character, and at least "autowrap_column" characters) ## %opt{autowrap_column}l: place the cursor on the "autowrap_column"th character - ## w;i: move the cursor to the beginning of the word (if it overslaps + ## w;i: move the cursor to the beginning of the word (if it overlaps ## the "autowrap_column"th column), or do nothing if the "autowrap_column"th ## character is a horizontal space, and insert a newline - ## kxXs\h+$d : select the line that we just made, as well as the one that was just - ## wrapped, and remove any trailing horizontal space - exec -draft "(?=[^\n]*\h)(?=[^\n]*[^\h])[^\n]{%opt{autowrap_column},}[^\n]%opt{autowrap_column}lw;ikxXs\h+$d " + ## :_autowrap-break-line: select the second half of the buffer that was just split, + ## and call itself recursively until all lines in the selection + ## are correctly split + exec -draft " + (?=[^\n]*\h)(?=[^\n]*[^\h])[^\n]{%opt{autowrap_column},}[^\n] + %opt{autowrap_column}l + w;i + :_autowrap-break-line" + } +} + +## Automatically wrap the selection +def autowrap-selection -docstring "Wrap the selection" %{ + eval -draft _autowrap-break-line + + try %{ + exec -draft "^[^\n]*\h*\n\h*[^\n]+$ + s\h*\n\h*(?=[^\z])c" } } ## Add a hook that will wrap the entire line every time a key is pressed def autowrap-enable -docstring "Wrap the lines in which characters are inserted" %{ hook -group autowrap window InsertChar [^\n] %{ - try %{ - exec -draft ":autowrap-selection" - } + exec -draft ":autowrap-selection" } }