首页 > 系统相关 >[20240516]建立完善vim bccalc_linux.vim插件.txt

[20240516]建立完善vim bccalc_linux.vim插件.txt

时间:2024-05-18 21:52:25浏览次数:22  
标签:插件 bc 16 bccalc vim let CalcLines str answer

[20240516]建立完善vim bccalc_linux.vim插件.txt

--//同步对linux版本做了修改,我自己很少使用,没有测试.
--//安装很简单的拷贝到plugin,目录.

$ cat bccalc_linux.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

标签:插件,bc,16,bccalc,vim,let,CalcLines,str,answer
From: https://www.cnblogs.com/lfree/p/18199817

相关文章

  • [20240514]建立完善vim bccalc_win.vim插件.txt
    [20240514]建立完善vimbccalc_win.vim插件.txt--//最近几天一直在使用vimbccalc_win.vim做运算,这是根据vim.org网站的一个调用bc做计算的一个插件bccalc.vim改写的,--//当时主要目的作为oracle一些计算的辅助工具,后来自己越写越复杂,在这次使用中还是遇到一些问题.1.取模运算......
  • vimtutor
    第一讲小结光标在屏幕文本中的移动既可以用箭头键,也可以使用hjkl字母键。h(左移)j(下行)k(上行)l(右移)欲进入Vim编辑器(从命令行提示符),请输入:vim文件名<回车>欲退出Vim编辑器,请输入:q!<回车>放弃......
  • VSCode安装vue3插件
    1.以前的volar已经弃用了。2.最近vue插件 3.安装好插件后,在vscode中创建项目可能会报错。解决方法:1.使用window+r调出cmd,运行node-v,npm-v都没问题。那么尝试以管理员方式运行vscode,如果还是不行,重启一下电脑。2.网络原因,网络波动或者网比较差的时候导致下载丢包,把node......
  • vscode vim
    easymotionfindchar:<leader><leader>sfindcharforward/backwark:<leader><leader>f/Ffindwordstart/end<leader><leader>w/bfindline<leader><leader>j/kfindwith/:<leader><leader>/......
  • vscode leetcode 插件
    区域测试//@lcprcase=start//"PAYPALISHIRINGGGG"\n3\n//@lcprcase=enddiy参数用于调试不同类型的参数和函数//@lcpr-div-debug-arg-start//funName=alternateDigitSum//paramTypes=["number"]//@lcpr-div-debug-arg-endfunName:函数名称paramTypes......
  • 常用Maya动画渲染插件及云渲染插件推荐
    Maya是一款在影视动画行业备受推崇的建模和动画软件,以其卓越的性能和灵活的功能而闻名。为了提高工作效率,Maya提供了广泛的插件选择,这些插件可以大幅简化用户在建模和渲染过程中的操作。本文将介绍一些常用的Maya插件和高效的云渲染工具,助您更高效地完成动画制作,赶紧来了解一下吧......
  • VIM常用命令
    可以通过:help命令查看帮助文档进入vimvim[文件名]基本模式正常模式(NormalMode):执行复制、粘贴、剪切、查找、替换等命令。按:输入命令行插入模式(InsertMode):可以输入文本,在正常模式下,按i、a、o等都可以进入插入模式可视模式(VisualMode):正常模式下按v可以进入可视模式......
  • 各大插件市场智能助手评分榜出炉!百度Comate稳居第一
    近日,在VSCode、Jetbrains等各大插件市场智能助手评分榜中,百度Comate分别以4.5和4.4位列第一,通义灵码位居第二、第三,CodeGeeX、iFlyCode、aiXcoder、GitHubCopilot等跟随其后。从外界获悉,百度Comate自发布以来,得到了广大程序员们的好评。许多用户表示,Comate不仅提高了他们的工......
  • Linux系统 输入vim 提示command not found
    让我想起了我面试的时候,面试官说你了解Linux系统吗?我说我不......他说好的,请问你平时怎么编辑文件呢。我说vim,进入编辑模式之后按照文档里面......他说如果没有vim呢?我说那就用自带的吧?他说自带的叫什么呢?我:......他:......他突然说:你平时抽烟吗?我:......whatcanisay......
  • 自研WPF插件系统(沙箱运行及热插拔)
    前言插件化的需求主要源于对软件架构灵活性的追求,特别是在开发大型、复杂或需要不断更新的软件系统时,插件化可以提高软件系统的可扩展性、可定制性、隔离性、安全性、可维护性、模块化、易于升级和更新以及支持第三方开发等方面的能力,从而满足不断变化的业务需求和技术挑战。......