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

--//今天移植bccalc插件到linux,发现一些问题.我自己已经在windows下使用一段时间,从来没有在linux下测试.看来很少人看我的blog.
--//对比以前我的脚本我才发现问题在于windows下echo与linux下不同.

--//windows下echo是内部命令,linux即有外部命令也有内部命令.

# type -a  echo
echo is a shell builtin
echo is /bin/echo

--//我的理解优先级应该是内部命令.这样在处理分号上两者是不同的.例子:
--//windows下可以正常执行:
d:\tmp>echo obase=16;255 | bc -l
FF

--//而linux下这样写会报错,主要分号变成了2个命令.
# echo obase=16;255 | bc -l
obase=16
-bash: 255: command not found

--//必须加入单引号避免分号问题.

# echo 'obase=16;255' | bc -l
FF

# (echo obase=16; echo 255) | bc -l
FF

--//这样在存在分号的地方,linux要修改如下例子:

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

--//这样才能正确执行.另外我还发现一个问题,我喜欢在linux下设置:set paste,这样copy and paste脚本时,不会出现锯齿情况.
--//但是这样的模式在插入模式下执行运算会无效.必须先设置:set nopaste
--//windows下源代码参考链接如下:
--//http://blog.itpub.net/267265/viewspace-2662186/=>[20191031]完善vim的bccalc插件7.txt

--//源代码如下:
"" 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

[20191101]完善vim的bccalc插件8.txt的更多相关文章

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

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

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

    [20190913]完善vim的bccacl插件2.txt --//继续完善vim的bccacl插件.--//\bc 计算也可以直接使用 \bb 操作,这样操作更快一些.--//增加直接写好算式计算的 ...

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

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

  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. Matlab非线性规划

    非线性规划 在matlab非线性规划数学模型可以写成一下形式: \[ minf(x)\\ s.t.\begin{cases} Ax \le B \\ Aeq·x = Beq\\ C(x) \le 0\ ...

  2. 安装SDK 6.0(二)

    2==>安装SDK 6.0 打开安卓Android Studio 出现 Unable to access Android SDK add-on list 点击 Cancal 在点击Cancel ...

  3. JavaScript数组循环

    JavaScript数组循环 一.前言 利用Javascript map(),reduce()和filter()数组方法可以遍历数组.而不是积累起来for循环和嵌套来处理列表和集合中的数据,利用这些方 ...

  4. Linux 终端(TTY)

    TTY 是 Teletype 或 Teletypewriter 的缩写,原来是指电传打字机,后来这种设备逐渐键盘和显示器取代.不管是电传打字机还是键盘显示器,都是作为计算机的终端设备存在的,所以 TT ...

  5. golang的make

    golang 分配内存主要有内置函数new和make,今天我们来探究一下make有哪些玩法. map只能为slice, map, channel分配内存,并返回一个初始化的值.首先来看下make有以下 ...

  6. Yii2中indexBy()的使用

    在项目开发中经常会使用到一些特殊的值作为数组的索引,一般可以先查询出数据后数组循环拼接成所需的格式.不过YII2框架提供了一种更简单的方法indexBy(). 参考Yii文档:https://www. ...

  7. 对token机制的学习和分析

    token,中文意思为令牌,是用户登录后会返回的一个字符串,里面包括用户信息.登录时间等,但是是加密过的密文,其加解密方式由后端决定. 在登录之后的接口请求中,前端需在请求中统一加上token,从而识 ...

  8. 如何判断IE OCX插件正常安装?

    项目中用到了一个第三方的ie ocx控件,而经常遇到客户和测试小伙伴反馈相关功能无法正常使用,也没有友好提示.考虑到这个问题,必须要有一个ie ocx控件的检查机制. 检查原理 创建ActiveXOb ...

  9. Unity容器<1>

    参考地址: https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn170416(v=pandp.10) 总览 Unity是一个轻量级 ...

  10. MySQL的数据库定义语法

    创建数据库 在MySQL中,使用 CREATE DATABASE 或 CREATE SCHEMA 语句创建数据库 语法结构: CREATE {DATABASE|SCHEMA}[IF NOT EXIST ...