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.

101 lines
2.5 KiB
Python

#!/usr/bin/env python3
import vim
import re
def dec2banana(num):
dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
numdict = len(dictionary)
v = num
st = ""
l = 0
i = (numdict - 1) % numdict
while not (v == 0 and i == (numdict - 1) % numdict and l >= 0):
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):
dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
numdict = len(dictionary)
if len(banana) % numdict != 0:
return("Banana non valida")
v = 0
for i in range(len(banana)):
r = (numdict + i) % 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):
dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
numdict = len(dictionary)
if len(banana) % numdict != 0:
return(False)
for i in range(len(banana)):
r = (numdict + i) % 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):
return re.sub(r"[0-9][0-9]*",callback,text)
def callback_dec(x):
b = x[0].strip().lower()
if isbanana(b):
return "%s" % banana2dec(b)
else:
return x[0]
def text2dec(text):
return re.sub(r"[A-Z][A-Z]*",callback_dec,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)