功能:对整个源码文件进行格式化,使得对齐错落有致,功能类似于VC6.0/VS2005/VS2008的Alt+F8功能。
因为设计功底较差,脚本写的较乱!
/**
对文件进行排版,按格式对齐
作者:王丰亮
时间:2011-11-10
*/
macro files_formatting()
{
hwnd = GetCurrentWnd()
hbuf = GetCurrentBuf()
if(hbuf ==0)
stop
result = one_file_formatting(hbuf) // 返回为0,表示括号对应,否则文件结构错误
if(result != 0)
Msg("括号数不成对,请进行检查!")
SaveBuf(hbuf)
SetCurrentBuf(hbuf)
}
// 对当前文件进行格式化对齐处理
macro one_file_formatting(hbuf)
{
const_space = " "
const_brace1 = "{"
const_brace2 = "}"
const_comments_single = "//" // 单行注释符号
const_comments_multi_begin = "/*" // 多行注释符号-开始符
const_comments_multi_end = "*/" // 多行注释符号-结束符
const_padding_num = 4 // 每次缩进量(空格)
current_padding_num = 0 // 当前缩进量(空格)
isMultiComment = 0
isSingleComment = 0
cur_line_index = 0 ;
total_line_num = GetBufLineCount(hbuf)
Msg(total_line_num)
while(cur_line_index < total_line_num)
{
dest_text = ""
text = GetBufLine(hbuf, cur_line_index)
// 将开头及尾部的空格去掉
text_no_space = string_trim(text, const_space)
if(strlen(text_no_space) == 0) // 全是空格,则转换为空行
{
dest_text = ""
}
else if(isMultiComment == 1)
{
dest_text = text
}
else if(text_no_space == const_brace2) // "}"单独在一行时,缩进单独处理
{
pad_str = padding_string(current_padding_num-const_padding_num, const_space) ; // 填充字符串
dest_text = cat(pad_str, text_no_space) // 目标字符串
}
else
{
pad_str = padding_string(current_padding_num, const_space) ; // 填充字符串
dest_text = cat(pad_str, text_no_space) // 目标字符串
}
PutBufLine(hbuf, cur_line_index, dest_text) // 保存
// 解析是否包含'{',或'}',暂时不考虑注释
sub_index = 0
while(sub_index < strlen(text_no_space))
{
if(isMultiComment == 1) // 寻找结束符
{
if(sub_index < strlen(text_no_space)-1)
{
if(strmid(text_no_space, sub_index, sub_index+2) == const_comments_multi_end) // 以“*/”结束符
{
isSingleComment = 0
isMultiComment = 0
sub_index = sub_index + 1
}
sub_index = sub_index + 1
continue
}
break // 本行结束
}
// 忽略注释
if(sub_index < strlen(text_no_space)-1) // 至少2个字符才判断注释,否则越界
{
if (strmid(text_no_space, sub_index, sub_index+2) == const_comments_single) // 以“//”开头的注释
{
isSingleComment = 1
isMultiComment = 0
sub_index = sub_index + 2
continue
}
else if(strmid(text_no_space, sub_index, sub_index+2) == const_comments_multi_begin) // 以“/*”开头的注释
{
// 需要查找对应的注释结束符
isSingleComment = 0
isMultiComment = 1
sub_index = sub_index + 2
continue
}
}
// 匹配brace
if(strmid(text_no_space, sub_index, sub_index+1) == const_brace1) // '{'
{
current_padding_num = current_padding_num + const_padding_num
}
else if(strmid(text_no_space, sub_index, sub_index+1) == const_brace2) // '}'
{
current_padding_num = current_padding_num - const_padding_num
}
sub_index = sub_index + 1
}
cur_line_index = cur_line_index + 1
}
return current_padding_num
}
// 去掉开头及尾部特定字符(如:空格)
macro string_trim(s, ts)
{
trim_s = string_trim_left(s, ts)
return string_trim_right(trim_s, ts)
}
// 去掉开头特定字符(如:空格)
macro string_trim_left(s, ts)
{
strim_s = s
if(strlen(strim_s) == 0)
return strim_s
// 遍历开头的空格
sub_indexSpace = 0
while(sub_indexSpace < strlen(strim_s))
{
if(strmid(strim_s, sub_indexSpace, sub_indexSpace+1) != ts)
break
sub_indexSpace = sub_indexSpace + 1
}
return strmid(strim_s, sub_indexSpace, strlen(strim_s))
}
// 去掉尾部特定字符(如:空格)
macro string_trim_right(s, ts)
{
strim_s = s
if(strlen(strim_s) == 0)
return strim_s
// 遍历尾部的空格
sub_indexSpace = strlen(strim_s)
while(sub_indexSpace > 0)
{
if(strmid(strim_s, sub_indexSpace-1, sub_indexSpace) != ts)
break
sub_indexSpace = sub_indexSpace - 1
}
return strmid(strim_s, 0, sub_indexSpace)
}
// 填充current_padding_num数量的字符/字符串(ch)
macro padding_string(current_padding_num, ch)
{
s = ""
i = 0
while(i < current_padding_num)
{
s = cat(s, ch)
i = i + 1
}
return s
}