用了許久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 timeszTime = GetSysTime(1)Hour = szTime.HourMinute = szTime.MinuteSecond = szTime.SecondDay = szTime.DayMonth = szTime.MonthYear = szTime.Yearif (Day < 10)szDay = "0@Day@"elseszDay = Day//szMonth = NumToName(Month)if (Month < 10) szMonth = "0@Month@"elseszMonth = MonthszDescription = Ask("請輸入修改原因")// begin assembling the title stringInsBufLine(hbuf, ln+1, "/*@szDescription@ @szMyName@.xmyanfa @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 timeszTime = GetSysTime(1)Hour = szTime.HourMinute = szTime.MinuteSecond = szTime.SecondDay = szTime.DayMonth = szTime.MonthYear = szTime.Yearif (Day < 10)szDay = "0@Day@"elseszDay = Day//szMonth = NumToName(Month)if (Month < 10) szMonth = "0@Month@"elseszMonth = MonthszDescription = Ask("請輸入修改原因:")// begin assembling the title stringInsBufLine(hbuf, ln + 1, "/*@szDescription@ @szMyName@.xmyanfa @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 timeszTime = GetSysTime(1)Hour = szTime.HourMinute = szTime.MinuteSecond = szTime.SecondDay = szTime.DayMonth = szTime.MonthYear = szTime.Yearif (Day < 10)szDay = "0@Day@"elseszDay = Day//szMonth = NumToName(Month)if (Month < 10) szMonth = "0@Month@"elseszMonth = MonthInsBufLine(hbuf, ln + 1, "/*@szMyName@.xmyanfa @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);}