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

"本文介绍了 Vim 的 bccalc 插件的更新,增加了对二进制运算的支持,如 /2^16 %2^16,并解决了输入中逗号的过滤问题。通过快捷键 c 和 c 提供了不同的功能,同时提供了多种转换功能,包括十进制与十六进制之间的转换、SV 编码处理等。此外,还展示了插件如何处理数学表达式并显示计算结果。"
摘要由CSDN通过智能技术生成

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

--//增加/ 2^16 %2^16功能,输入\tx,例子:
1398145029 = /2^16  %2^16 (type and mode) = 21334,5 = 0x53560005
0x53560005 = /2^16  %2^16 (type and mode) = 21334,5 = 1398145029
--//5356 =SV(对应asc码SV)

--//增加处理输入逗号过滤问题,这个有点麻烦的是有时候逗号是合法的。单独增加快捷使用\c,
--//而\bc的功能不动。

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

12,345+1,000 = 13345  --//输入\c.

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

### 回答1: 您可以使用以下命令来输入这些内容: 1. 打开终端并输入vim命令 2. 按下i键进入插入模式 3. 输入“Learn Vim. Vim Example Line1.”并按下Enter键 4. 输入“Vim Example Line 2”并按下Enter键 5. 输入“Vim Example Line 3”并按下Enter键 6. 输入“Vim Example Line 4”并按下Enter键 7. 输入“Vim Example Line 5”并按下Enter键 8. 按下Esc键退出插入模式 9. 输入:wq并按下Enter键保存并退出Vim编辑器 ### 回答2: 使用Vim编辑器输入以下内容的最少操作,可以按照以下步骤进行: 1. 打开Vim编辑器。 2. 进入插入模式,按下大写字母 i,光标跳到第一行的行首。 3. 输入 "Learn Vim." 字符串。 4. 按下回车键,新起一行。 5. 输入 "Vim Example Line1." 字符串。 6. 按下回车键,新起一行。 7. 输入 "Vim Example Line2" 字符串。 8. 按下回车键,新起一行。 9. 输入 "Vim Example Line3." 字符串。 10. 按下回车键,新起一行。 11. 输入 "Vim Example Line4." 字符串。 12. 按下回车键,新起一行。 13. 输入 "Vim Example Line5." 字符串。 14. 按下回车键。 15. 按下大写字母 ESC 键,退出插入模式。 经过以上操作,你可以使用Vim编辑器输入所需的内容。 ### 回答3: 使用Vim编辑器输入以下内容的最少操作,可以按照以下步骤进行: 1. 打开终端,输入`vim`来启动Vim编辑器。 2. 按下`i`键,进入插入模式,开始输入内容。 3. 输入"Learn Vim. Vim Example Line1."(包括双引号内的内容),然后按下`Enter`键换行。 4. 输入"Vim Example Line 2",按下`Enter`键换行。 5. 输入"Vim Example Line 3",按下`Enter`键换行。 6. 输入"Vim Example Line 4",按下`Enter`键换行。 7. 输入"Vim Example Line 5.",然后按下`Esc`键退出插入模式,回到命令模式。 8. 输入`:wq`,按下`Enter`键保存并退出Vim编辑器。 这样,你就使用了最少的操作将内容输入到Vim编辑器中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值