[20191101]完善vim的bccalc插件8.txt

来源:https://www.cnblogs.com/lfree/archive/2019/11/01/11777268.html
-Advertisement-
Play Games

[20191101]完善vim的bccalc插件8.txt--//今天移植bccalc插件到linux,發現一些問題.我自己已經在windows下使用一段時間,從來沒有在linux下測試.看來很少人看我的blog.--//對比以前我的腳本我才發現問題在於windows下echo與linux下不同.- ...


[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






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

-Advertisement-
Play Games
更多相關文章
  • Nginx七層負載均衡的幾種調度演算法   Nginx是一款輕量級的高性能web伺服器,同時也是一款非常優秀的負載均衡器和反向代理伺服器。由於支持強大的正則匹配規則、動靜分離、URLrewrite功能及安裝配置簡單且對網路穩定性依賴非常小等優點,所以常用來做為七層負載均衡使用。在硬 ...
  • 本文是《詳細解讀 Spring AOP 面向切麵編程(一)》的續集。 在上篇中,我們從寫死代碼,到使用代理;從編程式 Spring AOP 到聲明式 Spring AOP。一切都朝著簡單實用主義的方向在發展。沿著 Spring AOP 的方向,Rod Johnson(老羅)花了不少心思,都是為了讓我 ...
  • 棧空間:用戶棧和內核棧 程式的執行流程 進程其實都是在執行任務,而任務其實就是函數定義的(函數也稱為方法、子程式等,本質都一樣),所以進程的作用就是不斷的執行函數。程式啟動時,第一個要執行的函數是main()函數(有些語言隱藏了這個函數,但任何程式一定會有一個程式入口函數),然後在main()函數中 ...
  • 回到目錄 本小節我們以2N4123通用型BJT硅基晶體管為例,來介紹如何閱讀BJT的數據規格書,點此鏈接可以閱讀和下載2N4123的數據規格書。 1. 總體性能 打開datasheet後,首先看標題: 圖3-8.01 可以看到,這是2N4123、2N4124共用的一個datasheet,而且是通用型 ...
  • 學前理論 linux主要特征 :一切且文件(目錄、硬碟等都是文件);硬體都在/dev 目錄,如硬碟、U盤為/dev/sd[a d]; /dev/sr0(/dev/cdrom)是光碟機的設備名(df命令查看),為設備文件,代表的是光碟機本身,得把這個設備掛載到目錄下(一般為/mnt)(文件系統的臨時掛載點 ...
  • 對於記憶體監控,在top里我們要時刻監控第五行swap交換分區的used,如果這個數值在不斷的變化,說明內核在不斷進行記憶體和swap的數據交換,這是真正的記憶體不夠用了 ...
  • Docker概述 Docker是一個用於開發,交付和運行應用程式的開放平臺。 Docker優勢 更快速的交付和部署 對於開發人員 Build Once, Run Anywhere 容器意味著環境隔離和可重覆性。開發人員只需為應用創建一次運行環境,然後打包成容器便可在其他機器上運行。另外,容器環境與所 ...
  • KVM 快照的定義:快照就是將虛機在某一個時間點上的磁碟、記憶體和設備狀態保存一下,以備將來之用。它包括以下幾類: (1)磁碟快照:磁碟的內容(可能是虛機的全部磁碟或者部分磁碟)在某個時間點上被保存,然後可以被恢復。 磁碟數據的保存狀態: 在一個運行著的系統上,一個磁碟快照很可能只是崩潰一致的(cra ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...