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

来源:https://www.cnblogs.com/lfree/archive/2019/09/13/11517681.html
-Advertisement-
Play Games

[20190913]完善vim的bccacl插件2.txt--//繼續完善vim的bccacl插件。--//\bc 計算也可以直接使用 \bb 操作,這樣操作更快一些。--//增加直接寫好算式計算的快捷\cc(註不用輸入最後等號),這步直接調用bc,這樣算式沒有問題,都能計算正確。"" calcul ...


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

--//繼續完善vim的bccacl插件。
--//\bc 計算也可以直接使用 \bb 操作,這樣操作更快一些。
--//增加直接寫好算式計算的快捷\cc(註不用輸入最後等號),這步直接調用bc,這樣算式沒有問題,都能計算正確。

"" 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

--//例子:
x=12;y=23.1;x*y  = 277.2                 (輸入\cc)
a=12;b=F; a+b = 27                       (輸入\cc)
scale=0;4300011211%2^32  = 5043915       (輸入\cc)
4300011211%2^32 = .00000000003987734528  (輸入\cc)
4300011211 = scn_wrap,scn_base(10): 1,5043915 =scn_wrap,scn_base(16): 0x1,0x4cf6cb (輸入\32)

4300011211/2^32 = 1.00117437797598540782
4300011211-1.00117437797598540782*2^32 = .00000000003987734528

--//說明:bc -l 調用math庫函數,預設scale=20. 而一般取模運算應用都是整形,先設置scale=0再計算就ok了。

--//windows系列不用執行這步,如果算式包含*()^, windows下會報錯。
if has("unix")
    let str = escape (str, '*();&><|^')
endif

--//convert hexdecimal to decimal or decimal to hexdecimal,增加\hh,\dd,\dh,這樣操作更快一些。
nnoremap  <Leader>hd <Esc>A = <Esc>"eyy:call CalcLines(30016)<CR>
nnoremap  <Leader>hh <Esc>A = <Esc>"eyy:call CalcLines(30016)<CR>
nnoremap  <Leader>dd <Esc>A = <Esc>"eyy:call CalcLines(30016)<CR>
nnoremap  <Leader>dh <Esc>A = <Esc>"eyy:call CalcLines(30016)<CR>

--//優化函數Check_hex。
if ss =~ "[abcdef]"
    let has_hex = 1
    return has_hex
endif

--//最終代碼如下:
"" 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 ;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 ;dd "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

" 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 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>dd <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 = substitute (str, "0x", "", "g")
        let str = toupper (str)
        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 == 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)
        else
            let answer  = system ("echo " . str . "/4194304" . " \| bc " . preload)
            let answer1 = system ("echo " . str . "%4194304" . " \| bc " . preload)
        endif
        " let answer = "set dba " . answer . "," . answer1
        let answer = "set dba " . answer . "," . answer1 ." = alter system dump file " . answer . " block " . answer1
    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: " . answer . " " . answer1
        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)
            let answer = "scn_wrap,scn_base(10): " . answer . " = scn_wrap,scn_base(16): " . "0x" . tolower (answer1)
        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)
            let answer = "scn_wrap,scn_base(10): " . answer . " = scn_wrap,scn_base(16): " . "0x" . tolower (answer1)
        endif
    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)
            let answer = "file#,block# dba(10): " . answer . " = file#,block# dba(16): " . "0x" . tolower (answer1)     
        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)
            let answer = "file#,block# dba(10): " . answer . " = file#,block# dba(16): " . "0x" . tolower (answer1)
        endif
    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
    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

" ---------------------------------------------------------------------
" 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


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...