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
  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 /*快捷内容插入*/
180 macro _wcjHandleUfpType(key)
181 {
182     /*key = Ask("Enter ufp type short key");*/
183     ufptype = _wcjGetUfpType(key)
184     if (ufptype == "")
185         return false;
186     
187     /* 获得指定行文本 */
188     hbuf = GetCurrentBuf()
189     ln = GetBufLnCur(hbuf)
190     text = GetBufLine(hbuf, ln)
191 
192     /* 获得光标位置 */
193     hwnd = GetCurrentWnd()
194     sel     = GetWndSel(hwnd)
195     column = sel.ichFirst
196     
197     /* 为当前位置插入正确内容 */
198     DelBufLine(hbuf, ln)
199     before = strtrunc(text, column)
200     after  = strmid(text, column, strlen(text))
201     newtext = "@before@@ufptype@@after@"    
202     InsBufLine(hbuf, ln, newtext)
203 
204     /* 设置光标正确的位置 */
205     pos = column + strlen(ufptype)
206     SetBufIns(hbuf, ln, pos)
207 
208     return true;
209 }
210 
211 /**************************************macro related***********************************************/
212 /*插入ifdef*/
213 macro _wcjIfdefSz()
214 {
215     data = Ask("Enter ifdef condition:")
216     if (data == "")
217         return true
218         
219     hwnd = GetCurrentWnd()
220     lnFirst = GetWndSelLnFirst(hwnd)
221     lnLast = GetWndSelLnLast(hwnd)
222      
223     hbuf = GetCurrentBuf()
224     InsBufLine(hbuf, lnFirst, "#ifdef @data@")
225     InsBufLine(hbuf, lnLast+2, "#endif /* @data@ */")
226 
227     return true
228 }
229 
230 /*插入if*/
231 macro _wcjIfSz(data)
232 {
233     hwnd = GetCurrentWnd()
234     lnFirst = GetWndSelLnFirst(hwnd)
235     lnLast = GetWndSelLnLast(hwnd)
236      
237     hbuf = GetCurrentBuf()
238     InsBufLine(hbuf, lnFirst, "#if @data@")
239     InsBufLine(hbuf, lnLast+2, "#endif")
240 
241     return true
242 }
243 
244 /**************************************windows related***********************************************/
245 macro _wcjCloseWindows()
246 {
247     cwnd = WndListCount()  
248     iwnd = 0  
249     while (1)  
250     {  
251         hwnd = WndListItem(0)  
252         hbuf = GetWndBuf(hwnd)  
253         SaveBuf(hbuf)  
254         CloseWnd(hwnd)  
255         iwnd = iwnd + 1  
256         if(iwnd >= cwnd)  
257             break  
258     }  
259 
260     return true;
261 }
262 
263 /**************************************other related***********************************************/
264 macro _wcjAddInclude()
265 {
266     hbuf = GetCurrentBuf()
267     ln = GetBufLnCur(hbuf)
268 
269     /* 获得光标位置 */
270     hwnd = GetCurrentWnd()
271     sel     = GetWndSel(hwnd)
272     column = sel.ichFirst
273 
274     /*找到正确的符号*/
275     //symbol = GetSymbolLocationFromLn(hbuf, ln)
276     symbol = GetSymbolFromCursor(hbuf, ln, column)
277     if (symbol.Symbol == "")
278     {
279         msg("check cursor, can't find symbol")
280         return true
281     }
282 
283     /*文件名抽取*/    
284     filename = _wcjFileName(symbol.file);
285     len = strlen(filename)
286     filetype = strmid(filename, len-2, len)
287     if (filetype == ".c")
288         filename = Ask("func imp in @filename@, enter include file name or cancel:")
289 
290     if (filename == "")
291         return true
292 
293     includetext = "#include \"@filename@\""
294     
295     /* 正确的插入位置 */
296     count = GetBufLineCount(hbuf)
297     ln = 0
298     text = ""
299     lasttext = "invalid"
300     while(ln < count)
301     {
302         if(ln != 0)
303             lasttext = text
304         text = GetBufLine(hbuf, ln)
305 
306         /*找到合适位置*/
307         if (text == "#ifdef _cplusplus")
308         {
309             /*保证保留一个空格*/
310             if (lasttext == "")
311                 ln--
312             else
313                 InsBufLine(hbuf, ln, "")
314                 
315             /* 插入 */
316             InsBufLine(hbuf, ln, includetext)
317 
318             return true                        
319         }
320 
321         /*已添加*/
322         if (text == includetext)
323         {
324             return true
325         }
326         
327         ln++
328     }
329     
330     msg("can't add include, do it by youself")
331     return true
332 }
333 
334 /**************************************罗列所有快捷键***********************************************/
335 macro _wcjHandleWindows(key)  
336 {  
337     if (key == "winclose" || key == "wc")    return _wcjCloseWindows()
338 
339     return false;
340 } 
341 macro _wcjHandleOther(key)
342 {
343     if (key == "addinc")        return _wcjAddInclude()
344 
345     return false
346 }
347 
348 macro _wcjHandleMacro(key)
349 {
350     if (key == "if0")        return _wcjIfSz(0)    
351     if (key == "ifdef")        return _wcjIfdefSz()
352 
353     return false
354 }
355 macro _wcjHandleComment(key)
356 {
357     if (key == "commentfile" || key == "cf")                            return _wcjCommentFile()
358     if (key == "commentbefore" || key == "commentbef" || key == "cb")    return _wcjCommentBefore()
359     if (key == "commentheader" || key == "ch")                            return _wcjCommentHeader()
360 
361     return false
362 }
363 
364 /*程序中用到的自定义快捷键*/
365 macro _wcjGetUfpType(key)
366 {
367     key = tolower(key);
368     
369     if (key == "i")                    return "UFP_INT32"
370     if (key == "ui" || key=="u")    return "UFP_UINT32"
371     if (key == "ull")                return "UFP_UINT64"
372     if (key == "uv")                return "UFP_UINTPTR"
373     if (key == "v")                    return "UFP_VOID"
374     if (key == "vp")                return "UFP_PHYS_ADDR"
375     if (key == "n")                    return "UFP_NULL_PTR"
376     
377     
378     return ""
379 }
380 
381 
382 /* 主入口 */
383 macro wcjMain()
384 {
385     key = Ask("Enter anthing you want:")
386     if (key == "")
387         return ""
388 
389     key = tolower(key);
390     
391     /*ufp type处理*/
392     if (_wcjHandleUfpType(key))            return ""
393     /*macro相关*/
394     if (_wcjHandleMacro(key))            return ""
395     /*new file*/
396     if (_wcjHandleNewFile(key))            return ""            
397     /*comment*/
398     if (_wcjHandleComment(key))            return ""
399     /*窗体相关*/
400     if (_wcjHandleWindows(key))            return ""
401     
402     return _wcjHandleOther(key)
403 }

 

你可能感兴趣的:(source insight用于C语言编程的工具脚本)