Source Insight 简单自定义宏

        原来一直用记事本看/写代码,一直感觉挺好的,现在看的代码文档与注释都不是很多,内部还到处引用。记事本有点不是那么得心应手了。于是到网上找一款编辑器,好像大家都在用写字本。我疑惑啊,这记事本和写字本有个什么区别啊?再后来,我找到了Source Insight,用起来还不错。由于要经常加入文件头,函数头什么的。Source Insight又没有声音控制,不能向siri一样对她说:“给我加入文件头”,于是我退而求其次的决定使用Source Insight宏来完成这项任务。下面是我使用的宏文件。

 

macro GetFileName(pathName)
{
    nlength = strlen(pathName)
    i = nlength - 1
    name = ""
    while (i + 1)
    {
        ch = pathName[i]
        if ("\\" == "@ch@")
            break
        i = i - 1
    }
    i = i + 1
    while (i < nlength)
    {
        name = cat(name, pathName[i])
        i = i + 1
    }

    return name
}

macro InsertPersonalMark()
{
    sz = ""
    hbuf = GetCurrentBuf()
    ln = GetBufLnCur(hbuf)
    sz = Cat(sz, "")
    sz = Cat(sz, "/*------------------------------- myname start ----------------------------------*/")
    InsBufLine(hbuf, ln+1, sz)
    InsBufLine(hbuf, ln+2, "    ")
    InsBufLine(hbuf, ln+3, "/*------------------------------- myname end ------------------------------------*/")

    SetBufIns(hbuf, ln+2, 4)
}

macro InsertHeader2()
{
    hbuf = GetCurrentBuf()
    szFunc = GetCurSymbol()
    ln = GetSymbolLine(szFunc)
    
    InsBufLine(hbuf, ln, "/*------------------------------------------------------------------------")
    sz = " *  "
    sz = cat(sz, szFunc)
    sz = cat(sz, " - ")
    InsBufLine(hbuf, ln+1, sz)
    InsBufLine(hbuf, ln+2, " *----------------------------------------------------------------------*/")
    
    // put the insertion point inside the header comment
    length = strlen(sz)
    SetBufIns(hbuf, ln+1, length)
}

/* InsertFileHeader:

   Inserts a comment header block at the top of the current function.
   This actually works on any type of symbol, not just functions.

   To use this, define an environment variable "MYNAME" and set it
   to your email name.  eg. set MYNAME=raygr
*/

macro InsertFileHeader2()
{
    hbuf = GetCurrentBuf()
    szpathName = GetBufName(hbuf)
    szfileName = GetFileName(szpathName)
    sz = ""

    sz = cat(sz, " /* ")
    sz = cat(sz, szfileName)
    sz = cat(sz, " - ")
    
    length = strlen(sz)

    sz = cat(sz, " */")
    InsBufLine(hbuf, 0, sz)

    SetBufIns(hbuf, 0, length)
}

// Returns the index of the first non whitespace character
// Search starts at offset specified by 'first'
macro SkipWST( string, first )
{
    len = strlen( string )
    i = first

    while( i < len )
    {
        if( string[i] == " " || string[i] == "    " )
            i = i + 1
        else
            break
    }

    return i
}

macro CommentBlock()
{
    hbuf = GetCurrentBuf();
    hwnd = GetCurrentWnd();

    sel = GetWndSel(hwnd);

    if (hwnd != 0 && buf != 0 )
    {
        // insert /* on first line of selection
        iLine = sel.lnFirst;

        szLine = GetBufLine(hbuf, iLine);
        start = SkipWST( szLine, 0 )
        sz = strmid(szLine, 0, start)
        sz = cat(sz, "/* ")
        length = strlen(szLine)
        szBuf = strmid(szLine, start, length)
        sz = cat(sz, szBuf)
        PutBufLine(hbuf, iLine, sz)

        // insert */ on last line of selection
        iLine = sel.lnLast
        szLine = GetBufLine(hbuf, iLine)
        szLine = cat(szLine, " */")
                    PutBufLine(hbuf, iLine, szLine)

        // if (sel.lnFirst == sel.lnLast)
        {
            sel.ichFirst = start
            sel.ichLim = length + 6
        }
    }
    
    SetWndSel(hwnd, sel);
}


关于宏的使用:打开Base工程,在utils.em中加入以上宏。在菜单Options --> Menu assigment里将相关的宏加入到菜单中,就可以了。

 

注:GetFileName宏是从网上copy的,其它宏是Source Insight官方宏库里修改的,或参照重新编写的。

Source Insight 简单自定义宏_第1张图片

你可能感兴趣的:(Source Insight 简单自定义宏)