source insight 自定义菜单
打开base工程,编辑utils.em文件,把下面的函数添加到文件末尾
在Options-->menu assignment, 左边选择Macro:Comments_orUn_gx 右边选择work, insert
//添加或取消杠星注释
macro Comments_orUn_gx()
{ //用杠星注释多行,或取消杠星注释 ,不选中多行时,只处理当前行
hwnd = GetCurrentWnd()
hbuf = GetCurrentBuf()
if(hbuf ==0)
stop
// debugBuf只是为了调试
// debugBuf = NewBuf("debugBuf")
// ClearBuf(debugBuf)
lnSelFirst = GetWndSelLnFirst(hwnd) // 获得选中内容的第一行
lnSelLast = GetWndSelLnLast(hwnd) // 获得选中内容的最后一行
const_space = " " // 空格
const_comments_begin = "/*" // 多行注释符号-开始
const_comments_end = "*/" // 多行注释符号-结束
isCancelComments = 0
// 跳过开始的空行,否则下面报错
line_index = lnSelFirst
orig_text = GetBufLine(hbuf, line_index) // 获得第一行的text
while(strlen(orig_text) == 0)
{
line_index = line_index + 1
orig_text = GetBufLine(hbuf, line_index) // 获得下一行的text
}
// 根据第一行选中文本,确定是“注释”,还是“取消注释”
// 判断是否以“//”开始,如果是则认为是“取消注释”,首先需要去掉空格
subIndex = 0
while(strmid(orig_text, subIndex, subIndex+1) == const_space)
subIndex = subIndex + 1
if (strmid(orig_text, subIndex, subIndex+2) == const_comments_begin) // 以“/*”开头,取消注释
{
isCancelComments = 1
dest_text = strmid(orig_text, subIndex+2, strlen(orig_text))
if(strlen(dest_text) == 0)
{
DelBufLine(hbuf, line_index) // 本行只有“/*”时,删除
}
else
{
PutBufLine (hbuf, line_index, dest_text) // 替换以前的text
}
line_index = line_index + 1
}
else // 进行注释
{
InsBufLine(hbuf, lnSelFirst, "/*")
InsBufLine(hbuf, lnSelLast+2, "*/")
stop
}
// 遍历所有选中的行
// line_index = lnSelFirst // 前面已经跳过开头的空行
// while(line_index <= lnSelLast) // 对选中的内容进行操作
while(line_index < GetBufLineCount(hbuf)) //or 从当前行开始,查找到第一个“*/”为止或到结尾
{
orig_text = GetBufLine(hbuf, line_index) // 获得以前的text
if (strlen(orig_text) > 1) // 如果是空行或只有一个字符,则跳过
{
dest_text = ""
if(isCancelComments == 1) // 取消注释
{
// 查找注释符“*/”
subIndex = 0
while(subIndex < strlen(orig_text)-2 && strmid(orig_text, subIndex, subIndex+2) != const_comments_end)
subIndex = subIndex + 1
if (strmid(orig_text, subIndex, subIndex+2) == const_comments_end) // 找到“*/”,进行处理
{
prefixText = strmid(orig_text, 0, subIndex) // 前面的text
lastText = strmid(orig_text, subIndex+2, strlen(orig_text)) // 后面的text
dest_text = cat(prefixText, lastText)
if(strlen(dest_text) == 0)
{
DelBufLine(hbuf, line_index) // 本行只有“*/”时,删除
}
else
{
PutBufLine (hbuf, line_index, dest_text) // 替换以前的text
}
break // 退出
}
}
}
line_index = line_index + 1
}
}
//添加#if 0注释
macro Custom_AddMacroComment()//Alt+1
{ //add #if 0 #endif
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 )
}