source: dev/ProjectEditor/Replace.cpp@ 6

Last change on this file since 6 was 3, checked in by dai_9181, 18 years ago
File size: 10.9 KB
Line 
1#include "common.h"
2
3void TextEdit_ResetWordColor(int WndNum,CHARRANGE *pCharRange,int DocType){
4 extern HANDLE hHeap;
5 extern MDIINFO MdiInfo[MAX_WNDNUM];
6 int i,i2,IsStr;
7 char *pBuf,*pTemp;
8 CHARRANGE Range,cr2;
9
10 pBuf=MdiInfo[WndNum].pmti->buffer;
11
12 //行の先頭位置を取得
13 i=pCharRange->cpMin;
14 for(;i>0;i--){
15 if(pBuf[i-1]=='\r'&&pBuf[i]=='\n'){
16 i++;
17 break;
18 }
19 }
20 Range.cpMin=i;
21
22 //行の末端位置を取得
23 i=pCharRange->cpMax;
24 for(;;i++){
25 if(pBuf[i]=='\0') break;
26 if(pBuf[i]=='\r'&&pBuf[i+1]=='\n') break;
27 }
28 Range.cpMax=i;
29
30 //初期化(すべて黒色に戻す)
31 for(i=Range.cpMin;i<Range.cpMax;i++){
32 MdiInfo[WndNum].pmti->pColorRef[i]=tci.rgbDefault;
33 }
34
35 if(DocType==WNDTYPE_TEXT) return;
36
37 if(!bProfessional){
38 //Standard版ではHTMLタグの色分け機能を排除
39 if(DocType==WNDTYPE_HTML) return;
40 }
41
42 pTemp=(char *)HeapAlloc(hHeap,0,Range.cpMax-Range.cpMin+1);
43
44 for(i=Range.cpMin,IsStr=0;i<Range.cpMax;i++){
45 if((IsVariableTopChar(pBuf[i])||pBuf[i]=='#')&&IsStr==0){
46 cr2.cpMin=i;
47 for(i2=0;;i++,i2++){
48 if((!IsVariableChar(pBuf[i]))||pBuf[i]=='.'){
49 pTemp[i2]=0;
50 break;
51 }
52 pTemp[i2]=pBuf[i];
53 }
54 cr2.cpMax=i;
55
56 //予約語(青色)
57 BOOL bResult=0;
58 if(DocType==WNDTYPE_BASIC){
59 i2=IsBasicReservedWord(pTemp);
60 bResult=IsManagementCommand(i2);
61 }
62 else if(DocType==WNDTYPE_HTML) bResult=IsHtmlReservedWord(pTemp);
63
64 if(bResult){
65 for(i2=cr2.cpMin;i2<cr2.cpMax;i2++){
66 MdiInfo[WndNum].pmti->pColorRef[i2]=tci.rgbStatement;
67 }
68 }
69 }
70 if(IsStr||pBuf[i]=='\"'&&IsStr==0){
71 //文字列カラー(""で囲まれる範囲)
72 MdiInfo[WndNum].pmti->pColorRef[i]=tci.rgbString;
73 }
74 if(pBuf[i]=='\0') break;
75 if(pBuf[i]=='\"') IsStr^=1;
76 if(pBuf[i]=='\r'&&pBuf[i+1]=='\n'){
77 i++;
78 IsStr=0;
79 }
80 }
81
82 HeapDefaultFree(pTemp);
83}
84
85void TextEdit_Replace(int WndNum,char *str,BOOL bRedraw){
86 extern HANDLE hHeap;
87 extern MDIINFO MdiInfo[MAX_WNDNUM];
88 CHARRANGE Range,DelRange;
89
90 TextEdit_GetSel(WndNum,&DelRange);
91
92 Range.cpMin=DelRange.cpMin;
93 Range.cpMax=DelRange.cpMin+lstrlen(str);
94
95 //バッファ長を取得
96 int length;
97 length=lstrlen(MdiInfo[WndNum].pmti->buffer);
98
99 int delta;
100 delta=DelRange.cpMin-DelRange.cpMax+(Range.cpMax-Range.cpMin);
101 if(0<delta){
102 ////////////////////
103 // バッファを再確保
104 ////////////////////
105
106 MdiInfo[WndNum].pmti->buffer=(char *)HeapReAlloc(hHeap,
107 0,
108 MdiInfo[WndNum].pmti->buffer,
109 length+1+delta);
110
111 MdiInfo[WndNum].pmti->pColorRef=(COLORREF *)HeapReAlloc(hHeap,
112 0,
113 MdiInfo[WndNum].pmti->pColorRef,
114 (length+1+delta)*sizeof(COLORREF));
115 }
116
117
118 //ブレークポイントのオフセット分を計算
119 if(ProjectInfo.name[0]){
120 ProjectInfo.pobj_DBBreakPoint->replace(
121 MdiInfo[WndNum].path,
122 MdiInfo[WndNum].pmti->buffer,
123 &DelRange,
124 &Range,
125 str);
126 }
127 else{
128 extern CDBBreakPoint *pobj_DBBreakPoint;
129 pobj_DBBreakPoint->replace(
130 MdiInfo[WndNum].path,
131 MdiInfo[WndNum].pmti->buffer,
132 &DelRange,
133 &Range,
134 str);
135 }
136
137
138 //文字列バッファをスライドして、挿入
139 SlideString(MdiInfo[WndNum].pmti->buffer+DelRange.cpMax,
140 delta);
141 memcpy(MdiInfo[WndNum].pmti->buffer+Range.cpMin,str,lstrlen(str));
142
143 //文字カラーバッファをスライド
144 SlideBuffer(((char *)MdiInfo[WndNum].pmti->pColorRef)+DelRange.cpMax*sizeof(COLORREF),
145 (length-DelRange.cpMax) * sizeof(COLORREF),
146 delta*sizeof(COLORREF));
147 //色づけ
148 TextEdit_ResetWordColor(WndNum,&Range,MdiInfo[WndNum].DocType);
149
150 //新しいキャレット位置に変更
151 GetCaretPosFromBufferIndex(MdiInfo[WndNum].pmti->buffer,
152 Range.cpMax,
153 &MdiInfo[WndNum].pmti->StartCaretPos);
154 MdiInfo[WndNum].pmti->EndCaretPos=MdiInfo[WndNum].pmti->StartCaretPos;
155
156 //コントロールタブスペースの幅と行番号の文字数を更新
157 MdiInfo[WndNum].pmti->iWidth_ControlTabSpace=
158 GetControlTabSpace(WndNum,&MdiInfo[WndNum].pmti->iLineNumberTextCount);
159
160 if(bRedraw){
161 ///////////////
162 // 再描画
163 ///////////////
164
165 if(!TextEdit_ScrollCaret(WndNum,0)){
166 UpdateWindow(MdiInfo[WndNum].pmti->hEdit); //ちらつき防止
167 InvalidateRect(MdiInfo[WndNum].pmti->hEdit,NULL,0);
168 }
169
170 ResetCaretPos(WndNum,1);
171 }
172
173 //上下キャレット移動時の左右位置保持機能を解除
174 MdiInfo[WndNum].pmti->Temp_UpDown_CaretXPos=-1;
175
176 //編集メニューをリセット
177 ResetState_EditMenu();
178
179 //変更フラグをセット
180 MdiInfo[WndNum].pmti->bModify=1;
181
182
183 if(ProjectInfo.name[0]){
184 //プロジェクトが開かれているとき
185 if(MdiInfo[WndNum].DocType==WNDTYPE_BASIC){
186 //クラスツリーを再表示
187 ResetClassTree(GetFileNum(WndNum));
188 }
189 }
190}
191void TextEdit_ReplaceUpdateUndoData(int WndNum,char *str,BOOL bPaste,BOOL bRedraw){
192 extern HANDLE hHeap;
193 extern MDIINFO MdiInfo[MAX_WNDNUM];
194 int Command;
195 char *KeyStr,*DelStr;
196 char temporary[255];
197 CHARRANGE Range,DelRange;
198
199 if(MdiInfo[WndNum].IndentStr){
200 //自動インデントを有効にする
201 lstrcpy(temporary,MdiInfo[WndNum].IndentStr);
202
203 CancelBeforeAutoIndent(WndNum);
204 TextEdit_ReplaceUpdateUndoData(WndNum,temporary,0,1);
205 }
206
207 TextEdit_GetSel(WndNum,&DelRange);
208 if(DelRange.cpMin!=DelRange.cpMax){
209 Command=TEXTEDIT_UNDO_SELKEY;
210 DelStr=(char *)HeapAlloc(hHeap,0,DelRange.cpMax-DelRange.cpMin+1);
211
212 TextEdit_GetSelText(WndNum,&DelRange,DelStr);
213
214 Range.cpMin=DelRange.cpMin;
215 Range.cpMax=DelRange.cpMin;
216 }
217 else{
218 if(bPaste) Command=TEXTEDIT_UNDO_PASTE;
219 else Command=TEXTEDIT_UNDO_KEY;
220 DelStr=0;
221
222 Range=DelRange;
223 }
224 if(str[0]){
225 KeyStr=(char *)HeapAlloc(hHeap,0,lstrlen(str)+1);
226 lstrcpy(KeyStr,str);
227 Range.cpMax+=lstrlen(KeyStr);
228 }
229 else KeyStr=0;
230 TextEdit_NoticeChanging(WndNum,Command,KeyStr,DelStr,&Range,&DelRange);
231
232 TextEdit_Replace(WndNum,str,bRedraw);
233}
234
235
236void TextEdit_Convert(int id){
237 int i,i2;
238
239 int WndNum;
240 WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
241
242 CHARRANGE CharRange;
243 int length;
244 TextEdit_GetSel(WndNum,&CharRange);
245
246#ifdef THETEXT
247 if(id==IDM_CONV_HASRETURN||id==IDM_CONV_QUOTE){
248#else
249 if(id==IDM_CODEFORMAT){
250#endif
251 char *pBuf;
252 pBuf=MdiInfo[WndNum].pmti->buffer;
253
254 //行全体を選択(min)
255 for(i=CharRange.cpMin;i>0;i--){
256 if(pBuf[i-1]=='\r'&&pBuf[i]=='\n'){
257 i++;
258 break;
259 }
260 }
261 CharRange.cpMin=i;
262
263 //行全体を選択(max)
264 for(i=CharRange.cpMax;;i++){
265 if(pBuf[i]=='\r'&&pBuf[i+1]=='\n'||pBuf[i]=='\0') break;
266 }
267 CharRange.cpMax=i;
268
269 TextEdit_SetSel(WndNum,CharRange.cpMin,CharRange.cpMax,0);
270 }
271
272 length=CharRange.cpMax-CharRange.cpMin;
273
274 char *buffer,*temporary;
275 buffer=(char *)HeapAlloc(hHeap,0,length*3+1024);
276 temporary=(char *)HeapAlloc(hHeap,0,length*3+1024);
277 memcpy(buffer,MdiInfo[WndNum].pmti->buffer+CharRange.cpMin,length);
278 buffer[length]=0;
279
280 int x=0;
281 switch(id){
282 case IDM_CONV_ALPHA_SMALL:
283 CharLower(buffer);
284 break;
285 case IDM_CONV_ALPHA_BIG:
286 CharUpper(buffer);
287 break;
288 case IDM_CONV_HALF:
289 LCMapString(GetUserDefaultLCID(),LCMAP_HALFWIDTH,buffer,length+1,temporary,length*2+1);
290 lstrcpy(buffer,temporary);
291 break;
292 case IDM_CONV_MULTI:
293 LCMapString(GetUserDefaultLCID(),LCMAP_FULLWIDTH,buffer,length+1,temporary,length*2+1);
294 lstrcpy(buffer,temporary);
295 break;
296 case IDM_CONV_KATAKANA:
297 LCMapString(GetUserDefaultLCID(),LCMAP_KATAKANA,buffer,length+1,temporary,length*2+1);
298 lstrcpy(buffer,temporary);
299 break;
300 case IDM_CONV_HIRAGANA:
301 LCMapString(GetUserDefaultLCID(),LCMAP_HIRAGANA,buffer,length+1,temporary,length*2+1);
302 lstrcpy(buffer,temporary);
303 break;
304 case IDM_CONV_SPACE:
305 temporary=(char *)HeapReAlloc(hHeap,0,temporary,length*pobj_nv->TabSize+1);
306 buffer=(char *)HeapReAlloc(hHeap,0,buffer,length*pobj_nv->TabSize+1);
307 for(i=0,i2=0;;i++,i2++){
308 if(buffer[i]=='\t'){
309 memset(temporary+i2,' ',pobj_nv->TabSize);
310 i2+=pobj_nv->TabSize-1;
311 continue;
312 }
313 temporary[i2]=buffer[i];
314 if(buffer[i]=='\0') break;
315 }
316
317 lstrcpy(buffer,temporary);
318 break;
319 case IDM_CONV_TAB:
320 char szSpaceTemp[255];
321 memset(szSpaceTemp,' ',pobj_nv->TabSize);
322 szSpaceTemp[pobj_nv->TabSize]=0;
323
324 for(i=0,i2=0;;i++,i2++){
325 if(memcmp(buffer+i,szSpaceTemp,pobj_nv->TabSize)==0){
326 temporary[i2]='\t';
327 i+=pobj_nv->TabSize-1;
328 continue;
329 }
330 temporary[i2]=buffer[i];
331 if(buffer[i]=='\0') break;
332 }
333
334 lstrcpy(buffer,temporary);
335 break;
336
337#ifdef THETEXT
338 case IDM_CONV_HASRETURN:
339 //折り返し改行を挿入
340 int i3;
341 for(i=0,i2=0,x=0;;i++,i2++,x++){
342 if(buffer[i]=='\r'&&buffer[i+1]=='\n'){
343 temporary[i2++]=buffer[i++];
344 temporary[i2]=buffer[i];
345 x=-1;
346 continue;
347 }
348 if(x>pobj_nv->iMaxOneLineTextLength-2){
349 //自動改行(英単語を考慮)
350 int i3=0;
351 while(IsVariableChar(buffer[i-1])){
352 i--;
353 i2--;
354 x--;
355 i3++;
356 if(x==1){
357 i+=i3;
358 i2+=i3;
359 x+=i3;
360 break;
361 }
362 }
363 if(i3==1){
364 i++;
365 i2++;
366 x++;
367 }
368 temporary[i2++]='\r';
369 temporary[i2]='\n';
370 i--;
371 x=-1;
372 continue;
373 }
374
375 if(IsDBCSLeadByte(buffer[i])){
376 temporary[i2++]=buffer[i++];
377 temporary[i2]=buffer[i];
378 x++;
379 }
380 else{
381 temporary[i2]=buffer[i];
382
383 if(buffer[i]=='\t'){
384 //タブ文字
385 int tab;
386 tab=pobj_nv->TabSize;
387
388 if(x%tab==0) i3=tab;
389 else i3=tab-x%tab;
390 x+=i3-1;
391 }
392 }
393
394 if(buffer[i]=='\0') break;
395 }
396
397 lstrcpy(buffer,temporary);
398 break;
399 case IDM_CONV_QUOTE:
400 //引用文に変換
401 for(i=0,i2=0,x=0;;i++,i2++,x++){
402 if(x==0){
403 //"> " を挿入
404 temporary[i2++]='>';
405 temporary[i2++]=' ';
406 }
407 if(buffer[i]=='\r'&&buffer[i+1]=='\n'){
408 temporary[i2++]=buffer[i++];
409 temporary[i2]=buffer[i];
410 x=-1;
411 continue;
412 }
413 if(x>pobj_nv->iMaxOneLineTextLength-4){
414 //自動改行(英単語を考慮)
415 int i3=0;
416 while(IsVariableChar(buffer[i-1])){
417 i--;
418 i2--;
419 x--;
420 i3++;
421 if(x==1){
422 i+=i3;
423 i2+=i3;
424 x+=i3;
425 break;
426 }
427 }
428 if(i3==1){
429 i++;
430 i2++;
431 x++;
432 }
433 temporary[i2++]='\r';
434 temporary[i2]='\n';
435 i--;
436 x=-1;
437 continue;
438 }
439
440 if(IsDBCSLeadByte(buffer[i])){
441 temporary[i2++]=buffer[i++];
442 temporary[i2]=buffer[i];
443 x++;
444 }
445 else{
446 temporary[i2]=buffer[i];
447 if(buffer[i]=='\t'){
448 //タブ文字
449 int tab;
450 tab=pobj_nv->TabSize;
451
452 if(x%tab==0) i3=tab;
453 else i3=tab-x%tab;
454 x+=i3-1;
455 }
456 }
457
458 if(buffer[i]=='\0') break;
459 }
460
461 lstrcpy(buffer,temporary);
462 break;
463
464#else //ProjectEditor
465
466 case IDM_CODEFORMAT:
467 char *temp2;
468 temp2=CodeFormatter(buffer);
469 HeapDefaultFree(buffer);
470 buffer=temp2;
471 break;
472
473#endif
474 }
475
476
477
478 //テキストエディタの文字列をリプレイス
479 TextEdit_ReplaceUpdateUndoData(WndNum,
480 buffer,
481 1,
482 1);
483
484 HeapDefaultFree(buffer);
485 HeapDefaultFree(temporary);
486
487 //キャレット位置までスクロールする
488 TextEdit_ScrollCaret(WndNum,0);
489 ResetCaretPos(WndNum);
490}
Note: See TracBrowser for help on using the repository browser.