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.
128 lines
3.1 KiB
VimL
128 lines
3.1 KiB
VimL
1 year ago
|
if exists("g:loaded_Banana") || &cp
|
||
|
finish
|
||
|
endif
|
||
|
let g:loaded_Banana= 1 " your version number
|
||
|
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>t <Plug>BananaDec
|
||
|
endif
|
||
|
|
||
|
noremap <silent> <unique> <script> <Plug>BananaEnc
|
||
|
\ :call <SID>EncLine()<CR>
|
||
|
noremap <silent> <unique> <script> <Plug>BananaDec
|
||
|
\ :call <SID>DecLine()<CR>
|
||
|
|
||
|
|
||
|
fun! s:Enc()
|
||
|
:call s:ReplaceSelectedText(s:GetSelectedText())
|
||
|
endfun!
|
||
|
|
||
|
fun! s:EncLine()
|
||
|
:call setline('.', substitute(getline('.'), '[0-9][0-9]*', s:Doban(getline('.')),""))
|
||
|
endfun!
|
||
|
|
||
|
fun! s:DecLine()
|
||
|
":call setline('.', substitute(getline('.'), '([A-Z][A-Z])*', s:Unban(getline('.')),""))
|
||
|
:call setline('.', substitute(getline('.'), '[a-z][a-z]*', s:Unban(getline('.')),""))
|
||
|
endfun!
|
||
|
|
||
|
fun! s:GetSelectedText()
|
||
|
let [line_start, column_start] = getpos("'<")[1:2]
|
||
|
let [line_end, column_end] = getpos("'>")[1:2]
|
||
|
let lines = getline(line_start, line_end)
|
||
|
if len(lines) == 0
|
||
|
return ''
|
||
|
endif
|
||
|
let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)]
|
||
|
let lines[0] = lines[0][column_start - 1:]
|
||
|
return join(lines, "\n")
|
||
|
endfun!
|
||
|
|
||
|
fun! s:ReplaceSelectedText(new_str)
|
||
|
let [line_start, column_start] = getpos("'<")[1:2]
|
||
|
let [line_end, column_end] = getpos("'>")[1:2]
|
||
|
let lines = getline(line_start, line_end)
|
||
|
let new_str = lines[0][:column_start] . new_str . lines[-1][column_end + 1:]
|
||
|
let new_str_lst = split(new_str,"\n")
|
||
|
'<,'>s/.*/join(new_str_lst,"\n")/ge
|
||
|
delmarks < >
|
||
|
return new_str
|
||
|
endfun!
|
||
|
|
||
|
fun! s:Doban(num)
|
||
|
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)
|
||
|
let s:v = a:num
|
||
|
let s:st = ''
|
||
|
let s:l = 0
|
||
|
|
||
|
let s:i = (s:numdict - 1) % s:numdict
|
||
|
|
||
|
while ! (s:v == 0 && s:i == (s:numdict -1) % s:numdict && s:l >= 0)
|
||
|
let s:r = s:v % len(s:dictionary[s:i])
|
||
|
let s:v = s:v / len(s:dictionary[s:i])
|
||
|
let s:st = s:dictionary[s:i][s:r] . s:st
|
||
|
let s:i = abs((s:i - 1) % s:numdict)
|
||
|
let s:l = s:l + 1
|
||
|
endwhile
|
||
|
return s:st
|
||
|
endfun!
|
||
|
|
||
|
fun! s:Unban(ban)
|
||
|
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)
|
||
|
|
||
|
" if (len(a:ban) % s:numdict) != 0
|
||
|
" echo "Banana non valida"
|
||
|
" return a:ban
|
||
|
" endif
|
||
|
|
||
|
if ! s:IsBanana(a:ban)
|
||
|
echo "Banana non valida"
|
||
|
return a:ban
|
||
|
endif
|
||
|
|
||
|
let s:v = 0
|
||
|
for i in range(len(a:ban))
|
||
|
|
||
|
let s:r = (s:numdict + i) % s:numdict
|
||
|
let s:p = index(s:dictionary[s:r],a:ban[i])
|
||
|
|
||
|
" if s:p < 0
|
||
|
" echo "Carattere non valido in posizione" . i + 1
|
||
|
" return a:ban
|
||
|
" endif
|
||
|
|
||
|
let s:v = s:v * len(s:dictionary[s:r]) + s:p
|
||
|
endfor
|
||
|
|
||
|
return s:v
|
||
|
endfun!
|
||
|
|
||
|
fun! s:IsBanana(ban)
|
||
|
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)
|
||
|
if (len(a:ban) % s:numdict) != 0
|
||
|
return 0
|
||
|
endif
|
||
|
|
||
|
for i in range(len(a:ban))
|
||
|
let s:r = (s:numdict + i) % s:numdict
|
||
|
if index(s:dictionary[s:r],a:ban[i]) == -1
|
||
|
return 0
|
||
|
endif
|
||
|
endfor
|
||
|
|
||
|
return 1
|
||
|
endfun!
|
||
|
|
||
|
let &cpo= s:keepcpo
|
||
|
unlet s:keepcpo
|