parent
2fca008599
commit
df4b8a9ce5
@ -0,0 +1,195 @@
|
||||
"------------------------------------------------------------------------------
|
||||
" Exit when your app has already been loaded (or "compatible" mode set)
|
||||
if exists("g:loaded_Banana") || &cp
|
||||
finish
|
||||
endif
|
||||
let g:loaded_Banana= 1 " your version number
|
||||
let s:keepcpo = &cpo
|
||||
set cpo&vim
|
||||
|
||||
" Public Interface:
|
||||
" AppFunction: is a function you expect your users to call
|
||||
" PickAMap: some sequence of characters that will run your AppFunction
|
||||
" Repeat these three lines as needed for multiple functions which will
|
||||
" be used to provide an interface for the user
|
||||
if !hasmapto('<Plug>BananaEnc')
|
||||
map <unique> <Leader>b <Plug>BananaEnc
|
||||
endif
|
||||
|
||||
if !hasmapto('<Plug>BananaDec')
|
||||
map <unique> <Leader>d <Plug>BananaDec
|
||||
endif
|
||||
"
|
||||
"" Global Maps:
|
||||
""
|
||||
"noremap <silent> <unique> <script> <Plug>BananaPybanana
|
||||
" \ :set lz<CR>:call <SID>Pybanana()<CR>:set nolz<CR>
|
||||
noremap <silent> <unique> <script> <Plug>BananaEnc
|
||||
\ :call <SID>Enc()<CR>
|
||||
noremap <silent> <unique> <script> <Plug>BananaDec
|
||||
\ :call <SID>Dec()<CR>
|
||||
|
||||
"command! Banana call s:PyBanana()
|
||||
"nnoremap ;b :Banana<CR>
|
||||
"vnoremap ;b :<C-U>Banana<CR>
|
||||
"
|
||||
"------------------------------------------------------------------------------
|
||||
" s:AppFunction: this function is available via the <Plug>/<script>
|
||||
" interface above
|
||||
"
|
||||
"fun! s:Pybanana()
|
||||
"python3 << EOL
|
||||
"import vim
|
||||
"import re
|
||||
"
|
||||
"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_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
|
||||
" print(text)
|
||||
"
|
||||
"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 get_selected_text():
|
||||
" buf = vim.current.buffer
|
||||
" (lnum1, col1) = buf.mark('<')
|
||||
" (lnum2, col2) = buf.mark('>')
|
||||
" lines = vim.eval('getline({}, {})'.format(lnum1, lnum2))
|
||||
" lines[0] = lines[0][col1:]
|
||||
" lines[-1] = lines[-1][:col2]
|
||||
"
|
||||
" 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)
|
||||
" return new_str
|
||||
"
|
||||
"replace_selected_text(text2banana(get_selected_text()))
|
||||
"
|
||||
"EOL
|
||||
"
|
||||
" " your script function can set up maps to internal functions
|
||||
" "nnoremap <silent> <Left> :set lz<CR>:silent! call
|
||||
" "<SID>AppFunction2()<CR>:set nolz<CR>
|
||||
"
|
||||
" " your app can call functions in its own script and not worry
|
||||
" " about name
|
||||
" " clashes by preceding those function names with <SID>
|
||||
" "call s:InternalAppFunction(...)
|
||||
"
|
||||
" " or you could call it with
|
||||
" "call s:InternalAppFunction(...)
|
||||
"endfun
|
||||
|
||||
python3 import sys
|
||||
python3 import vim
|
||||
python3 sys.path.append(vim.eval('expand("<sfile>:h")'))
|
||||
|
||||
fun! s:Enc()
|
||||
python3 << EOL
|
||||
from banana import text2banana,get_selected_text,replace_selected_text
|
||||
|
||||
replace_selected_text(text2banana(get_selected_text()))
|
||||
|
||||
EOL
|
||||
endfun!
|
||||
|
||||
fun! s:Dec()
|
||||
python3 << EOL
|
||||
from banana import text2dec,get_selected_text,replace_selected_text
|
||||
|
||||
replace_selected_text(text2dec(get_selected_text()))
|
||||
|
||||
EOL
|
||||
endfun!
|
||||
|
||||
|
||||
"------------------------------------------------------------------------------
|
||||
" s:InternalAppFunction: this function cannot be
|
||||
" called from outside the
|
||||
" script, and its name won't clash with whatever else
|
||||
" the user has loaded
|
||||
"fun! s:InternalAppFunction(...)
|
||||
"
|
||||
" ..whatever..
|
||||
"endfun
|
||||
"
|
||||
"
|
||||
"------------------------------------------------------------------------------
|
||||
let &cpo= s:keepcpo
|
||||
unlet s:keepcpo
|
@ -0,0 +1,107 @@
|
||||
#!/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))
|
||||
lines[0] = lines[0][col1:]
|
||||
lines[-1] = lines[-1][:col2]
|
||||
|
||||
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)
|
||||
return new_str
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Ciao sono la libreria banana")
|
Loading…
Reference in New Issue