|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import vim
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def dec2banana(num, dictstart = None, shiftend = None, minlength = None, dictionary = None):
|
|
|
|
#defaults
|
|
|
|
if dictstart is None: dictstart = 0
|
|
|
|
if shiftend is None: shiftend = 0
|
|
|
|
if minlength is None: minlength = 0
|
|
|
|
if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
|
|
|
|
|
|
|
|
numdict = len(dictionary)
|
|
|
|
v = num
|
|
|
|
st = ""
|
|
|
|
l = 0
|
|
|
|
|
|
|
|
i = (numdict - 1 + dictstart + shiftend) % numdict
|
|
|
|
while not (v == 0 and i == (numdict - 1 + dictstart) % numdict and l >= minlength):
|
|
|
|
r = v % len(dictionary[i])
|
|
|
|
v = int(v / len(dictionary[i]))
|
|
|
|
st = dictionary[i][r] + st
|
|
|
|
i = (i - 1) % numdict
|
|
|
|
l += 1
|
|
|
|
|
|
|
|
return(st)
|
|
|
|
|
|
|
|
|
|
|
|
def banana2dec(banana, dictstart = None, shiftend = None, dictionary = None):
|
|
|
|
#defaults
|
|
|
|
if dictstart is None: dictstart = 0
|
|
|
|
if shiftend is None: shiftend = 0
|
|
|
|
if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
|
|
|
|
|
|
|
|
numdict = len(dictionary)
|
|
|
|
if (len(banana) - shiftend) % numdict != 0:
|
|
|
|
return("Banana non valida")
|
|
|
|
v = 0
|
|
|
|
for i in range(len(banana)):
|
|
|
|
r = (numdict + i + dictstart) % numdict
|
|
|
|
try:
|
|
|
|
v = v * len(dictionary[r]) + dictionary[r].index(banana[i])
|
|
|
|
except:
|
|
|
|
return("Carattere non valido in posizione", i+1)
|
|
|
|
|
|
|
|
return(v)
|
|
|
|
|
|
|
|
def isbanana(banana, dictstart = None, shiftend = None, dictionary = None):
|
|
|
|
#defaults
|
|
|
|
if dictstart is None: dictstart = 0
|
|
|
|
if shiftend is None: shiftend = 0
|
|
|
|
if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
|
|
|
|
|
|
|
|
numdict = len(dictionary)
|
|
|
|
if (len(banana) - shiftend) % numdict != 0:
|
|
|
|
return(False)
|
|
|
|
for i in range(len(banana)):
|
|
|
|
r = (numdict + i + dictstart) % numdict
|
|
|
|
if banana[i] not in dictionary[r]:
|
|
|
|
return(False)
|
|
|
|
|
|
|
|
return(True)
|
|
|
|
|
|
|
|
def callback(x):
|
|
|
|
return "%s" % dec2banana(int(x[0], base=10)).upper()
|
|
|
|
|
|
|
|
def text2banana(text):
|
|
|
|
text = re.sub(r"[0-9][0-9]*",callback,text)
|
|
|
|
return text
|
|
|
|
|
|
|
|
def callback_dec(x):
|
|
|
|
b = x[0].strip().lower()
|
|
|
|
if isbanana(b):
|
|
|
|
return "%s" % banana2dec(b)
|
|
|
|
else:
|
|
|
|
return x[0]
|
|
|
|
|
|
|
|
def text2dec(text):
|
|
|
|
text = re.sub(r"[A-Z][A-Z]*",callback_dec,text)
|
|
|
|
return text
|
|
|
|
|
|
|
|
def get_selected_text():
|
|
|
|
buf = vim.current.buffer
|
|
|
|
(lnum1, col1) = buf.mark('<')
|
|
|
|
(lnum2, col2) = buf.mark('>')
|
|
|
|
lines = vim.eval('getline({}, {})'.format(lnum1, lnum2))
|
|
|
|
if len(lines) > 1:
|
|
|
|
lines[0] = lines[0][col1:]
|
|
|
|
lines[-1] = lines[-1][:col2]
|
|
|
|
else:
|
|
|
|
lines[0] = lines[0][col1:col2+1]
|
|
|
|
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
|
|
def replace_selected_text(new_str):
|
|
|
|
buf = vim.current.buffer
|
|
|
|
(lnum1, col1) = buf.mark('<')
|
|
|
|
(lnum2, col2) = buf.mark('>')
|
|
|
|
lines = vim.eval('getline({}, {})'.format(lnum1,lnum2))
|
|
|
|
new_str = lines[0][:col1] + new_str + lines[-1][col2+1:]
|
|
|
|
new_str_list = new_str.split('\n')
|
|
|
|
del buf[lnum1-1:lnum2]
|
|
|
|
buf.append(new_str_list, lnum1-1)
|
|
|
|
vim.command("delmarks < >")
|
|
|
|
|
|
|
|
return new_str
|
|
|
|
|
|
|
|
def enc_line():
|
|
|
|
vim.current.line = re.sub(r"[0-9][0-9]*",callback,vim.current.line)
|
|
|
|
|
|
|
|
def dec_line():
|
|
|
|
vim.current.line = re.sub(r"[A-Z][A-Z]*",callback_dec,vim.current.line)
|