kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

autowrap.kak (2209B)


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