[20170612]vim bccalc插件.txt

简介: [20170612]vim bccalc插件.txt --//上午修改bccacl插件,加入10,16,dba,scn之类转换.由于linux版本,与windows版本存在一些差异,分别贴上来: 1.

[20170612]vim bccalc插件.txt

--//上午修改bccacl插件,加入10,16,dba,scn之类转换.由于linux版本,与windows版本存在一些差异,分别贴上来:

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

"" 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 ;32 "ey`>:call CalcLines(32)<CR>

"" calculate expression on current line, pick a mapping, or use the Leader
nnoremap  <Leader>bc "eyy$:call CalcLines(0)<CR>
nnoremap   <Leader>bx <Esc>A=<Esc>"eyy:call CalcLines(0)<CR>
nnoremap   <Leader>10 <Esc>A=<Esc>"eyy:call CalcLines(10)<CR>
nnoremap   <Leader>16 <Esc>A=<Esc>"eyy:call CalcLines(16)<CR>
nnoremap   <Leader>22 <Esc>A=<Esc>"eyy:call CalcLines(22)<CR>
nnoremap   <Leader>32 <Esc>A=<Esc>"eyy:call CalcLines(32)<CR>


"nnoremap  <Leader>bc "eyy$:call<SID>CalcBC(0)<CR>

"" calculate from insertmode
inoremap =: = <Esc>"eyy:call CalcLines()<CR>a

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

    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
    " let str = escape (str, '*();&><|^')

    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)
    endif
    if a:flag == 16
         let answer = system ("echo obase=16 ;" . str .  " \| bc -l " . preload)
         let answer = "0x" . tolower ( answer )
    endif
    if a:flag == 10
         let str = toupper ( str )
         let answer = system ("echo ibase=16 ;" . str .  " \| bc -l " . preload)
    endif
    if a:flag == 22
         let answer = system ("echo " . str . "/4194304" . " \| bc " . preload)
         let answer1 = system ("echo " . str . "%4194304" . " \| bc " . preload)
         let answer = " set dba " . answer . "," . answer1
    endif

    if a:flag == 32
         let answer = system ("echo " . str . "/4294967296" . " \| bc " . preload)
         let answer1 = system ("echo " . str . "%4294967296" . " \| bc " . preload)
         let answer = " scn_wrap,scn_base: " . answer . " " . answer1
    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

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

"" 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 ;32 "ey`>:call CalcLines(32)<CR>

"" calculate expression on current line, pick a mapping, or use the Leader
nnoremap  <Leader>bc "eyy$:call CalcLines(0)<CR>
nnoremap   <Leader>bx <Esc>A=<Esc>"eyy:call CalcLines(0)<CR>
nnoremap   <Leader>10 <Esc>A=<Esc>"eyy:call CalcLines(10)<CR>
nnoremap   <Leader>16 <Esc>A=<Esc>"eyy:call CalcLines(16)<CR>
nnoremap   <Leader>22 <Esc>A=<Esc>"eyy:call CalcLines(22)<CR>
nnoremap   <Leader>32 <Esc>A=<Esc>"eyy:call CalcLines(32)<CR>


"nnoremap  <Leader>bc "eyy$:call<SID>CalcBC(0)<CR>

"" calculate from insertmode
inoremap =: = <Esc>"eyy:call CalcLines()<CR>a

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

    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
    let str = escape (str, '*();&><|^')

    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)
    endif

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

    if a:flag == 22
         let answer = system ("echo " . str . "/4194304" . " \| bc " . preload)
         let answer1 = system ("echo " . str . "%4194304" . " \| bc " . preload)
         let answer = " set dba " . answer . "," . answer1
    endif

    if a:flag == 32
         let answer = system ("echo " . str . "/4294967296" . " \| bc " . preload)
         let answer1 = system ("echo " . str . "%4294967296" . " \| bc " . preload)
         let answer = " scn_wrap,scn_base: " . answer . " " . answer1
    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

目录
相关文章
|
4月前
|
XML IDE 开发工具
别看你风吹头顶凉但你绝对没有过这样方便的插件Intellij IDEA 自带的 Vim
别看你风吹头顶凉但你绝对没有过这样方便的插件Intellij IDEA 自带的 Vim
109 0
|
API 开发工具 开发者
vim插件开发之python-helloworld插件
vim插件开发之python-helloworld插件
115 0
|
Java API 开发工具
vim插件开发之osc动弹插件
vim插件开发之osc动弹插件
117 0
|
Unix Linux 开发工具
vim插件开发之helloworld插件
vim插件开发之helloworld插件
130 0
|
XML IDE 数据可视化
Vim的三款实用插件
Vim 是 Linux 下的常用文本编辑器,但也经常被称为是一个上古神器,因为它对于初学者而言相当不友好,也不好入门。
317 0
|
Linux 开发工具 Python
vim关于python的自动补全插件
一、克隆代码mkdir -p ~/.vim/toolscd ~/.vim/toolsgit clone https://github.com/rkulla/pydiction.git二、配置Pydiction - UNIX/LINUX/OSX: Put python_pydiction.
1576 0
|
开发工具
强大的vim插件
Vim是一个类似于Vi的著名的功能强大、高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性。常被称为编辑器之神,也是本人平时最喜欢使用的一款开发者编辑器,插件升级如下:     在终端输入: wget -qO- https://raw.
1086 0
|
SQL 开发工具 Perl
[20180417]vim小技巧.txt
[20180417]vim小技巧.txt --//今天调试plsql,发现跟踪到的sql语句在跟踪文件是在一行的,开始以为是开发写成这样,实际上PL/SQL写的语句 --//到跟踪就变成一行,这样就太长,这样为了更好观察我必须设置wrap,执行如下:set wr...
1071 0
|
开发工具 数据库管理 关系型数据库
[20180211]在vim中使用bc进行各种运算.txt
[20180211]在vim中使用bc进行各种运算.txt --//别人的建议,完善一下在vim调用bc进行各种运算. --//我以前定义如下,完成计算,10,16进制转换.
1070 0