Source Insight 美中不足的地方就是缺少像 注释,打开文件所在的目录等功能.然而我们可以通过增加宏来实现这些功能.
而宏的定义在 C:\Users\(用户名)\Documents\Source Insight\Projects\Base下的Base.PR工程中,用Source Insight本身打开这个工程.一般会有utils.em宏文件,打开此文件.(里面的相关宏函数以macro开头.语法类似于C语言,可以从Help中查看其含义.)
加入以下宏定义(来自网络有修改)并保存.
macro open_corresponding_cpp_c_or_h_file()
{
hcurrentbuf = getcurrentbuf ()
bname = getbufname (hcurrentbuf)
len = strlen (bname)
if (".cpp" == tolower( strmid (bname, len-4, len) ) )
{
filename = strmid (bname, 0, len-4)#".h"
}
else if (".c" ==tolower( strmid(bname,len-2,len)) )
{
filename = strmid (bname, 0, len-2)#".h"
}
else if (".h" ==tolower( strmid (bname, len-2, len)) )
{
cpp_name= strmid (bname, 0, len-2)#".cpp"
c_name=strmid(bname,0,len-2)#".c"
hit_cpp= openbuf (cpp_name)
hit_c = openbuf(c_name)
if(hit_cpp !=hnil)
{
setcurrentbuf(hit_cpp)
}
else if( hit_c !=hnil)
{
setcurrentbuf(hit_c)
}
stop
}
else
{
filename = nil
}
if (filename != nil)
{
hbufnext = openbuf (filename)
if (hbufnext != hnil)
{
setcurrentbuf (hbufnext)
}
}
}
//在sourceinsight的宏语言中,@str@ 可以连接两个字符串
/**
* 使用资源管理器打开当前文件所在文件夹,并个高亮选中当前文件 推荐快捷键 ctrl+T
*/
macro open_in_exporer_and_select()
{
buf = GetCurrentBuf();
curFilePath = GetBufName(buf);
cmdLine = "explorer.exe /select,@curFilePath@"; // 其实就是通过explorer.exe 命令行直接打开 eg: explorer.exe /select,C:\xx.log 会自动选中xx.log
StartMsg("Wait locate file in explorer...")
RunCmdLine(cmdLine, Nil, 0);
EndMsg()
}
// 推荐快捷键 ctrl+/
//将选择的行添加双斜杠注释或取消双斜杠注释
macro comment_multiline_double_slash()//添加//注释
{
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
}
PutBufLine(hbuf, Ln, Cat("//", buf))
Ln = Ln + 1
}
SetWndSel(hwnd, selection)
}
macro comment_multiline_del__double_slash()//删除// 注释
{
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
}
i=0;
while(i<len-1)//not support for statement
{
if( buf[i] =="/" )
{
if(buf[i+1]=="/")
{
buf[i] =" "
buf[i+1]=" "
PutBufLine(hbuf, Ln,Cat(StrMid(buf,0,i), StrMid(buf,i+2, Strlen(buf))))
break;
}
}
else
if(buf[i] != " " )
{
break;
}
i=i+1
}
Ln = Ln + 1
}
SetWndSel(hwnd, selection)
}
// 推荐快捷键 ctrl+3
//对选择的代码添加 或取消#if 0 #endif 包围
macro comment_multiline_sharp_if_else()
{
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 )
}
// 推荐快捷键 ctrl+8
//将选中的代码块添加多行注释
macro comment_selection()
{
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
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
}
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#" "
index = index + 1
}
str_start_insert = str_start_insert#"/*" //在注释开始符号和结束符号前都添加Tab字符,比代码行前面的空白少一个
str_end_insert = str_end_insert#"*/"
if (TrimString(szIfStart) == "/*" && TrimString(szIfEnd) == "*/")
{
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 )
}
//去掉左边空格,Tab等
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
}
点击菜单Options->Key Assignments... 在Command上输入macro即可看到上面相关的宏名.选择相应的宏,然后点击Assign New Key输入新的组合键如下
Macro: comment_multiline_del__double_slash : Ctrl+Alt+/ ( 清除一列 //注释)
Macro: comment_multiline_double_slash : Ctrl+/ (增加一列//注释)
Macro: comment_multiline_sharp_if_else : Ctrl+Alt+Shift+/ (#if 0 #endif 注释)
Macro: comment_selection : Alt+/ (/*注释*/)
Macro: open_corresponding_cpp_c_or_h_file : Ctrl+Alt+C (切换同名.h和.c/.cpp文件)
Macro: open_in_exporer_and_select : Ctrl+Alt+D (打开资料管理器并定位以文件)
其中关于//注释原来的宏是同一组快捷键,增加与清除是按两次,但是这样引起一个问题(如下所示),那就是原来的程序段中已经存在的注释(如程序说明)会被切换成没有注释
int main()
{
//这是个hellowrld
std::cout<<"hellworld"<<std::endl;
}
切换后
//int main()
//{
这是个hellowrld
// std::cout<<"hellworld"<<std::endl;
//}
所以我把它们分离开来.
同样我们也可以在这里修改原来的快捷键
File: Save All : Ctrl+Shift+A
Navigation: Select All : Ctrl+A
Search: Search Project... : Ctrl+Alt+Shift+F
Navigation: Go To Next Link : Alt+'
Navigation: Go To Previous Link : Alt+;
Navigation: Go To Previous Change : Ctrl+,
Navigation: Go To Next Change : Ctrl+.
Edit: Toggle Case : Ctrl+U
Navigation: Select Block : Alt+B
Symbol: Lookup References... : Ctrl+Alt+Shift+D
View: Highlight Word : Double M Click
Edit: Cut Word : Alt+C
设置背景色:在Options/Prefences.../Colors/Window Background 颜色土灰(180,180,180)
设置完成后可以通过Options/Save Configuration...保存这些设置到.CF3文件
配置附件下载https://pan.baidu.com/s/1XgGV3FCGMeBjknrnEiYGvQ