用了许久source Insight写C/C++代码,发现其中没有块注释功能很不方便,于是今天研究了下怎样让sourceInsight实现块注释。
网上介绍了很多方法实现块注释,但是都是对代码利用“//”逐行注释,没有用“/* */”实现的,我个人比较倾向于用/* */注释代码块,所以今天自己动手写了利用”/* */“实现块注释代码。
好了,废话不多说,直接上宏代码,后面会介绍使用方法:
macro _tsGetTabSize() { szTabSize = GetReg("TabSize"); if (szTabSize != "") { tabSize = AsciiFromChar(szTabSize[0]) - AsciiFromChar("0"); } else { tabSize = 4; } return tabSize; } macro CommentBlock_Joyce() { 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); } // // Undo the CommentBlock for the selected text. // macro UnCommentBlock_Joyce() { 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 } tabSize = 0; endLine = GetBufLine(hbuf, sel.lnLast); endLineLen = strlen(endLine); szLine = GetBufLine(hbuf, iLine); len = strlen(szLine); szNewLine = ""; commentState = 1; if (szLine[0] == "/" && szLine[1] == "*") { if(endLine[endLineLen-2] == "/" && endLine[endLineLen-3] == "*") { if (filetype == 1) // C { if (len > 1) { if (szLine[0] == "/" && szLine[1] == "*") { if (len > 2) { if (AsciiFromChar(szLine[2]) == 9) { tabSize = _tsGetTabSize() - 1; szNewLine = strmid(szLine, 3, strlen(szLine)); } } if (szNewLine == "") { szNewLine = strmid(szLine, 2, strlen(szLine)); tabSize = 2; } PutBufLine(hbuf, iLine, szNewLine); } } } if (filetype == 2) // PERL { if (len > 0) { if (szLine[0] == "#") { if (len > 1) { if (AsciiFromChar(szLine[1]) == 9) { tabSize = _tsGetTabSize() - 1; szNewLine = strmid(szLine, 2, strlen(szLine)); } } if (szNewLine == "") { szNewLine = strmid(szLine, 1, strlen(szLine)); tabSize = 2; } PutBufLine(hbuf, iLine, szNewLine); } } } iLine = sel.lnLast; szLine = GetBufLine(hbuf, iLine); len = strlen(szLine); szNewLine = ""; if (filetype == 1) // C { if (len > 1) { if (szLine[strlen(szLine)-2] == "/" && szLine[strlen(szLine)-3] == "*") { if (len > 2) { if (AsciiFromChar(szLine[2]) == 9) { tabSize = _tsGetTabSize() - 1; szNewLine = strmid(szLine, 0, strlen(szLine)-2); } } if (szNewLine == "") { szNewLine = strmid(szLine, 0, strlen(szLine)-3); tabSize = 2; } PutBufLine(hbuf, iLine, szNewLine); } } } if (filetype == 2) // PERL { if (len > 0) { if (szLine[0] == "#") { if (len > 1) { if (AsciiFromChar(szLine[1]) == 9) { tabSize = _tsGetTabSize() - 1; szNewLine = strmid(szLine, 2, strlen(szLine)); } } if (szNewLine == "") { szNewLine = strmid(szLine, 1, strlen(szLine)); tabSize = 2; } PutBufLine(hbuf, iLine, szNewLine); } } } } } if (sel.lnFirst == sel.lnLast) { sel.ichFirst = sel.ichFirst - tabSize; sel.ichLim = sel.ichLim - tabSize; } SetWndSel(hwnd, sel); }
下面介绍下使用方法:
1) 首先,打开sourceInsight 的"项目->打开项目->base”中的Utils.em文件,将以上宏代码复制到文件末尾,然后保存。
2) 启用宏。 菜单 “Options” -> “Key assignment”(中文版是选项->菜单关联)。 在列表框中找到下面的宏:CommentBlock_Joyce、UnCommentBlock_Joyce
3) 给这些宏分配按键。点击“键..”,选中你需要分配按键的宏,点击“分配新键..”,然后在键盘上选择你喜欢的按键吧~设置好之后,点击“好”。
好了,设置完毕,试试吧~
下面把网上有大神写的单行注释和利用“//”进行多行注释的代码一起贴上来,方便大家使用,设置方法和前面的一样。
macro SingleLineComment() { szMyName = "Joyce" // Get a handle to the current file buffer and the name // and location of the current symbol where the cursor is. hbuf = GetCurrentBuf() ln = GetBufLnCur(hbuf) // Get current time szTime = GetSysTime(1) Hour = szTime.Hour Minute = szTime.Minute Second = szTime.Second Day = szTime.Day Month = szTime.Month Year = szTime.Year if (Day < 10) szDay = "0@Day@" else szDay = Day //szMonth = NumToName(Month) if (Month < 10) szMonth = "0@Month@" else szMonth = Month szDescription = Ask("请输入修改原因") // begin assembling the title string InsBufLine(hbuf, ln+1, "/*@szDescription@ @[email protected] @Year@-@szMonth@-@szDay@*/") } macro MultiLineCommentHeader() { szMyName = "Joyce" // Get a handle to the current file buffer and the name // and location of the current symbol where the cursor is. hbuf = GetCurrentBuf() ln = GetBufLnCur(hbuf) // Get current time szTime = GetSysTime(1) Hour = szTime.Hour Minute = szTime.Minute Second = szTime.Second Day = szTime.Day Month = szTime.Month Year = szTime.Year if (Day < 10) szDay = "0@Day@" else szDay = Day //szMonth = NumToName(Month) if (Month < 10) szMonth = "0@Month@" else szMonth = Month szDescription = Ask("请输入修改原因:") // begin assembling the title string InsBufLine(hbuf, ln + 1, "/*@szDescription@ @[email protected] @Year@-@szMonth@-@szDay@ begin*/") } macro MultiLineCommentEnd() { szMyName = "Joyce" // Get a handle to the current file buffer and the name // and location of the current symbol where the cursor is. hbuf = GetCurrentBuf() ln = GetBufLnCur(hbuf) // Get current time szTime = GetSysTime(1) Hour = szTime.Hour Minute = szTime.Minute Second = szTime.Second Day = szTime.Day Month = szTime.Month Year = szTime.Year if (Day < 10) szDay = "0@Day@" else szDay = Day //szMonth = NumToName(Month) if (Month < 10) szMonth = "0@Month@" else szMonth = Month InsBufLine(hbuf, ln + 1, "/*@[email protected] @Year@-@szMonth@-@szDay@ end*/") } // // Comment the selected block of text using single line comments and indent it // macro CommentBlock() { hbuf = GetCurrentBuf(); hwnd = GetCurrentWnd(); sel = GetWndSel(hwnd); iLine = sel.lnFirst; // added by Yongqiang, indicate the comment char according to the file type // for example, using "#" for perl file(.pl) and "//" for others. 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 } while (iLine <= sel.lnLast) { szLine = GetBufLine(hbuf, iLine); if (filetype == 1) // C { szLine = cat("// ", szLine); } else // PERL { szLine = cat("# ", szLine); } PutBufLine(hbuf, iLine, szLine); iLine = iLine + 1; } if (sel.lnFirst == sel.lnLast) { tabSize = _tsGetTabSize() - 1; sel.ichFirst = sel.ichFirst + tabSize; sel.ichLim = sel.ichLim + tabSize; } SetWndSel(hwnd, sel); } // // Undo the CommentBlock for the selected text. // macro UnCommentBlock() { hbuf = GetCurrentBuf(); hwnd = GetCurrentWnd(); sel = GetWndSel(hwnd); iLine = sel.lnFirst; // added by Yongqiang, indicate the comment char according to the file type // for example, using "#" for perl file(.pl) and "//" for others. 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 } tabSize = 0; while (iLine <= sel.lnLast) { szLine = GetBufLine(hbuf, iLine); len = strlen(szLine); szNewLine = ""; if (filetype == 1) // C { if (len > 1) { if (szLine[0] == "/" && szLine[1] == "/") { if (len > 2) { if (AsciiFromChar(szLine[2]) == 9) { tabSize = _tsGetTabSize() - 1; szNewLine = strmid(szLine, 3, strlen(szLine)); } } if (szNewLine == "") { szNewLine = strmid(szLine, 2, strlen(szLine)); tabSize = 2; } PutBufLine(hbuf, iLine, szNewLine); } } } if (filetype == 2) // PERL { if (len > 0) { if (szLine[0] == "#") { if (len > 1) { if (AsciiFromChar(szLine[1]) == 9) { tabSize = _tsGetTabSize() - 1; szNewLine = strmid(szLine, 2, strlen(szLine)); } } if (szNewLine == "") { szNewLine = strmid(szLine, 1, strlen(szLine)); tabSize = 2; } PutBufLine(hbuf, iLine, szNewLine); } } } iLine = iLine + 1; } if (sel.lnFirst == sel.lnLast) { sel.ichFirst = sel.ichFirst - tabSize; sel.ichLim = sel.ichLim - tabSize; } SetWndSel(hwnd, sel); }