欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

[20191031]完善vim的bccalc插件7.txt

程序员文章站 2022-05-09 12:02:38
[20191031]完善vim的bccalc插件7.txt--//增加/ 2^16 %2^16功能,输入\tx,例子:1398145029 = /2^16 %2^16 (type and mode) = 21334,5 = 0x535600050x53560005 = /2^16 %2^16 (ty ......

[20191031]完善vim的bccalc插件7.txt

--//增加/ 2^16 %2^16功能,输入\tx,例子:
1398145029 = /2^16  %2^16 (type and mode) = 21334,5 = 0x53560005
0x53560005 = /2^16  %2^16 (type and mode) = 21334,5 = 1398145029
--//5356 =sv(对应asc码sv)

--//增加处理输入逗号过滤问题,这个有点麻烦的是有时候逗号是合法的。单独增加快捷使用\c,
--//而\bc的功能不动。

noremap   <leader>c, yp!!sed "s/,//g" \|bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<cr>ka = <esc>j

12,345+1,000 = 13345  --//输入\c.

--//代码如下bccalc_win.vim:

"" calculate expression entered on command line and give answer, e.g.:
" :calculate sin (3) + sin (4) ^ 2
command! -nargs=+ calculate echo "<args> = " . calculate ("<args>",0)

"" calculate expression from selection, pick a mapping, or use the leader form
vnoremap ;bc "ey`>:call calclines(0)<cr>
vnoremap ;bb "ey`>:call calclines(0)<cr>

vnoremap ;10 "ey`>:call calclines(10)<cr>
vnoremap ;16 "ey`>:call calclines(16)<cr>

vnoremap ;tx "ey`>:call calclines(1616)<cr>

vnoremap ;22 "ey`>:call calclines(22)<cr>
vnoremap ;dba "ey`>:call calclines(22)<cr>

vnoremap ;32 "ey`>:call calclines(32)<cr>
vnoremap ;scn "ey`>:call calclines(32)<cr>

vnoremap ;ss "ey`>:call calclines(10016)<cr>
vnoremap ;rr "ey`>:call calclines(20016)<cr>
vnoremap ;hd "ey`>:call calclines(30016)<cr>
vnoremap ;hh "ey`>:call calclines(30016)<cr>
vnoremap ;dh "ey`>:call calclines(30016)<cr>

"" calculate expression on current line, pick a mapping, or use the leader
nnoremap  <leader>bx <esc>a <esc>"eyy$:call calclines(0)<cr>
nnoremap  <leader>bc <esc>a = <esc>"eyy:call calclines(0)<cr>
nnoremap  <leader>bb <esc>a = <esc>"eyy:call calclines(0)<cr>
"noremap   <leader>cc yp!!bc -lq<cr>ka = <esc>j
noremap   <leader>cc yp!!bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<cr>ka = <esc>j
noremap   <leader>c, yp!!sed "s/,//g" \|bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<cr>ka = <esc>j

" convert hexdecimal to decimal
nnoremap  <leader>10 <esc>a = <esc>"eyy:call calclines(10)<cr>

" convert decimal to hexdecimal
nnoremap  <leader>16 <esc>a = <esc>"eyy:call calclines(16)<cr>

" split event p1 to type and mode
nnoremap  <leader>tx  <esc>a = <esc>"eyy:call calclines(1616)<cr>

" split dba(10) or dba(16) to file# and block#
nnoremap  <leader>22  <esc>a = <esc>"eyy:call calclines(22)<cr>
nnoremap  <leader>dba <esc>a = <esc>"eyy:call calclines(22)<cr>

" split scn(10) or scn(16) into scn_wrap,scn_base
nnoremap  <leader>32  <esc>a = <esc>"eyy:call calclines(32)<cr>
nnoremap  <leader>scn <esc>a = <esc>"eyy:call calclines(32)<cr>

" convert scn_wrap,scn_base(10) or scn_wrap,scn_base(16) to 10 or 16 base
nnoremap  <leader>ss <esc>a = <esc>"eyy:call calclines(10016)<cr>

" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
nnoremap  <leader>rr <esc>a = <esc>"eyy:call calclines(20016)<cr>

" convert hexdecimal to decimal or decimal to hexdecimal
nnoremap  <leader>hd <esc>a = <esc>"eyy:call calclines(30016)<cr>
nnoremap  <leader>hh <esc>a = <esc>"eyy:call calclines(30016)<cr>
nnoremap  <leader>dh <esc>a = <esc>"eyy:call calclines(30016)<cr>

"" calculate from insertmode
inoremap =: = <esc>"eyy:call calclines(0)<cr>a

" ---------------------------------------------------------------------
"  calculate:
"    clean up an expression, pass it to bc, return answer
function! calculate (s,flag)

    let has_hex = 0
    let str = a:s

    " remove newlines and trailing spaces
    let str = substitute (str, "\n",   "", "g")
    let str = substitute (str, '\s*$', "", "g")

    " sub common func names for bc equivalent
    let str = substitute (str, '\csin\s*(',  's (', 'g')
    let str = substitute (str, '\ccos\s*(',  'c (', 'g')
    let str = substitute (str, '\catan\s*(', 'a (', 'g')
    let str = substitute (str, "\cln\s*(",   'l (', 'g')
    let str = substitute (str, '\clog\s*(',  'l (', 'g')
    let str = substitute (str, '\cexp\s*(',  'e (', 'g')

    " alternate exponitiation symbols
    let str = substitute (str, '\*\*', '^', "g")
    let str = substitute (str, '`', '^',    "g")
    let str = substitute (str, '\^', '^^^^',    "g")

    " escape chars for shell
    if has("unix")
        let str = escape (str, '*();&><|^')
    endif

    let preload = exists ("g:bccalc_preload") ? g:bccalc_preload : ""

    " run bc
    " return str
    " let answer = system ("echo " . str . " \| bc -l " . preload)

    if a:flag == 0
         let answer = system ("echo " . str . " \| bc -l " . preload)
         " let answer = answer . " --- ". str
    endif

    if a:flag == 10
        let str = toupper (str)
        let str = substitute (str, "0x", "", "g")
        let answer = system ("echo ibase=16 ;" . str .  " \| bc -l " . preload)
    endif

    if a:flag == 16
        let answer = system ("echo obase=16 ;" . str .  " \| bc -l " . preload)
        let answer = "0x" . tolower ( answer )
    endif

    let has_hex = check_hex(str)

    if a:flag == 1616
        if has_hex == 1
            let str = toupper (str)
            let str = substitute (str, "0x", "", "g")
            " 0x10000 hexdecimal = 65536 (10) = 2^16(10)
            let answer  = system ("echo ibase=16 ;" . str . "/10000" . " \| bc " . preload)
            let answer1 = system ("echo ibase=16 ;" . str . "%10000" . " \| bc " . preload)
            let answer2 = system ("echo ibase=16 ;" . str .  " \| bc -l " . preload)
        else
            let answer  = system ("echo " . str . "/65536" . " \| bc " . preload)
            let answer1 = system ("echo " . str . "%65536" . " \| bc " . preload)
            let answer2 = "0x" . system ("echo obase=16 ;" . str .  " \| bc -l " . preload)
        endif
        let answer = "/2^16  %2^16 (type | mode) = " . answer . "," . answer1 ." = " . tolower(answer2)
    endif

    if a:flag == 22
        if has_hex == 1
            let str = toupper (str)
            let str = substitute (str, "0x", "", "g")
            " 0x400000 hexdecimal = 4194304 (10) = 2^22(10)
            let answer  = system ("echo ibase=16 ;" . str . "/400000" . " \| bc " . preload)
            let answer1 = system ("echo ibase=16 ;" . str . "%400000" . " \| bc " . preload)
            let answer2 = system ("echo ibase=16 ;" . str .  " \| bc -l " . preload)
        else
            let answer  = system ("echo " . str . "/4194304" . " \| bc " . preload)
            let answer1 = system ("echo " . str . "%4194304" . " \| bc " . preload)
            let answer2 = "0x" . system ("echo obase=16 ;" . str .  " \| bc -l " . preload)
        endif
        " let answer = "set dba " . answer . "," . answer1
        let answer = "set dba " . answer . "," . answer1 ." = alter system dump datefile " . answer . " block " . answer1 ." = " . tolower(answer2)
    endif

    if a:flag == 32
        if has_hex == 1
            let str = toupper (str)
            let str = substitute (str, "0x", "", "g")
            " 0x100000000 hexdecimal = 4294967296(10) = 2^32(10)
            let answer  = system ("echo ibase=16 ;" . str . "/100000000" . " \| bc " . preload)
            let answer1 = system ("echo ibase=16 ;" . str . "%100000000" . " \| bc " . preload)
            let answer2 = system ("echo obase=16 ;ibase=16 ;" . str . "/100000000" . " \| bc " . preload)
            let answer3 = system ("echo obase=16 ;ibase=16 ;" . str . "%100000000" . " \| bc " . preload)
        else
            let answer  = system ("echo " . str . "/4294967296" . " \| bc " . preload)
            let answer1 = system ("echo " . str . "%4294967296" . " \| bc " . preload)
            let answer2 = system ("echo obase=16 ;" . str . "/4294967296" . " \| bc " . preload)
            let answer3 = system ("echo obase=16 ;" . str . "%4294967296" . " \| bc " . preload)
        endif
        let answer = "scn_wrap,scn_base(10): " . answer . "," . answer1 . " = scn_wrap,scn_base(16): " . "0x" . tolower (answer2) . "," . "0x" . tolower(answer3)
    endif

    if a:flag == 10016
        if has_hex == 1
            let str = toupper (str)
            let str = substitute (str, "0x", "", "g")
            " 0x100000000 hexdecimal = 4294967296(10) = 2^32(10)
            let str = substitute (str, "[,.]", "*100000000+", "g")
            let answer  = system ("echo obase=10 ;ibase=16 ;" . str .  " \| bc -l " . preload)
            let answer1 = system ("echo obase=16 ;ibase=16 ;" . str .  " \| bc -l " . preload)
        else
            let str = substitute (str, "[,.]", "*4294967296+", "g")
            let answer  = system ("echo " . str . " \| bc -l " . preload)
            let answer1 = system ("echo obase=16 ;" . str .  " \| bc -l " . preload)
        endif
        let answer = "scn(10): " . answer . " = scn(16): " . "0x" . tolower (answer1)
    endif

    if a:flag == 20016
        if has_hex == 1
            let str = toupper ( str )
            let str = substitute (str, "0x", "", "g")
            " 0x400000 hexdecimal = 4194304 (10) = 2^22(10)
            let str = substitute (str, "[,.]", "*400000+", "g")
            let answer  = system ("echo obase=10 ;ibase=16 ;" . str .  " \| bc -l " . preload)
            let answer1 = system ("echo obase=16 ;ibase=16 ;" . str .  " \| bc -l " . preload)
        else
            let str = substitute (str, "[,.]", "*4194304+", "g")
            let answer  = system ("echo " . str . " \| bc -l " . preload)
            let answer1 = system ("echo obase=16 ;" . str .  " \| bc -l " . preload)
        endif
        let answer = "file#,block# dba(10): " . answer . " = file#,block# dba(16): " . "0x" . tolower (answer1)
    endif

    if a:flag == 30016
        if has_hex == 1
            let str = substitute (str, "0x", "", "g")
            let str = toupper ( str )
            let answer = system ("echo ibase=16 ;" . str .  " \| bc -l " . preload)
        else
            let answer = system ("echo obase=16 ;" . str .  " \| bc -l " . preload)
            let answer = "0x" . tolower ( answer )
        endif
    endif

    " strip newline and \
    let answer = substitute (answer, "[\\\n]", "", "g")

    " strip trailing 0s in decimals
    let answer = substitute (answer, '\.\(\d*[1-9]\)0\+$', '.\1', "")
    let answer = substitute (answer, '\.0\+$', '', "")

    return answer
endfunction

" ---------------------------------------------------------------------
" calclines:
"
" take expression from lines, either visually selected or the current line,
" pass to calculate function, echo or past answer after '='
function! calclines(flag)

    let has_equal = 0

    " remove newlines and trailing spaces
    let @e = substitute (@e, "\n", "",   "g")
    let @e = substitute (@e, '\s*$', "", "g")

    " if we end with an equal, strip, and remember for output
    if @e =~ "=$"
        let @e = substitute (@e, '=$', "", "")
        let has_equal = 1
    endif

    " if there is another equal in the line, assume chained equations, remove
    " leading ones
    let @e = substitute (@e, '^.\+=', '', '')

    let answer = calculate (@e,a:flag)

    " append answer or echo
    if has_equal == 1
        exec "normal a" . answer
    else
        echo "answer = " . answer
    endif
endfunction

" ---------------------------------------------------------------------
" check_hex:
"
" check if the string contains 0x, a, b, c, d, e, f  return has_hex=1
function! check_hex(str)
    let has_hex = 0
    let ss = a:str
    let ss = tolower ( ss )

    if ss =~ "0x"
        let has_hex = 1
        return has_hex
    endif

    if ss =~ "[abcdef]"
        let has_hex = 1
        return has_hex
    endif

endfunction