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