Source Insight 多行,单行,行内 注释,解除注释宏
程序员文章站
2022-06-01 16:05:51
...
Source Insight 各种好用,功能繁多, 但是没有注释的快捷键或菜单,要手动加宏实现
代码都是网上copy的, 但有的不能用,稍作修改了下,可以用了,都贴出来
美中不足,
对于在一行内的局部注释, 没有解除注释的宏,
不过这个也没关系了, 手动就可以,
macro Comments_orUn_gx() { //用杠星注释多行,或取消杠星注释 ,不选中多行时,只处理当前行 hwnd = GetCurrentWnd() hbuf = GetCurrentBuf() if(hbuf ==0) stop // debugBuf只是为了调试 // debugBuf = NewBuf("debugBuf") // ClearBuf(debugBuf) lnSelFirst = GetWndSelLnFirst(hwnd) // 获得选中内容的第一行 lnSelLast = GetWndSelLnLast(hwnd) // 获得选中内容的最后一行 const_space = " " // 空格 const_comments_begin = "/*" // 多行注释符号-开始 const_comments_end = "*/" // 多行注释符号-结束 isCancelComments = 0 // 跳过开始的空行,否则下面报错 line_index = lnSelFirst orig_text = GetBufLine(hbuf, line_index) // 获得第一行的text while(strlen(orig_text) == 0) { line_index = line_index + 1 orig_text = GetBufLine(hbuf, line_index) // 获得下一行的text } // 根据第一行选中文本,确定是“注释”,还是“取消注释” // 判断是否以“//”开始,如果是则认为是“取消注释”,首先需要去掉空格 subIndex = 0 while(strmid(orig_text, subIndex, subIndex+1) == const_space) subIndex = subIndex + 1 if (strmid(orig_text, subIndex, subIndex+2) == const_comments_begin) // 以“/*”开头,取消注释 { isCancelComments = 1 dest_text = strmid(orig_text, subIndex+2, strlen(orig_text)) if(strlen(dest_text) == 0) { DelBufLine(hbuf, line_index) // 本行只有“/*”时,删除 } else { PutBufLine (hbuf, line_index, dest_text) // 替换以前的text } line_index = line_index + 1 } else // 进行注释 { InsBufLine(hbuf, lnSelFirst, "/*") InsBufLine(hbuf, lnSelLast+2, "*/") stop } // 遍历所有选中的行 // line_index = lnSelFirst // 前面已经跳过开头的空行 // while(line_index <= lnSelLast) // 对选中的内容进行操作 while(line_index < GetBufLineCount(hbuf)) //or 从当前行开始,查找到第一个“*/”为止或到结尾 { orig_text = GetBufLine(hbuf, line_index) // 获得以前的text if (strlen(orig_text) > 1) // 如果是空行或只有一个字符,则跳过 { dest_text = "" if(isCancelComments == 1) // 取消注释 { // 查找注释符“*/” subIndex = 0 while(subIndex < strlen(orig_text)-2 && strmid(orig_text, subIndex, subIndex+2) != const_comments_end) subIndex = subIndex + 1 if (strmid(orig_text, subIndex, subIndex+2) == const_comments_end) // 找到“*/”,进行处理 { prefixText = strmid(orig_text, 0, subIndex) // 前面的text lastText = strmid(orig_text, subIndex+2, strlen(orig_text)) // 后面的text dest_text = cat(prefixText, lastText) if(strlen(dest_text) == 0) { DelBufLine(hbuf, line_index) // 本行只有“*/”时,删除 } else { PutBufLine (hbuf, line_index, dest_text) // 替换以前的text } break // 退出 } } } line_index = line_index + 1 } } 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[0]) - AsciiFromChar("0"); } else { tabSize = 4; } 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 - 1; while (i >= 0) { if (filename[i-1] == ".") { suffix = strmid(filename, i, len) break; } i = i -1; } if ( suffix == "pl" ) { filetype = 2; // PERL } else { filetype = 1; // C } szLine = GetBufLine(hbuf, iLine); if (filetype == 1) // C { szLine = cat("/*", szLine); } else // PERL { szLine = cat("# ", szLine); } PutBufLine(hbuf, iLine, szLine); iLine = sel.lnLast; szLine = GetBufLine(hbuf, iLine); if (filetype == 1) // C { szLine = cat(szLine, "*/"); } else // PERL { szLine = cat("# ", szLine); } PutBufLine(hbuf, iLine, szLine); if (sel.lnFirst == sel.lnLast) { tabSize = _tsGetTabSize() - 1; 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 = 0 while( start < len ) { if( strmid( buf, start, start + 1 ) == CharFromAscii(32) || strmid( buf, start, start + 1 ) == CharFromAscii(9) ) { start = start + 1 if( start > len ) break } else break } if( start < len && start < firststart ) { firststart = start } ln = ln + 1 } ln = lnFirst while( ln <= lnLast ) { buf = GetBufLine( hbuf, ln ) len = strlen( buf ) if( len > 0 ) { buf2 = cat( cat( strmid( buf, 0, firststart ), "//" ), strmid( buf, firststart, len ) ) PutBufLine ( hbuf, ln, buf2 ) } ln = ln + 1 } 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 ) if( len >= 2 ) { start = 0 while( strmid( buf, start, start + 1 ) == CharFromAscii(32) || strmid( buf, start, start + 1 ) == CharFromAscii(9) ) { start = start + 1 if( start >= len ) break } if( start < len - 2 ) { if( strmid( buf, start, start + 2 ) == "//" ) { buf2 = cat( strmid( buf, 0, start ), strmid( buf, start + 2, len ) ) PutBufLine( hbuf, ln, buf2 ) } } } ln = ln + 1 } SetWndSel( hwnd, selection ) }
代码加到utils.em中,或者新建个em文件,添加到base项目, 汉化版的加到"基础"项目
参考文章
http://download.csdn.net/download/ueiia/1587238
http://blog.csdn.net/tankles/article/details/6949409