让Source Insight的头文件和实现文件快捷键打开的方式

source insight并没有提供这样的快捷键,有时候用着比较烦人。终于找到了方法:使用宏命令,并设置快捷键与之绑定。

 

1、打开source insight,Project -> Open Project,打开Base项目,注:Base项目是source insight默认已经创建的。

 

2、打开Utils.em文件,在该文件的末尾加入如下内容:

 

[cpp] view plaincopy
 
  1. /*  
  2.   cpp和hpp文件互换(当前只支持同一目录下的文件互换)  
  3. */    
  4. macro switch_cpp_hpp()    
  5. {    
  6.     hwnd = GetCurrentWnd()    
  7.     hCurOpenBuf = GetCurrentBuf()    
  8.     if (hCurOpenBuf == 0)    
  9.         stop     
  10.     curOpenFileName = GetBufName(hCurOpenBuf)    
  11.     curOpenFileNameLen = strlen(curOpenFileName)    
  12.     
  13.     // 文件类型临时缓冲区    
  14.     strFileExt = NewBuf("strFileExtBuf")    
  15.     ClearBuf(strFileExt)    
  16.         
  17.     // 头文件类型    
  18.     index_hpp_begin = 0                             // 头文件开始索引    
  19.     AppendBufLine(strFileExt, ".h")    
  20.     AppendBufLine(strFileExt, ".hpp")    
  21.     index_hpp_end = GetBufLineCount(strFileExt)         // 头文件结束索引    
  22.         
  23.     // 源文件类型    
  24.     index_cpp_begin = index_hpp_end             // 源文件开始索引    
  25.     AppendBufLine(strFileExt, ".c")    
  26.     AppendBufLine(strFileExt, ".cpp")    
  27.     AppendBufLine(strFileExt, ".cc")    
  28.     AppendBufLine(strFileExt, ".cx")    
  29.     AppendBufLine(strFileExt, ".cxx")    
  30.     index_cpp_end = GetBufLineCount(strFileExt)     // 源文件结束索引    
  31.     
  32.     isCppFile = 0                       // 0:未知 1:头文件 2:源文件,默认未知扩展名    
  33.     curOpenFileExt = ""                     // 当前打开文件的扩展名    
  34.     index = index_hpp_begin     
  35.     // 遍历头文件,判断是否当前打开文件是头文件类型    
  36.     while(index < index_cpp_end)     
  37.     {    
  38.         curExt = GetBufLine(strFileExt, index)     
  39.         curExtLen = strlen(curExt)     
  40.         curOpenFileExt = strmid(curOpenFileName, curOpenFileNameLen-curExtLen, curOpenFileNameLen)      // 当前打开文件的扩展名    
  41.             
  42.         // 调试    
  43.         // AppendBufLine(debugBuf, curExt)    
  44.         // AppendBufLine(debugBuf, curOpenFileExt)    
  45.     
  46.             
  47.         if(curOpenFileExt == curExt)            // 匹配成功    
  48.         {    
  49.             if (index < index_hpp_end)    
  50.                 isCppFile = 1           // 当前打开文件是头文件    
  51.             else    
  52.                 isCppFile = 2           // 源文件    
  53.             break     
  54.         }    
  55.         index = index + 1     
  56.     }// while(index < index_cpp_end)    
  57.         
  58.     // 调试    
  59.     // AppendBufLine(debugBuf, isCppFile)    
  60.     
  61.     index_replace_begin = index_hpp_begin     
  62.     index_replace_end = index_hpp_end     
  63.         
  64.     if (isCppFile == 1)                 // 当前打开文件是头文件               
  65.     {    
  66.         index_replace_begin = index_cpp_begin     
  67.         index_replace_end = index_cpp_end     
  68.     }    
  69.     else if(isCppFile == 2)                 // 当前打开文件是源文件    
  70.     {    
  71.         index_replace_begin = index_hpp_begin     
  72.         index_replace_end = index_hpp_end     
  73.         
  74.         // 调试    
  75.         // AppendBufLine(debugBuf, "cpp")    
  76.     }    
  77.     else                            // 未知类型    
  78.     {    
  79.         //CloseBuf(strFileExt)              // 关闭缓冲区    
  80.     
  81.         //stop     
  82.         
  83.         index_replace_begin = 9999                      
  84.         index_replace_end = index_replace_begin     // 下面循环不会执行    
  85.     }    
  86.         
  87.     index = index_replace_begin     
  88.     while(index < index_replace_end)    
  89.     {    
  90.         destExt = GetBufLine(strFileExt, index)     
  91.         destFileName = strmid(curOpenFileName, 0, curOpenFileNameLen-strlen(curOpenFileExt))    // 不包括扩展名,绝对路径    
  92.             
  93.         // 尝试当前目标扩展名是否能够打开    
  94.         destFilePath = cat(destFileName, destExt)   // 文件名(包括扩展名)    
  95.             
  96.         hCurOpenBuf = OpenBuf(destFilePath)    
  97.         if(hCurOpenBuf != 0)    
  98.         {    
  99.             SetCurrentBuf(hCurOpenBuf)    
  100.             break     
  101.         }    
  102.             
  103.         // 尝试进行目录替换,看能否打开文件(如何设计:支持多个目录)    
  104.         // ...(赞不支持)    
  105.     
  106.             
  107.         index = index + 1     
  108.     }    
  109.     CloseBuf(strFileExt)                            // 关闭缓冲区    
  110.     // 调试    
  111.     // AppendBufLine(debugBuf, "finished")    
  112.         
  113. }  

 

 

3、关闭Base项目,重新打开Source Insight。

 

4、打开Option -> Key Assignments。在Command栏选中“Macro: switch_cpp_hpp”;点击Assign New Key按钮设置快捷键(Ctrl+·),点击OK,就设定好了。

 

5、再打开一个项目,试试你刚刚设定的快捷键吧!!!

 

本文纯属转载,原文地址:http://blog.csdn.net/lizhongkan/article/details/7081412

posted @ 2013-11-07 10:21  王井玉  阅读(1004)  评论(0)    收藏  举报