source insight用于C语言编程的工具脚本

简单来说,source insight提供的功能功能还不够傻瓜,用起来还不够方便,所以写了此脚本,提高开发效率。

部分source insight提供的功能也包含了进来,主要是因为我不喜欢使用太多的快捷键。

将代码中wcjMain关联到alt+a快捷键,随后输入你想操作的指令:如cb代表在本行之前添加注释即可。

目前提供的指令有20种左右,后面根据需要在补充。

1. 这个source insight中用的脚本,将其另存为任意名称.em文件

2. base工程中add该脚本

3. 在option->key assignment中将wcjMain关联某个快捷键后即可使用

4. 该脚本功能包括:

  • 类型替换
  • 注释添加
  • 标准的C头文件及实现文件的标准格式添加
  • 部分宏操作
  • 自动添加include
  • 增加var支持
  •   1 macro _wcjFileName(fullname)
      2 {
      3     length = strlen(fullname) 
      4     if (length == 0)
      5         return ""
      6 
      7     index = length
      8     while ("\" !=  fullname[--index]);
      9 
     10     purename = ""
     11     while (index < length)
     12         purename = cat(purename, fullname[++index])
     13         
     14     return purename    
     15 }
     16 /* 获取当前的文件名*/
     17 macro _wcjCurrentFileName()
     18 {
     19     hbuf = GetCurrentBuf()    
     20     fullname = GetBufName(hbuf)  
     21 
     22     return _wcjFileName(fullname)
     23 }
     24 
     25 /*头文件定义名称*/
     26 macro wcjIncDefName()
     27 {
     28     filename = _wcjCurrentFileName();
     29     length = strlen(filename);
     30 
     31     defname = "__"
     32     index = 0;
     33     while (index < length)
     34     {
     35         if (filename[index] == ".")
     36             defname = cat(defname, "_")
     37         else
     38             defname = cat(defname, toupper(filename[index]))
     39 
     40         ++index
     41     }
     42 
     43     defname = cat(defname, "__")
     44 
     45     return defname
     46 }
     47 
     48 /*获取当前时间*/
     49 macro wcjGetTime()
     50 {
     51     var  year
     52     var  month
     53     var  day
     54     var  commTime
     55     var  sysTime
     56 
     57     sysTime = GetSysTime(1)
     58     year = sysTime.Year
     59     month = sysTime.month
     60     day = sysTime.day
     61     commTime = "@year@-@month@-@day@"
     62     return commTime
     63 }
     64 
     65 /**************************************new file related***********************************************/
     66 macro _wcjCommentFile()
     67 {
     68     szMyName = "wangchunjie w00361341"
     69     
     70     hbuf = GetCurrentBuf()
     71     ln = 0
     72     InsBufLine(hbuf, ln++, "/*-------------------------------------------------------------------------")
     73     InsBufLine(hbuf, ln++, cat("    File:     ", _wcjCurrentFileName())
     74     InsBufLine(hbuf, ln++, cat("    Author: ", szMyName))
     75     InsBufLine(hbuf, ln++, cat("    Date:     ", wcjGetTime())
     76     InsBufLine(hbuf, ln++, cat("    Desc:     ", ""))
     77     InsBufLine(hbuf, ln++, "-------------------------------------------------------------------------*/")
     78 
     79     /* 设置光标正确的位置 */
     80     SetBufIns(hbuf, ln, 0)
     81     
     82     return true
     83 }
     84 
     85 /*在当前行的前一行添加注释*/
     86 macro _wcjCommentBefore()
     87 {
     88     hbuf = GetCurrentBuf()
     89     ln      = GetBufLnCur(hbuf)
     90 
     91     comment = ""
     92 
     93     /*补足空格*/
     94     text = GetBufLine(hbuf, ln)
     95     index = 0
     96     while (true) 
     97     {
     98         c = text[index++]
     99         if (c != " " && c != "    ")
    100             break;
    101             
    102         comment = cat(comment, c)    
    103     }
    104     
    105     comment = cat(comment, "/**<  */")
    106         
    107     InsBufLine(hbuf, ln, comment)
    108 
    109     /* 设置光标正确的位置 */
    110     SetBufIns(hbuf, ln, strlen(comment) - 3)
    111     
    112     return true
    113 }
    114 
    115 macro _wcjCommentHeader()
    116 {
    117     hbuf = GetCurrentBuf()
    118 
    119     szFunc = GetCurSymbol()
    120     ln = GetSymbolLine(szFunc)
    121     SetBufIns(hbuf, ln, 0)
    122     
    123     return _wcjCommentBefore()
    124 
    125 }
    126 
    127 
    128 /**************************************new file related***********************************************/
    129 macro _wcjNewFile(bInc)
    130 {
    131     defname = wcjIncDefName()
    132 
    133     _wcjCommentFile()
    134     
    135     hbuf = GetCurrentBuf()
    136     ln      = GetBufLnCur(hbuf)
    137 
    138     if (bInc) 
    139     {
    140         InsBufLine(hbuf, ln++, "#ifndef @defname@")
    141         InsBufLine(hbuf, ln++, "#define @defname@")
    142         InsBufLine(hbuf, ln++, "")        
    143     }
    144 
    145     InsBufLine(hbuf, ln++, "#ifdef _cplusplus")
    146     InsBufLine(hbuf, ln++, "#if _cplusplus")
    147     InsBufLine(hbuf, ln++, "extern "C"{")
    148     InsBufLine(hbuf, ln++, "#endif")
    149     InsBufLine(hbuf, ln++, "#endif")    
    150 
    151     InsBufLine(hbuf, ln++, "")
    152     cursorln = ln
    153     InsBufLine(hbuf, ln++, "")
    154     InsBufLine(hbuf, ln++, "")
    155     
    156     InsBufLine(hbuf, ln++, "#ifdef _cplusplus")
    157     InsBufLine(hbuf, ln++, "#if _cplusplus")
    158     InsBufLine(hbuf, ln++, "}")
    159     InsBufLine(hbuf, ln++, "#endif")
    160     InsBufLine(hbuf, ln++, "#endif")    
    161 
    162     if (bInc)
    163         InsBufLine(hbuf, ln++, "#endif /* @defname@ */")
    164 
    165     /* 设置光标正确的位置 */
    166     SetBufIns(hbuf, cursorln, 4)    
    167 }
    168 macro _wcjHandleNewFile(key)
    169 {
    170     /*插入C标准的include文件内容*/
    171     if (key == "newinc")    return _wcjNewFile(true)
    172     /*插入C标准的C文件内容*/
    173     if (key == "newc")        return _wcjNewFile(false)
    174 
    175     return false
    176 }
    177 
    178 /**************************************ufp type related***********************************************/
    179 macro _wcjInsertCursorText(data)
    180 {
    181     /* 获得指定行文本 */
    182     hbuf = GetCurrentBuf()
    183     ln = GetBufLnCur(hbuf)
    184     text = GetBufLine(hbuf, ln)
    185 
    186     /* 获得光标位置 */
    187     hwnd = GetCurrentWnd()
    188     sel     = GetWndSel(hwnd)
    189     column = sel.ichFirst
    190     
    191     /* 为当前位置插入正确内容 */
    192     DelBufLine(hbuf, ln)
    193     before = strtrunc(text, column)
    194     after  = strmid(text, column, strlen(text))
    195     newtext = "@before@@data@@after@"    
    196     InsBufLine(hbuf, ln, newtext)
    197 
    198     /* 设置光标正确的位置 */
    199     pos = column + strlen(data)
    200     SetBufIns(hbuf, ln, pos)    
    201 }
    202 /*快捷内容插入*/
    203 macro _wcjHandleUfpType(key)
    204 {
    205     /*key = Ask("Enter ufp type short key");*/
    206     ufptype = _wcjGetUfpType(key)
    207     if (ufptype == "")
    208         return false;
    209     
    210     _wcjInsertCursorText(ufptype);
    211 
    212     return true;
    213 }
    214 
    215 /**************************************macro related***********************************************/
    216 /*插入ifdef*/
    217 macro _wcjIfdefSz()
    218 {
    219     data = Ask("Enter ifdef condition:")
    220     if (data == "")
    221         return true
    222         
    223     hwnd = GetCurrentWnd()
    224     lnFirst = GetWndSelLnFirst(hwnd)
    225     lnLast = GetWndSelLnLast(hwnd)
    226      
    227     hbuf = GetCurrentBuf()
    228     InsBufLine(hbuf, lnFirst, "#ifdef @data@")
    229     InsBufLine(hbuf, lnLast+2, "#endif /* @data@ */")
    230 
    231     return true
    232 }
    233 
    234 /*插入if*/
    235 macro _wcjIfSz(data)
    236 {
    237     hwnd = GetCurrentWnd()
    238     lnFirst = GetWndSelLnFirst(hwnd)
    239     lnLast = GetWndSelLnLast(hwnd)
    240      
    241     hbuf = GetCurrentBuf()
    242     InsBufLine(hbuf, lnFirst, "#if @data@")
    243     InsBufLine(hbuf, lnLast+2, "#endif")
    244 
    245     return true
    246 }
    247 
    248 /**************************************windows related***********************************************/
    249 macro _wcjCloseWindows()
    250 {
    251     cwnd = WndListCount()  
    252     iwnd = 0  
    253     while (1)  
    254     {  
    255         hwnd = WndListItem(0)  
    256         hbuf = GetWndBuf(hwnd)  
    257         SaveBuf(hbuf)  
    258         CloseWnd(hwnd)  
    259         iwnd = iwnd + 1  
    260         if(iwnd >= cwnd)  
    261             break  
    262     }  
    263 
    264     return true;
    265 }
    266 
    267 /**************************************other related***********************************************/
    268 macro _wcjAddInclude()
    269 {
    270     hbuf = GetCurrentBuf()
    271     ln = GetBufLnCur(hbuf)
    272 
    273     /* 获得光标位置 */
    274     hwnd = GetCurrentWnd()
    275     sel     = GetWndSel(hwnd)
    276     column = sel.ichFirst
    277 
    278     /*找到正确的符号*/
    279     //symbol = GetSymbolLocationFromLn(hbuf, ln)
    280     symbol = GetSymbolFromCursor(hbuf, ln, column)
    281     if (symbol.Symbol == "")
    282     {
    283         msg("check cursor, can't find symbol")
    284         return true
    285     }
    286 
    287     /*文件名抽取*/    
    288     filename = _wcjFileName(symbol.file);
    289     len = strlen(filename)
    290     filetype = strmid(filename, len-2, len)
    291     if (filetype == ".c")
    292         filename = Ask("func imp in @filename@, enter include file name or cancel:")
    293 
    294     if (filename == "")
    295         return true
    296 
    297     includetext = "#include "@filename@""
    298     
    299     /* 正确的插入位置 */
    300     count = GetBufLineCount(hbuf)
    301     ln = 0
    302     text = ""
    303     lasttext = "invalid"
    304     while(ln < count)
    305     {
    306         if(ln != 0)
    307             lasttext = text
    308         text = GetBufLine(hbuf, ln)
    309 
    310         /*找到合适位置*/
    311         if (text == "#ifdef _cplusplus")
    312         {
    313             /*保证保留一个空格*/
    314             if (lasttext == "")
    315                 ln--
    316             else
    317                 InsBufLine(hbuf, ln, "")
    318                 
    319             /* 插入 */
    320             InsBufLine(hbuf, ln, includetext)
    321 
    322             return true                        
    323         }
    324 
    325         /*已添加*/
    326         if (text == includetext)
    327         {
    328             return true
    329         }
    330         
    331         ln++
    332     }
    333     
    334     msg("can't add include, do it by youself")
    335     return true
    336 }
    337 
    338 macro _wcjHandleVar()
    339 {
    340     key = Ask("Enter variable name:")
    341     if (key == "")
    342         return true
    343      
    344     hbuf = GetCurrentBuf()
    345 
    346     text = ""
    347     if (strtrunc(key, 1) == "i")                text = "UFP_INT32 @key@"
    348     if (strtrunc(key, 2) == "ui")                text = "UFP_UINT32 @key@"
    349     if (strtrunc(key, 3) == "ull")                text = "UFP_UINT64 @key@"
    350     if (strtrunc(key, 2) == "uv")                text = "UFP_UINTPTR @key@"
    351     if (strtrunc(key, 1) == "v")                text = "UFP_VOID @key@"
    352     if (strtrunc(key, 2) == "vp")                text = "UFP_PHYS_ADDR @key@"
    353     if (strtrunc(key, 1) == "n")                text = "UFP_NULL_PTR @key@"
    354     
    355     _wcjInsertCursorText(text)
    356 
    357     return true    
    358 }
    359 
    360 /**************************************罗列所有快捷键***********************************************/
    361 macro _wcjHandleWindows(key)  
    362 {  
    363     if (key == "winclose" || key == "wc")    return _wcjCloseWindows()
    364 
    365     return false;
    366 } 
    367 macro _wcjHandleOther(key)
    368 {
    369     if (key == "addinc")        return _wcjAddInclude()
    370 
    371     return false
    372 }
    373 
    374 macro _wcjHandleMacro(key)
    375 {
    376     if (key == "if0")        return _wcjIfSz(0)    
    377     if (key == "ifdef")        return _wcjIfdefSz()
    378 
    379     return false
    380 }
    381 macro _wcjHandleComment(key)
    382 {
    383     if (key == "commentfile" || key == "cf")                            return _wcjCommentFile()
    384     if (key == "commentbefore" || key == "commentbef" || key == "cb")    return _wcjCommentBefore()
    385     if (key == "commentheader" || key == "ch")                            return _wcjCommentHeader()
    386 
    387     return false
    388 }
    389 
    390 /*程序中用到的自定义快捷键*/
    391 macro _wcjGetUfpType(key)
    392 {
    393     key = tolower(key);
    394     
    395     if (key == "i")                    return "UFP_INT32"
    396     if (key == "ui" || key=="u")    return "UFP_UINT32"
    397     if (key == "ull")                return "UFP_UINT64"
    398     if (key == "uv")                return "UFP_UINTPTR"
    399     if (key == "v")                    return "UFP_VOID"
    400     if (key == "vp")                return "UFP_PHYS_ADDR"
    401     if (key == "n")                    return "UFP_NULL_PTR"
    402     
    403     
    404     return ""
    405 }
    406 
    407 
    408 /* 主入口 */
    409 macro wcjMain()
    410 {
    411     key = Ask("Enter anthing you want:")
    412     if (key == "")
    413         return ""
    414 
    415     key = tolower(key);
    416     
    417     /*ufp type处理*/
    418     if (_wcjHandleUfpType(key))            return ""
    419     /*macro相关*/
    420     if (_wcjHandleMacro(key))            return ""
    421     /*new file*/
    422     if (_wcjHandleNewFile(key))            return ""            
    423     /*comment*/
    424     if (_wcjHandleComment(key))            return ""
    425     /*窗体相关*/
    426     if (_wcjHandleWindows(key))            return ""
    427     /*变量*/
    428     if (key == "var")                    return _wcjHandleVar()
    429     
    430     return _wcjHandleOther(key)
    431 }
原文地址:https://www.cnblogs.com/desmondwang/p/5204106.html