add vmb for vim9 and legacy

master
Totem4 Totem4 1 year ago
parent 1beba3e809
commit e769c881f9

@ -0,0 +1,150 @@
" Vimball Archiver by Charles E. Campbell
UseVimball
finish
autoload/banana.vim [[[1
109
" banana.vim - Convert Base Banana (https://git.lattuga.net/itec/banana) and back
" T4 - totem4@tropici.net
" Version: 0.1
vim9script
if exists("g:autoloaded_Banana") || &cp
finish
endif
g:autoloaded_Banana = 1
export def Enc()
call ReplSel(Dec2Ban(GetSel()))
enddef
export def EncLine()
call setline('.', substitute(getline('.'), '\d\d*', '\=toupper(Doban(submatch(0)))', 'g'))
enddef
export def Dec()
call ReplSel(Ban2Dec(GetSel()))
enddef
export def DecLine()
call setline('.', substitute(getline('.'), '\C\u\u*', '\=Unban(tolower(submatch(0)))', 'g'))
enddef
var dictionary = [['b', 'c', 'd', 'f', 'g', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'z'], ['a', 'e', 'i', 'o', 'u']]
var numdict = len(dictionary)
def Doban(num: string): string
var v = str2nr(num)
var st = ''
var l = 0
var i = (numdict - 1) % numdict
while ! (v == 0 && i == (numdict - 1) % numdict && l >= 0)
var r = v % len(dictionary[i])
v = v / len(dictionary[i])
st = dictionary[i][r] .. st
i = abs((i - 1) % numdict)
l = l + 1
endwhile
return st
enddef
def Unban(ban: string): string
if ! IsBanana(ban)
return toupper(ban)
endif
var v = 0
for i in range(len(ban))
var r = (numdict + i) % numdict
var p = index(dictionary[r], ban[i])
v = v * len(dictionary[r]) + p
endfor
return string(v)
enddef
def IsBanana(ban: string): bool
if (len(ban) % numdict) != 0
return 0
endif
for i in range(len(ban))
var r = (numdict + i) % numdict
if index(dictionary[r], ban[i]) == -1
return 0
endif
endfor
return 1
enddef
def GetSel(): string
var [lstart, cstart] = getcharpos("'<")[1 : 2]
var [lend, cend] = getcharpos("'>")[1 : 2]
var lines = getline(lstart, lend)
if len(lines) == 0
return ''
endif
lines[-1] = lines[-1][: cend - (&selection == 'inclusive' ? 1 : 2)]
lines[0] = lines[0][cstart - 1 : ]
return join(lines, "\n")
enddef
def ReplSel(str: string)
var [lstart, cstart] = getcharpos("'<")[1 : 2]
var [lend, cend] = getcharpos("'>")[1 : 2]
var lines = getline(lstart, lend)
var new_str = slice(lines[0], 0, cstart - 1) .. str .. lines[-1][cend : ]
var new_str_lst = split(new_str, '\n')
call setline(lstart, new_str_lst)
setcursorcharpos(lstart, cstart)
enddef
def Dec2Ban(text: string): string
return substitute(text, '\d\d*', '\=toupper(Doban(submatch(0)))', 'g')
enddef
def Ban2Dec(text: string): string
return substitute(text, '\C\u\u*', '\=Unban(tolower(submatch(0)))', 'g')
enddef
plugin/banana.vim [[[1
34
" banana.vim - Convert Base Banana (https://git.lattuga.net/itec/banana) and back
" T4 - totem4@tropici.net
" Version: 0.1
vim9script
if exists("g:loaded_Banana") || &cp
finish
endif
g:loaded_Banana = 1
var keepcpo = &cpo
set cpo&vim
if !hasmapto('<Plug>BananaEnc')
map <unique> <Leader>b <Plug>BananaEnc
endif
if !hasmapto('<Plug>BananaDec')
map <unique> <Leader>d <Plug>BananaDec
endif
import autoload 'banana.vim'
nnoremap <silent> <unique> <script> <Plug>BananaEnc
\ :call <SID>banana.EncLine()<CR>
nnoremap <silent> <unique> <script> <Plug>BananaDec
\ :call <SID>banana.DecLine()<CR>
vnoremap <silent> <unique> <script> <Plug>BananaEnc
\ :call <SID>banana.Enc()<CR>
vnoremap <silent> <unique> <script> <Plug>BananaDec
\ :call <SID>banana.Dec()<CR>
&cpo = keepcpo

@ -0,0 +1,145 @@
" Vimball Archiver by Charles E. Campbell
UseVimball
finish
autoload/banana.vim [[[1
107
" banana.vim - Convert Base Banana (https://git.lattuga.net/itec/banana) and back
" T4 - totem4@tropici.net
" Version: 0.1
if exists("g:autoloaded_Banana") || &cp
finish
endif
let g:autoloaded_Banana= 1
fun! banana#Enc() range
call s:ReplSel(s:Dec2Ban(s:GetSel()))
endfun!
fun! banana#EncLine()
call setline('.', substitute(getline('.'), '\d\d*', '\=toupper(s:Doban(submatch(0)))','g'))
endfun!
fun! banana#Dec() range
call s:ReplSel(s:Ban2Dec(s:GetSel()))
endfun!
fun! banana#DecLine()
call setline('.', substitute(getline('.'), '\C\u\u*', '\=s:Unban(tolower(submatch(0)))','g'))
endfun!
let s:dictionary = [['b', 'c', 'd', 'f', 'g', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'z'], ['a', 'e', 'i', 'o', 'u']]
let s:numdict = len(s:dictionary)
fun! s:Doban(num)
let l:v = a:num
let l:st = ''
let l:l = 0
let l:i = (s:numdict - 1) % s:numdict
while ! (l:v == 0 && l:i == (s:numdict -1) % s:numdict && l:l >= 0)
let l:r = l:v % len(s:dictionary[l:i])
let l:v = l:v / len(s:dictionary[l:i])
let l:st = s:dictionary[l:i][l:r] . l:st
let l:i = abs((l:i - 1) % s:numdict)
let l:l = l:l + 1
endwhile
return l:st
endfun!
fun! s:Unban(ban)
if ! s:IsBanana(a:ban)
return toupper(a:ban)
endif
let l:v = 0
for l:i in range(len(a:ban))
let l:r = (s:numdict + l:i) % s:numdict
let l:p = index(s:dictionary[l:r],a:ban[l:i])
let l:v = l:v * len(s:dictionary[l:r]) + l:p
endfor
return l:v
endfun!
fun! s:IsBanana(ban)
if (len(a:ban) % s:numdict) != 0
return 0
endif
for l:i in range(len(a:ban))
let l:r = (s:numdict + l:i) % s:numdict
if index(s:dictionary[l:r],a:ban[l:i]) == -1
return 0
endif
endfor
return 1
endfun!
fun! s:GetSel()
let [l:lstart, l:cstart] = getcharpos("'<")[1:2]
let [l:lend, l:cend] = getcharpos("'>")[1:2]
let l:lines = getline(l:lstart, l:lend)
if len(l:lines) == 0
return ''
endif
let l:lines[-1] = l:lines[-1][: l:cend - (&selection == 'inclusive' ? 1 : 2)]
let l:lines[0] = l:lines[0][l:cstart - 1:]
return join(l:lines, "\n")
endfun!
fun! s:ReplSel(str)
let [l:lstart, l:cstart] = getcharpos("'<")[1:2]
let [l:lend, l:cend] = getcharpos("'>")[1:2]
let l:lines = getline(l:lstart, l:lend)
let l:new_str = slice(l:lines[0],0,l:cstart - 1) . a:str . l:lines[-1][l:cend:]
let l:new_str_lst = split(l:new_str,'\n')
call setline(l:lstart ,l:new_str_lst)
call setcursorcharpos(lstart, cstart)
endfun!
fun! s:Dec2Ban(text)
return substitute(a:text, '\d\d*', '\=toupper(s:Doban(submatch(0)))', 'g')
endfun!
fun! s:Ban2Dec(text)
return substitute(a:text, '\C\u\u*', '\=s:Unban(tolower(submatch(0)))', 'g')
endfun!
plugin/banana.vim [[[1
31
" banana.vim - Convert Base Banana (https://git.lattuga.net/itec/banana) and back
" T4 - totem4@tropici.net
" Version: 0.1
if exists("g:loaded_Banana") || &cp
finish
endif
let g:loaded_Banana= 1
let s:keepcpo = &cpo
set cpo&vim
if !hasmapto('<Plug>BananaEnc')
map <unique> <Leader>b <Plug>BananaEnc
endif
if !hasmapto('<Plug>BananaDec')
map <unique> <Leader>d <Plug>BananaDec
endif
vnoremap <silent> <unique> <script> <Plug>BananaEnc
\ :call banana#Enc()<CR>
vnoremap <silent> <unique> <script> <Plug>BananaDec
\ :call banana#Dec()<CR>
nnoremap <silent> <unique> <script> <Plug>BananaEnc
\ :call banana#EncLine()<CR>
nnoremap <silent> <unique> <script> <Plug>BananaDec
\ :call banana#DecLine()<CR>
let &cpo= s:keepcpo
unlet s:keepcpo
Loading…
Cancel
Save