最近研究了下sourceinsight的宏編制,挺有意思的,結合網上摘抄的部分代碼,然后自己修改了些細節,整體效果還不錯,這里貼出來供其他人參考參考;
第一個宏:插入文件描述信息
macro InsertFileHeader() { var time; var filename; var comments; szMyName = "xxx";//getenv(MYNAME) time = getCommentsTime(); filename = GetFullFileName(); hbuf = GetCurrentBuf(); InsBufLine ( hbuf, 0, "/**************************************************************************" ); InsBufLine ( hbuf, 1, "* 公 司 : xxxxxxxxxx" ); if ( strlen ( szMyName ) > 0 ) { InsBufLine ( hbuf, 2, "* 地 址 : xxxxxxxxxxxxxxxxxx" ); InsBufLine ( hbuf, 3, "* 網 址 : xxxxxxxxxxxxxxxx" ); sz = "* 編 制 : @szMyName@"; InsBufLine ( hbuf, 4, sz ); comments = "* 時 間 : "; comments = cat ( comments, time ); InsBufLine ( hbuf, 5, comments ); InsBufLine ( hbuf, 6, "* 描 述 : " ) ln = 7; } else ln = 2; InsBufLine ( hbuf, ln, "**************************************************************************/" ); }
這個宏在sourceinsight內的效果如圖所示:
在MDK內顯示效果如圖所示:
看得出來在兩個不同的編輯環境內沒有產生對齊異常的問題;
第二個宏:添加#ifdef0---#endif條件編譯
macro Ifdefin0() { hwnd = GetCurrentWnd(); sel = GetWndSel ( hwnd ); lnFirst = GetWndSelLnFirst ( hwnd ); lnLast = GetWndSelLnLast ( hwnd ); hbuf = GetCurrentBuf(); if ( LnFirst == 0 ) { szIfStart = ""; } else { szIfStart = GetBufLine ( hbuf, LnFirst - 1 ); } szIfEnd = GetBufLine ( hbuf, lnLast + 1 ); if ( szIfStart == "#if 0" && szIfEnd == "#endif" ) { DelBufLine ( hbuf, lnLast + 1 ); DelBufLine ( hbuf, lnFirst - 1 ); //sel.lnFirst = sel.lnFirst + 1; //sel.lnLast = sel.lnLast + 1; } else { InsBufLine ( hbuf, lnFirst, "#if 0" ); InsBufLine ( hbuf, lnLast + 2, "#endif" ); sel.lnFirst = sel.lnFirst + 1; sel.lnLast = sel.lnLast + 1; } SetWndSel ( hwnd, sel ); }
第三個宏:添加函數描述信息
macro getFunType() { var fType; fType = ASK ( "請輸入函數返回類型" ); return fType; } macro getFunName() { var fName; fName = ASK ( "請輸入函數名字" ); return fName; } macro getFunDesc() { var fDesc; fDesc = ASK ( "請輸入函數描述" ); return fDesc; } macro getFunParamNum() { var fParamNum; fParamNum = ASK ( "請輸入參數個數" ) ; return fParamNum; } macro getAuthor() { var fAuthor; fAuthor = ASK ( "請輸入作者姓名工號" ) ; return fAuthor; } macro getCommentsTime() { var year; var month; var day; var commTime; var sysTime; sysTime = GetSysTime ( 1 ); year = sysTime.Year; month = sysTime.month; day = sysTime.day; commTime = "@year@-@month@-@day@"; return commTime; } macro insertFunComment() { var hBuff; var line; var ftype; var fname; var fdesc; var fiparam; var fiparam2; var fauthor; var time; var comments; var num; var num2; var num3; num = getFunParamNum(); num2 = num; time = getCommentsTime(); fauthor = getAuthor(); ftype = getFunType(); fname = getFunName(); fdesc = getFunDesc(); hBuff = GetCurrentBuf(); line = GetBufLnCur ( hBuff ); InsBufLine ( hBuff, line, "/*************************************************************" ); comments = "* 函 數 名 : "; comments = cat ( comments, fname ); InsBufLine ( hBuff, line + 1, comments ); comments = "* 函數描述 : "; comments = cat ( comments, fdesc ); InsBufLine ( hBuff, line + 2, comments ); comments = "* 參數列表 : "; while ( num-- ) { num3 = num2 - num; fiparam = ASK ( "本函數共@num2@個參數,請輸入第@num3@個參數" ); if ( num3 == num2 ) fiparam2 = cat ( fiparam2, fiparam ); else { fiparam2 = cat ( fiparam2, fiparam ); fiparam2 = cat ( fiparam2, ", " ); } if ( num3 == 1 ) { comments = cat ( comments, fiparam ); InsBufLine ( hBuff, line + 2 + num3, comments ); } else { comments = "* "; comments = cat ( comments, fiparam ); InsBufLine ( hBuff, line + 2 + num3, comments ); } } comments = "* 返回類型 : "; comments = cat ( comments, ftype ); InsBufLine ( hBuff, line + 3 + num3, comments ); comments = "* 編 制 者 : "; comments = cat ( comments, fauthor ); InsBufLine ( hBuff, line + 4 + num3, comments ); comments = "* 編制時間 : "; comments = cat ( comments, time ); InsBufLine ( hBuff, line + 5 + num3 comments ); InsBufLine ( hBuff, line + 6 + num3, "*************************************************************/" ); //下來是生成函數體部分 //函數定義部分 comments = ""; comments = cat ( comments, ftype ); comments = cat ( comments, " " ); comments = cat ( comments, fname ); comments = cat ( comments, "(" ); comments = cat ( comments, fiparam2 ); comments = cat ( comments, ")" ); InsBufLine ( hBuff, line + 7 + num3, comments ); InsBufLine ( hBuff, line + 8 + num3, "{" ); InsBufLine ( hBuff, line + 9 + num3, " " ); comments = " "; InsBufLine ( hBuff, line + 10 + num3, "}" ); }
最后在sourceinsight內的運行效果如圖所示:
在MDK內的顯示效果如圖所示:
看得出來在兩個不同的編輯環境內也沒有產生對齊異常的問題;