playground/vimscript/five.vim

62 lines
1.8 KiB
VimL

" Vim global plugin for limiting the width of text
" Last Change: 2021 Feb 22
" Maintainer: Julin S
" License: MIT License
if exists("g:loaded_mailify")
finish
endif
let g:loaded_mailify = 1
if !hasmapto('<Plug>MailifyAdd')
" <unique> would cause error if the key is already bound
xnoremap <unique> <Leader>m :call s:Mailify() <Plug>MailifyMailify
endif
function Mailify()
py3 << EOF
from typing import List
import re
import vim
def mailify(lines: "vim.buffer", limit: int = 80) -> List[str]:
rv = []
for line in lines:
if not line or line.isspace():
rv.append(line)
while line:
endidx = None
linelen = len(line)
if linelen < limit:
endidx = linelen
else:
for idx in range(limit-1, -1, -1):
if line[idx] == " ":
endidx = idx
break
if endidx is None:
for idx in range(limit, linelen):
if line[idx] == " ":
endidx = idx
break
if endidx is None:
enidx = linelen
rv.append(line[:endidx])
line = line[endidx+1:]
return rv
curbuf = vim.current.buffer
(startline, _) = curbuf.mark("<")
(endline, _) = curbuf.mark(">")
mailified_lines = mailify(curbuf[startline-1: endline])
del curbuf[startline-1: endline]
curbuf.append(mailified_lines, startline-1)
EOF
endfunction
"xnoremap mmm :call Mailify()<cr>
" <unique> would cause error if the key is already bound
xnoremap <unique> <Leader>m :call Mailify()<cr>