[20191101]完善vim的bccalc外掛8.txt
[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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2662279/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20191031]完善vim的bccalc外掛7.txt
- [20210810]完善vim bccalc_win外掛.txt
- [20210812]完善vim bccalc_linux外掛.txtLinux
- [20210507]完善vim bccalc_win外掛.txt
- [20220117]呼叫bccalc_win.vim外掛失效.txt
- [20220316]完善vim gdb.vim外掛.txt
- [20190909]完善vim的bccacl外掛.txt
- [20200225]vim bccalc外掛與utl-8編碼.txt
- [20190913]完善vim的bccacl外掛2.txt
- Vim 外掛管理
- vim外掛之Taglist
- 如何安裝 Vim 外掛
- 關於vim的實用外掛
- [20200424]vim visSum.vim合計外掛.txt
- vim外掛 NERDTree安裝使用
- python vim外掛是什麼Python
- Vim-plug:極簡 Vim 外掛管理器
- vim外掛管理器,我選vim-pluginPlugin
- 5 個好用的開發者 Vim 外掛
- 實用!開發者的 Vim 外掛(一)
- 實用!開發者的 Vim 外掛(二)
- [20231226]vim Align外掛使用例子.txt
- 如何安裝最新的Vim8.2與Vim外掛管理器vim-plug
- vim實戰:外掛安裝(Vundle,NerdTree)
- VIM 程式碼片段外掛 Ultisnips 使用教程
- Vim常用外掛安裝及配置方法
- vim 的模糊查詢外掛 LeaderF 新功能介紹
- Vim的snipMate外掛 php程式碼自動補全PHP
- 如何使用 Ansible 配置 Vim並安裝外掛
- [20211224]vim外掛格式化sql語句.txtSQL
- vim 的 grep 外掛`Leaderf rg`:grep 和模糊匹配的完美結合
- Gradle 與 AGP 構建 API: 進一步完善您的外掛!GradleAPI
- 7款優秀Vim外掛幫你打造完美IDEIDE
- ros環境搭建及vim-plug外掛安裝ROS
- VS Code - Vim 外掛自動切換輸入法
- 多語言程式設計必備的十大 Vim 外掛程式設計
- Vimium外掛讓鍵盤黨像操作Vim一樣操作ChromeChrome
- 外掛如何呼叫本外掛的View?View