耕耘者 QQ:460189859
一、说明
本文档介绍了sourceinsight 常用的一些宏,通过这些宏我们可以设置我们自己喜欢的一些操作以提高编程效率。以下这些宏作者已经验证过,都能在sourceinsight 中实现,只是有一个还有点BUG,就是添加头注释那个宏,用是能用当效果不是很好,读者可以自己试一试,同时也望指正(^_^)。一下的宏主要包括有单行注释(commentSigleLine)、块注释(commentBlock)、消除块注释(UncommentBlock)、删除行(killLine)、跳转到标示符(JumpAnywhere)、IfdefRewin、insertComment、、、、、、。同时还有四个宏解决了在删除中文字符时只删除一半的情况SuperBackspace、SuperDelete、SuperCursorLeft、SuperCursorRight。具体说明可以参考每个宏的注释说明!!!
二、使用方法
1.打开Base工程(该工程一般在"我的文档\SourceInsight\Projects\Base"中);
2.加入宏文件(也可以把代码加到已有文件中,如Utils.em文件中);
3.Options->MenuAssignments打开MenuAssignments窗口, 点击run让添加的宏生效,之后
关闭SI再打开就可
以在Options->KeyAssignments中找到我们刚才添加的宏了
4.对于使用的快捷键可以根据自己的操作习惯和爱好来设置^_^
三、快捷键设置(个人偏好)
Projectwindow Alt+R
Contextwindow Alt+C
Symbolwindow Alt+S
Splitwindow Alt+1
commentBlock Alt+Q
uncommentBlock Alt+W
inserComment F2
jumpAnywhere Alt+J
killLine Alt+D
commentSigleLine Alt+L
/****************************************************************************
INSERT HEADER
Insertsacommentheaderblockatthetopofthecurrentfunction.
Thisactuallyworksonanytypeofsymbol,notjust functions.
Tousethis,defineanenvironmentvariable"MYNAME"andsetit
toyouremailname. eg.setMYNAME=raygr
*****************************************************************************/
macroInsertHeader()
{
//Gettheowner'snamefromtheenvironmentvariable:MYNAME.
//Ifthevariabledoesn'texist,thentheownerfieldis skipped.
szMyName=getenv(MYNAME)
//Getahandleto thecurrentfilebufferandthename
//andlocation ofthecurrentsymbolwherethecursoris.
hbuf=GetCurrentBuf()
szFunc=GetCurSymbol()
ln =GetSymbolLine(szFunc)
//beginassemblingthetitlestring
sz="/* "
/*convertsymbolnameto TEXT LIKE THIS*/
cch=strlen(szFunc)
ich =0
while(ich<cch)
{
ch=szFunc[ich]
if (ich>0)
if (isupper(ch))
sz=cat(sz," ")
else
sz=cat(sz,"")
sz=Cat(sz,toupper(ch))
ich =ich +1
}
sz=Cat(sz," */")
// InsBufLine(hbuf,ln, sz)
InsBufLine(hbuf,ln+1, "/*-------------------------------------------------------------------------")
/*if ownervariableexists,insert Owner:name*/
if (strlen(szMyName)>0)
{
InsBufLine(hbuf,ln+2, " Owner:@szMyName@")
InsBufLine(hbuf,ln+3, "")
ln =ln +4
}
else
ln =ln +2
InsBufLine(hbuf,ln, " ")//provideanindent already
InsBufLine(hbuf,ln+1, "-------------------------------------------------------------------------*/")
//puttheinsertion pointinside theheadercomment
SetBufIns(hbuf,ln, 4)
}
/*****************************************************************************
InsertFileHeader:
Insertsacommentheaderblockatthetopofthecurrentfunction.
Thisactuallyworksonanytypeofsymbol,notjust functions.
Tousethis,defineanenvironmentvariable"MYNAME"andsetit
toyouremailname. eg.setMYNAME=raygr
*****************************************************************************/
macroInsertFileHeader()
{
szMyName=getenv(MYNAME)
hbuf=GetCurrentBuf()
InsBufLine(hbuf,0,"/*-------------------------------------------------------------------------")
/*if ownervariableexists,insert Owner:name*/
InsBufLine(hbuf,1," ")
if (strlen(szMyName)>0)
{
sz=" Owner:@szMyName@"
InsBufLine(hbuf,2,"")
InsBufLine(hbuf,3,sz)
ln =4
}
else
ln =2
InsBufLine(hbuf,ln, "-------------------------------------------------------------------------*/")
}
//Inserts"ReturnsTrue..orFalse..."atthecurrentline
macroReturnTrueOrFalse()
{
hbuf=GetCurrentBuf()
ln =GetBufLineCur(hbuf)
InsBufLine(hbuf,ln, " ReturnsTrueif successfulorFalseif errors.")
}
/*****************************************************************************
功能:当按键盘上的Alt+C键时会在光标处插入#ifdefREVIEW.....#endif
使用:在sourceinsight 里的keyassignments对话框里定义宏功能KillLine的快捷键
对应键盘功能键Alt+C
*****************************************************************************/
macroIfdefReview()
{
IfdefSz("REVIEW");
}
/*****************************************************************************
功能:当按键盘上的Alt+X键时会在光标处插入#ifdefBOGUS.....#endif
使用:在sourceinsight 里的keyassignments对话框里定义宏功能KillLine的快捷键
对应键盘功能键Alt+X
*****************************************************************************/
macroIfdefBogus()
{
IfdefSz("BOGUS");
}
/*****************************************************************************
功能:当按键盘上的Alt+Z键时会在光标处插入#ifdefNEVER.....#endif
使用:在sourceinsight 里的keyassignments对话框里定义宏功能KillLine的快捷键
对应键盘功能键Alt+D
*****************************************************************************/
macroIfdefNever()
{
IfdefSz("NEVER");
}
/*****************************************************************************
功能:当按键盘上的Alt+2键时会在光标处插入#ifdef(等输入).....#endif
使用:在sourceinsight 里的keyassignments对话框里定义宏功能KillLine的快捷键
对应键盘功能键Alt+2
*****************************************************************************/
macroInsertIfdef()
{
sz=Ask("Enterifdef condition:")
if (sz!="")
IfdefSz(sz);
}
macroInsertCPlusPlus()
{
IfdefSz("__cplusplus");
}
//Wrapifdef <sz>..endifaroundthecurrentselection
macroIfdefSz(sz)
{
hwnd=GetCurrentWnd()
lnFirst =GetWndSelLnFirst(hwnd)
lnLast =GetWndSelLnLast(hwnd)
hbuf=GetCurrentBuf()
InsBufLine(hbuf,lnFirst, "#ifdef@sz@")
InsBufLine(hbuf,lnLast+2, "#endif/*@sz@*/")
}
/*****************************************************************************
功能:当按键盘上的Alt+D键时删除光标的当前行并且保存到缓冲区里(Deletethecurrent
line andappendsit to theclipboardbuffer)
使用:在sourceinsight 里的keyassignments对话框里定义宏功能KillLine的快捷键
对应键盘功能键Alt+D
*****************************************************************************/
macroKillLine()
{
hbufCur=GetCurrentBuf();
lnCur =GetBufLnCur(hbufCur)
hbufClip=GetBufHandle("Clipboard")
AppendBufLine(hbufClip,GetBufLine(hbufCur,lnCur))
DelBufLine(hbufCur,lnCur)
}
//Pastelines killedwithKillLine(clipboardis emptied)
macroPasteKillLine()
{
Paste
EmptyBuf(GetBufHandle("Clipboard"))
}
//deletealllines in thebuffer
macroEmptyBuf(hbuf)
{
lnMax =GetBufLineCount(hbuf)
while(lnMax>0)
{
DelBufLine(hbuf,0)
lnMax =lnMax - 1
}
}
/*****************************************************************************
功能:当按键盘上的Alt+J键时会弹出一个窗口可以输入要跳转到的申明和函数
使用:在sourceinsight 里的keyassignments对话框里定义宏功能KillLine的快捷键
对应键盘功能键Alt+J
*****************************************************************************/
macroJumpAnywhere()
{
symbol=Ask("Whatdeclarationwouldyoulike to see?")
JumpToSymbolDef(symbol)
}
//list allsiblingsofauserspecifiedsymbol
//Asiblingis anyothersymboldeclaredin thesamefile.
macroOutputSiblingSymbols()
{
symbol=Ask("Whatsymbolwouldyoulike to list siblingsfor?")
hbuf=ListAllSiblings(symbol)
SetCurrentBuf(hbuf)
}
//Givenasymbolname,openthefileits declaredin and
//createanewoutputbufferlisting allofthesymbolsdeclared
//in thatfile. Returnsthenewbufferhandle.
macroListAllSiblings(symbol)
{
loc =GetSymbolLocation(symbol)
if (loc=="")
{
msg("@symbol@notfound.")
stop
}
hbufOutput=NewBuf("Results")
hbuf=OpenBuf(loc.file)
if (hbuf==0)
{
msg("Can'topenfile.")
stop
}
isymMax =GetBufSymCount(hbuf)
isym =0;
while(isym<isymMax)
{
AppendBufLine(hbufOutput,GetBufSymName(hbuf,isym))
isym =isym +1
}
CloseBuf(hbuf)
returnhbufOutput
}
/****************************************************************************
功能:当按键盘上的BackSpace键时向前删除一个汉字或者一个英文字符(SI默认删除
汉字要分两次即一次只能删除一个字符,这样的话很容造成汉字乱码)
使用:在sourceinsight里的keyassignments对话框里定义宏功能SuperBackspace的快捷键
对应键盘功能键backspace
*****************************************************************************/
macroSuperBackspace()
{
hwnd=GetCurrentWnd();
hbuf=GetCurrentBuf();
if (hbuf==0)
stop; //emptybuffer
//getcurrentcursorpostion
ipos =GetWndSelIchFirst(hwnd);
//getcurrentline number
ln =GetBufLnCur(hbuf);
if ((GetBufSelText(hbuf)!="")|| (GetWndSelLnFirst(hwnd)!=GetWndSelLnLast(hwnd))){
//sth.wasselected,delselection
SetBufSelText(hbuf,""); // stupid&buggysourceinsight
//delthe" "
SuperBackspace(1);
stop;
}
//copycurrentline
text=GetBufLine(hbuf,ln);
//getstringlength
len =strlen(text);
//if thecursoris atthestartofline, combinewithprevline
if (ipos==0|| len ==0){
if (ln<=0)
stop; //top offile
ln =ln -1; //donotuse"ln--"forcompatibilitywitholderversions
prevline=GetBufLine(hbuf,ln);
prevlen=strlen(prevline);
//combinetwolines
text=cat(prevline,text);
//deltwolines
DelBufLine(hbuf,ln);
DelBufLine(hbuf,ln);
//insert thecombinedone
InsBufLine(hbuf,ln, text);
//setthecursorposition
SetBufIns(hbuf,ln, prevlen);
stop;
}
num=1;//delonechar
if (ipos>=1){
//processChinesecharacter
i =ipos;
count=0;
while(AsciiFromChar(text[i-1])>=160){
i =i -1;
count=count+1;
if (i==0)
break;
}
if (count>0){
//Ithink it mightbeatwo-bytecharacter
num=2;
//Thisidiot doesnotsupportmodandbitwiseoperators
if ((count/ 2*2!=count)&&(ipos<len))
ipos =ipos +1; // adjustcursorposition
}
}
//keepingsafe
if (ipos-num<0)
num=ipos;
//delchar(s)
text=cat(strmid(text,0,ipos -num),strmid(text,ipos, len));
DelBufLine(hbuf,ln);
InsBufLine(hbuf,ln, text);
SetBufIns(hbuf,ln, ipos -num);
stop;
}
/*****************************************************************************
功能:当按键盘上的Delete键时向前删除一个汉字或者一个英文字符(SI默认删除汉字
要分两次即一次只能删除一个字符,这样的话很容造成汉字乱码)
使用:在sourceinsight 里的keyassignments对话框里定义宏功能SuperDelete的快捷键
对应键盘功能键delete
******************************************************************************/
macroSuperDelete()
{
hwnd=GetCurrentWnd();
hbuf=GetCurrentBuf();
if (hbuf==0)
stop; //emptybuffer
//getcurrentcursorpostion
ipos =GetWndSelIchFirst(hwnd);
//getcurrentline number
ln =GetBufLnCur(hbuf);
if ((GetBufSelText(hbuf)!="")|| (GetWndSelLnFirst(hwnd)!=GetWndSelLnLast(hwnd))){
//sth.wasselected,delselection
SetBufSelText(hbuf,"");//stupid&buggysourceinsight
//delthe" "
SuperDelete(1);
stop;
}
//copycurrentline
text=GetBufLine(hbuf,ln);
//getstringlength
len =strlen(text);
if (ipos==len || len ==0)
{
totalLn=GetBufLineCount(hbuf);
lastText =GetBufLine(hBuf,totalLn-1);
lastLen =strlen(lastText);
if (ipos==lastLen)// endoffile
stop;
ln =ln +1; // donotuse"ln--"forcompatibilitywitholderversions
nextline=GetBufLine(hbuf,ln);
nextlen=strlen(nextline);
//combinetwolines
text=cat(text,nextline);
//deltwolines
DelBufLine(hbuf,ln-1);
DelBufLine(hbuf,ln-1);
//insert thecombinedone
InsBufLine(hbuf,ln-1, text);
//setthecursorposition
SetBufIns(hbuf,ln-1, len);
stop;
}
num=1;//delonechar
if (ipos>0)
{
//processChinesecharacter
i =ipos;
count=0;
while(AsciiFromChar(text[i-1])>=160)
{
i =i -1;
count=count+1;
if (i==0)
break;
}
if (count>0)
{
//Ithink it mightbeatwo-bytecharacter
num=2;
//Thisidiot doesnotsupportmodandbitwiseoperators
if (((count/ 2*2!=count)|| count==0)&&(ipos<len-1))
ipos =ipos +1; // adjustcursorposition
}
//keepingsafe
if (ipos-num<0)
num=ipos;
}
else
{
i =ipos;
count=0;
while(AsciiFromChar(text)>=160)
{
i =i +1;
count=count+1;
if(i ==len-1)
break;
}
if(count >0)
{
num=2;
}
}
text=cat(strmid(text,0,ipos), strmid(text,ipos+num, len));
DelBufLine(hbuf,ln);
InsBufLine(hbuf,ln, text);
SetBufIns(hbuf,ln, ipos);
stop;
}
/*****************************************************************************
功能:当按键盘上的->键时会向右移动一个汉字或者一个英文字符(SI默认删除汉字要分
两次即一次只能删除一个字符,这样的话很容造成汉字乱码)
使用:在sourceinsight里的keyassignments对话框里定义宏功能SuperCursorLeft的快捷键
对应键盘功能键shift+->
******************************************************************************/
macroIsComplexCharacter()
{
hwnd=GetCurrentWnd();
hbuf=GetCurrentBuf();
if (hbuf==0)
return0;
//当前位置
pos=GetWndSelIchFirst(hwnd);
//当前行数
ln =GetBufLnCur(hbuf);
//得到当前行
text=GetBufLine(hbuf,ln);
//得到当前行长度
len =strlen(text);
//从头计算汉字字符的个数
if(pos >0)
{
i=pos;
count=0;
while(AsciiFromChar(text[i-1])>=160)
{
i =i -1;
count=count+1;
if(i ==0)
break;
}
if((count/2)*2==count||count==0)
return0;
else
return1;
}
return0;
}
macromoveleft()
{
hwnd=GetCurrentWnd();
hbuf=GetCurrentBuf();
if (hbuf==0)
stop; //emptybuffer
ln =GetBufLnCur(hbuf);
ipos =GetWndSelIchFirst(hwnd);
if(GetBufSelText(hbuf) !=""|| (ipos==0&&ln ==0)) // 第0行或者是选中文字,则不
移动
{
SetBufIns(hbuf,ln, ipos);
stop;
}
if(ipos ==0)
{
preLine=GetBufLine(hbuf,ln-1);
SetBufIns(hBuf,ln-1, strlen(preLine)-1);
}
else
{
SetBufIns(hBuf,ln, ipos-1);
}
}
macroSuperCursorLeft()
{
moveleft();
if(IsComplexCharacter())
moveleft();
}
/*****************************************************************************
功能:当按键盘上的<-键时会向右移动一个汉字或者一个英文字符(SI默认删除汉字要分
两次即一次只能删除一个字符,这样的话很容造成汉字乱码)
使用:在sourceinsight里的keyassignments对话框里定义宏功能SuperCursorRight的快捷键
对应键盘功能键<-******************************************************************************/
macromoveRight()
{
hwnd=GetCurrentWnd();
hbuf=GetCurrentBuf();
if (hbuf==0)
stop; //emptybuffer
ln =GetBufLnCur(hbuf);
ipos =GetWndSelIchFirst(hwnd);
totalLn=GetBufLineCount(hbuf);
text=GetBufLine(hbuf,ln);
if(GetBufSelText(hbuf)!="") //选中文字
{
ipos =GetWndSelIchLim(hwnd);
ln =GetWndSelLnLast(hwnd);
SetBufIns(hbuf,ln, ipos);
stop;
}
if(ipos ==strlen(text)-1&&ln ==totalLn-1)// 末行
stop;
if(ipos ==strlen(text))
{
SetBufIns(hBuf,ln+1, 0);
}
else
{
SetBufIns(hBuf,ln, ipos+1);
}
}
macroSuperCursorRight()
{
moveRight();
if(IsComplexCharacter()) //definedin SuperCursorLeft.em
moveRight();
}
/*****************************************************************************
功能:当按键盘上的shift+->键时会选中所需要的文字
使用:在sourceinsight里的keyassignments对话框里定义宏功能SuperShiftCursorRight的快
捷键对应键盘功能键shift+->
******************************************************************************/
macroIsShiftRightComplexCharacter()
{
hwnd=GetCurrentWnd();
hbuf=GetCurrentBuf();
if (hbuf==0)
return0;
selRec=GetWndSel(hwnd);
pos=selRec.ichLim;
ln =selRec.lnLast;
text=GetBufLine(hbuf,ln);
len =strlen(text);
if(len ==0|| len <pos)
return1;
//Msg("@len@;@pos@;");
if(pos >0)
{
i=pos;
count=0;
while(AsciiFromChar(text[i-1])>=160)
{
i =i -1;
count=count+1;
if(i ==0)
break;
}
if((count/2)*2==count||count==0)
return0;
else
return1;
}
return0;
}
macroshiftMoveRight()
{
hwnd=GetCurrentWnd();
hbuf=GetCurrentBuf();
if (hbuf==0)
stop;
ln =GetBufLnCur(hbuf);
ipos =GetWndSelIchFirst(hwnd);
totalLn=GetBufLineCount(hbuf);
text=GetBufLine(hbuf,ln);
selRec=GetWndSel(hwnd);
curLen=GetBufLineLength(hbuf,selRec.lnLast);
if(selRec.ichLim ==curLen+1|| curLen==0)
{
if(selRec.lnLast ==totalLn-1)
stop;
selRec.lnLast=selRec.lnLast+1;
selRec.ichLim=1;
SetWndSel(hwnd,selRec);
if(IsShiftRightComplexCharacter())
shiftMoveRight();
stop;
}
selRec.ichLim=selRec.ichLim+1;
SetWndSel(hwnd,selRec);
}
macroSuperShiftCursorRight()
{
if(IsComplexCharacter())
SuperCursorRight();
shiftMoveRight();
if(IsShiftRightComplexCharacter())
shiftMoveRight();
}
/*****************************************************************************
功能:当按键盘上的shift+<-键时会选中所需要的文字
使用:在sourceinsight里的keyassignments对话框里定义宏功能SuperCursorLeft的快捷键
对应键盘功能键shift+<-
******************************************************************************/
macroIsShiftLeftComplexCharacter()
{
hwnd=GetCurrentWnd();
hbuf=GetCurrentBuf();
if (hbuf==0)
return0;
selRec=GetWndSel(hwnd);
pos=selRec.ichFirst;
ln =selRec.lnFirst;
text=GetBufLine(hbuf,ln);
len =strlen(text);
if(len ==0|| len <pos)
return1;
//Msg("@len@;@pos@;");
if(pos >0)
{
i=pos;
count=0;
while(AsciiFromChar(text[i-1])>=160)
{
i =i -1;
count=count+1;
if(i ==0)
break;
}
if((count/2)*2==count||count==0)
return0;
else
return1;
}
return0;
}
macroshiftMoveLeft()
{
hwnd=GetCurrentWnd();
hbuf=GetCurrentBuf();
if (hbuf==0)
stop;
ln =GetBufLnCur(hbuf);
ipos =GetWndSelIchFirst(hwnd);
totalLn=GetBufLineCount(hbuf);
text=GetBufLine(hbuf,ln);
selRec=GetWndSel(hwnd);
//curLen =GetBufLineLength(hbuf,selRec.lnFirst);
//Msg("@curLen@;@selRec@");
if(selRec.ichFirst ==0)
{
if(selRec.lnFirst ==0)
stop;
selRec.lnFirst=selRec.lnFirst-1;
selRec.ichFirst=GetBufLineLength(hbuf,selRec.lnFirst)-1;
SetWndSel(hwnd,selRec);
if(IsShiftLeftComplexCharacter())
shiftMoveLeft();
stop;
}
selRec.ichFirst=selRec.ichFirst-1;
SetWndSel(hwnd,selRec);
}
macroSuperShiftCursorLeft()
{
if(IsComplexCharacter())
SuperCursorLeft();
shiftMoveLeft();
if(IsShiftLeftComplexCharacter())
shiftMoveLeft();
}
/*****************************************************************************
功能:将光标所在行的代码用//
使用:在sourceinsight里的keyassignments对话框里定义宏功能CommentSingleLine的快捷
键对应键盘功能键f12
******************************************************************************/
macroCommentSingleLine()
{
hbuf=GetCurrentBuf()
ln =GetBufLnCur(hbuf)
str=GetBufLine(hbuf,ln)
str=cat("//",str)
// str=cat(str,"*/")
PutBufLine(hbuf,ln, str)
}
/*****************************************************************************
功能:将所有选中的行用//注释(Commenttheselectedblockoftext usingsingleline
commentsandindent it)
使用:在sourceinsight里的keyassignments对话框里定义宏功能CommentBlock的快捷键
对应键盘功能键
******************************************************************************/
macroCommentBlock()
{
hbuf=GetCurrentBuf();
hwnd=GetCurrentWnd();
sel=GetWndSel(hwnd);
iLine =sel.lnFirst;
while(iLine<=sel.lnLast)
{
szLine=GetBufLine(hbuf,iLine);
szLine=cat("// ",szLine);
PutBufLine(hbuf,iLine, szLine);
iLine =iLine +1;
}
if (sel.lnFirst==sel.lnLast)
{
tabSize =_tsGetTabSize()-1;
sel.ichFirst=sel.ichFirst+tabSize;
sel.ichLim=sel.ichLim+tabSize;
}
SetWndSel(hwnd,sel);
}
/*****************************************************************************
功能:接除所有选中的行用的注释(UndotheCommentBlockfortheselectedtext.)
使用:在sourceinsight里的keyassignments对话框里定义宏功能UnCommentBlock的快捷键
对应键盘功能键
******************************************************************************/
macroUnCommentBlock()
{
hbuf=GetCurrentBuf();
hwnd=GetCurrentWnd();
sel=GetWndSel(hwnd);
iLine =sel.lnFirst;
tabSize =0;
while(iLine<=sel.lnLast)
{
szLine=GetBufLine(hbuf,iLine);
len =strlen(szLine);
szNewLine="";
if (len>1)
{
if (szLine[0]=="/"&&szLine[1]=="/")
{
if (len>2)
{
if (AsciiFromChar(szLine[2])==9)
{
tabSize =_tsGetTabSize()-1;
szNewLine=strmid(szLine,3,strlen(szLine));
}
}
if (szNewLine=="")
{
szNewLine=strmid(szLine,2,strlen(szLine));
tabSize =2;
}
PutBufLine(hbuf,iLine, szNewLine);
}
}
iLine =iLine +1;
}
if (sel.lnFirst==sel.lnLast)
{
sel.ichFirst=sel.ichFirst-tabSize;
sel.ichLim=sel.ichLim-tabSize;
}
SetWndSel(hwnd,sel);
}
macro_tsGetTabSize()
{
szTabSize=GetReg("TabSize");
if (szTabSize!="")
{
tabSize =AsciiFromChar(szTabSize[0])-AsciiFromChar("0");
}
else
{
tabSize =4;
}
returntabSize;
}