[20190913]完善vim的bccacl插件2.txt

--//继续完善vim的bccacl插件。
--//\bc 计算也可以直接使用 \bb 操作,这样操作更快一些。
--//增加直接写好算式计算的快捷\cc(注不用输入最后等号),这步直接调用bc,这样算式没有问题,都能计算正确。

"" 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

--//例子:
x=12;y=23.1;x*y  = 277.2                 (输入\cc)
a=12;b=F; a+b = 27                       (输入\cc)
scale=0;4300011211%2^32  = 5043915       (输入\cc)
4300011211%2^32 = .00000000003987734528  (输入\cc)
4300011211 = scn_wrap,scn_base(10): 1,5043915 =scn_wrap,scn_base(16): 0x1,0x4cf6cb (输入\32)

4300011211/2^32 = 1.00117437797598540782
4300011211-1.00117437797598540782*2^32 = .00000000003987734528

--//说明:bc -l 调用math库函数,缺省scale=20. 而一般取模运算应用都是整形,先设置scale=0再计算就ok了。

--//windows系列不用执行这步,如果算式包含*()^, windows下会报错。
if has("unix")
    let str = escape (str, '*();&><|^')
endif

--//convert hexdecimal to decimal or decimal to hexdecimal,增加\hh,\dd,\dh,这样操作更快一些。
nnoremap  <Leader>hd <Esc>A = <Esc>"eyy:call CalcLines(30016)<CR>
nnoremap  <Leader>hh <Esc>A = <Esc>"eyy:call CalcLines(30016)<CR>
nnoremap  <Leader>dd <Esc>A = <Esc>"eyy:call CalcLines(30016)<CR>
nnoremap  <Leader>dh <Esc>A = <Esc>"eyy:call CalcLines(30016)<CR>

--//优化函数Check_hex。
if ss =~ "[abcdef]"
    let has_hex = 1
    return has_hex
endif

--//最终代码如下:
"" 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 ;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 ;dd "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

" 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) 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>dd <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 = 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# dba(10): " . answer . " = file#,block# dba(16): " . "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# dba(10): " . answer . " = file#,block# dba(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', "")

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

[20190913]完善vim的bccacl插件2.txt的更多相关文章

  1. [20190909]完善vim的bccacl插件.txt

    [20190909]完善vim的bccacl插件.txt http://blog.itpub.net/267265/viewspace-2140886/http://blog.itpub.net/26 ...

  2. [20191101]完善vim的bccalc插件8.txt

    [20191101]完善vim的bccalc插件8.txt --//今天移植bccalc插件到linux,发现一些问题.我自己已经在windows下使用一段时间,从来没有在linux下测试.看来很少人 ...

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

    [20191031]完善vim的bccalc插件7.txt --//增加/ 2^16 %2^16功能,输入\tx,例子:1398145029 = /2^16  %2^16 (type and mode ...

  4. [20190920]完善vim调用sqlplus脚本.txt

    [20190920]完善vim调用sqlplus脚本.txt --//以前写的http://blog.itpub.net/267265/viewspace-2140936/=>[20170617 ...

  5. vim配置及插件安装管理(超级详细)

    1 写在前面   Linux下编程一直被诟病的一点是: 没有一个好用的IDE, 但是听说Linux牛人, 黑客之类的也都不用IDE. 但是对我等从Windows平台转移过来的Coder来说, 一个好用 ...

  6. vim配置及插件安装管理(超级详细)[转]

    1 写在前面   Linux下编程一直被诟病的一点是: 没有一个好用的IDE, 但是听说Linux牛人, 黑客之类的也都不用IDE. 但是对我等从Windows平台转移过来的Coder来说, 一个好用 ...

  7. vim配置文件和插件管理

    本文通过总结零碎的资料总结而成,更多是去引导学习vim配置文件及插件使用. .vimrc配置文件,内容如下(备注清晰) "引入插件pathogen使用 execute pathogen#in ...

  8. vim 代码注释插件

    参考: 1.http://www.vim.org/scripts/script.php?script_id=1218 2.http://www.dutor.net/index.php/2010/05/ ...

  9. vundle就是vim bundle的插件管理成ide

    如何配置一个高效的php编辑环境, 很好 对vundle的操作, 除了仓库名称是vundle.git (*.git就是仓库) 和 本地目录名是 vundle之外, 其他的操作都是bundle git ...

随机推荐

  1. Winform中自定义添加ZedGraph右键实现设置所有Y轴刻度的上下限

    场景 Winforn中实现ZedGraph自定义添加右键菜单项(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  2. ubuntu18.10 上安装docker容器

    网上有的安装步骤太复杂,并且安装过程中容易出错,其它安装不难,只需一条命令即可. 安装成功后,使用命令查看docker状态 systemctl status docker 安装前更新下包源 sudo ...

  3. 【JDBC】CRUD操作

    JDBC的CRUD操作 向数据库中保存记录 修改数据库中的记录 删除数据库中的记录 查询数据库中的记录 保存代码的实现 package demo1; import java.sql.Connectio ...

  4. Nuget使用时遇到的问题,Solved

    在VS的程序包管理控制台中输入Install-package MySql.Data时,默认安装最新的版本8.0.18, 但是安装完成后,发现包并没有添加到项目的引用列表中, 在解决方案的package ...

  5. Octave绘图数据

    t = [0:0.01:0.98]   :设置一个步长为0.01的矩阵 y1 = sin(2*pi*4*t) :设置一个sin函数 plot(t,y1) :绘制出以 t 为横轴  以  y1为纵轴的图 ...

  6. postman---postman提示 Could not get any response

    在通过postman请求做接口测试的过程中,有时候会遇到一些报错,当遇到这些报错我们不要着急,看着具体哪里报错,然后进行解决 postman报错 经常使用postman的小伙伴们都应该遇到过一些报错, ...

  7. Internet Download Manager是什么?

    在互联网下载管理器(也被称为IDM)是一款共享软件下载管理器,这意味着你可以下载该程序,并尝试它的试用期内免费. 以下是IDM最佳功能的列表: 支持多种浏览器和应用程序 一键下载文件 内置防病毒检查 ...

  8. 四、读取一系列dcm图片,然后重新写入

    一.程序功能 读取一系列的CT dcm图片,然后重新写入到一个文件夹 二.代码 #pragma warning(disable:4996) #include "itkGDCMImageIO. ...

  9. 201871010107-公海瑜《面向对象程序设计(java)》第一周学习总结

    201871010107-公海瑜<面向对象程序设计(java)>第一周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/   ...

  10. DRF--重写views

    前戏 在前面几篇文章里,我们写了get请求,post请求,put请求,在来写个delete请求,大概如下. class BookView(APIView): # 查询所有的数据和post方法 def ...