source insight增加注释宏

时间:2023-03-10 04:51:23
source insight增加注释宏

打开base项目,下main.c文件里加入以下代码

对于在一行内的局部注释, 没有找到解除注释的宏,

macro Comments_orUn_gx()
{   //用杠星注释多行,或取消杠星注释 ,不选中多行时,只处理当前行
    hwnd = GetCurrentWnd()
    hbuf = GetCurrentBuf()
    )
        stop   

    // debugBuf只是为了调试
    // debugBuf = NewBuf("debugBuf")
    // ClearBuf(debugBuf)   

    lnSelFirst = GetWndSelLnFirst(hwnd)     // 获得选中内容的第一行
    lnSelLast = GetWndSelLnLast(hwnd)       // 获得选中内容的最后一行  

    const_space = " "               // 空格
    const_comments_begin = "/*"             // 多行注释符号-开始
    const_comments_end = "*/"           // 多行注释符号-结束
    isCancelComments =    

    // 跳过开始的空行,否则下面报错
    line_index = lnSelFirst
    orig_text = GetBufLine(hbuf, line_index)    // 获得第一行的text
    )
    {
        line_index = line_index +
        orig_text = GetBufLine(hbuf, line_index)            // 获得下一行的text
    }  

    // 根据第一行选中文本,确定是“注释”,还是“取消注释”
    // 判断是否以“//”开始,如果是则认为是“取消注释”,首先需要去掉空格
    subIndex =
    ) == const_space)
            subIndex = subIndex +    

    ) == const_comments_begin)    // 以“/*”开头,取消注释
    {
        isCancelComments =    

        dest_text = strmid(orig_text, subIndex+, strlen(orig_text))
        )
        {
            DelBufLine(hbuf, line_index)                // 本行只有“/*”时,删除
        }
        else
        {
            PutBufLine (hbuf, line_index, dest_text)        // 替换以前的text
        }
        line_index = line_index +
    }
    else    // 进行注释
    {
        InsBufLine(hbuf, lnSelFirst, "/*")
        InsBufLine(hbuf, lnSelLast+,  "*/")  

        stop
    }  

    // 遍历所有选中的行
    // line_index = lnSelFirst                      // 前面已经跳过开头的空行
    // while(line_index <= lnSelLast)                    // 对选中的内容进行操作
    while(line_index < GetBufLineCount(hbuf))                //or 从当前行开始,查找到第一个“*/”为止或到结尾
    {
        orig_text = GetBufLine(hbuf, line_index)    // 获得以前的text
        )                   // 如果是空行或只有一个字符,则跳过
        {
            dest_text = ""
            )               // 取消注释
            {
                // 查找注释符“*/”
                subIndex =
                 && strmid(orig_text, subIndex, subIndex+) != const_comments_end)
                        subIndex = subIndex +    

                ) == const_comments_end)  // 找到“*/”,进行处理
                {
                    prefixText = strmid(orig_text, , subIndex)             // 前面的text
                    lastText = strmid(orig_text, subIndex+, strlen(orig_text))     // 后面的text
                    dest_text = cat(prefixText, lastText)   

                    )
                    {
                        DelBufLine(hbuf, line_index)                // 本行只有“*/”时,删除
                    }
                    else
                    {
                        PutBufLine (hbuf, line_index, dest_text)        // 替换以前的text
                    }  

                    break                               // 退出
                }
            }
        }  

        line_index = line_index +
    }
}  

macro CommentSelecte_inOneLine()
{   //注释选中,只在单行中有效,不选中任何字符的话就在光标处插入一对杠星注释符
    hbuf = GetCurrentBuf()
    ln = GetBufLnCur(hbuf)
    str = GetBufSelText(hbuf)
    str = cat("/*",str)
    str = cat(str,"*/")
    SetBufSelText (hbuf, str)
}  

macro _tsGetTabSize()
{  //只被Comment_gx() 宏调用
    szTabSize = GetReg("TabSize");    

    if (szTabSize != "")
    {
        tabSize = AsciiFromChar(szTabSize[]) - AsciiFromChar(");
    }
    else
    {
        tabSize = ;
    }    

    return tabSize;
}    

macro Comment_gx()
{  //用杠星注释,不换行,至少注释一行,不推荐使用
    hbuf = GetCurrentBuf();
    hwnd = GetCurrentWnd();    

    sel = GetWndSel(hwnd);    

    iLine = sel.lnFirst;    

    // indicate the comment char according to the file type
    // for example, using "#" for perl file(.pl) and "/* */" for C/C++.
    filename = tolower(GetBufName(hbuf));
    suffix = "";
    len = strlen(filename);
    i = len - ;
    )
    {
        ] == ".")
        {
            suffix = strmid(filename, i, len)
            break;
        }
        i = i -;
    }
    if  ( suffix == "pl" )
    {
        filetype = ; // PERL
    }
    else
    {
        filetype = ; // C
    }    

    szLine = GetBufLine(hbuf, iLine);
    )  // C
    {
        szLine = cat("/*", szLine);
    }
    else                // PERL
    {
        szLine = cat("# ", szLine);
    }
    PutBufLine(hbuf, iLine, szLine);
    iLine = sel.lnLast;
    szLine = GetBufLine(hbuf, iLine);
    )  // C
    {
        szLine = cat(szLine, "*/");
    }
    else                // PERL
    {
        szLine = cat("# ", szLine);
    }
    PutBufLine(hbuf, iLine, szLine);    

    if (sel.lnFirst == sel.lnLast)
    {
        tabSize = _tsGetTabSize() - ;
        sel.ichFirst = sel.ichFirst + tabSize;
        sel.ichLim = sel.ichLim + tabSize;
    }
    SetWndSel(hwnd, sel);
}    

macro Comment_gg()
{   //用杠杠注释,不选中多行的话,注释当前行
    hwnd = GetCurrentWnd()
    selection = GetWndSel( hwnd )
    lnFirst = GetWndSelLnFirst( hwnd )
    lnLast = GetWndSelLnLast( hwnd )  

    hbuf = GetCurrentBuf()  

    ln = lnFirst
    buf = GetBufLine( hbuf, ln )
    len = strlen( buf )
    firststart = len
    while( ln <= lnLast )
    {
        buf = GetBufLine( hbuf, ln )
        len = strlen( buf )
        start =
        while( start < len )
        {
             ) == CharFromAscii() || strmid( buf, start, start +  ) == CharFromAscii() )
            {
                start = start +
                if( start > len )
                    break
            }
            else
                break
        }
        if( start < len && start < firststart )
        {
            firststart = start
        }
        ln = ln +
    }  

    ln = lnFirst
    while( ln <= lnLast )
    {
        buf = GetBufLine( hbuf, ln )
        len = strlen( buf )
         )
        {
            buf2 = cat( cat( strmid( buf, , firststart ), "//" ), strmid( buf, firststart, len ) )
            PutBufLine ( hbuf, ln, buf2 )
        }
        ln = ln +
    }
    SetWndSel( hwnd, selection )
}  

macro unComment_gg()
{   //取消杠杠注释,不选中多行的话,默认只处理当前行
    hwnd = GetCurrentWnd()
    selection = GetWndSel( hwnd )
    lnFirst = GetWndSelLnFirst( hwnd )
    lnLast = GetWndSelLnLast( hwnd )  

    hbuf = GetCurrentBuf()
    ln = lnFirst
    while( ln <= lnLast )
    {
        buf = GetBufLine( hbuf, ln )
        len = strlen( buf )
         )
        {
            start =   

             ) == CharFromAscii() || strmid( buf, start, start +  ) == CharFromAscii() )
            {
                start = start +
                if( start >= len )
                    break
            }
             )
            {
                 ) == "//" )
                {
                    buf2 = cat( strmid( buf, , start ), strmid( buf, start + , len ) )
                    PutBufLine( hbuf, ln, buf2 )
                }
            }
        }
        ln = ln +
    }
    SetWndSel( hwnd, selection )
} 

增加后,通过在键分配中分配宏命令的按键