Source Insight 中有个叫 Base 的项目,里面是其自带的宏语言例子。
使用其自带的宏已经可以做很多事情了,如对选中的代码自动添加#if和#endif,产生文件头注释等。
使用这个宏语言可以写一些新的功能,减少我们程序员的劳动强度。
比如,C语言的头文件中有很多#if宏,而且有很多是嵌套的,更大的问题是很多大型代码里,#if宏大多不像代码一样缩进,所以查找那个#elif或#else跟哪个#if配对就很麻烦,#endif还好,在Source Insight 中只要双击就可以选中整个if块。
为了更方便的查找#if的下一个#elif或#else,我写了以下的宏:
//查找#if 或#else #elif 的下一块。
//主要用于跳过复杂的宏块。
macro FindNextBlock()
{
hbuf = GetCurrentBuf()
lnMax = GetBufLineCount(hbuf)
lnCur = GetBufLnCur(hbuf)
tempStr = GetBufLine (hbuf, lnCur)
tempHead = GetCMacroHead(tempStr)
flag = 0
if(tempHead != "#if" && tempHead !="#el")
{
Msg("Error:selecte line!");
return
}
else
{
lnCur = lnCur + 1
while(lnCur <lnMax)
{
tempStr = GetBufLine (hbuf, lnCur)
tempHead = GetCMacroHead(tempStr)
if(flag == 0)
{
if(tempHead == "#el" || tempHead == "#en")
{
//stop
SetBufIns (hbuf, lnCur, 0)
Msg(lnCur)
return
}
}
else
{
if(tempHead == "#en")
{
flag = flag -1
}
}
if(tempHead == "#if")
{
flag = flag + 1
}
//下一行
lnCur = lnCur+1
}
}
}
//主要用于跳过复杂的宏块。
macro FindNextBlock()
{
hbuf = GetCurrentBuf()
lnMax = GetBufLineCount(hbuf)
lnCur = GetBufLnCur(hbuf)
tempStr = GetBufLine (hbuf, lnCur)
tempHead = GetCMacroHead(tempStr)
flag = 0
if(tempHead != "#if" && tempHead !="#el")
{
Msg("Error:selecte line!");
return
}
else
{
lnCur = lnCur + 1
while(lnCur <lnMax)
{
tempStr = GetBufLine (hbuf, lnCur)
tempHead = GetCMacroHead(tempStr)
if(flag == 0)
{
if(tempHead == "#el" || tempHead == "#en")
{
//stop
SetBufIns (hbuf, lnCur, 0)
Msg(lnCur)
return
}
}
else
{
if(tempHead == "#en")
{
flag = flag -1
}
}
if(tempHead == "#if")
{
flag = flag + 1
}
//下一行
lnCur = lnCur+1
}
}
}
GetCMacroHead是自己写的函数,获得宏的开头三个字符,代码很简单,就不贴了。