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