sourceinsight自定義宏

該宏腳本使用的是 sourceinsight 4.00.0095, 邏輯上, 應該所有的4.0版本都能用, 但未測試

1. 涉及的功能有:

    alt1 alt2切換標籤      綁定的鍵位是: alt+1 alt+2 alt+3... alt+0

    .h與.cpp切換            綁定的鍵位是: alt+F12

    刪除光標右邊的所有的東西        綁定的鍵位是: ctl+k

    刪除光標左面的字符                   綁定的鍵位是: ctl+backspace

    刪除光標右邊的字符                  綁定的鍵位是: ctl+delete

    插入#ifndef *** #define **** #endif ****      綁定的鍵位是: ctl+shift+=

    註釋                        綁定的鍵位是: alt+;

 

2. 具體的源碼如下:

// 給所選擇的行添加註釋
// 使用行註釋, 若已經存在的註釋, 則取消註釋
// 添加的註釋與字符之間無空格
macro CommentBlock()
{
    hbuf = GetCurrentBuf();
    hwnd = GetCurrentWnd();
    if (!hbuf || !hwnd) return 0

    sel = GetWndSel(hwnd);
    allcnt = GetBufLineCount(hbuf)

    chTab = CharFromAscii(9)
    chSpace = CharFromAscii(32)
    chComment = CharFromAscii(47)
    // 循環所有選擇的行
    iLine = sel.lnFirst;
    while (iLine <= sel.lnLast)
    {
        newLine = ""  //備用的新行
        curLine = GetBufLine(hbuf, iLine);
        col1 = 0
        col2 = strlen(curLine)
        has1 = 0
        if (col2 > 0) {
            while (col1 <= col2) {
                ch = curLine[col1]
                
                if (ch == chSpace || ch == chTab) {
                    newLine = newLine # ch
                }
                else {
                    if (ch == chComment && col1 < col2) {
                        tch = curLine[col1+1]
                        if (tch == chComment) {
                            remain = strmid(curLine, col1+2, col2)
                            newLine = newLine # remain
                            break
                        }
                    }
                    remain = strmid(curLine, col1, col2)
                    newLine = newLine # "//" # remain
                    break
                }
                col1++
            }
            // 把已經添加註釋的行, 替換
            PutBufLine(hbuf, iLine, newLine);
        }
        iLine = iLine + 1;
    }
}

// alt + 1 switch tab, windows tab options中tab sorting使用Tab name
macro alt1() {  setCurWindow(0)}
macro alt2() {  setCurWindow(1)}
macro alt3() {  setCurWindow(2)}
macro alt4() {  setCurWindow(3)}
macro alt5() {  setCurWindow(4)}
macro alt6() {  setCurWindow(5)}
macro alt7() {  setCurWindow(6)}
macro alt8() {  setCurWindow(7)}
macro alt9() {  setCurWindow(8)}
macro alt0() {  setCurWindow(9)}

// 1. 創建一個buf(si中只能用buf來保存array)
// 2. 循環取所有的窗口的文件名, 按字母序插入創建的buf中
// 3. buf中保存的文件名格式是: "文件件"+","+"id", 其中id是該文件的窗口id
// 4. 根據alt的id取buf中保存的文件名, 取出窗口id, 切換到該窗口id
function setCurWindow(idx) {
    // 創建buf, 並清空該buf(該buf不會刪除)
    nbuf = NewBuf("abcdefg")
    ClearBuf (nbuf)
    // 循環取每一個窗口文件並插入到buf中
    cwnd = WndListCount()
    iwnd = 0
    while (iwnd < cwnd) {
        hwnd = WndListItem(iwnd)
        name = GetBufName (GetWndBuf (hwnd))
        len = strlen(name)
        pos = len
        while (pos-- > 0) {
            if (name[pos] == "\\" || name[pos] == "/") {
                name = strmid(name, pos + 1, len)
                break
            }
        }
        name = name # "," # iwnd
        num = GetBufLineCount (nbuf)
        pos = 0
        while (pos < num) {
            cln = GetBufLine (nbuf, pos)
            len1 = strlen(name) - 2
            len2 = strlen(cln) - 2
            if (compare_str(name, len1, cln, len2) < 0) {
                InsBufLine (nbuf, pos, name)
                break
            }
            pos ++
        }
        if (pos == num) {
            AppendBufLine (nbuf, name)
        }
        iwnd = iwnd + 1
    }
    // 取idx中所對應的buf的文件名, 並設置爲當前窗口
    if (idx >= 0 && idx < GetBufLineCount (nbuf)) {
        cln = GetBufLine (nbuf, idx)
        tlen = strlen(cln)
        idx_pos = strmid(cln, tlen-1, tlen)
        my_cur_wnd = WndListItem(idx_pos)
        SetCurrentWnd(my_cur_wnd)
    }
    CloseBuf (nbuf)
}

// 比較兩個字符串, 按字符序
// 若順序是: str1->str2返回-1 str2->str1返回 1
function compare_str(str1, len1, str2, len2) {
    pos = 0
    while (1) {
        if (pos == len1 && pos == len2) return -1
        else if (pos >= len1) return -1
        else if (pos >= len2) return 1
        t1 = AsciiFromChar(str1[pos])
        t2 = AsciiFromChar(str2[pos])
        if (t1 > t2) return 1
        else if (t1 < t2) return -1
        pos++
    }
}

// 在.h或.hpp文件中插入 #ifndef *** #define **** #endif ****
macro add_def() {
    hbuf = GetCurrentBuf()
    if (hbuf == hNil) return 0
    name = GetBufName(hbuf)
    len = strlen(name)
    if (len <= 3) return 0
    pos = len
    while (pos-- > 0) {
        if (name[pos] == "\\" || name[pos] == "/") {
            name = strmid(name, pos + 1, len)
            break
        }
    }
    len = strlen(name)
    if (len < 3) return 0
    if (name[len-1] == "h" && name[len-2] == ".") {
        newname = "_" # strtrunc(name, len-2) # "_h_"
    }
    else if (name[len-1] == "p" && name[len-2] == "p" && name[len-3] == "h"&& name[len-4] == ".") {
        newname = "_" # strtrunc(name, len-4) # "_h_"
    }
    else stop
    newname = toupper(newname)
    upadd2 = "#ifndef " # newname
    upadd1 = "#define " # newname
    downadd = "#endif /* "# newname # " */"
    InsBufLine(hbuf, 0, upadd1)
    InsBufLine(hbuf, 0, upadd2)
    AppendBufLine(hbuf, "")
    AppendBufLine(hbuf, downadd)
}

//刪除光標右邊的所有的東西, 若是空行, 則刪除當前行;
//若刪除的部分有內容, 則複製到clip
macro killRight() 
{
    hbuf = GetCurrentBuf()
    hwnd = GetCurrentWnd()
    sel = GetWndSel(hwnd)
    allcnt = GetBufLineCount(hbuf)
    if (allcnt <= 0) return;
    hbufClip = GetBufHandle("Clipboard")
    if (sel.fExtended ) return ;
    // 循環所有選擇的行
    iLine = sel.lnFirst
    curLine = GetBufLine(hbuf, iLine);
    if (sel.ichFirst == 0) {
        // 若在行首
        DelBufLine(hbuf, iLine)
        // 或不是空行, 則加入到clip
        if (strlen(curLine) > 0) {
            EmptyBuf(hbufClip)
            AppendBufLine(hbufClip, curLine)
        }
    }
    else {
        // 只保留當前光標左邊的, 
        len1 = strlen(curLine)
        cacheLine = strmid(curLine, sel.ichFirst, len1) 
        if (strlen(cacheLine) > 0) {
            //只有當有保留的字符時, 纔會複製並放在緩存中, 並設置新行
            newLine = strmid(curLine, 0, sel.ichFirst)
            EmptyBuf(hbufClip)
            AppendBufLine(hbufClip, cacheLine)
            // msg("====" # cacheLine # "====")
            PutBufLine(hbuf, iLine, newLine)
        }
    }
}


//刪除光標左面的東西
// 若光標左邊是空白, 則刪除到第一個非空白字符或到行首
// 若光標左邊是字符, 則刪除到第一個空白字符或到行首
// 若光標左邊是行首, 則刪除到上一行, 並把光標右邊的與上一行合併(即使右邊是空白也會合並上去)
macro left_del() {
    hbuf = GetCurrentBuf()
    hwnd = GetCurrentWnd()
    if (hwnd == hNil || hbuf == hNil) return 0
    sel = GetWndSel(hwnd)
    if (sel.fExtended ) return 0
    // 循環所有選擇的行
    if (sel.ichFirst == 0) {
        pos = sel.lnFirst
        if (pos == 0) return 0
        // 若在行首, 則刪除當前行, 補充到上一行去; 若上一行爲空, 則迭代到非空的行
        curLine = GetBufLine(hbuf, pos)
        preLine = ""
        preLen = 0
        while (pos > 0) {
            //刪除當前行
            DelBufLine(hbuf, pos)
            pos--   //pos指向上一行
            preLine = getBufLine(hbuf, pos)
            preLen = strlen(preLine)
            if (preLen > 0) break
        }
        preLine = cat(preLine, curLine)
        PutBufLine(hbuf, pos, preLine)
        sel.lnFirst = pos
        sel.lnLast = pos
        sel.ichFirst = preLen
        sel.ichLim = sel.ichFirst
        SetWndSel(hwnd, sel)
    }
    else {
        //若不在行首, 則向左刪除到遇到的非字符
        pos = sel.lnFirst
        curLine = GetBufLine(hbuf, pos)
        len1 = strlen(curLine)
        if (len1 <= 0) return 0
        idx = sel.ichFirst
        tailLine = strmid(curLine, idx, len1)
        preChar = -1
        preSpace = -1
        while (idx-- > 0) {
            //退出的條件有兩個: 
            //1.若光標旁邊的字符串時, 然後遇到非字符串時; 或者相反的情況
            //2.若遇到空格, 接下來不是空格時
            ischar = 0
            d = AsciiFromChar(curLine[idx])
            if ((d >= 65 && d <= 90) || (d >= 97 && d <= 122)) ischar = 1
            if (d == 32) if (preSpace < 0) preSpace = 1
            if (preChar < 0) preChar = ischar
            if ((preChar != ischar) || (preSpace > 0 && d != 32)) {
                idx++
                break
            }
        }
        headLine = ""
        if (idx > 0) headLine = strmid(curLine, 0, idx)
        newLine = cat(headLine, tailLine)
        PutBufLine(hbuf, pos, newLine)
        if (idx > 0) sel.ichFirst = idx
        else sel.ichFirst = 0
        sel.ichLim = sel.ichFirst
        SetWndSel(hwnd, sel)

    }
}

//刪除光標右邊的東西, 邏輯同刪除光標左邊的東西
macro right_del() {
    hbuf = GetCurrentBuf()
    hwnd = GetCurrentWnd()
    if (hwnd == hNil || hbuf == hNil) return 0
    sel = GetWndSel(hwnd)
    if (sel.fExtended ) return 0
    pos = sel.lnFirst
    curLine = GetBufLine(hbuf, pos)
    curLen = strlen(curLine)
    if (curLen == 0 || sel.ichFirst == curLen) {
        //空行或若在行尾, 添加下一行到當前行; 若下一行爲空, 則迭代到非空的行
        nLine = GetBufLineCount(hbuf)
        if (pos >= nLine - 1) return 0 //已經是最後一行, 則退出
        idx = pos + 1
        nextLine = ""
        while (idx < nLine) {
            //尋找有數據的行
            nextLine = getBufLine(hbuf, idx)
            idx++   //返回的idx永遠指向下一行
            if (strlen(nextLine) > 0) break
        }
        //
        while (--idx > pos) DelBufLine(hbuf, idx)
        newLine = cat(curLine, nextLine)
        PutBufLine(hbuf, pos, newLine)
        SetWndSel(hwnd, sel)
    }
    else {
        //非空行且若非行尾, 則向右刪除到遇到的非字符
        idx = sel.ichFirst
        headLine = strmid(curLine, 0, idx)
        postChar = -1
        postSpace = -1
        while (idx < curLen) {
            //退出的條件有兩個: 
            //1.若光標旁邊的字符串時, 然後遇到非字符串時; 或者相反的情況
            //2.若遇到空格, 接下來不是空格時
            ischar = 0
            d = AsciiFromChar(curLine[idx])
            if ((d >= 65 && d <= 90) || (d >= 97 && d <= 122)) ischar = 1
            if (d == 32) if (postSpace < 0) postSpace = 1
            if (postChar < 0) postChar = ischar
            if ((postChar != ischar) || (postSpace > 0 && d != 32)) break
            idx++
        }
        tailLine = ""
        if (idx < curLen) tailLine = strmid(curLine, idx, curLen)
        newLine = cat(headLine, tailLine)
        PutBufLine(hbuf, pos, newLine)
        SetWndSel(hwnd, sel)
    }
}


// 跳轉到當前文件的對應的.h或.cpp文件, 僅在同一個目錄的
// .h.hpp=>.c.cpp
// .c.cpp=>.h.hpp
macro find_h_cpp() {
    hbuf = GetCurrentBuf ()
    if (!hbuf) stop
    fname = GetBufName (hbuf)
    nname = ""
    nname_1 = ""
    ext = ""
    flen = strlen(fname)
    pos = flen
    while (pos > 0) {
        pos = pos - 1
        cha = fname[pos]
        if (cha == ".") {
            tname = strtrunc(fname, pos + 1)
            text = strmid(fname, pos + 1, flen)
            ext = toupper (text)
            if (ext == "H" || ext == "HPP") {
                nname = tname # "c"
                nname_1 = tname # "cpp"
                break
            }
            else if (ext == "C" || ext == "CPP") {
                nname = tname # "h"
                nname_1 = tname # "hpp"
                break
            }
            else {
                msg("not a valid file: " # fname)
                Return 
            }
        }
    }
    if (strlen(nname) > 0) {
        hnbuf = OpenBuf(nname)
        if (hnbuf) {
            SetCurrentBuf (hnbuf)
            return
        }
    }
    if (strlen(nname_1) > 0) {
        hnbuf = OpenBuf(nname_1)
        if (hnbuf) 
            SetCurrentBuf (hnbuf)
        else
            msg("cannot find @nname@")
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章