以下是Source Insight中文字符串支持的宏的实现,在此做个备份。
代码来自网上,非笔者所写。原有代码有个明显的Bug(Del的时候会导致多删除一个字符和多插入一个空格),已经被笔者fix掉。
使用时请将此部分代码贴到Source Insight的Base project的Utils.em文件末尾,并且在Options / Key Assignments添加相应的宏-键映射。
另外,在页面http://www.sourceinsight.com/public/macros/也有很多宏,可以参考使用。
1 /*====================================================================== 2 1、BackSpace后退键 3 ======================================================================*/ 4 macro SuperBackspace() 5 { 6 hwnd = GetCurrentWnd(); 7 hbuf = GetCurrentBuf(); 8 if (hbuf == 0) 9 stop; // empty buffer 10 // get current cursor postion 11 ipos = GetWndSelIchFirst(hwnd); 12 // get current line number 13 ln = GetBufLnCur(hbuf); 14 if ((GetBufSelText(hbuf) != "") || (GetWndSelLnFirst(hwnd) != GetWndSelLnLast(hwnd))) { 15 // sth. was selected, del selection 16 SetBufSelText(hbuf, " "); // stupid & buggy sourceinsight 17 // del the " " 18 SuperBackspace(1); 19 stop; 20 } 21 // copy current line 22 text = GetBufLine(hbuf, ln); 23 // get string length 24 len = strlen(text); 25 // if the cursor is at the start of line, combine with prev line 26 if (ipos == 0 || len == 0) { 27 if (ln <= 0) 28 stop; // top of file 29 ln = ln - 1; // do not use "ln--" for compatibility with older versions 30 prevline = GetBufLine(hbuf, ln); 31 prevlen = strlen(prevline); 32 // combine two lines 33 text = cat(prevline, text); 34 // del two lines 35 DelBufLine(hbuf, ln); 36 DelBufLine(hbuf, ln); 37 // insert the combined one 38 InsBufLine(hbuf, ln, text); 39 // set the cursor position 40 SetBufIns(hbuf, ln, prevlen); 41 stop; 42 } 43 num = 1; // del one char 44 if (ipos >= 1) { 45 // process Chinese character 46 i = ipos; 47 count = 0; 48 while (AsciiFromChar(text[i - 1]) >= 160) { 49 i = i - 1; 50 count = count + 1; 51 if (i == 0) 52 break; 53 } 54 if (count > 0) { 55 // I think it might be a two-byte character 56 num = 2; 57 // This idiot does not support mod and bitwise operators 58 if ((count / 2 * 2 != count) && (ipos < len)) 59 ipos = ipos + 1; // adjust cursor position 60 } 61 } 62 // keeping safe 63 if (ipos - num < 0) 64 num = ipos; 65 // del char(s) 66 text = cat(strmid(text, 0, ipos - num), strmid(text, ipos, len)); 67 DelBufLine(hbuf, ln); 68 InsBufLine(hbuf, ln, text); 69 SetBufIns(hbuf, ln, ipos - num); 70 stop; 71 } 72 /*====================================================================== 73 2、删除键——SuperDelete.em 74 ======================================================================*/ 75 macro SuperDelete() 76 { 77 hwnd = GetCurrentWnd(); 78 hbuf = GetCurrentBuf(); 79 if (hbuf == 0) 80 stop; // empty buffer 81 // get current cursor postion 82 ipos = GetWndSelIchFirst(hwnd); 83 // get current line number 84 ln = GetBufLnCur(hbuf); 85 if ((GetBufSelText(hbuf) != "") || (GetWndSelLnFirst(hwnd) != GetWndSelLnLast(hwnd))) { 86 // sth. was selected, del selection 87 SetBufSelText(hbuf, " "); // stupid & buggy sourceinsight 88 // del the " " 89 SuperBackspace(1); 90 stop; 91 } 92 // copy current line 93 text = GetBufLine(hbuf, ln); 94 // get string length 95 len = strlen(text); 96 97 if (ipos == len || len == 0) { 98 totalLn = GetBufLineCount (hbuf); 99 lastText = GetBufLine(hBuf, totalLn-1); 100 lastLen = strlen(lastText); 101 if (ipos == lastLen)// end of file 102 stop; 103 ln = ln + 1; // do not use "ln--" for compatibility with older versions 104 nextline = GetBufLine(hbuf, ln); 105 nextlen = strlen(nextline); 106 // combine two lines 107 text = cat(text, nextline); 108 // del two lines 109 DelBufLine(hbuf, ln-1); 110 DelBufLine(hbuf, ln-1); 111 // insert the combined one 112 InsBufLine(hbuf, ln-1, text); 113 // set the cursor position 114 SetBufIns(hbuf, ln-1, len); 115 stop; 116 } 117 num = 1; // del one char 118 if (ipos > 0) { 119 // process Chinese character 120 i = ipos; 121 count = 0; 122 while (AsciiFromChar(text[i-1]) >= 160) { 123 i = i - 1; 124 count = count + 1; 125 if (i == 0) 126 break; 127 } 128 if (count > 0) { 129 // I think it might be a two-byte character 130 num = 2; 131 // This idiot does not support mod and bitwise operators 132 if (((count / 2 * 2 != count) || count == 0) && (ipos < len-1)) 133 ipos = ipos + 1; // adjust cursor position 134 } 135 // keeping safe 136 if (ipos - num < 0) 137 num = ipos; 138 } 139 else { 140 i = ipos; 141 count = 0; 142 while(AsciiFromChar(text) >= 160) { 143 i = i + 1; 144 count = count + 1; 145 if(i == len-1) 146 break; 147 } 148 if(count > 0) { 149 num = 2; 150 } 151 } 152 153 text = cat(strmid(text, 0, ipos), strmid(text, ipos+num, len)); 154 DelBufLine(hbuf, ln); 155 InsBufLine(hbuf, ln, text); 156 SetBufIns(hbuf, ln, ipos); 157 stop; 158 } 159 /*====================================================================== 160 3、左移键——SuperCursorLeft.em 161 ======================================================================*/ 162 macro IsComplexCharacter() 163 { 164 hwnd = GetCurrentWnd(); 165 hbuf = GetCurrentBuf(); 166 if (hbuf == 0) 167 return 0; 168 //当前位置 169 pos = GetWndSelIchFirst(hwnd); 170 //当前行数 171 ln = GetBufLnCur(hbuf); 172 //得到当前行 173 text = GetBufLine(hbuf, ln); 174 //得到当前行长度 175 len = strlen(text); 176 //从头计算汉字字符的个数 177 if(pos > 0) 178 { 179 i=pos; 180 count=0; 181 while(AsciiFromChar(text[i-1]) >= 160) 182 { 183 i = i - 1; 184 count = count+1; 185 if(i == 0) 186 break; 187 } 188 if((count/2)*2==count|| count==0) 189 return 0; 190 else 191 return 1; 192 } 193 return 0; 194 } 195 macro moveleft() 196 { 197 hwnd = GetCurrentWnd(); 198 hbuf = GetCurrentBuf(); 199 if (hbuf == 0) 200 stop; // empty buffer 201 202 ln = GetBufLnCur(hbuf); 203 ipos = GetWndSelIchFirst(hwnd); 204 if(GetBufSelText(hbuf) != "" || (ipos == 0 && ln == 0)) // 第0行或者是选中文字,则不移动 205 { 206 SetBufIns(hbuf, ln, ipos); 207 stop; 208 } 209 if(ipos == 0) 210 { 211 preLine = GetBufLine(hbuf, ln-1); 212 SetBufIns(hBuf, ln-1, strlen(preLine)-1); 213 } 214 else 215 { 216 SetBufIns(hBuf, ln, ipos-1); 217 } 218 } 219 macro SuperCursorLeft() 220 { 221 moveleft(); 222 if(IsComplexCharacter()) 223 moveleft(); 224 } 225 /*====================================================================== 226 4、右移键——SuperCursorRight.em 227 ======================================================================*/ 228 macro moveRight() 229 { 230 hwnd = GetCurrentWnd(); 231 hbuf = GetCurrentBuf(); 232 if (hbuf == 0) 233 stop; // empty buffer 234 ln = GetBufLnCur(hbuf); 235 ipos = GetWndSelIchFirst(hwnd); 236 totalLn = GetBufLineCount(hbuf); 237 text = GetBufLine(hbuf, ln); 238 if(GetBufSelText(hbuf) != "") //选中文字 239 { 240 ipos = GetWndSelIchLim(hwnd); 241 ln = GetWndSelLnLast(hwnd); 242 SetBufIns(hbuf, ln, ipos); 243 stop; 244 } 245 if(ipos == strlen(text)-1 && ln == totalLn-1) // 末行 246 stop; 247 if(ipos == strlen(text)) 248 { 249 SetBufIns(hBuf, ln+1, 0); 250 } 251 else 252 { 253 SetBufIns(hBuf, ln, ipos+1); 254 } 255 } 256 macro SuperCursorRight() 257 { 258 moveRight(); 259 if(IsComplexCharacter()) // defined in SuperCursorLeft.em 260 moveRight(); 261 } 262 /*====================================================================== 263 5、shift+右移键——ShiftCursorRight.em 264 ======================================================================*/ 265 macro IsShiftRightComplexCharacter() 266 { 267 hwnd = GetCurrentWnd(); 268 hbuf = GetCurrentBuf(); 269 if (hbuf == 0) 270 return 0; 271 selRec = GetWndSel(hwnd); 272 pos = selRec.ichLim; 273 ln = selRec.lnLast; 274 text = GetBufLine(hbuf, ln); 275 len = strlen(text); 276 if(len == 0 || len < pos) 277 return 1; 278 //Msg("@len@;@pos@;"); 279 if(pos > 0) 280 { 281 i=pos; 282 count=0; 283 while(AsciiFromChar(text[i-1]) >= 160) 284 { 285 i = i - 1; 286 count = count+1; 287 if(i == 0) 288 break; 289 } 290 if((count/2)*2==count|| count==0) 291 return 0; 292 else 293 return 1; 294 } 295 return 0; 296 } 297 macro shiftMoveRight() 298 { 299 hwnd = GetCurrentWnd(); 300 hbuf = GetCurrentBuf(); 301 if (hbuf == 0) 302 stop; 303 304 ln = GetBufLnCur(hbuf); 305 ipos = GetWndSelIchFirst(hwnd); 306 totalLn = GetBufLineCount(hbuf); 307 text = GetBufLine(hbuf, ln); 308 selRec = GetWndSel(hwnd); 309 curLen = GetBufLineLength(hbuf, selRec.lnLast); 310 if(selRec.ichLim == curLen+1 || curLen == 0) 311 { 312 if(selRec.lnLast == totalLn -1) 313 stop; 314 selRec.lnLast = selRec.lnLast + 1; 315 selRec.ichLim = 1; 316 SetWndSel(hwnd, selRec); 317 if(IsShiftRightComplexCharacter()) 318 shiftMoveRight(); 319 stop; 320 } 321 selRec.ichLim = selRec.ichLim+1; 322 SetWndSel(hwnd, selRec); 323 } 324 macro SuperShiftCursorRight() 325 { 326 if(IsComplexCharacter()) 327 SuperCursorRight(); 328 shiftMoveRight(); 329 if(IsShiftRightComplexCharacter()) 330 shiftMoveRight(); 331 } 332 /*====================================================================== 333 6、shift+左移键——ShiftCursorLeft.em 334 ======================================================================*/ 335 macro IsShiftLeftComplexCharacter() 336 { 337 hwnd = GetCurrentWnd(); 338 hbuf = GetCurrentBuf(); 339 if (hbuf == 0) 340 return 0; 341 selRec = GetWndSel(hwnd); 342 pos = selRec.ichFirst; 343 ln = selRec.lnFirst; 344 text = GetBufLine(hbuf, ln); 345 len = strlen(text); 346 if(len == 0 || len < pos) 347 return 1; 348 //Msg("@len@;@pos@;"); 349 if(pos > 0) 350 { 351 i=pos; 352 count=0; 353 while(AsciiFromChar(text[i-1]) >= 160) 354 { 355 i = i - 1; 356 count = count+1; 357 if(i == 0) 358 break; 359 } 360 if((count/2)*2==count|| count==0) 361 return 0; 362 else 363 return 1; 364 } 365 return 0; 366 } 367 macro shiftMoveLeft() 368 { 369 hwnd = GetCurrentWnd(); 370 hbuf = GetCurrentBuf(); 371 if (hbuf == 0) 372 stop; 373 374 ln = GetBufLnCur(hbuf); 375 ipos = GetWndSelIchFirst(hwnd); 376 totalLn = GetBufLineCount(hbuf); 377 text = GetBufLine(hbuf, ln); 378 selRec = GetWndSel(hwnd); 379 //curLen = GetBufLineLength(hbuf, selRec.lnFirst); 380 //Msg("@curLen@;@selRec@"); 381 if(selRec.ichFirst == 0) 382 { 383 if(selRec.lnFirst == 0) 384 stop; 385 selRec.lnFirst = selRec.lnFirst - 1; 386 selRec.ichFirst = GetBufLineLength(hbuf, selRec.lnFirst)-1; 387 SetWndSel(hwnd, selRec); 388 if(IsShiftLeftComplexCharacter()) 389 shiftMoveLeft(); 390 stop; 391 } 392 selRec.ichFirst = selRec.ichFirst-1; 393 SetWndSel(hwnd, selRec); 394 } 395 macro SuperShiftCursorLeft() 396 { 397 if(IsComplexCharacter()) 398 SuperCursorLeft(); 399 shiftMoveLeft(); 400 if(IsShiftLeftComplexCharacter()) 401 shiftMoveLeft(); 402 } 403 /*---END---*/
原文:http://www.cnblogs.com/qpcegg/p/SourceInsight_ChineseSupport_Macro.html