[20190909]完善vim的bccacl插件.txthttp://blog.itpub.net/267265/viewspace-2140886/http://blog.itpub.net/267265/viewspace-2140823/http://blog.itpub.net/267265 ...
[20190909]完善vim的bccacl插件.txt
http://blog.itpub.net/267265/viewspace-2140886/
http://blog.itpub.net/267265/viewspace-2140823/
http://blog.itpub.net/267265/viewspace-2140602/
http://blog.itpub.net/267265/viewspace-2142560/=>[20170725]vim調用bccalc插件問題.txt
--//我個人很喜歡在vim調用bc做各種計算,使用插件bccale,參考前面的鏈接.一位網友建議我做一些完善,要求能智能判斷輸入變數
--//是10進位還是16進位,實際上一些情況無法判斷.
--//實際上當時發現存在一些小bug,我很少使用它做計算,特別是小數點的運算。
1/30000*325=.0183333333333333225
--//註不需要輸入等號,打入\bc.顯示結果.
--//而我在bc -l下執行如下:
1/30000*325
.01083333333333333225
--//上個星期6,7再次檢查,發現這個bug實際上很簡單,原作者做了刪除尾部0的操作。
" strip trailing 0s in decimals
" let answer = substitute (answer, '\.\(\d*[1-9]\)0\+', '.\1', "")
--//應該修改如下,正則表達式要加入$,這樣才是匹配尾部0的操作。
" strip trailing 0s in decimals
let answer = substitute (answer, '\.\(\d*[1-9]\)0\+$', '.\1', "")
--//另外我當時windows與linux版本分開,今天把它們合併起來。windows下處理^存在問題。修改如下:
" escape chars for shell
if has("unix")
let str = escape (str, '*();&><|^')
else
let str = escape (str, '*();&><|')
endif
--//解決插入模式下計算問題,我修改的版本給CalcLines加入參數。
"" calculate from insertmode
inoremap =: = <Esc>"eyy:call CalcLines()<CR>a
--//修改如下:
"" calculate from insertmode
inoremap =: = <Esc>"eyy:call CalcLines(0)<CR>a
--//增加16進位的識別,很簡單的檢查,如果無法識別當10進位,如果明確16進位最好在前面輸入0x.
--//加入dba塊地址,scn的轉換10,16進位的功能,並加入註解便於閱讀。
$ cat bccalc.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()<CR>
"vnoremap <Leader>bc "ey`>:call<SID>CalcBC(1)<CR>
vnoremap ;bc "ey`>:call CalcLines(0)<CR>
vnoremap ;10 "ey`>:call CalcLines(10)<CR>
vnoremap ;16 "ey`>:call CalcLines(16)<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>
"" 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>
" 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 dba(10) 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) 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#(10) or file#,block#(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>bc "eyy$:call<SID>CalcBC(0)<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, '*();&><|^')
else
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 = substitute (str, "0x", "", "g")
let str = toupper ( str )
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 == 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)
else
let answer = system ("echo " . str . "/4194304" . " \| bc " . preload)
let answer1 = system ("echo " . str . "%4194304" . " \| bc " . preload)
endif
" let answer = "set dba " . answer . "," . answer1
let answer = "set dba " . answer . "," . answer1 ." = alter system dump file " . answer . " block " . answer1
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: " . answer . " " . answer1
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)
let answer = "scn_wrap,scn_base(10): " . answer . " = scn_wrap,scn_base(16): " . "0x" . tolower (answer1)
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)
let answer = "scn_wrap,scn_base(10): " . answer . " = scn_wrap,scn_base(16): " . "0x" . tolower (answer1)
endif
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)
let answer = "file#,block#(10): " . answer . " = file#,block#(10): " . "0x" . tolower (answer1)
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)
let answer = "file#,block#(10): " . answer . " = file#,block#(16): " . "0x" . tolower (answer1)
endif
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
let answer = substitute (answer, "\n", "", "g")
" strip trailing 0s in decimals
" let answer = substitute (answer, '\.\(\d*[1-9]\)0\+', '.\1', "")
let answer = substitute (answer, '\.\(\d*[1-9]\)0\+$', '.\1', "")
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 =~ "a"
let has_hex = 1
return has_hex
endif
if ss =~ "b"
let has_hex = 1
return has_hex
endif
if ss =~ "c"
let has_hex = 1
endif
if ss =~ "d"
let has_hex = 1
return has_hex
endif
if ss =~ "e"
let has_hex = 1
return has_hex
endif
if ss =~ "f"
let has_hex = 1
return has_hex
endif
endfunction
--//拷貝到plugin目錄就ok了。簡單介紹一些使用:
1.輸入算式,按<esc> \bc,也可以在插入模式下輸入=:(當然要快,慢了就無效了)。
如果算式最後是有=,可以按<esc> \bx
2.\10 對應的是16進位轉10進位
\16 對應的是10進位轉16進位
\hd 猜測雙向轉化(簡單記憶: h表示16,d表示d),10->16 or 16->10.可能不對,如果16進位沒有abcdef 0x等字元,當作10->16計算.
3.\rr 對應是file#,block#轉化為10,16進位數, 註意參數也是猜測.
4.\ss 對應是scn_wrap,scn_base轉化為10,16進位數,註意參數也是猜測.
5.在visual模式下,對應的命令僅僅\換成;,可以在提示行顯示結果。
僅僅註意1個問題使用shift+方向鍵,進入的select模式(windows用戶喜歡這樣操作),必須按ctrl+g切換為visual模式。
再輸入相應命令.
--//一些例子:
4229121 = 0x408801 (輸入\16)
0x408801 = 4229121 (輸入\10)
408801 = 4229121 (輸入\10)
0x408801 = 4229121 (輸入\hd)
408801 = 0x63ce1 (輸入\hd,註:因為無法判別輸入10還是16進位,輸入變數當)
(作10進位,可以在前面加入0x表示16進位)
4229121 = set dba 1,34817 = alter system dump file 1 block 34817 (輸入\22 或者 \dba)
1,34817 = file#,block#(10): 4229121 = file#,block#(16): 0x408801 (輸入\rr)
34817 = 0x8801 (輸入\16)
0x1.0x8801 = file#,block#(10): 4229121 = file#,block#(10): 0x408801 (輸入\rr)
1.8801 = file#,block#(10): 4203105 = file#,block#(16): 0x402261 (輸入\rr,無法判別輸入參數是10還是16進位,當10進位處理)
4203105 = set dba 1,8801 = alter system dump file 1 block 8801 (輸入\22 或者 \dba)
0x1.8801 = file#,block#(10): 4229121 = file#,block#(10): 0x408801 (輸入\rr)
12884914013 = scn_wrap,scn_base(10): 3,12125 =scn_wrap,scn_base(16): 0x3,0x2f5d (輸入\32 或者 \scn)
3,12125 = scn_wrap,scn_base(10): 12884914013 = scn_wrap,scn_base(16): 0x300002f5d (輸入\ss)
0x3,0x2f5d = scn_wrap,scn_base(10): 12884914013 = scn_wrap,scn_base(16): 0x300002f5d (輸入\ss)
3.2f5d = scn_wrap,scn_base(10): 12884914013 = scn_wrap,scn_base(16): 0x300002f5d (輸入\ss)
1+34+3 = 38 (輸入\bc)
1+34+3 = 38 (輸入\bx,在算式最後有等號的情況下)
1+34+3 (輸入\bx,在算式沒有等號的情況下,在提示行顯示結果)
1+34+3 = 38 (在插入模式下輸入等號後快速輸入:,可以直接輸出結果。)
--//希望大家給一些建議,我如果有時間繼續完善這個插件。
--//下載地址:https://files.cnblogs.com/files/lfree/bccalc_win.rar