[20240514]建立完善vim bccalc_win.vim插件.txt
[20240514]建立完善vim bccalc_win.vim插件.txt
--//最近几天一直在使用vim bccalc_win.vim做运算,这是根据vim.org网站的一个调用bc做计算的一个插件bccalc.vim改写的,
--//当时主要目的作为oracle一些计算的辅助工具,后来自己越写越复杂,在这次使用中还是遇到一些问题.
1.取模运算问题:
--//实际上这个问题以前也遇到过.
12%10 = 0 --//按\bc
4%3 = .00000000000000000001 --//按\bc.
--//实际这个快捷调用bc -l,使用库函数,scale=20.导致计算不对,实际上使用\b0 快捷就可以了.(scale=0)
12%10 = 2 --//按\b0
4%3 = 1 --//按\b0
--//当我写成如下执行时:
scale=0; 4%3 = .00000000000000000001 --//按\bc.
--//问题再现,我看了源代码,存在如下内容:
function! CalcLines(flag)
....
" if there is another equal in the line, assume chained equations, remove leading ones"
let @e = substitute (@e, '^.\+=', '', '')
--//如果中间存在=字符,移除前面的字符,这样如下代码执行错误.
--// scale=0; 4%3 过滤后变成 0; 4%3,再执行计算,注调试可以使用register e查看寄存器内容.
0; 4%3 = 0.00000000000000000001 --//按\bc.
D:\notes>echo 0; 4%3|bc -l
0
.00000000000000000001
--//注解这行就ok了.我感觉也许选择注解更加科学一些.
--//例子:
a=3.1;b=2.1;a+b = 5.2 --// 按\bc,如果按照以前的代码这样变成
a=3.1;b=2.1;a+b = 2.1 --// 因为这样字符串变成 2.1;a+b ,计算后变成:
D:\notes>echo 2.1;a+b | bc -l
2.1
0
--//脚本的输出过滤掉\n以及最后的0字符,导致结果变成了2.1.明显不对.注解后这个问题就不存在了.
--//我还增加<leader>b0 调用bc的功能不带-l参数,这样整形运算取模就不会再有问题
4%3 = 1 --//按\b0
2.其他改进:
--//再次花了一点时间重读源代码,对一些命令进行了整理,取消了原来外部调用!!bc相关命令.
--//注解掉<leader>cc, <leader>c0 <leader>c,这些代码,不再使用!!这样执行模式调用bc,实际上这个是自己最早自用的版本.
--//可以使用<leader>bc代替支持多行,有一个小缺点,就是每行结尾像c语言一样写分号.
--//增加<leader>b,支持过滤掉字符串里面的,再运算.
--//不管select,visual,normal任何模式全部统一使用<leader>前缀,这样方便操作以及记忆.
--//我记得以前改写过可以实现16进制到16进制计算,快捷给忘了,检查发现我定义为<leader>16x,要按4个键,再增加一个定义
--//<leader>hh,表示hex->hex的意思,取消原来<leader>hh的定义.
--//注:使用<leader>hh,里面输入即使没有0x也是当作16进制数字,例子:
0x23+0x15 = 0x38
23+0x15 = 0x38
0x23+15 = 0x38
23+15 = 0x38
a+b = 0x15
3.增加列累加运算:
--//增加select visual模式下列累加运算,以前有人提过,我一直没做,因为网上有1个visSum.vim插件实现类似功能.精度有一些问题,仅
--//仅支持小数点6位.
--//<leader>du 在select visual模式下做列累加运算,实现的非常牵强,感觉效果还可以.
--//参考链接:[20200424]vim visSum.vim合计插件.txt,选择 <leader>du 避免重名.
--//许多细节可能考虑不到...感觉不管如何做都存在许多问题.....
--//另外windows下bc.exe可执行程序自己要另行安装,一般我选择unxutils包,里面包括tr,sed等命令,设置好PATH环境变量就可以使用了.
--//调试小技巧,可以查看vim register e,f.
--//准备另外写一个简单的操作说明书.
4.改进后源代码如下:
$ cat 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 on current line using bc -l, pick a mapping, or use the Leader
"" vnoremap <Leader>cc mz"fy'><ESC>o<ESC>"fp!!bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>y$d$`za = <ESC>pJ
"" calculate expression on current line using bc , pick a mapping, or use the Leader
"" vnoremap <Leader>c0 mz"fy'><ESC>o<ESC>"fp!!bc -q\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>y$d$`za = <ESC>pJ
"" calculate expression on current line ( delete ,) using bc -l, pick a mapping, or use the Leader
"" vnoremap <Leader>c, mz"fy'><ESC>o<ESC>"fp!!sed "s/,//g" \|bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>y$d$`za = <ESC>pJ
"" calculate expression from selection using bc -l, pick a mapping, or use the Leader
vnoremap <Leader>bc "ey`>:call CalcLines(0)<CR>
vnoremap <Leader>bb "ey`>:call CalcLines(0)<CR>
vnoremap <Leader>cc "ey`>:call CalcLines(0)<CR>
"" calculate expression from selection using bc , pick a mapping, or use the Leader
vnoremap <Leader>b0 "ey`>:call CalcLines(1)<CR>
vnoremap <Leader>c0 "ey`>:call CalcLines(1)<CR>
"" calculate expression from selection line ( delete ,) using bc -l, pick a mapping, or use the Leader
vnoremap <Leader>b, "ey`>:call CalcLines(2)<CR>
vnoremap <Leader>c, "ey`>:call CalcLines(2)<CR>
"" convert hexdecimal to decimal
vnoremap <Leader>10 "ey`>:call CalcLines(10)<CR>
"" convert decimal to hexdecimal
vnoremap <Leader>16 "ey`>:call CalcLines(16)<CR>
"" computer hexdecimal to hexdecimal
vnoremap <Leader>16x "ey`>:call CalcLines(1016)<CR>
vnoremap <Leader>hh "ey`>:call CalcLines(1016)<CR>
"" split event P1 or P3 to TYPE and MODE or NAME and MODE
vnoremap <Leader>tx "ey`>:call CalcLines(1616)<CR>
"" split dba(10) or dba(16) to file# and block#
vnoremap <Leader>22 "ey`>:call CalcLines(22)<CR>
vnoremap <Leader>dba "ey`>:call CalcLines(22)<CR>
"" split scn(10) or scn(16) into scn_wrap,scn_base
vnoremap <Leader>32 "ey`>:call CalcLines(32)<CR>
vnoremap <Leader>scn "ey`>:call CalcLines(32)<CR>
"" convert scn_wrap,scn_base(10) or scn_wrap,scn_base(16) to 10 or 16 base
vnoremap <Leader>ss "ey`>:call CalcLines(10016)<CR>
"" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
vnoremap <Leader>rr "ey`>:call CalcLines(20016)<CR>
"" convert hexdecimal to decimal or decimal to hexdecimal
vnoremap <Leader>hd "ey`>:call CalcLines(30016)<CR>
vnoremap <Leader>dh "ey`>:call CalcLines(30016)<CR>
"" visual sum
vnoremap <Leader>du "ey`>oSum<ESC>:call CalcLines(40016)<CR>
"" --------------------------------------------------------------------
"" calculate expression on current line using bc -l, pick a mapping, or use the Leader
"" 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
"" calculate expression on current line using bc , pick a mapping, or use the Leader
"" noremap <Leader>c0 Yp!!bc -q\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J
"" calculate expression on current line ( delete ,) using bc -l, pick a mapping, or use the Leader
"" 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
"" calculate expression on current line using bc -l, pick a mapping, or use the Leader
nnoremap <Leader>bc <Esc>"eyy$:call CalcLines(0)<CR>
nnoremap <Leader>bb <Esc>"eyy$:call CalcLines(0)<CR>
nnoremap <Leader>cc <Esc>"eyy$:call CalcLines(0)<CR>
"" calculate expression on current line using bc , pick a mapping, or use the Leader
nnoremap <Leader>b0 <Esc>"eyy$:call CalcLines(1)<CR>
nnoremap <Leader>c0 <Esc>"eyy$:call CalcLines(1)<CR>
"" calculate expression on current line ( delete ,) using bc -l, pick a mapping, or use the Leader
nnoremap <Leader>b, <Esc>"eyy$:call CalcLines(2)<CR>
nnoremap <Leader>c, <Esc>"eyy$:call CalcLines(2)<CR>
"" convert hexdecimal to decimal
nnoremap <Leader>10 <Esc>"eyy$:call CalcLines(10)<CR>
"" convert decimal to hexdecimal
nnoremap <Leader>16 <Esc>"eyy$:call CalcLines(16)<CR>
"" computer hexdecimal to hexdecimal
nnoremap <Leader>16x <Esc>"eyy$:call CalcLines(1016)<CR>
nnoremap <Leader>hh <Esc>"eyy$:call CalcLines(1016)<CR>
"" split event P1 or P3 to TYPE and MODE or NAME and MODE
nnoremap <Leader>tx <Esc>"eyy$:call CalcLines(1616)<CR>
"" split dba(10) or dba(16) to file# and block#
nnoremap <Leader>22 <Esc>"eyy$:call CalcLines(22)<CR>
nnoremap <Leader>dba <Esc>"eyy$:call CalcLines(22)<CR>
"" split scn(10) or scn(16) into scn_wrap,scn_base
nnoremap <Leader>32 <Esc>"eyy$:call CalcLines(32)<CR>
nnoremap <Leader>scn <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>"eyy$:call CalcLines(10016)<CR>
"" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
nnoremap <Leader>rr <Esc>"eyy$:call CalcLines(20016)<CR>
"" convert hexdecimal to decimal or decimal to hexdecimal
nnoremap <Leader>hd <Esc>"eyy$:call CalcLines(30016)<CR>
nnoremap <Leader>dh <Esc>"eyy$:call CalcLines(30016)<CR>
"" --------------------------------------------------------------------
"" calculate from insertmode
inoremap =: =<Esc>"eyy$:call CalcLines(0)<CR>a
inoremap =- =<Esc>"eyy$:call CalcLines(30016)<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")
if has("windows")
let str = substitute (str, '\^', '^^^^', "g")
endif
" 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
" only bc not argument -l
if a:flag == 1
let answer = system ("echo " . str . " \| bc " . preload)
" let answer = answer . " --- ". str
endif
" filter , from str
if a:flag == 2
let str = substitute (str, ",", "", "g")
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
if a:flag == 1016
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
let answer = system ("echo obase=16 ;ibase =16;" . str . " \| bc " . 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 datafile " . 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")
if a:flag == 40016
let @e = substitute (@e, "\n", "+", "g")
else
let @e = substitute (@e, "\n", "", "g")
endif
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)
if a:flag == 40016
let answer = Calculate (@e,0)
else
let answer = Calculate (@e,a:flag)
endif
" append answer or echo
if has_equal == 1
exec "normal a " . answer
else
exec "normal a " . "= " . answer
"" 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
--//最近几天一直在使用vim bccalc_win.vim做运算,这是根据vim.org网站的一个调用bc做计算的一个插件bccalc.vim改写的,
--//当时主要目的作为oracle一些计算的辅助工具,后来自己越写越复杂,在这次使用中还是遇到一些问题.
1.取模运算问题:
--//实际上这个问题以前也遇到过.
12%10 = 0 --//按\bc
4%3 = .00000000000000000001 --//按\bc.
--//实际这个快捷调用bc -l,使用库函数,scale=20.导致计算不对,实际上使用\b0 快捷就可以了.(scale=0)
12%10 = 2 --//按\b0
4%3 = 1 --//按\b0
--//当我写成如下执行时:
scale=0; 4%3 = .00000000000000000001 --//按\bc.
--//问题再现,我看了源代码,存在如下内容:
function! CalcLines(flag)
....
" if there is another equal in the line, assume chained equations, remove leading ones"
let @e = substitute (@e, '^.\+=', '', '')
--//如果中间存在=字符,移除前面的字符,这样如下代码执行错误.
--// scale=0; 4%3 过滤后变成 0; 4%3,再执行计算,注调试可以使用register e查看寄存器内容.
0; 4%3 = 0.00000000000000000001 --//按\bc.
D:\notes>echo 0; 4%3|bc -l
0
.00000000000000000001
--//注解这行就ok了.我感觉也许选择注解更加科学一些.
--//例子:
a=3.1;b=2.1;a+b = 5.2 --// 按\bc,如果按照以前的代码这样变成
a=3.1;b=2.1;a+b = 2.1 --// 因为这样字符串变成 2.1;a+b ,计算后变成:
D:\notes>echo 2.1;a+b | bc -l
2.1
0
--//脚本的输出过滤掉\n以及最后的0字符,导致结果变成了2.1.明显不对.注解后这个问题就不存在了.
--//我还增加<leader>b0 调用bc的功能不带-l参数,这样整形运算取模就不会再有问题
4%3 = 1 --//按\b0
2.其他改进:
--//再次花了一点时间重读源代码,对一些命令进行了整理,取消了原来外部调用!!bc相关命令.
--//注解掉<leader>cc, <leader>c0 <leader>c,这些代码,不再使用!!这样执行模式调用bc,实际上这个是自己最早自用的版本.
--//可以使用<leader>bc代替支持多行,有一个小缺点,就是每行结尾像c语言一样写分号.
--//增加<leader>b,支持过滤掉字符串里面的,再运算.
--//不管select,visual,normal任何模式全部统一使用<leader>前缀,这样方便操作以及记忆.
--//我记得以前改写过可以实现16进制到16进制计算,快捷给忘了,检查发现我定义为<leader>16x,要按4个键,再增加一个定义
--//<leader>hh,表示hex->hex的意思,取消原来<leader>hh的定义.
--//注:使用<leader>hh,里面输入即使没有0x也是当作16进制数字,例子:
0x23+0x15 = 0x38
23+0x15 = 0x38
0x23+15 = 0x38
23+15 = 0x38
a+b = 0x15
3.增加列累加运算:
--//增加select visual模式下列累加运算,以前有人提过,我一直没做,因为网上有1个visSum.vim插件实现类似功能.精度有一些问题,仅
--//仅支持小数点6位.
--//<leader>du 在select visual模式下做列累加运算,实现的非常牵强,感觉效果还可以.
--//参考链接:[20200424]vim visSum.vim合计插件.txt,选择 <leader>du 避免重名.
--//许多细节可能考虑不到...感觉不管如何做都存在许多问题.....
--//另外windows下bc.exe可执行程序自己要另行安装,一般我选择unxutils包,里面包括tr,sed等命令,设置好PATH环境变量就可以使用了.
--//调试小技巧,可以查看vim register e,f.
--//准备另外写一个简单的操作说明书.
4.改进后源代码如下:
$ cat 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 on current line using bc -l, pick a mapping, or use the Leader
"" vnoremap <Leader>cc mz"fy'><ESC>o<ESC>"fp!!bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>y$d$`za = <ESC>pJ
"" calculate expression on current line using bc , pick a mapping, or use the Leader
"" vnoremap <Leader>c0 mz"fy'><ESC>o<ESC>"fp!!bc -q\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>y$d$`za = <ESC>pJ
"" calculate expression on current line ( delete ,) using bc -l, pick a mapping, or use the Leader
"" vnoremap <Leader>c, mz"fy'><ESC>o<ESC>"fp!!sed "s/,//g" \|bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>y$d$`za = <ESC>pJ
"" calculate expression from selection using bc -l, pick a mapping, or use the Leader
vnoremap <Leader>bc "ey`>:call CalcLines(0)<CR>
vnoremap <Leader>bb "ey`>:call CalcLines(0)<CR>
vnoremap <Leader>cc "ey`>:call CalcLines(0)<CR>
"" calculate expression from selection using bc , pick a mapping, or use the Leader
vnoremap <Leader>b0 "ey`>:call CalcLines(1)<CR>
vnoremap <Leader>c0 "ey`>:call CalcLines(1)<CR>
"" calculate expression from selection line ( delete ,) using bc -l, pick a mapping, or use the Leader
vnoremap <Leader>b, "ey`>:call CalcLines(2)<CR>
vnoremap <Leader>c, "ey`>:call CalcLines(2)<CR>
"" convert hexdecimal to decimal
vnoremap <Leader>10 "ey`>:call CalcLines(10)<CR>
"" convert decimal to hexdecimal
vnoremap <Leader>16 "ey`>:call CalcLines(16)<CR>
"" computer hexdecimal to hexdecimal
vnoremap <Leader>16x "ey`>:call CalcLines(1016)<CR>
vnoremap <Leader>hh "ey`>:call CalcLines(1016)<CR>
"" split event P1 or P3 to TYPE and MODE or NAME and MODE
vnoremap <Leader>tx "ey`>:call CalcLines(1616)<CR>
"" split dba(10) or dba(16) to file# and block#
vnoremap <Leader>22 "ey`>:call CalcLines(22)<CR>
vnoremap <Leader>dba "ey`>:call CalcLines(22)<CR>
"" split scn(10) or scn(16) into scn_wrap,scn_base
vnoremap <Leader>32 "ey`>:call CalcLines(32)<CR>
vnoremap <Leader>scn "ey`>:call CalcLines(32)<CR>
"" convert scn_wrap,scn_base(10) or scn_wrap,scn_base(16) to 10 or 16 base
vnoremap <Leader>ss "ey`>:call CalcLines(10016)<CR>
"" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
vnoremap <Leader>rr "ey`>:call CalcLines(20016)<CR>
"" convert hexdecimal to decimal or decimal to hexdecimal
vnoremap <Leader>hd "ey`>:call CalcLines(30016)<CR>
vnoremap <Leader>dh "ey`>:call CalcLines(30016)<CR>
"" visual sum
vnoremap <Leader>du "ey`>oSum<ESC>:call CalcLines(40016)<CR>
"" --------------------------------------------------------------------
"" calculate expression on current line using bc -l, pick a mapping, or use the Leader
"" 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
"" calculate expression on current line using bc , pick a mapping, or use the Leader
"" noremap <Leader>c0 Yp!!bc -q\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J
"" calculate expression on current line ( delete ,) using bc -l, pick a mapping, or use the Leader
"" 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
"" calculate expression on current line using bc -l, pick a mapping, or use the Leader
nnoremap <Leader>bc <Esc>"eyy$:call CalcLines(0)<CR>
nnoremap <Leader>bb <Esc>"eyy$:call CalcLines(0)<CR>
nnoremap <Leader>cc <Esc>"eyy$:call CalcLines(0)<CR>
"" calculate expression on current line using bc , pick a mapping, or use the Leader
nnoremap <Leader>b0 <Esc>"eyy$:call CalcLines(1)<CR>
nnoremap <Leader>c0 <Esc>"eyy$:call CalcLines(1)<CR>
"" calculate expression on current line ( delete ,) using bc -l, pick a mapping, or use the Leader
nnoremap <Leader>b, <Esc>"eyy$:call CalcLines(2)<CR>
nnoremap <Leader>c, <Esc>"eyy$:call CalcLines(2)<CR>
"" convert hexdecimal to decimal
nnoremap <Leader>10 <Esc>"eyy$:call CalcLines(10)<CR>
"" convert decimal to hexdecimal
nnoremap <Leader>16 <Esc>"eyy$:call CalcLines(16)<CR>
"" computer hexdecimal to hexdecimal
nnoremap <Leader>16x <Esc>"eyy$:call CalcLines(1016)<CR>
nnoremap <Leader>hh <Esc>"eyy$:call CalcLines(1016)<CR>
"" split event P1 or P3 to TYPE and MODE or NAME and MODE
nnoremap <Leader>tx <Esc>"eyy$:call CalcLines(1616)<CR>
"" split dba(10) or dba(16) to file# and block#
nnoremap <Leader>22 <Esc>"eyy$:call CalcLines(22)<CR>
nnoremap <Leader>dba <Esc>"eyy$:call CalcLines(22)<CR>
"" split scn(10) or scn(16) into scn_wrap,scn_base
nnoremap <Leader>32 <Esc>"eyy$:call CalcLines(32)<CR>
nnoremap <Leader>scn <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>"eyy$:call CalcLines(10016)<CR>
"" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
nnoremap <Leader>rr <Esc>"eyy$:call CalcLines(20016)<CR>
"" convert hexdecimal to decimal or decimal to hexdecimal
nnoremap <Leader>hd <Esc>"eyy$:call CalcLines(30016)<CR>
nnoremap <Leader>dh <Esc>"eyy$:call CalcLines(30016)<CR>
"" --------------------------------------------------------------------
"" calculate from insertmode
inoremap =: =<Esc>"eyy$:call CalcLines(0)<CR>a
inoremap =- =<Esc>"eyy$:call CalcLines(30016)<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")
if has("windows")
let str = substitute (str, '\^', '^^^^', "g")
endif
" 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
" only bc not argument -l
if a:flag == 1
let answer = system ("echo " . str . " \| bc " . preload)
" let answer = answer . " --- ". str
endif
" filter , from str
if a:flag == 2
let str = substitute (str, ",", "", "g")
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
if a:flag == 1016
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
let answer = system ("echo obase=16 ;ibase =16;" . str . " \| bc " . 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 datafile " . 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")
if a:flag == 40016
let @e = substitute (@e, "\n", "+", "g")
else
let @e = substitute (@e, "\n", "", "g")
endif
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)
if a:flag == 40016
let answer = Calculate (@e,0)
else
let answer = Calculate (@e,a:flag)
endif
" append answer or echo
if has_equal == 1
exec "normal a " . answer
else
exec "normal a " . "= " . answer
"" 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