2017-11-03 10:34:41 +03:00
|
|
|
declare-option -docstring "maximum amount of characters per line, after which a newline character will be inserted" \
|
2017-05-16 14:35:43 +03:00
|
|
|
int autowrap_column 80
|
2015-07-24 16:00:29 +03:00
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
declare-option -docstring %{when enabled, paragraph formatting will reformat the whole paragraph in which characters are being inserted
|
2017-05-16 14:35:43 +03:00
|
|
|
This can potentially break formatting of documents containing markup (e.g. markdown)} \
|
|
|
|
bool autowrap_format_paragraph no
|
2017-11-03 10:34:41 +03:00
|
|
|
declare-option -docstring %{command to which the paragraphs to wrap will be passed
|
2017-05-16 14:35:43 +03:00
|
|
|
all occurences of '%c' are replaced with `autowrap_column`} \
|
|
|
|
str autowrap_fmtcmd 'fold -s -w %c'
|
2015-09-16 11:58:47 +03:00
|
|
|
|
2017-11-03 11:09:45 +03:00
|
|
|
define-command -hidden autowrap-cursor %{ evaluate-commands -save-regs '/"|^@m' %{
|
2015-09-16 11:58:47 +03:00
|
|
|
try %{
|
2016-07-08 13:50:47 +03:00
|
|
|
## if the line isn't too long, do nothing
|
2017-11-03 11:09:45 +03:00
|
|
|
execute-keys -draft "<a-x><a-k>^[^\n]{%opt{autowrap_column},}[^\n]<ret>"
|
2016-07-08 13:50:47 +03:00
|
|
|
|
2016-03-13 10:47:17 +03:00
|
|
|
try %{
|
2016-07-14 16:40:08 +03:00
|
|
|
reg m "%val{selections_desc}"
|
|
|
|
|
2016-07-08 13:50:47 +03:00
|
|
|
## if we're adding characters past the limit, just wrap them around
|
2017-11-03 11:09:45 +03:00
|
|
|
execute-keys -draft "<a-h><a-k>.{%opt{autowrap_column}}\h*[^\s]*<ret>1s(\h+)[^\h]*\z<ret>c<ret>"
|
2016-07-08 13:50:47 +03:00
|
|
|
} catch %{
|
|
|
|
## if we're adding characters in the middle of a sentence, use
|
|
|
|
## the `fmtcmd` command to wrap the entire paragraph
|
2018-05-07 00:29:52 +03:00
|
|
|
evaluate-commands %sh{
|
2016-09-24 12:22:09 +03:00
|
|
|
if [ "${kak_opt_autowrap_format_paragraph}" = true ] \
|
|
|
|
&& [ -n "${kak_opt_autowrap_fmtcmd}" ]; then
|
2016-07-08 13:50:47 +03:00
|
|
|
format_cmd=$(printf %s "${kak_opt_autowrap_fmtcmd}" \
|
2016-07-14 16:40:08 +03:00
|
|
|
| sed "s/%c/${kak_opt_autowrap_column}/g")
|
|
|
|
printf %s "
|
2017-11-03 11:09:45 +03:00
|
|
|
evaluate-commands -draft %{
|
|
|
|
execute-keys '<a-]>p<a-x><a-j>|${format_cmd}<ret>'
|
|
|
|
try %{ execute-keys s\h+$<ret> d }
|
2016-09-24 12:22:09 +03:00
|
|
|
}
|
2018-06-03 07:46:44 +03:00
|
|
|
select '${kak_main_reg_m}'
|
2016-07-14 16:40:08 +03:00
|
|
|
"
|
2016-07-08 13:50:47 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 11:48:07 +03:00
|
|
|
}
|
2016-03-13 10:47:17 +03:00
|
|
|
} }
|
2015-07-29 11:48:07 +03:00
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command autowrap-enable -docstring "Automatically wrap the lines in which characters are inserted" %{
|
2016-07-08 13:50:47 +03:00
|
|
|
hook -group autowrap window InsertChar [^\n] autowrap-cursor
|
2015-07-24 16:00:29 +03:00
|
|
|
}
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command autowrap-disable -docstring "Disable automatic line wrapping" %{
|
2017-01-04 03:07:45 +03:00
|
|
|
remove-hooks window autowrap
|
2015-07-24 16:00:29 +03:00
|
|
|
}
|