You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.4 KiB
VimL
110 lines
2.4 KiB
VimL
1 year ago
|
" 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()
|
||
1 year ago
|
call ReplSel(Dec2Ban(GetSel()))
|
||
1 year ago
|
enddef
|
||
|
|
||
1 year ago
|
export def EncLine()
|
||
1 year ago
|
call setline('.', substitute(getline('.'), '\d\d*', '\=toupper(Doban(submatch(0)))', 'g'))
|
||
|
enddef
|
||
|
|
||
|
export def Dec()
|
||
1 year ago
|
call ReplSel(Ban2Dec(GetSel()))
|
||
1 year ago
|
enddef
|
||
|
|
||
1 year ago
|
export def DecLine()
|
||
1 year ago
|
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)
|
||
1 year ago
|
setcursorcharpos(lstart, cstart)
|
||
1 year ago
|
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
|