Source Insight中的宏(转) - hustfeiji
我们经常要对一整段代码进行注释,很多代码编辑器都提供了这样的功能:用快捷键“Ctrl + /”来实现“//”的多行注释。
但是在用source insight的时候,发现竟然没有这样的功能。于是在网上搜了一下,sourceinsight里面的多行注释可以用宏来实现。
以下是实现多行注释的宏代码(在别的网站copy过来的,经过测试,还是很好用的):
macro MultiLineComment()
{
hwnd = GetCurrentWnd()
selection = GetWndSel(hwnd)
LnFirst =GetWndSelLnFirst(hwnd) //取首行行号
LnLast =GetWndSelLnLast(hwnd) //取末行行号
hbuf = GetCurrentBuf()
if(GetBufLine(hbuf, 0) ==”//magic-number:tph85666031″){
stop
}
Ln = Lnfirst
buf = GetBufLine(hbuf, Ln)
len = strlen(buf)
while(Ln <= Lnlast) {
buf = GetBufLine(hbuf, Ln) //取Ln对应的行
if(buf ==””){ //跳过空行
Ln = Ln + 1
continue
}
if(StrMid(buf, 0, 1) == “/”){ //需要取消注释,防止只有单字符的行
if(StrMid(buf, 1, 2) == “/”){
PutBufLine(hbuf, Ln, StrMid(buf, 2, Strlen(buf)))
}
}
if(StrMid(buf,0,1) !=”/”){ //需要添加注释
PutBufLine(hbuf, Ln, Cat(“//”, buf))
}
Ln = Ln + 1
}
SetWndSel(hwnd, selection)
}
将上面的代码另存为Comment.em文件。假设宏文件中有宏Marco: SuperBackspace,我们要将该宏绑定到BackSpace键。操作步骤如下:
① 将Comment.em复制入SourceInsight的Base工程路径下(一般是在C:\Documents and Settings\Administrator\My Documents\Source Insight\Projects\Base),也可以放在其他位置;
② 打开SourceInsight,选择菜单 Project→Open Project,打开Base项目(Base项目是SourceInsight安装是就有的);
③ Project->Add and Remove Project Files… 加入宏文件(也可以把宏代码加到已有文件中,如Utils.em文件中);
④ 重启SourceInsight;
⑤ 绑定到快捷键:选择菜单Options→Key Assignments,将Marco:SuperBackspace绑定到Alt+2组合键;
这里还有一份添加“#ifdef 0”和“#endif”的宏代码:
macro AddMacroComment()
{
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 CommentSingleLine()
{
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)
str = GetBufLine (hbuf, ln)
str = cat(“/*”,str)
str = cat(str,”*/”)
PutBufLine (hbuf, ln, str)
}
将一行中鼠标选中部分注释掉:
macro CommentSelStr()
{
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)
str = GetBufSelText(hbuf)
str = cat(“/*”,str)
str = cat(str,”*/”)
SetBufSelText (hbuf, str)
}
打开文件所在目录,并高亮。
/**
* 使用资源管理器打开当前文件所在文件夹,并个高亮选中当前文件 推荐快捷键 ctrl+T
*/
macro ToExplorerFolder()
{
buf = GetCurrentBuf();
curFilePath = GetBufName(buf);
cmdLine = “explorer.exe /select,@curFilePath@”; // 其实就是通过explorer.exe 命令行直接打开 eg: explorer.exe /select,C:\xx.log 会自动选中xx.log
RunCmdLine(cmdLine, Nil, 0);
}
//在sourceinsight的宏语言中,@str@ 可以连接两个字符串
最后是source insight与宏有关的资源: