自己动手编写了几个很不错的宏定义,分享出来,希望可以帮助大家。
//去掉字符串左边的空格
macro TrimLeft(szLine)
{
nLen = strlen(szLine)
if(nLen == 0)
{
return szLine
}
nIdx = 0
while( nIdx < nLen )
{
if( ( szLine[nIdx] != " ") && (szLine[nIdx] != "\t") )
{
break
}
nIdx = nIdx + 1
}
return strmid(szLine,nIdx,nLen)
}
//去掉字符串右边的空格
macro TrimRight(szLine)
{
nLen = strlen(szLine)
if(nLen == 0)
{
return szLine
}
nIdx = nLen
while( nIdx > 0 )
{
nIdx = nIdx - 1
if( ( szLine[nIdx] != " ") && (szLine[nIdx] != "\t") )
{
break
}
}
return strmid(szLine,0,nIdx+1)
}
//去掉字符串两边空格
macro TrimString(szLine)
{
szLine = TrimLeft(szLine)
szLIne = TrimRight(szLine)
return szLine
}
//对选择的代码添加 或取消#if 0 #endif 包围
macro AddOrDelMacroComment()
{
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) //被选择的代码块的最后一行的下一行内容
szCodeStart = GetBufLine(hbuf, LnFirst) //被选择的代码块的第一行内容
szCodeEnd = GetBufLine(hbuf, lnLast)//被选择的代码块的最后一行内容
start_space_count = 0 //第一行代码的前面的空白个数 只计算Tab个数,忽略空格
end_space_count = 0 //最后一行的代码的前面的空白个数
insert_space_count = 0 //我们要插入的#if 0 字符串前面应该插入多少个Tab
index = 0
while(index<strlen(szCodeStart))
{
if(AsciiFromChar(szCodeStart[index])== 9) //9是Tab字符的ASCII
{
start_space_count = start_space_count +1
}
index = index + 1
}
index = 0
while(index<strlen(szCodeEnd))
{
if(AsciiFromChar(szCodeEnd[index])== 9)
{
end_space_count=end_space_count+1
}
index = index + 1
}
//代码块的第一行和最后一行前面的Tab个数,取比较小的那个值
if(start_space_count<end_space_count)
{
insert_space_count = start_space_count -1
}
else
{
insert_space_count = end_space_count -1
}
str_start_insert=""
str_end_insert=""
index=0
while(index<insert_space_count)
{
str_start_insert=str_start_insert#" " //这里添加的Tab字符
str_end_insert=str_end_insert#" " //这里添加的也是Tab字符
index = index + 1
}
str_start_insert=str_start_insert#"#if 0" //在#if 0 开始符号和结束符号前都添加Tab字符,比代码行前面的空白少一个
str_end_insert=str_end_insert#"#endif"
if (TrimString(szIfStart) == "#if 0" && TrimString(szIfEnd) =="#endif") {
DelBufLine(hbuf, lnLast+1)
DelBufLine(hbuf, lnFirst-1)
sel.lnFirst = sel.lnFirst - 1
sel.lnLast = sel.lnLast - 1
}
else
{
InsBufLine(hbuf, lnFirst, str_start_insert)
InsBufLine(hbuf, lnLast+2, str_end_insert)
sel.lnFirst = sel.lnFirst + 1
sel.lnLast = sel.lnLast + 1
}
SetWndSel( hwnd, sel )
}
/* 获取当前时间 */
macro GetCurrentTime()
{
time = GetSysTime(1);
/* year */
str = time.year;
/* month */
if(time.month < 10)
str = cat("@str@-0",time.month);
else
str = cat("@str@-",time.month);
/* day */
if(time.day < 10)
str = cat("@str@-0",time.day);
else
str = cat("@str@-",time.day);
return str;
}
/* 添加函数注释,选中函数名使用此宏自动添加函数名 */
macro InsertFuncComment()
{
var comments
var hBuff
var line
var fun
var author
author = " "
hBuff = GetCurrentBuf()
fun = GetBufSelText(hBuff)
line = GetBufLnCur(hBuff)
InsBufLine(hBuff, line++, "/*******************************************************************************")
comments = " * Function Name : "
comments = cat(comments,fun)
InsBufLine(hBuff, line++, comments)
comments = " * Description : "
InsBufLine(hBuff, line++, comments)
comments = " * Input : "
InsBufLine(hBuff, line++, comments)
comments = " * Output : "
InsBufLine(hBuff, line++, comments)
comments = " * Return : "
InsBufLine(hBuff, line++, comments)
comments = " * Author : "
comments = cat(comments, author)
InsBufLine(hBuff, line++, comments)
comments = " * Date : "
comments = cat(comments, GetCurrentTime());
InsBufLine(hBuff, line++, comments)
InsBufLine(hBuff, line++, "*******************************************************************************/")
SaveBuf(hBuff)
}
/* 添加文件头和文件结束尾 */
macro InsertFileComment()
{
var comments
var hBuff
var line
var line_count
var mender
var file_name
var file_name_len
var i
mender = "Modified by Zhang Ruisong on "
hBuff = GetCurrentBuf()
line = GetBufLnCur(hBuff)
file_name = GetBufName(hBuff)
file_name_len = strlen(file_name)
if(0 == file_name_len)
{
file_name = nil
}
else
{
i = file_name_len
while("\\" != file_name[--i])
{
}
file_name = strmid(file_name,i+1,file_name_len)
}
str_year = GetSysTime(1)
str_year = str_year.year
InsBufLine(hBuff, line++, "/******************** (C) COPYRIGHT @str_year@ Company *******************************")
comments = "* File Name : "
comments = cat(comments, file_name)
InsBufLine(hBuff, line++, comments)
comments = "* Author : "
InsBufLine(hBuff, line++, comments)
comments = "* Version : "
InsBufLine(hBuff, line++, comments)
comments = "* Date : "
comments = cat(comments, GetCurrentTime());
InsBufLine(hBuff, line++, comments)
comments = "* Description : "
InsBufLine(hBuff, line++, comments)
comments = "* Function list : "
InsBufLine(hBuff, line++, comments)
comments = "* History : "
InsBufLine(hBuff, line++, comments)
InsBufLine(hBuff, line++, "*******************************************************************************/")
SaveBuf(hBuff)
line_count = GetBufLineCount(hBuff)
InsBufLine(hBuff, line_count, "/* End Of File */");
SaveBuf(hBuff)
}
macro InsertSpaceComment()
{
hbuf = GetCurrentBuf();
ln_count = GetBufLineCount(hbuf);
ln = 0;
while (ln< ln_count)
{
str_line = GetBufLine(hbuf,ln); /* 获取当前行 */
str_modify = 0;
str = str_line;
i = 0;
str_len = strlen(str);
while (nil != str[i])
{
offset = 1;
/* switch check */
if (i + 6 < str_len)
{
offset = 7;
if ("switch(" == strmid(str,i,i + offset))
{
str_len = strlen(str);
str_head = strmid(str, 0, i + offset - 1);
str_tail = strmid(str, i + offset - 1, str_len);
str_head = cat(str_head, " ");
i = strlen(str_head);
str = cat(str_head, str_tail);
str_modify = 1;
continue;
}
}
/* while check */
if (i + 5 < str_len)
{
offset = 6;
if ("while(" == strmid(str,i,i + offset))
{
str_len = strlen(str);
str_head = strmid(str, 0, i + offset - 1);
str_tail = strmid(str, i + offset - 1, str_len);
str_head = cat(str_head, " ");
i = strlen(str_head);
str = cat(str_head, str_tail);
str_modify = 1;
continue;
}
}
/* for check */
if (i + 3 < str_len)
{
offset = 4;
if ("for(" == strmid(str,i,i + offset))
{
str_len = strlen(str);
str_head = strmid(str, 0, i + offset - 1);
str_tail = strmid(str, i + offset - 1, str_len);
str_head = cat(str_head, " ");
i = strlen(str_head);
str = cat(str_head, str_tail);
str_modify = 1;
continue;
}
}
/* if check */
if (i + 2 < str_len)
{
offset = 3;
if("if(" == strmid(str,i,i + offset))
{
str_len = strlen(str);
str_head = strmid(str, 0, i + offset - 1);
str_tail = strmid(str, i + offset - 1, str_len);
str_head = cat(str_head, " ");
i = strlen(str_head);
str = cat(str_head, str_tail);
str_modify = 1;
continue;
}
}
/* other check*/
if (i + 1 < str_len)
{
offset = 2;
/* , ; check */
if (("," == str[i] || ";" == str[i]) && str[i+1] != " ")
{
/* 换行 */
if (";" == str[i] && str[i+1] == "\n")
{
}
/* 非换行 */
else
{
str_len = strlen(str);
str_head = strmid(str, 0, i + offset - 1);
str_tail = strmid(str, i + offset - 1, str_len);
str_head = cat(str_head, " ");
i = strlen(str_head);
str = cat(str_head, str_tail);
str_modify = 1;
continue;
}
}
/* 单目运算符 check */
// if (("&" == str[i] && str[i+1] != " " && str[i+1] != "&" && str[i+1] != "=")
// ||("|" == str[i] && str[i+1] != " " && str[i+1] != "|" && str[i+1] != "=")
// {
// str_len = strlen(str);
// str_head = strmid(str, 0, i + offset - 1);
// str_tail = strmid(str, i + offset - 1, str_len);
//
// str_head = cat(str_head, " ");
//
// i = strlen(str_head);
// str = cat(str_head, str_tail);
//
// str_modify = 1;
// continue;
// }
}
/* 向前检查 */
// if (i > 0)
// {
// offset = 0;
// /* 单目运算符 check */
// if (("&" == str[i] && str[i-1] != " ")
// ||("|" == str[i] && str[i-1] != " ")
// {
// str_len = strlen(str);
// str_head = strmid(str, 0, i + offset - 1);
// str_tail = strmid(str, i + offset - 1, str_len);
//
// str_head = cat(str_head, " ");
//
// i = strlen(str_head);
// str = cat(str_head, str_tail);
//
// str_modify = 1;
// continue;
// }
// }
i++;
}
if (1 == str_modify)
{
PutBufLine(hbuf, ln, str);
}
ln++;
}
}