Fix the autowrap script, use an external tool to wrap paragraphs

This commit is contained in:
Frank LENORMAND 2016-07-08 13:50:47 +03:00
parent 88a9607552
commit a605497553

View File

@ -1,39 +1,38 @@
## Maximum amount of characters per line # Maximum amount of characters per line
decl int autowrap_column 80 decl int autowrap_column 80
# remove the whitespaces selected by `autowrap-line` # If enabled, paragraph formatting will reformat the whole paragraph in which characters are being inserted
# and handle "recursive" calls when needed # This can potentially break formatting of documents containing markup (e.g. markdown)
def -hidden _autowrap-cut-selection %{ decl bool autowrap_format_paragraph yes
try %{ # Command to which the paragraphs to wrap will be passed, all occurences of '%c' are replaced with `autowrap_column`
# remove the whitespaces decl str autowrap_fmtcmd 'fmt -s -w %c'
# then save the indentation of the original line and apply it to the new one
exec -draft c<ret><esc> K <a-&>
# if there's text after the current line, merge the two
exec xX <a-k>[^\n]\n[^\n]<ret> <a-j>
}
eval autowrap-line
}
def autowrap-line -docstring "Wrap the current line" %{ eval -draft %{ def -hidden autowrap-cursor %{ eval -draft %{
try %{ try %{
# check that the line is too long and has to be wrapped ## if the line isn't too long, do nothing
exec "<a-x> s^(?=.*\h).{%opt{autowrap_column}}[^\n]<ret>" exec -draft "<a-x><a-k>^[^\n]{%opt{autowrap_column},}<ret>"
try %{ try %{
# either select the trailing whitespaces, or the ones before the last word of the line ## if we're adding characters past the limit, just wrap them around
exec s\h+(?=[^\h]+\')|\h+\'<ret> exec "<a-h><a-k>.{%opt{autowrap_column},}<ret>1s(\h+)[^\h]+\'<ret>c<ret><esc>"
eval _autowrap-cut-selection } catch %{
} catch %{ try %{ ## if we're adding characters in the middle of a sentence, use
# wrap the line on the first whitespace that's past the column limit ## the `fmtcmd` command to wrap the entire paragraph
exec "x s^[^\h]{%opt{autowrap_column},}\K\h+<ret>" %sh{
eval _autowrap-cut-selection if [[ "${kak_opt_autowrap_format_paragraph}" = true ]] \
} } && [[ -n "${kak_opt_autowrap_fmtcmd}" ]]; then
format_cmd=$(printf %s "${kak_opt_autowrap_fmtcmd}" \
| sed -e "s/%c/${kak_opt_autowrap_column}/g" \
-e 's/ /<space>/g')
printf %s "exec -draft <a-i>p|${format_cmd}<ret>"
fi
}
}
} }
} } } }
def autowrap-enable -docstring "Wrap the lines in which characters are inserted" %{ def autowrap-enable -docstring "Wrap the lines in which characters are inserted" %{
hook -group autowrap window InsertChar [^\n] %{ hook -group autowrap window InsertChar [^\n] autowrap-cursor
eval autowrap-line
}
} }
def autowrap-disable -docstring "Disable automatic line wrapping" %{ def autowrap-disable -docstring "Disable automatic line wrapping" %{